2 * Debug helper to dump the current kernel pagetables of the system
3 * so that we can see what the various memory ranges are set to.
5 * (C) Copyright 2008 Intel Corporation
7 * Author: Arjan van de Ven <arjan@linux.intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/debugfs.h>
19 #include <asm/pgtable.h>
22 * The dumper groups pagetable entries of the same type into one, and for
23 * that it needs to keep some state when walking, and flush this state
24 * when a "break" in the continuity is found.
28 pgprot_t current_prot;
29 unsigned long start_address;
30 unsigned long current_address;
37 /* Multipliers for offsets within the PTEs */
38 #define LEVEL_4_MULT (PAGE_SIZE)
39 #define LEVEL_3_MULT (512UL * LEVEL_4_MULT)
40 #define LEVEL_2_MULT (512UL * LEVEL_3_MULT)
41 #define LEVEL_1_MULT (512UL * LEVEL_2_MULT)
45 * Print a readable form of a pgprot_t to the seq_file
47 static void printk_prot(struct seq_file *m, pgprot_t prot, int level)
49 unsigned long pr = pgprot_val(prot);
52 seq_printf(m, "USR ");
60 seq_printf(m, "PWT ");
64 seq_printf(m, "PCD ");
68 /* Bit 9 has a different meaning on level 3 vs 4 */
71 seq_printf(m, "PSE ");
76 seq_printf(m, "pat ");
80 if (pr & _PAGE_GLOBAL)
81 seq_printf(m, "GLB ");
91 * Sign-extend the 48 bit address to 64 bit
93 static unsigned long sign_extend(unsigned long u)
96 u = u | (0xffffUL << 48);
101 * This function gets called on a break in a continuous series
102 * of PTE entries; the next one is different so we need to
103 * print what we collected so far.
105 static void note_page(struct seq_file *m, struct pg_state *st,
106 pgprot_t new_prot, int level)
108 unsigned long prot, cur;
111 * If we have a "break" in the series, we need to flush the state that
112 * we have now. "break" is either changing perms or a different level.
114 prot = pgprot_val(new_prot) & ~(PTE_MASK);
115 cur = pgprot_val(st->current_prot) & ~(PTE_MASK);
117 if ((prot != cur || level != st->level) &&
118 st->current_address != st->start_address) {
123 * We print markers for special areas of address space,
124 * such as the start of vmalloc space etc.
125 * This helps in the interpretation.
127 if (!st->printed_vmalloc &&
128 st->start_address >= VMALLOC_START) {
129 seq_printf(m, "---[ VMALLOC SPACE ]---\n");
130 st->printed_vmalloc = 1;
132 if (!st->printed_modules &&
133 st->start_address >= MODULES_VADDR) {
134 seq_printf(m, "---[ MODULES SPACE ]---\n");
135 st->printed_modules = 1;
137 if (st->printed_modules < 2 &&
138 st->start_address >= MODULES_END) {
139 seq_printf(m, "---[ END MODULES SPACE ]---\n");
140 st->printed_modules = 2;
142 if (!st->printed_vmemmap &&
143 st->start_address >= VMEMMAP_START) {
144 seq_printf(m, "---[ VMMEMMAP SPACE ]---\n");
145 st->printed_vmemmap = 1;
147 if (!st->printed_highmap &&
148 st->start_address >= __START_KERNEL_map) {
149 seq_printf(m, "---[ HIGH KERNEL MAPPING ]---\n");
150 st->printed_highmap = 1;
154 * Now print the actual finished series
156 seq_printf(m, "[ %016lx - %016lx ",
157 st->start_address, st->current_address);
159 delta = (st->current_address - st->start_address) >> 10;
160 if ((delta & 1023) == 0) {
164 if (pgprot_val(st->current_prot)) {
165 seq_printf(m, "Size %9lu%cb ", delta, unit);
166 printk_prot(m, st->current_prot, st->level);
167 seq_printf(m, "L%i]\n", st->level);
169 /* don't print protections on non-present memory */
170 seq_printf(m, "%14lu%cb", delta, unit);
171 seq_printf(m, " L%i]\n",
174 st->start_address = st->current_address;
175 st->current_prot = new_prot;
180 static void walk_level_4(struct seq_file *m, struct pg_state *st, pmd_t addr,
186 start = (pte_t *) pmd_page_vaddr(addr);
187 for (i = 0; i < PTRS_PER_PTE; i++) {
188 pgprot_t prot = pte_pgprot(*start);
190 st->current_address = sign_extend(P + i * LEVEL_4_MULT);
191 note_page(m, st, prot, 4);
197 static void walk_level_3(struct seq_file *m, struct pg_state *st, pud_t addr,
203 start = (pmd_t *) pud_page_vaddr(addr);
204 for (i = 0; i < PTRS_PER_PMD; i++) {
205 st->current_address = sign_extend(P + i * LEVEL_3_MULT);
206 if (!pmd_none(*start)) {
209 prot = pmd_val(*start) & ~(PTE_MASK);
210 /* Deal with 2Mb pages */
211 if (pmd_large(*start))
212 note_page(m, st, __pgprot(prot), 3);
214 walk_level_4(m, st, *start,
215 P + i * LEVEL_3_MULT);
217 note_page(m, st, __pgprot(0), 3);
223 static void walk_level_2(struct seq_file *m, struct pg_state *st, pgd_t addr,
229 start = (pud_t *) pgd_page_vaddr(addr);
231 for (i = 0; i < PTRS_PER_PUD; i++) {
232 if (!pud_none(*start)) {
235 prot = pud_val(*start) & ~(PTE_MASK);
236 /* Deal with 1Gb pages */
237 if (pud_large(*start))
238 note_page(m, st, __pgprot(prot), 2);
240 walk_level_3(m, st, *start,
241 P + i * LEVEL_2_MULT);
243 note_page(m, st, __pgprot(0), 2);
249 static void walk_level_1(struct seq_file *m)
251 pgd_t *start = (pgd_t *) &init_level4_pgt;
255 memset(&st, 0, sizeof(st));
258 for (i = 0; i < PTRS_PER_PGD; i++) {
259 if (!pgd_none(*start))
260 walk_level_2(m, &st, *start, i * LEVEL_1_MULT);
262 note_page(m, &st, __pgprot(0), 1);
267 static int ptdump_show(struct seq_file *m, void *v)
269 seq_puts(m, "Kernel pagetable dump\n");
274 static int ptdump_open(struct inode *inode, struct file *filp)
276 return single_open(filp, ptdump_show, NULL);
279 static const struct file_operations ptdump_fops = {
283 .release = single_release,
286 int pt_dump_init(void)
290 pe = debugfs_create_file("kernel_page_tables", 0600, NULL, NULL,
298 __initcall(pt_dump_init);
299 MODULE_LICENSE("GPL");
300 MODULE_AUTHOR("Arjan van de Ven <arjan@linux.intel.com>");
301 MODULE_DESCRIPTION("Kernel debugging helper that dumps pagetables");