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