xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 57ad583f2086d55ada284c54bfc440123cf73964)
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>
2399cd1302SRam Pai #include <linux/pkeys.h>
2414cf11afSPaul Mackerras #include <linux/stddef.h>
2514cf11afSPaul Mackerras #include <linux/unistd.h>
268dad3f92SPaul Mackerras #include <linux/ptrace.h>
2714cf11afSPaul Mackerras #include <linux/user.h>
2814cf11afSPaul Mackerras #include <linux/interrupt.h>
2914cf11afSPaul Mackerras #include <linux/init.h>
308a39b05fSPaul Gortmaker #include <linux/extable.h>
318a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
328dad3f92SPaul Mackerras #include <linux/prctl.h>
3314cf11afSPaul Mackerras #include <linux/delay.h>
3414cf11afSPaul Mackerras #include <linux/kprobes.h>
35cc532915SMichael Ellerman #include <linux/kexec.h>
365474c120SMichael Hanselmann #include <linux/backlight.h>
3773c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
381eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3976462232SChristian Dietrich #include <linux/ratelimit.h>
40ba12eedeSLi Zhong #include <linux/context_tracking.h>
415080332cSMichael Neuling #include <linux/smp.h>
4214cf11afSPaul Mackerras 
4380947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4414cf11afSPaul Mackerras #include <asm/pgtable.h>
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
467644d581SMichael Ellerman #include <asm/debugfs.h>
4714cf11afSPaul Mackerras #include <asm/io.h>
4886417780SPaul Mackerras #include <asm/machdep.h>
4986417780SPaul Mackerras #include <asm/rtas.h>
50f7f6f4feSDavid Gibson #include <asm/pmc.h>
5114cf11afSPaul Mackerras #include <asm/reg.h>
5214cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5314cf11afSPaul Mackerras #include <asm/backlight.h>
5414cf11afSPaul Mackerras #endif
55dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5686417780SPaul Mackerras #include <asm/firmware.h>
57dc1c1ca3SStephen Rothwell #include <asm/processor.h>
586ce6c629SMichael Neuling #include <asm/tm.h>
59dc1c1ca3SStephen Rothwell #endif
60c0ce7d08SDavid Wilder #include <asm/kexec.h>
6116c57b36SKumar Gala #include <asm/ppc-opcode.h>
62cce1f106SShaohui Xie #include <asm/rio.h>
63ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
64ae3a197eSDavid Howells #include <asm/switch_to.h>
65f54db641SMichael Neuling #include <asm/tm.h>
66ae3a197eSDavid Howells #include <asm/debug.h>
6742f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
68fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
694e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
706cc89badSNaveen N. Rao #include <asm/kprobes.h>
71dc1c1ca3SStephen Rothwell 
72da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
735be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
745be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
755be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
765be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
775be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
789422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
795be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
8014cf11afSPaul Mackerras 
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
869422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8814cf11afSPaul Mackerras #endif
8914cf11afSPaul Mackerras 
908b3c34cfSMichael Neuling /* Transactional Memory trap debug */
918b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
928b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
938b3c34cfSMichael Neuling #else
948b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
958b3c34cfSMichael Neuling #endif
968b3c34cfSMichael Neuling 
9714cf11afSPaul Mackerras /*
9814cf11afSPaul Mackerras  * Trap & Exception support
9914cf11afSPaul Mackerras  */
10014cf11afSPaul Mackerras 
1016031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1026031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1036031d9d9Santon@samba.org {
1046031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1056031d9d9Santon@samba.org 	if (pmac_backlight) {
1066031d9d9Santon@samba.org 		struct backlight_properties *props;
1076031d9d9Santon@samba.org 
1086031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1096031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1106031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1116031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1126031d9d9Santon@samba.org 	}
1136031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1146031d9d9Santon@samba.org }
1156031d9d9Santon@samba.org #else
1166031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1176031d9d9Santon@samba.org #endif
1186031d9d9Santon@samba.org 
1196fcd6baaSNicholas Piggin /*
1206fcd6baaSNicholas Piggin  * If oops/die is expected to crash the machine, return true here.
1216fcd6baaSNicholas Piggin  *
1226fcd6baaSNicholas Piggin  * This should not be expected to be 100% accurate, there may be
1236fcd6baaSNicholas Piggin  * notifiers registered or other unexpected conditions that may bring
1246fcd6baaSNicholas Piggin  * down the kernel. Or if the current process in the kernel is holding
1256fcd6baaSNicholas Piggin  * locks or has other critical state, the kernel may become effectively
1266fcd6baaSNicholas Piggin  * unusable anyway.
1276fcd6baaSNicholas Piggin  */
1286fcd6baaSNicholas Piggin bool die_will_crash(void)
1296fcd6baaSNicholas Piggin {
1306fcd6baaSNicholas Piggin 	if (should_fadump_crash())
1316fcd6baaSNicholas Piggin 		return true;
1326fcd6baaSNicholas Piggin 	if (kexec_should_crash(current))
1336fcd6baaSNicholas Piggin 		return true;
1346fcd6baaSNicholas Piggin 	if (in_interrupt() || panic_on_oops ||
1356fcd6baaSNicholas Piggin 			!current->pid || is_global_init(current))
1366fcd6baaSNicholas Piggin 		return true;
1376fcd6baaSNicholas Piggin 
1386fcd6baaSNicholas Piggin 	return false;
1396fcd6baaSNicholas Piggin }
1406fcd6baaSNicholas Piggin 
141760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
142760ca4dcSAnton Blanchard static int die_owner = -1;
143760ca4dcSAnton Blanchard static unsigned int die_nest_count;
144c0ce7d08SDavid Wilder static int die_counter;
145760ca4dcSAnton Blanchard 
14603465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
147760ca4dcSAnton Blanchard {
148760ca4dcSAnton Blanchard 	int cpu;
14934c2a14fSanton@samba.org 	unsigned long flags;
15014cf11afSPaul Mackerras 
151293e4688Santon@samba.org 	oops_enter();
152293e4688Santon@samba.org 
153760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
154760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
155760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
156760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
157760ca4dcSAnton Blanchard 		if (cpu == die_owner)
158760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
159760ca4dcSAnton Blanchard 		else
160760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
161760ca4dcSAnton Blanchard 	}
162760ca4dcSAnton Blanchard 	die_nest_count++;
163760ca4dcSAnton Blanchard 	die_owner = cpu;
16414cf11afSPaul Mackerras 	console_verbose();
16514cf11afSPaul Mackerras 	bust_spinlocks(1);
1666031d9d9Santon@samba.org 	if (machine_is(powermac))
1676031d9d9Santon@samba.org 		pmac_backlight_unblank();
168760ca4dcSAnton Blanchard 	return flags;
16934c2a14fSanton@samba.org }
17003465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
1715474c120SMichael Hanselmann 
17203465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
173760ca4dcSAnton Blanchard 			       int signr)
174760ca4dcSAnton Blanchard {
17514cf11afSPaul Mackerras 	bust_spinlocks(0);
176373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
177760ca4dcSAnton Blanchard 	die_nest_count--;
17858154c8cSAnton Blanchard 	oops_exit();
17958154c8cSAnton Blanchard 	printk("\n");
1807458e8b2SNicholas Piggin 	if (!die_nest_count) {
181760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
1827458e8b2SNicholas Piggin 		die_owner = -1;
183760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
1847458e8b2SNicholas Piggin 	}
185760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
186cc532915SMichael Ellerman 
187ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
188ebaeb5aeSMahesh Salgaonkar 
1894388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
190cc532915SMichael Ellerman 		crash_kexec(regs);
1919b00ac06SAnton Blanchard 
192760ca4dcSAnton Blanchard 	if (!signr)
193760ca4dcSAnton Blanchard 		return;
194760ca4dcSAnton Blanchard 
19558154c8cSAnton Blanchard 	/*
19658154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
19758154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
19858154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
19958154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
20058154c8cSAnton Blanchard 	 */
20158154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
20258154c8cSAnton Blanchard 	    is_global_init(current)) {
20358154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
20458154c8cSAnton Blanchard 	}
20558154c8cSAnton Blanchard 
20614cf11afSPaul Mackerras 	if (in_interrupt())
20714cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
208cea6a4baSHorms 	if (panic_on_oops)
209012c437dSHorms 		panic("Fatal exception");
210760ca4dcSAnton Blanchard 	do_exit(signr);
211760ca4dcSAnton Blanchard }
21203465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
213cea6a4baSHorms 
21403465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
215760ca4dcSAnton Blanchard {
216760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2172e82ca3cSMichael Ellerman 
2182e82ca3cSMichael Ellerman 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
2192e82ca3cSMichael Ellerman 		printk("LE ");
2202e82ca3cSMichael Ellerman 	else
2212e82ca3cSMichael Ellerman 		printk("BE ");
2222e82ca3cSMichael Ellerman 
2231c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_PREEMPT))
22472c0d9eeSMichael Ellerman 		pr_cont("PREEMPT ");
2251c56cd8eSMichael Ellerman 
2261c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_SMP))
22772c0d9eeSMichael Ellerman 		pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
2281c56cd8eSMichael Ellerman 
229e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
23072c0d9eeSMichael Ellerman 		pr_cont("DEBUG_PAGEALLOC ");
2311c56cd8eSMichael Ellerman 
2321c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_NUMA))
23372c0d9eeSMichael Ellerman 		pr_cont("NUMA ");
2341c56cd8eSMichael Ellerman 
23572c0d9eeSMichael Ellerman 	pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
236760ca4dcSAnton Blanchard 
237760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
238760ca4dcSAnton Blanchard 		return 1;
239760ca4dcSAnton Blanchard 
240760ca4dcSAnton Blanchard 	print_modules();
241760ca4dcSAnton Blanchard 	show_regs(regs);
24214cf11afSPaul Mackerras 
24314cf11afSPaul Mackerras 	return 0;
24414cf11afSPaul Mackerras }
24503465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
24614cf11afSPaul Mackerras 
247760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
248760ca4dcSAnton Blanchard {
2496f44b20eSNicholas Piggin 	unsigned long flags;
250760ca4dcSAnton Blanchard 
2516f44b20eSNicholas Piggin 	if (debugger(regs))
2526f44b20eSNicholas Piggin 		return;
2536f44b20eSNicholas Piggin 
2546f44b20eSNicholas Piggin 	flags = oops_begin(regs);
255760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
256760ca4dcSAnton Blanchard 		err = 0;
257760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
258760ca4dcSAnton Blanchard }
25915770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
260760ca4dcSAnton Blanchard 
26125baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
26225baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
26325baa35bSOleg Nesterov {
26425baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
26525baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
26625baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
26725baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
26825baa35bSOleg Nesterov }
26925baa35bSOleg Nesterov 
27099cd1302SRam Pai 
27199cd1302SRam Pai void _exception_pkey(int signr, struct pt_regs *regs, int code,
27299cd1302SRam Pai 		unsigned long addr, int key)
27314cf11afSPaul Mackerras {
27414cf11afSPaul Mackerras 	siginfo_t info;
275d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
276d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
277d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
278d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
27914cf11afSPaul Mackerras 
28014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
281760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
28214cf11afSPaul Mackerras 		return;
283760ca4dcSAnton Blanchard 	}
284760ca4dcSAnton Blanchard 
285760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
28676462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
287d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
288d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
28914cf11afSPaul Mackerras 	}
29014cf11afSPaul Mackerras 
291a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2929f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2939f2f79e3SBenjamin Herrenschmidt 
29441ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
295c5cc1f4dSThiago Jung Bauermann 
296c5cc1f4dSThiago Jung Bauermann 	/*
297c5cc1f4dSThiago Jung Bauermann 	 * Save all the pkey registers AMR/IAMR/UAMOR. Eg: Core dumps need
298c5cc1f4dSThiago Jung Bauermann 	 * to capture the content, if the task gets killed.
299c5cc1f4dSThiago Jung Bauermann 	 */
300c5cc1f4dSThiago Jung Bauermann 	thread_pkey_regs_save(&current->thread);
301c5cc1f4dSThiago Jung Bauermann 
30214cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
30314cf11afSPaul Mackerras 	info.si_signo = signr;
30414cf11afSPaul Mackerras 	info.si_code = code;
30514cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
30699cd1302SRam Pai 	info.si_pkey = key;
30799cd1302SRam Pai 
30814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
30914cf11afSPaul Mackerras }
31014cf11afSPaul Mackerras 
31199cd1302SRam Pai void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
31299cd1302SRam Pai {
31399cd1302SRam Pai 	_exception_pkey(signr, regs, code, addr, 0);
31499cd1302SRam Pai }
31599cd1302SRam Pai 
31614cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
31714cf11afSPaul Mackerras {
3182b4f3ac5SNicholas Piggin 	/*
3192b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
3202b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
3212b4f3ac5SNicholas Piggin 	 */
3222b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
3232b4f3ac5SNicholas Piggin 	if (!nested)
3242b4f3ac5SNicholas Piggin 		nmi_enter();
3252b4f3ac5SNicholas Piggin 
326ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
327ca41ad43SNicholas Piggin 
32814cf11afSPaul Mackerras 	/* See if any machine dependent calls */
329c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
330c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
331c4f3b52cSNicholas Piggin 			goto out;
332c902be71SArnd Bergmann 	}
33314cf11afSPaul Mackerras 
3344388c9b3SNicholas Piggin 	if (debugger(regs))
3354388c9b3SNicholas Piggin 		goto out;
3364388c9b3SNicholas Piggin 
3374388c9b3SNicholas Piggin 	/*
3384388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
3394388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
3404388c9b3SNicholas Piggin 	 * registered).
3414388c9b3SNicholas Piggin 	 */
3424388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
3434388c9b3SNicholas Piggin 
3444388c9b3SNicholas Piggin 	crash_kexec(regs);
3454388c9b3SNicholas Piggin 
3464388c9b3SNicholas Piggin 	/*
3474388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
3484388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
3494388c9b3SNicholas Piggin 	 * code.
3504388c9b3SNicholas Piggin 	 */
3514388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
3524388c9b3SNicholas Piggin 
3534388c9b3SNicholas Piggin 	/*
3544388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
3554388c9b3SNicholas Piggin 	 * panic.
3564388c9b3SNicholas Piggin 	 */
3574552d128SNicholas Piggin 	die("System Reset", regs, SIGABRT);
3584388c9b3SNicholas Piggin 
3594388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
3604388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
3614388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
36214cf11afSPaul Mackerras 
363c4f3b52cSNicholas Piggin out:
364c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
365c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
366c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
3674388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable nested System Reset");
368c4f3b52cSNicholas Piggin #endif
36914cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
37014cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
3714388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable System Reset");
37214cf11afSPaul Mackerras 
3732b4f3ac5SNicholas Piggin 	if (!nested)
3742b4f3ac5SNicholas Piggin 		nmi_exit();
3752b4f3ac5SNicholas Piggin 
37614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
37714cf11afSPaul Mackerras }
3781e9b4507SMahesh Salgaonkar 
37914cf11afSPaul Mackerras /*
38014cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
38114cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
38214cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
38314cf11afSPaul Mackerras  * table.
38414cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
38514cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
38614cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
38714cf11afSPaul Mackerras  *  -- paulus.
38814cf11afSPaul Mackerras  */
38914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
39014cf11afSPaul Mackerras {
39168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
39214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
39314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
39414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
39514cf11afSPaul Mackerras 
39614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
39714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
39814cf11afSPaul Mackerras 		/*
39914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
40014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
40114cf11afSPaul Mackerras 		 * As the address is in the exception table
40214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
40314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
40414cf11afSPaul Mackerras 		 * load or store.
40514cf11afSPaul Mackerras 		 */
406ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
40714cf11afSPaul Mackerras 			nip -= 2;
408ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
40914cf11afSPaul Mackerras 			--nip;
410ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
41114cf11afSPaul Mackerras 			unsigned int rb;
41214cf11afSPaul Mackerras 
41314cf11afSPaul Mackerras 			--nip;
41414cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
41514cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
41614cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
41714cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
41814cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
41961a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
42014cf11afSPaul Mackerras 			return 1;
42114cf11afSPaul Mackerras 		}
42214cf11afSPaul Mackerras 	}
42368a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
42414cf11afSPaul Mackerras 	return 0;
42514cf11afSPaul Mackerras }
42614cf11afSPaul Mackerras 
427172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
42814cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
42914cf11afSPaul Mackerras    is in the ESR. */
43014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
43114cf11afSPaul Mackerras #define REASON_FP		ESR_FP
43214cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
43314cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
43414cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
43514cf11afSPaul Mackerras 
43614cf11afSPaul Mackerras /* single-step stuff */
43751ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
43851ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
43914cf11afSPaul Mackerras 
44014cf11afSPaul Mackerras #else
44114cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
44214cf11afSPaul Mackerras    exception is in the MSR. */
44314cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
444d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
445d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
446d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
447d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
448d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
44914cf11afSPaul Mackerras 
45014cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
45114cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
45214cf11afSPaul Mackerras #endif
45314cf11afSPaul Mackerras 
4540d0935b3SMichael Ellerman #if defined(CONFIG_E500)
455fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
456fe04b112SScott Wood {
457fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
458a4e89ffbSMatt Weber 	unsigned long pvr = mfspr(SPRN_PVR);
459fe04b112SScott Wood 	unsigned long reason = mcsr;
460fe04b112SScott Wood 	int recoverable = 1;
461fe04b112SScott Wood 
46282a9a480SScott Wood 	if (reason & MCSR_LD) {
463cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
464cce1f106SShaohui Xie 		if (recoverable == 1)
465cce1f106SShaohui Xie 			goto silent_out;
466cce1f106SShaohui Xie 	}
467cce1f106SShaohui Xie 
468fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
469fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
470fe04b112SScott Wood 
471fe04b112SScott Wood 	if (reason & MCSR_MCP)
472fe04b112SScott Wood 		printk("Machine Check Signal\n");
473fe04b112SScott Wood 
474fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
475fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
476fe04b112SScott Wood 
477fe04b112SScott Wood 		/*
478fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
479fe04b112SScott Wood 		 */
480fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
481fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
482fe04b112SScott Wood 			;
483fe04b112SScott Wood 
484fe04b112SScott Wood 		/*
485fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
486fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
487fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
488fe04b112SScott Wood 		 */
489fe04b112SScott Wood 		reason &= ~MCSR_IF;
490fe04b112SScott Wood 	}
491fe04b112SScott Wood 
492fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
493fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
49437caf9f2SKumar Gala 
49537caf9f2SKumar Gala 		/*
49637caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
49737caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
49837caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
49937caf9f2SKumar Gala 		 */
500a4e89ffbSMatt Weber 		/* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
501a4e89ffbSMatt Weber 		 * is not implemented but L1 data cache always runs in write
502a4e89ffbSMatt Weber 		 * shadow mode. Hence on data cache parity errors HW will
503a4e89ffbSMatt Weber 		 * automatically invalidate the L1 Data Cache.
504a4e89ffbSMatt Weber 		 */
505a4e89ffbSMatt Weber 		if (PVR_VER(pvr) != PVR_VER_E6500) {
50637caf9f2SKumar Gala 			if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
507fe04b112SScott Wood 				recoverable = 0;
508fe04b112SScott Wood 		}
509a4e89ffbSMatt Weber 	}
510fe04b112SScott Wood 
511fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
512fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
513fe04b112SScott Wood 		recoverable = 0;
514fe04b112SScott Wood 	}
515fe04b112SScott Wood 
516fe04b112SScott Wood 	if (reason & MCSR_NMI)
517fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
518fe04b112SScott Wood 
519fe04b112SScott Wood 	if (reason & MCSR_IF) {
520fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
521fe04b112SScott Wood 		recoverable = 0;
522fe04b112SScott Wood 	}
523fe04b112SScott Wood 
524fe04b112SScott Wood 	if (reason & MCSR_LD) {
525fe04b112SScott Wood 		printk("Load Error Report\n");
526fe04b112SScott Wood 		recoverable = 0;
527fe04b112SScott Wood 	}
528fe04b112SScott Wood 
529fe04b112SScott Wood 	if (reason & MCSR_ST) {
530fe04b112SScott Wood 		printk("Store Error Report\n");
531fe04b112SScott Wood 		recoverable = 0;
532fe04b112SScott Wood 	}
533fe04b112SScott Wood 
534fe04b112SScott Wood 	if (reason & MCSR_LDG) {
535fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
536fe04b112SScott Wood 		recoverable = 0;
537fe04b112SScott Wood 	}
538fe04b112SScott Wood 
539fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
540fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
541fe04b112SScott Wood 
542fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
543fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
544fe04b112SScott Wood 		recoverable = 0;
545fe04b112SScott Wood 	}
546fe04b112SScott Wood 
547fe04b112SScott Wood 	if (reason & MCSR_MAV) {
548fe04b112SScott Wood 		u64 addr;
549fe04b112SScott Wood 
550fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
551fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
552fe04b112SScott Wood 
553fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
554fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
555fe04b112SScott Wood 	}
556fe04b112SScott Wood 
557cce1f106SShaohui Xie silent_out:
558fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
559fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
560fe04b112SScott Wood }
561fe04b112SScott Wood 
56247c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
56347c0bd1aSBenjamin Herrenschmidt {
56442bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
56547c0bd1aSBenjamin Herrenschmidt 
566cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
567cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
568cce1f106SShaohui Xie 			return 1;
5694e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5704e0e3435SHongtao Jia 			return 1;
571cce1f106SShaohui Xie 	}
572cce1f106SShaohui Xie 
57314cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57414cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
57514cf11afSPaul Mackerras 
57614cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
57714cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
57814cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
57914cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
58014cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
58114cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
58214cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
58314cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
58414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
58514cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
58614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
58714cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
58814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
58914cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
59014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
59114cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
59214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
59314cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
595c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
59614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
59714cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
59814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
59914cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
60047c0bd1aSBenjamin Herrenschmidt 
60147c0bd1aSBenjamin Herrenschmidt 	return 0;
60247c0bd1aSBenjamin Herrenschmidt }
6034490c06bSKumar Gala 
6044490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6054490c06bSKumar Gala {
6064490c06bSKumar Gala 	return 0;
6074490c06bSKumar Gala }
60814cf11afSPaul Mackerras #elif defined(CONFIG_E200)
60947c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
61047c0bd1aSBenjamin Herrenschmidt {
61142bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
61247c0bd1aSBenjamin Herrenschmidt 
61314cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61414cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
61514cf11afSPaul Mackerras 
61614cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
61714cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
61814cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
61914cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
62014cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
62114cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
62214cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
62314cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
62414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
62514cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
62614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
62714cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
62814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
62914cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
63047c0bd1aSBenjamin Herrenschmidt 
63147c0bd1aSBenjamin Herrenschmidt 	return 0;
63247c0bd1aSBenjamin Herrenschmidt }
6337f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
63447c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
63547c0bd1aSBenjamin Herrenschmidt {
63642bff234SMichael Ellerman 	unsigned long reason = regs->msr;
63747c0bd1aSBenjamin Herrenschmidt 
63814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
63914cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
64014cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
64114cf11afSPaul Mackerras 	case 0x80000:
64214cf11afSPaul Mackerras 		printk("Machine check signal\n");
64314cf11afSPaul Mackerras 		break;
64414cf11afSPaul Mackerras 	case 0:		/* for 601 */
64514cf11afSPaul Mackerras 	case 0x40000:
64614cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
64714cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
64814cf11afSPaul Mackerras 		break;
64914cf11afSPaul Mackerras 	case 0x20000:
65014cf11afSPaul Mackerras 		printk("Data parity error signal\n");
65114cf11afSPaul Mackerras 		break;
65214cf11afSPaul Mackerras 	case 0x10000:
65314cf11afSPaul Mackerras 		printk("Address parity error signal\n");
65414cf11afSPaul Mackerras 		break;
65514cf11afSPaul Mackerras 	case 0x20000000:
65614cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
65714cf11afSPaul Mackerras 		break;
65814cf11afSPaul Mackerras 	case 0x40000000:
65914cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
66014cf11afSPaul Mackerras 		break;
66114cf11afSPaul Mackerras 	case 0x00100000:
66214cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
66314cf11afSPaul Mackerras 		break;
66414cf11afSPaul Mackerras 	default:
66514cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
66614cf11afSPaul Mackerras 	}
66775918a4bSOlof Johansson 	return 0;
66875918a4bSOlof Johansson }
66947c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
67075918a4bSOlof Johansson 
67175918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
67275918a4bSOlof Johansson {
67375918a4bSOlof Johansson 	int recover = 0;
674b96672ddSNicholas Piggin 	bool nested = in_nmi();
675b96672ddSNicholas Piggin 	if (!nested)
676b96672ddSNicholas Piggin 		nmi_enter();
67775918a4bSOlof Johansson 
678f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
679f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
68069111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
68189713ed1SAnton Blanchard 
682d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
683d93b0ac0SMahesh Salgaonkar 
68447c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
68547c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
68647c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
68747c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
68847c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
68947c0bd1aSBenjamin Herrenschmidt 	 */
69075918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
69175918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
69247c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
69347c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
69475918a4bSOlof Johansson 
69547c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
696ba12eedeSLi Zhong 		goto bail;
69775918a4bSOlof Johansson 
698a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
699ba12eedeSLi Zhong 		goto bail;
70075918a4bSOlof Johansson 
70175918a4bSOlof Johansson 	if (check_io_access(regs))
702ba12eedeSLi Zhong 		goto bail;
70375918a4bSOlof Johansson 
7048dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
70514cf11afSPaul Mackerras 
70614cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
70714cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
708b96672ddSNicholas Piggin 		nmi_panic(regs, "Unrecoverable Machine check");
709ba12eedeSLi Zhong 
710ba12eedeSLi Zhong bail:
711b96672ddSNicholas Piggin 	if (!nested)
712b96672ddSNicholas Piggin 		nmi_exit();
71314cf11afSPaul Mackerras }
71414cf11afSPaul Mackerras 
71514cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
71614cf11afSPaul Mackerras {
71714cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
71814cf11afSPaul Mackerras }
71914cf11afSPaul Mackerras 
7205080332cSMichael Neuling #ifdef CONFIG_VSX
7215080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs)
7225080332cSMichael Neuling {
7235080332cSMichael Neuling 	unsigned int ra, rb, t, i, sel, instr, rc;
7245080332cSMichael Neuling 	const void __user *addr;
7255080332cSMichael Neuling 	u8 vbuf[16], *vdst;
7265080332cSMichael Neuling 	unsigned long ea, msr, msr_mask;
7275080332cSMichael Neuling 	bool swap;
7285080332cSMichael Neuling 
7295080332cSMichael Neuling 	if (__get_user_inatomic(instr, (unsigned int __user *)regs->nip))
7305080332cSMichael Neuling 		return;
7315080332cSMichael Neuling 
7325080332cSMichael Neuling 	/*
7335080332cSMichael Neuling 	 * lxvb16x	opcode: 0x7c0006d8
7345080332cSMichael Neuling 	 * lxvd2x	opcode: 0x7c000698
7355080332cSMichael Neuling 	 * lxvh8x	opcode: 0x7c000658
7365080332cSMichael Neuling 	 * lxvw4x	opcode: 0x7c000618
7375080332cSMichael Neuling 	 */
7385080332cSMichael Neuling 	if ((instr & 0xfc00073e) != 0x7c000618) {
7395080332cSMichael Neuling 		pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
7405080332cSMichael Neuling 			 " instr=%08x\n",
7415080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7425080332cSMichael Neuling 			 regs->nip, instr);
7435080332cSMichael Neuling 		return;
7445080332cSMichael Neuling 	}
7455080332cSMichael Neuling 
7465080332cSMichael Neuling 	/* Grab vector registers into the task struct */
7475080332cSMichael Neuling 	msr = regs->msr; /* Grab msr before we flush the bits */
7485080332cSMichael Neuling 	flush_vsx_to_thread(current);
7495080332cSMichael Neuling 	enable_kernel_altivec();
7505080332cSMichael Neuling 
7515080332cSMichael Neuling 	/*
7525080332cSMichael Neuling 	 * Is userspace running with a different endian (this is rare but
7535080332cSMichael Neuling 	 * not impossible)
7545080332cSMichael Neuling 	 */
7555080332cSMichael Neuling 	swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
7565080332cSMichael Neuling 
7575080332cSMichael Neuling 	/* Decode the instruction */
7585080332cSMichael Neuling 	ra = (instr >> 16) & 0x1f;
7595080332cSMichael Neuling 	rb = (instr >> 11) & 0x1f;
7605080332cSMichael Neuling 	t = (instr >> 21) & 0x1f;
7615080332cSMichael Neuling 	if (instr & 1)
7625080332cSMichael Neuling 		vdst = (u8 *)&current->thread.vr_state.vr[t];
7635080332cSMichael Neuling 	else
7645080332cSMichael Neuling 		vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
7655080332cSMichael Neuling 
7665080332cSMichael Neuling 	/* Grab the vector address */
7675080332cSMichael Neuling 	ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
7685080332cSMichael Neuling 	if (is_32bit_task())
7695080332cSMichael Neuling 		ea &= 0xfffffffful;
7705080332cSMichael Neuling 	addr = (__force const void __user *)ea;
7715080332cSMichael Neuling 
7725080332cSMichael Neuling 	/* Check it */
7735080332cSMichael Neuling 	if (!access_ok(VERIFY_READ, addr, 16)) {
7745080332cSMichael Neuling 		pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
7755080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
7765080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7775080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
7785080332cSMichael Neuling 		return;
7795080332cSMichael Neuling 	}
7805080332cSMichael Neuling 
7815080332cSMichael Neuling 	/* Read the vector */
7825080332cSMichael Neuling 	rc = 0;
7835080332cSMichael Neuling 	if ((unsigned long)addr & 0xfUL)
7845080332cSMichael Neuling 		/* unaligned case */
7855080332cSMichael Neuling 		rc = __copy_from_user_inatomic(vbuf, addr, 16);
7865080332cSMichael Neuling 	else
7875080332cSMichael Neuling 		__get_user_atomic_128_aligned(vbuf, addr, rc);
7885080332cSMichael Neuling 	if (rc) {
7895080332cSMichael Neuling 		pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
7905080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
7915080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
7925080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
7935080332cSMichael Neuling 		return;
7945080332cSMichael Neuling 	}
7955080332cSMichael Neuling 
7965080332cSMichael Neuling 	pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
7975080332cSMichael Neuling 		 " instr=%08x addr=%016lx\n",
7985080332cSMichael Neuling 		 smp_processor_id(), current->comm, current->pid, regs->nip,
7995080332cSMichael Neuling 		 instr, (unsigned long) addr);
8005080332cSMichael Neuling 
8015080332cSMichael Neuling 	/* Grab instruction "selector" */
8025080332cSMichael Neuling 	sel = (instr >> 6) & 3;
8035080332cSMichael Neuling 
8045080332cSMichael Neuling 	/*
8055080332cSMichael Neuling 	 * Check to make sure the facility is actually enabled. This
8065080332cSMichael Neuling 	 * could happen if we get a false positive hit.
8075080332cSMichael Neuling 	 *
8085080332cSMichael Neuling 	 * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
8095080332cSMichael Neuling 	 * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
8105080332cSMichael Neuling 	 */
8115080332cSMichael Neuling 	msr_mask = MSR_VSX;
8125080332cSMichael Neuling 	if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
8135080332cSMichael Neuling 		msr_mask = MSR_VEC;
8145080332cSMichael Neuling 	if (!(msr & msr_mask)) {
8155080332cSMichael Neuling 		pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
8165080332cSMichael Neuling 			 " instr=%08x msr:%016lx\n",
8175080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8185080332cSMichael Neuling 			 regs->nip, instr, msr);
8195080332cSMichael Neuling 		return;
8205080332cSMichael Neuling 	}
8215080332cSMichael Neuling 
8225080332cSMichael Neuling 	/* Do logging here before we modify sel based on endian */
8235080332cSMichael Neuling 	switch (sel) {
8245080332cSMichael Neuling 	case 0:	/* lxvw4x */
8255080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvw4x, regs);
8265080332cSMichael Neuling 		break;
8275080332cSMichael Neuling 	case 1: /* lxvh8x */
8285080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvh8x, regs);
8295080332cSMichael Neuling 		break;
8305080332cSMichael Neuling 	case 2: /* lxvd2x */
8315080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvd2x, regs);
8325080332cSMichael Neuling 		break;
8335080332cSMichael Neuling 	case 3: /* lxvb16x */
8345080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvb16x, regs);
8355080332cSMichael Neuling 		break;
8365080332cSMichael Neuling 	}
8375080332cSMichael Neuling 
8385080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__
8395080332cSMichael Neuling 	/*
8405080332cSMichael Neuling 	 * An LE kernel stores the vector in the task struct as an LE
8415080332cSMichael Neuling 	 * byte array (effectively swapping both the components and
8425080332cSMichael Neuling 	 * the content of the components). Those instructions expect
8435080332cSMichael Neuling 	 * the components to remain in ascending address order, so we
8445080332cSMichael Neuling 	 * swap them back.
8455080332cSMichael Neuling 	 *
8465080332cSMichael Neuling 	 * If we are running a BE user space, the expectation is that
8475080332cSMichael Neuling 	 * of a simple memcpy, so forcing the emulation to look like
8485080332cSMichael Neuling 	 * a lxvb16x should do the trick.
8495080332cSMichael Neuling 	 */
8505080332cSMichael Neuling 	if (swap)
8515080332cSMichael Neuling 		sel = 3;
8525080332cSMichael Neuling 
8535080332cSMichael Neuling 	switch (sel) {
8545080332cSMichael Neuling 	case 0:	/* lxvw4x */
8555080332cSMichael Neuling 		for (i = 0; i < 4; i++)
8565080332cSMichael Neuling 			((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
8575080332cSMichael Neuling 		break;
8585080332cSMichael Neuling 	case 1: /* lxvh8x */
8595080332cSMichael Neuling 		for (i = 0; i < 8; i++)
8605080332cSMichael Neuling 			((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
8615080332cSMichael Neuling 		break;
8625080332cSMichael Neuling 	case 2: /* lxvd2x */
8635080332cSMichael Neuling 		for (i = 0; i < 2; i++)
8645080332cSMichael Neuling 			((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
8655080332cSMichael Neuling 		break;
8665080332cSMichael Neuling 	case 3: /* lxvb16x */
8675080332cSMichael Neuling 		for (i = 0; i < 16; i++)
8685080332cSMichael Neuling 			vdst[i] = vbuf[15-i];
8695080332cSMichael Neuling 		break;
8705080332cSMichael Neuling 	}
8715080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */
8725080332cSMichael Neuling 	/* On a big endian kernel, a BE userspace only needs a memcpy */
8735080332cSMichael Neuling 	if (!swap)
8745080332cSMichael Neuling 		sel = 3;
8755080332cSMichael Neuling 
8765080332cSMichael Neuling 	/* Otherwise, we need to swap the content of the components */
8775080332cSMichael Neuling 	switch (sel) {
8785080332cSMichael Neuling 	case 0:	/* lxvw4x */
8795080332cSMichael Neuling 		for (i = 0; i < 4; i++)
8805080332cSMichael Neuling 			((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
8815080332cSMichael Neuling 		break;
8825080332cSMichael Neuling 	case 1: /* lxvh8x */
8835080332cSMichael Neuling 		for (i = 0; i < 8; i++)
8845080332cSMichael Neuling 			((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
8855080332cSMichael Neuling 		break;
8865080332cSMichael Neuling 	case 2: /* lxvd2x */
8875080332cSMichael Neuling 		for (i = 0; i < 2; i++)
8885080332cSMichael Neuling 			((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
8895080332cSMichael Neuling 		break;
8905080332cSMichael Neuling 	case 3: /* lxvb16x */
8915080332cSMichael Neuling 		memcpy(vdst, vbuf, 16);
8925080332cSMichael Neuling 		break;
8935080332cSMichael Neuling 	}
8945080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */
8955080332cSMichael Neuling 
8965080332cSMichael Neuling 	/* Go to next instruction */
8975080332cSMichael Neuling 	regs->nip += 4;
8985080332cSMichael Neuling }
8995080332cSMichael Neuling #endif /* CONFIG_VSX */
9005080332cSMichael Neuling 
9010869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
9020869b6fdSMahesh Salgaonkar {
9030869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
9040869b6fdSMahesh Salgaonkar 
9050869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
9060869b6fdSMahesh Salgaonkar 	irq_enter();
9070869b6fdSMahesh Salgaonkar 
9085080332cSMichael Neuling #ifdef CONFIG_VSX
9095080332cSMichael Neuling 	/* Real mode flagged P9 special emu is needed */
9105080332cSMichael Neuling 	if (local_paca->hmi_p9_special_emu) {
9115080332cSMichael Neuling 		local_paca->hmi_p9_special_emu = 0;
9125080332cSMichael Neuling 
9135080332cSMichael Neuling 		/*
9145080332cSMichael Neuling 		 * We don't want to take page faults while doing the
9155080332cSMichael Neuling 		 * emulation, we just replay the instruction if necessary.
9165080332cSMichael Neuling 		 */
9175080332cSMichael Neuling 		pagefault_disable();
9185080332cSMichael Neuling 		p9_hmi_special_emu(regs);
9195080332cSMichael Neuling 		pagefault_enable();
9205080332cSMichael Neuling 	}
9215080332cSMichael Neuling #endif /* CONFIG_VSX */
9225080332cSMichael Neuling 
9230869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
9240869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
9250869b6fdSMahesh Salgaonkar 
9260869b6fdSMahesh Salgaonkar 	irq_exit();
9270869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
9280869b6fdSMahesh Salgaonkar }
9290869b6fdSMahesh Salgaonkar 
930dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
93114cf11afSPaul Mackerras {
932ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
933ba12eedeSLi Zhong 
93414cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
93514cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
93614cf11afSPaul Mackerras 
93714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
938ba12eedeSLi Zhong 
939ba12eedeSLi Zhong 	exception_exit(prev_state);
94014cf11afSPaul Mackerras }
94114cf11afSPaul Mackerras 
942dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
94314cf11afSPaul Mackerras {
944ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
945ba12eedeSLi Zhong 
94614cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
94714cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
948ba12eedeSLi Zhong 		goto bail;
94914cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
950ba12eedeSLi Zhong 		goto bail;
95114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
952ba12eedeSLi Zhong 
953ba12eedeSLi Zhong bail:
954ba12eedeSLi Zhong 	exception_exit(prev_state);
95514cf11afSPaul Mackerras }
95614cf11afSPaul Mackerras 
95714cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
95814cf11afSPaul Mackerras {
95914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
96014cf11afSPaul Mackerras }
96114cf11afSPaul Mackerras 
96203465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
96314cf11afSPaul Mackerras {
964ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
965ba12eedeSLi Zhong 
9662538c2d0SK.Prasad 	clear_single_step(regs);
96714cf11afSPaul Mackerras 
9686cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
9696cc89badSNaveen N. Rao 		return;
9706cc89badSNaveen N. Rao 
97114cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
97214cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
973ba12eedeSLi Zhong 		goto bail;
97414cf11afSPaul Mackerras 	if (debugger_sstep(regs))
975ba12eedeSLi Zhong 		goto bail;
97614cf11afSPaul Mackerras 
97714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
978ba12eedeSLi Zhong 
979ba12eedeSLi Zhong bail:
980ba12eedeSLi Zhong 	exception_exit(prev_state);
98114cf11afSPaul Mackerras }
98203465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
98314cf11afSPaul Mackerras 
98414cf11afSPaul Mackerras /*
98514cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
98614cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
98714cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
98814cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
98914cf11afSPaul Mackerras  */
9908dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
99114cf11afSPaul Mackerras {
9922538c2d0SK.Prasad 	if (single_stepping(regs))
9932538c2d0SK.Prasad 		single_step_exception(regs);
99414cf11afSPaul Mackerras }
99514cf11afSPaul Mackerras 
9965fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
997dc1c1ca3SStephen Rothwell {
9985fad293bSKumar Gala 	int ret = 0;
999dc1c1ca3SStephen Rothwell 
1000dc1c1ca3SStephen Rothwell 	/* Invalid operation */
1001dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
10025fad293bSKumar Gala 		ret = FPE_FLTINV;
1003dc1c1ca3SStephen Rothwell 
1004dc1c1ca3SStephen Rothwell 	/* Overflow */
1005dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
10065fad293bSKumar Gala 		ret = FPE_FLTOVF;
1007dc1c1ca3SStephen Rothwell 
1008dc1c1ca3SStephen Rothwell 	/* Underflow */
1009dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
10105fad293bSKumar Gala 		ret = FPE_FLTUND;
1011dc1c1ca3SStephen Rothwell 
1012dc1c1ca3SStephen Rothwell 	/* Divide by zero */
1013dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
10145fad293bSKumar Gala 		ret = FPE_FLTDIV;
1015dc1c1ca3SStephen Rothwell 
1016dc1c1ca3SStephen Rothwell 	/* Inexact result */
1017dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
10185fad293bSKumar Gala 		ret = FPE_FLTRES;
10195fad293bSKumar Gala 
10205fad293bSKumar Gala 	return ret;
10215fad293bSKumar Gala }
10225fad293bSKumar Gala 
10235fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
10245fad293bSKumar Gala {
10255fad293bSKumar Gala 	int code = 0;
10265fad293bSKumar Gala 
10275fad293bSKumar Gala 	flush_fp_to_thread(current);
10285fad293bSKumar Gala 
1029de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
1030dc1c1ca3SStephen Rothwell 
1031dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
1032dc1c1ca3SStephen Rothwell }
1033dc1c1ca3SStephen Rothwell 
1034dc1c1ca3SStephen Rothwell /*
1035dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
103614cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
103714cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
103814cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
103914cf11afSPaul Mackerras  *
104014cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
104114cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
104214cf11afSPaul Mackerras  * bits is faster and easier.
104386417780SPaul Mackerras  *
104414cf11afSPaul Mackerras  */
104514cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
104614cf11afSPaul Mackerras {
104714cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
104814cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
104914cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
105014cf11afSPaul Mackerras 	u32 num_bytes;
105114cf11afSPaul Mackerras 	unsigned long EA;
105214cf11afSPaul Mackerras 	int pos = 0;
105314cf11afSPaul Mackerras 
105414cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
105516c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
105614cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
105714cf11afSPaul Mackerras 			return -EINVAL;
105814cf11afSPaul Mackerras 
105914cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
106014cf11afSPaul Mackerras 
106116c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
106216c57b36SKumar Gala 		case PPC_INST_LSWX:
106316c57b36SKumar Gala 		case PPC_INST_STSWX:
106414cf11afSPaul Mackerras 			EA += NB_RB;
106514cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
106614cf11afSPaul Mackerras 			break;
106716c57b36SKumar Gala 		case PPC_INST_LSWI:
106816c57b36SKumar Gala 		case PPC_INST_STSWI:
106914cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
107014cf11afSPaul Mackerras 			break;
107114cf11afSPaul Mackerras 		default:
107214cf11afSPaul Mackerras 			return -EINVAL;
107314cf11afSPaul Mackerras 	}
107414cf11afSPaul Mackerras 
107514cf11afSPaul Mackerras 	while (num_bytes != 0)
107614cf11afSPaul Mackerras 	{
107714cf11afSPaul Mackerras 		u8 val;
107814cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
107914cf11afSPaul Mackerras 
108080aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
108180aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
108280aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
108380aa0fb4SJames Yang 
108416c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
108516c57b36SKumar Gala 			case PPC_INST_LSWX:
108616c57b36SKumar Gala 			case PPC_INST_LSWI:
108714cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
108814cf11afSPaul Mackerras 					return -EFAULT;
108914cf11afSPaul Mackerras 				/* first time updating this reg,
109014cf11afSPaul Mackerras 				 * zero it out */
109114cf11afSPaul Mackerras 				if (pos == 0)
109214cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
109314cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
109414cf11afSPaul Mackerras 				break;
109516c57b36SKumar Gala 			case PPC_INST_STSWI:
109616c57b36SKumar Gala 			case PPC_INST_STSWX:
109714cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
109814cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
109914cf11afSPaul Mackerras 					return -EFAULT;
110014cf11afSPaul Mackerras 				break;
110114cf11afSPaul Mackerras 		}
110214cf11afSPaul Mackerras 		/* move EA to next address */
110314cf11afSPaul Mackerras 		EA += 1;
110414cf11afSPaul Mackerras 		num_bytes--;
110514cf11afSPaul Mackerras 
110614cf11afSPaul Mackerras 		/* manage our position within the register */
110714cf11afSPaul Mackerras 		if (++pos == 4) {
110814cf11afSPaul Mackerras 			pos = 0;
110914cf11afSPaul Mackerras 			if (++rT == 32)
111014cf11afSPaul Mackerras 				rT = 0;
111114cf11afSPaul Mackerras 		}
111214cf11afSPaul Mackerras 	}
111314cf11afSPaul Mackerras 
111414cf11afSPaul Mackerras 	return 0;
111514cf11afSPaul Mackerras }
111614cf11afSPaul Mackerras 
1117c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
1118c3412dcbSWill Schmidt {
1119c3412dcbSWill Schmidt 	u32 ra,rs;
1120c3412dcbSWill Schmidt 	unsigned long tmp;
1121c3412dcbSWill Schmidt 
1122c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
1123c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
1124c3412dcbSWill Schmidt 
1125c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
1126c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
1127c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
1128c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1129c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
1130c3412dcbSWill Schmidt 
1131c3412dcbSWill Schmidt 	return 0;
1132c3412dcbSWill Schmidt }
1133c3412dcbSWill Schmidt 
1134c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
1135c1469f13SKumar Gala {
1136c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
1137c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
1138c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
1139c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
1140c1469f13SKumar Gala 	u8 bit;
1141c1469f13SKumar Gala 	unsigned long tmp;
1142c1469f13SKumar Gala 
1143c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
1144c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
1145c1469f13SKumar Gala 
1146c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1147c1469f13SKumar Gala 
1148c1469f13SKumar Gala 	return 0;
1149c1469f13SKumar Gala }
1150c1469f13SKumar Gala 
11516ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
11526ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
11536ce6c629SMichael Neuling {
11546ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
11556ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
11566ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
11576ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
11586ce6c629SMichael Neuling 	 */
11596ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
11606ce6c629SMichael Neuling 		tm_enable();
11616ce6c629SMichael Neuling 		tm_abort(cause);
11626ce6c629SMichael Neuling 		return true;
11636ce6c629SMichael Neuling 	}
11646ce6c629SMichael Neuling 	return false;
11656ce6c629SMichael Neuling }
11666ce6c629SMichael Neuling #else
11676ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
11686ce6c629SMichael Neuling {
11696ce6c629SMichael Neuling 	return false;
11706ce6c629SMichael Neuling }
11716ce6c629SMichael Neuling #endif
11726ce6c629SMichael Neuling 
117314cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
117414cf11afSPaul Mackerras {
117514cf11afSPaul Mackerras 	u32 instword;
117614cf11afSPaul Mackerras 	u32 rd;
117714cf11afSPaul Mackerras 
11784288e343SAnton Blanchard 	if (!user_mode(regs))
117914cf11afSPaul Mackerras 		return -EINVAL;
118014cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
118114cf11afSPaul Mackerras 
118214cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
118314cf11afSPaul Mackerras 		return -EFAULT;
118414cf11afSPaul Mackerras 
118514cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
118616c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1187eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
118814cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
118914cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
119014cf11afSPaul Mackerras 		return 0;
119114cf11afSPaul Mackerras 	}
119214cf11afSPaul Mackerras 
119314cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
119480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1195eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
119614cf11afSPaul Mackerras 		return 0;
119780947e7cSGeert Uytterhoeven 	}
119814cf11afSPaul Mackerras 
119914cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
120016c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
120186417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
120214cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
120314cf11afSPaul Mackerras 
1204eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
120514cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
120614cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
120714cf11afSPaul Mackerras 		return 0;
120814cf11afSPaul Mackerras 	}
120914cf11afSPaul Mackerras 
121014cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
121180947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
12126ce6c629SMichael Neuling 		if (tm_abort_check(regs,
12136ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
12146ce6c629SMichael Neuling 			return -EINVAL;
1215eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
121614cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
121780947e7cSGeert Uytterhoeven 	}
121814cf11afSPaul Mackerras 
1219c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
122016c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1221eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1222c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1223c3412dcbSWill Schmidt 	}
1224c3412dcbSWill Schmidt 
1225c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
122616c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1227eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1228c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1229c1469f13SKumar Gala 	}
1230c1469f13SKumar Gala 
12319863c28aSJames Yang 	/* Emulate sync instruction variants */
12329863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
12339863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
12349863c28aSJames Yang 		asm volatile("sync");
12359863c28aSJames Yang 		return 0;
12369863c28aSJames Yang 	}
12379863c28aSJames Yang 
1238efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1239efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
124073d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
124173d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
124273d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
124373d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1244efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1245efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1246efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1247efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1248efcac658SAlexey Kardashevskiy 		return 0;
1249efcac658SAlexey Kardashevskiy 	}
1250efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
125173d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
125273d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
125373d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
125473d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1255efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1256efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1257efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
125800ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1259efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
126000ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1261efcac658SAlexey Kardashevskiy 		return 0;
1262efcac658SAlexey Kardashevskiy 	}
1263efcac658SAlexey Kardashevskiy #endif
1264efcac658SAlexey Kardashevskiy 
126514cf11afSPaul Mackerras 	return -EINVAL;
126614cf11afSPaul Mackerras }
126714cf11afSPaul Mackerras 
126873c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
126914cf11afSPaul Mackerras {
127073c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
127114cf11afSPaul Mackerras }
127214cf11afSPaul Mackerras 
12733a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
12743a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
12753a3b5aa6SKevin Hao {
12763a3b5aa6SKevin Hao 	int ret;
12773a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
12783a3b5aa6SKevin Hao 
12793a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
12803a3b5aa6SKevin Hao 	if (ret >= 0)
12813a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
12823a3b5aa6SKevin Hao 
12833a3b5aa6SKevin Hao 	switch (ret) {
12843a3b5aa6SKevin Hao 	case 0:
12853a3b5aa6SKevin Hao 		emulate_single_step(regs);
12863a3b5aa6SKevin Hao 		return 0;
12873a3b5aa6SKevin Hao 	case 1: {
12883a3b5aa6SKevin Hao 			int code = 0;
1289de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
12903a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
12913a3b5aa6SKevin Hao 			return 0;
12923a3b5aa6SKevin Hao 		}
12933a3b5aa6SKevin Hao 	case -EFAULT:
12943a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12953a3b5aa6SKevin Hao 		return 0;
12963a3b5aa6SKevin Hao 	}
12973a3b5aa6SKevin Hao 
12983a3b5aa6SKevin Hao 	return -1;
12993a3b5aa6SKevin Hao }
13003a3b5aa6SKevin Hao #else
13013a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
13023a3b5aa6SKevin Hao #endif
13033a3b5aa6SKevin Hao 
130403465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
130514cf11afSPaul Mackerras {
1306ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
130714cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
130814cf11afSPaul Mackerras 
1309aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
131004903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
131114cf11afSPaul Mackerras 
131214cf11afSPaul Mackerras 	if (reason & REASON_FP) {
131314cf11afSPaul Mackerras 		/* IEEE FP exception */
1314dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1315ba12eedeSLi Zhong 		goto bail;
13168dad3f92SPaul Mackerras 	}
13178dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1318a4c3f909SBalbir Singh 		unsigned long bugaddr;
1319ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1320ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1321ba797b28SJason Wessel 		if (debugger_bpt(regs))
1322ba12eedeSLi Zhong 			goto bail;
1323ba797b28SJason Wessel 
13246cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
13256cc89badSNaveen N. Rao 			goto bail;
13266cc89badSNaveen N. Rao 
132714cf11afSPaul Mackerras 		/* trap exception */
1328dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1329dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1330ba12eedeSLi Zhong 			goto bail;
133173c9ceabSJeremy Fitzhardinge 
1332a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1333a4c3f909SBalbir Singh 		/*
1334a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1335a4c3f909SBalbir Singh 		 */
1336a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1337a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1338a4c3f909SBalbir Singh 
133973c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1340a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
134114cf11afSPaul Mackerras 			regs->nip += 4;
1342ba12eedeSLi Zhong 			goto bail;
134314cf11afSPaul Mackerras 		}
13448dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1345ba12eedeSLi Zhong 		goto bail;
13468dad3f92SPaul Mackerras 	}
1347bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1348bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1349bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1350bc2a9408SMichael Neuling 		 * This occurs when:
1351bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1352bc2a9408SMichael Neuling 		 *    transition in TM states.
1353bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1354bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1355bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1356bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1357632f0574SMichael Ellerman 		 *
1358632f0574SMichael Ellerman 		 * If usermode caused this, it's done something illegal and
1359bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1360bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1361bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1362bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1363bc2a9408SMichael Neuling 		 */
1364bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1365bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1366ba12eedeSLi Zhong 			goto bail;
1367bc2a9408SMichael Neuling 		} else {
1368bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1369bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1370bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1371bc2a9408SMichael Neuling 		}
1372bc2a9408SMichael Neuling 	}
1373bc2a9408SMichael Neuling #endif
13748dad3f92SPaul Mackerras 
1375b3f6a459SMichael Ellerman 	/*
1376b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1377b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1378b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1379b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1380b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1381b3f6a459SMichael Ellerman 	 */
1382b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1383b3f6a459SMichael Ellerman 		goto sigill;
1384b3f6a459SMichael Ellerman 
1385a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1386a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1387cd8a5673SPaul Mackerras 		local_irq_enable();
1388cd8a5673SPaul Mackerras 
138904903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
139004903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
139104903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
139204903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
139304903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
139404903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
13954e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
13964e63f8edSBenjamin Herrenschmidt 	 */
13973a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1398ba12eedeSLi Zhong 		goto bail;
139904903a30SKumar Gala 
14008dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
14018dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
140214cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
140314cf11afSPaul Mackerras 		case 0:
140414cf11afSPaul Mackerras 			regs->nip += 4;
140514cf11afSPaul Mackerras 			emulate_single_step(regs);
1406ba12eedeSLi Zhong 			goto bail;
140714cf11afSPaul Mackerras 		case -EFAULT:
140814cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1409ba12eedeSLi Zhong 			goto bail;
14108dad3f92SPaul Mackerras 		}
14118dad3f92SPaul Mackerras 	}
14128dad3f92SPaul Mackerras 
1413b3f6a459SMichael Ellerman sigill:
141414cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
141514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
141614cf11afSPaul Mackerras 	else
141714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1418ba12eedeSLi Zhong 
1419ba12eedeSLi Zhong bail:
1420ba12eedeSLi Zhong 	exception_exit(prev_state);
142114cf11afSPaul Mackerras }
142203465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
142314cf11afSPaul Mackerras 
1424bf593907SPaul Mackerras /*
1425bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1426bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1427bf593907SPaul Mackerras  */
142803465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1429bf593907SPaul Mackerras {
1430bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1431bf593907SPaul Mackerras 	program_check_exception(regs);
1432bf593907SPaul Mackerras }
143303465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1434bf593907SPaul Mackerras 
1435dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
143614cf11afSPaul Mackerras {
1437ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
14384393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
143914cf11afSPaul Mackerras 
1440a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1441a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1442a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1443a3512b2dSBenjamin Herrenschmidt 
14446ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
14456ce6c629SMichael Neuling 		goto bail;
14466ce6c629SMichael Neuling 
1447e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1448e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
144914cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
145014cf11afSPaul Mackerras 
145114cf11afSPaul Mackerras 	if (fixed == 1) {
145214cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
145314cf11afSPaul Mackerras 		emulate_single_step(regs);
1454ba12eedeSLi Zhong 		goto bail;
145514cf11afSPaul Mackerras 	}
145614cf11afSPaul Mackerras 
145714cf11afSPaul Mackerras 	/* Operand address was bad */
145814cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
14594393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
14604393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
14614393c4f6SBenjamin Herrenschmidt 	} else {
14624393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
14634393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
146414cf11afSPaul Mackerras 	}
14654393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
14664393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
14674393c4f6SBenjamin Herrenschmidt 	else
14684393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1469ba12eedeSLi Zhong 
1470ba12eedeSLi Zhong bail:
1471ba12eedeSLi Zhong 	exception_exit(prev_state);
147214cf11afSPaul Mackerras }
147314cf11afSPaul Mackerras 
1474f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1475f0f558b1SPaul Mackerras {
1476f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1477f0f558b1SPaul Mackerras 
1478f0f558b1SPaul Mackerras 	if (user_mode(regs))
1479f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1480f0f558b1SPaul Mackerras 	else
1481f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1482f0f558b1SPaul Mackerras 
1483f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1484f0f558b1SPaul Mackerras }
1485f0f558b1SPaul Mackerras 
148614cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
148714cf11afSPaul Mackerras {
148814cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
148914cf11afSPaul Mackerras 	       current, regs->gpr[1]);
149014cf11afSPaul Mackerras 	debugger(regs);
149114cf11afSPaul Mackerras 	show_regs(regs);
149214cf11afSPaul Mackerras 	panic("kernel stack overflow");
149314cf11afSPaul Mackerras }
149414cf11afSPaul Mackerras 
149514cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
149614cf11afSPaul Mackerras {
149714cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
149814cf11afSPaul Mackerras 	       regs->nip, regs->msr);
149914cf11afSPaul Mackerras 	debugger(regs);
150014cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
150114cf11afSPaul Mackerras }
150214cf11afSPaul Mackerras 
1503dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1504dc1c1ca3SStephen Rothwell {
1505ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1506ba12eedeSLi Zhong 
1507dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1508dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1509dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1510ba12eedeSLi Zhong 
1511ba12eedeSLi Zhong 	exception_exit(prev_state);
1512dc1c1ca3SStephen Rothwell }
1513dc1c1ca3SStephen Rothwell 
1514dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1515dc1c1ca3SStephen Rothwell {
1516ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1517ba12eedeSLi Zhong 
1518dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1519dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1520dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1521dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1522ba12eedeSLi Zhong 		goto bail;
1523dc1c1ca3SStephen Rothwell 	}
15246c4841c2SAnton Blanchard 
1525dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1526dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1527dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1528ba12eedeSLi Zhong 
1529ba12eedeSLi Zhong bail:
1530ba12eedeSLi Zhong 	exception_exit(prev_state);
1531dc1c1ca3SStephen Rothwell }
1532dc1c1ca3SStephen Rothwell 
1533ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1534ce48b210SMichael Neuling {
1535ce48b210SMichael Neuling 	if (user_mode(regs)) {
1536ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1537ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1538ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1539ce48b210SMichael Neuling 		return;
1540ce48b210SMichael Neuling 	}
1541ce48b210SMichael Neuling 
1542ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1543ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1544ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1545ce48b210SMichael Neuling }
1546ce48b210SMichael Neuling 
15472517617eSMichael Neuling #ifdef CONFIG_PPC64
1548172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1549172f7aaaSCyril Bur {
15505d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
15515d176f75SCyril Bur 	if (user_mode(regs)) {
15525d176f75SCyril Bur 		current->thread.load_tm++;
15535d176f75SCyril Bur 		regs->msr |= MSR_TM;
15545d176f75SCyril Bur 		tm_enable();
15555d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
15565d176f75SCyril Bur 		return;
15575d176f75SCyril Bur 	}
15585d176f75SCyril Bur #endif
1559172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1560172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1561172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1562172f7aaaSCyril Bur }
1563172f7aaaSCyril Bur 
1564021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1565d0c0c9a1SMichael Neuling {
1566021424a1SMichael Ellerman 	static char *facility_strings[] = {
15672517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
15682517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
15692517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
15702517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
15712517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
15722517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
15732517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
15742517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1575794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
15769b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1577021424a1SMichael Ellerman 	};
15782517617eSMichael Neuling 	char *facility = "unknown";
1579021424a1SMichael Ellerman 	u64 value;
1580c952c1c4SAnshuman Khandual 	u32 instword, rd;
15812517617eSMichael Neuling 	u8 status;
15822517617eSMichael Neuling 	bool hv;
1583021424a1SMichael Ellerman 
15842271db20SBenjamin Herrenschmidt 	hv = (TRAP(regs) == 0xf80);
15852517617eSMichael Neuling 	if (hv)
1586b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
15872517617eSMichael Neuling 	else
15882517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
15892517617eSMichael Neuling 
15902517617eSMichael Neuling 	status = value >> 56;
15912517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1592c952c1c4SAnshuman Khandual 		/*
1593c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1594c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1595c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1596c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1597c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1598c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1599c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1600c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1601c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1602c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1603c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1604c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1605c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1606c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
16072517617eSMichael Neuling 		 */
1608c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1609c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1610c952c1c4SAnshuman Khandual 			return;
1611c952c1c4SAnshuman Khandual 		}
1612c952c1c4SAnshuman Khandual 
1613c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1614c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1615c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1616c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1617c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
16182517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1619b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1620b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1621c952c1c4SAnshuman Khandual 		}
1622c952c1c4SAnshuman Khandual 
1623c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1624c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1625c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1626c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1627c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1628c952c1c4SAnshuman Khandual 				return;
1629c952c1c4SAnshuman Khandual 			}
1630c952c1c4SAnshuman Khandual 			regs->nip += 4;
1631c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1632c952c1c4SAnshuman Khandual 		}
16332517617eSMichael Neuling 		return;
1634b14b6260SMichael Ellerman 	}
1635b14b6260SMichael Ellerman 
1636172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1637172f7aaaSCyril Bur 		/*
1638172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1639172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1640172f7aaaSCyril Bur 		 *
1641172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1642172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1643172f7aaaSCyril Bur 		 * support.
1644172f7aaaSCyril Bur 		 *
1645172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1646172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1647172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1648172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1649172f7aaaSCyril Bur 		 */
1650172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1651172f7aaaSCyril Bur 			goto out;
1652172f7aaaSCyril Bur 
1653172f7aaaSCyril Bur 		tm_unavailable(regs);
1654172f7aaaSCyril Bur 		return;
1655172f7aaaSCyril Bur 	}
1656172f7aaaSCyril Bur 
165793c2ec0fSBalbir Singh 	if ((hv || status >= 2) &&
165893c2ec0fSBalbir Singh 	    (status < ARRAY_SIZE(facility_strings)) &&
16592517617eSMichael Neuling 	    facility_strings[status])
16602517617eSMichael Neuling 		facility = facility_strings[status];
1661021424a1SMichael Ellerman 
1662d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1663d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1664d0c0c9a1SMichael Neuling 		local_irq_enable();
1665d0c0c9a1SMichael Neuling 
166693c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
166793c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1668d0c0c9a1SMichael Neuling 
1669172f7aaaSCyril Bur out:
1670d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1671d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1672d0c0c9a1SMichael Neuling 		return;
1673d0c0c9a1SMichael Neuling 	}
1674d0c0c9a1SMichael Neuling 
1675021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1676d0c0c9a1SMichael Neuling }
16772517617eSMichael Neuling #endif
1678d0c0c9a1SMichael Neuling 
1679f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1680f54db641SMichael Neuling 
1681f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1682f54db641SMichael Neuling {
1683f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1684f54db641SMichael Neuling 
1685f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1686f54db641SMichael Neuling 		 regs->nip, regs->msr);
1687f54db641SMichael Neuling 
1688f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1689f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1690f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1691f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1692f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1693f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1694f54db641SMichael Neuling 	 */
1695d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1696f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1697f54db641SMichael Neuling 
1698f54db641SMichael Neuling 	/* Enable FP for the task: */
1699a7771176SCyril Bur 	current->thread.load_fp = 1;
1700f54db641SMichael Neuling 
1701f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1702f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1703f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
17043ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
17053ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1706f54db641SMichael Neuling 	 */
1707eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1708f54db641SMichael Neuling }
1709f54db641SMichael Neuling 
1710f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1711f54db641SMichael Neuling {
1712f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1713f54db641SMichael Neuling 	 * the same way.
1714f54db641SMichael Neuling 	 */
1715f54db641SMichael Neuling 
1716f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1717f54db641SMichael Neuling 		 "MSR=%lx\n",
1718f54db641SMichael Neuling 		 regs->nip, regs->msr);
1719d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1720a7771176SCyril Bur 	current->thread.load_vec = 1;
1721eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1722f54db641SMichael Neuling 	current->thread.used_vr = 1;
17233ac8ff1cSPaul Mackerras }
17243ac8ff1cSPaul Mackerras 
1725f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1726f54db641SMichael Neuling {
1727f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1728f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1729f54db641SMichael Neuling 	 *
1730f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1731f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1732f54db641SMichael Neuling 	 */
1733f54db641SMichael Neuling 
1734f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1735f54db641SMichael Neuling 		 "MSR=%lx\n",
1736f54db641SMichael Neuling 		 regs->nip, regs->msr);
1737f54db641SMichael Neuling 
17383ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
17393ac8ff1cSPaul Mackerras 
1740f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1741d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1742f54db641SMichael Neuling 
1743a7771176SCyril Bur 	current->thread.load_vec = 1;
1744a7771176SCyril Bur 	current->thread.load_fp = 1;
17453ac8ff1cSPaul Mackerras 
1746eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1747f54db641SMichael Neuling }
1748f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1749f54db641SMichael Neuling 
1750dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1751dc1c1ca3SStephen Rothwell {
175269111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
175389713ed1SAnton Blanchard 
1754dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1755dc1c1ca3SStephen Rothwell }
1756dc1c1ca3SStephen Rothwell 
1757172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
17583bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
17593bffb652SDave Kleikamp {
17603bffb652SDave Kleikamp 	int changed = 0;
17613bffb652SDave Kleikamp 	/*
17623bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
17633bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
17643bffb652SDave Kleikamp 	 */
17653bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
17663bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
17673bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
176851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
17693bffb652SDave Kleikamp #endif
17703bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
17713bffb652SDave Kleikamp 			     5);
17723bffb652SDave Kleikamp 		changed |= 0x01;
17733bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
17743bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
17753bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
17763bffb652SDave Kleikamp 			     6);
17773bffb652SDave Kleikamp 		changed |= 0x01;
17783bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
177951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
17803bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
17813bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
17823bffb652SDave Kleikamp 			     1);
17833bffb652SDave Kleikamp 		changed |= 0x01;
17843bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
178551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
17863bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
17873bffb652SDave Kleikamp 			     2);
17883bffb652SDave Kleikamp 		changed |= 0x01;
17893bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
179051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
17913bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
17923bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
17933bffb652SDave Kleikamp 			     3);
17943bffb652SDave Kleikamp 		changed |= 0x01;
17953bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
179651ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
17973bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
17983bffb652SDave Kleikamp 			     4);
17993bffb652SDave Kleikamp 		changed |= 0x01;
18003bffb652SDave Kleikamp 	}
18013bffb652SDave Kleikamp 	/*
18023bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
18033bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
18043bffb652SDave Kleikamp 	 * back on or not.
18053bffb652SDave Kleikamp 	 */
180651ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
180751ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
18083bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
18093bffb652SDave Kleikamp 	else
18103bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
181151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
18123bffb652SDave Kleikamp 
18133bffb652SDave Kleikamp 	if (changed & 0x01)
181451ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
18153bffb652SDave Kleikamp }
181614cf11afSPaul Mackerras 
181703465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
181814cf11afSPaul Mackerras {
181951ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
18203bffb652SDave Kleikamp 
1821ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1822ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1823ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1824ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1825ec097c84SRoland McGrath 	 */
1826ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1827ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1828ec097c84SRoland McGrath 
1829ec097c84SRoland McGrath 		/* Disable BT */
1830ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1831ec097c84SRoland McGrath 		/* Clear the BT event */
1832ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1833ec097c84SRoland McGrath 
1834ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1835ec097c84SRoland McGrath 		if (user_mode(regs)) {
183651ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
183751ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1838ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1839ec097c84SRoland McGrath 			return;
1840ec097c84SRoland McGrath 		}
1841ec097c84SRoland McGrath 
18426cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
18436cc89badSNaveen N. Rao 			return;
18446cc89badSNaveen N. Rao 
1845ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1846ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1847ec097c84SRoland McGrath 			return;
1848ec097c84SRoland McGrath 		}
1849ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1850ec097c84SRoland McGrath 			return;
1851ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
185214cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1853f8279621SKumar Gala 
185414cf11afSPaul Mackerras 		/* Disable instruction completion */
185514cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
185614cf11afSPaul Mackerras 		/* Clear the instruction completion event */
185714cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1858f8279621SKumar Gala 
18596cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
18606cc89badSNaveen N. Rao 			return;
18616cc89badSNaveen N. Rao 
1862f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1863f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
186414cf11afSPaul Mackerras 			return;
186514cf11afSPaul Mackerras 		}
1866f8279621SKumar Gala 
1867f8279621SKumar Gala 		if (debugger_sstep(regs))
1868f8279621SKumar Gala 			return;
1869f8279621SKumar Gala 
18703bffb652SDave Kleikamp 		if (user_mode(regs)) {
187151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
187251ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
187351ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
18743bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
18753bffb652SDave Kleikamp 			else
18763bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
187751ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
18783bffb652SDave Kleikamp 		}
1879f8279621SKumar Gala 
1880f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
18813bffb652SDave Kleikamp 	} else
18823bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
188314cf11afSPaul Mackerras }
188403465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1885172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
188614cf11afSPaul Mackerras 
188714cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
188814cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
188914cf11afSPaul Mackerras {
189014cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
189114cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
189214cf11afSPaul Mackerras }
189314cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
189414cf11afSPaul Mackerras 
189514cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1896dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
189714cf11afSPaul Mackerras {
189814cf11afSPaul Mackerras 	int err;
189914cf11afSPaul Mackerras 
190014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
190114cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
190214cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
19038dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
190414cf11afSPaul Mackerras 	}
190514cf11afSPaul Mackerras 
1906dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1907dc1c1ca3SStephen Rothwell 
1908eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
190914cf11afSPaul Mackerras 	err = emulate_altivec(regs);
191014cf11afSPaul Mackerras 	if (err == 0) {
191114cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
191214cf11afSPaul Mackerras 		emulate_single_step(regs);
191314cf11afSPaul Mackerras 		return;
191414cf11afSPaul Mackerras 	}
191514cf11afSPaul Mackerras 
191614cf11afSPaul Mackerras 	if (err == -EFAULT) {
191714cf11afSPaul Mackerras 		/* got an error reading the instruction */
191814cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
191914cf11afSPaul Mackerras 	} else {
192014cf11afSPaul Mackerras 		/* didn't recognize the instruction */
192114cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
192276462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
192314cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1924de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
192514cf11afSPaul Mackerras 	}
192614cf11afSPaul Mackerras }
192714cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
192814cf11afSPaul Mackerras 
192914cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
193014cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
193114cf11afSPaul Mackerras 			   unsigned long error_code)
193214cf11afSPaul Mackerras {
193314cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
193414cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
193514cf11afSPaul Mackerras 	 * something smarter
193614cf11afSPaul Mackerras 	 */
193714cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
193814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
193914cf11afSPaul Mackerras 	return;
194014cf11afSPaul Mackerras }
194114cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
194214cf11afSPaul Mackerras 
194314cf11afSPaul Mackerras #ifdef CONFIG_SPE
194414cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
194514cf11afSPaul Mackerras {
19466a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
194714cf11afSPaul Mackerras 	unsigned long spefscr;
194814cf11afSPaul Mackerras 	int fpexc_mode;
194914cf11afSPaul Mackerras 	int code = 0;
19506a800f36SLiu Yu 	int err;
19516a800f36SLiu Yu 
1952685659eeSyu liu 	flush_spe_to_thread(current);
195314cf11afSPaul Mackerras 
195414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
195514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
195614cf11afSPaul Mackerras 
195714cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
195814cf11afSPaul Mackerras 		code = FPE_FLTOVF;
195914cf11afSPaul Mackerras 	}
196014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
196114cf11afSPaul Mackerras 		code = FPE_FLTUND;
196214cf11afSPaul Mackerras 	}
196314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
196414cf11afSPaul Mackerras 		code = FPE_FLTDIV;
196514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
196614cf11afSPaul Mackerras 		code = FPE_FLTINV;
196714cf11afSPaul Mackerras 	}
196814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
196914cf11afSPaul Mackerras 		code = FPE_FLTRES;
197014cf11afSPaul Mackerras 
19716a800f36SLiu Yu 	err = do_spe_mathemu(regs);
19726a800f36SLiu Yu 	if (err == 0) {
19736a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
19746a800f36SLiu Yu 		emulate_single_step(regs);
197514cf11afSPaul Mackerras 		return;
197614cf11afSPaul Mackerras 	}
19776a800f36SLiu Yu 
19786a800f36SLiu Yu 	if (err == -EFAULT) {
19796a800f36SLiu Yu 		/* got an error reading the instruction */
19806a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
19816a800f36SLiu Yu 	} else if (err == -EINVAL) {
19826a800f36SLiu Yu 		/* didn't recognize the instruction */
19836a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
19846a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
19856a800f36SLiu Yu 	} else {
19866a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
19876a800f36SLiu Yu 	}
19886a800f36SLiu Yu 
19896a800f36SLiu Yu 	return;
19906a800f36SLiu Yu }
19916a800f36SLiu Yu 
19926a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
19936a800f36SLiu Yu {
19946a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
19956a800f36SLiu Yu 	int err;
19966a800f36SLiu Yu 
19976a800f36SLiu Yu 	preempt_disable();
19986a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
19996a800f36SLiu Yu 		giveup_spe(current);
20006a800f36SLiu Yu 	preempt_enable();
20016a800f36SLiu Yu 
20026a800f36SLiu Yu 	regs->nip -= 4;
20036a800f36SLiu Yu 	err = speround_handler(regs);
20046a800f36SLiu Yu 	if (err == 0) {
20056a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
20066a800f36SLiu Yu 		emulate_single_step(regs);
20076a800f36SLiu Yu 		return;
20086a800f36SLiu Yu 	}
20096a800f36SLiu Yu 
20106a800f36SLiu Yu 	if (err == -EFAULT) {
20116a800f36SLiu Yu 		/* got an error reading the instruction */
20126a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
20136a800f36SLiu Yu 	} else if (err == -EINVAL) {
20146a800f36SLiu Yu 		/* didn't recognize the instruction */
20156a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
20166a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
20176a800f36SLiu Yu 	} else {
20186a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
20196a800f36SLiu Yu 		return;
20206a800f36SLiu Yu 	}
20216a800f36SLiu Yu }
202214cf11afSPaul Mackerras #endif
202314cf11afSPaul Mackerras 
2024dc1c1ca3SStephen Rothwell /*
2025dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
2026dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
2027dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
2028dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
2029dc1c1ca3SStephen Rothwell  */
2030dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
2031dc1c1ca3SStephen Rothwell {
2032dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
2033dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
2034dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
2035dc1c1ca3SStephen Rothwell }
203615770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
2037dc1c1ca3SStephen Rothwell 
20381e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
203914cf11afSPaul Mackerras /*
204014cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
204114cf11afSPaul Mackerras  * spins until a reboot occurs
204214cf11afSPaul Mackerras  */
204314cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
204414cf11afSPaul Mackerras {
204514cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
204614cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
204714cf11afSPaul Mackerras 	return;
204814cf11afSPaul Mackerras }
204914cf11afSPaul Mackerras 
205014cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
205114cf11afSPaul Mackerras {
205214cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
205314cf11afSPaul Mackerras 	WatchdogHandler(regs);
205414cf11afSPaul Mackerras }
205514cf11afSPaul Mackerras #endif
2056dc1c1ca3SStephen Rothwell 
2057dc1c1ca3SStephen Rothwell /*
2058dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
2059dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
2060dc1c1ca3SStephen Rothwell  */
2061dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
2062dc1c1ca3SStephen Rothwell {
2063dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
2064dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
2065dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
2066dc1c1ca3SStephen Rothwell }
206715770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
206814cf11afSPaul Mackerras 
206914cf11afSPaul Mackerras void __init trap_init(void)
207014cf11afSPaul Mackerras {
207114cf11afSPaul Mackerras }
207280947e7cSGeert Uytterhoeven 
207380947e7cSGeert Uytterhoeven 
207480947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
207580947e7cSGeert Uytterhoeven 
207680947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
207780947e7cSGeert Uytterhoeven 
207880947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
207980947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
208080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
208180947e7cSGeert Uytterhoeven #endif
208280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
208380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
208480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
208580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
208680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
208780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
208880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
208980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
209080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
209180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
2092a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
209380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
209480947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
209580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
209680947e7cSGeert Uytterhoeven #endif
209780947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
209880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
209980947e7cSGeert Uytterhoeven #endif
2100efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
2101efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
2102efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
2103f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
21045080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvw4x),
21055080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvh8x),
21065080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvd2x),
21075080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvb16x),
2108efcac658SAlexey Kardashevskiy #endif
210980947e7cSGeert Uytterhoeven };
211080947e7cSGeert Uytterhoeven 
211180947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
211280947e7cSGeert Uytterhoeven 
211380947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
211480947e7cSGeert Uytterhoeven {
211576462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
211680947e7cSGeert Uytterhoeven 			    type);
211780947e7cSGeert Uytterhoeven }
211880947e7cSGeert Uytterhoeven 
211980947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
212080947e7cSGeert Uytterhoeven {
212180947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
212280947e7cSGeert Uytterhoeven 	unsigned int i;
212380947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
212480947e7cSGeert Uytterhoeven 
212580947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
212680947e7cSGeert Uytterhoeven 		return -ENODEV;
212780947e7cSGeert Uytterhoeven 
212880947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
212980947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
213080947e7cSGeert Uytterhoeven 	if (!dir)
213180947e7cSGeert Uytterhoeven 		return -ENOMEM;
213280947e7cSGeert Uytterhoeven 
2133*57ad583fSRussell Currey 	d = debugfs_create_u32("do_warn", 0644, dir,
213480947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
213580947e7cSGeert Uytterhoeven 	if (!d)
213680947e7cSGeert Uytterhoeven 		goto fail;
213780947e7cSGeert Uytterhoeven 
213880947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
2139*57ad583fSRussell Currey 		d = debugfs_create_u32(entries[i].name, 0644, dir,
214080947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
214180947e7cSGeert Uytterhoeven 		if (!d)
214280947e7cSGeert Uytterhoeven 			goto fail;
214380947e7cSGeert Uytterhoeven 	}
214480947e7cSGeert Uytterhoeven 
214580947e7cSGeert Uytterhoeven 	return 0;
214680947e7cSGeert Uytterhoeven 
214780947e7cSGeert Uytterhoeven fail:
214880947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
214980947e7cSGeert Uytterhoeven 	return -ENOMEM;
215080947e7cSGeert Uytterhoeven }
215180947e7cSGeert Uytterhoeven 
215280947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
215380947e7cSGeert Uytterhoeven 
215480947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2155