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