首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xv6中获取指针的页目录

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

Stack Overflow用户
提问于 2016-10-11 01:21:06
回答 2查看 2.6K关注 0票数 1

这里是我在proc.c中的“translate()”,我希望得到给定指针的虚拟地址的物理地址,但是我不知道如何获得指针pgdir(页目录).

代码语言:javascript
复制
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;

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-18 15:46:13

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

票数 2
EN

Stack Overflow用户

发布于 2016-10-14 08:17:53

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

代码语言:javascript
复制
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的任何其他地方引用它。

对我来说,你的翻译功能看起来很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39969116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档