Macros are defined in asm/pgtable.h which are important for the navigation and examination of page table entries. To navigate the page directories, three macros are provided which break up a linear address space into its component parts. pgd_offset() takes an address and the mm_struct for the process and returns the PGD entry that covers the requested address. pmd_offset() takes a PGD entry and an address and returns the relevant PMD. pte_offset() takes a PMD and returns the relevant PTE. The remainder of the linear address provided is the offset within the page. The relationship between these fields is illustrated in Figure 4.3
The second round of macros determine if the page table entries are present or may be used.
There is many parts of the VM which are littered with page table walk code and it is important to recognize it. A very simple example of a page table walk is the function follow_page() in mm/memory.c which is as follows;
405 static struct page * follow_page(struct mm_struct *mm, unsigned long address, int write) 406 { 407 pgd_t *pgd; 408 pmd_t *pmd; 409 pte_t *ptep, pte; 410 411 pgd = pgd_offset(mm, address); 412 if (pgd_none(*pgd) || pgd_bad(*pgd)) 413 goto out; 414 415 pmd = pmd_offset(pgd, address); 416 if (pmd_none(*pmd) || pmd_bad(*pmd)) 417 goto out; 418 419 ptep = pte_offset(pmd, address); 420 if (!ptep) 421 goto out; 422 423 pte = *ptep; 424 if (pte_present(pte)) { 425 if (!write || 426 (pte_write(pte) && pte_dirty(pte))) 427 return pte_page(pte); 428 } 429 430 out: 431 return 0; 432 }
It simply uses the three offset macros to navigate the page tables and the _none and _bad macros to make sure it is looking at a valid page table. The page table walk had effectively ended at line 423.
The third set of macros examine and set the permissions of an entry. The permissions determine what a userspace process can and cannot do with a particular page. For example, the kernel page table entries are never readable to a userspace process.
The fourth set of macros examine and set the state of an entry. There is only two states that are important in Linux, the dirty bit and the accessed bit. To check these bits, the macros pte_dirty and pte_young macros are used. To set the bits, the macros pte_mkdirty and pte_mkyoung are used and to clear them, the macros pte_mkclean and pte_old are available.