1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org> 4 * 5 * This is an implementation of a DWARF unwinder. Its main purpose is 6 * for generating stacktrace information. Based on the DWARF 3 7 * specification from http://www.dwarfstd.org. 8 * 9 * TODO: 10 * - DWARF64 doesn't work. 11 * - Registers with DWARF_VAL_OFFSET rules aren't handled properly. 12 */ 13 14 /* #define DEBUG */ 15 #include <linux/kernel.h> 16 #include <linux/io.h> 17 #include <linux/list.h> 18 #include <linux/mempool.h> 19 #include <linux/mm.h> 20 #include <linux/elf.h> 21 #include <linux/ftrace.h> 22 #include <linux/module.h> 23 #include <linux/slab.h> 24 #include <asm/dwarf.h> 25 #include <asm/unwinder.h> 26 #include <asm/sections.h> 27 #include <asm/unaligned.h> 28 #include <asm/stacktrace.h> 29 30 /* Reserve enough memory for two stack frames */ 31 #define DWARF_FRAME_MIN_REQ 2 32 /* ... with 4 registers per frame. */ 33 #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4) 34 35 static struct kmem_cache *dwarf_frame_cachep; 36 static mempool_t *dwarf_frame_pool; 37 38 static struct kmem_cache *dwarf_reg_cachep; 39 static mempool_t *dwarf_reg_pool; 40 41 static struct rb_root cie_root; 42 static DEFINE_SPINLOCK(dwarf_cie_lock); 43 44 static struct rb_root fde_root; 45 static DEFINE_SPINLOCK(dwarf_fde_lock); 46 47 static struct dwarf_cie *cached_cie; 48 49 static unsigned int dwarf_unwinder_ready; 50 51 /** 52 * dwarf_frame_alloc_reg - allocate memory for a DWARF register 53 * @frame: the DWARF frame whose list of registers we insert on 54 * @reg_num: the register number 55 * 56 * Allocate space for, and initialise, a dwarf reg from 57 * dwarf_reg_pool and insert it onto the (unsorted) linked-list of 58 * dwarf registers for @frame. 59 * 60 * Return the initialised DWARF reg. 61 */ 62 static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame, 63 unsigned int reg_num) 64 { 65 struct dwarf_reg *reg; 66 67 reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC); 68 if (!reg) { 69 printk(KERN_WARNING "Unable to allocate a DWARF register\n"); 70 /* 71 * Let's just bomb hard here, we have no way to 72 * gracefully recover. 73 */ 74 UNWINDER_BUG(); 75 } 76 77 reg->number = reg_num; 78 reg->addr = 0; 79 reg->flags = 0; 80 81 list_add(®->link, &frame->reg_list); 82 83 return reg; 84 } 85 86 static void dwarf_frame_free_regs(struct dwarf_frame *frame) 87 { 88 struct dwarf_reg *reg, *n; 89 90 list_for_each_entry_safe(reg, n, &frame->reg_list, link) { 91 list_del(®->link); 92 mempool_free(reg, dwarf_reg_pool); 93 } 94 } 95 96 /** 97 * dwarf_frame_reg - return a DWARF register 98 * @frame: the DWARF frame to search in for @reg_num 99 * @reg_num: the register number to search for 100 * 101 * Lookup and return the dwarf reg @reg_num for this frame. Return 102 * NULL if @reg_num is an register invalid number. 103 */ 104 static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame, 105 unsigned int reg_num) 106 { 107 struct dwarf_reg *reg; 108 109 list_for_each_entry(reg, &frame->reg_list, link) { 110 if (reg->number == reg_num) 111 return reg; 112 } 113 114 return NULL; 115 } 116 117 /** 118 * dwarf_read_addr - read dwarf data 119 * @src: source address of data 120 * @dst: destination address to store the data to 121 * 122 * Read 'n' bytes from @src, where 'n' is the size of an address on 123 * the native machine. We return the number of bytes read, which 124 * should always be 'n'. We also have to be careful when reading 125 * from @src and writing to @dst, because they can be arbitrarily 126 * aligned. Return 'n' - the number of bytes read. 127 */ 128 static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst) 129 { 130 u32 val = get_unaligned(src); 131 put_unaligned(val, dst); 132 return sizeof(unsigned long *); 133 } 134 135 /** 136 * dwarf_read_uleb128 - read unsigned LEB128 data 137 * @addr: the address where the ULEB128 data is stored 138 * @ret: address to store the result 139 * 140 * Decode an unsigned LEB128 encoded datum. The algorithm is taken 141 * from Appendix C of the DWARF 3 spec. For information on the 142 * encodings refer to section "7.6 - Variable Length Data". Return 143 * the number of bytes read. 144 */ 145 static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret) 146 { 147 unsigned int result; 148 unsigned char byte; 149 int shift, count; 150 151 result = 0; 152 shift = 0; 153 count = 0; 154 155 while (1) { 156 byte = __raw_readb(addr); 157 addr++; 158 count++; 159 160 result |= (byte & 0x7f) << shift; 161 shift += 7; 162 163 if (!(byte & 0x80)) 164 break; 165 } 166 167 *ret = result; 168 169 return count; 170 } 171 172 /** 173 * dwarf_read_leb128 - read signed LEB128 data 174 * @addr: the address of the LEB128 encoded data 175 * @ret: address to store the result 176 * 177 * Decode signed LEB128 data. The algorithm is taken from Appendix 178 * C of the DWARF 3 spec. Return the number of bytes read. 179 */ 180 static inline unsigned long dwarf_read_leb128(char *addr, int *ret) 181 { 182 unsigned char byte; 183 int result, shift; 184 int num_bits; 185 int count; 186 187 result = 0; 188 shift = 0; 189 count = 0; 190 191 while (1) { 192 byte = __raw_readb(addr); 193 addr++; 194 result |= (byte & 0x7f) << shift; 195 shift += 7; 196 count++; 197 198 if (!(byte & 0x80)) 199 break; 200 } 201 202 /* The number of bits in a signed integer. */ 203 num_bits = 8 * sizeof(result); 204 205 if ((shift < num_bits) && (byte & 0x40)) 206 result |= (-1 << shift); 207 208 *ret = result; 209 210 return count; 211 } 212 213 /** 214 * dwarf_read_encoded_value - return the decoded value at @addr 215 * @addr: the address of the encoded value 216 * @val: where to write the decoded value 217 * @encoding: the encoding with which we can decode @addr 218 * 219 * GCC emits encoded address in the .eh_frame FDE entries. Decode 220 * the value at @addr using @encoding. The decoded value is written 221 * to @val and the number of bytes read is returned. 222 */ 223 static int dwarf_read_encoded_value(char *addr, unsigned long *val, 224 char encoding) 225 { 226 unsigned long decoded_addr = 0; 227 int count = 0; 228 229 switch (encoding & 0x70) { 230 case DW_EH_PE_absptr: 231 break; 232 case DW_EH_PE_pcrel: 233 decoded_addr = (unsigned long)addr; 234 break; 235 default: 236 pr_debug("encoding=0x%x\n", (encoding & 0x70)); 237 UNWINDER_BUG(); 238 } 239 240 if ((encoding & 0x07) == 0x00) 241 encoding |= DW_EH_PE_udata4; 242 243 switch (encoding & 0x0f) { 244 case DW_EH_PE_sdata4: 245 case DW_EH_PE_udata4: 246 count += 4; 247 decoded_addr += get_unaligned((u32 *)addr); 248 __raw_writel(decoded_addr, val); 249 break; 250 default: 251 pr_debug("encoding=0x%x\n", encoding); 252 UNWINDER_BUG(); 253 } 254 255 return count; 256 } 257 258 /** 259 * dwarf_entry_len - return the length of an FDE or CIE 260 * @addr: the address of the entry 261 * @len: the length of the entry 262 * 263 * Read the initial_length field of the entry and store the size of 264 * the entry in @len. We return the number of bytes read. Return a 265 * count of 0 on error. 266 */ 267 static inline int dwarf_entry_len(char *addr, unsigned long *len) 268 { 269 u32 initial_len; 270 int count; 271 272 initial_len = get_unaligned((u32 *)addr); 273 count = 4; 274 275 /* 276 * An initial length field value in the range DW_LEN_EXT_LO - 277 * DW_LEN_EXT_HI indicates an extension, and should not be 278 * interpreted as a length. The only extension that we currently 279 * understand is the use of DWARF64 addresses. 280 */ 281 if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) { 282 /* 283 * The 64-bit length field immediately follows the 284 * compulsory 32-bit length field. 285 */ 286 if (initial_len == DW_EXT_DWARF64) { 287 *len = get_unaligned((u64 *)addr + 4); 288 count = 12; 289 } else { 290 printk(KERN_WARNING "Unknown DWARF extension\n"); 291 count = 0; 292 } 293 } else 294 *len = initial_len; 295 296 return count; 297 } 298 299 /** 300 * dwarf_lookup_cie - locate the cie 301 * @cie_ptr: pointer to help with lookup 302 */ 303 static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr) 304 { 305 struct rb_node **rb_node = &cie_root.rb_node; 306 struct dwarf_cie *cie = NULL; 307 unsigned long flags; 308 309 spin_lock_irqsave(&dwarf_cie_lock, flags); 310 311 /* 312 * We've cached the last CIE we looked up because chances are 313 * that the FDE wants this CIE. 314 */ 315 if (cached_cie && cached_cie->cie_pointer == cie_ptr) { 316 cie = cached_cie; 317 goto out; 318 } 319 320 while (*rb_node) { 321 struct dwarf_cie *cie_tmp; 322 323 cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node); 324 BUG_ON(!cie_tmp); 325 326 if (cie_ptr == cie_tmp->cie_pointer) { 327 cie = cie_tmp; 328 cached_cie = cie_tmp; 329 goto out; 330 } else { 331 if (cie_ptr < cie_tmp->cie_pointer) 332 rb_node = &(*rb_node)->rb_left; 333 else 334 rb_node = &(*rb_node)->rb_right; 335 } 336 } 337 338 out: 339 spin_unlock_irqrestore(&dwarf_cie_lock, flags); 340 return cie; 341 } 342 343 /** 344 * dwarf_lookup_fde - locate the FDE that covers pc 345 * @pc: the program counter 346 */ 347 struct dwarf_fde *dwarf_lookup_fde(unsigned long pc) 348 { 349 struct rb_node **rb_node = &fde_root.rb_node; 350 struct dwarf_fde *fde = NULL; 351 unsigned long flags; 352 353 spin_lock_irqsave(&dwarf_fde_lock, flags); 354 355 while (*rb_node) { 356 struct dwarf_fde *fde_tmp; 357 unsigned long tmp_start, tmp_end; 358 359 fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node); 360 BUG_ON(!fde_tmp); 361 362 tmp_start = fde_tmp->initial_location; 363 tmp_end = fde_tmp->initial_location + fde_tmp->address_range; 364 365 if (pc < tmp_start) { 366 rb_node = &(*rb_node)->rb_left; 367 } else { 368 if (pc < tmp_end) { 369 fde = fde_tmp; 370 goto out; 371 } else 372 rb_node = &(*rb_node)->rb_right; 373 } 374 } 375 376 out: 377 spin_unlock_irqrestore(&dwarf_fde_lock, flags); 378 379 return fde; 380 } 381 382 /** 383 * dwarf_cfa_execute_insns - execute instructions to calculate a CFA 384 * @insn_start: address of the first instruction 385 * @insn_end: address of the last instruction 386 * @cie: the CIE for this function 387 * @fde: the FDE for this function 388 * @frame: the instructions calculate the CFA for this frame 389 * @pc: the program counter of the address we're interested in 390 * 391 * Execute the Call Frame instruction sequence starting at 392 * @insn_start and ending at @insn_end. The instructions describe 393 * how to calculate the Canonical Frame Address of a stackframe. 394 * Store the results in @frame. 395 */ 396 static int dwarf_cfa_execute_insns(unsigned char *insn_start, 397 unsigned char *insn_end, 398 struct dwarf_cie *cie, 399 struct dwarf_fde *fde, 400 struct dwarf_frame *frame, 401 unsigned long pc) 402 { 403 unsigned char insn; 404 unsigned char *current_insn; 405 unsigned int count, delta, reg, expr_len, offset; 406 struct dwarf_reg *regp; 407 408 current_insn = insn_start; 409 410 while (current_insn < insn_end && frame->pc <= pc) { 411 insn = __raw_readb(current_insn++); 412 413 /* 414 * Firstly, handle the opcodes that embed their operands 415 * in the instructions. 416 */ 417 switch (DW_CFA_opcode(insn)) { 418 case DW_CFA_advance_loc: 419 delta = DW_CFA_operand(insn); 420 delta *= cie->code_alignment_factor; 421 frame->pc += delta; 422 continue; 423 /* NOTREACHED */ 424 case DW_CFA_offset: 425 reg = DW_CFA_operand(insn); 426 count = dwarf_read_uleb128(current_insn, &offset); 427 current_insn += count; 428 offset *= cie->data_alignment_factor; 429 regp = dwarf_frame_alloc_reg(frame, reg); 430 regp->addr = offset; 431 regp->flags |= DWARF_REG_OFFSET; 432 continue; 433 /* NOTREACHED */ 434 case DW_CFA_restore: 435 reg = DW_CFA_operand(insn); 436 continue; 437 /* NOTREACHED */ 438 } 439 440 /* 441 * Secondly, handle the opcodes that don't embed their 442 * operands in the instruction. 443 */ 444 switch (insn) { 445 case DW_CFA_nop: 446 continue; 447 case DW_CFA_advance_loc1: 448 delta = *current_insn++; 449 frame->pc += delta * cie->code_alignment_factor; 450 break; 451 case DW_CFA_advance_loc2: 452 delta = get_unaligned((u16 *)current_insn); 453 current_insn += 2; 454 frame->pc += delta * cie->code_alignment_factor; 455 break; 456 case DW_CFA_advance_loc4: 457 delta = get_unaligned((u32 *)current_insn); 458 current_insn += 4; 459 frame->pc += delta * cie->code_alignment_factor; 460 break; 461 case DW_CFA_offset_extended: 462 count = dwarf_read_uleb128(current_insn, ®); 463 current_insn += count; 464 count = dwarf_read_uleb128(current_insn, &offset); 465 current_insn += count; 466 offset *= cie->data_alignment_factor; 467 break; 468 case DW_CFA_restore_extended: 469 count = dwarf_read_uleb128(current_insn, ®); 470 current_insn += count; 471 break; 472 case DW_CFA_undefined: 473 count = dwarf_read_uleb128(current_insn, ®); 474 current_insn += count; 475 regp = dwarf_frame_alloc_reg(frame, reg); 476 regp->flags |= DWARF_UNDEFINED; 477 break; 478 case DW_CFA_def_cfa: 479 count = dwarf_read_uleb128(current_insn, 480 &frame->cfa_register); 481 current_insn += count; 482 count = dwarf_read_uleb128(current_insn, 483 &frame->cfa_offset); 484 current_insn += count; 485 486 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET; 487 break; 488 case DW_CFA_def_cfa_register: 489 count = dwarf_read_uleb128(current_insn, 490 &frame->cfa_register); 491 current_insn += count; 492 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET; 493 break; 494 case DW_CFA_def_cfa_offset: 495 count = dwarf_read_uleb128(current_insn, &offset); 496 current_insn += count; 497 frame->cfa_offset = offset; 498 break; 499 case DW_CFA_def_cfa_expression: 500 count = dwarf_read_uleb128(current_insn, &expr_len); 501 current_insn += count; 502 503 frame->cfa_expr = current_insn; 504 frame->cfa_expr_len = expr_len; 505 current_insn += expr_len; 506 507 frame->flags |= DWARF_FRAME_CFA_REG_EXP; 508 break; 509 case DW_CFA_offset_extended_sf: 510 count = dwarf_read_uleb128(current_insn, ®); 511 current_insn += count; 512 count = dwarf_read_leb128(current_insn, &offset); 513 current_insn += count; 514 offset *= cie->data_alignment_factor; 515 regp = dwarf_frame_alloc_reg(frame, reg); 516 regp->flags |= DWARF_REG_OFFSET; 517 regp->addr = offset; 518 break; 519 case DW_CFA_val_offset: 520 count = dwarf_read_uleb128(current_insn, ®); 521 current_insn += count; 522 count = dwarf_read_leb128(current_insn, &offset); 523 offset *= cie->data_alignment_factor; 524 regp = dwarf_frame_alloc_reg(frame, reg); 525 regp->flags |= DWARF_VAL_OFFSET; 526 regp->addr = offset; 527 break; 528 case DW_CFA_GNU_args_size: 529 count = dwarf_read_uleb128(current_insn, &offset); 530 current_insn += count; 531 break; 532 case DW_CFA_GNU_negative_offset_extended: 533 count = dwarf_read_uleb128(current_insn, ®); 534 current_insn += count; 535 count = dwarf_read_uleb128(current_insn, &offset); 536 offset *= cie->data_alignment_factor; 537 538 regp = dwarf_frame_alloc_reg(frame, reg); 539 regp->flags |= DWARF_REG_OFFSET; 540 regp->addr = -offset; 541 break; 542 default: 543 pr_debug("unhandled DWARF instruction 0x%x\n", insn); 544 UNWINDER_BUG(); 545 break; 546 } 547 } 548 549 return 0; 550 } 551 552 /** 553 * dwarf_free_frame - free the memory allocated for @frame 554 * @frame: the frame to free 555 */ 556 void dwarf_free_frame(struct dwarf_frame *frame) 557 { 558 dwarf_frame_free_regs(frame); 559 mempool_free(frame, dwarf_frame_pool); 560 } 561 562 extern void ret_from_irq(void); 563 564 /** 565 * dwarf_unwind_stack - unwind the stack 566 * 567 * @pc: address of the function to unwind 568 * @prev: struct dwarf_frame of the previous stackframe on the callstack 569 * 570 * Return a struct dwarf_frame representing the most recent frame 571 * on the callstack. Each of the lower (older) stack frames are 572 * linked via the "prev" member. 573 */ 574 struct dwarf_frame *dwarf_unwind_stack(unsigned long pc, 575 struct dwarf_frame *prev) 576 { 577 struct dwarf_frame *frame; 578 struct dwarf_cie *cie; 579 struct dwarf_fde *fde; 580 struct dwarf_reg *reg; 581 unsigned long addr; 582 583 /* 584 * If we've been called in to before initialization has 585 * completed, bail out immediately. 586 */ 587 if (!dwarf_unwinder_ready) 588 return NULL; 589 590 /* 591 * If we're starting at the top of the stack we need get the 592 * contents of a physical register to get the CFA in order to 593 * begin the virtual unwinding of the stack. 594 * 595 * NOTE: the return address is guaranteed to be setup by the 596 * time this function makes its first function call. 597 */ 598 if (!pc || !prev) 599 pc = _THIS_IP_; 600 601 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 602 /* 603 * If our stack has been patched by the function graph tracer 604 * then we might see the address of return_to_handler() where we 605 * expected to find the real return address. 606 */ 607 if (pc == (unsigned long)&return_to_handler) { 608 int index = current->curr_ret_stack; 609 610 /* 611 * We currently have no way of tracking how many 612 * return_to_handler()'s we've seen. If there is more 613 * than one patched return address on our stack, 614 * complain loudly. 615 */ 616 WARN_ON(index > 0); 617 618 pc = current->ret_stack[index].ret; 619 } 620 #endif 621 622 frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC); 623 if (!frame) { 624 printk(KERN_ERR "Unable to allocate a dwarf frame\n"); 625 UNWINDER_BUG(); 626 } 627 628 INIT_LIST_HEAD(&frame->reg_list); 629 frame->flags = 0; 630 frame->prev = prev; 631 frame->return_addr = 0; 632 633 fde = dwarf_lookup_fde(pc); 634 if (!fde) { 635 /* 636 * This is our normal exit path. There are two reasons 637 * why we might exit here, 638 * 639 * a) pc has no asscociated DWARF frame info and so 640 * we don't know how to unwind this frame. This is 641 * usually the case when we're trying to unwind a 642 * frame that was called from some assembly code 643 * that has no DWARF info, e.g. syscalls. 644 * 645 * b) the DEBUG info for pc is bogus. There's 646 * really no way to distinguish this case from the 647 * case above, which sucks because we could print a 648 * warning here. 649 */ 650 goto bail; 651 } 652 653 cie = dwarf_lookup_cie(fde->cie_pointer); 654 655 frame->pc = fde->initial_location; 656 657 /* CIE initial instructions */ 658 dwarf_cfa_execute_insns(cie->initial_instructions, 659 cie->instructions_end, cie, fde, 660 frame, pc); 661 662 /* FDE instructions */ 663 dwarf_cfa_execute_insns(fde->instructions, fde->end, cie, 664 fde, frame, pc); 665 666 /* Calculate the CFA */ 667 switch (frame->flags) { 668 case DWARF_FRAME_CFA_REG_OFFSET: 669 if (prev) { 670 reg = dwarf_frame_reg(prev, frame->cfa_register); 671 UNWINDER_BUG_ON(!reg); 672 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET); 673 674 addr = prev->cfa + reg->addr; 675 frame->cfa = __raw_readl(addr); 676 677 } else { 678 /* 679 * Again, we're starting from the top of the 680 * stack. We need to physically read 681 * the contents of a register in order to get 682 * the Canonical Frame Address for this 683 * function. 684 */ 685 frame->cfa = dwarf_read_arch_reg(frame->cfa_register); 686 } 687 688 frame->cfa += frame->cfa_offset; 689 break; 690 default: 691 UNWINDER_BUG(); 692 } 693 694 reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG); 695 696 /* 697 * If we haven't seen the return address register or the return 698 * address column is undefined then we must assume that this is 699 * the end of the callstack. 700 */ 701 if (!reg || reg->flags == DWARF_UNDEFINED) 702 goto bail; 703 704 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET); 705 706 addr = frame->cfa + reg->addr; 707 frame->return_addr = __raw_readl(addr); 708 709 /* 710 * Ah, the joys of unwinding through interrupts. 711 * 712 * Interrupts are tricky - the DWARF info needs to be _really_ 713 * accurate and unfortunately I'm seeing a lot of bogus DWARF 714 * info. For example, I've seen interrupts occur in epilogues 715 * just after the frame pointer (r14) had been restored. The 716 * problem was that the DWARF info claimed that the CFA could be 717 * reached by using the value of the frame pointer before it was 718 * restored. 719 * 720 * So until the compiler can be trusted to produce reliable 721 * DWARF info when it really matters, let's stop unwinding once 722 * we've calculated the function that was interrupted. 723 */ 724 if (prev && prev->pc == (unsigned long)ret_from_irq) 725 frame->return_addr = 0; 726 727 return frame; 728 729 bail: 730 dwarf_free_frame(frame); 731 return NULL; 732 } 733 734 static int dwarf_parse_cie(void *entry, void *p, unsigned long len, 735 unsigned char *end, struct module *mod) 736 { 737 struct rb_node **rb_node = &cie_root.rb_node; 738 struct rb_node *parent = *rb_node; 739 struct dwarf_cie *cie; 740 unsigned long flags; 741 int count; 742 743 cie = kzalloc(sizeof(*cie), GFP_KERNEL); 744 if (!cie) 745 return -ENOMEM; 746 747 cie->length = len; 748 749 /* 750 * Record the offset into the .eh_frame section 751 * for this CIE. It allows this CIE to be 752 * quickly and easily looked up from the 753 * corresponding FDE. 754 */ 755 cie->cie_pointer = (unsigned long)entry; 756 757 cie->version = *(char *)p++; 758 UNWINDER_BUG_ON(cie->version != 1); 759 760 cie->augmentation = p; 761 p += strlen(cie->augmentation) + 1; 762 763 count = dwarf_read_uleb128(p, &cie->code_alignment_factor); 764 p += count; 765 766 count = dwarf_read_leb128(p, &cie->data_alignment_factor); 767 p += count; 768 769 /* 770 * Which column in the rule table contains the 771 * return address? 772 */ 773 if (cie->version == 1) { 774 cie->return_address_reg = __raw_readb(p); 775 p++; 776 } else { 777 count = dwarf_read_uleb128(p, &cie->return_address_reg); 778 p += count; 779 } 780 781 if (cie->augmentation[0] == 'z') { 782 unsigned int length, count; 783 cie->flags |= DWARF_CIE_Z_AUGMENTATION; 784 785 count = dwarf_read_uleb128(p, &length); 786 p += count; 787 788 UNWINDER_BUG_ON((unsigned char *)p > end); 789 790 cie->initial_instructions = p + length; 791 cie->augmentation++; 792 } 793 794 while (*cie->augmentation) { 795 /* 796 * "L" indicates a byte showing how the 797 * LSDA pointer is encoded. Skip it. 798 */ 799 if (*cie->augmentation == 'L') { 800 p++; 801 cie->augmentation++; 802 } else if (*cie->augmentation == 'R') { 803 /* 804 * "R" indicates a byte showing 805 * how FDE addresses are 806 * encoded. 807 */ 808 cie->encoding = *(char *)p++; 809 cie->augmentation++; 810 } else if (*cie->augmentation == 'P') { 811 /* 812 * "R" indicates a personality 813 * routine in the CIE 814 * augmentation. 815 */ 816 UNWINDER_BUG(); 817 } else if (*cie->augmentation == 'S') { 818 UNWINDER_BUG(); 819 } else { 820 /* 821 * Unknown augmentation. Assume 822 * 'z' augmentation. 823 */ 824 p = cie->initial_instructions; 825 UNWINDER_BUG_ON(!p); 826 break; 827 } 828 } 829 830 cie->initial_instructions = p; 831 cie->instructions_end = end; 832 833 /* Add to list */ 834 spin_lock_irqsave(&dwarf_cie_lock, flags); 835 836 while (*rb_node) { 837 struct dwarf_cie *cie_tmp; 838 839 cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node); 840 841 parent = *rb_node; 842 843 if (cie->cie_pointer < cie_tmp->cie_pointer) 844 rb_node = &parent->rb_left; 845 else if (cie->cie_pointer >= cie_tmp->cie_pointer) 846 rb_node = &parent->rb_right; 847 else 848 WARN_ON(1); 849 } 850 851 rb_link_node(&cie->node, parent, rb_node); 852 rb_insert_color(&cie->node, &cie_root); 853 854 #ifdef CONFIG_MODULES 855 if (mod != NULL) 856 list_add_tail(&cie->link, &mod->arch.cie_list); 857 #endif 858 859 spin_unlock_irqrestore(&dwarf_cie_lock, flags); 860 861 return 0; 862 } 863 864 static int dwarf_parse_fde(void *entry, u32 entry_type, 865 void *start, unsigned long len, 866 unsigned char *end, struct module *mod) 867 { 868 struct rb_node **rb_node = &fde_root.rb_node; 869 struct rb_node *parent = *rb_node; 870 struct dwarf_fde *fde; 871 struct dwarf_cie *cie; 872 unsigned long flags; 873 int count; 874 void *p = start; 875 876 fde = kzalloc(sizeof(*fde), GFP_KERNEL); 877 if (!fde) 878 return -ENOMEM; 879 880 fde->length = len; 881 882 /* 883 * In a .eh_frame section the CIE pointer is the 884 * delta between the address within the FDE 885 */ 886 fde->cie_pointer = (unsigned long)(p - entry_type - 4); 887 888 cie = dwarf_lookup_cie(fde->cie_pointer); 889 fde->cie = cie; 890 891 if (cie->encoding) 892 count = dwarf_read_encoded_value(p, &fde->initial_location, 893 cie->encoding); 894 else 895 count = dwarf_read_addr(p, &fde->initial_location); 896 897 p += count; 898 899 if (cie->encoding) 900 count = dwarf_read_encoded_value(p, &fde->address_range, 901 cie->encoding & 0x0f); 902 else 903 count = dwarf_read_addr(p, &fde->address_range); 904 905 p += count; 906 907 if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) { 908 unsigned int length; 909 count = dwarf_read_uleb128(p, &length); 910 p += count + length; 911 } 912 913 /* Call frame instructions. */ 914 fde->instructions = p; 915 fde->end = end; 916 917 /* Add to list. */ 918 spin_lock_irqsave(&dwarf_fde_lock, flags); 919 920 while (*rb_node) { 921 struct dwarf_fde *fde_tmp; 922 unsigned long tmp_start, tmp_end; 923 unsigned long start, end; 924 925 fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node); 926 927 start = fde->initial_location; 928 end = fde->initial_location + fde->address_range; 929 930 tmp_start = fde_tmp->initial_location; 931 tmp_end = fde_tmp->initial_location + fde_tmp->address_range; 932 933 parent = *rb_node; 934 935 if (start < tmp_start) 936 rb_node = &parent->rb_left; 937 else if (start >= tmp_end) 938 rb_node = &parent->rb_right; 939 else 940 WARN_ON(1); 941 } 942 943 rb_link_node(&fde->node, parent, rb_node); 944 rb_insert_color(&fde->node, &fde_root); 945 946 #ifdef CONFIG_MODULES 947 if (mod != NULL) 948 list_add_tail(&fde->link, &mod->arch.fde_list); 949 #endif 950 951 spin_unlock_irqrestore(&dwarf_fde_lock, flags); 952 953 return 0; 954 } 955 956 static void dwarf_unwinder_dump(struct task_struct *task, 957 struct pt_regs *regs, 958 unsigned long *sp, 959 const struct stacktrace_ops *ops, 960 void *data) 961 { 962 struct dwarf_frame *frame, *_frame; 963 unsigned long return_addr; 964 965 _frame = NULL; 966 return_addr = 0; 967 968 while (1) { 969 frame = dwarf_unwind_stack(return_addr, _frame); 970 971 if (_frame) 972 dwarf_free_frame(_frame); 973 974 _frame = frame; 975 976 if (!frame || !frame->return_addr) 977 break; 978 979 return_addr = frame->return_addr; 980 ops->address(data, return_addr, 1); 981 } 982 983 if (frame) 984 dwarf_free_frame(frame); 985 } 986 987 static struct unwinder dwarf_unwinder = { 988 .name = "dwarf-unwinder", 989 .dump = dwarf_unwinder_dump, 990 .rating = 150, 991 }; 992 993 static void __init dwarf_unwinder_cleanup(void) 994 { 995 struct dwarf_fde *fde, *next_fde; 996 struct dwarf_cie *cie, *next_cie; 997 998 /* 999 * Deallocate all the memory allocated for the DWARF unwinder. 1000 * Traverse all the FDE/CIE lists and remove and free all the 1001 * memory associated with those data structures. 1002 */ 1003 rbtree_postorder_for_each_entry_safe(fde, next_fde, &fde_root, node) 1004 kfree(fde); 1005 1006 rbtree_postorder_for_each_entry_safe(cie, next_cie, &cie_root, node) 1007 kfree(cie); 1008 1009 mempool_destroy(dwarf_reg_pool); 1010 mempool_destroy(dwarf_frame_pool); 1011 kmem_cache_destroy(dwarf_reg_cachep); 1012 kmem_cache_destroy(dwarf_frame_cachep); 1013 } 1014 1015 /** 1016 * dwarf_parse_section - parse DWARF section 1017 * @eh_frame_start: start address of the .eh_frame section 1018 * @eh_frame_end: end address of the .eh_frame section 1019 * @mod: the kernel module containing the .eh_frame section 1020 * 1021 * Parse the information in a .eh_frame section. 1022 */ 1023 static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end, 1024 struct module *mod) 1025 { 1026 u32 entry_type; 1027 void *p, *entry; 1028 int count, err = 0; 1029 unsigned long len = 0; 1030 unsigned int c_entries, f_entries; 1031 unsigned char *end; 1032 1033 c_entries = 0; 1034 f_entries = 0; 1035 entry = eh_frame_start; 1036 1037 while ((char *)entry < eh_frame_end) { 1038 p = entry; 1039 1040 count = dwarf_entry_len(p, &len); 1041 if (count == 0) { 1042 /* 1043 * We read a bogus length field value. There is 1044 * nothing we can do here apart from disabling 1045 * the DWARF unwinder. We can't even skip this 1046 * entry and move to the next one because 'len' 1047 * tells us where our next entry is. 1048 */ 1049 err = -EINVAL; 1050 goto out; 1051 } else 1052 p += count; 1053 1054 /* initial length does not include itself */ 1055 end = p + len; 1056 1057 entry_type = get_unaligned((u32 *)p); 1058 p += 4; 1059 1060 if (entry_type == DW_EH_FRAME_CIE) { 1061 err = dwarf_parse_cie(entry, p, len, end, mod); 1062 if (err < 0) 1063 goto out; 1064 else 1065 c_entries++; 1066 } else { 1067 err = dwarf_parse_fde(entry, entry_type, p, len, 1068 end, mod); 1069 if (err < 0) 1070 goto out; 1071 else 1072 f_entries++; 1073 } 1074 1075 entry = (char *)entry + len + 4; 1076 } 1077 1078 printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n", 1079 c_entries, f_entries); 1080 1081 return 0; 1082 1083 out: 1084 return err; 1085 } 1086 1087 #ifdef CONFIG_MODULES 1088 int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, 1089 struct module *me) 1090 { 1091 unsigned int i, err; 1092 unsigned long start, end; 1093 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 1094 1095 start = end = 0; 1096 1097 for (i = 1; i < hdr->e_shnum; i++) { 1098 /* Alloc bit cleared means "ignore it." */ 1099 if ((sechdrs[i].sh_flags & SHF_ALLOC) 1100 && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) { 1101 start = sechdrs[i].sh_addr; 1102 end = start + sechdrs[i].sh_size; 1103 break; 1104 } 1105 } 1106 1107 /* Did we find the .eh_frame section? */ 1108 if (i != hdr->e_shnum) { 1109 INIT_LIST_HEAD(&me->arch.cie_list); 1110 INIT_LIST_HEAD(&me->arch.fde_list); 1111 err = dwarf_parse_section((char *)start, (char *)end, me); 1112 if (err) { 1113 printk(KERN_WARNING "%s: failed to parse DWARF info\n", 1114 me->name); 1115 return err; 1116 } 1117 } 1118 1119 return 0; 1120 } 1121 1122 /** 1123 * module_dwarf_cleanup - remove FDE/CIEs associated with @mod 1124 * @mod: the module that is being unloaded 1125 * 1126 * Remove any FDEs and CIEs from the global lists that came from 1127 * @mod's .eh_frame section because @mod is being unloaded. 1128 */ 1129 void module_dwarf_cleanup(struct module *mod) 1130 { 1131 struct dwarf_fde *fde, *ftmp; 1132 struct dwarf_cie *cie, *ctmp; 1133 unsigned long flags; 1134 1135 spin_lock_irqsave(&dwarf_cie_lock, flags); 1136 1137 list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) { 1138 list_del(&cie->link); 1139 rb_erase(&cie->node, &cie_root); 1140 kfree(cie); 1141 } 1142 1143 spin_unlock_irqrestore(&dwarf_cie_lock, flags); 1144 1145 spin_lock_irqsave(&dwarf_fde_lock, flags); 1146 1147 list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) { 1148 list_del(&fde->link); 1149 rb_erase(&fde->node, &fde_root); 1150 kfree(fde); 1151 } 1152 1153 spin_unlock_irqrestore(&dwarf_fde_lock, flags); 1154 } 1155 #endif /* CONFIG_MODULES */ 1156 1157 /** 1158 * dwarf_unwinder_init - initialise the dwarf unwinder 1159 * 1160 * Build the data structures describing the .dwarf_frame section to 1161 * make it easier to lookup CIE and FDE entries. Because the 1162 * .eh_frame section is packed as tightly as possible it is not 1163 * easy to lookup the FDE for a given PC, so we build a list of FDE 1164 * and CIE entries that make it easier. 1165 */ 1166 static int __init dwarf_unwinder_init(void) 1167 { 1168 int err = -ENOMEM; 1169 1170 dwarf_frame_cachep = kmem_cache_create("dwarf_frames", 1171 sizeof(struct dwarf_frame), 0, 1172 SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL); 1173 1174 dwarf_reg_cachep = kmem_cache_create("dwarf_regs", 1175 sizeof(struct dwarf_reg), 0, 1176 SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL); 1177 1178 dwarf_frame_pool = mempool_create_slab_pool(DWARF_FRAME_MIN_REQ, 1179 dwarf_frame_cachep); 1180 if (!dwarf_frame_pool) 1181 goto out; 1182 1183 dwarf_reg_pool = mempool_create_slab_pool(DWARF_REG_MIN_REQ, 1184 dwarf_reg_cachep); 1185 if (!dwarf_reg_pool) 1186 goto out; 1187 1188 err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL); 1189 if (err) 1190 goto out; 1191 1192 err = unwinder_register(&dwarf_unwinder); 1193 if (err) 1194 goto out; 1195 1196 dwarf_unwinder_ready = 1; 1197 1198 return 0; 1199 1200 out: 1201 printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err); 1202 dwarf_unwinder_cleanup(); 1203 return err; 1204 } 1205 early_initcall(dwarf_unwinder_init); 1206