Linux each executable program has the same virtual address assignment, how is the program loaded when the OS starts the process?
1. Process structure Body
Each process has a task_struct structure, and the mm field of the struct is responsible for mapping the virtual address of the program memory.
Each vm_area_struct in mm corresponds to the segment virtual address space of the executable program, such as. text, and so on. When the program is executed, these virtual addresses of the program
are filled into different vm_area_struct.
struct vm_area_struct {... unsign Ed long Vm_start; // virtual address start address unsigned long vm_end; // virtual address End address struct file * vm_file; // executable file handle unsigned long Vm_pgoff; // segment in executable program position struct vm_operations_struct * vm_ops; // The function that operates the executable program ...};
2. Virtual Address access
The Linux process accesses the virtual address using the page mechanism. To simplify, it can be understood that the virtual address consists of 3 parts.
(1) High 10bit represents the base address of the page directory and is stored in the x86 CR3 register.
(2) The middle 10bit is the page index, the directory base address plus the index can get the page descriptor address. The page description has the actual physical address of the page.
(3) The last 12bit is biased for the virtual address in the physical page.
When the program accesses the virtual address, there is the chip hardware to complete the above action.
3. Program loading
To ensure that these addresses are normally accessed when the program is running, you must populate the page catalog entries, page indexes, and physical pages at load time.
(1) When the program is loaded, the page directory is retrieved by the CR3 register of the site PDG.
(2) Access the virtual address, assuming that the high 10bit is I, the Middle 10bit is J, and the last 12bit is K. First read the page directory address, determine if pdg[i] is 0, if the 0 trigger Do_page_fault () assigns a physical page.
(3) Pdg[i][j] For page descriptor, modify description, save page Physical address.
(4) Using VM_AREA_STRUCT structure in the File,ops,pgoff, such as reading programs to memory.
4. Page Exchange
Each process is a virtual 4GB of memory, what if the physical is not enough?
Each physical page has a MEM_MAP structure that records the following information:
(1) Use count on this page
(2) Page age
(3) Page frame number
For a data image, if the page has not been written, you can discard it directly, and if the page is modified, you need to save the dirty page in the swap file.
Linux process 2--Process loading