xref: /openbmc/linux/arch/xtensa/kernel/traps.c (revision 2d1c645cc50b8f5a718b24bad9eb3931e7105d12)
15a0015d6SChris Zankel /*
25a0015d6SChris Zankel  * arch/xtensa/kernel/traps.c
35a0015d6SChris Zankel  *
45a0015d6SChris Zankel  * Exception handling.
55a0015d6SChris Zankel  *
65a0015d6SChris Zankel  * Derived from code with the following copyrights:
75a0015d6SChris Zankel  * Copyright (C) 1994 - 1999 by Ralf Baechle
85a0015d6SChris Zankel  * Modified for R3000 by Paul M. Antoine, 1995, 1996
95a0015d6SChris Zankel  * Complete output from die() by Ulf Carlsson, 1998
105a0015d6SChris Zankel  * Copyright (C) 1999 Silicon Graphics, Inc.
115a0015d6SChris Zankel  *
125a0015d6SChris Zankel  * Essentially rewritten for the Xtensa architecture port.
135a0015d6SChris Zankel  *
145a0015d6SChris Zankel  * Copyright (C) 2001 - 2005 Tensilica Inc.
155a0015d6SChris Zankel  *
165a0015d6SChris Zankel  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
175a0015d6SChris Zankel  * Chris Zankel	<chris@zankel.net>
185a0015d6SChris Zankel  * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca>
195a0015d6SChris Zankel  * Kevin Chea
205a0015d6SChris Zankel  *
215a0015d6SChris Zankel  * This file is subject to the terms and conditions of the GNU General Public
225a0015d6SChris Zankel  * License.  See the file "COPYING" in the main directory of this archive
235a0015d6SChris Zankel  * for more details.
245a0015d6SChris Zankel  */
255a0015d6SChris Zankel 
265a0015d6SChris Zankel #include <linux/kernel.h>
275a0015d6SChris Zankel #include <linux/sched.h>
285a0015d6SChris Zankel #include <linux/init.h>
295a0015d6SChris Zankel #include <linux/module.h>
305a0015d6SChris Zankel #include <linux/stringify.h>
315a0015d6SChris Zankel #include <linux/kallsyms.h>
325c888d53SNishanth Aravamudan #include <linux/delay.h>
335a891ed5SAlexey Dobriyan #include <linux/hardirq.h>
345a0015d6SChris Zankel 
355a0015d6SChris Zankel #include <asm/ptrace.h>
365a0015d6SChris Zankel #include <asm/timex.h>
375a0015d6SChris Zankel #include <asm/uaccess.h>
385a0015d6SChris Zankel #include <asm/pgtable.h>
395a0015d6SChris Zankel #include <asm/processor.h>
405a0015d6SChris Zankel 
415a0015d6SChris Zankel #ifdef CONFIG_KGDB
425a0015d6SChris Zankel extern int gdb_enter;
435a0015d6SChris Zankel extern int return_from_debug_flag;
445a0015d6SChris Zankel #endif
455a0015d6SChris Zankel 
465a0015d6SChris Zankel /*
475a0015d6SChris Zankel  * Machine specific interrupt handlers
485a0015d6SChris Zankel  */
495a0015d6SChris Zankel 
505a0015d6SChris Zankel extern void kernel_exception(void);
515a0015d6SChris Zankel extern void user_exception(void);
525a0015d6SChris Zankel 
535a0015d6SChris Zankel extern void fast_syscall_kernel(void);
545a0015d6SChris Zankel extern void fast_syscall_user(void);
555a0015d6SChris Zankel extern void fast_alloca(void);
565a0015d6SChris Zankel extern void fast_unaligned(void);
575a0015d6SChris Zankel extern void fast_second_level_miss(void);
585a0015d6SChris Zankel extern void fast_store_prohibited(void);
595a0015d6SChris Zankel extern void fast_coprocessor(void);
605a0015d6SChris Zankel 
615a0015d6SChris Zankel extern void do_illegal_instruction (struct pt_regs*);
625a0015d6SChris Zankel extern void do_interrupt (struct pt_regs*);
635a0015d6SChris Zankel extern void do_unaligned_user (struct pt_regs*);
645a0015d6SChris Zankel extern void do_multihit (struct pt_regs*, unsigned long);
655a0015d6SChris Zankel extern void do_page_fault (struct pt_regs*, unsigned long);
665a0015d6SChris Zankel extern void do_debug (struct pt_regs*);
675a0015d6SChris Zankel extern void system_call (struct pt_regs*);
685a0015d6SChris Zankel 
695a0015d6SChris Zankel /*
705a0015d6SChris Zankel  * The vector table must be preceded by a save area (which
715a0015d6SChris Zankel  * implies it must be in RAM, unless one places RAM immediately
725a0015d6SChris Zankel  * before a ROM and puts the vector at the start of the ROM (!))
735a0015d6SChris Zankel  */
745a0015d6SChris Zankel 
755a0015d6SChris Zankel #define KRNL		0x01
765a0015d6SChris Zankel #define USER		0x02
775a0015d6SChris Zankel 
785a0015d6SChris Zankel #define COPROCESSOR(x)							\
79173d6681SChris Zankel { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
805a0015d6SChris Zankel 
815a0015d6SChris Zankel typedef struct {
825a0015d6SChris Zankel 	int cause;
835a0015d6SChris Zankel 	int fast;
845a0015d6SChris Zankel 	void* handler;
855a0015d6SChris Zankel } dispatch_init_table_t;
865a0015d6SChris Zankel 
87b91dc336SChris Zankel static dispatch_init_table_t __initdata dispatch_init_table[] = {
885a0015d6SChris Zankel 
89173d6681SChris Zankel { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
90173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
91173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
92173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
93173d6681SChris Zankel /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
94173d6681SChris Zankel /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
95173d6681SChris Zankel { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
96173d6681SChris Zankel { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
97173d6681SChris Zankel /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
98173d6681SChris Zankel /* EXCCAUSE_PRIVILEGED unhandled */
995a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
1004ded6282SMax Filippov #ifdef CONFIG_XTENSA_UNALIGNED_USER
101173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
1025a0015d6SChris Zankel #else
103173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
1045a0015d6SChris Zankel #endif
105173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
1065a0015d6SChris Zankel #endif
107e5083a63SJohannes Weiner #ifdef CONFIG_MMU
108173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
109173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
110173d6681SChris Zankel { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
111173d6681SChris Zankel { EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
112173d6681SChris Zankel /* EXCCAUSE_SIZE_RESTRICTION unhandled */
113173d6681SChris Zankel { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
114173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
115173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
116173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
117173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
118173d6681SChris Zankel /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
119173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
120173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
121173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
122e5083a63SJohannes Weiner #endif /* CONFIG_MMU */
1235a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
124c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0)
1255a0015d6SChris Zankel COPROCESSOR(0),
1265a0015d6SChris Zankel #endif
127c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1)
1285a0015d6SChris Zankel COPROCESSOR(1),
1295a0015d6SChris Zankel #endif
130c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2)
1315a0015d6SChris Zankel COPROCESSOR(2),
1325a0015d6SChris Zankel #endif
133c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3)
1345a0015d6SChris Zankel COPROCESSOR(3),
1355a0015d6SChris Zankel #endif
136c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4)
1375a0015d6SChris Zankel COPROCESSOR(4),
1385a0015d6SChris Zankel #endif
139c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5)
1405a0015d6SChris Zankel COPROCESSOR(5),
1415a0015d6SChris Zankel #endif
142c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6)
1435a0015d6SChris Zankel COPROCESSOR(6),
1445a0015d6SChris Zankel #endif
145c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7)
1465a0015d6SChris Zankel COPROCESSOR(7),
1475a0015d6SChris Zankel #endif
1485a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
1495a0015d6SChris Zankel { -1, -1, 0 }
1505a0015d6SChris Zankel 
1515a0015d6SChris Zankel };
1525a0015d6SChris Zankel 
1535a0015d6SChris Zankel /* The exception table <exc_table> serves two functions:
1545a0015d6SChris Zankel  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
1555a0015d6SChris Zankel  * 2. it is a temporary memory buffer for the exception handlers.
1565a0015d6SChris Zankel  */
1575a0015d6SChris Zankel 
1585a0015d6SChris Zankel unsigned long exc_table[EXC_TABLE_SIZE/4];
1595a0015d6SChris Zankel 
1605a0015d6SChris Zankel void die(const char*, struct pt_regs*, long);
1615a0015d6SChris Zankel 
1625a0015d6SChris Zankel static inline void
1635a0015d6SChris Zankel __die_if_kernel(const char *str, struct pt_regs *regs, long err)
1645a0015d6SChris Zankel {
1655a0015d6SChris Zankel 	if (!user_mode(regs))
1665a0015d6SChris Zankel 		die(str, regs, err);
1675a0015d6SChris Zankel }
1685a0015d6SChris Zankel 
1695a0015d6SChris Zankel /*
1705a0015d6SChris Zankel  * Unhandled Exceptions. Kill user task or panic if in kernel space.
1715a0015d6SChris Zankel  */
1725a0015d6SChris Zankel 
1735a0015d6SChris Zankel void do_unhandled(struct pt_regs *regs, unsigned long exccause)
1745a0015d6SChris Zankel {
1755a0015d6SChris Zankel 	__die_if_kernel("Caught unhandled exception - should not happen",
1765a0015d6SChris Zankel 	    		regs, SIGKILL);
1775a0015d6SChris Zankel 
1785a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process */
1795a0015d6SChris Zankel 	printk("Caught unhandled exception in '%s' "
1805a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx) - should not happen\n"
1815a0015d6SChris Zankel 	       "\tEXCCAUSE is %ld\n",
18219c5870cSAlexey Dobriyan 	       current->comm, task_pid_nr(current), regs->pc, exccause);
1835a0015d6SChris Zankel 	force_sig(SIGILL, current);
1845a0015d6SChris Zankel }
1855a0015d6SChris Zankel 
1865a0015d6SChris Zankel /*
1875a0015d6SChris Zankel  * Multi-hit exception. This if fatal!
1885a0015d6SChris Zankel  */
1895a0015d6SChris Zankel 
1905a0015d6SChris Zankel void do_multihit(struct pt_regs *regs, unsigned long exccause)
1915a0015d6SChris Zankel {
1925a0015d6SChris Zankel 	die("Caught multihit exception", regs, SIGKILL);
1935a0015d6SChris Zankel }
1945a0015d6SChris Zankel 
1955a0015d6SChris Zankel /*
196*2d1c645cSMarc Gauthier  * IRQ handler.
197*2d1c645cSMarc Gauthier  * PS.INTLEVEL is the current IRQ priority level.
1985a0015d6SChris Zankel  */
1995a0015d6SChris Zankel 
2005a0015d6SChris Zankel extern void do_IRQ(int, struct pt_regs *);
2015a0015d6SChris Zankel 
2025a0015d6SChris Zankel void do_interrupt(struct pt_regs *regs)
2035a0015d6SChris Zankel {
204*2d1c645cSMarc Gauthier 	static const unsigned int_level_mask[] = {
205*2d1c645cSMarc Gauthier 		0,
206*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL1_MASK,
207*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL2_MASK,
208*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL3_MASK,
209*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL4_MASK,
210*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL5_MASK,
211*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL6_MASK,
212*2d1c645cSMarc Gauthier 		XCHAL_INTLEVEL7_MASK,
213*2d1c645cSMarc Gauthier 	};
214*2d1c645cSMarc Gauthier 	unsigned level = get_sr(ps) & PS_INTLEVEL_MASK;
2155a0015d6SChris Zankel 
216*2d1c645cSMarc Gauthier 	if (WARN_ON_ONCE(level >= ARRAY_SIZE(int_level_mask)))
217*2d1c645cSMarc Gauthier 		return;
218*2d1c645cSMarc Gauthier 
219*2d1c645cSMarc Gauthier 	for (;;) {
220*2d1c645cSMarc Gauthier 		unsigned intread = get_sr(interrupt);
221*2d1c645cSMarc Gauthier 		unsigned intenable = get_sr(intenable);
222*2d1c645cSMarc Gauthier 		unsigned int_at_level = intread & intenable &
223*2d1c645cSMarc Gauthier 			int_level_mask[level];
224*2d1c645cSMarc Gauthier 
225*2d1c645cSMarc Gauthier 		if (!int_at_level)
226*2d1c645cSMarc Gauthier 			return;
227*2d1c645cSMarc Gauthier 
228*2d1c645cSMarc Gauthier 		/*
229*2d1c645cSMarc Gauthier 		 * Clear the interrupt before processing, in case it's
230*2d1c645cSMarc Gauthier 		 *  edge-triggered or software-generated
2315a0015d6SChris Zankel 		 */
232*2d1c645cSMarc Gauthier 		while (int_at_level) {
233*2d1c645cSMarc Gauthier 			unsigned i = __ffs(int_at_level);
234*2d1c645cSMarc Gauthier 			unsigned mask = 1 << i;
2355a0015d6SChris Zankel 
236*2d1c645cSMarc Gauthier 			int_at_level ^= mask;
237bc5378fcSMax Filippov 			set_sr(mask, intclear);
2385a0015d6SChris Zankel 			do_IRQ(i, regs);
2395a0015d6SChris Zankel 		}
2405a0015d6SChris Zankel 	}
2415a0015d6SChris Zankel }
2425a0015d6SChris Zankel 
2435a0015d6SChris Zankel /*
2445a0015d6SChris Zankel  * Illegal instruction. Fatal if in kernel space.
2455a0015d6SChris Zankel  */
2465a0015d6SChris Zankel 
2475a0015d6SChris Zankel void
2485a0015d6SChris Zankel do_illegal_instruction(struct pt_regs *regs)
2495a0015d6SChris Zankel {
2505a0015d6SChris Zankel 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
2515a0015d6SChris Zankel 
2525a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process. */
2535a0015d6SChris Zankel 
2545a0015d6SChris Zankel 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
25519c5870cSAlexey Dobriyan 	    current->comm, task_pid_nr(current), regs->pc);
2565a0015d6SChris Zankel 	force_sig(SIGILL, current);
2575a0015d6SChris Zankel }
2585a0015d6SChris Zankel 
2595a0015d6SChris Zankel 
2605a0015d6SChris Zankel /*
2615a0015d6SChris Zankel  * Handle unaligned memory accesses from user space. Kill task.
2625a0015d6SChris Zankel  *
2635a0015d6SChris Zankel  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
2645a0015d6SChris Zankel  * accesses causes from user space.
2655a0015d6SChris Zankel  */
2665a0015d6SChris Zankel 
2675a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
2684ded6282SMax Filippov #ifndef CONFIG_XTENSA_UNALIGNED_USER
2695a0015d6SChris Zankel void
2705a0015d6SChris Zankel do_unaligned_user (struct pt_regs *regs)
2715a0015d6SChris Zankel {
2725a0015d6SChris Zankel 	siginfo_t info;
2735a0015d6SChris Zankel 
2745a0015d6SChris Zankel 	__die_if_kernel("Unhandled unaligned exception in kernel",
2755a0015d6SChris Zankel 	    		regs, SIGKILL);
2765a0015d6SChris Zankel 
2775a0015d6SChris Zankel 	current->thread.bad_vaddr = regs->excvaddr;
2785a0015d6SChris Zankel 	current->thread.error_code = -3;
2795a0015d6SChris Zankel 	printk("Unaligned memory access to %08lx in '%s' "
2805a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx)\n",
28119c5870cSAlexey Dobriyan 	       regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
2825a0015d6SChris Zankel 	info.si_signo = SIGBUS;
2835a0015d6SChris Zankel 	info.si_errno = 0;
2845a0015d6SChris Zankel 	info.si_code = BUS_ADRALN;
2855a0015d6SChris Zankel 	info.si_addr = (void *) regs->excvaddr;
2865a0015d6SChris Zankel 	force_sig_info(SIGSEGV, &info, current);
2875a0015d6SChris Zankel 
2885a0015d6SChris Zankel }
2895a0015d6SChris Zankel #endif
2905a0015d6SChris Zankel #endif
2915a0015d6SChris Zankel 
2925a0015d6SChris Zankel void
2935a0015d6SChris Zankel do_debug(struct pt_regs *regs)
2945a0015d6SChris Zankel {
2955a0015d6SChris Zankel #ifdef CONFIG_KGDB
2965a0015d6SChris Zankel 	/* If remote debugging is configured AND enabled, we give control to
2975a0015d6SChris Zankel 	 * kgdb.  Otherwise, we fall through, perhaps giving control to the
2985a0015d6SChris Zankel 	 * native debugger.
2995a0015d6SChris Zankel 	 */
3005a0015d6SChris Zankel 
3015a0015d6SChris Zankel 	if (gdb_enter) {
3025a0015d6SChris Zankel 		extern void gdb_handle_exception(struct pt_regs *);
3035a0015d6SChris Zankel 		gdb_handle_exception(regs);
3045a0015d6SChris Zankel 		return_from_debug_flag = 1;
3055a0015d6SChris Zankel 		return;
3065a0015d6SChris Zankel 	}
3075a0015d6SChris Zankel #endif
3085a0015d6SChris Zankel 
3095a0015d6SChris Zankel 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
3105a0015d6SChris Zankel 
3115a0015d6SChris Zankel 	/* If in user mode, send SIGTRAP signal to current process */
3125a0015d6SChris Zankel 
3135a0015d6SChris Zankel 	force_sig(SIGTRAP, current);
3145a0015d6SChris Zankel }
3155a0015d6SChris Zankel 
3165a0015d6SChris Zankel 
31728570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */
31828570e8dSMax Filippov 
31928570e8dSMax Filippov void * __init trap_set_handler(int cause, void *handler)
32028570e8dSMax Filippov {
32128570e8dSMax Filippov 	unsigned long *entry = &exc_table[EXC_TABLE_DEFAULT / 4 + cause];
32228570e8dSMax Filippov 	void *previous = (void *)*entry;
32328570e8dSMax Filippov 	*entry = (unsigned long)handler;
32428570e8dSMax Filippov 	return previous;
32528570e8dSMax Filippov }
32628570e8dSMax Filippov 
32728570e8dSMax Filippov 
3285a0015d6SChris Zankel /*
3295a0015d6SChris Zankel  * Initialize dispatch tables.
3305a0015d6SChris Zankel  *
3315a0015d6SChris Zankel  * The exception vectors are stored compressed the __init section in the
3325a0015d6SChris Zankel  * dispatch_init_table. This function initializes the following three tables
3335a0015d6SChris Zankel  * from that compressed table:
3345a0015d6SChris Zankel  * - fast user		first dispatch table for user exceptions
3355a0015d6SChris Zankel  * - fast kernel	first dispatch table for kernel exceptions
3365a0015d6SChris Zankel  * - default C-handler	C-handler called by the default fast handler.
3375a0015d6SChris Zankel  *
3385a0015d6SChris Zankel  * See vectors.S for more details.
3395a0015d6SChris Zankel  */
3405a0015d6SChris Zankel 
3415a0015d6SChris Zankel #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
3425a0015d6SChris Zankel 
343b91dc336SChris Zankel void __init trap_init(void)
3445a0015d6SChris Zankel {
3455a0015d6SChris Zankel 	int i;
3465a0015d6SChris Zankel 
3475a0015d6SChris Zankel 	/* Setup default vectors. */
3485a0015d6SChris Zankel 
3495a0015d6SChris Zankel 	for(i = 0; i < 64; i++) {
3505a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
3515a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
3525a0015d6SChris Zankel 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
3535a0015d6SChris Zankel 	}
3545a0015d6SChris Zankel 
3555a0015d6SChris Zankel 	/* Setup specific handlers. */
3565a0015d6SChris Zankel 
3575a0015d6SChris Zankel 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
3585a0015d6SChris Zankel 
3595a0015d6SChris Zankel 		int fast = dispatch_init_table[i].fast;
3605a0015d6SChris Zankel 		int cause = dispatch_init_table[i].cause;
3615a0015d6SChris Zankel 		void *handler = dispatch_init_table[i].handler;
3625a0015d6SChris Zankel 
3635a0015d6SChris Zankel 		if (fast == 0)
3645a0015d6SChris Zankel 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
3655a0015d6SChris Zankel 		if (fast && fast & USER)
3665a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
3675a0015d6SChris Zankel 		if (fast && fast & KRNL)
3685a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
3695a0015d6SChris Zankel 	}
3705a0015d6SChris Zankel 
3715a0015d6SChris Zankel 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
3725a0015d6SChris Zankel 
3735a0015d6SChris Zankel 	i = (unsigned long)exc_table;
374bc5378fcSMax Filippov 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (i));
3755a0015d6SChris Zankel }
3765a0015d6SChris Zankel 
3775a0015d6SChris Zankel /*
3785a0015d6SChris Zankel  * This function dumps the current valid window frame and other base registers.
3795a0015d6SChris Zankel  */
3805a0015d6SChris Zankel 
3815a0015d6SChris Zankel void show_regs(struct pt_regs * regs)
3825a0015d6SChris Zankel {
3835a0015d6SChris Zankel 	int i, wmask;
3845a0015d6SChris Zankel 
3855a0015d6SChris Zankel 	wmask = regs->wmask & ~1;
3865a0015d6SChris Zankel 
3878d7e8240SChris Zankel 	for (i = 0; i < 16; i++) {
3885a0015d6SChris Zankel 		if ((i % 8) == 0)
389ad361c98SJoe Perches 			printk(KERN_INFO "a%02d:", i);
390ad361c98SJoe Perches 		printk(KERN_CONT " %08lx", regs->areg[i]);
3915a0015d6SChris Zankel 	}
392ad361c98SJoe Perches 	printk(KERN_CONT "\n");
3935a0015d6SChris Zankel 
3945a0015d6SChris Zankel 	printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
3955a0015d6SChris Zankel 	       regs->pc, regs->ps, regs->depc, regs->excvaddr);
3965a0015d6SChris Zankel 	printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
3975a0015d6SChris Zankel 	       regs->lbeg, regs->lend, regs->lcount, regs->sar);
3985a0015d6SChris Zankel 	if (user_mode(regs))
3995a0015d6SChris Zankel 		printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
4005a0015d6SChris Zankel 		       regs->windowbase, regs->windowstart, regs->wmask,
4015a0015d6SChris Zankel 		       regs->syscall);
4025a0015d6SChris Zankel }
4035a0015d6SChris Zankel 
404586411dcSJohannes Weiner static __always_inline unsigned long *stack_pointer(struct task_struct *task)
405586411dcSJohannes Weiner {
406586411dcSJohannes Weiner 	unsigned long *sp;
407586411dcSJohannes Weiner 
408586411dcSJohannes Weiner 	if (!task || task == current)
409586411dcSJohannes Weiner 		__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
410586411dcSJohannes Weiner 	else
411586411dcSJohannes Weiner 		sp = (unsigned long *)task->thread.sp;
412586411dcSJohannes Weiner 
413586411dcSJohannes Weiner 	return sp;
414586411dcSJohannes Weiner }
415586411dcSJohannes Weiner 
416f9aa7e18SDavid Howells static inline void spill_registers(void)
417f9aa7e18SDavid Howells {
418f9aa7e18SDavid Howells 	unsigned int a0, ps;
419f9aa7e18SDavid Howells 
420f9aa7e18SDavid Howells 	__asm__ __volatile__ (
421*2d1c645cSMarc Gauthier 		"movi	a14, " __stringify(PS_EXCM_BIT | LOCKLEVEL) "\n\t"
422f9aa7e18SDavid Howells 		"mov	a12, a0\n\t"
423bc5378fcSMax Filippov 		"rsr	a13, sar\n\t"
424bc5378fcSMax Filippov 		"xsr	a14, ps\n\t"
425f9aa7e18SDavid Howells 		"movi	a0, _spill_registers\n\t"
426f9aa7e18SDavid Howells 		"rsync\n\t"
427f9aa7e18SDavid Howells 		"callx0 a0\n\t"
428f9aa7e18SDavid Howells 		"mov	a0, a12\n\t"
429bc5378fcSMax Filippov 		"wsr	a13, sar\n\t"
430bc5378fcSMax Filippov 		"wsr	a14, ps\n\t"
431f9aa7e18SDavid Howells 		:: "a" (&a0), "a" (&ps)
432c4c4594bSChris Zankel 		: "a2", "a3", "a4", "a7", "a11", "a12", "a13", "a14", "a15",
433c4c4594bSChris Zankel 		  "memory");
434f9aa7e18SDavid Howells }
435f9aa7e18SDavid Howells 
4365a0015d6SChris Zankel void show_trace(struct task_struct *task, unsigned long *sp)
4375a0015d6SChris Zankel {
4385a0015d6SChris Zankel 	unsigned long a0, a1, pc;
4395a0015d6SChris Zankel 	unsigned long sp_start, sp_end;
4405a0015d6SChris Zankel 
44128a0ce7fSJohannes Weiner 	if (sp)
4425a0015d6SChris Zankel 		a1 = (unsigned long)sp;
44328a0ce7fSJohannes Weiner 	else
444586411dcSJohannes Weiner 		a1 = (unsigned long)stack_pointer(task);
4455a0015d6SChris Zankel 
4465a0015d6SChris Zankel 	sp_start = a1 & ~(THREAD_SIZE-1);
4475a0015d6SChris Zankel 	sp_end = sp_start + THREAD_SIZE;
4485a0015d6SChris Zankel 
4495a0015d6SChris Zankel 	printk("Call Trace:");
4505a0015d6SChris Zankel #ifdef CONFIG_KALLSYMS
4515a0015d6SChris Zankel 	printk("\n");
4525a0015d6SChris Zankel #endif
4535a0015d6SChris Zankel 	spill_registers();
4545a0015d6SChris Zankel 
4555a0015d6SChris Zankel 	while (a1 > sp_start && a1 < sp_end) {
4565a0015d6SChris Zankel 		sp = (unsigned long*)a1;
4575a0015d6SChris Zankel 
4585a0015d6SChris Zankel 		a0 = *(sp - 4);
4595a0015d6SChris Zankel 		a1 = *(sp - 3);
4605a0015d6SChris Zankel 
4615a0015d6SChris Zankel 		if (a1 <= (unsigned long) sp)
4625a0015d6SChris Zankel 			break;
4635a0015d6SChris Zankel 
4645a0015d6SChris Zankel 		pc = MAKE_PC_FROM_RA(a0, a1);
4655a0015d6SChris Zankel 
4665a0015d6SChris Zankel 		if (kernel_text_address(pc)) {
4675a0015d6SChris Zankel 			printk(" [<%08lx>] ", pc);
4685a0015d6SChris Zankel 			print_symbol("%s\n", pc);
4695a0015d6SChris Zankel 		}
4705a0015d6SChris Zankel 	}
4715a0015d6SChris Zankel 	printk("\n");
4725a0015d6SChris Zankel }
4735a0015d6SChris Zankel 
4745a0015d6SChris Zankel /*
4755a0015d6SChris Zankel  * This routine abuses get_user()/put_user() to reference pointers
4765a0015d6SChris Zankel  * with at least a bit of error checking ...
4775a0015d6SChris Zankel  */
4785a0015d6SChris Zankel 
4795a0015d6SChris Zankel static int kstack_depth_to_print = 24;
4805a0015d6SChris Zankel 
4815a0015d6SChris Zankel void show_stack(struct task_struct *task, unsigned long *sp)
4825a0015d6SChris Zankel {
4835a0015d6SChris Zankel 	int i = 0;
4845a0015d6SChris Zankel 	unsigned long *stack;
4855a0015d6SChris Zankel 
48628a0ce7fSJohannes Weiner 	if (!sp)
487586411dcSJohannes Weiner 		sp = stack_pointer(task);
4885a0015d6SChris Zankel 	stack = sp;
4895a0015d6SChris Zankel 
4905a0015d6SChris Zankel 	printk("\nStack: ");
4915a0015d6SChris Zankel 
4925a0015d6SChris Zankel 	for (i = 0; i < kstack_depth_to_print; i++) {
4935a0015d6SChris Zankel 		if (kstack_end(sp))
4945a0015d6SChris Zankel 			break;
4955a0015d6SChris Zankel 		if (i && ((i % 8) == 0))
4965a0015d6SChris Zankel 			printk("\n       ");
4975a0015d6SChris Zankel 		printk("%08lx ", *sp++);
4985a0015d6SChris Zankel 	}
4995a0015d6SChris Zankel 	printk("\n");
5005a0015d6SChris Zankel 	show_trace(task, stack);
5015a0015d6SChris Zankel }
5025a0015d6SChris Zankel 
5035a0015d6SChris Zankel void dump_stack(void)
5045a0015d6SChris Zankel {
5055a0015d6SChris Zankel 	show_stack(current, NULL);
5065a0015d6SChris Zankel }
5075a0015d6SChris Zankel 
5085a0015d6SChris Zankel EXPORT_SYMBOL(dump_stack);
5095a0015d6SChris Zankel 
5105a0015d6SChris Zankel 
5115a0015d6SChris Zankel void show_code(unsigned int *pc)
5125a0015d6SChris Zankel {
5135a0015d6SChris Zankel 	long i;
5145a0015d6SChris Zankel 
5155a0015d6SChris Zankel 	printk("\nCode:");
5165a0015d6SChris Zankel 
5175a0015d6SChris Zankel 	for(i = -3 ; i < 6 ; i++) {
5185a0015d6SChris Zankel 		unsigned long insn;
5195a0015d6SChris Zankel 		if (__get_user(insn, pc + i)) {
5205a0015d6SChris Zankel 			printk(" (Bad address in pc)\n");
5215a0015d6SChris Zankel 			break;
5225a0015d6SChris Zankel 		}
5235a0015d6SChris Zankel 		printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
5245a0015d6SChris Zankel 	}
5255a0015d6SChris Zankel }
5265a0015d6SChris Zankel 
52734af946aSIngo Molnar DEFINE_SPINLOCK(die_lock);
5285a0015d6SChris Zankel 
5295a0015d6SChris Zankel void die(const char * str, struct pt_regs * regs, long err)
5305a0015d6SChris Zankel {
5315a0015d6SChris Zankel 	static int die_counter;
5325a0015d6SChris Zankel 	int nl = 0;
5335a0015d6SChris Zankel 
5345a0015d6SChris Zankel 	console_verbose();
5355a0015d6SChris Zankel 	spin_lock_irq(&die_lock);
5365a0015d6SChris Zankel 
5375a0015d6SChris Zankel 	printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
5385a0015d6SChris Zankel #ifdef CONFIG_PREEMPT
5395a0015d6SChris Zankel 	printk("PREEMPT ");
5405a0015d6SChris Zankel 	nl = 1;
5415a0015d6SChris Zankel #endif
5425a0015d6SChris Zankel 	if (nl)
5435a0015d6SChris Zankel 		printk("\n");
5445a0015d6SChris Zankel 	show_regs(regs);
5455a0015d6SChris Zankel 	if (!user_mode(regs))
5465a0015d6SChris Zankel 		show_stack(NULL, (unsigned long*)regs->areg[1]);
5475a0015d6SChris Zankel 
548bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
5495a0015d6SChris Zankel 	spin_unlock_irq(&die_lock);
5505a0015d6SChris Zankel 
5515a0015d6SChris Zankel 	if (in_interrupt())
5525a0015d6SChris Zankel 		panic("Fatal exception in interrupt");
5535a0015d6SChris Zankel 
554cea6a4baSHorms 	if (panic_on_oops)
555012c437dSHorms 		panic("Fatal exception");
556cea6a4baSHorms 
5575a0015d6SChris Zankel 	do_exit(err);
5585a0015d6SChris Zankel }
559