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>
18 #include <asm/memory.h>
19 #include <asm/pgtable.h>
20 #include <asm/ptdump.h>
22 static struct addr_marker address_markers
[] = {
23 { MODULES_VADDR
, "Modules" },
24 { PAGE_OFFSET
, "Kernel Mapping" },
25 { 0, "vmalloc() Area" },
26 { VMALLOC_END
, "vmalloc() End" },
27 { FIXADDR_START
, "Fixmap Area" },
28 { VECTORS_BASE
, "Vectors" },
29 { VECTORS_BASE
+ PAGE_SIZE
* 2, "Vectors End" },
33 #define pt_dump_seq_printf(m, fmt, args...) \
36 seq_printf(m, fmt, ##args); \
39 #define pt_dump_seq_puts(m, fmt) \
47 const struct addr_marker
*marker
;
48 unsigned long start_address
;
52 unsigned long wx_pages
;
53 const char *current_domain
;
65 static const struct prot_bits pte_bits
[] = {
89 .mask
= L_PTE_MT_MASK
,
90 .val
= L_PTE_MT_UNCACHED
,
93 .mask
= L_PTE_MT_MASK
,
94 .val
= L_PTE_MT_BUFFERABLE
,
95 .set
= "MEM/BUFFERABLE/WC",
97 .mask
= L_PTE_MT_MASK
,
98 .val
= L_PTE_MT_WRITETHROUGH
,
99 .set
= "MEM/CACHED/WT",
101 .mask
= L_PTE_MT_MASK
,
102 .val
= L_PTE_MT_WRITEBACK
,
103 .set
= "MEM/CACHED/WBRA",
104 #ifndef CONFIG_ARM_LPAE
106 .mask
= L_PTE_MT_MASK
,
107 .val
= L_PTE_MT_MINICACHE
,
108 .set
= "MEM/MINICACHE",
111 .mask
= L_PTE_MT_MASK
,
112 .val
= L_PTE_MT_WRITEALLOC
,
113 .set
= "MEM/CACHED/WBWA",
115 .mask
= L_PTE_MT_MASK
,
116 .val
= L_PTE_MT_DEV_SHARED
,
118 #ifndef CONFIG_ARM_LPAE
120 .mask
= L_PTE_MT_MASK
,
121 .val
= L_PTE_MT_DEV_NONSHARED
,
122 .set
= "DEV/NONSHARED",
125 .mask
= L_PTE_MT_MASK
,
126 .val
= L_PTE_MT_DEV_WC
,
129 .mask
= L_PTE_MT_MASK
,
130 .val
= L_PTE_MT_DEV_CACHED
,
135 static const struct prot_bits section_bits
[] = {
136 #ifdef CONFIG_ARM_LPAE
138 .mask
= PMD_SECT_USER
,
139 .val
= PMD_SECT_USER
,
142 .mask
= L_PMD_SECT_RDONLY
| PMD_SECT_AP2
,
143 .val
= L_PMD_SECT_RDONLY
| PMD_SECT_AP2
,
147 #elif __LINUX_ARM_ARCH__ >= 6
149 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
150 .val
= PMD_SECT_APX
| PMD_SECT_AP_WRITE
,
154 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
155 .val
= PMD_SECT_AP_WRITE
,
158 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
159 .val
= PMD_SECT_AP_READ
,
162 .mask
= PMD_SECT_APX
| PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
163 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
165 #else /* ARMv4/ARMv5 */
166 /* These are approximate */
168 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
173 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
174 .val
= PMD_SECT_AP_WRITE
,
177 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
178 .val
= PMD_SECT_AP_READ
,
181 .mask
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
182 .val
= PMD_SECT_AP_READ
| PMD_SECT_AP_WRITE
,
200 const struct prot_bits
*bits
;
203 const struct prot_bits
*ro_bit
;
204 const struct prot_bits
*nx_bit
;
207 static struct pg_level pg_level
[] = {
212 .bits
= section_bits
,
213 .num
= ARRAY_SIZE(section_bits
),
216 .num
= ARRAY_SIZE(pte_bits
),
220 static void dump_prot(struct pg_state
*st
, const struct prot_bits
*bits
, size_t num
)
224 for (i
= 0; i
< num
; i
++, bits
++) {
227 if ((st
->current_prot
& bits
->mask
) == bits
->val
)
233 pt_dump_seq_printf(st
->seq
, " %s", s
);
237 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
241 if ((st
->current_prot
& pg_level
[st
->level
].ro_bit
->mask
) ==
242 pg_level
[st
->level
].ro_bit
->val
)
244 if ((st
->current_prot
& pg_level
[st
->level
].nx_bit
->mask
) ==
245 pg_level
[st
->level
].nx_bit
->val
)
248 WARN_ONCE(1, "arm/mm: Found insecure W+X mapping at address %pS\n",
249 (void *)st
->start_address
);
251 st
->wx_pages
+= (addr
- st
->start_address
) / PAGE_SIZE
;
254 static void note_page(struct pg_state
*st
, unsigned long addr
,
255 unsigned int level
, u64 val
, const char *domain
)
257 static const char units
[] = "KMGTPE";
258 u64 prot
= val
& pg_level
[level
].mask
;
262 st
->current_prot
= prot
;
263 st
->current_domain
= domain
;
264 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
265 } else if (prot
!= st
->current_prot
|| level
!= st
->level
||
266 domain
!= st
->current_domain
||
267 addr
>= st
->marker
[1].start_address
) {
268 const char *unit
= units
;
271 if (st
->current_prot
) {
272 note_prot_wx(st
, addr
);
273 pt_dump_seq_printf(st
->seq
, "0x%08lx-0x%08lx ",
274 st
->start_address
, addr
);
276 delta
= (addr
- st
->start_address
) >> 10;
277 while (!(delta
& 1023) && unit
[1]) {
281 pt_dump_seq_printf(st
->seq
, "%9lu%c", delta
, *unit
);
282 if (st
->current_domain
)
283 pt_dump_seq_printf(st
->seq
, " %s",
285 if (pg_level
[st
->level
].bits
)
286 dump_prot(st
, pg_level
[st
->level
].bits
, pg_level
[st
->level
].num
);
287 pt_dump_seq_printf(st
->seq
, "\n");
290 if (addr
>= st
->marker
[1].start_address
) {
292 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n",
295 st
->start_address
= addr
;
296 st
->current_prot
= prot
;
297 st
->current_domain
= domain
;
302 static void walk_pte(struct pg_state
*st
, pmd_t
*pmd
, unsigned long start
,
305 pte_t
*pte
= pte_offset_kernel(pmd
, 0);
309 for (i
= 0; i
< PTRS_PER_PTE
; i
++, pte
++) {
310 addr
= start
+ i
* PAGE_SIZE
;
311 note_page(st
, addr
, 4, pte_val(*pte
), domain
);
315 static const char *get_domain_name(pmd_t
*pmd
)
317 #ifndef CONFIG_ARM_LPAE
318 switch (pmd_val(*pmd
) & PMD_DOMAIN_MASK
) {
319 case PMD_DOMAIN(DOMAIN_KERNEL
):
321 case PMD_DOMAIN(DOMAIN_USER
):
323 case PMD_DOMAIN(DOMAIN_IO
):
325 case PMD_DOMAIN(DOMAIN_VECTORS
):
334 static void walk_pmd(struct pg_state
*st
, pud_t
*pud
, unsigned long start
)
336 pmd_t
*pmd
= pmd_offset(pud
, 0);
341 for (i
= 0; i
< PTRS_PER_PMD
; i
++, pmd
++) {
342 addr
= start
+ i
* PMD_SIZE
;
343 domain
= get_domain_name(pmd
);
344 if (pmd_none(*pmd
) || pmd_large(*pmd
) || !pmd_present(*pmd
))
345 note_page(st
, addr
, 3, pmd_val(*pmd
), domain
);
347 walk_pte(st
, pmd
, addr
, domain
);
349 if (SECTION_SIZE
< PMD_SIZE
&& pmd_large(pmd
[1])) {
350 addr
+= SECTION_SIZE
;
352 domain
= get_domain_name(pmd
);
353 note_page(st
, addr
, 3, pmd_val(*pmd
), domain
);
358 static void walk_pud(struct pg_state
*st
, pgd_t
*pgd
, unsigned long start
)
360 pud_t
*pud
= pud_offset(pgd
, 0);
364 for (i
= 0; i
< PTRS_PER_PUD
; i
++, pud
++) {
365 addr
= start
+ i
* PUD_SIZE
;
366 if (!pud_none(*pud
)) {
367 walk_pmd(st
, pud
, addr
);
369 note_page(st
, addr
, 2, pud_val(*pud
), NULL
);
374 static void walk_pgd(struct pg_state
*st
, struct mm_struct
*mm
,
377 pgd_t
*pgd
= pgd_offset(mm
, 0UL);
381 for (i
= 0; i
< PTRS_PER_PGD
; i
++, pgd
++) {
382 addr
= start
+ i
* PGDIR_SIZE
;
383 if (!pgd_none(*pgd
)) {
384 walk_pud(st
, pgd
, addr
);
386 note_page(st
, addr
, 1, pgd_val(*pgd
), NULL
);
391 void ptdump_walk_pgd(struct seq_file
*m
, struct ptdump_info
*info
)
393 struct pg_state st
= {
395 .marker
= info
->markers
,
399 walk_pgd(&st
, info
->mm
, info
->base_addr
);
400 note_page(&st
, 0, 0, 0, NULL
);
403 static void ptdump_initialize(void)
407 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
408 if (pg_level
[i
].bits
)
409 for (j
= 0; j
< pg_level
[i
].num
; j
++) {
410 pg_level
[i
].mask
|= pg_level
[i
].bits
[j
].mask
;
411 if (pg_level
[i
].bits
[j
].ro_bit
)
412 pg_level
[i
].ro_bit
= &pg_level
[i
].bits
[j
];
413 if (pg_level
[i
].bits
[j
].nx_bit
)
414 pg_level
[i
].nx_bit
= &pg_level
[i
].bits
[j
];
417 address_markers
[2].start_address
= VMALLOC_START
;
420 static struct ptdump_info kernel_ptdump_info
= {
422 .markers
= address_markers
,
426 void ptdump_check_wx(void)
428 struct pg_state st
= {
430 .marker
= (struct addr_marker
[]) {
437 walk_pgd(&st
, &init_mm
, 0);
438 note_page(&st
, 0, 0, 0, NULL
);
440 pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n",
443 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
446 static int ptdump_init(void)
449 ptdump_debugfs_register(&kernel_ptdump_info
, "kernel_page_tables");
452 __initcall(ptdump_init
);