blob: 31b204b26ba08915e647369fbe6c3c0930a7642e [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>
Jason Wessel88547002008-07-29 15:58:53 -050026#include <linux/kgdb.h>
27#include <linux/kdebug.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000028#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/bootinfo.h>
31#include <asm/branch.h>
32#include <asm/break.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000033#include <asm/cop2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/cpu.h>
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000035#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/fpu.h>
Ralf Baechleba3049e2008-10-28 17:38:42 +000037#include <asm/fpu_emulator.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000038#include <asm/mipsregs.h>
39#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/module.h>
41#include <asm/pgtable.h>
42#include <asm/ptrace.h>
43#include <asm/sections.h>
44#include <asm/system.h>
45#include <asm/tlbdebug.h>
46#include <asm/traps.h>
47#include <asm/uaccess.h>
David Daneyb67b2b72008-09-23 00:08:45 -070048#include <asm/watch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090051#include <asm/stacktrace.h>
David Daneyf9bb4cf2008-12-11 15:33:23 -080052#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090054extern void check_wait(void);
55extern asmlinkage void r4k_wait(void);
56extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010057extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058extern asmlinkage void handle_tlbm(void);
59extern asmlinkage void handle_tlbl(void);
60extern asmlinkage void handle_tlbs(void);
61extern asmlinkage void handle_adel(void);
62extern asmlinkage void handle_ades(void);
63extern asmlinkage void handle_ibe(void);
64extern asmlinkage void handle_dbe(void);
65extern asmlinkage void handle_sys(void);
66extern asmlinkage void handle_bp(void);
67extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090068extern asmlinkage void handle_ri_rdhwr_vivt(void);
69extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070extern asmlinkage void handle_cpu(void);
71extern asmlinkage void handle_ov(void);
72extern asmlinkage void handle_tr(void);
73extern asmlinkage void handle_fpe(void);
74extern asmlinkage void handle_mdmx(void);
75extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000076extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000077extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078extern asmlinkage void handle_mcheck(void);
79extern asmlinkage void handle_reserved(void);
80
Ralf Baechle12616ed2005-10-18 10:26:46 +010081extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090082 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84void (*board_be_init)(void);
85int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000086void (*board_nmi_handler_setup)(void);
87void (*board_ejtag_handler_setup)(void);
88void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020091static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090092{
Ralf Baechle39b8d522008-04-28 17:14:26 +010093 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090094 unsigned long addr;
95
96 printk("Call Trace:");
97#ifdef CONFIG_KALLSYMS
98 printk("\n");
99#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200100 while (!kstack_end(sp)) {
101 unsigned long __user *p =
102 (unsigned long __user *)(unsigned long)sp++;
103 if (__get_user(addr, p)) {
104 printk(" (Bad stack address)");
105 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100106 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200107 if (__kernel_text_address(addr))
108 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900109 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200110 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900111}
112
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900113#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900114int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900115static int __init set_raw_show_trace(char *str)
116{
117 raw_show_trace = 1;
118 return 1;
119}
120__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900121#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200122
Ralf Baechleeae23f22007-10-14 23:27:21 +0100123static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900124{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200125 unsigned long sp = regs->regs[29];
126 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900127 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900128
129 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200130 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900131 return;
132 }
133 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200134 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200135 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900136 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200137 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900138 printk("\n");
139}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * This routine abuses get_user()/put_user() to reference pointers
143 * with at least a bit of error checking ...
144 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100145static void show_stacktrace(struct task_struct *task,
146 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 const int field = 2 * sizeof(unsigned long);
149 long stackdata;
150 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900151 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 printk("Stack :");
154 i = 0;
155 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
156 if (i && ((i % (64 / field)) == 0))
157 printk("\n ");
158 if (i > 39) {
159 printk(" ...");
160 break;
161 }
162
163 if (__get_user(stackdata, sp++)) {
164 printk(" (Bad stack address)");
165 break;
166 }
167
168 printk(" %0*lx", field, stackdata);
169 i++;
170 }
171 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200172 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900173}
174
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900175void show_stack(struct task_struct *task, unsigned long *sp)
176{
177 struct pt_regs regs;
178 if (sp) {
179 regs.regs[29] = (unsigned long)sp;
180 regs.regs[31] = 0;
181 regs.cp0_epc = 0;
182 } else {
183 if (task && task != current) {
184 regs.regs[29] = task->thread.reg29;
185 regs.regs[31] = 0;
186 regs.cp0_epc = task->thread.reg31;
187 } else {
188 prepare_frametrace(&regs);
189 }
190 }
191 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * The architecture-independent dump_stack generator
196 */
197void dump_stack(void)
198{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200199 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200201 prepare_frametrace(&regs);
202 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205EXPORT_SYMBOL(dump_stack);
206
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900207static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100210 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 printk("\nCode:");
213
Ralf Baechle39b8d522008-04-28 17:14:26 +0100214 if ((unsigned long)pc & 1)
215 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 for(i = -3 ; i < 6 ; i++) {
217 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100218 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 printk(" (Bad address in epc)\n");
220 break;
221 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100222 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224}
225
Ralf Baechleeae23f22007-10-14 23:27:21 +0100226static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 const int field = 2 * sizeof(unsigned long);
229 unsigned int cause = regs->cp0_cause;
230 int i;
231
232 printk("Cpu %d\n", smp_processor_id());
233
234 /*
235 * Saved main processor registers
236 */
237 for (i = 0; i < 32; ) {
238 if ((i % 4) == 0)
239 printk("$%2d :", i);
240 if (i == 0)
241 printk(" %0*lx", field, 0UL);
242 else if (i == 26 || i == 27)
243 printk(" %*s", field, "");
244 else
245 printk(" %0*lx", field, regs->regs[i]);
246
247 i++;
248 if ((i % 4) == 0)
249 printk("\n");
250 }
251
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100252#ifdef CONFIG_CPU_HAS_SMARTMIPS
253 printk("Acx : %0*lx\n", field, regs->acx);
254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 printk("Hi : %0*lx\n", field, regs->hi);
256 printk("Lo : %0*lx\n", field, regs->lo);
257
258 /*
259 * Saved cp0 registers
260 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100261 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
262 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100264 printk("ra : %0*lx %pS\n", field, regs->regs[31],
265 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 printk("Status: %08x ", (uint32_t) regs->cp0_status);
268
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000269 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
270 if (regs->cp0_status & ST0_KUO)
271 printk("KUo ");
272 if (regs->cp0_status & ST0_IEO)
273 printk("IEo ");
274 if (regs->cp0_status & ST0_KUP)
275 printk("KUp ");
276 if (regs->cp0_status & ST0_IEP)
277 printk("IEp ");
278 if (regs->cp0_status & ST0_KUC)
279 printk("KUc ");
280 if (regs->cp0_status & ST0_IEC)
281 printk("IEc ");
282 } else {
283 if (regs->cp0_status & ST0_KX)
284 printk("KX ");
285 if (regs->cp0_status & ST0_SX)
286 printk("SX ");
287 if (regs->cp0_status & ST0_UX)
288 printk("UX ");
289 switch (regs->cp0_status & ST0_KSU) {
290 case KSU_USER:
291 printk("USER ");
292 break;
293 case KSU_SUPERVISOR:
294 printk("SUPERVISOR ");
295 break;
296 case KSU_KERNEL:
297 printk("KERNEL ");
298 break;
299 default:
300 printk("BAD_MODE ");
301 break;
302 }
303 if (regs->cp0_status & ST0_ERL)
304 printk("ERL ");
305 if (regs->cp0_status & ST0_EXL)
306 printk("EXL ");
307 if (regs->cp0_status & ST0_IE)
308 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 printk("\n");
311
312 printk("Cause : %08x\n", cause);
313
314 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
315 if (1 <= cause && cause <= 5)
316 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
317
Ralf Baechle9966db252007-10-11 23:46:17 +0100318 printk("PrId : %08x (%s)\n", read_c0_prid(),
319 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Ralf Baechleeae23f22007-10-14 23:27:21 +0100322/*
323 * FIXME: really the generic show_regs should take a const pointer argument.
324 */
325void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100327 __show_regs((struct pt_regs *)regs);
328}
329
330void show_registers(const struct pt_regs *regs)
331{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100332 const int field = 2 * sizeof(unsigned long);
333
Ralf Baechleeae23f22007-10-14 23:27:21 +0100334 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100336 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
337 current->comm, current->pid, current_thread_info(), current,
338 field, current_thread_info()->tp_value);
339 if (cpu_has_userlocal) {
340 unsigned long tls;
341
342 tls = read_c0_userlocal();
343 if (tls != current_thread_info()->tp_value)
344 printk("*HwTLS: %0*lx\n", field, tls);
345 }
346
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900347 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900348 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 printk("\n");
350}
351
352static DEFINE_SPINLOCK(die_lock);
353
Ralf Baechleeae23f22007-10-14 23:27:21 +0100354void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100357#ifdef CONFIG_MIPS_MT_SMTC
358 unsigned long dvpret = dvpe();
359#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 console_verbose();
362 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100363 bust_spinlocks(1);
364#ifdef CONFIG_MIPS_MT_SMTC
365 mips_mt_regdump(dvpret);
366#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100367 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700369 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200371
372 if (in_interrupt())
373 panic("Fatal exception in interrupt");
374
375 if (panic_on_oops) {
376 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
377 ssleep(5);
378 panic("Fatal exception");
379 }
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 do_exit(SIGSEGV);
382}
383
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200384extern struct exception_table_entry __start___dbe_table[];
385extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000387__asm__(
388" .section __dbe_table, \"a\"\n"
389" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/* Given an address, look for it in the exception tables. */
392static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
393{
394 const struct exception_table_entry *e;
395
396 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
397 if (!e)
398 e = search_module_dbetables(addr);
399 return e;
400}
401
402asmlinkage void do_be(struct pt_regs *regs)
403{
404 const int field = 2 * sizeof(unsigned long);
405 const struct exception_table_entry *fixup = NULL;
406 int data = regs->cp0_cause & 4;
407 int action = MIPS_BE_FATAL;
408
409 /* XXX For now. Fixme, this searches the wrong table ... */
410 if (data && !user_mode(regs))
411 fixup = search_dbe_tables(exception_epc(regs));
412
413 if (fixup)
414 action = MIPS_BE_FIXUP;
415
416 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900417 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 switch (action) {
420 case MIPS_BE_DISCARD:
421 return;
422 case MIPS_BE_FIXUP:
423 if (fixup) {
424 regs->cp0_epc = fixup->nextinsn;
425 return;
426 }
427 break;
428 default:
429 break;
430 }
431
432 /*
433 * Assume it would be too dangerous to continue ...
434 */
435 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
436 data ? "Data" : "Instruction",
437 field, regs->cp0_epc, field, regs->regs[31]);
Jason Wessel88547002008-07-29 15:58:53 -0500438 if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
439 == NOTIFY_STOP)
440 return;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 die_if_kernel("Oops", regs);
443 force_sig(SIGBUS, current);
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100447 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449
450#define OPCODE 0xfc000000
451#define BASE 0x03e00000
452#define RT 0x001f0000
453#define OFFSET 0x0000ffff
454#define LL 0xc0000000
455#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100456#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000457#define SPEC3 0x7c000000
458#define RD 0x0000f800
459#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100460#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000461#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463/*
464 * The ll_bit is cleared by r*_switch.S
465 */
466
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200467unsigned int ll_bit;
468struct task_struct *ll_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100470static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000472 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
476 * analyse the ll instruction that just caused a ri exception
477 * and put the referenced address to addr.
478 */
479
480 /* sign extend offset */
481 offset = opcode & OFFSET;
482 offset <<= 16;
483 offset >>= 16;
484
Ralf Baechlefe00f942005-03-01 19:22:29 +0000485 vaddr = (unsigned long __user *)
486 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100488 if ((unsigned long)vaddr & 3)
489 return SIGBUS;
490 if (get_user(value, vaddr))
491 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 preempt_disable();
494
495 if (ll_task == NULL || ll_task == current) {
496 ll_bit = 1;
497 } else {
498 ll_bit = 0;
499 }
500 ll_task = current;
501
502 preempt_enable();
503
504 regs->regs[(opcode & RT) >> 16] = value;
505
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100509static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000511 unsigned long __user *vaddr;
512 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /*
516 * analyse the sc instruction that just caused a ri exception
517 * and put the referenced address to addr.
518 */
519
520 /* sign extend offset */
521 offset = opcode & OFFSET;
522 offset <<= 16;
523 offset >>= 16;
524
Ralf Baechlefe00f942005-03-01 19:22:29 +0000525 vaddr = (unsigned long __user *)
526 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 reg = (opcode & RT) >> 16;
528
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100529 if ((unsigned long)vaddr & 3)
530 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 preempt_disable();
533
534 if (ll_bit == 0 || ll_task != current) {
535 regs->regs[reg] = 0;
536 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 preempt_enable();
541
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100542 if (put_user(regs->regs[reg], vaddr))
543 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 regs->regs[reg] = 1;
546
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
551 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
552 * opcodes are supposed to result in coprocessor unusable exceptions if
553 * executed on ll/sc-less processors. That's the theory. In practice a
554 * few processors such as NEC's VR4100 throw reserved instruction exceptions
555 * instead, so we're doing the emulation thing in both exception handlers.
556 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100557static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100559 if ((opcode & OPCODE) == LL)
560 return simulate_ll(regs, opcode);
561 if ((opcode & OPCODE) == SC)
562 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100564 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Ralf Baechle3c370262005-04-13 17:43:59 +0000567/*
568 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100569 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000570 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100571static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000572{
Al Virodc8f6022006-01-12 01:06:07 -0800573 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000574
575 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
576 int rd = (opcode & RD) >> 11;
577 int rt = (opcode & RT) >> 16;
578 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100579 case 0: /* CPU number */
580 regs->regs[rt] = smp_processor_id();
581 return 0;
582 case 1: /* SYNCI length */
583 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
584 current_cpu_data.icache.linesz);
585 return 0;
586 case 2: /* Read count register */
587 regs->regs[rt] = read_c0_count();
588 return 0;
589 case 3: /* Count register resolution */
590 switch (current_cpu_data.cputype) {
591 case CPU_20KC:
592 case CPU_25KF:
593 regs->regs[rt] = 1;
594 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000595 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100596 regs->regs[rt] = 2;
597 }
598 return 0;
599 case 29:
600 regs->regs[rt] = ti->tp_value;
601 return 0;
602 default:
603 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000604 }
605 }
606
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500607 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100608 return -1;
609}
Ralf Baechlee5679882006-11-30 01:14:47 +0000610
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100611static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
612{
613 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
614 return 0;
615
616 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619asmlinkage void do_ov(struct pt_regs *regs)
620{
621 siginfo_t info;
622
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000623 die_if_kernel("Integer overflow", regs);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 info.si_code = FPE_INTOVF;
626 info.si_signo = SIGFPE;
627 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000628 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 force_sig_info(SIGFPE, &info, current);
630}
631
632/*
633 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
634 */
635asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
636{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100637 siginfo_t info;
638
Jason Wessel88547002008-07-29 15:58:53 -0500639 if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
640 == NOTIFY_STOP)
641 return;
Chris Dearman57725f92006-06-30 23:35:28 +0100642 die_if_kernel("FP exception in kernel code", regs);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (fcr31 & FPU_CSR_UNI_X) {
645 int sig;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000648 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * software emulator on-board, let's use it...
650 *
651 * Force FPU to dump state into task/thread context. We're
652 * moving a lot of data here for what is probably a single
653 * instruction, but the alternative is to pre-decode the FP
654 * register operands before invoking the emulator, which seems
655 * a bit extreme for what should be an infrequent event.
656 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000657 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900658 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100661 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*
664 * We can't allow the emulated instruction to leave any of
665 * the cause bit set in $fcr31.
666 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900667 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900670 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* If something went wrong, signal */
673 if (sig)
674 force_sig(sig, current);
675
676 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100677 } else if (fcr31 & FPU_CSR_INV_X)
678 info.si_code = FPE_FLTINV;
679 else if (fcr31 & FPU_CSR_DIV_X)
680 info.si_code = FPE_FLTDIV;
681 else if (fcr31 & FPU_CSR_OVF_X)
682 info.si_code = FPE_FLTOVF;
683 else if (fcr31 & FPU_CSR_UDF_X)
684 info.si_code = FPE_FLTUND;
685 else if (fcr31 & FPU_CSR_INE_X)
686 info.si_code = FPE_FLTRES;
687 else
688 info.si_code = __SI_FAULT;
689 info.si_signo = SIGFPE;
690 info.si_errno = 0;
691 info.si_addr = (void __user *) regs->cp0_epc;
692 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Ralf Baechledf270052008-04-20 16:28:54 +0100695static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
696 const char *str)
697{
698 siginfo_t info;
699 char b[40];
700
Jason Wessel88547002008-07-29 15:58:53 -0500701 if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
702 return;
703
Ralf Baechledf270052008-04-20 16:28:54 +0100704 /*
705 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
706 * insns, even for trap and break codes that indicate arithmetic
707 * failures. Weird ...
708 * But should we continue the brokenness??? --macro
709 */
710 switch (code) {
711 case BRK_OVERFLOW:
712 case BRK_DIVZERO:
713 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
714 die_if_kernel(b, regs);
715 if (code == BRK_DIVZERO)
716 info.si_code = FPE_INTDIV;
717 else
718 info.si_code = FPE_INTOVF;
719 info.si_signo = SIGFPE;
720 info.si_errno = 0;
721 info.si_addr = (void __user *) regs->cp0_epc;
722 force_sig_info(SIGFPE, &info, current);
723 break;
724 case BRK_BUG:
725 die_if_kernel("Kernel bug detected", regs);
726 force_sig(SIGTRAP, current);
727 break;
Ralf Baechleba3049e2008-10-28 17:38:42 +0000728 case BRK_MEMU:
729 /*
730 * Address errors may be deliberately induced by the FPU
731 * emulator to retake control of the CPU after executing the
732 * instruction in the delay slot of an emulated branch.
733 *
734 * Terminate if exception was recognized as a delay slot return
735 * otherwise handle as normal.
736 */
737 if (do_dsemulret(regs))
738 return;
739
740 die_if_kernel("Math emu break/trap", regs);
741 force_sig(SIGTRAP, current);
742 break;
Ralf Baechledf270052008-04-20 16:28:54 +0100743 default:
744 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
745 die_if_kernel(b, regs);
746 force_sig(SIGTRAP, current);
747 }
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750asmlinkage void do_bp(struct pt_regs *regs)
751{
752 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900754 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000755 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
758 * There is the ancient bug in the MIPS assemblers that the break
759 * code starts left to bit 16 instead to bit 6 in the opcode.
760 * Gas is bug-compatible, but not always, grrr...
761 * We handle both cases with a simple heuristics. --macro
762 */
763 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100764 if (bcode >= (1 << 10))
765 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Ralf Baechledf270052008-04-20 16:28:54 +0100767 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900768 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000769
770out_sigsegv:
771 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774asmlinkage void do_tr(struct pt_regs *regs)
775{
776 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900778 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000779 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* Immediate versions don't provide a code. */
782 if (!(opcode & OPCODE))
783 tcode = ((opcode >> 6) & ((1 << 10) - 1));
784
Ralf Baechledf270052008-04-20 16:28:54 +0100785 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900786 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000787
788out_sigsegv:
789 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792asmlinkage void do_ri(struct pt_regs *regs)
793{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100794 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
795 unsigned long old_epc = regs->cp0_epc;
796 unsigned int opcode = 0;
797 int status = -1;
798
Jason Wessel88547002008-07-29 15:58:53 -0500799 if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
800 == NOTIFY_STOP)
801 return;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 die_if_kernel("Reserved instruction in kernel code", regs);
804
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100805 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000806 return;
807
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100808 if (unlikely(get_user(opcode, epc) < 0))
809 status = SIGSEGV;
810
811 if (!cpu_has_llsc && status < 0)
812 status = simulate_llsc(regs, opcode);
813
814 if (status < 0)
815 status = simulate_rdhwr(regs, opcode);
816
817 if (status < 0)
818 status = simulate_sync(regs, opcode);
819
820 if (status < 0)
821 status = SIGILL;
822
823 if (unlikely(status > 0)) {
824 regs->cp0_epc = old_epc; /* Undo skip-over. */
825 force_sig(status, current);
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Ralf Baechled223a862007-07-10 17:33:02 +0100829/*
830 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
831 * emulated more than some threshold number of instructions, force migration to
832 * a "CPU" that has FP support.
833 */
834static void mt_ase_fp_affinity(void)
835{
836#ifdef CONFIG_MIPS_MT_FPAFF
837 if (mt_fpemul_threshold > 0 &&
838 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
839 /*
840 * If there's no FPU present, or if the application has already
841 * restricted the allowed set to exclude any CPUs with FPUs,
842 * we'll skip the procedure.
843 */
844 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
845 cpumask_t tmask;
846
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200847 current->thread.user_cpus_allowed
848 = current->cpus_allowed;
849 cpus_and(tmask, current->cpus_allowed,
850 mt_fpu_cpumask);
Ralf Baechled223a862007-07-10 17:33:02 +0100851 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100852 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100853 }
854 }
855#endif /* CONFIG_MIPS_MT_FPAFF */
856}
857
Ralf Baechle69f3a7d2009-11-24 01:24:58 +0000858/*
859 * No lock; only written during early bootup by CPU 0.
860 */
861static RAW_NOTIFIER_HEAD(cu2_chain);
862
863int __ref register_cu2_notifier(struct notifier_block *nb)
864{
865 return raw_notifier_chain_register(&cu2_chain, nb);
866}
867
868int cu2_notifier_call_chain(unsigned long val, void *v)
869{
870 return raw_notifier_call_chain(&cu2_chain, val, v);
871}
872
873static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
874 void *data)
875{
876 struct pt_regs *regs = data;
877
878 switch (action) {
879 default:
880 die_if_kernel("Unhandled kernel unaligned access or invalid "
881 "instruction", regs);
882 /* Fall through */
883
884 case CU2_EXCEPTION:
885 force_sig(SIGILL, current);
886 }
887
888 return NOTIFY_OK;
889}
890
891static struct notifier_block default_cu2_notifier = {
892 .notifier_call = default_cu2_call,
893 .priority = 0x80000000, /* Run last */
894};
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896asmlinkage void do_cpu(struct pt_regs *regs)
897{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100898 unsigned int __user *epc;
899 unsigned long old_epc;
900 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100902 int status;
David Daneyf9bb4cf2008-12-11 15:33:23 -0800903 unsigned long __maybe_unused flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Atsushi Nemoto53231802007-04-14 02:37:26 +0900905 die_if_kernel("do_cpu invoked from kernel context!", regs);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
908
909 switch (cpid) {
910 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100911 epc = (unsigned int __user *)exception_epc(regs);
912 old_epc = regs->cp0_epc;
913 opcode = 0;
914 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100916 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000918
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100919 if (unlikely(get_user(opcode, epc) < 0))
920 status = SIGSEGV;
921
922 if (!cpu_has_llsc && status < 0)
923 status = simulate_llsc(regs, opcode);
924
925 if (status < 0)
926 status = simulate_rdhwr(regs, opcode);
927
928 if (status < 0)
929 status = SIGILL;
930
931 if (unlikely(status > 0)) {
932 regs->cp0_epc = old_epc; /* Undo skip-over. */
933 force_sig(status, current);
934 }
935
936 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900939 if (used_math()) /* Using the FPU again. */
940 own_fpu(1);
941 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 init_fpu();
943 set_used_math();
944 }
945
Atsushi Nemoto53231802007-04-14 02:37:26 +0900946 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900947 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900948 sig = fpu_emulator_cop1Handler(regs,
949 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (sig)
951 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +0100952 else
953 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return;
957
958 case 2:
Ralf Baechle69f3a7d2009-11-24 01:24:58 +0000959 raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
960 break;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 case 3:
963 break;
964 }
965
966 force_sig(SIGILL, current);
967}
968
969asmlinkage void do_mdmx(struct pt_regs *regs)
970{
971 force_sig(SIGILL, current);
972}
973
David Daney8bc6d052009-01-05 15:29:58 -0800974/*
975 * Called with interrupts disabled.
976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977asmlinkage void do_watch(struct pt_regs *regs)
978{
David Daneyb67b2b72008-09-23 00:08:45 -0700979 u32 cause;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /*
David Daneyb67b2b72008-09-23 00:08:45 -0700982 * Clear WP (bit 22) bit of cause register so we don't loop
983 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
David Daneyb67b2b72008-09-23 00:08:45 -0700985 cause = read_c0_cause();
986 cause &= ~(1 << 22);
987 write_c0_cause(cause);
988
989 /*
990 * If the current thread has the watch registers loaded, save
991 * their values and send SIGTRAP. Otherwise another thread
992 * left the registers set, clear them and continue.
993 */
994 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
995 mips_read_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -0800996 local_irq_enable();
David Daneyb67b2b72008-09-23 00:08:45 -0700997 force_sig(SIGTRAP, current);
David Daney8bc6d052009-01-05 15:29:58 -0800998 } else {
David Daneyb67b2b72008-09-23 00:08:45 -0700999 mips_clear_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -08001000 local_irq_enable();
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004asmlinkage void do_mcheck(struct pt_regs *regs)
1005{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001006 const int field = 2 * sizeof(unsigned long);
1007 int multi_match = regs->cp0_status & ST0_TS;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001010
1011 if (multi_match) {
1012 printk("Index : %0x\n", read_c0_index());
1013 printk("Pagemask: %0x\n", read_c0_pagemask());
1014 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
1015 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
1016 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
1017 printk("\n");
1018 dump_tlb_all();
1019 }
1020
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +09001021 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * Some chips may have other causes of machine check (e.g. SB1
1025 * graduation timer)
1026 */
1027 panic("Caught Machine Check exception - %scaused by multiple "
1028 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001029 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001032asmlinkage void do_mt(struct pt_regs *regs)
1033{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001034 int subcode;
1035
Ralf Baechle41c594a2006-04-05 09:45:45 +01001036 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1037 >> VPECONTROL_EXCPT_SHIFT;
1038 switch (subcode) {
1039 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001040 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001041 break;
1042 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001043 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001044 break;
1045 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001046 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001047 break;
1048 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001049 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001050 break;
1051 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001052 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001053 break;
1054 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001055 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001056 break;
1057 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001058 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +01001059 subcode);
1060 break;
1061 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001062 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1063
1064 force_sig(SIGILL, current);
1065}
1066
1067
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001068asmlinkage void do_dsp(struct pt_regs *regs)
1069{
1070 if (cpu_has_dsp)
1071 panic("Unexpected DSP exception\n");
1072
1073 force_sig(SIGILL, current);
1074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076asmlinkage void do_reserved(struct pt_regs *regs)
1077{
1078 /*
1079 * Game over - no way to handle this if it ever occurs. Most probably
1080 * caused by a new unknown cpu type or after another deadly
1081 * hard/software error.
1082 */
1083 show_regs(regs);
1084 panic("Caught reserved exception %ld - should not happen.",
1085 (regs->cp0_cause & 0x7f) >> 2);
1086}
1087
Ralf Baechle39b8d522008-04-28 17:14:26 +01001088static int __initdata l1parity = 1;
1089static int __init nol1parity(char *s)
1090{
1091 l1parity = 0;
1092 return 1;
1093}
1094__setup("nol1par", nol1parity);
1095static int __initdata l2parity = 1;
1096static int __init nol2parity(char *s)
1097{
1098 l2parity = 0;
1099 return 1;
1100}
1101__setup("nol2par", nol2parity);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/*
1104 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1105 * it different ways.
1106 */
1107static inline void parity_protection_init(void)
1108{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001109 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001111 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001112 case CPU_74K:
1113 case CPU_1004K:
1114 {
1115#define ERRCTL_PE 0x80000000
1116#define ERRCTL_L2P 0x00800000
1117 unsigned long errctl;
1118 unsigned int l1parity_present, l2parity_present;
1119
1120 errctl = read_c0_ecc();
1121 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1122
1123 /* probe L1 parity support */
1124 write_c0_ecc(errctl | ERRCTL_PE);
1125 back_to_back_c0_hazard();
1126 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1127
1128 /* probe L2 parity support */
1129 write_c0_ecc(errctl|ERRCTL_L2P);
1130 back_to_back_c0_hazard();
1131 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1132
1133 if (l1parity_present && l2parity_present) {
1134 if (l1parity)
1135 errctl |= ERRCTL_PE;
1136 if (l1parity ^ l2parity)
1137 errctl |= ERRCTL_L2P;
1138 } else if (l1parity_present) {
1139 if (l1parity)
1140 errctl |= ERRCTL_PE;
1141 } else if (l2parity_present) {
1142 if (l2parity)
1143 errctl |= ERRCTL_L2P;
1144 } else {
1145 /* No parity available */
1146 }
1147
1148 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1149
1150 write_c0_ecc(errctl);
1151 back_to_back_c0_hazard();
1152 errctl = read_c0_ecc();
1153 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1154
1155 if (l1parity_present)
1156 printk(KERN_INFO "Cache parity protection %sabled\n",
1157 (errctl & ERRCTL_PE) ? "en" : "dis");
1158
1159 if (l2parity_present) {
1160 if (l1parity_present && l1parity)
1161 errctl ^= ERRCTL_L2P;
1162 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1163 (errctl & ERRCTL_L2P) ? "en" : "dis");
1164 }
1165 }
1166 break;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001169 write_c0_ecc(0x80000000);
1170 back_to_back_c0_hazard();
1171 /* Set the PE bit (bit 31) in the c0_errctl register. */
1172 printk(KERN_INFO "Cache parity protection %sabled\n",
1173 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 case CPU_20KC:
1176 case CPU_25KF:
1177 /* Clear the DE bit (bit 16) in the c0_status register. */
1178 printk(KERN_INFO "Enable cache parity protection for "
1179 "MIPS 20KC/25KF CPUs.\n");
1180 clear_c0_status(ST0_DE);
1181 break;
1182 default:
1183 break;
1184 }
1185}
1186
1187asmlinkage void cache_parity_error(void)
1188{
1189 const int field = 2 * sizeof(unsigned long);
1190 unsigned int reg_val;
1191
1192 /* For the moment, report the problem and hang. */
1193 printk("Cache error exception:\n");
1194 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1195 reg_val = read_c0_cacheerr();
1196 printk("c0_cacheerr == %08x\n", reg_val);
1197
1198 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1199 reg_val & (1<<30) ? "secondary" : "primary",
1200 reg_val & (1<<31) ? "data" : "insn");
1201 printk("Error bits: %s%s%s%s%s%s%s\n",
1202 reg_val & (1<<29) ? "ED " : "",
1203 reg_val & (1<<28) ? "ET " : "",
1204 reg_val & (1<<26) ? "EE " : "",
1205 reg_val & (1<<25) ? "EB " : "",
1206 reg_val & (1<<24) ? "EI " : "",
1207 reg_val & (1<<23) ? "E1 " : "",
1208 reg_val & (1<<22) ? "E0 " : "");
1209 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1210
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001211#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (reg_val & (1<<22))
1213 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1214
1215 if (reg_val & (1<<23))
1216 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1217#endif
1218
1219 panic("Can't handle the cache error!");
1220}
1221
1222/*
1223 * SDBBP EJTAG debug exception handler.
1224 * We skip the instruction and return to the next instruction.
1225 */
1226void ejtag_exception_handler(struct pt_regs *regs)
1227{
1228 const int field = 2 * sizeof(unsigned long);
1229 unsigned long depc, old_epc;
1230 unsigned int debug;
1231
Chris Dearman70ae6122006-06-30 12:32:37 +01001232 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 depc = read_c0_depc();
1234 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001235 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (debug & 0x80000000) {
1237 /*
1238 * In branch delay slot.
1239 * We cheat a little bit here and use EPC to calculate the
1240 * debug return address (DEPC). EPC is restored after the
1241 * calculation.
1242 */
1243 old_epc = regs->cp0_epc;
1244 regs->cp0_epc = depc;
1245 __compute_return_epc(regs);
1246 depc = regs->cp0_epc;
1247 regs->cp0_epc = old_epc;
1248 } else
1249 depc += 4;
1250 write_c0_depc(depc);
1251
1252#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001253 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 write_c0_debug(debug | 0x100);
1255#endif
1256}
1257
1258/*
1259 * NMI exception handler.
1260 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001261NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001263 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 printk("NMI taken!!!!\n");
1265 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Ralf Baechlee01402b2005-07-14 15:57:16 +00001268#define VECTORSPACING 0x100 /* for EI/VI mode */
1269
1270unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001272unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274/*
1275 * As a side effect of the way this is implemented we're limited
1276 * to interrupt handlers in the address range from
1277 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1278 */
1279void *set_except_vector(int n, void *addr)
1280{
1281 unsigned long handler = (unsigned long) addr;
1282 unsigned long old_handler = exception_handlers[n];
1283
1284 exception_handlers[n] = handler;
1285 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001286 *(u32 *)(ebase + 0x200) = 0x08000000 |
1287 (0x03ffffff & (handler >> 2));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001288 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 return (void *)old_handler;
1291}
1292
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001293static asmlinkage void do_default_vi(void)
1294{
1295 show_regs(get_irq_regs());
1296 panic("Caught unexpected vectored interrupt.");
1297}
1298
Ralf Baechleef300e42007-05-06 18:31:18 +01001299static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001300{
1301 unsigned long handler;
1302 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001303 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001304 u32 *w;
1305 unsigned char *b;
1306
Ralf Baechleb72b7092009-03-30 14:49:44 +02001307 BUG_ON(!cpu_has_veic && !cpu_has_vint);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001308
1309 if (addr == NULL) {
1310 handler = (unsigned long) do_default_vi;
1311 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001312 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001313 handler = (unsigned long) addr;
1314 vi_handlers[n] = (unsigned long) addr;
1315
1316 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1317
Ralf Baechlef6771db2007-11-08 18:02:29 +00001318 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001319 panic("Shadow register set %d not supported", srs);
1320
1321 if (cpu_has_veic) {
1322 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001323 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001324 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001325 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001326 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001327 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001328 }
1329
1330 if (srs == 0) {
1331 /*
1332 * If no shadow set is selected then use the default handler
1333 * that does normal register saving and a standard interrupt exit
1334 */
1335
1336 extern char except_vec_vi, except_vec_vi_lui;
1337 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001338 extern char rollback_except_vec_vi;
1339 char *vec_start = (cpu_wait == r4k_wait) ?
1340 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001341#ifdef CONFIG_MIPS_MT_SMTC
1342 /*
1343 * We need to provide the SMTC vectored interrupt handler
1344 * not only with the address of the handler, but with the
1345 * Status.IM bit to be masked before going there.
1346 */
1347 extern char except_vec_vi_mori;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001348 const int mori_offset = &except_vec_vi_mori - vec_start;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001349#endif /* CONFIG_MIPS_MT_SMTC */
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001350 const int handler_len = &except_vec_vi_end - vec_start;
1351 const int lui_offset = &except_vec_vi_lui - vec_start;
1352 const int ori_offset = &except_vec_vi_ori - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001353
1354 if (handler_len > VECTORSPACING) {
1355 /*
1356 * Sigh... panicing won't help as the console
1357 * is probably not configured :(
1358 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001359 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001360 }
1361
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001362 memcpy(b, vec_start, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001363#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001364 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1365
Ralf Baechle41c594a2006-04-05 09:45:45 +01001366 w = (u32 *)(b + mori_offset);
1367 *w = (*w & 0xffff0000) | (0x100 << n);
1368#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001369 w = (u32 *)(b + lui_offset);
1370 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1371 w = (u32 *)(b + ori_offset);
1372 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001373 local_flush_icache_range((unsigned long)b,
1374 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001375 }
1376 else {
1377 /*
1378 * In other cases jump directly to the interrupt handler
1379 *
1380 * It is the handlers responsibility to save registers if required
1381 * (eg hi/lo) and return from the exception using "eret"
1382 */
1383 w = (u32 *)b;
1384 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1385 *w = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001386 local_flush_icache_range((unsigned long)b,
1387 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001388 }
1389
1390 return (void *)old_handler;
1391}
1392
Ralf Baechleef300e42007-05-06 18:31:18 +01001393void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001394{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001395 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001396}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398extern void cpu_cache_init(void);
1399extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001400extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Ralf Baechle42f77542007-10-18 17:48:11 +01001402/*
1403 * Timer interrupt
1404 */
1405int cp0_compare_irq;
David VomLehn010c1082009-12-21 17:49:22 -08001406int cp0_compare_irq_shift;
Ralf Baechle42f77542007-10-18 17:48:11 +01001407
1408/*
1409 * Performance counter IRQ or -1 if shared with timer
1410 */
1411int cp0_perfcount_irq;
1412EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1413
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001414static int __cpuinitdata noulri;
1415
1416static int __init ulri_disable(char *s)
1417{
1418 pr_info("Disabling ulri\n");
1419 noulri = 1;
1420
1421 return 1;
1422}
1423__setup("noulri", ulri_disable);
1424
Ralf Baechle234fcd12008-03-08 09:56:28 +00001425void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 unsigned int cpu = smp_processor_id();
1428 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001429#ifdef CONFIG_MIPS_MT_SMTC
1430 int secondaryTC = 0;
1431 int bootTC = (cpu == 0);
1432
1433 /*
1434 * Only do per_cpu_trap_init() for first TC of Each VPE.
1435 * Note that this hack assumes that the SMTC init code
1436 * assigns TCs consecutively and in ascending order.
1437 */
1438
1439 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1440 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1441 secondaryTC = 1;
1442#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /*
1445 * Disable coprocessors and select 32-bit or 64-bit addressing
1446 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1447 * flag that some firmware may have left set and the TS bit (for
1448 * IP27). Set XX for ISA IV code to work.
1449 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001450#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1452#endif
1453 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1454 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001455 if (cpu_has_dsp)
1456 status_set |= ST0_MX;
1457
Ralf Baechleb38c7392006-02-07 01:20:43 +00001458 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 -07001459 status_set);
1460
Ralf Baechlea3692022007-07-10 17:33:02 +01001461 if (cpu_has_mips_r2) {
David Daneyfbeda192009-05-13 15:59:55 -07001462 unsigned int enable = 0x0000000f | cpu_hwrena_impl_bits;
Ralf Baechlea3692022007-07-10 17:33:02 +01001463
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001464 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001465 enable |= (1 << 29);
1466
1467 write_c0_hwrena(enable);
1468 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001469
Ralf Baechle41c594a2006-04-05 09:45:45 +01001470#ifdef CONFIG_MIPS_MT_SMTC
1471 if (!secondaryTC) {
1472#endif /* CONFIG_MIPS_MT_SMTC */
1473
Ralf Baechlee01402b2005-07-14 15:57:16 +00001474 if (cpu_has_veic || cpu_has_vint) {
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001475 unsigned long sr = set_c0_status(ST0_BEV);
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001476 write_c0_ebase(ebase);
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001477 write_c0_status(sr);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001478 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001479 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001480 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001481 if (cpu_has_divec) {
1482 if (cpu_has_mipsmt) {
1483 unsigned int vpflags = dvpe();
1484 set_c0_cause(CAUSEF_IV);
1485 evpe(vpflags);
1486 } else
1487 set_c0_cause(CAUSEF_IV);
1488 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001489
1490 /*
1491 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1492 *
1493 * o read IntCtl.IPTI to determine the timer interrupt
1494 * o read IntCtl.IPPCI to determine the performance counter interrupt
1495 */
1496 if (cpu_has_mips_r2) {
David VomLehn010c1082009-12-21 17:49:22 -08001497 cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
1498 cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
1499 cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001500 if (cp0_perfcount_irq == cp0_compare_irq)
1501 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001502 } else {
1503 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Wu Zhangjinf4fc5802010-02-01 17:10:55 +08001504 cp0_compare_irq_shift = cp0_compare_irq;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001505 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001506 }
1507
Ralf Baechle41c594a2006-04-05 09:45:45 +01001508#ifdef CONFIG_MIPS_MT_SMTC
1509 }
1510#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1513 TLBMISS_HANDLER_SETUP();
1514
1515 atomic_inc(&init_mm.mm_count);
1516 current->active_mm = &init_mm;
1517 BUG_ON(current->mm);
1518 enter_lazy_tlb(&init_mm, current);
1519
Ralf Baechle41c594a2006-04-05 09:45:45 +01001520#ifdef CONFIG_MIPS_MT_SMTC
1521 if (bootTC) {
1522#endif /* CONFIG_MIPS_MT_SMTC */
1523 cpu_cache_init();
1524 tlb_init();
1525#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001526 } else if (!secondaryTC) {
1527 /*
1528 * First TC in non-boot VPE must do subset of tlb_init()
1529 * for MMU countrol registers.
1530 */
1531 write_c0_pagemask(PM_DEFAULT_MASK);
1532 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001533 }
1534#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Ralf Baechlee01402b2005-07-14 15:57:16 +00001537/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001538void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001539{
1540 memcpy((void *)(ebase + offset), addr, size);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001541 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001542}
1543
Ralf Baechle234fcd12008-03-08 09:56:28 +00001544static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001545 "Trying to set NULL cache error exception handler";
1546
Ralf Baechle42fe7ee2009-01-28 18:48:23 +00001547/*
1548 * Install uncached CPU exception handler.
1549 * This is suitable only for the cache error exception which is the only
1550 * exception handler that is being run uncached.
1551 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001552void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1553 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001554{
1555#ifdef CONFIG_32BIT
1556 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1557#endif
1558#ifdef CONFIG_64BIT
1559 unsigned long uncached_ebase = TO_UNCAC(ebase);
1560#endif
1561
Ralf Baechle641e97f2007-10-11 23:46:05 +01001562 if (!addr)
1563 panic(panic_null_cerr);
1564
Ralf Baechlee01402b2005-07-14 15:57:16 +00001565 memcpy((void *)(uncached_ebase + offset), addr, size);
1566}
1567
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001568static int __initdata rdhwr_noopt;
1569static int __init set_rdhwr_noopt(char *str)
1570{
1571 rdhwr_noopt = 1;
1572 return 1;
1573}
1574
1575__setup("rdhwr_noopt", set_rdhwr_noopt);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577void __init trap_init(void)
1578{
1579 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 extern char except_vec4;
1581 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001582 int rollback;
1583
1584 check_wait();
1585 rollback = (cpu_wait == r4k_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jason Wessel88547002008-07-29 15:58:53 -05001587#if defined(CONFIG_KGDB)
1588 if (kgdb_early_setup)
1589 return; /* Already done */
1590#endif
1591
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001592 if (cpu_has_veic || cpu_has_vint) {
1593 unsigned long size = 0x200 + VECTORSPACING*64;
1594 ebase = (unsigned long)
1595 __alloc_bootmem(size, 1 << fls(size), 0);
1596 } else {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001597 ebase = CAC_BASE;
David Daney566f74f2008-10-23 17:56:35 -07001598 if (cpu_has_mips_r2)
1599 ebase += (read_c0_ebase() & 0x3ffff000);
1600 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 per_cpu_trap_init();
1603
1604 /*
1605 * Copy the generic exception handlers to their final destination.
1606 * This will be overriden later as suitable for a particular
1607 * configuration.
1608 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001609 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /*
1612 * Setup default vectors
1613 */
1614 for (i = 0; i <= 31; i++)
1615 set_except_vector(i, handle_reserved);
1616
1617 /*
1618 * Copy the EJTAG debug exception vector handler code to it's final
1619 * destination.
1620 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001621 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001622 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /*
1625 * Only some CPUs have the watch exceptions.
1626 */
1627 if (cpu_has_watch)
1628 set_except_vector(23, handle_watch);
1629
1630 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001631 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001633 if (cpu_has_veic || cpu_has_vint) {
1634 int nvec = cpu_has_veic ? 64 : 8;
1635 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001636 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001637 }
1638 else if (cpu_has_divec)
1639 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /*
1642 * Some CPUs can enable/disable for cache parity detection, but does
1643 * it different ways.
1644 */
1645 parity_protection_init();
1646
1647 /*
1648 * The Data Bus Errors / Instruction Bus Errors are signaled
1649 * by external hardware. Therefore these two exceptions
1650 * may have board specific handlers.
1651 */
1652 if (board_be_init)
1653 board_be_init();
1654
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001655 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 set_except_vector(1, handle_tlbm);
1657 set_except_vector(2, handle_tlbl);
1658 set_except_vector(3, handle_tlbs);
1659
1660 set_except_vector(4, handle_adel);
1661 set_except_vector(5, handle_ades);
1662
1663 set_except_vector(6, handle_ibe);
1664 set_except_vector(7, handle_dbe);
1665
1666 set_except_vector(8, handle_sys);
1667 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001668 set_except_vector(10, rdhwr_noopt ? handle_ri :
1669 (cpu_has_vtag_icache ?
1670 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 set_except_vector(11, handle_cpu);
1672 set_except_vector(12, handle_ov);
1673 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Ralf Baechle10cc3522007-10-11 23:46:15 +01001675 if (current_cpu_type() == CPU_R6000 ||
1676 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * The R6000 is the only R-series CPU that features a machine
1679 * check exception (similar to the R4000 cache error) and
1680 * unaligned ldc1/sdc1 exception. The handlers have not been
1681 * written yet. Well, anyway there is no R6000 machine on the
1682 * current list of targets for Linux/MIPS.
1683 * (Duh, crap, there is someone with a triple R6k machine)
1684 */
1685 //set_except_vector(14, handle_mc);
1686 //set_except_vector(15, handle_ndc);
1687 }
1688
Ralf Baechlee01402b2005-07-14 15:57:16 +00001689
1690 if (board_nmi_handler_setup)
1691 board_nmi_handler_setup();
1692
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001693 if (cpu_has_fpu && !cpu_has_nofpuex)
1694 set_except_vector(15, handle_fpe);
1695
1696 set_except_vector(22, handle_mdmx);
1697
1698 if (cpu_has_mcheck)
1699 set_except_vector(24, handle_mcheck);
1700
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001701 if (cpu_has_mipsmt)
1702 set_except_vector(25, handle_mt);
1703
Chris Dearmanacaec422007-05-24 22:30:18 +01001704 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001705
1706 if (cpu_has_vce)
1707 /* Special exception: R4[04]00 uses also the divec space. */
David Daney566f74f2008-10-23 17:56:35 -07001708 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001709 else if (cpu_has_4kex)
David Daney566f74f2008-10-23 17:56:35 -07001710 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001711 else
David Daney566f74f2008-10-23 17:56:35 -07001712 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001713
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001714 local_flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001715 flush_tlb_handlers();
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02001716
1717 sort_extable(__start___dbe_table, __stop___dbe_table);
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001718
1719 register_cu2_notifier(&default_cu2_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}