blob: 9651f68c21164e175b451939ce2238f4946391a8 [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
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +010011 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Maciej W. Rozycki
Steven J. Hill2a0b24f2013-03-25 12:15:55 -050012 * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc. All rights reserved.
Markos Chandrasb08a9c92013-12-04 16:20:08 +000013 * Copyright (C) 2014, Imagination Technologies Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
Ralf Baechle8e8a52e2007-05-31 14:00:19 +010015#include <linux/bug.h>
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +010016#include <linux/compiler.h>
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +020017#include <linux/context_tracking.h>
James Hoganae4ce452014-03-04 10:20:43 +000018#include <linux/cpu_pm.h>
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020019#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
Nathan Lynch8742cd22011-09-30 13:49:35 -050021#include <linux/kernel.h>
Paul Gortmakerf9ded562012-02-28 19:24:46 -050022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/sched.h>
25#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/spinlock.h>
27#include <linux/kallsyms.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000028#include <linux/bootmem.h>
Maxime Bizond4fd1982006-07-20 18:52:02 +020029#include <linux/interrupt.h>
Ralf Baechle39b8d522008-04-28 17:14:26 +010030#include <linux/ptrace.h>
Jason Wessel88547002008-07-29 15:58:53 -050031#include <linux/kgdb.h>
32#include <linux/kdebug.h>
David Daneyc1bf2072010-08-03 11:22:20 -070033#include <linux/kprobes.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000034#include <linux/notifier.h>
Jason Wessel5dd11d52010-05-20 21:04:26 -050035#include <linux/kdb.h>
David Howellsca4d3e672010-10-07 14:08:54 +010036#include <linux/irq.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080037#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/bootinfo.h>
40#include <asm/branch.h>
41#include <asm/break.h>
Ralf Baechle69f3a7d2009-11-24 01:24:58 +000042#include <asm/cop2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/cpu.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020044#include <asm/cpu-type.h>
Ralf Baechlee50c0a82005-05-31 11:49:19 +000045#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/fpu.h>
Ralf Baechleba3049e2008-10-28 17:38:42 +000047#include <asm/fpu_emulator.h>
Ralf Baechlebdc92d742013-05-21 16:59:19 +020048#include <asm/idle.h>
Ralf Baechle340ee4b2005-08-17 17:44:08 +000049#include <asm/mipsregs.h>
50#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/module.h>
Paul Burton1db1af82014-01-27 15:23:11 +000052#include <asm/msa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/pgtable.h>
54#include <asm/ptrace.h>
55#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/tlbdebug.h>
57#include <asm/traps.h>
58#include <asm/uaccess.h>
David Daneyb67b2b72008-09-23 00:08:45 -070059#include <asm/watch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/types.h>
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +090062#include <asm/stacktrace.h>
Florian Fainelli92bbe1b2010-01-28 15:22:37 +010063#include <asm/uasm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090065extern void check_wait(void);
Atsushi Nemotoc65a5482007-11-12 02:05:18 +090066extern asmlinkage void rollback_handle_int(void);
Ralf Baechlee4ac58a2006-04-03 17:56:36 +010067extern asmlinkage void handle_int(void);
Ralf Baechle86a17082013-02-08 01:21:34 +010068extern u32 handle_tlbl[];
69extern u32 handle_tlbs[];
70extern u32 handle_tlbm[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071extern asmlinkage void handle_adel(void);
72extern asmlinkage void handle_ades(void);
73extern asmlinkage void handle_ibe(void);
74extern asmlinkage void handle_dbe(void);
75extern asmlinkage void handle_sys(void);
76extern asmlinkage void handle_bp(void);
77extern asmlinkage void handle_ri(void);
Atsushi Nemoto5b104962006-09-11 17:50:29 +090078extern asmlinkage void handle_ri_rdhwr_vivt(void);
79extern asmlinkage void handle_ri_rdhwr(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080extern asmlinkage void handle_cpu(void);
81extern asmlinkage void handle_ov(void);
82extern asmlinkage void handle_tr(void);
Paul Burton2bcb3fb2014-01-27 15:23:12 +000083extern asmlinkage void handle_msa_fpe(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084extern asmlinkage void handle_fpe(void);
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +000085extern asmlinkage void handle_ftlb(void);
Paul Burton1db1af82014-01-27 15:23:11 +000086extern asmlinkage void handle_msa(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087extern asmlinkage void handle_mdmx(void);
88extern asmlinkage void handle_watch(void);
Ralf Baechle340ee4b2005-08-17 17:44:08 +000089extern asmlinkage void handle_mt(void);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000090extern asmlinkage void handle_dsp(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091extern asmlinkage void handle_mcheck(void);
92extern asmlinkage void handle_reserved(void);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094void (*board_be_init)(void);
95int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
Ralf Baechlee01402b2005-07-14 15:57:16 +000096void (*board_nmi_handler_setup)(void);
97void (*board_ejtag_handler_setup)(void);
98void (*board_bind_eic_interrupt)(int irq, int regset);
Kevin Cernekee6fb97ef2011-11-16 01:25:45 +000099void (*board_ebase_setup)(void);
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000100void(*board_cache_error_setup)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200102static void show_raw_backtrace(unsigned long reg29)
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900103{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100104 unsigned long *sp = (unsigned long *)(reg29 & ~3);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900105 unsigned long addr;
106
107 printk("Call Trace:");
108#ifdef CONFIG_KALLSYMS
109 printk("\n");
110#endif
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200111 while (!kstack_end(sp)) {
112 unsigned long __user *p =
113 (unsigned long __user *)(unsigned long)sp++;
114 if (__get_user(addr, p)) {
115 printk(" (Bad stack address)");
116 break;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100117 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200118 if (__kernel_text_address(addr))
119 print_ip_sym(addr);
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900120 }
Thomas Bogendoerfer10220c82008-05-12 17:58:48 +0200121 printk("\n");
Atsushi Nemotoe889d782006-07-25 23:51:36 +0900122}
123
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900124#ifdef CONFIG_KALLSYMS
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900125int raw_show_trace;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900126static int __init set_raw_show_trace(char *str)
127{
128 raw_show_trace = 1;
129 return 1;
130}
131__setup("raw_show_trace", set_raw_show_trace);
Atsushi Nemoto1df0f0f2006-09-26 23:44:01 +0900132#endif
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200133
Ralf Baechleeae23f22007-10-14 23:27:21 +0100134static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900135{
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200136 unsigned long sp = regs->regs[29];
137 unsigned long ra = regs->regs[31];
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900138 unsigned long pc = regs->cp0_epc;
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900139
Vincent Wene909be82012-07-19 09:11:16 +0200140 if (!task)
141 task = current;
142
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900143 if (raw_show_trace || !__kernel_text_address(pc)) {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200144 show_raw_backtrace(sp);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900145 return;
146 }
147 printk("Call Trace:\n");
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200148 do {
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200149 print_ip_sym(pc);
Atsushi Nemoto19246002006-09-29 18:02:51 +0900150 pc = unwind_stack(task, &sp, pc, &ra);
Franck Bui-Huu4d157d52006-08-03 09:29:21 +0200151 } while (pc);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900152 printk("\n");
153}
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * This routine abuses get_user()/put_user() to reference pointers
157 * with at least a bit of error checking ...
158 */
Ralf Baechleeae23f22007-10-14 23:27:21 +0100159static void show_stacktrace(struct task_struct *task,
160 const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 const int field = 2 * sizeof(unsigned long);
163 long stackdata;
164 int i;
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900165 unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 printk("Stack :");
168 i = 0;
169 while ((unsigned long) sp & (PAGE_SIZE - 1)) {
170 if (i && ((i % (64 / field)) == 0))
Ralf Baechle70342282013-01-22 12:59:30 +0100171 printk("\n ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (i > 39) {
173 printk(" ...");
174 break;
175 }
176
177 if (__get_user(stackdata, sp++)) {
178 printk(" (Bad stack address)");
179 break;
180 }
181
182 printk(" %0*lx", field, stackdata);
183 i++;
184 }
185 printk("\n");
Franck Bui-Huu87151ae2006-08-03 09:29:17 +0200186 show_backtrace(task, regs);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900187}
188
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900189void show_stack(struct task_struct *task, unsigned long *sp)
190{
191 struct pt_regs regs;
192 if (sp) {
193 regs.regs[29] = (unsigned long)sp;
194 regs.regs[31] = 0;
195 regs.cp0_epc = 0;
196 } else {
197 if (task && task != current) {
198 regs.regs[29] = task->thread.reg29;
199 regs.regs[31] = 0;
200 regs.cp0_epc = task->thread.reg31;
Jason Wessel5dd11d52010-05-20 21:04:26 -0500201#ifdef CONFIG_KGDB_KDB
202 } else if (atomic_read(&kgdb_active) != -1 &&
203 kdb_current_regs) {
204 memcpy(&regs, kdb_current_regs, sizeof(regs));
205#endif /* CONFIG_KGDB_KDB */
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900206 } else {
207 prepare_frametrace(&regs);
208 }
209 }
210 show_stacktrace(task, &regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +0900213static void show_code(unsigned int __user *pc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 long i;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100216 unsigned short __user *pc16 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 printk("\nCode:");
219
Ralf Baechle39b8d522008-04-28 17:14:26 +0100220 if ((unsigned long)pc & 1)
221 pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 for(i = -3 ; i < 6 ; i++) {
223 unsigned int insn;
Ralf Baechle39b8d522008-04-28 17:14:26 +0100224 if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 printk(" (Bad address in epc)\n");
226 break;
227 }
Ralf Baechle39b8d522008-04-28 17:14:26 +0100228 printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230}
231
Ralf Baechleeae23f22007-10-14 23:27:21 +0100232static void __show_regs(const struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 const int field = 2 * sizeof(unsigned long);
235 unsigned int cause = regs->cp0_cause;
236 int i;
237
Tejun Heoa43cb952013-04-30 15:27:17 -0700238 show_regs_print_info(KERN_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /*
241 * Saved main processor registers
242 */
243 for (i = 0; i < 32; ) {
244 if ((i % 4) == 0)
245 printk("$%2d :", i);
246 if (i == 0)
247 printk(" %0*lx", field, 0UL);
248 else if (i == 26 || i == 27)
249 printk(" %*s", field, "");
250 else
251 printk(" %0*lx", field, regs->regs[i]);
252
253 i++;
254 if ((i % 4) == 0)
255 printk("\n");
256 }
257
Franck Bui-Huu9693a852007-02-02 17:41:47 +0100258#ifdef CONFIG_CPU_HAS_SMARTMIPS
259 printk("Acx : %0*lx\n", field, regs->acx);
260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 printk("Hi : %0*lx\n", field, regs->hi);
262 printk("Lo : %0*lx\n", field, regs->lo);
263
264 /*
265 * Saved cp0 registers
266 */
Ralf Baechleb012cff2008-07-15 18:44:33 +0100267 printk("epc : %0*lx %pS\n", field, regs->cp0_epc,
268 (void *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(" %s\n", print_tainted());
Ralf Baechleb012cff2008-07-15 18:44:33 +0100270 printk("ra : %0*lx %pS\n", field, regs->regs[31],
271 (void *) regs->regs[31]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Ralf Baechle70342282013-01-22 12:59:30 +0100273 printk("Status: %08x ", (uint32_t) regs->cp0_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Ralf Baechle1990e542013-06-26 17:06:34 +0200275 if (cpu_has_3kex) {
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000276 if (regs->cp0_status & ST0_KUO)
277 printk("KUo ");
278 if (regs->cp0_status & ST0_IEO)
279 printk("IEo ");
280 if (regs->cp0_status & ST0_KUP)
281 printk("KUp ");
282 if (regs->cp0_status & ST0_IEP)
283 printk("IEp ");
284 if (regs->cp0_status & ST0_KUC)
285 printk("KUc ");
286 if (regs->cp0_status & ST0_IEC)
287 printk("IEc ");
Ralf Baechle1990e542013-06-26 17:06:34 +0200288 } else if (cpu_has_4kex) {
Maciej W. Rozycki3b2396d2005-06-22 20:43:29 +0000289 if (regs->cp0_status & ST0_KX)
290 printk("KX ");
291 if (regs->cp0_status & ST0_SX)
292 printk("SX ");
293 if (regs->cp0_status & ST0_UX)
294 printk("UX ");
295 switch (regs->cp0_status & ST0_KSU) {
296 case KSU_USER:
297 printk("USER ");
298 break;
299 case KSU_SUPERVISOR:
300 printk("SUPERVISOR ");
301 break;
302 case KSU_KERNEL:
303 printk("KERNEL ");
304 break;
305 default:
306 printk("BAD_MODE ");
307 break;
308 }
309 if (regs->cp0_status & ST0_ERL)
310 printk("ERL ");
311 if (regs->cp0_status & ST0_EXL)
312 printk("EXL ");
313 if (regs->cp0_status & ST0_IE)
314 printk("IE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 printk("\n");
317
318 printk("Cause : %08x\n", cause);
319
320 cause = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
321 if (1 <= cause && cause <= 5)
322 printk("BadVA : %0*lx\n", field, regs->cp0_badvaddr);
323
Ralf Baechle9966db252007-10-11 23:46:17 +0100324 printk("PrId : %08x (%s)\n", read_c0_prid(),
325 cpu_name_string());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Ralf Baechleeae23f22007-10-14 23:27:21 +0100328/*
329 * FIXME: really the generic show_regs should take a const pointer argument.
330 */
331void show_regs(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Ralf Baechleeae23f22007-10-14 23:27:21 +0100333 __show_regs((struct pt_regs *)regs);
334}
335
David Daneyc1bf2072010-08-03 11:22:20 -0700336void show_registers(struct pt_regs *regs)
Ralf Baechleeae23f22007-10-14 23:27:21 +0100337{
Ralf Baechle39b8d522008-04-28 17:14:26 +0100338 const int field = 2 * sizeof(unsigned long);
Leonid Yegoshin83e4da1e2013-10-08 12:39:31 +0100339 mm_segment_t old_fs = get_fs();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100340
Ralf Baechleeae23f22007-10-14 23:27:21 +0100341 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 print_modules();
Ralf Baechle39b8d522008-04-28 17:14:26 +0100343 printk("Process %s (pid: %d, threadinfo=%p, task=%p, tls=%0*lx)\n",
344 current->comm, current->pid, current_thread_info(), current,
345 field, current_thread_info()->tp_value);
346 if (cpu_has_userlocal) {
347 unsigned long tls;
348
349 tls = read_c0_userlocal();
350 if (tls != current_thread_info()->tp_value)
351 printk("*HwTLS: %0*lx\n", field, tls);
352 }
353
Leonid Yegoshin83e4da1e2013-10-08 12:39:31 +0100354 if (!user_mode(regs))
355 /* Necessary for getting the correct stack content */
356 set_fs(KERNEL_DS);
Atsushi Nemotof66686f2006-07-29 23:27:20 +0900357 show_stacktrace(current, regs);
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +0900358 show_code((unsigned int __user *) regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 printk("\n");
Leonid Yegoshin83e4da1e2013-10-08 12:39:31 +0100360 set_fs(old_fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
David Daney70dc6f02010-08-03 15:44:43 -0700363static int regs_to_trapnr(struct pt_regs *regs)
364{
365 return (regs->cp0_cause >> 2) & 0x1f;
366}
367
Wu Zhangjin4d85f6a2011-07-23 12:41:24 +0000368static DEFINE_RAW_SPINLOCK(die_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
David Daney70dc6f02010-08-03 15:44:43 -0700370void __noreturn die(const char *str, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 static int die_counter;
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400373 int sig = SIGSEGV;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100374#ifdef CONFIG_MIPS_MT_SMTC
Nathan Lynch8742cd22011-09-30 13:49:35 -0500375 unsigned long dvpret;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100376#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Nathan Lynch8742cd22011-09-30 13:49:35 -0500378 oops_enter();
379
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200380 if (notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs),
381 SIGSEGV) == NOTIFY_STOP)
Ralf Baechle10423c92011-05-13 10:33:28 +0100382 sig = 0;
Jason Wessel5dd11d52010-05-20 21:04:26 -0500383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 console_verbose();
Wu Zhangjin4d85f6a2011-07-23 12:41:24 +0000385 raw_spin_lock_irq(&die_lock);
Nathan Lynch8742cd22011-09-30 13:49:35 -0500386#ifdef CONFIG_MIPS_MT_SMTC
387 dvpret = dvpe();
388#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100389 bust_spinlocks(1);
390#ifdef CONFIG_MIPS_MT_SMTC
391 mips_mt_regdump(dvpret);
392#endif /* CONFIG_MIPS_MT_SMTC */
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400393
Ralf Baechle178086c2005-10-13 17:07:54 +0100394 printk("%s[#%d]:\n", str, ++die_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 show_registers(regs);
Rusty Russell373d4d02013-01-21 17:17:39 +1030396 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Wu Zhangjin4d85f6a2011-07-23 12:41:24 +0000397 raw_spin_unlock_irq(&die_lock);
Maxime Bizond4fd1982006-07-20 18:52:02 +0200398
Nathan Lynch8742cd22011-09-30 13:49:35 -0500399 oops_exit();
400
Maxime Bizond4fd1982006-07-20 18:52:02 +0200401 if (in_interrupt())
402 panic("Fatal exception in interrupt");
403
404 if (panic_on_oops) {
Ralf Baechleab75dc02011-11-17 15:07:31 +0000405 printk(KERN_EMERG "Fatal exception: panic in 5 seconds");
Maxime Bizond4fd1982006-07-20 18:52:02 +0200406 ssleep(5);
407 panic("Fatal exception");
408 }
409
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200410 if (regs && kexec_should_crash(current))
411 crash_kexec(regs);
412
Yury Polyanskiyce384d82010-04-26 00:53:10 -0400413 do_exit(sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Thomas Bogendoerfer05106172008-08-04 19:44:34 +0200416extern struct exception_table_entry __start___dbe_table[];
417extern struct exception_table_entry __stop___dbe_table[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ralf Baechleb6dcec92007-02-18 15:57:09 +0000419__asm__(
420" .section __dbe_table, \"a\"\n"
421" .previous \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423/* Given an address, look for it in the exception tables. */
424static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
425{
426 const struct exception_table_entry *e;
427
428 e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
429 if (!e)
430 e = search_module_dbetables(addr);
431 return e;
432}
433
434asmlinkage void do_be(struct pt_regs *regs)
435{
436 const int field = 2 * sizeof(unsigned long);
437 const struct exception_table_entry *fixup = NULL;
438 int data = regs->cp0_cause & 4;
439 int action = MIPS_BE_FATAL;
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200440 enum ctx_state prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200442 prev_state = exception_enter();
Ralf Baechle70342282013-01-22 12:59:30 +0100443 /* XXX For now. Fixme, this searches the wrong table ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (data && !user_mode(regs))
445 fixup = search_dbe_tables(exception_epc(regs));
446
447 if (fixup)
448 action = MIPS_BE_FIXUP;
449
450 if (board_be_handler)
Atsushi Nemoto28fc5822007-07-13 01:49:49 +0900451 action = board_be_handler(regs, fixup != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 switch (action) {
454 case MIPS_BE_DISCARD:
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 case MIPS_BE_FIXUP:
457 if (fixup) {
458 regs->cp0_epc = fixup->nextinsn;
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 break;
462 default:
463 break;
464 }
465
466 /*
467 * Assume it would be too dangerous to continue ...
468 */
469 printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
470 data ? "Data" : "Instruction",
471 field, regs->cp0_epc, field, regs->regs[31]);
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200472 if (notify_die(DIE_OOPS, "bus error", regs, 0, regs_to_trapnr(regs),
473 SIGBUS) == NOTIFY_STOP)
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200474 goto out;
Jason Wessel88547002008-07-29 15:58:53 -0500475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 die_if_kernel("Oops", regs);
477 force_sig(SIGBUS, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200478
479out:
480 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100484 * ll/sc, rdhwr, sync emulation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
486
487#define OPCODE 0xfc000000
488#define BASE 0x03e00000
489#define RT 0x001f0000
490#define OFFSET 0x0000ffff
491#define LL 0xc0000000
492#define SC 0xe0000000
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100493#define SPEC0 0x00000000
Ralf Baechle3c370262005-04-13 17:43:59 +0000494#define SPEC3 0x7c000000
495#define RD 0x0000f800
496#define FUNC 0x0000003f
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100497#define SYNC 0x0000000f
Ralf Baechle3c370262005-04-13 17:43:59 +0000498#define RDHWR 0x0000003b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500500/* microMIPS definitions */
501#define MM_POOL32A_FUNC 0xfc00ffff
502#define MM_RDHWR 0x00006b3c
503#define MM_RS 0x001f0000
504#define MM_RT 0x03e00000
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/*
507 * The ll_bit is cleared by r*_switch.S
508 */
509
Ralf Baechlef1e39a42009-09-17 02:25:05 +0200510unsigned int ll_bit;
511struct task_struct *ll_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100513static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000515 unsigned long value, __user *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * analyse the ll instruction that just caused a ri exception
520 * and put the referenced address to addr.
521 */
522
523 /* sign extend offset */
524 offset = opcode & OFFSET;
525 offset <<= 16;
526 offset >>= 16;
527
Ralf Baechlefe00f942005-03-01 19:22:29 +0000528 vaddr = (unsigned long __user *)
Steven J. Hillb9688312013-01-12 23:29:27 +0000529 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100531 if ((unsigned long)vaddr & 3)
532 return SIGBUS;
533 if (get_user(value, vaddr))
534 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 preempt_disable();
537
538 if (ll_task == NULL || ll_task == current) {
539 ll_bit = 1;
540 } else {
541 ll_bit = 0;
542 }
543 ll_task = current;
544
545 preempt_enable();
546
547 regs->regs[(opcode & RT) >> 16] = value;
548
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100552static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Ralf Baechlefe00f942005-03-01 19:22:29 +0000554 unsigned long __user *vaddr;
555 unsigned long reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 long offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /*
559 * analyse the sc instruction that just caused a ri exception
560 * and put the referenced address to addr.
561 */
562
563 /* sign extend offset */
564 offset = opcode & OFFSET;
565 offset <<= 16;
566 offset >>= 16;
567
Ralf Baechlefe00f942005-03-01 19:22:29 +0000568 vaddr = (unsigned long __user *)
Steven J. Hillb9688312013-01-12 23:29:27 +0000569 ((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 reg = (opcode & RT) >> 16;
571
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100572 if ((unsigned long)vaddr & 3)
573 return SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 preempt_disable();
576
577 if (ll_bit == 0 || ll_task != current) {
578 regs->regs[reg] = 0;
579 preempt_enable();
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 preempt_enable();
584
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100585 if (put_user(regs->regs[reg], vaddr))
586 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 regs->regs[reg] = 1;
589
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*
594 * ll uses the opcode of lwc0 and sc uses the opcode of swc0. That is both
595 * opcodes are supposed to result in coprocessor unusable exceptions if
596 * executed on ll/sc-less processors. That's the theory. In practice a
597 * few processors such as NEC's VR4100 throw reserved instruction exceptions
598 * instead, so we're doing the emulation thing in both exception handlers.
599 */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100600static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800602 if ((opcode & OPCODE) == LL) {
603 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200604 1, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100605 return simulate_ll(regs, opcode);
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800606 }
607 if ((opcode & OPCODE) == SC) {
608 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200609 1, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100610 return simulate_sc(regs, opcode);
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100613 return -1; /* Must be something else ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Ralf Baechle3c370262005-04-13 17:43:59 +0000616/*
617 * Simulate trapping 'rdhwr' instructions to provide user accessible
Chris Dearman1f5826b2006-05-08 18:02:16 +0100618 * registers not implemented in hardware.
Ralf Baechle3c370262005-04-13 17:43:59 +0000619 */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500620static int simulate_rdhwr(struct pt_regs *regs, int rd, int rt)
Ralf Baechle3c370262005-04-13 17:43:59 +0000621{
Al Virodc8f6022006-01-12 01:06:07 -0800622 struct thread_info *ti = task_thread_info(current);
Ralf Baechle3c370262005-04-13 17:43:59 +0000623
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500624 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
625 1, regs, 0);
626 switch (rd) {
627 case 0: /* CPU number */
628 regs->regs[rt] = smp_processor_id();
629 return 0;
630 case 1: /* SYNCI length */
631 regs->regs[rt] = min(current_cpu_data.dcache.linesz,
632 current_cpu_data.icache.linesz);
633 return 0;
634 case 2: /* Read count register */
635 regs->regs[rt] = read_c0_count();
636 return 0;
637 case 3: /* Count register resolution */
Ralf Baechle69f24d12013-09-17 10:25:47 +0200638 switch (current_cpu_type()) {
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500639 case CPU_20KC:
640 case CPU_25KF:
641 regs->regs[rt] = 1;
642 break;
643 default:
644 regs->regs[rt] = 2;
645 }
646 return 0;
647 case 29:
648 regs->regs[rt] = ti->tp_value;
649 return 0;
650 default:
651 return -1;
652 }
653}
654
655static int simulate_rdhwr_normal(struct pt_regs *regs, unsigned int opcode)
656{
Ralf Baechle3c370262005-04-13 17:43:59 +0000657 if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
658 int rd = (opcode & RD) >> 11;
659 int rt = (opcode & RT) >> 16;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500660
661 simulate_rdhwr(regs, rd, rt);
662 return 0;
663 }
664
665 /* Not ours. */
666 return -1;
667}
668
669static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned short opcode)
670{
671 if ((opcode & MM_POOL32A_FUNC) == MM_RDHWR) {
672 int rd = (opcode & MM_RS) >> 16;
673 int rt = (opcode & MM_RT) >> 21;
674 simulate_rdhwr(regs, rd, rt);
675 return 0;
Ralf Baechle3c370262005-04-13 17:43:59 +0000676 }
677
Daniel Jacobowitz56ebd512005-11-26 22:34:41 -0500678 /* Not ours. */
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100679 return -1;
680}
Ralf Baechlee5679882006-11-30 01:14:47 +0000681
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100682static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
683{
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800684 if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
685 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200686 1, regs, 0);
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100687 return 0;
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800688 }
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100689
690 return -1; /* Must be something else ... */
Ralf Baechle3c370262005-04-13 17:43:59 +0000691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693asmlinkage void do_ov(struct pt_regs *regs)
694{
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200695 enum ctx_state prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 siginfo_t info;
697
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200698 prev_state = exception_enter();
Ralf Baechle36ccf1c2006-02-14 21:04:54 +0000699 die_if_kernel("Integer overflow", regs);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 info.si_code = FPE_INTOVF;
702 info.si_signo = SIGFPE;
703 info.si_errno = 0;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000704 info.si_addr = (void __user *) regs->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 force_sig_info(SIGFPE, &info, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200706 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500709int process_fpemu_return(int sig, void __user *fault_addr)
David Daney515b0292010-10-21 16:32:26 -0700710{
711 if (sig == SIGSEGV || sig == SIGBUS) {
712 struct siginfo si = {0};
713 si.si_addr = fault_addr;
714 si.si_signo = sig;
715 if (sig == SIGSEGV) {
716 if (find_vma(current->mm, (unsigned long)fault_addr))
717 si.si_code = SEGV_ACCERR;
718 else
719 si.si_code = SEGV_MAPERR;
720 } else {
721 si.si_code = BUS_ADRERR;
722 }
723 force_sig_info(sig, &si, current);
724 return 1;
725 } else if (sig) {
726 force_sig(sig, current);
727 return 1;
728 } else {
729 return 0;
730 }
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
735 */
736asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
737{
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200738 enum ctx_state prev_state;
David Daney515b0292010-10-21 16:32:26 -0700739 siginfo_t info = {0};
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100740
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200741 prev_state = exception_enter();
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200742 if (notify_die(DIE_FP, "FP exception", regs, 0, regs_to_trapnr(regs),
743 SIGFPE) == NOTIFY_STOP)
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200744 goto out;
Chris Dearman57725f92006-06-30 23:35:28 +0100745 die_if_kernel("FP exception in kernel code", regs);
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (fcr31 & FPU_CSR_UNI_X) {
748 int sig;
David Daney515b0292010-10-21 16:32:26 -0700749 void __user *fault_addr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000752 * Unimplemented operation exception. If we've got the full
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * software emulator on-board, let's use it...
754 *
755 * Force FPU to dump state into task/thread context. We're
756 * moving a lot of data here for what is probably a single
757 * instruction, but the alternative is to pre-decode the FP
758 * register operands before invoking the emulator, which seems
759 * a bit extreme for what should be an infrequent event.
760 */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000761 /* Ensure 'resume' not overwrite saved fp context again. */
Atsushi Nemoto53dc8022007-03-10 01:07:45 +0900762 lose_fpu(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Run the emulator */
David Daney515b0292010-10-21 16:32:26 -0700765 sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
766 &fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * We can't allow the emulated instruction to leave any of
770 * the cause bit set in $fcr31.
771 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900772 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* Restore the hardware register state */
Ralf Baechle70342282013-01-22 12:59:30 +0100775 own_fpu(1); /* Using the FPU again. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* If something went wrong, signal */
David Daney515b0292010-10-21 16:32:26 -0700778 process_fpemu_return(sig, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200780 goto out;
Thiemo Seufer948a34c2007-08-22 01:42:04 +0100781 } else if (fcr31 & FPU_CSR_INV_X)
782 info.si_code = FPE_FLTINV;
783 else if (fcr31 & FPU_CSR_DIV_X)
784 info.si_code = FPE_FLTDIV;
785 else if (fcr31 & FPU_CSR_OVF_X)
786 info.si_code = FPE_FLTOVF;
787 else if (fcr31 & FPU_CSR_UDF_X)
788 info.si_code = FPE_FLTUND;
789 else if (fcr31 & FPU_CSR_INE_X)
790 info.si_code = FPE_FLTRES;
791 else
792 info.si_code = __SI_FAULT;
793 info.si_signo = SIGFPE;
794 info.si_errno = 0;
795 info.si_addr = (void __user *) regs->cp0_epc;
796 force_sig_info(SIGFPE, &info, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200797
798out:
799 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Ralf Baechledf270052008-04-20 16:28:54 +0100802static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
803 const char *str)
804{
805 siginfo_t info;
806 char b[40];
807
Jason Wessel5dd11d52010-05-20 21:04:26 -0500808#ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
David Daney70dc6f02010-08-03 15:44:43 -0700809 if (kgdb_ll_trap(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
Jason Wessel5dd11d52010-05-20 21:04:26 -0500810 return;
811#endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
812
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200813 if (notify_die(DIE_TRAP, str, regs, code, regs_to_trapnr(regs),
814 SIGTRAP) == NOTIFY_STOP)
Jason Wessel88547002008-07-29 15:58:53 -0500815 return;
816
Ralf Baechledf270052008-04-20 16:28:54 +0100817 /*
818 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
819 * insns, even for trap and break codes that indicate arithmetic
820 * failures. Weird ...
821 * But should we continue the brokenness??? --macro
822 */
823 switch (code) {
824 case BRK_OVERFLOW:
825 case BRK_DIVZERO:
826 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
827 die_if_kernel(b, regs);
828 if (code == BRK_DIVZERO)
829 info.si_code = FPE_INTDIV;
830 else
831 info.si_code = FPE_INTOVF;
832 info.si_signo = SIGFPE;
833 info.si_errno = 0;
834 info.si_addr = (void __user *) regs->cp0_epc;
835 force_sig_info(SIGFPE, &info, current);
836 break;
837 case BRK_BUG:
838 die_if_kernel("Kernel bug detected", regs);
839 force_sig(SIGTRAP, current);
840 break;
Ralf Baechleba3049e2008-10-28 17:38:42 +0000841 case BRK_MEMU:
842 /*
843 * Address errors may be deliberately induced by the FPU
844 * emulator to retake control of the CPU after executing the
845 * instruction in the delay slot of an emulated branch.
846 *
847 * Terminate if exception was recognized as a delay slot return
848 * otherwise handle as normal.
849 */
850 if (do_dsemulret(regs))
851 return;
852
853 die_if_kernel("Math emu break/trap", regs);
854 force_sig(SIGTRAP, current);
855 break;
Ralf Baechledf270052008-04-20 16:28:54 +0100856 default:
857 scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
858 die_if_kernel(b, regs);
859 force_sig(SIGTRAP, current);
860 }
861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863asmlinkage void do_bp(struct pt_regs *regs)
864{
865 unsigned int opcode, bcode;
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200866 enum ctx_state prev_state;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500867 unsigned long epc;
868 u16 instr[2];
Leonid Yegoshin078dde52013-12-04 16:39:34 +0000869 mm_segment_t seg;
870
871 seg = get_fs();
872 if (!user_mode(regs))
873 set_fs(KERNEL_DS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200875 prev_state = exception_enter();
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500876 if (get_isa16_mode(regs->cp0_epc)) {
877 /* Calculate EPC. */
878 epc = exception_epc(regs);
879 if (cpu_has_mmips) {
880 if ((__get_user(instr[0], (u16 __user *)msk_isa16_mode(epc)) ||
881 (__get_user(instr[1], (u16 __user *)msk_isa16_mode(epc + 2)))))
882 goto out_sigsegv;
Markos Chandrasb08a9c92013-12-04 16:20:08 +0000883 opcode = (instr[0] << 16) | instr[1];
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500884 } else {
Markos Chandrasb08a9c92013-12-04 16:20:08 +0000885 /* MIPS16e mode */
886 if (__get_user(instr[0],
887 (u16 __user *)msk_isa16_mode(epc)))
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500888 goto out_sigsegv;
Markos Chandrasb08a9c92013-12-04 16:20:08 +0000889 bcode = (instr[0] >> 6) & 0x3f;
890 do_trap_or_bp(regs, bcode, "Break");
891 goto out;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500892 }
893 } else {
Markos Chandrasb08a9c92013-12-04 16:20:08 +0000894 if (__get_user(opcode,
895 (unsigned int __user *) exception_epc(regs)))
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500896 goto out_sigsegv;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /*
900 * There is the ancient bug in the MIPS assemblers that the break
901 * code starts left to bit 16 instead to bit 6 in the opcode.
902 * Gas is bug-compatible, but not always, grrr...
903 * We handle both cases with a simple heuristics. --macro
904 */
905 bcode = ((opcode >> 6) & ((1 << 20) - 1));
Ralf Baechledf270052008-04-20 16:28:54 +0100906 if (bcode >= (1 << 10))
907 bcode >>= 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Daneyc1bf2072010-08-03 11:22:20 -0700909 /*
910 * notify the kprobe handlers, if instruction is likely to
911 * pertain to them.
912 */
913 switch (bcode) {
914 case BRK_KPROBE_BP:
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200915 if (notify_die(DIE_BREAK, "debug", regs, bcode,
916 regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200917 goto out;
David Daneyc1bf2072010-08-03 11:22:20 -0700918 else
919 break;
920 case BRK_KPROBE_SSTEPBP:
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200921 if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode,
922 regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200923 goto out;
David Daneyc1bf2072010-08-03 11:22:20 -0700924 else
925 break;
926 default:
927 break;
928 }
929
Ralf Baechledf270052008-04-20 16:28:54 +0100930 do_trap_or_bp(regs, bcode, "Break");
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200931
932out:
Leonid Yegoshin078dde52013-12-04 16:39:34 +0000933 set_fs(seg);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200934 exception_exit(prev_state);
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900935 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000936
937out_sigsegv:
938 force_sig(SIGSEGV, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942asmlinkage void do_tr(struct pt_regs *regs)
943{
Maciej W. Rozyckia9a6e7a2013-05-23 14:31:23 +0000944 u32 opcode, tcode = 0;
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200945 enum ctx_state prev_state;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500946 u16 instr[2];
Leonid Yegoshin078dde52013-12-04 16:39:34 +0000947 mm_segment_t seg;
Maciej W. Rozyckia9a6e7a2013-05-23 14:31:23 +0000948 unsigned long epc = msk_isa16_mode(exception_epc(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Leonid Yegoshin078dde52013-12-04 16:39:34 +0000950 seg = get_fs();
951 if (!user_mode(regs))
952 set_fs(get_ds());
953
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200954 prev_state = exception_enter();
Maciej W. Rozyckia9a6e7a2013-05-23 14:31:23 +0000955 if (get_isa16_mode(regs->cp0_epc)) {
956 if (__get_user(instr[0], (u16 __user *)(epc + 0)) ||
957 __get_user(instr[1], (u16 __user *)(epc + 2)))
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500958 goto out_sigsegv;
Maciej W. Rozyckia9a6e7a2013-05-23 14:31:23 +0000959 opcode = (instr[0] << 16) | instr[1];
960 /* Immediate versions don't provide a code. */
961 if (!(opcode & OPCODE))
962 tcode = (opcode >> 12) & ((1 << 4) - 1);
963 } else {
964 if (__get_user(opcode, (u32 __user *)epc))
965 goto out_sigsegv;
966 /* Immediate versions don't provide a code. */
967 if (!(opcode & OPCODE))
968 tcode = (opcode >> 6) & ((1 << 10) - 1);
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Ralf Baechledf270052008-04-20 16:28:54 +0100971 do_trap_or_bp(regs, tcode, "Trap");
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200972
973out:
Leonid Yegoshin078dde52013-12-04 16:39:34 +0000974 set_fs(seg);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200975 exception_exit(prev_state);
Atsushi Nemoto90fccb12007-02-06 16:02:21 +0900976 return;
Ralf Baechlee5679882006-11-30 01:14:47 +0000977
978out_sigsegv:
979 force_sig(SIGSEGV, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983asmlinkage void do_ri(struct pt_regs *regs)
984{
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100985 unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
986 unsigned long old_epc = regs->cp0_epc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -0500987 unsigned long old31 = regs->regs[31];
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200988 enum ctx_state prev_state;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100989 unsigned int opcode = 0;
990 int status = -1;
991
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200992 prev_state = exception_enter();
Ralf Baechledc73e4c2013-10-09 08:54:15 +0200993 if (notify_die(DIE_RI, "RI Fault", regs, 0, regs_to_trapnr(regs),
994 SIGILL) == NOTIFY_STOP)
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +0200995 goto out;
Jason Wessel88547002008-07-29 15:58:53 -0500996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 die_if_kernel("Reserved instruction in kernel code", regs);
998
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +0100999 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001000 goto out;
Ralf Baechle3c370262005-04-13 17:43:59 +00001001
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001002 if (get_isa16_mode(regs->cp0_epc)) {
1003 unsigned short mmop[2] = { 0 };
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001004
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001005 if (unlikely(get_user(mmop[0], epc) < 0))
1006 status = SIGSEGV;
1007 if (unlikely(get_user(mmop[1], epc) < 0))
1008 status = SIGSEGV;
1009 opcode = (mmop[0] << 16) | mmop[1];
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001010
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001011 if (status < 0)
1012 status = simulate_rdhwr_mm(regs, opcode);
1013 } else {
1014 if (unlikely(get_user(opcode, epc) < 0))
1015 status = SIGSEGV;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001016
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001017 if (!cpu_has_llsc && status < 0)
1018 status = simulate_llsc(regs, opcode);
1019
1020 if (status < 0)
1021 status = simulate_rdhwr_normal(regs, opcode);
1022
1023 if (status < 0)
1024 status = simulate_sync(regs, opcode);
1025 }
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001026
1027 if (status < 0)
1028 status = SIGILL;
1029
1030 if (unlikely(status > 0)) {
1031 regs->cp0_epc = old_epc; /* Undo skip-over. */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001032 regs->regs[31] = old31;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001033 force_sig(status, current);
1034 }
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001035
1036out:
1037 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Ralf Baechled223a862007-07-10 17:33:02 +01001040/*
1041 * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
1042 * emulated more than some threshold number of instructions, force migration to
1043 * a "CPU" that has FP support.
1044 */
1045static void mt_ase_fp_affinity(void)
1046{
1047#ifdef CONFIG_MIPS_MT_FPAFF
1048 if (mt_fpemul_threshold > 0 &&
1049 ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
1050 /*
1051 * If there's no FPU present, or if the application has already
1052 * restricted the allowed set to exclude any CPUs with FPUs,
1053 * we'll skip the procedure.
1054 */
1055 if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
1056 cpumask_t tmask;
1057
Kevin D. Kissell9cc12362008-09-09 21:33:36 +02001058 current->thread.user_cpus_allowed
1059 = current->cpus_allowed;
1060 cpus_and(tmask, current->cpus_allowed,
1061 mt_fpu_cpumask);
Julia Lawalled1bbde2010-03-26 23:03:07 +01001062 set_cpus_allowed_ptr(current, &tmask);
Ralf Baechle293c5bd2007-07-25 16:19:33 +01001063 set_thread_flag(TIF_FPUBOUND);
Ralf Baechled223a862007-07-10 17:33:02 +01001064 }
1065 }
1066#endif /* CONFIG_MIPS_MT_FPAFF */
1067}
1068
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001069/*
1070 * No lock; only written during early bootup by CPU 0.
1071 */
1072static RAW_NOTIFIER_HEAD(cu2_chain);
1073
1074int __ref register_cu2_notifier(struct notifier_block *nb)
1075{
1076 return raw_notifier_chain_register(&cu2_chain, nb);
1077}
1078
1079int cu2_notifier_call_chain(unsigned long val, void *v)
1080{
1081 return raw_notifier_call_chain(&cu2_chain, val, v);
1082}
1083
1084static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
Ralf Baechle70342282013-01-22 12:59:30 +01001085 void *data)
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001086{
1087 struct pt_regs *regs = data;
1088
Jayachandran C83bee792013-06-10 06:30:01 +00001089 die_if_kernel("COP2: Unhandled kernel unaligned access or invalid "
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001090 "instruction", regs);
Jayachandran C83bee792013-06-10 06:30:01 +00001091 force_sig(SIGILL, current);
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001092
1093 return NOTIFY_OK;
1094}
1095
Paul Burton1db1af82014-01-27 15:23:11 +00001096static int enable_restore_fp_context(int msa)
1097{
1098 int err, was_fpu_owner;
1099
1100 if (!used_math()) {
1101 /* First time FP context user. */
1102 err = init_fpu();
1103 if (msa && !err)
1104 enable_msa();
1105 if (!err)
1106 set_used_math();
1107 return err;
1108 }
1109
1110 /*
1111 * This task has formerly used the FP context.
1112 *
1113 * If this thread has no live MSA vector context then we can simply
1114 * restore the scalar FP context. If it has live MSA vector context
1115 * (that is, it has or may have used MSA since last performing a
1116 * function call) then we'll need to restore the vector context. This
1117 * applies even if we're currently only executing a scalar FP
1118 * instruction. This is because if we were to later execute an MSA
1119 * instruction then we'd either have to:
1120 *
1121 * - Restore the vector context & clobber any registers modified by
1122 * scalar FP instructions between now & then.
1123 *
1124 * or
1125 *
1126 * - Not restore the vector context & lose the most significant bits
1127 * of all vector registers.
1128 *
1129 * Neither of those options is acceptable. We cannot restore the least
1130 * significant bits of the registers now & only restore the most
1131 * significant bits later because the most significant bits of any
1132 * vector registers whose aliased FP register is modified now will have
1133 * been zeroed. We'd have no way to know that when restoring the vector
1134 * context & thus may load an outdated value for the most significant
1135 * bits of a vector register.
1136 */
1137 if (!msa && !thread_msa_context_live())
1138 return own_fpu(1);
1139
1140 /*
1141 * This task is using or has previously used MSA. Thus we require
1142 * that Status.FR == 1.
1143 */
1144 was_fpu_owner = is_fpu_owner();
1145 err = own_fpu(0);
1146 if (err)
1147 return err;
1148
1149 enable_msa();
1150 write_msa_csr(current->thread.fpu.msacsr);
1151 set_thread_flag(TIF_USEDMSA);
1152
1153 /*
1154 * If this is the first time that the task is using MSA and it has
1155 * previously used scalar FP in this time slice then we already nave
1156 * FP context which we shouldn't clobber.
1157 */
1158 if (!test_and_set_thread_flag(TIF_MSA_CTX_LIVE) && was_fpu_owner)
1159 return 0;
1160
1161 /* We need to restore the vector context. */
1162 restore_msa(current);
1163 return 0;
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166asmlinkage void do_cpu(struct pt_regs *regs)
1167{
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001168 enum ctx_state prev_state;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001169 unsigned int __user *epc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001170 unsigned long old_epc, old31;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001171 unsigned int opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 unsigned int cpid;
Paul Burton597ce172013-11-22 13:12:07 +00001173 int status, err;
David Daneyf9bb4cf2008-12-11 15:33:23 -08001174 unsigned long __maybe_unused flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001176 prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
1178
Jayachandran C83bee792013-06-10 06:30:01 +00001179 if (cpid != 2)
1180 die_if_kernel("do_cpu invoked from kernel context!", regs);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 switch (cpid) {
1183 case 0:
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001184 epc = (unsigned int __user *)exception_epc(regs);
1185 old_epc = regs->cp0_epc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001186 old31 = regs->regs[31];
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001187 opcode = 0;
1188 status = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001190 if (unlikely(compute_return_epc(regs) < 0))
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001191 goto out;
Ralf Baechle3c370262005-04-13 17:43:59 +00001192
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001193 if (get_isa16_mode(regs->cp0_epc)) {
1194 unsigned short mmop[2] = { 0 };
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001195
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001196 if (unlikely(get_user(mmop[0], epc) < 0))
1197 status = SIGSEGV;
1198 if (unlikely(get_user(mmop[1], epc) < 0))
1199 status = SIGSEGV;
1200 opcode = (mmop[0] << 16) | mmop[1];
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001201
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001202 if (status < 0)
1203 status = simulate_rdhwr_mm(regs, opcode);
1204 } else {
1205 if (unlikely(get_user(opcode, epc) < 0))
1206 status = SIGSEGV;
1207
1208 if (!cpu_has_llsc && status < 0)
1209 status = simulate_llsc(regs, opcode);
1210
1211 if (status < 0)
1212 status = simulate_rdhwr_normal(regs, opcode);
1213 }
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001214
1215 if (status < 0)
1216 status = SIGILL;
1217
1218 if (unlikely(status > 0)) {
1219 regs->cp0_epc = old_epc; /* Undo skip-over. */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001220 regs->regs[31] = old31;
Maciej W. Rozycki60b0d652007-10-16 18:43:26 +01001221 force_sig(status, current);
1222 }
1223
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001224 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Maciej W. Rozycki051ff442012-03-06 20:28:54 +00001226 case 3:
1227 /*
1228 * Old (MIPS I and MIPS II) processors will set this code
1229 * for COP1X opcode instructions that replaced the original
Ralf Baechle70342282013-01-22 12:59:30 +01001230 * COP3 space. We don't limit COP1 space instructions in
Maciej W. Rozycki051ff442012-03-06 20:28:54 +00001231 * the emulator according to the CPU ISA, so we want to
1232 * treat COP1X instructions consistently regardless of which
Ralf Baechle70342282013-01-22 12:59:30 +01001233 * code the CPU chose. Therefore we redirect this trap to
Maciej W. Rozycki051ff442012-03-06 20:28:54 +00001234 * the FP emulator too.
1235 *
1236 * Then some newer FPU-less processors use this code
1237 * erroneously too, so they are covered by this choice
1238 * as well.
1239 */
1240 if (raw_cpu_has_fpu)
1241 break;
1242 /* Fall through. */
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 case 1:
Paul Burton1db1af82014-01-27 15:23:11 +00001245 err = enable_restore_fp_context(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Paul Burton597ce172013-11-22 13:12:07 +00001247 if (!raw_cpu_has_fpu || err) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001248 int sig;
David Daney515b0292010-10-21 16:32:26 -07001249 void __user *fault_addr = NULL;
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001250 sig = fpu_emulator_cop1Handler(regs,
David Daney515b0292010-10-21 16:32:26 -07001251 &current->thread.fpu,
1252 0, &fault_addr);
Paul Burton597ce172013-11-22 13:12:07 +00001253 if (!process_fpemu_return(sig, fault_addr) && !err)
Ralf Baechled223a862007-07-10 17:33:02 +01001254 mt_ase_fp_affinity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 case 2:
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00001260 raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263
1264 force_sig(SIGILL, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001265
1266out:
1267 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Paul Burton2bcb3fb2014-01-27 15:23:12 +00001270asmlinkage void do_msa_fpe(struct pt_regs *regs)
1271{
1272 enum ctx_state prev_state;
1273
1274 prev_state = exception_enter();
1275 die_if_kernel("do_msa_fpe invoked from kernel context!", regs);
1276 force_sig(SIGFPE, current);
1277 exception_exit(prev_state);
1278}
1279
Paul Burton1db1af82014-01-27 15:23:11 +00001280asmlinkage void do_msa(struct pt_regs *regs)
1281{
1282 enum ctx_state prev_state;
1283 int err;
1284
1285 prev_state = exception_enter();
1286
1287 if (!cpu_has_msa || test_thread_flag(TIF_32BIT_FPREGS)) {
1288 force_sig(SIGILL, current);
1289 goto out;
1290 }
1291
1292 die_if_kernel("do_msa invoked from kernel context!", regs);
1293
1294 err = enable_restore_fp_context(1);
1295 if (err)
1296 force_sig(SIGILL, current);
1297out:
1298 exception_exit(prev_state);
1299}
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301asmlinkage void do_mdmx(struct pt_regs *regs)
1302{
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001303 enum ctx_state prev_state;
1304
1305 prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 force_sig(SIGILL, current);
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001307 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
David Daney8bc6d052009-01-05 15:29:58 -08001310/*
1311 * Called with interrupts disabled.
1312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313asmlinkage void do_watch(struct pt_regs *regs)
1314{
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001315 enum ctx_state prev_state;
David Daneyb67b2b72008-09-23 00:08:45 -07001316 u32 cause;
1317
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001318 prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
David Daneyb67b2b72008-09-23 00:08:45 -07001320 * Clear WP (bit 22) bit of cause register so we don't loop
1321 * forever.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
David Daneyb67b2b72008-09-23 00:08:45 -07001323 cause = read_c0_cause();
1324 cause &= ~(1 << 22);
1325 write_c0_cause(cause);
1326
1327 /*
1328 * If the current thread has the watch registers loaded, save
1329 * their values and send SIGTRAP. Otherwise another thread
1330 * left the registers set, clear them and continue.
1331 */
1332 if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
1333 mips_read_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -08001334 local_irq_enable();
David Daneyb67b2b72008-09-23 00:08:45 -07001335 force_sig(SIGTRAP, current);
David Daney8bc6d052009-01-05 15:29:58 -08001336 } else {
David Daneyb67b2b72008-09-23 00:08:45 -07001337 mips_clear_watch_registers();
David Daney8bc6d052009-01-05 15:29:58 -08001338 local_irq_enable();
1339 }
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001340 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343asmlinkage void do_mcheck(struct pt_regs *regs)
1344{
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001345 const int field = 2 * sizeof(unsigned long);
1346 int multi_match = regs->cp0_status & ST0_TS;
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001347 enum ctx_state prev_state;
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001348
Ralf Baechlec3fc5cd2013-05-29 01:07:19 +02001349 prev_state = exception_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 show_regs(regs);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001351
1352 if (multi_match) {
Ralf Baechle70342282013-01-22 12:59:30 +01001353 printk("Index : %0x\n", read_c0_index());
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001354 printk("Pagemask: %0x\n", read_c0_pagemask());
1355 printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
1356 printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
1357 printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
1358 printk("\n");
1359 dump_tlb_all();
1360 }
1361
Atsushi Nemotoe1bb82892007-07-13 23:51:46 +09001362 show_code((unsigned int __user *) regs->cp0_epc);
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * Some chips may have other causes of machine check (e.g. SB1
1366 * graduation timer)
1367 */
1368 panic("Caught Machine Check exception - %scaused by multiple "
1369 "matching entries in the TLB.",
Ralf Baechlecac4bcb2006-05-24 16:51:02 +01001370 (multi_match) ? "" : "not ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001373asmlinkage void do_mt(struct pt_regs *regs)
1374{
Ralf Baechle41c594a2006-04-05 09:45:45 +01001375 int subcode;
1376
Ralf Baechle41c594a2006-04-05 09:45:45 +01001377 subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1378 >> VPECONTROL_EXCPT_SHIFT;
1379 switch (subcode) {
1380 case 0:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001381 printk(KERN_DEBUG "Thread Underflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001382 break;
1383 case 1:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001384 printk(KERN_DEBUG "Thread Overflow\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001385 break;
1386 case 2:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001387 printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001388 break;
1389 case 3:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001390 printk(KERN_DEBUG "Gating Storage Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001391 break;
1392 case 4:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001393 printk(KERN_DEBUG "YIELD Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001394 break;
1395 case 5:
Masanari Iidaf232c7e2012-02-08 21:53:14 +09001396 printk(KERN_DEBUG "Gating Storage Scheduler Exception\n");
Ralf Baechle41c594a2006-04-05 09:45:45 +01001397 break;
1398 default:
Chris Dearmane35a5e32006-06-30 14:19:45 +01001399 printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
Ralf Baechle41c594a2006-04-05 09:45:45 +01001400 subcode);
1401 break;
1402 }
Ralf Baechle340ee4b2005-08-17 17:44:08 +00001403 die_if_kernel("MIPS MT Thread exception in kernel", regs);
1404
1405 force_sig(SIGILL, current);
1406}
1407
1408
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001409asmlinkage void do_dsp(struct pt_regs *regs)
1410{
1411 if (cpu_has_dsp)
Ralf Baechleab75dc02011-11-17 15:07:31 +00001412 panic("Unexpected DSP exception");
Ralf Baechlee50c0a82005-05-31 11:49:19 +00001413
1414 force_sig(SIGILL, current);
1415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417asmlinkage void do_reserved(struct pt_regs *regs)
1418{
1419 /*
Ralf Baechle70342282013-01-22 12:59:30 +01001420 * Game over - no way to handle this if it ever occurs. Most probably
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * caused by a new unknown cpu type or after another deadly
1422 * hard/software error.
1423 */
1424 show_regs(regs);
1425 panic("Caught reserved exception %ld - should not happen.",
1426 (regs->cp0_cause & 0x7f) >> 2);
1427}
1428
Ralf Baechle39b8d522008-04-28 17:14:26 +01001429static int __initdata l1parity = 1;
1430static int __init nol1parity(char *s)
1431{
1432 l1parity = 0;
1433 return 1;
1434}
1435__setup("nol1par", nol1parity);
1436static int __initdata l2parity = 1;
1437static int __init nol2parity(char *s)
1438{
1439 l2parity = 0;
1440 return 1;
1441}
1442__setup("nol2par", nol2parity);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444/*
1445 * Some MIPS CPUs can enable/disable for cache parity detection, but do
1446 * it different ways.
1447 */
1448static inline void parity_protection_init(void)
1449{
Ralf Baechle10cc3522007-10-11 23:46:15 +01001450 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001452 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001453 case CPU_74K:
1454 case CPU_1004K:
Steven J. Hill442e14a2014-01-17 15:03:50 -06001455 case CPU_1074K:
Leonid Yegoshin26ab96d2013-11-27 10:07:53 +00001456 case CPU_INTERAPTIV:
Leonid Yegoshin708ac4b2013-11-14 16:12:27 +00001457 case CPU_PROAPTIV:
James Hoganaced4cb2014-01-22 16:19:38 +00001458 case CPU_P5600:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001459 {
1460#define ERRCTL_PE 0x80000000
1461#define ERRCTL_L2P 0x00800000
1462 unsigned long errctl;
1463 unsigned int l1parity_present, l2parity_present;
1464
1465 errctl = read_c0_ecc();
1466 errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1467
1468 /* probe L1 parity support */
1469 write_c0_ecc(errctl | ERRCTL_PE);
1470 back_to_back_c0_hazard();
1471 l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1472
1473 /* probe L2 parity support */
1474 write_c0_ecc(errctl|ERRCTL_L2P);
1475 back_to_back_c0_hazard();
1476 l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1477
1478 if (l1parity_present && l2parity_present) {
1479 if (l1parity)
1480 errctl |= ERRCTL_PE;
1481 if (l1parity ^ l2parity)
1482 errctl |= ERRCTL_L2P;
1483 } else if (l1parity_present) {
1484 if (l1parity)
1485 errctl |= ERRCTL_PE;
1486 } else if (l2parity_present) {
1487 if (l2parity)
1488 errctl |= ERRCTL_L2P;
1489 } else {
1490 /* No parity available */
1491 }
1492
1493 printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1494
1495 write_c0_ecc(errctl);
1496 back_to_back_c0_hazard();
1497 errctl = read_c0_ecc();
1498 printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1499
1500 if (l1parity_present)
1501 printk(KERN_INFO "Cache parity protection %sabled\n",
1502 (errctl & ERRCTL_PE) ? "en" : "dis");
1503
1504 if (l2parity_present) {
1505 if (l1parity_present && l1parity)
1506 errctl ^= ERRCTL_L2P;
1507 printk(KERN_INFO "L2 cache parity protection %sabled\n",
1508 (errctl & ERRCTL_L2P) ? "en" : "dis");
1509 }
1510 }
1511 break;
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 case CPU_5KC:
Leonid Yegoshin78d48032012-07-06 21:56:01 +02001514 case CPU_5KE:
Kelvin Cheung2fa36392012-06-20 20:05:32 +01001515 case CPU_LOONGSON1:
Ralf Baechle14f18b72005-03-01 18:15:08 +00001516 write_c0_ecc(0x80000000);
1517 back_to_back_c0_hazard();
1518 /* Set the PE bit (bit 31) in the c0_errctl register. */
1519 printk(KERN_INFO "Cache parity protection %sabled\n",
1520 (read_c0_ecc() & 0x80000000) ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
1522 case CPU_20KC:
1523 case CPU_25KF:
1524 /* Clear the DE bit (bit 16) in the c0_status register. */
1525 printk(KERN_INFO "Enable cache parity protection for "
1526 "MIPS 20KC/25KF CPUs.\n");
1527 clear_c0_status(ST0_DE);
1528 break;
1529 default:
1530 break;
1531 }
1532}
1533
1534asmlinkage void cache_parity_error(void)
1535{
1536 const int field = 2 * sizeof(unsigned long);
1537 unsigned int reg_val;
1538
1539 /* For the moment, report the problem and hang. */
1540 printk("Cache error exception:\n");
1541 printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1542 reg_val = read_c0_cacheerr();
1543 printk("c0_cacheerr == %08x\n", reg_val);
1544
1545 printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1546 reg_val & (1<<30) ? "secondary" : "primary",
1547 reg_val & (1<<31) ? "data" : "insn");
Leonid Yegoshin6de20452013-10-10 09:58:59 +01001548 if (cpu_has_mips_r2 &&
1549 ((current_cpu_data.processor_id && 0xff0000) == PRID_COMP_MIPS)) {
1550 pr_err("Error bits: %s%s%s%s%s%s%s%s\n",
1551 reg_val & (1<<29) ? "ED " : "",
1552 reg_val & (1<<28) ? "ET " : "",
1553 reg_val & (1<<27) ? "ES " : "",
1554 reg_val & (1<<26) ? "EE " : "",
1555 reg_val & (1<<25) ? "EB " : "",
1556 reg_val & (1<<24) ? "EI " : "",
1557 reg_val & (1<<23) ? "E1 " : "",
1558 reg_val & (1<<22) ? "E0 " : "");
1559 } else {
1560 pr_err("Error bits: %s%s%s%s%s%s%s\n",
1561 reg_val & (1<<29) ? "ED " : "",
1562 reg_val & (1<<28) ? "ET " : "",
1563 reg_val & (1<<26) ? "EE " : "",
1564 reg_val & (1<<25) ? "EB " : "",
1565 reg_val & (1<<24) ? "EI " : "",
1566 reg_val & (1<<23) ? "E1 " : "",
1567 reg_val & (1<<22) ? "E0 " : "");
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1570
Ralf Baechleec917c2c2005-10-07 16:58:15 +01001571#if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (reg_val & (1<<22))
1573 printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1574
1575 if (reg_val & (1<<23))
1576 printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1577#endif
1578
1579 panic("Can't handle the cache error!");
1580}
1581
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +00001582asmlinkage void do_ftlb(void)
1583{
1584 const int field = 2 * sizeof(unsigned long);
1585 unsigned int reg_val;
1586
1587 /* For the moment, report the problem and hang. */
1588 if (cpu_has_mips_r2 &&
1589 ((current_cpu_data.processor_id && 0xff0000) == PRID_COMP_MIPS)) {
1590 pr_err("FTLB error exception, cp0_ecc=0x%08x:\n",
1591 read_c0_ecc());
1592 pr_err("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1593 reg_val = read_c0_cacheerr();
1594 pr_err("c0_cacheerr == %08x\n", reg_val);
1595
1596 if ((reg_val & 0xc0000000) == 0xc0000000) {
1597 pr_err("Decoded c0_cacheerr: FTLB parity error\n");
1598 } else {
1599 pr_err("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1600 reg_val & (1<<30) ? "secondary" : "primary",
1601 reg_val & (1<<31) ? "data" : "insn");
1602 }
1603 } else {
1604 pr_err("FTLB error exception\n");
1605 }
1606 /* Just print the cacheerr bits for now */
1607 cache_parity_error();
1608}
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/*
1611 * SDBBP EJTAG debug exception handler.
1612 * We skip the instruction and return to the next instruction.
1613 */
1614void ejtag_exception_handler(struct pt_regs *regs)
1615{
1616 const int field = 2 * sizeof(unsigned long);
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001617 unsigned long depc, old_epc, old_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 unsigned int debug;
1619
Chris Dearman70ae6122006-06-30 12:32:37 +01001620 printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 depc = read_c0_depc();
1622 debug = read_c0_debug();
Chris Dearman70ae6122006-06-30 12:32:37 +01001623 printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (debug & 0x80000000) {
1625 /*
1626 * In branch delay slot.
1627 * We cheat a little bit here and use EPC to calculate the
1628 * debug return address (DEPC). EPC is restored after the
1629 * calculation.
1630 */
1631 old_epc = regs->cp0_epc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001632 old_ra = regs->regs[31];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 regs->cp0_epc = depc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001634 compute_return_epc(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 depc = regs->cp0_epc;
1636 regs->cp0_epc = old_epc;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001637 regs->regs[31] = old_ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 } else
1639 depc += 4;
1640 write_c0_depc(depc);
1641
1642#if 0
Chris Dearman70ae6122006-06-30 12:32:37 +01001643 printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 write_c0_debug(debug | 0x100);
1645#endif
1646}
1647
1648/*
1649 * NMI exception handler.
Kevin Cernekee34bd92e2011-11-16 01:25:44 +00001650 * No lock; only written during early bootup by CPU 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
Kevin Cernekee34bd92e2011-11-16 01:25:44 +00001652static RAW_NOTIFIER_HEAD(nmi_chain);
1653
1654int register_nmi_notifier(struct notifier_block *nb)
1655{
1656 return raw_notifier_chain_register(&nmi_chain, nb);
1657}
1658
Joe Perchesff2d8b12012-01-12 17:17:21 -08001659void __noreturn nmi_exception_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Leonid Yegoshin83e4da1e2013-10-08 12:39:31 +01001661 char str[100];
1662
Kevin Cernekee34bd92e2011-11-16 01:25:44 +00001663 raw_notifier_call_chain(&nmi_chain, 0, regs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001664 bust_spinlocks(1);
Leonid Yegoshin83e4da1e2013-10-08 12:39:31 +01001665 snprintf(str, 100, "CPU%d NMI taken, CP0_EPC=%lx\n",
1666 smp_processor_id(), regs->cp0_epc);
1667 regs->cp0_epc = read_c0_errorepc();
1668 die(str, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Ralf Baechlee01402b2005-07-14 15:57:16 +00001671#define VECTORSPACING 0x100 /* for EI/VI mode */
1672
1673unsigned long ebase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674unsigned long exception_handlers[32];
Ralf Baechlee01402b2005-07-14 15:57:16 +00001675unsigned long vi_handlers[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Florian Fainelli2d1b6e92010-01-28 15:21:42 +01001677void __init *set_except_vector(int n, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 unsigned long handler = (unsigned long) addr;
Ralf Baechleb22d1b62013-05-09 17:57:30 +02001680 unsigned long old_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001682#ifdef CONFIG_CPU_MICROMIPS
1683 /*
1684 * Only the TLB handlers are cache aligned with an even
1685 * address. All other handlers are on an odd address and
1686 * require no modification. Otherwise, MIPS32 mode will
1687 * be entered when handling any TLB exceptions. That
1688 * would be bad...since we must stay in microMIPS mode.
1689 */
1690 if (!(handler & 0x1))
1691 handler |= 1;
1692#endif
Ralf Baechleb22d1b62013-05-09 17:57:30 +02001693 old_handler = xchg(&exception_handlers[n], handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (n == 0 && cpu_has_divec) {
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001696#ifdef CONFIG_CPU_MICROMIPS
1697 unsigned long jump_mask = ~((1 << 27) - 1);
1698#else
Florian Fainelli92bbe1b2010-01-28 15:22:37 +01001699 unsigned long jump_mask = ~((1 << 28) - 1);
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001700#endif
Florian Fainelli92bbe1b2010-01-28 15:22:37 +01001701 u32 *buf = (u32 *)(ebase + 0x200);
1702 unsigned int k0 = 26;
1703 if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
1704 uasm_i_j(&buf, handler & ~jump_mask);
1705 uasm_i_nop(&buf);
1706 } else {
1707 UASM_i_LA(&buf, k0, handler);
1708 uasm_i_jr(&buf, k0);
1709 uasm_i_nop(&buf);
1710 }
1711 local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 return (void *)old_handler;
1714}
1715
Ralf Baechle86a17082013-02-08 01:21:34 +01001716static void do_default_vi(void)
Atsushi Nemoto6ba07e52007-05-21 23:45:38 +09001717{
1718 show_regs(get_irq_regs());
1719 panic("Caught unexpected vectored interrupt.");
1720}
1721
Ralf Baechleef300e42007-05-06 18:31:18 +01001722static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001723{
1724 unsigned long handler;
1725 unsigned long old_handler = vi_handlers[n];
Ralf Baechlef6771db2007-11-08 18:02:29 +00001726 int srssets = current_cpu_data.srsets;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001727 u16 *h;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001728 unsigned char *b;
1729
Ralf Baechleb72b7092009-03-30 14:49:44 +02001730 BUG_ON(!cpu_has_veic && !cpu_has_vint);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001731
1732 if (addr == NULL) {
1733 handler = (unsigned long) do_default_vi;
1734 srs = 0;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001735 } else
Ralf Baechlee01402b2005-07-14 15:57:16 +00001736 handler = (unsigned long) addr;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001737 vi_handlers[n] = handler;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001738
1739 b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1740
Ralf Baechlef6771db2007-11-08 18:02:29 +00001741 if (srs >= srssets)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001742 panic("Shadow register set %d not supported", srs);
1743
1744 if (cpu_has_veic) {
1745 if (board_bind_eic_interrupt)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001746 board_bind_eic_interrupt(n, srs);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001747 } else if (cpu_has_vint) {
Ralf Baechlee01402b2005-07-14 15:57:16 +00001748 /* SRSMap is only defined if shadow sets are implemented */
Ralf Baechlef6771db2007-11-08 18:02:29 +00001749 if (srssets > 1)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001750 change_c0_srsmap(0xf << n*4, srs << n*4);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001751 }
1752
1753 if (srs == 0) {
1754 /*
1755 * If no shadow set is selected then use the default handler
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001756 * that does normal register saving and standard interrupt exit
Ralf Baechlee01402b2005-07-14 15:57:16 +00001757 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00001758 extern char except_vec_vi, except_vec_vi_lui;
1759 extern char except_vec_vi_ori, except_vec_vi_end;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001760 extern char rollback_except_vec_vi;
Ralf Baechlef94d9a82013-05-21 17:30:36 +02001761 char *vec_start = using_rollback_handler() ?
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001762 &rollback_except_vec_vi : &except_vec_vi;
Ralf Baechle41c594a2006-04-05 09:45:45 +01001763#ifdef CONFIG_MIPS_MT_SMTC
1764 /*
1765 * We need to provide the SMTC vectored interrupt handler
1766 * not only with the address of the handler, but with the
1767 * Status.IM bit to be masked before going there.
1768 */
1769 extern char except_vec_vi_mori;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001770#if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
1771 const int mori_offset = &except_vec_vi_mori - vec_start + 2;
1772#else
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001773 const int mori_offset = &except_vec_vi_mori - vec_start;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001774#endif
Ralf Baechle41c594a2006-04-05 09:45:45 +01001775#endif /* CONFIG_MIPS_MT_SMTC */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001776#if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
1777 const int lui_offset = &except_vec_vi_lui - vec_start + 2;
1778 const int ori_offset = &except_vec_vi_ori - vec_start + 2;
1779#else
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09001780 const int lui_offset = &except_vec_vi_lui - vec_start;
1781 const int ori_offset = &except_vec_vi_ori - vec_start;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001782#endif
1783 const int handler_len = &except_vec_vi_end - vec_start;
Ralf Baechlee01402b2005-07-14 15:57:16 +00001784
1785 if (handler_len > VECTORSPACING) {
1786 /*
1787 * Sigh... panicing won't help as the console
1788 * is probably not configured :(
1789 */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001790 panic("VECTORSPACING too small");
Ralf Baechlee01402b2005-07-14 15:57:16 +00001791 }
1792
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001793 set_handler(((unsigned long)b - ebase), vec_start,
1794#ifdef CONFIG_CPU_MICROMIPS
1795 (handler_len - 1));
1796#else
1797 handler_len);
1798#endif
Ralf Baechle41c594a2006-04-05 09:45:45 +01001799#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle8e8a52e2007-05-31 14:00:19 +01001800 BUG_ON(n > 7); /* Vector index %d exceeds SMTC maximum. */
1801
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001802 h = (u16 *)(b + mori_offset);
1803 *h = (0x100 << n);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001804#endif /* CONFIG_MIPS_MT_SMTC */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001805 h = (u16 *)(b + lui_offset);
1806 *h = (handler >> 16) & 0xffff;
1807 h = (u16 *)(b + ori_offset);
1808 *h = (handler & 0xffff);
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001809 local_flush_icache_range((unsigned long)b,
1810 (unsigned long)(b+handler_len));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001811 }
1812 else {
1813 /*
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001814 * In other cases jump directly to the interrupt handler. It
1815 * is the handler's responsibility to save registers if required
1816 * (eg hi/lo) and return from the exception using "eret".
Ralf Baechlee01402b2005-07-14 15:57:16 +00001817 */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05001818 u32 insn;
1819
1820 h = (u16 *)b;
1821 /* j handler */
1822#ifdef CONFIG_CPU_MICROMIPS
1823 insn = 0xd4000000 | (((u32)handler & 0x07ffffff) >> 1);
1824#else
1825 insn = 0x08000000 | (((u32)handler & 0x0fffffff) >> 2);
1826#endif
1827 h[0] = (insn >> 16) & 0xffff;
1828 h[1] = insn & 0xffff;
1829 h[2] = 0;
1830 h[3] = 0;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001831 local_flush_icache_range((unsigned long)b,
1832 (unsigned long)(b+8));
Ralf Baechlee01402b2005-07-14 15:57:16 +00001833 }
1834
1835 return (void *)old_handler;
1836}
1837
Ralf Baechleef300e42007-05-06 18:31:18 +01001838void *set_vi_handler(int n, vi_handler_t addr)
Ralf Baechlee01402b2005-07-14 15:57:16 +00001839{
Ralf Baechleff3eab22006-03-29 14:12:58 +01001840 return set_vi_srs_handler(n, addr, 0);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001841}
Ralf Baechlef41ae0b2006-06-05 17:24:46 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843extern void tlb_init(void);
1844
Ralf Baechle42f77542007-10-18 17:48:11 +01001845/*
1846 * Timer interrupt
1847 */
1848int cp0_compare_irq;
Ralf Baechle68b63522012-07-19 09:13:52 +02001849EXPORT_SYMBOL_GPL(cp0_compare_irq);
David VomLehn010c1082009-12-21 17:49:22 -08001850int cp0_compare_irq_shift;
Ralf Baechle42f77542007-10-18 17:48:11 +01001851
1852/*
1853 * Performance counter IRQ or -1 if shared with timer
1854 */
1855int cp0_perfcount_irq;
1856EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1857
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001858static int noulri;
Chris Dearmanbdc94eb2007-10-03 10:43:56 +01001859
1860static int __init ulri_disable(char *s)
1861{
1862 pr_info("Disabling ulri\n");
1863 noulri = 1;
1864
1865 return 1;
1866}
1867__setup("noulri", ulri_disable);
1868
James Hoganae4ce452014-03-04 10:20:43 +00001869/* configure STATUS register */
1870static void configure_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * Disable coprocessors and select 32-bit or 64-bit addressing
1874 * and the 16/32 or 32/32 FPR register model. Reset the BEV
1875 * flag that some firmware may have left set and the TS bit (for
1876 * IP27). Set XX for ISA IV code to work.
1877 */
James Hoganae4ce452014-03-04 10:20:43 +00001878 unsigned int status_set = ST0_CU0;
Ralf Baechle875d43e2005-09-03 15:56:16 -07001879#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1881#endif
Deng-Cheng Zhuadb37892013-04-01 18:14:28 +00001882 if (current_cpu_data.isa_level & MIPS_CPU_ISA_IV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 status_set |= ST0_XX;
Chris Dearmanbbaf2382007-12-13 22:42:19 +00001884 if (cpu_has_dsp)
1885 status_set |= ST0_MX;
1886
Ralf Baechleb38c7392006-02-07 01:20:43 +00001887 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 -07001888 status_set);
James Hoganae4ce452014-03-04 10:20:43 +00001889}
1890
1891/* configure HWRENA register */
1892static void configure_hwrena(void)
1893{
1894 unsigned int hwrena = cpu_hwrena_impl_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001896 if (cpu_has_mips_r2)
1897 hwrena |= 0x0000000f;
Ralf Baechlea3692022007-07-10 17:33:02 +01001898
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001899 if (!noulri && cpu_has_userlocal)
1900 hwrena |= (1 << 29);
Ralf Baechlea3692022007-07-10 17:33:02 +01001901
Kevin Cernekee18d693b2010-10-16 14:22:38 -07001902 if (hwrena)
1903 write_c0_hwrena(hwrena);
James Hoganae4ce452014-03-04 10:20:43 +00001904}
Ralf Baechlee01402b2005-07-14 15:57:16 +00001905
James Hoganae4ce452014-03-04 10:20:43 +00001906static void configure_exception_vector(void)
1907{
Ralf Baechlee01402b2005-07-14 15:57:16 +00001908 if (cpu_has_veic || cpu_has_vint) {
Chris Dearman9fb4c2b92009-03-20 15:33:55 -07001909 unsigned long sr = set_c0_status(ST0_BEV);
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001910 write_c0_ebase(ebase);
Chris Dearman9fb4c2b92009-03-20 15:33:55 -07001911 write_c0_status(sr);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001912 /* Setting vector spacing enables EI/VI mode */
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001913 change_c0_intctl(0x3e0, VECTORSPACING);
Ralf Baechlee01402b2005-07-14 15:57:16 +00001914 }
Ralf Baechled03d0a52005-08-17 13:44:26 +00001915 if (cpu_has_divec) {
1916 if (cpu_has_mipsmt) {
1917 unsigned int vpflags = dvpe();
1918 set_c0_cause(CAUSEF_IV);
1919 evpe(vpflags);
1920 } else
1921 set_c0_cause(CAUSEF_IV);
1922 }
James Hoganae4ce452014-03-04 10:20:43 +00001923}
1924
1925void per_cpu_trap_init(bool is_boot_cpu)
1926{
1927 unsigned int cpu = smp_processor_id();
1928#ifdef CONFIG_MIPS_MT_SMTC
1929 int secondaryTC = 0;
1930 int bootTC = (cpu == 0);
1931
1932 /*
1933 * Only do per_cpu_trap_init() for first TC of Each VPE.
1934 * Note that this hack assumes that the SMTC init code
1935 * assigns TCs consecutively and in ascending order.
1936 */
1937
1938 if (((read_c0_tcbind() & TCBIND_CURTC) != 0) &&
1939 ((read_c0_tcbind() & TCBIND_CURVPE) == cpu_data[cpu - 1].vpe_id))
1940 secondaryTC = 1;
1941#endif /* CONFIG_MIPS_MT_SMTC */
1942
1943 configure_status();
1944 configure_hwrena();
1945
1946#ifdef CONFIG_MIPS_MT_SMTC
1947 if (!secondaryTC) {
1948#endif /* CONFIG_MIPS_MT_SMTC */
1949
1950 configure_exception_vector();
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001951
1952 /*
1953 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1954 *
1955 * o read IntCtl.IPTI to determine the timer interrupt
1956 * o read IntCtl.IPPCI to determine the performance counter interrupt
1957 */
1958 if (cpu_has_mips_r2) {
David VomLehn010c1082009-12-21 17:49:22 -08001959 cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
1960 cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
1961 cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001962 if (cp0_perfcount_irq == cp0_compare_irq)
1963 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001964 } else {
1965 cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
Ralf Baechlec6a4ebb2012-07-06 23:56:00 +02001966 cp0_compare_irq_shift = CP0_LEGACY_PERFCNT_IRQ;
Chris Dearmanc3e838a2007-06-21 12:59:57 +01001967 cp0_perfcount_irq = -1;
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +01001968 }
1969
Ralf Baechle41c594a2006-04-05 09:45:45 +01001970#ifdef CONFIG_MIPS_MT_SMTC
1971 }
1972#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
David Daney48c4ac92013-05-13 13:56:44 -07001974 if (!cpu_data[cpu].asid_cache)
1975 cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 atomic_inc(&init_mm.mm_count);
1978 current->active_mm = &init_mm;
1979 BUG_ON(current->mm);
1980 enter_lazy_tlb(&init_mm, current);
1981
Ralf Baechle41c594a2006-04-05 09:45:45 +01001982#ifdef CONFIG_MIPS_MT_SMTC
1983 if (bootTC) {
1984#endif /* CONFIG_MIPS_MT_SMTC */
David Daney6650df32012-05-15 00:04:50 -07001985 /* Boot CPU's cache setup in setup_arch(). */
1986 if (!is_boot_cpu)
1987 cpu_cache_init();
Ralf Baechle41c594a2006-04-05 09:45:45 +01001988 tlb_init();
1989#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle6a058882007-05-31 14:03:45 +01001990 } else if (!secondaryTC) {
1991 /*
1992 * First TC in non-boot VPE must do subset of tlb_init()
1993 * for MMU countrol registers.
1994 */
1995 write_c0_pagemask(PM_DEFAULT_MASK);
1996 write_c0_wired(0);
Ralf Baechle41c594a2006-04-05 09:45:45 +01001997 }
1998#endif /* CONFIG_MIPS_MT_SMTC */
David Daney3d8bfdd2010-12-21 14:19:11 -08001999 TLBMISS_HANDLER_SETUP();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Ralf Baechlee01402b2005-07-14 15:57:16 +00002002/* Install CPU exception handler */
Paul Gortmaker078a55f2013-06-18 13:38:59 +00002003void set_handler(unsigned long offset, void *addr, unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00002004{
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002005#ifdef CONFIG_CPU_MICROMIPS
2006 memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size);
2007#else
Ralf Baechlee01402b2005-07-14 15:57:16 +00002008 memcpy((void *)(ebase + offset), addr, size);
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002009#endif
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02002010 local_flush_icache_range(ebase + offset, ebase + offset + size);
Ralf Baechlee01402b2005-07-14 15:57:16 +00002011}
2012
Paul Gortmaker078a55f2013-06-18 13:38:59 +00002013static char panic_null_cerr[] =
Ralf Baechle641e97f2007-10-11 23:46:05 +01002014 "Trying to set NULL cache error exception handler";
2015
Ralf Baechle42fe7ee2009-01-28 18:48:23 +00002016/*
2017 * Install uncached CPU exception handler.
2018 * This is suitable only for the cache error exception which is the only
2019 * exception handler that is being run uncached.
2020 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +00002021void set_uncached_handler(unsigned long offset, void *addr,
Ralf Baechle234fcd12008-03-08 09:56:28 +00002022 unsigned long size)
Ralf Baechlee01402b2005-07-14 15:57:16 +00002023{
Sebastian Andrzej Siewior4f81b012010-04-27 22:53:30 +02002024 unsigned long uncached_ebase = CKSEG1ADDR(ebase);
Ralf Baechlee01402b2005-07-14 15:57:16 +00002025
Ralf Baechle641e97f2007-10-11 23:46:05 +01002026 if (!addr)
2027 panic(panic_null_cerr);
2028
Ralf Baechlee01402b2005-07-14 15:57:16 +00002029 memcpy((void *)(uncached_ebase + offset), addr, size);
2030}
2031
Atsushi Nemoto5b104962006-09-11 17:50:29 +09002032static int __initdata rdhwr_noopt;
2033static int __init set_rdhwr_noopt(char *str)
2034{
2035 rdhwr_noopt = 1;
2036 return 1;
2037}
2038
2039__setup("rdhwr_noopt", set_rdhwr_noopt);
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041void __init trap_init(void)
2042{
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002043 extern char except_vec3_generic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 extern char except_vec4;
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002045 extern char except_vec3_r4000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 unsigned long i;
Atsushi Nemotoc65a5482007-11-12 02:05:18 +09002047
2048 check_wait();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Jason Wessel88547002008-07-29 15:58:53 -05002050#if defined(CONFIG_KGDB)
2051 if (kgdb_early_setup)
Ralf Baechle70342282013-01-22 12:59:30 +01002052 return; /* Already done */
Jason Wessel88547002008-07-29 15:58:53 -05002053#endif
2054
Chris Dearman9fb4c2b92009-03-20 15:33:55 -07002055 if (cpu_has_veic || cpu_has_vint) {
2056 unsigned long size = 0x200 + VECTORSPACING*64;
2057 ebase = (unsigned long)
2058 __alloc_bootmem(size, 1 << fls(size), 0);
2059 } else {
Sanjay Lal9843b032012-11-21 18:34:03 -08002060#ifdef CONFIG_KVM_GUEST
2061#define KVM_GUEST_KSEG0 0x40000000
2062 ebase = KVM_GUEST_KSEG0;
2063#else
2064 ebase = CKSEG0;
2065#endif
David Daney566f74f2008-10-23 17:56:35 -07002066 if (cpu_has_mips_r2)
2067 ebase += (read_c0_ebase() & 0x3ffff000);
2068 }
Ralf Baechlee01402b2005-07-14 15:57:16 +00002069
Steven J. Hillc6213c62013-06-05 21:25:17 +00002070 if (cpu_has_mmips) {
2071 unsigned int config3 = read_c0_config3();
2072
2073 if (IS_ENABLED(CONFIG_CPU_MICROMIPS))
2074 write_c0_config3(config3 | MIPS_CONF3_ISA_OE);
2075 else
2076 write_c0_config3(config3 & ~MIPS_CONF3_ISA_OE);
2077 }
2078
Kevin Cernekee6fb97ef2011-11-16 01:25:45 +00002079 if (board_ebase_setup)
2080 board_ebase_setup();
David Daney6650df32012-05-15 00:04:50 -07002081 per_cpu_trap_init(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /*
2084 * Copy the generic exception handlers to their final destination.
2085 * This will be overriden later as suitable for a particular
2086 * configuration.
2087 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00002088 set_handler(0x180, &except_vec3_generic, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /*
2091 * Setup default vectors
2092 */
2093 for (i = 0; i <= 31; i++)
2094 set_except_vector(i, handle_reserved);
2095
2096 /*
2097 * Copy the EJTAG debug exception vector handler code to it's final
2098 * destination.
2099 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00002100 if (cpu_has_ejtag && board_ejtag_handler_setup)
Ralf Baechle49a89ef2007-10-11 23:46:15 +01002101 board_ejtag_handler_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /*
2104 * Only some CPUs have the watch exceptions.
2105 */
2106 if (cpu_has_watch)
2107 set_except_vector(23, handle_watch);
2108
2109 /*
Ralf Baechlee01402b2005-07-14 15:57:16 +00002110 * Initialise interrupt handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 */
Ralf Baechlee01402b2005-07-14 15:57:16 +00002112 if (cpu_has_veic || cpu_has_vint) {
2113 int nvec = cpu_has_veic ? 64 : 8;
2114 for (i = 0; i < nvec; i++)
Ralf Baechleff3eab22006-03-29 14:12:58 +01002115 set_vi_handler(i, NULL);
Ralf Baechlee01402b2005-07-14 15:57:16 +00002116 }
2117 else if (cpu_has_divec)
2118 set_handler(0x200, &except_vec4, 0x8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /*
2121 * Some CPUs can enable/disable for cache parity detection, but does
2122 * it different ways.
2123 */
2124 parity_protection_init();
2125
2126 /*
2127 * The Data Bus Errors / Instruction Bus Errors are signaled
2128 * by external hardware. Therefore these two exceptions
2129 * may have board specific handlers.
2130 */
2131 if (board_be_init)
2132 board_be_init();
2133
Ralf Baechlef94d9a82013-05-21 17:30:36 +02002134 set_except_vector(0, using_rollback_handler() ? rollback_handle_int
2135 : handle_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 set_except_vector(1, handle_tlbm);
2137 set_except_vector(2, handle_tlbl);
2138 set_except_vector(3, handle_tlbs);
2139
2140 set_except_vector(4, handle_adel);
2141 set_except_vector(5, handle_ades);
2142
2143 set_except_vector(6, handle_ibe);
2144 set_except_vector(7, handle_dbe);
2145
2146 set_except_vector(8, handle_sys);
2147 set_except_vector(9, handle_bp);
Atsushi Nemoto5b104962006-09-11 17:50:29 +09002148 set_except_vector(10, rdhwr_noopt ? handle_ri :
2149 (cpu_has_vtag_icache ?
2150 handle_ri_rdhwr_vivt : handle_ri_rdhwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 set_except_vector(11, handle_cpu);
2152 set_except_vector(12, handle_ov);
2153 set_except_vector(13, handle_tr);
Paul Burton2bcb3fb2014-01-27 15:23:12 +00002154 set_except_vector(14, handle_msa_fpe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Ralf Baechle10cc3522007-10-11 23:46:15 +01002156 if (current_cpu_type() == CPU_R6000 ||
2157 current_cpu_type() == CPU_R6000A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * The R6000 is the only R-series CPU that features a machine
2160 * check exception (similar to the R4000 cache error) and
2161 * unaligned ldc1/sdc1 exception. The handlers have not been
Ralf Baechle70342282013-01-22 12:59:30 +01002162 * written yet. Well, anyway there is no R6000 machine on the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * current list of targets for Linux/MIPS.
2164 * (Duh, crap, there is someone with a triple R6k machine)
2165 */
2166 //set_except_vector(14, handle_mc);
2167 //set_except_vector(15, handle_ndc);
2168 }
2169
Ralf Baechlee01402b2005-07-14 15:57:16 +00002170
2171 if (board_nmi_handler_setup)
2172 board_nmi_handler_setup();
2173
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002174 if (cpu_has_fpu && !cpu_has_nofpuex)
2175 set_except_vector(15, handle_fpe);
2176
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +00002177 set_except_vector(16, handle_ftlb);
Paul Burton1db1af82014-01-27 15:23:11 +00002178 set_except_vector(21, handle_msa);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002179 set_except_vector(22, handle_mdmx);
2180
2181 if (cpu_has_mcheck)
2182 set_except_vector(24, handle_mcheck);
2183
Ralf Baechle340ee4b2005-08-17 17:44:08 +00002184 if (cpu_has_mipsmt)
2185 set_except_vector(25, handle_mt);
2186
Chris Dearmanacaec422007-05-24 22:30:18 +01002187 set_except_vector(26, handle_dsp);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002188
David Daneyfcbf1df2012-05-15 00:04:46 -07002189 if (board_cache_error_setup)
2190 board_cache_error_setup();
2191
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002192 if (cpu_has_vce)
2193 /* Special exception: R4[04]00 uses also the divec space. */
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002194 set_handler(0x180, &except_vec3_r4000, 0x100);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002195 else if (cpu_has_4kex)
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002196 set_handler(0x180, &except_vec3_generic, 0x80);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002197 else
Steven J. Hill2a0b24f2013-03-25 12:15:55 -05002198 set_handler(0x080, &except_vec3_generic, 0x80);
Ralf Baechlee50c0a82005-05-31 11:49:19 +00002199
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02002200 local_flush_icache_range(ebase, ebase + 0x400);
Thomas Bogendoerfer05106172008-08-04 19:44:34 +02002201
2202 sort_extable(__start___dbe_table, __stop___dbe_table);
Ralf Baechle69f3a7d2009-11-24 01:24:58 +00002203
Ralf Baechle4483b152010-08-05 13:25:59 +01002204 cu2_notifier(default_cu2_call, 0x80000000); /* Run last */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
James Hoganae4ce452014-03-04 10:20:43 +00002206
2207static int trap_pm_notifier(struct notifier_block *self, unsigned long cmd,
2208 void *v)
2209{
2210 switch (cmd) {
2211 case CPU_PM_ENTER_FAILED:
2212 case CPU_PM_EXIT:
2213 configure_status();
2214 configure_hwrena();
2215 configure_exception_vector();
2216
2217 /* Restore register with CPU number for TLB handlers */
2218 TLBMISS_HANDLER_RESTORE();
2219
2220 break;
2221 }
2222
2223 return NOTIFY_OK;
2224}
2225
2226static struct notifier_block trap_pm_notifier_block = {
2227 .notifier_call = trap_pm_notifier,
2228};
2229
2230static int __init trap_pm_init(void)
2231{
2232 return cpu_pm_register_notifier(&trap_pm_notifier_block);
2233}
2234arch_initcall(trap_pm_init);