dump_pagetables.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <linux/seq_file.h>
  2. #include <linux/debugfs.h>
  3. #include <linux/sched.h>
  4. #include <linux/mm.h>
  5. #include <asm/sections.h>
  6. #include <asm/pgtable.h>
  7. static unsigned long max_addr;
  8. struct addr_marker {
  9. unsigned long start_address;
  10. const char *name;
  11. };
  12. enum address_markers_idx {
  13. IDENTITY_NR = 0,
  14. KERNEL_START_NR,
  15. KERNEL_END_NR,
  16. VMEMMAP_NR,
  17. VMALLOC_NR,
  18. MODULES_NR,
  19. };
  20. static struct addr_marker address_markers[] = {
  21. [IDENTITY_NR] = {0, "Identity Mapping"},
  22. [KERNEL_START_NR] = {(unsigned long)&_stext, "Kernel Image Start"},
  23. [KERNEL_END_NR] = {(unsigned long)&_end, "Kernel Image End"},
  24. [VMEMMAP_NR] = {0, "vmemmap Area"},
  25. [VMALLOC_NR] = {0, "vmalloc Area"},
  26. [MODULES_NR] = {0, "Modules Area"},
  27. { -1, NULL }
  28. };
  29. struct pg_state {
  30. int level;
  31. unsigned int current_prot;
  32. unsigned long start_address;
  33. unsigned long current_address;
  34. const struct addr_marker *marker;
  35. };
  36. static void print_prot(struct seq_file *m, unsigned int pr, int level)
  37. {
  38. static const char * const level_name[] =
  39. { "ASCE", "PGD", "PUD", "PMD", "PTE" };
  40. seq_printf(m, "%s ", level_name[level]);
  41. if (pr & _PAGE_INVALID) {
  42. seq_printf(m, "I\n");
  43. return;
  44. }
  45. seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
  46. seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
  47. }
  48. static void note_page(struct seq_file *m, struct pg_state *st,
  49. unsigned int new_prot, int level)
  50. {
  51. static const char units[] = "KMGTPE";
  52. int width = sizeof(unsigned long) * 2;
  53. const char *unit = units;
  54. unsigned int prot, cur;
  55. unsigned long delta;
  56. /*
  57. * If we have a "break" in the series, we need to flush the state
  58. * that we have now. "break" is either changing perms, levels or
  59. * address space marker.
  60. */
  61. prot = new_prot;
  62. cur = st->current_prot;
  63. if (!st->level) {
  64. /* First entry */
  65. st->current_prot = new_prot;
  66. st->level = level;
  67. st->marker = address_markers;
  68. seq_printf(m, "---[ %s ]---\n", st->marker->name);
  69. } else if (prot != cur || level != st->level ||
  70. st->current_address >= st->marker[1].start_address) {
  71. /* Print the actual finished series */
  72. seq_printf(m, "0x%0*lx-0x%0*lx",
  73. width, st->start_address,
  74. width, st->current_address);
  75. delta = (st->current_address - st->start_address) >> 10;
  76. while (!(delta & 0x3ff) && unit[1]) {
  77. delta >>= 10;
  78. unit++;
  79. }
  80. seq_printf(m, "%9lu%c ", delta, *unit);
  81. print_prot(m, st->current_prot, st->level);
  82. if (st->current_address >= st->marker[1].start_address) {
  83. st->marker++;
  84. seq_printf(m, "---[ %s ]---\n", st->marker->name);
  85. }
  86. st->start_address = st->current_address;
  87. st->current_prot = new_prot;
  88. st->level = level;
  89. }
  90. }
  91. /*
  92. * The actual page table walker functions. In order to keep the
  93. * implementation of print_prot() short, we only check and pass
  94. * _PAGE_INVALID and _PAGE_PROTECT flags to note_page() if a region,
  95. * segment or page table entry is invalid or read-only.
  96. * After all it's just a hint that the current level being walked
  97. * contains an invalid or read-only entry.
  98. */
  99. static void walk_pte_level(struct seq_file *m, struct pg_state *st,
  100. pmd_t *pmd, unsigned long addr)
  101. {
  102. unsigned int prot;
  103. pte_t *pte;
  104. int i;
  105. for (i = 0; i < PTRS_PER_PTE && addr < max_addr; i++) {
  106. st->current_address = addr;
  107. pte = pte_offset_kernel(pmd, addr);
  108. prot = pte_val(*pte) &
  109. (_PAGE_PROTECT | _PAGE_INVALID | _PAGE_NOEXEC);
  110. note_page(m, st, prot, 4);
  111. addr += PAGE_SIZE;
  112. }
  113. }
  114. static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
  115. pud_t *pud, unsigned long addr)
  116. {
  117. unsigned int prot;
  118. pmd_t *pmd;
  119. int i;
  120. for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
  121. st->current_address = addr;
  122. pmd = pmd_offset(pud, addr);
  123. if (!pmd_none(*pmd)) {
  124. if (pmd_large(*pmd)) {
  125. prot = pmd_val(*pmd) &
  126. (_SEGMENT_ENTRY_PROTECT |
  127. _SEGMENT_ENTRY_NOEXEC);
  128. note_page(m, st, prot, 3);
  129. } else
  130. walk_pte_level(m, st, pmd, addr);
  131. } else
  132. note_page(m, st, _PAGE_INVALID, 3);
  133. addr += PMD_SIZE;
  134. }
  135. }
  136. static void walk_pud_level(struct seq_file *m, struct pg_state *st,
  137. pgd_t *pgd, unsigned long addr)
  138. {
  139. unsigned int prot;
  140. pud_t *pud;
  141. int i;
  142. for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
  143. st->current_address = addr;
  144. pud = pud_offset(pgd, addr);
  145. if (!pud_none(*pud))
  146. if (pud_large(*pud)) {
  147. prot = pud_val(*pud) &
  148. (_REGION_ENTRY_PROTECT |
  149. _REGION_ENTRY_NOEXEC);
  150. note_page(m, st, prot, 2);
  151. } else
  152. walk_pmd_level(m, st, pud, addr);
  153. else
  154. note_page(m, st, _PAGE_INVALID, 2);
  155. addr += PUD_SIZE;
  156. }
  157. }
  158. static void walk_pgd_level(struct seq_file *m)
  159. {
  160. unsigned long addr = 0;
  161. struct pg_state st;
  162. pgd_t *pgd;
  163. int i;
  164. memset(&st, 0, sizeof(st));
  165. for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
  166. st.current_address = addr;
  167. pgd = pgd_offset_k(addr);
  168. if (!pgd_none(*pgd))
  169. walk_pud_level(m, &st, pgd, addr);
  170. else
  171. note_page(m, &st, _PAGE_INVALID, 1);
  172. addr += PGDIR_SIZE;
  173. cond_resched();
  174. }
  175. /* Flush out the last page */
  176. st.current_address = max_addr;
  177. note_page(m, &st, 0, 0);
  178. }
  179. static int ptdump_show(struct seq_file *m, void *v)
  180. {
  181. walk_pgd_level(m);
  182. return 0;
  183. }
  184. static int ptdump_open(struct inode *inode, struct file *filp)
  185. {
  186. return single_open(filp, ptdump_show, NULL);
  187. }
  188. static const struct file_operations ptdump_fops = {
  189. .open = ptdump_open,
  190. .read = seq_read,
  191. .llseek = seq_lseek,
  192. .release = single_release,
  193. };
  194. static int pt_dump_init(void)
  195. {
  196. /*
  197. * Figure out the maximum virtual address being accessible with the
  198. * kernel ASCE. We need this to keep the page table walker functions
  199. * from accessing non-existent entries.
  200. */
  201. max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
  202. max_addr = 1UL << (max_addr * 11 + 31);
  203. address_markers[MODULES_NR].start_address = MODULES_VADDR;
  204. address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
  205. address_markers[VMALLOC_NR].start_address = VMALLOC_START;
  206. debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
  207. return 0;
  208. }
  209. device_initcall(pt_dump_init);