How to get the page directory of a pointer in xv6
<p>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)...</p>

<pre><code>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-&gt;ts.cr3;
 cprintf("page directory base is: %p\n",cpu-&gt;ts.cr3);
 pde = &amp;pgdir[PDX(vaddr)];
 if(*pde &amp; 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 = &amp;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;

}
</code></pre>