dump_pagetables.c 8.7 KB

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