1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_S390_UNWIND_H
3 #define _ASM_S390_UNWIND_H
4 
5 #include <linux/sched.h>
6 #include <linux/ftrace.h>
7 #include <linux/rethook.h>
8 #include <linux/llist.h>
9 #include <asm/ptrace.h>
10 #include <asm/stacktrace.h>
11 
12 /*
13  * To use the stack unwinder it has to be initialized with unwind_start.
14  * There four combinations for task and regs:
15  * 1) task==NULL, regs==NULL: the unwind starts for the task that is currently
16  *    running, sp/ip picked up from the CPU registers
17  * 2) task==NULL, regs!=NULL: the unwind starts from the sp/ip found in
18  *    the struct pt_regs of an interrupt frame for the current task
19  * 3) task!=NULL, regs==NULL: the unwind starts for an inactive task with
20  *    the sp picked up from task->thread.ksp and the ip picked up from the
21  *    return address stored by __switch_to
22  * 4) task!=NULL, regs!=NULL: the sp/ip are picked up from the interrupt
23  *    frame 'regs' of a inactive task
24  * If 'first_frame' is not zero unwind_start skips unwind frames until it
25  * reaches the specified stack pointer.
26  * The end of the unwinding is indicated with unwind_done, this can be true
27  * right after unwind_start, e.g. with first_frame!=0 that can not be found.
28  * unwind_next_frame skips to the next frame.
29  * Once the unwind is completed unwind_error() can be used to check if there
30  * has been a situation where the unwinder could not correctly understand
31  * the tasks call chain.
32  */
33 
34 struct unwind_state {
35 	struct stack_info stack_info;
36 	unsigned long stack_mask;
37 	struct task_struct *task;
38 	struct pt_regs *regs;
39 	unsigned long sp, ip;
40 	int graph_idx;
41 	struct llist_node *kr_cur;
42 	bool reliable;
43 	bool error;
44 };
45 
46 /* Recover the return address modified by rethook and ftrace_graph. */
unwind_recover_ret_addr(struct unwind_state * state,unsigned long ip)47 static inline unsigned long unwind_recover_ret_addr(struct unwind_state *state,
48 						    unsigned long ip)
49 {
50 	ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *)state->sp);
51 #ifdef CONFIG_RETHOOK
52 	if (is_rethook_trampoline(ip))
53 		ip = rethook_find_ret_addr(state->task, state->sp, &state->kr_cur);
54 #endif
55 	return ip;
56 }
57 
58 void __unwind_start(struct unwind_state *state, struct task_struct *task,
59 		    struct pt_regs *regs, unsigned long first_frame);
60 bool unwind_next_frame(struct unwind_state *state);
61 unsigned long unwind_get_return_address(struct unwind_state *state);
62 
unwind_done(struct unwind_state * state)63 static inline bool unwind_done(struct unwind_state *state)
64 {
65 	return state->stack_info.type == STACK_TYPE_UNKNOWN;
66 }
67 
unwind_error(struct unwind_state * state)68 static inline bool unwind_error(struct unwind_state *state)
69 {
70 	return state->error;
71 }
72 
unwind_start(struct unwind_state * state,struct task_struct * task,struct pt_regs * regs,unsigned long first_frame)73 static __always_inline void unwind_start(struct unwind_state *state,
74 					 struct task_struct *task,
75 					 struct pt_regs *regs,
76 					 unsigned long first_frame)
77 {
78 	task = task ?: current;
79 	first_frame = first_frame ?: get_stack_pointer(task, regs);
80 	__unwind_start(state, task, regs, first_frame);
81 }
82 
unwind_get_entry_regs(struct unwind_state * state)83 static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
84 {
85 	return unwind_done(state) ? NULL : state->regs;
86 }
87 
88 #define unwind_for_each_frame(state, task, regs, first_frame)	\
89 	for (unwind_start(state, task, regs, first_frame);	\
90 	     !unwind_done(state);				\
91 	     unwind_next_frame(state))
92 
unwind_init(void)93 static inline void unwind_init(void) {}
unwind_module_init(struct module * mod,void * orc_ip,size_t orc_ip_size,void * orc,size_t orc_size)94 static inline void unwind_module_init(struct module *mod, void *orc_ip,
95 				      size_t orc_ip_size, void *orc,
96 				      size_t orc_size) {}
97 
98 #endif /* _ASM_S390_UNWIND_H */
99