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 { VMEMMAP_START, "vmemmap start" },
55 { 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.
78 struct ptdump_state ptdump;
80 const struct addr_marker *marker;
81 unsigned long start_address;
85 unsigned long wx_pages;
86 unsigned long uxn_pages;
96 static const struct prot_bits pte_bits[] = {
138 .mask = PTE_TABLE_BIT,
139 .val = PTE_TABLE_BIT,
153 .mask = PTE_ATTRINDX_MASK,
154 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
155 .set = "DEVICE/nGnRnE",
157 .mask = PTE_ATTRINDX_MASK,
158 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
159 .set = "DEVICE/nGnRE",
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),
169 .mask = PTE_ATTRINDX_MASK,
170 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
171 .set = "MEM/NORMAL-TAGGED",
176 const struct prot_bits *bits;
182 static struct pg_level pg_level[] = {
186 .num = ARRAY_SIZE(pte_bits),
190 .num = ARRAY_SIZE(pte_bits),
192 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
194 .num = ARRAY_SIZE(pte_bits),
196 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
198 .num = ARRAY_SIZE(pte_bits),
202 .num = ARRAY_SIZE(pte_bits),
206 static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
211 for (i = 0; i < num; i++, bits++) {
214 if ((st->current_prot & bits->mask) == bits->val)
220 pt_dump_seq_printf(st->seq, " %s", s);
224 static void note_prot_uxn(struct pg_state *st, unsigned long addr)
229 if ((st->current_prot & PTE_UXN) == PTE_UXN)
232 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
233 (void *)st->start_address, (void *)st->start_address);
235 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
238 static void note_prot_wx(struct pg_state *st, unsigned long addr)
242 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
244 if ((st->current_prot & PTE_PXN) == PTE_PXN)
247 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
248 (void *)st->start_address, (void *)st->start_address);
250 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
253 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
256 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
257 static const char units[] = "KMGTPE";
261 prot = val & pg_level[level].mask;
263 if (st->level == -1) {
265 st->current_prot = prot;
266 st->start_address = addr;
267 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
268 } else if (prot != st->current_prot || level != st->level ||
269 addr >= st->marker[1].start_address) {
270 const char *unit = units;
273 if (st->current_prot) {
274 note_prot_uxn(st, addr);
275 note_prot_wx(st, addr);
278 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
279 st->start_address, addr);
281 delta = (addr - st->start_address) >> 10;
282 while (!(delta & 1023) && unit[1]) {
286 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
287 pg_level[st->level].name);
288 if (st->current_prot && pg_level[st->level].bits)
289 dump_prot(st, pg_level[st->level].bits,
290 pg_level[st->level].num);
291 pt_dump_seq_puts(st->seq, "\n");
293 if (addr >= st->marker[1].start_address) {
295 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
298 st->start_address = addr;
299 st->current_prot = prot;
303 if (addr >= st->marker[1].start_address) {
305 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
310 void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
312 unsigned long end = ~0UL;
315 if (info->base_addr < TASK_SIZE_64)
318 st = (struct pg_state){
320 .marker = info->markers,
323 .note_page = note_page,
324 .range = (struct ptdump_range[]){
325 {info->base_addr, end},
331 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
334 static void __init ptdump_initialize(void)
338 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
339 if (pg_level[i].bits)
340 for (j = 0; j < pg_level[i].num; j++)
341 pg_level[i].mask |= pg_level[i].bits[j].mask;
344 static struct ptdump_info kernel_ptdump_info = {
346 .markers = address_markers,
347 .base_addr = PAGE_OFFSET,
350 void ptdump_check_wx(void)
352 struct pg_state st = {
354 .marker = (struct addr_marker[]) {
361 .note_page = note_page,
362 .range = (struct ptdump_range[]) {
369 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
371 if (st.wx_pages || st.uxn_pages)
372 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
373 st.wx_pages, st.uxn_pages);
375 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
378 static int __init ptdump_init(void)
380 address_markers[PAGE_END_NR].start_address = PAGE_END;
381 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
382 address_markers[KASAN_START_NR].start_address = KASAN_SHADOW_START;
385 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
388 device_initcall(ptdump_init);