Blame view

arch/sh/kernel/dwarf.c 29.3 KB
bd353861c   Matt Fleming   sh: dwarf unwinde...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org>
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
   *
   * This is an implementation of a DWARF unwinder. Its main purpose is
   * for generating stacktrace information. Based on the DWARF 3
   * specification from http://www.dwarfstd.org.
   *
   * TODO:
   *	- DWARF64 doesn't work.
97efbbd58   Matt Fleming   sh: unwinder: Set...
14
   *	- Registers with DWARF_VAL_OFFSET rules aren't handled properly.
bd353861c   Matt Fleming   sh: dwarf unwinde...
15
16
17
18
19
20
   */
  
  /* #define DEBUG */
  #include <linux/kernel.h>
  #include <linux/io.h>
  #include <linux/list.h>
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
21
  #include <linux/mempool.h>
bd353861c   Matt Fleming   sh: dwarf unwinde...
22
  #include <linux/mm.h>
5a3abba77   Paul Mundt   sh: Tidy up the d...
23
  #include <linux/elf.h>
60339fad5   Matt Fleming   sh: Check for ret...
24
  #include <linux/ftrace.h>
1d5cc550e   Paul Mundt   sh: dwarf unwinde...
25
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
26
  #include <linux/slab.h>
bd353861c   Matt Fleming   sh: dwarf unwinde...
27
28
29
  #include <asm/dwarf.h>
  #include <asm/unwinder.h>
  #include <asm/sections.h>
3497447f1   Paul Mundt   sh: unwinder: Fix...
30
  #include <asm/unaligned.h>
bd353861c   Matt Fleming   sh: dwarf unwinde...
31
  #include <asm/stacktrace.h>
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
32
33
34
35
36
37
38
39
40
41
  /* Reserve enough memory for two stack frames */
  #define DWARF_FRAME_MIN_REQ	2
  /* ... with 4 registers per frame. */
  #define DWARF_REG_MIN_REQ	(DWARF_FRAME_MIN_REQ * 4)
  
  static struct kmem_cache *dwarf_frame_cachep;
  static mempool_t *dwarf_frame_pool;
  
  static struct kmem_cache *dwarf_reg_cachep;
  static mempool_t *dwarf_reg_pool;
858918b77   Matt Fleming   sh: Optimise FDE/...
42
  static struct rb_root cie_root;
97f361e24   Paul Mundt   sh: unwinder: Mov...
43
  static DEFINE_SPINLOCK(dwarf_cie_lock);
bd353861c   Matt Fleming   sh: dwarf unwinde...
44

858918b77   Matt Fleming   sh: Optimise FDE/...
45
  static struct rb_root fde_root;
97f361e24   Paul Mundt   sh: unwinder: Mov...
46
  static DEFINE_SPINLOCK(dwarf_fde_lock);
bd353861c   Matt Fleming   sh: dwarf unwinde...
47
48
  
  static struct dwarf_cie *cached_cie;
8a37f5205   Paul Mundt   sh: handle early ...
49
  static unsigned int dwarf_unwinder_ready;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
50
51
52
53
54
55
56
57
  /**
   *	dwarf_frame_alloc_reg - allocate memory for a DWARF register
   *	@frame: the DWARF frame whose list of registers we insert on
   *	@reg_num: the register number
   *
   *	Allocate space for, and initialise, a dwarf reg from
   *	dwarf_reg_pool and insert it onto the (unsorted) linked-list of
   *	dwarf registers for @frame.
bd353861c   Matt Fleming   sh: dwarf unwinde...
58
   *
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
59
   *	Return the initialised DWARF reg.
bd353861c   Matt Fleming   sh: dwarf unwinde...
60
   */
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
61
62
  static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
  					       unsigned int reg_num)
bd353861c   Matt Fleming   sh: dwarf unwinde...
63
  {
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
64
  	struct dwarf_reg *reg;
bd353861c   Matt Fleming   sh: dwarf unwinde...
65

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
66
67
68
69
  	reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
  	if (!reg) {
  		printk(KERN_WARNING "Unable to allocate a DWARF register
  ");
bd353861c   Matt Fleming   sh: dwarf unwinde...
70
71
72
73
  		/*
  		 * Let's just bomb hard here, we have no way to
  		 * gracefully recover.
  		 */
b344e24a8   Matt Fleming   sh: unwinder: Int...
74
  		UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
75
  	}
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  	reg->number = reg_num;
  	reg->addr = 0;
  	reg->flags = 0;
  
  	list_add(&reg->link, &frame->reg_list);
  
  	return reg;
  }
  
  static void dwarf_frame_free_regs(struct dwarf_frame *frame)
  {
  	struct dwarf_reg *reg, *n;
  
  	list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
  		list_del(&reg->link);
  		mempool_free(reg, dwarf_reg_pool);
  	}
  }
  
  /**
   *	dwarf_frame_reg - return a DWARF register
   *	@frame: the DWARF frame to search in for @reg_num
   *	@reg_num: the register number to search for
   *
   *	Lookup and return the dwarf reg @reg_num for this frame. Return
   *	NULL if @reg_num is an register invalid number.
   */
  static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
  					 unsigned int reg_num)
  {
  	struct dwarf_reg *reg;
  
  	list_for_each_entry(reg, &frame->reg_list, link) {
  		if (reg->number == reg_num)
  			return reg;
bd353861c   Matt Fleming   sh: dwarf unwinde...
111
  	}
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
112
  	return NULL;
bd353861c   Matt Fleming   sh: dwarf unwinde...
113
114
115
116
117
118
119
120
121
122
123
124
125
  }
  
  /**
   *	dwarf_read_addr - read dwarf data
   *	@src: source address of data
   *	@dst: destination address to store the data to
   *
   *	Read 'n' bytes from @src, where 'n' is the size of an address on
   *	the native machine. We return the number of bytes read, which
   *	should always be 'n'. We also have to be careful when reading
   *	from @src and writing to @dst, because they can be arbitrarily
   *	aligned. Return 'n' - the number of bytes read.
   */
3497447f1   Paul Mundt   sh: unwinder: Fix...
126
  static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
