1

这是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;

}
4

2 回答 2

2

您需要使用argint()argptr()阅读论点。

于 2017-04-18T15:46:13.987 回答
1

procproc.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->pgdir在 proc.c 或包含 proc.h 的任何其他地方引用。

对于它的价值,你的翻译功能对我来说看起来不错。

于 2016-10-14T08:17:53.560 回答