In the previous blog, we have seen how Linux can effectively convert logical addresses to linear addresses by using 80x86 segment and paging hardware units, and convert them from linear addresses to physical addresses. So how do our applications use these logical addresses and the address layout of the entire memory? For example, the memory is like a city, and the residents living in this city are like various processes. A citizen eats, and sleeps, of course, it is necessary to use various resources such as "House", "car", and "ticket. Some resources are fixed, such as "Houses", which are called static data. Some resources are dynamic, such as "cars", which are called dynamic data. Some resources are used for purchase (production) data, such as "tickets", is called code.
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:
• Page boxes within the range of unavailable physical addresses are generally used to store BIOS information.
• Page box containing 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 box 0 is used by the BIOS to store the hardware configurations that have been checked during power-on self-test and 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 BIOS routines 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 a specific 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 above figure, the range is 0x000a0000
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 |
Shows how Linux fills in the first 3 MB of RAM.
We can see that the available page box is the rest of the memory, which is called dynamic memory. This is not only a valuable resource required by the process, but also a valuable resource required by the kernel itself. In fact, the performance of the entire system depends on how to effectively manage the dynamic memory. Therefore, all multi-task operating systems are trying their best to optimize the use of dynamic memory, that is, to release the memory whenever necessary.
In the following blog, we will focus on how the kernel allocates dynamic memory to itself. It mainly includes page box management, high-end memory ing, partner system algorithm, slab distributor, memory pool, and non-continuous memory zone management.