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" },
53 #define pt_dump_seq_printf(m, fmt, args...) \
56 seq_printf(m, fmt, ##args); \
59 #define pt_dump_seq_puts(m, fmt) \
66 * The page dumper groups page table entries of the same type into a single
67 * description. It uses pg_state to track the range information while
68 * iterating over the pte entries. When the continuity is broken it then
69 * dumps out a description of the range.
73 const struct addr_marker
*marker
;
74 unsigned long start_address
;
78 unsigned long wx_pages
;
79 unsigned long uxn_pages
;
89 static const struct prot_bits pte_bits
[] = {
131 .mask
= PTE_TABLE_BIT
,
132 .val
= PTE_TABLE_BIT
,
140 .mask
= PTE_ATTRINDX_MASK
,
141 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRnE
),
142 .set
= "DEVICE/nGnRnE",
144 .mask
= PTE_ATTRINDX_MASK
,
145 .val
= PTE_ATTRINDX(MT_DEVICE_nGnRE
),
146 .set
= "DEVICE/nGnRE",
148 .mask
= PTE_ATTRINDX_MASK
,
149 .val
= PTE_ATTRINDX(MT_DEVICE_GRE
),
152 .mask
= PTE_ATTRINDX_MASK
,
153 .val
= PTE_ATTRINDX(MT_NORMAL_NC
),
154 .set
= "MEM/NORMAL-NC",
156 .mask
= PTE_ATTRINDX_MASK
,
157 .val
= PTE_ATTRINDX(MT_NORMAL
),
163 const struct prot_bits
*bits
;
169 static struct pg_level pg_level
[] = {
174 .num
= ARRAY_SIZE(pte_bits
),
176 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
178 .num
= ARRAY_SIZE(pte_bits
),
180 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
182 .num
= ARRAY_SIZE(pte_bits
),
186 .num
= ARRAY_SIZE(pte_bits
),
190 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
,
195 for (i
= 0; i
< num
; i
++, bits
++) {
198 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
204 pt_dump_seq_printf(st
->seq
, " %s", s
);
208 static void note_prot_uxn(struct pg_state
*st
, unsigned long addr
)
213 if ((st
->current_prot
& PTE_UXN
) == PTE_UXN
)
216 WARN_ONCE(1, "arm64/mm: Found non-UXN mapping at address %p/%pS\n",
217 (void *)st
->start_address
, (void *)st
->start_address
);
219 st
->uxn_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
222 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
226 if ((st
->current_prot
& PTE_RDONLY
) == PTE_RDONLY
)
228 if ((st
->current_prot
& PTE_PXN
) == PTE_PXN
)
231 WARN_ONCE(1, "arm64/mm: Found insecure W+X mapping at address %p/%pS\n",
232 (void *)st
->start_address
, (void *)st
->start_address
);
234 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
237 static void note_page(struct pg_state
*st
, unsigned long addr
, unsigned level
,
240 static const char units
[] = "KMGTPE";
241 u64 prot
= val
& pg_level
[level
].mask
;
245 st
->current_prot
= prot
;
246 st
->start_address
= addr
;
247 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
248 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
249 addr
>= st
->marker
[1].start_address
) {
250 const char *unit
= units
;
253 if (st
->current_prot
) {
254 note_prot_uxn(st
, addr
);
255 note_prot_wx(st
, addr
);
256 pt_dump_seq_printf(st
->seq
, "0x%016lx-0x%016lx ",
257 st
->start_address
, addr
);
259 delta
= (addr
- st
->start_address
) >> 10;
260 while (!(delta
& 1023) && unit
[1]) {
264 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
265 pg_level
[st
->level
].name
);
266 if (pg_level
[st
->level
].bits
)
267 dump_prot(st
, pg_level
[st
->level
].bits
,
268 pg_level
[st
->level
].num
);
269 pt_dump_seq_puts(st
->seq
, "\n");
272 if (addr
>= st
->marker
[1].start_address
) {
274 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
277 st
->start_address
= addr
;
278 st
->current_prot
= prot
;
282 if (addr
>= st
->marker
[1].start_address
) {
284 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
289 static void walk_pte(struct pg_state
*st
, pmd_t
*pmdp
, unsigned long start
)
291 pte_t
*ptep
= pte_offset_kernel(pmdp
, 0UL);
295 for (i
= 0; i
< PTRS_PER_PTE
; i
++, ptep
++) {
296 addr
= start
+ i
* PAGE_SIZE
;
297 note_page(st
, addr
, 4, READ_ONCE(pte_val(*ptep
)));
301 static void walk_pmd(struct pg_state
*st
, pud_t
*pudp
, unsigned long start
)
303 pmd_t
*pmdp
= pmd_offset(pudp
, 0UL);
307 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmdp
++) {
308 pmd_t pmd
= READ_ONCE(*pmdp
);
310 addr
= start
+ i
* PMD_SIZE
;
311 if (pmd_none(pmd
) || pmd_sect(pmd
)) {
312 note_page(st
, addr
, 3, pmd_val(pmd
));
314 BUG_ON(pmd_bad(pmd
));
315 walk_pte(st
, pmdp
, addr
);
320 static void walk_pud(struct pg_state
*st
, pgd_t
*pgdp
, unsigned long start
)
322 pud_t
*pudp
= pud_offset(pgdp
, 0UL);
326 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pudp
++) {
327 pud_t pud
= READ_ONCE(*pudp
);
329 addr
= start
+ i
* PUD_SIZE
;
330 if (pud_none(pud
) || pud_sect(pud
)) {
331 note_page(st
, addr
, 2, pud_val(pud
));
333 BUG_ON(pud_bad(pud
));
334 walk_pmd(st
, pudp
, addr
);
339 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
342 pgd_t
*pgdp
= pgd_offset(mm
, 0UL);
346 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgdp
++) {
347 pgd_t pgd
= READ_ONCE(*pgdp
);
349 addr
= start
+ i
* PGDIR_SIZE
;
351 note_page(st
, addr
, 1, pgd_val(pgd
));
353 BUG_ON(pgd_bad(pgd
));
354 walk_pud(st
, pgdp
, addr
);
359 void ptdump_walk_pgd(struct seq_file
*m
, struct ptdump_info
*info
)
361 struct pg_state st
= {
363 .marker
= info
->markers
,
366 walk_pgd(&st
, info
->mm
, info
->base_addr
);
368 note_page(&st
, 0, 0, 0);
371 static void ptdump_initialize(void)
375 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
376 if (pg_level
[i
].bits
)
377 for (j
= 0; j
< pg_level
[i
].num
; j
++)
378 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
381 static struct ptdump_info kernel_ptdump_info
= {
383 .markers
= address_markers
,
384 .base_addr
= VA_START
,
387 void ptdump_check_wx(void)
389 struct pg_state st
= {
391 .marker
= (struct addr_marker
[]) {
398 walk_pgd(&st
, &init_mm
, VA_START
);
399 note_page(&st
, 0, 0, 0);
400 if (st
.wx_pages
|| st
.uxn_pages
)
401 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found, %lu non-UXN pages found\n",
402 st
.wx_pages
, st
.uxn_pages
);
404 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
407 static int ptdump_init(void)
410 return ptdump_debugfs_register(&kernel_ptdump_info
,
411 "kernel_page_tables");
413 device_initcall(ptdump_init
);