xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 2aa6195e43b3740258ead93aee42ac719dd4c4b0)
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>
4014cf11afSPaul Mackerras 
4180947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4214cf11afSPaul Mackerras #include <asm/pgtable.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
447644d581SMichael Ellerman #include <asm/debugfs.h>
4514cf11afSPaul Mackerras #include <asm/io.h>
4686417780SPaul Mackerras #include <asm/machdep.h>
4786417780SPaul Mackerras #include <asm/rtas.h>
48f7f6f4feSDavid Gibson #include <asm/pmc.h>
4914cf11afSPaul Mackerras #include <asm/reg.h>
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
566ce6c629SMichael Neuling #include <asm/tm.h>
57dc1c1ca3SStephen Rothwell #endif
58c0ce7d08SDavid Wilder #include <asm/kexec.h>
5916c57b36SKumar Gala #include <asm/ppc-opcode.h>
60cce1f106SShaohui Xie #include <asm/rio.h>
61ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
62ae3a197eSDavid Howells #include <asm/switch_to.h>
63f54db641SMichael Neuling #include <asm/tm.h>
64ae3a197eSDavid Howells #include <asm/debug.h>
6542f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
66fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
674e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
686cc89badSNaveen N. Rao #include <asm/kprobes.h>
69a99b9c5eSMurilo Opsfelder Araujo #include <asm/stacktrace.h>
70de3c83c2SMathieu Malaterre #include <asm/nmi.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 	printk_safe_flush_on_panic();
17535adacd6SNicholas Piggin 	kmsg_dump(KMSG_DUMP_PANIC);
17635adacd6SNicholas Piggin 	bust_spinlocks(0);
17735adacd6SNicholas Piggin 	debug_locks_off();
178de6da1e8SFeng Tang 	console_flush_on_panic(CONSOLE_FLUSH_PENDING);
17935adacd6SNicholas Piggin }
18035adacd6SNicholas Piggin 
18103465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
182760ca4dcSAnton Blanchard {
183760ca4dcSAnton Blanchard 	int cpu;
18434c2a14fSanton@samba.org 	unsigned long flags;
18514cf11afSPaul Mackerras 
186293e4688Santon@samba.org 	oops_enter();
187293e4688Santon@samba.org 
188760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
189760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
190760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
191760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
192760ca4dcSAnton Blanchard 		if (cpu == die_owner)
193760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
194760ca4dcSAnton Blanchard 		else
195760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
196760ca4dcSAnton Blanchard 	}
197760ca4dcSAnton Blanchard 	die_nest_count++;
198760ca4dcSAnton Blanchard 	die_owner = cpu;
19914cf11afSPaul Mackerras 	console_verbose();
20014cf11afSPaul Mackerras 	bust_spinlocks(1);
2016031d9d9Santon@samba.org 	if (machine_is(powermac))
2026031d9d9Santon@samba.org 		pmac_backlight_unblank();
203760ca4dcSAnton Blanchard 	return flags;
20434c2a14fSanton@samba.org }
20503465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
2065474c120SMichael Hanselmann 
20703465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
208760ca4dcSAnton Blanchard 			       int signr)
209760ca4dcSAnton Blanchard {
21014cf11afSPaul Mackerras 	bust_spinlocks(0);
211373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
212760ca4dcSAnton Blanchard 	die_nest_count--;
21358154c8cSAnton Blanchard 	oops_exit();
21458154c8cSAnton Blanchard 	printk("\n");
2157458e8b2SNicholas Piggin 	if (!die_nest_count) {
216760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
2177458e8b2SNicholas Piggin 		die_owner = -1;
218760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
2197458e8b2SNicholas Piggin 	}
220760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
221cc532915SMichael Ellerman 
222d40b6768SNicholas Piggin 	/*
223d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
224d40b6768SNicholas Piggin 	 */
225d40b6768SNicholas Piggin 	if (TRAP(regs) == 0x100)
226d40b6768SNicholas Piggin 		return;
227d40b6768SNicholas Piggin 
228ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
229ebaeb5aeSMahesh Salgaonkar 
2304388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
231cc532915SMichael Ellerman 		crash_kexec(regs);
2329b00ac06SAnton Blanchard 
233760ca4dcSAnton Blanchard 	if (!signr)
234760ca4dcSAnton Blanchard 		return;
235760ca4dcSAnton Blanchard 
23658154c8cSAnton Blanchard 	/*
23758154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
23858154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
23958154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
24058154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
24158154c8cSAnton Blanchard 	 */
24258154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
24358154c8cSAnton Blanchard 	    is_global_init(current)) {
24458154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
24558154c8cSAnton Blanchard 	}
24658154c8cSAnton Blanchard 
247cea6a4baSHorms 	if (panic_on_oops)
248012c437dSHorms 		panic("Fatal exception");
249760ca4dcSAnton Blanchard 	do_exit(signr);
250760ca4dcSAnton Blanchard }
25103465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
252cea6a4baSHorms 
253d7e02f7bSAneesh Kumar K.V static char *get_mmu_str(void)
254d7e02f7bSAneesh Kumar K.V {
255d7e02f7bSAneesh Kumar K.V 	if (early_radix_enabled())
256d7e02f7bSAneesh Kumar K.V 		return " MMU=Radix";
257d7e02f7bSAneesh Kumar K.V 	if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE))
258d7e02f7bSAneesh Kumar K.V 		return " MMU=Hash";
259d7e02f7bSAneesh Kumar K.V 	return "";
260d7e02f7bSAneesh Kumar K.V }
261d7e02f7bSAneesh Kumar K.V 
26203465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
263760ca4dcSAnton Blanchard {
264760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2652e82ca3cSMichael Ellerman 
266d7e02f7bSAneesh Kumar K.V 	printk("%s PAGE_SIZE=%luK%s%s%s%s%s%s %s\n",
26778227443SMichael Ellerman 	       IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) ? "LE" : "BE",
268d7e02f7bSAneesh Kumar K.V 	       PAGE_SIZE / 1024, get_mmu_str(),
26978227443SMichael Ellerman 	       IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "",
27078227443SMichael Ellerman 	       IS_ENABLED(CONFIG_SMP) ? " SMP" : "",
27178227443SMichael Ellerman 	       IS_ENABLED(CONFIG_SMP) ? (" NR_CPUS=" __stringify(NR_CPUS)) : "",
27278227443SMichael Ellerman 	       debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "",
27378227443SMichael Ellerman 	       IS_ENABLED(CONFIG_NUMA) ? " NUMA" : "",
27478227443SMichael Ellerman 	       ppc_md.name ? ppc_md.name : "");
275760ca4dcSAnton Blanchard 
276760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
277760ca4dcSAnton Blanchard 		return 1;
278760ca4dcSAnton Blanchard 
279760ca4dcSAnton Blanchard 	print_modules();
280760ca4dcSAnton Blanchard 	show_regs(regs);
28114cf11afSPaul Mackerras 
28214cf11afSPaul Mackerras 	return 0;
28314cf11afSPaul Mackerras }
28403465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
28514cf11afSPaul Mackerras 
286760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
287760ca4dcSAnton Blanchard {
2886f44b20eSNicholas Piggin 	unsigned long flags;
289760ca4dcSAnton Blanchard 
290d40b6768SNicholas Piggin 	/*
291d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
292d40b6768SNicholas Piggin 	 */
293d40b6768SNicholas Piggin 	if (TRAP(regs) != 0x100) {
2946f44b20eSNicholas Piggin 		if (debugger(regs))
2956f44b20eSNicholas Piggin 			return;
296d40b6768SNicholas Piggin 	}
2976f44b20eSNicholas Piggin 
2986f44b20eSNicholas Piggin 	flags = oops_begin(regs);
299760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
300760ca4dcSAnton Blanchard 		err = 0;
301760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
302760ca4dcSAnton Blanchard }
30315770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
304760ca4dcSAnton Blanchard 
305efc463adSEric W. Biederman void user_single_step_report(struct pt_regs *regs)
30625baa35bSOleg Nesterov {
3072e1661d2SEric W. Biederman 	force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)regs->nip);
30825baa35bSOleg Nesterov }
30925baa35bSOleg Nesterov 
310658b0f92SMurilo Opsfelder Araujo static void show_signal_msg(int signr, struct pt_regs *regs, int code,
311658b0f92SMurilo Opsfelder Araujo 			    unsigned long addr)
31214cf11afSPaul Mackerras {
313997dd26cSMichael Ellerman 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
314997dd26cSMichael Ellerman 				      DEFAULT_RATELIMIT_BURST);
315997dd26cSMichael Ellerman 
316997dd26cSMichael Ellerman 	if (!show_unhandled_signals)
31735a52a10SMurilo Opsfelder Araujo 		return;
31835a52a10SMurilo Opsfelder Araujo 
31935a52a10SMurilo Opsfelder Araujo 	if (!unhandled_signal(current, signr))
32035a52a10SMurilo Opsfelder Araujo 		return;
32135a52a10SMurilo Opsfelder Araujo 
322997dd26cSMichael Ellerman 	if (!__ratelimit(&rs))
323997dd26cSMichael Ellerman 		return;
324997dd26cSMichael Ellerman 
3250f642d61SMurilo Opsfelder Araujo 	pr_info("%s[%d]: %s (%d) at %lx nip %lx lr %lx code %x",
3260f642d61SMurilo Opsfelder Araujo 		current->comm, current->pid, signame(signr), signr,
327d0c3d534SOlof Johansson 		addr, regs->nip, regs->link, code);
3280f642d61SMurilo Opsfelder Araujo 
3290f642d61SMurilo Opsfelder Araujo 	print_vma_addr(KERN_CONT " in ", regs->nip);
3300f642d61SMurilo Opsfelder Araujo 
3310f642d61SMurilo Opsfelder Araujo 	pr_cont("\n");
332a99b9c5eSMurilo Opsfelder Araujo 
333a99b9c5eSMurilo Opsfelder Araujo 	show_user_instructions(regs);
33414cf11afSPaul Mackerras }
335658b0f92SMurilo Opsfelder Araujo 
3362c44ce28SEric W. Biederman static bool exception_common(int signr, struct pt_regs *regs, int code,
3372c44ce28SEric W. Biederman 			      unsigned long addr)
338658b0f92SMurilo Opsfelder Araujo {
339658b0f92SMurilo Opsfelder Araujo 	if (!user_mode(regs)) {
340658b0f92SMurilo Opsfelder Araujo 		die("Exception in kernel mode", regs, signr);
3412c44ce28SEric W. Biederman 		return false;
342658b0f92SMurilo Opsfelder Araujo 	}
343658b0f92SMurilo Opsfelder Araujo 
344658b0f92SMurilo Opsfelder Araujo 	show_signal_msg(signr, regs, code, addr);
34514cf11afSPaul Mackerras 
346a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
3479f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
3489f2f79e3SBenjamin Herrenschmidt 
34941ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
350c5cc1f4dSThiago Jung Bauermann 
351c5cc1f4dSThiago Jung Bauermann 	/*
352c5cc1f4dSThiago Jung Bauermann 	 * Save all the pkey registers AMR/IAMR/UAMOR. Eg: Core dumps need
353c5cc1f4dSThiago Jung Bauermann 	 * to capture the content, if the task gets killed.
354c5cc1f4dSThiago Jung Bauermann 	 */
355c5cc1f4dSThiago Jung Bauermann 	thread_pkey_regs_save(&current->thread);
356c5cc1f4dSThiago Jung Bauermann 
3572c44ce28SEric W. Biederman 	return true;
3582c44ce28SEric W. Biederman }
3592c44ce28SEric W. Biederman 
3605d8fb8a5SEric W. Biederman void _exception_pkey(struct pt_regs *regs, unsigned long addr, int key)
3612c44ce28SEric W. Biederman {
3625d8fb8a5SEric W. Biederman 	if (!exception_common(SIGSEGV, regs, SEGV_PKUERR, addr))
3632c44ce28SEric W. Biederman 		return;
3642c44ce28SEric W. Biederman 
36577c70728SEric W. Biederman 	force_sig_pkuerr((void __user *) addr, key);
36614cf11afSPaul Mackerras }
36714cf11afSPaul Mackerras 
36899cd1302SRam Pai void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
36999cd1302SRam Pai {
370c1c7c85cSEric W. Biederman 	if (!exception_common(signr, regs, code, addr))
371c1c7c85cSEric W. Biederman 		return;
372c1c7c85cSEric W. Biederman 
3732e1661d2SEric W. Biederman 	force_sig_fault(signr, code, (void __user *)addr);
37499cd1302SRam Pai }
37599cd1302SRam Pai 
376ccd47702SNicholas Piggin /*
377ccd47702SNicholas Piggin  * The interrupt architecture has a quirk in that the HV interrupts excluding
378ccd47702SNicholas Piggin  * the NMIs (0x100 and 0x200) do not clear MSR[RI] at entry. The first thing
379ccd47702SNicholas Piggin  * that an interrupt handler must do is save off a GPR into a scratch register,
380ccd47702SNicholas Piggin  * and all interrupts on POWERNV (HV=1) use the HSPRG1 register as scratch.
381ccd47702SNicholas Piggin  * Therefore an NMI can clobber an HV interrupt's live HSPRG1 without noticing
382ccd47702SNicholas Piggin  * that it is non-reentrant, which leads to random data corruption.
383ccd47702SNicholas Piggin  *
384ccd47702SNicholas Piggin  * The solution is for NMI interrupts in HV mode to check if they originated
385ccd47702SNicholas Piggin  * from these critical HV interrupt regions. If so, then mark them not
386ccd47702SNicholas Piggin  * recoverable.
387ccd47702SNicholas Piggin  *
388ccd47702SNicholas Piggin  * An alternative would be for HV NMIs to use SPRG for scratch to avoid the
389ccd47702SNicholas Piggin  * HSPRG1 clobber, however this would cause guest SPRG to be clobbered. Linux
390ccd47702SNicholas Piggin  * guests should always have MSR[RI]=0 when its scratch SPRG is in use, so
391ccd47702SNicholas Piggin  * that would work. However any other guest OS that may have the SPRG live
392ccd47702SNicholas Piggin  * and MSR[RI]=1 could encounter silent corruption.
393ccd47702SNicholas Piggin  *
394ccd47702SNicholas Piggin  * Builds that do not support KVM could take this second option to increase
395ccd47702SNicholas Piggin  * the recoverability of NMIs.
396ccd47702SNicholas Piggin  */
397ccd47702SNicholas Piggin void hv_nmi_check_nonrecoverable(struct pt_regs *regs)
398ccd47702SNicholas Piggin {
399ccd47702SNicholas Piggin #ifdef CONFIG_PPC_POWERNV
400ccd47702SNicholas Piggin 	unsigned long kbase = (unsigned long)_stext;
401ccd47702SNicholas Piggin 	unsigned long nip = regs->nip;
402ccd47702SNicholas Piggin 
403ccd47702SNicholas Piggin 	if (!(regs->msr & MSR_RI))
404ccd47702SNicholas Piggin 		return;
405ccd47702SNicholas Piggin 	if (!(regs->msr & MSR_HV))
406ccd47702SNicholas Piggin 		return;
407ccd47702SNicholas Piggin 	if (regs->msr & MSR_PR)
408ccd47702SNicholas Piggin 		return;
409ccd47702SNicholas Piggin 
410ccd47702SNicholas Piggin 	/*
411ccd47702SNicholas Piggin 	 * Now test if the interrupt has hit a range that may be using
412ccd47702SNicholas Piggin 	 * HSPRG1 without having RI=0 (i.e., an HSRR interrupt). The
413ccd47702SNicholas Piggin 	 * problem ranges all run un-relocated. Test real and virt modes
414ccd47702SNicholas Piggin 	 * at the same time by droping the high bit of the nip (virt mode
415ccd47702SNicholas Piggin 	 * entry points still have the +0x4000 offset).
416ccd47702SNicholas Piggin 	 */
417ccd47702SNicholas Piggin 	nip &= ~0xc000000000000000ULL;
418ccd47702SNicholas Piggin 	if ((nip >= 0x500 && nip < 0x600) || (nip >= 0x4500 && nip < 0x4600))
419ccd47702SNicholas Piggin 		goto nonrecoverable;
420ccd47702SNicholas Piggin 	if ((nip >= 0x980 && nip < 0xa00) || (nip >= 0x4980 && nip < 0x4a00))
421ccd47702SNicholas Piggin 		goto nonrecoverable;
422ccd47702SNicholas Piggin 	if ((nip >= 0xe00 && nip < 0xec0) || (nip >= 0x4e00 && nip < 0x4ec0))
423ccd47702SNicholas Piggin 		goto nonrecoverable;
424ccd47702SNicholas Piggin 	if ((nip >= 0xf80 && nip < 0xfa0) || (nip >= 0x4f80 && nip < 0x4fa0))
425ccd47702SNicholas Piggin 		goto nonrecoverable;
426bd3524feSNicholas Piggin 
427ccd47702SNicholas Piggin 	/* Trampoline code runs un-relocated so subtract kbase. */
428bd3524feSNicholas Piggin 	if (nip >= (unsigned long)(start_real_trampolines - kbase) &&
429bd3524feSNicholas Piggin 			nip < (unsigned long)(end_real_trampolines - kbase))
430ccd47702SNicholas Piggin 		goto nonrecoverable;
431bd3524feSNicholas Piggin 	if (nip >= (unsigned long)(start_virt_trampolines - kbase) &&
432bd3524feSNicholas Piggin 			nip < (unsigned long)(end_virt_trampolines - kbase))
433ccd47702SNicholas Piggin 		goto nonrecoverable;
434ccd47702SNicholas Piggin 	return;
435ccd47702SNicholas Piggin 
436ccd47702SNicholas Piggin nonrecoverable:
437ccd47702SNicholas Piggin 	regs->msr &= ~MSR_RI;
438ccd47702SNicholas Piggin #endif
439ccd47702SNicholas Piggin }
440ccd47702SNicholas Piggin 
44114cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
44214cf11afSPaul Mackerras {
443cbf2ba95SNicholas Piggin 	unsigned long hsrr0, hsrr1;
444cbf2ba95SNicholas Piggin 	bool nested = in_nmi();
445cbf2ba95SNicholas Piggin 	bool saved_hsrrs = false;
446bbbc8032SNicholas Piggin 	u8 ftrace_enabled = this_cpu_get_ftrace_enabled();
447bbbc8032SNicholas Piggin 
448bbbc8032SNicholas Piggin 	this_cpu_set_ftrace_enabled(0);
449cbf2ba95SNicholas Piggin 
4502b4f3ac5SNicholas Piggin 	/*
4512b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
4522b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
4532b4f3ac5SNicholas Piggin 	 */
4542b4f3ac5SNicholas Piggin 	if (!nested)
4552b4f3ac5SNicholas Piggin 		nmi_enter();
4562b4f3ac5SNicholas Piggin 
457cbf2ba95SNicholas Piggin 	/*
458cbf2ba95SNicholas Piggin 	 * System reset can interrupt code where HSRRs are live and MSR[RI]=1.
459cbf2ba95SNicholas Piggin 	 * The system reset interrupt itself may clobber HSRRs (e.g., to call
460cbf2ba95SNicholas Piggin 	 * OPAL), so save them here and restore them before returning.
461cbf2ba95SNicholas Piggin 	 *
462cbf2ba95SNicholas Piggin 	 * Machine checks don't need to save HSRRs, as the real mode handler
463cbf2ba95SNicholas Piggin 	 * is careful to avoid them, and the regular handler is not delivered
464cbf2ba95SNicholas Piggin 	 * as an NMI.
465cbf2ba95SNicholas Piggin 	 */
466cbf2ba95SNicholas Piggin 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
467cbf2ba95SNicholas Piggin 		hsrr0 = mfspr(SPRN_HSRR0);
468cbf2ba95SNicholas Piggin 		hsrr1 = mfspr(SPRN_HSRR1);
469cbf2ba95SNicholas Piggin 		saved_hsrrs = true;
470cbf2ba95SNicholas Piggin 	}
471cbf2ba95SNicholas Piggin 
472ccd47702SNicholas Piggin 	hv_nmi_check_nonrecoverable(regs);
473ccd47702SNicholas Piggin 
474ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
475ca41ad43SNicholas Piggin 
47614cf11afSPaul Mackerras 	/* See if any machine dependent calls */
477c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
478c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
479c4f3b52cSNicholas Piggin 			goto out;
480c902be71SArnd Bergmann 	}
48114cf11afSPaul Mackerras 
4824388c9b3SNicholas Piggin 	if (debugger(regs))
4834388c9b3SNicholas Piggin 		goto out;
4844388c9b3SNicholas Piggin 
485e7ca44edSGanesh Goudar 	kmsg_dump(KMSG_DUMP_OOPS);
4864388c9b3SNicholas Piggin 	/*
4874388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
4884388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
4894388c9b3SNicholas Piggin 	 * registered).
4904388c9b3SNicholas Piggin 	 */
4914388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
4924388c9b3SNicholas Piggin 
4934388c9b3SNicholas Piggin 	crash_kexec(regs);
4944388c9b3SNicholas Piggin 
4954388c9b3SNicholas Piggin 	/*
4964388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
4974388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
4984388c9b3SNicholas Piggin 	 * code.
4994388c9b3SNicholas Piggin 	 */
5004388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
5014388c9b3SNicholas Piggin 
5024388c9b3SNicholas Piggin 	/*
5034388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
5044388c9b3SNicholas Piggin 	 * panic.
5054388c9b3SNicholas Piggin 	 */
5064552d128SNicholas Piggin 	die("System Reset", regs, SIGABRT);
5074388c9b3SNicholas Piggin 
5084388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
5094388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5104388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
51114cf11afSPaul Mackerras 
512c4f3b52cSNicholas Piggin out:
513c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
514c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
515c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
516265d6e58SNicholas Piggin 		die("Unrecoverable nested System Reset", regs, SIGABRT);
517c4f3b52cSNicholas Piggin #endif
51814cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
51914cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
520265d6e58SNicholas Piggin 		die("Unrecoverable System Reset", regs, SIGABRT);
52114cf11afSPaul Mackerras 
522cbf2ba95SNicholas Piggin 	if (saved_hsrrs) {
523cbf2ba95SNicholas Piggin 		mtspr(SPRN_HSRR0, hsrr0);
524cbf2ba95SNicholas Piggin 		mtspr(SPRN_HSRR1, hsrr1);
525cbf2ba95SNicholas Piggin 	}
526cbf2ba95SNicholas Piggin 
5272b4f3ac5SNicholas Piggin 	if (!nested)
5282b4f3ac5SNicholas Piggin 		nmi_exit();
5292b4f3ac5SNicholas Piggin 
530bbbc8032SNicholas Piggin 	this_cpu_set_ftrace_enabled(ftrace_enabled);
531bbbc8032SNicholas Piggin 
53214cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
53314cf11afSPaul Mackerras }
5341e9b4507SMahesh Salgaonkar 
53514cf11afSPaul Mackerras /*
53614cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
53714cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
53814cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
53914cf11afSPaul Mackerras  * table.
54014cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
54114cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
54214cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
54314cf11afSPaul Mackerras  *  -- paulus.
54414cf11afSPaul Mackerras  */
54514cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
54614cf11afSPaul Mackerras {
54768a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
54814cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
54914cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
55014cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
55114cf11afSPaul Mackerras 
55214cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
55314cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
55414cf11afSPaul Mackerras 		/*
55514cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
55614cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
55714cf11afSPaul Mackerras 		 * As the address is in the exception table
55814cf11afSPaul Mackerras 		 * we should be able to read the instr there.
55914cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
56014cf11afSPaul Mackerras 		 * load or store.
56114cf11afSPaul Mackerras 		 */
562ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
56314cf11afSPaul Mackerras 			nip -= 2;
564ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
56514cf11afSPaul Mackerras 			--nip;
566ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
56714cf11afSPaul Mackerras 			unsigned int rb;
56814cf11afSPaul Mackerras 
56914cf11afSPaul Mackerras 			--nip;
57014cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
57114cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
57214cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
57314cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
57414cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
57561a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
57614cf11afSPaul Mackerras 			return 1;
57714cf11afSPaul Mackerras 		}
57814cf11afSPaul Mackerras 	}
57968a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
58014cf11afSPaul Mackerras 	return 0;
58114cf11afSPaul Mackerras }
58214cf11afSPaul Mackerras 
583172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
58414cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
58514cf11afSPaul Mackerras    is in the ESR. */
58614cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
58714cf11afSPaul Mackerras #define REASON_FP		ESR_FP
58814cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
58914cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
59014cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
59114cf11afSPaul Mackerras 
59214cf11afSPaul Mackerras /* single-step stuff */
59351ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
59451ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
5950e524e76SMatt Evans #define clear_br_trace(regs)	do {} while(0)
59614cf11afSPaul Mackerras #else
59714cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
59814cf11afSPaul Mackerras    exception is in the MSR. */
59914cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
600d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
601d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
602d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
603d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
604d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
60514cf11afSPaul Mackerras 
60614cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
60714cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
6080e524e76SMatt Evans #define clear_br_trace(regs)	((regs)->msr &= ~MSR_BE)
60914cf11afSPaul Mackerras #endif
61014cf11afSPaul Mackerras 
6110d0935b3SMichael Ellerman #if defined(CONFIG_E500)
612fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
613fe04b112SScott Wood {
614fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
615a4e89ffbSMatt Weber 	unsigned long pvr = mfspr(SPRN_PVR);
616fe04b112SScott Wood 	unsigned long reason = mcsr;
617fe04b112SScott Wood 	int recoverable = 1;
618fe04b112SScott Wood 
61982a9a480SScott Wood 	if (reason & MCSR_LD) {
620cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
621cce1f106SShaohui Xie 		if (recoverable == 1)
622cce1f106SShaohui Xie 			goto silent_out;
623cce1f106SShaohui Xie 	}
624cce1f106SShaohui Xie 
625fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
626fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
627fe04b112SScott Wood 
628fe04b112SScott Wood 	if (reason & MCSR_MCP)
629422123ccSChristophe Leroy 		pr_cont("Machine Check Signal\n");
630fe04b112SScott Wood 
631fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
632422123ccSChristophe Leroy 		pr_cont("Instruction Cache Parity Error\n");
633fe04b112SScott Wood 
634fe04b112SScott Wood 		/*
635fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
636fe04b112SScott Wood 		 */
637fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
638fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
639fe04b112SScott Wood 			;
640fe04b112SScott Wood 
641fe04b112SScott Wood 		/*
642fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
643fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
644fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
645fe04b112SScott Wood 		 */
646fe04b112SScott Wood 		reason &= ~MCSR_IF;
647fe04b112SScott Wood 	}
648fe04b112SScott Wood 
649fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
650422123ccSChristophe Leroy 		pr_cont("Data Cache Parity Error\n");
65137caf9f2SKumar Gala 
65237caf9f2SKumar Gala 		/*
65337caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
65437caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
65537caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
65637caf9f2SKumar Gala 		 */
657a4e89ffbSMatt Weber 		/* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
658a4e89ffbSMatt Weber 		 * is not implemented but L1 data cache always runs in write
659a4e89ffbSMatt Weber 		 * shadow mode. Hence on data cache parity errors HW will
660a4e89ffbSMatt Weber 		 * automatically invalidate the L1 Data Cache.
661a4e89ffbSMatt Weber 		 */
662a4e89ffbSMatt Weber 		if (PVR_VER(pvr) != PVR_VER_E6500) {
66337caf9f2SKumar Gala 			if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
664fe04b112SScott Wood 				recoverable = 0;
665fe04b112SScott Wood 		}
666a4e89ffbSMatt Weber 	}
667fe04b112SScott Wood 
668fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
669422123ccSChristophe Leroy 		pr_cont("Hit on multiple TLB entries\n");
670fe04b112SScott Wood 		recoverable = 0;
671fe04b112SScott Wood 	}
672fe04b112SScott Wood 
673fe04b112SScott Wood 	if (reason & MCSR_NMI)
674422123ccSChristophe Leroy 		pr_cont("Non-maskable interrupt\n");
675fe04b112SScott Wood 
676fe04b112SScott Wood 	if (reason & MCSR_IF) {
677422123ccSChristophe Leroy 		pr_cont("Instruction Fetch Error Report\n");
678fe04b112SScott Wood 		recoverable = 0;
679fe04b112SScott Wood 	}
680fe04b112SScott Wood 
681fe04b112SScott Wood 	if (reason & MCSR_LD) {
682422123ccSChristophe Leroy 		pr_cont("Load Error Report\n");
683fe04b112SScott Wood 		recoverable = 0;
684fe04b112SScott Wood 	}
685fe04b112SScott Wood 
686fe04b112SScott Wood 	if (reason & MCSR_ST) {
687422123ccSChristophe Leroy 		pr_cont("Store Error Report\n");
688fe04b112SScott Wood 		recoverable = 0;
689fe04b112SScott Wood 	}
690fe04b112SScott Wood 
691fe04b112SScott Wood 	if (reason & MCSR_LDG) {
692422123ccSChristophe Leroy 		pr_cont("Guarded Load Error Report\n");
693fe04b112SScott Wood 		recoverable = 0;
694fe04b112SScott Wood 	}
695fe04b112SScott Wood 
696fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
697422123ccSChristophe Leroy 		pr_cont("Simultaneous tlbsync operations\n");
698fe04b112SScott Wood 
699fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
700422123ccSChristophe Leroy 		pr_cont("Level 2 Cache Error\n");
701fe04b112SScott Wood 		recoverable = 0;
702fe04b112SScott Wood 	}
703fe04b112SScott Wood 
704fe04b112SScott Wood 	if (reason & MCSR_MAV) {
705fe04b112SScott Wood 		u64 addr;
706fe04b112SScott Wood 
707fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
708fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
709fe04b112SScott Wood 
710422123ccSChristophe Leroy 		pr_cont("Machine Check %s Address: %#llx\n",
711fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
712fe04b112SScott Wood 	}
713fe04b112SScott Wood 
714cce1f106SShaohui Xie silent_out:
715fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
716fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
717fe04b112SScott Wood }
718fe04b112SScott Wood 
71947c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
72047c0bd1aSBenjamin Herrenschmidt {
72142bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
72247c0bd1aSBenjamin Herrenschmidt 
723cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
724cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
725cce1f106SShaohui Xie 			return 1;
7264e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
7274e0e3435SHongtao Jia 			return 1;
728cce1f106SShaohui Xie 	}
729cce1f106SShaohui Xie 
73014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
73114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
73214cf11afSPaul Mackerras 
73314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
734422123ccSChristophe Leroy 		pr_cont("Machine Check Signal\n");
73514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
736422123ccSChristophe Leroy 		pr_cont("Instruction Cache Parity Error\n");
73714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
738422123ccSChristophe Leroy 		pr_cont("Data Cache Push Parity Error\n");
73914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
740422123ccSChristophe Leroy 		pr_cont("Data Cache Parity Error\n");
74114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
742422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Address Error\n");
74314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
744422123ccSChristophe Leroy 		pr_cont("Bus - Read Address Error\n");
74514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
746422123ccSChristophe Leroy 		pr_cont("Bus - Write Address Error\n");
74714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
748422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Data Error\n");
74914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
750422123ccSChristophe Leroy 		pr_cont("Bus - Read Data Bus Error\n");
75114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
752422123ccSChristophe Leroy 		pr_cont("Bus - Write Data Bus Error\n");
75314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
754422123ccSChristophe Leroy 		pr_cont("Bus - Instruction Parity Error\n");
75514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
756422123ccSChristophe Leroy 		pr_cont("Bus - Read Parity Error\n");
75747c0bd1aSBenjamin Herrenschmidt 
75847c0bd1aSBenjamin Herrenschmidt 	return 0;
75947c0bd1aSBenjamin Herrenschmidt }
7604490c06bSKumar Gala 
7614490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
7624490c06bSKumar Gala {
7634490c06bSKumar Gala 	return 0;
7644490c06bSKumar Gala }
76514cf11afSPaul Mackerras #elif defined(CONFIG_E200)
76647c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
76747c0bd1aSBenjamin Herrenschmidt {
76842bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
76947c0bd1aSBenjamin Herrenschmidt 
77014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
77114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
77214cf11afSPaul Mackerras 
77314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
774422123ccSChristophe Leroy 		pr_cont("Machine Check Signal\n");
77514cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
776422123ccSChristophe Leroy 		pr_cont("Cache Push Parity Error\n");
77714cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
778422123ccSChristophe Leroy 		pr_cont("Cache Parity Error\n");
77914cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
780422123ccSChristophe Leroy 		pr_cont("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
78114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
782422123ccSChristophe Leroy 		pr_cont("Bus - Read Bus Error on instruction fetch\n");
78314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
784422123ccSChristophe Leroy 		pr_cont("Bus - Read Bus Error on data load\n");
78514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
786422123ccSChristophe Leroy 		pr_cont("Bus - Write Bus Error on buffered store or cache line push\n");
78747c0bd1aSBenjamin Herrenschmidt 
78847c0bd1aSBenjamin Herrenschmidt 	return 0;
78947c0bd1aSBenjamin Herrenschmidt }
7907f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
79147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
79247c0bd1aSBenjamin Herrenschmidt {
79342bff234SMichael Ellerman 	unsigned long reason = regs->msr;
79447c0bd1aSBenjamin Herrenschmidt 
79514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
79614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
79714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
79814cf11afSPaul Mackerras 	case 0x80000:
799422123ccSChristophe Leroy 		pr_cont("Machine check signal\n");
80014cf11afSPaul Mackerras 		break;
80114cf11afSPaul Mackerras 	case 0:		/* for 601 */
80214cf11afSPaul Mackerras 	case 0x40000:
80314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
804422123ccSChristophe Leroy 		pr_cont("Transfer error ack signal\n");
80514cf11afSPaul Mackerras 		break;
80614cf11afSPaul Mackerras 	case 0x20000:
807422123ccSChristophe Leroy 		pr_cont("Data parity error signal\n");
80814cf11afSPaul Mackerras 		break;
80914cf11afSPaul Mackerras 	case 0x10000:
810422123ccSChristophe Leroy 		pr_cont("Address parity error signal\n");
81114cf11afSPaul Mackerras 		break;
81214cf11afSPaul Mackerras 	case 0x20000000:
813422123ccSChristophe Leroy 		pr_cont("L1 Data Cache error\n");
81414cf11afSPaul Mackerras 		break;
81514cf11afSPaul Mackerras 	case 0x40000000:
816422123ccSChristophe Leroy 		pr_cont("L1 Instruction Cache error\n");
81714cf11afSPaul Mackerras 		break;
81814cf11afSPaul Mackerras 	case 0x00100000:
819422123ccSChristophe Leroy 		pr_cont("L2 data cache parity error\n");
82014cf11afSPaul Mackerras 		break;
82114cf11afSPaul Mackerras 	default:
822422123ccSChristophe Leroy 		pr_cont("Unknown values in msr\n");
82314cf11afSPaul Mackerras 	}
82475918a4bSOlof Johansson 	return 0;
82575918a4bSOlof Johansson }
82647c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
82775918a4bSOlof Johansson 
82875918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
82975918a4bSOlof Johansson {
83075918a4bSOlof Johansson 	int recover = 0;
831116ac378SNicholas Piggin 	bool nested;
832116ac378SNicholas Piggin 
833116ac378SNicholas Piggin 	/*
834116ac378SNicholas Piggin 	 * BOOK3S_64 does not call this handler as a non-maskable interrupt
835116ac378SNicholas Piggin 	 * (it uses its own early real-mode handler to handle the MCE proper
836116ac378SNicholas Piggin 	 * and then raises irq_work to call this handler when interrupts are
837116ac378SNicholas Piggin 	 * enabled). Set nested = true for this case, which just makes it avoid
838116ac378SNicholas Piggin 	 * the nmi_enter/exit.
839116ac378SNicholas Piggin 	 */
840116ac378SNicholas Piggin 	if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) || in_nmi())
841116ac378SNicholas Piggin 		nested = true;
842116ac378SNicholas Piggin 	else
843116ac378SNicholas Piggin 		nested = false;
844b96672ddSNicholas Piggin 	if (!nested)
845b96672ddSNicholas Piggin 		nmi_enter();
84675918a4bSOlof Johansson 
84769111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
84889713ed1SAnton Blanchard 
849d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
850d93b0ac0SMahesh Salgaonkar 
85147c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
85247c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
85347c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
85447c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
85547c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
85647c0bd1aSBenjamin Herrenschmidt 	 */
85775918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
85875918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
85947c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
86047c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
86175918a4bSOlof Johansson 
86247c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
863ba12eedeSLi Zhong 		goto bail;
86475918a4bSOlof Johansson 
865a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
866ba12eedeSLi Zhong 		goto bail;
86775918a4bSOlof Johansson 
86875918a4bSOlof Johansson 	if (check_io_access(regs))
869ba12eedeSLi Zhong 		goto bail;
87075918a4bSOlof Johansson 
871daf00ae7SChristophe Leroy 	if (!nested)
872daf00ae7SChristophe Leroy 		nmi_exit();
873daf00ae7SChristophe Leroy 
874daf00ae7SChristophe Leroy 	die("Machine check", regs, SIGBUS);
875daf00ae7SChristophe Leroy 
8760bbea75cSChristophe Leroy 	/* Must die if the interrupt is not recoverable */
8770bbea75cSChristophe Leroy 	if (!(regs->msr & MSR_RI))
878265d6e58SNicholas Piggin 		die("Unrecoverable Machine check", regs, SIGBUS);
8790bbea75cSChristophe Leroy 
880daf00ae7SChristophe Leroy 	return;
881daf00ae7SChristophe Leroy 
882ba12eedeSLi Zhong bail:
883b96672ddSNicholas Piggin 	if (!nested)
884b96672ddSNicholas Piggin 		nmi_exit();
88514cf11afSPaul Mackerras }
88614cf11afSPaul Mackerras 
88714cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
88814cf11afSPaul Mackerras {
88914cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
89014cf11afSPaul Mackerras }
89114cf11afSPaul Mackerras 
8925080332cSMichael Neuling #ifdef CONFIG_VSX
8935080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs)
8945080332cSMichael Neuling {
8955080332cSMichael Neuling 	unsigned int ra, rb, t, i, sel, instr, rc;
8965080332cSMichael Neuling 	const void __user *addr;
8975080332cSMichael Neuling 	u8 vbuf[16], *vdst;
8985080332cSMichael Neuling 	unsigned long ea, msr, msr_mask;
8995080332cSMichael Neuling 	bool swap;
9005080332cSMichael Neuling 
9015080332cSMichael Neuling 	if (__get_user_inatomic(instr, (unsigned int __user *)regs->nip))
9025080332cSMichael Neuling 		return;
9035080332cSMichael Neuling 
9045080332cSMichael Neuling 	/*
9055080332cSMichael Neuling 	 * lxvb16x	opcode: 0x7c0006d8
9065080332cSMichael Neuling 	 * lxvd2x	opcode: 0x7c000698
9075080332cSMichael Neuling 	 * lxvh8x	opcode: 0x7c000658
9085080332cSMichael Neuling 	 * lxvw4x	opcode: 0x7c000618
9095080332cSMichael Neuling 	 */
9105080332cSMichael Neuling 	if ((instr & 0xfc00073e) != 0x7c000618) {
9115080332cSMichael Neuling 		pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
9125080332cSMichael Neuling 			 " instr=%08x\n",
9135080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9145080332cSMichael Neuling 			 regs->nip, instr);
9155080332cSMichael Neuling 		return;
9165080332cSMichael Neuling 	}
9175080332cSMichael Neuling 
9185080332cSMichael Neuling 	/* Grab vector registers into the task struct */
9195080332cSMichael Neuling 	msr = regs->msr; /* Grab msr before we flush the bits */
9205080332cSMichael Neuling 	flush_vsx_to_thread(current);
9215080332cSMichael Neuling 	enable_kernel_altivec();
9225080332cSMichael Neuling 
9235080332cSMichael Neuling 	/*
9245080332cSMichael Neuling 	 * Is userspace running with a different endian (this is rare but
9255080332cSMichael Neuling 	 * not impossible)
9265080332cSMichael Neuling 	 */
9275080332cSMichael Neuling 	swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
9285080332cSMichael Neuling 
9295080332cSMichael Neuling 	/* Decode the instruction */
9305080332cSMichael Neuling 	ra = (instr >> 16) & 0x1f;
9315080332cSMichael Neuling 	rb = (instr >> 11) & 0x1f;
9325080332cSMichael Neuling 	t = (instr >> 21) & 0x1f;
9335080332cSMichael Neuling 	if (instr & 1)
9345080332cSMichael Neuling 		vdst = (u8 *)&current->thread.vr_state.vr[t];
9355080332cSMichael Neuling 	else
9365080332cSMichael Neuling 		vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
9375080332cSMichael Neuling 
9385080332cSMichael Neuling 	/* Grab the vector address */
9395080332cSMichael Neuling 	ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
9405080332cSMichael Neuling 	if (is_32bit_task())
9415080332cSMichael Neuling 		ea &= 0xfffffffful;
9425080332cSMichael Neuling 	addr = (__force const void __user *)ea;
9435080332cSMichael Neuling 
9445080332cSMichael Neuling 	/* Check it */
94596d4f267SLinus Torvalds 	if (!access_ok(addr, 16)) {
9465080332cSMichael Neuling 		pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
9475080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
9485080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9495080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
9505080332cSMichael Neuling 		return;
9515080332cSMichael Neuling 	}
9525080332cSMichael Neuling 
9535080332cSMichael Neuling 	/* Read the vector */
9545080332cSMichael Neuling 	rc = 0;
9555080332cSMichael Neuling 	if ((unsigned long)addr & 0xfUL)
9565080332cSMichael Neuling 		/* unaligned case */
9575080332cSMichael Neuling 		rc = __copy_from_user_inatomic(vbuf, addr, 16);
9585080332cSMichael Neuling 	else
9595080332cSMichael Neuling 		__get_user_atomic_128_aligned(vbuf, addr, rc);
9605080332cSMichael Neuling 	if (rc) {
9615080332cSMichael Neuling 		pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
9625080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
9635080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9645080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
9655080332cSMichael Neuling 		return;
9665080332cSMichael Neuling 	}
9675080332cSMichael Neuling 
9685080332cSMichael Neuling 	pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
9695080332cSMichael Neuling 		 " instr=%08x addr=%016lx\n",
9705080332cSMichael Neuling 		 smp_processor_id(), current->comm, current->pid, regs->nip,
9715080332cSMichael Neuling 		 instr, (unsigned long) addr);
9725080332cSMichael Neuling 
9735080332cSMichael Neuling 	/* Grab instruction "selector" */
9745080332cSMichael Neuling 	sel = (instr >> 6) & 3;
9755080332cSMichael Neuling 
9765080332cSMichael Neuling 	/*
9775080332cSMichael Neuling 	 * Check to make sure the facility is actually enabled. This
9785080332cSMichael Neuling 	 * could happen if we get a false positive hit.
9795080332cSMichael Neuling 	 *
9805080332cSMichael Neuling 	 * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
9815080332cSMichael Neuling 	 * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
9825080332cSMichael Neuling 	 */
9835080332cSMichael Neuling 	msr_mask = MSR_VSX;
9845080332cSMichael Neuling 	if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
9855080332cSMichael Neuling 		msr_mask = MSR_VEC;
9865080332cSMichael Neuling 	if (!(msr & msr_mask)) {
9875080332cSMichael Neuling 		pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
9885080332cSMichael Neuling 			 " instr=%08x msr:%016lx\n",
9895080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
9905080332cSMichael Neuling 			 regs->nip, instr, msr);
9915080332cSMichael Neuling 		return;
9925080332cSMichael Neuling 	}
9935080332cSMichael Neuling 
9945080332cSMichael Neuling 	/* Do logging here before we modify sel based on endian */
9955080332cSMichael Neuling 	switch (sel) {
9965080332cSMichael Neuling 	case 0:	/* lxvw4x */
9975080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvw4x, regs);
9985080332cSMichael Neuling 		break;
9995080332cSMichael Neuling 	case 1: /* lxvh8x */
10005080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvh8x, regs);
10015080332cSMichael Neuling 		break;
10025080332cSMichael Neuling 	case 2: /* lxvd2x */
10035080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvd2x, regs);
10045080332cSMichael Neuling 		break;
10055080332cSMichael Neuling 	case 3: /* lxvb16x */
10065080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvb16x, regs);
10075080332cSMichael Neuling 		break;
10085080332cSMichael Neuling 	}
10095080332cSMichael Neuling 
10105080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__
10115080332cSMichael Neuling 	/*
10125080332cSMichael Neuling 	 * An LE kernel stores the vector in the task struct as an LE
10135080332cSMichael Neuling 	 * byte array (effectively swapping both the components and
10145080332cSMichael Neuling 	 * the content of the components). Those instructions expect
10155080332cSMichael Neuling 	 * the components to remain in ascending address order, so we
10165080332cSMichael Neuling 	 * swap them back.
10175080332cSMichael Neuling 	 *
10185080332cSMichael Neuling 	 * If we are running a BE user space, the expectation is that
10195080332cSMichael Neuling 	 * of a simple memcpy, so forcing the emulation to look like
10205080332cSMichael Neuling 	 * a lxvb16x should do the trick.
10215080332cSMichael Neuling 	 */
10225080332cSMichael Neuling 	if (swap)
10235080332cSMichael Neuling 		sel = 3;
10245080332cSMichael Neuling 
10255080332cSMichael Neuling 	switch (sel) {
10265080332cSMichael Neuling 	case 0:	/* lxvw4x */
10275080332cSMichael Neuling 		for (i = 0; i < 4; i++)
10285080332cSMichael Neuling 			((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
10295080332cSMichael Neuling 		break;
10305080332cSMichael Neuling 	case 1: /* lxvh8x */
10315080332cSMichael Neuling 		for (i = 0; i < 8; i++)
10325080332cSMichael Neuling 			((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
10335080332cSMichael Neuling 		break;
10345080332cSMichael Neuling 	case 2: /* lxvd2x */
10355080332cSMichael Neuling 		for (i = 0; i < 2; i++)
10365080332cSMichael Neuling 			((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
10375080332cSMichael Neuling 		break;
10385080332cSMichael Neuling 	case 3: /* lxvb16x */
10395080332cSMichael Neuling 		for (i = 0; i < 16; i++)
10405080332cSMichael Neuling 			vdst[i] = vbuf[15-i];
10415080332cSMichael Neuling 		break;
10425080332cSMichael Neuling 	}
10435080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */
10445080332cSMichael Neuling 	/* On a big endian kernel, a BE userspace only needs a memcpy */
10455080332cSMichael Neuling 	if (!swap)
10465080332cSMichael Neuling 		sel = 3;
10475080332cSMichael Neuling 
10485080332cSMichael Neuling 	/* Otherwise, we need to swap the content of the components */
10495080332cSMichael Neuling 	switch (sel) {
10505080332cSMichael Neuling 	case 0:	/* lxvw4x */
10515080332cSMichael Neuling 		for (i = 0; i < 4; i++)
10525080332cSMichael Neuling 			((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
10535080332cSMichael Neuling 		break;
10545080332cSMichael Neuling 	case 1: /* lxvh8x */
10555080332cSMichael Neuling 		for (i = 0; i < 8; i++)
10565080332cSMichael Neuling 			((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
10575080332cSMichael Neuling 		break;
10585080332cSMichael Neuling 	case 2: /* lxvd2x */
10595080332cSMichael Neuling 		for (i = 0; i < 2; i++)
10605080332cSMichael Neuling 			((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
10615080332cSMichael Neuling 		break;
10625080332cSMichael Neuling 	case 3: /* lxvb16x */
10635080332cSMichael Neuling 		memcpy(vdst, vbuf, 16);
10645080332cSMichael Neuling 		break;
10655080332cSMichael Neuling 	}
10665080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */
10675080332cSMichael Neuling 
10685080332cSMichael Neuling 	/* Go to next instruction */
10695080332cSMichael Neuling 	regs->nip += 4;
10705080332cSMichael Neuling }
10715080332cSMichael Neuling #endif /* CONFIG_VSX */
10725080332cSMichael Neuling 
10730869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
10740869b6fdSMahesh Salgaonkar {
10750869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
10760869b6fdSMahesh Salgaonkar 
10770869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
10780869b6fdSMahesh Salgaonkar 	irq_enter();
10790869b6fdSMahesh Salgaonkar 
10805080332cSMichael Neuling #ifdef CONFIG_VSX
10815080332cSMichael Neuling 	/* Real mode flagged P9 special emu is needed */
10825080332cSMichael Neuling 	if (local_paca->hmi_p9_special_emu) {
10835080332cSMichael Neuling 		local_paca->hmi_p9_special_emu = 0;
10845080332cSMichael Neuling 
10855080332cSMichael Neuling 		/*
10865080332cSMichael Neuling 		 * We don't want to take page faults while doing the
10875080332cSMichael Neuling 		 * emulation, we just replay the instruction if necessary.
10885080332cSMichael Neuling 		 */
10895080332cSMichael Neuling 		pagefault_disable();
10905080332cSMichael Neuling 		p9_hmi_special_emu(regs);
10915080332cSMichael Neuling 		pagefault_enable();
10925080332cSMichael Neuling 	}
10935080332cSMichael Neuling #endif /* CONFIG_VSX */
10945080332cSMichael Neuling 
10950869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
10960869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
10970869b6fdSMahesh Salgaonkar 
10980869b6fdSMahesh Salgaonkar 	irq_exit();
10990869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
11000869b6fdSMahesh Salgaonkar }
11010869b6fdSMahesh Salgaonkar 
1102dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
110314cf11afSPaul Mackerras {
1104ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1105ba12eedeSLi Zhong 
110614cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
110714cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
110814cf11afSPaul Mackerras 
1109e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
1110ba12eedeSLi Zhong 
1111ba12eedeSLi Zhong 	exception_exit(prev_state);
111214cf11afSPaul Mackerras }
111314cf11afSPaul Mackerras 
1114dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
111514cf11afSPaul Mackerras {
1116ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1117ba12eedeSLi Zhong 
111814cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
111914cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1120ba12eedeSLi Zhong 		goto bail;
112114cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
1122ba12eedeSLi Zhong 		goto bail;
112314cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1124ba12eedeSLi Zhong 
1125ba12eedeSLi Zhong bail:
1126ba12eedeSLi Zhong 	exception_exit(prev_state);
112714cf11afSPaul Mackerras }
112814cf11afSPaul Mackerras 
112914cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
113014cf11afSPaul Mackerras {
1131e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
113214cf11afSPaul Mackerras }
113314cf11afSPaul Mackerras 
113403465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
113514cf11afSPaul Mackerras {
1136ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1137ba12eedeSLi Zhong 
11382538c2d0SK.Prasad 	clear_single_step(regs);
11390e524e76SMatt Evans 	clear_br_trace(regs);
114014cf11afSPaul Mackerras 
11416cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
11426cc89badSNaveen N. Rao 		return;
11436cc89badSNaveen N. Rao 
114414cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
114514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1146ba12eedeSLi Zhong 		goto bail;
114714cf11afSPaul Mackerras 	if (debugger_sstep(regs))
1148ba12eedeSLi Zhong 		goto bail;
114914cf11afSPaul Mackerras 
115014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1151ba12eedeSLi Zhong 
1152ba12eedeSLi Zhong bail:
1153ba12eedeSLi Zhong 	exception_exit(prev_state);
115414cf11afSPaul Mackerras }
115503465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
115614cf11afSPaul Mackerras 
115714cf11afSPaul Mackerras /*
115814cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
115914cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
116014cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
116114cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
116214cf11afSPaul Mackerras  */
11638dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
116414cf11afSPaul Mackerras {
11652538c2d0SK.Prasad 	if (single_stepping(regs))
11662538c2d0SK.Prasad 		single_step_exception(regs);
116714cf11afSPaul Mackerras }
116814cf11afSPaul Mackerras 
11695fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
1170dc1c1ca3SStephen Rothwell {
1171aeb1c0f6SEric W. Biederman 	int ret = FPE_FLTUNK;
1172dc1c1ca3SStephen Rothwell 
1173dc1c1ca3SStephen Rothwell 	/* Invalid operation */
1174dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
11755fad293bSKumar Gala 		ret = FPE_FLTINV;
1176dc1c1ca3SStephen Rothwell 
1177dc1c1ca3SStephen Rothwell 	/* Overflow */
1178dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
11795fad293bSKumar Gala 		ret = FPE_FLTOVF;
1180dc1c1ca3SStephen Rothwell 
1181dc1c1ca3SStephen Rothwell 	/* Underflow */
1182dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
11835fad293bSKumar Gala 		ret = FPE_FLTUND;
1184dc1c1ca3SStephen Rothwell 
1185dc1c1ca3SStephen Rothwell 	/* Divide by zero */
1186dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
11875fad293bSKumar Gala 		ret = FPE_FLTDIV;
1188dc1c1ca3SStephen Rothwell 
1189dc1c1ca3SStephen Rothwell 	/* Inexact result */
1190dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
11915fad293bSKumar Gala 		ret = FPE_FLTRES;
11925fad293bSKumar Gala 
11935fad293bSKumar Gala 	return ret;
11945fad293bSKumar Gala }
11955fad293bSKumar Gala 
11965fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
11975fad293bSKumar Gala {
11985fad293bSKumar Gala 	int code = 0;
11995fad293bSKumar Gala 
12005fad293bSKumar Gala 	flush_fp_to_thread(current);
12015fad293bSKumar Gala 
1202de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
1203dc1c1ca3SStephen Rothwell 
1204dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
1205dc1c1ca3SStephen Rothwell }
1206dc1c1ca3SStephen Rothwell 
1207dc1c1ca3SStephen Rothwell /*
1208dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
120914cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
121014cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
121114cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
121214cf11afSPaul Mackerras  *
121314cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
121414cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
121514cf11afSPaul Mackerras  * bits is faster and easier.
121686417780SPaul Mackerras  *
121714cf11afSPaul Mackerras  */
121814cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
121914cf11afSPaul Mackerras {
122014cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
122114cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
122214cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
122314cf11afSPaul Mackerras 	u32 num_bytes;
122414cf11afSPaul Mackerras 	unsigned long EA;
122514cf11afSPaul Mackerras 	int pos = 0;
122614cf11afSPaul Mackerras 
122714cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
122816c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
122914cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
123014cf11afSPaul Mackerras 			return -EINVAL;
123114cf11afSPaul Mackerras 
123214cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
123314cf11afSPaul Mackerras 
123416c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
123516c57b36SKumar Gala 		case PPC_INST_LSWX:
123616c57b36SKumar Gala 		case PPC_INST_STSWX:
123714cf11afSPaul Mackerras 			EA += NB_RB;
123814cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
123914cf11afSPaul Mackerras 			break;
124016c57b36SKumar Gala 		case PPC_INST_LSWI:
124116c57b36SKumar Gala 		case PPC_INST_STSWI:
124214cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
124314cf11afSPaul Mackerras 			break;
124414cf11afSPaul Mackerras 		default:
124514cf11afSPaul Mackerras 			return -EINVAL;
124614cf11afSPaul Mackerras 	}
124714cf11afSPaul Mackerras 
124814cf11afSPaul Mackerras 	while (num_bytes != 0)
124914cf11afSPaul Mackerras 	{
125014cf11afSPaul Mackerras 		u8 val;
125114cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
125214cf11afSPaul Mackerras 
125380aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
125480aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
125580aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
125680aa0fb4SJames Yang 
125716c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
125816c57b36SKumar Gala 			case PPC_INST_LSWX:
125916c57b36SKumar Gala 			case PPC_INST_LSWI:
126014cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
126114cf11afSPaul Mackerras 					return -EFAULT;
126214cf11afSPaul Mackerras 				/* first time updating this reg,
126314cf11afSPaul Mackerras 				 * zero it out */
126414cf11afSPaul Mackerras 				if (pos == 0)
126514cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
126614cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
126714cf11afSPaul Mackerras 				break;
126816c57b36SKumar Gala 			case PPC_INST_STSWI:
126916c57b36SKumar Gala 			case PPC_INST_STSWX:
127014cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
127114cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
127214cf11afSPaul Mackerras 					return -EFAULT;
127314cf11afSPaul Mackerras 				break;
127414cf11afSPaul Mackerras 		}
127514cf11afSPaul Mackerras 		/* move EA to next address */
127614cf11afSPaul Mackerras 		EA += 1;
127714cf11afSPaul Mackerras 		num_bytes--;
127814cf11afSPaul Mackerras 
127914cf11afSPaul Mackerras 		/* manage our position within the register */
128014cf11afSPaul Mackerras 		if (++pos == 4) {
128114cf11afSPaul Mackerras 			pos = 0;
128214cf11afSPaul Mackerras 			if (++rT == 32)
128314cf11afSPaul Mackerras 				rT = 0;
128414cf11afSPaul Mackerras 		}
128514cf11afSPaul Mackerras 	}
128614cf11afSPaul Mackerras 
128714cf11afSPaul Mackerras 	return 0;
128814cf11afSPaul Mackerras }
128914cf11afSPaul Mackerras 
1290c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
1291c3412dcbSWill Schmidt {
1292c3412dcbSWill Schmidt 	u32 ra,rs;
1293c3412dcbSWill Schmidt 	unsigned long tmp;
1294c3412dcbSWill Schmidt 
1295c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
1296c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
1297c3412dcbSWill Schmidt 
1298c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
1299c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
1300c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
1301c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1302c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
1303c3412dcbSWill Schmidt 
1304c3412dcbSWill Schmidt 	return 0;
1305c3412dcbSWill Schmidt }
1306c3412dcbSWill Schmidt 
1307c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
1308c1469f13SKumar Gala {
1309c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
1310c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
1311c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
1312c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
1313c1469f13SKumar Gala 	u8 bit;
1314c1469f13SKumar Gala 	unsigned long tmp;
1315c1469f13SKumar Gala 
1316c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
1317c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
1318c1469f13SKumar Gala 
1319c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1320c1469f13SKumar Gala 
1321c1469f13SKumar Gala 	return 0;
1322c1469f13SKumar Gala }
1323c1469f13SKumar Gala 
13246ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
13256ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
13266ce6c629SMichael Neuling {
13276ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
13286ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
13296ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
13306ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
13316ce6c629SMichael Neuling 	 */
13326ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
13336ce6c629SMichael Neuling 		tm_enable();
13346ce6c629SMichael Neuling 		tm_abort(cause);
13356ce6c629SMichael Neuling 		return true;
13366ce6c629SMichael Neuling 	}
13376ce6c629SMichael Neuling 	return false;
13386ce6c629SMichael Neuling }
13396ce6c629SMichael Neuling #else
13406ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
13416ce6c629SMichael Neuling {
13426ce6c629SMichael Neuling 	return false;
13436ce6c629SMichael Neuling }
13446ce6c629SMichael Neuling #endif
13456ce6c629SMichael Neuling 
134614cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
134714cf11afSPaul Mackerras {
134814cf11afSPaul Mackerras 	u32 instword;
134914cf11afSPaul Mackerras 	u32 rd;
135014cf11afSPaul Mackerras 
13514288e343SAnton Blanchard 	if (!user_mode(regs))
135214cf11afSPaul Mackerras 		return -EINVAL;
135314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
135414cf11afSPaul Mackerras 
135514cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
135614cf11afSPaul Mackerras 		return -EFAULT;
135714cf11afSPaul Mackerras 
135814cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
135916c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1360eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
136114cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
136214cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
136314cf11afSPaul Mackerras 		return 0;
136414cf11afSPaul Mackerras 	}
136514cf11afSPaul Mackerras 
136614cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
136780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1368eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
136914cf11afSPaul Mackerras 		return 0;
137080947e7cSGeert Uytterhoeven 	}
137114cf11afSPaul Mackerras 
137214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
137316c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
137486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
137514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
137614cf11afSPaul Mackerras 
1377eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
137814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
137914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
138014cf11afSPaul Mackerras 		return 0;
138114cf11afSPaul Mackerras 	}
138214cf11afSPaul Mackerras 
138314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
138480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
13856ce6c629SMichael Neuling 		if (tm_abort_check(regs,
13866ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
13876ce6c629SMichael Neuling 			return -EINVAL;
1388eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
138914cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
139080947e7cSGeert Uytterhoeven 	}
139114cf11afSPaul Mackerras 
1392c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
139316c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1394eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1395c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1396c3412dcbSWill Schmidt 	}
1397c3412dcbSWill Schmidt 
1398c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
139916c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1400eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1401c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1402c1469f13SKumar Gala 	}
1403c1469f13SKumar Gala 
14049863c28aSJames Yang 	/* Emulate sync instruction variants */
14059863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
14069863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
14079863c28aSJames Yang 		asm volatile("sync");
14089863c28aSJames Yang 		return 0;
14099863c28aSJames Yang 	}
14109863c28aSJames Yang 
1411efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1412efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
141373d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
141473d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
141573d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
141673d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1417efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1418efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1419efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1420efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1421efcac658SAlexey Kardashevskiy 		return 0;
1422efcac658SAlexey Kardashevskiy 	}
1423efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
142473d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
142573d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
142673d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
142773d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1428efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1429efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1430efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
143100ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1432efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
143300ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1434efcac658SAlexey Kardashevskiy 		return 0;
1435efcac658SAlexey Kardashevskiy 	}
1436efcac658SAlexey Kardashevskiy #endif
1437efcac658SAlexey Kardashevskiy 
143814cf11afSPaul Mackerras 	return -EINVAL;
143914cf11afSPaul Mackerras }
144014cf11afSPaul Mackerras 
144173c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
144214cf11afSPaul Mackerras {
144373c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
144414cf11afSPaul Mackerras }
144514cf11afSPaul Mackerras 
14463a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
14473a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
14483a3b5aa6SKevin Hao {
14493a3b5aa6SKevin Hao 	int ret;
14503a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
14513a3b5aa6SKevin Hao 
14523a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
14533a3b5aa6SKevin Hao 	if (ret >= 0)
14543a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
14553a3b5aa6SKevin Hao 
14563a3b5aa6SKevin Hao 	switch (ret) {
14573a3b5aa6SKevin Hao 	case 0:
14583a3b5aa6SKevin Hao 		emulate_single_step(regs);
14593a3b5aa6SKevin Hao 		return 0;
14603a3b5aa6SKevin Hao 	case 1: {
14613a3b5aa6SKevin Hao 			int code = 0;
1462de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
14633a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
14643a3b5aa6SKevin Hao 			return 0;
14653a3b5aa6SKevin Hao 		}
14663a3b5aa6SKevin Hao 	case -EFAULT:
14673a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
14683a3b5aa6SKevin Hao 		return 0;
14693a3b5aa6SKevin Hao 	}
14703a3b5aa6SKevin Hao 
14713a3b5aa6SKevin Hao 	return -1;
14723a3b5aa6SKevin Hao }
14733a3b5aa6SKevin Hao #else
14743a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
14753a3b5aa6SKevin Hao #endif
14763a3b5aa6SKevin Hao 
147703465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
147814cf11afSPaul Mackerras {
1479ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
148014cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
148114cf11afSPaul Mackerras 
1482aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
148304903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
148414cf11afSPaul Mackerras 
148514cf11afSPaul Mackerras 	if (reason & REASON_FP) {
148614cf11afSPaul Mackerras 		/* IEEE FP exception */
1487dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1488ba12eedeSLi Zhong 		goto bail;
14898dad3f92SPaul Mackerras 	}
14908dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1491a4c3f909SBalbir Singh 		unsigned long bugaddr;
1492ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1493ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1494ba797b28SJason Wessel 		if (debugger_bpt(regs))
1495ba12eedeSLi Zhong 			goto bail;
1496ba797b28SJason Wessel 
14976cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
14986cc89badSNaveen N. Rao 			goto bail;
14996cc89badSNaveen N. Rao 
150014cf11afSPaul Mackerras 		/* trap exception */
1501dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1502dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1503ba12eedeSLi Zhong 			goto bail;
150473c9ceabSJeremy Fitzhardinge 
1505a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1506a4c3f909SBalbir Singh 		/*
1507a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1508a4c3f909SBalbir Singh 		 */
1509a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1510a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1511a4c3f909SBalbir Singh 
151273c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1513a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
151414cf11afSPaul Mackerras 			regs->nip += 4;
1515ba12eedeSLi Zhong 			goto bail;
151614cf11afSPaul Mackerras 		}
15178dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1518ba12eedeSLi Zhong 		goto bail;
15198dad3f92SPaul Mackerras 	}
1520bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1521bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1522bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1523bc2a9408SMichael Neuling 		 * This occurs when:
1524bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1525bc2a9408SMichael Neuling 		 *    transition in TM states.
1526bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1527bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1528bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1529bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1530632f0574SMichael Ellerman 		 *
1531632f0574SMichael Ellerman 		 * If usermode caused this, it's done something illegal and
1532bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1533bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1534bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1535bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1536bc2a9408SMichael Neuling 		 */
1537bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1538bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1539ba12eedeSLi Zhong 			goto bail;
1540bc2a9408SMichael Neuling 		} else {
1541bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
154211be3958SBreno Leitao 			       "at %lx (msr 0x%lx) tm_scratch=%llx\n",
154311be3958SBreno Leitao 			       regs->nip, regs->msr, get_paca()->tm_scratch);
1544bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1545bc2a9408SMichael Neuling 		}
1546bc2a9408SMichael Neuling 	}
1547bc2a9408SMichael Neuling #endif
15488dad3f92SPaul Mackerras 
1549b3f6a459SMichael Ellerman 	/*
1550b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1551b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1552b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1553b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1554b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1555b3f6a459SMichael Ellerman 	 */
1556b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1557b3f6a459SMichael Ellerman 		goto sigill;
1558b3f6a459SMichael Ellerman 
1559a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1560a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1561cd8a5673SPaul Mackerras 		local_irq_enable();
1562cd8a5673SPaul Mackerras 
156304903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
156404903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
156504903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
156604903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
156704903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
156804903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
15694e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
15704e63f8edSBenjamin Herrenschmidt 	 */
15713a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1572ba12eedeSLi Zhong 		goto bail;
157304903a30SKumar Gala 
15748dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
15758dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
157614cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
157714cf11afSPaul Mackerras 		case 0:
157814cf11afSPaul Mackerras 			regs->nip += 4;
157914cf11afSPaul Mackerras 			emulate_single_step(regs);
1580ba12eedeSLi Zhong 			goto bail;
158114cf11afSPaul Mackerras 		case -EFAULT:
158214cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1583ba12eedeSLi Zhong 			goto bail;
15848dad3f92SPaul Mackerras 		}
15858dad3f92SPaul Mackerras 	}
15868dad3f92SPaul Mackerras 
1587b3f6a459SMichael Ellerman sigill:
158814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
158914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
159014cf11afSPaul Mackerras 	else
159114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1592ba12eedeSLi Zhong 
1593ba12eedeSLi Zhong bail:
1594ba12eedeSLi Zhong 	exception_exit(prev_state);
159514cf11afSPaul Mackerras }
159603465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
159714cf11afSPaul Mackerras 
1598bf593907SPaul Mackerras /*
1599bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1600bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1601bf593907SPaul Mackerras  */
160203465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1603bf593907SPaul Mackerras {
1604bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1605bf593907SPaul Mackerras 	program_check_exception(regs);
1606bf593907SPaul Mackerras }
160703465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1608bf593907SPaul Mackerras 
1609dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
161014cf11afSPaul Mackerras {
1611ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
16124393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
161314cf11afSPaul Mackerras 
1614a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1615a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1616a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1617a3512b2dSBenjamin Herrenschmidt 
16186ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
16196ce6c629SMichael Neuling 		goto bail;
16206ce6c629SMichael Neuling 
1621e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1622e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
162314cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
162414cf11afSPaul Mackerras 
162514cf11afSPaul Mackerras 	if (fixed == 1) {
162614cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
162714cf11afSPaul Mackerras 		emulate_single_step(regs);
1628ba12eedeSLi Zhong 		goto bail;
162914cf11afSPaul Mackerras 	}
163014cf11afSPaul Mackerras 
163114cf11afSPaul Mackerras 	/* Operand address was bad */
163214cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
16334393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
16344393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
16354393c4f6SBenjamin Herrenschmidt 	} else {
16364393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
16374393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
163814cf11afSPaul Mackerras 	}
16394393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
16404393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
16414393c4f6SBenjamin Herrenschmidt 	else
16424393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1643ba12eedeSLi Zhong 
1644ba12eedeSLi Zhong bail:
1645ba12eedeSLi Zhong 	exception_exit(prev_state);
164614cf11afSPaul Mackerras }
164714cf11afSPaul Mackerras 
164814cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
164914cf11afSPaul Mackerras {
16509bf3d3c4SChristophe Leroy 	pr_crit("Kernel stack overflow in process %s[%d], r1=%lx\n",
16519bf3d3c4SChristophe Leroy 		current->comm, task_pid_nr(current), regs->gpr[1]);
165214cf11afSPaul Mackerras 	debugger(regs);
165314cf11afSPaul Mackerras 	show_regs(regs);
165414cf11afSPaul Mackerras 	panic("kernel stack overflow");
165514cf11afSPaul Mackerras }
165614cf11afSPaul Mackerras 
16573978eb78SChristophe Leroy void stack_overflow_exception(struct pt_regs *regs)
16583978eb78SChristophe Leroy {
16593978eb78SChristophe Leroy 	enum ctx_state prev_state = exception_enter();
16603978eb78SChristophe Leroy 
16613978eb78SChristophe Leroy 	die("Kernel stack overflow", regs, SIGSEGV);
16623978eb78SChristophe Leroy 
16633978eb78SChristophe Leroy 	exception_exit(prev_state);
16643978eb78SChristophe Leroy }
16653978eb78SChristophe Leroy 
1666dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1667dc1c1ca3SStephen Rothwell {
1668ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1669ba12eedeSLi Zhong 
1670dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1671dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1672dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1673ba12eedeSLi Zhong 
1674ba12eedeSLi Zhong 	exception_exit(prev_state);
1675dc1c1ca3SStephen Rothwell }
1676dc1c1ca3SStephen Rothwell 
1677dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1678dc1c1ca3SStephen Rothwell {
1679ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1680ba12eedeSLi Zhong 
1681dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1682dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1683dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1684dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1685ba12eedeSLi Zhong 		goto bail;
1686dc1c1ca3SStephen Rothwell 	}
16876c4841c2SAnton Blanchard 
1688dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1689dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1690dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1691ba12eedeSLi Zhong 
1692ba12eedeSLi Zhong bail:
1693ba12eedeSLi Zhong 	exception_exit(prev_state);
1694dc1c1ca3SStephen Rothwell }
1695dc1c1ca3SStephen Rothwell 
1696ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1697ce48b210SMichael Neuling {
1698ce48b210SMichael Neuling 	if (user_mode(regs)) {
1699ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1700ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1701ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1702ce48b210SMichael Neuling 		return;
1703ce48b210SMichael Neuling 	}
1704ce48b210SMichael Neuling 
1705ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1706ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1707ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1708ce48b210SMichael Neuling }
1709ce48b210SMichael Neuling 
17102517617eSMichael Neuling #ifdef CONFIG_PPC64
1711172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1712172f7aaaSCyril Bur {
17135d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
17145d176f75SCyril Bur 	if (user_mode(regs)) {
17155d176f75SCyril Bur 		current->thread.load_tm++;
17165d176f75SCyril Bur 		regs->msr |= MSR_TM;
17175d176f75SCyril Bur 		tm_enable();
17185d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
17195d176f75SCyril Bur 		return;
17205d176f75SCyril Bur 	}
17215d176f75SCyril Bur #endif
1722172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1723172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1724172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1725172f7aaaSCyril Bur }
1726172f7aaaSCyril Bur 
1727021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1728d0c0c9a1SMichael Neuling {
1729021424a1SMichael Ellerman 	static char *facility_strings[] = {
17302517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
17312517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
17322517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
17332517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
17342517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
17352517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
17362517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
17372517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1738794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
17399b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1740*2aa6195eSAlistair Popple 		[FSCR_PREFIX_LG] = "PREFIX",
1741021424a1SMichael Ellerman 	};
17422517617eSMichael Neuling 	char *facility = "unknown";
1743021424a1SMichael Ellerman 	u64 value;
1744c952c1c4SAnshuman Khandual 	u32 instword, rd;
17452517617eSMichael Neuling 	u8 status;
17462517617eSMichael Neuling 	bool hv;
1747021424a1SMichael Ellerman 
17482271db20SBenjamin Herrenschmidt 	hv = (TRAP(regs) == 0xf80);
17492517617eSMichael Neuling 	if (hv)
1750b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
17512517617eSMichael Neuling 	else
17522517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
17532517617eSMichael Neuling 
17542517617eSMichael Neuling 	status = value >> 56;
1755709b973cSAnshuman Khandual 	if ((hv || status >= 2) &&
1756709b973cSAnshuman Khandual 	    (status < ARRAY_SIZE(facility_strings)) &&
1757709b973cSAnshuman Khandual 	    facility_strings[status])
1758709b973cSAnshuman Khandual 		facility = facility_strings[status];
1759709b973cSAnshuman Khandual 
1760709b973cSAnshuman Khandual 	/* We should not have taken this interrupt in kernel */
1761709b973cSAnshuman Khandual 	if (!user_mode(regs)) {
1762709b973cSAnshuman Khandual 		pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n",
1763709b973cSAnshuman Khandual 			 facility, status, regs->nip);
1764709b973cSAnshuman Khandual 		die("Unexpected facility unavailable exception", regs, SIGABRT);
1765709b973cSAnshuman Khandual 	}
1766709b973cSAnshuman Khandual 
1767709b973cSAnshuman Khandual 	/* We restore the interrupt state now */
1768709b973cSAnshuman Khandual 	if (!arch_irq_disabled_regs(regs))
1769709b973cSAnshuman Khandual 		local_irq_enable();
1770709b973cSAnshuman Khandual 
17712517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1772c952c1c4SAnshuman Khandual 		/*
1773c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1774c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1775c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1776c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1777c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1778c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1779c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1780c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1781c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1782c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1783c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1784c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1785c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1786c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
17872517617eSMichael Neuling 		 */
1788c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1789c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1790c952c1c4SAnshuman Khandual 			return;
1791c952c1c4SAnshuman Khandual 		}
1792c952c1c4SAnshuman Khandual 
1793c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1794c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1795c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1796c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1797c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
17982517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1799b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1800b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1801c952c1c4SAnshuman Khandual 		}
1802c952c1c4SAnshuman Khandual 
1803c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1804c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1805c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1806c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1807c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1808c952c1c4SAnshuman Khandual 				return;
1809c952c1c4SAnshuman Khandual 			}
1810c952c1c4SAnshuman Khandual 			regs->nip += 4;
1811c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1812c952c1c4SAnshuman Khandual 		}
18132517617eSMichael Neuling 		return;
1814b14b6260SMichael Ellerman 	}
1815b14b6260SMichael Ellerman 
1816172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1817172f7aaaSCyril Bur 		/*
1818172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1819172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1820172f7aaaSCyril Bur 		 *
1821172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1822172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1823172f7aaaSCyril Bur 		 * support.
1824172f7aaaSCyril Bur 		 *
1825172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1826172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1827172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1828172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1829172f7aaaSCyril Bur 		 */
1830172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1831172f7aaaSCyril Bur 			goto out;
1832172f7aaaSCyril Bur 
1833172f7aaaSCyril Bur 		tm_unavailable(regs);
1834172f7aaaSCyril Bur 		return;
1835172f7aaaSCyril Bur 	}
1836172f7aaaSCyril Bur 
183793c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
183893c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1839d0c0c9a1SMichael Neuling 
1840172f7aaaSCyril Bur out:
1841d0c0c9a1SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1842d0c0c9a1SMichael Neuling }
18432517617eSMichael Neuling #endif
1844d0c0c9a1SMichael Neuling 
1845f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1846f54db641SMichael Neuling 
1847f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1848f54db641SMichael Neuling {
1849f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1850f54db641SMichael Neuling 
1851f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1852f54db641SMichael Neuling 		 regs->nip, regs->msr);
1853f54db641SMichael Neuling 
1854f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1855f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1856f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1857f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1858f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1859f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1860f54db641SMichael Neuling 	 */
1861d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
186296695563SBreno Leitao 
186396695563SBreno Leitao 	/*
186496695563SBreno Leitao 	 * Reclaim initially saved out bogus (lazy) FPRs to ckfp_state, and
186596695563SBreno Leitao 	 * then it was overwrite by the thr->fp_state by tm_reclaim_thread().
186696695563SBreno Leitao 	 *
186796695563SBreno Leitao 	 * At this point, ck{fp,vr}_state contains the exact values we want to
186896695563SBreno Leitao 	 * recheckpoint.
186996695563SBreno Leitao 	 */
1870f54db641SMichael Neuling 
1871f54db641SMichael Neuling 	/* Enable FP for the task: */
1872a7771176SCyril Bur 	current->thread.load_fp = 1;
1873f54db641SMichael Neuling 
187496695563SBreno Leitao 	/*
187596695563SBreno Leitao 	 * Recheckpoint all the checkpointed ckpt, ck{fp, vr}_state registers.
1876f54db641SMichael Neuling 	 */
1877eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1878f54db641SMichael Neuling }
1879f54db641SMichael Neuling 
1880f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1881f54db641SMichael Neuling {
1882f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1883f54db641SMichael Neuling 	 * the same way.
1884f54db641SMichael Neuling 	 */
1885f54db641SMichael Neuling 
1886f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1887f54db641SMichael Neuling 		 "MSR=%lx\n",
1888f54db641SMichael Neuling 		 regs->nip, regs->msr);
1889d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1890a7771176SCyril Bur 	current->thread.load_vec = 1;
1891eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1892f54db641SMichael Neuling 	current->thread.used_vr = 1;
18933ac8ff1cSPaul Mackerras }
18943ac8ff1cSPaul Mackerras 
1895f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1896f54db641SMichael Neuling {
1897f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1898f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1899f54db641SMichael Neuling 	 *
1900f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1901f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1902f54db641SMichael Neuling 	 */
1903f54db641SMichael Neuling 
1904f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1905f54db641SMichael Neuling 		 "MSR=%lx\n",
1906f54db641SMichael Neuling 		 regs->nip, regs->msr);
1907f54db641SMichael Neuling 
19083ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
19093ac8ff1cSPaul Mackerras 
1910f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1911d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1912f54db641SMichael Neuling 
1913a7771176SCyril Bur 	current->thread.load_vec = 1;
1914a7771176SCyril Bur 	current->thread.load_fp = 1;
19153ac8ff1cSPaul Mackerras 
1916eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1917f54db641SMichael Neuling }
1918f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1919f54db641SMichael Neuling 
1920dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1921dc1c1ca3SStephen Rothwell {
192269111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
192389713ed1SAnton Blanchard 
1924dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1925dc1c1ca3SStephen Rothwell }
1926dc1c1ca3SStephen Rothwell 
1927172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
19283bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
19293bffb652SDave Kleikamp {
19303bffb652SDave Kleikamp 	int changed = 0;
19313bffb652SDave Kleikamp 	/*
19323bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
19333bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
19343bffb652SDave Kleikamp 	 */
19353bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
19363bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
19373bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
193851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
19393bffb652SDave Kleikamp #endif
194047355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status,
19413bffb652SDave Kleikamp 			     5);
19423bffb652SDave Kleikamp 		changed |= 0x01;
19433bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
19443bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
194547355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status,
19463bffb652SDave Kleikamp 			     6);
19473bffb652SDave Kleikamp 		changed |= 0x01;
19483bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
194951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
19503bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
195147355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status,
19523bffb652SDave Kleikamp 			     1);
19533bffb652SDave Kleikamp 		changed |= 0x01;
19543bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
195551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
195647355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status,
19573bffb652SDave Kleikamp 			     2);
19583bffb652SDave Kleikamp 		changed |= 0x01;
19593bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
196051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
19613bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
196247355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status,
19633bffb652SDave Kleikamp 			     3);
19643bffb652SDave Kleikamp 		changed |= 0x01;
19653bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
196651ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
196747355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status,
19683bffb652SDave Kleikamp 			     4);
19693bffb652SDave Kleikamp 		changed |= 0x01;
19703bffb652SDave Kleikamp 	}
19713bffb652SDave Kleikamp 	/*
19723bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
19733bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
19743bffb652SDave Kleikamp 	 * back on or not.
19753bffb652SDave Kleikamp 	 */
197651ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
197751ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
19783bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
19793bffb652SDave Kleikamp 	else
19803bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
198151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
19823bffb652SDave Kleikamp 
19833bffb652SDave Kleikamp 	if (changed & 0x01)
198451ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
19853bffb652SDave Kleikamp }
198614cf11afSPaul Mackerras 
198703465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
198814cf11afSPaul Mackerras {
198951ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
19903bffb652SDave Kleikamp 
1991ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1992ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1993ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1994ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1995ec097c84SRoland McGrath 	 */
1996ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1997ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1998ec097c84SRoland McGrath 
1999ec097c84SRoland McGrath 		/* Disable BT */
2000ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
2001ec097c84SRoland McGrath 		/* Clear the BT event */
2002ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
2003ec097c84SRoland McGrath 
2004ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
2005ec097c84SRoland McGrath 		if (user_mode(regs)) {
200651ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
200751ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
2008ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
2009ec097c84SRoland McGrath 			return;
2010ec097c84SRoland McGrath 		}
2011ec097c84SRoland McGrath 
20126cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
20136cc89badSNaveen N. Rao 			return;
20146cc89badSNaveen N. Rao 
2015ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
2016ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
2017ec097c84SRoland McGrath 			return;
2018ec097c84SRoland McGrath 		}
2019ec097c84SRoland McGrath 		if (debugger_sstep(regs))
2020ec097c84SRoland McGrath 			return;
2021ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
202214cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
2023f8279621SKumar Gala 
202414cf11afSPaul Mackerras 		/* Disable instruction completion */
202514cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
202614cf11afSPaul Mackerras 		/* Clear the instruction completion event */
202714cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
2028f8279621SKumar Gala 
20296cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
20306cc89badSNaveen N. Rao 			return;
20316cc89badSNaveen N. Rao 
2032f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
2033f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
203414cf11afSPaul Mackerras 			return;
203514cf11afSPaul Mackerras 		}
2036f8279621SKumar Gala 
2037f8279621SKumar Gala 		if (debugger_sstep(regs))
2038f8279621SKumar Gala 			return;
2039f8279621SKumar Gala 
20403bffb652SDave Kleikamp 		if (user_mode(regs)) {
204151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
204251ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
204351ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
20443bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
20453bffb652SDave Kleikamp 			else
20463bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
204751ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
20483bffb652SDave Kleikamp 		}
2049f8279621SKumar Gala 
2050f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
20513bffb652SDave Kleikamp 	} else
20523bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
205314cf11afSPaul Mackerras }
205403465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
2055172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
205614cf11afSPaul Mackerras 
205714cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
205814cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
205914cf11afSPaul Mackerras {
206014cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
206114cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
206214cf11afSPaul Mackerras }
206314cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
206414cf11afSPaul Mackerras 
206514cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
2066dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
206714cf11afSPaul Mackerras {
206814cf11afSPaul Mackerras 	int err;
206914cf11afSPaul Mackerras 
207014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
207114cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
207214cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
20738dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
207414cf11afSPaul Mackerras 	}
207514cf11afSPaul Mackerras 
2076dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
2077dc1c1ca3SStephen Rothwell 
2078eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
207914cf11afSPaul Mackerras 	err = emulate_altivec(regs);
208014cf11afSPaul Mackerras 	if (err == 0) {
208114cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
208214cf11afSPaul Mackerras 		emulate_single_step(regs);
208314cf11afSPaul Mackerras 		return;
208414cf11afSPaul Mackerras 	}
208514cf11afSPaul Mackerras 
208614cf11afSPaul Mackerras 	if (err == -EFAULT) {
208714cf11afSPaul Mackerras 		/* got an error reading the instruction */
208814cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
208914cf11afSPaul Mackerras 	} else {
209014cf11afSPaul Mackerras 		/* didn't recognize the instruction */
209114cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
209276462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
209314cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
2094de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
209514cf11afSPaul Mackerras 	}
209614cf11afSPaul Mackerras }
209714cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
209814cf11afSPaul Mackerras 
209914cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
210014cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
210114cf11afSPaul Mackerras 			   unsigned long error_code)
210214cf11afSPaul Mackerras {
210314cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
210414cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
210514cf11afSPaul Mackerras 	 * something smarter
210614cf11afSPaul Mackerras 	 */
210714cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
210814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
210914cf11afSPaul Mackerras 	return;
211014cf11afSPaul Mackerras }
211114cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
211214cf11afSPaul Mackerras 
211314cf11afSPaul Mackerras #ifdef CONFIG_SPE
211414cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
211514cf11afSPaul Mackerras {
21166a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
211714cf11afSPaul Mackerras 	unsigned long spefscr;
211814cf11afSPaul Mackerras 	int fpexc_mode;
2119aeb1c0f6SEric W. Biederman 	int code = FPE_FLTUNK;
21206a800f36SLiu Yu 	int err;
21216a800f36SLiu Yu 
2122ef429124SChristophe Leroy 	/* We restore the interrupt state now */
2123ef429124SChristophe Leroy 	if (!arch_irq_disabled_regs(regs))
2124ef429124SChristophe Leroy 		local_irq_enable();
2125ef429124SChristophe Leroy 
2126685659eeSyu liu 	flush_spe_to_thread(current);
212714cf11afSPaul Mackerras 
212814cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
212914cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
213014cf11afSPaul Mackerras 
213114cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
213214cf11afSPaul Mackerras 		code = FPE_FLTOVF;
213314cf11afSPaul Mackerras 	}
213414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
213514cf11afSPaul Mackerras 		code = FPE_FLTUND;
213614cf11afSPaul Mackerras 	}
213714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
213814cf11afSPaul Mackerras 		code = FPE_FLTDIV;
213914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
214014cf11afSPaul Mackerras 		code = FPE_FLTINV;
214114cf11afSPaul Mackerras 	}
214214cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
214314cf11afSPaul Mackerras 		code = FPE_FLTRES;
214414cf11afSPaul Mackerras 
21456a800f36SLiu Yu 	err = do_spe_mathemu(regs);
21466a800f36SLiu Yu 	if (err == 0) {
21476a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
21486a800f36SLiu Yu 		emulate_single_step(regs);
214914cf11afSPaul Mackerras 		return;
215014cf11afSPaul Mackerras 	}
21516a800f36SLiu Yu 
21526a800f36SLiu Yu 	if (err == -EFAULT) {
21536a800f36SLiu Yu 		/* got an error reading the instruction */
21546a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
21556a800f36SLiu Yu 	} else if (err == -EINVAL) {
21566a800f36SLiu Yu 		/* didn't recognize the instruction */
21576a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
21586a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
21596a800f36SLiu Yu 	} else {
21606a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
21616a800f36SLiu Yu 	}
21626a800f36SLiu Yu 
21636a800f36SLiu Yu 	return;
21646a800f36SLiu Yu }
21656a800f36SLiu Yu 
21666a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
21676a800f36SLiu Yu {
21686a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
21696a800f36SLiu Yu 	int err;
21706a800f36SLiu Yu 
2171ef429124SChristophe Leroy 	/* We restore the interrupt state now */
2172ef429124SChristophe Leroy 	if (!arch_irq_disabled_regs(regs))
2173ef429124SChristophe Leroy 		local_irq_enable();
2174ef429124SChristophe Leroy 
21756a800f36SLiu Yu 	preempt_disable();
21766a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
21776a800f36SLiu Yu 		giveup_spe(current);
21786a800f36SLiu Yu 	preempt_enable();
21796a800f36SLiu Yu 
21806a800f36SLiu Yu 	regs->nip -= 4;
21816a800f36SLiu Yu 	err = speround_handler(regs);
21826a800f36SLiu Yu 	if (err == 0) {
21836a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
21846a800f36SLiu Yu 		emulate_single_step(regs);
21856a800f36SLiu Yu 		return;
21866a800f36SLiu Yu 	}
21876a800f36SLiu Yu 
21886a800f36SLiu Yu 	if (err == -EFAULT) {
21896a800f36SLiu Yu 		/* got an error reading the instruction */
21906a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
21916a800f36SLiu Yu 	} else if (err == -EINVAL) {
21926a800f36SLiu Yu 		/* didn't recognize the instruction */
21936a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
21946a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
21956a800f36SLiu Yu 	} else {
2196aeb1c0f6SEric W. Biederman 		_exception(SIGFPE, regs, FPE_FLTUNK, regs->nip);
21976a800f36SLiu Yu 		return;
21986a800f36SLiu Yu 	}
21996a800f36SLiu Yu }
220014cf11afSPaul Mackerras #endif
220114cf11afSPaul Mackerras 
2202dc1c1ca3SStephen Rothwell /*
2203dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
2204dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
2205dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
2206dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
2207dc1c1ca3SStephen Rothwell  */
2208dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
2209dc1c1ca3SStephen Rothwell {
221051423a9cSChristophe Leroy 	pr_emerg("Unrecoverable exception %lx at %lx (msr=%lx)\n",
221151423a9cSChristophe Leroy 		 regs->trap, regs->nip, regs->msr);
2212dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
2213dc1c1ca3SStephen Rothwell }
221415770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
2215dc1c1ca3SStephen Rothwell 
22161e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
221714cf11afSPaul Mackerras /*
221814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
221914cf11afSPaul Mackerras  * spins until a reboot occurs
222014cf11afSPaul Mackerras  */
222114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
222214cf11afSPaul Mackerras {
222314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
222414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
222514cf11afSPaul Mackerras 	return;
222614cf11afSPaul Mackerras }
222714cf11afSPaul Mackerras 
222814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
222914cf11afSPaul Mackerras {
223014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
223114cf11afSPaul Mackerras 	WatchdogHandler(regs);
223214cf11afSPaul Mackerras }
223314cf11afSPaul Mackerras #endif
2234dc1c1ca3SStephen Rothwell 
2235dc1c1ca3SStephen Rothwell /*
2236dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
2237dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
2238dc1c1ca3SStephen Rothwell  */
2239dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
2240dc1c1ca3SStephen Rothwell {
2241dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
2242dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
2243dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
2244dc1c1ca3SStephen Rothwell }
224515770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
224614cf11afSPaul Mackerras 
224714cf11afSPaul Mackerras void __init trap_init(void)
224814cf11afSPaul Mackerras {
224914cf11afSPaul Mackerras }
225080947e7cSGeert Uytterhoeven 
225180947e7cSGeert Uytterhoeven 
225280947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
225380947e7cSGeert Uytterhoeven 
225480947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
225580947e7cSGeert Uytterhoeven 
225680947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
225780947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
225880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
225980947e7cSGeert Uytterhoeven #endif
226080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
226180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
226280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
226380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
226480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
226580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
226680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
226780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
226880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
226980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
2270a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
227180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
227280947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
227380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
227480947e7cSGeert Uytterhoeven #endif
227580947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
227680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
227780947e7cSGeert Uytterhoeven #endif
2278efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
2279efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
2280efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
2281f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
22825080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvw4x),
22835080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvh8x),
22845080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvd2x),
22855080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvb16x),
2286efcac658SAlexey Kardashevskiy #endif
228780947e7cSGeert Uytterhoeven };
228880947e7cSGeert Uytterhoeven 
228980947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
229080947e7cSGeert Uytterhoeven 
229180947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
229280947e7cSGeert Uytterhoeven {
229376462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
229480947e7cSGeert Uytterhoeven 			    type);
229580947e7cSGeert Uytterhoeven }
229680947e7cSGeert Uytterhoeven 
229780947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
229880947e7cSGeert Uytterhoeven {
2299860286cfSGreg Kroah-Hartman 	struct dentry *dir;
230080947e7cSGeert Uytterhoeven 	unsigned int i;
230180947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
230280947e7cSGeert Uytterhoeven 
230380947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
230480947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
230580947e7cSGeert Uytterhoeven 
2306860286cfSGreg Kroah-Hartman 	debugfs_create_u32("do_warn", 0644, dir, &ppc_warn_emulated);
230780947e7cSGeert Uytterhoeven 
2308860286cfSGreg Kroah-Hartman 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++)
2309860286cfSGreg Kroah-Hartman 		debugfs_create_u32(entries[i].name, 0644, dir,
231080947e7cSGeert Uytterhoeven 				   (u32 *)&entries[i].val.counter);
231180947e7cSGeert Uytterhoeven 
231280947e7cSGeert Uytterhoeven 	return 0;
231380947e7cSGeert Uytterhoeven }
231480947e7cSGeert Uytterhoeven 
231580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
231680947e7cSGeert Uytterhoeven 
231780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2318