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.
5 * (C) Copyright 2008 Intel Corporation
7 * Author: Arjan van de Ven <arjan@linux.intel.com>
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
15 #include <linux/debugfs.h>
16 #include <linux/kasan.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21 #include <linux/highmem.h>
22 #include <linux/pci.h>
24 #include <asm/e820/types.h>
25 #include <asm/pgtable.h>
28 * The dumper groups pagetable entries of the same type into one, and for
29 * that it needs to keep some state when walking, and flush this state
30 * when a "break" in the continuity is found.
34 pgprot_t current_prot;
35 pgprotval_t effective_prot;
36 unsigned long start_address;
37 unsigned long current_address;
38 const struct addr_marker *marker;
42 unsigned long wx_pages;
46 unsigned long start_address;
48 unsigned long max_lines;
51 /* Address space markers hints */
55 enum address_markers_idx {
58 #ifdef CONFIG_MODIFY_LDT_SYSCALL
65 KASAN_SHADOW_START_NR,
69 #ifdef CONFIG_X86_ESPFIX64
82 static struct addr_marker address_markers[] = {
83 [USER_SPACE_NR] = { 0, "User Space" },
84 [KERNEL_SPACE_NR] = { (1UL << 63), "Kernel Space" },
85 [LOW_KERNEL_NR] = { 0UL, "Low Kernel Mapping" },
86 [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" },
87 [VMEMMAP_START_NR] = { 0UL, "Vmemmap" },
90 * These fields get initialized with the (dynamic)
91 * KASAN_SHADOW_{START,END} values in pt_dump_init().
93 [KASAN_SHADOW_START_NR] = { 0UL, "KASAN shadow" },
94 [KASAN_SHADOW_END_NR] = { 0UL, "KASAN shadow end" },
96 #ifdef CONFIG_MODIFY_LDT_SYSCALL
97 [LDT_NR] = { 0UL, "LDT remap" },
99 [CPU_ENTRY_AREA_NR] = { CPU_ENTRY_AREA_BASE,"CPU entry Area" },
100 #ifdef CONFIG_X86_ESPFIX64
101 [ESPFIX_START_NR] = { ESPFIX_BASE_ADDR, "ESPfix Area", 16 },
104 [EFI_END_NR] = { EFI_VA_END, "EFI Runtime Services" },
106 [HIGH_KERNEL_NR] = { __START_KERNEL_map, "High Kernel Mapping" },
107 [MODULES_VADDR_NR] = { MODULES_VADDR, "Modules" },
108 [MODULES_END_NR] = { MODULES_END, "End Modules" },
109 [FIXADDR_START_NR] = { FIXADDR_START, "Fixmap Area" },
110 [END_OF_SPACE_NR] = { -1, NULL }
113 #define INIT_PGD ((pgd_t *) &init_top_pgt)
115 #else /* CONFIG_X86_64 */
117 enum address_markers_idx {
122 #ifdef CONFIG_HIGHMEM
125 #ifdef CONFIG_MODIFY_LDT_SYSCALL
133 static struct addr_marker address_markers[] = {
134 [USER_SPACE_NR] = { 0, "User Space" },
135 [KERNEL_SPACE_NR] = { PAGE_OFFSET, "Kernel Mapping" },
136 [VMALLOC_START_NR] = { 0UL, "vmalloc() Area" },
137 [VMALLOC_END_NR] = { 0UL, "vmalloc() End" },
138 #ifdef CONFIG_HIGHMEM
139 [PKMAP_BASE_NR] = { 0UL, "Persistent kmap() Area" },
141 #ifdef CONFIG_MODIFY_LDT_SYSCALL
142 [LDT_NR] = { 0UL, "LDT remap" },
144 [CPU_ENTRY_AREA_NR] = { 0UL, "CPU entry area" },
145 [FIXADDR_START_NR] = { 0UL, "Fixmap area" },
146 [END_OF_SPACE_NR] = { -1, NULL }
149 #define INIT_PGD (swapper_pg_dir)
151 #endif /* !CONFIG_X86_64 */
153 /* Multipliers for offsets within the PTEs */
154 #define PTE_LEVEL_MULT (PAGE_SIZE)
155 #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
156 #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
157 #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
158 #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT)
160 #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
163 printk(KERN_INFO fmt, ##args); \
166 seq_printf(m, fmt, ##args); \
169 #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
172 printk(KERN_CONT fmt, ##args); \
175 seq_printf(m, fmt, ##args); \
179 * Print a readable form of a pgprot_t to the seq_file
181 static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
183 pgprotval_t pr = pgprot_val(prot);
184 static const char * const level_name[] =
185 { "cr3", "pgd", "p4d", "pud", "pmd", "pte" };
187 if (!(pr & _PAGE_PRESENT)) {
189 pt_dump_cont_printf(m, dmsg, " ");
192 pt_dump_cont_printf(m, dmsg, "USR ");
194 pt_dump_cont_printf(m, dmsg, " ");
196 pt_dump_cont_printf(m, dmsg, "RW ");
198 pt_dump_cont_printf(m, dmsg, "ro ");
200 pt_dump_cont_printf(m, dmsg, "PWT ");
202 pt_dump_cont_printf(m, dmsg, " ");
204 pt_dump_cont_printf(m, dmsg, "PCD ");
206 pt_dump_cont_printf(m, dmsg, " ");
208 /* Bit 7 has a different meaning on level 3 vs 4 */
209 if (level <= 4 && pr & _PAGE_PSE)
210 pt_dump_cont_printf(m, dmsg, "PSE ");
212 pt_dump_cont_printf(m, dmsg, " ");
213 if ((level == 5 && pr & _PAGE_PAT) ||
214 ((level == 4 || level == 3) && pr & _PAGE_PAT_LARGE))
215 pt_dump_cont_printf(m, dmsg, "PAT ");
217 pt_dump_cont_printf(m, dmsg, " ");
218 if (pr & _PAGE_GLOBAL)
219 pt_dump_cont_printf(m, dmsg, "GLB ");
221 pt_dump_cont_printf(m, dmsg, " ");
223 pt_dump_cont_printf(m, dmsg, "NX ");
225 pt_dump_cont_printf(m, dmsg, "x ");
227 pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
231 * On 64 bits, sign-extend the 48 bit address to 64 bit
233 static unsigned long normalize_addr(unsigned long u)
236 if (!IS_ENABLED(CONFIG_X86_64))
239 shift = 64 - (__VIRTUAL_MASK_SHIFT + 1);
240 return (signed long)(u << shift) >> shift;
243 static void note_wx(struct pg_state *st)
245 unsigned long npages;
247 npages = (st->current_address - st->start_address) / PAGE_SIZE;
249 #ifdef CONFIG_PCI_BIOS
251 * If PCI BIOS is enabled, the PCI BIOS area is forced to WX.
252 * Inform about it, but avoid the warning.
254 if (pcibios_enabled && st->start_address >= PAGE_OFFSET + BIOS_BEGIN &&
255 st->current_address <= PAGE_OFFSET + BIOS_END) {
256 pr_warn_once("x86/mm: PCI BIOS W+X mapping %lu pages\n", npages);
260 /* Account the WX pages */
261 st->wx_pages += npages;
262 WARN_ONCE(1, "x86/mm: Found insecure W+X mapping at address %pS\n",
263 (void *)st->start_address);
267 * This function gets called on a break in a continuous series
268 * of PTE entries; the next one is different so we need to
269 * print what we collected so far.
271 static void note_page(struct seq_file *m, struct pg_state *st,
272 pgprot_t new_prot, pgprotval_t new_eff, int level)
274 pgprotval_t prot, cur, eff;
275 static const char units[] = "BKMGTPE";
278 * If we have a "break" in the series, we need to flush the state that
279 * we have now. "break" is either changing perms, levels or
280 * address space marker.
282 prot = pgprot_val(new_prot);
283 cur = pgprot_val(st->current_prot);
284 eff = st->effective_prot;
288 st->current_prot = new_prot;
289 st->effective_prot = new_eff;
291 st->marker = address_markers;
293 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
295 } else if (prot != cur || new_eff != eff || level != st->level ||
296 st->current_address >= st->marker[1].start_address) {
297 const char *unit = units;
299 int width = sizeof(unsigned long) * 2;
301 if (st->check_wx && (eff & _PAGE_RW) && !(eff & _PAGE_NX))
305 * Now print the actual finished series
307 if (!st->marker->max_lines ||
308 st->lines < st->marker->max_lines) {
309 pt_dump_seq_printf(m, st->to_dmesg,
311 width, st->start_address,
312 width, st->current_address);
314 delta = st->current_address - st->start_address;
315 while (!(delta & 1023) && unit[1]) {
319 pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
321 printk_prot(m, st->current_prot, st->level,
327 * We print markers for special areas of address space,
328 * such as the start of vmalloc space etc.
329 * This helps in the interpretation.
331 if (st->current_address >= st->marker[1].start_address) {
332 if (st->marker->max_lines &&
333 st->lines > st->marker->max_lines) {
334 unsigned long nskip =
335 st->lines - st->marker->max_lines;
336 pt_dump_seq_printf(m, st->to_dmesg,
337 "... %lu entr%s skipped ... \n",
339 nskip == 1 ? "y" : "ies");
343 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
347 st->start_address = st->current_address;
348 st->current_prot = new_prot;
349 st->effective_prot = new_eff;
354 static inline pgprotval_t effective_prot(pgprotval_t prot1, pgprotval_t prot2)
356 return (prot1 & prot2 & (_PAGE_USER | _PAGE_RW)) |
357 ((prot1 | prot2) & _PAGE_NX);
360 static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
361 pgprotval_t eff_in, unsigned long P)
365 pgprotval_t prot, eff;
367 for (i = 0; i < PTRS_PER_PTE; i++) {
368 st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
369 pte = pte_offset_map(&addr, st->current_address);
370 prot = pte_flags(*pte);
371 eff = effective_prot(eff_in, prot);
372 note_page(m, st, __pgprot(prot), eff, 5);
379 * This is an optimization for KASAN=y case. Since all kasan page tables
380 * eventually point to the kasan_early_shadow_page we could call note_page()
381 * right away without walking through lower level page tables. This saves
382 * us dozens of seconds (minutes for 5-level config) while checking for
383 * W+X mapping or reading kernel_page_tables debugfs file.
385 static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st,
388 if (__pa(pt) == __pa(kasan_early_shadow_pmd) ||
389 (pgtable_l5_enabled() &&
390 __pa(pt) == __pa(kasan_early_shadow_p4d)) ||
391 __pa(pt) == __pa(kasan_early_shadow_pud)) {
392 pgprotval_t prot = pte_flags(kasan_early_shadow_pte[0]);
393 note_page(m, st, __pgprot(prot), 0, 5);
399 static inline bool kasan_page_table(struct seq_file *m, struct pg_state *st,
408 static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
409 pgprotval_t eff_in, unsigned long P)
412 pmd_t *start, *pmd_start;
413 pgprotval_t prot, eff;
415 pmd_start = start = (pmd_t *)pud_page_vaddr(addr);
416 for (i = 0; i < PTRS_PER_PMD; i++) {
417 st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
418 if (!pmd_none(*start)) {
419 prot = pmd_flags(*start);
420 eff = effective_prot(eff_in, prot);
421 if (pmd_large(*start) || !pmd_present(*start)) {
422 note_page(m, st, __pgprot(prot), eff, 4);
423 } else if (!kasan_page_table(m, st, pmd_start)) {
424 walk_pte_level(m, st, *start, eff,
425 P + i * PMD_LEVEL_MULT);
428 note_page(m, st, __pgprot(0), 0, 4);
434 #define walk_pmd_level(m,s,a,e,p) walk_pte_level(m,s,__pmd(pud_val(a)),e,p)
435 #define pud_large(a) pmd_large(__pmd(pud_val(a)))
436 #define pud_none(a) pmd_none(__pmd(pud_val(a)))
441 static void walk_pud_level(struct seq_file *m, struct pg_state *st, p4d_t addr,
442 pgprotval_t eff_in, unsigned long P)
445 pud_t *start, *pud_start;
446 pgprotval_t prot, eff;
448 pud_start = start = (pud_t *)p4d_page_vaddr(addr);
450 for (i = 0; i < PTRS_PER_PUD; i++) {
451 st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
452 if (!pud_none(*start)) {
453 prot = pud_flags(*start);
454 eff = effective_prot(eff_in, prot);
455 if (pud_large(*start) || !pud_present(*start)) {
456 note_page(m, st, __pgprot(prot), eff, 3);
457 } else if (!kasan_page_table(m, st, pud_start)) {
458 walk_pmd_level(m, st, *start, eff,
459 P + i * PUD_LEVEL_MULT);
462 note_page(m, st, __pgprot(0), 0, 3);
469 #define walk_pud_level(m,s,a,e,p) walk_pmd_level(m,s,__pud(p4d_val(a)),e,p)
470 #define p4d_large(a) pud_large(__pud(p4d_val(a)))
471 #define p4d_none(a) pud_none(__pud(p4d_val(a)))
474 static void walk_p4d_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
475 pgprotval_t eff_in, unsigned long P)
478 p4d_t *start, *p4d_start;
479 pgprotval_t prot, eff;
481 if (PTRS_PER_P4D == 1)
482 return walk_pud_level(m, st, __p4d(pgd_val(addr)), eff_in, P);
484 p4d_start = start = (p4d_t *)pgd_page_vaddr(addr);
486 for (i = 0; i < PTRS_PER_P4D; i++) {
487 st->current_address = normalize_addr(P + i * P4D_LEVEL_MULT);
488 if (!p4d_none(*start)) {
489 prot = p4d_flags(*start);
490 eff = effective_prot(eff_in, prot);
491 if (p4d_large(*start) || !p4d_present(*start)) {
492 note_page(m, st, __pgprot(prot), eff, 2);
493 } else if (!kasan_page_table(m, st, p4d_start)) {
494 walk_pud_level(m, st, *start, eff,
495 P + i * P4D_LEVEL_MULT);
498 note_page(m, st, __pgprot(0), 0, 2);
504 #define pgd_large(a) (pgtable_l5_enabled() ? pgd_large(a) : p4d_large(__p4d(pgd_val(a))))
505 #define pgd_none(a) (pgtable_l5_enabled() ? pgd_none(a) : p4d_none(__p4d(pgd_val(a))))
507 static inline bool is_hypervisor_range(int idx)
511 * A hole in the beginning of kernel address space reserved
514 return (idx >= pgd_index(GUARD_HOLE_BASE_ADDR)) &&
515 (idx < pgd_index(GUARD_HOLE_END_ADDR));
521 static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd,
522 bool checkwx, bool dmesg)
524 pgd_t *start = INIT_PGD;
525 pgprotval_t prot, eff;
527 struct pg_state st = {};
534 st.check_wx = checkwx;
538 for (i = 0; i < PTRS_PER_PGD; i++) {
539 st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
540 if (!pgd_none(*start) && !is_hypervisor_range(i)) {
541 prot = pgd_flags(*start);
542 #ifdef CONFIG_X86_PAE
543 eff = _PAGE_USER | _PAGE_RW;
547 if (pgd_large(*start) || !pgd_present(*start)) {
548 note_page(m, &st, __pgprot(prot), eff, 1);
550 walk_p4d_level(m, &st, *start, eff,
554 note_page(m, &st, __pgprot(0), 0, 1);
560 /* Flush out the last page */
561 st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
562 note_page(m, &st, __pgprot(0), 0, 0);
566 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
569 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
572 void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
574 ptdump_walk_pgd_level_core(m, pgd, false, true);
577 void ptdump_walk_pgd_level_debugfs(struct seq_file *m, pgd_t *pgd, bool user)
579 #ifdef CONFIG_PAGE_TABLE_ISOLATION
580 if (user && static_cpu_has(X86_FEATURE_PTI))
581 pgd = kernel_to_user_pgdp(pgd);
583 ptdump_walk_pgd_level_core(m, pgd, false, false);
585 EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs);
587 void ptdump_walk_user_pgd_level_checkwx(void)
589 #ifdef CONFIG_PAGE_TABLE_ISOLATION
590 pgd_t *pgd = INIT_PGD;
592 if (!(__supported_pte_mask & _PAGE_NX) ||
593 !static_cpu_has(X86_FEATURE_PTI))
596 pr_info("x86/mm: Checking user space page tables\n");
597 pgd = kernel_to_user_pgdp(pgd);
598 ptdump_walk_pgd_level_core(NULL, pgd, true, false);
602 void ptdump_walk_pgd_level_checkwx(void)
604 ptdump_walk_pgd_level_core(NULL, NULL, true, false);
607 static int __init pt_dump_init(void)
610 * Various markers are not compile-time constants, so assign them
614 address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
615 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
616 address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
617 #ifdef CONFIG_MODIFY_LDT_SYSCALL
618 address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
621 address_markers[KASAN_SHADOW_START_NR].start_address = KASAN_SHADOW_START;
622 address_markers[KASAN_SHADOW_END_NR].start_address = KASAN_SHADOW_END;
626 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
627 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
628 # ifdef CONFIG_HIGHMEM
629 address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
631 address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
632 address_markers[CPU_ENTRY_AREA_NR].start_address = CPU_ENTRY_AREA_BASE;
633 # ifdef CONFIG_MODIFY_LDT_SYSCALL
634 address_markers[LDT_NR].start_address = LDT_BASE_ADDR;
639 __initcall(pt_dump_init);