Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / arch / riscv / mm / ptdump.c
blob7eab76a931061010c0b16bbc6da458827658f94f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2019 SiFive
4 */
6 #include <linux/init.h>
7 #include <linux/debugfs.h>
8 #include <linux/seq_file.h>
9 #include <linux/ptdump.h>
11 #include <asm/ptdump.h>
12 #include <asm/pgtable.h>
13 #include <asm/kasan.h>
15 #define pt_dump_seq_printf(m, fmt, args...) \
16 ({ \
17 if (m) \
18 seq_printf(m, fmt, ##args); \
21 #define pt_dump_seq_puts(m, fmt) \
22 ({ \
23 if (m) \
24 seq_printf(m, fmt); \
28 * The page dumper groups page table entries of the same type into a single
29 * description. It uses pg_state to track the range information while
30 * iterating over the pte entries. When the continuity is broken it then
31 * dumps out a description of the range.
33 struct pg_state {
34 struct ptdump_state ptdump;
35 struct seq_file *seq;
36 const struct addr_marker *marker;
37 unsigned long start_address;
38 unsigned long start_pa;
39 unsigned long last_pa;
40 int level;
41 u64 current_prot;
42 bool check_wx;
43 unsigned long wx_pages;
46 /* Address marker */
47 struct addr_marker {
48 unsigned long start_address;
49 const char *name;
52 static struct addr_marker address_markers[] = {
53 #ifdef CONFIG_KASAN
54 {KASAN_SHADOW_START, "Kasan shadow start"},
55 {KASAN_SHADOW_END, "Kasan shadow end"},
56 #endif
57 {FIXADDR_START, "Fixmap start"},
58 {FIXADDR_TOP, "Fixmap end"},
59 {PCI_IO_START, "PCI I/O start"},
60 {PCI_IO_END, "PCI I/O end"},
61 #ifdef CONFIG_SPARSEMEM_VMEMMAP
62 {VMEMMAP_START, "vmemmap start"},
63 {VMEMMAP_END, "vmemmap end"},
64 #endif
65 {VMALLOC_START, "vmalloc() area"},
66 {VMALLOC_END, "vmalloc() end"},
67 {PAGE_OFFSET, "Linear mapping"},
68 {-1, NULL},
71 /* Page Table Entry */
72 struct prot_bits {
73 u64 mask;
74 u64 val;
75 const char *set;
76 const char *clear;
79 static const struct prot_bits pte_bits[] = {
81 .mask = _PAGE_SOFT,
82 .val = _PAGE_SOFT,
83 .set = "RSW",
84 .clear = " ",
85 }, {
86 .mask = _PAGE_DIRTY,
87 .val = _PAGE_DIRTY,
88 .set = "D",
89 .clear = ".",
90 }, {
91 .mask = _PAGE_ACCESSED,
92 .val = _PAGE_ACCESSED,
93 .set = "A",
94 .clear = ".",
95 }, {
96 .mask = _PAGE_GLOBAL,
97 .val = _PAGE_GLOBAL,
98 .set = "G",
99 .clear = ".",
100 }, {
101 .mask = _PAGE_USER,
102 .val = _PAGE_USER,
103 .set = "U",
104 .clear = ".",
105 }, {
106 .mask = _PAGE_EXEC,
107 .val = _PAGE_EXEC,
108 .set = "X",
109 .clear = ".",
110 }, {
111 .mask = _PAGE_WRITE,
112 .val = _PAGE_WRITE,
113 .set = "W",
114 .clear = ".",
115 }, {
116 .mask = _PAGE_READ,
117 .val = _PAGE_READ,
118 .set = "R",
119 .clear = ".",
120 }, {
121 .mask = _PAGE_PRESENT,
122 .val = _PAGE_PRESENT,
123 .set = "V",
124 .clear = ".",
128 /* Page Level */
129 struct pg_level {
130 const char *name;
131 u64 mask;
134 static struct pg_level pg_level[] = {
135 { /* pgd */
136 .name = "PGD",
137 }, { /* p4d */
138 .name = (CONFIG_PGTABLE_LEVELS > 4) ? "P4D" : "PGD",
139 }, { /* pud */
140 .name = (CONFIG_PGTABLE_LEVELS > 3) ? "PUD" : "PGD",
141 }, { /* pmd */
142 .name = (CONFIG_PGTABLE_LEVELS > 2) ? "PMD" : "PGD",
143 }, { /* pte */
144 .name = "PTE",
148 static void dump_prot(struct pg_state *st)
150 unsigned int i;
152 for (i = 0; i < ARRAY_SIZE(pte_bits); i++) {
153 const char *s;
155 if ((st->current_prot & pte_bits[i].mask) == pte_bits[i].val)
156 s = pte_bits[i].set;
157 else
158 s = pte_bits[i].clear;
160 if (s)
161 pt_dump_seq_printf(st->seq, " %s", s);
165 #ifdef CONFIG_64BIT
166 #define ADDR_FORMAT "0x%016lx"
167 #else
168 #define ADDR_FORMAT "0x%08lx"
169 #endif
170 static void dump_addr(struct pg_state *st, unsigned long addr)
172 static const char units[] = "KMGTPE";
173 const char *unit = units;
174 unsigned long delta;
176 pt_dump_seq_printf(st->seq, ADDR_FORMAT "-" ADDR_FORMAT " ",
177 st->start_address, addr);
179 pt_dump_seq_printf(st->seq, " " ADDR_FORMAT " ", st->start_pa);
180 delta = (addr - st->start_address) >> 10;
182 while (!(delta & 1023) && unit[1]) {
183 delta >>= 10;
184 unit++;
187 pt_dump_seq_printf(st->seq, "%9lu%c %s", delta, *unit,
188 pg_level[st->level].name);
191 static void note_prot_wx(struct pg_state *st, unsigned long addr)
193 if (!st->check_wx)
194 return;
196 if ((st->current_prot & (_PAGE_WRITE | _PAGE_EXEC)) !=
197 (_PAGE_WRITE | _PAGE_EXEC))
198 return;
200 WARN_ONCE(1, "riscv/mm: Found insecure W+X mapping at address %p/%pS\n",
201 (void *)st->start_address, (void *)st->start_address);
203 st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
206 static void note_page(struct ptdump_state *pt_st, unsigned long addr,
207 int level, unsigned long val)
209 struct pg_state *st = container_of(pt_st, struct pg_state, ptdump);
210 u64 pa = PFN_PHYS(pte_pfn(__pte(val)));
211 u64 prot = 0;
213 if (level >= 0)
214 prot = val & pg_level[level].mask;
216 if (st->level == -1) {
217 st->level = level;
218 st->current_prot = prot;
219 st->start_address = addr;
220 st->start_pa = pa;
221 st->last_pa = pa;
222 pt_dump_seq_printf(st->seq, "---[ %s ]---\n", st->marker->name);
223 } else if (prot != st->current_prot ||
224 level != st->level || addr >= st->marker[1].start_address) {
225 if (st->current_prot) {
226 note_prot_wx(st, addr);
227 dump_addr(st, addr);
228 dump_prot(st);
229 pt_dump_seq_puts(st->seq, "\n");
232 while (addr >= st->marker[1].start_address) {
233 st->marker++;
234 pt_dump_seq_printf(st->seq, "---[ %s ]---\n",
235 st->marker->name);
238 st->start_address = addr;
239 st->start_pa = pa;
240 st->last_pa = pa;
241 st->current_prot = prot;
242 st->level = level;
243 } else {
244 st->last_pa = pa;
248 static void ptdump_walk(struct seq_file *s)
250 struct pg_state st = {
251 .seq = s,
252 .marker = address_markers,
253 .level = -1,
254 .ptdump = {
255 .note_page = note_page,
256 .range = (struct ptdump_range[]) {
257 {KERN_VIRT_START, ULONG_MAX},
258 {0, 0}
263 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
266 void ptdump_check_wx(void)
268 struct pg_state st = {
269 .seq = NULL,
270 .marker = (struct addr_marker[]) {
271 {0, NULL},
272 {-1, NULL},
274 .level = -1,
275 .check_wx = true,
276 .ptdump = {
277 .note_page = note_page,
278 .range = (struct ptdump_range[]) {
279 {KERN_VIRT_START, ULONG_MAX},
280 {0, 0}
285 ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
287 if (st.wx_pages)
288 pr_warn("Checked W+X mappings: failed, %lu W+X pages found\n",
289 st.wx_pages);
290 else
291 pr_info("Checked W+X mappings: passed, no W+X pages found\n");
294 static int ptdump_show(struct seq_file *m, void *v)
296 ptdump_walk(m);
298 return 0;
301 DEFINE_SHOW_ATTRIBUTE(ptdump);
303 static int ptdump_init(void)
305 unsigned int i, j;
307 for (i = 0; i < ARRAY_SIZE(pg_level); i++)
308 for (j = 0; j < ARRAY_SIZE(pte_bits); j++)
309 pg_level[i].mask |= pte_bits[j].mask;
311 debugfs_create_file("kernel_page_tables", 0400, NULL, NULL,
312 &ptdump_fops);
314 return 0;
317 device_initcall(ptdump_init);