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/sched.h>
23 #include <linux/seq_file.h>
24 #include <asm/fixmap.h>
25 #include <asm/pgtable.h>
26 #include <linux/const.h>
28 #include <asm/pgalloc.h>
30 #include "dump_linuxpagetables.h"
33 #define KERN_VIRT_START 0
37 * To visualise what is happening,
39 * - PTRS_PER_P** = how many entries there are in the corresponding P**
40 * - P**_SHIFT = how many bits of the address we use to index into the
42 * - P**_SIZE is how much memory we can access through the table - not the
43 * size of the table itself.
44 * P**={PGD, PUD, PMD, PTE}
47 * Each entry of the PGD points to a PUD. Each entry of a PUD points to a
48 * PMD. Each entry of a PMD points to a PTE. And every PTE entry points to
51 * In the case where there are only 3 levels, the PUD is folded into the
52 * PGD: every PUD has only one entry which points to the PMD.
54 * The page dumper groups page table entries of the same type into a single
55 * description. It uses pg_state to track the range information while
56 * iterating over the PTE entries. When the continuity is broken it then
57 * dumps out a description of the range - ie PTEs that are virtually contiguous
58 * with the same PTE flags are chunked together. This is to make it clear how
59 * different areas of the kernel virtual memory are used.
64 const struct addr_marker *marker;
65 unsigned long start_address;
66 unsigned long start_pa;
67 unsigned long last_pa;
73 unsigned long start_address;
77 static struct addr_marker address_markers[] = {
78 { 0, "Start of kernel VM" },
79 { 0, "vmalloc() Area" },
80 { 0, "vmalloc() End" },
82 { 0, "isa I/O start" },
84 { 0, "phb I/O start" },
86 { 0, "I/O remap start" },
87 { 0, "I/O remap end" },
88 { 0, "vmemmap start" },
90 { 0, "Early I/O remap start" },
91 { 0, "Early I/O remap end" },
92 #ifdef CONFIG_NOT_COHERENT_CACHE
93 { 0, "Consistent mem start" },
94 { 0, "Consistent mem end" },
97 { 0, "Highmem PTEs start" },
98 { 0, "Highmem PTEs end" },
100 { 0, "Fixmap start" },
106 static void dump_flag_info(struct pg_state *st, const struct flag_info
107 *flag, u64 pte, int num)
111 for (i = 0; i < num; i++, flag++) {
112 const char *s = NULL;
115 /* flag not defined so don't check it */
118 /* Some 'flags' are actually values */
120 val = pte & flag->val;
122 val = val >> flag->shift;
123 seq_printf(st->seq, " %s:%llx", flag->set, val);
125 if ((pte & flag->mask) == flag->val)
130 seq_printf(st->seq, " %s", s);
132 st->current_flags &= ~flag->mask;
134 if (st->current_flags != 0)
135 seq_printf(st->seq, " unknown flags:%llx", st->current_flags);
138 static void dump_addr(struct pg_state *st, unsigned long addr)
140 static const char units[] = "KMGTPE";
141 const char *unit = units;
145 seq_printf(st->seq, "0x%016lx-0x%016lx ", st->start_address, addr-1);
146 seq_printf(st->seq, "0x%016lx ", st->start_pa);
148 seq_printf(st->seq, "0x%08lx-0x%08lx ", st->start_address, addr - 1);
149 seq_printf(st->seq, "0x%08lx ", st->start_pa);
152 delta = (addr - st->start_address) >> 10;
153 /* Work out what appropriate unit to use */
154 while (!(delta & 1023) && unit[1]) {
158 seq_printf(st->seq, "%9lu%c", delta, *unit);
162 static void note_page(struct pg_state *st, unsigned long addr,
163 unsigned int level, u64 val)
165 u64 flag = val & pg_level[level].mask;
166 u64 pa = val & PTE_RPN_MASK;
168 /* At first no level is set */
171 st->current_flags = flag;
172 st->start_address = addr;
175 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
177 * Dump the section of virtual memory when:
178 * - the PTE flags from one entry to the next differs.
179 * - we change levels in the tree.
180 * - the address is in a different section of memory and is thus
181 * used for a different purpose, regardless of the flags.
182 * - the pa of this page is not adjacent to the last inspected page
184 } else if (flag != st->current_flags || level != st->level ||
185 addr >= st->marker[1].start_address ||
186 pa != st->last_pa + PAGE_SIZE) {
188 /* Check the PTE flags */
189 if (st->current_flags) {
192 /* Dump all the flags */
193 if (pg_level[st->level].flag)
194 dump_flag_info(st, pg_level[st->level].flag,
196 pg_level[st->level].num);
198 seq_putc(st->seq, '\n');
202 * Address indicates we have passed the end of the
203 * current section of virtual memory
205 while (addr >= st->marker[1].start_address) {
207 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
209 st->start_address = addr;
212 st->current_flags = flag;
219 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
221 pte_t *pte = pte_offset_kernel(pmd, 0);
225 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
226 addr = start + i * PAGE_SIZE;
227 note_page(st, addr, 4, pte_val(*pte));
232 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
234 pmd_t *pmd = pmd_offset(pud, 0);
238 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
239 addr = start + i * PMD_SIZE;
240 if (!pmd_none(*pmd) && !pmd_huge(*pmd))
242 walk_pte(st, pmd, addr);
244 note_page(st, addr, 3, pmd_val(*pmd));
248 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
250 pud_t *pud = pud_offset(pgd, 0);
254 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
255 addr = start + i * PUD_SIZE;
256 if (!pud_none(*pud) && !pud_huge(*pud))
258 walk_pmd(st, pud, addr);
260 note_page(st, addr, 2, pud_val(*pud));
264 static void walk_pagetables(struct pg_state *st)
266 pgd_t *pgd = pgd_offset_k(0UL);
270 addr = st->start_address;
273 * Traverse the linux pagetable structure and dump pages that are in
274 * the hash pagetable.
276 for (i = 0; i < PTRS_PER_PGD; i++, pgd++, addr += PGDIR_SIZE) {
277 if (!pgd_none(*pgd) && !pgd_huge(*pgd))
279 walk_pud(st, pgd, addr);
281 note_page(st, addr, 1, pgd_val(*pgd));
285 static void populate_markers(void)
289 address_markers[i++].start_address = PAGE_OFFSET;
290 address_markers[i++].start_address = VMALLOC_START;
291 address_markers[i++].start_address = VMALLOC_END;
293 address_markers[i++].start_address = ISA_IO_BASE;
294 address_markers[i++].start_address = ISA_IO_END;
295 address_markers[i++].start_address = PHB_IO_BASE;
296 address_markers[i++].start_address = PHB_IO_END;
297 address_markers[i++].start_address = IOREMAP_BASE;
298 address_markers[i++].start_address = IOREMAP_END;
299 #ifdef CONFIG_PPC_BOOK3S_64
300 address_markers[i++].start_address = H_VMEMMAP_BASE;
302 address_markers[i++].start_address = VMEMMAP_BASE;
304 #else /* !CONFIG_PPC64 */
305 address_markers[i++].start_address = ioremap_bot;
306 address_markers[i++].start_address = IOREMAP_TOP;
307 #ifdef CONFIG_NOT_COHERENT_CACHE
308 address_markers[i++].start_address = IOREMAP_TOP;
309 address_markers[i++].start_address = IOREMAP_TOP +
310 CONFIG_CONSISTENT_SIZE;
312 #ifdef CONFIG_HIGHMEM
313 address_markers[i++].start_address = PKMAP_BASE;
314 address_markers[i++].start_address = PKMAP_ADDR(LAST_PKMAP);
316 address_markers[i++].start_address = FIXADDR_START;
317 address_markers[i++].start_address = FIXADDR_TOP;
318 #endif /* CONFIG_PPC64 */
321 static int ptdump_show(struct seq_file *m, void *v)
323 struct pg_state st = {
325 .marker = address_markers,
329 st.start_address = PAGE_OFFSET;
331 st.start_address = KERN_VIRT_START;
333 /* Traverse kernel page tables */
334 walk_pagetables(&st);
335 note_page(&st, 0, 0, 0);
340 static int ptdump_open(struct inode *inode, struct file *file)
342 return single_open(file, ptdump_show, NULL);
345 static const struct file_operations ptdump_fops = {
349 .release = single_release,
352 static void build_pgtable_complete_mask(void)
356 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
357 if (pg_level[i].flag)
358 for (j = 0; j < pg_level[i].num; j++)
359 pg_level[i].mask |= pg_level[i].flag[j].mask;
362 static int ptdump_init(void)
364 struct dentry *debugfs_file;
367 build_pgtable_complete_mask();
368 debugfs_file = debugfs_create_file("kernel_page_tables", 0400, NULL,
370 return debugfs_file ? 0 : -ENOMEM;
372 device_initcall(ptdump_init);