In the previous blog post, we have seen how Linux effectively uses 80x86 's segmented and paged hardware units to translate logical addresses into linear addresses, which are translated from linear to physical addresses. So how does our application use these logical addresses, and what is the address layout of the entire memory? For example, memory is like a city, and the citizens living in this city are like every process, and a citizen eats and sleeps, and of course it is used for various resources such as "house", "Car" and "ticket". Some resources are fixed, such as "house", we call it static data, some resources are dynamic, such as "car", we call it Dynamic Data, some resources are used to buy (produce) data, such as "ticket", we call code.
Now, let's look at how the memory of this huge city history is laid out. During the system initialization phase, the kernel first establishes a physical address mapping in real mode to specify which physical address ranges are available to the kernel and which are not (mainly based on shared memory that maps hardware device I/O, or according to the corresponding page box contains the BIOS data).
Some parts of the memory are permanently assigned to the Bois or kernel, which holds BIOS information, kernel code, and static kernel data structures. So the kernel registers the following page boxes as reserved:
? A page box in a range of physical addresses that is not available, typically used to store BIOS information.
? A page box that contains kernel code and initialized data structures.
Pages that are marked as reserved in the page box must never be dynamically allocated or swapped to disk.
In general, the Linux kernel is installed in RAM, starting from the physical address 0x00100000, that is, starting with the second MB. The total number of page boxes required depends on the configuration of the kernel: A typical configuration of the kernel can be completely installed in less than 3MB of RAM.
Why is the kernel not installed where the first MB of RAM starts? Mainly for the specific PC architecture considerations. For example:
? Page box 0 is used by the BIOS to store the hardware configuration that was checked during the power-on self-test (power-on self-test,post). As a result, many laptop BIOS even writes data to the page box after the system is initialized.
? The range of physical addresses from 0x000a0000 to 0X000FFFFF is usually left to the BIOS routines, and the internal memory on the ISA graphics card is mapped. This area is the famous hole from 640KB to 1MB on all IBM compatible PCs: the physical address exists but is reserved, and the corresponding page box cannot be used by the operating system.
? Other page boxes within the first 1MB may be reserved by a specific computer model. For example, the IBM laptop maps the 0x0a page box to the 0x9f page box.
During the early stages of the boot process, the kernel asks the BIOS and understands the size of the physical memory and calls the Machine_specific_memory_setup () function to establish a physical address mapping. Assuming that our memory is 128MB, then the first MB is given to the BIOS. The entire 128MB of memory is physically mapped into the following layouts:
0X00000000-0X0009FFFF 640K space available outside the first page box
0X000A0000-0X000EFFFF reserved
0X000F0000-0X000FFFFF reserved for BIOS routines
0X00100000-0X07FEFFFF 126.9MB free Space
0X07FF0000-0X07FF2FFF ACPI Data
0X07FF3000-0X07FFFFFF ACPI NVS
0XFFFF0000-0XFFFFFFFF reserved
Here is a brief introduction to the end of 128MB memory, from the 0x07ff0000 to 0x07ff2fff the physical address range of the power-on self-test (POST) stage is written by the BIOS system hardware device information, during the initialization phase, the kernel copies this information into a suitable kernel data structure, These page boxes are then considered to be available. Instead, the physical address range from 0x07ff3000 to 0X07FFFFFF is mapped to the ROM chip of the hardware device. The physical address range starting from 0xffff0000 is marked as reserved because it is mapped by hardware to the ROM chip of the BIOS. Note that the BIOS may not provide some information about the physical address range (in the diagram above, the range is 0x000a0000 to 0X000EFFFF). For security reasons, Linux assumes that such a range is not available.
Although, we see the first MB, the BIOS is not used up, but in order to avoid loading the kernel into a set of discontinuous page boxes, affecting performance, Linux skips 1MB of RAM, starting from the 2nd MB to load. In fact, in general, for two MB, that is, 512 page boxes, the initialization of the kernel code and some static data, is sufficient.
The kernel may not see all of the physical memory that is reported by the BIOS: for example, if you do not compile with PAE support, the kernel can only address 4GB of RAM, even if there is much more physical memory available for use. The Setup_memory () function is called after Machine_specific_memory_setup () is executed: It parses the physical memory area table and initializes a number of variables to describe the physical memory layout of the kernel, as shown in the following table:
|
Variable name |
Description |
|
Num_physpages |
Page box number of the highest available page box |
|
Totalram_pages |
Total number of available page boxes |
|
Min_low_pfn |
The page box number of the first available page box in RAM after a kernel image |
|
Max_pfn |
Page box number of the last available page box |
|
Max_low_pfn |
The page box number of the last page box mapped directly by the kernel (low address memory) |
|
Totalhigh_pages |
Total number of page boxes not directly mapped by the kernel (high address memory) |
|
Highstart_pfn |
The page box number of the first page box that is not directly mapped by the kernel |
|
Highend_pfn |
The page box number of the last page box that the kernel does not directly map |
Shows how Linux fills the first 3MB of RAM.
We see the available page boxes in the diagram, which are the rest of the memory, which we call dynamic memory, which is not only a valuable resource for the process, but also a valuable resource for the kernel itself. In fact, the performance of the entire system depends on how effectively the dynamic memory is managed. As a result, all multitasking operating systems are now doing their best to optimize the use of dynamic memory, which is to do as much as possible when it needs to be allocated and freed when it is not needed.
In the following blog post, we will focus on how the kernel allocates dynamic memory to itself. Mainly includes page frame management, high-end memory mapping, partner system algorithm, slab allocator, memory pool, non-contiguous memory area management.
Linux Memory layout