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>
33 #include <linux/hardirq.h>
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 #include <asm/traps.h>
44 extern int return_from_debug_flag
;
48 * Machine specific interrupt handlers
51 extern void kernel_exception(void);
52 extern void user_exception(void);
54 extern void fast_syscall_kernel(void);
55 extern void fast_syscall_user(void);
56 extern void fast_alloca(void);
57 extern void fast_unaligned(void);
58 extern void fast_second_level_miss(void);
59 extern void fast_store_prohibited(void);
60 extern void fast_coprocessor(void);
62 extern void do_illegal_instruction (struct pt_regs
*);
63 extern void do_interrupt (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
*);
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 (!))
79 #define COPROCESSOR(x) \
80 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
86 } dispatch_init_table_t
;
88 static dispatch_init_table_t __initdata dispatch_init_table
[] = {
90 { EXCCAUSE_ILLEGAL_INSTRUCTION
, 0, do_illegal_instruction
},
91 { EXCCAUSE_SYSTEM_CALL
, KRNL
, fast_syscall_kernel
},
92 { EXCCAUSE_SYSTEM_CALL
, USER
, fast_syscall_user
},
93 { EXCCAUSE_SYSTEM_CALL
, 0, system_call
},
94 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
95 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
96 { EXCCAUSE_LEVEL1_INTERRUPT
, 0, do_interrupt
},
97 { EXCCAUSE_ALLOCA
, USER
|KRNL
, fast_alloca
},
98 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
99 /* EXCCAUSE_PRIVILEGED unhandled */
100 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
101 #ifdef CONFIG_XTENSA_UNALIGNED_USER
102 { EXCCAUSE_UNALIGNED
, USER
, fast_unaligned
},
104 { EXCCAUSE_UNALIGNED
, 0, do_unaligned_user
},
106 { EXCCAUSE_UNALIGNED
, KRNL
, fast_unaligned
},
109 { EXCCAUSE_ITLB_MISS
, 0, do_page_fault
},
110 { EXCCAUSE_ITLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
111 { EXCCAUSE_ITLB_MULTIHIT
, 0, do_multihit
},
112 { EXCCAUSE_ITLB_PRIVILEGE
, 0, do_page_fault
},
113 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
114 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE
, 0, do_page_fault
},
115 { EXCCAUSE_DTLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
116 { EXCCAUSE_DTLB_MISS
, 0, do_page_fault
},
117 { EXCCAUSE_DTLB_MULTIHIT
, 0, do_multihit
},
118 { EXCCAUSE_DTLB_PRIVILEGE
, 0, do_page_fault
},
119 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
120 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, USER
|KRNL
, fast_store_prohibited
},
121 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, 0, do_page_fault
},
122 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE
, 0, do_page_fault
},
123 #endif /* CONFIG_MMU */
124 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
125 #if XTENSA_HAVE_COPROCESSOR(0)
128 #if XTENSA_HAVE_COPROCESSOR(1)
131 #if XTENSA_HAVE_COPROCESSOR(2)
134 #if XTENSA_HAVE_COPROCESSOR(3)
137 #if XTENSA_HAVE_COPROCESSOR(4)
140 #if XTENSA_HAVE_COPROCESSOR(5)
143 #if XTENSA_HAVE_COPROCESSOR(6)
146 #if XTENSA_HAVE_COPROCESSOR(7)
149 { EXCCAUSE_MAPPED_DEBUG
, 0, do_debug
},
154 /* The exception table <exc_table> serves two functions:
155 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
156 * 2. it is a temporary memory buffer for the exception handlers.
159 unsigned long exc_table
[EXC_TABLE_SIZE
/4];
161 void die(const char*, struct pt_regs
*, long);
164 __die_if_kernel(const char *str
, struct pt_regs
*regs
, long err
)
166 if (!user_mode(regs
))
171 * Unhandled Exceptions. Kill user task or panic if in kernel space.
174 void do_unhandled(struct pt_regs
*regs
, unsigned long exccause
)
176 __die_if_kernel("Caught unhandled exception - should not happen",
179 /* If in user mode, send SIGILL signal to current process */
180 printk("Caught unhandled exception in '%s' "
181 "(pid = %d, pc = %#010lx) - should not happen\n"
182 "\tEXCCAUSE is %ld\n",
183 current
->comm
, task_pid_nr(current
), regs
->pc
, exccause
);
184 force_sig(SIGILL
, current
);
188 * Multi-hit exception. This if fatal!
191 void do_multihit(struct pt_regs
*regs
, unsigned long exccause
)
193 die("Caught multihit exception", regs
, SIGKILL
);
198 * PS.INTLEVEL is the current IRQ priority level.
201 extern void do_IRQ(int, struct pt_regs
*);
203 void do_interrupt(struct pt_regs
*regs
)
205 static const unsigned int_level_mask
[] = {
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
,
215 unsigned level
= get_sr(ps
) & PS_INTLEVEL_MASK
;
217 if (WARN_ON_ONCE(level
>= ARRAY_SIZE(int_level_mask
)))
221 unsigned intread
= get_sr(interrupt
);
222 unsigned intenable
= get_sr(intenable
);
223 unsigned int_at_level
= intread
& intenable
&
224 int_level_mask
[level
];
230 * Clear the interrupt before processing, in case it's
231 * edge-triggered or software-generated
233 while (int_at_level
) {
234 unsigned i
= __ffs(int_at_level
);
235 unsigned mask
= 1 << i
;
237 int_at_level
^= mask
;
238 set_sr(mask
, intclear
);
245 * Illegal instruction. Fatal if in kernel space.
249 do_illegal_instruction(struct pt_regs
*regs
)
251 __die_if_kernel("Illegal instruction in kernel", regs
, SIGKILL
);
253 /* If in user mode, send SIGILL signal to current process. */
255 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
256 current
->comm
, task_pid_nr(current
), regs
->pc
);
257 force_sig(SIGILL
, current
);
262 * Handle unaligned memory accesses from user space. Kill task.
264 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
265 * accesses causes from user space.
268 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
269 #ifndef CONFIG_XTENSA_UNALIGNED_USER
271 do_unaligned_user (struct pt_regs
*regs
)
275 __die_if_kernel("Unhandled unaligned exception in kernel",
278 current
->thread
.bad_vaddr
= regs
->excvaddr
;
279 current
->thread
.error_code
= -3;
280 printk("Unaligned memory access to %08lx in '%s' "
281 "(pid = %d, pc = %#010lx)\n",
282 regs
->excvaddr
, current
->comm
, task_pid_nr(current
), regs
->pc
);
283 info
.si_signo
= SIGBUS
;
285 info
.si_code
= BUS_ADRALN
;
286 info
.si_addr
= (void *) regs
->excvaddr
;
287 force_sig_info(SIGSEGV
, &info
, current
);
294 do_debug(struct pt_regs
*regs
)
297 /* If remote debugging is configured AND enabled, we give control to
298 * kgdb. Otherwise, we fall through, perhaps giving control to the
303 extern void gdb_handle_exception(struct pt_regs
*);
304 gdb_handle_exception(regs
);
305 return_from_debug_flag
= 1;
310 __die_if_kernel("Breakpoint in kernel", regs
, SIGKILL
);
312 /* If in user mode, send SIGTRAP signal to current process */
314 force_sig(SIGTRAP
, current
);
318 /* Set exception C handler - for temporary use when probing exceptions */
320 void * __init
trap_set_handler(int cause
, void *handler
)
322 unsigned long *entry
= &exc_table
[EXC_TABLE_DEFAULT
/ 4 + cause
];
323 void *previous
= (void *)*entry
;
324 *entry
= (unsigned long)handler
;
330 * Initialize dispatch tables.
332 * The exception vectors are stored compressed the __init section in the
333 * dispatch_init_table. This function initializes the following three tables
334 * from that compressed table:
335 * - fast user first dispatch table for user exceptions
336 * - fast kernel first dispatch table for kernel exceptions
337 * - default C-handler C-handler called by the default fast handler.
339 * See vectors.S for more details.
342 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
344 void __init
trap_init(void)
348 /* Setup default vectors. */
350 for(i
= 0; i
< 64; i
++) {
351 set_handler(EXC_TABLE_FAST_USER
/4 + i
, user_exception
);
352 set_handler(EXC_TABLE_FAST_KERNEL
/4 + i
, kernel_exception
);
353 set_handler(EXC_TABLE_DEFAULT
/4 + i
, do_unhandled
);
356 /* Setup specific handlers. */
358 for(i
= 0; dispatch_init_table
[i
].cause
>= 0; i
++) {
360 int fast
= dispatch_init_table
[i
].fast
;
361 int cause
= dispatch_init_table
[i
].cause
;
362 void *handler
= dispatch_init_table
[i
].handler
;
365 set_handler (EXC_TABLE_DEFAULT
/4 + cause
, handler
);
366 if (fast
&& fast
& USER
)
367 set_handler (EXC_TABLE_FAST_USER
/4 + cause
, handler
);
368 if (fast
&& fast
& KRNL
)
369 set_handler (EXC_TABLE_FAST_KERNEL
/4 + cause
, handler
);
372 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
374 i
= (unsigned long)exc_table
;
375 __asm__
__volatile__("wsr %0, excsave1\n" : : "a" (i
));
379 * This function dumps the current valid window frame and other base registers.
382 void show_regs(struct pt_regs
* regs
)
386 show_regs_print_info(KERN_DEFAULT
);
388 wmask
= regs
->wmask
& ~1;
390 for (i
= 0; i
< 16; i
++) {
392 printk(KERN_INFO
"a%02d:", i
);
393 printk(KERN_CONT
" %08lx", regs
->areg
[i
]);
395 printk(KERN_CONT
"\n");
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 static __always_inline
unsigned long *stack_pointer(struct task_struct
*task
)
411 if (!task
|| task
== current
)
412 __asm__
__volatile__ ("mov %0, a1\n" : "=a"(sp
));
414 sp
= (unsigned long *)task
->thread
.sp
;
419 void show_trace(struct task_struct
*task
, unsigned long *sp
)
421 unsigned long a0
, a1
, pc
;
422 unsigned long sp_start
, sp_end
;
425 a1
= (unsigned long)sp
;
427 a1
= (unsigned long)stack_pointer(task
);
429 sp_start
= a1
& ~(THREAD_SIZE
-1);
430 sp_end
= sp_start
+ THREAD_SIZE
;
432 printk("Call Trace:");
433 #ifdef CONFIG_KALLSYMS
438 while (a1
> sp_start
&& a1
< sp_end
) {
439 sp
= (unsigned long*)a1
;
444 if (a1
<= (unsigned long) sp
)
447 pc
= MAKE_PC_FROM_RA(a0
, a1
);
449 if (kernel_text_address(pc
)) {
450 printk(" [<%08lx>] ", pc
);
451 print_symbol("%s\n", pc
);
458 * This routine abuses get_user()/put_user() to reference pointers
459 * with at least a bit of error checking ...
462 static int kstack_depth_to_print
= 24;
464 void show_stack(struct task_struct
*task
, unsigned long *sp
)
467 unsigned long *stack
;
470 sp
= stack_pointer(task
);
475 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
478 if (i
&& ((i
% 8) == 0))
480 printk("%08lx ", *sp
++);
483 show_trace(task
, stack
);
486 void show_code(unsigned int *pc
)
492 for(i
= -3 ; i
< 6 ; i
++) {
494 if (__get_user(insn
, pc
+ i
)) {
495 printk(" (Bad address in pc)\n");
498 printk("%c%08lx%c",(i
?' ':'<'),insn
,(i
?' ':'>'));
502 DEFINE_SPINLOCK(die_lock
);
504 void die(const char * str
, struct pt_regs
* regs
, long err
)
506 static int die_counter
;
510 spin_lock_irq(&die_lock
);
512 printk("%s: sig: %ld [#%d]\n", str
, err
, ++die_counter
);
513 #ifdef CONFIG_PREEMPT
520 if (!user_mode(regs
))
521 show_stack(NULL
, (unsigned long*)regs
->areg
[1]);
523 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
524 spin_unlock_irq(&die_lock
);
527 panic("Fatal exception in interrupt");
530 panic("Fatal exception");