Finding the PDE offset and the PTE offset from a virtual add
Finding the PDE offset and the PTE offset from a virtual add
How do you find the PDE offset and PTE offset from a virtual address?
Re:Finding the PDE offset and the PTE offset from a virtual
PDE_OFFSET = (addr / PAGE_SIZE / PAGES_PER_PDE) * sizeof(PDE_ENTRY)
PTE_OFFSET = (addr / PAGE_SIZE % PAGES_PER_PDE) * sizeof(PTE_ENTRY)
PAGE_SIZE = 4096
PAGES_PER_PDE = 1024
I think the size of each entry is 4 bytes but I'm not sure of that.
Disclaimer: This is off the top of my head and not checked against documentation.
PTE_OFFSET = (addr / PAGE_SIZE % PAGES_PER_PDE) * sizeof(PTE_ENTRY)
PAGE_SIZE = 4096
PAGES_PER_PDE = 1024
I think the size of each entry is 4 bytes but I'm not sure of that.
Disclaimer: This is off the top of my head and not checked against documentation.
Re:Finding the PDE offset and the PTE offset from a virtual
Wouldn't it be like this?
The code is heavily based on the fact that all PDEs start addresses are 4MB-aligned and thus only rely on the last ten bits of the address, and the PTEs are always 4KB-aligned and so we use the next/previous (depends on how you look at it) 10 bits.
Code: Select all
/** Gets the page directory entry for a given virtual address.
*/
unsigned long get_pde(unsigned long *va)
{
unsigned long first_ten = ((unsigned long)va) >> 22; // Top ten bits tell index of pde
return page_directory[first_ten]; // Use global pointer to page directory
}
/** Gets the page table entry for a given virtual address.
*/
unsigned long get_pte(unsigned long *va)
{
unsigned long pde = get_pde(va);
unsigned long *pde_address = (unsigned long *)(pde & 0xFFFFF000); // Clear all flags in bits 0-11 to obtain addr
unsigned long next_ten = (((unsigned long)va) >> 12) & 0x3FF; // Next ten bits tell index of pte in table
return pde_address[next_ten];
}
Re:Finding the PDE offset and the PTE offset from a virtual
I'm pretty sure thats equivalent to my code. The point of mine was to explain how its done without doing it for him .
Re:Finding the PDE offset and the PTE offset from a virtual
Oops ;D Forgot about the MT-philosophy for a moment