1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2016, Rashmica Gupta, IBM Corp.
5 * This traverses the kernel pagetables and dumps the
6 * information about the used sections of memory to
7 * /sys/kernel/debug/kernel_pagetables.
9 * Derived from the arm64 implementation:
10 * Copyright (c) 2014, The Linux Foundation, Laura Abbott.
11 * (C) Copyright 2008 Intel Corporation, Arjan van de Ven.
13 #include <linux/debugfs.h>
15 #include <linux/hugetlb.h>
18 #include <linux/highmem.h>
19 #include <linux/ptdump.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <asm/fixmap.h>
23 #include <linux/const.h>
25 #include <asm/hugetlb.h>
27 #include <mm/mmu_decl.h>
32 * To visualise what is happening,
34 * - PTRS_PER_P** = how many entries there are in the corresponding P**
35 * - P**_SHIFT = how many bits of the address we use to index into the
37 * - P**_SIZE is how much memory we can access through the table - not the
38 * size of the table itself.
39 * P**={PGD, PUD, PMD, PTE}
42 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
43 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
46 * In the case where there are only 3 levels, the PUD is folded into the
47 * PGD: every PUD has only one entry which points to the PMD.
49 * The page dumper groups page table entries of the same type into a single
50 * description. It uses pg_state to track the range information while
51 * iterating over the PTE entries. When the continuity is broken it then
52 * dumps out a description of the range - ie PTEs that are virtually contiguous
53 * with the same PTE flags are chunked together. This is to make it clear how
54 * different areas of the kernel virtual memory are used.
58 struct ptdump_state ptdump;
60 const struct addr_marker *marker;
61 unsigned long start_address;
62 unsigned long start_pa;
66 unsigned long wx_pages;
70 unsigned long start_address;
74 static struct addr_marker address_markers[] = {
75 { 0, "Start of kernel VM" },
77 { 0, "modules start" },
80 { 0, "vmalloc() Area" },
81 { 0, "vmalloc() End" },
83 { 0, "isa I/O start" },
85 { 0, "phb I/O start" },
87 { 0, "I/O remap start" },
88 { 0, "I/O remap end" },
89 { 0, "vmemmap start" },
91 { 0, "Early I/O remap start" },
92 { 0, "Early I/O remap end" },
94 { 0, "Highmem PTEs start" },
95 { 0, "Highmem PTEs end" },
97 { 0, "Fixmap start" },
101 { 0, "kasan shadow mem start" },
102 { 0, "kasan shadow mem end" },
107 static struct ptdump_range ptdump_range[] __ro_after_init = {
108 {TASK_SIZE_MAX, ~0UL},
112 #define pt_dump_seq_printf(m, fmt, args...) \
115 seq_printf(m, fmt, ##args); \
118 #define pt_dump_seq_putc(m, c) \
124 void pt_dump_size(struct seq_file *m, unsigned long size)
126 static const char units[] = "KMGTPE";
127 const char *unit = units;
129 /* Work out what appropriate unit to use */
130 while (!(size & 1023) && unit[1]) {
134 pt_dump_seq_printf(m, "%9lu%c ", size, *unit);
137 static void dump_flag_info(struct pg_state *st, const struct flag_info
138 *flag, u64 pte, int num)
142 for (i = 0; i < num; i++, flag++) {
143 const char *s = NULL;
146 /* flag not defined so don't check it */
149 /* Some 'flags' are actually values */
151 val = pte & flag->val;
153 val = val >> flag->shift;
154 pt_dump_seq_printf(st->seq, " %s:%llx", flag->set, val);
156 if ((pte & flag->mask) == flag->val)
161 pt_dump_seq_printf(st->seq, " %s", s);
163 st->current_flags &= ~flag->mask;
165 if (st->current_flags != 0)
166 pt_dump_seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
169 static void dump_addr(struct pg_state *st, unsigned long addr)
172 #define REG "0x%016lx"
174 #define REG "0x%08lx"
177 pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
178 pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa);
179 pt_dump_size(st->seq, (addr - st->start_address) >> 10);
182 static void note_prot_wx(struct pg_state *st, unsigned long addr)
184 pte_t pte = __pte(st->current_flags);
186 if (!IS_ENABLED(CONFIG_PPC_DEBUG_WX) || !st->check_wx)
189 if (!pte_write(pte) || !pte_exec(pte))
192 WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n",
193 (void *)st->start_address, (void *)st->start_address);
195 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
198 static void note_page_update_state(struct pg_state *st, unsigned long addr, int level, u64 val)
200 u64 flag = level >= 0 ? val & pg_level[level].mask : 0;
201 u64 pa = val & PTE_RPN_MASK;
204 st->current_flags = flag;
205 st->start_address = addr;
208 while (addr >= st->marker[1].start_address) {
210 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
214 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
216 u64 flag = level >= 0 ? val & pg_level[level].mask : 0;
217 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
219 /* At first no level is set */
220 if (st->level == -1) {
221 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
222 note_page_update_state(st, addr, level, val);
224 * Dump the section of virtual memory when:
225 * - the PTE flags from one entry to the next differs.
226 * - we change levels in the tree.
227 * - the address is in a different section of memory and is thus
228 * used for a different purpose, regardless of the flags.
230 } else if (flag != st->current_flags || level != st->level ||
231 addr >= st->marker[1].start_address) {
233 /* Check the PTE flags */
234 if (st->current_flags) {
235 note_prot_wx(st, addr);
238 /* Dump all the flags */
239 if (pg_level[st->level].flag)
240 dump_flag_info(st, pg_level[st->level].flag,
242 pg_level[st->level].num);
244 pt_dump_seq_putc(st->seq, '\n');
248 * Address indicates we have passed the end of the
249 * current section of virtual memory
251 note_page_update_state(st, addr, level, val);
255 static void populate_markers(void)
260 address_markers[i++].start_address = PAGE_OFFSET;
262 address_markers[i++].start_address = TASK_SIZE;
265 address_markers[i++].start_address = MODULES_VADDR;
266 address_markers[i++].start_address = MODULES_END;
268 address_markers[i++].start_address = VMALLOC_START;
269 address_markers[i++].start_address = VMALLOC_END;
271 address_markers[i++].start_address = ISA_IO_BASE;
272 address_markers[i++].start_address = ISA_IO_END;
273 address_markers[i++].start_address = PHB_IO_BASE;
274 address_markers[i++].start_address = PHB_IO_END;
275 address_markers[i++].start_address = IOREMAP_BASE;
276 address_markers[i++].start_address = IOREMAP_END;
277 /* What is the ifdef about? */
278 #ifdef CONFIG_PPC_BOOK3S_64
279 address_markers[i++].start_address = H_VMEMMAP_START;
281 address_markers[i++].start_address = VMEMMAP_BASE;
283 #else /* !CONFIG_PPC64 */
284 address_markers[i++].start_address = ioremap_bot;
285 address_markers[i++].start_address = IOREMAP_TOP;
286 #ifdef CONFIG_HIGHMEM
287 address_markers[i++].start_address = PKMAP_BASE;
288 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
290 address_markers[i++].start_address = FIXADDR_START;
291 address_markers[i++].start_address = FIXADDR_TOP;
293 address_markers[i++].start_address = KASAN_SHADOW_START;
294 address_markers[i++].start_address = KASAN_SHADOW_END;
296 #endif /* CONFIG_PPC64 */
299 static int ptdump_show(struct seq_file *m, void *v)
301 struct pg_state st = {
303 .marker = address_markers,
306 .note_page = note_page,
307 .range = ptdump_range,
311 /* Traverse kernel page tables */
312 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
316 DEFINE_SHOW_ATTRIBUTE(ptdump);
318 static void build_pgtable_complete_mask(void)
322 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
323 if (pg_level[i].flag)
324 for (j = 0; j < pg_level[i].num; j++)
325 pg_level[i].mask |= pg_level[i].flag[j].mask;
328 #ifdef CONFIG_DEBUG_WX
329 void ptdump_check_wx(void)
331 struct pg_state st = {
333 .marker = (struct addr_marker[]) {
340 .note_page = note_page,
341 .range = ptdump_range,
345 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
348 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
351 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
355 static int __init ptdump_init(void)
358 if (!radix_enabled())
359 ptdump_range[0].start = KERN_VIRT_START;
361 ptdump_range[0].start = PAGE_OFFSET;
365 build_pgtable_complete_mask();
367 if (IS_ENABLED(CONFIG_PTDUMP_DEBUGFS))
368 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
372 device_initcall(ptdump_init);