xref: /linux/arch/x86/kernel/unwind_frame.c (revision 262fa734a0023a43391f9bd4a4099487b8393f35)
17c7900f8SJosh Poimboeuf #include <linux/sched.h>
229930025SIngo Molnar #include <linux/sched/task.h>
368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
4a8b7a923SJosh Poimboeuf #include <linux/interrupt.h>
5a8b7a923SJosh Poimboeuf #include <asm/sections.h>
67c7900f8SJosh Poimboeuf #include <asm/ptrace.h>
77c7900f8SJosh Poimboeuf #include <asm/bitops.h>
87c7900f8SJosh Poimboeuf #include <asm/stacktrace.h>
97c7900f8SJosh Poimboeuf #include <asm/unwind.h>
107c7900f8SJosh Poimboeuf 
117c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2)
127c7900f8SJosh Poimboeuf 
1384936118SJosh Poimboeuf /*
1484936118SJosh Poimboeuf  * This disables KASAN checking when reading a value from another task's stack,
1584936118SJosh Poimboeuf  * since the other task could be running on another CPU and could have poisoned
1684936118SJosh Poimboeuf  * the stack in the meantime.
1784936118SJosh Poimboeuf  */
1884936118SJosh Poimboeuf #define READ_ONCE_TASK_STACK(task, x)			\
1984936118SJosh Poimboeuf ({							\
2084936118SJosh Poimboeuf 	unsigned long val;				\
2184936118SJosh Poimboeuf 	if (task == current)				\
2284936118SJosh Poimboeuf 		val = READ_ONCE(x);			\
2384936118SJosh Poimboeuf 	else						\
2484936118SJosh Poimboeuf 		val = READ_ONCE_NOCHECK(x);		\
2584936118SJosh Poimboeuf 	val;						\
2684936118SJosh Poimboeuf })
2784936118SJosh Poimboeuf 
28aa4f8534SJosh Poimboeuf static void unwind_dump(struct unwind_state *state)
298b5e99f0SJosh Poimboeuf {
308b5e99f0SJosh Poimboeuf 	static bool dumped_before = false;
318b5e99f0SJosh Poimboeuf 	bool prev_zero, zero = false;
32aa4f8534SJosh Poimboeuf 	unsigned long word, *sp;
33*262fa734SJosh Poimboeuf 	struct stack_info stack_info = {0};
34*262fa734SJosh Poimboeuf 	unsigned long visit_mask = 0;
358b5e99f0SJosh Poimboeuf 
368b5e99f0SJosh Poimboeuf 	if (dumped_before)
378b5e99f0SJosh Poimboeuf 		return;
388b5e99f0SJosh Poimboeuf 
398b5e99f0SJosh Poimboeuf 	dumped_before = true;
408b5e99f0SJosh Poimboeuf 
414ea3d741SJosh Poimboeuf 	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
428b5e99f0SJosh Poimboeuf 			state->stack_info.type, state->stack_info.next_sp,
438b5e99f0SJosh Poimboeuf 			state->stack_mask, state->graph_idx);
448b5e99f0SJosh Poimboeuf 
45*262fa734SJosh Poimboeuf 	for (sp = state->orig_sp; sp; sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
46*262fa734SJosh Poimboeuf 		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
47*262fa734SJosh Poimboeuf 			break;
48*262fa734SJosh Poimboeuf 
49*262fa734SJosh Poimboeuf 		for (; sp < stack_info.end; sp++) {
50*262fa734SJosh Poimboeuf 
518b5e99f0SJosh Poimboeuf 			word = READ_ONCE_NOCHECK(*sp);
528b5e99f0SJosh Poimboeuf 
538b5e99f0SJosh Poimboeuf 			prev_zero = zero;
548b5e99f0SJosh Poimboeuf 			zero = word == 0;
558b5e99f0SJosh Poimboeuf 
568b5e99f0SJosh Poimboeuf 			if (zero) {
578b5e99f0SJosh Poimboeuf 				if (!prev_zero)
589b135b23SJosh Poimboeuf 					printk_deferred("%p: %0*x ...\n",
599b135b23SJosh Poimboeuf 							sp, BITS_PER_LONG/4, 0);
608b5e99f0SJosh Poimboeuf 				continue;
618b5e99f0SJosh Poimboeuf 			}
628b5e99f0SJosh Poimboeuf 
639b135b23SJosh Poimboeuf 			printk_deferred("%p: %0*lx (%pB)\n",
649b135b23SJosh Poimboeuf 					sp, BITS_PER_LONG/4, word, (void *)word);
658b5e99f0SJosh Poimboeuf 		}
668b5e99f0SJosh Poimboeuf 	}
67*262fa734SJosh Poimboeuf }
688b5e99f0SJosh Poimboeuf 
697c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
707c7900f8SJosh Poimboeuf {
717c7900f8SJosh Poimboeuf 	if (unwind_done(state))
727c7900f8SJosh Poimboeuf 		return 0;
737c7900f8SJosh Poimboeuf 
746bcdf9d5SJosh Poimboeuf 	return __kernel_text_address(state->ip) ? state->ip : 0;
757c7900f8SJosh Poimboeuf }
767c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
777c7900f8SJosh Poimboeuf 
7824d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs)
7924d86f59SJosh Poimboeuf {
8024d86f59SJosh Poimboeuf 	/* x86_32 regs from kernel mode are two words shorter: */
8124d86f59SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
8224d86f59SJosh Poimboeuf 		return sizeof(*regs) - 2*sizeof(long);
8324d86f59SJosh Poimboeuf 
8424d86f59SJosh Poimboeuf 	return sizeof(*regs);
8524d86f59SJosh Poimboeuf }
8624d86f59SJosh Poimboeuf 
87a8b7a923SJosh Poimboeuf static bool in_entry_code(unsigned long ip)
88a8b7a923SJosh Poimboeuf {
89a8b7a923SJosh Poimboeuf 	char *addr = (char *)ip;
90a8b7a923SJosh Poimboeuf 
91a8b7a923SJosh Poimboeuf 	if (addr >= __entry_text_start && addr < __entry_text_end)
92a8b7a923SJosh Poimboeuf 		return true;
93a8b7a923SJosh Poimboeuf 
94a8b7a923SJosh Poimboeuf #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
95a8b7a923SJosh Poimboeuf 	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
96a8b7a923SJosh Poimboeuf 		return true;
97a8b7a923SJosh Poimboeuf #endif
98a8b7a923SJosh Poimboeuf 
99a8b7a923SJosh Poimboeuf 	return false;
100a8b7a923SJosh Poimboeuf }
101a8b7a923SJosh Poimboeuf 
102b0d50c7bSJosh Poimboeuf static inline unsigned long *last_frame(struct unwind_state *state)
103b0d50c7bSJosh Poimboeuf {
104b0d50c7bSJosh Poimboeuf 	return (unsigned long *)task_pt_regs(state->task) - 2;
105b0d50c7bSJosh Poimboeuf }
106b0d50c7bSJosh Poimboeuf 
10787a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32
10887a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3
10987a6b297SJosh Poimboeuf #else
11087a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1
11187a6b297SJosh Poimboeuf #endif
11287a6b297SJosh Poimboeuf 
113b0d50c7bSJosh Poimboeuf static inline unsigned long *last_aligned_frame(struct unwind_state *state)
114b0d50c7bSJosh Poimboeuf {
115b0d50c7bSJosh Poimboeuf 	return last_frame(state) - GCC_REALIGN_WORDS;
116b0d50c7bSJosh Poimboeuf }
117b0d50c7bSJosh Poimboeuf 
118acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
119acb4608aSJosh Poimboeuf {
120b0d50c7bSJosh Poimboeuf 	unsigned long *last_bp = last_frame(state);
121b0d50c7bSJosh Poimboeuf 	unsigned long *aligned_bp = last_aligned_frame(state);
122acb4608aSJosh Poimboeuf 
1238023e0e2SJosh Poimboeuf 	/*
1248023e0e2SJosh Poimboeuf 	 * We have to check for the last task frame at two different locations
1258023e0e2SJosh Poimboeuf 	 * because gcc can occasionally decide to realign the stack pointer and
12687a6b297SJosh Poimboeuf 	 * change the offset of the stack frame in the prologue of a function
12787a6b297SJosh Poimboeuf 	 * called by head/entry code.  Examples:
12887a6b297SJosh Poimboeuf 	 *
12987a6b297SJosh Poimboeuf 	 * <start_secondary>:
13087a6b297SJosh Poimboeuf 	 *      push   %edi
13187a6b297SJosh Poimboeuf 	 *      lea    0x8(%esp),%edi
13287a6b297SJosh Poimboeuf 	 *      and    $0xfffffff8,%esp
13387a6b297SJosh Poimboeuf 	 *      pushl  -0x4(%edi)
13487a6b297SJosh Poimboeuf 	 *      push   %ebp
13587a6b297SJosh Poimboeuf 	 *      mov    %esp,%ebp
13687a6b297SJosh Poimboeuf 	 *
13787a6b297SJosh Poimboeuf 	 * <x86_64_start_kernel>:
13887a6b297SJosh Poimboeuf 	 *      lea    0x8(%rsp),%r10
13987a6b297SJosh Poimboeuf 	 *      and    $0xfffffffffffffff0,%rsp
14087a6b297SJosh Poimboeuf 	 *      pushq  -0x8(%r10)
14187a6b297SJosh Poimboeuf 	 *      push   %rbp
14287a6b297SJosh Poimboeuf 	 *      mov    %rsp,%rbp
14387a6b297SJosh Poimboeuf 	 *
14487a6b297SJosh Poimboeuf 	 * Note that after aligning the stack, it pushes a duplicate copy of
14587a6b297SJosh Poimboeuf 	 * the return address before pushing the frame pointer.
1468023e0e2SJosh Poimboeuf 	 */
14787a6b297SJosh Poimboeuf 	return (state->bp == last_bp ||
14887a6b297SJosh Poimboeuf 		(state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
149acb4608aSJosh Poimboeuf }
150acb4608aSJosh Poimboeuf 
151946c1911SJosh Poimboeuf /*
152946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
153946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
154946c1911SJosh Poimboeuf  */
155946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
156946c1911SJosh Poimboeuf {
157946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
158946c1911SJosh Poimboeuf 
159946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
160946c1911SJosh Poimboeuf 		return NULL;
161946c1911SJosh Poimboeuf 
162946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
163946c1911SJosh Poimboeuf }
164946c1911SJosh Poimboeuf 
1655ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state,
1665ed8d8bbSJosh Poimboeuf 			       unsigned long *next_bp)
1677c7900f8SJosh Poimboeuf {
1687c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
1695ed8d8bbSJosh Poimboeuf 	enum stack_type prev_type = info->type;
1705ed8d8bbSJosh Poimboeuf 	struct pt_regs *regs;
1716bcdf9d5SJosh Poimboeuf 	unsigned long *frame, *prev_frame_end, *addr_p, addr;
1725ed8d8bbSJosh Poimboeuf 	size_t len;
1735ed8d8bbSJosh Poimboeuf 
1745ed8d8bbSJosh Poimboeuf 	if (state->regs)
1755ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->regs + regs_size(state->regs);
1765ed8d8bbSJosh Poimboeuf 	else
1775ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
1785ed8d8bbSJosh Poimboeuf 
1795ed8d8bbSJosh Poimboeuf 	/* Is the next frame pointer an encoded pointer to pt_regs? */
1805ed8d8bbSJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
1815ed8d8bbSJosh Poimboeuf 	if (regs) {
1825ed8d8bbSJosh Poimboeuf 		frame = (unsigned long *)regs;
1835ed8d8bbSJosh Poimboeuf 		len = regs_size(regs);
184a8b7a923SJosh Poimboeuf 		state->got_irq = true;
1855ed8d8bbSJosh Poimboeuf 	} else {
1865ed8d8bbSJosh Poimboeuf 		frame = next_bp;
1875ed8d8bbSJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
1885ed8d8bbSJosh Poimboeuf 	}
1897c7900f8SJosh Poimboeuf 
1907c7900f8SJosh Poimboeuf 	/*
1915ed8d8bbSJosh Poimboeuf 	 * If the next bp isn't on the current stack, switch to the next one.
1927c7900f8SJosh Poimboeuf 	 *
1937c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
1945ed8d8bbSJosh Poimboeuf 	 * that info->next_sp could point to an empty stack and the next bp
1955ed8d8bbSJosh Poimboeuf 	 * could be on a subsequent stack.
1967c7900f8SJosh Poimboeuf 	 */
1975ed8d8bbSJosh Poimboeuf 	while (!on_stack(info, frame, len))
1987c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
1997c7900f8SJosh Poimboeuf 				   &state->stack_mask))
2007c7900f8SJosh Poimboeuf 			return false;
2017c7900f8SJosh Poimboeuf 
2025ed8d8bbSJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
2035ed8d8bbSJosh Poimboeuf 	if (state->orig_sp && state->stack_info.type == prev_type &&
2045ed8d8bbSJosh Poimboeuf 	    frame < prev_frame_end)
2055ed8d8bbSJosh Poimboeuf 		return false;
2065ed8d8bbSJosh Poimboeuf 
2075ed8d8bbSJosh Poimboeuf 	/* Move state to the next frame: */
2085ed8d8bbSJosh Poimboeuf 	if (regs) {
2095ed8d8bbSJosh Poimboeuf 		state->regs = regs;
2105ed8d8bbSJosh Poimboeuf 		state->bp = NULL;
2115ed8d8bbSJosh Poimboeuf 	} else {
2125ed8d8bbSJosh Poimboeuf 		state->bp = next_bp;
2135ed8d8bbSJosh Poimboeuf 		state->regs = NULL;
2145ed8d8bbSJosh Poimboeuf 	}
2155ed8d8bbSJosh Poimboeuf 
2166bcdf9d5SJosh Poimboeuf 	/* Save the return address: */
2176bcdf9d5SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
2186bcdf9d5SJosh Poimboeuf 		state->ip = 0;
2196bcdf9d5SJosh Poimboeuf 	else {
2206bcdf9d5SJosh Poimboeuf 		addr_p = unwind_get_return_address_ptr(state);
2216bcdf9d5SJosh Poimboeuf 		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
2226bcdf9d5SJosh Poimboeuf 		state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
2236bcdf9d5SJosh Poimboeuf 						  addr, addr_p);
2246bcdf9d5SJosh Poimboeuf 	}
2256bcdf9d5SJosh Poimboeuf 
2265ed8d8bbSJosh Poimboeuf 	/* Save the original stack pointer for unwind_dump(): */
227*262fa734SJosh Poimboeuf 	if (!state->orig_sp)
2285ed8d8bbSJosh Poimboeuf 		state->orig_sp = frame;
2298b5e99f0SJosh Poimboeuf 
2307c7900f8SJosh Poimboeuf 	return true;
2317c7900f8SJosh Poimboeuf }
2327c7900f8SJosh Poimboeuf 
2337c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
2347c7900f8SJosh Poimboeuf {
235946c1911SJosh Poimboeuf 	struct pt_regs *regs;
2365ed8d8bbSJosh Poimboeuf 	unsigned long *next_bp;
2377c7900f8SJosh Poimboeuf 
2387c7900f8SJosh Poimboeuf 	if (unwind_done(state))
2397c7900f8SJosh Poimboeuf 		return false;
2407c7900f8SJosh Poimboeuf 
2415ed8d8bbSJosh Poimboeuf 	/* Have we reached the end? */
242946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
243946c1911SJosh Poimboeuf 		goto the_end;
244946c1911SJosh Poimboeuf 
245acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
246acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
247acb4608aSJosh Poimboeuf 
248acb4608aSJosh Poimboeuf 		/*
249acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
250acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
251acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
252acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
253acb4608aSJosh Poimboeuf 		 *
254acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
255acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
256acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
257acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
258acb4608aSJosh Poimboeuf 		 */
259acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
260acb4608aSJosh Poimboeuf 			goto the_end;
261acb4608aSJosh Poimboeuf 
262acb4608aSJosh Poimboeuf 		/*
263acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
264acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
265acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
266acb4608aSJosh Poimboeuf 		 */
267acb4608aSJosh Poimboeuf 		state->regs = regs;
268acb4608aSJosh Poimboeuf 		state->bp = NULL;
2696bcdf9d5SJosh Poimboeuf 		state->ip = 0;
270acb4608aSJosh Poimboeuf 		return true;
271acb4608aSJosh Poimboeuf 	}
272acb4608aSJosh Poimboeuf 
2735ed8d8bbSJosh Poimboeuf 	/* Get the next frame pointer: */
274946c1911SJosh Poimboeuf 	if (state->regs)
275946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
276946c1911SJosh Poimboeuf 	else
27784936118SJosh Poimboeuf 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
2787c7900f8SJosh Poimboeuf 
2795ed8d8bbSJosh Poimboeuf 	/* Move to the next frame if it's safe: */
280a8b7a923SJosh Poimboeuf 	if (!update_stack_state(state, next_bp))
281c32c47c6SJosh Poimboeuf 		goto bad_address;
282c32c47c6SJosh Poimboeuf 
2837c7900f8SJosh Poimboeuf 	return true;
284946c1911SJosh Poimboeuf 
285c32c47c6SJosh Poimboeuf bad_address:
286900742d8SJosh Poimboeuf 	/*
287900742d8SJosh Poimboeuf 	 * When unwinding a non-current task, the task might actually be
288900742d8SJosh Poimboeuf 	 * running on another CPU, in which case it could be modifying its
289900742d8SJosh Poimboeuf 	 * stack while we're reading it.  This is generally not a problem and
290900742d8SJosh Poimboeuf 	 * can be ignored as long as the caller understands that unwinding
291900742d8SJosh Poimboeuf 	 * another task will not always succeed.
292900742d8SJosh Poimboeuf 	 */
293900742d8SJosh Poimboeuf 	if (state->task != current)
294900742d8SJosh Poimboeuf 		goto the_end;
295900742d8SJosh Poimboeuf 
296a8b7a923SJosh Poimboeuf 	/*
297a8b7a923SJosh Poimboeuf 	 * Don't warn if the unwinder got lost due to an interrupt in entry
298b0d50c7bSJosh Poimboeuf 	 * code or in the C handler before the first frame pointer got set up:
299a8b7a923SJosh Poimboeuf 	 */
300a8b7a923SJosh Poimboeuf 	if (state->got_irq && in_entry_code(state->ip))
301a8b7a923SJosh Poimboeuf 		goto the_end;
302b0d50c7bSJosh Poimboeuf 	if (state->regs &&
303b0d50c7bSJosh Poimboeuf 	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
304b0d50c7bSJosh Poimboeuf 	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
305b0d50c7bSJosh Poimboeuf 		goto the_end;
306a8b7a923SJosh Poimboeuf 
30724d86f59SJosh Poimboeuf 	if (state->regs) {
30824d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
30924d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
31024d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
3115ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
312aa4f8534SJosh Poimboeuf 		unwind_dump(state);
31324d86f59SJosh Poimboeuf 	} else {
314c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
315c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
316c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
3175ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
318aa4f8534SJosh Poimboeuf 		unwind_dump(state);
31924d86f59SJosh Poimboeuf 	}
320946c1911SJosh Poimboeuf the_end:
321946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
322946c1911SJosh Poimboeuf 	return false;
3237c7900f8SJosh Poimboeuf }
3247c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
3257c7900f8SJosh Poimboeuf 
3267c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
3277c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
3287c7900f8SJosh Poimboeuf {
3295ed8d8bbSJosh Poimboeuf 	unsigned long *bp;
330946c1911SJosh Poimboeuf 
3317c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
3327c7900f8SJosh Poimboeuf 	state->task = task;
333a8b7a923SJosh Poimboeuf 	state->got_irq = (regs);
3347c7900f8SJosh Poimboeuf 
3355ed8d8bbSJosh Poimboeuf 	/* Don't even attempt to start from user mode regs: */
3367c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
3377c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
3387c7900f8SJosh Poimboeuf 		return;
3397c7900f8SJosh Poimboeuf 	}
3407c7900f8SJosh Poimboeuf 
341946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
3427c7900f8SJosh Poimboeuf 
3435ed8d8bbSJosh Poimboeuf 	/* Initialize stack info and make sure the frame data is accessible: */
3445ed8d8bbSJosh Poimboeuf 	get_stack_info(bp, state->task, &state->stack_info,
3457c7900f8SJosh Poimboeuf 		       &state->stack_mask);
3465ed8d8bbSJosh Poimboeuf 	update_stack_state(state, bp);
3477c7900f8SJosh Poimboeuf 
3487c7900f8SJosh Poimboeuf 	/*
3497c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
3507c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
3517c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
3527c7900f8SJosh Poimboeuf 	 */
3537c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
3547c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
3557c7900f8SJosh Poimboeuf 			state->bp < first_frame))
3567c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
3577c7900f8SJosh Poimboeuf }
3587c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
359