xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision fc5e709731429bc2db27897630e7c0089f297680)
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/user.h>
2514cf11afSPaul Mackerras #include <linux/interrupt.h>
2614cf11afSPaul Mackerras #include <linux/init.h>
2714cf11afSPaul Mackerras #include <linux/module.h>
288dad3f92SPaul Mackerras #include <linux/prctl.h>
2914cf11afSPaul Mackerras #include <linux/delay.h>
3014cf11afSPaul Mackerras #include <linux/kprobes.h>
31cc532915SMichael Ellerman #include <linux/kexec.h>
325474c120SMichael Hanselmann #include <linux/backlight.h>
3373c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
341eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3580947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3614cf11afSPaul Mackerras 
3780947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
3814cf11afSPaul Mackerras #include <asm/pgtable.h>
3914cf11afSPaul Mackerras #include <asm/uaccess.h>
4014cf11afSPaul Mackerras #include <asm/system.h>
4114cf11afSPaul Mackerras #include <asm/io.h>
4286417780SPaul Mackerras #include <asm/machdep.h>
4386417780SPaul Mackerras #include <asm/rtas.h>
44f7f6f4feSDavid Gibson #include <asm/pmc.h>
45dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4614cf11afSPaul Mackerras #include <asm/reg.h>
4786417780SPaul Mackerras #endif
4814cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
4914cf11afSPaul Mackerras #include <asm/backlight.h>
5014cf11afSPaul Mackerras #endif
51dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5286417780SPaul Mackerras #include <asm/firmware.h>
53dc1c1ca3SStephen Rothwell #include <asm/processor.h>
54dc1c1ca3SStephen Rothwell #endif
55c0ce7d08SDavid Wilder #include <asm/kexec.h>
5616c57b36SKumar Gala #include <asm/ppc-opcode.h>
57620165f9SKumar Gala #ifdef CONFIG_FSL_BOOKE
58620165f9SKumar Gala #include <asm/dbell.h>
59620165f9SKumar Gala #endif
60dc1c1ca3SStephen Rothwell 
617dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
625be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
635be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
645be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
655be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
6914cf11afSPaul Mackerras 
7014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7714cf11afSPaul Mackerras #endif
7814cf11afSPaul Mackerras 
7914cf11afSPaul Mackerras /*
8014cf11afSPaul Mackerras  * Trap & Exception support
8114cf11afSPaul Mackerras  */
8214cf11afSPaul Mackerras 
836031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
846031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
856031d9d9Santon@samba.org {
866031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
876031d9d9Santon@samba.org 	if (pmac_backlight) {
886031d9d9Santon@samba.org 		struct backlight_properties *props;
896031d9d9Santon@samba.org 
906031d9d9Santon@samba.org 		props = &pmac_backlight->props;
916031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
926031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
936031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
946031d9d9Santon@samba.org 	}
956031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
966031d9d9Santon@samba.org }
976031d9d9Santon@samba.org #else
986031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
996031d9d9Santon@samba.org #endif
1006031d9d9Santon@samba.org 
10114cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
10214cf11afSPaul Mackerras {
10334c2a14fSanton@samba.org 	static struct {
104b8f87782SThomas Gleixner 		raw_spinlock_t lock;
10534c2a14fSanton@samba.org 		u32 lock_owner;
10634c2a14fSanton@samba.org 		int lock_owner_depth;
10734c2a14fSanton@samba.org 	} die = {
108b8f87782SThomas Gleixner 		.lock =			__RAW_SPIN_LOCK_UNLOCKED(die.lock),
10934c2a14fSanton@samba.org 		.lock_owner =		-1,
11034c2a14fSanton@samba.org 		.lock_owner_depth =	0
11134c2a14fSanton@samba.org 	};
112c0ce7d08SDavid Wilder 	static int die_counter;
11334c2a14fSanton@samba.org 	unsigned long flags;
11414cf11afSPaul Mackerras 
11514cf11afSPaul Mackerras 	if (debugger(regs))
11614cf11afSPaul Mackerras 		return 1;
11714cf11afSPaul Mackerras 
118293e4688Santon@samba.org 	oops_enter();
119293e4688Santon@samba.org 
12034c2a14fSanton@samba.org 	if (die.lock_owner != raw_smp_processor_id()) {
12114cf11afSPaul Mackerras 		console_verbose();
122b8f87782SThomas Gleixner 		raw_spin_lock_irqsave(&die.lock, flags);
12334c2a14fSanton@samba.org 		die.lock_owner = smp_processor_id();
12434c2a14fSanton@samba.org 		die.lock_owner_depth = 0;
12514cf11afSPaul Mackerras 		bust_spinlocks(1);
1266031d9d9Santon@samba.org 		if (machine_is(powermac))
1276031d9d9Santon@samba.org 			pmac_backlight_unblank();
12834c2a14fSanton@samba.org 	} else {
12934c2a14fSanton@samba.org 		local_save_flags(flags);
13034c2a14fSanton@samba.org 	}
1315474c120SMichael Hanselmann 
13234c2a14fSanton@samba.org 	if (++die.lock_owner_depth < 3) {
13314cf11afSPaul Mackerras 		printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
13414cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
13514cf11afSPaul Mackerras 		printk("PREEMPT ");
13614cf11afSPaul Mackerras #endif
13714cf11afSPaul Mackerras #ifdef CONFIG_SMP
13814cf11afSPaul Mackerras 		printk("SMP NR_CPUS=%d ", NR_CPUS);
13914cf11afSPaul Mackerras #endif
14014cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
14114cf11afSPaul Mackerras 		printk("DEBUG_PAGEALLOC ");
14214cf11afSPaul Mackerras #endif
14314cf11afSPaul Mackerras #ifdef CONFIG_NUMA
14414cf11afSPaul Mackerras 		printk("NUMA ");
14514cf11afSPaul Mackerras #endif
146ae7f4463Santon@samba.org 		printk("%s\n", ppc_md.name ? ppc_md.name : "");
147e8222502SBenjamin Herrenschmidt 
14866fcb105SAnton Blanchard 		sysfs_printk_last_file();
14966fcb105SAnton Blanchard 		if (notify_die(DIE_OOPS, str, regs, err, 255,
15066fcb105SAnton Blanchard 			       SIGSEGV) == NOTIFY_STOP)
15166fcb105SAnton Blanchard 			return 1;
15266fcb105SAnton Blanchard 
15314cf11afSPaul Mackerras 		print_modules();
15414cf11afSPaul Mackerras 		show_regs(regs);
15534c2a14fSanton@samba.org 	} else {
15634c2a14fSanton@samba.org 		printk("Recursive die() failure, output suppressed\n");
15734c2a14fSanton@samba.org 	}
15834c2a14fSanton@samba.org 
15914cf11afSPaul Mackerras 	bust_spinlocks(0);
16034c2a14fSanton@samba.org 	die.lock_owner = -1;
161bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
162b8f87782SThomas Gleixner 	raw_spin_unlock_irqrestore(&die.lock, flags);
163cc532915SMichael Ellerman 
164c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
165c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
166cc532915SMichael Ellerman 		crash_kexec(regs);
167c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
16814cf11afSPaul Mackerras 
16914cf11afSPaul Mackerras 	if (in_interrupt())
17014cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
17114cf11afSPaul Mackerras 
172cea6a4baSHorms 	if (panic_on_oops)
173012c437dSHorms 		panic("Fatal exception");
174cea6a4baSHorms 
175293e4688Santon@samba.org 	oops_exit();
17614cf11afSPaul Mackerras 	do_exit(err);
17714cf11afSPaul Mackerras 
17814cf11afSPaul Mackerras 	return 0;
17914cf11afSPaul Mackerras }
18014cf11afSPaul Mackerras 
18125baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
18225baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
18325baa35bSOleg Nesterov {
18425baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
18525baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
18625baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
18725baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
18825baa35bSOleg Nesterov }
18925baa35bSOleg Nesterov 
19014cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
19114cf11afSPaul Mackerras {
19214cf11afSPaul Mackerras 	siginfo_t info;
193d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
194d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
195d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
196d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
19714cf11afSPaul Mackerras 
19814cf11afSPaul Mackerras 	if (!user_mode(regs)) {
19914cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
20014cf11afSPaul Mackerras 			return;
201d0c3d534SOlof Johansson 	} else if (show_unhandled_signals &&
202d0c3d534SOlof Johansson 		    unhandled_signal(current, signr) &&
203d0c3d534SOlof Johansson 		    printk_ratelimit()) {
204d0c3d534SOlof Johansson 			printk(regs->msr & MSR_SF ? fmt64 : fmt32,
205d0c3d534SOlof Johansson 				current->comm, current->pid, signr,
206d0c3d534SOlof Johansson 				addr, regs->nip, regs->link, code);
20714cf11afSPaul Mackerras 		}
20814cf11afSPaul Mackerras 
20914cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
21014cf11afSPaul Mackerras 	info.si_signo = signr;
21114cf11afSPaul Mackerras 	info.si_code = code;
21214cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
21314cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
21414cf11afSPaul Mackerras }
21514cf11afSPaul Mackerras 
21614cf11afSPaul Mackerras #ifdef CONFIG_PPC64
21714cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
21814cf11afSPaul Mackerras {
21914cf11afSPaul Mackerras 	/* See if any machine dependent calls */
220c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
221c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
222c902be71SArnd Bergmann 			return;
223c902be71SArnd Bergmann 	}
22414cf11afSPaul Mackerras 
225c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
226c0ce7d08SDavid Wilder 	cpu_set(smp_processor_id(), cpus_in_sr);
227c0ce7d08SDavid Wilder #endif
228c0ce7d08SDavid Wilder 
2298dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
23014cf11afSPaul Mackerras 
231eac8392fSDavid Wilder 	/*
232eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
233eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
234eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
235eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
236eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
237eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
238eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
239eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
240eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
241eac8392fSDavid Wilder 	 */
242eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
243eac8392fSDavid Wilder 
24414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
24514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
24614cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
24714cf11afSPaul Mackerras 
24814cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
24914cf11afSPaul Mackerras }
25014cf11afSPaul Mackerras #endif
25114cf11afSPaul Mackerras 
25214cf11afSPaul Mackerras /*
25314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
25414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
25514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
25614cf11afSPaul Mackerras  * table.
25714cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
25814cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
25914cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
26014cf11afSPaul Mackerras  *  -- paulus.
26114cf11afSPaul Mackerras  */
26214cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
26314cf11afSPaul Mackerras {
26468a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
26514cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
26614cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
26714cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
26814cf11afSPaul Mackerras 
26914cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
27014cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
27114cf11afSPaul Mackerras 		/*
27214cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
27314cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
27414cf11afSPaul Mackerras 		 * As the address is in the exception table
27514cf11afSPaul Mackerras 		 * we should be able to read the instr there.
27614cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
27714cf11afSPaul Mackerras 		 * load or store.
27814cf11afSPaul Mackerras 		 */
27914cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
28014cf11afSPaul Mackerras 			nip -= 2;
28114cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
28214cf11afSPaul Mackerras 			--nip;
28314cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
28414cf11afSPaul Mackerras 			/* sync or twi */
28514cf11afSPaul Mackerras 			unsigned int rb;
28614cf11afSPaul Mackerras 
28714cf11afSPaul Mackerras 			--nip;
28814cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
28914cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
29014cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
29114cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
29214cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
29314cf11afSPaul Mackerras 			regs->nip = entry->fixup;
29414cf11afSPaul Mackerras 			return 1;
29514cf11afSPaul Mackerras 		}
29614cf11afSPaul Mackerras 	}
29768a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
29814cf11afSPaul Mackerras 	return 0;
29914cf11afSPaul Mackerras }
30014cf11afSPaul Mackerras 
301172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
30214cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
30314cf11afSPaul Mackerras    is in the ESR. */
30414cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
30514cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
30614cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
30714cf11afSPaul Mackerras #else
30886d7a9a9SBecky Bruce #define get_mc_reason(regs)	(mfspr(SPRN_MCSR) & MCSR_MASK)
30914cf11afSPaul Mackerras #endif
31014cf11afSPaul Mackerras #define REASON_FP		ESR_FP
31114cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
31214cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
31314cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
31414cf11afSPaul Mackerras 
31514cf11afSPaul Mackerras /* single-step stuff */
31614cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
31714cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
31814cf11afSPaul Mackerras 
31914cf11afSPaul Mackerras #else
32014cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
32114cf11afSPaul Mackerras    exception is in the MSR. */
32214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
32314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
32414cf11afSPaul Mackerras #define REASON_FP		0x100000
32514cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
32614cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
32714cf11afSPaul Mackerras #define REASON_TRAP		0x20000
32814cf11afSPaul Mackerras 
32914cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
33014cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
33114cf11afSPaul Mackerras #endif
33214cf11afSPaul Mackerras 
33347c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
33447c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
33514cf11afSPaul Mackerras {
3361a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
33714cf11afSPaul Mackerras 
33814cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
33914cf11afSPaul Mackerras 		printk("Instruction");
34014cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
34114cf11afSPaul Mackerras 	} else
34214cf11afSPaul Mackerras 		printk("Data");
34314cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
34447c0bd1aSBenjamin Herrenschmidt 
34547c0bd1aSBenjamin Herrenschmidt 	return 0;
34647c0bd1aSBenjamin Herrenschmidt }
34747c0bd1aSBenjamin Herrenschmidt 
34847c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
34947c0bd1aSBenjamin Herrenschmidt {
35047c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
35147c0bd1aSBenjamin Herrenschmidt 
35214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
35314cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
35414cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
35514cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35614cf11afSPaul Mackerras 	}
35714cf11afSPaul Mackerras 	else {
35814cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
35914cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
36014cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
36114cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
36214cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
36314cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
36414cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
36514cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
36614cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
36714cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
36814cf11afSPaul Mackerras 			flush_instruction_cache();
36914cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
37014cf11afSPaul Mackerras 		}
37114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
37214cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
37314cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
37414cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
37514cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
37614cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
37714cf11afSPaul Mackerras 
37814cf11afSPaul Mackerras 		/* Clear MCSR */
37914cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
38014cf11afSPaul Mackerras 	}
38147c0bd1aSBenjamin Herrenschmidt 	return 0;
38247c0bd1aSBenjamin Herrenschmidt }
383*fc5e7097SDave Kleikamp 
384*fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
385*fc5e7097SDave Kleikamp {
386*fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
387*fc5e7097SDave Kleikamp 	u32 mcsr;
388*fc5e7097SDave Kleikamp 
389*fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
390*fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
391*fc5e7097SDave Kleikamp 		printk(KERN_ERR
392*fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
393*fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
394*fc5e7097SDave Kleikamp 		return 0;
395*fc5e7097SDave Kleikamp 	}
396*fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
397*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
398*fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
399*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
400*fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
401*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
402*fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
403*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
404*fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
405*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
406*fc5e7097SDave Kleikamp 		flush_instruction_cache();
407*fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
408*fc5e7097SDave Kleikamp 	}
409*fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
410*fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
411*fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
412*fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
413*fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
414*fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
415*fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
416*fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
417*fc5e7097SDave Kleikamp 
418*fc5e7097SDave Kleikamp 	/* Clear MCSR */
419*fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
420*fc5e7097SDave Kleikamp 
421*fc5e7097SDave Kleikamp 	return 0;
422*fc5e7097SDave Kleikamp }
42314cf11afSPaul Mackerras #elif defined(CONFIG_E500)
42447c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
42547c0bd1aSBenjamin Herrenschmidt {
42647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
42747c0bd1aSBenjamin Herrenschmidt 
42814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42914cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
43014cf11afSPaul Mackerras 
43114cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
43214cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
43314cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
43414cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
43514cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
43614cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
43714cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
43814cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
43914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
44014cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
44114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
44214cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
44314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
44414cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
44514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
44614cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
44714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
44814cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
44914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
45014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
45114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
45214cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
45314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
45414cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
45547c0bd1aSBenjamin Herrenschmidt 
45647c0bd1aSBenjamin Herrenschmidt 	return 0;
45747c0bd1aSBenjamin Herrenschmidt }
45814cf11afSPaul Mackerras #elif defined(CONFIG_E200)
45947c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
46047c0bd1aSBenjamin Herrenschmidt {
46147c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
46247c0bd1aSBenjamin Herrenschmidt 
46314cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
46414cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
46514cf11afSPaul Mackerras 
46614cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
46714cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
46814cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
46914cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
47014cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
47114cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
47214cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
47314cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
47414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
47514cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
47614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
47714cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
47814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
47914cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
48047c0bd1aSBenjamin Herrenschmidt 
48147c0bd1aSBenjamin Herrenschmidt 	return 0;
48247c0bd1aSBenjamin Herrenschmidt }
48347c0bd1aSBenjamin Herrenschmidt #else
48447c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
48547c0bd1aSBenjamin Herrenschmidt {
48647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
48747c0bd1aSBenjamin Herrenschmidt 
48814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
48914cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
49014cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
49114cf11afSPaul Mackerras 	case 0x80000:
49214cf11afSPaul Mackerras 		printk("Machine check signal\n");
49314cf11afSPaul Mackerras 		break;
49414cf11afSPaul Mackerras 	case 0:		/* for 601 */
49514cf11afSPaul Mackerras 	case 0x40000:
49614cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
49714cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
49814cf11afSPaul Mackerras 		break;
49914cf11afSPaul Mackerras 	case 0x20000:
50014cf11afSPaul Mackerras 		printk("Data parity error signal\n");
50114cf11afSPaul Mackerras 		break;
50214cf11afSPaul Mackerras 	case 0x10000:
50314cf11afSPaul Mackerras 		printk("Address parity error signal\n");
50414cf11afSPaul Mackerras 		break;
50514cf11afSPaul Mackerras 	case 0x20000000:
50614cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
50714cf11afSPaul Mackerras 		break;
50814cf11afSPaul Mackerras 	case 0x40000000:
50914cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
51014cf11afSPaul Mackerras 		break;
51114cf11afSPaul Mackerras 	case 0x00100000:
51214cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
51314cf11afSPaul Mackerras 		break;
51414cf11afSPaul Mackerras 	default:
51514cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
51614cf11afSPaul Mackerras 	}
51775918a4bSOlof Johansson 	return 0;
51875918a4bSOlof Johansson }
51947c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
52075918a4bSOlof Johansson 
52175918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
52275918a4bSOlof Johansson {
52375918a4bSOlof Johansson 	int recover = 0;
52475918a4bSOlof Johansson 
52589713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
52689713ed1SAnton Blanchard 
52747c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
52847c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
52947c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
53047c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
53147c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
53247c0bd1aSBenjamin Herrenschmidt 	 */
53375918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
53475918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
53547c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
53647c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
53775918a4bSOlof Johansson 
53847c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
53975918a4bSOlof Johansson 		return;
54075918a4bSOlof Johansson 
54175918a4bSOlof Johansson 	if (user_mode(regs)) {
54275918a4bSOlof Johansson 		regs->msr |= MSR_RI;
54375918a4bSOlof Johansson 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
54475918a4bSOlof Johansson 		return;
54575918a4bSOlof Johansson 	}
54675918a4bSOlof Johansson 
54775918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
54847c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
54947c0bd1aSBenjamin Herrenschmidt 	 *
55047c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
55147c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
55247c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
55347c0bd1aSBenjamin Herrenschmidt 	 */
55475918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
55575918a4bSOlof Johansson 	return;
55675918a4bSOlof Johansson #endif
55775918a4bSOlof Johansson 
55875918a4bSOlof Johansson 	if (debugger_fault_handler(regs)) {
55975918a4bSOlof Johansson 		regs->msr |= MSR_RI;
56075918a4bSOlof Johansson 		return;
56175918a4bSOlof Johansson 	}
56275918a4bSOlof Johansson 
56375918a4bSOlof Johansson 	if (check_io_access(regs))
56475918a4bSOlof Johansson 		return;
56575918a4bSOlof Johansson 
56614cf11afSPaul Mackerras 	if (debugger_fault_handler(regs))
56714cf11afSPaul Mackerras 		return;
5688dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
56914cf11afSPaul Mackerras 
57014cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
57114cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
57214cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
57314cf11afSPaul Mackerras }
57414cf11afSPaul Mackerras 
57514cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
57614cf11afSPaul Mackerras {
57714cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
57814cf11afSPaul Mackerras }
57914cf11afSPaul Mackerras 
580dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
58114cf11afSPaul Mackerras {
58214cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
58314cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
58414cf11afSPaul Mackerras 
58514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
58614cf11afSPaul Mackerras }
58714cf11afSPaul Mackerras 
588dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
58914cf11afSPaul Mackerras {
59014cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
59114cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
59214cf11afSPaul Mackerras 		return;
59314cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
59414cf11afSPaul Mackerras 		return;
59514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
59614cf11afSPaul Mackerras }
59714cf11afSPaul Mackerras 
59814cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
59914cf11afSPaul Mackerras {
60014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
60114cf11afSPaul Mackerras }
60214cf11afSPaul Mackerras 
6038dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
60414cf11afSPaul Mackerras {
60514cf11afSPaul Mackerras 	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
60614cf11afSPaul Mackerras 
60714cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
60814cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
60914cf11afSPaul Mackerras 		return;
61014cf11afSPaul Mackerras 	if (debugger_sstep(regs))
61114cf11afSPaul Mackerras 		return;
61214cf11afSPaul Mackerras 
61314cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
61414cf11afSPaul Mackerras }
61514cf11afSPaul Mackerras 
61614cf11afSPaul Mackerras /*
61714cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
61814cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
61914cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
62014cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
62114cf11afSPaul Mackerras  */
6228dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
62314cf11afSPaul Mackerras {
62414cf11afSPaul Mackerras 	if (single_stepping(regs)) {
62514cf11afSPaul Mackerras 		clear_single_step(regs);
62614cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
62714cf11afSPaul Mackerras 	}
62814cf11afSPaul Mackerras }
62914cf11afSPaul Mackerras 
6305fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
631dc1c1ca3SStephen Rothwell {
6325fad293bSKumar Gala 	int ret = 0;
633dc1c1ca3SStephen Rothwell 
634dc1c1ca3SStephen Rothwell 	/* Invalid operation */
635dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
6365fad293bSKumar Gala 		ret = FPE_FLTINV;
637dc1c1ca3SStephen Rothwell 
638dc1c1ca3SStephen Rothwell 	/* Overflow */
639dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
6405fad293bSKumar Gala 		ret = FPE_FLTOVF;
641dc1c1ca3SStephen Rothwell 
642dc1c1ca3SStephen Rothwell 	/* Underflow */
643dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
6445fad293bSKumar Gala 		ret = FPE_FLTUND;
645dc1c1ca3SStephen Rothwell 
646dc1c1ca3SStephen Rothwell 	/* Divide by zero */
647dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
6485fad293bSKumar Gala 		ret = FPE_FLTDIV;
649dc1c1ca3SStephen Rothwell 
650dc1c1ca3SStephen Rothwell 	/* Inexact result */
651dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
6525fad293bSKumar Gala 		ret = FPE_FLTRES;
6535fad293bSKumar Gala 
6545fad293bSKumar Gala 	return ret;
6555fad293bSKumar Gala }
6565fad293bSKumar Gala 
6575fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
6585fad293bSKumar Gala {
6595fad293bSKumar Gala 	int code = 0;
6605fad293bSKumar Gala 
6615fad293bSKumar Gala 	flush_fp_to_thread(current);
6625fad293bSKumar Gala 
6635fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
664dc1c1ca3SStephen Rothwell 
665dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
666dc1c1ca3SStephen Rothwell }
667dc1c1ca3SStephen Rothwell 
668dc1c1ca3SStephen Rothwell /*
669dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
67014cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
67114cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
67214cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
67314cf11afSPaul Mackerras  *
67414cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
67514cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
67614cf11afSPaul Mackerras  * bits is faster and easier.
67786417780SPaul Mackerras  *
67814cf11afSPaul Mackerras  */
67914cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
68014cf11afSPaul Mackerras {
68114cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
68214cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
68314cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
68414cf11afSPaul Mackerras 	u32 num_bytes;
68514cf11afSPaul Mackerras 	unsigned long EA;
68614cf11afSPaul Mackerras 	int pos = 0;
68714cf11afSPaul Mackerras 
68814cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
68916c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
69014cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
69114cf11afSPaul Mackerras 			return -EINVAL;
69214cf11afSPaul Mackerras 
69314cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
69414cf11afSPaul Mackerras 
69516c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
69616c57b36SKumar Gala 		case PPC_INST_LSWX:
69716c57b36SKumar Gala 		case PPC_INST_STSWX:
69814cf11afSPaul Mackerras 			EA += NB_RB;
69914cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
70014cf11afSPaul Mackerras 			break;
70116c57b36SKumar Gala 		case PPC_INST_LSWI:
70216c57b36SKumar Gala 		case PPC_INST_STSWI:
70314cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
70414cf11afSPaul Mackerras 			break;
70514cf11afSPaul Mackerras 		default:
70614cf11afSPaul Mackerras 			return -EINVAL;
70714cf11afSPaul Mackerras 	}
70814cf11afSPaul Mackerras 
70914cf11afSPaul Mackerras 	while (num_bytes != 0)
71014cf11afSPaul Mackerras 	{
71114cf11afSPaul Mackerras 		u8 val;
71214cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
71314cf11afSPaul Mackerras 
71416c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
71516c57b36SKumar Gala 			case PPC_INST_LSWX:
71616c57b36SKumar Gala 			case PPC_INST_LSWI:
71714cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
71814cf11afSPaul Mackerras 					return -EFAULT;
71914cf11afSPaul Mackerras 				/* first time updating this reg,
72014cf11afSPaul Mackerras 				 * zero it out */
72114cf11afSPaul Mackerras 				if (pos == 0)
72214cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
72314cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
72414cf11afSPaul Mackerras 				break;
72516c57b36SKumar Gala 			case PPC_INST_STSWI:
72616c57b36SKumar Gala 			case PPC_INST_STSWX:
72714cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
72814cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
72914cf11afSPaul Mackerras 					return -EFAULT;
73014cf11afSPaul Mackerras 				break;
73114cf11afSPaul Mackerras 		}
73214cf11afSPaul Mackerras 		/* move EA to next address */
73314cf11afSPaul Mackerras 		EA += 1;
73414cf11afSPaul Mackerras 		num_bytes--;
73514cf11afSPaul Mackerras 
73614cf11afSPaul Mackerras 		/* manage our position within the register */
73714cf11afSPaul Mackerras 		if (++pos == 4) {
73814cf11afSPaul Mackerras 			pos = 0;
73914cf11afSPaul Mackerras 			if (++rT == 32)
74014cf11afSPaul Mackerras 				rT = 0;
74114cf11afSPaul Mackerras 		}
74214cf11afSPaul Mackerras 	}
74314cf11afSPaul Mackerras 
74414cf11afSPaul Mackerras 	return 0;
74514cf11afSPaul Mackerras }
74614cf11afSPaul Mackerras 
747c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
748c3412dcbSWill Schmidt {
749c3412dcbSWill Schmidt 	u32 ra,rs;
750c3412dcbSWill Schmidt 	unsigned long tmp;
751c3412dcbSWill Schmidt 
752c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
753c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
754c3412dcbSWill Schmidt 
755c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
756c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
757c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
758c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
759c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
760c3412dcbSWill Schmidt 
761c3412dcbSWill Schmidt 	return 0;
762c3412dcbSWill Schmidt }
763c3412dcbSWill Schmidt 
764c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
765c1469f13SKumar Gala {
766c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
767c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
768c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
769c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
770c1469f13SKumar Gala 	u8 bit;
771c1469f13SKumar Gala 	unsigned long tmp;
772c1469f13SKumar Gala 
773c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
774c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
775c1469f13SKumar Gala 
776c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
777c1469f13SKumar Gala 
778c1469f13SKumar Gala 	return 0;
779c1469f13SKumar Gala }
780c1469f13SKumar Gala 
78114cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
78214cf11afSPaul Mackerras {
78314cf11afSPaul Mackerras 	u32 instword;
78414cf11afSPaul Mackerras 	u32 rd;
78514cf11afSPaul Mackerras 
786fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
78714cf11afSPaul Mackerras 		return -EINVAL;
78814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
78914cf11afSPaul Mackerras 
79014cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
79114cf11afSPaul Mackerras 		return -EFAULT;
79214cf11afSPaul Mackerras 
79314cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
79416c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
795eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
79614cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
79714cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
79814cf11afSPaul Mackerras 		return 0;
79914cf11afSPaul Mackerras 	}
80014cf11afSPaul Mackerras 
80114cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
80280947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
803eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
80414cf11afSPaul Mackerras 		return 0;
80580947e7cSGeert Uytterhoeven 	}
80614cf11afSPaul Mackerras 
80714cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
80816c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
80986417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
81014cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
81114cf11afSPaul Mackerras 
812eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
81314cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
81414cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
81514cf11afSPaul Mackerras 		return 0;
81614cf11afSPaul Mackerras 	}
81714cf11afSPaul Mackerras 
81814cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
81980947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
820eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
82114cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
82280947e7cSGeert Uytterhoeven 	}
82314cf11afSPaul Mackerras 
824c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
82516c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
826eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
827c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
828c3412dcbSWill Schmidt 	}
829c3412dcbSWill Schmidt 
830c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
83116c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
832eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
833c1469f13SKumar Gala 		return emulate_isel(regs, instword);
834c1469f13SKumar Gala 	}
835c1469f13SKumar Gala 
83614cf11afSPaul Mackerras 	return -EINVAL;
83714cf11afSPaul Mackerras }
83814cf11afSPaul Mackerras 
83973c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
84014cf11afSPaul Mackerras {
84173c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
84214cf11afSPaul Mackerras }
84314cf11afSPaul Mackerras 
8448dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
84514cf11afSPaul Mackerras {
84614cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
84714cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
84814cf11afSPaul Mackerras 
849aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
85004903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
85114cf11afSPaul Mackerras 
85214cf11afSPaul Mackerras 	if (reason & REASON_FP) {
85314cf11afSPaul Mackerras 		/* IEEE FP exception */
854dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
8558dad3f92SPaul Mackerras 		return;
8568dad3f92SPaul Mackerras 	}
8578dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
85814cf11afSPaul Mackerras 		/* trap exception */
859dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
860dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
861dc1c1ca3SStephen Rothwell 			return;
86214cf11afSPaul Mackerras 		if (debugger_bpt(regs))
86314cf11afSPaul Mackerras 			return;
86473c9ceabSJeremy Fitzhardinge 
86573c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
866608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
86714cf11afSPaul Mackerras 			regs->nip += 4;
86814cf11afSPaul Mackerras 			return;
86914cf11afSPaul Mackerras 		}
8708dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
8718dad3f92SPaul Mackerras 		return;
8728dad3f92SPaul Mackerras 	}
8738dad3f92SPaul Mackerras 
874cd8a5673SPaul Mackerras 	local_irq_enable();
875cd8a5673SPaul Mackerras 
87604903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
87704903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
87804903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
87904903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
88004903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
88104903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
88204903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
88304903a30SKumar Gala 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
8845fad293bSKumar Gala 	switch (do_mathemu(regs)) {
8855fad293bSKumar Gala 	case 0:
88604903a30SKumar Gala 		emulate_single_step(regs);
88704903a30SKumar Gala 		return;
8885fad293bSKumar Gala 	case 1: {
8895fad293bSKumar Gala 			int code = 0;
8905fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
8915fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
8925fad293bSKumar Gala 			return;
89304903a30SKumar Gala 		}
8945fad293bSKumar Gala 	case -EFAULT:
8955fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8965fad293bSKumar Gala 		return;
8975fad293bSKumar Gala 	}
8985fad293bSKumar Gala 	/* fall through on any other errors */
89904903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
90004903a30SKumar Gala 
9018dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
9028dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
90314cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
90414cf11afSPaul Mackerras 		case 0:
90514cf11afSPaul Mackerras 			regs->nip += 4;
90614cf11afSPaul Mackerras 			emulate_single_step(regs);
9078dad3f92SPaul Mackerras 			return;
90814cf11afSPaul Mackerras 		case -EFAULT:
90914cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9108dad3f92SPaul Mackerras 			return;
9118dad3f92SPaul Mackerras 		}
9128dad3f92SPaul Mackerras 	}
9138dad3f92SPaul Mackerras 
91414cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
91514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
91614cf11afSPaul Mackerras 	else
91714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
91814cf11afSPaul Mackerras }
91914cf11afSPaul Mackerras 
920dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
92114cf11afSPaul Mackerras {
9224393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
92314cf11afSPaul Mackerras 
924e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
925e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
92614cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
92714cf11afSPaul Mackerras 
92814cf11afSPaul Mackerras 	if (fixed == 1) {
92914cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
93014cf11afSPaul Mackerras 		emulate_single_step(regs);
93114cf11afSPaul Mackerras 		return;
93214cf11afSPaul Mackerras 	}
93314cf11afSPaul Mackerras 
93414cf11afSPaul Mackerras 	/* Operand address was bad */
93514cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
9364393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
9374393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
9384393c4f6SBenjamin Herrenschmidt 	} else {
9394393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
9404393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
94114cf11afSPaul Mackerras 	}
9424393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
9434393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
9444393c4f6SBenjamin Herrenschmidt 	else
9454393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
94614cf11afSPaul Mackerras }
94714cf11afSPaul Mackerras 
94814cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
94914cf11afSPaul Mackerras {
95014cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
95114cf11afSPaul Mackerras 	       current, regs->gpr[1]);
95214cf11afSPaul Mackerras 	debugger(regs);
95314cf11afSPaul Mackerras 	show_regs(regs);
95414cf11afSPaul Mackerras 	panic("kernel stack overflow");
95514cf11afSPaul Mackerras }
95614cf11afSPaul Mackerras 
95714cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
95814cf11afSPaul Mackerras {
95914cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
96014cf11afSPaul Mackerras 	       regs->nip, regs->msr);
96114cf11afSPaul Mackerras 	debugger(regs);
96214cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
96314cf11afSPaul Mackerras }
96414cf11afSPaul Mackerras 
96514cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
96614cf11afSPaul Mackerras {
96714cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
96819c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
96914cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
97014cf11afSPaul Mackerras }
97114cf11afSPaul Mackerras 
972dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
973dc1c1ca3SStephen Rothwell {
974dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
975dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
976dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
977dc1c1ca3SStephen Rothwell }
978dc1c1ca3SStephen Rothwell 
979dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
980dc1c1ca3SStephen Rothwell {
981dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
982dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
983dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
984dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
985dc1c1ca3SStephen Rothwell 		return;
986dc1c1ca3SStephen Rothwell 	}
9876c4841c2SAnton Blanchard 
988dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
989dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
990dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
991dc1c1ca3SStephen Rothwell }
992dc1c1ca3SStephen Rothwell 
993ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
994ce48b210SMichael Neuling {
995ce48b210SMichael Neuling 	if (user_mode(regs)) {
996ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
997ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
998ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
999ce48b210SMichael Neuling 		return;
1000ce48b210SMichael Neuling 	}
1001ce48b210SMichael Neuling 
1002ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1003ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1004ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1005ce48b210SMichael Neuling }
1006ce48b210SMichael Neuling 
1007dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1008dc1c1ca3SStephen Rothwell {
100989713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
101089713ed1SAnton Blanchard 
1011dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1012dc1c1ca3SStephen Rothwell }
1013dc1c1ca3SStephen Rothwell 
10148dad3f92SPaul Mackerras #ifdef CONFIG_8xx
101514cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
101614cf11afSPaul Mackerras {
101714cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
101814cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
10195dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
102014cf11afSPaul Mackerras 	int errcode;
10215dd57a13SScott Wood #endif
102214cf11afSPaul Mackerras 
102314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
102414cf11afSPaul Mackerras 
102514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
102614cf11afSPaul Mackerras 		debugger(regs);
102714cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
102814cf11afSPaul Mackerras 	}
102914cf11afSPaul Mackerras 
103014cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
103114cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
103280947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1033eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
10345fad293bSKumar Gala 
10355fad293bSKumar Gala 	switch (errcode) {
10365fad293bSKumar Gala 	case 0:
10375fad293bSKumar Gala 		emulate_single_step(regs);
10385fad293bSKumar Gala 		return;
10395fad293bSKumar Gala 	case 1: {
10405fad293bSKumar Gala 			int code = 0;
10415fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10425fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10435fad293bSKumar Gala 			return;
10445fad293bSKumar Gala 		}
10455fad293bSKumar Gala 	case -EFAULT:
10465fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10475fad293bSKumar Gala 		return;
10485fad293bSKumar Gala 	default:
10495fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10505fad293bSKumar Gala 		return;
10515fad293bSKumar Gala 	}
10525fad293bSKumar Gala 
10535dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
105414cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
105580947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1056eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
105780947e7cSGeert Uytterhoeven 
10585fad293bSKumar Gala 	switch (errcode) {
10595fad293bSKumar Gala 	case 0:
106014cf11afSPaul Mackerras 		emulate_single_step(regs);
10615fad293bSKumar Gala 		return;
10625fad293bSKumar Gala 	case 1:
10635fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10645fad293bSKumar Gala 		return;
10655fad293bSKumar Gala 	case -EFAULT:
10665fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10675fad293bSKumar Gala 		return;
10685fad293bSKumar Gala 	}
10695dd57a13SScott Wood #else
10705dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
10715fad293bSKumar Gala #endif
107214cf11afSPaul Mackerras }
10738dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
107414cf11afSPaul Mackerras 
1075172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
10763bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
10773bffb652SDave Kleikamp {
10783bffb652SDave Kleikamp 	int changed = 0;
10793bffb652SDave Kleikamp 	/*
10803bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
10813bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
10823bffb652SDave Kleikamp 	 */
10833bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
10843bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
10853bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
10863bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
10873bffb652SDave Kleikamp #endif
10883bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
10893bffb652SDave Kleikamp 			     5);
10903bffb652SDave Kleikamp 		changed |= 0x01;
10913bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
10923bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
10933bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
10943bffb652SDave Kleikamp 			     6);
10953bffb652SDave Kleikamp 		changed |= 0x01;
10963bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
10973bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
10983bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
10993bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
11003bffb652SDave Kleikamp 			     1);
11013bffb652SDave Kleikamp 		changed |= 0x01;
11023bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
11033bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
11043bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
11053bffb652SDave Kleikamp 			     2);
11063bffb652SDave Kleikamp 		changed |= 0x01;
11073bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
11083bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
11093bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
11103bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
11113bffb652SDave Kleikamp 			     3);
11123bffb652SDave Kleikamp 		changed |= 0x01;
11133bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
11143bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
11153bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
11163bffb652SDave Kleikamp 			     4);
11173bffb652SDave Kleikamp 		changed |= 0x01;
11183bffb652SDave Kleikamp 	}
11193bffb652SDave Kleikamp 	/*
11203bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
11213bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
11223bffb652SDave Kleikamp 	 * back on or not.
11233bffb652SDave Kleikamp 	 */
11243bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
11253bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
11263bffb652SDave Kleikamp 	else
11273bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
11283bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
11293bffb652SDave Kleikamp 
11303bffb652SDave Kleikamp 	if (changed & 0x01)
11313bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
11323bffb652SDave Kleikamp }
113314cf11afSPaul Mackerras 
1134f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
113514cf11afSPaul Mackerras {
11363bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
11373bffb652SDave Kleikamp 
1138ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1139ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1140ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1141ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1142ec097c84SRoland McGrath 	 */
1143ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1144ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1145ec097c84SRoland McGrath 
1146ec097c84SRoland McGrath 		/* Disable BT */
1147ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1148ec097c84SRoland McGrath 		/* Clear the BT event */
1149ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1150ec097c84SRoland McGrath 
1151ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1152ec097c84SRoland McGrath 		if (user_mode(regs)) {
1153ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1154ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1155ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1156ec097c84SRoland McGrath 			return;
1157ec097c84SRoland McGrath 		}
1158ec097c84SRoland McGrath 
1159ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1160ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1161ec097c84SRoland McGrath 			return;
1162ec097c84SRoland McGrath 		}
1163ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1164ec097c84SRoland McGrath 			return;
1165ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
116614cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1167f8279621SKumar Gala 
116814cf11afSPaul Mackerras 		/* Disable instruction completion */
116914cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
117014cf11afSPaul Mackerras 		/* Clear the instruction completion event */
117114cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1172f8279621SKumar Gala 
1173f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1174f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
117514cf11afSPaul Mackerras 			return;
117614cf11afSPaul Mackerras 		}
1177f8279621SKumar Gala 
1178f8279621SKumar Gala 		if (debugger_sstep(regs))
1179f8279621SKumar Gala 			return;
1180f8279621SKumar Gala 
11813bffb652SDave Kleikamp 		if (user_mode(regs)) {
11823bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
11833bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
11843bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
11853bffb652SDave Kleikamp 					       current->thread.dbcr1))
11863bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
11873bffb652SDave Kleikamp 			else
11883bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
11893bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
11903bffb652SDave Kleikamp #endif
11913bffb652SDave Kleikamp 		}
1192f8279621SKumar Gala 
1193f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
11943bffb652SDave Kleikamp 	} else
11953bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
119614cf11afSPaul Mackerras }
1197172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
119814cf11afSPaul Mackerras 
119914cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
120014cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
120114cf11afSPaul Mackerras {
120214cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
120314cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
120414cf11afSPaul Mackerras }
120514cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
120614cf11afSPaul Mackerras 
120714cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1208dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
120914cf11afSPaul Mackerras {
121014cf11afSPaul Mackerras 	int err;
121114cf11afSPaul Mackerras 
121214cf11afSPaul Mackerras 	if (!user_mode(regs)) {
121314cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
121414cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
12158dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
121614cf11afSPaul Mackerras 	}
121714cf11afSPaul Mackerras 
1218dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1219dc1c1ca3SStephen Rothwell 
1220eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
122114cf11afSPaul Mackerras 	err = emulate_altivec(regs);
122214cf11afSPaul Mackerras 	if (err == 0) {
122314cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
122414cf11afSPaul Mackerras 		emulate_single_step(regs);
122514cf11afSPaul Mackerras 		return;
122614cf11afSPaul Mackerras 	}
122714cf11afSPaul Mackerras 
122814cf11afSPaul Mackerras 	if (err == -EFAULT) {
122914cf11afSPaul Mackerras 		/* got an error reading the instruction */
123014cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
123114cf11afSPaul Mackerras 	} else {
123214cf11afSPaul Mackerras 		/* didn't recognize the instruction */
123314cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
123414cf11afSPaul Mackerras 		if (printk_ratelimit())
123514cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
123614cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
123714cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
123814cf11afSPaul Mackerras 	}
123914cf11afSPaul Mackerras }
124014cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
124114cf11afSPaul Mackerras 
1242ce48b210SMichael Neuling #ifdef CONFIG_VSX
1243ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1244ce48b210SMichael Neuling {
1245ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1246ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1247ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1248ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1249ce48b210SMichael Neuling 	}
1250ce48b210SMichael Neuling 
1251ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1252ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1253ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1254ce48b210SMichael Neuling }
1255ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1256ce48b210SMichael Neuling 
125714cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
1258620165f9SKumar Gala 
1259620165f9SKumar Gala void doorbell_exception(struct pt_regs *regs)
1260620165f9SKumar Gala {
1261620165f9SKumar Gala #ifdef CONFIG_SMP
1262620165f9SKumar Gala 	int cpu = smp_processor_id();
1263620165f9SKumar Gala 	int msg;
1264620165f9SKumar Gala 
1265620165f9SKumar Gala 	if (num_online_cpus() < 2)
1266620165f9SKumar Gala 		return;
1267620165f9SKumar Gala 
1268620165f9SKumar Gala 	for (msg = 0; msg < 4; msg++)
1269620165f9SKumar Gala 		if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1270620165f9SKumar Gala 			smp_message_recv(msg);
1271620165f9SKumar Gala #else
1272620165f9SKumar Gala 	printk(KERN_WARNING "Received doorbell on non-smp system\n");
1273620165f9SKumar Gala #endif
1274620165f9SKumar Gala }
1275620165f9SKumar Gala 
127614cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
127714cf11afSPaul Mackerras 			   unsigned long error_code)
127814cf11afSPaul Mackerras {
127914cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
128014cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
128114cf11afSPaul Mackerras 	 * something smarter
128214cf11afSPaul Mackerras 	 */
128314cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
128414cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
128514cf11afSPaul Mackerras 	return;
128614cf11afSPaul Mackerras }
128714cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
128814cf11afSPaul Mackerras 
128914cf11afSPaul Mackerras #ifdef CONFIG_SPE
129014cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
129114cf11afSPaul Mackerras {
12926a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
129314cf11afSPaul Mackerras 	unsigned long spefscr;
129414cf11afSPaul Mackerras 	int fpexc_mode;
129514cf11afSPaul Mackerras 	int code = 0;
12966a800f36SLiu Yu 	int err;
12976a800f36SLiu Yu 
12986a800f36SLiu Yu 	preempt_disable();
12996a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
13006a800f36SLiu Yu 		giveup_spe(current);
13016a800f36SLiu Yu 	preempt_enable();
130214cf11afSPaul Mackerras 
130314cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
130414cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
130514cf11afSPaul Mackerras 
130614cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
130714cf11afSPaul Mackerras 		code = FPE_FLTOVF;
130814cf11afSPaul Mackerras 	}
130914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
131014cf11afSPaul Mackerras 		code = FPE_FLTUND;
131114cf11afSPaul Mackerras 	}
131214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
131314cf11afSPaul Mackerras 		code = FPE_FLTDIV;
131414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
131514cf11afSPaul Mackerras 		code = FPE_FLTINV;
131614cf11afSPaul Mackerras 	}
131714cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
131814cf11afSPaul Mackerras 		code = FPE_FLTRES;
131914cf11afSPaul Mackerras 
13206a800f36SLiu Yu 	err = do_spe_mathemu(regs);
13216a800f36SLiu Yu 	if (err == 0) {
13226a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
13236a800f36SLiu Yu 		emulate_single_step(regs);
132414cf11afSPaul Mackerras 		return;
132514cf11afSPaul Mackerras 	}
13266a800f36SLiu Yu 
13276a800f36SLiu Yu 	if (err == -EFAULT) {
13286a800f36SLiu Yu 		/* got an error reading the instruction */
13296a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
13306a800f36SLiu Yu 	} else if (err == -EINVAL) {
13316a800f36SLiu Yu 		/* didn't recognize the instruction */
13326a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
13336a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
13346a800f36SLiu Yu 	} else {
13356a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
13366a800f36SLiu Yu 	}
13376a800f36SLiu Yu 
13386a800f36SLiu Yu 	return;
13396a800f36SLiu Yu }
13406a800f36SLiu Yu 
13416a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
13426a800f36SLiu Yu {
13436a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
13446a800f36SLiu Yu 	int err;
13456a800f36SLiu Yu 
13466a800f36SLiu Yu 	preempt_disable();
13476a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
13486a800f36SLiu Yu 		giveup_spe(current);
13496a800f36SLiu Yu 	preempt_enable();
13506a800f36SLiu Yu 
13516a800f36SLiu Yu 	regs->nip -= 4;
13526a800f36SLiu Yu 	err = speround_handler(regs);
13536a800f36SLiu Yu 	if (err == 0) {
13546a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
13556a800f36SLiu Yu 		emulate_single_step(regs);
13566a800f36SLiu Yu 		return;
13576a800f36SLiu Yu 	}
13586a800f36SLiu Yu 
13596a800f36SLiu Yu 	if (err == -EFAULT) {
13606a800f36SLiu Yu 		/* got an error reading the instruction */
13616a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
13626a800f36SLiu Yu 	} else if (err == -EINVAL) {
13636a800f36SLiu Yu 		/* didn't recognize the instruction */
13646a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
13656a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
13666a800f36SLiu Yu 	} else {
13676a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
13686a800f36SLiu Yu 		return;
13696a800f36SLiu Yu 	}
13706a800f36SLiu Yu }
137114cf11afSPaul Mackerras #endif
137214cf11afSPaul Mackerras 
1373dc1c1ca3SStephen Rothwell /*
1374dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1375dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1376dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1377dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1378dc1c1ca3SStephen Rothwell  */
1379dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1380dc1c1ca3SStephen Rothwell {
1381dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1382dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1383dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1384dc1c1ca3SStephen Rothwell }
1385dc1c1ca3SStephen Rothwell 
138614cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
138714cf11afSPaul Mackerras /*
138814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
138914cf11afSPaul Mackerras  * spins until a reboot occurs
139014cf11afSPaul Mackerras  */
139114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
139214cf11afSPaul Mackerras {
139314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
139414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
139514cf11afSPaul Mackerras 	return;
139614cf11afSPaul Mackerras }
139714cf11afSPaul Mackerras 
139814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
139914cf11afSPaul Mackerras {
140014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
140114cf11afSPaul Mackerras 	WatchdogHandler(regs);
140214cf11afSPaul Mackerras }
140314cf11afSPaul Mackerras #endif
1404dc1c1ca3SStephen Rothwell 
1405dc1c1ca3SStephen Rothwell /*
1406dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1407dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1408dc1c1ca3SStephen Rothwell  */
1409dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1410dc1c1ca3SStephen Rothwell {
1411dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1412dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1413dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1414dc1c1ca3SStephen Rothwell }
141514cf11afSPaul Mackerras 
141614cf11afSPaul Mackerras void __init trap_init(void)
141714cf11afSPaul Mackerras {
141814cf11afSPaul Mackerras }
141980947e7cSGeert Uytterhoeven 
142080947e7cSGeert Uytterhoeven 
142180947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
142280947e7cSGeert Uytterhoeven 
142380947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
142480947e7cSGeert Uytterhoeven 
142580947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
142680947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
142780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
142880947e7cSGeert Uytterhoeven #endif
142980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
143080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
143180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
143280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
143380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
143480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
143580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
143680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
143780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
143880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
143980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
144080947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
144180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
144280947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
144380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
144480947e7cSGeert Uytterhoeven #endif
144580947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
144680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
144780947e7cSGeert Uytterhoeven #endif
144880947e7cSGeert Uytterhoeven };
144980947e7cSGeert Uytterhoeven 
145080947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
145180947e7cSGeert Uytterhoeven 
145280947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
145380947e7cSGeert Uytterhoeven {
145480947e7cSGeert Uytterhoeven 	if (printk_ratelimit())
145580947e7cSGeert Uytterhoeven 		pr_warning("%s used emulated %s instruction\n", current->comm,
145680947e7cSGeert Uytterhoeven 			   type);
145780947e7cSGeert Uytterhoeven }
145880947e7cSGeert Uytterhoeven 
145980947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
146080947e7cSGeert Uytterhoeven {
146180947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
146280947e7cSGeert Uytterhoeven 	unsigned int i;
146380947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
146480947e7cSGeert Uytterhoeven 
146580947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
146680947e7cSGeert Uytterhoeven 		return -ENODEV;
146780947e7cSGeert Uytterhoeven 
146880947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
146980947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
147080947e7cSGeert Uytterhoeven 	if (!dir)
147180947e7cSGeert Uytterhoeven 		return -ENOMEM;
147280947e7cSGeert Uytterhoeven 
147380947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
147480947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
147580947e7cSGeert Uytterhoeven 	if (!d)
147680947e7cSGeert Uytterhoeven 		goto fail;
147780947e7cSGeert Uytterhoeven 
147880947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
147980947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
148080947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
148180947e7cSGeert Uytterhoeven 		if (!d)
148280947e7cSGeert Uytterhoeven 			goto fail;
148380947e7cSGeert Uytterhoeven 	}
148480947e7cSGeert Uytterhoeven 
148580947e7cSGeert Uytterhoeven 	return 0;
148680947e7cSGeert Uytterhoeven 
148780947e7cSGeert Uytterhoeven fail:
148880947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
148980947e7cSGeert Uytterhoeven 	return -ENOMEM;
149080947e7cSGeert Uytterhoeven }
149180947e7cSGeert Uytterhoeven 
149280947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
149380947e7cSGeert Uytterhoeven 
149480947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1495