2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * Debug helper to dump the current kernel pagetables of the system
4 * so that we can see what the various memory ranges are set to.
6 * Derived from x86 and arm implementation:
7 * (C) Copyright 2008 Intel Corporation
9 * Author: Arjan van de Ven <arjan@linux.intel.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; version 2
16 #include <linux/debugfs.h>
17 #include <linux/errno.h>
20 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
25 #include <asm/fixmap.h>
26 #include <asm/memory.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgtable-hwdef.h>
30 #define LOWEST_ADDR (UL(0xffffffffffffffff) << VA_BITS)
33 unsigned long start_address;
37 enum address_markers_idx {
40 #ifdef CONFIG_SPARSEMEM_VMEMMAP
53 static struct addr_marker address_markers[] = {
54 { VMALLOC_START, "vmalloc() Area" },
55 { VMALLOC_END, "vmalloc() End" },
56 #ifdef CONFIG_SPARSEMEM_VMEMMAP
57 { 0, "vmemmap start" },
60 { FIXADDR_START, "Fixmap start" },
61 { FIXADDR_TOP, "Fixmap end" },
62 { PCI_IO_START, "PCI I/O start" },
63 { PCI_IO_END, "PCI I/O end" },
64 { MODULES_VADDR, "Modules start" },
65 { MODULES_END, "Modules end" },
66 { PAGE_OFFSET, "Kernel Mapping" },
71 * The page dumper groups page table entries of the same type into a single
72 * description. It uses pg_state to track the range information while
73 * iterating over the pte entries. When the continuity is broken it then
74 * dumps out a description of the range.
78 const struct addr_marker *marker;
79 unsigned long start_address;
91 static const struct prot_bits pte_bits[] = {
128 .mask = PTE_TABLE_BIT,
129 .val = PTE_TABLE_BIT,
137 .mask = PTE_ATTRINDX_MASK,
138 .val = PTE_ATTRINDX(MT_DEVICE_nGnRnE),
139 .set = "DEVICE/nGnRnE",
141 .mask = PTE_ATTRINDX_MASK,
142 .val = PTE_ATTRINDX(MT_DEVICE_nGnRE),
143 .set = "DEVICE/nGnRE",
145 .mask = PTE_ATTRINDX_MASK,
146 .val = PTE_ATTRINDX(MT_DEVICE_GRE),
149 .mask = PTE_ATTRINDX_MASK,
150 .val = PTE_ATTRINDX(MT_NORMAL_NC),
151 .set = "MEM/NORMAL-NC",
153 .mask = PTE_ATTRINDX_MASK,
154 .val = PTE_ATTRINDX(MT_NORMAL),
160 const struct prot_bits *bits;
165 static struct pg_level pg_level[] = {
169 .num = ARRAY_SIZE(pte_bits),
172 .num = ARRAY_SIZE(pte_bits),
175 .num = ARRAY_SIZE(pte_bits),
178 .num = ARRAY_SIZE(pte_bits),
182 static void dump_prot(struct pg_state *st, const struct prot_bits *bits,
187 for (i = 0; i < num; i++, bits++) {
190 if ((st->current_prot & bits->mask) == bits->val)
196 seq_printf(st->seq, " %s", s);
200 static void note_page(struct pg_state *st, unsigned long addr, unsigned level,
203 static const char units[] = "KMGTPE";
204 u64 prot = val & pg_level[level].mask;
208 st->current_prot = prot;
209 st->start_address = addr;
210 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
211 } else if (prot != st->current_prot || level != st->level ||
212 addr >= st->marker[1].start_address) {
213 const char *unit = units;
216 if (st->current_prot) {
217 seq_printf(st->seq, "0x%016lx-0x%016lx ",
218 st->start_address, addr);
220 delta = (addr - st->start_address) >> 10;
221 while (!(delta & 1023) && unit[1]) {
225 seq_printf(st->seq, "%9lu%c", delta, *unit);
226 if (pg_level[st->level].bits)
227 dump_prot(st, pg_level[st->level].bits,
228 pg_level[st->level].num);
229 seq_puts(st->seq, "\n");
232 if (addr >= st->marker[1].start_address) {
234 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
237 st->start_address = addr;
238 st->current_prot = prot;
242 if (addr >= st->marker[1].start_address) {
244 seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
249 static void walk_pte(struct pg_state *st, pmd_t *pmd, unsigned long start)
251 pte_t *pte = pte_offset_kernel(pmd, 0);
255 for (i = 0; i < PTRS_PER_PTE; i++, pte++) {
256 addr = start + i * PAGE_SIZE;
257 note_page(st, addr, 4, pte_val(*pte));
261 static void walk_pmd(struct pg_state *st, pud_t *pud, unsigned long start)
263 pmd_t *pmd = pmd_offset(pud, 0);
267 for (i = 0; i < PTRS_PER_PMD; i++, pmd++) {
268 addr = start + i * PMD_SIZE;
269 if (pmd_none(*pmd) || pmd_sect(*pmd)) {
270 note_page(st, addr, 3, pmd_val(*pmd));
272 BUG_ON(pmd_bad(*pmd));
273 walk_pte(st, pmd, addr);
278 static void walk_pud(struct pg_state *st, pgd_t *pgd, unsigned long start)
280 pud_t *pud = pud_offset(pgd, 0);
284 for (i = 0; i < PTRS_PER_PUD; i++, pud++) {
285 addr = start + i * PUD_SIZE;
286 if (pud_none(*pud) || pud_sect(*pud)) {
287 note_page(st, addr, 2, pud_val(*pud));
289 BUG_ON(pud_bad(*pud));
290 walk_pmd(st, pud, addr);
295 static void walk_pgd(struct pg_state *st, struct mm_struct *mm, unsigned long start)
297 pgd_t *pgd = pgd_offset(mm, 0UL);
301 for (i = 0; i < PTRS_PER_PGD; i++, pgd++) {
302 addr = start + i * PGDIR_SIZE;
303 if (pgd_none(*pgd)) {
304 note_page(st, addr, 1, pgd_val(*pgd));
306 BUG_ON(pgd_bad(*pgd));
307 walk_pud(st, pgd, addr);
312 static int ptdump_show(struct seq_file *m, void *v)
314 struct pg_state st = {
316 .marker = address_markers,
319 walk_pgd(&st, &init_mm, LOWEST_ADDR);
321 note_page(&st, 0, 0, 0);
325 static int ptdump_open(struct inode *inode, struct file *file)
327 return single_open(file, ptdump_show, NULL);
330 static const struct file_operations ptdump_fops = {
334 .release = single_release,
337 static int ptdump_init(void)
342 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
343 if (pg_level[i].bits)
344 for (j = 0; j < pg_level[i].num; j++)
345 pg_level[i].mask |= pg_level[i].bits[j].mask;
347 #ifdef CONFIG_SPARSEMEM_VMEMMAP
348 address_markers[VMEMMAP_START_NR].start_address =
349 (unsigned long)virt_to_page(PAGE_OFFSET);
350 address_markers[VMEMMAP_END_NR].start_address =
351 (unsigned long)virt_to_page(high_memory);
354 pe = debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
356 return pe ? 0 : -ENOMEM;
358 device_initcall(ptdump_init);