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