xref: /linux/arch/x86/kernel/unwind_orc.c (revision a266ef69b890f099069cf51bb40572611c435a54)
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 
11 #define orc_warn(fmt, ...) \
12 	printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
13 
14 #define orc_warn_current(args...)					\
15 ({									\
16 	if (state->task == current && !state->error)			\
17 		orc_warn(args);						\
18 })
19 
20 extern int __start_orc_unwind_ip[];
21 extern int __stop_orc_unwind_ip[];
22 extern struct orc_entry __start_orc_unwind[];
23 extern struct orc_entry __stop_orc_unwind[];
24 
25 static bool orc_init __ro_after_init;
26 static unsigned int lookup_num_blocks __ro_after_init;
27 
28 static inline unsigned long orc_ip(const int *ip)
29 {
30 	return (unsigned long)ip + *ip;
31 }
32 
33 static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
34 				    unsigned int num_entries, unsigned long ip)
35 {
36 	int *first = ip_table;
37 	int *last = ip_table + num_entries - 1;
38 	int *mid = first, *found = first;
39 
40 	if (!num_entries)
41 		return NULL;
42 
43 	/*
44 	 * Do a binary range search to find the rightmost duplicate of a given
45 	 * starting address.  Some entries are section terminators which are
46 	 * "weak" entries for ensuring there are no gaps.  They should be
47 	 * ignored when they conflict with a real entry.
48 	 */
49 	while (first <= last) {
50 		mid = first + ((last - first) / 2);
51 
52 		if (orc_ip(mid) <= ip) {
53 			found = mid;
54 			first = mid + 1;
55 		} else
56 			last = mid - 1;
57 	}
58 
59 	return u_table + (found - ip_table);
60 }
61 
62 #ifdef CONFIG_MODULES
63 static struct orc_entry *orc_module_find(unsigned long ip)
64 {
65 	struct module *mod;
66 
67 	mod = __module_address(ip);
68 	if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
69 		return NULL;
70 	return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
71 			  mod->arch.num_orcs, ip);
72 }
73 #else
74 static struct orc_entry *orc_module_find(unsigned long ip)
75 {
76 	return NULL;
77 }
78 #endif
79 
80 #ifdef CONFIG_DYNAMIC_FTRACE
81 static struct orc_entry *orc_find(unsigned long ip);
82 
83 /*
84  * Ftrace dynamic trampolines do not have orc entries of their own.
85  * But they are copies of the ftrace entries that are static and
86  * defined in ftrace_*.S, which do have orc entries.
87  *
88  * If the unwinder comes across a ftrace trampoline, then find the
89  * ftrace function that was used to create it, and use that ftrace
90  * function's orc entry, as the placement of the return code in
91  * the stack will be identical.
92  */
93 static struct orc_entry *orc_ftrace_find(unsigned long ip)
94 {
95 	struct ftrace_ops *ops;
96 	unsigned long tramp_addr, offset;
97 
98 	ops = ftrace_ops_trampoline(ip);
99 	if (!ops)
100 		return NULL;
101 
102 	/* Set tramp_addr to the start of the code copied by the trampoline */
103 	if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
104 		tramp_addr = (unsigned long)ftrace_regs_caller;
105 	else
106 		tramp_addr = (unsigned long)ftrace_caller;
107 
108 	/* Now place tramp_addr to the location within the trampoline ip is at */
109 	offset = ip - ops->trampoline;
110 	tramp_addr += offset;
111 
112 	/* Prevent unlikely recursion */
113 	if (ip == tramp_addr)
114 		return NULL;
115 
116 	return orc_find(tramp_addr);
117 }
118 #else
119 static struct orc_entry *orc_ftrace_find(unsigned long ip)
120 {
121 	return NULL;
122 }
123 #endif
124 
125 /*
126  * If we crash with IP==0, the last successfully executed instruction
127  * was probably an indirect function call with a NULL function pointer,
128  * and we don't have unwind information for NULL.
129  * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
130  * pointer into its parent and then continue normally from there.
131  */
132 static struct orc_entry null_orc_entry = {
133 	.sp_offset = sizeof(long),
134 	.sp_reg = ORC_REG_SP,
135 	.bp_reg = ORC_REG_UNDEFINED,
136 	.type = UNWIND_HINT_TYPE_CALL
137 };
138 
139 #ifdef CONFIG_CALL_THUNKS
140 static struct orc_entry *orc_callthunk_find(unsigned long ip)
141 {
142 	if (!is_callthunk((void *)ip))
143 		return NULL;
144 
145 	return &null_orc_entry;
146 }
147 #else
148 static struct orc_entry *orc_callthunk_find(unsigned long ip)
149 {
150 	return NULL;
151 }
152 #endif
153 
154 /* Fake frame pointer entry -- used as a fallback for generated code */
155 static struct orc_entry orc_fp_entry = {
156 	.type		= UNWIND_HINT_TYPE_CALL,
157 	.sp_reg		= ORC_REG_BP,
158 	.sp_offset	= 16,
159 	.bp_reg		= ORC_REG_PREV_SP,
160 	.bp_offset	= -16,
161 	.end		= 0,
162 };
163 
164 static struct orc_entry *orc_find(unsigned long ip)
165 {
166 	static struct orc_entry *orc;
167 
168 	if (ip == 0)
169 		return &null_orc_entry;
170 
171 	/* For non-init vmlinux addresses, use the fast lookup table: */
172 	if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
173 		unsigned int idx, start, stop;
174 
175 		idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
176 
177 		if (unlikely((idx >= lookup_num_blocks-1))) {
178 			orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
179 				 idx, lookup_num_blocks, (void *)ip);
180 			return NULL;
181 		}
182 
183 		start = orc_lookup[idx];
184 		stop = orc_lookup[idx + 1] + 1;
185 
186 		if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
187 			     (__start_orc_unwind + stop > __stop_orc_unwind))) {
188 			orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
189 				 idx, lookup_num_blocks, start, stop, (void *)ip);
190 			return NULL;
191 		}
192 
193 		return __orc_find(__start_orc_unwind_ip + start,
194 				  __start_orc_unwind + start, stop - start, ip);
195 	}
196 
197 	/* vmlinux .init slow lookup: */
198 	if (is_kernel_inittext(ip))
199 		return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
200 				  __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
201 
202 	/* Module lookup: */
203 	orc = orc_module_find(ip);
204 	if (orc)
205 		return orc;
206 
207 	orc =  orc_ftrace_find(ip);
208 	if (orc)
209 		return orc;
210 
211 	return orc_callthunk_find(ip);
212 }
213 
214 #ifdef CONFIG_MODULES
215 
216 static DEFINE_MUTEX(sort_mutex);
217 static int *cur_orc_ip_table = __start_orc_unwind_ip;
218 static struct orc_entry *cur_orc_table = __start_orc_unwind;
219 
220 static void orc_sort_swap(void *_a, void *_b, int size)
221 {
222 	struct orc_entry *orc_a, *orc_b;
223 	struct orc_entry orc_tmp;
224 	int *a = _a, *b = _b, tmp;
225 	int delta = _b - _a;
226 
227 	/* Swap the .orc_unwind_ip entries: */
228 	tmp = *a;
229 	*a = *b + delta;
230 	*b = tmp - delta;
231 
232 	/* Swap the corresponding .orc_unwind entries: */
233 	orc_a = cur_orc_table + (a - cur_orc_ip_table);
234 	orc_b = cur_orc_table + (b - cur_orc_ip_table);
235 	orc_tmp = *orc_a;
236 	*orc_a = *orc_b;
237 	*orc_b = orc_tmp;
238 }
239 
240 static int orc_sort_cmp(const void *_a, const void *_b)
241 {
242 	struct orc_entry *orc_a;
243 	const int *a = _a, *b = _b;
244 	unsigned long a_val = orc_ip(a);
245 	unsigned long b_val = orc_ip(b);
246 
247 	if (a_val > b_val)
248 		return 1;
249 	if (a_val < b_val)
250 		return -1;
251 
252 	/*
253 	 * The "weak" section terminator entries need to always be on the left
254 	 * to ensure the lookup code skips them in favor of real entries.
255 	 * These terminator entries exist to handle any gaps created by
256 	 * whitelisted .o files which didn't get objtool generation.
257 	 */
258 	orc_a = cur_orc_table + (a - cur_orc_ip_table);
259 	return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
260 }
261 
262 void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
263 			void *_orc, size_t orc_size)
264 {
265 	int *orc_ip = _orc_ip;
266 	struct orc_entry *orc = _orc;
267 	unsigned int num_entries = orc_ip_size / sizeof(int);
268 
269 	WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
270 		     orc_size % sizeof(*orc) != 0 ||
271 		     num_entries != orc_size / sizeof(*orc));
272 
273 	/*
274 	 * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
275 	 * associate an .orc_unwind_ip table entry with its corresponding
276 	 * .orc_unwind entry so they can both be swapped.
277 	 */
278 	mutex_lock(&sort_mutex);
279 	cur_orc_ip_table = orc_ip;
280 	cur_orc_table = orc;
281 	sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
282 	mutex_unlock(&sort_mutex);
283 
284 	mod->arch.orc_unwind_ip = orc_ip;
285 	mod->arch.orc_unwind = orc;
286 	mod->arch.num_orcs = num_entries;
287 }
288 #endif
289 
290 void __init unwind_init(void)
291 {
292 	size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
293 	size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
294 	size_t num_entries = orc_ip_size / sizeof(int);
295 	struct orc_entry *orc;
296 	int i;
297 
298 	if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
299 	    orc_size % sizeof(struct orc_entry) != 0 ||
300 	    num_entries != orc_size / sizeof(struct orc_entry)) {
301 		orc_warn("WARNING: Bad or missing .orc_unwind table.  Disabling unwinder.\n");
302 		return;
303 	}
304 
305 	/*
306 	 * Note, the orc_unwind and orc_unwind_ip tables were already
307 	 * sorted at build time via the 'sorttable' tool.
308 	 * It's ready for binary search straight away, no need to sort it.
309 	 */
310 
311 	/* Initialize the fast lookup table: */
312 	lookup_num_blocks = orc_lookup_end - orc_lookup;
313 	for (i = 0; i < lookup_num_blocks-1; i++) {
314 		orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
315 				 num_entries,
316 				 LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
317 		if (!orc) {
318 			orc_warn("WARNING: Corrupt .orc_unwind table.  Disabling unwinder.\n");
319 			return;
320 		}
321 
322 		orc_lookup[i] = orc - __start_orc_unwind;
323 	}
324 
325 	/* Initialize the ending block: */
326 	orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
327 			 LOOKUP_STOP_IP);
328 	if (!orc) {
329 		orc_warn("WARNING: Corrupt .orc_unwind table.  Disabling unwinder.\n");
330 		return;
331 	}
332 	orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
333 
334 	orc_init = true;
335 }
336 
337 unsigned long unwind_get_return_address(struct unwind_state *state)
338 {
339 	if (unwind_done(state))
340 		return 0;
341 
342 	return __kernel_text_address(state->ip) ? state->ip : 0;
343 }
344 EXPORT_SYMBOL_GPL(unwind_get_return_address);
345 
346 unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
347 {
348 	if (unwind_done(state))
349 		return NULL;
350 
351 	if (state->regs)
352 		return &state->regs->ip;
353 
354 	if (state->sp)
355 		return (unsigned long *)state->sp - 1;
356 
357 	return NULL;
358 }
359 
360 static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
361 			    size_t len)
362 {
363 	struct stack_info *info = &state->stack_info;
364 	void *addr = (void *)_addr;
365 
366 	if (on_stack(info, addr, len))
367 		return true;
368 
369 	return !get_stack_info(addr, state->task, info, &state->stack_mask) &&
370 		on_stack(info, addr, len);
371 }
372 
373 static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
374 			    unsigned long *val)
375 {
376 	if (!stack_access_ok(state, addr, sizeof(long)))
377 		return false;
378 
379 	*val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
380 	return true;
381 }
382 
383 static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
384 			     unsigned long *ip, unsigned long *sp)
385 {
386 	struct pt_regs *regs = (struct pt_regs *)addr;
387 
388 	/* x86-32 support will be more complicated due to the &regs->sp hack */
389 	BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
390 
391 	if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
392 		return false;
393 
394 	*ip = READ_ONCE_NOCHECK(regs->ip);
395 	*sp = READ_ONCE_NOCHECK(regs->sp);
396 	return true;
397 }
398 
399 static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
400 				  unsigned long *ip, unsigned long *sp)
401 {
402 	struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
403 
404 	if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
405 		return false;
406 
407 	*ip = READ_ONCE_NOCHECK(regs->ip);
408 	*sp = READ_ONCE_NOCHECK(regs->sp);
409 	return true;
410 }
411 
412 /*
413  * If state->regs is non-NULL, and points to a full pt_regs, just get the reg
414  * value from state->regs.
415  *
416  * Otherwise, if state->regs just points to IRET regs, and the previous frame
417  * had full regs, it's safe to get the value from the previous regs.  This can
418  * happen when early/late IRQ entry code gets interrupted by an NMI.
419  */
420 static bool get_reg(struct unwind_state *state, unsigned int reg_off,
421 		    unsigned long *val)
422 {
423 	unsigned int reg = reg_off/8;
424 
425 	if (!state->regs)
426 		return false;
427 
428 	if (state->full_regs) {
429 		*val = READ_ONCE_NOCHECK(((unsigned long *)state->regs)[reg]);
430 		return true;
431 	}
432 
433 	if (state->prev_regs) {
434 		*val = READ_ONCE_NOCHECK(((unsigned long *)state->prev_regs)[reg]);
435 		return true;
436 	}
437 
438 	return false;
439 }
440 
441 bool unwind_next_frame(struct unwind_state *state)
442 {
443 	unsigned long ip_p, sp, tmp, orig_ip = state->ip, prev_sp = state->sp;
444 	enum stack_type prev_type = state->stack_info.type;
445 	struct orc_entry *orc;
446 	bool indirect = false;
447 
448 	if (unwind_done(state))
449 		return false;
450 
451 	/* Don't let modules unload while we're reading their ORC data. */
452 	preempt_disable();
453 
454 	/* End-of-stack check for user tasks: */
455 	if (state->regs && user_mode(state->regs))
456 		goto the_end;
457 
458 	/*
459 	 * Find the orc_entry associated with the text address.
460 	 *
461 	 * For a call frame (as opposed to a signal frame), state->ip points to
462 	 * the instruction after the call.  That instruction's stack layout
463 	 * could be different from the call instruction's layout, for example
464 	 * if the call was to a noreturn function.  So get the ORC data for the
465 	 * call instruction itself.
466 	 */
467 	orc = orc_find(state->signal ? state->ip : state->ip - 1);
468 	if (!orc) {
469 		/*
470 		 * As a fallback, try to assume this code uses a frame pointer.
471 		 * This is useful for generated code, like BPF, which ORC
472 		 * doesn't know about.  This is just a guess, so the rest of
473 		 * the unwind is no longer considered reliable.
474 		 */
475 		orc = &orc_fp_entry;
476 		state->error = true;
477 	}
478 
479 	/* End-of-stack check for kernel threads: */
480 	if (orc->sp_reg == ORC_REG_UNDEFINED) {
481 		if (!orc->end)
482 			goto err;
483 
484 		goto the_end;
485 	}
486 
487 	/* Find the previous frame's stack: */
488 	switch (orc->sp_reg) {
489 	case ORC_REG_SP:
490 		sp = state->sp + orc->sp_offset;
491 		break;
492 
493 	case ORC_REG_BP:
494 		sp = state->bp + orc->sp_offset;
495 		break;
496 
497 	case ORC_REG_SP_INDIRECT:
498 		sp = state->sp;
499 		indirect = true;
500 		break;
501 
502 	case ORC_REG_BP_INDIRECT:
503 		sp = state->bp + orc->sp_offset;
504 		indirect = true;
505 		break;
506 
507 	case ORC_REG_R10:
508 		if (!get_reg(state, offsetof(struct pt_regs, r10), &sp)) {
509 			orc_warn_current("missing R10 value at %pB\n",
510 					 (void *)state->ip);
511 			goto err;
512 		}
513 		break;
514 
515 	case ORC_REG_R13:
516 		if (!get_reg(state, offsetof(struct pt_regs, r13), &sp)) {
517 			orc_warn_current("missing R13 value at %pB\n",
518 					 (void *)state->ip);
519 			goto err;
520 		}
521 		break;
522 
523 	case ORC_REG_DI:
524 		if (!get_reg(state, offsetof(struct pt_regs, di), &sp)) {
525 			orc_warn_current("missing RDI value at %pB\n",
526 					 (void *)state->ip);
527 			goto err;
528 		}
529 		break;
530 
531 	case ORC_REG_DX:
532 		if (!get_reg(state, offsetof(struct pt_regs, dx), &sp)) {
533 			orc_warn_current("missing DX value at %pB\n",
534 					 (void *)state->ip);
535 			goto err;
536 		}
537 		break;
538 
539 	default:
540 		orc_warn("unknown SP base reg %d at %pB\n",
541 			 orc->sp_reg, (void *)state->ip);
542 		goto err;
543 	}
544 
545 	if (indirect) {
546 		if (!deref_stack_reg(state, sp, &sp))
547 			goto err;
548 
549 		if (orc->sp_reg == ORC_REG_SP_INDIRECT)
550 			sp += orc->sp_offset;
551 	}
552 
553 	/* Find IP, SP and possibly regs: */
554 	switch (orc->type) {
555 	case UNWIND_HINT_TYPE_CALL:
556 		ip_p = sp - sizeof(long);
557 
558 		if (!deref_stack_reg(state, ip_p, &state->ip))
559 			goto err;
560 
561 		state->ip = unwind_recover_ret_addr(state, state->ip,
562 						    (unsigned long *)ip_p);
563 		state->sp = sp;
564 		state->regs = NULL;
565 		state->prev_regs = NULL;
566 		state->signal = false;
567 		break;
568 
569 	case UNWIND_HINT_TYPE_REGS:
570 		if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
571 			orc_warn_current("can't access registers at %pB\n",
572 					 (void *)orig_ip);
573 			goto err;
574 		}
575 		/*
576 		 * There is a small chance to interrupt at the entry of
577 		 * arch_rethook_trampoline() where the ORC info doesn't exist.
578 		 * That point is right after the RET to arch_rethook_trampoline()
579 		 * which was modified return address.
580 		 * At that point, the @addr_p of the unwind_recover_rethook()
581 		 * (this has to point the address of the stack entry storing
582 		 * the modified return address) must be "SP - (a stack entry)"
583 		 * because SP is incremented by the RET.
584 		 */
585 		state->ip = unwind_recover_rethook(state, state->ip,
586 				(unsigned long *)(state->sp - sizeof(long)));
587 		state->regs = (struct pt_regs *)sp;
588 		state->prev_regs = NULL;
589 		state->full_regs = true;
590 		state->signal = true;
591 		break;
592 
593 	case UNWIND_HINT_TYPE_REGS_PARTIAL:
594 		if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
595 			orc_warn_current("can't access iret registers at %pB\n",
596 					 (void *)orig_ip);
597 			goto err;
598 		}
599 		/* See UNWIND_HINT_TYPE_REGS case comment. */
600 		state->ip = unwind_recover_rethook(state, state->ip,
601 				(unsigned long *)(state->sp - sizeof(long)));
602 
603 		if (state->full_regs)
604 			state->prev_regs = state->regs;
605 		state->regs = (void *)sp - IRET_FRAME_OFFSET;
606 		state->full_regs = false;
607 		state->signal = true;
608 		break;
609 
610 	default:
611 		orc_warn("unknown .orc_unwind entry type %d at %pB\n",
612 			 orc->type, (void *)orig_ip);
613 		goto err;
614 	}
615 
616 	/* Find BP: */
617 	switch (orc->bp_reg) {
618 	case ORC_REG_UNDEFINED:
619 		if (get_reg(state, offsetof(struct pt_regs, bp), &tmp))
620 			state->bp = tmp;
621 		break;
622 
623 	case ORC_REG_PREV_SP:
624 		if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
625 			goto err;
626 		break;
627 
628 	case ORC_REG_BP:
629 		if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
630 			goto err;
631 		break;
632 
633 	default:
634 		orc_warn("unknown BP base reg %d for ip %pB\n",
635 			 orc->bp_reg, (void *)orig_ip);
636 		goto err;
637 	}
638 
639 	/* Prevent a recursive loop due to bad ORC data: */
640 	if (state->stack_info.type == prev_type &&
641 	    on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
642 	    state->sp <= prev_sp) {
643 		orc_warn_current("stack going in the wrong direction? at %pB\n",
644 				 (void *)orig_ip);
645 		goto err;
646 	}
647 
648 	preempt_enable();
649 	return true;
650 
651 err:
652 	state->error = true;
653 
654 the_end:
655 	preempt_enable();
656 	state->stack_info.type = STACK_TYPE_UNKNOWN;
657 	return false;
658 }
659 EXPORT_SYMBOL_GPL(unwind_next_frame);
660 
661 void __unwind_start(struct unwind_state *state, struct task_struct *task,
662 		    struct pt_regs *regs, unsigned long *first_frame)
663 {
664 	memset(state, 0, sizeof(*state));
665 	state->task = task;
666 
667 	if (!orc_init)
668 		goto err;
669 
670 	/*
671 	 * Refuse to unwind the stack of a task while it's executing on another
672 	 * CPU.  This check is racy, but that's ok: the unwinder has other
673 	 * checks to prevent it from going off the rails.
674 	 */
675 	if (task_on_another_cpu(task))
676 		goto err;
677 
678 	if (regs) {
679 		if (user_mode(regs))
680 			goto the_end;
681 
682 		state->ip = regs->ip;
683 		state->sp = regs->sp;
684 		state->bp = regs->bp;
685 		state->regs = regs;
686 		state->full_regs = true;
687 		state->signal = true;
688 
689 	} else if (task == current) {
690 		asm volatile("lea (%%rip), %0\n\t"
691 			     "mov %%rsp, %1\n\t"
692 			     "mov %%rbp, %2\n\t"
693 			     : "=r" (state->ip), "=r" (state->sp),
694 			       "=r" (state->bp));
695 
696 	} else {
697 		struct inactive_task_frame *frame = (void *)task->thread.sp;
698 
699 		state->sp = task->thread.sp + sizeof(*frame);
700 		state->bp = READ_ONCE_NOCHECK(frame->bp);
701 		state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
702 		state->signal = (void *)state->ip == ret_from_fork;
703 	}
704 
705 	if (get_stack_info((unsigned long *)state->sp, state->task,
706 			   &state->stack_info, &state->stack_mask)) {
707 		/*
708 		 * We weren't on a valid stack.  It's possible that
709 		 * we overflowed a valid stack into a guard page.
710 		 * See if the next page up is valid so that we can
711 		 * generate some kind of backtrace if this happens.
712 		 */
713 		void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
714 		state->error = true;
715 		if (get_stack_info(next_page, state->task, &state->stack_info,
716 				   &state->stack_mask))
717 			return;
718 	}
719 
720 	/*
721 	 * The caller can provide the address of the first frame directly
722 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
723 	 * to start unwinding at.  Skip ahead until we reach it.
724 	 */
725 
726 	/* When starting from regs, skip the regs frame: */
727 	if (regs) {
728 		unwind_next_frame(state);
729 		return;
730 	}
731 
732 	/* Otherwise, skip ahead to the user-specified starting frame: */
733 	while (!unwind_done(state) &&
734 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
735 			state->sp <= (unsigned long)first_frame))
736 		unwind_next_frame(state);
737 
738 	return;
739 
740 err:
741 	state->error = true;
742 the_end:
743 	state->stack_info.type = STACK_TYPE_UNKNOWN;
744 }
745 EXPORT_SYMBOL_GPL(__unwind_start);
746