0

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:

static pte_t *
walkpgdir(pde_t *pgdir, const void *va, int alloc)       -- vm.c

char*
uva2ka(pde_t *pgdir, char *uva)    --vm.c

Here is my code:

proc.c

int addr_translate(char* virtual_address)
{
    int physical_address;
    pde_t *pgdir,*pgtab,*pde;

    //must initialise pgdir

    pde = &pgdir[PDX(virtual_address)];
    if(*pde & 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 = &pgtab[PTX(virtual_address)];
    physical_address=(char*)P2V(PTE_ADDR(*pte));

    cprintf(" --PHYSICAL ADDRESS-- %d\n",physical_address);

    return 0;

   }

sysproc.c

char*
sys_addr_translation(char *s)
{
  argstr(0, &s);
  addr_translate(s);
  return s;
}

addr_translation.c (From where I'm invoking my system call)

#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();
}

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 argstr() in my system call.

The part where I'm stuck is, I'm not able to understand how to initialise pgdir (page directory) in my add_translate() function in proc.c.

danglingpointer
  • 4,708
  • 3
  • 24
  • 42

2 Answers2

0

For the line: physical_address=(char*)P2V(PTE_ADDR(*pte)); I think this should be V2P, because what you want is physical address, right? :)

Meilan
  • 434
  • 6
  • 17
0

You can use struct proc *curproc = myproc(); and then you'll have access to the pgdir through curproc->pgdir.

Danilo Favato
  • 283
  • 1
  • 11