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 * Derived from x86 implementation:
6 * (C) Copyright 2008 Intel Corporation
8 * Author: Arjan van de Ven <arjan@linux.intel.com>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; version 2
15 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
20 #include <asm/fixmap.h>
21 #include <asm/pgtable.h>
24 unsigned long start_address
;
28 static struct addr_marker address_markers
[] = {
29 { MODULES_VADDR
, "Modules" },
30 { PAGE_OFFSET
, "Kernel Mapping" },
31 { 0, "vmalloc() Area" },
32 { VMALLOC_END
, "vmalloc() End" },
33 { FIXADDR_START
, "Fixmap Area" },
34 { CONFIG_VECTORS_BASE
, "Vectors" },
35 { CONFIG_VECTORS_BASE
+ PAGE_SIZE
* 2, "Vectors End" },
41 const struct addr_marker
*marker
;
42 unsigned long start_address
;
54 static const struct prot_bits pte_bits
[] = {
76 .mask
= L_PTE_MT_MASK
,
77 .val
= L_PTE_MT_UNCACHED
,
80 .mask
= L_PTE_MT_MASK
,
81 .val
= L_PTE_MT_BUFFERABLE
,
82 .set
= "MEM/BUFFERABLE/WC",
84 .mask
= L_PTE_MT_MASK
,
85 .val
= L_PTE_MT_WRITETHROUGH
,
86 .set
= "MEM/CACHED/WT",
88 .mask
= L_PTE_MT_MASK
,
89 .val
= L_PTE_MT_WRITEBACK
,
90 .set
= "MEM/CACHED/WBRA",
91 #ifndef CONFIG_ARM_LPAE
93 .mask
= L_PTE_MT_MASK
,
94 .val
= L_PTE_MT_MINICACHE
,
95 .set
= "MEM/MINICACHE",
98 .mask
= L_PTE_MT_MASK
,
99 .val
= L_PTE_MT_WRITEALLOC
,
100 .set
= "MEM/CACHED/WBWA",
102 .mask
= L_PTE_MT_MASK
,
103 .val
= L_PTE_MT_DEV_SHARED
,
105 #ifndef CONFIG_ARM_LPAE
107 .mask
= L_PTE_MT_MASK
,
108 .val
= L_PTE_MT_DEV_NONSHARED
,
109 .set
= "DEV/NONSHARED",
112 .mask
= L_PTE_MT_MASK
,
113 .val
= L_PTE_MT_DEV_WC
,
116 .mask
= L_PTE_MT_MASK
,
117 .val
= L_PTE_MT_DEV_CACHED
,
122 static const struct prot_bits section_bits
[] = {
123 #ifdef CONFIG_ARM_LPAE
125 .mask
= PMD_SECT_USER
,
126 .val
= PMD_SECT_USER
,
129 .mask
= L_PMD_SECT_RDONLY
,
130 .val
= L_PMD_SECT_RDONLY
,
133 #elif __LINUX_ARM_ARCH__ >= 6
135 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
136 .val
= PMD_SECT_APX
| PMD_SECT_AP_WRITE
,
139 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
140 .val
= PMD_SECT_AP_WRITE
,
143 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
144 .val
= PMD_SECT_AP_READ
,
147 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
148 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
150 #else /* ARMv4/ARMv5 */
151 /* These are approximate */
153 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
157 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
158 .val
= PMD_SECT_AP_WRITE
,
161 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
162 .val
= PMD_SECT_AP_READ
,
165 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
166 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
183 const struct prot_bits
*bits
;
188 static struct pg_level pg_level
[] = {
193 .bits
= section_bits
,
194 .num
= ARRAY_SIZE(section_bits
),
197 .num
= ARRAY_SIZE(pte_bits
),
201 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
, size_t num
)
205 for (i
= 0; i
< num
; i
++, bits
++) {
208 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
214 seq_printf(st
->seq
, " %s", s
);
218 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
, u64 val
)
220 static const char units
[] = "KMGTPE";
221 u64 prot
= val
& pg_level
[level
].mask
;
225 st
->current_prot
= prot
;
226 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
227 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
228 addr
>= st
->marker
[1].start_address
) {
229 const char *unit
= units
;
232 if (st
->current_prot
) {
233 seq_printf(st
->seq
, "0x%08lx-0x%08lx ",
234 st
->start_address
, addr
);
236 delta
= (addr
- st
->start_address
) >> 10;
237 while (!(delta
& 1023) && unit
[1]) {
241 seq_printf(st
->seq
, "%9lu%c", delta
, *unit
);
242 if (pg_level
[st
->level
].bits
)
243 dump_prot(st
, pg_level
[st
->level
].bits
, pg_level
[st
->level
].num
);
244 seq_printf(st
->seq
, "\n");
247 if (addr
>= st
->marker
[1].start_address
) {
249 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
251 st
->start_address
= addr
;
252 st
->current_prot
= prot
;
257 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
)
259 pte_t
*pte
= pte_offset_kernel(pmd
, 0);
263 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
264 addr
= start
+ i
* PAGE_SIZE
;
265 note_page(st
, addr
, 4, pte_val(*pte
));
269 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
271 pmd_t
*pmd
= pmd_offset(pud
, 0);
275 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
276 addr
= start
+ i
* PMD_SIZE
;
277 if (pmd_none(*pmd
) || pmd_large(*pmd
) || !pmd_present(*pmd
))
278 note_page(st
, addr
, 3, pmd_val(*pmd
));
280 walk_pte(st
, pmd
, addr
);
282 if (SECTION_SIZE
< PMD_SIZE
&& pmd_large(pmd
[1]))
283 note_page(st
, addr
+ SECTION_SIZE
, 3, pmd_val(pmd
[1]));
287 static void walk_pud(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
289 pud_t
*pud
= pud_offset(pgd
, 0);
293 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
294 addr
= start
+ i
* PUD_SIZE
;
295 if (!pud_none(*pud
)) {
296 walk_pmd(st
, pud
, addr
);
298 note_page(st
, addr
, 2, pud_val(*pud
));
303 static void walk_pgd(struct seq_file
*m
)
305 pgd_t
*pgd
= swapper_pg_dir
;
310 memset(&st
, 0, sizeof(st
));
312 st
.marker
= address_markers
;
314 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
315 addr
= i
* PGDIR_SIZE
;
316 if (!pgd_none(*pgd
)) {
317 walk_pud(&st
, pgd
, addr
);
319 note_page(&st
, addr
, 1, pgd_val(*pgd
));
323 note_page(&st
, 0, 0, 0);
326 static int ptdump_show(struct seq_file
*m
, void *v
)
332 static int ptdump_open(struct inode
*inode
, struct file
*file
)
334 return single_open(file
, ptdump_show
, NULL
);
337 static const struct file_operations ptdump_fops
= {
341 .release
= single_release
,
344 static int ptdump_init(void)
349 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
350 if (pg_level
[i
].bits
)
351 for (j
= 0; j
< pg_level
[i
].num
; j
++)
352 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
354 address_markers
[2].start_address
= VMALLOC_START
;
356 pe
= debugfs_create_file("kernel_page_tables", 0400, NULL
, NULL
,
358 return pe
? 0 : -ENOMEM
;
360 __initcall(ptdump_init
);