xref: /openbmc/linux/arch/xtensa/kernel/traps.c (revision 6d99a79c)
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 
38 #include <asm/stacktrace.h>
39 #include <asm/ptrace.h>
40 #include <asm/timex.h>
41 #include <linux/uaccess.h>
42 #include <asm/pgtable.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_syscall_kernel(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 { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
92 { EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
93 { EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
94 { EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
95 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
96 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
97 { EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
98 { EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
99 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
100 /* EXCCAUSE_PRIVILEGED unhandled */
101 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
102 #ifdef CONFIG_XTENSA_UNALIGNED_USER
103 { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
104 #endif
105 { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
106 { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
107 #endif
108 #ifdef CONFIG_MMU
109 { EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
110 { EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
111 { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
112 { EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
113 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
114 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
115 { EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
116 { EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
117 { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
118 { EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
119 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
120 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
121 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
122 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
123 #endif /* CONFIG_MMU */
124 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
125 #if XTENSA_HAVE_COPROCESSOR(0)
126 COPROCESSOR(0),
127 #endif
128 #if XTENSA_HAVE_COPROCESSOR(1)
129 COPROCESSOR(1),
130 #endif
131 #if XTENSA_HAVE_COPROCESSOR(2)
132 COPROCESSOR(2),
133 #endif
134 #if XTENSA_HAVE_COPROCESSOR(3)
135 COPROCESSOR(3),
136 #endif
137 #if XTENSA_HAVE_COPROCESSOR(4)
138 COPROCESSOR(4),
139 #endif
140 #if XTENSA_HAVE_COPROCESSOR(5)
141 COPROCESSOR(5),
142 #endif
143 #if XTENSA_HAVE_COPROCESSOR(6)
144 COPROCESSOR(6),
145 #endif
146 #if XTENSA_HAVE_COPROCESSOR(7)
147 COPROCESSOR(7),
148 #endif
149 #if XTENSA_FAKE_NMI
150 { EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
151 #endif
152 { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
153 { -1, -1, 0 }
154 
155 };
156 
157 /* The exception table <exc_table> serves two functions:
158  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
159  * 2. it is a temporary memory buffer for the exception handlers.
160  */
161 
162 DEFINE_PER_CPU(struct exc_table, exc_table);
163 DEFINE_PER_CPU(struct debug_table, debug_table);
164 
165 void die(const char*, struct pt_regs*, long);
166 
167 static inline void
168 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
169 {
170 	if (!user_mode(regs))
171 		die(str, regs, err);
172 }
173 
174 /*
175  * Unhandled Exceptions. Kill user task or panic if in kernel space.
176  */
177 
178 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
179 {
180 	__die_if_kernel("Caught unhandled exception - should not happen",
181 			regs, SIGKILL);
182 
183 	/* If in user mode, send SIGILL signal to current process */
184 	pr_info_ratelimited("Caught unhandled exception in '%s' "
185 			    "(pid = %d, pc = %#010lx) - should not happen\n"
186 			    "\tEXCCAUSE is %ld\n",
187 			    current->comm, task_pid_nr(current), regs->pc,
188 			    exccause);
189 	force_sig(SIGILL, current);
190 }
191 
192 /*
193  * Multi-hit exception. This if fatal!
194  */
195 
196 void do_multihit(struct pt_regs *regs, unsigned long exccause)
197 {
198 	die("Caught multihit exception", regs, SIGKILL);
199 }
200 
201 /*
202  * IRQ handler.
203  */
204 
205 extern void do_IRQ(int, struct pt_regs *);
206 
207 #if XTENSA_FAKE_NMI
208 
209 #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
210 
211 #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
212       IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
213 #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
214 #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
215 
216 static inline void check_valid_nmi(void)
217 {
218 	unsigned intread = get_sr(interrupt);
219 	unsigned intenable = get_sr(intenable);
220 
221 	BUG_ON(intread & intenable &
222 	       ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
223 		 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
224 		 BIT(XCHAL_PROFILING_INTERRUPT)));
225 }
226 
227 #else
228 
229 static inline void check_valid_nmi(void)
230 {
231 }
232 
233 #endif
234 
235 irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
236 
237 DEFINE_PER_CPU(unsigned long, nmi_count);
238 
239 void do_nmi(struct pt_regs *regs)
240 {
241 	struct pt_regs *old_regs;
242 
243 	if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
244 		trace_hardirqs_off();
245 
246 	old_regs = set_irq_regs(regs);
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;
269 
270 	trace_hardirqs_off();
271 
272 	old_regs = set_irq_regs(regs);
273 	irq_enter();
274 
275 	for (;;) {
276 		unsigned intread = get_sr(interrupt);
277 		unsigned intenable = get_sr(intenable);
278 		unsigned int_at_level = intread & intenable;
279 		unsigned level;
280 
281 		for (level = LOCKLEVEL; level > 0; --level) {
282 			if (int_at_level & int_level_mask[level]) {
283 				int_at_level &= int_level_mask[level];
284 				break;
285 			}
286 		}
287 
288 		if (level == 0)
289 			break;
290 
291 		do_IRQ(__ffs(int_at_level), regs);
292 	}
293 
294 	irq_exit();
295 	set_irq_regs(old_regs);
296 }
297 
298 /*
299  * Illegal instruction. Fatal if in kernel space.
300  */
301 
302 void
303 do_illegal_instruction(struct pt_regs *regs)
304 {
305 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
306 
307 	/* If in user mode, send SIGILL signal to current process. */
308 
309 	pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
310 			    current->comm, task_pid_nr(current), regs->pc);
311 	force_sig(SIGILL, current);
312 }
313 
314 
315 /*
316  * Handle unaligned memory accesses from user space. Kill task.
317  *
318  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
319  * accesses causes from user space.
320  */
321 
322 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
323 void
324 do_unaligned_user (struct pt_regs *regs)
325 {
326 	__die_if_kernel("Unhandled unaligned exception in kernel",
327 			regs, SIGKILL);
328 
329 	current->thread.bad_vaddr = regs->excvaddr;
330 	current->thread.error_code = -3;
331 	pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
332 			    "(pid = %d, pc = %#010lx)\n",
333 			    regs->excvaddr, current->comm,
334 			    task_pid_nr(current), regs->pc);
335 	force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr, current);
336 }
337 #endif
338 
339 /* Handle debug events.
340  * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
341  * preemption disabled to avoid rescheduling and keep mapping of hardware
342  * breakpoint structures to debug registers intact, so that
343  * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
344  */
345 void
346 do_debug(struct pt_regs *regs)
347 {
348 #ifdef CONFIG_HAVE_HW_BREAKPOINT
349 	int ret = check_hw_breakpoint(regs);
350 
351 	preempt_enable();
352 	if (ret == 0)
353 		return;
354 #endif
355 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
356 
357 	/* If in user mode, send SIGTRAP signal to current process */
358 
359 	force_sig(SIGTRAP, current);
360 }
361 
362 
363 #define set_handler(type, cause, handler)				\
364 	do {								\
365 		unsigned int cpu;					\
366 									\
367 		for_each_possible_cpu(cpu)				\
368 			per_cpu(exc_table, cpu).type[cause] = (handler);\
369 	} while (0)
370 
371 /* Set exception C handler - for temporary use when probing exceptions */
372 
373 void * __init trap_set_handler(int cause, void *handler)
374 {
375 	void *previous = per_cpu(exc_table, 0).default_handler[cause];
376 
377 	set_handler(default_handler, cause, handler);
378 	return previous;
379 }
380 
381 
382 static void trap_init_excsave(void)
383 {
384 	unsigned long excsave1 = (unsigned long)this_cpu_ptr(&exc_table);
385 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (excsave1));
386 }
387 
388 static void trap_init_debug(void)
389 {
390 	unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
391 
392 	this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
393 	__asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
394 			     :: "a"(debugsave));
395 }
396 
397 /*
398  * Initialize dispatch tables.
399  *
400  * The exception vectors are stored compressed the __init section in the
401  * dispatch_init_table. This function initializes the following three tables
402  * from that compressed table:
403  * - fast user		first dispatch table for user exceptions
404  * - fast kernel	first dispatch table for kernel exceptions
405  * - default C-handler	C-handler called by the default fast handler.
406  *
407  * See vectors.S for more details.
408  */
409 
410 void __init trap_init(void)
411 {
412 	int i;
413 
414 	/* Setup default vectors. */
415 
416 	for (i = 0; i < EXCCAUSE_N; i++) {
417 		set_handler(fast_user_handler, i, user_exception);
418 		set_handler(fast_kernel_handler, i, kernel_exception);
419 		set_handler(default_handler, i, do_unhandled);
420 	}
421 
422 	/* Setup specific handlers. */
423 
424 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
425 
426 		int fast = dispatch_init_table[i].fast;
427 		int cause = dispatch_init_table[i].cause;
428 		void *handler = dispatch_init_table[i].handler;
429 
430 		if (fast == 0)
431 			set_handler(default_handler, cause, handler);
432 		if (fast && fast & USER)
433 			set_handler(fast_user_handler, cause, handler);
434 		if (fast && fast & KRNL)
435 			set_handler(fast_kernel_handler, cause, handler);
436 	}
437 
438 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
439 	trap_init_excsave();
440 	trap_init_debug();
441 }
442 
443 #ifdef CONFIG_SMP
444 void secondary_trap_init(void)
445 {
446 	trap_init_excsave();
447 	trap_init_debug();
448 }
449 #endif
450 
451 /*
452  * This function dumps the current valid window frame and other base registers.
453  */
454 
455 void show_regs(struct pt_regs * regs)
456 {
457 	int i, wmask;
458 
459 	show_regs_print_info(KERN_DEFAULT);
460 
461 	wmask = regs->wmask & ~1;
462 
463 	for (i = 0; i < 16; i++) {
464 		if ((i % 8) == 0)
465 			pr_info("a%02d:", i);
466 		pr_cont(" %08lx", regs->areg[i]);
467 	}
468 	pr_cont("\n");
469 	pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
470 		regs->pc, regs->ps, regs->depc, regs->excvaddr);
471 	pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
472 		regs->lbeg, regs->lend, regs->lcount, regs->sar);
473 	if (user_mode(regs))
474 		pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
475 			regs->windowbase, regs->windowstart, regs->wmask,
476 			regs->syscall);
477 }
478 
479 static int show_trace_cb(struct stackframe *frame, void *data)
480 {
481 	if (kernel_text_address(frame->pc))
482 		pr_cont(" [<%08lx>] %pB\n", frame->pc, (void *)frame->pc);
483 	return 0;
484 }
485 
486 void show_trace(struct task_struct *task, unsigned long *sp)
487 {
488 	if (!sp)
489 		sp = stack_pointer(task);
490 
491 	pr_info("Call Trace:\n");
492 	walk_stackframe(sp, show_trace_cb, NULL);
493 #ifndef CONFIG_KALLSYMS
494 	pr_cont("\n");
495 #endif
496 }
497 
498 static int kstack_depth_to_print = 24;
499 
500 void show_stack(struct task_struct *task, unsigned long *sp)
501 {
502 	int i = 0;
503 	unsigned long *stack;
504 
505 	if (!sp)
506 		sp = stack_pointer(task);
507 	stack = sp;
508 
509 	pr_info("Stack:\n");
510 
511 	for (i = 0; i < kstack_depth_to_print; i++) {
512 		if (kstack_end(sp))
513 			break;
514 		pr_cont(" %08lx", *sp++);
515 		if (i % 8 == 7)
516 			pr_cont("\n");
517 	}
518 	show_trace(task, stack);
519 }
520 
521 DEFINE_SPINLOCK(die_lock);
522 
523 void die(const char * str, struct pt_regs * regs, long err)
524 {
525 	static int die_counter;
526 
527 	console_verbose();
528 	spin_lock_irq(&die_lock);
529 
530 	pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter,
531 		IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "");
532 	show_regs(regs);
533 	if (!user_mode(regs))
534 		show_stack(NULL, (unsigned long*)regs->areg[1]);
535 
536 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
537 	spin_unlock_irq(&die_lock);
538 
539 	if (in_interrupt())
540 		panic("Fatal exception in interrupt");
541 
542 	if (panic_on_oops)
543 		panic("Fatal exception");
544 
545 	do_exit(err);
546 }
547