xref: /linux/arch/xtensa/kernel/traps.c (revision 1c4087e97eb53b45709d12d2c96f03e26bcaee12)
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 
26*1c4087e9SRandy Dunlap #include <linux/cpu.h>
275a0015d6SChris Zankel #include <linux/kernel.h>
283f07c014SIngo Molnar #include <linux/sched/signal.h>
29b17b0153SIngo Molnar #include <linux/sched/debug.h>
303f8c2452SIngo Molnar #include <linux/sched/task_stack.h>
315a0015d6SChris Zankel #include <linux/init.h>
325a0015d6SChris Zankel #include <linux/module.h>
335a0015d6SChris Zankel #include <linux/stringify.h>
345a0015d6SChris Zankel #include <linux/kallsyms.h>
355c888d53SNishanth Aravamudan #include <linux/delay.h>
365a891ed5SAlexey Dobriyan #include <linux/hardirq.h>
37c130d3beSMax Filippov #include <linux/ratelimit.h>
3865fddcfcSMike Rapoport #include <linux/pgtable.h>
395a0015d6SChris Zankel 
403e4196a5SMax Filippov #include <asm/stacktrace.h>
415a0015d6SChris Zankel #include <asm/ptrace.h>
425a0015d6SChris Zankel #include <asm/timex.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
445a0015d6SChris Zankel #include <asm/processor.h>
452d6f82feSMax Filippov #include <asm/traps.h>
46c91e02bdSMax Filippov #include <asm/hw_breakpoint.h>
475a0015d6SChris Zankel 
485a0015d6SChris Zankel /*
495a0015d6SChris Zankel  * Machine specific interrupt handlers
505a0015d6SChris Zankel  */
515a0015d6SChris Zankel 
52db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs);
53408b1d3cSMax Filippov static void do_div0(struct pt_regs *regs);
54db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs);
55db0d07faSMax Filippov #if XTENSA_FAKE_NMI
56db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs);
57db0d07faSMax Filippov #endif
58f29cf776SMax Filippov #ifdef CONFIG_XTENSA_LOAD_STORE
59f29cf776SMax Filippov static void do_load_store(struct pt_regs *regs);
60f29cf776SMax Filippov #endif
61db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs);
62db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs);
6311e969bcSMax Filippov #if XTENSA_HAVE_COPROCESSORS
6411e969bcSMax Filippov static void do_coprocessor(struct pt_regs *regs);
6511e969bcSMax Filippov #endif
66db0d07faSMax Filippov static void do_debug(struct pt_regs *regs);
675a0015d6SChris Zankel 
685a0015d6SChris Zankel /*
695a0015d6SChris Zankel  * The vector table must be preceded by a save area (which
705a0015d6SChris Zankel  * implies it must be in RAM, unless one places RAM immediately
715a0015d6SChris Zankel  * before a ROM and puts the vector at the start of the ROM (!))
725a0015d6SChris Zankel  */
735a0015d6SChris Zankel 
745a0015d6SChris Zankel #define KRNL		0x01
755a0015d6SChris Zankel #define USER		0x02
765a0015d6SChris Zankel 
775a0015d6SChris Zankel #define COPROCESSOR(x)							\
7811e969bcSMax Filippov { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER|KRNL, fast_coprocessor },\
7911e969bcSMax Filippov { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, 0, do_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 
8909f8a6dbSMax Filippov #ifdef CONFIG_USER_ABI_CALL0_PROBE
9009f8a6dbSMax Filippov { EXCCAUSE_ILLEGAL_INSTRUCTION,	USER,	   fast_illegal_instruction_user },
9109f8a6dbSMax Filippov #endif
92173d6681SChris Zankel { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
93173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
94173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
95173d6681SChris Zankel /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
96f29cf776SMax Filippov #ifdef CONFIG_XTENSA_LOAD_STORE
97f29cf776SMax Filippov { EXCCAUSE_LOAD_STORE_ERROR,	USER|KRNL, fast_load_store },
98f29cf776SMax Filippov { EXCCAUSE_LOAD_STORE_ERROR,	0,	   do_load_store },
99f29cf776SMax Filippov #endif
100173d6681SChris Zankel { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
101da0a4e5cSMax Filippov #ifdef SUPPORT_WINDOWED
102173d6681SChris Zankel { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
103da0a4e5cSMax Filippov #endif
104408b1d3cSMax Filippov { EXCCAUSE_INTEGER_DIVIDE_BY_ZERO, 0,	   do_div0 },
105173d6681SChris Zankel /* EXCCAUSE_PRIVILEGED unhandled */
106a160e941SMax Filippov #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION || \
107a160e941SMax Filippov 		IS_ENABLED(CONFIG_XTENSA_LOAD_STORE)
1084ded6282SMax Filippov #ifdef CONFIG_XTENSA_UNALIGNED_USER
109173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
1105a0015d6SChris Zankel #endif
111173d6681SChris Zankel { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
1125a0015d6SChris Zankel #endif
1133522bcfeSMax Filippov { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
114e5083a63SJohannes Weiner #ifdef CONFIG_MMU
115173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,			0,	   do_page_fault },
116173d6681SChris Zankel { EXCCAUSE_ITLB_MISS,			USER|KRNL, fast_second_level_miss},
117173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,			USER|KRNL, fast_second_level_miss},
118173d6681SChris Zankel { EXCCAUSE_DTLB_MISS,			0,	   do_page_fault },
119a8f0c31fSMax Filippov { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
120a8f0c31fSMax Filippov #endif /* CONFIG_MMU */
121a8f0c31fSMax Filippov #ifdef CONFIG_PFAULT
122a8f0c31fSMax Filippov { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
123a8f0c31fSMax Filippov { EXCCAUSE_ITLB_PRIVILEGE,		0,	   do_page_fault },
124a8f0c31fSMax Filippov { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
125173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
126173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE,		0,	   do_page_fault },
127173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
128173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
129a8f0c31fSMax Filippov #endif
1305a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
131c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0)
1325a0015d6SChris Zankel COPROCESSOR(0),
1335a0015d6SChris Zankel #endif
134c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1)
1355a0015d6SChris Zankel COPROCESSOR(1),
1365a0015d6SChris Zankel #endif
137c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2)
1385a0015d6SChris Zankel COPROCESSOR(2),
1395a0015d6SChris Zankel #endif
140c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3)
1415a0015d6SChris Zankel COPROCESSOR(3),
1425a0015d6SChris Zankel #endif
143c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4)
1445a0015d6SChris Zankel COPROCESSOR(4),
1455a0015d6SChris Zankel #endif
146c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5)
1475a0015d6SChris Zankel COPROCESSOR(5),
1485a0015d6SChris Zankel #endif
149c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6)
1505a0015d6SChris Zankel COPROCESSOR(6),
1515a0015d6SChris Zankel #endif
152c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7)
1535a0015d6SChris Zankel COPROCESSOR(7),
1545a0015d6SChris Zankel #endif
15538fef73cSMax Filippov #if XTENSA_FAKE_NMI
15638fef73cSMax Filippov { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
15738fef73cSMax Filippov #endif
1585a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
1595a0015d6SChris Zankel { -1, -1, 0 }
1605a0015d6SChris Zankel 
1615a0015d6SChris Zankel };
1625a0015d6SChris Zankel 
1635a0015d6SChris Zankel /* The exception table <exc_table> serves two functions:
1645a0015d6SChris Zankel  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
1655a0015d6SChris Zankel  * 2. it is a temporary memory buffer for the exception handlers.
1665a0015d6SChris Zankel  */
1675a0015d6SChris Zankel 
168f21a79caSMax Filippov DEFINE_PER_CPU(struct exc_table, exc_table);
1696ec7026aSMax Filippov DEFINE_PER_CPU(struct debug_table, debug_table);
1706ec7026aSMax Filippov 
1715a0015d6SChris Zankel void die(const char*, struct pt_regs*, long);
1725a0015d6SChris Zankel 
1735a0015d6SChris Zankel static inline void
1745a0015d6SChris Zankel __die_if_kernel(const char *str, struct pt_regs *regs, long err)
1755a0015d6SChris Zankel {
1765a0015d6SChris Zankel 	if (!user_mode(regs))
1775a0015d6SChris Zankel 		die(str, regs, err);
1785a0015d6SChris Zankel }
1795a0015d6SChris Zankel 
180f7667ca1SMax Filippov #ifdef CONFIG_PRINT_USER_CODE_ON_UNHANDLED_EXCEPTION
181f7667ca1SMax Filippov static inline void dump_user_code(struct pt_regs *regs)
182f7667ca1SMax Filippov {
183f7667ca1SMax Filippov 	char buf[32];
184f7667ca1SMax Filippov 
185f7667ca1SMax Filippov 	if (copy_from_user(buf, (void __user *)(regs->pc & -16), sizeof(buf)) == 0) {
186f7667ca1SMax Filippov 		print_hex_dump(KERN_INFO, " ", DUMP_PREFIX_NONE,
187f7667ca1SMax Filippov 			       32, 1, buf, sizeof(buf), false);
188f7667ca1SMax Filippov 
189f7667ca1SMax Filippov 	}
190f7667ca1SMax Filippov }
191f7667ca1SMax Filippov #else
192f7667ca1SMax Filippov static inline void dump_user_code(struct pt_regs *regs)
193f7667ca1SMax Filippov {
194f7667ca1SMax Filippov }
195f7667ca1SMax Filippov #endif
196f7667ca1SMax Filippov 
1975a0015d6SChris Zankel /*
1985a0015d6SChris Zankel  * Unhandled Exceptions. Kill user task or panic if in kernel space.
1995a0015d6SChris Zankel  */
2005a0015d6SChris Zankel 
201fc55402bSMax Filippov void do_unhandled(struct pt_regs *regs)
2025a0015d6SChris Zankel {
2035a0015d6SChris Zankel 	__die_if_kernel("Caught unhandled exception - should not happen",
2045a0015d6SChris Zankel 			regs, SIGKILL);
2055a0015d6SChris Zankel 
2065a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process */
207c130d3beSMax Filippov 	pr_info_ratelimited("Caught unhandled exception in '%s' "
2085a0015d6SChris Zankel 			    "(pid = %d, pc = %#010lx) - should not happen\n"
2095a0015d6SChris Zankel 			    "\tEXCCAUSE is %ld\n",
210c130d3beSMax Filippov 			    current->comm, task_pid_nr(current), regs->pc,
211fc55402bSMax Filippov 			    regs->exccause);
212f7667ca1SMax Filippov 	dump_user_code(regs);
2133cf5d076SEric W. Biederman 	force_sig(SIGILL);
2145a0015d6SChris Zankel }
2155a0015d6SChris Zankel 
2165a0015d6SChris Zankel /*
2175a0015d6SChris Zankel  * Multi-hit exception. This if fatal!
2185a0015d6SChris Zankel  */
2195a0015d6SChris Zankel 
220db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs)
2215a0015d6SChris Zankel {
2225a0015d6SChris Zankel 	die("Caught multihit exception", regs, SIGKILL);
2235a0015d6SChris Zankel }
2245a0015d6SChris Zankel 
2255a0015d6SChris Zankel /*
2262d1c645cSMarc Gauthier  * IRQ handler.
2275a0015d6SChris Zankel  */
2285a0015d6SChris Zankel 
22938fef73cSMax Filippov #if XTENSA_FAKE_NMI
23038fef73cSMax Filippov 
231e4629194SMax Filippov #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
232e4629194SMax Filippov 
233e4629194SMax Filippov #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
234e4629194SMax Filippov       IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
235e4629194SMax Filippov #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
236e4629194SMax Filippov #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
237e4629194SMax Filippov 
238e4629194SMax Filippov static inline void check_valid_nmi(void)
239e4629194SMax Filippov {
240cad6fadeSMax Filippov 	unsigned intread = xtensa_get_sr(interrupt);
241cad6fadeSMax Filippov 	unsigned intenable = xtensa_get_sr(intenable);
242e4629194SMax Filippov 
243e4629194SMax Filippov 	BUG_ON(intread & intenable &
244e4629194SMax Filippov 	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
245e4629194SMax Filippov 		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
246e4629194SMax Filippov 		 BIT(XCHAL_PROFILING_INTERRUPT)));
247e4629194SMax Filippov }
248e4629194SMax Filippov 
249e4629194SMax Filippov #else
250e4629194SMax Filippov 
251e4629194SMax Filippov static inline void check_valid_nmi(void)
252e4629194SMax Filippov {
253e4629194SMax Filippov }
254e4629194SMax Filippov 
255e4629194SMax Filippov #endif
256e4629194SMax Filippov 
25738fef73cSMax Filippov irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
25838fef73cSMax Filippov 
25938fef73cSMax Filippov DEFINE_PER_CPU(unsigned long, nmi_count);
26038fef73cSMax Filippov 
261db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs)
26238fef73cSMax Filippov {
263de4415d0SMax Filippov 	struct pt_regs *old_regs = set_irq_regs(regs);
26438fef73cSMax Filippov 
26538fef73cSMax Filippov 	nmi_enter();
26638fef73cSMax Filippov 	++*this_cpu_ptr(&nmi_count);
267e4629194SMax Filippov 	check_valid_nmi();
26838fef73cSMax Filippov 	xtensa_pmu_irq_handler(0, NULL);
26938fef73cSMax Filippov 	nmi_exit();
27038fef73cSMax Filippov 	set_irq_regs(old_regs);
27138fef73cSMax Filippov }
27238fef73cSMax Filippov #endif
27338fef73cSMax Filippov 
274db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs)
2755a0015d6SChris Zankel {
2762d1c645cSMarc Gauthier 	static const unsigned int_level_mask[] = {
2772d1c645cSMarc Gauthier 		0,
2782d1c645cSMarc Gauthier 		XCHAL_INTLEVEL1_MASK,
2792d1c645cSMarc Gauthier 		XCHAL_INTLEVEL2_MASK,
2802d1c645cSMarc Gauthier 		XCHAL_INTLEVEL3_MASK,
2812d1c645cSMarc Gauthier 		XCHAL_INTLEVEL4_MASK,
2822d1c645cSMarc Gauthier 		XCHAL_INTLEVEL5_MASK,
2832d1c645cSMarc Gauthier 		XCHAL_INTLEVEL6_MASK,
2842d1c645cSMarc Gauthier 		XCHAL_INTLEVEL7_MASK,
2852d1c645cSMarc Gauthier 	};
286de4415d0SMax Filippov 	struct pt_regs *old_regs = set_irq_regs(regs);
28743ba2237SMax Filippov 	unsigned unhandled = ~0u;
28899623239SMax Filippov 
28999623239SMax Filippov 	irq_enter();
2902d1c645cSMarc Gauthier 
2912d1c645cSMarc Gauthier 	for (;;) {
292cad6fadeSMax Filippov 		unsigned intread = xtensa_get_sr(interrupt);
293cad6fadeSMax Filippov 		unsigned intenable = xtensa_get_sr(intenable);
294895666a9SMax Filippov 		unsigned int_at_level = intread & intenable;
295895666a9SMax Filippov 		unsigned level;
2962d1c645cSMarc Gauthier 
297895666a9SMax Filippov 		for (level = LOCKLEVEL; level > 0; --level) {
298895666a9SMax Filippov 			if (int_at_level & int_level_mask[level]) {
299895666a9SMax Filippov 				int_at_level &= int_level_mask[level];
30043ba2237SMax Filippov 				if (int_at_level & unhandled)
30143ba2237SMax Filippov 					int_at_level &= unhandled;
30243ba2237SMax Filippov 				else
30343ba2237SMax Filippov 					unhandled |= int_level_mask[level];
304895666a9SMax Filippov 				break;
305895666a9SMax Filippov 			}
306895666a9SMax Filippov 		}
307895666a9SMax Filippov 
308895666a9SMax Filippov 		if (level == 0)
30999623239SMax Filippov 			break;
3102d1c645cSMarc Gauthier 
31143ba2237SMax Filippov 		/* clear lowest pending irq in the unhandled mask */
31243ba2237SMax Filippov 		unhandled ^= (int_at_level & -int_at_level);
31399623239SMax Filippov 		do_IRQ(__ffs(int_at_level), regs);
31499623239SMax Filippov 	}
3155a0015d6SChris Zankel 
31699623239SMax Filippov 	irq_exit();
31799623239SMax Filippov 	set_irq_regs(old_regs);
3185a0015d6SChris Zankel }
3195a0015d6SChris Zankel 
320d7486200SMax Filippov static bool check_div0(struct pt_regs *regs)
321d7486200SMax Filippov {
322d7486200SMax Filippov 	static const u8 pattern[] = {'D', 'I', 'V', '0'};
323d7486200SMax Filippov 	const u8 *p;
324d7486200SMax Filippov 	u8 buf[5];
325d7486200SMax Filippov 
326d7486200SMax Filippov 	if (user_mode(regs)) {
327d7486200SMax Filippov 		if (copy_from_user(buf, (void __user *)regs->pc + 2, 5))
328dc60001eSYang Li 			return false;
329d7486200SMax Filippov 		p = buf;
330d7486200SMax Filippov 	} else {
331d7486200SMax Filippov 		p = (const u8 *)regs->pc + 2;
332d7486200SMax Filippov 	}
333d7486200SMax Filippov 
334d7486200SMax Filippov 	return memcmp(p, pattern, sizeof(pattern)) == 0 ||
335d7486200SMax Filippov 		memcmp(p + 1, pattern, sizeof(pattern)) == 0;
336d7486200SMax Filippov }
337d7486200SMax Filippov 
3385a0015d6SChris Zankel /*
3395a0015d6SChris Zankel  * Illegal instruction. Fatal if in kernel space.
3405a0015d6SChris Zankel  */
3415a0015d6SChris Zankel 
342db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs)
3435a0015d6SChris Zankel {
3445cc5f19fSMax Filippov #ifdef CONFIG_USER_ABI_CALL0_PROBE
3455cc5f19fSMax Filippov 	/*
3465cc5f19fSMax Filippov 	 * When call0 application encounters an illegal instruction fast
3475cc5f19fSMax Filippov 	 * exception handler will attempt to set PS.WOE and retry failing
3485cc5f19fSMax Filippov 	 * instruction.
3495cc5f19fSMax Filippov 	 * If we get here we know that that instruction is also illegal
3505cc5f19fSMax Filippov 	 * with PS.WOE set, so it's not related to the windowed option
3515cc5f19fSMax Filippov 	 * hence PS.WOE may be cleared.
3525cc5f19fSMax Filippov 	 */
3535cc5f19fSMax Filippov 	if (regs->pc == current_thread_info()->ps_woe_fix_addr)
3545cc5f19fSMax Filippov 		regs->ps &= ~PS_WOE_MASK;
3555cc5f19fSMax Filippov #endif
356d7486200SMax Filippov 	if (check_div0(regs)) {
357d7486200SMax Filippov 		do_div0(regs);
358d7486200SMax Filippov 		return;
359d7486200SMax Filippov 	}
360d7486200SMax Filippov 
3615a0015d6SChris Zankel 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
3625a0015d6SChris Zankel 
3635a0015d6SChris Zankel 	/* If in user mode, send SIGILL signal to current process. */
3645a0015d6SChris Zankel 
365c130d3beSMax Filippov 	pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
36619c5870cSAlexey Dobriyan 			    current->comm, task_pid_nr(current), regs->pc);
3673cf5d076SEric W. Biederman 	force_sig(SIGILL);
3685a0015d6SChris Zankel }
3695a0015d6SChris Zankel 
370408b1d3cSMax Filippov static void do_div0(struct pt_regs *regs)
371408b1d3cSMax Filippov {
372408b1d3cSMax Filippov 	__die_if_kernel("Unhandled division by 0 in kernel", regs, SIGKILL);
373408b1d3cSMax Filippov 	force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->pc);
374408b1d3cSMax Filippov }
3755a0015d6SChris Zankel 
376f29cf776SMax Filippov #ifdef CONFIG_XTENSA_LOAD_STORE
377f29cf776SMax Filippov static void do_load_store(struct pt_regs *regs)
378f29cf776SMax Filippov {
379f29cf776SMax Filippov 	__die_if_kernel("Unhandled load/store exception in kernel",
380f29cf776SMax Filippov 			regs, SIGKILL);
381f29cf776SMax Filippov 
382f29cf776SMax Filippov 	pr_info_ratelimited("Load/store error to %08lx in '%s' (pid = %d, pc = %#010lx)\n",
383f29cf776SMax Filippov 			    regs->excvaddr, current->comm,
384f29cf776SMax Filippov 			    task_pid_nr(current), regs->pc);
385f29cf776SMax Filippov 	force_sig_fault(SIGBUS, BUS_ADRERR, (void *)regs->excvaddr);
386f29cf776SMax Filippov }
387f29cf776SMax Filippov #endif
388f29cf776SMax Filippov 
3895a0015d6SChris Zankel /*
3905a0015d6SChris Zankel  * Handle unaligned memory accesses from user space. Kill task.
3915a0015d6SChris Zankel  *
3925a0015d6SChris Zankel  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
3935a0015d6SChris Zankel  * accesses causes from user space.
3945a0015d6SChris Zankel  */
3955a0015d6SChris Zankel 
396db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs)
3975a0015d6SChris Zankel {
3985a0015d6SChris Zankel 	__die_if_kernel("Unhandled unaligned exception in kernel",
3995a0015d6SChris Zankel 			regs, SIGKILL);
4005a0015d6SChris Zankel 
401c130d3beSMax Filippov 	pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
4025a0015d6SChris Zankel 			    "(pid = %d, pc = %#010lx)\n",
403c130d3beSMax Filippov 			    regs->excvaddr, current->comm,
404c130d3beSMax Filippov 			    task_pid_nr(current), regs->pc);
4052e1661d2SEric W. Biederman 	force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
4065a0015d6SChris Zankel }
4075a0015d6SChris Zankel 
40811e969bcSMax Filippov #if XTENSA_HAVE_COPROCESSORS
40911e969bcSMax Filippov static void do_coprocessor(struct pt_regs *regs)
41011e969bcSMax Filippov {
41111e969bcSMax Filippov 	coprocessor_flush_release_all(current_thread_info());
41211e969bcSMax Filippov }
41311e969bcSMax Filippov #endif
41411e969bcSMax Filippov 
415c91e02bdSMax Filippov /* Handle debug events.
416c91e02bdSMax Filippov  * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
417c91e02bdSMax Filippov  * preemption disabled to avoid rescheduling and keep mapping of hardware
418c91e02bdSMax Filippov  * breakpoint structures to debug registers intact, so that
419c91e02bdSMax Filippov  * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
420c91e02bdSMax Filippov  */
421db0d07faSMax Filippov static void do_debug(struct pt_regs *regs)
4225a0015d6SChris Zankel {
423c91e02bdSMax Filippov #ifdef CONFIG_HAVE_HW_BREAKPOINT
424c91e02bdSMax Filippov 	int ret = check_hw_breakpoint(regs);
425c91e02bdSMax Filippov 
426c91e02bdSMax Filippov 	preempt_enable();
427c91e02bdSMax Filippov 	if (ret == 0)
428c91e02bdSMax Filippov 		return;
429c91e02bdSMax Filippov #endif
4305a0015d6SChris Zankel 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
4315a0015d6SChris Zankel 
4325a0015d6SChris Zankel 	/* If in user mode, send SIGTRAP signal to current process */
4335a0015d6SChris Zankel 
4343cf5d076SEric W. Biederman 	force_sig(SIGTRAP);
4355a0015d6SChris Zankel }
4365a0015d6SChris Zankel 
4375a0015d6SChris Zankel 
438f21a79caSMax Filippov #define set_handler(type, cause, handler)				\
439f21a79caSMax Filippov 	do {								\
440f21a79caSMax Filippov 		unsigned int cpu;					\
441f21a79caSMax Filippov 									\
442f21a79caSMax Filippov 		for_each_possible_cpu(cpu)				\
443f21a79caSMax Filippov 			per_cpu(exc_table, cpu).type[cause] = (handler);\
444f21a79caSMax Filippov 	} while (0)
445f615136cSMax Filippov 
44628570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */
44728570e8dSMax Filippov 
448fc55402bSMax Filippov xtensa_exception_handler *
449fc55402bSMax Filippov __init trap_set_handler(int cause, xtensa_exception_handler *handler)
45028570e8dSMax Filippov {
451f21a79caSMax Filippov 	void *previous = per_cpu(exc_table, 0).default_handler[cause];
452f21a79caSMax Filippov 
453f21a79caSMax Filippov 	set_handler(default_handler, cause, handler);
45428570e8dSMax Filippov 	return previous;
45528570e8dSMax Filippov }
45628570e8dSMax Filippov 
45728570e8dSMax Filippov 
45849b424feSMax Filippov static void trap_init_excsave(void)
459f615136cSMax Filippov {
4609fa8c59fSMax Filippov 	xtensa_set_sr(this_cpu_ptr(&exc_table), excsave1);
461f615136cSMax Filippov }
462f615136cSMax Filippov 
4636ec7026aSMax Filippov static void trap_init_debug(void)
4646ec7026aSMax Filippov {
4656ec7026aSMax Filippov 	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
4666ec7026aSMax Filippov 
4676ec7026aSMax Filippov 	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
4686ec7026aSMax Filippov 	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
4696ec7026aSMax Filippov 			     :: "a"(debugsave));
4706ec7026aSMax Filippov }
4716ec7026aSMax Filippov 
4725a0015d6SChris Zankel /*
4735a0015d6SChris Zankel  * Initialize dispatch tables.
4745a0015d6SChris Zankel  *
4755a0015d6SChris Zankel  * The exception vectors are stored compressed the __init section in the
4765a0015d6SChris Zankel  * dispatch_init_table. This function initializes the following three tables
4775a0015d6SChris Zankel  * from that compressed table:
4785a0015d6SChris Zankel  * - fast user		first dispatch table for user exceptions
4795a0015d6SChris Zankel  * - fast kernel	first dispatch table for kernel exceptions
4805a0015d6SChris Zankel  * - default C-handler	C-handler called by the default fast handler.
4815a0015d6SChris Zankel  *
4825a0015d6SChris Zankel  * See vectors.S for more details.
4835a0015d6SChris Zankel  */
4845a0015d6SChris Zankel 
485b91dc336SChris Zankel void __init trap_init(void)
4865a0015d6SChris Zankel {
4875a0015d6SChris Zankel 	int i;
4885a0015d6SChris Zankel 
4895a0015d6SChris Zankel 	/* Setup default vectors. */
4905a0015d6SChris Zankel 
491f21a79caSMax Filippov 	for (i = 0; i < EXCCAUSE_N; i++) {
492f21a79caSMax Filippov 		set_handler(fast_user_handler, i, user_exception);
493f21a79caSMax Filippov 		set_handler(fast_kernel_handler, i, kernel_exception);
494f21a79caSMax Filippov 		set_handler(default_handler, i, do_unhandled);
4955a0015d6SChris Zankel 	}
4965a0015d6SChris Zankel 
4975a0015d6SChris Zankel 	/* Setup specific handlers. */
4985a0015d6SChris Zankel 
4995a0015d6SChris Zankel 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
5005a0015d6SChris Zankel 		int fast = dispatch_init_table[i].fast;
5015a0015d6SChris Zankel 		int cause = dispatch_init_table[i].cause;
5025a0015d6SChris Zankel 		void *handler = dispatch_init_table[i].handler;
5035a0015d6SChris Zankel 
5045a0015d6SChris Zankel 		if (fast == 0)
505f21a79caSMax Filippov 			set_handler(default_handler, cause, handler);
50660deebe6SMax Filippov 		if ((fast & USER) != 0)
507f21a79caSMax Filippov 			set_handler(fast_user_handler, cause, handler);
50860deebe6SMax Filippov 		if ((fast & KRNL) != 0)
509f21a79caSMax Filippov 			set_handler(fast_kernel_handler, cause, handler);
5105a0015d6SChris Zankel 	}
5115a0015d6SChris Zankel 
5125a0015d6SChris Zankel 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
513f615136cSMax Filippov 	trap_init_excsave();
5146ec7026aSMax Filippov 	trap_init_debug();
5155a0015d6SChris Zankel }
5165a0015d6SChris Zankel 
517f615136cSMax Filippov #ifdef CONFIG_SMP
51849b424feSMax Filippov void secondary_trap_init(void)
519f615136cSMax Filippov {
520f615136cSMax Filippov 	trap_init_excsave();
5216ec7026aSMax Filippov 	trap_init_debug();
522f615136cSMax Filippov }
523f615136cSMax Filippov #endif
524f615136cSMax Filippov 
5255a0015d6SChris Zankel /*
5265a0015d6SChris Zankel  * This function dumps the current valid window frame and other base registers.
5275a0015d6SChris Zankel  */
5285a0015d6SChris Zankel 
5295a0015d6SChris Zankel void show_regs(struct pt_regs * regs)
5305a0015d6SChris Zankel {
531431d1a34SMax Filippov 	int i;
5325a0015d6SChris Zankel 
533a43cb95dSTejun Heo 	show_regs_print_info(KERN_DEFAULT);
534a43cb95dSTejun Heo 
5358d7e8240SChris Zankel 	for (i = 0; i < 16; i++) {
5365a0015d6SChris Zankel 		if ((i % 8) == 0)
537d4eccafcSMax Filippov 			pr_info("a%02d:", i);
538d4eccafcSMax Filippov 		pr_cont(" %08lx", regs->areg[i]);
5395a0015d6SChris Zankel 	}
540d4eccafcSMax Filippov 	pr_cont("\n");
541d4eccafcSMax Filippov 	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
5425a0015d6SChris Zankel 		regs->pc, regs->ps, regs->depc, regs->excvaddr);
543d4eccafcSMax Filippov 	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
5445a0015d6SChris Zankel 		regs->lbeg, regs->lend, regs->lcount, regs->sar);
5455a0015d6SChris Zankel 	if (user_mode(regs))
546d4eccafcSMax Filippov 		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
5475a0015d6SChris Zankel 			regs->windowbase, regs->windowstart, regs->wmask,
5485a0015d6SChris Zankel 			regs->syscall);
5495a0015d6SChris Zankel }
5505a0015d6SChris Zankel 
5513e4196a5SMax Filippov static int show_trace_cb(struct stackframe *frame, void *data)
552586411dcSJohannes Weiner {
55347fb7029SDmitry Safonov 	const char *loglvl = data;
55447fb7029SDmitry Safonov 
555e640cc30SMax Filippov 	if (kernel_text_address(frame->pc))
55647fb7029SDmitry Safonov 		printk("%s [<%08lx>] %pB\n",
55747fb7029SDmitry Safonov 			loglvl, frame->pc, (void *)frame->pc);
5583e4196a5SMax Filippov 	return 0;
559586411dcSJohannes Weiner }
560586411dcSJohannes Weiner 
56147fb7029SDmitry Safonov static void show_trace(struct task_struct *task, unsigned long *sp,
56247fb7029SDmitry Safonov 		       const char *loglvl)
5635a0015d6SChris Zankel {
5643e4196a5SMax Filippov 	if (!sp)
5653e4196a5SMax Filippov 		sp = stack_pointer(task);
5665a0015d6SChris Zankel 
56747fb7029SDmitry Safonov 	printk("%sCall Trace:\n", loglvl);
56847fb7029SDmitry Safonov 	walk_stackframe(sp, show_trace_cb, (void *)loglvl);
5695a0015d6SChris Zankel }
5705a0015d6SChris Zankel 
571c5fccebcSMax Filippov #define STACK_DUMP_ENTRY_SIZE 4
572cc34f290SMax Filippov #define STACK_DUMP_LINE_SIZE 16
5738951eb15SMax Filippov static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
5745a0015d6SChris Zankel 
575cc34f290SMax Filippov struct stack_fragment
576cc34f290SMax Filippov {
577cc34f290SMax Filippov 	size_t len;
578cc34f290SMax Filippov 	size_t off;
579cc34f290SMax Filippov 	u8 *sp;
580cc34f290SMax Filippov 	const char *loglvl;
581cc34f290SMax Filippov };
582cc34f290SMax Filippov 
583cc34f290SMax Filippov static int show_stack_fragment_cb(struct stackframe *frame, void *data)
584cc34f290SMax Filippov {
585cc34f290SMax Filippov 	struct stack_fragment *sf = data;
586cc34f290SMax Filippov 
587cc34f290SMax Filippov 	while (sf->off < sf->len) {
588cc34f290SMax Filippov 		u8 line[STACK_DUMP_LINE_SIZE];
589cc34f290SMax Filippov 		size_t line_len = sf->len - sf->off > STACK_DUMP_LINE_SIZE ?
590cc34f290SMax Filippov 			STACK_DUMP_LINE_SIZE : sf->len - sf->off;
591cc34f290SMax Filippov 		bool arrow = sf->off == 0;
592cc34f290SMax Filippov 
593cc34f290SMax Filippov 		if (frame && frame->sp == (unsigned long)(sf->sp + sf->off))
594cc34f290SMax Filippov 			arrow = true;
595cc34f290SMax Filippov 
596cc34f290SMax Filippov 		__memcpy(line, sf->sp + sf->off, line_len);
597cc34f290SMax Filippov 		print_hex_dump(sf->loglvl, arrow ? "> " : "  ", DUMP_PREFIX_NONE,
598cc34f290SMax Filippov 			       STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE,
599cc34f290SMax Filippov 			       line, line_len, false);
600cc34f290SMax Filippov 		sf->off += STACK_DUMP_LINE_SIZE;
601cc34f290SMax Filippov 		if (arrow)
602cc34f290SMax Filippov 			return 0;
603cc34f290SMax Filippov 	}
604cc34f290SMax Filippov 	return 1;
605cc34f290SMax Filippov }
606cc34f290SMax Filippov 
6079cb8f069SDmitry Safonov void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
6085a0015d6SChris Zankel {
609cc34f290SMax Filippov 	struct stack_fragment sf;
6105a0015d6SChris Zankel 
61128a0ce7fSJohannes Weiner 	if (!sp)
612586411dcSJohannes Weiner 		sp = stack_pointer(task);
613c5fccebcSMax Filippov 
614cc34f290SMax Filippov 	sf.len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE),
615c5fccebcSMax Filippov 		     kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE);
616cc34f290SMax Filippov 	sf.off = 0;
617cc34f290SMax Filippov 	sf.sp = (u8 *)sp;
618cc34f290SMax Filippov 	sf.loglvl = loglvl;
6195a0015d6SChris Zankel 
62020da1e8bSDmitry Safonov 	printk("%sStack:\n", loglvl);
621cc34f290SMax Filippov 	walk_stackframe(sp, show_stack_fragment_cb, &sf);
622cc34f290SMax Filippov 	while (sf.off < sf.len)
623cc34f290SMax Filippov 		show_stack_fragment_cb(NULL, &sf);
62420da1e8bSDmitry Safonov 	show_trace(task, sp, loglvl);
62520da1e8bSDmitry Safonov }
62620da1e8bSDmitry Safonov 
62734af946aSIngo Molnar DEFINE_SPINLOCK(die_lock);
6285a0015d6SChris Zankel 
6299fd5a04dSEric W. Biederman void __noreturn die(const char * str, struct pt_regs * regs, long err)
6305a0015d6SChris Zankel {
6315a0015d6SChris Zankel 	static int die_counter;
6326c5260d7SThomas Gleixner 	const char *pr = "";
6336c5260d7SThomas Gleixner 
6346c5260d7SThomas Gleixner 	if (IS_ENABLED(CONFIG_PREEMPTION))
6356c5260d7SThomas Gleixner 		pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
6365a0015d6SChris Zankel 
6375a0015d6SChris Zankel 	console_verbose();
6385a0015d6SChris Zankel 	spin_lock_irq(&die_lock);
6395a0015d6SChris Zankel 
6406c5260d7SThomas Gleixner 	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr);
6415a0015d6SChris Zankel 	show_regs(regs);
6425a0015d6SChris Zankel 	if (!user_mode(regs))
6439cb8f069SDmitry Safonov 		show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO);
6445a0015d6SChris Zankel 
645373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
6465a0015d6SChris Zankel 	spin_unlock_irq(&die_lock);
6475a0015d6SChris Zankel 
6485a0015d6SChris Zankel 	if (in_interrupt())
6495a0015d6SChris Zankel 		panic("Fatal exception in interrupt");
6505a0015d6SChris Zankel 
651cea6a4baSHorms 	if (panic_on_oops)
652012c437dSHorms 		panic("Fatal exception");
653cea6a4baSHorms 
6540e25498fSEric W. Biederman 	make_task_dead(err);
6555a0015d6SChris Zankel }
656