1 /* MN10300 Exception handling
3 * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd.
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Modified by David Howells (dhowells@redhat.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/timer.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/kallsyms.h>
26 #include <linux/pci.h>
27 #include <linux/kdebug.h>
28 #include <linux/bug.h>
29 #include <linux/irq.h>
30 #include <asm/processor.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
34 #include <asm/atomic.h>
36 #include <asm/pgalloc.h>
37 #include <asm/cacheflush.h>
38 #include <asm/cpu-regs.h>
39 #include <asm/busctl-regs.h>
40 #include <unit/leds.h>
42 #include <asm/gdb-stub.h>
43 #include <asm/sections.h>
45 #if (CONFIG_INTERRUPT_VECTOR_BASE & 0xffffff)
46 #error "INTERRUPT_VECTOR_BASE not aligned to 16MiB boundary!"
49 struct pt_regs *__frame; /* current frame pointer */
50 EXPORT_SYMBOL(__frame);
52 int kstack_depth_to_print = 24;
54 spinlock_t die_lock = __SPIN_LOCK_UNLOCKED(die_lock);
56 ATOMIC_NOTIFIER_HEAD(mn10300_die_chain);
59 * These constants are for searching for possible module text
60 * segments. MODULE_RANGE is a guess of how much space is likely
63 #define MODULE_RANGE (8 * 1024 * 1024)
65 #define DO_ERROR(signr, prologue, str, name) \
66 asmlinkage void name(struct pt_regs *regs, u32 intcode) \
69 if (die_if_no_fixup(str, regs, intcode)) \
71 force_sig(signr, current); \
74 #define DO_EINFO(signr, prologue, str, name, sicode) \
75 asmlinkage void name(struct pt_regs *regs, u32 intcode) \
79 if (die_if_no_fixup(str, regs, intcode)) \
81 info.si_signo = signr; \
82 if (signr == SIGILL && sicode == ILL_ILLOPC) { \
84 if (get_user(opcode, (uint8_t __user *)regs->pc) == 0) \
86 info.si_signo = SIGTRAP; \
89 info.si_code = sicode; \
90 info.si_addr = (void *) regs->pc; \
91 force_sig_info(info.si_signo, &info, current); \
94 DO_ERROR(SIGTRAP, {}, "trap", trap);
95 DO_ERROR(SIGSEGV, {}, "ibreak", ibreak);
96 DO_ERROR(SIGSEGV, {}, "obreak", obreak);
97 DO_EINFO(SIGSEGV, {}, "access error", access_error, SEGV_ACCERR);
98 DO_EINFO(SIGSEGV, {}, "insn access error", insn_acc_error, SEGV_ACCERR);
99 DO_EINFO(SIGSEGV, {}, "data access error", data_acc_error, SEGV_ACCERR);
100 DO_EINFO(SIGILL, {}, "privileged opcode", priv_op, ILL_PRVOPC);
101 DO_EINFO(SIGILL, {}, "invalid opcode", invalid_op, ILL_ILLOPC);
102 DO_EINFO(SIGILL, {}, "invalid ex opcode", invalid_exop, ILL_ILLOPC);
103 DO_EINFO(SIGBUS, {}, "invalid address", mem_error, BUS_ADRERR);
104 DO_EINFO(SIGBUS, {}, "bus error", bus_error, BUS_ADRERR);
105 DO_EINFO(SIGILL, {}, "FPU invalid opcode", fpu_invalid_op, ILL_COPROC);
108 #ifndef CONFIG_MN10300_USING_JTAG
113 "single step", istep);
118 asmlinkage void nmi(struct pt_regs *regs, enum exception_code code)
120 /* see if gdbstub wants to deal with it */
121 #ifdef CONFIG_GDBSTUB
122 if (gdbstub_intercept(regs, code))
126 printk(KERN_WARNING "--- Register Dump ---\n");
127 show_registers(regs);
128 printk(KERN_WARNING "---------------------\n");
132 * show a stack trace from the specified stack pointer
134 void show_trace(unsigned long *sp)
136 unsigned long *stack, addr, module_start, module_end;
139 printk(KERN_EMERG "\n"
140 KERN_EMERG "Call Trace:");
144 module_start = VMALLOC_START;
145 module_end = VMALLOC_END;
147 while (((long) stack & (THREAD_SIZE - 1)) != 0) {
149 if (__kernel_text_address(addr)) {
151 printk(" [<%08lx>]", addr);
152 print_symbol(" %s", addr);
156 printk("\n" KERN_EMERG " ");
157 printk("[<%08lx>] ", addr);
167 * show the raw stack from the specified stack pointer
169 void show_stack(struct task_struct *task, unsigned long *sp)
171 unsigned long *stack;
175 sp = (unsigned long *) &sp;
178 printk(KERN_EMERG "Stack:");
179 for (i = 0; i < kstack_depth_to_print; i++) {
180 if (((long) stack & (THREAD_SIZE - 1)) == 0)
183 printk("\n" KERN_EMERG " ");
184 printk("%08lx ", *stack++);
191 * the architecture-independent dump_stack generator
193 void dump_stack(void)
197 show_stack(current, &stack);
199 EXPORT_SYMBOL(dump_stack);
202 * dump the register file in the specified exception frame
204 void show_registers_only(struct pt_regs *regs)
208 ssp = (unsigned long) regs + sizeof(*regs);
210 printk(KERN_EMERG "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
211 regs->pc, regs->epsw, ssp, user_mode(regs) ? "User" : "Super");
212 printk(KERN_EMERG "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
213 regs->d0, regs->d1, regs->d2, regs->d3);
214 printk(KERN_EMERG "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
215 regs->a0, regs->a1, regs->a2, regs->a3);
216 printk(KERN_EMERG "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
217 regs->e0, regs->e1, regs->e2, regs->e3);
218 printk(KERN_EMERG "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
219 regs->e4, regs->e5, regs->e6, regs->e7);
220 printk(KERN_EMERG "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
221 regs->lar, regs->lir, regs->mdr, regs->sp);
222 printk(KERN_EMERG "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
223 regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
224 printk(KERN_EMERG "threadinfo=%p task=%p)\n",
225 current_thread_info(), current);
227 if ((unsigned long) current >= 0x90000000UL &&
228 (unsigned long) current < 0x94000000UL)
229 printk(KERN_EMERG "Process %s (pid: %d)\n",
230 current->comm, current->pid);
232 printk(KERN_EMERG "CPUP: %04hx\n", CPUP);
233 printk(KERN_EMERG "TBR: %08x\n", TBR);
234 printk(KERN_EMERG "DEAR: %08x\n", DEAR);
235 printk(KERN_EMERG "sISR: %08x\n", sISR);
236 printk(KERN_EMERG "NMICR: %04hx\n", NMICR);
237 printk(KERN_EMERG "BCBERR: %08x\n", BCBERR);
238 printk(KERN_EMERG "BCBEAR: %08x\n", BCBEAR);
239 printk(KERN_EMERG "MMUFCR: %08x\n", MMUFCR);
240 printk(KERN_EMERG "IPTEU : %08x IPTEL2: %08x\n", IPTEU, IPTEL2);
241 printk(KERN_EMERG "DPTEU: %08x DPTEL2: %08x\n", DPTEU, DPTEL2);
245 * dump the registers and the stack
247 void show_registers(struct pt_regs *regs)
252 show_registers_only(regs);
254 if (!user_mode(regs))
255 sp = (unsigned long) regs + sizeof(*regs);
259 /* when in-kernel, we also print out the stack and code at the
260 * time of the fault..
262 if (!user_mode(regs)) {
263 printk(KERN_EMERG "\n");
264 show_stack(current, (unsigned long *) sp);
267 printk(KERN_EMERG "\n"
268 KERN_EMERG "Code: ");
269 if (regs->pc < PAGE_OFFSET)
272 for (i = 0; i < 20; i++) {
274 if (__get_user(c, &((unsigned char *) regs->pc)[i]))
288 printk(KERN_EMERG " Bad PC value.");
296 void show_trace_task(struct task_struct *tsk)
298 unsigned long sp = tsk->thread.sp;
300 /* User space on another CPU? */
301 if ((sp ^ (unsigned long) tsk) & (PAGE_MASK << 1))
304 show_trace((unsigned long *) sp);
308 * note the untimely death of part of the kernel
310 void die(const char *str, struct pt_regs *regs, enum exception_code code)
313 spin_lock_irq(&die_lock);
314 printk(KERN_EMERG "\n"
315 KERN_EMERG "%s: %04x\n",
317 show_registers(regs);
319 if (regs->pc >= 0x02000000 && regs->pc < 0x04000000 &&
320 (regs->epsw & (EPSW_IM | EPSW_IE)) != (EPSW_IM | EPSW_IE)) {
321 printk(KERN_EMERG "Exception in usermode interrupt handler\n");
322 printk(KERN_EMERG "\n"
323 KERN_EMERG " Please connect to kernel debugger !!\n");
324 asm volatile ("0: bra 0b");
327 spin_unlock_irq(&die_lock);
332 * see if there's a fixup handler we can force a jump to when an exception
333 * happens due to something kernel code did
335 int die_if_no_fixup(const char *str, struct pt_regs *regs,
336 enum exception_code code)
341 peripheral_leds_display_exception(code);
344 /* see if we can fixup the kernel accessing memory */
352 case EXCEP_ILLDATACC:
354 case EXCEP_PRIVINSACC:
355 case EXCEP_PRIVDATACC:
356 case EXCEP_DATINSACC:
357 if (fixup_exception(regs))
360 if (regs->pc && *(uint8_t *)regs->pc == 0xff)
361 if (notify_die(DIE_BREAKPOINT, str, regs, code, 0, 0))
368 /* see if gdbstub wants to deal with it */
369 #ifdef CONFIG_GDBSTUB
370 if (gdbstub_intercept(regs, code))
374 if (notify_die(DIE_GPF, str, regs, code, 0, 0))
377 /* make the process die as the last resort */
378 die(str, regs, code);
382 * handle unsupported syscall instructions (syscall 1-15)
384 static asmlinkage void unsupported_syscall(struct pt_regs *regs,
385 enum exception_code code)
387 struct task_struct *tsk = current;
390 /* catch a kernel BUG() */
391 if (code == EXCEP_SYSCALL15 && !user_mode(regs)) {
392 if (report_bug(regs->pc, regs) == BUG_TRAP_TYPE_BUG) {
393 #ifdef CONFIG_GDBSTUB
394 gdbstub_intercept(regs, code);
399 regs->pc -= 2; /* syscall return addr is _after_ the instruction */
401 die_if_no_fixup("An unsupported syscall insn was used by the kernel\n",
404 info.si_signo = SIGILL;
405 info.si_errno = ENOSYS;
406 info.si_code = ILL_ILLTRP;
407 info.si_addr = (void *) regs->pc;
408 force_sig_info(SIGILL, &info, tsk);
412 * display the register file when the stack pointer gets clobbered
414 asmlinkage void do_double_fault(struct pt_regs *regs)
416 struct task_struct *tsk = current;
418 strcpy(tsk->comm, "emergency tsk");
421 printk(KERN_EMERG "--- double fault ---\n");
422 show_registers(regs);
426 * asynchronous bus error (external, usually I/O DMA)
428 asmlinkage void io_bus_error(u32 bcberr, u32 bcbear, struct pt_regs *regs)
432 printk(KERN_EMERG "\n"
433 KERN_EMERG "Asynchronous I/O Bus Error\n"
434 KERN_EMERG "==========================\n");
436 if (bcberr & BCBERR_BEME)
437 printk(KERN_EMERG "- Multiple recorded errors\n");
439 printk(KERN_EMERG "- Faulting Buses:%s%s%s\n",
440 bcberr & BCBERR_BEMR_CI ? " CPU-Ins-Fetch" : "",
441 bcberr & BCBERR_BEMR_CD ? " CPU-Data" : "",
442 bcberr & BCBERR_BEMR_DMA ? " DMA" : "");
444 printk(KERN_EMERG "- %s %s access made to %s at address %08x\n",
445 bcberr & BCBERR_BEBST ? "Burst" : "Single",
446 bcberr & BCBERR_BERW ? "Read" : "Write",
447 bcberr & BCBERR_BESB_MON ? "Monitor Space" :
448 bcberr & BCBERR_BESB_IO ? "Internal CPU I/O Space" :
449 bcberr & BCBERR_BESB_EX ? "External I/O Bus" :
450 bcberr & BCBERR_BESB_OPEX ? "External Memory Bus" :
455 printk(KERN_EMERG "- Detected by the %s\n",
456 bcberr&BCBERR_BESD ? "Bus Control Unit" : "Slave Bus");
459 #define BRIDGEREGB(X) (*(volatile __u8 *)(0xBE040000 + (X)))
460 #define BRIDGEREGW(X) (*(volatile __u16 *)(0xBE040000 + (X)))
461 #define BRIDGEREGL(X) (*(volatile __u32 *)(0xBE040000 + (X)))
463 printk(KERN_EMERG "- PCI Memory Paging Reg: %08x\n",
464 *(volatile __u32 *) (0xBFFFFFF4));
465 printk(KERN_EMERG "- PCI Bridge Base Address 0: %08x\n",
466 BRIDGEREGL(PCI_BASE_ADDRESS_0));
467 printk(KERN_EMERG "- PCI Bridge AMPCI Base Address: %08x\n",
469 printk(KERN_EMERG "- PCI Bridge Command: %04hx\n",
470 BRIDGEREGW(PCI_COMMAND));
471 printk(KERN_EMERG "- PCI Bridge Status: %04hx\n",
472 BRIDGEREGW(PCI_STATUS));
473 printk(KERN_EMERG "- PCI Bridge Int Status: %08hx\n",
477 printk(KERN_EMERG "\n");
478 show_registers(regs);
480 panic("Halted due to asynchronous I/O Bus Error\n");
484 * handle an exception for which a handler has not yet been installed
486 asmlinkage void uninitialised_exception(struct pt_regs *regs,
487 enum exception_code code)
490 /* see if gdbstub wants to deal with it */
491 #ifdef CONFIG_GDBSTUB
492 if (gdbstub_intercept(regs, code))
496 peripheral_leds_display_exception(code);
497 printk(KERN_EMERG "Uninitialised Exception 0x%04x\n", code & 0xFFFF);
498 show_registers(regs);
505 * set an interrupt stub to jump to a handler
506 * ! NOTE: this does *not* flush the caches
508 void __init __set_intr_stub(enum exception_code code, void *handler)
511 u8 *vector = (u8 *)(CONFIG_INTERRUPT_VECTOR_BASE + code);
513 addr = (unsigned long) handler - (unsigned long) vector;
514 vector[0] = 0xdc; /* JMP handler */
516 vector[2] = addr >> 8;
517 vector[3] = addr >> 16;
518 vector[4] = addr >> 24;
525 * set an interrupt stub to jump to a handler
527 void __init set_intr_stub(enum exception_code code, void *handler)
530 u8 *vector = (u8 *)(CONFIG_INTERRUPT_VECTOR_BASE + code);
532 addr = (unsigned long) handler - (unsigned long) vector;
533 vector[0] = 0xdc; /* JMP handler */
535 vector[2] = addr >> 8;
536 vector[3] = addr >> 16;
537 vector[4] = addr >> 24;
542 mn10300_dcache_flush_inv();
543 mn10300_icache_inv();
547 * set an interrupt stub to invoke the JTAG unit and then jump to a handler
549 void __init set_jtag_stub(enum exception_code code, void *handler)
552 u8 *vector = (u8 *)(CONFIG_INTERRUPT_VECTOR_BASE + code);
554 addr = (unsigned long) handler - ((unsigned long) vector + 1);
555 vector[0] = 0xff; /* PI to jump into JTAG debugger */
556 vector[1] = 0xdc; /* jmp handler */
558 vector[3] = addr >> 8;
559 vector[4] = addr >> 16;
560 vector[5] = addr >> 24;
564 mn10300_dcache_flush_inv();
565 flush_icache_range((unsigned long) vector, (unsigned long) vector + 8);
569 * initialise the exception table
571 void __init trap_init(void)
573 set_excp_vector(EXCEP_TRAP, trap);
574 set_excp_vector(EXCEP_ISTEP, istep);
575 set_excp_vector(EXCEP_IBREAK, ibreak);
576 set_excp_vector(EXCEP_OBREAK, obreak);
578 set_excp_vector(EXCEP_PRIVINS, priv_op);
579 set_excp_vector(EXCEP_UNIMPINS, invalid_op);
580 set_excp_vector(EXCEP_UNIMPEXINS, invalid_exop);
581 set_excp_vector(EXCEP_MEMERR, mem_error);
582 set_excp_vector(EXCEP_MISALIGN, misalignment);
583 set_excp_vector(EXCEP_BUSERROR, bus_error);
584 set_excp_vector(EXCEP_ILLINSACC, insn_acc_error);
585 set_excp_vector(EXCEP_ILLDATACC, data_acc_error);
586 set_excp_vector(EXCEP_IOINSACC, insn_acc_error);
587 set_excp_vector(EXCEP_PRIVINSACC, insn_acc_error);
588 set_excp_vector(EXCEP_PRIVDATACC, data_acc_error);
589 set_excp_vector(EXCEP_DATINSACC, insn_acc_error);
590 set_excp_vector(EXCEP_FPU_DISABLED, fpu_disabled);
591 set_excp_vector(EXCEP_FPU_UNIMPINS, fpu_invalid_op);
592 set_excp_vector(EXCEP_FPU_OPERATION, fpu_exception);
594 set_excp_vector(EXCEP_NMI, nmi);
596 set_excp_vector(EXCEP_SYSCALL1, unsupported_syscall);
597 set_excp_vector(EXCEP_SYSCALL2, unsupported_syscall);
598 set_excp_vector(EXCEP_SYSCALL3, unsupported_syscall);
599 set_excp_vector(EXCEP_SYSCALL4, unsupported_syscall);
600 set_excp_vector(EXCEP_SYSCALL5, unsupported_syscall);
601 set_excp_vector(EXCEP_SYSCALL6, unsupported_syscall);
602 set_excp_vector(EXCEP_SYSCALL7, unsupported_syscall);
603 set_excp_vector(EXCEP_SYSCALL8, unsupported_syscall);
604 set_excp_vector(EXCEP_SYSCALL9, unsupported_syscall);
605 set_excp_vector(EXCEP_SYSCALL10, unsupported_syscall);
606 set_excp_vector(EXCEP_SYSCALL11, unsupported_syscall);
607 set_excp_vector(EXCEP_SYSCALL12, unsupported_syscall);
608 set_excp_vector(EXCEP_SYSCALL13, unsupported_syscall);
609 set_excp_vector(EXCEP_SYSCALL14, unsupported_syscall);
610 set_excp_vector(EXCEP_SYSCALL15, unsupported_syscall);
614 * determine if a program counter value is a valid bug address
616 int is_valid_bugaddr(unsigned long pc)
618 return pc >= PAGE_OFFSET;