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 #define pt_dump_seq_printf(m, fmt, args...) \
32 seq_printf(m, fmt, ##args); \
35 #define pt_dump_seq_puts(m, fmt) \
41 static const struct ptdump_prot_bits pte_bits
[] = {
83 .mask
= PTE_TABLE_BIT
| PTE_VALID
,
98 .mask
= PTE_ATTRINDX_MASK
,
99 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
100 .set
= "DEVICE/nGnRnE",
102 .mask
= PTE_ATTRINDX_MASK
,
103 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
104 .set
= "DEVICE/nGnRE",
106 .mask
= PTE_ATTRINDX_MASK
,
107 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
108 .set
= "MEM/NORMAL-NC",
110 .mask
= PTE_ATTRINDX_MASK
,
111 .val
= PTE_ATTRINDX(MT_NORMAL
),
114 .mask
= PTE_ATTRINDX_MASK
,
115 .val
= PTE_ATTRINDX(MT_NORMAL_TAGGED
),
116 .set
= "MEM/NORMAL-TAGGED",
120 static struct ptdump_pg_level kernel_pg_levels
[] __ro_after_init
= {
124 .num
= ARRAY_SIZE(pte_bits
),
128 .num
= ARRAY_SIZE(pte_bits
),
132 .num
= ARRAY_SIZE(pte_bits
),
136 .num
= ARRAY_SIZE(pte_bits
),
140 .num
= ARRAY_SIZE(pte_bits
),
144 static void dump_prot(struct ptdump_pg_state
*st
, const struct ptdump_prot_bits
*bits
,
149 for (i
= 0; i
< num
; i
++, bits
++) {
152 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
158 pt_dump_seq_printf(st
->seq
, " %s", s
);
162 static void note_prot_uxn(struct ptdump_pg_state
*st
, unsigned long addr
)
167 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
170 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
171 (void *)st
->start_address
, (void *)st
->start_address
);
173 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
176 static void note_prot_wx(struct ptdump_pg_state
*st
, unsigned long addr
)
180 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
182 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
185 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
186 (void *)st
->start_address
, (void *)st
->start_address
);
188 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
191 void note_page(struct ptdump_state
*pt_st
, unsigned long addr
, int level
,
194 struct ptdump_pg_state
*st
= container_of(pt_st
, struct ptdump_pg_state
, ptdump
);
195 struct ptdump_pg_level
*pg_level
= st
->pg_level
;
196 static const char units
[] = "KMGTPE";
199 /* check if the current level has been folded dynamically */
200 if (st
->mm
&& ((level
== 1 && mm_p4d_folded(st
->mm
)) ||
201 (level
== 2 && mm_pud_folded(st
->mm
))))
205 prot
= val
& pg_level
[level
].mask
;
207 if (st
->level
== -1) {
209 st
->current_prot
= prot
;
210 st
->start_address
= addr
;
211 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
212 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
213 addr
>= st
->marker
[1].start_address
) {
214 const char *unit
= units
;
217 if (st
->current_prot
) {
218 note_prot_uxn(st
, addr
);
219 note_prot_wx(st
, addr
);
222 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
223 st
->start_address
, addr
);
225 delta
= (addr
- st
->start_address
) >> 10;
226 while (!(delta
& 1023) && unit
[1]) {
230 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
231 pg_level
[st
->level
].name
);
232 if (st
->current_prot
&& pg_level
[st
->level
].bits
)
233 dump_prot(st
, pg_level
[st
->level
].bits
,
234 pg_level
[st
->level
].num
);
235 pt_dump_seq_puts(st
->seq
, "\n");
237 if (addr
>= st
->marker
[1].start_address
) {
239 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
242 st
->start_address
= addr
;
243 st
->current_prot
= prot
;
247 if (addr
>= st
->marker
[1].start_address
) {
249 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
254 void ptdump_walk(struct seq_file
*s
, struct ptdump_info
*info
)
256 unsigned long end
= ~0UL;
257 struct ptdump_pg_state st
;
259 if (info
->base_addr
< TASK_SIZE_64
)
262 st
= (struct ptdump_pg_state
){
264 .marker
= info
->markers
,
266 .pg_level
= &kernel_pg_levels
[0],
269 .note_page
= note_page
,
270 .range
= (struct ptdump_range
[]){
271 {info
->base_addr
, end
},
277 ptdump_walk_pgd(&st
.ptdump
, info
->mm
, NULL
);
280 static void __init
ptdump_initialize(void)
284 for (i
= 0; i
< ARRAY_SIZE(kernel_pg_levels
); i
++)
285 if (kernel_pg_levels
[i
].bits
)
286 for (j
= 0; j
< kernel_pg_levels
[i
].num
; j
++)
287 kernel_pg_levels
[i
].mask
|= kernel_pg_levels
[i
].bits
[j
].mask
;
290 static struct ptdump_info kernel_ptdump_info __ro_after_init
= {
294 bool ptdump_check_wx(void)
296 struct ptdump_pg_state st
= {
298 .marker
= (struct addr_marker
[]) {
302 .pg_level
= &kernel_pg_levels
[0],
306 .note_page
= note_page
,
307 .range
= (struct ptdump_range
[]) {
308 {_PAGE_OFFSET(vabits_actual
), ~0UL},
314 ptdump_walk_pgd(&st
.ptdump
, &init_mm
, NULL
);
316 if (st
.wx_pages
|| st
.uxn_pages
) {
317 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
318 st
.wx_pages
, st
.uxn_pages
);
322 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
328 static int __init
ptdump_init(void)
330 u64 page_offset
= _PAGE_OFFSET(vabits_actual
);
331 u64 vmemmap_start
= (u64
)virt_to_page((void *)page_offset
);
332 struct addr_marker m
[] = {
333 { PAGE_OFFSET
, "Linear Mapping start" },
334 { PAGE_END
, "Linear Mapping end" },
335 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
336 { KASAN_SHADOW_START
, "Kasan shadow start" },
337 { KASAN_SHADOW_END
, "Kasan shadow end" },
339 { MODULES_VADDR
, "Modules start" },
340 { MODULES_END
, "Modules end" },
341 { VMALLOC_START
, "vmalloc() area" },
342 { VMALLOC_END
, "vmalloc() end" },
343 { vmemmap_start
, "vmemmap start" },
344 { VMEMMAP_END
, "vmemmap end" },
345 { PCI_IO_START
, "PCI I/O start" },
346 { PCI_IO_END
, "PCI I/O end" },
347 { FIXADDR_TOT_START
, "Fixmap start" },
348 { FIXADDR_TOP
, "Fixmap end" },
351 static struct addr_marker address_markers
[ARRAY_SIZE(m
)] __ro_after_init
;
353 kernel_ptdump_info
.markers
= memcpy(address_markers
, m
, sizeof(m
));
354 kernel_ptdump_info
.base_addr
= page_offset
;
357 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
360 device_initcall(ptdump_init
);