This source file includes following definitions.
- orc_ip
- __orc_find
- orc_module_find
- orc_module_find
- orc_ftrace_find
- orc_ftrace_find
- orc_find
- orc_sort_swap
- orc_sort_cmp
- unwind_module_init
- unwind_init
- unwind_get_return_address
- unwind_get_return_address_ptr
- stack_access_ok
- deref_stack_reg
- deref_stack_regs
- deref_stack_iret_regs
- get_reg
- unwind_next_frame
- __unwind_start
1
2 #include <linux/module.h>
3 #include <linux/sort.h>
4 #include <asm/ptrace.h>
5 #include <asm/stacktrace.h>
6 #include <asm/unwind.h>
7 #include <asm/orc_types.h>
8 #include <asm/orc_lookup.h>
9
10 #define orc_warn(fmt, ...) \
11 printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__)
12
13 extern int __start_orc_unwind_ip[];
14 extern int __stop_orc_unwind_ip[];
15 extern struct orc_entry __start_orc_unwind[];
16 extern struct orc_entry __stop_orc_unwind[];
17
18 static DEFINE_MUTEX(sort_mutex);
19 int *cur_orc_ip_table = __start_orc_unwind_ip;
20 struct orc_entry *cur_orc_table = __start_orc_unwind;
21
22 unsigned int lookup_num_blocks;
23 bool orc_init;
24
25 static inline unsigned long orc_ip(const int *ip)
26 {
27 return (unsigned long)ip + *ip;
28 }
29
30 static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
31 unsigned int num_entries, unsigned long ip)
32 {
33 int *first = ip_table;
34 int *last = ip_table + num_entries - 1;
35 int *mid = first, *found = first;
36
37 if (!num_entries)
38 return NULL;
39
40
41
42
43
44
45
46 while (first <= last) {
47 mid = first + ((last - first) / 2);
48
49 if (orc_ip(mid) <= ip) {
50 found = mid;
51 first = mid + 1;
52 } else
53 last = mid - 1;
54 }
55
56 return u_table + (found - ip_table);
57 }
58
59 #ifdef CONFIG_MODULES
60 static struct orc_entry *orc_module_find(unsigned long ip)
61 {
62 struct module *mod;
63
64 mod = __module_address(ip);
65 if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
66 return NULL;
67 return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
68 mod->arch.num_orcs, ip);
69 }
70 #else
71 static struct orc_entry *orc_module_find(unsigned long ip)
72 {
73 return NULL;
74 }
75 #endif
76
77 #ifdef CONFIG_DYNAMIC_FTRACE
78 static struct orc_entry *orc_find(unsigned long ip);
79
80
81
82
83
84
85
86
87
88
89
90 static struct orc_entry *orc_ftrace_find(unsigned long ip)
91 {
92 struct ftrace_ops *ops;
93 unsigned long caller;
94
95 ops = ftrace_ops_trampoline(ip);
96 if (!ops)
97 return NULL;
98
99 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
100 caller = (unsigned long)ftrace_regs_call;
101 else
102 caller = (unsigned long)ftrace_call;
103
104
105 if (ip == caller)
106 return NULL;
107
108 return orc_find(caller);
109 }
110 #else
111 static struct orc_entry *orc_ftrace_find(unsigned long ip)
112 {
113 return NULL;
114 }
115 #endif
116
117
118
119
120
121
122
123
124 static struct orc_entry null_orc_entry = {
125 .sp_offset = sizeof(long),
126 .sp_reg = ORC_REG_SP,
127 .bp_reg = ORC_REG_UNDEFINED,
128 .type = ORC_TYPE_CALL
129 };
130
131
132 static struct orc_entry orc_fp_entry = {
133 .type = ORC_TYPE_CALL,
134 .sp_reg = ORC_REG_BP,
135 .sp_offset = 16,
136 .bp_reg = ORC_REG_PREV_SP,
137 .bp_offset = -16,
138 .end = 0,
139 };
140
141 static struct orc_entry *orc_find(unsigned long ip)
142 {
143 static struct orc_entry *orc;
144
145 if (ip == 0)
146 return &null_orc_entry;
147
148
149 if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
150 unsigned int idx, start, stop;
151
152 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
153
154 if (unlikely((idx >= lookup_num_blocks-1))) {
155 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
156 idx, lookup_num_blocks, (void *)ip);
157 return NULL;
158 }
159
160 start = orc_lookup[idx];
161 stop = orc_lookup[idx + 1] + 1;
162
163 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
164 (__start_orc_unwind + stop > __stop_orc_unwind))) {
165 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
166 idx, lookup_num_blocks, start, stop, (void *)ip);
167 return NULL;
168 }
169
170 return __orc_find(__start_orc_unwind_ip + start,
171 __start_orc_unwind + start, stop - start, ip);
172 }
173
174
175 if (init_kernel_text(ip))
176 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
177 __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
178
179
180 orc = orc_module_find(ip);
181 if (orc)
182 return orc;
183
184 return orc_ftrace_find(ip);
185 }
186
187 static void orc_sort_swap(void *_a, void *_b, int size)
188 {
189 struct orc_entry *orc_a, *orc_b;
190 struct orc_entry orc_tmp;
191 int *a = _a, *b = _b, tmp;
192 int delta = _b - _a;
193
194
195 tmp = *a;
196 *a = *b + delta;
197 *b = tmp - delta;
198
199
200 orc_a = cur_orc_table + (a - cur_orc_ip_table);
201 orc_b = cur_orc_table + (b - cur_orc_ip_table);
202 orc_tmp = *orc_a;
203 *orc_a = *orc_b;
204 *orc_b = orc_tmp;
205 }
206
207 static int orc_sort_cmp(const void *_a, const void *_b)
208 {
209 struct orc_entry *orc_a;
210 const int *a = _a, *b = _b;
211 unsigned long a_val = orc_ip(a);
212 unsigned long b_val = orc_ip(b);
213
214 if (a_val > b_val)
215 return 1;
216 if (a_val < b_val)
217 return -1;
218
219
220
221
222
223
224
225 orc_a = cur_orc_table + (a - cur_orc_ip_table);
226 return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
227 }
228
229 #ifdef CONFIG_MODULES
230 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
231 void *_orc, size_t orc_size)
232 {
233 int *orc_ip = _orc_ip;
234 struct orc_entry *orc = _orc;
235 unsigned int num_entries = orc_ip_size / sizeof(int);
236
237 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
238 orc_size % sizeof(*orc) != 0 ||
239 num_entries != orc_size / sizeof(*orc));
240
241
242
243
244
245
246 mutex_lock(&sort_mutex);
247 cur_orc_ip_table = orc_ip;
248 cur_orc_table = orc;
249 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
250 mutex_unlock(&sort_mutex);
251
252 mod->arch.orc_unwind_ip = orc_ip;
253 mod->arch.orc_unwind = orc;
254 mod->arch.num_orcs = num_entries;
255 }
256 #endif
257
258 void __init unwind_init(void)
259 {
260 size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
261 size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
262 size_t num_entries = orc_ip_size / sizeof(int);
263 struct orc_entry *orc;
264 int i;
265
266 if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
267 orc_size % sizeof(struct orc_entry) != 0 ||
268 num_entries != orc_size / sizeof(struct orc_entry)) {
269 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
270 return;
271 }
272
273
274 sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
275 orc_sort_swap);
276
277
278 lookup_num_blocks = orc_lookup_end - orc_lookup;
279 for (i = 0; i < lookup_num_blocks-1; i++) {
280 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
281 num_entries,
282 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
283 if (!orc) {
284 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
285 return;
286 }
287
288 orc_lookup[i] = orc - __start_orc_unwind;
289 }
290
291
292 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
293 LOOKUP_STOP_IP);
294 if (!orc) {
295 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
296 return;
297 }
298 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
299
300 orc_init = true;
301 }
302
303 unsigned long unwind_get_return_address(struct unwind_state *state)
304 {
305 if (unwind_done(state))
306 return 0;
307
308 return __kernel_text_address(state->ip) ? state->ip : 0;
309 }
310 EXPORT_SYMBOL_GPL(unwind_get_return_address);
311
312 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
313 {
314 struct task_struct *task = state->task;
315
316 if (unwind_done(state))
317 return NULL;
318
319 if (state->regs)
320 return &state->regs->ip;
321
322 if (task != current && state->sp == task->thread.sp) {
323 struct inactive_task_frame *frame = (void *)task->thread.sp;
324 return &frame->ret_addr;
325 }
326
327 if (state->sp)
328 return (unsigned long *)state->sp - 1;
329
330 return NULL;
331 }
332
333 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
334 size_t len)
335 {
336 struct stack_info *info = &state->stack_info;
337 void *addr = (void *)_addr;
338
339 if (!on_stack(info, addr, len) &&
340 (get_stack_info(addr, state->task, info, &state->stack_mask)))
341 return false;
342
343 return true;
344 }
345
346 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
347 unsigned long *val)
348 {
349 if (!stack_access_ok(state, addr, sizeof(long)))
350 return false;
351
352 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
353 return true;
354 }
355
356 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
357 unsigned long *ip, unsigned long *sp)
358 {
359 struct pt_regs *regs = (struct pt_regs *)addr;
360
361
362 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
363
364 if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
365 return false;
366
367 *ip = regs->ip;
368 *sp = regs->sp;
369 return true;
370 }
371
372 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
373 unsigned long *ip, unsigned long *sp)
374 {
375 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
376
377 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
378 return false;
379
380 *ip = regs->ip;
381 *sp = regs->sp;
382 return true;
383 }
384
385
386
387
388
389
390
391
392
393 static bool get_reg(struct unwind_state *state, unsigned int reg_off,
394 unsigned long *val)
395 {
396 unsigned int reg = reg_off/8;
397
398 if (!state->regs)
399 return false;
400
401 if (state->full_regs) {
402 *val = ((unsigned long *)state->regs)[reg];
403 return true;
404 }
405
406 if (state->prev_regs) {
407 *val = ((unsigned long *)state->prev_regs)[reg];
408 return true;
409 }
410
411 return false;
412 }
413
414 bool unwind_next_frame(struct unwind_state *state)
415 {
416 unsigned long ip_p, sp, tmp, orig_ip = state->ip, prev_sp = state->sp;
417 enum stack_type prev_type = state->stack_info.type;
418 struct orc_entry *orc;
419 bool indirect = false;
420
421 if (unwind_done(state))
422 return false;
423
424
425 preempt_disable();
426
427
428 if (state->regs && user_mode(state->regs))
429 goto the_end;
430
431
432
433
434
435
436
437 orc = orc_find(state->signal ? state->ip : state->ip - 1);
438 if (!orc) {
439
440
441
442
443
444
445 orc = &orc_fp_entry;
446 state->error = true;
447 }
448
449
450 if (orc->sp_reg == ORC_REG_UNDEFINED) {
451 if (!orc->end)
452 goto err;
453
454 goto the_end;
455 }
456
457
458 switch (orc->sp_reg) {
459 case ORC_REG_SP:
460 sp = state->sp + orc->sp_offset;
461 break;
462
463 case ORC_REG_BP:
464 sp = state->bp + orc->sp_offset;
465 break;
466
467 case ORC_REG_SP_INDIRECT:
468 sp = state->sp + orc->sp_offset;
469 indirect = true;
470 break;
471
472 case ORC_REG_BP_INDIRECT:
473 sp = state->bp + orc->sp_offset;
474 indirect = true;
475 break;
476
477 case ORC_REG_R10:
478 if (!get_reg(state, offsetof(struct pt_regs, r10), &sp)) {
479 orc_warn("missing regs for base reg R10 at ip %pB\n",
480 (void *)state->ip);
481 goto err;
482 }
483 break;
484
485 case ORC_REG_R13:
486 if (!get_reg(state, offsetof(struct pt_regs, r13), &sp)) {
487 orc_warn("missing regs for base reg R13 at ip %pB\n",
488 (void *)state->ip);
489 goto err;
490 }
491 break;
492
493 case ORC_REG_DI:
494 if (!get_reg(state, offsetof(struct pt_regs, di), &sp)) {
495 orc_warn("missing regs for base reg DI at ip %pB\n",
496 (void *)state->ip);
497 goto err;
498 }
499 break;
500
501 case ORC_REG_DX:
502 if (!get_reg(state, offsetof(struct pt_regs, dx), &sp)) {
503 orc_warn("missing regs for base reg DX at ip %pB\n",
504 (void *)state->ip);
505 goto err;
506 }
507 break;
508
509 default:
510 orc_warn("unknown SP base reg %d for ip %pB\n",
511 orc->sp_reg, (void *)state->ip);
512 goto err;
513 }
514
515 if (indirect) {
516 if (!deref_stack_reg(state, sp, &sp))
517 goto err;
518 }
519
520
521 switch (orc->type) {
522 case ORC_TYPE_CALL:
523 ip_p = sp - sizeof(long);
524
525 if (!deref_stack_reg(state, ip_p, &state->ip))
526 goto err;
527
528 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
529 state->ip, (void *)ip_p);
530
531 state->sp = sp;
532 state->regs = NULL;
533 state->prev_regs = NULL;
534 state->signal = false;
535 break;
536
537 case ORC_TYPE_REGS:
538 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
539 orc_warn("can't dereference registers at %p for ip %pB\n",
540 (void *)sp, (void *)orig_ip);
541 goto err;
542 }
543
544 state->regs = (struct pt_regs *)sp;
545 state->prev_regs = NULL;
546 state->full_regs = true;
547 state->signal = true;
548 break;
549
550 case ORC_TYPE_REGS_IRET:
551 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
552 orc_warn("can't dereference iret registers at %p for ip %pB\n",
553 (void *)sp, (void *)orig_ip);
554 goto err;
555 }
556
557 if (state->full_regs)
558 state->prev_regs = state->regs;
559 state->regs = (void *)sp - IRET_FRAME_OFFSET;
560 state->full_regs = false;
561 state->signal = true;
562 break;
563
564 default:
565 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
566 orc->type, (void *)orig_ip);
567 goto err;
568 }
569
570
571 switch (orc->bp_reg) {
572 case ORC_REG_UNDEFINED:
573 if (get_reg(state, offsetof(struct pt_regs, bp), &tmp))
574 state->bp = tmp;
575 break;
576
577 case ORC_REG_PREV_SP:
578 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
579 goto err;
580 break;
581
582 case ORC_REG_BP:
583 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
584 goto err;
585 break;
586
587 default:
588 orc_warn("unknown BP base reg %d for ip %pB\n",
589 orc->bp_reg, (void *)orig_ip);
590 goto err;
591 }
592
593
594 if (state->stack_info.type == prev_type &&
595 on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
596 state->sp <= prev_sp) {
597 orc_warn("stack going in the wrong direction? ip=%pB\n",
598 (void *)orig_ip);
599 goto err;
600 }
601
602 preempt_enable();
603 return true;
604
605 err:
606 state->error = true;
607
608 the_end:
609 preempt_enable();
610 state->stack_info.type = STACK_TYPE_UNKNOWN;
611 return false;
612 }
613 EXPORT_SYMBOL_GPL(unwind_next_frame);
614
615 void __unwind_start(struct unwind_state *state, struct task_struct *task,
616 struct pt_regs *regs, unsigned long *first_frame)
617 {
618 memset(state, 0, sizeof(*state));
619 state->task = task;
620
621 if (!orc_init)
622 goto err;
623
624
625
626
627
628
629 if (task_on_another_cpu(task))
630 goto err;
631
632 if (regs) {
633 if (user_mode(regs))
634 goto the_end;
635
636 state->ip = regs->ip;
637 state->sp = regs->sp;
638 state->bp = regs->bp;
639 state->regs = regs;
640 state->full_regs = true;
641 state->signal = true;
642
643 } else if (task == current) {
644 asm volatile("lea (%%rip), %0\n\t"
645 "mov %%rsp, %1\n\t"
646 "mov %%rbp, %2\n\t"
647 : "=r" (state->ip), "=r" (state->sp),
648 "=r" (state->bp));
649
650 } else {
651 struct inactive_task_frame *frame = (void *)task->thread.sp;
652
653 state->sp = task->thread.sp;
654 state->bp = READ_ONCE_NOCHECK(frame->bp);
655 state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
656 }
657
658 if (get_stack_info((unsigned long *)state->sp, state->task,
659 &state->stack_info, &state->stack_mask)) {
660
661
662
663
664
665
666 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
667 state->error = true;
668 if (get_stack_info(next_page, state->task, &state->stack_info,
669 &state->stack_mask))
670 return;
671 }
672
673
674
675
676
677
678
679
680 if (regs) {
681 unwind_next_frame(state);
682 return;
683 }
684
685
686 while (!unwind_done(state) &&
687 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
688 state->sp < (unsigned long)first_frame))
689 unwind_next_frame(state);
690
691 return;
692
693 err:
694 state->error = true;
695 the_end:
696 state->stack_info.type = STACK_TYPE_UNKNOWN;
697 }
698 EXPORT_SYMBOL_GPL(__unwind_start);