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 - 2013 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/stacktrace.h>
36 #include <asm/ptrace.h>
37 #include <asm/timex.h>
38 #include <asm/uaccess.h>
39 #include <asm/pgtable.h>
40 #include <asm/processor.h>
41 #include <asm/traps.h>
45 extern int return_from_debug_flag;
49 * Machine specific interrupt handlers
52 extern void kernel_exception(void);
53 extern void user_exception(void);
55 extern void fast_syscall_kernel(void);
56 extern void fast_syscall_user(void);
57 extern void fast_alloca(void);
58 extern void fast_unaligned(void);
59 extern void fast_second_level_miss(void);
60 extern void fast_store_prohibited(void);
61 extern void fast_coprocessor(void);
63 extern void do_illegal_instruction (struct pt_regs*);
64 extern void do_interrupt (struct pt_regs*);
65 extern void do_unaligned_user (struct pt_regs*);
66 extern void do_multihit (struct pt_regs*, unsigned long);
67 extern void do_page_fault (struct pt_regs*, unsigned long);
68 extern void do_debug (struct pt_regs*);
69 extern void system_call (struct pt_regs*);
72 * The vector table must be preceded by a save area (which
73 * implies it must be in RAM, unless one places RAM immediately
74 * before a ROM and puts the vector at the start of the ROM (!))
80 #define COPROCESSOR(x) \
81 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
87 } dispatch_init_table_t;
89 static dispatch_init_table_t __initdata dispatch_init_table[] = {
91 { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
92 { EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
93 { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
94 { EXCCAUSE_SYSTEM_CALL, 0, system_call },
95 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
96 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
97 { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
98 { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
99 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
100 /* EXCCAUSE_PRIVILEGED unhandled */
101 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
102 #ifdef CONFIG_XTENSA_UNALIGNED_USER
103 { EXCCAUSE_UNALIGNED, USER, fast_unaligned },
105 { EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
107 { EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
110 { EXCCAUSE_ITLB_MISS, 0, do_page_fault },
111 { EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
112 { EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
113 { EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
114 /* EXCCAUSE_SIZE_RESTRICTION unhandled */
115 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
116 { EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
117 { EXCCAUSE_DTLB_MISS, 0, do_page_fault },
118 { EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
119 { EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
120 /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
121 { EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
122 { EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
123 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
124 #endif /* CONFIG_MMU */
125 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
126 #if XTENSA_HAVE_COPROCESSOR(0)
129 #if XTENSA_HAVE_COPROCESSOR(1)
132 #if XTENSA_HAVE_COPROCESSOR(2)
135 #if XTENSA_HAVE_COPROCESSOR(3)
138 #if XTENSA_HAVE_COPROCESSOR(4)
141 #if XTENSA_HAVE_COPROCESSOR(5)
144 #if XTENSA_HAVE_COPROCESSOR(6)
147 #if XTENSA_HAVE_COPROCESSOR(7)
150 { EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
155 /* The exception table <exc_table> serves two functions:
156 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
157 * 2. it is a temporary memory buffer for the exception handlers.
160 unsigned long exc_table[EXC_TABLE_SIZE/4];
162 void die(const char*, struct pt_regs*, long);
165 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
167 if (!user_mode(regs))
172 * Unhandled Exceptions. Kill user task or panic if in kernel space.
175 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
177 __die_if_kernel("Caught unhandled exception - should not happen",
180 /* If in user mode, send SIGILL signal to current process */
181 printk("Caught unhandled exception in '%s' "
182 "(pid = %d, pc = %#010lx) - should not happen\n"
183 "\tEXCCAUSE is %ld\n",
184 current->comm, task_pid_nr(current), regs->pc, exccause);
185 force_sig(SIGILL, current);
189 * Multi-hit exception. This if fatal!
192 void do_multihit(struct pt_regs *regs, unsigned long exccause)
194 die("Caught multihit exception", regs, SIGKILL);
199 * PS.INTLEVEL is the current IRQ priority level.
202 extern void do_IRQ(int, struct pt_regs *);
204 void do_interrupt(struct pt_regs *regs)
206 static const unsigned int_level_mask[] = {
208 XCHAL_INTLEVEL1_MASK,
209 XCHAL_INTLEVEL2_MASK,
210 XCHAL_INTLEVEL3_MASK,
211 XCHAL_INTLEVEL4_MASK,
212 XCHAL_INTLEVEL5_MASK,
213 XCHAL_INTLEVEL6_MASK,
214 XCHAL_INTLEVEL7_MASK,
216 unsigned level = get_sr(ps) & PS_INTLEVEL_MASK;
218 if (WARN_ON_ONCE(level >= ARRAY_SIZE(int_level_mask)))
222 unsigned intread = get_sr(interrupt);
223 unsigned intenable = get_sr(intenable);
224 unsigned int_at_level = intread & intenable &
225 int_level_mask[level];
231 * Clear the interrupt before processing, in case it's
232 * edge-triggered or software-generated
234 while (int_at_level) {
235 unsigned i = __ffs(int_at_level);
236 unsigned mask = 1 << i;
238 int_at_level ^= mask;
239 set_sr(mask, intclear);
246 * Illegal instruction. Fatal if in kernel space.
250 do_illegal_instruction(struct pt_regs *regs)
252 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
254 /* If in user mode, send SIGILL signal to current process. */
256 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
257 current->comm, task_pid_nr(current), regs->pc);
258 force_sig(SIGILL, current);
263 * Handle unaligned memory accesses from user space. Kill task.
265 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
266 * accesses causes from user space.
269 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
270 #ifndef CONFIG_XTENSA_UNALIGNED_USER
272 do_unaligned_user (struct pt_regs *regs)
276 __die_if_kernel("Unhandled unaligned exception in kernel",
279 current->thread.bad_vaddr = regs->excvaddr;
280 current->thread.error_code = -3;
281 printk("Unaligned memory access to %08lx in '%s' "
282 "(pid = %d, pc = %#010lx)\n",
283 regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
284 info.si_signo = SIGBUS;
286 info.si_code = BUS_ADRALN;
287 info.si_addr = (void *) regs->excvaddr;
288 force_sig_info(SIGSEGV, &info, current);
295 do_debug(struct pt_regs *regs)
298 /* If remote debugging is configured AND enabled, we give control to
299 * kgdb. Otherwise, we fall through, perhaps giving control to the
304 extern void gdb_handle_exception(struct pt_regs *);
305 gdb_handle_exception(regs);
306 return_from_debug_flag = 1;
311 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
313 /* If in user mode, send SIGTRAP signal to current process */
315 force_sig(SIGTRAP, current);
319 /* Set exception C handler - for temporary use when probing exceptions */
321 void * __init trap_set_handler(int cause, void *handler)
323 unsigned long *entry = &exc_table[EXC_TABLE_DEFAULT / 4 + cause];
324 void *previous = (void *)*entry;
325 *entry = (unsigned long)handler;
331 * Initialize dispatch tables.
333 * The exception vectors are stored compressed the __init section in the
334 * dispatch_init_table. This function initializes the following three tables
335 * from that compressed table:
336 * - fast user first dispatch table for user exceptions
337 * - fast kernel first dispatch table for kernel exceptions
338 * - default C-handler C-handler called by the default fast handler.
340 * See vectors.S for more details.
343 #define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
345 void __init trap_init(void)
349 /* Setup default vectors. */
351 for(i = 0; i < 64; i++) {
352 set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
353 set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
354 set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
357 /* Setup specific handlers. */
359 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
361 int fast = dispatch_init_table[i].fast;
362 int cause = dispatch_init_table[i].cause;
363 void *handler = dispatch_init_table[i].handler;
366 set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
367 if (fast && fast & USER)
368 set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
369 if (fast && fast & KRNL)
370 set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
373 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
375 i = (unsigned long)exc_table;
376 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (i));
380 * This function dumps the current valid window frame and other base registers.
383 void show_regs(struct pt_regs * regs)
387 wmask = regs->wmask & ~1;
389 for (i = 0; i < 16; i++) {
391 printk(KERN_INFO "a%02d:", i);
392 printk(KERN_CONT " %08lx", regs->areg[i]);
394 printk(KERN_CONT "\n");
396 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
397 regs->pc, regs->ps, regs->depc, regs->excvaddr);
398 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
399 regs->lbeg, regs->lend, regs->lcount, regs->sar);
401 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
402 regs->windowbase, regs->windowstart, regs->wmask,
406 static int show_trace_cb(struct stackframe *frame, void *data)
408 if (kernel_text_address(frame->pc)) {
409 printk(" [<%08lx>] ", frame->pc);
410 print_symbol("%s\n", frame->pc);
415 void show_trace(struct task_struct *task, unsigned long *sp)
418 sp = stack_pointer(task);
420 printk("Call Trace:");
421 #ifdef CONFIG_KALLSYMS
424 walk_stackframe(sp, show_trace_cb, NULL);
429 * This routine abuses get_user()/put_user() to reference pointers
430 * with at least a bit of error checking ...
433 static int kstack_depth_to_print = 24;
435 void show_stack(struct task_struct *task, unsigned long *sp)
438 unsigned long *stack;
441 sp = stack_pointer(task);
446 for (i = 0; i < kstack_depth_to_print; i++) {
449 if (i && ((i % 8) == 0))
451 printk("%08lx ", *sp++);
454 show_trace(task, stack);
457 void dump_stack(void)
459 show_stack(current, NULL);
462 EXPORT_SYMBOL(dump_stack);
465 void show_code(unsigned int *pc)
471 for(i = -3 ; i < 6 ; i++) {
473 if (__get_user(insn, pc + i)) {
474 printk(" (Bad address in pc)\n");
477 printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
481 DEFINE_SPINLOCK(die_lock);
483 void die(const char * str, struct pt_regs * regs, long err)
485 static int die_counter;
489 spin_lock_irq(&die_lock);
491 printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
492 #ifdef CONFIG_PREEMPT
499 if (!user_mode(regs))
500 show_stack(NULL, (unsigned long*)regs->areg[1]);
502 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
503 spin_unlock_irq(&die_lock);
506 panic("Fatal exception in interrupt");
509 panic("Fatal exception");