bd353861c   Matt Fleming   sh: dwarf unwinde...
127
  {
bf43a160f   Paul Mundt   sh: unwinder: Res...
128
129
  	u32 val = get_unaligned(src);
  	put_unaligned(val, dst);
bd353861c   Matt Fleming   sh: dwarf unwinde...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  	return sizeof(unsigned long *);
  }
  
  /**
   *	dwarf_read_uleb128 - read unsigned LEB128 data
   *	@addr: the address where the ULEB128 data is stored
   *	@ret: address to store the result
   *
   *	Decode an unsigned LEB128 encoded datum. The algorithm is taken
   *	from Appendix C of the DWARF 3 spec. For information on the
   *	encodings refer to section "7.6 - Variable Length Data". Return
   *	the number of bytes read.
   */
  static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
  {
  	unsigned int result;
  	unsigned char byte;
  	int shift, count;
  
  	result = 0;
  	shift = 0;
  	count = 0;
  
  	while (1) {
  		byte = __raw_readb(addr);
  		addr++;
  		count++;
  
  		result |= (byte & 0x7f) << shift;
  		shift += 7;
  
  		if (!(byte & 0x80))
  			break;
  	}
  
  	*ret = result;
  
  	return count;
  }
  
  /**
   *	dwarf_read_leb128 - read signed LEB128 data
   *	@addr: the address of the LEB128 encoded data
   *	@ret: address to store the result
   *
   *	Decode signed LEB128 data. The algorithm is taken from Appendix
   *	C of the DWARF 3 spec. Return the number of bytes read.
   */
  static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
  {
  	unsigned char byte;
  	int result, shift;
  	int num_bits;
  	int count;
  
  	result = 0;
  	shift = 0;
  	count = 0;
  
  	while (1) {
  		byte = __raw_readb(addr);
  		addr++;
  		result |= (byte & 0x7f) << shift;
  		shift += 7;
  		count++;
  
  		if (!(byte & 0x80))
  			break;
  	}
  
  	/* The number of bits in a signed integer. */
  	num_bits = 8 * sizeof(result);
  
  	if ((shift < num_bits) && (byte & 0x40))
  		result |= (-1 << shift);
  
  	*ret = result;
  
  	return count;
  }
  
  /**
   *	dwarf_read_encoded_value - return the decoded value at @addr
   *	@addr: the address of the encoded value
   *	@val: where to write the decoded value
   *	@encoding: the encoding with which we can decode @addr
   *
   *	GCC emits encoded address in the .eh_frame FDE entries. Decode
   *	the value at @addr using @encoding. The decoded value is written
   *	to @val and the number of bytes read is returned.
   */
  static int dwarf_read_encoded_value(char *addr, unsigned long *val,
  				    char encoding)
  {
  	unsigned long decoded_addr = 0;
  	int count = 0;
  
  	switch (encoding & 0x70) {
  	case DW_EH_PE_absptr:
  		break;
  	case DW_EH_PE_pcrel:
  		decoded_addr = (unsigned long)addr;
  		break;
  	default:
  		pr_debug("encoding=0x%x
  ", (encoding & 0x70));
b344e24a8   Matt Fleming   sh: unwinder: Int...
236
  		UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
237
238
239
240
241
242
243
244
245
  	}
  
  	if ((encoding & 0x07) == 0x00)
  		encoding |= DW_EH_PE_udata4;
  
  	switch (encoding & 0x0f) {
  	case DW_EH_PE_sdata4:
  	case DW_EH_PE_udata4:
  		count += 4;
3497447f1   Paul Mundt   sh: unwinder: Fix...
246
  		decoded_addr += get_unaligned((u32 *)addr);
bd353861c   Matt Fleming   sh: dwarf unwinde...
247
248
249
250
251
  		__raw_writel(decoded_addr, val);
  		break;
  	default:
  		pr_debug("encoding=0x%x
  ", encoding);
b344e24a8   Matt Fleming   sh: unwinder: Int...
252
  		UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  	}
  
  	return count;
  }
  
  /**
   *	dwarf_entry_len - return the length of an FDE or CIE
   *	@addr: the address of the entry
   *	@len: the length of the entry
   *
   *	Read the initial_length field of the entry and store the size of
   *	the entry in @len. We return the number of bytes read. Return a
   *	count of 0 on error.
   */
  static inline int dwarf_entry_len(char *addr, unsigned long *len)
  {
  	u32 initial_len;
  	int count;
3497447f1   Paul Mundt   sh: unwinder: Fix...
271
  	initial_len = get_unaligned((u32 *)addr);
bd353861c   Matt Fleming   sh: dwarf unwinde...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  	count = 4;
  
  	/*
  	 * An initial length field value in the range DW_LEN_EXT_LO -
  	 * DW_LEN_EXT_HI indicates an extension, and should not be
  	 * interpreted as a length. The only extension that we currently
  	 * understand is the use of DWARF64 addresses.
  	 */
  	if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
  		/*
  		 * The 64-bit length field immediately follows the
  		 * compulsory 32-bit length field.
  		 */
  		if (initial_len == DW_EXT_DWARF64) {
3497447f1   Paul Mundt   sh: unwinder: Fix...
286
  			*len = get_unaligned((u64 *)addr + 4);
bd353861c   Matt Fleming   sh: dwarf unwinde...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  			count = 12;
  		} else {
  			printk(KERN_WARNING "Unknown DWARF extension
  ");
  			count = 0;
  		}
  	} else
  		*len = initial_len;
  
  	return count;
  }
  
  /**
   *	dwarf_lookup_cie - locate the cie
   *	@cie_ptr: pointer to help with lookup
   */
  static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
305
306
  	struct rb_node **rb_node = &cie_root.rb_node;
  	struct dwarf_cie *cie = NULL;
bd353861c   Matt Fleming   sh: dwarf unwinde...
307
308
309
310
311
312
313
314
315
316
317
318
  	unsigned long flags;
  
  	spin_lock_irqsave(&dwarf_cie_lock, flags);
  
  	/*
  	 * We've cached the last CIE we looked up because chances are
  	 * that the FDE wants this CIE.
  	 */
  	if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
  		cie = cached_cie;
  		goto out;
  	}
858918b77   Matt Fleming   sh: Optimise FDE/...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  	while (*rb_node) {
  		struct dwarf_cie *cie_tmp;
  
  		cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
  		BUG_ON(!cie_tmp);
  
  		if (cie_ptr == cie_tmp->cie_pointer) {
  			cie = cie_tmp;
  			cached_cie = cie_tmp;
  			goto out;
  		} else {
  			if (cie_ptr < cie_tmp->cie_pointer)
  				rb_node = &(*rb_node)->rb_left;
  			else
  				rb_node = &(*rb_node)->rb_right;
bd353861c   Matt Fleming   sh: dwarf unwinde...
334
335
  		}
  	}
bd353861c   Matt Fleming   sh: dwarf unwinde...
336
337
338
339
340
341
342
343
344
345
346
  out:
  	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  	return cie;
  }
  
  /**
   *	dwarf_lookup_fde - locate the FDE that covers pc
   *	@pc: the program counter
   */
  struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
