GNU Linux-libre 4.4.288-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 = *(unsigned long *)(fp);
54         frame->pc = frame->lr;
55         frame->lr = *(unsigned long *)(fp + 4);
56 #else
57         /* check current frame pointer is within bounds */
58         if (fp < low + 12 || fp > high - 4)
59                 return -EINVAL;
60
61         /* restore the registers from the stack frame */
62         frame->fp = *(unsigned long *)(fp - 12);
63         frame->sp = *(unsigned long *)(fp - 8);
64         frame->pc = *(unsigned long *)(fp - 4);
65 #endif
66
67         return 0;
68 }
69 #endif
70
71 void notrace walk_stackframe(struct stackframe *frame,
72                      int (*fn)(struct stackframe *, void *), void *data)
73 {
74         while (1) {
75                 int ret;
76
77                 if (fn(frame, data))
78                         break;
79                 ret = unwind_frame(frame);
80                 if (ret < 0)
81                         break;
82         }
83 }
84 EXPORT_SYMBOL(walk_stackframe);
85
86 #ifdef CONFIG_STACKTRACE
87 struct stack_trace_data {
88         struct stack_trace *trace;
89         unsigned long last_pc;
90         unsigned int no_sched_functions;
91         unsigned int skip;
92 };
93
94 static int save_trace(struct stackframe *frame, void *d)
95 {
96         struct stack_trace_data *data = d;
97         struct stack_trace *trace = data->trace;
98         struct pt_regs *regs;
99         unsigned long addr = frame->pc;
100
101         if (data->no_sched_functions && in_sched_functions(addr))
102                 return 0;
103         if (data->skip) {
104                 data->skip--;
105                 return 0;
106         }
107
108         trace->entries[trace->nr_entries++] = addr;
109
110         if (trace->nr_entries >= trace->max_entries)
111                 return 1;
112
113         /*
114          * in_exception_text() is designed to test if the PC is one of
115          * the functions which has an exception stack above it, but
116          * unfortunately what is in frame->pc is the return LR value,
117          * not the saved PC value.  So, we need to track the previous
118          * frame PC value when doing this.
119          */
120         addr = data->last_pc;
121         data->last_pc = frame->pc;
122         if (!in_exception_text(addr))
123                 return 0;
124
125         regs = (struct pt_regs *)frame->sp;
126
127         trace->entries[trace->nr_entries++] = regs->ARM_pc;
128
129         return trace->nr_entries >= trace->max_entries;
130 }
131
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)
135 {
136         struct stack_trace_data data;
137         struct stackframe frame;
138
139         data.trace = trace;
140         data.last_pc = ULONG_MAX;
141         data.skip = trace->skip;
142         data.no_sched_functions = nosched;
143
144         if (tsk != current) {
145 #ifdef CONFIG_SMP
146                 /*
147                  * What guarantees do we have here that 'tsk' is not
148                  * running on another CPU?  For now, ignore it as we
149                  * can't guarantee we won't explode.
150                  */
151                 if (trace->nr_entries < trace->max_entries)
152                         trace->entries[trace->nr_entries++] = ULONG_MAX;
153                 return;
154 #else
155                 frame.fp = thread_saved_fp(tsk);
156                 frame.sp = thread_saved_sp(tsk);
157                 frame.lr = 0;           /* recovered from the stack */
158                 frame.pc = thread_saved_pc(tsk);
159 #endif
160         } else {
161                 /* We don't want this function nor the caller */
162                 data.skip += 2;
163                 frame.fp = (unsigned long)__builtin_frame_address(0);
164                 frame.sp = current_stack_pointer;
165                 frame.lr = (unsigned long)__builtin_return_address(0);
166                 frame.pc = (unsigned long)__save_stack_trace;
167         }
168
169         walk_stackframe(&frame, save_trace, &data);
170         if (trace->nr_entries < trace->max_entries)
171                 trace->entries[trace->nr_entries++] = ULONG_MAX;
172 }
173
174 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
175 {
176         struct stack_trace_data data;
177         struct stackframe frame;
178
179         data.trace = trace;
180         data.skip = trace->skip;
181         data.no_sched_functions = 0;
182
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
188         walk_stackframe(&frame, save_trace, &data);
189         if (trace->nr_entries < trace->max_entries)
190                 trace->entries[trace->nr_entries++] = ULONG_MAX;
191 }
192
193 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
194 {
195         __save_stack_trace(tsk, trace, 1);
196 }
197
198 void save_stack_trace(struct stack_trace *trace)
199 {
200         __save_stack_trace(current, trace, 0);
201 }
202 EXPORT_SYMBOL_GPL(save_stack_trace);
203 #endif