xref: /linux/arch/xtensa/kernel/traps.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
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.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/stringify.h>
31 #include <linux/kallsyms.h>
32 #include <linux/delay.h>
33 #include <linux/hardirq.h>
34 
35 #include <asm/stacktrace.h>
36 #include <asm/ptrace.h>
37 #include <asm/timex.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/processor.h>
41 #include <asm/traps.h>
42 
43 #ifdef CONFIG_KGDB
44 extern int gdb_enter;
45 extern int return_from_debug_flag;
46 #endif
47 
48 /*
49  * Machine specific interrupt handlers
50  */
51 
52 extern void kernel_exception(void);
53 extern void user_exception(void);
54 
55 extern void fast_syscall_kernel(void);
56 extern void fast_syscall_user(void);
57 extern void fast_alloca(void);
58 extern void fast_unaligned(void);
59 extern void fast_second_level_miss(void);
60 extern void fast_store_prohibited(void);
61 extern void fast_coprocessor(void);
62 
63 extern void do_illegal_instruction (struct pt_regs*);
64 extern void do_interrupt (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 #else
105 { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
106 #endif
107 { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
108 #endif
109 #ifdef CONFIG_MMU
110 { EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
111 { EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
112 { EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
113 { EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
114 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
115 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
116 { EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
117 { EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
118 { EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
119 { EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
120 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
121 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
122 { EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
123 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
124 #endif /* CONFIG_MMU */
125 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
126 #if XTENSA_HAVE_COPROCESSOR(0)
127 COPROCESSOR(0),
128 #endif
129 #if XTENSA_HAVE_COPROCESSOR(1)
130 COPROCESSOR(1),
131 #endif
132 #if XTENSA_HAVE_COPROCESSOR(2)
133 COPROCESSOR(2),
134 #endif
135 #if XTENSA_HAVE_COPROCESSOR(3)
136 COPROCESSOR(3),
137 #endif
138 #if XTENSA_HAVE_COPROCESSOR(4)
139 COPROCESSOR(4),
140 #endif
141 #if XTENSA_HAVE_COPROCESSOR(5)
142 COPROCESSOR(5),
143 #endif
144 #if XTENSA_HAVE_COPROCESSOR(6)
145 COPROCESSOR(6),
146 #endif
147 #if XTENSA_HAVE_COPROCESSOR(7)
148 COPROCESSOR(7),
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 unsigned long exc_table[EXC_TABLE_SIZE/4];
161 
162 void die(const char*, struct pt_regs*, long);
163 
164 static inline void
165 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
166 {
167 	if (!user_mode(regs))
168 		die(str, regs, err);
169 }
170 
171 /*
172  * Unhandled Exceptions. Kill user task or panic if in kernel space.
173  */
174 
175 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
176 {
177 	__die_if_kernel("Caught unhandled exception - should not happen",
178 	    		regs, SIGKILL);
179 
180 	/* If in user mode, send SIGILL signal to current process */
181 	printk("Caught unhandled exception in '%s' "
182 	       "(pid = %d, pc = %#010lx) - should not happen\n"
183 	       "\tEXCCAUSE is %ld\n",
184 	       current->comm, task_pid_nr(current), regs->pc, exccause);
185 	force_sig(SIGILL, current);
186 }
187 
188 /*
189  * Multi-hit exception. This if fatal!
190  */
191 
192 void do_multihit(struct pt_regs *regs, unsigned long exccause)
193 {
194 	die("Caught multihit exception", regs, SIGKILL);
195 }
196 
197 /*
198  * IRQ handler.
199  */
200 
201 extern void do_IRQ(int, struct pt_regs *);
202 
203 void do_interrupt(struct pt_regs *regs)
204 {
205 	static const unsigned int_level_mask[] = {
206 		0,
207 		XCHAL_INTLEVEL1_MASK,
208 		XCHAL_INTLEVEL2_MASK,
209 		XCHAL_INTLEVEL3_MASK,
210 		XCHAL_INTLEVEL4_MASK,
211 		XCHAL_INTLEVEL5_MASK,
212 		XCHAL_INTLEVEL6_MASK,
213 		XCHAL_INTLEVEL7_MASK,
214 	};
215 
216 	for (;;) {
217 		unsigned intread = get_sr(interrupt);
218 		unsigned intenable = get_sr(intenable);
219 		unsigned int_at_level = intread & intenable;
220 		unsigned level;
221 
222 		for (level = LOCKLEVEL; level > 0; --level) {
223 			if (int_at_level & int_level_mask[level]) {
224 				int_at_level &= int_level_mask[level];
225 				break;
226 			}
227 		}
228 
229 		if (level == 0)
230 			return;
231 
232 		/*
233 		 * Clear the interrupt before processing, in case it's
234 		 *  edge-triggered or software-generated
235 		 */
236 		while (int_at_level) {
237 			unsigned i = __ffs(int_at_level);
238 			unsigned mask = 1 << i;
239 
240 			int_at_level ^= mask;
241 			set_sr(mask, intclear);
242 			do_IRQ(i, regs);
243 		}
244 	}
245 }
246 
247 /*
248  * Illegal instruction. Fatal if in kernel space.
249  */
250 
251 void
252 do_illegal_instruction(struct pt_regs *regs)
253 {
254 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
255 
256 	/* If in user mode, send SIGILL signal to current process. */
257 
258 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
259 	    current->comm, task_pid_nr(current), regs->pc);
260 	force_sig(SIGILL, current);
261 }
262 
263 
264 /*
265  * Handle unaligned memory accesses from user space. Kill task.
266  *
267  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
268  * accesses causes from user space.
269  */
270 
271 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
272 #ifndef CONFIG_XTENSA_UNALIGNED_USER
273 void
274 do_unaligned_user (struct pt_regs *regs)
275 {
276 	siginfo_t info;
277 
278 	__die_if_kernel("Unhandled unaligned exception in kernel",
279 	    		regs, SIGKILL);
280 
281 	current->thread.bad_vaddr = regs->excvaddr;
282 	current->thread.error_code = -3;
283 	printk("Unaligned memory access to %08lx in '%s' "
284 	       "(pid = %d, pc = %#010lx)\n",
285 	       regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
286 	info.si_signo = SIGBUS;
287 	info.si_errno = 0;
288 	info.si_code = BUS_ADRALN;
289 	info.si_addr = (void *) regs->excvaddr;
290 	force_sig_info(SIGSEGV, &info, current);
291 
292 }
293 #endif
294 #endif
295 
296 void
297 do_debug(struct pt_regs *regs)
298 {
299 #ifdef CONFIG_KGDB
300 	/* If remote debugging is configured AND enabled, we give control to
301 	 * kgdb.  Otherwise, we fall through, perhaps giving control to the
302 	 * native debugger.
303 	 */
304 
305 	if (gdb_enter) {
306 		extern void gdb_handle_exception(struct pt_regs *);
307 		gdb_handle_exception(regs);
308 		return_from_debug_flag = 1;
309 		return;
310 	}
311 #endif
312 
313 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
314 
315 	/* If in user mode, send SIGTRAP signal to current process */
316 
317 	force_sig(SIGTRAP, current);
318 }
319 
320 
321 /* Set exception C handler - for temporary use when probing exceptions */
322 
323 void * __init trap_set_handler(int cause, void *handler)
324 {
325 	unsigned long *entry = &exc_table[EXC_TABLE_DEFAULT / 4 + cause];
326 	void *previous = (void *)*entry;
327 	*entry = (unsigned long)handler;
328 	return previous;
329 }
330 
331 
332 /*
333  * Initialize dispatch tables.
334  *
335  * The exception vectors are stored compressed the __init section in the
336  * dispatch_init_table. This function initializes the following three tables
337  * from that compressed table:
338  * - fast user		first dispatch table for user exceptions
339  * - fast kernel	first dispatch table for kernel exceptions
340  * - default C-handler	C-handler called by the default fast handler.
341  *
342  * See vectors.S for more details.
343  */
344 
345 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
346 
347 void __init trap_init(void)
348 {
349 	int i;
350 
351 	/* Setup default vectors. */
352 
353 	for(i = 0; i < 64; i++) {
354 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
355 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
356 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
357 	}
358 
359 	/* Setup specific handlers. */
360 
361 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
362 
363 		int fast = dispatch_init_table[i].fast;
364 		int cause = dispatch_init_table[i].cause;
365 		void *handler = dispatch_init_table[i].handler;
366 
367 		if (fast == 0)
368 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
369 		if (fast && fast & USER)
370 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
371 		if (fast && fast & KRNL)
372 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
373 	}
374 
375 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
376 
377 	i = (unsigned long)exc_table;
378 	__asm__ __volatile__("wsr  %0, excsave1\n" : : "a" (i));
379 }
380 
381 /*
382  * This function dumps the current valid window frame and other base registers.
383  */
384 
385 void show_regs(struct pt_regs * regs)
386 {
387 	int i, wmask;
388 
389 	show_regs_print_info(KERN_DEFAULT);
390 
391 	wmask = regs->wmask & ~1;
392 
393 	for (i = 0; i < 16; i++) {
394 		if ((i % 8) == 0)
395 			printk(KERN_INFO "a%02d:", i);
396 		printk(KERN_CONT " %08lx", regs->areg[i]);
397 	}
398 	printk(KERN_CONT "\n");
399 
400 	printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
401 	       regs->pc, regs->ps, regs->depc, regs->excvaddr);
402 	printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
403 	       regs->lbeg, regs->lend, regs->lcount, regs->sar);
404 	if (user_mode(regs))
405 		printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
406 		       regs->windowbase, regs->windowstart, regs->wmask,
407 		       regs->syscall);
408 }
409 
410 static int show_trace_cb(struct stackframe *frame, void *data)
411 {
412 	if (kernel_text_address(frame->pc)) {
413 		printk(" [<%08lx>] ", frame->pc);
414 		print_symbol("%s\n", frame->pc);
415 	}
416 	return 0;
417 }
418 
419 void show_trace(struct task_struct *task, unsigned long *sp)
420 {
421 	if (!sp)
422 		sp = stack_pointer(task);
423 
424 	printk("Call Trace:");
425 #ifdef CONFIG_KALLSYMS
426 	printk("\n");
427 #endif
428 	walk_stackframe(sp, show_trace_cb, NULL);
429 	printk("\n");
430 }
431 
432 /*
433  * This routine abuses get_user()/put_user() to reference pointers
434  * with at least a bit of error checking ...
435  */
436 
437 static int kstack_depth_to_print = 24;
438 
439 void show_stack(struct task_struct *task, unsigned long *sp)
440 {
441 	int i = 0;
442 	unsigned long *stack;
443 
444 	if (!sp)
445 		sp = stack_pointer(task);
446 	stack = sp;
447 
448 	printk("\nStack: ");
449 
450 	for (i = 0; i < kstack_depth_to_print; i++) {
451 		if (kstack_end(sp))
452 			break;
453 		if (i && ((i % 8) == 0))
454 			printk("\n       ");
455 		printk("%08lx ", *sp++);
456 	}
457 	printk("\n");
458 	show_trace(task, stack);
459 }
460 
461 void show_code(unsigned int *pc)
462 {
463 	long i;
464 
465 	printk("\nCode:");
466 
467 	for(i = -3 ; i < 6 ; i++) {
468 		unsigned long insn;
469 		if (__get_user(insn, pc + i)) {
470 			printk(" (Bad address in pc)\n");
471 			break;
472 		}
473 		printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
474 	}
475 }
476 
477 DEFINE_SPINLOCK(die_lock);
478 
479 void die(const char * str, struct pt_regs * regs, long err)
480 {
481 	static int die_counter;
482 	int nl = 0;
483 
484 	console_verbose();
485 	spin_lock_irq(&die_lock);
486 
487 	printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
488 #ifdef CONFIG_PREEMPT
489 	printk("PREEMPT ");
490 	nl = 1;
491 #endif
492 	if (nl)
493 		printk("\n");
494 	show_regs(regs);
495 	if (!user_mode(regs))
496 		show_stack(NULL, (unsigned long*)regs->areg[1]);
497 
498 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
499 	spin_unlock_irq(&die_lock);
500 
501 	if (in_interrupt())
502 		panic("Fatal exception in interrupt");
503 
504 	if (panic_on_oops)
505 		panic("Fatal exception");
506 
507 	do_exit(err);
508 }
509