xref: /openbmc/linux/arch/s390/lib/test_unwind.c (revision 9cf0666f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test module for unwind_for_each_frame
4  */
5 
6 #include <kunit/test.h>
7 #include <asm/unwind.h>
8 #include <linux/completion.h>
9 #include <linux/kallsyms.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/timer.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/kprobes.h>
16 #include <linux/wait.h>
17 #include <asm/irq.h>
18 
19 struct kunit *current_test;
20 
21 #define BT_BUF_SIZE (PAGE_SIZE * 4)
22 
23 /*
24  * To avoid printk line limit split backtrace by lines
25  */
26 static void print_backtrace(char *bt)
27 {
28 	char *p;
29 
30 	while (true) {
31 		p = strsep(&bt, "\n");
32 		if (!p)
33 			break;
34 		kunit_err(current_test, "%s\n", p);
35 	}
36 }
37 
38 /*
39  * Calls unwind_for_each_frame(task, regs, sp) and verifies that the result
40  * contains unwindme_func2 followed by unwindme_func1.
41  */
42 static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs,
43 				unsigned long sp)
44 {
45 	int frame_count, prev_is_func2, seen_func2_func1;
46 	const int max_frames = 128;
47 	struct unwind_state state;
48 	size_t bt_pos = 0;
49 	int ret = 0;
50 	char *bt;
51 
52 	bt = kmalloc(BT_BUF_SIZE, GFP_ATOMIC);
53 	if (!bt) {
54 		kunit_err(current_test, "failed to allocate backtrace buffer\n");
55 		return -ENOMEM;
56 	}
57 	/* Unwind. */
58 	frame_count = 0;
59 	prev_is_func2 = 0;
60 	seen_func2_func1 = 0;
61 	unwind_for_each_frame(&state, task, regs, sp) {
62 		unsigned long addr = unwind_get_return_address(&state);
63 		char sym[KSYM_SYMBOL_LEN];
64 
65 		if (frame_count++ == max_frames)
66 			break;
67 		if (state.reliable && !addr) {
68 			kunit_err(current_test, "unwind state reliable but addr is 0\n");
69 			ret = -EINVAL;
70 			break;
71 		}
72 		sprint_symbol(sym, addr);
73 		if (bt_pos < BT_BUF_SIZE) {
74 			bt_pos += snprintf(bt + bt_pos, BT_BUF_SIZE - bt_pos,
75 					   state.reliable ? " [%-7s%px] %pSR\n" :
76 							    "([%-7s%px] %pSR)\n",
77 					   stack_type_name(state.stack_info.type),
78 					   (void *)state.sp, (void *)state.ip);
79 			if (bt_pos >= BT_BUF_SIZE)
80 				kunit_err(current_test, "backtrace buffer is too small\n");
81 		}
82 		frame_count += 1;
83 		if (prev_is_func2 && str_has_prefix(sym, "unwindme_func1"))
84 			seen_func2_func1 = 1;
85 		prev_is_func2 = str_has_prefix(sym, "unwindme_func2");
86 	}
87 
88 	/* Check the results. */
89 	if (unwind_error(&state)) {
90 		kunit_err(current_test, "unwind error\n");
91 		ret = -EINVAL;
92 	}
93 	if (!seen_func2_func1) {
94 		kunit_err(current_test, "unwindme_func2 and unwindme_func1 not found\n");
95 		ret = -EINVAL;
96 	}
97 	if (frame_count == max_frames) {
98 		kunit_err(current_test, "Maximum number of frames exceeded\n");
99 		ret = -EINVAL;
100 	}
101 	if (ret)
102 		print_backtrace(bt);
103 	kfree(bt);
104 	return ret;
105 }
106 
107 /* State of the task being unwound. */
108 struct unwindme {
109 	int flags;
110 	int ret;
111 	struct task_struct *task;
112 	struct completion task_ready;
113 	wait_queue_head_t task_wq;
114 	unsigned long sp;
115 };
116 
117 static struct unwindme *unwindme;
118 
119 /* Values of unwindme.flags. */
120 #define UWM_DEFAULT		0x0
121 #define UWM_THREAD		0x1	/* Unwind a separate task. */
122 #define UWM_REGS		0x2	/* Pass regs to test_unwind(). */
123 #define UWM_SP			0x4	/* Pass sp to test_unwind(). */
124 #define UWM_CALLER		0x8	/* Unwind starting from caller. */
125 #define UWM_SWITCH_STACK	0x10	/* Use call_on_stack. */
126 #define UWM_IRQ			0x20	/* Unwind from irq context. */
127 #define UWM_PGM			0x40	/* Unwind from program check handler. */
128 
129 static __always_inline unsigned long get_psw_addr(void)
130 {
131 	unsigned long psw_addr;
132 
133 	asm volatile(
134 		"basr	%[psw_addr],0\n"
135 		: [psw_addr] "=d" (psw_addr));
136 	return psw_addr;
137 }
138 
139 #ifdef CONFIG_KPROBES
140 static int pgm_pre_handler(struct kprobe *p, struct pt_regs *regs)
141 {
142 	struct unwindme *u = unwindme;
143 
144 	u->ret = test_unwind(NULL, (u->flags & UWM_REGS) ? regs : NULL,
145 			     (u->flags & UWM_SP) ? u->sp : 0);
146 	return 0;
147 }
148 #endif
149 
150 /* This function may or may not appear in the backtrace. */
151 static noinline int unwindme_func4(struct unwindme *u)
152 {
153 	if (!(u->flags & UWM_CALLER))
154 		u->sp = current_frame_address();
155 	if (u->flags & UWM_THREAD) {
156 		complete(&u->task_ready);
157 		wait_event(u->task_wq, kthread_should_park());
158 		kthread_parkme();
159 		return 0;
160 #ifdef CONFIG_KPROBES
161 	} else if (u->flags & UWM_PGM) {
162 		struct kprobe kp;
163 		int ret;
164 
165 		unwindme = u;
166 		memset(&kp, 0, sizeof(kp));
167 		kp.symbol_name = "do_report_trap";
168 		kp.pre_handler = pgm_pre_handler;
169 		ret = register_kprobe(&kp);
170 		if (ret < 0) {
171 			kunit_err(current_test, "register_kprobe failed %d\n", ret);
172 			return -EINVAL;
173 		}
174 
175 		/*
176 		 * trigger specification exception
177 		 */
178 		asm volatile(
179 			"	mvcl	%%r1,%%r1\n"
180 			"0:	nopr	%%r7\n"
181 			EX_TABLE(0b, 0b)
182 			:);
183 
184 		unregister_kprobe(&kp);
185 		unwindme = NULL;
186 		return u->ret;
187 #endif
188 	} else {
189 		struct pt_regs regs;
190 
191 		memset(&regs, 0, sizeof(regs));
192 		regs.psw.addr = get_psw_addr();
193 		regs.gprs[15] = current_stack_pointer();
194 		return test_unwind(NULL,
195 				   (u->flags & UWM_REGS) ? &regs : NULL,
196 				   (u->flags & UWM_SP) ? u->sp : 0);
197 	}
198 }
199 
200 /* This function may or may not appear in the backtrace. */
201 static noinline int unwindme_func3(struct unwindme *u)
202 {
203 	u->sp = current_frame_address();
204 	return unwindme_func4(u);
205 }
206 
207 /* This function must appear in the backtrace. */
208 static noinline int unwindme_func2(struct unwindme *u)
209 {
210 	unsigned long flags;
211 	int rc;
212 
213 	if (u->flags & UWM_SWITCH_STACK) {
214 		local_irq_save(flags);
215 		local_mcck_disable();
216 		rc = call_on_stack(1, S390_lowcore.nodat_stack,
217 				   int, unwindme_func3, struct unwindme *, u);
218 		local_mcck_enable();
219 		local_irq_restore(flags);
220 		return rc;
221 	} else {
222 		return unwindme_func3(u);
223 	}
224 }
225 
226 /* This function must follow unwindme_func2 in the backtrace. */
227 static noinline int unwindme_func1(void *u)
228 {
229 	return unwindme_func2((struct unwindme *)u);
230 }
231 
232 static void unwindme_timer_fn(struct timer_list *unused)
233 {
234 	struct unwindme *u = READ_ONCE(unwindme);
235 
236 	if (u) {
237 		unwindme = NULL;
238 		u->task = NULL;
239 		u->ret = unwindme_func1(u);
240 		complete(&u->task_ready);
241 	}
242 }
243 
244 static struct timer_list unwind_timer;
245 
246 static int test_unwind_irq(struct unwindme *u)
247 {
248 	unwindme = u;
249 	init_completion(&u->task_ready);
250 	timer_setup(&unwind_timer, unwindme_timer_fn, 0);
251 	mod_timer(&unwind_timer, jiffies + 1);
252 	wait_for_completion(&u->task_ready);
253 	return u->ret;
254 }
255 
256 /* Spawns a task and passes it to test_unwind(). */
257 static int test_unwind_task(struct kunit *test, struct unwindme *u)
258 {
259 	struct task_struct *task;
260 	int ret;
261 
262 	/* Initialize thread-related fields. */
263 	init_completion(&u->task_ready);
264 	init_waitqueue_head(&u->task_wq);
265 
266 	/*
267 	 * Start the task and wait until it reaches unwindme_func4() and sleeps
268 	 * in (task_ready, unwind_done] range.
269 	 */
270 	task = kthread_run(unwindme_func1, u, "%s", __func__);
271 	if (IS_ERR(task)) {
272 		kunit_err(test, "kthread_run() failed\n");
273 		return PTR_ERR(task);
274 	}
275 	/*
276 	 * Make sure task reaches unwindme_func4 before parking it,
277 	 * we might park it before kthread function has been executed otherwise
278 	 */
279 	wait_for_completion(&u->task_ready);
280 	kthread_park(task);
281 	/* Unwind. */
282 	ret = test_unwind(task, NULL, (u->flags & UWM_SP) ? u->sp : 0);
283 	kthread_stop(task);
284 	return ret;
285 }
286 
287 struct test_params {
288 	int flags;
289 	char *name;
290 };
291 
292 /*
293  * Create required parameter list for tests
294  */
295 static const struct test_params param_list[] = {
296 	{.flags = UWM_DEFAULT, .name = "UWM_DEFAULT"},
297 	{.flags = UWM_SP, .name = "UWM_SP"},
298 	{.flags = UWM_REGS, .name = "UWM_REGS"},
299 	{.flags = UWM_SWITCH_STACK,
300 		.name = "UWM_SWITCH_STACK"},
301 	{.flags = UWM_SP | UWM_REGS,
302 		.name = "UWM_SP | UWM_REGS"},
303 	{.flags = UWM_CALLER | UWM_SP,
304 		.name = "WM_CALLER | UWM_SP"},
305 	{.flags = UWM_CALLER | UWM_SP | UWM_REGS,
306 		.name = "UWM_CALLER | UWM_SP | UWM_REGS"},
307 	{.flags = UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK,
308 		.name = "UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK"},
309 	{.flags = UWM_THREAD, .name = "UWM_THREAD"},
310 	{.flags = UWM_THREAD | UWM_SP,
311 		.name = "UWM_THREAD | UWM_SP"},
312 	{.flags = UWM_THREAD | UWM_CALLER | UWM_SP,
313 		.name = "UWM_THREAD | UWM_CALLER | UWM_SP"},
314 	{.flags = UWM_IRQ, .name = "UWM_IRQ"},
315 	{.flags = UWM_IRQ | UWM_SWITCH_STACK,
316 		.name = "UWM_IRQ | UWM_SWITCH_STACK"},
317 	{.flags = UWM_IRQ | UWM_SP,
318 		.name = "UWM_IRQ | UWM_SP"},
319 	{.flags = UWM_IRQ | UWM_REGS,
320 		.name = "UWM_IRQ | UWM_REGS"},
321 	{.flags = UWM_IRQ | UWM_SP | UWM_REGS,
322 		.name = "UWM_IRQ | UWM_SP | UWM_REGS"},
323 	{.flags = UWM_IRQ | UWM_CALLER | UWM_SP,
324 		.name = "UWM_IRQ | UWM_CALLER | UWM_SP"},
325 	{.flags = UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS,
326 		.name = "UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS"},
327 	{.flags = UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK,
328 		.name = "UWM_IRQ | UWM_CALLER | UWM_SP | UWM_REGS | UWM_SWITCH_STACK"},
329 	#ifdef CONFIG_KPROBES
330 	{.flags = UWM_PGM, .name = "UWM_PGM"},
331 	{.flags = UWM_PGM | UWM_SP,
332 		.name = "UWM_PGM | UWM_SP"},
333 	{.flags = UWM_PGM | UWM_REGS,
334 		.name = "UWM_PGM | UWM_REGS"},
335 	{.flags = UWM_PGM | UWM_SP | UWM_REGS,
336 		.name = "UWM_PGM | UWM_SP | UWM_REGS"},
337 	#endif
338 };
339 
340 /*
341  * Parameter description generator: required for KUNIT_ARRAY_PARAM()
342  */
343 static void get_desc(const struct test_params *params, char *desc)
344 {
345 	strscpy(desc, params->name, KUNIT_PARAM_DESC_SIZE);
346 }
347 
348 /*
349  * Create test_unwind_gen_params
350  */
351 KUNIT_ARRAY_PARAM(test_unwind, param_list, get_desc);
352 
353 static void test_unwind_flags(struct kunit *test)
354 {
355 	struct unwindme u;
356 	const struct test_params *params;
357 
358 	current_test = test;
359 	params = (const struct test_params *)test->param_value;
360 	u.flags = params->flags;
361 	if (u.flags & UWM_THREAD)
362 		KUNIT_EXPECT_EQ(test, 0, test_unwind_task(test, &u));
363 	else if (u.flags & UWM_IRQ)
364 		KUNIT_EXPECT_EQ(test, 0, test_unwind_irq(&u));
365 	else
366 		KUNIT_EXPECT_EQ(test, 0, unwindme_func1(&u));
367 }
368 
369 static struct kunit_case unwind_test_cases[] = {
370 	KUNIT_CASE_PARAM(test_unwind_flags, test_unwind_gen_params),
371 	{}
372 };
373 
374 static struct kunit_suite test_unwind_suite = {
375 	.name = "test_unwind",
376 	.test_cases = unwind_test_cases,
377 };
378 
379 kunit_test_suites(&test_unwind_suite);
380 
381 MODULE_LICENSE("GPL");
382