1
2#include <linux/sched.h>
3#include <linux/sched/task.h>
4#include <linux/sched/task_stack.h>
5#include <linux/interrupt.h>
6#include <asm/sections.h>
7#include <asm/ptrace.h>
8#include <asm/bitops.h>
9#include <asm/stacktrace.h>
10#include <asm/unwind.h>
11
12#define FRAME_HEADER_SIZE (sizeof(long) * 2)
13
14unsigned long unwind_get_return_address(struct unwind_state *state)
15{
16 if (unwind_done(state))
17 return 0;
18
19 return __kernel_text_address(state->ip) ? state->ip : 0;
20}
21EXPORT_SYMBOL_GPL(unwind_get_return_address);
22
23unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
24{
25 if (unwind_done(state))
26 return NULL;
27
28 return state->regs ? &state->regs->ip : state->bp + 1;
29}
30
31static void unwind_dump(struct unwind_state *state)
32{
33 static bool dumped_before = false;
34 bool prev_zero, zero = false;
35 unsigned long word, *sp;
36 struct stack_info stack_info = {0};
37 unsigned long visit_mask = 0;
38
39 if (dumped_before)
40 return;
41
42 dumped_before = true;
43
44 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
45 state->stack_info.type, state->stack_info.next_sp,
46 state->stack_mask, state->graph_idx);
47
48 for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
49 sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
50 if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
51 break;
52
53 for (; sp < stack_info.end; sp++) {
54
55 word = READ_ONCE_NOCHECK(*sp);
56
57 prev_zero = zero;
58 zero = word == 0;
59
60 if (zero) {
61 if (!prev_zero)
62 printk_deferred("%p: %0*x ...\n",
63 sp, BITS_PER_LONG/4, 0);
64 continue;
65 }
66
67 printk_deferred("%p: %0*lx (%pB)\n",
68 sp, BITS_PER_LONG/4, word, (void *)word);
69 }
70 }
71}
72
73static bool in_entry_code(unsigned long ip)
74{
75 char *addr = (char *)ip;
76
77 return addr >= __entry_text_start && addr < __entry_text_end;
78}
79
80static inline unsigned long *last_frame(struct unwind_state *state)
81{
82 return (unsigned long *)task_pt_regs(state->task) - 2;
83}
84
85static bool is_last_frame(struct unwind_state *state)
86{
87 return state->bp == last_frame(state);
88}
89
90#ifdef CONFIG_X86_32
91#define GCC_REALIGN_WORDS 3
92#else
93#define GCC_REALIGN_WORDS 1
94#endif
95
96static inline unsigned long *last_aligned_frame(struct unwind_state *state)
97{
98 return last_frame(state) - GCC_REALIGN_WORDS;
99}
100
101static bool is_last_aligned_frame(struct unwind_state *state)
102{
103 unsigned long *last_bp = last_frame(state);
104 unsigned long *aligned_bp = last_aligned_frame(state);
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
130}
131
132static bool is_last_ftrace_frame(struct unwind_state *state)
133{
134 unsigned long *last_bp = last_frame(state);
135 unsigned long *last_ftrace_bp = last_bp - 3;
136
137
138
139
140
141
142
143
144
145
146
147
148
149 return (state->bp == last_ftrace_bp &&
150 *state->bp == *(state->bp + 2) &&
151 *(state->bp + 1) == *(state->bp + 4));
152}
153
154static bool is_last_task_frame(struct unwind_state *state)
155{
156 return is_last_frame(state) || is_last_aligned_frame(state) ||
157 is_last_ftrace_frame(state);
158}
159
160
161
162
163
164#ifdef CONFIG_X86_64
165static struct pt_regs *decode_frame_pointer(unsigned long *bp)
166{
167 unsigned long regs = (unsigned long)bp;
168
169 if (!(regs & 0x1))
170 return NULL;
171
172 return (struct pt_regs *)(regs & ~0x1);
173}
174#else
175static struct pt_regs *decode_frame_pointer(unsigned long *bp)
176{
177 unsigned long regs = (unsigned long)bp;
178
179 if (regs & 0x80000000)
180 return NULL;
181
182 return (struct pt_regs *)(regs | 0x80000000);
183}
184#endif
185
186static bool update_stack_state(struct unwind_state *state,
187 unsigned long *next_bp)
188{
189 struct stack_info *info = &state->stack_info;
190 enum stack_type prev_type = info->type;
191 struct pt_regs *regs;
192 unsigned long *frame, *prev_frame_end, *addr_p, addr;
193 size_t len;
194
195 if (state->regs)
196 prev_frame_end = (void *)state->regs + sizeof(*state->regs);
197 else
198 prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
199
200
201 regs = decode_frame_pointer(next_bp);
202 if (regs) {
203 frame = (unsigned long *)regs;
204 len = sizeof(*regs);
205 state->got_irq = true;
206 } else {
207 frame = next_bp;
208 len = FRAME_HEADER_SIZE;
209 }
210
211
212
213
214
215
216
217
218 while (!on_stack(info, frame, len))
219 if (get_stack_info(info->next_sp, state->task, info,
220 &state->stack_mask))
221 return false;
222
223
224 if (state->orig_sp && state->stack_info.type == prev_type &&
225 frame < prev_frame_end)
226 return false;
227
228
229 if (regs) {
230 state->regs = regs;
231 state->bp = NULL;
232 } else {
233 state->bp = next_bp;
234 state->regs = NULL;
235 }
236
237
238 if (state->regs && user_mode(state->regs))
239 state->ip = 0;
240 else {
241 addr_p = unwind_get_return_address_ptr(state);
242 addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
243 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
244 addr, addr_p);
245 }
246
247
248 if (!state->orig_sp)
249 state->orig_sp = frame;
250
251 return true;
252}
253
254bool unwind_next_frame(struct unwind_state *state)
255{
256 struct pt_regs *regs;
257 unsigned long *next_bp;
258
259 if (unwind_done(state))
260 return false;
261
262
263 if (state->regs && user_mode(state->regs))
264 goto the_end;
265
266 if (is_last_task_frame(state)) {
267 regs = task_pt_regs(state->task);
268
269
270
271
272
273
274
275
276
277
278
279
280 if (!user_mode(regs))
281 goto the_end;
282
283
284
285
286
287
288 state->regs = regs;
289 state->bp = NULL;
290 state->ip = 0;
291 return true;
292 }
293
294
295 if (state->next_bp) {
296 next_bp = state->next_bp;
297 state->next_bp = NULL;
298 } else if (state->regs) {
299 next_bp = (unsigned long *)state->regs->bp;
300 } else {
301 next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
302 }
303
304
305 if (!update_stack_state(state, next_bp))
306 goto bad_address;
307
308 return true;
309
310bad_address:
311 state->error = true;
312
313
314
315
316
317
318
319
320 if (state->task != current)
321 goto the_end;
322
323
324
325
326
327 if (state->got_irq && in_entry_code(state->ip))
328 goto the_end;
329 if (state->regs &&
330 state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
331 state->regs->sp < (unsigned long)task_pt_regs(state->task))
332 goto the_end;
333
334
335
336
337
338 if (IS_ENABLED(CONFIG_X86_32))
339 goto the_end;
340
341 if (state->task != current)
342 goto the_end;
343
344 if (state->regs) {
345 printk_deferred_once(KERN_WARNING
346 "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
347 state->regs, state->task->comm,
348 state->task->pid, next_bp);
349 unwind_dump(state);
350 } else {
351 printk_deferred_once(KERN_WARNING
352 "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
353 state->bp, state->task->comm,
354 state->task->pid, next_bp);
355 unwind_dump(state);
356 }
357the_end:
358 state->stack_info.type = STACK_TYPE_UNKNOWN;
359 return false;
360}
361EXPORT_SYMBOL_GPL(unwind_next_frame);
362
363void __unwind_start(struct unwind_state *state, struct task_struct *task,
364 struct pt_regs *regs, unsigned long *first_frame)
365{
366 unsigned long *bp;
367
368 memset(state, 0, sizeof(*state));
369 state->task = task;
370 state->got_irq = (regs);
371
372
373 if (regs && user_mode(regs)) {
374 state->stack_info.type = STACK_TYPE_UNKNOWN;
375 return;
376 }
377
378 bp = get_frame_pointer(task, regs);
379
380
381
382
383
384
385
386
387
388
389 if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
390 state->next_bp = bp;
391 bp = ((unsigned long *)regs->sp) - 1;
392 }
393
394
395 get_stack_info(bp, state->task, &state->stack_info,
396 &state->stack_mask);
397 update_stack_state(state, bp);
398
399
400
401
402
403
404 while (!unwind_done(state) &&
405 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
406 (state->next_bp == NULL && state->bp < first_frame)))
407 unwind_next_frame(state);
408}
409EXPORT_SYMBOL_GPL(__unwind_start);
410