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/sched.h>
20 #include <linux/seq_file.h>
21 #include <asm/fixmap.h>
22 #include <asm/pgtable.h>
23 #include <linux/const.h>
25 #include <asm/pgalloc.h>
30 #define KERN_VIRT_START PAGE_OFFSET
34 * To visualise what is happening,
36 * - PTRS_PER_P** = how many entries there are in the corresponding P**
37 * - P**_SHIFT = how many bits of the address we use to index into the
39 * - P**_SIZE is how much memory we can access through the table - not the
40 * size of the table itself.
41 * P**={PGD, PUD, PMD, PTE}
44 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
45 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
48 * In the case where there are only 3 levels, the PUD is folded into the
49 * PGD: every PUD has only one entry which points to the PMD.
51 * The page dumper groups page table entries of the same type into a single
52 * description. It uses pg_state to track the range information while
53 * iterating over the PTE entries. When the continuity is broken it then
54 * dumps out a description of the range - ie PTEs that are virtually contiguous
55 * with the same PTE flags are chunked together. This is to make it clear how
56 * different areas of the kernel virtual memory are used.
61 const struct addr_marker *marker;
62 unsigned long start_address;
63 unsigned long start_pa;
64 unsigned long last_pa;
68 unsigned long wx_pages;
72 unsigned long start_address;
76 static struct addr_marker address_markers[] = {
77 { 0, "Start of kernel VM" },
78 { 0, "vmalloc() Area" },
79 { 0, "vmalloc() End" },
81 { 0, "isa I/O start" },
83 { 0, "phb I/O start" },
85 { 0, "I/O remap start" },
86 { 0, "I/O remap end" },
87 { 0, "vmemmap start" },
89 { 0, "Early I/O remap start" },
90 { 0, "Early I/O remap end" },
91 #ifdef CONFIG_NOT_COHERENT_CACHE
92 { 0, "Consistent mem start" },
93 { 0, "Consistent mem end" },
96 { 0, "Highmem PTEs start" },
97 { 0, "Highmem PTEs end" },
99 { 0, "Fixmap start" },
103 { 0, "kasan shadow mem start" },
104 { 0, "kasan shadow mem end" },
109 #define pt_dump_seq_printf(m, fmt, args...) \
112 seq_printf(m, fmt, ##args); \
115 #define pt_dump_seq_putc(m, c) \
121 static void dump_flag_info(struct pg_state *st, const struct flag_info
122 *flag, u64 pte, int num)
126 for (i = 0; i < num; i++, flag++) {
127 const char *s = NULL;
130 /* flag not defined so don't check it */
133 /* Some 'flags' are actually values */
135 val = pte & flag->val;
137 val = val >> flag->shift;
138 pt_dump_seq_printf(st->seq, " %s:%llx", flag->set, val);
140 if ((pte & flag->mask) == flag->val)
145 pt_dump_seq_printf(st->seq, " %s", s);
147 st->current_flags &= ~flag->mask;
149 if (st->current_flags != 0)
150 pt_dump_seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
153 static void dump_addr(struct pg_state *st, unsigned long addr)
155 static const char units[] = "KMGTPE";
156 const char *unit = units;
160 #define REG "0x%016lx"
162 #define REG "0x%08lx"
165 pt_dump_seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
166 if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) {
167 pt_dump_seq_printf(st->seq, "[" REG "]", st->start_pa);
168 delta = PAGE_SIZE >> 10;
170 pt_dump_seq_printf(st->seq, " " REG " ", st->start_pa);
171 delta = (addr - st->start_address) >> 10;
173 /* Work out what appropriate unit to use */
174 while (!(delta & 1023) && unit[1]) {
178 pt_dump_seq_printf(st->seq, "%9lu%c", delta, *unit);
182 static void note_prot_wx(struct pg_state *st, unsigned long addr)
187 if (!((st->current_flags & pgprot_val(PAGE_KERNEL_X)) == pgprot_val(PAGE_KERNEL_X)))
190 WARN_ONCE(1, "powerpc/mm: Found insecure W+X mapping at address %p/%pS\n",
191 (void *)st->start_address, (void *)st->start_address);
193 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
196 static void note_page(struct pg_state *st, unsigned long addr,
197 unsigned int level, u64 val)
199 u64 flag = val & pg_level[level].mask;
200 u64 pa = val & PTE_RPN_MASK;
202 /* At first no level is set */
205 st->current_flags = flag;
206 st->start_address = addr;
209 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
211 * Dump the section of virtual memory when:
212 * - the PTE flags from one entry to the next differs.
213 * - we change levels in the tree.
214 * - the address is in a different section of memory and is thus
215 * used for a different purpose, regardless of the flags.
216 * - the pa of this page is not adjacent to the last inspected page
218 } else if (flag != st->current_flags || level != st->level ||
219 addr >= st->marker[1].start_address ||
220 (pa != st->last_pa + PAGE_SIZE &&
221 (pa != st->start_pa || st->start_pa != st->last_pa))) {
223 /* Check the PTE flags */
224 if (st->current_flags) {
225 note_prot_wx(st, addr);
228 /* Dump all the flags */
229 if (pg_level[st->level].flag)
230 dump_flag_info(st, pg_level[st->level].flag,
232 pg_level[st->level].num);
234 pt_dump_seq_putc(st->seq, '\n');
238 * Address indicates we have passed the end of the
239 * current section of virtual memory
241 while (addr >= st->marker[1].start_address) {
243 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
245 st->start_address = addr;
248 st->current_flags = flag;
255 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
257 pte_t *pte = pte_offset_kernel(pmd, 0);
261 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
262 addr = start + i * PAGE_SIZE;
263 note_page(st, addr, 4, pte_val(*pte));
268 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
270 pmd_t *pmd = pmd_offset(pud, 0);
274 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
275 addr = start + i * PMD_SIZE;
276 if (!pmd_none(*pmd) && !pmd_is_leaf(*pmd))
278 walk_pte(st, pmd, addr);
280 note_page(st, addr, 3, pmd_val(*pmd));
284 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
286 pud_t *pud = pud_offset(pgd, 0);
290 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
291 addr = start + i * PUD_SIZE;
292 if (!pud_none(*pud) && !pud_is_leaf(*pud))
294 walk_pmd(st, pud, addr);
296 note_page(st, addr, 2, pud_val(*pud));
300 static void walk_pagetables(struct pg_state *st)
302 pgd_t *pgd = pgd_offset_k(0UL);
306 addr = st->start_address;
309 * Traverse the linux pagetable structure and dump pages that are in
310 * the hash pagetable.
312 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
313 if (!pgd_none(*pgd) && !pgd_is_leaf(*pgd))
315 walk_pud(st, pgd, addr);
317 note_page(st, addr, 1, pgd_val(*pgd));
321 static void populate_markers(void)
325 address_markers[i++].start_address = PAGE_OFFSET;
326 address_markers[i++].start_address = VMALLOC_START;
327 address_markers[i++].start_address = VMALLOC_END;
329 address_markers[i++].start_address = ISA_IO_BASE;
330 address_markers[i++].start_address = ISA_IO_END;
331 address_markers[i++].start_address = PHB_IO_BASE;
332 address_markers[i++].start_address = PHB_IO_END;
333 address_markers[i++].start_address = IOREMAP_BASE;
334 address_markers[i++].start_address = IOREMAP_END;
335 /* What is the ifdef about? */
336 #ifdef CONFIG_PPC_BOOK3S_64
337 address_markers[i++].start_address = H_VMEMMAP_START;
339 address_markers[i++].start_address = VMEMMAP_BASE;
341 #else /* !CONFIG_PPC64 */
342 address_markers[i++].start_address = ioremap_bot;
343 address_markers[i++].start_address = IOREMAP_TOP;
344 #ifdef CONFIG_NOT_COHERENT_CACHE
345 address_markers[i++].start_address = IOREMAP_TOP;
346 address_markers[i++].start_address = IOREMAP_TOP +
347 CONFIG_CONSISTENT_SIZE;
349 #ifdef CONFIG_HIGHMEM
350 address_markers[i++].start_address = PKMAP_BASE;
351 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
353 address_markers[i++].start_address = FIXADDR_START;
354 address_markers[i++].start_address = FIXADDR_TOP;
356 address_markers[i++].start_address = KASAN_SHADOW_START;
357 address_markers[i++].start_address = KASAN_SHADOW_END;
359 #endif /* CONFIG_PPC64 */
362 static int ptdump_show(struct seq_file *m, void *v)
364 struct pg_state st = {
366 .marker = address_markers,
370 st.start_address = PAGE_OFFSET;
372 st.start_address = KERN_VIRT_START;
374 /* Traverse kernel page tables */
375 walk_pagetables(&st);
376 note_page(&st, 0, 0, 0);
381 static int ptdump_open(struct inode *inode, struct file *file)
383 return single_open(file, ptdump_show, NULL);
386 static const struct file_operations ptdump_fops = {
390 .release = single_release,
393 static void build_pgtable_complete_mask(void)
397 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
398 if (pg_level[i].flag)
399 for (j = 0; j < pg_level[i].num; j++)
400 pg_level[i].mask |= pg_level[i].flag[j].mask;
403 #ifdef CONFIG_PPC_DEBUG_WX
404 void ptdump_check_wx(void)
406 struct pg_state st = {
408 .marker = address_markers,
413 st.start_address = PAGE_OFFSET;
415 st.start_address = KERN_VIRT_START;
417 walk_pagetables(&st);
420 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
423 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
427 static int ptdump_init(void)
429 struct dentry *debugfs_file;
432 build_pgtable_complete_mask();
433 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
435 return debugfs_file ? 0 : -ENOMEM;
437 device_initcall(ptdump_init);