2 * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * This is an implementation of a DWARF unwinder. Its main purpose is
9 * for generating stacktrace information. Based on the DWARF 3
10 * specification from http://www.dwarfstd.org.
13 * - DWARF64 doesn't work.
17 #include <linux/kernel.h>
19 #include <linux/list.h>
21 #include <asm/dwarf.h>
22 #include <asm/unwinder.h>
23 #include <asm/sections.h>
24 #include <asm/unaligned.h>
25 #include <asm/dwarf.h>
26 #include <asm/stacktrace.h>
28 static LIST_HEAD(dwarf_cie_list);
29 DEFINE_SPINLOCK(dwarf_cie_lock);
31 static LIST_HEAD(dwarf_fde_list);
32 DEFINE_SPINLOCK(dwarf_fde_lock);
34 static struct dwarf_cie *cached_cie;
37 * Figure out whether we need to allocate some dwarf registers. If dwarf
38 * registers have already been allocated then we may need to realloc
39 * them. "reg" is a register number that we need to be able to access
42 * Register numbers start at zero, therefore we need to allocate space
43 * for "reg" + 1 registers.
45 static void dwarf_frame_alloc_regs(struct dwarf_frame *frame,
48 struct dwarf_reg *regs;
49 unsigned int num_regs = reg + 1;
53 new_size = num_regs * sizeof(*regs);
54 old_size = frame->num_regs * sizeof(*regs);
56 /* Fast path: don't allocate any regs if we've already got enough. */
57 if (frame->num_regs >= num_regs)
60 regs = kzalloc(new_size, GFP_KERNEL);
62 printk(KERN_WARNING "Unable to allocate DWARF registers\n");
64 * Let's just bomb hard here, we have no way to
71 memcpy(regs, frame->regs, old_size);
76 frame->num_regs = num_regs;
80 * dwarf_read_addr - read dwarf data
81 * @src: source address of data
82 * @dst: destination address to store the data to
84 * Read 'n' bytes from @src, where 'n' is the size of an address on
85 * the native machine. We return the number of bytes read, which
86 * should always be 'n'. We also have to be careful when reading
87 * from @src and writing to @dst, because they can be arbitrarily
88 * aligned. Return 'n' - the number of bytes read.
90 static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
92 *dst = get_unaligned(src);
93 return sizeof(unsigned long *);
97 * dwarf_read_uleb128 - read unsigned LEB128 data
98 * @addr: the address where the ULEB128 data is stored
99 * @ret: address to store the result
101 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
102 * from Appendix C of the DWARF 3 spec. For information on the
103 * encodings refer to section "7.6 - Variable Length Data". Return
104 * the number of bytes read.
106 static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
117 byte = __raw_readb(addr);
121 result |= (byte & 0x7f) << shift;
134 * dwarf_read_leb128 - read signed LEB128 data
135 * @addr: the address of the LEB128 encoded data
136 * @ret: address to store the result
138 * Decode signed LEB128 data. The algorithm is taken from Appendix
139 * C of the DWARF 3 spec. Return the number of bytes read.
141 static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
153 byte = __raw_readb(addr);
155 result |= (byte & 0x7f) << shift;
163 /* The number of bits in a signed integer. */
164 num_bits = 8 * sizeof(result);
166 if ((shift < num_bits) && (byte & 0x40))
167 result |= (-1 << shift);
175 * dwarf_read_encoded_value - return the decoded value at @addr
176 * @addr: the address of the encoded value
177 * @val: where to write the decoded value
178 * @encoding: the encoding with which we can decode @addr
180 * GCC emits encoded address in the .eh_frame FDE entries. Decode
181 * the value at @addr using @encoding. The decoded value is written
182 * to @val and the number of bytes read is returned.
184 static int dwarf_read_encoded_value(char *addr, unsigned long *val,
187 unsigned long decoded_addr = 0;
190 switch (encoding & 0x70) {
191 case DW_EH_PE_absptr:
194 decoded_addr = (unsigned long)addr;
197 pr_debug("encoding=0x%x\n", (encoding & 0x70));
201 if ((encoding & 0x07) == 0x00)
202 encoding |= DW_EH_PE_udata4;
204 switch (encoding & 0x0f) {
205 case DW_EH_PE_sdata4:
206 case DW_EH_PE_udata4:
208 decoded_addr += get_unaligned((u32 *)addr);
209 __raw_writel(decoded_addr, val);
212 pr_debug("encoding=0x%x\n", encoding);
220 * dwarf_entry_len - return the length of an FDE or CIE
221 * @addr: the address of the entry
222 * @len: the length of the entry
224 * Read the initial_length field of the entry and store the size of
225 * the entry in @len. We return the number of bytes read. Return a
226 * count of 0 on error.
228 static inline int dwarf_entry_len(char *addr, unsigned long *len)
233 initial_len = get_unaligned((u32 *)addr);
237 * An initial length field value in the range DW_LEN_EXT_LO -
238 * DW_LEN_EXT_HI indicates an extension, and should not be
239 * interpreted as a length. The only extension that we currently
240 * understand is the use of DWARF64 addresses.
242 if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
244 * The 64-bit length field immediately follows the
245 * compulsory 32-bit length field.
247 if (initial_len == DW_EXT_DWARF64) {
248 *len = get_unaligned((u64 *)addr + 4);
251 printk(KERN_WARNING "Unknown DWARF extension\n");
261 * dwarf_lookup_cie - locate the cie
262 * @cie_ptr: pointer to help with lookup
264 static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
266 struct dwarf_cie *cie, *n;
269 spin_lock_irqsave(&dwarf_cie_lock, flags);
272 * We've cached the last CIE we looked up because chances are
273 * that the FDE wants this CIE.
275 if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
280 list_for_each_entry_safe(cie, n, &dwarf_cie_list, link) {
281 if (cie->cie_pointer == cie_ptr) {
287 /* Couldn't find the entry in the list. */
288 if (&cie->link == &dwarf_cie_list)
291 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
296 * dwarf_lookup_fde - locate the FDE that covers pc
297 * @pc: the program counter
299 struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
302 struct dwarf_fde *fde, *n;
304 spin_lock_irqsave(&dwarf_fde_lock, flags);
305 list_for_each_entry_safe(fde, n, &dwarf_fde_list, link) {
306 unsigned long start, end;
308 start = fde->initial_location;
309 end = fde->initial_location + fde->address_range;
311 if (pc >= start && pc < end)
315 /* Couldn't find the entry in the list. */
316 if (&fde->link == &dwarf_fde_list)
319 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
325 * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
326 * @insn_start: address of the first instruction
327 * @insn_end: address of the last instruction
328 * @cie: the CIE for this function
329 * @fde: the FDE for this function
330 * @frame: the instructions calculate the CFA for this frame
331 * @pc: the program counter of the address we're interested in
333 * Execute the Call Frame instruction sequence starting at
334 * @insn_start and ending at @insn_end. The instructions describe
335 * how to calculate the Canonical Frame Address of a stackframe.
336 * Store the results in @frame.
338 static int dwarf_cfa_execute_insns(unsigned char *insn_start,
339 unsigned char *insn_end,
340 struct dwarf_cie *cie,
341 struct dwarf_fde *fde,
342 struct dwarf_frame *frame,
346 unsigned char *current_insn;
347 unsigned int count, delta, reg, expr_len, offset;
349 current_insn = insn_start;
351 while (current_insn < insn_end && frame->pc <= pc) {
352 insn = __raw_readb(current_insn++);
355 * Firstly, handle the opcodes that embed their operands
356 * in the instructions.
358 switch (DW_CFA_opcode(insn)) {
359 case DW_CFA_advance_loc:
360 delta = DW_CFA_operand(insn);
361 delta *= cie->code_alignment_factor;
366 reg = DW_CFA_operand(insn);
367 count = dwarf_read_uleb128(current_insn, &offset);
368 current_insn += count;
369 offset *= cie->data_alignment_factor;
370 dwarf_frame_alloc_regs(frame, reg);
371 frame->regs[reg].addr = offset;
372 frame->regs[reg].flags |= DWARF_REG_OFFSET;
376 reg = DW_CFA_operand(insn);
382 * Secondly, handle the opcodes that don't embed their
383 * operands in the instruction.
388 case DW_CFA_advance_loc1:
389 delta = *current_insn++;
390 frame->pc += delta * cie->code_alignment_factor;
392 case DW_CFA_advance_loc2:
393 delta = get_unaligned((u16 *)current_insn);
395 frame->pc += delta * cie->code_alignment_factor;
397 case DW_CFA_advance_loc4:
398 delta = get_unaligned((u32 *)current_insn);
400 frame->pc += delta * cie->code_alignment_factor;
402 case DW_CFA_offset_extended:
403 count = dwarf_read_uleb128(current_insn, ®);
404 current_insn += count;
405 count = dwarf_read_uleb128(current_insn, &offset);
406 current_insn += count;
407 offset *= cie->data_alignment_factor;
409 case DW_CFA_restore_extended:
410 count = dwarf_read_uleb128(current_insn, ®);
411 current_insn += count;
413 case DW_CFA_undefined:
414 count = dwarf_read_uleb128(current_insn, ®);
415 current_insn += count;
418 count = dwarf_read_uleb128(current_insn,
419 &frame->cfa_register);
420 current_insn += count;
421 count = dwarf_read_uleb128(current_insn,
423 current_insn += count;
425 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
427 case DW_CFA_def_cfa_register:
428 count = dwarf_read_uleb128(current_insn,
429 &frame->cfa_register);
430 current_insn += count;
431 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
433 case DW_CFA_def_cfa_offset:
434 count = dwarf_read_uleb128(current_insn, &offset);
435 current_insn += count;
436 frame->cfa_offset = offset;
438 case DW_CFA_def_cfa_expression:
439 count = dwarf_read_uleb128(current_insn, &expr_len);
440 current_insn += count;
442 frame->cfa_expr = current_insn;
443 frame->cfa_expr_len = expr_len;
444 current_insn += expr_len;
446 frame->flags |= DWARF_FRAME_CFA_REG_EXP;
448 case DW_CFA_offset_extended_sf:
449 count = dwarf_read_uleb128(current_insn, ®);
450 current_insn += count;
451 count = dwarf_read_leb128(current_insn, &offset);
452 current_insn += count;
453 offset *= cie->data_alignment_factor;
454 dwarf_frame_alloc_regs(frame, reg);
455 frame->regs[reg].flags |= DWARF_REG_OFFSET;
456 frame->regs[reg].addr = offset;
458 case DW_CFA_val_offset:
459 count = dwarf_read_uleb128(current_insn, ®);
460 current_insn += count;
461 count = dwarf_read_leb128(current_insn, &offset);
462 offset *= cie->data_alignment_factor;
463 frame->regs[reg].flags |= DWARF_REG_OFFSET;
464 frame->regs[reg].addr = offset;
467 pr_debug("unhandled DWARF instruction 0x%x\n", insn);
476 * dwarf_unwind_stack - recursively unwind the stack
477 * @pc: address of the function to unwind
478 * @prev: struct dwarf_frame of the previous stackframe on the callstack
480 * Return a struct dwarf_frame representing the most recent frame
481 * on the callstack. Each of the lower (older) stack frames are
482 * linked via the "prev" member.
484 struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
485 struct dwarf_frame *prev)
487 struct dwarf_frame *frame;
488 struct dwarf_cie *cie;
489 struct dwarf_fde *fde;
494 * If this is the first invocation of this recursive function we
495 * need get the contents of a physical register to get the CFA
496 * in order to begin the virtual unwinding of the stack.
498 * The constant DWARF_ARCH_UNWIND_OFFSET is added to the address of
499 * this function because the return address register
500 * (DWARF_ARCH_RA_REG) will probably not be initialised until a
501 * few instructions into the prologue.
504 pc = (unsigned long)&dwarf_unwind_stack;
505 pc += DWARF_ARCH_UNWIND_OFFSET;
508 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
514 fde = dwarf_lookup_fde(pc);
517 * This is our normal exit path - the one that stops the
518 * recursion. There's two reasons why we might exit
521 * a) pc has no asscociated DWARF frame info and so
522 * we don't know how to unwind this frame. This is
523 * usually the case when we're trying to unwind a
524 * frame that was called from some assembly code
525 * that has no DWARF info, e.g. syscalls.
527 * b) the DEBUG info for pc is bogus. There's
528 * really no way to distinguish this case from the
529 * case above, which sucks because we could print a
535 cie = dwarf_lookup_cie(fde->cie_pointer);
537 frame->pc = fde->initial_location;
539 /* CIE initial instructions */
540 dwarf_cfa_execute_insns(cie->initial_instructions,
541 cie->instructions_end, cie, fde, frame, pc);
543 /* FDE instructions */
544 dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
547 /* Calculate the CFA */
548 switch (frame->flags) {
549 case DWARF_FRAME_CFA_REG_OFFSET:
551 BUG_ON(!prev->regs[frame->cfa_register].flags);
554 addr += prev->regs[frame->cfa_register].addr;
555 frame->cfa = __raw_readl(addr);
559 * Again, this is the first invocation of this
560 * recurisve function. We need to physically
561 * read the contents of a register in order to
562 * get the Canonical Frame Address for this
565 frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
568 frame->cfa += frame->cfa_offset;
574 /* If we haven't seen the return address reg, we're screwed. */
575 BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);
577 for (i = 0; i <= frame->num_regs; i++) {
578 struct dwarf_reg *reg = &frame->regs[i];
584 offset += frame->cfa;
587 addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;
588 frame->return_addr = __raw_readl(addr);
590 frame->next = dwarf_unwind_stack(frame->return_addr, frame);
594 static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
597 struct dwarf_cie *cie;
601 cie = kzalloc(sizeof(*cie), GFP_KERNEL);
608 * Record the offset into the .eh_frame section
609 * for this CIE. It allows this CIE to be
610 * quickly and easily looked up from the
613 cie->cie_pointer = (unsigned long)entry;
615 cie->version = *(char *)p++;
616 BUG_ON(cie->version != 1);
618 cie->augmentation = p;
619 p += strlen(cie->augmentation) + 1;
621 count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
624 count = dwarf_read_leb128(p, &cie->data_alignment_factor);
628 * Which column in the rule table contains the
631 if (cie->version == 1) {
632 cie->return_address_reg = __raw_readb(p);
635 count = dwarf_read_uleb128(p, &cie->return_address_reg);
639 if (cie->augmentation[0] == 'z') {
640 unsigned int length, count;
641 cie->flags |= DWARF_CIE_Z_AUGMENTATION;
643 count = dwarf_read_uleb128(p, &length);
646 BUG_ON((unsigned char *)p > end);
648 cie->initial_instructions = p + length;
652 while (*cie->augmentation) {
654 * "L" indicates a byte showing how the
655 * LSDA pointer is encoded. Skip it.
657 if (*cie->augmentation == 'L') {
660 } else if (*cie->augmentation == 'R') {
662 * "R" indicates a byte showing
663 * how FDE addresses are
666 cie->encoding = *(char *)p++;
668 } else if (*cie->augmentation == 'P') {
670 * "R" indicates a personality
675 } else if (*cie->augmentation == 'S') {
679 * Unknown augmentation. Assume
682 p = cie->initial_instructions;
688 cie->initial_instructions = p;
689 cie->instructions_end = end;
692 spin_lock_irqsave(&dwarf_cie_lock, flags);
693 list_add_tail(&cie->link, &dwarf_cie_list);
694 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
699 static int dwarf_parse_fde(void *entry, u32 entry_type,
700 void *start, unsigned long len)
702 struct dwarf_fde *fde;
703 struct dwarf_cie *cie;
708 fde = kzalloc(sizeof(*fde), GFP_KERNEL);
715 * In a .eh_frame section the CIE pointer is the
716 * delta between the address within the FDE
718 fde->cie_pointer = (unsigned long)(p - entry_type - 4);
720 cie = dwarf_lookup_cie(fde->cie_pointer);
724 count = dwarf_read_encoded_value(p, &fde->initial_location,
727 count = dwarf_read_addr(p, &fde->initial_location);
732 count = dwarf_read_encoded_value(p, &fde->address_range,
733 cie->encoding & 0x0f);
735 count = dwarf_read_addr(p, &fde->address_range);
739 if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
741 count = dwarf_read_uleb128(p, &length);
745 /* Call frame instructions. */
746 fde->instructions = p;
747 fde->end = start + len;
750 spin_lock_irqsave(&dwarf_fde_lock, flags);
751 list_add_tail(&fde->link, &dwarf_fde_list);
752 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
757 static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,
759 const struct stacktrace_ops *ops, void *data)
761 struct dwarf_frame *frame;
763 frame = dwarf_unwind_stack(0, NULL);
765 while (frame && frame->return_addr) {
766 ops->address(data, frame->return_addr, 1);
771 static struct unwinder dwarf_unwinder = {
772 .name = "dwarf-unwinder",
773 .dump = dwarf_unwinder_dump,
777 static void dwarf_unwinder_cleanup(void)
779 struct dwarf_cie *cie, *m;
780 struct dwarf_fde *fde, *n;
784 * Deallocate all the memory allocated for the DWARF unwinder.
785 * Traverse all the FDE/CIE lists and remove and free all the
786 * memory associated with those data structures.
788 spin_lock_irqsave(&dwarf_cie_lock, flags);
789 list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)
791 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
793 spin_lock_irqsave(&dwarf_fde_lock, flags);
794 list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)
796 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
800 * dwarf_unwinder_init - initialise the dwarf unwinder
802 * Build the data structures describing the .dwarf_frame section to
803 * make it easier to lookup CIE and FDE entries. Because the
804 * .eh_frame section is packed as tightly as possible it is not
805 * easy to lookup the FDE for a given PC, so we build a list of FDE
806 * and CIE entries that make it easier.
808 void dwarf_unwinder_init(void)
814 unsigned int c_entries, f_entries;
816 INIT_LIST_HEAD(&dwarf_cie_list);
817 INIT_LIST_HEAD(&dwarf_fde_list);
821 entry = &__start_eh_frame;
823 while ((char *)entry < __stop_eh_frame) {
826 count = dwarf_entry_len(p, &len);
829 * We read a bogus length field value. There is
830 * nothing we can do here apart from disabling
831 * the DWARF unwinder. We can't even skip this
832 * entry and move to the next one because 'len'
833 * tells us where our next entry is.
839 /* initial length does not include itself */
842 entry_type = get_unaligned((u32 *)p);
845 if (entry_type == DW_EH_FRAME_CIE) {
846 err = dwarf_parse_cie(entry, p, len, end);
852 err = dwarf_parse_fde(entry, entry_type, p, len);
859 entry = (char *)entry + len + 4;
862 printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
863 c_entries, f_entries);
865 err = unwinder_register(&dwarf_unwinder);
872 printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
873 dwarf_unwinder_cleanup();