More AP / SP bits for the 34K, the Malta bits and things. Still wants
[safe/jmp/linux-2.6] / 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 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  Maciej W. Rozycki
13  */
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/spinlock.h>
22 #include <linux/kallsyms.h>
23 #include <linux/bootmem.h>
24
25 #include <asm/bootinfo.h>
26 #include <asm/branch.h>
27 #include <asm/break.h>
28 #include <asm/cpu.h>
29 #include <asm/dsp.h>
30 #include <asm/fpu.h>
31 #include <asm/module.h>
32 #include <asm/pgtable.h>
33 #include <asm/ptrace.h>
34 #include <asm/sections.h>
35 #include <asm/system.h>
36 #include <asm/tlbdebug.h>
37 #include <asm/traps.h>
38 #include <asm/uaccess.h>
39 #include <asm/mmu_context.h>
40 #include <asm/watch.h>
41 #include <asm/types.h>
42
43 extern asmlinkage void handle_tlbm(void);
44 extern asmlinkage void handle_tlbl(void);
45 extern asmlinkage void handle_tlbs(void);
46 extern asmlinkage void handle_adel(void);
47 extern asmlinkage void handle_ades(void);
48 extern asmlinkage void handle_ibe(void);
49 extern asmlinkage void handle_dbe(void);
50 extern asmlinkage void handle_sys(void);
51 extern asmlinkage void handle_bp(void);
52 extern asmlinkage void handle_ri(void);
53 extern asmlinkage void handle_cpu(void);
54 extern asmlinkage void handle_ov(void);
55 extern asmlinkage void handle_tr(void);
56 extern asmlinkage void handle_fpe(void);
57 extern asmlinkage void handle_mdmx(void);
58 extern asmlinkage void handle_watch(void);
59 extern asmlinkage void handle_dsp(void);
60 extern asmlinkage void handle_mcheck(void);
61 extern asmlinkage void handle_reserved(void);
62
63 extern int fpu_emulator_cop1Handler(int xcptno, struct pt_regs *xcp,
64         struct mips_fpu_soft_struct *ctx);
65
66 void (*board_be_init)(void);
67 int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
68 void (*board_nmi_handler_setup)(void);
69 void (*board_ejtag_handler_setup)(void);
70 void (*board_bind_eic_interrupt)(int irq, int regset);
71
72 /*
73  * These constant is for searching for possible module text segments.
74  * MODULE_RANGE is a guess of how much space is likely to be vmalloced.
75  */
76 #define MODULE_RANGE (8*1024*1024)
77
78 /*
79  * This routine abuses get_user()/put_user() to reference pointers
80  * with at least a bit of error checking ...
81  */
82 void show_stack(struct task_struct *task, unsigned long *sp)
83 {
84         const int field = 2 * sizeof(unsigned long);
85         long stackdata;
86         int i;
87
88         if (!sp) {
89                 if (task && task != current)
90                         sp = (unsigned long *) task->thread.reg29;
91                 else
92                         sp = (unsigned long *) &sp;
93         }
94
95         printk("Stack :");
96         i = 0;
97         while ((unsigned long) sp & (PAGE_SIZE - 1)) {
98                 if (i && ((i % (64 / field)) == 0))
99                         printk("\n       ");
100                 if (i > 39) {
101                         printk(" ...");
102                         break;
103                 }
104
105                 if (__get_user(stackdata, sp++)) {
106                         printk(" (Bad stack address)");
107                         break;
108                 }
109
110                 printk(" %0*lx", field, stackdata);
111                 i++;
112         }
113         printk("\n");
114 }
115
116 void show_trace(struct task_struct *task, unsigned long *stack)
117 {
118         const int field = 2 * sizeof(unsigned long);
119         unsigned long addr;
120
121         if (!stack) {
122                 if (task && task != current)
123                         stack = (unsigned long *) task->thread.reg29;
124                 else
125                         stack = (unsigned long *) &stack;
126         }
127
128         printk("Call Trace:");
129 #ifdef CONFIG_KALLSYMS
130         printk("\n");
131 #endif
132         while (!kstack_end(stack)) {
133                 addr = *stack++;
134                 if (__kernel_text_address(addr)) {
135                         printk(" [<%0*lx>] ", field, addr);
136                         print_symbol("%s\n", addr);
137                 }
138         }
139         printk("\n");
140 }
141
142 /*
143  * The architecture-independent dump_stack generator
144  */
145 void dump_stack(void)
146 {
147         unsigned long stack;
148
149         show_trace(current, &stack);
150 }
151
152 EXPORT_SYMBOL(dump_stack);
153
154 void show_code(unsigned int *pc)
155 {
156         long i;
157
158         printk("\nCode:");
159
160         for(i = -3 ; i < 6 ; i++) {
161                 unsigned int insn;
162                 if (__get_user(insn, pc + i)) {
163                         printk(" (Bad address in epc)\n");
164                         break;
165                 }
166                 printk("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
167         }
168 }
169
170 void show_regs(struct pt_regs *regs)
171 {
172         const int field = 2 * sizeof(unsigned long);
173         unsigned int cause = regs->cp0_cause;
174         int i;
175
176         printk("Cpu %d\n", smp_processor_id());
177
178         /*
179          * Saved main processor registers
180          */
181         for (i = 0; i < 32; ) {
182                 if ((i % 4) == 0)
183                         printk("$%2d   :", i);
184                 if (i == 0)
185                         printk(" %0*lx", field, 0UL);
186                 else if (i == 26 || i == 27)
187                         printk(" %*s", field, "");
188                 else
189                         printk(" %0*lx", field, regs->regs[i]);
190
191                 i++;
192                 if ((i % 4) == 0)
193                         printk("\n");
194         }
195
196         printk("Hi    : %0*lx\n", field, regs->hi);
197         printk("Lo    : %0*lx\n", field, regs->lo);
198
199         /*
200          * Saved cp0 registers
201          */
202         printk("epc   : %0*lx ", field, regs->cp0_epc);
203         print_symbol("%s ", regs->cp0_epc);
204         printk("    %s\n", print_tainted());
205         printk("ra    : %0*lx ", field, regs->regs[31]);
206         print_symbol("%s\n", regs->regs[31]);
207
208         printk("Status: %08x    ", (uint32_t) regs->cp0_status);
209
210         if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
211                 if (regs->cp0_status & ST0_KUO)
212                         printk("KUo ");
213                 if (regs->cp0_status & ST0_IEO)
214                         printk("IEo ");
215                 if (regs->cp0_status & ST0_KUP)
216                         printk("KUp ");
217                 if (regs->cp0_status & ST0_IEP)
218                         printk("IEp ");
219                 if (regs->cp0_status & ST0_KUC)
220                         printk("KUc ");
221                 if (regs->cp0_status & ST0_IEC)
222                         printk("IEc ");
223         } else {
224                 if (regs->cp0_status & ST0_KX)
225                         printk("KX ");
226                 if (regs->cp0_status & ST0_SX)
227                         printk("SX ");
228                 if (regs->cp0_status & ST0_UX)
229                         printk("UX ");
230                 switch (regs->cp0_status & ST0_KSU) {
231                 case KSU_USER:
232                         printk("USER ");
233                         break;
234                 case KSU_SUPERVISOR:
235                         printk("SUPERVISOR ");
236                         break;
237                 case KSU_KERNEL:
238                         printk("KERNEL ");
239                         break;
240                 default:
241                         printk("BAD_MODE ");
242                         break;
243                 }
244                 if (regs->cp0_status & ST0_ERL)
245                         printk("ERL ");
246                 if (regs->cp0_status & ST0_EXL)
247                         printk("EXL ");
248                 if (regs->cp0_status & ST0_IE)
249                         printk("IE ");
250         }
251         printk("\n");
252
253         printk("Cause : %08x\n", cause);
254
255         cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
256         if (1 <= cause && cause <= 5)
257                 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
258
259         printk("PrId  : %08x\n", read_c0_prid());
260 }
261
262 void show_registers(struct pt_regs *regs)
263 {
264         show_regs(regs);
265         print_modules();
266         printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
267                 current->comm, current->pid, current_thread_info(), current);
268         show_stack(current, (long *) regs->regs[29]);
269         show_trace(current, (long *) regs->regs[29]);
270         show_code((unsigned int *) regs->cp0_epc);
271         printk("\n");
272 }
273
274 static DEFINE_SPINLOCK(die_lock);
275
276 NORET_TYPE void ATTRIB_NORET __die(const char * str, struct pt_regs * regs,
277                                    const char * file, const char * func,
278                                    unsigned long line)
279 {
280         static int die_counter;
281
282         console_verbose();
283         spin_lock_irq(&die_lock);
284         printk("%s", str);
285         if (file && func)
286                 printk(" in %s:%s, line %ld", file, func, line);
287         printk("[#%d]:\n", ++die_counter);
288         show_registers(regs);
289         spin_unlock_irq(&die_lock);
290         do_exit(SIGSEGV);
291 }
292
293 void __die_if_kernel(const char * str, struct pt_regs * regs,
294                      const char * file, const char * func, unsigned long line)
295 {
296         if (!user_mode(regs))
297                 __die(str, regs, file, func, line);
298 }
299
300 extern const struct exception_table_entry __start___dbe_table[];
301 extern const struct exception_table_entry __stop___dbe_table[];
302
303 void __declare_dbe_table(void)
304 {
305         __asm__ __volatile__(
306         ".section\t__dbe_table,\"a\"\n\t"
307         ".previous"
308         );
309 }
310
311 /* Given an address, look for it in the exception tables. */
312 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
313 {
314         const struct exception_table_entry *e;
315
316         e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
317         if (!e)
318                 e = search_module_dbetables(addr);
319         return e;
320 }
321
322 asmlinkage void do_be(struct pt_regs *regs)
323 {
324         const int field = 2 * sizeof(unsigned long);
325         const struct exception_table_entry *fixup = NULL;
326         int data = regs->cp0_cause & 4;
327         int action = MIPS_BE_FATAL;
328
329         /* XXX For now.  Fixme, this searches the wrong table ...  */
330         if (data && !user_mode(regs))
331                 fixup = search_dbe_tables(exception_epc(regs));
332
333         if (fixup)
334                 action = MIPS_BE_FIXUP;
335
336         if (board_be_handler)
337                 action = board_be_handler(regs, fixup != 0);
338
339         switch (action) {
340         case MIPS_BE_DISCARD:
341                 return;
342         case MIPS_BE_FIXUP:
343                 if (fixup) {
344                         regs->cp0_epc = fixup->nextinsn;
345                         return;
346                 }
347                 break;
348         default:
349                 break;
350         }
351
352         /*
353          * Assume it would be too dangerous to continue ...
354          */
355         printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
356                data ? "Data" : "Instruction",
357                field, regs->cp0_epc, field, regs->regs[31]);
358         die_if_kernel("Oops", regs);
359         force_sig(SIGBUS, current);
360 }
361
362 static inline int get_insn_opcode(struct pt_regs *regs, unsigned int *opcode)
363 {
364         unsigned int __user *epc;
365
366         epc = (unsigned int __user *) regs->cp0_epc +
367               ((regs->cp0_cause & CAUSEF_BD) != 0);
368         if (!get_user(*opcode, epc))
369                 return 0;
370
371         force_sig(SIGSEGV, current);
372         return 1;
373 }
374
375 /*
376  * ll/sc emulation
377  */
378
379 #define OPCODE 0xfc000000
380 #define BASE   0x03e00000
381 #define RT     0x001f0000
382 #define OFFSET 0x0000ffff
383 #define LL     0xc0000000
384 #define SC     0xe0000000
385 #define SPEC3  0x7c000000
386 #define RD     0x0000f800
387 #define FUNC   0x0000003f
388 #define RDHWR  0x0000003b
389
390 /*
391  * The ll_bit is cleared by r*_switch.S
392  */
393
394 unsigned long ll_bit;
395
396 static struct task_struct *ll_task = NULL;
397
398 static inline void simulate_ll(struct pt_regs *regs, unsigned int opcode)
399 {
400         unsigned long value, __user *vaddr;
401         long offset;
402         int signal = 0;
403
404         /*
405          * analyse the ll instruction that just caused a ri exception
406          * and put the referenced address to addr.
407          */
408
409         /* sign extend offset */
410         offset = opcode & OFFSET;
411         offset <<= 16;
412         offset >>= 16;
413
414         vaddr = (unsigned long __user *)
415                 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
416
417         if ((unsigned long)vaddr & 3) {
418                 signal = SIGBUS;
419                 goto sig;
420         }
421         if (get_user(value, vaddr)) {
422                 signal = SIGSEGV;
423                 goto sig;
424         }
425
426         preempt_disable();
427
428         if (ll_task == NULL || ll_task == current) {
429                 ll_bit = 1;
430         } else {
431                 ll_bit = 0;
432         }
433         ll_task = current;
434
435         preempt_enable();
436
437         compute_return_epc(regs);
438
439         regs->regs[(opcode & RT) >> 16] = value;
440
441         return;
442
443 sig:
444         force_sig(signal, current);
445 }
446
447 static inline void simulate_sc(struct pt_regs *regs, unsigned int opcode)
448 {
449         unsigned long __user *vaddr;
450         unsigned long reg;
451         long offset;
452         int signal = 0;
453
454         /*
455          * analyse the sc instruction that just caused a ri exception
456          * and put the referenced address to addr.
457          */
458
459         /* sign extend offset */
460         offset = opcode & OFFSET;
461         offset <<= 16;
462         offset >>= 16;
463
464         vaddr = (unsigned long __user *)
465                 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
466         reg = (opcode & RT) >> 16;
467
468         if ((unsigned long)vaddr & 3) {
469                 signal = SIGBUS;
470                 goto sig;
471         }
472
473         preempt_disable();
474
475         if (ll_bit == 0 || ll_task != current) {
476                 compute_return_epc(regs);
477                 regs->regs[reg] = 0;
478                 preempt_enable();
479                 return;
480         }
481
482         preempt_enable();
483
484         if (put_user(regs->regs[reg], vaddr)) {
485                 signal = SIGSEGV;
486                 goto sig;
487         }
488
489         compute_return_epc(regs);
490         regs->regs[reg] = 1;
491
492         return;
493
494 sig:
495         force_sig(signal, current);
496 }
497
498 /*
499  * ll uses the opcode of lwc0 and sc uses the opcode of swc0.  That is both
500  * opcodes are supposed to result in coprocessor unusable exceptions if
501  * executed on ll/sc-less processors.  That's the theory.  In practice a
502  * few processors such as NEC's VR4100 throw reserved instruction exceptions
503  * instead, so we're doing the emulation thing in both exception handlers.
504  */
505 static inline int simulate_llsc(struct pt_regs *regs)
506 {
507         unsigned int opcode;
508
509         if (unlikely(get_insn_opcode(regs, &opcode)))
510                 return -EFAULT;
511
512         if ((opcode & OPCODE) == LL) {
513                 simulate_ll(regs, opcode);
514                 return 0;
515         }
516         if ((opcode & OPCODE) == SC) {
517                 simulate_sc(regs, opcode);
518                 return 0;
519         }
520
521         return -EFAULT;                 /* Strange things going on ... */
522 }
523
524 /*
525  * Simulate trapping 'rdhwr' instructions to provide user accessible
526  * registers not implemented in hardware.  The only current use of this
527  * is the thread area pointer.
528  */
529 static inline int simulate_rdhwr(struct pt_regs *regs)
530 {
531         struct thread_info *ti = current->thread_info;
532         unsigned int opcode;
533
534         if (unlikely(get_insn_opcode(regs, &opcode)))
535                 return -EFAULT;
536
537         if (unlikely(compute_return_epc(regs)))
538                 return -EFAULT;
539
540         if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
541                 int rd = (opcode & RD) >> 11;
542                 int rt = (opcode & RT) >> 16;
543                 switch (rd) {
544                         case 29:
545                                 regs->regs[rt] = ti->tp_value;
546                                 break;
547                         default:
548                                 return -EFAULT;
549                 }
550         }
551
552         return 0;
553 }
554
555 asmlinkage void do_ov(struct pt_regs *regs)
556 {
557         siginfo_t info;
558
559         info.si_code = FPE_INTOVF;
560         info.si_signo = SIGFPE;
561         info.si_errno = 0;
562         info.si_addr = (void __user *) regs->cp0_epc;
563         force_sig_info(SIGFPE, &info, current);
564 }
565
566 /*
567  * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
568  */
569 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
570 {
571         if (fcr31 & FPU_CSR_UNI_X) {
572                 int sig;
573
574                 preempt_disable();
575
576 #ifdef CONFIG_PREEMPT
577                 if (!is_fpu_owner()) {
578                         /* We might lose fpu before disabling preempt... */
579                         own_fpu();
580                         BUG_ON(!used_math());
581                         restore_fp(current);
582                 }
583 #endif
584                 /*
585                  * Unimplemented operation exception.  If we've got the full
586                  * software emulator on-board, let's use it...
587                  *
588                  * Force FPU to dump state into task/thread context.  We're
589                  * moving a lot of data here for what is probably a single
590                  * instruction, but the alternative is to pre-decode the FP
591                  * register operands before invoking the emulator, which seems
592                  * a bit extreme for what should be an infrequent event.
593                  */
594                 save_fp(current);
595                 /* Ensure 'resume' not overwrite saved fp context again. */
596                 lose_fpu();
597
598                 preempt_enable();
599
600                 /* Run the emulator */
601                 sig = fpu_emulator_cop1Handler (0, regs,
602                         &current->thread.fpu.soft);
603
604                 preempt_disable();
605
606                 own_fpu();      /* Using the FPU again.  */
607                 /*
608                  * We can't allow the emulated instruction to leave any of
609                  * the cause bit set in $fcr31.
610                  */
611                 current->thread.fpu.soft.fcr31 &= ~FPU_CSR_ALL_X;
612
613                 /* Restore the hardware register state */
614                 restore_fp(current);
615
616                 preempt_enable();
617
618                 /* If something went wrong, signal */
619                 if (sig)
620                         force_sig(sig, current);
621
622                 return;
623         }
624
625         force_sig(SIGFPE, current);
626 }
627
628 asmlinkage void do_bp(struct pt_regs *regs)
629 {
630         unsigned int opcode, bcode;
631         siginfo_t info;
632
633         die_if_kernel("Break instruction in kernel code", regs);
634
635         if (get_insn_opcode(regs, &opcode))
636                 return;
637
638         /*
639          * There is the ancient bug in the MIPS assemblers that the break
640          * code starts left to bit 16 instead to bit 6 in the opcode.
641          * Gas is bug-compatible, but not always, grrr...
642          * We handle both cases with a simple heuristics.  --macro
643          */
644         bcode = ((opcode >> 6) & ((1 << 20) - 1));
645         if (bcode < (1 << 10))
646                 bcode <<= 10;
647
648         /*
649          * (A short test says that IRIX 5.3 sends SIGTRAP for all break
650          * insns, even for break codes that indicate arithmetic failures.
651          * Weird ...)
652          * But should we continue the brokenness???  --macro
653          */
654         switch (bcode) {
655         case BRK_OVERFLOW << 10:
656         case BRK_DIVZERO << 10:
657                 if (bcode == (BRK_DIVZERO << 10))
658                         info.si_code = FPE_INTDIV;
659                 else
660                         info.si_code = FPE_INTOVF;
661                 info.si_signo = SIGFPE;
662                 info.si_errno = 0;
663                 info.si_addr = (void __user *) regs->cp0_epc;
664                 force_sig_info(SIGFPE, &info, current);
665                 break;
666         default:
667                 force_sig(SIGTRAP, current);
668         }
669 }
670
671 asmlinkage void do_tr(struct pt_regs *regs)
672 {
673         unsigned int opcode, tcode = 0;
674         siginfo_t info;
675
676         die_if_kernel("Trap instruction in kernel code", regs);
677
678         if (get_insn_opcode(regs, &opcode))
679                 return;
680
681         /* Immediate versions don't provide a code.  */
682         if (!(opcode & OPCODE))
683                 tcode = ((opcode >> 6) & ((1 << 10) - 1));
684
685         /*
686          * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
687          * insns, even for trap codes that indicate arithmetic failures.
688          * Weird ...)
689          * But should we continue the brokenness???  --macro
690          */
691         switch (tcode) {
692         case BRK_OVERFLOW:
693         case BRK_DIVZERO:
694                 if (tcode == BRK_DIVZERO)
695                         info.si_code = FPE_INTDIV;
696                 else
697                         info.si_code = FPE_INTOVF;
698                 info.si_signo = SIGFPE;
699                 info.si_errno = 0;
700                 info.si_addr = (void __user *) regs->cp0_epc;
701                 force_sig_info(SIGFPE, &info, current);
702                 break;
703         default:
704                 force_sig(SIGTRAP, current);
705         }
706 }
707
708 asmlinkage void do_ri(struct pt_regs *regs)
709 {
710         die_if_kernel("Reserved instruction in kernel code", regs);
711
712         if (!cpu_has_llsc)
713                 if (!simulate_llsc(regs))
714                         return;
715
716         if (!simulate_rdhwr(regs))
717                 return;
718
719         force_sig(SIGILL, current);
720 }
721
722 asmlinkage void do_cpu(struct pt_regs *regs)
723 {
724         unsigned int cpid;
725
726         die_if_kernel("do_cpu invoked from kernel context!", regs);
727
728         cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
729
730         switch (cpid) {
731         case 0:
732                 if (!cpu_has_llsc)
733                         if (!simulate_llsc(regs))
734                                 return;
735
736                 if (!simulate_rdhwr(regs))
737                         return;
738
739                 break;
740
741         case 1:
742                 preempt_disable();
743
744                 own_fpu();
745                 if (used_math()) {      /* Using the FPU again.  */
746                         restore_fp(current);
747                 } else {                        /* First time FPU user.  */
748                         init_fpu();
749                         set_used_math();
750                 }
751
752                 preempt_enable();
753
754                 if (!cpu_has_fpu) {
755                         int sig = fpu_emulator_cop1Handler(0, regs,
756                                                 &current->thread.fpu.soft);
757                         if (sig)
758                                 force_sig(sig, current);
759                 }
760
761                 return;
762
763         case 2:
764         case 3:
765                 break;
766         }
767
768         force_sig(SIGILL, current);
769 }
770
771 asmlinkage void do_mdmx(struct pt_regs *regs)
772 {
773         force_sig(SIGILL, current);
774 }
775
776 asmlinkage void do_watch(struct pt_regs *regs)
777 {
778         /*
779          * We use the watch exception where available to detect stack
780          * overflows.
781          */
782         dump_tlb_all();
783         show_regs(regs);
784         panic("Caught WATCH exception - probably caused by stack overflow.");
785 }
786
787 asmlinkage void do_mcheck(struct pt_regs *regs)
788 {
789         show_regs(regs);
790         dump_tlb_all();
791         /*
792          * Some chips may have other causes of machine check (e.g. SB1
793          * graduation timer)
794          */
795         panic("Caught Machine Check exception - %scaused by multiple "
796               "matching entries in the TLB.",
797               (regs->cp0_status & ST0_TS) ? "" : "not ");
798 }
799
800 asmlinkage void do_dsp(struct pt_regs *regs)
801 {
802         if (cpu_has_dsp)
803                 panic("Unexpected DSP exception\n");
804
805         force_sig(SIGILL, current);
806 }
807
808 asmlinkage void do_reserved(struct pt_regs *regs)
809 {
810         /*
811          * Game over - no way to handle this if it ever occurs.  Most probably
812          * caused by a new unknown cpu type or after another deadly
813          * hard/software error.
814          */
815         show_regs(regs);
816         panic("Caught reserved exception %ld - should not happen.",
817               (regs->cp0_cause & 0x7f) >> 2);
818 }
819
820 asmlinkage void do_default_vi(struct pt_regs *regs)
821 {
822         show_regs(regs);
823         panic("Caught unexpected vectored interrupt.");
824 }
825
826 /*
827  * Some MIPS CPUs can enable/disable for cache parity detection, but do
828  * it different ways.
829  */
830 static inline void parity_protection_init(void)
831 {
832         switch (current_cpu_data.cputype) {
833         case CPU_24K:
834         case CPU_5KC:
835                 write_c0_ecc(0x80000000);
836                 back_to_back_c0_hazard();
837                 /* Set the PE bit (bit 31) in the c0_errctl register. */
838                 printk(KERN_INFO "Cache parity protection %sabled\n",
839                        (read_c0_ecc() & 0x80000000) ? "en" : "dis");
840                 break;
841         case CPU_20KC:
842         case CPU_25KF:
843                 /* Clear the DE bit (bit 16) in the c0_status register. */
844                 printk(KERN_INFO "Enable cache parity protection for "
845                        "MIPS 20KC/25KF CPUs.\n");
846                 clear_c0_status(ST0_DE);
847                 break;
848         default:
849                 break;
850         }
851 }
852
853 asmlinkage void cache_parity_error(void)
854 {
855         const int field = 2 * sizeof(unsigned long);
856         unsigned int reg_val;
857
858         /* For the moment, report the problem and hang. */
859         printk("Cache error exception:\n");
860         printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
861         reg_val = read_c0_cacheerr();
862         printk("c0_cacheerr == %08x\n", reg_val);
863
864         printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
865                reg_val & (1<<30) ? "secondary" : "primary",
866                reg_val & (1<<31) ? "data" : "insn");
867         printk("Error bits: %s%s%s%s%s%s%s\n",
868                reg_val & (1<<29) ? "ED " : "",
869                reg_val & (1<<28) ? "ET " : "",
870                reg_val & (1<<26) ? "EE " : "",
871                reg_val & (1<<25) ? "EB " : "",
872                reg_val & (1<<24) ? "EI " : "",
873                reg_val & (1<<23) ? "E1 " : "",
874                reg_val & (1<<22) ? "E0 " : "");
875         printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
876
877 #if defined(CONFIG_CPU_MIPS32_R1) || defined(CONFIG_CPU_MIPS64_R1)
878         if (reg_val & (1<<22))
879                 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
880
881         if (reg_val & (1<<23))
882                 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
883 #endif
884
885         panic("Can't handle the cache error!");
886 }
887
888 /*
889  * SDBBP EJTAG debug exception handler.
890  * We skip the instruction and return to the next instruction.
891  */
892 void ejtag_exception_handler(struct pt_regs *regs)
893 {
894         const int field = 2 * sizeof(unsigned long);
895         unsigned long depc, old_epc;
896         unsigned int debug;
897
898         printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
899         depc = read_c0_depc();
900         debug = read_c0_debug();
901         printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
902         if (debug & 0x80000000) {
903                 /*
904                  * In branch delay slot.
905                  * We cheat a little bit here and use EPC to calculate the
906                  * debug return address (DEPC). EPC is restored after the
907                  * calculation.
908                  */
909                 old_epc = regs->cp0_epc;
910                 regs->cp0_epc = depc;
911                 __compute_return_epc(regs);
912                 depc = regs->cp0_epc;
913                 regs->cp0_epc = old_epc;
914         } else
915                 depc += 4;
916         write_c0_depc(depc);
917
918 #if 0
919         printk("\n\n----- Enable EJTAG single stepping ----\n\n");
920         write_c0_debug(debug | 0x100);
921 #endif
922 }
923
924 /*
925  * NMI exception handler.
926  */
927 void nmi_exception_handler(struct pt_regs *regs)
928 {
929         printk("NMI taken!!!!\n");
930         die("NMI", regs);
931         while(1) ;
932 }
933
934 #define VECTORSPACING 0x100     /* for EI/VI mode */
935
936 unsigned long ebase;
937 unsigned long exception_handlers[32];
938 unsigned long vi_handlers[64];
939
940 /*
941  * As a side effect of the way this is implemented we're limited
942  * to interrupt handlers in the address range from
943  * KSEG0 <= x < KSEG0 + 256mb on the Nevada.  Oh well ...
944  */
945 void *set_except_vector(int n, void *addr)
946 {
947         unsigned long handler = (unsigned long) addr;
948         unsigned long old_handler = exception_handlers[n];
949
950         exception_handlers[n] = handler;
951         if (n == 0 && cpu_has_divec) {
952                 *(volatile u32 *)(ebase + 0x200) = 0x08000000 |
953                                                  (0x03ffffff & (handler >> 2));
954                 flush_icache_range(ebase + 0x200, ebase + 0x204);
955         }
956         return (void *)old_handler;
957 }
958
959 #ifdef CONFIG_CPU_MIPSR2
960 /*
961  * Shadow register allocation
962  * FIXME: SMP...
963  */
964
965 /* MIPSR2 shadow register sets */
966 struct shadow_registers {
967         spinlock_t sr_lock;     /*  */
968         int sr_supported;       /* Number of shadow register sets supported */
969         int sr_allocated;       /* Bitmap of allocated shadow registers */
970 } shadow_registers;
971
972 void mips_srs_init(void)
973 {
974 #ifdef CONFIG_CPU_MIPSR2_SRS
975         shadow_registers.sr_supported = ((read_c0_srsctl() >> 26) & 0x0f) + 1;
976         printk ("%d MIPSR2 register sets available\n", shadow_registers.sr_supported);
977 #else
978         shadow_registers.sr_supported = 1;
979 #endif
980         shadow_registers.sr_allocated = 1;      /* Set 0 used by kernel */
981         spin_lock_init(&shadow_registers.sr_lock);
982 }
983
984 int mips_srs_max(void)
985 {
986         return shadow_registers.sr_supported;
987 }
988
989 int mips_srs_alloc (void)
990 {
991         struct shadow_registers *sr = &shadow_registers;
992         unsigned long flags;
993         int set;
994
995         spin_lock_irqsave(&sr->sr_lock, flags);
996
997         for (set = 0; set < sr->sr_supported; set++) {
998                 if ((sr->sr_allocated & (1 << set)) == 0) {
999                         sr->sr_allocated |= 1 << set;
1000                         spin_unlock_irqrestore(&sr->sr_lock, flags);
1001                         return set;
1002                 }
1003         }
1004
1005         /* None available */
1006         spin_unlock_irqrestore(&sr->sr_lock, flags);
1007         return -1;
1008 }
1009
1010 void mips_srs_free (int set)
1011 {
1012         struct shadow_registers *sr = &shadow_registers;
1013         unsigned long flags;
1014
1015         spin_lock_irqsave(&sr->sr_lock, flags);
1016         sr->sr_allocated &= ~(1 << set);
1017         spin_unlock_irqrestore(&sr->sr_lock, flags);
1018 }
1019
1020 void *set_vi_srs_handler (int n, void *addr, int srs)
1021 {
1022         unsigned long handler;
1023         unsigned long old_handler = vi_handlers[n];
1024         u32 *w;
1025         unsigned char *b;
1026
1027         if (!cpu_has_veic && !cpu_has_vint)
1028                 BUG();
1029
1030         if (addr == NULL) {
1031                 handler = (unsigned long) do_default_vi;
1032                 srs = 0;
1033         }
1034         else
1035                 handler = (unsigned long) addr;
1036         vi_handlers[n] = (unsigned long) addr;
1037
1038         b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1039
1040         if (srs >= mips_srs_max())
1041                 panic("Shadow register set %d not supported", srs);
1042
1043         if (cpu_has_veic) {
1044                 if (board_bind_eic_interrupt)
1045                         board_bind_eic_interrupt (n, srs);
1046         }
1047         else if (cpu_has_vint) {
1048                 /* SRSMap is only defined if shadow sets are implemented */
1049                 if (mips_srs_max() > 1)
1050                         change_c0_srsmap (0xf << n*4, srs << n*4);
1051         }
1052
1053         if (srs == 0) {
1054                 /*
1055                  * If no shadow set is selected then use the default handler
1056                  * that does normal register saving and a standard interrupt exit
1057                  */
1058
1059                 extern char except_vec_vi, except_vec_vi_lui;
1060                 extern char except_vec_vi_ori, except_vec_vi_end;
1061                 const int handler_len = &except_vec_vi_end - &except_vec_vi;
1062                 const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
1063                 const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
1064
1065                 if (handler_len > VECTORSPACING) {
1066                         /*
1067                          * Sigh... panicing won't help as the console
1068                          * is probably not configured :(
1069                          */
1070                         panic ("VECTORSPACING too small");
1071                 }
1072
1073                 memcpy (b, &except_vec_vi, handler_len);
1074                 w = (u32 *)(b + lui_offset);
1075                 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1076                 w = (u32 *)(b + ori_offset);
1077                 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1078                 flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
1079         }
1080         else {
1081                 /*
1082                  * In other cases jump directly to the interrupt handler
1083                  *
1084                  * It is the handlers responsibility to save registers if required
1085                  * (eg hi/lo) and return from the exception using "eret"
1086                  */
1087                 w = (u32 *)b;
1088                 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1089                 *w = 0;
1090                 flush_icache_range((unsigned long)b, (unsigned long)(b+8));
1091         }
1092
1093         return (void *)old_handler;
1094 }
1095
1096 void *set_vi_handler (int n, void *addr)
1097 {
1098         return set_vi_srs_handler (n, addr, 0);
1099 }
1100 #endif
1101
1102 /*
1103  * This is used by native signal handling
1104  */
1105 asmlinkage int (*save_fp_context)(struct sigcontext *sc);
1106 asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
1107
1108 extern asmlinkage int _save_fp_context(struct sigcontext *sc);
1109 extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
1110
1111 extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
1112 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
1113
1114 static inline void signal_init(void)
1115 {
1116         if (cpu_has_fpu) {
1117                 save_fp_context = _save_fp_context;
1118                 restore_fp_context = _restore_fp_context;
1119         } else {
1120                 save_fp_context = fpu_emulator_save_context;
1121                 restore_fp_context = fpu_emulator_restore_context;
1122         }
1123 }
1124
1125 #ifdef CONFIG_MIPS32_COMPAT
1126
1127 /*
1128  * This is used by 32-bit signal stuff on the 64-bit kernel
1129  */
1130 asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
1131 asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
1132
1133 extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
1134 extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
1135
1136 extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
1137 extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
1138
1139 static inline void signal32_init(void)
1140 {
1141         if (cpu_has_fpu) {
1142                 save_fp_context32 = _save_fp_context32;
1143                 restore_fp_context32 = _restore_fp_context32;
1144         } else {
1145                 save_fp_context32 = fpu_emulator_save_context32;
1146                 restore_fp_context32 = fpu_emulator_restore_context32;
1147         }
1148 }
1149 #endif
1150
1151 extern void cpu_cache_init(void);
1152 extern void tlb_init(void);
1153
1154 void __init per_cpu_trap_init(void)
1155 {
1156         unsigned int cpu = smp_processor_id();
1157         unsigned int status_set = ST0_CU0;
1158
1159         /*
1160          * Disable coprocessors and select 32-bit or 64-bit addressing
1161          * and the 16/32 or 32/32 FPR register model.  Reset the BEV
1162          * flag that some firmware may have left set and the TS bit (for
1163          * IP27).  Set XX for ISA IV code to work.
1164          */
1165 #ifdef CONFIG_64BIT
1166         status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1167 #endif
1168         if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1169                 status_set |= ST0_XX;
1170         change_c0_status(ST0_CU|ST0_MX|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1171                          status_set);
1172
1173         if (cpu_has_dsp)
1174                 set_c0_status(ST0_MX);
1175
1176 #ifdef CONFIG_CPU_MIPSR2
1177         write_c0_hwrena (0x0000000f); /* Allow rdhwr to all registers */
1178 #endif
1179
1180         /*
1181          * Interrupt handling.
1182          */
1183         if (cpu_has_veic || cpu_has_vint) {
1184                 write_c0_ebase (ebase);
1185                 /* Setting vector spacing enables EI/VI mode  */
1186                 change_c0_intctl (0x3e0, VECTORSPACING);
1187         }
1188         if (cpu_has_divec)
1189                 set_c0_cause(CAUSEF_IV);
1190
1191         cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1192         TLBMISS_HANDLER_SETUP();
1193
1194         atomic_inc(&init_mm.mm_count);
1195         current->active_mm = &init_mm;
1196         BUG_ON(current->mm);
1197         enter_lazy_tlb(&init_mm, current);
1198
1199         cpu_cache_init();
1200         tlb_init();
1201 }
1202
1203 /* Install CPU exception handler */
1204 void __init set_handler (unsigned long offset, void *addr, unsigned long size)
1205 {
1206         memcpy((void *)(ebase + offset), addr, size);
1207         flush_icache_range(ebase + offset, ebase + offset + size);
1208 }
1209
1210 /* Install uncached CPU exception handler */
1211 void __init set_uncached_handler (unsigned long offset, void *addr, unsigned long size)
1212 {
1213 #ifdef CONFIG_32BIT
1214         unsigned long uncached_ebase = KSEG1ADDR(ebase);
1215 #endif
1216 #ifdef CONFIG_64BIT
1217         unsigned long uncached_ebase = TO_UNCAC(ebase);
1218 #endif
1219
1220         memcpy((void *)(uncached_ebase + offset), addr, size);
1221 }
1222
1223 void __init trap_init(void)
1224 {
1225         extern char except_vec3_generic, except_vec3_r4000;
1226         extern char except_vec4;
1227         unsigned long i;
1228
1229         if (cpu_has_veic || cpu_has_vint)
1230                 ebase = (unsigned long) alloc_bootmem_low_pages (0x200 + VECTORSPACING*64);
1231         else
1232                 ebase = CAC_BASE;
1233
1234 #ifdef CONFIG_CPU_MIPSR2
1235         mips_srs_init();
1236 #endif
1237
1238         per_cpu_trap_init();
1239
1240         /*
1241          * Copy the generic exception handlers to their final destination.
1242          * This will be overriden later as suitable for a particular
1243          * configuration.
1244          */
1245         set_handler(0x180, &except_vec3_generic, 0x80);
1246
1247         /*
1248          * Setup default vectors
1249          */
1250         for (i = 0; i <= 31; i++)
1251                 set_except_vector(i, handle_reserved);
1252
1253         /*
1254          * Copy the EJTAG debug exception vector handler code to it's final
1255          * destination.
1256          */
1257         if (cpu_has_ejtag && board_ejtag_handler_setup)
1258                 board_ejtag_handler_setup ();
1259
1260         /*
1261          * Only some CPUs have the watch exceptions.
1262          */
1263         if (cpu_has_watch)
1264                 set_except_vector(23, handle_watch);
1265
1266         /*
1267          * Initialise interrupt handlers
1268          */
1269         if (cpu_has_veic || cpu_has_vint) {
1270                 int nvec = cpu_has_veic ? 64 : 8;
1271                 for (i = 0; i < nvec; i++)
1272                         set_vi_handler (i, NULL);
1273         }
1274         else if (cpu_has_divec)
1275                 set_handler(0x200, &except_vec4, 0x8);
1276
1277         /*
1278          * Some CPUs can enable/disable for cache parity detection, but does
1279          * it different ways.
1280          */
1281         parity_protection_init();
1282
1283         /*
1284          * The Data Bus Errors / Instruction Bus Errors are signaled
1285          * by external hardware.  Therefore these two exceptions
1286          * may have board specific handlers.
1287          */
1288         if (board_be_init)
1289                 board_be_init();
1290
1291         set_except_vector(1, handle_tlbm);
1292         set_except_vector(2, handle_tlbl);
1293         set_except_vector(3, handle_tlbs);
1294
1295         set_except_vector(4, handle_adel);
1296         set_except_vector(5, handle_ades);
1297
1298         set_except_vector(6, handle_ibe);
1299         set_except_vector(7, handle_dbe);
1300
1301         set_except_vector(8, handle_sys);
1302         set_except_vector(9, handle_bp);
1303         set_except_vector(10, handle_ri);
1304         set_except_vector(11, handle_cpu);
1305         set_except_vector(12, handle_ov);
1306         set_except_vector(13, handle_tr);
1307
1308         if (current_cpu_data.cputype == CPU_R6000 ||
1309             current_cpu_data.cputype == CPU_R6000A) {
1310                 /*
1311                  * The R6000 is the only R-series CPU that features a machine
1312                  * check exception (similar to the R4000 cache error) and
1313                  * unaligned ldc1/sdc1 exception.  The handlers have not been
1314                  * written yet.  Well, anyway there is no R6000 machine on the
1315                  * current list of targets for Linux/MIPS.
1316                  * (Duh, crap, there is someone with a triple R6k machine)
1317                  */
1318                 //set_except_vector(14, handle_mc);
1319                 //set_except_vector(15, handle_ndc);
1320         }
1321
1322
1323         if (board_nmi_handler_setup)
1324                 board_nmi_handler_setup();
1325
1326         if (cpu_has_fpu && !cpu_has_nofpuex)
1327                 set_except_vector(15, handle_fpe);
1328
1329         set_except_vector(22, handle_mdmx);
1330
1331         if (cpu_has_mcheck)
1332                 set_except_vector(24, handle_mcheck);
1333
1334         if (cpu_has_dsp)
1335                 set_except_vector(26, handle_dsp);
1336
1337         if (cpu_has_vce)
1338                 /* Special exception: R4[04]00 uses also the divec space. */
1339                 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1340         else if (cpu_has_4kex)
1341                 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1342         else
1343                 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1344
1345         signal_init();
1346 #ifdef CONFIG_MIPS32_COMPAT
1347         signal32_init();
1348 #endif
1349
1350         flush_icache_range(ebase, ebase + 0x400);
1351 }