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 const struct mm_struct *mm;
52 unsigned long start_address;
56 unsigned long wx_pages;
57 unsigned long uxn_pages;
67 static const struct prot_bits pte_bits[] = {
109 .mask = PTE_TABLE_BIT,
110 .val = PTE_TABLE_BIT,
124 .mask = PTE_ATTRINDX_MASK,
125 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
126 .set = "DEVICE/nGnRnE",
128 .mask = PTE_ATTRINDX_MASK,
129 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
130 .set = "DEVICE/nGnRE",
132 .mask = PTE_ATTRINDX_MASK,
133 .val = PTE_ATTRINDX(MT_NORMAL_NC),
134 .set = "MEM/NORMAL-NC",
136 .mask = PTE_ATTRINDX_MASK,
137 .val = PTE_ATTRINDX(MT_NORMAL),
140 .mask = PTE_ATTRINDX_MASK,
141 .val = PTE_ATTRINDX(MT_NORMAL_TAGGED),
142 .set = "MEM/NORMAL-TAGGED",
147 const struct prot_bits *bits;
153 static struct pg_level pg_level[] __ro_after_init = {
157 .num = ARRAY_SIZE(pte_bits),
161 .num = ARRAY_SIZE(pte_bits),
165 .num = ARRAY_SIZE(pte_bits),
169 .num = ARRAY_SIZE(pte_bits),
173 .num = ARRAY_SIZE(pte_bits),
177 static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
182 for (i = 0; i < num; i++, bits++) {
185 if ((st->current_prot & bits->mask) == bits->val)
191 pt_dump_seq_printf(st->seq, " %s", s);
195 static void note_prot_uxn(struct pg_state *st, unsigned long addr)
200 if ((st->current_prot & PTE_UXN) == PTE_UXN)
203 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
204 (void *)st->start_address, (void *)st->start_address);
206 st->uxn_pages += (addr - st->start_address) / PAGE_SIZE;
209 static void note_prot_wx(struct pg_state *st, unsigned long addr)
213 if ((st->current_prot & PTE_RDONLY) == PTE_RDONLY)
215 if ((st->current_prot & PTE_PXN) == PTE_PXN)
218 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
219 (void *)st->start_address, (void *)st->start_address);
221 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
224 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level,
227 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
228 static const char units[] = "KMGTPE";
231 /* check if the current level has been folded dynamically */
232 if ((level == 1 && mm_p4d_folded(st->mm)) ||
233 (level == 2 && mm_pud_folded(st->mm)))
237 prot = val & pg_level[level].mask;
239 if (st->level == -1) {
241 st->current_prot = prot;
242 st->start_address = addr;
243 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
244 } else if (prot != st->current_prot || level != st->level ||
245 addr >= st->marker[1].start_address) {
246 const char *unit = units;
249 if (st->current_prot) {
250 note_prot_uxn(st, addr);
251 note_prot_wx(st, addr);
254 pt_dump_seq_printf(st->seq, "0x%016lx-0x%016lx ",
255 st->start_address, addr);
257 delta = (addr - st->start_address) >> 10;
258 while (!(delta & 1023) && unit[1]) {
262 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
263 pg_level[st->level].name);
264 if (st->current_prot && pg_level[st->level].bits)
265 dump_prot(st, pg_level[st->level].bits,
266 pg_level[st->level].num);
267 pt_dump_seq_puts(st->seq, "\n");
269 if (addr >= st->marker[1].start_address) {
271 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
274 st->start_address = addr;
275 st->current_prot = prot;
279 if (addr >= st->marker[1].start_address) {
281 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
286 void ptdump_walk(struct seq_file *s, struct ptdump_info *info)
288 unsigned long end = ~0UL;
291 if (info->base_addr < TASK_SIZE_64)
294 st = (struct pg_state){
296 .marker = info->markers,
300 .note_page = note_page,
301 .range = (struct ptdump_range[]){
302 {info->base_addr, end},
308 ptdump_walk_pgd(&st.ptdump, info->mm, NULL);
311 static void __init ptdump_initialize(void)
315 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
316 if (pg_level[i].bits)
317 for (j = 0; j < pg_level[i].num; j++)
318 pg_level[i].mask |= pg_level[i].bits[j].mask;
321 static struct ptdump_info kernel_ptdump_info __ro_after_init = {
325 bool ptdump_check_wx(void)
327 struct pg_state st = {
329 .marker = (struct addr_marker[]) {
336 .note_page = note_page,
337 .range = (struct ptdump_range[]) {
338 {_PAGE_OFFSET(vabits_actual), ~0UL},
344 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
346 if (st.wx_pages || st.uxn_pages) {
347 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
348 st.wx_pages, st.uxn_pages);
352 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
358 static int __init ptdump_init(void)
360 u64 page_offset = _PAGE_OFFSET(vabits_actual);
361 u64 vmemmap_start = (u64)virt_to_page((void *)page_offset);
362 struct addr_marker m[] = {
363 { PAGE_OFFSET, "Linear Mapping start" },
364 { PAGE_END, "Linear Mapping end" },
365 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
366 { KASAN_SHADOW_START, "Kasan shadow start" },
367 { KASAN_SHADOW_END, "Kasan shadow end" },
369 { MODULES_VADDR, "Modules start" },
370 { MODULES_END, "Modules end" },
371 { VMALLOC_START, "vmalloc() area" },
372 { VMALLOC_END, "vmalloc() end" },
373 { vmemmap_start, "vmemmap start" },
374 { VMEMMAP_END, "vmemmap end" },
375 { PCI_IO_START, "PCI I/O start" },
376 { PCI_IO_END, "PCI I/O end" },
377 { FIXADDR_TOT_START, "Fixmap start" },
378 { FIXADDR_TOP, "Fixmap end" },
381 static struct addr_marker address_markers[ARRAY_SIZE(m)] __ro_after_init;
383 kernel_ptdump_info.markers = memcpy(address_markers, m, sizeof(m));
384 kernel_ptdump_info.base_addr = page_offset;
387 ptdump_debugfs_register(&kernel_ptdump_info, "kernel_page_tables");
390 device_initcall(ptdump_init);