本文介绍了Linux操作系统(Ubuntu的),C语言:虚拟到物理地址的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,我有一个虚拟的获得物理地址的问题。

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?

我已经在一些SYS电话迷迷糊糊 /asm/io.h 其中 virt_to_phys()功能定义;然而,看来这头已经过时,我无法四处找工作。

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.

不过, io.h ,请访问: /usr/src/linux-headers-2.6.35-28-generic/arch/x86/include/asm / 。我现在的内核是 2.6.35-28 ,但 io.h 不包含在 / usr / include目录/ ASM /

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/?

因此​​,要重申:我需要一种方法来获取虚拟的物理地址。 preferably在运行时从应用程序中导出。但是,使用的显示器甚至是解决办法的/ proc / PID /图就行了。

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.

任何意见或建议,将大大AP preciated。

Any ideas or comments would be greatly appreciated.

修改
做了一些研究对这个话题我发现了一些有助于在这方面了。

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

原来,这比可行的多,但需要一点点的一种解决方法。
这里是一个一个简单的应用程序,分析了当前页映射。
有问题的文件,原来是(二进制文件) / proc /进程/页映射(包含虚拟页的物理映射)。无论如何,在连接的code可以进行修改,以作为显示器的应用程序或东西。

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.

感谢所有帮助和解答!

推荐答案

在用户code,你可以不知道对应于虚拟地址的物理地址。这是信息根本就没有出口在内核之外。它甚至可以在任何时候改变,特别是如果内核决定换出你的进程内存的一部分。

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.

的/ proc / $ PID /图,您有什么的虚拟的在你的程序的地址空间地址的对应信息(mmapped文件,堆,栈等)。这就是你会得到。

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.

如果你在内核code工作(你都没有,显然),你可以找到相应的内存页的物理地址。但即便如此 virt_to_phys 不是故事的全部;我建议你​​阅读(特别是章的和)。

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

ASM / io.h 是一个内核头。当你编译用户code这是不可用,因为它的内容只是就没有意义。它宣称的功能是不提供任何图书馆,只是在内核中。

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.

这篇关于Linux操作系统(Ubuntu的),C语言:虚拟到物理地址的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 00:57