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