unwind_frame.c 11.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
#include <linux/sched.h>
3
#include <linux/sched/task.h>
4
#include <linux/sched/task_stack.h>
5 6
#include <linux/interrupt.h>
#include <asm/sections.h>
7 8 9 10 11 12 13
#include <asm/ptrace.h>
#include <asm/bitops.h>
#include <asm/stacktrace.h>
#include <asm/unwind.h>

#define FRAME_HEADER_SIZE (sizeof(long) * 2)

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
unsigned long unwind_get_return_address(struct unwind_state *state)
{
	if (unwind_done(state))
		return 0;

	return __kernel_text_address(state->ip) ? state->ip : 0;
}
EXPORT_SYMBOL_GPL(unwind_get_return_address);

unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
{
	if (unwind_done(state))
		return NULL;

	return state->regs ? &state->regs->ip : state->bp + 1;
}
30

31
static void unwind_dump(struct unwind_state *state)
32 33 34
{
	static bool dumped_before = false;
	bool prev_zero, zero = false;
35
	unsigned long word, *sp;
36 37
	struct stack_info stack_info = {0};
	unsigned long visit_mask = 0;
38 39 40 41 42 43

	if (dumped_before)
		return;

	dumped_before = true;

44
	printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
45 46 47
			state->stack_info.type, state->stack_info.next_sp,
			state->stack_mask, state->graph_idx);

48 49
	for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
	     sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
50 51
		if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
			break;
52

53
		for (; sp < stack_info.end; sp++) {
54

55 56 57 58
			word = READ_ONCE_NOCHECK(*sp);

			prev_zero = zero;
			zero = word == 0;
59

60 61 62 63 64 65 66 67 68 69
			if (zero) {
				if (!prev_zero)
					printk_deferred("%p: %0*x ...\n",
							sp, BITS_PER_LONG/4, 0);
				continue;
			}

			printk_deferred("%p: %0*lx (%pB)\n",
					sp, BITS_PER_LONG/4, word, (void *)word);
		}
70 71 72
	}
}

73 74 75 76 77 78 79 80 81
static size_t regs_size(struct pt_regs *regs)
{
	/* x86_32 regs from kernel mode are two words shorter: */
	if (IS_ENABLED(CONFIG_X86_32) && !user_mode(regs))
		return sizeof(*regs) - 2*sizeof(long);

	return sizeof(*regs);
}

82 83 84 85 86 87 88 89 90 91 92 93 94
static bool in_entry_code(unsigned long ip)
{
	char *addr = (char *)ip;

	if (addr >= __entry_text_start && addr < __entry_text_end)
		return true;

	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
		return true;

	return false;
}

95 96 97 98 99
static inline unsigned long *last_frame(struct unwind_state *state)
{
	return (unsigned long *)task_pt_regs(state->task) - 2;
}

100 101 102 103 104
static bool is_last_frame(struct unwind_state *state)
{
	return state->bp == last_frame(state);
}

105 106 107 108 109 110
#ifdef CONFIG_X86_32
#define GCC_REALIGN_WORDS 3
#else
#define GCC_REALIGN_WORDS 1
#endif

111 112 113 114 115
static inline unsigned long *last_aligned_frame(struct unwind_state *state)
{
	return last_frame(state) - GCC_REALIGN_WORDS;
}

116
static bool is_last_aligned_frame(struct unwind_state *state)
117
{
118 119
	unsigned long *last_bp = last_frame(state);
	unsigned long *aligned_bp = last_aligned_frame(state);
120

121
	/*
122 123 124
	 * GCC can occasionally decide to realign the stack pointer and change
	 * the offset of the stack frame in the prologue of a function called
	 * by head/entry code.  Examples:
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	 *
	 * <start_secondary>:
	 *      push   %edi
	 *      lea    0x8(%esp),%edi
	 *      and    $0xfffffff8,%esp
	 *      pushl  -0x4(%edi)
	 *      push   %ebp
	 *      mov    %esp,%ebp
	 *
	 * <x86_64_start_kernel>:
	 *      lea    0x8(%rsp),%r10
	 *      and    $0xfffffffffffffff0,%rsp
	 *      pushq  -0x8(%r10)
	 *      push   %rbp
	 *      mov    %rsp,%rbp
	 *
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	 * After aligning the stack, it pushes a duplicate copy of the return
	 * address before pushing the frame pointer.
	 */
	return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
}

static bool is_last_ftrace_frame(struct unwind_state *state)
{
	unsigned long *last_bp = last_frame(state);
	unsigned long *last_ftrace_bp = last_bp - 3;

	/*
	 * When unwinding from an ftrace handler of a function called by entry
	 * code, the stack layout of the last frame is:
	 *
	 *   bp
	 *   parent ret addr
	 *   bp
	 *   function ret addr
	 *   parent ret addr
	 *   pt_regs
	 *   -----------------
163
	 */
164 165 166 167 168 169 170 171 172
	return (state->bp == last_ftrace_bp &&
		*state->bp == *(state->bp + 2) &&
		*(state->bp + 1) == *(state->bp + 4));
}

