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>
9 #define orc_warn(fmt, ...) \
10 printk_deferred_once(KERN_WARNING pr_fmt("WARNING: " fmt), ##__VA_ARGS__)
12 extern int __start_orc_unwind_ip
[];
13 extern int __stop_orc_unwind_ip
[];
14 extern struct orc_entry __start_orc_unwind
[];
15 extern struct orc_entry __stop_orc_unwind
[];
17 static DEFINE_MUTEX(sort_mutex
);
18 int *cur_orc_ip_table
= __start_orc_unwind_ip
;
19 struct orc_entry
*cur_orc_table
= __start_orc_unwind
;
21 unsigned int lookup_num_blocks
;
24 static inline unsigned long orc_ip(const int *ip
)
26 return (unsigned long)ip
+ *ip
;
29 static struct orc_entry
*__orc_find(int *ip_table
, struct orc_entry
*u_table
,
30 unsigned int num_entries
, unsigned long ip
)
32 int *first
= ip_table
;
33 int *last
= ip_table
+ num_entries
- 1;
34 int *mid
= first
, *found
= first
;
40 * Do a binary range search to find the rightmost duplicate of a given
41 * starting address. Some entries are section terminators which are
42 * "weak" entries for ensuring there are no gaps. They should be
43 * ignored when they conflict with a real entry.
45 while (first
<= last
) {
46 mid
= first
+ ((last
- first
) / 2);
48 if (orc_ip(mid
) <= ip
) {
55 return u_table
+ (found
- ip_table
);
59 static struct orc_entry
*orc_module_find(unsigned long ip
)
63 mod
= __module_address(ip
);
64 if (!mod
|| !mod
->arch
.orc_unwind
|| !mod
->arch
.orc_unwind_ip
)
66 return __orc_find(mod
->arch
.orc_unwind_ip
, mod
->arch
.orc_unwind
,
67 mod
->arch
.num_orcs
, ip
);
70 static struct orc_entry
*orc_module_find(unsigned long ip
)
76 #ifdef CONFIG_DYNAMIC_FTRACE
77 static struct orc_entry
*orc_find(unsigned long ip
);
80 * Ftrace dynamic trampolines do not have orc entries of their own.
81 * But they are copies of the ftrace entries that are static and
82 * defined in ftrace_*.S, which do have orc entries.
84 * If the undwinder comes across a ftrace trampoline, then find the
85 * ftrace function that was used to create it, and use that ftrace
86 * function's orc entrie, as the placement of the return code in
87 * the stack will be identical.
89 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
91 struct ftrace_ops
*ops
;
94 ops
= ftrace_ops_trampoline(ip
);
98 if (ops
->flags
& FTRACE_OPS_FL_SAVE_REGS
)
99 caller
= (unsigned long)ftrace_regs_call
;
101 caller
= (unsigned long)ftrace_call
;
103 /* Prevent unlikely recursion */
107 return orc_find(caller
);
110 static struct orc_entry
*orc_ftrace_find(unsigned long ip
)
117 * If we crash with IP==0, the last successfully executed instruction
118 * was probably an indirect function call with a NULL function pointer,
119 * and we don't have unwind information for NULL.
120 * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
121 * pointer into its parent and then continue normally from there.
123 static struct orc_entry null_orc_entry
= {
124 .sp_offset
= sizeof(long),
125 .sp_reg
= ORC_REG_SP
,
126 .bp_reg
= ORC_REG_UNDEFINED
,
127 .type
= ORC_TYPE_CALL
130 static struct orc_entry
*orc_find(unsigned long ip
)
132 static struct orc_entry
*orc
;
138 return &null_orc_entry
;
140 /* For non-init vmlinux addresses, use the fast lookup table: */
141 if (ip
>= LOOKUP_START_IP
&& ip
< LOOKUP_STOP_IP
) {
142 unsigned int idx
, start
, stop
;
144 idx
= (ip
- LOOKUP_START_IP
) / LOOKUP_BLOCK_SIZE
;
146 if (unlikely((idx
>= lookup_num_blocks
-1))) {
147 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
148 idx
, lookup_num_blocks
, (void *)ip
);
152 start
= orc_lookup
[idx
];
153 stop
= orc_lookup
[idx
+ 1] + 1;
155 if (unlikely((__start_orc_unwind
+ start
>= __stop_orc_unwind
) ||
156 (__start_orc_unwind
+ stop
> __stop_orc_unwind
))) {
157 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
158 idx
, lookup_num_blocks
, start
, stop
, (void *)ip
);
162 return __orc_find(__start_orc_unwind_ip
+ start
,
163 __start_orc_unwind
+ start
, stop
- start
, ip
);
166 /* vmlinux .init slow lookup: */
167 if (init_kernel_text(ip
))
168 return __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
169 __stop_orc_unwind_ip
- __start_orc_unwind_ip
, ip
);
172 orc
= orc_module_find(ip
);
176 return orc_ftrace_find(ip
);
179 static void orc_sort_swap(void *_a
, void *_b
, int size
)
181 struct orc_entry
*orc_a
, *orc_b
;
182 struct orc_entry orc_tmp
;
183 int *a
= _a
, *b
= _b
, tmp
;
186 /* Swap the .orc_unwind_ip entries: */
191 /* Swap the corresponding .orc_unwind entries: */
192 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
193 orc_b
= cur_orc_table
+ (b
- cur_orc_ip_table
);
199 static int orc_sort_cmp(const void *_a
, const void *_b
)
201 struct orc_entry
*orc_a
;
202 const int *a
= _a
, *b
= _b
;
203 unsigned long a_val
= orc_ip(a
);
204 unsigned long b_val
= orc_ip(b
);
212 * The "weak" section terminator entries need to always be on the left
213 * to ensure the lookup code skips them in favor of real entries.
214 * These terminator entries exist to handle any gaps created by
215 * whitelisted .o files which didn't get objtool generation.
217 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
218 return orc_a
->sp_reg
== ORC_REG_UNDEFINED
&& !orc_a
->end
? -1 : 1;
221 #ifdef CONFIG_MODULES
222 void unwind_module_init(struct module
*mod
, void *_orc_ip
, size_t orc_ip_size
,
223 void *_orc
, size_t orc_size
)
225 int *orc_ip
= _orc_ip
;
226 struct orc_entry
*orc
= _orc
;
227 unsigned int num_entries
= orc_ip_size
/ sizeof(int);
229 WARN_ON_ONCE(orc_ip_size
% sizeof(int) != 0 ||
230 orc_size
% sizeof(*orc
) != 0 ||
231 num_entries
!= orc_size
/ sizeof(*orc
));
234 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
235 * associate an .orc_unwind_ip table entry with its corresponding
236 * .orc_unwind entry so they can both be swapped.
238 mutex_lock(&sort_mutex
);
239 cur_orc_ip_table
= orc_ip
;
241 sort(orc_ip
, num_entries
, sizeof(int), orc_sort_cmp
, orc_sort_swap
);
242 mutex_unlock(&sort_mutex
);
244 mod
->arch
.orc_unwind_ip
= orc_ip
;
245 mod
->arch
.orc_unwind
= orc
;
246 mod
->arch
.num_orcs
= num_entries
;
250 void __init
unwind_init(void)
252 size_t orc_ip_size
= (void *)__stop_orc_unwind_ip
- (void *)__start_orc_unwind_ip
;
253 size_t orc_size
= (void *)__stop_orc_unwind
- (void *)__start_orc_unwind
;
254 size_t num_entries
= orc_ip_size
/ sizeof(int);
255 struct orc_entry
*orc
;
258 if (!num_entries
|| orc_ip_size
% sizeof(int) != 0 ||
259 orc_size
% sizeof(struct orc_entry
) != 0 ||
260 num_entries
!= orc_size
/ sizeof(struct orc_entry
)) {
261 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
265 /* Sort the .orc_unwind and .orc_unwind_ip tables: */
266 sort(__start_orc_unwind_ip
, num_entries
, sizeof(int), orc_sort_cmp
,
269 /* Initialize the fast lookup table: */
270 lookup_num_blocks
= orc_lookup_end
- orc_lookup
;
271 for (i
= 0; i
< lookup_num_blocks
-1; i
++) {
272 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
274 LOOKUP_START_IP
+ (LOOKUP_BLOCK_SIZE
* i
));
276 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
280 orc_lookup
[i
] = orc
- __start_orc_unwind
;
283 /* Initialize the ending block: */
284 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
, num_entries
,
287 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
290 orc_lookup
[lookup_num_blocks
-1] = orc
- __start_orc_unwind
;
295 unsigned long unwind_get_return_address(struct unwind_state
*state
)
297 if (unwind_done(state
))
300 return __kernel_text_address(state
->ip
) ? state
->ip
: 0;
302 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
304 unsigned long *unwind_get_return_address_ptr(struct unwind_state
*state
)
306 if (unwind_done(state
))
310 return &state
->regs
->ip
;
313 return (unsigned long *)state
->sp
- 1;
318 static bool stack_access_ok(struct unwind_state
*state
, unsigned long _addr
,
321 struct stack_info
*info
= &state
->stack_info
;
322 void *addr
= (void *)_addr
;
324 if (!on_stack(info
, addr
, len
) &&
325 (get_stack_info(addr
, state
->task
, info
, &state
->stack_mask
)))
331 static bool deref_stack_reg(struct unwind_state
*state
, unsigned long addr
,
334 if (!stack_access_ok(state
, addr
, sizeof(long)))
337 *val
= READ_ONCE_NOCHECK(*(unsigned long *)addr
);
341 static bool deref_stack_regs(struct unwind_state
*state
, unsigned long addr
,
342 unsigned long *ip
, unsigned long *sp
)
344 struct pt_regs
*regs
= (struct pt_regs
*)addr
;
346 /* x86-32 support will be more complicated due to the ®s->sp hack */
347 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32
));
349 if (!stack_access_ok(state
, addr
, sizeof(struct pt_regs
)))
357 static bool deref_stack_iret_regs(struct unwind_state
*state
, unsigned long addr
,
358 unsigned long *ip
, unsigned long *sp
)
360 struct pt_regs
*regs
= (void *)addr
- IRET_FRAME_OFFSET
;
362 if (!stack_access_ok(state
, addr
, IRET_FRAME_SIZE
))
370 bool unwind_next_frame(struct unwind_state
*state
)
372 unsigned long ip_p
, sp
, orig_ip
= state
->ip
, prev_sp
= state
->sp
;
373 enum stack_type prev_type
= state
->stack_info
.type
;
374 struct orc_entry
*orc
;
375 bool indirect
= false;
377 if (unwind_done(state
))
380 /* Don't let modules unload while we're reading their ORC data. */
383 /* End-of-stack check for user tasks: */
384 if (state
->regs
&& user_mode(state
->regs
))
388 * Find the orc_entry associated with the text address.
390 * Decrement call return addresses by one so they work for sibling
391 * calls and calls to noreturn functions.
393 orc
= orc_find(state
->signal
? state
->ip
: state
->ip
- 1);
397 /* End-of-stack check for kernel threads: */
398 if (orc
->sp_reg
== ORC_REG_UNDEFINED
) {
405 /* Find the previous frame's stack: */
406 switch (orc
->sp_reg
) {
408 sp
= state
->sp
+ orc
->sp_offset
;
412 sp
= state
->bp
+ orc
->sp_offset
;
415 case ORC_REG_SP_INDIRECT
:
416 sp
= state
->sp
+ orc
->sp_offset
;
420 case ORC_REG_BP_INDIRECT
:
421 sp
= state
->bp
+ orc
->sp_offset
;
426 if (!state
->regs
|| !state
->full_regs
) {
427 orc_warn("missing regs for base reg R10 at ip %pB\n",
431 sp
= state
->regs
->r10
;
435 if (!state
->regs
|| !state
->full_regs
) {
436 orc_warn("missing regs for base reg R13 at ip %pB\n",
440 sp
= state
->regs
->r13
;
444 if (!state
->regs
|| !state
->full_regs
) {
445 orc_warn("missing regs for base reg DI at ip %pB\n",
449 sp
= state
->regs
->di
;
453 if (!state
->regs
|| !state
->full_regs
) {
454 orc_warn("missing regs for base reg DX at ip %pB\n",
458 sp
= state
->regs
->dx
;
462 orc_warn("unknown SP base reg %d for ip %pB\n",
463 orc
->sp_reg
, (void *)state
->ip
);
468 if (!deref_stack_reg(state
, sp
, &sp
))
472 /* Find IP, SP and possibly regs: */
475 ip_p
= sp
- sizeof(long);
477 if (!deref_stack_reg(state
, ip_p
, &state
->ip
))
480 state
->ip
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
,
481 state
->ip
, (void *)ip_p
);
485 state
->signal
= false;
489 if (!deref_stack_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
490 orc_warn("can't dereference registers at %p for ip %pB\n",
491 (void *)sp
, (void *)orig_ip
);
495 state
->regs
= (struct pt_regs
*)sp
;
496 state
->full_regs
= true;
497 state
->signal
= true;
500 case ORC_TYPE_REGS_IRET
:
501 if (!deref_stack_iret_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
502 orc_warn("can't dereference iret registers at %p for ip %pB\n",
503 (void *)sp
, (void *)orig_ip
);
507 state
->regs
= (void *)sp
- IRET_FRAME_OFFSET
;
508 state
->full_regs
= false;
509 state
->signal
= true;
513 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
514 orc
->type
, (void *)orig_ip
);
519 switch (orc
->bp_reg
) {
520 case ORC_REG_UNDEFINED
:
521 if (state
->regs
&& state
->full_regs
)
522 state
->bp
= state
->regs
->bp
;
525 case ORC_REG_PREV_SP
:
526 if (!deref_stack_reg(state
, sp
+ orc
->bp_offset
, &state
->bp
))
531 if (!deref_stack_reg(state
, state
->bp
+ orc
->bp_offset
, &state
->bp
))
536 orc_warn("unknown BP base reg %d for ip %pB\n",
537 orc
->bp_reg
, (void *)orig_ip
);
541 /* Prevent a recursive loop due to bad ORC data: */
542 if (state
->stack_info
.type
== prev_type
&&
543 on_stack(&state
->stack_info
, (void *)state
->sp
, sizeof(long)) &&
544 state
->sp
<= prev_sp
) {
545 orc_warn("stack going in the wrong direction? ip=%pB\n",
558 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
561 EXPORT_SYMBOL_GPL(unwind_next_frame
);
563 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
564 struct pt_regs
*regs
, unsigned long *first_frame
)
566 memset(state
, 0, sizeof(*state
));
570 * Refuse to unwind the stack of a task while it's executing on another
571 * CPU. This check is racy, but that's ok: the unwinder has other
572 * checks to prevent it from going off the rails.
574 if (task_on_another_cpu(task
))
581 state
->ip
= regs
->ip
;
582 state
->sp
= kernel_stack_pointer(regs
);
583 state
->bp
= regs
->bp
;
585 state
->full_regs
= true;
586 state
->signal
= true;
588 } else if (task
== current
) {
589 asm volatile("lea (%%rip), %0\n\t"
592 : "=r" (state
->ip
), "=r" (state
->sp
),
596 struct inactive_task_frame
*frame
= (void *)task
->thread
.sp
;
598 state
->sp
= task
->thread
.sp
;
599 state
->bp
= READ_ONCE_NOCHECK(frame
->bp
);
600 state
->ip
= READ_ONCE_NOCHECK(frame
->ret_addr
);
603 if (get_stack_info((unsigned long *)state
->sp
, state
->task
,
604 &state
->stack_info
, &state
->stack_mask
)) {
606 * We weren't on a valid stack. It's possible that
607 * we overflowed a valid stack into a guard page.
608 * See if the next page up is valid so that we can
609 * generate some kind of backtrace if this happens.
611 void *next_page
= (void *)PAGE_ALIGN((unsigned long)state
->sp
);
612 if (get_stack_info(next_page
, state
->task
, &state
->stack_info
,
618 * The caller can provide the address of the first frame directly
619 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
620 * to start unwinding at. Skip ahead until we reach it.
623 /* When starting from regs, skip the regs frame: */
625 unwind_next_frame(state
);
629 /* Otherwise, skip ahead to the user-specified starting frame: */
630 while (!unwind_done(state
) &&
631 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
632 state
->sp
<= (unsigned long)first_frame
))
633 unwind_next_frame(state
);
638 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
641 EXPORT_SYMBOL_GPL(__unwind_start
);