GNU Linux-libre 4.19.286-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 = READ_ONCE_NOCHECK(*(unsigned long *)(fp));
56         frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 4));
57 #else
58         /* check current frame pointer is within bounds */
59         if (fp < low + 12 || fp > high - 4)
60                 return -EINVAL;
61
62         /* restore the registers from the stack frame */
63         frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 12));
64         frame->sp = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 8));
65         frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp - 4));
66 #endif
67
68         return 0;
69 }
70 #endif
71
72 void notrace walk_stackframe(struct stackframe *frame,
73                      int (*fn)(struct stackframe *, void *), void *data)
74 {
75         while (1) {
76                 int ret;
77
78                 if (fn(frame, data))
79                         break;
80                 ret = unwind_frame(frame);
81                 if (ret < 0)
82                         break;
83         }
84 }
85 EXPORT_SYMBOL(walk_stackframe);
86
87 #ifdef CONFIG_STACKTRACE
88 struct stack_trace_data {
89         struct stack_trace *trace;
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         if (!in_entry_text(frame->pc))
114                 return 0;
115
116         regs = (struct pt_regs *)frame->sp;
117         if ((unsigned long)&regs[1] > ALIGN(frame->sp, THREAD_SIZE))
118                 return 0;
119
120         trace->entries[trace->nr_entries++] = regs->ARM_pc;
121
122         return trace->nr_entries >= trace->max_entries;
123 }
124
125 /* This must be noinline to so that our skip calculation works correctly */
126 static noinline void __save_stack_trace(struct task_struct *tsk,
127         struct stack_trace *trace, unsigned int nosched)
128 {
129         struct stack_trace_data data;
130         struct stackframe frame;
131
132         data.trace = trace;
133         data.skip = trace->skip;
134         data.no_sched_functions = nosched;
135
136         if (tsk != current) {
137 #ifdef CONFIG_SMP
138                 /*
139                  * What guarantees do we have here that 'tsk' is not
140                  * running on another CPU?  For now, ignore it as we
141                  * can't guarantee we won't explode.
142                  */
143                 if (trace->nr_entries < trace->max_entries)
144                         trace->entries[trace->nr_entries++] = ULONG_MAX;
145                 return;
146 #else
147                 frame.fp = thread_saved_fp(tsk);
148                 frame.sp = thread_saved_sp(tsk);
149                 frame.lr = 0;           /* recovered from the stack */
150                 frame.pc = thread_saved_pc(tsk);
151 #endif
152         } else {
153                 /* We don't want this function nor the caller */
154                 data.skip += 2;
155                 frame.fp = (unsigned long)__builtin_frame_address(0);
156                 frame.sp = current_stack_pointer;
157                 frame.lr = (unsigned long)__builtin_return_address(0);
158                 frame.pc = (unsigned long)__save_stack_trace;
159         }
160
161         walk_stackframe(&frame, save_trace, &data);
162         if (trace->nr_entries < trace->max_entries)
163                 trace->entries[trace->nr_entries++] = ULONG_MAX;
164 }
165
166 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
167 {
168         struct stack_trace_data data;
169         struct stackframe frame;
170
171         data.trace = trace;
172         data.skip = trace->skip;
173         data.no_sched_functions = 0;
174
175         frame.fp = regs->ARM_fp;
176         frame.sp = regs->ARM_sp;
177         frame.lr = regs->ARM_lr;
178         frame.pc = regs->ARM_pc;
179
180         walk_stackframe(&frame, save_trace, &data);
181         if (trace->nr_entries < trace->max_entries)
182                 trace->entries[trace->nr_entries++] = ULONG_MAX;
183 }
184
185 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
186 {
187         __save_stack_trace(tsk, trace, 1);
188 }
189 EXPORT_SYMBOL(save_stack_trace_tsk);
190
191 void save_stack_trace(struct stack_trace *trace)
192 {
193         __save_stack_trace(current, trace, 0);
194 }
195 EXPORT_SYMBOL_GPL(save_stack_trace);
196 #endif