xref: /linux/arch/powerpc/kernel/traps.c (revision f08fb25bc66986b0952724530a640d9970fa52c1)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
214cf11afSPaul Mackerras /*
314cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
4fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
514cf11afSPaul Mackerras  *
614cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
714cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
814cf11afSPaul Mackerras  */
914cf11afSPaul Mackerras 
1014cf11afSPaul Mackerras /*
1114cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras #include <linux/errno.h>
1514cf11afSPaul Mackerras #include <linux/sched.h>
16b17b0153SIngo Molnar #include <linux/sched/debug.h>
1714cf11afSPaul Mackerras #include <linux/kernel.h>
1814cf11afSPaul Mackerras #include <linux/mm.h>
1999cd1302SRam Pai #include <linux/pkeys.h>
2014cf11afSPaul Mackerras #include <linux/stddef.h>
2114cf11afSPaul Mackerras #include <linux/unistd.h>
228dad3f92SPaul Mackerras #include <linux/ptrace.h>
2314cf11afSPaul Mackerras #include <linux/user.h>
2414cf11afSPaul Mackerras #include <linux/interrupt.h>
2514cf11afSPaul Mackerras #include <linux/init.h>
268a39b05fSPaul Gortmaker #include <linux/extable.h>
278a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
288dad3f92SPaul Mackerras #include <linux/prctl.h>
2914cf11afSPaul Mackerras #include <linux/delay.h>
3014cf11afSPaul Mackerras #include <linux/kprobes.h>
31cc532915SMichael Ellerman #include <linux/kexec.h>
325474c120SMichael Hanselmann #include <linux/backlight.h>
3373c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
341eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3576462232SChristian Dietrich #include <linux/ratelimit.h>
36ba12eedeSLi Zhong #include <linux/context_tracking.h>
375080332cSMichael Neuling #include <linux/smp.h>
3835adacd6SNicholas Piggin #include <linux/console.h>
3935adacd6SNicholas Piggin #include <linux/kmsg_dump.h>
40dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h>
4114cf11afSPaul Mackerras 
4280947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
443a96570fSNicholas Piggin #include <asm/interrupt.h>
4514cf11afSPaul Mackerras #include <asm/io.h>
4686417780SPaul Mackerras #include <asm/machdep.h>
4786417780SPaul Mackerras #include <asm/rtas.h>
48f7f6f4feSDavid Gibson #include <asm/pmc.h>
4914cf11afSPaul Mackerras #include <asm/reg.h>
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
56dc1c1ca3SStephen Rothwell #endif
57c0ce7d08SDavid Wilder #include <asm/kexec.h>
5816c57b36SKumar Gala #include <asm/ppc-opcode.h>
59cce1f106SShaohui Xie #include <asm/rio.h>
60ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
61ae3a197eSDavid Howells #include <asm/switch_to.h>
62f54db641SMichael Neuling #include <asm/tm.h>
63ae3a197eSDavid Howells #include <asm/debug.h>
6442f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
65fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
664e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
676cc89badSNaveen N. Rao #include <asm/kprobes.h>
68a99b9c5eSMurilo Opsfelder Araujo #include <asm/stacktrace.h>
69de3c83c2SMathieu Malaterre #include <asm/nmi.h>
70deefd0aeSChristophe Leroy #include <asm/disassemble.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 
970f642d61SMurilo Opsfelder Araujo static const char *signame(int signr)
980f642d61SMurilo Opsfelder Araujo {
990f642d61SMurilo Opsfelder Araujo 	switch (signr) {
1000f642d61SMurilo Opsfelder Araujo 	case SIGBUS:	return "bus error";
1010f642d61SMurilo Opsfelder Araujo 	case SIGFPE:	return "floating point exception";
1020f642d61SMurilo Opsfelder Araujo 	case SIGILL:	return "illegal instruction";
1030f642d61SMurilo Opsfelder Araujo 	case SIGSEGV:	return "segfault";
1040f642d61SMurilo Opsfelder Araujo 	case SIGTRAP:	return "unhandled trap";
1050f642d61SMurilo Opsfelder Araujo 	}
1060f642d61SMurilo Opsfelder Araujo 
1070f642d61SMurilo Opsfelder Araujo 	return "unknown signal";
1080f642d61SMurilo Opsfelder Araujo }
1090f642d61SMurilo Opsfelder Araujo 
11014cf11afSPaul Mackerras /*
11114cf11afSPaul Mackerras  * Trap & Exception support
11214cf11afSPaul Mackerras  */
11314cf11afSPaul Mackerras 
1146031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1156031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1166031d9d9Santon@samba.org {
1176031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1186031d9d9Santon@samba.org 	if (pmac_backlight) {
1196031d9d9Santon@samba.org 		struct backlight_properties *props;
1206031d9d9Santon@samba.org 
1216031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1226031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1236031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1246031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1256031d9d9Santon@samba.org 	}
1266031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1276031d9d9Santon@samba.org }
1286031d9d9Santon@samba.org #else
1296031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1306031d9d9Santon@samba.org #endif
1316031d9d9Santon@samba.org 
1326fcd6baaSNicholas Piggin /*
1336fcd6baaSNicholas Piggin  * If oops/die is expected to crash the machine, return true here.
1346fcd6baaSNicholas Piggin  *
1356fcd6baaSNicholas Piggin  * This should not be expected to be 100% accurate, there may be
1366fcd6baaSNicholas Piggin  * notifiers registered or other unexpected conditions that may bring
1376fcd6baaSNicholas Piggin  * down the kernel. Or if the current process in the kernel is holding
1386fcd6baaSNicholas Piggin  * locks or has other critical state, the kernel may become effectively
1396fcd6baaSNicholas Piggin  * unusable anyway.
1406fcd6baaSNicholas Piggin  */
1416fcd6baaSNicholas Piggin bool die_will_crash(void)
1426fcd6baaSNicholas Piggin {
1436fcd6baaSNicholas Piggin 	if (should_fadump_crash())
1446fcd6baaSNicholas Piggin 		return true;
1456fcd6baaSNicholas Piggin 	if (kexec_should_crash(current))
1466fcd6baaSNicholas Piggin 		return true;
1476fcd6baaSNicholas Piggin 	if (in_interrupt() || panic_on_oops ||
1486fcd6baaSNicholas Piggin 			!current->pid || is_global_init(current))
1496fcd6baaSNicholas Piggin 		return true;
1506fcd6baaSNicholas Piggin 
1516fcd6baaSNicholas Piggin 	return false;
1526fcd6baaSNicholas Piggin }
1536fcd6baaSNicholas Piggin 
154760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
155760ca4dcSAnton Blanchard static int die_owner = -1;
156760ca4dcSAnton Blanchard static unsigned int die_nest_count;
157c0ce7d08SDavid Wilder static int die_counter;
158760ca4dcSAnton Blanchard 
15935adacd6SNicholas Piggin extern void panic_flush_kmsg_start(void)
16035adacd6SNicholas Piggin {
16135adacd6SNicholas Piggin 	/*
16235adacd6SNicholas Piggin 	 * These are mostly taken from kernel/panic.c, but tries to do
16335adacd6SNicholas Piggin 	 * relatively minimal work. Don't use delay functions (TB may
16435adacd6SNicholas Piggin 	 * be broken), don't crash dump (need to set a firmware log),
16535adacd6SNicholas Piggin 	 * don't run notifiers. We do want to get some information to
16635adacd6SNicholas Piggin 	 * Linux console.
16735adacd6SNicholas Piggin 	 */
16835adacd6SNicholas Piggin 	console_verbose();
16935adacd6SNicholas Piggin 	bust_spinlocks(1);
17035adacd6SNicholas Piggin }
17135adacd6SNicholas Piggin 
17235adacd6SNicholas Piggin extern void panic_flush_kmsg_end(void)
17335adacd6SNicholas Piggin {
17435adacd6SNicholas Piggin 	kmsg_dump(KMSG_DUMP_PANIC);
17535adacd6SNicholas Piggin 	bust_spinlocks(0);
17635adacd6SNicholas Piggin 	debug_locks_off();
177de6da1e8SFeng Tang 	console_flush_on_panic(CONSOLE_FLUSH_PENDING);
17835adacd6SNicholas Piggin }
17935adacd6SNicholas Piggin 
18003465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
181760ca4dcSAnton Blanchard {
182760ca4dcSAnton Blanchard 	int cpu;
18334c2a14fSanton@samba.org 	unsigned long flags;
18414cf11afSPaul Mackerras 
185293e4688Santon@samba.org 	oops_enter();
186293e4688Santon@samba.org 
187760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
188760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
189760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
190760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
191760ca4dcSAnton Blanchard 		if (cpu == die_owner)
192760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
193760ca4dcSAnton Blanchard 		else
194760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
195760ca4dcSAnton Blanchard 	}
196760ca4dcSAnton Blanchard 	die_nest_count++;
197760ca4dcSAnton Blanchard 	die_owner = cpu;
19814cf11afSPaul Mackerras 	console_verbose();
19914cf11afSPaul Mackerras 	bust_spinlocks(1);
2006031d9d9Santon@samba.org 	if (machine_is(powermac))
2016031d9d9Santon@samba.org 		pmac_backlight_unblank();
202760ca4dcSAnton Blanchard 	return flags;
20334c2a14fSanton@samba.org }
20403465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
2055474c120SMichael Hanselmann 
20603465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
207760ca4dcSAnton Blanchard 			       int signr)
208760ca4dcSAnton Blanchard {
20914cf11afSPaul Mackerras 	bust_spinlocks(0);
210373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
211760ca4dcSAnton Blanchard 	die_nest_count--;
21258154c8cSAnton Blanchard 	oops_exit();
21358154c8cSAnton Blanchard 	printk("\n");
2147458e8b2SNicholas Piggin 	if (!die_nest_count) {
215760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
2167458e8b2SNicholas Piggin 		die_owner = -1;
217760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
2187458e8b2SNicholas Piggin 	}
219760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
220cc532915SMichael Ellerman 
221d40b6768SNicholas Piggin 	/*
222d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
223d40b6768SNicholas Piggin 	 */
2247153d4bfSXiongwei Song 	if (TRAP(regs) == INTERRUPT_SYSTEM_RESET)
225d40b6768SNicholas Piggin 		return;
226d40b6768SNicholas Piggin 
227ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
228ebaeb5aeSMahesh Salgaonkar 
2294388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
230cc532915SMichael Ellerman 		crash_kexec(regs);
2319b00ac06SAnton Blanchard 
232760ca4dcSAnton Blanchard 	if (!signr)
233760ca4dcSAnton Blanchard 		return;
234760ca4dcSAnton Blanchard 
23558154c8cSAnton Blanchard 	/*
23658154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
23758154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
23858154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
23958154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
24058154c8cSAnton Blanchard 	 */
24158154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
24258154c8cSAnton Blanchard 	    is_global_init(current)) {
24358154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
24458154c8cSAnton Blanchard 	}
24558154c8cSAnton Blanchard 
246cea6a4baSHorms 	if (panic_on_oops)
247012c437dSHorms 		panic("Fatal exception");
248760ca4dcSAnton Blanchard 	do_exit(signr);
249760ca4dcSAnton Blanchard }
25003465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
251cea6a4baSHorms 
252d7e02f7bSAneesh Kumar K.V static char *get_mmu_str(void)
253d7e02f7bSAneesh Kumar K.V {
254d7e02f7bSAneesh Kumar K.V 	if (early_radix_enabled())
255d7e02f7bSAneesh Kumar K.V 		return " MMU=Radix";
256d7e02f7bSAneesh Kumar K.V 	if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
257d7e02f7bSAneesh Kumar K.V 		return " MMU=Hash";
258d7e02f7bSAneesh Kumar K.V 	return "";
259d7e02f7bSAneesh Kumar K.V }
260d7e02f7bSAneesh Kumar K.V 
26103465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
262760ca4dcSAnton Blanchard {
263760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2642e82ca3cSMichael Ellerman 
265d7e02f7bSAneesh Kumar K.V 	printk("%s PAGE_SIZE=%luK%s%s%s%s%s%s %s\n",
26678227443SMichael Ellerman 	       IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) ? "LE" : "BE",
267d7e02f7bSAneesh Kumar K.V 	       PAGE_SIZE / 1024, get_mmu_str(),
26878227443SMichael Ellerman 	       IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
26978227443SMichael Ellerman 	       IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
27078227443SMichael Ellerman 	       IS_ENABLED(CONFIG_SMP) ? (" NR_CPUS=" __stringify(NR_CPUS)) : "",
27178227443SMichael Ellerman 	       debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
27278227443SMichael Ellerman 	       IS_ENABLED(CONFIG_NUMA) ? " NUMA" : "",
27378227443SMichael Ellerman 	       ppc_md.name ? ppc_md.name : "");
274760ca4dcSAnton Blanchard 
275760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
276760ca4dcSAnton Blanchard 		return 1;
277760ca4dcSAnton Blanchard 
278760ca4dcSAnton Blanchard 	print_modules();
279760ca4dcSAnton Blanchard 	show_regs(regs);
28014cf11afSPaul Mackerras 
28114cf11afSPaul Mackerras 	return 0;
28214cf11afSPaul Mackerras }
28303465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
28414cf11afSPaul Mackerras 
285760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
286760ca4dcSAnton Blanchard {
2876f44b20eSNicholas Piggin 	unsigned long flags;
288760ca4dcSAnton Blanchard 
289d40b6768SNicholas Piggin 	/*
290d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
291d40b6768SNicholas Piggin 	 */
2927153d4bfSXiongwei Song 	if (TRAP(regs) != INTERRUPT_SYSTEM_RESET) {
2936f44b20eSNicholas Piggin 		if (debugger(regs))
2946f44b20eSNicholas Piggin 			return;
295d40b6768SNicholas Piggin 	}
2966f44b20eSNicholas Piggin 
2976f44b20eSNicholas Piggin 	flags = oops_begin(regs);
298760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
299760ca4dcSAnton Blanchard 		err = 0;
300760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
301760ca4dcSAnton Blanchard }
30215770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
303760ca4dcSAnton Blanchard 
304efc463adSEric W. Biederman void user_single_step_report(struct pt_regs *regs)
30525baa35bSOleg Nesterov {
3062e1661d2SEric W. Biederman 	force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)regs->nip);
30725baa35bSOleg Nesterov }
30825baa35bSOleg Nesterov 
309658b0f92SMurilo Opsfelder Araujo static void show_signal_msg(int signr, struct pt_regs *regs, int code,
310658b0f92SMurilo Opsfelder Araujo 			    unsigned long addr)
31114cf11afSPaul Mackerras {
312997dd26cSMichael Ellerman 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
313997dd26cSMichael Ellerman 				      DEFAULT_RATELIMIT_BURST);
314997dd26cSMichael Ellerman 
315997dd26cSMichael Ellerman 	if (!show_unhandled_signals)
31635a52a10SMurilo Opsfelder Araujo 		return;
31735a52a10SMurilo Opsfelder Araujo 
31835a52a10SMurilo Opsfelder Araujo 	if (!unhandled_signal(current, signr))
31935a52a10SMurilo Opsfelder Araujo 		return;
32035a52a10SMurilo Opsfelder Araujo 
321997dd26cSMichael Ellerman 	if (!__ratelimit(&rs))
322997dd26cSMichael Ellerman 		return;
323997dd26cSMichael Ellerman 
3240f642d61SMurilo Opsfelder Araujo 	pr_info("%s[%d]: %s (%d) at %lx nip %lx lr %lx code %x",
3250f642d61SMurilo Opsfelder Araujo 		current->comm, current->pid, signame(signr), signr,
326d0c3d534SOlof Johansson 		addr, regs->nip, regs->link, code);
3270f642d61SMurilo Opsfelder Araujo 
3280f642d61SMurilo Opsfelder Araujo 	print_vma_addr(KERN_CONT " in ", regs->nip);
3290f642d61SMurilo Opsfelder Araujo 
3300f642d61SMurilo Opsfelder Araujo 	pr_cont("\n");
331a99b9c5eSMurilo Opsfelder Araujo 
332a99b9c5eSMurilo Opsfelder Araujo 	show_user_instructions(regs);
33314cf11afSPaul Mackerras }
334658b0f92SMurilo Opsfelder Araujo 
3352c44ce28SEric W. Biederman static bool exception_common(int signr, struct pt_regs *regs, int code,
3362c44ce28SEric W. Biederman 			      unsigned long addr)
337658b0f92SMurilo Opsfelder Araujo {
338658b0f92SMurilo Opsfelder Araujo 	if (!user_mode(regs)) {
339658b0f92SMurilo Opsfelder Araujo 		die("Exception in kernel mode", regs, signr);
3402c44ce28SEric W. Biederman 		return false;
341658b0f92SMurilo Opsfelder Araujo 	}
342658b0f92SMurilo Opsfelder Araujo 
343d0afd44cSNicholas Piggin 	/*
344d0afd44cSNicholas Piggin 	 * Must not enable interrupts even for user-mode exception, because
345d0afd44cSNicholas Piggin 	 * this can be called from machine check, which may be a NMI or IRQ
346d0afd44cSNicholas Piggin 	 * which don't like interrupts being enabled. Could check for
347d0afd44cSNicholas Piggin 	 * in_hardirq || in_nmi perhaps, but there doesn't seem to be a good
348d0afd44cSNicholas Piggin 	 * reason why _exception() should enable irqs for an exception handler,
349d0afd44cSNicholas Piggin 	 * the handlers themselves do that directly.
350d0afd44cSNicholas Piggin 	 */
35114cf11afSPaul Mackerras 
352d0afd44cSNicholas Piggin 	show_signal_msg(signr, regs, code, addr);
3539f2f79e3SBenjamin Herrenschmidt 
35441ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
355c5cc1f4dSThiago Jung Bauermann 
3562c44ce28SEric W. Biederman 	return true;
3572c44ce28SEric W. Biederman }
3582c44ce28SEric W. Biederman 
3595d8fb8a5SEric W. Biederman void _exception_pkey(struct pt_regs *regs, unsigned long addr, int key)
3602c44ce28SEric W. Biederman {
3615d8fb8a5SEric W. Biederman 	if (!exception_common(SIGSEGV, regs, SEGV_PKUERR, addr))
3622c44ce28SEric W. Biederman 		return;
3632c44ce28SEric W. Biederman 
36477c70728SEric W. Biederman 	force_sig_pkuerr((void __user *) addr, key);
36514cf11afSPaul Mackerras }
36614cf11afSPaul Mackerras 
36799cd1302SRam Pai void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
36899cd1302SRam Pai {
369c1c7c85cSEric W. Biederman 	if (!exception_common(signr, regs, code, addr))
370c1c7c85cSEric W. Biederman 		return;
371c1c7c85cSEric W. Biederman 
3722e1661d2SEric W. Biederman 	force_sig_fault(signr, code, (void __user *)addr);
37399cd1302SRam Pai }
37499cd1302SRam Pai 
375ccd47702SNicholas Piggin /*
376ccd47702SNicholas Piggin  * The interrupt architecture has a quirk in that the HV interrupts excluding
377ccd47702SNicholas Piggin  * the NMIs (0x100 and 0x200) do not clear MSR[RI] at entry. The first thing
378ccd47702SNicholas Piggin  * that an interrupt handler must do is save off a GPR into a scratch register,
379ccd47702SNicholas Piggin  * and all interrupts on POWERNV (HV=1) use the HSPRG1 register as scratch.
380ccd47702SNicholas Piggin  * Therefore an NMI can clobber an HV interrupt's live HSPRG1 without noticing
381ccd47702SNicholas Piggin  * that it is non-reentrant, which leads to random data corruption.
382ccd47702SNicholas Piggin  *
383ccd47702SNicholas Piggin  * The solution is for NMI interrupts in HV mode to check if they originated
384ccd47702SNicholas Piggin  * from these critical HV interrupt regions. If so, then mark them not
385ccd47702SNicholas Piggin  * recoverable.
386ccd47702SNicholas Piggin  *
387ccd47702SNicholas Piggin  * An alternative would be for HV NMIs to use SPRG for scratch to avoid the
388ccd47702SNicholas Piggin  * HSPRG1 clobber, however this would cause guest SPRG to be clobbered. Linux
389ccd47702SNicholas Piggin  * guests should always have MSR[RI]=0 when its scratch SPRG is in use, so
390ccd47702SNicholas Piggin  * that would work. However any other guest OS that may have the SPRG live
391ccd47702SNicholas Piggin  * and MSR[RI]=1 could encounter silent corruption.
392ccd47702SNicholas Piggin  *
393ccd47702SNicholas Piggin  * Builds that do not support KVM could take this second option to increase
394ccd47702SNicholas Piggin  * the recoverability of NMIs.
395ccd47702SNicholas Piggin  */
396ccd47702SNicholas Piggin void hv_nmi_check_nonrecoverable(struct pt_regs *regs)
397ccd47702SNicholas Piggin {
398ccd47702SNicholas Piggin #ifdef CONFIG_PPC_POWERNV
399ccd47702SNicholas Piggin 	unsigned long kbase = (unsigned long)_stext;
400ccd47702SNicholas Piggin 	unsigned long nip = regs->nip;
401ccd47702SNicholas Piggin 
402ccd47702SNicholas Piggin 	if (!(regs->msr & MSR_RI))
403ccd47702SNicholas Piggin 		return;
404ccd47702SNicholas Piggin 	if (!(regs->msr & MSR_HV))
405ccd47702SNicholas Piggin 		return;
406ccd47702SNicholas Piggin 	if (regs->msr & MSR_PR)
407ccd47702SNicholas Piggin 		return;
408ccd47702SNicholas Piggin 
409ccd47702SNicholas Piggin 	/*
410ccd47702SNicholas Piggin 	 * Now test if the interrupt has hit a range that may be using
411ccd47702SNicholas Piggin 	 * HSPRG1 without having RI=0 (i.e., an HSRR interrupt). The
412ccd47702SNicholas Piggin 	 * problem ranges all run un-relocated. Test real and virt modes
4135c4a4802SBhaskar Chowdhury 	 * at the same time by dropping the high bit of the nip (virt mode
414ccd47702SNicholas Piggin 	 * entry points still have the +0x4000 offset).
415ccd47702SNicholas Piggin 	 */
416ccd47702SNicholas Piggin 	nip &= ~0xc000000000000000ULL;
417ccd47702SNicholas Piggin 	if ((nip >= 0x500 && nip < 0x600) || (nip >= 0x4500 && nip < 0x4600))
418ccd47702SNicholas Piggin 		goto nonrecoverable;
419ccd47702SNicholas Piggin 	if ((nip >= 0x980 && nip < 0xa00) || (nip >= 0x4980 && nip < 0x4a00))
420ccd47702SNicholas Piggin 		goto nonrecoverable;
421ccd47702SNicholas Piggin 	if ((nip >= 0xe00 && nip < 0xec0) || (nip >= 0x4e00 && nip < 0x4ec0))
422ccd47702SNicholas Piggin 		goto nonrecoverable;
423ccd47702SNicholas Piggin 	if ((nip >= 0xf80 && nip < 0xfa0) || (nip >= 0x4f80 && nip < 0x4fa0))
424ccd47702SNicholas Piggin 		goto nonrecoverable;
425bd3524feSNicholas Piggin 
426ccd47702SNicholas Piggin 	/* Trampoline code runs un-relocated so subtract kbase. */
427bd3524feSNicholas Piggin 	if (nip >= (unsigned long)(start_real_trampolines - kbase) &&
428bd3524feSNicholas Piggin 			nip < (unsigned long)(end_real_trampolines - kbase))
429ccd47702SNicholas Piggin 		goto nonrecoverable;
430bd3524feSNicholas Piggin 	if (nip >= (unsigned long)(start_virt_trampolines - kbase) &&
431bd3524feSNicholas Piggin 			nip < (unsigned long)(end_virt_trampolines - kbase))
432ccd47702SNicholas Piggin 		goto nonrecoverable;
433ccd47702SNicholas Piggin 	return;
434ccd47702SNicholas Piggin 
435ccd47702SNicholas Piggin nonrecoverable:
436806c0e6eSChristophe Leroy 	regs_set_unrecoverable(regs);
437ccd47702SNicholas Piggin #endif
438ccd47702SNicholas Piggin }
4393a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(system_reset_exception)
44014cf11afSPaul Mackerras {
441cbf2ba95SNicholas Piggin 	unsigned long hsrr0, hsrr1;
442cbf2ba95SNicholas Piggin 	bool saved_hsrrs = false;
4432b4f3ac5SNicholas Piggin 
444cbf2ba95SNicholas Piggin 	/*
445cbf2ba95SNicholas Piggin 	 * System reset can interrupt code where HSRRs are live and MSR[RI]=1.
446cbf2ba95SNicholas Piggin 	 * The system reset interrupt itself may clobber HSRRs (e.g., to call
447cbf2ba95SNicholas Piggin 	 * OPAL), so save them here and restore them before returning.
448cbf2ba95SNicholas Piggin 	 *
449cbf2ba95SNicholas Piggin 	 * Machine checks don't need to save HSRRs, as the real mode handler
450cbf2ba95SNicholas Piggin 	 * is careful to avoid them, and the regular handler is not delivered
451cbf2ba95SNicholas Piggin 	 * as an NMI.
452cbf2ba95SNicholas Piggin 	 */
453cbf2ba95SNicholas Piggin 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
454cbf2ba95SNicholas Piggin 		hsrr0 = mfspr(SPRN_HSRR0);
455cbf2ba95SNicholas Piggin 		hsrr1 = mfspr(SPRN_HSRR1);
456cbf2ba95SNicholas Piggin 		saved_hsrrs = true;
457cbf2ba95SNicholas Piggin 	}
458cbf2ba95SNicholas Piggin 
459ccd47702SNicholas Piggin 	hv_nmi_check_nonrecoverable(regs);
460ccd47702SNicholas Piggin 
461ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
462ca41ad43SNicholas Piggin 
46314cf11afSPaul Mackerras 	/* See if any machine dependent calls */
464c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
465c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
466c4f3b52cSNicholas Piggin 			goto out;
467c902be71SArnd Bergmann 	}
46814cf11afSPaul Mackerras 
4694388c9b3SNicholas Piggin 	if (debugger(regs))
4704388c9b3SNicholas Piggin 		goto out;
4714388c9b3SNicholas Piggin 
472e7ca44edSGanesh Goudar 	kmsg_dump(KMSG_DUMP_OOPS);
4734388c9b3SNicholas Piggin 	/*
4744388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
4754388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
4764388c9b3SNicholas Piggin 	 * registered).
4774388c9b3SNicholas Piggin 	 */
4784388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
4794388c9b3SNicholas Piggin 
4804388c9b3SNicholas Piggin 	crash_kexec(regs);
4814388c9b3SNicholas Piggin 
4824388c9b3SNicholas Piggin 	/*
4834388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
4844388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
4854388c9b3SNicholas Piggin 	 * code.
4864388c9b3SNicholas Piggin 	 */
4874388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
4884388c9b3SNicholas Piggin 
4894388c9b3SNicholas Piggin 	/*
4904388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
4914388c9b3SNicholas Piggin 	 * panic.
4924388c9b3SNicholas Piggin 	 */
4934552d128SNicholas Piggin 	die("System Reset", regs, SIGABRT);
4944388c9b3SNicholas Piggin 
4954388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
4964388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
4974388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
49814cf11afSPaul Mackerras 
499c4f3b52cSNicholas Piggin out:
500c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
501c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
502c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
503265d6e58SNicholas Piggin 		die("Unrecoverable nested System Reset", regs, SIGABRT);
504c4f3b52cSNicholas Piggin #endif
50514cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
506806c0e6eSChristophe Leroy 	if (regs_is_unrecoverable(regs)) {
50711cb0a25SNicholas Piggin 		/* For the reason explained in die_mce, nmi_exit before die */
50811cb0a25SNicholas Piggin 		nmi_exit();
509265d6e58SNicholas Piggin 		die("Unrecoverable System Reset", regs, SIGABRT);
51011cb0a25SNicholas Piggin 	}
51114cf11afSPaul Mackerras 
512cbf2ba95SNicholas Piggin 	if (saved_hsrrs) {
513cbf2ba95SNicholas Piggin 		mtspr(SPRN_HSRR0, hsrr0);
514cbf2ba95SNicholas Piggin 		mtspr(SPRN_HSRR1, hsrr1);
515cbf2ba95SNicholas Piggin 	}
516cbf2ba95SNicholas Piggin 
51714cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
5183a96570fSNicholas Piggin 
5193a96570fSNicholas Piggin 	return 0;
52014cf11afSPaul Mackerras }
5211e9b4507SMahesh Salgaonkar 
52214cf11afSPaul Mackerras /*
52314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
52414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
52514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
52614cf11afSPaul Mackerras  * table.
52714cf11afSPaul Mackerras  *  -- paulus.
52814cf11afSPaul Mackerras  */
52914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
53014cf11afSPaul Mackerras {
53168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
53214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
53314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
53414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
53514cf11afSPaul Mackerras 
53614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
53714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
53814cf11afSPaul Mackerras 		/*
53914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
54014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
54114cf11afSPaul Mackerras 		 * As the address is in the exception table
54214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
54314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
54414cf11afSPaul Mackerras 		 * load or store.
54514cf11afSPaul Mackerras 		 */
546deefd0aeSChristophe Leroy 		if (*nip == PPC_RAW_NOP())
54714cf11afSPaul Mackerras 			nip -= 2;
548deefd0aeSChristophe Leroy 		else if (*nip == PPC_RAW_ISYNC())
54914cf11afSPaul Mackerras 			--nip;
550deefd0aeSChristophe Leroy 		if (*nip == PPC_RAW_SYNC() || get_op(*nip) == OP_TRAP) {
55114cf11afSPaul Mackerras 			unsigned int rb;
55214cf11afSPaul Mackerras 
55314cf11afSPaul Mackerras 			--nip;
55414cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
55514cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
55614cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
55714cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
558806c0e6eSChristophe Leroy 			regs_set_recoverable(regs);
55959dc5bfcSNicholas Piggin 			regs_set_return_ip(regs, extable_fixup(entry));
56014cf11afSPaul Mackerras 			return 1;
56114cf11afSPaul Mackerras 		}
56214cf11afSPaul Mackerras 	}
56368a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
56414cf11afSPaul Mackerras 	return 0;
56514cf11afSPaul Mackerras }
56614cf11afSPaul Mackerras 
567172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
56814cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
56914cf11afSPaul Mackerras    is in the ESR. */
5704f8e78c0SXiongwei Song #define get_reason(regs)	((regs)->esr)
57114cf11afSPaul Mackerras #define REASON_FP		ESR_FP
57214cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
57314cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
57414cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
5759409d2f9SJordan Niethe #define REASON_PREFIXED		0
5769409d2f9SJordan Niethe #define REASON_BOUNDARY		0
57714cf11afSPaul Mackerras 
57814cf11afSPaul Mackerras /* single-step stuff */
57951ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
58051ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
5810e524e76SMatt Evans #define clear_br_trace(regs)	do {} while(0)
58214cf11afSPaul Mackerras #else
58314cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
58414cf11afSPaul Mackerras    exception is in the MSR. */
58514cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
586d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
587d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
588d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
589d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
590d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
5919409d2f9SJordan Niethe #define REASON_PREFIXED		SRR1_PREFIXED
5929409d2f9SJordan Niethe #define REASON_BOUNDARY		SRR1_BOUNDARY
59314cf11afSPaul Mackerras 
59414cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
59559dc5bfcSNicholas Piggin #define clear_single_step(regs)	(regs_set_return_msr((regs), (regs)->msr & ~MSR_SE))
59659dc5bfcSNicholas Piggin #define clear_br_trace(regs)	(regs_set_return_msr((regs), (regs)->msr & ~MSR_BE))
59714cf11afSPaul Mackerras #endif
59814cf11afSPaul Mackerras 
5999409d2f9SJordan Niethe #define inst_length(reason)	(((reason) & REASON_PREFIXED) ? 8 : 4)
6009409d2f9SJordan Niethe 
6010d0935b3SMichael Ellerman #if defined(CONFIG_E500)
602fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
603fe04b112SScott Wood {
604fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
605a4e89ffbSMatt Weber 	unsigned long pvr = mfspr(SPRN_PVR);
606fe04b112SScott Wood 	unsigned long reason = mcsr;
607fe04b112SScott Wood 	int recoverable = 1;
608fe04b112SScott Wood 
60982a9a480SScott Wood 	if (reason & MCSR_LD) {
610cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
611cce1f106SShaohui Xie 		if (recoverable == 1)
612cce1f106SShaohui Xie 			goto silent_out;
613cce1f106SShaohui Xie 	}
614cce1f106SShaohui Xie 
615fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
616fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
617fe04b112SScott Wood 
618fe04b112SScott Wood 	if (reason & MCSR_MCP)
619422123ccSChristophe Leroy 		pr_cont("Machine Check Signal\n");
620fe04b112SScott Wood 
621fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
622422123ccSChristophe Leroy 		pr_cont("Instruction Cache Parity Error\n");
623fe04b112SScott Wood 
624fe04b112SScott Wood 		/*
625fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
626fe04b112SScott Wood 		 */
627fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
628fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
629fe04b112SScott Wood 			;
630fe04b112SScott Wood 
631fe04b112SScott Wood 		/*
632fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
633fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
634fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
635fe04b112SScott Wood 		 */
636fe04b112SScott Wood 		reason &= ~MCSR_IF;
637fe04b112SScott Wood 	}
638fe04b112SScott Wood 
639fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
640422123ccSChristophe Leroy 		pr_cont("Data Cache Parity Error\n");
64137caf9f2SKumar Gala 
64237caf9f2SKumar Gala 		/*
64337caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
64437caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
64537caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
64637caf9f2SKumar Gala 		 */
647a4e89ffbSMatt Weber 		/* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
648a4e89ffbSMatt Weber 		 * is not implemented but L1 data cache always runs in write
649a4e89ffbSMatt Weber 		 * shadow mode. Hence on data cache parity errors HW will
650a4e89ffbSMatt Weber 		 * automatically invalidate the L1 Data Cache.
651a4e89ffbSMatt Weber 		 */
652a4e89ffbSMatt Weber 		if (PVR_VER(pvr) != PVR_VER_E6500) {
65337caf9f2SKumar Gala 			if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
654fe04b112SScott Wood 				recoverable = 0;
655fe04b112SScott Wood 		}
656a4e89ffbSMatt Weber 	}
657fe04b112SScott Wood 
658fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
659422123ccSChristophe Leroy 		pr_cont("Hit on multiple TLB entries\n");
660fe04b112SScott Wood 		recoverable = 0;
661fe04b112SScott Wood 	}
662fe04b112SScott Wood 
663fe04b112SScott Wood 	if (reason & MCSR_NMI)
664422123ccSChristophe Leroy 		pr_cont("Non-maskable interrupt\n");
665fe04b112SScott Wood 
666fe04b112SScott Wood 	if (reason & MCSR_IF) {
667422123ccSChristophe Leroy 		pr_cont("Instruction Fetch Error Report\n");
668fe04b112SScott Wood 		recoverable = 0;
669fe04b112SScott Wood 	}
670fe04b112SScott Wood 
671fe04b112SScott Wood 	if (reason & MCSR_LD) {
672422123ccSChristophe Leroy 		pr_cont("Load Error Report\n");
673fe04b112SScott Wood 		recoverable = 0;
674fe04b112SScott Wood 	}
675fe04b112SScott Wood 
676fe04b112SScott Wood 	if (reason & MCSR_ST) {
677422123ccSChristophe Leroy 		pr_cont("Store Error Report\n");
678fe04b112SScott Wood 		recoverable = 0;
679fe04b112SScott Wood 	}
680fe04b112SScott Wood 
681fe04b112SScott Wood 	if (reason & MCSR_LDG) {
682422123ccSChristophe Leroy 		pr_cont("Guarded Load Error Report\n");
683fe04b112SScott Wood 		recoverable = 0;
684fe04b112SScott Wood 	}
685fe04b112SScott Wood 
686fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
687422123ccSChristophe Leroy 		pr_cont("Simultaneous tlbsync operations\n");
688fe04b112SScott Wood 
689fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
690422123ccSChristophe Leroy 		pr_cont("Level 2 Cache Error\n");
691fe04b112SScott Wood 		recoverable = 0;
692fe04b112SScott Wood 	}
693fe04b112SScott Wood 
694fe04b112SScott Wood 	if (reason & MCSR_MAV) {
695fe04b112SScott Wood 		u64 addr;
696fe04b112SScott Wood 
697fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
698fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
699fe04b112SScott Wood 
700422123ccSChristophe Leroy 		pr_cont("Machine Check %s Address: %#llx\n",
701fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
702fe04b112SScott Wood 	}
703fe04b112SScott Wood 
704cce1f106SShaohui Xie silent_out:
705fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
706fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
707fe04b112SScott Wood }
708fe04b112SScott Wood 
70947c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
71047c0bd1aSBenjamin Herrenschmidt {
71142bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
71247c0bd1aSBenjamin Herrenschmidt 
713cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
714cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
715cce1f106SShaohui Xie 			return 1;
7164e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
7174e0e3435SHongtao Jia 			return 1;
718cce1f106SShaohui Xie 	}
719cce1f106SShaohui Xie 
72014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
72114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
72214cf11afSPaul Mackerras 
72314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
724422123ccSChristophe Leroy 		pr_cont("Machine Check Signal\n");
72514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
726422123ccSChristophe Leroy 		pr_cont("Instruction Cache Parity Error\n");
72714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
728422123ccSChristophe Leroy 		pr_cont("Data Cache Push Parity Error\n");
72914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
730422123ccSChristophe Leroy 		pr_cont("Data Cache Parity Error\n");
73114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
732422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Address Error\n");
73314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
734422123ccSChristophe Leroy 		pr_cont("Bus - Read Address Error\n");
73514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
736422123ccSChristophe Leroy 		pr_cont("Bus - Write Address Error\n");
73714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
738422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Data Error\n");
73914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
740422123ccSChristophe Leroy 		pr_cont("Bus - Read Data Bus Error\n");
74114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
742422123ccSChristophe Leroy 		pr_cont("Bus - Write Data Bus Error\n");
74314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
744422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Parity Error\n");
74514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
746422123ccSChristophe Leroy 		pr_cont("Bus - Read Parity Error\n");
74747c0bd1aSBenjamin Herrenschmidt 
74847c0bd1aSBenjamin Herrenschmidt 	return 0;
74947c0bd1aSBenjamin Herrenschmidt }
7504490c06bSKumar Gala 
7514490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
7524490c06bSKumar Gala {
7534490c06bSKumar Gala 	return 0;
7544490c06bSKumar Gala }
7557f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
75647c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
75747c0bd1aSBenjamin Herrenschmidt {
75842bff234SMichael Ellerman 	unsigned long reason = regs->msr;
75947c0bd1aSBenjamin Herrenschmidt 
76014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
76114cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
76214cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
76314cf11afSPaul Mackerras 	case 0x80000:
764422123ccSChristophe Leroy 		pr_cont("Machine check signal\n");
76514cf11afSPaul Mackerras 		break;
76614cf11afSPaul Mackerras 	case 0x40000:
76714cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
768422123ccSChristophe Leroy 		pr_cont("Transfer error ack signal\n");
76914cf11afSPaul Mackerras 		break;
77014cf11afSPaul Mackerras 	case 0x20000:
771422123ccSChristophe Leroy 		pr_cont("Data parity error signal\n");
77214cf11afSPaul Mackerras 		break;
77314cf11afSPaul Mackerras 	case 0x10000:
774422123ccSChristophe Leroy 		pr_cont("Address parity error signal\n");
77514cf11afSPaul Mackerras 		break;
77614cf11afSPaul Mackerras 	case 0x20000000:
777422123ccSChristophe Leroy 		pr_cont("L1 Data Cache error\n");
77814cf11afSPaul Mackerras 		break;
77914cf11afSPaul Mackerras 	case 0x40000000:
780422123ccSChristophe Leroy 		pr_cont("L1 Instruction Cache error\n");
78114cf11afSPaul Mackerras 		break;
78214cf11afSPaul Mackerras 	case 0x00100000:
783422123ccSChristophe Leroy 		pr_cont("L2 data cache parity error\n");
78414cf11afSPaul Mackerras 		break;
78514cf11afSPaul Mackerras 	default:
786422123ccSChristophe Leroy 		pr_cont("Unknown values in msr\n");
78714cf11afSPaul Mackerras 	}
78875918a4bSOlof Johansson 	return 0;
78975918a4bSOlof Johansson }
79047c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
79175918a4bSOlof Johansson 
792209e9d50SNicholas Piggin void die_mce(const char *str, struct pt_regs *regs, long err)
793209e9d50SNicholas Piggin {
794209e9d50SNicholas Piggin 	/*
795209e9d50SNicholas Piggin 	 * The machine check wants to kill the interrupted context, but
796209e9d50SNicholas Piggin 	 * do_exit() checks for in_interrupt() and panics in that case, so
797209e9d50SNicholas Piggin 	 * exit the irq/nmi before calling die.
798209e9d50SNicholas Piggin 	 */
799*f08fb25bSNicholas Piggin 	if (in_nmi())
800209e9d50SNicholas Piggin 		nmi_exit();
801*f08fb25bSNicholas Piggin 	else
802*f08fb25bSNicholas Piggin 		irq_exit();
803209e9d50SNicholas Piggin 	die(str, regs, err);
804209e9d50SNicholas Piggin }
805209e9d50SNicholas Piggin 
806118178e6SNicholas Piggin /*
807*f08fb25bSNicholas Piggin  * BOOK3S_64 does not usually call this handler as a non-maskable interrupt
808118178e6SNicholas Piggin  * (it uses its own early real-mode handler to handle the MCE proper
809118178e6SNicholas Piggin  * and then raises irq_work to call this handler when interrupts are
810*f08fb25bSNicholas Piggin  * enabled). The only time when this is not true is if the early handler
811*f08fb25bSNicholas Piggin  * is unrecoverable, then it does call this directly to try to get a
812*f08fb25bSNicholas Piggin  * message out.
813118178e6SNicholas Piggin  */
814*f08fb25bSNicholas Piggin static void __machine_check_exception(struct pt_regs *regs)
81575918a4bSOlof Johansson {
81675918a4bSOlof Johansson 	int recover = 0;
81769ea03b5SPeter Zijlstra 
81869111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
81989713ed1SAnton Blanchard 
820d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
821d93b0ac0SMahesh Salgaonkar 
82247c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
82347c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
82447c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
82547c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
82647c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
82747c0bd1aSBenjamin Herrenschmidt 	 */
82875918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
82975918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
83047c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
83147c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
83275918a4bSOlof Johansson 
83347c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
834ba12eedeSLi Zhong 		goto bail;
83575918a4bSOlof Johansson 
836a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
837ba12eedeSLi Zhong 		goto bail;
83875918a4bSOlof Johansson 
83975918a4bSOlof Johansson 	if (check_io_access(regs))
840ba12eedeSLi Zhong 		goto bail;
84175918a4bSOlof Johansson 
842209e9d50SNicholas Piggin 	die_mce("Machine check", regs, SIGBUS);
843daf00ae7SChristophe Leroy 
844c538938fSNicholas Piggin bail:
8450bbea75cSChristophe Leroy 	/* Must die if the interrupt is not recoverable */
846806c0e6eSChristophe Leroy 	if (regs_is_unrecoverable(regs))
847209e9d50SNicholas Piggin 		die_mce("Unrecoverable Machine check", regs, SIGBUS);
848*f08fb25bSNicholas Piggin }
849daf00ae7SChristophe Leroy 
8503a96570fSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
851*f08fb25bSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(machine_check_exception_async)
852*f08fb25bSNicholas Piggin {
853*f08fb25bSNicholas Piggin 	__machine_check_exception(regs);
854*f08fb25bSNicholas Piggin }
8553a96570fSNicholas Piggin #endif
856*f08fb25bSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(machine_check_exception)
857*f08fb25bSNicholas Piggin {
858*f08fb25bSNicholas Piggin 	__machine_check_exception(regs);
859*f08fb25bSNicholas Piggin 
860*f08fb25bSNicholas Piggin 	return 0;
86114cf11afSPaul Mackerras }
86214cf11afSPaul Mackerras 
8633a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SMIException) /* async? */
86414cf11afSPaul Mackerras {
86514cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
86614cf11afSPaul Mackerras }
86714cf11afSPaul Mackerras 
8685080332cSMichael Neuling #ifdef CONFIG_VSX
8695080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs)
8705080332cSMichael Neuling {
8715080332cSMichael Neuling 	unsigned int ra, rb, t, i, sel, instr, rc;
8725080332cSMichael Neuling 	const void __user *addr;
8731da4a027SMichael Neuling 	u8 vbuf[16] __aligned(16), *vdst;
8745080332cSMichael Neuling 	unsigned long ea, msr, msr_mask;
8755080332cSMichael Neuling 	bool swap;
8765080332cSMichael Neuling 
877bad956b8SChristophe Leroy 	if (__get_user(instr, (unsigned int __user *)regs->nip))
8785080332cSMichael Neuling 		return;
8795080332cSMichael Neuling 
8805080332cSMichael Neuling 	/*
8815080332cSMichael Neuling 	 * lxvb16x	opcode: 0x7c0006d8
8825080332cSMichael Neuling 	 * lxvd2x	opcode: 0x7c000698
8835080332cSMichael Neuling 	 * lxvh8x	opcode: 0x7c000658
8845080332cSMichael Neuling 	 * lxvw4x	opcode: 0x7c000618
8855080332cSMichael Neuling 	 */
8865080332cSMichael Neuling 	if ((instr & 0xfc00073e) != 0x7c000618) {
8875080332cSMichael Neuling 		pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
8885080332cSMichael Neuling 			 " instr=%08x\n",
8895080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8905080332cSMichael Neuling 			 regs->nip, instr);
8915080332cSMichael Neuling 		return;
8925080332cSMichael Neuling 	}
8935080332cSMichael Neuling 
8945080332cSMichael Neuling 	/* Grab vector registers into the task struct */
8955080332cSMichael Neuling 	msr = regs->msr; /* Grab msr before we flush the bits */
8965080332cSMichael Neuling 	flush_vsx_to_thread(current);
8975080332cSMichael Neuling 	enable_kernel_altivec();
8985080332cSMichael Neuling 
8995080332cSMichael Neuling 	/*
9005080332cSMichael Neuling 	 * Is userspace running with a different endian (this is rare but
9015080332cSMichael Neuling 	 * not impossible)
9025080332cSMichael Neuling 	 */
9035080332cSMichael Neuling 	swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
9045080332cSMichael Neuling 
9055080332cSMichael Neuling 	/* Decode the instruction */
9065080332cSMichael Neuling 	ra = (instr >> 16) & 0x1f;
9075080332cSMichael Neuling 	rb = (instr >> 11) & 0x1f;
9085080332cSMichael Neuling 	t = (instr >> 21) & 0x1f;
9095080332cSMichael Neuling 	if (instr & 1)
9105080332cSMichael Neuling 		vdst = (u8 *)&current->thread.vr_state.vr[t];
9115080332cSMichael Neuling 	else
9125080332cSMichael Neuling 		vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
9135080332cSMichael Neuling 
9145080332cSMichael Neuling 	/* Grab the vector address */
9155080332cSMichael Neuling 	ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
9165080332cSMichael Neuling 	if (is_32bit_task())
9175080332cSMichael Neuling 		ea &= 0xfffffffful;
9185080332cSMichael Neuling 	addr = (__force const void __user *)ea;
9195080332cSMichael Neuling 
9205080332cSMichael Neuling 	/* Check it */
92196d4f267SLinus Torvalds 	if (!access_ok(addr, 16)) {
9225080332cSMichael Neuling 		pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
9235080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
9245080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9255080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
9265080332cSMichael Neuling 		return;
9275080332cSMichael Neuling 	}
9285080332cSMichael Neuling 
9295080332cSMichael Neuling 	/* Read the vector */
9305080332cSMichael Neuling 	rc = 0;
9315080332cSMichael Neuling 	if ((unsigned long)addr & 0xfUL)
9325080332cSMichael Neuling 		/* unaligned case */
9335080332cSMichael Neuling 		rc = __copy_from_user_inatomic(vbuf, addr, 16);
9345080332cSMichael Neuling 	else
9355080332cSMichael Neuling 		__get_user_atomic_128_aligned(vbuf, addr, rc);
9365080332cSMichael Neuling 	if (rc) {
9375080332cSMichael Neuling 		pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
9385080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
9395080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9405080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
9415080332cSMichael Neuling 		return;
9425080332cSMichael Neuling 	}
9435080332cSMichael Neuling 
9445080332cSMichael Neuling 	pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
9455080332cSMichael Neuling 		 " instr=%08x addr=%016lx\n",
9465080332cSMichael Neuling 		 smp_processor_id(), current->comm, current->pid, regs->nip,
9475080332cSMichael Neuling 		 instr, (unsigned long) addr);
9485080332cSMichael Neuling 
9495080332cSMichael Neuling 	/* Grab instruction "selector" */
9505080332cSMichael Neuling 	sel = (instr >> 6) & 3;
9515080332cSMichael Neuling 
9525080332cSMichael Neuling 	/*
9535080332cSMichael Neuling 	 * Check to make sure the facility is actually enabled. This
9545080332cSMichael Neuling 	 * could happen if we get a false positive hit.
9555080332cSMichael Neuling 	 *
9565080332cSMichael Neuling 	 * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
9575080332cSMichael Neuling 	 * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
9585080332cSMichael Neuling 	 */
9595080332cSMichael Neuling 	msr_mask = MSR_VSX;
9605080332cSMichael Neuling 	if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
9615080332cSMichael Neuling 		msr_mask = MSR_VEC;
9625080332cSMichael Neuling 	if (!(msr & msr_mask)) {
9635080332cSMichael Neuling 		pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
9645080332cSMichael Neuling 			 " instr=%08x msr:%016lx\n",
9655080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9665080332cSMichael Neuling 			 regs->nip, instr, msr);
9675080332cSMichael Neuling 		return;
9685080332cSMichael Neuling 	}
9695080332cSMichael Neuling 
9705080332cSMichael Neuling 	/* Do logging here before we modify sel based on endian */
9715080332cSMichael Neuling 	switch (sel) {
9725080332cSMichael Neuling 	case 0:	/* lxvw4x */
9735080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvw4x, regs);
9745080332cSMichael Neuling 		break;
9755080332cSMichael Neuling 	case 1: /* lxvh8x */
9765080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvh8x, regs);
9775080332cSMichael Neuling 		break;
9785080332cSMichael Neuling 	case 2: /* lxvd2x */
9795080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvd2x, regs);
9805080332cSMichael Neuling 		break;
9815080332cSMichael Neuling 	case 3: /* lxvb16x */
9825080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvb16x, regs);
9835080332cSMichael Neuling 		break;
9845080332cSMichael Neuling 	}
9855080332cSMichael Neuling 
9865080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__
9875080332cSMichael Neuling 	/*
9885080332cSMichael Neuling 	 * An LE kernel stores the vector in the task struct as an LE
9895080332cSMichael Neuling 	 * byte array (effectively swapping both the components and
9905080332cSMichael Neuling 	 * the content of the components). Those instructions expect
9915080332cSMichael Neuling 	 * the components to remain in ascending address order, so we
9925080332cSMichael Neuling 	 * swap them back.
9935080332cSMichael Neuling 	 *
9945080332cSMichael Neuling 	 * If we are running a BE user space, the expectation is that
9955080332cSMichael Neuling 	 * of a simple memcpy, so forcing the emulation to look like
9965080332cSMichael Neuling 	 * a lxvb16x should do the trick.
9975080332cSMichael Neuling 	 */
9985080332cSMichael Neuling 	if (swap)
9995080332cSMichael Neuling 		sel = 3;
10005080332cSMichael Neuling 
10015080332cSMichael Neuling 	switch (sel) {
10025080332cSMichael Neuling 	case 0:	/* lxvw4x */
10035080332cSMichael Neuling 		for (i = 0; i < 4; i++)
10045080332cSMichael Neuling 			((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
10055080332cSMichael Neuling 		break;
10065080332cSMichael Neuling 	case 1: /* lxvh8x */
10075080332cSMichael Neuling 		for (i = 0; i < 8; i++)
10085080332cSMichael Neuling 			((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
10095080332cSMichael Neuling 		break;
10105080332cSMichael Neuling 	case 2: /* lxvd2x */
10115080332cSMichael Neuling 		for (i = 0; i < 2; i++)
10125080332cSMichael Neuling 			((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
10135080332cSMichael Neuling 		break;
10145080332cSMichael Neuling 	case 3: /* lxvb16x */
10155080332cSMichael Neuling 		for (i = 0; i < 16; i++)
10165080332cSMichael Neuling 			vdst[i] = vbuf[15-i];
10175080332cSMichael Neuling 		break;
10185080332cSMichael Neuling 	}
10195080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */
10205080332cSMichael Neuling 	/* On a big endian kernel, a BE userspace only needs a memcpy */
10215080332cSMichael Neuling 	if (!swap)
10225080332cSMichael Neuling 		sel = 3;
10235080332cSMichael Neuling 
10245080332cSMichael Neuling 	/* Otherwise, we need to swap the content of the components */
10255080332cSMichael Neuling 	switch (sel) {
10265080332cSMichael Neuling 	case 0:	/* lxvw4x */
10275080332cSMichael Neuling 		for (i = 0; i < 4; i++)
10285080332cSMichael Neuling 			((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
10295080332cSMichael Neuling 		break;
10305080332cSMichael Neuling 	case 1: /* lxvh8x */
10315080332cSMichael Neuling 		for (i = 0; i < 8; i++)
10325080332cSMichael Neuling 			((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
10335080332cSMichael Neuling 		break;
10345080332cSMichael Neuling 	case 2: /* lxvd2x */
10355080332cSMichael Neuling 		for (i = 0; i < 2; i++)
10365080332cSMichael Neuling 			((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
10375080332cSMichael Neuling 		break;
10385080332cSMichael Neuling 	case 3: /* lxvb16x */
10395080332cSMichael Neuling 		memcpy(vdst, vbuf, 16);
10405080332cSMichael Neuling 		break;
10415080332cSMichael Neuling 	}
10425080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */
10435080332cSMichael Neuling 
10445080332cSMichael Neuling 	/* Go to next instruction */
104559dc5bfcSNicholas Piggin 	regs_add_return_ip(regs, 4);
10465080332cSMichael Neuling }
10475080332cSMichael Neuling #endif /* CONFIG_VSX */
10485080332cSMichael Neuling 
10493a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(handle_hmi_exception)
10500869b6fdSMahesh Salgaonkar {
10510869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
10520869b6fdSMahesh Salgaonkar 
10530869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
10540869b6fdSMahesh Salgaonkar 
10555080332cSMichael Neuling #ifdef CONFIG_VSX
10565080332cSMichael Neuling 	/* Real mode flagged P9 special emu is needed */
10575080332cSMichael Neuling 	if (local_paca->hmi_p9_special_emu) {
10585080332cSMichael Neuling 		local_paca->hmi_p9_special_emu = 0;
10595080332cSMichael Neuling 
10605080332cSMichael Neuling 		/*
10615080332cSMichael Neuling 		 * We don't want to take page faults while doing the
10625080332cSMichael Neuling 		 * emulation, we just replay the instruction if necessary.
10635080332cSMichael Neuling 		 */
10645080332cSMichael Neuling 		pagefault_disable();
10655080332cSMichael Neuling 		p9_hmi_special_emu(regs);
10665080332cSMichael Neuling 		pagefault_enable();
10675080332cSMichael Neuling 	}
10685080332cSMichael Neuling #endif /* CONFIG_VSX */
10695080332cSMichael Neuling 
10700869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
10710869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
10720869b6fdSMahesh Salgaonkar 
10730869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
10740869b6fdSMahesh Salgaonkar }
10750869b6fdSMahesh Salgaonkar 
10763a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(unknown_exception)
107714cf11afSPaul Mackerras {
107814cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
107914cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
108014cf11afSPaul Mackerras 
1081e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
108214cf11afSPaul Mackerras }
108314cf11afSPaul Mackerras 
10843a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(unknown_async_exception)
10856c6aee00SNicholas Piggin {
10866c6aee00SNicholas Piggin 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
10876c6aee00SNicholas Piggin 	       regs->nip, regs->msr, regs->trap);
10886c6aee00SNicholas Piggin 
10896c6aee00SNicholas Piggin 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
10906c6aee00SNicholas Piggin }
10916c6aee00SNicholas Piggin 
10923db8aa10SNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(unknown_nmi_exception)
10933db8aa10SNicholas Piggin {
10943db8aa10SNicholas Piggin 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
10953db8aa10SNicholas Piggin 	       regs->nip, regs->msr, regs->trap);
10963db8aa10SNicholas Piggin 
10973db8aa10SNicholas Piggin 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
10983db8aa10SNicholas Piggin 
10993db8aa10SNicholas Piggin 	return 0;
11003db8aa10SNicholas Piggin }
11013db8aa10SNicholas Piggin 
11023a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(instruction_breakpoint_exception)
110314cf11afSPaul Mackerras {
110414cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
110514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1106540d4d34SNicholas Piggin 		return;
110714cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
1108540d4d34SNicholas Piggin 		return;
110914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
111014cf11afSPaul Mackerras }
111114cf11afSPaul Mackerras 
11123a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(RunModeException)
111314cf11afSPaul Mackerras {
1114e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
111514cf11afSPaul Mackerras }
111614cf11afSPaul Mackerras 
111701fcac8eSChristophe Leroy static void __single_step_exception(struct pt_regs *regs)
111814cf11afSPaul Mackerras {
11192538c2d0SK.Prasad 	clear_single_step(regs);
11200e524e76SMatt Evans 	clear_br_trace(regs);
112114cf11afSPaul Mackerras 
11226cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
11236cc89badSNaveen N. Rao 		return;
11246cc89badSNaveen N. Rao 
112514cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
112614cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1127540d4d34SNicholas Piggin 		return;
112814cf11afSPaul Mackerras 	if (debugger_sstep(regs))
1129540d4d34SNicholas Piggin 		return;
113014cf11afSPaul Mackerras 
113114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
113214cf11afSPaul Mackerras }
113314cf11afSPaul Mackerras 
113401fcac8eSChristophe Leroy DEFINE_INTERRUPT_HANDLER(single_step_exception)
113501fcac8eSChristophe Leroy {
113601fcac8eSChristophe Leroy 	__single_step_exception(regs);
113701fcac8eSChristophe Leroy }
113801fcac8eSChristophe Leroy 
113914cf11afSPaul Mackerras /*
114014cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
114114cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
114214cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
114314cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
114414cf11afSPaul Mackerras  */
11458dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
114614cf11afSPaul Mackerras {
11472538c2d0SK.Prasad 	if (single_stepping(regs))
114801fcac8eSChristophe Leroy 		__single_step_exception(regs);
114914cf11afSPaul Mackerras }
115014cf11afSPaul Mackerras 
11515fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
1152dc1c1ca3SStephen Rothwell {
1153aeb1c0f6SEric W. Biederman 	int ret = FPE_FLTUNK;
1154dc1c1ca3SStephen Rothwell 
1155dc1c1ca3SStephen Rothwell 	/* Invalid operation */
1156dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
11575fad293bSKumar Gala 		ret = FPE_FLTINV;
1158dc1c1ca3SStephen Rothwell 
1159dc1c1ca3SStephen Rothwell 	/* Overflow */
1160dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
11615fad293bSKumar Gala 		ret = FPE_FLTOVF;
1162dc1c1ca3SStephen Rothwell 
1163dc1c1ca3SStephen Rothwell 	/* Underflow */
1164dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
11655fad293bSKumar Gala 		ret = FPE_FLTUND;
1166dc1c1ca3SStephen Rothwell 
1167dc1c1ca3SStephen Rothwell 	/* Divide by zero */
1168dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
11695fad293bSKumar Gala 		ret = FPE_FLTDIV;
1170dc1c1ca3SStephen Rothwell 
1171dc1c1ca3SStephen Rothwell 	/* Inexact result */
1172dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
11735fad293bSKumar Gala 		ret = FPE_FLTRES;
11745fad293bSKumar Gala 
11755fad293bSKumar Gala 	return ret;
11765fad293bSKumar Gala }
11775fad293bSKumar Gala 
11785fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
11795fad293bSKumar Gala {
11805fad293bSKumar Gala 	int code = 0;
11815fad293bSKumar Gala 
11825fad293bSKumar Gala 	flush_fp_to_thread(current);
11835fad293bSKumar Gala 
1184b6254cedSChristophe Leroy #ifdef CONFIG_PPC_FPU_REGS
1185de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
1186b6254cedSChristophe Leroy #endif
1187dc1c1ca3SStephen Rothwell 
1188dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
1189dc1c1ca3SStephen Rothwell }
1190dc1c1ca3SStephen Rothwell 
1191dc1c1ca3SStephen Rothwell /*
1192dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
119314cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
119414cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
119514cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
119614cf11afSPaul Mackerras  *
119714cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
119814cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
119914cf11afSPaul Mackerras  * bits is faster and easier.
120086417780SPaul Mackerras  *
120114cf11afSPaul Mackerras  */
120214cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
120314cf11afSPaul Mackerras {
120414cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
120514cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
120614cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
120714cf11afSPaul Mackerras 	u32 num_bytes;
120814cf11afSPaul Mackerras 	unsigned long EA;
120914cf11afSPaul Mackerras 	int pos = 0;
121014cf11afSPaul Mackerras 
121114cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
121216c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
121314cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
121414cf11afSPaul Mackerras 			return -EINVAL;
121514cf11afSPaul Mackerras 
121614cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
121714cf11afSPaul Mackerras 
121816c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
121916c57b36SKumar Gala 		case PPC_INST_LSWX:
122016c57b36SKumar Gala 		case PPC_INST_STSWX:
122114cf11afSPaul Mackerras 			EA += NB_RB;
122214cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
122314cf11afSPaul Mackerras 			break;
122416c57b36SKumar Gala 		case PPC_INST_LSWI:
122516c57b36SKumar Gala 		case PPC_INST_STSWI:
122614cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
122714cf11afSPaul Mackerras 			break;
122814cf11afSPaul Mackerras 		default:
122914cf11afSPaul Mackerras 			return -EINVAL;
123014cf11afSPaul Mackerras 	}
123114cf11afSPaul Mackerras 
123214cf11afSPaul Mackerras 	while (num_bytes != 0)
123314cf11afSPaul Mackerras 	{
123414cf11afSPaul Mackerras 		u8 val;
123514cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
123614cf11afSPaul Mackerras 
123780aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
123880aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
123980aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
124080aa0fb4SJames Yang 
124116c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
124216c57b36SKumar Gala 			case PPC_INST_LSWX:
124316c57b36SKumar Gala 			case PPC_INST_LSWI:
124414cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
124514cf11afSPaul Mackerras 					return -EFAULT;
124614cf11afSPaul Mackerras 				/* first time updating this reg,
124714cf11afSPaul Mackerras 				 * zero it out */
124814cf11afSPaul Mackerras 				if (pos == 0)
124914cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
125014cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
125114cf11afSPaul Mackerras 				break;
125216c57b36SKumar Gala 			case PPC_INST_STSWI:
125316c57b36SKumar Gala 			case PPC_INST_STSWX:
125414cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
125514cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
125614cf11afSPaul Mackerras 					return -EFAULT;
125714cf11afSPaul Mackerras 				break;
125814cf11afSPaul Mackerras 		}
125914cf11afSPaul Mackerras 		/* move EA to next address */
126014cf11afSPaul Mackerras 		EA += 1;
126114cf11afSPaul Mackerras 		num_bytes--;
126214cf11afSPaul Mackerras 
126314cf11afSPaul Mackerras 		/* manage our position within the register */
126414cf11afSPaul Mackerras 		if (++pos == 4) {
126514cf11afSPaul Mackerras 			pos = 0;
126614cf11afSPaul Mackerras 			if (++rT == 32)
126714cf11afSPaul Mackerras 				rT = 0;
126814cf11afSPaul Mackerras 		}
126914cf11afSPaul Mackerras 	}
127014cf11afSPaul Mackerras 
127114cf11afSPaul Mackerras 	return 0;
127214cf11afSPaul Mackerras }
127314cf11afSPaul Mackerras 
1274c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
1275c3412dcbSWill Schmidt {
1276c3412dcbSWill Schmidt 	u32 ra,rs;
1277c3412dcbSWill Schmidt 	unsigned long tmp;
1278c3412dcbSWill Schmidt 
1279c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
1280c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
1281c3412dcbSWill Schmidt 
1282c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
1283c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
1284c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
1285c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1286c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
1287c3412dcbSWill Schmidt 
1288c3412dcbSWill Schmidt 	return 0;
1289c3412dcbSWill Schmidt }
1290c3412dcbSWill Schmidt 
1291c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
1292c1469f13SKumar Gala {
1293c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
1294c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
1295c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
1296c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
1297c1469f13SKumar Gala 	u8 bit;
1298c1469f13SKumar Gala 	unsigned long tmp;
1299c1469f13SKumar Gala 
1300c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
1301c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
1302c1469f13SKumar Gala 
1303c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1304c1469f13SKumar Gala 
1305c1469f13SKumar Gala 	return 0;
1306c1469f13SKumar Gala }
1307c1469f13SKumar Gala 
13086ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
13096ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
13106ce6c629SMichael Neuling {
13116ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
13126ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
13136ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
13146ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
13156ce6c629SMichael Neuling 	 */
13166ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
13176ce6c629SMichael Neuling 		tm_enable();
13186ce6c629SMichael Neuling 		tm_abort(cause);
13196ce6c629SMichael Neuling 		return true;
13206ce6c629SMichael Neuling 	}
13216ce6c629SMichael Neuling 	return false;
13226ce6c629SMichael Neuling }
13236ce6c629SMichael Neuling #else
13246ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
13256ce6c629SMichael Neuling {
13266ce6c629SMichael Neuling 	return false;
13276ce6c629SMichael Neuling }
13286ce6c629SMichael Neuling #endif
13296ce6c629SMichael Neuling 
133014cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
133114cf11afSPaul Mackerras {
133214cf11afSPaul Mackerras 	u32 instword;
133314cf11afSPaul Mackerras 	u32 rd;
133414cf11afSPaul Mackerras 
13354288e343SAnton Blanchard 	if (!user_mode(regs))
133614cf11afSPaul Mackerras 		return -EINVAL;
133714cf11afSPaul Mackerras 
133814cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
133914cf11afSPaul Mackerras 		return -EFAULT;
134014cf11afSPaul Mackerras 
134114cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
134216c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1343eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
134414cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
134514cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
134614cf11afSPaul Mackerras 		return 0;
134714cf11afSPaul Mackerras 	}
134814cf11afSPaul Mackerras 
134914cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
135080947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1351eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
135214cf11afSPaul Mackerras 		return 0;
135380947e7cSGeert Uytterhoeven 	}
135414cf11afSPaul Mackerras 
135514cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
135616c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
135786417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
135814cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
135914cf11afSPaul Mackerras 
1360eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
136114cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
136214cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
136314cf11afSPaul Mackerras 		return 0;
136414cf11afSPaul Mackerras 	}
136514cf11afSPaul Mackerras 
136614cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
136780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
13686ce6c629SMichael Neuling 		if (tm_abort_check(regs,
13696ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
13706ce6c629SMichael Neuling 			return -EINVAL;
1371eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
137214cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
137380947e7cSGeert Uytterhoeven 	}
137414cf11afSPaul Mackerras 
1375c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
137616c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1377eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1378c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1379c3412dcbSWill Schmidt 	}
1380c3412dcbSWill Schmidt 
1381c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
138216c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1383eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1384c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1385c1469f13SKumar Gala 	}
1386c1469f13SKumar Gala 
13879863c28aSJames Yang 	/* Emulate sync instruction variants */
13889863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
13899863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
13909863c28aSJames Yang 		asm volatile("sync");
13919863c28aSJames Yang 		return 0;
13929863c28aSJames Yang 	}
13939863c28aSJames Yang 
1394efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1395efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
139673d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
139773d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
139873d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
139973d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1400efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1401efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1402efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1403efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1404efcac658SAlexey Kardashevskiy 		return 0;
1405efcac658SAlexey Kardashevskiy 	}
1406efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
140773d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
140873d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
140973d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
141073d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1411efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1412efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1413efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
141400ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1415efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
141600ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1417efcac658SAlexey Kardashevskiy 		return 0;
1418efcac658SAlexey Kardashevskiy 	}
1419efcac658SAlexey Kardashevskiy #endif
1420efcac658SAlexey Kardashevskiy 
142114cf11afSPaul Mackerras 	return -EINVAL;
142214cf11afSPaul Mackerras }
142314cf11afSPaul Mackerras 
142473c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
142514cf11afSPaul Mackerras {
142673c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
142714cf11afSPaul Mackerras }
142814cf11afSPaul Mackerras 
14293a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
14303a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
14313a3b5aa6SKevin Hao {
14323a3b5aa6SKevin Hao 	int ret;
14333a3b5aa6SKevin Hao 
14343a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
14353a3b5aa6SKevin Hao 	if (ret >= 0)
14363a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
14373a3b5aa6SKevin Hao 
14383a3b5aa6SKevin Hao 	switch (ret) {
14393a3b5aa6SKevin Hao 	case 0:
14403a3b5aa6SKevin Hao 		emulate_single_step(regs);
14413a3b5aa6SKevin Hao 		return 0;
14423a3b5aa6SKevin Hao 	case 1: {
14433a3b5aa6SKevin Hao 			int code = 0;
1444de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
14453a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
14463a3b5aa6SKevin Hao 			return 0;
14473a3b5aa6SKevin Hao 		}
14483a3b5aa6SKevin Hao 	case -EFAULT:
14493a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
14503a3b5aa6SKevin Hao 		return 0;
14513a3b5aa6SKevin Hao 	}
14523a3b5aa6SKevin Hao 
14533a3b5aa6SKevin Hao 	return -1;
14543a3b5aa6SKevin Hao }
14553a3b5aa6SKevin Hao #else
14563a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
14573a3b5aa6SKevin Hao #endif
14583a3b5aa6SKevin Hao 
1459fd3f1e0fSNicholas Piggin static void do_program_check(struct pt_regs *regs)
146014cf11afSPaul Mackerras {
146114cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
146214cf11afSPaul Mackerras 
1463aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
146404903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
146514cf11afSPaul Mackerras 
146614cf11afSPaul Mackerras 	if (reason & REASON_FP) {
146714cf11afSPaul Mackerras 		/* IEEE FP exception */
1468dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1469fd3f1e0fSNicholas Piggin 		return;
14708dad3f92SPaul Mackerras 	}
14718dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1472a4c3f909SBalbir Singh 		unsigned long bugaddr;
1473ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1474ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1475ba797b28SJason Wessel 		if (debugger_bpt(regs))
1476fd3f1e0fSNicholas Piggin 			return;
1477ba797b28SJason Wessel 
14786cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
1479fd3f1e0fSNicholas Piggin 			return;
14806cc89badSNaveen N. Rao 
148114cf11afSPaul Mackerras 		/* trap exception */
1482dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1483dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1484fd3f1e0fSNicholas Piggin 			return;
148573c9ceabSJeremy Fitzhardinge 
1486a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1487a4c3f909SBalbir Singh 		/*
1488a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1489a4c3f909SBalbir Singh 		 */
1490a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1491a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1492a4c3f909SBalbir Singh 
149373c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1494a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
14951e688dd2SChristophe Leroy 			const struct exception_table_entry *entry;
14961e688dd2SChristophe Leroy 
14971e688dd2SChristophe Leroy 			entry = search_exception_tables(bugaddr);
14981e688dd2SChristophe Leroy 			if (entry) {
14991e688dd2SChristophe Leroy 				regs_set_return_ip(regs, extable_fixup(entry) + regs->nip - bugaddr);
1500fd3f1e0fSNicholas Piggin 				return;
150114cf11afSPaul Mackerras 			}
15021e688dd2SChristophe Leroy 		}
15038dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1504fd3f1e0fSNicholas Piggin 		return;
15058dad3f92SPaul Mackerras 	}
1506bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1507bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1508bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1509bc2a9408SMichael Neuling 		 * This occurs when:
1510bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1511bc2a9408SMichael Neuling 		 *    transition in TM states.
1512bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1513bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1514bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1515bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1516632f0574SMichael Ellerman 		 *
1517632f0574SMichael Ellerman 		 * If usermode caused this, it's done something illegal and
1518bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1519bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1520bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1521bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1522bc2a9408SMichael Neuling 		 */
1523bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1524bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1525fd3f1e0fSNicholas Piggin 			return;
1526bc2a9408SMichael Neuling 		} else {
1527bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
152811be3958SBreno Leitao 			       "at %lx (msr 0x%lx) tm_scratch=%llx\n",
152911be3958SBreno Leitao 			       regs->nip, regs->msr, get_paca()->tm_scratch);
1530bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1531bc2a9408SMichael Neuling 		}
1532bc2a9408SMichael Neuling 	}
1533bc2a9408SMichael Neuling #endif
15348dad3f92SPaul Mackerras 
1535b3f6a459SMichael Ellerman 	/*
1536b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1537b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1538b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1539b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1540b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1541b3f6a459SMichael Ellerman 	 */
1542b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1543b3f6a459SMichael Ellerman 		goto sigill;
1544b3f6a459SMichael Ellerman 
1545e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
1546cd8a5673SPaul Mackerras 
154704903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
154804903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
154904903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
155004903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
155104903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
155204903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
15534e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
15544e63f8edSBenjamin Herrenschmidt 	 */
15553a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1556fd3f1e0fSNicholas Piggin 		return;
155704903a30SKumar Gala 
15588dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
15598dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
156014cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
156114cf11afSPaul Mackerras 		case 0:
156259dc5bfcSNicholas Piggin 			regs_add_return_ip(regs, 4);
156314cf11afSPaul Mackerras 			emulate_single_step(regs);
1564fd3f1e0fSNicholas Piggin 			return;
156514cf11afSPaul Mackerras 		case -EFAULT:
156614cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1567fd3f1e0fSNicholas Piggin 			return;
15688dad3f92SPaul Mackerras 		}
15698dad3f92SPaul Mackerras 	}
15708dad3f92SPaul Mackerras 
1571b3f6a459SMichael Ellerman sigill:
157214cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
157314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
157414cf11afSPaul Mackerras 	else
157514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1576ba12eedeSLi Zhong 
1577fd3f1e0fSNicholas Piggin }
1578fd3f1e0fSNicholas Piggin 
15793a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(program_check_exception)
1580fd3f1e0fSNicholas Piggin {
1581fd3f1e0fSNicholas Piggin 	do_program_check(regs);
158214cf11afSPaul Mackerras }
158314cf11afSPaul Mackerras 
1584bf593907SPaul Mackerras /*
1585bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1586bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1587bf593907SPaul Mackerras  */
15883a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(emulation_assist_interrupt)
1589bf593907SPaul Mackerras {
159059dc5bfcSNicholas Piggin 	regs_set_return_msr(regs, regs->msr | REASON_ILLEGAL);
1591fd3f1e0fSNicholas Piggin 	do_program_check(regs);
1592bf593907SPaul Mackerras }
1593bf593907SPaul Mackerras 
15943a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(alignment_exception)
159514cf11afSPaul Mackerras {
15964393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
15979409d2f9SJordan Niethe 	unsigned long  reason;
159814cf11afSPaul Mackerras 
1599e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
1600a3512b2dSBenjamin Herrenschmidt 
16019409d2f9SJordan Niethe 	reason = get_reason(regs);
16029409d2f9SJordan Niethe 	if (reason & REASON_BOUNDARY) {
16039409d2f9SJordan Niethe 		sig = SIGBUS;
16049409d2f9SJordan Niethe 		code = BUS_ADRALN;
16059409d2f9SJordan Niethe 		goto bad;
16069409d2f9SJordan Niethe 	}
16079409d2f9SJordan Niethe 
16086ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1609540d4d34SNicholas Piggin 		return;
16106ce6c629SMichael Neuling 
1611e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1612e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
161314cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
161414cf11afSPaul Mackerras 
161514cf11afSPaul Mackerras 	if (fixed == 1) {
16169409d2f9SJordan Niethe 		/* skip over emulated instruction */
161759dc5bfcSNicholas Piggin 		regs_add_return_ip(regs, inst_length(reason));
161814cf11afSPaul Mackerras 		emulate_single_step(regs);
1619540d4d34SNicholas Piggin 		return;
162014cf11afSPaul Mackerras 	}
162114cf11afSPaul Mackerras 
162214cf11afSPaul Mackerras 	/* Operand address was bad */
162314cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
16244393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
16254393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
16264393c4f6SBenjamin Herrenschmidt 	} else {
16274393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
16284393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
162914cf11afSPaul Mackerras 	}
16309409d2f9SJordan Niethe bad:
16314393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
16324393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
16334393c4f6SBenjamin Herrenschmidt 	else
16348458c628SNicholas Piggin 		bad_page_fault(regs, sig);
163514cf11afSPaul Mackerras }
163614cf11afSPaul Mackerras 
16373a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(stack_overflow_exception)
16383978eb78SChristophe Leroy {
16393978eb78SChristophe Leroy 	die("Kernel stack overflow", regs, SIGSEGV);
16403978eb78SChristophe Leroy }
16413978eb78SChristophe Leroy 
16423a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(kernel_fp_unavailable_exception)
1643dc1c1ca3SStephen Rothwell {
1644dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1645dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1646dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1647dc1c1ca3SStephen Rothwell }
1648dc1c1ca3SStephen Rothwell 
16493a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_unavailable_exception)
1650dc1c1ca3SStephen Rothwell {
1651dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1652dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1653dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1654dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1655540d4d34SNicholas Piggin 		return;
1656dc1c1ca3SStephen Rothwell 	}
16576c4841c2SAnton Blanchard 
1658dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1659dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1660dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1661dc1c1ca3SStephen Rothwell }
1662dc1c1ca3SStephen Rothwell 
16633a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(vsx_unavailable_exception)
1664ce48b210SMichael Neuling {
1665ce48b210SMichael Neuling 	if (user_mode(regs)) {
1666ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1667ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1668ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1669ce48b210SMichael Neuling 		return;
1670ce48b210SMichael Neuling 	}
1671ce48b210SMichael Neuling 
1672ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1673ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1674ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1675ce48b210SMichael Neuling }
1676ce48b210SMichael Neuling 
16772517617eSMichael Neuling #ifdef CONFIG_PPC64
1678172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1679172f7aaaSCyril Bur {
16805d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
16815d176f75SCyril Bur 	if (user_mode(regs)) {
16825d176f75SCyril Bur 		current->thread.load_tm++;
168359dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr | MSR_TM);
16845d176f75SCyril Bur 		tm_enable();
16855d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
16865d176f75SCyril Bur 		return;
16875d176f75SCyril Bur 	}
16885d176f75SCyril Bur #endif
1689172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1690172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1691172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1692172f7aaaSCyril Bur }
1693172f7aaaSCyril Bur 
16943a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(facility_unavailable_exception)
1695d0c0c9a1SMichael Neuling {
1696021424a1SMichael Ellerman 	static char *facility_strings[] = {
16972517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
16982517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
16992517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
17002517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
17012517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
17022517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
17032517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
17042517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1705794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
17069b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
17072aa6195eSAlistair Popple 		[FSCR_PREFIX_LG] = "PREFIX",
1708021424a1SMichael Ellerman 	};
17092517617eSMichael Neuling 	char *facility = "unknown";
1710021424a1SMichael Ellerman 	u64 value;
1711c952c1c4SAnshuman Khandual 	u32 instword, rd;
17122517617eSMichael Neuling 	u8 status;
17132517617eSMichael Neuling 	bool hv;
1714021424a1SMichael Ellerman 
17157153d4bfSXiongwei Song 	hv = (TRAP(regs) == INTERRUPT_H_FAC_UNAVAIL);
17162517617eSMichael Neuling 	if (hv)
1717b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
17182517617eSMichael Neuling 	else
17192517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
17202517617eSMichael Neuling 
17212517617eSMichael Neuling 	status = value >> 56;
1722709b973cSAnshuman Khandual 	if ((hv || status >= 2) &&
1723709b973cSAnshuman Khandual 	    (status < ARRAY_SIZE(facility_strings)) &&
1724709b973cSAnshuman Khandual 	    facility_strings[status])
1725709b973cSAnshuman Khandual 		facility = facility_strings[status];
1726709b973cSAnshuman Khandual 
1727709b973cSAnshuman Khandual 	/* We should not have taken this interrupt in kernel */
1728709b973cSAnshuman Khandual 	if (!user_mode(regs)) {
1729709b973cSAnshuman Khandual 		pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n",
1730709b973cSAnshuman Khandual 			 facility, status, regs->nip);
1731709b973cSAnshuman Khandual 		die("Unexpected facility unavailable exception", regs, SIGABRT);
1732709b973cSAnshuman Khandual 	}
1733709b973cSAnshuman Khandual 
1734e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
1735709b973cSAnshuman Khandual 
17362517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1737c952c1c4SAnshuman Khandual 		/*
1738c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1739c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1740c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1741c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1742c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1743c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1744c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1745c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1746c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1747c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1748c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1749c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1750c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1751c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
17522517617eSMichael Neuling 		 */
1753c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1754c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1755c952c1c4SAnshuman Khandual 			return;
1756c952c1c4SAnshuman Khandual 		}
1757c952c1c4SAnshuman Khandual 
1758c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1759c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1760c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1761c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1762c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
17632517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1764b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1765b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1766c952c1c4SAnshuman Khandual 		}
1767c952c1c4SAnshuman Khandual 
1768c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1769c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1770c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1771c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1772c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1773c952c1c4SAnshuman Khandual 				return;
1774c952c1c4SAnshuman Khandual 			}
177559dc5bfcSNicholas Piggin 			regs_add_return_ip(regs, 4);
1776c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1777c952c1c4SAnshuman Khandual 		}
17782517617eSMichael Neuling 		return;
1779b14b6260SMichael Ellerman 	}
1780b14b6260SMichael Ellerman 
1781172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1782172f7aaaSCyril Bur 		/*
1783172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1784172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1785172f7aaaSCyril Bur 		 *
1786172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1787172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1788172f7aaaSCyril Bur 		 * support.
1789172f7aaaSCyril Bur 		 *
1790172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1791172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1792172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1793172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1794172f7aaaSCyril Bur 		 */
1795172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1796172f7aaaSCyril Bur 			goto out;
1797172f7aaaSCyril Bur 
1798172f7aaaSCyril Bur 		tm_unavailable(regs);
1799172f7aaaSCyril Bur 		return;
1800172f7aaaSCyril Bur 	}
1801172f7aaaSCyril Bur 
180293c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
180393c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1804d0c0c9a1SMichael Neuling 
1805172f7aaaSCyril Bur out:
1806d0c0c9a1SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1807d0c0c9a1SMichael Neuling }
18082517617eSMichael Neuling #endif
1809d0c0c9a1SMichael Neuling 
1810f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1811f54db641SMichael Neuling 
18123a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(fp_unavailable_tm)
1813f54db641SMichael Neuling {
1814f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1815f54db641SMichael Neuling 
1816f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1817f54db641SMichael Neuling 		 regs->nip, regs->msr);
1818f54db641SMichael Neuling 
1819f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1820f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1821f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1822f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1823f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1824f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1825f54db641SMichael Neuling 	 */
1826d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
182796695563SBreno Leitao 
182896695563SBreno Leitao 	/*
182996695563SBreno Leitao 	 * Reclaim initially saved out bogus (lazy) FPRs to ckfp_state, and
183096695563SBreno Leitao 	 * then it was overwrite by the thr->fp_state by tm_reclaim_thread().
183196695563SBreno Leitao 	 *
183296695563SBreno Leitao 	 * At this point, ck{fp,vr}_state contains the exact values we want to
183396695563SBreno Leitao 	 * recheckpoint.
183496695563SBreno Leitao 	 */
1835f54db641SMichael Neuling 
1836f54db641SMichael Neuling 	/* Enable FP for the task: */
1837a7771176SCyril Bur 	current->thread.load_fp = 1;
1838f54db641SMichael Neuling 
183996695563SBreno Leitao 	/*
184096695563SBreno Leitao 	 * Recheckpoint all the checkpointed ckpt, ck{fp, vr}_state registers.
1841f54db641SMichael Neuling 	 */
1842eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1843f54db641SMichael Neuling }
1844f54db641SMichael Neuling 
18453a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_unavailable_tm)
1846f54db641SMichael Neuling {
1847f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1848f54db641SMichael Neuling 	 * the same way.
1849f54db641SMichael Neuling 	 */
1850f54db641SMichael Neuling 
1851f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1852f54db641SMichael Neuling 		 "MSR=%lx\n",
1853f54db641SMichael Neuling 		 regs->nip, regs->msr);
1854d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1855a7771176SCyril Bur 	current->thread.load_vec = 1;
1856eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1857f54db641SMichael Neuling 	current->thread.used_vr = 1;
18583ac8ff1cSPaul Mackerras }
18593ac8ff1cSPaul Mackerras 
18603a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(vsx_unavailable_tm)
1861f54db641SMichael Neuling {
1862f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1863f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1864f54db641SMichael Neuling 	 *
1865f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1866f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1867f54db641SMichael Neuling 	 */
1868f54db641SMichael Neuling 
1869f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1870f54db641SMichael Neuling 		 "MSR=%lx\n",
1871f54db641SMichael Neuling 		 regs->nip, regs->msr);
1872f54db641SMichael Neuling 
18733ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
18743ac8ff1cSPaul Mackerras 
1875f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1876d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1877f54db641SMichael Neuling 
1878a7771176SCyril Bur 	current->thread.load_vec = 1;
1879a7771176SCyril Bur 	current->thread.load_fp = 1;
18803ac8ff1cSPaul Mackerras 
1881eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1882f54db641SMichael Neuling }
1883f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1884f54db641SMichael Neuling 
18853a96570fSNicholas Piggin #ifdef CONFIG_PPC64
18863a96570fSNicholas Piggin DECLARE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi);
18873a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi)
1888dc1c1ca3SStephen Rothwell {
188969111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
189089713ed1SAnton Blanchard 
1891dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1892156b5371SNicholas Piggin 
18933a96570fSNicholas Piggin 	return 0;
18943a96570fSNicholas Piggin }
18953a96570fSNicholas Piggin #endif
18963a96570fSNicholas Piggin 
18973a96570fSNicholas Piggin DECLARE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async);
18983a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async)
1899156b5371SNicholas Piggin {
1900156b5371SNicholas Piggin 	__this_cpu_inc(irq_stat.pmu_irqs);
1901156b5371SNicholas Piggin 
1902156b5371SNicholas Piggin 	perf_irq(regs);
1903156b5371SNicholas Piggin }
1904156b5371SNicholas Piggin 
19053a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_RAW(performance_monitor_exception)
1906156b5371SNicholas Piggin {
1907156b5371SNicholas Piggin 	/*
1908156b5371SNicholas Piggin 	 * On 64-bit, if perf interrupts hit in a local_irq_disable
1909156b5371SNicholas Piggin 	 * (soft-masked) region, we consider them as NMIs. This is required to
1910156b5371SNicholas Piggin 	 * prevent hash faults on user addresses when reading callchains (and
1911156b5371SNicholas Piggin 	 * looks better from an irq tracing perspective).
1912156b5371SNicholas Piggin 	 */
1913156b5371SNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC64) && unlikely(arch_irq_disabled_regs(regs)))
1914156b5371SNicholas Piggin 		performance_monitor_exception_nmi(regs);
1915156b5371SNicholas Piggin 	else
1916156b5371SNicholas Piggin 		performance_monitor_exception_async(regs);
19173a96570fSNicholas Piggin 
19183a96570fSNicholas Piggin 	return 0;
1919dc1c1ca3SStephen Rothwell }
1920dc1c1ca3SStephen Rothwell 
1921172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
19223bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
19233bffb652SDave Kleikamp {
19243bffb652SDave Kleikamp 	int changed = 0;
19253bffb652SDave Kleikamp 	/*
19263bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
19273bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
19283bffb652SDave Kleikamp 	 */
19293bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
19303bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
19313bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
193251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
19333bffb652SDave Kleikamp #endif
193447355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status,
19353bffb652SDave Kleikamp 			     5);
19363bffb652SDave Kleikamp 		changed |= 0x01;
19373bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
19383bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
193947355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status,
19403bffb652SDave Kleikamp 			     6);
19413bffb652SDave Kleikamp 		changed |= 0x01;
19423bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
194351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
19443bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
194547355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status,
19463bffb652SDave Kleikamp 			     1);
19473bffb652SDave Kleikamp 		changed |= 0x01;
19483bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
194951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
195047355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status,
19513bffb652SDave Kleikamp 			     2);
19523bffb652SDave Kleikamp 		changed |= 0x01;
19533bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
195451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
19553bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
195647355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status,
19573bffb652SDave Kleikamp 			     3);
19583bffb652SDave Kleikamp 		changed |= 0x01;
19593bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
196051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
196147355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status,
19623bffb652SDave Kleikamp 			     4);
19633bffb652SDave Kleikamp 		changed |= 0x01;
19643bffb652SDave Kleikamp 	}
19653bffb652SDave Kleikamp 	/*
19663bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
19673bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
19683bffb652SDave Kleikamp 	 * back on or not.
19693bffb652SDave Kleikamp 	 */
197051ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
197151ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
197259dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr | MSR_DE);
19733bffb652SDave Kleikamp 	else
19743bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
197551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
19763bffb652SDave Kleikamp 
19773bffb652SDave Kleikamp 	if (changed & 0x01)
197851ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
19793bffb652SDave Kleikamp }
198014cf11afSPaul Mackerras 
19813a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(DebugException)
198214cf11afSPaul Mackerras {
1983755d6641SNicholas Piggin 	unsigned long debug_status = regs->dsisr;
1984755d6641SNicholas Piggin 
198551ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
19863bffb652SDave Kleikamp 
1987ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1988ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1989ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1990ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1991ec097c84SRoland McGrath 	 */
1992ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
199359dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr & ~MSR_DE);
1994ec097c84SRoland McGrath 
1995ec097c84SRoland McGrath 		/* Disable BT */
1996ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1997ec097c84SRoland McGrath 		/* Clear the BT event */
1998ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1999ec097c84SRoland McGrath 
2000ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
2001ec097c84SRoland McGrath 		if (user_mode(regs)) {
200251ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
200351ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
200459dc5bfcSNicholas Piggin 			regs_set_return_msr(regs, regs->msr | MSR_DE);
2005ec097c84SRoland McGrath 			return;
2006ec097c84SRoland McGrath 		}
2007ec097c84SRoland McGrath 
20086cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
20096cc89badSNaveen N. Rao 			return;
20106cc89badSNaveen N. Rao 
2011ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
2012ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
2013ec097c84SRoland McGrath 			return;
2014ec097c84SRoland McGrath 		}
2015ec097c84SRoland McGrath 		if (debugger_sstep(regs))
2016ec097c84SRoland McGrath 			return;
2017ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
201859dc5bfcSNicholas Piggin 		regs_set_return_msr(regs, regs->msr & ~MSR_DE);
2019f8279621SKumar Gala 
202014cf11afSPaul Mackerras 		/* Disable instruction completion */
202114cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
202214cf11afSPaul Mackerras 		/* Clear the instruction completion event */
202314cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
2024f8279621SKumar Gala 
20256cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
20266cc89badSNaveen N. Rao 			return;
20276cc89badSNaveen N. Rao 
2028f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
2029f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
203014cf11afSPaul Mackerras 			return;
203114cf11afSPaul Mackerras 		}
2032f8279621SKumar Gala 
2033f8279621SKumar Gala 		if (debugger_sstep(regs))
2034f8279621SKumar Gala 			return;
2035f8279621SKumar Gala 
20363bffb652SDave Kleikamp 		if (user_mode(regs)) {
203751ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
203851ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
203951ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
204059dc5bfcSNicholas Piggin 				regs_set_return_msr(regs, regs->msr | MSR_DE);
20413bffb652SDave Kleikamp 			else
20423bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
204351ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
20443bffb652SDave Kleikamp 		}
2045f8279621SKumar Gala 
2046f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
20473bffb652SDave Kleikamp 	} else
20483bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
204914cf11afSPaul Mackerras }
2050172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
205114cf11afSPaul Mackerras 
205214cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
20533a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_assist_exception)
205414cf11afSPaul Mackerras {
205514cf11afSPaul Mackerras 	int err;
205614cf11afSPaul Mackerras 
205714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
205814cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
205914cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
20608dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
206114cf11afSPaul Mackerras 	}
206214cf11afSPaul Mackerras 
2063dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
2064dc1c1ca3SStephen Rothwell 
2065eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
206614cf11afSPaul Mackerras 	err = emulate_altivec(regs);
206714cf11afSPaul Mackerras 	if (err == 0) {
206859dc5bfcSNicholas Piggin 		regs_add_return_ip(regs, 4); /* skip emulated instruction */
206914cf11afSPaul Mackerras 		emulate_single_step(regs);
207014cf11afSPaul Mackerras 		return;
207114cf11afSPaul Mackerras 	}
207214cf11afSPaul Mackerras 
207314cf11afSPaul Mackerras 	if (err == -EFAULT) {
207414cf11afSPaul Mackerras 		/* got an error reading the instruction */
207514cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
207614cf11afSPaul Mackerras 	} else {
207714cf11afSPaul Mackerras 		/* didn't recognize the instruction */
207814cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
207976462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
208014cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
2081de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
208214cf11afSPaul Mackerras 	}
208314cf11afSPaul Mackerras }
208414cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
208514cf11afSPaul Mackerras 
208614cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
20873a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(CacheLockingException)
208814cf11afSPaul Mackerras {
2089b4ced803SNicholas Piggin 	unsigned long error_code = regs->dsisr;
2090b4ced803SNicholas Piggin 
209114cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
209214cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
209314cf11afSPaul Mackerras 	 * something smarter
209414cf11afSPaul Mackerras 	 */
209514cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
209614cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
209714cf11afSPaul Mackerras 	return;
209814cf11afSPaul Mackerras }
209914cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
210014cf11afSPaul Mackerras 
210114cf11afSPaul Mackerras #ifdef CONFIG_SPE
21023a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SPEFloatingPointException)
210314cf11afSPaul Mackerras {
21046a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
210514cf11afSPaul Mackerras 	unsigned long spefscr;
210614cf11afSPaul Mackerras 	int fpexc_mode;
2107aeb1c0f6SEric W. Biederman 	int code = FPE_FLTUNK;
21086a800f36SLiu Yu 	int err;
21096a800f36SLiu Yu 
2110e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
2111ef429124SChristophe Leroy 
2112685659eeSyu liu 	flush_spe_to_thread(current);
211314cf11afSPaul Mackerras 
211414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
211514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
211614cf11afSPaul Mackerras 
211714cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
211814cf11afSPaul Mackerras 		code = FPE_FLTOVF;
211914cf11afSPaul Mackerras 	}
212014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
212114cf11afSPaul Mackerras 		code = FPE_FLTUND;
212214cf11afSPaul Mackerras 	}
212314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
212414cf11afSPaul Mackerras 		code = FPE_FLTDIV;
212514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
212614cf11afSPaul Mackerras 		code = FPE_FLTINV;
212714cf11afSPaul Mackerras 	}
212814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
212914cf11afSPaul Mackerras 		code = FPE_FLTRES;
213014cf11afSPaul Mackerras 
21316a800f36SLiu Yu 	err = do_spe_mathemu(regs);
21326a800f36SLiu Yu 	if (err == 0) {
213359dc5bfcSNicholas Piggin 		regs_add_return_ip(regs, 4); /* skip emulated instruction */
21346a800f36SLiu Yu 		emulate_single_step(regs);
213514cf11afSPaul Mackerras 		return;
213614cf11afSPaul Mackerras 	}
21376a800f36SLiu Yu 
21386a800f36SLiu Yu 	if (err == -EFAULT) {
21396a800f36SLiu Yu 		/* got an error reading the instruction */
21406a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
21416a800f36SLiu Yu 	} else if (err == -EINVAL) {
21426a800f36SLiu Yu 		/* didn't recognize the instruction */
21436a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
21446a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
21456a800f36SLiu Yu 	} else {
21466a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
21476a800f36SLiu Yu 	}
21486a800f36SLiu Yu 
21496a800f36SLiu Yu 	return;
21506a800f36SLiu Yu }
21516a800f36SLiu Yu 
21523a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SPEFloatingPointRoundException)
21536a800f36SLiu Yu {
21546a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
21556a800f36SLiu Yu 	int err;
21566a800f36SLiu Yu 
2157e6f8a6c8SNicholas Piggin 	interrupt_cond_local_irq_enable(regs);
2158ef429124SChristophe Leroy 
21596a800f36SLiu Yu 	preempt_disable();
21606a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
21616a800f36SLiu Yu 		giveup_spe(current);
21626a800f36SLiu Yu 	preempt_enable();
21636a800f36SLiu Yu 
216459dc5bfcSNicholas Piggin 	regs_add_return_ip(regs, -4);
21656a800f36SLiu Yu 	err = speround_handler(regs);
21666a800f36SLiu Yu 	if (err == 0) {
216759dc5bfcSNicholas Piggin 		regs_add_return_ip(regs, 4); /* skip emulated instruction */
21686a800f36SLiu Yu 		emulate_single_step(regs);
21696a800f36SLiu Yu 		return;
21706a800f36SLiu Yu 	}
21716a800f36SLiu Yu 
21726a800f36SLiu Yu 	if (err == -EFAULT) {
21736a800f36SLiu Yu 		/* got an error reading the instruction */
21746a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
21756a800f36SLiu Yu 	} else if (err == -EINVAL) {
21766a800f36SLiu Yu 		/* didn't recognize the instruction */
21776a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
21786a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
21796a800f36SLiu Yu 	} else {
2180aeb1c0f6SEric W. Biederman 		_exception(SIGFPE, regs, FPE_FLTUNK, regs->nip);
21816a800f36SLiu Yu 		return;
21826a800f36SLiu Yu 	}
21836a800f36SLiu Yu }
218414cf11afSPaul Mackerras #endif
218514cf11afSPaul Mackerras 
2186dc1c1ca3SStephen Rothwell /*
2187dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
2188dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
2189dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
2190dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
2191dc1c1ca3SStephen Rothwell  */
2192a58cbed6SChristophe Leroy void __noreturn unrecoverable_exception(struct pt_regs *regs)
2193dc1c1ca3SStephen Rothwell {
219451423a9cSChristophe Leroy 	pr_emerg("Unrecoverable exception %lx at %lx (msr=%lx)\n",
219551423a9cSChristophe Leroy 		 regs->trap, regs->nip, regs->msr);
2196dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
2197a58cbed6SChristophe Leroy 	/* die() should not return */
2198a58cbed6SChristophe Leroy 	for (;;)
2199a58cbed6SChristophe Leroy 		;
2200dc1c1ca3SStephen Rothwell }
2201dc1c1ca3SStephen Rothwell 
22021e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
220314cf11afSPaul Mackerras /*
220414cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
220514cf11afSPaul Mackerras  * spins until a reboot occurs
220614cf11afSPaul Mackerras  */
220714cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
220814cf11afSPaul Mackerras {
220914cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
221014cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
221114cf11afSPaul Mackerras 	return;
221214cf11afSPaul Mackerras }
221314cf11afSPaul Mackerras 
22143db8aa10SNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(WatchdogException)
221514cf11afSPaul Mackerras {
221614cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
221714cf11afSPaul Mackerras 	WatchdogHandler(regs);
22183db8aa10SNicholas Piggin 	return 0;
221914cf11afSPaul Mackerras }
222014cf11afSPaul Mackerras #endif
2221dc1c1ca3SStephen Rothwell 
2222dc1c1ca3SStephen Rothwell /*
2223dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
2224dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
2225dc1c1ca3SStephen Rothwell  */
22263a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(kernel_bad_stack)
2227dc1c1ca3SStephen Rothwell {
2228dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
2229dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
2230dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
2231dc1c1ca3SStephen Rothwell }
223214cf11afSPaul Mackerras 
223380947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
223480947e7cSGeert Uytterhoeven 
223580947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
223680947e7cSGeert Uytterhoeven 
223780947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
223880947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
223980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
224080947e7cSGeert Uytterhoeven #endif
224180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
224280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
224380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
224480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
224580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
224680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
224780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
224880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
224980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
225080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
2251a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
225280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
225380947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
225480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
225580947e7cSGeert Uytterhoeven #endif
225680947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
225780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
225880947e7cSGeert Uytterhoeven #endif
2259efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
2260efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
2261efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
2262f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
22635080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvw4x),
22645080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvh8x),
22655080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvd2x),
22665080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvb16x),
2267efcac658SAlexey Kardashevskiy #endif
226880947e7cSGeert Uytterhoeven };
226980947e7cSGeert Uytterhoeven 
227080947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
227180947e7cSGeert Uytterhoeven 
227280947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
227380947e7cSGeert Uytterhoeven {
227476462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
227580947e7cSGeert Uytterhoeven 			    type);
227680947e7cSGeert Uytterhoeven }
227780947e7cSGeert Uytterhoeven 
227880947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
227980947e7cSGeert Uytterhoeven {
2280860286cfSGreg Kroah-Hartman 	struct dentry *dir;
228180947e7cSGeert Uytterhoeven 	unsigned int i;
228280947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
228380947e7cSGeert Uytterhoeven 
228480947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
2285dbf77fedSAneesh Kumar K.V 				 arch_debugfs_dir);
228680947e7cSGeert Uytterhoeven 
2287860286cfSGreg Kroah-Hartman 	debugfs_create_u32("do_warn", 0644, dir, &ppc_warn_emulated);
228880947e7cSGeert Uytterhoeven 
2289860286cfSGreg Kroah-Hartman 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++)
2290860286cfSGreg Kroah-Hartman 		debugfs_create_u32(entries[i].name, 0644, dir,
229180947e7cSGeert Uytterhoeven 				   (u32 *)&entries[i].val.counter);
229280947e7cSGeert Uytterhoeven 
229380947e7cSGeert Uytterhoeven 	return 0;
229480947e7cSGeert Uytterhoeven }
229580947e7cSGeert Uytterhoeven 
229680947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
229780947e7cSGeert Uytterhoeven 
229880947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2299