sh: unwinder: Fix up usage of unaligned accessors.
[cascardo/linux.git] / arch / sh / kernel / dwarf.c
1 /*
2  * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
3  *
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
6  * for more details.
7  *
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.
11  *
12  * TODO:
13  *      - DWARF64 doesn't work.
14  */
15
16 /* #define DEBUG */
17 #include <linux/kernel.h>
18 #include <linux/io.h>
19 #include <linux/list.h>
20 #include <linux/mm.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>
27
28 static LIST_HEAD(dwarf_cie_list);
29 DEFINE_SPINLOCK(dwarf_cie_lock);
30
31 static LIST_HEAD(dwarf_fde_list);
32 DEFINE_SPINLOCK(dwarf_fde_lock);
33
34 static struct dwarf_cie *cached_cie;
35
36 /*
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
40  * after this call.
41  *
42  * Register numbers start at zero, therefore we need to allocate space
43  * for "reg" + 1 registers.
44  */
45 static void dwarf_frame_alloc_regs(struct dwarf_frame *frame,
46                                    unsigned int reg)
47 {
48         struct dwarf_reg *regs;
49         unsigned int num_regs = reg + 1;
50         size_t new_size;
51         size_t old_size;
52
53         new_size = num_regs * sizeof(*regs);
54         old_size = frame->num_regs * sizeof(*regs);
55
56         /* Fast path: don't allocate any regs if we've already got enough. */
57         if (frame->num_regs >= num_regs)
58                 return;
59
60         regs = kzalloc(new_size, GFP_KERNEL);
61         if (!regs) {
62                 printk(KERN_WARNING "Unable to allocate DWARF registers\n");
63                 /*
64                  * Let's just bomb hard here, we have no way to
65                  * gracefully recover.
66                  */
67                 BUG();
68         }
69
70         if (frame->regs) {
71                 memcpy(regs, frame->regs, old_size);
72                 kfree(frame->regs);
73         }
74
75         frame->regs = regs;
76         frame->num_regs = num_regs;
77 }
78
79 /**
80  *      dwarf_read_addr - read dwarf data
81  *      @src: source address of data
82  *      @dst: destination address to store the data to
83  *
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.
89  */
90 static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
91 {
92         *dst = get_unaligned(src);
93         return sizeof(unsigned long *);
94 }
95
96 /**
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
100  *
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.
105  */
106 static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
107 {
108         unsigned int result;
109         unsigned char byte;
110         int shift, count;
111
112         result = 0;
113         shift = 0;
114         count = 0;
115
116         while (1) {
117                 byte = __raw_readb(addr);
118                 addr++;
119                 count++;
120
121                 result |= (byte & 0x7f) << shift;
122                 shift += 7;
123
124                 if (!(byte & 0x80))
125                         break;
126         }
127
128         *ret = result;
129
130         return count;
131 }
132
133 /**
134  *      dwarf_read_leb128 - read signed LEB128 data
135  *      @addr: the address of the LEB128 encoded data
136  *      @ret: address to store the result
137  *
138  *      Decode signed LEB128 data. The algorithm is taken from Appendix
139  *      C of the DWARF 3 spec. Return the number of bytes read.
140  */
141 static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
142 {
143         unsigned char byte;
144         int result, shift;
145         int num_bits;
146         int count;
147
148         result = 0;
149         shift = 0;
150         count = 0;
151
152         while (1) {
153                 byte = __raw_readb(addr);
154                 addr++;
155                 result |= (byte & 0x7f) << shift;
156                 shift += 7;
157                 count++;
158
159                 if (!(byte & 0x80))
160                         break;
161         }
162
163         /* The number of bits in a signed integer. */
164         num_bits = 8 * sizeof(result);
165
166         if ((shift < num_bits) && (byte & 0x40))
167                 result |= (-1 << shift);
168
169         *ret = result;
170
171         return count;
172 }
173
174 /**
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
179  *
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.
183  */
184 static int dwarf_read_encoded_value(char *addr, unsigned long *val,
185                                     char encoding)
186 {
187         unsigned long decoded_addr = 0;
188         int count = 0;
189
190         switch (encoding & 0x70) {
191         case DW_EH_PE_absptr:
192                 break;
193         case DW_EH_PE_pcrel:
194                 decoded_addr = (unsigned long)addr;
195                 break;
196         default:
197                 pr_debug("encoding=0x%x\n", (encoding & 0x70));
198                 BUG();
199         }
200
201         if ((encoding & 0x07) == 0x00)
202                 encoding |= DW_EH_PE_udata4;
203
204         switch (encoding & 0x0f) {
205         case DW_EH_PE_sdata4:
206         case DW_EH_PE_udata4:
207                 count += 4;
208                 decoded_addr += get_unaligned((u32 *)addr);
209                 __raw_writel(decoded_addr, val);
210                 break;
211         default:
212                 pr_debug("encoding=0x%x\n", encoding);
213                 BUG();
214         }
215
216         return count;
217 }
218
219 /**
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
223  *
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.
227  */
228 static inline int dwarf_entry_len(char *addr, unsigned long *len)
229 {
230         u32 initial_len;
231         int count;
232
233         initial_len = get_unaligned((u32 *)addr);
234         count = 4;
235
236         /*
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.
241          */
242         if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
243                 /*
244                  * The 64-bit length field immediately follows the
245                  * compulsory 32-bit length field.
246                  */
247                 if (initial_len == DW_EXT_DWARF64) {
248                         *len = get_unaligned((u64 *)addr + 4);
249                         count = 12;
250                 } else {
251                         printk(KERN_WARNING "Unknown DWARF extension\n");
252                         count = 0;
253                 }
254         } else
255                 *len = initial_len;
256
257         return count;
258 }
259
260 /**
261  *      dwarf_lookup_cie - locate the cie
262  *      @cie_ptr: pointer to help with lookup
263  */
264 static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
265 {
266         struct dwarf_cie *cie, *n;
267         unsigned long flags;
268
269         spin_lock_irqsave(&dwarf_cie_lock, flags);
270
271         /*
272          * We've cached the last CIE we looked up because chances are
273          * that the FDE wants this CIE.
274          */
275         if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
276                 cie = cached_cie;
277                 goto out;
278         }
279
280         list_for_each_entry_safe(cie, n, &dwarf_cie_list, link) {
281                 if (cie->cie_pointer == cie_ptr) {
282                         cached_cie = cie;
283                         break;
284                 }
285         }
286
287         /* Couldn't find the entry in the list. */
288         if (&cie->link == &dwarf_cie_list)
289                 cie = NULL;
290 out:
291         spin_unlock_irqrestore(&dwarf_cie_lock, flags);
292         return cie;
293 }
294
295 /**
296  *      dwarf_lookup_fde - locate the FDE that covers pc
297  *      @pc: the program counter
298  */
299 struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
300 {
301         unsigned long flags;
302         struct dwarf_fde *fde, *n;
303
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;
307
308                 start = fde->initial_location;
309                 end = fde->initial_location + fde->address_range;
310
311                 if (pc >= start && pc < end)
312                         break;
313         }
314
315         /* Couldn't find the entry in the list. */
316         if (&fde->link == &dwarf_fde_list)
317                 fde = NULL;
318
319         spin_unlock_irqrestore(&dwarf_fde_lock, flags);
320
321         return fde;
322 }
323
324 /**
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
332  *
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.
337  */
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,
343                                    unsigned long pc)
344 {
345         unsigned char insn;
346         unsigned char *current_insn;
347         unsigned int count, delta, reg, expr_len, offset;
348
349         current_insn = insn_start;
350
351         while (current_insn < insn_end && frame->pc <= pc) {
352                 insn = __raw_readb(current_insn++);
353
354                 /*
355                  * Firstly, handle the opcodes that embed their operands
356                  * in the instructions.
357                  */
358                 switch (DW_CFA_opcode(insn)) {
359                 case DW_CFA_advance_loc:
360                         delta = DW_CFA_operand(insn);
361                         delta *= cie->code_alignment_factor;
362                         frame->pc += delta;
363                         continue;
364                         /* NOTREACHED */
365                 case DW_CFA_offset:
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;
373                         continue;
374                         /* NOTREACHED */
375                 case DW_CFA_restore:
376                         reg = DW_CFA_operand(insn);
377                         continue;
378                         /* NOTREACHED */
379                 }
380
381                 /*
382                  * Secondly, handle the opcodes that don't embed their
383                  * operands in the instruction.
384                  */
385                 switch (insn) {
386                 case DW_CFA_nop:
387                         continue;
388                 case DW_CFA_advance_loc1:
389                         delta = *current_insn++;
390                         frame->pc += delta * cie->code_alignment_factor;
391                         break;
392                 case DW_CFA_advance_loc2:
393                         delta = get_unaligned((u16 *)current_insn);
394                         current_insn += 2;
395                         frame->pc += delta * cie->code_alignment_factor;
396                         break;
397                 case DW_CFA_advance_loc4:
398                         delta = get_unaligned((u32 *)current_insn);
399                         current_insn += 4;
400                         frame->pc += delta * cie->code_alignment_factor;
401                         break;
402                 case DW_CFA_offset_extended:
403                         count = dwarf_read_uleb128(current_insn, &reg);
404                         current_insn += count;
405                         count = dwarf_read_uleb128(current_insn, &offset);
406                         current_insn += count;
407                         offset *= cie->data_alignment_factor;
408                         break;
409                 case DW_CFA_restore_extended:
410                         count = dwarf_read_uleb128(current_insn, &reg);
411                         current_insn += count;
412                         break;
413                 case DW_CFA_undefined:
414                         count = dwarf_read_uleb128(current_insn, &reg);
415                         current_insn += count;
416                         break;
417                 case DW_CFA_def_cfa:
418                         count = dwarf_read_uleb128(current_insn,
419                                                    &frame->cfa_register);
420                         current_insn += count;
421                         count = dwarf_read_uleb128(current_insn,
422                                                    &frame->cfa_offset);
423                         current_insn += count;
424
425                         frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
426                         break;
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;
432                         break;
433                 case DW_CFA_def_cfa_offset:
434                         count = dwarf_read_uleb128(current_insn, &offset);
435                         current_insn += count;
436                         frame->cfa_offset = offset;
437                         break;
438                 case DW_CFA_def_cfa_expression:
439                         count = dwarf_read_uleb128(current_insn, &expr_len);
440                         current_insn += count;
441
442                         frame->cfa_expr = current_insn;
443                         frame->cfa_expr_len = expr_len;
444                         current_insn += expr_len;
445
446                         frame->flags |= DWARF_FRAME_CFA_REG_EXP;
447                         break;
448                 case DW_CFA_offset_extended_sf:
449                         count = dwarf_read_uleb128(current_insn, &reg);
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;
457                         break;
458                 case DW_CFA_val_offset:
459                         count = dwarf_read_uleb128(current_insn, &reg);
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;
465                         break;
466                 default:
467                         pr_debug("unhandled DWARF instruction 0x%x\n", insn);
468                         break;
469                 }
470         }
471
472         return 0;
473 }
474
475 /**
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
479  *
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.
483  */
484 struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
485                                        struct dwarf_frame *prev)
486 {
487         struct dwarf_frame *frame;
488         struct dwarf_cie *cie;
489         struct dwarf_fde *fde;
490         unsigned long addr;
491         int i, offset;
492
493         /*
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.
497          *
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.
502          */
503         if (!pc && !prev) {
504                 pc = (unsigned long)&dwarf_unwind_stack;
505                 pc += DWARF_ARCH_UNWIND_OFFSET;
506         }
507
508         frame = kzalloc(sizeof(*frame), GFP_KERNEL);
509         if (!frame)
510                 return NULL;
511
512         frame->prev = prev;
513
514         fde = dwarf_lookup_fde(pc);
515         if (!fde) {
516                 /*
517                  * This is our normal exit path - the one that stops the
518                  * recursion. There's two reasons why we might exit
519                  * here,
520                  *
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.
526                  *
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
530                  *      warning here.
531                  */
532                 return NULL;
533         }
534
535         cie = dwarf_lookup_cie(fde->cie_pointer);
536
537         frame->pc = fde->initial_location;
538
539         /* CIE initial instructions */
540         dwarf_cfa_execute_insns(cie->initial_instructions,
541                                 cie->instructions_end, cie, fde, frame, pc);
542
543         /* FDE instructions */
544         dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
545                                 fde, frame, pc);
546
547         /* Calculate the CFA */
548         switch (frame->flags) {
549         case DWARF_FRAME_CFA_REG_OFFSET:
550                 if (prev) {
551                         BUG_ON(!prev->regs[frame->cfa_register].flags);
552
553                         addr = prev->cfa;
554                         addr += prev->regs[frame->cfa_register].addr;
555                         frame->cfa = __raw_readl(addr);
556
557                 } else {
558                         /*
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
563                          * function.
564                          */
565                         frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
566                 }
567
568                 frame->cfa += frame->cfa_offset;
569                 break;
570         default:
571                 BUG();
572         }
573
574         /* If we haven't seen the return address reg, we're screwed. */
575         BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);
576
577         for (i = 0; i <= frame->num_regs; i++) {
578                 struct dwarf_reg *reg = &frame->regs[i];
579
580                 if (!reg->flags)
581                         continue;
582
583                 offset = reg->addr;
584                 offset += frame->cfa;
585         }
586
587         addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;
588         frame->return_addr = __raw_readl(addr);
589
590         frame->next = dwarf_unwind_stack(frame->return_addr, frame);
591         return frame;
592 }
593
594 static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
595                            unsigned char *end)
596 {
597         struct dwarf_cie *cie;
598         unsigned long flags;
599         int count;
600
601         cie = kzalloc(sizeof(*cie), GFP_KERNEL);
602         if (!cie)
603                 return -ENOMEM;
604
605         cie->length = len;
606
607         /*
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
611          * corresponding FDE.
612          */
613         cie->cie_pointer = (unsigned long)entry;
614
615         cie->version = *(char *)p++;
616         BUG_ON(cie->version != 1);
617
618         cie->augmentation = p;
619         p += strlen(cie->augmentation) + 1;
620
621         count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
622         p += count;
623
624         count = dwarf_read_leb128(p, &cie->data_alignment_factor);
625         p += count;
626
627         /*
628          * Which column in the rule table contains the
629          * return address?
630          */
631         if (cie->version == 1) {
632                 cie->return_address_reg = __raw_readb(p);
633                 p++;
634         } else {
635                 count = dwarf_read_uleb128(p, &cie->return_address_reg);
636                 p += count;
637         }
638
639         if (cie->augmentation[0] == 'z') {
640                 unsigned int length, count;
641                 cie->flags |= DWARF_CIE_Z_AUGMENTATION;
642
643                 count = dwarf_read_uleb128(p, &length);
644                 p += count;
645
646                 BUG_ON((unsigned char *)p > end);
647
648                 cie->initial_instructions = p + length;
649                 cie->augmentation++;
650         }
651
652         while (*cie->augmentation) {
653                 /*
654                  * "L" indicates a byte showing how the
655                  * LSDA pointer is encoded. Skip it.
656                  */
657                 if (*cie->augmentation == 'L') {
658                         p++;
659                         cie->augmentation++;
660                 } else if (*cie->augmentation == 'R') {
661                         /*
662                          * "R" indicates a byte showing
663                          * how FDE addresses are
664                          * encoded.
665                          */
666                         cie->encoding = *(char *)p++;
667                         cie->augmentation++;
668                 } else if (*cie->augmentation == 'P') {
669                         /*
670                          * "R" indicates a personality
671                          * routine in the CIE
672                          * augmentation.
673                          */
674                         BUG();
675                 } else if (*cie->augmentation == 'S') {
676                         BUG();
677                 } else {
678                         /*
679                          * Unknown augmentation. Assume
680                          * 'z' augmentation.
681                          */
682                         p = cie->initial_instructions;
683                         BUG_ON(!p);
684                         break;
685                 }
686         }
687
688         cie->initial_instructions = p;
689         cie->instructions_end = end;
690
691         /* Add to list */
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);
695
696         return 0;
697 }
698
699 static int dwarf_parse_fde(void *entry, u32 entry_type,
700                            void *start, unsigned long len)
701 {
702         struct dwarf_fde *fde;
703         struct dwarf_cie *cie;
704         unsigned long flags;
705         int count;
706         void *p = start;
707
708         fde = kzalloc(sizeof(*fde), GFP_KERNEL);
709         if (!fde)
710                 return -ENOMEM;
711
712         fde->length = len;
713
714         /*
715          * In a .eh_frame section the CIE pointer is the
716          * delta between the address within the FDE
717          */
718         fde->cie_pointer = (unsigned long)(p - entry_type - 4);
719
720         cie = dwarf_lookup_cie(fde->cie_pointer);
721         fde->cie = cie;
722
723         if (cie->encoding)
724                 count = dwarf_read_encoded_value(p, &fde->initial_location,
725                                                  cie->encoding);
726         else
727                 count = dwarf_read_addr(p, &fde->initial_location);
728
729         p += count;
730
731         if (cie->encoding)
732                 count = dwarf_read_encoded_value(p, &fde->address_range,
733                                                  cie->encoding & 0x0f);
734         else
735                 count = dwarf_read_addr(p, &fde->address_range);
736
737         p += count;
738
739         if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
740                 unsigned int length;
741                 count = dwarf_read_uleb128(p, &length);
742                 p += count + length;
743         }
744
745         /* Call frame instructions. */
746         fde->instructions = p;
747         fde->end = start + len;
748
749         /* Add to list. */
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);
753
754         return 0;
755 }
756
757 static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,
758                                 unsigned long *sp,
759                                 const struct stacktrace_ops *ops, void *data)
760 {
761         struct dwarf_frame *frame;
762
763         frame = dwarf_unwind_stack(0, NULL);
764
765         while (frame && frame->return_addr) {
766                 ops->address(data, frame->return_addr, 1);
767                 frame = frame->next;
768         }
769 }
770
771 static struct unwinder dwarf_unwinder = {
772         .name = "dwarf-unwinder",
773         .dump = dwarf_unwinder_dump,
774         .rating = 150,
775 };
776
777 static void dwarf_unwinder_cleanup(void)
778 {
779         struct dwarf_cie *cie, *m;
780         struct dwarf_fde *fde, *n;
781         unsigned long flags;
782
783         /*
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.
787          */
788         spin_lock_irqsave(&dwarf_cie_lock, flags);
789         list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)
790                 kfree(cie);
791         spin_unlock_irqrestore(&dwarf_cie_lock, flags);
792
793         spin_lock_irqsave(&dwarf_fde_lock, flags);
794         list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)
795                 kfree(fde);
796         spin_unlock_irqrestore(&dwarf_fde_lock, flags);
797 }
798
799 /**
800  *      dwarf_unwinder_init - initialise the dwarf unwinder
801  *
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.
807  */
808 void dwarf_unwinder_init(void)
809 {
810         u32 entry_type;
811         void *p, *entry;
812         int count, err;
813         unsigned long len;
814         unsigned int c_entries, f_entries;
815         unsigned char *end;
816         INIT_LIST_HEAD(&dwarf_cie_list);
817         INIT_LIST_HEAD(&dwarf_fde_list);
818
819         c_entries = 0;
820         f_entries = 0;
821         entry = &__start_eh_frame;
822
823         while ((char *)entry < __stop_eh_frame) {
824                 p = entry;
825
826                 count = dwarf_entry_len(p, &len);
827                 if (count == 0) {
828                         /*
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.
834                          */
835                         goto out;
836                 } else
837                         p += count;
838
839                 /* initial length does not include itself */
840                 end = p + len;
841
842                 entry_type = get_unaligned((u32 *)p);
843                 p += 4;
844
845                 if (entry_type == DW_EH_FRAME_CIE) {
846                         err = dwarf_parse_cie(entry, p, len, end);
847                         if (err < 0)
848                                 goto out;
849                         else
850                                 c_entries++;
851                 } else {
852                         err = dwarf_parse_fde(entry, entry_type, p, len);
853                         if (err < 0)
854                                 goto out;
855                         else
856                                 f_entries++;
857                 }
858
859                 entry = (char *)entry + len + 4;
860         }
861
862         printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
863                c_entries, f_entries);
864
865         err = unwinder_register(&dwarf_unwinder);
866         if (err)
867                 goto out;
868
869         return;
870
871 out:
872         printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
873         dwarf_unwinder_cleanup();
874 }