dump_pagetables.c 6.0 KB

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