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