xref: /openbmc/linux/arch/xtensa/kernel/traps.c (revision b17b01533b719e9949e437abf66436a875739b40)
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>
273f07c014SIngo Molnar #include <linux/sched/signal.h>
28*b17b0153SIngo Molnar #include <linux/sched/debug.h>
295a0015d6SChris Zankel #include <linux/init.h>
305a0015d6SChris Zankel #include <linux/module.h>
315a0015d6SChris Zankel #include <linux/stringify.h>
325a0015d6SChris Zankel #include <linux/kallsyms.h>
335c888d53SNishanth Aravamudan #include <linux/delay.h>
345a891ed5SAlexey Dobriyan #include <linux/hardirq.h>
355a0015d6SChris Zankel 
363e4196a5SMax Filippov #include <asm/stacktrace.h>
375a0015d6SChris Zankel #include <asm/ptrace.h>
385a0015d6SChris Zankel #include <asm/timex.h>
397c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
405a0015d6SChris Zankel #include <asm/pgtable.h>
415a0015d6SChris Zankel #include <asm/processor.h>
422d6f82feSMax Filippov #include <asm/traps.h>
43c91e02bdSMax Filippov #include <asm/hw_breakpoint.h>
445a0015d6SChris Zankel 
455a0015d6SChris Zankel /*
465a0015d6SChris Zankel  * Machine specific interrupt handlers
475a0015d6SChris Zankel  */
485a0015d6SChris Zankel 
495a0015d6SChris Zankel extern void kernel_exception(void);
505a0015d6SChris Zankel extern void user_exception(void);
515a0015d6SChris Zankel 
525a0015d6SChris Zankel extern void fast_syscall_kernel(void);
535a0015d6SChris Zankel extern void fast_syscall_user(void);
545a0015d6SChris Zankel extern void fast_alloca(void);
555a0015d6SChris Zankel extern void fast_unaligned(void);
565a0015d6SChris Zankel extern void fast_second_level_miss(void);
575a0015d6SChris Zankel extern void fast_store_prohibited(void);
585a0015d6SChris Zankel extern void fast_coprocessor(void);
595a0015d6SChris Zankel 
605a0015d6SChris Zankel extern void do_illegal_instruction (struct pt_regs*);
615a0015d6SChris Zankel extern void do_interrupt (struct pt_regs*);
6238fef73cSMax Filippov extern void do_nmi(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 #endif
1033cfc096eSMax Filippov { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
104173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
1055a0015d6SChris Zankel #endif
106e5083a63SJohannes Weiner #ifdef CONFIG_MMU
107173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
108173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
109173d6681SChris Zankel { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
110173d6681SChris Zankel { EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
111173d6681SChris Zankel /* EXCCAUSE_SIZE_RESTRICTION unhandled */
112173d6681SChris Zankel { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
113173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
114173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
115173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
116173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
117173d6681SChris Zankel /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
118173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
119173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
120173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
121e5083a63SJohannes Weiner #endif /* CONFIG_MMU */
1225a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
123c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0)
1245a0015d6SChris Zankel COPROCESSOR(0),
1255a0015d6SChris Zankel #endif
126c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1)
1275a0015d6SChris Zankel COPROCESSOR(1),
1285a0015d6SChris Zankel #endif
129c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2)
1305a0015d6SChris Zankel COPROCESSOR(2),
1315a0015d6SChris Zankel #endif
132c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3)
1335a0015d6SChris Zankel COPROCESSOR(3),
1345a0015d6SChris Zankel #endif
135c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4)
1365a0015d6SChris Zankel COPROCESSOR(4),
1375a0015d6SChris Zankel #endif
138c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5)
1395a0015d6SChris Zankel COPROCESSOR(5),
1405a0015d6SChris Zankel #endif
141c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6)
1425a0015d6SChris Zankel COPROCESSOR(6),
1435a0015d6SChris Zankel #endif
144c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7)
1455a0015d6SChris Zankel COPROCESSOR(7),
1465a0015d6SChris Zankel #endif
14738fef73cSMax Filippov #if XTENSA_FAKE_NMI
14838fef73cSMax Filippov { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
14938fef73cSMax Filippov #endif
1505a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
1515a0015d6SChris Zankel { -1, -1, 0 }
1525a0015d6SChris Zankel 
1535a0015d6SChris Zankel };
1545a0015d6SChris Zankel 
1555a0015d6SChris Zankel /* The exception table <exc_table> serves two functions:
1565a0015d6SChris Zankel  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
1575a0015d6SChris Zankel  * 2. it is a temporary memory buffer for the exception handlers.
1585a0015d6SChris Zankel  */
1595a0015d6SChris Zankel 
160f615136cSMax Filippov DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);
1615a0015d6SChris Zankel 
1626ec7026aSMax Filippov DEFINE_PER_CPU(struct debug_table, debug_table);
1636ec7026aSMax Filippov 
1645a0015d6SChris Zankel void die(const char*, struct pt_regs*, long);
1655a0015d6SChris Zankel 
1665a0015d6SChris Zankel static inline void
1675a0015d6SChris Zankel __die_if_kernel(const char *str, struct pt_regs *regs, long err)
1685a0015d6SChris Zankel {
1695a0015d6SChris Zankel 	if (!user_mode(regs))
1705a0015d6SChris Zankel 		die(str, regs, err);
1715a0015d6SChris Zankel }
1725a0015d6SChris Zankel 
1735a0015d6SChris Zankel /*
1745a0015d6SChris Zankel  * Unhandled Exceptions. Kill user task or panic if in kernel space.
1755a0015d6SChris Zankel  */
1765a0015d6SChris Zankel 
1775a0015d6SChris Zankel void do_unhandled(struct pt_regs *regs, unsigned long exccause)
1785a0015d6SChris Zankel {
1795a0015d6SChris Zankel 	__die_if_kernel("Caught unhandled exception - should not happen",
1805a0015d6SChris Zankel 	    		regs, SIGKILL);
1815a0015d6SChris Zankel 
1825a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process */
1835a0015d6SChris Zankel 	printk("Caught unhandled exception in '%s' "
1845a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx) - should not happen\n"
1855a0015d6SChris Zankel 	       "\tEXCCAUSE is %ld\n",
18619c5870cSAlexey Dobriyan 	       current->comm, task_pid_nr(current), regs->pc, exccause);
1875a0015d6SChris Zankel 	force_sig(SIGILL, current);
1885a0015d6SChris Zankel }
1895a0015d6SChris Zankel 
1905a0015d6SChris Zankel /*
1915a0015d6SChris Zankel  * Multi-hit exception. This if fatal!
1925a0015d6SChris Zankel  */
1935a0015d6SChris Zankel 
1945a0015d6SChris Zankel void do_multihit(struct pt_regs *regs, unsigned long exccause)
1955a0015d6SChris Zankel {
1965a0015d6SChris Zankel 	die("Caught multihit exception", regs, SIGKILL);
1975a0015d6SChris Zankel }
1985a0015d6SChris Zankel 
1995a0015d6SChris Zankel /*
2002d1c645cSMarc Gauthier  * IRQ handler.
2015a0015d6SChris Zankel  */
2025a0015d6SChris Zankel 
2035a0015d6SChris Zankel extern void do_IRQ(int, struct pt_regs *);
2045a0015d6SChris Zankel 
20538fef73cSMax Filippov #if XTENSA_FAKE_NMI
20638fef73cSMax Filippov 
207e4629194SMax Filippov #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
208e4629194SMax Filippov 
209e4629194SMax Filippov #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
210e4629194SMax Filippov       IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
211e4629194SMax Filippov #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
212e4629194SMax Filippov #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
213e4629194SMax Filippov 
214e4629194SMax Filippov static inline void check_valid_nmi(void)
215e4629194SMax Filippov {
216e4629194SMax Filippov 	unsigned intread = get_sr(interrupt);
217e4629194SMax Filippov 	unsigned intenable = get_sr(intenable);
218e4629194SMax Filippov 
219e4629194SMax Filippov 	BUG_ON(intread & intenable &
220e4629194SMax Filippov 	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
221e4629194SMax Filippov 		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
222e4629194SMax Filippov 		 BIT(XCHAL_PROFILING_INTERRUPT)));
223e4629194SMax Filippov }
224e4629194SMax Filippov 
225e4629194SMax Filippov #else
226e4629194SMax Filippov 
227e4629194SMax Filippov static inline void check_valid_nmi(void)
228e4629194SMax Filippov {
229e4629194SMax Filippov }
230e4629194SMax Filippov 
231e4629194SMax Filippov #endif
232e4629194SMax Filippov 
23338fef73cSMax Filippov irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
23438fef73cSMax Filippov 
23538fef73cSMax Filippov DEFINE_PER_CPU(unsigned long, nmi_count);
23638fef73cSMax Filippov 
23738fef73cSMax Filippov void do_nmi(struct pt_regs *regs)
23838fef73cSMax Filippov {
23938fef73cSMax Filippov 	struct pt_regs *old_regs;
24038fef73cSMax Filippov 
24138fef73cSMax Filippov 	if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
24238fef73cSMax Filippov 		trace_hardirqs_off();
24338fef73cSMax Filippov 
24438fef73cSMax Filippov 	old_regs = set_irq_regs(regs);
24538fef73cSMax Filippov 	nmi_enter();
24638fef73cSMax Filippov 	++*this_cpu_ptr(&nmi_count);
247e4629194SMax Filippov 	check_valid_nmi();
24838fef73cSMax Filippov 	xtensa_pmu_irq_handler(0, NULL);
24938fef73cSMax Filippov 	nmi_exit();
25038fef73cSMax Filippov 	set_irq_regs(old_regs);
25138fef73cSMax Filippov }
25238fef73cSMax Filippov #endif
25338fef73cSMax Filippov 
2545a0015d6SChris Zankel void do_interrupt(struct pt_regs *regs)
2555a0015d6SChris Zankel {
2562d1c645cSMarc Gauthier 	static const unsigned int_level_mask[] = {
2572d1c645cSMarc Gauthier 		0,
2582d1c645cSMarc Gauthier 		XCHAL_INTLEVEL1_MASK,
2592d1c645cSMarc Gauthier 		XCHAL_INTLEVEL2_MASK,
2602d1c645cSMarc Gauthier 		XCHAL_INTLEVEL3_MASK,
2612d1c645cSMarc Gauthier 		XCHAL_INTLEVEL4_MASK,
2622d1c645cSMarc Gauthier 		XCHAL_INTLEVEL5_MASK,
2632d1c645cSMarc Gauthier 		XCHAL_INTLEVEL6_MASK,
2642d1c645cSMarc Gauthier 		XCHAL_INTLEVEL7_MASK,
2652d1c645cSMarc Gauthier 	};
2667d5f6a9aSMax Filippov 	struct pt_regs *old_regs;
26799623239SMax Filippov 
2687d5f6a9aSMax Filippov 	trace_hardirqs_off();
2697d5f6a9aSMax Filippov 
2707d5f6a9aSMax Filippov 	old_regs = set_irq_regs(regs);
27199623239SMax Filippov 	irq_enter();
2722d1c645cSMarc Gauthier 
2732d1c645cSMarc Gauthier 	for (;;) {
2742d1c645cSMarc Gauthier 		unsigned intread = get_sr(interrupt);
2752d1c645cSMarc Gauthier 		unsigned intenable = get_sr(intenable);
276895666a9SMax Filippov 		unsigned int_at_level = intread & intenable;
277895666a9SMax Filippov 		unsigned level;
2782d1c645cSMarc Gauthier 
279895666a9SMax Filippov 		for (level = LOCKLEVEL; level > 0; --level) {
280895666a9SMax Filippov 			if (int_at_level & int_level_mask[level]) {
281895666a9SMax Filippov 				int_at_level &= int_level_mask[level];
282895666a9SMax Filippov 				break;
283895666a9SMax Filippov 			}
284895666a9SMax Filippov 		}
285895666a9SMax Filippov 
286895666a9SMax Filippov 		if (level == 0)
28799623239SMax Filippov 			break;
2882d1c645cSMarc Gauthier 
28999623239SMax Filippov 		do_IRQ(__ffs(int_at_level), regs);
29099623239SMax Filippov 	}
2915a0015d6SChris Zankel 
29299623239SMax Filippov 	irq_exit();
29399623239SMax Filippov 	set_irq_regs(old_regs);
2945a0015d6SChris Zankel }
2955a0015d6SChris Zankel 
2965a0015d6SChris Zankel /*
2975a0015d6SChris Zankel  * Illegal instruction. Fatal if in kernel space.
2985a0015d6SChris Zankel  */
2995a0015d6SChris Zankel 
3005a0015d6SChris Zankel void
3015a0015d6SChris Zankel do_illegal_instruction(struct pt_regs *regs)
3025a0015d6SChris Zankel {
3035a0015d6SChris Zankel 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
3045a0015d6SChris Zankel 
3055a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process. */
3065a0015d6SChris Zankel 
3075a0015d6SChris Zankel 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
30819c5870cSAlexey Dobriyan 	    current->comm, task_pid_nr(current), regs->pc);
3095a0015d6SChris Zankel 	force_sig(SIGILL, current);
3105a0015d6SChris Zankel }
3115a0015d6SChris Zankel 
3125a0015d6SChris Zankel 
3135a0015d6SChris Zankel /*
3145a0015d6SChris Zankel  * Handle unaligned memory accesses from user space. Kill task.
3155a0015d6SChris Zankel  *
3165a0015d6SChris Zankel  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
3175a0015d6SChris Zankel  * accesses causes from user space.
3185a0015d6SChris Zankel  */
3195a0015d6SChris Zankel 
3205a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
3215a0015d6SChris Zankel void
3225a0015d6SChris Zankel do_unaligned_user (struct pt_regs *regs)
3235a0015d6SChris Zankel {
3245a0015d6SChris Zankel 	siginfo_t info;
3255a0015d6SChris Zankel 
3265a0015d6SChris Zankel 	__die_if_kernel("Unhandled unaligned exception in kernel",
3275a0015d6SChris Zankel 	    		regs, SIGKILL);
3285a0015d6SChris Zankel 
3295a0015d6SChris Zankel 	current->thread.bad_vaddr = regs->excvaddr;
3305a0015d6SChris Zankel 	current->thread.error_code = -3;
3315a0015d6SChris Zankel 	printk("Unaligned memory access to %08lx in '%s' "
3325a0015d6SChris Zankel 	       "(pid = %d, pc = %#010lx)\n",
33319c5870cSAlexey Dobriyan 	       regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
3345a0015d6SChris Zankel 	info.si_signo = SIGBUS;
3355a0015d6SChris Zankel 	info.si_errno = 0;
3365a0015d6SChris Zankel 	info.si_code = BUS_ADRALN;
3375a0015d6SChris Zankel 	info.si_addr = (void *) regs->excvaddr;
3385a0015d6SChris Zankel 	force_sig_info(SIGSEGV, &info, current);
3395a0015d6SChris Zankel 
3405a0015d6SChris Zankel }
3415a0015d6SChris Zankel #endif
3425a0015d6SChris Zankel 
343c91e02bdSMax Filippov /* Handle debug events.
344c91e02bdSMax Filippov  * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
345c91e02bdSMax Filippov  * preemption disabled to avoid rescheduling and keep mapping of hardware
346c91e02bdSMax Filippov  * breakpoint structures to debug registers intact, so that
347c91e02bdSMax Filippov  * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
348c91e02bdSMax Filippov  */
3495a0015d6SChris Zankel void
3505a0015d6SChris Zankel do_debug(struct pt_regs *regs)
3515a0015d6SChris Zankel {
352c91e02bdSMax Filippov #ifdef CONFIG_HAVE_HW_BREAKPOINT
353c91e02bdSMax Filippov 	int ret = check_hw_breakpoint(regs);
354c91e02bdSMax Filippov 
355c91e02bdSMax Filippov 	preempt_enable();
356c91e02bdSMax Filippov 	if (ret == 0)
357c91e02bdSMax Filippov 		return;
358c91e02bdSMax Filippov #endif
3595a0015d6SChris Zankel 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
3605a0015d6SChris Zankel 
3615a0015d6SChris Zankel 	/* If in user mode, send SIGTRAP signal to current process */
3625a0015d6SChris Zankel 
3635a0015d6SChris Zankel 	force_sig(SIGTRAP, current);
3645a0015d6SChris Zankel }
3655a0015d6SChris Zankel 
3665a0015d6SChris Zankel 
367f615136cSMax Filippov static void set_handler(int idx, void *handler)
368f615136cSMax Filippov {
369f615136cSMax Filippov 	unsigned int cpu;
370f615136cSMax Filippov 
371f615136cSMax Filippov 	for_each_possible_cpu(cpu)
372f615136cSMax Filippov 		per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
373f615136cSMax Filippov }
374f615136cSMax Filippov 
37528570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */
37628570e8dSMax Filippov 
37728570e8dSMax Filippov void * __init trap_set_handler(int cause, void *handler)
37828570e8dSMax Filippov {
379f615136cSMax Filippov 	void *previous = (void *)per_cpu(exc_table, 0)[
380f615136cSMax Filippov 		EXC_TABLE_DEFAULT / 4 + cause];
381f615136cSMax Filippov 	set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
38228570e8dSMax Filippov 	return previous;
38328570e8dSMax Filippov }
38428570e8dSMax Filippov 
38528570e8dSMax Filippov 
38649b424feSMax Filippov static void trap_init_excsave(void)
387f615136cSMax Filippov {
388f615136cSMax Filippov 	unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
389f615136cSMax Filippov 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (excsave1));
390f615136cSMax Filippov }
391f615136cSMax Filippov 
3926ec7026aSMax Filippov static void trap_init_debug(void)
3936ec7026aSMax Filippov {
3946ec7026aSMax Filippov 	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
3956ec7026aSMax Filippov 
3966ec7026aSMax Filippov 	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
3976ec7026aSMax Filippov 	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
3986ec7026aSMax Filippov 			     :: "a"(debugsave));
3996ec7026aSMax Filippov }
4006ec7026aSMax Filippov 
4015a0015d6SChris Zankel /*
4025a0015d6SChris Zankel  * Initialize dispatch tables.
4035a0015d6SChris Zankel  *
4045a0015d6SChris Zankel  * The exception vectors are stored compressed the __init section in the
4055a0015d6SChris Zankel  * dispatch_init_table. This function initializes the following three tables
4065a0015d6SChris Zankel  * from that compressed table:
4075a0015d6SChris Zankel  * - fast user		first dispatch table for user exceptions
4085a0015d6SChris Zankel  * - fast kernel	first dispatch table for kernel exceptions
4095a0015d6SChris Zankel  * - default C-handler	C-handler called by the default fast handler.
4105a0015d6SChris Zankel  *
4115a0015d6SChris Zankel  * See vectors.S for more details.
4125a0015d6SChris Zankel  */
4135a0015d6SChris Zankel 
414b91dc336SChris Zankel void __init trap_init(void)
4155a0015d6SChris Zankel {
4165a0015d6SChris Zankel 	int i;
4175a0015d6SChris Zankel 
4185a0015d6SChris Zankel 	/* Setup default vectors. */
4195a0015d6SChris Zankel 
4205a0015d6SChris Zankel 	for(i = 0; i < 64; i++) {
4215a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
4225a0015d6SChris Zankel 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
4235a0015d6SChris Zankel 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
4245a0015d6SChris Zankel 	}
4255a0015d6SChris Zankel 
4265a0015d6SChris Zankel 	/* Setup specific handlers. */
4275a0015d6SChris Zankel 
4285a0015d6SChris Zankel 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
4295a0015d6SChris Zankel 
4305a0015d6SChris Zankel 		int fast = dispatch_init_table[i].fast;
4315a0015d6SChris Zankel 		int cause = dispatch_init_table[i].cause;
4325a0015d6SChris Zankel 		void *handler = dispatch_init_table[i].handler;
4335a0015d6SChris Zankel 
4345a0015d6SChris Zankel 		if (fast == 0)
4355a0015d6SChris Zankel 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
4365a0015d6SChris Zankel 		if (fast && fast & USER)
4375a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
4385a0015d6SChris Zankel 		if (fast && fast & KRNL)
4395a0015d6SChris Zankel 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
4405a0015d6SChris Zankel 	}
4415a0015d6SChris Zankel 
4425a0015d6SChris Zankel 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
443f615136cSMax Filippov 	trap_init_excsave();
4446ec7026aSMax Filippov 	trap_init_debug();
4455a0015d6SChris Zankel }
4465a0015d6SChris Zankel 
447f615136cSMax Filippov #ifdef CONFIG_SMP
44849b424feSMax Filippov void secondary_trap_init(void)
449f615136cSMax Filippov {
450f615136cSMax Filippov 	trap_init_excsave();
4516ec7026aSMax Filippov 	trap_init_debug();
452f615136cSMax Filippov }
453f615136cSMax Filippov #endif
454f615136cSMax Filippov 
4555a0015d6SChris Zankel /*
4565a0015d6SChris Zankel  * This function dumps the current valid window frame and other base registers.
4575a0015d6SChris Zankel  */
4585a0015d6SChris Zankel 
4595a0015d6SChris Zankel void show_regs(struct pt_regs * regs)
4605a0015d6SChris Zankel {
4615a0015d6SChris Zankel 	int i, wmask;
4625a0015d6SChris Zankel 
463a43cb95dSTejun Heo 	show_regs_print_info(KERN_DEFAULT);
464a43cb95dSTejun Heo 
4655a0015d6SChris Zankel 	wmask = regs->wmask & ~1;
4665a0015d6SChris Zankel 
4678d7e8240SChris Zankel 	for (i = 0; i < 16; i++) {
4685a0015d6SChris Zankel 		if ((i % 8) == 0)
469d4eccafcSMax Filippov 			pr_info("a%02d:", i);
470d4eccafcSMax Filippov 		pr_cont(" %08lx", regs->areg[i]);
4715a0015d6SChris Zankel 	}
472d4eccafcSMax Filippov 	pr_cont("\n");
473d4eccafcSMax Filippov 	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
4745a0015d6SChris Zankel 		regs->pc, regs->ps, regs->depc, regs->excvaddr);
475d4eccafcSMax Filippov 	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
4765a0015d6SChris Zankel 		regs->lbeg, regs->lend, regs->lcount, regs->sar);
4775a0015d6SChris Zankel 	if (user_mode(regs))
478d4eccafcSMax Filippov 		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
4795a0015d6SChris Zankel 			regs->windowbase, regs->windowstart, regs->wmask,
4805a0015d6SChris Zankel 			regs->syscall);
4815a0015d6SChris Zankel }
4825a0015d6SChris Zankel 
4833e4196a5SMax Filippov static int show_trace_cb(struct stackframe *frame, void *data)
484586411dcSJohannes Weiner {
4853e4196a5SMax Filippov 	if (kernel_text_address(frame->pc)) {
486d4eccafcSMax Filippov 		pr_cont(" [<%08lx>]", frame->pc);
4873e4196a5SMax Filippov 		print_symbol(" %s\n", frame->pc);
4883e4196a5SMax Filippov 	}
4893e4196a5SMax Filippov 	return 0;
490586411dcSJohannes Weiner }
491586411dcSJohannes Weiner 
4925a0015d6SChris Zankel void show_trace(struct task_struct *task, unsigned long *sp)
4935a0015d6SChris Zankel {
4943e4196a5SMax Filippov 	if (!sp)
4953e4196a5SMax Filippov 		sp = stack_pointer(task);
4965a0015d6SChris Zankel 
497d4eccafcSMax Filippov 	pr_info("Call Trace:\n");
4983e4196a5SMax Filippov 	walk_stackframe(sp, show_trace_cb, NULL);
499d4eccafcSMax Filippov #ifndef CONFIG_KALLSYMS
500d4eccafcSMax Filippov 	pr_cont("\n");
501d4eccafcSMax Filippov #endif
5025a0015d6SChris Zankel }
5035a0015d6SChris Zankel 
5045a0015d6SChris Zankel static int kstack_depth_to_print = 24;
5055a0015d6SChris Zankel 
5065a0015d6SChris Zankel void show_stack(struct task_struct *task, unsigned long *sp)
5075a0015d6SChris Zankel {
5085a0015d6SChris Zankel 	int i = 0;
5095a0015d6SChris Zankel 	unsigned long *stack;
5105a0015d6SChris Zankel 
51128a0ce7fSJohannes Weiner 	if (!sp)
512586411dcSJohannes Weiner 		sp = stack_pointer(task);
5135a0015d6SChris Zankel 	stack = sp;
5145a0015d6SChris Zankel 
515d4eccafcSMax Filippov 	pr_info("Stack:\n");
5165a0015d6SChris Zankel 
5175a0015d6SChris Zankel 	for (i = 0; i < kstack_depth_to_print; i++) {
5185a0015d6SChris Zankel 		if (kstack_end(sp))
5195a0015d6SChris Zankel 			break;
520d4eccafcSMax Filippov 		pr_cont(" %08lx", *sp++);
521d4eccafcSMax Filippov 		if (i % 8 == 7)
522d4eccafcSMax Filippov 			pr_cont("\n");
5235a0015d6SChris Zankel 	}
5245a0015d6SChris Zankel 	show_trace(task, stack);
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 
5335a0015d6SChris Zankel 	console_verbose();
5345a0015d6SChris Zankel 	spin_lock_irq(&die_lock);
5355a0015d6SChris Zankel 
536d4eccafcSMax Filippov 	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter,
537d4eccafcSMax Filippov 		IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "");
5385a0015d6SChris Zankel 	show_regs(regs);
5395a0015d6SChris Zankel 	if (!user_mode(regs))
5405a0015d6SChris Zankel 		show_stack(NULL, (unsigned long*)regs->areg[1]);
5415a0015d6SChris Zankel 
542373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5435a0015d6SChris Zankel 	spin_unlock_irq(&die_lock);
5445a0015d6SChris Zankel 
5455a0015d6SChris Zankel 	if (in_interrupt())
5465a0015d6SChris Zankel 		panic("Fatal exception in interrupt");
5475a0015d6SChris Zankel 
548cea6a4baSHorms 	if (panic_on_oops)
549012c437dSHorms 		panic("Fatal exception");
550cea6a4baSHorms 
5515a0015d6SChris Zankel 	do_exit(err);
5525a0015d6SChris Zankel }
553