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>
43 extern int return_from_debug_flag
;
47 * Machine specific interrupt handlers
50 extern void kernel_exception(void);
51 extern void user_exception(void);
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);
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
*);
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 (!))
78 #define COPROCESSOR(x) \
79 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
85 } dispatch_init_table_t
;
87 static dispatch_init_table_t __initdata dispatch_init_table
[] = {
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_XTENSA_UNALIGNED_USER
101 { EXCCAUSE_UNALIGNED
, USER
, fast_unaligned
},
103 { EXCCAUSE_UNALIGNED
, 0, do_unaligned_user
},
105 { EXCCAUSE_UNALIGNED
, KRNL
, fast_unaligned
},
108 { EXCCAUSE_ITLB_MISS
, 0, do_page_fault
},
109 { EXCCAUSE_ITLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
110 { EXCCAUSE_ITLB_MULTIHIT
, 0, do_multihit
},
111 { EXCCAUSE_ITLB_PRIVILEGE
, 0, do_page_fault
},
112 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
113 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE
, 0, do_page_fault
},
114 { EXCCAUSE_DTLB_MISS
, USER
|KRNL
, fast_second_level_miss
},
115 { EXCCAUSE_DTLB_MISS
, 0, do_page_fault
},
116 { EXCCAUSE_DTLB_MULTIHIT
, 0, do_multihit
},
117 { EXCCAUSE_DTLB_PRIVILEGE
, 0, do_page_fault
},
118 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
119 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, USER
|KRNL
, fast_store_prohibited
},
120 { EXCCAUSE_STORE_CACHE_ATTRIBUTE
, 0, do_page_fault
},
121 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE
, 0, do_page_fault
},
122 #endif /* CONFIG_MMU */
123 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
124 #if XTENSA_HAVE_COPROCESSOR(0)
127 #if XTENSA_HAVE_COPROCESSOR(1)
130 #if XTENSA_HAVE_COPROCESSOR(2)
133 #if XTENSA_HAVE_COPROCESSOR(3)
136 #if XTENSA_HAVE_COPROCESSOR(4)
139 #if XTENSA_HAVE_COPROCESSOR(5)
142 #if XTENSA_HAVE_COPROCESSOR(6)
145 #if XTENSA_HAVE_COPROCESSOR(7)
148 { EXCCAUSE_MAPPED_DEBUG
, 0, do_debug
},
153 /* The exception table <exc_table> serves two functions:
154 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
155 * 2. it is a temporary memory buffer for the exception handlers.
158 unsigned long exc_table
[EXC_TABLE_SIZE
/4];
160 void die(const char*, struct pt_regs
*, long);
163 __die_if_kernel(const char *str
, struct pt_regs
*regs
, long err
)
165 if (!user_mode(regs
))
170 * Unhandled Exceptions. Kill user task or panic if in kernel space.
173 void do_unhandled(struct pt_regs
*regs
, unsigned long exccause
)
175 __die_if_kernel("Caught unhandled exception - should not happen",
178 /* If in user mode, send SIGILL signal to current process */
179 printk("Caught unhandled exception in '%s' "
180 "(pid = %d, pc = %#010lx) - should not happen\n"
181 "\tEXCCAUSE is %ld\n",
182 current
->comm
, task_pid_nr(current
), regs
->pc
, exccause
);
183 force_sig(SIGILL
, current
);
187 * Multi-hit exception. This if fatal!
190 void do_multihit(struct pt_regs
*regs
, unsigned long exccause
)
192 die("Caught multihit exception", regs
, SIGKILL
);
197 * We currently have no priority encoding.
200 unsigned long ignored_level1_interrupts
;
201 extern void do_IRQ(int, struct pt_regs
*);
203 void do_interrupt (struct pt_regs
*regs
)
205 unsigned long intread
= get_sr (interrupt
);
206 unsigned long intenable
= get_sr (intenable
);
209 /* Handle all interrupts (no priorities).
210 * (Clear the interrupt before processing, in case it's
211 * edge-triggered or software-generated)
214 for (i
=0, mask
= 1; i
< XCHAL_NUM_INTERRUPTS
; i
++, mask
<<= 1) {
215 if (mask
& (intread
& intenable
)) {
216 set_sr (mask
, intclear
);
223 * Illegal instruction. Fatal if in kernel space.
227 do_illegal_instruction(struct pt_regs
*regs
)
229 __die_if_kernel("Illegal instruction in kernel", regs
, SIGKILL
);
231 /* If in user mode, send SIGILL signal to current process. */
233 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
234 current
->comm
, task_pid_nr(current
), regs
->pc
);
235 force_sig(SIGILL
, current
);
240 * Handle unaligned memory accesses from user space. Kill task.
242 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
243 * accesses causes from user space.
246 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
247 #ifndef CONFIG_XTENSA_UNALIGNED_USER
249 do_unaligned_user (struct pt_regs
*regs
)
253 __die_if_kernel("Unhandled unaligned exception in kernel",
256 current
->thread
.bad_vaddr
= regs
->excvaddr
;
257 current
->thread
.error_code
= -3;
258 printk("Unaligned memory access to %08lx in '%s' "
259 "(pid = %d, pc = %#010lx)\n",
260 regs
->excvaddr
, current
->comm
, task_pid_nr(current
), regs
->pc
);
261 info
.si_signo
= SIGBUS
;
263 info
.si_code
= BUS_ADRALN
;
264 info
.si_addr
= (void *) regs
->excvaddr
;
265 force_sig_info(SIGSEGV
, &info
, current
);
272 do_debug(struct pt_regs
*regs
)
275 /* If remote debugging is configured AND enabled, we give control to
276 * kgdb. Otherwise, we fall through, perhaps giving control to the
281 extern void gdb_handle_exception(struct pt_regs
*);
282 gdb_handle_exception(regs
);
283 return_from_debug_flag
= 1;
288 __die_if_kernel("Breakpoint in kernel", regs
, SIGKILL
);
290 /* If in user mode, send SIGTRAP signal to current process */
292 force_sig(SIGTRAP
, current
);
296 /* Set exception C handler - for temporary use when probing exceptions */
298 void * __init
trap_set_handler(int cause
, void *handler
)
300 unsigned long *entry
= &exc_table
[EXC_TABLE_DEFAULT
/ 4 + cause
];
301 void *previous
= (void *)*entry
;
302 *entry
= (unsigned long)handler
;
308 * Initialize dispatch tables.
310 * The exception vectors are stored compressed the __init section in the
311 * dispatch_init_table. This function initializes the following three tables
312 * from that compressed table:
313 * - fast user first dispatch table for user exceptions
314 * - fast kernel first dispatch table for kernel exceptions
315 * - default C-handler C-handler called by the default fast handler.
317 * See vectors.S for more details.
320 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
322 void __init
trap_init(void)
326 /* Setup default vectors. */
328 for(i
= 0; i
< 64; i
++) {
329 set_handler(EXC_TABLE_FAST_USER
/4 + i
, user_exception
);
330 set_handler(EXC_TABLE_FAST_KERNEL
/4 + i
, kernel_exception
);
331 set_handler(EXC_TABLE_DEFAULT
/4 + i
, do_unhandled
);
334 /* Setup specific handlers. */
336 for(i
= 0; dispatch_init_table
[i
].cause
>= 0; i
++) {
338 int fast
= dispatch_init_table
[i
].fast
;
339 int cause
= dispatch_init_table
[i
].cause
;
340 void *handler
= dispatch_init_table
[i
].handler
;
343 set_handler (EXC_TABLE_DEFAULT
/4 + cause
, handler
);
344 if (fast
&& fast
& USER
)
345 set_handler (EXC_TABLE_FAST_USER
/4 + cause
, handler
);
346 if (fast
&& fast
& KRNL
)
347 set_handler (EXC_TABLE_FAST_KERNEL
/4 + cause
, handler
);
350 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
352 i
= (unsigned long)exc_table
;
353 __asm__
__volatile__("wsr %0, excsave1\n" : : "a" (i
));
357 * This function dumps the current valid window frame and other base registers.
360 void show_regs(struct pt_regs
* regs
)
364 wmask
= regs
->wmask
& ~1;
366 for (i
= 0; i
< 16; i
++) {
368 printk(KERN_INFO
"a%02d:", i
);
369 printk(KERN_CONT
" %08lx", regs
->areg
[i
]);
371 printk(KERN_CONT
"\n");
373 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
374 regs
->pc
, regs
->ps
, regs
->depc
, regs
->excvaddr
);
375 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
376 regs
->lbeg
, regs
->lend
, regs
->lcount
, regs
->sar
);
378 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
379 regs
->windowbase
, regs
->windowstart
, regs
->wmask
,
383 static __always_inline
unsigned long *stack_pointer(struct task_struct
*task
)
387 if (!task
|| task
== current
)
388 __asm__
__volatile__ ("mov %0, a1\n" : "=a"(sp
));
390 sp
= (unsigned long *)task
->thread
.sp
;
395 static inline void spill_registers(void)
399 __asm__
__volatile__ (
400 "movi a14, " __stringify(PS_EXCM_BIT
| 1) "\n\t"
404 "movi a0, _spill_registers\n\t"
410 :: "a" (&a0
), "a" (&ps
)
411 : "a2", "a3", "a4", "a7", "a11", "a12", "a13", "a14", "a15",
415 void show_trace(struct task_struct
*task
, unsigned long *sp
)
417 unsigned long a0
, a1
, pc
;
418 unsigned long sp_start
, sp_end
;
421 a1
= (unsigned long)sp
;
423 a1
= (unsigned long)stack_pointer(task
);
425 sp_start
= a1
& ~(THREAD_SIZE
-1);
426 sp_end
= sp_start
+ THREAD_SIZE
;
428 printk("Call Trace:");
429 #ifdef CONFIG_KALLSYMS
434 while (a1
> sp_start
&& a1
< sp_end
) {
435 sp
= (unsigned long*)a1
;
440 if (a1
<= (unsigned long) sp
)
443 pc
= MAKE_PC_FROM_RA(a0
, a1
);
445 if (kernel_text_address(pc
)) {
446 printk(" [<%08lx>] ", pc
);
447 print_symbol("%s\n", pc
);
454 * This routine abuses get_user()/put_user() to reference pointers
455 * with at least a bit of error checking ...
458 static int kstack_depth_to_print
= 24;
460 void show_stack(struct task_struct
*task
, unsigned long *sp
)
463 unsigned long *stack
;
466 sp
= stack_pointer(task
);
471 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
474 if (i
&& ((i
% 8) == 0))
476 printk("%08lx ", *sp
++);
479 show_trace(task
, stack
);
482 void dump_stack(void)
484 show_stack(current
, NULL
);
487 EXPORT_SYMBOL(dump_stack
);
490 void show_code(unsigned int *pc
)
496 for(i
= -3 ; i
< 6 ; i
++) {
498 if (__get_user(insn
, pc
+ i
)) {
499 printk(" (Bad address in pc)\n");
502 printk("%c%08lx%c",(i
?' ':'<'),insn
,(i
?' ':'>'));
506 DEFINE_SPINLOCK(die_lock
);
508 void die(const char * str
, struct pt_regs
* regs
, long err
)
510 static int die_counter
;
514 spin_lock_irq(&die_lock
);
516 printk("%s: sig: %ld [#%d]\n", str
, err
, ++die_counter
);
517 #ifdef CONFIG_PREEMPT
524 if (!user_mode(regs
))
525 show_stack(NULL
, (unsigned long*)regs
->areg
[1]);
527 add_taint(TAINT_DIE
);
528 spin_unlock_irq(&die_lock
);
531 panic("Fatal exception in interrupt");
534 panic("Fatal exception");