xref: /openbmc/linux/arch/sh/kernel/dwarf.c (revision dc56367c)
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  *	- Registers with DWARF_VAL_OFFSET rules aren't handled properly.
15  */
16 
17 /* #define DEBUG */
18 #include <linux/kernel.h>
19 #include <linux/io.h>
20 #include <linux/list.h>
21 #include <linux/mempool.h>
22 #include <linux/mm.h>
23 #include <linux/elf.h>
24 #include <linux/ftrace.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <asm/dwarf.h>
28 #include <asm/unwinder.h>
29 #include <asm/sections.h>
30 #include <asm/unaligned.h>
31 #include <asm/stacktrace.h>
32 
33 /* Reserve enough memory for two stack frames */
34 #define DWARF_FRAME_MIN_REQ	2
35 /* ... with 4 registers per frame. */
36 #define DWARF_REG_MIN_REQ	(DWARF_FRAME_MIN_REQ * 4)
37 
38 static struct kmem_cache *dwarf_frame_cachep;
39 static mempool_t *dwarf_frame_pool;
40 
41 static struct kmem_cache *dwarf_reg_cachep;
42 static mempool_t *dwarf_reg_pool;
43 
44 static struct rb_root cie_root;
45 static DEFINE_SPINLOCK(dwarf_cie_lock);
46 
47 static struct rb_root fde_root;
48 static DEFINE_SPINLOCK(dwarf_fde_lock);
49 
50 static struct dwarf_cie *cached_cie;
51 
52 static unsigned int dwarf_unwinder_ready;
53 
54 /**
55  *	dwarf_frame_alloc_reg - allocate memory for a DWARF register
56  *	@frame: the DWARF frame whose list of registers we insert on
57  *	@reg_num: the register number
58  *
59  *	Allocate space for, and initialise, a dwarf reg from
60  *	dwarf_reg_pool and insert it onto the (unsorted) linked-list of
61  *	dwarf registers for @frame.
62  *
63  *	Return the initialised DWARF reg.
64  */
65 static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,
66 					       unsigned int reg_num)
67 {
68 	struct dwarf_reg *reg;
69 
70 	reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);
71 	if (!reg) {
72 		printk(KERN_WARNING "Unable to allocate a DWARF register\n");
73 		/*
74 		 * Let's just bomb hard here, we have no way to
75 		 * gracefully recover.
76 		 */
77 		UNWINDER_BUG();
78 	}
79 
80 	reg->number = reg_num;
81 	reg->addr = 0;
82 	reg->flags = 0;
83 
84 	list_add(&reg->link, &frame->reg_list);
85 
86 	return reg;
87 }
88 
89 static void dwarf_frame_free_regs(struct dwarf_frame *frame)
90 {
91 	struct dwarf_reg *reg, *n;
92 
93 	list_for_each_entry_safe(reg, n, &frame->reg_list, link) {
94 		list_del(&reg->link);
95 		mempool_free(reg, dwarf_reg_pool);
96 	}
97 }
98 
99 /**
100  *	dwarf_frame_reg - return a DWARF register
101  *	@frame: the DWARF frame to search in for @reg_num
102  *	@reg_num: the register number to search for
103  *
104  *	Lookup and return the dwarf reg @reg_num for this frame. Return
105  *	NULL if @reg_num is an register invalid number.
106  */
107 static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,
108 					 unsigned int reg_num)
109 {
110 	struct dwarf_reg *reg;
111 
112 	list_for_each_entry(reg, &frame->reg_list, link) {
113 		if (reg->number == reg_num)
114 			return reg;
115 	}
116 
117 	return NULL;
118 }
119 
120 /**
121  *	dwarf_read_addr - read dwarf data
122  *	@src: source address of data
123  *	@dst: destination address to store the data to
124  *
125  *	Read 'n' bytes from @src, where 'n' is the size of an address on
126  *	the native machine. We return the number of bytes read, which
127  *	should always be 'n'. We also have to be careful when reading
128  *	from @src and writing to @dst, because they can be arbitrarily
129  *	aligned. Return 'n' - the number of bytes read.
130  */
131 static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst)
132 {
133 	u32 val = get_unaligned(src);
134 	put_unaligned(val, dst);
135 	return sizeof(unsigned long *);
136 }
137 
138 /**
139  *	dwarf_read_uleb128 - read unsigned LEB128 data
140  *	@addr: the address where the ULEB128 data is stored
141  *	@ret: address to store the result
142  *
143  *	Decode an unsigned LEB128 encoded datum. The algorithm is taken
144  *	from Appendix C of the DWARF 3 spec. For information on the
145  *	encodings refer to section "7.6 - Variable Length Data". Return
146  *	the number of bytes read.
147  */
148 static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret)
149 {
150 	unsigned int result;
151 	unsigned char byte;
152 	int shift, count;
153 
154 	result = 0;
155 	shift = 0;
156 	count = 0;
157 
158 	while (1) {
159 		byte = __raw_readb(addr);
160 		addr++;
161 		count++;
162 
163 		result |= (byte & 0x7f) << shift;
164 		shift += 7;
165 
166 		if (!(byte & 0x80))
167 			break;
168 	}
169 
170 	*ret = result;
171 
172 	return count;
173 }
174 
175 /**
176  *	dwarf_read_leb128 - read signed LEB128 data
177  *	@addr: the address of the LEB128 encoded data
178  *	@ret: address to store the result
179  *
180  *	Decode signed LEB128 data. The algorithm is taken from Appendix
181  *	C of the DWARF 3 spec. Return the number of bytes read.
182  */
183 static inline unsigned long dwarf_read_leb128(char *addr, int *ret)
184 {
185 	unsigned char byte;
186 	int result, shift;
187 	int num_bits;
188 	int count;
189 
190 	result = 0;
191 	shift = 0;
192 	count = 0;
193 
194 	while (1) {
195 		byte = __raw_readb(addr);
196 		addr++;
197 		result |= (byte & 0x7f) << shift;
198 		shift += 7;
199 		count++;
200 
201 		if (!(byte & 0x80))
202 			break;
203 	}
204 
205 	/* The number of bits in a signed integer. */
206 	num_bits = 8 * sizeof(result);
207 
208 	if ((shift < num_bits) && (byte & 0x40))
209 		result |= (-1 << shift);
210 
211 	*ret = result;
212 
213 	return count;
214 }
215 
216 /**
217  *	dwarf_read_encoded_value - return the decoded value at @addr
218  *	@addr: the address of the encoded value
219  *	@val: where to write the decoded value
220  *	@encoding: the encoding with which we can decode @addr
221  *
222  *	GCC emits encoded address in the .eh_frame FDE entries. Decode
223  *	the value at @addr using @encoding. The decoded value is written
224  *	to @val and the number of bytes read is returned.
225  */
226 static int dwarf_read_encoded_value(char *addr, unsigned long *val,
227 				    char encoding)
228 {
229 	unsigned long decoded_addr = 0;
230 	int count = 0;
231 
232 	switch (encoding & 0x70) {
233 	case DW_EH_PE_absptr:
234 		break;
235 	case DW_EH_PE_pcrel:
236 		decoded_addr = (unsigned long)addr;
237 		break;
238 	default:
239 		pr_debug("encoding=0x%x\n", (encoding & 0x70));
240 		UNWINDER_BUG();
241 	}
242 
243 	if ((encoding & 0x07) == 0x00)
244 		encoding |= DW_EH_PE_udata4;
245 
246 	switch (encoding & 0x0f) {
247 	case DW_EH_PE_sdata4:
248 	case DW_EH_PE_udata4:
249 		count += 4;
250 		decoded_addr += get_unaligned((u32 *)addr);
251 		__raw_writel(decoded_addr, val);
252 		break;
253 	default:
254 		pr_debug("encoding=0x%x\n", encoding);
255 		UNWINDER_BUG();
256 	}
257 
258 	return count;
259 }
260 
261 /**
262  *	dwarf_entry_len - return the length of an FDE or CIE
263  *	@addr: the address of the entry
264  *	@len: the length of the entry
265  *
266  *	Read the initial_length field of the entry and store the size of
267  *	the entry in @len. We return the number of bytes read. Return a
268  *	count of 0 on error.
269  */
270 static inline int dwarf_entry_len(char *addr, unsigned long *len)
271 {
272 	u32 initial_len;
273 	int count;
274 
275 	initial_len = get_unaligned((u32 *)addr);
276 	count = 4;
277 
278 	/*
279 	 * An initial length field value in the range DW_LEN_EXT_LO -
280 	 * DW_LEN_EXT_HI indicates an extension, and should not be
281 	 * interpreted as a length. The only extension that we currently
282 	 * understand is the use of DWARF64 addresses.
283 	 */
284 	if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) {
285 		/*
286 		 * The 64-bit length field immediately follows the
287 		 * compulsory 32-bit length field.
288 		 */
289 		if (initial_len == DW_EXT_DWARF64) {
290 			*len = get_unaligned((u64 *)addr + 4);
291 			count = 12;
292 		} else {
293 			printk(KERN_WARNING "Unknown DWARF extension\n");
294 			count = 0;
295 		}
296 	} else
297 		*len = initial_len;
298 
299 	return count;
300 }
301 
302 /**
303  *	dwarf_lookup_cie - locate the cie
304  *	@cie_ptr: pointer to help with lookup
305  */
306 static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)
307 {
308 	struct rb_node **rb_node = &cie_root.rb_node;
309 	struct dwarf_cie *cie = NULL;
310 	unsigned long flags;
311 
312 	spin_lock_irqsave(&dwarf_cie_lock, flags);
313 
314 	/*
315 	 * We've cached the last CIE we looked up because chances are
316 	 * that the FDE wants this CIE.
317 	 */
318 	if (cached_cie && cached_cie->cie_pointer == cie_ptr) {
319 		cie = cached_cie;
320 		goto out;
321 	}
322 
323 	while (*rb_node) {
324 		struct dwarf_cie *cie_tmp;
325 
326 		cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
327 		BUG_ON(!cie_tmp);
328 
329 		if (cie_ptr == cie_tmp->cie_pointer) {
330 			cie = cie_tmp;
331 			cached_cie = cie_tmp;
332 			goto out;
333 		} else {
334 			if (cie_ptr < cie_tmp->cie_pointer)
335 				rb_node = &(*rb_node)->rb_left;
336 			else
337 				rb_node = &(*rb_node)->rb_right;
338 		}
339 	}
340 
341 out:
342 	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
343 	return cie;
344 }
345 
346 /**
347  *	dwarf_lookup_fde - locate the FDE that covers pc
348  *	@pc: the program counter
349  */
350 struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
351 {
352 	struct rb_node **rb_node = &fde_root.rb_node;
353 	struct dwarf_fde *fde = NULL;
354 	unsigned long flags;
355 
356 	spin_lock_irqsave(&dwarf_fde_lock, flags);
357 
358 	while (*rb_node) {
359 		struct dwarf_fde *fde_tmp;
360 		unsigned long tmp_start, tmp_end;
361 
362 		fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
363 		BUG_ON(!fde_tmp);
364 
365 		tmp_start = fde_tmp->initial_location;
366 		tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
367 
368 		if (pc < tmp_start) {
369 			rb_node = &(*rb_node)->rb_left;
370 		} else {
371 			if (pc < tmp_end) {
372 				fde = fde_tmp;
373 				goto out;
374 			} else
375 				rb_node = &(*rb_node)->rb_right;
376 		}
377 	}
378 
379 out:
380 	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
381 
382 	return fde;
383 }
384 
385 /**
386  *	dwarf_cfa_execute_insns - execute instructions to calculate a CFA
387  *	@insn_start: address of the first instruction
388  *	@insn_end: address of the last instruction
389  *	@cie: the CIE for this function
390  *	@fde: the FDE for this function
391  *	@frame: the instructions calculate the CFA for this frame
392  *	@pc: the program counter of the address we're interested in
393  *
394  *	Execute the Call Frame instruction sequence starting at
395  *	@insn_start and ending at @insn_end. The instructions describe
396  *	how to calculate the Canonical Frame Address of a stackframe.
397  *	Store the results in @frame.
398  */
399 static int dwarf_cfa_execute_insns(unsigned char *insn_start,
400 				   unsigned char *insn_end,
401 				   struct dwarf_cie *cie,
402 				   struct dwarf_fde *fde,
403 				   struct dwarf_frame *frame,
404 				   unsigned long pc)
405 {
406 	unsigned char insn;
407 	unsigned char *current_insn;
408 	unsigned int count, delta, reg, expr_len, offset;
409 	struct dwarf_reg *regp;
410 
411 	current_insn = insn_start;
412 
413 	while (current_insn < insn_end && frame->pc <= pc) {
414 		insn = __raw_readb(current_insn++);
415 
416 		/*
417 		 * Firstly, handle the opcodes that embed their operands
418 		 * in the instructions.
419 		 */
420 		switch (DW_CFA_opcode(insn)) {
421 		case DW_CFA_advance_loc:
422 			delta = DW_CFA_operand(insn);
423 			delta *= cie->code_alignment_factor;
424 			frame->pc += delta;
425 			continue;
426 			/* NOTREACHED */
427 		case DW_CFA_offset:
428 			reg = DW_CFA_operand(insn);
429 			count = dwarf_read_uleb128(current_insn, &offset);
430 			current_insn += count;
431 			offset *= cie->data_alignment_factor;
432 			regp = dwarf_frame_alloc_reg(frame, reg);
433 			regp->addr = offset;
434 			regp->flags |= DWARF_REG_OFFSET;
435 			continue;
436 			/* NOTREACHED */
437 		case DW_CFA_restore:
438 			reg = DW_CFA_operand(insn);
439 			continue;
440 			/* NOTREACHED */
441 		}
442 
443 		/*
444 		 * Secondly, handle the opcodes that don't embed their
445 		 * operands in the instruction.
446 		 */
447 		switch (insn) {
448 		case DW_CFA_nop:
449 			continue;
450 		case DW_CFA_advance_loc1:
451 			delta = *current_insn++;
452 			frame->pc += delta * cie->code_alignment_factor;
453 			break;
454 		case DW_CFA_advance_loc2:
455 			delta = get_unaligned((u16 *)current_insn);
456 			current_insn += 2;
457 			frame->pc += delta * cie->code_alignment_factor;
458 			break;
459 		case DW_CFA_advance_loc4:
460 			delta = get_unaligned((u32 *)current_insn);
461 			current_insn += 4;
462 			frame->pc += delta * cie->code_alignment_factor;
463 			break;
464 		case DW_CFA_offset_extended:
465 			count = dwarf_read_uleb128(current_insn, &reg);
466 			current_insn += count;
467 			count = dwarf_read_uleb128(current_insn, &offset);
468 			current_insn += count;
469 			offset *= cie->data_alignment_factor;
470 			break;
471 		case DW_CFA_restore_extended:
472 			count = dwarf_read_uleb128(current_insn, &reg);
473 			current_insn += count;
474 			break;
475 		case DW_CFA_undefined:
476 			count = dwarf_read_uleb128(current_insn, &reg);
477 			current_insn += count;
478 			regp = dwarf_frame_alloc_reg(frame, reg);
479 			regp->flags |= DWARF_UNDEFINED;
480 			break;
481 		case DW_CFA_def_cfa:
482 			count = dwarf_read_uleb128(current_insn,
483 						   &frame->cfa_register);
484 			current_insn += count;
485 			count = dwarf_read_uleb128(current_insn,
486 						   &frame->cfa_offset);
487 			current_insn += count;
488 
489 			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
490 			break;
491 		case DW_CFA_def_cfa_register:
492 			count = dwarf_read_uleb128(current_insn,
493 						   &frame->cfa_register);
494 			current_insn += count;
495 			frame->flags |= DWARF_FRAME_CFA_REG_OFFSET;
496 			break;
497 		case DW_CFA_def_cfa_offset:
498 			count = dwarf_read_uleb128(current_insn, &offset);
499 			current_insn += count;
500 			frame->cfa_offset = offset;
501 			break;
502 		case DW_CFA_def_cfa_expression:
503 			count = dwarf_read_uleb128(current_insn, &expr_len);
504 			current_insn += count;
505 
506 			frame->cfa_expr = current_insn;
507 			frame->cfa_expr_len = expr_len;
508 			current_insn += expr_len;
509 
510 			frame->flags |= DWARF_FRAME_CFA_REG_EXP;
511 			break;
512 		case DW_CFA_offset_extended_sf:
513 			count = dwarf_read_uleb128(current_insn, &reg);
514 			current_insn += count;
515 			count = dwarf_read_leb128(current_insn, &offset);
516 			current_insn += count;
517 			offset *= cie->data_alignment_factor;
518 			regp = dwarf_frame_alloc_reg(frame, reg);
519 			regp->flags |= DWARF_REG_OFFSET;
520 			regp->addr = offset;
521 			break;
522 		case DW_CFA_val_offset:
523 			count = dwarf_read_uleb128(current_insn, &reg);
524 			current_insn += count;
525 			count = dwarf_read_leb128(current_insn, &offset);
526 			offset *= cie->data_alignment_factor;
527 			regp = dwarf_frame_alloc_reg(frame, reg);
528 			regp->flags |= DWARF_VAL_OFFSET;
529 			regp->addr = offset;
530 			break;
531 		case DW_CFA_GNU_args_size:
532 			count = dwarf_read_uleb128(current_insn, &offset);
533 			current_insn += count;
534 			break;
535 		case DW_CFA_GNU_negative_offset_extended:
536 			count = dwarf_read_uleb128(current_insn, &reg);
537 			current_insn += count;
538 			count = dwarf_read_uleb128(current_insn, &offset);
539 			offset *= cie->data_alignment_factor;
540 
541 			regp = dwarf_frame_alloc_reg(frame, reg);
542 			regp->flags |= DWARF_REG_OFFSET;
543 			regp->addr = -offset;
544 			break;
545 		default:
546 			pr_debug("unhandled DWARF instruction 0x%x\n", insn);
547 			UNWINDER_BUG();
548 			break;
549 		}
550 	}
551 
552 	return 0;
553 }
554 
555 /**
556  *	dwarf_free_frame - free the memory allocated for @frame
557  *	@frame: the frame to free
558  */
559 void dwarf_free_frame(struct dwarf_frame *frame)
560 {
561 	dwarf_frame_free_regs(frame);
562 	mempool_free(frame, dwarf_frame_pool);
563 }
564 
565 extern void ret_from_irq(void);
566 
567 /**
568  *	dwarf_unwind_stack - unwind the stack
569  *
570  *	@pc: address of the function to unwind
571  *	@prev: struct dwarf_frame of the previous stackframe on the callstack
572  *
573  *	Return a struct dwarf_frame representing the most recent frame
574  *	on the callstack. Each of the lower (older) stack frames are
575  *	linked via the "prev" member.
576  */
577 struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,
578 				       struct dwarf_frame *prev)
579 {
580 	struct dwarf_frame *frame;
581 	struct dwarf_cie *cie;
582 	struct dwarf_fde *fde;
583 	struct dwarf_reg *reg;
584 	unsigned long addr;
585 
586 	/*
587 	 * If we've been called in to before initialization has
588 	 * completed, bail out immediately.
589 	 */
590 	if (!dwarf_unwinder_ready)
591 		return NULL;
592 
593 	/*
594 	 * If we're starting at the top of the stack we need get the
595 	 * contents of a physical register to get the CFA in order to
596 	 * begin the virtual unwinding of the stack.
597 	 *
598 	 * NOTE: the return address is guaranteed to be setup by the
599 	 * time this function makes its first function call.
600 	 */
601 	if (!pc || !prev)
602 		pc = _THIS_IP_;
603 
604 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
605 	/*
606 	 * If our stack has been patched by the function graph tracer
607 	 * then we might see the address of return_to_handler() where we
608 	 * expected to find the real return address.
609 	 */
610 	if (pc == (unsigned long)&return_to_handler) {
611 		struct ftrace_ret_stack *ret_stack;
612 
613 		ret_stack = ftrace_graph_get_ret_stack(current, 0);
614 		if (ret_stack)
615 			pc = ret_stack->ret;
616 		/*
617 		 * We currently have no way of tracking how many
618 		 * return_to_handler()'s we've seen. If there is more
619 		 * than one patched return address on our stack,
620 		 * complain loudly.
621 		 */
622 		WARN_ON(ftrace_graph_get_ret_stack(current, 1));
623 	}
624 #endif
625 
626 	frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);
627 	if (!frame) {
628 		printk(KERN_ERR "Unable to allocate a dwarf frame\n");
629 		UNWINDER_BUG();
630 	}
631 
632 	INIT_LIST_HEAD(&frame->reg_list);
633 	frame->flags = 0;
634 	frame->prev = prev;
635 	frame->return_addr = 0;
636 
637 	fde = dwarf_lookup_fde(pc);
638 	if (!fde) {
639 		/*
640 		 * This is our normal exit path. There are two reasons
641 		 * why we might exit here,
642 		 *
643 		 *	a) pc has no asscociated DWARF frame info and so
644 		 *	we don't know how to unwind this frame. This is
645 		 *	usually the case when we're trying to unwind a
646 		 *	frame that was called from some assembly code
647 		 *	that has no DWARF info, e.g. syscalls.
648 		 *
649 		 *	b) the DEBUG info for pc is bogus. There's
650 		 *	really no way to distinguish this case from the
651 		 *	case above, which sucks because we could print a
652 		 *	warning here.
653 		 */
654 		goto bail;
655 	}
656 
657 	cie = dwarf_lookup_cie(fde->cie_pointer);
658 
659 	frame->pc = fde->initial_location;
660 
661 	/* CIE initial instructions */
662 	dwarf_cfa_execute_insns(cie->initial_instructions,
663 				cie->instructions_end, cie, fde,
664 				frame, pc);
665 
666 	/* FDE instructions */
667 	dwarf_cfa_execute_insns(fde->instructions, fde->end, cie,
668 				fde, frame, pc);
669 
670 	/* Calculate the CFA */
671 	switch (frame->flags) {
672 	case DWARF_FRAME_CFA_REG_OFFSET:
673 		if (prev) {
674 			reg = dwarf_frame_reg(prev, frame->cfa_register);
675 			UNWINDER_BUG_ON(!reg);
676 			UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
677 
678 			addr = prev->cfa + reg->addr;
679 			frame->cfa = __raw_readl(addr);
680 
681 		} else {
682 			/*
683 			 * Again, we're starting from the top of the
684 			 * stack. We need to physically read
685 			 * the contents of a register in order to get
686 			 * the Canonical Frame Address for this
687 			 * function.
688 			 */
689 			frame->cfa = dwarf_read_arch_reg(frame->cfa_register);
690 		}
691 
692 		frame->cfa += frame->cfa_offset;
693 		break;
694 	default:
695 		UNWINDER_BUG();
696 	}
697 
698 	reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);
699 
700 	/*
701 	 * If we haven't seen the return address register or the return
702 	 * address column is undefined then we must assume that this is
703 	 * the end of the callstack.
704 	 */
705 	if (!reg || reg->flags == DWARF_UNDEFINED)
706 		goto bail;
707 
708 	UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);
709 
710 	addr = frame->cfa + reg->addr;
711 	frame->return_addr = __raw_readl(addr);
712 
713 	/*
714 	 * Ah, the joys of unwinding through interrupts.
715 	 *
716 	 * Interrupts are tricky - the DWARF info needs to be _really_
717 	 * accurate and unfortunately I'm seeing a lot of bogus DWARF
718 	 * info. For example, I've seen interrupts occur in epilogues
719 	 * just after the frame pointer (r14) had been restored. The
720 	 * problem was that the DWARF info claimed that the CFA could be
721 	 * reached by using the value of the frame pointer before it was
722 	 * restored.
723 	 *
724 	 * So until the compiler can be trusted to produce reliable
725 	 * DWARF info when it really matters, let's stop unwinding once
726 	 * we've calculated the function that was interrupted.
727 	 */
728 	if (prev && prev->pc == (unsigned long)ret_from_irq)
729 		frame->return_addr = 0;
730 
731 	return frame;
732 
733 bail:
734 	dwarf_free_frame(frame);
735 	return NULL;
736 }
737 
738 static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
739 			   unsigned char *end, struct module *mod)
740 {
741 	struct rb_node **rb_node = &cie_root.rb_node;
742 	struct rb_node *parent = *rb_node;
743 	struct dwarf_cie *cie;
744 	unsigned long flags;
745 	int count;
746 
747 	cie = kzalloc(sizeof(*cie), GFP_KERNEL);
748 	if (!cie)
749 		return -ENOMEM;
750 
751 	cie->length = len;
752 
753 	/*
754 	 * Record the offset into the .eh_frame section
755 	 * for this CIE. It allows this CIE to be
756 	 * quickly and easily looked up from the
757 	 * corresponding FDE.
758 	 */
759 	cie->cie_pointer = (unsigned long)entry;
760 
761 	cie->version = *(char *)p++;
762 	UNWINDER_BUG_ON(cie->version != 1);
763 
764 	cie->augmentation = p;
765 	p += strlen(cie->augmentation) + 1;
766 
767 	count = dwarf_read_uleb128(p, &cie->code_alignment_factor);
768 	p += count;
769 
770 	count = dwarf_read_leb128(p, &cie->data_alignment_factor);
771 	p += count;
772 
773 	/*
774 	 * Which column in the rule table contains the
775 	 * return address?
776 	 */
777 	if (cie->version == 1) {
778 		cie->return_address_reg = __raw_readb(p);
779 		p++;
780 	} else {
781 		count = dwarf_read_uleb128(p, &cie->return_address_reg);
782 		p += count;
783 	}
784 
785 	if (cie->augmentation[0] == 'z') {
786 		unsigned int length, count;
787 		cie->flags |= DWARF_CIE_Z_AUGMENTATION;
788 
789 		count = dwarf_read_uleb128(p, &length);
790 		p += count;
791 
792 		UNWINDER_BUG_ON((unsigned char *)p > end);
793 
794 		cie->initial_instructions = p + length;
795 		cie->augmentation++;
796 	}
797 
798 	while (*cie->augmentation) {
799 		/*
800 		 * "L" indicates a byte showing how the
801 		 * LSDA pointer is encoded. Skip it.
802 		 */
803 		if (*cie->augmentation == 'L') {
804 			p++;
805 			cie->augmentation++;
806 		} else if (*cie->augmentation == 'R') {
807 			/*
808 			 * "R" indicates a byte showing
809 			 * how FDE addresses are
810 			 * encoded.
811 			 */
812 			cie->encoding = *(char *)p++;
813 			cie->augmentation++;
814 		} else if (*cie->augmentation == 'P') {
815 			/*
816 			 * "R" indicates a personality
817 			 * routine in the CIE
818 			 * augmentation.
819 			 */
820 			UNWINDER_BUG();
821 		} else if (*cie->augmentation == 'S') {
822 			UNWINDER_BUG();
823 		} else {
824 			/*
825 			 * Unknown augmentation. Assume
826 			 * 'z' augmentation.
827 			 */
828 			p = cie->initial_instructions;
829 			UNWINDER_BUG_ON(!p);
830 			break;
831 		}
832 	}
833 
834 	cie->initial_instructions = p;
835 	cie->instructions_end = end;
836 
837 	/* Add to list */
838 	spin_lock_irqsave(&dwarf_cie_lock, flags);
839 
840 	while (*rb_node) {
841 		struct dwarf_cie *cie_tmp;
842 
843 		cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
844 
845 		parent = *rb_node;
846 
847 		if (cie->cie_pointer < cie_tmp->cie_pointer)
848 			rb_node = &parent->rb_left;
849 		else if (cie->cie_pointer >= cie_tmp->cie_pointer)
850 			rb_node = &parent->rb_right;
851 		else
852 			WARN_ON(1);
853 	}
854 
855 	rb_link_node(&cie->node, parent, rb_node);
856 	rb_insert_color(&cie->node, &cie_root);
857 
858 #ifdef CONFIG_MODULES
859 	if (mod != NULL)
860 		list_add_tail(&cie->link, &mod->arch.cie_list);
861 #endif
862 
863 	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
864 
865 	return 0;
866 }
867 
868 static int dwarf_parse_fde(void *entry, u32 entry_type,
869 			   void *start, unsigned long len,
870 			   unsigned char *end, struct module *mod)
871 {
872 	struct rb_node **rb_node = &fde_root.rb_node;
873 	struct rb_node *parent = *rb_node;
874 	struct dwarf_fde *fde;
875 	struct dwarf_cie *cie;
876 	unsigned long flags;
877 	int count;
878 	void *p = start;
879 
880 	fde = kzalloc(sizeof(*fde), GFP_KERNEL);
881 	if (!fde)
882 		return -ENOMEM;
883 
884 	fde->length = len;
885 
886 	/*
887 	 * In a .eh_frame section the CIE pointer is the
888 	 * delta between the address within the FDE
889 	 */
890 	fde->cie_pointer = (unsigned long)(p - entry_type - 4);
891 
892 	cie = dwarf_lookup_cie(fde->cie_pointer);
893 	fde->cie = cie;
894 
895 	if (cie->encoding)
896 		count = dwarf_read_encoded_value(p, &fde->initial_location,
897 						 cie->encoding);
898 	else
899 		count = dwarf_read_addr(p, &fde->initial_location);
900 
901 	p += count;
902 
903 	if (cie->encoding)
904 		count = dwarf_read_encoded_value(p, &fde->address_range,
905 						 cie->encoding & 0x0f);
906 	else
907 		count = dwarf_read_addr(p, &fde->address_range);
908 
909 	p += count;
910 
911 	if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) {
912 		unsigned int length;
913 		count = dwarf_read_uleb128(p, &length);
914 		p += count + length;
915 	}
916 
917 	/* Call frame instructions. */
918 	fde->instructions = p;
919 	fde->end = end;
920 
921 	/* Add to list. */
922 	spin_lock_irqsave(&dwarf_fde_lock, flags);
923 
924 	while (*rb_node) {
925 		struct dwarf_fde *fde_tmp;
926 		unsigned long tmp_start, tmp_end;
927 		unsigned long start, end;
928 
929 		fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
930 
931 		start = fde->initial_location;
932 		end = fde->initial_location + fde->address_range;
933 
934 		tmp_start = fde_tmp->initial_location;
935 		tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
936 
937 		parent = *rb_node;
938 
939 		if (start < tmp_start)
940 			rb_node = &parent->rb_left;
941 		else if (start >= tmp_end)
942 			rb_node = &parent->rb_right;
943 		else
944 			WARN_ON(1);
945 	}
946 
947 	rb_link_node(&fde->node, parent, rb_node);
948 	rb_insert_color(&fde->node, &fde_root);
949 
950 #ifdef CONFIG_MODULES
951 	if (mod != NULL)
952 		list_add_tail(&fde->link, &mod->arch.fde_list);
953 #endif
954 
955 	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
956 
957 	return 0;
958 }
959 
960 static void dwarf_unwinder_dump(struct task_struct *task,
961 				struct pt_regs *regs,
962 				unsigned long *sp,
963 				const struct stacktrace_ops *ops,
964 				void *data)
965 {
966 	struct dwarf_frame *frame, *_frame;
967 	unsigned long return_addr;
968 
969 	_frame = NULL;
970 	return_addr = 0;
971 
972 	while (1) {
973 		frame = dwarf_unwind_stack(return_addr, _frame);
974 
975 		if (_frame)
976 			dwarf_free_frame(_frame);
977 
978 		_frame = frame;
979 
980 		if (!frame || !frame->return_addr)
981 			break;
982 
983 		return_addr = frame->return_addr;
984 		ops->address(data, return_addr, 1);
985 	}
986 
987 	if (frame)
988 		dwarf_free_frame(frame);
989 }
990 
991 static struct unwinder dwarf_unwinder = {
992 	.name = "dwarf-unwinder",
993 	.dump = dwarf_unwinder_dump,
994 	.rating = 150,
995 };
996 
997 static void __init dwarf_unwinder_cleanup(void)
998 {
999 	struct dwarf_fde *fde, *next_fde;
1000 	struct dwarf_cie *cie, *next_cie;
1001 
1002 	/*
1003 	 * Deallocate all the memory allocated for the DWARF unwinder.
1004 	 * Traverse all the FDE/CIE lists and remove and free all the
1005 	 * memory associated with those data structures.
1006 	 */
1007 	rbtree_postorder_for_each_entry_safe(fde, next_fde, &fde_root, node)
1008 		kfree(fde);
1009 
1010 	rbtree_postorder_for_each_entry_safe(cie, next_cie, &cie_root, node)
1011 		kfree(cie);
1012 
1013 	mempool_destroy(dwarf_reg_pool);
1014 	mempool_destroy(dwarf_frame_pool);
1015 	kmem_cache_destroy(dwarf_reg_cachep);
1016 	kmem_cache_destroy(dwarf_frame_cachep);
1017 }
1018 
1019 /**
1020  *	dwarf_parse_section - parse DWARF section
1021  *	@eh_frame_start: start address of the .eh_frame section
1022  *	@eh_frame_end: end address of the .eh_frame section
1023  *	@mod: the kernel module containing the .eh_frame section
1024  *
1025  *	Parse the information in a .eh_frame section.
1026  */
1027 static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
1028 			       struct module *mod)
1029 {
1030 	u32 entry_type;
1031 	void *p, *entry;
1032 	int count, err = 0;
1033 	unsigned long len = 0;
1034 	unsigned int c_entries, f_entries;
1035 	unsigned char *end;
1036 
1037 	c_entries = 0;
1038 	f_entries = 0;
1039 	entry = eh_frame_start;
1040 
1041 	while ((char *)entry < eh_frame_end) {
1042 		p = entry;
1043 
1044 		count = dwarf_entry_len(p, &len);
1045 		if (count == 0) {
1046 			/*
1047 			 * We read a bogus length field value. There is
1048 			 * nothing we can do here apart from disabling
1049 			 * the DWARF unwinder. We can't even skip this
1050 			 * entry and move to the next one because 'len'
1051 			 * tells us where our next entry is.
1052 			 */
1053 			err = -EINVAL;
1054 			goto out;
1055 		} else
1056 			p += count;
1057 
1058 		/* initial length does not include itself */
1059 		end = p + len;
1060 
1061 		entry_type = get_unaligned((u32 *)p);
1062 		p += 4;
1063 
1064 		if (entry_type == DW_EH_FRAME_CIE) {
1065 			err = dwarf_parse_cie(entry, p, len, end, mod);
1066 			if (err < 0)
1067 				goto out;
1068 			else
1069 				c_entries++;
1070 		} else {
1071 			err = dwarf_parse_fde(entry, entry_type, p, len,
1072 					      end, mod);
1073 			if (err < 0)
1074 				goto out;
1075 			else
1076 				f_entries++;
1077 		}
1078 
1079 		entry = (char *)entry + len + 4;
1080 	}
1081 
1082 	printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
1083 	       c_entries, f_entries);
1084 
1085 	return 0;
1086 
1087 out:
1088 	return err;
1089 }
1090 
1091 #ifdef CONFIG_MODULES
1092 int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
1093 			  struct module *me)
1094 {
1095 	unsigned int i, err;
1096 	unsigned long start, end;
1097 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1098 
1099 	start = end = 0;
1100 
1101 	for (i = 1; i < hdr->e_shnum; i++) {
1102 		/* Alloc bit cleared means "ignore it." */
1103 		if ((sechdrs[i].sh_flags & SHF_ALLOC)
1104 		    && !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
1105 			start = sechdrs[i].sh_addr;
1106 			end = start + sechdrs[i].sh_size;
1107 			break;
1108 		}
1109 	}
1110 
1111 	/* Did we find the .eh_frame section? */
1112 	if (i != hdr->e_shnum) {
1113 		INIT_LIST_HEAD(&me->arch.cie_list);
1114 		INIT_LIST_HEAD(&me->arch.fde_list);
1115 		err = dwarf_parse_section((char *)start, (char *)end, me);
1116 		if (err) {
1117 			printk(KERN_WARNING "%s: failed to parse DWARF info\n",
1118 			       me->name);
1119 			return err;
1120 		}
1121 	}
1122 
1123 	return 0;
1124 }
1125 
1126 /**
1127  *	module_dwarf_cleanup - remove FDE/CIEs associated with @mod
1128  *	@mod: the module that is being unloaded
1129  *
1130  *	Remove any FDEs and CIEs from the global lists that came from
1131  *	@mod's .eh_frame section because @mod is being unloaded.
1132  */
1133 void module_dwarf_cleanup(struct module *mod)
1134 {
1135 	struct dwarf_fde *fde, *ftmp;
1136 	struct dwarf_cie *cie, *ctmp;
1137 	unsigned long flags;
1138 
1139 	spin_lock_irqsave(&dwarf_cie_lock, flags);
1140 
1141 	list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
1142 		list_del(&cie->link);
1143 		rb_erase(&cie->node, &cie_root);
1144 		kfree(cie);
1145 	}
1146 
1147 	spin_unlock_irqrestore(&dwarf_cie_lock, flags);
1148 
1149 	spin_lock_irqsave(&dwarf_fde_lock, flags);
1150 
1151 	list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
1152 		list_del(&fde->link);
1153 		rb_erase(&fde->node, &fde_root);
1154 		kfree(fde);
1155 	}
1156 
1157 	spin_unlock_irqrestore(&dwarf_fde_lock, flags);
1158 }
1159 #endif /* CONFIG_MODULES */
1160 
1161 /**
1162  *	dwarf_unwinder_init - initialise the dwarf unwinder
1163  *
1164  *	Build the data structures describing the .dwarf_frame section to
1165  *	make it easier to lookup CIE and FDE entries. Because the
1166  *	.eh_frame section is packed as tightly as possible it is not
1167  *	easy to lookup the FDE for a given PC, so we build a list of FDE
1168  *	and CIE entries that make it easier.
1169  */
1170 static int __init dwarf_unwinder_init(void)
1171 {
1172 	int err = -ENOMEM;
1173 
1174 	dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
1175 			sizeof(struct dwarf_frame), 0,
1176 			SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL);
1177 
1178 	dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
1179 			sizeof(struct dwarf_reg), 0,
1180 			SLAB_PANIC | SLAB_HWCACHE_ALIGN, NULL);
1181 
1182 	dwarf_frame_pool = mempool_create_slab_pool(DWARF_FRAME_MIN_REQ,
1183 						    dwarf_frame_cachep);
1184 	if (!dwarf_frame_pool)
1185 		goto out;
1186 
1187 	dwarf_reg_pool = mempool_create_slab_pool(DWARF_REG_MIN_REQ,
1188 						  dwarf_reg_cachep);
1189 	if (!dwarf_reg_pool)
1190 		goto out;
1191 
1192 	err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
1193 	if (err)
1194 		goto out;
1195 
1196 	err = unwinder_register(&dwarf_unwinder);
1197 	if (err)
1198 		goto out;
1199 
1200 	dwarf_unwinder_ready = 1;
1201 
1202 	return 0;
1203 
1204 out:
1205 	printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
1206 	dwarf_unwinder_cleanup();
1207 	return err;
1208 }
1209 early_initcall(dwarf_unwinder_init);
1210