How to obtain the physical address of virtual address under Linux

Source: Internet
Author: User

First extract the following URL to the description of Pagemap.

Https://www.kernel.org/doc/Documentation/vm/pagemap.txt


*/proc/pid/pagemap. This file lets a userspace process find out which
Physical frame Each virtual page was mapped to. It contains one 64-bit
Value for each virtual page, containing the following data
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
* Bit Soft-dirty Pte is a (see Documentation/vm/soft-dirty.txt)
* Bits 56-60 Zero
* Bit-page is file-page or Shared-anon
* Bit Page swapped
* Bit to page present


If the page is not present and in Swaps, 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 is mapped (or in swap) and comparing mapped
Pages between processes.


Next, we give the code to get the physical address corresponding to the virtual address according to the above description


#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define Page_map_file "/proc/self/pagemap"
#define Pfn_mask ((((uint64_t) 1) <<55)-1)
#define Pfn_present_flag (((uint64_t) 1) <<63)


int mem_addr_vir2phy (unsigned long vir, unsigned long *phy)
{
int FD;
int page_size=getpagesize ();
unsigned long vir_page_idx = vir/page_size;
unsigned long pfn_item_offset = vir_page_idx*sizeof (uint64_t);
uint64_t Pfn_item;

FD = open (Page_map_file, o_rdonly);
if (fd<0)
{
printf ("Open%s failed", Page_map_file);
return-1;
}


if ((off_t)-1 = = Lseek (FD, Pfn_item_offset, Seek_set))
{
printf ("Lseek%s failed", Page_map_file);
return-1;
}


if (sizeof (uint64_t)! = Read (fd, &pfn_item, sizeof (uint64_t)))
{
printf ("Read%s failed", Page_map_file);
return-1;
}


if (0== (Pfn_item & Pfn_present_flag))
{
printf ("page is not present");
return-1;
}


*phy = (Pfn_item & pfn_mask) *page_size + vir% Page_size;
return 0;


}

How to obtain the physical address of virtual address under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.