dump_pagetables.c 9.9 KB

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