xref: /openbmc/linux/arch/s390/mm/dump_pagetables.c (revision 44ecda71)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/set_memory.h>
3 #include <linux/ptdump.h>
4 #include <linux/seq_file.h>
5 #include <linux/debugfs.h>
6 #include <linux/mm.h>
7 #include <linux/kfence.h>
8 #include <linux/kasan.h>
9 #include <asm/ptdump.h>
10 #include <asm/kasan.h>
11 #include <asm/abs_lowcore.h>
12 #include <asm/nospec-branch.h>
13 #include <asm/sections.h>
14 #include <asm/maccess.h>
15 
16 static unsigned long max_addr;
17 
18 struct addr_marker {
19 	unsigned long start_address;
20 	const char *name;
21 };
22 
23 enum address_markers_idx {
24 	IDENTITY_BEFORE_NR = 0,
25 	IDENTITY_BEFORE_END_NR,
26 	AMODE31_START_NR,
27 	AMODE31_END_NR,
28 	KERNEL_START_NR,
29 	KERNEL_END_NR,
30 #ifdef CONFIG_KFENCE
31 	KFENCE_START_NR,
32 	KFENCE_END_NR,
33 #endif
34 	IDENTITY_AFTER_NR,
35 	IDENTITY_AFTER_END_NR,
36 #ifdef CONFIG_KASAN
37 	KASAN_SHADOW_START_NR,
38 	KASAN_SHADOW_END_NR,
39 #endif
40 	VMEMMAP_NR,
41 	VMEMMAP_END_NR,
42 	VMALLOC_NR,
43 	VMALLOC_END_NR,
44 	MODULES_NR,
45 	MODULES_END_NR,
46 	ABS_LOWCORE_NR,
47 	ABS_LOWCORE_END_NR,
48 	MEMCPY_REAL_NR,
49 	MEMCPY_REAL_END_NR,
50 };
51 
52 static struct addr_marker address_markers[] = {
53 	[IDENTITY_BEFORE_NR]	= {0, "Identity Mapping Start"},
54 	[IDENTITY_BEFORE_END_NR] = {(unsigned long)_stext, "Identity Mapping End"},
55 	[AMODE31_START_NR]	= {0, "Amode31 Area Start"},
56 	[AMODE31_END_NR]	= {0, "Amode31 Area End"},
57 	[KERNEL_START_NR]	= {(unsigned long)_stext, "Kernel Image Start"},
58 	[KERNEL_END_NR]		= {(unsigned long)_end, "Kernel Image End"},
59 #ifdef CONFIG_KFENCE
60 	[KFENCE_START_NR]	= {0, "KFence Pool Start"},
61 	[KFENCE_END_NR]		= {0, "KFence Pool End"},
62 #endif
63 	[IDENTITY_AFTER_NR]	= {(unsigned long)_end, "Identity Mapping Start"},
64 	[IDENTITY_AFTER_END_NR]	= {0, "Identity Mapping End"},
65 #ifdef CONFIG_KASAN
66 	[KASAN_SHADOW_START_NR]	= {KASAN_SHADOW_START, "Kasan Shadow Start"},
67 	[KASAN_SHADOW_END_NR]	= {KASAN_SHADOW_END, "Kasan Shadow End"},
68 #endif
69 	[VMEMMAP_NR]		= {0, "vmemmap Area Start"},
70 	[VMEMMAP_END_NR]	= {0, "vmemmap Area End"},
71 	[VMALLOC_NR]		= {0, "vmalloc Area Start"},
72 	[VMALLOC_END_NR]	= {0, "vmalloc Area End"},
73 	[MODULES_NR]		= {0, "Modules Area Start"},
74 	[MODULES_END_NR]	= {0, "Modules Area End"},
75 	[ABS_LOWCORE_NR]	= {0, "Lowcore Area Start"},
76 	[ABS_LOWCORE_END_NR]	= {0, "Lowcore Area End"},
77 	[MEMCPY_REAL_NR]	= {0, "Real Memory Copy Area Start"},
78 	[MEMCPY_REAL_END_NR]	= {0, "Real Memory Copy Area End"},
79 	{ -1, NULL }
80 };
81 
82 struct pg_state {
83 	struct ptdump_state ptdump;
84 	struct seq_file *seq;
85 	int level;
86 	unsigned int current_prot;
87 	bool check_wx;
88 	unsigned long wx_pages;
89 	unsigned long start_address;
90 	const struct addr_marker *marker;
91 };
92 
93 #define pt_dump_seq_printf(m, fmt, args...)	\
94 ({						\
95 	struct seq_file *__m = (m);		\
96 						\
97 	if (__m)				\
98 		seq_printf(__m, fmt, ##args);	\
99 })
100 
101 #define pt_dump_seq_puts(m, fmt)		\
102 ({						\
103 	struct seq_file *__m = (m);		\
104 						\
105 	if (__m)				\
106 		seq_printf(__m, fmt);		\
107 })
108 
109 static void print_prot(struct seq_file *m, unsigned int pr, int level)
110 {
111 	static const char * const level_name[] =
112 		{ "ASCE", "PGD", "PUD", "PMD", "PTE" };
113 
114 	pt_dump_seq_printf(m, "%s ", level_name[level]);
115 	if (pr & _PAGE_INVALID) {
116 		pt_dump_seq_printf(m, "I\n");
117 		return;
118 	}
119 	pt_dump_seq_puts(m, (pr & _PAGE_PROTECT) ? "RO " : "RW ");
120 	pt_dump_seq_puts(m, (pr & _PAGE_NOEXEC) ? "NX\n" : "X\n");
121 }
122 
123 static void note_prot_wx(struct pg_state *st, unsigned long addr)
124 {
125 #ifdef CONFIG_DEBUG_WX
126 	if (!st->check_wx)
127 		return;
128 	if (st->current_prot & _PAGE_INVALID)
129 		return;
130 	if (st->current_prot & _PAGE_PROTECT)
131 		return;
132 	if (st->current_prot & _PAGE_NOEXEC)
133 		return;
134 	/*
135 	 * The first lowcore page is W+X if spectre mitigations are using
136 	 * trampolines or the BEAR enhancements facility is not installed,
137 	 * in which case we have two lpswe instructions in lowcore that need
138 	 * to be executable.
139 	 */
140 	if (addr == PAGE_SIZE && (nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)))
141 		return;
142 	WARN_ONCE(1, "s390/mm: Found insecure W+X mapping at address %pS\n",
143 		  (void *)st->start_address);
144 	st->wx_pages += (addr - st->start_address) / PAGE_SIZE;
145 #endif /* CONFIG_DEBUG_WX */
146 }
147 
148 static void note_page(struct ptdump_state *pt_st, unsigned long addr, int level, u64 val)
149 {
150 	int width = sizeof(unsigned long) * 2;
151 	static const char units[] = "KMGTPE";
152 	const char *unit = units;
153 	unsigned long delta;
154 	struct pg_state *st;
155 	struct seq_file *m;
156 	unsigned int prot;
157 
158 	st = container_of(pt_st, struct pg_state, ptdump);
159 	m = st->seq;
160 	prot = val & (_PAGE_PROTECT | _PAGE_NOEXEC);
161 	if (level == 4 && (val & _PAGE_INVALID))
162 		prot = _PAGE_INVALID;
163 	/* For pmd_none() & friends val gets passed as zero. */
164 	if (level != 4 && !val)
165 		prot = _PAGE_INVALID;
166 	/* Final flush from generic code. */
167 	if (level == -1)
168 		addr = max_addr;
169 	if (st->level == -1) {
170 		pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
171 		st->start_address = addr;
172 		st->current_prot = prot;
173 		st->level = level;
174 	} else if (prot != st->current_prot || level != st->level ||
175 		   addr >= st->marker[1].start_address) {
176 		note_prot_wx(st, addr);
177 		pt_dump_seq_printf(m, "0x%0*lx-0x%0*lx ",
178 				   width, st->start_address,
179 				   width, addr);
180 		delta = (addr - st->start_address) >> 10;
181 		while (!(delta & 0x3ff) && unit[1]) {
182 			delta >>= 10;
183 			unit++;
184 		}
185 		pt_dump_seq_printf(m, "%9lu%c ", delta, *unit);
186 		print_prot(m, st->current_prot, st->level);
187 		while (addr >= st->marker[1].start_address) {
188 			st->marker++;
189 			pt_dump_seq_printf(m, "---[ %s ]---\n", st->marker->name);
190 		}
191 		st->start_address = addr;
192 		st->current_prot = prot;
193 		st->level = level;
194 	}
195 }
196 
197 #ifdef CONFIG_DEBUG_WX
198 void ptdump_check_wx(void)
199 {
200 	struct pg_state st = {
201 		.ptdump = {
202 			.note_page = note_page,
203 			.range = (struct ptdump_range[]) {
204 				{.start = 0, .end = max_addr},
205 				{.start = 0, .end = 0},
206 			}
207 		},
208 		.seq = NULL,
209 		.level = -1,
210 		.current_prot = 0,
211 		.check_wx = true,
212 		.wx_pages = 0,
213 		.start_address = 0,
214 		.marker = (struct addr_marker[]) {
215 			{ .start_address =  0, .name = NULL},
216 			{ .start_address = -1, .name = NULL},
217 		},
218 	};
219 
220 	if (!MACHINE_HAS_NX)
221 		return;
222 	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
223 	if (st.wx_pages)
224 		pr_warn("Checked W+X mappings: FAILED, %lu W+X pages found\n", st.wx_pages);
225 	else
226 		pr_info("Checked W+X mappings: passed, no %sW+X pages found\n",
227 			(nospec_uses_trampoline() || !static_key_enabled(&cpu_has_bear)) ?
228 			"unexpected " : "");
229 }
230 #endif /* CONFIG_DEBUG_WX */
231 
232 #ifdef CONFIG_PTDUMP_DEBUGFS
233 static int ptdump_show(struct seq_file *m, void *v)
234 {
235 	struct pg_state st = {
236 		.ptdump = {
237 			.note_page = note_page,
238 			.range = (struct ptdump_range[]) {
239 				{.start = 0, .end = max_addr},
240 				{.start = 0, .end = 0},
241 			}
242 		},
243 		.seq = m,
244 		.level = -1,
245 		.current_prot = 0,
246 		.check_wx = false,
247 		.wx_pages = 0,
248 		.start_address = 0,
249 		.marker = address_markers,
250 	};
251 
252 	get_online_mems();
253 	mutex_lock(&cpa_mutex);
254 	ptdump_walk_pgd(&st.ptdump, &init_mm, NULL);
255 	mutex_unlock(&cpa_mutex);
256 	put_online_mems();
257 	return 0;
258 }
259 DEFINE_SHOW_ATTRIBUTE(ptdump);
260 #endif /* CONFIG_PTDUMP_DEBUGFS */
261 
262 /*
263  * Heapsort from lib/sort.c is not a stable sorting algorithm, do a simple
264  * insertion sort to preserve the original order of markers with the same
265  * start address.
266  */
267 static void sort_address_markers(void)
268 {
269 	struct addr_marker tmp;
270 	int i, j;
271 
272 	for (i = 1; i < ARRAY_SIZE(address_markers) - 1; i++) {
273 		tmp = address_markers[i];
274 		for (j = i - 1; j >= 0 && address_markers[j].start_address > tmp.start_address; j--)
275 			address_markers[j + 1] = address_markers[j];
276 		address_markers[j + 1] = tmp;
277 	}
278 }
279 
280 static int pt_dump_init(void)
281 {
282 #ifdef CONFIG_KFENCE
283 	unsigned long kfence_start = (unsigned long)__kfence_pool;
284 #endif
285 	/*
286 	 * Figure out the maximum virtual address being accessible with the
287 	 * kernel ASCE. We need this to keep the page table walker functions
288 	 * from accessing non-existent entries.
289 	 */
290 	max_addr = (S390_lowcore.kernel_asce & _REGION_ENTRY_TYPE_MASK) >> 2;
291 	max_addr = 1UL << (max_addr * 11 + 31);
292 	address_markers[IDENTITY_AFTER_END_NR].start_address = ident_map_size;
293 	address_markers[AMODE31_START_NR].start_address = __samode31;
294 	address_markers[AMODE31_END_NR].start_address = __eamode31;
295 	address_markers[MODULES_NR].start_address = MODULES_VADDR;
296 	address_markers[MODULES_END_NR].start_address = MODULES_END;
297 	address_markers[ABS_LOWCORE_NR].start_address = __abs_lowcore;
298 	address_markers[ABS_LOWCORE_END_NR].start_address = __abs_lowcore + ABS_LOWCORE_MAP_SIZE;
299 	address_markers[MEMCPY_REAL_NR].start_address = __memcpy_real_area;
300 	address_markers[MEMCPY_REAL_END_NR].start_address = __memcpy_real_area + PAGE_SIZE;
301 	address_markers[VMEMMAP_NR].start_address = (unsigned long) vmemmap;
302 	address_markers[VMEMMAP_END_NR].start_address = (unsigned long)vmemmap + vmemmap_size;
303 	address_markers[VMALLOC_NR].start_address = VMALLOC_START;
304 	address_markers[VMALLOC_END_NR].start_address = VMALLOC_END;
305 #ifdef CONFIG_KFENCE
306 	address_markers[KFENCE_START_NR].start_address = kfence_start;
307 	address_markers[KFENCE_END_NR].start_address = kfence_start + KFENCE_POOL_SIZE;
308 #endif
309 	sort_address_markers();
310 #ifdef CONFIG_PTDUMP_DEBUGFS
311 	debugfs_create_file("kernel_page_tables", 0400, NULL, NULL, &ptdump_fops);
312 #endif /* CONFIG_PTDUMP_DEBUGFS */
313 	return 0;
314 }
315 device_initcall(pt_dump_init);
316