查找虚拟内存地址的物理内存地址

查找虚拟内存地址的物理内存地址

问题描述:


  • 在Unix系统中,可以找到的物理内存地址对于一个给定的虚拟内存地址?如果是的话,怎么样?

我试图解决真正的问题是,我怎么能找到,如果操作系统两个虚拟地址映射到同一个物理区域?

The real problem I'm trying to solve is, how can I find out if the OS maps two virtual addresses to the exact same physical region?

例如。在下面的 smaps 例如,我怎么知道如果两个内存区域,事实上,物理上相同?

E.g. in the below smaps example, how do I know if both memory regions are, in fact, physically identical?

cat /proc/<pid>/smaps

...
7f7165d42000-7f7265d42000 r--p 00000000 00:14 641846                     /run/shm/test (deleted)
Size:            4194304 kB
Rss:             4194304 kB
Pss:             2097152 kB
...
VmFlags: rd mr mw me nr sd 
7f7265d42000-7f7365d42000 rw-s 00000000 00:14 641846                     /run/shm/test
Size:            4194304 kB
Rss:             4194304 kB
Pss:             2097152 kB
...
VmFlags: rd wr sh mr mw me ms sd 
...

奖励:的有没有办法简单地做到这一点编程在 C

Bonus: Is there a way to simply do it programmatically in C ?

我试图找出重复的,但无法找到一个恰当的。

I tried to look for duplicates but could not find a pertinent one.

在Linux下,你可以通过在解析文件做的/ proc /&LT; PID&GT; ,即: 地图页映射。有一个小的用户空间的工具,它会为你的这里

On Linux you can do it by parsing files in /proc/<pid>, namely, maps and pagemap. There is a little user-space tool that does it for you here.

编译(不需要特殊的选项),运行页面类型-p&LT; PID&GT; -l -N ,发现在第一列中的虚拟页地址,在第二列中读出的物理地址。

Compile it (no special options are needed), run page-types -p <pid> -l -N, find the virtual page address in the first column, read the physical address in the second column.

这应该很容易变成一个库,以编程方式使用。记住,该实用程序的某些操作需要root权限(比如读的/ proc / kpageflags ),但是,需要对这项工作没有。

It should be straightforward to turn this into a library and use programmatically. Bear in mind that some operations of the utility require root access (such as reading /proc/kpageflags), however, none is needed for this task.