1 // SPDX-License-Identifier: GPL-2.0-only
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>
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;
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;
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) {
56 return u_table + (found - ip_table);
60 static struct orc_entry *orc_module_find(unsigned long ip)
64 mod = __module_address(ip);
65 if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
67 return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
68 mod->arch.num_orcs, ip);
71 static struct orc_entry *orc_module_find(unsigned long ip)
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;
95 ops = ftrace_ops_trampoline(ip);
99 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
100 caller = (unsigned long)ftrace_regs_call;
102 caller = (unsigned long)ftrace_call;
104 /* Prevent unlikely recursion */
108 return orc_find(caller);
111 static struct orc_entry *orc_ftrace_find(unsigned long ip)
118 * If we crash with IP==0, the last successfully executed instruction
119 * was probably an indirect function call with a NULL function pointer,
120 * and we don't have unwind information for NULL.
121 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
122 * pointer into its parent and then continue normally from there.
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
131 static struct orc_entry *orc_find(unsigned long ip)
133 static struct orc_entry *orc;
139 return &null_orc_entry;
141 /* For non-init vmlinux addresses, use the fast lookup table: */
142 if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
143 unsigned int idx, start, stop;
145 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
147 if (unlikely((idx >= lookup_num_blocks-1))) {
148 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
149 idx, lookup_num_blocks, (void *)ip);
153 start = orc_lookup[idx];
154 stop = orc_lookup[idx + 1] + 1;
156 if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
157 (__start_orc_unwind + stop > __stop_orc_unwind))) {
158 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
159 idx, lookup_num_blocks, start, stop, (void *)ip);
163 return __orc_find(__start_orc_unwind_ip + start,
164 __start_orc_unwind + start, stop - start, ip);
167 /* vmlinux .init slow lookup: */
168 if (init_kernel_text(ip))
169 return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
170 __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
173 orc = orc_module_find(ip);
177 return orc_ftrace_find(ip);
180 static void orc_sort_swap(void *_a, void *_b, int size)
182 struct orc_entry *orc_a, *orc_b;
183 struct orc_entry orc_tmp;
184 int *a = _a, *b = _b, tmp;
187 /* Swap the .orc_unwind_ip entries: */
192 /* Swap the corresponding .orc_unwind entries: */
193 orc_a = cur_orc_table + (a - cur_orc_ip_table);
194 orc_b = cur_orc_table + (b - cur_orc_ip_table);
200 static int orc_sort_cmp(const void *_a, const void *_b)
202 struct orc_entry *orc_a;
203 const int *a = _a, *b = _b;
204 unsigned long a_val = orc_ip(a);
205 unsigned long b_val = orc_ip(b);
213 * The "weak" section terminator entries need to always be on the left
214 * to ensure the lookup code skips them in favor of real entries.
215 * These terminator entries exist to handle any gaps created by
216 * whitelisted .o files which didn't get objtool generation.
218 orc_a = cur_orc_table + (a - cur_orc_ip_table);
219 return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
222 #ifdef CONFIG_MODULES
223 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
224 void *_orc, size_t orc_size)
226 int *orc_ip = _orc_ip;
227 struct orc_entry *orc = _orc;
228 unsigned int num_entries = orc_ip_size / sizeof(int);
230 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
231 orc_size % sizeof(*orc) != 0 ||
232 num_entries != orc_size / sizeof(*orc));
235 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
236 * associate an .orc_unwind_ip table entry with its corresponding
237 * .orc_unwind entry so they can both be swapped.
239 mutex_lock(&sort_mutex);
240 cur_orc_ip_table = orc_ip;
242 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
243 mutex_unlock(&sort_mutex);
245 mod->arch.orc_unwind_ip = orc_ip;
246 mod->arch.orc_unwind = orc;
247 mod->arch.num_orcs = num_entries;
251 void __init unwind_init(void)
253 size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
254 size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
255 size_t num_entries = orc_ip_size / sizeof(int);
256 struct orc_entry *orc;
259 if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
260 orc_size % sizeof(struct orc_entry) != 0 ||
261 num_entries != orc_size / sizeof(struct orc_entry)) {
262 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
266 /* Sort the .orc_unwind and .orc_unwind_ip tables: */
267 sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
270 /* Initialize the fast lookup table: */
271 lookup_num_blocks = orc_lookup_end - orc_lookup;
272 for (i = 0; i < lookup_num_blocks-1; i++) {
273 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
275 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
277 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
281 orc_lookup[i] = orc - __start_orc_unwind;
284 /* Initialize the ending block: */
285 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
288 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
291 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
296 unsigned long unwind_get_return_address(struct unwind_state *state)
298 if (unwind_done(state))
301 return __kernel_text_address(state->ip) ? state->ip : 0;
303 EXPORT_SYMBOL_GPL(unwind_get_return_address);
305 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
307 if (unwind_done(state))
311 return &state->regs->ip;
314 return (unsigned long *)state->sp - 1;
319 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
322 struct stack_info *info = &state->stack_info;
323 void *addr = (void *)_addr;
325 if (!on_stack(info, addr, len) &&
326 (get_stack_info(addr, state->task, info, &state->stack_mask)))
332 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
335 if (!stack_access_ok(state, addr, sizeof(long)))
338 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
342 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
343 unsigned long *ip, unsigned long *sp)
345 struct pt_regs *regs = (struct pt_regs *)addr;
347 /* x86-32 support will be more complicated due to the ®s->sp hack */
348 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
350 if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
358 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
359 unsigned long *ip, unsigned long *sp)
361 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
363 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
371 bool unwind_next_frame(struct unwind_state *state)
373 unsigned long ip_p, sp, orig_ip = state->ip, prev_sp = state->sp;
374 enum stack_type prev_type = state->stack_info.type;
375 struct orc_entry *orc;
376 bool indirect = false;
378 if (unwind_done(state))
381 /* Don't let modules unload while we're reading their ORC data. */
384 /* End-of-stack check for user tasks: */
385 if (state->regs && user_mode(state->regs))
389 * Find the orc_entry associated with the text address.
391 * Decrement call return addresses by one so they work for sibling
392 * calls and calls to noreturn functions.
394 orc = orc_find(state->signal ? state->ip : state->ip - 1);
398 /* End-of-stack check for kernel threads: */
399 if (orc->sp_reg == ORC_REG_UNDEFINED) {
406 /* Find the previous frame's stack: */
407 switch (orc->sp_reg) {
409 sp = state->sp + orc->sp_offset;
413 sp = state->bp + orc->sp_offset;
416 case ORC_REG_SP_INDIRECT:
417 sp = state->sp + orc->sp_offset;
421 case ORC_REG_BP_INDIRECT:
422 sp = state->bp + orc->sp_offset;
427 if (!state->regs || !state->full_regs) {
428 orc_warn("missing regs for base reg R10 at ip %pB\n",
432 sp = state->regs->r10;
436 if (!state->regs || !state->full_regs) {
437 orc_warn("missing regs for base reg R13 at ip %pB\n",
441 sp = state->regs->r13;
445 if (!state->regs || !state->full_regs) {
446 orc_warn("missing regs for base reg DI at ip %pB\n",
450 sp = state->regs->di;
454 if (!state->regs || !state->full_regs) {
455 orc_warn("missing regs for base reg DX at ip %pB\n",
459 sp = state->regs->dx;
463 orc_warn("unknown SP base reg %d for ip %pB\n",
464 orc->sp_reg, (void *)state->ip);
469 if (!deref_stack_reg(state, sp, &sp))
473 /* Find IP, SP and possibly regs: */
476 ip_p = sp - sizeof(long);
478 if (!deref_stack_reg(state, ip_p, &state->ip))
481 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
482 state->ip, (void *)ip_p);
486 state->signal = false;
490 if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
491 orc_warn("can't dereference registers at %p for ip %pB\n",
492 (void *)sp, (void *)orig_ip);
496 state->regs = (struct pt_regs *)sp;
497 state->full_regs = true;
498 state->signal = true;
501 case ORC_TYPE_REGS_IRET:
502 if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
503 orc_warn("can't dereference iret registers at %p for ip %pB\n",
504 (void *)sp, (void *)orig_ip);
508 state->regs = (void *)sp - IRET_FRAME_OFFSET;
509 state->full_regs = false;
510 state->signal = true;
514 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
515 orc->type, (void *)orig_ip);
520 switch (orc->bp_reg) {
521 case ORC_REG_UNDEFINED:
522 if (state->regs && state->full_regs)
523 state->bp = state->regs->bp;
526 case ORC_REG_PREV_SP:
527 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
532 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
537 orc_warn("unknown BP base reg %d for ip %pB\n",
538 orc->bp_reg, (void *)orig_ip);
542 /* Prevent a recursive loop due to bad ORC data: */
543 if (state->stack_info.type == prev_type &&
544 on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
545 state->sp <= prev_sp) {
546 orc_warn("stack going in the wrong direction? ip=%pB\n",
559 state->stack_info.type = STACK_TYPE_UNKNOWN;
562 EXPORT_SYMBOL_GPL(unwind_next_frame);
564 void __unwind_start(struct unwind_state *state, struct task_struct *task,
565 struct pt_regs *regs, unsigned long *first_frame)
567 memset(state, 0, sizeof(*state));
571 * Refuse to unwind the stack of a task while it's executing on another
572 * CPU. This check is racy, but that's ok: the unwinder has other
573 * checks to prevent it from going off the rails.
575 if (task_on_another_cpu(task))
582 state->ip = regs->ip;
583 state->sp = kernel_stack_pointer(regs);
584 state->bp = regs->bp;
586 state->full_regs = true;
587 state->signal = true;
589 } else if (task == current) {
590 asm volatile("lea (%%rip), %0\n\t"
593 : "=r" (state->ip), "=r" (state->sp),
597 struct inactive_task_frame *frame = (void *)task->thread.sp;
599 state->sp = task->thread.sp;
600 state->bp = READ_ONCE_NOCHECK(frame->bp);
601 state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
604 if (get_stack_info((unsigned long *)state->sp, state->task,
605 &state->stack_info, &state->stack_mask)) {
607 * We weren't on a valid stack. It's possible that
608 * we overflowed a valid stack into a guard page.
609 * See if the next page up is valid so that we can
610 * generate some kind of backtrace if this happens.
612 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
613 if (get_stack_info(next_page, state->task, &state->stack_info,
619 * The caller can provide the address of the first frame directly
620 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
621 * to start unwinding at. Skip ahead until we reach it.
624 /* When starting from regs, skip the regs frame: */
626 unwind_next_frame(state);
630 /* Otherwise, skip ahead to the user-specified starting frame: */
631 while (!unwind_done(state) &&
632 (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
633 state->sp <= (unsigned long)first_frame))
634 unwind_next_frame(state);
639 state->stack_info.type = STACK_TYPE_UNKNOWN;
642 EXPORT_SYMBOL_GPL(__unwind_start);