GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / x86 / kernel / unwind_orc.c
1 #include <linux/module.h>
2 #include <linux/sort.h>
3 #include <asm/ptrace.h>
4 #include <asm/stacktrace.h>
5 #include <asm/unwind.h>
6 #include <asm/orc_types.h>
7 #include <asm/orc_lookup.h>
8 #include <asm/sections.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          * Do a binary range search to find the rightmost duplicate of a given
42          * starting address.  Some entries are section terminators which are
43          * "weak" entries for ensuring there are no gaps.  They should be
44          * ignored when they conflict with a real entry.
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 /*
78  * If we crash with IP==0, the last successfully executed instruction
79  * was probably an indirect function call with a NULL function pointer,
80  * and we don't have unwind information for NULL.
81  * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
82  * pointer into its parent and then continue normally from there.
83  */
84 static struct orc_entry null_orc_entry = {
85         .sp_offset = sizeof(long),
86         .sp_reg = ORC_REG_SP,
87         .bp_reg = ORC_REG_UNDEFINED,
88         .type = ORC_TYPE_CALL
89 };
90
91 static struct orc_entry *orc_find(unsigned long ip)
92 {
93         if (ip == 0)
94                 return &null_orc_entry;
95
96         /* For non-init vmlinux addresses, use the fast lookup table: */
97         if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
98                 unsigned int idx, start, stop;
99
100                 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
101
102                 if (unlikely((idx >= lookup_num_blocks-1))) {
103                         orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
104                                  idx, lookup_num_blocks, (void *)ip);
105                         return NULL;
106                 }
107
108                 start = orc_lookup[idx];
109                 stop = orc_lookup[idx + 1] + 1;
110
111                 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
112                              (__start_orc_unwind + stop > __stop_orc_unwind))) {
113                         orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
114                                  idx, lookup_num_blocks, start, stop, (void *)ip);
115                         return NULL;
116                 }
117
118                 return __orc_find(__start_orc_unwind_ip + start,
119                                   __start_orc_unwind + start, stop - start, ip);
120         }
121
122         /* vmlinux .init slow lookup: */
123         if (ip >= (unsigned long)_sinittext && ip < (unsigned long)_einittext)
124                 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
125                                   __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
126
127         /* Module lookup: */
128         return orc_module_find(ip);
129 }
130
131 static void orc_sort_swap(void *_a, void *_b, int size)
132 {
133         struct orc_entry *orc_a, *orc_b;
134         struct orc_entry orc_tmp;
135         int *a = _a, *b = _b, tmp;
136         int delta = _b - _a;
137
138         /* Swap the .orc_unwind_ip entries: */
139         tmp = *a;
140         *a = *b + delta;
141         *b = tmp - delta;
142
143         /* Swap the corresponding .orc_unwind entries: */
144         orc_a = cur_orc_table + (a - cur_orc_ip_table);
145         orc_b = cur_orc_table + (b - cur_orc_ip_table);
146         orc_tmp = *orc_a;
147         *orc_a = *orc_b;
148         *orc_b = orc_tmp;
149 }
150
151 static int orc_sort_cmp(const void *_a, const void *_b)
152 {
153         struct orc_entry *orc_a;
154         const int *a = _a, *b = _b;
155         unsigned long a_val = orc_ip(a);
156         unsigned long b_val = orc_ip(b);
157
158         if (a_val > b_val)
159                 return 1;
160         if (a_val < b_val)
161                 return -1;
162
163         /*
164          * The "weak" section terminator entries need to always be on the left
165          * to ensure the lookup code skips them in favor of real entries.
166          * These terminator entries exist to handle any gaps created by
167          * whitelisted .o files which didn't get objtool generation.
168          */
169         orc_a = cur_orc_table + (a - cur_orc_ip_table);
170         return orc_a->sp_reg == ORC_REG_UNDEFINED ? -1 : 1;
171 }
172
173 #ifdef CONFIG_MODULES
174 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
175                         void *_orc, size_t orc_size)
176 {
177         int *orc_ip = _orc_ip;
178         struct orc_entry *orc = _orc;
179         unsigned int num_entries = orc_ip_size / sizeof(int);
180
181         WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
182                      orc_size % sizeof(*orc) != 0 ||
183                      num_entries != orc_size / sizeof(*orc));
184
185         /*
186          * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
187          * associate an .orc_unwind_ip table entry with its corresponding
188          * .orc_unwind entry so they can both be swapped.
189          */
190         mutex_lock(&sort_mutex);
191         cur_orc_ip_table = orc_ip;
192         cur_orc_table = orc;
193         sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
194         mutex_unlock(&sort_mutex);
195
196         mod->arch.orc_unwind_ip = orc_ip;
197         mod->arch.orc_unwind = orc;
198         mod->arch.num_orcs = num_entries;
199 }
200 #endif
201
202 void __init unwind_init(void)
203 {
204         size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
205         size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
206         size_t num_entries = orc_ip_size / sizeof(int);
207         struct orc_entry *orc;
208         int i;
209
210         if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
211             orc_size % sizeof(struct orc_entry) != 0 ||
212             num_entries != orc_size / sizeof(struct orc_entry)) {
213                 orc_warn("WARNING: Bad or missing .orc_unwind table.  Disabling unwinder.\n");
214                 return;
215         }
216
217         /* Sort the .orc_unwind and .orc_unwind_ip tables: */
218         sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
219              orc_sort_swap);
220
221         /* Initialize the fast lookup table: */
222         lookup_num_blocks = orc_lookup_end - orc_lookup;
223         for (i = 0; i < lookup_num_blocks-1; i++) {
224                 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
225                                  num_entries,
226                                  LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
227                 if (!orc) {
228                         orc_warn("WARNING: Corrupt .orc_unwind table.  Disabling unwinder.\n");
229                         return;
230                 }
231
232                 orc_lookup[i] = orc - __start_orc_unwind;
233         }
234
235         /* Initialize the ending block: */
236         orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
237                          LOOKUP_STOP_IP);
238         if (!orc) {
239                 orc_warn("WARNING: Corrupt .orc_unwind table.  Disabling unwinder.\n");
240                 return;
241         }
242         orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
243
244         orc_init = true;
245 }
246
247 unsigned long unwind_get_return_address(struct unwind_state *state)
248 {
249         if (unwind_done(state))
250                 return 0;
251
252         return __kernel_text_address(state->ip) ? state->ip : 0;
253 }
254 EXPORT_SYMBOL_GPL(unwind_get_return_address);
255
256 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
257 {
258         if (unwind_done(state))
259                 return NULL;
260
261         if (state->regs)
262                 return &state->regs->ip;
263
264         if (state->sp)
265                 return (unsigned long *)state->sp - 1;
266
267         return NULL;
268 }
269
270 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
271                             size_t len)
272 {
273         struct stack_info *info = &state->stack_info;
274         void *addr = (void *)_addr;
275
276         if (!on_stack(info, addr, len) &&
277             (get_stack_info(addr, state->task, info, &state->stack_mask)))
278                 return false;
279
280         return true;
281 }
282
283 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
284                             unsigned long *val)
285 {
286         if (!stack_access_ok(state, addr, sizeof(long)))
287                 return false;
288
289         *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
290         return true;
291 }
292
293 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
294                              unsigned long *ip, unsigned long *sp)
295 {
296         struct pt_regs *regs = (struct pt_regs *)addr;
297
298         /* x86-32 support will be more complicated due to the &regs->sp hack */
299         BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
300
301         if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
302                 return false;
303
304         *ip = regs->ip;
305         *sp = regs->sp;
306         return true;
307 }
308
309 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
310                                   unsigned long *ip, unsigned long *sp)
311 {
312         struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
313
314         if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
315                 return false;
316
317         *ip = regs->ip;
318         *sp = regs->sp;
319         return true;
320 }
321
322 bool unwind_next_frame(struct unwind_state *state)
323 {
324         unsigned long ip_p, sp, orig_ip, prev_sp = state->sp;
325         enum stack_type prev_type = state->stack_info.type;
326         struct orc_entry *orc;
327         bool indirect = false;
328
329         if (unwind_done(state))
330                 return false;
331
332         /* Don't let modules unload while we're reading their ORC data. */
333         preempt_disable();
334
335         /* Have we reached the end? */
336         if (state->regs && user_mode(state->regs))
337                 goto done;
338
339         /*
340          * Find the orc_entry associated with the text address.
341          *
342          * For a call frame (as opposed to a signal frame), state->ip points to
343          * the instruction after the call.  That instruction's stack layout
344          * could be different from the call instruction's layout, for example
345          * if the call was to a noreturn function.  So get the ORC data for the
346          * call instruction itself.
347          */
348         orc = orc_find(state->signal ? state->ip : state->ip - 1);
349         if (!orc || orc->sp_reg == ORC_REG_UNDEFINED)
350                 goto done;
351         orig_ip = state->ip;
352
353         /* Find the previous frame's stack: */
354         switch (orc->sp_reg) {
355         case ORC_REG_SP:
356                 sp = state->sp + orc->sp_offset;
357                 break;
358
359         case ORC_REG_BP:
360                 sp = state->bp + orc->sp_offset;
361                 break;
362
363         case ORC_REG_SP_INDIRECT:
364                 sp = state->sp + orc->sp_offset;
365                 indirect = true;
366                 break;
367
368         case ORC_REG_BP_INDIRECT:
369                 sp = state->bp + orc->sp_offset;
370                 indirect = true;
371                 break;
372
373         case ORC_REG_R10:
374                 if (!state->regs || !state->full_regs) {
375                         orc_warn("missing regs for base reg R10 at ip %pB\n",
376                                  (void *)state->ip);
377                         goto done;
378                 }
379                 sp = state->regs->r10;
380                 break;
381
382         case ORC_REG_R13:
383                 if (!state->regs || !state->full_regs) {
384                         orc_warn("missing regs for base reg R13 at ip %pB\n",
385                                  (void *)state->ip);
386                         goto done;
387                 }
388                 sp = state->regs->r13;
389                 break;
390
391         case ORC_REG_DI:
392                 if (!state->regs || !state->full_regs) {
393                         orc_warn("missing regs for base reg DI at ip %pB\n",
394                                  (void *)state->ip);
395                         goto done;
396                 }
397                 sp = state->regs->di;
398                 break;
399
400         case ORC_REG_DX:
401                 if (!state->regs || !state->full_regs) {
402                         orc_warn("missing regs for base reg DX at ip %pB\n",
403                                  (void *)state->ip);
404                         goto done;
405                 }
406                 sp = state->regs->dx;
407                 break;
408
409         default:
410                 orc_warn("unknown SP base reg %d for ip %pB\n",
411                          orc->sp_reg, (void *)state->ip);
412                 goto done;
413         }
414
415         if (indirect) {
416                 if (!deref_stack_reg(state, sp, &sp))
417                         goto done;
418         }
419
420         /* Find IP, SP and possibly regs: */
421         switch (orc->type) {
422         case ORC_TYPE_CALL:
423                 ip_p = sp - sizeof(long);
424
425                 if (!deref_stack_reg(state, ip_p, &state->ip))
426                         goto done;
427
428                 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
429                                                   state->ip, (void *)ip_p);
430
431                 state->sp = sp;
432                 state->regs = NULL;
433                 state->signal = false;
434                 break;
435
436         case ORC_TYPE_REGS:
437                 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
438                         orc_warn("can't dereference registers at %p for ip %pB\n",
439                                  (void *)sp, (void *)orig_ip);
440                         goto done;
441                 }
442
443                 state->regs = (struct pt_regs *)sp;
444                 state->full_regs = true;
445                 state->signal = true;
446                 break;
447
448         case ORC_TYPE_REGS_IRET:
449                 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
450                         orc_warn("can't dereference iret registers at %p for ip %pB\n",
451                                  (void *)sp, (void *)orig_ip);
452                         goto done;
453                 }
454
455                 state->regs = (void *)sp - IRET_FRAME_OFFSET;
456                 state->full_regs = false;
457                 state->signal = true;
458                 break;
459
460         default:
461                 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
462                          orc->type, (void *)orig_ip);
463                 goto done;
464         }
465
466         /* Find BP: */
467         switch (orc->bp_reg) {
468         case ORC_REG_UNDEFINED:
469                 if (state->regs && state->full_regs)
470                         state->bp = state->regs->bp;
471                 break;
472
473         case ORC_REG_PREV_SP:
474                 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
475                         goto done;
476                 break;
477
478         case ORC_REG_BP:
479                 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
480                         goto done;
481                 break;
482
483         default:
484                 orc_warn("unknown BP base reg %d for ip %pB\n",
485                          orc->bp_reg, (void *)orig_ip);
486                 goto done;
487         }
488
489         /* Prevent a recursive loop due to bad ORC data: */
490         if (state->stack_info.type == prev_type &&
491             on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
492             state->sp <= prev_sp) {
493                 orc_warn("stack going in the wrong direction? ip=%pB\n",
494                          (void *)orig_ip);
495                 goto done;
496         }
497
498         preempt_enable();
499         return true;
500
501 done:
502         preempt_enable();
503         state->stack_info.type = STACK_TYPE_UNKNOWN;
504         return false;
505 }
506 EXPORT_SYMBOL_GPL(unwind_next_frame);
507
508 void __unwind_start(struct unwind_state *state, struct task_struct *task,
509                     struct pt_regs *regs, unsigned long *first_frame)
510 {
511         memset(state, 0, sizeof(*state));
512         state->task = task;
513
514         if (!orc_init)
515                 goto err;
516
517         /*
518          * Refuse to unwind the stack of a task while it's executing on another
519          * CPU.  This check is racy, but that's ok: the unwinder has other
520          * checks to prevent it from going off the rails.
521          */
522         if (task_on_another_cpu(task))
523                 goto err;
524
525         if (regs) {
526                 if (user_mode(regs))
527                         goto the_end;
528
529                 state->ip = regs->ip;
530                 state->sp = kernel_stack_pointer(regs);
531                 state->bp = regs->bp;
532                 state->regs = regs;
533                 state->full_regs = true;
534                 state->signal = true;
535
536         } else if (task == current) {
537                 asm volatile("lea (%%rip), %0\n\t"
538                              "mov %%rsp, %1\n\t"
539                              "mov %%rbp, %2\n\t"
540                              : "=r" (state->ip), "=r" (state->sp),
541                                "=r" (state->bp));
542
543         } else {
544                 struct inactive_task_frame *frame = (void *)task->thread.sp;
545
546                 state->sp = task->thread.sp + sizeof(*frame);
547                 state->bp = READ_ONCE_NOCHECK(frame->bp);
548                 state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
549                 state->signal = (void *)state->ip == ret_from_fork;
550         }
551
552         if (get_stack_info((unsigned long *)state->sp, state->task,
553                            &state->stack_info, &state->stack_mask)) {
554                 /*
555                  * We weren't on a valid stack.  It's possible that
556                  * we overflowed a valid stack into a guard page.
557                  * See if the next page up is valid so that we can
558                  * generate some kind of backtrace if this happens.
559                  */
560                 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
561                 state->error = true;
562                 if (get_stack_info(next_page, state->task, &state->stack_info,
563                                    &state->stack_mask))
564                         return;
565         }
566
567         /*
568          * The caller can provide the address of the first frame directly
569          * (first_frame) or indirectly (regs->sp) to indicate which stack frame
570          * to start unwinding at.  Skip ahead until we reach it.
571          */
572
573         /* When starting from regs, skip the regs frame: */
574         if (regs) {
575                 unwind_next_frame(state);
576                 return;
577         }
578
579         /* Otherwise, skip ahead to the user-specified starting frame: */
580         while (!unwind_done(state) &&
581                (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
582                         state->sp < (unsigned long)first_frame))
583                 unwind_next_frame(state);
584
585         return;
586
587 err:
588         state->error = true;
589 the_end:
590         state->stack_info.type = STACK_TYPE_UNKNOWN;
591 }
592 EXPORT_SYMBOL_GPL(__unwind_start);