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