dwarf.c 25.0 KB
Newer Older
M
Matt Fleming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * This is an implementation of a DWARF unwinder. Its main purpose is
 * for generating stacktrace information. Based on the DWARF 3
 * specification from http://www.dwarfstd.org.
 *
 * TODO:
 *	- DWARF64 doesn't work.
14
 *	- Registers with DWARF_VAL_OFFSET rules aren't handled properly.
M
Matt Fleming 已提交
15 16 17 18 19 20
 */

/* #define DEBUG */
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/list.h>
21
#include <linux/mempool.h>
M
Matt Fleming 已提交
22 23 24 25
#include <linux/mm.h>
#include <asm/dwarf.h>
#include <asm/unwinder.h>
#include <asm/sections.h>
26
#include <asm/unaligned.h>
M
Matt Fleming 已提交
27 28 29
#include <asm/dwarf.h>
#include <asm/stacktrace.h>

30 31 32 33 34 35 36 37 38 39 40
/* Reserve enough memory for two stack frames */
#define DWARF_FRAME_MIN_REQ	2
/* ... with 4 registers per frame. */
#define DWARF_REG_MIN_REQ	(DWARF_FRAME_MIN_REQ * 4)

static struct kmem_cache *dwarf_frame_cachep;
static mempool_t *dwarf_frame_pool;

static struct kmem_cache *dwarf_reg_cachep;
static mempool_t *dwarf_reg_pool;

M
Matt Fleming 已提交
41
static LIST_HEAD(dwarf_cie_list);
42
static DEFINE_SPINLOCK(dwarf_cie_lock);
M
Matt Fleming 已提交
43 44

static LIST_HEAD(dwarf_fde_list);
45
static DEFINE_SPINLOCK(dwarf_fde_lock);
M
Matt Fleming 已提交
46 47 48

static struct dwarf_cie *cached_cie;

49 50 51 52 53 54 55 56
/**
 *	dwarf_frame_alloc_reg - allocate memory for a DWARF register
 *	@frame: the DWARF frame whose list of registers we insert on
 *	@reg_num: the register number
 *
 *	Allocate space for, and initialise, a dwarf reg from
 *	dwarf_reg_pool and insert it onto the (unsorted) linked-list of
 *	dwarf registers for @frame.
M
Matt Fleming 已提交
57
 *
58
 *	Return the initialised DWARF reg.
M
Matt Fleming 已提交
59
 */
60 61
static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
					       unsigned int reg_num)
M
Matt Fleming 已提交
62
{
63
	struct dwarf_reg *reg;
M
Matt Fleming 已提交
64

65 66 67
	reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
	if (!reg) {
		printk(KERN_WARNING "Unable to allocate a DWARF register\n");
M
Matt Fleming 已提交
68 69 70 71
		/*
		 * Let's just bomb hard here, we have no way to
		 * gracefully recover.
		 */
72
		UNWINDER_BUG();
M
Matt Fleming 已提交
73 74
	}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	reg->number = reg_num;
	reg->addr = 0;
	reg->flags = 0;

	list_add(&reg->link, &frame->reg_list);

	return reg;
}

static void dwarf_frame_free_regs(struct dwarf_frame *frame)
{
	struct dwarf_reg *reg, *n;

	list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
		list_del(&reg->link);
		mempool_free(reg, dwarf_reg_pool);
	}
}

/**
 *	dwarf_frame_reg - return a DWARF register
 *	@frame: the DWARF frame to search in for @reg_num
 *	@reg_num: the register number to search for
 *
 *	Lookup and return the dwarf reg @reg_num for this frame. Return
 *	NULL if @reg_num is an register invalid number.
 */
static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
					 unsigned int reg_num)
{
	struct dwarf_reg *reg;

	list_for_each_entry(reg, &frame->reg_list, link) {
		if (reg->number == reg_num)
			return reg;
M
Matt Fleming 已提交
110 111
	}

112
	return NULL;
M
Matt Fleming 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
}

/**
 *	dwarf_read_addr - read dwarf data
 *	@src: source address of data
 *	@dst: destination address to store the data to
 *
 *	Read 'n' bytes from @src, where 'n' is the size of an address on
 *	the native machine. We return the number of bytes read, which
 *	should always be 'n'. We also have to be careful when reading
 *	from @src and writing to @dst, because they can be arbitrarily
 *	aligned. Return 'n' - the number of bytes read.
 */
