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