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;
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)
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.
84 static struct orc_entry null_orc_entry = {
85 .sp_offset = sizeof(long),
87 .bp_reg = ORC_REG_UNDEFINED,
91 static struct orc_entry *orc_find(unsigned long ip)
94 return &null_orc_entry;
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;
100 idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
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);
108 start = orc_lookup[idx];
109 stop = orc_lookup[idx + 1] + 1;
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);
118 return __orc_find(__start_orc_unwind_ip + start,
119 __start_orc_unwind + start, stop - start, ip);
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);
128 return orc_module_find(ip);
131 static void orc_sort_swap(void *_a, void *_b, int size)
133 struct orc_entry *orc_a, *orc_b;
134 struct orc_entry orc_tmp;
135 int *a = _a, *b = _b, tmp;
138 /* Swap the .orc_unwind_ip entries: */
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);
151 static int orc_sort_cmp(const void *_a, const void *_b)
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);
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.
169 orc_a = cur_orc_table + (a - cur_orc_ip_table);
170 return orc_a->sp_reg == ORC_REG_UNDEFINED ? -1 : 1;
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)
177 int *orc_ip = _orc_ip;
178 struct orc_entry *orc = _orc;
179 unsigned int num_entries = orc_ip_size / sizeof(int);
181 WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
182 orc_size % sizeof(*orc) != 0 ||
183 num_entries != orc_size / sizeof(*orc));
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.
190 mutex_lock(&sort_mutex);
191 cur_orc_ip_table = orc_ip;
193 sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
194 mutex_unlock(&sort_mutex);
196 mod->arch.orc_unwind_ip = orc_ip;
197 mod->arch.orc_unwind = orc;
198 mod->arch.num_orcs = num_entries;
202 void __init unwind_init(void)
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;
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");
217 /* Sort the .orc_unwind and .orc_unwind_ip tables: */
218 sort(__start_orc_unwind_ip, num_entries, sizeof(int), orc_sort_cmp,
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,
226 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
228 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
232 orc_lookup[i] = orc - __start_orc_unwind;
235 /* Initialize the ending block: */
236 orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
239 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
242 orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
247 unsigned long unwind_get_return_address(struct unwind_state *state)
249 if (unwind_done(state))
252 return __kernel_text_address(state->ip) ? state->ip : 0;
254 EXPORT_SYMBOL_GPL(unwind_get_return_address);
256 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
258 if (unwind_done(state))
262 return &state->regs->ip;
265 return (unsigned long *)state->sp - 1;
270 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
273 struct stack_info *info = &state->stack_info;
274 void *addr = (void *)_addr;
276 if (!on_stack(info, addr, len) &&
277 (get_stack_info(addr, state->task, info, &state->stack_mask)))
283 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
286 if (!stack_access_ok(state, addr, sizeof(long)))
289 *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
293 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
294 unsigned long *ip, unsigned long *sp)
296 struct pt_regs *regs = (struct pt_regs *)addr;
298 /* x86-32 support will be more complicated due to the ®s->sp hack */
299 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
301 if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
309 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
310 unsigned long *ip, unsigned long *sp)
312 struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
314 if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
322 bool unwind_next_frame(struct unwind_state *state)
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;
329 if (unwind_done(state))
332 /* Don't let modules unload while we're reading their ORC data. */
335 /* Have we reached the end? */
336 if (state->regs && user_mode(state->regs))
340 * Find the orc_entry associated with the text address.
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.
348 orc = orc_find(state->signal ? state->ip : state->ip - 1);
349 if (!orc || orc->sp_reg == ORC_REG_UNDEFINED)
353 /* Find the previous frame's stack: */
354 switch (orc->sp_reg) {
356 sp = state->sp + orc->sp_offset;
360 sp = state->bp + orc->sp_offset;
363 case ORC_REG_SP_INDIRECT:
364 sp = state->sp + orc->sp_offset;
368 case ORC_REG_BP_INDIRECT:
369 sp = state->bp + orc->sp_offset;
374 if (!state->regs || !state->full_regs) {
375 orc_warn("missing regs for base reg R10 at ip %pB\n",
379 sp = state->regs->r10;
383 if (!state->regs || !state->full_regs) {
384 orc_warn("missing regs for base reg R13 at ip %pB\n",
388 sp = state->regs->r13;
392 if (!state->regs || !state->full_regs) {
393 orc_warn("missing regs for base reg DI at ip %pB\n",
397 sp = state->regs->di;
401 if (!state->regs || !state->full_regs) {
402 orc_warn("missing regs for base reg DX at ip %pB\n",
406 sp = state->regs->dx;
410 orc_warn("unknown SP base reg %d for ip %pB\n",
411 orc->sp_reg, (void *)state->ip);
416 if (!deref_stack_reg(state, sp, &sp))
420 /* Find IP, SP and possibly regs: */
423 ip_p = sp - sizeof(long);
425 if (!deref_stack_reg(state, ip_p, &state->ip))
428 state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
429 state->ip, (void *)ip_p);
433 state->signal = false;
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);
443 state->regs = (struct pt_regs *)sp;
444 state->full_regs = true;
445 state->signal = true;
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);
455 state->regs = (void *)sp - IRET_FRAME_OFFSET;
456 state->full_regs = false;
457 state->signal = true;
461 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
462 orc->type, (void *)orig_ip);
467 switch (orc->bp_reg) {
468 case ORC_REG_UNDEFINED:
469 if (state->regs && state->full_regs)
470 state->bp = state->regs->bp;
473 case ORC_REG_PREV_SP:
474 if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
479 if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
484 orc_warn("unknown BP base reg %d for ip %pB\n",
485 orc->bp_reg, (void *)orig_ip);
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",
503 state->stack_info.type = STACK_TYPE_UNKNOWN;
506 EXPORT_SYMBOL_GPL(unwind_next_frame);
508 void __unwind_start(struct unwind_state *state, struct task_struct *task,
509 struct pt_regs *regs, unsigned long *first_frame)
511 memset(state, 0, sizeof(*state));
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.
522 if (task_on_another_cpu(task))
529 state->ip = regs->ip;
530 state->sp = kernel_stack_pointer(regs);
531 state->bp = regs->bp;
533 state->full_regs = true;
534 state->signal = true;
536 } else if (task == current) {
537 asm volatile("lea (%%rip), %0\n\t"
540 : "=r" (state->ip), "=r" (state->sp),
544 struct inactive_task_frame *frame = (void *)task->thread.sp;
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;
552 if (get_stack_info((unsigned long *)state->sp, state->task,
553 &state->stack_info, &state->stack_mask)) {
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.
560 void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
562 if (get_stack_info(next_page, state->task, &state->stack_info,
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.
573 /* When starting from regs, skip the regs frame: */
575 unwind_next_frame(state);
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);
590 state->stack_info.type = STACK_TYPE_UNKNOWN;
592 EXPORT_SYMBOL_GPL(__unwind_start);