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>
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 #define REG "0x%016lx"
148 #define REG "0x%08lx"
151 seq_printf(st->seq, REG "-" REG " ", st->start_address, addr - 1);
152 if (st->start_pa == st->last_pa && st->start_address + PAGE_SIZE != addr) {
153 seq_printf(st->seq, "[" REG "]", st->start_pa);
154 delta = PAGE_SIZE >> 10;
156 seq_printf(st->seq, " " REG " ", st->start_pa);
157 delta = (addr - st->start_address) >> 10;
159 /* Work out what appropriate unit to use */
160 while (!(delta & 1023) && unit[1]) {
164 seq_printf(st->seq, "%9lu%c", delta, *unit);
168 static void note_page(struct pg_state *st, unsigned long addr,
169 unsigned int level, u64 val)
171 u64 flag = val & pg_level[level].mask;
172 u64 pa = val & PTE_RPN_MASK;
174 /* At first no level is set */
177 st->current_flags = flag;
178 st->start_address = addr;
181 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
183 * Dump the section of virtual memory when:
184 * - the PTE flags from one entry to the next differs.
185 * - we change levels in the tree.
186 * - the address is in a different section of memory and is thus
187 * used for a different purpose, regardless of the flags.
188 * - the pa of this page is not adjacent to the last inspected page
190 } else if (flag != st->current_flags || level != st->level ||
191 addr >= st->marker[1].start_address ||
192 (pa != st->last_pa + PAGE_SIZE &&
193 (pa != st->start_pa || st->start_pa != st->last_pa))) {
195 /* Check the PTE flags */
196 if (st->current_flags) {
199 /* Dump all the flags */
200 if (pg_level[st->level].flag)
201 dump_flag_info(st, pg_level[st->level].flag,
203 pg_level[st->level].num);
205 seq_putc(st->seq, '\n');
209 * Address indicates we have passed the end of the
210 * current section of virtual memory
212 while (addr >= st->marker[1].start_address) {
214 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
216 st->start_address = addr;
219 st->current_flags = flag;
226 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
228 pte_t *pte = pte_offset_kernel(pmd, 0);
232 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
233 addr = start + i * PAGE_SIZE;
234 note_page(st, addr, 4, pte_val(*pte));
239 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
241 pmd_t *pmd = pmd_offset(pud, 0);
245 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
246 addr = start + i * PMD_SIZE;
247 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
249 walk_pte(st, pmd, addr);
251 note_page(st, addr, 3, pmd_val(*pmd));
255 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
257 pud_t *pud = pud_offset(pgd, 0);
261 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
262 addr = start + i * PUD_SIZE;
263 if (!pud_none(*pud) && !pud_huge(*pud))
265 walk_pmd(st, pud, addr);
267 note_page(st, addr, 2, pud_val(*pud));
271 static void walk_pagetables(struct pg_state *st)
273 pgd_t *pgd = pgd_offset_k(0UL);
277 addr = st->start_address;
280 * Traverse the linux pagetable structure and dump pages that are in
281 * the hash pagetable.
283 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
284 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
286 walk_pud(st, pgd, addr);
288 note_page(st, addr, 1, pgd_val(*pgd));
292 static void populate_markers(void)
296 address_markers[i++].start_address = PAGE_OFFSET;
297 address_markers[i++].start_address = VMALLOC_START;
298 address_markers[i++].start_address = VMALLOC_END;
300 address_markers[i++].start_address = ISA_IO_BASE;
301 address_markers[i++].start_address = ISA_IO_END;
302 address_markers[i++].start_address = PHB_IO_BASE;
303 address_markers[i++].start_address = PHB_IO_END;
304 address_markers[i++].start_address = IOREMAP_BASE;
305 address_markers[i++].start_address = IOREMAP_END;
306 #ifdef CONFIG_PPC_BOOK3S_64
307 address_markers[i++].start_address = H_VMEMMAP_BASE;
309 address_markers[i++].start_address = VMEMMAP_BASE;
311 #else /* !CONFIG_PPC64 */
312 address_markers[i++].start_address = ioremap_bot;
313 address_markers[i++].start_address = IOREMAP_TOP;
314 #ifdef CONFIG_NOT_COHERENT_CACHE
315 address_markers[i++].start_address = IOREMAP_TOP;
316 address_markers[i++].start_address = IOREMAP_TOP +
317 CONFIG_CONSISTENT_SIZE;
319 #ifdef CONFIG_HIGHMEM
320 address_markers[i++].start_address = PKMAP_BASE;
321 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
323 address_markers[i++].start_address = FIXADDR_START;
324 address_markers[i++].start_address = FIXADDR_TOP;
325 #endif /* CONFIG_PPC64 */
328 static int ptdump_show(struct seq_file *m, void *v)
330 struct pg_state st = {
332 .marker = address_markers,
336 st.start_address = PAGE_OFFSET;
338 st.start_address = KERN_VIRT_START;
340 /* Traverse kernel page tables */
341 walk_pagetables(&st);
342 note_page(&st, 0, 0, 0);
347 static int ptdump_open(struct inode *inode, struct file *file)
349 return single_open(file, ptdump_show, NULL);
352 static const struct file_operations ptdump_fops = {
356 .release = single_release,
359 static void build_pgtable_complete_mask(void)
363 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
364 if (pg_level[i].flag)
365 for (j = 0; j < pg_level[i].num; j++)
366 pg_level[i].mask |= pg_level[i].flag[j].mask;
369 static int ptdump_init(void)
371 struct dentry *debugfs_file;
374 build_pgtable_complete_mask();
375 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
377 return debugfs_file ? 0 : -ENOMEM;
379 device_initcall(ptdump_init);