static bool is_last_task_frame(struct unwind_state *state)
{
	return is_last_frame(state) || is_last_aligned_frame(state) ||
	       is_last_ftrace_frame(state);
173 174
}

175 176 177 178
/*
 * This determines if the frame pointer actually contains an encoded pointer to
 * pt_regs on the stack.  See ENCODE_FRAME_POINTER.
 */
179
#ifdef CONFIG_X86_64
180 181 182 183 184 185 186 187 188
static struct pt_regs *decode_frame_pointer(unsigned long *bp)
{
	unsigned long regs = (unsigned long)bp;

	if (!(regs & 0x1))
		return NULL;

	return (struct pt_regs *)(regs & ~0x1);
}
189 190 191 192 193 194 195 196 197 198 199
#else
static struct pt_regs *decode_frame_pointer(unsigned long *bp)
{
	unsigned long regs = (unsigned long)bp;

	if (regs & 0x80000000)
		return NULL;

	return (struct pt_regs *)(regs | 0x80000000);
}
#endif
200

201 202 203 204 205 206
#ifdef CONFIG_X86_32
#define KERNEL_REGS_SIZE (sizeof(struct pt_regs) - 2*sizeof(long))
#else
#define KERNEL_REGS_SIZE (sizeof(struct pt_regs))
#endif

207 208
static bool update_stack_state(struct unwind_state *state,
			       unsigned long *next_bp)
209 210
{
	struct stack_info *info = &state->stack_info;
211 212
	enum stack_type prev_type = info->type;
	struct pt_regs *regs;
213
	unsigned long *frame, *prev_frame_end, *addr_p, addr;
214 215 216 217 218 219 220 221 222 223 224
	size_t len;

	if (state->regs)
		prev_frame_end = (void *)state->regs + regs_size(state->regs);
	else
		prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;

	/* Is the next frame pointer an encoded pointer to pt_regs? */
	regs = decode_frame_pointer(next_bp);
	if (regs) {
		frame = (unsigned long *)regs;
225
		len = KERNEL_REGS_SIZE;
226
		state->got_irq = true;
227 228 229 230
	} else {
		frame = next_bp;
		len = FRAME_HEADER_SIZE;
	}
231 232

	/*
233
	 * If the next bp isn't on the current stack, switch to the next one.
234 235
	 *
	 * We may have to traverse multiple stacks to deal with the possibility
236 237
	 * that info->next_sp could point to an empty stack and the next bp
	 * could be on a subsequent stack.
238
	 */
239
	while (!on_stack(info, frame, len))
240 241 242 243
		if (get_stack_info(info->next_sp, state->task, info,
				   &state->stack_mask))
			return false;

244 245 246 247 248
	/* Make sure it only unwinds up and doesn't overlap the prev frame: */
	if (state->orig_sp && state->stack_info.type == prev_type &&
	    frame < prev_frame_end)
		return false;

249 250 251 252 253 254 255 256
	/*
	 * On 32-bit with user mode regs, make sure the last two regs are safe
	 * to access:
	 */
	if (IS_ENABLED(CONFIG_X86_32) && regs && user_mode(regs) &&
	    !on_stack(info, frame, len + 2*sizeof(long)))
		return false;

257 258 259 260 261 262 263 264 265
	/* Move state to the next frame: */
	if (regs) {
		state->regs = regs;
		state->bp = NULL;
	} else {
		state->bp = next_bp;
		state->regs = NULL;
	}

266 267 268 269 270 271 272 273 274 275
	/* Save the return address: */
	if (state->regs && user_mode(state->regs))
		state->ip = 0;
	else {
		addr_p = unwind_get_return_address_ptr(state);
		addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
		state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
						  addr, addr_p);
	}

276
	/* Save the original stack pointer for unwind_dump(): */
277
	if (!state->orig_sp)
278
		state->orig_sp = frame;
279

280 281 282 283 284
	return true;
}

