1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2019 SiFive
7 #include <linux/init.h>
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/ptdump.h>
12 #include <asm/ptdump.h>
13 #include <linux/pgtable.h>
14 #include <asm/kasan.h>
16 #define pt_dump_seq_printf(m, fmt, args...) \
19 seq_printf(m, fmt, ##args); \
22 #define pt_dump_seq_puts(m, fmt) \
29 * The page dumper groups page table entries of the same type into a single
30 * description. It uses pg_state to track the range information while
31 * iterating over the pte entries. When the continuity is broken it then
32 * dumps out a description of the range.
35 struct ptdump_state ptdump
;
37 const struct addr_marker
*marker
;
38 unsigned long start_address
;
39 unsigned long start_pa
;
40 unsigned long last_pa
;
44 unsigned long wx_pages
;
49 unsigned long start_address
;
53 /* Private information for debugfs */
56 const struct addr_marker
*markers
;
57 unsigned long base_addr
;
61 static struct addr_marker address_markers
[] = {
63 {KASAN_SHADOW_START
, "Kasan shadow start"},
64 {KASAN_SHADOW_END
, "Kasan shadow end"},
66 {FIXADDR_START
, "Fixmap start"},
67 {FIXADDR_TOP
, "Fixmap end"},
68 {PCI_IO_START
, "PCI I/O start"},
69 {PCI_IO_END
, "PCI I/O end"},
70 #ifdef CONFIG_SPARSEMEM_VMEMMAP
71 {VMEMMAP_START
, "vmemmap start"},
72 {VMEMMAP_END
, "vmemmap end"},
74 {VMALLOC_START
, "vmalloc() area"},
75 {VMALLOC_END
, "vmalloc() end"},
76 {PAGE_OFFSET
, "Linear mapping"},
80 static struct ptd_mm_info kernel_ptd_info
= {
82 .markers
= address_markers
,
83 .base_addr
= KERN_VIRT_START
,
88 static struct addr_marker efi_addr_markers
[] = {
89 { 0, "UEFI runtime start" },
90 { SZ_1G
, "UEFI runtime end" },
94 static struct ptd_mm_info efi_ptd_info
= {
96 .markers
= efi_addr_markers
,
102 /* Page Table Entry */
110 static const struct prot_bits pte_bits
[] = {
122 .mask
= _PAGE_ACCESSED
,
123 .val
= _PAGE_ACCESSED
,
127 .mask
= _PAGE_GLOBAL
,
152 .mask
= _PAGE_PRESENT
,
153 .val
= _PAGE_PRESENT
,
165 static struct pg_level pg_level
[] = {
169 .name
= (CONFIG_PGTABLE_LEVELS
> 4) ? "P4D" : "PGD",
171 .name
= (CONFIG_PGTABLE_LEVELS
> 3) ? "PUD" : "PGD",
173 .name
= (CONFIG_PGTABLE_LEVELS
> 2) ? "PMD" : "PGD",
179 static void dump_prot(struct pg_state
*st
)
183 for (i
= 0; i
< ARRAY_SIZE(pte_bits
); i
++) {
186 if ((st
->current_prot
& pte_bits
[i
].mask
) == pte_bits
[i
].val
)
189 s
= pte_bits
[i
].clear
;
192 pt_dump_seq_printf(st
->seq
, " %s", s
);
197 #define ADDR_FORMAT "0x%016lx"
199 #define ADDR_FORMAT "0x%08lx"
201 static void dump_addr(struct pg_state
*st
, unsigned long addr
)
203 static const char units
[] = "KMGTPE";
204 const char *unit
= units
;
207 pt_dump_seq_printf(st
->seq
, ADDR_FORMAT
"-" ADDR_FORMAT
" ",
208 st
->start_address
, addr
);
210 pt_dump_seq_printf(st
->seq
, " " ADDR_FORMAT
" ", st
->start_pa
);
211 delta
= (addr
- st
->start_address
) >> 10;
213 while (!(delta
& 1023) && unit
[1]) {
218 pt_dump_seq_printf(st
->seq
, "%9lu%c %s", delta
, *unit
,
219 pg_level
[st
->level
].name
);
222 static void note_prot_wx(struct pg_state
*st
, unsigned long addr
)
227 if ((st
->current_prot
& (_PAGE_WRITE
| _PAGE_EXEC
)) !=
228 (_PAGE_WRITE
| _PAGE_EXEC
))
231 WARN_ONCE(1, "riscv/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 ptdump_state
*pt_st
, unsigned long addr
,
240 struct pg_state
*st
= container_of(pt_st
, struct pg_state
, ptdump
);
241 u64 pa
= PFN_PHYS(pte_pfn(__pte(val
)));
245 prot
= val
& pg_level
[level
].mask
;
247 if (st
->level
== -1) {
249 st
->current_prot
= prot
;
250 st
->start_address
= addr
;
253 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n", st
->marker
->name
);
254 } else if (prot
!= st
->current_prot
||
255 level
!= st
->level
|| addr
>= st
->marker
[1].start_address
) {
256 if (st
->current_prot
) {
257 note_prot_wx(st
, addr
);
260 pt_dump_seq_puts(st
->seq
, "\n");
263 while (addr
>= st
->marker
[1].start_address
) {
265 pt_dump_seq_printf(st
->seq
, "---[ %s ]---\n",
269 st
->start_address
= addr
;
272 st
->current_prot
= prot
;
279 static void ptdump_walk(struct seq_file
*s
, struct ptd_mm_info
*pinfo
)
281 struct pg_state st
= {
283 .marker
= pinfo
->markers
,
286 .note_page
= note_page
,
287 .range
= (struct ptdump_range
[]) {
288 {pinfo
->base_addr
, pinfo
->end
},
294 ptdump_walk_pgd(&st
.ptdump
, pinfo
->mm
, NULL
);
297 void ptdump_check_wx(void)
299 struct pg_state st
= {
301 .marker
= (struct addr_marker
[]) {
308 .note_page
= note_page
,
309 .range
= (struct ptdump_range
[]) {
310 {KERN_VIRT_START
, ULONG_MAX
},
316 ptdump_walk_pgd(&st
.ptdump
, &init_mm
, NULL
);
319 pr_warn("Checked W+X mappings: failed, %lu W+X pages found\n",
322 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
325 static int ptdump_show(struct seq_file
*m
, void *v
)
327 ptdump_walk(m
, m
->private);
332 DEFINE_SHOW_ATTRIBUTE(ptdump
);
334 static int ptdump_init(void)
338 for (i
= 0; i
< ARRAY_SIZE(pg_level
); i
++)
339 for (j
= 0; j
< ARRAY_SIZE(pte_bits
); j
++)
340 pg_level
[i
].mask
|= pte_bits
[j
].mask
;
342 debugfs_create_file("kernel_page_tables", 0400, NULL
, &kernel_ptd_info
,
345 if (efi_enabled(EFI_RUNTIME_SERVICES
))
346 debugfs_create_file("efi_page_tables", 0400, NULL
, &efi_ptd_info
,
353 device_initcall(ptdump_init
);