xref: /linux/arch/x86/kernel/unwind_frame.c (revision 5ed8d8bb38c5dcd78de540182cedb0fb19399aab)
17c7900f8SJosh Poimboeuf #include <linux/sched.h>
229930025SIngo Molnar #include <linux/sched/task.h>
368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
47c7900f8SJosh Poimboeuf #include <asm/ptrace.h>
57c7900f8SJosh Poimboeuf #include <asm/bitops.h>
67c7900f8SJosh Poimboeuf #include <asm/stacktrace.h>
77c7900f8SJosh Poimboeuf #include <asm/unwind.h>
87c7900f8SJosh Poimboeuf 
97c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2)
107c7900f8SJosh Poimboeuf 
1184936118SJosh Poimboeuf /*
1284936118SJosh Poimboeuf  * This disables KASAN checking when reading a value from another task's stack,
1384936118SJosh Poimboeuf  * since the other task could be running on another CPU and could have poisoned
1484936118SJosh Poimboeuf  * the stack in the meantime.
1584936118SJosh Poimboeuf  */
1684936118SJosh Poimboeuf #define READ_ONCE_TASK_STACK(task, x)			\
1784936118SJosh Poimboeuf ({							\
1884936118SJosh Poimboeuf 	unsigned long val;				\
1984936118SJosh Poimboeuf 	if (task == current)				\
2084936118SJosh Poimboeuf 		val = READ_ONCE(x);			\
2184936118SJosh Poimboeuf 	else						\
2284936118SJosh Poimboeuf 		val = READ_ONCE_NOCHECK(x);		\
2384936118SJosh Poimboeuf 	val;						\
2484936118SJosh Poimboeuf })
2584936118SJosh Poimboeuf 
268b5e99f0SJosh Poimboeuf static void unwind_dump(struct unwind_state *state, unsigned long *sp)
278b5e99f0SJosh Poimboeuf {
288b5e99f0SJosh Poimboeuf 	static bool dumped_before = false;
298b5e99f0SJosh Poimboeuf 	bool prev_zero, zero = false;
308b5e99f0SJosh Poimboeuf 	unsigned long word;
318b5e99f0SJosh Poimboeuf 
328b5e99f0SJosh Poimboeuf 	if (dumped_before)
338b5e99f0SJosh Poimboeuf 		return;
348b5e99f0SJosh Poimboeuf 
358b5e99f0SJosh Poimboeuf 	dumped_before = true;
368b5e99f0SJosh Poimboeuf 
378b5e99f0SJosh Poimboeuf 	printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
388b5e99f0SJosh Poimboeuf 			state->stack_info.type, state->stack_info.next_sp,
398b5e99f0SJosh Poimboeuf 			state->stack_mask, state->graph_idx);
408b5e99f0SJosh Poimboeuf 
418b5e99f0SJosh Poimboeuf 	for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
428b5e99f0SJosh Poimboeuf 		word = READ_ONCE_NOCHECK(*sp);
438b5e99f0SJosh Poimboeuf 
448b5e99f0SJosh Poimboeuf 		prev_zero = zero;
458b5e99f0SJosh Poimboeuf 		zero = word == 0;
468b5e99f0SJosh Poimboeuf 
478b5e99f0SJosh Poimboeuf 		if (zero) {
488b5e99f0SJosh Poimboeuf 			if (!prev_zero)
498b5e99f0SJosh Poimboeuf 				printk_deferred("%p: %016x ...\n", sp, 0);
508b5e99f0SJosh Poimboeuf 			continue;
518b5e99f0SJosh Poimboeuf 		}
528b5e99f0SJosh Poimboeuf 
538b5e99f0SJosh Poimboeuf 		printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
548b5e99f0SJosh Poimboeuf 	}
558b5e99f0SJosh Poimboeuf }
568b5e99f0SJosh Poimboeuf 
577c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
587c7900f8SJosh Poimboeuf {
597c7900f8SJosh Poimboeuf 	unsigned long addr;
607c7900f8SJosh Poimboeuf 	unsigned long *addr_p = unwind_get_return_address_ptr(state);
617c7900f8SJosh Poimboeuf 
627c7900f8SJosh Poimboeuf 	if (unwind_done(state))
637c7900f8SJosh Poimboeuf 		return 0;
647c7900f8SJosh Poimboeuf 
65946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
66946c1911SJosh Poimboeuf 		return 0;
67946c1911SJosh Poimboeuf 
6884936118SJosh Poimboeuf 	addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
6984936118SJosh Poimboeuf 	addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
707c7900f8SJosh Poimboeuf 				     addr_p);
717c7900f8SJosh Poimboeuf 
72c280f773SJosh Poimboeuf 	return __kernel_text_address(addr) ? addr : 0;
737c7900f8SJosh Poimboeuf }
747c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
757c7900f8SJosh Poimboeuf 
7624d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs)
7724d86f59SJosh Poimboeuf {
7824d86f59SJosh Poimboeuf 	/* x86_32 regs from kernel mode are two words shorter: */
7924d86f59SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
8024d86f59SJosh Poimboeuf 		return sizeof(*regs) - 2*sizeof(long);
8124d86f59SJosh Poimboeuf 
8224d86f59SJosh Poimboeuf 	return sizeof(*regs);
8324d86f59SJosh Poimboeuf }
8424d86f59SJosh Poimboeuf 
8587a6b297SJosh Poimboeuf #ifdef CONFIG_X86_32
8687a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 3
8787a6b297SJosh Poimboeuf #else
8887a6b297SJosh Poimboeuf #define GCC_REALIGN_WORDS 1
8987a6b297SJosh Poimboeuf #endif
9087a6b297SJosh Poimboeuf 
91acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
92acb4608aSJosh Poimboeuf {
9387a6b297SJosh Poimboeuf 	unsigned long *last_bp = (unsigned long *)task_pt_regs(state->task) - 2;
9487a6b297SJosh Poimboeuf 	unsigned long *aligned_bp = last_bp - GCC_REALIGN_WORDS;
95acb4608aSJosh Poimboeuf 
968023e0e2SJosh Poimboeuf 	/*
978023e0e2SJosh Poimboeuf 	 * We have to check for the last task frame at two different locations
988023e0e2SJosh Poimboeuf 	 * because gcc can occasionally decide to realign the stack pointer and
9987a6b297SJosh Poimboeuf 	 * change the offset of the stack frame in the prologue of a function
10087a6b297SJosh Poimboeuf 	 * called by head/entry code.  Examples:
10187a6b297SJosh Poimboeuf 	 *
10287a6b297SJosh Poimboeuf 	 * <start_secondary>:
10387a6b297SJosh Poimboeuf 	 *      push   %edi
10487a6b297SJosh Poimboeuf 	 *      lea    0x8(%esp),%edi
10587a6b297SJosh Poimboeuf 	 *      and    $0xfffffff8,%esp
10687a6b297SJosh Poimboeuf 	 *      pushl  -0x4(%edi)
10787a6b297SJosh Poimboeuf 	 *      push   %ebp
10887a6b297SJosh Poimboeuf 	 *      mov    %esp,%ebp
10987a6b297SJosh Poimboeuf 	 *
11087a6b297SJosh Poimboeuf 	 * <x86_64_start_kernel>:
11187a6b297SJosh Poimboeuf 	 *      lea    0x8(%rsp),%r10
11287a6b297SJosh Poimboeuf 	 *      and    $0xfffffffffffffff0,%rsp
11387a6b297SJosh Poimboeuf 	 *      pushq  -0x8(%r10)
11487a6b297SJosh Poimboeuf 	 *      push   %rbp
11587a6b297SJosh Poimboeuf 	 *      mov    %rsp,%rbp
11687a6b297SJosh Poimboeuf 	 *
11787a6b297SJosh Poimboeuf 	 * Note that after aligning the stack, it pushes a duplicate copy of
11887a6b297SJosh Poimboeuf 	 * the return address before pushing the frame pointer.
1198023e0e2SJosh Poimboeuf 	 */
12087a6b297SJosh Poimboeuf 	return (state->bp == last_bp ||
12187a6b297SJosh Poimboeuf 		(state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
122acb4608aSJosh Poimboeuf }
123acb4608aSJosh Poimboeuf 
124946c1911SJosh Poimboeuf /*
125946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
126946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
127946c1911SJosh Poimboeuf  */
128946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
129946c1911SJosh Poimboeuf {
130946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
131946c1911SJosh Poimboeuf 
132946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
133946c1911SJosh Poimboeuf 		return NULL;
134946c1911SJosh Poimboeuf 
135946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
136946c1911SJosh Poimboeuf }
137946c1911SJosh Poimboeuf 
138*5ed8d8bbSJosh Poimboeuf static bool update_stack_state(struct unwind_state *state,
139*5ed8d8bbSJosh Poimboeuf 			       unsigned long *next_bp)
1407c7900f8SJosh Poimboeuf {
1417c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
142*5ed8d8bbSJosh Poimboeuf 	enum stack_type prev_type = info->type;
143*5ed8d8bbSJosh Poimboeuf 	struct pt_regs *regs;
144*5ed8d8bbSJosh Poimboeuf 	unsigned long *frame, *prev_frame_end;
145*5ed8d8bbSJosh Poimboeuf 	size_t len;
146*5ed8d8bbSJosh Poimboeuf 
147*5ed8d8bbSJosh Poimboeuf 	if (state->regs)
148*5ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->regs + regs_size(state->regs);
149*5ed8d8bbSJosh Poimboeuf 	else
150*5ed8d8bbSJosh Poimboeuf 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
151*5ed8d8bbSJosh Poimboeuf 
152*5ed8d8bbSJosh Poimboeuf 	/* Is the next frame pointer an encoded pointer to pt_regs? */
153*5ed8d8bbSJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
154*5ed8d8bbSJosh Poimboeuf 	if (regs) {
155*5ed8d8bbSJosh Poimboeuf 		frame = (unsigned long *)regs;
156*5ed8d8bbSJosh Poimboeuf 		len = regs_size(regs);
157*5ed8d8bbSJosh Poimboeuf 	} else {
158*5ed8d8bbSJosh Poimboeuf 		frame = next_bp;
159*5ed8d8bbSJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
160*5ed8d8bbSJosh Poimboeuf 	}
1617c7900f8SJosh Poimboeuf 
1627c7900f8SJosh Poimboeuf 	/*
163*5ed8d8bbSJosh Poimboeuf 	 * If the next bp isn't on the current stack, switch to the next one.
1647c7900f8SJosh Poimboeuf 	 *
1657c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
166*5ed8d8bbSJosh Poimboeuf 	 * that info->next_sp could point to an empty stack and the next bp
167*5ed8d8bbSJosh Poimboeuf 	 * could be on a subsequent stack.
1687c7900f8SJosh Poimboeuf 	 */
169*5ed8d8bbSJosh Poimboeuf 	while (!on_stack(info, frame, len))
1707c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
1717c7900f8SJosh Poimboeuf 				   &state->stack_mask))
1727c7900f8SJosh Poimboeuf 			return false;
1737c7900f8SJosh Poimboeuf 
174*5ed8d8bbSJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
175*5ed8d8bbSJosh Poimboeuf 	if (state->orig_sp && state->stack_info.type == prev_type &&
176*5ed8d8bbSJosh Poimboeuf 	    frame < prev_frame_end)
177*5ed8d8bbSJosh Poimboeuf 		return false;
178*5ed8d8bbSJosh Poimboeuf 
179*5ed8d8bbSJosh Poimboeuf 	/* Move state to the next frame: */
180*5ed8d8bbSJosh Poimboeuf 	if (regs) {
181*5ed8d8bbSJosh Poimboeuf 		state->regs = regs;
182*5ed8d8bbSJosh Poimboeuf 		state->bp = NULL;
183*5ed8d8bbSJosh Poimboeuf 	} else {
184*5ed8d8bbSJosh Poimboeuf 		state->bp = next_bp;
185*5ed8d8bbSJosh Poimboeuf 		state->regs = NULL;
186*5ed8d8bbSJosh Poimboeuf 	}
187*5ed8d8bbSJosh Poimboeuf 
188*5ed8d8bbSJosh Poimboeuf 	/* Save the original stack pointer for unwind_dump(): */
189*5ed8d8bbSJosh Poimboeuf 	if (!state->orig_sp || info->type != prev_type)
190*5ed8d8bbSJosh Poimboeuf 		state->orig_sp = frame;
1918b5e99f0SJosh Poimboeuf 
1927c7900f8SJosh Poimboeuf 	return true;
1937c7900f8SJosh Poimboeuf }
1947c7900f8SJosh Poimboeuf 
1957c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
1967c7900f8SJosh Poimboeuf {
197946c1911SJosh Poimboeuf 	struct pt_regs *regs;
198*5ed8d8bbSJosh Poimboeuf 	unsigned long *next_bp;
1997c7900f8SJosh Poimboeuf 
2007c7900f8SJosh Poimboeuf 	if (unwind_done(state))
2017c7900f8SJosh Poimboeuf 		return false;
2027c7900f8SJosh Poimboeuf 
203*5ed8d8bbSJosh Poimboeuf 	/* Have we reached the end? */
204946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
205946c1911SJosh Poimboeuf 		goto the_end;
206946c1911SJosh Poimboeuf 
207acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
208acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
209acb4608aSJosh Poimboeuf 
210acb4608aSJosh Poimboeuf 		/*
211acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
212acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
213acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
214acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
215acb4608aSJosh Poimboeuf 		 *
216acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
217acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
218acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
219acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
220acb4608aSJosh Poimboeuf 		 */
221acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
222acb4608aSJosh Poimboeuf 			goto the_end;
223acb4608aSJosh Poimboeuf 
224acb4608aSJosh Poimboeuf 		/*
225acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
226acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
227acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
228acb4608aSJosh Poimboeuf 		 */
229acb4608aSJosh Poimboeuf 		state->regs = regs;
230acb4608aSJosh Poimboeuf 		state->bp = NULL;
231acb4608aSJosh Poimboeuf 		return true;
232acb4608aSJosh Poimboeuf 	}
233acb4608aSJosh Poimboeuf 
234*5ed8d8bbSJosh Poimboeuf 	/* Get the next frame pointer: */
235946c1911SJosh Poimboeuf 	if (state->regs)
236946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
237946c1911SJosh Poimboeuf 	else
23884936118SJosh Poimboeuf 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
2397c7900f8SJosh Poimboeuf 
240*5ed8d8bbSJosh Poimboeuf 	/* Move to the next frame if it's safe: */
241*5ed8d8bbSJosh Poimboeuf 	if (!update_stack_state(state, next_bp)) {
242c32c47c6SJosh Poimboeuf 		/*
243c32c47c6SJosh Poimboeuf 		 * Don't warn on bad regs->bp.  An interrupt in entry code
244c32c47c6SJosh Poimboeuf 		 * might cause a false positive warning.
245c32c47c6SJosh Poimboeuf 		 */
246c32c47c6SJosh Poimboeuf 		if (state->regs)
247c32c47c6SJosh Poimboeuf 			goto the_end;
248c32c47c6SJosh Poimboeuf 
249c32c47c6SJosh Poimboeuf 		goto bad_address;
250c32c47c6SJosh Poimboeuf 	}
251c32c47c6SJosh Poimboeuf 
2527c7900f8SJosh Poimboeuf 	return true;
253946c1911SJosh Poimboeuf 
254c32c47c6SJosh Poimboeuf bad_address:
255900742d8SJosh Poimboeuf 	/*
256900742d8SJosh Poimboeuf 	 * When unwinding a non-current task, the task might actually be
257900742d8SJosh Poimboeuf 	 * running on another CPU, in which case it could be modifying its
258900742d8SJosh Poimboeuf 	 * stack while we're reading it.  This is generally not a problem and
259900742d8SJosh Poimboeuf 	 * can be ignored as long as the caller understands that unwinding
260900742d8SJosh Poimboeuf 	 * another task will not always succeed.
261900742d8SJosh Poimboeuf 	 */
262900742d8SJosh Poimboeuf 	if (state->task != current)
263900742d8SJosh Poimboeuf 		goto the_end;
264900742d8SJosh Poimboeuf 
26524d86f59SJosh Poimboeuf 	if (state->regs) {
26624d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
26724d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
26824d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
269*5ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
2708b5e99f0SJosh Poimboeuf 		unwind_dump(state, (unsigned long *)state->regs);
27124d86f59SJosh Poimboeuf 	} else {
272c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
273c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
274c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
275*5ed8d8bbSJosh Poimboeuf 			state->task->pid, next_bp);
2768b5e99f0SJosh Poimboeuf 		unwind_dump(state, state->bp);
27724d86f59SJosh Poimboeuf 	}
278946c1911SJosh Poimboeuf the_end:
279946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
280946c1911SJosh Poimboeuf 	return false;
2817c7900f8SJosh Poimboeuf }
2827c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
2837c7900f8SJosh Poimboeuf 
2847c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
2857c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
2867c7900f8SJosh Poimboeuf {
287*5ed8d8bbSJosh Poimboeuf 	unsigned long *bp;
288946c1911SJosh Poimboeuf 
2897c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
2907c7900f8SJosh Poimboeuf 	state->task = task;
2917c7900f8SJosh Poimboeuf 
292*5ed8d8bbSJosh Poimboeuf 	/* Don't even attempt to start from user mode regs: */
2937c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
2947c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
2957c7900f8SJosh Poimboeuf 		return;
2967c7900f8SJosh Poimboeuf 	}
2977c7900f8SJosh Poimboeuf 
298946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
2997c7900f8SJosh Poimboeuf 
300*5ed8d8bbSJosh Poimboeuf 	/* Initialize stack info and make sure the frame data is accessible: */
301*5ed8d8bbSJosh Poimboeuf 	get_stack_info(bp, state->task, &state->stack_info,
3027c7900f8SJosh Poimboeuf 		       &state->stack_mask);
303*5ed8d8bbSJosh Poimboeuf 	update_stack_state(state, bp);
3047c7900f8SJosh Poimboeuf 
3057c7900f8SJosh Poimboeuf 	/*
3067c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
3077c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
3087c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
3097c7900f8SJosh Poimboeuf 	 */
3107c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
3117c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
3127c7900f8SJosh Poimboeuf 			state->bp < first_frame))
3137c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
3147c7900f8SJosh Poimboeuf }
3157c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
316