Linux high-end memory mapping method

  
        

1.1 Kernel address space (linear space) distribution

(1) Direct mapping area: The maximum 896M interval from 3G in linear space, which is the direct memory mapping area, the linear address and physical address of the area. There is a linear conversion relationship: linear address = 3G + physical address.

(2) Dynamic memory mapping area: This area is allocated by the kernel function vmalloc. The characteristic is that the linear space is continuous, but the corresponding physical space is not necessarily continuous. The physical page corresponding to the linear address assigned by vmalloc may be in low-end memory or in high-end memory.

(3) Permanent memory map area: This area can access high-end memory. The access method is to allocate a high-end memory page using alloc_page(_GFP_HIGHMEM) or use the kmap function to map the allocated high-end memory to the area.

(4) Fixed mapping area: This area and the top of 4G only have a 4k isolation band, and each address item serves a specific purpose, such as ACPI_BASE.

Description:

Note that user space can of course use high-end memory, and it is normal use. The kernel uses high-end memory space (if any) when allocating memory that is not used frequently. The so-called infrequent use is relatively speaking. For example, some data structures of the kernel are frequently used, and some data of the user is infrequently used. When a user starts an application, it needs memory, and each application has a 3G linear address. When mapping the page table to these addresses, you can directly use the high-end memory.

And one more thing to correct is that the 128M linear address is not just used in these places. If you want to load a device that needs to map its memory to the kernel, it also needs to use this. Segment linear address space to complete, otherwise the kernel can not access the memory space on the device.

In summary, the kernel's high-end linear address is to access memory resources other than the kernel's fixed mapping. When a process uses memory, it triggers a page fault exception. It is a matter for the kernel to specifically map which physical pages to the user process. There is no concept of high-end memory in user space.

1.2 High-End Memory Mapping

High-end memory mapping means: mapping a linear address space (ranging from PAGE_OFFSET + 896M to the last 128M of 4G) to a physical page frame above 896M. As shown below:

There are three ways to store high-end memory (both indirect mapping):

1.2.1 Mapping to "kernel dynamic mapping space" (non-contiguous memory area mapping)

This method is very simple, because vmalloc (), in the kernel "dynamic mapping space" when applying for memory, it is possible to get the page from the high-end memory (see vmalloc implementation), so that high-end memory It is possible to map to "kernel dynamic mapping space".

1.2.2 Permanent Kernel Mapping

If you get a page corresponding to high-end memory through alloc_page(), how do you find a linear space for it?

Leave a linear space from PKMAP_BASE to FIXADDR_START for mapping high-end memory. On the 2.4 kernel, this address range is between 4G-8M and 4G-4M. This space starts with "kernel permanent mapping space" or "permanent kernel mapping space". This space uses the same page directory table as other spaces. For the kernel, it is swapper_pg_dir, for normal processes, through the CR3 register. direction. Normally, this space is 4M in size, so only one page table is needed, and the kernel looks for this page table by going to pkmap_page_table. With kmap(), you can map a page to this space. Since this space is 4M in size, it can map up to 1024 pages at the same time. Therefore, for a page that is not used, and when it should be released from this space (that is, the mapping relationship), kunmap() can be used to release the linear address corresponding to a page from this space. Permanent memory mapping allows for long-term mapping.

1.2.3 Temporary Mapping

The kernel reserves some linear space between FIXADDR_START and FIXADDR_TOP for special needs. This space is called "fixed mapping space"; in this space, there is a part of the temporary mapping for high-end memory.

This space has the following characteristics:

1. Each CPU occupies a space

2. In the space occupied by each CPU, it is divided into multiple A small space, each small space is 1 page, each small space is used for one purpose, these purposes are defined in km_type in kmap_types.h. When a temporary mapping is to be performed, the purpose of the mapping needs to be specified. According to the mapping purpose, the corresponding small space can be found, and then the address of the space is used as the mapping address. This means that a temporary mapping will cause the previous mapping to be overwritten.

Temporary mapping is achieved with kmap_atomic(). Can be used inside the interrupt handler and the delay function, never blocking. Because the temporary memory map is part of a fixed memory map, an address is fixed for use by a kernel component.

Copyright © Windows knowledge All Rights Reserved