126
static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
M
Matt Fleming 已提交
127
{
128 129
	u32 val = get_unaligned(src);
	put_unaligned(val, dst);
M
Matt Fleming 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	return sizeof(unsigned long *);
}

/**
 *	dwarf_read_uleb128 - read unsigned LEB128 data
 *	@addr: the address where the ULEB128 data is stored
 *	@ret: address to store the result
 *
 *	Decode an unsigned LEB128 encoded datum. The algorithm is taken
 *	from Appendix C of the DWARF 3 spec. For information on the
 *	encodings refer to section "7.6 - Variable Length Data". Return
 *	the number of bytes read.
 */
static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
{
	unsigned int result;
	unsigned char byte;
	int shift, count;

	result = 0;
	shift = 0;
	count = 0;

	while (1) {
		byte = __raw_readb(addr);
		addr++;
		count++;

		result |= (byte & 0x7f) << shift;
		shift += 7;

		if (!(byte & 0x80))
			break;
	}

	*ret = result;

	return count;
}

/**
 *	dwarf_read_leb128 - read signed LEB128 data
 *	@addr: the address of the LEB128 encoded data
 *	@ret: address to store the result
 *
 *	Decode signed LEB128 data. The algorithm is taken from Appendix
 *	C of the DWARF 3 spec. Return the number of bytes read.
 */
static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
{
	unsigned char byte;
	int result, shift;
	int num_bits;
	int count;

	result = 0;
	shift = 0;
	count = 0;

	while (1) {
		byte = __raw_readb(addr);
		addr++;
		result |= (byte & 0x7f) << shift;
		shift += 7;
		count++;

		if (!(byte & 0x80))
			break;
	}

	/* The number of bits in a signed integer. */
	num_bits = 8 * sizeof(result);

	if ((shift < num_bits) && (byte & 0x40))
		result |= (-1 << shift);

	*ret = result;

	return count;
}

/**
 *	dwarf_read_encoded_value - return the decoded value at @addr
 *	@addr: the address of the encoded value
 *	@val: where to write the decoded value
 *	@encoding: the encoding with which we can decode @addr
 *
 *	GCC emits encoded address in the .eh_frame FDE entries. Decode
 *	the value at @addr using @encoding. The decoded value is written
 *	to @val and the number of bytes read is returned.
 */
static int dwarf_read_encoded_value(char *addr, unsigned long *val,
				    char encoding)
{
	unsigned long decoded_addr = 0;
	int count = 0;

	switch (encoding & 0x70) {
	case DW_EH_PE_absptr:
		break;
	case DW_EH_PE_pcrel:
		decoded_addr = (unsigned long)addr;
		break;
	default:
		pr_debug("encoding=0x%x\n", (encoding & 0x70));
235
		UNWINDER_BUG();
M
Matt Fleming 已提交
236 237 238 239 240 241 242 243 244
	}

	if ((encoding & 0x07) == 0x00)
		encoding |= DW_EH_PE_udata4;

	switch (encoding & 0x0f) {
	case DW_EH_PE_sdata4:
	case DW_EH_PE_udata4:
		count += 4;
245
		decoded_addr += get_unaligned((u32 *)addr);
M
Matt Fleming 已提交
246 247 248 249
		__raw_writel(decoded_addr, val);
		break;
	default:
		pr_debug("encoding=0x%x\n", encoding);
250
		UNWINDER_BUG();
M
Matt Fleming 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	}

	return count;
}

/**
 *	dwarf_entry_len - return the length of an FDE or CIE
 *	@addr: the address of the entry
 *	@len: the length of the entry
 *
 *	Read the initial_length field of the entry and store the size of
 *	the entry in @len. We return the number of bytes read. Return a
 *	count of 0 on error.
 */
