blob: b8ea4e9d0d87ee8c169feafb93c8000231d3158b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Ralf Baechle36ccf1c2006-02-14 21:04:54 +00006 * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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.
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +010012 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Ralf Baechle8e8a52e2007-05-31 14:00:19 +010014#include <linux/bug.h>
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +010015#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
17#include <linux/mm.h>
18#include <linux/module.h>
19#include <linux/sched.h>
20#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/spinlock.h>
22#include <linux/kallsyms.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000023#include <linux/bootmem.h>
Maxime Bizond4fd1982006-07-20 18:52:02 +020024#include <linux/interrupt.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010025#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/bootinfo.h>
28#include <asm/branch.h>
29#include <asm/break.h>
30#include <asm/cpu.h>
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000031#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/fpu.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000033#include <asm/mipsregs.h>
34#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/module.h>
36#include <asm/pgtable.h>
37#include <asm/ptrace.h>
38#include <asm/sections.h>
39#include <asm/system.h>
40#include <asm/tlbdebug.h>
41#include <asm/traps.h>
42#include <asm/uaccess.h>
43#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090045#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010047extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048extern asmlinkage void handle_tlbm(void);
49extern asmlinkage void handle_tlbl(void);
50extern asmlinkage void handle_tlbs(void);
51extern asmlinkage void handle_adel(void);
52extern asmlinkage void handle_ades(void);
53extern asmlinkage void handle_ibe(void);
54extern asmlinkage void handle_dbe(void);
55extern asmlinkage void handle_sys(void);
56extern asmlinkage void handle_bp(void);
57extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090058extern asmlinkage void handle_ri_rdhwr_vivt(void);
59extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060extern asmlinkage void handle_cpu(void);
61extern asmlinkage void handle_ov(void);
62extern asmlinkage void handle_tr(void);
63extern asmlinkage void handle_fpe(void);
64extern asmlinkage void handle_mdmx(void);
65extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000066extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000067extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern asmlinkage void handle_mcheck(void);
69extern asmlinkage void handle_reserved(void);
70
Ralf Baechle12616ed2005-10-18 10:26:46 +010071extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090072 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74void (*board_be_init)(void);
75int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000076void (*board_nmi_handler_setup)(void);
77void (*board_ejtag_handler_setup)(void);
78void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020081static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090082{
Ralf Baechle39b8d522008-04-28 17:14:26 +010083 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090084 unsigned long addr;
85
86 printk("Call Trace:");
87#ifdef CONFIG_KALLSYMS
88 printk("\n");
89#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +020090 while (!kstack_end(sp)) {
91 unsigned long __user *p =
92 (unsigned long __user *)(unsigned long)sp++;
93 if (__get_user(addr, p)) {
94 printk(" (Bad stack address)");
95 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +010096 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +020097 if (__kernel_text_address(addr))
98 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090099 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200100 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900101}
102
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900103#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900104int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900105static int __init set_raw_show_trace(char *str)
106{
107 raw_show_trace = 1;
108 return 1;
109}
110__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900111#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200112
Ralf Baechleeae23f22007-10-14 23:27:21 +0100113static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900114{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200115 unsigned long sp = regs->regs[29];
116 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900117 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900118
119 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200120 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900121 return;
122 }
123 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200124 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200125 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900126 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200127 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900128 printk("\n");
129}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
132 * This routine abuses get_user()/put_user() to reference pointers
133 * with at least a bit of error checking ...
134 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100135static void show_stacktrace(struct task_struct *task,
136 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 const int field = 2 * sizeof(unsigned long);
139 long stackdata;
140 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900141 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 printk("Stack :");
144 i = 0;
145 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
146 if (i && ((i % (64 / field)) == 0))
147 printk("\n ");
148 if (i > 39) {
149 printk(" ...");
150 break;
151 }
152
153 if (__get_user(stackdata, sp++)) {
154 printk(" (Bad stack address)");
155 break;
156 }
157
158 printk(" %0*lx", field, stackdata);
159 i++;
160 }
161 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200162 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900163}
164
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900165void show_stack(struct task_struct *task, unsigned long *sp)
166{
167 struct pt_regs regs;
168 if (sp) {
169 regs.regs[29] = (unsigned long)sp;
170 regs.regs[31] = 0;
171 regs.cp0_epc = 0;
172 } else {
173 if (task && task != current) {
174 regs.regs[29] = task->thread.reg29;
175 regs.regs[31] = 0;
176 regs.cp0_epc = task->thread.reg31;
177 } else {
178 prepare_frametrace(&regs);
179 }
180 }
181 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
185 * The architecture-independent dump_stack generator
186 */
187void dump_stack(void)
188{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200189 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200191 prepare_frametrace(&regs);
192 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195EXPORT_SYMBOL(dump_stack);
196
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900197static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100200 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 printk("\nCode:");
203
Ralf Baechle39b8d522008-04-28 17:14:26 +0100204 if ((unsigned long)pc & 1)
205 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 for(i = -3 ; i < 6 ; i++) {
207 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100208 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 printk(" (Bad address in epc)\n");
210 break;
211 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100212 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214}
215
Ralf Baechleeae23f22007-10-14 23:27:21 +0100216static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 const int field = 2 * sizeof(unsigned long);
219 unsigned int cause = regs->cp0_cause;
220 int i;
221
222 printk("Cpu %d\n", smp_processor_id());
223
224 /*
225 * Saved main processor registers
226 */
227 for (i = 0; i < 32; ) {
228 if ((i % 4) == 0)
229 printk("$%2d :", i);
230 if (i == 0)
231 printk(" %0*lx", field, 0UL);
232 else if (i == 26 || i == 27)
233 printk(" %*s", field, "");
234 else
235 printk(" %0*lx", field, regs->regs[i]);
236
237 i++;
238 if ((i % 4) == 0)
239 printk("\n");
240 }
241
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100242#ifdef CONFIG_CPU_HAS_SMARTMIPS
243 printk("Acx : %0*lx\n", field, regs->acx);
244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 printk("Hi : %0*lx\n", field, regs->hi);
246 printk("Lo : %0*lx\n", field, regs->lo);
247
248 /*
249 * Saved cp0 registers
250 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100251 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
252 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100254 printk("ra : %0*lx %pS\n", field, regs->regs[31],
255 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 printk("Status: %08x ", (uint32_t) regs->cp0_status);
258
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000259 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
260 if (regs->cp0_status & ST0_KUO)
261 printk("KUo ");
262 if (regs->cp0_status & ST0_IEO)
263 printk("IEo ");
264 if (regs->cp0_status & ST0_KUP)
265 printk("KUp ");
266 if (regs->cp0_status & ST0_IEP)
267 printk("IEp ");
268 if (regs->cp0_status & ST0_KUC)
269 printk("KUc ");
270 if (regs->cp0_status & ST0_IEC)
271 printk("IEc ");
272 } else {
273 if (regs->cp0_status & ST0_KX)
274 printk("KX ");
275 if (regs->cp0_status & ST0_SX)
276 printk("SX ");
277 if (regs->cp0_status & ST0_UX)
278 printk("UX ");
279 switch (regs->cp0_status & ST0_KSU) {
280 case KSU_USER:
281 printk("USER ");
282 break;
283 case KSU_SUPERVISOR:
284 printk("SUPERVISOR ");
285 break;
286 case KSU_KERNEL:
287 printk("KERNEL ");
288 break;
289 default:
290 printk("BAD_MODE ");
291 break;
292 }
293 if (regs->cp0_status & ST0_ERL)
294 printk("ERL ");
295 if (regs->cp0_status & ST0_EXL)
296 printk("EXL ");
297 if (regs->cp0_status & ST0_IE)
298 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 printk("\n");
301
302 printk("Cause : %08x\n", cause);
303
304 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
305 if (1 <= cause && cause <= 5)
306 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
307
Ralf Baechle9966db252007-10-11 23:46:17 +0100308 printk("PrId : %08x (%s)\n", read_c0_prid(),
309 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Ralf Baechleeae23f22007-10-14 23:27:21 +0100312/*
313 * FIXME: really the generic show_regs should take a const pointer argument.
314 */
315void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100317 __show_regs((struct pt_regs *)regs);
318}
319
320void show_registers(const struct pt_regs *regs)
321{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100322 const int field = 2 * sizeof(unsigned long);
323
Ralf Baechleeae23f22007-10-14 23:27:21 +0100324 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100326 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
327 current->comm, current->pid, current_thread_info(), current,
328 field, current_thread_info()->tp_value);
329 if (cpu_has_userlocal) {
330 unsigned long tls;
331
332 tls = read_c0_userlocal();
333 if (tls != current_thread_info()->tp_value)
334 printk("*HwTLS: %0*lx\n", field, tls);
335 }
336
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900337 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900338 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 printk("\n");
340}
341
342static DEFINE_SPINLOCK(die_lock);
343
Ralf Baechleeae23f22007-10-14 23:27:21 +0100344void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100347#ifdef CONFIG_MIPS_MT_SMTC
348 unsigned long dvpret = dvpe();
349#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 console_verbose();
352 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100353 bust_spinlocks(1);
354#ifdef CONFIG_MIPS_MT_SMTC
355 mips_mt_regdump(dvpret);
356#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100357 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700359 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200361
362 if (in_interrupt())
363 panic("Fatal exception in interrupt");
364
365 if (panic_on_oops) {
366 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
367 ssleep(5);
368 panic("Fatal exception");
369 }
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 do_exit(SIGSEGV);
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374extern const struct exception_table_entry __start___dbe_table[];
375extern const struct exception_table_entry __stop___dbe_table[];
376
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000377__asm__(
378" .section __dbe_table, \"a\"\n"
379" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/* Given an address, look for it in the exception tables. */
382static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
383{
384 const struct exception_table_entry *e;
385
386 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
387 if (!e)
388 e = search_module_dbetables(addr);
389 return e;
390}
391
392asmlinkage void do_be(struct pt_regs *regs)
393{
394 const int field = 2 * sizeof(unsigned long);
395 const struct exception_table_entry *fixup = NULL;
396 int data = regs->cp0_cause & 4;
397 int action = MIPS_BE_FATAL;
398
399 /* XXX For now. Fixme, this searches the wrong table ... */
400 if (data && !user_mode(regs))
401 fixup = search_dbe_tables(exception_epc(regs));
402
403 if (fixup)
404 action = MIPS_BE_FIXUP;
405
406 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900407 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 switch (action) {
410 case MIPS_BE_DISCARD:
411 return;
412 case MIPS_BE_FIXUP:
413 if (fixup) {
414 regs->cp0_epc = fixup->nextinsn;
415 return;
416 }
417 break;
418 default:
419 break;
420 }
421
422 /*
423 * Assume it would be too dangerous to continue ...
424 */
425 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
426 data ? "Data" : "Instruction",
427 field, regs->cp0_epc, field, regs->regs[31]);
428 die_if_kernel("Oops", regs);
429 force_sig(SIGBUS, current);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100433 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
435
436#define OPCODE 0xfc000000
437#define BASE 0x03e00000
438#define RT 0x001f0000
439#define OFFSET 0x0000ffff
440#define LL 0xc0000000
441#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100442#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000443#define SPEC3 0x7c000000
444#define RD 0x0000f800
445#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100446#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000447#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/*
450 * The ll_bit is cleared by r*_switch.S
451 */
452
453unsigned long ll_bit;
454
455static struct task_struct *ll_task = NULL;
456
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100457static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000459 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * analyse the ll instruction that just caused a ri exception
464 * and put the referenced address to addr.
465 */
466
467 /* sign extend offset */
468 offset = opcode & OFFSET;
469 offset <<= 16;
470 offset >>= 16;
471
Ralf Baechlefe00f942005-03-01 19:22:29 +0000472 vaddr = (unsigned long __user *)
473 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100475 if ((unsigned long)vaddr & 3)
476 return SIGBUS;
477 if (get_user(value, vaddr))
478 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 preempt_disable();
481
482 if (ll_task == NULL || ll_task == current) {
483 ll_bit = 1;
484 } else {
485 ll_bit = 0;
486 }
487 ll_task = current;
488
489 preempt_enable();
490
491 regs->regs[(opcode & RT) >> 16] = value;
492
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100496static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000498 unsigned long __user *vaddr;
499 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * analyse the sc instruction that just caused a ri exception
504 * and put the referenced address to addr.
505 */
506
507 /* sign extend offset */
508 offset = opcode & OFFSET;
509 offset <<= 16;
510 offset >>= 16;
511
Ralf Baechlefe00f942005-03-01 19:22:29 +0000512 vaddr = (unsigned long __user *)
513 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 reg = (opcode & RT) >> 16;
515
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100516 if ((unsigned long)vaddr & 3)
517 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 preempt_disable();
520
521 if (ll_bit == 0 || ll_task != current) {
522 regs->regs[reg] = 0;
523 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
527 preempt_enable();
528
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100529 if (put_user(regs->regs[reg], vaddr))
530 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 regs->regs[reg] = 1;
533
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*
538 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
539 * opcodes are supposed to result in coprocessor unusable exceptions if
540 * executed on ll/sc-less processors. That's the theory. In practice a
541 * few processors such as NEC's VR4100 throw reserved instruction exceptions
542 * instead, so we're doing the emulation thing in both exception handlers.
543 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100544static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100546 if ((opcode & OPCODE) == LL)
547 return simulate_ll(regs, opcode);
548 if ((opcode & OPCODE) == SC)
549 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100551 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Ralf Baechle3c370262005-04-13 17:43:59 +0000554/*
555 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100556 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000557 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100558static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000559{
Al Virodc8f6022006-01-12 01:06:07 -0800560 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000561
562 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
563 int rd = (opcode & RD) >> 11;
564 int rt = (opcode & RT) >> 16;
565 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100566 case 0: /* CPU number */
567 regs->regs[rt] = smp_processor_id();
568 return 0;
569 case 1: /* SYNCI length */
570 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
571 current_cpu_data.icache.linesz);
572 return 0;
573 case 2: /* Read count register */
574 regs->regs[rt] = read_c0_count();
575 return 0;
576 case 3: /* Count register resolution */
577 switch (current_cpu_data.cputype) {
578 case CPU_20KC:
579 case CPU_25KF:
580 regs->regs[rt] = 1;
581 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000582 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100583 regs->regs[rt] = 2;
584 }
585 return 0;
586 case 29:
587 regs->regs[rt] = ti->tp_value;
588 return 0;
589 default:
590 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000591 }
592 }
593
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500594 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100595 return -1;
596}
Ralf Baechlee5679882006-11-30 01:14:47 +0000597
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100598static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
599{
600 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
601 return 0;
602
603 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606asmlinkage void do_ov(struct pt_regs *regs)
607{
608 siginfo_t info;
609
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000610 die_if_kernel("Integer overflow", regs);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 info.si_code = FPE_INTOVF;
613 info.si_signo = SIGFPE;
614 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000615 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 force_sig_info(SIGFPE, &info, current);
617}
618
619/*
620 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
621 */
622asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
623{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100624 siginfo_t info;
625
Chris Dearman57725f92006-06-30 23:35:28 +0100626 die_if_kernel("FP exception in kernel code", regs);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (fcr31 & FPU_CSR_UNI_X) {
629 int sig;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000632 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 * software emulator on-board, let's use it...
634 *
635 * Force FPU to dump state into task/thread context. We're
636 * moving a lot of data here for what is probably a single
637 * instruction, but the alternative is to pre-decode the FP
638 * register operands before invoking the emulator, which seems
639 * a bit extreme for what should be an infrequent event.
640 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000641 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900642 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100645 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * We can't allow the emulated instruction to leave any of
649 * the cause bit set in $fcr31.
650 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900651 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900654 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* If something went wrong, signal */
657 if (sig)
658 force_sig(sig, current);
659
660 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100661 } else if (fcr31 & FPU_CSR_INV_X)
662 info.si_code = FPE_FLTINV;
663 else if (fcr31 & FPU_CSR_DIV_X)
664 info.si_code = FPE_FLTDIV;
665 else if (fcr31 & FPU_CSR_OVF_X)
666 info.si_code = FPE_FLTOVF;
667 else if (fcr31 & FPU_CSR_UDF_X)
668 info.si_code = FPE_FLTUND;
669 else if (fcr31 & FPU_CSR_INE_X)
670 info.si_code = FPE_FLTRES;
671 else
672 info.si_code = __SI_FAULT;
673 info.si_signo = SIGFPE;
674 info.si_errno = 0;
675 info.si_addr = (void __user *) regs->cp0_epc;
676 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Ralf Baechledf270052008-04-20 16:28:54 +0100679static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
680 const char *str)
681{
682 siginfo_t info;
683 char b[40];
684
685 /*
686 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
687 * insns, even for trap and break codes that indicate arithmetic
688 * failures. Weird ...
689 * But should we continue the brokenness??? --macro
690 */
691 switch (code) {
692 case BRK_OVERFLOW:
693 case BRK_DIVZERO:
694 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
695 die_if_kernel(b, regs);
696 if (code == BRK_DIVZERO)
697 info.si_code = FPE_INTDIV;
698 else
699 info.si_code = FPE_INTOVF;
700 info.si_signo = SIGFPE;
701 info.si_errno = 0;
702 info.si_addr = (void __user *) regs->cp0_epc;
703 force_sig_info(SIGFPE, &info, current);
704 break;
705 case BRK_BUG:
706 die_if_kernel("Kernel bug detected", regs);
707 force_sig(SIGTRAP, current);
708 break;
709 default:
710 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
711 die_if_kernel(b, regs);
712 force_sig(SIGTRAP, current);
713 }
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716asmlinkage void do_bp(struct pt_regs *regs)
717{
718 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900720 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000721 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * There is the ancient bug in the MIPS assemblers that the break
725 * code starts left to bit 16 instead to bit 6 in the opcode.
726 * Gas is bug-compatible, but not always, grrr...
727 * We handle both cases with a simple heuristics. --macro
728 */
729 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100730 if (bcode >= (1 << 10))
731 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Ralf Baechledf270052008-04-20 16:28:54 +0100733 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900734 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000735
736out_sigsegv:
737 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740asmlinkage void do_tr(struct pt_regs *regs)
741{
742 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900744 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000745 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Immediate versions don't provide a code. */
748 if (!(opcode & OPCODE))
749 tcode = ((opcode >> 6) & ((1 << 10) - 1));
750
Ralf Baechledf270052008-04-20 16:28:54 +0100751 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900752 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000753
754out_sigsegv:
755 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758asmlinkage void do_ri(struct pt_regs *regs)
759{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100760 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
761 unsigned long old_epc = regs->cp0_epc;
762 unsigned int opcode = 0;
763 int status = -1;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 die_if_kernel("Reserved instruction in kernel code", regs);
766
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100767 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000768 return;
769
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100770 if (unlikely(get_user(opcode, epc) < 0))
771 status = SIGSEGV;
772
773 if (!cpu_has_llsc && status < 0)
774 status = simulate_llsc(regs, opcode);
775
776 if (status < 0)
777 status = simulate_rdhwr(regs, opcode);
778
779 if (status < 0)
780 status = simulate_sync(regs, opcode);
781
782 if (status < 0)
783 status = SIGILL;
784
785 if (unlikely(status > 0)) {
786 regs->cp0_epc = old_epc; /* Undo skip-over. */
787 force_sig(status, current);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Ralf Baechled223a862007-07-10 17:33:02 +0100791/*
792 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
793 * emulated more than some threshold number of instructions, force migration to
794 * a "CPU" that has FP support.
795 */
796static void mt_ase_fp_affinity(void)
797{
798#ifdef CONFIG_MIPS_MT_FPAFF
799 if (mt_fpemul_threshold > 0 &&
800 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
801 /*
802 * If there's no FPU present, or if the application has already
803 * restricted the allowed set to exclude any CPUs with FPUs,
804 * we'll skip the procedure.
805 */
806 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
807 cpumask_t tmask;
808
809 cpus_and(tmask, current->thread.user_cpus_allowed,
810 mt_fpu_cpumask);
811 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100812 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100813 }
814 }
815#endif /* CONFIG_MIPS_MT_FPAFF */
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818asmlinkage void do_cpu(struct pt_regs *regs)
819{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100820 unsigned int __user *epc;
821 unsigned long old_epc;
822 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100824 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Atsushi Nemoto53231802007-04-14 02:37:26 +0900826 die_if_kernel("do_cpu invoked from kernel context!", regs);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
829
830 switch (cpid) {
831 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100832 epc = (unsigned int __user *)exception_epc(regs);
833 old_epc = regs->cp0_epc;
834 opcode = 0;
835 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100837 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000839
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100840 if (unlikely(get_user(opcode, epc) < 0))
841 status = SIGSEGV;
842
843 if (!cpu_has_llsc && status < 0)
844 status = simulate_llsc(regs, opcode);
845
846 if (status < 0)
847 status = simulate_rdhwr(regs, opcode);
848
849 if (status < 0)
850 status = SIGILL;
851
852 if (unlikely(status > 0)) {
853 regs->cp0_epc = old_epc; /* Undo skip-over. */
854 force_sig(status, current);
855 }
856
857 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900860 if (used_math()) /* Using the FPU again. */
861 own_fpu(1);
862 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 init_fpu();
864 set_used_math();
865 }
866
Atsushi Nemoto53231802007-04-14 02:37:26 +0900867 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900868 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900869 sig = fpu_emulator_cop1Handler(regs,
870 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (sig)
872 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +0100873 else
874 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return;
878
879 case 2:
880 case 3:
881 break;
882 }
883
884 force_sig(SIGILL, current);
885}
886
887asmlinkage void do_mdmx(struct pt_regs *regs)
888{
889 force_sig(SIGILL, current);
890}
891
892asmlinkage void do_watch(struct pt_regs *regs)
893{
894 /*
895 * We use the watch exception where available to detect stack
896 * overflows.
897 */
898 dump_tlb_all();
899 show_regs(regs);
900 panic("Caught WATCH exception - probably caused by stack overflow.");
901}
902
903asmlinkage void do_mcheck(struct pt_regs *regs)
904{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100905 const int field = 2 * sizeof(unsigned long);
906 int multi_match = regs->cp0_status & ST0_TS;
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100909
910 if (multi_match) {
911 printk("Index : %0x\n", read_c0_index());
912 printk("Pagemask: %0x\n", read_c0_pagemask());
913 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
914 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
915 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
916 printk("\n");
917 dump_tlb_all();
918 }
919
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900920 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * Some chips may have other causes of machine check (e.g. SB1
924 * graduation timer)
925 */
926 panic("Caught Machine Check exception - %scaused by multiple "
927 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100928 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000931asmlinkage void do_mt(struct pt_regs *regs)
932{
Ralf Baechle41c594a2006-04-05 09:45:45 +0100933 int subcode;
934
Ralf Baechle41c594a2006-04-05 09:45:45 +0100935 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
936 >> VPECONTROL_EXCPT_SHIFT;
937 switch (subcode) {
938 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100939 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100940 break;
941 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100942 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100943 break;
944 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100945 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100946 break;
947 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100948 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100949 break;
950 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100951 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100952 break;
953 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100954 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100955 break;
956 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100957 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +0100958 subcode);
959 break;
960 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000961 die_if_kernel("MIPS MT Thread exception in kernel", regs);
962
963 force_sig(SIGILL, current);
964}
965
966
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000967asmlinkage void do_dsp(struct pt_regs *regs)
968{
969 if (cpu_has_dsp)
970 panic("Unexpected DSP exception\n");
971
972 force_sig(SIGILL, current);
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975asmlinkage void do_reserved(struct pt_regs *regs)
976{
977 /*
978 * Game over - no way to handle this if it ever occurs. Most probably
979 * caused by a new unknown cpu type or after another deadly
980 * hard/software error.
981 */
982 show_regs(regs);
983 panic("Caught reserved exception %ld - should not happen.",
984 (regs->cp0_cause & 0x7f) >> 2);
985}
986
Ralf Baechle39b8d522008-04-28 17:14:26 +0100987static int __initdata l1parity = 1;
988static int __init nol1parity(char *s)
989{
990 l1parity = 0;
991 return 1;
992}
993__setup("nol1par", nol1parity);
994static int __initdata l2parity = 1;
995static int __init nol2parity(char *s)
996{
997 l2parity = 0;
998 return 1;
999}
1000__setup("nol2par", nol2parity);
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002/*
1003 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1004 * it different ways.
1005 */
1006static inline void parity_protection_init(void)
1007{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001008 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001010 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001011 case CPU_74K:
1012 case CPU_1004K:
1013 {
1014#define ERRCTL_PE 0x80000000
1015#define ERRCTL_L2P 0x00800000
1016 unsigned long errctl;
1017 unsigned int l1parity_present, l2parity_present;
1018
1019 errctl = read_c0_ecc();
1020 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1021
1022 /* probe L1 parity support */
1023 write_c0_ecc(errctl | ERRCTL_PE);
1024 back_to_back_c0_hazard();
1025 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1026
1027 /* probe L2 parity support */
1028 write_c0_ecc(errctl|ERRCTL_L2P);
1029 back_to_back_c0_hazard();
1030 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1031
1032 if (l1parity_present && l2parity_present) {
1033 if (l1parity)
1034 errctl |= ERRCTL_PE;
1035 if (l1parity ^ l2parity)
1036 errctl |= ERRCTL_L2P;
1037 } else if (l1parity_present) {
1038 if (l1parity)
1039 errctl |= ERRCTL_PE;
1040 } else if (l2parity_present) {
1041 if (l2parity)
1042 errctl |= ERRCTL_L2P;
1043 } else {
1044 /* No parity available */
1045 }
1046
1047 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1048
1049 write_c0_ecc(errctl);
1050 back_to_back_c0_hazard();
1051 errctl = read_c0_ecc();
1052 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1053
1054 if (l1parity_present)
1055 printk(KERN_INFO "Cache parity protection %sabled\n",
1056 (errctl & ERRCTL_PE) ? "en" : "dis");
1057
1058 if (l2parity_present) {
1059 if (l1parity_present && l1parity)
1060 errctl ^= ERRCTL_L2P;
1061 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1062 (errctl & ERRCTL_L2P) ? "en" : "dis");
1063 }
1064 }
1065 break;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001068 write_c0_ecc(0x80000000);
1069 back_to_back_c0_hazard();
1070 /* Set the PE bit (bit 31) in the c0_errctl register. */
1071 printk(KERN_INFO "Cache parity protection %sabled\n",
1072 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074 case CPU_20KC:
1075 case CPU_25KF:
1076 /* Clear the DE bit (bit 16) in the c0_status register. */
1077 printk(KERN_INFO "Enable cache parity protection for "
1078 "MIPS 20KC/25KF CPUs.\n");
1079 clear_c0_status(ST0_DE);
1080 break;
1081 default:
1082 break;
1083 }
1084}
1085
1086asmlinkage void cache_parity_error(void)
1087{
1088 const int field = 2 * sizeof(unsigned long);
1089 unsigned int reg_val;
1090
1091 /* For the moment, report the problem and hang. */
1092 printk("Cache error exception:\n");
1093 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1094 reg_val = read_c0_cacheerr();
1095 printk("c0_cacheerr == %08x\n", reg_val);
1096
1097 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1098 reg_val & (1<<30) ? "secondary" : "primary",
1099 reg_val & (1<<31) ? "data" : "insn");
1100 printk("Error bits: %s%s%s%s%s%s%s\n",
1101 reg_val & (1<<29) ? "ED " : "",
1102 reg_val & (1<<28) ? "ET " : "",
1103 reg_val & (1<<26) ? "EE " : "",
1104 reg_val & (1<<25) ? "EB " : "",
1105 reg_val & (1<<24) ? "EI " : "",
1106 reg_val & (1<<23) ? "E1 " : "",
1107 reg_val & (1<<22) ? "E0 " : "");
1108 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1109
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001110#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (reg_val & (1<<22))
1112 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1113
1114 if (reg_val & (1<<23))
1115 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1116#endif
1117
1118 panic("Can't handle the cache error!");
1119}
1120
1121/*
1122 * SDBBP EJTAG debug exception handler.
1123 * We skip the instruction and return to the next instruction.
1124 */
1125void ejtag_exception_handler(struct pt_regs *regs)
1126{
1127 const int field = 2 * sizeof(unsigned long);
1128 unsigned long depc, old_epc;
1129 unsigned int debug;
1130
Chris Dearman70ae6122006-06-30 12:32:37 +01001131 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 depc = read_c0_depc();
1133 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001134 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (debug & 0x80000000) {
1136 /*
1137 * In branch delay slot.
1138 * We cheat a little bit here and use EPC to calculate the
1139 * debug return address (DEPC). EPC is restored after the
1140 * calculation.
1141 */
1142 old_epc = regs->cp0_epc;
1143 regs->cp0_epc = depc;
1144 __compute_return_epc(regs);
1145 depc = regs->cp0_epc;
1146 regs->cp0_epc = old_epc;
1147 } else
1148 depc += 4;
1149 write_c0_depc(depc);
1150
1151#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001152 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 write_c0_debug(debug | 0x100);
1154#endif
1155}
1156
1157/*
1158 * NMI exception handler.
1159 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001160NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001162 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 printk("NMI taken!!!!\n");
1164 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
Ralf Baechlee01402b2005-07-14 15:57:16 +00001167#define VECTORSPACING 0x100 /* for EI/VI mode */
1168
1169unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001171unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173/*
1174 * As a side effect of the way this is implemented we're limited
1175 * to interrupt handlers in the address range from
1176 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1177 */
1178void *set_except_vector(int n, void *addr)
1179{
1180 unsigned long handler = (unsigned long) addr;
1181 unsigned long old_handler = exception_handlers[n];
1182
1183 exception_handlers[n] = handler;
1184 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001185 *(u32 *)(ebase + 0x200) = 0x08000000 |
1186 (0x03ffffff & (handler >> 2));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001187 flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 return (void *)old_handler;
1190}
1191
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001192static asmlinkage void do_default_vi(void)
1193{
1194 show_regs(get_irq_regs());
1195 panic("Caught unexpected vectored interrupt.");
1196}
1197
Ralf Baechleef300e42007-05-06 18:31:18 +01001198static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001199{
1200 unsigned long handler;
1201 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001202 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001203 u32 *w;
1204 unsigned char *b;
1205
1206 if (!cpu_has_veic && !cpu_has_vint)
1207 BUG();
1208
1209 if (addr == NULL) {
1210 handler = (unsigned long) do_default_vi;
1211 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001212 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001213 handler = (unsigned long) addr;
1214 vi_handlers[n] = (unsigned long) addr;
1215
1216 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1217
Ralf Baechlef6771db2007-11-08 18:02:29 +00001218 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001219 panic("Shadow register set %d not supported", srs);
1220
1221 if (cpu_has_veic) {
1222 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001223 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001224 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001225 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001226 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001227 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001228 }
1229
1230 if (srs == 0) {
1231 /*
1232 * If no shadow set is selected then use the default handler
1233 * that does normal register saving and a standard interrupt exit
1234 */
1235
1236 extern char except_vec_vi, except_vec_vi_lui;
1237 extern char except_vec_vi_ori, except_vec_vi_end;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001238#ifdef CONFIG_MIPS_MT_SMTC
1239 /*
1240 * We need to provide the SMTC vectored interrupt handler
1241 * not only with the address of the handler, but with the
1242 * Status.IM bit to be masked before going there.
1243 */
1244 extern char except_vec_vi_mori;
1245 const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
1246#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001247 const int handler_len = &except_vec_vi_end - &except_vec_vi;
1248 const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
1249 const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
1250
1251 if (handler_len > VECTORSPACING) {
1252 /*
1253 * Sigh... panicing won't help as the console
1254 * is probably not configured :(
1255 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001256 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001257 }
1258
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001259 memcpy(b, &except_vec_vi, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001260#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001261 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1262
Ralf Baechle41c594a2006-04-05 09:45:45 +01001263 w = (u32 *)(b + mori_offset);
1264 *w = (*w & 0xffff0000) | (0x100 << n);
1265#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001266 w = (u32 *)(b + lui_offset);
1267 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1268 w = (u32 *)(b + ori_offset);
1269 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1270 flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
1271 }
1272 else {
1273 /*
1274 * In other cases jump directly to the interrupt handler
1275 *
1276 * It is the handlers responsibility to save registers if required
1277 * (eg hi/lo) and return from the exception using "eret"
1278 */
1279 w = (u32 *)b;
1280 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1281 *w = 0;
1282 flush_icache_range((unsigned long)b, (unsigned long)(b+8));
1283 }
1284
1285 return (void *)old_handler;
1286}
1287
Ralf Baechleef300e42007-05-06 18:31:18 +01001288void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001289{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001290 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001291}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*
1294 * This is used by native signal handling
1295 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001296asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1297asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001299extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1300extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001302extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1303extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Ralf Baechle41c594a2006-04-05 09:45:45 +01001305#ifdef CONFIG_SMP
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001306static int smp_save_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001307{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001308 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001309 ? _save_fp_context(sc)
1310 : fpu_emulator_save_context(sc);
1311}
1312
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001313static int smp_restore_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001314{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001315 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001316 ? _restore_fp_context(sc)
1317 : fpu_emulator_restore_context(sc);
1318}
1319#endif
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321static inline void signal_init(void)
1322{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001323#ifdef CONFIG_SMP
1324 /* For now just do the cpu_has_fpu check when the functions are invoked */
1325 save_fp_context = smp_save_fp_context;
1326 restore_fp_context = smp_restore_fp_context;
1327#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (cpu_has_fpu) {
1329 save_fp_context = _save_fp_context;
1330 restore_fp_context = _restore_fp_context;
1331 } else {
1332 save_fp_context = fpu_emulator_save_context;
1333 restore_fp_context = fpu_emulator_restore_context;
1334 }
Ralf Baechle41c594a2006-04-05 09:45:45 +01001335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338#ifdef CONFIG_MIPS32_COMPAT
1339
1340/*
1341 * This is used by 32-bit signal stuff on the 64-bit kernel
1342 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001343asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1344asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001346extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1347extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001349extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1350extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352static inline void signal32_init(void)
1353{
1354 if (cpu_has_fpu) {
1355 save_fp_context32 = _save_fp_context32;
1356 restore_fp_context32 = _restore_fp_context32;
1357 } else {
1358 save_fp_context32 = fpu_emulator_save_context32;
1359 restore_fp_context32 = fpu_emulator_restore_context32;
1360 }
1361}
1362#endif
1363
1364extern void cpu_cache_init(void);
1365extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001366extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Ralf Baechle42f77542007-10-18 17:48:11 +01001368/*
1369 * Timer interrupt
1370 */
1371int cp0_compare_irq;
1372
1373/*
1374 * Performance counter IRQ or -1 if shared with timer
1375 */
1376int cp0_perfcount_irq;
1377EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1378
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001379static int __cpuinitdata noulri;
1380
1381static int __init ulri_disable(char *s)
1382{
1383 pr_info("Disabling ulri\n");
1384 noulri = 1;
1385
1386 return 1;
1387}
1388__setup("noulri", ulri_disable);
1389
Ralf Baechle234fcd12008-03-08 09:56:28 +00001390void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 unsigned int cpu = smp_processor_id();
1393 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001394#ifdef CONFIG_MIPS_MT_SMTC
1395 int secondaryTC = 0;
1396 int bootTC = (cpu == 0);
1397
1398 /*
1399 * Only do per_cpu_trap_init() for first TC of Each VPE.
1400 * Note that this hack assumes that the SMTC init code
1401 * assigns TCs consecutively and in ascending order.
1402 */
1403
1404 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1405 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1406 secondaryTC = 1;
1407#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
1410 * Disable coprocessors and select 32-bit or 64-bit addressing
1411 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1412 * flag that some firmware may have left set and the TS bit (for
1413 * IP27). Set XX for ISA IV code to work.
1414 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001415#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1417#endif
1418 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1419 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001420 if (cpu_has_dsp)
1421 status_set |= ST0_MX;
1422
Ralf Baechleb38c7392006-02-07 01:20:43 +00001423 change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 status_set);
1425
Ralf Baechlea3692022007-07-10 17:33:02 +01001426 if (cpu_has_mips_r2) {
1427 unsigned int enable = 0x0000000f;
1428
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001429 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001430 enable |= (1 << 29);
1431
1432 write_c0_hwrena(enable);
1433 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001434
Ralf Baechle41c594a2006-04-05 09:45:45 +01001435#ifdef CONFIG_MIPS_MT_SMTC
1436 if (!secondaryTC) {
1437#endif /* CONFIG_MIPS_MT_SMTC */
1438
Ralf Baechlee01402b2005-07-14 15:57:16 +00001439 if (cpu_has_veic || cpu_has_vint) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001440 write_c0_ebase(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001441 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001442 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001443 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001444 if (cpu_has_divec) {
1445 if (cpu_has_mipsmt) {
1446 unsigned int vpflags = dvpe();
1447 set_c0_cause(CAUSEF_IV);
1448 evpe(vpflags);
1449 } else
1450 set_c0_cause(CAUSEF_IV);
1451 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001452
1453 /*
1454 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1455 *
1456 * o read IntCtl.IPTI to determine the timer interrupt
1457 * o read IntCtl.IPPCI to determine the performance counter interrupt
1458 */
1459 if (cpu_has_mips_r2) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001460 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1461 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001462 if (cp0_perfcount_irq == cp0_compare_irq)
1463 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001464 } else {
1465 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001466 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001467 }
1468
Ralf Baechle41c594a2006-04-05 09:45:45 +01001469#ifdef CONFIG_MIPS_MT_SMTC
1470 }
1471#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1474 TLBMISS_HANDLER_SETUP();
1475
1476 atomic_inc(&init_mm.mm_count);
1477 current->active_mm = &init_mm;
1478 BUG_ON(current->mm);
1479 enter_lazy_tlb(&init_mm, current);
1480
Ralf Baechle41c594a2006-04-05 09:45:45 +01001481#ifdef CONFIG_MIPS_MT_SMTC
1482 if (bootTC) {
1483#endif /* CONFIG_MIPS_MT_SMTC */
1484 cpu_cache_init();
1485 tlb_init();
1486#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001487 } else if (!secondaryTC) {
1488 /*
1489 * First TC in non-boot VPE must do subset of tlb_init()
1490 * for MMU countrol registers.
1491 */
1492 write_c0_pagemask(PM_DEFAULT_MASK);
1493 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001494 }
1495#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Ralf Baechlee01402b2005-07-14 15:57:16 +00001498/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001499void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001500{
1501 memcpy((void *)(ebase + offset), addr, size);
1502 flush_icache_range(ebase + offset, ebase + offset + size);
1503}
1504
Ralf Baechle234fcd12008-03-08 09:56:28 +00001505static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001506 "Trying to set NULL cache error exception handler";
1507
Ralf Baechlee01402b2005-07-14 15:57:16 +00001508/* Install uncached CPU exception handler */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001509void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1510 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001511{
1512#ifdef CONFIG_32BIT
1513 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1514#endif
1515#ifdef CONFIG_64BIT
1516 unsigned long uncached_ebase = TO_UNCAC(ebase);
1517#endif
1518
Ralf Baechle641e97f2007-10-11 23:46:05 +01001519 if (!addr)
1520 panic(panic_null_cerr);
1521
Ralf Baechlee01402b2005-07-14 15:57:16 +00001522 memcpy((void *)(uncached_ebase + offset), addr, size);
1523}
1524
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001525static int __initdata rdhwr_noopt;
1526static int __init set_rdhwr_noopt(char *str)
1527{
1528 rdhwr_noopt = 1;
1529 return 1;
1530}
1531
1532__setup("rdhwr_noopt", set_rdhwr_noopt);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534void __init trap_init(void)
1535{
1536 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 extern char except_vec4;
1538 unsigned long i;
1539
Ralf Baechlee01402b2005-07-14 15:57:16 +00001540 if (cpu_has_veic || cpu_has_vint)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001541 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001542 else
1543 ebase = CAC_BASE;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 per_cpu_trap_init();
1546
1547 /*
1548 * Copy the generic exception handlers to their final destination.
1549 * This will be overriden later as suitable for a particular
1550 * configuration.
1551 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001552 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /*
1555 * Setup default vectors
1556 */
1557 for (i = 0; i <= 31; i++)
1558 set_except_vector(i, handle_reserved);
1559
1560 /*
1561 * Copy the EJTAG debug exception vector handler code to it's final
1562 * destination.
1563 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001564 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001565 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /*
1568 * Only some CPUs have the watch exceptions.
1569 */
1570 if (cpu_has_watch)
1571 set_except_vector(23, handle_watch);
1572
1573 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001574 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001576 if (cpu_has_veic || cpu_has_vint) {
1577 int nvec = cpu_has_veic ? 64 : 8;
1578 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001579 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001580 }
1581 else if (cpu_has_divec)
1582 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 /*
1585 * Some CPUs can enable/disable for cache parity detection, but does
1586 * it different ways.
1587 */
1588 parity_protection_init();
1589
1590 /*
1591 * The Data Bus Errors / Instruction Bus Errors are signaled
1592 * by external hardware. Therefore these two exceptions
1593 * may have board specific handlers.
1594 */
1595 if (board_be_init)
1596 board_be_init();
1597
Ralf Baechlee4ac58a2006-04-03 17:56:36 +01001598 set_except_vector(0, handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 set_except_vector(1, handle_tlbm);
1600 set_except_vector(2, handle_tlbl);
1601 set_except_vector(3, handle_tlbs);
1602
1603 set_except_vector(4, handle_adel);
1604 set_except_vector(5, handle_ades);
1605
1606 set_except_vector(6, handle_ibe);
1607 set_except_vector(7, handle_dbe);
1608
1609 set_except_vector(8, handle_sys);
1610 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001611 set_except_vector(10, rdhwr_noopt ? handle_ri :
1612 (cpu_has_vtag_icache ?
1613 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 set_except_vector(11, handle_cpu);
1615 set_except_vector(12, handle_ov);
1616 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Ralf Baechle10cc3522007-10-11 23:46:15 +01001618 if (current_cpu_type() == CPU_R6000 ||
1619 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /*
1621 * The R6000 is the only R-series CPU that features a machine
1622 * check exception (similar to the R4000 cache error) and
1623 * unaligned ldc1/sdc1 exception. The handlers have not been
1624 * written yet. Well, anyway there is no R6000 machine on the
1625 * current list of targets for Linux/MIPS.
1626 * (Duh, crap, there is someone with a triple R6k machine)
1627 */
1628 //set_except_vector(14, handle_mc);
1629 //set_except_vector(15, handle_ndc);
1630 }
1631
Ralf Baechlee01402b2005-07-14 15:57:16 +00001632
1633 if (board_nmi_handler_setup)
1634 board_nmi_handler_setup();
1635
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001636 if (cpu_has_fpu && !cpu_has_nofpuex)
1637 set_except_vector(15, handle_fpe);
1638
1639 set_except_vector(22, handle_mdmx);
1640
1641 if (cpu_has_mcheck)
1642 set_except_vector(24, handle_mcheck);
1643
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001644 if (cpu_has_mipsmt)
1645 set_except_vector(25, handle_mt);
1646
Chris Dearmanacaec422007-05-24 22:30:18 +01001647 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001648
1649 if (cpu_has_vce)
1650 /* Special exception: R4[04]00 uses also the divec space. */
1651 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1652 else if (cpu_has_4kex)
1653 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1654 else
1655 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 signal_init();
1658#ifdef CONFIG_MIPS32_COMPAT
1659 signal32_init();
1660#endif
1661
Ralf Baechlee01402b2005-07-14 15:57:16 +00001662 flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001663 flush_tlb_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}