Linux Memory Management-physical and virtual storage space layout in Linux

Source: Internet
Author: User
On a 32-bit CPU platform supporting MMU, the addresses of physical and virtual buckets in Linux are from 0x00000000

On a 32-bit processor platform supporting MMU, the addresses of physical and virtual buckets in Linux systems are from 0x00000000 to 0 xFFFFFFFF, 4 GB in total, however, the physical storage space is completely different from the virtual storage space layout. Linux runs in a virtual bucket and maps the physical memory that actually exists in the system to the entire 4 GB virtual bucket according to different requirements.

Physical storage layout

The physical storage space layout of Linux is related to the processor.

Note:

1) the maximum node number n cannot be greater than the MAX_NUMNODES-1.

2) MAX_NUMNODES indicates the maximum number of nodes supported by the system. In the ARM system, the Sharp chip supports up to 16 nodes, and the other chip supports up to 4 nodes.

3) numnodes indicates the actual number of memory nodes in the current system.

4) in a system that does not support the CONFIG_DISCONTIGMEM option, there is only one memory node.

5) the maximum bank number m cannot be greater than the NR_BANKS-1.

6) NR_BANKS indicates the maximum number of memory banks supported by the system, which is generally equal to the number of cpu ram slices. In the ARM system, the Sharp chip supports up to 16 banks, while other chips support up to 8 banks.

7) The mem_init () function releases the space occupied by the page frame code table of all nodes, the space of the page descriptor of the holes, and the pages of the idle memory.

Virtual Storage Space Layout

In a system that supports MMU, the MMU function is enabled after hardware initialization is completed, so that the entire system runs in a virtual bucket, MMU of the processor is used to map a virtual bucket to a physical bucket, while the ing between a virtual bucket and a five-path bucket is managed by the Linux kernel. In 32-bit systems, physical storage space occupies 4 GB, and virtual storage space also occupies 4 GB, linux maps the memory space that actually exists in the physical space much less than 4 GB to all the space except the I/O space mapped to the entire 4 GB virtual storage space, therefore, the virtual memory space is much larger than the physical memory space, which means that the same physical memory may be mapped to multiple virtual memory address spaces, which is the responsibility of Linux memory management.

Note:

1) linear address space: refers to the entire 4 GB virtual storage space in Linux from 0x00000000 to 0xFFFFFFFF.

2) kernel space: the kernel space indicates the code or data running in the supervisor mode of the processor. the kernel space occupies 1 GB linear address space from 0xC0000000 to 0xFFFFFFFF, the kernel linear address space is shared by all processes, but only processes running in the kernel state can access it. user processes can switch to the kernel state to access the kernel space through system calls, the address generated when the process runs in the kernel state belongs to the kernel space.

3) user space: the user space occupies 3 GB linear address space from 0x00000000 to 0xBFFFFFFF. each process has an independent 3 GB user space. Therefore, the user space is exclusive to each process, however, the kernel thread does not have user space because it does not generate user space addresses. In addition, the sub-process shares (inherits) the "user space" of the parent process, instead of sharing the user space of the parent process, only the ing between the user linear address and the physical memory address of the parent process. Processes running in the user state and kernel state can access the user space.

4) kernel logical address space: refers to the linear address space from PAGE_OFFSET to high_memory. it is the physical memory ing area of the system and maps all or part of it (if the system contains high-end memory) physical memory. The logical address space of the kernel corresponds to the physical address space of the system RAM memory (including memory holes ), there is only one fixed offset between the address in the kernel logical address space and the address corresponding to the physical address space in the RAM memory. if the physical address space in the RAM memory is edited from 0x00000000, the offset is PAGE_OFFSET.

5) low-end memory: the physical memory mapped to the kernel logical address space is the low-end memory. there is always a permanent one-to-one kernel logical address in the low-end linear address space in Linux, during system initialization, the low-end memory is permanently mapped to the kernel logical address space, and a virtual ing page table is created for the low-end memory. The conversion between physical addresses and linear addresses in the physical memory in the low-end memory can be performed through two macros, _ pa (x) and _ va (x), __pa (x) convert the address x of the kernel logical address space to the corresponding physical address, which is equivalent to _ effec_to_phys (unsigned long) (x), __va (x, converts the address of the low-end physical memory space to the corresponding kernel logical address, which is equivalent to (void *) _ phys_to_virt (unsigned long) (x ))).

6) high-end memory: the physical memory above the low-end memory address is high-end memory. there is no fixed one-to-one kernel logic address in the Linux linear address space in the high-end, during system initialization, a ing page table is not set up for the memory to map it to the Linux linear address space. Instead, a ing page table is created for the allocated high-end physical memory when high-end memory is used, enable it to be used by the kernel; otherwise, it cannot be used. The preceding _ pa (x) and _ va (x) macros cannot be used for switching between high-end memory physical addresses.

7) concept of high-end memory: As mentioned above, Linux divides the 4 GB linear address space into two parts. from 0x00000000 to 0xBFFFFFFF, a total of 3 GB space is occupied by the user process, this linear address space is not fixed to the physical memory space. the 4GB linear address space from 0xC0000000 to 0xFFFFFFFF is used as the kernel space. in the embedded system, in addition to ing physical memory space, this linear address space also maps I/O space such as the peripheral register space inside the processor. 0xC0000000 ~ The kernel logical address space between high_memory is used to fix the physical memory ing in the system, that is, 0xC0000000 ~ The space size between high_memory and the physical memory size of the system are the same (of course, in a non-continuous memory system with the CONFIG_DISCONTIGMEMD option configured, the kernel logical address space may have memory holes like the physical memory space). If the physical memory capacity in the system is much smaller than 1 GB, then the high_memory ~ above the kernel logical address space in the current kernel address space ~ There is enough space between 0xFFFFFFFF to fix the ing of some I/O space. However, if the physical memory capacity (including memory holes) in the system is greater than 1 GB, there is not enough kernel linear address space to fix all the physical memory of the ing system and some I/O space. to solve this problem, we have set an experience value on the x86 processor platform: 896 MB, that is, if the physical memory (including memory holes) in the system is greater than 896 MB, the first MB physical memory (low-end memory) is fixed to the kernel logical address space 0xC0000000 ~ 0xC0000000 + 896 MB (= high_memory), while the physical memory after 896MB is not fixed to the kernel linear address space ING, this part of the memory is called high-end physical memory. In this case, the kernel linear address space is high_memory ~ The MB space between 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 an experience on the x86 processor platform. it leaves MB of linear address space to map high-end memory and I/O address space. in the embedded system, we can modify this threshold according to the actual situation. for example, in MIPS, this value is set to 0x20000000B (512 MB). The CONFIG_HIGHMEM option must be configured only when the physical memory capacity in the system is greater than 0x20000000B, enable the kernel to allocate and map high-end memory. For details about how to set high-end physical memory and high-end physical memory threshold, see the memory page area (zone) concept.
8) high-end linear address space: the linear address space from high_memory to 0xFFFFFFFF belongs to the high-end linear address space, where VMALLOC_START ~ The linear address between VMALLOC_END is used by the vmalloc () function to allocate high-end physical memory that is physically discontinuous but linear address space is continuous, or is used by the vmap () function to map high-end or low-end physical memory, or the ioremap () function re-maps the I/O physical space. The linear address space of the LAST_PKMAP (usually equal to 1024) page starting with PKMAP_BASE is used by the kmap () function to permanently map high-end physical memory. The linear address space of the KM_TYPE_NR * NR_CPUS page starting with FIXADDR_START is 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 I/O address spaces during system initialization.

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