blob: 3f6de76d485dc059151d27334a7abc871d99d2a9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/bootinfo.h>
30#include <asm/branch.h>
31#include <asm/break.h>
32#include <asm/cpu.h>
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000033#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/fpu.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000035#include <asm/mipsregs.h>
36#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/module.h>
38#include <asm/pgtable.h>
39#include <asm/ptrace.h>
40#include <asm/sections.h>
41#include <asm/system.h>
42#include <asm/tlbdebug.h>
43#include <asm/traps.h>
44#include <asm/uaccess.h>
David Daneyb67b2b72008-09-23 00:08:45 -070045#include <asm/watch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090048#include <asm/stacktrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090050extern void check_wait(void);
51extern asmlinkage void r4k_wait(void);
52extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010053extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054extern asmlinkage void handle_tlbm(void);
55extern asmlinkage void handle_tlbl(void);
56extern asmlinkage void handle_tlbs(void);
57extern asmlinkage void handle_adel(void);
58extern asmlinkage void handle_ades(void);
59extern asmlinkage void handle_ibe(void);
60extern asmlinkage void handle_dbe(void);
61extern asmlinkage void handle_sys(void);
62extern asmlinkage void handle_bp(void);
63extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090064extern asmlinkage void handle_ri_rdhwr_vivt(void);
65extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066extern asmlinkage void handle_cpu(void);
67extern asmlinkage void handle_ov(void);
68extern asmlinkage void handle_tr(void);
69extern asmlinkage void handle_fpe(void);
70extern asmlinkage void handle_mdmx(void);
71extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000072extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000073extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074extern asmlinkage void handle_mcheck(void);
75extern asmlinkage void handle_reserved(void);
76
Ralf Baechle12616ed2005-10-18 10:26:46 +010077extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090078 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80void (*board_be_init)(void);
81int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000082void (*board_nmi_handler_setup)(void);
83void (*board_ejtag_handler_setup)(void);
84void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020087static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090088{
Ralf Baechle39b8d522008-04-28 17:14:26 +010089 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090090 unsigned long addr;
91
92 printk("Call Trace:");
93#ifdef CONFIG_KALLSYMS
94 printk("\n");
95#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +020096 while (!kstack_end(sp)) {
97 unsigned long __user *p =
98 (unsigned long __user *)(unsigned long)sp++;
99 if (__get_user(addr, p)) {
100 printk(" (Bad stack address)");
101 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100102 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200103 if (__kernel_text_address(addr))
104 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900105 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200106 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900107}
108
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900109#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900110int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900111static int __init set_raw_show_trace(char *str)
112{
113 raw_show_trace = 1;
114 return 1;
115}
116__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900117#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200118
Ralf Baechleeae23f22007-10-14 23:27:21 +0100119static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900120{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200121 unsigned long sp = regs->regs[29];
122 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900123 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900124
125 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200126 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900127 return;
128 }
129 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200130 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200131 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900132 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200133 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900134 printk("\n");
135}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*
138 * This routine abuses get_user()/put_user() to reference pointers
139 * with at least a bit of error checking ...
140 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100141static void show_stacktrace(struct task_struct *task,
142 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 const int field = 2 * sizeof(unsigned long);
145 long stackdata;
146 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900147 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 printk("Stack :");
150 i = 0;
151 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
152 if (i && ((i % (64 / field)) == 0))
153 printk("\n ");
154 if (i > 39) {
155 printk(" ...");
156 break;
157 }
158
159 if (__get_user(stackdata, sp++)) {
160 printk(" (Bad stack address)");
161 break;
162 }
163
164 printk(" %0*lx", field, stackdata);
165 i++;
166 }
167 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200168 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900169}
170
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900171void show_stack(struct task_struct *task, unsigned long *sp)
172{
173 struct pt_regs regs;
174 if (sp) {
175 regs.regs[29] = (unsigned long)sp;
176 regs.regs[31] = 0;
177 regs.cp0_epc = 0;
178 } else {
179 if (task && task != current) {
180 regs.regs[29] = task->thread.reg29;
181 regs.regs[31] = 0;
182 regs.cp0_epc = task->thread.reg31;
183 } else {
184 prepare_frametrace(&regs);
185 }
186 }
187 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190/*
191 * The architecture-independent dump_stack generator
192 */
193void dump_stack(void)
194{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200195 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200197 prepare_frametrace(&regs);
198 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201EXPORT_SYMBOL(dump_stack);
202
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900203static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100206 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 printk("\nCode:");
209
Ralf Baechle39b8d522008-04-28 17:14:26 +0100210 if ((unsigned long)pc & 1)
211 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 for(i = -3 ; i < 6 ; i++) {
213 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100214 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 printk(" (Bad address in epc)\n");
216 break;
217 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100218 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220}
221
Ralf Baechleeae23f22007-10-14 23:27:21 +0100222static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 const int field = 2 * sizeof(unsigned long);
225 unsigned int cause = regs->cp0_cause;
226 int i;
227
228 printk("Cpu %d\n", smp_processor_id());
229
230 /*
231 * Saved main processor registers
232 */
233 for (i = 0; i < 32; ) {
234 if ((i % 4) == 0)
235 printk("$%2d :", i);
236 if (i == 0)
237 printk(" %0*lx", field, 0UL);
238 else if (i == 26 || i == 27)
239 printk(" %*s", field, "");
240 else
241 printk(" %0*lx", field, regs->regs[i]);
242
243 i++;
244 if ((i % 4) == 0)
245 printk("\n");
246 }
247
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100248#ifdef CONFIG_CPU_HAS_SMARTMIPS
249 printk("Acx : %0*lx\n", field, regs->acx);
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 printk("Hi : %0*lx\n", field, regs->hi);
252 printk("Lo : %0*lx\n", field, regs->lo);
253
254 /*
255 * Saved cp0 registers
256 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100257 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
258 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100260 printk("ra : %0*lx %pS\n", field, regs->regs[31],
261 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 printk("Status: %08x ", (uint32_t) regs->cp0_status);
264
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000265 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
266 if (regs->cp0_status & ST0_KUO)
267 printk("KUo ");
268 if (regs->cp0_status & ST0_IEO)
269 printk("IEo ");
270 if (regs->cp0_status & ST0_KUP)
271 printk("KUp ");
272 if (regs->cp0_status & ST0_IEP)
273 printk("IEp ");
274 if (regs->cp0_status & ST0_KUC)
275 printk("KUc ");
276 if (regs->cp0_status & ST0_IEC)
277 printk("IEc ");
278 } else {
279 if (regs->cp0_status & ST0_KX)
280 printk("KX ");
281 if (regs->cp0_status & ST0_SX)
282 printk("SX ");
283 if (regs->cp0_status & ST0_UX)
284 printk("UX ");
285 switch (regs->cp0_status & ST0_KSU) {
286 case KSU_USER:
287 printk("USER ");
288 break;
289 case KSU_SUPERVISOR:
290 printk("SUPERVISOR ");
291 break;
292 case KSU_KERNEL:
293 printk("KERNEL ");
294 break;
295 default:
296 printk("BAD_MODE ");
297 break;
298 }
299 if (regs->cp0_status & ST0_ERL)
300 printk("ERL ");
301 if (regs->cp0_status & ST0_EXL)
302 printk("EXL ");
303 if (regs->cp0_status & ST0_IE)
304 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 printk("\n");
307
308 printk("Cause : %08x\n", cause);
309
310 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
311 if (1 <= cause && cause <= 5)
312 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
313
Ralf Baechle9966db252007-10-11 23:46:17 +0100314 printk("PrId : %08x (%s)\n", read_c0_prid(),
315 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Ralf Baechleeae23f22007-10-14 23:27:21 +0100318/*
319 * FIXME: really the generic show_regs should take a const pointer argument.
320 */
321void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100323 __show_regs((struct pt_regs *)regs);
324}
325
326void show_registers(const struct pt_regs *regs)
327{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100328 const int field = 2 * sizeof(unsigned long);
329
Ralf Baechleeae23f22007-10-14 23:27:21 +0100330 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100332 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
333 current->comm, current->pid, current_thread_info(), current,
334 field, current_thread_info()->tp_value);
335 if (cpu_has_userlocal) {
336 unsigned long tls;
337
338 tls = read_c0_userlocal();
339 if (tls != current_thread_info()->tp_value)
340 printk("*HwTLS: %0*lx\n", field, tls);
341 }
342
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900343 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900344 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 printk("\n");
346}
347
348static DEFINE_SPINLOCK(die_lock);
349
Ralf Baechleeae23f22007-10-14 23:27:21 +0100350void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100353#ifdef CONFIG_MIPS_MT_SMTC
354 unsigned long dvpret = dvpe();
355#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 console_verbose();
358 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100359 bust_spinlocks(1);
360#ifdef CONFIG_MIPS_MT_SMTC
361 mips_mt_regdump(dvpret);
362#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100363 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700365 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200367
368 if (in_interrupt())
369 panic("Fatal exception in interrupt");
370
371 if (panic_on_oops) {
372 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
373 ssleep(5);
374 panic("Fatal exception");
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 do_exit(SIGSEGV);
378}
379
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200380extern struct exception_table_entry __start___dbe_table[];
381extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000383__asm__(
384" .section __dbe_table, \"a\"\n"
385" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387/* Given an address, look for it in the exception tables. */
388static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
389{
390 const struct exception_table_entry *e;
391
392 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
393 if (!e)
394 e = search_module_dbetables(addr);
395 return e;
396}
397
398asmlinkage void do_be(struct pt_regs *regs)
399{
400 const int field = 2 * sizeof(unsigned long);
401 const struct exception_table_entry *fixup = NULL;
402 int data = regs->cp0_cause & 4;
403 int action = MIPS_BE_FATAL;
404
405 /* XXX For now. Fixme, this searches the wrong table ... */
406 if (data && !user_mode(regs))
407 fixup = search_dbe_tables(exception_epc(regs));
408
409 if (fixup)
410 action = MIPS_BE_FIXUP;
411
412 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900413 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 switch (action) {
416 case MIPS_BE_DISCARD:
417 return;
418 case MIPS_BE_FIXUP:
419 if (fixup) {
420 regs->cp0_epc = fixup->nextinsn;
421 return;
422 }
423 break;
424 default:
425 break;
426 }
427
428 /*
429 * Assume it would be too dangerous to continue ...
430 */
431 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
432 data ? "Data" : "Instruction",
433 field, regs->cp0_epc, field, regs->regs[31]);
Jason Wessel88547002008-07-29 15:58:53 -0500434 if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
435 == NOTIFY_STOP)
436 return;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 die_if_kernel("Oops", regs);
439 force_sig(SIGBUS, current);
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100443 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 */
445
446#define OPCODE 0xfc000000
447#define BASE 0x03e00000
448#define RT 0x001f0000
449#define OFFSET 0x0000ffff
450#define LL 0xc0000000
451#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100452#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000453#define SPEC3 0x7c000000
454#define RD 0x0000f800
455#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100456#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000457#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459/*
460 * The ll_bit is cleared by r*_switch.S
461 */
462
463unsigned long ll_bit;
464
465static struct task_struct *ll_task = NULL;
466
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100467static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000469 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /*
473 * analyse the ll instruction that just caused a ri exception
474 * and put the referenced address to addr.
475 */
476
477 /* sign extend offset */
478 offset = opcode & OFFSET;
479 offset <<= 16;
480 offset >>= 16;
481
Ralf Baechlefe00f942005-03-01 19:22:29 +0000482 vaddr = (unsigned long __user *)
483 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100485 if ((unsigned long)vaddr & 3)
486 return SIGBUS;
487 if (get_user(value, vaddr))
488 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 preempt_disable();
491
492 if (ll_task == NULL || ll_task == current) {
493 ll_bit = 1;
494 } else {
495 ll_bit = 0;
496 }
497 ll_task = current;
498
499 preempt_enable();
500
501 regs->regs[(opcode & RT) >> 16] = value;
502
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100506static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000508 unsigned long __user *vaddr;
509 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /*
513 * analyse the sc instruction that just caused a ri exception
514 * and put the referenced address to addr.
515 */
516
517 /* sign extend offset */
518 offset = opcode & OFFSET;
519 offset <<= 16;
520 offset >>= 16;
521
Ralf Baechlefe00f942005-03-01 19:22:29 +0000522 vaddr = (unsigned long __user *)
523 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 reg = (opcode & RT) >> 16;
525
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100526 if ((unsigned long)vaddr & 3)
527 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 preempt_disable();
530
531 if (ll_bit == 0 || ll_task != current) {
532 regs->regs[reg] = 0;
533 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 preempt_enable();
538
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100539 if (put_user(regs->regs[reg], vaddr))
540 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 regs->regs[reg] = 1;
543
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547/*
548 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
549 * opcodes are supposed to result in coprocessor unusable exceptions if
550 * executed on ll/sc-less processors. That's the theory. In practice a
551 * few processors such as NEC's VR4100 throw reserved instruction exceptions
552 * instead, so we're doing the emulation thing in both exception handlers.
553 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100554static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100556 if ((opcode & OPCODE) == LL)
557 return simulate_ll(regs, opcode);
558 if ((opcode & OPCODE) == SC)
559 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100561 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Ralf Baechle3c370262005-04-13 17:43:59 +0000564/*
565 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100566 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000567 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100568static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000569{
Al Virodc8f6022006-01-12 01:06:07 -0800570 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000571
572 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
573 int rd = (opcode & RD) >> 11;
574 int rt = (opcode & RT) >> 16;
575 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100576 case 0: /* CPU number */
577 regs->regs[rt] = smp_processor_id();
578 return 0;
579 case 1: /* SYNCI length */
580 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
581 current_cpu_data.icache.linesz);
582 return 0;
583 case 2: /* Read count register */
584 regs->regs[rt] = read_c0_count();
585 return 0;
586 case 3: /* Count register resolution */
587 switch (current_cpu_data.cputype) {
588 case CPU_20KC:
589 case CPU_25KF:
590 regs->regs[rt] = 1;
591 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000592 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100593 regs->regs[rt] = 2;
594 }
595 return 0;
596 case 29:
597 regs->regs[rt] = ti->tp_value;
598 return 0;
599 default:
600 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000601 }
602 }
603
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500604 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100605 return -1;
606}
Ralf Baechlee5679882006-11-30 01:14:47 +0000607
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100608static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
609{
610 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
611 return 0;
612
613 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616asmlinkage void do_ov(struct pt_regs *regs)
617{
618 siginfo_t info;
619
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000620 die_if_kernel("Integer overflow", regs);
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 info.si_code = FPE_INTOVF;
623 info.si_signo = SIGFPE;
624 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000625 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 force_sig_info(SIGFPE, &info, current);
627}
628
629/*
630 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
631 */
632asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
633{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100634 siginfo_t info;
635
Jason Wessel88547002008-07-29 15:58:53 -0500636 if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
637 == NOTIFY_STOP)
638 return;
Chris Dearman57725f92006-06-30 23:35:28 +0100639 die_if_kernel("FP exception in kernel code", regs);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (fcr31 & FPU_CSR_UNI_X) {
642 int sig;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000645 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 * software emulator on-board, let's use it...
647 *
648 * Force FPU to dump state into task/thread context. We're
649 * moving a lot of data here for what is probably a single
650 * instruction, but the alternative is to pre-decode the FP
651 * register operands before invoking the emulator, which seems
652 * a bit extreme for what should be an infrequent event.
653 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000654 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900655 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100658 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /*
661 * We can't allow the emulated instruction to leave any of
662 * the cause bit set in $fcr31.
663 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900664 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900667 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* If something went wrong, signal */
670 if (sig)
671 force_sig(sig, current);
672
673 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100674 } else if (fcr31 & FPU_CSR_INV_X)
675 info.si_code = FPE_FLTINV;
676 else if (fcr31 & FPU_CSR_DIV_X)
677 info.si_code = FPE_FLTDIV;
678 else if (fcr31 & FPU_CSR_OVF_X)
679 info.si_code = FPE_FLTOVF;
680 else if (fcr31 & FPU_CSR_UDF_X)
681 info.si_code = FPE_FLTUND;
682 else if (fcr31 & FPU_CSR_INE_X)
683 info.si_code = FPE_FLTRES;
684 else
685 info.si_code = __SI_FAULT;
686 info.si_signo = SIGFPE;
687 info.si_errno = 0;
688 info.si_addr = (void __user *) regs->cp0_epc;
689 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Ralf Baechledf270052008-04-20 16:28:54 +0100692static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
693 const char *str)
694{
695 siginfo_t info;
696 char b[40];
697
Jason Wessel88547002008-07-29 15:58:53 -0500698 if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
699 return;
700
Ralf Baechledf270052008-04-20 16:28:54 +0100701 /*
702 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
703 * insns, even for trap and break codes that indicate arithmetic
704 * failures. Weird ...
705 * But should we continue the brokenness??? --macro
706 */
707 switch (code) {
708 case BRK_OVERFLOW:
709 case BRK_DIVZERO:
710 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
711 die_if_kernel(b, regs);
712 if (code == BRK_DIVZERO)
713 info.si_code = FPE_INTDIV;
714 else
715 info.si_code = FPE_INTOVF;
716 info.si_signo = SIGFPE;
717 info.si_errno = 0;
718 info.si_addr = (void __user *) regs->cp0_epc;
719 force_sig_info(SIGFPE, &info, current);
720 break;
721 case BRK_BUG:
722 die_if_kernel("Kernel bug detected", regs);
723 force_sig(SIGTRAP, current);
724 break;
725 default:
726 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
727 die_if_kernel(b, regs);
728 force_sig(SIGTRAP, current);
729 }
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732asmlinkage void do_bp(struct pt_regs *regs)
733{
734 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900736 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000737 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /*
740 * There is the ancient bug in the MIPS assemblers that the break
741 * code starts left to bit 16 instead to bit 6 in the opcode.
742 * Gas is bug-compatible, but not always, grrr...
743 * We handle both cases with a simple heuristics. --macro
744 */
745 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100746 if (bcode >= (1 << 10))
747 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Ralf Baechledf270052008-04-20 16:28:54 +0100749 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900750 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000751
752out_sigsegv:
753 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756asmlinkage void do_tr(struct pt_regs *regs)
757{
758 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900760 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000761 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 /* Immediate versions don't provide a code. */
764 if (!(opcode & OPCODE))
765 tcode = ((opcode >> 6) & ((1 << 10) - 1));
766
Ralf Baechledf270052008-04-20 16:28:54 +0100767 do_trap_or_bp(regs, tcode, "Trap");
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_ri(struct pt_regs *regs)
775{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100776 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
777 unsigned long old_epc = regs->cp0_epc;
778 unsigned int opcode = 0;
779 int status = -1;
780
Jason Wessel88547002008-07-29 15:58:53 -0500781 if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
782 == NOTIFY_STOP)
783 return;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 die_if_kernel("Reserved instruction in kernel code", regs);
786
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100787 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000788 return;
789
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100790 if (unlikely(get_user(opcode, epc) < 0))
791 status = SIGSEGV;
792
793 if (!cpu_has_llsc && status < 0)
794 status = simulate_llsc(regs, opcode);
795
796 if (status < 0)
797 status = simulate_rdhwr(regs, opcode);
798
799 if (status < 0)
800 status = simulate_sync(regs, opcode);
801
802 if (status < 0)
803 status = SIGILL;
804
805 if (unlikely(status > 0)) {
806 regs->cp0_epc = old_epc; /* Undo skip-over. */
807 force_sig(status, current);
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Ralf Baechled223a862007-07-10 17:33:02 +0100811/*
812 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
813 * emulated more than some threshold number of instructions, force migration to
814 * a "CPU" that has FP support.
815 */
816static void mt_ase_fp_affinity(void)
817{
818#ifdef CONFIG_MIPS_MT_FPAFF
819 if (mt_fpemul_threshold > 0 &&
820 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
821 /*
822 * If there's no FPU present, or if the application has already
823 * restricted the allowed set to exclude any CPUs with FPUs,
824 * we'll skip the procedure.
825 */
826 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
827 cpumask_t tmask;
828
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200829 current->thread.user_cpus_allowed
830 = current->cpus_allowed;
831 cpus_and(tmask, current->cpus_allowed,
832 mt_fpu_cpumask);
Ralf Baechled223a862007-07-10 17:33:02 +0100833 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100834 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100835 }
836 }
837#endif /* CONFIG_MIPS_MT_FPAFF */
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840asmlinkage void do_cpu(struct pt_regs *regs)
841{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100842 unsigned int __user *epc;
843 unsigned long old_epc;
844 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100846 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Atsushi Nemoto53231802007-04-14 02:37:26 +0900848 die_if_kernel("do_cpu invoked from kernel context!", regs);
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
851
852 switch (cpid) {
853 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100854 epc = (unsigned int __user *)exception_epc(regs);
855 old_epc = regs->cp0_epc;
856 opcode = 0;
857 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100859 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000861
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100862 if (unlikely(get_user(opcode, epc) < 0))
863 status = SIGSEGV;
864
865 if (!cpu_has_llsc && status < 0)
866 status = simulate_llsc(regs, opcode);
867
868 if (status < 0)
869 status = simulate_rdhwr(regs, opcode);
870
871 if (status < 0)
872 status = SIGILL;
873
874 if (unlikely(status > 0)) {
875 regs->cp0_epc = old_epc; /* Undo skip-over. */
876 force_sig(status, current);
877 }
878
879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900882 if (used_math()) /* Using the FPU again. */
883 own_fpu(1);
884 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 init_fpu();
886 set_used_math();
887 }
888
Atsushi Nemoto53231802007-04-14 02:37:26 +0900889 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900890 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900891 sig = fpu_emulator_cop1Handler(regs,
892 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (sig)
894 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +0100895 else
896 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
900
901 case 2:
902 case 3:
903 break;
904 }
905
906 force_sig(SIGILL, current);
907}
908
909asmlinkage void do_mdmx(struct pt_regs *regs)
910{
911 force_sig(SIGILL, current);
912}
913
914asmlinkage void do_watch(struct pt_regs *regs)
915{
David Daneyb67b2b72008-09-23 00:08:45 -0700916 u32 cause;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /*
David Daneyb67b2b72008-09-23 00:08:45 -0700919 * Clear WP (bit 22) bit of cause register so we don't loop
920 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
David Daneyb67b2b72008-09-23 00:08:45 -0700922 cause = read_c0_cause();
923 cause &= ~(1 << 22);
924 write_c0_cause(cause);
925
926 /*
927 * If the current thread has the watch registers loaded, save
928 * their values and send SIGTRAP. Otherwise another thread
929 * left the registers set, clear them and continue.
930 */
931 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
932 mips_read_watch_registers();
933 force_sig(SIGTRAP, current);
934 } else
935 mips_clear_watch_registers();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938asmlinkage void do_mcheck(struct pt_regs *regs)
939{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100940 const int field = 2 * sizeof(unsigned long);
941 int multi_match = regs->cp0_status & ST0_TS;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100944
945 if (multi_match) {
946 printk("Index : %0x\n", read_c0_index());
947 printk("Pagemask: %0x\n", read_c0_pagemask());
948 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
949 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
950 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
951 printk("\n");
952 dump_tlb_all();
953 }
954
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900955 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /*
958 * Some chips may have other causes of machine check (e.g. SB1
959 * graduation timer)
960 */
961 panic("Caught Machine Check exception - %scaused by multiple "
962 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100963 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000966asmlinkage void do_mt(struct pt_regs *regs)
967{
Ralf Baechle41c594a2006-04-05 09:45:45 +0100968 int subcode;
969
Ralf Baechle41c594a2006-04-05 09:45:45 +0100970 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
971 >> VPECONTROL_EXCPT_SHIFT;
972 switch (subcode) {
973 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100974 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100975 break;
976 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100977 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100978 break;
979 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100980 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100981 break;
982 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100983 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100984 break;
985 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100986 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100987 break;
988 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100989 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100990 break;
991 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100992 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +0100993 subcode);
994 break;
995 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000996 die_if_kernel("MIPS MT Thread exception in kernel", regs);
997
998 force_sig(SIGILL, current);
999}
1000
1001
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001002asmlinkage void do_dsp(struct pt_regs *regs)
1003{
1004 if (cpu_has_dsp)
1005 panic("Unexpected DSP exception\n");
1006
1007 force_sig(SIGILL, current);
1008}
1009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010asmlinkage void do_reserved(struct pt_regs *regs)
1011{
1012 /*
1013 * Game over - no way to handle this if it ever occurs. Most probably
1014 * caused by a new unknown cpu type or after another deadly
1015 * hard/software error.
1016 */
1017 show_regs(regs);
1018 panic("Caught reserved exception %ld - should not happen.",
1019 (regs->cp0_cause & 0x7f) >> 2);
1020}
1021
Ralf Baechle39b8d522008-04-28 17:14:26 +01001022static int __initdata l1parity = 1;
1023static int __init nol1parity(char *s)
1024{
1025 l1parity = 0;
1026 return 1;
1027}
1028__setup("nol1par", nol1parity);
1029static int __initdata l2parity = 1;
1030static int __init nol2parity(char *s)
1031{
1032 l2parity = 0;
1033 return 1;
1034}
1035__setup("nol2par", nol2parity);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
1038 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1039 * it different ways.
1040 */
1041static inline void parity_protection_init(void)
1042{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001043 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001045 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001046 case CPU_74K:
1047 case CPU_1004K:
1048 {
1049#define ERRCTL_PE 0x80000000
1050#define ERRCTL_L2P 0x00800000
1051 unsigned long errctl;
1052 unsigned int l1parity_present, l2parity_present;
1053
1054 errctl = read_c0_ecc();
1055 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1056
1057 /* probe L1 parity support */
1058 write_c0_ecc(errctl | ERRCTL_PE);
1059 back_to_back_c0_hazard();
1060 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1061
1062 /* probe L2 parity support */
1063 write_c0_ecc(errctl|ERRCTL_L2P);
1064 back_to_back_c0_hazard();
1065 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1066
1067 if (l1parity_present && l2parity_present) {
1068 if (l1parity)
1069 errctl |= ERRCTL_PE;
1070 if (l1parity ^ l2parity)
1071 errctl |= ERRCTL_L2P;
1072 } else if (l1parity_present) {
1073 if (l1parity)
1074 errctl |= ERRCTL_PE;
1075 } else if (l2parity_present) {
1076 if (l2parity)
1077 errctl |= ERRCTL_L2P;
1078 } else {
1079 /* No parity available */
1080 }
1081
1082 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1083
1084 write_c0_ecc(errctl);
1085 back_to_back_c0_hazard();
1086 errctl = read_c0_ecc();
1087 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1088
1089 if (l1parity_present)
1090 printk(KERN_INFO "Cache parity protection %sabled\n",
1091 (errctl & ERRCTL_PE) ? "en" : "dis");
1092
1093 if (l2parity_present) {
1094 if (l1parity_present && l1parity)
1095 errctl ^= ERRCTL_L2P;
1096 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1097 (errctl & ERRCTL_L2P) ? "en" : "dis");
1098 }
1099 }
1100 break;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001103 write_c0_ecc(0x80000000);
1104 back_to_back_c0_hazard();
1105 /* Set the PE bit (bit 31) in the c0_errctl register. */
1106 printk(KERN_INFO "Cache parity protection %sabled\n",
1107 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 case CPU_20KC:
1110 case CPU_25KF:
1111 /* Clear the DE bit (bit 16) in the c0_status register. */
1112 printk(KERN_INFO "Enable cache parity protection for "
1113 "MIPS 20KC/25KF CPUs.\n");
1114 clear_c0_status(ST0_DE);
1115 break;
1116 default:
1117 break;
1118 }
1119}
1120
1121asmlinkage void cache_parity_error(void)
1122{
1123 const int field = 2 * sizeof(unsigned long);
1124 unsigned int reg_val;
1125
1126 /* For the moment, report the problem and hang. */
1127 printk("Cache error exception:\n");
1128 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1129 reg_val = read_c0_cacheerr();
1130 printk("c0_cacheerr == %08x\n", reg_val);
1131
1132 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1133 reg_val & (1<<30) ? "secondary" : "primary",
1134 reg_val & (1<<31) ? "data" : "insn");
1135 printk("Error bits: %s%s%s%s%s%s%s\n",
1136 reg_val & (1<<29) ? "ED " : "",
1137 reg_val & (1<<28) ? "ET " : "",
1138 reg_val & (1<<26) ? "EE " : "",
1139 reg_val & (1<<25) ? "EB " : "",
1140 reg_val & (1<<24) ? "EI " : "",
1141 reg_val & (1<<23) ? "E1 " : "",
1142 reg_val & (1<<22) ? "E0 " : "");
1143 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1144
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001145#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (reg_val & (1<<22))
1147 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1148
1149 if (reg_val & (1<<23))
1150 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1151#endif
1152
1153 panic("Can't handle the cache error!");
1154}
1155
1156/*
1157 * SDBBP EJTAG debug exception handler.
1158 * We skip the instruction and return to the next instruction.
1159 */
1160void ejtag_exception_handler(struct pt_regs *regs)
1161{
1162 const int field = 2 * sizeof(unsigned long);
1163 unsigned long depc, old_epc;
1164 unsigned int debug;
1165
Chris Dearman70ae6122006-06-30 12:32:37 +01001166 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 depc = read_c0_depc();
1168 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001169 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (debug & 0x80000000) {
1171 /*
1172 * In branch delay slot.
1173 * We cheat a little bit here and use EPC to calculate the
1174 * debug return address (DEPC). EPC is restored after the
1175 * calculation.
1176 */
1177 old_epc = regs->cp0_epc;
1178 regs->cp0_epc = depc;
1179 __compute_return_epc(regs);
1180 depc = regs->cp0_epc;
1181 regs->cp0_epc = old_epc;
1182 } else
1183 depc += 4;
1184 write_c0_depc(depc);
1185
1186#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001187 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 write_c0_debug(debug | 0x100);
1189#endif
1190}
1191
1192/*
1193 * NMI exception handler.
1194 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001195NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001197 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 printk("NMI taken!!!!\n");
1199 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Ralf Baechlee01402b2005-07-14 15:57:16 +00001202#define VECTORSPACING 0x100 /* for EI/VI mode */
1203
1204unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001206unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208/*
1209 * As a side effect of the way this is implemented we're limited
1210 * to interrupt handlers in the address range from
1211 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1212 */
1213void *set_except_vector(int n, void *addr)
1214{
1215 unsigned long handler = (unsigned long) addr;
1216 unsigned long old_handler = exception_handlers[n];
1217
1218 exception_handlers[n] = handler;
1219 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001220 *(u32 *)(ebase + 0x200) = 0x08000000 |
1221 (0x03ffffff & (handler >> 2));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001222 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 return (void *)old_handler;
1225}
1226
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001227static asmlinkage void do_default_vi(void)
1228{
1229 show_regs(get_irq_regs());
1230 panic("Caught unexpected vectored interrupt.");
1231}
1232
Ralf Baechleef300e42007-05-06 18:31:18 +01001233static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001234{
1235 unsigned long handler;
1236 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001237 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001238 u32 *w;
1239 unsigned char *b;
1240
1241 if (!cpu_has_veic && !cpu_has_vint)
1242 BUG();
1243
1244 if (addr == NULL) {
1245 handler = (unsigned long) do_default_vi;
1246 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001247 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001248 handler = (unsigned long) addr;
1249 vi_handlers[n] = (unsigned long) addr;
1250
1251 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1252
Ralf Baechlef6771db2007-11-08 18:02:29 +00001253 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001254 panic("Shadow register set %d not supported", srs);
1255
1256 if (cpu_has_veic) {
1257 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001258 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001259 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001260 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001261 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001262 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001263 }
1264
1265 if (srs == 0) {
1266 /*
1267 * If no shadow set is selected then use the default handler
1268 * that does normal register saving and a standard interrupt exit
1269 */
1270
1271 extern char except_vec_vi, except_vec_vi_lui;
1272 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001273 extern char rollback_except_vec_vi;
1274 char *vec_start = (cpu_wait == r4k_wait) ?
1275 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001276#ifdef CONFIG_MIPS_MT_SMTC
1277 /*
1278 * We need to provide the SMTC vectored interrupt handler
1279 * not only with the address of the handler, but with the
1280 * Status.IM bit to be masked before going there.
1281 */
1282 extern char except_vec_vi_mori;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001283 const int mori_offset = &except_vec_vi_mori - vec_start;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001284#endif /* CONFIG_MIPS_MT_SMTC */
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001285 const int handler_len = &except_vec_vi_end - vec_start;
1286 const int lui_offset = &except_vec_vi_lui - vec_start;
1287 const int ori_offset = &except_vec_vi_ori - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001288
1289 if (handler_len > VECTORSPACING) {
1290 /*
1291 * Sigh... panicing won't help as the console
1292 * is probably not configured :(
1293 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001294 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001295 }
1296
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001297 memcpy(b, vec_start, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001298#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001299 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1300
Ralf Baechle41c594a2006-04-05 09:45:45 +01001301 w = (u32 *)(b + mori_offset);
1302 *w = (*w & 0xffff0000) | (0x100 << n);
1303#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001304 w = (u32 *)(b + lui_offset);
1305 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1306 w = (u32 *)(b + ori_offset);
1307 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001308 local_flush_icache_range((unsigned long)b,
1309 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001310 }
1311 else {
1312 /*
1313 * In other cases jump directly to the interrupt handler
1314 *
1315 * It is the handlers responsibility to save registers if required
1316 * (eg hi/lo) and return from the exception using "eret"
1317 */
1318 w = (u32 *)b;
1319 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1320 *w = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001321 local_flush_icache_range((unsigned long)b,
1322 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001323 }
1324
1325 return (void *)old_handler;
1326}
1327
Ralf Baechleef300e42007-05-06 18:31:18 +01001328void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001329{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001330 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001331}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * This is used by native signal handling
1335 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001336asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1337asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001339extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1340extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001342extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1343extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Ralf Baechle41c594a2006-04-05 09:45:45 +01001345#ifdef CONFIG_SMP
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001346static int smp_save_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001347{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001348 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001349 ? _save_fp_context(sc)
1350 : fpu_emulator_save_context(sc);
1351}
1352
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001353static int smp_restore_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001354{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001355 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001356 ? _restore_fp_context(sc)
1357 : fpu_emulator_restore_context(sc);
1358}
1359#endif
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361static inline void signal_init(void)
1362{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001363#ifdef CONFIG_SMP
1364 /* For now just do the cpu_has_fpu check when the functions are invoked */
1365 save_fp_context = smp_save_fp_context;
1366 restore_fp_context = smp_restore_fp_context;
1367#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (cpu_has_fpu) {
1369 save_fp_context = _save_fp_context;
1370 restore_fp_context = _restore_fp_context;
1371 } else {
1372 save_fp_context = fpu_emulator_save_context;
1373 restore_fp_context = fpu_emulator_restore_context;
1374 }
Ralf Baechle41c594a2006-04-05 09:45:45 +01001375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378#ifdef CONFIG_MIPS32_COMPAT
1379
1380/*
1381 * This is used by 32-bit signal stuff on the 64-bit kernel
1382 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001383asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1384asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001386extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1387extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001389extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1390extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392static inline void signal32_init(void)
1393{
1394 if (cpu_has_fpu) {
1395 save_fp_context32 = _save_fp_context32;
1396 restore_fp_context32 = _restore_fp_context32;
1397 } else {
1398 save_fp_context32 = fpu_emulator_save_context32;
1399 restore_fp_context32 = fpu_emulator_restore_context32;
1400 }
1401}
1402#endif
1403
1404extern void cpu_cache_init(void);
1405extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001406extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Ralf Baechle42f77542007-10-18 17:48:11 +01001408/*
1409 * Timer interrupt
1410 */
1411int cp0_compare_irq;
1412
1413/*
1414 * Performance counter IRQ or -1 if shared with timer
1415 */
1416int cp0_perfcount_irq;
1417EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1418
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001419static int __cpuinitdata noulri;
1420
1421static int __init ulri_disable(char *s)
1422{
1423 pr_info("Disabling ulri\n");
1424 noulri = 1;
1425
1426 return 1;
1427}
1428__setup("noulri", ulri_disable);
1429
Ralf Baechle234fcd12008-03-08 09:56:28 +00001430void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 unsigned int cpu = smp_processor_id();
1433 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001434#ifdef CONFIG_MIPS_MT_SMTC
1435 int secondaryTC = 0;
1436 int bootTC = (cpu == 0);
1437
1438 /*
1439 * Only do per_cpu_trap_init() for first TC of Each VPE.
1440 * Note that this hack assumes that the SMTC init code
1441 * assigns TCs consecutively and in ascending order.
1442 */
1443
1444 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1445 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1446 secondaryTC = 1;
1447#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
1450 * Disable coprocessors and select 32-bit or 64-bit addressing
1451 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1452 * flag that some firmware may have left set and the TS bit (for
1453 * IP27). Set XX for ISA IV code to work.
1454 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001455#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1457#endif
1458 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1459 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001460 if (cpu_has_dsp)
1461 status_set |= ST0_MX;
1462
Ralf Baechleb38c7392006-02-07 01:20:43 +00001463 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 -07001464 status_set);
1465
Ralf Baechlea3692022007-07-10 17:33:02 +01001466 if (cpu_has_mips_r2) {
1467 unsigned int enable = 0x0000000f;
1468
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001469 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001470 enable |= (1 << 29);
1471
1472 write_c0_hwrena(enable);
1473 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001474
Ralf Baechle41c594a2006-04-05 09:45:45 +01001475#ifdef CONFIG_MIPS_MT_SMTC
1476 if (!secondaryTC) {
1477#endif /* CONFIG_MIPS_MT_SMTC */
1478
Ralf Baechlee01402b2005-07-14 15:57:16 +00001479 if (cpu_has_veic || cpu_has_vint) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001480 write_c0_ebase(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001481 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001482 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001483 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001484 if (cpu_has_divec) {
1485 if (cpu_has_mipsmt) {
1486 unsigned int vpflags = dvpe();
1487 set_c0_cause(CAUSEF_IV);
1488 evpe(vpflags);
1489 } else
1490 set_c0_cause(CAUSEF_IV);
1491 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001492
1493 /*
1494 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1495 *
1496 * o read IntCtl.IPTI to determine the timer interrupt
1497 * o read IntCtl.IPPCI to determine the performance counter interrupt
1498 */
1499 if (cpu_has_mips_r2) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001500 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1501 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001502 if (cp0_perfcount_irq == cp0_compare_irq)
1503 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001504 } else {
1505 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001506 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001507 }
1508
Ralf Baechle41c594a2006-04-05 09:45:45 +01001509#ifdef CONFIG_MIPS_MT_SMTC
1510 }
1511#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1514 TLBMISS_HANDLER_SETUP();
1515
1516 atomic_inc(&init_mm.mm_count);
1517 current->active_mm = &init_mm;
1518 BUG_ON(current->mm);
1519 enter_lazy_tlb(&init_mm, current);
1520
Ralf Baechle41c594a2006-04-05 09:45:45 +01001521#ifdef CONFIG_MIPS_MT_SMTC
1522 if (bootTC) {
1523#endif /* CONFIG_MIPS_MT_SMTC */
1524 cpu_cache_init();
1525 tlb_init();
1526#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001527 } else if (!secondaryTC) {
1528 /*
1529 * First TC in non-boot VPE must do subset of tlb_init()
1530 * for MMU countrol registers.
1531 */
1532 write_c0_pagemask(PM_DEFAULT_MASK);
1533 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001534 }
1535#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Ralf Baechlee01402b2005-07-14 15:57:16 +00001538/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001539void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001540{
1541 memcpy((void *)(ebase + offset), addr, size);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001542 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001543}
1544
Ralf Baechle234fcd12008-03-08 09:56:28 +00001545static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001546 "Trying to set NULL cache error exception handler";
1547
Ralf Baechlee01402b2005-07-14 15:57:16 +00001548/* Install uncached CPU exception handler */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001549void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1550 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001551{
1552#ifdef CONFIG_32BIT
1553 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1554#endif
1555#ifdef CONFIG_64BIT
1556 unsigned long uncached_ebase = TO_UNCAC(ebase);
1557#endif
David Daney566f74f2008-10-23 17:56:35 -07001558 if (cpu_has_mips_r2)
1559 ebase += (read_c0_ebase() & 0x3ffff000);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001560
Ralf Baechle641e97f2007-10-11 23:46:05 +01001561 if (!addr)
1562 panic(panic_null_cerr);
1563
Ralf Baechlee01402b2005-07-14 15:57:16 +00001564 memcpy((void *)(uncached_ebase + offset), addr, size);
1565}
1566
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001567static int __initdata rdhwr_noopt;
1568static int __init set_rdhwr_noopt(char *str)
1569{
1570 rdhwr_noopt = 1;
1571 return 1;
1572}
1573
1574__setup("rdhwr_noopt", set_rdhwr_noopt);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576void __init trap_init(void)
1577{
1578 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 extern char except_vec4;
1580 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001581 int rollback;
1582
1583 check_wait();
1584 rollback = (cpu_wait == r4k_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jason Wessel88547002008-07-29 15:58:53 -05001586#if defined(CONFIG_KGDB)
1587 if (kgdb_early_setup)
1588 return; /* Already done */
1589#endif
1590
Ralf Baechlee01402b2005-07-14 15:57:16 +00001591 if (cpu_has_veic || cpu_has_vint)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001592 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
David Daney566f74f2008-10-23 17:56:35 -07001593 else {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001594 ebase = CAC_BASE;
David Daney566f74f2008-10-23 17:56:35 -07001595 if (cpu_has_mips_r2)
1596 ebase += (read_c0_ebase() & 0x3ffff000);
1597 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 per_cpu_trap_init();
1600
1601 /*
1602 * Copy the generic exception handlers to their final destination.
1603 * This will be overriden later as suitable for a particular
1604 * configuration.
1605 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001606 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 /*
1609 * Setup default vectors
1610 */
1611 for (i = 0; i <= 31; i++)
1612 set_except_vector(i, handle_reserved);
1613
1614 /*
1615 * Copy the EJTAG debug exception vector handler code to it's final
1616 * destination.
1617 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001618 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001619 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /*
1622 * Only some CPUs have the watch exceptions.
1623 */
1624 if (cpu_has_watch)
1625 set_except_vector(23, handle_watch);
1626
1627 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001628 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001630 if (cpu_has_veic || cpu_has_vint) {
1631 int nvec = cpu_has_veic ? 64 : 8;
1632 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001633 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001634 }
1635 else if (cpu_has_divec)
1636 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 /*
1639 * Some CPUs can enable/disable for cache parity detection, but does
1640 * it different ways.
1641 */
1642 parity_protection_init();
1643
1644 /*
1645 * The Data Bus Errors / Instruction Bus Errors are signaled
1646 * by external hardware. Therefore these two exceptions
1647 * may have board specific handlers.
1648 */
1649 if (board_be_init)
1650 board_be_init();
1651
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001652 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 set_except_vector(1, handle_tlbm);
1654 set_except_vector(2, handle_tlbl);
1655 set_except_vector(3, handle_tlbs);
1656
1657 set_except_vector(4, handle_adel);
1658 set_except_vector(5, handle_ades);
1659
1660 set_except_vector(6, handle_ibe);
1661 set_except_vector(7, handle_dbe);
1662
1663 set_except_vector(8, handle_sys);
1664 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001665 set_except_vector(10, rdhwr_noopt ? handle_ri :
1666 (cpu_has_vtag_icache ?
1667 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 set_except_vector(11, handle_cpu);
1669 set_except_vector(12, handle_ov);
1670 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Ralf Baechle10cc3522007-10-11 23:46:15 +01001672 if (current_cpu_type() == CPU_R6000 ||
1673 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * The R6000 is the only R-series CPU that features a machine
1676 * check exception (similar to the R4000 cache error) and
1677 * unaligned ldc1/sdc1 exception. The handlers have not been
1678 * written yet. Well, anyway there is no R6000 machine on the
1679 * current list of targets for Linux/MIPS.
1680 * (Duh, crap, there is someone with a triple R6k machine)
1681 */
1682 //set_except_vector(14, handle_mc);
1683 //set_except_vector(15, handle_ndc);
1684 }
1685
Ralf Baechlee01402b2005-07-14 15:57:16 +00001686
1687 if (board_nmi_handler_setup)
1688 board_nmi_handler_setup();
1689
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001690 if (cpu_has_fpu && !cpu_has_nofpuex)
1691 set_except_vector(15, handle_fpe);
1692
1693 set_except_vector(22, handle_mdmx);
1694
1695 if (cpu_has_mcheck)
1696 set_except_vector(24, handle_mcheck);
1697
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001698 if (cpu_has_mipsmt)
1699 set_except_vector(25, handle_mt);
1700
Chris Dearmanacaec422007-05-24 22:30:18 +01001701 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001702
1703 if (cpu_has_vce)
1704 /* Special exception: R4[04]00 uses also the divec space. */
David Daney566f74f2008-10-23 17:56:35 -07001705 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001706 else if (cpu_has_4kex)
David Daney566f74f2008-10-23 17:56:35 -07001707 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001708 else
David Daney566f74f2008-10-23 17:56:35 -07001709 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 signal_init();
1712#ifdef CONFIG_MIPS32_COMPAT
1713 signal32_init();
1714#endif
1715
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001716 local_flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001717 flush_tlb_handlers();
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02001718
1719 sort_extable(__start___dbe_table, __stop___dbe_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}