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.
14 * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.
18 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/mempool.h>
23 #include <linux/elf.h>
24 #include <linux/ftrace.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <asm/dwarf.h>
28 #include <asm/unwinder.h>
29 #include <asm/sections.h>
30 #include <asm/unaligned.h>
31 #include <asm/stacktrace.h>
33 /* Reserve enough memory for two stack frames */
34 #define DWARF_FRAME_MIN_REQ 2
35 /* ... with 4 registers per frame. */
36 #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)
38 static struct kmem_cache
*dwarf_frame_cachep
;
39 static mempool_t
*dwarf_frame_pool
;
41 static struct kmem_cache
*dwarf_reg_cachep
;
42 static mempool_t
*dwarf_reg_pool
;
44 static struct rb_root cie_root
;
45 static DEFINE_SPINLOCK(dwarf_cie_lock
);
47 static struct rb_root fde_root
;
48 static DEFINE_SPINLOCK(dwarf_fde_lock
);
50 static struct dwarf_cie
*cached_cie
;
52 static unsigned int dwarf_unwinder_ready
;
55 * dwarf_frame_alloc_reg - allocate memory for a DWARF register
56 * @frame: the DWARF frame whose list of registers we insert on
57 * @reg_num: the register number
59 * Allocate space for, and initialise, a dwarf reg from
60 * dwarf_reg_pool and insert it onto the (unsorted) linked-list of
61 * dwarf registers for @frame.
63 * Return the initialised DWARF reg.
65 static struct dwarf_reg
*dwarf_frame_alloc_reg(struct dwarf_frame
*frame
,
68 struct dwarf_reg
*reg
;
70 reg
= mempool_alloc(dwarf_reg_pool
, GFP_ATOMIC
);
72 printk(KERN_WARNING
"Unable to allocate a DWARF register\n");
74 * Let's just bomb hard here, we have no way to
80 reg
->number
= reg_num
;
84 list_add(®
->link
, &frame
->reg_list
);
89 static void dwarf_frame_free_regs(struct dwarf_frame
*frame
)
91 struct dwarf_reg
*reg
, *n
;
93 list_for_each_entry_safe(reg
, n
, &frame
->reg_list
, link
) {
95 mempool_free(reg
, dwarf_reg_pool
);
100 * dwarf_frame_reg - return a DWARF register
101 * @frame: the DWARF frame to search in for @reg_num
102 * @reg_num: the register number to search for
104 * Lookup and return the dwarf reg @reg_num for this frame. Return
105 * NULL if @reg_num is an register invalid number.
107 static struct dwarf_reg
*dwarf_frame_reg(struct dwarf_frame
*frame
,
108 unsigned int reg_num
)
110 struct dwarf_reg
*reg
;
112 list_for_each_entry(reg
, &frame
->reg_list
, link
) {
113 if (reg
->number
== reg_num
)
121 * dwarf_read_addr - read dwarf data
122 * @src: source address of data
123 * @dst: destination address to store the data to
125 * Read 'n' bytes from @src, where 'n' is the size of an address on
126 * the native machine. We return the number of bytes read, which
127 * should always be 'n'. We also have to be careful when reading
128 * from @src and writing to @dst, because they can be arbitrarily
129 * aligned. Return 'n' - the number of bytes read.
131 static inline int dwarf_read_addr(unsigned long *src
, unsigned long *dst
)
133 u32 val
= get_unaligned(src
);
134 put_unaligned(val
, dst
);
135 return sizeof(unsigned long *);
139 * dwarf_read_uleb128 - read unsigned LEB128 data
140 * @addr: the address where the ULEB128 data is stored
141 * @ret: address to store the result
143 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
144 * from Appendix C of the DWARF 3 spec. For information on the
145 * encodings refer to section "7.6 - Variable Length Data". Return
146 * the number of bytes read.
148 static inline unsigned long dwarf_read_uleb128(char *addr
, unsigned int *ret
)
159 byte
= __raw_readb(addr
);
163 result
|= (byte
& 0x7f) << shift
;
176 * dwarf_read_leb128 - read signed LEB128 data
177 * @addr: the address of the LEB128 encoded data
178 * @ret: address to store the result
180 * Decode signed LEB128 data. The algorithm is taken from Appendix
181 * C of the DWARF 3 spec. Return the number of bytes read.
183 static inline unsigned long dwarf_read_leb128(char *addr
, int *ret
)
195 byte
= __raw_readb(addr
);
197 result
|= (byte
& 0x7f) << shift
;
205 /* The number of bits in a signed integer. */
206 num_bits
= 8 * sizeof(result
);
208 if ((shift
< num_bits
) && (byte
& 0x40))
209 result
|= (-1 << shift
);
217 * dwarf_read_encoded_value - return the decoded value at @addr
218 * @addr: the address of the encoded value
219 * @val: where to write the decoded value
220 * @encoding: the encoding with which we can decode @addr
222 * GCC emits encoded address in the .eh_frame FDE entries. Decode
223 * the value at @addr using @encoding. The decoded value is written
224 * to @val and the number of bytes read is returned.
226 static int dwarf_read_encoded_value(char *addr
, unsigned long *val
,
229 unsigned long decoded_addr
= 0;
232 switch (encoding
& 0x70) {
233 case DW_EH_PE_absptr
:
236 decoded_addr
= (unsigned long)addr
;
239 pr_debug("encoding=0x%x\n", (encoding
& 0x70));
243 if ((encoding
& 0x07) == 0x00)
244 encoding
|= DW_EH_PE_udata4
;
246 switch (encoding
& 0x0f) {
247 case DW_EH_PE_sdata4
:
248 case DW_EH_PE_udata4
:
250 decoded_addr
+= get_unaligned((u32
*)addr
);
251 __raw_writel(decoded_addr
, val
);
254 pr_debug("encoding=0x%x\n", encoding
);
262 * dwarf_entry_len - return the length of an FDE or CIE
263 * @addr: the address of the entry
264 * @len: the length of the entry
266 * Read the initial_length field of the entry and store the size of
267 * the entry in @len. We return the number of bytes read. Return a
268 * count of 0 on error.
270 static inline int dwarf_entry_len(char *addr
, unsigned long *len
)
275 initial_len
= get_unaligned((u32
*)addr
);
279 * An initial length field value in the range DW_LEN_EXT_LO -
280 * DW_LEN_EXT_HI indicates an extension, and should not be
281 * interpreted as a length. The only extension that we currently
282 * understand is the use of DWARF64 addresses.
284 if (initial_len
>= DW_EXT_LO
&& initial_len
<= DW_EXT_HI
) {
286 * The 64-bit length field immediately follows the
287 * compulsory 32-bit length field.
289 if (initial_len
== DW_EXT_DWARF64
) {
290 *len
= get_unaligned((u64
*)addr
+ 4);
293 printk(KERN_WARNING
"Unknown DWARF extension\n");
303 * dwarf_lookup_cie - locate the cie
304 * @cie_ptr: pointer to help with lookup
306 static struct dwarf_cie
*dwarf_lookup_cie(unsigned long cie_ptr
)
308 struct rb_node
**rb_node
= &cie_root
.rb_node
;
309 struct dwarf_cie
*cie
= NULL
;
312 spin_lock_irqsave(&dwarf_cie_lock
, flags
);
315 * We've cached the last CIE we looked up because chances are
316 * that the FDE wants this CIE.
318 if (cached_cie
&& cached_cie
->cie_pointer
== cie_ptr
) {
324 struct dwarf_cie
*cie_tmp
;
326 cie_tmp
= rb_entry(*rb_node
, struct dwarf_cie
, node
);
329 if (cie_ptr
== cie_tmp
->cie_pointer
) {
331 cached_cie
= cie_tmp
;
334 if (cie_ptr
< cie_tmp
->cie_pointer
)
335 rb_node
= &(*rb_node
)->rb_left
;
337 rb_node
= &(*rb_node
)->rb_right
;
342 spin_unlock_irqrestore(&dwarf_cie_lock
, flags
);
347 * dwarf_lookup_fde - locate the FDE that covers pc
348 * @pc: the program counter
350 struct dwarf_fde
*dwarf_lookup_fde(unsigned long pc
)
352 struct rb_node
**rb_node
= &fde_root
.rb_node
;
353 struct dwarf_fde
*fde
= NULL
;
356 spin_lock_irqsave(&dwarf_fde_lock
, flags
);
359 struct dwarf_fde
*fde_tmp
;
360 unsigned long tmp_start
, tmp_end
;
362 fde_tmp
= rb_entry(*rb_node
, struct dwarf_fde
, node
);
365 tmp_start
= fde_tmp
->initial_location
;
366 tmp_end
= fde_tmp
->initial_location
+ fde_tmp
->address_range
;
368 if (pc
< tmp_start
) {
369 rb_node
= &(*rb_node
)->rb_left
;
375 rb_node
= &(*rb_node
)->rb_right
;
380 spin_unlock_irqrestore(&dwarf_fde_lock
, flags
);
386 * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
387 * @insn_start: address of the first instruction
388 * @insn_end: address of the last instruction
389 * @cie: the CIE for this function
390 * @fde: the FDE for this function
391 * @frame: the instructions calculate the CFA for this frame
392 * @pc: the program counter of the address we're interested in
394 * Execute the Call Frame instruction sequence starting at
395 * @insn_start and ending at @insn_end. The instructions describe
396 * how to calculate the Canonical Frame Address of a stackframe.
397 * Store the results in @frame.
399 static int dwarf_cfa_execute_insns(unsigned char *insn_start
,
400 unsigned char *insn_end
,
401 struct dwarf_cie
*cie
,
402 struct dwarf_fde
*fde
,
403 struct dwarf_frame
*frame
,
407 unsigned char *current_insn
;
408 unsigned int count
, delta
, reg
, expr_len
, offset
;
409 struct dwarf_reg
*regp
;
411 current_insn
= insn_start
;
413 while (current_insn
< insn_end
&& frame
->pc
<= pc
) {
414 insn
= __raw_readb(current_insn
++);
417 * Firstly, handle the opcodes that embed their operands
418 * in the instructions.
420 switch (DW_CFA_opcode(insn
)) {
421 case DW_CFA_advance_loc
:
422 delta
= DW_CFA_operand(insn
);
423 delta
*= cie
->code_alignment_factor
;
428 reg
= DW_CFA_operand(insn
);
429 count
= dwarf_read_uleb128(current_insn
, &offset
);
430 current_insn
+= count
;
431 offset
*= cie
->data_alignment_factor
;
432 regp
= dwarf_frame_alloc_reg(frame
, reg
);
434 regp
->flags
|= DWARF_REG_OFFSET
;
438 reg
= DW_CFA_operand(insn
);
444 * Secondly, handle the opcodes that don't embed their
445 * operands in the instruction.
450 case DW_CFA_advance_loc1
:
451 delta
= *current_insn
++;
452 frame
->pc
+= delta
* cie
->code_alignment_factor
;
454 case DW_CFA_advance_loc2
:
455 delta
= get_unaligned((u16
*)current_insn
);
457 frame
->pc
+= delta
* cie
->code_alignment_factor
;
459 case DW_CFA_advance_loc4
:
460 delta
= get_unaligned((u32
*)current_insn
);
462 frame
->pc
+= delta
* cie
->code_alignment_factor
;
464 case DW_CFA_offset_extended
:
465 count
= dwarf_read_uleb128(current_insn
, ®
);
466 current_insn
+= count
;
467 count
= dwarf_read_uleb128(current_insn
, &offset
);
468 current_insn
+= count
;
469 offset
*= cie
->data_alignment_factor
;
471 case DW_CFA_restore_extended
:
472 count
= dwarf_read_uleb128(current_insn
, ®
);
473 current_insn
+= count
;
475 case DW_CFA_undefined
:
476 count
= dwarf_read_uleb128(current_insn
, ®
);
477 current_insn
+= count
;
478 regp
= dwarf_frame_alloc_reg(frame
, reg
);
479 regp
->flags
|= DWARF_UNDEFINED
;
482 count
= dwarf_read_uleb128(current_insn
,
483 &frame
->cfa_register
);
484 current_insn
+= count
;
485 count
= dwarf_read_uleb128(current_insn
,
487 current_insn
+= count
;
489 frame
->flags
|= DWARF_FRAME_CFA_REG_OFFSET
;
491 case DW_CFA_def_cfa_register
:
492 count
= dwarf_read_uleb128(current_insn
,
493 &frame
->cfa_register
);
494 current_insn
+= count
;
495 frame
->flags
|= DWARF_FRAME_CFA_REG_OFFSET
;
497 case DW_CFA_def_cfa_offset
:
498 count
= dwarf_read_uleb128(current_insn
, &offset
);
499 current_insn
+= count
;
500 frame
->cfa_offset
= offset
;
502 case DW_CFA_def_cfa_expression
:
503 count
= dwarf_read_uleb128(current_insn
, &expr_len
);
504 current_insn
+= count
;
506 frame
->cfa_expr
= current_insn
;
507 frame
->cfa_expr_len
= expr_len
;
508 current_insn
+= expr_len
;
510 frame
->flags
|= DWARF_FRAME_CFA_REG_EXP
;
512 case DW_CFA_offset_extended_sf
:
513 count
= dwarf_read_uleb128(current_insn
, ®
);
514 current_insn
+= count
;
515 count
= dwarf_read_leb128(current_insn
, &offset
);
516 current_insn
+= count
;
517 offset
*= cie
->data_alignment_factor
;
518 regp
= dwarf_frame_alloc_reg(frame
, reg
);
519 regp
->flags
|= DWARF_REG_OFFSET
;
522 case DW_CFA_val_offset
:
523 count
= dwarf_read_uleb128(current_insn
, ®
);
524 current_insn
+= count
;
525 count
= dwarf_read_leb128(current_insn
, &offset
);
526 offset
*= cie
->data_alignment_factor
;
527 regp
= dwarf_frame_alloc_reg(frame
, reg
);
528 regp
->flags
|= DWARF_VAL_OFFSET
;
531 case DW_CFA_GNU_args_size
:
532 count
= dwarf_read_uleb128(current_insn
, &offset
);
533 current_insn
+= count
;
535 case DW_CFA_GNU_negative_offset_extended
:
536 count
= dwarf_read_uleb128(current_insn
, ®
);
537 current_insn
+= count
;
538 count
= dwarf_read_uleb128(current_insn
, &offset
);
539 offset
*= cie
->data_alignment_factor
;
541 regp
= dwarf_frame_alloc_reg(frame
, reg
);
542 regp
->flags
|= DWARF_REG_OFFSET
;
543 regp
->addr
= -offset
;
546 pr_debug("unhandled DWARF instruction 0x%x\n", insn
);
556 * dwarf_free_frame - free the memory allocated for @frame
557 * @frame: the frame to free
559 void dwarf_free_frame(struct dwarf_frame
*frame
)
561 dwarf_frame_free_regs(frame
);
562 mempool_free(frame
, dwarf_frame_pool
);
565 extern void ret_from_irq(void);
568 * dwarf_unwind_stack - unwind the stack
570 * @pc: address of the function to unwind
571 * @prev: struct dwarf_frame of the previous stackframe on the callstack
573 * Return a struct dwarf_frame representing the most recent frame
574 * on the callstack. Each of the lower (older) stack frames are
575 * linked via the "prev" member.
577 struct dwarf_frame
*dwarf_unwind_stack(unsigned long pc
,
578 struct dwarf_frame
*prev
)
580 struct dwarf_frame
*frame
;
581 struct dwarf_cie
*cie
;
582 struct dwarf_fde
*fde
;
583 struct dwarf_reg
*reg
;
587 * If we've been called in to before initialization has
588 * completed, bail out immediately.
590 if (!dwarf_unwinder_ready
)
594 * If we're starting at the top of the stack we need get the
595 * contents of a physical register to get the CFA in order to
596 * begin the virtual unwinding of the stack.
598 * NOTE: the return address is guaranteed to be setup by the
599 * time this function makes its first function call.
602 pc
= (unsigned long)current_text_addr();
604 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
606 * If our stack has been patched by the function graph tracer
607 * then we might see the address of return_to_handler() where we
608 * expected to find the real return address.
610 if (pc
== (unsigned long)&return_to_handler
) {
611 int index
= current
->curr_ret_stack
;
614 * We currently have no way of tracking how many
615 * return_to_handler()'s we've seen. If there is more
616 * than one patched return address on our stack,
621 pc
= current
->ret_stack
[index
].ret
;
625 frame
= mempool_alloc(dwarf_frame_pool
, GFP_ATOMIC
);
627 printk(KERN_ERR
"Unable to allocate a dwarf frame\n");
631 INIT_LIST_HEAD(&frame
->reg_list
);
634 frame
->return_addr
= 0;
636 fde
= dwarf_lookup_fde(pc
);
639 * This is our normal exit path. There are two reasons
640 * why we might exit here,
642 * a) pc has no asscociated DWARF frame info and so
643 * we don't know how to unwind this frame. This is
644 * usually the case when we're trying to unwind a
645 * frame that was called from some assembly code
646 * that has no DWARF info, e.g. syscalls.
648 * b) the DEBUG info for pc is bogus. There's
649 * really no way to distinguish this case from the
650 * case above, which sucks because we could print a
656 cie
= dwarf_lookup_cie(fde
->cie_pointer
);
658 frame
->pc
= fde
->initial_location
;
660 /* CIE initial instructions */
661 dwarf_cfa_execute_insns(cie
->initial_instructions
,
662 cie
->instructions_end
, cie
, fde
,
665 /* FDE instructions */
666 dwarf_cfa_execute_insns(fde
->instructions
, fde
->end
, cie
,
669 /* Calculate the CFA */
670 switch (frame
->flags
) {
671 case DWARF_FRAME_CFA_REG_OFFSET
:
673 reg
= dwarf_frame_reg(prev
, frame
->cfa_register
);
674 UNWINDER_BUG_ON(!reg
);
675 UNWINDER_BUG_ON(reg
->flags
!= DWARF_REG_OFFSET
);
677 addr
= prev
->cfa
+ reg
->addr
;
678 frame
->cfa
= __raw_readl(addr
);
682 * Again, we're starting from the top of the
683 * stack. We need to physically read
684 * the contents of a register in order to get
685 * the Canonical Frame Address for this
688 frame
->cfa
= dwarf_read_arch_reg(frame
->cfa_register
);
691 frame
->cfa
+= frame
->cfa_offset
;
697 reg
= dwarf_frame_reg(frame
, DWARF_ARCH_RA_REG
);
700 * If we haven't seen the return address register or the return
701 * address column is undefined then we must assume that this is
702 * the end of the callstack.
704 if (!reg
|| reg
->flags
== DWARF_UNDEFINED
)
707 UNWINDER_BUG_ON(reg
->flags
!= DWARF_REG_OFFSET
);
709 addr
= frame
->cfa
+ reg
->addr
;
710 frame
->return_addr
= __raw_readl(addr
);
713 * Ah, the joys of unwinding through interrupts.
715 * Interrupts are tricky - the DWARF info needs to be _really_
716 * accurate and unfortunately I'm seeing a lot of bogus DWARF
717 * info. For example, I've seen interrupts occur in epilogues
718 * just after the frame pointer (r14) had been restored. The
719 * problem was that the DWARF info claimed that the CFA could be
720 * reached by using the value of the frame pointer before it was
723 * So until the compiler can be trusted to produce reliable
724 * DWARF info when it really matters, let's stop unwinding once
725 * we've calculated the function that was interrupted.
727 if (prev
&& prev
->pc
== (unsigned long)ret_from_irq
)
728 frame
->return_addr
= 0;
733 dwarf_free_frame(frame
);
737 static int dwarf_parse_cie(void *entry
, void *p
, unsigned long len
,
738 unsigned char *end
, struct module
*mod
)
740 struct rb_node
**rb_node
= &cie_root
.rb_node
;
741 struct rb_node
*parent
= *rb_node
;
742 struct dwarf_cie
*cie
;
746 cie
= kzalloc(sizeof(*cie
), GFP_KERNEL
);
753 * Record the offset into the .eh_frame section
754 * for this CIE. It allows this CIE to be
755 * quickly and easily looked up from the
758 cie
->cie_pointer
= (unsigned long)entry
;
760 cie
->version
= *(char *)p
++;
761 UNWINDER_BUG_ON(cie
->version
!= 1);
763 cie
->augmentation
= p
;
764 p
+= strlen(cie
->augmentation
) + 1;
766 count
= dwarf_read_uleb128(p
, &cie
->code_alignment_factor
);
769 count
= dwarf_read_leb128(p
, &cie
->data_alignment_factor
);
773 * Which column in the rule table contains the
776 if (cie
->version
== 1) {
777 cie
->return_address_reg
= __raw_readb(p
);
780 count
= dwarf_read_uleb128(p
, &cie
->return_address_reg
);
784 if (cie
->augmentation
[0] == 'z') {
785 unsigned int length
, count
;
786 cie
->flags
|= DWARF_CIE_Z_AUGMENTATION
;
788 count
= dwarf_read_uleb128(p
, &length
);
791 UNWINDER_BUG_ON((unsigned char *)p
> end
);
793 cie
->initial_instructions
= p
+ length
;
797 while (*cie
->augmentation
) {
799 * "L" indicates a byte showing how the
800 * LSDA pointer is encoded. Skip it.
802 if (*cie
->augmentation
== 'L') {
805 } else if (*cie
->augmentation
== 'R') {
807 * "R" indicates a byte showing
808 * how FDE addresses are
811 cie
->encoding
= *(char *)p
++;
813 } else if (*cie
->augmentation
== 'P') {
815 * "R" indicates a personality
820 } else if (*cie
->augmentation
== 'S') {
824 * Unknown augmentation. Assume
827 p
= cie
->initial_instructions
;
833 cie
->initial_instructions
= p
;
834 cie
->instructions_end
= end
;
837 spin_lock_irqsave(&dwarf_cie_lock
, flags
);
840 struct dwarf_cie
*cie_tmp
;
842 cie_tmp
= rb_entry(*rb_node
, struct dwarf_cie
, node
);
846 if (cie
->cie_pointer
< cie_tmp
->cie_pointer
)
847 rb_node
= &parent
->rb_left
;
848 else if (cie
->cie_pointer
>= cie_tmp
->cie_pointer
)
849 rb_node
= &parent
->rb_right
;
854 rb_link_node(&cie
->node
, parent
, rb_node
);
855 rb_insert_color(&cie
->node
, &cie_root
);
857 #ifdef CONFIG_MODULES
859 list_add_tail(&cie
->link
, &mod
->arch
.cie_list
);
862 spin_unlock_irqrestore(&dwarf_cie_lock
, flags
);
867 static int dwarf_parse_fde(void *entry
, u32 entry_type
,
868 void *start
, unsigned long len
,
869 unsigned char *end
, struct module
*mod
)
871 struct rb_node
**rb_node
= &fde_root
.rb_node
;
872 struct rb_node
*parent
= *rb_node
;
873 struct dwarf_fde
*fde
;
874 struct dwarf_cie
*cie
;
879 fde
= kzalloc(sizeof(*fde
), GFP_KERNEL
);
886 * In a .eh_frame section the CIE pointer is the
887 * delta between the address within the FDE
889 fde
->cie_pointer
= (unsigned long)(p
- entry_type
- 4);
891 cie
= dwarf_lookup_cie(fde
->cie_pointer
);
895 count
= dwarf_read_encoded_value(p
, &fde
->initial_location
,
898 count
= dwarf_read_addr(p
, &fde
->initial_location
);
903 count
= dwarf_read_encoded_value(p
, &fde
->address_range
,
904 cie
->encoding
& 0x0f);
906 count
= dwarf_read_addr(p
, &fde
->address_range
);
910 if (fde
->cie
->flags
& DWARF_CIE_Z_AUGMENTATION
) {
912 count
= dwarf_read_uleb128(p
, &length
);
916 /* Call frame instructions. */
917 fde
->instructions
= p
;
921 spin_lock_irqsave(&dwarf_fde_lock
, flags
);
924 struct dwarf_fde
*fde_tmp
;
925 unsigned long tmp_start
, tmp_end
;
926 unsigned long start
, end
;
928 fde_tmp
= rb_entry(*rb_node
, struct dwarf_fde
, node
);
930 start
= fde
->initial_location
;
931 end
= fde
->initial_location
+ fde
->address_range
;
933 tmp_start
= fde_tmp
->initial_location
;
934 tmp_end
= fde_tmp
->initial_location
+ fde_tmp
->address_range
;
938 if (start
< tmp_start
)
939 rb_node
= &parent
->rb_left
;
940 else if (start
>= tmp_end
)
941 rb_node
= &parent
->rb_right
;
946 rb_link_node(&fde
->node
, parent
, rb_node
);
947 rb_insert_color(&fde
->node
, &fde_root
);
949 #ifdef CONFIG_MODULES
951 list_add_tail(&fde
->link
, &mod
->arch
.fde_list
);
954 spin_unlock_irqrestore(&dwarf_fde_lock
, flags
);
959 static void dwarf_unwinder_dump(struct task_struct
*task
,
960 struct pt_regs
*regs
,
962 const struct stacktrace_ops
*ops
,
965 struct dwarf_frame
*frame
, *_frame
;
966 unsigned long return_addr
;
972 frame
= dwarf_unwind_stack(return_addr
, _frame
);
975 dwarf_free_frame(_frame
);
979 if (!frame
|| !frame
->return_addr
)
982 return_addr
= frame
->return_addr
;
983 ops
->address(data
, return_addr
, 1);
987 dwarf_free_frame(frame
);
990 static struct unwinder dwarf_unwinder
= {
991 .name
= "dwarf-unwinder",
992 .dump
= dwarf_unwinder_dump
,
996 static void __init
dwarf_unwinder_cleanup(void)
998 struct dwarf_fde
*fde
, *next_fde
;
999 struct dwarf_cie
*cie
, *next_cie
;
1002 * Deallocate all the memory allocated for the DWARF unwinder.
1003 * Traverse all the FDE/CIE lists and remove and free all the
1004 * memory associated with those data structures.
1006 rbtree_postorder_for_each_entry_safe(fde
, next_fde
, &fde_root
, node
)
1009 rbtree_postorder_for_each_entry_safe(cie
, next_cie
, &cie_root
, node
)
1012 mempool_destroy(dwarf_reg_pool
);
1013 mempool_destroy(dwarf_frame_pool
);
1014 kmem_cache_destroy(dwarf_reg_cachep
);
1015 kmem_cache_destroy(dwarf_frame_cachep
);
1019 * dwarf_parse_section - parse DWARF section
1020 * @eh_frame_start: start address of the .eh_frame section
1021 * @eh_frame_end: end address of the .eh_frame section
1022 * @mod: the kernel module containing the .eh_frame section
1024 * Parse the information in a .eh_frame section.
1026 static int dwarf_parse_section(char *eh_frame_start
, char *eh_frame_end
,
1032 unsigned long len
= 0;
1033 unsigned int c_entries
, f_entries
;
1038 entry
= eh_frame_start
;
1040 while ((char *)entry
< eh_frame_end
) {
1043 count
= dwarf_entry_len(p
, &len
);
1046 * We read a bogus length field value. There is
1047 * nothing we can do here apart from disabling
1048 * the DWARF unwinder. We can't even skip this
1049 * entry and move to the next one because 'len'
1050 * tells us where our next entry is.
1057 /* initial length does not include itself */
1060 entry_type
= get_unaligned((u32
*)p
);
1063 if (entry_type
== DW_EH_FRAME_CIE
) {
1064 err
= dwarf_parse_cie(entry
, p
, len
, end
, mod
);
1070 err
= dwarf_parse_fde(entry
, entry_type
, p
, len
,
1078 entry
= (char *)entry
+ len
+ 4;
1081 printk(KERN_INFO
"DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
1082 c_entries
, f_entries
);
1090 #ifdef CONFIG_MODULES
1091 int module_dwarf_finalize(const Elf_Ehdr
*hdr
, const Elf_Shdr
*sechdrs
,
1094 unsigned int i
, err
;
1095 unsigned long start
, end
;
1096 char *secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1100 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1101 /* Alloc bit cleared means "ignore it." */
1102 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1103 && !strcmp(secstrings
+sechdrs
[i
].sh_name
, ".eh_frame")) {
1104 start
= sechdrs
[i
].sh_addr
;
1105 end
= start
+ sechdrs
[i
].sh_size
;
1110 /* Did we find the .eh_frame section? */
1111 if (i
!= hdr
->e_shnum
) {
1112 INIT_LIST_HEAD(&me
->arch
.cie_list
);
1113 INIT_LIST_HEAD(&me
->arch
.fde_list
);
1114 err
= dwarf_parse_section((char *)start
, (char *)end
, me
);
1116 printk(KERN_WARNING
"%s: failed to parse DWARF info\n",
1126 * module_dwarf_cleanup - remove FDE/CIEs associated with @mod
1127 * @mod: the module that is being unloaded
1129 * Remove any FDEs and CIEs from the global lists that came from
1130 * @mod's .eh_frame section because @mod is being unloaded.
1132 void module_dwarf_cleanup(struct module
*mod
)
1134 struct dwarf_fde
*fde
, *ftmp
;
1135 struct dwarf_cie
*cie
, *ctmp
;
1136 unsigned long flags
;
1138 spin_lock_irqsave(&dwarf_cie_lock
, flags
);
1140 list_for_each_entry_safe(cie
, ctmp
, &mod
->arch
.cie_list
, link
) {
1141 list_del(&cie
->link
);
1142 rb_erase(&cie
->node
, &cie_root
);
1146 spin_unlock_irqrestore(&dwarf_cie_lock
, flags
);
1148 spin_lock_irqsave(&dwarf_fde_lock
, flags
);
1150 list_for_each_entry_safe(fde
, ftmp
, &mod
->arch
.fde_list
, link
) {
1151 list_del(&fde
->link
);
1152 rb_erase(&fde
->node
, &fde_root
);
1156 spin_unlock_irqrestore(&dwarf_fde_lock
, flags
);
1158 #endif /* CONFIG_MODULES */
1161 * dwarf_unwinder_init - initialise the dwarf unwinder
1163 * Build the data structures describing the .dwarf_frame section to
1164 * make it easier to lookup CIE and FDE entries. Because the
1165 * .eh_frame section is packed as tightly as possible it is not
1166 * easy to lookup the FDE for a given PC, so we build a list of FDE
1167 * and CIE entries that make it easier.
1169 static int __init
dwarf_unwinder_init(void)
1173 dwarf_frame_cachep
= kmem_cache_create("dwarf_frames",
1174 sizeof(struct dwarf_frame
), 0,
1175 SLAB_PANIC
| SLAB_HWCACHE_ALIGN
, NULL
);
1177 dwarf_reg_cachep
= kmem_cache_create("dwarf_regs",
1178 sizeof(struct dwarf_reg
), 0,
1179 SLAB_PANIC
| SLAB_HWCACHE_ALIGN
, NULL
);
1181 dwarf_frame_pool
= mempool_create_slab_pool(DWARF_FRAME_MIN_REQ
,
1182 dwarf_frame_cachep
);
1183 if (!dwarf_frame_pool
)
1186 dwarf_reg_pool
= mempool_create_slab_pool(DWARF_REG_MIN_REQ
,
1188 if (!dwarf_reg_pool
)
1191 err
= dwarf_parse_section(__start_eh_frame
, __stop_eh_frame
, NULL
);
1195 err
= unwinder_register(&dwarf_unwinder
);
1199 dwarf_unwinder_ready
= 1;
1204 printk(KERN_ERR
"Failed to initialise DWARF unwinder: %d\n", err
);
1205 dwarf_unwinder_cleanup();
1208 early_initcall(dwarf_unwinder_init
);