Physical and virtual storage space layout in Linux

Source: Internet
Author: User

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. For details, refer to the storage space distribution table (memory map) section in the processor user manual, here we only list the general layout of Linux physical memory space on the embedded processor platform, as shown in Figure 18-4.

 

 

Figure 18-4 general Linux physical memory space layout:

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. Figure 18-5 lists the general layout of virtual memory space in the Linux kernel (in fact, I/O space is also in use, usually occupying high-end memory space, which is not marked here ).

Figure 18-5 General Layout of virtual memory space in Linux:

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 in Figure 18-4 (including the 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
*) _ 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 ),IfThe physical memory capacity in the system is much smaller than 1 GB, so 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,IfThe physical memory capacity (including memory holes) in the system is greater than 1 GB, so there is not enough Kernel linear address space to fix the ing of all the physical memory and some I/O space of the system, to solve this problem, we set an experience value of 896 Mb on the x86 processor platform, that is, if the physical memory (including memory holes) in the system is greater than 896 MB, then, 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.

Problem:

1. Linux maps the physical memory to the kernel space during initialization. This also makes me very strange. Linux maintains a page array as the "repository" of the physical memory. The subscript of this array is actually the page number of the physical memory. Linux will initialize this array during initialization. In my understanding, the ing from the imaginary storage to the physical memory is actually implemented to the page array operation, which is enough. Why do we need to map physical memory addresses to kernel space? Also, why is the remaining 896 m kernel address space? Is the remaining m the kernel itself? If so, all the system calls and kernel data structures in the kernel will be squeezed into this space. In this way, we need to make a good plan for the relatively few spaces, instead of a normal program, the first is the virtual storage space address starting with 8; if not, what is the M side, and where does the kernel hide itself?

Linux does this to simplify the conversion between virtual addresses and physical addresses in the kernel. __va () and _ Pa () are easily transformed, it does not mean that the physical memory is fully occupied by the kernel when it is mapped to the kernel space.The physical memory used for non-dynamic data in the kernel cannot be changed, but the dynamic part can be released, after the physical memory is released, it can be reflected in the user space. Therefore, when there is no high-end memory, the kernel space page_offset maps to high_memory to all physical memory spaces, but it does not mean that the kernel space has always occupied all physical memory space, it only means that the virtual addresses in the kernel space are always mapped to all physical address spaces, and are mapped from the virtual address 0c000-0000 to the physical address 0 and backward. Some dynamic arrays in the kernel space will also release this part of the kernel address space, and some physical memory will be released accordingly. In this way, the physical memory can be mapped to the user space. In fact, the other part of the kernel space can be mapped to the local memory.

2. Real mode and protection mode

Generally, the real address mode does not distinguish between the user space and the kernel space. The user space program may modify the kernel space program. In actual mode, the CIDR block is selected, but the CIDR Block Base Address + offset address is simply used for addressing. In arm, it may be a direct physical addressing method. The protection mode relies on virtual storage management of MMU. It can protect multiple programs from mutual interference.

3. page missing Exception Handling

Generally, the Process Code is not loaded into the memory at a time, but a small part is first loaded and executed. During the execution, it is found that the command or data to be retrieved is no longer in memory. The actual result is that there is no physical address associated with the specified linear address. In this case, an error occurred while handling the page transfer. By selecting idle page frames, allocate a certain number of page frames (that is, physical address space) to copy the code or data of the stored device to the physical address space, and associate the paging frame with the specified linear address, that is, the page (virtual address page or physical address page or page frame) with the page frame. After all the processes are executed, some or all page frames are released.

Page missing exception handling:

If (data in physical memory)

{Convert a virtual address to a physical address

Read data}

Else

{If (data in disk)

{

If (physical memory is idle)

{Read data from disk to physical memory

Convert a virtual address to a physical address

Read data

}

Else

{Store data on a page in the physical memory to the disk

Read data from the disk to the physical memory of the page

Convert a virtual address to a physical address

Read data

}

}

Else

{Error reported }}

 

 

4. Linear Space to physical space

In linear space, process 1 and process n both have their own 0--3g-1 linear addresses, which are invisible to each other. The kernel space 3g--4G is shared by various processes. The linear address 3G is mapped to zero physical memory space. In fact, the physical space 0-m is the low-end memory, all mapped to the kernel space, and linear ing to the kernel space. This linear address and the physical address only differ by one offset c0000000. For more information about linear address ing in kernel space, see the preceding section. When the system needs to install a user program, it first generates a child process through the parent process. The child process only inherits the linear address ing between the parent process and the physical address, that is to say, the child process does not have the memory space of the parent process (including the linear space and physical space). The child process itself has an independent set of user space, but the kernel space is shared by all processes. Then, the sub-process code and data are copied from the storage device to the corresponding memory through the page missing mechanism. Note that because the parent process memory ing is inherited, the physical memory mapped to the parent process is preferentially located in the page-missing mechanism. To access the kernel space, the user process must use a system call or interrupt mechanism so that the user space does not interfere with each other. The kernel process does not generate user space addresses, so the kernel process does not need user space. In this way, each user process runs independently without interfering with each other and does not easily modify the kernel space. When you need to access the kernel space, you must enter the kernel space by means of system call or interruption. The kernel process does not generate user addresses, so the kernel process only runs in the kernel space.

 

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.