• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/delay.h>
33 #include <linux/hardirq.h>
34 
35 #include <asm/ptrace.h>
36 #include <asm/timex.h>
37 #include <asm/uaccess.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40 
41 #ifdef CONFIG_KGDB
42 extern int gdb_enter;
43 extern int return_from_debug_flag;
44 #endif
45 
46 /*
47  * Machine specific interrupt handlers
48  */
49 
50 extern void kernel_exception(void);
51 extern void user_exception(void);
52 
53 extern void fast_syscall_kernel(void);
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_unaligned_user (struct pt_regs*);
64 extern void do_multihit (struct pt_regs*, unsigned long);
65 extern void do_page_fault (struct pt_regs*, unsigned long);
66 extern void do_debug (struct pt_regs*);
67 extern void system_call (struct pt_regs*);
68 
69 /*
70  * The vector table must be preceded by a save area (which
71  * implies it must be in RAM, unless one places RAM immediately
72  * before a ROM and puts the vector at the start of the ROM (!))
73  */
74 
75 #define KRNL		0x01
76 #define USER		0x02
77 
78 #define COPROCESSOR(x)							\
79 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
80 
81 typedef struct {
82 	int cause;
83 	int fast;
84 	void* handler;
85 } dispatch_init_table_t;
86 
87 static dispatch_init_table_t __initdata dispatch_init_table[] = {
88 
89 { EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
90 { EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
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_UNALIGNED_USER
101 { EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
102 #else
103 { EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
104 #endif
105 { EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
106 #endif
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 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
122 #if XTENSA_HAVE_COPROCESSOR(0)
123 COPROCESSOR(0),
124 #endif
125 #if XTENSA_HAVE_COPROCESSOR(1)
126 COPROCESSOR(1),
127 #endif
128 #if XTENSA_HAVE_COPROCESSOR(2)
129 COPROCESSOR(2),
130 #endif
131 #if XTENSA_HAVE_COPROCESSOR(3)
132 COPROCESSOR(3),
133 #endif
134 #if XTENSA_HAVE_COPROCESSOR(4)
135 COPROCESSOR(4),
136 #endif
137 #if XTENSA_HAVE_COPROCESSOR(5)
138 COPROCESSOR(5),
139 #endif
140 #if XTENSA_HAVE_COPROCESSOR(6)
141 COPROCESSOR(6),
142 #endif
143 #if XTENSA_HAVE_COPROCESSOR(7)
144 COPROCESSOR(7),
145 #endif
146 { EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
147 { -1, -1, 0 }
148 
149 };
150 
151 /* The exception table <exc_table> serves two functions:
152  * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
153  * 2. it is a temporary memory buffer for the exception handlers.
154  */
155 
156 unsigned long exc_table[EXC_TABLE_SIZE/4];
157 
158 void die(const char*, struct pt_regs*, long);
159 
160 static inline void
__die_if_kernel(const char * str,struct pt_regs * regs,long err)161 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
162 {
163 	if (!user_mode(regs))
164 		die(str, regs, err);
165 }
166 
167 /*
168  * Unhandled Exceptions. Kill user task or panic if in kernel space.
169  */
170 
do_unhandled(struct pt_regs * regs,unsigned long exccause)171 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
172 {
173 	__die_if_kernel("Caught unhandled exception - should not happen",
174 	    		regs, SIGKILL);
175 
176 	/* If in user mode, send SIGILL signal to current process */
177 	printk("Caught unhandled exception in '%s' "
178 	       "(pid = %d, pc = %#010lx) - should not happen\n"
179 	       "\tEXCCAUSE is %ld\n",
180 	       current->comm, task_pid_nr(current), regs->pc, exccause);
181 	force_sig(SIGILL, current);
182 }
183 
184 /*
185  * Multi-hit exception. This if fatal!
186  */
187 
do_multihit(struct pt_regs * regs,unsigned long exccause)188 void do_multihit(struct pt_regs *regs, unsigned long exccause)
189 {
190 	die("Caught multihit exception", regs, SIGKILL);
191 }
192 
193 /*
194  * Level-1 interrupt.
195  * We currently have no priority encoding.
196  */
197 
198 unsigned long ignored_level1_interrupts;
199 extern void do_IRQ(int, struct pt_regs *);
200 
do_interrupt(struct pt_regs * regs)201 void do_interrupt (struct pt_regs *regs)
202 {
203 	unsigned long intread = get_sr (INTREAD);
204 	unsigned long intenable = get_sr (INTENABLE);
205 	int i, mask;
206 
207 	/* Handle all interrupts (no priorities).
208 	 * (Clear the interrupt before processing, in case it's
209 	 *  edge-triggered or software-generated)
210 	 */
211 
212 	for (i=0, mask = 1; i < XCHAL_NUM_INTERRUPTS; i++, mask <<= 1) {
213 		if (mask & (intread & intenable)) {
214 			set_sr (mask, INTCLEAR);
215 			do_IRQ (i,regs);
216 		}
217 	}
218 }
219 
220 /*
221  * Illegal instruction. Fatal if in kernel space.
222  */
223 
224 void
do_illegal_instruction(struct pt_regs * regs)225 do_illegal_instruction(struct pt_regs *regs)
226 {
227 	__die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
228 
229 	/* If in user mode, send SIGILL signal to current process. */
230 
231 	printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
232 	    current->comm, task_pid_nr(current), regs->pc);
233 	force_sig(SIGILL, current);
234 }
235 
236 
237 /*
238  * Handle unaligned memory accesses from user space. Kill task.
239  *
240  * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
241  * accesses causes from user space.
242  */
243 
244 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
245 #ifndef CONFIG_UNALIGNED_USER
246 void
do_unaligned_user(struct pt_regs * regs)247 do_unaligned_user (struct pt_regs *regs)
248 {
249 	siginfo_t info;
250 
251 	__die_if_kernel("Unhandled unaligned exception in kernel",
252 	    		regs, SIGKILL);
253 
254 	current->thread.bad_vaddr = regs->excvaddr;
255 	current->thread.error_code = -3;
256 	printk("Unaligned memory access to %08lx in '%s' "
257 	       "(pid = %d, pc = %#010lx)\n",
258 	       regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
259 	info.si_signo = SIGBUS;
260 	info.si_errno = 0;
261 	info.si_code = BUS_ADRALN;
262 	info.si_addr = (void *) regs->excvaddr;
263 	force_sig_info(SIGSEGV, &info, current);
264 
265 }
266 #endif
267 #endif
268 
269 void
do_debug(struct pt_regs * regs)270 do_debug(struct pt_regs *regs)
271 {
272 #ifdef CONFIG_KGDB
273 	/* If remote debugging is configured AND enabled, we give control to
274 	 * kgdb.  Otherwise, we fall through, perhaps giving control to the
275 	 * native debugger.
276 	 */
277 
278 	if (gdb_enter) {
279 		extern void gdb_handle_exception(struct pt_regs *);
280 		gdb_handle_exception(regs);
281 		return_from_debug_flag = 1;
282 		return;
283 	}
284 #endif
285 
286 	__die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
287 
288 	/* If in user mode, send SIGTRAP signal to current process */
289 
290 	force_sig(SIGTRAP, current);
291 }
292 
293 
294 /*
295  * Initialize dispatch tables.
296  *
297  * The exception vectors are stored compressed the __init section in the
298  * dispatch_init_table. This function initializes the following three tables
299  * from that compressed table:
300  * - fast user		first dispatch table for user exceptions
301  * - fast kernel	first dispatch table for kernel exceptions
302  * - default C-handler	C-handler called by the default fast handler.
303  *
304  * See vectors.S for more details.
305  */
306 
307 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
308 
trap_init(void)309 void __init trap_init(void)
310 {
311 	int i;
312 
313 	/* Setup default vectors. */
314 
315 	for(i = 0; i < 64; i++) {
316 		set_handler(EXC_TABLE_FAST_USER/4   + i, user_exception);
317 		set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
318 		set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
319 	}
320 
321 	/* Setup specific handlers. */
322 
323 	for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
324 
325 		int fast = dispatch_init_table[i].fast;
326 		int cause = dispatch_init_table[i].cause;
327 		void *handler = dispatch_init_table[i].handler;
328 
329 		if (fast == 0)
330 			set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
331 		if (fast && fast & USER)
332 			set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
333 		if (fast && fast & KRNL)
334 			set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
335 	}
336 
337 	/* Initialize EXCSAVE_1 to hold the address of the exception table. */
338 
339 	i = (unsigned long)exc_table;
340 	__asm__ __volatile__("wsr  %0, "__stringify(EXCSAVE_1)"\n" : : "a" (i));
341 }
342 
343 /*
344  * This function dumps the current valid window frame and other base registers.
345  */
346 
show_regs(struct pt_regs * regs)347 void show_regs(struct pt_regs * regs)
348 {
349 	int i, wmask;
350 
351 	wmask = regs->wmask & ~1;
352 
353 	for (i = 0; i < 16; i++) {
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 
show_trace(struct task_struct * task,unsigned long * sp)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 
show_stack(struct task_struct * task,unsigned long * sp)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 
dump_stack(void)439 void dump_stack(void)
440 {
441 	show_stack(current, NULL);
442 }
443 
444 EXPORT_SYMBOL(dump_stack);
445 
446 
show_code(unsigned int * pc)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 DEFINE_SPINLOCK(die_lock);
464 
die(const char * str,struct pt_regs * regs,long err)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 	add_taint(TAINT_DIE);
485 	spin_unlock_irq(&die_lock);
486 
487 	if (in_interrupt())
488 		panic("Fatal exception in interrupt");
489 
490 	if (panic_on_oops)
491 		panic("Fatal exception");
492 
493 	do_exit(err);
494 }
495 
496 
497