xref: /OK3568_Linux_fs/kernel/arch/x86/kernel/unwind_frame.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun #include <linux/sched.h>
3*4882a593Smuzhiyun #include <linux/sched/task.h>
4*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
5*4882a593Smuzhiyun #include <linux/interrupt.h>
6*4882a593Smuzhiyun #include <asm/sections.h>
7*4882a593Smuzhiyun #include <asm/ptrace.h>
8*4882a593Smuzhiyun #include <asm/bitops.h>
9*4882a593Smuzhiyun #include <asm/stacktrace.h>
10*4882a593Smuzhiyun #include <asm/unwind.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define FRAME_HEADER_SIZE (sizeof(long) * 2)
13*4882a593Smuzhiyun 
unwind_get_return_address(struct unwind_state * state)14*4882a593Smuzhiyun unsigned long unwind_get_return_address(struct unwind_state *state)
15*4882a593Smuzhiyun {
16*4882a593Smuzhiyun 	if (unwind_done(state))
17*4882a593Smuzhiyun 		return 0;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 	return __kernel_text_address(state->ip) ? state->ip : 0;
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(unwind_get_return_address);
22*4882a593Smuzhiyun 
unwind_get_return_address_ptr(struct unwind_state * state)23*4882a593Smuzhiyun unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	if (unwind_done(state))
26*4882a593Smuzhiyun 		return NULL;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	return state->regs ? &state->regs->ip : state->bp + 1;
29*4882a593Smuzhiyun }
30*4882a593Smuzhiyun 
unwind_dump(struct unwind_state * state)31*4882a593Smuzhiyun static void unwind_dump(struct unwind_state *state)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	static bool dumped_before = false;
34*4882a593Smuzhiyun 	bool prev_zero, zero = false;
35*4882a593Smuzhiyun 	unsigned long word, *sp;
36*4882a593Smuzhiyun 	struct stack_info stack_info = {0};
37*4882a593Smuzhiyun 	unsigned long visit_mask = 0;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	if (dumped_before)
40*4882a593Smuzhiyun 		return;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	dumped_before = true;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
45*4882a593Smuzhiyun 			state->stack_info.type, state->stack_info.next_sp,
46*4882a593Smuzhiyun 			state->stack_mask, state->graph_idx);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
49*4882a593Smuzhiyun 	     sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
50*4882a593Smuzhiyun 		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
51*4882a593Smuzhiyun 			break;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 		for (; sp < stack_info.end; sp++) {
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 			word = READ_ONCE_NOCHECK(*sp);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 			prev_zero = zero;
58*4882a593Smuzhiyun 			zero = word == 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 			if (zero) {
61*4882a593Smuzhiyun 				if (!prev_zero)
62*4882a593Smuzhiyun 					printk_deferred("%p: %0*x ...\n",
63*4882a593Smuzhiyun 							sp, BITS_PER_LONG/4, 0);
64*4882a593Smuzhiyun 				continue;
65*4882a593Smuzhiyun 			}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 			printk_deferred("%p: %0*lx (%pB)\n",
68*4882a593Smuzhiyun 					sp, BITS_PER_LONG/4, word, (void *)word);
69*4882a593Smuzhiyun 		}
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
in_entry_code(unsigned long ip)73*4882a593Smuzhiyun static bool in_entry_code(unsigned long ip)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	char *addr = (char *)ip;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return addr >= __entry_text_start && addr < __entry_text_end;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
last_frame(struct unwind_state * state)80*4882a593Smuzhiyun static inline unsigned long *last_frame(struct unwind_state *state)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	return (unsigned long *)task_pt_regs(state->task) - 2;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
is_last_frame(struct unwind_state * state)85*4882a593Smuzhiyun static bool is_last_frame(struct unwind_state *state)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	return state->bp == last_frame(state);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #ifdef CONFIG_X86_32
91*4882a593Smuzhiyun #define GCC_REALIGN_WORDS 3
92*4882a593Smuzhiyun #else
93*4882a593Smuzhiyun #define GCC_REALIGN_WORDS 1
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun 
last_aligned_frame(struct unwind_state * state)96*4882a593Smuzhiyun static inline unsigned long *last_aligned_frame(struct unwind_state *state)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun 	return last_frame(state) - GCC_REALIGN_WORDS;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
is_last_aligned_frame(struct unwind_state * state)101*4882a593Smuzhiyun static bool is_last_aligned_frame(struct unwind_state *state)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	unsigned long *last_bp = last_frame(state);
104*4882a593Smuzhiyun 	unsigned long *aligned_bp = last_aligned_frame(state);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * GCC can occasionally decide to realign the stack pointer and change
108*4882a593Smuzhiyun 	 * the offset of the stack frame in the prologue of a function called
109*4882a593Smuzhiyun 	 * by head/entry code.  Examples:
110*4882a593Smuzhiyun 	 *
111*4882a593Smuzhiyun 	 * <start_secondary>:
112*4882a593Smuzhiyun 	 *      push   %edi
113*4882a593Smuzhiyun 	 *      lea    0x8(%esp),%edi
114*4882a593Smuzhiyun 	 *      and    $0xfffffff8,%esp
115*4882a593Smuzhiyun 	 *      pushl  -0x4(%edi)
116*4882a593Smuzhiyun 	 *      push   %ebp
117*4882a593Smuzhiyun 	 *      mov    %esp,%ebp
118*4882a593Smuzhiyun 	 *
119*4882a593Smuzhiyun 	 * <x86_64_start_kernel>:
120*4882a593Smuzhiyun 	 *      lea    0x8(%rsp),%r10
121*4882a593Smuzhiyun 	 *      and    $0xfffffffffffffff0,%rsp
122*4882a593Smuzhiyun 	 *      pushq  -0x8(%r10)
123*4882a593Smuzhiyun 	 *      push   %rbp
124*4882a593Smuzhiyun 	 *      mov    %rsp,%rbp
125*4882a593Smuzhiyun 	 *
126*4882a593Smuzhiyun 	 * After aligning the stack, it pushes a duplicate copy of the return
127*4882a593Smuzhiyun 	 * address before pushing the frame pointer.
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
is_last_ftrace_frame(struct unwind_state * state)132*4882a593Smuzhiyun static bool is_last_ftrace_frame(struct unwind_state *state)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	unsigned long *last_bp = last_frame(state);
135*4882a593Smuzhiyun 	unsigned long *last_ftrace_bp = last_bp - 3;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/*
138*4882a593Smuzhiyun 	 * When unwinding from an ftrace handler of a function called by entry
139*4882a593Smuzhiyun 	 * code, the stack layout of the last frame is:
140*4882a593Smuzhiyun 	 *
141*4882a593Smuzhiyun 	 *   bp
142*4882a593Smuzhiyun 	 *   parent ret addr
143*4882a593Smuzhiyun 	 *   bp
144*4882a593Smuzhiyun 	 *   function ret addr
145*4882a593Smuzhiyun 	 *   parent ret addr
146*4882a593Smuzhiyun 	 *   pt_regs
147*4882a593Smuzhiyun 	 *   -----------------
148*4882a593Smuzhiyun 	 */
149*4882a593Smuzhiyun 	return (state->bp == last_ftrace_bp &&
150*4882a593Smuzhiyun 		*state->bp == *(state->bp + 2) &&
151*4882a593Smuzhiyun 		*(state->bp + 1) == *(state->bp + 4));
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
is_last_task_frame(struct unwind_state * state)154*4882a593Smuzhiyun static bool is_last_task_frame(struct unwind_state *state)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	return is_last_frame(state) || is_last_aligned_frame(state) ||
157*4882a593Smuzhiyun 	       is_last_ftrace_frame(state);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * This determines if the frame pointer actually contains an encoded pointer to
162*4882a593Smuzhiyun  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun #ifdef CONFIG_X86_64
decode_frame_pointer(unsigned long * bp)165*4882a593Smuzhiyun static struct pt_regs *decode_frame_pointer(unsigned long *bp)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	unsigned long regs = (unsigned long)bp;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (!(regs & 0x1))
170*4882a593Smuzhiyun 		return NULL;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return (struct pt_regs *)(regs & ~0x1);
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun #else
decode_frame_pointer(unsigned long * bp)175*4882a593Smuzhiyun static struct pt_regs *decode_frame_pointer(unsigned long *bp)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	unsigned long regs = (unsigned long)bp;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	if (regs & 0x80000000)
180*4882a593Smuzhiyun 		return NULL;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	return (struct pt_regs *)(regs | 0x80000000);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun #endif
185*4882a593Smuzhiyun 
update_stack_state(struct unwind_state * state,unsigned long * next_bp)186*4882a593Smuzhiyun static bool update_stack_state(struct unwind_state *state,
187*4882a593Smuzhiyun 			       unsigned long *next_bp)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	struct stack_info *info = &state->stack_info;
190*4882a593Smuzhiyun 	enum stack_type prev_type = info->type;
191*4882a593Smuzhiyun 	struct pt_regs *regs;
192*4882a593Smuzhiyun 	unsigned long *frame, *prev_frame_end, *addr_p, addr;
193*4882a593Smuzhiyun 	size_t len;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (state->regs)
196*4882a593Smuzhiyun 		prev_frame_end = (void *)state->regs + sizeof(*state->regs);
197*4882a593Smuzhiyun 	else
198*4882a593Smuzhiyun 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* Is the next frame pointer an encoded pointer to pt_regs? */
201*4882a593Smuzhiyun 	regs = decode_frame_pointer(next_bp);
202*4882a593Smuzhiyun 	if (regs) {
203*4882a593Smuzhiyun 		frame = (unsigned long *)regs;
204*4882a593Smuzhiyun 		len = sizeof(*regs);
205*4882a593Smuzhiyun 		state->got_irq = true;
206*4882a593Smuzhiyun 	} else {
207*4882a593Smuzhiyun 		frame = next_bp;
208*4882a593Smuzhiyun 		len = FRAME_HEADER_SIZE;
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/*
212*4882a593Smuzhiyun 	 * If the next bp isn't on the current stack, switch to the next one.
213*4882a593Smuzhiyun 	 *
214*4882a593Smuzhiyun 	 * We may have to traverse multiple stacks to deal with the possibility
215*4882a593Smuzhiyun 	 * that info->next_sp could point to an empty stack and the next bp
216*4882a593Smuzhiyun 	 * could be on a subsequent stack.
217*4882a593Smuzhiyun 	 */
218*4882a593Smuzhiyun 	while (!on_stack(info, frame, len))
219*4882a593Smuzhiyun 		if (get_stack_info(info->next_sp, state->task, info,
220*4882a593Smuzhiyun 				   &state->stack_mask))
221*4882a593Smuzhiyun 			return false;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
224*4882a593Smuzhiyun 	if (state->orig_sp && state->stack_info.type == prev_type &&
225*4882a593Smuzhiyun 	    frame < prev_frame_end)
226*4882a593Smuzhiyun 		return false;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	/* Move state to the next frame: */
229*4882a593Smuzhiyun 	if (regs) {
230*4882a593Smuzhiyun 		state->regs = regs;
231*4882a593Smuzhiyun 		state->bp = NULL;
232*4882a593Smuzhiyun 	} else {
233*4882a593Smuzhiyun 		state->bp = next_bp;
234*4882a593Smuzhiyun 		state->regs = NULL;
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Save the return address: */
238*4882a593Smuzhiyun 	if (state->regs && user_mode(state->regs))
239*4882a593Smuzhiyun 		state->ip = 0;
240*4882a593Smuzhiyun 	else {
241*4882a593Smuzhiyun 		addr_p = unwind_get_return_address_ptr(state);
242*4882a593Smuzhiyun 		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
243*4882a593Smuzhiyun 		state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
244*4882a593Smuzhiyun 						  addr, addr_p);
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* Save the original stack pointer for unwind_dump(): */
248*4882a593Smuzhiyun 	if (!state->orig_sp)
249*4882a593Smuzhiyun 		state->orig_sp = frame;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return true;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
unwind_next_frame(struct unwind_state * state)254*4882a593Smuzhiyun bool unwind_next_frame(struct unwind_state *state)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	struct pt_regs *regs;
257*4882a593Smuzhiyun 	unsigned long *next_bp;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (unwind_done(state))
260*4882a593Smuzhiyun 		return false;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Have we reached the end? */
263*4882a593Smuzhiyun 	if (state->regs && user_mode(state->regs))
264*4882a593Smuzhiyun 		goto the_end;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (is_last_task_frame(state)) {
267*4882a593Smuzhiyun 		regs = task_pt_regs(state->task);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		/*
270*4882a593Smuzhiyun 		 * kthreads (other than the boot CPU's idle thread) have some
271*4882a593Smuzhiyun 		 * partial regs at the end of their stack which were placed
272*4882a593Smuzhiyun 		 * there by copy_thread().  But the regs don't have any
273*4882a593Smuzhiyun 		 * useful information, so we can skip them.
274*4882a593Smuzhiyun 		 *
275*4882a593Smuzhiyun 		 * This user_mode() check is slightly broader than a PF_KTHREAD
276*4882a593Smuzhiyun 		 * check because it also catches the awkward situation where a
277*4882a593Smuzhiyun 		 * newly forked kthread transitions into a user task by calling
278*4882a593Smuzhiyun 		 * kernel_execve(), which eventually clears PF_KTHREAD.
279*4882a593Smuzhiyun 		 */
280*4882a593Smuzhiyun 		if (!user_mode(regs))
281*4882a593Smuzhiyun 			goto the_end;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 		/*
284*4882a593Smuzhiyun 		 * We're almost at the end, but not quite: there's still the
285*4882a593Smuzhiyun 		 * syscall regs frame.  Entry code doesn't encode the regs
286*4882a593Smuzhiyun 		 * pointer for syscalls, so we have to set it manually.
287*4882a593Smuzhiyun 		 */
288*4882a593Smuzhiyun 		state->regs = regs;
289*4882a593Smuzhiyun 		state->bp = NULL;
290*4882a593Smuzhiyun 		state->ip = 0;
291*4882a593Smuzhiyun 		return true;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	/* Get the next frame pointer: */
295*4882a593Smuzhiyun 	if (state->next_bp) {
296*4882a593Smuzhiyun 		next_bp = state->next_bp;
297*4882a593Smuzhiyun 		state->next_bp = NULL;
298*4882a593Smuzhiyun 	} else if (state->regs) {
299*4882a593Smuzhiyun 		next_bp = (unsigned long *)state->regs->bp;
300*4882a593Smuzhiyun 	} else {
301*4882a593Smuzhiyun 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	/* Move to the next frame if it's safe: */
305*4882a593Smuzhiyun 	if (!update_stack_state(state, next_bp))
306*4882a593Smuzhiyun 		goto bad_address;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return true;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun bad_address:
311*4882a593Smuzhiyun 	state->error = true;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/*
314*4882a593Smuzhiyun 	 * When unwinding a non-current task, the task might actually be
315*4882a593Smuzhiyun 	 * running on another CPU, in which case it could be modifying its
316*4882a593Smuzhiyun 	 * stack while we're reading it.  This is generally not a problem and
317*4882a593Smuzhiyun 	 * can be ignored as long as the caller understands that unwinding
318*4882a593Smuzhiyun 	 * another task will not always succeed.
319*4882a593Smuzhiyun 	 */
320*4882a593Smuzhiyun 	if (state->task != current)
321*4882a593Smuzhiyun 		goto the_end;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/*
324*4882a593Smuzhiyun 	 * Don't warn if the unwinder got lost due to an interrupt in entry
325*4882a593Smuzhiyun 	 * code or in the C handler before the first frame pointer got set up:
326*4882a593Smuzhiyun 	 */
327*4882a593Smuzhiyun 	if (state->got_irq && in_entry_code(state->ip))
328*4882a593Smuzhiyun 		goto the_end;
329*4882a593Smuzhiyun 	if (state->regs &&
330*4882a593Smuzhiyun 	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
331*4882a593Smuzhiyun 	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
332*4882a593Smuzhiyun 		goto the_end;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	/*
335*4882a593Smuzhiyun 	 * There are some known frame pointer issues on 32-bit.  Disable
336*4882a593Smuzhiyun 	 * unwinder warnings on 32-bit until it gets objtool support.
337*4882a593Smuzhiyun 	 */
338*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_X86_32))
339*4882a593Smuzhiyun 		goto the_end;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	if (state->task != current)
342*4882a593Smuzhiyun 		goto the_end;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	if (state->regs) {
345*4882a593Smuzhiyun 		printk_deferred_once(KERN_WARNING
346*4882a593Smuzhiyun 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
347*4882a593Smuzhiyun 			state->regs, state->task->comm,
348*4882a593Smuzhiyun 			state->task->pid, next_bp);
349*4882a593Smuzhiyun 		unwind_dump(state);
350*4882a593Smuzhiyun 	} else {
351*4882a593Smuzhiyun 		printk_deferred_once(KERN_WARNING
352*4882a593Smuzhiyun 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
353*4882a593Smuzhiyun 			state->bp, state->task->comm,
354*4882a593Smuzhiyun 			state->task->pid, next_bp);
355*4882a593Smuzhiyun 		unwind_dump(state);
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun the_end:
358*4882a593Smuzhiyun 	state->stack_info.type = STACK_TYPE_UNKNOWN;
359*4882a593Smuzhiyun 	return false;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(unwind_next_frame);
362*4882a593Smuzhiyun 
__unwind_start(struct unwind_state * state,struct task_struct * task,struct pt_regs * regs,unsigned long * first_frame)363*4882a593Smuzhiyun void __unwind_start(struct unwind_state *state, struct task_struct *task,
364*4882a593Smuzhiyun 		    struct pt_regs *regs, unsigned long *first_frame)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	unsigned long *bp;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	memset(state, 0, sizeof(*state));
369*4882a593Smuzhiyun 	state->task = task;
370*4882a593Smuzhiyun 	state->got_irq = (regs);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/* Don't even attempt to start from user mode regs: */
373*4882a593Smuzhiyun 	if (regs && user_mode(regs)) {
374*4882a593Smuzhiyun 		state->stack_info.type = STACK_TYPE_UNKNOWN;
375*4882a593Smuzhiyun 		return;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	bp = get_frame_pointer(task, regs);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	/*
381*4882a593Smuzhiyun 	 * If we crash with IP==0, the last successfully executed instruction
382*4882a593Smuzhiyun 	 * was probably an indirect function call with a NULL function pointer.
383*4882a593Smuzhiyun 	 * That means that SP points into the middle of an incomplete frame:
384*4882a593Smuzhiyun 	 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
385*4882a593Smuzhiyun 	 * would have written a frame pointer if we hadn't crashed.
386*4882a593Smuzhiyun 	 * Pretend that the frame is complete and that BP points to it, but save
387*4882a593Smuzhiyun 	 * the real BP so that we can use it when looking for the next frame.
388*4882a593Smuzhiyun 	 */
389*4882a593Smuzhiyun 	if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
390*4882a593Smuzhiyun 		state->next_bp = bp;
391*4882a593Smuzhiyun 		bp = ((unsigned long *)regs->sp) - 1;
392*4882a593Smuzhiyun 	}
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* Initialize stack info and make sure the frame data is accessible: */
395*4882a593Smuzhiyun 	get_stack_info(bp, state->task, &state->stack_info,
396*4882a593Smuzhiyun 		       &state->stack_mask);
397*4882a593Smuzhiyun 	update_stack_state(state, bp);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/*
400*4882a593Smuzhiyun 	 * The caller can provide the address of the first frame directly
401*4882a593Smuzhiyun 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
402*4882a593Smuzhiyun 	 * to start unwinding at.  Skip ahead until we reach it.
403*4882a593Smuzhiyun 	 */
404*4882a593Smuzhiyun 	while (!unwind_done(state) &&
405*4882a593Smuzhiyun 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
406*4882a593Smuzhiyun 			(state->next_bp == NULL && state->bp < first_frame)))
407*4882a593Smuzhiyun 		unwind_next_frame(state);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__unwind_start);
410