xref: /linux/arch/xtensa/kernel/traps.c (revision 55427d5b0dff5754f3579dde93955aac483e8d6a)
1 /*
2  * arch/xtensa/kernel/traps.c
3  *
4  * Exception handling.
5  *
6  * Derived from code with the following copyrights:
7  * Copyright (C) 1994 - 1999 by Ralf Baechle
8  * Modified for R3000 by Paul M. Antoine, 1995, 1996
9  * Complete output from die() by Ulf Carlsson, 1998
10  * Copyright (C) 1999 Silicon Graphics, Inc.
11  *
12  * Essentially rewritten for the Xtensa architecture port.
13  *
14  * Copyright (C) 2001 - 2013 Tensilica Inc.
15  *
16  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
17  * Chris Zankel	<chris@zankel.net>
18  * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca>
19  * Kevin Chea
20  *
21  * This file is subject to the terms and conditions of the GNU General Public
22  * License.  See the file "COPYING" in the main directory of this archive
23  * for more details.
24  */
25 
26 #include <linux/kernel.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sched/debug.h>
29 #include <linux/sched/task_stack.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/stringify.h>
33 #include <linux/kallsyms.h>
34 #include <linux/delay.h>
35 #include <linux/hardirq.h>
36 #include <linux/ratelimit.h>
37 #include <linux/pgtable.h>
38 
39 #include <asm/stacktrace.h>
40 #include <asm/ptrace.h>
41 #include <asm/timex.h>
42 #include <linux/uaccess.h>
43 #include <asm/processor.h>
44 #include <asm/traps.h>
45 #include <asm/hw_breakpoint.h>
46 
47 /*
48  * Machine specific interrupt handlers
49  */
50 
51 extern void kernel_exception(void);
52 extern void user_exception(void);
53 
54 extern void fast_illegal_instruction_user(void);
55 extern void fast_syscall_user(void);
56 extern void fast_alloca(void);
57 extern void fast_unaligned(void);
58 extern void fast_second_level_miss(void);
59 extern void fast_store_prohibited(void);
60 extern void fast_coprocessor(void);
61 
62 extern void do_illegal_instruction (struct pt_regs*);
63 extern void do_interrupt (struct pt_regs*);
64 extern void do_nmi(struct pt_regs *);
65 extern void do_unaligned_user (struct pt_regs*);
66 extern void do_multihit (struct pt_regs*, unsigned long);
67 extern void do_page_fault (struct pt_regs*, unsigned long);
68 extern void do_debug (struct pt_regs*);
69 extern void system_call (struct pt_regs*);
70 
71 /*
72  * The vector table must be preceded by a save area (which
73  * implies it must be in RAM, unless one places RAM immediately
74  * before a ROM and puts the vector at the start of the ROM (!))
75  */
76 
77 #define KRNL		0x01
78 #define USER		0x02
79 
80 #define COPROCESSOR(x)							\
81 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
82 
83 typedef struct {
84 	int cause;
85 	int fast;
86 	void* handler;
87 } dispatch_init_table_t;
88 
89 static dispatch_init_table_t __initdata dispatch_init_table[] = {
90 
91 #ifdef CONFIG_USER_ABI_CALL0_PROBE
92 { EXCCAUSE_ILLEGAL_INSTRUCTION,	USER,	   fast_illegal_instruction_user },
93 #endif
94 { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
95 { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
96 { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
97 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
98 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
99 { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
100 #ifdef SUPPORT_WINDOWED
101 { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
102 #endif
103 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
104 /* EXCCAUSE_PRIVILEGED unhandled */
105 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
106 #ifdef CONFIG_XTENSA_UNALIGNED_USER
107 { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
108 #endif
109 { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
110 { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
111 #endif
112 #ifdef CONFIG_MMU
113 { EXCCAUSE_ITLB_MISS,			0,	   do_page_fault },
114 { EXCCAUSE_ITLB_MISS,			USER|KRNL, fast_second_level_miss},
115 { EXCCAUSE_DTLB_MISS,			USER|KRNL, fast_second_level_miss},
116 { EXCCAUSE_DTLB_MISS,			0,	   do_page_fault },
117 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
118 #endif /* CONFIG_MMU */
119 #ifdef CONFIG_PFAULT
120 { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
121 { EXCCAUSE_ITLB_PRIVILEGE,		0,	   do_page_fault },
122 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
123 { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
124 { EXCCAUSE_DTLB_PRIVILEGE,		0,	   do_page_fault },
125 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
126 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
127 #endif
128 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
129 #if XTENSA_HAVE_COPROCESSOR(0)
130 COPROCESSOR(0),
131 #endif
132 #if XTENSA_HAVE_COPROCESSOR(1)
133 COPROCESSOR(1),
134 #endif
135 #if XTENSA_HAVE_COPROCESSOR(2)
136 COPROCESSOR(2),
137 #endif
138 #if XTENSA_HAVE_COPROCESSOR(3)
139 COPROCESSOR(3),
140 #endif
141 #if XTENSA_HAVE_COPROCESSOR(4)
142 COPROCESSOR(4),
143 #endif
144 #if XTENSA_HAVE_COPROCESSOR(5)
145 COPROCESSOR(5),
146 #endif
147 #if XTENSA_HAVE_COPROCESSOR(6)
148 COPROCESSOR(6),
149 #endif
150 #if XTENSA_HAVE_COPROCESSOR(7)
151 COPROCESSOR(7),
152 #endif
153 #if XTENSA_FAKE_NMI
154 { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
155 #endif
156 { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
157 { -1, -1, 0 }
158 
159 };
160 
161 /* The exception table <exc_table> serves two functions:
162  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
163  * 2. it is a temporary memory buffer for the exception handlers.
164  */
165 
166 DEFINE_PER_CPU(struct exc_table, exc_table);
167 DEFINE_PER_CPU(struct debug_table, debug_table);
168 
169 void die(const char*, struct pt_regs*, long);
170 
171 static inline void
172 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
173 {
174 	if (!user_mode(regs))
175 		die(str, regs, err);
176 }
177 
178 /*
179  * Unhandled Exceptions. Kill user task or panic if in kernel space.
180  */
181 
182 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
183 {
184 	__die_if_kernel("Caught unhandled exception - should not happen",
185 			regs, SIGKILL);
186 
187 	/* If in user mode, send SIGILL signal to current process */
188 	pr_info_ratelimited("Caught unhandled exception in '%s' "
189 			    "(pid = %d, pc = %#010lx) - should not happen\n"
190 			    "\tEXCCAUSE is %ld\n",
191 			    current->comm, task_pid_nr(current), regs->pc,
192 			    exccause);
193 	force_sig(SIGILL);
194 }
195 
196 /*
197  * Multi-hit exception. This if fatal!
198  */
199 
200 void do_multihit(struct pt_regs *regs, unsigned long exccause)
201 {
202 	die("Caught multihit exception", regs, SIGKILL);
203 }
204 
205 /*
206  * IRQ handler.
207  */
208 
209 extern void do_IRQ(int, struct pt_regs *);
210 
211 #if XTENSA_FAKE_NMI
212 
213 #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
214 
215 #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
216       IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
217 #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
218 #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
219 
220 static inline void check_valid_nmi(void)
221 {
222 	unsigned intread = xtensa_get_sr(interrupt);
223 	unsigned intenable = xtensa_get_sr(intenable);
224 
225 	BUG_ON(intread & intenable &
226 	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
227 		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
228 		 BIT(XCHAL_PROFILING_INTERRUPT)));
229 }
230 
231 #else
232 
233 static inline void check_valid_nmi(void)
234 {
235 }
236 
237 #endif
238 
239 irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
240 
241 DEFINE_PER_CPU(unsigned long, nmi_count);
242 
243 void do_nmi(struct pt_regs *regs)
244 {
245 	struct pt_regs *old_regs = set_irq_regs(regs);
246 
247 	nmi_enter();
248 	++*this_cpu_ptr(&nmi_count);
249 	check_valid_nmi();
250 	xtensa_pmu_irq_handler(0, NULL);
251 	nmi_exit();
252 	set_irq_regs(old_regs);
253 }
254 #endif
255 
256 void do_interrupt(struct pt_regs *regs)
257 {
258 	static const unsigned int_level_mask[] = {
259 		0,
260 		XCHAL_INTLEVEL1_MASK,
261 		XCHAL_INTLEVEL2_MASK,
262 		XCHAL_INTLEVEL3_MASK,
263 		XCHAL_INTLEVEL4_MASK,
264 		XCHAL_INTLEVEL5_MASK,
265 		XCHAL_INTLEVEL6_MASK,
266 		XCHAL_INTLEVEL7_MASK,
267 	};
268 	struct pt_regs *old_regs = set_irq_regs(regs);
269 	unsigned unhandled = ~0u;
270 
271 	irq_enter();
272 
273 	for (;;) {
274 		unsigned intread = xtensa_get_sr(interrupt);
275 		unsigned intenable = xtensa_get_sr(intenable);
276 		unsigned int_at_level = intread & intenable;
277 		unsigned level;
278 
279 		for (level = LOCKLEVEL; level > 0; --level) {
280 			if (int_at_level & int_level_mask[level]) {
281 				int_at_level &= int_level_mask[level];
282 				if (int_at_level & unhandled)
283 					int_at_level &= unhandled;
284 				else
285 					unhandled |= int_level_mask[level];
286 				break;
287 			}
288 		}
289 
290 		if (level == 0)
291 			break;
292 
293 		/* clear lowest pending irq in the unhandled mask */
294 		unhandled ^= (int_at_level & -int_at_level);
295 		do_IRQ(__ffs(int_at_level), regs);
296 	}
297 
298 	irq_exit();
299 	set_irq_regs(old_regs);
300 }
301 
302 /*
303  * Illegal instruction. Fatal if in kernel space.
304  */
305 
306 void
307 do_illegal_instruction(struct pt_regs *regs)
308 {
309 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
310 
311 	/* If in user mode, send SIGILL signal to current process. */
312 
313 	pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
314 			    current->comm, task_pid_nr(current), regs->pc);
315 	force_sig(SIGILL);
316 }
317 
318 
319 /*
320  * Handle unaligned memory accesses from user space. Kill task.
321  *
322  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
323  * accesses causes from user space.
324  */
325 
326 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
327 void
328 do_unaligned_user (struct pt_regs *regs)
329 {
330 	__die_if_kernel("Unhandled unaligned exception in kernel",
331 			regs, SIGKILL);
332 
333 	current->thread.bad_vaddr = regs->excvaddr;
334 	current->thread.error_code = -3;
335 	pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
336 			    "(pid = %d, pc = %#010lx)\n",
337 			    regs->excvaddr, current->comm,
338 			    task_pid_nr(current), regs->pc);
339 	force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
340 }
341 #endif
342 
343 /* Handle debug events.
344  * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
345  * preemption disabled to avoid rescheduling and keep mapping of hardware
346  * breakpoint structures to debug registers intact, so that
347  * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
348  */
349 void
350 do_debug(struct pt_regs *regs)
351 {
352 #ifdef CONFIG_HAVE_HW_BREAKPOINT
353 	int ret = check_hw_breakpoint(regs);
354 
355 	preempt_enable();
356 	if (ret == 0)
357 		return;
358 #endif
359 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
360 
361 	/* If in user mode, send SIGTRAP signal to current process */
362 
363 	force_sig(SIGTRAP);
364 }
365 
366 
367 #define set_handler(type, cause, handler)				\
368 	do {								\
369 		unsigned int cpu;					\
370 									\
371 		for_each_possible_cpu(cpu)				\
372 			per_cpu(exc_table, cpu).type[cause] = (handler);\
373 	} while (0)
374 
375 /* Set exception C handler - for temporary use when probing exceptions */
376 
377 void * __init trap_set_handler(int cause, void *handler)
378 {
379 	void *previous = per_cpu(exc_table, 0).default_handler[cause];
380 
381 	set_handler(default_handler, cause, handler);
382 	return previous;
383 }
384 
385 
386 static void trap_init_excsave(void)
387 {
388 	unsigned long excsave1 = (unsigned long)this_cpu_ptr(&exc_table);
389 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (excsave1));
390 }
391 
392 static void trap_init_debug(void)
393 {
394 	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
395 
396 	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
397 	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
398 			     :: "a"(debugsave));
399 }
400 
401 /*
402  * Initialize dispatch tables.
403  *
404  * The exception vectors are stored compressed the __init section in the
405  * dispatch_init_table. This function initializes the following three tables
406  * from that compressed table:
407  * - fast user		first dispatch table for user exceptions
408  * - fast kernel	first dispatch table for kernel exceptions
409  * - default C-handler	C-handler called by the default fast handler.
410  *
411  * See vectors.S for more details.
412  */
413 
414 void __init trap_init(void)
415 {
416 	int i;
417 
418 	/* Setup default vectors. */
419 
420 	for (i = 0; i < EXCCAUSE_N; i++) {
421 		set_handler(fast_user_handler, i, user_exception);
422 		set_handler(fast_kernel_handler, i, kernel_exception);
423 		set_handler(default_handler, i, do_unhandled);
424 	}
425 
426 	/* Setup specific handlers. */
427 
428 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
429 		int fast = dispatch_init_table[i].fast;
430 		int cause = dispatch_init_table[i].cause;
431 		void *handler = dispatch_init_table[i].handler;
432 
433 		if (fast == 0)
434 			set_handler(default_handler, cause, handler);
435 		if ((fast & USER) != 0)
436 			set_handler(fast_user_handler, cause, handler);
437 		if ((fast & KRNL) != 0)
438 			set_handler(fast_kernel_handler, cause, handler);
439 	}
440 
441 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
442 	trap_init_excsave();
443 	trap_init_debug();
444 }
445 
446 #ifdef CONFIG_SMP
447 void secondary_trap_init(void)
448 {
449 	trap_init_excsave();
450 	trap_init_debug();
451 }
452 #endif
453 
454 /*
455  * This function dumps the current valid window frame and other base registers.
456  */
457 
458 void show_regs(struct pt_regs * regs)
459 {
460 	int i;
461 
462 	show_regs_print_info(KERN_DEFAULT);
463 
464 	for (i = 0; i < 16; i++) {
465 		if ((i % 8) == 0)
466 			pr_info("a%02d:", i);
467 		pr_cont(" %08lx", regs->areg[i]);
468 	}
469 	pr_cont("\n");
470 	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
471 		regs->pc, regs->ps, regs->depc, regs->excvaddr);
472 	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
473 		regs->lbeg, regs->lend, regs->lcount, regs->sar);
474 	if (user_mode(regs))
475 		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
476 			regs->windowbase, regs->windowstart, regs->wmask,
477 			regs->syscall);
478 }
479 
480 static int show_trace_cb(struct stackframe *frame, void *data)
481 {
482 	const char *loglvl = data;
483 
484 	if (kernel_text_address(frame->pc))
485 		printk("%s [<%08lx>] %pB\n",
486 			loglvl, frame->pc, (void *)frame->pc);
487 	return 0;
488 }
489 
490 static void show_trace(struct task_struct *task, unsigned long *sp,
491 		       const char *loglvl)
492 {
493 	if (!sp)
494 		sp = stack_pointer(task);
495 
496 	printk("%sCall Trace:\n", loglvl);
497 	walk_stackframe(sp, show_trace_cb, (void *)loglvl);
498 }
499 
500 #define STACK_DUMP_ENTRY_SIZE 4
501 #define STACK_DUMP_LINE_SIZE 32
502 static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
503 
504 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
505 {
506 	size_t len;
507 
508 	if (!sp)
509 		sp = stack_pointer(task);
510 
511 	len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE),
512 		  kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE);
513 
514 	printk("%sStack:\n", loglvl);
515 	print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE,
516 		       STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE,
517 		       sp, len, false);
518 	show_trace(task, sp, loglvl);
519 }
520 
521 DEFINE_SPINLOCK(die_lock);
522 
523 void __noreturn die(const char * str, struct pt_regs * regs, long err)
524 {
525 	static int die_counter;
526 	const char *pr = "";
527 
528 	if (IS_ENABLED(CONFIG_PREEMPTION))
529 		pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
530 
531 	console_verbose();
532 	spin_lock_irq(&die_lock);
533 
534 	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr);
535 	show_regs(regs);
536 	if (!user_mode(regs))
537 		show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO);
538 
539 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
540 	spin_unlock_irq(&die_lock);
541 
542 	if (in_interrupt())
543 		panic("Fatal exception in interrupt");
544 
545 	if (panic_on_oops)
546 		panic("Fatal exception");
547 
548 	make_task_dead(err);
549 }
550