blob: f065eb05d254ab89fadf1fcde45ce82e99fda0e8 [file] [log] [blame]
Paul Gortmakerecea4ab2011-07-22 10:58:34 -04001#include <linux/export.h>
Russell Kingf16fb1e2007-04-28 09:59:37 +01002#include <linux/sched.h>
3#include <linux/stacktrace.h>
4
Catalin Marinas2d7c11b2009-02-11 13:07:53 +01005#include <asm/stacktrace.h>
Russell King07b40342014-05-03 16:17:16 +01006#include <asm/traps.h>
Russell Kingf16fb1e2007-04-28 09:59:37 +01007
Catalin Marinas2d7c11b2009-02-11 13:07:53 +01008#if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
9/*
10 * Unwind the current stack frame and store the new register values in the
11 * structure passed as argument. Unwinding is equivalent to a function return,
12 * hence the new PC value rather than LR should be used for backtrace.
13 *
14 * With framepointer enabled, a simple function prologue looks like this:
15 * mov ip, sp
16 * stmdb sp!, {fp, ip, lr, pc}
17 * sub fp, ip, #4
18 *
19 * A simple function epilogue looks like this:
20 * ldm sp, {fp, sp, pc}
21 *
22 * Note that with framepointer enabled, even the leaf functions have the same
23 * prologue and epilogue, therefore we can ignore the LR value in this case.
24 */
Uwe Kleine-König4bf1fa52009-07-21 09:56:27 +010025int notrace unwind_frame(struct stackframe *frame)
Russell Kingf16fb1e2007-04-28 09:59:37 +010026{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010027 unsigned long high, low;
28 unsigned long fp = frame->fp;
Russell Kingf16fb1e2007-04-28 09:59:37 +010029
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010030 /* only go to a higher address on the stack */
31 low = frame->sp;
Will Deacond33aadb2010-11-04 18:22:51 +010032 high = ALIGN(low, THREAD_SIZE);
Russell Kingf16fb1e2007-04-28 09:59:37 +010033
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010034 /* check current frame pointer is within bounds */
Konstantin Khlebnikov3abb6672013-12-05 14:23:48 +010035 if (fp < low + 12 || fp > high - 4)
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010036 return -EINVAL;
37
38 /* restore the registers from the stack frame */
39 frame->fp = *(unsigned long *)(fp - 12);
40 frame->sp = *(unsigned long *)(fp - 8);
41 frame->pc = *(unsigned long *)(fp - 4);
42
43 return 0;
44}
45#endif
46
Uwe Kleine-König4bf1fa52009-07-21 09:56:27 +010047void notrace walk_stackframe(struct stackframe *frame,
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010048 int (*fn)(struct stackframe *, void *), void *data)
49{
50 while (1) {
51 int ret;
Russell Kingf16fb1e2007-04-28 09:59:37 +010052
53 if (fn(frame, data))
54 break;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010055 ret = unwind_frame(frame);
56 if (ret < 0)
57 break;
58 }
Russell Kingf16fb1e2007-04-28 09:59:37 +010059}
Al Viro7b104bc2007-05-15 20:37:20 +010060EXPORT_SYMBOL(walk_stackframe);
Russell Kingf16fb1e2007-04-28 09:59:37 +010061
62#ifdef CONFIG_STACKTRACE
63struct stack_trace_data {
64 struct stack_trace *trace;
Russell King07b40342014-05-03 16:17:16 +010065 unsigned long last_pc;
Nicolas Pitref76e9152008-04-24 01:31:46 -040066 unsigned int no_sched_functions;
Russell Kingf16fb1e2007-04-28 09:59:37 +010067 unsigned int skip;
68};
69
70static int save_trace(struct stackframe *frame, void *d)
71{
72 struct stack_trace_data *data = d;
73 struct stack_trace *trace = data->trace;
Russell King07b40342014-05-03 16:17:16 +010074 struct pt_regs *regs;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010075 unsigned long addr = frame->pc;
Russell Kingf16fb1e2007-04-28 09:59:37 +010076
Nicolas Pitref76e9152008-04-24 01:31:46 -040077 if (data->no_sched_functions && in_sched_functions(addr))
78 return 0;
Russell Kingf16fb1e2007-04-28 09:59:37 +010079 if (data->skip) {
80 data->skip--;
81 return 0;
82 }
83
Nicolas Pitref76e9152008-04-24 01:31:46 -040084 trace->entries[trace->nr_entries++] = addr;
Russell Kingf16fb1e2007-04-28 09:59:37 +010085
Russell King07b40342014-05-03 16:17:16 +010086 if (trace->nr_entries >= trace->max_entries)
87 return 1;
88
89 /*
90 * in_exception_text() is designed to test if the PC is one of
91 * the functions which has an exception stack above it, but
92 * unfortunately what is in frame->pc is the return LR value,
93 * not the saved PC value. So, we need to track the previous
94 * frame PC value when doing this.
95 */
96 addr = data->last_pc;
97 data->last_pc = frame->pc;
98 if (!in_exception_text(addr))
99 return 0;
100
101 regs = (struct pt_regs *)frame->sp;
102
103 trace->entries[trace->nr_entries++] = regs->ARM_pc;
104
Russell Kingf16fb1e2007-04-28 09:59:37 +0100105 return trace->nr_entries >= trace->max_entries;
106}
107
Russell King3683f442014-05-03 11:03:28 +0100108/* This must be noinline to so that our skip calculation works correctly */
109static noinline void __save_stack_trace(struct task_struct *tsk,
110 struct stack_trace *trace, unsigned int nosched)
Russell Kingf16fb1e2007-04-28 09:59:37 +0100111{
112 struct stack_trace_data data;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100113 struct stackframe frame;
Russell Kingf16fb1e2007-04-28 09:59:37 +0100114
115 data.trace = trace;
Russell King07b40342014-05-03 16:17:16 +0100116 data.last_pc = ULONG_MAX;
Russell Kingf16fb1e2007-04-28 09:59:37 +0100117 data.skip = trace->skip;
Russell King3683f442014-05-03 11:03:28 +0100118 data.no_sched_functions = nosched;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400119
120 if (tsk != current) {
121#ifdef CONFIG_SMP
122 /*
Russell Kingd5996b22011-01-15 09:27:04 +0000123 * What guarantees do we have here that 'tsk' is not
124 * running on another CPU? For now, ignore it as we
125 * can't guarantee we won't explode.
Nicolas Pitref76e9152008-04-24 01:31:46 -0400126 */
Russell Kingd5996b22011-01-15 09:27:04 +0000127 if (trace->nr_entries < trace->max_entries)
128 trace->entries[trace->nr_entries++] = ULONG_MAX;
129 return;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400130#else
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100131 frame.fp = thread_saved_fp(tsk);
132 frame.sp = thread_saved_sp(tsk);
133 frame.lr = 0; /* recovered from the stack */
134 frame.pc = thread_saved_pc(tsk);
Nicolas Pitref76e9152008-04-24 01:31:46 -0400135#endif
136 } else {
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100137 register unsigned long current_sp asm ("sp");
138
Russell King3683f442014-05-03 11:03:28 +0100139 /* We don't want this function nor the caller */
140 data.skip += 2;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100141 frame.fp = (unsigned long)__builtin_frame_address(0);
142 frame.sp = current_sp;
143 frame.lr = (unsigned long)__builtin_return_address(0);
Russell King3683f442014-05-03 11:03:28 +0100144 frame.pc = (unsigned long)__save_stack_trace;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400145 }
Russell Kingf16fb1e2007-04-28 09:59:37 +0100146
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100147 walk_stackframe(&frame, save_trace, &data);
Nicolas Pitref76e9152008-04-24 01:31:46 -0400148 if (trace->nr_entries < trace->max_entries)
149 trace->entries[trace->nr_entries++] = ULONG_MAX;
150}
151
Lin Yongting9c986662014-05-04 16:27:41 +0100152void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
153{
154 struct stack_trace_data data;
155 struct stackframe frame;
156
157 data.trace = trace;
158 data.skip = trace->skip;
159 data.no_sched_functions = 0;
160
161 frame.fp = regs->ARM_fp;
162 frame.sp = regs->ARM_sp;
163 frame.lr = regs->ARM_lr;
164 frame.pc = regs->ARM_pc;
165
166 walk_stackframe(&frame, save_trace, &data);
167 if (trace->nr_entries < trace->max_entries)
168 trace->entries[trace->nr_entries++] = ULONG_MAX;
169}
170
Russell King3683f442014-05-03 11:03:28 +0100171void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
172{
173 __save_stack_trace(tsk, trace, 1);
174}
175
Nicolas Pitref76e9152008-04-24 01:31:46 -0400176void save_stack_trace(struct stack_trace *trace)
177{
Russell King3683f442014-05-03 11:03:28 +0100178 __save_stack_trace(current, trace, 0);
Russell Kingf16fb1e2007-04-28 09:59:37 +0100179}
Ingo Molnar7b4c9502008-07-03 09:17:55 +0200180EXPORT_SYMBOL_GPL(save_stack_trace);
Russell Kingf16fb1e2007-04-28 09:59:37 +0100181#endif