- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.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 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  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
24 #include <asm/bootinfo.h>
25 #include <asm/branch.h>
26 #include <asm/break.h>
27 #include <asm/cpu.h>
28 #include <asm/fpu.h>
29 #include <asm/module.h>
30 #include <asm/pgtable.h>
31 #include <asm/ptrace.h>
32 #include <asm/sections.h>
33 #include <asm/system.h>
34 #include <asm/tlbdebug.h>
35 #include <asm/traps.h>
36 #include <asm/uaccess.h>
37 #include <asm/mmu_context.h>
38 #include <asm/watch.h>
39 #include <asm/types.h>
40
41 extern asmlinkage void handle_mod(void);
42 extern asmlinkage void handle_tlbl(void);
43 extern asmlinkage void handle_tlbs(void);
44 extern asmlinkage void __xtlb_mod(void);
45 extern asmlinkage void __xtlb_tlbl(void);
46 extern asmlinkage void __xtlb_tlbs(void);
47 extern asmlinkage void handle_adel(void);
48 extern asmlinkage void handle_ades(void);
49 extern asmlinkage void handle_ibe(void);
50 extern asmlinkage void handle_dbe(void);
51 extern asmlinkage void handle_sys(void);
52 extern asmlinkage void handle_bp(void);
53 extern asmlinkage void handle_ri(void);
54 extern asmlinkage void handle_cpu(void);
55 extern asmlinkage void handle_ov(void);
56 extern asmlinkage void handle_tr(void);
57 extern asmlinkage void handle_fpe(void);
58 extern asmlinkage void handle_mdmx(void);
59 extern asmlinkage void handle_watch(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
69 /*
70  * These constant is for searching for possible module text segments.
71  * MODULE_RANGE is a guess of how much space is likely to be vmalloced.
72  */
73 #define MODULE_RANGE (8*1024*1024)
74
75 /*
76  * This routine abuses get_user()/put_user() to reference pointers
77  * with at least a bit of error checking ...
78  */
79 void show_stack(struct task_struct *task, unsigned long *sp)
80 {
81         const int field = 2 * sizeof(unsigned long);
82         long stackdata;
83         int i;
84
85         sp = sp ? sp : (unsigned long *) &sp;
86
87         printk("Stack :");
88         i = 0;
89         while ((unsigned long) sp & (PAGE_SIZE - 1)) {
90                 if (i && ((i % (64 / field)) == 0))
91                         printk("\n       ");
92                 if (i > 39) {
93                         printk(" ...");
94                         break;
95                 }
96
97                 if (__get_user(stackdata, sp++)) {
98                         printk(" (Bad stack address)");
99                         break;
100                 }
101
102                 printk(" %0*lx", field, stackdata);
103                 i++;
104         }
105         printk("\n");
106 }
107
108 void show_trace(struct task_struct *task, unsigned long *stack)
109 {
110         const int field = 2 * sizeof(unsigned long);
111         unsigned long addr;
112
113         if (!stack)
114                 stack = (unsigned long*)&stack;
115
116         printk("Call Trace:");
117 #ifdef CONFIG_KALLSYMS
118         printk("\n");
119 #endif
120         while (!kstack_end(stack)) {
121                 addr = *stack++;
122                 if (__kernel_text_address(addr)) {
123                         printk(" [<%0*lx>] ", field, addr);
124                         print_symbol("%s\n", addr);
125                 }
126         }
127         printk("\n");
128 }
129
130 /*
131  * The architecture-independent dump_stack generator
132  */
133 void dump_stack(void)
134 {
135         unsigned long stack;
136
137         show_trace(current, &stack);
138 }
139
140 EXPORT_SYMBOL(dump_stack);
141
142 void show_code(unsigned int *pc)
143 {
144         long i;
145
146         printk("\nCode:");
147
148         for(i = -3 ; i < 6 ; i++) {
149                 unsigned int insn;
150                 if (__get_user(insn, pc + i)) {
151                         printk(" (Bad address in epc)\n");
152                         break;
153                 }
154                 printk("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
155         }
156 }
157
158 void show_regs(struct pt_regs *regs)
159 {
160         const int field = 2 * sizeof(unsigned long);
161         unsigned int cause = regs->cp0_cause;
162         int i;
163
164         printk("Cpu %d\n", smp_processor_id());
165
166         /*
167          * Saved main processor registers
168          */
169         for (i = 0; i < 32; ) {
170                 if ((i % 4) == 0)
171                         printk("$%2d   :", i);
172                 if (i == 0)
173                         printk(" %0*lx", field, 0UL);
174                 else if (i == 26 || i == 27)
175                         printk(" %*s", field, "");
176                 else
177                         printk(" %0*lx", field, regs->regs[i]);
178
179                 i++;
180                 if ((i % 4) == 0)
181                         printk("\n");
182         }
183
184         printk("Hi    : %0*lx\n", field, regs->hi);
185         printk("Lo    : %0*lx\n", field, regs->lo);
186
187         /*
188          * Saved cp0 registers
189          */
190         printk("epc   : %0*lx ", field, regs->cp0_epc);
191         print_symbol("%s ", regs->cp0_epc);
192         printk("    %s\n", print_tainted());
193         printk("ra    : %0*lx ", field, regs->regs[31]);
194         print_symbol("%s\n", regs->regs[31]);
195
196         printk("Status: %08x    ", (uint32_t) regs->cp0_status);
197
198         if (regs->cp0_status & ST0_KX)
199                 printk("KX ");
200         if (regs->cp0_status & ST0_SX)
201                 printk("SX ");
202         if (regs->cp0_status & ST0_UX)
203                 printk("UX ");
204         switch (regs->cp0_status & ST0_KSU) {
205         case KSU_USER:
206                 printk("USER ");
207                 break;
208         case KSU_SUPERVISOR:
209                 printk("SUPERVISOR ");
210                 break;
211         case KSU_KERNEL:
212                 printk("KERNEL ");
213                 break;
214         default:
215                 printk("BAD_MODE ");
216                 break;
217         }
218         if (regs->cp0_status & ST0_ERL)
219                 printk("ERL ");
220         if (regs->cp0_status & ST0_EXL)
221                 printk("EXL ");
222         if (regs->cp0_status & ST0_IE)
223                 printk("IE ");
224         printk("\n");
225
226         printk("Cause : %08x\n", cause);
227
228         cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
229         if (1 <= cause && cause <= 5)
230                 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
231
232         printk("PrId  : %08x\n", read_c0_prid());
233 }
234
235 void show_registers(struct pt_regs *regs)
236 {
237         show_regs(regs);
238         print_modules();
239         printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
240                 current->comm, current->pid, current_thread_info(), current);
241         show_stack(current, (long *) regs->regs[29]);
242         show_trace(current, (long *) regs->regs[29]);
243         show_code((unsigned int *) regs->cp0_epc);
244         printk("\n");
245 }
246
247 static spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
248
249 NORET_TYPE void __die(const char * str, struct pt_regs * regs,
250         const char * file, const char * func, unsigned long line)
251 {
252         static int die_counter;
253
254         console_verbose();
255         spin_lock_irq(&die_lock);
256         printk("%s", str);
257         if (file && func)
258                 printk(" in %s:%s, line %ld", file, func, line);
259         printk("[#%d]:\n", ++die_counter);
260         show_registers(regs);
261         spin_unlock_irq(&die_lock);
262         do_exit(SIGSEGV);
263 }
264
265 void __die_if_kernel(const char * str, struct pt_regs * regs,
266                      const char * file, const char * func, unsigned long line)
267 {
268         if (!user_mode(regs))
269                 __die(str, regs, file, func, line);
270 }
271
272 extern const struct exception_table_entry __start___dbe_table[];
273 extern const struct exception_table_entry __stop___dbe_table[];
274
275 void __declare_dbe_table(void)
276 {
277         __asm__ __volatile__(
278         ".section\t__dbe_table,\"a\"\n\t"
279         ".previous"
280         );
281 }
282
283 /* Given an address, look for it in the exception tables. */
284 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
285 {
286         const struct exception_table_entry *e;
287
288         e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
289         if (!e)
290                 e = search_module_dbetables(addr);
291         return e;
292 }
293
294 asmlinkage void do_be(struct pt_regs *regs)
295 {
296         const int field = 2 * sizeof(unsigned long);
297         const struct exception_table_entry *fixup = NULL;
298         int data = regs->cp0_cause & 4;
299         int action = MIPS_BE_FATAL;
300
301         /* XXX For now.  Fixme, this searches the wrong table ...  */
302         if (data && !user_mode(regs))
303                 fixup = search_dbe_tables(exception_epc(regs));
304
305         if (fixup)
306                 action = MIPS_BE_FIXUP;
307
308         if (board_be_handler)
309                 action = board_be_handler(regs, fixup != 0);
310
311         switch (action) {
312         case MIPS_BE_DISCARD:
313                 return;
314         case MIPS_BE_FIXUP:
315                 if (fixup) {
316                         regs->cp0_epc = fixup->nextinsn;
317                         return;
318                 }
319                 break;
320         default:
321                 break;
322         }
323
324         /*
325          * Assume it would be too dangerous to continue ...
326          */
327         printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
328                data ? "Data" : "Instruction",
329                field, regs->cp0_epc, field, regs->regs[31]);
330         die_if_kernel("Oops", regs);
331         force_sig(SIGBUS, current);
332 }
333
334 static inline int get_insn_opcode(struct pt_regs *regs, unsigned int *opcode)
335 {
336         unsigned int *epc;
337
338         epc = (unsigned int *) regs->cp0_epc +
339               ((regs->cp0_cause & CAUSEF_BD) != 0);
340         if (!get_user(*opcode, epc))
341                 return 0;
342
343         force_sig(SIGSEGV, current);
344         return 1;
345 }
346
347 /*
348  * ll/sc emulation
349  */
350
351 #define OPCODE 0xfc000000
352 #define BASE   0x03e00000
353 #define RT     0x001f0000
354 #define OFFSET 0x0000ffff
355 #define LL     0xc0000000
356 #define SC     0xe0000000
357
358 /*
359  * The ll_bit is cleared by r*_switch.S
360  */
361
362 unsigned long ll_bit;
363
364 static struct task_struct *ll_task = NULL;
365
366 static inline void simulate_ll(struct pt_regs *regs, unsigned int opcode)
367 {
368         unsigned long value, *vaddr;
369         long offset;
370         int signal = 0;
371
372         /*
373          * analyse the ll instruction that just caused a ri exception
374          * and put the referenced address to addr.
375          */
376
377         /* sign extend offset */
378         offset = opcode & OFFSET;
379         offset <<= 16;
380         offset >>= 16;
381
382         vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset);
383
384         if ((unsigned long)vaddr & 3) {
385                 signal = SIGBUS;
386                 goto sig;
387         }
388         if (get_user(value, vaddr)) {
389                 signal = SIGSEGV;
390                 goto sig;
391         }
392
393         preempt_disable();
394
395         if (ll_task == NULL || ll_task == current) {
396                 ll_bit = 1;
397         } else {
398                 ll_bit = 0;
399         }
400         ll_task = current;
401
402         preempt_enable();
403
404         regs->regs[(opcode & RT) >> 16] = value;
405
406         compute_return_epc(regs);
407         return;
408
409 sig:
410         force_sig(signal, current);
411 }
412
413 static inline void simulate_sc(struct pt_regs *regs, unsigned int opcode)
414 {
415         unsigned long *vaddr, reg;
416         long offset;
417         int signal = 0;
418
419         /*
420          * analyse the sc instruction that just caused a ri exception
421          * and put the referenced address to addr.
422          */
423
424         /* sign extend offset */
425         offset = opcode & OFFSET;
426         offset <<= 16;
427         offset >>= 16;
428
429         vaddr = (unsigned long *)((long)(regs->regs[(opcode & BASE) >> 21]) + offset);
430         reg = (opcode & RT) >> 16;
431
432         if ((unsigned long)vaddr & 3) {
433                 signal = SIGBUS;
434                 goto sig;
435         }
436
437         preempt_disable();
438
439         if (ll_bit == 0 || ll_task != current) {
440                 regs->regs[reg] = 0;
441                 preempt_enable();
442                 compute_return_epc(regs);
443                 return;
444         }
445
446         preempt_enable();
447
448         if (put_user(regs->regs[reg], vaddr)) {
449                 signal = SIGSEGV;
450                 goto sig;
451         }
452
453         regs->regs[reg] = 1;
454
455         compute_return_epc(regs);
456         return;
457
458 sig:
459         force_sig(signal, current);
460 }
461
462 /*
463  * ll uses the opcode of lwc0 and sc uses the opcode of swc0.  That is both
464  * opcodes are supposed to result in coprocessor unusable exceptions if
465  * executed on ll/sc-less processors.  That's the theory.  In practice a
466  * few processors such as NEC's VR4100 throw reserved instruction exceptions
467  * instead, so we're doing the emulation thing in both exception handlers.
468  */
469 static inline int simulate_llsc(struct pt_regs *regs)
470 {
471         unsigned int opcode;
472
473         if (unlikely(get_insn_opcode(regs, &opcode)))
474                 return -EFAULT;
475
476         if ((opcode & OPCODE) == LL) {
477                 simulate_ll(regs, opcode);
478                 return 0;
479         }
480         if ((opcode & OPCODE) == SC) {
481                 simulate_sc(regs, opcode);
482                 return 0;
483         }
484
485         return -EFAULT;                 /* Strange things going on ... */
486 }
487
488 asmlinkage void do_ov(struct pt_regs *regs)
489 {
490         siginfo_t info;
491
492         info.si_code = FPE_INTOVF;
493         info.si_signo = SIGFPE;
494         info.si_errno = 0;
495         info.si_addr = (void *)regs->cp0_epc;
496         force_sig_info(SIGFPE, &info, current);
497 }
498
499 /*
500  * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
501  */
502 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
503 {
504         if (fcr31 & FPU_CSR_UNI_X) {
505                 int sig;
506
507                 preempt_disable();
508
509                 /*
510                  * Unimplemented operation exception.  If we've got the full
511                  * software emulator on-board, let's use it...
512                  *
513                  * Force FPU to dump state into task/thread context.  We're
514                  * moving a lot of data here for what is probably a single
515                  * instruction, but the alternative is to pre-decode the FP
516                  * register operands before invoking the emulator, which seems
517                  * a bit extreme for what should be an infrequent event.
518                  */
519                 save_fp(current);
520
521                 /* Run the emulator */
522                 sig = fpu_emulator_cop1Handler (0, regs,
523                         &current->thread.fpu.soft);
524
525                 /*
526                  * We can't allow the emulated instruction to leave any of
527                  * the cause bit set in $fcr31.
528                  */
529                 current->thread.fpu.soft.fcr31 &= ~FPU_CSR_ALL_X;
530
531                 /* Restore the hardware register state */
532                 restore_fp(current);
533
534                 preempt_enable();
535
536                 /* If something went wrong, signal */
537                 if (sig)
538                         force_sig(sig, current);
539
540                 return;
541         }
542
543         force_sig(SIGFPE, current);
544 }
545
546 asmlinkage void do_bp(struct pt_regs *regs)
547 {
548         unsigned int opcode, bcode;
549         siginfo_t info;
550
551         die_if_kernel("Break instruction in kernel code", regs);
552
553         if (get_insn_opcode(regs, &opcode))
554                 return;
555
556         /*
557          * There is the ancient bug in the MIPS assemblers that the break
558          * code starts left to bit 16 instead to bit 6 in the opcode.
559          * Gas is bug-compatible, but not always, grrr...
560          * We handle both cases with a simple heuristics.  --macro
561          */
562         bcode = ((opcode >> 6) & ((1 << 20) - 1));
563         if (bcode < (1 << 10))
564                 bcode <<= 10;
565
566         /*
567          * (A short test says that IRIX 5.3 sends SIGTRAP for all break
568          * insns, even for break codes that indicate arithmetic failures.
569          * Weird ...)
570          * But should we continue the brokenness???  --macro
571          */
572         switch (bcode) {
573         case BRK_OVERFLOW << 10:
574         case BRK_DIVZERO << 10:
575                 if (bcode == (BRK_DIVZERO << 10))
576                         info.si_code = FPE_INTDIV;
577                 else
578                         info.si_code = FPE_INTOVF;
579                 info.si_signo = SIGFPE;
580                 info.si_errno = 0;
581                 info.si_addr = (void *)regs->cp0_epc;
582                 force_sig_info(SIGFPE, &info, current);
583                 break;
584         default:
585                 force_sig(SIGTRAP, current);
586         }
587 }
588
589 asmlinkage void do_tr(struct pt_regs *regs)
590 {
591         unsigned int opcode, tcode = 0;
592         siginfo_t info;
593
594         die_if_kernel("Trap instruction in kernel code", regs);
595
596         if (get_insn_opcode(regs, &opcode))
597                 return;
598
599         /* Immediate versions don't provide a code.  */
600         if (!(opcode & OPCODE))
601                 tcode = ((opcode >> 6) & ((1 << 10) - 1));
602
603         /*
604          * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
605          * insns, even for trap codes that indicate arithmetic failures.
606          * Weird ...)
607          * But should we continue the brokenness???  --macro
608          */
609         switch (tcode) {
610         case BRK_OVERFLOW:
611         case BRK_DIVZERO:
612                 if (tcode == BRK_DIVZERO)
613                         info.si_code = FPE_INTDIV;
614                 else
615                         info.si_code = FPE_INTOVF;
616                 info.si_signo = SIGFPE;
617                 info.si_errno = 0;
618                 info.si_addr = (void *)regs->cp0_epc;
619                 force_sig_info(SIGFPE, &info, current);
620                 break;
621         default:
622                 force_sig(SIGTRAP, current);
623         }
624 }
625
626 asmlinkage void do_ri(struct pt_regs *regs)
627 {
628         die_if_kernel("Reserved instruction in kernel code", regs);
629
630         if (!cpu_has_llsc)
631                 if (!simulate_llsc(regs))
632                         return;
633
634         force_sig(SIGILL, current);
635 }
636
637 asmlinkage void do_cpu(struct pt_regs *regs)
638 {
639         unsigned int cpid;
640
641         die_if_kernel("do_cpu invoked from kernel context!", regs);
642
643         cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
644
645         switch (cpid) {
646         case 0:
647                 if (cpu_has_llsc)
648                         break;
649
650                 if (!simulate_llsc(regs))
651                         return;
652                 break;
653
654         case 1:
655                 preempt_disable();
656
657                 own_fpu();
658                 if (current->used_math) {       /* Using the FPU again.  */
659                         restore_fp(current);
660                 } else {                        /* First time FPU user.  */
661                         init_fpu();
662                         current->used_math = 1;
663                 }
664
665                 if (!cpu_has_fpu) {
666                         int sig = fpu_emulator_cop1Handler(0, regs,
667                                                 &current->thread.fpu.soft);
668                         if (sig)
669                                 force_sig(sig, current);
670                 }
671
672                 preempt_enable();
673
674                 return;
675
676         case 2:
677         case 3:
678                 break;
679         }
680
681         force_sig(SIGILL, current);
682 }
683
684 asmlinkage void do_mdmx(struct pt_regs *regs)
685 {
686         force_sig(SIGILL, current);
687 }
688
689 asmlinkage void do_watch(struct pt_regs *regs)
690 {
691         /*
692          * We use the watch exception where available to detect stack
693          * overflows.
694          */
695         dump_tlb_all();
696         show_regs(regs);
697         panic("Caught WATCH exception - probably caused by stack overflow.");
698 }
699
700 asmlinkage void do_mcheck(struct pt_regs *regs)
701 {
702         show_regs(regs);
703         dump_tlb_all();
704         /*
705          * Some chips may have other causes of machine check (e.g. SB1
706          * graduation timer)
707          */
708         panic("Caught Machine Check exception - %scaused by multiple "
709               "matching entries in the TLB.",
710               (regs->cp0_status & ST0_TS) ? "" : "not ");
711 }
712
713 asmlinkage void do_reserved(struct pt_regs *regs)
714 {
715         /*
716          * Game over - no way to handle this if it ever occurs.  Most probably
717          * caused by a new unknown cpu type or after another deadly
718          * hard/software error.
719          */
720         show_regs(regs);
721         panic("Caught reserved exception %ld - should not happen.",
722               (regs->cp0_cause & 0x7f) >> 2);
723 }
724
725 /*
726  * Some MIPS CPUs can enable/disable for cache parity detection, but do
727  * it different ways.
728  */
729 static inline void parity_protection_init(void)
730 {
731         switch (current_cpu_data.cputype) {
732         case CPU_24K:
733                 /* 24K cache parity not currently implemented in FPGA */
734                 printk(KERN_INFO "Disable cache parity protection for "
735                        "MIPS 24K CPU.\n");
736                 write_c0_ecc(read_c0_ecc() & ~0x80000000);
737                 break;
738         case CPU_5KC:
739                 /* Set the PE bit (bit 31) in the c0_ecc register. */
740                 printk(KERN_INFO "Enable cache parity protection for "
741                        "MIPS 5KC/24K CPUs.\n");
742                 write_c0_ecc(read_c0_ecc() | 0x80000000);
743                 break;
744         case CPU_20KC:
745         case CPU_25KF:
746                 /* Clear the DE bit (bit 16) in the c0_status register. */
747                 printk(KERN_INFO "Enable cache parity protection for "
748                        "MIPS 20KC/25KF CPUs.\n");
749                 clear_c0_status(ST0_DE);
750                 break;
751         default:
752                 break;
753         }
754 }
755
756 asmlinkage void cache_parity_error(void)
757 {
758         const int field = 2 * sizeof(unsigned long);
759         unsigned int reg_val;
760
761         /* For the moment, report the problem and hang. */
762         printk("Cache error exception:\n");
763         printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
764         reg_val = read_c0_cacheerr();
765         printk("c0_cacheerr == %08x\n", reg_val);
766
767         printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
768                reg_val & (1<<30) ? "secondary" : "primary",
769                reg_val & (1<<31) ? "data" : "insn");
770         printk("Error bits: %s%s%s%s%s%s%s\n",
771                reg_val & (1<<29) ? "ED " : "",
772                reg_val & (1<<28) ? "ET " : "",
773                reg_val & (1<<26) ? "EE " : "",
774                reg_val & (1<<25) ? "EB " : "",
775                reg_val & (1<<24) ? "EI " : "",
776                reg_val & (1<<23) ? "E1 " : "",
777                reg_val & (1<<22) ? "E0 " : "");
778         printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
779
780 #if defined(CONFIG_CPU_MIPS32) || defined (CONFIG_CPU_MIPS64)
781         if (reg_val & (1<<22))
782                 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
783
784         if (reg_val & (1<<23))
785                 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
786 #endif
787
788         panic("Can't handle the cache error!");
789 }
790
791 /*
792  * SDBBP EJTAG debug exception handler.
793  * We skip the instruction and return to the next instruction.
794  */
795 void ejtag_exception_handler(struct pt_regs *regs)
796 {
797         const int field = 2 * sizeof(unsigned long);
798         unsigned long depc, old_epc;
799         unsigned int debug;
800
801         printk("SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
802         depc = read_c0_depc();
803         debug = read_c0_debug();
804         printk("c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
805         if (debug & 0x80000000) {
806                 /*
807                  * In branch delay slot.
808                  * We cheat a little bit here and use EPC to calculate the
809                  * debug return address (DEPC). EPC is restored after the
810                  * calculation.
811                  */
812                 old_epc = regs->cp0_epc;
813                 regs->cp0_epc = depc;
814                 __compute_return_epc(regs);
815                 depc = regs->cp0_epc;
816                 regs->cp0_epc = old_epc;
817         } else
818                 depc += 4;
819         write_c0_depc(depc);
820
821 #if 0
822         printk("\n\n----- Enable EJTAG single stepping ----\n\n");
823         write_c0_debug(debug | 0x100);
824 #endif
825 }
826
827 /*
828  * NMI exception handler.
829  */
830 void nmi_exception_handler(struct pt_regs *regs)
831 {
832         printk("NMI taken!!!!\n");
833         die("NMI", regs);
834         while(1) ;
835 }
836
837 unsigned long exception_handlers[32];
838
839 /*
840  * As a side effect of the way this is implemented we're limited
841  * to interrupt handlers in the address range from
842  * KSEG0 <= x < KSEG0 + 256mb on the Nevada.  Oh well ...
843  */
844 void *set_except_vector(int n, void *addr)
845 {
846         unsigned long handler = (unsigned long) addr;
847         unsigned long old_handler = exception_handlers[n];
848
849         exception_handlers[n] = handler;
850         if (n == 0 && cpu_has_divec) {
851                 *(volatile u32 *)(CAC_BASE + 0x200) = 0x08000000 |
852                                                  (0x03ffffff & (handler >> 2));
853                 flush_icache_range(CAC_BASE + 0x200, CAC_BASE + 0x204);
854         }
855         return (void *)old_handler;
856 }
857
858 /*
859  * This is used by native signal handling
860  */
861 asmlinkage int (*save_fp_context)(struct sigcontext *sc);
862 asmlinkage int (*restore_fp_context)(struct sigcontext *sc);
863
864 extern asmlinkage int _save_fp_context(struct sigcontext *sc);
865 extern asmlinkage int _restore_fp_context(struct sigcontext *sc);
866
867 extern asmlinkage int fpu_emulator_save_context(struct sigcontext *sc);
868 extern asmlinkage int fpu_emulator_restore_context(struct sigcontext *sc);
869
870 static inline void signal_init(void)
871 {
872         if (cpu_has_fpu) {
873                 save_fp_context = _save_fp_context;
874                 restore_fp_context = _restore_fp_context;
875         } else {
876                 save_fp_context = fpu_emulator_save_context;
877                 restore_fp_context = fpu_emulator_restore_context;
878         }
879 }
880
881 #ifdef CONFIG_MIPS32_COMPAT
882
883 /*
884  * This is used by 32-bit signal stuff on the 64-bit kernel
885  */
886 asmlinkage int (*save_fp_context32)(struct sigcontext32 *sc);
887 asmlinkage int (*restore_fp_context32)(struct sigcontext32 *sc);
888
889 extern asmlinkage int _save_fp_context32(struct sigcontext32 *sc);
890 extern asmlinkage int _restore_fp_context32(struct sigcontext32 *sc);
891
892 extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 *sc);
893 extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 *sc);
894
895 static inline void signal32_init(void)
896 {
897         if (cpu_has_fpu) {
898                 save_fp_context32 = _save_fp_context32;
899                 restore_fp_context32 = _restore_fp_context32;
900         } else {
901                 save_fp_context32 = fpu_emulator_save_context32;
902                 restore_fp_context32 = fpu_emulator_restore_context32;
903         }
904 }
905 #endif
906
907 extern void cpu_cache_init(void);
908 extern void tlb_init(void);
909
910 void __init per_cpu_trap_init(void)
911 {
912         unsigned int cpu = smp_processor_id();
913         unsigned int status_set = ST0_CU0;
914
915         /*
916          * Disable coprocessors and select 32-bit or 64-bit addressing
917          * and the 16/32 or 32/32 FPR register model.  Reset the BEV
918          * flag that some firmware may have left set and the TS bit (for
919          * IP27).  Set XX for ISA IV code to work.
920          */
921 #ifdef CONFIG_MIPS64
922         status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
923 #endif
924         if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
925                 status_set |= ST0_XX;
926         change_c0_status(ST0_CU|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
927                          status_set);
928
929         /*
930          * Some MIPS CPUs have a dedicated interrupt vector which reduces the
931          * interrupt processing overhead.  Use it where available.
932          */
933         if (cpu_has_divec)
934                 set_c0_cause(CAUSEF_IV);
935
936         cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
937         TLBMISS_HANDLER_SETUP();
938
939         atomic_inc(&init_mm.mm_count);
940         current->active_mm = &init_mm;
941         BUG_ON(current->mm);
942         enter_lazy_tlb(&init_mm, current);
943
944         cpu_cache_init();
945         tlb_init();
946 }
947
948 void __init trap_init(void)
949 {
950         extern char except_vec3_generic, except_vec3_r4000;
951         extern char except_vec_ejtag_debug;
952         extern char except_vec4;
953         unsigned long i;
954
955         per_cpu_trap_init();
956
957         /*
958          * Copy the generic exception handlers to their final destination.
959          * This will be overriden later as suitable for a particular
960          * configuration.
961          */
962         memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
963
964         /*
965          * Setup default vectors
966          */
967         for (i = 0; i <= 31; i++)
968                 set_except_vector(i, handle_reserved);
969
970         /*
971          * Copy the EJTAG debug exception vector handler code to it's final
972          * destination.
973          */
974         if (cpu_has_ejtag)
975                 memcpy((void *)(CAC_BASE + 0x300), &except_vec_ejtag_debug, 0x80);
976
977         /*
978          * Only some CPUs have the watch exceptions.
979          */
980         if (cpu_has_watch)
981                 set_except_vector(23, handle_watch);
982
983         /*
984          * Some MIPS CPUs have a dedicated interrupt vector which reduces the
985          * interrupt processing overhead.  Use it where available.
986          */
987         if (cpu_has_divec)
988                 memcpy((void *)(CAC_BASE + 0x200), &except_vec4, 0x8);
989
990         /*
991          * Some CPUs can enable/disable for cache parity detection, but does
992          * it different ways.
993          */
994         parity_protection_init();
995
996         /*
997          * The Data Bus Errors / Instruction Bus Errors are signaled
998          * by external hardware.  Therefore these two exceptions
999          * may have board specific handlers.
1000          */
1001         if (board_be_init)
1002                 board_be_init();
1003
1004 #ifdef CONFIG_MIPS32
1005         set_except_vector(1, handle_mod);
1006         set_except_vector(2, handle_tlbl);
1007         set_except_vector(3, handle_tlbs);
1008 #endif
1009 #ifdef CONFIG_MIPS64
1010         set_except_vector(1, __xtlb_mod);
1011         set_except_vector(2, __xtlb_tlbl);
1012         set_except_vector(3, __xtlb_tlbs);
1013 #endif
1014         set_except_vector(4, handle_adel);
1015         set_except_vector(5, handle_ades);
1016
1017         set_except_vector(6, handle_ibe);
1018         set_except_vector(7, handle_dbe);
1019
1020         set_except_vector(8, handle_sys);
1021         set_except_vector(9, handle_bp);
1022         set_except_vector(10, handle_ri);
1023         set_except_vector(11, handle_cpu);
1024         set_except_vector(12, handle_ov);
1025         set_except_vector(13, handle_tr);
1026         set_except_vector(22, handle_mdmx);
1027
1028         if (cpu_has_fpu && !cpu_has_nofpuex)
1029                 set_except_vector(15, handle_fpe);
1030
1031         if (cpu_has_mcheck)
1032                 set_except_vector(24, handle_mcheck);
1033
1034         if (cpu_has_vce)
1035                 /* Special exception: R4[04]00 uses also the divec space. */
1036                 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1037         else if (cpu_has_4kex)
1038                 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1039         else
1040                 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1041
1042         if (current_cpu_data.cputype == CPU_R6000 ||
1043             current_cpu_data.cputype == CPU_R6000A) {
1044                 /*
1045                  * The R6000 is the only R-series CPU that features a machine
1046                  * check exception (similar to the R4000 cache error) and
1047                  * unaligned ldc1/sdc1 exception.  The handlers have not been
1048                  * written yet.  Well, anyway there is no R6000 machine on the
1049                  * current list of targets for Linux/MIPS.
1050                  * (Duh, crap, there is someone with a tripple R6k machine)
1051                  */
1052                 //set_except_vector(14, handle_mc);
1053                 //set_except_vector(15, handle_ndc);
1054         }
1055
1056         signal_init();
1057 #ifdef CONFIG_MIPS32_COMPAT
1058         signal32_init();
1059 #endif
1060
1061         flush_icache_range(CAC_BASE, CAC_BASE + 0x400);
1062 }