gpt4 book ai didi

c - 如何在 Linux 中获取文件中某个位置的物理地址

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:33 32 4
gpt4 key购买 nike

我正在尝试获取与文件中特定字节关联的(物理)位置。我该怎么做?

我不能在 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 .

关于c - 如何在 Linux 中获取文件中某个位置的物理地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216705/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com