xref: /linux/arch/x86/kernel/unwind_frame.c (revision 84936118bdf37bda513d4a361c38181a216427e0)
17c7900f8SJosh Poimboeuf #include <linux/sched.h>
27c7900f8SJosh Poimboeuf #include <asm/ptrace.h>
37c7900f8SJosh Poimboeuf #include <asm/bitops.h>
47c7900f8SJosh Poimboeuf #include <asm/stacktrace.h>
57c7900f8SJosh Poimboeuf #include <asm/unwind.h>
67c7900f8SJosh Poimboeuf 
77c7900f8SJosh Poimboeuf #define FRAME_HEADER_SIZE (sizeof(long) * 2)
87c7900f8SJosh Poimboeuf 
9*84936118SJosh Poimboeuf /*
10*84936118SJosh Poimboeuf  * This disables KASAN checking when reading a value from another task's stack,
11*84936118SJosh Poimboeuf  * since the other task could be running on another CPU and could have poisoned
12*84936118SJosh Poimboeuf  * the stack in the meantime.
13*84936118SJosh Poimboeuf  */
14*84936118SJosh Poimboeuf #define READ_ONCE_TASK_STACK(task, x)			\
15*84936118SJosh Poimboeuf ({							\
16*84936118SJosh Poimboeuf 	unsigned long val;				\
17*84936118SJosh Poimboeuf 	if (task == current)				\
18*84936118SJosh Poimboeuf 		val = READ_ONCE(x);			\
19*84936118SJosh Poimboeuf 	else						\
20*84936118SJosh Poimboeuf 		val = READ_ONCE_NOCHECK(x);		\
21*84936118SJosh Poimboeuf 	val;						\
22*84936118SJosh Poimboeuf })
23*84936118SJosh Poimboeuf 
248b5e99f0SJosh Poimboeuf static void unwind_dump(struct unwind_state *state, unsigned long *sp)
258b5e99f0SJosh Poimboeuf {
268b5e99f0SJosh Poimboeuf 	static bool dumped_before = false;
278b5e99f0SJosh Poimboeuf 	bool prev_zero, zero = false;
288b5e99f0SJosh Poimboeuf 	unsigned long word;
298b5e99f0SJosh Poimboeuf 
308b5e99f0SJosh Poimboeuf 	if (dumped_before)
318b5e99f0SJosh Poimboeuf 		return;
328b5e99f0SJosh Poimboeuf 
338b5e99f0SJosh Poimboeuf 	dumped_before = true;
348b5e99f0SJosh Poimboeuf 
358b5e99f0SJosh Poimboeuf 	printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
368b5e99f0SJosh Poimboeuf 			state->stack_info.type, state->stack_info.next_sp,
378b5e99f0SJosh Poimboeuf 			state->stack_mask, state->graph_idx);
388b5e99f0SJosh Poimboeuf 
398b5e99f0SJosh Poimboeuf 	for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
408b5e99f0SJosh Poimboeuf 		word = READ_ONCE_NOCHECK(*sp);
418b5e99f0SJosh Poimboeuf 
428b5e99f0SJosh Poimboeuf 		prev_zero = zero;
438b5e99f0SJosh Poimboeuf 		zero = word == 0;
448b5e99f0SJosh Poimboeuf 
458b5e99f0SJosh Poimboeuf 		if (zero) {
468b5e99f0SJosh Poimboeuf 			if (!prev_zero)
478b5e99f0SJosh Poimboeuf 				printk_deferred("%p: %016x ...\n", sp, 0);
488b5e99f0SJosh Poimboeuf 			continue;
498b5e99f0SJosh Poimboeuf 		}
508b5e99f0SJosh Poimboeuf 
518b5e99f0SJosh Poimboeuf 		printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
528b5e99f0SJosh Poimboeuf 	}
538b5e99f0SJosh Poimboeuf }
548b5e99f0SJosh Poimboeuf 
557c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
567c7900f8SJosh Poimboeuf {
577c7900f8SJosh Poimboeuf 	unsigned long addr;
587c7900f8SJosh Poimboeuf 	unsigned long *addr_p = unwind_get_return_address_ptr(state);
597c7900f8SJosh Poimboeuf 
607c7900f8SJosh Poimboeuf 	if (unwind_done(state))
617c7900f8SJosh Poimboeuf 		return 0;
627c7900f8SJosh Poimboeuf 
63946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
64946c1911SJosh Poimboeuf 		return 0;
65946c1911SJosh Poimboeuf 
66*84936118SJosh Poimboeuf 	addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
67*84936118SJosh Poimboeuf 	addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, addr,
687c7900f8SJosh Poimboeuf 				     addr_p);
697c7900f8SJosh Poimboeuf 
70c280f773SJosh Poimboeuf 	return __kernel_text_address(addr) ? addr : 0;
717c7900f8SJosh Poimboeuf }
727c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
737c7900f8SJosh Poimboeuf 
7424d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs)
7524d86f59SJosh Poimboeuf {
7624d86f59SJosh Poimboeuf 	/* x86_32 regs from kernel mode are two words shorter: */
7724d86f59SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
7824d86f59SJosh Poimboeuf 		return sizeof(*regs) - 2*sizeof(long);
7924d86f59SJosh Poimboeuf 
8024d86f59SJosh Poimboeuf 	return sizeof(*regs);
8124d86f59SJosh Poimboeuf }
8224d86f59SJosh Poimboeuf 
83acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
84acb4608aSJosh Poimboeuf {
85acb4608aSJosh Poimboeuf 	unsigned long bp = (unsigned long)state->bp;
86acb4608aSJosh Poimboeuf 	unsigned long regs = (unsigned long)task_pt_regs(state->task);
87acb4608aSJosh Poimboeuf 
888023e0e2SJosh Poimboeuf 	/*
898023e0e2SJosh Poimboeuf 	 * We have to check for the last task frame at two different locations
908023e0e2SJosh Poimboeuf 	 * because gcc can occasionally decide to realign the stack pointer and
918023e0e2SJosh Poimboeuf 	 * change the offset of the stack frame by a word in the prologue of a
928023e0e2SJosh Poimboeuf 	 * function called by head/entry code.
938023e0e2SJosh Poimboeuf 	 */
948023e0e2SJosh Poimboeuf 	return bp == regs - FRAME_HEADER_SIZE ||
958023e0e2SJosh Poimboeuf 	       bp == regs - FRAME_HEADER_SIZE - sizeof(long);
96acb4608aSJosh Poimboeuf }
97acb4608aSJosh Poimboeuf 
98946c1911SJosh Poimboeuf /*
99946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
100946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
101946c1911SJosh Poimboeuf  */
102946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
103946c1911SJosh Poimboeuf {
104946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
105946c1911SJosh Poimboeuf 
106946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
107946c1911SJosh Poimboeuf 		return NULL;
108946c1911SJosh Poimboeuf 
109946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
110946c1911SJosh Poimboeuf }
111946c1911SJosh Poimboeuf 
1127c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr,
1137c7900f8SJosh Poimboeuf 			       size_t len)
1147c7900f8SJosh Poimboeuf {
1157c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
1168b5e99f0SJosh Poimboeuf 	enum stack_type orig_type = info->type;
1177c7900f8SJosh Poimboeuf 
1187c7900f8SJosh Poimboeuf 	/*
1197c7900f8SJosh Poimboeuf 	 * If addr isn't on the current stack, switch to the next one.
1207c7900f8SJosh Poimboeuf 	 *
1217c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
1227c7900f8SJosh Poimboeuf 	 * that 'info->next_sp' could point to an empty stack and 'addr' could
1237c7900f8SJosh Poimboeuf 	 * be on a subsequent stack.
1247c7900f8SJosh Poimboeuf 	 */
1257c7900f8SJosh Poimboeuf 	while (!on_stack(info, addr, len))
1267c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
1277c7900f8SJosh Poimboeuf 				   &state->stack_mask))
1287c7900f8SJosh Poimboeuf 			return false;
1297c7900f8SJosh Poimboeuf 
1308b5e99f0SJosh Poimboeuf 	if (!state->orig_sp || info->type != orig_type)
1318b5e99f0SJosh Poimboeuf 		state->orig_sp = addr;
1328b5e99f0SJosh Poimboeuf 
1337c7900f8SJosh Poimboeuf 	return true;
1347c7900f8SJosh Poimboeuf }
1357c7900f8SJosh Poimboeuf 
1367c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
1377c7900f8SJosh Poimboeuf {
138946c1911SJosh Poimboeuf 	struct pt_regs *regs;
139946c1911SJosh Poimboeuf 	unsigned long *next_bp, *next_frame;
140946c1911SJosh Poimboeuf 	size_t next_len;
14124d86f59SJosh Poimboeuf 	enum stack_type prev_type = state->stack_info.type;
1427c7900f8SJosh Poimboeuf 
1437c7900f8SJosh Poimboeuf 	if (unwind_done(state))
1447c7900f8SJosh Poimboeuf 		return false;
1457c7900f8SJosh Poimboeuf 
146946c1911SJosh Poimboeuf 	/* have we reached the end? */
147946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
148946c1911SJosh Poimboeuf 		goto the_end;
149946c1911SJosh Poimboeuf 
150acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
151acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
152acb4608aSJosh Poimboeuf 
153acb4608aSJosh Poimboeuf 		/*
154acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
155acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
156acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
157acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
158acb4608aSJosh Poimboeuf 		 *
159acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
160acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
161acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
162acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
163acb4608aSJosh Poimboeuf 		 */
164acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
165acb4608aSJosh Poimboeuf 			goto the_end;
166acb4608aSJosh Poimboeuf 
167acb4608aSJosh Poimboeuf 		/*
168acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
169acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
170acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
171acb4608aSJosh Poimboeuf 		 */
172acb4608aSJosh Poimboeuf 		state->regs = regs;
173acb4608aSJosh Poimboeuf 		state->bp = NULL;
174acb4608aSJosh Poimboeuf 		return true;
175acb4608aSJosh Poimboeuf 	}
176acb4608aSJosh Poimboeuf 
177946c1911SJosh Poimboeuf 	/* get the next frame pointer */
178946c1911SJosh Poimboeuf 	if (state->regs)
179946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
180946c1911SJosh Poimboeuf 	else
181*84936118SJosh Poimboeuf 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task,*state->bp);
1827c7900f8SJosh Poimboeuf 
183946c1911SJosh Poimboeuf 	/* is the next frame pointer an encoded pointer to pt_regs? */
184946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
185946c1911SJosh Poimboeuf 	if (regs) {
186946c1911SJosh Poimboeuf 		next_frame = (unsigned long *)regs;
187946c1911SJosh Poimboeuf 		next_len = sizeof(*regs);
188946c1911SJosh Poimboeuf 	} else {
189946c1911SJosh Poimboeuf 		next_frame = next_bp;
190946c1911SJosh Poimboeuf 		next_len = FRAME_HEADER_SIZE;
191946c1911SJosh Poimboeuf 	}
1927c7900f8SJosh Poimboeuf 
193946c1911SJosh Poimboeuf 	/* make sure the next frame's data is accessible */
194c32c47c6SJosh Poimboeuf 	if (!update_stack_state(state, next_frame, next_len)) {
195c32c47c6SJosh Poimboeuf 		/*
196c32c47c6SJosh Poimboeuf 		 * Don't warn on bad regs->bp.  An interrupt in entry code
197c32c47c6SJosh Poimboeuf 		 * might cause a false positive warning.
198c32c47c6SJosh Poimboeuf 		 */
199c32c47c6SJosh Poimboeuf 		if (state->regs)
200c32c47c6SJosh Poimboeuf 			goto the_end;
201c32c47c6SJosh Poimboeuf 
202c32c47c6SJosh Poimboeuf 		goto bad_address;
203c32c47c6SJosh Poimboeuf 	}
204c32c47c6SJosh Poimboeuf 
20524d86f59SJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the last frame: */
20624d86f59SJosh Poimboeuf 	if (state->stack_info.type == prev_type) {
20724d86f59SJosh Poimboeuf 		if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
20824d86f59SJosh Poimboeuf 			goto bad_address;
20924d86f59SJosh Poimboeuf 
21024d86f59SJosh Poimboeuf 		if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
21124d86f59SJosh Poimboeuf 			goto bad_address;
21224d86f59SJosh Poimboeuf 	}
21324d86f59SJosh Poimboeuf 
2147c7900f8SJosh Poimboeuf 	/* move to the next frame */
215946c1911SJosh Poimboeuf 	if (regs) {
216946c1911SJosh Poimboeuf 		state->regs = regs;
217946c1911SJosh Poimboeuf 		state->bp = NULL;
218946c1911SJosh Poimboeuf 	} else {
2197c7900f8SJosh Poimboeuf 		state->bp = next_bp;
220946c1911SJosh Poimboeuf 		state->regs = NULL;
221946c1911SJosh Poimboeuf 	}
222946c1911SJosh Poimboeuf 
2237c7900f8SJosh Poimboeuf 	return true;
224946c1911SJosh Poimboeuf 
225c32c47c6SJosh Poimboeuf bad_address:
226900742d8SJosh Poimboeuf 	/*
227900742d8SJosh Poimboeuf 	 * When unwinding a non-current task, the task might actually be
228900742d8SJosh Poimboeuf 	 * running on another CPU, in which case it could be modifying its
229900742d8SJosh Poimboeuf 	 * stack while we're reading it.  This is generally not a problem and
230900742d8SJosh Poimboeuf 	 * can be ignored as long as the caller understands that unwinding
231900742d8SJosh Poimboeuf 	 * another task will not always succeed.
232900742d8SJosh Poimboeuf 	 */
233900742d8SJosh Poimboeuf 	if (state->task != current)
234900742d8SJosh Poimboeuf 		goto the_end;
235900742d8SJosh Poimboeuf 
23624d86f59SJosh Poimboeuf 	if (state->regs) {
23724d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
23824d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
23924d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
24024d86f59SJosh Poimboeuf 			state->task->pid, next_frame);
2418b5e99f0SJosh Poimboeuf 		unwind_dump(state, (unsigned long *)state->regs);
24224d86f59SJosh Poimboeuf 	} else {
243c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
244c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
245c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
24624d86f59SJosh Poimboeuf 			state->task->pid, next_frame);
2478b5e99f0SJosh Poimboeuf 		unwind_dump(state, state->bp);
24824d86f59SJosh Poimboeuf 	}
249946c1911SJosh Poimboeuf the_end:
250946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
251946c1911SJosh Poimboeuf 	return false;
2527c7900f8SJosh Poimboeuf }
2537c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
2547c7900f8SJosh Poimboeuf 
2557c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
2567c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
2577c7900f8SJosh Poimboeuf {
258946c1911SJosh Poimboeuf 	unsigned long *bp, *frame;
259946c1911SJosh Poimboeuf 	size_t len;
260946c1911SJosh Poimboeuf 
2617c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
2627c7900f8SJosh Poimboeuf 	state->task = task;
2637c7900f8SJosh Poimboeuf 
2647c7900f8SJosh Poimboeuf 	/* don't even attempt to start from user mode regs */
2657c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
2667c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
2677c7900f8SJosh Poimboeuf 		return;
2687c7900f8SJosh Poimboeuf 	}
2697c7900f8SJosh Poimboeuf 
2707c7900f8SJosh Poimboeuf 	/* set up the starting stack frame */
271946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
272946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(bp);
273946c1911SJosh Poimboeuf 	if (regs) {
274946c1911SJosh Poimboeuf 		state->regs = regs;
275946c1911SJosh Poimboeuf 		frame = (unsigned long *)regs;
276946c1911SJosh Poimboeuf 		len = sizeof(*regs);
277946c1911SJosh Poimboeuf 	} else {
278946c1911SJosh Poimboeuf 		state->bp = bp;
279946c1911SJosh Poimboeuf 		frame = bp;
280946c1911SJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
281946c1911SJosh Poimboeuf 	}
2827c7900f8SJosh Poimboeuf 
2837c7900f8SJosh Poimboeuf 	/* initialize stack info and make sure the frame data is accessible */
284946c1911SJosh Poimboeuf 	get_stack_info(frame, state->task, &state->stack_info,
2857c7900f8SJosh Poimboeuf 		       &state->stack_mask);
286946c1911SJosh Poimboeuf 	update_stack_state(state, frame, len);
2877c7900f8SJosh Poimboeuf 
2887c7900f8SJosh Poimboeuf 	/*
2897c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
2907c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
2917c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
2927c7900f8SJosh Poimboeuf 	 */
2937c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
2947c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
2957c7900f8SJosh Poimboeuf 			state->bp < first_frame))
2967c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
2977c7900f8SJosh Poimboeuf }
2987c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
299