347
348
  	struct rb_node **rb_node = &fde_root.rb_node;
  	struct dwarf_fde *fde = NULL;
bd353861c   Matt Fleming   sh: dwarf unwinde...
349
  	unsigned long flags;
bd353861c   Matt Fleming   sh: dwarf unwinde...
350
351
  
  	spin_lock_irqsave(&dwarf_fde_lock, flags);
97f361e24   Paul Mundt   sh: unwinder: Mov...
352

858918b77   Matt Fleming   sh: Optimise FDE/...
353
354
355
  	while (*rb_node) {
  		struct dwarf_fde *fde_tmp;
  		unsigned long tmp_start, tmp_end;
bd353861c   Matt Fleming   sh: dwarf unwinde...
356

858918b77   Matt Fleming   sh: Optimise FDE/...
357
358
  		fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
  		BUG_ON(!fde_tmp);
bd353861c   Matt Fleming   sh: dwarf unwinde...
359

858918b77   Matt Fleming   sh: Optimise FDE/...
360
361
  		tmp_start = fde_tmp->initial_location;
  		tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
bd353861c   Matt Fleming   sh: dwarf unwinde...
362

858918b77   Matt Fleming   sh: Optimise FDE/...
363
364
365
366
367
368
369
370
371
372
  		if (pc < tmp_start) {
  			rb_node = &(*rb_node)->rb_left;
  		} else {
  			if (pc < tmp_end) {
  				fde = fde_tmp;
  				goto out;
  			} else
  				rb_node = &(*rb_node)->rb_right;
  		}
  	}
bd353861c   Matt Fleming   sh: dwarf unwinde...
373

858918b77   Matt Fleming   sh: Optimise FDE/...
374
  out:
bd353861c   Matt Fleming   sh: dwarf unwinde...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  
  	return fde;
  }
  
  /**
   *	dwarf_cfa_execute_insns - execute instructions to calculate a CFA
   *	@insn_start: address of the first instruction
   *	@insn_end: address of the last instruction
   *	@cie: the CIE for this function
   *	@fde: the FDE for this function
   *	@frame: the instructions calculate the CFA for this frame
   *	@pc: the program counter of the address we're interested in
   *
   *	Execute the Call Frame instruction sequence starting at
   *	@insn_start and ending at @insn_end. The instructions describe
   *	how to calculate the Canonical Frame Address of a stackframe.
   *	Store the results in @frame.
   */
  static int dwarf_cfa_execute_insns(unsigned char *insn_start,
  				   unsigned char *insn_end,
  				   struct dwarf_cie *cie,
  				   struct dwarf_fde *fde,
  				   struct dwarf_frame *frame,
b955873bf   Matt Fleming   sh: Try again at ...
399
  				   unsigned long pc)
