1206 lines · 29.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org> * * 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. *	- Registers with DWARF_VAL_OFFSET rules aren't handled properly. */ /* #define DEBUG */#include <linux/kernel.h>#include <linux/io.h>#include <linux/list.h>#include <linux/mempool.h>#include <linux/mm.h>#include <linux/elf.h>#include <linux/ftrace.h>#include <linux/module.h>#include <linux/slab.h>#include <asm/dwarf.h>#include <asm/unwinder.h>#include <asm/sections.h>#include <linux/unaligned.h>#include <asm/stacktrace.h> /* 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; static struct rb_root cie_root;static DEFINE_SPINLOCK(dwarf_cie_lock); static struct rb_root fde_root;static DEFINE_SPINLOCK(dwarf_fde_lock); static struct dwarf_cie *cached_cie; static unsigned int dwarf_unwinder_ready; /** *	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. * *	Return the initialised DWARF reg. */static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,					       unsigned int reg_num){	struct dwarf_reg *reg; 	reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);	if (!reg) {		printk(KERN_WARNING "Unable to allocate a DWARF register\n");		/*		 * Let's just bomb hard here, we have no way to		 * gracefully recover.		 */		UNWINDER_BUG();	} 	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;	} 	return NULL;} /** *	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. */static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst){	u32 val = get_unaligned(src);	put_unaligned(val, dst);	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\n", (encoding & 0x70));		UNWINDER_BUG();	} 	if ((encoding & 0x07) == 0x00)		encoding |= DW_EH_PE_udata4; 	switch (encoding & 0x0f) {	case DW_EH_PE_sdata4:	case DW_EH_PE_udata4:		count += 4;		decoded_addr += get_unaligned((u32 *)addr);		__raw_writel(decoded_addr, val);		break;	default:		pr_debug("encoding=0x%x\n", encoding);		UNWINDER_BUG();	} 	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; 	initial_len = get_unaligned((u32 *)addr);	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) {			*len = get_unaligned((u64 *)addr + 4);			count = 12;		} else {			printk(KERN_WARNING "Unknown DWARF extension\n");			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){	struct rb_node **rb_node = &cie_root.rb_node;	struct dwarf_cie *cie = NULL;	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;	} 	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;		}	} out:	spin_unlock_irqrestore(&dwarf_cie_lock, flags);	return cie;} /** *	dwarf_lookup_fde - locate the FDE that covers pc *	@pc: the program counter */static struct dwarf_fde *dwarf_lookup_fde(unsigned long pc){	struct rb_node **rb_node = &fde_root.rb_node;	struct dwarf_fde *fde = NULL;	unsigned long flags; 	spin_lock_irqsave(&dwarf_fde_lock, flags); 	while (*rb_node) {		struct dwarf_fde *fde_tmp;		unsigned long tmp_start, tmp_end; 		fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);		BUG_ON(!fde_tmp); 		tmp_start = fde_tmp->initial_location;		tmp_end = fde_tmp->initial_location + fde_tmp->address_range; 		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;		}	} out:	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,				   unsigned long pc){	unsigned char insn;	unsigned char *current_insn;	unsigned int count, delta, reg, expr_len, offset;	struct dwarf_reg *regp; 	current_insn = insn_start; 	while (current_insn < insn_end && frame->pc <= pc) {		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;			regp = dwarf_frame_alloc_reg(frame, reg);			regp->addr = offset;			regp->flags |= DWARF_REG_OFFSET;			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:			delta = get_unaligned((u16 *)current_insn);			current_insn += 2;			frame->pc += delta * cie->code_alignment_factor;			break;		case DW_CFA_advance_loc4:			delta = get_unaligned((u32 *)current_insn);			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;			regp = dwarf_frame_alloc_reg(frame, reg);			regp->flags |= DWARF_UNDEFINED;			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);

Lines 1–500 of 1206.