dump_pagetables.c 12 KB

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