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>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
21 #include <asm/kasan.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.
31 pgprot_t current_prot;
32 unsigned long start_address;
33 unsigned long current_address;
34 const struct addr_marker *marker;
38 unsigned long wx_pages;
42 unsigned long start_address;
44 unsigned long max_lines;
47 /* indices for address_markers; keep sync'd w/ address_markers below */
48 enum address_markers_idx {
56 KASAN_SHADOW_START_NR,
59 # ifdef CONFIG_X86_ESPFIX64
69 # ifdef CONFIG_HIGHMEM
76 /* Address space markers hints */
77 static struct addr_marker address_markers[] = {
80 { 0x8000000000000000UL, "Kernel Space" },
81 { 0/* PAGE_OFFSET */, "Low Kernel Mapping" },
82 { 0/* VMALLOC_START */, "vmalloc() Area" },
83 { 0/* VMEMMAP_START */, "Vmemmap" },
85 { KASAN_SHADOW_START, "KASAN shadow" },
86 { KASAN_SHADOW_END, "KASAN shadow end" },
88 # ifdef CONFIG_X86_ESPFIX64
89 { ESPFIX_BASE_ADDR, "ESPfix Area", 16 },
92 { EFI_VA_END, "EFI Runtime Services" },
94 { __START_KERNEL_map, "High Kernel Mapping" },
95 { MODULES_VADDR, "Modules" },
96 { MODULES_END, "End Modules" },
98 { PAGE_OFFSET, "Kernel Mapping" },
99 { 0/* VMALLOC_START */, "vmalloc() Area" },
100 { 0/*VMALLOC_END*/, "vmalloc() End" },
101 # ifdef CONFIG_HIGHMEM
102 { 0/*PKMAP_BASE*/, "Persistent kmap() Area" },
104 { 0/*FIXADDR_START*/, "Fixmap Area" },
106 { -1, NULL } /* End of list */
109 /* Multipliers for offsets within the PTEs */
110 #define PTE_LEVEL_MULT (PAGE_SIZE)
111 #define PMD_LEVEL_MULT (PTRS_PER_PTE * PTE_LEVEL_MULT)
112 #define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
113 #define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
115 #define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
118 printk(KERN_INFO fmt, ##args); \
121 seq_printf(m, fmt, ##args); \
124 #define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
127 printk(KERN_CONT fmt, ##args); \
130 seq_printf(m, fmt, ##args); \
134 * Print a readable form of a pgprot_t to the seq_file
136 static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
138 pgprotval_t pr = pgprot_val(prot);
139 static const char * const level_name[] =
140 { "cr3", "pgd", "pud", "pmd", "pte" };
142 if (!pgprot_val(prot)) {
144 pt_dump_cont_printf(m, dmsg, " ");
147 pt_dump_cont_printf(m, dmsg, "USR ");
149 pt_dump_cont_printf(m, dmsg, " ");
151 pt_dump_cont_printf(m, dmsg, "RW ");
153 pt_dump_cont_printf(m, dmsg, "ro ");
155 pt_dump_cont_printf(m, dmsg, "PWT ");
157 pt_dump_cont_printf(m, dmsg, " ");
159 pt_dump_cont_printf(m, dmsg, "PCD ");
161 pt_dump_cont_printf(m, dmsg, " ");
163 /* Bit 7 has a different meaning on level 3 vs 4 */
164 if (level <= 3 && pr & _PAGE_PSE)
165 pt_dump_cont_printf(m, dmsg, "PSE ");
167 pt_dump_cont_printf(m, dmsg, " ");
168 if ((level == 4 && pr & _PAGE_PAT) ||
169 ((level == 3 || level == 2) && pr & _PAGE_PAT_LARGE))
170 pt_dump_cont_printf(m, dmsg, "PAT ");
172 pt_dump_cont_printf(m, dmsg, " ");
173 if (pr & _PAGE_GLOBAL)
174 pt_dump_cont_printf(m, dmsg, "GLB ");
176 pt_dump_cont_printf(m, dmsg, " ");
178 pt_dump_cont_printf(m, dmsg, "NX ");
180 pt_dump_cont_printf(m, dmsg, "x ");
182 pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
186 * On 64 bits, sign-extend the 48 bit address to 64 bit
188 static unsigned long normalize_addr(unsigned long u)
191 return (signed long)(u << 16) >> 16;
198 * This function gets called on a break in a continuous series
199 * of PTE entries; the next one is different so we need to
200 * print what we collected so far.
202 static void note_page(struct seq_file *m, struct pg_state *st,
203 pgprot_t new_prot, int level)
205 pgprotval_t prot, cur;
206 static const char units[] = "BKMGTPE";
209 * If we have a "break" in the series, we need to flush the state that
210 * we have now. "break" is either changing perms, levels or
211 * address space marker.
213 prot = pgprot_val(new_prot);
214 cur = pgprot_val(st->current_prot);
218 st->current_prot = new_prot;
220 st->marker = address_markers;
222 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
224 } else if (prot != cur || level != st->level ||
225 st->current_address >= st->marker[1].start_address) {
226 const char *unit = units;
228 int width = sizeof(unsigned long) * 2;
229 pgprotval_t pr = pgprot_val(st->current_prot);
231 if (st->check_wx && (pr & _PAGE_RW) && !(pr & _PAGE_NX)) {
233 "x86/mm: Found insecure W+X mapping at address %p/%pS\n",
234 (void *)st->start_address,
235 (void *)st->start_address);
236 st->wx_pages += (st->current_address -
237 st->start_address) / PAGE_SIZE;
241 * Now print the actual finished series
243 if (!st->marker->max_lines ||
244 st->lines < st->marker->max_lines) {
245 pt_dump_seq_printf(m, st->to_dmesg,
247 width, st->start_address,
248 width, st->current_address);
250 delta = st->current_address - st->start_address;
251 while (!(delta & 1023) && unit[1]) {
255 pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ",
257 printk_prot(m, st->current_prot, st->level,
263 * We print markers for special areas of address space,
264 * such as the start of vmalloc space etc.
265 * This helps in the interpretation.
267 if (st->current_address >= st->marker[1].start_address) {
268 if (st->marker->max_lines &&
269 st->lines > st->marker->max_lines) {
270 unsigned long nskip =
271 st->lines - st->marker->max_lines;
272 pt_dump_seq_printf(m, st->to_dmesg,
273 "... %lu entr%s skipped ... \n",
275 nskip == 1 ? "y" : "ies");
279 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
283 st->start_address = st->current_address;
284 st->current_prot = new_prot;
289 static void walk_pte_level(struct seq_file *m, struct pg_state *st, pmd_t addr,
296 start = (pte_t *) pmd_page_vaddr(addr);
297 for (i = 0; i < PTRS_PER_PTE; i++) {
298 prot = pte_flags(*start);
299 st->current_address = normalize_addr(P + i * PTE_LEVEL_MULT);
300 note_page(m, st, __pgprot(prot), 4);
307 static void walk_pmd_level(struct seq_file *m, struct pg_state *st, pud_t addr,
314 start = (pmd_t *) pud_page_vaddr(addr);
315 for (i = 0; i < PTRS_PER_PMD; i++) {
316 st->current_address = normalize_addr(P + i * PMD_LEVEL_MULT);
317 if (!pmd_none(*start)) {
318 if (pmd_large(*start) || !pmd_present(*start)) {
319 prot = pmd_flags(*start);
320 note_page(m, st, __pgprot(prot), 3);
322 walk_pte_level(m, st, *start,
323 P + i * PMD_LEVEL_MULT);
326 note_page(m, st, __pgprot(0), 3);
332 #define walk_pmd_level(m,s,a,p) walk_pte_level(m,s,__pmd(pud_val(a)),p)
333 #define pud_large(a) pmd_large(__pmd(pud_val(a)))
334 #define pud_none(a) pmd_none(__pmd(pud_val(a)))
340 * This is an optimization for CONFIG_DEBUG_WX=y + CONFIG_KASAN=y
341 * KASAN fills page tables with the same values. Since there is no
342 * point in checking page table more than once we just skip repeated
343 * entries. This saves us dozens of seconds during boot.
345 static bool pud_already_checked(pud_t *prev_pud, pud_t *pud, bool checkwx)
347 return checkwx && prev_pud && (pud_val(*prev_pud) == pud_val(*pud));
350 static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
356 pud_t *prev_pud = NULL;
358 start = (pud_t *) pgd_page_vaddr(addr);
360 for (i = 0; i < PTRS_PER_PUD; i++) {
361 st->current_address = normalize_addr(P + i * PUD_LEVEL_MULT);
362 if (!pud_none(*start) &&
363 !pud_already_checked(prev_pud, start, st->check_wx)) {
364 if (pud_large(*start) || !pud_present(*start)) {
365 prot = pud_flags(*start);
366 note_page(m, st, __pgprot(prot), 2);
368 walk_pmd_level(m, st, *start,
369 P + i * PUD_LEVEL_MULT);
372 note_page(m, st, __pgprot(0), 2);
380 #define walk_pud_level(m,s,a,p) walk_pmd_level(m,s,__pud(pgd_val(a)),p)
381 #define pgd_large(a) pud_large(__pud(pgd_val(a)))
382 #define pgd_none(a) pud_none(__pud(pgd_val(a)))
385 static inline bool is_hypervisor_range(int idx)
389 * ffff800000000000 - ffff87ffffffffff is reserved for
392 return (idx >= pgd_index(__PAGE_OFFSET) - 16) &&
393 (idx < pgd_index(__PAGE_OFFSET));
399 static void ptdump_walk_pgd_level_core(struct seq_file *m, pgd_t *pgd,
403 pgd_t *start = (pgd_t *) &init_level4_pgt;
405 pgd_t *start = swapper_pg_dir;
409 struct pg_state st = {};
416 st.check_wx = checkwx;
420 for (i = 0; i < PTRS_PER_PGD; i++) {
421 st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
422 if (!pgd_none(*start) && !is_hypervisor_range(i)) {
423 if (pgd_large(*start) || !pgd_present(*start)) {
424 prot = pgd_flags(*start);
425 note_page(m, &st, __pgprot(prot), 1);
427 walk_pud_level(m, &st, *start,
431 note_page(m, &st, __pgprot(0), 1);
437 /* Flush out the last page */
438 st.current_address = normalize_addr(PTRS_PER_PGD*PGD_LEVEL_MULT);
439 note_page(m, &st, __pgprot(0), 0);
443 pr_info("x86/mm: Checked W+X mappings: FAILED, %lu W+X pages found.\n",
446 pr_info("x86/mm: Checked W+X mappings: passed, no W+X pages found.\n");
449 void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
451 ptdump_walk_pgd_level_core(m, pgd, false);
453 EXPORT_SYMBOL_GPL(ptdump_walk_pgd_level);
455 void ptdump_walk_pgd_level_checkwx(void)
457 ptdump_walk_pgd_level_core(NULL, NULL, true);
460 static int __init pt_dump_init(void)
463 * Various markers are not compile-time constants, so assign them
467 address_markers[LOW_KERNEL_NR].start_address = PAGE_OFFSET;
468 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
469 address_markers[VMEMMAP_START_NR].start_address = VMEMMAP_START;
472 address_markers[VMALLOC_START_NR].start_address = VMALLOC_START;
473 address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
474 # ifdef CONFIG_HIGHMEM
475 address_markers[PKMAP_BASE_NR].start_address = PKMAP_BASE;
477 address_markers[FIXADDR_START_NR].start_address = FIXADDR_START;
482 __initcall(pt_dump_init);