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