2016-10-11 26 views
1

這是我在proc.c中的'translate()' 我想獲得給定虛擬地址的指針的物理地址,但我不知道如何拿到三分pgdir(頁目錄)...如何獲得在xv6中的指針的頁面目錄

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; 

} 

回答

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->pgdir或其他任何地方,proc.h是包括在內。

爲什麼值得您的翻譯功能看起來不錯。

2

您需要使用argint()argptr()來讀取參數。

相關問題