blob: cb8b0e2c79547ba1c67a577cc852daf51f558dab [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 Baechlee50c0a82005-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 Baechlee50c0a82005-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
Marc St-Jean9267a302007-06-14 15:55:31 -060074void (*board_watchpoint_handler)(struct pt_regs *regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075void (*board_be_init)(void);
76int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000077void (*board_nmi_handler_setup)(void);
78void (*board_ejtag_handler_setup)(void);
79void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020082static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090083{
Ralf Baechle39b8d522008-04-28 17:14:26 +010084 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090085 unsigned long addr;
86
87 printk("Call Trace:");
88#ifdef CONFIG_KALLSYMS
89 printk("\n");
90#endif
Ralf Baechle39b8d522008-04-28 17:14:26 +010091#define IS_KVA01(a) ((((unsigned int)a) & 0xc0000000) == 0x80000000)
92 if (IS_KVA01(sp)) {
93 while (!kstack_end(sp)) {
94 addr = *sp++;
95 if (__kernel_text_address(addr))
96 print_ip_sym(addr);
97 }
98 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +090099 }
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900100}
101
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900102#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900103int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900104static int __init set_raw_show_trace(char *str)
105{
106 raw_show_trace = 1;
107 return 1;
108}
109__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900110#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200111
Ralf Baechleeae23f22007-10-14 23:27:21 +0100112static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900113{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200114 unsigned long sp = regs->regs[29];
115 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900116 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900117
118 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200119 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900120 return;
121 }
122 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200123 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200124 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900125 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200126 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900127 printk("\n");
128}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * This routine abuses get_user()/put_user() to reference pointers
132 * with at least a bit of error checking ...
133 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100134static void show_stacktrace(struct task_struct *task,
135 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 const int field = 2 * sizeof(unsigned long);
138 long stackdata;
139 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900140 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 printk("Stack :");
143 i = 0;
144 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
145 if (i && ((i % (64 / field)) == 0))
146 printk("\n ");
147 if (i > 39) {
148 printk(" ...");
149 break;
150 }
151
152 if (__get_user(stackdata, sp++)) {
153 printk(" (Bad stack address)");
154 break;
155 }
156
157 printk(" %0*lx", field, stackdata);
158 i++;
159 }
160 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200161 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900162}
163
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900164void show_stack(struct task_struct *task, unsigned long *sp)
165{
166 struct pt_regs regs;
167 if (sp) {
168 regs.regs[29] = (unsigned long)sp;
169 regs.regs[31] = 0;
170 regs.cp0_epc = 0;
171 } else {
172 if (task && task != current) {
173 regs.regs[29] = task->thread.reg29;
174 regs.regs[31] = 0;
175 regs.cp0_epc = task->thread.reg31;
176 } else {
177 prepare_frametrace(&regs);
178 }
179 }
180 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 * The architecture-independent dump_stack generator
185 */
186void dump_stack(void)
187{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200188 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200190 prepare_frametrace(&regs);
191 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194EXPORT_SYMBOL(dump_stack);
195
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900196static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100199 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 printk("\nCode:");
202
Ralf Baechle39b8d522008-04-28 17:14:26 +0100203 if ((unsigned long)pc & 1)
204 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for(i = -3 ; i < 6 ; i++) {
206 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100207 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 printk(" (Bad address in epc)\n");
209 break;
210 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100211 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213}
214
Ralf Baechleeae23f22007-10-14 23:27:21 +0100215static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 const int field = 2 * sizeof(unsigned long);
218 unsigned int cause = regs->cp0_cause;
219 int i;
220
221 printk("Cpu %d\n", smp_processor_id());
222
223 /*
224 * Saved main processor registers
225 */
226 for (i = 0; i < 32; ) {
227 if ((i % 4) == 0)
228 printk("$%2d :", i);
229 if (i == 0)
230 printk(" %0*lx", field, 0UL);
231 else if (i == 26 || i == 27)
232 printk(" %*s", field, "");
233 else
234 printk(" %0*lx", field, regs->regs[i]);
235
236 i++;
237 if ((i % 4) == 0)
238 printk("\n");
239 }
240
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100241#ifdef CONFIG_CPU_HAS_SMARTMIPS
242 printk("Acx : %0*lx\n", field, regs->acx);
243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 printk("Hi : %0*lx\n", field, regs->hi);
245 printk("Lo : %0*lx\n", field, regs->lo);
246
247 /*
248 * Saved cp0 registers
249 */
250 printk("epc : %0*lx ", field, regs->cp0_epc);
251 print_symbol("%s ", regs->cp0_epc);
252 printk(" %s\n", print_tainted());
253 printk("ra : %0*lx ", field, regs->regs[31]);
254 print_symbol("%s\n", regs->regs[31]);
255
256 printk("Status: %08x ", (uint32_t) regs->cp0_status);
257
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000258 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
259 if (regs->cp0_status & ST0_KUO)
260 printk("KUo ");
261 if (regs->cp0_status & ST0_IEO)
262 printk("IEo ");
263 if (regs->cp0_status & ST0_KUP)
264 printk("KUp ");
265 if (regs->cp0_status & ST0_IEP)
266 printk("IEp ");
267 if (regs->cp0_status & ST0_KUC)
268 printk("KUc ");
269 if (regs->cp0_status & ST0_IEC)
270 printk("IEc ");
271 } else {
272 if (regs->cp0_status & ST0_KX)
273 printk("KX ");
274 if (regs->cp0_status & ST0_SX)
275 printk("SX ");
276 if (regs->cp0_status & ST0_UX)
277 printk("UX ");
278 switch (regs->cp0_status & ST0_KSU) {
279 case KSU_USER:
280 printk("USER ");
281 break;
282 case KSU_SUPERVISOR:
283 printk("SUPERVISOR ");
284 break;
285 case KSU_KERNEL:
286 printk("KERNEL ");
287 break;
288 default:
289 printk("BAD_MODE ");
290 break;
291 }
292 if (regs->cp0_status & ST0_ERL)
293 printk("ERL ");
294 if (regs->cp0_status & ST0_EXL)
295 printk("EXL ");
296 if (regs->cp0_status & ST0_IE)
297 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 printk("\n");
300
301 printk("Cause : %08x\n", cause);
302
303 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
304 if (1 <= cause && cause <= 5)
305 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
306
Ralf Baechle9966db252007-10-11 23:46:17 +0100307 printk("PrId : %08x (%s)\n", read_c0_prid(),
308 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Ralf Baechleeae23f22007-10-14 23:27:21 +0100311/*
312 * FIXME: really the generic show_regs should take a const pointer argument.
313 */
314void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100316 __show_regs((struct pt_regs *)regs);
317}
318
319void show_registers(const struct pt_regs *regs)
320{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100321 const int field = 2 * sizeof(unsigned long);
322
Ralf Baechleeae23f22007-10-14 23:27:21 +0100323 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100325 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
326 current->comm, current->pid, current_thread_info(), current,
327 field, current_thread_info()->tp_value);
328 if (cpu_has_userlocal) {
329 unsigned long tls;
330
331 tls = read_c0_userlocal();
332 if (tls != current_thread_info()->tp_value)
333 printk("*HwTLS: %0*lx\n", field, tls);
334 }
335
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900336 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900337 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 printk("\n");
339}
340
341static DEFINE_SPINLOCK(die_lock);
342
Ralf Baechleeae23f22007-10-14 23:27:21 +0100343void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100346#ifdef CONFIG_MIPS_MT_SMTC
347 unsigned long dvpret = dvpe();
348#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 console_verbose();
351 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100352 bust_spinlocks(1);
353#ifdef CONFIG_MIPS_MT_SMTC
354 mips_mt_regdump(dvpret);
355#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100356 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700358 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200360
361 if (in_interrupt())
362 panic("Fatal exception in interrupt");
363
364 if (panic_on_oops) {
365 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
366 ssleep(5);
367 panic("Fatal exception");
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 do_exit(SIGSEGV);
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373extern const struct exception_table_entry __start___dbe_table[];
374extern const struct exception_table_entry __stop___dbe_table[];
375
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000376__asm__(
377" .section __dbe_table, \"a\"\n"
378" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/* Given an address, look for it in the exception tables. */
381static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
382{
383 const struct exception_table_entry *e;
384
385 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
386 if (!e)
387 e = search_module_dbetables(addr);
388 return e;
389}
390
391asmlinkage void do_be(struct pt_regs *regs)
392{
393 const int field = 2 * sizeof(unsigned long);
394 const struct exception_table_entry *fixup = NULL;
395 int data = regs->cp0_cause & 4;
396 int action = MIPS_BE_FATAL;
397
398 /* XXX For now. Fixme, this searches the wrong table ... */
399 if (data && !user_mode(regs))
400 fixup = search_dbe_tables(exception_epc(regs));
401
402 if (fixup)
403 action = MIPS_BE_FIXUP;
404
405 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900406 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 switch (action) {
409 case MIPS_BE_DISCARD:
410 return;
411 case MIPS_BE_FIXUP:
412 if (fixup) {
413 regs->cp0_epc = fixup->nextinsn;
414 return;
415 }
416 break;
417 default:
418 break;
419 }
420
421 /*
422 * Assume it would be too dangerous to continue ...
423 */
424 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
425 data ? "Data" : "Instruction",
426 field, regs->cp0_epc, field, regs->regs[31]);
427 die_if_kernel("Oops", regs);
428 force_sig(SIGBUS, current);
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100432 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 */
434
435#define OPCODE 0xfc000000
436#define BASE 0x03e00000
437#define RT 0x001f0000
438#define OFFSET 0x0000ffff
439#define LL 0xc0000000
440#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100441#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000442#define SPEC3 0x7c000000
443#define RD 0x0000f800
444#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100445#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000446#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448/*
449 * The ll_bit is cleared by r*_switch.S
450 */
451
452unsigned long ll_bit;
453
454static struct task_struct *ll_task = NULL;
455
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100456static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000458 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * analyse the ll instruction that just caused a ri exception
463 * and put the referenced address to addr.
464 */
465
466 /* sign extend offset */
467 offset = opcode & OFFSET;
468 offset <<= 16;
469 offset >>= 16;
470
Ralf Baechlefe00f942005-03-01 19:22:29 +0000471 vaddr = (unsigned long __user *)
472 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100474 if ((unsigned long)vaddr & 3)
475 return SIGBUS;
476 if (get_user(value, vaddr))
477 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 preempt_disable();
480
481 if (ll_task == NULL || ll_task == current) {
482 ll_bit = 1;
483 } else {
484 ll_bit = 0;
485 }
486 ll_task = current;
487
488 preempt_enable();
489
490 regs->regs[(opcode & RT) >> 16] = value;
491
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100495static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000497 unsigned long __user *vaddr;
498 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /*
502 * analyse the sc instruction that just caused a ri exception
503 * and put the referenced address to addr.
504 */
505
506 /* sign extend offset */
507 offset = opcode & OFFSET;
508 offset <<= 16;
509 offset >>= 16;
510
Ralf Baechlefe00f942005-03-01 19:22:29 +0000511 vaddr = (unsigned long __user *)
512 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 reg = (opcode & RT) >> 16;
514
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100515 if ((unsigned long)vaddr & 3)
516 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 preempt_disable();
519
520 if (ll_bit == 0 || ll_task != current) {
521 regs->regs[reg] = 0;
522 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
526 preempt_enable();
527
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100528 if (put_user(regs->regs[reg], vaddr))
529 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 regs->regs[reg] = 1;
532
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
538 * opcodes are supposed to result in coprocessor unusable exceptions if
539 * executed on ll/sc-less processors. That's the theory. In practice a
540 * few processors such as NEC's VR4100 throw reserved instruction exceptions
541 * instead, so we're doing the emulation thing in both exception handlers.
542 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100543static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100545 if ((opcode & OPCODE) == LL)
546 return simulate_ll(regs, opcode);
547 if ((opcode & OPCODE) == SC)
548 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100550 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Ralf Baechle3c370262005-04-13 17:43:59 +0000553/*
554 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100555 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000556 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100557static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000558{
Al Virodc8f6022006-01-12 01:06:07 -0800559 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000560
561 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
562 int rd = (opcode & RD) >> 11;
563 int rt = (opcode & RT) >> 16;
564 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100565 case 0: /* CPU number */
566 regs->regs[rt] = smp_processor_id();
567 return 0;
568 case 1: /* SYNCI length */
569 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
570 current_cpu_data.icache.linesz);
571 return 0;
572 case 2: /* Read count register */
573 regs->regs[rt] = read_c0_count();
574 return 0;
575 case 3: /* Count register resolution */
576 switch (current_cpu_data.cputype) {
577 case CPU_20KC:
578 case CPU_25KF:
579 regs->regs[rt] = 1;
580 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000581 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100582 regs->regs[rt] = 2;
583 }
584 return 0;
585 case 29:
586 regs->regs[rt] = ti->tp_value;
587 return 0;
588 default:
589 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000590 }
591 }
592
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500593 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100594 return -1;
595}
Ralf Baechlee5679882006-11-30 01:14:47 +0000596
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100597static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
598{
599 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
600 return 0;
601
602 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000603}
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605asmlinkage void do_ov(struct pt_regs *regs)
606{
607 siginfo_t info;
608
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000609 die_if_kernel("Integer overflow", regs);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 info.si_code = FPE_INTOVF;
612 info.si_signo = SIGFPE;
613 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000614 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 force_sig_info(SIGFPE, &info, current);
616}
617
618/*
619 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
620 */
621asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
622{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100623 siginfo_t info;
624
Chris Dearman57725f92006-06-30 23:35:28 +0100625 die_if_kernel("FP exception in kernel code", regs);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (fcr31 & FPU_CSR_UNI_X) {
628 int sig;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000631 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * software emulator on-board, let's use it...
633 *
634 * Force FPU to dump state into task/thread context. We're
635 * moving a lot of data here for what is probably a single
636 * instruction, but the alternative is to pre-decode the FP
637 * register operands before invoking the emulator, which seems
638 * a bit extreme for what should be an infrequent event.
639 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000640 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900641 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100644 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /*
647 * We can't allow the emulated instruction to leave any of
648 * the cause bit set in $fcr31.
649 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900650 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900653 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /* If something went wrong, signal */
656 if (sig)
657 force_sig(sig, current);
658
659 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100660 } else if (fcr31 & FPU_CSR_INV_X)
661 info.si_code = FPE_FLTINV;
662 else if (fcr31 & FPU_CSR_DIV_X)
663 info.si_code = FPE_FLTDIV;
664 else if (fcr31 & FPU_CSR_OVF_X)
665 info.si_code = FPE_FLTOVF;
666 else if (fcr31 & FPU_CSR_UDF_X)
667 info.si_code = FPE_FLTUND;
668 else if (fcr31 & FPU_CSR_INE_X)
669 info.si_code = FPE_FLTRES;
670 else
671 info.si_code = __SI_FAULT;
672 info.si_signo = SIGFPE;
673 info.si_errno = 0;
674 info.si_addr = (void __user *) regs->cp0_epc;
675 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Ralf Baechledf270052008-04-20 16:28:54 +0100678static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
679 const char *str)
680{
681 siginfo_t info;
682 char b[40];
683
684 /*
685 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
686 * insns, even for trap and break codes that indicate arithmetic
687 * failures. Weird ...
688 * But should we continue the brokenness??? --macro
689 */
690 switch (code) {
691 case BRK_OVERFLOW:
692 case BRK_DIVZERO:
693 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
694 die_if_kernel(b, regs);
695 if (code == BRK_DIVZERO)
696 info.si_code = FPE_INTDIV;
697 else
698 info.si_code = FPE_INTOVF;
699 info.si_signo = SIGFPE;
700 info.si_errno = 0;
701 info.si_addr = (void __user *) regs->cp0_epc;
702 force_sig_info(SIGFPE, &info, current);
703 break;
704 case BRK_BUG:
705 die_if_kernel("Kernel bug detected", regs);
706 force_sig(SIGTRAP, current);
707 break;
708 default:
709 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
710 die_if_kernel(b, regs);
711 force_sig(SIGTRAP, current);
712 }
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715asmlinkage void do_bp(struct pt_regs *regs)
716{
717 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900719 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000720 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /*
723 * There is the ancient bug in the MIPS assemblers that the break
724 * code starts left to bit 16 instead to bit 6 in the opcode.
725 * Gas is bug-compatible, but not always, grrr...
726 * We handle both cases with a simple heuristics. --macro
727 */
728 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100729 if (bcode >= (1 << 10))
730 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Ralf Baechledf270052008-04-20 16:28:54 +0100732 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900733 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000734
735out_sigsegv:
736 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739asmlinkage void do_tr(struct pt_regs *regs)
740{
741 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900743 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000744 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* Immediate versions don't provide a code. */
747 if (!(opcode & OPCODE))
748 tcode = ((opcode >> 6) & ((1 << 10) - 1));
749
Ralf Baechledf270052008-04-20 16:28:54 +0100750 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900751 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000752
753out_sigsegv:
754 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757asmlinkage void do_ri(struct pt_regs *regs)
758{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100759 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
760 unsigned long old_epc = regs->cp0_epc;
761 unsigned int opcode = 0;
762 int status = -1;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 die_if_kernel("Reserved instruction in kernel code", regs);
765
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100766 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000767 return;
768
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100769 if (unlikely(get_user(opcode, epc) < 0))
770 status = SIGSEGV;
771
772 if (!cpu_has_llsc && status < 0)
773 status = simulate_llsc(regs, opcode);
774
775 if (status < 0)
776 status = simulate_rdhwr(regs, opcode);
777
778 if (status < 0)
779 status = simulate_sync(regs, opcode);
780
781 if (status < 0)
782 status = SIGILL;
783
784 if (unlikely(status > 0)) {
785 regs->cp0_epc = old_epc; /* Undo skip-over. */
786 force_sig(status, current);
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Ralf Baechled223a862007-07-10 17:33:02 +0100790/*
791 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
792 * emulated more than some threshold number of instructions, force migration to
793 * a "CPU" that has FP support.
794 */
795static void mt_ase_fp_affinity(void)
796{
797#ifdef CONFIG_MIPS_MT_FPAFF
798 if (mt_fpemul_threshold > 0 &&
799 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
800 /*
801 * If there's no FPU present, or if the application has already
802 * restricted the allowed set to exclude any CPUs with FPUs,
803 * we'll skip the procedure.
804 */
805 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
806 cpumask_t tmask;
807
808 cpus_and(tmask, current->thread.user_cpus_allowed,
809 mt_fpu_cpumask);
810 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100811 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100812 }
813 }
814#endif /* CONFIG_MIPS_MT_FPAFF */
815}
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817asmlinkage void do_cpu(struct pt_regs *regs)
818{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100819 unsigned int __user *epc;
820 unsigned long old_epc;
821 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100823 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Atsushi Nemoto53231802007-04-14 02:37:26 +0900825 die_if_kernel("do_cpu invoked from kernel context!", regs);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
828
829 switch (cpid) {
830 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100831 epc = (unsigned int __user *)exception_epc(regs);
832 old_epc = regs->cp0_epc;
833 opcode = 0;
834 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100836 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000838
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100839 if (unlikely(get_user(opcode, epc) < 0))
840 status = SIGSEGV;
841
842 if (!cpu_has_llsc && status < 0)
843 status = simulate_llsc(regs, opcode);
844
845 if (status < 0)
846 status = simulate_rdhwr(regs, opcode);
847
848 if (status < 0)
849 status = SIGILL;
850
851 if (unlikely(status > 0)) {
852 regs->cp0_epc = old_epc; /* Undo skip-over. */
853 force_sig(status, current);
854 }
855
856 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900859 if (used_math()) /* Using the FPU again. */
860 own_fpu(1);
861 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 init_fpu();
863 set_used_math();
864 }
865
Atsushi Nemoto53231802007-04-14 02:37:26 +0900866 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900867 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900868 sig = fpu_emulator_cop1Handler(regs,
869 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (sig)
871 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +0100872 else
873 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return;
877
878 case 2:
879 case 3:
880 break;
881 }
882
883 force_sig(SIGILL, current);
884}
885
886asmlinkage void do_mdmx(struct pt_regs *regs)
887{
888 force_sig(SIGILL, current);
889}
890
891asmlinkage void do_watch(struct pt_regs *regs)
892{
Marc St-Jean9267a302007-06-14 15:55:31 -0600893 if (board_watchpoint_handler) {
894 (*board_watchpoint_handler)(regs);
895 return;
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * We use the watch exception where available to detect stack
900 * overflows.
901 */
902 dump_tlb_all();
903 show_regs(regs);
904 panic("Caught WATCH exception - probably caused by stack overflow.");
905}
906
907asmlinkage void do_mcheck(struct pt_regs *regs)
908{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100909 const int field = 2 * sizeof(unsigned long);
910 int multi_match = regs->cp0_status & ST0_TS;
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100913
914 if (multi_match) {
915 printk("Index : %0x\n", read_c0_index());
916 printk("Pagemask: %0x\n", read_c0_pagemask());
917 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
918 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
919 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
920 printk("\n");
921 dump_tlb_all();
922 }
923
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900924 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * Some chips may have other causes of machine check (e.g. SB1
928 * graduation timer)
929 */
930 panic("Caught Machine Check exception - %scaused by multiple "
931 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100932 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000935asmlinkage void do_mt(struct pt_regs *regs)
936{
Ralf Baechle41c594a2006-04-05 09:45:45 +0100937 int subcode;
938
Ralf Baechle41c594a2006-04-05 09:45:45 +0100939 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
940 >> VPECONTROL_EXCPT_SHIFT;
941 switch (subcode) {
942 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100943 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100944 break;
945 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100946 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100947 break;
948 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100949 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100950 break;
951 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100952 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100953 break;
954 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100955 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100956 break;
957 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100958 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100959 break;
960 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100961 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +0100962 subcode);
963 break;
964 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000965 die_if_kernel("MIPS MT Thread exception in kernel", regs);
966
967 force_sig(SIGILL, current);
968}
969
970
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000971asmlinkage void do_dsp(struct pt_regs *regs)
972{
973 if (cpu_has_dsp)
974 panic("Unexpected DSP exception\n");
975
976 force_sig(SIGILL, current);
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979asmlinkage void do_reserved(struct pt_regs *regs)
980{
981 /*
982 * Game over - no way to handle this if it ever occurs. Most probably
983 * caused by a new unknown cpu type or after another deadly
984 * hard/software error.
985 */
986 show_regs(regs);
987 panic("Caught reserved exception %ld - should not happen.",
988 (regs->cp0_cause & 0x7f) >> 2);
989}
990
Ralf Baechle39b8d522008-04-28 17:14:26 +0100991static int __initdata l1parity = 1;
992static int __init nol1parity(char *s)
993{
994 l1parity = 0;
995 return 1;
996}
997__setup("nol1par", nol1parity);
998static int __initdata l2parity = 1;
999static int __init nol2parity(char *s)
1000{
1001 l2parity = 0;
1002 return 1;
1003}
1004__setup("nol2par", nol2parity);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006/*
1007 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1008 * it different ways.
1009 */
1010static inline void parity_protection_init(void)
1011{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001012 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001014 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001015 case CPU_74K:
1016 case CPU_1004K:
1017 {
1018#define ERRCTL_PE 0x80000000
1019#define ERRCTL_L2P 0x00800000
1020 unsigned long errctl;
1021 unsigned int l1parity_present, l2parity_present;
1022
1023 errctl = read_c0_ecc();
1024 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1025
1026 /* probe L1 parity support */
1027 write_c0_ecc(errctl | ERRCTL_PE);
1028 back_to_back_c0_hazard();
1029 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1030
1031 /* probe L2 parity support */
1032 write_c0_ecc(errctl|ERRCTL_L2P);
1033 back_to_back_c0_hazard();
1034 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1035
1036 if (l1parity_present && l2parity_present) {
1037 if (l1parity)
1038 errctl |= ERRCTL_PE;
1039 if (l1parity ^ l2parity)
1040 errctl |= ERRCTL_L2P;
1041 } else if (l1parity_present) {
1042 if (l1parity)
1043 errctl |= ERRCTL_PE;
1044 } else if (l2parity_present) {
1045 if (l2parity)
1046 errctl |= ERRCTL_L2P;
1047 } else {
1048 /* No parity available */
1049 }
1050
1051 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1052
1053 write_c0_ecc(errctl);
1054 back_to_back_c0_hazard();
1055 errctl = read_c0_ecc();
1056 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1057
1058 if (l1parity_present)
1059 printk(KERN_INFO "Cache parity protection %sabled\n",
1060 (errctl & ERRCTL_PE) ? "en" : "dis");
1061
1062 if (l2parity_present) {
1063 if (l1parity_present && l1parity)
1064 errctl ^= ERRCTL_L2P;
1065 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1066 (errctl & ERRCTL_L2P) ? "en" : "dis");
1067 }
1068 }
1069 break;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001072 write_c0_ecc(0x80000000);
1073 back_to_back_c0_hazard();
1074 /* Set the PE bit (bit 31) in the c0_errctl register. */
1075 printk(KERN_INFO "Cache parity protection %sabled\n",
1076 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 break;
1078 case CPU_20KC:
1079 case CPU_25KF:
1080 /* Clear the DE bit (bit 16) in the c0_status register. */
1081 printk(KERN_INFO "Enable cache parity protection for "
1082 "MIPS 20KC/25KF CPUs.\n");
1083 clear_c0_status(ST0_DE);
1084 break;
1085 default:
1086 break;
1087 }
1088}
1089
1090asmlinkage void cache_parity_error(void)
1091{
1092 const int field = 2 * sizeof(unsigned long);
1093 unsigned int reg_val;
1094
1095 /* For the moment, report the problem and hang. */
1096 printk("Cache error exception:\n");
1097 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1098 reg_val = read_c0_cacheerr();
1099 printk("c0_cacheerr == %08x\n", reg_val);
1100
1101 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1102 reg_val & (1<<30) ? "secondary" : "primary",
1103 reg_val & (1<<31) ? "data" : "insn");
1104 printk("Error bits: %s%s%s%s%s%s%s\n",
1105 reg_val & (1<<29) ? "ED " : "",
1106 reg_val & (1<<28) ? "ET " : "",
1107 reg_val & (1<<26) ? "EE " : "",
1108 reg_val & (1<<25) ? "EB " : "",
1109 reg_val & (1<<24) ? "EI " : "",
1110 reg_val & (1<<23) ? "E1 " : "",
1111 reg_val & (1<<22) ? "E0 " : "");
1112 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1113
Ralf Baechleec917c22005-10-07 16:58:15 +01001114#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (reg_val & (1<<22))
1116 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1117
1118 if (reg_val & (1<<23))
1119 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1120#endif
1121
1122 panic("Can't handle the cache error!");
1123}
1124
1125/*
1126 * SDBBP EJTAG debug exception handler.
1127 * We skip the instruction and return to the next instruction.
1128 */
1129void ejtag_exception_handler(struct pt_regs *regs)
1130{
1131 const int field = 2 * sizeof(unsigned long);
1132 unsigned long depc, old_epc;
1133 unsigned int debug;
1134
Chris Dearman70ae6122006-06-30 12:32:37 +01001135 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 depc = read_c0_depc();
1137 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001138 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (debug & 0x80000000) {
1140 /*
1141 * In branch delay slot.
1142 * We cheat a little bit here and use EPC to calculate the
1143 * debug return address (DEPC). EPC is restored after the
1144 * calculation.
1145 */
1146 old_epc = regs->cp0_epc;
1147 regs->cp0_epc = depc;
1148 __compute_return_epc(regs);
1149 depc = regs->cp0_epc;
1150 regs->cp0_epc = old_epc;
1151 } else
1152 depc += 4;
1153 write_c0_depc(depc);
1154
1155#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001156 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 write_c0_debug(debug | 0x100);
1158#endif
1159}
1160
1161/*
1162 * NMI exception handler.
1163 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001164NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001166 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 printk("NMI taken!!!!\n");
1168 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Ralf Baechlee01402b2005-07-14 15:57:16 +00001171#define VECTORSPACING 0x100 /* for EI/VI mode */
1172
1173unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001175unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177/*
1178 * As a side effect of the way this is implemented we're limited
1179 * to interrupt handlers in the address range from
1180 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1181 */
1182void *set_except_vector(int n, void *addr)
1183{
1184 unsigned long handler = (unsigned long) addr;
1185 unsigned long old_handler = exception_handlers[n];
1186
1187 exception_handlers[n] = handler;
1188 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001189 *(u32 *)(ebase + 0x200) = 0x08000000 |
1190 (0x03ffffff & (handler >> 2));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001191 flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 return (void *)old_handler;
1194}
1195
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001196static asmlinkage void do_default_vi(void)
1197{
1198 show_regs(get_irq_regs());
1199 panic("Caught unexpected vectored interrupt.");
1200}
1201
Ralf Baechleef300e42007-05-06 18:31:18 +01001202static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001203{
1204 unsigned long handler;
1205 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001206 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001207 u32 *w;
1208 unsigned char *b;
1209
1210 if (!cpu_has_veic && !cpu_has_vint)
1211 BUG();
1212
1213 if (addr == NULL) {
1214 handler = (unsigned long) do_default_vi;
1215 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001216 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001217 handler = (unsigned long) addr;
1218 vi_handlers[n] = (unsigned long) addr;
1219
1220 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1221
Ralf Baechlef6771db2007-11-08 18:02:29 +00001222 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001223 panic("Shadow register set %d not supported", srs);
1224
1225 if (cpu_has_veic) {
1226 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001227 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001228 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001229 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001230 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001231 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001232 }
1233
1234 if (srs == 0) {
1235 /*
1236 * If no shadow set is selected then use the default handler
1237 * that does normal register saving and a standard interrupt exit
1238 */
1239
1240 extern char except_vec_vi, except_vec_vi_lui;
1241 extern char except_vec_vi_ori, except_vec_vi_end;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001242#ifdef CONFIG_MIPS_MT_SMTC
1243 /*
1244 * We need to provide the SMTC vectored interrupt handler
1245 * not only with the address of the handler, but with the
1246 * Status.IM bit to be masked before going there.
1247 */
1248 extern char except_vec_vi_mori;
1249 const int mori_offset = &except_vec_vi_mori - &except_vec_vi;
1250#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001251 const int handler_len = &except_vec_vi_end - &except_vec_vi;
1252 const int lui_offset = &except_vec_vi_lui - &except_vec_vi;
1253 const int ori_offset = &except_vec_vi_ori - &except_vec_vi;
1254
1255 if (handler_len > VECTORSPACING) {
1256 /*
1257 * Sigh... panicing won't help as the console
1258 * is probably not configured :(
1259 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001260 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001261 }
1262
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001263 memcpy(b, &except_vec_vi, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001264#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001265 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1266
Ralf Baechle41c594a2006-04-05 09:45:45 +01001267 w = (u32 *)(b + mori_offset);
1268 *w = (*w & 0xffff0000) | (0x100 << n);
1269#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001270 w = (u32 *)(b + lui_offset);
1271 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1272 w = (u32 *)(b + ori_offset);
1273 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
1274 flush_icache_range((unsigned long)b, (unsigned long)(b+handler_len));
1275 }
1276 else {
1277 /*
1278 * In other cases jump directly to the interrupt handler
1279 *
1280 * It is the handlers responsibility to save registers if required
1281 * (eg hi/lo) and return from the exception using "eret"
1282 */
1283 w = (u32 *)b;
1284 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1285 *w = 0;
1286 flush_icache_range((unsigned long)b, (unsigned long)(b+8));
1287 }
1288
1289 return (void *)old_handler;
1290}
1291
Ralf Baechleef300e42007-05-06 18:31:18 +01001292void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001293{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001294 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001295}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/*
1298 * This is used by native signal handling
1299 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001300asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1301asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001303extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1304extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001306extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1307extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Ralf Baechle41c594a2006-04-05 09:45:45 +01001309#ifdef CONFIG_SMP
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001310static int smp_save_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001311{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001312 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001313 ? _save_fp_context(sc)
1314 : fpu_emulator_save_context(sc);
1315}
1316
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001317static int smp_restore_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001318{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001319 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001320 ? _restore_fp_context(sc)
1321 : fpu_emulator_restore_context(sc);
1322}
1323#endif
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325static inline void signal_init(void)
1326{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001327#ifdef CONFIG_SMP
1328 /* For now just do the cpu_has_fpu check when the functions are invoked */
1329 save_fp_context = smp_save_fp_context;
1330 restore_fp_context = smp_restore_fp_context;
1331#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (cpu_has_fpu) {
1333 save_fp_context = _save_fp_context;
1334 restore_fp_context = _restore_fp_context;
1335 } else {
1336 save_fp_context = fpu_emulator_save_context;
1337 restore_fp_context = fpu_emulator_restore_context;
1338 }
Ralf Baechle41c594a2006-04-05 09:45:45 +01001339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342#ifdef CONFIG_MIPS32_COMPAT
1343
1344/*
1345 * This is used by 32-bit signal stuff on the 64-bit kernel
1346 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001347asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1348asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001350extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1351extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001353extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1354extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356static inline void signal32_init(void)
1357{
1358 if (cpu_has_fpu) {
1359 save_fp_context32 = _save_fp_context32;
1360 restore_fp_context32 = _restore_fp_context32;
1361 } else {
1362 save_fp_context32 = fpu_emulator_save_context32;
1363 restore_fp_context32 = fpu_emulator_restore_context32;
1364 }
1365}
1366#endif
1367
1368extern void cpu_cache_init(void);
1369extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001370extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Ralf Baechle42f77542007-10-18 17:48:11 +01001372/*
1373 * Timer interrupt
1374 */
1375int cp0_compare_irq;
1376
1377/*
1378 * Performance counter IRQ or -1 if shared with timer
1379 */
1380int cp0_perfcount_irq;
1381EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1382
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001383static int __cpuinitdata noulri;
1384
1385static int __init ulri_disable(char *s)
1386{
1387 pr_info("Disabling ulri\n");
1388 noulri = 1;
1389
1390 return 1;
1391}
1392__setup("noulri", ulri_disable);
1393
Ralf Baechle234fcd12008-03-08 09:56:28 +00001394void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 unsigned int cpu = smp_processor_id();
1397 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001398#ifdef CONFIG_MIPS_MT_SMTC
1399 int secondaryTC = 0;
1400 int bootTC = (cpu == 0);
1401
1402 /*
1403 * Only do per_cpu_trap_init() for first TC of Each VPE.
1404 * Note that this hack assumes that the SMTC init code
1405 * assigns TCs consecutively and in ascending order.
1406 */
1407
1408 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1409 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1410 secondaryTC = 1;
1411#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
1414 * Disable coprocessors and select 32-bit or 64-bit addressing
1415 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1416 * flag that some firmware may have left set and the TS bit (for
1417 * IP27). Set XX for ISA IV code to work.
1418 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001419#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1421#endif
1422 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1423 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001424 if (cpu_has_dsp)
1425 status_set |= ST0_MX;
1426
Ralf Baechleb38c7392006-02-07 01:20:43 +00001427 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 -07001428 status_set);
1429
Ralf Baechlea3692022007-07-10 17:33:02 +01001430 if (cpu_has_mips_r2) {
1431 unsigned int enable = 0x0000000f;
1432
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001433 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001434 enable |= (1 << 29);
1435
1436 write_c0_hwrena(enable);
1437 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001438
Ralf Baechle41c594a2006-04-05 09:45:45 +01001439#ifdef CONFIG_MIPS_MT_SMTC
1440 if (!secondaryTC) {
1441#endif /* CONFIG_MIPS_MT_SMTC */
1442
Ralf Baechlee01402b2005-07-14 15:57:16 +00001443 if (cpu_has_veic || cpu_has_vint) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001444 write_c0_ebase(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001445 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001446 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001447 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001448 if (cpu_has_divec) {
1449 if (cpu_has_mipsmt) {
1450 unsigned int vpflags = dvpe();
1451 set_c0_cause(CAUSEF_IV);
1452 evpe(vpflags);
1453 } else
1454 set_c0_cause(CAUSEF_IV);
1455 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001456
1457 /*
1458 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1459 *
1460 * o read IntCtl.IPTI to determine the timer interrupt
1461 * o read IntCtl.IPPCI to determine the performance counter interrupt
1462 */
1463 if (cpu_has_mips_r2) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001464 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1465 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001466 if (cp0_perfcount_irq == cp0_compare_irq)
1467 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001468 } else {
1469 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001470 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001471 }
1472
Ralf Baechle41c594a2006-04-05 09:45:45 +01001473#ifdef CONFIG_MIPS_MT_SMTC
1474 }
1475#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1478 TLBMISS_HANDLER_SETUP();
1479
1480 atomic_inc(&init_mm.mm_count);
1481 current->active_mm = &init_mm;
1482 BUG_ON(current->mm);
1483 enter_lazy_tlb(&init_mm, current);
1484
Ralf Baechle41c594a2006-04-05 09:45:45 +01001485#ifdef CONFIG_MIPS_MT_SMTC
1486 if (bootTC) {
1487#endif /* CONFIG_MIPS_MT_SMTC */
1488 cpu_cache_init();
1489 tlb_init();
1490#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001491 } else if (!secondaryTC) {
1492 /*
1493 * First TC in non-boot VPE must do subset of tlb_init()
1494 * for MMU countrol registers.
1495 */
1496 write_c0_pagemask(PM_DEFAULT_MASK);
1497 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001498 }
1499#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Ralf Baechlee01402b2005-07-14 15:57:16 +00001502/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001503void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001504{
1505 memcpy((void *)(ebase + offset), addr, size);
1506 flush_icache_range(ebase + offset, ebase + offset + size);
1507}
1508
Ralf Baechle234fcd12008-03-08 09:56:28 +00001509static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001510 "Trying to set NULL cache error exception handler";
1511
Ralf Baechlee01402b2005-07-14 15:57:16 +00001512/* Install uncached CPU exception handler */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001513void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1514 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001515{
1516#ifdef CONFIG_32BIT
1517 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1518#endif
1519#ifdef CONFIG_64BIT
1520 unsigned long uncached_ebase = TO_UNCAC(ebase);
1521#endif
1522
Ralf Baechle641e97f2007-10-11 23:46:05 +01001523 if (!addr)
1524 panic(panic_null_cerr);
1525
Ralf Baechlee01402b2005-07-14 15:57:16 +00001526 memcpy((void *)(uncached_ebase + offset), addr, size);
1527}
1528
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001529static int __initdata rdhwr_noopt;
1530static int __init set_rdhwr_noopt(char *str)
1531{
1532 rdhwr_noopt = 1;
1533 return 1;
1534}
1535
1536__setup("rdhwr_noopt", set_rdhwr_noopt);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538void __init trap_init(void)
1539{
1540 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 extern char except_vec4;
1542 unsigned long i;
1543
Ralf Baechlee01402b2005-07-14 15:57:16 +00001544 if (cpu_has_veic || cpu_has_vint)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001545 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001546 else
1547 ebase = CAC_BASE;
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 per_cpu_trap_init();
1550
1551 /*
1552 * Copy the generic exception handlers to their final destination.
1553 * This will be overriden later as suitable for a particular
1554 * configuration.
1555 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001556 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /*
1559 * Setup default vectors
1560 */
1561 for (i = 0; i <= 31; i++)
1562 set_except_vector(i, handle_reserved);
1563
1564 /*
1565 * Copy the EJTAG debug exception vector handler code to it's final
1566 * destination.
1567 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001568 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001569 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /*
1572 * Only some CPUs have the watch exceptions.
1573 */
1574 if (cpu_has_watch)
1575 set_except_vector(23, handle_watch);
1576
1577 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001578 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001580 if (cpu_has_veic || cpu_has_vint) {
1581 int nvec = cpu_has_veic ? 64 : 8;
1582 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001583 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001584 }
1585 else if (cpu_has_divec)
1586 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * Some CPUs can enable/disable for cache parity detection, but does
1590 * it different ways.
1591 */
1592 parity_protection_init();
1593
1594 /*
1595 * The Data Bus Errors / Instruction Bus Errors are signaled
1596 * by external hardware. Therefore these two exceptions
1597 * may have board specific handlers.
1598 */
1599 if (board_be_init)
1600 board_be_init();
1601
Ralf Baechlee4ac58a2006-04-03 17:56:36 +01001602 set_except_vector(0, handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 set_except_vector(1, handle_tlbm);
1604 set_except_vector(2, handle_tlbl);
1605 set_except_vector(3, handle_tlbs);
1606
1607 set_except_vector(4, handle_adel);
1608 set_except_vector(5, handle_ades);
1609
1610 set_except_vector(6, handle_ibe);
1611 set_except_vector(7, handle_dbe);
1612
1613 set_except_vector(8, handle_sys);
1614 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001615 set_except_vector(10, rdhwr_noopt ? handle_ri :
1616 (cpu_has_vtag_icache ?
1617 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 set_except_vector(11, handle_cpu);
1619 set_except_vector(12, handle_ov);
1620 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Ralf Baechle10cc3522007-10-11 23:46:15 +01001622 if (current_cpu_type() == CPU_R6000 ||
1623 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /*
1625 * The R6000 is the only R-series CPU that features a machine
1626 * check exception (similar to the R4000 cache error) and
1627 * unaligned ldc1/sdc1 exception. The handlers have not been
1628 * written yet. Well, anyway there is no R6000 machine on the
1629 * current list of targets for Linux/MIPS.
1630 * (Duh, crap, there is someone with a triple R6k machine)
1631 */
1632 //set_except_vector(14, handle_mc);
1633 //set_except_vector(15, handle_ndc);
1634 }
1635
Ralf Baechlee01402b2005-07-14 15:57:16 +00001636
1637 if (board_nmi_handler_setup)
1638 board_nmi_handler_setup();
1639
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001640 if (cpu_has_fpu && !cpu_has_nofpuex)
1641 set_except_vector(15, handle_fpe);
1642
1643 set_except_vector(22, handle_mdmx);
1644
1645 if (cpu_has_mcheck)
1646 set_except_vector(24, handle_mcheck);
1647
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001648 if (cpu_has_mipsmt)
1649 set_except_vector(25, handle_mt);
1650
Chris Dearmanacaec422007-05-24 22:30:18 +01001651 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001652
1653 if (cpu_has_vce)
1654 /* Special exception: R4[04]00 uses also the divec space. */
1655 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_r4000, 0x100);
1656 else if (cpu_has_4kex)
1657 memcpy((void *)(CAC_BASE + 0x180), &except_vec3_generic, 0x80);
1658 else
1659 memcpy((void *)(CAC_BASE + 0x080), &except_vec3_generic, 0x80);
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 signal_init();
1662#ifdef CONFIG_MIPS32_COMPAT
1663 signal32_init();
1664#endif
1665
Ralf Baechlee01402b2005-07-14 15:57:16 +00001666 flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001667 flush_tlb_handlers();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}