简体   繁体   中英

How does page fault handler fill up the page table entry with a physical address if CPU sees virtual addresses?

I was reading about page faulting and from what I read, the MMU consults the page table to translate virtual addresses into physical addresses. It is the responsibility of the OS (via the page fault handler) to fill up these page table entries.

What confuses me is how does the page fault handler obtain the physical addresses in the first place? In the diagrams and notes I saw, the CPU seems to use virtual addresses and the MMU transparently translates them to physical addresses. Does the CPU specially work with physical address rather than virtual addresses for page fault handling?

If there is an access to some 4K page that is not present in memory and the page fault handler successfully locates the corresponding 4K page on disk, how does it acquire a 4K page of physical memory and figure out the physical address of the 4K page of physical memory?

Part of the OS`s responsibility is to keep track of a list of physical pages. You can look on OSDev to see how this is done - usually by querying BIOS/UEFI-exposed functions, which give you (usually non-contiguous) lists of free memory.

UEFI in particular exposes GetMemoryMap at boot time to get an array of memory descriptors.


Given a maintained list of available physical pages - when the OS handles a page fault, it has access to the faulting virtual address, and it can decide what to do. If it needs to allocate a new page, then it will consults its list of free pages, and choose an available physical page to map into the virtual address space. On x86 this mapping is done by modifying the page table and loading it into the cr3 register.

Once the page is mapped, it can be written to using virtual addresses.

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