static inline int dwarf_entry_len(char *addr, unsigned long *len)
{
	u32 initial_len;
	int count;

270
	initial_len = get_unaligned((u32 *)addr);
M
Matt Fleming 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284
	count = 4;

	/*
	 * An initial length field value in the range DW_LEN_EXT_LO -
	 * DW_LEN_EXT_HI indicates an extension, and should not be
	 * interpreted as a length. The only extension that we currently
	 * understand is the use of DWARF64 addresses.
	 */
	if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
		/*
		 * The 64-bit length field immediately follows the
		 * compulsory 32-bit length field.
		 */
		if (initial_len == DW_EXT_DWARF64) {
285
			*len = get_unaligned((u64 *)addr + 4);
M
Matt Fleming 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
			count = 12;
		} else {
			printk(KERN_WARNING "Unknown DWARF extension\n");
			count = 0;
		}
	} else
		*len = initial_len;

	return count;
}

/**
 *	dwarf_lookup_cie - locate the cie
 *	@cie_ptr: pointer to help with lookup
 */
static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
{
303
	struct dwarf_cie *cie;
M
Matt Fleming 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316
	unsigned long flags;

	spin_lock_irqsave(&dwarf_cie_lock, flags);

	/*
	 * We've cached the last CIE we looked up because chances are
	 * that the FDE wants this CIE.
	 */
	if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
		cie = cached_cie;
		goto out;
	}

317
	list_for_each_entry(cie, &dwarf_cie_list, link) {
M
Matt Fleming 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		if (cie->cie_pointer == cie_ptr) {
			cached_cie = cie;
			break;
		}
	}

	/* Couldn't find the entry in the list. */
	if (&cie->link == &dwarf_cie_list)
		cie = NULL;
out:
	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
	return cie;
}

/**
 *	dwarf_lookup_fde - locate the FDE that covers pc
 *	@pc: the program counter
 */
struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
{
338
	struct dwarf_fde *fde;
M
Matt Fleming 已提交
339 340 341
	unsigned long flags;

	spin_lock_irqsave(&dwarf_fde_lock, flags);
342 343

	list_for_each_entry(fde, &dwarf_fde_list, link) {
M
Matt Fleming 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		unsigned long start, end;

		start = fde->initial_location;
		end = fde->initial_location + fde->address_range;

		if (pc >= start && pc < end)
			break;
	}

	/* Couldn't find the entry in the list. */
	if (&fde->link == &dwarf_fde_list)
		fde = NULL;

	spin_unlock_irqrestore(&dwarf_fde_lock, flags);

	return fde;
}

/**
 *	dwarf_cfa_execute_insns - execute instructions to calculate a CFA
 *	@insn_start: address of the first instruction
 *	@insn_end: address of the last instruction
 *	@cie: the CIE for this function
 *	@fde: the FDE for this function
 *	@frame: the instructions calculate the CFA for this frame
 *	@pc: the program counter of the address we're interested in
 *
 *	Execute the Call Frame instruction sequence starting at
 *	@insn_start and ending at @insn_end. The instructions describe
 *	how to calculate the Canonical Frame Address of a stackframe.
 *	Store the results in @frame.
 */
static int dwarf_cfa_execute_insns(unsigned char *insn_start,
				   unsigned char *insn_end,
				   struct dwarf_cie *cie,
				   struct dwarf_fde *fde,
				   struct dwarf_frame *frame,
381
				   unsigned long pc)
