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
)
77 static struct orc_entry
*orc_find(unsigned long ip
)
82 /* For non-init vmlinux addresses, use the fast lookup table: */
83 if (ip
>= LOOKUP_START_IP
&& ip
< LOOKUP_STOP_IP
) {
84 unsigned int idx
, start
, stop
;
86 idx
= (ip
- LOOKUP_START_IP
) / LOOKUP_BLOCK_SIZE
;
88 if (unlikely((idx
>= lookup_num_blocks
-1))) {
89 orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
90 idx
, lookup_num_blocks
, (void *)ip
);
94 start
= orc_lookup
[idx
];
95 stop
= orc_lookup
[idx
+ 1] + 1;
97 if (unlikely((__start_orc_unwind
+ start
>= __stop_orc_unwind
) ||
98 (__start_orc_unwind
+ stop
> __stop_orc_unwind
))) {
99 orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
100 idx
, lookup_num_blocks
, start
, stop
, (void *)ip
);
104 return __orc_find(__start_orc_unwind_ip
+ start
,
105 __start_orc_unwind
+ start
, stop
- start
, ip
);
108 /* vmlinux .init slow lookup: */
109 if (ip
>= (unsigned long)_sinittext
&& ip
< (unsigned long)_einittext
)
110 return __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
111 __stop_orc_unwind_ip
- __start_orc_unwind_ip
, ip
);
114 return orc_module_find(ip
);
117 static void orc_sort_swap(void *_a
, void *_b
, int size
)
119 struct orc_entry
*orc_a
, *orc_b
;
120 struct orc_entry orc_tmp
;
121 int *a
= _a
, *b
= _b
, tmp
;
124 /* Swap the .orc_unwind_ip entries: */
129 /* Swap the corresponding .orc_unwind entries: */
130 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
131 orc_b
= cur_orc_table
+ (b
- cur_orc_ip_table
);
137 static int orc_sort_cmp(const void *_a
, const void *_b
)
139 struct orc_entry
*orc_a
;
140 const int *a
= _a
, *b
= _b
;
141 unsigned long a_val
= orc_ip(a
);
142 unsigned long b_val
= orc_ip(b
);
150 * The "weak" section terminator entries need to always be on the left
151 * to ensure the lookup code skips them in favor of real entries.
152 * These terminator entries exist to handle any gaps created by
153 * whitelisted .o files which didn't get objtool generation.
155 orc_a
= cur_orc_table
+ (a
- cur_orc_ip_table
);
156 return orc_a
->sp_reg
== ORC_REG_UNDEFINED
? -1 : 1;
159 #ifdef CONFIG_MODULES
160 void unwind_module_init(struct module
*mod
, void *_orc_ip
, size_t orc_ip_size
,
161 void *_orc
, size_t orc_size
)
163 int *orc_ip
= _orc_ip
;
164 struct orc_entry
*orc
= _orc
;
165 unsigned int num_entries
= orc_ip_size
/ sizeof(int);
167 WARN_ON_ONCE(orc_ip_size
% sizeof(int) != 0 ||
168 orc_size
% sizeof(*orc
) != 0 ||
169 num_entries
!= orc_size
/ sizeof(*orc
));
172 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
173 * associate an .orc_unwind_ip table entry with its corresponding
174 * .orc_unwind entry so they can both be swapped.
176 mutex_lock(&sort_mutex
);
177 cur_orc_ip_table
= orc_ip
;
179 sort(orc_ip
, num_entries
, sizeof(int), orc_sort_cmp
, orc_sort_swap
);
180 mutex_unlock(&sort_mutex
);
182 mod
->arch
.orc_unwind_ip
= orc_ip
;
183 mod
->arch
.orc_unwind
= orc
;
184 mod
->arch
.num_orcs
= num_entries
;
188 void __init
unwind_init(void)
190 size_t orc_ip_size
= (void *)__stop_orc_unwind_ip
- (void *)__start_orc_unwind_ip
;
191 size_t orc_size
= (void *)__stop_orc_unwind
- (void *)__start_orc_unwind
;
192 size_t num_entries
= orc_ip_size
/ sizeof(int);
193 struct orc_entry
*orc
;
196 if (!num_entries
|| orc_ip_size
% sizeof(int) != 0 ||
197 orc_size
% sizeof(struct orc_entry
) != 0 ||
198 num_entries
!= orc_size
/ sizeof(struct orc_entry
)) {
199 orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
203 /* Sort the .orc_unwind and .orc_unwind_ip tables: */
204 sort(__start_orc_unwind_ip
, num_entries
, sizeof(int), orc_sort_cmp
,
207 /* Initialize the fast lookup table: */
208 lookup_num_blocks
= orc_lookup_end
- orc_lookup
;
209 for (i
= 0; i
< lookup_num_blocks
-1; i
++) {
210 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
,
212 LOOKUP_START_IP
+ (LOOKUP_BLOCK_SIZE
* i
));
214 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
218 orc_lookup
[i
] = orc
- __start_orc_unwind
;
221 /* Initialize the ending block: */
222 orc
= __orc_find(__start_orc_unwind_ip
, __start_orc_unwind
, num_entries
,
225 orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
228 orc_lookup
[lookup_num_blocks
-1] = orc
- __start_orc_unwind
;
233 unsigned long unwind_get_return_address(struct unwind_state
*state
)
235 if (unwind_done(state
))
238 return __kernel_text_address(state
->ip
) ? state
->ip
: 0;
240 EXPORT_SYMBOL_GPL(unwind_get_return_address
);
242 unsigned long *unwind_get_return_address_ptr(struct unwind_state
*state
)
244 if (unwind_done(state
))
248 return &state
->regs
->ip
;
251 return (unsigned long *)state
->sp
- 1;
256 static bool stack_access_ok(struct unwind_state
*state
, unsigned long _addr
,
259 struct stack_info
*info
= &state
->stack_info
;
260 void *addr
= (void *)_addr
;
262 if (!on_stack(info
, addr
, len
) &&
263 (get_stack_info(addr
, state
->task
, info
, &state
->stack_mask
)))
269 static bool deref_stack_reg(struct unwind_state
*state
, unsigned long addr
,
272 if (!stack_access_ok(state
, addr
, sizeof(long)))
275 *val
= READ_ONCE_NOCHECK(*(unsigned long *)addr
);
279 static bool deref_stack_regs(struct unwind_state
*state
, unsigned long addr
,
280 unsigned long *ip
, unsigned long *sp
)
282 struct pt_regs
*regs
= (struct pt_regs
*)addr
;
284 /* x86-32 support will be more complicated due to the ®s->sp hack */
285 BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32
));
287 if (!stack_access_ok(state
, addr
, sizeof(struct pt_regs
)))
295 static bool deref_stack_iret_regs(struct unwind_state
*state
, unsigned long addr
,
296 unsigned long *ip
, unsigned long *sp
)
298 struct pt_regs
*regs
= (void *)addr
- IRET_FRAME_OFFSET
;
300 if (!stack_access_ok(state
, addr
, IRET_FRAME_SIZE
))
308 bool unwind_next_frame(struct unwind_state
*state
)
310 unsigned long ip_p
, sp
, orig_ip
, prev_sp
= state
->sp
;
311 enum stack_type prev_type
= state
->stack_info
.type
;
312 struct orc_entry
*orc
;
313 bool indirect
= false;
315 if (unwind_done(state
))
318 /* Don't let modules unload while we're reading their ORC data. */
321 /* Have we reached the end? */
322 if (state
->regs
&& user_mode(state
->regs
))
326 * Find the orc_entry associated with the text address.
328 * Decrement call return addresses by one so they work for sibling
329 * calls and calls to noreturn functions.
331 orc
= orc_find(state
->signal
? state
->ip
: state
->ip
- 1);
332 if (!orc
|| orc
->sp_reg
== ORC_REG_UNDEFINED
)
336 /* Find the previous frame's stack: */
337 switch (orc
->sp_reg
) {
339 sp
= state
->sp
+ orc
->sp_offset
;
343 sp
= state
->bp
+ orc
->sp_offset
;
346 case ORC_REG_SP_INDIRECT
:
347 sp
= state
->sp
+ orc
->sp_offset
;
351 case ORC_REG_BP_INDIRECT
:
352 sp
= state
->bp
+ orc
->sp_offset
;
357 if (!state
->regs
|| !state
->full_regs
) {
358 orc_warn("missing regs for base reg R10 at ip %pB\n",
362 sp
= state
->regs
->r10
;
366 if (!state
->regs
|| !state
->full_regs
) {
367 orc_warn("missing regs for base reg R13 at ip %pB\n",
371 sp
= state
->regs
->r13
;
375 if (!state
->regs
|| !state
->full_regs
) {
376 orc_warn("missing regs for base reg DI at ip %pB\n",
380 sp
= state
->regs
->di
;
384 if (!state
->regs
|| !state
->full_regs
) {
385 orc_warn("missing regs for base reg DX at ip %pB\n",
389 sp
= state
->regs
->dx
;
393 orc_warn("unknown SP base reg %d for ip %pB\n",
394 orc
->sp_reg
, (void *)state
->ip
);
399 if (!deref_stack_reg(state
, sp
, &sp
))
403 /* Find IP, SP and possibly regs: */
406 ip_p
= sp
- sizeof(long);
408 if (!deref_stack_reg(state
, ip_p
, &state
->ip
))
411 state
->ip
= ftrace_graph_ret_addr(state
->task
, &state
->graph_idx
,
412 state
->ip
, (void *)ip_p
);
416 state
->signal
= false;
420 if (!deref_stack_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
421 orc_warn("can't dereference registers at %p for ip %pB\n",
422 (void *)sp
, (void *)orig_ip
);
426 state
->regs
= (struct pt_regs
*)sp
;
427 state
->full_regs
= true;
428 state
->signal
= true;
431 case ORC_TYPE_REGS_IRET
:
432 if (!deref_stack_iret_regs(state
, sp
, &state
->ip
, &state
->sp
)) {
433 orc_warn("can't dereference iret registers at %p for ip %pB\n",
434 (void *)sp
, (void *)orig_ip
);
438 state
->regs
= (void *)sp
- IRET_FRAME_OFFSET
;
439 state
->full_regs
= false;
440 state
->signal
= true;
444 orc_warn("unknown .orc_unwind entry type %d for ip %pB\n",
445 orc
->type
, (void *)orig_ip
);
450 switch (orc
->bp_reg
) {
451 case ORC_REG_UNDEFINED
:
452 if (state
->regs
&& state
->full_regs
)
453 state
->bp
= state
->regs
->bp
;
456 case ORC_REG_PREV_SP
:
457 if (!deref_stack_reg(state
, sp
+ orc
->bp_offset
, &state
->bp
))
462 if (!deref_stack_reg(state
, state
->bp
+ orc
->bp_offset
, &state
->bp
))
467 orc_warn("unknown BP base reg %d for ip %pB\n",
468 orc
->bp_reg
, (void *)orig_ip
);
472 /* Prevent a recursive loop due to bad ORC data: */
473 if (state
->stack_info
.type
== prev_type
&&
474 on_stack(&state
->stack_info
, (void *)state
->sp
, sizeof(long)) &&
475 state
->sp
<= prev_sp
) {
476 orc_warn("stack going in the wrong direction? ip=%pB\n",
486 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
489 EXPORT_SYMBOL_GPL(unwind_next_frame
);
491 void __unwind_start(struct unwind_state
*state
, struct task_struct
*task
,
492 struct pt_regs
*regs
, unsigned long *first_frame
)
494 memset(state
, 0, sizeof(*state
));
498 * Refuse to unwind the stack of a task while it's executing on another
499 * CPU. This check is racy, but that's ok: the unwinder has other
500 * checks to prevent it from going off the rails.
502 if (task_on_another_cpu(task
))
509 state
->ip
= regs
->ip
;
510 state
->sp
= kernel_stack_pointer(regs
);
511 state
->bp
= regs
->bp
;
513 state
->full_regs
= true;
514 state
->signal
= true;
516 } else if (task
== current
) {
517 asm volatile("lea (%%rip), %0\n\t"
520 : "=r" (state
->ip
), "=r" (state
->sp
),
524 struct inactive_task_frame
*frame
= (void *)task
->thread
.sp
;
526 state
->sp
= task
->thread
.sp
;
527 state
->bp
= READ_ONCE_NOCHECK(frame
->bp
);
528 state
->ip
= READ_ONCE_NOCHECK(frame
->ret_addr
);
531 if (get_stack_info((unsigned long *)state
->sp
, state
->task
,
532 &state
->stack_info
, &state
->stack_mask
)) {
534 * We weren't on a valid stack. It's possible that
535 * we overflowed a valid stack into a guard page.
536 * See if the next page up is valid so that we can
537 * generate some kind of backtrace if this happens.
539 void *next_page
= (void *)PAGE_ALIGN((unsigned long)state
->sp
);
540 if (get_stack_info(next_page
, state
->task
, &state
->stack_info
,
546 * The caller can provide the address of the first frame directly
547 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
548 * to start unwinding at. Skip ahead until we reach it.
551 /* When starting from regs, skip the regs frame: */
553 unwind_next_frame(state
);
557 /* Otherwise, skip ahead to the user-specified starting frame: */
558 while (!unwind_done(state
) &&
559 (!on_stack(&state
->stack_info
, first_frame
, sizeof(long)) ||
560 state
->sp
<= (unsigned long)first_frame
))
561 unwind_next_frame(state
);
566 state
->stack_info
.type
= STACK_TYPE_UNKNOWN
;
569 EXPORT_SYMBOL_GPL(__unwind_start
);