1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/export.h>
3 #include <linux/kprobes.h>
4 #include <linux/sched.h>
5 #include <linux/sched/debug.h>
6 #include <linux/stacktrace.h>
8 #include <asm/sections.h>
9 #include <asm/stacktrace.h>
10 #include <asm/traps.h>
12 #if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
14 * Unwind the current stack frame and store the new register values in the
15 * structure passed as argument. Unwinding is equivalent to a function return,
16 * hence the new PC value rather than LR should be used for backtrace.
18 * With framepointer enabled, a simple function prologue looks like this:
20 * stmdb sp!, {fp, ip, lr, pc}
23 * A simple function epilogue looks like this:
24 * ldm sp, {fp, sp, pc}
26 * When compiled with clang, pc and sp are not pushed. A simple function
27 * prologue looks like this when built with clang:
33 * A simple function epilogue looks like this when built with clang:
39 * Note that with framepointer enabled, even the leaf functions have the same
40 * prologue and epilogue, therefore we can ignore the LR value in this case.
42 int notrace unwind_frame(struct stackframe *frame)
44 unsigned long high, low;
45 unsigned long fp = frame->fp;
47 /* only go to a higher address on the stack */
49 high = ALIGN(low, THREAD_SIZE);
51 #ifdef CONFIG_CC_IS_CLANG
52 /* check current frame pointer is within bounds */
53 if (fp < low + 4 || fp > high - 4)
56 frame->sp = frame->fp;
57 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
58 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
60 /* check current frame pointer is within bounds */
61 if (fp < low + 12 || fp > high - 4)
64 /* restore the registers from the stack frame */
65 frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
66 frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
67 frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
69 #ifdef CONFIG_KRETPROBES
70 if (is_kretprobe_trampoline(frame->pc))
71 frame->pc = kretprobe_find_ret_addr(frame->tsk,
72 (void *)frame->fp, &frame->kr_cur);
79 void notrace walk_stackframe(struct stackframe *frame,
80 int (*fn)(struct stackframe *, void *), void *data)
87 ret = unwind_frame(frame);
92 EXPORT_SYMBOL(walk_stackframe);
94 #ifdef CONFIG_STACKTRACE
95 struct stack_trace_data {
96 struct stack_trace *trace;
97 unsigned int no_sched_functions;
101 static int save_trace(struct stackframe *frame, void *d)
103 struct stack_trace_data *data = d;
104 struct stack_trace *trace = data->trace;
105 struct pt_regs *regs;
106 unsigned long addr = frame->pc;
108 if (data->no_sched_functions && in_sched_functions(addr))
115 trace->entries[trace->nr_entries++] = addr;
117 if (trace->nr_entries >= trace->max_entries)
120 if (!in_entry_text(frame->pc))
123 regs = (struct pt_regs *)frame->sp;
124 if ((unsigned long)®s[1] > ALIGN(frame->sp, THREAD_SIZE))
127 trace->entries[trace->nr_entries++] = regs->ARM_pc;
129 return trace->nr_entries >= trace->max_entries;
132 /* This must be noinline to so that our skip calculation works correctly */
133 static noinline void __save_stack_trace(struct task_struct *tsk,
134 struct stack_trace *trace, unsigned int nosched)
136 struct stack_trace_data data;
137 struct stackframe frame;
140 data.skip = trace->skip;
141 data.no_sched_functions = nosched;
143 if (tsk != current) {
146 * What guarantees do we have here that 'tsk' is not
147 * running on another CPU? For now, ignore it as we
148 * can't guarantee we won't explode.
152 frame.fp = thread_saved_fp(tsk);
153 frame.sp = thread_saved_sp(tsk);
154 frame.lr = 0; /* recovered from the stack */
155 frame.pc = thread_saved_pc(tsk);
158 /* We don't want this function nor the caller */
160 frame.fp = (unsigned long)__builtin_frame_address(0);
161 frame.sp = current_stack_pointer;
162 frame.lr = (unsigned long)__builtin_return_address(0);
164 frame.pc = (unsigned long)&&here;
166 #ifdef CONFIG_KRETPROBES
171 walk_stackframe(&frame, save_trace, &data);
174 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
176 struct stack_trace_data data;
177 struct stackframe frame;
180 data.skip = trace->skip;
181 data.no_sched_functions = 0;
183 frame.fp = regs->ARM_fp;
184 frame.sp = regs->ARM_sp;
185 frame.lr = regs->ARM_lr;
186 frame.pc = regs->ARM_pc;
187 #ifdef CONFIG_KRETPROBES
192 walk_stackframe(&frame, save_trace, &data);
195 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
197 __save_stack_trace(tsk, trace, 1);
199 EXPORT_SYMBOL(save_stack_trace_tsk);
201 void save_stack_trace(struct stack_trace *trace)
203 __save_stack_trace(current, trace, 0);
205 EXPORT_SYMBOL_GPL(save_stack_trace);