如何在xv6中获取指针的页面目录

时间:2016-10-11 01:21:07

标签: linux xv6

这是proc.c中的'translate()' 我想获得给定指针虚拟地址的物理地址,但我不知道如何获取指针pgdir(页面目录)......

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;

}

2 个答案:

答案 0 :(得分:2)

您需要使用argint()argptr()来阅读参数。

答案 1 :(得分:0)

有一个全局变量proc,它位于proc.h中。

proc.h, lines 34 to 43

// Per-CPU variables, holding pointers to the
// current cpu and to the current process.
// The asm suffix tells gcc to use "%gs:0" to refer to cpu
// and "%gs:4" to refer to proc.  seginit sets up the
// %gs segment register so that %gs refers to the memory
// holding those two variables in the local cpu's struct cpu.
// This is similar to how thread-local variables are implemented
// in thread libraries such as Linux pthreads.
extern struct cpu *cpu asm("%gs:0");       // &cpus[cpunum()]
extern struct proc *proc asm("%gs:4");     // cpus[cpunum()].proc

您可以在proc.c中或在包含proc.h的任何其他地方引用proc->pgdir

对于你的翻译功能看起来很值得我。

相关问题