Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2151548
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-08-22 17:25:46

1.  linux内核文档Documentation/vm/pagemap.txt

1.2  用户空间获取物理地址的代码
  1. cong@msi:/work/test/ctest/vaddr$ cat test.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <inttypes.h>
  5. #include <stdint.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>

  9. #define page_map_file "/proc/self/pagemap"

  10. #define PFN_MASK ((((uint64_t)1)<<55)-1)

  11. #define PFN_PRESENT_FLAG (((uint64_t)1)<<63)

  12. int mem_addr_vir2phy(unsigned long vir, unsigned long *phy)
  13. {
  14.     int fd;
  15.     int page_size=getpagesize();
  16.     unsigned long vir_page_idx = vir/page_size;
  17.     unsigned long pfn_item_offset = vir_page_idx*sizeof(uint64_t);
  18.     uint64_t pfn_item;

  19.     fd = open(page_map_file, O_RDONLY);
  20.     if (fd<0)
  21.     {
  22.         printf("open %s failed", page_map_file);
  23.         return -1;
  24.     }
  25.     if ((off_t)-1 == lseek(fd, pfn_item_offset, SEEK_SET))
  26.     {
  27.         printf("lseek %s failed", page_map_file);
  28.         return -1;
  29.     }
  30.     if (sizeof(uint64_t) != read(fd, &pfn_item, sizeof(uint64_t)))
  31.     {
  32.         printf("read %s failed", page_map_file);
  33.         return -1;
  34.     }
  35.     if (0==(pfn_item & PFN_PRESENT_FLAG))
  36.     {
  37.         printf("page is not present");
  38.         return -1;
  39.     }
  40.     *phy = (pfn_item & PFN_MASK)*page_size + vir % page_size;
  41.     return 0;
  42. }

  43. void main()
  44. {
  45.     int a=0x12345678;
  46.     unsigned long phy;
  47.     mem_addr_vir2phy((unsigned long)&a, &phy);
  48.     printf("vaddr=%p,phy=0x%lx\n", &a, phy);
  49.     while(1)
  50.     {
  51.         sleep(100);
  52.     }
  53. }
注:函数mem_addr_vir2phy代码出自 
《Linux下获取虚拟地址对应的物理地址的方法》

这儿我只是打了一个main函数进行测试

二.验证结果的正确性
2.1 使用方法
cong@msi:/work/test/ctest/vaddr/Access_Physical_Memory$ sudo mknod /dev/dram c 85 0
cong@msi:/work/test/ctest/vaddr/Access_Physical_Memory$ ./fileview /dev/dram

注:上述驱动与fileview出自
《Linux用户程序如何访问物理内存》

2.2.2 我的ubuntun 的内核版本是 3.13.0-49-generic
作了少许改动
  1. 27d26
  2. < #include <linux/mm.h>
  3. 47,49c46,47
  4. <     unsigned long pages = get_num_physpages();    
  5. <     //dram_size = (loff_t)num_physpages << PAGE_SHIFT;
  6. <     dram_size = pages << PAGE_SHIFT;
  7. ---
  8. >     
  9. >     dram_size = (loff_t)num_physpages << PAGE_SHIFT;
  10. 77c75
  11. < #if 1
  12. ---
  13. > #if 0
2.3 验证一下结果
2.3.1 这个是用户空间打印出来的物理地址是0x41F47284


2.3.2 这是物理内存0x41F47284处的数据

