/proc/[pid]/pagemaps and /proc/[pid]/maps | linux

15
我试图理解标题中提到的两个文件。我已经查阅了位数的含义;然而,我无法理解如何从中提取有用的信息(或者我只是以错误的方式进行了尝试)。
让我来解释一下:pagemaps是一个相当新的“特性”伪文件,其中包含分配给当前[pid]的虚拟页面的物理帧信息。也就是说,给定一个从地址x开始的虚拟页面,比如‘vas’表示虚拟地址开始,我可以使用vas对pagemap文件进行索引,以获取映射的物理页面帧的64位。这些位包含有关该虚拟页面的信息。但是,当我提取这些位并进行位移时,我会迷失在我所看到的内容中。
这些位的表示如下:0-54是页面帧号,55-60是页面位移,第63位是存在位,还有其他我不太感兴趣的位。在使用/proc/[pid]/maps中的vas地址进行一些映射后,似乎每个进程的页面都被交换了,即第63位始终为零。 :(
我想问题应该是,我应该如何有效地使用pagemaps来获得由/proc/[pid]/maps给出的地址的等效物理地址?
公平地说,我几天前发布了类似的问题,但方法有点不同。
如果有人能在这个问题上提供一些帮助,我将非常感激。
===编辑===
回应下面的评论:
我正在读取/proc/[pid]/maps中的一行,这些行看起来像:
00400000-00401000 r-xp 00000000 08:01 8915461 /home/janjust/my_programs/shared_mem 7ffffef1b000-7ffffef3c000 rw-p 00000000 00:00 0 [stack]
然后我提取它所涉及的虚拟页数,并索引一个二进制文件/proc/[pid]/pagemaps,对于每个虚拟页面,我都可以提取它分配给的物理页面。
输出看起来像:
00400000-00401000 r-xp 00000000 08:01 8915461 /home/janjust/my_programs/shared_mem num_pages: 1 : 86000000001464C6
虚拟范围内每个虚拟页的一个物理地址。
读取该行并提取物理地址的代码如下:
74     /* process /proc/pid/maps, by line*/
75     while(fgets(line, 256, in_map) != NULL){
76         unsigned long vas;
77         unsigned long vae;
78         int num_pages;
79 
80         //print line
81         printf("%s", line);
82 
83         /*scan for the virtual addresses*/
84         n = sscanf(line, "%lX-%lX", &vas, &vae);
85         if(n != 2){
86             printf("Involid line read from %s\n",maps);
87             continue;
88         }
89 
90         num_pages = (vae - vas) / PAGE_SIZE;
91         printf("num_pages: %d\n", num_pages);
92 
93         if(num_pages > 0){
94             long index  = (vas / PAGE_SIZE) * sizeof(unsigned long long);
95             off64_t o;
96             ssize_t t;
97 
98             /* seek to index in pagemaps */
99             o = lseek64(pm, index, SEEK_SET);
100             if (o != index){
101                 printf("Error seeking to o:%ld, index:%ld.\n", o, index);
102             }
103 
104             /* map the virtual to physical page */
105             while(num_pages > 0){
106                 unsigned long long pa;
107 
108                 /* Read a 64-bit word from each pagemap file... */
109                 t = read(pm, &pa, sizeof(unsigned long long));
110                 if(t < 0){
111                     printf("Error reading file \"%s\" \n", page_map);
112                     goto next_line;
113                 }
114                 printf(": %016llX\n", pa);

然而,尽管我认为我得到了正确的输出,但索引似乎要么类型不匹配,要么发生了其他问题: 例如,maps中的[shared mem]行给出了错误的索引; 然而,我仍然能够扫描二进制文件并获取物理页面地址。
以下是该输出的示例:
969 7f7f08d58000-7f7f08d59000 rw-s 00000000 00:04 0    /SYSV00003039 (deleted)
970 num_pages: 1
971 Error seeking to o:-1081840960, index:273796065984.
972 : 8600000000148267

好的,最后我应该说一下,这是在64位操作系统下出现的问题,在32位操作系统下不会出现。


有趣。我实际上也在尝试做同样的事情,但是我没有得到合理的结果。我真正想知道的是/proc/[pid]/pagemap中的索引。在你的代码(以及我的代码)中,你有这个:`long index = (vas / PAGE_SIZE) * sizeof(unsigned long long);`我想知道的是PAGE_SIZE。大多数架构都允许使用大页 - 例如,在x86上,页面可以是4kB或4MB。这不会使使用统一的PAGE_SIZE索引/proc/[pid]/pagemap变得不可行吗? - Ted Middleton
3个回答

7

下面是一个使用/proc/<pid>/pagemap+ /proc/<pid>/maps的转储示例程序,它可以将虚拟地址转换为物理地址:

这里有一个示例程序

以下程序使用两种方法:/proc/<pid>/pagemap+ /proc/<pid>/maps,以转储页面表信息,并显示它们如何共同使用。用法:

sudo ./pagemap_dump.out <pid>

示例输出:

addr pfn soft-dirty file/shared swapped present library
400000 12845d 0 1 0 1 /bin/bash
401000 12845e 0 1 0 1 /bin/bash
402000 12845f 0 1 0 1 /bin/bash

这告诉我们,例如虚拟地址0x400000映射到物理地址0x12845d000
为什么需要sudohttps://unix.stackexchange.com/questions/345915/how-to-change-permission-of-proc-self-pagemap-file/383838#383838 此程序分两步进行:
  • parse the human readable lines lines from /proc/<pid>/maps. This files contains lines of form:

    7ffff7b6d000-7ffff7bdd000 r-xp 00000000 fe:00 658                        /lib/libuClibc-1.0.22.so
    

    which gives us:

    • 7f8af99f8000-7f8af99ff000: a virtual address range that belong to the process, possibly containing multiple pages.
    • /lib/libuClibc-1.0.22.so the name of the library that owns that memory.
  • loop over each page of each address range, and ask /proc/<pid>/pagemap for more information about that page, including the physical address.

pagemap_dump.c

#define _XOPEN_SOURCE 700
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct {
    uint64_t pfn : 55;
    unsigned int soft_dirty : 1;
    unsigned int file_page : 1;
    unsigned int swapped : 1;
    unsigned int present : 1;
} PagemapEntry;

/* Parse the pagemap entry for the given virtual address.
 *
 * @param[out] entry      the parsed entry
 * @param[in]  pagemap_fd file descriptor to an open /proc/pid/pagemap file
 * @param[in]  vaddr      virtual address to get entry for
 * @return 0 for success, 1 for failure
 */
int pagemap_get_entry(PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
{
    size_t nread;
    ssize_t ret;
    uint64_t data;

    nread = 0;
    while (nread < sizeof(data)) {
        ret = pread(pagemap_fd, ((uint8_t*)&data) + nread, sizeof(data) - nread,
                (vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread);
        nread += ret;
        if (ret <= 0) {
            return 1;
        }
    }
    entry->pfn = data & (((uint64_t)1 << 55) - 1);
    entry->soft_dirty = (data >> 55) & 1;
    entry->file_page = (data >> 61) & 1;
    entry->swapped = (data >> 62) & 1;
    entry->present = (data >> 63) & 1;
    return 0;
}

/* Convert the given virtual address to physical using /proc/PID/pagemap.
 *
 * @param[out] paddr physical address
 * @param[in]  pid   process to convert for
 * @param[in] vaddr virtual address to get entry for
 * @return 0 for success, 1 for failure
 */
int virt_to_phys_user(uintptr_t *paddr, pid_t pid, uintptr_t vaddr)
{
    char pagemap_file[BUFSIZ];
    int pagemap_fd;

    snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t)pid);
    pagemap_fd = open(pagemap_file, O_RDONLY);
    if (pagemap_fd < 0) {
        return 1;
    }
    PagemapEntry entry;
    if (pagemap_get_entry(&entry, pagemap_fd, vaddr)) {
        return 1;
    }
    close(pagemap_fd);
    *paddr = (entry.pfn * sysconf(_SC_PAGE_SIZE)) + (vaddr % sysconf(_SC_PAGE_SIZE));
    return 0;
}

int main(int argc, char **argv)
{
    char buffer[BUFSIZ];
    char maps_file[BUFSIZ];
    char pagemap_file[BUFSIZ];
    int maps_fd;
    int offset = 0;
    int pagemap_fd;
    pid_t pid;

    if (argc < 2) {
        printf("Usage: %s pid\n", argv[0]);
        return EXIT_FAILURE;
    }
    pid = strtoull(argv[1], NULL, 0);
    snprintf(maps_file, sizeof(maps_file), "/proc/%ju/maps", (uintmax_t)pid);
    snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t)pid);
    maps_fd = open(maps_file, O_RDONLY);
    if (maps_fd < 0) {
        perror("open maps");
        return EXIT_FAILURE;
    }
    pagemap_fd = open(pagemap_file, O_RDONLY);
    if (pagemap_fd < 0) {
        perror("open pagemap");
        return EXIT_FAILURE;
    }
    printf("addr pfn soft-dirty file/shared swapped present library\n");
    for (;;) {
        ssize_t length = read(maps_fd, buffer + offset, sizeof buffer - offset);
        if (length <= 0) break;
        length += offset;
        for (size_t i = offset; i < (size_t)length; i++) {
            uintptr_t low = 0, high = 0;
            if (buffer[i] == '\n' && i) {
                const char *lib_name;
                size_t y;
                /* Parse a line from maps. Each line contains a range that contains many pages. */
                {
                    size_t x = i - 1;
                    while (x && buffer[x] != '\n') x--;
                    if (buffer[x] == '\n') x++;
                    while (buffer[x] != '-' && x < sizeof buffer) {
                        char c = buffer[x++];
                        low *= 16;
                        if (c >= '0' && c <= '9') {
                            low += c - '0';
                        } else if (c >= 'a' && c <= 'f') {
                            low += c - 'a' + 10;
                        } else {
                            break;
                        }
                    }
                    while (buffer[x] != '-' && x < sizeof buffer) x++;
                    if (buffer[x] == '-') x++;
                    while (buffer[x] != ' ' && x < sizeof buffer) {
                        char c = buffer[x++];
                        high *= 16;
                        if (c >= '0' && c <= '9') {
                            high += c - '0';
                        } else if (c >= 'a' && c <= 'f') {
                            high += c - 'a' + 10;
                        } else {
                            break;
                        }
                    }
                    lib_name = 0;
                    for (int field = 0; field < 4; field++) {
                        x++;
                        while(buffer[x] != ' ' && x < sizeof buffer) x++;
                    }
                    while (buffer[x] == ' ' && x < sizeof buffer) x++;
                    y = x;
                    while (buffer[y] != '\n' && y < sizeof buffer) y++;
                    buffer[y] = 0;
                    lib_name = buffer + x;
                }
                /* Get info about all pages in this page range with pagemap. */
                {
                    PagemapEntry entry;
                    for (uintptr_t addr = low; addr < high; addr += sysconf(_SC_PAGE_SIZE)) {
                        /* TODO always fails for the last page (vsyscall), why? pread returns 0. */
                        if (!pagemap_get_entry(&entry, pagemap_fd, addr)) {
                            printf("%jx %jx %u %u %u %u %s\n",
                                (uintmax_t)addr,
                                (uintmax_t)entry.pfn,
                                entry.soft_dirty,
                                entry.file_page,
                                entry.swapped,
                                entry.present,
                                lib_name
                            );
                        }
                    }
                }
                buffer[y] = '\n';
            }
        }
    }
    close(maps_fd);
    close(pagemap_fd);
    return EXIT_SUCCESS;
}

1
我认为在读取pagemap条目时出现了错误。如果由于任何原因首先只读取了两个字节,则下一次迭代将从original_offset + 2读取8个字节。因此,要么删除pread的第四个参数中的nread += ret+ nread,要么像这样更改它:ret = pread(pagemap_fd, ((char*)&data) + nread, sizeof(data) - nread, (vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread); - Phidelux
1
似乎还有另一个错误,因为您屏蔽了54位而不是55位(如文档所述:“位0-54页帧号(PFN)如果存在”)。 因此,应该是 entry->pfn = data & (((uint64_t)1 << 54) - 1); - Phidelux
@Phidelux 感谢您的修复。我会尽快审核它们。 - Ciro Santilli OurBigBook.com
@Phidelux 好的,-nread 我已经在我的上游修复了,但是忘记在这里应用了 :-) 你关于 54 和 55 的观点完全正确,现在已经修复。如果您发现任何其他错误,请告诉我。 - Ciro Santilli OurBigBook.com

3

哦,好的,索引是正确的,但将8字节的off64_t与长整型索引进行比较时,解释o时出现了错误,这就是我遇到该错误的原因。 哈!这是一个愚蠢的错误。 因此,添加适当的头文件就解决了这个问题。

缺少头文件 :-/ 叹气 修复了将off64_t与无符号长整型进行比较的问题。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接