2 * arch/xtensa/kernel/traps.c
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.
12 * Essentially rewritten for the Xtensa architecture port.
14 * Copyright (C) 2001 - 2005 Tensilica Inc.
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>
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
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>
34 #include <asm/ptrace.h>
35 #include <asm/timex.h>
36 #include <asm/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
42 extern int return_from_debug_flag
;
46 * Machine specific interrupt handlers
49 extern void kernel_exception(void);
50 extern void user_exception(void);
52 extern void fast_syscall_kernel(void);
53 extern void fast_syscall_user(void);
54 extern void fast_alloca(void);
55 extern void fast_unaligned(void);
56 extern void fast_second_level_miss(void);
57 extern void fast_store_prohibited(void);
58 extern void fast_coprocessor(void);
60 extern void do_illegal_instruction (struct pt_regs
*);
61 extern void do_interrupt (struct pt_regs
*);
62 extern void do_unaligned_user (struct pt_regs
*);
63 extern void do_multihit (struct pt_regs
*, unsigned long);
64 extern void do_page_fault (struct pt_regs
*, unsigned long);
65 extern void do_debug (struct pt_regs
*);
66 extern void system_call (struct pt_regs
*);
69 * The vector table must be preceded by a save area (which
70 * implies it must be in RAM, unless one places RAM immediately
71 * before a ROM and puts the vector at the start of the ROM (!))
77 #define COPROCESSOR(x) \
78 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
84 } dispatch_init_table_t
;
86 static dispatch_init_table_t __initdata dispatch_init_table
[] = {
88 { EXCCAUSE_ILLEGAL_INSTRUCTION
, 0, do_illegal_instruction
},
89 { EXCCAUSE_SYSTEM_CALL
, KRNL
, fast_syscall_kernel
},
90 { EXCCAUSE_SYSTEM_CALL
, USER
, fast_syscall_user
},
91 { EXCCAUSE_SYSTEM_CALL
, 0, system_call
},
92 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
93 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
94 { EXCCAUSE_LEVEL1_INTERRUPT
, 0, do_interrupt
},
95 { EXCCAUSE_ALLOCA
, USER
|KRNL
, fast_alloca
},
96 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
97 /* EXCCAUSE_PRIVILEGED unhandled */
98 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
99 #ifdef CONFIG_UNALIGNED_USER
100 { EXCCAUSE_UNALIGNED
, USER
, fast_unaligned
},
102 { EXCCAUSE_UNALIGNED
, 0, do_unaligned_user
},
104 { EXCCAUSE_UNALIGNED
, KRNL
, fast_unaligned
},
106 { EXCCAUSE_ITLB_MISS
, 0, do_page_fault
},
107 { EXCCAUSE_ITLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
108 { EXCCAUSE_ITLB_MULTIHIT
, 0, do_multihit
},
109 { EXCCAUSE_ITLB_PRIVILEGE
, 0, do_page_fault
},
110 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
111 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE
, 0, do_page_fault
},
112 { EXCCAUSE_DTLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
113 { EXCCAUSE_DTLB_MISS
, 0, do_page_fault
},
114 { EXCCAUSE_DTLB_MULTIHIT
, 0, do_multihit
},
115 { EXCCAUSE_DTLB_PRIVILEGE
, 0, do_page_fault
},
116 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
117 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, USER
|KRNL
, fast_store_prohibited
},
118 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, 0, do_page_fault
},
119 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE
, 0, do_page_fault
},
120 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
121 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
122 #if (XCHAL_CP_MASK & 1)
124 #if XTENSA_HAVE_COPROCESSOR(0)
125 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
128 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
129 #if (XCHAL_CP_MASK & 2)
131 #if XTENSA_HAVE_COPROCESSOR(1)
132 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
135 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
136 #if (XCHAL_CP_MASK & 4)
138 #if XTENSA_HAVE_COPROCESSOR(2)
139 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
142 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
143 #if (XCHAL_CP_MASK & 8)
145 #if XTENSA_HAVE_COPROCESSOR(3)
146 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
149 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
150 #if (XCHAL_CP_MASK & 16)
152 #if XTENSA_HAVE_COPROCESSOR(4)
153 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
156 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
157 #if (XCHAL_CP_MASK & 32)
159 #if XTENSA_HAVE_COPROCESSOR(5)
160 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
163 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
164 #if (XCHAL_CP_MASK & 64)
166 #if XTENSA_HAVE_COPROCESSOR(6)
167 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
170 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
171 #if (XCHAL_CP_MASK & 128)
173 #if XTENSA_HAVE_COPROCESSOR(7)
174 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
177 { EXCCAUSE_MAPPED_DEBUG
, 0, do_debug
},
182 /* The exception table <exc_table> serves two functions:
183 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
184 * 2. it is a temporary memory buffer for the exception handlers.
187 unsigned long exc_table
[EXC_TABLE_SIZE
/4];
189 void die(const char*, struct pt_regs
*, long);
192 __die_if_kernel(const char *str
, struct pt_regs
*regs
, long err
)
194 if (!user_mode(regs
))
199 * Unhandled Exceptions. Kill user task or panic if in kernel space.
202 void do_unhandled(struct pt_regs
*regs
, unsigned long exccause
)
204 __die_if_kernel("Caught unhandled exception - should not happen",
207 /* If in user mode, send SIGILL signal to current process */
208 printk("Caught unhandled exception in '%s' "
209 "(pid = %d, pc = %#010lx) - should not happen\n"
210 "\tEXCCAUSE is %ld\n",
211 current
->comm
, task_pid_nr(current
), regs
->pc
, exccause
);
212 force_sig(SIGILL
, current
);
216 * Multi-hit exception. This if fatal!
219 void do_multihit(struct pt_regs
*regs
, unsigned long exccause
)
221 die("Caught multihit exception", regs
, SIGKILL
);
226 * We currently have no priority encoding.
229 unsigned long ignored_level1_interrupts
;
230 extern void do_IRQ(int, struct pt_regs
*);
232 void do_interrupt (struct pt_regs
*regs
)
234 unsigned long intread
= get_sr (INTREAD
);
235 unsigned long intenable
= get_sr (INTENABLE
);
238 /* Handle all interrupts (no priorities).
239 * (Clear the interrupt before processing, in case it's
240 * edge-triggered or software-generated)
243 for (i
=0, mask
= 1; i
< XCHAL_NUM_INTERRUPTS
; i
++, mask
<<= 1) {
244 if (mask
& (intread
& intenable
)) {
245 set_sr (mask
, INTCLEAR
);
252 * Illegal instruction. Fatal if in kernel space.
256 do_illegal_instruction(struct pt_regs
*regs
)
258 __die_if_kernel("Illegal instruction in kernel", regs
, SIGKILL
);
260 /* If in user mode, send SIGILL signal to current process. */
262 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
263 current
->comm
, task_pid_nr(current
), regs
->pc
);
264 force_sig(SIGILL
, current
);
269 * Handle unaligned memory accesses from user space. Kill task.
271 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
272 * accesses causes from user space.
275 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
276 #ifndef CONFIG_UNALIGNED_USER
278 do_unaligned_user (struct pt_regs
*regs
)
282 __die_if_kernel("Unhandled unaligned exception in kernel",
285 current
->thread
.bad_vaddr
= regs
->excvaddr
;
286 current
->thread
.error_code
= -3;
287 printk("Unaligned memory access to %08lx in '%s' "
288 "(pid = %d, pc = %#010lx)\n",
289 regs
->excvaddr
, current
->comm
, task_pid_nr(current
), regs
->pc
);
290 info
.si_signo
= SIGBUS
;
292 info
.si_code
= BUS_ADRALN
;
293 info
.si_addr
= (void *) regs
->excvaddr
;
294 force_sig_info(SIGSEGV
, &info
, current
);
301 do_debug(struct pt_regs
*regs
)
304 /* If remote debugging is configured AND enabled, we give control to
305 * kgdb. Otherwise, we fall through, perhaps giving control to the
310 extern void gdb_handle_exception(struct pt_regs
*);
311 gdb_handle_exception(regs
);
312 return_from_debug_flag
= 1;
317 __die_if_kernel("Breakpoint in kernel", regs
, SIGKILL
);
319 /* If in user mode, send SIGTRAP signal to current process */
321 force_sig(SIGTRAP
, current
);
326 * Initialize dispatch tables.
328 * The exception vectors are stored compressed the __init section in the
329 * dispatch_init_table. This function initializes the following three tables
330 * from that compressed table:
331 * - fast user first dispatch table for user exceptions
332 * - fast kernel first dispatch table for kernel exceptions
333 * - default C-handler C-handler called by the default fast handler.
335 * See vectors.S for more details.
338 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
340 void __init
trap_init(void)
344 /* Setup default vectors. */
346 for(i
= 0; i
< 64; i
++) {
347 set_handler(EXC_TABLE_FAST_USER
/4 + i
, user_exception
);
348 set_handler(EXC_TABLE_FAST_KERNEL
/4 + i
, kernel_exception
);
349 set_handler(EXC_TABLE_DEFAULT
/4 + i
, do_unhandled
);
352 /* Setup specific handlers. */
354 for(i
= 0; dispatch_init_table
[i
].cause
>= 0; i
++) {
356 int fast
= dispatch_init_table
[i
].fast
;
357 int cause
= dispatch_init_table
[i
].cause
;
358 void *handler
= dispatch_init_table
[i
].handler
;
361 set_handler (EXC_TABLE_DEFAULT
/4 + cause
, handler
);
362 if (fast
&& fast
& USER
)
363 set_handler (EXC_TABLE_FAST_USER
/4 + cause
, handler
);
364 if (fast
&& fast
& KRNL
)
365 set_handler (EXC_TABLE_FAST_KERNEL
/4 + cause
, handler
);
368 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
370 i
= (unsigned long)exc_table
;
371 __asm__
__volatile__("wsr %0, "__stringify(EXCSAVE_1
)"\n" : : "a" (i
));
375 * This function dumps the current valid window frame and other base registers.
378 void show_regs(struct pt_regs
* regs
)
382 wmask
= regs
->wmask
& ~1;
384 <<<<<<< HEAD
:arch
/xtensa
/kernel
/traps
.c
385 for (i
= 0; i
< 32; i
++) {
386 if (wmask
& (1 << (i
/ 4)))
389 for (i
= 0; i
< 16; i
++) {
390 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/xtensa
/kernel
/traps
.c
392 printk ("\n" KERN_INFO
"a%02d: ", i
);
393 printk("%08lx ", regs
->areg
[i
]);
397 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
398 regs
->pc
, regs
->ps
, regs
->depc
, regs
->excvaddr
);
399 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
400 regs
->lbeg
, regs
->lend
, regs
->lcount
, regs
->sar
);
402 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
403 regs
->windowbase
, regs
->windowstart
, regs
->wmask
,
407 void show_trace(struct task_struct
*task
, unsigned long *sp
)
409 unsigned long a0
, a1
, pc
;
410 unsigned long sp_start
, sp_end
;
412 a1
= (unsigned long)sp
;
415 __asm__
__volatile__ ("mov %0, a1\n" : "=a"(a1
));
418 sp_start
= a1
& ~(THREAD_SIZE
-1);
419 sp_end
= sp_start
+ THREAD_SIZE
;
421 printk("Call Trace:");
422 #ifdef CONFIG_KALLSYMS
427 while (a1
> sp_start
&& a1
< sp_end
) {
428 sp
= (unsigned long*)a1
;
433 if (a1
<= (unsigned long) sp
)
436 pc
= MAKE_PC_FROM_RA(a0
, a1
);
438 if (kernel_text_address(pc
)) {
439 printk(" [<%08lx>] ", pc
);
440 print_symbol("%s\n", pc
);
447 * This routine abuses get_user()/put_user() to reference pointers
448 * with at least a bit of error checking ...
451 static int kstack_depth_to_print
= 24;
453 void show_stack(struct task_struct
*task
, unsigned long *sp
)
456 unsigned long *stack
;
459 __asm__
__volatile__ ("mov %0, a1\n" : "=a"(sp
));
465 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
468 if (i
&& ((i
% 8) == 0))
470 printk("%08lx ", *sp
++);
473 show_trace(task
, stack
);
476 void dump_stack(void)
478 show_stack(current
, NULL
);
481 EXPORT_SYMBOL(dump_stack
);
484 void show_code(unsigned int *pc
)
490 for(i
= -3 ; i
< 6 ; i
++) {
492 if (__get_user(insn
, pc
+ i
)) {
493 printk(" (Bad address in pc)\n");
496 printk("%c%08lx%c",(i
?' ':'<'),insn
,(i
?' ':'>'));
500 DEFINE_SPINLOCK(die_lock
);
502 void die(const char * str
, struct pt_regs
* regs
, long err
)
504 static int die_counter
;
508 spin_lock_irq(&die_lock
);
510 printk("%s: sig: %ld [#%d]\n", str
, err
, ++die_counter
);
511 #ifdef CONFIG_PREEMPT
518 if (!user_mode(regs
))
519 show_stack(NULL
, (unsigned long*)regs
->areg
[1]);
521 add_taint(TAINT_DIE
);
522 spin_unlock_irq(&die_lock
);
525 panic("Fatal exception in interrupt");
528 panic("Fatal exception");