|
| 1 | +#include <stdlib.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <errno.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/mman.h> |
| 7 | +#define __NR_get_pagetable_layout 233 |
| 8 | +#define __NR_expose_page_table 378 |
| 9 | + |
| 10 | +#define PGD_SIZE ((1 << 11) * sizeof(unsigned long)) |
| 11 | +#define PTE_SIZE ((1 << 20) * sizeof(unsigned long)) |
| 12 | + |
| 13 | +// The structure "pagetable_layout_info" stores the information of pagetable layout |
| 14 | +struct pagetable_layout_info |
| 15 | +{ |
| 16 | + uint32_t pgdir_shift; |
| 17 | + uint32_t page_shift; |
| 18 | +}; |
| 19 | + |
| 20 | +// for a hex byte c, return hex byte (c-8) |
| 21 | +char lower(char c) |
| 22 | +{ |
| 23 | + if (c >= 'a') |
| 24 | + return c - 'a' + '2'; |
| 25 | + if (c >= 'A') |
| 26 | + return c - 'A' + '2'; |
| 27 | + return c - 8; |
| 28 | +} |
| 29 | + |
| 30 | +// convert a hex string up to 8 bytes to unsigned long |
| 31 | +unsigned long strtolu(char *str) |
| 32 | +{ |
| 33 | + unsigned long res; |
| 34 | + |
| 35 | + // check if the value of the string exceeds (2^31)-1 |
| 36 | + int flag = (strlen(str) == 8) && (str[0] >= '8'); |
| 37 | + if (flag) |
| 38 | + str[0] = lower(str[0]); |
| 39 | + |
| 40 | + // convert hex to unsigned long |
| 41 | + res = strtol(str, NULL, 16); |
| 42 | + res |= (flag << 31); |
| 43 | + |
| 44 | + return res; |
| 45 | +} |
| 46 | + |
| 47 | +void display_pagetable_layout(struct pagetable_layout_info* layout) |
| 48 | +{ |
| 49 | + printf("============================================================\n"); |
| 50 | + printf("Android pagetable layout:\n"); |
| 51 | + printf(" pgdir_shift = %u\n", layout->pgdir_shift); |
| 52 | + printf(" page_shift = %u\n", layout->page_shift); |
| 53 | + printf("============================================================\n"); |
| 54 | +} |
| 55 | + |
| 56 | +void get_pagetable_layout() |
| 57 | +{ |
| 58 | + struct pagetable_layout_info pgtbl_info; |
| 59 | + |
| 60 | + // invoke system call get_pagetable_layout() |
| 61 | + if (syscall(__NR_get_pagetable_layout, &pgtbl_info, sizeof(struct pagetable_layout_info))) |
| 62 | + { |
| 63 | + printf("Failed to get pagetable layout.\n\n"); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + display_pagetable_layout(&pgtbl_info); |
| 68 | +} |
| 69 | + |
| 70 | +void expose_page_table(pid_t pid, unsigned long fake_pgd, unsigned long page_table_addr, unsigned long begin_vaddr, unsigned long end_vaddr) |
| 71 | +{ |
| 72 | + unsigned long physical_addr, v_pgd, v_pte, v_offset, pageFrame; |
| 73 | + unsigned long *p; |
| 74 | + |
| 75 | + // invoke system call expose_page_table() |
| 76 | + if (syscall(__NR_expose_page_table, pid, fake_pgd, page_table_addr, begin_vaddr, end_vaddr)) |
| 77 | + { |
| 78 | + printf("Failed to expose pagetable layout.\n"); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + // segment the virtual address |
| 83 | + v_pgd = (begin_vaddr >> 21) & 0x7FF; |
| 84 | + v_pte = (begin_vaddr >> PAGE_SHIFT) & 0x1FF; |
| 85 | + v_offset = begin_vaddr & 0xFFF; |
| 86 | + |
| 87 | + // calculate physical address |
| 88 | + p = (unsigned long*)((unsigned long*)fake_pgd)[v_pgd]; |
| 89 | + pageFrame = p[v_pte] & 0xFFFFF000; |
| 90 | + if (!pageFrame) |
| 91 | + { |
| 92 | + printf("Invalid physical address!"); |
| 93 | + } |
| 94 | + physical_addr = pageFrame + v_offset; |
| 95 | + |
| 96 | + // print calculation details |
| 97 | + printf("Virtual Address Translation:\n"); |
| 98 | + printf(" virtual address = 0x%08lx\n", begin_vaddr); |
| 99 | + printf(" pgd: 0x%03lx\tpte: 0x%03lx\toffset: 0x%03lx\n", v_pgd, v_pte, v_offset); |
| 100 | + printf(" pgd_base[0x%lx] = 0x%08lx\tpte[0x%lx] = 0x%08lx\n", v_pgd, (unsigned long)p, v_pte, pageFrame); |
| 101 | + printf(" physical address = 0x%08lx\n", physical_addr); |
| 102 | +} |
| 103 | + |
| 104 | +int main(int argc, char *argv[]) |
| 105 | +{ |
| 106 | + pid_t pid; |
| 107 | + unsigned long fake_pgd, page_table_addr, begin_vaddr, end_vaddr; |
| 108 | + |
| 109 | + // invoke system call |
| 110 | + get_pagetable_layout(); |
| 111 | + |
| 112 | + // check paramaters |
| 113 | + if (argc != 3) |
| 114 | + { |
| 115 | + printf("Unmatched parameters for VATranslate!\n"); |
| 116 | + printf("The correct format should be:\n"); |
| 117 | + printf("./VATranslate #PID #VA\n"); |
| 118 | + return -EAGAIN; |
| 119 | + } |
| 120 | + pid = atoi(argv[1]); |
| 121 | + begin_vaddr =strtolu(argv[2]); |
| 122 | + end_vaddr = begin_vaddr + 1; |
| 123 | + |
| 124 | + // allocate user-space memory |
| 125 | + fake_pgd = (unsigned long)malloc(2 * PAGE_SIZE); |
| 126 | + if (!fake_pgd) |
| 127 | + return -EFAULT; |
| 128 | + page_table_addr = (unsigned long)mmap(NULL, 1 << 22, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 129 | + if (!page_table_addr) |
| 130 | + return -EFAULT; |
| 131 | + // invoke system call |
| 132 | + expose_page_table(pid, fake_pgd, page_table_addr, begin_vaddr, end_vaddr); |
| 133 | + |
| 134 | + // free user-space memory |
| 135 | + free((void*)fake_pgd); |
| 136 | + munmap((void*)page_table_addr, 1 << 22); |
| 137 | + return 0; |
| 138 | +} |
0 commit comments