xref: /openbmc/linux/arch/mips/kernel/traps.c (revision 206204a1)
1 /*
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  *
6  * Copyright (C) 1994 - 1999, 2000, 01, 06 Ralf Baechle
7  * Copyright (C) 1995, 1996 Paul M. Antoine
8  * Copyright (C) 1998 Ulf Carlsson
9  * Copyright (C) 1999 Silicon Graphics, Inc.
10  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11  * Copyright (C) 2002, 2003, 2004, 2005, 2007  Maciej W. Rozycki
12  * Copyright (C) 2000, 2001, 2012 MIPS Technologies, Inc.  All rights reserved.
13  * Copyright (C) 2014, Imagination Technologies Ltd.
14  */
15 #include <linux/bug.h>
16 #include <linux/compiler.h>
17 #include <linux/context_tracking.h>
18 #include <linux/cpu_pm.h>
19 #include <linux/kexec.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mm.h>
24 #include <linux/sched.h>
25 #include <linux/smp.h>
26 #include <linux/spinlock.h>
27 #include <linux/kallsyms.h>
28 #include <linux/bootmem.h>
29 #include <linux/interrupt.h>
30 #include <linux/ptrace.h>
31 #include <linux/kgdb.h>
32 #include <linux/kdebug.h>
33 #include <linux/kprobes.h>
34 #include <linux/notifier.h>
35 #include <linux/kdb.h>
36 #include <linux/irq.h>
37 #include <linux/perf_event.h>
38 
39 #include <asm/bootinfo.h>
40 #include <asm/branch.h>
41 #include <asm/break.h>
42 #include <asm/cop2.h>
43 #include <asm/cpu.h>
44 #include <asm/cpu-type.h>
45 #include <asm/dsp.h>
46 #include <asm/fpu.h>
47 #include <asm/fpu_emulator.h>
48 #include <asm/idle.h>
49 #include <asm/mipsregs.h>
50 #include <asm/mipsmtregs.h>
51 #include <asm/module.h>
52 #include <asm/msa.h>
53 #include <asm/pgtable.h>
54 #include <asm/ptrace.h>
55 #include <asm/sections.h>
56 #include <asm/tlbdebug.h>
57 #include <asm/traps.h>
58 #include <asm/uaccess.h>
59 #include <asm/watch.h>
60 #include <asm/mmu_context.h>
61 #include <asm/types.h>
62 #include <asm/stacktrace.h>
63 #include <asm/uasm.h>
64 
65 extern void check_wait(void);
66 extern asmlinkage void rollback_handle_int(void);
67 extern asmlinkage void handle_int(void);
68 extern u32 handle_tlbl[];
69 extern u32 handle_tlbs[];
70 extern u32 handle_tlbm[];
71 extern asmlinkage void handle_adel(void);
72 extern asmlinkage void handle_ades(void);
73 extern asmlinkage void handle_ibe(void);
74 extern asmlinkage void handle_dbe(void);
75 extern asmlinkage void handle_sys(void);
76 extern asmlinkage void handle_bp(void);
77 extern asmlinkage void handle_ri(void);
78 extern asmlinkage void handle_ri_rdhwr_vivt(void);
79 extern asmlinkage void handle_ri_rdhwr(void);
80 extern asmlinkage void handle_cpu(void);
81 extern asmlinkage void handle_ov(void);
82 extern asmlinkage void handle_tr(void);
83 extern asmlinkage void handle_msa_fpe(void);
84 extern asmlinkage void handle_fpe(void);
85 extern asmlinkage void handle_ftlb(void);
86 extern asmlinkage void handle_msa(void);
87 extern asmlinkage void handle_mdmx(void);
88 extern asmlinkage void handle_watch(void);
89 extern asmlinkage void handle_mt(void);
90 extern asmlinkage void handle_dsp(void);
91 extern asmlinkage void handle_mcheck(void);
92 extern asmlinkage void handle_reserved(void);
93 
94 void (*board_be_init)(void);
95 int (*board_be_handler)(struct pt_regs *regs, int is_fixup);
96 void (*board_nmi_handler_setup)(void);
97 void (*board_ejtag_handler_setup)(void);
98 void (*board_bind_eic_interrupt)(int irq, int regset);
99 void (*board_ebase_setup)(void);
100 void(*board_cache_error_setup)(void);
101 
102 static void show_raw_backtrace(unsigned long reg29)
103 {
104 	unsigned long *sp = (unsigned long *)(reg29 & ~3);
105 	unsigned long addr;
106 
107 	printk("Call Trace:");
108 #ifdef CONFIG_KALLSYMS
109 	printk("\n");
110 #endif
111 	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;
117 		}
118 		if (__kernel_text_address(addr))
119 			print_ip_sym(addr);
120 	}
121 	printk("\n");
122 }
123 
124 #ifdef CONFIG_KALLSYMS
125 int raw_show_trace;
126 static 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);
132 #endif
133 
134 static void show_backtrace(struct task_struct *task, const struct pt_regs *regs)
135 {
136 	unsigned long sp = regs->regs[29];
137 	unsigned long ra = regs->regs[31];
138 	unsigned long pc = regs->cp0_epc;
139 
140 	if (!task)
141 		task = current;
142 
143 	if (raw_show_trace || !__kernel_text_address(pc)) {
144 		show_raw_backtrace(sp);
145 		return;
146 	}
147 	printk("Call Trace:\n");
148 	do {
149 		print_ip_sym(pc);
150 		pc = unwind_stack(task, &sp, pc, &ra);
151 	} while (pc);
152 	printk("\n");
153 }
154 
155 /*
156  * This routine abuses get_user()/put_user() to reference pointers
157  * with at least a bit of error checking ...
158  */
159 static void show_stacktrace(struct task_struct *task,
160 	const struct pt_regs *regs)
161 {
162 	const int field = 2 * sizeof(unsigned long);
163 	long stackdata;
164 	int i;
165 	unsigned long __user *sp = (unsigned long __user *)regs->regs[29];
166 
167 	printk("Stack :");
168 	i = 0;
169 	while ((unsigned long) sp & (PAGE_SIZE - 1)) {
170 		if (i && ((i % (64 / field)) == 0))
171 			printk("\n	 ");
172 		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");
186 	show_backtrace(task, regs);
187 }
188 
189 void 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;
201 #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 */
206 		} else {
207 			prepare_frametrace(&regs);
208 		}
209 	}
210 	show_stacktrace(task, &regs);
211 }
212 
213 static void show_code(unsigned int __user *pc)
214 {
215 	long i;
216 	unsigned short __user *pc16 = NULL;
217 
218 	printk("\nCode:");
219 
220 	if ((unsigned long)pc & 1)
221 		pc16 = (unsigned short __user *)((unsigned long)pc & ~1);
222 	for(i = -3 ; i < 6 ; i++) {
223 		unsigned int insn;
224 		if (pc16 ? __get_user(insn, pc16 + i) : __get_user(insn, pc + i)) {
225 			printk(" (Bad address in epc)\n");
226 			break;
227 		}
228 		printk("%c%0*x%c", (i?' ':'<'), pc16 ? 4 : 8, insn, (i?' ':'>'));
229 	}
230 }
231 
232 static void __show_regs(const struct pt_regs *regs)
233 {
234 	const int field = 2 * sizeof(unsigned long);
235 	unsigned int cause = regs->cp0_cause;
236 	int i;
237 
238 	show_regs_print_info(KERN_DEFAULT);
239 
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 
258 #ifdef CONFIG_CPU_HAS_SMARTMIPS
259 	printk("Acx    : %0*lx\n", field, regs->acx);
260 #endif
261 	printk("Hi    : %0*lx\n", field, regs->hi);
262 	printk("Lo    : %0*lx\n", field, regs->lo);
263 
264 	/*
265 	 * Saved cp0 registers
266 	 */
267 	printk("epc   : %0*lx %pS\n", field, regs->cp0_epc,
268 	       (void *) regs->cp0_epc);
269 	printk("    %s\n", print_tainted());
270 	printk("ra    : %0*lx %pS\n", field, regs->regs[31],
271 	       (void *) regs->regs[31]);
272 
273 	printk("Status: %08x	", (uint32_t) regs->cp0_status);
274 
275 	if (cpu_has_3kex) {
276 		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 ");
288 	} else if (cpu_has_4kex) {
289 		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 ");
315 	}
316 	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 
324 	printk("PrId  : %08x (%s)\n", read_c0_prid(),
325 	       cpu_name_string());
326 }
327 
328 /*
329  * FIXME: really the generic show_regs should take a const pointer argument.
330  */
331 void show_regs(struct pt_regs *regs)
332 {
333 	__show_regs((struct pt_regs *)regs);
334 }
335 
336 void show_registers(struct pt_regs *regs)
337 {
338 	const int field = 2 * sizeof(unsigned long);
339 	mm_segment_t old_fs = get_fs();
340 
341 	__show_regs(regs);
342 	print_modules();
343 	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 
354 	if (!user_mode(regs))
355 		/* Necessary for getting the correct stack content */
356 		set_fs(KERNEL_DS);
357 	show_stacktrace(current, regs);
358 	show_code((unsigned int __user *) regs->cp0_epc);
359 	printk("\n");
360 	set_fs(old_fs);
361 }
362 
363 static int regs_to_trapnr(struct pt_regs *regs)
364 {
365 	return (regs->cp0_cause >> 2) & 0x1f;
366 }
367 
368 static DEFINE_RAW_SPINLOCK(die_lock);
369 
370 void __noreturn die(const char *str, struct pt_regs *regs)
371 {
372 	static int die_counter;
373 	int sig = SIGSEGV;
374 
375 	oops_enter();
376 
377 	if (notify_die(DIE_OOPS, str, regs, 0, regs_to_trapnr(regs),
378 		       SIGSEGV) == NOTIFY_STOP)
379 		sig = 0;
380 
381 	console_verbose();
382 	raw_spin_lock_irq(&die_lock);
383 	bust_spinlocks(1);
384 
385 	printk("%s[#%d]:\n", str, ++die_counter);
386 	show_registers(regs);
387 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
388 	raw_spin_unlock_irq(&die_lock);
389 
390 	oops_exit();
391 
392 	if (in_interrupt())
393 		panic("Fatal exception in interrupt");
394 
395 	if (panic_on_oops) {
396 		printk(KERN_EMERG "Fatal exception: panic in 5 seconds");
397 		ssleep(5);
398 		panic("Fatal exception");
399 	}
400 
401 	if (regs && kexec_should_crash(current))
402 		crash_kexec(regs);
403 
404 	do_exit(sig);
405 }
406 
407 extern struct exception_table_entry __start___dbe_table[];
408 extern struct exception_table_entry __stop___dbe_table[];
409 
410 __asm__(
411 "	.section	__dbe_table, \"a\"\n"
412 "	.previous			\n");
413 
414 /* Given an address, look for it in the exception tables. */
415 static const struct exception_table_entry *search_dbe_tables(unsigned long addr)
416 {
417 	const struct exception_table_entry *e;
418 
419 	e = search_extable(__start___dbe_table, __stop___dbe_table - 1, addr);
420 	if (!e)
421 		e = search_module_dbetables(addr);
422 	return e;
423 }
424 
425 asmlinkage void do_be(struct pt_regs *regs)
426 {
427 	const int field = 2 * sizeof(unsigned long);
428 	const struct exception_table_entry *fixup = NULL;
429 	int data = regs->cp0_cause & 4;
430 	int action = MIPS_BE_FATAL;
431 	enum ctx_state prev_state;
432 
433 	prev_state = exception_enter();
434 	/* XXX For now.	 Fixme, this searches the wrong table ...  */
435 	if (data && !user_mode(regs))
436 		fixup = search_dbe_tables(exception_epc(regs));
437 
438 	if (fixup)
439 		action = MIPS_BE_FIXUP;
440 
441 	if (board_be_handler)
442 		action = board_be_handler(regs, fixup != NULL);
443 
444 	switch (action) {
445 	case MIPS_BE_DISCARD:
446 		goto out;
447 	case MIPS_BE_FIXUP:
448 		if (fixup) {
449 			regs->cp0_epc = fixup->nextinsn;
450 			goto out;
451 		}
452 		break;
453 	default:
454 		break;
455 	}
456 
457 	/*
458 	 * Assume it would be too dangerous to continue ...
459 	 */
460 	printk(KERN_ALERT "%s bus error, epc == %0*lx, ra == %0*lx\n",
461 	       data ? "Data" : "Instruction",
462 	       field, regs->cp0_epc, field, regs->regs[31]);
463 	if (notify_die(DIE_OOPS, "bus error", regs, 0, regs_to_trapnr(regs),
464 		       SIGBUS) == NOTIFY_STOP)
465 		goto out;
466 
467 	die_if_kernel("Oops", regs);
468 	force_sig(SIGBUS, current);
469 
470 out:
471 	exception_exit(prev_state);
472 }
473 
474 /*
475  * ll/sc, rdhwr, sync emulation
476  */
477 
478 #define OPCODE 0xfc000000
479 #define BASE   0x03e00000
480 #define RT     0x001f0000
481 #define OFFSET 0x0000ffff
482 #define LL     0xc0000000
483 #define SC     0xe0000000
484 #define SPEC0  0x00000000
485 #define SPEC3  0x7c000000
486 #define RD     0x0000f800
487 #define FUNC   0x0000003f
488 #define SYNC   0x0000000f
489 #define RDHWR  0x0000003b
490 
491 /*  microMIPS definitions   */
492 #define MM_POOL32A_FUNC 0xfc00ffff
493 #define MM_RDHWR        0x00006b3c
494 #define MM_RS           0x001f0000
495 #define MM_RT           0x03e00000
496 
497 /*
498  * The ll_bit is cleared by r*_switch.S
499  */
500 
501 unsigned int ll_bit;
502 struct task_struct *ll_task;
503 
504 static inline int simulate_ll(struct pt_regs *regs, unsigned int opcode)
505 {
506 	unsigned long value, __user *vaddr;
507 	long offset;
508 
509 	/*
510 	 * analyse the ll instruction that just caused a ri exception
511 	 * and put the referenced address to addr.
512 	 */
513 
514 	/* sign extend offset */
515 	offset = opcode & OFFSET;
516 	offset <<= 16;
517 	offset >>= 16;
518 
519 	vaddr = (unsigned long __user *)
520 		((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
521 
522 	if ((unsigned long)vaddr & 3)
523 		return SIGBUS;
524 	if (get_user(value, vaddr))
525 		return SIGSEGV;
526 
527 	preempt_disable();
528 
529 	if (ll_task == NULL || ll_task == current) {
530 		ll_bit = 1;
531 	} else {
532 		ll_bit = 0;
533 	}
534 	ll_task = current;
535 
536 	preempt_enable();
537 
538 	regs->regs[(opcode & RT) >> 16] = value;
539 
540 	return 0;
541 }
542 
543 static inline int simulate_sc(struct pt_regs *regs, unsigned int opcode)
544 {
545 	unsigned long __user *vaddr;
546 	unsigned long reg;
547 	long offset;
548 
549 	/*
550 	 * analyse the sc instruction that just caused a ri exception
551 	 * and put the referenced address to addr.
552 	 */
553 
554 	/* sign extend offset */
555 	offset = opcode & OFFSET;
556 	offset <<= 16;
557 	offset >>= 16;
558 
559 	vaddr = (unsigned long __user *)
560 		((unsigned long)(regs->regs[(opcode & BASE) >> 21]) + offset);
561 	reg = (opcode & RT) >> 16;
562 
563 	if ((unsigned long)vaddr & 3)
564 		return SIGBUS;
565 
566 	preempt_disable();
567 
568 	if (ll_bit == 0 || ll_task != current) {
569 		regs->regs[reg] = 0;
570 		preempt_enable();
571 		return 0;
572 	}
573 
574 	preempt_enable();
575 
576 	if (put_user(regs->regs[reg], vaddr))
577 		return SIGSEGV;
578 
579 	regs->regs[reg] = 1;
580 
581 	return 0;
582 }
583 
584 /*
585  * ll uses the opcode of lwc0 and sc uses the opcode of swc0.  That is both
586  * opcodes are supposed to result in coprocessor unusable exceptions if
587  * executed on ll/sc-less processors.  That's the theory.  In practice a
588  * few processors such as NEC's VR4100 throw reserved instruction exceptions
589  * instead, so we're doing the emulation thing in both exception handlers.
590  */
591 static int simulate_llsc(struct pt_regs *regs, unsigned int opcode)
592 {
593 	if ((opcode & OPCODE) == LL) {
594 		perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
595 				1, regs, 0);
596 		return simulate_ll(regs, opcode);
597 	}
598 	if ((opcode & OPCODE) == SC) {
599 		perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
600 				1, regs, 0);
601 		return simulate_sc(regs, opcode);
602 	}
603 
604 	return -1;			/* Must be something else ... */
605 }
606 
607 /*
608  * Simulate trapping 'rdhwr' instructions to provide user accessible
609  * registers not implemented in hardware.
610  */
611 static int simulate_rdhwr(struct pt_regs *regs, int rd, int rt)
612 {
613 	struct thread_info *ti = task_thread_info(current);
614 
615 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
616 			1, regs, 0);
617 	switch (rd) {
618 	case 0:		/* CPU number */
619 		regs->regs[rt] = smp_processor_id();
620 		return 0;
621 	case 1:		/* SYNCI length */
622 		regs->regs[rt] = min(current_cpu_data.dcache.linesz,
623 				     current_cpu_data.icache.linesz);
624 		return 0;
625 	case 2:		/* Read count register */
626 		regs->regs[rt] = read_c0_count();
627 		return 0;
628 	case 3:		/* Count register resolution */
629 		switch (current_cpu_type()) {
630 		case CPU_20KC:
631 		case CPU_25KF:
632 			regs->regs[rt] = 1;
633 			break;
634 		default:
635 			regs->regs[rt] = 2;
636 		}
637 		return 0;
638 	case 29:
639 		regs->regs[rt] = ti->tp_value;
640 		return 0;
641 	default:
642 		return -1;
643 	}
644 }
645 
646 static int simulate_rdhwr_normal(struct pt_regs *regs, unsigned int opcode)
647 {
648 	if ((opcode & OPCODE) == SPEC3 && (opcode & FUNC) == RDHWR) {
649 		int rd = (opcode & RD) >> 11;
650 		int rt = (opcode & RT) >> 16;
651 
652 		simulate_rdhwr(regs, rd, rt);
653 		return 0;
654 	}
655 
656 	/* Not ours.  */
657 	return -1;
658 }
659 
660 static int simulate_rdhwr_mm(struct pt_regs *regs, unsigned short opcode)
661 {
662 	if ((opcode & MM_POOL32A_FUNC) == MM_RDHWR) {
663 		int rd = (opcode & MM_RS) >> 16;
664 		int rt = (opcode & MM_RT) >> 21;
665 		simulate_rdhwr(regs, rd, rt);
666 		return 0;
667 	}
668 
669 	/* Not ours.  */
670 	return -1;
671 }
672 
673 static int simulate_sync(struct pt_regs *regs, unsigned int opcode)
674 {
675 	if ((opcode & OPCODE) == SPEC0 && (opcode & FUNC) == SYNC) {
676 		perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS,
677 				1, regs, 0);
678 		return 0;
679 	}
680 
681 	return -1;			/* Must be something else ... */
682 }
683 
684 asmlinkage void do_ov(struct pt_regs *regs)
685 {
686 	enum ctx_state prev_state;
687 	siginfo_t info;
688 
689 	prev_state = exception_enter();
690 	die_if_kernel("Integer overflow", regs);
691 
692 	info.si_code = FPE_INTOVF;
693 	info.si_signo = SIGFPE;
694 	info.si_errno = 0;
695 	info.si_addr = (void __user *) regs->cp0_epc;
696 	force_sig_info(SIGFPE, &info, current);
697 	exception_exit(prev_state);
698 }
699 
700 int process_fpemu_return(int sig, void __user *fault_addr)
701 {
702 	if (sig == SIGSEGV || sig == SIGBUS) {
703 		struct siginfo si = {0};
704 		si.si_addr = fault_addr;
705 		si.si_signo = sig;
706 		if (sig == SIGSEGV) {
707 			down_read(&current->mm->mmap_sem);
708 			if (find_vma(current->mm, (unsigned long)fault_addr))
709 				si.si_code = SEGV_ACCERR;
710 			else
711 				si.si_code = SEGV_MAPERR;
712 			up_read(&current->mm->mmap_sem);
713 		} else {
714 			si.si_code = BUS_ADRERR;
715 		}
716 		force_sig_info(sig, &si, current);
717 		return 1;
718 	} else if (sig) {
719 		force_sig(sig, current);
720 		return 1;
721 	} else {
722 		return 0;
723 	}
724 }
725 
726 /*
727  * XXX Delayed fp exceptions when doing a lazy ctx switch XXX
728  */
729 asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
730 {
731 	enum ctx_state prev_state;
732 	siginfo_t info = {0};
733 
734 	prev_state = exception_enter();
735 	if (notify_die(DIE_FP, "FP exception", regs, 0, regs_to_trapnr(regs),
736 		       SIGFPE) == NOTIFY_STOP)
737 		goto out;
738 	die_if_kernel("FP exception in kernel code", regs);
739 
740 	if (fcr31 & FPU_CSR_UNI_X) {
741 		int sig;
742 		void __user *fault_addr = NULL;
743 
744 		/*
745 		 * Unimplemented operation exception.  If we've got the full
746 		 * software emulator on-board, let's use it...
747 		 *
748 		 * Force FPU to dump state into task/thread context.  We're
749 		 * moving a lot of data here for what is probably a single
750 		 * instruction, but the alternative is to pre-decode the FP
751 		 * register operands before invoking the emulator, which seems
752 		 * a bit extreme for what should be an infrequent event.
753 		 */
754 		/* Ensure 'resume' not overwrite saved fp context again. */
755 		lose_fpu(1);
756 
757 		/* Run the emulator */
758 		sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
759 					       &fault_addr);
760 
761 		/*
762 		 * We can't allow the emulated instruction to leave any of
763 		 * the cause bit set in $fcr31.
764 		 */
765 		current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
766 
767 		/* Restore the hardware register state */
768 		own_fpu(1);	/* Using the FPU again.	 */
769 
770 		/* If something went wrong, signal */
771 		process_fpemu_return(sig, fault_addr);
772 
773 		goto out;
774 	} else if (fcr31 & FPU_CSR_INV_X)
775 		info.si_code = FPE_FLTINV;
776 	else if (fcr31 & FPU_CSR_DIV_X)
777 		info.si_code = FPE_FLTDIV;
778 	else if (fcr31 & FPU_CSR_OVF_X)
779 		info.si_code = FPE_FLTOVF;
780 	else if (fcr31 & FPU_CSR_UDF_X)
781 		info.si_code = FPE_FLTUND;
782 	else if (fcr31 & FPU_CSR_INE_X)
783 		info.si_code = FPE_FLTRES;
784 	else
785 		info.si_code = __SI_FAULT;
786 	info.si_signo = SIGFPE;
787 	info.si_errno = 0;
788 	info.si_addr = (void __user *) regs->cp0_epc;
789 	force_sig_info(SIGFPE, &info, current);
790 
791 out:
792 	exception_exit(prev_state);
793 }
794 
795 static void do_trap_or_bp(struct pt_regs *regs, unsigned int code,
796 	const char *str)
797 {
798 	siginfo_t info;
799 	char b[40];
800 
801 #ifdef CONFIG_KGDB_LOW_LEVEL_TRAP
802 	if (kgdb_ll_trap(DIE_TRAP, str, regs, code, regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
803 		return;
804 #endif /* CONFIG_KGDB_LOW_LEVEL_TRAP */
805 
806 	if (notify_die(DIE_TRAP, str, regs, code, regs_to_trapnr(regs),
807 		       SIGTRAP) == NOTIFY_STOP)
808 		return;
809 
810 	/*
811 	 * A short test says that IRIX 5.3 sends SIGTRAP for all trap
812 	 * insns, even for trap and break codes that indicate arithmetic
813 	 * failures.  Weird ...
814 	 * But should we continue the brokenness???  --macro
815 	 */
816 	switch (code) {
817 	case BRK_OVERFLOW:
818 	case BRK_DIVZERO:
819 		scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
820 		die_if_kernel(b, regs);
821 		if (code == BRK_DIVZERO)
822 			info.si_code = FPE_INTDIV;
823 		else
824 			info.si_code = FPE_INTOVF;
825 		info.si_signo = SIGFPE;
826 		info.si_errno = 0;
827 		info.si_addr = (void __user *) regs->cp0_epc;
828 		force_sig_info(SIGFPE, &info, current);
829 		break;
830 	case BRK_BUG:
831 		die_if_kernel("Kernel bug detected", regs);
832 		force_sig(SIGTRAP, current);
833 		break;
834 	case BRK_MEMU:
835 		/*
836 		 * Address errors may be deliberately induced by the FPU
837 		 * emulator to retake control of the CPU after executing the
838 		 * instruction in the delay slot of an emulated branch.
839 		 *
840 		 * Terminate if exception was recognized as a delay slot return
841 		 * otherwise handle as normal.
842 		 */
843 		if (do_dsemulret(regs))
844 			return;
845 
846 		die_if_kernel("Math emu break/trap", regs);
847 		force_sig(SIGTRAP, current);
848 		break;
849 	default:
850 		scnprintf(b, sizeof(b), "%s instruction in kernel code", str);
851 		die_if_kernel(b, regs);
852 		force_sig(SIGTRAP, current);
853 	}
854 }
855 
856 asmlinkage void do_bp(struct pt_regs *regs)
857 {
858 	unsigned int opcode, bcode;
859 	enum ctx_state prev_state;
860 	unsigned long epc;
861 	u16 instr[2];
862 	mm_segment_t seg;
863 
864 	seg = get_fs();
865 	if (!user_mode(regs))
866 		set_fs(KERNEL_DS);
867 
868 	prev_state = exception_enter();
869 	if (get_isa16_mode(regs->cp0_epc)) {
870 		/* Calculate EPC. */
871 		epc = exception_epc(regs);
872 		if (cpu_has_mmips) {
873 			if ((__get_user(instr[0], (u16 __user *)msk_isa16_mode(epc)) ||
874 			    (__get_user(instr[1], (u16 __user *)msk_isa16_mode(epc + 2)))))
875 				goto out_sigsegv;
876 			opcode = (instr[0] << 16) | instr[1];
877 		} else {
878 			/* MIPS16e mode */
879 			if (__get_user(instr[0],
880 				       (u16 __user *)msk_isa16_mode(epc)))
881 				goto out_sigsegv;
882 			bcode = (instr[0] >> 6) & 0x3f;
883 			do_trap_or_bp(regs, bcode, "Break");
884 			goto out;
885 		}
886 	} else {
887 		if (__get_user(opcode,
888 			       (unsigned int __user *) exception_epc(regs)))
889 			goto out_sigsegv;
890 	}
891 
892 	/*
893 	 * There is the ancient bug in the MIPS assemblers that the break
894 	 * code starts left to bit 16 instead to bit 6 in the opcode.
895 	 * Gas is bug-compatible, but not always, grrr...
896 	 * We handle both cases with a simple heuristics.  --macro
897 	 */
898 	bcode = ((opcode >> 6) & ((1 << 20) - 1));
899 	if (bcode >= (1 << 10))
900 		bcode >>= 10;
901 
902 	/*
903 	 * notify the kprobe handlers, if instruction is likely to
904 	 * pertain to them.
905 	 */
906 	switch (bcode) {
907 	case BRK_KPROBE_BP:
908 		if (notify_die(DIE_BREAK, "debug", regs, bcode,
909 			       regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
910 			goto out;
911 		else
912 			break;
913 	case BRK_KPROBE_SSTEPBP:
914 		if (notify_die(DIE_SSTEPBP, "single_step", regs, bcode,
915 			       regs_to_trapnr(regs), SIGTRAP) == NOTIFY_STOP)
916 			goto out;
917 		else
918 			break;
919 	default:
920 		break;
921 	}
922 
923 	do_trap_or_bp(regs, bcode, "Break");
924 
925 out:
926 	set_fs(seg);
927 	exception_exit(prev_state);
928 	return;
929 
930 out_sigsegv:
931 	force_sig(SIGSEGV, current);
932 	goto out;
933 }
934 
935 asmlinkage void do_tr(struct pt_regs *regs)
936 {
937 	u32 opcode, tcode = 0;
938 	enum ctx_state prev_state;
939 	u16 instr[2];
940 	mm_segment_t seg;
941 	unsigned long epc = msk_isa16_mode(exception_epc(regs));
942 
943 	seg = get_fs();
944 	if (!user_mode(regs))
945 		set_fs(get_ds());
946 
947 	prev_state = exception_enter();
948 	if (get_isa16_mode(regs->cp0_epc)) {
949 		if (__get_user(instr[0], (u16 __user *)(epc + 0)) ||
950 		    __get_user(instr[1], (u16 __user *)(epc + 2)))
951 			goto out_sigsegv;
952 		opcode = (instr[0] << 16) | instr[1];
953 		/* Immediate versions don't provide a code.  */
954 		if (!(opcode & OPCODE))
955 			tcode = (opcode >> 12) & ((1 << 4) - 1);
956 	} else {
957 		if (__get_user(opcode, (u32 __user *)epc))
958 			goto out_sigsegv;
959 		/* Immediate versions don't provide a code.  */
960 		if (!(opcode & OPCODE))
961 			tcode = (opcode >> 6) & ((1 << 10) - 1);
962 	}
963 
964 	do_trap_or_bp(regs, tcode, "Trap");
965 
966 out:
967 	set_fs(seg);
968 	exception_exit(prev_state);
969 	return;
970 
971 out_sigsegv:
972 	force_sig(SIGSEGV, current);
973 	goto out;
974 }
975 
976 asmlinkage void do_ri(struct pt_regs *regs)
977 {
978 	unsigned int __user *epc = (unsigned int __user *)exception_epc(regs);
979 	unsigned long old_epc = regs->cp0_epc;
980 	unsigned long old31 = regs->regs[31];
981 	enum ctx_state prev_state;
982 	unsigned int opcode = 0;
983 	int status = -1;
984 
985 	prev_state = exception_enter();
986 	if (notify_die(DIE_RI, "RI Fault", regs, 0, regs_to_trapnr(regs),
987 		       SIGILL) == NOTIFY_STOP)
988 		goto out;
989 
990 	die_if_kernel("Reserved instruction in kernel code", regs);
991 
992 	if (unlikely(compute_return_epc(regs) < 0))
993 		goto out;
994 
995 	if (get_isa16_mode(regs->cp0_epc)) {
996 		unsigned short mmop[2] = { 0 };
997 
998 		if (unlikely(get_user(mmop[0], epc) < 0))
999 			status = SIGSEGV;
1000 		if (unlikely(get_user(mmop[1], epc) < 0))
1001 			status = SIGSEGV;
1002 		opcode = (mmop[0] << 16) | mmop[1];
1003 
1004 		if (status < 0)
1005 			status = simulate_rdhwr_mm(regs, opcode);
1006 	} else {
1007 		if (unlikely(get_user(opcode, epc) < 0))
1008 			status = SIGSEGV;
1009 
1010 		if (!cpu_has_llsc && status < 0)
1011 			status = simulate_llsc(regs, opcode);
1012 
1013 		if (status < 0)
1014 			status = simulate_rdhwr_normal(regs, opcode);
1015 
1016 		if (status < 0)
1017 			status = simulate_sync(regs, opcode);
1018 	}
1019 
1020 	if (status < 0)
1021 		status = SIGILL;
1022 
1023 	if (unlikely(status > 0)) {
1024 		regs->cp0_epc = old_epc;		/* Undo skip-over.  */
1025 		regs->regs[31] = old31;
1026 		force_sig(status, current);
1027 	}
1028 
1029 out:
1030 	exception_exit(prev_state);
1031 }
1032 
1033 /*
1034  * MIPS MT processors may have fewer FPU contexts than CPU threads. If we've
1035  * emulated more than some threshold number of instructions, force migration to
1036  * a "CPU" that has FP support.
1037  */
1038 static void mt_ase_fp_affinity(void)
1039 {
1040 #ifdef CONFIG_MIPS_MT_FPAFF
1041 	if (mt_fpemul_threshold > 0 &&
1042 	     ((current->thread.emulated_fp++ > mt_fpemul_threshold))) {
1043 		/*
1044 		 * If there's no FPU present, or if the application has already
1045 		 * restricted the allowed set to exclude any CPUs with FPUs,
1046 		 * we'll skip the procedure.
1047 		 */
1048 		if (cpus_intersects(current->cpus_allowed, mt_fpu_cpumask)) {
1049 			cpumask_t tmask;
1050 
1051 			current->thread.user_cpus_allowed
1052 				= current->cpus_allowed;
1053 			cpus_and(tmask, current->cpus_allowed,
1054 				mt_fpu_cpumask);
1055 			set_cpus_allowed_ptr(current, &tmask);
1056 			set_thread_flag(TIF_FPUBOUND);
1057 		}
1058 	}
1059 #endif /* CONFIG_MIPS_MT_FPAFF */
1060 }
1061 
1062 /*
1063  * No lock; only written during early bootup by CPU 0.
1064  */
1065 static RAW_NOTIFIER_HEAD(cu2_chain);
1066 
1067 int __ref register_cu2_notifier(struct notifier_block *nb)
1068 {
1069 	return raw_notifier_chain_register(&cu2_chain, nb);
1070 }
1071 
1072 int cu2_notifier_call_chain(unsigned long val, void *v)
1073 {
1074 	return raw_notifier_call_chain(&cu2_chain, val, v);
1075 }
1076 
1077 static int default_cu2_call(struct notifier_block *nfb, unsigned long action,
1078 	void *data)
1079 {
1080 	struct pt_regs *regs = data;
1081 
1082 	die_if_kernel("COP2: Unhandled kernel unaligned access or invalid "
1083 			      "instruction", regs);
1084 	force_sig(SIGILL, current);
1085 
1086 	return NOTIFY_OK;
1087 }
1088 
1089 static int enable_restore_fp_context(int msa)
1090 {
1091 	int err, was_fpu_owner;
1092 
1093 	if (!used_math()) {
1094 		/* First time FP context user. */
1095 		err = init_fpu();
1096 		if (msa && !err)
1097 			enable_msa();
1098 		if (!err)
1099 			set_used_math();
1100 		return err;
1101 	}
1102 
1103 	/*
1104 	 * This task has formerly used the FP context.
1105 	 *
1106 	 * If this thread has no live MSA vector context then we can simply
1107 	 * restore the scalar FP context. If it has live MSA vector context
1108 	 * (that is, it has or may have used MSA since last performing a
1109 	 * function call) then we'll need to restore the vector context. This
1110 	 * applies even if we're currently only executing a scalar FP
1111 	 * instruction. This is because if we were to later execute an MSA
1112 	 * instruction then we'd either have to:
1113 	 *
1114 	 *  - Restore the vector context & clobber any registers modified by
1115 	 *    scalar FP instructions between now & then.
1116 	 *
1117 	 * or
1118 	 *
1119 	 *  - Not restore the vector context & lose the most significant bits
1120 	 *    of all vector registers.
1121 	 *
1122 	 * Neither of those options is acceptable. We cannot restore the least
1123 	 * significant bits of the registers now & only restore the most
1124 	 * significant bits later because the most significant bits of any
1125 	 * vector registers whose aliased FP register is modified now will have
1126 	 * been zeroed. We'd have no way to know that when restoring the vector
1127 	 * context & thus may load an outdated value for the most significant
1128 	 * bits of a vector register.
1129 	 */
1130 	if (!msa && !thread_msa_context_live())
1131 		return own_fpu(1);
1132 
1133 	/*
1134 	 * This task is using or has previously used MSA. Thus we require
1135 	 * that Status.FR == 1.
1136 	 */
1137 	was_fpu_owner = is_fpu_owner();
1138 	err = own_fpu(0);
1139 	if (err)
1140 		return err;
1141 
1142 	enable_msa();
1143 	write_msa_csr(current->thread.fpu.msacsr);
1144 	set_thread_flag(TIF_USEDMSA);
1145 
1146 	/*
1147 	 * If this is the first time that the task is using MSA and it has
1148 	 * previously used scalar FP in this time slice then we already nave
1149 	 * FP context which we shouldn't clobber.
1150 	 */
1151 	if (!test_and_set_thread_flag(TIF_MSA_CTX_LIVE) && was_fpu_owner)
1152 		return 0;
1153 
1154 	/* We need to restore the vector context. */
1155 	restore_msa(current);
1156 	return 0;
1157 }
1158 
1159 asmlinkage void do_cpu(struct pt_regs *regs)
1160 {
1161 	enum ctx_state prev_state;
1162 	unsigned int __user *epc;
1163 	unsigned long old_epc, old31;
1164 	unsigned int opcode;
1165 	unsigned int cpid;
1166 	int status, err;
1167 	unsigned long __maybe_unused flags;
1168 
1169 	prev_state = exception_enter();
1170 	cpid = (regs->cp0_cause >> CAUSEB_CE) & 3;
1171 
1172 	if (cpid != 2)
1173 		die_if_kernel("do_cpu invoked from kernel context!", regs);
1174 
1175 	switch (cpid) {
1176 	case 0:
1177 		epc = (unsigned int __user *)exception_epc(regs);
1178 		old_epc = regs->cp0_epc;
1179 		old31 = regs->regs[31];
1180 		opcode = 0;
1181 		status = -1;
1182 
1183 		if (unlikely(compute_return_epc(regs) < 0))
1184 			goto out;
1185 
1186 		if (get_isa16_mode(regs->cp0_epc)) {
1187 			unsigned short mmop[2] = { 0 };
1188 
1189 			if (unlikely(get_user(mmop[0], epc) < 0))
1190 				status = SIGSEGV;
1191 			if (unlikely(get_user(mmop[1], epc) < 0))
1192 				status = SIGSEGV;
1193 			opcode = (mmop[0] << 16) | mmop[1];
1194 
1195 			if (status < 0)
1196 				status = simulate_rdhwr_mm(regs, opcode);
1197 		} else {
1198 			if (unlikely(get_user(opcode, epc) < 0))
1199 				status = SIGSEGV;
1200 
1201 			if (!cpu_has_llsc && status < 0)
1202 				status = simulate_llsc(regs, opcode);
1203 
1204 			if (status < 0)
1205 				status = simulate_rdhwr_normal(regs, opcode);
1206 		}
1207 
1208 		if (status < 0)
1209 			status = SIGILL;
1210 
1211 		if (unlikely(status > 0)) {
1212 			regs->cp0_epc = old_epc;	/* Undo skip-over.  */
1213 			regs->regs[31] = old31;
1214 			force_sig(status, current);
1215 		}
1216 
1217 		goto out;
1218 
1219 	case 3:
1220 		/*
1221 		 * Old (MIPS I and MIPS II) processors will set this code
1222 		 * for COP1X opcode instructions that replaced the original
1223 		 * COP3 space.	We don't limit COP1 space instructions in
1224 		 * the emulator according to the CPU ISA, so we want to
1225 		 * treat COP1X instructions consistently regardless of which
1226 		 * code the CPU chose.	Therefore we redirect this trap to
1227 		 * the FP emulator too.
1228 		 *
1229 		 * Then some newer FPU-less processors use this code
1230 		 * erroneously too, so they are covered by this choice
1231 		 * as well.
1232 		 */
1233 		if (raw_cpu_has_fpu)
1234 			break;
1235 		/* Fall through.  */
1236 
1237 	case 1:
1238 		err = enable_restore_fp_context(0);
1239 
1240 		if (!raw_cpu_has_fpu || err) {
1241 			int sig;
1242 			void __user *fault_addr = NULL;
1243 			sig = fpu_emulator_cop1Handler(regs,
1244 						       &current->thread.fpu,
1245 						       0, &fault_addr);
1246 			if (!process_fpemu_return(sig, fault_addr) && !err)
1247 				mt_ase_fp_affinity();
1248 		}
1249 
1250 		goto out;
1251 
1252 	case 2:
1253 		raw_notifier_call_chain(&cu2_chain, CU2_EXCEPTION, regs);
1254 		goto out;
1255 	}
1256 
1257 	force_sig(SIGILL, current);
1258 
1259 out:
1260 	exception_exit(prev_state);
1261 }
1262 
1263 asmlinkage void do_msa_fpe(struct pt_regs *regs)
1264 {
1265 	enum ctx_state prev_state;
1266 
1267 	prev_state = exception_enter();
1268 	die_if_kernel("do_msa_fpe invoked from kernel context!", regs);
1269 	force_sig(SIGFPE, current);
1270 	exception_exit(prev_state);
1271 }
1272 
1273 asmlinkage void do_msa(struct pt_regs *regs)
1274 {
1275 	enum ctx_state prev_state;
1276 	int err;
1277 
1278 	prev_state = exception_enter();
1279 
1280 	if (!cpu_has_msa || test_thread_flag(TIF_32BIT_FPREGS)) {
1281 		force_sig(SIGILL, current);
1282 		goto out;
1283 	}
1284 
1285 	die_if_kernel("do_msa invoked from kernel context!", regs);
1286 
1287 	err = enable_restore_fp_context(1);
1288 	if (err)
1289 		force_sig(SIGILL, current);
1290 out:
1291 	exception_exit(prev_state);
1292 }
1293 
1294 asmlinkage void do_mdmx(struct pt_regs *regs)
1295 {
1296 	enum ctx_state prev_state;
1297 
1298 	prev_state = exception_enter();
1299 	force_sig(SIGILL, current);
1300 	exception_exit(prev_state);
1301 }
1302 
1303 /*
1304  * Called with interrupts disabled.
1305  */
1306 asmlinkage void do_watch(struct pt_regs *regs)
1307 {
1308 	enum ctx_state prev_state;
1309 	u32 cause;
1310 
1311 	prev_state = exception_enter();
1312 	/*
1313 	 * Clear WP (bit 22) bit of cause register so we don't loop
1314 	 * forever.
1315 	 */
1316 	cause = read_c0_cause();
1317 	cause &= ~(1 << 22);
1318 	write_c0_cause(cause);
1319 
1320 	/*
1321 	 * If the current thread has the watch registers loaded, save
1322 	 * their values and send SIGTRAP.  Otherwise another thread
1323 	 * left the registers set, clear them and continue.
1324 	 */
1325 	if (test_tsk_thread_flag(current, TIF_LOAD_WATCH)) {
1326 		mips_read_watch_registers();
1327 		local_irq_enable();
1328 		force_sig(SIGTRAP, current);
1329 	} else {
1330 		mips_clear_watch_registers();
1331 		local_irq_enable();
1332 	}
1333 	exception_exit(prev_state);
1334 }
1335 
1336 asmlinkage void do_mcheck(struct pt_regs *regs)
1337 {
1338 	const int field = 2 * sizeof(unsigned long);
1339 	int multi_match = regs->cp0_status & ST0_TS;
1340 	enum ctx_state prev_state;
1341 
1342 	prev_state = exception_enter();
1343 	show_regs(regs);
1344 
1345 	if (multi_match) {
1346 		printk("Index	: %0x\n", read_c0_index());
1347 		printk("Pagemask: %0x\n", read_c0_pagemask());
1348 		printk("EntryHi : %0*lx\n", field, read_c0_entryhi());
1349 		printk("EntryLo0: %0*lx\n", field, read_c0_entrylo0());
1350 		printk("EntryLo1: %0*lx\n", field, read_c0_entrylo1());
1351 		printk("\n");
1352 		dump_tlb_all();
1353 	}
1354 
1355 	show_code((unsigned int __user *) regs->cp0_epc);
1356 
1357 	/*
1358 	 * Some chips may have other causes of machine check (e.g. SB1
1359 	 * graduation timer)
1360 	 */
1361 	panic("Caught Machine Check exception - %scaused by multiple "
1362 	      "matching entries in the TLB.",
1363 	      (multi_match) ? "" : "not ");
1364 }
1365 
1366 asmlinkage void do_mt(struct pt_regs *regs)
1367 {
1368 	int subcode;
1369 
1370 	subcode = (read_vpe_c0_vpecontrol() & VPECONTROL_EXCPT)
1371 			>> VPECONTROL_EXCPT_SHIFT;
1372 	switch (subcode) {
1373 	case 0:
1374 		printk(KERN_DEBUG "Thread Underflow\n");
1375 		break;
1376 	case 1:
1377 		printk(KERN_DEBUG "Thread Overflow\n");
1378 		break;
1379 	case 2:
1380 		printk(KERN_DEBUG "Invalid YIELD Qualifier\n");
1381 		break;
1382 	case 3:
1383 		printk(KERN_DEBUG "Gating Storage Exception\n");
1384 		break;
1385 	case 4:
1386 		printk(KERN_DEBUG "YIELD Scheduler Exception\n");
1387 		break;
1388 	case 5:
1389 		printk(KERN_DEBUG "Gating Storage Scheduler Exception\n");
1390 		break;
1391 	default:
1392 		printk(KERN_DEBUG "*** UNKNOWN THREAD EXCEPTION %d ***\n",
1393 			subcode);
1394 		break;
1395 	}
1396 	die_if_kernel("MIPS MT Thread exception in kernel", regs);
1397 
1398 	force_sig(SIGILL, current);
1399 }
1400 
1401 
1402 asmlinkage void do_dsp(struct pt_regs *regs)
1403 {
1404 	if (cpu_has_dsp)
1405 		panic("Unexpected DSP exception");
1406 
1407 	force_sig(SIGILL, current);
1408 }
1409 
1410 asmlinkage void do_reserved(struct pt_regs *regs)
1411 {
1412 	/*
1413 	 * Game over - no way to handle this if it ever occurs.	 Most probably
1414 	 * caused by a new unknown cpu type or after another deadly
1415 	 * hard/software error.
1416 	 */
1417 	show_regs(regs);
1418 	panic("Caught reserved exception %ld - should not happen.",
1419 	      (regs->cp0_cause & 0x7f) >> 2);
1420 }
1421 
1422 static int __initdata l1parity = 1;
1423 static int __init nol1parity(char *s)
1424 {
1425 	l1parity = 0;
1426 	return 1;
1427 }
1428 __setup("nol1par", nol1parity);
1429 static int __initdata l2parity = 1;
1430 static int __init nol2parity(char *s)
1431 {
1432 	l2parity = 0;
1433 	return 1;
1434 }
1435 __setup("nol2par", nol2parity);
1436 
1437 /*
1438  * Some MIPS CPUs can enable/disable for cache parity detection, but do
1439  * it different ways.
1440  */
1441 static inline void parity_protection_init(void)
1442 {
1443 	switch (current_cpu_type()) {
1444 	case CPU_24K:
1445 	case CPU_34K:
1446 	case CPU_74K:
1447 	case CPU_1004K:
1448 	case CPU_1074K:
1449 	case CPU_INTERAPTIV:
1450 	case CPU_PROAPTIV:
1451 	case CPU_P5600:
1452 		{
1453 #define ERRCTL_PE	0x80000000
1454 #define ERRCTL_L2P	0x00800000
1455 			unsigned long errctl;
1456 			unsigned int l1parity_present, l2parity_present;
1457 
1458 			errctl = read_c0_ecc();
1459 			errctl &= ~(ERRCTL_PE|ERRCTL_L2P);
1460 
1461 			/* probe L1 parity support */
1462 			write_c0_ecc(errctl | ERRCTL_PE);
1463 			back_to_back_c0_hazard();
1464 			l1parity_present = (read_c0_ecc() & ERRCTL_PE);
1465 
1466 			/* probe L2 parity support */
1467 			write_c0_ecc(errctl|ERRCTL_L2P);
1468 			back_to_back_c0_hazard();
1469 			l2parity_present = (read_c0_ecc() & ERRCTL_L2P);
1470 
1471 			if (l1parity_present && l2parity_present) {
1472 				if (l1parity)
1473 					errctl |= ERRCTL_PE;
1474 				if (l1parity ^ l2parity)
1475 					errctl |= ERRCTL_L2P;
1476 			} else if (l1parity_present) {
1477 				if (l1parity)
1478 					errctl |= ERRCTL_PE;
1479 			} else if (l2parity_present) {
1480 				if (l2parity)
1481 					errctl |= ERRCTL_L2P;
1482 			} else {
1483 				/* No parity available */
1484 			}
1485 
1486 			printk(KERN_INFO "Writing ErrCtl register=%08lx\n", errctl);
1487 
1488 			write_c0_ecc(errctl);
1489 			back_to_back_c0_hazard();
1490 			errctl = read_c0_ecc();
1491 			printk(KERN_INFO "Readback ErrCtl register=%08lx\n", errctl);
1492 
1493 			if (l1parity_present)
1494 				printk(KERN_INFO "Cache parity protection %sabled\n",
1495 				       (errctl & ERRCTL_PE) ? "en" : "dis");
1496 
1497 			if (l2parity_present) {
1498 				if (l1parity_present && l1parity)
1499 					errctl ^= ERRCTL_L2P;
1500 				printk(KERN_INFO "L2 cache parity protection %sabled\n",
1501 				       (errctl & ERRCTL_L2P) ? "en" : "dis");
1502 			}
1503 		}
1504 		break;
1505 
1506 	case CPU_5KC:
1507 	case CPU_5KE:
1508 	case CPU_LOONGSON1:
1509 		write_c0_ecc(0x80000000);
1510 		back_to_back_c0_hazard();
1511 		/* Set the PE bit (bit 31) in the c0_errctl register. */
1512 		printk(KERN_INFO "Cache parity protection %sabled\n",
1513 		       (read_c0_ecc() & 0x80000000) ? "en" : "dis");
1514 		break;
1515 	case CPU_20KC:
1516 	case CPU_25KF:
1517 		/* Clear the DE bit (bit 16) in the c0_status register. */
1518 		printk(KERN_INFO "Enable cache parity protection for "
1519 		       "MIPS 20KC/25KF CPUs.\n");
1520 		clear_c0_status(ST0_DE);
1521 		break;
1522 	default:
1523 		break;
1524 	}
1525 }
1526 
1527 asmlinkage void cache_parity_error(void)
1528 {
1529 	const int field = 2 * sizeof(unsigned long);
1530 	unsigned int reg_val;
1531 
1532 	/* For the moment, report the problem and hang. */
1533 	printk("Cache error exception:\n");
1534 	printk("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1535 	reg_val = read_c0_cacheerr();
1536 	printk("c0_cacheerr == %08x\n", reg_val);
1537 
1538 	printk("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1539 	       reg_val & (1<<30) ? "secondary" : "primary",
1540 	       reg_val & (1<<31) ? "data" : "insn");
1541 	if (cpu_has_mips_r2 &&
1542 	    ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
1543 		pr_err("Error bits: %s%s%s%s%s%s%s%s\n",
1544 			reg_val & (1<<29) ? "ED " : "",
1545 			reg_val & (1<<28) ? "ET " : "",
1546 			reg_val & (1<<27) ? "ES " : "",
1547 			reg_val & (1<<26) ? "EE " : "",
1548 			reg_val & (1<<25) ? "EB " : "",
1549 			reg_val & (1<<24) ? "EI " : "",
1550 			reg_val & (1<<23) ? "E1 " : "",
1551 			reg_val & (1<<22) ? "E0 " : "");
1552 	} else {
1553 		pr_err("Error bits: %s%s%s%s%s%s%s\n",
1554 			reg_val & (1<<29) ? "ED " : "",
1555 			reg_val & (1<<28) ? "ET " : "",
1556 			reg_val & (1<<26) ? "EE " : "",
1557 			reg_val & (1<<25) ? "EB " : "",
1558 			reg_val & (1<<24) ? "EI " : "",
1559 			reg_val & (1<<23) ? "E1 " : "",
1560 			reg_val & (1<<22) ? "E0 " : "");
1561 	}
1562 	printk("IDX: 0x%08x\n", reg_val & ((1<<22)-1));
1563 
1564 #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
1565 	if (reg_val & (1<<22))
1566 		printk("DErrAddr0: 0x%0*lx\n", field, read_c0_derraddr0());
1567 
1568 	if (reg_val & (1<<23))
1569 		printk("DErrAddr1: 0x%0*lx\n", field, read_c0_derraddr1());
1570 #endif
1571 
1572 	panic("Can't handle the cache error!");
1573 }
1574 
1575 asmlinkage void do_ftlb(void)
1576 {
1577 	const int field = 2 * sizeof(unsigned long);
1578 	unsigned int reg_val;
1579 
1580 	/* For the moment, report the problem and hang. */
1581 	if (cpu_has_mips_r2 &&
1582 	    ((current_cpu_data.processor_id & 0xff0000) == PRID_COMP_MIPS)) {
1583 		pr_err("FTLB error exception, cp0_ecc=0x%08x:\n",
1584 		       read_c0_ecc());
1585 		pr_err("cp0_errorepc == %0*lx\n", field, read_c0_errorepc());
1586 		reg_val = read_c0_cacheerr();
1587 		pr_err("c0_cacheerr == %08x\n", reg_val);
1588 
1589 		if ((reg_val & 0xc0000000) == 0xc0000000) {
1590 			pr_err("Decoded c0_cacheerr: FTLB parity error\n");
1591 		} else {
1592 			pr_err("Decoded c0_cacheerr: %s cache fault in %s reference.\n",
1593 			       reg_val & (1<<30) ? "secondary" : "primary",
1594 			       reg_val & (1<<31) ? "data" : "insn");
1595 		}
1596 	} else {
1597 		pr_err("FTLB error exception\n");
1598 	}
1599 	/* Just print the cacheerr bits for now */
1600 	cache_parity_error();
1601 }
1602 
1603 /*
1604  * SDBBP EJTAG debug exception handler.
1605  * We skip the instruction and return to the next instruction.
1606  */
1607 void ejtag_exception_handler(struct pt_regs *regs)
1608 {
1609 	const int field = 2 * sizeof(unsigned long);
1610 	unsigned long depc, old_epc, old_ra;
1611 	unsigned int debug;
1612 
1613 	printk(KERN_DEBUG "SDBBP EJTAG debug exception - not handled yet, just ignored!\n");
1614 	depc = read_c0_depc();
1615 	debug = read_c0_debug();
1616 	printk(KERN_DEBUG "c0_depc = %0*lx, DEBUG = %08x\n", field, depc, debug);
1617 	if (debug & 0x80000000) {
1618 		/*
1619 		 * In branch delay slot.
1620 		 * We cheat a little bit here and use EPC to calculate the
1621 		 * debug return address (DEPC). EPC is restored after the
1622 		 * calculation.
1623 		 */
1624 		old_epc = regs->cp0_epc;
1625 		old_ra = regs->regs[31];
1626 		regs->cp0_epc = depc;
1627 		compute_return_epc(regs);
1628 		depc = regs->cp0_epc;
1629 		regs->cp0_epc = old_epc;
1630 		regs->regs[31] = old_ra;
1631 	} else
1632 		depc += 4;
1633 	write_c0_depc(depc);
1634 
1635 #if 0
1636 	printk(KERN_DEBUG "\n\n----- Enable EJTAG single stepping ----\n\n");
1637 	write_c0_debug(debug | 0x100);
1638 #endif
1639 }
1640 
1641 /*
1642  * NMI exception handler.
1643  * No lock; only written during early bootup by CPU 0.
1644  */
1645 static RAW_NOTIFIER_HEAD(nmi_chain);
1646 
1647 int register_nmi_notifier(struct notifier_block *nb)
1648 {
1649 	return raw_notifier_chain_register(&nmi_chain, nb);
1650 }
1651 
1652 void __noreturn nmi_exception_handler(struct pt_regs *regs)
1653 {
1654 	char str[100];
1655 
1656 	raw_notifier_call_chain(&nmi_chain, 0, regs);
1657 	bust_spinlocks(1);
1658 	snprintf(str, 100, "CPU%d NMI taken, CP0_EPC=%lx\n",
1659 		 smp_processor_id(), regs->cp0_epc);
1660 	regs->cp0_epc = read_c0_errorepc();
1661 	die(str, regs);
1662 }
1663 
1664 #define VECTORSPACING 0x100	/* for EI/VI mode */
1665 
1666 unsigned long ebase;
1667 unsigned long exception_handlers[32];
1668 unsigned long vi_handlers[64];
1669 
1670 void __init *set_except_vector(int n, void *addr)
1671 {
1672 	unsigned long handler = (unsigned long) addr;
1673 	unsigned long old_handler;
1674 
1675 #ifdef CONFIG_CPU_MICROMIPS
1676 	/*
1677 	 * Only the TLB handlers are cache aligned with an even
1678 	 * address. All other handlers are on an odd address and
1679 	 * require no modification. Otherwise, MIPS32 mode will
1680 	 * be entered when handling any TLB exceptions. That
1681 	 * would be bad...since we must stay in microMIPS mode.
1682 	 */
1683 	if (!(handler & 0x1))
1684 		handler |= 1;
1685 #endif
1686 	old_handler = xchg(&exception_handlers[n], handler);
1687 
1688 	if (n == 0 && cpu_has_divec) {
1689 #ifdef CONFIG_CPU_MICROMIPS
1690 		unsigned long jump_mask = ~((1 << 27) - 1);
1691 #else
1692 		unsigned long jump_mask = ~((1 << 28) - 1);
1693 #endif
1694 		u32 *buf = (u32 *)(ebase + 0x200);
1695 		unsigned int k0 = 26;
1696 		if ((handler & jump_mask) == ((ebase + 0x200) & jump_mask)) {
1697 			uasm_i_j(&buf, handler & ~jump_mask);
1698 			uasm_i_nop(&buf);
1699 		} else {
1700 			UASM_i_LA(&buf, k0, handler);
1701 			uasm_i_jr(&buf, k0);
1702 			uasm_i_nop(&buf);
1703 		}
1704 		local_flush_icache_range(ebase + 0x200, (unsigned long)buf);
1705 	}
1706 	return (void *)old_handler;
1707 }
1708 
1709 static void do_default_vi(void)
1710 {
1711 	show_regs(get_irq_regs());
1712 	panic("Caught unexpected vectored interrupt.");
1713 }
1714 
1715 static void *set_vi_srs_handler(int n, vi_handler_t addr, int srs)
1716 {
1717 	unsigned long handler;
1718 	unsigned long old_handler = vi_handlers[n];
1719 	int srssets = current_cpu_data.srsets;
1720 	u16 *h;
1721 	unsigned char *b;
1722 
1723 	BUG_ON(!cpu_has_veic && !cpu_has_vint);
1724 
1725 	if (addr == NULL) {
1726 		handler = (unsigned long) do_default_vi;
1727 		srs = 0;
1728 	} else
1729 		handler = (unsigned long) addr;
1730 	vi_handlers[n] = handler;
1731 
1732 	b = (unsigned char *)(ebase + 0x200 + n*VECTORSPACING);
1733 
1734 	if (srs >= srssets)
1735 		panic("Shadow register set %d not supported", srs);
1736 
1737 	if (cpu_has_veic) {
1738 		if (board_bind_eic_interrupt)
1739 			board_bind_eic_interrupt(n, srs);
1740 	} else if (cpu_has_vint) {
1741 		/* SRSMap is only defined if shadow sets are implemented */
1742 		if (srssets > 1)
1743 			change_c0_srsmap(0xf << n*4, srs << n*4);
1744 	}
1745 
1746 	if (srs == 0) {
1747 		/*
1748 		 * If no shadow set is selected then use the default handler
1749 		 * that does normal register saving and standard interrupt exit
1750 		 */
1751 		extern char except_vec_vi, except_vec_vi_lui;
1752 		extern char except_vec_vi_ori, except_vec_vi_end;
1753 		extern char rollback_except_vec_vi;
1754 		char *vec_start = using_rollback_handler() ?
1755 			&rollback_except_vec_vi : &except_vec_vi;
1756 #if defined(CONFIG_CPU_MICROMIPS) || defined(CONFIG_CPU_BIG_ENDIAN)
1757 		const int lui_offset = &except_vec_vi_lui - vec_start + 2;
1758 		const int ori_offset = &except_vec_vi_ori - vec_start + 2;
1759 #else
1760 		const int lui_offset = &except_vec_vi_lui - vec_start;
1761 		const int ori_offset = &except_vec_vi_ori - vec_start;
1762 #endif
1763 		const int handler_len = &except_vec_vi_end - vec_start;
1764 
1765 		if (handler_len > VECTORSPACING) {
1766 			/*
1767 			 * Sigh... panicing won't help as the console
1768 			 * is probably not configured :(
1769 			 */
1770 			panic("VECTORSPACING too small");
1771 		}
1772 
1773 		set_handler(((unsigned long)b - ebase), vec_start,
1774 #ifdef CONFIG_CPU_MICROMIPS
1775 				(handler_len - 1));
1776 #else
1777 				handler_len);
1778 #endif
1779 		h = (u16 *)(b + lui_offset);
1780 		*h = (handler >> 16) & 0xffff;
1781 		h = (u16 *)(b + ori_offset);
1782 		*h = (handler & 0xffff);
1783 		local_flush_icache_range((unsigned long)b,
1784 					 (unsigned long)(b+handler_len));
1785 	}
1786 	else {
1787 		/*
1788 		 * In other cases jump directly to the interrupt handler. It
1789 		 * is the handler's responsibility to save registers if required
1790 		 * (eg hi/lo) and return from the exception using "eret".
1791 		 */
1792 		u32 insn;
1793 
1794 		h = (u16 *)b;
1795 		/* j handler */
1796 #ifdef CONFIG_CPU_MICROMIPS
1797 		insn = 0xd4000000 | (((u32)handler & 0x07ffffff) >> 1);
1798 #else
1799 		insn = 0x08000000 | (((u32)handler & 0x0fffffff) >> 2);
1800 #endif
1801 		h[0] = (insn >> 16) & 0xffff;
1802 		h[1] = insn & 0xffff;
1803 		h[2] = 0;
1804 		h[3] = 0;
1805 		local_flush_icache_range((unsigned long)b,
1806 					 (unsigned long)(b+8));
1807 	}
1808 
1809 	return (void *)old_handler;
1810 }
1811 
1812 void *set_vi_handler(int n, vi_handler_t addr)
1813 {
1814 	return set_vi_srs_handler(n, addr, 0);
1815 }
1816 
1817 extern void tlb_init(void);
1818 
1819 /*
1820  * Timer interrupt
1821  */
1822 int cp0_compare_irq;
1823 EXPORT_SYMBOL_GPL(cp0_compare_irq);
1824 int cp0_compare_irq_shift;
1825 
1826 /*
1827  * Performance counter IRQ or -1 if shared with timer
1828  */
1829 int cp0_perfcount_irq;
1830 EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
1831 
1832 static int noulri;
1833 
1834 static int __init ulri_disable(char *s)
1835 {
1836 	pr_info("Disabling ulri\n");
1837 	noulri = 1;
1838 
1839 	return 1;
1840 }
1841 __setup("noulri", ulri_disable);
1842 
1843 /* configure STATUS register */
1844 static void configure_status(void)
1845 {
1846 	/*
1847 	 * Disable coprocessors and select 32-bit or 64-bit addressing
1848 	 * and the 16/32 or 32/32 FPR register model.  Reset the BEV
1849 	 * flag that some firmware may have left set and the TS bit (for
1850 	 * IP27).  Set XX for ISA IV code to work.
1851 	 */
1852 	unsigned int status_set = ST0_CU0;
1853 #ifdef CONFIG_64BIT
1854 	status_set |= ST0_FR|ST0_KX|ST0_SX|ST0_UX;
1855 #endif
1856 	if (current_cpu_data.isa_level & MIPS_CPU_ISA_IV)
1857 		status_set |= ST0_XX;
1858 	if (cpu_has_dsp)
1859 		status_set |= ST0_MX;
1860 
1861 	change_c0_status(ST0_CU|ST0_MX|ST0_RE|ST0_FR|ST0_BEV|ST0_TS|ST0_KX|ST0_SX|ST0_UX,
1862 			 status_set);
1863 }
1864 
1865 /* configure HWRENA register */
1866 static void configure_hwrena(void)
1867 {
1868 	unsigned int hwrena = cpu_hwrena_impl_bits;
1869 
1870 	if (cpu_has_mips_r2)
1871 		hwrena |= 0x0000000f;
1872 
1873 	if (!noulri && cpu_has_userlocal)
1874 		hwrena |= (1 << 29);
1875 
1876 	if (hwrena)
1877 		write_c0_hwrena(hwrena);
1878 }
1879 
1880 static void configure_exception_vector(void)
1881 {
1882 	if (cpu_has_veic || cpu_has_vint) {
1883 		unsigned long sr = set_c0_status(ST0_BEV);
1884 		write_c0_ebase(ebase);
1885 		write_c0_status(sr);
1886 		/* Setting vector spacing enables EI/VI mode  */
1887 		change_c0_intctl(0x3e0, VECTORSPACING);
1888 	}
1889 	if (cpu_has_divec) {
1890 		if (cpu_has_mipsmt) {
1891 			unsigned int vpflags = dvpe();
1892 			set_c0_cause(CAUSEF_IV);
1893 			evpe(vpflags);
1894 		} else
1895 			set_c0_cause(CAUSEF_IV);
1896 	}
1897 }
1898 
1899 void per_cpu_trap_init(bool is_boot_cpu)
1900 {
1901 	unsigned int cpu = smp_processor_id();
1902 
1903 	configure_status();
1904 	configure_hwrena();
1905 
1906 	configure_exception_vector();
1907 
1908 	/*
1909 	 * Before R2 both interrupt numbers were fixed to 7, so on R2 only:
1910 	 *
1911 	 *  o read IntCtl.IPTI to determine the timer interrupt
1912 	 *  o read IntCtl.IPPCI to determine the performance counter interrupt
1913 	 */
1914 	if (cpu_has_mips_r2) {
1915 		cp0_compare_irq_shift = CAUSEB_TI - CAUSEB_IP;
1916 		cp0_compare_irq = (read_c0_intctl() >> INTCTLB_IPTI) & 7;
1917 		cp0_perfcount_irq = (read_c0_intctl() >> INTCTLB_IPPCI) & 7;
1918 		if (cp0_perfcount_irq == cp0_compare_irq)
1919 			cp0_perfcount_irq = -1;
1920 	} else {
1921 		cp0_compare_irq = CP0_LEGACY_COMPARE_IRQ;
1922 		cp0_compare_irq_shift = CP0_LEGACY_PERFCNT_IRQ;
1923 		cp0_perfcount_irq = -1;
1924 	}
1925 
1926 	if (!cpu_data[cpu].asid_cache)
1927 		cpu_data[cpu].asid_cache = ASID_FIRST_VERSION;
1928 
1929 	atomic_inc(&init_mm.mm_count);
1930 	current->active_mm = &init_mm;
1931 	BUG_ON(current->mm);
1932 	enter_lazy_tlb(&init_mm, current);
1933 
1934 		/* Boot CPU's cache setup in setup_arch(). */
1935 		if (!is_boot_cpu)
1936 			cpu_cache_init();
1937 		tlb_init();
1938 	TLBMISS_HANDLER_SETUP();
1939 }
1940 
1941 /* Install CPU exception handler */
1942 void set_handler(unsigned long offset, void *addr, unsigned long size)
1943 {
1944 #ifdef CONFIG_CPU_MICROMIPS
1945 	memcpy((void *)(ebase + offset), ((unsigned char *)addr - 1), size);
1946 #else
1947 	memcpy((void *)(ebase + offset), addr, size);
1948 #endif
1949 	local_flush_icache_range(ebase + offset, ebase + offset + size);
1950 }
1951 
1952 static char panic_null_cerr[] =
1953 	"Trying to set NULL cache error exception handler";
1954 
1955 /*
1956  * Install uncached CPU exception handler.
1957  * This is suitable only for the cache error exception which is the only
1958  * exception handler that is being run uncached.
1959  */
1960 void set_uncached_handler(unsigned long offset, void *addr,
1961 	unsigned long size)
1962 {
1963 	unsigned long uncached_ebase = CKSEG1ADDR(ebase);
1964 
1965 	if (!addr)
1966 		panic(panic_null_cerr);
1967 
1968 	memcpy((void *)(uncached_ebase + offset), addr, size);
1969 }
1970 
1971 static int __initdata rdhwr_noopt;
1972 static int __init set_rdhwr_noopt(char *str)
1973 {
1974 	rdhwr_noopt = 1;
1975 	return 1;
1976 }
1977 
1978 __setup("rdhwr_noopt", set_rdhwr_noopt);
1979 
1980 void __init trap_init(void)
1981 {
1982 	extern char except_vec3_generic;
1983 	extern char except_vec4;
1984 	extern char except_vec3_r4000;
1985 	unsigned long i;
1986 
1987 	check_wait();
1988 
1989 #if defined(CONFIG_KGDB)
1990 	if (kgdb_early_setup)
1991 		return; /* Already done */
1992 #endif
1993 
1994 	if (cpu_has_veic || cpu_has_vint) {
1995 		unsigned long size = 0x200 + VECTORSPACING*64;
1996 		ebase = (unsigned long)
1997 			__alloc_bootmem(size, 1 << fls(size), 0);
1998 	} else {
1999 #ifdef CONFIG_KVM_GUEST
2000 #define KVM_GUEST_KSEG0     0x40000000
2001         ebase = KVM_GUEST_KSEG0;
2002 #else
2003         ebase = CKSEG0;
2004 #endif
2005 		if (cpu_has_mips_r2)
2006 			ebase += (read_c0_ebase() & 0x3ffff000);
2007 	}
2008 
2009 	if (cpu_has_mmips) {
2010 		unsigned int config3 = read_c0_config3();
2011 
2012 		if (IS_ENABLED(CONFIG_CPU_MICROMIPS))
2013 			write_c0_config3(config3 | MIPS_CONF3_ISA_OE);
2014 		else
2015 			write_c0_config3(config3 & ~MIPS_CONF3_ISA_OE);
2016 	}
2017 
2018 	if (board_ebase_setup)
2019 		board_ebase_setup();
2020 	per_cpu_trap_init(true);
2021 
2022 	/*
2023 	 * Copy the generic exception handlers to their final destination.
2024 	 * This will be overriden later as suitable for a particular
2025 	 * configuration.
2026 	 */
2027 	set_handler(0x180, &except_vec3_generic, 0x80);
2028 
2029 	/*
2030 	 * Setup default vectors
2031 	 */
2032 	for (i = 0; i <= 31; i++)
2033 		set_except_vector(i, handle_reserved);
2034 
2035 	/*
2036 	 * Copy the EJTAG debug exception vector handler code to it's final
2037 	 * destination.
2038 	 */
2039 	if (cpu_has_ejtag && board_ejtag_handler_setup)
2040 		board_ejtag_handler_setup();
2041 
2042 	/*
2043 	 * Only some CPUs have the watch exceptions.
2044 	 */
2045 	if (cpu_has_watch)
2046 		set_except_vector(23, handle_watch);
2047 
2048 	/*
2049 	 * Initialise interrupt handlers
2050 	 */
2051 	if (cpu_has_veic || cpu_has_vint) {
2052 		int nvec = cpu_has_veic ? 64 : 8;
2053 		for (i = 0; i < nvec; i++)
2054 			set_vi_handler(i, NULL);
2055 	}
2056 	else if (cpu_has_divec)
2057 		set_handler(0x200, &except_vec4, 0x8);
2058 
2059 	/*
2060 	 * Some CPUs can enable/disable for cache parity detection, but does
2061 	 * it different ways.
2062 	 */
2063 	parity_protection_init();
2064 
2065 	/*
2066 	 * The Data Bus Errors / Instruction Bus Errors are signaled
2067 	 * by external hardware.  Therefore these two exceptions
2068 	 * may have board specific handlers.
2069 	 */
2070 	if (board_be_init)
2071 		board_be_init();
2072 
2073 	set_except_vector(0, using_rollback_handler() ? rollback_handle_int
2074 						      : handle_int);
2075 	set_except_vector(1, handle_tlbm);
2076 	set_except_vector(2, handle_tlbl);
2077 	set_except_vector(3, handle_tlbs);
2078 
2079 	set_except_vector(4, handle_adel);
2080 	set_except_vector(5, handle_ades);
2081 
2082 	set_except_vector(6, handle_ibe);
2083 	set_except_vector(7, handle_dbe);
2084 
2085 	set_except_vector(8, handle_sys);
2086 	set_except_vector(9, handle_bp);
2087 	set_except_vector(10, rdhwr_noopt ? handle_ri :
2088 			  (cpu_has_vtag_icache ?
2089 			   handle_ri_rdhwr_vivt : handle_ri_rdhwr));
2090 	set_except_vector(11, handle_cpu);
2091 	set_except_vector(12, handle_ov);
2092 	set_except_vector(13, handle_tr);
2093 	set_except_vector(14, handle_msa_fpe);
2094 
2095 	if (current_cpu_type() == CPU_R6000 ||
2096 	    current_cpu_type() == CPU_R6000A) {
2097 		/*
2098 		 * The R6000 is the only R-series CPU that features a machine
2099 		 * check exception (similar to the R4000 cache error) and
2100 		 * unaligned ldc1/sdc1 exception.  The handlers have not been
2101 		 * written yet.	 Well, anyway there is no R6000 machine on the
2102 		 * current list of targets for Linux/MIPS.
2103 		 * (Duh, crap, there is someone with a triple R6k machine)
2104 		 */
2105 		//set_except_vector(14, handle_mc);
2106 		//set_except_vector(15, handle_ndc);
2107 	}
2108 
2109 
2110 	if (board_nmi_handler_setup)
2111 		board_nmi_handler_setup();
2112 
2113 	if (cpu_has_fpu && !cpu_has_nofpuex)
2114 		set_except_vector(15, handle_fpe);
2115 
2116 	set_except_vector(16, handle_ftlb);
2117 	set_except_vector(21, handle_msa);
2118 	set_except_vector(22, handle_mdmx);
2119 
2120 	if (cpu_has_mcheck)
2121 		set_except_vector(24, handle_mcheck);
2122 
2123 	if (cpu_has_mipsmt)
2124 		set_except_vector(25, handle_mt);
2125 
2126 	set_except_vector(26, handle_dsp);
2127 
2128 	if (board_cache_error_setup)
2129 		board_cache_error_setup();
2130 
2131 	if (cpu_has_vce)
2132 		/* Special exception: R4[04]00 uses also the divec space. */
2133 		set_handler(0x180, &except_vec3_r4000, 0x100);
2134 	else if (cpu_has_4kex)
2135 		set_handler(0x180, &except_vec3_generic, 0x80);
2136 	else
2137 		set_handler(0x080, &except_vec3_generic, 0x80);
2138 
2139 	local_flush_icache_range(ebase, ebase + 0x400);
2140 
2141 	sort_extable(__start___dbe_table, __stop___dbe_table);
2142 
2143 	cu2_notifier(default_cu2_call, 0x80000000);	/* Run last  */
2144 }
2145 
2146 static int trap_pm_notifier(struct notifier_block *self, unsigned long cmd,
2147 			    void *v)
2148 {
2149 	switch (cmd) {
2150 	case CPU_PM_ENTER_FAILED:
2151 	case CPU_PM_EXIT:
2152 		configure_status();
2153 		configure_hwrena();
2154 		configure_exception_vector();
2155 
2156 		/* Restore register with CPU number for TLB handlers */
2157 		TLBMISS_HANDLER_RESTORE();
2158 
2159 		break;
2160 	}
2161 
2162 	return NOTIFY_OK;
2163 }
2164 
2165 static struct notifier_block trap_pm_notifier_block = {
2166 	.notifier_call = trap_pm_notifier,
2167 };
2168 
2169 static int __init trap_pm_init(void)
2170 {
2171 	return cpu_pm_register_notifier(&trap_pm_notifier_block);
2172 }
2173 arch_initcall(trap_pm_init);
2174