我正在尝试获取与文件中特定字节关联的(物理)位置。我该怎么做?
我不能在 C 中执行此操作,因为我必须将文件读入缓冲区,如果我尝试获取 RAM 中的物理(而非虚拟)地址,我将获取缓冲区的地址而不是特定字节在文件中。
帮助将不胜感激。
谢谢
通过mmap
将共享内存映射到您的进程,访问包含错误数据的页面,然后读取/proc/self/pagemap
查找有关虚拟内存页如何映射到物理内存的信息。
* /proc/pid/pagemap. This file lets a userspace process find out which
physical frame each virtual page is mapped to. It contains one 64-bit
value for each virtual page, containing the following data (from
fs/proc/task_mmu.c, above pagemap_read):
* Bits 0-54 page frame number (PFN) if present
* Bits 0-4 swap type if swapped
* Bits 5-54 swap offset if swapped
* Bits 55-60 page shift (page size = 1<<page shift)
* Bit 61 reserved for future use
* Bit 62 page swapped
* Bit 63 page present
If the page is not present but in swap, then the PFN contains an
encoding of the swap file number and the page's offset into the
swap. Unmapped pages return a null PFN. This allows determining
precisely which pages are mapped (or in swap) and comparing mapped
pages between processes.
Efficient users of this interface will use /proc/pid/maps to
determine which areas of memory are actually mapped and llseek to
skip over unmapped regions.
注意:这似乎只适用于较新的内核。另外,这里是 how to translate the PFN into a physical address .
我是一名优秀的程序员,十分优秀!