xref: /openbmc/linux/arch/xtensa/kernel/traps.c (revision 38fef73c21d117cf992fb5ec6e30630e54e13f4f)
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  *
143e4196a5SMax Filippov  * Copyright (C) 2001 - 2013 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 
353e4196a5SMax Filippov #include <asm/stacktrace.h>
365a0015d6SChris Zankel #include <asm/ptrace.h>
375a0015d6SChris Zankel #include <asm/timex.h>
385a0015d6SChris Zankel #include <asm/uaccess.h>
395a0015d6SChris Zankel #include <asm/pgtable.h>
405a0015d6SChris Zankel #include <asm/processor.h>
412d6f82feSMax Filippov #include <asm/traps.h>
425a0015d6SChris Zankel 
435a0015d6SChris Zankel #ifdef CONFIG_KGDB
445a0015d6SChris Zankel extern int gdb_enter;
455a0015d6SChris Zankel extern int return_from_debug_flag;
465a0015d6SChris Zankel #endif
475a0015d6SChris Zankel 
485a0015d6SChris Zankel /*
495a0015d6SChris Zankel  * Machine specific interrupt handlers
505a0015d6SChris Zankel  */
515a0015d6SChris Zankel 
525a0015d6SChris Zankel extern void kernel_exception(void);
535a0015d6SChris Zankel extern void user_exception(void);
545a0015d6SChris Zankel 
555a0015d6SChris Zankel extern void fast_syscall_kernel(void);
565a0015d6SChris Zankel extern void fast_syscall_user(void);
575a0015d6SChris Zankel extern void fast_alloca(void);
585a0015d6SChris Zankel extern void fast_unaligned(void);
595a0015d6SChris Zankel extern void fast_second_level_miss(void);
605a0015d6SChris Zankel extern void fast_store_prohibited(void);
615a0015d6SChris Zankel extern void fast_coprocessor(void);
625a0015d6SChris Zankel 
635a0015d6SChris Zankel extern void do_illegal_instruction (struct pt_regs*);
645a0015d6SChris Zankel extern void do_interrupt (struct pt_regs*);
65*38fef73cSMax Filippov extern void do_nmi(struct pt_regs *);
665a0015d6SChris Zankel extern void do_unaligned_user (struct pt_regs*);
675a0015d6SChris Zankel extern void do_multihit (struct pt_regs*, unsigned long);
685a0015d6SChris Zankel extern void do_page_fault (struct pt_regs*, unsigned long);
695a0015d6SChris Zankel extern void do_debug (struct pt_regs*);
705a0015d6SChris Zankel extern void system_call (struct pt_regs*);
715a0015d6SChris Zankel 
725a0015d6SChris Zankel /*
735a0015d6SChris Zankel  * The vector table must be preceded by a save area (which
745a0015d6SChris Zankel  * implies it must be in RAM, unless one places RAM immediately
755a0015d6SChris Zankel  * before a ROM and puts the vector at the start of the ROM (!))
765a0015d6SChris Zankel  */
775a0015d6SChris Zankel 
785a0015d6SChris Zankel #define KRNL		0x01
795a0015d6SChris Zankel #define USER		0x02
805a0015d6SChris Zankel 
815a0015d6SChris Zankel #define COPROCESSOR(x)							\
82173d6681SChris Zankel { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
835a0015d6SChris Zankel 
845a0015d6SChris Zankel typedef struct {
855a0015d6SChris Zankel 	int cause;
865a0015d6SChris Zankel 	int fast;
875a0015d6SChris Zankel 	void* handler;
885a0015d6SChris Zankel } dispatch_init_table_t;
895a0015d6SChris Zankel 
90b91dc336SChris Zankel static dispatch_init_table_t __initdata dispatch_init_table[] = {
915a0015d6SChris Zankel 
92173d6681SChris Zankel { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
93173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
94173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
95173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
96173d6681SChris Zankel /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
97173d6681SChris Zankel /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
98173d6681SChris Zankel { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
99173d6681SChris Zankel { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
100173d6681SChris Zankel /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
101173d6681SChris Zankel /* EXCCAUSE_PRIVILEGED unhandled */
1025a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
1034ded6282SMax Filippov #ifdef CONFIG_XTENSA_UNALIGNED_USER
104173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
1055a0015d6SChris Zankel #endif
1063cfc096eSMax Filippov { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
107173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
1085a0015d6SChris Zankel #endif
109e5083a63SJohannes Weiner #ifdef CONFIG_MMU
110173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
111173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
112173d6681SChris Zankel { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
113173d6681SChris Zankel { EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
114173d6681SChris Zankel /* EXCCAUSE_SIZE_RESTRICTION unhandled */
115173d6681SChris Zankel { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
116173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
117173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
118173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
119173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
120173d6681SChris Zankel /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
121173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
122173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
123173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
124e5083a63SJohannes Weiner #endif /* CONFIG_MMU */
1255a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
126c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0)
1275a0015d6SChris Zankel COPROCESSOR(0),
1285a0015d6SChris Zankel #endif
129c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1)
1305a0015d6SChris Zankel COPROCESSOR(1),
1315a0015d6SChris Zankel #endif
132c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2)
1335a0015d6SChris Zankel COPROCESSOR(2),
1345a0015d6SChris Zankel #endif
135c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3)
1365a0015d6SChris Zankel COPROCESSOR(3),
1375a0015d6SChris Zankel #endif
138c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4)
1395a0015d6SChris Zankel COPROCESSOR(4),
1405a0015d6SChris Zankel #endif
141c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5)
1425a0015d6SChris Zankel COPROCESSOR(5),
1435a0015d6SChris Zankel #endif
144c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6)
1455a0015d6SChris Zankel COPROCESSOR(6),
1465a0015d6SChris Zankel #endif
147c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7)
1485a0015d6SChris Zankel COPROCESSOR(7),
1495a0015d6SChris Zankel #endif
150*38fef73cSMax Filippov #if XTENSA_FAKE_NMI
151*38fef73cSMax Filippov { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
152*38fef73cSMax Filippov #endif
1535a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
1545a0015d6SChris Zankel { -1, -1, 0 }
1555a0015d6SChris Zankel 
1565a0015d6SChris Zankel };
1575a0015d6SChris Zankel 
1585a0015d6SChris Zankel /* The exception table <exc_table> serves two functions:
1595a0015d6SChris Zankel  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
1605a0015d6SChris Zankel  * 2. it is a temporary memory buffer for the exception handlers.
1615a0015d6SChris Zankel  */
1625a0015d6SChris Zankel 
163f615136cSMax Filippov DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);
1645a0015d6SChris Zankel 
1655a0015d6SChris Zankel void die(const char*, struct pt_regs*, long);
1665a0015d6SChris Zankel 
1675a0015d6SChris Zankel static inline void
1685a0015d6SChris Zankel __die_if_kernel(const char *str, struct pt_regs *regs, long err)
1695a0015d6SChris Zankel {
1705a0015d6SChris Zankel 	if (!user_mode(regs))
1715a0015d6SChris Zankel 		die(str, regs, err);
1725a0015d6SChris Zankel }
1735a0015d6SChris Zankel 
1745a0015d6SChris Zankel /*
1755a0015d6SChris Zankel  * Unhandled Exceptions. Kill user task or panic if in kernel space.
1765a0015d6SChris Zankel  */
1775a0015d6SChris Zankel 
1785a0015d6SChris Zankel void do_unhandled(struct pt_regs *regs, unsigned long exccause)
1795a0015d6SChris Zankel {
1805a0015d6SChris Zankel 	__die_if_kernel("Caught unhandled exception - should not happen",
1815a0015d6SChris Zankel 	    		regs, SIGKILL);
1825a0015d6SChris Zankel 
1835a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process */
1845a0015d6SChris Zankel 	printk("Caught unhandled exception in '%s' "
1855a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx) - should not happen\n"
1865a0015d6SChris Zankel 	       "\tEXCCAUSE is %ld\n",
18719c5870cSAlexey Dobriyan 	       current->comm, task_pid_nr(current), regs->pc, exccause);
1885a0015d6SChris Zankel 	force_sig(SIGILL, current);
1895a0015d6SChris Zankel }
1905a0015d6SChris Zankel 
1915a0015d6SChris Zankel /*
1925a0015d6SChris Zankel  * Multi-hit exception. This if fatal!
1935a0015d6SChris Zankel  */
1945a0015d6SChris Zankel 
1955a0015d6SChris Zankel void do_multihit(struct pt_regs *regs, unsigned long exccause)
1965a0015d6SChris Zankel {
1975a0015d6SChris Zankel 	die("Caught multihit exception", regs, SIGKILL);
1985a0015d6SChris Zankel }
1995a0015d6SChris Zankel 
2005a0015d6SChris Zankel /*
2012d1c645cSMarc Gauthier  * IRQ handler.
2025a0015d6SChris Zankel  */
2035a0015d6SChris Zankel 
2045a0015d6SChris Zankel extern void do_IRQ(int, struct pt_regs *);
2055a0015d6SChris Zankel 
206*38fef73cSMax Filippov #if XTENSA_FAKE_NMI
207*38fef73cSMax Filippov 
208*38fef73cSMax Filippov irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
209*38fef73cSMax Filippov 
210*38fef73cSMax Filippov DEFINE_PER_CPU(unsigned long, nmi_count);
211*38fef73cSMax Filippov 
212*38fef73cSMax Filippov void do_nmi(struct pt_regs *regs)
213*38fef73cSMax Filippov {
214*38fef73cSMax Filippov 	struct pt_regs *old_regs;
215*38fef73cSMax Filippov 
216*38fef73cSMax Filippov 	if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
217*38fef73cSMax Filippov 		trace_hardirqs_off();
218*38fef73cSMax Filippov 
219*38fef73cSMax Filippov 	old_regs = set_irq_regs(regs);
220*38fef73cSMax Filippov 	nmi_enter();
221*38fef73cSMax Filippov 	++*this_cpu_ptr(&nmi_count);
222*38fef73cSMax Filippov 	xtensa_pmu_irq_handler(0, NULL);
223*38fef73cSMax Filippov 	nmi_exit();
224*38fef73cSMax Filippov 	set_irq_regs(old_regs);
225*38fef73cSMax Filippov }
226*38fef73cSMax Filippov #endif
227*38fef73cSMax Filippov 
2285a0015d6SChris Zankel void do_interrupt(struct pt_regs *regs)
2295a0015d6SChris Zankel {
2302d1c645cSMarc Gauthier 	static const unsigned int_level_mask[] = {
2312d1c645cSMarc Gauthier 		0,
2322d1c645cSMarc Gauthier 		XCHAL_INTLEVEL1_MASK,
2332d1c645cSMarc Gauthier 		XCHAL_INTLEVEL2_MASK,
2342d1c645cSMarc Gauthier 		XCHAL_INTLEVEL3_MASK,
2352d1c645cSMarc Gauthier 		XCHAL_INTLEVEL4_MASK,
2362d1c645cSMarc Gauthier 		XCHAL_INTLEVEL5_MASK,
2372d1c645cSMarc Gauthier 		XCHAL_INTLEVEL6_MASK,
2382d1c645cSMarc Gauthier 		XCHAL_INTLEVEL7_MASK,
2392d1c645cSMarc Gauthier 	};
2407d5f6a9aSMax Filippov 	struct pt_regs *old_regs;
24199623239SMax Filippov 
2427d5f6a9aSMax Filippov 	trace_hardirqs_off();
2437d5f6a9aSMax Filippov 
2447d5f6a9aSMax Filippov 	old_regs = set_irq_regs(regs);
24599623239SMax Filippov 	irq_enter();
2462d1c645cSMarc Gauthier 
2472d1c645cSMarc Gauthier 	for (;;) {
2482d1c645cSMarc Gauthier 		unsigned intread = get_sr(interrupt);
2492d1c645cSMarc Gauthier 		unsigned intenable = get_sr(intenable);
250895666a9SMax Filippov 		unsigned int_at_level = intread & intenable;
251895666a9SMax Filippov 		unsigned level;
2522d1c645cSMarc Gauthier 
253895666a9SMax Filippov 		for (level = LOCKLEVEL; level > 0; --level) {
254895666a9SMax Filippov 			if (int_at_level & int_level_mask[level]) {
255895666a9SMax Filippov 				int_at_level &= int_level_mask[level];
256895666a9SMax Filippov 				break;
257895666a9SMax Filippov 			}
258895666a9SMax Filippov 		}
259895666a9SMax Filippov 
260895666a9SMax Filippov 		if (level == 0)
26199623239SMax Filippov 			break;
2622d1c645cSMarc Gauthier 
26399623239SMax Filippov 		do_IRQ(__ffs(int_at_level), regs);
26499623239SMax Filippov 	}
2655a0015d6SChris Zankel 
26699623239SMax Filippov 	irq_exit();
26799623239SMax Filippov 	set_irq_regs(old_regs);
2685a0015d6SChris Zankel }
2695a0015d6SChris Zankel 
2705a0015d6SChris Zankel /*
2715a0015d6SChris Zankel  * Illegal instruction. Fatal if in kernel space.
2725a0015d6SChris Zankel  */
2735a0015d6SChris Zankel 
2745a0015d6SChris Zankel void
2755a0015d6SChris Zankel do_illegal_instruction(struct pt_regs *regs)
2765a0015d6SChris Zankel {
2775a0015d6SChris Zankel 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
2785a0015d6SChris Zankel 
2795a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process. */
2805a0015d6SChris Zankel 
2815a0015d6SChris Zankel 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
28219c5870cSAlexey Dobriyan 	    current->comm, task_pid_nr(current), regs->pc);
2835a0015d6SChris Zankel 	force_sig(SIGILL, current);
2845a0015d6SChris Zankel }
2855a0015d6SChris Zankel 
2865a0015d6SChris Zankel 
2875a0015d6SChris Zankel /*
2885a0015d6SChris Zankel  * Handle unaligned memory accesses from user space. Kill task.
2895a0015d6SChris Zankel  *
2905a0015d6SChris Zankel  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
2915a0015d6SChris Zankel  * accesses causes from user space.
2925a0015d6SChris Zankel  */
2935a0015d6SChris Zankel 
2945a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
2955a0015d6SChris Zankel void
2965a0015d6SChris Zankel do_unaligned_user (struct pt_regs *regs)
2975a0015d6SChris Zankel {
2985a0015d6SChris Zankel 	siginfo_t info;
2995a0015d6SChris Zankel 
3005a0015d6SChris Zankel 	__die_if_kernel("Unhandled unaligned exception in kernel",
3015a0015d6SChris Zankel 	    		regs, SIGKILL);
3025a0015d6SChris Zankel 
3035a0015d6SChris Zankel 	current->thread.bad_vaddr = regs->excvaddr;
3045a0015d6SChris Zankel 	current->thread.error_code = -3;
3055a0015d6SChris Zankel 	printk("Unaligned memory access to %08lx in '%s' "
3065a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx)\n",
30719c5870cSAlexey Dobriyan 	       regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
3085a0015d6SChris Zankel 	info.si_signo = SIGBUS;
3095a0015d6SChris Zankel 	info.si_errno = 0;
3105a0015d6SChris Zankel 	info.si_code = BUS_ADRALN;
3115a0015d6SChris Zankel 	info.si_addr = (void *) regs->excvaddr;
3125a0015d6SChris Zankel 	force_sig_info(SIGSEGV, &info, current);
3135a0015d6SChris Zankel 
3145a0015d6SChris Zankel }
3155a0015d6SChris Zankel #endif
3165a0015d6SChris Zankel 
3175a0015d6SChris Zankel void
3185a0015d6SChris Zankel do_debug(struct pt_regs *regs)
3195a0015d6SChris Zankel {
3205a0015d6SChris Zankel #ifdef CONFIG_KGDB
3215a0015d6SChris Zankel 	/* If remote debugging is configured AND enabled, we give control to
3225a0015d6SChris Zankel 	 * kgdb.  Otherwise, we fall through, perhaps giving control to the
3235a0015d6SChris Zankel 	 * native debugger.
3245a0015d6SChris Zankel 	 */
3255a0015d6SChris Zankel 
3265a0015d6SChris Zankel 	if (gdb_enter) {
3275a0015d6SChris Zankel 		extern void gdb_handle_exception(struct pt_regs *);
3285a0015d6SChris Zankel 		gdb_handle_exception(regs);
3295a0015d6SChris Zankel 		return_from_debug_flag = 1;
3305a0015d6SChris Zankel 		return;
3315a0015d6SChris Zankel 	}
3325a0015d6SChris Zankel #endif
3335a0015d6SChris Zankel 
3345a0015d6SChris Zankel 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
3355a0015d6SChris Zankel 
3365a0015d6SChris Zankel 	/* If in user mode, send SIGTRAP signal to current process */
3375a0015d6SChris Zankel 
3385a0015d6SChris Zankel 	force_sig(SIGTRAP, current);
3395a0015d6SChris Zankel }
3405a0015d6SChris Zankel 
3415a0015d6SChris Zankel 
342f615136cSMax Filippov static void set_handler(int idx, void *handler)
343f615136cSMax Filippov {
344f615136cSMax Filippov 	unsigned int cpu;
345f615136cSMax Filippov 
346f615136cSMax Filippov 	for_each_possible_cpu(cpu)
347f615136cSMax Filippov 		per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
348f615136cSMax Filippov }
349f615136cSMax Filippov 
35028570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */
35128570e8dSMax Filippov 
35228570e8dSMax Filippov void * __init trap_set_handler(int cause, void *handler)
35328570e8dSMax Filippov {
354f615136cSMax Filippov 	void *previous = (void *)per_cpu(exc_table, 0)[
355f615136cSMax Filippov 		EXC_TABLE_DEFAULT / 4 + cause];
356f615136cSMax Filippov 	set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
35728570e8dSMax Filippov 	return previous;
35828570e8dSMax Filippov }
35928570e8dSMax Filippov 
36028570e8dSMax Filippov 
36149b424feSMax Filippov static void trap_init_excsave(void)
362f615136cSMax Filippov {
363f615136cSMax Filippov 	unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
364f615136cSMax Filippov 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (excsave1));
365f615136cSMax Filippov }
366f615136cSMax Filippov 
3675a0015d6SChris Zankel /*
3685a0015d6SChris Zankel  * Initialize dispatch tables.
3695a0015d6SChris Zankel  *
3705a0015d6SChris Zankel  * The exception vectors are stored compressed the __init section in the
3715a0015d6SChris Zankel  * dispatch_init_table. This function initializes the following three tables
3725a0015d6SChris Zankel  * from that compressed table:
3735a0015d6SChris Zankel  * - fast user		first dispatch table for user exceptions
3745a0015d6SChris Zankel  * - fast kernel	first dispatch table for kernel exceptions
3755a0015d6SChris Zankel  * - default C-handler	C-handler called by the default fast handler.
3765a0015d6SChris Zankel  *
3775a0015d6SChris Zankel  * See vectors.S for more details.
3785a0015d6SChris Zankel  */
3795a0015d6SChris Zankel 
380b91dc336SChris Zankel void __init trap_init(void)
3815a0015d6SChris Zankel {
3825a0015d6SChris Zankel 	int i;
3835a0015d6SChris Zankel 
3845a0015d6SChris Zankel 	/* Setup default vectors. */
3855a0015d6SChris Zankel 
3865a0015d6SChris Zankel 	for(i = 0; i < 64; i++) {
3875a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
3885a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
3895a0015d6SChris Zankel 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
3905a0015d6SChris Zankel 	}
3915a0015d6SChris Zankel 
3925a0015d6SChris Zankel 	/* Setup specific handlers. */
3935a0015d6SChris Zankel 
3945a0015d6SChris Zankel 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
3955a0015d6SChris Zankel 
3965a0015d6SChris Zankel 		int fast = dispatch_init_table[i].fast;
3975a0015d6SChris Zankel 		int cause = dispatch_init_table[i].cause;
3985a0015d6SChris Zankel 		void *handler = dispatch_init_table[i].handler;
3995a0015d6SChris Zankel 
4005a0015d6SChris Zankel 		if (fast == 0)
4015a0015d6SChris Zankel 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
4025a0015d6SChris Zankel 		if (fast && fast & USER)
4035a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
4045a0015d6SChris Zankel 		if (fast && fast & KRNL)
4055a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
4065a0015d6SChris Zankel 	}
4075a0015d6SChris Zankel 
4085a0015d6SChris Zankel 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
409f615136cSMax Filippov 	trap_init_excsave();
4105a0015d6SChris Zankel }
4115a0015d6SChris Zankel 
412f615136cSMax Filippov #ifdef CONFIG_SMP
41349b424feSMax Filippov void secondary_trap_init(void)
414f615136cSMax Filippov {
415f615136cSMax Filippov 	trap_init_excsave();
416f615136cSMax Filippov }
417f615136cSMax Filippov #endif
418f615136cSMax Filippov 
4195a0015d6SChris Zankel /*
4205a0015d6SChris Zankel  * This function dumps the current valid window frame and other base registers.
4215a0015d6SChris Zankel  */
4225a0015d6SChris Zankel 
4235a0015d6SChris Zankel void show_regs(struct pt_regs * regs)
4245a0015d6SChris Zankel {
4255a0015d6SChris Zankel 	int i, wmask;
4265a0015d6SChris Zankel 
427a43cb95dSTejun Heo 	show_regs_print_info(KERN_DEFAULT);
428a43cb95dSTejun Heo 
4295a0015d6SChris Zankel 	wmask = regs->wmask & ~1;
4305a0015d6SChris Zankel 
4318d7e8240SChris Zankel 	for (i = 0; i < 16; i++) {
4325a0015d6SChris Zankel 		if ((i % 8) == 0)
433ad361c98SJoe Perches 			printk(KERN_INFO "a%02d:", i);
434ad361c98SJoe Perches 		printk(KERN_CONT " %08lx", regs->areg[i]);
4355a0015d6SChris Zankel 	}
436ad361c98SJoe Perches 	printk(KERN_CONT "\n");
4375a0015d6SChris Zankel 
4385a0015d6SChris Zankel 	printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
4395a0015d6SChris Zankel 	       regs->pc, regs->ps, regs->depc, regs->excvaddr);
4405a0015d6SChris Zankel 	printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
4415a0015d6SChris Zankel 	       regs->lbeg, regs->lend, regs->lcount, regs->sar);
4425a0015d6SChris Zankel 	if (user_mode(regs))
4435a0015d6SChris Zankel 		printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
4445a0015d6SChris Zankel 		       regs->windowbase, regs->windowstart, regs->wmask,
4455a0015d6SChris Zankel 		       regs->syscall);
4465a0015d6SChris Zankel }
4475a0015d6SChris Zankel 
4483e4196a5SMax Filippov static int show_trace_cb(struct stackframe *frame, void *data)
449586411dcSJohannes Weiner {
4503e4196a5SMax Filippov 	if (kernel_text_address(frame->pc)) {
4513e4196a5SMax Filippov 		printk(" [<%08lx>] ", frame->pc);
4523e4196a5SMax Filippov 		print_symbol("%s\n", frame->pc);
4533e4196a5SMax Filippov 	}
4543e4196a5SMax Filippov 	return 0;
455586411dcSJohannes Weiner }
456586411dcSJohannes Weiner 
4575a0015d6SChris Zankel void show_trace(struct task_struct *task, unsigned long *sp)
4585a0015d6SChris Zankel {
4593e4196a5SMax Filippov 	if (!sp)
4603e4196a5SMax Filippov 		sp = stack_pointer(task);
4615a0015d6SChris Zankel 
4625a0015d6SChris Zankel 	printk("Call Trace:");
4635a0015d6SChris Zankel #ifdef CONFIG_KALLSYMS
4645a0015d6SChris Zankel 	printk("\n");
4655a0015d6SChris Zankel #endif
4663e4196a5SMax Filippov 	walk_stackframe(sp, show_trace_cb, NULL);
4675a0015d6SChris Zankel 	printk("\n");
4685a0015d6SChris Zankel }
4695a0015d6SChris Zankel 
4705a0015d6SChris Zankel /*
4715a0015d6SChris Zankel  * This routine abuses get_user()/put_user() to reference pointers
4725a0015d6SChris Zankel  * with at least a bit of error checking ...
4735a0015d6SChris Zankel  */
4745a0015d6SChris Zankel 
4755a0015d6SChris Zankel static int kstack_depth_to_print = 24;
4765a0015d6SChris Zankel 
4775a0015d6SChris Zankel void show_stack(struct task_struct *task, unsigned long *sp)
4785a0015d6SChris Zankel {
4795a0015d6SChris Zankel 	int i = 0;
4805a0015d6SChris Zankel 	unsigned long *stack;
4815a0015d6SChris Zankel 
48228a0ce7fSJohannes Weiner 	if (!sp)
483586411dcSJohannes Weiner 		sp = stack_pointer(task);
4845a0015d6SChris Zankel 	stack = sp;
4855a0015d6SChris Zankel 
4865a0015d6SChris Zankel 	printk("\nStack: ");
4875a0015d6SChris Zankel 
4885a0015d6SChris Zankel 	for (i = 0; i < kstack_depth_to_print; i++) {
4895a0015d6SChris Zankel 		if (kstack_end(sp))
4905a0015d6SChris Zankel 			break;
4915a0015d6SChris Zankel 		if (i && ((i % 8) == 0))
4925a0015d6SChris Zankel 			printk("\n       ");
4935a0015d6SChris Zankel 		printk("%08lx ", *sp++);
4945a0015d6SChris Zankel 	}
4955a0015d6SChris Zankel 	printk("\n");
4965a0015d6SChris Zankel 	show_trace(task, stack);
4975a0015d6SChris Zankel }
4985a0015d6SChris Zankel 
4995a0015d6SChris Zankel void show_code(unsigned int *pc)
5005a0015d6SChris Zankel {
5015a0015d6SChris Zankel 	long i;
5025a0015d6SChris Zankel 
5035a0015d6SChris Zankel 	printk("\nCode:");
5045a0015d6SChris Zankel 
5055a0015d6SChris Zankel 	for(i = -3 ; i < 6 ; i++) {
5065a0015d6SChris Zankel 		unsigned long insn;
5075a0015d6SChris Zankel 		if (__get_user(insn, pc + i)) {
5085a0015d6SChris Zankel 			printk(" (Bad address in pc)\n");
5095a0015d6SChris Zankel 			break;
5105a0015d6SChris Zankel 		}
5115a0015d6SChris Zankel 		printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
5125a0015d6SChris Zankel 	}
5135a0015d6SChris Zankel }
5145a0015d6SChris Zankel 
51534af946aSIngo Molnar DEFINE_SPINLOCK(die_lock);
5165a0015d6SChris Zankel 
5175a0015d6SChris Zankel void die(const char * str, struct pt_regs * regs, long err)
5185a0015d6SChris Zankel {
5195a0015d6SChris Zankel 	static int die_counter;
5205a0015d6SChris Zankel 	int nl = 0;
5215a0015d6SChris Zankel 
5225a0015d6SChris Zankel 	console_verbose();
5235a0015d6SChris Zankel 	spin_lock_irq(&die_lock);
5245a0015d6SChris Zankel 
5255a0015d6SChris Zankel 	printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
5265a0015d6SChris Zankel #ifdef CONFIG_PREEMPT
5275a0015d6SChris Zankel 	printk("PREEMPT ");
5285a0015d6SChris Zankel 	nl = 1;
5295a0015d6SChris Zankel #endif
5305a0015d6SChris Zankel 	if (nl)
5315a0015d6SChris Zankel 		printk("\n");
5325a0015d6SChris Zankel 	show_regs(regs);
5335a0015d6SChris Zankel 	if (!user_mode(regs))
5345a0015d6SChris Zankel 		show_stack(NULL, (unsigned long*)regs->areg[1]);
5355a0015d6SChris Zankel 
536373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5375a0015d6SChris Zankel 	spin_unlock_irq(&die_lock);
5385a0015d6SChris Zankel 
5395a0015d6SChris Zankel 	if (in_interrupt())
5405a0015d6SChris Zankel 		panic("Fatal exception in interrupt");
5415a0015d6SChris Zankel 
542cea6a4baSHorms 	if (panic_on_oops)
543012c437dSHorms 		panic("Fatal exception");
544cea6a4baSHorms 
5455a0015d6SChris Zankel 	do_exit(err);
5465a0015d6SChris Zankel }
547