Linux memory layout example
Now let's take a look at how memory is laid out in this huge city history. During system initialization, the kernel first establishes a physical address ing in the real mode to specify which physical address ranges are available to the kernel and which are unavailable (mainly based on the shared memory mapped to the hardware device I/O, or according to the BIOS data contained in the corresponding page box ).
Some parts of the memory will be permanently allocated to the BOIS or kernel for storing BIOS information, kernel code, and static kernel data structure. Therefore, the kernel records the following pages as retained:
Pages within the range of unavailable physical addresses are generally used to store BIOS information.
Page that contains the kernel code and initialized data structure.
Pages marked as reserved pages cannot be dynamically allocated or exchanged to disks.
Generally, the Linux kernel is installed in RAM starting from the physical address 0x00100000, that is, starting from the second MB. The total number of required page boxes depends on the Kernel configuration scheme: the kernel obtained by a typical configuration can be fully installed in RAM of less than 3 MB.
Why is the kernel not installed at the beginning of the first MB in RAM? This is mainly for the specific PC architecture. For example:
The page 0 is used by the BIOS to store the hardware configurations that have been checked during Power-On Self-Test (POST. Therefore, the BIOS of many laptops even writes data to this page after system initialization.
The range of physical addresses from 0x000a0000 to 0x000fffff is usually left to the BIOS routine and mapped to the internal memory on the ISA graphics card. This area is the famous hole between 640KB and 1MB on the IBM compatible PC: the physical address exists but is retained, and the corresponding page box cannot be used by the operating system.
Other page boxes within the first 1 MB may be retained by the specified computer model. For example, the IBM laptop maps the 0x0a page to the 0x9f page.
In the early stages of the startup process, the kernel asks the BIOS about the physical memory size and calls the machine_specific_memory_setup () function to create a physical address ing. Suppose our memory is 128 MB, then the first MB is sent to the BIOS. The entire MB memory is physically mapped to the following layout:
0x00000000-0x0009ffff the 640K space except the first page is available
0x000a0000-0x000effff Reserved
0x000f0000-0x000fffff reserved for BIOS routines
0x00100000-0x07feffff 126.9MB available space
0x07ff0000-0x07ff2fff ACPI data
0x07ff3000-0x07ffffff ACPI NVS
0xffff0000-0 xffffffff Reserved
Here, we will briefly introduce the information about the system hardware devices written by BIOS during the batchcompute (POST) phase from 0x07ff0000 to 0x07ff2fff physical address range. During the initialization phase, the kernel copies the information to a suitable kernel data structure and considers the page boxes to be available. Conversely, the physical address ranges from 0x07ff3000 to 0x07ffffff are mapped to the ROM chip of the hardware device. The physical address range starting from 0xffff0000 is marked as retained because it is mapped from hardware to the bios rom chip. Note that the BIOS may not provide some information about the physical address range (in the preceding figure, the range is 0x000a0000 to 0x000effff ). For the sake of security and reliability, Linux assumes that such a range is unavailable.
Although we can see that the BIOS is not used up in the first MB, to avoid loading the kernel into a set of discontinuous page boxes and affecting performance, Linux skips the 1MB RAM, starts from 2nd MB. In fact, in general, for two MB, that is, 512 page boxes, it is enough for the kernel code during initialization and some static data.
The kernel may not see all the physical memory reported by the BIOS: for example, if PAE support is not used for compilation, the kernel can only address 4 gb ram even if a larger physical memory is available. The setup_memory () function is called after machine_specific_memory_setup () is executed: It analyzes the physical memory region table and initializes some variables to describe the physical memory layout of the kernel. These variables are shown in the following table:
|
Variable name |
Description |
|
Num_physpages |
The page number of the highest available page |
|
Totalram_pages |
Total number of available page boxes |
|
Min_low_pfn |
The page number of the first available page box after the kernel image in RAM |
|
Max_pfn |
The page number of the last available page box |
|
Max_low_pfn |
The page number of the last page box directly mapped by the kernel (low address memory) |
|
Totalhigh_pages |
Total number of non-direct kernel ing page boxes (high address memory) |
|
Highstart_pfn |
Page number of the first page box of kernel non-direct ing |
|
Highend_pfn |
Page number of the last page box of kernel non-direct ing |