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" },
72 const struct addr_marker
*marker
;
73 unsigned long start_address
;
85 static const struct prot_bits pte_bits
[] = {
121 .mask
= PTE_ATTRINDX_MASK
,
122 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
123 .set
= "DEVICE/nGnRnE",
125 .mask
= PTE_ATTRINDX_MASK
,
126 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
127 .set
= "DEVICE/nGnRE",
129 .mask
= PTE_ATTRINDX_MASK
,
130 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
133 .mask
= PTE_ATTRINDX_MASK
,
134 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
135 .set
= "MEM/NORMAL-NC",
137 .mask
= PTE_ATTRINDX_MASK
,
138 .val
= PTE_ATTRINDX(MT_NORMAL
),
144 const struct prot_bits
*bits
;
149 static struct pg_level pg_level
[] = {
153 .num
= ARRAY_SIZE(pte_bits
),
156 .num
= ARRAY_SIZE(pte_bits
),
159 .num
= ARRAY_SIZE(pte_bits
),
162 .num
= ARRAY_SIZE(pte_bits
),
166 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
171 for (i
= 0; i
< num
; i
++, bits
++) {
174 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
180 seq_printf(st
->seq
, " %s", s
);
184 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
,
187 static const char units
[] = "KMGTPE";
188 u64 prot
= val
& pg_level
[level
].mask
;
192 st
->current_prot
= prot
;
193 st
->start_address
= addr
;
194 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
195 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
196 addr
>= st
->marker
[1].start_address
) {
197 const char *unit
= units
;
200 if (st
->current_prot
) {
201 seq_printf(st
->seq
, "0x%16lx-0x%16lx ",
202 st
->start_address
, addr
);
204 delta
= (addr
- st
->start_address
) >> 10;
205 while (!(delta
& 1023) && unit
[1]) {
209 seq_printf(st
->seq
, "%9lu%c", delta
, *unit
);
210 if (pg_level
[st
->level
].bits
)
211 dump_prot(st
, pg_level
[st
->level
].bits
,
212 pg_level
[st
->level
].num
);
213 seq_puts(st
->seq
, "\n");
216 if (addr
>= st
->marker
[1].start_address
) {
218 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
221 st
->start_address
= addr
;
222 st
->current_prot
= prot
;
226 if (addr
>= st
->marker
[1].start_address
) {
228 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
233 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
)
235 pte_t
*pte
= pte_offset_kernel(pmd
, 0);
239 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
240 addr
= start
+ i
* PAGE_SIZE
;
241 note_page(st
, addr
, 4, pte_val(*pte
));
245 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
247 pmd_t
*pmd
= pmd_offset(pud
, 0);
251 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
252 addr
= start
+ i
* PMD_SIZE
;
253 if (pmd_none(*pmd
) || pmd_sect(*pmd
)) {
254 note_page(st
, addr
, 3, pmd_val(*pmd
));
256 BUG_ON(pmd_bad(*pmd
));
257 walk_pte(st
, pmd
, addr
);
262 static void walk_pud(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
264 pud_t
*pud
= pud_offset(pgd
, 0);
268 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
269 addr
= start
+ i
* PUD_SIZE
;
270 if (pud_none(*pud
) || pud_sect(*pud
)) {
271 note_page(st
, addr
, 2, pud_val(*pud
));
273 BUG_ON(pud_bad(*pud
));
274 walk_pmd(st
, pud
, addr
);
279 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
, unsigned long start
)
281 pgd_t
*pgd
= pgd_offset(mm
, 0UL);
285 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
286 addr
= start
+ i
* PGDIR_SIZE
;
287 if (pgd_none(*pgd
)) {
288 note_page(st
, addr
, 1, pgd_val(*pgd
));
290 BUG_ON(pgd_bad(*pgd
));
291 walk_pud(st
, pgd
, addr
);
296 static int ptdump_show(struct seq_file
*m
, void *v
)
298 struct pg_state st
= {
300 .marker
= address_markers
,
303 walk_pgd(&st
, &init_mm
, LOWEST_ADDR
);
305 note_page(&st
, 0, 0, 0);
309 static int ptdump_open(struct inode
*inode
, struct file
*file
)
311 return single_open(file
, ptdump_show
, NULL
);
314 static const struct file_operations ptdump_fops
= {
318 .release
= single_release
,
321 static int ptdump_init(void)
326 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
327 if (pg_level
[i
].bits
)
328 for (j
= 0; j
< pg_level
[i
].num
; j
++)
329 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
331 #ifdef CONFIG_SPARSEMEM_VMEMMAP
332 address_markers
[VMEMMAP_START_NR
].start_address
=
333 (unsigned long)virt_to_page(PAGE_OFFSET
);
334 address_markers
[VMEMMAP_END_NR
].start_address
=
335 (unsigned long)virt_to_page(high_memory
);
338 pe
= debugfs_create_file("kernel_page_tables", 0400, NULL
, NULL
,
340 return pe
? 0 : -ENOMEM
;
342 device_initcall(ptdump_init
);