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