Linux system virtual memory space

  
        

First borrow a picture online, I feel this is clear:

Linux system virtual memory space general layout diagram

1.1 linear space

linear address space: refers to The entire 4GB virtual storage space from 0x00000000 to 0xFFFFFFFF in Linux systems. Linear space is divided into user space and kernel space.

1.1.1 User Space (Process Address Space)

User Space refers to a linear address space of 3GB from 0x00000000 to 0xBFFFFFFF. Each process has an independent 3GB user space, so User space is unique to each process, but the kernel thread has no user space because it does not generate user space addresses. In addition, the child space shared (inherited) by the child process is only the same as the parent user's linear address to the physical memory address, rather than the shared parent user space. Users running in both user mode and kernel mode can access user space.

linux uses virtual memory management technology, each process has a 3G size independent process address space, this address space is user space. The user space of each process is completely independent and irrelevant. How the process accesses kernel space: system calls and interrupts.

Process-related operations such as creating processes require memory to be allocated to the process. At this time, the process is not a physical address, but a virtual address. The actual physical memory is only when the process actually accesses the newly acquired virtual address, and then the "page mechanism" generates "missing" exception, and thus enters the program that allocates the actual page frame. This exception is the basic guarantee for the existence of the virtual memory mechanism. It tells the kernel to allocate physical pages for the process and establish the corresponding page table. After that, the virtual address is actually mapped to the physical address.

1.1.2 Kernel Space

Kernel space represents code or data running in the highest level of the processor's supervisor mode. Kernel space occupies 1GB of linearity from 0xC0000000 to 0xFFFFFFFF. The address space, the kernel linear address space is shared by all processes, but only the processes running in the kernel state can access. The user process can switch to the kernel state to access the kernel space through the system call. The address generated when the process runs in the kernel state belongs to the kernel. space.

Kernel space can be divided into the following linear spaces:

1. The kernel logical address space

is from PAGE_OFFSET(3G)3G+896) The linear address space is the system physical memory map area that maps all or part of the physical memory (if the system contains high-end memory). The address in the kernel logical address space is only one fixed offset (3G) from the corresponding address in the RAM memory physical address space. If the RAM memory physical address space is addressed from the 0x00000000 address, then the offset is PAGE_OFFSET.

2. High-end linear address space: The linear address space from high_memory (3G+896M) to 0xFFFFFFFF belongs to the high-end linear address space, where linear address between VMALLOC_START~VMALLOC_END: ​​(1) is vmalloc() The function is used to allocate high-end physical memory that is physically discontinuous but continuous in linear address space, or (2) used by the vmap() function to map high-end or low-end physical memory, or (3) by Ioremap() function to remap I. /O physical space. The LAST_PKMAP (generally equal to 1024) page linear address space where PKMAP_BASE starts: used by the kmap() function to permanently map high-end physical memory. FIXADDR_START starts with KM_TYPE_NR*NR_CPUS page linear address space: used by the kmap_atomic() function to temporarily map high-end physical memory. Other unused high-end linear address spaces can be used to permanently map the I/O address space during system initialization.

1.2 Physical space 1.2.1 Low-end memory (physical memory)

The physical memory mapped by the kernel logical address space is the low-end memory (the actual physical memory size, but less than 896), low The end memory always has a permanent one-to-one corresponding kernel logical address in the Linux linear address space. During the system initialization process, the low-end memory is permanently mapped to the kernel logical address space, and a virtual mapping page table is established for the low-end memory. The conversion between the physical address and the linear address of the physical memory in the low-end memory can be performed by two macros __pa(x) and __va(x), #define __pa(x) ((unsigned long)(x)- PAGE_OFFSET) __pa(x) converts the address x of the kernel logical address space into the corresponding physical address, which is equivalent to __virt_to_phys((unsigned long)(x)), __va(x), on the contrary, the address of the low-end physical memory space Converted to the corresponding kernel logical address, equivalent to ((void *)__phys_to_virt((unsigned long)(x)))).

1.2.2 High-end memory (physical memory)

The physical memory above the low-end memory address is high-end memory (above physical memory 896), and the high-end memory is not in the Linux linear address space. There is no fixed one-to-one corresponding kernel logical address. During the system initialization process, the mapping page table will not be mapped to the Linux linear address space, but the high-end physical memory will be used for the allocated high-end physical memory. Map the page table so that it can be used by the kernel, otherwise it cannot be used. The conversion of the physical address of the upper memory to the linear address cannot use the __pa(x) and __va(x) macros above.

The origin of the high-end memory concept: As mentioned above, Linux divides the 4GB linear address space into two parts, from 0x00000000 to 0xBFFFFFFF a total of 3GB space as user space exclusive to the user process, this part of the linear address space does not Fixed mapping to physical memory space; 4GB linear address space from 0xC0000000 to 0xFFFFFFFF as kernel space, in embedded systems, this part of the linear address space in addition to mapping physical memory space to map the processor internal peripheral register space I/O space. The kernel logical address space between 0xC0000000~high_memory is dedicated to fix the physical memory in the mapping system, that is, the space between 0xC0000000~high_memory is the same as the physical memory space of the system (of course, the non-continuous configuration of the CONFIG_DISCONTIGMEMD option is configured. In the memory system, the kernel logical address space and the physical memory space may have memory holes. If the physical memory capacity in the system is much smaller than 1 GB, then there is a high_memory~0xFFFFFFFF between the kernel logical address space in the kernel linear address space. There is enough space to map some I/O space. However, if the physical memory capacity (including memory holes) in the system is greater than 1GB, then there is not enough kernel linear address space to fix all physical memory and some I/O space of the mapping system. In order to solve this problem, in x86 processor The platform sets an experience value: 896MB, that is, if the physical memory (including memory holes) in the system is greater than 896MB, then the first 896MB physical memory is fixedly mapped to the kernel logical address space 0xC0000000~0xC0000000+896MB (=high_memory). The physical memory after 896MB does not establish a fixed mapping to the kernel linear address space. This part of the memory is called high-end physical memory. At this time, the 128MB space between the kernel linear address space high_memory~0xFFFFFFFF is called the high-end memory linear address space, which is used to map high-end physical memory and I/O space. 896MB is the experience value of x86 processor platform, leaving 128MB linear address space to map high-end memory and I/O address space. In embedded systems, this threshold can be modified according to specific conditions. For example, MIPS sets this value to 0x20000000B. (512MB), then only when the physical memory space capacity in the system is greater than 0x20000000B, the kernel needs to configure the CONFIG_HIGHMEM option to enable the kernel to allocate and map high-end memory. The conditions for setting up high-end physical memory and high-end physical memory thresholds are described in the memory area concept above.

Copyright © Windows knowledge All Rights Reserved