Linux operating systems and drivers run in kernel space, applications run in user space, both cannot simply use pointers to pass data, because the virtual memory mechanism used by Linux, user-space data may be swapped out, and when kernel space uses user-space pointers, the corresponding data may not be in memory.
Linux Kernel address space partitioning
Typically, the 32-bit Linux kernel address space is partitioned 0~3g to user space and 3~4g to kernel space. Note that this is a 32-bit kernel address space partition, and the 64-bit kernel address space partition is different.
1, x86 Physical address space layout:
The following space at the top of the physical address space is occupied by the I/O memory mapping of the PCI device, and their size and layout are determined by the PCI specification. 640k~1m This address space is occupied by the BIOS and VGA adapters.
When the Linux system is initialized, a Page object is created for each physical page based on the actual physical memory size, and all Page objects form a mem_map array.
Further, for different purposes, the Linux kernel divides all physical pages into 3 types of memory management areas, respectively, Zone_dma,zone_normal,zone_highmem.
The ZONE_DMA range is 0~16m, and the physical pages for this area are dedicated to the DMA use of I/O devices. The physical pages of DMA are required to be managed separately because DMA uses physical addresses to access memory, does not go through the MMU, and requires a contiguous buffer, so in order to provide a physically contiguous buffer, a region must be specifically partitioned from the physical address space for DMA.
The scope of the Zone_normal is 16m~896m, and the physical pages of the region are directly available to the kernel.
The scope of the ZONE_HIGHMEM is 896m~ end, the area is high-end memory, the kernel cannot be used directly.
2, Linux virtual address kernel space distribution
Under kernel image, there is 16M of kernel space for DMA operation. The 128M address at the high end of the kernel space is composed of 3 parts, Vmalloc area, persistent kernel mapping zone, and temporary kernel mapping area.
Because of the direct mapping between zone_normal and the kernel linear space, the kernel places frequently used data such as kernel code, GDT, IDT, PGD, mem_map arrays, etc. in Zone_normal. Instead of placing infrequently used data, such as user data, page tables (PT), in Zone_ Highmem, a mapping Relationship (Kmap ()) is established only when you want to access that data. For example, when the kernel accesses the I/O device storage space, it uses ioremap () to map the Mmio area memory at the high end of the physical address to the Vmalloc zone in the kernel space, and then disconnects the mapping when it is exhausted.
3, Linux virtual address user space distribution
The code area of the user process typically starts with the 0x08048000 of the virtual address space, which is to facilitate checking for null pointers. Above the code area is the data area, uninitialized data area, heap area, stack area, and parameters, Global environment variables.
4. Relationship between Linux virtual address and Physical address mapping
Linux divides the linear address space of the 4G into 2 parts, 0~3g the user space,3g~4g to kernel space.
Because the paging mechanism is turned on and the kernel wants to access the physical address space, it must first establish a mapping relationship and then access it through the virtual address. In order to access all the physical address space, it is obviously impossible to map all the physical address space to the 1G kernel linear space. The kernel then maps the physical address space of the 0~896m to its own linear address space, so that it can access the physical pages in ZONE_DMA and zone_normal at any time, and the remaining 128M linear address space in the kernel is not sufficient to fully map all the Zone_ Highmem,linux takes a dynamic mapping method that maps the physical pages in the Zone_highmem to the last 128M linear address space in kernel space, and releases the mappings after use for other physical pages to map. Although this is a matter of efficiency, the kernel, after all, can access all the physical address space normally.
5, the understanding of Buddyinfo
Cat/proc/buddyinfo displays as follows:
Node 0, Zone DMA 0 4 5 4 4 3 ...
Node 0, Zone Normal 1 0 0 1 101 8 ...
Node 0, Zone highmem 2 0 0 1 1 0 ...
Where node represents the number of nodes in the NUMA environment, where only one node 0;zone represents the region under each node, typically DMA, normal, and hignmem three regions, and the following columns represent the free page blocks for each order in the partner system. For example, for the second column of Zone DMA (starting from 0), the number of free pages is 5*2^4 and the available memory is 5*2^4*page_size.
The calculation method is:
The number of columns in the *2^ column *page_size where the number of columns is calculated from 0, that is, the first column is the number of the current column *2^0*page_size
Problems:
1. Does the user space (process) have a high-end memory concept?
User processes do not have high-end memory concepts. Only high-end memory exists in kernel space. A user process can access up to 3G of physical memory, and the kernel process can access all physical memory.
2. Is there high-end memory in the 64-bit kernel?
Currently, there is no high-end memory in the 64-bit Linux kernel because the 64-bit kernel can support more than 512GB of memory. If the machine installs more physical memory than the kernel address space, there will be high-end memory.
3. How much physical memory can the user process access? How much physical memory can the kernel code access?
The 32-bit system user process can access up to 3GB, and the kernel code can access all physical memory.
The 64-bit system user process can access more than 512GB, and the kernel code can access all physical memory.
4, high-end memory and physical address, logical address, linear address relationship?
High-end memory is only related to the logical address, not the logical address, the physical address is not directly related.
5. Why not allocate all the address space to the kernel?
If all the address space is given to memory, then how does the user process use memory? How to ensure that the kernel uses memory and user processes do not conflict?
Linux kernel memory management (ZONE_DMA Zone_normal Zone_highmem)