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