xref: /linux/arch/powerpc/kernel/traps.c (revision 47355040d2760566901057287b35d5f10e217e12)
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>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/mm.h>
2314cf11afSPaul Mackerras #include <linux/stddef.h>
2414cf11afSPaul Mackerras #include <linux/unistd.h>
258dad3f92SPaul Mackerras #include <linux/ptrace.h>
2614cf11afSPaul Mackerras #include <linux/user.h>
2714cf11afSPaul Mackerras #include <linux/interrupt.h>
2814cf11afSPaul Mackerras #include <linux/init.h>
298a39b05fSPaul Gortmaker #include <linux/extable.h>
308a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
318dad3f92SPaul Mackerras #include <linux/prctl.h>
3214cf11afSPaul Mackerras #include <linux/delay.h>
3314cf11afSPaul Mackerras #include <linux/kprobes.h>
34cc532915SMichael Ellerman #include <linux/kexec.h>
355474c120SMichael Hanselmann #include <linux/backlight.h>
3673c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
371eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3876462232SChristian Dietrich #include <linux/ratelimit.h>
39ba12eedeSLi Zhong #include <linux/context_tracking.h>
405080332cSMichael Neuling #include <linux/smp.h>
4114cf11afSPaul Mackerras 
4280947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4314cf11afSPaul Mackerras #include <asm/pgtable.h>
447c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
457644d581SMichael Ellerman #include <asm/debugfs.h>
4614cf11afSPaul Mackerras #include <asm/io.h>
4786417780SPaul Mackerras #include <asm/machdep.h>
4886417780SPaul Mackerras #include <asm/rtas.h>
49f7f6f4feSDavid Gibson #include <asm/pmc.h>
5014cf11afSPaul Mackerras #include <asm/reg.h>
5114cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5214cf11afSPaul Mackerras #include <asm/backlight.h>
5314cf11afSPaul Mackerras #endif
54dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5586417780SPaul Mackerras #include <asm/firmware.h>
56dc1c1ca3SStephen Rothwell #include <asm/processor.h>
576ce6c629SMichael Neuling #include <asm/tm.h>
58dc1c1ca3SStephen Rothwell #endif
59c0ce7d08SDavid Wilder #include <asm/kexec.h>
6016c57b36SKumar Gala #include <asm/ppc-opcode.h>
61cce1f106SShaohui Xie #include <asm/rio.h>
62ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
63ae3a197eSDavid Howells #include <asm/switch_to.h>
64f54db641SMichael Neuling #include <asm/tm.h>
65ae3a197eSDavid Howells #include <asm/debug.h>
6642f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
67fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
684e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
696cc89badSNaveen N. Rao #include <asm/kprobes.h>
70dc1c1ca3SStephen Rothwell 
71da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
725be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
735be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
745be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
755be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
765be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
779422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
785be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7914cf11afSPaul Mackerras 
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
859422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8714cf11afSPaul Mackerras #endif
8814cf11afSPaul Mackerras 
898b3c34cfSMichael Neuling /* Transactional Memory trap debug */
908b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
918b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
928b3c34cfSMichael Neuling #else
938b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
948b3c34cfSMichael Neuling #endif
958b3c34cfSMichael Neuling 
9614cf11afSPaul Mackerras /*
9714cf11afSPaul Mackerras  * Trap & Exception support
9814cf11afSPaul Mackerras  */
9914cf11afSPaul Mackerras 
1006031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1016031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1026031d9d9Santon@samba.org {
1036031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1046031d9d9Santon@samba.org 	if (pmac_backlight) {
1056031d9d9Santon@samba.org 		struct backlight_properties *props;
1066031d9d9Santon@samba.org 
1076031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1086031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1096031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1106031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1116031d9d9Santon@samba.org 	}
1126031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1136031d9d9Santon@samba.org }
1146031d9d9Santon@samba.org #else
1156031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1166031d9d9Santon@samba.org #endif
1176031d9d9Santon@samba.org 
1186fcd6baaSNicholas Piggin /*
1196fcd6baaSNicholas Piggin  * If oops/die is expected to crash the machine, return true here.
1206fcd6baaSNicholas Piggin  *
1216fcd6baaSNicholas Piggin  * This should not be expected to be 100% accurate, there may be
1226fcd6baaSNicholas Piggin  * notifiers registered or other unexpected conditions that may bring
1236fcd6baaSNicholas Piggin  * down the kernel. Or if the current process in the kernel is holding
1246fcd6baaSNicholas Piggin  * locks or has other critical state, the kernel may become effectively
1256fcd6baaSNicholas Piggin  * unusable anyway.
1266fcd6baaSNicholas Piggin  */
1276fcd6baaSNicholas Piggin bool die_will_crash(void)
1286fcd6baaSNicholas Piggin {
1296fcd6baaSNicholas Piggin 	if (should_fadump_crash())
1306fcd6baaSNicholas Piggin 		return true;
1316fcd6baaSNicholas Piggin 	if (kexec_should_crash(current))
1326fcd6baaSNicholas Piggin 		return true;
1336fcd6baaSNicholas Piggin 	if (in_interrupt() || panic_on_oops ||
1346fcd6baaSNicholas Piggin 			!current->pid || is_global_init(current))
1356fcd6baaSNicholas Piggin 		return true;
1366fcd6baaSNicholas Piggin 
1376fcd6baaSNicholas Piggin 	return false;
1386fcd6baaSNicholas Piggin }
1396fcd6baaSNicholas Piggin 
140760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
141760ca4dcSAnton Blanchard static int die_owner = -1;
142760ca4dcSAnton Blanchard static unsigned int die_nest_count;
143c0ce7d08SDavid Wilder static int die_counter;
144760ca4dcSAnton Blanchard 
14503465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
146760ca4dcSAnton Blanchard {
147760ca4dcSAnton Blanchard 	int cpu;
14834c2a14fSanton@samba.org 	unsigned long flags;
14914cf11afSPaul Mackerras 
150293e4688Santon@samba.org 	oops_enter();
151293e4688Santon@samba.org 
152760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
153760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
154760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
155760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
156760ca4dcSAnton Blanchard 		if (cpu == die_owner)
157760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
158760ca4dcSAnton Blanchard 		else
159760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
160760ca4dcSAnton Blanchard 	}
161760ca4dcSAnton Blanchard 	die_nest_count++;
162760ca4dcSAnton Blanchard 	die_owner = cpu;
16314cf11afSPaul Mackerras 	console_verbose();
16414cf11afSPaul Mackerras 	bust_spinlocks(1);
1656031d9d9Santon@samba.org 	if (machine_is(powermac))
1666031d9d9Santon@samba.org 		pmac_backlight_unblank();
167760ca4dcSAnton Blanchard 	return flags;
16834c2a14fSanton@samba.org }
16903465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
1705474c120SMichael Hanselmann 
17103465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
172760ca4dcSAnton Blanchard 			       int signr)
173760ca4dcSAnton Blanchard {
17414cf11afSPaul Mackerras 	bust_spinlocks(0);
175373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
176760ca4dcSAnton Blanchard 	die_nest_count--;
17758154c8cSAnton Blanchard 	oops_exit();
17858154c8cSAnton Blanchard 	printk("\n");
1797458e8b2SNicholas Piggin 	if (!die_nest_count) {
180760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
1817458e8b2SNicholas Piggin 		die_owner = -1;
182760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
1837458e8b2SNicholas Piggin 	}
184760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
185cc532915SMichael Ellerman 
186ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
187ebaeb5aeSMahesh Salgaonkar 
1884388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
189cc532915SMichael Ellerman 		crash_kexec(regs);
1909b00ac06SAnton Blanchard 
191760ca4dcSAnton Blanchard 	if (!signr)
192760ca4dcSAnton Blanchard 		return;
193760ca4dcSAnton Blanchard 
19458154c8cSAnton Blanchard 	/*
19558154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
19658154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
19758154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
19858154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
19958154c8cSAnton Blanchard 	 */
20058154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
20158154c8cSAnton Blanchard 	    is_global_init(current)) {
20258154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
20358154c8cSAnton Blanchard 	}
20458154c8cSAnton Blanchard 
20514cf11afSPaul Mackerras 	if (in_interrupt())
20614cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
207cea6a4baSHorms 	if (panic_on_oops)
208012c437dSHorms 		panic("Fatal exception");
209760ca4dcSAnton Blanchard 	do_exit(signr);
210760ca4dcSAnton Blanchard }
21103465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
212cea6a4baSHorms 
21303465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
214760ca4dcSAnton Blanchard {
215760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2162e82ca3cSMichael Ellerman 
2172e82ca3cSMichael Ellerman 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
2182e82ca3cSMichael Ellerman 		printk("LE ");
2192e82ca3cSMichael Ellerman 	else
2202e82ca3cSMichael Ellerman 		printk("BE ");
2212e82ca3cSMichael Ellerman 
2221c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_PREEMPT))
22372c0d9eeSMichael Ellerman 		pr_cont("PREEMPT ");
2241c56cd8eSMichael Ellerman 
2251c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_SMP))
22672c0d9eeSMichael Ellerman 		pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
2271c56cd8eSMichael Ellerman 
228e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
22972c0d9eeSMichael Ellerman 		pr_cont("DEBUG_PAGEALLOC ");
2301c56cd8eSMichael Ellerman 
2311c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_NUMA))
23272c0d9eeSMichael Ellerman 		pr_cont("NUMA ");
2331c56cd8eSMichael Ellerman 
23472c0d9eeSMichael Ellerman 	pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
235760ca4dcSAnton Blanchard 
236760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
237760ca4dcSAnton Blanchard 		return 1;
238760ca4dcSAnton Blanchard 
239760ca4dcSAnton Blanchard 	print_modules();
240760ca4dcSAnton Blanchard 	show_regs(regs);
24114cf11afSPaul Mackerras 
24214cf11afSPaul Mackerras 	return 0;
24314cf11afSPaul Mackerras }
24403465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
24514cf11afSPaul Mackerras 
246760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
247760ca4dcSAnton Blanchard {
2486f44b20eSNicholas Piggin 	unsigned long flags;
249760ca4dcSAnton Blanchard 
2506f44b20eSNicholas Piggin 	if (debugger(regs))
2516f44b20eSNicholas Piggin 		return;
2526f44b20eSNicholas Piggin 
2536f44b20eSNicholas Piggin 	flags = oops_begin(regs);
254760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
255760ca4dcSAnton Blanchard 		err = 0;
256760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
257760ca4dcSAnton Blanchard }
25815770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
259760ca4dcSAnton Blanchard 
26025baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
26125baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
26225baa35bSOleg Nesterov {
26325baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
26425baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
26525baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
26625baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
26725baa35bSOleg Nesterov }
26825baa35bSOleg Nesterov 
26914cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
27014cf11afSPaul Mackerras {
27114cf11afSPaul Mackerras 	siginfo_t info;
272d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
273d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
274d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
275d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
27614cf11afSPaul Mackerras 
27714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
278760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
27914cf11afSPaul Mackerras 		return;
280760ca4dcSAnton Blanchard 	}
281760ca4dcSAnton Blanchard 
282760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
28376462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
284d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
285d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
28614cf11afSPaul Mackerras 	}
28714cf11afSPaul Mackerras 
288a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2899f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2909f2f79e3SBenjamin Herrenschmidt 
29141ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
29214cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
29314cf11afSPaul Mackerras 	info.si_signo = signr;
29414cf11afSPaul Mackerras 	info.si_code = code;
29514cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
29614cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
29714cf11afSPaul Mackerras }
29814cf11afSPaul Mackerras 
29914cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
30014cf11afSPaul Mackerras {
3012b4f3ac5SNicholas Piggin 	/*
3022b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
3032b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
3042b4f3ac5SNicholas Piggin 	 */
3052b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
3062b4f3ac5SNicholas Piggin 	if (!nested)
3072b4f3ac5SNicholas Piggin 		nmi_enter();
3082b4f3ac5SNicholas Piggin 
309ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
310ca41ad43SNicholas Piggin 
31114cf11afSPaul Mackerras 	/* See if any machine dependent calls */
312c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
313c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
314c4f3b52cSNicholas Piggin 			goto out;
315c902be71SArnd Bergmann 	}
31614cf11afSPaul Mackerras 
3174388c9b3SNicholas Piggin 	if (debugger(regs))
3184388c9b3SNicholas Piggin 		goto out;
3194388c9b3SNicholas Piggin 
3204388c9b3SNicholas Piggin 	/*
3214388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
3224388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
3234388c9b3SNicholas Piggin 	 * registered).
3244388c9b3SNicholas Piggin 	 */
3254388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
3264388c9b3SNicholas Piggin 
3274388c9b3SNicholas Piggin 	crash_kexec(regs);
3284388c9b3SNicholas Piggin 
3294388c9b3SNicholas Piggin 	/*
3304388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
3314388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
3324388c9b3SNicholas Piggin 	 * code.
3334388c9b3SNicholas Piggin 	 */
3344388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
3354388c9b3SNicholas Piggin 
3364388c9b3SNicholas Piggin 	/*
3374388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
3384388c9b3SNicholas Piggin 	 * panic.
3394388c9b3SNicholas Piggin 	 */
3404388c9b3SNicholas Piggin 	__die("System Reset", regs, SIGABRT);
3414388c9b3SNicholas Piggin 
3424388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
3434388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
3444388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
34514cf11afSPaul Mackerras 
346c4f3b52cSNicholas Piggin out:
347c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
348c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
349c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
3504388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable nested System Reset");
351c4f3b52cSNicholas Piggin #endif
35214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
35314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
3544388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable System Reset");
35514cf11afSPaul Mackerras 
3562b4f3ac5SNicholas Piggin 	if (!nested)
3572b4f3ac5SNicholas Piggin 		nmi_exit();
3582b4f3ac5SNicholas Piggin 
35914cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
36014cf11afSPaul Mackerras }
3611e9b4507SMahesh Salgaonkar 
36214cf11afSPaul Mackerras /*
36314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
36414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
36514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
36614cf11afSPaul Mackerras  * table.
36714cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
36814cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
36914cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
37014cf11afSPaul Mackerras  *  -- paulus.
37114cf11afSPaul Mackerras  */
37214cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
37314cf11afSPaul Mackerras {
37468a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
37514cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
37614cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
37714cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
37814cf11afSPaul Mackerras 
37914cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
38014cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
38114cf11afSPaul Mackerras 		/*
38214cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
38314cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
38414cf11afSPaul Mackerras 		 * As the address is in the exception table
38514cf11afSPaul Mackerras 		 * we should be able to read the instr there.
38614cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
38714cf11afSPaul Mackerras 		 * load or store.
38814cf11afSPaul Mackerras 		 */
389ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
39014cf11afSPaul Mackerras 			nip -= 2;
391ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
39214cf11afSPaul Mackerras 			--nip;
393ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
39414cf11afSPaul Mackerras 			unsigned int rb;
39514cf11afSPaul Mackerras 
39614cf11afSPaul Mackerras 			--nip;
39714cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
39814cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
39914cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
40014cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
40114cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
40261a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
40314cf11afSPaul Mackerras 			return 1;
40414cf11afSPaul Mackerras 		}
40514cf11afSPaul Mackerras 	}
40668a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
40714cf11afSPaul Mackerras 	return 0;
40814cf11afSPaul Mackerras }
40914cf11afSPaul Mackerras 
410172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
41114cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
41214cf11afSPaul Mackerras    is in the ESR. */
41314cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
41414cf11afSPaul Mackerras #define REASON_FP		ESR_FP
41514cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
41614cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
41714cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
41814cf11afSPaul Mackerras 
41914cf11afSPaul Mackerras /* single-step stuff */
42051ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
42151ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
42214cf11afSPaul Mackerras 
42314cf11afSPaul Mackerras #else
42414cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
42514cf11afSPaul Mackerras    exception is in the MSR. */
42614cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
427d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
428d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
429d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
430d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
431d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
43214cf11afSPaul Mackerras 
43314cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
43414cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
43514cf11afSPaul Mackerras #endif
43614cf11afSPaul Mackerras 
4370d0935b3SMichael Ellerman #if defined(CONFIG_E500)
438fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
439fe04b112SScott Wood {
440fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
441a4e89ffbSMatt Weber 	unsigned long pvr = mfspr(SPRN_PVR);
442fe04b112SScott Wood 	unsigned long reason = mcsr;
443fe04b112SScott Wood 	int recoverable = 1;
444fe04b112SScott Wood 
44582a9a480SScott Wood 	if (reason & MCSR_LD) {
446cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
447cce1f106SShaohui Xie 		if (recoverable == 1)
448cce1f106SShaohui Xie 			goto silent_out;
449cce1f106SShaohui Xie 	}
450cce1f106SShaohui Xie 
451fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
452fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
453fe04b112SScott Wood 
454fe04b112SScott Wood 	if (reason & MCSR_MCP)
455fe04b112SScott Wood 		printk("Machine Check Signal\n");
456fe04b112SScott Wood 
457fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
458fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
459fe04b112SScott Wood 
460fe04b112SScott Wood 		/*
461fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
462fe04b112SScott Wood 		 */
463fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
464fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
465fe04b112SScott Wood 			;
466fe04b112SScott Wood 
467fe04b112SScott Wood 		/*
468fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
469fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
470fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
471fe04b112SScott Wood 		 */
472fe04b112SScott Wood 		reason &= ~MCSR_IF;
473fe04b112SScott Wood 	}
474fe04b112SScott Wood 
475fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
476fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
47737caf9f2SKumar Gala 
47837caf9f2SKumar Gala 		/*
47937caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
48037caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
48137caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
48237caf9f2SKumar Gala 		 */
483a4e89ffbSMatt Weber 		/* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
484a4e89ffbSMatt Weber 		 * is not implemented but L1 data cache always runs in write
485a4e89ffbSMatt Weber 		 * shadow mode. Hence on data cache parity errors HW will
486a4e89ffbSMatt Weber 		 * automatically invalidate the L1 Data Cache.
487a4e89ffbSMatt Weber 		 */
488a4e89ffbSMatt Weber 		if (PVR_VER(pvr) != PVR_VER_E6500) {
48937caf9f2SKumar Gala 			if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
490fe04b112SScott Wood 				recoverable = 0;
491fe04b112SScott Wood 		}
492a4e89ffbSMatt Weber 	}
493fe04b112SScott Wood 
494fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
495fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
496fe04b112SScott Wood 		recoverable = 0;
497fe04b112SScott Wood 	}
498fe04b112SScott Wood 
499fe04b112SScott Wood 	if (reason & MCSR_NMI)
500fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
501fe04b112SScott Wood 
502fe04b112SScott Wood 	if (reason & MCSR_IF) {
503fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
504fe04b112SScott Wood 		recoverable = 0;
505fe04b112SScott Wood 	}
506fe04b112SScott Wood 
507fe04b112SScott Wood 	if (reason & MCSR_LD) {
508fe04b112SScott Wood 		printk("Load Error Report\n");
509fe04b112SScott Wood 		recoverable = 0;
510fe04b112SScott Wood 	}
511fe04b112SScott Wood 
512fe04b112SScott Wood 	if (reason & MCSR_ST) {
513fe04b112SScott Wood 		printk("Store Error Report\n");
514fe04b112SScott Wood 		recoverable = 0;
515fe04b112SScott Wood 	}
516fe04b112SScott Wood 
517fe04b112SScott Wood 	if (reason & MCSR_LDG) {
518fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
519fe04b112SScott Wood 		recoverable = 0;
520fe04b112SScott Wood 	}
521fe04b112SScott Wood 
522fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
523fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
524fe04b112SScott Wood 
525fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
526fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
527fe04b112SScott Wood 		recoverable = 0;
528fe04b112SScott Wood 	}
529fe04b112SScott Wood 
530fe04b112SScott Wood 	if (reason & MCSR_MAV) {
531fe04b112SScott Wood 		u64 addr;
532fe04b112SScott Wood 
533fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
534fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
535fe04b112SScott Wood 
536fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
537fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
538fe04b112SScott Wood 	}
539fe04b112SScott Wood 
540cce1f106SShaohui Xie silent_out:
541fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
542fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
543fe04b112SScott Wood }
544fe04b112SScott Wood 
54547c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
54647c0bd1aSBenjamin Herrenschmidt {
54742bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
54847c0bd1aSBenjamin Herrenschmidt 
549cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
550cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
551cce1f106SShaohui Xie 			return 1;
5524e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5534e0e3435SHongtao Jia 			return 1;
554cce1f106SShaohui Xie 	}
555cce1f106SShaohui Xie 
55614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
55714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
55814cf11afSPaul Mackerras 
55914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
56014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
56114cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
56214cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
56314cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
56414cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
56514cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
56614cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
56714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
56814cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
56914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
57014cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
57114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
57214cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
57314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
57414cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
57514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
57614cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
57714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
578c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
57914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
58014cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
58114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
58214cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
58347c0bd1aSBenjamin Herrenschmidt 
58447c0bd1aSBenjamin Herrenschmidt 	return 0;
58547c0bd1aSBenjamin Herrenschmidt }
5864490c06bSKumar Gala 
5874490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5884490c06bSKumar Gala {
5894490c06bSKumar Gala 	return 0;
5904490c06bSKumar Gala }
59114cf11afSPaul Mackerras #elif defined(CONFIG_E200)
59247c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
59347c0bd1aSBenjamin Herrenschmidt {
59442bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
59547c0bd1aSBenjamin Herrenschmidt 
59614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
59714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
59814cf11afSPaul Mackerras 
59914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
60014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
60114cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
60214cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
60314cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
60414cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
60514cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
60614cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
60714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
60814cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
60914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
61014cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
61114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
61214cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
61347c0bd1aSBenjamin Herrenschmidt 
61447c0bd1aSBenjamin Herrenschmidt 	return 0;
61547c0bd1aSBenjamin Herrenschmidt }
6167f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
61747c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
61847c0bd1aSBenjamin Herrenschmidt {
61942bff234SMichael Ellerman 	unsigned long reason = regs->msr;
62047c0bd1aSBenjamin Herrenschmidt 
62114cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62214cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
62314cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
62414cf11afSPaul Mackerras 	case 0x80000:
62514cf11afSPaul Mackerras 		printk("Machine check signal\n");
62614cf11afSPaul Mackerras 		break;
62714cf11afSPaul Mackerras 	case 0:		/* for 601 */
62814cf11afSPaul Mackerras 	case 0x40000:
62914cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
63014cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
63114cf11afSPaul Mackerras 		break;
63214cf11afSPaul Mackerras 	case 0x20000:
63314cf11afSPaul Mackerras 		printk("Data parity error signal\n");
63414cf11afSPaul Mackerras 		break;
63514cf11afSPaul Mackerras 	case 0x10000:
63614cf11afSPaul Mackerras 		printk("Address parity error signal\n");
63714cf11afSPaul Mackerras 		break;
63814cf11afSPaul Mackerras 	case 0x20000000:
63914cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
64014cf11afSPaul Mackerras 		break;
64114cf11afSPaul Mackerras 	case 0x40000000:
64214cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
64314cf11afSPaul Mackerras 		break;
64414cf11afSPaul Mackerras 	case 0x00100000:
64514cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
64614cf11afSPaul Mackerras 		break;
64714cf11afSPaul Mackerras 	default:
64814cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
64914cf11afSPaul Mackerras 	}
65075918a4bSOlof Johansson 	return 0;
65175918a4bSOlof Johansson }
65247c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
65375918a4bSOlof Johansson 
65475918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
65575918a4bSOlof Johansson {
65675918a4bSOlof Johansson 	int recover = 0;
657b96672ddSNicholas Piggin 	bool nested = in_nmi();
658b96672ddSNicholas Piggin 	if (!nested)
659b96672ddSNicholas Piggin 		nmi_enter();
66075918a4bSOlof Johansson 
661f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
662f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
66369111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
66489713ed1SAnton Blanchard 
665d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
666d93b0ac0SMahesh Salgaonkar 
66747c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
66847c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
66947c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
67047c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
67147c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
67247c0bd1aSBenjamin Herrenschmidt 	 */
67375918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
67475918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
67547c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
67647c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
67775918a4bSOlof Johansson 
67847c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
679ba12eedeSLi Zhong 		goto bail;
68075918a4bSOlof Johansson 
681a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
682ba12eedeSLi Zhong 		goto bail;
68375918a4bSOlof Johansson 
68475918a4bSOlof Johansson 	if (check_io_access(regs))
685ba12eedeSLi Zhong 		goto bail;
68675918a4bSOlof Johansson 
6878dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
68814cf11afSPaul Mackerras 
68914cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
69014cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
691b96672ddSNicholas Piggin 		nmi_panic(regs, "Unrecoverable Machine check");
692ba12eedeSLi Zhong 
693ba12eedeSLi Zhong bail:
694b96672ddSNicholas Piggin 	if (!nested)
695b96672ddSNicholas Piggin 		nmi_exit();
69614cf11afSPaul Mackerras }
69714cf11afSPaul Mackerras 
69814cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
69914cf11afSPaul Mackerras {
70014cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
70114cf11afSPaul Mackerras }
70214cf11afSPaul Mackerras 
7035080332cSMichael Neuling #ifdef CONFIG_VSX
7045080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs)
7055080332cSMichael Neuling {
7065080332cSMichael Neuling 	unsigned int ra, rb, t, i, sel, instr, rc;
7075080332cSMichael Neuling 	const void __user *addr;
7085080332cSMichael Neuling 	u8 vbuf[16], *vdst;
7095080332cSMichael Neuling 	unsigned long ea, msr, msr_mask;
7105080332cSMichael Neuling 	bool swap;
7115080332cSMichael Neuling 
7125080332cSMichael Neuling 	if (__get_user_inatomic(instr, (unsigned int __user *)regs->nip))
7135080332cSMichael Neuling 		return;
7145080332cSMichael Neuling 
7155080332cSMichael Neuling 	/*
7165080332cSMichael Neuling 	 * lxvb16x	opcode: 0x7c0006d8
7175080332cSMichael Neuling 	 * lxvd2x	opcode: 0x7c000698
7185080332cSMichael Neuling 	 * lxvh8x	opcode: 0x7c000658
7195080332cSMichael Neuling 	 * lxvw4x	opcode: 0x7c000618
7205080332cSMichael Neuling 	 */
7215080332cSMichael Neuling 	if ((instr & 0xfc00073e) != 0x7c000618) {
7225080332cSMichael Neuling 		pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
7235080332cSMichael Neuling 			 " instr=%08x\n",
7245080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7255080332cSMichael Neuling 			 regs->nip, instr);
7265080332cSMichael Neuling 		return;
7275080332cSMichael Neuling 	}
7285080332cSMichael Neuling 
7295080332cSMichael Neuling 	/* Grab vector registers into the task struct */
7305080332cSMichael Neuling 	msr = regs->msr; /* Grab msr before we flush the bits */
7315080332cSMichael Neuling 	flush_vsx_to_thread(current);
7325080332cSMichael Neuling 	enable_kernel_altivec();
7335080332cSMichael Neuling 
7345080332cSMichael Neuling 	/*
7355080332cSMichael Neuling 	 * Is userspace running with a different endian (this is rare but
7365080332cSMichael Neuling 	 * not impossible)
7375080332cSMichael Neuling 	 */
7385080332cSMichael Neuling 	swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
7395080332cSMichael Neuling 
7405080332cSMichael Neuling 	/* Decode the instruction */
7415080332cSMichael Neuling 	ra = (instr >> 16) & 0x1f;
7425080332cSMichael Neuling 	rb = (instr >> 11) & 0x1f;
7435080332cSMichael Neuling 	t = (instr >> 21) & 0x1f;
7445080332cSMichael Neuling 	if (instr & 1)
7455080332cSMichael Neuling 		vdst = (u8 *)&current->thread.vr_state.vr[t];
7465080332cSMichael Neuling 	else
7475080332cSMichael Neuling 		vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
7485080332cSMichael Neuling 
7495080332cSMichael Neuling 	/* Grab the vector address */
7505080332cSMichael Neuling 	ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
7515080332cSMichael Neuling 	if (is_32bit_task())
7525080332cSMichael Neuling 		ea &= 0xfffffffful;
7535080332cSMichael Neuling 	addr = (__force const void __user *)ea;
7545080332cSMichael Neuling 
7555080332cSMichael Neuling 	/* Check it */
7565080332cSMichael Neuling 	if (!access_ok(VERIFY_READ, addr, 16)) {
7575080332cSMichael Neuling 		pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
7585080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
7595080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7605080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
7615080332cSMichael Neuling 		return;
7625080332cSMichael Neuling 	}
7635080332cSMichael Neuling 
7645080332cSMichael Neuling 	/* Read the vector */
7655080332cSMichael Neuling 	rc = 0;
7665080332cSMichael Neuling 	if ((unsigned long)addr & 0xfUL)
7675080332cSMichael Neuling 		/* unaligned case */
7685080332cSMichael Neuling 		rc = __copy_from_user_inatomic(vbuf, addr, 16);
7695080332cSMichael Neuling 	else
7705080332cSMichael Neuling 		__get_user_atomic_128_aligned(vbuf, addr, rc);
7715080332cSMichael Neuling 	if (rc) {
7725080332cSMichael Neuling 		pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
7735080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
7745080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7755080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
7765080332cSMichael Neuling 		return;
7775080332cSMichael Neuling 	}
7785080332cSMichael Neuling 
7795080332cSMichael Neuling 	pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
7805080332cSMichael Neuling 		 " instr=%08x addr=%016lx\n",
7815080332cSMichael Neuling 		 smp_processor_id(), current->comm, current->pid, regs->nip,
7825080332cSMichael Neuling 		 instr, (unsigned long) addr);
7835080332cSMichael Neuling 
7845080332cSMichael Neuling 	/* Grab instruction "selector" */
7855080332cSMichael Neuling 	sel = (instr >> 6) & 3;
7865080332cSMichael Neuling 
7875080332cSMichael Neuling 	/*
7885080332cSMichael Neuling 	 * Check to make sure the facility is actually enabled. This
7895080332cSMichael Neuling 	 * could happen if we get a false positive hit.
7905080332cSMichael Neuling 	 *
7915080332cSMichael Neuling 	 * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
7925080332cSMichael Neuling 	 * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
7935080332cSMichael Neuling 	 */
7945080332cSMichael Neuling 	msr_mask = MSR_VSX;
7955080332cSMichael Neuling 	if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
7965080332cSMichael Neuling 		msr_mask = MSR_VEC;
7975080332cSMichael Neuling 	if (!(msr & msr_mask)) {
7985080332cSMichael Neuling 		pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
7995080332cSMichael Neuling 			 " instr=%08x msr:%016lx\n",
8005080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8015080332cSMichael Neuling 			 regs->nip, instr, msr);
8025080332cSMichael Neuling 		return;
8035080332cSMichael Neuling 	}
8045080332cSMichael Neuling 
8055080332cSMichael Neuling 	/* Do logging here before we modify sel based on endian */
8065080332cSMichael Neuling 	switch (sel) {
8075080332cSMichael Neuling 	case 0:	/* lxvw4x */
8085080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvw4x, regs);
8095080332cSMichael Neuling 		break;
8105080332cSMichael Neuling 	case 1: /* lxvh8x */
8115080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvh8x, regs);
8125080332cSMichael Neuling 		break;
8135080332cSMichael Neuling 	case 2: /* lxvd2x */
8145080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvd2x, regs);
8155080332cSMichael Neuling 		break;
8165080332cSMichael Neuling 	case 3: /* lxvb16x */
8175080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvb16x, regs);
8185080332cSMichael Neuling 		break;
8195080332cSMichael Neuling 	}
8205080332cSMichael Neuling 
8215080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__
8225080332cSMichael Neuling 	/*
8235080332cSMichael Neuling 	 * An LE kernel stores the vector in the task struct as an LE
8245080332cSMichael Neuling 	 * byte array (effectively swapping both the components and
8255080332cSMichael Neuling 	 * the content of the components). Those instructions expect
8265080332cSMichael Neuling 	 * the components to remain in ascending address order, so we
8275080332cSMichael Neuling 	 * swap them back.
8285080332cSMichael Neuling 	 *
8295080332cSMichael Neuling 	 * If we are running a BE user space, the expectation is that
8305080332cSMichael Neuling 	 * of a simple memcpy, so forcing the emulation to look like
8315080332cSMichael Neuling 	 * a lxvb16x should do the trick.
8325080332cSMichael Neuling 	 */
8335080332cSMichael Neuling 	if (swap)
8345080332cSMichael Neuling 		sel = 3;
8355080332cSMichael Neuling 
8365080332cSMichael Neuling 	switch (sel) {
8375080332cSMichael Neuling 	case 0:	/* lxvw4x */
8385080332cSMichael Neuling 		for (i = 0; i < 4; i++)
8395080332cSMichael Neuling 			((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
8405080332cSMichael Neuling 		break;
8415080332cSMichael Neuling 	case 1: /* lxvh8x */
8425080332cSMichael Neuling 		for (i = 0; i < 8; i++)
8435080332cSMichael Neuling 			((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
8445080332cSMichael Neuling 		break;
8455080332cSMichael Neuling 	case 2: /* lxvd2x */
8465080332cSMichael Neuling 		for (i = 0; i < 2; i++)
8475080332cSMichael Neuling 			((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
8485080332cSMichael Neuling 		break;
8495080332cSMichael Neuling 	case 3: /* lxvb16x */
8505080332cSMichael Neuling 		for (i = 0; i < 16; i++)
8515080332cSMichael Neuling 			vdst[i] = vbuf[15-i];
8525080332cSMichael Neuling 		break;
8535080332cSMichael Neuling 	}
8545080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */
8555080332cSMichael Neuling 	/* On a big endian kernel, a BE userspace only needs a memcpy */
8565080332cSMichael Neuling 	if (!swap)
8575080332cSMichael Neuling 		sel = 3;
8585080332cSMichael Neuling 
8595080332cSMichael Neuling 	/* Otherwise, we need to swap the content of the components */
8605080332cSMichael Neuling 	switch (sel) {
8615080332cSMichael Neuling 	case 0:	/* lxvw4x */
8625080332cSMichael Neuling 		for (i = 0; i < 4; i++)
8635080332cSMichael Neuling 			((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
8645080332cSMichael Neuling 		break;
8655080332cSMichael Neuling 	case 1: /* lxvh8x */
8665080332cSMichael Neuling 		for (i = 0; i < 8; i++)
8675080332cSMichael Neuling 			((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
8685080332cSMichael Neuling 		break;
8695080332cSMichael Neuling 	case 2: /* lxvd2x */
8705080332cSMichael Neuling 		for (i = 0; i < 2; i++)
8715080332cSMichael Neuling 			((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
8725080332cSMichael Neuling 		break;
8735080332cSMichael Neuling 	case 3: /* lxvb16x */
8745080332cSMichael Neuling 		memcpy(vdst, vbuf, 16);
8755080332cSMichael Neuling 		break;
8765080332cSMichael Neuling 	}
8775080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */
8785080332cSMichael Neuling 
8795080332cSMichael Neuling 	/* Go to next instruction */
8805080332cSMichael Neuling 	regs->nip += 4;
8815080332cSMichael Neuling }
8825080332cSMichael Neuling #endif /* CONFIG_VSX */
8835080332cSMichael Neuling 
8840869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
8850869b6fdSMahesh Salgaonkar {
8860869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
8870869b6fdSMahesh Salgaonkar 
8880869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
8890869b6fdSMahesh Salgaonkar 	irq_enter();
8900869b6fdSMahesh Salgaonkar 
8915080332cSMichael Neuling #ifdef CONFIG_VSX
8925080332cSMichael Neuling 	/* Real mode flagged P9 special emu is needed */
8935080332cSMichael Neuling 	if (local_paca->hmi_p9_special_emu) {
8945080332cSMichael Neuling 		local_paca->hmi_p9_special_emu = 0;
8955080332cSMichael Neuling 
8965080332cSMichael Neuling 		/*
8975080332cSMichael Neuling 		 * We don't want to take page faults while doing the
8985080332cSMichael Neuling 		 * emulation, we just replay the instruction if necessary.
8995080332cSMichael Neuling 		 */
9005080332cSMichael Neuling 		pagefault_disable();
9015080332cSMichael Neuling 		p9_hmi_special_emu(regs);
9025080332cSMichael Neuling 		pagefault_enable();
9035080332cSMichael Neuling 	}
9045080332cSMichael Neuling #endif /* CONFIG_VSX */
9055080332cSMichael Neuling 
9060869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
9070869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
9080869b6fdSMahesh Salgaonkar 
9090869b6fdSMahesh Salgaonkar 	irq_exit();
9100869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
9110869b6fdSMahesh Salgaonkar }
9120869b6fdSMahesh Salgaonkar 
913dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
91414cf11afSPaul Mackerras {
915ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
916ba12eedeSLi Zhong 
91714cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
91814cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
91914cf11afSPaul Mackerras 
920cf4674c4SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_FIXME, 0);
921ba12eedeSLi Zhong 
922ba12eedeSLi Zhong 	exception_exit(prev_state);
92314cf11afSPaul Mackerras }
92414cf11afSPaul Mackerras 
925dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
92614cf11afSPaul Mackerras {
927ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
928ba12eedeSLi Zhong 
92914cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
93014cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
931ba12eedeSLi Zhong 		goto bail;
93214cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
933ba12eedeSLi Zhong 		goto bail;
93414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
935ba12eedeSLi Zhong 
936ba12eedeSLi Zhong bail:
937ba12eedeSLi Zhong 	exception_exit(prev_state);
93814cf11afSPaul Mackerras }
93914cf11afSPaul Mackerras 
94014cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
94114cf11afSPaul Mackerras {
942cf4674c4SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_FIXME, 0);
94314cf11afSPaul Mackerras }
94414cf11afSPaul Mackerras 
94503465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
94614cf11afSPaul Mackerras {
947ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
948ba12eedeSLi Zhong 
9492538c2d0SK.Prasad 	clear_single_step(regs);
95014cf11afSPaul Mackerras 
9516cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
9526cc89badSNaveen N. Rao 		return;
9536cc89badSNaveen N. Rao 
95414cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
95514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
956ba12eedeSLi Zhong 		goto bail;
95714cf11afSPaul Mackerras 	if (debugger_sstep(regs))
958ba12eedeSLi Zhong 		goto bail;
95914cf11afSPaul Mackerras 
96014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
961ba12eedeSLi Zhong 
962ba12eedeSLi Zhong bail:
963ba12eedeSLi Zhong 	exception_exit(prev_state);
96414cf11afSPaul Mackerras }
96503465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
96614cf11afSPaul Mackerras 
96714cf11afSPaul Mackerras /*
96814cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
96914cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
97014cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
97114cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
97214cf11afSPaul Mackerras  */
9738dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
97414cf11afSPaul Mackerras {
9752538c2d0SK.Prasad 	if (single_stepping(regs))
9762538c2d0SK.Prasad 		single_step_exception(regs);
97714cf11afSPaul Mackerras }
97814cf11afSPaul Mackerras 
9795fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
980dc1c1ca3SStephen Rothwell {
981cf4674c4SEric W. Biederman 	int ret = FPE_FIXME;
982dc1c1ca3SStephen Rothwell 
983dc1c1ca3SStephen Rothwell 	/* Invalid operation */
984dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
9855fad293bSKumar Gala 		ret = FPE_FLTINV;
986dc1c1ca3SStephen Rothwell 
987dc1c1ca3SStephen Rothwell 	/* Overflow */
988dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
9895fad293bSKumar Gala 		ret = FPE_FLTOVF;
990dc1c1ca3SStephen Rothwell 
991dc1c1ca3SStephen Rothwell 	/* Underflow */
992dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
9935fad293bSKumar Gala 		ret = FPE_FLTUND;
994dc1c1ca3SStephen Rothwell 
995dc1c1ca3SStephen Rothwell 	/* Divide by zero */
996dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
9975fad293bSKumar Gala 		ret = FPE_FLTDIV;
998dc1c1ca3SStephen Rothwell 
999dc1c1ca3SStephen Rothwell 	/* Inexact result */
1000dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
10015fad293bSKumar Gala 		ret = FPE_FLTRES;
10025fad293bSKumar Gala 
10035fad293bSKumar Gala 	return ret;
10045fad293bSKumar Gala }
10055fad293bSKumar Gala 
10065fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
10075fad293bSKumar Gala {
10085fad293bSKumar Gala 	int code = 0;
10095fad293bSKumar Gala 
10105fad293bSKumar Gala 	flush_fp_to_thread(current);
10115fad293bSKumar Gala 
1012de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
1013dc1c1ca3SStephen Rothwell 
1014dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
1015dc1c1ca3SStephen Rothwell }
1016dc1c1ca3SStephen Rothwell 
1017dc1c1ca3SStephen Rothwell /*
1018dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
101914cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
102014cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
102114cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
102214cf11afSPaul Mackerras  *
102314cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
102414cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
102514cf11afSPaul Mackerras  * bits is faster and easier.
102686417780SPaul Mackerras  *
102714cf11afSPaul Mackerras  */
102814cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
102914cf11afSPaul Mackerras {
103014cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
103114cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
103214cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
103314cf11afSPaul Mackerras 	u32 num_bytes;
103414cf11afSPaul Mackerras 	unsigned long EA;
103514cf11afSPaul Mackerras 	int pos = 0;
103614cf11afSPaul Mackerras 
103714cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
103816c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
103914cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
104014cf11afSPaul Mackerras 			return -EINVAL;
104114cf11afSPaul Mackerras 
104214cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
104314cf11afSPaul Mackerras 
104416c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
104516c57b36SKumar Gala 		case PPC_INST_LSWX:
104616c57b36SKumar Gala 		case PPC_INST_STSWX:
104714cf11afSPaul Mackerras 			EA += NB_RB;
104814cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
104914cf11afSPaul Mackerras 			break;
105016c57b36SKumar Gala 		case PPC_INST_LSWI:
105116c57b36SKumar Gala 		case PPC_INST_STSWI:
105214cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
105314cf11afSPaul Mackerras 			break;
105414cf11afSPaul Mackerras 		default:
105514cf11afSPaul Mackerras 			return -EINVAL;
105614cf11afSPaul Mackerras 	}
105714cf11afSPaul Mackerras 
105814cf11afSPaul Mackerras 	while (num_bytes != 0)
105914cf11afSPaul Mackerras 	{
106014cf11afSPaul Mackerras 		u8 val;
106114cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
106214cf11afSPaul Mackerras 
106380aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
106480aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
106580aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
106680aa0fb4SJames Yang 
106716c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
106816c57b36SKumar Gala 			case PPC_INST_LSWX:
106916c57b36SKumar Gala 			case PPC_INST_LSWI:
107014cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
107114cf11afSPaul Mackerras 					return -EFAULT;
107214cf11afSPaul Mackerras 				/* first time updating this reg,
107314cf11afSPaul Mackerras 				 * zero it out */
107414cf11afSPaul Mackerras 				if (pos == 0)
107514cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
107614cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
107714cf11afSPaul Mackerras 				break;
107816c57b36SKumar Gala 			case PPC_INST_STSWI:
107916c57b36SKumar Gala 			case PPC_INST_STSWX:
108014cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
108114cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
108214cf11afSPaul Mackerras 					return -EFAULT;
108314cf11afSPaul Mackerras 				break;
108414cf11afSPaul Mackerras 		}
108514cf11afSPaul Mackerras 		/* move EA to next address */
108614cf11afSPaul Mackerras 		EA += 1;
108714cf11afSPaul Mackerras 		num_bytes--;
108814cf11afSPaul Mackerras 
108914cf11afSPaul Mackerras 		/* manage our position within the register */
109014cf11afSPaul Mackerras 		if (++pos == 4) {
109114cf11afSPaul Mackerras 			pos = 0;
109214cf11afSPaul Mackerras 			if (++rT == 32)
109314cf11afSPaul Mackerras 				rT = 0;
109414cf11afSPaul Mackerras 		}
109514cf11afSPaul Mackerras 	}
109614cf11afSPaul Mackerras 
109714cf11afSPaul Mackerras 	return 0;
109814cf11afSPaul Mackerras }
109914cf11afSPaul Mackerras 
1100c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
1101c3412dcbSWill Schmidt {
1102c3412dcbSWill Schmidt 	u32 ra,rs;
1103c3412dcbSWill Schmidt 	unsigned long tmp;
1104c3412dcbSWill Schmidt 
1105c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
1106c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
1107c3412dcbSWill Schmidt 
1108c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
1109c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
1110c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
1111c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1112c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
1113c3412dcbSWill Schmidt 
1114c3412dcbSWill Schmidt 	return 0;
1115c3412dcbSWill Schmidt }
1116c3412dcbSWill Schmidt 
1117c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
1118c1469f13SKumar Gala {
1119c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
1120c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
1121c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
1122c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
1123c1469f13SKumar Gala 	u8 bit;
1124c1469f13SKumar Gala 	unsigned long tmp;
1125c1469f13SKumar Gala 
1126c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
1127c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
1128c1469f13SKumar Gala 
1129c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1130c1469f13SKumar Gala 
1131c1469f13SKumar Gala 	return 0;
1132c1469f13SKumar Gala }
1133c1469f13SKumar Gala 
11346ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
11356ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
11366ce6c629SMichael Neuling {
11376ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
11386ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
11396ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
11406ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
11416ce6c629SMichael Neuling 	 */
11426ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
11436ce6c629SMichael Neuling 		tm_enable();
11446ce6c629SMichael Neuling 		tm_abort(cause);
11456ce6c629SMichael Neuling 		return true;
11466ce6c629SMichael Neuling 	}
11476ce6c629SMichael Neuling 	return false;
11486ce6c629SMichael Neuling }
11496ce6c629SMichael Neuling #else
11506ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
11516ce6c629SMichael Neuling {
11526ce6c629SMichael Neuling 	return false;
11536ce6c629SMichael Neuling }
11546ce6c629SMichael Neuling #endif
11556ce6c629SMichael Neuling 
115614cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
115714cf11afSPaul Mackerras {
115814cf11afSPaul Mackerras 	u32 instword;
115914cf11afSPaul Mackerras 	u32 rd;
116014cf11afSPaul Mackerras 
11614288e343SAnton Blanchard 	if (!user_mode(regs))
116214cf11afSPaul Mackerras 		return -EINVAL;
116314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
116414cf11afSPaul Mackerras 
116514cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
116614cf11afSPaul Mackerras 		return -EFAULT;
116714cf11afSPaul Mackerras 
116814cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
116916c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1170eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
117114cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
117214cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
117314cf11afSPaul Mackerras 		return 0;
117414cf11afSPaul Mackerras 	}
117514cf11afSPaul Mackerras 
117614cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
117780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1178eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
117914cf11afSPaul Mackerras 		return 0;
118080947e7cSGeert Uytterhoeven 	}
118114cf11afSPaul Mackerras 
118214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
118316c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
118486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
118514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
118614cf11afSPaul Mackerras 
1187eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
118814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
118914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
119014cf11afSPaul Mackerras 		return 0;
119114cf11afSPaul Mackerras 	}
119214cf11afSPaul Mackerras 
119314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
119480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
11956ce6c629SMichael Neuling 		if (tm_abort_check(regs,
11966ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
11976ce6c629SMichael Neuling 			return -EINVAL;
1198eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
119914cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
120080947e7cSGeert Uytterhoeven 	}
120114cf11afSPaul Mackerras 
1202c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
120316c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1204eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1205c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1206c3412dcbSWill Schmidt 	}
1207c3412dcbSWill Schmidt 
1208c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
120916c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1210eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1211c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1212c1469f13SKumar Gala 	}
1213c1469f13SKumar Gala 
12149863c28aSJames Yang 	/* Emulate sync instruction variants */
12159863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
12169863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
12179863c28aSJames Yang 		asm volatile("sync");
12189863c28aSJames Yang 		return 0;
12199863c28aSJames Yang 	}
12209863c28aSJames Yang 
1221efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1222efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
122373d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
122473d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
122573d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
122673d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1227efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1228efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1229efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1230efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1231efcac658SAlexey Kardashevskiy 		return 0;
1232efcac658SAlexey Kardashevskiy 	}
1233efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
123473d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
123573d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
123673d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
123773d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1238efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1239efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1240efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
124100ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1242efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
124300ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1244efcac658SAlexey Kardashevskiy 		return 0;
1245efcac658SAlexey Kardashevskiy 	}
1246efcac658SAlexey Kardashevskiy #endif
1247efcac658SAlexey Kardashevskiy 
124814cf11afSPaul Mackerras 	return -EINVAL;
124914cf11afSPaul Mackerras }
125014cf11afSPaul Mackerras 
125173c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
125214cf11afSPaul Mackerras {
125373c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
125414cf11afSPaul Mackerras }
125514cf11afSPaul Mackerras 
12563a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
12573a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
12583a3b5aa6SKevin Hao {
12593a3b5aa6SKevin Hao 	int ret;
12603a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
12613a3b5aa6SKevin Hao 
12623a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
12633a3b5aa6SKevin Hao 	if (ret >= 0)
12643a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
12653a3b5aa6SKevin Hao 
12663a3b5aa6SKevin Hao 	switch (ret) {
12673a3b5aa6SKevin Hao 	case 0:
12683a3b5aa6SKevin Hao 		emulate_single_step(regs);
12693a3b5aa6SKevin Hao 		return 0;
12703a3b5aa6SKevin Hao 	case 1: {
12713a3b5aa6SKevin Hao 			int code = 0;
1272de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
12733a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
12743a3b5aa6SKevin Hao 			return 0;
12753a3b5aa6SKevin Hao 		}
12763a3b5aa6SKevin Hao 	case -EFAULT:
12773a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12783a3b5aa6SKevin Hao 		return 0;
12793a3b5aa6SKevin Hao 	}
12803a3b5aa6SKevin Hao 
12813a3b5aa6SKevin Hao 	return -1;
12823a3b5aa6SKevin Hao }
12833a3b5aa6SKevin Hao #else
12843a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
12853a3b5aa6SKevin Hao #endif
12863a3b5aa6SKevin Hao 
128703465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
128814cf11afSPaul Mackerras {
1289ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
129014cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
129114cf11afSPaul Mackerras 
1292aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
129304903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
129414cf11afSPaul Mackerras 
129514cf11afSPaul Mackerras 	if (reason & REASON_FP) {
129614cf11afSPaul Mackerras 		/* IEEE FP exception */
1297dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1298ba12eedeSLi Zhong 		goto bail;
12998dad3f92SPaul Mackerras 	}
13008dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1301a4c3f909SBalbir Singh 		unsigned long bugaddr;
1302ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1303ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1304ba797b28SJason Wessel 		if (debugger_bpt(regs))
1305ba12eedeSLi Zhong 			goto bail;
1306ba797b28SJason Wessel 
13076cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
13086cc89badSNaveen N. Rao 			goto bail;
13096cc89badSNaveen N. Rao 
131014cf11afSPaul Mackerras 		/* trap exception */
1311dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1312dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1313ba12eedeSLi Zhong 			goto bail;
131473c9ceabSJeremy Fitzhardinge 
1315a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1316a4c3f909SBalbir Singh 		/*
1317a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1318a4c3f909SBalbir Singh 		 */
1319a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1320a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1321a4c3f909SBalbir Singh 
132273c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1323a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
132414cf11afSPaul Mackerras 			regs->nip += 4;
1325ba12eedeSLi Zhong 			goto bail;
132614cf11afSPaul Mackerras 		}
13278dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1328ba12eedeSLi Zhong 		goto bail;
13298dad3f92SPaul Mackerras 	}
1330bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1331bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1332bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1333bc2a9408SMichael Neuling 		 * This occurs when:
1334bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1335bc2a9408SMichael Neuling 		 *    transition in TM states.
1336bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1337bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1338bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1339bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1340632f0574SMichael Ellerman 		 *
1341632f0574SMichael Ellerman 		 * If usermode caused this, it's done something illegal and
1342bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1343bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1344bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1345bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1346bc2a9408SMichael Neuling 		 */
1347bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1348bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1349ba12eedeSLi Zhong 			goto bail;
1350bc2a9408SMichael Neuling 		} else {
1351bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1352bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1353bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1354bc2a9408SMichael Neuling 		}
1355bc2a9408SMichael Neuling 	}
1356bc2a9408SMichael Neuling #endif
13578dad3f92SPaul Mackerras 
1358b3f6a459SMichael Ellerman 	/*
1359b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1360b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1361b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1362b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1363b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1364b3f6a459SMichael Ellerman 	 */
1365b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1366b3f6a459SMichael Ellerman 		goto sigill;
1367b3f6a459SMichael Ellerman 
1368a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1369a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1370cd8a5673SPaul Mackerras 		local_irq_enable();
1371cd8a5673SPaul Mackerras 
137204903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
137304903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
137404903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
137504903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
137604903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
137704903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
13784e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
13794e63f8edSBenjamin Herrenschmidt 	 */
13803a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1381ba12eedeSLi Zhong 		goto bail;
138204903a30SKumar Gala 
13838dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
13848dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
138514cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
138614cf11afSPaul Mackerras 		case 0:
138714cf11afSPaul Mackerras 			regs->nip += 4;
138814cf11afSPaul Mackerras 			emulate_single_step(regs);
1389ba12eedeSLi Zhong 			goto bail;
139014cf11afSPaul Mackerras 		case -EFAULT:
139114cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1392ba12eedeSLi Zhong 			goto bail;
13938dad3f92SPaul Mackerras 		}
13948dad3f92SPaul Mackerras 	}
13958dad3f92SPaul Mackerras 
1396b3f6a459SMichael Ellerman sigill:
139714cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
139814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
139914cf11afSPaul Mackerras 	else
140014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1401ba12eedeSLi Zhong 
1402ba12eedeSLi Zhong bail:
1403ba12eedeSLi Zhong 	exception_exit(prev_state);
140414cf11afSPaul Mackerras }
140503465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
140614cf11afSPaul Mackerras 
1407bf593907SPaul Mackerras /*
1408bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1409bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1410bf593907SPaul Mackerras  */
141103465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1412bf593907SPaul Mackerras {
1413bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1414bf593907SPaul Mackerras 	program_check_exception(regs);
1415bf593907SPaul Mackerras }
141603465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1417bf593907SPaul Mackerras 
1418dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
141914cf11afSPaul Mackerras {
1420ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
14214393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
142214cf11afSPaul Mackerras 
1423a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1424a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1425a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1426a3512b2dSBenjamin Herrenschmidt 
14276ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
14286ce6c629SMichael Neuling 		goto bail;
14296ce6c629SMichael Neuling 
1430e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1431e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
143214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
143314cf11afSPaul Mackerras 
143414cf11afSPaul Mackerras 	if (fixed == 1) {
143514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
143614cf11afSPaul Mackerras 		emulate_single_step(regs);
1437ba12eedeSLi Zhong 		goto bail;
143814cf11afSPaul Mackerras 	}
143914cf11afSPaul Mackerras 
144014cf11afSPaul Mackerras 	/* Operand address was bad */
144114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
14424393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
14434393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
14444393c4f6SBenjamin Herrenschmidt 	} else {
14454393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
14464393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
144714cf11afSPaul Mackerras 	}
14484393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
14494393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
14504393c4f6SBenjamin Herrenschmidt 	else
14514393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1452ba12eedeSLi Zhong 
1453ba12eedeSLi Zhong bail:
1454ba12eedeSLi Zhong 	exception_exit(prev_state);
145514cf11afSPaul Mackerras }
145614cf11afSPaul Mackerras 
1457f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1458f0f558b1SPaul Mackerras {
1459f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1460f0f558b1SPaul Mackerras 
1461f0f558b1SPaul Mackerras 	if (user_mode(regs))
1462f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1463f0f558b1SPaul Mackerras 	else
1464f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1465f0f558b1SPaul Mackerras 
1466f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1467f0f558b1SPaul Mackerras }
1468f0f558b1SPaul Mackerras 
146914cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
147014cf11afSPaul Mackerras {
147114cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
147214cf11afSPaul Mackerras 	       current, regs->gpr[1]);
147314cf11afSPaul Mackerras 	debugger(regs);
147414cf11afSPaul Mackerras 	show_regs(regs);
147514cf11afSPaul Mackerras 	panic("kernel stack overflow");
147614cf11afSPaul Mackerras }
147714cf11afSPaul Mackerras 
147814cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
147914cf11afSPaul Mackerras {
148014cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
148114cf11afSPaul Mackerras 	       regs->nip, regs->msr);
148214cf11afSPaul Mackerras 	debugger(regs);
148314cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
148414cf11afSPaul Mackerras }
148514cf11afSPaul Mackerras 
1486dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1487dc1c1ca3SStephen Rothwell {
1488ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1489ba12eedeSLi Zhong 
1490dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1491dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1492dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1493ba12eedeSLi Zhong 
1494ba12eedeSLi Zhong 	exception_exit(prev_state);
1495dc1c1ca3SStephen Rothwell }
1496dc1c1ca3SStephen Rothwell 
1497dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1498dc1c1ca3SStephen Rothwell {
1499ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1500ba12eedeSLi Zhong 
1501dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1502dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1503dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1504dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1505ba12eedeSLi Zhong 		goto bail;
1506dc1c1ca3SStephen Rothwell 	}
15076c4841c2SAnton Blanchard 
1508dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1509dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1510dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1511ba12eedeSLi Zhong 
1512ba12eedeSLi Zhong bail:
1513ba12eedeSLi Zhong 	exception_exit(prev_state);
1514dc1c1ca3SStephen Rothwell }
1515dc1c1ca3SStephen Rothwell 
1516ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1517ce48b210SMichael Neuling {
1518ce48b210SMichael Neuling 	if (user_mode(regs)) {
1519ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1520ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1521ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1522ce48b210SMichael Neuling 		return;
1523ce48b210SMichael Neuling 	}
1524ce48b210SMichael Neuling 
1525ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1526ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1527ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1528ce48b210SMichael Neuling }
1529ce48b210SMichael Neuling 
15302517617eSMichael Neuling #ifdef CONFIG_PPC64
1531172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1532172f7aaaSCyril Bur {
15335d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
15345d176f75SCyril Bur 	if (user_mode(regs)) {
15355d176f75SCyril Bur 		current->thread.load_tm++;
15365d176f75SCyril Bur 		regs->msr |= MSR_TM;
15375d176f75SCyril Bur 		tm_enable();
15385d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
15395d176f75SCyril Bur 		return;
15405d176f75SCyril Bur 	}
15415d176f75SCyril Bur #endif
1542172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1543172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1544172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1545172f7aaaSCyril Bur }
1546172f7aaaSCyril Bur 
1547021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1548d0c0c9a1SMichael Neuling {
1549021424a1SMichael Ellerman 	static char *facility_strings[] = {
15502517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
15512517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
15522517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
15532517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
15542517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
15552517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
15562517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
15572517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1558794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
15599b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1560021424a1SMichael Ellerman 	};
15612517617eSMichael Neuling 	char *facility = "unknown";
1562021424a1SMichael Ellerman 	u64 value;
1563c952c1c4SAnshuman Khandual 	u32 instword, rd;
15642517617eSMichael Neuling 	u8 status;
15652517617eSMichael Neuling 	bool hv;
1566021424a1SMichael Ellerman 
15672517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
15682517617eSMichael Neuling 	if (hv)
1569b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
15702517617eSMichael Neuling 	else
15712517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
15722517617eSMichael Neuling 
15732517617eSMichael Neuling 	status = value >> 56;
15742517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1575c952c1c4SAnshuman Khandual 		/*
1576c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1577c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1578c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1579c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1580c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1581c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1582c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1583c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1584c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1585c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1586c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1587c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1588c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1589c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
15902517617eSMichael Neuling 		 */
1591c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1592c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1593c952c1c4SAnshuman Khandual 			return;
1594c952c1c4SAnshuman Khandual 		}
1595c952c1c4SAnshuman Khandual 
1596c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1597c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1598c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1599c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1600c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
16012517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1602b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1603b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1604c952c1c4SAnshuman Khandual 		}
1605c952c1c4SAnshuman Khandual 
1606c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1607c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1608c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1609c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1610c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1611c952c1c4SAnshuman Khandual 				return;
1612c952c1c4SAnshuman Khandual 			}
1613c952c1c4SAnshuman Khandual 			regs->nip += 4;
1614c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1615c952c1c4SAnshuman Khandual 		}
16162517617eSMichael Neuling 		return;
1617b14b6260SMichael Ellerman 	}
1618b14b6260SMichael Ellerman 
1619172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1620172f7aaaSCyril Bur 		/*
1621172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1622172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1623172f7aaaSCyril Bur 		 *
1624172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1625172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1626172f7aaaSCyril Bur 		 * support.
1627172f7aaaSCyril Bur 		 *
1628172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1629172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1630172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1631172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1632172f7aaaSCyril Bur 		 */
1633172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1634172f7aaaSCyril Bur 			goto out;
1635172f7aaaSCyril Bur 
1636172f7aaaSCyril Bur 		tm_unavailable(regs);
1637172f7aaaSCyril Bur 		return;
1638172f7aaaSCyril Bur 	}
1639172f7aaaSCyril Bur 
164093c2ec0fSBalbir Singh 	if ((hv || status >= 2) &&
164193c2ec0fSBalbir Singh 	    (status < ARRAY_SIZE(facility_strings)) &&
16422517617eSMichael Neuling 	    facility_strings[status])
16432517617eSMichael Neuling 		facility = facility_strings[status];
1644021424a1SMichael Ellerman 
1645d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1646d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1647d0c0c9a1SMichael Neuling 		local_irq_enable();
1648d0c0c9a1SMichael Neuling 
164993c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
165093c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1651d0c0c9a1SMichael Neuling 
1652172f7aaaSCyril Bur out:
1653d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1654d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1655d0c0c9a1SMichael Neuling 		return;
1656d0c0c9a1SMichael Neuling 	}
1657d0c0c9a1SMichael Neuling 
1658021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1659d0c0c9a1SMichael Neuling }
16602517617eSMichael Neuling #endif
1661d0c0c9a1SMichael Neuling 
1662f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1663f54db641SMichael Neuling 
1664f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1665f54db641SMichael Neuling {
1666f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1667f54db641SMichael Neuling 
1668f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1669f54db641SMichael Neuling 		 regs->nip, regs->msr);
1670f54db641SMichael Neuling 
1671f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1672f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1673f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1674f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1675f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1676f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1677f54db641SMichael Neuling 	 */
1678d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1679f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1680f54db641SMichael Neuling 
1681f54db641SMichael Neuling 	/* Enable FP for the task: */
1682a7771176SCyril Bur 	current->thread.load_fp = 1;
1683f54db641SMichael Neuling 
1684f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1685f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1686f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
16873ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
16883ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1689f54db641SMichael Neuling 	 */
1690eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1691f54db641SMichael Neuling }
1692f54db641SMichael Neuling 
1693f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1694f54db641SMichael Neuling {
1695f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1696f54db641SMichael Neuling 	 * the same way.
1697f54db641SMichael Neuling 	 */
1698f54db641SMichael Neuling 
1699f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1700f54db641SMichael Neuling 		 "MSR=%lx\n",
1701f54db641SMichael Neuling 		 regs->nip, regs->msr);
1702d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1703a7771176SCyril Bur 	current->thread.load_vec = 1;
1704eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1705f54db641SMichael Neuling 	current->thread.used_vr = 1;
17063ac8ff1cSPaul Mackerras }
17073ac8ff1cSPaul Mackerras 
1708f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1709f54db641SMichael Neuling {
1710f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1711f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1712f54db641SMichael Neuling 	 *
1713f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1714f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1715f54db641SMichael Neuling 	 */
1716f54db641SMichael Neuling 
1717f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1718f54db641SMichael Neuling 		 "MSR=%lx\n",
1719f54db641SMichael Neuling 		 regs->nip, regs->msr);
1720f54db641SMichael Neuling 
17213ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
17223ac8ff1cSPaul Mackerras 
1723f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1724d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1725f54db641SMichael Neuling 
1726a7771176SCyril Bur 	current->thread.load_vec = 1;
1727a7771176SCyril Bur 	current->thread.load_fp = 1;
17283ac8ff1cSPaul Mackerras 
1729eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1730f54db641SMichael Neuling }
1731f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1732f54db641SMichael Neuling 
1733dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1734dc1c1ca3SStephen Rothwell {
173569111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
173689713ed1SAnton Blanchard 
1737dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1738dc1c1ca3SStephen Rothwell }
1739dc1c1ca3SStephen Rothwell 
1740172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
17413bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
17423bffb652SDave Kleikamp {
17433bffb652SDave Kleikamp 	int changed = 0;
17443bffb652SDave Kleikamp 	/*
17453bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
17463bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
17473bffb652SDave Kleikamp 	 */
17483bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
17493bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
17503bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
175151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
17523bffb652SDave Kleikamp #endif
1753*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status,
17543bffb652SDave Kleikamp 			     5);
17553bffb652SDave Kleikamp 		changed |= 0x01;
17563bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
17573bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1758*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status,
17593bffb652SDave Kleikamp 			     6);
17603bffb652SDave Kleikamp 		changed |= 0x01;
17613bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
176251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
17633bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1764*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status,
17653bffb652SDave Kleikamp 			     1);
17663bffb652SDave Kleikamp 		changed |= 0x01;
17673bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
176851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
1769*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status,
17703bffb652SDave Kleikamp 			     2);
17713bffb652SDave Kleikamp 		changed |= 0x01;
17723bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
177351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
17743bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1775*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status,
17763bffb652SDave Kleikamp 			     3);
17773bffb652SDave Kleikamp 		changed |= 0x01;
17783bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
177951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
1780*47355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status,
17813bffb652SDave Kleikamp 			     4);
17823bffb652SDave Kleikamp 		changed |= 0x01;
17833bffb652SDave Kleikamp 	}
17843bffb652SDave Kleikamp 	/*
17853bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
17863bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
17873bffb652SDave Kleikamp 	 * back on or not.
17883bffb652SDave Kleikamp 	 */
178951ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
179051ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
17913bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
17923bffb652SDave Kleikamp 	else
17933bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
179451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
17953bffb652SDave Kleikamp 
17963bffb652SDave Kleikamp 	if (changed & 0x01)
179751ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
17983bffb652SDave Kleikamp }
179914cf11afSPaul Mackerras 
180003465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
180114cf11afSPaul Mackerras {
180251ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
18033bffb652SDave Kleikamp 
1804ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1805ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1806ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1807ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1808ec097c84SRoland McGrath 	 */
1809ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1810ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1811ec097c84SRoland McGrath 
1812ec097c84SRoland McGrath 		/* Disable BT */
1813ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1814ec097c84SRoland McGrath 		/* Clear the BT event */
1815ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1816ec097c84SRoland McGrath 
1817ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1818ec097c84SRoland McGrath 		if (user_mode(regs)) {
181951ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
182051ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1821ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1822ec097c84SRoland McGrath 			return;
1823ec097c84SRoland McGrath 		}
1824ec097c84SRoland McGrath 
18256cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
18266cc89badSNaveen N. Rao 			return;
18276cc89badSNaveen N. Rao 
1828ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1829ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1830ec097c84SRoland McGrath 			return;
1831ec097c84SRoland McGrath 		}
1832ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1833ec097c84SRoland McGrath 			return;
1834ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
183514cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1836f8279621SKumar Gala 
183714cf11afSPaul Mackerras 		/* Disable instruction completion */
183814cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
183914cf11afSPaul Mackerras 		/* Clear the instruction completion event */
184014cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1841f8279621SKumar Gala 
18426cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
18436cc89badSNaveen N. Rao 			return;
18446cc89badSNaveen N. Rao 
1845f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1846f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
184714cf11afSPaul Mackerras 			return;
184814cf11afSPaul Mackerras 		}
1849f8279621SKumar Gala 
1850f8279621SKumar Gala 		if (debugger_sstep(regs))
1851f8279621SKumar Gala 			return;
1852f8279621SKumar Gala 
18533bffb652SDave Kleikamp 		if (user_mode(regs)) {
185451ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
185551ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
185651ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
18573bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
18583bffb652SDave Kleikamp 			else
18593bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
186051ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
18613bffb652SDave Kleikamp 		}
1862f8279621SKumar Gala 
1863f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
18643bffb652SDave Kleikamp 	} else
18653bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
186614cf11afSPaul Mackerras }
186703465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1868172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
186914cf11afSPaul Mackerras 
187014cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
187114cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
187214cf11afSPaul Mackerras {
187314cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
187414cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
187514cf11afSPaul Mackerras }
187614cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
187714cf11afSPaul Mackerras 
187814cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1879dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
188014cf11afSPaul Mackerras {
188114cf11afSPaul Mackerras 	int err;
188214cf11afSPaul Mackerras 
188314cf11afSPaul Mackerras 	if (!user_mode(regs)) {
188414cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
188514cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
18868dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
188714cf11afSPaul Mackerras 	}
188814cf11afSPaul Mackerras 
1889dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1890dc1c1ca3SStephen Rothwell 
1891eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
189214cf11afSPaul Mackerras 	err = emulate_altivec(regs);
189314cf11afSPaul Mackerras 	if (err == 0) {
189414cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
189514cf11afSPaul Mackerras 		emulate_single_step(regs);
189614cf11afSPaul Mackerras 		return;
189714cf11afSPaul Mackerras 	}
189814cf11afSPaul Mackerras 
189914cf11afSPaul Mackerras 	if (err == -EFAULT) {
190014cf11afSPaul Mackerras 		/* got an error reading the instruction */
190114cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
190214cf11afSPaul Mackerras 	} else {
190314cf11afSPaul Mackerras 		/* didn't recognize the instruction */
190414cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
190576462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
190614cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1907de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
190814cf11afSPaul Mackerras 	}
190914cf11afSPaul Mackerras }
191014cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
191114cf11afSPaul Mackerras 
191214cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
191314cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
191414cf11afSPaul Mackerras 			   unsigned long error_code)
191514cf11afSPaul Mackerras {
191614cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
191714cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
191814cf11afSPaul Mackerras 	 * something smarter
191914cf11afSPaul Mackerras 	 */
192014cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
192114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
192214cf11afSPaul Mackerras 	return;
192314cf11afSPaul Mackerras }
192414cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
192514cf11afSPaul Mackerras 
192614cf11afSPaul Mackerras #ifdef CONFIG_SPE
192714cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
192814cf11afSPaul Mackerras {
19296a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
193014cf11afSPaul Mackerras 	unsigned long spefscr;
193114cf11afSPaul Mackerras 	int fpexc_mode;
1932cf4674c4SEric W. Biederman 	int code = FPE_FIXME;
19336a800f36SLiu Yu 	int err;
19346a800f36SLiu Yu 
1935685659eeSyu liu 	flush_spe_to_thread(current);
193614cf11afSPaul Mackerras 
193714cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
193814cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
193914cf11afSPaul Mackerras 
194014cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
194114cf11afSPaul Mackerras 		code = FPE_FLTOVF;
194214cf11afSPaul Mackerras 	}
194314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
194414cf11afSPaul Mackerras 		code = FPE_FLTUND;
194514cf11afSPaul Mackerras 	}
194614cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
194714cf11afSPaul Mackerras 		code = FPE_FLTDIV;
194814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
194914cf11afSPaul Mackerras 		code = FPE_FLTINV;
195014cf11afSPaul Mackerras 	}
195114cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
195214cf11afSPaul Mackerras 		code = FPE_FLTRES;
195314cf11afSPaul Mackerras 
19546a800f36SLiu Yu 	err = do_spe_mathemu(regs);
19556a800f36SLiu Yu 	if (err == 0) {
19566a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
19576a800f36SLiu Yu 		emulate_single_step(regs);
195814cf11afSPaul Mackerras 		return;
195914cf11afSPaul Mackerras 	}
19606a800f36SLiu Yu 
19616a800f36SLiu Yu 	if (err == -EFAULT) {
19626a800f36SLiu Yu 		/* got an error reading the instruction */
19636a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
19646a800f36SLiu Yu 	} else if (err == -EINVAL) {
19656a800f36SLiu Yu 		/* didn't recognize the instruction */
19666a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
19676a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
19686a800f36SLiu Yu 	} else {
19696a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
19706a800f36SLiu Yu 	}
19716a800f36SLiu Yu 
19726a800f36SLiu Yu 	return;
19736a800f36SLiu Yu }
19746a800f36SLiu Yu 
19756a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
19766a800f36SLiu Yu {
19776a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
19786a800f36SLiu Yu 	int err;
19796a800f36SLiu Yu 
19806a800f36SLiu Yu 	preempt_disable();
19816a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
19826a800f36SLiu Yu 		giveup_spe(current);
19836a800f36SLiu Yu 	preempt_enable();
19846a800f36SLiu Yu 
19856a800f36SLiu Yu 	regs->nip -= 4;
19866a800f36SLiu Yu 	err = speround_handler(regs);
19876a800f36SLiu Yu 	if (err == 0) {
19886a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
19896a800f36SLiu Yu 		emulate_single_step(regs);
19906a800f36SLiu Yu 		return;
19916a800f36SLiu Yu 	}
19926a800f36SLiu Yu 
19936a800f36SLiu Yu 	if (err == -EFAULT) {
19946a800f36SLiu Yu 		/* got an error reading the instruction */
19956a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
19966a800f36SLiu Yu 	} else if (err == -EINVAL) {
19976a800f36SLiu Yu 		/* didn't recognize the instruction */
19986a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
19996a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
20006a800f36SLiu Yu 	} else {
2001cf4674c4SEric W. Biederman 		_exception(SIGFPE, regs, FPE_FIXME, regs->nip);
20026a800f36SLiu Yu 		return;
20036a800f36SLiu Yu 	}
20046a800f36SLiu Yu }
200514cf11afSPaul Mackerras #endif
200614cf11afSPaul Mackerras 
2007dc1c1ca3SStephen Rothwell /*
2008dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
2009dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
2010dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
2011dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
2012dc1c1ca3SStephen Rothwell  */
2013dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
2014dc1c1ca3SStephen Rothwell {
2015dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
2016dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
2017dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
2018dc1c1ca3SStephen Rothwell }
201915770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
2020dc1c1ca3SStephen Rothwell 
20211e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
202214cf11afSPaul Mackerras /*
202314cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
202414cf11afSPaul Mackerras  * spins until a reboot occurs
202514cf11afSPaul Mackerras  */
202614cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
202714cf11afSPaul Mackerras {
202814cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
202914cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
203014cf11afSPaul Mackerras 	return;
203114cf11afSPaul Mackerras }
203214cf11afSPaul Mackerras 
203314cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
203414cf11afSPaul Mackerras {
203514cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
203614cf11afSPaul Mackerras 	WatchdogHandler(regs);
203714cf11afSPaul Mackerras }
203814cf11afSPaul Mackerras #endif
2039dc1c1ca3SStephen Rothwell 
2040dc1c1ca3SStephen Rothwell /*
2041dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
2042dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
2043dc1c1ca3SStephen Rothwell  */
2044dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
2045dc1c1ca3SStephen Rothwell {
2046dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
2047dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
2048dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
2049dc1c1ca3SStephen Rothwell }
205015770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
205114cf11afSPaul Mackerras 
205214cf11afSPaul Mackerras void __init trap_init(void)
205314cf11afSPaul Mackerras {
205414cf11afSPaul Mackerras }
205580947e7cSGeert Uytterhoeven 
205680947e7cSGeert Uytterhoeven 
205780947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
205880947e7cSGeert Uytterhoeven 
205980947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
206080947e7cSGeert Uytterhoeven 
206180947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
206280947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
206380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
206480947e7cSGeert Uytterhoeven #endif
206580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
206680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
206780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
206880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
206980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
207080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
207180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
207280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
207380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
207480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
2075a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
207680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
207780947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
207880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
207980947e7cSGeert Uytterhoeven #endif
208080947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
208180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
208280947e7cSGeert Uytterhoeven #endif
2083efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
2084efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
2085efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
2086f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
20875080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvw4x),
20885080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvh8x),
20895080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvd2x),
20905080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvb16x),
2091efcac658SAlexey Kardashevskiy #endif
209280947e7cSGeert Uytterhoeven };
209380947e7cSGeert Uytterhoeven 
209480947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
209580947e7cSGeert Uytterhoeven 
209680947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
209780947e7cSGeert Uytterhoeven {
209876462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
209980947e7cSGeert Uytterhoeven 			    type);
210080947e7cSGeert Uytterhoeven }
210180947e7cSGeert Uytterhoeven 
210280947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
210380947e7cSGeert Uytterhoeven {
210480947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
210580947e7cSGeert Uytterhoeven 	unsigned int i;
210680947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
210780947e7cSGeert Uytterhoeven 
210880947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
210980947e7cSGeert Uytterhoeven 		return -ENODEV;
211080947e7cSGeert Uytterhoeven 
211180947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
211280947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
211380947e7cSGeert Uytterhoeven 	if (!dir)
211480947e7cSGeert Uytterhoeven 		return -ENOMEM;
211580947e7cSGeert Uytterhoeven 
211680947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
211780947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
211880947e7cSGeert Uytterhoeven 	if (!d)
211980947e7cSGeert Uytterhoeven 		goto fail;
212080947e7cSGeert Uytterhoeven 
212180947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
212280947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
212380947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
212480947e7cSGeert Uytterhoeven 		if (!d)
212580947e7cSGeert Uytterhoeven 			goto fail;
212680947e7cSGeert Uytterhoeven 	}
212780947e7cSGeert Uytterhoeven 
212880947e7cSGeert Uytterhoeven 	return 0;
212980947e7cSGeert Uytterhoeven 
213080947e7cSGeert Uytterhoeven fail:
213180947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
213280947e7cSGeert Uytterhoeven 	return -ENOMEM;
213380947e7cSGeert Uytterhoeven }
213480947e7cSGeert Uytterhoeven 
213580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
213680947e7cSGeert Uytterhoeven 
213780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2138