[英]How to get process's virtual address in Linux kernel
Currently, I'm trying to figure out how to get the virtual address (VA) of a specific process in the Linux kernel, since there are several functions taking VA as an argument related to different page directories, including pgd_offset(), pgd_index(), p4d_offset(), p4d_index()...目前,我正在尝试弄清楚如何在 Linux kernel 中获取特定进程的虚拟地址(VA),因为有几个函数将 VA 作为与不同页面目录相关的参数,包括 pgd_offset()、pgd_index( ), p4d_offset(), p4d_index()...
Could anyone explain the functions of these functions, including xxx_offset(), xxx_index()?(xxx:pgd, p4d, pmd...) And how to use these functions?谁能解释一下这些函数的作用,包括 xxx_offset(), xxx_index()?(xxx:pgd, p4d, pmd...) 以及如何使用这些函数?
What does the VA mean when it is taken as an argument of functions mentioned above, is that the virtual address of the process?当它作为上述函数的参数时,VA是什么意思,是进程的虚拟地址吗? And how can I get the VA of a specific process?
以及如何获得特定流程的 VA? I've already known that we can use process's task_struct->mm->mmap to get the range of the virtual address space, but no idea about how to get a specific virtual address.
我已经知道我们可以使用进程的task_struct->mm->mmap来获取虚拟地址空间的范围,但是不知道如何获取特定的虚拟地址。
Is the task_struct->mm->pgd_t indicating the base address of PGD_directory? task_struct->mm->pgd_t是表示PGD_directory的基地址吗?
Your question doesn't really make sense.你的问题真的没有意义。 You don't "get a virtual address of a process".
您不会“获得进程的虚拟地址”。 A process has a virtual address space that serves as a virtual memory map for data, code, stack, heap, etc.
进程有一个虚拟地址空间,用作数据、代码、堆栈、堆等的虚拟 memory map。
Those functions are taking a single virtual address within the process virtual address space and helping with walking through the page tables to find its page table entry and then its physical address (or checking page table entry flags).这些函数在进程虚拟地址空间中获取单个虚拟地址,并帮助遍历页表以查找其页表条目,然后找到其物理地址(或检查页表条目标志)。 In Linux, there are 4 page tables levels to go through to get to the page table entry.
在 Linux 中,有 4 个页表级别,通过 go 到达页表条目。 Normally the levels are pgd (page table directory), pud (page upper directory), pmd (page mid directory), and pte (page table entry).
通常级别是pgd(页表目录)、pud(页上层目录)、pmd(页中间目录)和pte(页表条目)。 But I think recently p4d was added as an extra page table level.
但我认为最近 p4d 被添加为额外的页表级别。 Typically, the address of the page directory (top-level page table) is stored in the CR3 register.
通常,页目录(顶级页表)的地址存储在 CR3 寄存器中。 So you use that address to access the directory, then use the pgd_index and pgd_offset to find the address of the next level (p4d) you need to look into, and repeat till you hit the pte.
因此,您使用该地址访问目录,然后使用 pgd_index 和 pgd_offset 找到您需要查看的下一级(p4d)的地址,并重复直到您点击 pte。 A useful file to see this in action is the
mm/page_walk.c
file.一个有用的文件是
mm/page_walk.c
文件。
A process accesses memory during its runtime and typically this memory is referred to by virtual addresses.进程在其运行时访问 memory,通常这个 memory 由虚拟地址引用。 When it accesses an address that isn't in the TLB, the address must be walked through as described above to find out its location and permissions flags.
当它访问不在 TLB 中的地址时,必须按上述方式遍历该地址以找出其位置和权限标志。 There is no "getting the VA of a process", but when your program uses mmap or malloc and you get addresses of variables, those addresses are typically virtual addresses.
没有“获取进程的 VA”,但是当您的程序使用 mmap 或 malloc 并且您获得变量的地址时,这些地址通常是虚拟地址。 You can look in
/proc/proc_number/maps
to see the virtual address layout of a process with PID proc_number
.您可以查看
/proc/proc_number/maps
以查看具有 PID proc_number
的进程的虚拟地址布局。 Note that with address space layout randomization turned on, this map will be different every time you run the same program.请注意,在地址空间布局随机化打开的情况下,每次运行相同的程序时,此 map 都会有所不同。
I'm not sure, but you probably can test it by comparing that variable with the pgd address used in the page_walk.c file I linked above.我不确定,但您可能可以通过将该变量与我在上面链接的 page_walk.c 文件中使用的 pgd 地址进行比较来测试它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.