Alex Rivera | Logout

Is there any API for determining the physical address from virtual address in Linux?

Asked 2011-04-21T18:52:52.830
49

Is there any API for determining the physical address from virtual address in Linux operating system?

Edit
Report

2 Answers

19

As answered before, normal programs should not need to worry about physical addresses as they run in a virtual address space with all its conveniences. Furthermore, not every virtual address has a physical address, the may belong to mapped files or swapped pages. However, sometimes it may be interesting to see this mapping, even in userland.

For this purpose, the Linux kernel exposes its mapping to userland through a set of files in the /proc. The documentation can be found here. Short summary:

  1. /proc/$pid/maps provides a list of mappings of virtual addresses together with additional information, such as the corresponding file for mapped files.
  2. /proc/$pid/pagemap provides more information about each mapped page, including the physical address if it exists.

This website provides a C program that dumps the mappings of all running processes using this interface and an explanation of what it does.

answered 2012-12-19T10:01:13.777
7

I wonder why there is no user-land API.

Because user land memory's physical address is unknown.

Linux uses demand paging for user land memory. Your user land object will not have physical memory until it is accessed. When the system is short of memory, your user land object may be swapped out and lose physical memory unless the page is locked for the process. When you access the object again, it is swapped in and given physical memory, but it is likely different physical memory from the previous one. You may take a snapshot of page mapping, but it is not guaranteed to be the same in the next moment.

So, looking for the physical address of a user land object is usually meaningless.

answered 2013-01-29T19:01:58.500

Your Answer