Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / arch / mips / kernel / traps.c
1 /*
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
4  * for more details.
5  *
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
13  */
14 #include <linux/bug.h>
15 #include <linux/compiler.h>
16 #include <linux/init.h>
17 #include <linux/mm.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>
28 #include <linux/notifier.h>
29
30 #include <asm/bootinfo.h>
31 #include <asm/branch.h>
32 #include <asm/break.h>
33 #include <asm/cop2.h>
34 #include <asm/cpu.h>
35 #include <asm/dsp.h>
36 #include <asm/fpu.h>
37 #include <asm/fpu_emulator.h>
38 #include <asm/mipsregs.h>
39 #include <asm/mipsmtregs.h>
40 #include <asm/module.h>
41 #include <asm/pgtable.h>
42 #include <asm/ptrace.h>
43 #include <asm/sections.h>
44 #include <asm/system.h>
45 #include <asm/tlbdebug.h>
46 #include <asm/traps.h>
47 #include <asm/uaccess.h>
48 #include <asm/watch.h>
49 #include <asm/mmu_context.h>
50 #include <asm/types.h>
51 #include <asm/stacktrace.h>
52 #include <asm/irq.h>
53
54 extern void check_wait(void);
55 extern asmlinkage void r4k_wait(void);
56 extern asmlinkage void rollback_handle_int(void);
57 extern asmlinkage void handle_int(void);
58 extern asmlinkage void handle_tlbm(void);
59 extern asmlinkage void handle_tlbl(void);
60 extern asmlinkage void handle_tlbs(void);
61 extern asmlinkage void handle_adel(void);
62 extern asmlinkage void handle_ades(void);
63 extern asmlinkage void handle_ibe(void);
64 extern asmlinkage void handle_dbe(void);
65 extern asmlinkage void handle_sys(void);
66 extern asmlinkage void handle_bp(void);
67 extern asmlinkage void handle_ri(void);
68 extern asmlinkage void handle_ri_rdhwr_vivt(void);
69 extern asmlinkage void handle_ri_rdhwr(void);
70 extern asmlinkage void handle_cpu(void);
71 extern asmlinkage void handle_ov(void);
72 extern asmlinkage void handle_tr(void);
73 extern asmlinkage void handle_fpe(void);
74 extern asmlinkage void handle_mdmx(void);
75 extern asmlinkage void handle_watch(void);
76 extern asmlinkage void handle_mt(void);
77 extern asmlinkage void handle_dsp(void);
78 extern asmlinkage void handle_mcheck(void);
79 extern asmlinkage void handle_reserved(void);
80
81 extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
82         struct mips_fpu_struct *ctx, int has_fpu);
83
84 void (*board_be_init)(void);
85 int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
86 void (*board_nmi_handler_setup)(void);
87 void (*board_ejtag_handler_setup)(void);
88 void (*board_bind_eic_interrupt)(int irq, int regset);
89
90
91 static void show_raw_backtrace(unsigned long reg29)
92 {
93         unsigned long *sp = (unsigned long *)(reg29 & ~3);
94         unsigned long addr;
95
96         printk("Call Trace:");
97 #ifdef CONFIG_KALLSYMS
98         printk("\n");
99 #endif
100         while (!kstack_end(sp)) {
101                 unsigned long __user *p =
102                         (unsigned long __user *)(unsigned long)sp++;
103                 if (__get_user(addr, p)) {
104                         printk(" (Bad stack address)");
105                         break;
106                 }
107                 if (__kernel_text_address(addr))
108                         print_ip_sym(addr);
109         }
110         printk("\n");
111 }
112
113 #ifdef CONFIG_KALLSYMS
114 int raw_show_trace;
115 static int __init set_raw_show_trace(char *str)
116 {
117         raw_show_trace = 1;
118         return 1;
119 }
120 __setup("raw_show_trace", set_raw_show_trace);
121 #endif
122
123 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
124 {
125         unsigned long sp = regs->regs[29];
126         unsigned long ra = regs->regs[31];
127         unsigned long pc = regs->cp0_epc;
128
129         if (raw_show_trace || !__kernel_text_address(pc)) {
130                 show_raw_backtrace(sp);
131                 return;
132         }
133         printk("Call Trace:\n");
134         do {
135                 print_ip_sym(pc);
136                 pc = unwind_stack(task, &sp, pc, &ra);
137         } while (pc);
138         printk("\n");
139 }
140
141 /*
142  * This routine abuses get_user()/put_user() to reference pointers
143  * with at least a bit of error checking ...
144  */
145 static void show_stacktrace(struct task_struct *task,
146         const struct pt_regs *regs)
147 {
148         const int field = 2 * sizeof(unsigned long);
149         long stackdata;
150         int i;
151         unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
152
153         printk("Stack :");
154         i = 0;
155         while ((unsigned long) sp & (PAGE_SIZE - 1)) {
156                 if (i && ((i % (64 / field)) == 0))
157                         printk("\n       ");
158                 if (i > 39) {
159                         printk(" ...");
160                         break;
161                 }
162
163                 if (__get_user(stackdata, sp++)) {
164                         printk(" (Bad stack address)");
165                         break;
166                 }
167
168                 printk(" %0*lx", field, stackdata);
169                 i++;
170         }
171         printk("\n");
172         show_backtrace(task, regs);
173 }
174
175 void show_stack(struct task_struct *task, unsigned long *sp)
176 {
177         struct pt_regs regs;
178         if (sp) {
179                 regs.regs[29] = (unsigned long)sp;
180                 regs.regs[31] = 0;
181                 regs.cp0_epc = 0;
182         } else {
183                 if (task && task != current) {
184                         regs.regs[29] = task->thread.reg29;
185                         regs.regs[31] = 0;
186                         regs.cp0_epc = task->thread.reg31;
187                 } else {
188                         prepare_frametrace(&regs);
189                 }
190         }
191         show_stacktrace(task, &regs);
192 }
193
194 /*
195  * The architecture-independent dump_stack generator
196  */
197 void dump_stack(void)
198 {
199         struct pt_regs regs;
200
201         prepare_frametrace(&regs);
202         show_backtrace(current, &regs);
203 }
204
205 EXPORT_SYMBOL(dump_stack);
206
207 static void show_code(unsigned int __user *pc)
208 {
209         long i;
210         unsigned short __user *pc16 = NULL;
211
212         printk("\nCode:");
213
214         if ((unsigned long)pc & 1)
215                 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
216         for(i = -3 ; i < 6 ; i++) {
217                 unsigned int insn;
218                 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
219                         printk(" (Bad address in epc)\n");
220                         break;
221                 }
222                 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
223         }
224 }
225
226 static void __show_regs(const struct pt_regs *regs)
227 {
228         const int field = 2 * sizeof(unsigned long);
229         unsigned int cause = regs->cp0_cause;
230         int i;
231
232         printk("Cpu %d\n", smp_processor_id());
233
234         /*
235          * Saved main processor registers
236          */
237         for (i = 0; i < 32; ) {
238                 if ((i % 4) == 0)
239                         printk("$%2d   :", i);
240                 if (i == 0)
241                         printk(" %0*lx", field, 0UL);
242                 else if (i == 26 || i == 27)
243                         printk(" %*s", field, "");
244                 else
245                         printk(" %0*lx", field, regs->regs[i]);
246
247                 i++;
248                 if ((i % 4) == 0)
249                         printk("\n");
250         }
251
252 #ifdef CONFIG_CPU_HAS_SMARTMIPS
253         printk("Acx    : %0*lx\n", field, regs->acx);
254 #endif
255         printk("Hi    : %0*lx\n", field, regs->hi);
256         printk("Lo    : %0*lx\n", field, regs->lo);
257
258         /*
259          * Saved cp0 registers
260          */
261         printk("epc   : %0*lx %pS\n", field, regs->cp0_epc,
262                (void *) regs->cp0_epc);
263         printk("    %s\n", print_tainted());
264         printk("ra    : %0*lx %pS\n", field, regs->regs[31],
265                (void *) regs->regs[31]);
266
267         printk("Status: %08x    ", (uint32_t) regs->cp0_status);
268
269         if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
270                 if (regs->cp0_status & ST0_KUO)
271                         printk("KUo ");
272                 if (regs->cp0_status & ST0_IEO)
273                         printk("IEo ");
274                 if (regs->cp0_status & ST0_KUP)
275                         printk("KUp ");
276                 if (regs->cp0_status & ST0_IEP)
277                         printk("IEp ");
278                 if (regs->cp0_status & ST0_KUC)
279                         printk("KUc ");
280                 if (regs->cp0_status & ST0_IEC)
281                         printk("IEc ");
282         } else {
283                 if (regs->cp0_status & ST0_KX)
284                         printk("KX ");
285                 if (regs->cp0_status & ST0_SX)
286                         printk("SX ");
287                 if (regs->cp0_status & ST0_UX)
288                         printk("UX ");
289                 switch (regs->cp0_status & ST0_KSU) {
290                 case KSU_USER:
291                         printk("USER ");
292                         break;
293                 case KSU_SUPERVISOR:
294                         printk("SUPERVISOR ");
295                         break;
296                 case KSU_KERNEL:
297                         printk("KERNEL ");
298                         break;
299                 default:
300                         printk("BAD_MODE ");
301                         break;
302                 }
303                 if (regs->cp0_status & ST0_ERL)
304                         printk("ERL ");
305                 if (regs->cp0_status & ST0_EXL)
306                         printk("EXL ");
307                 if (regs->cp0_status & ST0_IE)
308                         printk("IE ");
309         }
310         printk("\n");
311
312         printk("Cause : %08x\n", cause);
313
314         cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
315         if (1 <= cause && cause <= 5)
316                 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
317
318         printk("PrId  : %08x (%s)\n", read_c0_prid(),
319                cpu_name_string());
320 }
321
322 /*
323  * FIXME: really the generic show_regs should take a const pointer argument.
324  */
325 void show_regs(struct pt_regs *regs)
326 {
327         __show_regs((struct pt_regs *)regs);
328 }
329
330 void show_registers(const struct pt_regs *regs)
331 {
332         const int field = 2 * sizeof(unsigned long);
333
334         __show_regs(regs);
335         print_modules();
336         printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
337                current->comm, current->pid, current_thread_info(), current,
338               field, current_thread_info()->tp_value);
339         if (cpu_has_userlocal) {
340                 unsigned long tls;
341
342                 tls = read_c0_userlocal();
343                 if (tls != current_thread_info()->tp_value)
344                         printk("*HwTLS: %0*lx\n", field, tls);
345         }
346
347         show_stacktrace(current, regs);
348         show_code((unsigned int __user *) regs->cp0_epc);
349         printk("\n");
350 }
351
352 static DEFINE_SPINLOCK(die_lock);
353
354 void __noreturn die(const char * str, const struct pt_regs * regs)
355 {
356         static int die_counter;
357 #ifdef CONFIG_MIPS_MT_SMTC
358         unsigned long dvpret = dvpe();
359 #endif /* CONFIG_MIPS_MT_SMTC */
360
361         console_verbose();
362         spin_lock_irq(&die_lock);
363         bust_spinlocks(1);
364 #ifdef CONFIG_MIPS_MT_SMTC
365         mips_mt_regdump(dvpret);
366 #endif /* CONFIG_MIPS_MT_SMTC */
367         printk("%s[#%d]:\n", str, ++die_counter);
368         show_registers(regs);
369         add_taint(TAINT_DIE);
370         spin_unlock_irq(&die_lock);
371
372         if (in_interrupt())
373                 panic("Fatal exception in interrupt");
374
375         if (panic_on_oops) {
376                 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
377                 ssleep(5);
378                 panic("Fatal exception");
379         }
380
381         do_exit(SIGSEGV);
382 }
383
384 extern struct exception_table_entry __start___dbe_table[];
385 extern struct exception_table_entry __stop___dbe_table[];
386
387 __asm__(
388 "       .section        __dbe_table, \"a\"\n"
389 "       .previous                       \n");
390
391 /* Given an address, look for it in the exception tables. */
392 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
393 {
394         const struct exception_table_entry *e;
395
396         e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
397         if (!e)
398                 e = search_module_dbetables(addr);
399         return e;
400 }
401
402 asmlinkage void do_be(struct pt_regs *regs)
403 {
404         const int field = 2 * sizeof(unsigned long);
405         const struct exception_table_entry *fixup = NULL;
406         int data = regs->cp0_cause & 4;
407         int action = MIPS_BE_FATAL;
408
409         /* XXX For now.  Fixme, this searches the wrong table ...  */
410         if (data && !user_mode(regs))
411                 fixup = search_dbe_tables(exception_epc(regs));
412
413         if (fixup)
414                 action = MIPS_BE_FIXUP;
415
416         if (board_be_handler)
417                 action = board_be_handler(regs, fixup != NULL);
418
419         switch (action) {
420         case MIPS_BE_DISCARD:
421                 return;
422         case MIPS_BE_FIXUP:
423                 if (fixup) {
424                         regs->cp0_epc = fixup->nextinsn;
425                         return;
426                 }
427                 break;
428         default:
429                 break;
430         }
431
432         /*
433          * Assume it would be too dangerous to continue ...
434          */
435         printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
436                data ? "Data" : "Instruction",
437                field, regs->cp0_epc, field, regs->regs[31]);
438         if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
439             == NOTIFY_STOP)
440                 return;
441
442         die_if_kernel("Oops", regs);
443         force_sig(SIGBUS, current);
444 }
445
446 /*
447  * ll/sc, rdhwr, sync emulation
448  */
449
450 #define OPCODE 0xfc000000
451 #define BASE   0x03e00000
452 #define RT     0x001f0000
453 #define OFFSET 0x0000ffff
454 #define LL     0xc0000000
455 #define SC     0xe0000000
456 #define SPEC0  0x00000000
457 #define SPEC3  0x7c000000
458 #define RD     0x0000f800
459 #define FUNC   0x0000003f
460 #define SYNC   0x0000000f
461 #define RDHWR  0x0000003b
462
463 /*
464  * The ll_bit is cleared by r*_switch.S
465  */
466
467 unsigned int ll_bit;
468 struct task_struct *ll_task;
469
470 static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
471 {
472         unsigned long value, __user *vaddr;
473         long offset;
474
475         /*
476          * analyse the ll instruction that just caused a ri exception
477          * and put the referenced address to addr.
478          */
479
480         /* sign extend offset */
481         offset = opcode & OFFSET;
482         offset <<= 16;
483         offset >>= 16;
484
485         vaddr = (unsigned long __user *)
486                 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
487
488         if ((unsigned long)vaddr & 3)
489                 return SIGBUS;
490         if (get_user(value, vaddr))
491                 return SIGSEGV;
492
493         preempt_disable();
494
495         if (ll_task == NULL || ll_task == current) {
496                 ll_bit = 1;
497         } else {
498                 ll_bit = 0;
499         }
500         ll_task = current;
501
502         preempt_enable();
503
504         regs->regs[(opcode & RT) >> 16] = value;
505
506         return 0;
507 }
508
509 static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
510 {
511         unsigned long __user *vaddr;
512         unsigned long reg;
513         long offset;
514
515         /*
516          * analyse the sc instruction that just caused a ri exception
517          * and put the referenced address to addr.
518          */
519
520         /* sign extend offset */
521         offset = opcode & OFFSET;
522         offset <<= 16;
523         offset >>= 16;
524
525         vaddr = (unsigned long __user *)
526                 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
527         reg = (opcode & RT) >> 16;
528
529         if ((unsigned long)vaddr & 3)
530                 return SIGBUS;
531
532         preempt_disable();
533
534         if (ll_bit == 0 || ll_task != current) {
535                 regs->regs[reg] = 0;
536                 preempt_enable();
537                 return 0;
538         }
539
540         preempt_enable();
541
542         if (put_user(regs->regs[reg], vaddr))
543                 return SIGSEGV;
544
545         regs->regs[reg] = 1;
546
547         return 0;
548 }
549
550 /*
551  * ll uses the opcode of lwc0 and sc uses the opcode of swc0.  That is both
552  * opcodes are supposed to result in coprocessor unusable exceptions if
553  * executed on ll/sc-less processors.  That's the theory.  In practice a
554  * few processors such as NEC's VR4100 throw reserved instruction exceptions
555  * instead, so we're doing the emulation thing in both exception handlers.
556  */
557 static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
558 {
559         if ((opcode & OPCODE) == LL)
560                 return simulate_ll(regs, opcode);
561         if ((opcode & OPCODE) == SC)
562                 return simulate_sc(regs, opcode);
563
564         return -1;                      /* Must be something else ... */
565 }
566
567 /*
568  * Simulate trapping 'rdhwr' instructions to provide user accessible
569  * registers not implemented in hardware.
570  */
571 static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
572 {
573         struct thread_info *ti = task_thread_info(current);
574
575         if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
576                 int rd = (opcode & RD) >> 11;
577                 int rt = (opcode & RT) >> 16;
578                 switch (rd) {
579                 case 0:         /* CPU number */
580                         regs->regs[rt] = smp_processor_id();
581                         return 0;
582                 case 1:         /* SYNCI length */
583                         regs->regs[rt] = min(current_cpu_data.dcache.linesz,
584                                              current_cpu_data.icache.linesz);
585                         return 0;
586                 case 2:         /* Read count register */
587                         regs->regs[rt] = read_c0_count();
588                         return 0;
589                 case 3:         /* Count register resolution */
590                         switch (current_cpu_data.cputype) {
591                         case CPU_20KC:
592                         case CPU_25KF:
593                                 regs->regs[rt] = 1;
594                                 break;
595                         default:
596                                 regs->regs[rt] = 2;
597                         }
598                         return 0;
599                 case 29:
600                         regs->regs[rt] = ti->tp_value;
601                         return 0;
602                 default:
603                         return -1;
604                 }
605         }
606
607         /* Not ours.  */
608         return -1;
609 }
610
611 static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
612 {
613         if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
614                 return 0;
615
616         return -1;                      /* Must be something else ... */
617 }
618
619 asmlinkage void do_ov(struct pt_regs *regs)
620 {
621         siginfo_t info;
622
623         die_if_kernel("Integer overflow", regs);
624
625         info.si_code = FPE_INTOVF;
626         info.si_signo = SIGFPE;
627         info.si_errno = 0;
628         info.si_addr = (void __user *) regs->cp0_epc;
629         force_sig_info(SIGFPE, &info, current);
630 }
631
632 /*
633  * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
634  */
635 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
636 {
637         siginfo_t info;
638
639         if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
640             == NOTIFY_STOP)
641                 return;
642         die_if_kernel("FP exception in kernel code", regs);
643
644         if (fcr31 & FPU_CSR_UNI_X) {
645                 int sig;
646
647                 /*
648                  * Unimplemented operation exception.  If we've got the full
649                  * software emulator on-board, let's use it...
650                  *
651                  * Force FPU to dump state into task/thread context.  We're
652                  * moving a lot of data here for what is probably a single
653                  * instruction, but the alternative is to pre-decode the FP
654                  * register operands before invoking the emulator, which seems
655                  * a bit extreme for what should be an infrequent event.
656                  */
657                 /* Ensure 'resume' not overwrite saved fp context again. */
658                 lose_fpu(1);
659
660                 /* Run the emulator */
661                 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
662
663                 /*
664                  * We can't allow the emulated instruction to leave any of
665                  * the cause bit set in $fcr31.
666                  */
667                 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
668
669                 /* Restore the hardware register state */
670                 own_fpu(1);     /* Using the FPU again.  */
671
672                 /* If something went wrong, signal */
673                 if (sig)
674                         force_sig(sig, current);
675
676                 return;
677         } else if (fcr31 & FPU_CSR_INV_X)
678                 info.si_code = FPE_FLTINV;
679         else if (fcr31 & FPU_CSR_DIV_X)
680                 info.si_code = FPE_FLTDIV;
681         else if (fcr31 & FPU_CSR_OVF_X)
682                 info.si_code = FPE_FLTOVF;
683         else if (fcr31 & FPU_CSR_UDF_X)
684                 info.si_code = FPE_FLTUND;
685         else if (fcr31 & FPU_CSR_INE_X)
686                 info.si_code = FPE_FLTRES;
687         else
688                 info.si_code = __SI_FAULT;
689         info.si_signo = SIGFPE;
690         info.si_errno = 0;
691         info.si_addr = (void __user *) regs->cp0_epc;
692         force_sig_info(SIGFPE, &info, current);
693 }
694
695 static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
696         const char *str)
697 {
698         siginfo_t info;
699         char b[40];
700
701         if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
702                 return;
703
704         /*
705          * A short test says that IRIX 5.3 sends SIGTRAP for all trap
706          * insns, even for trap and break codes that indicate arithmetic
707          * failures.  Weird ...
708          * But should we continue the brokenness???  --macro
709          */
710         switch (code) {
711         case BRK_OVERFLOW:
712         case BRK_DIVZERO:
713                 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
714                 die_if_kernel(b, regs);
715                 if (code == BRK_DIVZERO)
716                         info.si_code = FPE_INTDIV;
717                 else
718                         info.si_code = FPE_INTOVF;
719                 info.si_signo = SIGFPE;
720                 info.si_errno = 0;
721                 info.si_addr = (void __user *) regs->cp0_epc;
722                 force_sig_info(SIGFPE, &info, current);
723                 break;
724         case BRK_BUG:
725                 die_if_kernel("Kernel bug detected", regs);
726                 force_sig(SIGTRAP, current);
727                 break;
728         case BRK_MEMU:
729                 /*
730                  * Address errors may be deliberately induced by the FPU
731                  * emulator to retake control of the CPU after executing the
732                  * instruction in the delay slot of an emulated branch.
733                  *
734                  * Terminate if exception was recognized as a delay slot return
735                  * otherwise handle as normal.
736                  */
737                 if (do_dsemulret(regs))
738                         return;
739
740                 die_if_kernel("Math emu break/trap", regs);
741                 force_sig(SIGTRAP, current);
742                 break;
743         default:
744                 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
745                 die_if_kernel(b, regs);
746                 force_sig(SIGTRAP, current);
747         }
748 }
749
750 asmlinkage void do_bp(struct pt_regs *regs)
751 {
752         unsigned int opcode, bcode;
753
754         if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
755                 goto out_sigsegv;
756
757         /*
758          * There is the ancient bug in the MIPS assemblers that the break
759          * code starts left to bit 16 instead to bit 6 in the opcode.
760          * Gas is bug-compatible, but not always, grrr...
761          * We handle both cases with a simple heuristics.  --macro
762          */
763         bcode = ((opcode >> 6) & ((1 << 20) - 1));
764         if (bcode >= (1 << 10))
765                 bcode >>= 10;
766
767         do_trap_or_bp(regs, bcode, "Break");
768         return;
769
770 out_sigsegv:
771         force_sig(SIGSEGV, current);
772 }
773
774 asmlinkage void do_tr(struct pt_regs *regs)
775 {
776         unsigned int opcode, tcode = 0;
777
778         if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
779                 goto out_sigsegv;
780
781         /* Immediate versions don't provide a code.  */
782         if (!(opcode & OPCODE))
783                 tcode = ((opcode >> 6) & ((1 << 10) - 1));
784
785         do_trap_or_bp(regs, tcode, "Trap");
786         return;
787
788 out_sigsegv:
789         force_sig(SIGSEGV, current);
790 }
791
792 asmlinkage void do_ri(struct pt_regs *regs)
793 {
794         unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
795         unsigned long old_epc = regs->cp0_epc;
796         unsigned int opcode = 0;
797         int status = -1;
798
799         if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
800             == NOTIFY_STOP)
801                 return;
802
803         die_if_kernel("Reserved instruction in kernel code", regs);
804
805         if (unlikely(compute_return_epc(regs) < 0))
806                 return;
807
808         if (unlikely(get_user(opcode, epc) < 0))
809                 status = SIGSEGV;
810
811         if (!cpu_has_llsc && status < 0)
812                 status = simulate_llsc(regs, opcode);
813
814         if (status < 0)
815                 status = simulate_rdhwr(regs, opcode);
816
817         if (status < 0)
818                 status = simulate_sync(regs, opcode);
819
820         if (status < 0)
821                 status = SIGILL;
822
823         if (unlikely(status > 0)) {
824                 regs->cp0_epc = old_epc;                /* Undo skip-over.  */
825                 force_sig(status, current);
826         }
827 }
828
829 /*
830  * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
831  * emulated more than some threshold number of instructions, force migration to
832  * a "CPU" that has FP support.
833  */
834 static void mt_ase_fp_affinity(void)
835 {
836 #ifdef CONFIG_MIPS_MT_FPAFF
837         if (mt_fpemul_threshold > 0 &&
838              ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
839                 /*
840                  * If there's no FPU present, or if the application has already
841                  * restricted the allowed set to exclude any CPUs with FPUs,
842                  * we'll skip the procedure.
843                  */
844                 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
845                         cpumask_t tmask;
846
847                         current->thread.user_cpus_allowed
848                                 = current->cpus_allowed;
849                         cpus_and(tmask, current->cpus_allowed,
850                                 mt_fpu_cpumask);
851                         set_cpus_allowed(current, tmask);
852                         set_thread_flag(TIF_FPUBOUND);
853                 }
854         }
855 #endif /* CONFIG_MIPS_MT_FPAFF */
856 }
857
858 /*
859  * No lock; only written during early bootup by CPU 0.
860  */
861 static RAW_NOTIFIER_HEAD(cu2_chain);
862
863 int __ref register_cu2_notifier(struct notifier_block *nb)
864 {
865         return raw_notifier_chain_register(&cu2_chain, nb);
866 }
867
868 int cu2_notifier_call_chain(unsigned long val, void *v)
869 {
870         return raw_notifier_call_chain(&cu2_chain, val, v);
871 }
872
873 static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
874         void *data)
875 {
876         struct pt_regs *regs = data;
877
878         switch (action) {
879         default:
880                 die_if_kernel("Unhandled kernel unaligned access or invalid "
881                               "instruction", regs);
882                 /* Fall through  */
883
884         case CU2_EXCEPTION:
885                 force_sig(SIGILL, current);
886         }
887
888         return NOTIFY_OK;
889 }
890
891 static struct notifier_block default_cu2_notifier = {
892         .notifier_call  = default_cu2_call,
893         .priority       = 0x80000000,           /* Run last  */
894 };
895
896 asmlinkage void do_cpu(struct pt_regs *regs)
897 {
898         unsigned int __user *epc;
899         unsigned long old_epc;
900         unsigned int opcode;
901         unsigned int cpid;
902         int status;
903         unsigned long __maybe_unused flags;
904
905         die_if_kernel("do_cpu invoked from kernel context!", regs);
906
907         cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
908
909         switch (cpid) {
910         case 0:
911                 epc = (unsigned int __user *)exception_epc(regs);
912                 old_epc = regs->cp0_epc;
913                 opcode = 0;
914                 status = -1;
915
916                 if (unlikely(compute_return_epc(regs) < 0))
917                         return;
918
919                 if (unlikely(get_user(opcode, epc) < 0))
920                         status = SIGSEGV;
921
922                 if (!cpu_has_llsc && status < 0)
923                         status = simulate_llsc(regs, opcode);
924
925                 if (status < 0)
926                         status = simulate_rdhwr(regs, opcode);
927
928                 if (status < 0)
929                         status = SIGILL;
930
931                 if (unlikely(status > 0)) {
932                         regs->cp0_epc = old_epc;        /* Undo skip-over.  */
933                         force_sig(status, current);
934                 }
935
936                 return;
937
938         case 1:
939                 if (used_math())        /* Using the FPU again.  */
940                         own_fpu(1);
941                 else {                  /* First time FPU user.  */
942                         init_fpu();
943                         set_used_math();
944                 }
945
946                 if (!raw_cpu_has_fpu) {
947                         int sig;
948                         sig = fpu_emulator_cop1Handler(regs,
949                                                 &current->thread.fpu, 0);
950                         if (sig)
951                                 force_sig(sig, current);
952                         else
953                                 mt_ase_fp_affinity();
954                 }
955
956                 return;
957
958         case 2:
959                 raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
960                 break;
961
962         case 3:
963                 break;
964         }
965
966         force_sig(SIGILL, current);
967 }
968
969 asmlinkage void do_mdmx(struct pt_regs *regs)
970 {
971         force_sig(SIGILL, current);
972 }
973
974 /*
975  * Called with interrupts disabled.
976  */
977 asmlinkage void do_watch(struct pt_regs *regs)
978 {
979         u32 cause;
980
981         /*
982          * Clear WP (bit 22) bit of cause register so we don't loop
983          * forever.
984          */
985         cause = read_c0_cause();
986         cause &= ~(1 << 22);
987         write_c0_cause(cause);
988
989         /*
990          * If the current thread has the watch registers loaded, save
991          * their values and send SIGTRAP.  Otherwise another thread
992          * left the registers set, clear them and continue.
993          */
994         if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
995                 mips_read_watch_registers();
996                 local_irq_enable();
997                 force_sig(SIGTRAP, current);
998         } else {
999                 mips_clear_watch_registers();
1000                 local_irq_enable();
1001         }
1002 }
1003
1004 asmlinkage void do_mcheck(struct pt_regs *regs)
1005 {
1006         const int field = 2 * sizeof(unsigned long);
1007         int multi_match = regs->cp0_status & ST0_TS;
1008
1009         show_regs(regs);
1010
1011         if (multi_match) {
1012                 printk("Index   : %0x\n", read_c0_index());
1013                 printk("Pagemask: %0x\n", read_c0_pagemask());
1014                 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
1015                 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
1016                 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
1017                 printk("\n");
1018                 dump_tlb_all();
1019         }
1020
1021         show_code((unsigned int __user *) regs->cp0_epc);
1022
1023         /*
1024          * Some chips may have other causes of machine check (e.g. SB1
1025          * graduation timer)
1026          */
1027         panic("Caught Machine Check exception - %scaused by multiple "
1028               "matching entries in the TLB.",
1029               (multi_match) ? "" : "not ");
1030 }
1031
1032 asmlinkage void do_mt(struct pt_regs *regs)
1033 {
1034         int subcode;
1035
1036         subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1037                         >> VPECONTROL_EXCPT_SHIFT;
1038         switch (subcode) {
1039         case 0:
1040                 printk(KERN_DEBUG "Thread Underflow\n");
1041                 break;
1042         case 1:
1043                 printk(KERN_DEBUG "Thread Overflow\n");
1044                 break;
1045         case 2:
1046                 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
1047                 break;
1048         case 3:
1049                 printk(KERN_DEBUG "Gating Storage Exception\n");
1050                 break;
1051         case 4:
1052                 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
1053                 break;
1054         case 5:
1055                 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
1056                 break;
1057         default:
1058                 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
1059                         subcode);
1060                 break;
1061         }
1062         die_if_kernel("MIPS MT Thread exception in kernel", regs);
1063
1064         force_sig(SIGILL, current);
1065 }
1066
1067
1068 asmlinkage void do_dsp(struct pt_regs *regs)
1069 {
1070         if (cpu_has_dsp)
1071                 panic("Unexpected DSP exception\n");
1072
1073         force_sig(SIGILL, current);
1074 }
1075
1076 asmlinkage void do_reserved(struct pt_regs *regs)
1077 {
1078         /*
1079          * Game over - no way to handle this if it ever occurs.  Most probably
1080          * caused by a new unknown cpu type or after another deadly
1081          * hard/software error.
1082          */
1083         show_regs(regs);
1084         panic("Caught reserved exception %ld - should not happen.",
1085               (regs->cp0_cause & 0x7f) >> 2);
1086 }
1087
1088 static int __initdata l1parity = 1;
1089 static int __init nol1parity(char *s)
1090 {
1091         l1parity = 0;
1092         return 1;
1093 }
1094 __setup("nol1par", nol1parity);
1095 static int __initdata l2parity = 1;
1096 static int __init nol2parity(char *s)
1097 {
1098         l2parity = 0;
1099         return 1;
1100 }
1101 __setup("nol2par", nol2parity);
1102
1103 /*
1104  * Some MIPS CPUs can enable/disable for cache parity detection, but do
1105  * it different ways.
1106  */
1107 static inline void parity_protection_init(void)
1108 {
1109         switch (current_cpu_type()) {
1110         case CPU_24K:
1111         case CPU_34K:
1112         case CPU_74K:
1113         case CPU_1004K:
1114                 {
1115 #define ERRCTL_PE       0x80000000
1116 #define ERRCTL_L2P      0x00800000
1117                         unsigned long errctl;
1118                         unsigned int l1parity_present, l2parity_present;
1119
1120                         errctl = read_c0_ecc();
1121                         errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1122
1123                         /* probe L1 parity support */
1124                         write_c0_ecc(errctl | ERRCTL_PE);
1125                         back_to_back_c0_hazard();
1126                         l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1127
1128                         /* probe L2 parity support */
1129                         write_c0_ecc(errctl|ERRCTL_L2P);
1130                         back_to_back_c0_hazard();
1131                         l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1132
1133                         if (l1parity_present && l2parity_present) {
1134                                 if (l1parity)
1135                                         errctl |= ERRCTL_PE;
1136                                 if (l1parity ^ l2parity)
1137                                         errctl |= ERRCTL_L2P;
1138                         } else if (l1parity_present) {
1139                                 if (l1parity)
1140                                         errctl |= ERRCTL_PE;
1141                         } else if (l2parity_present) {
1142                                 if (l2parity)
1143                                         errctl |= ERRCTL_L2P;
1144                         } else {
1145                                 /* No parity available */
1146                         }
1147
1148                         printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1149
1150                         write_c0_ecc(errctl);
1151                         back_to_back_c0_hazard();
1152                         errctl = read_c0_ecc();
1153                         printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1154
1155                         if (l1parity_present)
1156                                 printk(KERN_INFO "Cache parity protection %sabled\n",
1157                                        (errctl & ERRCTL_PE) ? "en" : "dis");
1158
1159                         if (l2parity_present) {
1160                                 if (l1parity_present && l1parity)
1161                                         errctl ^= ERRCTL_L2P;
1162                                 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1163                                        (errctl & ERRCTL_L2P) ? "en" : "dis");
1164                         }
1165                 }
1166                 break;
1167
1168         case CPU_5KC:
1169                 write_c0_ecc(0x80000000);
1170                 back_to_back_c0_hazard();
1171                 /* Set the PE bit (bit 31) in the c0_errctl register. */
1172                 printk(KERN_INFO "Cache parity protection %sabled\n",
1173                        (read_c0_ecc() & 0x80000000) ? "en" : "dis");
1174                 break;
1175         case CPU_20KC:
1176         case CPU_25KF:
1177                 /* Clear the DE bit (bit 16) in the c0_status register. */
1178                 printk(KERN_INFO "Enable cache parity protection for "
1179                        "MIPS 20KC/25KF CPUs.\n");
1180                 clear_c0_status(ST0_DE);
1181                 break;
1182         default:
1183                 break;
1184         }
1185 }
1186
1187 asmlinkage void cache_parity_error(void)
1188 {
1189         const int field = 2 * sizeof(unsigned long);
1190         unsigned int reg_val;
1191
1192         /* For the moment, report the problem and hang. */
1193         printk("Cache error exception:\n");
1194         printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1195         reg_val = read_c0_cacheerr();
1196         printk("c0_cacheerr == %08x\n", reg_val);
1197
1198         printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1199                reg_val & (1<<30) ? "secondary" : "primary",
1200                reg_val & (1<<31) ? "data" : "insn");
1201         printk("Error bits: %s%s%s%s%s%s%s\n",
1202                reg_val & (1<<29) ? "ED " : "",
1203                reg_val & (1<<28) ? "ET " : "",
1204                reg_val & (1<<26) ? "EE " : "",
1205                reg_val & (1<<25) ? "EB " : "",
1206                reg_val & (1<<24) ? "EI " : "",
1207                reg_val & (1<<23) ? "E1 " : "",
1208                reg_val & (1<<22) ? "E0 " : "");
1209         printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1210
1211 #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
1212         if (reg_val & (1<<22))
1213                 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1214
1215         if (reg_val & (1<<23))
1216                 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1217 #endif
1218
1219         panic("Can't handle the cache error!");
1220 }
1221
1222 /*
1223  * SDBBP EJTAG debug exception handler.
1224  * We skip the instruction and return to the next instruction.
1225  */
1226 void ejtag_exception_handler(struct pt_regs *regs)
1227 {
1228         const int field = 2 * sizeof(unsigned long);
1229         unsigned long depc, old_epc;
1230         unsigned int debug;
1231
1232         printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
1233         depc = read_c0_depc();
1234         debug = read_c0_debug();
1235         printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
1236         if (debug & 0x80000000) {
1237                 /*
1238                  * In branch delay slot.
1239                  * We cheat a little bit here and use EPC to calculate the
1240                  * debug return address (DEPC). EPC is restored after the
1241                  * calculation.
1242                  */
1243                 old_epc = regs->cp0_epc;
1244                 regs->cp0_epc = depc;
1245                 __compute_return_epc(regs);
1246                 depc = regs->cp0_epc;
1247                 regs->cp0_epc = old_epc;
1248         } else
1249                 depc += 4;
1250         write_c0_depc(depc);
1251
1252 #if 0
1253         printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
1254         write_c0_debug(debug | 0x100);
1255 #endif
1256 }
1257
1258 /*
1259  * NMI exception handler.
1260  */
1261 NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
1262 {
1263         bust_spinlocks(1);
1264         printk("NMI taken!!!!\n");
1265         die("NMI", regs);
1266 }
1267
1268 #define VECTORSPACING 0x100     /* for EI/VI mode */
1269
1270 unsigned long ebase;
1271 unsigned long exception_handlers[32];
1272 unsigned long vi_handlers[64];
1273
1274 /*
1275  * As a side effect of the way this is implemented we're limited
1276  * to interrupt handlers in the address range from
1277  * KSEG0 <= x < KSEG0 + 256mb on the Nevada.  Oh well ...
1278  */
1279 void *set_except_vector(int n, void *addr)
1280 {
1281         unsigned long handler = (unsigned long) addr;
1282         unsigned long old_handler = exception_handlers[n];
1283
1284         exception_handlers[n] = handler;
1285         if (n == 0 && cpu_has_divec) {
1286                 *(u32 *)(ebase + 0x200) = 0x08000000 |
1287                                           (0x03ffffff & (handler >> 2));
1288                 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
1289         }
1290         return (void *)old_handler;
1291 }
1292
1293 static asmlinkage void do_default_vi(void)
1294 {
1295         show_regs(get_irq_regs());
1296         panic("Caught unexpected vectored interrupt.");
1297 }
1298
1299 static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
1300 {
1301         unsigned long handler;
1302         unsigned long old_handler = vi_handlers[n];
1303         int srssets = current_cpu_data.srsets;
1304         u32 *w;
1305         unsigned char *b;
1306
1307         BUG_ON(!cpu_has_veic && !cpu_has_vint);
1308
1309         if (addr == NULL) {
1310                 handler = (unsigned long) do_default_vi;
1311                 srs = 0;
1312         } else
1313                 handler = (unsigned long) addr;
1314         vi_handlers[n] = (unsigned long) addr;
1315
1316         b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1317
1318         if (srs >= srssets)
1319                 panic("Shadow register set %d not supported", srs);
1320
1321         if (cpu_has_veic) {
1322                 if (board_bind_eic_interrupt)
1323                         board_bind_eic_interrupt(n, srs);
1324         } else if (cpu_has_vint) {
1325                 /* SRSMap is only defined if shadow sets are implemented */
1326                 if (srssets > 1)
1327                         change_c0_srsmap(0xf << n*4, srs << n*4);
1328         }
1329
1330         if (srs == 0) {
1331                 /*
1332                  * If no shadow set is selected then use the default handler
1333                  * that does normal register saving and a standard interrupt exit
1334                  */
1335
1336                 extern char except_vec_vi, except_vec_vi_lui;
1337                 extern char except_vec_vi_ori, except_vec_vi_end;
1338                 extern char rollback_except_vec_vi;
1339                 char *vec_start = (cpu_wait == r4k_wait) ?
1340                         &rollback_except_vec_vi : &except_vec_vi;
1341 #ifdef CONFIG_MIPS_MT_SMTC
1342                 /*
1343                  * We need to provide the SMTC vectored interrupt handler
1344                  * not only with the address of the handler, but with the
1345                  * Status.IM bit to be masked before going there.
1346                  */
1347                 extern char except_vec_vi_mori;
1348                 const int mori_offset = &except_vec_vi_mori - vec_start;
1349 #endif /* CONFIG_MIPS_MT_SMTC */
1350                 const int handler_len = &except_vec_vi_end - vec_start;
1351                 const int lui_offset = &except_vec_vi_lui - vec_start;
1352                 const int ori_offset = &except_vec_vi_ori - vec_start;
1353
1354                 if (handler_len > VECTORSPACING) {
1355                         /*
1356                          * Sigh... panicing won't help as the console
1357                          * is probably not configured :(
1358                          */
1359                         panic("VECTORSPACING too small");
1360                 }
1361
1362                 memcpy(b, vec_start, handler_len);
1363 #ifdef CONFIG_MIPS_MT_SMTC
1364                 BUG_ON(n > 7);  /* Vector index %d exceeds SMTC maximum. */
1365
1366                 w = (u32 *)(b + mori_offset);
1367                 *w = (*w & 0xffff0000) | (0x100 << n);
1368 #endif /* CONFIG_MIPS_MT_SMTC */
1369                 w = (u32 *)(b + lui_offset);
1370                 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1371                 w = (u32 *)(b + ori_offset);
1372                 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1373                 local_flush_icache_range((unsigned long)b,
1374                                          (unsigned long)(b+handler_len));
1375         }
1376         else {
1377                 /*
1378                  * In other cases jump directly to the interrupt handler
1379                  *
1380                  * It is the handlers responsibility to save registers if required
1381                  * (eg hi/lo) and return from the exception using "eret"
1382                  */
1383                 w = (u32 *)b;
1384                 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1385                 *w = 0;
1386                 local_flush_icache_range((unsigned long)b,
1387                                          (unsigned long)(b+8));
1388         }
1389
1390         return (void *)old_handler;
1391 }
1392
1393 void *set_vi_handler(int n, vi_handler_t addr)
1394 {
1395         return set_vi_srs_handler(n, addr, 0);
1396 }
1397
1398 extern void cpu_cache_init(void);
1399 extern void tlb_init(void);
1400 extern void flush_tlb_handlers(void);
1401
1402 /*
1403  * Timer interrupt
1404  */
1405 int cp0_compare_irq;
1406
1407 /*
1408  * Performance counter IRQ or -1 if shared with timer
1409  */
1410 int cp0_perfcount_irq;
1411 EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1412
1413 static int __cpuinitdata noulri;
1414
1415 static int __init ulri_disable(char *s)
1416 {
1417         pr_info("Disabling ulri\n");
1418         noulri = 1;
1419
1420         return 1;
1421 }
1422 __setup("noulri", ulri_disable);
1423
1424 void __cpuinit per_cpu_trap_init(void)
1425 {
1426         unsigned int cpu = smp_processor_id();
1427         unsigned int status_set = ST0_CU0;
1428 #ifdef CONFIG_MIPS_MT_SMTC
1429         int secondaryTC = 0;
1430         int bootTC = (cpu == 0);
1431
1432         /*
1433          * Only do per_cpu_trap_init() for first TC of Each VPE.
1434          * Note that this hack assumes that the SMTC init code
1435          * assigns TCs consecutively and in ascending order.
1436          */
1437
1438         if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1439             ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1440                 secondaryTC = 1;
1441 #endif /* CONFIG_MIPS_MT_SMTC */
1442
1443         /*
1444          * Disable coprocessors and select 32-bit or 64-bit addressing
1445          * and the 16/32 or 32/32 FPR register model.  Reset the BEV
1446          * flag that some firmware may have left set and the TS bit (for
1447          * IP27).  Set XX for ISA IV code to work.
1448          */
1449 #ifdef CONFIG_64BIT
1450         status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1451 #endif
1452         if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1453                 status_set |= ST0_XX;
1454         if (cpu_has_dsp)
1455                 status_set |= ST0_MX;
1456
1457         change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1458                          status_set);
1459
1460         if (cpu_has_mips_r2) {
1461                 unsigned int enable = 0x0000000f | cpu_hwrena_impl_bits;
1462
1463                 if (!noulri && cpu_has_userlocal)
1464                         enable |= (1 << 29);
1465
1466                 write_c0_hwrena(enable);
1467         }
1468
1469 #ifdef CONFIG_MIPS_MT_SMTC
1470         if (!secondaryTC) {
1471 #endif /* CONFIG_MIPS_MT_SMTC */
1472
1473         if (cpu_has_veic || cpu_has_vint) {
1474                 unsigned long sr = set_c0_status(ST0_BEV);
1475                 write_c0_ebase(ebase);
1476                 write_c0_status(sr);
1477                 /* Setting vector spacing enables EI/VI mode  */
1478                 change_c0_intctl(0x3e0, VECTORSPACING);
1479         }
1480         if (cpu_has_divec) {
1481                 if (cpu_has_mipsmt) {
1482                         unsigned int vpflags = dvpe();
1483                         set_c0_cause(CAUSEF_IV);
1484                         evpe(vpflags);
1485                 } else
1486                         set_c0_cause(CAUSEF_IV);
1487         }
1488
1489         /*
1490          * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1491          *
1492          *  o read IntCtl.IPTI to determine the timer interrupt
1493          *  o read IntCtl.IPPCI to determine the performance counter interrupt
1494          */
1495         if (cpu_has_mips_r2) {
1496                 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1497                 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
1498                 if (cp0_perfcount_irq == cp0_compare_irq)
1499                         cp0_perfcount_irq = -1;
1500         } else {
1501                 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
1502                 cp0_perfcount_irq = -1;
1503         }
1504
1505 #ifdef CONFIG_MIPS_MT_SMTC
1506         }
1507 #endif /* CONFIG_MIPS_MT_SMTC */
1508
1509         cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1510         TLBMISS_HANDLER_SETUP();
1511
1512         atomic_inc(&init_mm.mm_count);
1513         current->active_mm = &init_mm;
1514         BUG_ON(current->mm);
1515         enter_lazy_tlb(&init_mm, current);
1516
1517 #ifdef CONFIG_MIPS_MT_SMTC
1518         if (bootTC) {
1519 #endif /* CONFIG_MIPS_MT_SMTC */
1520                 cpu_cache_init();
1521                 tlb_init();
1522 #ifdef CONFIG_MIPS_MT_SMTC
1523         } else if (!secondaryTC) {
1524                 /*
1525                  * First TC in non-boot VPE must do subset of tlb_init()
1526                  * for MMU countrol registers.
1527                  */
1528                 write_c0_pagemask(PM_DEFAULT_MASK);
1529                 write_c0_wired(0);
1530         }
1531 #endif /* CONFIG_MIPS_MT_SMTC */
1532 }
1533
1534 /* Install CPU exception handler */
1535 void __init set_handler(unsigned long offset, void *addr, unsigned long size)
1536 {
1537         memcpy((void *)(ebase + offset), addr, size);
1538         local_flush_icache_range(ebase + offset, ebase + offset + size);
1539 }
1540
1541 static char panic_null_cerr[] __cpuinitdata =
1542         "Trying to set NULL cache error exception handler";
1543
1544 /*
1545  * Install uncached CPU exception handler.
1546  * This is suitable only for the cache error exception which is the only
1547  * exception handler that is being run uncached.
1548  */
1549 void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1550         unsigned long size)
1551 {
1552 #ifdef CONFIG_32BIT
1553         unsigned long uncached_ebase = KSEG1ADDR(ebase);
1554 #endif
1555 #ifdef CONFIG_64BIT
1556         unsigned long uncached_ebase = TO_UNCAC(ebase);
1557 #endif
1558
1559         if (!addr)
1560                 panic(panic_null_cerr);
1561
1562         memcpy((void *)(uncached_ebase + offset), addr, size);
1563 }
1564
1565 static int __initdata rdhwr_noopt;
1566 static int __init set_rdhwr_noopt(char *str)
1567 {
1568         rdhwr_noopt = 1;
1569         return 1;
1570 }
1571
1572 __setup("rdhwr_noopt", set_rdhwr_noopt);
1573
1574 void __init trap_init(void)
1575 {
1576         extern char except_vec3_generic, except_vec3_r4000;
1577         extern char except_vec4;
1578         unsigned long i;
1579         int rollback;
1580
1581         check_wait();
1582         rollback = (cpu_wait == r4k_wait);
1583
1584 #if defined(CONFIG_KGDB)
1585         if (kgdb_early_setup)
1586                 return; /* Already done */
1587 #endif
1588
1589         if (cpu_has_veic || cpu_has_vint) {
1590                 unsigned long size = 0x200 + VECTORSPACING*64;
1591                 ebase = (unsigned long)
1592                         __alloc_bootmem(size, 1 << fls(size), 0);
1593         } else {
1594                 ebase = CAC_BASE;
1595                 if (cpu_has_mips_r2)
1596                         ebase += (read_c0_ebase() & 0x3ffff000);
1597         }
1598
1599         per_cpu_trap_init();
1600
1601         /*
1602          * Copy the generic exception handlers to their final destination.
1603          * This will be overriden later as suitable for a particular
1604          * configuration.
1605          */
1606         set_handler(0x180, &except_vec3_generic, 0x80);
1607
1608         /*
1609          * Setup default vectors
1610          */
1611         for (i = 0; i <= 31; i++)
1612                 set_except_vector(i, handle_reserved);
1613
1614         /*
1615          * Copy the EJTAG debug exception vector handler code to it's final
1616          * destination.
1617          */
1618         if (cpu_has_ejtag && board_ejtag_handler_setup)
1619                 board_ejtag_handler_setup();
1620
1621         /*
1622          * Only some CPUs have the watch exceptions.
1623          */
1624         if (cpu_has_watch)
1625                 set_except_vector(23, handle_watch);
1626
1627         /*
1628          * Initialise interrupt handlers
1629          */
1630         if (cpu_has_veic || cpu_has_vint) {
1631                 int nvec = cpu_has_veic ? 64 : 8;
1632                 for (i = 0; i < nvec; i++)
1633                         set_vi_handler(i, NULL);
1634         }
1635         else if (cpu_has_divec)
1636                 set_handler(0x200, &except_vec4, 0x8);
1637
1638         /*
1639          * Some CPUs can enable/disable for cache parity detection, but does
1640          * it different ways.
1641          */
1642         parity_protection_init();
1643
1644         /*
1645          * The Data Bus Errors / Instruction Bus Errors are signaled
1646          * by external hardware.  Therefore these two exceptions
1647          * may have board specific handlers.
1648          */
1649         if (board_be_init)
1650                 board_be_init();
1651
1652         set_except_vector(0, rollback ? rollback_handle_int : handle_int);
1653         set_except_vector(1, handle_tlbm);
1654         set_except_vector(2, handle_tlbl);
1655         set_except_vector(3, handle_tlbs);
1656
1657         set_except_vector(4, handle_adel);
1658         set_except_vector(5, handle_ades);
1659
1660         set_except_vector(6, handle_ibe);
1661         set_except_vector(7, handle_dbe);
1662
1663         set_except_vector(8, handle_sys);
1664         set_except_vector(9, handle_bp);
1665         set_except_vector(10, rdhwr_noopt ? handle_ri :
1666                           (cpu_has_vtag_icache ?
1667                            handle_ri_rdhwr_vivt : handle_ri_rdhwr));
1668         set_except_vector(11, handle_cpu);
1669         set_except_vector(12, handle_ov);
1670         set_except_vector(13, handle_tr);
1671
1672         if (current_cpu_type() == CPU_R6000 ||
1673             current_cpu_type() == CPU_R6000A) {
1674                 /*
1675                  * The R6000 is the only R-series CPU that features a machine
1676                  * check exception (similar to the R4000 cache error) and
1677                  * unaligned ldc1/sdc1 exception.  The handlers have not been
1678                  * written yet.  Well, anyway there is no R6000 machine on the
1679                  * current list of targets for Linux/MIPS.
1680                  * (Duh, crap, there is someone with a triple R6k machine)
1681                  */
1682                 //set_except_vector(14, handle_mc);
1683                 //set_except_vector(15, handle_ndc);
1684         }
1685
1686
1687         if (board_nmi_handler_setup)
1688                 board_nmi_handler_setup();
1689
1690         if (cpu_has_fpu && !cpu_has_nofpuex)
1691                 set_except_vector(15, handle_fpe);
1692
1693         set_except_vector(22, handle_mdmx);
1694
1695         if (cpu_has_mcheck)
1696                 set_except_vector(24, handle_mcheck);
1697
1698         if (cpu_has_mipsmt)
1699                 set_except_vector(25, handle_mt);
1700
1701         set_except_vector(26, handle_dsp);
1702
1703         if (cpu_has_vce)
1704                 /* Special exception: R4[04]00 uses also the divec space. */
1705                 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
1706         else if (cpu_has_4kex)
1707                 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
1708         else
1709                 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
1710
1711         local_flush_icache_range(ebase, ebase + 0x400);
1712         flush_tlb_handlers();
1713
1714         sort_extable(__start___dbe_table, __stop___dbe_table);
1715
1716         register_cu2_notifier(&default_cu2_notifier);
1717 }