blob: 353056110f2b6cca2865bc17a187eaf10806572d [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 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090051extern void check_wait(void);
52extern asmlinkage void r4k_wait(void);
53extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010054extern asmlinkage void handle_int(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055extern asmlinkage void handle_tlbm(void);
56extern asmlinkage void handle_tlbl(void);
57extern asmlinkage void handle_tlbs(void);
58extern asmlinkage void handle_adel(void);
59extern asmlinkage void handle_ades(void);
60extern asmlinkage void handle_ibe(void);
61extern asmlinkage void handle_dbe(void);
62extern asmlinkage void handle_sys(void);
63extern asmlinkage void handle_bp(void);
64extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090065extern asmlinkage void handle_ri_rdhwr_vivt(void);
66extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067extern asmlinkage void handle_cpu(void);
68extern asmlinkage void handle_ov(void);
69extern asmlinkage void handle_tr(void);
70extern asmlinkage void handle_fpe(void);
71extern asmlinkage void handle_mdmx(void);
72extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000073extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000074extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075extern asmlinkage void handle_mcheck(void);
76extern asmlinkage void handle_reserved(void);
77
Ralf Baechle12616ed2005-10-18 10:26:46 +010078extern int fpu_emulator_cop1Handler(struct pt_regs *xcp,
Atsushi Nemotoe04582b2006-10-09 00:10:01 +090079 struct mips_fpu_struct *ctx, int has_fpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81void (*board_be_init)(void);
82int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000083void (*board_nmi_handler_setup)(void);
84void (*board_ejtag_handler_setup)(void);
85void (*board_bind_eic_interrupt)(int irq, int regset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Franck Bui-Huu4d157d52006-08-03 09:29:21 +020088static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +090089{
Ralf Baechle39b8d522008-04-28 17:14:26 +010090 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +090091 unsigned long addr;
92
93 printk("Call Trace:");
94#ifdef CONFIG_KALLSYMS
95 printk("\n");
96#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +020097 while (!kstack_end(sp)) {
98 unsigned long __user *p =
99 (unsigned long __user *)(unsigned long)sp++;
100 if (__get_user(addr, p)) {
101 printk(" (Bad stack address)");
102 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100103 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200104 if (__kernel_text_address(addr))
105 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900106 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200107 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900108}
109
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900110#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900111int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900112static int __init set_raw_show_trace(char *str)
113{
114 raw_show_trace = 1;
115 return 1;
116}
117__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900118#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200119
Ralf Baechleeae23f22007-10-14 23:27:21 +0100120static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900121{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200122 unsigned long sp = regs->regs[29];
123 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900124 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900125
126 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200127 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900128 return;
129 }
130 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200131 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200132 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900133 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200134 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900135 printk("\n");
136}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * This routine abuses get_user()/put_user() to reference pointers
140 * with at least a bit of error checking ...
141 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100142static void show_stacktrace(struct task_struct *task,
143 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 const int field = 2 * sizeof(unsigned long);
146 long stackdata;
147 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900148 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 printk("Stack :");
151 i = 0;
152 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
153 if (i && ((i % (64 / field)) == 0))
154 printk("\n ");
155 if (i > 39) {
156 printk(" ...");
157 break;
158 }
159
160 if (__get_user(stackdata, sp++)) {
161 printk(" (Bad stack address)");
162 break;
163 }
164
165 printk(" %0*lx", field, stackdata);
166 i++;
167 }
168 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200169 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900170}
171
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900172void show_stack(struct task_struct *task, unsigned long *sp)
173{
174 struct pt_regs regs;
175 if (sp) {
176 regs.regs[29] = (unsigned long)sp;
177 regs.regs[31] = 0;
178 regs.cp0_epc = 0;
179 } else {
180 if (task && task != current) {
181 regs.regs[29] = task->thread.reg29;
182 regs.regs[31] = 0;
183 regs.cp0_epc = task->thread.reg31;
184 } else {
185 prepare_frametrace(&regs);
186 }
187 }
188 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
192 * The architecture-independent dump_stack generator
193 */
194void dump_stack(void)
195{
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200196 struct pt_regs regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Franck Bui-Huu1666a6f2006-08-03 09:29:19 +0200198 prepare_frametrace(&regs);
199 show_backtrace(current, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202EXPORT_SYMBOL(dump_stack);
203
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900204static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100207 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 printk("\nCode:");
210
Ralf Baechle39b8d522008-04-28 17:14:26 +0100211 if ((unsigned long)pc & 1)
212 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 for(i = -3 ; i < 6 ; i++) {
214 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100215 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printk(" (Bad address in epc)\n");
217 break;
218 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100219 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221}
222
Ralf Baechleeae23f22007-10-14 23:27:21 +0100223static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 const int field = 2 * sizeof(unsigned long);
226 unsigned int cause = regs->cp0_cause;
227 int i;
228
229 printk("Cpu %d\n", smp_processor_id());
230
231 /*
232 * Saved main processor registers
233 */
234 for (i = 0; i < 32; ) {
235 if ((i % 4) == 0)
236 printk("$%2d :", i);
237 if (i == 0)
238 printk(" %0*lx", field, 0UL);
239 else if (i == 26 || i == 27)
240 printk(" %*s", field, "");
241 else
242 printk(" %0*lx", field, regs->regs[i]);
243
244 i++;
245 if ((i % 4) == 0)
246 printk("\n");
247 }
248
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100249#ifdef CONFIG_CPU_HAS_SMARTMIPS
250 printk("Acx : %0*lx\n", field, regs->acx);
251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 printk("Hi : %0*lx\n", field, regs->hi);
253 printk("Lo : %0*lx\n", field, regs->lo);
254
255 /*
256 * Saved cp0 registers
257 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100258 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
259 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100261 printk("ra : %0*lx %pS\n", field, regs->regs[31],
262 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 printk("Status: %08x ", (uint32_t) regs->cp0_status);
265
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000266 if (current_cpu_data.isa_level == MIPS_CPU_ISA_I) {
267 if (regs->cp0_status & ST0_KUO)
268 printk("KUo ");
269 if (regs->cp0_status & ST0_IEO)
270 printk("IEo ");
271 if (regs->cp0_status & ST0_KUP)
272 printk("KUp ");
273 if (regs->cp0_status & ST0_IEP)
274 printk("IEp ");
275 if (regs->cp0_status & ST0_KUC)
276 printk("KUc ");
277 if (regs->cp0_status & ST0_IEC)
278 printk("IEc ");
279 } else {
280 if (regs->cp0_status & ST0_KX)
281 printk("KX ");
282 if (regs->cp0_status & ST0_SX)
283 printk("SX ");
284 if (regs->cp0_status & ST0_UX)
285 printk("UX ");
286 switch (regs->cp0_status & ST0_KSU) {
287 case KSU_USER:
288 printk("USER ");
289 break;
290 case KSU_SUPERVISOR:
291 printk("SUPERVISOR ");
292 break;
293 case KSU_KERNEL:
294 printk("KERNEL ");
295 break;
296 default:
297 printk("BAD_MODE ");
298 break;
299 }
300 if (regs->cp0_status & ST0_ERL)
301 printk("ERL ");
302 if (regs->cp0_status & ST0_EXL)
303 printk("EXL ");
304 if (regs->cp0_status & ST0_IE)
305 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 printk("\n");
308
309 printk("Cause : %08x\n", cause);
310
311 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
312 if (1 <= cause && cause <= 5)
313 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
314
Ralf Baechle9966db252007-10-11 23:46:17 +0100315 printk("PrId : %08x (%s)\n", read_c0_prid(),
316 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Ralf Baechleeae23f22007-10-14 23:27:21 +0100319/*
320 * FIXME: really the generic show_regs should take a const pointer argument.
321 */
322void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100324 __show_regs((struct pt_regs *)regs);
325}
326
327void show_registers(const struct pt_regs *regs)
328{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100329 const int field = 2 * sizeof(unsigned long);
330
Ralf Baechleeae23f22007-10-14 23:27:21 +0100331 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100333 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
334 current->comm, current->pid, current_thread_info(), current,
335 field, current_thread_info()->tp_value);
336 if (cpu_has_userlocal) {
337 unsigned long tls;
338
339 tls = read_c0_userlocal();
340 if (tls != current_thread_info()->tp_value)
341 printk("*HwTLS: %0*lx\n", field, tls);
342 }
343
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900344 show_stacktrace(current, regs);
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900345 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 printk("\n");
347}
348
349static DEFINE_SPINLOCK(die_lock);
350
Ralf Baechleeae23f22007-10-14 23:27:21 +0100351void __noreturn die(const char * str, const struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 static int die_counter;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100354#ifdef CONFIG_MIPS_MT_SMTC
355 unsigned long dvpret = dvpe();
356#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 console_verbose();
359 spin_lock_irq(&die_lock);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100360 bust_spinlocks(1);
361#ifdef CONFIG_MIPS_MT_SMTC
362 mips_mt_regdump(dvpret);
363#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle178086c2005-10-13 17:07:54 +0100364 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 show_registers(regs);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700366 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200368
369 if (in_interrupt())
370 panic("Fatal exception in interrupt");
371
372 if (panic_on_oops) {
373 printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
374 ssleep(5);
375 panic("Fatal exception");
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 do_exit(SIGSEGV);
379}
380
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200381extern struct exception_table_entry __start___dbe_table[];
382extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000384__asm__(
385" .section __dbe_table, \"a\"\n"
386" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/* Given an address, look for it in the exception tables. */
389static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
390{
391 const struct exception_table_entry *e;
392
393 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
394 if (!e)
395 e = search_module_dbetables(addr);
396 return e;
397}
398
399asmlinkage void do_be(struct pt_regs *regs)
400{
401 const int field = 2 * sizeof(unsigned long);
402 const struct exception_table_entry *fixup = NULL;
403 int data = regs->cp0_cause & 4;
404 int action = MIPS_BE_FATAL;
405
406 /* XXX For now. Fixme, this searches the wrong table ... */
407 if (data && !user_mode(regs))
408 fixup = search_dbe_tables(exception_epc(regs));
409
410 if (fixup)
411 action = MIPS_BE_FIXUP;
412
413 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900414 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 switch (action) {
417 case MIPS_BE_DISCARD:
418 return;
419 case MIPS_BE_FIXUP:
420 if (fixup) {
421 regs->cp0_epc = fixup->nextinsn;
422 return;
423 }
424 break;
425 default:
426 break;
427 }
428
429 /*
430 * Assume it would be too dangerous to continue ...
431 */
432 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
433 data ? "Data" : "Instruction",
434 field, regs->cp0_epc, field, regs->regs[31]);
Jason Wessel88547002008-07-29 15:58:53 -0500435 if (notify_die(DIE_OOPS, "bus error", regs, SIGBUS, 0, 0)
436 == NOTIFY_STOP)
437 return;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 die_if_kernel("Oops", regs);
440 force_sig(SIGBUS, current);
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100444 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
446
447#define OPCODE 0xfc000000
448#define BASE 0x03e00000
449#define RT 0x001f0000
450#define OFFSET 0x0000ffff
451#define LL 0xc0000000
452#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100453#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000454#define SPEC3 0x7c000000
455#define RD 0x0000f800
456#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100457#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000458#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/*
461 * The ll_bit is cleared by r*_switch.S
462 */
463
464unsigned long ll_bit;
465
466static struct task_struct *ll_task = NULL;
467
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100468static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000470 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * analyse the ll instruction that just caused a ri exception
475 * and put the referenced address to addr.
476 */
477
478 /* sign extend offset */
479 offset = opcode & OFFSET;
480 offset <<= 16;
481 offset >>= 16;
482
Ralf Baechlefe00f942005-03-01 19:22:29 +0000483 vaddr = (unsigned long __user *)
484 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100486 if ((unsigned long)vaddr & 3)
487 return SIGBUS;
488 if (get_user(value, vaddr))
489 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 preempt_disable();
492
493 if (ll_task == NULL || ll_task == current) {
494 ll_bit = 1;
495 } else {
496 ll_bit = 0;
497 }
498 ll_task = current;
499
500 preempt_enable();
501
502 regs->regs[(opcode & RT) >> 16] = value;
503
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100507static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000509 unsigned long __user *vaddr;
510 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * analyse the sc instruction that just caused a ri exception
515 * and put the referenced address to addr.
516 */
517
518 /* sign extend offset */
519 offset = opcode & OFFSET;
520 offset <<= 16;
521 offset >>= 16;
522
Ralf Baechlefe00f942005-03-01 19:22:29 +0000523 vaddr = (unsigned long __user *)
524 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 reg = (opcode & RT) >> 16;
526
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100527 if ((unsigned long)vaddr & 3)
528 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 preempt_disable();
531
532 if (ll_bit == 0 || ll_task != current) {
533 regs->regs[reg] = 0;
534 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 preempt_enable();
539
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100540 if (put_user(regs->regs[reg], vaddr))
541 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 regs->regs[reg] = 1;
544
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/*
549 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
550 * opcodes are supposed to result in coprocessor unusable exceptions if
551 * executed on ll/sc-less processors. That's the theory. In practice a
552 * few processors such as NEC's VR4100 throw reserved instruction exceptions
553 * instead, so we're doing the emulation thing in both exception handlers.
554 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100555static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100557 if ((opcode & OPCODE) == LL)
558 return simulate_ll(regs, opcode);
559 if ((opcode & OPCODE) == SC)
560 return simulate_sc(regs, opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100562 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Ralf Baechle3c370262005-04-13 17:43:59 +0000565/*
566 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100567 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000568 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100569static int simulate_rdhwr(struct pt_regs *regs, unsigned int opcode)
Ralf Baechle3c370262005-04-13 17:43:59 +0000570{
Al Virodc8f6022006-01-12 01:06:07 -0800571 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000572
573 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
574 int rd = (opcode & RD) >> 11;
575 int rt = (opcode & RT) >> 16;
576 switch (rd) {
Chris Dearman1f5826b2006-05-08 18:02:16 +0100577 case 0: /* CPU number */
578 regs->regs[rt] = smp_processor_id();
579 return 0;
580 case 1: /* SYNCI length */
581 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
582 current_cpu_data.icache.linesz);
583 return 0;
584 case 2: /* Read count register */
585 regs->regs[rt] = read_c0_count();
586 return 0;
587 case 3: /* Count register resolution */
588 switch (current_cpu_data.cputype) {
589 case CPU_20KC:
590 case CPU_25KF:
591 regs->regs[rt] = 1;
592 break;
Ralf Baechle3c370262005-04-13 17:43:59 +0000593 default:
Chris Dearman1f5826b2006-05-08 18:02:16 +0100594 regs->regs[rt] = 2;
595 }
596 return 0;
597 case 29:
598 regs->regs[rt] = ti->tp_value;
599 return 0;
600 default:
601 return -1;
Ralf Baechle3c370262005-04-13 17:43:59 +0000602 }
603 }
604
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500605 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100606 return -1;
607}
Ralf Baechlee5679882006-11-30 01:14:47 +0000608
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100609static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
610{
611 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC)
612 return 0;
613
614 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617asmlinkage void do_ov(struct pt_regs *regs)
618{
619 siginfo_t info;
620
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000621 die_if_kernel("Integer overflow", regs);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 info.si_code = FPE_INTOVF;
624 info.si_signo = SIGFPE;
625 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000626 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 force_sig_info(SIGFPE, &info, current);
628}
629
630/*
631 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
632 */
633asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
634{
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100635 siginfo_t info;
636
Jason Wessel88547002008-07-29 15:58:53 -0500637 if (notify_die(DIE_FP, "FP exception", regs, SIGFPE, 0, 0)
638 == NOTIFY_STOP)
639 return;
Chris Dearman57725f92006-06-30 23:35:28 +0100640 die_if_kernel("FP exception in kernel code", regs);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (fcr31 & FPU_CSR_UNI_X) {
643 int sig;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000646 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * software emulator on-board, let's use it...
648 *
649 * Force FPU to dump state into task/thread context. We're
650 * moving a lot of data here for what is probably a single
651 * instruction, but the alternative is to pre-decode the FP
652 * register operands before invoking the emulator, which seems
653 * a bit extreme for what should be an infrequent event.
654 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000655 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900656 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Run the emulator */
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100659 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
662 * We can't allow the emulated instruction to leave any of
663 * the cause bit set in $fcr31.
664 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900665 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* Restore the hardware register state */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900668 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /* If something went wrong, signal */
671 if (sig)
672 force_sig(sig, current);
673
674 return;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100675 } else if (fcr31 & FPU_CSR_INV_X)
676 info.si_code = FPE_FLTINV;
677 else if (fcr31 & FPU_CSR_DIV_X)
678 info.si_code = FPE_FLTDIV;
679 else if (fcr31 & FPU_CSR_OVF_X)
680 info.si_code = FPE_FLTOVF;
681 else if (fcr31 & FPU_CSR_UDF_X)
682 info.si_code = FPE_FLTUND;
683 else if (fcr31 & FPU_CSR_INE_X)
684 info.si_code = FPE_FLTRES;
685 else
686 info.si_code = __SI_FAULT;
687 info.si_signo = SIGFPE;
688 info.si_errno = 0;
689 info.si_addr = (void __user *) regs->cp0_epc;
690 force_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Ralf Baechledf270052008-04-20 16:28:54 +0100693static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
694 const char *str)
695{
696 siginfo_t info;
697 char b[40];
698
Jason Wessel88547002008-07-29 15:58:53 -0500699 if (notify_die(DIE_TRAP, str, regs, code, 0, 0) == NOTIFY_STOP)
700 return;
701
Ralf Baechledf270052008-04-20 16:28:54 +0100702 /*
703 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
704 * insns, even for trap and break codes that indicate arithmetic
705 * failures. Weird ...
706 * But should we continue the brokenness??? --macro
707 */
708 switch (code) {
709 case BRK_OVERFLOW:
710 case BRK_DIVZERO:
711 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
712 die_if_kernel(b, regs);
713 if (code == BRK_DIVZERO)
714 info.si_code = FPE_INTDIV;
715 else
716 info.si_code = FPE_INTOVF;
717 info.si_signo = SIGFPE;
718 info.si_errno = 0;
719 info.si_addr = (void __user *) regs->cp0_epc;
720 force_sig_info(SIGFPE, &info, current);
721 break;
722 case BRK_BUG:
723 die_if_kernel("Kernel bug detected", regs);
724 force_sig(SIGTRAP, current);
725 break;
Ralf Baechleba3049e2008-10-28 17:38:42 +0000726 case BRK_MEMU:
727 /*
728 * Address errors may be deliberately induced by the FPU
729 * emulator to retake control of the CPU after executing the
730 * instruction in the delay slot of an emulated branch.
731 *
732 * Terminate if exception was recognized as a delay slot return
733 * otherwise handle as normal.
734 */
735 if (do_dsemulret(regs))
736 return;
737
738 die_if_kernel("Math emu break/trap", regs);
739 force_sig(SIGTRAP, current);
740 break;
Ralf Baechledf270052008-04-20 16:28:54 +0100741 default:
742 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
743 die_if_kernel(b, regs);
744 force_sig(SIGTRAP, current);
745 }
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748asmlinkage void do_bp(struct pt_regs *regs)
749{
750 unsigned int opcode, bcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900752 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000753 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * There is the ancient bug in the MIPS assemblers that the break
757 * code starts left to bit 16 instead to bit 6 in the opcode.
758 * Gas is bug-compatible, but not always, grrr...
759 * We handle both cases with a simple heuristics. --macro
760 */
761 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100762 if (bcode >= (1 << 10))
763 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ralf Baechledf270052008-04-20 16:28:54 +0100765 do_trap_or_bp(regs, bcode, "Break");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900766 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000767
768out_sigsegv:
769 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772asmlinkage void do_tr(struct pt_regs *regs)
773{
774 unsigned int opcode, tcode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Atsushi Nemotoba755f82007-04-12 20:02:54 +0900776 if (__get_user(opcode, (unsigned int __user *) exception_epc(regs)))
Ralf Baechlee5679882006-11-30 01:14:47 +0000777 goto out_sigsegv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* Immediate versions don't provide a code. */
780 if (!(opcode & OPCODE))
781 tcode = ((opcode >> 6) & ((1 << 10) - 1));
782
Ralf Baechledf270052008-04-20 16:28:54 +0100783 do_trap_or_bp(regs, tcode, "Trap");
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900784 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000785
786out_sigsegv:
787 force_sig(SIGSEGV, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790asmlinkage void do_ri(struct pt_regs *regs)
791{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100792 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
793 unsigned long old_epc = regs->cp0_epc;
794 unsigned int opcode = 0;
795 int status = -1;
796
Jason Wessel88547002008-07-29 15:58:53 -0500797 if (notify_die(DIE_RI, "RI Fault", regs, SIGSEGV, 0, 0)
798 == NOTIFY_STOP)
799 return;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 die_if_kernel("Reserved instruction in kernel code", regs);
802
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100803 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechle3c370262005-04-13 17:43:59 +0000804 return;
805
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100806 if (unlikely(get_user(opcode, epc) < 0))
807 status = SIGSEGV;
808
809 if (!cpu_has_llsc && status < 0)
810 status = simulate_llsc(regs, opcode);
811
812 if (status < 0)
813 status = simulate_rdhwr(regs, opcode);
814
815 if (status < 0)
816 status = simulate_sync(regs, opcode);
817
818 if (status < 0)
819 status = SIGILL;
820
821 if (unlikely(status > 0)) {
822 regs->cp0_epc = old_epc; /* Undo skip-over. */
823 force_sig(status, current);
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Ralf Baechled223a862007-07-10 17:33:02 +0100827/*
828 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
829 * emulated more than some threshold number of instructions, force migration to
830 * a "CPU" that has FP support.
831 */
832static void mt_ase_fp_affinity(void)
833{
834#ifdef CONFIG_MIPS_MT_FPAFF
835 if (mt_fpemul_threshold > 0 &&
836 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
837 /*
838 * If there's no FPU present, or if the application has already
839 * restricted the allowed set to exclude any CPUs with FPUs,
840 * we'll skip the procedure.
841 */
842 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
843 cpumask_t tmask;
844
Kevin D. Kissell9cc12362008-09-09 21:33:36 +0200845 current->thread.user_cpus_allowed
846 = current->cpus_allowed;
847 cpus_and(tmask, current->cpus_allowed,
848 mt_fpu_cpumask);
Ralf Baechled223a862007-07-10 17:33:02 +0100849 set_cpus_allowed(current, tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +0100850 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +0100851 }
852 }
853#endif /* CONFIG_MIPS_MT_FPAFF */
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856asmlinkage void do_cpu(struct pt_regs *regs)
857{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100858 unsigned int __user *epc;
859 unsigned long old_epc;
860 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int cpid;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100862 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Atsushi Nemoto53231802007-04-14 02:37:26 +0900864 die_if_kernel("do_cpu invoked from kernel context!", regs);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
867
868 switch (cpid) {
869 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100870 epc = (unsigned int __user *)exception_epc(regs);
871 old_epc = regs->cp0_epc;
872 opcode = 0;
873 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100875 if (unlikely(compute_return_epc(regs) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return;
Ralf Baechle3c370262005-04-13 17:43:59 +0000877
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100878 if (unlikely(get_user(opcode, epc) < 0))
879 status = SIGSEGV;
880
881 if (!cpu_has_llsc && status < 0)
882 status = simulate_llsc(regs, opcode);
883
884 if (status < 0)
885 status = simulate_rdhwr(regs, opcode);
886
887 if (status < 0)
888 status = SIGILL;
889
890 if (unlikely(status > 0)) {
891 regs->cp0_epc = old_epc; /* Undo skip-over. */
892 force_sig(status, current);
893 }
894
895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 case 1:
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900898 if (used_math()) /* Using the FPU again. */
899 own_fpu(1);
900 else { /* First time FPU user. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 init_fpu();
902 set_used_math();
903 }
904
Atsushi Nemoto53231802007-04-14 02:37:26 +0900905 if (!raw_cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900906 int sig;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900907 sig = fpu_emulator_cop1Handler(regs,
908 &current->thread.fpu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (sig)
910 force_sig(sig, current);
Ralf Baechled223a862007-07-10 17:33:02 +0100911 else
912 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return;
916
917 case 2:
918 case 3:
919 break;
920 }
921
922 force_sig(SIGILL, current);
923}
924
925asmlinkage void do_mdmx(struct pt_regs *regs)
926{
927 force_sig(SIGILL, current);
928}
929
930asmlinkage void do_watch(struct pt_regs *regs)
931{
David Daneyb67b2b72008-09-23 00:08:45 -0700932 u32 cause;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /*
David Daneyb67b2b72008-09-23 00:08:45 -0700935 * Clear WP (bit 22) bit of cause register so we don't loop
936 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
David Daneyb67b2b72008-09-23 00:08:45 -0700938 cause = read_c0_cause();
939 cause &= ~(1 << 22);
940 write_c0_cause(cause);
941
942 /*
943 * If the current thread has the watch registers loaded, save
944 * their values and send SIGTRAP. Otherwise another thread
945 * left the registers set, clear them and continue.
946 */
947 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
948 mips_read_watch_registers();
949 force_sig(SIGTRAP, current);
950 } else
951 mips_clear_watch_registers();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
954asmlinkage void do_mcheck(struct pt_regs *regs)
955{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100956 const int field = 2 * sizeof(unsigned long);
957 int multi_match = regs->cp0_status & ST0_TS;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100960
961 if (multi_match) {
962 printk("Index : %0x\n", read_c0_index());
963 printk("Pagemask: %0x\n", read_c0_pagemask());
964 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
965 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
966 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
967 printk("\n");
968 dump_tlb_all();
969 }
970
Atsushi Nemotoe1bb8282007-07-13 23:51:46 +0900971 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /*
974 * Some chips may have other causes of machine check (e.g. SB1
975 * graduation timer)
976 */
977 panic("Caught Machine Check exception - %scaused by multiple "
978 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +0100979 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Ralf Baechle340ee4b2005-08-17 17:44:08 +0000982asmlinkage void do_mt(struct pt_regs *regs)
983{
Ralf Baechle41c594a2006-04-05 09:45:45 +0100984 int subcode;
985
Ralf Baechle41c594a2006-04-05 09:45:45 +0100986 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
987 >> VPECONTROL_EXCPT_SHIFT;
988 switch (subcode) {
989 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100990 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100991 break;
992 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100993 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100994 break;
995 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100996 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +0100997 break;
998 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +0100999 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001000 break;
1001 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001002 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001003 break;
1004 case 5:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001005 printk(KERN_DEBUG "Gating Storage Schedulier Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001006 break;
1007 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001008 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +01001009 subcode);
1010 break;
1011 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001012 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1013
1014 force_sig(SIGILL, current);
1015}
1016
1017
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001018asmlinkage void do_dsp(struct pt_regs *regs)
1019{
1020 if (cpu_has_dsp)
1021 panic("Unexpected DSP exception\n");
1022
1023 force_sig(SIGILL, current);
1024}
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026asmlinkage void do_reserved(struct pt_regs *regs)
1027{
1028 /*
1029 * Game over - no way to handle this if it ever occurs. Most probably
1030 * caused by a new unknown cpu type or after another deadly
1031 * hard/software error.
1032 */
1033 show_regs(regs);
1034 panic("Caught reserved exception %ld - should not happen.",
1035 (regs->cp0_cause & 0x7f) >> 2);
1036}
1037
Ralf Baechle39b8d522008-04-28 17:14:26 +01001038static int __initdata l1parity = 1;
1039static int __init nol1parity(char *s)
1040{
1041 l1parity = 0;
1042 return 1;
1043}
1044__setup("nol1par", nol1parity);
1045static int __initdata l2parity = 1;
1046static int __init nol2parity(char *s)
1047{
1048 l2parity = 0;
1049 return 1;
1050}
1051__setup("nol2par", nol2parity);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1055 * it different ways.
1056 */
1057static inline void parity_protection_init(void)
1058{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001059 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001061 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001062 case CPU_74K:
1063 case CPU_1004K:
1064 {
1065#define ERRCTL_PE 0x80000000
1066#define ERRCTL_L2P 0x00800000
1067 unsigned long errctl;
1068 unsigned int l1parity_present, l2parity_present;
1069
1070 errctl = read_c0_ecc();
1071 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1072
1073 /* probe L1 parity support */
1074 write_c0_ecc(errctl | ERRCTL_PE);
1075 back_to_back_c0_hazard();
1076 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1077
1078 /* probe L2 parity support */
1079 write_c0_ecc(errctl|ERRCTL_L2P);
1080 back_to_back_c0_hazard();
1081 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1082
1083 if (l1parity_present && l2parity_present) {
1084 if (l1parity)
1085 errctl |= ERRCTL_PE;
1086 if (l1parity ^ l2parity)
1087 errctl |= ERRCTL_L2P;
1088 } else if (l1parity_present) {
1089 if (l1parity)
1090 errctl |= ERRCTL_PE;
1091 } else if (l2parity_present) {
1092 if (l2parity)
1093 errctl |= ERRCTL_L2P;
1094 } else {
1095 /* No parity available */
1096 }
1097
1098 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1099
1100 write_c0_ecc(errctl);
1101 back_to_back_c0_hazard();
1102 errctl = read_c0_ecc();
1103 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1104
1105 if (l1parity_present)
1106 printk(KERN_INFO "Cache parity protection %sabled\n",
1107 (errctl & ERRCTL_PE) ? "en" : "dis");
1108
1109 if (l2parity_present) {
1110 if (l1parity_present && l1parity)
1111 errctl ^= ERRCTL_L2P;
1112 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1113 (errctl & ERRCTL_L2P) ? "en" : "dis");
1114 }
1115 }
1116 break;
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 case CPU_5KC:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001119 write_c0_ecc(0x80000000);
1120 back_to_back_c0_hazard();
1121 /* Set the PE bit (bit 31) in the c0_errctl register. */
1122 printk(KERN_INFO "Cache parity protection %sabled\n",
1123 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case CPU_20KC:
1126 case CPU_25KF:
1127 /* Clear the DE bit (bit 16) in the c0_status register. */
1128 printk(KERN_INFO "Enable cache parity protection for "
1129 "MIPS 20KC/25KF CPUs.\n");
1130 clear_c0_status(ST0_DE);
1131 break;
1132 default:
1133 break;
1134 }
1135}
1136
1137asmlinkage void cache_parity_error(void)
1138{
1139 const int field = 2 * sizeof(unsigned long);
1140 unsigned int reg_val;
1141
1142 /* For the moment, report the problem and hang. */
1143 printk("Cache error exception:\n");
1144 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1145 reg_val = read_c0_cacheerr();
1146 printk("c0_cacheerr == %08x\n", reg_val);
1147
1148 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1149 reg_val & (1<<30) ? "secondary" : "primary",
1150 reg_val & (1<<31) ? "data" : "insn");
1151 printk("Error bits: %s%s%s%s%s%s%s\n",
1152 reg_val & (1<<29) ? "ED " : "",
1153 reg_val & (1<<28) ? "ET " : "",
1154 reg_val & (1<<26) ? "EE " : "",
1155 reg_val & (1<<25) ? "EB " : "",
1156 reg_val & (1<<24) ? "EI " : "",
1157 reg_val & (1<<23) ? "E1 " : "",
1158 reg_val & (1<<22) ? "E0 " : "");
1159 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1160
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001161#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (reg_val & (1<<22))
1163 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1164
1165 if (reg_val & (1<<23))
1166 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1167#endif
1168
1169 panic("Can't handle the cache error!");
1170}
1171
1172/*
1173 * SDBBP EJTAG debug exception handler.
1174 * We skip the instruction and return to the next instruction.
1175 */
1176void ejtag_exception_handler(struct pt_regs *regs)
1177{
1178 const int field = 2 * sizeof(unsigned long);
1179 unsigned long depc, old_epc;
1180 unsigned int debug;
1181
Chris Dearman70ae6122006-06-30 12:32:37 +01001182 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 depc = read_c0_depc();
1184 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001185 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (debug & 0x80000000) {
1187 /*
1188 * In branch delay slot.
1189 * We cheat a little bit here and use EPC to calculate the
1190 * debug return address (DEPC). EPC is restored after the
1191 * calculation.
1192 */
1193 old_epc = regs->cp0_epc;
1194 regs->cp0_epc = depc;
1195 __compute_return_epc(regs);
1196 depc = regs->cp0_epc;
1197 regs->cp0_epc = old_epc;
1198 } else
1199 depc += 4;
1200 write_c0_depc(depc);
1201
1202#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001203 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 write_c0_debug(debug | 0x100);
1205#endif
1206}
1207
1208/*
1209 * NMI exception handler.
1210 */
Thiemo Seufer34412c72007-08-20 23:43:49 +01001211NORET_TYPE void ATTRIB_NORET nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001213 bust_spinlocks(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 printk("NMI taken!!!!\n");
1215 die("NMI", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Ralf Baechlee01402b2005-07-14 15:57:16 +00001218#define VECTORSPACING 0x100 /* for EI/VI mode */
1219
1220unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001222unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224/*
1225 * As a side effect of the way this is implemented we're limited
1226 * to interrupt handlers in the address range from
1227 * KSEG0 <= x < KSEG0 + 256mb on the Nevada. Oh well ...
1228 */
1229void *set_except_vector(int n, void *addr)
1230{
1231 unsigned long handler = (unsigned long) addr;
1232 unsigned long old_handler = exception_handlers[n];
1233
1234 exception_handlers[n] = handler;
1235 if (n == 0 && cpu_has_divec) {
Ralf Baechleec70f652007-10-11 23:46:03 +01001236 *(u32 *)(ebase + 0x200) = 0x08000000 |
1237 (0x03ffffff & (handler >> 2));
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001238 local_flush_icache_range(ebase + 0x200, ebase + 0x204);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 return (void *)old_handler;
1241}
1242
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001243static asmlinkage void do_default_vi(void)
1244{
1245 show_regs(get_irq_regs());
1246 panic("Caught unexpected vectored interrupt.");
1247}
1248
Ralf Baechleef300e42007-05-06 18:31:18 +01001249static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001250{
1251 unsigned long handler;
1252 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001253 int srssets = current_cpu_data.srsets;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001254 u32 *w;
1255 unsigned char *b;
1256
1257 if (!cpu_has_veic && !cpu_has_vint)
1258 BUG();
1259
1260 if (addr == NULL) {
1261 handler = (unsigned long) do_default_vi;
1262 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001263 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001264 handler = (unsigned long) addr;
1265 vi_handlers[n] = (unsigned long) addr;
1266
1267 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1268
Ralf Baechlef6771db2007-11-08 18:02:29 +00001269 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001270 panic("Shadow register set %d not supported", srs);
1271
1272 if (cpu_has_veic) {
1273 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001274 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001275 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001276 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001277 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001278 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001279 }
1280
1281 if (srs == 0) {
1282 /*
1283 * If no shadow set is selected then use the default handler
1284 * that does normal register saving and a standard interrupt exit
1285 */
1286
1287 extern char except_vec_vi, except_vec_vi_lui;
1288 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001289 extern char rollback_except_vec_vi;
1290 char *vec_start = (cpu_wait == r4k_wait) ?
1291 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001292#ifdef CONFIG_MIPS_MT_SMTC
1293 /*
1294 * We need to provide the SMTC vectored interrupt handler
1295 * not only with the address of the handler, but with the
1296 * Status.IM bit to be masked before going there.
1297 */
1298 extern char except_vec_vi_mori;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001299 const int mori_offset = &except_vec_vi_mori - vec_start;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001300#endif /* CONFIG_MIPS_MT_SMTC */
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001301 const int handler_len = &except_vec_vi_end - vec_start;
1302 const int lui_offset = &except_vec_vi_lui - vec_start;
1303 const int ori_offset = &except_vec_vi_ori - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001304
1305 if (handler_len > VECTORSPACING) {
1306 /*
1307 * Sigh... panicing won't help as the console
1308 * is probably not configured :(
1309 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001310 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001311 }
1312
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001313 memcpy(b, vec_start, handler_len);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001314#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001315 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1316
Ralf Baechle41c594a2006-04-05 09:45:45 +01001317 w = (u32 *)(b + mori_offset);
1318 *w = (*w & 0xffff0000) | (0x100 << n);
1319#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001320 w = (u32 *)(b + lui_offset);
1321 *w = (*w & 0xffff0000) | (((u32)handler >> 16) & 0xffff);
1322 w = (u32 *)(b + ori_offset);
1323 *w = (*w & 0xffff0000) | ((u32)handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001324 local_flush_icache_range((unsigned long)b,
1325 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001326 }
1327 else {
1328 /*
1329 * In other cases jump directly to the interrupt handler
1330 *
1331 * It is the handlers responsibility to save registers if required
1332 * (eg hi/lo) and return from the exception using "eret"
1333 */
1334 w = (u32 *)b;
1335 *w++ = 0x08000000 | (((u32)handler >> 2) & 0x03fffff); /* j handler */
1336 *w = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001337 local_flush_icache_range((unsigned long)b,
1338 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001339 }
1340
1341 return (void *)old_handler;
1342}
1343
Ralf Baechleef300e42007-05-06 18:31:18 +01001344void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001345{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001346 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001347}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/*
1350 * This is used by native signal handling
1351 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001352asmlinkage int (*save_fp_context)(struct sigcontext __user *sc);
1353asmlinkage int (*restore_fp_context)(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001355extern asmlinkage int _save_fp_context(struct sigcontext __user *sc);
1356extern asmlinkage int _restore_fp_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001358extern asmlinkage int fpu_emulator_save_context(struct sigcontext __user *sc);
1359extern asmlinkage int fpu_emulator_restore_context(struct sigcontext __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Ralf Baechle41c594a2006-04-05 09:45:45 +01001361#ifdef CONFIG_SMP
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001362static int smp_save_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001363{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001364 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001365 ? _save_fp_context(sc)
1366 : fpu_emulator_save_context(sc);
1367}
1368
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001369static int smp_restore_fp_context(struct sigcontext __user *sc)
Ralf Baechle41c594a2006-04-05 09:45:45 +01001370{
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001371 return raw_cpu_has_fpu
Ralf Baechle41c594a2006-04-05 09:45:45 +01001372 ? _restore_fp_context(sc)
1373 : fpu_emulator_restore_context(sc);
1374}
1375#endif
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377static inline void signal_init(void)
1378{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001379#ifdef CONFIG_SMP
1380 /* For now just do the cpu_has_fpu check when the functions are invoked */
1381 save_fp_context = smp_save_fp_context;
1382 restore_fp_context = smp_restore_fp_context;
1383#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (cpu_has_fpu) {
1385 save_fp_context = _save_fp_context;
1386 restore_fp_context = _restore_fp_context;
1387 } else {
1388 save_fp_context = fpu_emulator_save_context;
1389 restore_fp_context = fpu_emulator_restore_context;
1390 }
Ralf Baechle41c594a2006-04-05 09:45:45 +01001391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394#ifdef CONFIG_MIPS32_COMPAT
1395
1396/*
1397 * This is used by 32-bit signal stuff on the 64-bit kernel
1398 */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001399asmlinkage int (*save_fp_context32)(struct sigcontext32 __user *sc);
1400asmlinkage int (*restore_fp_context32)(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001402extern asmlinkage int _save_fp_context32(struct sigcontext32 __user *sc);
1403extern asmlinkage int _restore_fp_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Atsushi Nemoto53dc8022007-03-10 01:07:45 +09001405extern asmlinkage int fpu_emulator_save_context32(struct sigcontext32 __user *sc);
1406extern asmlinkage int fpu_emulator_restore_context32(struct sigcontext32 __user *sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408static inline void signal32_init(void)
1409{
1410 if (cpu_has_fpu) {
1411 save_fp_context32 = _save_fp_context32;
1412 restore_fp_context32 = _restore_fp_context32;
1413 } else {
1414 save_fp_context32 = fpu_emulator_save_context32;
1415 restore_fp_context32 = fpu_emulator_restore_context32;
1416 }
1417}
1418#endif
1419
1420extern void cpu_cache_init(void);
1421extern void tlb_init(void);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001422extern void flush_tlb_handlers(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Ralf Baechle42f77542007-10-18 17:48:11 +01001424/*
1425 * Timer interrupt
1426 */
1427int cp0_compare_irq;
1428
1429/*
1430 * Performance counter IRQ or -1 if shared with timer
1431 */
1432int cp0_perfcount_irq;
1433EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1434
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001435static int __cpuinitdata noulri;
1436
1437static int __init ulri_disable(char *s)
1438{
1439 pr_info("Disabling ulri\n");
1440 noulri = 1;
1441
1442 return 1;
1443}
1444__setup("noulri", ulri_disable);
1445
Ralf Baechle234fcd12008-03-08 09:56:28 +00001446void __cpuinit per_cpu_trap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 unsigned int cpu = smp_processor_id();
1449 unsigned int status_set = ST0_CU0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001450#ifdef CONFIG_MIPS_MT_SMTC
1451 int secondaryTC = 0;
1452 int bootTC = (cpu == 0);
1453
1454 /*
1455 * Only do per_cpu_trap_init() for first TC of Each VPE.
1456 * Note that this hack assumes that the SMTC init code
1457 * assigns TCs consecutively and in ascending order.
1458 */
1459
1460 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1461 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1462 secondaryTC = 1;
1463#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /*
1466 * Disable coprocessors and select 32-bit or 64-bit addressing
1467 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1468 * flag that some firmware may have left set and the TS bit (for
1469 * IP27). Set XX for ISA IV code to work.
1470 */
Ralf Baechle875d43e2005-09-03 15:56:16 -07001471#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1473#endif
1474 if (current_cpu_data.isa_level == MIPS_CPU_ISA_IV)
1475 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001476 if (cpu_has_dsp)
1477 status_set |= ST0_MX;
1478
Ralf Baechleb38c7392006-02-07 01:20:43 +00001479 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 -07001480 status_set);
1481
Ralf Baechlea3692022007-07-10 17:33:02 +01001482 if (cpu_has_mips_r2) {
1483 unsigned int enable = 0x0000000f;
1484
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001485 if (!noulri && cpu_has_userlocal)
Ralf Baechlea3692022007-07-10 17:33:02 +01001486 enable |= (1 << 29);
1487
1488 write_c0_hwrena(enable);
1489 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001490
Ralf Baechle41c594a2006-04-05 09:45:45 +01001491#ifdef CONFIG_MIPS_MT_SMTC
1492 if (!secondaryTC) {
1493#endif /* CONFIG_MIPS_MT_SMTC */
1494
Ralf Baechlee01402b2005-07-14 15:57:16 +00001495 if (cpu_has_veic || cpu_has_vint) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001496 write_c0_ebase(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001497 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001498 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001499 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001500 if (cpu_has_divec) {
1501 if (cpu_has_mipsmt) {
1502 unsigned int vpflags = dvpe();
1503 set_c0_cause(CAUSEF_IV);
1504 evpe(vpflags);
1505 } else
1506 set_c0_cause(CAUSEF_IV);
1507 }
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001508
1509 /*
1510 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1511 *
1512 * o read IntCtl.IPTI to determine the timer interrupt
1513 * o read IntCtl.IPPCI to determine the performance counter interrupt
1514 */
1515 if (cpu_has_mips_r2) {
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001516 cp0_compare_irq = (read_c0_intctl() >> 29) & 7;
1517 cp0_perfcount_irq = (read_c0_intctl() >> 26) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001518 if (cp0_perfcount_irq == cp0_compare_irq)
1519 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001520 } else {
1521 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001522 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001523 }
1524
Ralf Baechle41c594a2006-04-05 09:45:45 +01001525#ifdef CONFIG_MIPS_MT_SMTC
1526 }
1527#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1530 TLBMISS_HANDLER_SETUP();
1531
1532 atomic_inc(&init_mm.mm_count);
1533 current->active_mm = &init_mm;
1534 BUG_ON(current->mm);
1535 enter_lazy_tlb(&init_mm, current);
1536
Ralf Baechle41c594a2006-04-05 09:45:45 +01001537#ifdef CONFIG_MIPS_MT_SMTC
1538 if (bootTC) {
1539#endif /* CONFIG_MIPS_MT_SMTC */
1540 cpu_cache_init();
1541 tlb_init();
1542#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001543 } else if (!secondaryTC) {
1544 /*
1545 * First TC in non-boot VPE must do subset of tlb_init()
1546 * for MMU countrol registers.
1547 */
1548 write_c0_pagemask(PM_DEFAULT_MASK);
1549 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001550 }
1551#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Ralf Baechlee01402b2005-07-14 15:57:16 +00001554/* Install CPU exception handler */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001555void __init set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001556{
1557 memcpy((void *)(ebase + offset), addr, size);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001558 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001559}
1560
Ralf Baechle234fcd12008-03-08 09:56:28 +00001561static char panic_null_cerr[] __cpuinitdata =
Ralf Baechle641e97f2007-10-11 23:46:05 +01001562 "Trying to set NULL cache error exception handler";
1563
Ralf Baechlee01402b2005-07-14 15:57:16 +00001564/* Install uncached CPU exception handler */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001565void __cpuinit set_uncached_handler(unsigned long offset, void *addr,
1566 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001567{
1568#ifdef CONFIG_32BIT
1569 unsigned long uncached_ebase = KSEG1ADDR(ebase);
1570#endif
1571#ifdef CONFIG_64BIT
1572 unsigned long uncached_ebase = TO_UNCAC(ebase);
1573#endif
David Daney566f74f2008-10-23 17:56:35 -07001574 if (cpu_has_mips_r2)
1575 ebase += (read_c0_ebase() & 0x3ffff000);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001576
Ralf Baechle641e97f2007-10-11 23:46:05 +01001577 if (!addr)
1578 panic(panic_null_cerr);
1579
Ralf Baechlee01402b2005-07-14 15:57:16 +00001580 memcpy((void *)(uncached_ebase + offset), addr, size);
1581}
1582
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001583static int __initdata rdhwr_noopt;
1584static int __init set_rdhwr_noopt(char *str)
1585{
1586 rdhwr_noopt = 1;
1587 return 1;
1588}
1589
1590__setup("rdhwr_noopt", set_rdhwr_noopt);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592void __init trap_init(void)
1593{
1594 extern char except_vec3_generic, except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 extern char except_vec4;
1596 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001597 int rollback;
1598
1599 check_wait();
1600 rollback = (cpu_wait == r4k_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Jason Wessel88547002008-07-29 15:58:53 -05001602#if defined(CONFIG_KGDB)
1603 if (kgdb_early_setup)
1604 return; /* Already done */
1605#endif
1606
Ralf Baechlee01402b2005-07-14 15:57:16 +00001607 if (cpu_has_veic || cpu_has_vint)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001608 ebase = (unsigned long) alloc_bootmem_low_pages(0x200 + VECTORSPACING*64);
David Daney566f74f2008-10-23 17:56:35 -07001609 else {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001610 ebase = CAC_BASE;
David Daney566f74f2008-10-23 17:56:35 -07001611 if (cpu_has_mips_r2)
1612 ebase += (read_c0_ebase() & 0x3ffff000);
1613 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 per_cpu_trap_init();
1616
1617 /*
1618 * Copy the generic exception handlers to their final destination.
1619 * This will be overriden later as suitable for a particular
1620 * configuration.
1621 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001622 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 /*
1625 * Setup default vectors
1626 */
1627 for (i = 0; i <= 31; i++)
1628 set_except_vector(i, handle_reserved);
1629
1630 /*
1631 * Copy the EJTAG debug exception vector handler code to it's final
1632 * destination.
1633 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001634 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001635 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
1638 * Only some CPUs have the watch exceptions.
1639 */
1640 if (cpu_has_watch)
1641 set_except_vector(23, handle_watch);
1642
1643 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00001644 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001646 if (cpu_has_veic || cpu_has_vint) {
1647 int nvec = cpu_has_veic ? 64 : 8;
1648 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01001649 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001650 }
1651 else if (cpu_has_divec)
1652 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 /*
1655 * Some CPUs can enable/disable for cache parity detection, but does
1656 * it different ways.
1657 */
1658 parity_protection_init();
1659
1660 /*
1661 * The Data Bus Errors / Instruction Bus Errors are signaled
1662 * by external hardware. Therefore these two exceptions
1663 * may have board specific handlers.
1664 */
1665 if (board_be_init)
1666 board_be_init();
1667
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001668 set_except_vector(0, rollback ? rollback_handle_int : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 set_except_vector(1, handle_tlbm);
1670 set_except_vector(2, handle_tlbl);
1671 set_except_vector(3, handle_tlbs);
1672
1673 set_except_vector(4, handle_adel);
1674 set_except_vector(5, handle_ades);
1675
1676 set_except_vector(6, handle_ibe);
1677 set_except_vector(7, handle_dbe);
1678
1679 set_except_vector(8, handle_sys);
1680 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09001681 set_except_vector(10, rdhwr_noopt ? handle_ri :
1682 (cpu_has_vtag_icache ?
1683 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 set_except_vector(11, handle_cpu);
1685 set_except_vector(12, handle_ov);
1686 set_except_vector(13, handle_tr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Ralf Baechle10cc3522007-10-11 23:46:15 +01001688 if (current_cpu_type() == CPU_R6000 ||
1689 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * The R6000 is the only R-series CPU that features a machine
1692 * check exception (similar to the R4000 cache error) and
1693 * unaligned ldc1/sdc1 exception. The handlers have not been
1694 * written yet. Well, anyway there is no R6000 machine on the
1695 * current list of targets for Linux/MIPS.
1696 * (Duh, crap, there is someone with a triple R6k machine)
1697 */
1698 //set_except_vector(14, handle_mc);
1699 //set_except_vector(15, handle_ndc);
1700 }
1701
Ralf Baechlee01402b2005-07-14 15:57:16 +00001702
1703 if (board_nmi_handler_setup)
1704 board_nmi_handler_setup();
1705
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001706 if (cpu_has_fpu && !cpu_has_nofpuex)
1707 set_except_vector(15, handle_fpe);
1708
1709 set_except_vector(22, handle_mdmx);
1710
1711 if (cpu_has_mcheck)
1712 set_except_vector(24, handle_mcheck);
1713
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001714 if (cpu_has_mipsmt)
1715 set_except_vector(25, handle_mt);
1716
Chris Dearmanacaec422007-05-24 22:30:18 +01001717 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001718
1719 if (cpu_has_vce)
1720 /* Special exception: R4[04]00 uses also the divec space. */
David Daney566f74f2008-10-23 17:56:35 -07001721 memcpy((void *)(ebase + 0x180), &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001722 else if (cpu_has_4kex)
David Daney566f74f2008-10-23 17:56:35 -07001723 memcpy((void *)(ebase + 0x180), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001724 else
David Daney566f74f2008-10-23 17:56:35 -07001725 memcpy((void *)(ebase + 0x080), &except_vec3_generic, 0x80);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +00001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 signal_init();
1728#ifdef CONFIG_MIPS32_COMPAT
1729 signal32_init();
1730#endif
1731
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001732 local_flush_icache_range(ebase, ebase + 0x400);
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001733 flush_tlb_handlers();
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02001734
1735 sort_extable(__start___dbe_table, __stop___dbe_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}