Linux (Ubuntu), C language: Virtual to Physical Address Translation
Asked Answered
A

3

9

As the title suggests, I have a problem of obtaining the physical address from a virtual one.

Let me explain: Given a variable declaration in process space, how can I derive it's physical address mapped by the OS?

I've stumbled upon some sys calls /asm/io.h where the virt_to_phys() function is defined; however it seems this header is outdated and I can't find a work around.

However; io.h is available at: /usr/src/linux-headers-2.6.35-28-generic/arch/x86/include/asm/. My current kernel is 2.6.35-28, but io.h isn't included in /usr/include/asm/?

So, to reiterate: I need a way to get the physical address from virtual. Preferably derived from within the application at runtime. But even a workaround of using a monitor of /proc/PID/maps will do.

Any ideas or comments would be greatly appreciated.


EDIT After doing a bit of research on this topic I found something that helps in this regard.

It turns out this is more than doable, although requires a bit of a workaround. Here is a link to a simple app that analyses the current mapped pages. The file in question turns out is (a binary file) /proc/pid/pagemap (contains the physical mapping of virtual pages). Anyway, the code in that link can be modified to serve as a monitor app or something.

I needed the physical address for cache simulation purposes.

Thanks for all the help and answers!

Andrew answered 5/6, 2011 at 20:38 Comment(1)
What do you need it for?Ackack
C
21

In user code, you can't know the physical address corresponding to a virtual address. This is information is simply not exported outside the kernel. It could even change at any time, especially if the kernel decides to swap out part of your process's memory.

In /proc/$pid/maps, you have information about what the virtual addresses in your program's address space correspond to (mmapped files, heap, stack, etc.). That's all you'll get.

If you're working on kernel code (which you aren't, apparently), you can find out the physical address corresponding to a page of memory. But even then virt_to_phys isn't the whole story; I recommend reading Linux Device Drivers (especially chapters 8 and 15).

The header asm/io.h is a kernel header. It's not available when you're compiling user code because its contents just wouldn't make sense. The functions it declares aren't available in any library, only in the kernel.

Catchpenny answered 5/6, 2011 at 20:57 Comment(2)
Exactly what I was going to say, but with even more detail! +1Mosasaur
+1 I would like to point to that if the memory is swapped out a physical location doesn't even existSuperable
B
3

As partially answered by the original poster, 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 with their virtual addresses and the corresponding file for mapped files.
  2. /proc/$pid/pagemap provides more information about each mapped page, including the physical address if it exists.

As the original poster found later, this example provides a sample implementation of how to use these files.

Battaglia answered 19/12, 2012 at 9:53 Comment(0)
B
1

Pass the virtual address to the kernel using systemcall/procfs and use vmalloc_to_pfn. Return the Physical address through procfs/registers.

Bangtail answered 23/11, 2011 at 20:9 Comment(1)
But isn't it so, that vmalloc_to_pfn returns Page Frame Number, and not an address? (e.g. #11443273 ) Now should the PFN be converted to an address, then?Professor

© 2022 - 2024 — McMap. All rights reserved.