bd353861c   Matt Fleming   sh: dwarf unwinde...
400
401
402
403
  {
  	unsigned char insn;
  	unsigned char *current_insn;
  	unsigned int count, delta, reg, expr_len, offset;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
404
  	struct dwarf_reg *regp;
bd353861c   Matt Fleming   sh: dwarf unwinde...
405
406
  
  	current_insn = insn_start;
b955873bf   Matt Fleming   sh: Try again at ...
407
  	while (current_insn < insn_end && frame->pc <= pc) {
bd353861c   Matt Fleming   sh: dwarf unwinde...
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  		insn = __raw_readb(current_insn++);
  
  		/*
  		 * Firstly, handle the opcodes that embed their operands
  		 * in the instructions.
  		 */
  		switch (DW_CFA_opcode(insn)) {
  		case DW_CFA_advance_loc:
  			delta = DW_CFA_operand(insn);
  			delta *= cie->code_alignment_factor;
  			frame->pc += delta;
  			continue;
  			/* NOTREACHED */
  		case DW_CFA_offset:
  			reg = DW_CFA_operand(insn);
  			count = dwarf_read_uleb128(current_insn, &offset);
  			current_insn += count;
  			offset *= cie->data_alignment_factor;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
426
427
428
  			regp = dwarf_frame_alloc_reg(frame, reg);
  			regp->addr = offset;
  			regp->flags |= DWARF_REG_OFFSET;
bd353861c   Matt Fleming   sh: dwarf unwinde...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
  			continue;
  			/* NOTREACHED */
  		case DW_CFA_restore:
  			reg = DW_CFA_operand(insn);
  			continue;
  			/* NOTREACHED */
  		}
  
  		/*
  		 * Secondly, handle the opcodes that don't embed their
  		 * operands in the instruction.
  		 */
  		switch (insn) {
  		case DW_CFA_nop:
  			continue;
  		case DW_CFA_advance_loc1:
  			delta = *current_insn++;
  			frame->pc += delta * cie->code_alignment_factor;
  			break;
  		case DW_CFA_advance_loc2:
3497447f1   Paul Mundt   sh: unwinder: Fix...
449
  			delta = get_unaligned((u16 *)current_insn);
bd353861c   Matt Fleming   sh: dwarf unwinde...
450
451
452
453
  			current_insn += 2;
  			frame->pc += delta * cie->code_alignment_factor;
  			break;
  		case DW_CFA_advance_loc4:
3497447f1   Paul Mundt   sh: unwinder: Fix...
454
  			delta = get_unaligned((u32 *)current_insn);
bd353861c   Matt Fleming   sh: dwarf unwinde...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  			current_insn += 4;
  			frame->pc += delta * cie->code_alignment_factor;
  			break;
  		case DW_CFA_offset_extended:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
  			count = dwarf_read_uleb128(current_insn, &offset);
  			current_insn += count;
  			offset *= cie->data_alignment_factor;
  			break;
  		case DW_CFA_restore_extended:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
  			break;
  		case DW_CFA_undefined:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
5580e9044   Matt Fleming   sh: Handle the DW...
472
473
  			regp = dwarf_frame_alloc_reg(frame, reg);
  			regp->flags |= DWARF_UNDEFINED;
bd353861c   Matt Fleming   sh: dwarf unwinde...
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  			break;
  		case DW_CFA_def_cfa:
  			count = dwarf_read_uleb128(current_insn,
  						   &frame->cfa_register);
  			current_insn += count;
  			count = dwarf_read_uleb128(current_insn,
  						   &frame->cfa_offset);
  			current_insn += count;
  
  			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  			break;
  		case DW_CFA_def_cfa_register:
  			count = dwarf_read_uleb128(current_insn,
  						   &frame->cfa_register);
  			current_insn += count;
  			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
  			break;
  		case DW_CFA_def_cfa_offset:
  			count = dwarf_read_uleb128(current_insn, &offset);
  			current_insn += count;
  			frame->cfa_offset = offset;
  			break;
  		case DW_CFA_def_cfa_expression:
  			count = dwarf_read_uleb128(current_insn, &expr_len);
  			current_insn += count;
  
  			frame->cfa_expr = current_insn;
  			frame->cfa_expr_len = expr_len;
  			current_insn += expr_len;
  
  			frame->flags |= DWARF_FRAME_CFA_REG_EXP;
  			break;
  		case DW_CFA_offset_extended_sf:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
  			count = dwarf_read_leb128(current_insn, &offset);
  			current_insn += count;
  			offset *= cie->data_alignment_factor;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
512
513
514
  			regp = dwarf_frame_alloc_reg(frame, reg);
  			regp->flags |= DWARF_REG_OFFSET;
  			regp->addr = offset;
bd353861c   Matt Fleming   sh: dwarf unwinde...
515
516
517
518
519
520
  			break;
  		case DW_CFA_val_offset:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
  			count = dwarf_read_leb128(current_insn, &offset);
  			offset *= cie->data_alignment_factor;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
521
  			regp = dwarf_frame_alloc_reg(frame, reg);
97efbbd58   Matt Fleming   sh: unwinder: Set...
522
  			regp->flags |= DWARF_VAL_OFFSET;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
523
  			regp->addr = offset;
bd353861c   Matt Fleming   sh: dwarf unwinde...
524
  			break;
cd7246f0e   Matt Fleming   sh: Add support f...
525
526
527
528
529
530
531
532
533
  		case DW_CFA_GNU_args_size:
  			count = dwarf_read_uleb128(current_insn, &offset);
  			current_insn += count;
  			break;
  		case DW_CFA_GNU_negative_offset_extended:
  			count = dwarf_read_uleb128(current_insn, &reg);
  			current_insn += count;
  			count = dwarf_read_uleb128(current_insn, &offset);
  			offset *= cie->data_alignment_factor;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
534
535
536
537
  
  			regp = dwarf_frame_alloc_reg(frame, reg);
  			regp->flags |= DWARF_REG_OFFSET;
  			regp->addr = -offset;
cd7246f0e   Matt Fleming   sh: Add support f...
538
  			break;
bd353861c   Matt Fleming   sh: dwarf unwinde...
539
540
541
  		default:
  			pr_debug("unhandled DWARF instruction 0x%x
  ", insn);
b344e24a8   Matt Fleming   sh: unwinder: Int...
542
  			UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
543
544
545
546
547
548
549
550
  			break;
  		}
  	}
  
  	return 0;
  }
  
  /**
ed4fe7f48   Matt Fleming   sh: Fix memory le...
551
552
553
554
555
556
557
558
   *	dwarf_free_frame - free the memory allocated for @frame
   *	@frame: the frame to free
   */
  void dwarf_free_frame(struct dwarf_frame *frame)
  {
  	dwarf_frame_free_regs(frame);
  	mempool_free(frame, dwarf_frame_pool);
  }
944a34386   Matt Fleming   sh: Don't continu...
559
  extern void ret_from_irq(void);
ed4fe7f48   Matt Fleming   sh: Fix memory le...
560
  /**
c2d474d6f   Matt Fleming   sh: Remove any re...
561
562
   *	dwarf_unwind_stack - unwind the stack
   *
bd353861c   Matt Fleming   sh: dwarf unwinde...
563
564
565
566
567
568
569
   *	@pc: address of the function to unwind
   *	@prev: struct dwarf_frame of the previous stackframe on the callstack
   *
   *	Return a struct dwarf_frame representing the most recent frame
   *	on the callstack. Each of the lower (older) stack frames are
   *	linked via the "prev" member.
   */
858918b77   Matt Fleming   sh: Optimise FDE/...
570
571
  struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
  				       struct dwarf_frame *prev)
