简体   繁体   English

如何在 Linux kernel 中获取进程的虚拟地址

[英]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()...

  1. 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...) 以及如何使用这些函数?

  2. 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来获取虚拟地址空间的范围,但是不知道如何获取特定的虚拟地址。

  3. 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。

  1. 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文件。

  2. 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 都会有所不同。

  3. 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.

相关问题 Linux进程虚拟地址空间的地址范围 - Linux process virtual address space's address range 如何使用pid和不使用pid在linux中打印内核堆栈的低位和高位地址以进行处理 - How to print kernel stack's lower and upper address in linux for process using pid and without using pid 如何以编程方式获取Linux进程的堆栈开始和结束地址? - How programmatically get Linux process's stack start and end address? Linux内核:获取内核驱动程序的功能地址 - Linux kernel: get function address for kernel driver 虚拟地址到物理地址并在android linux内核中反转 - Virtual address to physical address and reverse in android linux kernel C中是否有function可以在Linux上获取当前进程的虚拟运行时间? - Is there a function in C to get the current process's virtual runtime on Linux? linux 内核 - 如何获取物理地址(内存管理)? - linux kernel - how to get physical address (memory management)? 如何从 Linux kernel 模块中的逻辑地址获取物理地址? - How to get the physical address from the logical one in a Linux kernel module? 如何从Linux内核的PID中获取进程描述符? - How can I get the process descriptor from a PID in Linux kernel? 有什么方法可以确定一个(多)线程/任务的堆栈地址在 Linux 上的进程的虚拟 memory 中开始? - Is there any way to determine a (multi) thread/task's stack address start in the virtual memory of a process on Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM