5

I need to get the physical address of a huge-page (2 MB) from user-space. I managed to get the physical address of a normal 4 KB page from pagemap as shown in https://shanetully.com/2014/12/translating-virtual-addresses-to-physcial-addresses-in-user-space/#codeprocpidpagemapcode however I don't understand how I should use pagemap to get the physical address of a huge-page. How is a huge-page page frame number represented in pagemap? Any references and especially any piece of code would be highly appreciated.

alex10791
  • 434
  • 4
  • 11
  • 1
    Did you read the [documentation for pagemap](https://www.kernel.org/doc/Documentation/vm/pagemap.txt)? It's even referenced from the link you have provided. Specifically the section describing `COMPOUND_HEAD` and `COMPOUND_TAIL`. – kaylum May 31 '16 at 22:42
  • I have been looking in the pagemap documentation but under the HUGE section which didn't say much. So according to COMPOUND_HEAD and COMPOUND_TAIL the beginning of my 2 MB buffer should be the head hence any place in the first 4 KB of my buffer should map to the PFN of the entire huge-page? How should I then interpret the value? Should I expect 9 zeros at the end of the PFN? And consider bit 10 and above as the physical memory bits extending my offset? – alex10791 May 31 '16 at 23:36

1 Answers1

1

It should be the same. The primary difference between normal pages and huge pages are a few layers of the page tables. The page table walk ends early for huge pages (at least on x86).

Huge-pages generally end in more zeros than typical pages, as they need to be aligned to their size (in the 2MB case, the low 21 bits should all be zero).

If you are getting -EINVAL from your reads, check out this quote from the pagemap documentation(the example reads the wrong number of bytes):

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

ruthafjord
  • 2,037
  • 1
  • 11
  • 14
  • But it might also not be the same. Let me know which error you are getting and I'll see if I can understand why. – ruthafjord Jun 01 '16 at 04:28
  • Thanks a lot, it seems to work at the moment. I noticed that I got addresses with a lot of trailing zeros which made sense but I didn't know for sure, it was just an observation I didn't want to blindly rely on. Thanks again – alex10791 Jun 02 '16 at 18:26