x86/oprofile: Fix bogus GCC-8 warning in nmi_setup()
[cris-mirror.git] / arch / x86 / kernel / unwind_orc.c
blob1f9188f5357cb38e45295c07ba5f2e902563257e
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>
10 #define orc_warn(fmt, ...) \
11 printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__)
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[];
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;
22 unsigned int lookup_num_blocks;
23 bool orc_init;
25 static inline unsigned long orc_ip(const int *ip)
27 return (unsigned long)ip + *ip;
30 static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
31 unsigned int num_entries, unsigned long ip)
33 int *first = ip_table;
34 int *last = ip_table + num_entries - 1;
35 int *mid = first, *found = first;
37 if (!num_entries)
38 return NULL;
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.
46 while (first <= last) {
47 mid = first + ((last - first) / 2);
49 if (orc_ip(mid) <= ip) {
50 found = mid;
51 first = mid + 1;
52 } else
53 last = mid - 1;
56 return u_table + (found - ip_table);
59 #ifdef CONFIG_MODULES
60 static struct orc_entry *orc_module_find(unsigned long ip)
62 struct module *mod;
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);
70 #else
71 static struct orc_entry *orc_module_find(unsigned long ip)
73 return NULL;
75 #endif
77 #ifdef CONFIG_DYNAMIC_FTRACE
78 static struct orc_entry *orc_find(unsigned long ip);
81 * Ftrace dynamic trampolines do not have orc entries of their own.
82 * But they are copies of the ftrace entries that are static and
83 * defined in ftrace_*.S, which do have orc entries.
85 * If the undwinder comes across a ftrace trampoline, then find the
86 * ftrace function that was used to create it, and use that ftrace
87 * function's orc entrie, as the placement of the return code in
88 * the stack will be identical.
90 static struct orc_entry *orc_ftrace_find(unsigned long ip)
92 struct ftrace_ops *ops;
93 unsigned long caller;
95 ops = ftrace_ops_trampoline(ip);
96 if (!ops)
97 return NULL;
99 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
100 caller = (unsigned long)ftrace_regs_call;
101 else
102 caller = (unsigned long)ftrace_call;
104 /* Prevent unlikely recursion */
105 if (ip == caller)
106 return NULL;
108 return orc_find(caller);
110 #else
111 static struct orc_entry *orc_ftrace_find(unsigned long ip)
113 return NULL;
115 #endif
117 static struct orc_entry *orc_find(unsigned long ip)
119 static struct orc_entry *orc;
121 if (!orc_init)
122 return NULL;
124 /* For non-init vmlinux addresses, use the fast lookup table: */
125 if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
126 unsigned int idx, start, stop;
128 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
130 if (unlikely((idx >= lookup_num_blocks-1))) {
131 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
132 idx, lookup_num_blocks, (void *)ip);
133 return NULL;
136 start = orc_lookup[idx];
137 stop = orc_lookup[idx + 1] + 1;
139 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
140 (__start_orc_unwind + stop > __stop_orc_unwind))) {
141 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
142 idx, lookup_num_blocks, start, stop, (void *)ip);
143 return NULL;
146 return __orc_find(__start_orc_unwind_ip + start,
147 __start_orc_unwind + start, stop - start, ip);
150 /* vmlinux .init slow lookup: */
151 if (ip >= (unsigned long)_sinittext && ip < (unsigned long)_einittext)
152 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
153 __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
155 /* Module lookup: */
156 orc = orc_module_find(ip);
157 if (orc)
158 return orc;
160 return orc_ftrace_find(ip);
163 static void orc_sort_swap(void *_a, void *_b, int size)
165 struct orc_entry *orc_a, *orc_b;
166 struct orc_entry orc_tmp;
167 int *a = _a, *b = _b, tmp;
168 int delta = _b - _a;
170 /* Swap the .orc_unwind_ip entries: */
171 tmp = *a;
172 *a = *b + delta;
173 *b = tmp - delta;
175 /* Swap the corresponding .orc_unwind entries: */
176 orc_a = cur_orc_table + (a - cur_orc_ip_table);
177 orc_b = cur_orc_table + (b - cur_orc_ip_table);
178 orc_tmp = *orc_a;
179 *orc_a = *orc_b;
180 *orc_b = orc_tmp;
183 static int orc_sort_cmp(const void *_a, const void *_b)
185 struct orc_entry *orc_a;
186 const int *a = _a, *b = _b;
187 unsigned long a_val = orc_ip(a);
188 unsigned long b_val = orc_ip(b);
190 if (a_val > b_val)
191 return 1;
192 if (a_val < b_val)
193 return -1;
196 * The "weak" section terminator entries need to always be on the left
197 * to ensure the lookup code skips them in favor of real entries.
198 * These terminator entries exist to handle any gaps created by
199 * whitelisted .o files which didn't get objtool generation.
201 orc_a = cur_orc_table + (a - cur_orc_ip_table);
202 return orc_a->sp_reg == ORC_REG_UNDEFINED ? -1 : 1;
205 #ifdef CONFIG_MODULES
206 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
207 void *_orc, size_t orc_size)
209 int *orc_ip = _orc_ip;
210 struct orc_entry *orc = _orc;
211 unsigned int num_entries = orc_ip_size / sizeof(int);
213 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
214 orc_size % sizeof(*orc) != 0 ||
215 num_entries != orc_size / sizeof(*orc));
218 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
219 * associate an .orc_unwind_ip table entry with its corresponding
220 * .orc_unwind entry so they can both be swapped.
222 mutex_lock(&sort_mutex);
223 cur_orc_ip_table = orc_ip;
224 cur_orc_table = orc;
225 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
226 mutex_unlock(&sort_mutex);
228 mod->arch.orc_unwind_ip = orc_ip;
229 mod->arch.orc_unwind = orc;
230 mod->arch.num_orcs = num_entries;
232 #endif
234 void __init unwind_init(void)
236 size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
237 size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
238 size_t num_entries = orc_ip_size / sizeof(int);
239 struct orc_entry *orc;
240 int i;
242 if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
243 orc_size % sizeof(struct orc_entry) != 0 ||
244 num_entries != orc_size / sizeof(struct orc_entry)) {
245 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
246 return;
249 /* Sort the .orc_unwind and .orc_unwind_ip tables: */
250 sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
251 orc_sort_swap);
253 /* Initialize the fast lookup table: */
254 lookup_num_blocks = orc_lookup_end - orc_lookup;
255 for (i = 0; i < lookup_num_blocks-1; i++) {
256 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
257 num_entries,
258 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
259 if (!orc) {
260 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
261 return;
264 orc_lookup[i] = orc - __start_orc_unwind;
267 /* Initialize the ending block: */
268 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
269 LOOKUP_STOP_IP);
270 if (!orc) {
271 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
272 return;
274 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
276 orc_init = true;
279 unsigned long unwind_get_return_address(struct unwind_state *state)
281 if (unwind_done(state))
282 return 0;
284 return __kernel_text_address(state->ip) ? state->ip : 0;
286 EXPORT_SYMBOL_GPL(unwind_get_return_address);
288 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
290 if (unwind_done(state))
291 return NULL;
293 if (state->regs)
294 return &state->regs->ip;
296 if (state->sp)
297 return (unsigned long *)state->sp - 1;
299 return NULL;
302 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
303 size_t len)
305 struct stack_info *info = &state->stack_info;
306 void *addr = (void *)_addr;
308 if (!on_stack(info, addr, len) &&
309 (get_stack_info(addr, state->task, info, &state->stack_mask)))
310 return false;
312 return true;
315 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
316 unsigned long *val)
318 if (!stack_access_ok(state, addr, sizeof(long)))
319 return false;
321 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
322 return true;
325 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
326 unsigned long *ip, unsigned long *sp)
328 struct pt_regs *regs = (struct pt_regs *)addr;
330 /* x86-32 support will be more complicated due to the &regs->sp hack */
331 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
333 if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
334 return false;
336 *ip = regs->ip;
337 *sp = regs->sp;
338 return true;
341 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
342 unsigned long *ip, unsigned long *sp)
344 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
346 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
347 return false;
349 *ip = regs->ip;
350 *sp = regs->sp;
351 return true;
354 bool unwind_next_frame(struct unwind_state *state)
356 unsigned long ip_p, sp, orig_ip, prev_sp = state->sp;
357 enum stack_type prev_type = state->stack_info.type;
358 struct orc_entry *orc;
359 bool indirect = false;
361 if (unwind_done(state))
362 return false;
364 /* Don't let modules unload while we're reading their ORC data. */
365 preempt_disable();
367 /* Have we reached the end? */
368 if (state->regs && user_mode(state->regs))
369 goto done;
372 * Find the orc_entry associated with the text address.
374 * Decrement call return addresses by one so they work for sibling
375 * calls and calls to noreturn functions.
377 orc = orc_find(state->signal ? state->ip : state->ip - 1);
378 if (!orc || orc->sp_reg == ORC_REG_UNDEFINED)
379 goto done;
380 orig_ip = state->ip;
382 /* Find the previous frame's stack: */
383 switch (orc->sp_reg) {
384 case ORC_REG_SP:
385 sp = state->sp + orc->sp_offset;
386 break;
388 case ORC_REG_BP:
389 sp = state->bp + orc->sp_offset;
390 break;
392 case ORC_REG_SP_INDIRECT:
393 sp = state->sp + orc->sp_offset;
394 indirect = true;
395 break;
397 case ORC_REG_BP_INDIRECT:
398 sp = state->bp + orc->sp_offset;
399 indirect = true;
400 break;
402 case ORC_REG_R10:
403 if (!state->regs || !state->full_regs) {
404 orc_warn("missing regs for base reg R10 at ip %pB\n",
405 (void *)state->ip);
406 goto done;
408 sp = state->regs->r10;
409 break;
411 case ORC_REG_R13:
412 if (!state->regs || !state->full_regs) {
413 orc_warn("missing regs for base reg R13 at ip %pB\n",
414 (void *)state->ip);
415 goto done;
417 sp = state->regs->r13;
418 break;
420 case ORC_REG_DI:
421 if (!state->regs || !state->full_regs) {
422 orc_warn("missing regs for base reg DI at ip %pB\n",
423 (void *)state->ip);
424 goto done;
426 sp = state->regs->di;
427 break;
429 case ORC_REG_DX:
430 if (!state->regs || !state->full_regs) {
431 orc_warn("missing regs for base reg DX at ip %pB\n",
432 (void *)state->ip);
433 goto done;
435 sp = state->regs->dx;
436 break;
438 default:
439 orc_warn("unknown SP base reg %d for ip %pB\n",
440 orc->sp_reg, (void *)state->ip);
441 goto done;
444 if (indirect) {
445 if (!deref_stack_reg(state, sp, &sp))
446 goto done;
449 /* Find IP, SP and possibly regs: */
450 switch (orc->type) {
451 case ORC_TYPE_CALL:
452 ip_p = sp - sizeof(long);
454 if (!deref_stack_reg(state, ip_p, &state->ip))
455 goto done;
457 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
458 state->ip, (void *)ip_p);
460 state->sp = sp;
461 state->regs = NULL;
462 state->signal = false;
463 break;
465 case ORC_TYPE_REGS:
466 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
467 orc_warn("can't dereference registers at %p for ip %pB\n",
468 (void *)sp, (void *)orig_ip);
469 goto done;
472 state->regs = (struct pt_regs *)sp;
473 state->full_regs = true;
474 state->signal = true;
475 break;
477 case ORC_TYPE_REGS_IRET:
478 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
479 orc_warn("can't dereference iret registers at %p for ip %pB\n",
480 (void *)sp, (void *)orig_ip);
481 goto done;
484 state->regs = (void *)sp - IRET_FRAME_OFFSET;
485 state->full_regs = false;
486 state->signal = true;
487 break;
489 default:
490 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
491 orc->type, (void *)orig_ip);
492 break;
495 /* Find BP: */
496 switch (orc->bp_reg) {
497 case ORC_REG_UNDEFINED:
498 if (state->regs && state->full_regs)
499 state->bp = state->regs->bp;
500 break;
502 case ORC_REG_PREV_SP:
503 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
504 goto done;
505 break;
507 case ORC_REG_BP:
508 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
509 goto done;
510 break;
512 default:
513 orc_warn("unknown BP base reg %d for ip %pB\n",
514 orc->bp_reg, (void *)orig_ip);
515 goto done;
518 /* Prevent a recursive loop due to bad ORC data: */
519 if (state->stack_info.type == prev_type &&
520 on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
521 state->sp <= prev_sp) {
522 orc_warn("stack going in the wrong direction? ip=%pB\n",
523 (void *)orig_ip);
524 goto done;
527 preempt_enable();
528 return true;
530 done:
531 preempt_enable();
532 state->stack_info.type = STACK_TYPE_UNKNOWN;
533 return false;
535 EXPORT_SYMBOL_GPL(unwind_next_frame);
537 void __unwind_start(struct unwind_state *state, struct task_struct *task,
538 struct pt_regs *regs, unsigned long *first_frame)
540 memset(state, 0, sizeof(*state));
541 state->task = task;
544 * Refuse to unwind the stack of a task while it's executing on another
545 * CPU. This check is racy, but that's ok: the unwinder has other
546 * checks to prevent it from going off the rails.
548 if (task_on_another_cpu(task))
549 goto done;
551 if (regs) {
552 if (user_mode(regs))
553 goto done;
555 state->ip = regs->ip;
556 state->sp = kernel_stack_pointer(regs);
557 state->bp = regs->bp;
558 state->regs = regs;
559 state->full_regs = true;
560 state->signal = true;
562 } else if (task == current) {
563 asm volatile("lea (%%rip), %0\n\t"
564 "mov %%rsp, %1\n\t"
565 "mov %%rbp, %2\n\t"
566 : "=r" (state->ip), "=r" (state->sp),
567 "=r" (state->bp));
569 } else {
570 struct inactive_task_frame *frame = (void *)task->thread.sp;
572 state->sp = task->thread.sp;
573 state->bp = READ_ONCE_NOCHECK(frame->bp);
574 state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
577 if (get_stack_info((unsigned long *)state->sp, state->task,
578 &state->stack_info, &state->stack_mask)) {
580 * We weren't on a valid stack. It's possible that
581 * we overflowed a valid stack into a guard page.
582 * See if the next page up is valid so that we can
583 * generate some kind of backtrace if this happens.
585 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
586 if (get_stack_info(next_page, state->task, &state->stack_info,
587 &state->stack_mask))
588 return;
592 * The caller can provide the address of the first frame directly
593 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
594 * to start unwinding at. Skip ahead until we reach it.
597 /* When starting from regs, skip the regs frame: */
598 if (regs) {
599 unwind_next_frame(state);
600 return;
603 /* Otherwise, skip ahead to the user-specified starting frame: */
604 while (!unwind_done(state) &&
605 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
606 state->sp <= (unsigned long)first_frame))
607 unwind_next_frame(state);
609 return;
611 done:
612 state->stack_info.type = STACK_TYPE_UNKNOWN;
613 return;
615 EXPORT_SYMBOL_GPL(__unwind_start);