dump_pagetables.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. /*
  152. * Now print the actual finished series
  153. */
  154. seq_printf(m, "0x%p-0x%p ",
  155. (void *)st->start_address,
  156. (void *)st->current_address);
  157. delta = (st->current_address - st->start_address) >> 10;
  158. while (!(delta & 1023) && unit[1]) {
  159. delta >>= 10;
  160. unit++;
  161. }
  162. seq_printf(m, "%9lu%c ", delta, *unit);
  163. printk_prot(m, st->current_prot, st->level);
  164. /*
  165. * We print markers for special areas of address space,
  166. * such as the start of vmalloc space etc.
  167. * This helps in the interpretation.
  168. */
  169. if (st->current_address >= st->marker[1].start_address) {
  170. st->marker++;
  171. seq_printf(m, "---[ %s ]---\n", st->marker->name);
  172. }
  173. st->start_address = st->current_address;
  174. st->current_prot = new_prot;
  175. st->level = level;
  176. }
  177. }
  178. static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
  179. unsigned long P)
  180. {
  181. int i;
  182. pte_t *start;
  183. start = (pte_t *) pmd_page_vaddr(addr);
  184. for (i = 0; i < PTRS_PER_PTE; i++) {
  185. pgprot_t prot = pte_pgprot(*start);
  186. st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
  187. note_page(m, st, prot, 4);
  188. start++;
  189. }
  190. }
  191. #if PTRS_PER_PMD > 1
  192. static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
  193. unsigned long P)
  194. {
  195. int i;
  196. pmd_t *start;
  197. start = (pmd_t *) pud_page_vaddr(addr);
  198. for (i = 0; i < PTRS_PER_PMD; i++) {
  199. st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
  200. if (!pmd_none(*start)) {
  201. pgprotval_t prot = pmd_val(*start) & PTE_FLAGS_MASK;
  202. if (pmd_large(*start) || !pmd_present(*start))
  203. note_page(m, st, __pgprot(prot), 3);
  204. else
  205. walk_pte_level(m, st, *start,
  206. P + i * PMD_LEVEL_MULT);
  207. } else
  208. note_page(m, st, __pgprot(0), 3);
  209. start++;
  210. }
  211. }
  212. #else
  213. #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
  214. #define pud_large(a) pmd_large(__pmd(pud_val(a)))
  215. #define pud_none(a) pmd_none(__pmd(pud_val(a)))
  216. #endif
  217. #if PTRS_PER_PUD > 1
  218. static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
  219. unsigned long P)
  220. {
  221. int i;
  222. pud_t *start;
  223. start = (pud_t *) pgd_page_vaddr(addr);
  224. for (i = 0; i < PTRS_PER_PUD; i++) {
  225. st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
  226. if (!pud_none(*start)) {
  227. pgprotval_t prot = pud_val(*start) & PTE_FLAGS_MASK;
  228. if (pud_large(*start) || !pud_present(*start))
  229. note_page(m, st, __pgprot(prot), 2);
  230. else
  231. walk_pmd_level(m, st, *start,
  232. P + i * PUD_LEVEL_MULT);
  233. } else
  234. note_page(m, st, __pgprot(0), 2);
  235. start++;
  236. }
  237. }
  238. #else
  239. #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(pgd_val(a)),p)
  240. #define pgd_large(a) pud_large(__pud(pgd_val(a)))
  241. #define pgd_none(a) pud_none(__pud(pgd_val(a)))
  242. #endif
  243. static void walk_pgd_level(struct seq_file *m)
  244. {
  245. #ifdef CONFIG_X86_64
  246. pgd_t *start = (pgd_t *) &init_level4_pgt;
  247. #else
  248. pgd_t *start = swapper_pg_dir;
  249. #endif
  250. int i;
  251. struct pg_state st;
  252. memset(&st, 0, sizeof(st));
  253. for (i = 0; i < PTRS_PER_PGD; i++) {
  254. st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
  255. if (!pgd_none(*start)) {
  256. pgprotval_t prot = pgd_val(*start) & PTE_FLAGS_MASK;
  257. if (pgd_large(*start) || !pgd_present(*start))
  258. note_page(m, &st, __pgprot(prot), 1);
  259. else
  260. walk_pud_level(m, &st, *start,
  261. i * PGD_LEVEL_MULT);
  262. } else
  263. note_page(m, &st, __pgprot(0), 1);
  264. start++;
  265. }
  266. /* Flush out the last page */
  267. st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
  268. note_page(m, &st, __pgprot(0), 0);
  269. }
  270. static int ptdump_show(struct seq_file *m, void *v)
  271. {
  272. walk_pgd_level(m);
  273. return 0;
  274. }
  275. static int ptdump_open(struct inode *inode, struct file *filp)
  276. {
  277. return single_open(filp, ptdump_show, NULL);
  278. }
  279. static const struct file_operations ptdump_fops = {
  280. .open = ptdump_open,
  281. .read = seq_read,
  282. .llseek = seq_lseek,
  283. .release = single_release,
  284. };
  285. static int pt_dump_init(void)
  286. {
  287. struct dentry *pe;
  288. #ifdef CONFIG_X86_32
  289. /* Not a compile-time constant on x86-32 */
  290. address_markers[2].start_address = VMALLOC_START;
  291. address_markers[3].start_address = VMALLOC_END;
  292. # ifdef CONFIG_HIGHMEM
  293. address_markers[4].start_address = PKMAP_BASE;
  294. address_markers[5].start_address = FIXADDR_START;
  295. # else
  296. address_markers[4].start_address = FIXADDR_START;
  297. # endif
  298. #endif
  299. pe = debugfs_create_file("kernel_page_tables", 0600, NULL, NULL,
  300. &ptdump_fops);
  301. if (!pe)
  302. return -ENOMEM;
  303. return 0;
  304. }
  305. __initcall(pt_dump_init);
  306. MODULE_LICENSE("GPL");
  307. MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
  308. MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");