How to view the user space and kernel space of Linux operating system

Source: Internet
Author: User
Tags least privilege

As the central core processing unit of the CPU, in addition to the continuous improvement of production technology, the processing of data and response speed also need to have a trade-off. A slight microcomputer principle of the people know that the Intel X86 system CPU provides four kinds of privileged mode ring0~ring3, which ring0 the highest privilege, ring3 the least privilege, the reason to make such a distinction is to protect the resources, in general, the resources to protect is nothing more than "memory , I/O ports, and the ability to execute special machine instructions ". At any one time, the x86 CPU is running in a certain privileged mode. Similarly, there are seven operating modes for the CPUs of the arm system: User mode (USR), fast interrupt mode (FIQ), interrupt mode (IRQ), Management mode (SVC), System mode (SYS), data access termination mode (ABT), and undefined instruction termination mode (und). In addition to user mode, the other 6 working modes are in privileged mode, while the other 5 modes in privileged mode are referred to as exception modes in addition to system mode.

So what does this feature of the underlying hardware CPU have to do with the user-mode and kernel-mode we're going to discuss? Let's make the original traceability to correct the understanding of the operating system. Every teacher who teaches the operating system course will tell you that the so-called operating system is a system for unified management of hardware resources and a software system to provide users with business interfaces. In layman's terms, "the operating system is to be able to manage your CPU and memory, drive your hard drive, manage your network card and monitor, and then respond correctly when you click the mouse and tap the keyboard." "It's plain enough."

So the question is, in the case of CPUs, if you are the designer of an operating system, how would you handle the differences in different CPU architectures (such as Intel, ARM, AMD, MIPS, etc.)? Of course, this problem is not the average person can hold, CU forum in the development of Dujiangyan operating system that Daniel predecessor really deserves to let Wu Bei admire and admiration. To continue our topic, that is to say, different operating systems are not the same in dealing with the differences in CPU architectures, and some operating systems provide such mechanisms as user-mode and kernel-mode (such as Linux), and some operating systems do not have a user-state or kernel-state distinction (such as VxWorks). The latter is not in our discussion, interested children's shoes can go to Google on their own.

In the case of Linux and Intel x86 CPUs, Linux uses RING0 mode to run kernel-state code, RING3 to run user-state code, RING1 and ring2 are not used. 32 for the operating system addressable range is 0~4GB, this Linux system divides it into the user space and the kernel space two parts:

Where user space occupies a low 3GB space, ranging from the virtual address of 0x00000000 to 0xBFFFFFFFF, the kernel space occupies a high level of 1GB space, the virtual address range from 0xc0000000 to 0xFFFFFFFF.

The kernel space contains the entire kernel code and all kernel modules, as well as the data maintained by the kernel. When the user runs a program, the process is created by the program is running in user space, when it to perform network send data and other actions, must be called by the write, send and other system functions to complete, these system calls will call the kernel code to complete the user's request operation, At this point the CPU must switch from RING3 to RING0, then go to the kernel address space to execute the code to complete the operation, and then switch back to Ring3, back to the user state.

Therefore, we can see that the user-mode and kernel-mode operating system, the user-state program can not arbitrarily manipulate the kernel address space in the data, with a certain degree of security protection, at the same time, in the context of a little bit of sacrifice. Also means that, as long as the kernel code is not a problem, user space program errors and bugs generally do not cause the system crashes, improve the robustness of the system.

In this way, the kernel does compare "pull", and Linux provides some mechanism for user-space and kernel-space communication so that we can develop more efficient programs. But keep in mind that a small error in the kernel can cause the entire system to crash, so be careful when developing kernel programs.

How to view the user space and kernel space of Linux operating system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.