1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 * Debug helper to dump the current kernel pagetables of the system
5 * so that we can see what the various memory ranges are set to.
7 * Derived from x86 and arm implementation:
8 * (C) Copyright 2008 Intel Corporation
10 * Author: Arjan van de Ven <arjan@linux.intel.com>
12 #include <linux/debugfs.h>
13 #include <linux/errno.h>
16 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
21 #include <asm/fixmap.h>
22 #include <asm/kasan.h>
23 #include <asm/memory.h>
24 #include <asm/pgtable.h>
25 #include <asm/pgtable-hwdef.h>
26 #include <asm/ptdump.h>
28 static const struct addr_marker address_markers
[] = {
30 { KASAN_SHADOW_START
, "Kasan shadow start" },
31 { KASAN_SHADOW_END
, "Kasan shadow end" },
33 { MODULES_VADDR
, "Modules start" },
34 { MODULES_END
, "Modules end" },
35 { VMALLOC_START
, "vmalloc() area" },
36 { VMALLOC_END
, "vmalloc() end" },
37 { FIXADDR_START
, "Fixmap start" },
38 { FIXADDR_TOP
, "Fixmap end" },
39 { PCI_IO_START
, "PCI I/O start" },
40 { PCI_IO_END
, "PCI I/O end" },
41 #ifdef CONFIG_SPARSEMEM_VMEMMAP
42 { VMEMMAP_START
, "vmemmap start" },
43 { VMEMMAP_START
+ VMEMMAP_SIZE
, "vmemmap end" },
45 { PAGE_OFFSET
, "Linear mapping" },
49 #define pt_dump_seq_printf(m, fmt, args...) \
52 seq_printf(m, fmt, ##args); \
55 #define pt_dump_seq_puts(m, fmt) \
62 * The page dumper groups page table entries of the same type into a single
63 * description. It uses pg_state to track the range information while
64 * iterating over the pte entries. When the continuity is broken it then
65 * dumps out a description of the range.
69 const struct addr_marker
*marker
;
70 unsigned long start_address
;
74 unsigned long wx_pages
;
75 unsigned long uxn_pages
;
85 static const struct prot_bits pte_bits
[] = {
127 .mask
= PTE_TABLE_BIT
,
128 .val
= PTE_TABLE_BIT
,
136 .mask
= PTE_ATTRINDX_MASK
,
137 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
138 .set
= "DEVICE/nGnRnE",
140 .mask
= PTE_ATTRINDX_MASK
,
141 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
142 .set
= "DEVICE/nGnRE",
144 .mask
= PTE_ATTRINDX_MASK
,
145 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
148 .mask
= PTE_ATTRINDX_MASK
,
149 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
150 .set
= "MEM/NORMAL-NC",
152 .mask
= PTE_ATTRINDX_MASK
,
153 .val
= PTE_ATTRINDX(MT_NORMAL
),
159 const struct prot_bits
*bits
;
165 static struct pg_level pg_level
[] = {
170 .num
= ARRAY_SIZE(pte_bits
),
172 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
174 .num
= ARRAY_SIZE(pte_bits
),
176 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
178 .num
= ARRAY_SIZE(pte_bits
),
182 .num
= ARRAY_SIZE(pte_bits
),
186 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
191 for (i
= 0; i
< num
; i
++, bits
++) {
194 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
200 pt_dump_seq_printf(st
->seq
, " %s", s
);
204 static void note_prot_uxn(struct pg_state
*st
, unsigned long addr
)
209 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
212 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
213 (void *)st
->start_address
, (void *)st
->start_address
);
215 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
218 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
222 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
224 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
227 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
228 (void *)st
->start_address
, (void *)st
->start_address
);
230 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
233 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
,
236 static const char units
[] = "KMGTPE";
237 u64 prot
= val
& pg_level
[level
].mask
;
241 st
->current_prot
= prot
;
242 st
->start_address
= addr
;
243 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
244 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
245 addr
>= st
->marker
[1].start_address
) {
246 const char *unit
= units
;
249 if (st
->current_prot
) {
250 note_prot_uxn(st
, addr
);
251 note_prot_wx(st
, addr
);
252 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
253 st
->start_address
, addr
);
255 delta
= (addr
- st
->start_address
) >> 10;
256 while (!(delta
& 1023) && unit
[1]) {
260 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
261 pg_level
[st
->level
].name
);
262 if (pg_level
[st
->level
].bits
)
263 dump_prot(st
, pg_level
[st
->level
].bits
,
264 pg_level
[st
->level
].num
);
265 pt_dump_seq_puts(st
->seq
, "\n");
268 if (addr
>= st
->marker
[1].start_address
) {
270 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
273 st
->start_address
= addr
;
274 st
->current_prot
= prot
;
278 if (addr
>= st
->marker
[1].start_address
) {
280 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
285 static void walk_pte(struct pg_state
*st
, pmd_t
*pmdp
, unsigned long start
,
288 unsigned long addr
= start
;
289 pte_t
*ptep
= pte_offset_kernel(pmdp
, start
);
292 note_page(st
, addr
, 4, READ_ONCE(pte_val(*ptep
)));
293 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
296 static void walk_pmd(struct pg_state
*st
, pud_t
*pudp
, unsigned long start
,
299 unsigned long next
, addr
= start
;
300 pmd_t
*pmdp
= pmd_offset(pudp
, start
);
303 pmd_t pmd
= READ_ONCE(*pmdp
);
304 next
= pmd_addr_end(addr
, end
);
306 if (pmd_none(pmd
) || pmd_sect(pmd
)) {
307 note_page(st
, addr
, 3, pmd_val(pmd
));
309 BUG_ON(pmd_bad(pmd
));
310 walk_pte(st
, pmdp
, addr
, next
);
312 } while (pmdp
++, addr
= next
, addr
!= end
);
315 static void walk_pud(struct pg_state
*st
, pgd_t
*pgdp
, unsigned long start
,
318 unsigned long next
, addr
= start
;
319 pud_t
*pudp
= pud_offset(pgdp
, start
);
322 pud_t pud
= READ_ONCE(*pudp
);
323 next
= pud_addr_end(addr
, end
);
325 if (pud_none(pud
) || pud_sect(pud
)) {
326 note_page(st
, addr
, 2, pud_val(pud
));
328 BUG_ON(pud_bad(pud
));
329 walk_pmd(st
, pudp
, addr
, next
);
331 } while (pudp
++, addr
= next
, addr
!= end
);
334 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
337 unsigned long end
= (start
< TASK_SIZE_64
) ? TASK_SIZE_64
: 0;
338 unsigned long next
, addr
= start
;
339 pgd_t
*pgdp
= pgd_offset(mm
, start
);
342 pgd_t pgd
= READ_ONCE(*pgdp
);
343 next
= pgd_addr_end(addr
, end
);
346 note_page(st
, addr
, 1, pgd_val(pgd
));
348 BUG_ON(pgd_bad(pgd
));
349 walk_pud(st
, pgdp
, addr
, next
);
351 } while (pgdp
++, addr
= next
, addr
!= end
);
354 void ptdump_walk_pgd(struct seq_file
*m
, struct ptdump_info
*info
)
356 struct pg_state st
= {
358 .marker
= info
->markers
,
361 walk_pgd(&st
, info
->mm
, info
->base_addr
);
363 note_page(&st
, 0, 0, 0);
366 static void ptdump_initialize(void)
370 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
371 if (pg_level
[i
].bits
)
372 for (j
= 0; j
< pg_level
[i
].num
; j
++)
373 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
376 static struct ptdump_info kernel_ptdump_info
= {
378 .markers
= address_markers
,
379 .base_addr
= VA_START
,
382 void ptdump_check_wx(void)
384 struct pg_state st
= {
386 .marker
= (struct addr_marker
[]) {
393 walk_pgd(&st
, &init_mm
, VA_START
);
394 note_page(&st
, 0, 0, 0);
395 if (st
.wx_pages
|| st
.uxn_pages
)
396 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
397 st
.wx_pages
, st
.uxn_pages
);
399 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
402 static int ptdump_init(void)
405 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
408 device_initcall(ptdump_init
);