M
Matt Fleming 已提交
382 383 384 385
{
	unsigned char insn;
	unsigned char *current_insn;
	unsigned int count, delta, reg, expr_len, offset;
386
	struct dwarf_reg *regp;
M
Matt Fleming 已提交
387 388 389

	current_insn = insn_start;

390
	while (current_insn < insn_end && frame->pc <= pc) {
M
Matt Fleming 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
		insn = __raw_readb(current_insn++);

		/*
		 * Firstly, handle the opcodes that embed their operands
		 * in the instructions.
		 */
		switch (DW_CFA_opcode(insn)) {
		case DW_CFA_advance_loc:
			delta = DW_CFA_operand(insn);
			delta *= cie->code_alignment_factor;
			frame->pc += delta;
			continue;
			/* NOTREACHED */
		case DW_CFA_offset:
			reg = DW_CFA_operand(insn);
			count = dwarf_read_uleb128(current_insn, &offset);
			current_insn += count;
			offset *= cie->data_alignment_factor;
409 410 411
			regp = dwarf_frame_alloc_reg(frame, reg);
			regp->addr = offset;
			regp->flags |= DWARF_REG_OFFSET;
M
Matt Fleming 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			continue;
			/* NOTREACHED */
		case DW_CFA_restore:
			reg = DW_CFA_operand(insn);
			continue;
			/* NOTREACHED */
		}

		/*
		 * Secondly, handle the opcodes that don't embed their
		 * operands in the instruction.
		 */
		switch (insn) {
		case DW_CFA_nop:
			continue;
		case DW_CFA_advance_loc1:
			delta = *current_insn++;
			frame->pc += delta * cie->code_alignment_factor;
			break;
		case DW_CFA_advance_loc2:
432
			delta = get_unaligned((u16 *)current_insn);
M
Matt Fleming 已提交
433 434 435 436
			current_insn += 2;
			frame->pc += delta * cie->code_alignment_factor;
			break;
		case DW_CFA_advance_loc4:
437
			delta = get_unaligned((u32 *)current_insn);
M
Matt Fleming 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
			current_insn += 4;
			frame->pc += delta * cie->code_alignment_factor;
			break;
		case DW_CFA_offset_extended:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
			count = dwarf_read_uleb128(current_insn, &offset);
			current_insn += count;
			offset *= cie->data_alignment_factor;
			break;
		case DW_CFA_restore_extended:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
			break;
		case DW_CFA_undefined:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
455 456
			regp = dwarf_frame_alloc_reg(frame, reg);
			regp->flags |= DWARF_UNDEFINED;
M
Matt Fleming 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
			break;
		case DW_CFA_def_cfa:
			count = dwarf_read_uleb128(current_insn,
						   &frame->cfa_register);
			current_insn += count;
			count = dwarf_read_uleb128(current_insn,
						   &frame->cfa_offset);
			current_insn += count;

			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
			break;
		case DW_CFA_def_cfa_register:
			count = dwarf_read_uleb128(current_insn,
						   &frame->cfa_register);
			current_insn += count;
			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
			break;
		case DW_CFA_def_cfa_offset:
			count = dwarf_read_uleb128(current_insn, &offset);
			current_insn += count;
			frame->cfa_offset = offset;
			break;
		case DW_CFA_def_cfa_expression:
			count = dwarf_read_uleb128(current_insn, &expr_len);
			current_insn += count;

			frame->cfa_expr = current_insn;
			frame->cfa_expr_len = expr_len;
			current_insn += expr_len;

			frame->flags |= DWARF_FRAME_CFA_REG_EXP;
			break;
		case DW_CFA_offset_extended_sf:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
			count = dwarf_read_leb128(current_insn, &offset);
			current_insn += count;
			offset *= cie->data_alignment_factor;
495 496 497
			regp = dwarf_frame_alloc_reg(frame, reg);
			regp->flags |= DWARF_REG_OFFSET;
			regp->addr = offset;
M
Matt Fleming 已提交
498 499 500 501 502 503
			break;
		case DW_CFA_val_offset:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
			count = dwarf_read_leb128(current_insn, &offset);
			offset *= cie->data_alignment_factor;
504
			regp = dwarf_frame_alloc_reg(frame, reg);
505
			regp->flags |= DWARF_VAL_OFFSET;
506
			regp->addr = offset;
M
Matt Fleming 已提交
507
			break;
508 509 510 511 512 513 514 515 516
		case DW_CFA_GNU_args_size:
			count = dwarf_read_uleb128(current_insn, &offset);
			current_insn += count;
			break;
		case DW_CFA_GNU_negative_offset_extended:
			count = dwarf_read_uleb128(current_insn, &reg);
			current_insn += count;
			count = dwarf_read_uleb128(current_insn, &offset);
			offset *= cie->data_alignment_factor;
517 518 519 520

			regp = dwarf_frame_alloc_reg(frame, reg);
			regp->flags |= DWARF_REG_OFFSET;
			regp->addr = -offset;
521
			break;
M
Matt Fleming 已提交
522 523
		default:
			pr_debug("unhandled DWARF instruction 0x%x\n", insn);
524
			UNWINDER_BUG();
M
Matt Fleming 已提交
525 526 527 528 529 530 531
			break;
		}
	}

	return 0;
}

532 533 534 535 536 537 538 539 540 541
/**
 *	dwarf_free_frame - free the memory allocated for @frame
 *	@frame: the frame to free
 */
