dump_pagetables.c 10 KB

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