简体   繁体   中英

How linux kernel create, initialize and setup page table for user process?

  • Per process has a page table. What's the time when kernel create a page table for process, in fork or exec ?

  • MMU should have a register pointing to base address of page table. Is this base a virtual address or physical address?

  • How kernel allocate physical page frames for page table?

During fork, an exact copy of mm_struct of current task is copied for new task using copy_mm(). In earlier versions of kernel, whenever a child was created, the full address space was duplicated immediately, however recent versions seem to have COW(Copy On Write) strategy where a copy is created, only during the first time when they need to modify the data.

Generally, MMU does the translation of Virtual address to Physical address. However, the address translation functionality/unit in MMU is dependent on the type of processor and the kind of integration/support from operating system. The MMU register points to the page table/segment of the current process in execution. The logical memory is considered to be in blocks called as pages and the physical memory is considered to be in blocks termed as frames. The logical address is a combination of page number and page offset. The page table is used for mapping the logical address to physical address by Operating system , where it determines the corresponding frame number in the table for the respective page number. This frame number in combination with page offset becomes the physical address.

In case of x86, the MMU translation can happen in two stages where the first step is translation of logical address(being pointed to by register) to linear address and which in-turn is converted to physical address in second step. The paging mode is set by configuring the MSB of CR0 register.There can be up to 1024 page tables whose information (page table base address and other information) are maintained in page directory(CR3). The linear address bits A22-A31 points to one of the 1024 possible page table's base address in page directory.

The page table and page descriptor are located in memory. So, they shall be handled via TLB(Translation lookaside buffer), a 4 way set associative cache that can hold upto 32 page table entries(recently accessed entries) to avoid being fetched from memory for every memory access.

During the boot process, the kernel understands from BIOS on the various physical memory ranges , the corresponding memory type mapping and creates the physical address map/table. The kernel then copies such information into appropriate kernel data structures and considers those page frames as usable.

Note that as soon as the kernel is loaded into memory, there might not be page tables as paging might not be initialized in CPU. However,subsequently, once the paging is initialized, the kernel initializes its own page tables in two stages. In the first phase, kernel creates initial page tables that shall be sufficient for having the kernel in RAM. In the second phase, the kernel uses all available RAM and creates the full fledged page tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM