]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/mips/kernel/traps.c
[MIPS] Add noulri kernel argument to disable "rdhwr $29" usermode support.
[net-next-2.6.git] / arch / mips / kernel / traps.c
CommitLineData
1da177e4
LT
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 *
36ccf1c0 6 * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
1da177e4
LT
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.
60b0d655 12 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
1da177e4 13 */
8e8a52ed 14#include <linux/bug.h>
60b0d655 15#include <linux/compiler.h>
1da177e4
LT
16#include <linux/init.h>
17#include <linux/mm.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/smp.h>
1da177e4
LT
21#include <linux/spinlock.h>
22#include <linux/kallsyms.h>
e01402b1 23#include <linux/bootmem.h>
d4fd1989 24#include <linux/interrupt.h>
1da177e4
LT
25
26#include <asm/bootinfo.h>
27#include <asm/branch.h>
28#include <asm/break.h>
29#include <asm/cpu.h>
e50c0a8f 30#include <asm/dsp.h>
1da177e4 31#include <asm/fpu.h>
340ee4b9
RB
32#include <asm/mipsregs.h>
33#include <asm/mipsmtregs.h>
1da177e4
LT
34#include <asm/module.h>
35#include <asm/pgtable.h>
36#include <asm/ptrace.h>
37#include <asm/sections.h>
38#include <asm/system.h>
39#include <asm/tlbdebug.h>
40#include <asm/traps.h>
41#include <asm/uaccess.h>
42#include <asm/mmu_context.h>
1da177e4 43#include <asm/types.h>
1df0f0ff 44#include <asm/stacktrace.h>
1da177e4 45
e4ac58af 46extern asmlinkage void handle_int(void);
1da177e4
LT
47extern asmlinkage void handle_tlbm(void);
48extern asmlinkage void handle_tlbl(void);
49extern asmlinkage void handle_tlbs(void);
50extern asmlinkage void handle_adel(void);
51extern asmlinkage void handle_ades(void);
52extern asmlinkage void handle_ibe(void);
53extern asmlinkage void handle_dbe(void);
54extern asmlinkage void handle_sys(void);
55extern asmlinkage void handle_bp(void);
56extern asmlinkage void handle_ri(void);
5b10496b
AN
57extern asmlinkage void handle_ri_rdhwr_vivt(void);
58extern asmlinkage void handle_ri_rdhwr(void);
1da177e4
LT
59extern asmlinkage void handle_cpu(void);
60extern asmlinkage void handle_ov(void);
61extern asmlinkage void handle_tr(void);
62extern asmlinkage void handle_fpe(void);
63extern asmlinkage void handle_mdmx(void);
64extern asmlinkage void handle_watch(void);
340ee4b9 65extern asmlinkage void handle_mt(void);
e50c0a8f 66extern asmlinkage void handle_dsp(void);
1da177e4
LT
67extern asmlinkage void handle_mcheck(void);
68extern asmlinkage void handle_reserved(void);
69
12616ed2 70extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
e04582b7 71 struct mips_fpu_struct *ctx, int has_fpu);
1da177e4 72
9267a30d 73void (*board_watchpoint_handler)(struct pt_regs *regs);
1da177e4
LT
74void (*board_be_init)(void);
75int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
e01402b1
RB
76void (*board_nmi_handler_setup)(void);
77void (*board_ejtag_handler_setup)(void);
78void (*board_bind_eic_interrupt)(int irq, int regset);
1da177e4 79
1da177e4 80
4d157d5e 81static void show_raw_backtrace(unsigned long reg29)
e889d78f 82{
4d157d5e 83 unsigned long *sp = (unsigned long *)reg29;
e889d78f
AN
84 unsigned long addr;
85
86 printk("Call Trace:");
87#ifdef CONFIG_KALLSYMS
88 printk("\n");
89#endif
87151ae3
FBH
90 while (!kstack_end(sp)) {
91 addr = *sp++;
92 if (__kernel_text_address(addr))
93 print_ip_sym(addr);
e889d78f
AN
94 }
95 printk("\n");
96}
97
f66686f7 98#ifdef CONFIG_KALLSYMS
1df0f0ff 99int raw_show_trace;
f66686f7
AN
100static int __init set_raw_show_trace(char *str)
101{
102 raw_show_trace = 1;
103 return 1;
104}
105__setup("raw_show_trace", set_raw_show_trace);
1df0f0ff 106#endif
4d157d5e 107
eae23f2c 108static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
f66686f7 109{
4d157d5e
FBH
110 unsigned long sp = regs->regs[29];
111 unsigned long ra = regs->regs[31];
f66686f7 112 unsigned long pc = regs->cp0_epc;
f66686f7
AN
113
114 if (raw_show_trace || !__kernel_text_address(pc)) {
87151ae3 115 show_raw_backtrace(sp);
f66686f7
AN
116 return;
117 }
118 printk("Call Trace:\n");
4d157d5e 119 do {
87151ae3 120 print_ip_sym(pc);
1924600c 121 pc = unwind_stack(task, &sp, pc, &ra);
4d157d5e 122 } while (pc);
f66686f7
AN
123 printk("\n");
124}
f66686f7 125
1da177e4
LT
126/*
127 * This routine abuses get_user()/put_user() to reference pointers
128 * with at least a bit of error checking ...
129 */
eae23f2c
RB
130static void show_stacktrace(struct task_struct *task,
131 const struct pt_regs *regs)
1da177e4
LT
132{
133 const int field = 2 * sizeof(unsigned long);
134 long stackdata;
135 int i;
5e0373b8 136 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
1da177e4
LT
137
138 printk("Stack :");
139 i = 0;
140 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
141 if (i && ((i % (64 / field)) == 0))
142 printk("\n ");
143 if (i > 39) {
144 printk(" ...");
145 break;
146 }
147
148 if (__get_user(stackdata, sp++)) {
149 printk(" (Bad stack address)");
150 break;
151 }
152
153 printk(" %0*lx", field, stackdata);
154 i++;
155 }
156 printk("\n");
87151ae3 157 show_backtrace(task, regs);
f66686f7
AN
158}
159
f66686f7
AN
160void show_stack(struct task_struct *task, unsigned long *sp)
161{
162 struct pt_regs regs;
163 if (sp) {
164 regs.regs[29] = (unsigned long)sp;
165 regs.regs[31] = 0;
166 regs.cp0_epc = 0;
167 } else {
168 if (task && task != current) {
169 regs.regs[29] = task->thread.reg29;
170 regs.regs[31] = 0;
171 regs.cp0_epc = task->thread.reg31;
172 } else {
173 prepare_frametrace(&regs);
174 }
175 }
176 show_stacktrace(task, &regs);
1da177e4
LT
177}
178
179/*
180 * The architecture-independent dump_stack generator
181 */
182void dump_stack(void)
183{
1666a6fc 184 struct pt_regs regs;
1da177e4 185
1666a6fc
FBH
186 prepare_frametrace(&regs);
187 show_backtrace(current, &regs);
1da177e4
LT
188}
189
190EXPORT_SYMBOL(dump_stack);
191
e1bb8289 192static void show_code(unsigned int __user *pc)
1da177e4
LT
193{
194 long i;
195
196 printk("\nCode:");
197
198 for(i = -3 ; i < 6 ; i++) {
199 unsigned int insn;
200 if (__get_user(insn, pc + i)) {
201 printk(" (Bad address in epc)\n");
202 break;
203 }
204 printk("%c%08x%c", (i?' ':'<'), insn, (i?' ':'>'));
205 }
206}
207
eae23f2c 208static void __show_regs(const struct pt_regs *regs)
1da177e4
LT
209{
210 const int field = 2 * sizeof(unsigned long);
211 unsigned int cause = regs->cp0_cause;
212 int i;
213
214 printk("Cpu %d\n", smp_processor_id());
215
216 /*
217 * Saved main processor registers
218 */
219 for (i = 0; i < 32; ) {
220 if ((i % 4) == 0)
221 printk("$%2d :", i);
222 if (i == 0)
223 printk(" %0*lx", field, 0UL);
224 else if (i == 26 || i == 27)
225 printk(" %*s", field, "");
226 else
227 printk(" %0*lx", field, regs->regs[i]);
228
229 i++;
230 if ((i % 4) == 0)
231 printk("\n");
232 }
233
9693a853
FBH
234#ifdef CONFIG_CPU_HAS_SMARTMIPS
235 printk("Acx : %0*lx\n", field, regs->acx);
236#endif
1da177e4
LT
237 printk("Hi : %0*lx\n", field, regs->hi);
238 printk("Lo : %0*lx\n", field, regs->lo);
239
240 /*
241 * Saved cp0 registers
242 */
243 printk("epc : %0*lx ", field, regs->cp0_epc);
244 print_symbol("%s ", regs->cp0_epc);
245 printk(" %s\n", print_tainted());
246 printk("ra : %0*lx ", field, regs->regs[31]);
247 print_symbol("%s\n", regs->regs[31]);
248
249 printk("Status: %08x ", (uint32_t) regs->cp0_status);
250
3b2396d9
MR
251 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
252 if (regs->cp0_status & ST0_KUO)
253 printk("KUo ");
254 if (regs->cp0_status & ST0_IEO)
255 printk("IEo ");
256 if (regs->cp0_status & ST0_KUP)
257 printk("KUp ");
258 if (regs->cp0_status & ST0_IEP)
259 printk("IEp ");
260 if (regs->cp0_status & ST0_KUC)
261 printk("KUc ");
262 if (regs->cp0_status & ST0_IEC)
263 printk("IEc ");
264 } else {
265 if (regs->cp0_status & ST0_KX)
266 printk("KX ");
267 if (regs->cp0_status & ST0_SX)
268 printk("SX ");
269 if (regs->cp0_status & ST0_UX)
270 printk("UX ");
271 switch (regs->cp0_status & ST0_KSU) {
272 case KSU_USER:
273 printk("USER ");
274 break;
275 case KSU_SUPERVISOR:
276 printk("SUPERVISOR ");
277 break;
278 case KSU_KERNEL:
279 printk("KERNEL ");
280 break;
281 default:
282 printk("BAD_MODE ");
283 break;
284 }
285 if (regs->cp0_status & ST0_ERL)
286 printk("ERL ");
287 if (regs->cp0_status & ST0_EXL)
288 printk("EXL ");
289 if (regs->cp0_status & ST0_IE)
290 printk("IE ");
1da177e4 291 }
1da177e4
LT
292 printk("\n");
293
294 printk("Cause : %08x\n", cause);
295
296 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
297 if (1 <= cause && cause <= 5)
298 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
299
9966db25
RB
300 printk("PrId : %08x (%s)\n", read_c0_prid(),
301 cpu_name_string());
1da177e4
LT
302}
303
eae23f2c
RB
304/*
305 * FIXME: really the generic show_regs should take a const pointer argument.
306 */
307void show_regs(struct pt_regs *regs)
308{
309 __show_regs((struct pt_regs *)regs);
310}
311
312void show_registers(const struct pt_regs *regs)
1da177e4 313{
eae23f2c 314 __show_regs(regs);
1da177e4
LT
315 print_modules();
316 printk("Process %s (pid: %d, threadinfo=%p, task=%p)\n",
19c5870c 317 current->comm, task_pid_nr(current), current_thread_info(), current);
f66686f7 318 show_stacktrace(current, regs);
e1bb8289 319 show_code((unsigned int __user *) regs->cp0_epc);
1da177e4
LT
320 printk("\n");
321}
322
323static DEFINE_SPINLOCK(die_lock);
324
eae23f2c 325void __noreturn die(const char * str, const struct pt_regs * regs)
1da177e4
LT
326{
327 static int die_counter;
41c594ab
RB
328#ifdef CONFIG_MIPS_MT_SMTC
329 unsigned long dvpret = dvpe();
330#endif /* CONFIG_MIPS_MT_SMTC */
1da177e4
LT
331
332 console_verbose();
333 spin_lock_irq(&die_lock);
41c594ab
RB
334 bust_spinlocks(1);
335#ifdef CONFIG_MIPS_MT_SMTC
336 mips_mt_regdump(dvpret);
337#endif /* CONFIG_MIPS_MT_SMTC */
178086c8 338 printk("%s[#%d]:\n", str, ++die_counter);
1da177e4 339 show_registers(regs);
bcdcd8e7 340 add_taint(TAINT_DIE);
1da177e4 341 spin_unlock_irq(&die_lock);
d4fd1989
MB
342
343 if (in_interrupt())
344 panic("Fatal exception in interrupt");
345
346 if (panic_on_oops) {
347 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
348 ssleep(5);
349 panic("Fatal exception");
350 }
351
1da177e4
LT
352 do_exit(SIGSEGV);
353}
354
1da177e4
LT
355extern const struct exception_table_entry __start___dbe_table[];
356extern const struct exception_table_entry __stop___dbe_table[];
357
b6dcec9b
RB
358__asm__(
359" .section __dbe_table, \"a\"\n"
360" .previous \n");
1da177e4
LT
361
362/* Given an address, look for it in the exception tables. */
363static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
364{
365 const struct exception_table_entry *e;
366
367 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
368 if (!e)
369 e = search_module_dbetables(addr);
370 return e;
371}
372
373asmlinkage void do_be(struct pt_regs *regs)
374{
375 const int field = 2 * sizeof(unsigned long);
376 const struct exception_table_entry *fixup = NULL;
377 int data = regs->cp0_cause & 4;
378 int action = MIPS_BE_FATAL;
379
380 /* XXX For now. Fixme, this searches the wrong table ... */
381 if (data && !user_mode(regs))
382 fixup = search_dbe_tables(exception_epc(regs));
383
384 if (fixup)
385 action = MIPS_BE_FIXUP;
386
387 if (board_be_handler)
28fc582c 388 action = board_be_handler(regs, fixup != NULL);
1da177e4
LT
389
390 switch (action) {
391 case MIPS_BE_DISCARD:
392 return;
393 case MIPS_BE_FIXUP:
394 if (fixup) {
395 regs->cp0_epc = fixup->nextinsn;
396 return;
397 }
398 break;
399 default:
400 break;
401 }
402
403 /*
404 * Assume it would be too dangerous to continue ...
405 */
406 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
407 data ? "Data" : "Instruction",
408 field, regs->cp0_epc, field, regs->regs[31]);
409 die_if_kernel("Oops", regs);
410 force_sig(SIGBUS, current);
411}
412
1da177e4 413/*
60b0d655 414 * ll/sc, rdhwr, sync emulation
1da177e4
LT
415 */
416
417#define OPCODE 0xfc000000
418#define BASE 0x03e00000
419#define RT 0x001f0000
420#define OFFSET 0x0000ffff
421#define LL 0xc0000000
422#define SC 0xe0000000
60b0d655 423#define SPEC0 0x00000000
3c37026d
RB
424#define SPEC3 0x7c000000
425#define RD 0x0000f800
426#define FUNC 0x0000003f
60b0d655 427#define SYNC 0x0000000f
3c37026d 428#define RDHWR 0x0000003b
1da177e4
LT
429
430/*
431 * The ll_bit is cleared by r*_switch.S
432 */
433
434unsigned long ll_bit;
435
436static struct task_struct *ll_task = NULL;
437
60b0d655 438static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
1da177e4 439{
fe00f943 440 unsigned long value, __user *vaddr;
1da177e4 441 long offset;
1da177e4
LT
442
443 /*
444 * analyse the ll instruction that just caused a ri exception
445 * and put the referenced address to addr.
446 */
447
448 /* sign extend offset */
449 offset = opcode & OFFSET;
450 offset <<= 16;
451 offset >>= 16;
452
fe00f943
RB
453 vaddr = (unsigned long __user *)
454 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
1da177e4 455
60b0d655
MR
456 if ((unsigned long)vaddr & 3)
457 return SIGBUS;
458 if (get_user(value, vaddr))
459 return SIGSEGV;
1da177e4
LT
460
461 preempt_disable();
462
463 if (ll_task == NULL || ll_task == current) {
464 ll_bit = 1;
465 } else {
466 ll_bit = 0;
467 }
468 ll_task = current;
469
470 preempt_enable();
471
472 regs->regs[(opcode & RT) >> 16] = value;
473
60b0d655 474 return 0;
1da177e4
LT
475}
476
60b0d655 477static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
1da177e4 478{
fe00f943
RB
479 unsigned long __user *vaddr;
480 unsigned long reg;
1da177e4 481 long offset;
1da177e4
LT
482
483 /*
484 * analyse the sc instruction that just caused a ri exception
485 * and put the referenced address to addr.
486 */
487
488 /* sign extend offset */
489 offset = opcode & OFFSET;
490 offset <<= 16;
491 offset >>= 16;
492
fe00f943
RB
493 vaddr = (unsigned long __user *)
494 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
1da177e4
LT
495 reg = (opcode & RT) >> 16;
496
60b0d655
MR
497 if ((unsigned long)vaddr & 3)
498 return SIGBUS;
1da177e4
LT
499
500 preempt_disable();
501
502 if (ll_bit == 0 || ll_task != current) {
503 regs->regs[reg] = 0;
504 preempt_enable();
60b0d655 505 return 0;
1da177e4
LT
506 }
507
508 preempt_enable();
509
60b0d655
MR
510 if (put_user(regs->regs[reg], vaddr))
511 return SIGSEGV;
1da177e4
LT
512
513 regs->regs[reg] = 1;
514
60b0d655 515 return 0;
1da177e4
LT
516}
517
518/*
519 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
520 * opcodes are supposed to result in coprocessor unusable exceptions if
521 * executed on ll/sc-less processors. That's the theory. In practice a
522 * few processors such as NEC's VR4100 throw reserved instruction exceptions
523 * instead, so we're doing the emulation thing in both exception handlers.
524 */
60b0d655 525static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
1da177e4 526{
60b0d655
MR
527 if ((opcode & OPCODE) == LL)
528 return simulate_ll(regs, opcode);
529 if ((opcode & OPCODE) == SC)
530 return simulate_sc(regs, opcode);
1da177e4 531
60b0d655 532 return -1; /* Must be something else ... */
1da177e4
LT
533}
534
3c37026d
RB
535/*
536 * Simulate trapping 'rdhwr' instructions to provide user accessible
1f5826bd 537 * registers not implemented in hardware.
3c37026d 538 */
60b0d655 539static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
3c37026d 540{
dc8f6029 541 struct thread_info *ti = task_thread_info(current);
3c37026d
RB
542
543 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
544 int rd = (opcode & RD) >> 11;
545 int rt = (opcode & RT) >> 16;
546 switch (rd) {
1f5826bd
CD
547 case 0: /* CPU number */
548 regs->regs[rt] = smp_processor_id();
549 return 0;
550 case 1: /* SYNCI length */
551 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
552 current_cpu_data.icache.linesz);
553 return 0;
554 case 2: /* Read count register */
555 regs->regs[rt] = read_c0_count();
556 return 0;
557 case 3: /* Count register resolution */
558 switch (current_cpu_data.cputype) {
559 case CPU_20KC:
560 case CPU_25KF:
561 regs->regs[rt] = 1;
562 break;
3c37026d 563 default:
1f5826bd
CD
564 regs->regs[rt] = 2;
565 }
566 return 0;
567 case 29:
568 regs->regs[rt] = ti->tp_value;
569 return 0;
570 default:
571 return -1;
3c37026d
RB
572 }
573 }
574
56ebd51b 575 /* Not ours. */
60b0d655
MR
576 return -1;
577}
e5679882 578
60b0d655
MR
579static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
580{
581 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
582 return 0;
583
584 return -1; /* Must be something else ... */
3c37026d
RB
585}
586
1da177e4
LT
587asmlinkage void do_ov(struct pt_regs *regs)
588{
589 siginfo_t info;
590
36ccf1c0
RB
591 die_if_kernel("Integer overflow", regs);
592
1da177e4
LT
593 info.si_code = FPE_INTOVF;
594 info.si_signo = SIGFPE;
595 info.si_errno = 0;
fe00f943 596 info.si_addr = (void __user *) regs->cp0_epc;
1da177e4
LT
597 force_sig_info(SIGFPE, &info, current);
598}
599
600/*
601 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
602 */
603asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
604{
948a34cf
TS
605 siginfo_t info;
606
57725f9e
CD
607 die_if_kernel("FP exception in kernel code", regs);
608
1da177e4
LT
609 if (fcr31 & FPU_CSR_UNI_X) {
610 int sig;
611
1da177e4 612 /*
a3dddd56 613 * Unimplemented operation exception. If we've got the full
1da177e4
LT
614 * software emulator on-board, let's use it...
615 *
616 * Force FPU to dump state into task/thread context. We're
617 * moving a lot of data here for what is probably a single
618 * instruction, but the alternative is to pre-decode the FP
619 * register operands before invoking the emulator, which seems
620 * a bit extreme for what should be an infrequent event.
621 */
cd21dfcf 622 /* Ensure 'resume' not overwrite saved fp context again. */
53dc8028 623 lose_fpu(1);
1da177e4
LT
624
625 /* Run the emulator */
49a89efb 626 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
1da177e4
LT
627
628 /*
629 * We can't allow the emulated instruction to leave any of
630 * the cause bit set in $fcr31.
631 */
eae89076 632 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
1da177e4
LT
633
634 /* Restore the hardware register state */
53dc8028 635 own_fpu(1); /* Using the FPU again. */
1da177e4
LT
636
637 /* If something went wrong, signal */
638 if (sig)
639 force_sig(sig, current);
640
641 return;
948a34cf
TS
642 } else if (fcr31 & FPU_CSR_INV_X)
643 info.si_code = FPE_FLTINV;
644 else if (fcr31 & FPU_CSR_DIV_X)
645 info.si_code = FPE_FLTDIV;
646 else if (fcr31 & FPU_CSR_OVF_X)
647 info.si_code = FPE_FLTOVF;
648 else if (fcr31 & FPU_CSR_UDF_X)
649 info.si_code = FPE_FLTUND;
650 else if (fcr31 & FPU_CSR_INE_X)
651 info.si_code = FPE_FLTRES;
652 else
653 info.si_code = __SI_FAULT;
654 info.si_signo = SIGFPE;
655 info.si_errno = 0;
656 info.si_addr = (void __user *) regs->cp0_epc;
657 force_sig_info(SIGFPE, &info, current);
1da177e4
LT
658}
659
660asmlinkage void do_bp(struct pt_regs *regs)
661{
662 unsigned int opcode, bcode;
663 siginfo_t info;
664
ba755f8e 665 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
e5679882 666 goto out_sigsegv;
1da177e4
LT
667
668 /*
669 * There is the ancient bug in the MIPS assemblers that the break
670 * code starts left to bit 16 instead to bit 6 in the opcode.
671 * Gas is bug-compatible, but not always, grrr...
672 * We handle both cases with a simple heuristics. --macro
673 */
674 bcode = ((opcode >> 6) & ((1 << 20) - 1));
675 if (bcode < (1 << 10))
676 bcode <<= 10;
677
678 /*
679 * (A short test says that IRIX 5.3 sends SIGTRAP for all break
680 * insns, even for break codes that indicate arithmetic failures.
681 * Weird ...)
682 * But should we continue the brokenness??? --macro
683 */
684 switch (bcode) {
685 case BRK_OVERFLOW << 10:
686 case BRK_DIVZERO << 10:
63dc68a8 687 die_if_kernel("Break instruction in kernel code", regs);
1da177e4
LT
688 if (bcode == (BRK_DIVZERO << 10))
689 info.si_code = FPE_INTDIV;
690 else
691 info.si_code = FPE_INTOVF;
692 info.si_signo = SIGFPE;
693 info.si_errno = 0;
fe00f943 694 info.si_addr = (void __user *) regs->cp0_epc;
1da177e4
LT
695 force_sig_info(SIGFPE, &info, current);
696 break;
63dc68a8
RB
697 case BRK_BUG:
698 die("Kernel bug detected", regs);
699 break;
1da177e4 700 default:
63dc68a8 701 die_if_kernel("Break instruction in kernel code", regs);
1da177e4
LT
702 force_sig(SIGTRAP, current);
703 }
90fccb13 704 return;
e5679882
RB
705
706out_sigsegv:
707 force_sig(SIGSEGV, current);
1da177e4
LT
708}
709
710asmlinkage void do_tr(struct pt_regs *regs)
711{
712 unsigned int opcode, tcode = 0;
713 siginfo_t info;
714
ba755f8e 715 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
e5679882 716 goto out_sigsegv;
1da177e4
LT
717
718 /* Immediate versions don't provide a code. */
719 if (!(opcode & OPCODE))
720 tcode = ((opcode >> 6) & ((1 << 10) - 1));
721
722 /*
723 * (A short test says that IRIX 5.3 sends SIGTRAP for all trap
724 * insns, even for trap codes that indicate arithmetic failures.
725 * Weird ...)
726 * But should we continue the brokenness??? --macro
727 */
728 switch (tcode) {
729 case BRK_OVERFLOW:
730 case BRK_DIVZERO:
63dc68a8 731 die_if_kernel("Trap instruction in kernel code", regs);
1da177e4
LT
732 if (tcode == BRK_DIVZERO)
733 info.si_code = FPE_INTDIV;
734 else
735 info.si_code = FPE_INTOVF;
736 info.si_signo = SIGFPE;
737 info.si_errno = 0;
fe00f943 738 info.si_addr = (void __user *) regs->cp0_epc;
1da177e4
LT
739 force_sig_info(SIGFPE, &info, current);
740 break;
63dc68a8
RB
741 case BRK_BUG:
742 die("Kernel bug detected", regs);
743 break;
1da177e4 744 default:
63dc68a8 745 die_if_kernel("Trap instruction in kernel code", regs);
1da177e4
LT
746 force_sig(SIGTRAP, current);
747 }
90fccb13 748 return;
e5679882
RB
749
750out_sigsegv:
751 force_sig(SIGSEGV, current);
1da177e4
LT
752}
753
754asmlinkage void do_ri(struct pt_regs *regs)
755{
60b0d655
MR
756 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
757 unsigned long old_epc = regs->cp0_epc;
758 unsigned int opcode = 0;
759 int status = -1;
1da177e4 760
60b0d655 761 die_if_kernel("Reserved instruction in kernel code", regs);
1da177e4 762
60b0d655 763 if (unlikely(compute_return_epc(regs) < 0))
3c37026d
RB
764 return;
765
60b0d655
MR
766 if (unlikely(get_user(opcode, epc) < 0))
767 status = SIGSEGV;
768
769 if (!cpu_has_llsc && status < 0)
770 status = simulate_llsc(regs, opcode);
771
772 if (status < 0)
773 status = simulate_rdhwr(regs, opcode);
774
775 if (status < 0)
776 status = simulate_sync(regs, opcode);
777
778 if (status < 0)
779 status = SIGILL;
780
781 if (unlikely(status > 0)) {
782 regs->cp0_epc = old_epc; /* Undo skip-over. */
783 force_sig(status, current);
784 }
1da177e4
LT
785}
786
d223a861
RB
787/*
788 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
789 * emulated more than some threshold number of instructions, force migration to
790 * a "CPU" that has FP support.
791 */
792static void mt_ase_fp_affinity(void)
793{
794#ifdef CONFIG_MIPS_MT_FPAFF
795 if (mt_fpemul_threshold > 0 &&
796 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
797 /*
798 * If there's no FPU present, or if the application has already
799 * restricted the allowed set to exclude any CPUs with FPUs,
800 * we'll skip the procedure.
801 */
802 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
803 cpumask_t tmask;
804
805 cpus_and(tmask, current->thread.user_cpus_allowed,
806 mt_fpu_cpumask);
807 set_cpus_allowed(current, tmask);
293c5bd1 808 set_thread_flag(TIF_FPUBOUND);
d223a861
RB
809 }
810 }
811#endif /* CONFIG_MIPS_MT_FPAFF */
812}
813
1da177e4
LT
814asmlinkage void do_cpu(struct pt_regs *regs)
815{
60b0d655
MR
816 unsigned int __user *epc;
817 unsigned long old_epc;
818 unsigned int opcode;
1da177e4 819 unsigned int cpid;
60b0d655 820 int status;
1da177e4 821
5323180d
AN
822 die_if_kernel("do_cpu invoked from kernel context!", regs);
823
1da177e4
LT
824 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
825
826 switch (cpid) {
827 case 0:
60b0d655
MR
828 epc = (unsigned int __user *)exception_epc(regs);
829 old_epc = regs->cp0_epc;
830 opcode = 0;
831 status = -1;
1da177e4 832
60b0d655 833 if (unlikely(compute_return_epc(regs) < 0))
1da177e4 834 return;
3c37026d 835
60b0d655
MR
836 if (unlikely(get_user(opcode, epc) < 0))
837 status = SIGSEGV;
838
839 if (!cpu_has_llsc && status < 0)
840 status = simulate_llsc(regs, opcode);
841
842 if (status < 0)
843 status = simulate_rdhwr(regs, opcode);
844
845 if (status < 0)
846 status = SIGILL;
847
848 if (unlikely(status > 0)) {
849 regs->cp0_epc = old_epc; /* Undo skip-over. */
850 force_sig(status, current);
851 }
852
853 return;
1da177e4
LT
854
855 case 1:
53dc8028
AN
856 if (used_math()) /* Using the FPU again. */
857 own_fpu(1);
858 else { /* First time FPU user. */
1da177e4
LT
859 init_fpu();
860 set_used_math();
861 }
862
5323180d 863 if (!raw_cpu_has_fpu) {
e04582b7 864 int sig;
e04582b7
AN
865 sig = fpu_emulator_cop1Handler(regs,
866 &current->thread.fpu, 0);
1da177e4
LT
867 if (sig)
868 force_sig(sig, current);
d223a861
RB
869 else
870 mt_ase_fp_affinity();
1da177e4
LT
871 }
872
1da177e4
LT
873 return;
874
875 case 2:
876 case 3:
877 break;
878 }
879
880 force_sig(SIGILL, current);
881}
882
883asmlinkage void do_mdmx(struct pt_regs *regs)
884{
885 force_sig(SIGILL, current);
886}
887
888asmlinkage void do_watch(struct pt_regs *regs)
889{
9267a30d
MSJ
890 if (board_watchpoint_handler) {
891 (*board_watchpoint_handler)(regs);
892 return;
893 }
894
1da177e4
LT
895 /*
896 * We use the watch exception where available to detect stack
897 * overflows.
898 */
899 dump_tlb_all();
900 show_regs(regs);
901 panic("Caught WATCH exception - probably caused by stack overflow.");
902}
903
904asmlinkage void do_mcheck(struct pt_regs *regs)
905{
cac4bcbc
RB
906 const int field = 2 * sizeof(unsigned long);
907 int multi_match = regs->cp0_status & ST0_TS;
908
1da177e4 909 show_regs(regs);
cac4bcbc
RB
910
911 if (multi_match) {
912 printk("Index : %0x\n", read_c0_index());
913 printk("Pagemask: %0x\n", read_c0_pagemask());
914 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
915 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
916 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
917 printk("\n");
918 dump_tlb_all();
919 }
920
e1bb8289 921 show_code((unsigned int __user *) regs->cp0_epc);
cac4bcbc 922
1da177e4
LT
923 /*
924 * Some chips may have other causes of machine check (e.g. SB1
925 * graduation timer)
926 */
927 panic("Caught Machine Check exception - %scaused by multiple "
928 "matching entries in the TLB.",
cac4bcbc 929 (multi_match) ? "" : "not ");
1da177e4
LT
930}
931
340ee4b9
RB
932asmlinkage void do_mt(struct pt_regs *regs)
933{
41c594ab
RB
934 int subcode;
935
41c594ab
RB
936 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
937 >> VPECONTROL_EXCPT_SHIFT;
938 switch (subcode) {
939 case 0:
e35a5e35 940 printk(KERN_DEBUG "Thread Underflow\n");
41c594ab
RB
941 break;
942 case 1:
e35a5e35 943 printk(KERN_DEBUG "Thread Overflow\n");
41c594ab
RB
944 break;
945 case 2:
e35a5e35 946 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
41c594ab
RB
947 break;
948 case 3:
e35a5e35 949 printk(KERN_DEBUG "Gating Storage Exception\n");
41c594ab
RB
950 break;
951 case 4:
e35a5e35 952 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
41c594ab
RB
953 break;
954 case 5:
e35a5e35 955 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
41c594ab
RB
956 break;
957 default:
e35a5e35 958 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
41c594ab
RB
959 subcode);
960 break;
961 }
340ee4b9
RB
962 die_if_kernel("MIPS MT Thread exception in kernel", regs);
963
964 force_sig(SIGILL, current);
965}
966
967
e50c0a8f
RB
968asmlinkage void do_dsp(struct pt_regs *regs)
969{
970 if (cpu_has_dsp)
971 panic("Unexpected DSP exception\n");
972
973 force_sig(SIGILL, current);
974}
975
1da177e4
LT
976asmlinkage void do_reserved(struct pt_regs *regs)
977{
978 /*
979 * Game over - no way to handle this if it ever occurs. Most probably
980 * caused by a new unknown cpu type or after another deadly
981 * hard/software error.
982 */
983 show_regs(regs);
984 panic("Caught reserved exception %ld - should not happen.",
985 (regs->cp0_cause & 0x7f) >> 2);
986}
987
988/*
989 * Some MIPS CPUs can enable/disable for cache parity detection, but do
990 * it different ways.
991 */
992static inline void parity_protection_init(void)
993{
10cc3529 994 switch (current_cpu_type()) {
1da177e4 995 case CPU_24K:
98a41de9 996 case CPU_34K:
1da177e4 997 case CPU_5KC:
14f18b7f
RB
998 write_c0_ecc(0x80000000);
999 back_to_back_c0_hazard();
1000 /* Set the PE bit (bit 31) in the c0_errctl register. */
1001 printk(KERN_INFO "Cache parity protection %sabled\n",
1002 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
1da177e4
LT
1003 break;
1004 case CPU_20KC:
1005 case CPU_25KF:
1006 /* Clear the DE bit (bit 16) in the c0_status register. */
1007 printk(KERN_INFO "Enable cache parity protection for "
1008 "MIPS 20KC/25KF CPUs.\n");
1009 clear_c0_status(ST0_DE);
1010 break;
1011 default:
1012 break;
1013 }
1014}
1015
1016asmlinkage void cache_parity_error(void)
1017{
1018 const int field = 2 * sizeof(unsigned long);
1019 unsigned int reg_val;
1020
1021 /* For the moment, report the problem and hang. */
1022 printk("Cache error exception:\n");
1023 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1024 reg_val = read_c0_cacheerr();
1025 printk("c0_cacheerr == %08x\n", reg_val);
1026
1027 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1028 reg_val & (1<<30) ? "secondary" : "primary",
1029 reg_val & (1<<31) ? "data" : "insn");
1030 printk("Error bits: %s%s%s%s%s%s%s\n",
1031 reg_val & (1<<29) ? "ED " : "",
1032 reg_val & (1<<28) ? "ET " : "",
1033 reg_val & (1<<26) ? "EE " : "",
1034 reg_val & (1<<25) ? "EB " : "",
1035 reg_val & (1<<24) ? "EI " : "",
1036 reg_val & (1<<23) ? "E1 " : "",
1037 reg_val & (1<<22) ? "E0 " : "");
1038 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1039
ec917c2c 1040#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
1da177e4
LT
1041 if (reg_val & (1<<22))
1042 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1043
1044 if (reg_val & (1<<23))
1045 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1046#endif
1047
1048 panic("Can't handle the cache error!");
1049}
1050
1051/*
1052 * SDBBP EJTAG debug exception handler.
1053 * We skip the instruction and return to the next instruction.
1054 */
1055void ejtag_exception_handler(struct pt_regs *regs)
1056{
1057 const int field = 2 * sizeof(unsigned long);
1058 unsigned long depc, old_epc;
1059 unsigned int debug;
1060
70ae6126 1061 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
1da177e4
LT
1062 depc = read_c0_depc();
1063 debug = read_c0_debug();
70ae6126 1064 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
1da177e4
LT
1065 if (debug & 0x80000000) {
1066 /*
1067 * In branch delay slot.
1068 * We cheat a little bit here and use EPC to calculate the
1069 * debug return address (DEPC). EPC is restored after the
1070 * calculation.
1071 */
1072 old_epc = regs->cp0_epc;
1073 regs->cp0_epc = depc;
1074 __compute_return_epc(regs);
1075 depc = regs->cp0_epc;
1076 regs->cp0_epc = old_epc;
1077 } else
1078 depc += 4;
1079 write_c0_depc(depc);
1080
1081#if 0
70ae6126 1082 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
1da177e4
LT
1083 write_c0_debug(debug | 0x100);
1084#endif
1085}
1086
1087/*
1088 * NMI exception handler.
1089 */
34412c72 1090NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
1da177e4 1091{
41c594ab 1092 bust_spinlocks(1);
1da177e4
LT
1093 printk("NMI taken!!!!\n");
1094 die("NMI", regs);
1da177e4
LT
1095}
1096
e01402b1
RB
1097#define VECTORSPACING 0x100 /* for EI/VI mode */
1098
1099unsigned long ebase;
1da177e4 1100unsigned long exception_handlers[32];
e01402b1 1101unsigned long vi_handlers[64];
1da177e4
LT
1102
1103/*
1104 * As a side effect of the way this is implemented we're limited
1105 * to interrupt handlers in the address range from
1106 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1107 */
1108void *set_except_vector(int n, void *addr)
1109{
1110 unsigned long handler = (unsigned long) addr;
1111 unsigned long old_handler = exception_handlers[n];
1112
1113 exception_handlers[n] = handler;
1114 if (n == 0 && cpu_has_divec) {
ec70f65e
RB
1115 *(u32 *)(ebase + 0x200) = 0x08000000 |
1116 (0x03ffffff & (handler >> 2));
e01402b1
RB
1117 flush_icache_range(ebase + 0x200, ebase + 0x204);
1118 }
1119 return (void *)old_handler;
1120}
1121
6ba07e59
AN
1122static asmlinkage void do_default_vi(void)
1123{
1124 show_regs(get_irq_regs());
1125 panic("Caught unexpected vectored interrupt.");
1126}
1127
ef300e42 1128static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
e01402b1
RB
1129{
1130 unsigned long handler;
1131 unsigned long old_handler = vi_handlers[n];
f6771dbb 1132 int srssets = current_cpu_data.srsets;
e01402b1
RB
1133 u32 *w;
1134 unsigned char *b;
1135
1136 if (!cpu_has_veic && !cpu_has_vint)
1137 BUG();
1138
1139 if (addr == NULL) {
1140 handler = (unsigned long) do_default_vi;
1141 srs = 0;
41c594ab 1142 } else
e01402b1
RB
1143 handler = (unsigned long) addr;
1144 vi_handlers[n] = (unsigned long) addr;
1145
1146 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1147
f6771dbb 1148 if (srs >= srssets)
e01402b1
RB
1149 panic("Shadow register set %d not supported", srs);
1150
1151 if (cpu_has_veic) {
1152 if (board_bind_eic_interrupt)
49a89efb 1153 board_bind_eic_interrupt(n, srs);
41c594ab 1154 } else if (cpu_has_vint) {
e01402b1 1155 /* SRSMap is only defined if shadow sets are implemented */
f6771dbb 1156 if (srssets > 1)
49a89efb 1157 change_c0_srsmap(0xf << n*4, srs << n*4);
e01402b1
RB
1158 }
1159
1160 if (srs == 0) {
1161 /*
1162 * If no shadow set is selected then use the default handler
1163 * that does normal register saving and a standard interrupt exit
1164 */
1165
1166 extern char except_vec_vi, except_vec_vi_lui;
1167 extern char except_vec_vi_ori, except_vec_vi_end;
41c594ab
RB
1168#ifdef CONFIG_MIPS_MT_SMTC
1169 /*
1170 * We need to provide the SMTC vectored interrupt handler
1171 * not only with the address of the handler, but with the
1172 * Status.IM bit to be masked before going there.
1173 */
1174 extern char except_vec_vi_mori;
1175 const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
1176#endif /* CONFIG_MIPS_MT_SMTC */
e01402b1
RB
1177 const int handler_len = &except_vec_vi_end - &except_vec_vi;
1178 const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
1179 const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
1180
1181 if (handler_len > VECTORSPACING) {
1182 /*
1183 * Sigh... panicing won't help as the console
1184 * is probably not configured :(
1185 */
49a89efb 1186 panic("VECTORSPACING too small");
e01402b1
RB
1187 }
1188
49a89efb 1189 memcpy(b, &except_vec_vi, handler_len);
41c594ab 1190#ifdef CONFIG_MIPS_MT_SMTC
8e8a52ed
RB
1191 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1192
41c594ab
RB
1193 w = (u32 *)(b + mori_offset);
1194 *w = (*w & 0xffff0000) | (0x100 << n);
1195#endif /* CONFIG_MIPS_MT_SMTC */
e01402b1
RB
1196 w = (u32 *)(b + lui_offset);
1197 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1198 w = (u32 *)(b + ori_offset);
1199 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1200 flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
1201 }
1202 else {
1203 /*
1204 * In other cases jump directly to the interrupt handler
1205 *
1206 * It is the handlers responsibility to save registers if required
1207 * (eg hi/lo) and return from the exception using "eret"
1208 */
1209 w = (u32 *)b;
1210 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1211 *w = 0;
1212 flush_icache_range((unsigned long)b, (unsigned long)(b+8));
1da177e4 1213 }
e01402b1 1214
1da177e4
LT
1215 return (void *)old_handler;
1216}
1217
ef300e42 1218void *set_vi_handler(int n, vi_handler_t addr)
e01402b1 1219{
ff3eab2a 1220 return set_vi_srs_handler(n, addr, 0);
e01402b1 1221}
f41ae0b2 1222
1da177e4
LT
1223/*
1224 * This is used by native signal handling
1225 */
53dc8028
AN
1226asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1227asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
1da177e4 1228
53dc8028
AN
1229extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1230extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
1da177e4 1231
53dc8028
AN
1232extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1233extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
1da177e4 1234
41c594ab 1235#ifdef CONFIG_SMP
53dc8028 1236static int smp_save_fp_context(struct sigcontext __user *sc)
41c594ab 1237{
53dc8028 1238 return raw_cpu_has_fpu
41c594ab
RB
1239 ? _save_fp_context(sc)
1240 : fpu_emulator_save_context(sc);
1241}
1242
53dc8028 1243static int smp_restore_fp_context(struct sigcontext __user *sc)
41c594ab 1244{
53dc8028 1245 return raw_cpu_has_fpu
41c594ab
RB
1246 ? _restore_fp_context(sc)
1247 : fpu_emulator_restore_context(sc);
1248}
1249#endif
1250
1da177e4
LT
1251static inline void signal_init(void)
1252{
41c594ab
RB
1253#ifdef CONFIG_SMP
1254 /* For now just do the cpu_has_fpu check when the functions are invoked */
1255 save_fp_context = smp_save_fp_context;
1256 restore_fp_context = smp_restore_fp_context;
1257#else
1da177e4
LT
1258 if (cpu_has_fpu) {
1259 save_fp_context = _save_fp_context;
1260 restore_fp_context = _restore_fp_context;
1261 } else {
1262 save_fp_context = fpu_emulator_save_context;
1263 restore_fp_context = fpu_emulator_restore_context;
1264 }
41c594ab 1265#endif
1da177e4
LT
1266}
1267
1268#ifdef CONFIG_MIPS32_COMPAT
1269
1270/*
1271 * This is used by 32-bit signal stuff on the 64-bit kernel
1272 */
53dc8028
AN
1273asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1274asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
1da177e4 1275
53dc8028
AN
1276extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1277extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
1da177e4 1278
53dc8028
AN
1279extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1280extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
1da177e4
LT
1281
1282static inline void signal32_init(void)
1283{
1284 if (cpu_has_fpu) {
1285 save_fp_context32 = _save_fp_context32;
1286 restore_fp_context32 = _restore_fp_context32;
1287 } else {
1288 save_fp_context32 = fpu_emulator_save_context32;
1289 restore_fp_context32 = fpu_emulator_restore_context32;
1290 }
1291}
1292#endif
1293
1294extern void cpu_cache_init(void);
1295extern void tlb_init(void);
1d40cfcd 1296extern void flush_tlb_handlers(void);
1da177e4 1297
42f77542
RB
1298/*
1299 * Timer interrupt
1300 */
1301int cp0_compare_irq;
1302
1303/*
1304 * Performance counter IRQ or -1 if shared with timer
1305 */
1306int cp0_perfcount_irq;
1307EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1308
bdc94eb4
CD
1309static int __cpuinitdata noulri;
1310
1311static int __init ulri_disable(char *s)
1312{
1313 pr_info("Disabling ulri\n");
1314 noulri = 1;
1315
1316 return 1;
1317}
1318__setup("noulri", ulri_disable);
1319
234fcd14 1320void __cpuinit per_cpu_trap_init(void)
1da177e4
LT
1321{
1322 unsigned int cpu = smp_processor_id();
1323 unsigned int status_set = ST0_CU0;
41c594ab
RB
1324#ifdef CONFIG_MIPS_MT_SMTC
1325 int secondaryTC = 0;
1326 int bootTC = (cpu == 0);
1327
1328 /*
1329 * Only do per_cpu_trap_init() for first TC of Each VPE.
1330 * Note that this hack assumes that the SMTC init code
1331 * assigns TCs consecutively and in ascending order.
1332 */
1333
1334 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1335 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1336 secondaryTC = 1;
1337#endif /* CONFIG_MIPS_MT_SMTC */
1da177e4
LT
1338
1339 /*
1340 * Disable coprocessors and select 32-bit or 64-bit addressing
1341 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1342 * flag that some firmware may have left set and the TS bit (for
1343 * IP27). Set XX for ISA IV code to work.
1344 */
875d43e7 1345#ifdef CONFIG_64BIT
1da177e4
LT
1346 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1347#endif
1348 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1349 status_set |= ST0_XX;
bbaf238b
CD
1350 if (cpu_has_dsp)
1351 status_set |= ST0_MX;
1352
b38c7399 1353 change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1da177e4
LT
1354 status_set);
1355
e01402b1 1356#ifdef CONFIG_CPU_MIPSR2
a3692020
RB
1357 if (cpu_has_mips_r2) {
1358 unsigned int enable = 0x0000000f;
1359
bdc94eb4 1360 if (!noulri && cpu_has_userlocal)
a3692020
RB
1361 enable |= (1 << 29);
1362
1363 write_c0_hwrena(enable);
1364 }
e01402b1
RB
1365#endif
1366
41c594ab
RB
1367#ifdef CONFIG_MIPS_MT_SMTC
1368 if (!secondaryTC) {
1369#endif /* CONFIG_MIPS_MT_SMTC */
1370
e01402b1 1371 if (cpu_has_veic || cpu_has_vint) {
49a89efb 1372 write_c0_ebase(ebase);
e01402b1 1373 /* Setting vector spacing enables EI/VI mode */
49a89efb 1374 change_c0_intctl(0x3e0, VECTORSPACING);
e01402b1 1375 }
d03d0a57
RB
1376 if (cpu_has_divec) {
1377 if (cpu_has_mipsmt) {
1378 unsigned int vpflags = dvpe();
1379 set_c0_cause(CAUSEF_IV);
1380 evpe(vpflags);
1381 } else
1382 set_c0_cause(CAUSEF_IV);
1383 }
3b1d4ed5
RB
1384
1385 /*
1386 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1387 *
1388 * o read IntCtl.IPTI to determine the timer interrupt
1389 * o read IntCtl.IPPCI to determine the performance counter interrupt
1390 */
1391 if (cpu_has_mips_r2) {
49a89efb
RB
1392 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1393 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
c3e838a2 1394 if (cp0_perfcount_irq == cp0_compare_irq)
3b1d4ed5 1395 cp0_perfcount_irq = -1;
c3e838a2
CD
1396 } else {
1397 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
1398 cp0_perfcount_irq = -1;
3b1d4ed5
RB
1399 }
1400
41c594ab
RB
1401#ifdef CONFIG_MIPS_MT_SMTC
1402 }
1403#endif /* CONFIG_MIPS_MT_SMTC */
1da177e4
LT
1404
1405 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1406 TLBMISS_HANDLER_SETUP();
1407
1408 atomic_inc(&init_mm.mm_count);
1409 current->active_mm = &init_mm;
1410 BUG_ON(current->mm);
1411 enter_lazy_tlb(&init_mm, current);
1412
41c594ab
RB
1413#ifdef CONFIG_MIPS_MT_SMTC
1414 if (bootTC) {
1415#endif /* CONFIG_MIPS_MT_SMTC */
1416 cpu_cache_init();
1417 tlb_init();
1418#ifdef CONFIG_MIPS_MT_SMTC
6a05888d
RB
1419 } else if (!secondaryTC) {
1420 /*
1421 * First TC in non-boot VPE must do subset of tlb_init()
1422 * for MMU countrol registers.
1423 */
1424 write_c0_pagemask(PM_DEFAULT_MASK);
1425 write_c0_wired(0);
41c594ab
RB
1426 }
1427#endif /* CONFIG_MIPS_MT_SMTC */
1da177e4
LT
1428}
1429
e01402b1 1430/* Install CPU exception handler */
49a89efb 1431void __init set_handler(unsigned long offset, void *addr, unsigned long size)
e01402b1
RB
1432{
1433 memcpy((void *)(ebase + offset), addr, size);
1434 flush_icache_range(ebase + offset, ebase + offset + size);
1435}
1436
234fcd14 1437static char panic_null_cerr[] __cpuinitdata =
641e97f3
RB
1438 "Trying to set NULL cache error exception handler";
1439
e01402b1 1440/* Install uncached CPU exception handler */
234fcd14
RB
1441void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1442 unsigned long size)
e01402b1
RB
1443{
1444#ifdef CONFIG_32BIT
1445 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1446#endif
1447#ifdef CONFIG_64BIT
1448 unsigned long uncached_ebase = TO_UNCAC(ebase);
1449#endif
1450
641e97f3
RB
1451 if (!addr)
1452 panic(panic_null_cerr);
1453
e01402b1
RB
1454 memcpy((void *)(uncached_ebase + offset), addr, size);
1455}
1456
5b10496b
AN
1457static int __initdata rdhwr_noopt;
1458static int __init set_rdhwr_noopt(char *str)
1459{
1460 rdhwr_noopt = 1;
1461 return 1;
1462}
1463
1464__setup("rdhwr_noopt", set_rdhwr_noopt);
1465
1da177e4
LT
1466void __init trap_init(void)
1467{
1468 extern char except_vec3_generic, except_vec3_r4000;
1da177e4
LT
1469 extern char except_vec4;
1470 unsigned long i;
1471
e01402b1 1472 if (cpu_has_veic || cpu_has_vint)
49a89efb 1473 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
e01402b1
RB
1474 else
1475 ebase = CAC_BASE;
1476
1da177e4
LT
1477 per_cpu_trap_init();
1478
1479 /*
1480 * Copy the generic exception handlers to their final destination.
1481 * This will be overriden later as suitable for a particular
1482 * configuration.
1483 */
e01402b1 1484 set_handler(0x180, &except_vec3_generic, 0x80);
1da177e4
LT
1485
1486 /*
1487 * Setup default vectors
1488 */
1489 for (i = 0; i <= 31; i++)
1490 set_except_vector(i, handle_reserved);
1491
1492 /*
1493 * Copy the EJTAG debug exception vector handler code to it's final
1494 * destination.
1495 */
e01402b1 1496 if (cpu_has_ejtag && board_ejtag_handler_setup)
49a89efb 1497 board_ejtag_handler_setup();
1da177e4
LT
1498
1499 /*
1500 * Only some CPUs have the watch exceptions.
1501 */
1502 if (cpu_has_watch)
1503 set_except_vector(23, handle_watch);
1504
1505 /*
e01402b1 1506 * Initialise interrupt handlers
1da177e4 1507 */
e01402b1
RB
1508 if (cpu_has_veic || cpu_has_vint) {
1509 int nvec = cpu_has_veic ? 64 : 8;
1510 for (i = 0; i < nvec; i++)
ff3eab2a 1511 set_vi_handler(i, NULL);
e01402b1
RB
1512 }
1513 else if (cpu_has_divec)
1514 set_handler(0x200, &except_vec4, 0x8);
1da177e4
LT
1515
1516 /*
1517 * Some CPUs can enable/disable for cache parity detection, but does
1518 * it different ways.
1519 */
1520 parity_protection_init();
1521
1522 /*
1523 * The Data Bus Errors / Instruction Bus Errors are signaled
1524 * by external hardware. Therefore these two exceptions
1525 * may have board specific handlers.
1526 */
1527 if (board_be_init)
1528 board_be_init();
1529
e4ac58af 1530 set_except_vector(0, handle_int);
1da177e4
LT
1531 set_except_vector(1, handle_tlbm);
1532 set_except_vector(2, handle_tlbl);
1533 set_except_vector(3, handle_tlbs);
1534
1535 set_except_vector(4, handle_adel);
1536 set_except_vector(5, handle_ades);
1537
1538 set_except_vector(6, handle_ibe);
1539 set_except_vector(7, handle_dbe);
1540
1541 set_except_vector(8, handle_sys);
1542 set_except_vector(9, handle_bp);
5b10496b
AN
1543 set_except_vector(10, rdhwr_noopt ? handle_ri :
1544 (cpu_has_vtag_icache ?
1545 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
1da177e4
LT
1546 set_except_vector(11, handle_cpu);
1547 set_except_vector(12, handle_ov);
1548 set_except_vector(13, handle_tr);
1da177e4 1549
10cc3529
RB
1550 if (current_cpu_type() == CPU_R6000 ||
1551 current_cpu_type() == CPU_R6000A) {
1da177e4
LT
1552 /*
1553 * The R6000 is the only R-series CPU that features a machine
1554 * check exception (similar to the R4000 cache error) and
1555 * unaligned ldc1/sdc1 exception. The handlers have not been
1556 * written yet. Well, anyway there is no R6000 machine on the
1557 * current list of targets for Linux/MIPS.
1558 * (Duh, crap, there is someone with a triple R6k machine)
1559 */
1560 //set_except_vector(14, handle_mc);
1561 //set_except_vector(15, handle_ndc);
1562 }
1563
e01402b1
RB
1564
1565 if (board_nmi_handler_setup)
1566 board_nmi_handler_setup();
1567
e50c0a8f
RB
1568 if (cpu_has_fpu && !cpu_has_nofpuex)
1569 set_except_vector(15, handle_fpe);
1570
1571 set_except_vector(22, handle_mdmx);
1572
1573 if (cpu_has_mcheck)
1574 set_except_vector(24, handle_mcheck);
1575
340ee4b9
RB
1576 if (cpu_has_mipsmt)
1577 set_except_vector(25, handle_mt);
1578
acaec427 1579 set_except_vector(26, handle_dsp);
e50c0a8f
RB
1580
1581 if (cpu_has_vce)
1582 /* Special exception: R4[04]00 uses also the divec space. */
1583 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1584 else if (cpu_has_4kex)
1585 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1586 else
1587 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1588
1da177e4
LT
1589 signal_init();
1590#ifdef CONFIG_MIPS32_COMPAT
1591 signal32_init();
1592#endif
1593
e01402b1 1594 flush_icache_range(ebase, ebase + 0x400);
1d40cfcd 1595 flush_tlb_handlers();
1da177e4 1596}