zoukankan      html  css  js  c++  java
  • How to translate virtual to physical addresses through /proc/pid/pagemap

    墙外通道:http://fivelinesofcode.blogspot.com/2014/03/how-to-translate-virtual-to-physical.html

    I currently work on a project where I need to make translations for virtual addresses of user-level application to physical addresses in Linux. I implemented my own system call to do that, but had hard times with verifying the results I'm getting.

    Later I found out that in newer kernels there is a really nice virtual file in the /proc file system to get this information. I tried to cat it, doing cat /proc/self/pagemap, and got terrible binary output in my console.

    So, it looks like working with this file is not such a pleasant experience. It's a binary file with all that it implies. I found couple of scripts that access this file and provide you with a nice text result, but unfortunately those were written in perl and ruby, and I needed to run it on very minimalistic embedded system. I needed something that fits into a single binary.

    Long story short, I decided to bite the bullet and write a tool in C. My contribution might be helpful for someone, that's why I'm sharing this code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <assert.h>
    #include <errno.h>
    #include <stdint.h>
    
    #define PAGEMAP_ENTRY 8
    #define GET_BIT(X,Y) (X & ((uint64_t)1<<Y)) >> Y
    #define GET_PFN(X) X & 0x7FFFFFFFFFFFFF
    
    const int __endian_bit = 1;
    #define is_bigendian() ( (*(char*)&__endian_bit) == 0 )
    
    int i, c, pid, status;
    unsigned long virt_addr; 
    uint64_t read_val, file_offset;
    char path_buf [0x100] = {};
    FILE * f;
    char *end;
    
    int read_pagemap(char * path_buf, unsigned long virt_addr);
    
    int main(int argc, char ** argv){
       //printf("%lu
    ", GET_BIT(0xA680000000000000, 63));
       //return 0;
       if(argc!=3){
          printf("Argument number is not correct!
     pagemap PID VIRTUAL_ADDRESS
    ");
          return -1;
       }
       if(!memcmp(argv[1],"self",sizeof("self"))){
          sprintf(path_buf, "/proc/self/pagemap");
          pid = -1;
       }
       else{
             pid = strtol(argv[1],&end, 10);
             if (end == argv[1] || *end != '' || pid<=0){ 
                printf("PID must be a positive number or 'self'
    ");
                return -1;
                }
           }
       virt_addr = strtol(argv[2], NULL, 16);
       if(pid!=-1)
          sprintf(path_buf, "/proc/%u/pagemap", pid);
       
       read_pagemap(path_buf, virt_addr);
       return 0;
    }
    
    int read_pagemap(char * path_buf, unsigned long virt_addr){
       printf("Big endian? %d
    ", is_bigendian());
       f = fopen(path_buf, "rb");
       if(!f){
          printf("Error! Cannot open %s
    ", path_buf);
          return -1;
       }
       
       //Shifting by virt-addr-offset number of bytes
       //and multiplying by the size of an address (the size of an entry in pagemap file)
       file_offset = virt_addr / getpagesize() * PAGEMAP_ENTRY;
       printf("Vaddr: 0x%lx, Page_size: %d, Entry_size: %d
    ", virt_addr, getpagesize(), PAGEMAP_ENTRY);
       printf("Reading %s at 0x%llx
    ", path_buf, (unsigned long long) file_offset);
       status = fseek(f, file_offset, SEEK_SET);
       if(status){
          perror("Failed to do fseek!");
          return -1;
       }
       errno = 0;
       read_val = 0;
       unsigned char c_buf[PAGEMAP_ENTRY];
       for(i=0; i < PAGEMAP_ENTRY; i++){
          c = getc(f);
          if(c==EOF){
             printf("
    Reached end of the file
    ");
             return 0;
          }
          if(is_bigendian())
               c_buf[i] = c;
          else
               c_buf[PAGEMAP_ENTRY - i - 1] = c;
          printf("[%d]0x%x ", i, c);
       }
       for(i=0; i < PAGEMAP_ENTRY; i++){
          //printf("%d ",c_buf[i]);
          read_val = (read_val << 8) + c_buf[i];
       }
       printf("
    ");
       printf("Result: 0x%llx
    ", (unsigned long long) read_val);
       //if(GET_BIT(read_val, 63))
       if(GET_BIT(read_val, 63))
          printf("PFN: 0x%llx
    ",(unsigned long long) GET_PFN(read_val));
       else
          printf("Page not present
    ");
       if(GET_BIT(read_val, 62))
          printf("Page swapped
    ");
       fclose(f);
       return 0;
    }

    And now how you use it. It's very simple. Of course you need to compile it. Then you need to find out what mapping your target process does have. You can do that by reading /proc/pid/maps file. Fortunately that file is human readable.
    When you know a valid virtual address, you can pass it to our tool to get actual value from pagemap, including physical frame number. Here is an example:

    $ #let's find get virtual address of a page
    $ cat /proc/self/maps 
    00400000-0040b000 r-xp 00000000 08:02 1177367                            /bin/cat
    0060a000-0060b000 r--p 0000a000 08:02 1177367                            /bin/cat
    0060b000-0060c000 rw-p 0000b000 08:02 1177367                            /bin/cat
    0223a000-0225b000 rw-p 00000000 00:00 0                                  [heap]
    7fe7e15e1000-7fe7e1cc3000 r--p 00000000 08:02 1577390                    /usr/lib/locale/locale-archive
    7fe7e1cc3000-7fe7e1e80000 r-xp 00000000 08:02 527324                     /lib/x86_64-linux-gnu/libc-2.17.so
    7fe7e1e80000-7fe7e2080000 ---p 001bd000 08:02 527324                     /lib/x86_64-linux-gnu/libc-2.17.so
    7fe7e2080000-7fe7e2084000 r--p 001bd000 08:02 527324                     /lib/x86_64-linux-gnu/libc-2.17.so
    7fe7e2084000-7fe7e2086000 rw-p 001c1000 08:02 527324                     /lib/x86_64-linux-gnu/libc-2.17.so
    7fe7e2086000-7fe7e208b000 rw-p 00000000 00:00 0 
    7fe7e208b000-7fe7e20ae000 r-xp 00000000 08:02 527300                     /lib/x86_64-linux-gnu/ld-2.17.so
    7fe7e228d000-7fe7e2290000 rw-p 00000000 00:00 0 
    7fe7e22ab000-7fe7e22ad000 rw-p 00000000 00:00 0 
    7fe7e22ad000-7fe7e22ae000 r--p 00022000 08:02 527300                     /lib/x86_64-linux-gnu/ld-2.17.so
    7fe7e22ae000-7fe7e22b0000 rw-p 00023000 08:02 527300                     /lib/x86_64-linux-gnu/ld-2.17.so
    7fffce6b6000-7fffce6d7000 rw-p 00000000 00:00 0                          [stack]
    7fffce722000-7fffce724000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    $ #don't forget alsr, normally only /bin/cat will remain same
    $ #so let's pick 0x00400000. Now we run our program. 
    $ #First argument is pid, "self" is a legal option too, the second is virtual address
    $ ./pagemap self 0x00400000
    Reading /proc/self/pagemap at 0x2000
    Result: 0a60000000008c445

    We got  0x0a60000000008c445 as a result. There are some bits showing that the page is valid, along with the size of the page. You can reed more in Linux documentation: https://www.kernel.org/doc/Documentation/vm/pagemap.txt. Basically, the physical page number is 0x8c445.

    Note, that in different kernel versions bits 56-60 have different meaning. In most current versions, they are forced to zero, however in kernel version 3.11.0 they represent page size.

     

  • 相关阅读:
    2017-2018-2 20179202《网络攻防技术》第一周作业
    《构建之法(第三版)》第四章
    《深入理解计算机系统(第三版)》第四章
    《文献管理与信息分析》第三章
    我们不一样
    linux centOS 7安装mysql 5.7.24
    centOS 8 使用dnf安装Docker
    centOS7服务器/dev/vda1空间100%,没有可用空间
    深刻理解 Docker 镜像大小
    使用linux docker环境打包Springboot jar包为docker镜像
  • 原文地址:https://www.cnblogs.com/lanrenxinxin/p/6216925.html
Copyright © 2011-2022 走看看