1 // SPDX-License-Identifier: GPL-2.0-only
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 implementation:
7 * (C) Copyright 2008 Intel Corporation
9 * Author: Arjan van de Ven <arjan@linux.intel.com>
11 #include <linux/debugfs.h>
14 #include <linux/seq_file.h>
16 #include <asm/domain.h>
17 #include <asm/fixmap.h>
19 #include <asm/ptdump.h>
21 static struct addr_marker address_markers
[] = {
23 { KASAN_SHADOW_START
, "Kasan shadow start"},
24 { KASAN_SHADOW_END
, "Kasan shadow end"},
26 { MODULES_VADDR
, "Modules" },
27 { PAGE_OFFSET
, "Kernel Mapping" },
28 { 0, "vmalloc() Area" },
29 { FDT_FIXED_BASE
, "FDT Area" },
30 { FIXADDR_START
, "Fixmap Area" },
31 { VECTORS_BASE
, "Vectors" },
32 { VECTORS_BASE
+ PAGE_SIZE
* 2, "Vectors End" },
36 #define pt_dump_seq_printf(m, fmt, args...) \
39 seq_printf(m, fmt, ##args); \
42 #define pt_dump_seq_puts(m, fmt) \
50 const struct addr_marker
*marker
;
51 unsigned long start_address
;
55 unsigned long wx_pages
;
56 const char *current_domain
;
68 static const struct prot_bits pte_bits
[] = {
92 .mask
= L_PTE_MT_MASK
,
93 .val
= L_PTE_MT_UNCACHED
,
96 .mask
= L_PTE_MT_MASK
,
97 .val
= L_PTE_MT_BUFFERABLE
,
98 .set
= "MEM/BUFFERABLE/WC",
100 .mask
= L_PTE_MT_MASK
,
101 .val
= L_PTE_MT_WRITETHROUGH
,
102 .set
= "MEM/CACHED/WT",
104 .mask
= L_PTE_MT_MASK
,
105 .val
= L_PTE_MT_WRITEBACK
,
106 .set
= "MEM/CACHED/WBRA",
107 #ifndef CONFIG_ARM_LPAE
109 .mask
= L_PTE_MT_MASK
,
110 .val
= L_PTE_MT_MINICACHE
,
111 .set
= "MEM/MINICACHE",
114 .mask
= L_PTE_MT_MASK
,
115 .val
= L_PTE_MT_WRITEALLOC
,
116 .set
= "MEM/CACHED/WBWA",
118 .mask
= L_PTE_MT_MASK
,
119 .val
= L_PTE_MT_DEV_SHARED
,
121 #ifndef CONFIG_ARM_LPAE
123 .mask
= L_PTE_MT_MASK
,
124 .val
= L_PTE_MT_DEV_NONSHARED
,
125 .set
= "DEV/NONSHARED",
128 .mask
= L_PTE_MT_MASK
,
129 .val
= L_PTE_MT_DEV_WC
,
132 .mask
= L_PTE_MT_MASK
,
133 .val
= L_PTE_MT_DEV_CACHED
,
138 static const struct prot_bits section_bits
[] = {
139 #ifdef CONFIG_ARM_LPAE
141 .mask
= PMD_SECT_USER
,
142 .val
= PMD_SECT_USER
,
145 .mask
= L_PMD_SECT_RDONLY
| PMD_SECT_AP2
,
146 .val
= L_PMD_SECT_RDONLY
| PMD_SECT_AP2
,
150 #elif __LINUX_ARM_ARCH__ >= 6
152 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
153 .val
= PMD_SECT_APX
| PMD_SECT_AP_WRITE
,
157 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
158 .val
= PMD_SECT_AP_WRITE
,
161 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
162 .val
= PMD_SECT_AP_READ
,
165 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
166 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
168 #else /* ARMv4/ARMv5 */
169 /* These are approximate */
171 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
176 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
177 .val
= PMD_SECT_AP_WRITE
,
180 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
181 .val
= PMD_SECT_AP_READ
,
184 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
185 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
204 const struct prot_bits
*bits
;
207 const struct prot_bits
*ro_bit
;
208 const struct prot_bits
*nx_bit
;
211 static struct pg_level pg_level
[] = {
217 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
218 .bits
= section_bits
,
219 .num
= ARRAY_SIZE(section_bits
),
223 .num
= ARRAY_SIZE(pte_bits
),
227 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
, size_t num
)
231 for (i
= 0; i
< num
; i
++, bits
++) {
234 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
240 pt_dump_seq_printf(st
->seq
, " %s", s
);
244 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
248 if ((st
->current_prot
& pg_level
[st
->level
].ro_bit
->mask
) ==
249 pg_level
[st
->level
].ro_bit
->val
)
251 if ((st
->current_prot
& pg_level
[st
->level
].nx_bit
->mask
) ==
252 pg_level
[st
->level
].nx_bit
->val
)
255 WARN_ONCE(1, "arm/mm: Found insecure W+X mapping at address %pS\n",
256 (void *)st
->start_address
);
258 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
261 static void note_page(struct pg_state
*st
, unsigned long addr
,
262 unsigned int level
, u64 val
, const char *domain
)
264 static const char units
[] = "KMGTPE";
265 u64 prot
= val
& pg_level
[level
].mask
;
269 st
->current_prot
= prot
;
270 st
->current_domain
= domain
;
271 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
272 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
273 domain
!= st
->current_domain
||
274 addr
>= st
->marker
[1].start_address
) {
275 const char *unit
= units
;
278 if (st
->current_prot
) {
279 note_prot_wx(st
, addr
);
280 pt_dump_seq_printf(st
->seq
, "0x%08lx-0x%08lx ",
281 st
->start_address
, addr
);
283 delta
= (addr
- st
->start_address
) >> 10;
284 while (!(delta
& 1023) && unit
[1]) {
288 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
289 pg_level
[st
->level
].name
);
290 if (st
->current_domain
)
291 pt_dump_seq_printf(st
->seq
, " %s",
293 if (pg_level
[st
->level
].bits
)
294 dump_prot(st
, pg_level
[st
->level
].bits
, pg_level
[st
->level
].num
);
295 pt_dump_seq_printf(st
->seq
, "\n");
298 if (addr
>= st
->marker
[1].start_address
) {
300 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n",
303 st
->start_address
= addr
;
304 st
->current_prot
= prot
;
305 st
->current_domain
= domain
;
310 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
,
313 pte_t
*pte
= pte_offset_kernel(pmd
, 0);
317 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
318 addr
= start
+ i
* PAGE_SIZE
;
319 note_page(st
, addr
, 5, pte_val(*pte
), domain
);
323 static const char *get_domain_name(pmd_t
*pmd
)
325 #ifndef CONFIG_ARM_LPAE
326 switch (pmd_val(*pmd
) & PMD_DOMAIN_MASK
) {
327 case PMD_DOMAIN(DOMAIN_KERNEL
):
329 case PMD_DOMAIN(DOMAIN_USER
):
331 case PMD_DOMAIN(DOMAIN_IO
):
333 case PMD_DOMAIN(DOMAIN_VECTORS
):
342 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
344 pmd_t
*pmd
= pmd_offset(pud
, 0);
349 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
350 addr
= start
+ i
* PMD_SIZE
;
351 domain
= get_domain_name(pmd
);
352 if (pmd_none(*pmd
) || pmd_leaf(*pmd
) || !pmd_present(*pmd
))
353 note_page(st
, addr
, 4, pmd_val(*pmd
), domain
);
355 walk_pte(st
, pmd
, addr
, domain
);
357 if (SECTION_SIZE
< PMD_SIZE
&& pmd_leaf(pmd
[1])) {
358 addr
+= SECTION_SIZE
;
360 domain
= get_domain_name(pmd
);
361 note_page(st
, addr
, 4, pmd_val(*pmd
), domain
);
366 static void walk_pud(struct pg_state
*st
, p4d_t
*p4d
, unsigned long start
)
368 pud_t
*pud
= pud_offset(p4d
, 0);
372 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
373 addr
= start
+ i
* PUD_SIZE
;
374 if (!pud_none(*pud
)) {
375 walk_pmd(st
, pud
, addr
);
377 note_page(st
, addr
, 3, pud_val(*pud
), NULL
);
382 static void walk_p4d(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
384 p4d_t
*p4d
= p4d_offset(pgd
, 0);
388 for (i
= 0; i
< PTRS_PER_P4D
; i
++, p4d
++) {
389 addr
= start
+ i
* P4D_SIZE
;
390 if (!p4d_none(*p4d
)) {
391 walk_pud(st
, p4d
, addr
);
393 note_page(st
, addr
, 2, p4d_val(*p4d
), NULL
);
398 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
401 pgd_t
*pgd
= pgd_offset(mm
, 0UL);
405 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
406 addr
= start
+ i
* PGDIR_SIZE
;
407 if (!pgd_none(*pgd
)) {
408 walk_p4d(st
, pgd
, addr
);
410 note_page(st
, addr
, 1, pgd_val(*pgd
), NULL
);
415 void ptdump_walk_pgd(struct seq_file
*m
, struct ptdump_info
*info
)
417 struct pg_state st
= {
419 .marker
= info
->markers
,
423 walk_pgd(&st
, info
->mm
, info
->base_addr
);
424 note_page(&st
, 0, 0, 0, NULL
);
427 static void __init
ptdump_initialize(void)
431 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
432 if (pg_level
[i
].bits
)
433 for (j
= 0; j
< pg_level
[i
].num
; j
++) {
434 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
435 if (pg_level
[i
].bits
[j
].ro_bit
)
436 pg_level
[i
].ro_bit
= &pg_level
[i
].bits
[j
];
437 if (pg_level
[i
].bits
[j
].nx_bit
)
438 pg_level
[i
].nx_bit
= &pg_level
[i
].bits
[j
];
441 address_markers
[4].start_address
= VMALLOC_START
;
443 address_markers
[2].start_address
= VMALLOC_START
;
447 static struct ptdump_info kernel_ptdump_info
= {
449 .markers
= address_markers
,
453 void ptdump_check_wx(void)
455 struct pg_state st
= {
457 .marker
= (struct addr_marker
[]) {
464 walk_pgd(&st
, &init_mm
, 0);
465 note_page(&st
, 0, 0, 0, NULL
);
467 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
470 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
473 static int __init
ptdump_init(void)
476 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
479 __initcall(ptdump_init
);