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_nmi(struct pt_regs *);
66 extern void do_unaligned_user (struct pt_regs*);
67 extern void do_multihit (struct pt_regs*, unsigned long);
68 extern void do_page_fault (struct pt_regs*, unsigned long);
69 extern void do_debug (struct pt_regs*);
70 extern void system_call (struct pt_regs*);
73 * The vector table must be preceded by a save area (which
74 * implies it must be in RAM, unless one places RAM immediately
75 * before a ROM and puts the vector at the start of the ROM (!))
81 #define COPROCESSOR(x) \
82 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
88 } dispatch_init_table_t;
90 static dispatch_init_table_t __initdata dispatch_init_table[] = {
92 { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
93 { EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
94 { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
95 { EXCCAUSE_SYSTEM_CALL, 0, system_call },
96 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
97 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
98 { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
99 { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
100 /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
101 /* EXCCAUSE_PRIVILEGED unhandled */
102 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
103 #ifdef CONFIG_XTENSA_UNALIGNED_USER
104 { EXCCAUSE_UNALIGNED, USER, fast_unaligned },
106 { 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)
151 { EXCCAUSE_MAPPED_NMI, 0, do_nmi },
153 { EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
158 /* The exception table <exc_table> serves two functions:
159 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
160 * 2. it is a temporary memory buffer for the exception handlers.
163 DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);
165 void die(const char*, struct pt_regs*, long);
168 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
170 if (!user_mode(regs))
175 * Unhandled Exceptions. Kill user task or panic if in kernel space.
178 void do_unhandled(struct pt_regs *regs, unsigned long exccause)
180 __die_if_kernel("Caught unhandled exception - should not happen",
183 /* If in user mode, send SIGILL signal to current process */
184 printk("Caught unhandled exception in '%s' "
185 "(pid = %d, pc = %#010lx) - should not happen\n"
186 "\tEXCCAUSE is %ld\n",
187 current->comm, task_pid_nr(current), regs->pc, exccause);
188 force_sig(SIGILL, current);
192 * Multi-hit exception. This if fatal!
195 void do_multihit(struct pt_regs *regs, unsigned long exccause)
197 die("Caught multihit exception", regs, SIGKILL);
204 extern void do_IRQ(int, struct pt_regs *);
208 #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
210 #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
211 IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
212 #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
213 #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
215 static inline void check_valid_nmi(void)
217 unsigned intread = get_sr(interrupt);
218 unsigned intenable = get_sr(intenable);
220 BUG_ON(intread & intenable &
221 ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
222 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
223 BIT(XCHAL_PROFILING_INTERRUPT)));
228 static inline void check_valid_nmi(void)
234 irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
236 DEFINE_PER_CPU(unsigned long, nmi_count);
238 void do_nmi(struct pt_regs *regs)
240 struct pt_regs *old_regs;
242 if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
243 trace_hardirqs_off();
245 old_regs = set_irq_regs(regs);
247 ++*this_cpu_ptr(&nmi_count);
249 xtensa_pmu_irq_handler(0, NULL);
251 set_irq_regs(old_regs);
255 void do_interrupt(struct pt_regs *regs)
257 static const unsigned int_level_mask[] = {
259 XCHAL_INTLEVEL1_MASK,
260 XCHAL_INTLEVEL2_MASK,
261 XCHAL_INTLEVEL3_MASK,
262 XCHAL_INTLEVEL4_MASK,
263 XCHAL_INTLEVEL5_MASK,
264 XCHAL_INTLEVEL6_MASK,
265 XCHAL_INTLEVEL7_MASK,
267 struct pt_regs *old_regs;
269 trace_hardirqs_off();
271 old_regs = set_irq_regs(regs);
275 unsigned intread = get_sr(interrupt);
276 unsigned intenable = get_sr(intenable);
277 unsigned int_at_level = intread & intenable;
280 for (level = LOCKLEVEL; level > 0; --level) {
281 if (int_at_level & int_level_mask[level]) {
282 int_at_level &= int_level_mask[level];
290 do_IRQ(__ffs(int_at_level), regs);
294 set_irq_regs(old_regs);
298 * Illegal instruction. Fatal if in kernel space.
302 do_illegal_instruction(struct pt_regs *regs)
304 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
306 /* If in user mode, send SIGILL signal to current process. */
308 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
309 current->comm, task_pid_nr(current), regs->pc);
310 force_sig(SIGILL, current);
315 * Handle unaligned memory accesses from user space. Kill task.
317 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
318 * accesses causes from user space.
321 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
323 do_unaligned_user (struct pt_regs *regs)
327 __die_if_kernel("Unhandled unaligned exception in kernel",
330 current->thread.bad_vaddr = regs->excvaddr;
331 current->thread.error_code = -3;
332 printk("Unaligned memory access to %08lx in '%s' "
333 "(pid = %d, pc = %#010lx)\n",
334 regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
335 info.si_signo = SIGBUS;
337 info.si_code = BUS_ADRALN;
338 info.si_addr = (void *) regs->excvaddr;
339 force_sig_info(SIGSEGV, &info, current);
345 do_debug(struct pt_regs *regs)
348 /* If remote debugging is configured AND enabled, we give control to
349 * kgdb. Otherwise, we fall through, perhaps giving control to the
354 extern void gdb_handle_exception(struct pt_regs *);
355 gdb_handle_exception(regs);
356 return_from_debug_flag = 1;
361 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
363 /* If in user mode, send SIGTRAP signal to current process */
365 force_sig(SIGTRAP, current);
369 static void set_handler(int idx, void *handler)
373 for_each_possible_cpu(cpu)
374 per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
377 /* Set exception C handler - for temporary use when probing exceptions */
379 void * __init trap_set_handler(int cause, void *handler)
381 void *previous = (void *)per_cpu(exc_table, 0)[
382 EXC_TABLE_DEFAULT / 4 + cause];
383 set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
388 static void trap_init_excsave(void)
390 unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
391 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1));
395 * Initialize dispatch tables.
397 * The exception vectors are stored compressed the __init section in the
398 * dispatch_init_table. This function initializes the following three tables
399 * from that compressed table:
400 * - fast user first dispatch table for user exceptions
401 * - fast kernel first dispatch table for kernel exceptions
402 * - default C-handler C-handler called by the default fast handler.
404 * See vectors.S for more details.
407 void __init trap_init(void)
411 /* Setup default vectors. */
413 for(i = 0; i < 64; i++) {
414 set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
415 set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
416 set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
419 /* Setup specific handlers. */
421 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
423 int fast = dispatch_init_table[i].fast;
424 int cause = dispatch_init_table[i].cause;
425 void *handler = dispatch_init_table[i].handler;
428 set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
429 if (fast && fast & USER)
430 set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
431 if (fast && fast & KRNL)
432 set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
435 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
440 void secondary_trap_init(void)
447 * This function dumps the current valid window frame and other base registers.
450 void show_regs(struct pt_regs * regs)
454 show_regs_print_info(KERN_DEFAULT);
456 wmask = regs->wmask & ~1;
458 for (i = 0; i < 16; i++) {
460 printk(KERN_INFO "a%02d:", i);
461 printk(KERN_CONT " %08lx", regs->areg[i]);
463 printk(KERN_CONT "\n");
465 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
466 regs->pc, regs->ps, regs->depc, regs->excvaddr);
467 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
468 regs->lbeg, regs->lend, regs->lcount, regs->sar);
470 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
471 regs->windowbase, regs->windowstart, regs->wmask,
475 static int show_trace_cb(struct stackframe *frame, void *data)
477 if (kernel_text_address(frame->pc)) {
478 printk(" [<%08lx>] ", frame->pc);
479 print_symbol("%s\n", frame->pc);
484 void show_trace(struct task_struct *task, unsigned long *sp)
487 sp = stack_pointer(task);
489 printk("Call Trace:");
490 #ifdef CONFIG_KALLSYMS
493 walk_stackframe(sp, show_trace_cb, NULL);
498 * This routine abuses get_user()/put_user() to reference pointers
499 * with at least a bit of error checking ...
502 static int kstack_depth_to_print = 24;
504 void show_stack(struct task_struct *task, unsigned long *sp)
507 unsigned long *stack;
510 sp = stack_pointer(task);
515 for (i = 0; i < kstack_depth_to_print; i++) {
518 if (i && ((i % 8) == 0))
520 printk("%08lx ", *sp++);
523 show_trace(task, stack);
526 void show_code(unsigned int *pc)
532 for(i = -3 ; i < 6 ; i++) {
534 if (__get_user(insn, pc + i)) {
535 printk(" (Bad address in pc)\n");
538 printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
542 DEFINE_SPINLOCK(die_lock);
544 void die(const char * str, struct pt_regs * regs, long err)
546 static int die_counter;
550 spin_lock_irq(&die_lock);
552 printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
553 #ifdef CONFIG_PREEMPT
560 if (!user_mode(regs))
561 show_stack(NULL, (unsigned long*)regs->areg[1]);
563 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
564 spin_unlock_irq(&die_lock);
567 panic("Fatal exception in interrupt");
570 panic("Fatal exception");