1. 程式人生 > >Linux 下獲取某虛擬地址對應的頁表資訊

Linux 下獲取某虛擬地址對應的頁表資訊

即給出某個虛擬地址,通過查詢頁表資訊,將其轉換成實體地址。

//該函式用於查詢address對應的頁表。摘自arch/x86/mm/fault.c

static void dump_pagetable(unsigned long address)

{

pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK); //頁目錄表基址

pgd_t *pgd = base + pgd_index(address);

pud_t *pud;// 頁表級數大於 3

pmd_t *pmd;// 頁表級數大於 2

pte_t *pte;

if (bad_address(pgd))

goto bad;

printk("PGD %lx ", pgd_val(*pgd));

if (!pgd_present(*pgd))

goto out;

pud = pud_offset(pgd, address);

if (bad_address(pud))

goto bad;

printk("PUD %lx ", pud_val(*pud));

if (!pud_present(*pud) || pud_large(*pud))

goto out;

pmd = pmd_offset(pud, address);

if (bad_address(pmd))

goto bad;

printk("PMD %lx ", pmd_val(*pmd));

if (!pmd_present(*pmd) || pmd_large(*pmd))

goto out;

pte = pte_offset_kernel(pmd, address);

if (bad_address(pte))

goto bad;

printk("PTE %lx", pte_val(*pte));

out:

printk("/n");

return;

bad:

printk("BAD/n");

}

 之後轉換成實體地址: pa = (pte_val(*pte) & PAGE_MASK | (address & ~PAGE_MASK);