178c98f90SMartin Schwidefsky /* SPDX-License-Identifier: GPL-2.0 */
278c98f90SMartin Schwidefsky #ifndef _ASM_S390_UNWIND_H
378c98f90SMartin Schwidefsky #define _ASM_S390_UNWIND_H
478c98f90SMartin Schwidefsky
578c98f90SMartin Schwidefsky #include <linux/sched.h>
678c98f90SMartin Schwidefsky #include <linux/ftrace.h>
7*1a280f48SVasily Gorbik #include <linux/rethook.h>
8d81675b6SVasily Gorbik #include <linux/llist.h>
978c98f90SMartin Schwidefsky #include <asm/ptrace.h>
1078c98f90SMartin Schwidefsky #include <asm/stacktrace.h>
1178c98f90SMartin Schwidefsky
1278c98f90SMartin Schwidefsky /*
1378c98f90SMartin Schwidefsky * To use the stack unwinder it has to be initialized with unwind_start.
1478c98f90SMartin Schwidefsky * There four combinations for task and regs:
1578c98f90SMartin Schwidefsky * 1) task==NULL, regs==NULL: the unwind starts for the task that is currently
1678c98f90SMartin Schwidefsky * running, sp/ip picked up from the CPU registers
1778c98f90SMartin Schwidefsky * 2) task==NULL, regs!=NULL: the unwind starts from the sp/ip found in
1878c98f90SMartin Schwidefsky * the struct pt_regs of an interrupt frame for the current task
1978c98f90SMartin Schwidefsky * 3) task!=NULL, regs==NULL: the unwind starts for an inactive task with
2078c98f90SMartin Schwidefsky * the sp picked up from task->thread.ksp and the ip picked up from the
2178c98f90SMartin Schwidefsky * return address stored by __switch_to
2278c98f90SMartin Schwidefsky * 4) task!=NULL, regs!=NULL: the sp/ip are picked up from the interrupt
2378c98f90SMartin Schwidefsky * frame 'regs' of a inactive task
2478c98f90SMartin Schwidefsky * If 'first_frame' is not zero unwind_start skips unwind frames until it
2578c98f90SMartin Schwidefsky * reaches the specified stack pointer.
2678c98f90SMartin Schwidefsky * The end of the unwinding is indicated with unwind_done, this can be true
2778c98f90SMartin Schwidefsky * right after unwind_start, e.g. with first_frame!=0 that can not be found.
2878c98f90SMartin Schwidefsky * unwind_next_frame skips to the next frame.
2978c98f90SMartin Schwidefsky * Once the unwind is completed unwind_error() can be used to check if there
3078c98f90SMartin Schwidefsky * has been a situation where the unwinder could not correctly understand
3178c98f90SMartin Schwidefsky * the tasks call chain.
3278c98f90SMartin Schwidefsky */
3378c98f90SMartin Schwidefsky
3478c98f90SMartin Schwidefsky struct unwind_state {
3578c98f90SMartin Schwidefsky struct stack_info stack_info;
3678c98f90SMartin Schwidefsky unsigned long stack_mask;
3778c98f90SMartin Schwidefsky struct task_struct *task;
3878c98f90SMartin Schwidefsky struct pt_regs *regs;
3978c98f90SMartin Schwidefsky unsigned long sp, ip;
4078c98f90SMartin Schwidefsky int graph_idx;
41d81675b6SVasily Gorbik struct llist_node *kr_cur;
4278c98f90SMartin Schwidefsky bool reliable;
4378c98f90SMartin Schwidefsky bool error;
4478c98f90SMartin Schwidefsky };
4578c98f90SMartin Schwidefsky
46*1a280f48SVasily Gorbik /* Recover the return address modified by rethook and ftrace_graph. */
unwind_recover_ret_addr(struct unwind_state * state,unsigned long ip)47d81675b6SVasily Gorbik static inline unsigned long unwind_recover_ret_addr(struct unwind_state *state,
48d81675b6SVasily Gorbik unsigned long ip)
49d81675b6SVasily Gorbik {
50ded466e1SSumanth Korikkar ip = ftrace_graph_ret_addr(state->task, &state->graph_idx, ip, (void *)state->sp);
51*1a280f48SVasily Gorbik #ifdef CONFIG_RETHOOK
52*1a280f48SVasily Gorbik if (is_rethook_trampoline(ip))
53*1a280f48SVasily Gorbik ip = rethook_find_ret_addr(state->task, state->sp, &state->kr_cur);
54*1a280f48SVasily Gorbik #endif
55d81675b6SVasily Gorbik return ip;
56d81675b6SVasily Gorbik }
57d81675b6SVasily Gorbik
5878c98f90SMartin Schwidefsky void __unwind_start(struct unwind_state *state, struct task_struct *task,
5978c98f90SMartin Schwidefsky struct pt_regs *regs, unsigned long first_frame);
6078c98f90SMartin Schwidefsky bool unwind_next_frame(struct unwind_state *state);
6178c98f90SMartin Schwidefsky unsigned long unwind_get_return_address(struct unwind_state *state);
6278c98f90SMartin Schwidefsky
unwind_done(struct unwind_state * state)6378c98f90SMartin Schwidefsky static inline bool unwind_done(struct unwind_state *state)
6478c98f90SMartin Schwidefsky {
6578c98f90SMartin Schwidefsky return state->stack_info.type == STACK_TYPE_UNKNOWN;
6678c98f90SMartin Schwidefsky }
6778c98f90SMartin Schwidefsky
unwind_error(struct unwind_state * state)6878c98f90SMartin Schwidefsky static inline bool unwind_error(struct unwind_state *state)
6978c98f90SMartin Schwidefsky {
7078c98f90SMartin Schwidefsky return state->error;
7178c98f90SMartin Schwidefsky }
7278c98f90SMartin Schwidefsky
unwind_start(struct unwind_state * state,struct task_struct * task,struct pt_regs * regs,unsigned long first_frame)7388b60426SVasily Gorbik static __always_inline void unwind_start(struct unwind_state *state,
7478c98f90SMartin Schwidefsky struct task_struct *task,
7578c98f90SMartin Schwidefsky struct pt_regs *regs,
76222ee908SVasily Gorbik unsigned long first_frame)
7778c98f90SMartin Schwidefsky {
78103b4ccaSVasily Gorbik task = task ?: current;
79222ee908SVasily Gorbik first_frame = first_frame ?: get_stack_pointer(task, regs);
80222ee908SVasily Gorbik __unwind_start(state, task, regs, first_frame);
8178c98f90SMartin Schwidefsky }
8278c98f90SMartin Schwidefsky
unwind_get_entry_regs(struct unwind_state * state)8378c98f90SMartin Schwidefsky static inline struct pt_regs *unwind_get_entry_regs(struct unwind_state *state)
8478c98f90SMartin Schwidefsky {
8578c98f90SMartin Schwidefsky return unwind_done(state) ? NULL : state->regs;
8678c98f90SMartin Schwidefsky }
8778c98f90SMartin Schwidefsky
8878c98f90SMartin Schwidefsky #define unwind_for_each_frame(state, task, regs, first_frame) \
8978c98f90SMartin Schwidefsky for (unwind_start(state, task, regs, first_frame); \
9078c98f90SMartin Schwidefsky !unwind_done(state); \
9178c98f90SMartin Schwidefsky unwind_next_frame(state))
9278c98f90SMartin Schwidefsky
unwind_init(void)9378c98f90SMartin Schwidefsky 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)9478c98f90SMartin Schwidefsky static inline void unwind_module_init(struct module *mod, void *orc_ip,
9578c98f90SMartin Schwidefsky size_t orc_ip_size, void *orc,
9678c98f90SMartin Schwidefsky size_t orc_size) {}
9778c98f90SMartin Schwidefsky
9878c98f90SMartin Schwidefsky #endif /* _ASM_S390_UNWIND_H */
99