bool unwind_next_frame(struct unwind_state *state)
{
285
	struct pt_regs *regs;
286
	unsigned long *next_bp;
287 288 289 290

	if (unwind_done(state))
		return false;

291
	/* Have we reached the end? */
292 293 294
	if (state->regs && user_mode(state->regs))
		goto the_end;

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	if (is_last_task_frame(state)) {
		regs = task_pt_regs(state->task);

		/*
		 * kthreads (other than the boot CPU's idle thread) have some
		 * partial regs at the end of their stack which were placed
		 * there by copy_thread_tls().  But the regs don't have any
		 * useful information, so we can skip them.
		 *
		 * This user_mode() check is slightly broader than a PF_KTHREAD
		 * check because it also catches the awkward situation where a
		 * newly forked kthread transitions into a user task by calling
		 * do_execve(), which eventually clears PF_KTHREAD.
		 */
		if (!user_mode(regs))
			goto the_end;

		/*
		 * We're almost at the end, but not quite: there's still the
		 * syscall regs frame.  Entry code doesn't encode the regs
		 * pointer for syscalls, so we have to set it manually.
		 */
		state->regs = regs;
		state->bp = NULL;
319
		state->ip = 0;
320 321 322
		return true;
	}

323
	/* Get the next frame pointer: */
324 325 326 327
	if (state->next_bp) {
		next_bp = state->next_bp;
		state->next_bp = NULL;
	} else if (state->regs) {
328
		next_bp = (unsigned long *)state->regs->bp;
329
	} else {
330
		next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
331
	}
332

333
	/* Move to the next frame if it's safe: */
334
	if (!update_stack_state(state, next_bp))
335 336
		goto bad_address;

337
	return true;
338

339
bad_address:
340 341
	state->error = true;

342 343 344 345 346 347 348 349 350 351
	/*
	 * When unwinding a non-current task, the task might actually be
	 * running on another CPU, in which case it could be modifying its
	 * stack while we're reading it.  This is generally not a problem and
	 * can be ignored as long as the caller understands that unwinding
	 * another task will not always succeed.
	 */
	if (state->task != current)
		goto the_end;

352 353
	/*
	 * Don't warn if the unwinder got lost due to an interrupt in entry
354
	 * code or in the C handler before the first frame pointer got set up:
355 356 357
	 */
	if (state->got_irq && in_entry_code(state->ip))
		goto the_end;
358 359 360 361
	if (state->regs &&
	    state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
	    state->regs->sp < (unsigned long)task_pt_regs(state->task))
		goto the_end;
362

363 364 365 366 367 368 369
	/*
	 * There are some known frame pointer issues on 32-bit.  Disable
	 * unwinder warnings on 32-bit until it gets objtool support.
	 */
	if (IS_ENABLED(CONFIG_X86_32))
		goto the_end;

370 371 372 373
	if (state->regs) {
		printk_deferred_once(KERN_WARNING
			"WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
			state->regs, state->task->comm,
374
			state->task->pid, next_bp);
375
		unwind_dump(state);
376 377 378 379
	} else {
		printk_deferred_once(KERN_WARNING
			"WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
			state->bp, state->task->comm,
380
			state->task->pid, next_bp);
381
		unwind_dump(state);
382
	}
383 384 385
the_end:
	state->stack_info.type = STACK_TYPE_UNKNOWN;
	return false;
386 387 388 389 390 391
}
EXPORT_SYMBOL_GPL(unwind_next_frame);

void __unwind_start(struct unwind_state *state, struct task_struct *task,
		    struct pt_regs *regs, unsigned long *first_frame)
{
392
	unsigned long *bp;
393

394 395
	memset(state, 0, sizeof(*state));
	state->task = task;
396
	state->got_irq = (regs);
397

398
	/* Don't even attempt to start from user mode regs: */
399 400 401 402 403
	if (regs && user_mode(regs)) {
		state->stack_info.type = STACK_TYPE_UNKNOWN;
		return;
	}

404
	bp = get_frame_pointer(task, regs);
405

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	/*
	 * If we crash with IP==0, the last successfully executed instruction
	 * was probably an indirect function call with a NULL function pointer.
	 * That means that SP points into the middle of an incomplete frame:
	 * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
	 * would have written a frame pointer if we hadn't crashed.
	 * Pretend that the frame is complete and that BP points to it, but save
	 * the real BP so that we can use it when looking for the next frame.
	 */
	if (regs && regs->ip == 0 &&
	    (unsigned long *)kernel_stack_pointer(regs) >= first_frame) {
		state->next_bp = bp;
		bp = ((unsigned long *)kernel_stack_pointer(regs)) - 1;
	}

421 422
	/* Initialize stack info and make sure the frame data is accessible: */
	get_stack_info(bp, state->task, &state->stack_info,
423
		       &state->stack_mask);
424
	update_stack_state(state, bp);
425 426 427 428 429 430 431 432

	/*
	 * The caller can provide the address of the first frame directly
	 * (first_frame) or indirectly (regs->sp) to indicate which stack frame
	 * to start unwinding at.  Skip ahead until we reach it.
	 */
	while (!unwind_done(state) &&
	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
433
			(state->next_bp == NULL && state->bp < first_frame)))
434 435 436
		unwind_next_frame(state);
}
EXPORT_SYMBOL_GPL(__unwind_start);