完全吻合,说明成功
2.4 代码下载
包括fileview和驱动
Access_Physical_Memory.rar(下载后改名为Access_Physical_Memory.tar.bz2)
这个代码出自
附录: 内核文档Documentation/vm/pagemap.txt
  1. pagemap, from the userspace perspective
  2. ---------------------------------------

  3. pagemap is a new (as of 2.6.25) set of interfaces in the kernel that allow
  4. userspace programs to examine the page tables and related information by
  5. reading files in /proc.

  6. There are three components to pagemap:

  7.  * /proc/pid/pagemap. This file lets a userspace process find out which
  8.    physical frame each virtual page is mapped to. It contains one 64-bit
  9.    value for each virtual page, containing the following data (from
  10.    fs/proc/task_mmu.c, above pagemap_read):

  11.     * Bits 0-54 page frame number (PFN) if present
  12.     * Bits 0-4 swap type if swapped
  13.     * Bits 5-54 swap offset if swapped
  14.     * Bits 55-60 page shift (page size = 1<<page shift)
  15.     * Bit 61 reserved for future use
  16.     * Bit 62 page swapped
  17.     * Bit 63 page present

  18.    If the page is not present but in swap, then the PFN contains an
  19.    encoding of the swap file number and the page's offset into the
  20.    swap. Unmapped pages return a null PFN. This allows determining
  21.    precisely which pages are mapped (or in swap) and comparing mapped
  22.    pages between processes.

  23.    Efficient users of this interface will use /proc/pid/maps to
  24.    determine which areas of memory are actually mapped and llseek to
  25.    skip over unmapped regions.

  26.  * /proc/kpagecount. This file contains a 64-bit count of the number of
  27.    times each page is mapped, indexed by PFN.

  28.  * /proc/kpageflags. This file contains a 64-bit set of flags for each
  29.    page, indexed by PFN.

  30.    The flags are (from fs/proc/page.c, above kpageflags_read):

  31.      0. LOCKED
  32.      1. ERROR
  33.      2. REFERENCED
  34.      3. UPTODATE
  35.      4. DIRTY
  36.      5. LRU
  37.      6. ACTIVE
  38.      7. SLAB
  39.      8. WRITEBACK
  40.      9. RECLAIM
  41.     10. BUDDY
  42.     11. MMAP
  43.     12. ANON
  44.     13. SWAPCACHE
  45.     14. SWAPBACKED
  46.     15. COMPOUND_HEAD
  47.     16. COMPOUND_TAIL
  48.     16. HUGE
  49.     18. UNEVICTABLE
  50.     19. HWPOISON
  51.     20. NOPAGE
  52.     21. KSM

  53. Short descriptions to the page flags:

  54.  0. LOCKED
  55.     page is being locked for exclusive access, eg. by undergoing read/write IO

  56.  7. SLAB
  57.     page is managed by the SLAB/SLOB/SLUB/SLQB kernel memory allocator
  58.     When compound page is used, SLUB/SLQB will only set this flag on the head
  59.     page; SLOB will not flag it at all.

  60. 10. BUDDY
  61.     a free memory block managed by the buddy system allocator
  62.     The buddy system organizes free memory in blocks of various orders.
  63.     An order N block has 2^N physically contiguous pages, with the BUDDY flag
  64.     set for and _only_ for the first page.

  65. 15. COMPOUND_HEAD
  66. 16. COMPOUND_TAIL
  67.     A compound page with order N consists of 2^N physically contiguous pages.
  68.     A compound page with order 2 takes the form of "HTTT", where H donates its
  69.     head page and T donates its tail page(s). The major consumers of compound
  70.     pages are hugeTLB pages (Documentation/vm/hugetlbpage.txt), the SLUB etc.
  71.     memory allocators and various device drivers. However in this interface,
  72.     only huge/giga pages are made visible to end users.
  73. 17. HUGE
  74.     this is an integral part of a HugeTLB page

  75. 19. HWPOISON
  76.     hardware detected memory corruption on this page: don't touch the

  77. 20. NOPAGE
  78.     no page frame exists at the requested address

  79. 21. KSM
  80.     identical memory pages dynamically shared between one or more processes

  81.     [IO related page flags]
  82.  1. ERROR IO error occurred
  83.  3. UPTODATE page has up-to-date data
  84.               ie. for file backed page: (in-memory data revision >= on-disk one)
  85.  4. DIRTY page has been written to, hence contains new data
  86.               ie. for file backed page: (in-memory data revision > on-disk one)
  87.  8. WRITEBACK page is being synced to disk

  88.     [LRU related page flags]
  89.  5. LRU page is in one of the LRU lists
  90.  6. ACTIVE page is in the active LRU list
  91. 18. UNEVICTABLE page is in the unevictable (non-)LRU list
  92.                 It is somehow pinned and not a candidate for LRU page reclaims,
  93.         eg. ramfs pages, shmctl(SHM_LOCK) and mlock() memory segments
  94.  2. REFERENCED page has been referenced since last LRU list enqueue/requeue
  95.  9. RECLAIM page will be reclaimed soon after its pageout IO completed
  96. 11. MMAP a memory mapped page
  97. 12. ANON a memory mapped page that is not part of a file
  98. 13. SWAPCACHE page is mapped to swap space, ie. has an associated swap entry
  99. 14. SWAPBACKED page is backed by swap/RAM

  100. The page-types tool in this directory can be used to query the above flags.

  101. Using pagemap to do something useful:

  102. The general procedure for using pagemap to find out about a process' memory
  103. usage goes like this:

  104.  1. Read /proc/pid/maps to determine which parts of the memory space are
  105.     mapped to what.
  106.  2. Select the maps you are interested in -- all of them, or a particular
  107.     library, or the stack or the heap, etc.
  108.  3. Open /proc/pid/pagemap and seek to the pages you would like to examine.
  109.  4. Read a u64 for each page from pagemap.
  110.  5. Open /proc/kpagecount and/or /proc/kpageflags. For each PFN you just
  111.     read, seek to that entry in the file, and read the data you want.

  112. For example, to find the "unique set size" (USS), which is the amount of
  113. memory that a process is using that is not shared with any other process,
  114. you can go through every map in the process, find the PFNs, look those up
  115. in kpagecount, and tally up the number of pages that are only referenced
  116. once.

  117. Other notes:

  118. Reading from any of the files will return -EINVAL if you are not starting
  119. the read on an 8-byte boundary (e.g., if you seeked an odd number of bytes
  120. into the file), or if the size of the read is not a multiple of 8 bytes.

阅读(3668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~