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