title: How to get pgdir in xv6?
body: <p>I'm trying to add a system call to xv6 that provides the translated physical address given a virtual address. Combining the following function definitions, I have written a system call. Functions used:</p>
  
  <pre><code>static pte_t *
  walkpgdir(pde_t *pgdir, const void *va, int alloc)       -- vm.c
  </code></pre>
  
  <hr>
  
  <pre><code>char*
  uva2ka(pde_t *pgdir, char *uva)    --vm.c
  </code></pre>
  
  <hr>
  
  <p>Here is my code: </p>
  
  <p>proc.c</p>
  
  <pre><code>int addr_translate(char* virtual_address)
  {
      int physical_address;
      pde_t *pgdir,*pgtab,*pde;
  
      //must initialise pgdir
  
      pde = &amp;pgdir[PDX(virtual_address)];
      if(*pde &amp; PTE_P){
      pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
      }
      else
      {
      cprintf("\n PTE Not Present! - Invalid Virtual address\n");
      return -1;
      }
      cprintf("\n ----------------- \n");
      cprintf(" Page Directory Entry (PDE): %d\n",*pde);
      cprintf(" PTE_P : %d\n",PTE_P);
      cprintf("\n ----------------- \n");
  
      //uva2ka
      pte_t *pte;
      pte = &amp;pgtab[PTX(virtual_address)];
      physical_address=(char*)P2V(PTE_ADDR(*pte));
  
      cprintf(" --PHYSICAL ADDRESS-- %d\n",physical_address);
  
      return 0;
  
     }
  </code></pre>
  
  <p>sysproc.c</p>
  
  <pre><code>char*
  sys_addr_translation(char *s)
  {
    argstr(0, &amp;s);
    addr_translate(s);
    return s;
  }
  </code></pre>
  
  <p>addr_translation.c (From where I'm invoking my system call)</p>
  
  <pre><code>#include "types.h"
  #include "stat.h"
  #include "user.h"
  
  int main(int argc,char *argv[])
  {
      printf(1,"\n ----ADDRESS TRANSLATION----\n");
      char *virtual_addr;
      virtual_addr=argv[1];
  
      printf(1,"\n Entered virtual address: %s\n",virtual_addr);
      printf(1,"\n Status from system call: %s",addr_translation(virtual_addr));
      exit();
  }
  </code></pre>
  
  <p>Basically, I'm taking the virtual address as an argument from the user and passing it to my system call. I'm retrieving the virtual address using <code>argstr()</code> in my system call. </p>
  
  <p>The part where I'm stuck is, I'm not able to understand how to initialise pgdir (page directory) in my <code>add_translate()</code> function in <code>proc.c</code>. </p>