dump_pagetables.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. # ifdef CONFIG_EFI
  74. { EFI_VA_END, "EFI Runtime Services" },
  75. # endif
  76. { __START_KERNEL_map, "High Kernel Mapping" },
  77. { MODULES_VADDR, "Modules" },
  78. { MODULES_END, "End Modules" },
  79. #else
  80. { PAGE_OFFSET, "Kernel Mapping" },
  81. { 0/* VMALLOC_START */, "vmalloc() Area" },
  82. { 0/*VMALLOC_END*/, "vmalloc() End" },
  83. # ifdef CONFIG_HIGHMEM
  84. { 0/*PKMAP_BASE*/, "Persisent kmap() Area" },
  85. # endif
  86. { 0/*FIXADDR_START*/, "Fixmap Area" },
  87. #endif
  88. { -1, NULL } /* End of list */
  89. };
  90. /* Multipliers for offsets within the PTEs */
  91. #define PTE_LEVEL_MULT (PAGE_SIZE)
  92. #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
  93. #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
  94. #define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
  95. #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
  96. ({ \
  97. if (to_dmesg) \
  98. printk(KERN_INFO fmt, ##args); \
  99. else \
  100. if (m) \
  101. seq_printf(m, fmt, ##args); \
  102. })
  103. #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
  104. ({ \
  105. if (to_dmesg) \
  106. printk(KERN_CONT fmt, ##args); \
  107. else \
  108. if (m) \
  109. seq_printf(m, fmt, ##args); \
  110. })
  111. /*
  112. * Print a readable form of a pgprot_t to the seq_file
  113. */
  114. static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
  115. {
  116. pgprotval_t pr = pgprot_val(prot);
  117. static const char * const level_name[] =
  118. { "cr3", "pgd", "pud", "pmd", "pte" };
  119. if (!pgprot_val(prot)) {
  120. /* Not present */
  121. pt_dump_cont_printf(m, dmsg, " ");
  122. } else {
  123. if (pr & _PAGE_USER)
  124. pt_dump_cont_printf(m, dmsg, "USR ");
  125. else
  126. pt_dump_cont_printf(m, dmsg, " ");
  127. if (pr & _PAGE_RW)
  128. pt_dump_cont_printf(m, dmsg, "RW ");
  129. else
  130. pt_dump_cont_printf(m, dmsg, "ro ");
  131. if (pr & _PAGE_PWT)
  132. pt_dump_cont_printf(m, dmsg, "PWT ");
  133. else
  134. pt_dump_cont_printf(m, dmsg, " ");
  135. if (pr & _PAGE_PCD)
  136. pt_dump_cont_printf(m, dmsg, "PCD ");
  137. else
  138. pt_dump_cont_printf(m, dmsg, " ");
  139. /* Bit 7 has a different meaning on level 3 vs 4 */
  140. if (level <= 3 && pr & _PAGE_PSE)
  141. pt_dump_cont_printf(m, dmsg, "PSE ");
  142. else
  143. pt_dump_cont_printf(m, dmsg, " ");
  144. if ((level == 4 && pr & _PAGE_PAT) ||
  145. ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE))
  146. pt_dump_cont_printf(m, dmsg, "pat ");
  147. else
  148. pt_dump_cont_printf(m, dmsg, " ");
  149. if (pr & _PAGE_GLOBAL)
  150. pt_dump_cont_printf(m, dmsg, "GLB ");
  151. else
  152. pt_dump_cont_printf(m, dmsg, " ");
  153. if (pr & _PAGE_NX)
  154. pt_dump_cont_printf(m, dmsg, "NX ");
  155. else
  156. pt_dump_cont_printf(m, dmsg, "x ");
  157. }
  158. pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
  159. }
  160. /*
  161. * On 64 bits, sign-extend the 48 bit address to 64 bit
  162. */
  163. static unsigned long normalize_addr(unsigned long u)
  164. {
  165. #ifdef CONFIG_X86_64
  166. return (signed long)(u << 16) >> 16;
  167. #else
  168. return u;
  169. #endif
  170. }
  171. /*
  172. * This function gets called on a break in a continuous series
  173. * of PTE entries; the next one is different so we need to
  174. * print what we collected so far.
  175. */
  176. static void note_page(struct seq_file *m, struct pg_state *st,
  177. pgprot_t new_prot, int level)
  178. {
  179. pgprotval_t prot, cur;
  180. static const char units[] = "BKMGTPE";
  181. /*
  182. * If we have a "break" in the series, we need to flush the state that
  183. * we have now. "break" is either changing perms, levels or
  184. * address space marker.
  185. */
  186. prot = pgprot_val(new_prot) & PTE_FLAGS_MASK;
  187. cur = pgprot_val(st->current_prot) & PTE_FLAGS_MASK;
  188. if (!st->level) {
  189. /* First entry */
  190. st->current_prot = new_prot;
  191. st->level = level;
  192. st->marker = address_markers;
  193. st->lines = 0;
  194. pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
  195. st->marker->name);
  196. } else if (prot != cur || level != st->level ||
  197. st->current_address >= st->marker[1].start_address) {
  198. const char *unit = units;
  199. unsigned long delta;
  200. int width = sizeof(unsigned long) * 2;
  201. /*
  202. * Now print the actual finished series
  203. */
  204. if (!st->marker->max_lines ||
  205. st->lines < st->marker->max_lines) {
  206. pt_dump_seq_printf(m, st->to_dmesg,
  207. "0x%0*lx-0x%0*lx ",
  208. width, st->start_address,
  209. width, st->current_address);
  210. delta = st->current_address - st->start_address;
  211. while (!(delta & 1023) && unit[1]) {
  212. delta >>= 10;
  213. unit++;
  214. }
  215. pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
  216. delta, *unit);
  217. printk_prot(m, st->current_prot, st->level,
  218. st->to_dmesg);
  219. }
  220. st->lines++;
  221. /*
  222. * We print markers for special areas of address space,
  223. * such as the start of vmalloc space etc.
  224. * This helps in the interpretation.
  225. */
  226. if (st->current_address >= st->marker[1].start_address) {
  227. if (st->marker->max_lines &&
  228. st->lines > st->marker->max_lines) {
  229. unsigned long nskip =
  230. st->lines - st->marker->max_lines;
  231. pt_dump_seq_printf(m, st->to_dmesg,
  232. "... %lu entr%s skipped ... \n",
  233. nskip,
  234. nskip == 1 ? "y" : "ies");
  235. }
  236. st->marker++;
  237. st->lines = 0;
  238. pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
  239. st->marker->name);
  240. }
  241. st->start_address = st->current_address;
  242. st->current_prot = new_prot;
  243. st->level = level;
  244. }
  245. }
  246. static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
  247. unsigned long P)
  248. {
  249. int i;
  250. pte_t *start;
  251. start = (pte_t *) pmd_page_vaddr(addr);
  252. for (i = 0; i < PTRS_PER_PTE; i++) {
  253. pgprot_t prot = pte_pgprot(*start);
  254. st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
  255. note_page(m, st, prot, 4);
  256. start++;
  257. }
  258. }
  259. #if PTRS_PER_PMD > 1
  260. static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
  261. unsigned long P)
  262. {
  263. int i;
  264. pmd_t *start;
  265. start = (pmd_t *) pud_page_vaddr(addr);
  266. for (i = 0; i < PTRS_PER_PMD; i++) {
  267. st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
  268. if (!pmd_none(*start)) {
  269. pgprotval_t prot = pmd_val(*start) & PTE_FLAGS_MASK;
  270. if (pmd_large(*start) || !pmd_present(*start))
  271. note_page(m, st, __pgprot(prot), 3);
  272. else
  273. walk_pte_level(m, st, *start,
  274. P + i * PMD_LEVEL_MULT);
  275. } else
  276. note_page(m, st, __pgprot(0), 3);
  277. start++;
  278. }
  279. }
  280. #else
  281. #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
  282. #define pud_large(a) pmd_large(__pmd(pud_val(a)))
  283. #define pud_none(a) pmd_none(__pmd(pud_val(a)))
  284. #endif
  285. #if PTRS_PER_PUD > 1
  286. static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
  287. unsigned long P)
  288. {
  289. int i;
  290. pud_t *start;
  291. start = (pud_t *) pgd_page_vaddr(addr);
  292. for (i = 0; i < PTRS_PER_PUD; i++) {
  293. st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
  294. if (!pud_none(*start)) {
  295. pgprotval_t prot = pud_val(*start) & PTE_FLAGS_MASK;
  296. if (pud_large(*start) || !pud_present(*start))
  297. note_page(m, st, __pgprot(prot), 2);
  298. else
  299. walk_pmd_level(m, st, *start,
  300. P + i * PUD_LEVEL_MULT);
  301. } else
  302. note_page(m, st, __pgprot(0), 2);
  303. start++;
  304. }
  305. }
  306. #else
  307. #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(pgd_val(a)),p)
  308. #define pgd_large(a) pud_large(__pud(pgd_val(a)))
  309. #define pgd_none(a) pud_none(__pud(pgd_val(a)))
  310. #endif
  311. void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
  312. {
  313. #ifdef CONFIG_X86_64
  314. pgd_t *start = (pgd_t *) &init_level4_pgt;
  315. #else
  316. pgd_t *start = swapper_pg_dir;
  317. #endif
  318. int i;
  319. struct pg_state st = {};
  320. if (pgd) {
  321. start = pgd;
  322. st.to_dmesg = true;
  323. }
  324. for (i = 0; i < PTRS_PER_PGD; i++) {
  325. st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
  326. if (!pgd_none(*start)) {
  327. pgprotval_t prot = pgd_val(*start) & PTE_FLAGS_MASK;
  328. if (pgd_large(*start) || !pgd_present(*start))
  329. note_page(m, &st, __pgprot(prot), 1);
  330. else
  331. walk_pud_level(m, &st, *start,
  332. i * PGD_LEVEL_MULT);
  333. } else
  334. note_page(m, &st, __pgprot(0), 1);
  335. start++;
  336. }
  337. /* Flush out the last page */
  338. st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
  339. note_page(m, &st, __pgprot(0), 0);
  340. }
  341. static int ptdump_show(struct seq_file *m, void *v)
  342. {
  343. ptdump_walk_pgd_level(m, NULL);
  344. return 0;
  345. }
  346. static int ptdump_open(struct inode *inode, struct file *filp)
  347. {
  348. return single_open(filp, ptdump_show, NULL);
  349. }
  350. static const struct file_operations ptdump_fops = {
  351. .open = ptdump_open,
  352. .read = seq_read,
  353. .llseek = seq_lseek,
  354. .release = single_release,
  355. };
  356. static int pt_dump_init(void)
  357. {
  358. struct dentry *pe;
  359. #ifdef CONFIG_X86_32
  360. /* Not a compile-time constant on x86-32 */
  361. address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
  362. address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
  363. # ifdef CONFIG_HIGHMEM
  364. address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
  365. # endif
  366. address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
  367. #endif
  368. pe = debugfs_create_file("kernel_page_tables", 0600, NULL, NULL,
  369. &ptdump_fops);
  370. if (!pe)
  371. return -ENOMEM;
  372. return 0;
  373. }
  374. __initcall(pt_dump_init);
  375. MODULE_LICENSE("GPL");
  376. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
  377. MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");