xref: /linux/arch/xtensa/kernel/traps.c (revision 9fa8c59f5f82aec99a7f87095ab093b68dd492a5)
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>
28b17b0153SIngo Molnar #include <linux/sched/debug.h>
293f8c2452SIngo Molnar #include <linux/sched/task_stack.h>
305a0015d6SChris Zankel #include <linux/init.h>
315a0015d6SChris Zankel #include <linux/module.h>
325a0015d6SChris Zankel #include <linux/stringify.h>
335a0015d6SChris Zankel #include <linux/kallsyms.h>
345c888d53SNishanth Aravamudan #include <linux/delay.h>
355a891ed5SAlexey Dobriyan #include <linux/hardirq.h>
36c130d3beSMax Filippov #include <linux/ratelimit.h>
3765fddcfcSMike Rapoport #include <linux/pgtable.h>
385a0015d6SChris Zankel 
393e4196a5SMax Filippov #include <asm/stacktrace.h>
405a0015d6SChris Zankel #include <asm/ptrace.h>
415a0015d6SChris Zankel #include <asm/timex.h>
427c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
435a0015d6SChris Zankel #include <asm/processor.h>
442d6f82feSMax Filippov #include <asm/traps.h>
45c91e02bdSMax Filippov #include <asm/hw_breakpoint.h>
465a0015d6SChris Zankel 
475a0015d6SChris Zankel /*
485a0015d6SChris Zankel  * Machine specific interrupt handlers
495a0015d6SChris Zankel  */
505a0015d6SChris Zankel 
51db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs);
52db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs);
53db0d07faSMax Filippov #if XTENSA_FAKE_NMI
54db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs);
55db0d07faSMax Filippov #endif
56db0d07faSMax Filippov #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
57db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs);
58db0d07faSMax Filippov #endif
59db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs);
60db0d07faSMax Filippov static void do_debug(struct pt_regs *regs);
615a0015d6SChris Zankel 
625a0015d6SChris Zankel /*
635a0015d6SChris Zankel  * The vector table must be preceded by a save area (which
645a0015d6SChris Zankel  * implies it must be in RAM, unless one places RAM immediately
655a0015d6SChris Zankel  * before a ROM and puts the vector at the start of the ROM (!))
665a0015d6SChris Zankel  */
675a0015d6SChris Zankel 
685a0015d6SChris Zankel #define KRNL		0x01
695a0015d6SChris Zankel #define USER		0x02
705a0015d6SChris Zankel 
715a0015d6SChris Zankel #define COPROCESSOR(x)							\
72173d6681SChris Zankel { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
735a0015d6SChris Zankel 
745a0015d6SChris Zankel typedef struct {
755a0015d6SChris Zankel 	int cause;
765a0015d6SChris Zankel 	int fast;
775a0015d6SChris Zankel 	void* handler;
785a0015d6SChris Zankel } dispatch_init_table_t;
795a0015d6SChris Zankel 
80b91dc336SChris Zankel static dispatch_init_table_t __initdata dispatch_init_table[] = {
815a0015d6SChris Zankel 
8209f8a6dbSMax Filippov #ifdef CONFIG_USER_ABI_CALL0_PROBE
8309f8a6dbSMax Filippov { EXCCAUSE_ILLEGAL_INSTRUCTION,	USER,	   fast_illegal_instruction_user },
8409f8a6dbSMax Filippov #endif
85173d6681SChris Zankel { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
86173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
87173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
88173d6681SChris Zankel /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
89173d6681SChris Zankel /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
90173d6681SChris Zankel { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
91da0a4e5cSMax Filippov #ifdef SUPPORT_WINDOWED
92173d6681SChris Zankel { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
93da0a4e5cSMax Filippov #endif
94173d6681SChris Zankel /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
95173d6681SChris Zankel /* EXCCAUSE_PRIVILEGED unhandled */
965a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
974ded6282SMax Filippov #ifdef CONFIG_XTENSA_UNALIGNED_USER
98173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
995a0015d6SChris Zankel #endif
1003cfc096eSMax Filippov { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
101173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
1025a0015d6SChris Zankel #endif
103e5083a63SJohannes Weiner #ifdef CONFIG_MMU
104173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,			0,	   do_page_fault },
105173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,			USER|KRNL, fast_second_level_miss},
106173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,			USER|KRNL, fast_second_level_miss},
107173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,			0,	   do_page_fault },
108a8f0c31fSMax Filippov { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
109a8f0c31fSMax Filippov #endif /* CONFIG_MMU */
110a8f0c31fSMax Filippov #ifdef CONFIG_PFAULT
111a8f0c31fSMax Filippov { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
112a8f0c31fSMax Filippov { EXCCAUSE_ITLB_PRIVILEGE,		0,	   do_page_fault },
113a8f0c31fSMax Filippov { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
114173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
115173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE,		0,	   do_page_fault },
116173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
117173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
118a8f0c31fSMax Filippov #endif
1195a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
120c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0)
1215a0015d6SChris Zankel COPROCESSOR(0),
1225a0015d6SChris Zankel #endif
123c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1)
1245a0015d6SChris Zankel COPROCESSOR(1),
1255a0015d6SChris Zankel #endif
126c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2)
1275a0015d6SChris Zankel COPROCESSOR(2),
1285a0015d6SChris Zankel #endif
129c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3)
1305a0015d6SChris Zankel COPROCESSOR(3),
1315a0015d6SChris Zankel #endif
132c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4)
1335a0015d6SChris Zankel COPROCESSOR(4),
1345a0015d6SChris Zankel #endif
135c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5)
1365a0015d6SChris Zankel COPROCESSOR(5),
1375a0015d6SChris Zankel #endif
138c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6)
1395a0015d6SChris Zankel COPROCESSOR(6),
1405a0015d6SChris Zankel #endif
141c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7)
1425a0015d6SChris Zankel COPROCESSOR(7),
1435a0015d6SChris Zankel #endif
14438fef73cSMax Filippov #if XTENSA_FAKE_NMI
14538fef73cSMax Filippov { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
14638fef73cSMax Filippov #endif
1475a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
1485a0015d6SChris Zankel { -1, -1, 0 }
1495a0015d6SChris Zankel 
1505a0015d6SChris Zankel };
1515a0015d6SChris Zankel 
1525a0015d6SChris Zankel /* The exception table <exc_table> serves two functions:
1535a0015d6SChris Zankel  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
1545a0015d6SChris Zankel  * 2. it is a temporary memory buffer for the exception handlers.
1555a0015d6SChris Zankel  */
1565a0015d6SChris Zankel 
157f21a79caSMax Filippov DEFINE_PER_CPU(struct exc_table, exc_table);
1586ec7026aSMax Filippov DEFINE_PER_CPU(struct debug_table, debug_table);
1596ec7026aSMax Filippov 
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 
173fc55402bSMax Filippov void do_unhandled(struct pt_regs *regs)
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 */
179c130d3beSMax Filippov 	pr_info_ratelimited("Caught unhandled exception in '%s' "
1805a0015d6SChris Zankel 			    "(pid = %d, pc = %#010lx) - should not happen\n"
1815a0015d6SChris Zankel 			    "\tEXCCAUSE is %ld\n",
182c130d3beSMax Filippov 			    current->comm, task_pid_nr(current), regs->pc,
183fc55402bSMax Filippov 			    regs->exccause);
1843cf5d076SEric W. Biederman 	force_sig(SIGILL);
1855a0015d6SChris Zankel }
1865a0015d6SChris Zankel 
1875a0015d6SChris Zankel /*
1885a0015d6SChris Zankel  * Multi-hit exception. This if fatal!
1895a0015d6SChris Zankel  */
1905a0015d6SChris Zankel 
191db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs)
1925a0015d6SChris Zankel {
1935a0015d6SChris Zankel 	die("Caught multihit exception", regs, SIGKILL);
1945a0015d6SChris Zankel }
1955a0015d6SChris Zankel 
1965a0015d6SChris Zankel /*
1972d1c645cSMarc Gauthier  * IRQ handler.
1985a0015d6SChris Zankel  */
1995a0015d6SChris Zankel 
20038fef73cSMax Filippov #if XTENSA_FAKE_NMI
20138fef73cSMax Filippov 
202e4629194SMax Filippov #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
203e4629194SMax Filippov 
204e4629194SMax Filippov #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
205e4629194SMax Filippov       IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
206e4629194SMax Filippov #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
207e4629194SMax Filippov #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
208e4629194SMax Filippov 
209e4629194SMax Filippov static inline void check_valid_nmi(void)
210e4629194SMax Filippov {
211cad6fadeSMax Filippov 	unsigned intread = xtensa_get_sr(interrupt);
212cad6fadeSMax Filippov 	unsigned intenable = xtensa_get_sr(intenable);
213e4629194SMax Filippov 
214e4629194SMax Filippov 	BUG_ON(intread & intenable &
215e4629194SMax Filippov 	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
216e4629194SMax Filippov 		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
217e4629194SMax Filippov 		 BIT(XCHAL_PROFILING_INTERRUPT)));
218e4629194SMax Filippov }
219e4629194SMax Filippov 
220e4629194SMax Filippov #else
221e4629194SMax Filippov 
222e4629194SMax Filippov static inline void check_valid_nmi(void)
223e4629194SMax Filippov {
224e4629194SMax Filippov }
225e4629194SMax Filippov 
226e4629194SMax Filippov #endif
227e4629194SMax Filippov 
22838fef73cSMax Filippov irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
22938fef73cSMax Filippov 
23038fef73cSMax Filippov DEFINE_PER_CPU(unsigned long, nmi_count);
23138fef73cSMax Filippov 
232db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs)
23338fef73cSMax Filippov {
234de4415d0SMax Filippov 	struct pt_regs *old_regs = set_irq_regs(regs);
23538fef73cSMax Filippov 
23638fef73cSMax Filippov 	nmi_enter();
23738fef73cSMax Filippov 	++*this_cpu_ptr(&nmi_count);
238e4629194SMax Filippov 	check_valid_nmi();
23938fef73cSMax Filippov 	xtensa_pmu_irq_handler(0, NULL);
24038fef73cSMax Filippov 	nmi_exit();
24138fef73cSMax Filippov 	set_irq_regs(old_regs);
24238fef73cSMax Filippov }
24338fef73cSMax Filippov #endif
24438fef73cSMax Filippov 
245db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs)
2465a0015d6SChris Zankel {
2472d1c645cSMarc Gauthier 	static const unsigned int_level_mask[] = {
2482d1c645cSMarc Gauthier 		0,
2492d1c645cSMarc Gauthier 		XCHAL_INTLEVEL1_MASK,
2502d1c645cSMarc Gauthier 		XCHAL_INTLEVEL2_MASK,
2512d1c645cSMarc Gauthier 		XCHAL_INTLEVEL3_MASK,
2522d1c645cSMarc Gauthier 		XCHAL_INTLEVEL4_MASK,
2532d1c645cSMarc Gauthier 		XCHAL_INTLEVEL5_MASK,
2542d1c645cSMarc Gauthier 		XCHAL_INTLEVEL6_MASK,
2552d1c645cSMarc Gauthier 		XCHAL_INTLEVEL7_MASK,
2562d1c645cSMarc Gauthier 	};
257de4415d0SMax Filippov 	struct pt_regs *old_regs = set_irq_regs(regs);
25843ba2237SMax Filippov 	unsigned unhandled = ~0u;
25999623239SMax Filippov 
26099623239SMax Filippov 	irq_enter();
2612d1c645cSMarc Gauthier 
2622d1c645cSMarc Gauthier 	for (;;) {
263cad6fadeSMax Filippov 		unsigned intread = xtensa_get_sr(interrupt);
264cad6fadeSMax Filippov 		unsigned intenable = xtensa_get_sr(intenable);
265895666a9SMax Filippov 		unsigned int_at_level = intread & intenable;
266895666a9SMax Filippov 		unsigned level;
2672d1c645cSMarc Gauthier 
268895666a9SMax Filippov 		for (level = LOCKLEVEL; level > 0; --level) {
269895666a9SMax Filippov 			if (int_at_level & int_level_mask[level]) {
270895666a9SMax Filippov 				int_at_level &= int_level_mask[level];
27143ba2237SMax Filippov 				if (int_at_level & unhandled)
27243ba2237SMax Filippov 					int_at_level &= unhandled;
27343ba2237SMax Filippov 				else
27443ba2237SMax Filippov 					unhandled |= int_level_mask[level];
275895666a9SMax Filippov 				break;
276895666a9SMax Filippov 			}
277895666a9SMax Filippov 		}
278895666a9SMax Filippov 
279895666a9SMax Filippov 		if (level == 0)
28099623239SMax Filippov 			break;
2812d1c645cSMarc Gauthier 
28243ba2237SMax Filippov 		/* clear lowest pending irq in the unhandled mask */
28343ba2237SMax Filippov 		unhandled ^= (int_at_level & -int_at_level);
28499623239SMax Filippov 		do_IRQ(__ffs(int_at_level), regs);
28599623239SMax Filippov 	}
2865a0015d6SChris Zankel 
28799623239SMax Filippov 	irq_exit();
28899623239SMax Filippov 	set_irq_regs(old_regs);
2895a0015d6SChris Zankel }
2905a0015d6SChris Zankel 
2915a0015d6SChris Zankel /*
2925a0015d6SChris Zankel  * Illegal instruction. Fatal if in kernel space.
2935a0015d6SChris Zankel  */
2945a0015d6SChris Zankel 
295db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs)
2965a0015d6SChris Zankel {
2975a0015d6SChris Zankel 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
2985a0015d6SChris Zankel 
2995a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process. */
3005a0015d6SChris Zankel 
301c130d3beSMax Filippov 	pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
30219c5870cSAlexey Dobriyan 			    current->comm, task_pid_nr(current), regs->pc);
3033cf5d076SEric W. Biederman 	force_sig(SIGILL);
3045a0015d6SChris Zankel }
3055a0015d6SChris Zankel 
3065a0015d6SChris Zankel 
3075a0015d6SChris Zankel /*
3085a0015d6SChris Zankel  * Handle unaligned memory accesses from user space. Kill task.
3095a0015d6SChris Zankel  *
3105a0015d6SChris Zankel  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
3115a0015d6SChris Zankel  * accesses causes from user space.
3125a0015d6SChris Zankel  */
3135a0015d6SChris Zankel 
3145a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
315db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs)
3165a0015d6SChris Zankel {
3175a0015d6SChris Zankel 	__die_if_kernel("Unhandled unaligned exception in kernel",
3185a0015d6SChris Zankel 			regs, SIGKILL);
3195a0015d6SChris Zankel 
3205a0015d6SChris Zankel 	current->thread.bad_vaddr = regs->excvaddr;
3215a0015d6SChris Zankel 	current->thread.error_code = -3;
322c130d3beSMax Filippov 	pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
3235a0015d6SChris Zankel 			    "(pid = %d, pc = %#010lx)\n",
324c130d3beSMax Filippov 			    regs->excvaddr, current->comm,
325c130d3beSMax Filippov 			    task_pid_nr(current), regs->pc);
3262e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
3275a0015d6SChris Zankel }
3285a0015d6SChris Zankel #endif
3295a0015d6SChris Zankel 
330c91e02bdSMax Filippov /* Handle debug events.
331c91e02bdSMax Filippov  * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
332c91e02bdSMax Filippov  * preemption disabled to avoid rescheduling and keep mapping of hardware
333c91e02bdSMax Filippov  * breakpoint structures to debug registers intact, so that
334c91e02bdSMax Filippov  * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
335c91e02bdSMax Filippov  */
336db0d07faSMax Filippov static void do_debug(struct pt_regs *regs)
3375a0015d6SChris Zankel {
338c91e02bdSMax Filippov #ifdef CONFIG_HAVE_HW_BREAKPOINT
339c91e02bdSMax Filippov 	int ret = check_hw_breakpoint(regs);
340c91e02bdSMax Filippov 
341c91e02bdSMax Filippov 	preempt_enable();
342c91e02bdSMax Filippov 	if (ret == 0)
343c91e02bdSMax Filippov 		return;
344c91e02bdSMax Filippov #endif
3455a0015d6SChris Zankel 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
3465a0015d6SChris Zankel 
3475a0015d6SChris Zankel 	/* If in user mode, send SIGTRAP signal to current process */
3485a0015d6SChris Zankel 
3493cf5d076SEric W. Biederman 	force_sig(SIGTRAP);
3505a0015d6SChris Zankel }
3515a0015d6SChris Zankel 
3525a0015d6SChris Zankel 
353f21a79caSMax Filippov #define set_handler(type, cause, handler)				\
354f21a79caSMax Filippov 	do {								\
355f21a79caSMax Filippov 		unsigned int cpu;					\
356f21a79caSMax Filippov 									\
357f21a79caSMax Filippov 		for_each_possible_cpu(cpu)				\
358f21a79caSMax Filippov 			per_cpu(exc_table, cpu).type[cause] = (handler);\
359f21a79caSMax Filippov 	} while (0)
360f615136cSMax Filippov 
36128570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */
36228570e8dSMax Filippov 
363fc55402bSMax Filippov xtensa_exception_handler *
364fc55402bSMax Filippov __init trap_set_handler(int cause, xtensa_exception_handler *handler)
36528570e8dSMax Filippov {
366f21a79caSMax Filippov 	void *previous = per_cpu(exc_table, 0).default_handler[cause];
367f21a79caSMax Filippov 
368f21a79caSMax Filippov 	set_handler(default_handler, cause, handler);
36928570e8dSMax Filippov 	return previous;
37028570e8dSMax Filippov }
37128570e8dSMax Filippov 
37228570e8dSMax Filippov 
37349b424feSMax Filippov static void trap_init_excsave(void)
374f615136cSMax Filippov {
375*9fa8c59fSMax Filippov 	xtensa_set_sr(this_cpu_ptr(&exc_table), excsave1);
376f615136cSMax Filippov }
377f615136cSMax Filippov 
3786ec7026aSMax Filippov static void trap_init_debug(void)
3796ec7026aSMax Filippov {
3806ec7026aSMax Filippov 	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
3816ec7026aSMax Filippov 
3826ec7026aSMax Filippov 	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
3836ec7026aSMax Filippov 	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
3846ec7026aSMax Filippov 			     :: "a"(debugsave));
3856ec7026aSMax Filippov }
3866ec7026aSMax Filippov 
3875a0015d6SChris Zankel /*
3885a0015d6SChris Zankel  * Initialize dispatch tables.
3895a0015d6SChris Zankel  *
3905a0015d6SChris Zankel  * The exception vectors are stored compressed the __init section in the
3915a0015d6SChris Zankel  * dispatch_init_table. This function initializes the following three tables
3925a0015d6SChris Zankel  * from that compressed table:
3935a0015d6SChris Zankel  * - fast user		first dispatch table for user exceptions
3945a0015d6SChris Zankel  * - fast kernel	first dispatch table for kernel exceptions
3955a0015d6SChris Zankel  * - default C-handler	C-handler called by the default fast handler.
3965a0015d6SChris Zankel  *
3975a0015d6SChris Zankel  * See vectors.S for more details.
3985a0015d6SChris Zankel  */
3995a0015d6SChris Zankel 
400b91dc336SChris Zankel void __init trap_init(void)
4015a0015d6SChris Zankel {
4025a0015d6SChris Zankel 	int i;
4035a0015d6SChris Zankel 
4045a0015d6SChris Zankel 	/* Setup default vectors. */
4055a0015d6SChris Zankel 
406f21a79caSMax Filippov 	for (i = 0; i < EXCCAUSE_N; i++) {
407f21a79caSMax Filippov 		set_handler(fast_user_handler, i, user_exception);
408f21a79caSMax Filippov 		set_handler(fast_kernel_handler, i, kernel_exception);
409f21a79caSMax Filippov 		set_handler(default_handler, i, do_unhandled);
4105a0015d6SChris Zankel 	}
4115a0015d6SChris Zankel 
4125a0015d6SChris Zankel 	/* Setup specific handlers. */
4135a0015d6SChris Zankel 
4145a0015d6SChris Zankel 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
4155a0015d6SChris Zankel 		int fast = dispatch_init_table[i].fast;
4165a0015d6SChris Zankel 		int cause = dispatch_init_table[i].cause;
4175a0015d6SChris Zankel 		void *handler = dispatch_init_table[i].handler;
4185a0015d6SChris Zankel 
4195a0015d6SChris Zankel 		if (fast == 0)
420f21a79caSMax Filippov 			set_handler(default_handler, cause, handler);
42160deebe6SMax Filippov 		if ((fast & USER) != 0)
422f21a79caSMax Filippov 			set_handler(fast_user_handler, cause, handler);
42360deebe6SMax Filippov 		if ((fast & KRNL) != 0)
424f21a79caSMax Filippov 			set_handler(fast_kernel_handler, cause, handler);
4255a0015d6SChris Zankel 	}
4265a0015d6SChris Zankel 
4275a0015d6SChris Zankel 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
428f615136cSMax Filippov 	trap_init_excsave();
4296ec7026aSMax Filippov 	trap_init_debug();
4305a0015d6SChris Zankel }
4315a0015d6SChris Zankel 
432f615136cSMax Filippov #ifdef CONFIG_SMP
43349b424feSMax Filippov void secondary_trap_init(void)
434f615136cSMax Filippov {
435f615136cSMax Filippov 	trap_init_excsave();
4366ec7026aSMax Filippov 	trap_init_debug();
437f615136cSMax Filippov }
438f615136cSMax Filippov #endif
439f615136cSMax Filippov 
4405a0015d6SChris Zankel /*
4415a0015d6SChris Zankel  * This function dumps the current valid window frame and other base registers.
4425a0015d6SChris Zankel  */
4435a0015d6SChris Zankel 
4445a0015d6SChris Zankel void show_regs(struct pt_regs * regs)
4455a0015d6SChris Zankel {
446431d1a34SMax Filippov 	int i;
4475a0015d6SChris Zankel 
448a43cb95dSTejun Heo 	show_regs_print_info(KERN_DEFAULT);
449a43cb95dSTejun Heo 
4508d7e8240SChris Zankel 	for (i = 0; i < 16; i++) {
4515a0015d6SChris Zankel 		if ((i % 8) == 0)
452d4eccafcSMax Filippov 			pr_info("a%02d:", i);
453d4eccafcSMax Filippov 		pr_cont(" %08lx", regs->areg[i]);
4545a0015d6SChris Zankel 	}
455d4eccafcSMax Filippov 	pr_cont("\n");
456d4eccafcSMax Filippov 	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
4575a0015d6SChris Zankel 		regs->pc, regs->ps, regs->depc, regs->excvaddr);
458d4eccafcSMax Filippov 	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
4595a0015d6SChris Zankel 		regs->lbeg, regs->lend, regs->lcount, regs->sar);
4605a0015d6SChris Zankel 	if (user_mode(regs))
461d4eccafcSMax Filippov 		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
4625a0015d6SChris Zankel 			regs->windowbase, regs->windowstart, regs->wmask,
4635a0015d6SChris Zankel 			regs->syscall);
4645a0015d6SChris Zankel }
4655a0015d6SChris Zankel 
4663e4196a5SMax Filippov static int show_trace_cb(struct stackframe *frame, void *data)
467586411dcSJohannes Weiner {
46847fb7029SDmitry Safonov 	const char *loglvl = data;
46947fb7029SDmitry Safonov 
470e640cc30SMax Filippov 	if (kernel_text_address(frame->pc))
47147fb7029SDmitry Safonov 		printk("%s [<%08lx>] %pB\n",
47247fb7029SDmitry Safonov 			loglvl, frame->pc, (void *)frame->pc);
4733e4196a5SMax Filippov 	return 0;
474586411dcSJohannes Weiner }
475586411dcSJohannes Weiner 
47647fb7029SDmitry Safonov static void show_trace(struct task_struct *task, unsigned long *sp,
47747fb7029SDmitry Safonov 		       const char *loglvl)
4785a0015d6SChris Zankel {
4793e4196a5SMax Filippov 	if (!sp)
4803e4196a5SMax Filippov 		sp = stack_pointer(task);
4815a0015d6SChris Zankel 
48247fb7029SDmitry Safonov 	printk("%sCall Trace:\n", loglvl);
48347fb7029SDmitry Safonov 	walk_stackframe(sp, show_trace_cb, (void *)loglvl);
4845a0015d6SChris Zankel }
4855a0015d6SChris Zankel 
486c5fccebcSMax Filippov #define STACK_DUMP_ENTRY_SIZE 4
487c5fccebcSMax Filippov #define STACK_DUMP_LINE_SIZE 32
4888951eb15SMax Filippov static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
4895a0015d6SChris Zankel 
4909cb8f069SDmitry Safonov void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
4915a0015d6SChris Zankel {
492c5fccebcSMax Filippov 	size_t len;
4935a0015d6SChris Zankel 
49428a0ce7fSJohannes Weiner 	if (!sp)
495586411dcSJohannes Weiner 		sp = stack_pointer(task);
496c5fccebcSMax Filippov 
497c5fccebcSMax Filippov 	len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE),
498c5fccebcSMax Filippov 		  kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE);
4995a0015d6SChris Zankel 
50020da1e8bSDmitry Safonov 	printk("%sStack:\n", loglvl);
50120da1e8bSDmitry Safonov 	print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE,
502c5fccebcSMax Filippov 		       STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE,
503c5fccebcSMax Filippov 		       sp, len, false);
50420da1e8bSDmitry Safonov 	show_trace(task, sp, loglvl);
50520da1e8bSDmitry Safonov }
50620da1e8bSDmitry Safonov 
50734af946aSIngo Molnar DEFINE_SPINLOCK(die_lock);
5085a0015d6SChris Zankel 
5099fd5a04dSEric W. Biederman void __noreturn die(const char * str, struct pt_regs * regs, long err)
5105a0015d6SChris Zankel {
5115a0015d6SChris Zankel 	static int die_counter;
5126c5260d7SThomas Gleixner 	const char *pr = "";
5136c5260d7SThomas Gleixner 
5146c5260d7SThomas Gleixner 	if (IS_ENABLED(CONFIG_PREEMPTION))
5156c5260d7SThomas Gleixner 		pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
5165a0015d6SChris Zankel 
5175a0015d6SChris Zankel 	console_verbose();
5185a0015d6SChris Zankel 	spin_lock_irq(&die_lock);
5195a0015d6SChris Zankel 
5206c5260d7SThomas Gleixner 	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr);
5215a0015d6SChris Zankel 	show_regs(regs);
5225a0015d6SChris Zankel 	if (!user_mode(regs))
5239cb8f069SDmitry Safonov 		show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO);
5245a0015d6SChris Zankel 
525373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5265a0015d6SChris Zankel 	spin_unlock_irq(&die_lock);
5275a0015d6SChris Zankel 
5285a0015d6SChris Zankel 	if (in_interrupt())
5295a0015d6SChris Zankel 		panic("Fatal exception in interrupt");
5305a0015d6SChris Zankel 
531cea6a4baSHorms 	if (panic_on_oops)
532012c437dSHorms 		panic("Fatal exception");
533cea6a4baSHorms 
5340e25498fSEric W. Biederman 	make_task_dead(err);
5355a0015d6SChris Zankel }
536