void dwarf_free_frame(struct dwarf_frame *frame)
{
	dwarf_frame_free_regs(frame);
	mempool_free(frame, dwarf_frame_pool);
}

M
Matt Fleming 已提交
542
/**
543 544
 *	dwarf_unwind_stack - unwind the stack
 *
M
Matt Fleming 已提交
545 546 547 548 549 550 551
 *	@pc: address of the function to unwind
 *	@prev: struct dwarf_frame of the previous stackframe on the callstack
 *
 *	Return a struct dwarf_frame representing the most recent frame
 *	on the callstack. Each of the lower (older) stack frames are
 *	linked via the "prev" member.
 */
552 553
struct dwarf_frame * dwarf_unwind_stack(unsigned long pc,
					struct dwarf_frame *prev)
M
Matt Fleming 已提交
554 555 556 557
{
	struct dwarf_frame *frame;
	struct dwarf_cie *cie;
	struct dwarf_fde *fde;
558
	struct dwarf_reg *reg;
M
Matt Fleming 已提交
559 560 561
	unsigned long addr;

	/*
562 563 564
	 * If we're starting at the top of the stack we need get the
	 * contents of a physical register to get the CFA in order to
	 * begin the virtual unwinding of the stack.
M
Matt Fleming 已提交
565
	 *
566 567
	 * NOTE: the return address is guaranteed to be setup by the
	 * time this function makes its first function call.
M
Matt Fleming 已提交
568
	 */
569 570
	if (!pc && !prev)
		pc = (unsigned long)current_text_addr();
M
Matt Fleming 已提交
571

572 573 574
	frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
	if (!frame) {
		printk(KERN_ERR "Unable to allocate a dwarf frame\n");
575
		UNWINDER_BUG();
576
	}
M
Matt Fleming 已提交
577

578 579
	INIT_LIST_HEAD(&frame->reg_list);
	frame->flags = 0;
M
Matt Fleming 已提交
580
	frame->prev = prev;
581
	frame->return_addr = 0;
M
Matt Fleming 已提交
582 583 584 585

	fde = dwarf_lookup_fde(pc);
	if (!fde) {
		/*
586 587
		 * This is our normal exit path. There are two reasons
		 * why we might exit here,
M
Matt Fleming 已提交
588 589 590 591 592 593 594 595 596 597 598 599
		 *
		 *	a) pc has no asscociated DWARF frame info and so
		 *	we don't know how to unwind this frame. This is
		 *	usually the case when we're trying to unwind a
		 *	frame that was called from some assembly code
		 *	that has no DWARF info, e.g. syscalls.
		 *
		 *	b) the DEBUG info for pc is bogus. There's
		 *	really no way to distinguish this case from the
		 *	case above, which sucks because we could print a
		 *	warning here.
		 */
600
		goto bail;
M
Matt Fleming 已提交
601 602 603 604 605 606 607 608
	}

	cie = dwarf_lookup_cie(fde->cie_pointer);

	frame->pc = fde->initial_location;

	/* CIE initial instructions */
	dwarf_cfa_execute_insns(cie->initial_instructions,
609
				cie->instructions_end, cie, fde,
610
				frame, pc);
M
Matt Fleming 已提交
611 612 613

	/* FDE instructions */
	dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
614
				fde, frame, pc);
M
Matt Fleming 已提交
615 616 617 618 619

	/* Calculate the CFA */
	switch (frame->flags) {
	case DWARF_FRAME_CFA_REG_OFFSET:
		if (prev) {
620
			reg = dwarf_frame_reg(prev, frame->cfa_register);
621 622
			UNWINDER_BUG_ON(!reg);
			UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
M
Matt Fleming 已提交
623

624
			addr = prev->cfa + reg->addr;
M
Matt Fleming 已提交
625 626 627 628
			frame->cfa = __raw_readl(addr);

		} else {
			/*
629 630 631 632
			 * Again, we're starting from the top of the
			 * stack. We need to physically read
			 * the contents of a register in order to get
			 * the Canonical Frame Address for this
M
Matt Fleming 已提交
633 634 635 636 637 638 639 640
			 * function.
			 */
			frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
		}

		frame->cfa += frame->cfa_offset;
		break;
	default:
641
		UNWINDER_BUG();
M
Matt Fleming 已提交
642 643
	}

