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.

105 lines
2.9 KiB

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