2 * Copyright 2016, Rashmica Gupta, IBM Corp.
4 * This traverses the kernel pagetables and dumps the
5 * information about the used sections of memory to
6 * /sys/kernel/debug/kernel_pagetables.
8 * Derived from the arm64 implementation:
9 * Copyright (c) 2014, The Linux Foundation, Laura Abbott.
10 * (C) Copyright 2008 Intel Corporation, Arjan van de Ven.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; version 2
17 #include <linux/debugfs.h>
19 #include <linux/hugetlb.h>
22 #include <linux/highmem.h>
23 #include <linux/sched.h>
24 #include <linux/seq_file.h>
25 #include <asm/fixmap.h>
26 #include <asm/pgtable.h>
27 #include <linux/const.h>
29 #include <asm/pgalloc.h>
31 #include "dump_linuxpagetables.h"
34 #define KERN_VIRT_START 0
38 * To visualise what is happening,
40 * - PTRS_PER_P** = how many entries there are in the corresponding P**
41 * - P**_SHIFT = how many bits of the address we use to index into the
43 * - P**_SIZE is how much memory we can access through the table - not the
44 * size of the table itself.
45 * P**={PGD, PUD, PMD, PTE}
48 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
49 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
52 * In the case where there are only 3 levels, the PUD is folded into the
53 * PGD: every PUD has only one entry which points to the PMD.
55 * The page dumper groups page table entries of the same type into a single
56 * description. It uses pg_state to track the range information while
57 * iterating over the PTE entries. When the continuity is broken it then
58 * dumps out a description of the range - ie PTEs that are virtually contiguous
59 * with the same PTE flags are chunked together. This is to make it clear how
60 * different areas of the kernel virtual memory are used.
65 const struct addr_marker *marker;
66 unsigned long start_address;
67 unsigned long start_pa;
68 unsigned long last_pa;
74 unsigned long start_address;
78 static struct addr_marker address_markers[] = {
79 { 0, "Start of kernel VM" },
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" },
93 #ifdef CONFIG_NOT_COHERENT_CACHE
94 { 0, "Consistent mem start" },
95 { 0, "Consistent mem end" },
98 { 0, "Highmem PTEs start" },
99 { 0, "Highmem PTEs end" },
101 { 0, "Fixmap start" },
107 static void dump_flag_info(struct pg_state *st, const struct flag_info
108 *flag, u64 pte, int num)
112 for (i = 0; i < num; i++, flag++) {
113 const char *s = NULL;
116 /* flag not defined so don't check it */
119 /* Some 'flags' are actually values */
121 val = pte & flag->val;
123 val = val >> flag->shift;
124 seq_printf(st->seq, " %s:%llx", flag->set, val);
126 if ((pte & flag->mask) == flag->val)
131 seq_printf(st->seq, " %s", s);
133 st->current_flags &= ~flag->mask;
135 if (st->current_flags != 0)
136 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
139 static void dump_addr(struct pg_state *st, unsigned long addr)
141 static const char units[] = "KMGTPE";
142 const char *unit = units;
146 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
147 seq_printf(st->seq, "0x%016lx ", st->start_pa);
149 seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
150 seq_printf(st->seq, "0x%08lx ", st->start_pa);
153 delta = (addr - st->start_address) >> 10;
154 /* Work out what appropriate unit to use */
155 while (!(delta & 1023) && unit[1]) {
159 seq_printf(st->seq, "%9lu%c", delta, *unit);
163 static void note_page(struct pg_state *st, unsigned long addr,
164 unsigned int level, u64 val)
166 u64 flag = val & pg_level[level].mask;
167 u64 pa = val & PTE_RPN_MASK;
169 /* At first no level is set */
172 st->current_flags = flag;
173 st->start_address = addr;
176 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
178 * Dump the section of virtual memory when:
179 * - the PTE flags from one entry to the next differs.
180 * - we change levels in the tree.
181 * - the address is in a different section of memory and is thus
182 * used for a different purpose, regardless of the flags.
183 * - the pa of this page is not adjacent to the last inspected page
185 } else if (flag != st->current_flags || level != st->level ||
186 addr >= st->marker[1].start_address ||
187 pa != st->last_pa + PAGE_SIZE) {
189 /* Check the PTE flags */
190 if (st->current_flags) {
193 /* Dump all the flags */
194 if (pg_level[st->level].flag)
195 dump_flag_info(st, pg_level[st->level].flag,
197 pg_level[st->level].num);
199 seq_putc(st->seq, '\n');
203 * Address indicates we have passed the end of the
204 * current section of virtual memory
206 while (addr >= st->marker[1].start_address) {
208 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
210 st->start_address = addr;
213 st->current_flags = flag;
220 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
222 pte_t *pte = pte_offset_kernel(pmd, 0);
226 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
227 addr = start + i * PAGE_SIZE;
228 note_page(st, addr, 4, pte_val(*pte));
233 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
235 pmd_t *pmd = pmd_offset(pud, 0);
239 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
240 addr = start + i * PMD_SIZE;
241 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
243 walk_pte(st, pmd, addr);
245 note_page(st, addr, 3, pmd_val(*pmd));
249 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
251 pud_t *pud = pud_offset(pgd, 0);
255 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
256 addr = start + i * PUD_SIZE;
257 if (!pud_none(*pud) && !pud_huge(*pud))
259 walk_pmd(st, pud, addr);
261 note_page(st, addr, 2, pud_val(*pud));
265 static void walk_pagetables(struct pg_state *st)
267 pgd_t *pgd = pgd_offset_k(0UL);
271 addr = st->start_address;
274 * Traverse the linux pagetable structure and dump pages that are in
275 * the hash pagetable.
277 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
278 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
280 walk_pud(st, pgd, addr);
282 note_page(st, addr, 1, pgd_val(*pgd));
286 static void populate_markers(void)
290 address_markers[i++].start_address = PAGE_OFFSET;
291 address_markers[i++].start_address = VMALLOC_START;
292 address_markers[i++].start_address = VMALLOC_END;
294 address_markers[i++].start_address = ISA_IO_BASE;
295 address_markers[i++].start_address = ISA_IO_END;
296 address_markers[i++].start_address = PHB_IO_BASE;
297 address_markers[i++].start_address = PHB_IO_END;
298 address_markers[i++].start_address = IOREMAP_BASE;
299 address_markers[i++].start_address = IOREMAP_END;
300 #ifdef CONFIG_PPC_BOOK3S_64
301 address_markers[i++].start_address = H_VMEMMAP_BASE;
303 address_markers[i++].start_address = VMEMMAP_BASE;
305 #else /* !CONFIG_PPC64 */
306 address_markers[i++].start_address = ioremap_bot;
307 address_markers[i++].start_address = IOREMAP_TOP;
308 #ifdef CONFIG_NOT_COHERENT_CACHE
309 address_markers[i++].start_address = IOREMAP_TOP;
310 address_markers[i++].start_address = IOREMAP_TOP +
311 CONFIG_CONSISTENT_SIZE;
313 #ifdef CONFIG_HIGHMEM
314 address_markers[i++].start_address = PKMAP_BASE;
315 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
317 address_markers[i++].start_address = FIXADDR_START;
318 address_markers[i++].start_address = FIXADDR_TOP;
319 #endif /* CONFIG_PPC64 */
322 static int ptdump_show(struct seq_file *m, void *v)
324 struct pg_state st = {
326 .marker = address_markers,
330 st.start_address = PAGE_OFFSET;
332 st.start_address = KERN_VIRT_START;
334 /* Traverse kernel page tables */
335 walk_pagetables(&st);
336 note_page(&st, 0, 0, 0);
341 static int ptdump_open(struct inode *inode, struct file *file)
343 return single_open(file, ptdump_show, NULL);
346 static const struct file_operations ptdump_fops = {
350 .release = single_release,
353 static void build_pgtable_complete_mask(void)
357 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
358 if (pg_level[i].flag)
359 for (j = 0; j < pg_level[i].num; j++)
360 pg_level[i].mask |= pg_level[i].flag[j].mask;
363 static int ptdump_init(void)
365 struct dentry *debugfs_file;
368 build_pgtable_complete_mask();
369 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
371 return debugfs_file ? 0 : -ENOMEM;
373 device_initcall(ptdump_init);