644
	reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
645 646 647 648 649 650 651 652 653

	/*
	 * If we haven't seen the return address register or the return
	 * address column is undefined then we must assume that this is
	 * the end of the callstack.
	 */
	if (!reg || reg->flags == DWARF_UNDEFINED)
		goto bail;

654
	UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
M
Matt Fleming 已提交
655

656
	addr = frame->cfa + reg->addr;
M
Matt Fleming 已提交
657 658 659
	frame->return_addr = __raw_readl(addr);

	return frame;
660 661

bail:
662
	dwarf_free_frame(frame);
663
	return NULL;
M
Matt Fleming 已提交
664 665 666
}

static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
667
			   unsigned char *end, struct module *mod)
M
Matt Fleming 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
{
	struct dwarf_cie *cie;
	unsigned long flags;
	int count;

	cie = kzalloc(sizeof(*cie), GFP_KERNEL);
	if (!cie)
		return -ENOMEM;

	cie->length = len;

	/*
	 * Record the offset into the .eh_frame section
	 * for this CIE. It allows this CIE to be
	 * quickly and easily looked up from the
	 * corresponding FDE.
	 */
	cie->cie_pointer = (unsigned long)entry;

	cie->version = *(char *)p++;
688
	UNWINDER_BUG_ON(cie->version != 1);
M
Matt Fleming 已提交
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

	cie->augmentation = p;
	p += strlen(cie->augmentation) + 1;

	count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
	p += count;

	count = dwarf_read_leb128(p, &cie->data_alignment_factor);
	p += count;

	/*
	 * Which column in the rule table contains the
	 * return address?
	 */
	if (cie->version == 1) {
		cie->return_address_reg = __raw_readb(p);
		p++;
	} else {
		count = dwarf_read_uleb128(p, &cie->return_address_reg);
		p += count;
	}

	if (cie->augmentation[0] == 'z') {
		unsigned int length, count;
		cie->flags |= DWARF_CIE_Z_AUGMENTATION;

		count = dwarf_read_uleb128(p, &length);
		p += count;

718
		UNWINDER_BUG_ON((unsigned char *)p > end);
M
Matt Fleming 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

		cie->initial_instructions = p + length;
		cie->augmentation++;
	}

	while (*cie->augmentation) {
		/*
		 * "L" indicates a byte showing how the
		 * LSDA pointer is encoded. Skip it.
		 */
		if (*cie->augmentation == 'L') {
			p++;
			cie->augmentation++;
		} else if (*cie->augmentation == 'R') {
			/*
			 * "R" indicates a byte showing
			 * how FDE addresses are
			 * encoded.
			 */
			cie->encoding = *(char *)p++;
			cie->augmentation++;
		} else if (*cie->augmentation == 'P') {
			/*
			 * "R" indicates a personality
			 * routine in the CIE
			 * augmentation.
			 */
746
			UNWINDER_BUG();
M
Matt Fleming 已提交
747
		} else if (*cie->augmentation == 'S') {
748
			UNWINDER_BUG();
M
Matt Fleming 已提交
749 750 751 752 753 754
		} else {
			/*
			 * Unknown augmentation. Assume
			 * 'z' augmentation.
			 */
			p = cie->initial_instructions;
755
			UNWINDER_BUG_ON(!p);
M
Matt Fleming 已提交
756 757 758 759 760 761 762
			break;
		}
	}

	cie->initial_instructions = p;
	cie->instructions_end = end;

763 764
	cie->mod = mod;

M
Matt Fleming 已提交
765 766 767 768 769 770 771 772 773
	/* Add to list */
	spin_lock_irqsave(&dwarf_cie_lock, flags);
	list_add_tail(&cie->link, &dwarf_cie_list);
	spin_unlock_irqrestore(&dwarf_cie_lock, flags);

	return 0;
}

static int dwarf_parse_fde(void *entry, u32 entry_type,
774
			   void *start, unsigned long len,
775
			   unsigned char *end, struct module *mod)
