xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision b96672dd840f2231c3e0804842d380c401739733)
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>
4014cf11afSPaul Mackerras 
4180947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4214cf11afSPaul Mackerras #include <asm/pgtable.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
447644d581SMichael Ellerman #include <asm/debugfs.h>
4514cf11afSPaul Mackerras #include <asm/io.h>
4686417780SPaul Mackerras #include <asm/machdep.h>
4786417780SPaul Mackerras #include <asm/rtas.h>
48f7f6f4feSDavid Gibson #include <asm/pmc.h>
4914cf11afSPaul Mackerras #include <asm/reg.h>
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
566ce6c629SMichael Neuling #include <asm/tm.h>
57dc1c1ca3SStephen Rothwell #endif
58c0ce7d08SDavid Wilder #include <asm/kexec.h>
5916c57b36SKumar Gala #include <asm/ppc-opcode.h>
60cce1f106SShaohui Xie #include <asm/rio.h>
61ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
62ae3a197eSDavid Howells #include <asm/switch_to.h>
63f54db641SMichael Neuling #include <asm/tm.h>
64ae3a197eSDavid Howells #include <asm/debug.h>
6542f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
66fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
674e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
686cc89badSNaveen N. Rao #include <asm/kprobes.h>
69dc1c1ca3SStephen Rothwell 
70da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
715be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
725be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
735be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
745be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
755be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
769422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
775be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7814cf11afSPaul Mackerras 
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
849422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8614cf11afSPaul Mackerras #endif
8714cf11afSPaul Mackerras 
888b3c34cfSMichael Neuling /* Transactional Memory trap debug */
898b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
908b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
918b3c34cfSMichael Neuling #else
928b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
938b3c34cfSMichael Neuling #endif
948b3c34cfSMichael Neuling 
9514cf11afSPaul Mackerras /*
9614cf11afSPaul Mackerras  * Trap & Exception support
9714cf11afSPaul Mackerras  */
9814cf11afSPaul Mackerras 
996031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1006031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1016031d9d9Santon@samba.org {
1026031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1036031d9d9Santon@samba.org 	if (pmac_backlight) {
1046031d9d9Santon@samba.org 		struct backlight_properties *props;
1056031d9d9Santon@samba.org 
1066031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1076031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1086031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1096031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1106031d9d9Santon@samba.org 	}
1116031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1126031d9d9Santon@samba.org }
1136031d9d9Santon@samba.org #else
1146031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1156031d9d9Santon@samba.org #endif
1166031d9d9Santon@samba.org 
1176fcd6baaSNicholas Piggin /*
1186fcd6baaSNicholas Piggin  * If oops/die is expected to crash the machine, return true here.
1196fcd6baaSNicholas Piggin  *
1206fcd6baaSNicholas Piggin  * This should not be expected to be 100% accurate, there may be
1216fcd6baaSNicholas Piggin  * notifiers registered or other unexpected conditions that may bring
1226fcd6baaSNicholas Piggin  * down the kernel. Or if the current process in the kernel is holding
1236fcd6baaSNicholas Piggin  * locks or has other critical state, the kernel may become effectively
1246fcd6baaSNicholas Piggin  * unusable anyway.
1256fcd6baaSNicholas Piggin  */
1266fcd6baaSNicholas Piggin bool die_will_crash(void)
1276fcd6baaSNicholas Piggin {
1286fcd6baaSNicholas Piggin 	if (should_fadump_crash())
1296fcd6baaSNicholas Piggin 		return true;
1306fcd6baaSNicholas Piggin 	if (kexec_should_crash(current))
1316fcd6baaSNicholas Piggin 		return true;
1326fcd6baaSNicholas Piggin 	if (in_interrupt() || panic_on_oops ||
1336fcd6baaSNicholas Piggin 			!current->pid || is_global_init(current))
1346fcd6baaSNicholas Piggin 		return true;
1356fcd6baaSNicholas Piggin 
1366fcd6baaSNicholas Piggin 	return false;
1376fcd6baaSNicholas Piggin }
1386fcd6baaSNicholas Piggin 
139760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
140760ca4dcSAnton Blanchard static int die_owner = -1;
141760ca4dcSAnton Blanchard static unsigned int die_nest_count;
142c0ce7d08SDavid Wilder static int die_counter;
143760ca4dcSAnton Blanchard 
14403465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
145760ca4dcSAnton Blanchard {
146760ca4dcSAnton Blanchard 	int cpu;
14734c2a14fSanton@samba.org 	unsigned long flags;
14814cf11afSPaul Mackerras 
149293e4688Santon@samba.org 	oops_enter();
150293e4688Santon@samba.org 
151760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
152760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
153760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
154760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
155760ca4dcSAnton Blanchard 		if (cpu == die_owner)
156760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
157760ca4dcSAnton Blanchard 		else
158760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
159760ca4dcSAnton Blanchard 	}
160760ca4dcSAnton Blanchard 	die_nest_count++;
161760ca4dcSAnton Blanchard 	die_owner = cpu;
16214cf11afSPaul Mackerras 	console_verbose();
16314cf11afSPaul Mackerras 	bust_spinlocks(1);
1646031d9d9Santon@samba.org 	if (machine_is(powermac))
1656031d9d9Santon@samba.org 		pmac_backlight_unblank();
166760ca4dcSAnton Blanchard 	return flags;
16734c2a14fSanton@samba.org }
16803465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
1695474c120SMichael Hanselmann 
17003465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
171760ca4dcSAnton Blanchard 			       int signr)
172760ca4dcSAnton Blanchard {
17314cf11afSPaul Mackerras 	bust_spinlocks(0);
174373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
175760ca4dcSAnton Blanchard 	die_nest_count--;
17658154c8cSAnton Blanchard 	oops_exit();
17758154c8cSAnton Blanchard 	printk("\n");
1787458e8b2SNicholas Piggin 	if (!die_nest_count) {
179760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
1807458e8b2SNicholas Piggin 		die_owner = -1;
181760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
1827458e8b2SNicholas Piggin 	}
183760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
184cc532915SMichael Ellerman 
185ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
186ebaeb5aeSMahesh Salgaonkar 
1874388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
188cc532915SMichael Ellerman 		crash_kexec(regs);
1899b00ac06SAnton Blanchard 
190760ca4dcSAnton Blanchard 	if (!signr)
191760ca4dcSAnton Blanchard 		return;
192760ca4dcSAnton Blanchard 
19358154c8cSAnton Blanchard 	/*
19458154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
19558154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
19658154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
19758154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
19858154c8cSAnton Blanchard 	 */
19958154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
20058154c8cSAnton Blanchard 	    is_global_init(current)) {
20158154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
20258154c8cSAnton Blanchard 	}
20358154c8cSAnton Blanchard 
20414cf11afSPaul Mackerras 	if (in_interrupt())
20514cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
206cea6a4baSHorms 	if (panic_on_oops)
207012c437dSHorms 		panic("Fatal exception");
208760ca4dcSAnton Blanchard 	do_exit(signr);
209760ca4dcSAnton Blanchard }
21003465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
211cea6a4baSHorms 
21203465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
213760ca4dcSAnton Blanchard {
214760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2152e82ca3cSMichael Ellerman 
2162e82ca3cSMichael Ellerman 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
2172e82ca3cSMichael Ellerman 		printk("LE ");
2182e82ca3cSMichael Ellerman 	else
2192e82ca3cSMichael Ellerman 		printk("BE ");
2202e82ca3cSMichael Ellerman 
2211c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_PREEMPT))
22272c0d9eeSMichael Ellerman 		pr_cont("PREEMPT ");
2231c56cd8eSMichael Ellerman 
2241c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_SMP))
22572c0d9eeSMichael Ellerman 		pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
2261c56cd8eSMichael Ellerman 
227e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
22872c0d9eeSMichael Ellerman 		pr_cont("DEBUG_PAGEALLOC ");
2291c56cd8eSMichael Ellerman 
2301c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_NUMA))
23172c0d9eeSMichael Ellerman 		pr_cont("NUMA ");
2321c56cd8eSMichael Ellerman 
23372c0d9eeSMichael Ellerman 	pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
234760ca4dcSAnton Blanchard 
235760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
236760ca4dcSAnton Blanchard 		return 1;
237760ca4dcSAnton Blanchard 
238760ca4dcSAnton Blanchard 	print_modules();
239760ca4dcSAnton Blanchard 	show_regs(regs);
24014cf11afSPaul Mackerras 
24114cf11afSPaul Mackerras 	return 0;
24214cf11afSPaul Mackerras }
24303465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
24414cf11afSPaul Mackerras 
245760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
246760ca4dcSAnton Blanchard {
2476f44b20eSNicholas Piggin 	unsigned long flags;
248760ca4dcSAnton Blanchard 
2496f44b20eSNicholas Piggin 	if (debugger(regs))
2506f44b20eSNicholas Piggin 		return;
2516f44b20eSNicholas Piggin 
2526f44b20eSNicholas Piggin 	flags = oops_begin(regs);
253760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
254760ca4dcSAnton Blanchard 		err = 0;
255760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
256760ca4dcSAnton Blanchard }
25715770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
258760ca4dcSAnton Blanchard 
25925baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
26025baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
26125baa35bSOleg Nesterov {
26225baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
26325baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
26425baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
26525baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
26625baa35bSOleg Nesterov }
26725baa35bSOleg Nesterov 
26814cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
26914cf11afSPaul Mackerras {
27014cf11afSPaul Mackerras 	siginfo_t info;
271d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
272d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
273d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
274d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
27514cf11afSPaul Mackerras 
27614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
277760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
27814cf11afSPaul Mackerras 		return;
279760ca4dcSAnton Blanchard 	}
280760ca4dcSAnton Blanchard 
281760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
28276462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
283d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
284d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
28514cf11afSPaul Mackerras 	}
28614cf11afSPaul Mackerras 
287a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2889f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2899f2f79e3SBenjamin Herrenschmidt 
29041ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
29114cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
29214cf11afSPaul Mackerras 	info.si_signo = signr;
29314cf11afSPaul Mackerras 	info.si_code = code;
29414cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
29514cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
29614cf11afSPaul Mackerras }
29714cf11afSPaul Mackerras 
29814cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
29914cf11afSPaul Mackerras {
3002b4f3ac5SNicholas Piggin 	/*
3012b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
3022b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
3032b4f3ac5SNicholas Piggin 	 */
3042b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
3052b4f3ac5SNicholas Piggin 	if (!nested)
3062b4f3ac5SNicholas Piggin 		nmi_enter();
3072b4f3ac5SNicholas Piggin 
308ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
309ca41ad43SNicholas Piggin 
31014cf11afSPaul Mackerras 	/* See if any machine dependent calls */
311c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
312c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
313c4f3b52cSNicholas Piggin 			goto out;
314c902be71SArnd Bergmann 	}
31514cf11afSPaul Mackerras 
3164388c9b3SNicholas Piggin 	if (debugger(regs))
3174388c9b3SNicholas Piggin 		goto out;
3184388c9b3SNicholas Piggin 
3194388c9b3SNicholas Piggin 	/*
3204388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
3214388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
3224388c9b3SNicholas Piggin 	 * registered).
3234388c9b3SNicholas Piggin 	 */
3244388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
3254388c9b3SNicholas Piggin 
3264388c9b3SNicholas Piggin 	crash_kexec(regs);
3274388c9b3SNicholas Piggin 
3284388c9b3SNicholas Piggin 	/*
3294388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
3304388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
3314388c9b3SNicholas Piggin 	 * code.
3324388c9b3SNicholas Piggin 	 */
3334388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
3344388c9b3SNicholas Piggin 
3354388c9b3SNicholas Piggin 	/*
3364388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
3374388c9b3SNicholas Piggin 	 * panic.
3384388c9b3SNicholas Piggin 	 */
3394388c9b3SNicholas Piggin 	__die("System Reset", regs, SIGABRT);
3404388c9b3SNicholas Piggin 
3414388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
3424388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
3434388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
34414cf11afSPaul Mackerras 
345c4f3b52cSNicholas Piggin out:
346c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
347c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
348c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
3494388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable nested System Reset");
350c4f3b52cSNicholas Piggin #endif
35114cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
35214cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
3534388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable System Reset");
35414cf11afSPaul Mackerras 
3552b4f3ac5SNicholas Piggin 	if (!nested)
3562b4f3ac5SNicholas Piggin 		nmi_exit();
3572b4f3ac5SNicholas Piggin 
35814cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
35914cf11afSPaul Mackerras }
3601e9b4507SMahesh Salgaonkar 
36114cf11afSPaul Mackerras /*
36214cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
36314cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
36414cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
36514cf11afSPaul Mackerras  * table.
36614cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
36714cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
36814cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
36914cf11afSPaul Mackerras  *  -- paulus.
37014cf11afSPaul Mackerras  */
37114cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
37214cf11afSPaul Mackerras {
37368a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
37414cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
37514cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
37614cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
37714cf11afSPaul Mackerras 
37814cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
37914cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
38014cf11afSPaul Mackerras 		/*
38114cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
38214cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
38314cf11afSPaul Mackerras 		 * As the address is in the exception table
38414cf11afSPaul Mackerras 		 * we should be able to read the instr there.
38514cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
38614cf11afSPaul Mackerras 		 * load or store.
38714cf11afSPaul Mackerras 		 */
388ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
38914cf11afSPaul Mackerras 			nip -= 2;
390ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
39114cf11afSPaul Mackerras 			--nip;
392ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
39314cf11afSPaul Mackerras 			unsigned int rb;
39414cf11afSPaul Mackerras 
39514cf11afSPaul Mackerras 			--nip;
39614cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
39714cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
39814cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
39914cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
40014cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
40161a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
40214cf11afSPaul Mackerras 			return 1;
40314cf11afSPaul Mackerras 		}
40414cf11afSPaul Mackerras 	}
40568a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
40614cf11afSPaul Mackerras 	return 0;
40714cf11afSPaul Mackerras }
40814cf11afSPaul Mackerras 
409172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
41014cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
41114cf11afSPaul Mackerras    is in the ESR. */
41214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
41314cf11afSPaul Mackerras #define REASON_FP		ESR_FP
41414cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
41514cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
41614cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
41714cf11afSPaul Mackerras 
41814cf11afSPaul Mackerras /* single-step stuff */
41951ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
42051ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
42114cf11afSPaul Mackerras 
42214cf11afSPaul Mackerras #else
42314cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
42414cf11afSPaul Mackerras    exception is in the MSR. */
42514cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
426d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
427d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
428d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
429d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
430d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
43114cf11afSPaul Mackerras 
43214cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
43314cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
43414cf11afSPaul Mackerras #endif
43514cf11afSPaul Mackerras 
4360d0935b3SMichael Ellerman #if defined(CONFIG_E500)
437fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
438fe04b112SScott Wood {
439fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
440fe04b112SScott Wood 	unsigned long reason = mcsr;
441fe04b112SScott Wood 	int recoverable = 1;
442fe04b112SScott Wood 
44382a9a480SScott Wood 	if (reason & MCSR_LD) {
444cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
445cce1f106SShaohui Xie 		if (recoverable == 1)
446cce1f106SShaohui Xie 			goto silent_out;
447cce1f106SShaohui Xie 	}
448cce1f106SShaohui Xie 
449fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
450fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
451fe04b112SScott Wood 
452fe04b112SScott Wood 	if (reason & MCSR_MCP)
453fe04b112SScott Wood 		printk("Machine Check Signal\n");
454fe04b112SScott Wood 
455fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
456fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
457fe04b112SScott Wood 
458fe04b112SScott Wood 		/*
459fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
460fe04b112SScott Wood 		 */
461fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
462fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
463fe04b112SScott Wood 			;
464fe04b112SScott Wood 
465fe04b112SScott Wood 		/*
466fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
467fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
468fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
469fe04b112SScott Wood 		 */
470fe04b112SScott Wood 		reason &= ~MCSR_IF;
471fe04b112SScott Wood 	}
472fe04b112SScott Wood 
473fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
474fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
47537caf9f2SKumar Gala 
47637caf9f2SKumar Gala 		/*
47737caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
47837caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
47937caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
48037caf9f2SKumar Gala 		 */
48137caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
482fe04b112SScott Wood 			recoverable = 0;
483fe04b112SScott Wood 	}
484fe04b112SScott Wood 
485fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
486fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
487fe04b112SScott Wood 		recoverable = 0;
488fe04b112SScott Wood 	}
489fe04b112SScott Wood 
490fe04b112SScott Wood 	if (reason & MCSR_NMI)
491fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
492fe04b112SScott Wood 
493fe04b112SScott Wood 	if (reason & MCSR_IF) {
494fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
495fe04b112SScott Wood 		recoverable = 0;
496fe04b112SScott Wood 	}
497fe04b112SScott Wood 
498fe04b112SScott Wood 	if (reason & MCSR_LD) {
499fe04b112SScott Wood 		printk("Load Error Report\n");
500fe04b112SScott Wood 		recoverable = 0;
501fe04b112SScott Wood 	}
502fe04b112SScott Wood 
503fe04b112SScott Wood 	if (reason & MCSR_ST) {
504fe04b112SScott Wood 		printk("Store Error Report\n");
505fe04b112SScott Wood 		recoverable = 0;
506fe04b112SScott Wood 	}
507fe04b112SScott Wood 
508fe04b112SScott Wood 	if (reason & MCSR_LDG) {
509fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
510fe04b112SScott Wood 		recoverable = 0;
511fe04b112SScott Wood 	}
512fe04b112SScott Wood 
513fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
514fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
515fe04b112SScott Wood 
516fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
517fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
518fe04b112SScott Wood 		recoverable = 0;
519fe04b112SScott Wood 	}
520fe04b112SScott Wood 
521fe04b112SScott Wood 	if (reason & MCSR_MAV) {
522fe04b112SScott Wood 		u64 addr;
523fe04b112SScott Wood 
524fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
525fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
526fe04b112SScott Wood 
527fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
528fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
529fe04b112SScott Wood 	}
530fe04b112SScott Wood 
531cce1f106SShaohui Xie silent_out:
532fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
533fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
534fe04b112SScott Wood }
535fe04b112SScott Wood 
53647c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
53747c0bd1aSBenjamin Herrenschmidt {
53842bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
53947c0bd1aSBenjamin Herrenschmidt 
540cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
541cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
542cce1f106SShaohui Xie 			return 1;
5434e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5444e0e3435SHongtao Jia 			return 1;
545cce1f106SShaohui Xie 	}
546cce1f106SShaohui Xie 
54714cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
54814cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
54914cf11afSPaul Mackerras 
55014cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
55114cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
55214cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
55314cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
55414cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
55514cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
55614cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
55714cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
55814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
55914cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
56014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
56114cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
56214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
56314cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
56414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
56514cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
56614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
56714cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
56814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
569c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
57014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
57114cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
57214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
57314cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
57447c0bd1aSBenjamin Herrenschmidt 
57547c0bd1aSBenjamin Herrenschmidt 	return 0;
57647c0bd1aSBenjamin Herrenschmidt }
5774490c06bSKumar Gala 
5784490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5794490c06bSKumar Gala {
5804490c06bSKumar Gala 	return 0;
5814490c06bSKumar Gala }
58214cf11afSPaul Mackerras #elif defined(CONFIG_E200)
58347c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
58447c0bd1aSBenjamin Herrenschmidt {
58542bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
58647c0bd1aSBenjamin Herrenschmidt 
58714cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
58814cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
58914cf11afSPaul Mackerras 
59014cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
59114cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
59214cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
59314cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
59414cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
59514cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
59614cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
59714cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
59814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
59914cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
60014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
60114cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
60214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
60314cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
60447c0bd1aSBenjamin Herrenschmidt 
60547c0bd1aSBenjamin Herrenschmidt 	return 0;
60647c0bd1aSBenjamin Herrenschmidt }
6077f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
60847c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
60947c0bd1aSBenjamin Herrenschmidt {
61042bff234SMichael Ellerman 	unsigned long reason = regs->msr;
61147c0bd1aSBenjamin Herrenschmidt 
61214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61314cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
61414cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
61514cf11afSPaul Mackerras 	case 0x80000:
61614cf11afSPaul Mackerras 		printk("Machine check signal\n");
61714cf11afSPaul Mackerras 		break;
61814cf11afSPaul Mackerras 	case 0:		/* for 601 */
61914cf11afSPaul Mackerras 	case 0x40000:
62014cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
62114cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
62214cf11afSPaul Mackerras 		break;
62314cf11afSPaul Mackerras 	case 0x20000:
62414cf11afSPaul Mackerras 		printk("Data parity error signal\n");
62514cf11afSPaul Mackerras 		break;
62614cf11afSPaul Mackerras 	case 0x10000:
62714cf11afSPaul Mackerras 		printk("Address parity error signal\n");
62814cf11afSPaul Mackerras 		break;
62914cf11afSPaul Mackerras 	case 0x20000000:
63014cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
63114cf11afSPaul Mackerras 		break;
63214cf11afSPaul Mackerras 	case 0x40000000:
63314cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
63414cf11afSPaul Mackerras 		break;
63514cf11afSPaul Mackerras 	case 0x00100000:
63614cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
63714cf11afSPaul Mackerras 		break;
63814cf11afSPaul Mackerras 	default:
63914cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
64014cf11afSPaul Mackerras 	}
64175918a4bSOlof Johansson 	return 0;
64275918a4bSOlof Johansson }
64347c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
64475918a4bSOlof Johansson 
64575918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
64675918a4bSOlof Johansson {
64775918a4bSOlof Johansson 	int recover = 0;
648*b96672ddSNicholas Piggin 	bool nested = in_nmi();
649*b96672ddSNicholas Piggin 	if (!nested)
650*b96672ddSNicholas Piggin 		nmi_enter();
65175918a4bSOlof Johansson 
652f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
653f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
65469111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
65589713ed1SAnton Blanchard 
656d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
657d93b0ac0SMahesh Salgaonkar 
65847c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
65947c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
66047c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
66147c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
66247c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
66347c0bd1aSBenjamin Herrenschmidt 	 */
66475918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
66575918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
66647c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
66747c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
66875918a4bSOlof Johansson 
66947c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
670ba12eedeSLi Zhong 		goto bail;
67175918a4bSOlof Johansson 
672a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
673ba12eedeSLi Zhong 		goto bail;
67475918a4bSOlof Johansson 
67575918a4bSOlof Johansson 	if (check_io_access(regs))
676ba12eedeSLi Zhong 		goto bail;
67775918a4bSOlof Johansson 
6788dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
67914cf11afSPaul Mackerras 
68014cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
68114cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
682*b96672ddSNicholas Piggin 		nmi_panic(regs, "Unrecoverable Machine check");
683ba12eedeSLi Zhong 
684ba12eedeSLi Zhong bail:
685*b96672ddSNicholas Piggin 	if (!nested)
686*b96672ddSNicholas Piggin 		nmi_exit();
68714cf11afSPaul Mackerras }
68814cf11afSPaul Mackerras 
68914cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
69014cf11afSPaul Mackerras {
69114cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
69214cf11afSPaul Mackerras }
69314cf11afSPaul Mackerras 
6940869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
6950869b6fdSMahesh Salgaonkar {
6960869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
6970869b6fdSMahesh Salgaonkar 
6980869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
6990869b6fdSMahesh Salgaonkar 	irq_enter();
7000869b6fdSMahesh Salgaonkar 
7010869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
7020869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
7030869b6fdSMahesh Salgaonkar 
7040869b6fdSMahesh Salgaonkar 	irq_exit();
7050869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
7060869b6fdSMahesh Salgaonkar }
7070869b6fdSMahesh Salgaonkar 
708dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
70914cf11afSPaul Mackerras {
710ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
711ba12eedeSLi Zhong 
71214cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
71314cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
71414cf11afSPaul Mackerras 
71514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
716ba12eedeSLi Zhong 
717ba12eedeSLi Zhong 	exception_exit(prev_state);
71814cf11afSPaul Mackerras }
71914cf11afSPaul Mackerras 
720dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
72114cf11afSPaul Mackerras {
722ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
723ba12eedeSLi Zhong 
72414cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
72514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
726ba12eedeSLi Zhong 		goto bail;
72714cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
728ba12eedeSLi Zhong 		goto bail;
72914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
730ba12eedeSLi Zhong 
731ba12eedeSLi Zhong bail:
732ba12eedeSLi Zhong 	exception_exit(prev_state);
73314cf11afSPaul Mackerras }
73414cf11afSPaul Mackerras 
73514cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
73614cf11afSPaul Mackerras {
73714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
73814cf11afSPaul Mackerras }
73914cf11afSPaul Mackerras 
74003465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
74114cf11afSPaul Mackerras {
742ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
743ba12eedeSLi Zhong 
7442538c2d0SK.Prasad 	clear_single_step(regs);
74514cf11afSPaul Mackerras 
7466cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
7476cc89badSNaveen N. Rao 		return;
7486cc89badSNaveen N. Rao 
74914cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
75014cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
751ba12eedeSLi Zhong 		goto bail;
75214cf11afSPaul Mackerras 	if (debugger_sstep(regs))
753ba12eedeSLi Zhong 		goto bail;
75414cf11afSPaul Mackerras 
75514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
756ba12eedeSLi Zhong 
757ba12eedeSLi Zhong bail:
758ba12eedeSLi Zhong 	exception_exit(prev_state);
75914cf11afSPaul Mackerras }
76003465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
76114cf11afSPaul Mackerras 
76214cf11afSPaul Mackerras /*
76314cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
76414cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
76514cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
76614cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
76714cf11afSPaul Mackerras  */
7688dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
76914cf11afSPaul Mackerras {
7702538c2d0SK.Prasad 	if (single_stepping(regs))
7712538c2d0SK.Prasad 		single_step_exception(regs);
77214cf11afSPaul Mackerras }
77314cf11afSPaul Mackerras 
7745fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
775dc1c1ca3SStephen Rothwell {
7765fad293bSKumar Gala 	int ret = 0;
777dc1c1ca3SStephen Rothwell 
778dc1c1ca3SStephen Rothwell 	/* Invalid operation */
779dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7805fad293bSKumar Gala 		ret = FPE_FLTINV;
781dc1c1ca3SStephen Rothwell 
782dc1c1ca3SStephen Rothwell 	/* Overflow */
783dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7845fad293bSKumar Gala 		ret = FPE_FLTOVF;
785dc1c1ca3SStephen Rothwell 
786dc1c1ca3SStephen Rothwell 	/* Underflow */
787dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7885fad293bSKumar Gala 		ret = FPE_FLTUND;
789dc1c1ca3SStephen Rothwell 
790dc1c1ca3SStephen Rothwell 	/* Divide by zero */
791dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7925fad293bSKumar Gala 		ret = FPE_FLTDIV;
793dc1c1ca3SStephen Rothwell 
794dc1c1ca3SStephen Rothwell 	/* Inexact result */
795dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7965fad293bSKumar Gala 		ret = FPE_FLTRES;
7975fad293bSKumar Gala 
7985fad293bSKumar Gala 	return ret;
7995fad293bSKumar Gala }
8005fad293bSKumar Gala 
8015fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8025fad293bSKumar Gala {
8035fad293bSKumar Gala 	int code = 0;
8045fad293bSKumar Gala 
8055fad293bSKumar Gala 	flush_fp_to_thread(current);
8065fad293bSKumar Gala 
807de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
808dc1c1ca3SStephen Rothwell 
809dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
810dc1c1ca3SStephen Rothwell }
811dc1c1ca3SStephen Rothwell 
812dc1c1ca3SStephen Rothwell /*
813dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
81414cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
81514cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
81614cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
81714cf11afSPaul Mackerras  *
81814cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
81914cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
82014cf11afSPaul Mackerras  * bits is faster and easier.
82186417780SPaul Mackerras  *
82214cf11afSPaul Mackerras  */
82314cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
82414cf11afSPaul Mackerras {
82514cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
82614cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
82714cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
82814cf11afSPaul Mackerras 	u32 num_bytes;
82914cf11afSPaul Mackerras 	unsigned long EA;
83014cf11afSPaul Mackerras 	int pos = 0;
83114cf11afSPaul Mackerras 
83214cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
83316c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
83414cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
83514cf11afSPaul Mackerras 			return -EINVAL;
83614cf11afSPaul Mackerras 
83714cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
83814cf11afSPaul Mackerras 
83916c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
84016c57b36SKumar Gala 		case PPC_INST_LSWX:
84116c57b36SKumar Gala 		case PPC_INST_STSWX:
84214cf11afSPaul Mackerras 			EA += NB_RB;
84314cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
84414cf11afSPaul Mackerras 			break;
84516c57b36SKumar Gala 		case PPC_INST_LSWI:
84616c57b36SKumar Gala 		case PPC_INST_STSWI:
84714cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
84814cf11afSPaul Mackerras 			break;
84914cf11afSPaul Mackerras 		default:
85014cf11afSPaul Mackerras 			return -EINVAL;
85114cf11afSPaul Mackerras 	}
85214cf11afSPaul Mackerras 
85314cf11afSPaul Mackerras 	while (num_bytes != 0)
85414cf11afSPaul Mackerras 	{
85514cf11afSPaul Mackerras 		u8 val;
85614cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
85714cf11afSPaul Mackerras 
85880aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
85980aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
86080aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
86180aa0fb4SJames Yang 
86216c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
86316c57b36SKumar Gala 			case PPC_INST_LSWX:
86416c57b36SKumar Gala 			case PPC_INST_LSWI:
86514cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
86614cf11afSPaul Mackerras 					return -EFAULT;
86714cf11afSPaul Mackerras 				/* first time updating this reg,
86814cf11afSPaul Mackerras 				 * zero it out */
86914cf11afSPaul Mackerras 				if (pos == 0)
87014cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
87114cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
87214cf11afSPaul Mackerras 				break;
87316c57b36SKumar Gala 			case PPC_INST_STSWI:
87416c57b36SKumar Gala 			case PPC_INST_STSWX:
87514cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
87614cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
87714cf11afSPaul Mackerras 					return -EFAULT;
87814cf11afSPaul Mackerras 				break;
87914cf11afSPaul Mackerras 		}
88014cf11afSPaul Mackerras 		/* move EA to next address */
88114cf11afSPaul Mackerras 		EA += 1;
88214cf11afSPaul Mackerras 		num_bytes--;
88314cf11afSPaul Mackerras 
88414cf11afSPaul Mackerras 		/* manage our position within the register */
88514cf11afSPaul Mackerras 		if (++pos == 4) {
88614cf11afSPaul Mackerras 			pos = 0;
88714cf11afSPaul Mackerras 			if (++rT == 32)
88814cf11afSPaul Mackerras 				rT = 0;
88914cf11afSPaul Mackerras 		}
89014cf11afSPaul Mackerras 	}
89114cf11afSPaul Mackerras 
89214cf11afSPaul Mackerras 	return 0;
89314cf11afSPaul Mackerras }
89414cf11afSPaul Mackerras 
895c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
896c3412dcbSWill Schmidt {
897c3412dcbSWill Schmidt 	u32 ra,rs;
898c3412dcbSWill Schmidt 	unsigned long tmp;
899c3412dcbSWill Schmidt 
900c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
901c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
902c3412dcbSWill Schmidt 
903c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
904c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
905c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
906c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
907c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
908c3412dcbSWill Schmidt 
909c3412dcbSWill Schmidt 	return 0;
910c3412dcbSWill Schmidt }
911c3412dcbSWill Schmidt 
912c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
913c1469f13SKumar Gala {
914c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
915c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
916c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
917c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
918c1469f13SKumar Gala 	u8 bit;
919c1469f13SKumar Gala 	unsigned long tmp;
920c1469f13SKumar Gala 
921c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
922c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
923c1469f13SKumar Gala 
924c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
925c1469f13SKumar Gala 
926c1469f13SKumar Gala 	return 0;
927c1469f13SKumar Gala }
928c1469f13SKumar Gala 
9296ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9306ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9316ce6c629SMichael Neuling {
9326ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9336ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9346ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9356ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
9366ce6c629SMichael Neuling 	 */
9376ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
9386ce6c629SMichael Neuling 		tm_enable();
9396ce6c629SMichael Neuling 		tm_abort(cause);
9406ce6c629SMichael Neuling 		return true;
9416ce6c629SMichael Neuling 	}
9426ce6c629SMichael Neuling 	return false;
9436ce6c629SMichael Neuling }
9446ce6c629SMichael Neuling #else
9456ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
9466ce6c629SMichael Neuling {
9476ce6c629SMichael Neuling 	return false;
9486ce6c629SMichael Neuling }
9496ce6c629SMichael Neuling #endif
9506ce6c629SMichael Neuling 
95114cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
95214cf11afSPaul Mackerras {
95314cf11afSPaul Mackerras 	u32 instword;
95414cf11afSPaul Mackerras 	u32 rd;
95514cf11afSPaul Mackerras 
9564288e343SAnton Blanchard 	if (!user_mode(regs))
95714cf11afSPaul Mackerras 		return -EINVAL;
95814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
95914cf11afSPaul Mackerras 
96014cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
96114cf11afSPaul Mackerras 		return -EFAULT;
96214cf11afSPaul Mackerras 
96314cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
96416c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
965eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
96614cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
96714cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
96814cf11afSPaul Mackerras 		return 0;
96914cf11afSPaul Mackerras 	}
97014cf11afSPaul Mackerras 
97114cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
97280947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
973eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
97414cf11afSPaul Mackerras 		return 0;
97580947e7cSGeert Uytterhoeven 	}
97614cf11afSPaul Mackerras 
97714cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
97816c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
97986417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
98014cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
98114cf11afSPaul Mackerras 
982eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
98314cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
98414cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
98514cf11afSPaul Mackerras 		return 0;
98614cf11afSPaul Mackerras 	}
98714cf11afSPaul Mackerras 
98814cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
98980947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
9906ce6c629SMichael Neuling 		if (tm_abort_check(regs,
9916ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
9926ce6c629SMichael Neuling 			return -EINVAL;
993eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
99414cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
99580947e7cSGeert Uytterhoeven 	}
99614cf11afSPaul Mackerras 
997c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
99816c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
999eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1000c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1001c3412dcbSWill Schmidt 	}
1002c3412dcbSWill Schmidt 
1003c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
100416c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1005eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1006c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1007c1469f13SKumar Gala 	}
1008c1469f13SKumar Gala 
10099863c28aSJames Yang 	/* Emulate sync instruction variants */
10109863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
10119863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
10129863c28aSJames Yang 		asm volatile("sync");
10139863c28aSJames Yang 		return 0;
10149863c28aSJames Yang 	}
10159863c28aSJames Yang 
1016efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1017efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
101873d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
101973d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
102073d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
102173d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1022efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1023efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1024efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1025efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1026efcac658SAlexey Kardashevskiy 		return 0;
1027efcac658SAlexey Kardashevskiy 	}
1028efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
102973d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
103073d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
103173d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
103273d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1033efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1034efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1035efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
103600ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1037efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
103800ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1039efcac658SAlexey Kardashevskiy 		return 0;
1040efcac658SAlexey Kardashevskiy 	}
1041efcac658SAlexey Kardashevskiy #endif
1042efcac658SAlexey Kardashevskiy 
104314cf11afSPaul Mackerras 	return -EINVAL;
104414cf11afSPaul Mackerras }
104514cf11afSPaul Mackerras 
104673c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
104714cf11afSPaul Mackerras {
104873c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
104914cf11afSPaul Mackerras }
105014cf11afSPaul Mackerras 
10513a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
10523a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
10533a3b5aa6SKevin Hao {
10543a3b5aa6SKevin Hao 	int ret;
10553a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
10563a3b5aa6SKevin Hao 
10573a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
10583a3b5aa6SKevin Hao 	if (ret >= 0)
10593a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
10603a3b5aa6SKevin Hao 
10613a3b5aa6SKevin Hao 	switch (ret) {
10623a3b5aa6SKevin Hao 	case 0:
10633a3b5aa6SKevin Hao 		emulate_single_step(regs);
10643a3b5aa6SKevin Hao 		return 0;
10653a3b5aa6SKevin Hao 	case 1: {
10663a3b5aa6SKevin Hao 			int code = 0;
1067de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
10683a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
10693a3b5aa6SKevin Hao 			return 0;
10703a3b5aa6SKevin Hao 		}
10713a3b5aa6SKevin Hao 	case -EFAULT:
10723a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10733a3b5aa6SKevin Hao 		return 0;
10743a3b5aa6SKevin Hao 	}
10753a3b5aa6SKevin Hao 
10763a3b5aa6SKevin Hao 	return -1;
10773a3b5aa6SKevin Hao }
10783a3b5aa6SKevin Hao #else
10793a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
10803a3b5aa6SKevin Hao #endif
10813a3b5aa6SKevin Hao 
108203465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
108314cf11afSPaul Mackerras {
1084ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
108514cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
108614cf11afSPaul Mackerras 
1087aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
108804903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
108914cf11afSPaul Mackerras 
109014cf11afSPaul Mackerras 	if (reason & REASON_FP) {
109114cf11afSPaul Mackerras 		/* IEEE FP exception */
1092dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1093ba12eedeSLi Zhong 		goto bail;
10948dad3f92SPaul Mackerras 	}
10958dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1096a4c3f909SBalbir Singh 		unsigned long bugaddr;
1097ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1098ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1099ba797b28SJason Wessel 		if (debugger_bpt(regs))
1100ba12eedeSLi Zhong 			goto bail;
1101ba797b28SJason Wessel 
11026cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
11036cc89badSNaveen N. Rao 			goto bail;
11046cc89badSNaveen N. Rao 
110514cf11afSPaul Mackerras 		/* trap exception */
1106dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1107dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1108ba12eedeSLi Zhong 			goto bail;
110973c9ceabSJeremy Fitzhardinge 
1110a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1111a4c3f909SBalbir Singh 		/*
1112a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1113a4c3f909SBalbir Singh 		 */
1114a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1115a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1116a4c3f909SBalbir Singh 
111773c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1118a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
111914cf11afSPaul Mackerras 			regs->nip += 4;
1120ba12eedeSLi Zhong 			goto bail;
112114cf11afSPaul Mackerras 		}
11228dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1123ba12eedeSLi Zhong 		goto bail;
11248dad3f92SPaul Mackerras 	}
1125bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1126bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1127bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1128bc2a9408SMichael Neuling 		 * This occurs when:
1129bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1130bc2a9408SMichael Neuling 		 *    transition in TM states.
1131bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1132bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1133bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1134bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1135bc2a9408SMichael Neuling 		 */
1136bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1137bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1138bc2a9408SMichael Neuling 			regs->nip += 4;
1139ba12eedeSLi Zhong 			goto bail;
1140bc2a9408SMichael Neuling 		}
1141bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1142bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1143bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1144bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1145bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1146bc2a9408SMichael Neuling 		 */
1147bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1148bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1149ba12eedeSLi Zhong 			goto bail;
1150bc2a9408SMichael Neuling 		} else {
1151bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1152bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1153bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1154bc2a9408SMichael Neuling 		}
1155bc2a9408SMichael Neuling 	}
1156bc2a9408SMichael Neuling #endif
11578dad3f92SPaul Mackerras 
1158b3f6a459SMichael Ellerman 	/*
1159b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1160b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1161b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1162b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1163b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1164b3f6a459SMichael Ellerman 	 */
1165b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1166b3f6a459SMichael Ellerman 		goto sigill;
1167b3f6a459SMichael Ellerman 
1168a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1169a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1170cd8a5673SPaul Mackerras 		local_irq_enable();
1171cd8a5673SPaul Mackerras 
117204903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
117304903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
117404903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
117504903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
117604903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
117704903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
11784e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
11794e63f8edSBenjamin Herrenschmidt 	 */
11803a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1181ba12eedeSLi Zhong 		goto bail;
118204903a30SKumar Gala 
11838dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
11848dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
118514cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
118614cf11afSPaul Mackerras 		case 0:
118714cf11afSPaul Mackerras 			regs->nip += 4;
118814cf11afSPaul Mackerras 			emulate_single_step(regs);
1189ba12eedeSLi Zhong 			goto bail;
119014cf11afSPaul Mackerras 		case -EFAULT:
119114cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1192ba12eedeSLi Zhong 			goto bail;
11938dad3f92SPaul Mackerras 		}
11948dad3f92SPaul Mackerras 	}
11958dad3f92SPaul Mackerras 
1196b3f6a459SMichael Ellerman sigill:
119714cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
119814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
119914cf11afSPaul Mackerras 	else
120014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1201ba12eedeSLi Zhong 
1202ba12eedeSLi Zhong bail:
1203ba12eedeSLi Zhong 	exception_exit(prev_state);
120414cf11afSPaul Mackerras }
120503465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
120614cf11afSPaul Mackerras 
1207bf593907SPaul Mackerras /*
1208bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1209bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1210bf593907SPaul Mackerras  */
121103465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1212bf593907SPaul Mackerras {
1213bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1214bf593907SPaul Mackerras 	program_check_exception(regs);
1215bf593907SPaul Mackerras }
121603465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1217bf593907SPaul Mackerras 
1218dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
121914cf11afSPaul Mackerras {
1220ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
12214393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
122214cf11afSPaul Mackerras 
1223a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1224a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1225a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1226a3512b2dSBenjamin Herrenschmidt 
12276ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12286ce6c629SMichael Neuling 		goto bail;
12296ce6c629SMichael Neuling 
1230e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1231e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
123214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
123314cf11afSPaul Mackerras 
123414cf11afSPaul Mackerras 	if (fixed == 1) {
123514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
123614cf11afSPaul Mackerras 		emulate_single_step(regs);
1237ba12eedeSLi Zhong 		goto bail;
123814cf11afSPaul Mackerras 	}
123914cf11afSPaul Mackerras 
124014cf11afSPaul Mackerras 	/* Operand address was bad */
124114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12424393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12434393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12444393c4f6SBenjamin Herrenschmidt 	} else {
12454393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12464393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
124714cf11afSPaul Mackerras 	}
12484393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12494393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
12504393c4f6SBenjamin Herrenschmidt 	else
12514393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1252ba12eedeSLi Zhong 
1253ba12eedeSLi Zhong bail:
1254ba12eedeSLi Zhong 	exception_exit(prev_state);
125514cf11afSPaul Mackerras }
125614cf11afSPaul Mackerras 
1257f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1258f0f558b1SPaul Mackerras {
1259f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1260f0f558b1SPaul Mackerras 
1261f0f558b1SPaul Mackerras 	if (user_mode(regs))
1262f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1263f0f558b1SPaul Mackerras 	else
1264f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1265f0f558b1SPaul Mackerras 
1266f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1267f0f558b1SPaul Mackerras }
1268f0f558b1SPaul Mackerras 
126914cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
127014cf11afSPaul Mackerras {
127114cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
127214cf11afSPaul Mackerras 	       current, regs->gpr[1]);
127314cf11afSPaul Mackerras 	debugger(regs);
127414cf11afSPaul Mackerras 	show_regs(regs);
127514cf11afSPaul Mackerras 	panic("kernel stack overflow");
127614cf11afSPaul Mackerras }
127714cf11afSPaul Mackerras 
127814cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
127914cf11afSPaul Mackerras {
128014cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
128114cf11afSPaul Mackerras 	       regs->nip, regs->msr);
128214cf11afSPaul Mackerras 	debugger(regs);
128314cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
128414cf11afSPaul Mackerras }
128514cf11afSPaul Mackerras 
1286dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1287dc1c1ca3SStephen Rothwell {
1288ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1289ba12eedeSLi Zhong 
1290dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1291dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1292dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1293ba12eedeSLi Zhong 
1294ba12eedeSLi Zhong 	exception_exit(prev_state);
1295dc1c1ca3SStephen Rothwell }
1296dc1c1ca3SStephen Rothwell 
1297dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1298dc1c1ca3SStephen Rothwell {
1299ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1300ba12eedeSLi Zhong 
1301dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1302dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1303dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1304dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1305ba12eedeSLi Zhong 		goto bail;
1306dc1c1ca3SStephen Rothwell 	}
13076c4841c2SAnton Blanchard 
1308dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1309dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1310dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1311ba12eedeSLi Zhong 
1312ba12eedeSLi Zhong bail:
1313ba12eedeSLi Zhong 	exception_exit(prev_state);
1314dc1c1ca3SStephen Rothwell }
1315dc1c1ca3SStephen Rothwell 
1316ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1317ce48b210SMichael Neuling {
1318ce48b210SMichael Neuling 	if (user_mode(regs)) {
1319ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1320ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1321ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1322ce48b210SMichael Neuling 		return;
1323ce48b210SMichael Neuling 	}
1324ce48b210SMichael Neuling 
1325ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1326ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1327ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1328ce48b210SMichael Neuling }
1329ce48b210SMichael Neuling 
13302517617eSMichael Neuling #ifdef CONFIG_PPC64
1331172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1332172f7aaaSCyril Bur {
13335d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
13345d176f75SCyril Bur 	if (user_mode(regs)) {
13355d176f75SCyril Bur 		current->thread.load_tm++;
13365d176f75SCyril Bur 		regs->msr |= MSR_TM;
13375d176f75SCyril Bur 		tm_enable();
13385d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
13395d176f75SCyril Bur 		return;
13405d176f75SCyril Bur 	}
13415d176f75SCyril Bur #endif
1342172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1343172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1344172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1345172f7aaaSCyril Bur }
1346172f7aaaSCyril Bur 
1347021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1348d0c0c9a1SMichael Neuling {
1349021424a1SMichael Ellerman 	static char *facility_strings[] = {
13502517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13512517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13522517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13532517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13542517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
13552517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
13562517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
13572517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1358794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
13599b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1360021424a1SMichael Ellerman 	};
13612517617eSMichael Neuling 	char *facility = "unknown";
1362021424a1SMichael Ellerman 	u64 value;
1363c952c1c4SAnshuman Khandual 	u32 instword, rd;
13642517617eSMichael Neuling 	u8 status;
13652517617eSMichael Neuling 	bool hv;
1366021424a1SMichael Ellerman 
13672517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
13682517617eSMichael Neuling 	if (hv)
1369b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
13702517617eSMichael Neuling 	else
13712517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
13722517617eSMichael Neuling 
13732517617eSMichael Neuling 	status = value >> 56;
13742517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1375c952c1c4SAnshuman Khandual 		/*
1376c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1377c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1378c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1379c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1380c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1381c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1382c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1383c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1384c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1385c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1386c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1387c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1388c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1389c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
13902517617eSMichael Neuling 		 */
1391c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1392c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1393c952c1c4SAnshuman Khandual 			return;
1394c952c1c4SAnshuman Khandual 		}
1395c952c1c4SAnshuman Khandual 
1396c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1397c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1398c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1399c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1400c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
14012517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1402b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1403b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1404c952c1c4SAnshuman Khandual 		}
1405c952c1c4SAnshuman Khandual 
1406c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1407c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1408c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1409c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1410c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1411c952c1c4SAnshuman Khandual 				return;
1412c952c1c4SAnshuman Khandual 			}
1413c952c1c4SAnshuman Khandual 			regs->nip += 4;
1414c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1415c952c1c4SAnshuman Khandual 		}
14162517617eSMichael Neuling 		return;
1417b14b6260SMichael Ellerman 	}
1418b14b6260SMichael Ellerman 
1419172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1420172f7aaaSCyril Bur 		/*
1421172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1422172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1423172f7aaaSCyril Bur 		 *
1424172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1425172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1426172f7aaaSCyril Bur 		 * support.
1427172f7aaaSCyril Bur 		 *
1428172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1429172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1430172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1431172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1432172f7aaaSCyril Bur 		 */
1433172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1434172f7aaaSCyril Bur 			goto out;
1435172f7aaaSCyril Bur 
1436172f7aaaSCyril Bur 		tm_unavailable(regs);
1437172f7aaaSCyril Bur 		return;
1438172f7aaaSCyril Bur 	}
1439172f7aaaSCyril Bur 
144093c2ec0fSBalbir Singh 	if ((hv || status >= 2) &&
144193c2ec0fSBalbir Singh 	    (status < ARRAY_SIZE(facility_strings)) &&
14422517617eSMichael Neuling 	    facility_strings[status])
14432517617eSMichael Neuling 		facility = facility_strings[status];
1444021424a1SMichael Ellerman 
1445d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1446d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1447d0c0c9a1SMichael Neuling 		local_irq_enable();
1448d0c0c9a1SMichael Neuling 
144993c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
145093c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1451d0c0c9a1SMichael Neuling 
1452172f7aaaSCyril Bur out:
1453d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1454d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1455d0c0c9a1SMichael Neuling 		return;
1456d0c0c9a1SMichael Neuling 	}
1457d0c0c9a1SMichael Neuling 
1458021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1459d0c0c9a1SMichael Neuling }
14602517617eSMichael Neuling #endif
1461d0c0c9a1SMichael Neuling 
1462f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1463f54db641SMichael Neuling 
1464f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1465f54db641SMichael Neuling {
1466f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1467f54db641SMichael Neuling 
1468f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1469f54db641SMichael Neuling 		 regs->nip, regs->msr);
1470f54db641SMichael Neuling 
1471f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1472f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1473f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1474f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1475f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1476f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1477f54db641SMichael Neuling 	 */
1478d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1479f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1480f54db641SMichael Neuling 
1481f54db641SMichael Neuling 	/* Enable FP for the task: */
1482f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1483f54db641SMichael Neuling 
1484f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1485f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1486f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
14873ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
14883ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1489f54db641SMichael Neuling 	 */
14903ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
14913ac8ff1cSPaul Mackerras 
14923ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
14933ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
1494dc310669SCyril Bur 		msr_check_and_set(MSR_VEC);
1495dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
14963ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
14973ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14983ac8ff1cSPaul Mackerras 	}
1499f54db641SMichael Neuling }
1500f54db641SMichael Neuling 
1501f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1502f54db641SMichael Neuling {
1503f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1504f54db641SMichael Neuling 	 * the same way.
1505f54db641SMichael Neuling 	 */
1506f54db641SMichael Neuling 
1507f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1508f54db641SMichael Neuling 		 "MSR=%lx\n",
1509f54db641SMichael Neuling 		 regs->nip, regs->msr);
1510d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1511f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
15123ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1513f54db641SMichael Neuling 	current->thread.used_vr = 1;
1514f54db641SMichael Neuling 
15153ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
1516dc310669SCyril Bur 		msr_check_and_set(MSR_FP);
1517dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
15183ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15193ac8ff1cSPaul Mackerras 	}
15203ac8ff1cSPaul Mackerras }
15213ac8ff1cSPaul Mackerras 
1522f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1523f54db641SMichael Neuling {
15243ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
15253ac8ff1cSPaul Mackerras 
1526f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1527f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1528f54db641SMichael Neuling 	 *
1529f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1530f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1531f54db641SMichael Neuling 	 */
1532f54db641SMichael Neuling 
1533f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1534f54db641SMichael Neuling 		 "MSR=%lx\n",
1535f54db641SMichael Neuling 		 regs->nip, regs->msr);
1536f54db641SMichael Neuling 
15373ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
15383ac8ff1cSPaul Mackerras 
15393ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
15403ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
15413ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15423ac8ff1cSPaul Mackerras 		return;
15433ac8ff1cSPaul Mackerras 	}
15443ac8ff1cSPaul Mackerras 
1545f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1546d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1547f54db641SMichael Neuling 
1548f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1549f54db641SMichael Neuling 		MSR_VSX;
15503ac8ff1cSPaul Mackerras 
15513ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
15523ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
15533ac8ff1cSPaul Mackerras 	 */
15543ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
15553ac8ff1cSPaul Mackerras 
1556dc310669SCyril Bur 	msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1557dc310669SCyril Bur 
15583ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
1559dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
15603ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
1561dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
1562f54db641SMichael Neuling }
1563f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1564f54db641SMichael Neuling 
1565dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1566dc1c1ca3SStephen Rothwell {
156769111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
156889713ed1SAnton Blanchard 
1569dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1570dc1c1ca3SStephen Rothwell }
1571dc1c1ca3SStephen Rothwell 
1572172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
15733bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
15743bffb652SDave Kleikamp {
15753bffb652SDave Kleikamp 	int changed = 0;
15763bffb652SDave Kleikamp 	/*
15773bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
15783bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
15793bffb652SDave Kleikamp 	 */
15803bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
15813bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
15823bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
158351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
15843bffb652SDave Kleikamp #endif
15853bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
15863bffb652SDave Kleikamp 			     5);
15873bffb652SDave Kleikamp 		changed |= 0x01;
15883bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
15893bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
15903bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
15913bffb652SDave Kleikamp 			     6);
15923bffb652SDave Kleikamp 		changed |= 0x01;
15933bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
159451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
15953bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
15963bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
15973bffb652SDave Kleikamp 			     1);
15983bffb652SDave Kleikamp 		changed |= 0x01;
15993bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
160051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
16013bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
16023bffb652SDave Kleikamp 			     2);
16033bffb652SDave Kleikamp 		changed |= 0x01;
16043bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
160551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
16063bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
16073bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
16083bffb652SDave Kleikamp 			     3);
16093bffb652SDave Kleikamp 		changed |= 0x01;
16103bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
161151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
16123bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
16133bffb652SDave Kleikamp 			     4);
16143bffb652SDave Kleikamp 		changed |= 0x01;
16153bffb652SDave Kleikamp 	}
16163bffb652SDave Kleikamp 	/*
16173bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
16183bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
16193bffb652SDave Kleikamp 	 * back on or not.
16203bffb652SDave Kleikamp 	 */
162151ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
162251ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
16233bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
16243bffb652SDave Kleikamp 	else
16253bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
162651ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16273bffb652SDave Kleikamp 
16283bffb652SDave Kleikamp 	if (changed & 0x01)
162951ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
16303bffb652SDave Kleikamp }
163114cf11afSPaul Mackerras 
163203465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
163314cf11afSPaul Mackerras {
163451ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
16353bffb652SDave Kleikamp 
1636ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1637ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1638ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1639ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1640ec097c84SRoland McGrath 	 */
1641ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1642ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1643ec097c84SRoland McGrath 
1644ec097c84SRoland McGrath 		/* Disable BT */
1645ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1646ec097c84SRoland McGrath 		/* Clear the BT event */
1647ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1648ec097c84SRoland McGrath 
1649ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1650ec097c84SRoland McGrath 		if (user_mode(regs)) {
165151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
165251ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1653ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1654ec097c84SRoland McGrath 			return;
1655ec097c84SRoland McGrath 		}
1656ec097c84SRoland McGrath 
16576cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
16586cc89badSNaveen N. Rao 			return;
16596cc89badSNaveen N. Rao 
1660ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1661ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1662ec097c84SRoland McGrath 			return;
1663ec097c84SRoland McGrath 		}
1664ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1665ec097c84SRoland McGrath 			return;
1666ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
166714cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1668f8279621SKumar Gala 
166914cf11afSPaul Mackerras 		/* Disable instruction completion */
167014cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
167114cf11afSPaul Mackerras 		/* Clear the instruction completion event */
167214cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1673f8279621SKumar Gala 
16746cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
16756cc89badSNaveen N. Rao 			return;
16766cc89badSNaveen N. Rao 
1677f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1678f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
167914cf11afSPaul Mackerras 			return;
168014cf11afSPaul Mackerras 		}
1681f8279621SKumar Gala 
1682f8279621SKumar Gala 		if (debugger_sstep(regs))
1683f8279621SKumar Gala 			return;
1684f8279621SKumar Gala 
16853bffb652SDave Kleikamp 		if (user_mode(regs)) {
168651ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
168751ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
168851ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
16893bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
16903bffb652SDave Kleikamp 			else
16913bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
169251ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16933bffb652SDave Kleikamp 		}
1694f8279621SKumar Gala 
1695f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
16963bffb652SDave Kleikamp 	} else
16973bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
169814cf11afSPaul Mackerras }
169903465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1700172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
170114cf11afSPaul Mackerras 
170214cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
170314cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
170414cf11afSPaul Mackerras {
170514cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
170614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
170714cf11afSPaul Mackerras }
170814cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
170914cf11afSPaul Mackerras 
171014cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1711dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
171214cf11afSPaul Mackerras {
171314cf11afSPaul Mackerras 	int err;
171414cf11afSPaul Mackerras 
171514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
171614cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
171714cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
17188dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
171914cf11afSPaul Mackerras 	}
172014cf11afSPaul Mackerras 
1721dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1722dc1c1ca3SStephen Rothwell 
1723eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
172414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
172514cf11afSPaul Mackerras 	if (err == 0) {
172614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
172714cf11afSPaul Mackerras 		emulate_single_step(regs);
172814cf11afSPaul Mackerras 		return;
172914cf11afSPaul Mackerras 	}
173014cf11afSPaul Mackerras 
173114cf11afSPaul Mackerras 	if (err == -EFAULT) {
173214cf11afSPaul Mackerras 		/* got an error reading the instruction */
173314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
173414cf11afSPaul Mackerras 	} else {
173514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
173614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
173776462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
173814cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1739de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
174014cf11afSPaul Mackerras 	}
174114cf11afSPaul Mackerras }
174214cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
174314cf11afSPaul Mackerras 
174414cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
174514cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
174614cf11afSPaul Mackerras 			   unsigned long error_code)
174714cf11afSPaul Mackerras {
174814cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
174914cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
175014cf11afSPaul Mackerras 	 * something smarter
175114cf11afSPaul Mackerras 	 */
175214cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
175314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
175414cf11afSPaul Mackerras 	return;
175514cf11afSPaul Mackerras }
175614cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
175714cf11afSPaul Mackerras 
175814cf11afSPaul Mackerras #ifdef CONFIG_SPE
175914cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
176014cf11afSPaul Mackerras {
17616a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
176214cf11afSPaul Mackerras 	unsigned long spefscr;
176314cf11afSPaul Mackerras 	int fpexc_mode;
176414cf11afSPaul Mackerras 	int code = 0;
17656a800f36SLiu Yu 	int err;
17666a800f36SLiu Yu 
1767685659eeSyu liu 	flush_spe_to_thread(current);
176814cf11afSPaul Mackerras 
176914cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
177014cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
177114cf11afSPaul Mackerras 
177214cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
177314cf11afSPaul Mackerras 		code = FPE_FLTOVF;
177414cf11afSPaul Mackerras 	}
177514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
177614cf11afSPaul Mackerras 		code = FPE_FLTUND;
177714cf11afSPaul Mackerras 	}
177814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
177914cf11afSPaul Mackerras 		code = FPE_FLTDIV;
178014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
178114cf11afSPaul Mackerras 		code = FPE_FLTINV;
178214cf11afSPaul Mackerras 	}
178314cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
178414cf11afSPaul Mackerras 		code = FPE_FLTRES;
178514cf11afSPaul Mackerras 
17866a800f36SLiu Yu 	err = do_spe_mathemu(regs);
17876a800f36SLiu Yu 	if (err == 0) {
17886a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17896a800f36SLiu Yu 		emulate_single_step(regs);
179014cf11afSPaul Mackerras 		return;
179114cf11afSPaul Mackerras 	}
17926a800f36SLiu Yu 
17936a800f36SLiu Yu 	if (err == -EFAULT) {
17946a800f36SLiu Yu 		/* got an error reading the instruction */
17956a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17966a800f36SLiu Yu 	} else if (err == -EINVAL) {
17976a800f36SLiu Yu 		/* didn't recognize the instruction */
17986a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17996a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18006a800f36SLiu Yu 	} else {
18016a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
18026a800f36SLiu Yu 	}
18036a800f36SLiu Yu 
18046a800f36SLiu Yu 	return;
18056a800f36SLiu Yu }
18066a800f36SLiu Yu 
18076a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
18086a800f36SLiu Yu {
18096a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
18106a800f36SLiu Yu 	int err;
18116a800f36SLiu Yu 
18126a800f36SLiu Yu 	preempt_disable();
18136a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
18146a800f36SLiu Yu 		giveup_spe(current);
18156a800f36SLiu Yu 	preempt_enable();
18166a800f36SLiu Yu 
18176a800f36SLiu Yu 	regs->nip -= 4;
18186a800f36SLiu Yu 	err = speround_handler(regs);
18196a800f36SLiu Yu 	if (err == 0) {
18206a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18216a800f36SLiu Yu 		emulate_single_step(regs);
18226a800f36SLiu Yu 		return;
18236a800f36SLiu Yu 	}
18246a800f36SLiu Yu 
18256a800f36SLiu Yu 	if (err == -EFAULT) {
18266a800f36SLiu Yu 		/* got an error reading the instruction */
18276a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18286a800f36SLiu Yu 	} else if (err == -EINVAL) {
18296a800f36SLiu Yu 		/* didn't recognize the instruction */
18306a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18316a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18326a800f36SLiu Yu 	} else {
18336a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
18346a800f36SLiu Yu 		return;
18356a800f36SLiu Yu 	}
18366a800f36SLiu Yu }
183714cf11afSPaul Mackerras #endif
183814cf11afSPaul Mackerras 
1839dc1c1ca3SStephen Rothwell /*
1840dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1841dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1842dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1843dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1844dc1c1ca3SStephen Rothwell  */
1845dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1846dc1c1ca3SStephen Rothwell {
1847dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1848dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1849dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1850dc1c1ca3SStephen Rothwell }
185115770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
1852dc1c1ca3SStephen Rothwell 
18531e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
185414cf11afSPaul Mackerras /*
185514cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
185614cf11afSPaul Mackerras  * spins until a reboot occurs
185714cf11afSPaul Mackerras  */
185814cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
185914cf11afSPaul Mackerras {
186014cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
186114cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
186214cf11afSPaul Mackerras 	return;
186314cf11afSPaul Mackerras }
186414cf11afSPaul Mackerras 
186514cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
186614cf11afSPaul Mackerras {
186714cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
186814cf11afSPaul Mackerras 	WatchdogHandler(regs);
186914cf11afSPaul Mackerras }
187014cf11afSPaul Mackerras #endif
1871dc1c1ca3SStephen Rothwell 
1872dc1c1ca3SStephen Rothwell /*
1873dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1874dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1875dc1c1ca3SStephen Rothwell  */
1876dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1877dc1c1ca3SStephen Rothwell {
1878dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1879dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1880dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1881dc1c1ca3SStephen Rothwell }
188215770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
188314cf11afSPaul Mackerras 
188414cf11afSPaul Mackerras void __init trap_init(void)
188514cf11afSPaul Mackerras {
188614cf11afSPaul Mackerras }
188780947e7cSGeert Uytterhoeven 
188880947e7cSGeert Uytterhoeven 
188980947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
189080947e7cSGeert Uytterhoeven 
189180947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
189280947e7cSGeert Uytterhoeven 
189380947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
189480947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
189580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
189680947e7cSGeert Uytterhoeven #endif
189780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
189880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
189980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
190080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
190180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
190280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
190380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
190480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
190580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
190680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1907a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
190880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
190980947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
191080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
191180947e7cSGeert Uytterhoeven #endif
191280947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
191380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
191480947e7cSGeert Uytterhoeven #endif
1915efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1916efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1917efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1918f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
1919efcac658SAlexey Kardashevskiy #endif
192080947e7cSGeert Uytterhoeven };
192180947e7cSGeert Uytterhoeven 
192280947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
192380947e7cSGeert Uytterhoeven 
192480947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
192580947e7cSGeert Uytterhoeven {
192676462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
192780947e7cSGeert Uytterhoeven 			    type);
192880947e7cSGeert Uytterhoeven }
192980947e7cSGeert Uytterhoeven 
193080947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
193180947e7cSGeert Uytterhoeven {
193280947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
193380947e7cSGeert Uytterhoeven 	unsigned int i;
193480947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
193580947e7cSGeert Uytterhoeven 
193680947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
193780947e7cSGeert Uytterhoeven 		return -ENODEV;
193880947e7cSGeert Uytterhoeven 
193980947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
194080947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
194180947e7cSGeert Uytterhoeven 	if (!dir)
194280947e7cSGeert Uytterhoeven 		return -ENOMEM;
194380947e7cSGeert Uytterhoeven 
194480947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
194580947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
194680947e7cSGeert Uytterhoeven 	if (!d)
194780947e7cSGeert Uytterhoeven 		goto fail;
194880947e7cSGeert Uytterhoeven 
194980947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
195080947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
195180947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
195280947e7cSGeert Uytterhoeven 		if (!d)
195380947e7cSGeert Uytterhoeven 			goto fail;
195480947e7cSGeert Uytterhoeven 	}
195580947e7cSGeert Uytterhoeven 
195680947e7cSGeert Uytterhoeven 	return 0;
195780947e7cSGeert Uytterhoeven 
195880947e7cSGeert Uytterhoeven fail:
195980947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
196080947e7cSGeert Uytterhoeven 	return -ENOMEM;
196180947e7cSGeert Uytterhoeven }
196280947e7cSGeert Uytterhoeven 
196380947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
196480947e7cSGeert Uytterhoeven 
196580947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1966