xref: /linux/arch/x86/kernel/unwind_frame.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27c7900f8SJosh Poimboeuf #include <linux/sched.h>
329930025SIngo Molnar #include <linux/sched/task.h>
468db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
5a8b7a923SJosh Poimboeuf #include <linux/interrupt.h>
6a8b7a923SJosh Poimboeuf #include <asm/sections.h>
77c7900f8SJosh Poimboeuf #include <asm/ptrace.h>
87c7900f8SJosh Poimboeuf #include <asm/bitops.h>
97c7900f8SJosh Poimboeuf #include <asm/stacktrace.h>
107c7900f8SJosh Poimboeuf #include <asm/unwind.h>
117c7900f8SJosh Poimboeuf 
127c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2)
137c7900f8SJosh Poimboeuf 
unwind_get_return_address(struct unwind_state * state)14ee9f8fceSJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
15ee9f8fceSJosh Poimboeuf {
16ee9f8fceSJosh Poimboeuf 	if (unwind_done(state))
17ee9f8fceSJosh Poimboeuf 		return 0;
18ee9f8fceSJosh Poimboeuf 
19ee9f8fceSJosh Poimboeuf 	return __kernel_text_address(state->ip) ? state->ip : 0;
20ee9f8fceSJosh Poimboeuf }
21ee9f8fceSJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
22ee9f8fceSJosh Poimboeuf 
unwind_get_return_address_ptr(struct unwind_state * state)23ee9f8fceSJosh Poimboeuf unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
24ee9f8fceSJosh Poimboeuf {
25ee9f8fceSJosh Poimboeuf 	if (unwind_done(state))
26ee9f8fceSJosh Poimboeuf 		return NULL;
27ee9f8fceSJosh Poimboeuf 
28ee9f8fceSJosh Poimboeuf 	return state->regs ? &state->regs->ip : state->bp + 1;
29ee9f8fceSJosh Poimboeuf }
3084936118SJosh Poimboeuf 
unwind_dump(struct unwind_state * state)31aa4f8534SJosh Poimboeuf static void unwind_dump(struct unwind_state *state)
328b5e99f0SJosh Poimboeuf {
338b5e99f0SJosh Poimboeuf 	static bool dumped_before = false;
348b5e99f0SJosh Poimboeuf 	bool prev_zero, zero = false;
35aa4f8534SJosh Poimboeuf 	unsigned long word, *sp;
36262fa734SJosh Poimboeuf 	struct stack_info stack_info = {0};
37262fa734SJosh Poimboeuf 	unsigned long visit_mask = 0;
388b5e99f0SJosh Poimboeuf 
398b5e99f0SJosh Poimboeuf 	if (dumped_before)
408b5e99f0SJosh Poimboeuf 		return;
418b5e99f0SJosh Poimboeuf 
428b5e99f0SJosh Poimboeuf 	dumped_before = true;
438b5e99f0SJosh Poimboeuf 
444ea3d741SJosh Poimboeuf 	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
458b5e99f0SJosh Poimboeuf 			state->stack_info.type, state->stack_info.next_sp,
468b5e99f0SJosh Poimboeuf 			state->stack_mask, state->graph_idx);
478b5e99f0SJosh Poimboeuf 
4899bd28a4SJosh Poimboeuf 	for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
4999bd28a4SJosh Poimboeuf 	     sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
50262fa734SJosh Poimboeuf 		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
51262fa734SJosh Poimboeuf 			break;
52262fa734SJosh Poimboeuf 
53262fa734SJosh Poimboeuf 		for (; sp < stack_info.end; sp++) {
54262fa734SJosh Poimboeuf 
558b5e99f0SJosh Poimboeuf 			word = READ_ONCE_NOCHECK(*sp);
568b5e99f0SJosh Poimboeuf 
578b5e99f0SJosh Poimboeuf 			prev_zero = zero;
588b5e99f0SJosh Poimboeuf 			zero = word == 0;
598b5e99f0SJosh Poimboeuf 
608b5e99f0SJosh Poimboeuf 			if (zero) {
618b5e99f0SJosh Poimboeuf 				if (!prev_zero)
629b135b23SJosh Poimboeuf 					printk_deferred("%p: %0*x ...\n",
639b135b23SJosh Poimboeuf 							sp, BITS_PER_LONG/4, 0);
648b5e99f0SJosh Poimboeuf 				continue;
658b5e99f0SJosh Poimboeuf 			}
668b5e99f0SJosh Poimboeuf 
679b135b23SJosh Poimboeuf 			printk_deferred("%p: %0*lx (%pB)\n",
689b135b23SJosh Poimboeuf 					sp, BITS_PER_LONG/4, word, (void *)word);
698b5e99f0SJosh Poimboeuf 		}
708b5e99f0SJosh Poimboeuf 	}
71262fa734SJosh Poimboeuf }
728b5e99f0SJosh Poimboeuf 
in_entry_code(unsigned long ip)73a8b7a923SJosh Poimboeuf static bool in_entry_code(unsigned long ip)
74a8b7a923SJosh Poimboeuf {
75a8b7a923SJosh Poimboeuf 	char *addr = (char *)ip;
76a8b7a923SJosh Poimboeuf 
77f0178fc0SThomas Gleixner 	return addr >= __entry_text_start && addr < __entry_text_end;
78a8b7a923SJosh Poimboeuf }
79a8b7a923SJosh Poimboeuf 
last_frame(struct unwind_state * state)80b0d50c7bSJosh Poimboeuf static inline unsigned long *last_frame(struct unwind_state *state)
81b0d50c7bSJosh Poimboeuf {
82b0d50c7bSJosh Poimboeuf 	return (unsigned long *)task_pt_regs(state->task) - 2;
83b0d50c7bSJosh Poimboeuf }
84b0d50c7bSJosh Poimboeuf 
is_last_frame(struct unwind_state * state)85519fb5c3SJosh Poimboeuf static bool is_last_frame(struct unwind_state *state)
86519fb5c3SJosh Poimboeuf {
87519fb5c3SJosh Poimboeuf 	return state->bp == last_frame(state);
88519fb5c3SJosh Poimboeuf }
89519fb5c3SJosh Poimboeuf 
9087a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32
9187a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3
9287a6b297SJosh Poimboeuf #else
9387a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1
9487a6b297SJosh Poimboeuf #endif
9587a6b297SJosh Poimboeuf 
last_aligned_frame(struct unwind_state * state)96b0d50c7bSJosh Poimboeuf static inline unsigned long *last_aligned_frame(struct unwind_state *state)
97b0d50c7bSJosh Poimboeuf {
98b0d50c7bSJosh Poimboeuf 	return last_frame(state) - GCC_REALIGN_WORDS;
99b0d50c7bSJosh Poimboeuf }
100b0d50c7bSJosh Poimboeuf 
is_last_aligned_frame(struct unwind_state * state)101519fb5c3SJosh Poimboeuf static bool is_last_aligned_frame(struct unwind_state *state)
102acb4608aSJosh Poimboeuf {
103b0d50c7bSJosh Poimboeuf 	unsigned long *last_bp = last_frame(state);
104b0d50c7bSJosh Poimboeuf 	unsigned long *aligned_bp = last_aligned_frame(state);
105acb4608aSJosh Poimboeuf 
1068023e0e2SJosh Poimboeuf 	/*
107519fb5c3SJosh Poimboeuf 	 * GCC can occasionally decide to realign the stack pointer and change
108519fb5c3SJosh Poimboeuf 	 * the offset of the stack frame in the prologue of a function called
109519fb5c3SJosh Poimboeuf 	 * by head/entry code.  Examples:
11087a6b297SJosh Poimboeuf 	 *
11187a6b297SJosh Poimboeuf 	 * <start_secondary>:
11287a6b297SJosh Poimboeuf 	 *      push   %edi
11387a6b297SJosh Poimboeuf 	 *      lea    0x8(%esp),%edi
11487a6b297SJosh Poimboeuf 	 *      and    $0xfffffff8,%esp
11587a6b297SJosh Poimboeuf 	 *      pushl  -0x4(%edi)
11687a6b297SJosh Poimboeuf 	 *      push   %ebp
11787a6b297SJosh Poimboeuf 	 *      mov    %esp,%ebp
11887a6b297SJosh Poimboeuf 	 *
11987a6b297SJosh Poimboeuf 	 * <x86_64_start_kernel>:
12087a6b297SJosh Poimboeuf 	 *      lea    0x8(%rsp),%r10
12187a6b297SJosh Poimboeuf 	 *      and    $0xfffffffffffffff0,%rsp
12287a6b297SJosh Poimboeuf 	 *      pushq  -0x8(%r10)
12387a6b297SJosh Poimboeuf 	 *      push   %rbp
12487a6b297SJosh Poimboeuf 	 *      mov    %rsp,%rbp
12587a6b297SJosh Poimboeuf 	 *
126519fb5c3SJosh Poimboeuf 	 * After aligning the stack, it pushes a duplicate copy of the return
127519fb5c3SJosh Poimboeuf 	 * address before pushing the frame pointer.
1288023e0e2SJosh Poimboeuf 	 */
129519fb5c3SJosh Poimboeuf 	return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
130519fb5c3SJosh Poimboeuf }
131519fb5c3SJosh Poimboeuf 
is_last_ftrace_frame(struct unwind_state * state)132519fb5c3SJosh Poimboeuf static bool is_last_ftrace_frame(struct unwind_state *state)
133519fb5c3SJosh Poimboeuf {
134519fb5c3SJosh Poimboeuf 	unsigned long *last_bp = last_frame(state);
135519fb5c3SJosh Poimboeuf 	unsigned long *last_ftrace_bp = last_bp - 3;
136519fb5c3SJosh Poimboeuf 
137519fb5c3SJosh Poimboeuf 	/*
138519fb5c3SJosh Poimboeuf 	 * When unwinding from an ftrace handler of a function called by entry
139519fb5c3SJosh Poimboeuf 	 * code, the stack layout of the last frame is:
140519fb5c3SJosh Poimboeuf 	 *
141519fb5c3SJosh Poimboeuf 	 *   bp
142519fb5c3SJosh Poimboeuf 	 *   parent ret addr
143519fb5c3SJosh Poimboeuf 	 *   bp
144519fb5c3SJosh Poimboeuf 	 *   function ret addr
145519fb5c3SJosh Poimboeuf 	 *   parent ret addr
146519fb5c3SJosh Poimboeuf 	 *   pt_regs
147519fb5c3SJosh Poimboeuf 	 *   -----------------
148519fb5c3SJosh Poimboeuf 	 */
149519fb5c3SJosh Poimboeuf 	return (state->bp == last_ftrace_bp &&
150519fb5c3SJosh Poimboeuf 		*state->bp == *(state->bp + 2) &&
151519fb5c3SJosh Poimboeuf 		*(state->bp + 1) == *(state->bp + 4));
152519fb5c3SJosh Poimboeuf }
153519fb5c3SJosh Poimboeuf 
is_last_task_frame(struct unwind_state * state)154519fb5c3SJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
155519fb5c3SJosh Poimboeuf {
156519fb5c3SJosh Poimboeuf 	return is_last_frame(state) || is_last_aligned_frame(state) ||
157519fb5c3SJosh Poimboeuf 	       is_last_ftrace_frame(state);
158acb4608aSJosh Poimboeuf }
159acb4608aSJosh Poimboeuf 
160946c1911SJosh Poimboeuf /*
161946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
162946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
163946c1911SJosh Poimboeuf  */
1645c99b692SJosh Poimboeuf #ifdef CONFIG_X86_64
decode_frame_pointer(unsigned long * bp)165946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
166946c1911SJosh Poimboeuf {
167946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
168946c1911SJosh Poimboeuf 
169946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
170946c1911SJosh Poimboeuf 		return NULL;
171946c1911SJosh Poimboeuf 
172946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
173946c1911SJosh Poimboeuf }
1745c99b692SJosh Poimboeuf #else
decode_frame_pointer(unsigned long * bp)1755c99b692SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
1765c99b692SJosh Poimboeuf {
1775c99b692SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
1785c99b692SJosh Poimboeuf 
1795c99b692SJosh Poimboeuf 	if (regs & 0x80000000)
1805c99b692SJosh Poimboeuf 		return NULL;
1815c99b692SJosh Poimboeuf 
1825c99b692SJosh Poimboeuf 	return (struct pt_regs *)(regs | 0x80000000);
1835c99b692SJosh Poimboeuf }
1845c99b692SJosh Poimboeuf #endif
185946c1911SJosh Poimboeuf 
186*37ad4ee8SAlexander Potapenko /*
187*37ad4ee8SAlexander Potapenko  * While walking the stack, KMSAN may stomp on stale locals from other
188*37ad4ee8SAlexander Potapenko  * functions that were marked as uninitialized upon function exit, and
189*37ad4ee8SAlexander Potapenko  * now hold the call frame information for the current function (e.g. the frame
190*37ad4ee8SAlexander Potapenko  * pointer). Because KMSAN does not specifically mark call frames as
191*37ad4ee8SAlexander Potapenko  * initialized, false positive reports are possible. To prevent such reports,
192*37ad4ee8SAlexander Potapenko  * we mark the functions scanning the stack (here and below) with
193*37ad4ee8SAlexander Potapenko  * __no_kmsan_checks.
194*37ad4ee8SAlexander Potapenko  */
195*37ad4ee8SAlexander Potapenko __no_kmsan_checks
update_stack_state(struct unwind_state * state,unsigned long * next_bp)1965ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state,
1975ed8d8bbSJosh Poimboeuf 			       unsigned long *next_bp)
1987c7900f8SJosh Poimboeuf {
1997c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
2005ed8d8bbSJosh Poimboeuf 	enum stack_type prev_type = info->type;
2015ed8d8bbSJosh Poimboeuf 	struct pt_regs *regs;
2026bcdf9d5SJosh Poimboeuf 	unsigned long *frame, *prev_frame_end, *addr_p, addr;
2035ed8d8bbSJosh Poimboeuf 	size_t len;
2045ed8d8bbSJosh Poimboeuf 
2055ed8d8bbSJosh Poimboeuf 	if (state->regs)
2063c88c692SPeter Zijlstra 		prev_frame_end = (void *)state->regs + sizeof(*state->regs);
2075ed8d8bbSJosh Poimboeuf 	else
2085ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
2095ed8d8bbSJosh Poimboeuf 
2105ed8d8bbSJosh Poimboeuf 	/* Is the next frame pointer an encoded pointer to pt_regs? */
2115ed8d8bbSJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
2125ed8d8bbSJosh Poimboeuf 	if (regs) {
2135ed8d8bbSJosh Poimboeuf 		frame = (unsigned long *)regs;
2143c88c692SPeter Zijlstra 		len = sizeof(*regs);
215a8b7a923SJosh Poimboeuf 		state->got_irq = true;
2165ed8d8bbSJosh Poimboeuf 	} else {
2175ed8d8bbSJosh Poimboeuf 		frame = next_bp;
2185ed8d8bbSJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
2195ed8d8bbSJosh Poimboeuf 	}
2207c7900f8SJosh Poimboeuf 
2217c7900f8SJosh Poimboeuf 	/*
2225ed8d8bbSJosh Poimboeuf 	 * If the next bp isn't on the current stack, switch to the next one.
2237c7900f8SJosh Poimboeuf 	 *
2247c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
2255ed8d8bbSJosh Poimboeuf 	 * that info->next_sp could point to an empty stack and the next bp
2265ed8d8bbSJosh Poimboeuf 	 * could be on a subsequent stack.
2277c7900f8SJosh Poimboeuf 	 */
2285ed8d8bbSJosh Poimboeuf 	while (!on_stack(info, frame, len))
2297c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
2307c7900f8SJosh Poimboeuf 				   &state->stack_mask))
2317c7900f8SJosh Poimboeuf 			return false;
2327c7900f8SJosh Poimboeuf 
2335ed8d8bbSJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
2345ed8d8bbSJosh Poimboeuf 	if (state->orig_sp && state->stack_info.type == prev_type &&
2355ed8d8bbSJosh Poimboeuf 	    frame < prev_frame_end)
2365ed8d8bbSJosh Poimboeuf 		return false;
2375ed8d8bbSJosh Poimboeuf 
2385ed8d8bbSJosh Poimboeuf 	/* Move state to the next frame: */
2395ed8d8bbSJosh Poimboeuf 	if (regs) {
2405ed8d8bbSJosh Poimboeuf 		state->regs = regs;
2415ed8d8bbSJosh Poimboeuf 		state->bp = NULL;
2425ed8d8bbSJosh Poimboeuf 	} else {
2435ed8d8bbSJosh Poimboeuf 		state->bp = next_bp;
2445ed8d8bbSJosh Poimboeuf 		state->regs = NULL;
2455ed8d8bbSJosh Poimboeuf 	}
2465ed8d8bbSJosh Poimboeuf 
2476bcdf9d5SJosh Poimboeuf 	/* Save the return address: */
2486bcdf9d5SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
2496bcdf9d5SJosh Poimboeuf 		state->ip = 0;
2506bcdf9d5SJosh Poimboeuf 	else {
2516bcdf9d5SJosh Poimboeuf 		addr_p = unwind_get_return_address_ptr(state);
2526bcdf9d5SJosh Poimboeuf 		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
25319138af1SMasami Hiramatsu 		state->ip = unwind_recover_ret_addr(state, addr, addr_p);
2546bcdf9d5SJosh Poimboeuf 	}
2556bcdf9d5SJosh Poimboeuf 
2565ed8d8bbSJosh Poimboeuf 	/* Save the original stack pointer for unwind_dump(): */
257262fa734SJosh Poimboeuf 	if (!state->orig_sp)
2585ed8d8bbSJosh Poimboeuf 		state->orig_sp = frame;
2598b5e99f0SJosh Poimboeuf 
2607c7900f8SJosh Poimboeuf 	return true;
2617c7900f8SJosh Poimboeuf }
2627c7900f8SJosh Poimboeuf 
263*37ad4ee8SAlexander Potapenko __no_kmsan_checks
unwind_next_frame(struct unwind_state * state)2647c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
2657c7900f8SJosh Poimboeuf {
266946c1911SJosh Poimboeuf 	struct pt_regs *regs;
2675ed8d8bbSJosh Poimboeuf 	unsigned long *next_bp;
2687c7900f8SJosh Poimboeuf 
2697c7900f8SJosh Poimboeuf 	if (unwind_done(state))
2707c7900f8SJosh Poimboeuf 		return false;
2717c7900f8SJosh Poimboeuf 
2725ed8d8bbSJosh Poimboeuf 	/* Have we reached the end? */
273946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
274946c1911SJosh Poimboeuf 		goto the_end;
275946c1911SJosh Poimboeuf 
276acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
277acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
278acb4608aSJosh Poimboeuf 
279acb4608aSJosh Poimboeuf 		/*
280acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
281acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
282714acdbdSChristian Brauner 		 * there by copy_thread().  But the regs don't have any
283acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
284acb4608aSJosh Poimboeuf 		 *
285acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
286acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
287acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
288be619f7fSEric W. Biederman 		 * kernel_execve(), which eventually clears PF_KTHREAD.
289acb4608aSJosh Poimboeuf 		 */
290acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
291acb4608aSJosh Poimboeuf 			goto the_end;
292acb4608aSJosh Poimboeuf 
293acb4608aSJosh Poimboeuf 		/*
294acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
295acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
296acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
297acb4608aSJosh Poimboeuf 		 */
298acb4608aSJosh Poimboeuf 		state->regs = regs;
299acb4608aSJosh Poimboeuf 		state->bp = NULL;
3006bcdf9d5SJosh Poimboeuf 		state->ip = 0;
301acb4608aSJosh Poimboeuf 		return true;
302acb4608aSJosh Poimboeuf 	}
303acb4608aSJosh Poimboeuf 
3045ed8d8bbSJosh Poimboeuf 	/* Get the next frame pointer: */
305f4f34e1bSJann Horn 	if (state->next_bp) {
306f4f34e1bSJann Horn 		next_bp = state->next_bp;
307f4f34e1bSJann Horn 		state->next_bp = NULL;
308f4f34e1bSJann Horn 	} else if (state->regs) {
309946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
310f4f34e1bSJann Horn 	} else {
31184936118SJosh Poimboeuf 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
312f4f34e1bSJann Horn 	}
3137c7900f8SJosh Poimboeuf 
3145ed8d8bbSJosh Poimboeuf 	/* Move to the next frame if it's safe: */
315a8b7a923SJosh Poimboeuf 	if (!update_stack_state(state, next_bp))
316c32c47c6SJosh Poimboeuf 		goto bad_address;
317c32c47c6SJosh Poimboeuf 
3187c7900f8SJosh Poimboeuf 	return true;
319946c1911SJosh Poimboeuf 
320c32c47c6SJosh Poimboeuf bad_address:
321af085d90SJosh Poimboeuf 	state->error = true;
322af085d90SJosh Poimboeuf 
323900742d8SJosh Poimboeuf 	/*
324900742d8SJosh Poimboeuf 	 * When unwinding a non-current task, the task might actually be
325900742d8SJosh Poimboeuf 	 * running on another CPU, in which case it could be modifying its
326900742d8SJosh Poimboeuf 	 * stack while we're reading it.  This is generally not a problem and
327900742d8SJosh Poimboeuf 	 * can be ignored as long as the caller understands that unwinding
328900742d8SJosh Poimboeuf 	 * another task will not always succeed.
329900742d8SJosh Poimboeuf 	 */
330900742d8SJosh Poimboeuf 	if (state->task != current)
331900742d8SJosh Poimboeuf 		goto the_end;
332900742d8SJosh Poimboeuf 
333a8b7a923SJosh Poimboeuf 	/*
334a8b7a923SJosh Poimboeuf 	 * Don't warn if the unwinder got lost due to an interrupt in entry
335b0d50c7bSJosh Poimboeuf 	 * code or in the C handler before the first frame pointer got set up:
336a8b7a923SJosh Poimboeuf 	 */
337a8b7a923SJosh Poimboeuf 	if (state->got_irq && in_entry_code(state->ip))
338a8b7a923SJosh Poimboeuf 		goto the_end;
339b0d50c7bSJosh Poimboeuf 	if (state->regs &&
340b0d50c7bSJosh Poimboeuf 	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
341b0d50c7bSJosh Poimboeuf 	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
342b0d50c7bSJosh Poimboeuf 		goto the_end;
343a8b7a923SJosh Poimboeuf 
344d4a2d031SJosh Poimboeuf 	/*
345d4a2d031SJosh Poimboeuf 	 * There are some known frame pointer issues on 32-bit.  Disable
346d4a2d031SJosh Poimboeuf 	 * unwinder warnings on 32-bit until it gets objtool support.
347d4a2d031SJosh Poimboeuf 	 */
348d4a2d031SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32))
349d4a2d031SJosh Poimboeuf 		goto the_end;
350d4a2d031SJosh Poimboeuf 
351b08418b5SJosh Poimboeuf 	if (state->task != current)
352b08418b5SJosh Poimboeuf 		goto the_end;
353b08418b5SJosh Poimboeuf 
35424d86f59SJosh Poimboeuf 	if (state->regs) {
35524d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
35624d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
35724d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
3585ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
359aa4f8534SJosh Poimboeuf 		unwind_dump(state);
36024d86f59SJosh Poimboeuf 	} else {
361c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
362c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
363c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
3645ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
365aa4f8534SJosh Poimboeuf 		unwind_dump(state);
36624d86f59SJosh Poimboeuf 	}
367946c1911SJosh Poimboeuf the_end:
368946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
369946c1911SJosh Poimboeuf 	return false;
3707c7900f8SJosh Poimboeuf }
3717c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
3727c7900f8SJosh Poimboeuf 
__unwind_start(struct unwind_state * state,struct task_struct * task,struct pt_regs * regs,unsigned long * first_frame)3737c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
3747c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
3757c7900f8SJosh Poimboeuf {
3765ed8d8bbSJosh Poimboeuf 	unsigned long *bp;
377946c1911SJosh Poimboeuf 
3787c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
3797c7900f8SJosh Poimboeuf 	state->task = task;
380a8b7a923SJosh Poimboeuf 	state->got_irq = (regs);
3817c7900f8SJosh Poimboeuf 
3825ed8d8bbSJosh Poimboeuf 	/* Don't even attempt to start from user mode regs: */
3837c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
3847c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
3857c7900f8SJosh Poimboeuf 		return;
3867c7900f8SJosh Poimboeuf 	}
3877c7900f8SJosh Poimboeuf 
388946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
3897c7900f8SJosh Poimboeuf 
390f4f34e1bSJann Horn 	/*
391f4f34e1bSJann Horn 	 * If we crash with IP==0, the last successfully executed instruction
392f4f34e1bSJann Horn 	 * was probably an indirect function call with a NULL function pointer.
393f4f34e1bSJann Horn 	 * That means that SP points into the middle of an incomplete frame:
394f4f34e1bSJann Horn 	 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
395f4f34e1bSJann Horn 	 * would have written a frame pointer if we hadn't crashed.
396f4f34e1bSJann Horn 	 * Pretend that the frame is complete and that BP points to it, but save
397f4f34e1bSJann Horn 	 * the real BP so that we can use it when looking for the next frame.
398f4f34e1bSJann Horn 	 */
3993c88c692SPeter Zijlstra 	if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
400f4f34e1bSJann Horn 		state->next_bp = bp;
4013c88c692SPeter Zijlstra 		bp = ((unsigned long *)regs->sp) - 1;
402f4f34e1bSJann Horn 	}
403f4f34e1bSJann Horn 
4045ed8d8bbSJosh Poimboeuf 	/* Initialize stack info and make sure the frame data is accessible: */
4055ed8d8bbSJosh Poimboeuf 	get_stack_info(bp, state->task, &state->stack_info,
4067c7900f8SJosh Poimboeuf 		       &state->stack_mask);
4075ed8d8bbSJosh Poimboeuf 	update_stack_state(state, bp);
4087c7900f8SJosh Poimboeuf 
4097c7900f8SJosh Poimboeuf 	/*
4107c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
4117c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
4127c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
4137c7900f8SJosh Poimboeuf 	 */
4147c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
4157c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
416f4f34e1bSJann Horn 			(state->next_bp == NULL && state->bp < first_frame)))
4177c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
4187c7900f8SJosh Poimboeuf }
4197c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
420