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/ptdump.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
22 #include <asm/fixmap.h>
23 #include <asm/kasan.h>
24 #include <asm/memory.h>
25 #include <asm/pgtable-hwdef.h>
26 #include <asm/ptdump.h>
29 enum address_markers_idx
{
32 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
37 static struct addr_marker address_markers
[] = {
38 { PAGE_OFFSET
, "Linear Mapping start" },
39 { 0 /* PAGE_END */, "Linear Mapping end" },
40 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
41 { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" },
42 { KASAN_SHADOW_END
, "Kasan shadow end" },
44 { BPF_JIT_REGION_START
, "BPF start" },
45 { BPF_JIT_REGION_END
, "BPF end" },
46 { MODULES_VADDR
, "Modules start" },
47 { MODULES_END
, "Modules end" },
48 { VMALLOC_START
, "vmalloc() area" },
49 { VMALLOC_END
, "vmalloc() end" },
50 { FIXADDR_START
, "Fixmap start" },
51 { FIXADDR_TOP
, "Fixmap end" },
52 { PCI_IO_START
, "PCI I/O start" },
53 { PCI_IO_END
, "PCI I/O end" },
54 #ifdef CONFIG_SPARSEMEM_VMEMMAP
55 { VMEMMAP_START
, "vmemmap start" },
56 { VMEMMAP_START
+ VMEMMAP_SIZE
, "vmemmap end" },
61 #define pt_dump_seq_printf(m, fmt, args...) \
64 seq_printf(m, fmt, ##args); \
67 #define pt_dump_seq_puts(m, fmt) \
74 * The page dumper groups page table entries of the same type into a single
75 * description. It uses pg_state to track the range information while
76 * iterating over the pte entries. When the continuity is broken it then
77 * dumps out a description of the range.
80 struct ptdump_state ptdump
;
82 const struct addr_marker
*marker
;
83 unsigned long start_address
;
87 unsigned long wx_pages
;
88 unsigned long uxn_pages
;
98 static const struct prot_bits pte_bits
[] = {
140 .mask
= PTE_TABLE_BIT
,
141 .val
= PTE_TABLE_BIT
,
155 .mask
= PTE_ATTRINDX_MASK
,
156 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
157 .set
= "DEVICE/nGnRnE",
159 .mask
= PTE_ATTRINDX_MASK
,
160 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
161 .set
= "DEVICE/nGnRE",
163 .mask
= PTE_ATTRINDX_MASK
,
164 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
167 .mask
= PTE_ATTRINDX_MASK
,
168 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
169 .set
= "MEM/NORMAL-NC",
171 .mask
= PTE_ATTRINDX_MASK
,
172 .val
= PTE_ATTRINDX(MT_NORMAL
),
175 .mask
= PTE_ATTRINDX_MASK
,
176 .val
= PTE_ATTRINDX(MT_NORMAL_TAGGED
),
177 .set
= "MEM/NORMAL-TAGGED",
182 const struct prot_bits
*bits
;
188 static struct pg_level pg_level
[] = {
192 .num
= ARRAY_SIZE(pte_bits
),
196 .num
= ARRAY_SIZE(pte_bits
),
198 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
200 .num
= ARRAY_SIZE(pte_bits
),
202 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
204 .num
= ARRAY_SIZE(pte_bits
),
208 .num
= ARRAY_SIZE(pte_bits
),
212 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
217 for (i
= 0; i
< num
; i
++, bits
++) {
220 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
226 pt_dump_seq_printf(st
->seq
, " %s", s
);
230 static void note_prot_uxn(struct pg_state
*st
, unsigned long addr
)
235 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
238 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
239 (void *)st
->start_address
, (void *)st
->start_address
);
241 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
244 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
248 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
250 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
253 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
254 (void *)st
->start_address
, (void *)st
->start_address
);
256 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
259 static void note_page(struct ptdump_state
*pt_st
, unsigned long addr
, int level
,
262 struct pg_state
*st
= container_of(pt_st
, struct pg_state
, ptdump
);
263 static const char units
[] = "KMGTPE";
267 prot
= val
& pg_level
[level
].mask
;
269 if (st
->level
== -1) {
271 st
->current_prot
= prot
;
272 st
->start_address
= addr
;
273 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
274 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
275 addr
>= st
->marker
[1].start_address
) {
276 const char *unit
= units
;
279 if (st
->current_prot
) {
280 note_prot_uxn(st
, addr
);
281 note_prot_wx(st
, addr
);
284 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
285 st
->start_address
, addr
);
287 delta
= (addr
- st
->start_address
) >> 10;
288 while (!(delta
& 1023) && unit
[1]) {
292 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
293 pg_level
[st
->level
].name
);
294 if (st
->current_prot
&& pg_level
[st
->level
].bits
)
295 dump_prot(st
, pg_level
[st
->level
].bits
,
296 pg_level
[st
->level
].num
);
297 pt_dump_seq_puts(st
->seq
, "\n");
299 if (addr
>= st
->marker
[1].start_address
) {
301 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
304 st
->start_address
= addr
;
305 st
->current_prot
= prot
;
309 if (addr
>= st
->marker
[1].start_address
) {
311 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
316 void ptdump_walk(struct seq_file
*s
, struct ptdump_info
*info
)
318 unsigned long end
= ~0UL;
321 if (info
->base_addr
< TASK_SIZE_64
)
324 st
= (struct pg_state
){
326 .marker
= info
->markers
,
328 .note_page
= note_page
,
329 .range
= (struct ptdump_range
[]){
330 {info
->base_addr
, end
},
336 ptdump_walk_pgd(&st
.ptdump
, info
->mm
, NULL
);
339 static void ptdump_initialize(void)
343 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
344 if (pg_level
[i
].bits
)
345 for (j
= 0; j
< pg_level
[i
].num
; j
++)
346 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
349 static struct ptdump_info kernel_ptdump_info
= {
351 .markers
= address_markers
,
352 .base_addr
= PAGE_OFFSET
,
355 void ptdump_check_wx(void)
357 struct pg_state st
= {
359 .marker
= (struct addr_marker
[]) {
366 .note_page
= note_page
,
367 .range
= (struct ptdump_range
[]) {
374 ptdump_walk_pgd(&st
.ptdump
, &init_mm
, NULL
);
376 if (st
.wx_pages
|| st
.uxn_pages
)
377 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
378 st
.wx_pages
, st
.uxn_pages
);
380 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
383 static int ptdump_init(void)
385 address_markers
[PAGE_END_NR
].start_address
= PAGE_END
;
386 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
387 address_markers
[KASAN_START_NR
].start_address
= KASAN_SHADOW_START
;
390 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
393 device_initcall(ptdump_init
);