bd353861c   Matt Fleming   sh: dwarf unwinde...
572
573
574
575
  {
  	struct dwarf_frame *frame;
  	struct dwarf_cie *cie;
  	struct dwarf_fde *fde;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
576
  	struct dwarf_reg *reg;
bd353861c   Matt Fleming   sh: dwarf unwinde...
577
  	unsigned long addr;
bd353861c   Matt Fleming   sh: dwarf unwinde...
578
579
  
  	/*
8a37f5205   Paul Mundt   sh: handle early ...
580
581
582
583
584
585
586
  	 * If we've been called in to before initialization has
  	 * completed, bail out immediately.
  	 */
  	if (!dwarf_unwinder_ready)
  		return NULL;
  
  	/*
c2d474d6f   Matt Fleming   sh: Remove any re...
587
588
589
  	 * If we're starting at the top of the stack we need get the
  	 * contents of a physical register to get the CFA in order to
  	 * begin the virtual unwinding of the stack.
bd353861c   Matt Fleming   sh: dwarf unwinde...
590
  	 *
f82646677   Matt Fleming   sh: Delete DWARF_...
591
592
  	 * NOTE: the return address is guaranteed to be setup by the
  	 * time this function makes its first function call.
bd353861c   Matt Fleming   sh: dwarf unwinde...
593
  	 */
421b54111   Paul Mundt   sh: unwinder: Fix...
594
  	if (!pc || !prev)
b955873bf   Matt Fleming   sh: Try again at ...
595
  		pc = (unsigned long)current_text_addr();
bd353861c   Matt Fleming   sh: dwarf unwinde...
596

60339fad5   Matt Fleming   sh: Check for ret...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  	/*
  	 * If our stack has been patched by the function graph tracer
  	 * then we might see the address of return_to_handler() where we
  	 * expected to find the real return address.
  	 */
  	if (pc == (unsigned long)&return_to_handler) {
  		int index = current->curr_ret_stack;
  
  		/*
  		 * We currently have no way of tracking how many
  		 * return_to_handler()'s we've seen. If there is more
  		 * than one patched return address on our stack,
  		 * complain loudly.
  		 */
  		WARN_ON(index > 0);
  
  		pc = current->ret_stack[index].ret;
  	}
  #endif
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
617
618
619
620
  	frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
  	if (!frame) {
  		printk(KERN_ERR "Unable to allocate a dwarf frame
  ");
b344e24a8   Matt Fleming   sh: unwinder: Int...
621
  		UNWINDER_BUG();
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
622
  	}
bd353861c   Matt Fleming   sh: dwarf unwinde...
623

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
624
625
  	INIT_LIST_HEAD(&frame->reg_list);
  	frame->flags = 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
626
  	frame->prev = prev;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
627
  	frame->return_addr = 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
628
629
630
631
  
  	fde = dwarf_lookup_fde(pc);
  	if (!fde) {
  		/*
c2d474d6f   Matt Fleming   sh: Remove any re...
632
633
  		 * This is our normal exit path. There are two reasons
  		 * why we might exit here,
bd353861c   Matt Fleming   sh: dwarf unwinde...
634
635
636
637
638
639
640
641
642
643
644
645
  		 *
  		 *	a) pc has no asscociated DWARF frame info and so
  		 *	we don't know how to unwind this frame. This is
  		 *	usually the case when we're trying to unwind a
  		 *	frame that was called from some assembly code
  		 *	that has no DWARF info, e.g. syscalls.
  		 *
  		 *	b) the DEBUG info for pc is bogus. There's
  		 *	really no way to distinguish this case from the
  		 *	case above, which sucks because we could print a
  		 *	warning here.
  		 */
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
646
  		goto bail;
bd353861c   Matt Fleming   sh: dwarf unwinde...
647
648
649
650
651
652
653
654
  	}
  
  	cie = dwarf_lookup_cie(fde->cie_pointer);
  
  	frame->pc = fde->initial_location;
  
  	/* CIE initial instructions */
  	dwarf_cfa_execute_insns(cie->initial_instructions,
f82646677   Matt Fleming   sh: Delete DWARF_...
655
  				cie->instructions_end, cie, fde,
b955873bf   Matt Fleming   sh: Try again at ...
656
  				frame, pc);
bd353861c   Matt Fleming   sh: dwarf unwinde...
657
658
659
  
  	/* FDE instructions */
  	dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
b955873bf   Matt Fleming   sh: Try again at ...
660
  				fde, frame, pc);
bd353861c   Matt Fleming   sh: dwarf unwinde...
661
662
663
664
665
  
  	/* Calculate the CFA */
  	switch (frame->flags) {
  	case DWARF_FRAME_CFA_REG_OFFSET:
  		if (prev) {
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
666
  			reg = dwarf_frame_reg(prev, frame->cfa_register);
b344e24a8   Matt Fleming   sh: unwinder: Int...
667
668
  			UNWINDER_BUG_ON(!reg);
  			UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
bd353861c   Matt Fleming   sh: dwarf unwinde...
669

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
670
  			addr = prev->cfa + reg->addr;
bd353861c   Matt Fleming   sh: dwarf unwinde...
671
672
673
674
  			frame->cfa = __raw_readl(addr);
  
  		} else {
  			/*
c2d474d6f   Matt Fleming   sh: Remove any re...
675
676
677
678
  			 * Again, we're starting from the top of the
  			 * stack. We need to physically read
  			 * the contents of a register in order to get
  			 * the Canonical Frame Address for this
bd353861c   Matt Fleming   sh: dwarf unwinde...
679
680
681
682
683
684
685
686
  			 * function.
  			 */
  			frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
  		}
  
  		frame->cfa += frame->cfa_offset;
  		break;
  	default:
b344e24a8   Matt Fleming   sh: unwinder: Int...
687
  		UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
688
  	}
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
689
  	reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
5580e9044   Matt Fleming   sh: Handle the DW...
690
691
692
693
694
695
696
697
  
  	/*
  	 * If we haven't seen the return address register or the return
  	 * address column is undefined then we must assume that this is
  	 * the end of the callstack.
  	 */
  	if (!reg || reg->flags == DWARF_UNDEFINED)
  		goto bail;
b344e24a8   Matt Fleming   sh: unwinder: Int...
698
  	UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
bd353861c   Matt Fleming   sh: dwarf unwinde...
699

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
700
  	addr = frame->cfa + reg->addr;
bd353861c   Matt Fleming   sh: dwarf unwinde...
701
  	frame->return_addr = __raw_readl(addr);
944a34386   Matt Fleming   sh: Don't continu...
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
  	/*
  	 * Ah, the joys of unwinding through interrupts.
  	 *
  	 * Interrupts are tricky - the DWARF info needs to be _really_
  	 * accurate and unfortunately I'm seeing a lot of bogus DWARF
  	 * info. For example, I've seen interrupts occur in epilogues
  	 * just after the frame pointer (r14) had been restored. The
  	 * problem was that the DWARF info claimed that the CFA could be
  	 * reached by using the value of the frame pointer before it was
  	 * restored.
  	 *
  	 * So until the compiler can be trusted to produce reliable
  	 * DWARF info when it really matters, let's stop unwinding once
  	 * we've calculated the function that was interrupted.
  	 */
  	if (prev && prev->pc == (unsigned long)ret_from_irq)
  		frame->return_addr = 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
719
  	return frame;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
720
721
  
  bail:
ed4fe7f48   Matt Fleming   sh: Fix memory le...
722
  	dwarf_free_frame(frame);
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
723
  	return NULL;
bd353861c   Matt Fleming   sh: dwarf unwinde...
724
725
726
  }
  
  static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
727
  			   unsigned char *end, struct module *mod)
bd353861c   Matt Fleming   sh: dwarf unwinde...
728
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
729
  	struct rb_node **rb_node = &cie_root.rb_node;
4e1a25940   Paul Mundt   sh: Silence unint...
730
  	struct rb_node *parent = *rb_node;
bd353861c   Matt Fleming   sh: dwarf unwinde...
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
  	struct dwarf_cie *cie;
  	unsigned long flags;
  	int count;
  
  	cie = kzalloc(sizeof(*cie), GFP_KERNEL);
  	if (!cie)
  		return -ENOMEM;
  
  	cie->length = len;
  
  	/*
  	 * Record the offset into the .eh_frame section
  	 * for this CIE. It allows this CIE to be
  	 * quickly and easily looked up from the
  	 * corresponding FDE.
  	 */
  	cie->cie_pointer = (unsigned long)entry;
  
  	cie->version = *(char *)p++;
b344e24a8   Matt Fleming   sh: unwinder: Int...
750
  	UNWINDER_BUG_ON(cie->version != 1);
