GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / arm / kernel / stacktrace.c
1 #include <linux/export.h>
2 #include <linux/sched.h>
3 #include <linux/stacktrace.h>
4
5 #include <asm/stacktrace.h>
6 #include <asm/traps.h>
7
8 #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  * When compiled with clang, pc and sp are not pushed. A simple function
23  * prologue looks like this when built with clang:
24  *
25  *      stmdb   {..., fp, lr}
26  *      add     fp, sp, #x
27  *      sub     sp, sp, #y
28  *
29  * A simple function epilogue looks like this when built with clang:
30  *
31  *      sub     sp, fp, #x
32  *      ldm     {..., fp, pc}
33  *
34  *
35  * Note that with framepointer enabled, even the leaf functions have the same
36  * prologue and epilogue, therefore we can ignore the LR value in this case.
37  */
38 int notrace unwind_frame(struct stackframe *frame)
39 {
40         unsigned long high, low;
41         unsigned long fp = frame->fp;
42
43         /* only go to a higher address on the stack */
44         low = frame->sp;
45         high = ALIGN(low, THREAD_SIZE);
46
47 #ifdef CONFIG_CC_IS_CLANG
48         /* check current frame pointer is within bounds */
49         if (fp < low + 4 || fp > high - 4)
50                 return -EINVAL;
51
52         frame->sp = frame->fp;
53         frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
54         frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
55 #else
56         /* check current frame pointer is within bounds */
57         if (fp < low + 12 || fp > high - 4)
58                 return -EINVAL;
59
60         /* restore the registers from the stack frame */
61         frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
62         frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
63         frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
64 #endif
65
66         return 0;
67 }
68 #endif
69
70 void notrace walk_stackframe(struct stackframe *frame,
71                      int (*fn)(struct stackframe *, void *), void *data)
72 {
73         while (1) {
74                 int ret;
75
76                 if (fn(frame, data))
77                         break;
78                 ret = unwind_frame(frame);
79                 if (ret < 0)
80                         break;
81         }
82 }
83 EXPORT_SYMBOL(walk_stackframe);
84
85 #ifdef CONFIG_STACKTRACE
86 struct stack_trace_data {
87         struct stack_trace *trace;
88         unsigned long last_pc;
89         unsigned int no_sched_functions;
90         unsigned int skip;
91 };
92
93 static int save_trace(struct stackframe *frame, void *d)
94 {
95         struct stack_trace_data *data = d;
96         struct stack_trace *trace = data->trace;
97         struct pt_regs *regs;
98         unsigned long addr = frame->pc;
99
100         if (data->no_sched_functions && in_sched_functions(addr))
101                 return 0;
102         if (data->skip) {
103                 data->skip--;
104                 return 0;
105         }
106
107         trace->entries[trace->nr_entries++] = addr;
108
109         if (trace->nr_entries >= trace->max_entries)
110                 return 1;
111
112         /*
113          * in_exception_text() is designed to test if the PC is one of
114          * the functions which has an exception stack above it, but
115          * unfortunately what is in frame->pc is the return LR value,
116          * not the saved PC value.  So, we need to track the previous
117          * frame PC value when doing this.
118          */
119         addr = data->last_pc;
120         data->last_pc = frame->pc;
121         if (!in_exception_text(addr))
122                 return 0;
123
124         regs = (struct pt_regs *)frame->sp;
125
126         trace->entries[trace->nr_entries++] = regs->ARM_pc;
127
128         return trace->nr_entries >= trace->max_entries;
129 }
130
131 /* This must be noinline to so that our skip calculation works correctly */
132 static noinline void __save_stack_trace(struct task_struct *tsk,
133         struct stack_trace *trace, unsigned int nosched)
134 {
135         struct stack_trace_data data;
136         struct stackframe frame;
137
138         data.trace = trace;
139         data.last_pc = ULONG_MAX;
140         data.skip = trace->skip;
141         data.no_sched_functions = nosched;
142
143         if (tsk != current) {
144 #ifdef CONFIG_SMP
145                 /*
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.
149                  */
150                 if (trace->nr_entries < trace->max_entries)
151                         trace->entries[trace->nr_entries++] = ULONG_MAX;
152                 return;
153 #else
154                 frame.fp = thread_saved_fp(tsk);
155                 frame.sp = thread_saved_sp(tsk);
156                 frame.lr = 0;           /* recovered from the stack */
157                 frame.pc = thread_saved_pc(tsk);
158 #endif
159         } else {
160                 /* We don't want this function nor the caller */
161                 data.skip += 2;
162                 frame.fp = (unsigned long)__builtin_frame_address(0);
163                 frame.sp = current_stack_pointer;
164                 frame.lr = (unsigned long)__builtin_return_address(0);
165                 frame.pc = (unsigned long)__save_stack_trace;
166         }
167
168         walk_stackframe(&frame, save_trace, &data);
169         if (trace->nr_entries < trace->max_entries)
170                 trace->entries[trace->nr_entries++] = ULONG_MAX;
171 }
172
173 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
174 {
175         struct stack_trace_data data;
176         struct stackframe frame;
177
178         data.trace = trace;
179         data.skip = trace->skip;
180         data.no_sched_functions = 0;
181
182         frame.fp = regs->ARM_fp;
183         frame.sp = regs->ARM_sp;
184         frame.lr = regs->ARM_lr;
185         frame.pc = regs->ARM_pc;
186
187         walk_stackframe(&frame, save_trace, &data);
188         if (trace->nr_entries < trace->max_entries)
189                 trace->entries[trace->nr_entries++] = ULONG_MAX;
190 }
191
192 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
193 {
194         __save_stack_trace(tsk, trace, 1);
195 }
196
197 void save_stack_trace(struct stack_trace *trace)
198 {
199         __save_stack_trace(current, trace, 0);
200 }
201 EXPORT_SYMBOL_GPL(save_stack_trace);
202 #endif