blob: 3a2fa203637a99d8da93962da8e151f07d1bf0fc [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +01003#include <linux/sched/debug.h>
Russell Kingf16fb1e2007-04-28 09:59:37 +01004#include <linux/stacktrace.h>
5
Catalin Marinas2d7c11b2009-02-11 13:07:53 +01006#include <asm/stacktrace.h>
Russell King07b40342014-05-03 16:17:16 +01007#include <asm/traps.h>
Russell Kingf16fb1e2007-04-28 09:59:37 +01008
Catalin Marinas2d7c11b2009-02-11 13:07:53 +01009#if defined(CONFIG_FRAME_POINTER) && !defined(CONFIG_ARM_UNWIND)
10/*
11 * Unwind the current stack frame and store the new register values in the
12 * structure passed as argument. Unwinding is equivalent to a function return,
13 * hence the new PC value rather than LR should be used for backtrace.
14 *
15 * With framepointer enabled, a simple function prologue looks like this:
16 * mov ip, sp
17 * stmdb sp!, {fp, ip, lr, pc}
18 * sub fp, ip, #4
19 *
20 * A simple function epilogue looks like this:
21 * ldm sp, {fp, sp, pc}
22 *
23 * Note that with framepointer enabled, even the leaf functions have the same
24 * prologue and epilogue, therefore we can ignore the LR value in this case.
25 */
Uwe Kleine-König4bf1fa52009-07-21 09:56:27 +010026int notrace unwind_frame(struct stackframe *frame)
Russell Kingf16fb1e2007-04-28 09:59:37 +010027{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010028 unsigned long high, low;
29 unsigned long fp = frame->fp;
Russell Kingf16fb1e2007-04-28 09:59:37 +010030
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010031 /* only go to a higher address on the stack */
32 low = frame->sp;
Will Deacond33aadb2010-11-04 18:22:51 +010033 high = ALIGN(low, THREAD_SIZE);
Russell Kingf16fb1e2007-04-28 09:59:37 +010034
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010035 /* check current frame pointer is within bounds */
Konstantin Khlebnikov3abb6672013-12-05 14:23:48 +010036 if (fp < low + 12 || fp > high - 4)
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010037 return -EINVAL;
38
39 /* restore the registers from the stack frame */
40 frame->fp = *(unsigned long *)(fp - 12);
41 frame->sp = *(unsigned long *)(fp - 8);
42 frame->pc = *(unsigned long *)(fp - 4);
43
44 return 0;
45}
46#endif
47
Uwe Kleine-König4bf1fa52009-07-21 09:56:27 +010048void notrace walk_stackframe(struct stackframe *frame,
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010049 int (*fn)(struct stackframe *, void *), void *data)
50{
51 while (1) {
52 int ret;
Russell Kingf16fb1e2007-04-28 09:59:37 +010053
54 if (fn(frame, data))
55 break;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010056 ret = unwind_frame(frame);
57 if (ret < 0)
58 break;
59 }
Russell Kingf16fb1e2007-04-28 09:59:37 +010060}
Al Viro7b104bc2007-05-15 20:37:20 +010061EXPORT_SYMBOL(walk_stackframe);
Russell Kingf16fb1e2007-04-28 09:59:37 +010062
63#ifdef CONFIG_STACKTRACE
64struct stack_trace_data {
65 struct stack_trace *trace;
Russell King07b40342014-05-03 16:17:16 +010066 unsigned long last_pc;
Nicolas Pitref76e9152008-04-24 01:31:46 -040067 unsigned int no_sched_functions;
Russell Kingf16fb1e2007-04-28 09:59:37 +010068 unsigned int skip;
69};
70
71static int save_trace(struct stackframe *frame, void *d)
72{
73 struct stack_trace_data *data = d;
74 struct stack_trace *trace = data->trace;
Russell King07b40342014-05-03 16:17:16 +010075 struct pt_regs *regs;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010076 unsigned long addr = frame->pc;
Russell Kingf16fb1e2007-04-28 09:59:37 +010077
Nicolas Pitref76e9152008-04-24 01:31:46 -040078 if (data->no_sched_functions && in_sched_functions(addr))
79 return 0;
Russell Kingf16fb1e2007-04-28 09:59:37 +010080 if (data->skip) {
81 data->skip--;
82 return 0;
83 }
84
Nicolas Pitref76e9152008-04-24 01:31:46 -040085 trace->entries[trace->nr_entries++] = addr;
Russell Kingf16fb1e2007-04-28 09:59:37 +010086
Russell King07b40342014-05-03 16:17:16 +010087 if (trace->nr_entries >= trace->max_entries)
88 return 1;
89
90 /*
91 * in_exception_text() is designed to test if the PC is one of
92 * the functions which has an exception stack above it, but
93 * unfortunately what is in frame->pc is the return LR value,
94 * not the saved PC value. So, we need to track the previous
95 * frame PC value when doing this.
96 */
97 addr = data->last_pc;
98 data->last_pc = frame->pc;
99 if (!in_exception_text(addr))
100 return 0;
101
102 regs = (struct pt_regs *)frame->sp;
103
104 trace->entries[trace->nr_entries++] = regs->ARM_pc;
105
Russell Kingf16fb1e2007-04-28 09:59:37 +0100106 return trace->nr_entries >= trace->max_entries;
107}
108
Russell King3683f442014-05-03 11:03:28 +0100109/* This must be noinline to so that our skip calculation works correctly */
110static noinline void __save_stack_trace(struct task_struct *tsk,
111 struct stack_trace *trace, unsigned int nosched)
Russell Kingf16fb1e2007-04-28 09:59:37 +0100112{
113 struct stack_trace_data data;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100114 struct stackframe frame;
Russell Kingf16fb1e2007-04-28 09:59:37 +0100115
116 data.trace = trace;
Russell King07b40342014-05-03 16:17:16 +0100117 data.last_pc = ULONG_MAX;
Russell Kingf16fb1e2007-04-28 09:59:37 +0100118 data.skip = trace->skip;
Russell King3683f442014-05-03 11:03:28 +0100119 data.no_sched_functions = nosched;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400120
121 if (tsk != current) {
122#ifdef CONFIG_SMP
123 /*
Russell Kingd5996b22011-01-15 09:27:04 +0000124 * What guarantees do we have here that 'tsk' is not
125 * running on another CPU? For now, ignore it as we
126 * can't guarantee we won't explode.
Nicolas Pitref76e9152008-04-24 01:31:46 -0400127 */
Russell Kingd5996b22011-01-15 09:27:04 +0000128 if (trace->nr_entries < trace->max_entries)
129 trace->entries[trace->nr_entries++] = ULONG_MAX;
130 return;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400131#else
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100132 frame.fp = thread_saved_fp(tsk);
133 frame.sp = thread_saved_sp(tsk);
134 frame.lr = 0; /* recovered from the stack */
135 frame.pc = thread_saved_pc(tsk);
Nicolas Pitref76e9152008-04-24 01:31:46 -0400136#endif
137 } else {
Russell King3683f442014-05-03 11:03:28 +0100138 /* We don't want this function nor the caller */
139 data.skip += 2;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100140 frame.fp = (unsigned long)__builtin_frame_address(0);
Behan Webster74dbeee2014-09-27 00:31:08 +0100141 frame.sp = current_stack_pointer;
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100142 frame.lr = (unsigned long)__builtin_return_address(0);
Russell King3683f442014-05-03 11:03:28 +0100143 frame.pc = (unsigned long)__save_stack_trace;
Nicolas Pitref76e9152008-04-24 01:31:46 -0400144 }
Russell Kingf16fb1e2007-04-28 09:59:37 +0100145
Catalin Marinas2d7c11b2009-02-11 13:07:53 +0100146 walk_stackframe(&frame, save_trace, &data);
Nicolas Pitref76e9152008-04-24 01:31:46 -0400147 if (trace->nr_entries < trace->max_entries)
148 trace->entries[trace->nr_entries++] = ULONG_MAX;
149}
150
Lin Yongting9c986662014-05-04 16:27:41 +0100151void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
152{
153 struct stack_trace_data data;
154 struct stackframe frame;
155
156 data.trace = trace;
157 data.skip = trace->skip;
158 data.no_sched_functions = 0;
159
160 frame.fp = regs->ARM_fp;
161 frame.sp = regs->ARM_sp;
162 frame.lr = regs->ARM_lr;
163 frame.pc = regs->ARM_pc;
164
165 walk_stackframe(&frame, save_trace, &data);
166 if (trace->nr_entries < trace->max_entries)
167 trace->entries[trace->nr_entries++] = ULONG_MAX;
168}
169
Russell King3683f442014-05-03 11:03:28 +0100170void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
171{
172 __save_stack_trace(tsk, trace, 1);
173}
174
Nicolas Pitref76e9152008-04-24 01:31:46 -0400175void save_stack_trace(struct stack_trace *trace)
176{
Russell King3683f442014-05-03 11:03:28 +0100177 __save_stack_trace(current, trace, 0);
Russell Kingf16fb1e2007-04-28 09:59:37 +0100178}
Ingo Molnar7b4c9502008-07-03 09:17:55 +0200179EXPORT_SYMBOL_GPL(save_stack_trace);
Russell Kingf16fb1e2007-04-28 09:59:37 +0100180#endif