Search code examples
memory-managementlinux-kernelmmu

How does the MMU/Kernel knows that a PTE is valid?


Assuming a two-level page table, let's say the program gets allocated 2 pages (8KB)

  GPD      PD1     PD2      PD3 
+-----+ +------+ +------+ +------+
+  1  + +  22  + +------+ +------+
+  0  + +  62  + +------+ +------+
+  0  + +  0   + +------+ +------+
+-----+ +------+ +------+ +------+

How does the MMU/kernel knows when a program attempts to access the third entry of PD1 or like the 2,3 entry of GPD? Does it initialize the unused PTEs with some value in order to distinguish the unused ones? (like with 0 or something)

I have read in here and there is no valid bit or something in the pte flags


Solution

  • There is a function named pte_none. It can test whether a PTE is empty or not. If a PTE is invalid, its value will be zero and the function pte_none will return TRUE.

    If a page table entry is zero, the present bit is also zero, so the mapping from virtual address to physical address does not exist. If a program wants to access this invalid virtual address, MMU will check the present bit and raise an exception called page fault and then CPU will execute an exception handler which is registered by Interrupt Descriptor Table(IDT). What the handler will do depends on the type of this exception. The result may be loading the desired page or terminating the program.