Linux user-state and kernel-state memory management technology

Source: Internet
Author: User

Usually the program Access address is a virtual address, with a three-bit operating system, access to the address space is 4G,linux divides 4G into two parts. 1 , where 0~3g is the user space and3~4g is the kernel space. Through the MMU , both parts of the space can be accessed to the actual physical memory.

the process can only access the 0~3g in the user state, only access the kernel state 3g~4g
* process enters kernel state via system call
* The 3g~4g portion of each process virtual space is the same
* process from the user state into the kernel state will not cause CR3 changes but will cause the stack changes


Figure 1


1 mapping relationships between virtual addresses and physical addresses

page as the basic mapping unit, the size of a page is generally 4k , user space is not particularly complex, but the kernel space will 1g virtual space by region is divided into 3 parts: ZONE_DMA ( memory starts with 16MB   zone_normal ( 16MB~896MB , zone_highmem ( 896MB ~ end ) .

Why should there be a concept of high-end memory?

If the logical address 0xc0000003 corresponds to the physical address of 0x3,0xc0000004 The corresponding physical address is 0x4,... , the relationship between the logical address and the physical address is

Physical Address = Logical Address –0xc0000000

Logical Address

Physical memory Address

0xc0000000

0 x0

0xc0000001

0 X1

0xc0000002

0 X2

0xc0000003

0 X3

...

...

0xe0000000

0 x20000000

...

...

0xFFFFFFFF

0 x40000000??

Fakewith the simple address mapping above, the kernel logical address space is accessed as0xc0000000 ~ 0xFFFFFFFF, then the corresponding physical memory range is0x0 ~ 0x40000000, which can only be accessed1Gphysical memory. If the machine is installed8Gphysical memory, the kernel can only access the previous1Gphysical memory, back7Gphysical memory will not be accessible because the kerneladdress space has all been mapped to the physical memory address range0x0 ~ 0x40000000. Even if you have installed8Gphysical memory, then the physical address is0x40000001memory, how to access the kernel? You must have a memory logical address in your codeof,0xc0000000 ~ 0xFFFFFFFFThe address space has been exhausted, so the physical address cannot be accessed0x40000000memory for the future


Figure 2

Figure 3


   earlier we explained the origin of high-end memory. LinuxDivide the kernel address space into three partsZONE_DMA,Zone_normaland theZone_highmem, high-end memoryHigh_memThe address space range is0xf8000000 ~ 0xFFFFFFFF(896MB~1024MB). So how does the kernel use 128MB High-end memory address space is how to implement access to all physical memory ?

when the kernel wants to access more than 896MB Physical Address memory, from 0xf8000000 ~ 0xFFFFFFFF find a corresponding size free logical address space within the range of the address space and borrow it for a while. Using this logical address space, create a map to the physical memory that you want to access (that is, populate the kernel PTE page table), temporarily for a while, and then return when you're done. This allows others to use the address space to access other physical memory, allowing access to all physical memory using a limited address space. 3.

2 Process switching issues in user space and kernel space

When the kernel creates a process, the colleague who creates the task_struct creates the appropriate stack for the process. Each process will have two stacks, a user stack, exist in the user space, a kernel stack, exist in the kernel space. When the process is running in user space, The contents of the CPU stack pointer register are the user stack address, the user stack is used, and The contents of the CPU stack pointer register are the kernel stack space address when the process is in kernel space. , using the kernel stack.

When a process falls into a kernel state because of an outage or a system call, the stack used by the process goes from the user stack to the kernel stack.

After the process is in the kernel state, the address of the user-state stack is stored in the kernel stack, then the contents of the stack pointer register are set to the address of the kernel stack, which completes the conversion of the user stack to the kernel stack, and when the process recovers from the kernel state to the user state, At the end of the kernel-state line, the address of the user stack stored in the kernel stack is restored to the stack pointer register. This enables the core stack and the user stack of the mutual transfer.

So, we know that the address of the user stack when it goes from the kernel to the user state is stored in the kernel stack when it is trapped in the kernel, but how do we know the address of the kernel stack when we get into the kernel?

The key is that the kernel stack of the process is always empty when the process goes from the user state to the kernel state. This is because, when the process is running in the user state, the user stack is used, when the process falls into the kernel state, the kernel stack holds the information about the kernel state running, but once the process returns to the user state, the information stored in the kernel stack is invalid and will be restored. So every time the process gets into the kernel from the user state, the kernel stack is empty. So when the process is in the kernel, the stack top address of the kernel stack is given directly to the stacking pointer register.

Linux user-state and kernel-state memory management technology

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.