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