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 static inline bool is_final_pt_regs(struct unwind_state *state,
42 /* user mode or kernel thread pt_regs at the bottom of task stack */
43 if (task_pt_regs(state->task) == regs)
46 /* user mode pt_regs at the bottom of irq stack */
47 return state->stack_info.type == STACK_TYPE_IRQ &&
48 state->stack_info.end - sizeof(struct pt_regs) == (unsigned long)regs &&
49 READ_ONCE_NOCHECK(regs->psw.mask) & PSW_MASK_PSTATE;
52 bool unwind_next_frame(struct unwind_state *state)
54 struct stack_info *info = &state->stack_info;
55 struct stack_frame *sf;
63 sf = (struct stack_frame *) sp;
64 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
67 /* skip bogus %r14 or if is the same as regs->psw.addr */
68 if (!__kernel_text_address(ip) || state->ip == unwind_recover_ret_addr(state, ip)) {
70 return unwind_next_frame(state);
73 sf = (struct stack_frame *) state->sp;
74 sp = READ_ONCE_NOCHECK(sf->back_chain);
76 /* Non-zero back-chain points to the previous frame */
77 if (unlikely(outside_of_stack(state, sp))) {
78 if (!update_stack_info(state, sp))
81 sf = (struct stack_frame *) sp;
82 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
85 /* No back-chain, look for a pt_regs structure */
86 sp = state->sp + STACK_FRAME_OVERHEAD;
87 if (!on_stack(info, sp, sizeof(struct pt_regs)))
89 regs = (struct pt_regs *) sp;
90 if (is_final_pt_regs(state, regs))
92 ip = READ_ONCE_NOCHECK(regs->psw.addr);
93 sp = READ_ONCE_NOCHECK(regs->gprs[15]);
94 if (unlikely(outside_of_stack(state, sp))) {
95 if (!update_stack_info(state, sp))
102 /* Sanity check: ABI requires SP to be aligned 8 bytes. */
106 /* Update unwind state */
109 state->reliable = reliable;
110 state->ip = unwind_recover_ret_addr(state, ip);
116 state->stack_info.type = STACK_TYPE_UNKNOWN;
119 EXPORT_SYMBOL_GPL(unwind_next_frame);
121 void __unwind_start(struct unwind_state *state, struct task_struct *task,
122 struct pt_regs *regs, unsigned long first_frame)
124 struct stack_info *info = &state->stack_info;
125 struct stack_frame *sf;
126 unsigned long ip, sp;
128 memset(state, 0, sizeof(*state));
132 /* Don't even attempt to start from user mode regs: */
133 if (regs && user_mode(regs)) {
134 info->type = STACK_TYPE_UNKNOWN;
138 /* Get the instruction pointer from pt_regs or the stack frame */
142 } else if (task == current) {
143 sp = current_frame_address();
145 sp = task->thread.ksp;
148 /* Get current stack pointer and initialize stack info */
149 if (!update_stack_info(state, sp)) {
150 /* Something is wrong with the stack pointer */
151 info->type = STACK_TYPE_UNKNOWN;
157 /* Stack frame is within valid stack */
158 sf = (struct stack_frame *)sp;
159 ip = READ_ONCE_NOCHECK(sf->gprs[8]);
162 /* Update unwind state */
164 state->reliable = true;
165 state->ip = unwind_recover_ret_addr(state, ip);
169 /* Skip through the call chain to the specified starting frame */
170 while (!unwind_done(state)) {
171 if (on_stack(&state->stack_info, first_frame, sizeof(struct stack_frame))) {
172 if (state->sp >= first_frame)
175 unwind_next_frame(state);
178 EXPORT_SYMBOL_GPL(__unwind_start);