1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/objtool.h>
3 #include <linux/module.h>
4 #include <linux/sort.h>
5 #include <asm/ptrace.h>
6 #include <asm/stacktrace.h>
7 #include <asm/unwind.h>
8 #include <asm/orc_types.h>
9 #include <asm/orc_lookup.h>
11 #define orc_warn(fmt, ...) \
12 printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
14 #define orc_warn_current(args...) \
16 if (state->task == current) \
20 extern int __start_orc_unwind_ip[];
21 extern int __stop_orc_unwind_ip[];
22 extern struct orc_entry __start_orc_unwind[];
23 extern struct orc_entry __stop_orc_unwind[];
25 static bool orc_init __ro_after_init;
26 static unsigned int lookup_num_blocks __ro_after_init;
28 static inline unsigned long orc_ip(const int *ip)
30 return (unsigned long)ip + *ip;
33 static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
34 unsigned int num_entries, unsigned long ip)
36 int *first = ip_table;
37 int *last = ip_table + num_entries - 1;
38 int *mid = first, *found = first;
44 * Do a binary range search to find the rightmost duplicate of a given
45 * starting address. Some entries are section terminators which are
46 * "weak" entries for ensuring there are no gaps. They should be
47 * ignored when they conflict with a real entry.
49 while (first <= last) {
50 mid = first + ((last - first) / 2);
52 if (orc_ip(mid) <= ip) {
59 return u_table + (found - ip_table);
63 static struct orc_entry *orc_module_find(unsigned long ip)
67 mod = __module_address(ip);
68 if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
70 return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
71 mod->arch.num_orcs, ip);
74 static struct orc_entry *orc_module_find(unsigned long ip)
80 #ifdef CONFIG_DYNAMIC_FTRACE
81 static struct orc_entry *orc_find(unsigned long ip);
84 * Ftrace dynamic trampolines do not have orc entries of their own.
85 * But they are copies of the ftrace entries that are static and
86 * defined in ftrace_*.S, which do have orc entries.
88 * If the unwinder comes across a ftrace trampoline, then find the
89 * ftrace function that was used to create it, and use that ftrace
90 * function's orc entry, as the placement of the return code in
91 * the stack will be identical.
93 static struct orc_entry *orc_ftrace_find(unsigned long ip)
95 struct ftrace_ops *ops;
96 unsigned long tramp_addr, offset;
98 ops = ftrace_ops_trampoline(ip);
102 /* Set tramp_addr to the start of the code copied by the trampoline */
103 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
104 tramp_addr = (unsigned long)ftrace_regs_caller;
106 tramp_addr = (unsigned long)ftrace_caller;
108 /* Now place tramp_addr to the location within the trampoline ip is at */
109 offset = ip - ops->trampoline;
110 tramp_addr += offset;
112 /* Prevent unlikely recursion */
113 if (ip == tramp_addr)
116 return orc_find(tramp_addr);
119 static struct orc_entry *orc_ftrace_find(unsigned long ip)
126 * If we crash with IP==0, the last successfully executed instruction
127 * was probably an indirect function call with a NULL function pointer,
128 * and we don't have unwind information for NULL.
129 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
130 * pointer into its parent and then continue normally from there.
132 static struct orc_entry null_orc_entry = {
133 .sp_offset = sizeof(long),
134 .sp_reg = ORC_REG_SP,
135 .bp_reg = ORC_REG_UNDEFINED,
136 .type = UNWIND_HINT_TYPE_CALL
139 /* Fake frame pointer entry -- used as a fallback for generated code */
140 static struct orc_entry orc_fp_entry = {
141 .type = UNWIND_HINT_TYPE_CALL,
142 .sp_reg = ORC_REG_BP,
144 .bp_reg = ORC_REG_PREV_SP,
149 static struct orc_entry *orc_find(unsigned long ip)
151 static struct orc_entry *orc;
154 return &null_orc_entry;
156 /* For non-init vmlinux addresses, use the fast lookup table: */
157 if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
158 unsigned int idx, start, stop;
160 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
162 if (unlikely((idx >= lookup_num_blocks-1))) {
163 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
164 idx, lookup_num_blocks, (void *)ip);
168 start = orc_lookup[idx];
169 stop = orc_lookup[idx + 1] + 1;
171 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
172 (__start_orc_unwind + stop > __stop_orc_unwind))) {
173 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
174 idx, lookup_num_blocks, start, stop, (void *)ip);
178 return __orc_find(__start_orc_unwind_ip + start,
179 __start_orc_unwind + start, stop - start, ip);
182 /* vmlinux .init slow lookup: */
183 if (init_kernel_text(ip))
184 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
185 __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
188 orc = orc_module_find(ip);
192 return orc_ftrace_find(ip);
195 #ifdef CONFIG_MODULES
197 static DEFINE_MUTEX(sort_mutex);
198 static int *cur_orc_ip_table = __start_orc_unwind_ip;
199 static struct orc_entry *cur_orc_table = __start_orc_unwind;
201 static void orc_sort_swap(void *_a, void *_b, int size)
203 struct orc_entry *orc_a, *orc_b;
204 struct orc_entry orc_tmp;
205 int *a = _a, *b = _b, tmp;
208 /* Swap the .orc_unwind_ip entries: */
213 /* Swap the corresponding .orc_unwind entries: */
214 orc_a = cur_orc_table + (a - cur_orc_ip_table);
215 orc_b = cur_orc_table + (b - cur_orc_ip_table);
221 static int orc_sort_cmp(const void *_a, const void *_b)
223 struct orc_entry *orc_a;
224 const int *a = _a, *b = _b;
225 unsigned long a_val = orc_ip(a);
226 unsigned long b_val = orc_ip(b);
234 * The "weak" section terminator entries need to always be on the left
235 * to ensure the lookup code skips them in favor of real entries.
236 * These terminator entries exist to handle any gaps created by
237 * whitelisted .o files which didn't get objtool generation.
239 orc_a = cur_orc_table + (a - cur_orc_ip_table);
240 return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
243 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
244 void *_orc, size_t orc_size)
246 int *orc_ip = _orc_ip;
247 struct orc_entry *orc = _orc;
248 unsigned int num_entries = orc_ip_size / sizeof(int);
250 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
251 orc_size % sizeof(*orc) != 0 ||
252 num_entries != orc_size / sizeof(*orc));
255 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
256 * associate an .orc_unwind_ip table entry with its corresponding
257 * .orc_unwind entry so they can both be swapped.
259 mutex_lock(&sort_mutex);
260 cur_orc_ip_table = orc_ip;
262 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
263 mutex_unlock(&sort_mutex);
265 mod->arch.orc_unwind_ip = orc_ip;
266 mod->arch.orc_unwind = orc;
267 mod->arch.num_orcs = num_entries;
271 void __init unwind_init(void)
273 size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
274 size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
275 size_t num_entries = orc_ip_size / sizeof(int);
276 struct orc_entry *orc;
279 if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
280 orc_size % sizeof(struct orc_entry) != 0 ||
281 num_entries != orc_size / sizeof(struct orc_entry)) {
282 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
287 * Note, the orc_unwind and orc_unwind_ip tables were already
288 * sorted at build time via the 'sorttable' tool.
289 * It's ready for binary search straight away, no need to sort it.
292 /* Initialize the fast lookup table: */
293 lookup_num_blocks = orc_lookup_end - orc_lookup;
294 for (i = 0; i < lookup_num_blocks-1; i++) {
295 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
297 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
299 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
303 orc_lookup[i] = orc - __start_orc_unwind;
306 /* Initialize the ending block: */
307 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
310 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
313 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
318 unsigned long unwind_get_return_address(struct unwind_state *state)
320 if (unwind_done(state))
323 return __kernel_text_address(state->ip) ? state->ip : 0;
325 EXPORT_SYMBOL_GPL(unwind_get_return_address);
327 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
329 if (unwind_done(state))
333 return &state->regs->ip;
336 return (unsigned long *)state->sp - 1;
341 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
344 struct stack_info *info = &state->stack_info;
345 void *addr = (void *)_addr;
347 if (!on_stack(info, addr, len) &&
348 (get_stack_info(addr, state->task, info, &state->stack_mask)))
354 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
357 if (!stack_access_ok(state, addr, sizeof(long)))
360 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
364 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
365 unsigned long *ip, unsigned long *sp)
367 struct pt_regs *regs = (struct pt_regs *)addr;
369 /* x86-32 support will be more complicated due to the ®s->sp hack */
370 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
372 if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
375 *ip = READ_ONCE_NOCHECK(regs->ip);
376 *sp = READ_ONCE_NOCHECK(regs->sp);
380 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
381 unsigned long *ip, unsigned long *sp)
383 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
385 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
388 *ip = READ_ONCE_NOCHECK(regs->ip);
389 *sp = READ_ONCE_NOCHECK(regs->sp);
394 * If state->regs is non-NULL, and points to a full pt_regs, just get the reg
395 * value from state->regs.
397 * Otherwise, if state->regs just points to IRET regs, and the previous frame
398 * had full regs, it's safe to get the value from the previous regs. This can
399 * happen when early/late IRQ entry code gets interrupted by an NMI.
401 static bool get_reg(struct unwind_state *state, unsigned int reg_off,
404 unsigned int reg = reg_off/8;
409 if (state->full_regs) {
410 *val = READ_ONCE_NOCHECK(((unsigned long *)state->regs)[reg]);
414 if (state->prev_regs) {
415 *val = READ_ONCE_NOCHECK(((unsigned long *)state->prev_regs)[reg]);
422 bool unwind_next_frame(struct unwind_state *state)
424 unsigned long ip_p, sp, tmp, orig_ip = state->ip, prev_sp = state->sp;
425 enum stack_type prev_type = state->stack_info.type;
426 struct orc_entry *orc;
427 bool indirect = false;
429 if (unwind_done(state))
432 /* Don't let modules unload while we're reading their ORC data. */
435 /* End-of-stack check for user tasks: */
436 if (state->regs && user_mode(state->regs))
440 * Find the orc_entry associated with the text address.
442 * For a call frame (as opposed to a signal frame), state->ip points to
443 * the instruction after the call. That instruction's stack layout
444 * could be different from the call instruction's layout, for example
445 * if the call was to a noreturn function. So get the ORC data for the
446 * call instruction itself.
448 orc = orc_find(state->signal ? state->ip : state->ip - 1);
451 * As a fallback, try to assume this code uses a frame pointer.
452 * This is useful for generated code, like BPF, which ORC
453 * doesn't know about. This is just a guess, so the rest of
454 * the unwind is no longer considered reliable.
460 /* End-of-stack check for kernel threads: */
461 if (orc->sp_reg == ORC_REG_UNDEFINED) {
468 /* Find the previous frame's stack: */
469 switch (orc->sp_reg) {
471 sp = state->sp + orc->sp_offset;
475 sp = state->bp + orc->sp_offset;
478 case ORC_REG_SP_INDIRECT:
479 sp = state->sp + orc->sp_offset;
483 case ORC_REG_BP_INDIRECT:
484 sp = state->bp + orc->sp_offset;
489 if (!get_reg(state, offsetof(struct pt_regs, r10), &sp)) {
490 orc_warn_current("missing R10 value at %pB\n",
497 if (!get_reg(state, offsetof(struct pt_regs, r13), &sp)) {
498 orc_warn_current("missing R13 value at %pB\n",
505 if (!get_reg(state, offsetof(struct pt_regs, di), &sp)) {
506 orc_warn_current("missing RDI value at %pB\n",
513 if (!get_reg(state, offsetof(struct pt_regs, dx), &sp)) {
514 orc_warn_current("missing DX value at %pB\n",
521 orc_warn("unknown SP base reg %d at %pB\n",
522 orc->sp_reg, (void *)state->ip);
527 if (!deref_stack_reg(state, sp, &sp))
531 /* Find IP, SP and possibly regs: */
533 case UNWIND_HINT_TYPE_CALL:
534 ip_p = sp - sizeof(long);
536 if (!deref_stack_reg(state, ip_p, &state->ip))
539 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
540 state->ip, (void *)ip_p);
544 state->prev_regs = NULL;
545 state->signal = false;
548 case UNWIND_HINT_TYPE_REGS:
549 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
550 orc_warn_current("can't access registers at %pB\n",
555 state->regs = (struct pt_regs *)sp;
556 state->prev_regs = NULL;
557 state->full_regs = true;
558 state->signal = true;
561 case UNWIND_HINT_TYPE_REGS_PARTIAL:
562 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
563 orc_warn_current("can't access iret registers at %pB\n",
568 if (state->full_regs)
569 state->prev_regs = state->regs;
570 state->regs = (void *)sp - IRET_FRAME_OFFSET;
571 state->full_regs = false;
572 state->signal = true;
576 orc_warn("unknown .orc_unwind entry type %d at %pB\n",
577 orc->type, (void *)orig_ip);
582 switch (orc->bp_reg) {
583 case ORC_REG_UNDEFINED:
584 if (get_reg(state, offsetof(struct pt_regs, bp), &tmp))
588 case ORC_REG_PREV_SP:
589 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
594 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
599 orc_warn("unknown BP base reg %d for ip %pB\n",
600 orc->bp_reg, (void *)orig_ip);
604 /* Prevent a recursive loop due to bad ORC data: */
605 if (state->stack_info.type == prev_type &&
606 on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
607 state->sp <= prev_sp) {
608 orc_warn_current("stack going in the wrong direction? at %pB\n",
621 state->stack_info.type = STACK_TYPE_UNKNOWN;
624 EXPORT_SYMBOL_GPL(unwind_next_frame);
626 void __unwind_start(struct unwind_state *state, struct task_struct *task,
627 struct pt_regs *regs, unsigned long *first_frame)
629 memset(state, 0, sizeof(*state));
636 * Refuse to unwind the stack of a task while it's executing on another
637 * CPU. This check is racy, but that's ok: the unwinder has other
638 * checks to prevent it from going off the rails.
640 if (task_on_another_cpu(task))
647 state->ip = regs->ip;
648 state->sp = regs->sp;
649 state->bp = regs->bp;
651 state->full_regs = true;
652 state->signal = true;
654 } else if (task == current) {
655 asm volatile("lea (%%rip), %0\n\t"
658 : "=r" (state->ip), "=r" (state->sp),
662 struct inactive_task_frame *frame = (void *)task->thread.sp;
664 state->sp = task->thread.sp + sizeof(*frame);
665 state->bp = READ_ONCE_NOCHECK(frame->bp);
666 state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
667 state->signal = (void *)state->ip == ret_from_fork;
670 if (get_stack_info((unsigned long *)state->sp, state->task,
671 &state->stack_info, &state->stack_mask)) {
673 * We weren't on a valid stack. It's possible that
674 * we overflowed a valid stack into a guard page.
675 * See if the next page up is valid so that we can
676 * generate some kind of backtrace if this happens.
678 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
680 if (get_stack_info(next_page, state->task, &state->stack_info,
686 * The caller can provide the address of the first frame directly
687 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
688 * to start unwinding at. Skip ahead until we reach it.
691 /* When starting from regs, skip the regs frame: */
693 unwind_next_frame(state);
697 /* Otherwise, skip ahead to the user-specified starting frame: */
698 while (!unwind_done(state) &&
699 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
700 state->sp <= (unsigned long)first_frame))
701 unwind_next_frame(state);
708 state->stack_info.type = STACK_TYPE_UNKNOWN;
710 EXPORT_SYMBOL_GPL(__unwind_start);