dump_pagetables.c 11 KB

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