bd353861c   Matt Fleming   sh: dwarf unwinde...
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  
  	cie->augmentation = p;
  	p += strlen(cie->augmentation) + 1;
  
  	count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
  	p += count;
  
  	count = dwarf_read_leb128(p, &cie->data_alignment_factor);
  	p += count;
  
  	/*
  	 * Which column in the rule table contains the
  	 * return address?
  	 */
  	if (cie->version == 1) {
  		cie->return_address_reg = __raw_readb(p);
  		p++;
  	} else {
  		count = dwarf_read_uleb128(p, &cie->return_address_reg);
  		p += count;
  	}
  
  	if (cie->augmentation[0] == 'z') {
  		unsigned int length, count;
  		cie->flags |= DWARF_CIE_Z_AUGMENTATION;
  
  		count = dwarf_read_uleb128(p, &length);
  		p += count;
b344e24a8   Matt Fleming   sh: unwinder: Int...
779
  		UNWINDER_BUG_ON((unsigned char *)p > end);
bd353861c   Matt Fleming   sh: dwarf unwinde...
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
  
  		cie->initial_instructions = p + length;
  		cie->augmentation++;
  	}
  
  	while (*cie->augmentation) {
  		/*
  		 * "L" indicates a byte showing how the
  		 * LSDA pointer is encoded. Skip it.
  		 */
  		if (*cie->augmentation == 'L') {
  			p++;
  			cie->augmentation++;
  		} else if (*cie->augmentation == 'R') {
  			/*
  			 * "R" indicates a byte showing
  			 * how FDE addresses are
  			 * encoded.
  			 */
  			cie->encoding = *(char *)p++;
  			cie->augmentation++;
  		} else if (*cie->augmentation == 'P') {
  			/*
  			 * "R" indicates a personality
  			 * routine in the CIE
  			 * augmentation.
  			 */
b344e24a8   Matt Fleming   sh: unwinder: Int...
807
  			UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
808
  		} else if (*cie->augmentation == 'S') {
b344e24a8   Matt Fleming   sh: unwinder: Int...
809
  			UNWINDER_BUG();
bd353861c   Matt Fleming   sh: dwarf unwinde...
810
811
812
813
814
815
  		} else {
  			/*
  			 * Unknown augmentation. Assume
  			 * 'z' augmentation.
  			 */
  			p = cie->initial_instructions;
b344e24a8   Matt Fleming   sh: unwinder: Int...
816
  			UNWINDER_BUG_ON(!p);
bd353861c   Matt Fleming   sh: dwarf unwinde...
817
818
819
820
821
822
823
824
825
  			break;
  		}
  	}
  
  	cie->initial_instructions = p;
  	cie->instructions_end = end;
  
  	/* Add to list */
  	spin_lock_irqsave(&dwarf_cie_lock, flags);
858918b77   Matt Fleming   sh: Optimise FDE/...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  
  	while (*rb_node) {
  		struct dwarf_cie *cie_tmp;
  
  		cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
  
  		parent = *rb_node;
  
  		if (cie->cie_pointer < cie_tmp->cie_pointer)
  			rb_node = &parent->rb_left;
  		else if (cie->cie_pointer >= cie_tmp->cie_pointer)
  			rb_node = &parent->rb_right;
  		else
  			WARN_ON(1);
  	}
  
  	rb_link_node(&cie->node, parent, rb_node);
  	rb_insert_color(&cie->node, &cie_root);
d8252d627   Paul Mundt   sh: fix up the dw...
844
  #ifdef CONFIG_MODULES
858918b77   Matt Fleming   sh: Optimise FDE/...
845
846
  	if (mod != NULL)
  		list_add_tail(&cie->link, &mod->arch.cie_list);
d8252d627   Paul Mundt   sh: fix up the dw...
847
  #endif
858918b77   Matt Fleming   sh: Optimise FDE/...
848

bd353861c   Matt Fleming   sh: dwarf unwinde...
849
850
851
852
853
854
  	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  
  	return 0;
  }
  
  static int dwarf_parse_fde(void *entry, u32 entry_type,
5480675dc   Matt Fleming   sh: Fix bug calcu...
855
  			   void *start, unsigned long len,
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
856
  			   unsigned char *end, struct module *mod)
bd353861c   Matt Fleming   sh: dwarf unwinde...
857
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
858
  	struct rb_node **rb_node = &fde_root.rb_node;
4e1a25940   Paul Mundt   sh: Silence unint...
859
  	struct rb_node *parent = *rb_node;
bd353861c   Matt Fleming   sh: dwarf unwinde...
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  	struct dwarf_fde *fde;
  	struct dwarf_cie *cie;
  	unsigned long flags;
  	int count;
  	void *p = start;
  
  	fde = kzalloc(sizeof(*fde), GFP_KERNEL);
  	if (!fde)
  		return -ENOMEM;
  
  	fde->length = len;
  
  	/*
  	 * In a .eh_frame section the CIE pointer is the
  	 * delta between the address within the FDE
  	 */
  	fde->cie_pointer = (unsigned long)(p - entry_type - 4);
  
  	cie = dwarf_lookup_cie(fde->cie_pointer);
  	fde->cie = cie;
  
  	if (cie->encoding)
  		count = dwarf_read_encoded_value(p, &fde->initial_location,
  						 cie->encoding);
  	else
  		count = dwarf_read_addr(p, &fde->initial_location);
  
  	p += count;
  
  	if (cie->encoding)
  		count = dwarf_read_encoded_value(p, &fde->address_range,
  						 cie->encoding & 0x0f);
  	else
  		count = dwarf_read_addr(p, &fde->address_range);
  
  	p += count;
  
  	if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
  		unsigned int length;
  		count = dwarf_read_uleb128(p, &length);
  		p += count + length;
  	}
  
  	/* Call frame instructions. */
  	fde->instructions = p;
5480675dc   Matt Fleming   sh: Fix bug calcu...
905
  	fde->end = end;
bd353861c   Matt Fleming   sh: dwarf unwinde...
906
907
908
  
  	/* Add to list. */
  	spin_lock_irqsave(&dwarf_fde_lock, flags);
