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.h>
26 #include <asm/pgtable-hwdef.h>
27 #include <asm/ptdump.h>
30 enum address_markers_idx
{
38 static struct addr_marker address_markers
[] = {
39 { PAGE_OFFSET
, "Linear Mapping start" },
40 { 0 /* PAGE_END */, "Linear Mapping end" },
42 { 0 /* KASAN_SHADOW_START */, "Kasan shadow start" },
43 { KASAN_SHADOW_END
, "Kasan shadow end" },
45 { MODULES_VADDR
, "Modules start" },
46 { MODULES_END
, "Modules end" },
47 { VMALLOC_START
, "vmalloc() area" },
48 { VMALLOC_END
, "vmalloc() end" },
49 { FIXADDR_START
, "Fixmap start" },
50 { FIXADDR_TOP
, "Fixmap end" },
51 { PCI_IO_START
, "PCI I/O start" },
52 { PCI_IO_END
, "PCI I/O end" },
53 #ifdef CONFIG_SPARSEMEM_VMEMMAP
54 { VMEMMAP_START
, "vmemmap start" },
55 { VMEMMAP_START
+ VMEMMAP_SIZE
, "vmemmap end" },
60 #define pt_dump_seq_printf(m, fmt, args...) \
63 seq_printf(m, fmt, ##args); \
66 #define pt_dump_seq_puts(m, fmt) \
73 * The page dumper groups page table entries of the same type into a single
74 * description. It uses pg_state to track the range information while
75 * iterating over the pte entries. When the continuity is broken it then
76 * dumps out a description of the range.
79 struct ptdump_state ptdump
;
81 const struct addr_marker
*marker
;
82 unsigned long start_address
;
86 unsigned long wx_pages
;
87 unsigned long uxn_pages
;
97 static const struct prot_bits pte_bits
[] = {
139 .mask
= PTE_TABLE_BIT
,
140 .val
= PTE_TABLE_BIT
,
149 .mask
= PTE_ATTRINDX_MASK
,
150 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
151 .set
= "DEVICE/nGnRnE",
153 .mask
= PTE_ATTRINDX_MASK
,
154 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
155 .set
= "DEVICE/nGnRE",
157 .mask
= PTE_ATTRINDX_MASK
,
158 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
161 .mask
= PTE_ATTRINDX_MASK
,
162 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
163 .set
= "MEM/NORMAL-NC",
165 .mask
= PTE_ATTRINDX_MASK
,
166 .val
= PTE_ATTRINDX(MT_NORMAL
),
172 const struct prot_bits
*bits
;
178 static struct pg_level pg_level
[] = {
182 .num
= ARRAY_SIZE(pte_bits
),
186 .num
= ARRAY_SIZE(pte_bits
),
188 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
190 .num
= ARRAY_SIZE(pte_bits
),
192 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
194 .num
= ARRAY_SIZE(pte_bits
),
198 .num
= ARRAY_SIZE(pte_bits
),
202 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
207 for (i
= 0; i
< num
; i
++, bits
++) {
210 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
216 pt_dump_seq_printf(st
->seq
, " %s", s
);
220 static void note_prot_uxn(struct pg_state
*st
, unsigned long addr
)
225 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
228 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
229 (void *)st
->start_address
, (void *)st
->start_address
);
231 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
234 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
238 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
240 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
243 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
244 (void *)st
->start_address
, (void *)st
->start_address
);
246 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
249 static void note_page(struct ptdump_state
*pt_st
, unsigned long addr
, int level
,
252 struct pg_state
*st
= container_of(pt_st
, struct pg_state
, ptdump
);
253 static const char units
[] = "KMGTPE";
257 prot
= val
& pg_level
[level
].mask
;
259 if (st
->level
== -1) {
261 st
->current_prot
= prot
;
262 st
->start_address
= addr
;
263 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
264 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
265 addr
>= st
->marker
[1].start_address
) {
266 const char *unit
= units
;
269 if (st
->current_prot
) {
270 note_prot_uxn(st
, addr
);
271 note_prot_wx(st
, addr
);
274 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
275 st
->start_address
, addr
);
277 delta
= (addr
- st
->start_address
) >> 10;
278 while (!(delta
& 1023) && unit
[1]) {
282 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
283 pg_level
[st
->level
].name
);
284 if (st
->current_prot
&& pg_level
[st
->level
].bits
)
285 dump_prot(st
, pg_level
[st
->level
].bits
,
286 pg_level
[st
->level
].num
);
287 pt_dump_seq_puts(st
->seq
, "\n");
289 if (addr
>= st
->marker
[1].start_address
) {
291 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
294 st
->start_address
= addr
;
295 st
->current_prot
= prot
;
299 if (addr
>= st
->marker
[1].start_address
) {
301 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
306 void ptdump_walk(struct seq_file
*s
, struct ptdump_info
*info
)
308 unsigned long end
= ~0UL;
311 if (info
->base_addr
< TASK_SIZE_64
)
314 st
= (struct pg_state
){
316 .marker
= info
->markers
,
318 .note_page
= note_page
,
319 .range
= (struct ptdump_range
[]){
320 {info
->base_addr
, end
},
326 ptdump_walk_pgd(&st
.ptdump
, info
->mm
, NULL
);
329 static void ptdump_initialize(void)
333 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
334 if (pg_level
[i
].bits
)
335 for (j
= 0; j
< pg_level
[i
].num
; j
++)
336 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
339 static struct ptdump_info kernel_ptdump_info
= {
341 .markers
= address_markers
,
342 .base_addr
= PAGE_OFFSET
,
345 void ptdump_check_wx(void)
347 struct pg_state st
= {
349 .marker
= (struct addr_marker
[]) {
356 .note_page
= note_page
,
357 .range
= (struct ptdump_range
[]) {
364 ptdump_walk_pgd(&st
.ptdump
, &init_mm
, NULL
);
366 if (st
.wx_pages
|| st
.uxn_pages
)
367 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
368 st
.wx_pages
, st
.uxn_pages
);
370 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
373 static int ptdump_init(void)
375 address_markers
[PAGE_END_NR
].start_address
= PAGE_END
;
377 address_markers
[KASAN_START_NR
].start_address
= KASAN_SHADOW_START
;
380 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
383 device_initcall(ptdump_init
);