dump_pagetables.c 6.1 KB

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