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/kasan.h>
27 #include <asm/memory.h>
28 #include <asm/pgtable.h>
29 #include <asm/pgtable-hwdef.h>
30 #include <asm/ptdump.h>
32 static const struct addr_marker address_markers
[] = {
34 { KASAN_SHADOW_START
, "Kasan shadow start" },
35 { KASAN_SHADOW_END
, "Kasan shadow end" },
37 { MODULES_VADDR
, "Modules start" },
38 { MODULES_END
, "Modules end" },
39 { VMALLOC_START
, "vmalloc() Area" },
40 { VMALLOC_END
, "vmalloc() End" },
41 { FIXADDR_START
, "Fixmap start" },
42 { FIXADDR_TOP
, "Fixmap end" },
43 { PCI_IO_START
, "PCI I/O start" },
44 { PCI_IO_END
, "PCI I/O end" },
45 #ifdef CONFIG_SPARSEMEM_VMEMMAP
46 { VMEMMAP_START
, "vmemmap start" },
47 { VMEMMAP_START
+ VMEMMAP_SIZE
, "vmemmap end" },
49 { PAGE_OFFSET
, "Linear Mapping" },
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.
61 const struct addr_marker
*marker
;
62 unsigned long start_address
;
74 static const struct prot_bits pte_bits
[] = {
116 .mask
= PTE_TABLE_BIT
,
117 .val
= PTE_TABLE_BIT
,
125 .mask
= PTE_ATTRINDX_MASK
,
126 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
127 .set
= "DEVICE/nGnRnE",
129 .mask
= PTE_ATTRINDX_MASK
,
130 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
131 .set
= "DEVICE/nGnRE",
133 .mask
= PTE_ATTRINDX_MASK
,
134 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
137 .mask
= PTE_ATTRINDX_MASK
,
138 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
139 .set
= "MEM/NORMAL-NC",
141 .mask
= PTE_ATTRINDX_MASK
,
142 .val
= PTE_ATTRINDX(MT_NORMAL
),
148 const struct prot_bits
*bits
;
154 static struct pg_level pg_level
[] = {
159 .num
= ARRAY_SIZE(pte_bits
),
161 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
163 .num
= ARRAY_SIZE(pte_bits
),
165 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
167 .num
= ARRAY_SIZE(pte_bits
),
171 .num
= ARRAY_SIZE(pte_bits
),
175 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
180 for (i
= 0; i
< num
; i
++, bits
++) {
183 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
189 seq_printf(st
->seq
, " %s", s
);
193 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
,
196 static const char units
[] = "KMGTPE";
197 u64 prot
= val
& pg_level
[level
].mask
;
201 st
->current_prot
= prot
;
202 st
->start_address
= addr
;
203 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
204 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
205 addr
>= st
->marker
[1].start_address
) {
206 const char *unit
= units
;
209 if (st
->current_prot
) {
210 seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
211 st
->start_address
, addr
);
213 delta
= (addr
- st
->start_address
) >> 10;
214 while (!(delta
& 1023) && unit
[1]) {
218 seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
219 pg_level
[st
->level
].name
);
220 if (pg_level
[st
->level
].bits
)
221 dump_prot(st
, pg_level
[st
->level
].bits
,
222 pg_level
[st
->level
].num
);
223 seq_puts(st
->seq
, "\n");
226 if (addr
>= st
->marker
[1].start_address
) {
228 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
231 st
->start_address
= addr
;
232 st
->current_prot
= prot
;
236 if (addr
>= st
->marker
[1].start_address
) {
238 seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
243 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
)
245 pte_t
*pte
= pte_offset_kernel(pmd
, 0UL);
249 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
250 addr
= start
+ i
* PAGE_SIZE
;
251 note_page(st
, addr
, 4, pte_val(*pte
));
255 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
257 pmd_t
*pmd
= pmd_offset(pud
, 0UL);
261 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
262 addr
= start
+ i
* PMD_SIZE
;
263 if (pmd_none(*pmd
) || pmd_sect(*pmd
)) {
264 note_page(st
, addr
, 3, pmd_val(*pmd
));
266 BUG_ON(pmd_bad(*pmd
));
267 walk_pte(st
, pmd
, addr
);
272 static void walk_pud(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
274 pud_t
*pud
= pud_offset(pgd
, 0UL);
278 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
279 addr
= start
+ i
* PUD_SIZE
;
280 if (pud_none(*pud
) || pud_sect(*pud
)) {
281 note_page(st
, addr
, 2, pud_val(*pud
));
283 BUG_ON(pud_bad(*pud
));
284 walk_pmd(st
, pud
, addr
);
289 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
292 pgd_t
*pgd
= pgd_offset(mm
, 0UL);
296 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
297 addr
= start
+ i
* PGDIR_SIZE
;
298 if (pgd_none(*pgd
)) {
299 note_page(st
, addr
, 1, pgd_val(*pgd
));
301 BUG_ON(pgd_bad(*pgd
));
302 walk_pud(st
, pgd
, addr
);
307 static int ptdump_show(struct seq_file
*m
, void *v
)
309 struct ptdump_info
*info
= m
->private;
310 struct pg_state st
= {
312 .marker
= info
->markers
,
315 walk_pgd(&st
, info
->mm
, info
->base_addr
);
317 note_page(&st
, 0, 0, 0);
321 static int ptdump_open(struct inode
*inode
, struct file
*file
)
323 return single_open(file
, ptdump_show
, inode
->i_private
);
326 static const struct file_operations ptdump_fops
= {
330 .release
= single_release
,
333 int ptdump_register(struct ptdump_info
*info
, const char *name
)
338 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
339 if (pg_level
[i
].bits
)
340 for (j
= 0; j
< pg_level
[i
].num
; j
++)
341 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
343 pe
= debugfs_create_file(name
, 0400, NULL
, info
, &ptdump_fops
);
344 return pe
? 0 : -ENOMEM
;
347 static struct ptdump_info kernel_ptdump_info
= {
349 .markers
= address_markers
,
350 .base_addr
= VA_START
,
353 static int ptdump_init(void)
355 return ptdump_register(&kernel_ptdump_info
, "kernel_page_tables");
357 device_initcall(ptdump_init
);