xref: /openbmc/linux/arch/x86/kernel/unwind_frame.c (revision f4f34e1b82eb4219d8eaa1c7e2e17ca219a6a2b5)
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 
13ee9f8fceSJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
14ee9f8fceSJosh Poimboeuf {
15ee9f8fceSJosh Poimboeuf 	if (unwind_done(state))
16ee9f8fceSJosh Poimboeuf 		return 0;
17ee9f8fceSJosh Poimboeuf 
18ee9f8fceSJosh Poimboeuf 	return __kernel_text_address(state->ip) ? state->ip : 0;
19ee9f8fceSJosh Poimboeuf }
20ee9f8fceSJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
21ee9f8fceSJosh Poimboeuf 
22ee9f8fceSJosh Poimboeuf unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
23ee9f8fceSJosh Poimboeuf {
24ee9f8fceSJosh Poimboeuf 	if (unwind_done(state))
25ee9f8fceSJosh Poimboeuf 		return NULL;
26ee9f8fceSJosh Poimboeuf 
27ee9f8fceSJosh Poimboeuf 	return state->regs ? &state->regs->ip : state->bp + 1;
28ee9f8fceSJosh Poimboeuf }
2984936118SJosh Poimboeuf 
30aa4f8534SJosh Poimboeuf static void unwind_dump(struct unwind_state *state)
318b5e99f0SJosh Poimboeuf {
328b5e99f0SJosh Poimboeuf 	static bool dumped_before = false;
338b5e99f0SJosh Poimboeuf 	bool prev_zero, zero = false;
34aa4f8534SJosh Poimboeuf 	unsigned long word, *sp;
35262fa734SJosh Poimboeuf 	struct stack_info stack_info = {0};
36262fa734SJosh Poimboeuf 	unsigned long visit_mask = 0;
378b5e99f0SJosh Poimboeuf 
388b5e99f0SJosh Poimboeuf 	if (dumped_before)
398b5e99f0SJosh Poimboeuf 		return;
408b5e99f0SJosh Poimboeuf 
418b5e99f0SJosh Poimboeuf 	dumped_before = true;
428b5e99f0SJosh Poimboeuf 
434ea3d741SJosh Poimboeuf 	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
448b5e99f0SJosh Poimboeuf 			state->stack_info.type, state->stack_info.next_sp,
458b5e99f0SJosh Poimboeuf 			state->stack_mask, state->graph_idx);
468b5e99f0SJosh Poimboeuf 
4799bd28a4SJosh Poimboeuf 	for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
4899bd28a4SJosh Poimboeuf 	     sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
49262fa734SJosh Poimboeuf 		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
50262fa734SJosh Poimboeuf 			break;
51262fa734SJosh Poimboeuf 
52262fa734SJosh Poimboeuf 		for (; sp < stack_info.end; sp++) {
53262fa734SJosh Poimboeuf 
548b5e99f0SJosh Poimboeuf 			word = READ_ONCE_NOCHECK(*sp);
558b5e99f0SJosh Poimboeuf 
568b5e99f0SJosh Poimboeuf 			prev_zero = zero;
578b5e99f0SJosh Poimboeuf 			zero = word == 0;
588b5e99f0SJosh Poimboeuf 
598b5e99f0SJosh Poimboeuf 			if (zero) {
608b5e99f0SJosh Poimboeuf 				if (!prev_zero)
619b135b23SJosh Poimboeuf 					printk_deferred("%p: %0*x ...\n",
629b135b23SJosh Poimboeuf 							sp, BITS_PER_LONG/4, 0);
638b5e99f0SJosh Poimboeuf 				continue;
648b5e99f0SJosh Poimboeuf 			}
658b5e99f0SJosh Poimboeuf 
669b135b23SJosh Poimboeuf 			printk_deferred("%p: %0*lx (%pB)\n",
679b135b23SJosh Poimboeuf 					sp, BITS_PER_LONG/4, word, (void *)word);
688b5e99f0SJosh Poimboeuf 		}
698b5e99f0SJosh Poimboeuf 	}
70262fa734SJosh Poimboeuf }
718b5e99f0SJosh Poimboeuf 
7224d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs)
7324d86f59SJosh Poimboeuf {
7424d86f59SJosh Poimboeuf 	/* x86_32 regs from kernel mode are two words shorter: */
7524d86f59SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
7624d86f59SJosh Poimboeuf 		return sizeof(*regs) - 2*sizeof(long);
7724d86f59SJosh Poimboeuf 
7824d86f59SJosh Poimboeuf 	return sizeof(*regs);
7924d86f59SJosh Poimboeuf }
8024d86f59SJosh Poimboeuf 
81a8b7a923SJosh Poimboeuf static bool in_entry_code(unsigned long ip)
82a8b7a923SJosh Poimboeuf {
83a8b7a923SJosh Poimboeuf 	char *addr = (char *)ip;
84a8b7a923SJosh Poimboeuf 
85a8b7a923SJosh Poimboeuf 	if (addr >= __entry_text_start && addr < __entry_text_end)
86a8b7a923SJosh Poimboeuf 		return true;
87a8b7a923SJosh Poimboeuf 
88a8b7a923SJosh Poimboeuf 	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
89a8b7a923SJosh Poimboeuf 		return true;
90a8b7a923SJosh Poimboeuf 
91a8b7a923SJosh Poimboeuf 	return false;
92a8b7a923SJosh Poimboeuf }
93a8b7a923SJosh Poimboeuf 
94b0d50c7bSJosh Poimboeuf static inline unsigned long *last_frame(struct unwind_state *state)
95b0d50c7bSJosh Poimboeuf {
96b0d50c7bSJosh Poimboeuf 	return (unsigned long *)task_pt_regs(state->task) - 2;
97b0d50c7bSJosh Poimboeuf }
98b0d50c7bSJosh Poimboeuf 
99519fb5c3SJosh Poimboeuf static bool is_last_frame(struct unwind_state *state)
100519fb5c3SJosh Poimboeuf {
101519fb5c3SJosh Poimboeuf 	return state->bp == last_frame(state);
102519fb5c3SJosh Poimboeuf }
103519fb5c3SJosh Poimboeuf 
10487a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32
10587a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3
10687a6b297SJosh Poimboeuf #else
10787a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1
10887a6b297SJosh Poimboeuf #endif
10987a6b297SJosh Poimboeuf 
110b0d50c7bSJosh Poimboeuf static inline unsigned long *last_aligned_frame(struct unwind_state *state)
111b0d50c7bSJosh Poimboeuf {
112b0d50c7bSJosh Poimboeuf 	return last_frame(state) - GCC_REALIGN_WORDS;
113b0d50c7bSJosh Poimboeuf }
114b0d50c7bSJosh Poimboeuf 
115519fb5c3SJosh Poimboeuf static bool is_last_aligned_frame(struct unwind_state *state)
116acb4608aSJosh Poimboeuf {
117b0d50c7bSJosh Poimboeuf 	unsigned long *last_bp = last_frame(state);
118b0d50c7bSJosh Poimboeuf 	unsigned long *aligned_bp = last_aligned_frame(state);
119acb4608aSJosh Poimboeuf 
1208023e0e2SJosh Poimboeuf 	/*
121519fb5c3SJosh Poimboeuf 	 * GCC can occasionally decide to realign the stack pointer and change
122519fb5c3SJosh Poimboeuf 	 * the offset of the stack frame in the prologue of a function called
123519fb5c3SJosh Poimboeuf 	 * by head/entry code.  Examples:
12487a6b297SJosh Poimboeuf 	 *
12587a6b297SJosh Poimboeuf 	 * <start_secondary>:
12687a6b297SJosh Poimboeuf 	 *      push   %edi
12787a6b297SJosh Poimboeuf 	 *      lea    0x8(%esp),%edi
12887a6b297SJosh Poimboeuf 	 *      and    $0xfffffff8,%esp
12987a6b297SJosh Poimboeuf 	 *      pushl  -0x4(%edi)
13087a6b297SJosh Poimboeuf 	 *      push   %ebp
13187a6b297SJosh Poimboeuf 	 *      mov    %esp,%ebp
13287a6b297SJosh Poimboeuf 	 *
13387a6b297SJosh Poimboeuf 	 * <x86_64_start_kernel>:
13487a6b297SJosh Poimboeuf 	 *      lea    0x8(%rsp),%r10
13587a6b297SJosh Poimboeuf 	 *      and    $0xfffffffffffffff0,%rsp
13687a6b297SJosh Poimboeuf 	 *      pushq  -0x8(%r10)
13787a6b297SJosh Poimboeuf 	 *      push   %rbp
13887a6b297SJosh Poimboeuf 	 *      mov    %rsp,%rbp
13987a6b297SJosh Poimboeuf 	 *
140519fb5c3SJosh Poimboeuf 	 * After aligning the stack, it pushes a duplicate copy of the return
141519fb5c3SJosh Poimboeuf 	 * address before pushing the frame pointer.
1428023e0e2SJosh Poimboeuf 	 */
143519fb5c3SJosh Poimboeuf 	return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
144519fb5c3SJosh Poimboeuf }
145519fb5c3SJosh Poimboeuf 
146519fb5c3SJosh Poimboeuf static bool is_last_ftrace_frame(struct unwind_state *state)
147519fb5c3SJosh Poimboeuf {
148519fb5c3SJosh Poimboeuf 	unsigned long *last_bp = last_frame(state);
149519fb5c3SJosh Poimboeuf 	unsigned long *last_ftrace_bp = last_bp - 3;
150519fb5c3SJosh Poimboeuf 
151519fb5c3SJosh Poimboeuf 	/*
152519fb5c3SJosh Poimboeuf 	 * When unwinding from an ftrace handler of a function called by entry
153519fb5c3SJosh Poimboeuf 	 * code, the stack layout of the last frame is:
154519fb5c3SJosh Poimboeuf 	 *
155519fb5c3SJosh Poimboeuf 	 *   bp
156519fb5c3SJosh Poimboeuf 	 *   parent ret addr
157519fb5c3SJosh Poimboeuf 	 *   bp
158519fb5c3SJosh Poimboeuf 	 *   function ret addr
159519fb5c3SJosh Poimboeuf 	 *   parent ret addr
160519fb5c3SJosh Poimboeuf 	 *   pt_regs
161519fb5c3SJosh Poimboeuf 	 *   -----------------
162519fb5c3SJosh Poimboeuf 	 */
163519fb5c3SJosh Poimboeuf 	return (state->bp == last_ftrace_bp &&
164519fb5c3SJosh Poimboeuf 		*state->bp == *(state->bp + 2) &&
165519fb5c3SJosh Poimboeuf 		*(state->bp + 1) == *(state->bp + 4));
166519fb5c3SJosh Poimboeuf }
167519fb5c3SJosh Poimboeuf 
168519fb5c3SJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
169519fb5c3SJosh Poimboeuf {
170519fb5c3SJosh Poimboeuf 	return is_last_frame(state) || is_last_aligned_frame(state) ||
171519fb5c3SJosh Poimboeuf 	       is_last_ftrace_frame(state);
172acb4608aSJosh Poimboeuf }
173acb4608aSJosh Poimboeuf 
174946c1911SJosh Poimboeuf /*
175946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
176946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
177946c1911SJosh Poimboeuf  */
1785c99b692SJosh Poimboeuf #ifdef CONFIG_X86_64
179946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
180946c1911SJosh Poimboeuf {
181946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
182946c1911SJosh Poimboeuf 
183946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
184946c1911SJosh Poimboeuf 		return NULL;
185946c1911SJosh Poimboeuf 
186946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
187946c1911SJosh Poimboeuf }
1885c99b692SJosh Poimboeuf #else
1895c99b692SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
1905c99b692SJosh Poimboeuf {
1915c99b692SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
1925c99b692SJosh Poimboeuf 
1935c99b692SJosh Poimboeuf 	if (regs & 0x80000000)
1945c99b692SJosh Poimboeuf 		return NULL;
1955c99b692SJosh Poimboeuf 
1965c99b692SJosh Poimboeuf 	return (struct pt_regs *)(regs | 0x80000000);
1975c99b692SJosh Poimboeuf }
1985c99b692SJosh Poimboeuf #endif
199946c1911SJosh Poimboeuf 
20062dd86acSJosh Poimboeuf #ifdef CONFIG_X86_32
20162dd86acSJosh Poimboeuf #define KERNEL_REGS_SIZE (sizeof(struct pt_regs) - 2*sizeof(long))
20262dd86acSJosh Poimboeuf #else
20362dd86acSJosh Poimboeuf #define KERNEL_REGS_SIZE (sizeof(struct pt_regs))
20462dd86acSJosh Poimboeuf #endif
20562dd86acSJosh Poimboeuf 
2065ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state,
2075ed8d8bbSJosh Poimboeuf 			       unsigned long *next_bp)
2087c7900f8SJosh Poimboeuf {
2097c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
2105ed8d8bbSJosh Poimboeuf 	enum stack_type prev_type = info->type;
2115ed8d8bbSJosh Poimboeuf 	struct pt_regs *regs;
2126bcdf9d5SJosh Poimboeuf 	unsigned long *frame, *prev_frame_end, *addr_p, addr;
2135ed8d8bbSJosh Poimboeuf 	size_t len;
2145ed8d8bbSJosh Poimboeuf 
2155ed8d8bbSJosh Poimboeuf 	if (state->regs)
2165ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->regs + regs_size(state->regs);
2175ed8d8bbSJosh Poimboeuf 	else
2185ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
2195ed8d8bbSJosh Poimboeuf 
2205ed8d8bbSJosh Poimboeuf 	/* Is the next frame pointer an encoded pointer to pt_regs? */
2215ed8d8bbSJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
2225ed8d8bbSJosh Poimboeuf 	if (regs) {
2235ed8d8bbSJosh Poimboeuf 		frame = (unsigned long *)regs;
22462dd86acSJosh Poimboeuf 		len = KERNEL_REGS_SIZE;
225a8b7a923SJosh Poimboeuf 		state->got_irq = true;
2265ed8d8bbSJosh Poimboeuf 	} else {
2275ed8d8bbSJosh Poimboeuf 		frame = next_bp;
2285ed8d8bbSJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
2295ed8d8bbSJosh Poimboeuf 	}
2307c7900f8SJosh Poimboeuf 
2317c7900f8SJosh Poimboeuf 	/*
2325ed8d8bbSJosh Poimboeuf 	 * If the next bp isn't on the current stack, switch to the next one.
2337c7900f8SJosh Poimboeuf 	 *
2347c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
2355ed8d8bbSJosh Poimboeuf 	 * that info->next_sp could point to an empty stack and the next bp
2365ed8d8bbSJosh Poimboeuf 	 * could be on a subsequent stack.
2377c7900f8SJosh Poimboeuf 	 */
2385ed8d8bbSJosh Poimboeuf 	while (!on_stack(info, frame, len))
2397c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
2407c7900f8SJosh Poimboeuf 				   &state->stack_mask))
2417c7900f8SJosh Poimboeuf 			return false;
2427c7900f8SJosh Poimboeuf 
2435ed8d8bbSJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
2445ed8d8bbSJosh Poimboeuf 	if (state->orig_sp && state->stack_info.type == prev_type &&
2455ed8d8bbSJosh Poimboeuf 	    frame < prev_frame_end)
2465ed8d8bbSJosh Poimboeuf 		return false;
2475ed8d8bbSJosh Poimboeuf 
24862dd86acSJosh Poimboeuf 	/*
24962dd86acSJosh Poimboeuf 	 * On 32-bit with user mode regs, make sure the last two regs are safe
25062dd86acSJosh Poimboeuf 	 * to access:
25162dd86acSJosh Poimboeuf 	 */
25262dd86acSJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && regs && user_mode(regs) &&
25362dd86acSJosh Poimboeuf 	    !on_stack(info, frame, len + 2*sizeof(long)))
25462dd86acSJosh Poimboeuf 		return false;
25562dd86acSJosh Poimboeuf 
2565ed8d8bbSJosh Poimboeuf 	/* Move state to the next frame: */
2575ed8d8bbSJosh Poimboeuf 	if (regs) {
2585ed8d8bbSJosh Poimboeuf 		state->regs = regs;
2595ed8d8bbSJosh Poimboeuf 		state->bp = NULL;
2605ed8d8bbSJosh Poimboeuf 	} else {
2615ed8d8bbSJosh Poimboeuf 		state->bp = next_bp;
2625ed8d8bbSJosh Poimboeuf 		state->regs = NULL;
2635ed8d8bbSJosh Poimboeuf 	}
2645ed8d8bbSJosh Poimboeuf 
2656bcdf9d5SJosh Poimboeuf 	/* Save the return address: */
2666bcdf9d5SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
2676bcdf9d5SJosh Poimboeuf 		state->ip = 0;
2686bcdf9d5SJosh Poimboeuf 	else {
2696bcdf9d5SJosh Poimboeuf 		addr_p = unwind_get_return_address_ptr(state);
2706bcdf9d5SJosh Poimboeuf 		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
2716bcdf9d5SJosh Poimboeuf 		state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
2726bcdf9d5SJosh Poimboeuf 						  addr, addr_p);
2736bcdf9d5SJosh Poimboeuf 	}
2746bcdf9d5SJosh Poimboeuf 
2755ed8d8bbSJosh Poimboeuf 	/* Save the original stack pointer for unwind_dump(): */
276262fa734SJosh Poimboeuf 	if (!state->orig_sp)
2775ed8d8bbSJosh Poimboeuf 		state->orig_sp = frame;
2788b5e99f0SJosh Poimboeuf 
2797c7900f8SJosh Poimboeuf 	return true;
2807c7900f8SJosh Poimboeuf }
2817c7900f8SJosh Poimboeuf 
2827c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
2837c7900f8SJosh Poimboeuf {
284946c1911SJosh Poimboeuf 	struct pt_regs *regs;
2855ed8d8bbSJosh Poimboeuf 	unsigned long *next_bp;
2867c7900f8SJosh Poimboeuf 
2877c7900f8SJosh Poimboeuf 	if (unwind_done(state))
2887c7900f8SJosh Poimboeuf 		return false;
2897c7900f8SJosh Poimboeuf 
2905ed8d8bbSJosh Poimboeuf 	/* Have we reached the end? */
291946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
292946c1911SJosh Poimboeuf 		goto the_end;
293946c1911SJosh Poimboeuf 
294acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
295acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
296acb4608aSJosh Poimboeuf 
297acb4608aSJosh Poimboeuf 		/*
298acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
299acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
300acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
301acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
302acb4608aSJosh Poimboeuf 		 *
303acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
304acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
305acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
306acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
307acb4608aSJosh Poimboeuf 		 */
308acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
309acb4608aSJosh Poimboeuf 			goto the_end;
310acb4608aSJosh Poimboeuf 
311acb4608aSJosh Poimboeuf 		/*
312acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
313acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
314acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
315acb4608aSJosh Poimboeuf 		 */
316acb4608aSJosh Poimboeuf 		state->regs = regs;
317acb4608aSJosh Poimboeuf 		state->bp = NULL;
3186bcdf9d5SJosh Poimboeuf 		state->ip = 0;
319acb4608aSJosh Poimboeuf 		return true;
320acb4608aSJosh Poimboeuf 	}
321acb4608aSJosh Poimboeuf 
3225ed8d8bbSJosh Poimboeuf 	/* Get the next frame pointer: */
323*f4f34e1bSJann Horn 	if (state->next_bp) {
324*f4f34e1bSJann Horn 		next_bp = state->next_bp;
325*f4f34e1bSJann Horn 		state->next_bp = NULL;
326*f4f34e1bSJann Horn 	} else if (state->regs) {
327946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
328*f4f34e1bSJann Horn 	} else {
32984936118SJosh Poimboeuf 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
330*f4f34e1bSJann Horn 	}
3317c7900f8SJosh Poimboeuf 
3325ed8d8bbSJosh Poimboeuf 	/* Move to the next frame if it's safe: */
333a8b7a923SJosh Poimboeuf 	if (!update_stack_state(state, next_bp))
334c32c47c6SJosh Poimboeuf 		goto bad_address;
335c32c47c6SJosh Poimboeuf 
3367c7900f8SJosh Poimboeuf 	return true;
337946c1911SJosh Poimboeuf 
338c32c47c6SJosh Poimboeuf bad_address:
339af085d90SJosh Poimboeuf 	state->error = true;
340af085d90SJosh Poimboeuf 
341900742d8SJosh Poimboeuf 	/*
342900742d8SJosh Poimboeuf 	 * When unwinding a non-current task, the task might actually be
343900742d8SJosh Poimboeuf 	 * running on another CPU, in which case it could be modifying its
344900742d8SJosh Poimboeuf 	 * stack while we're reading it.  This is generally not a problem and
345900742d8SJosh Poimboeuf 	 * can be ignored as long as the caller understands that unwinding
346900742d8SJosh Poimboeuf 	 * another task will not always succeed.
347900742d8SJosh Poimboeuf 	 */
348900742d8SJosh Poimboeuf 	if (state->task != current)
349900742d8SJosh Poimboeuf 		goto the_end;
350900742d8SJosh Poimboeuf 
351a8b7a923SJosh Poimboeuf 	/*
352a8b7a923SJosh Poimboeuf 	 * Don't warn if the unwinder got lost due to an interrupt in entry
353b0d50c7bSJosh Poimboeuf 	 * code or in the C handler before the first frame pointer got set up:
354a8b7a923SJosh Poimboeuf 	 */
355a8b7a923SJosh Poimboeuf 	if (state->got_irq && in_entry_code(state->ip))
356a8b7a923SJosh Poimboeuf 		goto the_end;
357b0d50c7bSJosh Poimboeuf 	if (state->regs &&
358b0d50c7bSJosh Poimboeuf 	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
359b0d50c7bSJosh Poimboeuf 	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
360b0d50c7bSJosh Poimboeuf 		goto the_end;
361a8b7a923SJosh Poimboeuf 
362d4a2d031SJosh Poimboeuf 	/*
363d4a2d031SJosh Poimboeuf 	 * There are some known frame pointer issues on 32-bit.  Disable
364d4a2d031SJosh Poimboeuf 	 * unwinder warnings on 32-bit until it gets objtool support.
365d4a2d031SJosh Poimboeuf 	 */
366d4a2d031SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32))
367d4a2d031SJosh Poimboeuf 		goto the_end;
368d4a2d031SJosh Poimboeuf 
36924d86f59SJosh Poimboeuf 	if (state->regs) {
37024d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
37124d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
37224d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
3735ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
374aa4f8534SJosh Poimboeuf 		unwind_dump(state);
37524d86f59SJosh Poimboeuf 	} else {
376c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
377c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
378c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
3795ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
380aa4f8534SJosh Poimboeuf 		unwind_dump(state);
38124d86f59SJosh Poimboeuf 	}
382946c1911SJosh Poimboeuf the_end:
383946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
384946c1911SJosh Poimboeuf 	return false;
3857c7900f8SJosh Poimboeuf }
3867c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
3877c7900f8SJosh Poimboeuf 
3887c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
3897c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
3907c7900f8SJosh Poimboeuf {
3915ed8d8bbSJosh Poimboeuf 	unsigned long *bp;
392946c1911SJosh Poimboeuf 
3937c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
3947c7900f8SJosh Poimboeuf 	state->task = task;
395a8b7a923SJosh Poimboeuf 	state->got_irq = (regs);
3967c7900f8SJosh Poimboeuf 
3975ed8d8bbSJosh Poimboeuf 	/* Don't even attempt to start from user mode regs: */
3987c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
3997c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
4007c7900f8SJosh Poimboeuf 		return;
4017c7900f8SJosh Poimboeuf 	}
4027c7900f8SJosh Poimboeuf 
403946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
4047c7900f8SJosh Poimboeuf 
405*f4f34e1bSJann Horn 	/*
406*f4f34e1bSJann Horn 	 * If we crash with IP==0, the last successfully executed instruction
407*f4f34e1bSJann Horn 	 * was probably an indirect function call with a NULL function pointer.
408*f4f34e1bSJann Horn 	 * That means that SP points into the middle of an incomplete frame:
409*f4f34e1bSJann Horn 	 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
410*f4f34e1bSJann Horn 	 * would have written a frame pointer if we hadn't crashed.
411*f4f34e1bSJann Horn 	 * Pretend that the frame is complete and that BP points to it, but save
412*f4f34e1bSJann Horn 	 * the real BP so that we can use it when looking for the next frame.
413*f4f34e1bSJann Horn 	 */
414*f4f34e1bSJann Horn 	if (regs && regs->ip == 0 &&
415*f4f34e1bSJann Horn 	    (unsigned long *)kernel_stack_pointer(regs) >= first_frame) {
416*f4f34e1bSJann Horn 		state->next_bp = bp;
417*f4f34e1bSJann Horn 		bp = ((unsigned long *)kernel_stack_pointer(regs)) - 1;
418*f4f34e1bSJann Horn 	}
419*f4f34e1bSJann Horn 
4205ed8d8bbSJosh Poimboeuf 	/* Initialize stack info and make sure the frame data is accessible: */
4215ed8d8bbSJosh Poimboeuf 	get_stack_info(bp, state->task, &state->stack_info,
4227c7900f8SJosh Poimboeuf 		       &state->stack_mask);
4235ed8d8bbSJosh Poimboeuf 	update_stack_state(state, bp);
4247c7900f8SJosh Poimboeuf 
4257c7900f8SJosh Poimboeuf 	/*
4267c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
4277c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
4287c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
4297c7900f8SJosh Poimboeuf 	 */
4307c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
4317c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
432*f4f34e1bSJann Horn 			(state->next_bp == NULL && state->bp < first_frame)))
4337c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
4347c7900f8SJosh Poimboeuf }
4357c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
436