virt_to_phys_user.c 855 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. Convert a virtual address to physical for a given process PID using /proc/PID/pagemap.
  3. https://stackoverflow.com/questions/5748492/is-there-any-api-for-determining-the-physical-address-from-virtual-address-in-li/45128487#45128487
  4. Test this out with usermem.c.
  5. */
  6. #define _XOPEN_SOURCE 700
  7. #include <stdio.h> /* printf */
  8. #include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE, strtoull */
  9. #include "common.h" /* virt_to_phys_user */
  10. int main(int argc, char **argv)
  11. {
  12. pid_t pid;
  13. uintptr_t vaddr, paddr = 0;
  14. if (argc < 3) {
  15. printf("Usage: %s pid vaddr\n", argv[0]);
  16. return EXIT_FAILURE;
  17. }
  18. pid = strtoull(argv[1], NULL, 0);
  19. vaddr = strtoull(argv[2], NULL, 0);
  20. if (virt_to_phys_user(&paddr, pid, vaddr)) {
  21. fprintf(stderr, "error: virt_to_phys_user\n");
  22. return EXIT_FAILURE;
  23. };
  24. printf("0x%jx\n", (uintmax_t)paddr);
  25. return EXIT_SUCCESS;
  26. }