blob: 0a18b4c62afb3f2f5a849d182a0c41d022d5b607 [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 Baechlee50c0a82005-05-31 11:49:19 +000033#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/fpu.h>
Ralf Baechleba3049e2008-10-28 17:38:42 +000035#include <asm/fpu_emulator.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000036#include <asm/mipsregs.h>
37#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/module.h>
39#include <asm/pgtable.h>
40#include <asm/ptrace.h>
41#include <asm/sections.h>
42#include <asm/system.h>
43#include <asm/tlbdebug.h>
44#include <asm/traps.h>
45#include <asm/uaccess.h>
David Daneyb67b2b72008-09-23 00:08:45 -070046#include <asm/watch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090049#include <asm/stacktrace.h>
David Daneyf9bb4cf2008-12-11 15:33:23 -080050#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090052extern void check_wait(void);
53extern asmlinkage void r4k_wait(void);
54extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010055extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056extern asmlinkage void handle_tlbm(void);
57extern asmlinkage void handle_tlbl(void);
58extern asmlinkage void handle_tlbs(void);
59extern asmlinkage void handle_adel(void);
60extern asmlinkage void handle_ades(void);
61extern asmlinkage void handle_ibe(void);
62extern asmlinkage void handle_dbe(void);
63extern asmlinkage void handle_sys(void);
64extern asmlinkage void handle_bp(void);
65extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090066extern asmlinkage void handle_ri_rdhwr_vivt(void);
67extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068extern asmlinkage void handle_cpu(void);
69extern asmlinkage void handle_ov(void);
70extern asmlinkage void handle_tr(void);
71extern asmlinkage void handle_fpe(void);
72extern asmlinkage void handle_mdmx(void);
73extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000074extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000075extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076extern asmlinkage void handle_mcheck(void);
77extern asmlinkage void handle_reserved(void);
78
Ralf Baechle12616ed2005-10-18 10:26:46 +010079extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090080 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Daneyf9bb4cf2008-12-11 15:33:23 -080082#ifdef CONFIG_CPU_CAVIUM_OCTEON
83extern asmlinkage void octeon_cop2_restore(struct octeon_cop2_state *task);
84#endif
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086void (*board_be_init)(void);
87int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000088void (*board_nmi_handler_setup)(void);
89void (*board_ejtag_handler_setup)(void);
90void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020093static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090094{
Ralf Baechle39b8d522008-04-28 17:14:26 +010095 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090096 unsigned long addr;
97
98 printk("Call Trace:");
99#ifdef CONFIG_KALLSYMS
100 printk("\n");
101#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200102 while (!kstack_end(sp)) {
103 unsigned long __user *p =
104 (unsigned long __user *)(unsigned long)sp++;
105 if (__get_user(addr, p)) {
106 printk(" (Bad stack address)");
107 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100108 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200109 if (__kernel_text_address(addr))
110 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900111 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200112 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900113}
114
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900115#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900116int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900117static int __init set_raw_show_trace(char *str)
118{
119 raw_show_trace = 1;
120 return 1;
121}
122__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900123#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200124
Ralf Baechleeae23f22007-10-14 23:27:21 +0100125static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900126{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200127 unsigned long sp = regs->regs[29];
128 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900129 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900130
131 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200132 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900133 return;
134 }
135 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200136 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200137 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900138 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200139 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900140 printk("\n");
141}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
144 * This routine abuses get_user()/put_user() to reference pointers
145 * with at least a bit of error checking ...
146 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100147static void show_stacktrace(struct task_struct *task,
148 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 const int field = 2 * sizeof(unsigned long);
151 long stackdata;
152 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900153 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 printk("Stack :");
156 i = 0;
157 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
158 if (i && ((i % (64 / field)) == 0))
159 printk("\n ");
160 if (i > 39) {
161 printk(" ...");
162 break;
163 }
164
165 if (__get_user(stackdata, sp++)) {
166 printk(" (Bad stack address)");
167 break;
168 }
169
170 printk(" %0*lx", field, stackdata);
171 i++;
172 }
173 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200174 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900175}
176
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900177void show_stack(struct task_struct *task, unsigned long *sp)
178{
179 struct pt_regs regs;
180 if (sp) {
181 regs.regs[29] = (unsigned long)sp;
182 regs.regs[31] = 0;
183 regs.cp0_epc = 0;
184 } else {
185 if (task && task != current) {
186 regs.regs[29] = task->thread.reg29;
187 regs.regs[31] = 0;
188 regs.cp0_epc = task->thread.reg31;
189 } else {
190 prepare_frametrace(&regs);
191 }
192 }
193 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * The architecture-independent dump_stack generator
198 */
199void dump_stack(void)
200{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200201 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200203 prepare_frametrace(&regs);
204 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207EXPORT_SYMBOL(dump_stack);
208
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +0900209static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100212 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 printk("\nCode:");
215
Ralf Baechle39b8d522008-04-28 17:14:26 +0100216 if ((unsigned long)pc & 1)
217 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 for(i = -3 ; i < 6 ; i++) {
219 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100220 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 printk(" (Bad address in epc)\n");
222 break;
223 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100224 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226}
227
Ralf Baechleeae23f22007-10-14 23:27:21 +0100228static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 const int field = 2 * sizeof(unsigned long);
231 unsigned int cause = regs->cp0_cause;
232 int i;
233
234 printk("Cpu %d\n", smp_processor_id());
235
236 /*
237 * Saved main processor registers
238 */
239 for (i = 0; i < 32; ) {
240 if ((i % 4) == 0)
241 printk("$%2d :", i);
242 if (i == 0)
243 printk(" %0*lx", field, 0UL);
244 else if (i == 26 || i == 27)
245 printk(" %*s", field, "");
246 else
247 printk(" %0*lx", field, regs->regs[i]);
248
249 i++;
250 if ((i % 4) == 0)
251 printk("\n");
252 }
253
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100254#ifdef CONFIG_CPU_HAS_SMARTMIPS
255 printk("Acx : %0*lx\n", field, regs->acx);
256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 printk("Hi : %0*lx\n", field, regs->hi);
258 printk("Lo : %0*lx\n", field, regs->lo);
259
260 /*
261 * Saved cp0 registers
262 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100263 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
264 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100266 printk("ra : %0*lx %pS\n", field, regs->regs[31],
267 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 printk("Status: %08x ", (uint32_t) regs->cp0_status);
270
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000271 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
272 if (regs->cp0_status & ST0_KUO)
273 printk("KUo ");
274 if (regs->cp0_status & ST0_IEO)
275 printk("IEo ");
276 if (regs->cp0_status & ST0_KUP)
277 printk("KUp ");
278 if (regs->cp0_status & ST0_IEP)
279 printk("IEp ");
280 if (regs->cp0_status & ST0_KUC)
281 printk("KUc ");
282 if (regs->cp0_status & ST0_IEC)
283 printk("IEc ");
284 } else {
285 if (regs->cp0_status & ST0_KX)
286 printk("KX ");
287 if (regs->cp0_status & ST0_SX)
288 printk("SX ");
289 if (regs->cp0_status & ST0_UX)
290 printk("UX ");
291 switch (regs->cp0_status & ST0_KSU) {
292 case KSU_USER:
293 printk("USER ");
294 break;
295 case KSU_SUPERVISOR:
296 printk("SUPERVISOR ");
297 break;
298 case KSU_KERNEL:
299 printk("KERNEL ");
300 break;
301 default:
302 printk("BAD_MODE ");
303 break;
304 }
305 if (regs->cp0_status & ST0_ERL)
306 printk("ERL ");
307 if (regs->cp0_status & ST0_EXL)
308 printk("EXL ");
309 if (regs->cp0_status & ST0_IE)
310 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 printk("\n");
313
314 printk("Cause : %08x\n", cause);
315
316 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
317 if (1 <= cause && cause <= 5)
318 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
319
Ralf Baechle9966db252007-10-11 23:46:17 +0100320 printk("PrId : %08x (%s)\n", read_c0_prid(),
321 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Ralf Baechleeae23f22007-10-14 23:27:21 +0100324/*
325 * FIXME: really the generic show_regs should take a const pointer argument.
326 */
327void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100329 __show_regs((struct pt_regs *)regs);
330}
331
332void show_registers(const struct pt_regs *regs)
333{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100334 const int field = 2 * sizeof(unsigned long);
335
Ralf Baechleeae23f22007-10-14 23:27:21 +0100336 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100338 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
339 current->comm, current->pid, current_thread_info(), current,
340 field, current_thread_info()->tp_value);
341 if (cpu_has_userlocal) {
342 unsigned long tls;
343
344 tls = read_c0_userlocal();
345 if (tls != current_thread_info()->tp_value)
346 printk("*HwTLS: %0*lx\n", field, tls);
347 }
348
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900349 show_stacktrace(current, regs);
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +0900350 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 printk("\n");
352}
353
354static DEFINE_SPINLOCK(die_lock);
355
Ralf Baechleeae23f22007-10-14 23:27:21 +0100356void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100359#ifdef CONFIG_MIPS_MT_SMTC
360 unsigned long dvpret = dvpe();
361#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 console_verbose();
364 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100365 bust_spinlocks(1);
366#ifdef CONFIG_MIPS_MT_SMTC
367 mips_mt_regdump(dvpret);
368#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100369 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700371 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200373
374 if (in_interrupt())
375 panic("Fatal exception in interrupt");
376
377 if (panic_on_oops) {
378 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
379 ssleep(5);
380 panic("Fatal exception");
381 }
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 do_exit(SIGSEGV);
384}
385
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200386extern struct exception_table_entry __start___dbe_table[];
387extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000389__asm__(
390" .section __dbe_table, \"a\"\n"
391" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393/* Given an address, look for it in the exception tables. */
394static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
395{
396 const struct exception_table_entry *e;
397
398 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
399 if (!e)
400 e = search_module_dbetables(addr);
401 return e;
402}
403
404asmlinkage void do_be(struct pt_regs *regs)
405{
406 const int field = 2 * sizeof(unsigned long);
407 const struct exception_table_entry *fixup = NULL;
408 int data = regs->cp0_cause & 4;
409 int action = MIPS_BE_FATAL;
410
411 /* XXX For now. Fixme, this searches the wrong table ... */
412 if (data && !user_mode(regs))
413 fixup = search_dbe_tables(exception_epc(regs));
414
415 if (fixup)
416 action = MIPS_BE_FIXUP;
417
418 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900419 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 switch (action) {
422 case MIPS_BE_DISCARD:
423 return;
424 case MIPS_BE_FIXUP:
425 if (fixup) {
426 regs->cp0_epc = fixup->nextinsn;
427 return;
428 }
429 break;
430 default:
431 break;
432 }
433
434 /*
435 * Assume it would be too dangerous to continue ...
436 */
437 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
438 data ? "Data" : "Instruction",
439 field, regs->cp0_epc, field, regs->regs[31]);
Jason Wessel88547002008-07-29 15:58:53 -0500440 if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
441 == NOTIFY_STOP)
442 return;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 die_if_kernel("Oops", regs);
445 force_sig(SIGBUS, current);
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100449 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451
452#define OPCODE 0xfc000000
453#define BASE 0x03e00000
454#define RT 0x001f0000
455#define OFFSET 0x0000ffff
456#define LL 0xc0000000
457#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100458#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000459#define SPEC3 0x7c000000
460#define RD 0x0000f800
461#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100462#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000463#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465/*
466 * The ll_bit is cleared by r*_switch.S
467 */
468
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200469unsigned int ll_bit;
470struct task_struct *ll_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100472static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000474 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /*
478 * analyse the ll instruction that just caused a ri exception
479 * and put the referenced address to addr.
480 */
481
482 /* sign extend offset */
483 offset = opcode & OFFSET;
484 offset <<= 16;
485 offset >>= 16;
486
Ralf Baechlefe00f942005-03-01 19:22:29 +0000487 vaddr = (unsigned long __user *)
488 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100490 if ((unsigned long)vaddr & 3)
491 return SIGBUS;
492 if (get_user(value, vaddr))
493 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 preempt_disable();
496
497 if (ll_task == NULL || ll_task == current) {
498 ll_bit = 1;
499 } else {
500 ll_bit = 0;
501 }
502 ll_task = current;
503
504 preempt_enable();
505
506 regs->regs[(opcode & RT) >> 16] = value;
507
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100511static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000513 unsigned long __user *vaddr;
514 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * analyse the sc instruction that just caused a ri exception
519 * and put the referenced address to addr.
520 */
521
522 /* sign extend offset */
523 offset = opcode & OFFSET;
524 offset <<= 16;
525 offset >>= 16;
526
Ralf Baechlefe00f942005-03-01 19:22:29 +0000527 vaddr = (unsigned long __user *)
528 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 reg = (opcode & RT) >> 16;
530
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100531 if ((unsigned long)vaddr & 3)
532 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 preempt_disable();
535
536 if (ll_bit == 0 || ll_task != current) {
537 regs->regs[reg] = 0;
538 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
542 preempt_enable();
543
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100544 if (put_user(regs->regs[reg], vaddr))
545 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 regs->regs[reg] = 1;
548
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/*
553 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
554 * opcodes are supposed to result in coprocessor unusable exceptions if
555 * executed on ll/sc-less processors. That's the theory. In practice a
556 * few processors such as NEC's VR4100 throw reserved instruction exceptions
557 * instead, so we're doing the emulation thing in both exception handlers.
558 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100559static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100561 if ((opcode & OPCODE) == LL)
562 return simulate_ll(regs, opcode);
563 if ((opcode & OPCODE) == SC)
564 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100566 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Ralf Baechle3c370262005-04-13 17:43:59 +0000569/*
570 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100571 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000572 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100573static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000574{
Al Virodc8f6022006-01-12 01:06:07 -0800575 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000576
577 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
578 int rd = (opcode & RD) >> 11;
579 int rt = (opcode & RT) >> 16;
580 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100581 case 0: /* CPU number */
582 regs->regs[rt] = smp_processor_id();
583 return 0;
584 case 1: /* SYNCI length */
585 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
586 current_cpu_data.icache.linesz);
587 return 0;
588 case 2: /* Read count register */
589 regs->regs[rt] = read_c0_count();
590 return 0;
591 case 3: /* Count register resolution */
592 switch (current_cpu_data.cputype) {
593 case CPU_20KC:
594 case CPU_25KF:
595 regs->regs[rt] = 1;
596 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000597 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100598 regs->regs[rt] = 2;
599 }
600 return 0;
601 case 29:
602 regs->regs[rt] = ti->tp_value;
603 return 0;
604 default:
605 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000606 }
607 }
608
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500609 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100610 return -1;
611}
Ralf Baechlee5679882006-11-30 01:14:47 +0000612
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100613static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
614{
615 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
616 return 0;
617
618 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621asmlinkage void do_ov(struct pt_regs *regs)
622{
623 siginfo_t info;
624
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000625 die_if_kernel("Integer overflow", regs);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 info.si_code = FPE_INTOVF;
628 info.si_signo = SIGFPE;
629 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000630 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 force_sig_info(SIGFPE, &info, current);
632}
633
634/*
635 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
636 */
637asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
638{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100639 siginfo_t info;
640
Jason Wessel88547002008-07-29 15:58:53 -0500641 if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
642 == NOTIFY_STOP)
643 return;
Chris Dearman57725f92006-06-30 23:35:28 +0100644 die_if_kernel("FP exception in kernel code", regs);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (fcr31 & FPU_CSR_UNI_X) {
647 int sig;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000650 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * software emulator on-board, let's use it...
652 *
653 * Force FPU to dump state into task/thread context. We're
654 * moving a lot of data here for what is probably a single
655 * instruction, but the alternative is to pre-decode the FP
656 * register operands before invoking the emulator, which seems
657 * a bit extreme for what should be an infrequent event.
658 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000659 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900660 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100663 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 /*
666 * We can't allow the emulated instruction to leave any of
667 * the cause bit set in $fcr31.
668 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900669 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900672 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* If something went wrong, signal */
675 if (sig)
676 force_sig(sig, current);
677
678 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100679 } else if (fcr31 & FPU_CSR_INV_X)
680 info.si_code = FPE_FLTINV;
681 else if (fcr31 & FPU_CSR_DIV_X)
682 info.si_code = FPE_FLTDIV;
683 else if (fcr31 & FPU_CSR_OVF_X)
684 info.si_code = FPE_FLTOVF;
685 else if (fcr31 & FPU_CSR_UDF_X)
686 info.si_code = FPE_FLTUND;
687 else if (fcr31 & FPU_CSR_INE_X)
688 info.si_code = FPE_FLTRES;
689 else
690 info.si_code = __SI_FAULT;
691 info.si_signo = SIGFPE;
692 info.si_errno = 0;
693 info.si_addr = (void __user *) regs->cp0_epc;
694 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Ralf Baechledf270052008-04-20 16:28:54 +0100697static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
698 const char *str)
699{
700 siginfo_t info;
701 char b[40];
702
Jason Wessel88547002008-07-29 15:58:53 -0500703 if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
704 return;
705
Ralf Baechledf270052008-04-20 16:28:54 +0100706 /*
707 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
708 * insns, even for trap and break codes that indicate arithmetic
709 * failures. Weird ...
710 * But should we continue the brokenness??? --macro
711 */
712 switch (code) {
713 case BRK_OVERFLOW:
714 case BRK_DIVZERO:
715 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
716 die_if_kernel(b, regs);
717 if (code == BRK_DIVZERO)
718 info.si_code = FPE_INTDIV;
719 else
720 info.si_code = FPE_INTOVF;
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);
725 break;
726 case BRK_BUG:
727 die_if_kernel("Kernel bug detected", regs);
728 force_sig(SIGTRAP, current);
729 break;
Ralf Baechleba3049e2008-10-28 17:38:42 +0000730 case BRK_MEMU:
731 /*
732 * Address errors may be deliberately induced by the FPU
733 * emulator to retake control of the CPU after executing the
734 * instruction in the delay slot of an emulated branch.
735 *
736 * Terminate if exception was recognized as a delay slot return
737 * otherwise handle as normal.
738 */
739 if (do_dsemulret(regs))
740 return;
741
742 die_if_kernel("Math emu break/trap", regs);
743 force_sig(SIGTRAP, current);
744 break;
Ralf Baechledf270052008-04-20 16:28:54 +0100745 default:
746 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
747 die_if_kernel(b, regs);
748 force_sig(SIGTRAP, current);
749 }
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752asmlinkage void do_bp(struct pt_regs *regs)
753{
754 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900756 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000757 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /*
760 * There is the ancient bug in the MIPS assemblers that the break
761 * code starts left to bit 16 instead to bit 6 in the opcode.
762 * Gas is bug-compatible, but not always, grrr...
763 * We handle both cases with a simple heuristics. --macro
764 */
765 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100766 if (bcode >= (1 << 10))
767 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Ralf Baechledf270052008-04-20 16:28:54 +0100769 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900770 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000771
772out_sigsegv:
773 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776asmlinkage void do_tr(struct pt_regs *regs)
777{
778 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900780 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000781 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 /* Immediate versions don't provide a code. */
784 if (!(opcode & OPCODE))
785 tcode = ((opcode >> 6) & ((1 << 10) - 1));
786
Ralf Baechledf270052008-04-20 16:28:54 +0100787 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900788 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000789
790out_sigsegv:
791 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794asmlinkage void do_ri(struct pt_regs *regs)
795{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100796 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
797 unsigned long old_epc = regs->cp0_epc;
798 unsigned int opcode = 0;
799 int status = -1;
800
Jason Wessel88547002008-07-29 15:58:53 -0500801 if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
802 == NOTIFY_STOP)
803 return;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 die_if_kernel("Reserved instruction in kernel code", regs);
806
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100807 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000808 return;
809
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100810 if (unlikely(get_user(opcode, epc) < 0))
811 status = SIGSEGV;
812
813 if (!cpu_has_llsc && status < 0)
814 status = simulate_llsc(regs, opcode);
815
816 if (status < 0)
817 status = simulate_rdhwr(regs, opcode);
818
819 if (status < 0)
820 status = simulate_sync(regs, opcode);
821
822 if (status < 0)
823 status = SIGILL;
824
825 if (unlikely(status > 0)) {
826 regs->cp0_epc = old_epc; /* Undo skip-over. */
827 force_sig(status, current);
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Ralf Baechled223a8612007-07-10 17:33:02 +0100831/*
832 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
833 * emulated more than some threshold number of instructions, force migration to
834 * a "CPU" that has FP support.
835 */
836static void mt_ase_fp_affinity(void)
837{
838#ifdef CONFIG_MIPS_MT_FPAFF
839 if (mt_fpemul_threshold > 0 &&
840 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
841 /*
842 * If there's no FPU present, or if the application has already
843 * restricted the allowed set to exclude any CPUs with FPUs,
844 * we'll skip the procedure.
845 */
846 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
847 cpumask_t tmask;
848
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200849 current->thread.user_cpus_allowed
850 = current->cpus_allowed;
851 cpus_and(tmask, current->cpus_allowed,
852 mt_fpu_cpumask);
Ralf Baechled223a8612007-07-10 17:33:02 +0100853 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100854 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a8612007-07-10 17:33:02 +0100855 }
856 }
857#endif /* CONFIG_MIPS_MT_FPAFF */
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860asmlinkage void do_cpu(struct pt_regs *regs)
861{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100862 unsigned int __user *epc;
863 unsigned long old_epc;
864 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100866 int status;
David Daneyf9bb4cf2008-12-11 15:33:23 -0800867 unsigned long __maybe_unused flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Atsushi Nemoto53231802007-04-14 02:37:26 +0900869 die_if_kernel("do_cpu invoked from kernel context!", regs);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
872
873 switch (cpid) {
874 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100875 epc = (unsigned int __user *)exception_epc(regs);
876 old_epc = regs->cp0_epc;
877 opcode = 0;
878 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100880 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000882
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100883 if (unlikely(get_user(opcode, epc) < 0))
884 status = SIGSEGV;
885
886 if (!cpu_has_llsc && status < 0)
887 status = simulate_llsc(regs, opcode);
888
889 if (status < 0)
890 status = simulate_rdhwr(regs, opcode);
891
892 if (status < 0)
893 status = SIGILL;
894
895 if (unlikely(status > 0)) {
896 regs->cp0_epc = old_epc; /* Undo skip-over. */
897 force_sig(status, current);
898 }
899
900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900903 if (used_math()) /* Using the FPU again. */
904 own_fpu(1);
905 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 init_fpu();
907 set_used_math();
908 }
909
Atsushi Nemoto53231802007-04-14 02:37:26 +0900910 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900911 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900912 sig = fpu_emulator_cop1Handler(regs,
913 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (sig)
915 force_sig(sig, current);
Ralf Baechled223a8612007-07-10 17:33:02 +0100916 else
917 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return;
921
922 case 2:
David Daneyf9bb4cf2008-12-11 15:33:23 -0800923#ifdef CONFIG_CPU_CAVIUM_OCTEON
924 prefetch(&current->thread.cp2);
925 local_irq_save(flags);
926 KSTK_STATUS(current) |= ST0_CU2;
927 status = read_c0_status();
928 write_c0_status(status | ST0_CU2);
929 octeon_cop2_restore(&(current->thread.cp2));
930 write_c0_status(status & ~ST0_CU2);
931 local_irq_restore(flags);
932 return;
933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case 3:
935 break;
936 }
937
938 force_sig(SIGILL, current);
939}
940
941asmlinkage void do_mdmx(struct pt_regs *regs)
942{
943 force_sig(SIGILL, current);
944}
945
David Daney8bc6d052009-01-05 15:29:58 -0800946/*
947 * Called with interrupts disabled.
948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949asmlinkage void do_watch(struct pt_regs *regs)
950{
David Daneyb67b2b72008-09-23 00:08:45 -0700951 u32 cause;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /*
David Daneyb67b2b72008-09-23 00:08:45 -0700954 * Clear WP (bit 22) bit of cause register so we don't loop
955 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
David Daneyb67b2b72008-09-23 00:08:45 -0700957 cause = read_c0_cause();
958 cause &= ~(1 << 22);
959 write_c0_cause(cause);
960
961 /*
962 * If the current thread has the watch registers loaded, save
963 * their values and send SIGTRAP. Otherwise another thread
964 * left the registers set, clear them and continue.
965 */
966 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
967 mips_read_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -0800968 local_irq_enable();
David Daneyb67b2b72008-09-23 00:08:45 -0700969 force_sig(SIGTRAP, current);
David Daney8bc6d052009-01-05 15:29:58 -0800970 } else {
David Daneyb67b2b72008-09-23 00:08:45 -0700971 mips_clear_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -0800972 local_irq_enable();
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976asmlinkage void do_mcheck(struct pt_regs *regs)
977{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100978 const int field = 2 * sizeof(unsigned long);
979 int multi_match = regs->cp0_status & ST0_TS;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100982
983 if (multi_match) {
984 printk("Index : %0x\n", read_c0_index());
985 printk("Pagemask: %0x\n", read_c0_pagemask());
986 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
987 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
988 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
989 printk("\n");
990 dump_tlb_all();
991 }
992
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +0900993 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * Some chips may have other causes of machine check (e.g. SB1
997 * graduation timer)
998 */
999 panic("Caught Machine Check exception - %scaused by multiple "
1000 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001001 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001004asmlinkage void do_mt(struct pt_regs *regs)
1005{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001006 int subcode;
1007
Ralf Baechle41c594a2006-04-05 09:45:45 +01001008 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1009 >> VPECONTROL_EXCPT_SHIFT;
1010 switch (subcode) {
1011 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001012 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001013 break;
1014 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001015 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001016 break;
1017 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001018 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001019 break;
1020 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001021 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001022 break;
1023 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001024 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001025 break;
1026 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001027 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001028 break;
1029 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001030 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +01001031 subcode);
1032 break;
1033 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001034 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1035
1036 force_sig(SIGILL, current);
1037}
1038
1039
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001040asmlinkage void do_dsp(struct pt_regs *regs)
1041{
1042 if (cpu_has_dsp)
1043 panic("Unexpected DSP exception\n");
1044
1045 force_sig(SIGILL, current);
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048asmlinkage void do_reserved(struct pt_regs *regs)
1049{
1050 /*
1051 * Game over - no way to handle this if it ever occurs. Most probably
1052 * caused by a new unknown cpu type or after another deadly
1053 * hard/software error.
1054 */
1055 show_regs(regs);
1056 panic("Caught reserved exception %ld - should not happen.",
1057 (regs->cp0_cause & 0x7f) >> 2);
1058}
1059
Ralf Baechle39b8d522008-04-28 17:14:26 +01001060static int __initdata l1parity = 1;
1061static int __init nol1parity(char *s)
1062{
1063 l1parity = 0;
1064 return 1;
1065}
1066__setup("nol1par", nol1parity);
1067static int __initdata l2parity = 1;
1068static int __init nol2parity(char *s)
1069{
1070 l2parity = 0;
1071 return 1;
1072}
1073__setup("nol2par", nol2parity);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/*
1076 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1077 * it different ways.
1078 */
1079static inline void parity_protection_init(void)
1080{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001081 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001083 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001084 case CPU_74K:
1085 case CPU_1004K:
1086 {
1087#define ERRCTL_PE 0x80000000
1088#define ERRCTL_L2P 0x00800000
1089 unsigned long errctl;
1090 unsigned int l1parity_present, l2parity_present;
1091
1092 errctl = read_c0_ecc();
1093 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1094
1095 /* probe L1 parity support */
1096 write_c0_ecc(errctl | ERRCTL_PE);
1097 back_to_back_c0_hazard();
1098 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1099
1100 /* probe L2 parity support */
1101 write_c0_ecc(errctl|ERRCTL_L2P);
1102 back_to_back_c0_hazard();
1103 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1104
1105 if (l1parity_present && l2parity_present) {
1106 if (l1parity)
1107 errctl |= ERRCTL_PE;
1108 if (l1parity ^ l2parity)
1109 errctl |= ERRCTL_L2P;
1110 } else if (l1parity_present) {
1111 if (l1parity)
1112 errctl |= ERRCTL_PE;
1113 } else if (l2parity_present) {
1114 if (l2parity)
1115 errctl |= ERRCTL_L2P;
1116 } else {
1117 /* No parity available */
1118 }
1119
1120 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1121
1122 write_c0_ecc(errctl);
1123 back_to_back_c0_hazard();
1124 errctl = read_c0_ecc();
1125 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1126
1127 if (l1parity_present)
1128 printk(KERN_INFO "Cache parity protection %sabled\n",
1129 (errctl & ERRCTL_PE) ? "en" : "dis");
1130
1131 if (l2parity_present) {
1132 if (l1parity_present && l1parity)
1133 errctl ^= ERRCTL_L2P;
1134 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1135 (errctl & ERRCTL_L2P) ? "en" : "dis");
1136 }
1137 }
1138 break;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001141 write_c0_ecc(0x80000000);
1142 back_to_back_c0_hazard();
1143 /* Set the PE bit (bit 31) in the c0_errctl register. */
1144 printk(KERN_INFO "Cache parity protection %sabled\n",
1145 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
1147 case CPU_20KC:
1148 case CPU_25KF:
1149 /* Clear the DE bit (bit 16) in the c0_status register. */
1150 printk(KERN_INFO "Enable cache parity protection for "
1151 "MIPS 20KC/25KF CPUs.\n");
1152 clear_c0_status(ST0_DE);
1153 break;
1154 default:
1155 break;
1156 }
1157}
1158
1159asmlinkage void cache_parity_error(void)
1160{
1161 const int field = 2 * sizeof(unsigned long);
1162 unsigned int reg_val;
1163
1164 /* For the moment, report the problem and hang. */
1165 printk("Cache error exception:\n");
1166 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1167 reg_val = read_c0_cacheerr();
1168 printk("c0_cacheerr == %08x\n", reg_val);
1169
1170 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1171 reg_val & (1<<30) ? "secondary" : "primary",
1172 reg_val & (1<<31) ? "data" : "insn");
1173 printk("Error bits: %s%s%s%s%s%s%s\n",
1174 reg_val & (1<<29) ? "ED " : "",
1175 reg_val & (1<<28) ? "ET " : "",
1176 reg_val & (1<<26) ? "EE " : "",
1177 reg_val & (1<<25) ? "EB " : "",
1178 reg_val & (1<<24) ? "EI " : "",
1179 reg_val & (1<<23) ? "E1 " : "",
1180 reg_val & (1<<22) ? "E0 " : "");
1181 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1182
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001183#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (reg_val & (1<<22))
1185 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1186
1187 if (reg_val & (1<<23))
1188 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1189#endif
1190
1191 panic("Can't handle the cache error!");
1192}
1193
1194/*
1195 * SDBBP EJTAG debug exception handler.
1196 * We skip the instruction and return to the next instruction.
1197 */
1198void ejtag_exception_handler(struct pt_regs *regs)
1199{
1200 const int field = 2 * sizeof(unsigned long);
1201 unsigned long depc, old_epc;
1202 unsigned int debug;
1203
Chris Dearman70ae6122006-06-30 12:32:37 +01001204 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 depc = read_c0_depc();
1206 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001207 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (debug & 0x80000000) {
1209 /*
1210 * In branch delay slot.
1211 * We cheat a little bit here and use EPC to calculate the
1212 * debug return address (DEPC). EPC is restored after the
1213 * calculation.
1214 */
1215 old_epc = regs->cp0_epc;
1216 regs->cp0_epc = depc;
1217 __compute_return_epc(regs);
1218 depc = regs->cp0_epc;
1219 regs->cp0_epc = old_epc;
1220 } else
1221 depc += 4;
1222 write_c0_depc(depc);
1223
1224#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001225 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 write_c0_debug(debug | 0x100);
1227#endif
1228}
1229
1230/*
1231 * NMI exception handler.
1232 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001233NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001235 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 printk("NMI taken!!!!\n");
1237 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Ralf Baechlee01402b2005-07-14 15:57:16 +00001240#define VECTORSPACING 0x100 /* for EI/VI mode */
1241
1242unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001244unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246/*
1247 * As a side effect of the way this is implemented we're limited
1248 * to interrupt handlers in the address range from
1249 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1250 */
1251void *set_except_vector(int n, void *addr)
1252{
1253 unsigned long handler = (unsigned long) addr;
1254 unsigned long old_handler = exception_handlers[n];
1255
1256 exception_handlers[n] = handler;
1257 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001258 *(u32 *)(ebase + 0x200) = 0x08000000 |
1259 (0x03ffffff & (handler >> 2));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001260 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262 return (void *)old_handler;
1263}
1264
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001265static asmlinkage void do_default_vi(void)
1266{
1267 show_regs(get_irq_regs());
1268 panic("Caught unexpected vectored interrupt.");
1269}
1270
Ralf Baechleef300e42007-05-06 18:31:18 +01001271static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001272{
1273 unsigned long handler;
1274 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001275 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001276 u32 *w;
1277 unsigned char *b;
1278
Ralf Baechleb72b7092009-03-30 14:49:44 +02001279 BUG_ON(!cpu_has_veic && !cpu_has_vint);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001280
1281 if (addr == NULL) {
1282 handler = (unsigned long) do_default_vi;
1283 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001284 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001285 handler = (unsigned long) addr;
1286 vi_handlers[n] = (unsigned long) addr;
1287
1288 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1289
Ralf Baechlef6771db2007-11-08 18:02:29 +00001290 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001291 panic("Shadow register set %d not supported", srs);
1292
1293 if (cpu_has_veic) {
1294 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001295 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001296 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001297 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001298 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001299 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001300 }
1301
1302 if (srs == 0) {
1303 /*
1304 * If no shadow set is selected then use the default handler
1305 * that does normal register saving and a standard interrupt exit
1306 */
1307
1308 extern char except_vec_vi, except_vec_vi_lui;
1309 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001310 extern char rollback_except_vec_vi;
1311 char *vec_start = (cpu_wait == r4k_wait) ?
1312 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001313#ifdef CONFIG_MIPS_MT_SMTC
1314 /*
1315 * We need to provide the SMTC vectored interrupt handler
1316 * not only with the address of the handler, but with the
1317 * Status.IM bit to be masked before going there.
1318 */
1319 extern char except_vec_vi_mori;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001320 const int mori_offset = &except_vec_vi_mori - vec_start;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001321#endif /* CONFIG_MIPS_MT_SMTC */
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001322 const int handler_len = &except_vec_vi_end - vec_start;
1323 const int lui_offset = &except_vec_vi_lui - vec_start;
1324 const int ori_offset = &except_vec_vi_ori - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001325
1326 if (handler_len > VECTORSPACING) {
1327 /*
1328 * Sigh... panicing won't help as the console
1329 * is probably not configured :(
1330 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001331 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001332 }
1333
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001334 memcpy(b, vec_start, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001335#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001336 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1337
Ralf Baechle41c594a2006-04-05 09:45:45 +01001338 w = (u32 *)(b + mori_offset);
1339 *w = (*w & 0xffff0000) | (0x100 << n);
1340#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001341 w = (u32 *)(b + lui_offset);
1342 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1343 w = (u32 *)(b + ori_offset);
1344 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001345 local_flush_icache_range((unsigned long)b,
1346 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001347 }
1348 else {
1349 /*
1350 * In other cases jump directly to the interrupt handler
1351 *
1352 * It is the handlers responsibility to save registers if required
1353 * (eg hi/lo) and return from the exception using "eret"
1354 */
1355 w = (u32 *)b;
1356 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1357 *w = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001358 local_flush_icache_range((unsigned long)b,
1359 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001360 }
1361
1362 return (void *)old_handler;
1363}
1364
Ralf Baechleef300e42007-05-06 18:31:18 +01001365void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001366{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001367 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001368}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370/*
1371 * This is used by native signal handling
1372 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001373asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1374asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001376extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1377extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001379extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1380extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Ralf Baechle41c594a2006-04-05 09:45:45 +01001382#ifdef CONFIG_SMP
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001383static int smp_save_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001384{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001385 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001386 ? _save_fp_context(sc)
1387 : fpu_emulator_save_context(sc);
1388}
1389
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001390static int smp_restore_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001391{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001392 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001393 ? _restore_fp_context(sc)
1394 : fpu_emulator_restore_context(sc);
1395}
1396#endif
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398static inline void signal_init(void)
1399{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001400#ifdef CONFIG_SMP
1401 /* For now just do the cpu_has_fpu check when the functions are invoked */
1402 save_fp_context = smp_save_fp_context;
1403 restore_fp_context = smp_restore_fp_context;
1404#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (cpu_has_fpu) {
1406 save_fp_context = _save_fp_context;
1407 restore_fp_context = _restore_fp_context;
1408 } else {
1409 save_fp_context = fpu_emulator_save_context;
1410 restore_fp_context = fpu_emulator_restore_context;
1411 }
Ralf Baechle41c594a2006-04-05 09:45:45 +01001412#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415#ifdef CONFIG_MIPS32_COMPAT
1416
1417/*
1418 * This is used by 32-bit signal stuff on the 64-bit kernel
1419 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001420asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1421asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001423extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1424extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001426extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1427extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429static inline void signal32_init(void)
1430{
1431 if (cpu_has_fpu) {
1432 save_fp_context32 = _save_fp_context32;
1433 restore_fp_context32 = _restore_fp_context32;
1434 } else {
1435 save_fp_context32 = fpu_emulator_save_context32;
1436 restore_fp_context32 = fpu_emulator_restore_context32;
1437 }
1438}
1439#endif
1440
1441extern void cpu_cache_init(void);
1442extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001443extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Ralf Baechle42f77542007-10-18 17:48:11 +01001445/*
1446 * Timer interrupt
1447 */
1448int cp0_compare_irq;
1449
1450/*
1451 * Performance counter IRQ or -1 if shared with timer
1452 */
1453int cp0_perfcount_irq;
1454EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1455
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001456static int __cpuinitdata noulri;
1457
1458static int __init ulri_disable(char *s)
1459{
1460 pr_info("Disabling ulri\n");
1461 noulri = 1;
1462
1463 return 1;
1464}
1465__setup("noulri", ulri_disable);
1466
Ralf Baechle234fcd12008-03-08 09:56:28 +00001467void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 unsigned int cpu = smp_processor_id();
1470 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001471#ifdef CONFIG_MIPS_MT_SMTC
1472 int secondaryTC = 0;
1473 int bootTC = (cpu == 0);
1474
1475 /*
1476 * Only do per_cpu_trap_init() for first TC of Each VPE.
1477 * Note that this hack assumes that the SMTC init code
1478 * assigns TCs consecutively and in ascending order.
1479 */
1480
1481 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1482 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1483 secondaryTC = 1;
1484#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /*
1487 * Disable coprocessors and select 32-bit or 64-bit addressing
1488 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1489 * flag that some firmware may have left set and the TS bit (for
1490 * IP27). Set XX for ISA IV code to work.
1491 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001492#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1494#endif
1495 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1496 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001497 if (cpu_has_dsp)
1498 status_set |= ST0_MX;
1499
Ralf Baechleb38c7392006-02-07 01:20:43 +00001500 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 -07001501 status_set);
1502
Ralf Baechlea3692022007-07-10 17:33:02 +01001503 if (cpu_has_mips_r2) {
David Daneyfbeda192009-05-13 15:59:55 -07001504 unsigned int enable = 0x0000000f | cpu_hwrena_impl_bits;
Ralf Baechlea3692022007-07-10 17:33:02 +01001505
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001506 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001507 enable |= (1 << 29);
1508
1509 write_c0_hwrena(enable);
1510 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001511
Ralf Baechle41c594a2006-04-05 09:45:45 +01001512#ifdef CONFIG_MIPS_MT_SMTC
1513 if (!secondaryTC) {
1514#endif /* CONFIG_MIPS_MT_SMTC */
1515
Ralf Baechlee01402b2005-07-14 15:57:16 +00001516 if (cpu_has_veic || cpu_has_vint) {
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001517 unsigned long sr = set_c0_status(ST0_BEV);
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001518 write_c0_ebase(ebase);
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001519 write_c0_status(sr);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001520 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001521 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001522 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001523 if (cpu_has_divec) {
1524 if (cpu_has_mipsmt) {
1525 unsigned int vpflags = dvpe();
1526 set_c0_cause(CAUSEF_IV);
1527 evpe(vpflags);
1528 } else
1529 set_c0_cause(CAUSEF_IV);
1530 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001531
1532 /*
1533 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1534 *
1535 * o read IntCtl.IPTI to determine the timer interrupt
1536 * o read IntCtl.IPPCI to determine the performance counter interrupt
1537 */
1538 if (cpu_has_mips_r2) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001539 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1540 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001541 if (cp0_perfcount_irq == cp0_compare_irq)
1542 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001543 } else {
1544 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001545 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001546 }
1547
Ralf Baechle41c594a2006-04-05 09:45:45 +01001548#ifdef CONFIG_MIPS_MT_SMTC
1549 }
1550#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1553 TLBMISS_HANDLER_SETUP();
1554
1555 atomic_inc(&init_mm.mm_count);
1556 current->active_mm = &init_mm;
1557 BUG_ON(current->mm);
1558 enter_lazy_tlb(&init_mm, current);
1559
Ralf Baechle41c594a2006-04-05 09:45:45 +01001560#ifdef CONFIG_MIPS_MT_SMTC
1561 if (bootTC) {
1562#endif /* CONFIG_MIPS_MT_SMTC */
1563 cpu_cache_init();
1564 tlb_init();
1565#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001566 } else if (!secondaryTC) {
1567 /*
1568 * First TC in non-boot VPE must do subset of tlb_init()
1569 * for MMU countrol registers.
1570 */
1571 write_c0_pagemask(PM_DEFAULT_MASK);
1572 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001573 }
1574#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Ralf Baechlee01402b2005-07-14 15:57:16 +00001577/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001578void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001579{
1580 memcpy((void *)(ebase + offset), addr, size);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001581 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001582}
1583
Ralf Baechle234fcd12008-03-08 09:56:28 +00001584static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001585 "Trying to set NULL cache error exception handler";
1586
Ralf Baechle42fe7ee2009-01-28 18:48:23 +00001587/*
1588 * Install uncached CPU exception handler.
1589 * This is suitable only for the cache error exception which is the only
1590 * exception handler that is being run uncached.
1591 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001592void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1593 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001594{
1595#ifdef CONFIG_32BIT
1596 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1597#endif
1598#ifdef CONFIG_64BIT
1599 unsigned long uncached_ebase = TO_UNCAC(ebase);
1600#endif
1601
Ralf Baechle641e97f2007-10-11 23:46:05 +01001602 if (!addr)
1603 panic(panic_null_cerr);
1604
Ralf Baechlee01402b2005-07-14 15:57:16 +00001605 memcpy((void *)(uncached_ebase + offset), addr, size);
1606}
1607
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001608static int __initdata rdhwr_noopt;
1609static int __init set_rdhwr_noopt(char *str)
1610{
1611 rdhwr_noopt = 1;
1612 return 1;
1613}
1614
1615__setup("rdhwr_noopt", set_rdhwr_noopt);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617void __init trap_init(void)
1618{
1619 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 extern char except_vec4;
1621 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001622 int rollback;
1623
1624 check_wait();
1625 rollback = (cpu_wait == r4k_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jason Wessel88547002008-07-29 15:58:53 -05001627#if defined(CONFIG_KGDB)
1628 if (kgdb_early_setup)
1629 return; /* Already done */
1630#endif
1631
Chris Dearman9fb4c2b2009-03-20 15:33:55 -07001632 if (cpu_has_veic || cpu_has_vint) {
1633 unsigned long size = 0x200 + VECTORSPACING*64;
1634 ebase = (unsigned long)
1635 __alloc_bootmem(size, 1 << fls(size), 0);
1636 } else {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001637 ebase = CAC_BASE;
David Daney566f74f2008-10-23 17:56:35 -07001638 if (cpu_has_mips_r2)
1639 ebase += (read_c0_ebase() & 0x3ffff000);
1640 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 per_cpu_trap_init();
1643
1644 /*
1645 * Copy the generic exception handlers to their final destination.
1646 * This will be overriden later as suitable for a particular
1647 * configuration.
1648 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001649 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /*
1652 * Setup default vectors
1653 */
1654 for (i = 0; i <= 31; i++)
1655 set_except_vector(i, handle_reserved);
1656
1657 /*
1658 * Copy the EJTAG debug exception vector handler code to it's final
1659 * destination.
1660 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001661 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001662 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /*
1665 * Only some CPUs have the watch exceptions.
1666 */
1667 if (cpu_has_watch)
1668 set_except_vector(23, handle_watch);
1669
1670 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001671 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001673 if (cpu_has_veic || cpu_has_vint) {
1674 int nvec = cpu_has_veic ? 64 : 8;
1675 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001676 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001677 }
1678 else if (cpu_has_divec)
1679 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /*
1682 * Some CPUs can enable/disable for cache parity detection, but does
1683 * it different ways.
1684 */
1685 parity_protection_init();
1686
1687 /*
1688 * The Data Bus Errors / Instruction Bus Errors are signaled
1689 * by external hardware. Therefore these two exceptions
1690 * may have board specific handlers.
1691 */
1692 if (board_be_init)
1693 board_be_init();
1694
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001695 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 set_except_vector(1, handle_tlbm);
1697 set_except_vector(2, handle_tlbl);
1698 set_except_vector(3, handle_tlbs);
1699
1700 set_except_vector(4, handle_adel);
1701 set_except_vector(5, handle_ades);
1702
1703 set_except_vector(6, handle_ibe);
1704 set_except_vector(7, handle_dbe);
1705
1706 set_except_vector(8, handle_sys);
1707 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001708 set_except_vector(10, rdhwr_noopt ? handle_ri :
1709 (cpu_has_vtag_icache ?
1710 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 set_except_vector(11, handle_cpu);
1712 set_except_vector(12, handle_ov);
1713 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Ralf Baechle10cc3522007-10-11 23:46:15 +01001715 if (current_cpu_type() == CPU_R6000 ||
1716 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /*
1718 * The R6000 is the only R-series CPU that features a machine
1719 * check exception (similar to the R4000 cache error) and
1720 * unaligned ldc1/sdc1 exception. The handlers have not been
1721 * written yet. Well, anyway there is no R6000 machine on the
1722 * current list of targets for Linux/MIPS.
1723 * (Duh, crap, there is someone with a triple R6k machine)
1724 */
1725 //set_except_vector(14, handle_mc);
1726 //set_except_vector(15, handle_ndc);
1727 }
1728
Ralf Baechlee01402b2005-07-14 15:57:16 +00001729
1730 if (board_nmi_handler_setup)
1731 board_nmi_handler_setup();
1732
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001733 if (cpu_has_fpu && !cpu_has_nofpuex)
1734 set_except_vector(15, handle_fpe);
1735
1736 set_except_vector(22, handle_mdmx);
1737
1738 if (cpu_has_mcheck)
1739 set_except_vector(24, handle_mcheck);
1740
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001741 if (cpu_has_mipsmt)
1742 set_except_vector(25, handle_mt);
1743
Chris Dearmanacaec422007-05-24 22:30:18 +01001744 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001745
1746 if (cpu_has_vce)
1747 /* Special exception: R4[04]00 uses also the divec space. */
David Daney566f74f2008-10-23 17:56:35 -07001748 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001749 else if (cpu_has_4kex)
David Daney566f74f2008-10-23 17:56:35 -07001750 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001751 else
David Daney566f74f2008-10-23 17:56:35 -07001752 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 signal_init();
1755#ifdef CONFIG_MIPS32_COMPAT
1756 signal32_init();
1757#endif
1758
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001759 local_flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001760 flush_tlb_handlers();
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02001761
1762 sort_extable(__start___dbe_table, __stop___dbe_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}