xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 42f5b4cacd783faf05e3ff8bf85e8be31f3dfa9d)
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>
4714cf11afSPaul Mackerras #include <asm/reg.h>
4814cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
4914cf11afSPaul Mackerras #include <asm/backlight.h>
5014cf11afSPaul Mackerras #endif
51dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5286417780SPaul Mackerras #include <asm/firmware.h>
53dc1c1ca3SStephen Rothwell #include <asm/processor.h>
546ce6c629SMichael Neuling #include <asm/tm.h>
55dc1c1ca3SStephen Rothwell #endif
56c0ce7d08SDavid Wilder #include <asm/kexec.h>
5716c57b36SKumar Gala #include <asm/ppc-opcode.h>
58cce1f106SShaohui Xie #include <asm/rio.h>
59ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
60ae3a197eSDavid Howells #include <asm/switch_to.h>
61f54db641SMichael Neuling #include <asm/tm.h>
62ae3a197eSDavid Howells #include <asm/debug.h>
63*42f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
644e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
65dc1c1ca3SStephen Rothwell 
667dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
675be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
695be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
705be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
715be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
729422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
735be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7414cf11afSPaul Mackerras 
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
809422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8214cf11afSPaul Mackerras #endif
8314cf11afSPaul Mackerras 
848b3c34cfSMichael Neuling /* Transactional Memory trap debug */
858b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
868b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
878b3c34cfSMichael Neuling #else
888b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
898b3c34cfSMichael Neuling #endif
908b3c34cfSMichael Neuling 
9114cf11afSPaul Mackerras /*
9214cf11afSPaul Mackerras  * Trap & Exception support
9314cf11afSPaul Mackerras  */
9414cf11afSPaul Mackerras 
956031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
966031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
976031d9d9Santon@samba.org {
986031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
996031d9d9Santon@samba.org 	if (pmac_backlight) {
1006031d9d9Santon@samba.org 		struct backlight_properties *props;
1016031d9d9Santon@samba.org 
1026031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1036031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1046031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1056031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1066031d9d9Santon@samba.org 	}
1076031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1086031d9d9Santon@samba.org }
1096031d9d9Santon@samba.org #else
1106031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1116031d9d9Santon@samba.org #endif
1126031d9d9Santon@samba.org 
113760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
114760ca4dcSAnton Blanchard static int die_owner = -1;
115760ca4dcSAnton Blanchard static unsigned int die_nest_count;
116c0ce7d08SDavid Wilder static int die_counter;
117760ca4dcSAnton Blanchard 
118760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
119760ca4dcSAnton Blanchard {
120760ca4dcSAnton Blanchard 	int cpu;
12134c2a14fSanton@samba.org 	unsigned long flags;
12214cf11afSPaul Mackerras 
12314cf11afSPaul Mackerras 	if (debugger(regs))
12414cf11afSPaul Mackerras 		return 1;
12514cf11afSPaul Mackerras 
126293e4688Santon@samba.org 	oops_enter();
127293e4688Santon@samba.org 
128760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
129760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
130760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
131760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
132760ca4dcSAnton Blanchard 		if (cpu == die_owner)
133760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
134760ca4dcSAnton Blanchard 		else
135760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
136760ca4dcSAnton Blanchard 	}
137760ca4dcSAnton Blanchard 	die_nest_count++;
138760ca4dcSAnton Blanchard 	die_owner = cpu;
13914cf11afSPaul Mackerras 	console_verbose();
14014cf11afSPaul Mackerras 	bust_spinlocks(1);
1416031d9d9Santon@samba.org 	if (machine_is(powermac))
1426031d9d9Santon@samba.org 		pmac_backlight_unblank();
143760ca4dcSAnton Blanchard 	return flags;
14434c2a14fSanton@samba.org }
1455474c120SMichael Hanselmann 
146760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
147760ca4dcSAnton Blanchard 			       int signr)
148760ca4dcSAnton Blanchard {
14914cf11afSPaul Mackerras 	bust_spinlocks(0);
150760ca4dcSAnton Blanchard 	die_owner = -1;
151373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
152760ca4dcSAnton Blanchard 	die_nest_count--;
15358154c8cSAnton Blanchard 	oops_exit();
15458154c8cSAnton Blanchard 	printk("\n");
155760ca4dcSAnton Blanchard 	if (!die_nest_count)
156760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
157760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
158760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
159cc532915SMichael Ellerman 
160ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
161ebaeb5aeSMahesh Salgaonkar 
1629b00ac06SAnton Blanchard 	/*
1639b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1649b00ac06SAnton Blanchard 	 * it through the crashdump code.
1659b00ac06SAnton Blanchard 	 */
1669b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
167cc532915SMichael Ellerman 		crash_kexec(regs);
1689b00ac06SAnton Blanchard 
1699b00ac06SAnton Blanchard 		/*
1709b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1719b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1729b00ac06SAnton Blanchard 		 * code.
1739b00ac06SAnton Blanchard 		 */
174c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1759b00ac06SAnton Blanchard 	}
17614cf11afSPaul Mackerras 
177760ca4dcSAnton Blanchard 	if (!signr)
178760ca4dcSAnton Blanchard 		return;
179760ca4dcSAnton Blanchard 
18058154c8cSAnton Blanchard 	/*
18158154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
18258154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18358154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18458154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18558154c8cSAnton Blanchard 	 */
18658154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
18758154c8cSAnton Blanchard 	    is_global_init(current)) {
18858154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
18958154c8cSAnton Blanchard 	}
19058154c8cSAnton Blanchard 
19114cf11afSPaul Mackerras 	if (in_interrupt())
19214cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
193cea6a4baSHorms 	if (panic_on_oops)
194012c437dSHorms 		panic("Fatal exception");
195760ca4dcSAnton Blanchard 	do_exit(signr);
196760ca4dcSAnton Blanchard }
197cea6a4baSHorms 
198760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
199760ca4dcSAnton Blanchard {
200760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
201760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
202760ca4dcSAnton Blanchard 	printk("PREEMPT ");
203760ca4dcSAnton Blanchard #endif
204760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
205760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
206760ca4dcSAnton Blanchard #endif
207e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
208760ca4dcSAnton Blanchard 		printk("DEBUG_PAGEALLOC ");
209760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
210760ca4dcSAnton Blanchard 	printk("NUMA ");
211760ca4dcSAnton Blanchard #endif
212760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
213760ca4dcSAnton Blanchard 
214760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
215760ca4dcSAnton Blanchard 		return 1;
216760ca4dcSAnton Blanchard 
217760ca4dcSAnton Blanchard 	print_modules();
218760ca4dcSAnton Blanchard 	show_regs(regs);
21914cf11afSPaul Mackerras 
22014cf11afSPaul Mackerras 	return 0;
22114cf11afSPaul Mackerras }
22214cf11afSPaul Mackerras 
223760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
224760ca4dcSAnton Blanchard {
225760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
226760ca4dcSAnton Blanchard 
227760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
228760ca4dcSAnton Blanchard 		err = 0;
229760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
230760ca4dcSAnton Blanchard }
231760ca4dcSAnton Blanchard 
23225baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23325baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23425baa35bSOleg Nesterov {
23525baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
23625baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
23725baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
23825baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
23925baa35bSOleg Nesterov }
24025baa35bSOleg Nesterov 
24114cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24214cf11afSPaul Mackerras {
24314cf11afSPaul Mackerras 	siginfo_t info;
244d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
245d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
246d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
247d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
24814cf11afSPaul Mackerras 
24914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
250760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
25114cf11afSPaul Mackerras 		return;
252760ca4dcSAnton Blanchard 	}
253760ca4dcSAnton Blanchard 
254760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
25576462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
256d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
257d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
25814cf11afSPaul Mackerras 	}
25914cf11afSPaul Mackerras 
260a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2619f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2629f2f79e3SBenjamin Herrenschmidt 
26341ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26414cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
26514cf11afSPaul Mackerras 	info.si_signo = signr;
26614cf11afSPaul Mackerras 	info.si_code = code;
26714cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
26814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
26914cf11afSPaul Mackerras }
27014cf11afSPaul Mackerras 
27114cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27214cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27314cf11afSPaul Mackerras {
27414cf11afSPaul Mackerras 	/* See if any machine dependent calls */
275c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
276c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
277c902be71SArnd Bergmann 			return;
278c902be71SArnd Bergmann 	}
27914cf11afSPaul Mackerras 
2808dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
28114cf11afSPaul Mackerras 
28214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28414cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
28514cf11afSPaul Mackerras 
28614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
28714cf11afSPaul Mackerras }
2881e9b4507SMahesh Salgaonkar 
2891e9b4507SMahesh Salgaonkar /*
2901e9b4507SMahesh Salgaonkar  * This function is called in real mode. Strictly no printk's please.
2911e9b4507SMahesh Salgaonkar  *
2921e9b4507SMahesh Salgaonkar  * regs->nip and regs->msr contains srr0 and ssr1.
2931e9b4507SMahesh Salgaonkar  */
2941e9b4507SMahesh Salgaonkar long machine_check_early(struct pt_regs *regs)
2951e9b4507SMahesh Salgaonkar {
2964c703416SMahesh Salgaonkar 	long handled = 0;
2974c703416SMahesh Salgaonkar 
29869111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
299e6654d5bSMahesh Salgaonkar 
30027ea2c42SDaniel Axtens 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
30127ea2c42SDaniel Axtens 
3024c703416SMahesh Salgaonkar 	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
3034c703416SMahesh Salgaonkar 		handled = cur_cpu_spec->machine_check_early(regs);
3044c703416SMahesh Salgaonkar 	return handled;
3051e9b4507SMahesh Salgaonkar }
3061e9b4507SMahesh Salgaonkar 
3070869b6fdSMahesh Salgaonkar long hmi_exception_realmode(struct pt_regs *regs)
3080869b6fdSMahesh Salgaonkar {
30969111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.hmi_exceptions);
3100869b6fdSMahesh Salgaonkar 
3110869b6fdSMahesh Salgaonkar 	if (ppc_md.hmi_exception_early)
3120869b6fdSMahesh Salgaonkar 		ppc_md.hmi_exception_early(regs);
3130869b6fdSMahesh Salgaonkar 
3140869b6fdSMahesh Salgaonkar 	return 0;
3150869b6fdSMahesh Salgaonkar }
3160869b6fdSMahesh Salgaonkar 
31714cf11afSPaul Mackerras #endif
31814cf11afSPaul Mackerras 
31914cf11afSPaul Mackerras /*
32014cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
32114cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
32214cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
32314cf11afSPaul Mackerras  * table.
32414cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
32514cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
32614cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
32714cf11afSPaul Mackerras  *  -- paulus.
32814cf11afSPaul Mackerras  */
32914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
33014cf11afSPaul Mackerras {
33168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
33214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
33314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
33414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
33514cf11afSPaul Mackerras 
33614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
33714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
33814cf11afSPaul Mackerras 		/*
33914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
34014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
34114cf11afSPaul Mackerras 		 * As the address is in the exception table
34214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
34314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
34414cf11afSPaul Mackerras 		 * load or store.
34514cf11afSPaul Mackerras 		 */
34614cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
34714cf11afSPaul Mackerras 			nip -= 2;
34814cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
34914cf11afSPaul Mackerras 			--nip;
35014cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
35114cf11afSPaul Mackerras 			/* sync or twi */
35214cf11afSPaul Mackerras 			unsigned int rb;
35314cf11afSPaul Mackerras 
35414cf11afSPaul Mackerras 			--nip;
35514cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
35614cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
35714cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
35814cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
35914cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
36014cf11afSPaul Mackerras 			regs->nip = entry->fixup;
36114cf11afSPaul Mackerras 			return 1;
36214cf11afSPaul Mackerras 		}
36314cf11afSPaul Mackerras 	}
36468a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
36514cf11afSPaul Mackerras 	return 0;
36614cf11afSPaul Mackerras }
36714cf11afSPaul Mackerras 
368172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
36914cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
37014cf11afSPaul Mackerras    is in the ESR. */
37114cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
37214cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
37314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
37414cf11afSPaul Mackerras #else
375fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
37614cf11afSPaul Mackerras #endif
37714cf11afSPaul Mackerras #define REASON_FP		ESR_FP
37814cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
37914cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
38014cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
38114cf11afSPaul Mackerras 
38214cf11afSPaul Mackerras /* single-step stuff */
38351ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
38451ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
38514cf11afSPaul Mackerras 
38614cf11afSPaul Mackerras #else
38714cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
38814cf11afSPaul Mackerras    exception is in the MSR. */
38914cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
39014cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3918b3c34cfSMichael Neuling #define REASON_TM		0x200000
39214cf11afSPaul Mackerras #define REASON_FP		0x100000
39314cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
39414cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
39514cf11afSPaul Mackerras #define REASON_TRAP		0x20000
39614cf11afSPaul Mackerras 
39714cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
39814cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
39914cf11afSPaul Mackerras #endif
40014cf11afSPaul Mackerras 
40147c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
40247c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
40314cf11afSPaul Mackerras {
4041a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
40514cf11afSPaul Mackerras 
40614cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
40714cf11afSPaul Mackerras 		printk("Instruction");
40814cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
40914cf11afSPaul Mackerras 	} else
41014cf11afSPaul Mackerras 		printk("Data");
41114cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
41247c0bd1aSBenjamin Herrenschmidt 
41347c0bd1aSBenjamin Herrenschmidt 	return 0;
41447c0bd1aSBenjamin Herrenschmidt }
41547c0bd1aSBenjamin Herrenschmidt 
41647c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
41747c0bd1aSBenjamin Herrenschmidt {
41847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
41947c0bd1aSBenjamin Herrenschmidt 
42014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42114cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
42214cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
42314cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
42414cf11afSPaul Mackerras 	}
42514cf11afSPaul Mackerras 	else {
42614cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
42714cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
42814cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
42914cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
43014cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
43114cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
43214cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
43314cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
43414cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
43514cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
43614cf11afSPaul Mackerras 			flush_instruction_cache();
43714cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
43814cf11afSPaul Mackerras 		}
43914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
44014cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
44114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
44214cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
44314cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
44414cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
44514cf11afSPaul Mackerras 
44614cf11afSPaul Mackerras 		/* Clear MCSR */
44714cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
44814cf11afSPaul Mackerras 	}
44947c0bd1aSBenjamin Herrenschmidt 	return 0;
45047c0bd1aSBenjamin Herrenschmidt }
451fc5e7097SDave Kleikamp 
452fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
453fc5e7097SDave Kleikamp {
454fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
455fc5e7097SDave Kleikamp 	u32 mcsr;
456fc5e7097SDave Kleikamp 
457fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
458fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
459fc5e7097SDave Kleikamp 		printk(KERN_ERR
460fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
461fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
462fc5e7097SDave Kleikamp 		return 0;
463fc5e7097SDave Kleikamp 	}
464fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
465fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
466fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
467fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
468fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
469fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
470fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
471fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
472fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
473fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
474fc5e7097SDave Kleikamp 		flush_instruction_cache();
475fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
476fc5e7097SDave Kleikamp 	}
477fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
478fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
479fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
480fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
481fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
482fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
483fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
484fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
485fc5e7097SDave Kleikamp 
486fc5e7097SDave Kleikamp 	/* Clear MCSR */
487fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
488fc5e7097SDave Kleikamp 
489fc5e7097SDave Kleikamp 	return 0;
490fc5e7097SDave Kleikamp }
49114cf11afSPaul Mackerras #elif defined(CONFIG_E500)
492fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
493fe04b112SScott Wood {
494fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
495fe04b112SScott Wood 	unsigned long reason = mcsr;
496fe04b112SScott Wood 	int recoverable = 1;
497fe04b112SScott Wood 
49882a9a480SScott Wood 	if (reason & MCSR_LD) {
499cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
500cce1f106SShaohui Xie 		if (recoverable == 1)
501cce1f106SShaohui Xie 			goto silent_out;
502cce1f106SShaohui Xie 	}
503cce1f106SShaohui Xie 
504fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
505fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
506fe04b112SScott Wood 
507fe04b112SScott Wood 	if (reason & MCSR_MCP)
508fe04b112SScott Wood 		printk("Machine Check Signal\n");
509fe04b112SScott Wood 
510fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
511fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
512fe04b112SScott Wood 
513fe04b112SScott Wood 		/*
514fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
515fe04b112SScott Wood 		 */
516fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
517fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
518fe04b112SScott Wood 			;
519fe04b112SScott Wood 
520fe04b112SScott Wood 		/*
521fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
522fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
523fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
524fe04b112SScott Wood 		 */
525fe04b112SScott Wood 		reason &= ~MCSR_IF;
526fe04b112SScott Wood 	}
527fe04b112SScott Wood 
528fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
529fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
53037caf9f2SKumar Gala 
53137caf9f2SKumar Gala 		/*
53237caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
53337caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
53437caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
53537caf9f2SKumar Gala 		 */
53637caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
537fe04b112SScott Wood 			recoverable = 0;
538fe04b112SScott Wood 	}
539fe04b112SScott Wood 
540fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
541fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
542fe04b112SScott Wood 		recoverable = 0;
543fe04b112SScott Wood 	}
544fe04b112SScott Wood 
545fe04b112SScott Wood 	if (reason & MCSR_NMI)
546fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
547fe04b112SScott Wood 
548fe04b112SScott Wood 	if (reason & MCSR_IF) {
549fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
550fe04b112SScott Wood 		recoverable = 0;
551fe04b112SScott Wood 	}
552fe04b112SScott Wood 
553fe04b112SScott Wood 	if (reason & MCSR_LD) {
554fe04b112SScott Wood 		printk("Load Error Report\n");
555fe04b112SScott Wood 		recoverable = 0;
556fe04b112SScott Wood 	}
557fe04b112SScott Wood 
558fe04b112SScott Wood 	if (reason & MCSR_ST) {
559fe04b112SScott Wood 		printk("Store Error Report\n");
560fe04b112SScott Wood 		recoverable = 0;
561fe04b112SScott Wood 	}
562fe04b112SScott Wood 
563fe04b112SScott Wood 	if (reason & MCSR_LDG) {
564fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
565fe04b112SScott Wood 		recoverable = 0;
566fe04b112SScott Wood 	}
567fe04b112SScott Wood 
568fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
569fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
570fe04b112SScott Wood 
571fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
572fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
573fe04b112SScott Wood 		recoverable = 0;
574fe04b112SScott Wood 	}
575fe04b112SScott Wood 
576fe04b112SScott Wood 	if (reason & MCSR_MAV) {
577fe04b112SScott Wood 		u64 addr;
578fe04b112SScott Wood 
579fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
580fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
581fe04b112SScott Wood 
582fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
583fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
584fe04b112SScott Wood 	}
585fe04b112SScott Wood 
586cce1f106SShaohui Xie silent_out:
587fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
588fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
589fe04b112SScott Wood }
590fe04b112SScott Wood 
59147c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
59247c0bd1aSBenjamin Herrenschmidt {
59347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
59447c0bd1aSBenjamin Herrenschmidt 
595cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
596cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
597cce1f106SShaohui Xie 			return 1;
5984e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5994e0e3435SHongtao Jia 			return 1;
600cce1f106SShaohui Xie 	}
601cce1f106SShaohui Xie 
60214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
60314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
60414cf11afSPaul Mackerras 
60514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
60614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
60714cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
60814cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
60914cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
61014cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
61114cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
61214cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
61314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
61414cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
61514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
61614cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
61714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
61814cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
61914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
62014cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
62114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
62214cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
62314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
624c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
62514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
62614cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
62714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
62814cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
62947c0bd1aSBenjamin Herrenschmidt 
63047c0bd1aSBenjamin Herrenschmidt 	return 0;
63147c0bd1aSBenjamin Herrenschmidt }
6324490c06bSKumar Gala 
6334490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6344490c06bSKumar Gala {
6354490c06bSKumar Gala 	return 0;
6364490c06bSKumar Gala }
63714cf11afSPaul Mackerras #elif defined(CONFIG_E200)
63847c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
63947c0bd1aSBenjamin Herrenschmidt {
64047c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
64147c0bd1aSBenjamin Herrenschmidt 
64214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
64314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
64414cf11afSPaul Mackerras 
64514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
64614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
64714cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
64814cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
64914cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
65014cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
65114cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
65214cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
65314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
65414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
65514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
65614cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
65714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
65814cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
65947c0bd1aSBenjamin Herrenschmidt 
66047c0bd1aSBenjamin Herrenschmidt 	return 0;
66147c0bd1aSBenjamin Herrenschmidt }
66247c0bd1aSBenjamin Herrenschmidt #else
66347c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
66447c0bd1aSBenjamin Herrenschmidt {
66547c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
66647c0bd1aSBenjamin Herrenschmidt 
66714cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
66814cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
66914cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
67014cf11afSPaul Mackerras 	case 0x80000:
67114cf11afSPaul Mackerras 		printk("Machine check signal\n");
67214cf11afSPaul Mackerras 		break;
67314cf11afSPaul Mackerras 	case 0:		/* for 601 */
67414cf11afSPaul Mackerras 	case 0x40000:
67514cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
67614cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
67714cf11afSPaul Mackerras 		break;
67814cf11afSPaul Mackerras 	case 0x20000:
67914cf11afSPaul Mackerras 		printk("Data parity error signal\n");
68014cf11afSPaul Mackerras 		break;
68114cf11afSPaul Mackerras 	case 0x10000:
68214cf11afSPaul Mackerras 		printk("Address parity error signal\n");
68314cf11afSPaul Mackerras 		break;
68414cf11afSPaul Mackerras 	case 0x20000000:
68514cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
68614cf11afSPaul Mackerras 		break;
68714cf11afSPaul Mackerras 	case 0x40000000:
68814cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
68914cf11afSPaul Mackerras 		break;
69014cf11afSPaul Mackerras 	case 0x00100000:
69114cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
69214cf11afSPaul Mackerras 		break;
69314cf11afSPaul Mackerras 	default:
69414cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
69514cf11afSPaul Mackerras 	}
69675918a4bSOlof Johansson 	return 0;
69775918a4bSOlof Johansson }
69847c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
69975918a4bSOlof Johansson 
70075918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
70175918a4bSOlof Johansson {
702ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
70375918a4bSOlof Johansson 	int recover = 0;
70475918a4bSOlof Johansson 
70569111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
70689713ed1SAnton Blanchard 
70747c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
70847c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
70947c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
71047c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
71147c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
71247c0bd1aSBenjamin Herrenschmidt 	 */
71375918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
71475918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
71547c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
71647c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
71775918a4bSOlof Johansson 
71847c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
719ba12eedeSLi Zhong 		goto bail;
72075918a4bSOlof Johansson 
72175918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
72247c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
72347c0bd1aSBenjamin Herrenschmidt 	 *
72447c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
72547c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
72647c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
72747c0bd1aSBenjamin Herrenschmidt 	 */
72875918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
729ba12eedeSLi Zhong 	goto bail;
73075918a4bSOlof Johansson #endif
73175918a4bSOlof Johansson 
732a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
733ba12eedeSLi Zhong 		goto bail;
73475918a4bSOlof Johansson 
73575918a4bSOlof Johansson 	if (check_io_access(regs))
736ba12eedeSLi Zhong 		goto bail;
73775918a4bSOlof Johansson 
7388dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
73914cf11afSPaul Mackerras 
74014cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
74114cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
74214cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
743ba12eedeSLi Zhong 
744ba12eedeSLi Zhong bail:
745ba12eedeSLi Zhong 	exception_exit(prev_state);
74614cf11afSPaul Mackerras }
74714cf11afSPaul Mackerras 
74814cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
74914cf11afSPaul Mackerras {
75014cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
75114cf11afSPaul Mackerras }
75214cf11afSPaul Mackerras 
7530869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
7540869b6fdSMahesh Salgaonkar {
7550869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
7560869b6fdSMahesh Salgaonkar 
7570869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
7580869b6fdSMahesh Salgaonkar 	irq_enter();
7590869b6fdSMahesh Salgaonkar 
7600869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
7610869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
7620869b6fdSMahesh Salgaonkar 
7630869b6fdSMahesh Salgaonkar 	irq_exit();
7640869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
7650869b6fdSMahesh Salgaonkar }
7660869b6fdSMahesh Salgaonkar 
767dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
76814cf11afSPaul Mackerras {
769ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
770ba12eedeSLi Zhong 
77114cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
77214cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
77314cf11afSPaul Mackerras 
77414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
775ba12eedeSLi Zhong 
776ba12eedeSLi Zhong 	exception_exit(prev_state);
77714cf11afSPaul Mackerras }
77814cf11afSPaul Mackerras 
779dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
78014cf11afSPaul Mackerras {
781ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
782ba12eedeSLi Zhong 
78314cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
78414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
785ba12eedeSLi Zhong 		goto bail;
78614cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
787ba12eedeSLi Zhong 		goto bail;
78814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
789ba12eedeSLi Zhong 
790ba12eedeSLi Zhong bail:
791ba12eedeSLi Zhong 	exception_exit(prev_state);
79214cf11afSPaul Mackerras }
79314cf11afSPaul Mackerras 
79414cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
79514cf11afSPaul Mackerras {
79614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
79714cf11afSPaul Mackerras }
79814cf11afSPaul Mackerras 
7998dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
80014cf11afSPaul Mackerras {
801ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
802ba12eedeSLi Zhong 
8032538c2d0SK.Prasad 	clear_single_step(regs);
80414cf11afSPaul Mackerras 
80514cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
80614cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
807ba12eedeSLi Zhong 		goto bail;
80814cf11afSPaul Mackerras 	if (debugger_sstep(regs))
809ba12eedeSLi Zhong 		goto bail;
81014cf11afSPaul Mackerras 
81114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
812ba12eedeSLi Zhong 
813ba12eedeSLi Zhong bail:
814ba12eedeSLi Zhong 	exception_exit(prev_state);
81514cf11afSPaul Mackerras }
81614cf11afSPaul Mackerras 
81714cf11afSPaul Mackerras /*
81814cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
81914cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
82014cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
82114cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
82214cf11afSPaul Mackerras  */
8238dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
82414cf11afSPaul Mackerras {
8252538c2d0SK.Prasad 	if (single_stepping(regs))
8262538c2d0SK.Prasad 		single_step_exception(regs);
82714cf11afSPaul Mackerras }
82814cf11afSPaul Mackerras 
8295fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
830dc1c1ca3SStephen Rothwell {
8315fad293bSKumar Gala 	int ret = 0;
832dc1c1ca3SStephen Rothwell 
833dc1c1ca3SStephen Rothwell 	/* Invalid operation */
834dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
8355fad293bSKumar Gala 		ret = FPE_FLTINV;
836dc1c1ca3SStephen Rothwell 
837dc1c1ca3SStephen Rothwell 	/* Overflow */
838dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
8395fad293bSKumar Gala 		ret = FPE_FLTOVF;
840dc1c1ca3SStephen Rothwell 
841dc1c1ca3SStephen Rothwell 	/* Underflow */
842dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
8435fad293bSKumar Gala 		ret = FPE_FLTUND;
844dc1c1ca3SStephen Rothwell 
845dc1c1ca3SStephen Rothwell 	/* Divide by zero */
846dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
8475fad293bSKumar Gala 		ret = FPE_FLTDIV;
848dc1c1ca3SStephen Rothwell 
849dc1c1ca3SStephen Rothwell 	/* Inexact result */
850dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
8515fad293bSKumar Gala 		ret = FPE_FLTRES;
8525fad293bSKumar Gala 
8535fad293bSKumar Gala 	return ret;
8545fad293bSKumar Gala }
8555fad293bSKumar Gala 
8565fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8575fad293bSKumar Gala {
8585fad293bSKumar Gala 	int code = 0;
8595fad293bSKumar Gala 
8605fad293bSKumar Gala 	flush_fp_to_thread(current);
8615fad293bSKumar Gala 
862de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
863dc1c1ca3SStephen Rothwell 
864dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
865dc1c1ca3SStephen Rothwell }
866dc1c1ca3SStephen Rothwell 
867dc1c1ca3SStephen Rothwell /*
868dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
86914cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
87014cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
87114cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
87214cf11afSPaul Mackerras  *
87314cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
87414cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
87514cf11afSPaul Mackerras  * bits is faster and easier.
87686417780SPaul Mackerras  *
87714cf11afSPaul Mackerras  */
87814cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
87914cf11afSPaul Mackerras {
88014cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
88114cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
88214cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
88314cf11afSPaul Mackerras 	u32 num_bytes;
88414cf11afSPaul Mackerras 	unsigned long EA;
88514cf11afSPaul Mackerras 	int pos = 0;
88614cf11afSPaul Mackerras 
88714cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
88816c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
88914cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
89014cf11afSPaul Mackerras 			return -EINVAL;
89114cf11afSPaul Mackerras 
89214cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
89314cf11afSPaul Mackerras 
89416c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
89516c57b36SKumar Gala 		case PPC_INST_LSWX:
89616c57b36SKumar Gala 		case PPC_INST_STSWX:
89714cf11afSPaul Mackerras 			EA += NB_RB;
89814cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
89914cf11afSPaul Mackerras 			break;
90016c57b36SKumar Gala 		case PPC_INST_LSWI:
90116c57b36SKumar Gala 		case PPC_INST_STSWI:
90214cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
90314cf11afSPaul Mackerras 			break;
90414cf11afSPaul Mackerras 		default:
90514cf11afSPaul Mackerras 			return -EINVAL;
90614cf11afSPaul Mackerras 	}
90714cf11afSPaul Mackerras 
90814cf11afSPaul Mackerras 	while (num_bytes != 0)
90914cf11afSPaul Mackerras 	{
91014cf11afSPaul Mackerras 		u8 val;
91114cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
91214cf11afSPaul Mackerras 
91380aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
91480aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
91580aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
91680aa0fb4SJames Yang 
91716c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
91816c57b36SKumar Gala 			case PPC_INST_LSWX:
91916c57b36SKumar Gala 			case PPC_INST_LSWI:
92014cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
92114cf11afSPaul Mackerras 					return -EFAULT;
92214cf11afSPaul Mackerras 				/* first time updating this reg,
92314cf11afSPaul Mackerras 				 * zero it out */
92414cf11afSPaul Mackerras 				if (pos == 0)
92514cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
92614cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
92714cf11afSPaul Mackerras 				break;
92816c57b36SKumar Gala 			case PPC_INST_STSWI:
92916c57b36SKumar Gala 			case PPC_INST_STSWX:
93014cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
93114cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
93214cf11afSPaul Mackerras 					return -EFAULT;
93314cf11afSPaul Mackerras 				break;
93414cf11afSPaul Mackerras 		}
93514cf11afSPaul Mackerras 		/* move EA to next address */
93614cf11afSPaul Mackerras 		EA += 1;
93714cf11afSPaul Mackerras 		num_bytes--;
93814cf11afSPaul Mackerras 
93914cf11afSPaul Mackerras 		/* manage our position within the register */
94014cf11afSPaul Mackerras 		if (++pos == 4) {
94114cf11afSPaul Mackerras 			pos = 0;
94214cf11afSPaul Mackerras 			if (++rT == 32)
94314cf11afSPaul Mackerras 				rT = 0;
94414cf11afSPaul Mackerras 		}
94514cf11afSPaul Mackerras 	}
94614cf11afSPaul Mackerras 
94714cf11afSPaul Mackerras 	return 0;
94814cf11afSPaul Mackerras }
94914cf11afSPaul Mackerras 
950c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
951c3412dcbSWill Schmidt {
952c3412dcbSWill Schmidt 	u32 ra,rs;
953c3412dcbSWill Schmidt 	unsigned long tmp;
954c3412dcbSWill Schmidt 
955c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
956c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
957c3412dcbSWill Schmidt 
958c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
959c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
960c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
961c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
962c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
963c3412dcbSWill Schmidt 
964c3412dcbSWill Schmidt 	return 0;
965c3412dcbSWill Schmidt }
966c3412dcbSWill Schmidt 
967c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
968c1469f13SKumar Gala {
969c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
970c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
971c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
972c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
973c1469f13SKumar Gala 	u8 bit;
974c1469f13SKumar Gala 	unsigned long tmp;
975c1469f13SKumar Gala 
976c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
977c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
978c1469f13SKumar Gala 
979c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
980c1469f13SKumar Gala 
981c1469f13SKumar Gala 	return 0;
982c1469f13SKumar Gala }
983c1469f13SKumar Gala 
9846ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9856ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9866ce6c629SMichael Neuling {
9876ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9886ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9896ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9906ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
9916ce6c629SMichael Neuling 	 */
9926ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
9936ce6c629SMichael Neuling 		tm_enable();
9946ce6c629SMichael Neuling 		tm_abort(cause);
9956ce6c629SMichael Neuling 		return true;
9966ce6c629SMichael Neuling 	}
9976ce6c629SMichael Neuling 	return false;
9986ce6c629SMichael Neuling }
9996ce6c629SMichael Neuling #else
10006ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
10016ce6c629SMichael Neuling {
10026ce6c629SMichael Neuling 	return false;
10036ce6c629SMichael Neuling }
10046ce6c629SMichael Neuling #endif
10056ce6c629SMichael Neuling 
100614cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
100714cf11afSPaul Mackerras {
100814cf11afSPaul Mackerras 	u32 instword;
100914cf11afSPaul Mackerras 	u32 rd;
101014cf11afSPaul Mackerras 
10114288e343SAnton Blanchard 	if (!user_mode(regs))
101214cf11afSPaul Mackerras 		return -EINVAL;
101314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
101414cf11afSPaul Mackerras 
101514cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
101614cf11afSPaul Mackerras 		return -EFAULT;
101714cf11afSPaul Mackerras 
101814cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
101916c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1020eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
102114cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
102214cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
102314cf11afSPaul Mackerras 		return 0;
102414cf11afSPaul Mackerras 	}
102514cf11afSPaul Mackerras 
102614cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
102780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1028eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
102914cf11afSPaul Mackerras 		return 0;
103080947e7cSGeert Uytterhoeven 	}
103114cf11afSPaul Mackerras 
103214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
103316c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
103486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
103514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
103614cf11afSPaul Mackerras 
1037eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
103814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
103914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
104014cf11afSPaul Mackerras 		return 0;
104114cf11afSPaul Mackerras 	}
104214cf11afSPaul Mackerras 
104314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
104480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
10456ce6c629SMichael Neuling 		if (tm_abort_check(regs,
10466ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
10476ce6c629SMichael Neuling 			return -EINVAL;
1048eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
104914cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
105080947e7cSGeert Uytterhoeven 	}
105114cf11afSPaul Mackerras 
1052c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
105316c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1054eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1055c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1056c3412dcbSWill Schmidt 	}
1057c3412dcbSWill Schmidt 
1058c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
105916c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1060eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1061c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1062c1469f13SKumar Gala 	}
1063c1469f13SKumar Gala 
10649863c28aSJames Yang 	/* Emulate sync instruction variants */
10659863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
10669863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
10679863c28aSJames Yang 		asm volatile("sync");
10689863c28aSJames Yang 		return 0;
10699863c28aSJames Yang 	}
10709863c28aSJames Yang 
1071efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1072efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
107373d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
107473d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
107573d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
107673d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1077efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1078efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1079efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1080efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1081efcac658SAlexey Kardashevskiy 		return 0;
1082efcac658SAlexey Kardashevskiy 	}
1083efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
108473d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
108573d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
108673d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
108773d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1088efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1089efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1090efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
109100ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1092efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
109300ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1094efcac658SAlexey Kardashevskiy 		return 0;
1095efcac658SAlexey Kardashevskiy 	}
1096efcac658SAlexey Kardashevskiy #endif
1097efcac658SAlexey Kardashevskiy 
109814cf11afSPaul Mackerras 	return -EINVAL;
109914cf11afSPaul Mackerras }
110014cf11afSPaul Mackerras 
110173c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
110214cf11afSPaul Mackerras {
110373c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
110414cf11afSPaul Mackerras }
110514cf11afSPaul Mackerras 
11063a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
11073a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
11083a3b5aa6SKevin Hao {
11093a3b5aa6SKevin Hao 	int ret;
11103a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
11113a3b5aa6SKevin Hao 
11123a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
11133a3b5aa6SKevin Hao 	if (ret >= 0)
11143a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
11153a3b5aa6SKevin Hao 
11163a3b5aa6SKevin Hao 	switch (ret) {
11173a3b5aa6SKevin Hao 	case 0:
11183a3b5aa6SKevin Hao 		emulate_single_step(regs);
11193a3b5aa6SKevin Hao 		return 0;
11203a3b5aa6SKevin Hao 	case 1: {
11213a3b5aa6SKevin Hao 			int code = 0;
1122de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
11233a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
11243a3b5aa6SKevin Hao 			return 0;
11253a3b5aa6SKevin Hao 		}
11263a3b5aa6SKevin Hao 	case -EFAULT:
11273a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11283a3b5aa6SKevin Hao 		return 0;
11293a3b5aa6SKevin Hao 	}
11303a3b5aa6SKevin Hao 
11313a3b5aa6SKevin Hao 	return -1;
11323a3b5aa6SKevin Hao }
11333a3b5aa6SKevin Hao #else
11343a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
11353a3b5aa6SKevin Hao #endif
11363a3b5aa6SKevin Hao 
11378dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
113814cf11afSPaul Mackerras {
1139ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
114014cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
114114cf11afSPaul Mackerras 
1142aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
114304903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
114414cf11afSPaul Mackerras 
114514cf11afSPaul Mackerras 	if (reason & REASON_FP) {
114614cf11afSPaul Mackerras 		/* IEEE FP exception */
1147dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1148ba12eedeSLi Zhong 		goto bail;
11498dad3f92SPaul Mackerras 	}
11508dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1151a4c3f909SBalbir Singh 		unsigned long bugaddr;
1152ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1153ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1154ba797b28SJason Wessel 		if (debugger_bpt(regs))
1155ba12eedeSLi Zhong 			goto bail;
1156ba797b28SJason Wessel 
115714cf11afSPaul Mackerras 		/* trap exception */
1158dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1159dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1160ba12eedeSLi Zhong 			goto bail;
116173c9ceabSJeremy Fitzhardinge 
1162a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1163a4c3f909SBalbir Singh 		/*
1164a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1165a4c3f909SBalbir Singh 		 */
1166a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1167a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1168a4c3f909SBalbir Singh 
116973c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1170a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
117114cf11afSPaul Mackerras 			regs->nip += 4;
1172ba12eedeSLi Zhong 			goto bail;
117314cf11afSPaul Mackerras 		}
11748dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1175ba12eedeSLi Zhong 		goto bail;
11768dad3f92SPaul Mackerras 	}
1177bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1178bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1179bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1180bc2a9408SMichael Neuling 		 * This occurs when:
1181bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1182bc2a9408SMichael Neuling 		 *    transition in TM states.
1183bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1184bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1185bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1186bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1187bc2a9408SMichael Neuling 		 */
1188bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1189bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1190bc2a9408SMichael Neuling 			regs->nip += 4;
1191ba12eedeSLi Zhong 			goto bail;
1192bc2a9408SMichael Neuling 		}
1193bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1194bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1195bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1196bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1197bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1198bc2a9408SMichael Neuling 		 */
1199bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1200bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1201ba12eedeSLi Zhong 			goto bail;
1202bc2a9408SMichael Neuling 		} else {
1203bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1204bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1205bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1206bc2a9408SMichael Neuling 		}
1207bc2a9408SMichael Neuling 	}
1208bc2a9408SMichael Neuling #endif
12098dad3f92SPaul Mackerras 
1210b3f6a459SMichael Ellerman 	/*
1211b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1212b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1213b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1214b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1215b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1216b3f6a459SMichael Ellerman 	 */
1217b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1218b3f6a459SMichael Ellerman 		goto sigill;
1219b3f6a459SMichael Ellerman 
1220a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1221a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1222cd8a5673SPaul Mackerras 		local_irq_enable();
1223cd8a5673SPaul Mackerras 
122404903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
122504903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
122604903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
122704903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
122804903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
122904903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
12304e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
12314e63f8edSBenjamin Herrenschmidt 	 */
12323a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1233ba12eedeSLi Zhong 		goto bail;
123404903a30SKumar Gala 
12358dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
12368dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
123714cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
123814cf11afSPaul Mackerras 		case 0:
123914cf11afSPaul Mackerras 			regs->nip += 4;
124014cf11afSPaul Mackerras 			emulate_single_step(regs);
1241ba12eedeSLi Zhong 			goto bail;
124214cf11afSPaul Mackerras 		case -EFAULT:
124314cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1244ba12eedeSLi Zhong 			goto bail;
12458dad3f92SPaul Mackerras 		}
12468dad3f92SPaul Mackerras 	}
12478dad3f92SPaul Mackerras 
1248b3f6a459SMichael Ellerman sigill:
124914cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
125014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
125114cf11afSPaul Mackerras 	else
125214cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1253ba12eedeSLi Zhong 
1254ba12eedeSLi Zhong bail:
1255ba12eedeSLi Zhong 	exception_exit(prev_state);
125614cf11afSPaul Mackerras }
125714cf11afSPaul Mackerras 
1258bf593907SPaul Mackerras /*
1259bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1260bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1261bf593907SPaul Mackerras  */
1262bf593907SPaul Mackerras void __kprobes emulation_assist_interrupt(struct pt_regs *regs)
1263bf593907SPaul Mackerras {
1264bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1265bf593907SPaul Mackerras 	program_check_exception(regs);
1266bf593907SPaul Mackerras }
1267bf593907SPaul Mackerras 
1268dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
126914cf11afSPaul Mackerras {
1270ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
12714393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
127214cf11afSPaul Mackerras 
1273a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1274a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1275a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1276a3512b2dSBenjamin Herrenschmidt 
12776ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12786ce6c629SMichael Neuling 		goto bail;
12796ce6c629SMichael Neuling 
1280e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1281e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
128214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
128314cf11afSPaul Mackerras 
128414cf11afSPaul Mackerras 	if (fixed == 1) {
128514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
128614cf11afSPaul Mackerras 		emulate_single_step(regs);
1287ba12eedeSLi Zhong 		goto bail;
128814cf11afSPaul Mackerras 	}
128914cf11afSPaul Mackerras 
129014cf11afSPaul Mackerras 	/* Operand address was bad */
129114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12924393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12934393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12944393c4f6SBenjamin Herrenschmidt 	} else {
12954393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12964393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
129714cf11afSPaul Mackerras 	}
12984393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12994393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
13004393c4f6SBenjamin Herrenschmidt 	else
13014393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1302ba12eedeSLi Zhong 
1303ba12eedeSLi Zhong bail:
1304ba12eedeSLi Zhong 	exception_exit(prev_state);
130514cf11afSPaul Mackerras }
130614cf11afSPaul Mackerras 
130714cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
130814cf11afSPaul Mackerras {
130914cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
131014cf11afSPaul Mackerras 	       current, regs->gpr[1]);
131114cf11afSPaul Mackerras 	debugger(regs);
131214cf11afSPaul Mackerras 	show_regs(regs);
131314cf11afSPaul Mackerras 	panic("kernel stack overflow");
131414cf11afSPaul Mackerras }
131514cf11afSPaul Mackerras 
131614cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
131714cf11afSPaul Mackerras {
131814cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
131914cf11afSPaul Mackerras 	       regs->nip, regs->msr);
132014cf11afSPaul Mackerras 	debugger(regs);
132114cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
132214cf11afSPaul Mackerras }
132314cf11afSPaul Mackerras 
1324dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1325dc1c1ca3SStephen Rothwell {
1326ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1327ba12eedeSLi Zhong 
1328dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1329dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1330dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1331ba12eedeSLi Zhong 
1332ba12eedeSLi Zhong 	exception_exit(prev_state);
1333dc1c1ca3SStephen Rothwell }
1334dc1c1ca3SStephen Rothwell 
1335dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1336dc1c1ca3SStephen Rothwell {
1337ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1338ba12eedeSLi Zhong 
1339dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1340dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1341dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1342dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1343ba12eedeSLi Zhong 		goto bail;
1344dc1c1ca3SStephen Rothwell 	}
13456c4841c2SAnton Blanchard 
1346dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1347dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1348dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1349ba12eedeSLi Zhong 
1350ba12eedeSLi Zhong bail:
1351ba12eedeSLi Zhong 	exception_exit(prev_state);
1352dc1c1ca3SStephen Rothwell }
1353dc1c1ca3SStephen Rothwell 
1354ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1355ce48b210SMichael Neuling {
1356ce48b210SMichael Neuling 	if (user_mode(regs)) {
1357ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1358ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1359ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1360ce48b210SMichael Neuling 		return;
1361ce48b210SMichael Neuling 	}
1362ce48b210SMichael Neuling 
1363ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1364ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1365ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1366ce48b210SMichael Neuling }
1367ce48b210SMichael Neuling 
13682517617eSMichael Neuling #ifdef CONFIG_PPC64
1369021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1370d0c0c9a1SMichael Neuling {
1371021424a1SMichael Ellerman 	static char *facility_strings[] = {
13722517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13732517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13742517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13752517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13762517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
13772517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
13782517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
13792517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1380021424a1SMichael Ellerman 	};
13812517617eSMichael Neuling 	char *facility = "unknown";
1382021424a1SMichael Ellerman 	u64 value;
1383c952c1c4SAnshuman Khandual 	u32 instword, rd;
13842517617eSMichael Neuling 	u8 status;
13852517617eSMichael Neuling 	bool hv;
1386021424a1SMichael Ellerman 
13872517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
13882517617eSMichael Neuling 	if (hv)
1389b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
13902517617eSMichael Neuling 	else
13912517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
13922517617eSMichael Neuling 
13932517617eSMichael Neuling 	status = value >> 56;
13942517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1395c952c1c4SAnshuman Khandual 		/*
1396c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1397c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1398c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1399c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1400c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1401c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1402c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1403c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1404c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1405c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1406c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1407c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1408c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1409c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
14102517617eSMichael Neuling 		 */
1411c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1412c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1413c952c1c4SAnshuman Khandual 			return;
1414c952c1c4SAnshuman Khandual 		}
1415c952c1c4SAnshuman Khandual 
1416c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1417c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1418c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1419c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1420c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
14212517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
14222517617eSMichael Neuling 			mtspr(SPRN_FSCR, value | FSCR_DSCR);
1423c952c1c4SAnshuman Khandual 		}
1424c952c1c4SAnshuman Khandual 
1425c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1426c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1427c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1428c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1429c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1430c952c1c4SAnshuman Khandual 				return;
1431c952c1c4SAnshuman Khandual 			}
1432c952c1c4SAnshuman Khandual 			regs->nip += 4;
1433c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1434c952c1c4SAnshuman Khandual 		}
14352517617eSMichael Neuling 		return;
1436b14b6260SMichael Ellerman 	}
1437b14b6260SMichael Ellerman 
14382517617eSMichael Neuling 	if ((status < ARRAY_SIZE(facility_strings)) &&
14392517617eSMichael Neuling 	    facility_strings[status])
14402517617eSMichael Neuling 		facility = facility_strings[status];
1441021424a1SMichael Ellerman 
1442d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1443d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1444d0c0c9a1SMichael Neuling 		local_irq_enable();
1445d0c0c9a1SMichael Neuling 
1446ee4ed6faSMichael Neuling 	pr_err_ratelimited(
1447ee4ed6faSMichael Neuling 		"%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
14482517617eSMichael Neuling 		hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
1449d0c0c9a1SMichael Neuling 
1450d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1451d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1452d0c0c9a1SMichael Neuling 		return;
1453d0c0c9a1SMichael Neuling 	}
1454d0c0c9a1SMichael Neuling 
1455021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1456d0c0c9a1SMichael Neuling }
14572517617eSMichael Neuling #endif
1458d0c0c9a1SMichael Neuling 
1459f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1460f54db641SMichael Neuling 
1461f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1462f54db641SMichael Neuling {
1463f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1464f54db641SMichael Neuling 
1465f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1466f54db641SMichael Neuling 		 regs->nip, regs->msr);
1467f54db641SMichael Neuling 
1468f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1469f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1470f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1471f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1472f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1473f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1474f54db641SMichael Neuling 	 */
1475d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1476f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1477f54db641SMichael Neuling 
1478f54db641SMichael Neuling 	/* Enable FP for the task: */
1479f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1480f54db641SMichael Neuling 
1481f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1482f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1483f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
14843ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
14853ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1486f54db641SMichael Neuling 	 */
14873ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
14883ac8ff1cSPaul Mackerras 
14893ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
14903ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
14913ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
14923ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
14933ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14943ac8ff1cSPaul Mackerras 	}
1495f54db641SMichael Neuling }
1496f54db641SMichael Neuling 
1497f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1498f54db641SMichael Neuling {
1499f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1500f54db641SMichael Neuling 	 * the same way.
1501f54db641SMichael Neuling 	 */
1502f54db641SMichael Neuling 
1503f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1504f54db641SMichael Neuling 		 "MSR=%lx\n",
1505f54db641SMichael Neuling 		 regs->nip, regs->msr);
1506d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1507f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
15083ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1509f54db641SMichael Neuling 	current->thread.used_vr = 1;
1510f54db641SMichael Neuling 
15113ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
15123ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
15133ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15143ac8ff1cSPaul Mackerras 	}
15153ac8ff1cSPaul Mackerras }
15163ac8ff1cSPaul Mackerras 
1517f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1518f54db641SMichael Neuling {
15193ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
15203ac8ff1cSPaul Mackerras 
1521f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1522f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1523f54db641SMichael Neuling 	 *
1524f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1525f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1526f54db641SMichael Neuling 	 */
1527f54db641SMichael Neuling 
1528f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1529f54db641SMichael Neuling 		 "MSR=%lx\n",
1530f54db641SMichael Neuling 		 regs->nip, regs->msr);
1531f54db641SMichael Neuling 
15323ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
15333ac8ff1cSPaul Mackerras 
15343ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
15353ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
15363ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15373ac8ff1cSPaul Mackerras 		return;
15383ac8ff1cSPaul Mackerras 	}
15393ac8ff1cSPaul Mackerras 
1540f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1541d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1542f54db641SMichael Neuling 
1543f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1544f54db641SMichael Neuling 		MSR_VSX;
15453ac8ff1cSPaul Mackerras 
15463ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
15473ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
15483ac8ff1cSPaul Mackerras 	 */
15493ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
15503ac8ff1cSPaul Mackerras 
15513ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
15523ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
15533ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
15543ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
1555f54db641SMichael Neuling }
1556f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1557f54db641SMichael Neuling 
1558dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1559dc1c1ca3SStephen Rothwell {
156069111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
156189713ed1SAnton Blanchard 
1562dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1563dc1c1ca3SStephen Rothwell }
1564dc1c1ca3SStephen Rothwell 
15658dad3f92SPaul Mackerras #ifdef CONFIG_8xx
156614cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
156714cf11afSPaul Mackerras {
156814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
156914cf11afSPaul Mackerras 
157014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
157114cf11afSPaul Mackerras 		debugger(regs);
15721eb2819dSLEROY Christophe 		die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
15731eb2819dSLEROY Christophe 			regs, SIGFPE);
157414cf11afSPaul Mackerras 	}
157514cf11afSPaul Mackerras 
15763a3b5aa6SKevin Hao 	if (!emulate_math(regs))
15773a3b5aa6SKevin Hao 		return;
15785fad293bSKumar Gala 
15795fad293bSKumar Gala 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
158014cf11afSPaul Mackerras }
15818dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
158214cf11afSPaul Mackerras 
1583172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
15843bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
15853bffb652SDave Kleikamp {
15863bffb652SDave Kleikamp 	int changed = 0;
15873bffb652SDave Kleikamp 	/*
15883bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
15893bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
15903bffb652SDave Kleikamp 	 */
15913bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
15923bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
15933bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
159451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
15953bffb652SDave Kleikamp #endif
15963bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
15973bffb652SDave Kleikamp 			     5);
15983bffb652SDave Kleikamp 		changed |= 0x01;
15993bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
16003bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
16013bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
16023bffb652SDave Kleikamp 			     6);
16033bffb652SDave Kleikamp 		changed |= 0x01;
16043bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
160551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
16063bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
16073bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
16083bffb652SDave Kleikamp 			     1);
16093bffb652SDave Kleikamp 		changed |= 0x01;
16103bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
161151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
16123bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
16133bffb652SDave Kleikamp 			     2);
16143bffb652SDave Kleikamp 		changed |= 0x01;
16153bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
161651ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
16173bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
16183bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
16193bffb652SDave Kleikamp 			     3);
16203bffb652SDave Kleikamp 		changed |= 0x01;
16213bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
162251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
16233bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
16243bffb652SDave Kleikamp 			     4);
16253bffb652SDave Kleikamp 		changed |= 0x01;
16263bffb652SDave Kleikamp 	}
16273bffb652SDave Kleikamp 	/*
16283bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
16293bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
16303bffb652SDave Kleikamp 	 * back on or not.
16313bffb652SDave Kleikamp 	 */
163251ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
163351ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
16343bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
16353bffb652SDave Kleikamp 	else
16363bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
163751ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16383bffb652SDave Kleikamp 
16393bffb652SDave Kleikamp 	if (changed & 0x01)
164051ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
16413bffb652SDave Kleikamp }
164214cf11afSPaul Mackerras 
1643f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
164414cf11afSPaul Mackerras {
164551ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
16463bffb652SDave Kleikamp 
1647ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1648ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1649ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1650ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1651ec097c84SRoland McGrath 	 */
1652ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1653ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1654ec097c84SRoland McGrath 
1655ec097c84SRoland McGrath 		/* Disable BT */
1656ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1657ec097c84SRoland McGrath 		/* Clear the BT event */
1658ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1659ec097c84SRoland McGrath 
1660ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1661ec097c84SRoland McGrath 		if (user_mode(regs)) {
166251ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
166351ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1664ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1665ec097c84SRoland McGrath 			return;
1666ec097c84SRoland McGrath 		}
1667ec097c84SRoland McGrath 
1668ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1669ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1670ec097c84SRoland McGrath 			return;
1671ec097c84SRoland McGrath 		}
1672ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1673ec097c84SRoland McGrath 			return;
1674ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
167514cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1676f8279621SKumar Gala 
167714cf11afSPaul Mackerras 		/* Disable instruction completion */
167814cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
167914cf11afSPaul Mackerras 		/* Clear the instruction completion event */
168014cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1681f8279621SKumar Gala 
1682f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1683f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
168414cf11afSPaul Mackerras 			return;
168514cf11afSPaul Mackerras 		}
1686f8279621SKumar Gala 
1687f8279621SKumar Gala 		if (debugger_sstep(regs))
1688f8279621SKumar Gala 			return;
1689f8279621SKumar Gala 
16903bffb652SDave Kleikamp 		if (user_mode(regs)) {
169151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
169251ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
169351ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
16943bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
16953bffb652SDave Kleikamp 			else
16963bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
169751ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16983bffb652SDave Kleikamp 		}
1699f8279621SKumar Gala 
1700f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
17013bffb652SDave Kleikamp 	} else
17023bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
170314cf11afSPaul Mackerras }
1704172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
170514cf11afSPaul Mackerras 
170614cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
170714cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
170814cf11afSPaul Mackerras {
170914cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
171014cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
171114cf11afSPaul Mackerras }
171214cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
171314cf11afSPaul Mackerras 
171414cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1715dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
171614cf11afSPaul Mackerras {
171714cf11afSPaul Mackerras 	int err;
171814cf11afSPaul Mackerras 
171914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
172014cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
172114cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
17228dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
172314cf11afSPaul Mackerras 	}
172414cf11afSPaul Mackerras 
1725dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1726dc1c1ca3SStephen Rothwell 
1727eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
172814cf11afSPaul Mackerras 	err = emulate_altivec(regs);
172914cf11afSPaul Mackerras 	if (err == 0) {
173014cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
173114cf11afSPaul Mackerras 		emulate_single_step(regs);
173214cf11afSPaul Mackerras 		return;
173314cf11afSPaul Mackerras 	}
173414cf11afSPaul Mackerras 
173514cf11afSPaul Mackerras 	if (err == -EFAULT) {
173614cf11afSPaul Mackerras 		/* got an error reading the instruction */
173714cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
173814cf11afSPaul Mackerras 	} else {
173914cf11afSPaul Mackerras 		/* didn't recognize the instruction */
174014cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
174176462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
174214cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1743de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
174414cf11afSPaul Mackerras 	}
174514cf11afSPaul Mackerras }
174614cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
174714cf11afSPaul Mackerras 
174814cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
174914cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
175014cf11afSPaul Mackerras 			   unsigned long error_code)
175114cf11afSPaul Mackerras {
175214cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
175314cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
175414cf11afSPaul Mackerras 	 * something smarter
175514cf11afSPaul Mackerras 	 */
175614cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
175714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
175814cf11afSPaul Mackerras 	return;
175914cf11afSPaul Mackerras }
176014cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
176114cf11afSPaul Mackerras 
176214cf11afSPaul Mackerras #ifdef CONFIG_SPE
176314cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
176414cf11afSPaul Mackerras {
17656a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
176614cf11afSPaul Mackerras 	unsigned long spefscr;
176714cf11afSPaul Mackerras 	int fpexc_mode;
176814cf11afSPaul Mackerras 	int code = 0;
17696a800f36SLiu Yu 	int err;
17706a800f36SLiu Yu 
1771685659eeSyu liu 	flush_spe_to_thread(current);
177214cf11afSPaul Mackerras 
177314cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
177414cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
177514cf11afSPaul Mackerras 
177614cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
177714cf11afSPaul Mackerras 		code = FPE_FLTOVF;
177814cf11afSPaul Mackerras 	}
177914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
178014cf11afSPaul Mackerras 		code = FPE_FLTUND;
178114cf11afSPaul Mackerras 	}
178214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
178314cf11afSPaul Mackerras 		code = FPE_FLTDIV;
178414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
178514cf11afSPaul Mackerras 		code = FPE_FLTINV;
178614cf11afSPaul Mackerras 	}
178714cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
178814cf11afSPaul Mackerras 		code = FPE_FLTRES;
178914cf11afSPaul Mackerras 
17906a800f36SLiu Yu 	err = do_spe_mathemu(regs);
17916a800f36SLiu Yu 	if (err == 0) {
17926a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17936a800f36SLiu Yu 		emulate_single_step(regs);
179414cf11afSPaul Mackerras 		return;
179514cf11afSPaul Mackerras 	}
17966a800f36SLiu Yu 
17976a800f36SLiu Yu 	if (err == -EFAULT) {
17986a800f36SLiu Yu 		/* got an error reading the instruction */
17996a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18006a800f36SLiu Yu 	} else if (err == -EINVAL) {
18016a800f36SLiu Yu 		/* didn't recognize the instruction */
18026a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18036a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18046a800f36SLiu Yu 	} else {
18056a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
18066a800f36SLiu Yu 	}
18076a800f36SLiu Yu 
18086a800f36SLiu Yu 	return;
18096a800f36SLiu Yu }
18106a800f36SLiu Yu 
18116a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
18126a800f36SLiu Yu {
18136a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
18146a800f36SLiu Yu 	int err;
18156a800f36SLiu Yu 
18166a800f36SLiu Yu 	preempt_disable();
18176a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
18186a800f36SLiu Yu 		giveup_spe(current);
18196a800f36SLiu Yu 	preempt_enable();
18206a800f36SLiu Yu 
18216a800f36SLiu Yu 	regs->nip -= 4;
18226a800f36SLiu Yu 	err = speround_handler(regs);
18236a800f36SLiu Yu 	if (err == 0) {
18246a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18256a800f36SLiu Yu 		emulate_single_step(regs);
18266a800f36SLiu Yu 		return;
18276a800f36SLiu Yu 	}
18286a800f36SLiu Yu 
18296a800f36SLiu Yu 	if (err == -EFAULT) {
18306a800f36SLiu Yu 		/* got an error reading the instruction */
18316a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18326a800f36SLiu Yu 	} else if (err == -EINVAL) {
18336a800f36SLiu Yu 		/* didn't recognize the instruction */
18346a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18356a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18366a800f36SLiu Yu 	} else {
18376a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
18386a800f36SLiu Yu 		return;
18396a800f36SLiu Yu 	}
18406a800f36SLiu Yu }
184114cf11afSPaul Mackerras #endif
184214cf11afSPaul Mackerras 
1843dc1c1ca3SStephen Rothwell /*
1844dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1845dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1846dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1847dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1848dc1c1ca3SStephen Rothwell  */
1849dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1850dc1c1ca3SStephen Rothwell {
1851dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1852dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1853dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1854dc1c1ca3SStephen Rothwell }
1855dc1c1ca3SStephen Rothwell 
18561e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
185714cf11afSPaul Mackerras /*
185814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
185914cf11afSPaul Mackerras  * spins until a reboot occurs
186014cf11afSPaul Mackerras  */
186114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
186214cf11afSPaul Mackerras {
186314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
186414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
186514cf11afSPaul Mackerras 	return;
186614cf11afSPaul Mackerras }
186714cf11afSPaul Mackerras 
186814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
186914cf11afSPaul Mackerras {
187014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
187114cf11afSPaul Mackerras 	WatchdogHandler(regs);
187214cf11afSPaul Mackerras }
187314cf11afSPaul Mackerras #endif
1874dc1c1ca3SStephen Rothwell 
1875dc1c1ca3SStephen Rothwell /*
1876dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1877dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1878dc1c1ca3SStephen Rothwell  */
1879dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1880dc1c1ca3SStephen Rothwell {
1881dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1882dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1883dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1884dc1c1ca3SStephen Rothwell }
188514cf11afSPaul Mackerras 
188614cf11afSPaul Mackerras void __init trap_init(void)
188714cf11afSPaul Mackerras {
188814cf11afSPaul Mackerras }
188980947e7cSGeert Uytterhoeven 
189080947e7cSGeert Uytterhoeven 
189180947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
189280947e7cSGeert Uytterhoeven 
189380947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
189480947e7cSGeert Uytterhoeven 
189580947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
189680947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
189780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
189880947e7cSGeert Uytterhoeven #endif
189980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
190080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
190180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
190280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
190380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
190480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
190580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
190680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
190780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
190880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1909a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
191080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
191180947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
191280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
191380947e7cSGeert Uytterhoeven #endif
191480947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
191580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
191680947e7cSGeert Uytterhoeven #endif
1917efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1918efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1919efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1920f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
1921efcac658SAlexey Kardashevskiy #endif
192280947e7cSGeert Uytterhoeven };
192380947e7cSGeert Uytterhoeven 
192480947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
192580947e7cSGeert Uytterhoeven 
192680947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
192780947e7cSGeert Uytterhoeven {
192876462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
192980947e7cSGeert Uytterhoeven 			    type);
193080947e7cSGeert Uytterhoeven }
193180947e7cSGeert Uytterhoeven 
193280947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
193380947e7cSGeert Uytterhoeven {
193480947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
193580947e7cSGeert Uytterhoeven 	unsigned int i;
193680947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
193780947e7cSGeert Uytterhoeven 
193880947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
193980947e7cSGeert Uytterhoeven 		return -ENODEV;
194080947e7cSGeert Uytterhoeven 
194180947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
194280947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
194380947e7cSGeert Uytterhoeven 	if (!dir)
194480947e7cSGeert Uytterhoeven 		return -ENOMEM;
194580947e7cSGeert Uytterhoeven 
194680947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
194780947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
194880947e7cSGeert Uytterhoeven 	if (!d)
194980947e7cSGeert Uytterhoeven 		goto fail;
195080947e7cSGeert Uytterhoeven 
195180947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
195280947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
195380947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
195480947e7cSGeert Uytterhoeven 		if (!d)
195580947e7cSGeert Uytterhoeven 			goto fail;
195680947e7cSGeert Uytterhoeven 	}
195780947e7cSGeert Uytterhoeven 
195880947e7cSGeert Uytterhoeven 	return 0;
195980947e7cSGeert Uytterhoeven 
196080947e7cSGeert Uytterhoeven fail:
196180947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
196280947e7cSGeert Uytterhoeven 	return -ENOMEM;
196380947e7cSGeert Uytterhoeven }
196480947e7cSGeert Uytterhoeven 
196580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
196680947e7cSGeert Uytterhoeven 
196780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1968