Search code examples
linuxxv6

How to get the page directory of a pointer in xv6


Here is my 'translate()' in proc.c I want to get the physical address given the virtual address of a pointer, but I don't know how to get the pointers pgdir(page directory)...

int translate(void* vaddr)
{
      cprintf("vaddr = %p\n",vaddr);
 int paddr;
 pde_t *pgdir;
 pte_t *pgtab;
 pde_t *pde;
 pte_t *pte;

 pgdir = (pde_t*)cpu->ts.cr3;
 cprintf("page directory base is: %p\n",cpu->ts.cr3);
 pde = &pgdir[PDX(vaddr)];
 if(*pde & PTE_P){
 pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
 }else{
 cprintf("pde = %d\n",*pde);
 cprintf("PTE_P = %d\n",PTE_P);
 cprintf("pte not present\n");
 return -1;
 }
 pte = &pgtab[PTX(vaddr)];
 paddr = PTE_ADDR(*pte);
  cprintf("the virtual address is %p\n",vaddr);
  cprintf("the physical address is %d\n",paddr);

  return 0;

}

Solution

  • You need to use argint() or argptr() to read the argument.