Kernel space and user space, kernel state and user State (reprinted)
Kernel space and user space
Linux simplifies the segmentation mechanism so that the virtual address and linear address are always the same. Therefore, the virtual address space in Linux is also 0 ~ 4G. The Linux kernel divides the space of 4G bytes into two parts. The maximum 1 GB (from the virtual address 0xc0000000 to 0 xffffffff) is used by the kernel, which is called the "kernel space ". 3G bytes (from the virtual address 0x00000000 to 0 xbfffffff) are used by each process, which is called "user space ). Because each process can enter the kernel through a system call, the Linux kernel is shared by all processes in the system. Therefore, from the perspective of a specific process, each process can have 4 GB of virtual space. Each process has its own private user space (0 ~ 3G), this space is invisible to other processes in the system. The maximum 1 GB virtual kernel space is shared by all processes and kernels.
1. ing between virtual kernel space and physical space
The kernel space stores kernel code and data, while the process user space stores the code and data of the user program. Both kernel space and user space are in virtual space. Someone may ask, when the system is started, isn't the kernel code and data loaded into the physical memory? Why are they also in virtual memory? This is related to the compilation program. We will understand this point later through a detailed discussion.
Although the kernel space occupies a maximum of 1 GB in each virtual space, the ing to physical memory always starts from the lowest address (0x00000000. For kernel space, its address ing is a very simple linear ing. 0xc0000000 is the displacement between physical addresses and linear addresses. in Linux code, it is called page_offset.
The ing between the virtual address space of the kernel and the physical address space can be seen in include/ASM/i386/page. H to the description and definition of address ing in the kernel space :/*
143 /*
144 * This handles the memory map .. We cocould make this a config
145 * option, but too needed people screw it up, and too few need
146 * It.
147 *
148 * A _ page_offset of 0xc0000000 means that the kernel has
149 * a virtual address space of one gigabyte, which limits
150 * Amount of physical memory you can use to about 950 MB.
151 *
152 * if you want more physical memory than this then see the config_highmem4g
153 * And config_highmem64g options in the Kernel configuration.
154 */
...
173 # DEFINE _ page_offset config_page_offset
...
# DEFINE _ Pa (x) (unsigned long) (x)-page_offset)
# DEFINE _ VA (x) (void *) (unsigned long) (x) + page_offset ))
Note in source code that if your physical memory is greater than 950 MB, you need to add the config_highmem4g and config_highmem64g options when compiling the kernel. If the physical memory is less than 950 MB, then for kernel space, given a virtual address X, its physical address is "x-PAGE_OFFSET", given a physical address X, its virtual address is "x + page_offset ".
It should be noted that macro _ Pa () only maps the virtual address of a kernel space to a physical address, and is not applicable to user space. The address ing of user space is much more complicated.
2. kernel Image
In the following description, the kernel code and data are called the kernel image ). When the system starts, the Linux kernel image is installed at the starting point of the physical address 0x00100000, that is, the interval starting from 1 MB (1 MB for use ). However, during normal operation, the entire kernel image should be in the virtual kernel space. Therefore, when the connection program connects to the kernel image, it adds an offset page_offset to all the symbolic addresses. In this way, the starting address of the kernel image in the kernel space is 0xc0100000.
For example, the process's page Directory PGD (which belongs to the kernel data structure) is in the kernel space. During process switching, you need to set the register S3. the starting address of this directory is a virtual address in the kernel space, however, the physical address is required for the request. In this case, _ Pa () is used for address conversion. In mm_context.h, there is such a line of statements:
ASM volatile ("movl % 0, % Cr": "R" (_ Pa (next-> PGD ));
This is a line of embedded assembly code, which means to convert the starting address next_pgd of the page Directory of the next process into a physical address through _ Pa () and store it in a register, then, use the mov command to write it into the 33rd register. After the processing of this line of statements, Cr 3 points to the new process next page Directory table PGD.
Differences between kernel mode and user mode
When a task (process) executes a system call and is executed in the kernel code, it is called that the process is in the kernel running state (or kernel state for short ). At this time, the processor is executed in the kernel code with the highest privilege level (level 0. When the process is in the kernel state, the kernel stack of the current process is used for the executed kernel code. Each process has its own kernel stack. When a process executes the user's code, it says it is in the user running state (User State ). That is, the processor runs in the (3) User code with the lowest privilege level. When a user program is being executed and suddenly interrupted, the user program can also be symbolically called in the kernel state of the process. Because the interrupt handler will use the kernel stack of the current process. This is similar to the status of processes in the kernel state.
1. Enter the core State when calling the system. Linux's hardware operations can only be performed in the core State, which can be controlled by writing a driver. Operating hardware in user mode will cause core dump.
2. differentiate between system calls and general functions. System calls are provided by the kernel, such as read (), write (), and open. General functions are provided by the function libraries in the software package, such as sin () and cos. There is no difference in syntax between the two.
3. Generally, system calls run in the core State and functions run in the user State. However, some functions use system calls (such as fopen) internally. Such functions enter the core State when calling the system call, while others run in the user State.
Generally, when a user program calls the system's API, it will interrupt and enter the kernel-state API. After the processing is complete, it will quit with the interrupt and return the user-State call function.
User API --> interrupt --> kernel API --> interrupt
---------------------------------------------------------------------
Simply put, a process starts to execute the kernel code because it executes the system call. We say that the process is in the kernel state. The code of a process executing the application itself indicates that the process is in the user State.
The CPU of intel X86 architecture is divided into several operation levels, from 0-3, 0 is the highest level, 3 is the lowest level
There are many restrictions for different levels. For example, the traditional in and out commands are port Input and Output commands, which can be used at level 0, but it cannot be used in three levels. If you use it, it will generate a trap to tell you that an error has occurred. Of course, there are many restrictions, not just this.
This feature is used in the operating system. When the operating system runs its own code, the CPU is cut into 0 levels. When the user runs the program, it only runs at three levels, in this way, if the user's program wants to do anything that damages the system, it will not be able to do it.
Of course, low-level programs cannot upgrade themselves to high-level ones. That is to say, if a user runs at three levels and wants to change himself to zero level, he cannot do it unless the operating system helps, with this feature, the operating system can control the operation of all programs to ensure system security. the level of the Operating System Runtime is usually called the kernel state (because it is the status of the operating system kernel runtime), and the level of the normal user Runtime is called the user State...
When the operating system is started, the CPU is in the real mode, and then it is equivalent to 0, so the operating system automatically obtains the highest permission and switches to the protection mode to 0, at this time, the operating system took the lead and became the highest level operator. Since your program is loaded by the operating system, when it loads you up, set your running status to three levels, that is, the lowest level, and then let you run, so there is no way, you can only run at the lowest level, the reason is that the operating system can manage user programs and kill user programs in kernel mode.
Operating System-user space and kernel space, user State and kernel state