Linux user space and kernel space

Source: Internet
Author: User
From the above, we know that 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 "kernel space ". 3G bytes (from the virtual address 0x00000000 to 0 xbfffffff) are used by various processes, 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.
Linux uses two-level protection mechanisms: Level 0 for Kernel use and Level 3 for users Program . As you can see (the figure cannot be shown here), 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 is stored in the kernel space. Code And data, while the user space of the Process stores the code and data of the user program. Both kernel space and user space are in virtual space. Readers will 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.

Let's take a look at the description and definition of address ing in the kernel space in include/ASM/i386/page. h:
* This handles the memory map... We cocould make this a config
* Option, but too needed people screw it up, and too few need
* It.
* A _ page_offset of 0xc0000000 means that the kernel has
* A virtual address space of one gigabyte, which limits
* Amount of physical memory you can use to about 950 MB.
* If you want more physical memory than this then see the config_highmem4g
* And config_highmem64g options in the Kernel configuration.

# DEFINE _ page_offset (0xc0000000)
# Define page_offset (unsigned long) _ page_offset)
# DEFINE _ Pa (x) (unsigned long) (x)-page_offset)
# DEFINE _ VA (x) (void *) (unsigned long) (x) + page_offset ))
Source code Note 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. We will not consider this situation for the moment. If the physical memory is less than 950 MB, a virtual address X with the physical address "X-
Page_offset ", given a physical address X, its virtual address is" x + page_offset ".
It is explained again 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, we call the kernel code and Data
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, % Convention": "R" (_ Pa (next-& gt; 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.

Related Article

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: 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.