blob: 8e9fbe75894e5f5199618264fcddde6410d42dc7 [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>
David Daneyc1bf2072010-08-03 11:22:20 -070028#include <linux/kprobes.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000029#include <linux/notifier.h>
Jason Wessel5dd11d52010-05-20 21:04:26 -050030#include <linux/kdb.h>
David Howellsca4d3e672010-10-07 14:08:54 +010031#include <linux/irq.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080032#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <asm/bootinfo.h>
35#include <asm/branch.h>
36#include <asm/break.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000037#include <asm/cop2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/cpu.h>
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000039#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/fpu.h>
Ralf Baechleba3049e2008-10-28 17:38:42 +000041#include <asm/fpu_emulator.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000042#include <asm/mipsregs.h>
43#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/module.h>
45#include <asm/pgtable.h>
46#include <asm/ptrace.h>
47#include <asm/sections.h>
48#include <asm/system.h>
49#include <asm/tlbdebug.h>
50#include <asm/traps.h>
51#include <asm/uaccess.h>
David Daneyb67b2b72008-09-23 00:08:45 -070052#include <asm/watch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090055#include <asm/stacktrace.h>
Florian Fainelli92bbe1b2010-01-28 15:22:37 +010056#include <asm/uasm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090058extern void check_wait(void);
59extern asmlinkage void r4k_wait(void);
60extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010061extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062extern asmlinkage void handle_tlbm(void);
63extern asmlinkage void handle_tlbl(void);
64extern asmlinkage void handle_tlbs(void);
65extern asmlinkage void handle_adel(void);
66extern asmlinkage void handle_ades(void);
67extern asmlinkage void handle_ibe(void);
68extern asmlinkage void handle_dbe(void);
69extern asmlinkage void handle_sys(void);
70extern asmlinkage void handle_bp(void);
71extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090072extern asmlinkage void handle_ri_rdhwr_vivt(void);
73extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074extern asmlinkage void handle_cpu(void);
75extern asmlinkage void handle_ov(void);
76extern asmlinkage void handle_tr(void);
77extern asmlinkage void handle_fpe(void);
78extern asmlinkage void handle_mdmx(void);
79extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000080extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000081extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082extern asmlinkage void handle_mcheck(void);
83extern asmlinkage void handle_reserved(void);
84
Ralf Baechle12616ed2005-10-18 10:26:46 +010085extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090086 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88void (*board_be_init)(void);
89int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000090void (*board_nmi_handler_setup)(void);
91void (*board_ejtag_handler_setup)(void);
92void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020095static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090096{
Ralf Baechle39b8d522008-04-28 17:14:26 +010097 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090098 unsigned long addr;
99
100 printk("Call Trace:");
101#ifdef CONFIG_KALLSYMS
102 printk("\n");
103#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200104 while (!kstack_end(sp)) {
105 unsigned long __user *p =
106 (unsigned long __user *)(unsigned long)sp++;
107 if (__get_user(addr, p)) {
108 printk(" (Bad stack address)");
109 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100110 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200111 if (__kernel_text_address(addr))
112 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900113 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200114 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900115}
116
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900117#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900118int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900119static int __init set_raw_show_trace(char *str)
120{
121 raw_show_trace = 1;
122 return 1;
123}
124__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900125#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200126
Ralf Baechleeae23f22007-10-14 23:27:21 +0100127static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900128{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200129 unsigned long sp = regs->regs[29];
130 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900131 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900132
133 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200134 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900135 return;
136 }
137 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200138 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200139 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900140 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200141 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900142 printk("\n");
143}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * This routine abuses get_user()/put_user() to reference pointers
147 * with at least a bit of error checking ...
148 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100149static void show_stacktrace(struct task_struct *task,
150 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 const int field = 2 * sizeof(unsigned long);
153 long stackdata;
154 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900155 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 printk("Stack :");
158 i = 0;
159 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
160 if (i && ((i % (64 / field)) == 0))
161 printk("\n ");
162 if (i > 39) {
163 printk(" ...");
164 break;
165 }
166
167 if (__get_user(stackdata, sp++)) {
168 printk(" (Bad stack address)");
169 break;
170 }
171
172 printk(" %0*lx", field, stackdata);
173 i++;
174 }
175 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200176 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900177}
178
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900179void show_stack(struct task_struct *task, unsigned long *sp)
180{
181 struct pt_regs regs;
182 if (sp) {
183 regs.regs[29] = (unsigned long)sp;
184 regs.regs[31] = 0;
185 regs.cp0_epc = 0;
186 } else {
187 if (task && task != current) {
188 regs.regs[29] = task->thread.reg29;
189 regs.regs[31] = 0;
190 regs.cp0_epc = task->thread.reg31;
Jason Wessel5dd11d52010-05-20 21:04:26 -0500191#ifdef CONFIG_KGDB_KDB
192 } else if (atomic_read(&kgdb_active) != -1 &&
193 kdb_current_regs) {
194 memcpy(&regs, kdb_current_regs, sizeof(regs));
195#endif /* CONFIG_KGDB_KDB */
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900196 } else {
197 prepare_frametrace(&regs);
198 }
199 }
200 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * The architecture-independent dump_stack generator
205 */
206void dump_stack(void)
207{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200208 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200210 prepare_frametrace(&regs);
211 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214EXPORT_SYMBOL(dump_stack);
215
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900216static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100219 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 printk("\nCode:");
222
Ralf Baechle39b8d522008-04-28 17:14:26 +0100223 if ((unsigned long)pc & 1)
224 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 for(i = -3 ; i < 6 ; i++) {
226 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100227 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 printk(" (Bad address in epc)\n");
229 break;
230 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100231 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233}
234
Ralf Baechleeae23f22007-10-14 23:27:21 +0100235static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 const int field = 2 * sizeof(unsigned long);
238 unsigned int cause = regs->cp0_cause;
239 int i;
240
241 printk("Cpu %d\n", smp_processor_id());
242
243 /*
244 * Saved main processor registers
245 */
246 for (i = 0; i < 32; ) {
247 if ((i % 4) == 0)
248 printk("$%2d :", i);
249 if (i == 0)
250 printk(" %0*lx", field, 0UL);
251 else if (i == 26 || i == 27)
252 printk(" %*s", field, "");
253 else
254 printk(" %0*lx", field, regs->regs[i]);
255
256 i++;
257 if ((i % 4) == 0)
258 printk("\n");
259 }
260
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100261#ifdef CONFIG_CPU_HAS_SMARTMIPS
262 printk("Acx : %0*lx\n", field, regs->acx);
263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 printk("Hi : %0*lx\n", field, regs->hi);
265 printk("Lo : %0*lx\n", field, regs->lo);
266
267 /*
268 * Saved cp0 registers
269 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100270 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
271 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100273 printk("ra : %0*lx %pS\n", field, regs->regs[31],
274 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 printk("Status: %08x ", (uint32_t) regs->cp0_status);
277
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000278 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
279 if (regs->cp0_status & ST0_KUO)
280 printk("KUo ");
281 if (regs->cp0_status & ST0_IEO)
282 printk("IEo ");
283 if (regs->cp0_status & ST0_KUP)
284 printk("KUp ");
285 if (regs->cp0_status & ST0_IEP)
286 printk("IEp ");
287 if (regs->cp0_status & ST0_KUC)
288 printk("KUc ");
289 if (regs->cp0_status & ST0_IEC)
290 printk("IEc ");
291 } else {
292 if (regs->cp0_status & ST0_KX)
293 printk("KX ");
294 if (regs->cp0_status & ST0_SX)
295 printk("SX ");
296 if (regs->cp0_status & ST0_UX)
297 printk("UX ");
298 switch (regs->cp0_status & ST0_KSU) {
299 case KSU_USER:
300 printk("USER ");
301 break;
302 case KSU_SUPERVISOR:
303 printk("SUPERVISOR ");
304 break;
305 case KSU_KERNEL:
306 printk("KERNEL ");
307 break;
308 default:
309 printk("BAD_MODE ");
310 break;
311 }
312 if (regs->cp0_status & ST0_ERL)
313 printk("ERL ");
314 if (regs->cp0_status & ST0_EXL)
315 printk("EXL ");
316 if (regs->cp0_status & ST0_IE)
317 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 printk("\n");
320
321 printk("Cause : %08x\n", cause);
322
323 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
324 if (1 <= cause && cause <= 5)
325 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
326
Ralf Baechle9966db252007-10-11 23:46:17 +0100327 printk("PrId : %08x (%s)\n", read_c0_prid(),
328 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Ralf Baechleeae23f22007-10-14 23:27:21 +0100331/*
332 * FIXME: really the generic show_regs should take a const pointer argument.
333 */
334void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100336 __show_regs((struct pt_regs *)regs);
337}
338
David Daneyc1bf2072010-08-03 11:22:20 -0700339void show_registers(struct pt_regs *regs)
Ralf Baechleeae23f22007-10-14 23:27:21 +0100340{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100341 const int field = 2 * sizeof(unsigned long);
342
Ralf Baechleeae23f22007-10-14 23:27:21 +0100343 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100345 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
346 current->comm, current->pid, current_thread_info(), current,
347 field, current_thread_info()->tp_value);
348 if (cpu_has_userlocal) {
349 unsigned long tls;
350
351 tls = read_c0_userlocal();
352 if (tls != current_thread_info()->tp_value)
353 printk("*HwTLS: %0*lx\n", field, tls);
354 }
355
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900356 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900357 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printk("\n");
359}
360
David Daney70dc6f02010-08-03 15:44:43 -0700361static int regs_to_trapnr(struct pt_regs *regs)
362{
363 return (regs->cp0_cause >> 2) & 0x1f;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static DEFINE_SPINLOCK(die_lock);
367
David Daney70dc6f02010-08-03 15:44:43 -0700368void __noreturn die(const char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 static int die_counter;
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400371 int sig = SIGSEGV;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100372#ifdef CONFIG_MIPS_MT_SMTC
373 unsigned long dvpret = dvpe();
374#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Daney70dc6f02010-08-03 15:44:43 -0700376 notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs), SIGSEGV);
Jason Wessel5dd11d52010-05-20 21:04:26 -0500377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 console_verbose();
379 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100380 bust_spinlocks(1);
381#ifdef CONFIG_MIPS_MT_SMTC
382 mips_mt_regdump(dvpret);
383#endif /* CONFIG_MIPS_MT_SMTC */
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400384
David Daney70dc6f02010-08-03 15:44:43 -0700385 if (notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs), SIGSEGV) == NOTIFY_STOP)
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400386 sig = 0;
387
Ralf Baechle178086c2005-10-13 17:07:54 +0100388 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700390 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200392
393 if (in_interrupt())
394 panic("Fatal exception in interrupt");
395
396 if (panic_on_oops) {
397 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
398 ssleep(5);
399 panic("Fatal exception");
400 }
401
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400402 do_exit(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200405extern struct exception_table_entry __start___dbe_table[];
406extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000408__asm__(
409" .section __dbe_table, \"a\"\n"
410" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/* Given an address, look for it in the exception tables. */
413static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
414{
415 const struct exception_table_entry *e;
416
417 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
418 if (!e)
419 e = search_module_dbetables(addr);
420 return e;
421}
422
423asmlinkage void do_be(struct pt_regs *regs)
424{
425 const int field = 2 * sizeof(unsigned long);
426 const struct exception_table_entry *fixup = NULL;
427 int data = regs->cp0_cause & 4;
428 int action = MIPS_BE_FATAL;
429
430 /* XXX For now. Fixme, this searches the wrong table ... */
431 if (data && !user_mode(regs))
432 fixup = search_dbe_tables(exception_epc(regs));
433
434 if (fixup)
435 action = MIPS_BE_FIXUP;
436
437 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900438 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 switch (action) {
441 case MIPS_BE_DISCARD:
442 return;
443 case MIPS_BE_FIXUP:
444 if (fixup) {
445 regs->cp0_epc = fixup->nextinsn;
446 return;
447 }
448 break;
449 default:
450 break;
451 }
452
453 /*
454 * Assume it would be too dangerous to continue ...
455 */
456 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
457 data ? "Data" : "Instruction",
458 field, regs->cp0_epc, field, regs->regs[31]);
David Daney70dc6f02010-08-03 15:44:43 -0700459 if (notify_die(DIE_OOPS, "bus error", regs, 0, regs_to_trapnr(regs), SIGBUS)
Jason Wessel88547002008-07-29 15:58:53 -0500460 == NOTIFY_STOP)
461 return;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 die_if_kernel("Oops", regs);
464 force_sig(SIGBUS, current);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100468 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470
471#define OPCODE 0xfc000000
472#define BASE 0x03e00000
473#define RT 0x001f0000
474#define OFFSET 0x0000ffff
475#define LL 0xc0000000
476#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100477#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000478#define SPEC3 0x7c000000
479#define RD 0x0000f800
480#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100481#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000482#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484/*
485 * The ll_bit is cleared by r*_switch.S
486 */
487
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200488unsigned int ll_bit;
489struct task_struct *ll_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100491static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000493 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /*
497 * analyse the ll instruction that just caused a ri exception
498 * and put the referenced address to addr.
499 */
500
501 /* sign extend offset */
502 offset = opcode & OFFSET;
503 offset <<= 16;
504 offset >>= 16;
505
Ralf Baechlefe00f942005-03-01 19:22:29 +0000506 vaddr = (unsigned long __user *)
507 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100509 if ((unsigned long)vaddr & 3)
510 return SIGBUS;
511 if (get_user(value, vaddr))
512 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 preempt_disable();
515
516 if (ll_task == NULL || ll_task == current) {
517 ll_bit = 1;
518 } else {
519 ll_bit = 0;
520 }
521 ll_task = current;
522
523 preempt_enable();
524
525 regs->regs[(opcode & RT) >> 16] = value;
526
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100530static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000532 unsigned long __user *vaddr;
533 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /*
537 * analyse the sc instruction that just caused a ri exception
538 * and put the referenced address to addr.
539 */
540
541 /* sign extend offset */
542 offset = opcode & OFFSET;
543 offset <<= 16;
544 offset >>= 16;
545
Ralf Baechlefe00f942005-03-01 19:22:29 +0000546 vaddr = (unsigned long __user *)
547 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 reg = (opcode & RT) >> 16;
549
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100550 if ((unsigned long)vaddr & 3)
551 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 preempt_disable();
554
555 if (ll_bit == 0 || ll_task != current) {
556 regs->regs[reg] = 0;
557 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 preempt_enable();
562
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100563 if (put_user(regs->regs[reg], vaddr))
564 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 regs->regs[reg] = 1;
567
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
573 * opcodes are supposed to result in coprocessor unusable exceptions if
574 * executed on ll/sc-less processors. That's the theory. In practice a
575 * few processors such as NEC's VR4100 throw reserved instruction exceptions
576 * instead, so we're doing the emulation thing in both exception handlers.
577 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100578static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800580 if ((opcode & OPCODE) == LL) {
581 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
582 1, 0, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100583 return simulate_ll(regs, opcode);
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800584 }
585 if ((opcode & OPCODE) == SC) {
586 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
587 1, 0, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100588 return simulate_sc(regs, opcode);
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100591 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Ralf Baechle3c370262005-04-13 17:43:59 +0000594/*
595 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100596 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000597 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100598static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000599{
Al Virodc8f6022006-01-12 01:06:07 -0800600 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000601
602 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
603 int rd = (opcode & RD) >> 11;
604 int rt = (opcode & RT) >> 16;
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800605 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
606 1, 0, regs, 0);
Ralf Baechle3c370262005-04-13 17:43:59 +0000607 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100608 case 0: /* CPU number */
609 regs->regs[rt] = smp_processor_id();
610 return 0;
611 case 1: /* SYNCI length */
612 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
613 current_cpu_data.icache.linesz);
614 return 0;
615 case 2: /* Read count register */
616 regs->regs[rt] = read_c0_count();
617 return 0;
618 case 3: /* Count register resolution */
619 switch (current_cpu_data.cputype) {
620 case CPU_20KC:
621 case CPU_25KF:
622 regs->regs[rt] = 1;
623 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000624 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100625 regs->regs[rt] = 2;
626 }
627 return 0;
628 case 29:
629 regs->regs[rt] = ti->tp_value;
630 return 0;
631 default:
632 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000633 }
634 }
635
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500636 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100637 return -1;
638}
Ralf Baechlee5679882006-11-30 01:14:47 +0000639
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100640static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
641{
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800642 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
643 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
644 1, 0, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100645 return 0;
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800646 }
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100647
648 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651asmlinkage void do_ov(struct pt_regs *regs)
652{
653 siginfo_t info;
654
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000655 die_if_kernel("Integer overflow", regs);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 info.si_code = FPE_INTOVF;
658 info.si_signo = SIGFPE;
659 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000660 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 force_sig_info(SIGFPE, &info, current);
662}
663
664/*
665 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
666 */
667asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
668{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100669 siginfo_t info;
670
David Daney70dc6f02010-08-03 15:44:43 -0700671 if (notify_die(DIE_FP, "FP exception", regs, 0, regs_to_trapnr(regs), SIGFPE)
Jason Wessel88547002008-07-29 15:58:53 -0500672 == NOTIFY_STOP)
673 return;
Chris Dearman57725f92006-06-30 23:35:28 +0100674 die_if_kernel("FP exception in kernel code", regs);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (fcr31 & FPU_CSR_UNI_X) {
677 int sig;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000680 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * software emulator on-board, let's use it...
682 *
683 * Force FPU to dump state into task/thread context. We're
684 * moving a lot of data here for what is probably a single
685 * instruction, but the alternative is to pre-decode the FP
686 * register operands before invoking the emulator, which seems
687 * a bit extreme for what should be an infrequent event.
688 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000689 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900690 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100693 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /*
696 * We can't allow the emulated instruction to leave any of
697 * the cause bit set in $fcr31.
698 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900699 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900702 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* If something went wrong, signal */
705 if (sig)
706 force_sig(sig, current);
707
708 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100709 } else if (fcr31 & FPU_CSR_INV_X)
710 info.si_code = FPE_FLTINV;
711 else if (fcr31 & FPU_CSR_DIV_X)
712 info.si_code = FPE_FLTDIV;
713 else if (fcr31 & FPU_CSR_OVF_X)
714 info.si_code = FPE_FLTOVF;
715 else if (fcr31 & FPU_CSR_UDF_X)
716 info.si_code = FPE_FLTUND;
717 else if (fcr31 & FPU_CSR_INE_X)
718 info.si_code = FPE_FLTRES;
719 else
720 info.si_code = __SI_FAULT;
721 info.si_signo = SIGFPE;
722 info.si_errno = 0;
723 info.si_addr = (void __user *) regs->cp0_epc;
724 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Ralf Baechledf270052008-04-20 16:28:54 +0100727static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
728 const char *str)
729{
730 siginfo_t info;
731 char b[40];
732
Jason Wessel5dd11d52010-05-20 21:04:26 -0500733#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
David Daney70dc6f02010-08-03 15:44:43 -0700734 if (kgdb_ll_trap(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
Jason Wessel5dd11d52010-05-20 21:04:26 -0500735 return;
736#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
737
David Daney70dc6f02010-08-03 15:44:43 -0700738 if (notify_die(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
Jason Wessel88547002008-07-29 15:58:53 -0500739 return;
740
Ralf Baechledf270052008-04-20 16:28:54 +0100741 /*
742 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
743 * insns, even for trap and break codes that indicate arithmetic
744 * failures. Weird ...
745 * But should we continue the brokenness??? --macro
746 */
747 switch (code) {
748 case BRK_OVERFLOW:
749 case BRK_DIVZERO:
750 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
751 die_if_kernel(b, regs);
752 if (code == BRK_DIVZERO)
753 info.si_code = FPE_INTDIV;
754 else
755 info.si_code = FPE_INTOVF;
756 info.si_signo = SIGFPE;
757 info.si_errno = 0;
758 info.si_addr = (void __user *) regs->cp0_epc;
759 force_sig_info(SIGFPE, &info, current);
760 break;
761 case BRK_BUG:
762 die_if_kernel("Kernel bug detected", regs);
763 force_sig(SIGTRAP, current);
764 break;
Ralf Baechleba3049e2008-10-28 17:38:42 +0000765 case BRK_MEMU:
766 /*
767 * Address errors may be deliberately induced by the FPU
768 * emulator to retake control of the CPU after executing the
769 * instruction in the delay slot of an emulated branch.
770 *
771 * Terminate if exception was recognized as a delay slot return
772 * otherwise handle as normal.
773 */
774 if (do_dsemulret(regs))
775 return;
776
777 die_if_kernel("Math emu break/trap", regs);
778 force_sig(SIGTRAP, current);
779 break;
Ralf Baechledf270052008-04-20 16:28:54 +0100780 default:
781 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
782 die_if_kernel(b, regs);
783 force_sig(SIGTRAP, current);
784 }
785}
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787asmlinkage void do_bp(struct pt_regs *regs)
788{
789 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900791 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000792 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /*
795 * There is the ancient bug in the MIPS assemblers that the break
796 * code starts left to bit 16 instead to bit 6 in the opcode.
797 * Gas is bug-compatible, but not always, grrr...
798 * We handle both cases with a simple heuristics. --macro
799 */
800 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100801 if (bcode >= (1 << 10))
802 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
David Daneyc1bf2072010-08-03 11:22:20 -0700804 /*
805 * notify the kprobe handlers, if instruction is likely to
806 * pertain to them.
807 */
808 switch (bcode) {
809 case BRK_KPROBE_BP:
David Daney70dc6f02010-08-03 15:44:43 -0700810 if (notify_die(DIE_BREAK, "debug", regs, bcode, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
David Daneyc1bf2072010-08-03 11:22:20 -0700811 return;
812 else
813 break;
814 case BRK_KPROBE_SSTEPBP:
David Daney70dc6f02010-08-03 15:44:43 -0700815 if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
David Daneyc1bf2072010-08-03 11:22:20 -0700816 return;
817 else
818 break;
819 default:
820 break;
821 }
822
Ralf Baechledf270052008-04-20 16:28:54 +0100823 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900824 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000825
826out_sigsegv:
827 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830asmlinkage void do_tr(struct pt_regs *regs)
831{
832 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900834 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000835 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* Immediate versions don't provide a code. */
838 if (!(opcode & OPCODE))
839 tcode = ((opcode >> 6) & ((1 << 10) - 1));
840
Ralf Baechledf270052008-04-20 16:28:54 +0100841 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900842 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000843
844out_sigsegv:
845 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848asmlinkage void do_ri(struct pt_regs *regs)
849{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100850 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
851 unsigned long old_epc = regs->cp0_epc;
852 unsigned int opcode = 0;
853 int status = -1;
854
David Daney70dc6f02010-08-03 15:44:43 -0700855 if (notify_die(DIE_RI, "RI Fault", regs, 0, regs_to_trapnr(regs), SIGILL)
Jason Wessel88547002008-07-29 15:58:53 -0500856 == NOTIFY_STOP)
857 return;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 die_if_kernel("Reserved instruction in kernel code", regs);
860
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100861 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000862 return;
863
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100864 if (unlikely(get_user(opcode, epc) < 0))
865 status = SIGSEGV;
866
867 if (!cpu_has_llsc && status < 0)
868 status = simulate_llsc(regs, opcode);
869
870 if (status < 0)
871 status = simulate_rdhwr(regs, opcode);
872
873 if (status < 0)
874 status = simulate_sync(regs, opcode);
875
876 if (status < 0)
877 status = SIGILL;
878
879 if (unlikely(status > 0)) {
880 regs->cp0_epc = old_epc; /* Undo skip-over. */
881 force_sig(status, current);
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Ralf Baechled223a862007-07-10 17:33:02 +0100885/*
886 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
887 * emulated more than some threshold number of instructions, force migration to
888 * a "CPU" that has FP support.
889 */
890static void mt_ase_fp_affinity(void)
891{
892#ifdef CONFIG_MIPS_MT_FPAFF
893 if (mt_fpemul_threshold > 0 &&
894 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
895 /*
896 * If there's no FPU present, or if the application has already
897 * restricted the allowed set to exclude any CPUs with FPUs,
898 * we'll skip the procedure.
899 */
900 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
901 cpumask_t tmask;
902
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200903 current->thread.user_cpus_allowed
904 = current->cpus_allowed;
905 cpus_and(tmask, current->cpus_allowed,
906 mt_fpu_cpumask);
Julia Lawalled1bbde2010-03-26 23:03:07 +0100907 set_cpus_allowed_ptr(current, &tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100908 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100909 }
910 }
911#endif /* CONFIG_MIPS_MT_FPAFF */
912}
913
Ralf Baechle69f3a7d2009-11-24 01:24:58 +0000914/*
915 * No lock; only written during early bootup by CPU 0.
916 */
917static RAW_NOTIFIER_HEAD(cu2_chain);
918
919int __ref register_cu2_notifier(struct notifier_block *nb)
920{
921 return raw_notifier_chain_register(&cu2_chain, nb);
922}
923
924int cu2_notifier_call_chain(unsigned long val, void *v)
925{
926 return raw_notifier_call_chain(&cu2_chain, val, v);
927}
928
929static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
930 void *data)
931{
932 struct pt_regs *regs = data;
933
934 switch (action) {
935 default:
936 die_if_kernel("Unhandled kernel unaligned access or invalid "
937 "instruction", regs);
938 /* Fall through */
939
940 case CU2_EXCEPTION:
941 force_sig(SIGILL, current);
942 }
943
944 return NOTIFY_OK;
945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947asmlinkage void do_cpu(struct pt_regs *regs)
948{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100949 unsigned int __user *epc;
950 unsigned long old_epc;
951 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100953 int status;
David Daneyf9bb4cf2008-12-11 15:33:23 -0800954 unsigned long __maybe_unused flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Atsushi Nemoto53231802007-04-14 02:37:26 +0900956 die_if_kernel("do_cpu invoked from kernel context!", regs);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
959
960 switch (cpid) {
961 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100962 epc = (unsigned int __user *)exception_epc(regs);
963 old_epc = regs->cp0_epc;
964 opcode = 0;
965 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100967 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000969
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100970 if (unlikely(get_user(opcode, epc) < 0))
971 status = SIGSEGV;
972
973 if (!cpu_has_llsc && status < 0)
974 status = simulate_llsc(regs, opcode);
975
976 if (status < 0)
977 status = simulate_rdhwr(regs, opcode);
978
979 if (status < 0)
980 status = SIGILL;
981
982 if (unlikely(status > 0)) {
983 regs->cp0_epc = old_epc; /* Undo skip-over. */
984 force_sig(status, current);
985 }
986
987 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900990 if (used_math()) /* Using the FPU again. */
991 own_fpu(1);
992 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 init_fpu();
994 set_used_math();
995 }
996
Atsushi Nemoto53231802007-04-14 02:37:26 +0900997 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900998 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900999 sig = fpu_emulator_cop1Handler(regs,
1000 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (sig)
1002 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +01001003 else
1004 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
1008
1009 case 2:
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001010 raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
Jesper Nilsson55dc9d52010-06-17 15:25:54 +02001011 return;
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case 3:
1014 break;
1015 }
1016
1017 force_sig(SIGILL, current);
1018}
1019
1020asmlinkage void do_mdmx(struct pt_regs *regs)
1021{
1022 force_sig(SIGILL, current);
1023}
1024
David Daney8bc6d052009-01-05 15:29:58 -08001025/*
1026 * Called with interrupts disabled.
1027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028asmlinkage void do_watch(struct pt_regs *regs)
1029{
David Daneyb67b2b72008-09-23 00:08:45 -07001030 u32 cause;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /*
David Daneyb67b2b72008-09-23 00:08:45 -07001033 * Clear WP (bit 22) bit of cause register so we don't loop
1034 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
David Daneyb67b2b72008-09-23 00:08:45 -07001036 cause = read_c0_cause();
1037 cause &= ~(1 << 22);
1038 write_c0_cause(cause);
1039
1040 /*
1041 * If the current thread has the watch registers loaded, save
1042 * their values and send SIGTRAP. Otherwise another thread
1043 * left the registers set, clear them and continue.
1044 */
1045 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
1046 mips_read_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -08001047 local_irq_enable();
David Daneyb67b2b72008-09-23 00:08:45 -07001048 force_sig(SIGTRAP, current);
David Daney8bc6d052009-01-05 15:29:58 -08001049 } else {
David Daneyb67b2b72008-09-23 00:08:45 -07001050 mips_clear_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -08001051 local_irq_enable();
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055asmlinkage void do_mcheck(struct pt_regs *regs)
1056{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001057 const int field = 2 * sizeof(unsigned long);
1058 int multi_match = regs->cp0_status & ST0_TS;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001061
1062 if (multi_match) {
1063 printk("Index : %0x\n", read_c0_index());
1064 printk("Pagemask: %0x\n", read_c0_pagemask());
1065 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
1066 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
1067 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
1068 printk("\n");
1069 dump_tlb_all();
1070 }
1071
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +09001072 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Some chips may have other causes of machine check (e.g. SB1
1076 * graduation timer)
1077 */
1078 panic("Caught Machine Check exception - %scaused by multiple "
1079 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001080 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001083asmlinkage void do_mt(struct pt_regs *regs)
1084{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001085 int subcode;
1086
Ralf Baechle41c594a2006-04-05 09:45:45 +01001087 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1088 >> VPECONTROL_EXCPT_SHIFT;
1089 switch (subcode) {
1090 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001091 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001092 break;
1093 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001094 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001095 break;
1096 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001097 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001098 break;
1099 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001100 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001101 break;
1102 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001103 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001104 break;
1105 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001106 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001107 break;
1108 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001109 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +01001110 subcode);
1111 break;
1112 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001113 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1114
1115 force_sig(SIGILL, current);
1116}
1117
1118
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001119asmlinkage void do_dsp(struct pt_regs *regs)
1120{
1121 if (cpu_has_dsp)
1122 panic("Unexpected DSP exception\n");
1123
1124 force_sig(SIGILL, current);
1125}
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127asmlinkage void do_reserved(struct pt_regs *regs)
1128{
1129 /*
1130 * Game over - no way to handle this if it ever occurs. Most probably
1131 * caused by a new unknown cpu type or after another deadly
1132 * hard/software error.
1133 */
1134 show_regs(regs);
1135 panic("Caught reserved exception %ld - should not happen.",
1136 (regs->cp0_cause & 0x7f) >> 2);
1137}
1138
Ralf Baechle39b8d522008-04-28 17:14:26 +01001139static int __initdata l1parity = 1;
1140static int __init nol1parity(char *s)
1141{
1142 l1parity = 0;
1143 return 1;
1144}
1145__setup("nol1par", nol1parity);
1146static int __initdata l2parity = 1;
1147static int __init nol2parity(char *s)
1148{
1149 l2parity = 0;
1150 return 1;
1151}
1152__setup("nol2par", nol2parity);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/*
1155 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1156 * it different ways.
1157 */
1158static inline void parity_protection_init(void)
1159{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001160 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001162 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001163 case CPU_74K:
1164 case CPU_1004K:
1165 {
1166#define ERRCTL_PE 0x80000000
1167#define ERRCTL_L2P 0x00800000
1168 unsigned long errctl;
1169 unsigned int l1parity_present, l2parity_present;
1170
1171 errctl = read_c0_ecc();
1172 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1173
1174 /* probe L1 parity support */
1175 write_c0_ecc(errctl | ERRCTL_PE);
1176 back_to_back_c0_hazard();
1177 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1178
1179 /* probe L2 parity support */
1180 write_c0_ecc(errctl|ERRCTL_L2P);
1181 back_to_back_c0_hazard();
1182 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1183
1184 if (l1parity_present && l2parity_present) {
1185 if (l1parity)
1186 errctl |= ERRCTL_PE;
1187 if (l1parity ^ l2parity)
1188 errctl |= ERRCTL_L2P;
1189 } else if (l1parity_present) {
1190 if (l1parity)
1191 errctl |= ERRCTL_PE;
1192 } else if (l2parity_present) {
1193 if (l2parity)
1194 errctl |= ERRCTL_L2P;
1195 } else {
1196 /* No parity available */
1197 }
1198
1199 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1200
1201 write_c0_ecc(errctl);
1202 back_to_back_c0_hazard();
1203 errctl = read_c0_ecc();
1204 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1205
1206 if (l1parity_present)
1207 printk(KERN_INFO "Cache parity protection %sabled\n",
1208 (errctl & ERRCTL_PE) ? "en" : "dis");
1209
1210 if (l2parity_present) {
1211 if (l1parity_present && l1parity)
1212 errctl ^= ERRCTL_L2P;
1213 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1214 (errctl & ERRCTL_L2P) ? "en" : "dis");
1215 }
1216 }
1217 break;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001220 write_c0_ecc(0x80000000);
1221 back_to_back_c0_hazard();
1222 /* Set the PE bit (bit 31) in the c0_errctl register. */
1223 printk(KERN_INFO "Cache parity protection %sabled\n",
1224 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 case CPU_20KC:
1227 case CPU_25KF:
1228 /* Clear the DE bit (bit 16) in the c0_status register. */
1229 printk(KERN_INFO "Enable cache parity protection for "
1230 "MIPS 20KC/25KF CPUs.\n");
1231 clear_c0_status(ST0_DE);
1232 break;
1233 default:
1234 break;
1235 }
1236}
1237
1238asmlinkage void cache_parity_error(void)
1239{
1240 const int field = 2 * sizeof(unsigned long);
1241 unsigned int reg_val;
1242
1243 /* For the moment, report the problem and hang. */
1244 printk("Cache error exception:\n");
1245 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1246 reg_val = read_c0_cacheerr();
1247 printk("c0_cacheerr == %08x\n", reg_val);
1248
1249 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1250 reg_val & (1<<30) ? "secondary" : "primary",
1251 reg_val & (1<<31) ? "data" : "insn");
1252 printk("Error bits: %s%s%s%s%s%s%s\n",
1253 reg_val & (1<<29) ? "ED " : "",
1254 reg_val & (1<<28) ? "ET " : "",
1255 reg_val & (1<<26) ? "EE " : "",
1256 reg_val & (1<<25) ? "EB " : "",
1257 reg_val & (1<<24) ? "EI " : "",
1258 reg_val & (1<<23) ? "E1 " : "",
1259 reg_val & (1<<22) ? "E0 " : "");
1260 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1261
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001262#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (reg_val & (1<<22))
1264 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1265
1266 if (reg_val & (1<<23))
1267 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1268#endif
1269
1270 panic("Can't handle the cache error!");
1271}
1272
1273/*
1274 * SDBBP EJTAG debug exception handler.
1275 * We skip the instruction and return to the next instruction.
1276 */
1277void ejtag_exception_handler(struct pt_regs *regs)
1278{
1279 const int field = 2 * sizeof(unsigned long);
1280 unsigned long depc, old_epc;
1281 unsigned int debug;
1282
Chris Dearman70ae6122006-06-30 12:32:37 +01001283 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 depc = read_c0_depc();
1285 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001286 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (debug & 0x80000000) {
1288 /*
1289 * In branch delay slot.
1290 * We cheat a little bit here and use EPC to calculate the
1291 * debug return address (DEPC). EPC is restored after the
1292 * calculation.
1293 */
1294 old_epc = regs->cp0_epc;
1295 regs->cp0_epc = depc;
1296 __compute_return_epc(regs);
1297 depc = regs->cp0_epc;
1298 regs->cp0_epc = old_epc;
1299 } else
1300 depc += 4;
1301 write_c0_depc(depc);
1302
1303#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001304 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 write_c0_debug(debug | 0x100);
1306#endif
1307}
1308
1309/*
1310 * NMI exception handler.
1311 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001312NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001314 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 printk("NMI taken!!!!\n");
1316 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Ralf Baechlee01402b2005-07-14 15:57:16 +00001319#define VECTORSPACING 0x100 /* for EI/VI mode */
1320
1321unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001323unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Florian Fainelli2d1b6e92010-01-28 15:21:42 +01001325void __init *set_except_vector(int n, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 unsigned long handler = (unsigned long) addr;
1328 unsigned long old_handler = exception_handlers[n];
1329
1330 exception_handlers[n] = handler;
1331 if (n == 0 && cpu_has_divec) {
Florian Fainelli92bbe1b2010-01-28 15:22:37 +01001332 unsigned long jump_mask = ~((1 << 28) - 1);
1333 u32 *buf = (u32 *)(ebase + 0x200);
1334 unsigned int k0 = 26;
1335 if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
1336 uasm_i_j(&buf, handler & ~jump_mask);
1337 uasm_i_nop(&buf);
1338 } else {
1339 UASM_i_LA(&buf, k0, handler);
1340 uasm_i_jr(&buf, k0);
1341 uasm_i_nop(&buf);
1342 }
1343 local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 return (void *)old_handler;
1346}
1347
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001348static asmlinkage void do_default_vi(void)
1349{
1350 show_regs(get_irq_regs());
1351 panic("Caught unexpected vectored interrupt.");
1352}
1353
Ralf Baechleef300e42007-05-06 18:31:18 +01001354static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001355{
1356 unsigned long handler;
1357 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001358 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001359 u32 *w;
1360 unsigned char *b;
1361
Ralf Baechleb72b7092009-03-30 14:49:44 +02001362 BUG_ON(!cpu_has_veic && !cpu_has_vint);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001363
1364 if (addr == NULL) {
1365 handler = (unsigned long) do_default_vi;
1366 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001367 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001368 handler = (unsigned long) addr;
1369 vi_handlers[n] = (unsigned long) addr;
1370
1371 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1372
Ralf Baechlef6771db2007-11-08 18:02:29 +00001373 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001374 panic("Shadow register set %d not supported", srs);
1375
1376 if (cpu_has_veic) {
1377 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001378 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001379 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001380 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001381 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001382 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001383 }
1384
1385 if (srs == 0) {
1386 /*
1387 * If no shadow set is selected then use the default handler
1388 * that does normal register saving and a standard interrupt exit
1389 */
1390
1391 extern char except_vec_vi, except_vec_vi_lui;
1392 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001393 extern char rollback_except_vec_vi;
1394 char *vec_start = (cpu_wait == r4k_wait) ?
1395 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001396#ifdef CONFIG_MIPS_MT_SMTC
1397 /*
1398 * We need to provide the SMTC vectored interrupt handler
1399 * not only with the address of the handler, but with the
1400 * Status.IM bit to be masked before going there.
1401 */
1402 extern char except_vec_vi_mori;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001403 const int mori_offset = &except_vec_vi_mori - vec_start;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001404#endif /* CONFIG_MIPS_MT_SMTC */
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001405 const int handler_len = &except_vec_vi_end - vec_start;
1406 const int lui_offset = &except_vec_vi_lui - vec_start;
1407 const int ori_offset = &except_vec_vi_ori - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001408
1409 if (handler_len > VECTORSPACING) {
1410 /*
1411 * Sigh... panicing won't help as the console
1412 * is probably not configured :(
1413 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001414 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001415 }
1416
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001417 memcpy(b, vec_start, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001418#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001419 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1420
Ralf Baechle41c594a2006-04-05 09:45:45 +01001421 w = (u32 *)(b + mori_offset);
1422 *w = (*w & 0xffff0000) | (0x100 << n);
1423#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001424 w = (u32 *)(b + lui_offset);
1425 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1426 w = (u32 *)(b + ori_offset);
1427 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001428 local_flush_icache_range((unsigned long)b,
1429 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001430 }
1431 else {
1432 /*
1433 * In other cases jump directly to the interrupt handler
1434 *
1435 * It is the handlers responsibility to save registers if required
1436 * (eg hi/lo) and return from the exception using "eret"
1437 */
1438 w = (u32 *)b;
1439 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1440 *w = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001441 local_flush_icache_range((unsigned long)b,
1442 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001443 }
1444
1445 return (void *)old_handler;
1446}
1447
Ralf Baechleef300e42007-05-06 18:31:18 +01001448void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001449{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001450 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001451}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453extern void cpu_cache_init(void);
1454extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001455extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Ralf Baechle42f77542007-10-18 17:48:11 +01001457/*
1458 * Timer interrupt
1459 */
1460int cp0_compare_irq;
David VomLehn010c1082009-12-21 17:49:22 -08001461int cp0_compare_irq_shift;
Ralf Baechle42f77542007-10-18 17:48:11 +01001462
1463/*
1464 * Performance counter IRQ or -1 if shared with timer
1465 */
1466int cp0_perfcount_irq;
1467EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1468
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001469static int __cpuinitdata noulri;
1470
1471static int __init ulri_disable(char *s)
1472{
1473 pr_info("Disabling ulri\n");
1474 noulri = 1;
1475
1476 return 1;
1477}
1478__setup("noulri", ulri_disable);
1479
Ralf Baechle234fcd12008-03-08 09:56:28 +00001480void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 unsigned int cpu = smp_processor_id();
1483 unsigned int status_set = ST0_CU0;
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001484 unsigned int hwrena = cpu_hwrena_impl_bits;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001485#ifdef CONFIG_MIPS_MT_SMTC
1486 int secondaryTC = 0;
1487 int bootTC = (cpu == 0);
1488
1489 /*
1490 * Only do per_cpu_trap_init() for first TC of Each VPE.
1491 * Note that this hack assumes that the SMTC init code
1492 * assigns TCs consecutively and in ascending order.
1493 */
1494
1495 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1496 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1497 secondaryTC = 1;
1498#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /*
1501 * Disable coprocessors and select 32-bit or 64-bit addressing
1502 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1503 * flag that some firmware may have left set and the TS bit (for
1504 * IP27). Set XX for ISA IV code to work.
1505 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001506#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1508#endif
1509 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1510 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001511 if (cpu_has_dsp)
1512 status_set |= ST0_MX;
1513
Ralf Baechleb38c7392006-02-07 01:20:43 +00001514 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 -07001515 status_set);
1516
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001517 if (cpu_has_mips_r2)
1518 hwrena |= 0x0000000f;
Ralf Baechlea3692022007-07-10 17:33:02 +01001519
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001520 if (!noulri && cpu_has_userlocal)
1521 hwrena |= (1 << 29);
Ralf Baechlea3692022007-07-10 17:33:02 +01001522
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001523 if (hwrena)
1524 write_c0_hwrena(hwrena);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001525
Ralf Baechle41c594a2006-04-05 09:45:45 +01001526#ifdef CONFIG_MIPS_MT_SMTC
1527 if (!secondaryTC) {
1528#endif /* CONFIG_MIPS_MT_SMTC */
1529
Ralf Baechlee01402b2005-07-14 15:57:16 +00001530 if (cpu_has_veic || cpu_has_vint) {
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001531 unsigned long sr = set_c0_status(ST0_BEV);
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001532 write_c0_ebase(ebase);
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001533 write_c0_status(sr);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001534 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001535 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001536 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001537 if (cpu_has_divec) {
1538 if (cpu_has_mipsmt) {
1539 unsigned int vpflags = dvpe();
1540 set_c0_cause(CAUSEF_IV);
1541 evpe(vpflags);
1542 } else
1543 set_c0_cause(CAUSEF_IV);
1544 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001545
1546 /*
1547 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1548 *
1549 * o read IntCtl.IPTI to determine the timer interrupt
1550 * o read IntCtl.IPPCI to determine the performance counter interrupt
1551 */
1552 if (cpu_has_mips_r2) {
David VomLehn010c1082009-12-21 17:49:22 -08001553 cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
1554 cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
1555 cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001556 if (cp0_perfcount_irq == cp0_compare_irq)
1557 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001558 } else {
1559 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Wu Zhangjinf4fc5802010-02-01 17:10:55 +08001560 cp0_compare_irq_shift = cp0_compare_irq;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001561 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001562 }
1563
Ralf Baechle41c594a2006-04-05 09:45:45 +01001564#ifdef CONFIG_MIPS_MT_SMTC
1565 }
1566#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1569 TLBMISS_HANDLER_SETUP();
1570
1571 atomic_inc(&init_mm.mm_count);
1572 current->active_mm = &init_mm;
1573 BUG_ON(current->mm);
1574 enter_lazy_tlb(&init_mm, current);
1575
Ralf Baechle41c594a2006-04-05 09:45:45 +01001576#ifdef CONFIG_MIPS_MT_SMTC
1577 if (bootTC) {
1578#endif /* CONFIG_MIPS_MT_SMTC */
1579 cpu_cache_init();
1580 tlb_init();
1581#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001582 } else if (!secondaryTC) {
1583 /*
1584 * First TC in non-boot VPE must do subset of tlb_init()
1585 * for MMU countrol registers.
1586 */
1587 write_c0_pagemask(PM_DEFAULT_MASK);
1588 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001589 }
1590#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Ralf Baechlee01402b2005-07-14 15:57:16 +00001593/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001594void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001595{
1596 memcpy((void *)(ebase + offset), addr, size);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001597 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001598}
1599
Ralf Baechle234fcd12008-03-08 09:56:28 +00001600static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001601 "Trying to set NULL cache error exception handler";
1602
Ralf Baechle42fe7ee2009-01-28 18:48:23 +00001603/*
1604 * Install uncached CPU exception handler.
1605 * This is suitable only for the cache error exception which is the only
1606 * exception handler that is being run uncached.
1607 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001608void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1609 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001610{
Sebastian Andrzej Siewior4f81b012010-04-27 22:53:30 +02001611 unsigned long uncached_ebase = CKSEG1ADDR(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001612
Ralf Baechle641e97f2007-10-11 23:46:05 +01001613 if (!addr)
1614 panic(panic_null_cerr);
1615
Ralf Baechlee01402b2005-07-14 15:57:16 +00001616 memcpy((void *)(uncached_ebase + offset), addr, size);
1617}
1618
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001619static int __initdata rdhwr_noopt;
1620static int __init set_rdhwr_noopt(char *str)
1621{
1622 rdhwr_noopt = 1;
1623 return 1;
1624}
1625
1626__setup("rdhwr_noopt", set_rdhwr_noopt);
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628void __init trap_init(void)
1629{
1630 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 extern char except_vec4;
1632 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001633 int rollback;
1634
1635 check_wait();
1636 rollback = (cpu_wait == r4k_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Jason Wessel88547002008-07-29 15:58:53 -05001638#if defined(CONFIG_KGDB)
1639 if (kgdb_early_setup)
1640 return; /* Already done */
1641#endif
1642
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001643 if (cpu_has_veic || cpu_has_vint) {
1644 unsigned long size = 0x200 + VECTORSPACING*64;
1645 ebase = (unsigned long)
1646 __alloc_bootmem(size, 1 << fls(size), 0);
1647 } else {
David Daneyf6be75d2010-04-06 13:29:50 -07001648 ebase = CKSEG0;
David Daney566f74f2008-10-23 17:56:35 -07001649 if (cpu_has_mips_r2)
1650 ebase += (read_c0_ebase() & 0x3ffff000);
1651 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 per_cpu_trap_init();
1654
1655 /*
1656 * Copy the generic exception handlers to their final destination.
1657 * This will be overriden later as suitable for a particular
1658 * configuration.
1659 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001660 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /*
1663 * Setup default vectors
1664 */
1665 for (i = 0; i <= 31; i++)
1666 set_except_vector(i, handle_reserved);
1667
1668 /*
1669 * Copy the EJTAG debug exception vector handler code to it's final
1670 * destination.
1671 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001672 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001673 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 /*
1676 * Only some CPUs have the watch exceptions.
1677 */
1678 if (cpu_has_watch)
1679 set_except_vector(23, handle_watch);
1680
1681 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001682 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001684 if (cpu_has_veic || cpu_has_vint) {
1685 int nvec = cpu_has_veic ? 64 : 8;
1686 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001687 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001688 }
1689 else if (cpu_has_divec)
1690 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /*
1693 * Some CPUs can enable/disable for cache parity detection, but does
1694 * it different ways.
1695 */
1696 parity_protection_init();
1697
1698 /*
1699 * The Data Bus Errors / Instruction Bus Errors are signaled
1700 * by external hardware. Therefore these two exceptions
1701 * may have board specific handlers.
1702 */
1703 if (board_be_init)
1704 board_be_init();
1705
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001706 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 set_except_vector(1, handle_tlbm);
1708 set_except_vector(2, handle_tlbl);
1709 set_except_vector(3, handle_tlbs);
1710
1711 set_except_vector(4, handle_adel);
1712 set_except_vector(5, handle_ades);
1713
1714 set_except_vector(6, handle_ibe);
1715 set_except_vector(7, handle_dbe);
1716
1717 set_except_vector(8, handle_sys);
1718 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001719 set_except_vector(10, rdhwr_noopt ? handle_ri :
1720 (cpu_has_vtag_icache ?
1721 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 set_except_vector(11, handle_cpu);
1723 set_except_vector(12, handle_ov);
1724 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Ralf Baechle10cc3522007-10-11 23:46:15 +01001726 if (current_cpu_type() == CPU_R6000 ||
1727 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /*
1729 * The R6000 is the only R-series CPU that features a machine
1730 * check exception (similar to the R4000 cache error) and
1731 * unaligned ldc1/sdc1 exception. The handlers have not been
1732 * written yet. Well, anyway there is no R6000 machine on the
1733 * current list of targets for Linux/MIPS.
1734 * (Duh, crap, there is someone with a triple R6k machine)
1735 */
1736 //set_except_vector(14, handle_mc);
1737 //set_except_vector(15, handle_ndc);
1738 }
1739
Ralf Baechlee01402b2005-07-14 15:57:16 +00001740
1741 if (board_nmi_handler_setup)
1742 board_nmi_handler_setup();
1743
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001744 if (cpu_has_fpu && !cpu_has_nofpuex)
1745 set_except_vector(15, handle_fpe);
1746
1747 set_except_vector(22, handle_mdmx);
1748
1749 if (cpu_has_mcheck)
1750 set_except_vector(24, handle_mcheck);
1751
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001752 if (cpu_has_mipsmt)
1753 set_except_vector(25, handle_mt);
1754
Chris Dearmanacaec422007-05-24 22:30:18 +01001755 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001756
1757 if (cpu_has_vce)
1758 /* Special exception: R4[04]00 uses also the divec space. */
David Daney566f74f2008-10-23 17:56:35 -07001759 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001760 else if (cpu_has_4kex)
David Daney566f74f2008-10-23 17:56:35 -07001761 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001762 else
David Daney566f74f2008-10-23 17:56:35 -07001763 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001764
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001765 local_flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001766 flush_tlb_handlers();
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02001767
1768 sort_extable(__start___dbe_table, __stop___dbe_table);
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001769
Ralf Baechle4483b152010-08-05 13:25:59 +01001770 cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}