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