| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 | /* * arch/arm/kernel/unwind.c * * Copyright (C) 2008 ARM Limited * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * Stack unwinding support for ARM * * An ARM EABI version of gcc is required to generate the unwind * tables. For information about the structure of the unwind tables, * see "Exception Handling ABI for the ARM Architecture" at: * * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html */#ifndef __CHECKER__#if !defined (__ARM_EABI__)#warning Your compiler does not have EABI support.#warning    ARM unwind is known to compile only with EABI compilers.#warning    Change compiler or disable ARM_UNWIND option.#elif (__GNUC__ == 4 && __GNUC_MINOR__ <= 2)#warning Your compiler is too buggy; it is known to not compile ARM unwind support.#warning    Change compiler or disable ARM_UNWIND option.#endif#endif /* __CHECKER__ */#include <linux/kernel.h>#include <linux/init.h>#include <linux/export.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/list.h>#include <asm/stacktrace.h>#include <asm/traps.h>#include <asm/unwind.h>/* Dummy functions to avoid linker complaints */void __aeabi_unwind_cpp_pr0(void){};EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0);void __aeabi_unwind_cpp_pr1(void){};EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1);void __aeabi_unwind_cpp_pr2(void){};EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2);struct unwind_ctrl_block {	unsigned long vrs[16];		/* virtual register set */	const unsigned long *insn;	/* pointer to the current instructions word */	int entries;			/* number of entries left to interpret */	int byte;			/* current byte number in the instructions word */};enum regs {#ifdef CONFIG_THUMB2_KERNEL	FP = 7,#else	FP = 11,#endif	SP = 13,	LR = 14,	PC = 15};extern const struct unwind_idx __start_unwind_idx[];static const struct unwind_idx *__origin_unwind_idx;extern const struct unwind_idx __stop_unwind_idx[];static DEFINE_SPINLOCK(unwind_lock);static LIST_HEAD(unwind_tables);/* Convert a prel31 symbol to an absolute address */#define prel31_to_addr(ptr)				\({							\	/* sign-extend to 32 bits */			\	long offset = (((long)*(ptr)) << 1) >> 1;	\	(unsigned long)(ptr) + offset;			\})/* * Binary search in the unwind index. The entries are * guaranteed to be sorted in ascending order by the linker. * * start = first entry * origin = first entry with positive offset (or stop if there is no such entry) * stop - 1 = last entry */static const struct unwind_idx *search_index(unsigned long addr,				       const struct unwind_idx *start,				       const struct unwind_idx *origin,				       const struct unwind_idx *stop){	unsigned long addr_prel31;	pr_debug("%s(%08lx, %p, %p, %p)\n",			__func__, addr, start, origin, stop);	/*	 * only search in the section with the matching sign. This way the	 * prel31 numbers can be compared as unsigned longs.	 */	if (addr < (unsigned long)start)		/* negative offsets: [start; origin) */		stop = origin;	else		/* positive offsets: [origin; stop) */		start = origin;	/* prel31 for address relavive to start */	addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;	while (start < stop - 1) {		const struct unwind_idx *mid = start + ((stop - start) >> 1);		/*		 * As addr_prel31 is relative to start an offset is needed to		 * make it relative to mid.		 */		if (addr_prel31 - ((unsigned long)mid - (unsigned long)start) <				mid->addr_offset)			stop = mid;		else {			/* keep addr_prel31 relative to start */			addr_prel31 -= ((unsigned long)mid -					(unsigned long)start);			start = mid;		}	}	if (likely(start->addr_offset <= addr_prel31))		return start;	else {		pr_warning("unwind: Unknown symbol address %08lx\n", addr);		return NULL;	}}static const struct unwind_idx *unwind_find_origin(		const struct unwind_idx *start, const struct unwind_idx *stop){	pr_debug("%s(%p, %p)\n", __func__, start, stop);	while (start < stop) {		const struct unwind_idx *mid = start + ((stop - start) >> 1);		if (mid->addr_offset >= 0x40000000)			/* negative offset */			start = mid + 1;		else			/* positive offset */			stop = mid;	}	pr_debug("%s -> %p\n", __func__, stop);	return stop;}static const struct unwind_idx *unwind_find_idx(unsigned long addr){	const struct unwind_idx *idx = NULL;	unsigned long flags;	pr_debug("%s(%08lx)\n", __func__, addr);	if (core_kernel_text(addr)) {		if (unlikely(!__origin_unwind_idx))			__origin_unwind_idx =				unwind_find_origin(__start_unwind_idx,						__stop_unwind_idx);		/* main unwind table */		idx = search_index(addr, __start_unwind_idx,				   __origin_unwind_idx,				   __stop_unwind_idx);	} else {		/* module unwind tables */		struct unwind_table *table;		spin_lock_irqsave(&unwind_lock, flags);		list_for_each_entry(table, &unwind_tables, list) {			if (addr >= table->begin_addr &&			    addr < table->end_addr) {				idx = search_index(addr, table->start,						   table->origin,						   table->stop);				/* Move-to-front to exploit common traces */				list_move(&table->list, &unwind_tables);				break;			}		}		spin_unlock_irqrestore(&unwind_lock, flags);	}	pr_debug("%s: idx = %p\n", __func__, idx);	return idx;}static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl){	unsigned long ret;	if (ctrl->entries <= 0) {		pr_warning("unwind: Corrupt unwind table\n");		return 0;	}	ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;	if (ctrl->byte == 0) {		ctrl->insn++;		ctrl->entries--;		ctrl->byte = 3;	} else		ctrl->byte--;	return ret;}/* * Execute the current unwind instruction. */static int unwind_exec_insn(struct unwind_ctrl_block *ctrl){	unsigned long insn = unwind_get_byte(ctrl);	pr_debug("%s: insn = %08lx\n", __func__, insn);	if ((insn & 0xc0) == 0x00)		ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4;	else if ((insn & 0xc0) == 0x40)
 |