858918b77   Matt Fleming   sh: Optimise FDE/...
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
  
  	while (*rb_node) {
  		struct dwarf_fde *fde_tmp;
  		unsigned long tmp_start, tmp_end;
  		unsigned long start, end;
  
  		fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
  
  		start = fde->initial_location;
  		end = fde->initial_location + fde->address_range;
  
  		tmp_start = fde_tmp->initial_location;
  		tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
  
  		parent = *rb_node;
  
  		if (start < tmp_start)
  			rb_node = &parent->rb_left;
  		else if (start >= tmp_end)
  			rb_node = &parent->rb_right;
  		else
  			WARN_ON(1);
  	}
  
  	rb_link_node(&fde->node, parent, rb_node);
  	rb_insert_color(&fde->node, &fde_root);
d8252d627   Paul Mundt   sh: fix up the dw...
935
  #ifdef CONFIG_MODULES
858918b77   Matt Fleming   sh: Optimise FDE/...
936
937
  	if (mod != NULL)
  		list_add_tail(&fde->link, &mod->arch.fde_list);
d8252d627   Paul Mundt   sh: fix up the dw...
938
  #endif
858918b77   Matt Fleming   sh: Optimise FDE/...
939

bd353861c   Matt Fleming   sh: dwarf unwinde...
940
941
942
943
  	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  
  	return 0;
  }
b344e24a8   Matt Fleming   sh: unwinder: Int...
944
945
  static void dwarf_unwinder_dump(struct task_struct *task,
  				struct pt_regs *regs,
bd353861c   Matt Fleming   sh: dwarf unwinde...
946
  				unsigned long *sp,
b344e24a8   Matt Fleming   sh: unwinder: Int...
947
948
  				const struct stacktrace_ops *ops,
  				void *data)
bd353861c   Matt Fleming   sh: dwarf unwinde...
949
  {
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
950
951
952
953
954
  	struct dwarf_frame *frame, *_frame;
  	unsigned long return_addr;
  
  	_frame = NULL;
  	return_addr = 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
955

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
956
957
  	while (1) {
  		frame = dwarf_unwind_stack(return_addr, _frame);
ed4fe7f48   Matt Fleming   sh: Fix memory le...
958
959
  		if (_frame)
  			dwarf_free_frame(_frame);
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
960
961
962
963
964
  
  		_frame = frame;
  
  		if (!frame || !frame->return_addr)
  			break;
bd353861c   Matt Fleming   sh: dwarf unwinde...
965

fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
966
967
  		return_addr = frame->return_addr;
  		ops->address(data, return_addr, 1);
bd353861c   Matt Fleming   sh: dwarf unwinde...
968
  	}
ed4fe7f48   Matt Fleming   sh: Fix memory le...
969
970
971
  
  	if (frame)
  		dwarf_free_frame(frame);
bd353861c   Matt Fleming   sh: dwarf unwinde...
972
973
974
975
976
977
978
979
980
981
  }
  
  static struct unwinder dwarf_unwinder = {
  	.name = "dwarf-unwinder",
  	.dump = dwarf_unwinder_dump,
  	.rating = 150,
  };
  
  static void dwarf_unwinder_cleanup(void)
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
982
983
  	struct rb_node **fde_rb_node = &fde_root.rb_node;
  	struct rb_node **cie_rb_node = &cie_root.rb_node;
bd353861c   Matt Fleming   sh: dwarf unwinde...
984
985
986
987
988
989
  
  	/*
  	 * Deallocate all the memory allocated for the DWARF unwinder.
  	 * Traverse all the FDE/CIE lists and remove and free all the
  	 * memory associated with those data structures.
  	 */
858918b77   Matt Fleming   sh: Optimise FDE/...
990
991
  	while (*fde_rb_node) {
  		struct dwarf_fde *fde;
bd353861c   Matt Fleming   sh: dwarf unwinde...
992

858918b77   Matt Fleming   sh: Optimise FDE/...
993
994
  		fde = rb_entry(*fde_rb_node, struct dwarf_fde, node);
  		rb_erase(*fde_rb_node, &fde_root);
bd353861c   Matt Fleming   sh: dwarf unwinde...
995
  		kfree(fde);
858918b77   Matt Fleming   sh: Optimise FDE/...
996
997
998
999
1000
1001
1002
1003
1004
  	}
  
  	while (*cie_rb_node) {
  		struct dwarf_cie *cie;
  
  		cie = rb_entry(*cie_rb_node, struct dwarf_cie, node);
  		rb_erase(*cie_rb_node, &cie_root);
  		kfree(cie);
  	}
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
1005
1006
1007
  
  	kmem_cache_destroy(dwarf_reg_cachep);
  	kmem_cache_destroy(dwarf_frame_cachep);
bd353861c   Matt Fleming   sh: dwarf unwinde...
1008
1009
1010
  }
  
  /**
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1011
1012
1013
1014
   *	dwarf_parse_section - parse DWARF section
   *	@eh_frame_start: start address of the .eh_frame section
   *	@eh_frame_end: end address of the .eh_frame section
   *	@mod: the kernel module containing the .eh_frame section
bd353861c   Matt Fleming   sh: dwarf unwinde...
1015
   *
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1016
   *	Parse the information in a .eh_frame section.
bd353861c   Matt Fleming   sh: dwarf unwinde...
1017
   */
5a3abba77   Paul Mundt   sh: Tidy up the d...
1018
1019
  static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
  			       struct module *mod)
bd353861c   Matt Fleming   sh: dwarf unwinde...
1020
1021
1022
  {
  	u32 entry_type;
  	void *p, *entry;
2f6dafc5f   Paul Mundt   sh: unwinder: Fix...
1023
  	int count, err = 0;
eca28e376   Paul Mundt   sh: Fix up uninit...
1024
  	unsigned long len = 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1025
1026
  	unsigned int c_entries, f_entries;
  	unsigned char *end;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1027
1028
1029
  
  	c_entries = 0;
  	f_entries = 0;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1030
  	entry = eh_frame_start;
fb3f3e7fc   Matt Fleming   sh: unwinder: Fix...
1031

a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1032
  	while ((char *)entry < eh_frame_end) {
bd353861c   Matt Fleming   sh: dwarf unwinde...
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  		p = entry;
  
  		count = dwarf_entry_len(p, &len);
  		if (count == 0) {
  			/*
  			 * We read a bogus length field value. There is
  			 * nothing we can do here apart from disabling
  			 * the DWARF unwinder. We can't even skip this
  			 * entry and move to the next one because 'len'
  			 * tells us where our next entry is.
  			 */
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1044
  			err = -EINVAL;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1045
1046
1047
1048
1049
1050
  			goto out;
  		} else
  			p += count;
  
  		/* initial length does not include itself */
  		end = p + len;
3497447f1   Paul Mundt   sh: unwinder: Fix...
1051
  		entry_type = get_unaligned((u32 *)p);
bd353861c   Matt Fleming   sh: dwarf unwinde...
1052
1053
1054
  		p += 4;
  
  		if (entry_type == DW_EH_FRAME_CIE) {
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1055
  			err = dwarf_parse_cie(entry, p, len, end, mod);
bd353861c   Matt Fleming   sh: dwarf unwinde...
1056
1057
1058
1059
1060
  			if (err < 0)
  				goto out;
  			else
  				c_entries++;
  		} else {
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1061
1062
  			err = dwarf_parse_fde(entry, entry_type, p, len,
  					      end, mod);
bd353861c   Matt Fleming   sh: dwarf unwinde...
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
  			if (err < 0)
  				goto out;
  			else
  				f_entries++;
  		}
  
  		entry = (char *)entry + len + 4;
  	}
  
  	printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs
  ",
  	       c_entries, f_entries);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1075
