2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
7 * Copyright (C) 1995, 1996 Paul M. Antoine
8 * Copyright (C) 1998 Ulf Carlsson
9 * Copyright (C) 1999 Silicon Graphics, Inc.
10 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11 * Copyright (C) 2000, 01 MIPS Technologies, Inc.
12 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
14 #include <linux/bug.h>
15 #include <linux/compiler.h>
16 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/smp.h>
21 #include <linux/spinlock.h>
22 #include <linux/kallsyms.h>
23 #include <linux/bootmem.h>
24 #include <linux/interrupt.h>
25 #include <linux/ptrace.h>
26 #include <linux/kgdb.h>
27 #include <linux/kdebug.h>
29 #include <asm/bootinfo.h>
30 #include <asm/branch.h>
31 #include <asm/break.h>
35 #include <asm/fpu_emulator.h>
36 #include <asm/mipsregs.h>
37 #include <asm/mipsmtregs.h>
38 #include <asm/module.h>
39 #include <asm/pgtable.h>
40 #include <asm/ptrace.h>
41 #include <asm/sections.h>
42 #include <asm/system.h>
43 #include <asm/tlbdebug.h>
44 #include <asm/traps.h>
45 #include <asm/uaccess.h>
46 #include <asm/watch.h>
47 #include <asm/mmu_context.h>
48 #include <asm/types.h>
49 #include <asm/stacktrace.h>
52 extern void check_wait(void);
53 extern asmlinkage void r4k_wait(void);
54 extern asmlinkage void rollback_handle_int(void);
55 extern asmlinkage void handle_int(void);
56 extern asmlinkage void handle_tlbm(void);
57 extern asmlinkage void handle_tlbl(void);
58 extern asmlinkage void handle_tlbs(void);
59 extern asmlinkage void handle_adel(void);
60 extern asmlinkage void handle_ades(void);
61 extern asmlinkage void handle_ibe(void);
62 extern asmlinkage void handle_dbe(void);
63 extern asmlinkage void handle_sys(void);
64 extern asmlinkage void handle_bp(void);
65 extern asmlinkage void handle_ri(void);
66 extern asmlinkage void handle_ri_rdhwr_vivt(void);
67 extern asmlinkage void handle_ri_rdhwr(void);
68 extern asmlinkage void handle_cpu(void);
69 extern asmlinkage void handle_ov(void);
70 extern asmlinkage void handle_tr(void);
71 extern asmlinkage void handle_fpe(void);
72 extern asmlinkage void handle_mdmx(void);
73 extern asmlinkage void handle_watch(void);
74 extern asmlinkage void handle_mt(void);
75 extern asmlinkage void handle_dsp(void);
76 extern asmlinkage void handle_mcheck(void);
77 extern asmlinkage void handle_reserved(void);
79 extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
80 struct mips_fpu_struct *ctx, int has_fpu);
82 #ifdef CONFIG_CPU_CAVIUM_OCTEON
83 extern asmlinkage void octeon_cop2_restore(struct octeon_cop2_state *task);
86 void (*board_be_init)(void);
87 int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
88 void (*board_nmi_handler_setup)(void);
89 void (*board_ejtag_handler_setup)(void);
90 void (*board_bind_eic_interrupt)(int irq, int regset);
93 static void show_raw_backtrace(unsigned long reg29)
95 unsigned long *sp = (unsigned long *)(reg29 & ~3);
98 printk("Call Trace:");
99 #ifdef CONFIG_KALLSYMS
102 while (!kstack_end(sp)) {
103 unsigned long __user *p =
104 (unsigned long __user *)(unsigned long)sp++;
105 if (__get_user(addr, p)) {
106 printk(" (Bad stack address)");
109 if (__kernel_text_address(addr))
115 #ifdef CONFIG_KALLSYMS
117 static int __init set_raw_show_trace(char *str)
122 __setup("raw_show_trace", set_raw_show_trace);
125 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
127 unsigned long sp = regs->regs[29];
128 unsigned long ra = regs->regs[31];
129 unsigned long pc = regs->cp0_epc;
131 if (raw_show_trace || !__kernel_text_address(pc)) {
132 show_raw_backtrace(sp);
135 printk("Call Trace:\n");
138 pc = unwind_stack(task, &sp, pc, &ra);
144 * This routine abuses get_user()/put_user() to reference pointers
145 * with at least a bit of error checking ...
147 static void show_stacktrace(struct task_struct *task,
148 const struct pt_regs *regs)
150 const int field = 2 * sizeof(unsigned long);
153 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
157 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
158 if (i && ((i % (64 / field)) == 0))
165 if (__get_user(stackdata, sp++)) {
166 printk(" (Bad stack address)");
170 printk(" %0*lx", field, stackdata);
174 show_backtrace(task, regs);
177 void show_stack(struct task_struct *task, unsigned long *sp)
181 regs.regs[29] = (unsigned long)sp;
185 if (task && task != current) {
186 regs.regs[29] = task->thread.reg29;
188 regs.cp0_epc = task->thread.reg31;
190 prepare_frametrace(®s);
193 show_stacktrace(task, ®s);
197 * The architecture-independent dump_stack generator
199 void dump_stack(void)
203 prepare_frametrace(®s);
204 show_backtrace(current, ®s);
207 EXPORT_SYMBOL(dump_stack);
209 static void show_code(unsigned int __user *pc)
212 unsigned short __user *pc16 = NULL;
216 if ((unsigned long)pc & 1)
217 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
218 for(i = -3 ; i < 6 ; i++) {
220 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
221 printk(" (Bad address in epc)\n");
224 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
228 static void __show_regs(const struct pt_regs *regs)
230 const int field = 2 * sizeof(unsigned long);
231 unsigned int cause = regs->cp0_cause;
234 printk("Cpu %d\n", smp_processor_id());
237 * Saved main processor registers
239 for (i = 0; i < 32; ) {
243 printk(" %0*lx", field, 0UL);
244 else if (i == 26 || i == 27)
245 printk(" %*s", field, "");
247 printk(" %0*lx", field, regs->regs[i]);
254 #ifdef CONFIG_CPU_HAS_SMARTMIPS
255 printk("Acx : %0*lx\n", field, regs->acx);
257 printk("Hi : %0*lx\n", field, regs->hi);
258 printk("Lo : %0*lx\n", field, regs->lo);
261 * Saved cp0 registers
263 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
264 (void *) regs->cp0_epc);
265 printk(" %s\n", print_tainted());
266 printk("ra : %0*lx %pS\n", field, regs->regs[31],
267 (void *) regs->regs[31]);
269 printk("Status: %08x ", (uint32_t) regs->cp0_status);
271 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
272 if (regs->cp0_status & ST0_KUO)
274 if (regs->cp0_status & ST0_IEO)
276 if (regs->cp0_status & ST0_KUP)
278 if (regs->cp0_status & ST0_IEP)
280 if (regs->cp0_status & ST0_KUC)
282 if (regs->cp0_status & ST0_IEC)
285 if (regs->cp0_status & ST0_KX)
287 if (regs->cp0_status & ST0_SX)
289 if (regs->cp0_status & ST0_UX)
291 switch (regs->cp0_status & ST0_KSU) {
296 printk("SUPERVISOR ");
305 if (regs->cp0_status & ST0_ERL)
307 if (regs->cp0_status & ST0_EXL)
309 if (regs->cp0_status & ST0_IE)
314 printk("Cause : %08x\n", cause);
316 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
317 if (1 <= cause && cause <= 5)
318 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
320 printk("PrId : %08x (%s)\n", read_c0_prid(),
325 * FIXME: really the generic show_regs should take a const pointer argument.
327 void show_regs(struct pt_regs *regs)
329 __show_regs((struct pt_regs *)regs);
332 void show_registers(const struct pt_regs *regs)
334 const int field = 2 * sizeof(unsigned long);
338 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
339 current->comm, current->pid, current_thread_info(), current,
340 field, current_thread_info()->tp_value);
341 if (cpu_has_userlocal) {
344 tls = read_c0_userlocal();
345 if (tls != current_thread_info()->tp_value)
346 printk("*HwTLS: %0*lx\n", field, tls);
349 show_stacktrace(current, regs);
350 show_code((unsigned int __user *) regs->cp0_epc);
354 static DEFINE_SPINLOCK(die_lock);
356 void __noreturn die(const char * str, const struct pt_regs * regs)
358 static int die_counter;
359 #ifdef CONFIG_MIPS_MT_SMTC
360 unsigned long dvpret = dvpe();
361 #endif /* CONFIG_MIPS_MT_SMTC */
364 spin_lock_irq(&die_lock);
366 #ifdef CONFIG_MIPS_MT_SMTC
367 mips_mt_regdump(dvpret);
368 #endif /* CONFIG_MIPS_MT_SMTC */
369 printk("%s[#%d]:\n", str, ++die_counter);
370 show_registers(regs);
371 add_taint(TAINT_DIE);
372 spin_unlock_irq(&die_lock);
375 panic("Fatal exception in interrupt");
378 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
380 panic("Fatal exception");
386 extern struct exception_table_entry __start___dbe_table[];
387 extern struct exception_table_entry __stop___dbe_table[];
390 " .section __dbe_table, \"a\"\n"
393 /* Given an address, look for it in the exception tables. */
394 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
396 const struct exception_table_entry *e;
398 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
400 e = search_module_dbetables(addr);
404 asmlinkage void do_be(struct pt_regs *regs)
406 const int field = 2 * sizeof(unsigned long);
407 const struct exception_table_entry *fixup = NULL;
408 int data = regs->cp0_cause & 4;
409 int action = MIPS_BE_FATAL;
411 /* XXX For now. Fixme, this searches the wrong table ... */
412 if (data && !user_mode(regs))
413 fixup = search_dbe_tables(exception_epc(regs));
416 action = MIPS_BE_FIXUP;
418 if (board_be_handler)
419 action = board_be_handler(regs, fixup != NULL);
422 case MIPS_BE_DISCARD:
426 regs->cp0_epc = fixup->nextinsn;
435 * Assume it would be too dangerous to continue ...
437 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
438 data ? "Data" : "Instruction",
439 field, regs->cp0_epc, field, regs->regs[31]);
440 if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
444 die_if_kernel("Oops", regs);
445 force_sig(SIGBUS, current);
449 * ll/sc, rdhwr, sync emulation
452 #define OPCODE 0xfc000000
453 #define BASE 0x03e00000
454 #define RT 0x001f0000
455 #define OFFSET 0x0000ffff
456 #define LL 0xc0000000
457 #define SC 0xe0000000
458 #define SPEC0 0x00000000
459 #define SPEC3 0x7c000000
460 #define RD 0x0000f800
461 #define FUNC 0x0000003f
462 #define SYNC 0x0000000f
463 #define RDHWR 0x0000003b
466 * The ll_bit is cleared by r*_switch.S
469 unsigned long ll_bit;
471 static struct task_struct *ll_task = NULL;
473 static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
475 unsigned long value, __user *vaddr;
479 * analyse the ll instruction that just caused a ri exception
480 * and put the referenced address to addr.
483 /* sign extend offset */
484 offset = opcode & OFFSET;
488 vaddr = (unsigned long __user *)
489 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
491 if ((unsigned long)vaddr & 3)
493 if (get_user(value, vaddr))
498 if (ll_task == NULL || ll_task == current) {
507 regs->regs[(opcode & RT) >> 16] = value;
512 static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
514 unsigned long __user *vaddr;
519 * analyse the sc instruction that just caused a ri exception
520 * and put the referenced address to addr.
523 /* sign extend offset */
524 offset = opcode & OFFSET;
528 vaddr = (unsigned long __user *)
529 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
530 reg = (opcode & RT) >> 16;
532 if ((unsigned long)vaddr & 3)
537 if (ll_bit == 0 || ll_task != current) {
545 if (put_user(regs->regs[reg], vaddr))
554 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
555 * opcodes are supposed to result in coprocessor unusable exceptions if
556 * executed on ll/sc-less processors. That's the theory. In practice a
557 * few processors such as NEC's VR4100 throw reserved instruction exceptions
558 * instead, so we're doing the emulation thing in both exception handlers.
560 static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
562 if ((opcode & OPCODE) == LL)
563 return simulate_ll(regs, opcode);
564 if ((opcode & OPCODE) == SC)
565 return simulate_sc(regs, opcode);
567 return -1; /* Must be something else ... */
571 * Simulate trapping 'rdhwr' instructions to provide user accessible
572 * registers not implemented in hardware.
574 static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
576 struct thread_info *ti = task_thread_info(current);
578 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
579 int rd = (opcode & RD) >> 11;
580 int rt = (opcode & RT) >> 16;
582 case 0: /* CPU number */
583 regs->regs[rt] = smp_processor_id();
585 case 1: /* SYNCI length */
586 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
587 current_cpu_data.icache.linesz);
589 case 2: /* Read count register */
590 regs->regs[rt] = read_c0_count();
592 case 3: /* Count register resolution */
593 switch (current_cpu_data.cputype) {
603 regs->regs[rt] = ti->tp_value;
614 static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
616 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
619 return -1; /* Must be something else ... */
622 asmlinkage void do_ov(struct pt_regs *regs)
626 die_if_kernel("Integer overflow", regs);
628 info.si_code = FPE_INTOVF;
629 info.si_signo = SIGFPE;
631 info.si_addr = (void __user *) regs->cp0_epc;
632 force_sig_info(SIGFPE, &info, current);
636 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
638 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
642 if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
645 die_if_kernel("FP exception in kernel code", regs);
647 if (fcr31 & FPU_CSR_UNI_X) {
651 * Unimplemented operation exception. If we've got the full
652 * software emulator on-board, let's use it...
654 * Force FPU to dump state into task/thread context. We're
655 * moving a lot of data here for what is probably a single
656 * instruction, but the alternative is to pre-decode the FP
657 * register operands before invoking the emulator, which seems
658 * a bit extreme for what should be an infrequent event.
660 /* Ensure 'resume' not overwrite saved fp context again. */
663 /* Run the emulator */
664 sig = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1);
667 * We can't allow the emulated instruction to leave any of
668 * the cause bit set in $fcr31.
670 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
672 /* Restore the hardware register state */
673 own_fpu(1); /* Using the FPU again. */
675 /* If something went wrong, signal */
677 force_sig(sig, current);
680 } else if (fcr31 & FPU_CSR_INV_X)
681 info.si_code = FPE_FLTINV;
682 else if (fcr31 & FPU_CSR_DIV_X)
683 info.si_code = FPE_FLTDIV;
684 else if (fcr31 & FPU_CSR_OVF_X)
685 info.si_code = FPE_FLTOVF;
686 else if (fcr31 & FPU_CSR_UDF_X)
687 info.si_code = FPE_FLTUND;
688 else if (fcr31 & FPU_CSR_INE_X)
689 info.si_code = FPE_FLTRES;
691 info.si_code = __SI_FAULT;
692 info.si_signo = SIGFPE;
694 info.si_addr = (void __user *) regs->cp0_epc;
695 force_sig_info(SIGFPE, &info, current);
698 static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
704 if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
708 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
709 * insns, even for trap and break codes that indicate arithmetic
710 * failures. Weird ...
711 * But should we continue the brokenness??? --macro
716 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
717 die_if_kernel(b, regs);
718 if (code == BRK_DIVZERO)
719 info.si_code = FPE_INTDIV;
721 info.si_code = FPE_INTOVF;
722 info.si_signo = SIGFPE;
724 info.si_addr = (void __user *) regs->cp0_epc;
725 force_sig_info(SIGFPE, &info, current);
728 die_if_kernel("Kernel bug detected", regs);
729 force_sig(SIGTRAP, current);
733 * Address errors may be deliberately induced by the FPU
734 * emulator to retake control of the CPU after executing the
735 * instruction in the delay slot of an emulated branch.
737 * Terminate if exception was recognized as a delay slot return
738 * otherwise handle as normal.
740 if (do_dsemulret(regs))
743 die_if_kernel("Math emu break/trap", regs);
744 force_sig(SIGTRAP, current);
747 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
748 die_if_kernel(b, regs);
749 force_sig(SIGTRAP, current);
753 asmlinkage void do_bp(struct pt_regs *regs)
755 unsigned int opcode, bcode;
757 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
761 * There is the ancient bug in the MIPS assemblers that the break
762 * code starts left to bit 16 instead to bit 6 in the opcode.
763 * Gas is bug-compatible, but not always, grrr...
764 * We handle both cases with a simple heuristics. --macro
766 bcode = ((opcode >> 6) & ((1 << 20) - 1));
767 if (bcode >= (1 << 10))
770 do_trap_or_bp(regs, bcode, "Break");
774 force_sig(SIGSEGV, current);
777 asmlinkage void do_tr(struct pt_regs *regs)
779 unsigned int opcode, tcode = 0;
781 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
784 /* Immediate versions don't provide a code. */
785 if (!(opcode & OPCODE))
786 tcode = ((opcode >> 6) & ((1 << 10) - 1));
788 do_trap_or_bp(regs, tcode, "Trap");
792 force_sig(SIGSEGV, current);
795 asmlinkage void do_ri(struct pt_regs *regs)
797 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
798 unsigned long old_epc = regs->cp0_epc;
799 unsigned int opcode = 0;
802 if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
806 die_if_kernel("Reserved instruction in kernel code", regs);
808 if (unlikely(compute_return_epc(regs) < 0))
811 if (unlikely(get_user(opcode, epc) < 0))
814 if (!cpu_has_llsc && status < 0)
815 status = simulate_llsc(regs, opcode);
818 status = simulate_rdhwr(regs, opcode);
821 status = simulate_sync(regs, opcode);
826 if (unlikely(status > 0)) {
827 regs->cp0_epc = old_epc; /* Undo skip-over. */
828 force_sig(status, current);
833 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
834 * emulated more than some threshold number of instructions, force migration to
835 * a "CPU" that has FP support.
837 static void mt_ase_fp_affinity(void)
839 #ifdef CONFIG_MIPS_MT_FPAFF
840 if (mt_fpemul_threshold > 0 &&
841 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
843 * If there's no FPU present, or if the application has already
844 * restricted the allowed set to exclude any CPUs with FPUs,
845 * we'll skip the procedure.
847 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
850 current->thread.user_cpus_allowed
851 = current->cpus_allowed;
852 cpus_and(tmask, current->cpus_allowed,
854 set_cpus_allowed(current, tmask);
855 set_thread_flag(TIF_FPUBOUND);
858 #endif /* CONFIG_MIPS_MT_FPAFF */
861 asmlinkage void do_cpu(struct pt_regs *regs)
863 unsigned int __user *epc;
864 unsigned long old_epc;
868 unsigned long __maybe_unused flags;
870 die_if_kernel("do_cpu invoked from kernel context!", regs);
872 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
876 epc = (unsigned int __user *)exception_epc(regs);
877 old_epc = regs->cp0_epc;
881 if (unlikely(compute_return_epc(regs) < 0))
884 if (unlikely(get_user(opcode, epc) < 0))
887 if (!cpu_has_llsc && status < 0)
888 status = simulate_llsc(regs, opcode);
891 status = simulate_rdhwr(regs, opcode);
896 if (unlikely(status > 0)) {
897 regs->cp0_epc = old_epc; /* Undo skip-over. */
898 force_sig(status, current);
904 if (used_math()) /* Using the FPU again. */
906 else { /* First time FPU user. */
911 if (!raw_cpu_has_fpu) {
913 sig = fpu_emulator_cop1Handler(regs,
914 ¤t->thread.fpu, 0);
916 force_sig(sig, current);
918 mt_ase_fp_affinity();
924 #ifdef CONFIG_CPU_CAVIUM_OCTEON
925 prefetch(¤t->thread.cp2);
926 local_irq_save(flags);
927 KSTK_STATUS(current) |= ST0_CU2;
928 status = read_c0_status();
929 write_c0_status(status | ST0_CU2);
930 octeon_cop2_restore(&(current->thread.cp2));
931 write_c0_status(status & ~ST0_CU2);
932 local_irq_restore(flags);
939 force_sig(SIGILL, current);
942 asmlinkage void do_mdmx(struct pt_regs *regs)
944 force_sig(SIGILL, current);
947 asmlinkage void do_watch(struct pt_regs *regs)
952 * Clear WP (bit 22) bit of cause register so we don't loop
955 cause = read_c0_cause();
957 write_c0_cause(cause);
960 * If the current thread has the watch registers loaded, save
961 * their values and send SIGTRAP. Otherwise another thread
962 * left the registers set, clear them and continue.
964 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
965 mips_read_watch_registers();
966 force_sig(SIGTRAP, current);
968 mips_clear_watch_registers();
971 asmlinkage void do_mcheck(struct pt_regs *regs)
973 const int field = 2 * sizeof(unsigned long);
974 int multi_match = regs->cp0_status & ST0_TS;
979 printk("Index : %0x\n", read_c0_index());
980 printk("Pagemask: %0x\n", read_c0_pagemask());
981 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
982 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
983 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
988 show_code((unsigned int __user *) regs->cp0_epc);
991 * Some chips may have other causes of machine check (e.g. SB1
994 panic("Caught Machine Check exception - %scaused by multiple "
995 "matching entries in the TLB.",
996 (multi_match) ? "" : "not ");
999 asmlinkage void do_mt(struct pt_regs *regs)
1003 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1004 >> VPECONTROL_EXCPT_SHIFT;
1007 printk(KERN_DEBUG "Thread Underflow\n");
1010 printk(KERN_DEBUG "Thread Overflow\n");
1013 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
1016 printk(KERN_DEBUG "Gating Storage Exception\n");
1019 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
1022 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
1025 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
1029 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1031 force_sig(SIGILL, current);
1035 asmlinkage void do_dsp(struct pt_regs *regs)
1038 panic("Unexpected DSP exception\n");
1040 force_sig(SIGILL, current);
1043 asmlinkage void do_reserved(struct pt_regs *regs)
1046 * Game over - no way to handle this if it ever occurs. Most probably
1047 * caused by a new unknown cpu type or after another deadly
1048 * hard/software error.
1051 panic("Caught reserved exception %ld - should not happen.",
1052 (regs->cp0_cause & 0x7f) >> 2);
1055 static int __initdata l1parity = 1;
1056 static int __init nol1parity(char *s)
1061 __setup("nol1par", nol1parity);
1062 static int __initdata l2parity = 1;
1063 static int __init nol2parity(char *s)
1068 __setup("nol2par", nol2parity);
1071 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1072 * it different ways.
1074 static inline void parity_protection_init(void)
1076 switch (current_cpu_type()) {
1082 #define ERRCTL_PE 0x80000000
1083 #define ERRCTL_L2P 0x00800000
1084 unsigned long errctl;
1085 unsigned int l1parity_present, l2parity_present;
1087 errctl = read_c0_ecc();
1088 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1090 /* probe L1 parity support */
1091 write_c0_ecc(errctl | ERRCTL_PE);
1092 back_to_back_c0_hazard();
1093 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1095 /* probe L2 parity support */
1096 write_c0_ecc(errctl|ERRCTL_L2P);
1097 back_to_back_c0_hazard();
1098 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1100 if (l1parity_present && l2parity_present) {
1102 errctl |= ERRCTL_PE;
1103 if (l1parity ^ l2parity)
1104 errctl |= ERRCTL_L2P;
1105 } else if (l1parity_present) {
1107 errctl |= ERRCTL_PE;
1108 } else if (l2parity_present) {
1110 errctl |= ERRCTL_L2P;
1112 /* No parity available */
1115 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1117 write_c0_ecc(errctl);
1118 back_to_back_c0_hazard();
1119 errctl = read_c0_ecc();
1120 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1122 if (l1parity_present)
1123 printk(KERN_INFO "Cache parity protection %sabled\n",
1124 (errctl & ERRCTL_PE) ? "en" : "dis");
1126 if (l2parity_present) {
1127 if (l1parity_present && l1parity)
1128 errctl ^= ERRCTL_L2P;
1129 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1130 (errctl & ERRCTL_L2P) ? "en" : "dis");
1136 write_c0_ecc(0x80000000);
1137 back_to_back_c0_hazard();
1138 /* Set the PE bit (bit 31) in the c0_errctl register. */
1139 printk(KERN_INFO "Cache parity protection %sabled\n",
1140 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
1144 /* Clear the DE bit (bit 16) in the c0_status register. */
1145 printk(KERN_INFO "Enable cache parity protection for "
1146 "MIPS 20KC/25KF CPUs.\n");
1147 clear_c0_status(ST0_DE);
1154 asmlinkage void cache_parity_error(void)
1156 const int field = 2 * sizeof(unsigned long);
1157 unsigned int reg_val;
1159 /* For the moment, report the problem and hang. */
1160 printk("Cache error exception:\n");
1161 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1162 reg_val = read_c0_cacheerr();
1163 printk("c0_cacheerr == %08x\n", reg_val);
1165 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1166 reg_val & (1<<30) ? "secondary" : "primary",
1167 reg_val & (1<<31) ? "data" : "insn");
1168 printk("Error bits: %s%s%s%s%s%s%s\n",
1169 reg_val & (1<<29) ? "ED " : "",
1170 reg_val & (1<<28) ? "ET " : "",
1171 reg_val & (1<<26) ? "EE " : "",
1172 reg_val & (1<<25) ? "EB " : "",
1173 reg_val & (1<<24) ? "EI " : "",
1174 reg_val & (1<<23) ? "E1 " : "",
1175 reg_val & (1<<22) ? "E0 " : "");
1176 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1178 #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
1179 if (reg_val & (1<<22))
1180 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1182 if (reg_val & (1<<23))
1183 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1186 panic("Can't handle the cache error!");
1190 * SDBBP EJTAG debug exception handler.
1191 * We skip the instruction and return to the next instruction.
1193 void ejtag_exception_handler(struct pt_regs *regs)
1195 const int field = 2 * sizeof(unsigned long);
1196 unsigned long depc, old_epc;
1199 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
1200 depc = read_c0_depc();
1201 debug = read_c0_debug();
1202 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
1203 if (debug & 0x80000000) {
1205 * In branch delay slot.
1206 * We cheat a little bit here and use EPC to calculate the
1207 * debug return address (DEPC). EPC is restored after the
1210 old_epc = regs->cp0_epc;
1211 regs->cp0_epc = depc;
1212 __compute_return_epc(regs);
1213 depc = regs->cp0_epc;
1214 regs->cp0_epc = old_epc;
1217 write_c0_depc(depc);
1220 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
1221 write_c0_debug(debug | 0x100);
1226 * NMI exception handler.
1228 NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
1231 printk("NMI taken!!!!\n");
1235 #define VECTORSPACING 0x100 /* for EI/VI mode */
1237 unsigned long ebase;
1238 unsigned long exception_handlers[32];
1239 unsigned long vi_handlers[64];
1242 * As a side effect of the way this is implemented we're limited
1243 * to interrupt handlers in the address range from
1244 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1246 void *set_except_vector(int n, void *addr)
1248 unsigned long handler = (unsigned long) addr;
1249 unsigned long old_handler = exception_handlers[n];
1251 exception_handlers[n] = handler;
1252 if (n == 0 && cpu_has_divec) {
1253 *(u32 *)(ebase + 0x200) = 0x08000000 |
1254 (0x03ffffff & (handler >> 2));
1255 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
1257 return (void *)old_handler;
1260 static asmlinkage void do_default_vi(void)
1262 show_regs(get_irq_regs());
1263 panic("Caught unexpected vectored interrupt.");
1266 static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
1268 unsigned long handler;
1269 unsigned long old_handler = vi_handlers[n];
1270 int srssets = current_cpu_data.srsets;
1274 if (!cpu_has_veic && !cpu_has_vint)
1278 handler = (unsigned long) do_default_vi;
1281 handler = (unsigned long) addr;
1282 vi_handlers[n] = (unsigned long) addr;
1284 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1287 panic("Shadow register set %d not supported", srs);
1290 if (board_bind_eic_interrupt)
1291 board_bind_eic_interrupt(n, srs);
1292 } else if (cpu_has_vint) {
1293 /* SRSMap is only defined if shadow sets are implemented */
1295 change_c0_srsmap(0xf << n*4, srs << n*4);
1300 * If no shadow set is selected then use the default handler
1301 * that does normal register saving and a standard interrupt exit
1304 extern char except_vec_vi, except_vec_vi_lui;
1305 extern char except_vec_vi_ori, except_vec_vi_end;
1306 extern char rollback_except_vec_vi;
1307 char *vec_start = (cpu_wait == r4k_wait) ?
1308 &rollback_except_vec_vi : &except_vec_vi;
1309 #ifdef CONFIG_MIPS_MT_SMTC
1311 * We need to provide the SMTC vectored interrupt handler
1312 * not only with the address of the handler, but with the
1313 * Status.IM bit to be masked before going there.
1315 extern char except_vec_vi_mori;
1316 const int mori_offset = &except_vec_vi_mori - vec_start;
1317 #endif /* CONFIG_MIPS_MT_SMTC */
1318 const int handler_len = &except_vec_vi_end - vec_start;
1319 const int lui_offset = &except_vec_vi_lui - vec_start;
1320 const int ori_offset = &except_vec_vi_ori - vec_start;
1322 if (handler_len > VECTORSPACING) {
1324 * Sigh... panicing won't help as the console
1325 * is probably not configured :(
1327 panic("VECTORSPACING too small");
1330 memcpy(b, vec_start, handler_len);
1331 #ifdef CONFIG_MIPS_MT_SMTC
1332 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1334 w = (u32 *)(b + mori_offset);
1335 *w = (*w & 0xffff0000) | (0x100 << n);
1336 #endif /* CONFIG_MIPS_MT_SMTC */
1337 w = (u32 *)(b + lui_offset);
1338 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1339 w = (u32 *)(b + ori_offset);
1340 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1341 local_flush_icache_range((unsigned long)b,
1342 (unsigned long)(b+handler_len));
1346 * In other cases jump directly to the interrupt handler
1348 * It is the handlers responsibility to save registers if required
1349 * (eg hi/lo) and return from the exception using "eret"
1352 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1354 local_flush_icache_range((unsigned long)b,
1355 (unsigned long)(b+8));
1358 return (void *)old_handler;
1361 void *set_vi_handler(int n, vi_handler_t addr)
1363 return set_vi_srs_handler(n, addr, 0);
1367 * This is used by native signal handling
1369 asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1370 asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
1372 extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1373 extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
1375 extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1376 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
1379 static int smp_save_fp_context(struct sigcontext __user *sc)
1381 return raw_cpu_has_fpu
1382 ? _save_fp_context(sc)
1383 : fpu_emulator_save_context(sc);
1386 static int smp_restore_fp_context(struct sigcontext __user *sc)
1388 return raw_cpu_has_fpu
1389 ? _restore_fp_context(sc)
1390 : fpu_emulator_restore_context(sc);
1394 static inline void signal_init(void)
1397 /* For now just do the cpu_has_fpu check when the functions are invoked */
1398 save_fp_context = smp_save_fp_context;
1399 restore_fp_context = smp_restore_fp_context;
1402 save_fp_context = _save_fp_context;
1403 restore_fp_context = _restore_fp_context;
1405 save_fp_context = fpu_emulator_save_context;
1406 restore_fp_context = fpu_emulator_restore_context;
1411 #ifdef CONFIG_MIPS32_COMPAT
1414 * This is used by 32-bit signal stuff on the 64-bit kernel
1416 asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1417 asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
1419 extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1420 extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
1422 extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1423 extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
1425 static inline void signal32_init(void)
1428 save_fp_context32 = _save_fp_context32;
1429 restore_fp_context32 = _restore_fp_context32;
1431 save_fp_context32 = fpu_emulator_save_context32;
1432 restore_fp_context32 = fpu_emulator_restore_context32;
1437 extern void cpu_cache_init(void);
1438 extern void tlb_init(void);
1439 extern void flush_tlb_handlers(void);
1444 int cp0_compare_irq;
1447 * Performance counter IRQ or -1 if shared with timer
1449 int cp0_perfcount_irq;
1450 EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1452 static int __cpuinitdata noulri;
1454 static int __init ulri_disable(char *s)
1456 pr_info("Disabling ulri\n");
1461 __setup("noulri", ulri_disable);
1463 void __cpuinit per_cpu_trap_init(void)
1465 unsigned int cpu = smp_processor_id();
1466 unsigned int status_set = ST0_CU0;
1467 #ifdef CONFIG_MIPS_MT_SMTC
1468 int secondaryTC = 0;
1469 int bootTC = (cpu == 0);
1472 * Only do per_cpu_trap_init() for first TC of Each VPE.
1473 * Note that this hack assumes that the SMTC init code
1474 * assigns TCs consecutively and in ascending order.
1477 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1478 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1480 #endif /* CONFIG_MIPS_MT_SMTC */
1483 * Disable coprocessors and select 32-bit or 64-bit addressing
1484 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1485 * flag that some firmware may have left set and the TS bit (for
1486 * IP27). Set XX for ISA IV code to work.
1489 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1491 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1492 status_set |= ST0_XX;
1494 status_set |= ST0_MX;
1496 change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1499 if (cpu_has_mips_r2) {
1500 unsigned int enable = 0x0000000f;
1502 if (!noulri && cpu_has_userlocal)
1503 enable |= (1 << 29);
1505 write_c0_hwrena(enable);
1508 #ifdef CONFIG_CPU_CAVIUM_OCTEON
1509 write_c0_hwrena(0xc000000f); /* Octeon has register 30 and 31 */
1512 #ifdef CONFIG_MIPS_MT_SMTC
1514 #endif /* CONFIG_MIPS_MT_SMTC */
1516 if (cpu_has_veic || cpu_has_vint) {
1517 write_c0_ebase(ebase);
1518 /* Setting vector spacing enables EI/VI mode */
1519 change_c0_intctl(0x3e0, VECTORSPACING);
1521 if (cpu_has_divec) {
1522 if (cpu_has_mipsmt) {
1523 unsigned int vpflags = dvpe();
1524 set_c0_cause(CAUSEF_IV);
1527 set_c0_cause(CAUSEF_IV);
1531 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1533 * o read IntCtl.IPTI to determine the timer interrupt
1534 * o read IntCtl.IPPCI to determine the performance counter interrupt
1536 if (cpu_has_mips_r2) {
1537 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1538 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
1539 if (cp0_perfcount_irq == cp0_compare_irq)
1540 cp0_perfcount_irq = -1;
1542 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
1543 cp0_perfcount_irq = -1;
1546 #ifdef CONFIG_MIPS_MT_SMTC
1548 #endif /* CONFIG_MIPS_MT_SMTC */
1550 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1551 TLBMISS_HANDLER_SETUP();
1553 atomic_inc(&init_mm.mm_count);
1554 current->active_mm = &init_mm;
1555 BUG_ON(current->mm);
1556 enter_lazy_tlb(&init_mm, current);
1558 #ifdef CONFIG_MIPS_MT_SMTC
1560 #endif /* CONFIG_MIPS_MT_SMTC */
1563 #ifdef CONFIG_MIPS_MT_SMTC
1564 } else if (!secondaryTC) {
1566 * First TC in non-boot VPE must do subset of tlb_init()
1567 * for MMU countrol registers.
1569 write_c0_pagemask(PM_DEFAULT_MASK);
1572 #endif /* CONFIG_MIPS_MT_SMTC */
1575 /* Install CPU exception handler */
1576 void __init set_handler(unsigned long offset, void *addr, unsigned long size)
1578 memcpy((void *)(ebase + offset), addr, size);
1579 local_flush_icache_range(ebase + offset, ebase + offset + size);
1582 static char panic_null_cerr[] __cpuinitdata =
1583 "Trying to set NULL cache error exception handler";
1585 /* Install uncached CPU exception handler */
1586 void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1590 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1593 unsigned long uncached_ebase = TO_UNCAC(ebase);
1595 if (cpu_has_mips_r2)
1596 ebase += (read_c0_ebase() & 0x3ffff000);
1599 panic(panic_null_cerr);
1601 memcpy((void *)(uncached_ebase + offset), addr, size);
1604 static int __initdata rdhwr_noopt;
1605 static int __init set_rdhwr_noopt(char *str)
1611 __setup("rdhwr_noopt", set_rdhwr_noopt);
1613 void __init trap_init(void)
1615 extern char except_vec3_generic, except_vec3_r4000;
1616 extern char except_vec4;
1621 rollback = (cpu_wait == r4k_wait);
1623 #if defined(CONFIG_KGDB)
1624 if (kgdb_early_setup)
1625 return; /* Already done */
1628 if (cpu_has_veic || cpu_has_vint)
1629 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
1632 if (cpu_has_mips_r2)
1633 ebase += (read_c0_ebase() & 0x3ffff000);
1636 per_cpu_trap_init();
1639 * Copy the generic exception handlers to their final destination.
1640 * This will be overriden later as suitable for a particular
1643 set_handler(0x180, &except_vec3_generic, 0x80);
1646 * Setup default vectors
1648 for (i = 0; i <= 31; i++)
1649 set_except_vector(i, handle_reserved);
1652 * Copy the EJTAG debug exception vector handler code to it's final
1655 if (cpu_has_ejtag && board_ejtag_handler_setup)
1656 board_ejtag_handler_setup();
1659 * Only some CPUs have the watch exceptions.
1662 set_except_vector(23, handle_watch);
1665 * Initialise interrupt handlers
1667 if (cpu_has_veic || cpu_has_vint) {
1668 int nvec = cpu_has_veic ? 64 : 8;
1669 for (i = 0; i < nvec; i++)
1670 set_vi_handler(i, NULL);
1672 else if (cpu_has_divec)
1673 set_handler(0x200, &except_vec4, 0x8);
1676 * Some CPUs can enable/disable for cache parity detection, but does
1677 * it different ways.
1679 parity_protection_init();
1682 * The Data Bus Errors / Instruction Bus Errors are signaled
1683 * by external hardware. Therefore these two exceptions
1684 * may have board specific handlers.
1689 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
1690 set_except_vector(1, handle_tlbm);
1691 set_except_vector(2, handle_tlbl);
1692 set_except_vector(3, handle_tlbs);
1694 set_except_vector(4, handle_adel);
1695 set_except_vector(5, handle_ades);
1697 set_except_vector(6, handle_ibe);
1698 set_except_vector(7, handle_dbe);
1700 set_except_vector(8, handle_sys);
1701 set_except_vector(9, handle_bp);
1702 set_except_vector(10, rdhwr_noopt ? handle_ri :
1703 (cpu_has_vtag_icache ?
1704 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
1705 set_except_vector(11, handle_cpu);
1706 set_except_vector(12, handle_ov);
1707 set_except_vector(13, handle_tr);
1709 if (current_cpu_type() == CPU_R6000 ||
1710 current_cpu_type() == CPU_R6000A) {
1712 * The R6000 is the only R-series CPU that features a machine
1713 * check exception (similar to the R4000 cache error) and
1714 * unaligned ldc1/sdc1 exception. The handlers have not been
1715 * written yet. Well, anyway there is no R6000 machine on the
1716 * current list of targets for Linux/MIPS.
1717 * (Duh, crap, there is someone with a triple R6k machine)
1719 //set_except_vector(14, handle_mc);
1720 //set_except_vector(15, handle_ndc);
1724 if (board_nmi_handler_setup)
1725 board_nmi_handler_setup();
1727 if (cpu_has_fpu && !cpu_has_nofpuex)
1728 set_except_vector(15, handle_fpe);
1730 set_except_vector(22, handle_mdmx);
1733 set_except_vector(24, handle_mcheck);
1736 set_except_vector(25, handle_mt);
1738 set_except_vector(26, handle_dsp);
1741 /* Special exception: R4[04]00 uses also the divec space. */
1742 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
1743 else if (cpu_has_4kex)
1744 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
1746 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
1749 #ifdef CONFIG_MIPS32_COMPAT
1753 local_flush_icache_range(ebase, ebase + 0x400);
1754 flush_tlb_handlers();
1756 sort_extable(__start___dbe_table, __stop___dbe_table);