dump_pagetables.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Debug helper to dump the current kernel pagetables of the system
  3. * so that we can see what the various memory ranges are set to.
  4. *
  5. * (C) Copyright 2008 Intel Corporation
  6. *
  7. * Author: Arjan van de Ven <arjan@linux.intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #include <linux/debugfs.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/seq_file.h>
  18. #include <asm/pgtable.h>
  19. /*
  20. * The dumper groups pagetable entries of the same type into one, and for
  21. * that it needs to keep some state when walking, and flush this state
  22. * when a "break" in the continuity is found.
  23. */
  24. struct pg_state {
  25. int level;
  26. pgprot_t current_prot;
  27. unsigned long start_address;
  28. unsigned long current_address;
  29. const struct addr_marker *marker;
  30. unsigned long lines;
  31. bool to_dmesg;
  32. };
  33. struct addr_marker {
  34. unsigned long start_address;
  35. const char *name;
  36. unsigned long max_lines;
  37. };
  38. /* indices for address_markers; keep sync'd w/ address_markers below */
  39. enum address_markers_idx {
  40. USER_SPACE_NR = 0,
  41. #ifdef CONFIG_X86_64
  42. KERNEL_SPACE_NR,
  43. LOW_KERNEL_NR,
  44. VMALLOC_START_NR,
  45. VMEMMAP_START_NR,
  46. # ifdef CONFIG_X86_ESPFIX64
  47. ESPFIX_START_NR,
  48. # endif
  49. HIGH_KERNEL_NR,
  50. MODULES_VADDR_NR,
  51. MODULES_END_NR,
  52. #else
  53. KERNEL_SPACE_NR,
  54. VMALLOC_START_NR,
  55. VMALLOC_END_NR,
  56. # ifdef CONFIG_HIGHMEM
  57. PKMAP_BASE_NR,
  58. # endif
  59. FIXADDR_START_NR,
  60. #endif
  61. };
  62. /* Address space markers hints */
  63. static struct addr_marker address_markers[] = {
  64. { 0, "User Space" },
  65. #ifdef CONFIG_X86_64
  66. { 0x8000000000000000UL, "Kernel Space" },
  67. { PAGE_OFFSET, "Low Kernel Mapping" },
  68. { VMALLOC_START, "vmalloc() Area" },
  69. { VMEMMAP_START, "Vmemmap" },
  70. # ifdef CONFIG_X86_ESPFIX64
  71. { ESPFIX_BASE_ADDR, "ESPfix Area", 16 },
  72. # endif
  73. { __START_KERNEL_map, "High Kernel Mapping" },
  74. { MODULES_VADDR, "Modules" },
  75. { MODULES_END, "End Modules" },
  76. #else
  77. { PAGE_OFFSET, "Kernel Mapping" },
  78. { 0/* VMALLOC_START */, "vmalloc() Area" },
  79. { 0/*VMALLOC_END*/, "vmalloc() End" },
  80. # ifdef CONFIG_HIGHMEM
  81. { 0/*PKMAP_BASE*/, "Persisent kmap() Area" },
  82. # endif
  83. { 0/*FIXADDR_START*/, "Fixmap Area" },
  84. #endif
  85. { -1, NULL } /* End of list */
  86. };
  87. /* Multipliers for offsets within the PTEs */
  88. #define PTE_LEVEL_MULT (PAGE_SIZE)
  89. #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
  90. #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
  91. #define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
  92. #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
  93. ({ \
  94. if (to_dmesg) \
  95. printk(KERN_INFO fmt, ##args); \
  96. else \
  97. if (m) \
  98. seq_printf(m, fmt, ##args); \
  99. })
  100. #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
  101. ({ \
  102. if (to_dmesg) \
  103. printk(KERN_CONT fmt, ##args); \
  104. else \
  105. if (m) \
  106. seq_printf(m, fmt, ##args); \
  107. })
  108. /*
  109. * Print a readable form of a pgprot_t to the seq_file
  110. */
  111. static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
  112. {
  113. pgprotval_t pr = pgprot_val(prot);
  114. static const char * const level_name[] =
  115. { "cr3", "pgd", "pud", "pmd", "pte" };
  116. if (!pgprot_val(prot)) {
  117. /* Not present */
  118. pt_dump_cont_printf(m, dmsg, " ");
  119. } else {
  120. if (pr & _PAGE_USER)
  121. pt_dump_cont_printf(m, dmsg, "USR ");
  122. else
  123. pt_dump_cont_printf(m, dmsg, " ");
  124. if (pr & _PAGE_RW)
  125. pt_dump_cont_printf(m, dmsg, "RW ");
  126. else
  127. pt_dump_cont_printf(m, dmsg, "ro ");
  128. if (pr & _PAGE_PWT)
  129. pt_dump_cont_printf(m, dmsg, "PWT ");
  130. else
  131. pt_dump_cont_printf(m, dmsg, " ");
  132. if (pr & _PAGE_PCD)
  133. pt_dump_cont_printf(m, dmsg, "PCD ");
  134. else
  135. pt_dump_cont_printf(m, dmsg, " ");
  136. /* Bit 9 has a different meaning on level 3 vs 4 */
  137. if (level <= 3) {
  138. if (pr & _PAGE_PSE)
  139. pt_dump_cont_printf(m, dmsg, "PSE ");
  140. else
  141. pt_dump_cont_printf(m, dmsg, " ");
  142. } else {
  143. if (pr & _PAGE_PAT)
  144. pt_dump_cont_printf(m, dmsg, "pat ");
  145. else
  146. pt_dump_cont_printf(m, dmsg, " ");
  147. }
  148. if (pr & _PAGE_GLOBAL)
  149. pt_dump_cont_printf(m, dmsg, "GLB ");
  150. else
  151. pt_dump_cont_printf(m, dmsg, " ");
  152. if (pr & _PAGE_NX)
  153. pt_dump_cont_printf(m, dmsg, "NX ");
  154. else
  155. pt_dump_cont_printf(m, dmsg, "x ");
  156. }
  157. pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
  158. }
  159. /*
  160. * On 64 bits, sign-extend the 48 bit address to 64 bit
  161. */
  162. static unsigned long normalize_addr(unsigned long u)
  163. {
  164. #ifdef CONFIG_X86_64
  165. return (signed long)(u << 16) >> 16;
  166. #else
  167. return u;
  168. #endif
  169. }
  170. /*
  171. * This function gets called on a break in a continuous series
  172. * of PTE entries; the next one is different so we need to
  173. * print what we collected so far.
  174. */
  175. static void note_page(struct seq_file *m, struct pg_state *st,
  176. pgprot_t new_prot, int level)
  177. {
  178. pgprotval_t prot, cur;
  179. static const char units[] = "BKMGTPE";
  180. /*
  181. * If we have a "break" in the series, we need to flush the state that
  182. * we have now. "break" is either changing perms, levels or
  183. * address space marker.
  184. */
  185. prot = pgprot_val(new_prot) & PTE_FLAGS_MASK;
  186. cur = pgprot_val(st->current_prot) & PTE_FLAGS_MASK;
  187. if (!st->level) {
  188. /* First entry */
  189. st->current_prot = new_prot;
  190. st->level = level;
  191. st->marker = address_markers;
  192. st->lines = 0;
  193. pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
  194. st->marker->name);
  195. } else if (prot != cur || level != st->level ||
  196. st->current_address >= st->marker[1].start_address) {
  197. const char *unit = units;
  198. unsigned long delta;
  199. int width = sizeof(unsigned long) * 2;
  200. /*
  201. * Now print the actual finished series
  202. */
  203. if (!st->marker->max_lines ||
  204. st->lines < st->marker->max_lines) {
  205. pt_dump_seq_printf(m, st->to_dmesg,
  206. "0x%0*lx-0x%0*lx ",
  207. width, st->start_address,
  208. width, st->current_address);
  209. delta = st->current_address - st->start_address;
  210. while (!(delta & 1023) && unit[1]) {
  211. delta >>= 10;
  212. unit++;
  213. }
  214. pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
  215. delta, *unit);
  216. printk_prot(m, st->current_prot, st->level,
  217. st->to_dmesg);
  218. }
  219. st->lines++;
  220. /*
  221. * We print markers for special areas of address space,
  222. * such as the start of vmalloc space etc.
  223. * This helps in the interpretation.
  224. */
  225. if (st->current_address >= st->marker[1].start_address) {
  226. if (st->marker->max_lines &&
  227. st->lines > st->marker->max_lines) {
  228. unsigned long nskip =
  229. st->lines - st->marker->max_lines;
  230. pt_dump_seq_printf(m, st->to_dmesg,
  231. "... %lu entr%s skipped ... \n",
  232. nskip,
  233. nskip == 1 ? "y" : "ies");
  234. }
  235. st->marker++;
  236. st->lines = 0;
  237. pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
  238. st->marker->name);
  239. }
  240. st->start_address = st->current_address;
  241. st->current_prot = new_prot;
  242. st->level = level;
  243. }
  244. }
  245. static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
  246. unsigned long P)
  247. {
  248. int i;
  249. pte_t *start;
  250. start = (pte_t *) pmd_page_vaddr(addr);
  251. for (i = 0; i < PTRS_PER_PTE; i++) {
  252. pgprot_t prot = pte_pgprot(*start);
  253. st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
  254. note_page(m, st, prot, 4);
  255. start++;
  256. }
  257. }
  258. #if PTRS_PER_PMD > 1
  259. static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
  260. unsigned long P)
  261. {
  262. int i;
  263. pmd_t *start;
  264. start = (pmd_t *) pud_page_vaddr(addr);
  265. for (i = 0; i < PTRS_PER_PMD; i++) {
  266. st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
  267. if (!pmd_none(*start)) {
  268. pgprotval_t prot = pmd_val(*start) & PTE_FLAGS_MASK;
  269. if (pmd_large(*start) || !pmd_present(*start))
  270. note_page(m, st, __pgprot(prot), 3);
  271. else
  272. walk_pte_level(m, st, *start,
  273. P + i * PMD_LEVEL_MULT);
  274. } else
  275. note_page(m, st, __pgprot(0), 3);
  276. start++;
  277. }
  278. }
  279. #else
  280. #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
  281. #define pud_large(a) pmd_large(__pmd(pud_val(a)))
  282. #define pud_none(a) pmd_none(__pmd(pud_val(a)))
  283. #endif
  284. #if PTRS_PER_PUD > 1
  285. static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
  286. unsigned long P)
  287. {
  288. int i;
  289. pud_t *start;
  290. start = (pud_t *) pgd_page_vaddr(addr);
  291. for (i = 0; i < PTRS_PER_PUD; i++) {
  292. st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
  293. if (!pud_none(*start)) {
  294. pgprotval_t prot = pud_val(*start) & PTE_FLAGS_MASK;
  295. if (pud_large(*start) || !pud_present(*start))
  296. note_page(m, st, __pgprot(prot), 2);
  297. else
  298. walk_pmd_level(m, st, *start,
  299. P + i * PUD_LEVEL_MULT);
  300. } else
  301. note_page(m, st, __pgprot(0), 2);
  302. start++;
  303. }
  304. }
  305. #else
  306. #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(pgd_val(a)),p)
  307. #define pgd_large(a) pud_large(__pud(pgd_val(a)))
  308. #define pgd_none(a) pud_none(__pud(pgd_val(a)))
  309. #endif
  310. void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
  311. {
  312. #ifdef CONFIG_X86_64
  313. pgd_t *start = (pgd_t *) &init_level4_pgt;
  314. #else
  315. pgd_t *start = swapper_pg_dir;
  316. #endif
  317. int i;
  318. struct pg_state st = {};
  319. if (pgd) {
  320. start = pgd;
  321. st.to_dmesg = true;
  322. }
  323. for (i = 0; i < PTRS_PER_PGD; i++) {
  324. st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
  325. if (!pgd_none(*start)) {
  326. pgprotval_t prot = pgd_val(*start) & PTE_FLAGS_MASK;
  327. if (pgd_large(*start) || !pgd_present(*start))
  328. note_page(m, &st, __pgprot(prot), 1);
  329. else
  330. walk_pud_level(m, &st, *start,
  331. i * PGD_LEVEL_MULT);
  332. } else
  333. note_page(m, &st, __pgprot(0), 1);
  334. start++;
  335. }
  336. /* Flush out the last page */
  337. st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
  338. note_page(m, &st, __pgprot(0), 0);
  339. }
  340. static int ptdump_show(struct seq_file *m, void *v)
  341. {
  342. ptdump_walk_pgd_level(m, NULL);
  343. return 0;
  344. }
  345. static int ptdump_open(struct inode *inode, struct file *filp)
  346. {
  347. return single_open(filp, ptdump_show, NULL);
  348. }
  349. static const struct file_operations ptdump_fops = {
  350. .open = ptdump_open,
  351. .read = seq_read,
  352. .llseek = seq_lseek,
  353. .release = single_release,
  354. };
  355. static int pt_dump_init(void)
  356. {
  357. struct dentry *pe;
  358. #ifdef CONFIG_X86_32
  359. /* Not a compile-time constant on x86-32 */
  360. address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
  361. address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
  362. # ifdef CONFIG_HIGHMEM
  363. address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
  364. # endif
  365. address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
  366. #endif
  367. pe = debugfs_create_file("kernel_page_tables", 0600, NULL, NULL,
  368. &ptdump_fops);
  369. if (!pe)
  370. return -ENOMEM;
  371. return 0;
  372. }
  373. __initcall(pt_dump_init);
  374. MODULE_LICENSE("GPL");
  375. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
  376. MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");