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