1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test module for unwind_for_each_frame
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/string.h>
13 #include <linux/kprobes.h>
14 #include <linux/wait.h>
16 #include <asm/delay.h>
18 #define BT_BUF_SIZE (PAGE_SIZE * 4)
21 * To avoid printk line limit split backtrace by lines
23 static void print_backtrace(char *bt
)
28 p
= strsep(&bt
, "\n");
36 * Calls unwind_for_each_frame(task, regs, sp) and verifies that the result
37 * contains unwindme_func2 followed by unwindme_func1.
39 static noinline
int test_unwind(struct task_struct
*task
, struct pt_regs
*regs
,
42 int frame_count
, prev_is_func2
, seen_func2_func1
;
43 const int max_frames
= 128;
44 struct unwind_state state
;
49 bt
= kmalloc(BT_BUF_SIZE
, GFP_ATOMIC
);
51 pr_err("failed to allocate backtrace buffer\n");
58 unwind_for_each_frame(&state
, task
, regs
, sp
) {
59 unsigned long addr
= unwind_get_return_address(&state
);
60 char sym
[KSYM_SYMBOL_LEN
];
62 if (frame_count
++ == max_frames
)
64 if (state
.reliable
&& !addr
) {
65 pr_err("unwind state reliable but addr is 0\n");
68 sprint_symbol(sym
, addr
);
69 if (bt_pos
< BT_BUF_SIZE
) {
70 bt_pos
+= snprintf(bt
+ bt_pos
, BT_BUF_SIZE
- bt_pos
,
71 state
.reliable
? " [%-7s%px] %pSR\n" :
73 stack_type_name(state
.stack_info
.type
),
74 (void *)state
.sp
, (void *)state
.ip
);
75 if (bt_pos
>= BT_BUF_SIZE
)
76 pr_err("backtrace buffer is too small\n");
79 if (prev_is_func2
&& str_has_prefix(sym
, "unwindme_func1"))
81 prev_is_func2
= str_has_prefix(sym
, "unwindme_func2");
84 /* Check the results. */
85 if (unwind_error(&state
)) {
86 pr_err("unwind error\n");
89 if (!seen_func2_func1
) {
90 pr_err("unwindme_func2 and unwindme_func1 not found\n");
93 if (frame_count
== max_frames
) {
94 pr_err("Maximum number of frames exceeded\n");
103 /* State of the task being unwound. */
107 struct task_struct
*task
;
108 struct completion task_ready
;
109 wait_queue_head_t task_wq
;
113 static struct unwindme
*unwindme
;
115 /* Values of unwindme.flags. */
116 #define UWM_DEFAULT 0x0
117 #define UWM_THREAD 0x1 /* Unwind a separate task. */
118 #define UWM_REGS 0x2 /* Pass regs to test_unwind(). */
119 #define UWM_SP 0x4 /* Pass sp to test_unwind(). */
120 #define UWM_CALLER 0x8 /* Unwind starting from caller. */
121 #define UWM_SWITCH_STACK 0x10 /* Use CALL_ON_STACK. */
122 #define UWM_IRQ 0x20 /* Unwind from irq context. */
123 #define UWM_PGM 0x40 /* Unwind from program check handler. */
125 static __always_inline
unsigned long get_psw_addr(void)
127 unsigned long psw_addr
;
130 "basr %[psw_addr],0\n"
131 : [psw_addr
] "=d" (psw_addr
));
135 #ifdef CONFIG_KPROBES
136 static int pgm_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
138 struct unwindme
*u
= unwindme
;
140 u
->ret
= test_unwind(NULL
, (u
->flags
& UWM_REGS
) ? regs
: NULL
,
141 (u
->flags
& UWM_SP
) ? u
->sp
: 0);
146 /* This function may or may not appear in the backtrace. */
147 static noinline
int unwindme_func4(struct unwindme
*u
)
149 if (!(u
->flags
& UWM_CALLER
))
150 u
->sp
= current_frame_address();
151 if (u
->flags
& UWM_THREAD
) {
152 complete(&u
->task_ready
);
153 wait_event(u
->task_wq
, kthread_should_park());
156 #ifdef CONFIG_KPROBES
157 } else if (u
->flags
& UWM_PGM
) {
162 memset(&kp
, 0, sizeof(kp
));
163 kp
.symbol_name
= "do_report_trap";
164 kp
.pre_handler
= pgm_pre_handler
;
165 ret
= register_kprobe(&kp
);
167 pr_err("register_kprobe failed %d\n", ret
);
172 * trigger specification exception
180 unregister_kprobe(&kp
);
187 memset(®s
, 0, sizeof(regs
));
188 regs
.psw
.addr
= get_psw_addr();
189 regs
.gprs
[15] = current_stack_pointer();
190 return test_unwind(NULL
,
191 (u
->flags
& UWM_REGS
) ? ®s
: NULL
,
192 (u
->flags
& UWM_SP
) ? u
->sp
: 0);
196 /* This function may or may not appear in the backtrace. */
197 static noinline
int unwindme_func3(struct unwindme
*u
)
199 u
->sp
= current_frame_address();
200 return unwindme_func4(u
);
203 /* This function must appear in the backtrace. */
204 static noinline
int unwindme_func2(struct unwindme
*u
)
208 if (u
->flags
& UWM_SWITCH_STACK
) {
210 rc
= CALL_ON_STACK(unwindme_func3
, S390_lowcore
.nodat_stack
, 1, u
);
214 return unwindme_func3(u
);
218 /* This function must follow unwindme_func2 in the backtrace. */
219 static noinline
int unwindme_func1(void *u
)
221 return unwindme_func2((struct unwindme
*)u
);
224 static void unwindme_irq_handler(struct ext_code ext_code
,
225 unsigned int param32
,
226 unsigned long param64
)
228 struct unwindme
*u
= READ_ONCE(unwindme
);
230 if (u
&& u
->task
== current
) {
233 u
->ret
= unwindme_func1(u
);
237 static int test_unwind_irq(struct unwindme
*u
)
240 if (register_external_irq(EXT_IRQ_CLK_COMP
, unwindme_irq_handler
)) {
241 pr_info("Couldn't register external interrupt handler");
247 unregister_external_irq(EXT_IRQ_CLK_COMP
, unwindme_irq_handler
);
252 /* Spawns a task and passes it to test_unwind(). */
253 static int test_unwind_task(struct unwindme
*u
)
255 struct task_struct
*task
;
258 /* Initialize thread-related fields. */
259 init_completion(&u
->task_ready
);
260 init_waitqueue_head(&u
->task_wq
);
263 * Start the task and wait until it reaches unwindme_func4() and sleeps
264 * in (task_ready, unwind_done] range.
266 task
= kthread_run(unwindme_func1
, u
, "%s", __func__
);
268 pr_err("kthread_run() failed\n");
269 return PTR_ERR(task
);
272 * Make sure task reaches unwindme_func4 before parking it,
273 * we might park it before kthread function has been executed otherwise
275 wait_for_completion(&u
->task_ready
);
278 ret
= test_unwind(task
, NULL
, (u
->flags
& UWM_SP
) ? u
->sp
: 0);
283 static int test_unwind_flags(int flags
)
288 if (u
.flags
& UWM_THREAD
)
289 return test_unwind_task(&u
);
290 else if (u
.flags
& UWM_IRQ
)
291 return test_unwind_irq(&u
);
293 return unwindme_func1(&u
);
296 static int test_unwind_init(void)
300 #define TEST(flags) \
302 pr_info("[ RUN ] " #flags "\n"); \
303 if (!test_unwind_flags((flags))) { \
304 pr_info("[ OK ] " #flags "\n"); \
306 pr_err("[ FAILED ] " #flags "\n"); \
314 TEST(UWM_SWITCH_STACK
);
315 TEST(UWM_SP
| UWM_REGS
);
316 TEST(UWM_CALLER
| UWM_SP
);
317 TEST(UWM_CALLER
| UWM_SP
| UWM_REGS
);
318 TEST(UWM_CALLER
| UWM_SP
| UWM_REGS
| UWM_SWITCH_STACK
);
320 TEST(UWM_THREAD
| UWM_SP
);
321 TEST(UWM_THREAD
| UWM_CALLER
| UWM_SP
);
323 TEST(UWM_IRQ
| UWM_SWITCH_STACK
);
324 TEST(UWM_IRQ
| UWM_SP
);
325 TEST(UWM_IRQ
| UWM_REGS
);
326 TEST(UWM_IRQ
| UWM_SP
| UWM_REGS
);
327 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
);
328 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
| UWM_REGS
);
329 TEST(UWM_IRQ
| UWM_CALLER
| UWM_SP
| UWM_REGS
| UWM_SWITCH_STACK
);
330 #ifdef CONFIG_KPROBES
332 TEST(UWM_PGM
| UWM_SP
);
333 TEST(UWM_PGM
| UWM_REGS
);
334 TEST(UWM_PGM
| UWM_SP
| UWM_REGS
);
341 static void test_unwind_exit(void)
345 module_init(test_unwind_init
);
346 module_exit(test_unwind_exit
);
347 MODULE_LICENSE("GPL");