M
Matt Fleming 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
	struct dwarf_fde *fde;
	struct dwarf_cie *cie;
	unsigned long flags;
	int count;
	void *p = start;

	fde = kzalloc(sizeof(*fde), GFP_KERNEL);
	if (!fde)
		return -ENOMEM;

	fde->length = len;

	/*
	 * In a .eh_frame section the CIE pointer is the
	 * delta between the address within the FDE
	 */
	fde->cie_pointer = (unsigned long)(p - entry_type - 4);

	cie = dwarf_lookup_cie(fde->cie_pointer);
	fde->cie = cie;

	if (cie->encoding)
		count = dwarf_read_encoded_value(p, &fde->initial_location,
						 cie->encoding);
	else
		count = dwarf_read_addr(p, &fde->initial_location);

	p += count;

	if (cie->encoding)
		count = dwarf_read_encoded_value(p, &fde->address_range,
						 cie->encoding & 0x0f);
	else
		count = dwarf_read_addr(p, &fde->address_range);

	p += count;

	if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
		unsigned int length;
		count = dwarf_read_uleb128(p, &length);
		p += count + length;
	}

	/* Call frame instructions. */
	fde->instructions = p;
822
	fde->end = end;
M
Matt Fleming 已提交
823

824 825
	fde->mod = mod;

M
Matt Fleming 已提交
826 827 828 829 830 831 832 833
	/* Add to list. */
	spin_lock_irqsave(&dwarf_fde_lock, flags);
	list_add_tail(&fde->link, &dwarf_fde_list);
	spin_unlock_irqrestore(&dwarf_fde_lock, flags);

	return 0;
}

834 835
static void dwarf_unwinder_dump(struct task_struct *task,
				struct pt_regs *regs,
M
Matt Fleming 已提交
836
				unsigned long *sp,
837 838
				const struct stacktrace_ops *ops,
				void *data)
M
Matt Fleming 已提交
839
{
840 841 842 843 844
	struct dwarf_frame *frame, *_frame;
	unsigned long return_addr;

	_frame = NULL;
	return_addr = 0;
M
Matt Fleming 已提交
845

846 847 848
	while (1) {
		frame = dwarf_unwind_stack(return_addr, _frame);

849 850
		if (_frame)
			dwarf_free_frame(_frame);
851 852 853 854 855

		_frame = frame;

		if (!frame || !frame->return_addr)
			break;
M
Matt Fleming 已提交
856

857 858
		return_addr = frame->return_addr;
		ops->address(data, return_addr, 1);
M
Matt Fleming 已提交
859
	}
860 861 862

	if (frame)
		dwarf_free_frame(frame);
M
Matt Fleming 已提交
863 864 865 866 867 868 869 870 871 872
}

static struct unwinder dwarf_unwinder = {
	.name = "dwarf-unwinder",
	.dump = dwarf_unwinder_dump,
	.rating = 150,
};

static void dwarf_unwinder_cleanup(void)
{
873 874
	struct dwarf_cie *cie;
	struct dwarf_fde *fde;
M
Matt Fleming 已提交
875 876 877 878 879 880

	/*
	 * Deallocate all the memory allocated for the DWARF unwinder.
	 * Traverse all the FDE/CIE lists and remove and free all the
	 * memory associated with those data structures.
	 */
881
	list_for_each_entry(cie, &dwarf_cie_list, link)
M
Matt Fleming 已提交
882 883
		kfree(cie);

884
	list_for_each_entry(fde, &dwarf_fde_list, link)
M
Matt Fleming 已提交
885
		kfree(fde);
886 887 888

	kmem_cache_destroy(dwarf_reg_cachep);
	kmem_cache_destroy(dwarf_frame_cachep);
M
Matt Fleming 已提交
889 890 891
}

/**
892 893 894 895
 *	dwarf_parse_section - parse DWARF section
 *	@eh_frame_start: start address of the .eh_frame section
 *	@eh_frame_end: end address of the .eh_frame section
 *	@mod: the kernel module containing the .eh_frame section
M
Matt Fleming 已提交
896
 *
897
 *	Parse the information in a .eh_frame section.
M
Matt Fleming 已提交
898
 */
899 900
int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
			struct module *mod)
