xref: /linux/arch/powerpc/kernel/traps.c (revision 4e0e3435b50285eafe5898124ce02f7577f6803a)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
614cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
714cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
814cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1114cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras /*
1514cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
2014cf11afSPaul Mackerras #include <linux/kernel.h>
2114cf11afSPaul Mackerras #include <linux/mm.h>
2214cf11afSPaul Mackerras #include <linux/stddef.h>
2314cf11afSPaul Mackerras #include <linux/unistd.h>
248dad3f92SPaul Mackerras #include <linux/ptrace.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/init.h>
2814cf11afSPaul Mackerras #include <linux/module.h>
298dad3f92SPaul Mackerras #include <linux/prctl.h>
3014cf11afSPaul Mackerras #include <linux/delay.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
32cc532915SMichael Ellerman #include <linux/kexec.h>
335474c120SMichael Hanselmann #include <linux/backlight.h>
3473c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
351eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3680947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3776462232SChristian Dietrich #include <linux/ratelimit.h>
38ba12eedeSLi Zhong #include <linux/context_tracking.h>
3914cf11afSPaul Mackerras 
4080947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4114cf11afSPaul Mackerras #include <asm/pgtable.h>
4214cf11afSPaul Mackerras #include <asm/uaccess.h>
4314cf11afSPaul Mackerras #include <asm/io.h>
4486417780SPaul Mackerras #include <asm/machdep.h>
4586417780SPaul Mackerras #include <asm/rtas.h>
46f7f6f4feSDavid Gibson #include <asm/pmc.h>
47dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4814cf11afSPaul Mackerras #include <asm/reg.h>
4986417780SPaul Mackerras #endif
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
566ce6c629SMichael Neuling #include <asm/tm.h>
57dc1c1ca3SStephen Rothwell #endif
58c0ce7d08SDavid Wilder #include <asm/kexec.h>
5916c57b36SKumar Gala #include <asm/ppc-opcode.h>
60cce1f106SShaohui Xie #include <asm/rio.h>
61ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
62ae3a197eSDavid Howells #include <asm/switch_to.h>
63f54db641SMichael Neuling #include <asm/tm.h>
64ae3a197eSDavid Howells #include <asm/debug.h>
65*4e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
66dc1c1ca3SStephen Rothwell 
677dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
685be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
695be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
705be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
715be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
725be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
739422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
745be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7514cf11afSPaul Mackerras 
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
819422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8314cf11afSPaul Mackerras #endif
8414cf11afSPaul Mackerras 
858b3c34cfSMichael Neuling /* Transactional Memory trap debug */
868b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
878b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
888b3c34cfSMichael Neuling #else
898b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
908b3c34cfSMichael Neuling #endif
918b3c34cfSMichael Neuling 
9214cf11afSPaul Mackerras /*
9314cf11afSPaul Mackerras  * Trap & Exception support
9414cf11afSPaul Mackerras  */
9514cf11afSPaul Mackerras 
966031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
976031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
986031d9d9Santon@samba.org {
996031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1006031d9d9Santon@samba.org 	if (pmac_backlight) {
1016031d9d9Santon@samba.org 		struct backlight_properties *props;
1026031d9d9Santon@samba.org 
1036031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1046031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1056031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1066031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1076031d9d9Santon@samba.org 	}
1086031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1096031d9d9Santon@samba.org }
1106031d9d9Santon@samba.org #else
1116031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1126031d9d9Santon@samba.org #endif
1136031d9d9Santon@samba.org 
114760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
115760ca4dcSAnton Blanchard static int die_owner = -1;
116760ca4dcSAnton Blanchard static unsigned int die_nest_count;
117c0ce7d08SDavid Wilder static int die_counter;
118760ca4dcSAnton Blanchard 
119760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
120760ca4dcSAnton Blanchard {
121760ca4dcSAnton Blanchard 	int cpu;
12234c2a14fSanton@samba.org 	unsigned long flags;
12314cf11afSPaul Mackerras 
12414cf11afSPaul Mackerras 	if (debugger(regs))
12514cf11afSPaul Mackerras 		return 1;
12614cf11afSPaul Mackerras 
127293e4688Santon@samba.org 	oops_enter();
128293e4688Santon@samba.org 
129760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
130760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
131760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
132760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
133760ca4dcSAnton Blanchard 		if (cpu == die_owner)
134760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
135760ca4dcSAnton Blanchard 		else
136760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
137760ca4dcSAnton Blanchard 	}
138760ca4dcSAnton Blanchard 	die_nest_count++;
139760ca4dcSAnton Blanchard 	die_owner = cpu;
14014cf11afSPaul Mackerras 	console_verbose();
14114cf11afSPaul Mackerras 	bust_spinlocks(1);
1426031d9d9Santon@samba.org 	if (machine_is(powermac))
1436031d9d9Santon@samba.org 		pmac_backlight_unblank();
144760ca4dcSAnton Blanchard 	return flags;
14534c2a14fSanton@samba.org }
1465474c120SMichael Hanselmann 
147760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
148760ca4dcSAnton Blanchard 			       int signr)
149760ca4dcSAnton Blanchard {
15014cf11afSPaul Mackerras 	bust_spinlocks(0);
151760ca4dcSAnton Blanchard 	die_owner = -1;
152373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
153760ca4dcSAnton Blanchard 	die_nest_count--;
15458154c8cSAnton Blanchard 	oops_exit();
15558154c8cSAnton Blanchard 	printk("\n");
156760ca4dcSAnton Blanchard 	if (!die_nest_count)
157760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
158760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
159760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
160cc532915SMichael Ellerman 
161ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
162ebaeb5aeSMahesh Salgaonkar 
1639b00ac06SAnton Blanchard 	/*
1649b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1659b00ac06SAnton Blanchard 	 * it through the crashdump code.
1669b00ac06SAnton Blanchard 	 */
1679b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
168cc532915SMichael Ellerman 		crash_kexec(regs);
1699b00ac06SAnton Blanchard 
1709b00ac06SAnton Blanchard 		/*
1719b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1729b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1739b00ac06SAnton Blanchard 		 * code.
1749b00ac06SAnton Blanchard 		 */
175c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1769b00ac06SAnton Blanchard 	}
17714cf11afSPaul Mackerras 
178760ca4dcSAnton Blanchard 	if (!signr)
179760ca4dcSAnton Blanchard 		return;
180760ca4dcSAnton Blanchard 
18158154c8cSAnton Blanchard 	/*
18258154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
18358154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18458154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18558154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18658154c8cSAnton Blanchard 	 */
18758154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
18858154c8cSAnton Blanchard 	    is_global_init(current)) {
18958154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
19058154c8cSAnton Blanchard 	}
19158154c8cSAnton Blanchard 
19214cf11afSPaul Mackerras 	if (in_interrupt())
19314cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
194cea6a4baSHorms 	if (panic_on_oops)
195012c437dSHorms 		panic("Fatal exception");
196760ca4dcSAnton Blanchard 	do_exit(signr);
197760ca4dcSAnton Blanchard }
198cea6a4baSHorms 
199760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
200760ca4dcSAnton Blanchard {
201760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
202760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
203760ca4dcSAnton Blanchard 	printk("PREEMPT ");
204760ca4dcSAnton Blanchard #endif
205760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
206760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
207760ca4dcSAnton Blanchard #endif
208760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
209760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
210760ca4dcSAnton Blanchard #endif
211760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
212760ca4dcSAnton Blanchard 	printk("NUMA ");
213760ca4dcSAnton Blanchard #endif
214760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
215760ca4dcSAnton Blanchard 
216760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
217760ca4dcSAnton Blanchard 		return 1;
218760ca4dcSAnton Blanchard 
219760ca4dcSAnton Blanchard 	print_modules();
220760ca4dcSAnton Blanchard 	show_regs(regs);
22114cf11afSPaul Mackerras 
22214cf11afSPaul Mackerras 	return 0;
22314cf11afSPaul Mackerras }
22414cf11afSPaul Mackerras 
225760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
226760ca4dcSAnton Blanchard {
227760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
228760ca4dcSAnton Blanchard 
229760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
230760ca4dcSAnton Blanchard 		err = 0;
231760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
232760ca4dcSAnton Blanchard }
233760ca4dcSAnton Blanchard 
23425baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23525baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23625baa35bSOleg Nesterov {
23725baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
23825baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
23925baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
24025baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
24125baa35bSOleg Nesterov }
24225baa35bSOleg Nesterov 
24314cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24414cf11afSPaul Mackerras {
24514cf11afSPaul Mackerras 	siginfo_t info;
246d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
247d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
248d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
249d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
25014cf11afSPaul Mackerras 
25114cf11afSPaul Mackerras 	if (!user_mode(regs)) {
252760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
25314cf11afSPaul Mackerras 		return;
254760ca4dcSAnton Blanchard 	}
255760ca4dcSAnton Blanchard 
256760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
25776462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
258d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
259d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
26014cf11afSPaul Mackerras 	}
26114cf11afSPaul Mackerras 
262a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2639f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2649f2f79e3SBenjamin Herrenschmidt 
26541ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26614cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
26714cf11afSPaul Mackerras 	info.si_signo = signr;
26814cf11afSPaul Mackerras 	info.si_code = code;
26914cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
27014cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
27114cf11afSPaul Mackerras }
27214cf11afSPaul Mackerras 
27314cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27414cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27514cf11afSPaul Mackerras {
27614cf11afSPaul Mackerras 	/* See if any machine dependent calls */
277c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
278c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
279c902be71SArnd Bergmann 			return;
280c902be71SArnd Bergmann 	}
28114cf11afSPaul Mackerras 
2828dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
28314cf11afSPaul Mackerras 
28414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28614cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
28714cf11afSPaul Mackerras 
28814cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
28914cf11afSPaul Mackerras }
29014cf11afSPaul Mackerras #endif
29114cf11afSPaul Mackerras 
29214cf11afSPaul Mackerras /*
29314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
29414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
29514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
29614cf11afSPaul Mackerras  * table.
29714cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
29814cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
29914cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
30014cf11afSPaul Mackerras  *  -- paulus.
30114cf11afSPaul Mackerras  */
30214cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
30314cf11afSPaul Mackerras {
30468a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
30514cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
30614cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
30714cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
30814cf11afSPaul Mackerras 
30914cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
31014cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
31114cf11afSPaul Mackerras 		/*
31214cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
31314cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
31414cf11afSPaul Mackerras 		 * As the address is in the exception table
31514cf11afSPaul Mackerras 		 * we should be able to read the instr there.
31614cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
31714cf11afSPaul Mackerras 		 * load or store.
31814cf11afSPaul Mackerras 		 */
31914cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
32014cf11afSPaul Mackerras 			nip -= 2;
32114cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
32214cf11afSPaul Mackerras 			--nip;
32314cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
32414cf11afSPaul Mackerras 			/* sync or twi */
32514cf11afSPaul Mackerras 			unsigned int rb;
32614cf11afSPaul Mackerras 
32714cf11afSPaul Mackerras 			--nip;
32814cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
32914cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
33014cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
33114cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
33214cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
33314cf11afSPaul Mackerras 			regs->nip = entry->fixup;
33414cf11afSPaul Mackerras 			return 1;
33514cf11afSPaul Mackerras 		}
33614cf11afSPaul Mackerras 	}
33768a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
33814cf11afSPaul Mackerras 	return 0;
33914cf11afSPaul Mackerras }
34014cf11afSPaul Mackerras 
341172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
34214cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
34314cf11afSPaul Mackerras    is in the ESR. */
34414cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
34514cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
34614cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
34714cf11afSPaul Mackerras #else
348fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
34914cf11afSPaul Mackerras #endif
35014cf11afSPaul Mackerras #define REASON_FP		ESR_FP
35114cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
35214cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
35314cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
35414cf11afSPaul Mackerras 
35514cf11afSPaul Mackerras /* single-step stuff */
35614cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
35714cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
35814cf11afSPaul Mackerras 
35914cf11afSPaul Mackerras #else
36014cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
36114cf11afSPaul Mackerras    exception is in the MSR. */
36214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
36314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3648b3c34cfSMichael Neuling #define REASON_TM		0x200000
36514cf11afSPaul Mackerras #define REASON_FP		0x100000
36614cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
36714cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
36814cf11afSPaul Mackerras #define REASON_TRAP		0x20000
36914cf11afSPaul Mackerras 
37014cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
37114cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
37214cf11afSPaul Mackerras #endif
37314cf11afSPaul Mackerras 
37447c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
37547c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
37614cf11afSPaul Mackerras {
3771a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
37814cf11afSPaul Mackerras 
37914cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
38014cf11afSPaul Mackerras 		printk("Instruction");
38114cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
38214cf11afSPaul Mackerras 	} else
38314cf11afSPaul Mackerras 		printk("Data");
38414cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
38547c0bd1aSBenjamin Herrenschmidt 
38647c0bd1aSBenjamin Herrenschmidt 	return 0;
38747c0bd1aSBenjamin Herrenschmidt }
38847c0bd1aSBenjamin Herrenschmidt 
38947c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
39047c0bd1aSBenjamin Herrenschmidt {
39147c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
39247c0bd1aSBenjamin Herrenschmidt 
39314cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
39414cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
39514cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
39614cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
39714cf11afSPaul Mackerras 	}
39814cf11afSPaul Mackerras 	else {
39914cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
40014cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
40114cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
40214cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
40314cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
40414cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
40514cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
40614cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
40714cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
40814cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
40914cf11afSPaul Mackerras 			flush_instruction_cache();
41014cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
41114cf11afSPaul Mackerras 		}
41214cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
41314cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
41414cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
41514cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
41614cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
41714cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
41814cf11afSPaul Mackerras 
41914cf11afSPaul Mackerras 		/* Clear MCSR */
42014cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
42114cf11afSPaul Mackerras 	}
42247c0bd1aSBenjamin Herrenschmidt 	return 0;
42347c0bd1aSBenjamin Herrenschmidt }
424fc5e7097SDave Kleikamp 
425fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
426fc5e7097SDave Kleikamp {
427fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
428fc5e7097SDave Kleikamp 	u32 mcsr;
429fc5e7097SDave Kleikamp 
430fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
431fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
432fc5e7097SDave Kleikamp 		printk(KERN_ERR
433fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
434fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
435fc5e7097SDave Kleikamp 		return 0;
436fc5e7097SDave Kleikamp 	}
437fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
438fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
439fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
440fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
441fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
442fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
443fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
444fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
445fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
446fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
447fc5e7097SDave Kleikamp 		flush_instruction_cache();
448fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
449fc5e7097SDave Kleikamp 	}
450fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
451fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
452fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
453fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
454fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
455fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
456fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
457fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
458fc5e7097SDave Kleikamp 
459fc5e7097SDave Kleikamp 	/* Clear MCSR */
460fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
461fc5e7097SDave Kleikamp 
462fc5e7097SDave Kleikamp 	return 0;
463fc5e7097SDave Kleikamp }
46414cf11afSPaul Mackerras #elif defined(CONFIG_E500)
465fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
466fe04b112SScott Wood {
467fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
468fe04b112SScott Wood 	unsigned long reason = mcsr;
469fe04b112SScott Wood 	int recoverable = 1;
470fe04b112SScott Wood 
47182a9a480SScott Wood 	if (reason & MCSR_LD) {
472cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
473cce1f106SShaohui Xie 		if (recoverable == 1)
474cce1f106SShaohui Xie 			goto silent_out;
475cce1f106SShaohui Xie 	}
476cce1f106SShaohui Xie 
477fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
478fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
479fe04b112SScott Wood 
480fe04b112SScott Wood 	if (reason & MCSR_MCP)
481fe04b112SScott Wood 		printk("Machine Check Signal\n");
482fe04b112SScott Wood 
483fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
484fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
485fe04b112SScott Wood 
486fe04b112SScott Wood 		/*
487fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
488fe04b112SScott Wood 		 */
489fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
490fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
491fe04b112SScott Wood 			;
492fe04b112SScott Wood 
493fe04b112SScott Wood 		/*
494fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
495fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
496fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
497fe04b112SScott Wood 		 */
498fe04b112SScott Wood 		reason &= ~MCSR_IF;
499fe04b112SScott Wood 	}
500fe04b112SScott Wood 
501fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
502fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
50337caf9f2SKumar Gala 
50437caf9f2SKumar Gala 		/*
50537caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
50637caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
50737caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
50837caf9f2SKumar Gala 		 */
50937caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
510fe04b112SScott Wood 			recoverable = 0;
511fe04b112SScott Wood 	}
512fe04b112SScott Wood 
513fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
514fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
515fe04b112SScott Wood 		recoverable = 0;
516fe04b112SScott Wood 	}
517fe04b112SScott Wood 
518fe04b112SScott Wood 	if (reason & MCSR_NMI)
519fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
520fe04b112SScott Wood 
521fe04b112SScott Wood 	if (reason & MCSR_IF) {
522fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
523fe04b112SScott Wood 		recoverable = 0;
524fe04b112SScott Wood 	}
525fe04b112SScott Wood 
526fe04b112SScott Wood 	if (reason & MCSR_LD) {
527fe04b112SScott Wood 		printk("Load Error Report\n");
528fe04b112SScott Wood 		recoverable = 0;
529fe04b112SScott Wood 	}
530fe04b112SScott Wood 
531fe04b112SScott Wood 	if (reason & MCSR_ST) {
532fe04b112SScott Wood 		printk("Store Error Report\n");
533fe04b112SScott Wood 		recoverable = 0;
534fe04b112SScott Wood 	}
535fe04b112SScott Wood 
536fe04b112SScott Wood 	if (reason & MCSR_LDG) {
537fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
538fe04b112SScott Wood 		recoverable = 0;
539fe04b112SScott Wood 	}
540fe04b112SScott Wood 
541fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
542fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
543fe04b112SScott Wood 
544fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
545fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
546fe04b112SScott Wood 		recoverable = 0;
547fe04b112SScott Wood 	}
548fe04b112SScott Wood 
549fe04b112SScott Wood 	if (reason & MCSR_MAV) {
550fe04b112SScott Wood 		u64 addr;
551fe04b112SScott Wood 
552fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
553fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
554fe04b112SScott Wood 
555fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
556fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
557fe04b112SScott Wood 	}
558fe04b112SScott Wood 
559cce1f106SShaohui Xie silent_out:
560fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
561fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
562fe04b112SScott Wood }
563fe04b112SScott Wood 
56447c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
56547c0bd1aSBenjamin Herrenschmidt {
56647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
56747c0bd1aSBenjamin Herrenschmidt 
568cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
569cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
570cce1f106SShaohui Xie 			return 1;
571*4e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
572*4e0e3435SHongtao Jia 			return 1;
573cce1f106SShaohui Xie 	}
574cce1f106SShaohui Xie 
57514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57614cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
57714cf11afSPaul Mackerras 
57814cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
57914cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
58014cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
58114cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
58214cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
58314cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
58414cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
58514cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
58614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
58714cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
58814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
58914cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
59014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
59114cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
59214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
59314cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
59414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
59514cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
59714cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
59914cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
60014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
60114cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
60247c0bd1aSBenjamin Herrenschmidt 
60347c0bd1aSBenjamin Herrenschmidt 	return 0;
60447c0bd1aSBenjamin Herrenschmidt }
6054490c06bSKumar Gala 
6064490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6074490c06bSKumar Gala {
6084490c06bSKumar Gala 	return 0;
6094490c06bSKumar Gala }
61014cf11afSPaul Mackerras #elif defined(CONFIG_E200)
61147c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
61247c0bd1aSBenjamin Herrenschmidt {
61347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
61447c0bd1aSBenjamin Herrenschmidt 
61514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61614cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
61714cf11afSPaul Mackerras 
61814cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
61914cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
62014cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
62114cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
62214cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
62314cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
62414cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
62514cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
62614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
62714cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
62814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
62914cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
63014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
63114cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
63247c0bd1aSBenjamin Herrenschmidt 
63347c0bd1aSBenjamin Herrenschmidt 	return 0;
63447c0bd1aSBenjamin Herrenschmidt }
63547c0bd1aSBenjamin Herrenschmidt #else
63647c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
63747c0bd1aSBenjamin Herrenschmidt {
63847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
63947c0bd1aSBenjamin Herrenschmidt 
64014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
64114cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
64214cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
64314cf11afSPaul Mackerras 	case 0x80000:
64414cf11afSPaul Mackerras 		printk("Machine check signal\n");
64514cf11afSPaul Mackerras 		break;
64614cf11afSPaul Mackerras 	case 0:		/* for 601 */
64714cf11afSPaul Mackerras 	case 0x40000:
64814cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
64914cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
65014cf11afSPaul Mackerras 		break;
65114cf11afSPaul Mackerras 	case 0x20000:
65214cf11afSPaul Mackerras 		printk("Data parity error signal\n");
65314cf11afSPaul Mackerras 		break;
65414cf11afSPaul Mackerras 	case 0x10000:
65514cf11afSPaul Mackerras 		printk("Address parity error signal\n");
65614cf11afSPaul Mackerras 		break;
65714cf11afSPaul Mackerras 	case 0x20000000:
65814cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
65914cf11afSPaul Mackerras 		break;
66014cf11afSPaul Mackerras 	case 0x40000000:
66114cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
66214cf11afSPaul Mackerras 		break;
66314cf11afSPaul Mackerras 	case 0x00100000:
66414cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
66514cf11afSPaul Mackerras 		break;
66614cf11afSPaul Mackerras 	default:
66714cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
66814cf11afSPaul Mackerras 	}
66975918a4bSOlof Johansson 	return 0;
67075918a4bSOlof Johansson }
67147c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
67275918a4bSOlof Johansson 
67375918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
67475918a4bSOlof Johansson {
675ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
67675918a4bSOlof Johansson 	int recover = 0;
67775918a4bSOlof Johansson 
67889713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
67989713ed1SAnton Blanchard 
68047c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
68147c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
68247c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
68347c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
68447c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
68547c0bd1aSBenjamin Herrenschmidt 	 */
68675918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
68775918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
68847c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
68947c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
69075918a4bSOlof Johansson 
69147c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
692ba12eedeSLi Zhong 		goto bail;
69375918a4bSOlof Johansson 
69475918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
69547c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
69647c0bd1aSBenjamin Herrenschmidt 	 *
69747c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
69847c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
69947c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
70047c0bd1aSBenjamin Herrenschmidt 	 */
70175918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
702ba12eedeSLi Zhong 	goto bail;
70375918a4bSOlof Johansson #endif
70475918a4bSOlof Johansson 
705a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
706ba12eedeSLi Zhong 		goto bail;
70775918a4bSOlof Johansson 
70875918a4bSOlof Johansson 	if (check_io_access(regs))
709ba12eedeSLi Zhong 		goto bail;
71075918a4bSOlof Johansson 
7118dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
71214cf11afSPaul Mackerras 
71314cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
71414cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
71514cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
716ba12eedeSLi Zhong 
717ba12eedeSLi Zhong bail:
718ba12eedeSLi Zhong 	exception_exit(prev_state);
71914cf11afSPaul Mackerras }
72014cf11afSPaul Mackerras 
72114cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
72214cf11afSPaul Mackerras {
72314cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
72414cf11afSPaul Mackerras }
72514cf11afSPaul Mackerras 
726dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
72714cf11afSPaul Mackerras {
728ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
729ba12eedeSLi Zhong 
73014cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
73114cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
73214cf11afSPaul Mackerras 
73314cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
734ba12eedeSLi Zhong 
735ba12eedeSLi Zhong 	exception_exit(prev_state);
73614cf11afSPaul Mackerras }
73714cf11afSPaul Mackerras 
738dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
73914cf11afSPaul Mackerras {
740ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
741ba12eedeSLi Zhong 
74214cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
74314cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
744ba12eedeSLi Zhong 		goto bail;
74514cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
746ba12eedeSLi Zhong 		goto bail;
74714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
748ba12eedeSLi Zhong 
749ba12eedeSLi Zhong bail:
750ba12eedeSLi Zhong 	exception_exit(prev_state);
75114cf11afSPaul Mackerras }
75214cf11afSPaul Mackerras 
75314cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
75414cf11afSPaul Mackerras {
75514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
75614cf11afSPaul Mackerras }
75714cf11afSPaul Mackerras 
7588dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
75914cf11afSPaul Mackerras {
760ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
761ba12eedeSLi Zhong 
7622538c2d0SK.Prasad 	clear_single_step(regs);
76314cf11afSPaul Mackerras 
76414cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
76514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
766ba12eedeSLi Zhong 		goto bail;
76714cf11afSPaul Mackerras 	if (debugger_sstep(regs))
768ba12eedeSLi Zhong 		goto bail;
76914cf11afSPaul Mackerras 
77014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
771ba12eedeSLi Zhong 
772ba12eedeSLi Zhong bail:
773ba12eedeSLi Zhong 	exception_exit(prev_state);
77414cf11afSPaul Mackerras }
77514cf11afSPaul Mackerras 
77614cf11afSPaul Mackerras /*
77714cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
77814cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
77914cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
78014cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
78114cf11afSPaul Mackerras  */
7828dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
78314cf11afSPaul Mackerras {
7842538c2d0SK.Prasad 	if (single_stepping(regs))
7852538c2d0SK.Prasad 		single_step_exception(regs);
78614cf11afSPaul Mackerras }
78714cf11afSPaul Mackerras 
7885fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
789dc1c1ca3SStephen Rothwell {
7905fad293bSKumar Gala 	int ret = 0;
791dc1c1ca3SStephen Rothwell 
792dc1c1ca3SStephen Rothwell 	/* Invalid operation */
793dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7945fad293bSKumar Gala 		ret = FPE_FLTINV;
795dc1c1ca3SStephen Rothwell 
796dc1c1ca3SStephen Rothwell 	/* Overflow */
797dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7985fad293bSKumar Gala 		ret = FPE_FLTOVF;
799dc1c1ca3SStephen Rothwell 
800dc1c1ca3SStephen Rothwell 	/* Underflow */
801dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
8025fad293bSKumar Gala 		ret = FPE_FLTUND;
803dc1c1ca3SStephen Rothwell 
804dc1c1ca3SStephen Rothwell 	/* Divide by zero */
805dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
8065fad293bSKumar Gala 		ret = FPE_FLTDIV;
807dc1c1ca3SStephen Rothwell 
808dc1c1ca3SStephen Rothwell 	/* Inexact result */
809dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
8105fad293bSKumar Gala 		ret = FPE_FLTRES;
8115fad293bSKumar Gala 
8125fad293bSKumar Gala 	return ret;
8135fad293bSKumar Gala }
8145fad293bSKumar Gala 
8155fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8165fad293bSKumar Gala {
8175fad293bSKumar Gala 	int code = 0;
8185fad293bSKumar Gala 
8195fad293bSKumar Gala 	flush_fp_to_thread(current);
8205fad293bSKumar Gala 
8215fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
822dc1c1ca3SStephen Rothwell 
823dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
824dc1c1ca3SStephen Rothwell }
825dc1c1ca3SStephen Rothwell 
826dc1c1ca3SStephen Rothwell /*
827dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
82814cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
82914cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
83014cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
83114cf11afSPaul Mackerras  *
83214cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
83314cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
83414cf11afSPaul Mackerras  * bits is faster and easier.
83586417780SPaul Mackerras  *
83614cf11afSPaul Mackerras  */
83714cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
83814cf11afSPaul Mackerras {
83914cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
84014cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
84114cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
84214cf11afSPaul Mackerras 	u32 num_bytes;
84314cf11afSPaul Mackerras 	unsigned long EA;
84414cf11afSPaul Mackerras 	int pos = 0;
84514cf11afSPaul Mackerras 
84614cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
84716c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
84814cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
84914cf11afSPaul Mackerras 			return -EINVAL;
85014cf11afSPaul Mackerras 
85114cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
85214cf11afSPaul Mackerras 
85316c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
85416c57b36SKumar Gala 		case PPC_INST_LSWX:
85516c57b36SKumar Gala 		case PPC_INST_STSWX:
85614cf11afSPaul Mackerras 			EA += NB_RB;
85714cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
85814cf11afSPaul Mackerras 			break;
85916c57b36SKumar Gala 		case PPC_INST_LSWI:
86016c57b36SKumar Gala 		case PPC_INST_STSWI:
86114cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
86214cf11afSPaul Mackerras 			break;
86314cf11afSPaul Mackerras 		default:
86414cf11afSPaul Mackerras 			return -EINVAL;
86514cf11afSPaul Mackerras 	}
86614cf11afSPaul Mackerras 
86714cf11afSPaul Mackerras 	while (num_bytes != 0)
86814cf11afSPaul Mackerras 	{
86914cf11afSPaul Mackerras 		u8 val;
87014cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
87114cf11afSPaul Mackerras 
87280aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
87380aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
87480aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
87580aa0fb4SJames Yang 
87616c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
87716c57b36SKumar Gala 			case PPC_INST_LSWX:
87816c57b36SKumar Gala 			case PPC_INST_LSWI:
87914cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
88014cf11afSPaul Mackerras 					return -EFAULT;
88114cf11afSPaul Mackerras 				/* first time updating this reg,
88214cf11afSPaul Mackerras 				 * zero it out */
88314cf11afSPaul Mackerras 				if (pos == 0)
88414cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
88514cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
88614cf11afSPaul Mackerras 				break;
88716c57b36SKumar Gala 			case PPC_INST_STSWI:
88816c57b36SKumar Gala 			case PPC_INST_STSWX:
88914cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
89014cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
89114cf11afSPaul Mackerras 					return -EFAULT;
89214cf11afSPaul Mackerras 				break;
89314cf11afSPaul Mackerras 		}
89414cf11afSPaul Mackerras 		/* move EA to next address */
89514cf11afSPaul Mackerras 		EA += 1;
89614cf11afSPaul Mackerras 		num_bytes--;
89714cf11afSPaul Mackerras 
89814cf11afSPaul Mackerras 		/* manage our position within the register */
89914cf11afSPaul Mackerras 		if (++pos == 4) {
90014cf11afSPaul Mackerras 			pos = 0;
90114cf11afSPaul Mackerras 			if (++rT == 32)
90214cf11afSPaul Mackerras 				rT = 0;
90314cf11afSPaul Mackerras 		}
90414cf11afSPaul Mackerras 	}
90514cf11afSPaul Mackerras 
90614cf11afSPaul Mackerras 	return 0;
90714cf11afSPaul Mackerras }
90814cf11afSPaul Mackerras 
909c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
910c3412dcbSWill Schmidt {
911c3412dcbSWill Schmidt 	u32 ra,rs;
912c3412dcbSWill Schmidt 	unsigned long tmp;
913c3412dcbSWill Schmidt 
914c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
915c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
916c3412dcbSWill Schmidt 
917c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
918c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
919c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
920c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
921c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
922c3412dcbSWill Schmidt 
923c3412dcbSWill Schmidt 	return 0;
924c3412dcbSWill Schmidt }
925c3412dcbSWill Schmidt 
926c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
927c1469f13SKumar Gala {
928c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
929c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
930c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
931c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
932c1469f13SKumar Gala 	u8 bit;
933c1469f13SKumar Gala 	unsigned long tmp;
934c1469f13SKumar Gala 
935c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
936c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
937c1469f13SKumar Gala 
938c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
939c1469f13SKumar Gala 
940c1469f13SKumar Gala 	return 0;
941c1469f13SKumar Gala }
942c1469f13SKumar Gala 
9436ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9446ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9456ce6c629SMichael Neuling {
9466ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9476ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9486ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9496ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
9506ce6c629SMichael Neuling 	 */
9516ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
9526ce6c629SMichael Neuling 		tm_enable();
9536ce6c629SMichael Neuling 		tm_abort(cause);
9546ce6c629SMichael Neuling 		return true;
9556ce6c629SMichael Neuling 	}
9566ce6c629SMichael Neuling 	return false;
9576ce6c629SMichael Neuling }
9586ce6c629SMichael Neuling #else
9596ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
9606ce6c629SMichael Neuling {
9616ce6c629SMichael Neuling 	return false;
9626ce6c629SMichael Neuling }
9636ce6c629SMichael Neuling #endif
9646ce6c629SMichael Neuling 
96514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
96614cf11afSPaul Mackerras {
96714cf11afSPaul Mackerras 	u32 instword;
96814cf11afSPaul Mackerras 	u32 rd;
96914cf11afSPaul Mackerras 
970fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
97114cf11afSPaul Mackerras 		return -EINVAL;
97214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
97314cf11afSPaul Mackerras 
97414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
97514cf11afSPaul Mackerras 		return -EFAULT;
97614cf11afSPaul Mackerras 
97714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
97816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
979eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
98014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
98114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
98214cf11afSPaul Mackerras 		return 0;
98314cf11afSPaul Mackerras 	}
98414cf11afSPaul Mackerras 
98514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
98680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
987eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
98814cf11afSPaul Mackerras 		return 0;
98980947e7cSGeert Uytterhoeven 	}
99014cf11afSPaul Mackerras 
99114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
99216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
99386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
99414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
99514cf11afSPaul Mackerras 
996eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
99714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
99814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
99914cf11afSPaul Mackerras 		return 0;
100014cf11afSPaul Mackerras 	}
100114cf11afSPaul Mackerras 
100214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
100380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
10046ce6c629SMichael Neuling 		if (tm_abort_check(regs,
10056ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
10066ce6c629SMichael Neuling 			return -EINVAL;
1007eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
100814cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
100980947e7cSGeert Uytterhoeven 	}
101014cf11afSPaul Mackerras 
1011c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
101216c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1013eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1014c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1015c3412dcbSWill Schmidt 	}
1016c3412dcbSWill Schmidt 
1017c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
101816c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1019eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1020c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1021c1469f13SKumar Gala 	}
1022c1469f13SKumar Gala 
1023efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1024efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
102573d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
102673d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
102773d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
102873d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1029efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1030efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1031efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1032efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1033efcac658SAlexey Kardashevskiy 		return 0;
1034efcac658SAlexey Kardashevskiy 	}
1035efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
103673d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
103773d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
103873d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
103973d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1040efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1041efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1042efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
104300ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1044efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
104500ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1046efcac658SAlexey Kardashevskiy 		return 0;
1047efcac658SAlexey Kardashevskiy 	}
1048efcac658SAlexey Kardashevskiy #endif
1049efcac658SAlexey Kardashevskiy 
105014cf11afSPaul Mackerras 	return -EINVAL;
105114cf11afSPaul Mackerras }
105214cf11afSPaul Mackerras 
105373c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
105414cf11afSPaul Mackerras {
105573c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
105614cf11afSPaul Mackerras }
105714cf11afSPaul Mackerras 
10588dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
105914cf11afSPaul Mackerras {
1060ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
106114cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
106214cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
106314cf11afSPaul Mackerras 
1064aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
106504903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
106614cf11afSPaul Mackerras 
106714cf11afSPaul Mackerras 	if (reason & REASON_FP) {
106814cf11afSPaul Mackerras 		/* IEEE FP exception */
1069dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1070ba12eedeSLi Zhong 		goto bail;
10718dad3f92SPaul Mackerras 	}
10728dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1073ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1074ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1075ba797b28SJason Wessel 		if (debugger_bpt(regs))
1076ba12eedeSLi Zhong 			goto bail;
1077ba797b28SJason Wessel 
107814cf11afSPaul Mackerras 		/* trap exception */
1079dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1080dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1081ba12eedeSLi Zhong 			goto bail;
108273c9ceabSJeremy Fitzhardinge 
108373c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1084608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
108514cf11afSPaul Mackerras 			regs->nip += 4;
1086ba12eedeSLi Zhong 			goto bail;
108714cf11afSPaul Mackerras 		}
10888dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1089ba12eedeSLi Zhong 		goto bail;
10908dad3f92SPaul Mackerras 	}
1091bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1092bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1093bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1094bc2a9408SMichael Neuling 		 * This occurs when:
1095bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1096bc2a9408SMichael Neuling 		 *    transition in TM states.
1097bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1098bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1099bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1100bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1101bc2a9408SMichael Neuling 		 */
1102bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1103bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1104bc2a9408SMichael Neuling 			regs->nip += 4;
1105ba12eedeSLi Zhong 			goto bail;
1106bc2a9408SMichael Neuling 		}
1107bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1108bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1109bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1110bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1111bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1112bc2a9408SMichael Neuling 		 */
1113bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1114bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1115ba12eedeSLi Zhong 			goto bail;
1116bc2a9408SMichael Neuling 		} else {
1117bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1118bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1119bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1120bc2a9408SMichael Neuling 		}
1121bc2a9408SMichael Neuling 	}
1122bc2a9408SMichael Neuling #endif
11238dad3f92SPaul Mackerras 
1124a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1125a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1126cd8a5673SPaul Mackerras 		local_irq_enable();
1127cd8a5673SPaul Mackerras 
112804903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
112904903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
113004903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
113104903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
113204903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
113304903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
113404903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
11354e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
11364e63f8edSBenjamin Herrenschmidt 	 */
11374e63f8edSBenjamin Herrenschmidt 
11384e63f8edSBenjamin Herrenschmidt 	/*
11394e63f8edSBenjamin Herrenschmidt 	 * If we support a HW FPU, we need to ensure the FP state
11404e63f8edSBenjamin Herrenschmidt 	 * if flushed into the thread_struct before attempting
11414e63f8edSBenjamin Herrenschmidt 	 * emulation
11424e63f8edSBenjamin Herrenschmidt 	 */
11434e63f8edSBenjamin Herrenschmidt #ifdef CONFIG_PPC_FPU
11444e63f8edSBenjamin Herrenschmidt 	flush_fp_to_thread(current);
11454e63f8edSBenjamin Herrenschmidt #endif
11465fad293bSKumar Gala 	switch (do_mathemu(regs)) {
11475fad293bSKumar Gala 	case 0:
114804903a30SKumar Gala 		emulate_single_step(regs);
1149ba12eedeSLi Zhong 		goto bail;
11505fad293bSKumar Gala 	case 1: {
11515fad293bSKumar Gala 			int code = 0;
11525fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11535fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
1154ba12eedeSLi Zhong 			goto bail;
115504903a30SKumar Gala 		}
11565fad293bSKumar Gala 	case -EFAULT:
11575fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1158ba12eedeSLi Zhong 		goto bail;
11595fad293bSKumar Gala 	}
11605fad293bSKumar Gala 	/* fall through on any other errors */
116104903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
116204903a30SKumar Gala 
11638dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
11648dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
116514cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
116614cf11afSPaul Mackerras 		case 0:
116714cf11afSPaul Mackerras 			regs->nip += 4;
116814cf11afSPaul Mackerras 			emulate_single_step(regs);
1169ba12eedeSLi Zhong 			goto bail;
117014cf11afSPaul Mackerras 		case -EFAULT:
117114cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1172ba12eedeSLi Zhong 			goto bail;
11738dad3f92SPaul Mackerras 		}
11748dad3f92SPaul Mackerras 	}
11758dad3f92SPaul Mackerras 
117614cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
117714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
117814cf11afSPaul Mackerras 	else
117914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1180ba12eedeSLi Zhong 
1181ba12eedeSLi Zhong bail:
1182ba12eedeSLi Zhong 	exception_exit(prev_state);
118314cf11afSPaul Mackerras }
118414cf11afSPaul Mackerras 
1185bf593907SPaul Mackerras /*
1186bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1187bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1188bf593907SPaul Mackerras  */
1189bf593907SPaul Mackerras void __kprobes emulation_assist_interrupt(struct pt_regs *regs)
1190bf593907SPaul Mackerras {
1191bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1192bf593907SPaul Mackerras 	program_check_exception(regs);
1193bf593907SPaul Mackerras }
1194bf593907SPaul Mackerras 
1195dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
119614cf11afSPaul Mackerras {
1197ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
11984393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
119914cf11afSPaul Mackerras 
1200a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1201a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1202a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1203a3512b2dSBenjamin Herrenschmidt 
12046ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12056ce6c629SMichael Neuling 		goto bail;
12066ce6c629SMichael Neuling 
1207e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1208e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
120914cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
121014cf11afSPaul Mackerras 
121114cf11afSPaul Mackerras 	if (fixed == 1) {
121214cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
121314cf11afSPaul Mackerras 		emulate_single_step(regs);
1214ba12eedeSLi Zhong 		goto bail;
121514cf11afSPaul Mackerras 	}
121614cf11afSPaul Mackerras 
121714cf11afSPaul Mackerras 	/* Operand address was bad */
121814cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12194393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12204393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12214393c4f6SBenjamin Herrenschmidt 	} else {
12224393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12234393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
122414cf11afSPaul Mackerras 	}
12254393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12264393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
12274393c4f6SBenjamin Herrenschmidt 	else
12284393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1229ba12eedeSLi Zhong 
1230ba12eedeSLi Zhong bail:
1231ba12eedeSLi Zhong 	exception_exit(prev_state);
123214cf11afSPaul Mackerras }
123314cf11afSPaul Mackerras 
123414cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
123514cf11afSPaul Mackerras {
123614cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
123714cf11afSPaul Mackerras 	       current, regs->gpr[1]);
123814cf11afSPaul Mackerras 	debugger(regs);
123914cf11afSPaul Mackerras 	show_regs(regs);
124014cf11afSPaul Mackerras 	panic("kernel stack overflow");
124114cf11afSPaul Mackerras }
124214cf11afSPaul Mackerras 
124314cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
124414cf11afSPaul Mackerras {
124514cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
124614cf11afSPaul Mackerras 	       regs->nip, regs->msr);
124714cf11afSPaul Mackerras 	debugger(regs);
124814cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
124914cf11afSPaul Mackerras }
125014cf11afSPaul Mackerras 
125114cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
125214cf11afSPaul Mackerras {
125314cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
125419c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
125514cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
125614cf11afSPaul Mackerras }
125714cf11afSPaul Mackerras 
1258dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1259dc1c1ca3SStephen Rothwell {
1260ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1261ba12eedeSLi Zhong 
1262dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1263dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1264dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1265ba12eedeSLi Zhong 
1266ba12eedeSLi Zhong 	exception_exit(prev_state);
1267dc1c1ca3SStephen Rothwell }
1268dc1c1ca3SStephen Rothwell 
1269dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1270dc1c1ca3SStephen Rothwell {
1271ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1272ba12eedeSLi Zhong 
1273dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1274dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1275dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1276dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1277ba12eedeSLi Zhong 		goto bail;
1278dc1c1ca3SStephen Rothwell 	}
12796c4841c2SAnton Blanchard 
1280dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1281dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1282dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1283ba12eedeSLi Zhong 
1284ba12eedeSLi Zhong bail:
1285ba12eedeSLi Zhong 	exception_exit(prev_state);
1286dc1c1ca3SStephen Rothwell }
1287dc1c1ca3SStephen Rothwell 
1288ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1289ce48b210SMichael Neuling {
1290ce48b210SMichael Neuling 	if (user_mode(regs)) {
1291ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1292ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1293ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1294ce48b210SMichael Neuling 		return;
1295ce48b210SMichael Neuling 	}
1296ce48b210SMichael Neuling 
1297ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1298ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1299ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1300ce48b210SMichael Neuling }
1301ce48b210SMichael Neuling 
1302021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1303d0c0c9a1SMichael Neuling {
1304021424a1SMichael Ellerman 	static char *facility_strings[] = {
1305021424a1SMichael Ellerman 		"FPU",
1306021424a1SMichael Ellerman 		"VMX/VSX",
1307021424a1SMichael Ellerman 		"DSCR",
1308021424a1SMichael Ellerman 		"PMU SPRs",
1309021424a1SMichael Ellerman 		"BHRB",
1310021424a1SMichael Ellerman 		"TM",
1311021424a1SMichael Ellerman 		"AT",
1312021424a1SMichael Ellerman 		"EBB",
1313021424a1SMichael Ellerman 		"TAR",
1314021424a1SMichael Ellerman 	};
1315b14b6260SMichael Ellerman 	char *facility, *prefix;
1316021424a1SMichael Ellerman 	u64 value;
1317021424a1SMichael Ellerman 
1318b14b6260SMichael Ellerman 	if (regs->trap == 0xf60) {
1319b14b6260SMichael Ellerman 		value = mfspr(SPRN_FSCR);
1320b14b6260SMichael Ellerman 		prefix = "";
1321b14b6260SMichael Ellerman 	} else {
1322b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
1323b14b6260SMichael Ellerman 		prefix = "Hypervisor ";
1324b14b6260SMichael Ellerman 	}
1325b14b6260SMichael Ellerman 
1326b14b6260SMichael Ellerman 	value = value >> 56;
1327021424a1SMichael Ellerman 
1328d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1329d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1330d0c0c9a1SMichael Neuling 		local_irq_enable();
1331d0c0c9a1SMichael Neuling 
1332021424a1SMichael Ellerman 	if (value < ARRAY_SIZE(facility_strings))
1333021424a1SMichael Ellerman 		facility = facility_strings[value];
1334021424a1SMichael Ellerman 	else
1335021424a1SMichael Ellerman 		facility = "unknown";
1336021424a1SMichael Ellerman 
1337b14b6260SMichael Ellerman 	pr_err("%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
1338b14b6260SMichael Ellerman 		prefix, facility, regs->nip, regs->msr);
1339d0c0c9a1SMichael Neuling 
1340d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1341d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1342d0c0c9a1SMichael Neuling 		return;
1343d0c0c9a1SMichael Neuling 	}
1344d0c0c9a1SMichael Neuling 
1345021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1346d0c0c9a1SMichael Neuling }
1347d0c0c9a1SMichael Neuling 
1348f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1349f54db641SMichael Neuling 
1350f54db641SMichael Neuling extern void do_load_up_fpu(struct pt_regs *regs);
1351f54db641SMichael Neuling 
1352f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1353f54db641SMichael Neuling {
1354f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1355f54db641SMichael Neuling 
1356f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1357f54db641SMichael Neuling 		 regs->nip, regs->msr);
1358f54db641SMichael Neuling 	tm_enable();
1359f54db641SMichael Neuling 
1360f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1361f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1362f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1363f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1364f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1365f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1366f54db641SMichael Neuling 	 */
1367f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1368f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1369f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1370f54db641SMichael Neuling 
1371f54db641SMichael Neuling 	/* Enable FP for the task: */
1372f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1373f54db641SMichael Neuling 
1374f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1375f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1376f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
1377f54db641SMichael Neuling 	 */
1378f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1379f54db641SMichael Neuling }
1380f54db641SMichael Neuling 
1381f54db641SMichael Neuling #ifdef CONFIG_ALTIVEC
1382f54db641SMichael Neuling extern void do_load_up_altivec(struct pt_regs *regs);
1383f54db641SMichael Neuling 
1384f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1385f54db641SMichael Neuling {
1386f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1387f54db641SMichael Neuling 	 * the same way.
1388f54db641SMichael Neuling 	 */
1389f54db641SMichael Neuling 
1390f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1391f54db641SMichael Neuling 		 "MSR=%lx\n",
1392f54db641SMichael Neuling 		 regs->nip, regs->msr);
1393f54db641SMichael Neuling 	tm_enable();
1394f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1395f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1396f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
1397f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1398f54db641SMichael Neuling 	current->thread.used_vr = 1;
1399f54db641SMichael Neuling }
1400f54db641SMichael Neuling #endif
1401f54db641SMichael Neuling 
1402f54db641SMichael Neuling #ifdef CONFIG_VSX
1403f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1404f54db641SMichael Neuling {
1405f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1406f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1407f54db641SMichael Neuling 	 *
1408f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1409f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1410f54db641SMichael Neuling 	 */
1411f54db641SMichael Neuling 
1412f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1413f54db641SMichael Neuling 		 "MSR=%lx\n",
1414f54db641SMichael Neuling 		 regs->nip, regs->msr);
1415f54db641SMichael Neuling 
1416f54db641SMichael Neuling 	tm_enable();
1417f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1418f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1419f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1420f54db641SMichael Neuling 
1421f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1422f54db641SMichael Neuling 		MSR_VSX;
1423f54db641SMichael Neuling 	/* This loads & recheckpoints FP and VRs. */
1424f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1425f54db641SMichael Neuling 	current->thread.used_vsr = 1;
1426f54db641SMichael Neuling }
1427f54db641SMichael Neuling #endif
1428f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1429f54db641SMichael Neuling 
1430dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1431dc1c1ca3SStephen Rothwell {
143289713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
143389713ed1SAnton Blanchard 
1434dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1435dc1c1ca3SStephen Rothwell }
1436dc1c1ca3SStephen Rothwell 
14378dad3f92SPaul Mackerras #ifdef CONFIG_8xx
143814cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
143914cf11afSPaul Mackerras {
144014cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
1441968219faSBenjamin Herrenschmidt #if defined(CONFIG_MATH_EMULATION)
144214cf11afSPaul Mackerras 	int errcode;
14435dd57a13SScott Wood #endif
144414cf11afSPaul Mackerras 
144514cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
144614cf11afSPaul Mackerras 
144714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
144814cf11afSPaul Mackerras 		debugger(regs);
144914cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
145014cf11afSPaul Mackerras 	}
145114cf11afSPaul Mackerras 
145214cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
145314cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
145480947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1455eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
14565fad293bSKumar Gala 
14575fad293bSKumar Gala 	switch (errcode) {
14585fad293bSKumar Gala 	case 0:
14595fad293bSKumar Gala 		emulate_single_step(regs);
14605fad293bSKumar Gala 		return;
14615fad293bSKumar Gala 	case 1: {
14625fad293bSKumar Gala 			int code = 0;
14635fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
14645fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
14655fad293bSKumar Gala 			return;
14665fad293bSKumar Gala 		}
14675fad293bSKumar Gala 	case -EFAULT:
14685fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
14695fad293bSKumar Gala 		return;
14705fad293bSKumar Gala 	default:
14715fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
14725fad293bSKumar Gala 		return;
14735fad293bSKumar Gala 	}
14745dd57a13SScott Wood #else
14755dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
14765fad293bSKumar Gala #endif
147714cf11afSPaul Mackerras }
14788dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
147914cf11afSPaul Mackerras 
1480172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
14813bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
14823bffb652SDave Kleikamp {
14833bffb652SDave Kleikamp 	int changed = 0;
14843bffb652SDave Kleikamp 	/*
14853bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
14863bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
14873bffb652SDave Kleikamp 	 */
14883bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
14893bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
14903bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
14913bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
14923bffb652SDave Kleikamp #endif
14933bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
14943bffb652SDave Kleikamp 			     5);
14953bffb652SDave Kleikamp 		changed |= 0x01;
14963bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
14973bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
14983bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
14993bffb652SDave Kleikamp 			     6);
15003bffb652SDave Kleikamp 		changed |= 0x01;
15013bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
15023bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
15033bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
15043bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
15053bffb652SDave Kleikamp 			     1);
15063bffb652SDave Kleikamp 		changed |= 0x01;
15073bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
15083bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
15093bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
15103bffb652SDave Kleikamp 			     2);
15113bffb652SDave Kleikamp 		changed |= 0x01;
15123bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
15133bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
15143bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
15153bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
15163bffb652SDave Kleikamp 			     3);
15173bffb652SDave Kleikamp 		changed |= 0x01;
15183bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
15193bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
15203bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
15213bffb652SDave Kleikamp 			     4);
15223bffb652SDave Kleikamp 		changed |= 0x01;
15233bffb652SDave Kleikamp 	}
15243bffb652SDave Kleikamp 	/*
15253bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
15263bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
15273bffb652SDave Kleikamp 	 * back on or not.
15283bffb652SDave Kleikamp 	 */
15293bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
15303bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
15313bffb652SDave Kleikamp 	else
15323bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
15333bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
15343bffb652SDave Kleikamp 
15353bffb652SDave Kleikamp 	if (changed & 0x01)
15363bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
15373bffb652SDave Kleikamp }
153814cf11afSPaul Mackerras 
1539f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
154014cf11afSPaul Mackerras {
15413bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
15423bffb652SDave Kleikamp 
1543ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1544ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1545ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1546ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1547ec097c84SRoland McGrath 	 */
1548ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1549ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1550ec097c84SRoland McGrath 
1551ec097c84SRoland McGrath 		/* Disable BT */
1552ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1553ec097c84SRoland McGrath 		/* Clear the BT event */
1554ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1555ec097c84SRoland McGrath 
1556ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1557ec097c84SRoland McGrath 		if (user_mode(regs)) {
1558ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1559ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1560ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1561ec097c84SRoland McGrath 			return;
1562ec097c84SRoland McGrath 		}
1563ec097c84SRoland McGrath 
1564ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1565ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1566ec097c84SRoland McGrath 			return;
1567ec097c84SRoland McGrath 		}
1568ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1569ec097c84SRoland McGrath 			return;
1570ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
157114cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1572f8279621SKumar Gala 
157314cf11afSPaul Mackerras 		/* Disable instruction completion */
157414cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
157514cf11afSPaul Mackerras 		/* Clear the instruction completion event */
157614cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1577f8279621SKumar Gala 
1578f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1579f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
158014cf11afSPaul Mackerras 			return;
158114cf11afSPaul Mackerras 		}
1582f8279621SKumar Gala 
1583f8279621SKumar Gala 		if (debugger_sstep(regs))
1584f8279621SKumar Gala 			return;
1585f8279621SKumar Gala 
15863bffb652SDave Kleikamp 		if (user_mode(regs)) {
15873bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
15883bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
15893bffb652SDave Kleikamp 					       current->thread.dbcr1))
15903bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
15913bffb652SDave Kleikamp 			else
15923bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
15933bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
15943bffb652SDave Kleikamp 		}
1595f8279621SKumar Gala 
1596f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
15973bffb652SDave Kleikamp 	} else
15983bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
159914cf11afSPaul Mackerras }
1600172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
160114cf11afSPaul Mackerras 
160214cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
160314cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
160414cf11afSPaul Mackerras {
160514cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
160614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
160714cf11afSPaul Mackerras }
160814cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
160914cf11afSPaul Mackerras 
161014cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1611dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
161214cf11afSPaul Mackerras {
161314cf11afSPaul Mackerras 	int err;
161414cf11afSPaul Mackerras 
161514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
161614cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
161714cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
16188dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
161914cf11afSPaul Mackerras 	}
162014cf11afSPaul Mackerras 
1621dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1622dc1c1ca3SStephen Rothwell 
1623eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
162414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
162514cf11afSPaul Mackerras 	if (err == 0) {
162614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
162714cf11afSPaul Mackerras 		emulate_single_step(regs);
162814cf11afSPaul Mackerras 		return;
162914cf11afSPaul Mackerras 	}
163014cf11afSPaul Mackerras 
163114cf11afSPaul Mackerras 	if (err == -EFAULT) {
163214cf11afSPaul Mackerras 		/* got an error reading the instruction */
163314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
163414cf11afSPaul Mackerras 	} else {
163514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
163614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
163776462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
163814cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
163914cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
164014cf11afSPaul Mackerras 	}
164114cf11afSPaul Mackerras }
164214cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
164314cf11afSPaul Mackerras 
1644ce48b210SMichael Neuling #ifdef CONFIG_VSX
1645ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1646ce48b210SMichael Neuling {
1647ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1648ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1649ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1650ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1651ce48b210SMichael Neuling 	}
1652ce48b210SMichael Neuling 
1653ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1654ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1655ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1656ce48b210SMichael Neuling }
1657ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1658ce48b210SMichael Neuling 
165914cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
166014cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
166114cf11afSPaul Mackerras 			   unsigned long error_code)
166214cf11afSPaul Mackerras {
166314cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
166414cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
166514cf11afSPaul Mackerras 	 * something smarter
166614cf11afSPaul Mackerras 	 */
166714cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
166814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
166914cf11afSPaul Mackerras 	return;
167014cf11afSPaul Mackerras }
167114cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
167214cf11afSPaul Mackerras 
167314cf11afSPaul Mackerras #ifdef CONFIG_SPE
167414cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
167514cf11afSPaul Mackerras {
16766a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
167714cf11afSPaul Mackerras 	unsigned long spefscr;
167814cf11afSPaul Mackerras 	int fpexc_mode;
167914cf11afSPaul Mackerras 	int code = 0;
16806a800f36SLiu Yu 	int err;
16816a800f36SLiu Yu 
1682685659eeSyu liu 	flush_spe_to_thread(current);
168314cf11afSPaul Mackerras 
168414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
168514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
168614cf11afSPaul Mackerras 
168714cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
168814cf11afSPaul Mackerras 		code = FPE_FLTOVF;
168914cf11afSPaul Mackerras 	}
169014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
169114cf11afSPaul Mackerras 		code = FPE_FLTUND;
169214cf11afSPaul Mackerras 	}
169314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
169414cf11afSPaul Mackerras 		code = FPE_FLTDIV;
169514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
169614cf11afSPaul Mackerras 		code = FPE_FLTINV;
169714cf11afSPaul Mackerras 	}
169814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
169914cf11afSPaul Mackerras 		code = FPE_FLTRES;
170014cf11afSPaul Mackerras 
17016a800f36SLiu Yu 	err = do_spe_mathemu(regs);
17026a800f36SLiu Yu 	if (err == 0) {
17036a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17046a800f36SLiu Yu 		emulate_single_step(regs);
170514cf11afSPaul Mackerras 		return;
170614cf11afSPaul Mackerras 	}
17076a800f36SLiu Yu 
17086a800f36SLiu Yu 	if (err == -EFAULT) {
17096a800f36SLiu Yu 		/* got an error reading the instruction */
17106a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17116a800f36SLiu Yu 	} else if (err == -EINVAL) {
17126a800f36SLiu Yu 		/* didn't recognize the instruction */
17136a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17146a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17156a800f36SLiu Yu 	} else {
17166a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
17176a800f36SLiu Yu 	}
17186a800f36SLiu Yu 
17196a800f36SLiu Yu 	return;
17206a800f36SLiu Yu }
17216a800f36SLiu Yu 
17226a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
17236a800f36SLiu Yu {
17246a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
17256a800f36SLiu Yu 	int err;
17266a800f36SLiu Yu 
17276a800f36SLiu Yu 	preempt_disable();
17286a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
17296a800f36SLiu Yu 		giveup_spe(current);
17306a800f36SLiu Yu 	preempt_enable();
17316a800f36SLiu Yu 
17326a800f36SLiu Yu 	regs->nip -= 4;
17336a800f36SLiu Yu 	err = speround_handler(regs);
17346a800f36SLiu Yu 	if (err == 0) {
17356a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17366a800f36SLiu Yu 		emulate_single_step(regs);
17376a800f36SLiu Yu 		return;
17386a800f36SLiu Yu 	}
17396a800f36SLiu Yu 
17406a800f36SLiu Yu 	if (err == -EFAULT) {
17416a800f36SLiu Yu 		/* got an error reading the instruction */
17426a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17436a800f36SLiu Yu 	} else if (err == -EINVAL) {
17446a800f36SLiu Yu 		/* didn't recognize the instruction */
17456a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17466a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17476a800f36SLiu Yu 	} else {
17486a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
17496a800f36SLiu Yu 		return;
17506a800f36SLiu Yu 	}
17516a800f36SLiu Yu }
175214cf11afSPaul Mackerras #endif
175314cf11afSPaul Mackerras 
1754dc1c1ca3SStephen Rothwell /*
1755dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1756dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1757dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1758dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1759dc1c1ca3SStephen Rothwell  */
1760dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1761dc1c1ca3SStephen Rothwell {
1762dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1763dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1764dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1765dc1c1ca3SStephen Rothwell }
1766dc1c1ca3SStephen Rothwell 
17671e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
176814cf11afSPaul Mackerras /*
176914cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
177014cf11afSPaul Mackerras  * spins until a reboot occurs
177114cf11afSPaul Mackerras  */
177214cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
177314cf11afSPaul Mackerras {
177414cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
177514cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
177614cf11afSPaul Mackerras 	return;
177714cf11afSPaul Mackerras }
177814cf11afSPaul Mackerras 
177914cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
178014cf11afSPaul Mackerras {
178114cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
178214cf11afSPaul Mackerras 	WatchdogHandler(regs);
178314cf11afSPaul Mackerras }
178414cf11afSPaul Mackerras #endif
1785dc1c1ca3SStephen Rothwell 
1786dc1c1ca3SStephen Rothwell /*
1787dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1788dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1789dc1c1ca3SStephen Rothwell  */
1790dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1791dc1c1ca3SStephen Rothwell {
1792dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1793dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1794dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1795dc1c1ca3SStephen Rothwell }
179614cf11afSPaul Mackerras 
179714cf11afSPaul Mackerras void __init trap_init(void)
179814cf11afSPaul Mackerras {
179914cf11afSPaul Mackerras }
180080947e7cSGeert Uytterhoeven 
180180947e7cSGeert Uytterhoeven 
180280947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
180380947e7cSGeert Uytterhoeven 
180480947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
180580947e7cSGeert Uytterhoeven 
180680947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
180780947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
180880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
180980947e7cSGeert Uytterhoeven #endif
181080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
181180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
181280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
181380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
181480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
181580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
181680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
181780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
181880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
181980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
182080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
182180947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
182280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
182380947e7cSGeert Uytterhoeven #endif
182480947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
182580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
182680947e7cSGeert Uytterhoeven #endif
1827efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1828efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1829efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1830efcac658SAlexey Kardashevskiy #endif
183180947e7cSGeert Uytterhoeven };
183280947e7cSGeert Uytterhoeven 
183380947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
183480947e7cSGeert Uytterhoeven 
183580947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
183680947e7cSGeert Uytterhoeven {
183776462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
183880947e7cSGeert Uytterhoeven 			    type);
183980947e7cSGeert Uytterhoeven }
184080947e7cSGeert Uytterhoeven 
184180947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
184280947e7cSGeert Uytterhoeven {
184380947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
184480947e7cSGeert Uytterhoeven 	unsigned int i;
184580947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
184680947e7cSGeert Uytterhoeven 
184780947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
184880947e7cSGeert Uytterhoeven 		return -ENODEV;
184980947e7cSGeert Uytterhoeven 
185080947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
185180947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
185280947e7cSGeert Uytterhoeven 	if (!dir)
185380947e7cSGeert Uytterhoeven 		return -ENOMEM;
185480947e7cSGeert Uytterhoeven 
185580947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
185680947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
185780947e7cSGeert Uytterhoeven 	if (!d)
185880947e7cSGeert Uytterhoeven 		goto fail;
185980947e7cSGeert Uytterhoeven 
186080947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
186180947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
186280947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
186380947e7cSGeert Uytterhoeven 		if (!d)
186480947e7cSGeert Uytterhoeven 			goto fail;
186580947e7cSGeert Uytterhoeven 	}
186680947e7cSGeert Uytterhoeven 
186780947e7cSGeert Uytterhoeven 	return 0;
186880947e7cSGeert Uytterhoeven 
186980947e7cSGeert Uytterhoeven fail:
187080947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
187180947e7cSGeert Uytterhoeven 	return -ENOMEM;
187280947e7cSGeert Uytterhoeven }
187380947e7cSGeert Uytterhoeven 
187480947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
187580947e7cSGeert Uytterhoeven 
187680947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1877