dump_pagetables.c 5.5 KB

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