xref: /linux/arch/x86/kernel/unwind_frame.c (revision 24d86f59093b0bcb3756cdf47f2db10ff4e90dbb)
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 
23b6959a36SJosh Poimboeuf 	if (!__kernel_text_address(addr)) {
24b6959a36SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
25b6959a36SJosh Poimboeuf 			"WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n",
26b6959a36SJosh Poimboeuf 			(void *)addr, addr_p, state->task->comm,
27b6959a36SJosh Poimboeuf 			state->task->pid);
28b6959a36SJosh Poimboeuf 		return 0;
29b6959a36SJosh Poimboeuf 	}
30b6959a36SJosh Poimboeuf 
31b6959a36SJosh Poimboeuf 	return addr;
327c7900f8SJosh Poimboeuf }
337c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_get_return_address);
347c7900f8SJosh Poimboeuf 
35*24d86f59SJosh Poimboeuf static size_t regs_size(struct pt_regs *regs)
36*24d86f59SJosh Poimboeuf {
37*24d86f59SJosh Poimboeuf 	/* x86_32 regs from kernel mode are two words shorter: */
38*24d86f59SJosh Poimboeuf 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
39*24d86f59SJosh Poimboeuf 		return sizeof(*regs) - 2*sizeof(long);
40*24d86f59SJosh Poimboeuf 
41*24d86f59SJosh Poimboeuf 	return sizeof(*regs);
42*24d86f59SJosh Poimboeuf }
43*24d86f59SJosh Poimboeuf 
44acb4608aSJosh Poimboeuf static bool is_last_task_frame(struct unwind_state *state)
45acb4608aSJosh Poimboeuf {
46acb4608aSJosh Poimboeuf 	unsigned long bp = (unsigned long)state->bp;
47acb4608aSJosh Poimboeuf 	unsigned long regs = (unsigned long)task_pt_regs(state->task);
48acb4608aSJosh Poimboeuf 
49acb4608aSJosh Poimboeuf 	return bp == regs - FRAME_HEADER_SIZE;
50acb4608aSJosh Poimboeuf }
51acb4608aSJosh Poimboeuf 
52946c1911SJosh Poimboeuf /*
53946c1911SJosh Poimboeuf  * This determines if the frame pointer actually contains an encoded pointer to
54946c1911SJosh Poimboeuf  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
55946c1911SJosh Poimboeuf  */
56946c1911SJosh Poimboeuf static struct pt_regs *decode_frame_pointer(unsigned long *bp)
57946c1911SJosh Poimboeuf {
58946c1911SJosh Poimboeuf 	unsigned long regs = (unsigned long)bp;
59946c1911SJosh Poimboeuf 
60946c1911SJosh Poimboeuf 	if (!(regs & 0x1))
61946c1911SJosh Poimboeuf 		return NULL;
62946c1911SJosh Poimboeuf 
63946c1911SJosh Poimboeuf 	return (struct pt_regs *)(regs & ~0x1);
64946c1911SJosh Poimboeuf }
65946c1911SJosh Poimboeuf 
667c7900f8SJosh Poimboeuf static bool update_stack_state(struct unwind_state *state, void *addr,
677c7900f8SJosh Poimboeuf 			       size_t len)
687c7900f8SJosh Poimboeuf {
697c7900f8SJosh Poimboeuf 	struct stack_info *info = &state->stack_info;
707c7900f8SJosh Poimboeuf 
717c7900f8SJosh Poimboeuf 	/*
727c7900f8SJosh Poimboeuf 	 * If addr isn't on the current stack, switch to the next one.
737c7900f8SJosh Poimboeuf 	 *
747c7900f8SJosh Poimboeuf 	 * We may have to traverse multiple stacks to deal with the possibility
757c7900f8SJosh Poimboeuf 	 * that 'info->next_sp' could point to an empty stack and 'addr' could
767c7900f8SJosh Poimboeuf 	 * be on a subsequent stack.
777c7900f8SJosh Poimboeuf 	 */
787c7900f8SJosh Poimboeuf 	while (!on_stack(info, addr, len))
797c7900f8SJosh Poimboeuf 		if (get_stack_info(info->next_sp, state->task, info,
807c7900f8SJosh Poimboeuf 				   &state->stack_mask))
817c7900f8SJosh Poimboeuf 			return false;
827c7900f8SJosh Poimboeuf 
837c7900f8SJosh Poimboeuf 	return true;
847c7900f8SJosh Poimboeuf }
857c7900f8SJosh Poimboeuf 
867c7900f8SJosh Poimboeuf bool unwind_next_frame(struct unwind_state *state)
877c7900f8SJosh Poimboeuf {
88946c1911SJosh Poimboeuf 	struct pt_regs *regs;
89946c1911SJosh Poimboeuf 	unsigned long *next_bp, *next_frame;
90946c1911SJosh Poimboeuf 	size_t next_len;
91*24d86f59SJosh Poimboeuf 	enum stack_type prev_type = state->stack_info.type;
927c7900f8SJosh Poimboeuf 
937c7900f8SJosh Poimboeuf 	if (unwind_done(state))
947c7900f8SJosh Poimboeuf 		return false;
957c7900f8SJosh Poimboeuf 
96946c1911SJosh Poimboeuf 	/* have we reached the end? */
97946c1911SJosh Poimboeuf 	if (state->regs && user_mode(state->regs))
98946c1911SJosh Poimboeuf 		goto the_end;
99946c1911SJosh Poimboeuf 
100acb4608aSJosh Poimboeuf 	if (is_last_task_frame(state)) {
101acb4608aSJosh Poimboeuf 		regs = task_pt_regs(state->task);
102acb4608aSJosh Poimboeuf 
103acb4608aSJosh Poimboeuf 		/*
104acb4608aSJosh Poimboeuf 		 * kthreads (other than the boot CPU's idle thread) have some
105acb4608aSJosh Poimboeuf 		 * partial regs at the end of their stack which were placed
106acb4608aSJosh Poimboeuf 		 * there by copy_thread_tls().  But the regs don't have any
107acb4608aSJosh Poimboeuf 		 * useful information, so we can skip them.
108acb4608aSJosh Poimboeuf 		 *
109acb4608aSJosh Poimboeuf 		 * This user_mode() check is slightly broader than a PF_KTHREAD
110acb4608aSJosh Poimboeuf 		 * check because it also catches the awkward situation where a
111acb4608aSJosh Poimboeuf 		 * newly forked kthread transitions into a user task by calling
112acb4608aSJosh Poimboeuf 		 * do_execve(), which eventually clears PF_KTHREAD.
113acb4608aSJosh Poimboeuf 		 */
114acb4608aSJosh Poimboeuf 		if (!user_mode(regs))
115acb4608aSJosh Poimboeuf 			goto the_end;
116acb4608aSJosh Poimboeuf 
117acb4608aSJosh Poimboeuf 		/*
118acb4608aSJosh Poimboeuf 		 * We're almost at the end, but not quite: there's still the
119acb4608aSJosh Poimboeuf 		 * syscall regs frame.  Entry code doesn't encode the regs
120acb4608aSJosh Poimboeuf 		 * pointer for syscalls, so we have to set it manually.
121acb4608aSJosh Poimboeuf 		 */
122acb4608aSJosh Poimboeuf 		state->regs = regs;
123acb4608aSJosh Poimboeuf 		state->bp = NULL;
124acb4608aSJosh Poimboeuf 		return true;
125acb4608aSJosh Poimboeuf 	}
126acb4608aSJosh Poimboeuf 
127946c1911SJosh Poimboeuf 	/* get the next frame pointer */
128946c1911SJosh Poimboeuf 	if (state->regs)
129946c1911SJosh Poimboeuf 		next_bp = (unsigned long *)state->regs->bp;
130946c1911SJosh Poimboeuf 	else
1317c7900f8SJosh Poimboeuf 		next_bp = (unsigned long *)*state->bp;
1327c7900f8SJosh Poimboeuf 
133946c1911SJosh Poimboeuf 	/* is the next frame pointer an encoded pointer to pt_regs? */
134946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(next_bp);
135946c1911SJosh Poimboeuf 	if (regs) {
136946c1911SJosh Poimboeuf 		next_frame = (unsigned long *)regs;
137946c1911SJosh Poimboeuf 		next_len = sizeof(*regs);
138946c1911SJosh Poimboeuf 	} else {
139946c1911SJosh Poimboeuf 		next_frame = next_bp;
140946c1911SJosh Poimboeuf 		next_len = FRAME_HEADER_SIZE;
141946c1911SJosh Poimboeuf 	}
1427c7900f8SJosh Poimboeuf 
143946c1911SJosh Poimboeuf 	/* make sure the next frame's data is accessible */
144c32c47c6SJosh Poimboeuf 	if (!update_stack_state(state, next_frame, next_len)) {
145c32c47c6SJosh Poimboeuf 		/*
146c32c47c6SJosh Poimboeuf 		 * Don't warn on bad regs->bp.  An interrupt in entry code
147c32c47c6SJosh Poimboeuf 		 * might cause a false positive warning.
148c32c47c6SJosh Poimboeuf 		 */
149c32c47c6SJosh Poimboeuf 		if (state->regs)
150c32c47c6SJosh Poimboeuf 			goto the_end;
151c32c47c6SJosh Poimboeuf 
152c32c47c6SJosh Poimboeuf 		goto bad_address;
153c32c47c6SJosh Poimboeuf 	}
154c32c47c6SJosh Poimboeuf 
155*24d86f59SJosh Poimboeuf 	/* Make sure it only unwinds up and doesn't overlap the last frame: */
156*24d86f59SJosh Poimboeuf 	if (state->stack_info.type == prev_type) {
157*24d86f59SJosh Poimboeuf 		if (state->regs && (void *)next_frame < (void *)state->regs + regs_size(state->regs))
158*24d86f59SJosh Poimboeuf 			goto bad_address;
159*24d86f59SJosh Poimboeuf 
160*24d86f59SJosh Poimboeuf 		if (state->bp && (void *)next_frame < (void *)state->bp + FRAME_HEADER_SIZE)
161*24d86f59SJosh Poimboeuf 			goto bad_address;
162*24d86f59SJosh Poimboeuf 	}
163*24d86f59SJosh Poimboeuf 
1647c7900f8SJosh Poimboeuf 	/* move to the next frame */
165946c1911SJosh Poimboeuf 	if (regs) {
166946c1911SJosh Poimboeuf 		state->regs = regs;
167946c1911SJosh Poimboeuf 		state->bp = NULL;
168946c1911SJosh Poimboeuf 	} else {
1697c7900f8SJosh Poimboeuf 		state->bp = next_bp;
170946c1911SJosh Poimboeuf 		state->regs = NULL;
171946c1911SJosh Poimboeuf 	}
172946c1911SJosh Poimboeuf 
1737c7900f8SJosh Poimboeuf 	return true;
174946c1911SJosh Poimboeuf 
175c32c47c6SJosh Poimboeuf bad_address:
176*24d86f59SJosh Poimboeuf 	if (state->regs) {
177*24d86f59SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
178*24d86f59SJosh Poimboeuf 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
179*24d86f59SJosh Poimboeuf 			state->regs, state->task->comm,
180*24d86f59SJosh Poimboeuf 			state->task->pid, next_frame);
181*24d86f59SJosh Poimboeuf 	} else {
182c32c47c6SJosh Poimboeuf 		printk_deferred_once(KERN_WARNING
183c32c47c6SJosh Poimboeuf 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
184c32c47c6SJosh Poimboeuf 			state->bp, state->task->comm,
185*24d86f59SJosh Poimboeuf 			state->task->pid, next_frame);
186*24d86f59SJosh Poimboeuf 	}
187946c1911SJosh Poimboeuf the_end:
188946c1911SJosh Poimboeuf 	state->stack_info.type = STACK_TYPE_UNKNOWN;
189946c1911SJosh Poimboeuf 	return false;
1907c7900f8SJosh Poimboeuf }
1917c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(unwind_next_frame);
1927c7900f8SJosh Poimboeuf 
1937c7900f8SJosh Poimboeuf void __unwind_start(struct unwind_state *state, struct task_struct *task,
1947c7900f8SJosh Poimboeuf 		    struct pt_regs *regs, unsigned long *first_frame)
1957c7900f8SJosh Poimboeuf {
196946c1911SJosh Poimboeuf 	unsigned long *bp, *frame;
197946c1911SJosh Poimboeuf 	size_t len;
198946c1911SJosh Poimboeuf 
1997c7900f8SJosh Poimboeuf 	memset(state, 0, sizeof(*state));
2007c7900f8SJosh Poimboeuf 	state->task = task;
2017c7900f8SJosh Poimboeuf 
2027c7900f8SJosh Poimboeuf 	/* don't even attempt to start from user mode regs */
2037c7900f8SJosh Poimboeuf 	if (regs && user_mode(regs)) {
2047c7900f8SJosh Poimboeuf 		state->stack_info.type = STACK_TYPE_UNKNOWN;
2057c7900f8SJosh Poimboeuf 		return;
2067c7900f8SJosh Poimboeuf 	}
2077c7900f8SJosh Poimboeuf 
2087c7900f8SJosh Poimboeuf 	/* set up the starting stack frame */
209946c1911SJosh Poimboeuf 	bp = get_frame_pointer(task, regs);
210946c1911SJosh Poimboeuf 	regs = decode_frame_pointer(bp);
211946c1911SJosh Poimboeuf 	if (regs) {
212946c1911SJosh Poimboeuf 		state->regs = regs;
213946c1911SJosh Poimboeuf 		frame = (unsigned long *)regs;
214946c1911SJosh Poimboeuf 		len = sizeof(*regs);
215946c1911SJosh Poimboeuf 	} else {
216946c1911SJosh Poimboeuf 		state->bp = bp;
217946c1911SJosh Poimboeuf 		frame = bp;
218946c1911SJosh Poimboeuf 		len = FRAME_HEADER_SIZE;
219946c1911SJosh Poimboeuf 	}
2207c7900f8SJosh Poimboeuf 
2217c7900f8SJosh Poimboeuf 	/* initialize stack info and make sure the frame data is accessible */
222946c1911SJosh Poimboeuf 	get_stack_info(frame, state->task, &state->stack_info,
2237c7900f8SJosh Poimboeuf 		       &state->stack_mask);
224946c1911SJosh Poimboeuf 	update_stack_state(state, frame, len);
2257c7900f8SJosh Poimboeuf 
2267c7900f8SJosh Poimboeuf 	/*
2277c7900f8SJosh Poimboeuf 	 * The caller can provide the address of the first frame directly
2287c7900f8SJosh Poimboeuf 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
2297c7900f8SJosh Poimboeuf 	 * to start unwinding at.  Skip ahead until we reach it.
2307c7900f8SJosh Poimboeuf 	 */
2317c7900f8SJosh Poimboeuf 	while (!unwind_done(state) &&
2327c7900f8SJosh Poimboeuf 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
2337c7900f8SJosh Poimboeuf 			state->bp < first_frame))
2347c7900f8SJosh Poimboeuf 		unwind_next_frame(state);
2357c7900f8SJosh Poimboeuf }
2367c7900f8SJosh Poimboeuf EXPORT_SYMBOL_GPL(__unwind_start);
237