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>
10 #include <asm/orc_header.h>
14 #define orc_warn(fmt, ...) \
15 printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
17 #define orc_warn_current(args...) \
19 static bool dumped_before; \
20 if (state->task == current && !state->error) { \
22 if (unwind_debug && !dumped_before) { \
23 dumped_before = true; \
29 extern int __start_orc_unwind_ip
[];
30 extern int __stop_orc_unwind_ip
[];
31 extern struct orc_entry __start_orc_unwind
[];
32 extern struct orc_entry __stop_orc_unwind
[];
34 static bool orc_init __ro_after_init
;
35 static bool unwind_debug __ro_after_init
;
36 static unsigned int lookup_num_blocks __ro_after_init
;
38 static int __init
unwind_debug_cmdline(char *str
)
44 early_param("unwind_debug", unwind_debug_cmdline
);
46 static void unwind_dump(struct unwind_state
*state
)
48 static bool dumped_before
;
49 unsigned long word
, *sp
;
50 struct stack_info stack_info
= {0};
51 unsigned long visit_mask
= 0;
58 printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
59 state
->stack_info
.type
, state
->stack_info
.next_sp
,
60 state
->stack_mask
, state
->graph_idx
);
62 for (sp
= __builtin_frame_address(0); sp
;
63 sp
= PTR_ALIGN(stack_info
.next_sp
, sizeof(long))) {
64 if (get_stack_info(sp
, state
->task
, &stack_info
, &visit_mask
))
67 for (; sp
< stack_info
.end
; sp
++) {
69 word
= READ_ONCE_NOCHECK(*sp
);
71 printk_deferred("%0*lx: %0*lx (%pB)\n", BITS_PER_LONG
/4,
72 (unsigned long)sp
, BITS_PER_LONG
/4,
78 static inline unsigned long orc_ip(const int *ip
)
80 return (unsigned long)ip
+ *ip
;
83 static struct orc_entry
*__orc_find(int *ip_table
, struct orc_entry
*u_table
,
84 unsigned int num_entries
, unsigned long ip
)
86 int *first
= ip_table
;
87 int *last
= ip_table
+ num_entries
- 1;
88 int *mid
, *found
= first
;
94 * Do a binary range search to find the rightmost duplicate of a given
95 * starting address. Some entries are section terminators which are
96 * "weak" entries for ensuring there are no gaps. They should be
97 * ignored when they conflict with a real entry.
99 while (first
<= last
) {
100 mid
= first
+ ((last
- first
) / 2);
102 if (orc_ip(mid
) <= ip
) {
109 return u_table
+ (found
- ip_table
);
112 #ifdef CONFIG_MODULES
113 static struct orc_entry
*orc_module_find(unsigned long ip
)
117 mod
= __module_address(ip
);
118 if (!mod
|| !mod
->arch
.orc_unwind
|| !mod
->arch
.orc_unwind_ip
)
120 return __orc_find(mod
->arch
.orc_unwind_ip
, mod
->arch
.orc_unwind
,
121 mod
->arch
.num_orcs
, ip
);
124 static struct orc_entry
*orc_module_find(unsigned long ip
)
130 #ifdef CONFIG_DYNAMIC_FTRACE
131 static struct orc_entry
*orc_find(unsigned long ip
);
134 * Ftrace dynamic trampolines do not have orc entries of their own.
135 * But they are copies of the ftrace entries that are static and
136 * defined in ftrace_*.S, which do have orc entries.
138 * If the unwinder comes across a ftrace trampoline, then find the
139 * ftrace function that was used to create it, and use that ftrace
140 * function's orc entry, as the placement of the return code in
141 * the stack will be identical.
143 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
145 struct ftrace_ops
*ops
;
146 unsigned long tramp_addr
, offset
;
148 ops
= ftrace_ops_trampoline(ip
);
152 /* Set tramp_addr to the start of the code copied by the trampoline */
153 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
154 tramp_addr
= (unsigned long)ftrace_regs_caller
;
156 tramp_addr
= (unsigned long)ftrace_caller
;
158 /* Now place tramp_addr to the location within the trampoline ip is at */
159 offset
= ip
- ops
->trampoline
;
160 tramp_addr
+= offset
;
162 /* Prevent unlikely recursion */
163 if (ip
== tramp_addr
)
166 return orc_find(tramp_addr
);
169 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
176 * If we crash with IP==0, the last successfully executed instruction
177 * was probably an indirect function call with a NULL function pointer,
178 * and we don't have unwind information for NULL.
179 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
180 * pointer into its parent and then continue normally from there.
182 static struct orc_entry null_orc_entry
= {
183 .sp_offset
= sizeof(long),
184 .sp_reg
= ORC_REG_SP
,
185 .bp_reg
= ORC_REG_UNDEFINED
,
186 .type
= ORC_TYPE_CALL
189 /* Fake frame pointer entry -- used as a fallback for generated code */
190 static struct orc_entry orc_fp_entry
= {
191 .type
= ORC_TYPE_CALL
,
192 .sp_reg
= ORC_REG_BP
,
194 .bp_reg
= ORC_REG_PREV_SP
,
198 static struct orc_entry
*orc_find(unsigned long ip
)
200 static struct orc_entry
*orc
;
203 return &null_orc_entry
;
205 /* For non-init vmlinux addresses, use the fast lookup table: */
206 if (ip
>= LOOKUP_START_IP
&& ip
< LOOKUP_STOP_IP
) {
207 unsigned int idx
, start
, stop
;
209 idx
= (ip
- LOOKUP_START_IP
) / LOOKUP_BLOCK_SIZE
;
211 if (unlikely((idx
>= lookup_num_blocks
-1))) {
212 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
213 idx
, lookup_num_blocks
, (void *)ip
);
217 start
= orc_lookup
[idx
];
218 stop
= orc_lookup
[idx
+ 1] + 1;
220 if (unlikely((__start_orc_unwind
+ start
>= __stop_orc_unwind
) ||
221 (__start_orc_unwind
+ stop
> __stop_orc_unwind
))) {
222 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
223 idx
, lookup_num_blocks
, start
, stop
, (void *)ip
);
227 return __orc_find(__start_orc_unwind_ip
+ start
,
228 __start_orc_unwind
+ start
, stop
- start
, ip
);
231 /* vmlinux .init slow lookup: */
232 if (is_kernel_inittext(ip
))
233 return __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
234 __stop_orc_unwind_ip
- __start_orc_unwind_ip
, ip
);
237 orc
= orc_module_find(ip
);
241 return orc_ftrace_find(ip
);
244 #ifdef CONFIG_MODULES
246 static DEFINE_MUTEX(sort_mutex
);
247 static int *cur_orc_ip_table
= __start_orc_unwind_ip
;
248 static struct orc_entry
*cur_orc_table
= __start_orc_unwind
;
250 static void orc_sort_swap(void *_a
, void *_b
, int size
)
252 struct orc_entry
*orc_a
, *orc_b
;
253 int *a
= _a
, *b
= _b
, tmp
;
256 /* Swap the .orc_unwind_ip entries: */
261 /* Swap the corresponding .orc_unwind entries: */
262 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
263 orc_b
= cur_orc_table
+ (b
- cur_orc_ip_table
);
264 swap(*orc_a
, *orc_b
);
267 static int orc_sort_cmp(const void *_a
, const void *_b
)
269 struct orc_entry
*orc_a
;
270 const int *a
= _a
, *b
= _b
;
271 unsigned long a_val
= orc_ip(a
);
272 unsigned long b_val
= orc_ip(b
);
280 * The "weak" section terminator entries need to always be first
281 * to ensure the lookup code skips them in favor of real entries.
282 * These terminator entries exist to handle any gaps created by
283 * whitelisted .o files which didn't get objtool generation.
285 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
286 return orc_a
->type
== ORC_TYPE_UNDEFINED
? -1 : 1;
289 void unwind_module_init(struct module
*mod
, void *_orc_ip
, size_t orc_ip_size
,
290 void *_orc
, size_t orc_size
)
292 int *orc_ip
= _orc_ip
;
293 struct orc_entry
*orc
= _orc
;
294 unsigned int num_entries
= orc_ip_size
/ sizeof(int);
296 WARN_ON_ONCE(orc_ip_size
% sizeof(int) != 0 ||
297 orc_size
% sizeof(*orc
) != 0 ||
298 num_entries
!= orc_size
/ sizeof(*orc
));
301 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
302 * associate an .orc_unwind_ip table entry with its corresponding
303 * .orc_unwind entry so they can both be swapped.
305 mutex_lock(&sort_mutex
);
306 cur_orc_ip_table
= orc_ip
;
308 sort(orc_ip
, num_entries
, sizeof(int), orc_sort_cmp
, orc_sort_swap
);
309 mutex_unlock(&sort_mutex
);
311 mod
->arch
.orc_unwind_ip
= orc_ip
;
312 mod
->arch
.orc_unwind
= orc
;
313 mod
->arch
.num_orcs
= num_entries
;
317 void __init
unwind_init(void)
319 size_t orc_ip_size
= (void *)__stop_orc_unwind_ip
- (void *)__start_orc_unwind_ip
;
320 size_t orc_size
= (void *)__stop_orc_unwind
- (void *)__start_orc_unwind
;
321 size_t num_entries
= orc_ip_size
/ sizeof(int);
322 struct orc_entry
*orc
;
325 if (!num_entries
|| orc_ip_size
% sizeof(int) != 0 ||
326 orc_size
% sizeof(struct orc_entry
) != 0 ||
327 num_entries
!= orc_size
/ sizeof(struct orc_entry
)) {
328 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
333 * Note, the orc_unwind and orc_unwind_ip tables were already
334 * sorted at build time via the 'sorttable' tool.
335 * It's ready for binary search straight away, no need to sort it.
338 /* Initialize the fast lookup table: */
339 lookup_num_blocks
= orc_lookup_end
- orc_lookup
;
340 for (i
= 0; i
< lookup_num_blocks
-1; i
++) {
341 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
343 LOOKUP_START_IP
+ (LOOKUP_BLOCK_SIZE
* i
));
345 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
349 orc_lookup
[i
] = orc
- __start_orc_unwind
;
352 /* Initialize the ending block: */
353 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
, num_entries
,
356 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
359 orc_lookup
[lookup_num_blocks
-1] = orc
- __start_orc_unwind
;
364 unsigned long unwind_get_return_address(struct unwind_state
*state
)
366 if (unwind_done(state
))
369 return __kernel_text_address(state
->ip
) ? state
->ip
: 0;
371 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
373 unsigned long *unwind_get_return_address_ptr(struct unwind_state
*state
)
375 if (unwind_done(state
))
379 return &state
->regs
->ip
;
382 return (unsigned long *)state
->sp
- 1;
387 static bool stack_access_ok(struct unwind_state
*state
, unsigned long _addr
,
390 struct stack_info
*info
= &state
->stack_info
;
391 void *addr
= (void *)_addr
;
393 if (on_stack(info
, addr
, len
))
396 return !get_stack_info(addr
, state
->task
, info
, &state
->stack_mask
) &&
397 on_stack(info
, addr
, len
);
400 static bool deref_stack_reg(struct unwind_state
*state
, unsigned long addr
,
403 if (!stack_access_ok(state
, addr
, sizeof(long)))
406 *val
= READ_ONCE_NOCHECK(*(unsigned long *)addr
);
410 static bool deref_stack_regs(struct unwind_state
*state
, unsigned long addr
,
411 unsigned long *ip
, unsigned long *sp
)
413 struct pt_regs
*regs
= (struct pt_regs
*)addr
;
415 /* x86-32 support will be more complicated due to the ®s->sp hack */
416 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32
));
418 if (!stack_access_ok(state
, addr
, sizeof(struct pt_regs
)))
421 *ip
= READ_ONCE_NOCHECK(regs
->ip
);
422 *sp
= READ_ONCE_NOCHECK(regs
->sp
);
426 static bool deref_stack_iret_regs(struct unwind_state
*state
, unsigned long addr
,
427 unsigned long *ip
, unsigned long *sp
)
429 struct pt_regs
*regs
= (void *)addr
- IRET_FRAME_OFFSET
;
431 if (!stack_access_ok(state
, addr
, IRET_FRAME_SIZE
))
434 *ip
= READ_ONCE_NOCHECK(regs
->ip
);
435 *sp
= READ_ONCE_NOCHECK(regs
->sp
);
440 * If state->regs is non-NULL, and points to a full pt_regs, just get the reg
441 * value from state->regs.
443 * Otherwise, if state->regs just points to IRET regs, and the previous frame
444 * had full regs, it's safe to get the value from the previous regs. This can
445 * happen when early/late IRQ entry code gets interrupted by an NMI.
447 static bool get_reg(struct unwind_state
*state
, unsigned int reg_off
,
450 unsigned int reg
= reg_off
/8;
455 if (state
->full_regs
) {
456 *val
= READ_ONCE_NOCHECK(((unsigned long *)state
->regs
)[reg
]);
460 if (state
->prev_regs
) {
461 *val
= READ_ONCE_NOCHECK(((unsigned long *)state
->prev_regs
)[reg
]);
468 bool unwind_next_frame(struct unwind_state
*state
)
470 unsigned long ip_p
, sp
, tmp
, orig_ip
= state
->ip
, prev_sp
= state
->sp
;
471 enum stack_type prev_type
= state
->stack_info
.type
;
472 struct orc_entry
*orc
;
473 bool indirect
= false;
475 if (unwind_done(state
))
478 /* Don't let modules unload while we're reading their ORC data. */
481 /* End-of-stack check for user tasks: */
482 if (state
->regs
&& user_mode(state
->regs
))
486 * Find the orc_entry associated with the text address.
488 * For a call frame (as opposed to a signal frame), state->ip points to
489 * the instruction after the call. That instruction's stack layout
490 * could be different from the call instruction's layout, for example
491 * if the call was to a noreturn function. So get the ORC data for the
492 * call instruction itself.
494 orc
= orc_find(state
->signal
? state
->ip
: state
->ip
- 1);
497 * As a fallback, try to assume this code uses a frame pointer.
498 * This is useful for generated code, like BPF, which ORC
499 * doesn't know about. This is just a guess, so the rest of
500 * the unwind is no longer considered reliable.
505 if (orc
->type
== ORC_TYPE_UNDEFINED
)
508 if (orc
->type
== ORC_TYPE_END_OF_STACK
)
512 state
->signal
= orc
->signal
;
514 /* Find the previous frame's stack: */
515 switch (orc
->sp_reg
) {
517 sp
= state
->sp
+ orc
->sp_offset
;
521 sp
= state
->bp
+ orc
->sp_offset
;
524 case ORC_REG_SP_INDIRECT
:
529 case ORC_REG_BP_INDIRECT
:
530 sp
= state
->bp
+ orc
->sp_offset
;
535 if (!get_reg(state
, offsetof(struct pt_regs
, r10
), &sp
)) {
536 orc_warn_current("missing R10 value at %pB\n",
543 if (!get_reg(state
, offsetof(struct pt_regs
, r13
), &sp
)) {
544 orc_warn_current("missing R13 value at %pB\n",
551 if (!get_reg(state
, offsetof(struct pt_regs
, di
), &sp
)) {
552 orc_warn_current("missing RDI value at %pB\n",
559 if (!get_reg(state
, offsetof(struct pt_regs
, dx
), &sp
)) {
560 orc_warn_current("missing DX value at %pB\n",
567 orc_warn("unknown SP base reg %d at %pB\n",
568 orc
->sp_reg
, (void *)state
->ip
);
573 if (!deref_stack_reg(state
, sp
, &sp
))
576 if (orc
->sp_reg
== ORC_REG_SP_INDIRECT
)
577 sp
+= orc
->sp_offset
;
580 /* Find IP, SP and possibly regs: */
583 ip_p
= sp
- sizeof(long);
585 if (!deref_stack_reg(state
, ip_p
, &state
->ip
))
588 state
->ip
= unwind_recover_ret_addr(state
, state
->ip
,
589 (unsigned long *)ip_p
);
592 state
->prev_regs
= NULL
;
596 if (!deref_stack_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
597 orc_warn_current("can't access registers at %pB\n",
602 * There is a small chance to interrupt at the entry of
603 * arch_rethook_trampoline() where the ORC info doesn't exist.
604 * That point is right after the RET to arch_rethook_trampoline()
605 * which was modified return address.
606 * At that point, the @addr_p of the unwind_recover_rethook()
607 * (this has to point the address of the stack entry storing
608 * the modified return address) must be "SP - (a stack entry)"
609 * because SP is incremented by the RET.
611 state
->ip
= unwind_recover_rethook(state
, state
->ip
,
612 (unsigned long *)(state
->sp
- sizeof(long)));
613 state
->regs
= (struct pt_regs
*)sp
;
614 state
->prev_regs
= NULL
;
615 state
->full_regs
= true;
618 case ORC_TYPE_REGS_PARTIAL
:
619 if (!deref_stack_iret_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
620 orc_warn_current("can't access iret registers at %pB\n",
624 /* See ORC_TYPE_REGS case comment. */
625 state
->ip
= unwind_recover_rethook(state
, state
->ip
,
626 (unsigned long *)(state
->sp
- sizeof(long)));
628 if (state
->full_regs
)
629 state
->prev_regs
= state
->regs
;
630 state
->regs
= (void *)sp
- IRET_FRAME_OFFSET
;
631 state
->full_regs
= false;
635 orc_warn("unknown .orc_unwind entry type %d at %pB\n",
636 orc
->type
, (void *)orig_ip
);
641 switch (orc
->bp_reg
) {
642 case ORC_REG_UNDEFINED
:
643 if (get_reg(state
, offsetof(struct pt_regs
, bp
), &tmp
))
647 case ORC_REG_PREV_SP
:
648 if (!deref_stack_reg(state
, sp
+ orc
->bp_offset
, &state
->bp
))
653 if (!deref_stack_reg(state
, state
->bp
+ orc
->bp_offset
, &state
->bp
))
658 orc_warn("unknown BP base reg %d for ip %pB\n",
659 orc
->bp_reg
, (void *)orig_ip
);
663 /* Prevent a recursive loop due to bad ORC data: */
664 if (state
->stack_info
.type
== prev_type
&&
665 on_stack(&state
->stack_info
, (void *)state
->sp
, sizeof(long)) &&
666 state
->sp
<= prev_sp
) {
667 orc_warn_current("stack going in the wrong direction? at %pB\n",
680 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
683 EXPORT_SYMBOL_GPL(unwind_next_frame
);
685 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
686 struct pt_regs
*regs
, unsigned long *first_frame
)
688 memset(state
, 0, sizeof(*state
));
695 * Refuse to unwind the stack of a task while it's executing on another
696 * CPU. This check is racy, but that's ok: the unwinder has other
697 * checks to prevent it from going off the rails.
699 if (task_on_another_cpu(task
))
706 state
->ip
= regs
->ip
;
707 state
->sp
= regs
->sp
;
708 state
->bp
= regs
->bp
;
710 state
->full_regs
= true;
711 state
->signal
= true;
713 } else if (task
== current
) {
714 asm volatile("lea (%%rip), %0\n\t"
717 : "=r" (state
->ip
), "=r" (state
->sp
),
721 struct inactive_task_frame
*frame
= (void *)task
->thread
.sp
;
723 state
->sp
= task
->thread
.sp
+ sizeof(*frame
);
724 state
->bp
= READ_ONCE_NOCHECK(frame
->bp
);
725 state
->ip
= READ_ONCE_NOCHECK(frame
->ret_addr
);
726 state
->signal
= (void *)state
->ip
== ret_from_fork
;
729 if (get_stack_info((unsigned long *)state
->sp
, state
->task
,
730 &state
->stack_info
, &state
->stack_mask
)) {
732 * We weren't on a valid stack. It's possible that
733 * we overflowed a valid stack into a guard page.
734 * See if the next page up is valid so that we can
735 * generate some kind of backtrace if this happens.
737 void *next_page
= (void *)PAGE_ALIGN((unsigned long)state
->sp
);
739 if (get_stack_info(next_page
, state
->task
, &state
->stack_info
,
745 * The caller can provide the address of the first frame directly
746 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
747 * to start unwinding at. Skip ahead until we reach it.
750 /* When starting from regs, skip the regs frame: */
752 unwind_next_frame(state
);
756 /* Otherwise, skip ahead to the user-specified starting frame: */
757 while (!unwind_done(state
) &&
758 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
759 state
->sp
<= (unsigned long)first_frame
))
760 unwind_next_frame(state
);
767 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
769 EXPORT_SYMBOL_GPL(__unwind_start
);