1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/sched.h>
3 #include <linux/sched/task.h>
4 #include <linux/sched/task_stack.h>
5 #include <linux/interrupt.h>
6 #include <asm/sections.h>
7 #include <asm/ptrace.h>
8 #include <asm/bitops.h>
9 #include <asm/stacktrace.h>
10 #include <asm/unwind.h>
12 unsigned long unwind_get_return_address(struct unwind_state *state)
14 if (unwind_done(state))
16 return __kernel_text_address(state->ip) ? state->ip : 0;
18 EXPORT_SYMBOL_GPL(unwind_get_return_address);
20 static bool outside_of_stack(struct unwind_state *state, unsigned long sp)
22 return (sp <= state->sp) ||
23 (sp > state->stack_info.end - sizeof(struct stack_frame));
26 static bool update_stack_info(struct unwind_state *state, unsigned long sp)
28 struct stack_info *info = &state->stack_info;
29 unsigned long *mask = &state->stack_mask;
31 /* New stack pointer leaves the current stack */
32 if (get_stack_info(sp, state->task, info, mask) != 0 ||
33 !on_stack(info, sp, sizeof(struct stack_frame)))
34 /* 'sp' does not point to a valid stack */
39 bool unwind_next_frame(struct unwind_state *state)
41 struct stack_info *info = &state->stack_info;
42 struct stack_frame *sf;
49 if (state->reuse_sp) {
51 state->reuse_sp = false;
53 sp = READ_ONCE_NOCHECK(regs->gprs[15]);
54 if (unlikely(outside_of_stack(state, sp))) {
55 if (!update_stack_info(state, sp))
59 sf = (struct stack_frame *) sp;
60 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
63 if (!__kernel_text_address(ip)) {
66 return unwind_next_frame(state);
69 sf = (struct stack_frame *) state->sp;
70 sp = READ_ONCE_NOCHECK(sf->back_chain);
72 /* Non-zero back-chain points to the previous frame */
73 if (unlikely(outside_of_stack(state, sp))) {
74 if (!update_stack_info(state, sp))
77 sf = (struct stack_frame *) sp;
78 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
81 /* No back-chain, look for a pt_regs structure */
82 sp = state->sp + STACK_FRAME_OVERHEAD;
83 if (!on_stack(info, sp, sizeof(struct pt_regs)))
85 regs = (struct pt_regs *) sp;
86 if (READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE)
88 ip = READ_ONCE_NOCHECK(regs->psw.addr);
93 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
94 /* Decode any ftrace redirection */
95 if (ip == (unsigned long) return_to_handler)
96 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
100 /* Update unwind state */
104 state->reliable = reliable;
110 state->stack_info.type = STACK_TYPE_UNKNOWN;
113 EXPORT_SYMBOL_GPL(unwind_next_frame);
115 void __unwind_start(struct unwind_state *state, struct task_struct *task,
116 struct pt_regs *regs, unsigned long sp)
118 struct stack_info *info = &state->stack_info;
119 unsigned long *mask = &state->stack_mask;
120 bool reliable, reuse_sp;
121 struct stack_frame *sf;
124 memset(state, 0, sizeof(*state));
128 /* Don't even attempt to start from user mode regs: */
129 if (regs && user_mode(regs)) {
130 info->type = STACK_TYPE_UNKNOWN;
134 /* Get current stack pointer and initialize stack info */
135 if (get_stack_info(sp, task, info, mask) != 0 ||
136 !on_stack(info, sp, sizeof(struct stack_frame))) {
137 /* Something is wrong with the stack pointer */
138 info->type = STACK_TYPE_UNKNOWN;
143 /* Get the instruction pointer from pt_regs or the stack frame */
145 ip = READ_ONCE_NOCHECK(regs->psw.addr);
149 sf = (struct stack_frame *) sp;
150 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
155 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
156 /* Decode any ftrace redirection */
157 if (ip == (unsigned long) return_to_handler)
158 ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
162 /* Update unwind state */
165 state->reliable = reliable;
166 state->reuse_sp = reuse_sp;
168 EXPORT_SYMBOL_GPL(__unwind_start);