Find physical memory address from virtual memory address

  • On Unix systems, can I find the physical memory address for a given virtual memory address? If so, how?

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

eg. in the smaps example smaps , how do I know if both areas of memory are 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 ... 

Bonus: is there a way to do this programmatically in C ?

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

+2
c memory-management linux unix
source share
1 answer

On Linux, you can do this by analyzing the files in /proc/<pid> , namely maps and pagemap . There is a small tool for user space that does this for you here .

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

It should just be turning it into a library and using it programmatically. Note that some utility operations require root access (for example, reading /proc/kpageflags ), but this task is not required.

+3
source share

All Articles