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) \
42 * The page dumper groups page table entries of the same type into a single
43 * description. It uses pg_state to track the range information while
44 * iterating over the pte entries. When the continuity is broken it then
45 * dumps out a description of the range.
48 struct ptdump_state ptdump;
50 const struct addr_marker *marker;
51 unsigned long start_address;
55 unsigned long wx_pages;
56 unsigned long uxn_pages;
66 static const struct prot_bits pte_bits[] = {
108 .mask = PTE_TABLE_BIT,
109 .val = PTE_TABLE_BIT,
123 .mask = PTE_ATTRINDX_MASK,
124 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
125 .set = "DEVICE/nGnRnE",
127 .mask = PTE_ATTRINDX_MASK,
128 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
129 .set = "DEVICE/nGnRE",
131 .mask = PTE_ATTRINDX_MASK,
132 .val = PTE_ATTRINDX(MT_NORMAL_NC),
133 .set = "MEM/NORMAL-NC",
135 .mask = PTE_ATTRINDX_MASK,
136 .val = PTE_ATTRINDX(MT_NORMAL),
139 .mask = PTE_ATTRINDX_MASK,
140 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
141 .set = "MEM/NORMAL-TAGGED",
146 const struct prot_bits *bits;
152 static struct pg_level pg_level[] = {
156 .num = ARRAY_SIZE(pte_bits),
160 .num = ARRAY_SIZE(pte_bits),
162 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
164 .num = ARRAY_SIZE(pte_bits),
166 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
168 .num = ARRAY_SIZE(pte_bits),
172 .num = ARRAY_SIZE(pte_bits),
176 static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
181 for (i = 0; i < num; i++, bits++) {
184 if ((st->current_prot & bits->mask) == bits->val)
190 pt_dump_seq_printf(st->seq, " %s", s);
194 static void note_prot_uxn(struct pg_state *st, unsigned long addr)
199 if ((st->current_prot & PTE_UXN) == PTE_UXN)
202 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
203 (void *)st->start_address, (void *)st->start_address);
205 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
208 static void note_prot_wx(struct pg_state *st, unsigned long addr)
212 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
214 if ((st->current_prot & PTE_PXN) == PTE_PXN)
217 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
218 (void *)st->start_address, (void *)st->start_address);
220 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
223 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
226 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
227 static const char units[] = "KMGTPE";
231 prot = val & pg_level[level].mask;
233 if (st->level == -1) {
235 st->current_prot = prot;
236 st->start_address = addr;
237 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
238 } else if (prot != st->current_prot || level != st->level ||
239 addr >= st->marker[1].start_address) {
240 const char *unit = units;
243 if (st->current_prot) {
244 note_prot_uxn(st, addr);
245 note_prot_wx(st, addr);
248 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
249 st->start_address, addr);
251 delta = (addr - st->start_address) >> 10;
252 while (!(delta & 1023) && unit[1]) {
256 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
257 pg_level[st->level].name);
258 if (st->current_prot && pg_level[st->level].bits)
259 dump_prot(st, pg_level[st->level].bits,
260 pg_level[st->level].num);
261 pt_dump_seq_puts(st->seq, "\n");
263 if (addr >= st->marker[1].start_address) {
265 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
268 st->start_address = addr;
269 st->current_prot = prot;
273 if (addr >= st->marker[1].start_address) {
275 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
280 void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
282 unsigned long end = ~0UL;
285 if (info->base_addr < TASK_SIZE_64)
288 st = (struct pg_state){
290 .marker = info->markers,
293 .note_page = note_page,
294 .range = (struct ptdump_range[]){
295 {info->base_addr, end},
301 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
304 static void __init ptdump_initialize(void)
308 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
309 if (pg_level[i].bits)
310 for (j = 0; j < pg_level[i].num; j++)
311 pg_level[i].mask |= pg_level[i].bits[j].mask;
314 static struct ptdump_info kernel_ptdump_info __ro_after_init = {
316 .base_addr = PAGE_OFFSET,
319 void ptdump_check_wx(void)
321 struct pg_state st = {
323 .marker = (struct addr_marker[]) {
330 .note_page = note_page,
331 .range = (struct ptdump_range[]) {
338 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
340 if (st.wx_pages || st.uxn_pages)
341 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
342 st.wx_pages, st.uxn_pages);
344 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
347 static int __init ptdump_init(void)
349 u64 page_offset = _PAGE_OFFSET(vabits_actual);
350 u64 vmemmap_start = (u64)virt_to_page((void *)page_offset);
351 struct addr_marker m[] = {
352 { PAGE_OFFSET, "Linear Mapping start" },
353 { PAGE_END, "Linear Mapping end" },
354 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
355 { KASAN_SHADOW_START, "Kasan shadow start" },
356 { KASAN_SHADOW_END, "Kasan shadow end" },
358 { MODULES_VADDR, "Modules start" },
359 { MODULES_END, "Modules end" },
360 { VMALLOC_START, "vmalloc() area" },
361 { VMALLOC_END, "vmalloc() end" },
362 { vmemmap_start, "vmemmap start" },
363 { VMEMMAP_END, "vmemmap end" },
364 { PCI_IO_START, "PCI I/O start" },
365 { PCI_IO_END, "PCI I/O end" },
366 { FIXADDR_TOT_START, "Fixmap start" },
367 { FIXADDR_TOP, "Fixmap end" },
370 static struct addr_marker address_markers[ARRAY_SIZE(m)] __ro_after_init;
372 kernel_ptdump_info.markers = memcpy(address_markers, m, sizeof(m));
375 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
378 device_initcall(ptdump_init);