1076
1077
1078
1079
  	return 0;
  
  out:
  	return err;
  }
5a3abba77   Paul Mundt   sh: Tidy up the d...
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  #ifdef CONFIG_MODULES
  int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  			  struct module *me)
  {
  	unsigned int i, err;
  	unsigned long start, end;
  	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  
  	start = end = 0;
  
  	for (i = 1; i < hdr->e_shnum; i++) {
  		/* Alloc bit cleared means "ignore it." */
  		if ((sechdrs[i].sh_flags & SHF_ALLOC)
  		    && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
  			start = sechdrs[i].sh_addr;
  			end = start + sechdrs[i].sh_size;
  			break;
  		}
  	}
  
  	/* Did we find the .eh_frame section? */
  	if (i != hdr->e_shnum) {
858918b77   Matt Fleming   sh: Optimise FDE/...
1102
1103
  		INIT_LIST_HEAD(&me->arch.cie_list);
  		INIT_LIST_HEAD(&me->arch.fde_list);
5a3abba77   Paul Mundt   sh: Tidy up the d...
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
  		err = dwarf_parse_section((char *)start, (char *)end, me);
  		if (err) {
  			printk(KERN_WARNING "%s: failed to parse DWARF info
  ",
  			       me->name);
  			return err;
  		}
  	}
  
  	return 0;
  }
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1115
  /**
5a3abba77   Paul Mundt   sh: Tidy up the d...
1116
   *	module_dwarf_cleanup - remove FDE/CIEs associated with @mod
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1117
1118
1119
1120
1121
   *	@mod: the module that is being unloaded
   *
   *	Remove any FDEs and CIEs from the global lists that came from
   *	@mod's .eh_frame section because @mod is being unloaded.
   */
5a3abba77   Paul Mundt   sh: Tidy up the d...
1122
  void module_dwarf_cleanup(struct module *mod)
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1123
  {
858918b77   Matt Fleming   sh: Optimise FDE/...
1124
1125
  	struct dwarf_fde *fde, *ftmp;
  	struct dwarf_cie *cie, *ctmp;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1126
1127
1128
  	unsigned long flags;
  
  	spin_lock_irqsave(&dwarf_cie_lock, flags);
858918b77   Matt Fleming   sh: Optimise FDE/...
1129
  	list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1130
  		list_del(&cie->link);
858918b77   Matt Fleming   sh: Optimise FDE/...
1131
  		rb_erase(&cie->node, &cie_root);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1132
  		kfree(cie);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1133
1134
1135
1136
1137
  	}
  
  	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
  
  	spin_lock_irqsave(&dwarf_fde_lock, flags);
858918b77   Matt Fleming   sh: Optimise FDE/...
1138
  	list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1139
  		list_del(&fde->link);
858918b77   Matt Fleming   sh: Optimise FDE/...
1140
  		rb_erase(&fde->node, &fde_root);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1141
  		kfree(fde);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1142
1143
1144
1145
  	}
  
  	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
  }
5a3abba77   Paul Mundt   sh: Tidy up the d...
1146
  #endif /* CONFIG_MODULES */
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  
  /**
   *	dwarf_unwinder_init - initialise the dwarf unwinder
   *
   *	Build the data structures describing the .dwarf_frame section to
   *	make it easier to lookup CIE and FDE entries. Because the
   *	.eh_frame section is packed as tightly as possible it is not
   *	easy to lookup the FDE for a given PC, so we build a list of FDE
   *	and CIE entries that make it easier.
   */
  static int __init dwarf_unwinder_init(void)
  {
8a37f5205   Paul Mundt   sh: handle early ...
1159
  	int err = -ENOMEM;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1160
1161
  
  	dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
8ec006c58   Paul Mundt   Merge branch 'sh/...
1162
1163
  			sizeof(struct dwarf_frame), 0,
  			SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1164
  	dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
8ec006c58   Paul Mundt   Merge branch 'sh/...
1165
1166
  			sizeof(struct dwarf_reg), 0,
  			SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1167
1168
1169
1170
1171
  
  	dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
  					  mempool_alloc_slab,
  					  mempool_free_slab,
  					  dwarf_frame_cachep);
8a37f5205   Paul Mundt   sh: handle early ...
1172
1173
  	if (!dwarf_frame_pool)
  		goto out;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1174
1175
1176
1177
1178
  
  	dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
  					 mempool_alloc_slab,
  					 mempool_free_slab,
  					 dwarf_reg_cachep);
8a37f5205   Paul Mundt   sh: handle early ...
1179
1180
  	if (!dwarf_reg_pool)
  		goto out;
a6a2f2ad6   Matt Fleming   sh: Teach the DWA...
1181
1182
1183
1184
  
  	err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
  	if (err)
  		goto out;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1185
1186
1187
  	err = unwinder_register(&dwarf_unwinder);
  	if (err)
  		goto out;
8a37f5205   Paul Mundt   sh: handle early ...
1188
  	dwarf_unwinder_ready = 1;
97f361e24   Paul Mundt   sh: unwinder: Mov...
1189
  	return 0;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1190
1191
1192
1193
1194
  
  out:
  	printk(KERN_ERR "Failed to initialise DWARF unwinder: %d
  ", err);
  	dwarf_unwinder_cleanup();
8a37f5205   Paul Mundt   sh: handle early ...
1195
  	return err;
bd353861c   Matt Fleming   sh: dwarf unwinde...
1196
  }
97f361e24   Paul Mundt   sh: unwinder: Mov...
1197
  early_initcall(dwarf_unwinder_init);