xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision c3412dcb75ff4d64b44bedc72761d5707d19edf7)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
314cf11afSPaul Mackerras  *
414cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
514cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
614cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
714cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
814cf11afSPaul Mackerras  *
914cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1014cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1114cf11afSPaul Mackerras  */
1214cf11afSPaul Mackerras 
1314cf11afSPaul Mackerras /*
1414cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1514cf11afSPaul Mackerras  */
1614cf11afSPaul Mackerras 
1714cf11afSPaul Mackerras #include <linux/errno.h>
1814cf11afSPaul Mackerras #include <linux/sched.h>
1914cf11afSPaul Mackerras #include <linux/kernel.h>
2014cf11afSPaul Mackerras #include <linux/mm.h>
2114cf11afSPaul Mackerras #include <linux/stddef.h>
2214cf11afSPaul Mackerras #include <linux/unistd.h>
238dad3f92SPaul Mackerras #include <linux/ptrace.h>
2414cf11afSPaul Mackerras #include <linux/slab.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/a.out.h>
2714cf11afSPaul Mackerras #include <linux/interrupt.h>
2814cf11afSPaul Mackerras #include <linux/init.h>
2914cf11afSPaul Mackerras #include <linux/module.h>
308dad3f92SPaul Mackerras #include <linux/prctl.h>
3114cf11afSPaul Mackerras #include <linux/delay.h>
3214cf11afSPaul Mackerras #include <linux/kprobes.h>
33cc532915SMichael Ellerman #include <linux/kexec.h>
345474c120SMichael Hanselmann #include <linux/backlight.h>
3514cf11afSPaul Mackerras 
3686417780SPaul Mackerras #include <asm/kdebug.h>
3714cf11afSPaul Mackerras #include <asm/pgtable.h>
3814cf11afSPaul Mackerras #include <asm/uaccess.h>
3914cf11afSPaul Mackerras #include <asm/system.h>
4014cf11afSPaul Mackerras #include <asm/io.h>
4186417780SPaul Mackerras #include <asm/machdep.h>
4286417780SPaul Mackerras #include <asm/rtas.h>
43f7f6f4feSDavid Gibson #include <asm/pmc.h>
44dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4514cf11afSPaul Mackerras #include <asm/reg.h>
4686417780SPaul Mackerras #endif
4714cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
4814cf11afSPaul Mackerras #include <asm/backlight.h>
4914cf11afSPaul Mackerras #endif
50dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5186417780SPaul Mackerras #include <asm/firmware.h>
52dc1c1ca3SStephen Rothwell #include <asm/processor.h>
53dc1c1ca3SStephen Rothwell #endif
54c0ce7d08SDavid Wilder #include <asm/kexec.h>
55dc1c1ca3SStephen Rothwell 
5686417780SPaul Mackerras #ifdef CONFIG_PPC64	/* XXX */
5786417780SPaul Mackerras #define _IO_BASE	pci_io_base
5886417780SPaul Mackerras #endif
5986417780SPaul Mackerras 
6014cf11afSPaul Mackerras #ifdef CONFIG_DEBUGGER
6114cf11afSPaul Mackerras int (*__debugger)(struct pt_regs *regs);
6214cf11afSPaul Mackerras int (*__debugger_ipi)(struct pt_regs *regs);
6314cf11afSPaul Mackerras int (*__debugger_bpt)(struct pt_regs *regs);
6414cf11afSPaul Mackerras int (*__debugger_sstep)(struct pt_regs *regs);
6514cf11afSPaul Mackerras int (*__debugger_iabr_match)(struct pt_regs *regs);
6614cf11afSPaul Mackerras int (*__debugger_dabr_match)(struct pt_regs *regs);
6714cf11afSPaul Mackerras int (*__debugger_fault_handler)(struct pt_regs *regs);
6814cf11afSPaul Mackerras 
6914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7614cf11afSPaul Mackerras #endif
7714cf11afSPaul Mackerras 
78e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(powerpc_die_chain);
7914cf11afSPaul Mackerras 
8014cf11afSPaul Mackerras int register_die_notifier(struct notifier_block *nb)
8114cf11afSPaul Mackerras {
82e041c683SAlan Stern 	return atomic_notifier_chain_register(&powerpc_die_chain, nb);
8314cf11afSPaul Mackerras }
84e041c683SAlan Stern EXPORT_SYMBOL(register_die_notifier);
85e041c683SAlan Stern 
86e041c683SAlan Stern int unregister_die_notifier(struct notifier_block *nb)
87e041c683SAlan Stern {
88e041c683SAlan Stern 	return atomic_notifier_chain_unregister(&powerpc_die_chain, nb);
89e041c683SAlan Stern }
90e041c683SAlan Stern EXPORT_SYMBOL(unregister_die_notifier);
9114cf11afSPaul Mackerras 
9214cf11afSPaul Mackerras /*
9314cf11afSPaul Mackerras  * Trap & Exception support
9414cf11afSPaul Mackerras  */
9514cf11afSPaul Mackerras 
9614cf11afSPaul Mackerras static DEFINE_SPINLOCK(die_lock);
9714cf11afSPaul Mackerras 
9814cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
9914cf11afSPaul Mackerras {
100c0ce7d08SDavid Wilder 	static int die_counter;
10114cf11afSPaul Mackerras 
10214cf11afSPaul Mackerras 	if (debugger(regs))
10314cf11afSPaul Mackerras 		return 1;
10414cf11afSPaul Mackerras 
10514cf11afSPaul Mackerras 	console_verbose();
10614cf11afSPaul Mackerras 	spin_lock_irq(&die_lock);
10714cf11afSPaul Mackerras 	bust_spinlocks(1);
1088dad3f92SPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
1095474c120SMichael Hanselmann 	mutex_lock(&pmac_backlight_mutex);
1105474c120SMichael Hanselmann 	if (machine_is(powermac) && pmac_backlight) {
1115474c120SMichael Hanselmann 		struct backlight_properties *props;
1125474c120SMichael Hanselmann 
1135474c120SMichael Hanselmann 		down(&pmac_backlight->sem);
1145474c120SMichael Hanselmann 		props = pmac_backlight->props;
1155474c120SMichael Hanselmann 		props->brightness = props->max_brightness;
1165474c120SMichael Hanselmann 		props->power = FB_BLANK_UNBLANK;
1175474c120SMichael Hanselmann 		props->update_status(pmac_backlight);
1185474c120SMichael Hanselmann 		up(&pmac_backlight->sem);
11914cf11afSPaul Mackerras 	}
1205474c120SMichael Hanselmann 	mutex_unlock(&pmac_backlight_mutex);
12114cf11afSPaul Mackerras #endif
12214cf11afSPaul Mackerras 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
12314cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
12414cf11afSPaul Mackerras 	printk("PREEMPT ");
12514cf11afSPaul Mackerras #endif
12614cf11afSPaul Mackerras #ifdef CONFIG_SMP
12714cf11afSPaul Mackerras 	printk("SMP NR_CPUS=%d ", NR_CPUS);
12814cf11afSPaul Mackerras #endif
12914cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
13014cf11afSPaul Mackerras 	printk("DEBUG_PAGEALLOC ");
13114cf11afSPaul Mackerras #endif
13214cf11afSPaul Mackerras #ifdef CONFIG_NUMA
13314cf11afSPaul Mackerras 	printk("NUMA ");
13414cf11afSPaul Mackerras #endif
135e8222502SBenjamin Herrenschmidt 	printk("%s\n", ppc_md.name ? "" : ppc_md.name);
136e8222502SBenjamin Herrenschmidt 
13714cf11afSPaul Mackerras 	print_modules();
13814cf11afSPaul Mackerras 	show_regs(regs);
13914cf11afSPaul Mackerras 	bust_spinlocks(0);
140c0ce7d08SDavid Wilder 	spin_unlock_irq(&die_lock);
141cc532915SMichael Ellerman 
142c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
143c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
144cc532915SMichael Ellerman 		crash_kexec(regs);
145c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
14614cf11afSPaul Mackerras 
14714cf11afSPaul Mackerras 	if (in_interrupt())
14814cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
14914cf11afSPaul Mackerras 
150cea6a4baSHorms 	if (panic_on_oops)
151012c437dSHorms 		panic("Fatal exception");
152cea6a4baSHorms 
15314cf11afSPaul Mackerras 	do_exit(err);
15414cf11afSPaul Mackerras 
15514cf11afSPaul Mackerras 	return 0;
15614cf11afSPaul Mackerras }
15714cf11afSPaul Mackerras 
15814cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
15914cf11afSPaul Mackerras {
16014cf11afSPaul Mackerras 	siginfo_t info;
16114cf11afSPaul Mackerras 
16214cf11afSPaul Mackerras 	if (!user_mode(regs)) {
16314cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
16414cf11afSPaul Mackerras 			return;
16514cf11afSPaul Mackerras 	}
16614cf11afSPaul Mackerras 
16714cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
16814cf11afSPaul Mackerras 	info.si_signo = signr;
16914cf11afSPaul Mackerras 	info.si_code = code;
17014cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
17114cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
17214cf11afSPaul Mackerras 
17314cf11afSPaul Mackerras 	/*
17414cf11afSPaul Mackerras 	 * Init gets no signals that it doesn't have a handler for.
17514cf11afSPaul Mackerras 	 * That's all very well, but if it has caused a synchronous
17614cf11afSPaul Mackerras 	 * exception and we ignore the resulting signal, it will just
17714cf11afSPaul Mackerras 	 * generate the same exception over and over again and we get
17814cf11afSPaul Mackerras 	 * nowhere.  Better to kill it and let the kernel panic.
17914cf11afSPaul Mackerras 	 */
18014cf11afSPaul Mackerras 	if (current->pid == 1) {
18114cf11afSPaul Mackerras 		__sighandler_t handler;
18214cf11afSPaul Mackerras 
18314cf11afSPaul Mackerras 		spin_lock_irq(&current->sighand->siglock);
18414cf11afSPaul Mackerras 		handler = current->sighand->action[signr-1].sa.sa_handler;
18514cf11afSPaul Mackerras 		spin_unlock_irq(&current->sighand->siglock);
18614cf11afSPaul Mackerras 		if (handler == SIG_DFL) {
18714cf11afSPaul Mackerras 			/* init has generated a synchronous exception
18814cf11afSPaul Mackerras 			   and it doesn't have a handler for the signal */
18914cf11afSPaul Mackerras 			printk(KERN_CRIT "init has generated signal %d "
19014cf11afSPaul Mackerras 			       "but has no handler for it\n", signr);
19114cf11afSPaul Mackerras 			do_exit(signr);
19214cf11afSPaul Mackerras 		}
19314cf11afSPaul Mackerras 	}
19414cf11afSPaul Mackerras }
19514cf11afSPaul Mackerras 
19614cf11afSPaul Mackerras #ifdef CONFIG_PPC64
19714cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
19814cf11afSPaul Mackerras {
19914cf11afSPaul Mackerras 	/* See if any machine dependent calls */
200c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
201c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
202c902be71SArnd Bergmann 			return;
203c902be71SArnd Bergmann 	}
20414cf11afSPaul Mackerras 
205c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
206c0ce7d08SDavid Wilder 	cpu_set(smp_processor_id(), cpus_in_sr);
207c0ce7d08SDavid Wilder #endif
208c0ce7d08SDavid Wilder 
2098dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
21014cf11afSPaul Mackerras 
211eac8392fSDavid Wilder 	/*
212eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
213eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
214eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
215eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
216eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
217eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
218eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
219eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
220eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
221eac8392fSDavid Wilder 	 */
222eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
223eac8392fSDavid Wilder 
22414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
22514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
22614cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
22714cf11afSPaul Mackerras 
22814cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
22914cf11afSPaul Mackerras }
23014cf11afSPaul Mackerras #endif
23114cf11afSPaul Mackerras 
23214cf11afSPaul Mackerras /*
23314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
23414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
23514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
23614cf11afSPaul Mackerras  * table.
23714cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
23814cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
23914cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
24014cf11afSPaul Mackerras  *  -- paulus.
24114cf11afSPaul Mackerras  */
24214cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
24314cf11afSPaul Mackerras {
2441a6a4ffeSKumar Gala #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
24514cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
24614cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
24714cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
24814cf11afSPaul Mackerras 
24914cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
25014cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
25114cf11afSPaul Mackerras 		/*
25214cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
25314cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
25414cf11afSPaul Mackerras 		 * As the address is in the exception table
25514cf11afSPaul Mackerras 		 * we should be able to read the instr there.
25614cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
25714cf11afSPaul Mackerras 		 * load or store.
25814cf11afSPaul Mackerras 		 */
25914cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
26014cf11afSPaul Mackerras 			nip -= 2;
26114cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
26214cf11afSPaul Mackerras 			--nip;
26314cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
26414cf11afSPaul Mackerras 			/* sync or twi */
26514cf11afSPaul Mackerras 			unsigned int rb;
26614cf11afSPaul Mackerras 
26714cf11afSPaul Mackerras 			--nip;
26814cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
26914cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
27014cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
27114cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
27214cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
27314cf11afSPaul Mackerras 			regs->nip = entry->fixup;
27414cf11afSPaul Mackerras 			return 1;
27514cf11afSPaul Mackerras 		}
27614cf11afSPaul Mackerras 	}
2771a6a4ffeSKumar Gala #endif /* CONFIG_PPC_PMAC && CONFIG_PPC32 */
27814cf11afSPaul Mackerras 	return 0;
27914cf11afSPaul Mackerras }
28014cf11afSPaul Mackerras 
28114cf11afSPaul Mackerras #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
28214cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
28314cf11afSPaul Mackerras    is in the ESR. */
28414cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
28514cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
28614cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
28714cf11afSPaul Mackerras #else
28814cf11afSPaul Mackerras #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
28914cf11afSPaul Mackerras #endif
29014cf11afSPaul Mackerras #define REASON_FP		ESR_FP
29114cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
29214cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
29314cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
29414cf11afSPaul Mackerras 
29514cf11afSPaul Mackerras /* single-step stuff */
29614cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
29714cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
29814cf11afSPaul Mackerras 
29914cf11afSPaul Mackerras #else
30014cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
30114cf11afSPaul Mackerras    exception is in the MSR. */
30214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
30314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
30414cf11afSPaul Mackerras #define REASON_FP		0x100000
30514cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
30614cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
30714cf11afSPaul Mackerras #define REASON_TRAP		0x20000
30814cf11afSPaul Mackerras 
30914cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
31014cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
31114cf11afSPaul Mackerras #endif
31214cf11afSPaul Mackerras 
31314cf11afSPaul Mackerras /*
31414cf11afSPaul Mackerras  * This is "fall-back" implementation for configurations
31514cf11afSPaul Mackerras  * which don't provide platform-specific machine check info
31614cf11afSPaul Mackerras  */
31714cf11afSPaul Mackerras void __attribute__ ((weak))
31814cf11afSPaul Mackerras platform_machine_check(struct pt_regs *regs)
31914cf11afSPaul Mackerras {
32014cf11afSPaul Mackerras }
32114cf11afSPaul Mackerras 
322dc1c1ca3SStephen Rothwell void machine_check_exception(struct pt_regs *regs)
32314cf11afSPaul Mackerras {
32414cf11afSPaul Mackerras 	int recover = 0;
3251a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
32614cf11afSPaul Mackerras 
32714cf11afSPaul Mackerras 	/* See if any machine dependent calls */
32814cf11afSPaul Mackerras 	if (ppc_md.machine_check_exception)
32914cf11afSPaul Mackerras 		recover = ppc_md.machine_check_exception(regs);
33014cf11afSPaul Mackerras 
33114cf11afSPaul Mackerras 	if (recover)
33214cf11afSPaul Mackerras 		return;
33314cf11afSPaul Mackerras 
33414cf11afSPaul Mackerras 	if (user_mode(regs)) {
33514cf11afSPaul Mackerras 		regs->msr |= MSR_RI;
33614cf11afSPaul Mackerras 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
33714cf11afSPaul Mackerras 		return;
33814cf11afSPaul Mackerras 	}
33914cf11afSPaul Mackerras 
34014cf11afSPaul Mackerras #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
34114cf11afSPaul Mackerras 	/* the qspan pci read routines can cause machine checks -- Cort */
34214cf11afSPaul Mackerras 	bad_page_fault(regs, regs->dar, SIGBUS);
34314cf11afSPaul Mackerras 	return;
34414cf11afSPaul Mackerras #endif
34514cf11afSPaul Mackerras 
34614cf11afSPaul Mackerras 	if (debugger_fault_handler(regs)) {
34714cf11afSPaul Mackerras 		regs->msr |= MSR_RI;
34814cf11afSPaul Mackerras 		return;
34914cf11afSPaul Mackerras 	}
35014cf11afSPaul Mackerras 
35114cf11afSPaul Mackerras 	if (check_io_access(regs))
35214cf11afSPaul Mackerras 		return;
35314cf11afSPaul Mackerras 
35414cf11afSPaul Mackerras #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
35514cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
35614cf11afSPaul Mackerras 		printk("Instruction");
35714cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35814cf11afSPaul Mackerras 	} else
35914cf11afSPaul Mackerras 		printk("Data");
36014cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
36114cf11afSPaul Mackerras #elif defined(CONFIG_440A)
36214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
36314cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
36414cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
36514cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
36614cf11afSPaul Mackerras 	}
36714cf11afSPaul Mackerras 	else {
36814cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
36914cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
37014cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
37114cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
37214cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
37314cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
37414cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
37514cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
37614cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
37714cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
37814cf11afSPaul Mackerras 			flush_instruction_cache();
37914cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
38014cf11afSPaul Mackerras 		}
38114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
38214cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
38314cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
38414cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
38514cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
38614cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
38714cf11afSPaul Mackerras 
38814cf11afSPaul Mackerras 		/* Clear MCSR */
38914cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
39014cf11afSPaul Mackerras 	}
39114cf11afSPaul Mackerras #elif defined (CONFIG_E500)
39214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
39314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
39414cf11afSPaul Mackerras 
39514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
39614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
39714cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
39814cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
39914cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
40014cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
40114cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
40214cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
40314cf11afSPaul Mackerras 	if (reason & MCSR_GL_CI)
40414cf11afSPaul Mackerras 		printk("Guarded Load or Cache-Inhibited stwcx.\n");
40514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
40614cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
40714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
40814cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
40914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
41014cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
41114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
41214cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
41314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
41414cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
41514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
41614cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
41714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
41814cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
41914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
42014cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
42114cf11afSPaul Mackerras #elif defined (CONFIG_E200)
42214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
42414cf11afSPaul Mackerras 
42514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
42614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
42714cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
42814cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
42914cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
43014cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
43114cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
43214cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
43314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
43414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
43514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
43614cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
43714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
43814cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
43914cf11afSPaul Mackerras #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
44014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
44114cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
44214cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
44314cf11afSPaul Mackerras 	case 0x80000:
44414cf11afSPaul Mackerras 		printk("Machine check signal\n");
44514cf11afSPaul Mackerras 		break;
44614cf11afSPaul Mackerras 	case 0:		/* for 601 */
44714cf11afSPaul Mackerras 	case 0x40000:
44814cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
44914cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
45014cf11afSPaul Mackerras 		break;
45114cf11afSPaul Mackerras 	case 0x20000:
45214cf11afSPaul Mackerras 		printk("Data parity error signal\n");
45314cf11afSPaul Mackerras 		break;
45414cf11afSPaul Mackerras 	case 0x10000:
45514cf11afSPaul Mackerras 		printk("Address parity error signal\n");
45614cf11afSPaul Mackerras 		break;
45714cf11afSPaul Mackerras 	case 0x20000000:
45814cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
45914cf11afSPaul Mackerras 		break;
46014cf11afSPaul Mackerras 	case 0x40000000:
46114cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
46214cf11afSPaul Mackerras 		break;
46314cf11afSPaul Mackerras 	case 0x00100000:
46414cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
46514cf11afSPaul Mackerras 		break;
46614cf11afSPaul Mackerras 	default:
46714cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
46814cf11afSPaul Mackerras 	}
46914cf11afSPaul Mackerras #endif /* CONFIG_4xx */
47014cf11afSPaul Mackerras 
47114cf11afSPaul Mackerras 	/*
47214cf11afSPaul Mackerras 	 * Optional platform-provided routine to print out
47314cf11afSPaul Mackerras 	 * additional info, e.g. bus error registers.
47414cf11afSPaul Mackerras 	 */
47514cf11afSPaul Mackerras 	platform_machine_check(regs);
47614cf11afSPaul Mackerras 
47714cf11afSPaul Mackerras 	if (debugger_fault_handler(regs))
47814cf11afSPaul Mackerras 		return;
4798dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
48014cf11afSPaul Mackerras 
48114cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
48214cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
48314cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
48414cf11afSPaul Mackerras }
48514cf11afSPaul Mackerras 
48614cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
48714cf11afSPaul Mackerras {
48814cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
48914cf11afSPaul Mackerras }
49014cf11afSPaul Mackerras 
491dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
49214cf11afSPaul Mackerras {
49314cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
49414cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
49514cf11afSPaul Mackerras 
49614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
49714cf11afSPaul Mackerras }
49814cf11afSPaul Mackerras 
499dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
50014cf11afSPaul Mackerras {
50114cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
50214cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
50314cf11afSPaul Mackerras 		return;
50414cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
50514cf11afSPaul Mackerras 		return;
50614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
50714cf11afSPaul Mackerras }
50814cf11afSPaul Mackerras 
50914cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
51014cf11afSPaul Mackerras {
51114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
51214cf11afSPaul Mackerras }
51314cf11afSPaul Mackerras 
5148dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
51514cf11afSPaul Mackerras {
51614cf11afSPaul Mackerras 	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
51714cf11afSPaul Mackerras 
51814cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
51914cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
52014cf11afSPaul Mackerras 		return;
52114cf11afSPaul Mackerras 	if (debugger_sstep(regs))
52214cf11afSPaul Mackerras 		return;
52314cf11afSPaul Mackerras 
52414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
52514cf11afSPaul Mackerras }
52614cf11afSPaul Mackerras 
52714cf11afSPaul Mackerras /*
52814cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
52914cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
53014cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
53114cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
53214cf11afSPaul Mackerras  */
5338dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
53414cf11afSPaul Mackerras {
53514cf11afSPaul Mackerras 	if (single_stepping(regs)) {
53614cf11afSPaul Mackerras 		clear_single_step(regs);
53714cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
53814cf11afSPaul Mackerras 	}
53914cf11afSPaul Mackerras }
54014cf11afSPaul Mackerras 
541dc1c1ca3SStephen Rothwell static void parse_fpe(struct pt_regs *regs)
542dc1c1ca3SStephen Rothwell {
543dc1c1ca3SStephen Rothwell 	int code = 0;
544dc1c1ca3SStephen Rothwell 	unsigned long fpscr;
545dc1c1ca3SStephen Rothwell 
546dc1c1ca3SStephen Rothwell 	flush_fp_to_thread(current);
547dc1c1ca3SStephen Rothwell 
54825c8a78bSDavid Gibson 	fpscr = current->thread.fpscr.val;
549dc1c1ca3SStephen Rothwell 
550dc1c1ca3SStephen Rothwell 	/* Invalid operation */
551dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
552dc1c1ca3SStephen Rothwell 		code = FPE_FLTINV;
553dc1c1ca3SStephen Rothwell 
554dc1c1ca3SStephen Rothwell 	/* Overflow */
555dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
556dc1c1ca3SStephen Rothwell 		code = FPE_FLTOVF;
557dc1c1ca3SStephen Rothwell 
558dc1c1ca3SStephen Rothwell 	/* Underflow */
559dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
560dc1c1ca3SStephen Rothwell 		code = FPE_FLTUND;
561dc1c1ca3SStephen Rothwell 
562dc1c1ca3SStephen Rothwell 	/* Divide by zero */
563dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
564dc1c1ca3SStephen Rothwell 		code = FPE_FLTDIV;
565dc1c1ca3SStephen Rothwell 
566dc1c1ca3SStephen Rothwell 	/* Inexact result */
567dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
568dc1c1ca3SStephen Rothwell 		code = FPE_FLTRES;
569dc1c1ca3SStephen Rothwell 
570dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
571dc1c1ca3SStephen Rothwell }
572dc1c1ca3SStephen Rothwell 
573dc1c1ca3SStephen Rothwell /*
574dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
57514cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
57614cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
57714cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
57814cf11afSPaul Mackerras  *
57914cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
58014cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
58114cf11afSPaul Mackerras  * bits is faster and easier.
58286417780SPaul Mackerras  *
58314cf11afSPaul Mackerras  */
58414cf11afSPaul Mackerras #define INST_MFSPR_PVR		0x7c1f42a6
58514cf11afSPaul Mackerras #define INST_MFSPR_PVR_MASK	0xfc1fffff
58614cf11afSPaul Mackerras 
58714cf11afSPaul Mackerras #define INST_DCBA		0x7c0005ec
58887589f08SPaul Mackerras #define INST_DCBA_MASK		0xfc0007fe
58914cf11afSPaul Mackerras 
59014cf11afSPaul Mackerras #define INST_MCRXR		0x7c000400
59187589f08SPaul Mackerras #define INST_MCRXR_MASK		0xfc0007fe
59214cf11afSPaul Mackerras 
59314cf11afSPaul Mackerras #define INST_STRING		0x7c00042a
59487589f08SPaul Mackerras #define INST_STRING_MASK	0xfc0007fe
59587589f08SPaul Mackerras #define INST_STRING_GEN_MASK	0xfc00067e
59614cf11afSPaul Mackerras #define INST_LSWI		0x7c0004aa
59714cf11afSPaul Mackerras #define INST_LSWX		0x7c00042a
59814cf11afSPaul Mackerras #define INST_STSWI		0x7c0005aa
59914cf11afSPaul Mackerras #define INST_STSWX		0x7c00052a
60014cf11afSPaul Mackerras 
601*c3412dcbSWill Schmidt #define INST_POPCNTB		0x7c0000f4
602*c3412dcbSWill Schmidt #define INST_POPCNTB_MASK	0xfc0007fe
603*c3412dcbSWill Schmidt 
60414cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
60514cf11afSPaul Mackerras {
60614cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
60714cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
60814cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
60914cf11afSPaul Mackerras 	u32 num_bytes;
61014cf11afSPaul Mackerras 	unsigned long EA;
61114cf11afSPaul Mackerras 	int pos = 0;
61214cf11afSPaul Mackerras 
61314cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
61414cf11afSPaul Mackerras 	if ((instword & INST_STRING_MASK) == INST_LSWX)
61514cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
61614cf11afSPaul Mackerras 			return -EINVAL;
61714cf11afSPaul Mackerras 
61814cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
61914cf11afSPaul Mackerras 
62014cf11afSPaul Mackerras 	switch (instword & INST_STRING_MASK) {
62114cf11afSPaul Mackerras 		case INST_LSWX:
62214cf11afSPaul Mackerras 		case INST_STSWX:
62314cf11afSPaul Mackerras 			EA += NB_RB;
62414cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
62514cf11afSPaul Mackerras 			break;
62614cf11afSPaul Mackerras 		case INST_LSWI:
62714cf11afSPaul Mackerras 		case INST_STSWI:
62814cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
62914cf11afSPaul Mackerras 			break;
63014cf11afSPaul Mackerras 		default:
63114cf11afSPaul Mackerras 			return -EINVAL;
63214cf11afSPaul Mackerras 	}
63314cf11afSPaul Mackerras 
63414cf11afSPaul Mackerras 	while (num_bytes != 0)
63514cf11afSPaul Mackerras 	{
63614cf11afSPaul Mackerras 		u8 val;
63714cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
63814cf11afSPaul Mackerras 
63914cf11afSPaul Mackerras 		switch ((instword & INST_STRING_MASK)) {
64014cf11afSPaul Mackerras 			case INST_LSWX:
64114cf11afSPaul Mackerras 			case INST_LSWI:
64214cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
64314cf11afSPaul Mackerras 					return -EFAULT;
64414cf11afSPaul Mackerras 				/* first time updating this reg,
64514cf11afSPaul Mackerras 				 * zero it out */
64614cf11afSPaul Mackerras 				if (pos == 0)
64714cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
64814cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
64914cf11afSPaul Mackerras 				break;
65014cf11afSPaul Mackerras 			case INST_STSWI:
65114cf11afSPaul Mackerras 			case INST_STSWX:
65214cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
65314cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
65414cf11afSPaul Mackerras 					return -EFAULT;
65514cf11afSPaul Mackerras 				break;
65614cf11afSPaul Mackerras 		}
65714cf11afSPaul Mackerras 		/* move EA to next address */
65814cf11afSPaul Mackerras 		EA += 1;
65914cf11afSPaul Mackerras 		num_bytes--;
66014cf11afSPaul Mackerras 
66114cf11afSPaul Mackerras 		/* manage our position within the register */
66214cf11afSPaul Mackerras 		if (++pos == 4) {
66314cf11afSPaul Mackerras 			pos = 0;
66414cf11afSPaul Mackerras 			if (++rT == 32)
66514cf11afSPaul Mackerras 				rT = 0;
66614cf11afSPaul Mackerras 		}
66714cf11afSPaul Mackerras 	}
66814cf11afSPaul Mackerras 
66914cf11afSPaul Mackerras 	return 0;
67014cf11afSPaul Mackerras }
67114cf11afSPaul Mackerras 
672*c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
673*c3412dcbSWill Schmidt {
674*c3412dcbSWill Schmidt 	u32 ra,rs;
675*c3412dcbSWill Schmidt 	unsigned long tmp;
676*c3412dcbSWill Schmidt 
677*c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
678*c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
679*c3412dcbSWill Schmidt 
680*c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
681*c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
682*c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
683*c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
684*c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
685*c3412dcbSWill Schmidt 
686*c3412dcbSWill Schmidt 	return 0;
687*c3412dcbSWill Schmidt }
688*c3412dcbSWill Schmidt 
68914cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
69014cf11afSPaul Mackerras {
69114cf11afSPaul Mackerras 	u32 instword;
69214cf11afSPaul Mackerras 	u32 rd;
69314cf11afSPaul Mackerras 
694fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
69514cf11afSPaul Mackerras 		return -EINVAL;
69614cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
69714cf11afSPaul Mackerras 
69814cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
69914cf11afSPaul Mackerras 		return -EFAULT;
70014cf11afSPaul Mackerras 
70114cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
70214cf11afSPaul Mackerras 	if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
70314cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
70414cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
70514cf11afSPaul Mackerras 		return 0;
70614cf11afSPaul Mackerras 	}
70714cf11afSPaul Mackerras 
70814cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
7098dad3f92SPaul Mackerras 	if ((instword & INST_DCBA_MASK) == INST_DCBA)
71014cf11afSPaul Mackerras 		return 0;
71114cf11afSPaul Mackerras 
71214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
71314cf11afSPaul Mackerras 	if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
71486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
71514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
71614cf11afSPaul Mackerras 
71714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
71814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
71914cf11afSPaul Mackerras 		return 0;
72014cf11afSPaul Mackerras 	}
72114cf11afSPaul Mackerras 
72214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
72314cf11afSPaul Mackerras 	if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
72414cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
72514cf11afSPaul Mackerras 
726*c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
727*c3412dcbSWill Schmidt 	if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
728*c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
729*c3412dcbSWill Schmidt 	}
730*c3412dcbSWill Schmidt 
73114cf11afSPaul Mackerras 	return -EINVAL;
73214cf11afSPaul Mackerras }
73314cf11afSPaul Mackerras 
73414cf11afSPaul Mackerras /*
73514cf11afSPaul Mackerras  * Look through the list of trap instructions that are used for BUG(),
73614cf11afSPaul Mackerras  * BUG_ON() and WARN_ON() and see if we hit one.  At this point we know
73714cf11afSPaul Mackerras  * that the exception was caused by a trap instruction of some kind.
73814cf11afSPaul Mackerras  * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
73914cf11afSPaul Mackerras  * otherwise.
74014cf11afSPaul Mackerras  */
74114cf11afSPaul Mackerras extern struct bug_entry __start___bug_table[], __stop___bug_table[];
74214cf11afSPaul Mackerras 
74314cf11afSPaul Mackerras #ifndef CONFIG_MODULES
74414cf11afSPaul Mackerras #define module_find_bug(x)	NULL
74514cf11afSPaul Mackerras #endif
74614cf11afSPaul Mackerras 
74714cf11afSPaul Mackerras struct bug_entry *find_bug(unsigned long bugaddr)
74814cf11afSPaul Mackerras {
74914cf11afSPaul Mackerras 	struct bug_entry *bug;
75014cf11afSPaul Mackerras 
75114cf11afSPaul Mackerras 	for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
75214cf11afSPaul Mackerras 		if (bugaddr == bug->bug_addr)
75314cf11afSPaul Mackerras 			return bug;
75414cf11afSPaul Mackerras 	return module_find_bug(bugaddr);
75514cf11afSPaul Mackerras }
75614cf11afSPaul Mackerras 
757dc1c1ca3SStephen Rothwell static int check_bug_trap(struct pt_regs *regs)
75814cf11afSPaul Mackerras {
75914cf11afSPaul Mackerras 	struct bug_entry *bug;
76014cf11afSPaul Mackerras 	unsigned long addr;
76114cf11afSPaul Mackerras 
76214cf11afSPaul Mackerras 	if (regs->msr & MSR_PR)
76314cf11afSPaul Mackerras 		return 0;	/* not in kernel */
76414cf11afSPaul Mackerras 	addr = regs->nip;	/* address of trap instruction */
76514cf11afSPaul Mackerras 	if (addr < PAGE_OFFSET)
76614cf11afSPaul Mackerras 		return 0;
76714cf11afSPaul Mackerras 	bug = find_bug(regs->nip);
76814cf11afSPaul Mackerras 	if (bug == NULL)
76914cf11afSPaul Mackerras 		return 0;
77014cf11afSPaul Mackerras 	if (bug->line & BUG_WARNING_TRAP) {
77114cf11afSPaul Mackerras 		/* this is a WARN_ON rather than BUG/BUG_ON */
772104dd65fSPaul Mackerras 		printk(KERN_ERR "Badness in %s at %s:%ld\n",
77314cf11afSPaul Mackerras 		       bug->function, bug->file,
77414cf11afSPaul Mackerras 		       bug->line & ~BUG_WARNING_TRAP);
77514cf11afSPaul Mackerras 		dump_stack();
77614cf11afSPaul Mackerras 		return 1;
77714cf11afSPaul Mackerras 	}
778104dd65fSPaul Mackerras 	printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
77914cf11afSPaul Mackerras 	       bug->function, bug->file, bug->line);
78014cf11afSPaul Mackerras 
78114cf11afSPaul Mackerras 	return 0;
78214cf11afSPaul Mackerras }
78314cf11afSPaul Mackerras 
7848dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
78514cf11afSPaul Mackerras {
78614cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
78714cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
78814cf11afSPaul Mackerras 
7898dad3f92SPaul Mackerras #ifdef CONFIG_MATH_EMULATION
79014cf11afSPaul Mackerras 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
79114cf11afSPaul Mackerras 	 * but there seems to be a hardware bug on the 405GP (RevD)
79214cf11afSPaul Mackerras 	 * that means ESR is sometimes set incorrectly - either to
79314cf11afSPaul Mackerras 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
79414cf11afSPaul Mackerras 	 * hardware people - not sure if it can happen on any illegal
79514cf11afSPaul Mackerras 	 * instruction or only on FP instructions, whether there is a
79614cf11afSPaul Mackerras 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
79714cf11afSPaul Mackerras 	if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
79814cf11afSPaul Mackerras 		emulate_single_step(regs);
79914cf11afSPaul Mackerras 		return;
80014cf11afSPaul Mackerras 	}
8018dad3f92SPaul Mackerras #endif /* CONFIG_MATH_EMULATION */
80214cf11afSPaul Mackerras 
80314cf11afSPaul Mackerras 	if (reason & REASON_FP) {
80414cf11afSPaul Mackerras 		/* IEEE FP exception */
805dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
8068dad3f92SPaul Mackerras 		return;
8078dad3f92SPaul Mackerras 	}
8088dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
80914cf11afSPaul Mackerras 		/* trap exception */
810dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
811dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
812dc1c1ca3SStephen Rothwell 			return;
81314cf11afSPaul Mackerras 		if (debugger_bpt(regs))
81414cf11afSPaul Mackerras 			return;
81514cf11afSPaul Mackerras 		if (check_bug_trap(regs)) {
81614cf11afSPaul Mackerras 			regs->nip += 4;
81714cf11afSPaul Mackerras 			return;
81814cf11afSPaul Mackerras 		}
8198dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
8208dad3f92SPaul Mackerras 		return;
8218dad3f92SPaul Mackerras 	}
8228dad3f92SPaul Mackerras 
823cd8a5673SPaul Mackerras 	local_irq_enable();
824cd8a5673SPaul Mackerras 
8258dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
8268dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
82714cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
82814cf11afSPaul Mackerras 		case 0:
82914cf11afSPaul Mackerras 			regs->nip += 4;
83014cf11afSPaul Mackerras 			emulate_single_step(regs);
8318dad3f92SPaul Mackerras 			return;
83214cf11afSPaul Mackerras 		case -EFAULT:
83314cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8348dad3f92SPaul Mackerras 			return;
8358dad3f92SPaul Mackerras 		}
8368dad3f92SPaul Mackerras 	}
8378dad3f92SPaul Mackerras 
83814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
83914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
84014cf11afSPaul Mackerras 	else
84114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
84214cf11afSPaul Mackerras }
84314cf11afSPaul Mackerras 
844dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
84514cf11afSPaul Mackerras {
846e9370ae1SPaul Mackerras 	int fixed = 0;
84714cf11afSPaul Mackerras 
848e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
849e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
85014cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
85114cf11afSPaul Mackerras 
85214cf11afSPaul Mackerras 	if (fixed == 1) {
85314cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
85414cf11afSPaul Mackerras 		emulate_single_step(regs);
85514cf11afSPaul Mackerras 		return;
85614cf11afSPaul Mackerras 	}
85714cf11afSPaul Mackerras 
85814cf11afSPaul Mackerras 	/* Operand address was bad */
85914cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
86014cf11afSPaul Mackerras 		if (user_mode(regs))
8618dad3f92SPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
86214cf11afSPaul Mackerras 		else
86314cf11afSPaul Mackerras 			/* Search exception table */
86414cf11afSPaul Mackerras 			bad_page_fault(regs, regs->dar, SIGSEGV);
86514cf11afSPaul Mackerras 		return;
86614cf11afSPaul Mackerras 	}
8678dad3f92SPaul Mackerras 	_exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
86814cf11afSPaul Mackerras }
86914cf11afSPaul Mackerras 
87014cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
87114cf11afSPaul Mackerras {
87214cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
87314cf11afSPaul Mackerras 	       current, regs->gpr[1]);
87414cf11afSPaul Mackerras 	debugger(regs);
87514cf11afSPaul Mackerras 	show_regs(regs);
87614cf11afSPaul Mackerras 	panic("kernel stack overflow");
87714cf11afSPaul Mackerras }
87814cf11afSPaul Mackerras 
87914cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
88014cf11afSPaul Mackerras {
88114cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
88214cf11afSPaul Mackerras 	       regs->nip, regs->msr);
88314cf11afSPaul Mackerras 	debugger(regs);
88414cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
88514cf11afSPaul Mackerras }
88614cf11afSPaul Mackerras 
88714cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
88814cf11afSPaul Mackerras {
88914cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
89014cf11afSPaul Mackerras 	       current, current->pid, regs->nip, regs->link, regs->gpr[0],
89114cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
89214cf11afSPaul Mackerras }
89314cf11afSPaul Mackerras 
894dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
895dc1c1ca3SStephen Rothwell {
896dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
897dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
898dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
899dc1c1ca3SStephen Rothwell }
900dc1c1ca3SStephen Rothwell 
901dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
902dc1c1ca3SStephen Rothwell {
9038dad3f92SPaul Mackerras #if !defined(CONFIG_ALTIVEC)
904dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
905dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
906dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
907dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
908dc1c1ca3SStephen Rothwell 		return;
909dc1c1ca3SStephen Rothwell 	}
910dc1c1ca3SStephen Rothwell #endif
911dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
912dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
913dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
914dc1c1ca3SStephen Rothwell }
915dc1c1ca3SStephen Rothwell 
916dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
917dc1c1ca3SStephen Rothwell {
918dc1c1ca3SStephen Rothwell 	perf_irq(regs);
919dc1c1ca3SStephen Rothwell }
920dc1c1ca3SStephen Rothwell 
9218dad3f92SPaul Mackerras #ifdef CONFIG_8xx
92214cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
92314cf11afSPaul Mackerras {
92414cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
92514cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
92614cf11afSPaul Mackerras 	int errcode;
92714cf11afSPaul Mackerras 
92814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
92914cf11afSPaul Mackerras 
93014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
93114cf11afSPaul Mackerras 		debugger(regs);
93214cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
93314cf11afSPaul Mackerras 	}
93414cf11afSPaul Mackerras 
93514cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
93614cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
93714cf11afSPaul Mackerras #else
93814cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
93914cf11afSPaul Mackerras #endif
94014cf11afSPaul Mackerras 	if (errcode) {
94114cf11afSPaul Mackerras 		if (errcode > 0)
94214cf11afSPaul Mackerras 			_exception(SIGFPE, regs, 0, 0);
94314cf11afSPaul Mackerras 		else if (errcode == -EFAULT)
94414cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, 0, 0);
94514cf11afSPaul Mackerras 		else
94614cf11afSPaul Mackerras 			_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
94714cf11afSPaul Mackerras 	} else
94814cf11afSPaul Mackerras 		emulate_single_step(regs);
94914cf11afSPaul Mackerras }
9508dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
95114cf11afSPaul Mackerras 
95214cf11afSPaul Mackerras #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
95314cf11afSPaul Mackerras 
95414cf11afSPaul Mackerras void DebugException(struct pt_regs *regs, unsigned long debug_status)
95514cf11afSPaul Mackerras {
95614cf11afSPaul Mackerras 	if (debug_status & DBSR_IC) {	/* instruction completion */
95714cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
95814cf11afSPaul Mackerras 		if (user_mode(regs)) {
95914cf11afSPaul Mackerras 			current->thread.dbcr0 &= ~DBCR0_IC;
96014cf11afSPaul Mackerras 		} else {
96114cf11afSPaul Mackerras 			/* Disable instruction completion */
96214cf11afSPaul Mackerras 			mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
96314cf11afSPaul Mackerras 			/* Clear the instruction completion event */
96414cf11afSPaul Mackerras 			mtspr(SPRN_DBSR, DBSR_IC);
96514cf11afSPaul Mackerras 			if (debugger_sstep(regs))
96614cf11afSPaul Mackerras 				return;
96714cf11afSPaul Mackerras 		}
96814cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
96914cf11afSPaul Mackerras 	}
97014cf11afSPaul Mackerras }
97114cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
97214cf11afSPaul Mackerras 
97314cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
97414cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
97514cf11afSPaul Mackerras {
97614cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
97714cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
97814cf11afSPaul Mackerras }
97914cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
98014cf11afSPaul Mackerras 
98114cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
982dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
98314cf11afSPaul Mackerras {
98414cf11afSPaul Mackerras 	int err;
98514cf11afSPaul Mackerras 
98614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
98714cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
98814cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
9898dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
99014cf11afSPaul Mackerras 	}
99114cf11afSPaul Mackerras 
992dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
993dc1c1ca3SStephen Rothwell 
99414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
99514cf11afSPaul Mackerras 	if (err == 0) {
99614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
99714cf11afSPaul Mackerras 		emulate_single_step(regs);
99814cf11afSPaul Mackerras 		return;
99914cf11afSPaul Mackerras 	}
100014cf11afSPaul Mackerras 
100114cf11afSPaul Mackerras 	if (err == -EFAULT) {
100214cf11afSPaul Mackerras 		/* got an error reading the instruction */
100314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
100414cf11afSPaul Mackerras 	} else {
100514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
100614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
100714cf11afSPaul Mackerras 		if (printk_ratelimit())
100814cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
100914cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
101014cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
101114cf11afSPaul Mackerras 	}
101214cf11afSPaul Mackerras }
101314cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
101414cf11afSPaul Mackerras 
101514cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
101614cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
101714cf11afSPaul Mackerras 			   unsigned long error_code)
101814cf11afSPaul Mackerras {
101914cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
102014cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
102114cf11afSPaul Mackerras 	 * something smarter
102214cf11afSPaul Mackerras 	 */
102314cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
102414cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
102514cf11afSPaul Mackerras 	return;
102614cf11afSPaul Mackerras }
102714cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
102814cf11afSPaul Mackerras 
102914cf11afSPaul Mackerras #ifdef CONFIG_SPE
103014cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
103114cf11afSPaul Mackerras {
103214cf11afSPaul Mackerras 	unsigned long spefscr;
103314cf11afSPaul Mackerras 	int fpexc_mode;
103414cf11afSPaul Mackerras 	int code = 0;
103514cf11afSPaul Mackerras 
103614cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
103714cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
103814cf11afSPaul Mackerras 
103914cf11afSPaul Mackerras 	/* Hardware does not neccessarily set sticky
104014cf11afSPaul Mackerras 	 * underflow/overflow/invalid flags */
104114cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
104214cf11afSPaul Mackerras 		code = FPE_FLTOVF;
104314cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FOVFS;
104414cf11afSPaul Mackerras 	}
104514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
104614cf11afSPaul Mackerras 		code = FPE_FLTUND;
104714cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FUNFS;
104814cf11afSPaul Mackerras 	}
104914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
105014cf11afSPaul Mackerras 		code = FPE_FLTDIV;
105114cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
105214cf11afSPaul Mackerras 		code = FPE_FLTINV;
105314cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FINVS;
105414cf11afSPaul Mackerras 	}
105514cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
105614cf11afSPaul Mackerras 		code = FPE_FLTRES;
105714cf11afSPaul Mackerras 
105814cf11afSPaul Mackerras 	current->thread.spefscr = spefscr;
105914cf11afSPaul Mackerras 
106014cf11afSPaul Mackerras 	_exception(SIGFPE, regs, code, regs->nip);
106114cf11afSPaul Mackerras 	return;
106214cf11afSPaul Mackerras }
106314cf11afSPaul Mackerras #endif
106414cf11afSPaul Mackerras 
1065dc1c1ca3SStephen Rothwell /*
1066dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1067dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1068dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1069dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1070dc1c1ca3SStephen Rothwell  */
1071dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1072dc1c1ca3SStephen Rothwell {
1073dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1074dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1075dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1076dc1c1ca3SStephen Rothwell }
1077dc1c1ca3SStephen Rothwell 
107814cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
107914cf11afSPaul Mackerras /*
108014cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
108114cf11afSPaul Mackerras  * spins until a reboot occurs
108214cf11afSPaul Mackerras  */
108314cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
108414cf11afSPaul Mackerras {
108514cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
108614cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
108714cf11afSPaul Mackerras 	return;
108814cf11afSPaul Mackerras }
108914cf11afSPaul Mackerras 
109014cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
109114cf11afSPaul Mackerras {
109214cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
109314cf11afSPaul Mackerras 	WatchdogHandler(regs);
109414cf11afSPaul Mackerras }
109514cf11afSPaul Mackerras #endif
1096dc1c1ca3SStephen Rothwell 
1097dc1c1ca3SStephen Rothwell /*
1098dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1099dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1100dc1c1ca3SStephen Rothwell  */
1101dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1102dc1c1ca3SStephen Rothwell {
1103dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1104dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1105dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1106dc1c1ca3SStephen Rothwell }
110714cf11afSPaul Mackerras 
110814cf11afSPaul Mackerras void __init trap_init(void)
110914cf11afSPaul Mackerras {
111014cf11afSPaul Mackerras }
1111