xref: /linux/arch/x86/kernel/unwind_frame.c (revision c32c47c68a0ae701088c5b2c3798856ed16746ae)
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 
97c7900f8SJosh Poimboeuf unsigned long unwind_get_return_address(struct unwind_state *state)
107c7900f8SJosh Poimboeuf {
117c7900f8SJosh Poimboeuf 	unsigned long addr;
127c7900f8SJosh Poimboeuf 	unsigned long *addr_p = unwind_get_return_address_ptr(state);
137c7900f8SJosh Poimboeuf 
147c7900f8SJosh Poimboeuf 	if (unwind_done(state))
157c7900f8SJosh Poimboeuf 		return 0;
167c7900f8SJosh Poimboeuf 
17946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
18946c1911SJosh Poimboeuf 		return 0;
19946c1911SJosh Poimboeuf 
207c7900f8SJosh Poimboeuf 	addr = ftrace_graph_ret_addr(state->task, &state->graph_idx, *addr_p,
217c7900f8SJosh Poimboeuf 				     addr_p);
227c7900f8SJosh Poimboeuf 
237c7900f8SJosh Poimboeuf 	return __kernel_text_address(addr) ? addr : 0;
247c7900f8SJosh Poimboeuf }
257c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
267c7900f8SJosh Poimboeuf 
27acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
28acb4608aSJosh Poimboeuf {
29acb4608aSJosh Poimboeuf 	unsigned long bp = (unsigned long)state->bp;
30acb4608aSJosh Poimboeuf 	unsigned long regs = (unsigned long)task_pt_regs(state->task);
31acb4608aSJosh Poimboeuf 
32acb4608aSJosh Poimboeuf 	return bp == regs - FRAME_HEADER_SIZE;
33acb4608aSJosh Poimboeuf }
34acb4608aSJosh Poimboeuf 
35946c1911SJosh Poimboeuf /*
36946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
37946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
38946c1911SJosh Poimboeuf  */
39946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
40946c1911SJosh Poimboeuf {
41946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
42946c1911SJosh Poimboeuf 
43946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
44946c1911SJosh Poimboeuf 		return NULL;
45946c1911SJosh Poimboeuf 
46946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
47946c1911SJosh Poimboeuf }
48946c1911SJosh Poimboeuf 
497c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr,
507c7900f8SJosh Poimboeuf 			       size_t len)
517c7900f8SJosh Poimboeuf {
527c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
537c7900f8SJosh Poimboeuf 
547c7900f8SJosh Poimboeuf 	/*
557c7900f8SJosh Poimboeuf 	 * If addr isn't on the current stack, switch to the next one.
567c7900f8SJosh Poimboeuf 	 *
577c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
587c7900f8SJosh Poimboeuf 	 * that 'info->next_sp' could point to an empty stack and 'addr' could
597c7900f8SJosh Poimboeuf 	 * be on a subsequent stack.
607c7900f8SJosh Poimboeuf 	 */
617c7900f8SJosh Poimboeuf 	while (!on_stack(info, addr, len))
627c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
637c7900f8SJosh Poimboeuf 				   &state->stack_mask))
647c7900f8SJosh Poimboeuf 			return false;
657c7900f8SJosh Poimboeuf 
667c7900f8SJosh Poimboeuf 	return true;
677c7900f8SJosh Poimboeuf }
687c7900f8SJosh Poimboeuf 
697c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
707c7900f8SJosh Poimboeuf {
71946c1911SJosh Poimboeuf 	struct pt_regs *regs;
72946c1911SJosh Poimboeuf 	unsigned long *next_bp, *next_frame;
73946c1911SJosh Poimboeuf 	size_t next_len;
747c7900f8SJosh Poimboeuf 
757c7900f8SJosh Poimboeuf 	if (unwind_done(state))
767c7900f8SJosh Poimboeuf 		return false;
777c7900f8SJosh Poimboeuf 
78946c1911SJosh Poimboeuf 	/* have we reached the end? */
79946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
80946c1911SJosh Poimboeuf 		goto the_end;
81946c1911SJosh Poimboeuf 
82acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
83acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
84acb4608aSJosh Poimboeuf 
85acb4608aSJosh Poimboeuf 		/*
86acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
87acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
88acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
89acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
90acb4608aSJosh Poimboeuf 		 *
91acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
92acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
93acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
94acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
95acb4608aSJosh Poimboeuf 		 */
96acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
97acb4608aSJosh Poimboeuf 			goto the_end;
98acb4608aSJosh Poimboeuf 
99acb4608aSJosh Poimboeuf 		/*
100acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
101acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
102acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
103acb4608aSJosh Poimboeuf 		 */
104acb4608aSJosh Poimboeuf 		state->regs = regs;
105acb4608aSJosh Poimboeuf 		state->bp = NULL;
106acb4608aSJosh Poimboeuf 		return true;
107acb4608aSJosh Poimboeuf 	}
108acb4608aSJosh Poimboeuf 
109946c1911SJosh Poimboeuf 	/* get the next frame pointer */
110946c1911SJosh Poimboeuf 	if (state->regs)
111946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
112946c1911SJosh Poimboeuf 	else
1137c7900f8SJosh Poimboeuf 		next_bp = (unsigned long *)*state->bp;
1147c7900f8SJosh Poimboeuf 
115946c1911SJosh Poimboeuf 	/* is the next frame pointer an encoded pointer to pt_regs? */
116946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
117946c1911SJosh Poimboeuf 	if (regs) {
118946c1911SJosh Poimboeuf 		next_frame = (unsigned long *)regs;
119946c1911SJosh Poimboeuf 		next_len = sizeof(*regs);
120946c1911SJosh Poimboeuf 	} else {
121946c1911SJosh Poimboeuf 		next_frame = next_bp;
122946c1911SJosh Poimboeuf 		next_len = FRAME_HEADER_SIZE;
123946c1911SJosh Poimboeuf 	}
1247c7900f8SJosh Poimboeuf 
125946c1911SJosh Poimboeuf 	/* make sure the next frame's data is accessible */
126*c32c47c6SJosh Poimboeuf 	if (!update_stack_state(state, next_frame, next_len)) {
127*c32c47c6SJosh Poimboeuf 		/*
128*c32c47c6SJosh Poimboeuf 		 * Don't warn on bad regs->bp.  An interrupt in entry code
129*c32c47c6SJosh Poimboeuf 		 * might cause a false positive warning.
130*c32c47c6SJosh Poimboeuf 		 */
131*c32c47c6SJosh Poimboeuf 		if (state->regs)
132*c32c47c6SJosh Poimboeuf 			goto the_end;
133*c32c47c6SJosh Poimboeuf 
134*c32c47c6SJosh Poimboeuf 		goto bad_address;
135*c32c47c6SJosh Poimboeuf 	}
136*c32c47c6SJosh Poimboeuf 
1377c7900f8SJosh Poimboeuf 	/* move to the next frame */
138946c1911SJosh Poimboeuf 	if (regs) {
139946c1911SJosh Poimboeuf 		state->regs = regs;
140946c1911SJosh Poimboeuf 		state->bp = NULL;
141946c1911SJosh Poimboeuf 	} else {
1427c7900f8SJosh Poimboeuf 		state->bp = next_bp;
143946c1911SJosh Poimboeuf 		state->regs = NULL;
144946c1911SJosh Poimboeuf 	}
145946c1911SJosh Poimboeuf 
1467c7900f8SJosh Poimboeuf 	return true;
147946c1911SJosh Poimboeuf 
148*c32c47c6SJosh Poimboeuf bad_address:
149*c32c47c6SJosh Poimboeuf 	printk_deferred_once(KERN_WARNING
150*c32c47c6SJosh Poimboeuf 		"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
151*c32c47c6SJosh Poimboeuf 		state->bp, state->task->comm,
152*c32c47c6SJosh Poimboeuf 		state->task->pid, next_bp);
153946c1911SJosh Poimboeuf the_end:
154946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
155946c1911SJosh Poimboeuf 	return false;
1567c7900f8SJosh Poimboeuf }
1577c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
1587c7900f8SJosh Poimboeuf 
1597c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
1607c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
1617c7900f8SJosh Poimboeuf {
162946c1911SJosh Poimboeuf 	unsigned long *bp, *frame;
163946c1911SJosh Poimboeuf 	size_t len;
164946c1911SJosh Poimboeuf 
1657c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
1667c7900f8SJosh Poimboeuf 	state->task = task;
1677c7900f8SJosh Poimboeuf 
1687c7900f8SJosh Poimboeuf 	/* don't even attempt to start from user mode regs */
1697c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
1707c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
1717c7900f8SJosh Poimboeuf 		return;
1727c7900f8SJosh Poimboeuf 	}
1737c7900f8SJosh Poimboeuf 
1747c7900f8SJosh Poimboeuf 	/* set up the starting stack frame */
175946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
176946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(bp);
177946c1911SJosh Poimboeuf 	if (regs) {
178946c1911SJosh Poimboeuf 		state->regs = regs;
179946c1911SJosh Poimboeuf 		frame = (unsigned long *)regs;
180946c1911SJosh Poimboeuf 		len = sizeof(*regs);
181946c1911SJosh Poimboeuf 	} else {
182946c1911SJosh Poimboeuf 		state->bp = bp;
183946c1911SJosh Poimboeuf 		frame = bp;
184946c1911SJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
185946c1911SJosh Poimboeuf 	}
1867c7900f8SJosh Poimboeuf 
1877c7900f8SJosh Poimboeuf 	/* initialize stack info and make sure the frame data is accessible */
188946c1911SJosh Poimboeuf 	get_stack_info(frame, state->task, &state->stack_info,
1897c7900f8SJosh Poimboeuf 		       &state->stack_mask);
190946c1911SJosh Poimboeuf 	update_stack_state(state, frame, len);
1917c7900f8SJosh Poimboeuf 
1927c7900f8SJosh Poimboeuf 	/*
1937c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
1947c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
1957c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
1967c7900f8SJosh Poimboeuf 	 */
1977c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
1987c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
1997c7900f8SJosh Poimboeuf 			state->bp < first_frame))
2007c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
2017c7900f8SJosh Poimboeuf }
2027c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
203