Recently read a little about the kernel of books, some harvest, now take out to share with you, improper place please advise
Schema conversion
Linux runs in real-time mode in the first half of the boot process, and the program runs using physical addresses, such as 0x9000, which refer to the physical address
0x9000. The second half is in the protected mode using a virtual address. The virtual address transforms the switch in CR1 registers, the virtual address transforms the foundation is the program establishes the page directory, the page table mechanism. is the virtual address through the page directory, the page table maps to the corresponding physical address.
Virtual Address
The Addressing space (virtual address) of 32-bit machine is 4G, in which 0-3g is the user space and 3g-4g is the system space. In the view of the user, our program is like running on memory in 4G space. And each process can use the same virtual address without interfering with each other. In fact, the operating system is playing a trick, cynical, such as the use of the program is 0x1000, Cheng used is also 0x1000
The operating system may point a 0x1000 to the 0x01000000 of physical memory, while the 0x1000 of B points to the 0x02000000 of physical memory.
Conversion principle
Linux uses a level three mapping mechanism namely: page directory, middle page table, page table. and i386 support is level two, so the middle page table
does not work, as transparent. Virtual address conversion to the actual physical address is automatically completed by the hardware MMU, the specific process is that the register CR3 get the page directory address, according to the virtual address of the 22-31-bit value (as a displacement) to obtain the address of the page table, According to the page table address plus the virtual address of the 12-21-bit (as a displacement) to get the value of the page table (physical page address), according to the physical page address plus virtual address 0-11 digits (as a displacement) to obtain the corresponding physical address, complete the conversion.
Status of the virtual address
Each process has its own page table, and the page table describes the space as 0-4g. System space also has its own page table that describes the space
For 0-4g, but 0-3g all initialized to 0, only in 3g-4g mapping, the way is linear mapping, such as 0xc0009000 refers to the physical memory 0x00009000,xc0029000 refers to the physical memory 0x00029000.
Processes use their own page tables both in system space and in user space while the process is running, and the page table 3g-4g space for all processes is the same map.
When the process switches, CR3 will be stationed by the page directory address of the next process. So when you switch to the next because all of the process's page table 3g-4g space is the same map, so no matter how CR3 switch, then the kernel can work.
Management of virtual memory
In order to manage virtual memory, each process has a mm_struct structure and some vm_area_struct, the former is for a
A general description of the virtual memory of the process, which is a description of each memory region of the process.
Because Linux is a paging system, and for the CPU provided by the segmented system, you can understand that the systems have been directly penetrated
The above reference to the Maudeca Hu Himing "Linux kernel source code scenario analysis", is really a good book