【问题标题】:How to go about getting the physical address of a location in a file in Linux如何在 Linux 中获取文件中某个位置的物理地址
【发布时间】:2012-04-18 19:37:17
【问题描述】:

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

我不能在 C 中执行此操作,因为我必须将文件读入缓冲区,如果我尝试在 RAM 中获取物理(非虚拟)地址,我将获得缓冲区的地址而不是特定字节在文件中。

我们将不胜感激。

谢谢

【问题讨论】:

  • 文件中的字节没有您所想的地址。它们存储在磁盘上,而不是 RAM 中,并且您正在请求 RAM 中的地址。
  • 我将文件“临时”存储在 /shmem 中,该文件基本上映射到 RAM。
  • 文件中的字节没有内存地址,除非您将文件的内容读取到缓冲区或使用 mmap 映射它。
  • @Falcata:好的,它在 RAM 中。为什么需要物理地址?通常只有操作系统本身处理物理地址。
  • 我知道内存中有问题,导致设备错误。现在通常在嵌入式系统上,我会在设备上运行内存测试,但在这种特殊情况下,这是一个安全的设备......所以没有很多选择。我已经将故障范围缩小到一个位置,并且通过写出 /shmem 并将其读回,我已经能够在 RAM 中找到一个位置。现在我想获取问题发生的字节的物理地址.

标签: c linux memory virtual street-address


【解决方案1】:

通过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

【讨论】:

    【解决方案2】:

    为此,您应该直接通过设备检查文件系统。这意味着您应该能够找到位图表、索引节点表、目录条目和类似的东西。这对于现代和即将推出的文件系统(例如 Btrfs)来说并非易事。

    除此之外,您还应该处理块和扇区偏移或地址(可能是 LBA 或可能基于柱面)。

    所以,在我看来,答案是,或者至少,它的解决方案会非常复杂。

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2017-04-26
      • 2019-12-15
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多