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 "WARNING: " fmt, ##__VA_ARGS__)
13 #define orc_warn_current(args...) \
15 if (state->task == current) \
19 extern int __start_orc_unwind_ip
[];
20 extern int __stop_orc_unwind_ip
[];
21 extern struct orc_entry __start_orc_unwind
[];
22 extern struct orc_entry __stop_orc_unwind
[];
24 static bool orc_init __ro_after_init
;
25 static unsigned int lookup_num_blocks __ro_after_init
;
27 static inline unsigned long orc_ip(const int *ip
)
29 return (unsigned long)ip
+ *ip
;
32 static struct orc_entry
*__orc_find(int *ip_table
, struct orc_entry
*u_table
,
33 unsigned int num_entries
, unsigned long ip
)
35 int *first
= ip_table
;
36 int *last
= ip_table
+ num_entries
- 1;
37 int *mid
= first
, *found
= first
;
43 * Do a binary range search to find the rightmost duplicate of a given
44 * starting address. Some entries are section terminators which are
45 * "weak" entries for ensuring there are no gaps. They should be
46 * ignored when they conflict with a real entry.
48 while (first
<= last
) {
49 mid
= first
+ ((last
- first
) / 2);
51 if (orc_ip(mid
) <= ip
) {
58 return u_table
+ (found
- ip_table
);
62 static struct orc_entry
*orc_module_find(unsigned long ip
)
66 mod
= __module_address(ip
);
67 if (!mod
|| !mod
->arch
.orc_unwind
|| !mod
->arch
.orc_unwind_ip
)
69 return __orc_find(mod
->arch
.orc_unwind_ip
, mod
->arch
.orc_unwind
,
70 mod
->arch
.num_orcs
, ip
);
73 static struct orc_entry
*orc_module_find(unsigned long ip
)
79 #ifdef CONFIG_DYNAMIC_FTRACE
80 static struct orc_entry
*orc_find(unsigned long ip
);
83 * Ftrace dynamic trampolines do not have orc entries of their own.
84 * But they are copies of the ftrace entries that are static and
85 * defined in ftrace_*.S, which do have orc entries.
87 * If the unwinder comes across a ftrace trampoline, then find the
88 * ftrace function that was used to create it, and use that ftrace
89 * function's orc entry, as the placement of the return code in
90 * the stack will be identical.
92 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
94 struct ftrace_ops
*ops
;
97 ops
= ftrace_ops_trampoline(ip
);
101 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
102 caller
= (unsigned long)ftrace_regs_call
;
104 caller
= (unsigned long)ftrace_call
;
106 /* Prevent unlikely recursion */
110 return orc_find(caller
);
113 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
120 * If we crash with IP==0, the last successfully executed instruction
121 * was probably an indirect function call with a NULL function pointer,
122 * and we don't have unwind information for NULL.
123 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
124 * pointer into its parent and then continue normally from there.
126 static struct orc_entry null_orc_entry
= {
127 .sp_offset
= sizeof(long),
128 .sp_reg
= ORC_REG_SP
,
129 .bp_reg
= ORC_REG_UNDEFINED
,
130 .type
= ORC_TYPE_CALL
133 /* Fake frame pointer entry -- used as a fallback for generated code */
134 static struct orc_entry orc_fp_entry
= {
135 .type
= ORC_TYPE_CALL
,
136 .sp_reg
= ORC_REG_BP
,
138 .bp_reg
= ORC_REG_PREV_SP
,
143 static struct orc_entry
*orc_find(unsigned long ip
)
145 static struct orc_entry
*orc
;
148 return &null_orc_entry
;
150 /* For non-init vmlinux addresses, use the fast lookup table: */
151 if (ip
>= LOOKUP_START_IP
&& ip
< LOOKUP_STOP_IP
) {
152 unsigned int idx
, start
, stop
;
154 idx
= (ip
- LOOKUP_START_IP
) / LOOKUP_BLOCK_SIZE
;
156 if (unlikely((idx
>= lookup_num_blocks
-1))) {
157 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
158 idx
, lookup_num_blocks
, (void *)ip
);
162 start
= orc_lookup
[idx
];
163 stop
= orc_lookup
[idx
+ 1] + 1;
165 if (unlikely((__start_orc_unwind
+ start
>= __stop_orc_unwind
) ||
166 (__start_orc_unwind
+ stop
> __stop_orc_unwind
))) {
167 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
168 idx
, lookup_num_blocks
, start
, stop
, (void *)ip
);
172 return __orc_find(__start_orc_unwind_ip
+ start
,
173 __start_orc_unwind
+ start
, stop
- start
, ip
);
176 /* vmlinux .init slow lookup: */
177 if (init_kernel_text(ip
))
178 return __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
179 __stop_orc_unwind_ip
- __start_orc_unwind_ip
, ip
);
182 orc
= orc_module_find(ip
);
186 return orc_ftrace_find(ip
);
189 #ifdef CONFIG_MODULES
191 static DEFINE_MUTEX(sort_mutex
);
192 static int *cur_orc_ip_table
= __start_orc_unwind_ip
;
193 static struct orc_entry
*cur_orc_table
= __start_orc_unwind
;
195 static void orc_sort_swap(void *_a
, void *_b
, int size
)
197 struct orc_entry
*orc_a
, *orc_b
;
198 struct orc_entry orc_tmp
;
199 int *a
= _a
, *b
= _b
, tmp
;
202 /* Swap the .orc_unwind_ip entries: */
207 /* Swap the corresponding .orc_unwind entries: */
208 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
209 orc_b
= cur_orc_table
+ (b
- cur_orc_ip_table
);
215 static int orc_sort_cmp(const void *_a
, const void *_b
)
217 struct orc_entry
*orc_a
;
218 const int *a
= _a
, *b
= _b
;
219 unsigned long a_val
= orc_ip(a
);
220 unsigned long b_val
= orc_ip(b
);
228 * The "weak" section terminator entries need to always be on the left
229 * to ensure the lookup code skips them in favor of real entries.
230 * These terminator entries exist to handle any gaps created by
231 * whitelisted .o files which didn't get objtool generation.
233 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
234 return orc_a
->sp_reg
== ORC_REG_UNDEFINED
&& !orc_a
->end
? -1 : 1;
237 void unwind_module_init(struct module
*mod
, void *_orc_ip
, size_t orc_ip_size
,
238 void *_orc
, size_t orc_size
)
240 int *orc_ip
= _orc_ip
;
241 struct orc_entry
*orc
= _orc
;
242 unsigned int num_entries
= orc_ip_size
/ sizeof(int);
244 WARN_ON_ONCE(orc_ip_size
% sizeof(int) != 0 ||
245 orc_size
% sizeof(*orc
) != 0 ||
246 num_entries
!= orc_size
/ sizeof(*orc
));
249 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
250 * associate an .orc_unwind_ip table entry with its corresponding
251 * .orc_unwind entry so they can both be swapped.
253 mutex_lock(&sort_mutex
);
254 cur_orc_ip_table
= orc_ip
;
256 sort(orc_ip
, num_entries
, sizeof(int), orc_sort_cmp
, orc_sort_swap
);
257 mutex_unlock(&sort_mutex
);
259 mod
->arch
.orc_unwind_ip
= orc_ip
;
260 mod
->arch
.orc_unwind
= orc
;
261 mod
->arch
.num_orcs
= num_entries
;
265 void __init
unwind_init(void)
267 size_t orc_ip_size
= (void *)__stop_orc_unwind_ip
- (void *)__start_orc_unwind_ip
;
268 size_t orc_size
= (void *)__stop_orc_unwind
- (void *)__start_orc_unwind
;
269 size_t num_entries
= orc_ip_size
/ sizeof(int);
270 struct orc_entry
*orc
;
273 if (!num_entries
|| orc_ip_size
% sizeof(int) != 0 ||
274 orc_size
% sizeof(struct orc_entry
) != 0 ||
275 num_entries
!= orc_size
/ sizeof(struct orc_entry
)) {
276 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
281 * Note, the orc_unwind and orc_unwind_ip tables were already
282 * sorted at build time via the 'sorttable' tool.
283 * It's ready for binary search straight away, no need to sort it.
286 /* Initialize the fast lookup table: */
287 lookup_num_blocks
= orc_lookup_end
- orc_lookup
;
288 for (i
= 0; i
< lookup_num_blocks
-1; i
++) {
289 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
291 LOOKUP_START_IP
+ (LOOKUP_BLOCK_SIZE
* i
));
293 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
297 orc_lookup
[i
] = orc
- __start_orc_unwind
;
300 /* Initialize the ending block: */
301 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
, num_entries
,
304 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
307 orc_lookup
[lookup_num_blocks
-1] = orc
- __start_orc_unwind
;
312 unsigned long unwind_get_return_address(struct unwind_state
*state
)
314 if (unwind_done(state
))
317 return __kernel_text_address(state
->ip
) ? state
->ip
: 0;
319 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
321 unsigned long *unwind_get_return_address_ptr(struct unwind_state
*state
)
323 struct task_struct
*task
= state
->task
;
325 if (unwind_done(state
))
329 return &state
->regs
->ip
;
331 if (task
!= current
&& state
->sp
== task
->thread
.sp
) {
332 struct inactive_task_frame
*frame
= (void *)task
->thread
.sp
;
333 return &frame
->ret_addr
;
337 return (unsigned long *)state
->sp
- 1;
342 static bool stack_access_ok(struct unwind_state
*state
, unsigned long _addr
,
345 struct stack_info
*info
= &state
->stack_info
;
346 void *addr
= (void *)_addr
;
348 if (!on_stack(info
, addr
, len
) &&
349 (get_stack_info(addr
, state
->task
, info
, &state
->stack_mask
)))
355 static bool deref_stack_reg(struct unwind_state
*state
, unsigned long addr
,
358 if (!stack_access_ok(state
, addr
, sizeof(long)))
361 *val
= READ_ONCE_NOCHECK(*(unsigned long *)addr
);
365 static bool deref_stack_regs(struct unwind_state
*state
, unsigned long addr
,
366 unsigned long *ip
, unsigned long *sp
)
368 struct pt_regs
*regs
= (struct pt_regs
*)addr
;
370 /* x86-32 support will be more complicated due to the ®s->sp hack */
371 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32
));
373 if (!stack_access_ok(state
, addr
, sizeof(struct pt_regs
)))
381 static bool deref_stack_iret_regs(struct unwind_state
*state
, unsigned long addr
,
382 unsigned long *ip
, unsigned long *sp
)
384 struct pt_regs
*regs
= (void *)addr
- IRET_FRAME_OFFSET
;
386 if (!stack_access_ok(state
, addr
, IRET_FRAME_SIZE
))
395 * If state->regs is non-NULL, and points to a full pt_regs, just get the reg
396 * value from state->regs.
398 * Otherwise, if state->regs just points to IRET regs, and the previous frame
399 * had full regs, it's safe to get the value from the previous regs. This can
400 * happen when early/late IRQ entry code gets interrupted by an NMI.
402 static bool get_reg(struct unwind_state
*state
, unsigned int reg_off
,
405 unsigned int reg
= reg_off
/8;
410 if (state
->full_regs
) {
411 *val
= ((unsigned long *)state
->regs
)[reg
];
415 if (state
->prev_regs
) {
416 *val
= ((unsigned long *)state
->prev_regs
)[reg
];
423 bool unwind_next_frame(struct unwind_state
*state
)
425 unsigned long ip_p
, sp
, tmp
, orig_ip
= state
->ip
, prev_sp
= state
->sp
;
426 enum stack_type prev_type
= state
->stack_info
.type
;
427 struct orc_entry
*orc
;
428 bool indirect
= false;
430 if (unwind_done(state
))
433 /* Don't let modules unload while we're reading their ORC data. */
436 /* End-of-stack check for user tasks: */
437 if (state
->regs
&& user_mode(state
->regs
))
441 * Find the orc_entry associated with the text address.
443 * Decrement call return addresses by one so they work for sibling
444 * calls and calls to noreturn functions.
446 orc
= orc_find(state
->signal
? state
->ip
: state
->ip
- 1);
449 * As a fallback, try to assume this code uses a frame pointer.
450 * This is useful for generated code, like BPF, which ORC
451 * doesn't know about. This is just a guess, so the rest of
452 * the unwind is no longer considered reliable.
458 /* End-of-stack check for kernel threads: */
459 if (orc
->sp_reg
== ORC_REG_UNDEFINED
) {
466 /* Find the previous frame's stack: */
467 switch (orc
->sp_reg
) {
469 sp
= state
->sp
+ orc
->sp_offset
;
473 sp
= state
->bp
+ orc
->sp_offset
;
476 case ORC_REG_SP_INDIRECT
:
477 sp
= state
->sp
+ orc
->sp_offset
;
481 case ORC_REG_BP_INDIRECT
:
482 sp
= state
->bp
+ orc
->sp_offset
;
487 if (!get_reg(state
, offsetof(struct pt_regs
, r10
), &sp
)) {
488 orc_warn_current("missing R10 value at %pB\n",
495 if (!get_reg(state
, offsetof(struct pt_regs
, r13
), &sp
)) {
496 orc_warn_current("missing R13 value at %pB\n",
503 if (!get_reg(state
, offsetof(struct pt_regs
, di
), &sp
)) {
504 orc_warn_current("missing RDI value at %pB\n",
511 if (!get_reg(state
, offsetof(struct pt_regs
, dx
), &sp
)) {
512 orc_warn_current("missing DX value at %pB\n",
519 orc_warn("unknown SP base reg %d at %pB\n",
520 orc
->sp_reg
, (void *)state
->ip
);
525 if (!deref_stack_reg(state
, sp
, &sp
))
529 /* Find IP, SP and possibly regs: */
532 ip_p
= sp
- sizeof(long);
534 if (!deref_stack_reg(state
, ip_p
, &state
->ip
))
537 state
->ip
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
,
538 state
->ip
, (void *)ip_p
);
542 state
->prev_regs
= NULL
;
543 state
->signal
= false;
547 if (!deref_stack_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
548 orc_warn_current("can't access registers at %pB\n",
553 state
->regs
= (struct pt_regs
*)sp
;
554 state
->prev_regs
= NULL
;
555 state
->full_regs
= true;
556 state
->signal
= true;
559 case ORC_TYPE_REGS_IRET
:
560 if (!deref_stack_iret_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
561 orc_warn_current("can't access iret registers at %pB\n",
566 if (state
->full_regs
)
567 state
->prev_regs
= state
->regs
;
568 state
->regs
= (void *)sp
- IRET_FRAME_OFFSET
;
569 state
->full_regs
= false;
570 state
->signal
= true;
574 orc_warn("unknown .orc_unwind entry type %d at %pB\n",
575 orc
->type
, (void *)orig_ip
);
580 switch (orc
->bp_reg
) {
581 case ORC_REG_UNDEFINED
:
582 if (get_reg(state
, offsetof(struct pt_regs
, bp
), &tmp
))
586 case ORC_REG_PREV_SP
:
587 if (!deref_stack_reg(state
, sp
+ orc
->bp_offset
, &state
->bp
))
592 if (!deref_stack_reg(state
, state
->bp
+ orc
->bp_offset
, &state
->bp
))
597 orc_warn("unknown BP base reg %d for ip %pB\n",
598 orc
->bp_reg
, (void *)orig_ip
);
602 /* Prevent a recursive loop due to bad ORC data: */
603 if (state
->stack_info
.type
== prev_type
&&
604 on_stack(&state
->stack_info
, (void *)state
->sp
, sizeof(long)) &&
605 state
->sp
<= prev_sp
) {
606 orc_warn_current("stack going in the wrong direction? at %pB\n",
619 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
622 EXPORT_SYMBOL_GPL(unwind_next_frame
);
624 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
625 struct pt_regs
*regs
, unsigned long *first_frame
)
627 memset(state
, 0, sizeof(*state
));
634 * Refuse to unwind the stack of a task while it's executing on another
635 * CPU. This check is racy, but that's ok: the unwinder has other
636 * checks to prevent it from going off the rails.
638 if (task_on_another_cpu(task
))
645 state
->ip
= regs
->ip
;
646 state
->sp
= regs
->sp
;
647 state
->bp
= regs
->bp
;
649 state
->full_regs
= true;
650 state
->signal
= true;
652 } else if (task
== current
) {
653 asm volatile("lea (%%rip), %0\n\t"
656 : "=r" (state
->ip
), "=r" (state
->sp
),
660 struct inactive_task_frame
*frame
= (void *)task
->thread
.sp
;
662 state
->sp
= task
->thread
.sp
;
663 state
->bp
= READ_ONCE_NOCHECK(frame
->bp
);
664 state
->ip
= READ_ONCE_NOCHECK(frame
->ret_addr
);
667 if (get_stack_info((unsigned long *)state
->sp
, state
->task
,
668 &state
->stack_info
, &state
->stack_mask
)) {
670 * We weren't on a valid stack. It's possible that
671 * we overflowed a valid stack into a guard page.
672 * See if the next page up is valid so that we can
673 * generate some kind of backtrace if this happens.
675 void *next_page
= (void *)PAGE_ALIGN((unsigned long)state
->sp
);
677 if (get_stack_info(next_page
, state
->task
, &state
->stack_info
,
683 * The caller can provide the address of the first frame directly
684 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
685 * to start unwinding at. Skip ahead until we reach it.
688 /* When starting from regs, skip the regs frame: */
690 unwind_next_frame(state
);
694 /* Otherwise, skip ahead to the user-specified starting frame: */
695 while (!unwind_done(state
) &&
696 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
697 state
->sp
< (unsigned long)first_frame
))
698 unwind_next_frame(state
);
705 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
707 EXPORT_SYMBOL_GPL(__unwind_start
);