dump_pagetables.c 9.1 KB

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