You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicMemoryOperation.h"
  2. const int __endian_bit = 1;
  3. #define is_bigendian() ((*(char *) &__endian_bit) == 0)
  4. #ifdef S32_BIT_OPERATIONS
  5. uint32_t getPhysicalAddr(uint32_t virt_addr)
  6. {
  7. #else
  8. uint64_t getPhysicalAddr(uint64_t virt_addr)
  9. {
  10. #endif
  11. uint64_t read_val, file_offset;
  12. FILE * f;
  13. char path_buf[0x100] = {};
  14. char * end;
  15. int i, c, pid, status;
  16. printf("Big endian? %d\n", is_bigendian());
  17. sprintf(path_buf, "/proc/%d/pagemap", getpid());
  18. f = fopen(path_buf, "rb");
  19. if (! f)
  20. {
  21. printf("Error! Cannot open %s\n", path_buf);
  22. return -1;
  23. }
  24. // Shifting by virt-addr-offset number of bytes
  25. // and multiplying by the size of an address (the size of an entry in pagemap file)
  26. file_offset = virt_addr / getpagesize() * PAGEMAP_ENTRY;
  27. printf("Vaddr: %p, Page_size: %d, Entry_size: %d\n", virt_addr, getpagesize(), PAGEMAP_ENTRY);
  28. printf("Reading %s at 0x%llx\n", path_buf, (unsigned long long) file_offset);
  29. status = fseek(f, file_offset, SEEK_SET);
  30. if (status)
  31. {
  32. perror("Failed to do fseek!");
  33. return 0;
  34. }
  35. errno = 0;
  36. read_val = 0;
  37. unsigned char c_buf[PAGEMAP_ENTRY];
  38. for (i = 0; i < PAGEMAP_ENTRY; i++)
  39. {
  40. c = getc(f);
  41. if (c == EOF)
  42. {
  43. fclose(f);
  44. printf("\nReached end of the file\n");
  45. return 0;
  46. }
  47. if (is_bigendian())
  48. {
  49. c_buf[i] = c;
  50. }
  51. else
  52. {
  53. c_buf[PAGEMAP_ENTRY - i - 1] = c;
  54. }
  55. printf("[%d]0x%x ", i, c);
  56. };
  57. fclose(f);
  58. for (i = 0; i < PAGEMAP_ENTRY; i++)
  59. {
  60. printf("%d ", c_buf[i]);
  61. read_val = (read_val << 8) + c_buf[i];
  62. }
  63. printf("\n");
  64. printf("Result: 0x%llx\n", (unsigned long long) read_val);
  65. if (GET_BIT(read_val, 62))
  66. {
  67. printf("Page swapped\n");
  68. }
  69. if (GET_BIT(read_val, 61))
  70. {
  71. printf("Page File\n");
  72. }
  73. // if(GET_BIT(read_val, 63))
  74. if (GET_BIT(read_val, 63))
  75. {
  76. printf("PFN: 0x%llx\n", (unsigned long long) GET_PFN(read_val));
  77. return GET_PFN(read_val);
  78. }
  79. else
  80. {
  81. printf("Page not present\n");
  82. return 0;
  83. }
  84. return 0;
  85. }
  86. /*
  87. int main(){
  88. unsigned int *Buffer = NULL;
  89. unsigned int PH_Addr = 0;
  90. char mapFile[128];
  91. unsigned int PageAddress = 0;
  92. unsigned int PageOffset = 0;
  93. int RetVal = 0;
  94. mlockall( MCL_CURRENT | MCL_FUTURE );
  95. printf("pid_t: %zu\n", sizeof(pid_t));
  96. printf("uid_t: %zu\n", sizeof(uid_t));
  97. printf("gid_t: %zu\n", sizeof(gid_t));
  98. sprintf( mapFile, "/proc/%d/pagemap", getpid() );
  99. printf("Mapping File: %s\n", mapFile );
  100. Buffer = (unsigned int *) malloc( 192*1024*1024 ); // 192Mb buffer;
  101. if( Buffer!= NULL ){
  102. printf("Buffer Allocated\n");
  103. printf("Buffer Virtual Address: %p \n", Buffer );
  104. PageAddress = ((unsigned int) Buffer ) >> 12;
  105. PageOffset = ((unsigned int) Buffer ) % 0x1000;
  106. printf(" Page: 0x%08x OffSet: 0x%08x\n", PageAddress, PageOffset );
  107. read_pagemap( mapFile, (int) Buffer );
  108. free( Buffer );
  109. printf("Buffer Released\n");
  110. } else {
  111. printf("Cannot Allocate Buffer\n");
  112. };
  113. munlockall();
  114. return 0;
  115. };
  116. */