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