blob: f242cd120cf1eb77c2d5786c11d972cc15fd9344 [file] [log] [blame]
Matt Flemingbd353862009-08-14 01:58:43 +09001/*
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.
Matt Fleming97efbbd2009-08-16 15:56:35 +010014 * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.
Matt Flemingbd353862009-08-14 01:58:43 +090015 */
16
17/* #define DEBUG */
18#include <linux/kernel.h>
19#include <linux/io.h>
20#include <linux/list.h>
Matt Flemingfb3f3e72009-08-16 15:44:08 +010021#include <linux/mempool.h>
Matt Flemingbd353862009-08-14 01:58:43 +090022#include <linux/mm.h>
23#include <asm/dwarf.h>
24#include <asm/unwinder.h>
25#include <asm/sections.h>
Paul Mundt34974472009-08-14 02:10:59 +090026#include <asm/unaligned.h>
Matt Flemingbd353862009-08-14 01:58:43 +090027#include <asm/dwarf.h>
28#include <asm/stacktrace.h>
29
Matt Flemingfb3f3e72009-08-16 15:44:08 +010030/* 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
35static struct kmem_cache *dwarf_frame_cachep;
36static mempool_t *dwarf_frame_pool;
37
38static struct kmem_cache *dwarf_reg_cachep;
39static mempool_t *dwarf_reg_pool;
40
Matt Flemingbd353862009-08-14 01:58:43 +090041static LIST_HEAD(dwarf_cie_list);
Paul Mundt97f361e2009-08-17 05:07:38 +090042static DEFINE_SPINLOCK(dwarf_cie_lock);
Matt Flemingbd353862009-08-14 01:58:43 +090043
44static LIST_HEAD(dwarf_fde_list);
Paul Mundt97f361e2009-08-17 05:07:38 +090045static DEFINE_SPINLOCK(dwarf_fde_lock);
Matt Flemingbd353862009-08-14 01:58:43 +090046
47static struct dwarf_cie *cached_cie;
48
Matt Flemingfb3f3e72009-08-16 15:44:08 +010049/**
50 * dwarf_frame_alloc_reg - allocate memory for a DWARF register
51 * @frame: the DWARF frame whose list of registers we insert on
52 * @reg_num: the register number
Matt Flemingbd353862009-08-14 01:58:43 +090053 *
Matt Flemingfb3f3e72009-08-16 15:44:08 +010054 * Allocate space for, and initialise, a dwarf reg from
55 * dwarf_reg_pool and insert it onto the (unsorted) linked-list of
56 * dwarf registers for @frame.
57 *
58 * Return the initialised DWARF reg.
Matt Flemingbd353862009-08-14 01:58:43 +090059 */
Matt Flemingfb3f3e72009-08-16 15:44:08 +010060static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
61 unsigned int reg_num)
Matt Flemingbd353862009-08-14 01:58:43 +090062{
Matt Flemingfb3f3e72009-08-16 15:44:08 +010063 struct dwarf_reg *reg;
Matt Flemingbd353862009-08-14 01:58:43 +090064
Matt Flemingfb3f3e72009-08-16 15:44:08 +010065 reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
66 if (!reg) {
67 printk(KERN_WARNING "Unable to allocate a DWARF register\n");
Matt Flemingbd353862009-08-14 01:58:43 +090068 /*
69 * Let's just bomb hard here, we have no way to
70 * gracefully recover.
71 */
Matt Flemingb344e24a2009-08-16 21:54:48 +010072 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +090073 }
74
Matt Flemingfb3f3e72009-08-16 15:44:08 +010075 reg->number = reg_num;
76 reg->addr = 0;
77 reg->flags = 0;
78
79 list_add(&reg->link, &frame->reg_list);
80
81 return reg;
82}
83
84static void dwarf_frame_free_regs(struct dwarf_frame *frame)
85{
86 struct dwarf_reg *reg, *n;
87
88 list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
89 list_del(&reg->link);
90 mempool_free(reg, dwarf_reg_pool);
91 }
92}
93
94/**
95 * dwarf_frame_reg - return a DWARF register
96 * @frame: the DWARF frame to search in for @reg_num
97 * @reg_num: the register number to search for
98 *
99 * Lookup and return the dwarf reg @reg_num for this frame. Return
100 * NULL if @reg_num is an register invalid number.
101 */
102static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
103 unsigned int reg_num)
104{
105 struct dwarf_reg *reg;
106
107 list_for_each_entry(reg, &frame->reg_list, link) {
108 if (reg->number == reg_num)
109 return reg;
Matt Flemingbd353862009-08-14 01:58:43 +0900110 }
111
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100112 return NULL;
Matt Flemingbd353862009-08-14 01:58:43 +0900113}
114
115/**
116 * dwarf_read_addr - read dwarf data
117 * @src: source address of data
118 * @dst: destination address to store the data to
119 *
120 * Read 'n' bytes from @src, where 'n' is the size of an address on
121 * the native machine. We return the number of bytes read, which
122 * should always be 'n'. We also have to be careful when reading
123 * from @src and writing to @dst, because they can be arbitrarily
124 * aligned. Return 'n' - the number of bytes read.
125 */
Paul Mundt34974472009-08-14 02:10:59 +0900126static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
Matt Flemingbd353862009-08-14 01:58:43 +0900127{
Paul Mundtbf43a162009-08-14 03:06:13 +0900128 u32 val = get_unaligned(src);
129 put_unaligned(val, dst);
Matt Flemingbd353862009-08-14 01:58:43 +0900130 return sizeof(unsigned long *);
131}
132
133/**
134 * dwarf_read_uleb128 - read unsigned LEB128 data
135 * @addr: the address where the ULEB128 data is stored
136 * @ret: address to store the result
137 *
138 * Decode an unsigned LEB128 encoded datum. The algorithm is taken
139 * from Appendix C of the DWARF 3 spec. For information on the
140 * encodings refer to section "7.6 - Variable Length Data". Return
141 * the number of bytes read.
142 */
143static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
144{
145 unsigned int result;
146 unsigned char byte;
147 int shift, count;
148
149 result = 0;
150 shift = 0;
151 count = 0;
152
153 while (1) {
154 byte = __raw_readb(addr);
155 addr++;
156 count++;
157
158 result |= (byte & 0x7f) << shift;
159 shift += 7;
160
161 if (!(byte & 0x80))
162 break;
163 }
164
165 *ret = result;
166
167 return count;
168}
169
170/**
171 * dwarf_read_leb128 - read signed LEB128 data
172 * @addr: the address of the LEB128 encoded data
173 * @ret: address to store the result
174 *
175 * Decode signed LEB128 data. The algorithm is taken from Appendix
176 * C of the DWARF 3 spec. Return the number of bytes read.
177 */
178static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
179{
180 unsigned char byte;
181 int result, shift;
182 int num_bits;
183 int count;
184
185 result = 0;
186 shift = 0;
187 count = 0;
188
189 while (1) {
190 byte = __raw_readb(addr);
191 addr++;
192 result |= (byte & 0x7f) << shift;
193 shift += 7;
194 count++;
195
196 if (!(byte & 0x80))
197 break;
198 }
199
200 /* The number of bits in a signed integer. */
201 num_bits = 8 * sizeof(result);
202
203 if ((shift < num_bits) && (byte & 0x40))
204 result |= (-1 << shift);
205
206 *ret = result;
207
208 return count;
209}
210
211/**
212 * dwarf_read_encoded_value - return the decoded value at @addr
213 * @addr: the address of the encoded value
214 * @val: where to write the decoded value
215 * @encoding: the encoding with which we can decode @addr
216 *
217 * GCC emits encoded address in the .eh_frame FDE entries. Decode
218 * the value at @addr using @encoding. The decoded value is written
219 * to @val and the number of bytes read is returned.
220 */
221static int dwarf_read_encoded_value(char *addr, unsigned long *val,
222 char encoding)
223{
224 unsigned long decoded_addr = 0;
225 int count = 0;
226
227 switch (encoding & 0x70) {
228 case DW_EH_PE_absptr:
229 break;
230 case DW_EH_PE_pcrel:
231 decoded_addr = (unsigned long)addr;
232 break;
233 default:
234 pr_debug("encoding=0x%x\n", (encoding & 0x70));
Matt Flemingb344e24a2009-08-16 21:54:48 +0100235 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900236 }
237
238 if ((encoding & 0x07) == 0x00)
239 encoding |= DW_EH_PE_udata4;
240
241 switch (encoding & 0x0f) {
242 case DW_EH_PE_sdata4:
243 case DW_EH_PE_udata4:
244 count += 4;
Paul Mundt34974472009-08-14 02:10:59 +0900245 decoded_addr += get_unaligned((u32 *)addr);
Matt Flemingbd353862009-08-14 01:58:43 +0900246 __raw_writel(decoded_addr, val);
247 break;
248 default:
249 pr_debug("encoding=0x%x\n", encoding);
Matt Flemingb344e24a2009-08-16 21:54:48 +0100250 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900251 }
252
253 return count;
254}
255
256/**
257 * dwarf_entry_len - return the length of an FDE or CIE
258 * @addr: the address of the entry
259 * @len: the length of the entry
260 *
261 * Read the initial_length field of the entry and store the size of
262 * the entry in @len. We return the number of bytes read. Return a
263 * count of 0 on error.
264 */
265static inline int dwarf_entry_len(char *addr, unsigned long *len)
266{
267 u32 initial_len;
268 int count;
269
Paul Mundt34974472009-08-14 02:10:59 +0900270 initial_len = get_unaligned((u32 *)addr);
Matt Flemingbd353862009-08-14 01:58:43 +0900271 count = 4;
272
273 /*
274 * An initial length field value in the range DW_LEN_EXT_LO -
275 * DW_LEN_EXT_HI indicates an extension, and should not be
276 * interpreted as a length. The only extension that we currently
277 * understand is the use of DWARF64 addresses.
278 */
279 if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
280 /*
281 * The 64-bit length field immediately follows the
282 * compulsory 32-bit length field.
283 */
284 if (initial_len == DW_EXT_DWARF64) {
Paul Mundt34974472009-08-14 02:10:59 +0900285 *len = get_unaligned((u64 *)addr + 4);
Matt Flemingbd353862009-08-14 01:58:43 +0900286 count = 12;
287 } else {
288 printk(KERN_WARNING "Unknown DWARF extension\n");
289 count = 0;
290 }
291 } else
292 *len = initial_len;
293
294 return count;
295}
296
297/**
298 * dwarf_lookup_cie - locate the cie
299 * @cie_ptr: pointer to help with lookup
300 */
301static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
302{
Paul Mundt97f361e2009-08-17 05:07:38 +0900303 struct dwarf_cie *cie;
Matt Flemingbd353862009-08-14 01:58:43 +0900304 unsigned long flags;
305
306 spin_lock_irqsave(&dwarf_cie_lock, flags);
307
308 /*
309 * We've cached the last CIE we looked up because chances are
310 * that the FDE wants this CIE.
311 */
312 if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
313 cie = cached_cie;
314 goto out;
315 }
316
Paul Mundt97f361e2009-08-17 05:07:38 +0900317 list_for_each_entry(cie, &dwarf_cie_list, link) {
Matt Flemingbd353862009-08-14 01:58:43 +0900318 if (cie->cie_pointer == cie_ptr) {
319 cached_cie = cie;
320 break;
321 }
322 }
323
324 /* Couldn't find the entry in the list. */
325 if (&cie->link == &dwarf_cie_list)
326 cie = NULL;
327out:
328 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
329 return cie;
330}
331
332/**
333 * dwarf_lookup_fde - locate the FDE that covers pc
334 * @pc: the program counter
335 */
336struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
337{
Paul Mundt97f361e2009-08-17 05:07:38 +0900338 struct dwarf_fde *fde;
Matt Flemingbd353862009-08-14 01:58:43 +0900339 unsigned long flags;
Matt Flemingbd353862009-08-14 01:58:43 +0900340
341 spin_lock_irqsave(&dwarf_fde_lock, flags);
Paul Mundt97f361e2009-08-17 05:07:38 +0900342
343 list_for_each_entry(fde, &dwarf_fde_list, link) {
Matt Flemingbd353862009-08-14 01:58:43 +0900344 unsigned long start, end;
345
346 start = fde->initial_location;
347 end = fde->initial_location + fde->address_range;
348
349 if (pc >= start && pc < end)
350 break;
351 }
352
353 /* Couldn't find the entry in the list. */
354 if (&fde->link == &dwarf_fde_list)
355 fde = NULL;
356
357 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
358
359 return fde;
360}
361
362/**
363 * dwarf_cfa_execute_insns - execute instructions to calculate a CFA
364 * @insn_start: address of the first instruction
365 * @insn_end: address of the last instruction
366 * @cie: the CIE for this function
367 * @fde: the FDE for this function
368 * @frame: the instructions calculate the CFA for this frame
369 * @pc: the program counter of the address we're interested in
370 *
371 * Execute the Call Frame instruction sequence starting at
372 * @insn_start and ending at @insn_end. The instructions describe
373 * how to calculate the Canonical Frame Address of a stackframe.
374 * Store the results in @frame.
375 */
376static int dwarf_cfa_execute_insns(unsigned char *insn_start,
377 unsigned char *insn_end,
378 struct dwarf_cie *cie,
379 struct dwarf_fde *fde,
380 struct dwarf_frame *frame,
Matt Flemingb9558732009-08-15 23:10:57 +0100381 unsigned long pc)
Matt Flemingbd353862009-08-14 01:58:43 +0900382{
383 unsigned char insn;
384 unsigned char *current_insn;
385 unsigned int count, delta, reg, expr_len, offset;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100386 struct dwarf_reg *regp;
Matt Flemingbd353862009-08-14 01:58:43 +0900387
388 current_insn = insn_start;
389
Matt Flemingb9558732009-08-15 23:10:57 +0100390 while (current_insn < insn_end && frame->pc <= pc) {
Matt Flemingbd353862009-08-14 01:58:43 +0900391 insn = __raw_readb(current_insn++);
392
393 /*
394 * Firstly, handle the opcodes that embed their operands
395 * in the instructions.
396 */
397 switch (DW_CFA_opcode(insn)) {
398 case DW_CFA_advance_loc:
399 delta = DW_CFA_operand(insn);
400 delta *= cie->code_alignment_factor;
401 frame->pc += delta;
402 continue;
403 /* NOTREACHED */
404 case DW_CFA_offset:
405 reg = DW_CFA_operand(insn);
406 count = dwarf_read_uleb128(current_insn, &offset);
407 current_insn += count;
408 offset *= cie->data_alignment_factor;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100409 regp = dwarf_frame_alloc_reg(frame, reg);
410 regp->addr = offset;
411 regp->flags |= DWARF_REG_OFFSET;
Matt Flemingbd353862009-08-14 01:58:43 +0900412 continue;
413 /* NOTREACHED */
414 case DW_CFA_restore:
415 reg = DW_CFA_operand(insn);
416 continue;
417 /* NOTREACHED */
418 }
419
420 /*
421 * Secondly, handle the opcodes that don't embed their
422 * operands in the instruction.
423 */
424 switch (insn) {
425 case DW_CFA_nop:
426 continue;
427 case DW_CFA_advance_loc1:
428 delta = *current_insn++;
429 frame->pc += delta * cie->code_alignment_factor;
430 break;
431 case DW_CFA_advance_loc2:
Paul Mundt34974472009-08-14 02:10:59 +0900432 delta = get_unaligned((u16 *)current_insn);
Matt Flemingbd353862009-08-14 01:58:43 +0900433 current_insn += 2;
434 frame->pc += delta * cie->code_alignment_factor;
435 break;
436 case DW_CFA_advance_loc4:
Paul Mundt34974472009-08-14 02:10:59 +0900437 delta = get_unaligned((u32 *)current_insn);
Matt Flemingbd353862009-08-14 01:58:43 +0900438 current_insn += 4;
439 frame->pc += delta * cie->code_alignment_factor;
440 break;
441 case DW_CFA_offset_extended:
442 count = dwarf_read_uleb128(current_insn, &reg);
443 current_insn += count;
444 count = dwarf_read_uleb128(current_insn, &offset);
445 current_insn += count;
446 offset *= cie->data_alignment_factor;
447 break;
448 case DW_CFA_restore_extended:
449 count = dwarf_read_uleb128(current_insn, &reg);
450 current_insn += count;
451 break;
452 case DW_CFA_undefined:
453 count = dwarf_read_uleb128(current_insn, &reg);
454 current_insn += count;
Matt Fleming5580e902009-08-20 19:53:49 +0100455 regp = dwarf_frame_alloc_reg(frame, reg);
456 regp->flags |= DWARF_UNDEFINED;
Matt Flemingbd353862009-08-14 01:58:43 +0900457 break;
458 case DW_CFA_def_cfa:
459 count = dwarf_read_uleb128(current_insn,
460 &frame->cfa_register);
461 current_insn += count;
462 count = dwarf_read_uleb128(current_insn,
463 &frame->cfa_offset);
464 current_insn += count;
465
466 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
467 break;
468 case DW_CFA_def_cfa_register:
469 count = dwarf_read_uleb128(current_insn,
470 &frame->cfa_register);
471 current_insn += count;
472 frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
473 break;
474 case DW_CFA_def_cfa_offset:
475 count = dwarf_read_uleb128(current_insn, &offset);
476 current_insn += count;
477 frame->cfa_offset = offset;
478 break;
479 case DW_CFA_def_cfa_expression:
480 count = dwarf_read_uleb128(current_insn, &expr_len);
481 current_insn += count;
482
483 frame->cfa_expr = current_insn;
484 frame->cfa_expr_len = expr_len;
485 current_insn += expr_len;
486
487 frame->flags |= DWARF_FRAME_CFA_REG_EXP;
488 break;
489 case DW_CFA_offset_extended_sf:
490 count = dwarf_read_uleb128(current_insn, &reg);
491 current_insn += count;
492 count = dwarf_read_leb128(current_insn, &offset);
493 current_insn += count;
494 offset *= cie->data_alignment_factor;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100495 regp = dwarf_frame_alloc_reg(frame, reg);
496 regp->flags |= DWARF_REG_OFFSET;
497 regp->addr = offset;
Matt Flemingbd353862009-08-14 01:58:43 +0900498 break;
499 case DW_CFA_val_offset:
500 count = dwarf_read_uleb128(current_insn, &reg);
501 current_insn += count;
502 count = dwarf_read_leb128(current_insn, &offset);
503 offset *= cie->data_alignment_factor;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100504 regp = dwarf_frame_alloc_reg(frame, reg);
Matt Fleming97efbbd2009-08-16 15:56:35 +0100505 regp->flags |= DWARF_VAL_OFFSET;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100506 regp->addr = offset;
Matt Flemingbd353862009-08-14 01:58:43 +0900507 break;
Matt Flemingcd7246f2009-08-16 01:44:33 +0100508 case DW_CFA_GNU_args_size:
509 count = dwarf_read_uleb128(current_insn, &offset);
510 current_insn += count;
511 break;
512 case DW_CFA_GNU_negative_offset_extended:
513 count = dwarf_read_uleb128(current_insn, &reg);
514 current_insn += count;
515 count = dwarf_read_uleb128(current_insn, &offset);
516 offset *= cie->data_alignment_factor;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100517
518 regp = dwarf_frame_alloc_reg(frame, reg);
519 regp->flags |= DWARF_REG_OFFSET;
520 regp->addr = -offset;
Matt Flemingcd7246f2009-08-16 01:44:33 +0100521 break;
Matt Flemingbd353862009-08-14 01:58:43 +0900522 default:
523 pr_debug("unhandled DWARF instruction 0x%x\n", insn);
Matt Flemingb344e24a2009-08-16 21:54:48 +0100524 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900525 break;
526 }
527 }
528
529 return 0;
530}
531
532/**
Matt Fleminged4fe7f2009-10-10 16:03:11 +0100533 * dwarf_free_frame - free the memory allocated for @frame
534 * @frame: the frame to free
535 */
536void dwarf_free_frame(struct dwarf_frame *frame)
537{
538 dwarf_frame_free_regs(frame);
539 mempool_free(frame, dwarf_frame_pool);
540}
541
542/**
Matt Flemingc2d474d62009-10-10 16:17:06 +0100543 * dwarf_unwind_stack - unwind the stack
544 *
Matt Flemingbd353862009-08-14 01:58:43 +0900545 * @pc: address of the function to unwind
546 * @prev: struct dwarf_frame of the previous stackframe on the callstack
547 *
548 * Return a struct dwarf_frame representing the most recent frame
549 * on the callstack. Each of the lower (older) stack frames are
550 * linked via the "prev" member.
551 */
Matt Flemingb344e24a2009-08-16 21:54:48 +0100552struct dwarf_frame * dwarf_unwind_stack(unsigned long pc,
553 struct dwarf_frame *prev)
Matt Flemingbd353862009-08-14 01:58:43 +0900554{
555 struct dwarf_frame *frame;
556 struct dwarf_cie *cie;
557 struct dwarf_fde *fde;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100558 struct dwarf_reg *reg;
Matt Flemingbd353862009-08-14 01:58:43 +0900559 unsigned long addr;
Matt Flemingbd353862009-08-14 01:58:43 +0900560
561 /*
Matt Flemingc2d474d62009-10-10 16:17:06 +0100562 * If we're starting at the top of the stack we need get the
563 * contents of a physical register to get the CFA in order to
564 * begin the virtual unwinding of the stack.
Matt Flemingbd353862009-08-14 01:58:43 +0900565 *
Matt Flemingf8264662009-08-13 20:41:31 +0100566 * NOTE: the return address is guaranteed to be setup by the
567 * time this function makes its first function call.
Matt Flemingbd353862009-08-14 01:58:43 +0900568 */
Matt Flemingb9558732009-08-15 23:10:57 +0100569 if (!pc && !prev)
570 pc = (unsigned long)current_text_addr();
Matt Flemingbd353862009-08-14 01:58:43 +0900571
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100572 frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
573 if (!frame) {
574 printk(KERN_ERR "Unable to allocate a dwarf frame\n");
Matt Flemingb344e24a2009-08-16 21:54:48 +0100575 UNWINDER_BUG();
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100576 }
Matt Flemingbd353862009-08-14 01:58:43 +0900577
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100578 INIT_LIST_HEAD(&frame->reg_list);
579 frame->flags = 0;
Matt Flemingbd353862009-08-14 01:58:43 +0900580 frame->prev = prev;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100581 frame->return_addr = 0;
Matt Flemingbd353862009-08-14 01:58:43 +0900582
583 fde = dwarf_lookup_fde(pc);
584 if (!fde) {
585 /*
Matt Flemingc2d474d62009-10-10 16:17:06 +0100586 * This is our normal exit path. There are two reasons
587 * why we might exit here,
Matt Flemingbd353862009-08-14 01:58:43 +0900588 *
589 * a) pc has no asscociated DWARF frame info and so
590 * we don't know how to unwind this frame. This is
591 * usually the case when we're trying to unwind a
592 * frame that was called from some assembly code
593 * that has no DWARF info, e.g. syscalls.
594 *
595 * b) the DEBUG info for pc is bogus. There's
596 * really no way to distinguish this case from the
597 * case above, which sucks because we could print a
598 * warning here.
599 */
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100600 goto bail;
Matt Flemingbd353862009-08-14 01:58:43 +0900601 }
602
603 cie = dwarf_lookup_cie(fde->cie_pointer);
604
605 frame->pc = fde->initial_location;
606
607 /* CIE initial instructions */
608 dwarf_cfa_execute_insns(cie->initial_instructions,
Matt Flemingf8264662009-08-13 20:41:31 +0100609 cie->instructions_end, cie, fde,
Matt Flemingb9558732009-08-15 23:10:57 +0100610 frame, pc);
Matt Flemingbd353862009-08-14 01:58:43 +0900611
612 /* FDE instructions */
613 dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
Matt Flemingb9558732009-08-15 23:10:57 +0100614 fde, frame, pc);
Matt Flemingbd353862009-08-14 01:58:43 +0900615
616 /* Calculate the CFA */
617 switch (frame->flags) {
618 case DWARF_FRAME_CFA_REG_OFFSET:
619 if (prev) {
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100620 reg = dwarf_frame_reg(prev, frame->cfa_register);
Matt Flemingb344e24a2009-08-16 21:54:48 +0100621 UNWINDER_BUG_ON(!reg);
622 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
Matt Flemingbd353862009-08-14 01:58:43 +0900623
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100624 addr = prev->cfa + reg->addr;
Matt Flemingbd353862009-08-14 01:58:43 +0900625 frame->cfa = __raw_readl(addr);
626
627 } else {
628 /*
Matt Flemingc2d474d62009-10-10 16:17:06 +0100629 * Again, we're starting from the top of the
630 * stack. We need to physically read
631 * the contents of a register in order to get
632 * the Canonical Frame Address for this
Matt Flemingbd353862009-08-14 01:58:43 +0900633 * function.
634 */
635 frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
636 }
637
638 frame->cfa += frame->cfa_offset;
639 break;
640 default:
Matt Flemingb344e24a2009-08-16 21:54:48 +0100641 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900642 }
643
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100644 reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
Matt Fleming5580e902009-08-20 19:53:49 +0100645
646 /*
647 * If we haven't seen the return address register or the return
648 * address column is undefined then we must assume that this is
649 * the end of the callstack.
650 */
651 if (!reg || reg->flags == DWARF_UNDEFINED)
652 goto bail;
653
Matt Flemingb344e24a2009-08-16 21:54:48 +0100654 UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
Matt Flemingbd353862009-08-14 01:58:43 +0900655
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100656 addr = frame->cfa + reg->addr;
Matt Flemingbd353862009-08-14 01:58:43 +0900657 frame->return_addr = __raw_readl(addr);
658
Matt Flemingbd353862009-08-14 01:58:43 +0900659 return frame;
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100660
661bail:
Matt Fleminged4fe7f2009-10-10 16:03:11 +0100662 dwarf_free_frame(frame);
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100663 return NULL;
Matt Flemingbd353862009-08-14 01:58:43 +0900664}
665
666static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100667 unsigned char *end, struct module *mod)
Matt Flemingbd353862009-08-14 01:58:43 +0900668{
669 struct dwarf_cie *cie;
670 unsigned long flags;
671 int count;
672
673 cie = kzalloc(sizeof(*cie), GFP_KERNEL);
674 if (!cie)
675 return -ENOMEM;
676
677 cie->length = len;
678
679 /*
680 * Record the offset into the .eh_frame section
681 * for this CIE. It allows this CIE to be
682 * quickly and easily looked up from the
683 * corresponding FDE.
684 */
685 cie->cie_pointer = (unsigned long)entry;
686
687 cie->version = *(char *)p++;
Matt Flemingb344e24a2009-08-16 21:54:48 +0100688 UNWINDER_BUG_ON(cie->version != 1);
Matt Flemingbd353862009-08-14 01:58:43 +0900689
690 cie->augmentation = p;
691 p += strlen(cie->augmentation) + 1;
692
693 count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
694 p += count;
695
696 count = dwarf_read_leb128(p, &cie->data_alignment_factor);
697 p += count;
698
699 /*
700 * Which column in the rule table contains the
701 * return address?
702 */
703 if (cie->version == 1) {
704 cie->return_address_reg = __raw_readb(p);
705 p++;
706 } else {
707 count = dwarf_read_uleb128(p, &cie->return_address_reg);
708 p += count;
709 }
710
711 if (cie->augmentation[0] == 'z') {
712 unsigned int length, count;
713 cie->flags |= DWARF_CIE_Z_AUGMENTATION;
714
715 count = dwarf_read_uleb128(p, &length);
716 p += count;
717
Matt Flemingb344e24a2009-08-16 21:54:48 +0100718 UNWINDER_BUG_ON((unsigned char *)p > end);
Matt Flemingbd353862009-08-14 01:58:43 +0900719
720 cie->initial_instructions = p + length;
721 cie->augmentation++;
722 }
723
724 while (*cie->augmentation) {
725 /*
726 * "L" indicates a byte showing how the
727 * LSDA pointer is encoded. Skip it.
728 */
729 if (*cie->augmentation == 'L') {
730 p++;
731 cie->augmentation++;
732 } else if (*cie->augmentation == 'R') {
733 /*
734 * "R" indicates a byte showing
735 * how FDE addresses are
736 * encoded.
737 */
738 cie->encoding = *(char *)p++;
739 cie->augmentation++;
740 } else if (*cie->augmentation == 'P') {
741 /*
742 * "R" indicates a personality
743 * routine in the CIE
744 * augmentation.
745 */
Matt Flemingb344e24a2009-08-16 21:54:48 +0100746 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900747 } else if (*cie->augmentation == 'S') {
Matt Flemingb344e24a2009-08-16 21:54:48 +0100748 UNWINDER_BUG();
Matt Flemingbd353862009-08-14 01:58:43 +0900749 } else {
750 /*
751 * Unknown augmentation. Assume
752 * 'z' augmentation.
753 */
754 p = cie->initial_instructions;
Matt Flemingb344e24a2009-08-16 21:54:48 +0100755 UNWINDER_BUG_ON(!p);
Matt Flemingbd353862009-08-14 01:58:43 +0900756 break;
757 }
758 }
759
760 cie->initial_instructions = p;
761 cie->instructions_end = end;
762
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100763 cie->mod = mod;
764
Matt Flemingbd353862009-08-14 01:58:43 +0900765 /* Add to list */
766 spin_lock_irqsave(&dwarf_cie_lock, flags);
767 list_add_tail(&cie->link, &dwarf_cie_list);
768 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
769
770 return 0;
771}
772
773static int dwarf_parse_fde(void *entry, u32 entry_type,
Matt Fleming54806752009-08-20 19:42:34 +0100774 void *start, unsigned long len,
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100775 unsigned char *end, struct module *mod)
Matt Flemingbd353862009-08-14 01:58:43 +0900776{
777 struct dwarf_fde *fde;
778 struct dwarf_cie *cie;
779 unsigned long flags;
780 int count;
781 void *p = start;
782
783 fde = kzalloc(sizeof(*fde), GFP_KERNEL);
784 if (!fde)
785 return -ENOMEM;
786
787 fde->length = len;
788
789 /*
790 * In a .eh_frame section the CIE pointer is the
791 * delta between the address within the FDE
792 */
793 fde->cie_pointer = (unsigned long)(p - entry_type - 4);
794
795 cie = dwarf_lookup_cie(fde->cie_pointer);
796 fde->cie = cie;
797
798 if (cie->encoding)
799 count = dwarf_read_encoded_value(p, &fde->initial_location,
800 cie->encoding);
801 else
802 count = dwarf_read_addr(p, &fde->initial_location);
803
804 p += count;
805
806 if (cie->encoding)
807 count = dwarf_read_encoded_value(p, &fde->address_range,
808 cie->encoding & 0x0f);
809 else
810 count = dwarf_read_addr(p, &fde->address_range);
811
812 p += count;
813
814 if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
815 unsigned int length;
816 count = dwarf_read_uleb128(p, &length);
817 p += count + length;
818 }
819
820 /* Call frame instructions. */
821 fde->instructions = p;
Matt Fleming54806752009-08-20 19:42:34 +0100822 fde->end = end;
Matt Flemingbd353862009-08-14 01:58:43 +0900823
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100824 fde->mod = mod;
825
Matt Flemingbd353862009-08-14 01:58:43 +0900826 /* Add to list. */
827 spin_lock_irqsave(&dwarf_fde_lock, flags);
828 list_add_tail(&fde->link, &dwarf_fde_list);
829 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
830
831 return 0;
832}
833
Matt Flemingb344e24a2009-08-16 21:54:48 +0100834static void dwarf_unwinder_dump(struct task_struct *task,
835 struct pt_regs *regs,
Matt Flemingbd353862009-08-14 01:58:43 +0900836 unsigned long *sp,
Matt Flemingb344e24a2009-08-16 21:54:48 +0100837 const struct stacktrace_ops *ops,
838 void *data)
Matt Flemingbd353862009-08-14 01:58:43 +0900839{
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100840 struct dwarf_frame *frame, *_frame;
841 unsigned long return_addr;
Matt Flemingbd353862009-08-14 01:58:43 +0900842
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100843 _frame = NULL;
844 return_addr = 0;
Matt Flemingbd353862009-08-14 01:58:43 +0900845
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100846 while (1) {
847 frame = dwarf_unwind_stack(return_addr, _frame);
848
Matt Fleminged4fe7f2009-10-10 16:03:11 +0100849 if (_frame)
850 dwarf_free_frame(_frame);
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100851
852 _frame = frame;
853
854 if (!frame || !frame->return_addr)
855 break;
856
857 return_addr = frame->return_addr;
858 ops->address(data, return_addr, 1);
Matt Flemingbd353862009-08-14 01:58:43 +0900859 }
Matt Fleminged4fe7f2009-10-10 16:03:11 +0100860
861 if (frame)
862 dwarf_free_frame(frame);
Matt Flemingbd353862009-08-14 01:58:43 +0900863}
864
865static struct unwinder dwarf_unwinder = {
866 .name = "dwarf-unwinder",
867 .dump = dwarf_unwinder_dump,
868 .rating = 150,
869};
870
871static void dwarf_unwinder_cleanup(void)
872{
Paul Mundt97f361e2009-08-17 05:07:38 +0900873 struct dwarf_cie *cie;
874 struct dwarf_fde *fde;
Matt Flemingbd353862009-08-14 01:58:43 +0900875
876 /*
877 * Deallocate all the memory allocated for the DWARF unwinder.
878 * Traverse all the FDE/CIE lists and remove and free all the
879 * memory associated with those data structures.
880 */
Paul Mundt97f361e2009-08-17 05:07:38 +0900881 list_for_each_entry(cie, &dwarf_cie_list, link)
Matt Flemingbd353862009-08-14 01:58:43 +0900882 kfree(cie);
Matt Flemingbd353862009-08-14 01:58:43 +0900883
Paul Mundt97f361e2009-08-17 05:07:38 +0900884 list_for_each_entry(fde, &dwarf_fde_list, link)
Matt Flemingbd353862009-08-14 01:58:43 +0900885 kfree(fde);
Matt Flemingfb3f3e72009-08-16 15:44:08 +0100886
887 kmem_cache_destroy(dwarf_reg_cachep);
888 kmem_cache_destroy(dwarf_frame_cachep);
Matt Flemingbd353862009-08-14 01:58:43 +0900889}
890
891/**
Matt Fleminga6a2f2a2009-10-09 23:20:54 +0100892 * dwarf_parse_section - parse DWARF section
893 * @eh_frame_start: start address of the .eh_frame section
894 * @eh_frame_end: end address of the .eh_frame section
895 * @mod: the kernel module containing the .eh_frame section
896 *
897 * Parse the information in a .eh_frame section.
898 */
899int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
900 struct module *mod)
901{
902 u32 entry_type;
903 void *p, *entry;
904 int count, err;
905 unsigned long len;
906 unsigned int c_entries, f_entries;
907 unsigned char *end;
908
909 c_entries = 0;
910 f_entries = 0;
911 entry = eh_frame_start;
912
913 while ((char *)entry < eh_frame_end) {
914 p = entry;
915
916 count = dwarf_entry_len(p, &len);
917 if (count == 0) {
918 /*
919 * We read a bogus length field value. There is
920 * nothing we can do here apart from disabling
921 * the DWARF unwinder. We can't even skip this
922 * entry and move to the next one because 'len'
923 * tells us where our next entry is.
924 */
925 err = -EINVAL;
926 goto out;
927 } else
928 p += count;
929
930 /* initial length does not include itself */
931 end = p + len;
932
933 entry_type = get_unaligned((u32 *)p);
934 p += 4;
935
936 if (entry_type == DW_EH_FRAME_CIE) {
937 err = dwarf_parse_cie(entry, p, len, end, mod);
938 if (err < 0)
939 goto out;
940 else
941 c_entries++;
942 } else {
943 err = dwarf_parse_fde(entry, entry_type, p, len,
944 end, mod);
945 if (err < 0)
946 goto out;
947 else
948 f_entries++;
949 }
950
951 entry = (char *)entry + len + 4;
952 }
953
954 printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
955 c_entries, f_entries);
956
957 return 0;
958
959out:
960 return err;
961}
962
963/**
964 * dwarf_module_unload - remove FDE/CIEs associated with @mod
965 * @mod: the module that is being unloaded
966 *
967 * Remove any FDEs and CIEs from the global lists that came from
968 * @mod's .eh_frame section because @mod is being unloaded.
969 */
970void dwarf_module_unload(struct module *mod)
971{
972 struct dwarf_fde *fde;
973 struct dwarf_cie *cie;
974 unsigned long flags;
975
976 spin_lock_irqsave(&dwarf_cie_lock, flags);
977
978again_cie:
979 list_for_each_entry(cie, &dwarf_cie_list, link) {
980 if (cie->mod == mod)
981 break;
982 }
983
984 if (&cie->link != &dwarf_cie_list) {
985 list_del(&cie->link);
986 kfree(cie);
987 goto again_cie;
988 }
989
990 spin_unlock_irqrestore(&dwarf_cie_lock, flags);
991
992 spin_lock_irqsave(&dwarf_fde_lock, flags);
993
994again_fde:
995 list_for_each_entry(fde, &dwarf_fde_list, link) {
996 if (fde->mod == mod)
997 break;
998 }
999
1000 if (&fde->link != &dwarf_fde_list) {
1001 list_del(&fde->link);
1002 kfree(fde);
1003 goto again_fde;
1004 }
1005
1006 spin_unlock_irqrestore(&dwarf_fde_lock, flags);
1007}
1008
1009/**
Matt Flemingbd353862009-08-14 01:58:43 +09001010 * dwarf_unwinder_init - initialise the dwarf unwinder
1011 *
1012 * Build the data structures describing the .dwarf_frame section to
1013 * make it easier to lookup CIE and FDE entries. Because the
1014 * .eh_frame section is packed as tightly as possible it is not
1015 * easy to lookup the FDE for a given PC, so we build a list of FDE
1016 * and CIE entries that make it easier.
1017 */
Paul Mundt97f361e2009-08-17 05:07:38 +09001018static int __init dwarf_unwinder_init(void)
Matt Flemingbd353862009-08-14 01:58:43 +09001019{
Matt Fleminga6a2f2a2009-10-09 23:20:54 +01001020 int err;
Matt Flemingbd353862009-08-14 01:58:43 +09001021 INIT_LIST_HEAD(&dwarf_cie_list);
1022 INIT_LIST_HEAD(&dwarf_fde_list);
1023
Matt Flemingfb3f3e72009-08-16 15:44:08 +01001024 dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
1025 sizeof(struct dwarf_frame), 0, SLAB_PANIC, NULL);
1026 dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
1027 sizeof(struct dwarf_reg), 0, SLAB_PANIC, NULL);
1028
1029 dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
1030 mempool_alloc_slab,
1031 mempool_free_slab,
1032 dwarf_frame_cachep);
1033
1034 dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
1035 mempool_alloc_slab,
1036 mempool_free_slab,
1037 dwarf_reg_cachep);
1038
Matt Fleminga6a2f2a2009-10-09 23:20:54 +01001039 err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
1040 if (err)
1041 goto out;
Matt Flemingbd353862009-08-14 01:58:43 +09001042
1043 err = unwinder_register(&dwarf_unwinder);
1044 if (err)
1045 goto out;
1046
Paul Mundt97f361e2009-08-17 05:07:38 +09001047 return 0;
Matt Flemingbd353862009-08-14 01:58:43 +09001048
1049out:
1050 printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
1051 dwarf_unwinder_cleanup();
Paul Mundt97f361e2009-08-17 05:07:38 +09001052 return -EINVAL;
Matt Flemingbd353862009-08-14 01:58:43 +09001053}
Paul Mundt97f361e2009-08-17 05:07:38 +09001054early_initcall(dwarf_unwinder_init);