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>
29 enum address_markers_idx
{
37 static struct addr_marker address_markers
[] = {
38 { PAGE_OFFSET
, "Linear Mapping start" },
39 { 0 /* PAGE_END */, "Linear Mapping end" },
41 { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" },
42 { KASAN_SHADOW_END
, "Kasan shadow end" },
44 { MODULES_VADDR
, "Modules start" },
45 { MODULES_END
, "Modules end" },
46 { VMALLOC_START
, "vmalloc() area" },
47 { VMALLOC_END
, "vmalloc() end" },
48 { FIXADDR_START
, "Fixmap start" },
49 { FIXADDR_TOP
, "Fixmap end" },
50 { PCI_IO_START
, "PCI I/O start" },
51 { PCI_IO_END
, "PCI I/O end" },
52 #ifdef CONFIG_SPARSEMEM_VMEMMAP
53 { VMEMMAP_START
, "vmemmap start" },
54 { VMEMMAP_START
+ VMEMMAP_SIZE
, "vmemmap end" },
59 #define pt_dump_seq_printf(m, fmt, args...) \
62 seq_printf(m, fmt, ##args); \
65 #define pt_dump_seq_puts(m, fmt) \
72 * The page dumper groups page table entries of the same type into a single
73 * description. It uses pg_state to track the range information while
74 * iterating over the pte entries. When the continuity is broken it then
75 * dumps out a description of the range.
79 const struct addr_marker
*marker
;
80 unsigned long start_address
;
84 unsigned long wx_pages
;
85 unsigned long uxn_pages
;
95 static const struct prot_bits pte_bits
[] = {
137 .mask
= PTE_TABLE_BIT
,
138 .val
= PTE_TABLE_BIT
,
146 .mask
= PTE_ATTRINDX_MASK
,
147 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
148 .set
= "DEVICE/nGnRnE",
150 .mask
= PTE_ATTRINDX_MASK
,
151 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
152 .set
= "DEVICE/nGnRE",
154 .mask
= PTE_ATTRINDX_MASK
,
155 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
158 .mask
= PTE_ATTRINDX_MASK
,
159 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
160 .set
= "MEM/NORMAL-NC",
162 .mask
= PTE_ATTRINDX_MASK
,
163 .val
= PTE_ATTRINDX(MT_NORMAL
),
169 const struct prot_bits
*bits
;
175 static struct pg_level pg_level
[] = {
180 .num
= ARRAY_SIZE(pte_bits
),
182 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
184 .num
= ARRAY_SIZE(pte_bits
),
186 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
188 .num
= ARRAY_SIZE(pte_bits
),
192 .num
= ARRAY_SIZE(pte_bits
),
196 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
201 for (i
= 0; i
< num
; i
++, bits
++) {
204 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
210 pt_dump_seq_printf(st
->seq
, " %s", s
);
214 static void note_prot_uxn(struct pg_state
*st
, unsigned long addr
)
219 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
222 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
223 (void *)st
->start_address
, (void *)st
->start_address
);
225 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
228 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
232 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
234 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
237 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
238 (void *)st
->start_address
, (void *)st
->start_address
);
240 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
243 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
,
246 static const char units
[] = "KMGTPE";
247 u64 prot
= val
& pg_level
[level
].mask
;
251 st
->current_prot
= prot
;
252 st
->start_address
= addr
;
253 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
254 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
255 addr
>= st
->marker
[1].start_address
) {
256 const char *unit
= units
;
259 if (st
->current_prot
) {
260 note_prot_uxn(st
, addr
);
261 note_prot_wx(st
, addr
);
262 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
263 st
->start_address
, addr
);
265 delta
= (addr
- st
->start_address
) >> 10;
266 while (!(delta
& 1023) && unit
[1]) {
270 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
271 pg_level
[st
->level
].name
);
272 if (pg_level
[st
->level
].bits
)
273 dump_prot(st
, pg_level
[st
->level
].bits
,
274 pg_level
[st
->level
].num
);
275 pt_dump_seq_puts(st
->seq
, "\n");
278 if (addr
>= st
->marker
[1].start_address
) {
280 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
283 st
->start_address
= addr
;
284 st
->current_prot
= prot
;
288 if (addr
>= st
->marker
[1].start_address
) {
290 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
295 static void walk_pte(struct pg_state
*st
, pmd_t
*pmdp
, unsigned long start
,
298 unsigned long addr
= start
;
299 pte_t
*ptep
= pte_offset_kernel(pmdp
, start
);
302 note_page(st
, addr
, 4, READ_ONCE(pte_val(*ptep
)));
303 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
306 static void walk_pmd(struct pg_state
*st
, pud_t
*pudp
, unsigned long start
,
309 unsigned long next
, addr
= start
;
310 pmd_t
*pmdp
= pmd_offset(pudp
, start
);
313 pmd_t pmd
= READ_ONCE(*pmdp
);
314 next
= pmd_addr_end(addr
, end
);
316 if (pmd_none(pmd
) || pmd_sect(pmd
)) {
317 note_page(st
, addr
, 3, pmd_val(pmd
));
319 BUG_ON(pmd_bad(pmd
));
320 walk_pte(st
, pmdp
, addr
, next
);
322 } while (pmdp
++, addr
= next
, addr
!= end
);
325 static void walk_pud(struct pg_state
*st
, pgd_t
*pgdp
, unsigned long start
,
328 unsigned long next
, addr
= start
;
329 pud_t
*pudp
= pud_offset(pgdp
, start
);
332 pud_t pud
= READ_ONCE(*pudp
);
333 next
= pud_addr_end(addr
, end
);
335 if (pud_none(pud
) || pud_sect(pud
)) {
336 note_page(st
, addr
, 2, pud_val(pud
));
338 BUG_ON(pud_bad(pud
));
339 walk_pmd(st
, pudp
, addr
, next
);
341 } while (pudp
++, addr
= next
, addr
!= end
);
344 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
347 unsigned long end
= (start
< TASK_SIZE_64
) ? TASK_SIZE_64
: 0;
348 unsigned long next
, addr
= start
;
349 pgd_t
*pgdp
= pgd_offset(mm
, start
);
352 pgd_t pgd
= READ_ONCE(*pgdp
);
353 next
= pgd_addr_end(addr
, end
);
356 note_page(st
, addr
, 1, pgd_val(pgd
));
358 BUG_ON(pgd_bad(pgd
));
359 walk_pud(st
, pgdp
, addr
, next
);
361 } while (pgdp
++, addr
= next
, addr
!= end
);
364 void ptdump_walk_pgd(struct seq_file
*m
, struct ptdump_info
*info
)
366 struct pg_state st
= {
368 .marker
= info
->markers
,
371 walk_pgd(&st
, info
->mm
, info
->base_addr
);
373 note_page(&st
, 0, 0, 0);
376 static void ptdump_initialize(void)
380 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
381 if (pg_level
[i
].bits
)
382 for (j
= 0; j
< pg_level
[i
].num
; j
++)
383 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
386 static struct ptdump_info kernel_ptdump_info
= {
388 .markers
= address_markers
,
389 .base_addr
= PAGE_OFFSET
,
392 void ptdump_check_wx(void)
394 struct pg_state st
= {
396 .marker
= (struct addr_marker
[]) {
403 walk_pgd(&st
, &init_mm
, PAGE_OFFSET
);
404 note_page(&st
, 0, 0, 0);
405 if (st
.wx_pages
|| st
.uxn_pages
)
406 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
407 st
.wx_pages
, st
.uxn_pages
);
409 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
412 static int ptdump_init(void)
414 address_markers
[PAGE_END_NR
].start_address
= PAGE_END
;
416 address_markers
[KASAN_START_NR
].start_address
= KASAN_SHADOW_START
;
419 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
422 device_initcall(ptdump_init
);