xref: /openbmc/linux/arch/xtensa/kernel/traps.c (revision 5a0015d6)
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 - 2005 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 
33 #include <asm/ptrace.h>
34 #include <asm/timex.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/processor.h>
38 
39 #ifdef CONFIG_KGDB
40 extern int gdb_enter;
41 extern int return_from_debug_flag;
42 #endif
43 
44 /*
45  * Machine specific interrupt handlers
46  */
47 
48 extern void kernel_exception(void);
49 extern void user_exception(void);
50 
51 extern void fast_syscall_kernel(void);
52 extern void fast_syscall_user(void);
53 extern void fast_alloca(void);
54 extern void fast_unaligned(void);
55 extern void fast_second_level_miss(void);
56 extern void fast_store_prohibited(void);
57 extern void fast_coprocessor(void);
58 
59 extern void do_illegal_instruction (struct pt_regs*);
60 extern void do_interrupt (struct pt_regs*);
61 extern void do_unaligned_user (struct pt_regs*);
62 extern void do_multihit (struct pt_regs*, unsigned long);
63 extern void do_page_fault (struct pt_regs*, unsigned long);
64 extern void do_debug (struct pt_regs*);
65 extern void system_call (struct pt_regs*);
66 
67 /*
68  * The vector table must be preceded by a save area (which
69  * implies it must be in RAM, unless one places RAM immediately
70  * before a ROM and puts the vector at the start of the ROM (!))
71  */
72 
73 #define KRNL		0x01
74 #define USER		0x02
75 
76 #define COPROCESSOR(x)							\
77 { XCHAL_EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
78 
79 typedef struct {
80 	int cause;
81 	int fast;
82 	void* handler;
83 } dispatch_init_table_t;
84 
85 dispatch_init_table_t __init dispatch_init_table[] = {
86 
87 { XCHAL_EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
88 { XCHAL_EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
89 { XCHAL_EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
90 { XCHAL_EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
91 /* XCHAL_EXCCAUSE_INSTRUCTION_FETCH unhandled */
92 /* XCHAL_EXCCAUSE_LOAD_STORE_ERROR unhandled*/
93 { XCHAL_EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
94 { XCHAL_EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
95 /* XCHAL_EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
96 /* XCHAL_EXCCAUSE_PRIVILEGED unhandled */
97 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
98 #ifdef CONFIG_UNALIGNED_USER
99 { XCHAL_EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
100 #else
101 { XCHAL_EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
102 #endif
103 { XCHAL_EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
104 #endif
105 { XCHAL_EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
106 { XCHAL_EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
107 { XCHAL_EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
108 { XCHAL_EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
109 /* XCHAL_EXCCAUSE_SIZE_RESTRICTION unhandled */
110 { XCHAL_EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
111 { XCHAL_EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
112 { XCHAL_EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
113 { XCHAL_EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
114 { XCHAL_EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
115 /* XCHAL_EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
116 { XCHAL_EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
117 { XCHAL_EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
118 { XCHAL_EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
119 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
120 #if (XCHAL_CP_MASK & 1)
121 COPROCESSOR(0),
122 #endif
123 #if (XCHAL_CP_MASK & 2)
124 COPROCESSOR(1),
125 #endif
126 #if (XCHAL_CP_MASK & 4)
127 COPROCESSOR(2),
128 #endif
129 #if (XCHAL_CP_MASK & 8)
130 COPROCESSOR(3),
131 #endif
132 #if (XCHAL_CP_MASK & 16)
133 COPROCESSOR(4),
134 #endif
135 #if (XCHAL_CP_MASK & 32)
136 COPROCESSOR(5),
137 #endif
138 #if (XCHAL_CP_MASK & 64)
139 COPROCESSOR(6),
140 #endif
141 #if (XCHAL_CP_MASK & 128)
142 COPROCESSOR(7),
143 #endif
144 { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
145 { -1, -1, 0 }
146 
147 };
148 
149 /* The exception table <exc_table> serves two functions:
150  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
151  * 2. it is a temporary memory buffer for the exception handlers.
152  */
153 
154 unsigned long exc_table[EXC_TABLE_SIZE/4];
155 
156 void die(const char*, struct pt_regs*, long);
157 
158 static inline void
159 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
160 {
161 	if (!user_mode(regs))
162 		die(str, regs, err);
163 }
164 
165 /*
166  * Unhandled Exceptions. Kill user task or panic if in kernel space.
167  */
168 
169 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
170 {
171 	__die_if_kernel("Caught unhandled exception - should not happen",
172 	    		regs, SIGKILL);
173 
174 	/* If in user mode, send SIGILL signal to current process */
175 	printk("Caught unhandled exception in '%s' "
176 	       "(pid = %d, pc = %#010lx) - should not happen\n"
177 	       "\tEXCCAUSE is %ld\n",
178 	       current->comm, current->pid, regs->pc, exccause);
179 	force_sig(SIGILL, current);
180 }
181 
182 /*
183  * Multi-hit exception. This if fatal!
184  */
185 
186 void do_multihit(struct pt_regs *regs, unsigned long exccause)
187 {
188 	die("Caught multihit exception", regs, SIGKILL);
189 }
190 
191 /*
192  * Level-1 interrupt.
193  * We currently have no priority encoding.
194  */
195 
196 unsigned long ignored_level1_interrupts;
197 extern void do_IRQ(int, struct pt_regs *);
198 
199 void do_interrupt (struct pt_regs *regs)
200 {
201 	unsigned long intread = get_sr (INTREAD);
202 	unsigned long intenable = get_sr (INTENABLE);
203 	int i, mask;
204 
205 	/* Handle all interrupts (no priorities).
206 	 * (Clear the interrupt before processing, in case it's
207 	 *  edge-triggered or software-generated)
208 	 */
209 
210 	for (i=0, mask = 1; i < XCHAL_NUM_INTERRUPTS; i++, mask <<= 1) {
211 		if (mask & (intread & intenable)) {
212 			set_sr (mask, INTCLEAR);
213 			do_IRQ (i,regs);
214 		}
215 	}
216 }
217 
218 /*
219  * Illegal instruction. Fatal if in kernel space.
220  */
221 
222 void
223 do_illegal_instruction(struct pt_regs *regs)
224 {
225 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
226 
227 	/* If in user mode, send SIGILL signal to current process. */
228 
229 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
230 	    current->comm, current->pid, regs->pc);
231 	force_sig(SIGILL, current);
232 }
233 
234 
235 /*
236  * Handle unaligned memory accesses from user space. Kill task.
237  *
238  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
239  * accesses causes from user space.
240  */
241 
242 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
243 #ifndef CONFIG_UNALIGNED_USER
244 void
245 do_unaligned_user (struct pt_regs *regs)
246 {
247 	siginfo_t info;
248 
249 	__die_if_kernel("Unhandled unaligned exception in kernel",
250 	    		regs, SIGKILL);
251 
252 	current->thread.bad_vaddr = regs->excvaddr;
253 	current->thread.error_code = -3;
254 	printk("Unaligned memory access to %08lx in '%s' "
255 	       "(pid = %d, pc = %#010lx)\n",
256 	       regs->excvaddr, current->comm, current->pid, regs->pc);
257 	info.si_signo = SIGBUS;
258 	info.si_errno = 0;
259 	info.si_code = BUS_ADRALN;
260 	info.si_addr = (void *) regs->excvaddr;
261 	force_sig_info(SIGSEGV, &info, current);
262 
263 }
264 #endif
265 #endif
266 
267 void
268 do_debug(struct pt_regs *regs)
269 {
270 #ifdef CONFIG_KGDB
271 	/* If remote debugging is configured AND enabled, we give control to
272 	 * kgdb.  Otherwise, we fall through, perhaps giving control to the
273 	 * native debugger.
274 	 */
275 
276 	if (gdb_enter) {
277 		extern void gdb_handle_exception(struct pt_regs *);
278 		gdb_handle_exception(regs);
279 		return_from_debug_flag = 1;
280 		return;
281 	}
282 #endif
283 
284 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
285 
286 	/* If in user mode, send SIGTRAP signal to current process */
287 
288 	force_sig(SIGTRAP, current);
289 }
290 
291 
292 /*
293  * Initialize dispatch tables.
294  *
295  * The exception vectors are stored compressed the __init section in the
296  * dispatch_init_table. This function initializes the following three tables
297  * from that compressed table:
298  * - fast user		first dispatch table for user exceptions
299  * - fast kernel	first dispatch table for kernel exceptions
300  * - default C-handler	C-handler called by the default fast handler.
301  *
302  * See vectors.S for more details.
303  */
304 
305 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
306 
307 void trap_init(void)
308 {
309 	int i;
310 
311 	/* Setup default vectors. */
312 
313 	for(i = 0; i < 64; i++) {
314 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
315 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
316 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
317 	}
318 
319 	/* Setup specific handlers. */
320 
321 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
322 
323 		int fast = dispatch_init_table[i].fast;
324 		int cause = dispatch_init_table[i].cause;
325 		void *handler = dispatch_init_table[i].handler;
326 
327 		if (fast == 0)
328 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
329 		if (fast && fast & USER)
330 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
331 		if (fast && fast & KRNL)
332 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
333 	}
334 
335 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
336 
337 	i = (unsigned long)exc_table;
338 	__asm__ __volatile__("wsr  %0, "__stringify(EXCSAVE_1)"\n" : : "a" (i));
339 }
340 
341 /*
342  * This function dumps the current valid window frame and other base registers.
343  */
344 
345 void show_regs(struct pt_regs * regs)
346 {
347 	int i, wmask;
348 
349 	wmask = regs->wmask & ~1;
350 
351 	for (i = 0; i < 32; i++) {
352 		if (wmask & (1 << (i / 4)))
353 			break;
354 		if ((i % 8) == 0)
355 			printk ("\n" KERN_INFO "a%02d: ", i);
356 		printk("%08lx ", regs->areg[i]);
357 	}
358 	printk("\n");
359 
360 	printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
361 	       regs->pc, regs->ps, regs->depc, regs->excvaddr);
362 	printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
363 	       regs->lbeg, regs->lend, regs->lcount, regs->sar);
364 	if (user_mode(regs))
365 		printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
366 		       regs->windowbase, regs->windowstart, regs->wmask,
367 		       regs->syscall);
368 }
369 
370 void show_trace(struct task_struct *task, unsigned long *sp)
371 {
372 	unsigned long a0, a1, pc;
373 	unsigned long sp_start, sp_end;
374 
375 	a1 = (unsigned long)sp;
376 
377 	if (a1 == 0)
378 		__asm__ __volatile__ ("mov %0, a1\n" : "=a"(a1));
379 
380 
381 	sp_start = a1 & ~(THREAD_SIZE-1);
382 	sp_end = sp_start + THREAD_SIZE;
383 
384 	printk("Call Trace:");
385 #ifdef CONFIG_KALLSYMS
386 	printk("\n");
387 #endif
388 	spill_registers();
389 
390 	while (a1 > sp_start && a1 < sp_end) {
391 		sp = (unsigned long*)a1;
392 
393 		a0 = *(sp - 4);
394 		a1 = *(sp - 3);
395 
396 		if (a1 <= (unsigned long) sp)
397 			break;
398 
399 		pc = MAKE_PC_FROM_RA(a0, a1);
400 
401 		if (kernel_text_address(pc)) {
402 			printk(" [<%08lx>] ", pc);
403 			print_symbol("%s\n", pc);
404 		}
405 	}
406 	printk("\n");
407 }
408 
409 /*
410  * This routine abuses get_user()/put_user() to reference pointers
411  * with at least a bit of error checking ...
412  */
413 
414 static int kstack_depth_to_print = 24;
415 
416 void show_stack(struct task_struct *task, unsigned long *sp)
417 {
418 	int i = 0;
419 	unsigned long *stack;
420 
421 	if (sp == 0)
422 		__asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
423 
424  	stack = sp;
425 
426 	printk("\nStack: ");
427 
428 	for (i = 0; i < kstack_depth_to_print; i++) {
429 		if (kstack_end(sp))
430 			break;
431 		if (i && ((i % 8) == 0))
432 			printk("\n       ");
433 		printk("%08lx ", *sp++);
434 	}
435 	printk("\n");
436 	show_trace(task, stack);
437 }
438 
439 void dump_stack(void)
440 {
441 	show_stack(current, NULL);
442 }
443 
444 EXPORT_SYMBOL(dump_stack);
445 
446 
447 void show_code(unsigned int *pc)
448 {
449 	long i;
450 
451 	printk("\nCode:");
452 
453 	for(i = -3 ; i < 6 ; i++) {
454 		unsigned long insn;
455 		if (__get_user(insn, pc + i)) {
456 			printk(" (Bad address in pc)\n");
457 			break;
458 		}
459 		printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
460 	}
461 }
462 
463 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
464 
465 void die(const char * str, struct pt_regs * regs, long err)
466 {
467 	static int die_counter;
468 	int nl = 0;
469 
470 	console_verbose();
471 	spin_lock_irq(&die_lock);
472 
473 	printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
474 #ifdef CONFIG_PREEMPT
475 	printk("PREEMPT ");
476 	nl = 1;
477 #endif
478 	if (nl)
479 		printk("\n");
480 	show_regs(regs);
481 	if (!user_mode(regs))
482 		show_stack(NULL, (unsigned long*)regs->areg[1]);
483 
484 	spin_unlock_irq(&die_lock);
485 
486 	if (in_interrupt())
487 		panic("Fatal exception in interrupt");
488 
489 	if (panic_on_oops) {
490 		printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
491 		set_current_state(TASK_UNINTERRUPTIBLE);
492 		schedule_timeout(5 * HZ);
493 		panic("Fatal exception");
494 	}
495 	do_exit(err);
496 }
497 
498 
499