1 // SPDX-License-Identifier: GPL-2.0-only
3 * Debug helper to dump the current kernel pagetables of the system
4 * so that we can see what the various memory ranges are set to.
6 * (C) Copyright 2008 Intel Corporation
8 * Author: Arjan van de Ven <arjan@linux.intel.com>
11 #include <linux/debugfs.h>
12 #include <linux/kasan.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/seq_file.h>
17 #include <linux/highmem.h>
18 #include <linux/pci.h>
19 #include <linux/ptdump.h>
21 #include <asm/e820/types.h>
22 #include <asm/pgtable.h>
25 * The dumper groups pagetable entries of the same type into one, and for
26 * that it needs to keep some state when walking, and flush this state
27 * when a "break" in the continuity is found.
30 struct ptdump_state ptdump
;
32 pgprotval_t current_prot
;
33 pgprotval_t effective_prot
;
34 pgprotval_t prot_levels
[5];
35 unsigned long start_address
;
36 const struct addr_marker
*marker
;
40 unsigned long wx_pages
;
45 unsigned long start_address
;
47 unsigned long max_lines
;
50 /* Address space markers hints */
54 enum address_markers_idx
{
57 #ifdef CONFIG_MODIFY_LDT_SYSCALL
64 KASAN_SHADOW_START_NR
,
68 #ifdef CONFIG_X86_ESPFIX64
81 static struct addr_marker address_markers
[] = {
82 [USER_SPACE_NR
] = { 0, "User Space" },
83 [KERNEL_SPACE_NR
] = { (1UL << 63), "Kernel Space" },
84 [LOW_KERNEL_NR
] = { 0UL, "Low Kernel Mapping" },
85 [VMALLOC_START_NR
] = { 0UL, "vmalloc() Area" },
86 [VMEMMAP_START_NR
] = { 0UL, "Vmemmap" },
89 * These fields get initialized with the (dynamic)
90 * KASAN_SHADOW_{START,END} values in pt_dump_init().
92 [KASAN_SHADOW_START_NR
] = { 0UL, "KASAN shadow" },
93 [KASAN_SHADOW_END_NR
] = { 0UL, "KASAN shadow end" },
95 #ifdef CONFIG_MODIFY_LDT_SYSCALL
96 [LDT_NR
] = { 0UL, "LDT remap" },
98 [CPU_ENTRY_AREA_NR
] = { CPU_ENTRY_AREA_BASE
,"CPU entry Area" },
99 #ifdef CONFIG_X86_ESPFIX64
100 [ESPFIX_START_NR
] = { ESPFIX_BASE_ADDR
, "ESPfix Area", 16 },
103 [EFI_END_NR
] = { EFI_VA_END
, "EFI Runtime Services" },
105 [HIGH_KERNEL_NR
] = { __START_KERNEL_map
, "High Kernel Mapping" },
106 [MODULES_VADDR_NR
] = { MODULES_VADDR
, "Modules" },
107 [MODULES_END_NR
] = { MODULES_END
, "End Modules" },
108 [FIXADDR_START_NR
] = { FIXADDR_START
, "Fixmap Area" },
109 [END_OF_SPACE_NR
] = { -1, NULL
}
112 #define INIT_PGD ((pgd_t *) &init_top_pgt)
114 #else /* CONFIG_X86_64 */
116 enum address_markers_idx
{
121 #ifdef CONFIG_HIGHMEM
124 #ifdef CONFIG_MODIFY_LDT_SYSCALL
132 static struct addr_marker address_markers
[] = {
133 [USER_SPACE_NR
] = { 0, "User Space" },
134 [KERNEL_SPACE_NR
] = { PAGE_OFFSET
, "Kernel Mapping" },
135 [VMALLOC_START_NR
] = { 0UL, "vmalloc() Area" },
136 [VMALLOC_END_NR
] = { 0UL, "vmalloc() End" },
137 #ifdef CONFIG_HIGHMEM
138 [PKMAP_BASE_NR
] = { 0UL, "Persistent kmap() Area" },
140 #ifdef CONFIG_MODIFY_LDT_SYSCALL
141 [LDT_NR
] = { 0UL, "LDT remap" },
143 [CPU_ENTRY_AREA_NR
] = { 0UL, "CPU entry area" },
144 [FIXADDR_START_NR
] = { 0UL, "Fixmap area" },
145 [END_OF_SPACE_NR
] = { -1, NULL
}
148 #define INIT_PGD (swapper_pg_dir)
150 #endif /* !CONFIG_X86_64 */
152 /* Multipliers for offsets within the PTEs */
153 #define PTE_LEVEL_MULT (PAGE_SIZE)
154 #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
155 #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
156 #define P4D_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
157 #define PGD_LEVEL_MULT (PTRS_PER_P4D * P4D_LEVEL_MULT)
159 #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
162 printk(KERN_INFO fmt, ##args); \
165 seq_printf(m, fmt, ##args); \
168 #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
171 printk(KERN_CONT fmt, ##args); \
174 seq_printf(m, fmt, ##args); \
178 * Print a readable form of a pgprot_t to the seq_file
180 static void printk_prot(struct seq_file
*m
, pgprotval_t pr
, int level
, bool dmsg
)
182 static const char * const level_name
[] =
183 { "pgd", "p4d", "pud", "pmd", "pte" };
185 if (!(pr
& _PAGE_PRESENT
)) {
187 pt_dump_cont_printf(m
, dmsg
, " ");
190 pt_dump_cont_printf(m
, dmsg
, "USR ");
192 pt_dump_cont_printf(m
, dmsg
, " ");
194 pt_dump_cont_printf(m
, dmsg
, "RW ");
196 pt_dump_cont_printf(m
, dmsg
, "ro ");
198 pt_dump_cont_printf(m
, dmsg
, "PWT ");
200 pt_dump_cont_printf(m
, dmsg
, " ");
202 pt_dump_cont_printf(m
, dmsg
, "PCD ");
204 pt_dump_cont_printf(m
, dmsg
, " ");
206 /* Bit 7 has a different meaning on level 3 vs 4 */
207 if (level
<= 3 && pr
& _PAGE_PSE
)
208 pt_dump_cont_printf(m
, dmsg
, "PSE ");
210 pt_dump_cont_printf(m
, dmsg
, " ");
211 if ((level
== 4 && pr
& _PAGE_PAT
) ||
212 ((level
== 3 || level
== 2) && pr
& _PAGE_PAT_LARGE
))
213 pt_dump_cont_printf(m
, dmsg
, "PAT ");
215 pt_dump_cont_printf(m
, dmsg
, " ");
216 if (pr
& _PAGE_GLOBAL
)
217 pt_dump_cont_printf(m
, dmsg
, "GLB ");
219 pt_dump_cont_printf(m
, dmsg
, " ");
221 pt_dump_cont_printf(m
, dmsg
, "NX ");
223 pt_dump_cont_printf(m
, dmsg
, "x ");
225 pt_dump_cont_printf(m
, dmsg
, "%s\n", level_name
[level
]);
228 static void note_wx(struct pg_state
*st
, unsigned long addr
)
230 unsigned long npages
;
232 npages
= (addr
- st
->start_address
) / PAGE_SIZE
;
234 #ifdef CONFIG_PCI_BIOS
236 * If PCI BIOS is enabled, the PCI BIOS area is forced to WX.
237 * Inform about it, but avoid the warning.
239 if (pcibios_enabled
&& st
->start_address
>= PAGE_OFFSET
+ BIOS_BEGIN
&&
240 addr
<= PAGE_OFFSET
+ BIOS_END
) {
241 pr_warn_once("x86/mm: PCI BIOS W+X mapping %lu pages\n", npages
);
245 /* Account the WX pages */
246 st
->wx_pages
+= npages
;
247 WARN_ONCE(__supported_pte_mask
& _PAGE_NX
,
248 "x86/mm: Found insecure W+X mapping at address %pS\n",
249 (void *)st
->start_address
);
252 static void effective_prot(struct ptdump_state
*pt_st
, int level
, u64 val
)
254 struct pg_state
*st
= container_of(pt_st
, struct pg_state
, ptdump
);
255 pgprotval_t prot
= val
& PTE_FLAGS_MASK
;
256 pgprotval_t effective
;
259 pgprotval_t higher_prot
= st
->prot_levels
[level
- 1];
261 effective
= (higher_prot
& prot
& (_PAGE_USER
| _PAGE_RW
)) |
262 ((higher_prot
| prot
) & _PAGE_NX
);
267 st
->prot_levels
[level
] = effective
;
271 * This function gets called on a break in a continuous series
272 * of PTE entries; the next one is different so we need to
273 * print what we collected so far.
275 static void note_page(struct ptdump_state
*pt_st
, unsigned long addr
, int level
,
278 struct pg_state
*st
= container_of(pt_st
, struct pg_state
, ptdump
);
279 pgprotval_t new_prot
, new_eff
;
280 pgprotval_t cur
, eff
;
281 static const char units
[] = "BKMGTPE";
282 struct seq_file
*m
= st
->seq
;
284 new_prot
= val
& PTE_FLAGS_MASK
;
288 new_eff
= st
->prot_levels
[level
];
291 * If we have a "break" in the series, we need to flush the state that
292 * we have now. "break" is either changing perms, levels or
293 * address space marker.
295 cur
= st
->current_prot
;
296 eff
= st
->effective_prot
;
298 if (st
->level
== -1) {
300 st
->current_prot
= new_prot
;
301 st
->effective_prot
= new_eff
;
303 st
->marker
= address_markers
;
305 pt_dump_seq_printf(m
, st
->to_dmesg
, "---[ %s ]---\n",
307 } else if (new_prot
!= cur
|| new_eff
!= eff
|| level
!= st
->level
||
308 addr
>= st
->marker
[1].start_address
) {
309 const char *unit
= units
;
311 int width
= sizeof(unsigned long) * 2;
313 if (st
->check_wx
&& (eff
& _PAGE_RW
) && !(eff
& _PAGE_NX
))
317 * Now print the actual finished series
319 if (!st
->marker
->max_lines
||
320 st
->lines
< st
->marker
->max_lines
) {
321 pt_dump_seq_printf(m
, st
->to_dmesg
,
323 width
, st
->start_address
,
326 delta
= addr
- st
->start_address
;
327 while (!(delta
& 1023) && unit
[1]) {
331 pt_dump_cont_printf(m
, st
->to_dmesg
, "%9lu%c ",
333 printk_prot(m
, st
->current_prot
, st
->level
,
339 * We print markers for special areas of address space,
340 * such as the start of vmalloc space etc.
341 * This helps in the interpretation.
343 if (addr
>= st
->marker
[1].start_address
) {
344 if (st
->marker
->max_lines
&&
345 st
->lines
> st
->marker
->max_lines
) {
346 unsigned long nskip
=
347 st
->lines
- st
->marker
->max_lines
;
348 pt_dump_seq_printf(m
, st
->to_dmesg
,
349 "... %lu entr%s skipped ... \n",
351 nskip
== 1 ? "y" : "ies");
355 pt_dump_seq_printf(m
, st
->to_dmesg
, "---[ %s ]---\n",
359 st
->start_address
= addr
;
360 st
->current_prot
= new_prot
;
361 st
->effective_prot
= new_eff
;
366 static void ptdump_walk_pgd_level_core(struct seq_file
*m
,
367 struct mm_struct
*mm
, pgd_t
*pgd
,
368 bool checkwx
, bool dmesg
)
370 const struct ptdump_range ptdump_ranges
[] = {
372 {0, PTRS_PER_PGD
* PGD_LEVEL_MULT
/ 2},
373 {GUARD_HOLE_END_ADDR
, ~0UL},
380 struct pg_state st
= {
382 .note_page
= note_page
,
383 .effective_prot
= effective_prot
,
384 .range
= ptdump_ranges
392 ptdump_walk_pgd(&st
.ptdump
, mm
, pgd
);
397 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
400 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
403 void ptdump_walk_pgd_level(struct seq_file
*m
, struct mm_struct
*mm
)
405 ptdump_walk_pgd_level_core(m
, mm
, mm
->pgd
, false, true);
408 void ptdump_walk_pgd_level_debugfs(struct seq_file
*m
, struct mm_struct
*mm
,
411 pgd_t
*pgd
= mm
->pgd
;
412 #ifdef CONFIG_PAGE_TABLE_ISOLATION
413 if (user
&& boot_cpu_has(X86_FEATURE_PTI
))
414 pgd
= kernel_to_user_pgdp(pgd
);
416 ptdump_walk_pgd_level_core(m
, mm
, pgd
, false, false);
418 EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level_debugfs
);
420 void ptdump_walk_user_pgd_level_checkwx(void)
422 #ifdef CONFIG_PAGE_TABLE_ISOLATION
423 pgd_t
*pgd
= INIT_PGD
;
425 if (!(__supported_pte_mask
& _PAGE_NX
) ||
426 !boot_cpu_has(X86_FEATURE_PTI
))
429 pr_info("x86/mm: Checking user space page tables\n");
430 pgd
= kernel_to_user_pgdp(pgd
);
431 ptdump_walk_pgd_level_core(NULL
, &init_mm
, pgd
, true, false);
435 void ptdump_walk_pgd_level_checkwx(void)
437 ptdump_walk_pgd_level_core(NULL
, &init_mm
, INIT_PGD
, true, false);
440 static int __init
pt_dump_init(void)
443 * Various markers are not compile-time constants, so assign them
447 address_markers
[LOW_KERNEL_NR
].start_address
= PAGE_OFFSET
;
448 address_markers
[VMALLOC_START_NR
].start_address
= VMALLOC_START
;
449 address_markers
[VMEMMAP_START_NR
].start_address
= VMEMMAP_START
;
450 #ifdef CONFIG_MODIFY_LDT_SYSCALL
451 address_markers
[LDT_NR
].start_address
= LDT_BASE_ADDR
;
454 address_markers
[KASAN_SHADOW_START_NR
].start_address
= KASAN_SHADOW_START
;
455 address_markers
[KASAN_SHADOW_END_NR
].start_address
= KASAN_SHADOW_END
;
459 address_markers
[VMALLOC_START_NR
].start_address
= VMALLOC_START
;
460 address_markers
[VMALLOC_END_NR
].start_address
= VMALLOC_END
;
461 # ifdef CONFIG_HIGHMEM
462 address_markers
[PKMAP_BASE_NR
].start_address
= PKMAP_BASE
;
464 address_markers
[FIXADDR_START_NR
].start_address
= FIXADDR_START
;
465 address_markers
[CPU_ENTRY_AREA_NR
].start_address
= CPU_ENTRY_AREA_BASE
;
466 # ifdef CONFIG_MODIFY_LDT_SYSCALL
467 address_markers
[LDT_NR
].start_address
= LDT_BASE_ADDR
;
472 __initcall(pt_dump_init
);