xref: /openbmc/linux/arch/x86/kernel/unwind_frame.c (revision a36954f5)
1 #include <linux/sched.h>
2 #include <linux/sched/task.h>
3 #include <linux/sched/task_stack.h>
4 #include <linux/interrupt.h>
5 #include <asm/sections.h>
6 #include <asm/ptrace.h>
7 #include <asm/bitops.h>
8 #include <asm/stacktrace.h>
9 #include <asm/unwind.h>
10 
11 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
12 
13 /*
14  * This disables KASAN checking when reading a value from another task's stack,
15  * since the other task could be running on another CPU and could have poisoned
16  * the stack in the meantime.
17  */
18 #define READ_ONCE_TASK_STACK(task, x)			\
19 ({							\
20 	unsigned long val;				\
21 	if (task == current)				\
22 		val = READ_ONCE(x);			\
23 	else						\
24 		val = READ_ONCE_NOCHECK(x);		\
25 	val;						\
26 })
27 
28 static void unwind_dump(struct unwind_state *state)
29 {
30 	static bool dumped_before = false;
31 	bool prev_zero, zero = false;
32 	unsigned long word, *sp;
33 	struct stack_info stack_info = {0};
34 	unsigned long visit_mask = 0;
35 
36 	if (dumped_before)
37 		return;
38 
39 	dumped_before = true;
40 
41 	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
42 			state->stack_info.type, state->stack_info.next_sp,
43 			state->stack_mask, state->graph_idx);
44 
45 	for (sp = state->orig_sp; sp; sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
46 		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
47 			break;
48 
49 		for (; sp < stack_info.end; sp++) {
50 
51 			word = READ_ONCE_NOCHECK(*sp);
52 
53 			prev_zero = zero;
54 			zero = word == 0;
55 
56 			if (zero) {
57 				if (!prev_zero)
58 					printk_deferred("%p: %0*x ...\n",
59 							sp, BITS_PER_LONG/4, 0);
60 				continue;
61 			}
62 
63 			printk_deferred("%p: %0*lx (%pB)\n",
64 					sp, BITS_PER_LONG/4, word, (void *)word);
65 		}
66 	}
67 }
68 
69 unsigned long unwind_get_return_address(struct unwind_state *state)
70 {
71 	if (unwind_done(state))
72 		return 0;
73 
74 	return __kernel_text_address(state->ip) ? state->ip : 0;
75 }
76 EXPORT_SYMBOL_GPL(unwind_get_return_address);
77 
78 static size_t regs_size(struct pt_regs *regs)
79 {
80 	/* x86_32 regs from kernel mode are two words shorter: */
81 	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
82 		return sizeof(*regs) - 2*sizeof(long);
83 
84 	return sizeof(*regs);
85 }
86 
87 static bool in_entry_code(unsigned long ip)
88 {
89 	char *addr = (char *)ip;
90 
91 	if (addr >= __entry_text_start && addr < __entry_text_end)
92 		return true;
93 
94 #if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
95 	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
96 		return true;
97 #endif
98 
99 	return false;
100 }
101 
102 static inline unsigned long *last_frame(struct unwind_state *state)
103 {
104 	return (unsigned long *)task_pt_regs(state->task) - 2;
105 }
106 
107 #ifdef CONFIG_X86_32
108 #define GCC_REALIGN_WORDS 3
109 #else
110 #define GCC_REALIGN_WORDS 1
111 #endif
112 
113 static inline unsigned long *last_aligned_frame(struct unwind_state *state)
114 {
115 	return last_frame(state) - GCC_REALIGN_WORDS;
116 }
117 
118 static bool is_last_task_frame(struct unwind_state *state)
119 {
120 	unsigned long *last_bp = last_frame(state);
121 	unsigned long *aligned_bp = last_aligned_frame(state);
122 
123 	/*
124 	 * We have to check for the last task frame at two different locations
125 	 * because gcc can occasionally decide to realign the stack pointer and
126 	 * change the offset of the stack frame in the prologue of a function
127 	 * called by head/entry code.  Examples:
128 	 *
129 	 * <start_secondary>:
130 	 *      push   %edi
131 	 *      lea    0x8(%esp),%edi
132 	 *      and    $0xfffffff8,%esp
133 	 *      pushl  -0x4(%edi)
134 	 *      push   %ebp
135 	 *      mov    %esp,%ebp
136 	 *
137 	 * <x86_64_start_kernel>:
138 	 *      lea    0x8(%rsp),%r10
139 	 *      and    $0xfffffffffffffff0,%rsp
140 	 *      pushq  -0x8(%r10)
141 	 *      push   %rbp
142 	 *      mov    %rsp,%rbp
143 	 *
144 	 * Note that after aligning the stack, it pushes a duplicate copy of
145 	 * the return address before pushing the frame pointer.
146 	 */
147 	return (state->bp == last_bp ||
148 		(state->bp == aligned_bp && *(aligned_bp+1) == *(last_bp+1)));
149 }
150 
151 /*
152  * This determines if the frame pointer actually contains an encoded pointer to
153  * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
154  */
155 static struct pt_regs *decode_frame_pointer(unsigned long *bp)
156 {
157 	unsigned long regs = (unsigned long)bp;
158 
159 	if (!(regs & 0x1))
160 		return NULL;
161 
162 	return (struct pt_regs *)(regs & ~0x1);
163 }
164 
165 static bool update_stack_state(struct unwind_state *state,
166 			       unsigned long *next_bp)
167 {
168 	struct stack_info *info = &state->stack_info;
169 	enum stack_type prev_type = info->type;
170 	struct pt_regs *regs;
171 	unsigned long *frame, *prev_frame_end, *addr_p, addr;
172 	size_t len;
173 
174 	if (state->regs)
175 		prev_frame_end = (void *)state->regs + regs_size(state->regs);
176 	else
177 		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
178 
179 	/* Is the next frame pointer an encoded pointer to pt_regs? */
180 	regs = decode_frame_pointer(next_bp);
181 	if (regs) {
182 		frame = (unsigned long *)regs;
183 		len = regs_size(regs);
184 		state->got_irq = true;
185 	} else {
186 		frame = next_bp;
187 		len = FRAME_HEADER_SIZE;
188 	}
189 
190 	/*
191 	 * If the next bp isn't on the current stack, switch to the next one.
192 	 *
193 	 * We may have to traverse multiple stacks to deal with the possibility
194 	 * that info->next_sp could point to an empty stack and the next bp
195 	 * could be on a subsequent stack.
196 	 */
197 	while (!on_stack(info, frame, len))
198 		if (get_stack_info(info->next_sp, state->task, info,
199 				   &state->stack_mask))
200 			return false;
201 
202 	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
203 	if (state->orig_sp && state->stack_info.type == prev_type &&
204 	    frame < prev_frame_end)
205 		return false;
206 
207 	/* Move state to the next frame: */
208 	if (regs) {
209 		state->regs = regs;
210 		state->bp = NULL;
211 	} else {
212 		state->bp = next_bp;
213 		state->regs = NULL;
214 	}
215 
216 	/* Save the return address: */
217 	if (state->regs && user_mode(state->regs))
218 		state->ip = 0;
219 	else {
220 		addr_p = unwind_get_return_address_ptr(state);
221 		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
222 		state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
223 						  addr, addr_p);
224 	}
225 
226 	/* Save the original stack pointer for unwind_dump(): */
227 	if (!state->orig_sp)
228 		state->orig_sp = frame;
229 
230 	return true;
231 }
232 
233 bool unwind_next_frame(struct unwind_state *state)
234 {
235 	struct pt_regs *regs;
236 	unsigned long *next_bp;
237 
238 	if (unwind_done(state))
239 		return false;
240 
241 	/* Have we reached the end? */
242 	if (state->regs && user_mode(state->regs))
243 		goto the_end;
244 
245 	if (is_last_task_frame(state)) {
246 		regs = task_pt_regs(state->task);
247 
248 		/*
249 		 * kthreads (other than the boot CPU's idle thread) have some
250 		 * partial regs at the end of their stack which were placed
251 		 * there by copy_thread_tls().  But the regs don't have any
252 		 * useful information, so we can skip them.
253 		 *
254 		 * This user_mode() check is slightly broader than a PF_KTHREAD
255 		 * check because it also catches the awkward situation where a
256 		 * newly forked kthread transitions into a user task by calling
257 		 * do_execve(), which eventually clears PF_KTHREAD.
258 		 */
259 		if (!user_mode(regs))
260 			goto the_end;
261 
262 		/*
263 		 * We're almost at the end, but not quite: there's still the
264 		 * syscall regs frame.  Entry code doesn't encode the regs
265 		 * pointer for syscalls, so we have to set it manually.
266 		 */
267 		state->regs = regs;
268 		state->bp = NULL;
269 		state->ip = 0;
270 		return true;
271 	}
272 
273 	/* Get the next frame pointer: */
274 	if (state->regs)
275 		next_bp = (unsigned long *)state->regs->bp;
276 	else
277 		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
278 
279 	/* Move to the next frame if it's safe: */
280 	if (!update_stack_state(state, next_bp))
281 		goto bad_address;
282 
283 	return true;
284 
285 bad_address:
286 	state->error = true;
287 
288 	/*
289 	 * When unwinding a non-current task, the task might actually be
290 	 * running on another CPU, in which case it could be modifying its
291 	 * stack while we're reading it.  This is generally not a problem and
292 	 * can be ignored as long as the caller understands that unwinding
293 	 * another task will not always succeed.
294 	 */
295 	if (state->task != current)
296 		goto the_end;
297 
298 	/*
299 	 * Don't warn if the unwinder got lost due to an interrupt in entry
300 	 * code or in the C handler before the first frame pointer got set up:
301 	 */
302 	if (state->got_irq && in_entry_code(state->ip))
303 		goto the_end;
304 	if (state->regs &&
305 	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
306 	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
307 		goto the_end;
308 
309 	if (state->regs) {
310 		printk_deferred_once(KERN_WARNING
311 			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
312 			state->regs, state->task->comm,
313 			state->task->pid, next_bp);
314 		unwind_dump(state);
315 	} else {
316 		printk_deferred_once(KERN_WARNING
317 			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
318 			state->bp, state->task->comm,
319 			state->task->pid, next_bp);
320 		unwind_dump(state);
321 	}
322 the_end:
323 	state->stack_info.type = STACK_TYPE_UNKNOWN;
324 	return false;
325 }
326 EXPORT_SYMBOL_GPL(unwind_next_frame);
327 
328 void __unwind_start(struct unwind_state *state, struct task_struct *task,
329 		    struct pt_regs *regs, unsigned long *first_frame)
330 {
331 	unsigned long *bp;
332 
333 	memset(state, 0, sizeof(*state));
334 	state->task = task;
335 	state->got_irq = (regs);
336 
337 	/* Don't even attempt to start from user mode regs: */
338 	if (regs && user_mode(regs)) {
339 		state->stack_info.type = STACK_TYPE_UNKNOWN;
340 		return;
341 	}
342 
343 	bp = get_frame_pointer(task, regs);
344 
345 	/* Initialize stack info and make sure the frame data is accessible: */
346 	get_stack_info(bp, state->task, &state->stack_info,
347 		       &state->stack_mask);
348 	update_stack_state(state, bp);
349 
350 	/*
351 	 * The caller can provide the address of the first frame directly
352 	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
353 	 * to start unwinding at.  Skip ahead until we reach it.
354 	 */
355 	while (!unwind_done(state) &&
356 	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
357 			state->bp < first_frame))
358 		unwind_next_frame(state);
359 }
360 EXPORT_SYMBOL_GPL(__unwind_start);
361