M
Matt Fleming 已提交
901 902 903 904 905 906 907 908 909 910
{
	u32 entry_type;
	void *p, *entry;
	int count, err;
	unsigned long len;
	unsigned int c_entries, f_entries;
	unsigned char *end;

	c_entries = 0;
	f_entries = 0;
911
	entry = eh_frame_start;
912

913
	while ((char *)entry < eh_frame_end) {
M
Matt Fleming 已提交
914 915 916 917 918 919 920 921 922 923 924
		p = entry;

		count = dwarf_entry_len(p, &len);
		if (count == 0) {
			/*
			 * We read a bogus length field value. There is
			 * nothing we can do here apart from disabling
			 * the DWARF unwinder. We can't even skip this
			 * entry and move to the next one because 'len'
			 * tells us where our next entry is.
			 */
925
			err = -EINVAL;
M
Matt Fleming 已提交
926 927 928 929 930 931 932
			goto out;
		} else
			p += count;

		/* initial length does not include itself */
		end = p + len;

933
		entry_type = get_unaligned((u32 *)p);
M
Matt Fleming 已提交
934 935 936
		p += 4;

		if (entry_type == DW_EH_FRAME_CIE) {
937
			err = dwarf_parse_cie(entry, p, len, end, mod);
M
Matt Fleming 已提交
938 939 940 941 942
			if (err < 0)
				goto out;
			else
				c_entries++;
		} else {
943 944
			err = dwarf_parse_fde(entry, entry_type, p, len,
					      end, mod);
M
Matt Fleming 已提交
945 946 947 948 949 950 951 952 953 954 955 956
			if (err < 0)
				goto out;
			else
				f_entries++;
		}

		entry = (char *)entry + len + 4;
	}

	printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
	       c_entries, f_entries);

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	return 0;

out:
	return err;
}

/**
 *	dwarf_module_unload - remove FDE/CIEs associated with @mod
 *	@mod: the module that is being unloaded
 *
 *	Remove any FDEs and CIEs from the global lists that came from
 *	@mod's .eh_frame section because @mod is being unloaded.
 */
void dwarf_module_unload(struct module *mod)
{
	struct dwarf_fde *fde;
	struct dwarf_cie *cie;
	unsigned long flags;

	spin_lock_irqsave(&dwarf_cie_lock, flags);

again_cie:
	list_for_each_entry(cie, &dwarf_cie_list, link) {
		if (cie->mod == mod)
			break;
	}

	if (&cie->link != &dwarf_cie_list) {
		list_del(&cie->link);
		kfree(cie);
		goto again_cie;
	}

	spin_unlock_irqrestore(&dwarf_cie_lock, flags);

	spin_lock_irqsave(&dwarf_fde_lock, flags);

again_fde:
	list_for_each_entry(fde, &dwarf_fde_list, link) {
		if (fde->mod == mod)
			break;
	}

	if (&fde->link != &dwarf_fde_list) {
		list_del(&fde->link);
		kfree(fde);
		goto again_fde;
	}

	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
}

/**
 *	dwarf_unwinder_init - initialise the dwarf unwinder
 *
 *	Build the data structures describing the .dwarf_frame section to
 *	make it easier to lookup CIE and FDE entries. Because the
 *	.eh_frame section is packed as tightly as possible it is not
 *	easy to lookup the FDE for a given PC, so we build a list of FDE
 *	and CIE entries that make it easier.
 */
static int __init dwarf_unwinder_init(void)
{
	int err;
	INIT_LIST_HEAD(&dwarf_cie_list);
	INIT_LIST_HEAD(&dwarf_fde_list);

	dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
			sizeof(struct dwarf_frame), 0, SLAB_PANIC, NULL);
	dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
			sizeof(struct dwarf_reg), 0, SLAB_PANIC, NULL);

	dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
					  mempool_alloc_slab,
					  mempool_free_slab,
					  dwarf_frame_cachep);

	dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
					 mempool_alloc_slab,
					 mempool_free_slab,
					 dwarf_reg_cachep);

	err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
	if (err)
		goto out;

M
Matt Fleming 已提交
1043 1044 1045 1046
	err = unwinder_register(&dwarf_unwinder);
	if (err)
		goto out;

1047
	return 0;
M
Matt Fleming 已提交
1048 1049 1050 1051

out:
	printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
	dwarf_unwinder_cleanup();
1052
	return -EINVAL;
M
Matt Fleming 已提交
1053
}
1054
early_initcall(dwarf_unwinder_init);