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>
27 #include <asm/bootinfo.h>
28 #include <asm/branch.h>
29 #include <asm/break.h>
33 #include <asm/mipsregs.h>
34 #include <asm/mipsmtregs.h>
35 #include <asm/module.h>
36 #include <asm/pgtable.h>
37 #include <asm/ptrace.h>
38 #include <asm/sections.h>
39 #include <asm/system.h>
40 #include <asm/tlbdebug.h>
41 #include <asm/traps.h>
42 #include <asm/uaccess.h>
43 #include <asm/mmu_context.h>
44 #include <asm/types.h>
45 #include <asm/stacktrace.h>
47 extern asmlinkage void handle_int(void);
48 extern asmlinkage void handle_tlbm(void);
49 extern asmlinkage void handle_tlbl(void);
50 extern asmlinkage void handle_tlbs(void);
51 extern asmlinkage void handle_adel(void);
52 extern asmlinkage void handle_ades(void);
53 extern asmlinkage void handle_ibe(void);
54 extern asmlinkage void handle_dbe(void);
55 extern asmlinkage void handle_sys(void);
56 extern asmlinkage void handle_bp(void);
57 extern asmlinkage void handle_ri(void);
58 extern asmlinkage void handle_ri_rdhwr_vivt(void);
59 extern asmlinkage void handle_ri_rdhwr(void);
60 extern asmlinkage void handle_cpu(void);
61 extern asmlinkage void handle_ov(void);
62 extern asmlinkage void handle_tr(void);
63 extern asmlinkage void handle_fpe(void);
64 extern asmlinkage void handle_mdmx(void);
65 extern asmlinkage void handle_watch(void);
66 extern asmlinkage void handle_mt(void);
67 extern asmlinkage void handle_dsp(void);
68 extern asmlinkage void handle_mcheck(void);
69 extern asmlinkage void handle_reserved(void);
71 extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
72 struct mips_fpu_struct *ctx, int has_fpu);
74 void (*board_be_init)(void);
75 int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
76 void (*board_nmi_handler_setup)(void);
77 void (*board_ejtag_handler_setup)(void);
78 void (*board_bind_eic_interrupt)(int irq, int regset);
81 static void show_raw_backtrace(unsigned long reg29)
83 unsigned long *sp = (unsigned long *)(reg29 & ~3);
86 printk("Call Trace:");
87 #ifdef CONFIG_KALLSYMS
90 while (!kstack_end(sp)) {
91 unsigned long __user *p =
92 (unsigned long __user *)(unsigned long)sp++;
93 if (__get_user(addr, p)) {
94 printk(" (Bad stack address)");
97 if (__kernel_text_address(addr))
103 #ifdef CONFIG_KALLSYMS
105 static int __init set_raw_show_trace(char *str)
110 __setup("raw_show_trace", set_raw_show_trace);
113 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
115 unsigned long sp = regs->regs[29];
116 unsigned long ra = regs->regs[31];
117 unsigned long pc = regs->cp0_epc;
119 if (raw_show_trace || !__kernel_text_address(pc)) {
120 show_raw_backtrace(sp);
123 printk("Call Trace:\n");
126 pc = unwind_stack(task, &sp, pc, &ra);
132 * This routine abuses get_user()/put_user() to reference pointers
133 * with at least a bit of error checking ...
135 static void show_stacktrace(struct task_struct *task,
136 const struct pt_regs *regs)
138 const int field = 2 * sizeof(unsigned long);
141 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
145 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
146 if (i && ((i % (64 / field)) == 0))
153 if (__get_user(stackdata, sp++)) {
154 printk(" (Bad stack address)");
158 printk(" %0*lx", field, stackdata);
162 show_backtrace(task, regs);
165 void show_stack(struct task_struct *task, unsigned long *sp)
169 regs.regs[29] = (unsigned long)sp;
173 if (task && task != current) {
174 regs.regs[29] = task->thread.reg29;
176 regs.cp0_epc = task->thread.reg31;
178 prepare_frametrace(®s);
181 show_stacktrace(task, ®s);
185 * The architecture-independent dump_stack generator
187 void dump_stack(void)
191 prepare_frametrace(®s);
192 show_backtrace(current, ®s);
195 EXPORT_SYMBOL(dump_stack);
197 static void show_code(unsigned int __user *pc)
200 unsigned short __user *pc16 = NULL;
204 if ((unsigned long)pc & 1)
205 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
206 for(i = -3 ; i < 6 ; i++) {
208 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
209 printk(" (Bad address in epc)\n");
212 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
216 static void __show_regs(const struct pt_regs *regs)
218 const int field = 2 * sizeof(unsigned long);
219 unsigned int cause = regs->cp0_cause;
222 printk("Cpu %d\n", smp_processor_id());
225 * Saved main processor registers
227 for (i = 0; i < 32; ) {
231 printk(" %0*lx", field, 0UL);
232 else if (i == 26 || i == 27)
233 printk(" %*s", field, "");
235 printk(" %0*lx", field, regs->regs[i]);
242 #ifdef CONFIG_CPU_HAS_SMARTMIPS
243 printk("Acx : %0*lx\n", field, regs->acx);
245 printk("Hi : %0*lx\n", field, regs->hi);
246 printk("Lo : %0*lx\n", field, regs->lo);
249 * Saved cp0 registers
251 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
252 (void *) regs->cp0_epc);
253 printk(" %s\n", print_tainted());
254 printk("ra : %0*lx %pS\n", field, regs->regs[31],
255 (void *) regs->regs[31]);
257 printk("Status: %08x ", (uint32_t) regs->cp0_status);
259 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
260 if (regs->cp0_status & ST0_KUO)
262 if (regs->cp0_status & ST0_IEO)
264 if (regs->cp0_status & ST0_KUP)
266 if (regs->cp0_status & ST0_IEP)
268 if (regs->cp0_status & ST0_KUC)
270 if (regs->cp0_status & ST0_IEC)
273 if (regs->cp0_status & ST0_KX)
275 if (regs->cp0_status & ST0_SX)
277 if (regs->cp0_status & ST0_UX)
279 switch (regs->cp0_status & ST0_KSU) {
284 printk("SUPERVISOR ");
293 if (regs->cp0_status & ST0_ERL)
295 if (regs->cp0_status & ST0_EXL)
297 if (regs->cp0_status & ST0_IE)
302 printk("Cause : %08x\n", cause);
304 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
305 if (1 <= cause && cause <= 5)
306 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
308 printk("PrId : %08x (%s)\n", read_c0_prid(),
313 * FIXME: really the generic show_regs should take a const pointer argument.
315 void show_regs(struct pt_regs *regs)
317 __show_regs((struct pt_regs *)regs);
320 void show_registers(const struct pt_regs *regs)
322 const int field = 2 * sizeof(unsigned long);
326 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
327 current->comm, current->pid, current_thread_info(), current,
328 field, current_thread_info()->tp_value);
329 if (cpu_has_userlocal) {
332 tls = read_c0_userlocal();
333 if (tls != current_thread_info()->tp_value)
334 printk("*HwTLS: %0*lx\n", field, tls);
337 show_stacktrace(current, regs);
338 show_code((unsigned int __user *) regs->cp0_epc);
342 static DEFINE_SPINLOCK(die_lock);
344 void __noreturn die(const char * str, const struct pt_regs * regs)
346 static int die_counter;
347 #ifdef CONFIG_MIPS_MT_SMTC
348 unsigned long dvpret = dvpe();
349 #endif /* CONFIG_MIPS_MT_SMTC */
352 spin_lock_irq(&die_lock);
354 #ifdef CONFIG_MIPS_MT_SMTC
355 mips_mt_regdump(dvpret);
356 #endif /* CONFIG_MIPS_MT_SMTC */
357 printk("%s[#%d]:\n", str, ++die_counter);
358 show_registers(regs);
359 add_taint(TAINT_DIE);
360 spin_unlock_irq(&die_lock);
363 panic("Fatal exception in interrupt");
366 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
368 panic("Fatal exception");
374 extern const struct exception_table_entry __start___dbe_table[];
375 extern const struct exception_table_entry __stop___dbe_table[];
378 " .section __dbe_table, \"a\"\n"
381 /* Given an address, look for it in the exception tables. */
382 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
384 const struct exception_table_entry *e;
386 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
388 e = search_module_dbetables(addr);
392 asmlinkage void do_be(struct pt_regs *regs)
394 const int field = 2 * sizeof(unsigned long);
395 const struct exception_table_entry *fixup = NULL;
396 int data = regs->cp0_cause & 4;
397 int action = MIPS_BE_FATAL;
399 /* XXX For now. Fixme, this searches the wrong table ... */
400 if (data && !user_mode(regs))
401 fixup = search_dbe_tables(exception_epc(regs));
404 action = MIPS_BE_FIXUP;
406 if (board_be_handler)
407 action = board_be_handler(regs, fixup != NULL);
410 case MIPS_BE_DISCARD:
414 regs->cp0_epc = fixup->nextinsn;
423 * Assume it would be too dangerous to continue ...
425 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
426 data ? "Data" : "Instruction",
427 field, regs->cp0_epc, field, regs->regs[31]);
428 die_if_kernel("Oops", regs);
429 force_sig(SIGBUS, current);
433 * ll/sc, rdhwr, sync emulation
436 #define OPCODE 0xfc000000
437 #define BASE 0x03e00000
438 #define RT 0x001f0000
439 #define OFFSET 0x0000ffff
440 #define LL 0xc0000000
441 #define SC 0xe0000000
442 #define SPEC0 0x00000000
443 #define SPEC3 0x7c000000
444 #define RD 0x0000f800
445 #define FUNC 0x0000003f
446 #define SYNC 0x0000000f
447 #define RDHWR 0x0000003b
450 * The ll_bit is cleared by r*_switch.S
453 unsigned long ll_bit;
455 static struct task_struct *ll_task = NULL;
457 static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
459 unsigned long value, __user *vaddr;
463 * analyse the ll instruction that just caused a ri exception
464 * and put the referenced address to addr.
467 /* sign extend offset */
468 offset = opcode & OFFSET;
472 vaddr = (unsigned long __user *)
473 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
475 if ((unsigned long)vaddr & 3)
477 if (get_user(value, vaddr))
482 if (ll_task == NULL || ll_task == current) {
491 regs->regs[(opcode & RT) >> 16] = value;
496 static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
498 unsigned long __user *vaddr;
503 * analyse the sc instruction that just caused a ri exception
504 * and put the referenced address to addr.
507 /* sign extend offset */
508 offset = opcode & OFFSET;
512 vaddr = (unsigned long __user *)
513 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
514 reg = (opcode & RT) >> 16;
516 if ((unsigned long)vaddr & 3)
521 if (ll_bit == 0 || ll_task != current) {
529 if (put_user(regs->regs[reg], vaddr))
538 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
539 * opcodes are supposed to result in coprocessor unusable exceptions if
540 * executed on ll/sc-less processors. That's the theory. In practice a
541 * few processors such as NEC's VR4100 throw reserved instruction exceptions
542 * instead, so we're doing the emulation thing in both exception handlers.
544 static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
546 if ((opcode & OPCODE) == LL)
547 return simulate_ll(regs, opcode);
548 if ((opcode & OPCODE) == SC)
549 return simulate_sc(regs, opcode);
551 return -1; /* Must be something else ... */
555 * Simulate trapping 'rdhwr' instructions to provide user accessible
556 * registers not implemented in hardware.
558 static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
560 struct thread_info *ti = task_thread_info(current);
562 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
563 int rd = (opcode & RD) >> 11;
564 int rt = (opcode & RT) >> 16;
566 case 0: /* CPU number */
567 regs->regs[rt] = smp_processor_id();
569 case 1: /* SYNCI length */
570 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
571 current_cpu_data.icache.linesz);
573 case 2: /* Read count register */
574 regs->regs[rt] = read_c0_count();
576 case 3: /* Count register resolution */
577 switch (current_cpu_data.cputype) {
587 regs->regs[rt] = ti->tp_value;
598 static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
600 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
603 return -1; /* Must be something else ... */
606 asmlinkage void do_ov(struct pt_regs *regs)
610 die_if_kernel("Integer overflow", regs);
612 info.si_code = FPE_INTOVF;
613 info.si_signo = SIGFPE;
615 info.si_addr = (void __user *) regs->cp0_epc;
616 force_sig_info(SIGFPE, &info, current);
620 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
622 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
626 die_if_kernel("FP exception in kernel code", regs);
628 if (fcr31 & FPU_CSR_UNI_X) {
632 * Unimplemented operation exception. If we've got the full
633 * software emulator on-board, let's use it...
635 * Force FPU to dump state into task/thread context. We're
636 * moving a lot of data here for what is probably a single
637 * instruction, but the alternative is to pre-decode the FP
638 * register operands before invoking the emulator, which seems
639 * a bit extreme for what should be an infrequent event.
641 /* Ensure 'resume' not overwrite saved fp context again. */
644 /* Run the emulator */
645 sig = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 1);
648 * We can't allow the emulated instruction to leave any of
649 * the cause bit set in $fcr31.
651 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
653 /* Restore the hardware register state */
654 own_fpu(1); /* Using the FPU again. */
656 /* If something went wrong, signal */
658 force_sig(sig, current);
661 } else if (fcr31 & FPU_CSR_INV_X)
662 info.si_code = FPE_FLTINV;
663 else if (fcr31 & FPU_CSR_DIV_X)
664 info.si_code = FPE_FLTDIV;
665 else if (fcr31 & FPU_CSR_OVF_X)
666 info.si_code = FPE_FLTOVF;
667 else if (fcr31 & FPU_CSR_UDF_X)
668 info.si_code = FPE_FLTUND;
669 else if (fcr31 & FPU_CSR_INE_X)
670 info.si_code = FPE_FLTRES;
672 info.si_code = __SI_FAULT;
673 info.si_signo = SIGFPE;
675 info.si_addr = (void __user *) regs->cp0_epc;
676 force_sig_info(SIGFPE, &info, current);
679 static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
686 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
687 * insns, even for trap and break codes that indicate arithmetic
688 * failures. Weird ...
689 * But should we continue the brokenness??? --macro
694 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
695 die_if_kernel(b, regs);
696 if (code == BRK_DIVZERO)
697 info.si_code = FPE_INTDIV;
699 info.si_code = FPE_INTOVF;
700 info.si_signo = SIGFPE;
702 info.si_addr = (void __user *) regs->cp0_epc;
703 force_sig_info(SIGFPE, &info, current);
706 die_if_kernel("Kernel bug detected", regs);
707 force_sig(SIGTRAP, current);
710 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
711 die_if_kernel(b, regs);
712 force_sig(SIGTRAP, current);
716 asmlinkage void do_bp(struct pt_regs *regs)
718 unsigned int opcode, bcode;
720 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
724 * There is the ancient bug in the MIPS assemblers that the break
725 * code starts left to bit 16 instead to bit 6 in the opcode.
726 * Gas is bug-compatible, but not always, grrr...
727 * We handle both cases with a simple heuristics. --macro
729 bcode = ((opcode >> 6) & ((1 << 20) - 1));
730 if (bcode >= (1 << 10))
733 do_trap_or_bp(regs, bcode, "Break");
737 force_sig(SIGSEGV, current);
740 asmlinkage void do_tr(struct pt_regs *regs)
742 unsigned int opcode, tcode = 0;
744 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
747 /* Immediate versions don't provide a code. */
748 if (!(opcode & OPCODE))
749 tcode = ((opcode >> 6) & ((1 << 10) - 1));
751 do_trap_or_bp(regs, tcode, "Trap");
755 force_sig(SIGSEGV, current);
758 asmlinkage void do_ri(struct pt_regs *regs)
760 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
761 unsigned long old_epc = regs->cp0_epc;
762 unsigned int opcode = 0;
765 die_if_kernel("Reserved instruction in kernel code", regs);
767 if (unlikely(compute_return_epc(regs) < 0))
770 if (unlikely(get_user(opcode, epc) < 0))
773 if (!cpu_has_llsc && status < 0)
774 status = simulate_llsc(regs, opcode);
777 status = simulate_rdhwr(regs, opcode);
780 status = simulate_sync(regs, opcode);
785 if (unlikely(status > 0)) {
786 regs->cp0_epc = old_epc; /* Undo skip-over. */
787 force_sig(status, current);
792 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
793 * emulated more than some threshold number of instructions, force migration to
794 * a "CPU" that has FP support.
796 static void mt_ase_fp_affinity(void)
798 #ifdef CONFIG_MIPS_MT_FPAFF
799 if (mt_fpemul_threshold > 0 &&
800 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
802 * If there's no FPU present, or if the application has already
803 * restricted the allowed set to exclude any CPUs with FPUs,
804 * we'll skip the procedure.
806 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
809 cpus_and(tmask, current->thread.user_cpus_allowed,
811 set_cpus_allowed(current, tmask);
812 set_thread_flag(TIF_FPUBOUND);
815 #endif /* CONFIG_MIPS_MT_FPAFF */
818 asmlinkage void do_cpu(struct pt_regs *regs)
820 unsigned int __user *epc;
821 unsigned long old_epc;
826 die_if_kernel("do_cpu invoked from kernel context!", regs);
828 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
832 epc = (unsigned int __user *)exception_epc(regs);
833 old_epc = regs->cp0_epc;
837 if (unlikely(compute_return_epc(regs) < 0))
840 if (unlikely(get_user(opcode, epc) < 0))
843 if (!cpu_has_llsc && status < 0)
844 status = simulate_llsc(regs, opcode);
847 status = simulate_rdhwr(regs, opcode);
852 if (unlikely(status > 0)) {
853 regs->cp0_epc = old_epc; /* Undo skip-over. */
854 force_sig(status, current);
860 if (used_math()) /* Using the FPU again. */
862 else { /* First time FPU user. */
867 if (!raw_cpu_has_fpu) {
869 sig = fpu_emulator_cop1Handler(regs,
870 ¤t->thread.fpu, 0);
872 force_sig(sig, current);
874 mt_ase_fp_affinity();
884 force_sig(SIGILL, current);
887 asmlinkage void do_mdmx(struct pt_regs *regs)
889 force_sig(SIGILL, current);
892 asmlinkage void do_watch(struct pt_regs *regs)
895 * We use the watch exception where available to detect stack
900 panic("Caught WATCH exception - probably caused by stack overflow.");
903 asmlinkage void do_mcheck(struct pt_regs *regs)
905 const int field = 2 * sizeof(unsigned long);
906 int multi_match = regs->cp0_status & ST0_TS;
911 printk("Index : %0x\n", read_c0_index());
912 printk("Pagemask: %0x\n", read_c0_pagemask());
913 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
914 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
915 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
920 show_code((unsigned int __user *) regs->cp0_epc);
923 * Some chips may have other causes of machine check (e.g. SB1
926 panic("Caught Machine Check exception - %scaused by multiple "
927 "matching entries in the TLB.",
928 (multi_match) ? "" : "not ");
931 asmlinkage void do_mt(struct pt_regs *regs)
935 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
936 >> VPECONTROL_EXCPT_SHIFT;
939 printk(KERN_DEBUG "Thread Underflow\n");
942 printk(KERN_DEBUG "Thread Overflow\n");
945 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
948 printk(KERN_DEBUG "Gating Storage Exception\n");
951 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
954 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
957 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
961 die_if_kernel("MIPS MT Thread exception in kernel", regs);
963 force_sig(SIGILL, current);
967 asmlinkage void do_dsp(struct pt_regs *regs)
970 panic("Unexpected DSP exception\n");
972 force_sig(SIGILL, current);
975 asmlinkage void do_reserved(struct pt_regs *regs)
978 * Game over - no way to handle this if it ever occurs. Most probably
979 * caused by a new unknown cpu type or after another deadly
980 * hard/software error.
983 panic("Caught reserved exception %ld - should not happen.",
984 (regs->cp0_cause & 0x7f) >> 2);
987 static int __initdata l1parity = 1;
988 static int __init nol1parity(char *s)
993 __setup("nol1par", nol1parity);
994 static int __initdata l2parity = 1;
995 static int __init nol2parity(char *s)
1000 __setup("nol2par", nol2parity);
1003 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1004 * it different ways.
1006 static inline void parity_protection_init(void)
1008 switch (current_cpu_type()) {
1014 #define ERRCTL_PE 0x80000000
1015 #define ERRCTL_L2P 0x00800000
1016 unsigned long errctl;
1017 unsigned int l1parity_present, l2parity_present;
1019 errctl = read_c0_ecc();
1020 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1022 /* probe L1 parity support */
1023 write_c0_ecc(errctl | ERRCTL_PE);
1024 back_to_back_c0_hazard();
1025 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1027 /* probe L2 parity support */
1028 write_c0_ecc(errctl|ERRCTL_L2P);
1029 back_to_back_c0_hazard();
1030 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1032 if (l1parity_present && l2parity_present) {
1034 errctl |= ERRCTL_PE;
1035 if (l1parity ^ l2parity)
1036 errctl |= ERRCTL_L2P;
1037 } else if (l1parity_present) {
1039 errctl |= ERRCTL_PE;
1040 } else if (l2parity_present) {
1042 errctl |= ERRCTL_L2P;
1044 /* No parity available */
1047 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1049 write_c0_ecc(errctl);
1050 back_to_back_c0_hazard();
1051 errctl = read_c0_ecc();
1052 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1054 if (l1parity_present)
1055 printk(KERN_INFO "Cache parity protection %sabled\n",
1056 (errctl & ERRCTL_PE) ? "en" : "dis");
1058 if (l2parity_present) {
1059 if (l1parity_present && l1parity)
1060 errctl ^= ERRCTL_L2P;
1061 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1062 (errctl & ERRCTL_L2P) ? "en" : "dis");
1068 write_c0_ecc(0x80000000);
1069 back_to_back_c0_hazard();
1070 /* Set the PE bit (bit 31) in the c0_errctl register. */
1071 printk(KERN_INFO "Cache parity protection %sabled\n",
1072 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
1076 /* Clear the DE bit (bit 16) in the c0_status register. */
1077 printk(KERN_INFO "Enable cache parity protection for "
1078 "MIPS 20KC/25KF CPUs.\n");
1079 clear_c0_status(ST0_DE);
1086 asmlinkage void cache_parity_error(void)
1088 const int field = 2 * sizeof(unsigned long);
1089 unsigned int reg_val;
1091 /* For the moment, report the problem and hang. */
1092 printk("Cache error exception:\n");
1093 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1094 reg_val = read_c0_cacheerr();
1095 printk("c0_cacheerr == %08x\n", reg_val);
1097 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1098 reg_val & (1<<30) ? "secondary" : "primary",
1099 reg_val & (1<<31) ? "data" : "insn");
1100 printk("Error bits: %s%s%s%s%s%s%s\n",
1101 reg_val & (1<<29) ? "ED " : "",
1102 reg_val & (1<<28) ? "ET " : "",
1103 reg_val & (1<<26) ? "EE " : "",
1104 reg_val & (1<<25) ? "EB " : "",
1105 reg_val & (1<<24) ? "EI " : "",
1106 reg_val & (1<<23) ? "E1 " : "",
1107 reg_val & (1<<22) ? "E0 " : "");
1108 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1110 #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
1111 if (reg_val & (1<<22))
1112 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1114 if (reg_val & (1<<23))
1115 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1118 panic("Can't handle the cache error!");
1122 * SDBBP EJTAG debug exception handler.
1123 * We skip the instruction and return to the next instruction.
1125 void ejtag_exception_handler(struct pt_regs *regs)
1127 const int field = 2 * sizeof(unsigned long);
1128 unsigned long depc, old_epc;
1131 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
1132 depc = read_c0_depc();
1133 debug = read_c0_debug();
1134 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
1135 if (debug & 0x80000000) {
1137 * In branch delay slot.
1138 * We cheat a little bit here and use EPC to calculate the
1139 * debug return address (DEPC). EPC is restored after the
1142 old_epc = regs->cp0_epc;
1143 regs->cp0_epc = depc;
1144 __compute_return_epc(regs);
1145 depc = regs->cp0_epc;
1146 regs->cp0_epc = old_epc;
1149 write_c0_depc(depc);
1152 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
1153 write_c0_debug(debug | 0x100);
1158 * NMI exception handler.
1160 NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
1163 printk("NMI taken!!!!\n");
1167 #define VECTORSPACING 0x100 /* for EI/VI mode */
1169 unsigned long ebase;
1170 unsigned long exception_handlers[32];
1171 unsigned long vi_handlers[64];
1174 * As a side effect of the way this is implemented we're limited
1175 * to interrupt handlers in the address range from
1176 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1178 void *set_except_vector(int n, void *addr)
1180 unsigned long handler = (unsigned long) addr;
1181 unsigned long old_handler = exception_handlers[n];
1183 exception_handlers[n] = handler;
1184 if (n == 0 && cpu_has_divec) {
1185 *(u32 *)(ebase + 0x200) = 0x08000000 |
1186 (0x03ffffff & (handler >> 2));
1187 flush_icache_range(ebase + 0x200, ebase + 0x204);
1189 return (void *)old_handler;
1192 static asmlinkage void do_default_vi(void)
1194 show_regs(get_irq_regs());
1195 panic("Caught unexpected vectored interrupt.");
1198 static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
1200 unsigned long handler;
1201 unsigned long old_handler = vi_handlers[n];
1202 int srssets = current_cpu_data.srsets;
1206 if (!cpu_has_veic && !cpu_has_vint)
1210 handler = (unsigned long) do_default_vi;
1213 handler = (unsigned long) addr;
1214 vi_handlers[n] = (unsigned long) addr;
1216 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1219 panic("Shadow register set %d not supported", srs);
1222 if (board_bind_eic_interrupt)
1223 board_bind_eic_interrupt(n, srs);
1224 } else if (cpu_has_vint) {
1225 /* SRSMap is only defined if shadow sets are implemented */
1227 change_c0_srsmap(0xf << n*4, srs << n*4);
1232 * If no shadow set is selected then use the default handler
1233 * that does normal register saving and a standard interrupt exit
1236 extern char except_vec_vi, except_vec_vi_lui;
1237 extern char except_vec_vi_ori, except_vec_vi_end;
1238 #ifdef CONFIG_MIPS_MT_SMTC
1240 * We need to provide the SMTC vectored interrupt handler
1241 * not only with the address of the handler, but with the
1242 * Status.IM bit to be masked before going there.
1244 extern char except_vec_vi_mori;
1245 const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
1246 #endif /* CONFIG_MIPS_MT_SMTC */
1247 const int handler_len = &except_vec_vi_end - &except_vec_vi;
1248 const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
1249 const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
1251 if (handler_len > VECTORSPACING) {
1253 * Sigh... panicing won't help as the console
1254 * is probably not configured :(
1256 panic("VECTORSPACING too small");
1259 memcpy(b, &except_vec_vi, handler_len);
1260 #ifdef CONFIG_MIPS_MT_SMTC
1261 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1263 w = (u32 *)(b + mori_offset);
1264 *w = (*w & 0xffff0000) | (0x100 << n);
1265 #endif /* CONFIG_MIPS_MT_SMTC */
1266 w = (u32 *)(b + lui_offset);
1267 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1268 w = (u32 *)(b + ori_offset);
1269 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1270 flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
1274 * In other cases jump directly to the interrupt handler
1276 * It is the handlers responsibility to save registers if required
1277 * (eg hi/lo) and return from the exception using "eret"
1280 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1282 flush_icache_range((unsigned long)b, (unsigned long)(b+8));
1285 return (void *)old_handler;
1288 void *set_vi_handler(int n, vi_handler_t addr)
1290 return set_vi_srs_handler(n, addr, 0);
1294 * This is used by native signal handling
1296 asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1297 asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
1299 extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1300 extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
1302 extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1303 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
1306 static int smp_save_fp_context(struct sigcontext __user *sc)
1308 return raw_cpu_has_fpu
1309 ? _save_fp_context(sc)
1310 : fpu_emulator_save_context(sc);
1313 static int smp_restore_fp_context(struct sigcontext __user *sc)
1315 return raw_cpu_has_fpu
1316 ? _restore_fp_context(sc)
1317 : fpu_emulator_restore_context(sc);
1321 static inline void signal_init(void)
1324 /* For now just do the cpu_has_fpu check when the functions are invoked */
1325 save_fp_context = smp_save_fp_context;
1326 restore_fp_context = smp_restore_fp_context;
1329 save_fp_context = _save_fp_context;
1330 restore_fp_context = _restore_fp_context;
1332 save_fp_context = fpu_emulator_save_context;
1333 restore_fp_context = fpu_emulator_restore_context;
1338 #ifdef CONFIG_MIPS32_COMPAT
1341 * This is used by 32-bit signal stuff on the 64-bit kernel
1343 asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1344 asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
1346 extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1347 extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
1349 extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1350 extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
1352 static inline void signal32_init(void)
1355 save_fp_context32 = _save_fp_context32;
1356 restore_fp_context32 = _restore_fp_context32;
1358 save_fp_context32 = fpu_emulator_save_context32;
1359 restore_fp_context32 = fpu_emulator_restore_context32;
1364 extern void cpu_cache_init(void);
1365 extern void tlb_init(void);
1366 extern void flush_tlb_handlers(void);
1371 int cp0_compare_irq;
1374 * Performance counter IRQ or -1 if shared with timer
1376 int cp0_perfcount_irq;
1377 EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1379 static int __cpuinitdata noulri;
1381 static int __init ulri_disable(char *s)
1383 pr_info("Disabling ulri\n");
1388 __setup("noulri", ulri_disable);
1390 void __cpuinit per_cpu_trap_init(void)
1392 unsigned int cpu = smp_processor_id();
1393 unsigned int status_set = ST0_CU0;
1394 #ifdef CONFIG_MIPS_MT_SMTC
1395 int secondaryTC = 0;
1396 int bootTC = (cpu == 0);
1399 * Only do per_cpu_trap_init() for first TC of Each VPE.
1400 * Note that this hack assumes that the SMTC init code
1401 * assigns TCs consecutively and in ascending order.
1404 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1405 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1407 #endif /* CONFIG_MIPS_MT_SMTC */
1410 * Disable coprocessors and select 32-bit or 64-bit addressing
1411 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1412 * flag that some firmware may have left set and the TS bit (for
1413 * IP27). Set XX for ISA IV code to work.
1416 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1418 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1419 status_set |= ST0_XX;
1421 status_set |= ST0_MX;
1423 change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1426 if (cpu_has_mips_r2) {
1427 unsigned int enable = 0x0000000f;
1429 if (!noulri && cpu_has_userlocal)
1430 enable |= (1 << 29);
1432 write_c0_hwrena(enable);
1435 #ifdef CONFIG_MIPS_MT_SMTC
1437 #endif /* CONFIG_MIPS_MT_SMTC */
1439 if (cpu_has_veic || cpu_has_vint) {
1440 write_c0_ebase(ebase);
1441 /* Setting vector spacing enables EI/VI mode */
1442 change_c0_intctl(0x3e0, VECTORSPACING);
1444 if (cpu_has_divec) {
1445 if (cpu_has_mipsmt) {
1446 unsigned int vpflags = dvpe();
1447 set_c0_cause(CAUSEF_IV);
1450 set_c0_cause(CAUSEF_IV);
1454 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1456 * o read IntCtl.IPTI to determine the timer interrupt
1457 * o read IntCtl.IPPCI to determine the performance counter interrupt
1459 if (cpu_has_mips_r2) {
1460 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1461 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
1462 if (cp0_perfcount_irq == cp0_compare_irq)
1463 cp0_perfcount_irq = -1;
1465 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
1466 cp0_perfcount_irq = -1;
1469 #ifdef CONFIG_MIPS_MT_SMTC
1471 #endif /* CONFIG_MIPS_MT_SMTC */
1473 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1474 TLBMISS_HANDLER_SETUP();
1476 atomic_inc(&init_mm.mm_count);
1477 current->active_mm = &init_mm;
1478 BUG_ON(current->mm);
1479 enter_lazy_tlb(&init_mm, current);
1481 #ifdef CONFIG_MIPS_MT_SMTC
1483 #endif /* CONFIG_MIPS_MT_SMTC */
1486 #ifdef CONFIG_MIPS_MT_SMTC
1487 } else if (!secondaryTC) {
1489 * First TC in non-boot VPE must do subset of tlb_init()
1490 * for MMU countrol registers.
1492 write_c0_pagemask(PM_DEFAULT_MASK);
1495 #endif /* CONFIG_MIPS_MT_SMTC */
1498 /* Install CPU exception handler */
1499 void __init set_handler(unsigned long offset, void *addr, unsigned long size)
1501 memcpy((void *)(ebase + offset), addr, size);
1502 flush_icache_range(ebase + offset, ebase + offset + size);
1505 static char panic_null_cerr[] __cpuinitdata =
1506 "Trying to set NULL cache error exception handler";
1508 /* Install uncached CPU exception handler */
1509 void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1513 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1516 unsigned long uncached_ebase = TO_UNCAC(ebase);
1520 panic(panic_null_cerr);
1522 memcpy((void *)(uncached_ebase + offset), addr, size);
1525 static int __initdata rdhwr_noopt;
1526 static int __init set_rdhwr_noopt(char *str)
1532 __setup("rdhwr_noopt", set_rdhwr_noopt);
1534 void __init trap_init(void)
1536 extern char except_vec3_generic, except_vec3_r4000;
1537 extern char except_vec4;
1540 if (cpu_has_veic || cpu_has_vint)
1541 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
1545 per_cpu_trap_init();
1548 * Copy the generic exception handlers to their final destination.
1549 * This will be overriden later as suitable for a particular
1552 set_handler(0x180, &except_vec3_generic, 0x80);
1555 * Setup default vectors
1557 for (i = 0; i <= 31; i++)
1558 set_except_vector(i, handle_reserved);
1561 * Copy the EJTAG debug exception vector handler code to it's final
1564 if (cpu_has_ejtag && board_ejtag_handler_setup)
1565 board_ejtag_handler_setup();
1568 * Only some CPUs have the watch exceptions.
1571 set_except_vector(23, handle_watch);
1574 * Initialise interrupt handlers
1576 if (cpu_has_veic || cpu_has_vint) {
1577 int nvec = cpu_has_veic ? 64 : 8;
1578 for (i = 0; i < nvec; i++)
1579 set_vi_handler(i, NULL);
1581 else if (cpu_has_divec)
1582 set_handler(0x200, &except_vec4, 0x8);
1585 * Some CPUs can enable/disable for cache parity detection, but does
1586 * it different ways.
1588 parity_protection_init();
1591 * The Data Bus Errors / Instruction Bus Errors are signaled
1592 * by external hardware. Therefore these two exceptions
1593 * may have board specific handlers.
1598 set_except_vector(0, handle_int);
1599 set_except_vector(1, handle_tlbm);
1600 set_except_vector(2, handle_tlbl);
1601 set_except_vector(3, handle_tlbs);
1603 set_except_vector(4, handle_adel);
1604 set_except_vector(5, handle_ades);
1606 set_except_vector(6, handle_ibe);
1607 set_except_vector(7, handle_dbe);
1609 set_except_vector(8, handle_sys);
1610 set_except_vector(9, handle_bp);
1611 set_except_vector(10, rdhwr_noopt ? handle_ri :
1612 (cpu_has_vtag_icache ?
1613 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
1614 set_except_vector(11, handle_cpu);
1615 set_except_vector(12, handle_ov);
1616 set_except_vector(13, handle_tr);
1618 if (current_cpu_type() == CPU_R6000 ||
1619 current_cpu_type() == CPU_R6000A) {
1621 * The R6000 is the only R-series CPU that features a machine
1622 * check exception (similar to the R4000 cache error) and
1623 * unaligned ldc1/sdc1 exception. The handlers have not been
1624 * written yet. Well, anyway there is no R6000 machine on the
1625 * current list of targets for Linux/MIPS.
1626 * (Duh, crap, there is someone with a triple R6k machine)
1628 //set_except_vector(14, handle_mc);
1629 //set_except_vector(15, handle_ndc);
1633 if (board_nmi_handler_setup)
1634 board_nmi_handler_setup();
1636 if (cpu_has_fpu && !cpu_has_nofpuex)
1637 set_except_vector(15, handle_fpe);
1639 set_except_vector(22, handle_mdmx);
1642 set_except_vector(24, handle_mcheck);
1645 set_except_vector(25, handle_mt);
1647 set_except_vector(26, handle_dsp);
1650 /* Special exception: R4[04]00 uses also the divec space. */
1651 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1652 else if (cpu_has_4kex)
1653 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1655 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1658 #ifdef CONFIG_MIPS32_COMPAT
1662 flush_icache_range(ebase, ebase + 0x400);
1663 flush_tlb_handlers();