dump_pagetables.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <linux/seq_file.h>
  2. #include <linux/debugfs.h>
  3. #include <linux/module.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_printf(m, "%s", pr & _PAGE_PROTECT ? "RO " : "RW ");
  46. seq_putc(m, '\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) & (_PAGE_PROTECT | _PAGE_INVALID);
  109. note_page(m, st, prot, 4);
  110. addr += PAGE_SIZE;
  111. }
  112. }
  113. static void walk_pmd_level(struct seq_file *m, struct pg_state *st,
  114. pud_t *pud, unsigned long addr)
  115. {
  116. unsigned int prot;
  117. pmd_t *pmd;
  118. int i;
  119. for (i = 0; i < PTRS_PER_PMD && addr < max_addr; i++) {
  120. st->current_address = addr;
  121. pmd = pmd_offset(pud, addr);
  122. if (!pmd_none(*pmd)) {
  123. if (pmd_large(*pmd)) {
  124. prot = pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT;
  125. note_page(m, st, prot, 3);
  126. } else
  127. walk_pte_level(m, st, pmd, addr);
  128. } else
  129. note_page(m, st, _PAGE_INVALID, 3);
  130. addr += PMD_SIZE;
  131. }
  132. }
  133. static void walk_pud_level(struct seq_file *m, struct pg_state *st,
  134. pgd_t *pgd, unsigned long addr)
  135. {
  136. unsigned int prot;
  137. pud_t *pud;
  138. int i;
  139. for (i = 0; i < PTRS_PER_PUD && addr < max_addr; i++) {
  140. st->current_address = addr;
  141. pud = pud_offset(pgd, addr);
  142. if (!pud_none(*pud))
  143. if (pud_large(*pud)) {
  144. prot = pud_val(*pud) & _REGION_ENTRY_PROTECT;
  145. note_page(m, st, prot, 2);
  146. } else
  147. walk_pmd_level(m, st, pud, addr);
  148. else
  149. note_page(m, st, _PAGE_INVALID, 2);
  150. addr += PUD_SIZE;
  151. }
  152. }
  153. static void walk_pgd_level(struct seq_file *m)
  154. {
  155. unsigned long addr = 0;
  156. struct pg_state st;
  157. pgd_t *pgd;
  158. int i;
  159. memset(&st, 0, sizeof(st));
  160. for (i = 0; i < PTRS_PER_PGD && addr < max_addr; i++) {
  161. st.current_address = addr;
  162. pgd = pgd_offset_k(addr);
  163. if (!pgd_none(*pgd))
  164. walk_pud_level(m, &st, pgd, addr);
  165. else
  166. note_page(m, &st, _PAGE_INVALID, 1);
  167. addr += PGDIR_SIZE;
  168. }
  169. /* Flush out the last page */
  170. st.current_address = max_addr;
  171. note_page(m, &st, 0, 0);
  172. }
  173. static int ptdump_show(struct seq_file *m, void *v)
  174. {
  175. walk_pgd_level(m);
  176. return 0;
  177. }
  178. static int ptdump_open(struct inode *inode, struct file *filp)
  179. {
  180. return single_open(filp, ptdump_show, NULL);
  181. }
  182. static const struct file_operations ptdump_fops = {
  183. .open = ptdump_open,
  184. .read = seq_read,
  185. .llseek = seq_lseek,
  186. .release = single_release,
  187. };
  188. static int pt_dump_init(void)
  189. {
  190. /*
  191. * Figure out the maximum virtual address being accessible with the
  192. * kernel ASCE. We need this to keep the page table walker functions
  193. * from accessing non-existent entries.
  194. */
  195. max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
  196. max_addr = 1UL << (max_addr * 11 + 31);
  197. address_markers[MODULES_NR].start_address = MODULES_VADDR;
  198. address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
  199. address_markers[VMALLOC_NR].start_address = VMALLOC_START;
  200. debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
  201. return 0;
  202. }
  203. device_initcall(pt_dump_init);