RFQ/BOM 0 Sign In / Register

Select Your Location

What are the memory addressing modes of Linux?

October 07, 2020

2147

Why memory management:


Early programs run directly on physical addresses, which means that the space required by the program does not exceed the physical memory of the machine. There is no problem. However, in actual scenarios, they are all multi-tasking and multi-process. The address reserved for each process is not reliable. Take a chestnut: if there are 3 programs a, b, c, a need 10M, b needs 100M, c needs 20M, the total memory is 120M, according to the previous allocation method, the first 10M is for a, 10M-110M is for b, the system There is 10M left, but c needs 20M. Obviously, the remaining memory is not enough for c. How to do?


1. Efficiency issues


You might think of writing the data of program b to the disk when the program c is running, and then writing the data back from the disk when running b, not to mention that it cannot meet the needs of parallel running of programs b and c, even frequent io The time-consuming problem caused by the operation is also unacceptable.


2. Process address isolation problem


In addition to efficiency issues, the reserved space for a process will crash if it needs to be accessed by other processes. For example, the space accessed by process a is the first 10M, but a section of code accessing 10-110M in the a program may cause the crash of the b program, so the address spaces of the processes need to be isolated from each other.


3. Relocation problem


In the real scenario, it is impossible for a single task to run in the allocated memory. When multiple tasks are running in parallel, it is possible to apply for addresses in other processes when dynamically applying to release the memory. At this time, you need to relocate to a new address. .


Memory management is nothing more than trying to solve the above three problems. How to improve the efficiency of memory usage? How to isolate the address space of a process? How to solve the relocation problem when the program is running?


How memory management maps from virtual addresses to physical addresses:


The process of mapping memory management from virtual address to physical address is the process of solving the above three problems. Memory management uses segmentation mechanism and paging mechanism to solve the above three problems respectively.


Segmentation mechanism:


As long as the program is divided into sections and the entire section is translated to any position, the address in the section is unchanged from the base address of the section, no matter what the base address of the section is, as long as the offset address in the section is given, the cpu can access the correct Instructions. Therefore, when loading the user program, as long as the content of the entire section is copied to a new location, and then the address in the section base address register is changed to this address, the program can run accurately, because the program uses the offset address within the section , Relatively new segment base address, the content of the offset address is still the same.


It can be seen that the segmentation mechanism solves the problem of inter-process isolation and relocation. This action is done in hardware, but some hardware does not have a segmentation mechanism. As a cross-platform Linux, it uses a more versatile paging mechanism to solve the conversion from linear address to virtual address to physical address.


Paging mechanism:


You can refer to "How does the CPU access memory?" 》Understand the concept of the first-level page table. In order to be compatible with 32-bit and 64-bit, Linux usually uses four-level page tables, page global directories, page upper-level directories, page intermediate directories, and page tables:

I will not explain in detail how Linux uses the four-level page table to convert linear addresses (virtual addresses) to physical addresses.


When the process is switching, it finds the pgd field in mm_struct according to task_struct, obtains the page global catalog of the new process, and then fills it into the CR3 register to complete the page switching.


Let's take a look at the process of mmu page addressing:


On the code:

It can be seen that the physical address corresponding to the virtual address ffff99b488d48000 is 80000000c8d48000. This process is also the process of mmu.