xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 1c56cd8ee945abdf955ad28a1f5c7576cd288be2)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
614cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
714cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
814cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1114cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras /*
1514cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/mm.h>
2314cf11afSPaul Mackerras #include <linux/stddef.h>
2414cf11afSPaul Mackerras #include <linux/unistd.h>
258dad3f92SPaul Mackerras #include <linux/ptrace.h>
2614cf11afSPaul Mackerras #include <linux/user.h>
2714cf11afSPaul Mackerras #include <linux/interrupt.h>
2814cf11afSPaul Mackerras #include <linux/init.h>
298a39b05fSPaul Gortmaker #include <linux/extable.h>
308a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
318dad3f92SPaul Mackerras #include <linux/prctl.h>
3214cf11afSPaul Mackerras #include <linux/delay.h>
3314cf11afSPaul Mackerras #include <linux/kprobes.h>
34cc532915SMichael Ellerman #include <linux/kexec.h>
355474c120SMichael Hanselmann #include <linux/backlight.h>
3673c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
371eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3876462232SChristian Dietrich #include <linux/ratelimit.h>
39ba12eedeSLi Zhong #include <linux/context_tracking.h>
4014cf11afSPaul Mackerras 
4180947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4214cf11afSPaul Mackerras #include <asm/pgtable.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
447644d581SMichael Ellerman #include <asm/debugfs.h>
4514cf11afSPaul Mackerras #include <asm/io.h>
4686417780SPaul Mackerras #include <asm/machdep.h>
4786417780SPaul Mackerras #include <asm/rtas.h>
48f7f6f4feSDavid Gibson #include <asm/pmc.h>
4914cf11afSPaul Mackerras #include <asm/reg.h>
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
566ce6c629SMichael Neuling #include <asm/tm.h>
57dc1c1ca3SStephen Rothwell #endif
58c0ce7d08SDavid Wilder #include <asm/kexec.h>
5916c57b36SKumar Gala #include <asm/ppc-opcode.h>
60cce1f106SShaohui Xie #include <asm/rio.h>
61ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
62ae3a197eSDavid Howells #include <asm/switch_to.h>
63f54db641SMichael Neuling #include <asm/tm.h>
64ae3a197eSDavid Howells #include <asm/debug.h>
6542f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
66fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
674e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
686cc89badSNaveen N. Rao #include <asm/kprobes.h>
69dc1c1ca3SStephen Rothwell 
70da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
715be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
725be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
735be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
745be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
755be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
769422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
775be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7814cf11afSPaul Mackerras 
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
849422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8614cf11afSPaul Mackerras #endif
8714cf11afSPaul Mackerras 
888b3c34cfSMichael Neuling /* Transactional Memory trap debug */
898b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
908b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
918b3c34cfSMichael Neuling #else
928b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
938b3c34cfSMichael Neuling #endif
948b3c34cfSMichael Neuling 
9514cf11afSPaul Mackerras /*
9614cf11afSPaul Mackerras  * Trap & Exception support
9714cf11afSPaul Mackerras  */
9814cf11afSPaul Mackerras 
996031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1006031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1016031d9d9Santon@samba.org {
1026031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1036031d9d9Santon@samba.org 	if (pmac_backlight) {
1046031d9d9Santon@samba.org 		struct backlight_properties *props;
1056031d9d9Santon@samba.org 
1066031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1076031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1086031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1096031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1106031d9d9Santon@samba.org 	}
1116031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1126031d9d9Santon@samba.org }
1136031d9d9Santon@samba.org #else
1146031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1156031d9d9Santon@samba.org #endif
1166031d9d9Santon@samba.org 
117760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
118760ca4dcSAnton Blanchard static int die_owner = -1;
119760ca4dcSAnton Blanchard static unsigned int die_nest_count;
120c0ce7d08SDavid Wilder static int die_counter;
121760ca4dcSAnton Blanchard 
12203465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
123760ca4dcSAnton Blanchard {
124760ca4dcSAnton Blanchard 	int cpu;
12534c2a14fSanton@samba.org 	unsigned long flags;
12614cf11afSPaul Mackerras 
127293e4688Santon@samba.org 	oops_enter();
128293e4688Santon@samba.org 
129760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
130760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
131760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
132760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
133760ca4dcSAnton Blanchard 		if (cpu == die_owner)
134760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
135760ca4dcSAnton Blanchard 		else
136760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
137760ca4dcSAnton Blanchard 	}
138760ca4dcSAnton Blanchard 	die_nest_count++;
139760ca4dcSAnton Blanchard 	die_owner = cpu;
14014cf11afSPaul Mackerras 	console_verbose();
14114cf11afSPaul Mackerras 	bust_spinlocks(1);
1426031d9d9Santon@samba.org 	if (machine_is(powermac))
1436031d9d9Santon@samba.org 		pmac_backlight_unblank();
144760ca4dcSAnton Blanchard 	return flags;
14534c2a14fSanton@samba.org }
14603465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
1475474c120SMichael Hanselmann 
14803465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
149760ca4dcSAnton Blanchard 			       int signr)
150760ca4dcSAnton Blanchard {
15114cf11afSPaul Mackerras 	bust_spinlocks(0);
152373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
153760ca4dcSAnton Blanchard 	die_nest_count--;
15458154c8cSAnton Blanchard 	oops_exit();
15558154c8cSAnton Blanchard 	printk("\n");
1567458e8b2SNicholas Piggin 	if (!die_nest_count) {
157760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
1587458e8b2SNicholas Piggin 		die_owner = -1;
159760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
1607458e8b2SNicholas Piggin 	}
161760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
162cc532915SMichael Ellerman 
163ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
164ebaeb5aeSMahesh Salgaonkar 
1659b00ac06SAnton Blanchard 	/*
1669b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1679b00ac06SAnton Blanchard 	 * it through the crashdump code.
1689b00ac06SAnton Blanchard 	 */
1699b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
170cc532915SMichael Ellerman 		crash_kexec(regs);
1719b00ac06SAnton Blanchard 
1729b00ac06SAnton Blanchard 		/*
1739b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1749b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1759b00ac06SAnton Blanchard 		 * code.
1769b00ac06SAnton Blanchard 		 */
177c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1789b00ac06SAnton Blanchard 	}
17914cf11afSPaul Mackerras 
180760ca4dcSAnton Blanchard 	if (!signr)
181760ca4dcSAnton Blanchard 		return;
182760ca4dcSAnton Blanchard 
18358154c8cSAnton Blanchard 	/*
18458154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
18558154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18658154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18758154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18858154c8cSAnton Blanchard 	 */
18958154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
19058154c8cSAnton Blanchard 	    is_global_init(current)) {
19158154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
19258154c8cSAnton Blanchard 	}
19358154c8cSAnton Blanchard 
19414cf11afSPaul Mackerras 	if (in_interrupt())
19514cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
196cea6a4baSHorms 	if (panic_on_oops)
197012c437dSHorms 		panic("Fatal exception");
198760ca4dcSAnton Blanchard 	do_exit(signr);
199760ca4dcSAnton Blanchard }
20003465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
201cea6a4baSHorms 
20203465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
203760ca4dcSAnton Blanchard {
204760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2052e82ca3cSMichael Ellerman 
2062e82ca3cSMichael Ellerman 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
2072e82ca3cSMichael Ellerman 		printk("LE ");
2082e82ca3cSMichael Ellerman 	else
2092e82ca3cSMichael Ellerman 		printk("BE ");
2102e82ca3cSMichael Ellerman 
211*1c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_PREEMPT))
21272c0d9eeSMichael Ellerman 		pr_cont("PREEMPT ");
213*1c56cd8eSMichael Ellerman 
214*1c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_SMP))
21572c0d9eeSMichael Ellerman 		pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
216*1c56cd8eSMichael Ellerman 
217e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
21872c0d9eeSMichael Ellerman 		pr_cont("DEBUG_PAGEALLOC ");
219*1c56cd8eSMichael Ellerman 
220*1c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_NUMA))
22172c0d9eeSMichael Ellerman 		pr_cont("NUMA ");
222*1c56cd8eSMichael Ellerman 
22372c0d9eeSMichael Ellerman 	pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
224760ca4dcSAnton Blanchard 
225760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
226760ca4dcSAnton Blanchard 		return 1;
227760ca4dcSAnton Blanchard 
228760ca4dcSAnton Blanchard 	print_modules();
229760ca4dcSAnton Blanchard 	show_regs(regs);
23014cf11afSPaul Mackerras 
23114cf11afSPaul Mackerras 	return 0;
23214cf11afSPaul Mackerras }
23303465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
23414cf11afSPaul Mackerras 
235760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
236760ca4dcSAnton Blanchard {
2376f44b20eSNicholas Piggin 	unsigned long flags;
238760ca4dcSAnton Blanchard 
2396f44b20eSNicholas Piggin 	if (debugger(regs))
2406f44b20eSNicholas Piggin 		return;
2416f44b20eSNicholas Piggin 
2426f44b20eSNicholas Piggin 	flags = oops_begin(regs);
243760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
244760ca4dcSAnton Blanchard 		err = 0;
245760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
246760ca4dcSAnton Blanchard }
24715770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
248760ca4dcSAnton Blanchard 
24925baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
25025baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
25125baa35bSOleg Nesterov {
25225baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
25325baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
25425baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
25525baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
25625baa35bSOleg Nesterov }
25725baa35bSOleg Nesterov 
25814cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
25914cf11afSPaul Mackerras {
26014cf11afSPaul Mackerras 	siginfo_t info;
261d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
262d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
263d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
264d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
26514cf11afSPaul Mackerras 
26614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
267760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
26814cf11afSPaul Mackerras 		return;
269760ca4dcSAnton Blanchard 	}
270760ca4dcSAnton Blanchard 
271760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
27276462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
273d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
274d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
27514cf11afSPaul Mackerras 	}
27614cf11afSPaul Mackerras 
277a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2789f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2799f2f79e3SBenjamin Herrenschmidt 
28041ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
28114cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
28214cf11afSPaul Mackerras 	info.si_signo = signr;
28314cf11afSPaul Mackerras 	info.si_code = code;
28414cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
28514cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
28614cf11afSPaul Mackerras }
28714cf11afSPaul Mackerras 
28814cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
28914cf11afSPaul Mackerras {
2902b4f3ac5SNicholas Piggin 	/*
2912b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
2922b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
2932b4f3ac5SNicholas Piggin 	 */
2942b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
2952b4f3ac5SNicholas Piggin 	if (!nested)
2962b4f3ac5SNicholas Piggin 		nmi_enter();
2972b4f3ac5SNicholas Piggin 
298ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
299ca41ad43SNicholas Piggin 
30014cf11afSPaul Mackerras 	/* See if any machine dependent calls */
301c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
302c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
303c4f3b52cSNicholas Piggin 			goto out;
304c902be71SArnd Bergmann 	}
30514cf11afSPaul Mackerras 
3068dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
30714cf11afSPaul Mackerras 
308c4f3b52cSNicholas Piggin out:
309c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
310c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
311c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
312c4f3b52cSNicholas Piggin 		panic("Unrecoverable nested System Reset");
313c4f3b52cSNicholas Piggin #endif
31414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
31514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
31614cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
31714cf11afSPaul Mackerras 
3182b4f3ac5SNicholas Piggin 	if (!nested)
3192b4f3ac5SNicholas Piggin 		nmi_exit();
3202b4f3ac5SNicholas Piggin 
32114cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
32214cf11afSPaul Mackerras }
3231e9b4507SMahesh Salgaonkar 
32414cf11afSPaul Mackerras /*
32514cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
32614cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
32714cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
32814cf11afSPaul Mackerras  * table.
32914cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
33014cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
33114cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
33214cf11afSPaul Mackerras  *  -- paulus.
33314cf11afSPaul Mackerras  */
33414cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
33514cf11afSPaul Mackerras {
33668a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
33714cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
33814cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
33914cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
34014cf11afSPaul Mackerras 
34114cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
34214cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
34314cf11afSPaul Mackerras 		/*
34414cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
34514cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
34614cf11afSPaul Mackerras 		 * As the address is in the exception table
34714cf11afSPaul Mackerras 		 * we should be able to read the instr there.
34814cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
34914cf11afSPaul Mackerras 		 * load or store.
35014cf11afSPaul Mackerras 		 */
351ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
35214cf11afSPaul Mackerras 			nip -= 2;
353ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
35414cf11afSPaul Mackerras 			--nip;
355ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
35614cf11afSPaul Mackerras 			unsigned int rb;
35714cf11afSPaul Mackerras 
35814cf11afSPaul Mackerras 			--nip;
35914cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
36014cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
36114cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
36214cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
36314cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
36461a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
36514cf11afSPaul Mackerras 			return 1;
36614cf11afSPaul Mackerras 		}
36714cf11afSPaul Mackerras 	}
36868a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
36914cf11afSPaul Mackerras 	return 0;
37014cf11afSPaul Mackerras }
37114cf11afSPaul Mackerras 
372172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
37314cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
37414cf11afSPaul Mackerras    is in the ESR. */
37514cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
37614cf11afSPaul Mackerras #define REASON_FP		ESR_FP
37714cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
37814cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
37914cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
38014cf11afSPaul Mackerras 
38114cf11afSPaul Mackerras /* single-step stuff */
38251ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
38351ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
38414cf11afSPaul Mackerras 
38514cf11afSPaul Mackerras #else
38614cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
38714cf11afSPaul Mackerras    exception is in the MSR. */
38814cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
389d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
390d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
391d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
392d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
393d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
39414cf11afSPaul Mackerras 
39514cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
39614cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
39714cf11afSPaul Mackerras #endif
39814cf11afSPaul Mackerras 
3990d0935b3SMichael Ellerman #if defined(CONFIG_E500)
400fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
401fe04b112SScott Wood {
402fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
403fe04b112SScott Wood 	unsigned long reason = mcsr;
404fe04b112SScott Wood 	int recoverable = 1;
405fe04b112SScott Wood 
40682a9a480SScott Wood 	if (reason & MCSR_LD) {
407cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
408cce1f106SShaohui Xie 		if (recoverable == 1)
409cce1f106SShaohui Xie 			goto silent_out;
410cce1f106SShaohui Xie 	}
411cce1f106SShaohui Xie 
412fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
413fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
414fe04b112SScott Wood 
415fe04b112SScott Wood 	if (reason & MCSR_MCP)
416fe04b112SScott Wood 		printk("Machine Check Signal\n");
417fe04b112SScott Wood 
418fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
419fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
420fe04b112SScott Wood 
421fe04b112SScott Wood 		/*
422fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
423fe04b112SScott Wood 		 */
424fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
425fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
426fe04b112SScott Wood 			;
427fe04b112SScott Wood 
428fe04b112SScott Wood 		/*
429fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
430fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
431fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
432fe04b112SScott Wood 		 */
433fe04b112SScott Wood 		reason &= ~MCSR_IF;
434fe04b112SScott Wood 	}
435fe04b112SScott Wood 
436fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
437fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
43837caf9f2SKumar Gala 
43937caf9f2SKumar Gala 		/*
44037caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
44137caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
44237caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
44337caf9f2SKumar Gala 		 */
44437caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
445fe04b112SScott Wood 			recoverable = 0;
446fe04b112SScott Wood 	}
447fe04b112SScott Wood 
448fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
449fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
450fe04b112SScott Wood 		recoverable = 0;
451fe04b112SScott Wood 	}
452fe04b112SScott Wood 
453fe04b112SScott Wood 	if (reason & MCSR_NMI)
454fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
455fe04b112SScott Wood 
456fe04b112SScott Wood 	if (reason & MCSR_IF) {
457fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
458fe04b112SScott Wood 		recoverable = 0;
459fe04b112SScott Wood 	}
460fe04b112SScott Wood 
461fe04b112SScott Wood 	if (reason & MCSR_LD) {
462fe04b112SScott Wood 		printk("Load Error Report\n");
463fe04b112SScott Wood 		recoverable = 0;
464fe04b112SScott Wood 	}
465fe04b112SScott Wood 
466fe04b112SScott Wood 	if (reason & MCSR_ST) {
467fe04b112SScott Wood 		printk("Store Error Report\n");
468fe04b112SScott Wood 		recoverable = 0;
469fe04b112SScott Wood 	}
470fe04b112SScott Wood 
471fe04b112SScott Wood 	if (reason & MCSR_LDG) {
472fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
473fe04b112SScott Wood 		recoverable = 0;
474fe04b112SScott Wood 	}
475fe04b112SScott Wood 
476fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
477fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
478fe04b112SScott Wood 
479fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
480fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
481fe04b112SScott Wood 		recoverable = 0;
482fe04b112SScott Wood 	}
483fe04b112SScott Wood 
484fe04b112SScott Wood 	if (reason & MCSR_MAV) {
485fe04b112SScott Wood 		u64 addr;
486fe04b112SScott Wood 
487fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
488fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
489fe04b112SScott Wood 
490fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
491fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
492fe04b112SScott Wood 	}
493fe04b112SScott Wood 
494cce1f106SShaohui Xie silent_out:
495fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
496fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
497fe04b112SScott Wood }
498fe04b112SScott Wood 
49947c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
50047c0bd1aSBenjamin Herrenschmidt {
50142bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
50247c0bd1aSBenjamin Herrenschmidt 
503cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
504cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
505cce1f106SShaohui Xie 			return 1;
5064e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5074e0e3435SHongtao Jia 			return 1;
508cce1f106SShaohui Xie 	}
509cce1f106SShaohui Xie 
51014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
51114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
51214cf11afSPaul Mackerras 
51314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
51414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
51514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
51614cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
51714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
51814cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
51914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
52014cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
52114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
52214cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
52314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
52414cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
52514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
52614cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
52714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
52814cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
52914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
53014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
53114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
532c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
53314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
53414cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
53514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
53614cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
53747c0bd1aSBenjamin Herrenschmidt 
53847c0bd1aSBenjamin Herrenschmidt 	return 0;
53947c0bd1aSBenjamin Herrenschmidt }
5404490c06bSKumar Gala 
5414490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5424490c06bSKumar Gala {
5434490c06bSKumar Gala 	return 0;
5444490c06bSKumar Gala }
54514cf11afSPaul Mackerras #elif defined(CONFIG_E200)
54647c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
54747c0bd1aSBenjamin Herrenschmidt {
54842bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
54947c0bd1aSBenjamin Herrenschmidt 
55014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
55114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
55214cf11afSPaul Mackerras 
55314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
55414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
55514cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
55614cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
55714cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
55814cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
55914cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
56014cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
56114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
56214cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
56314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
56414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
56514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
56614cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
56747c0bd1aSBenjamin Herrenschmidt 
56847c0bd1aSBenjamin Herrenschmidt 	return 0;
56947c0bd1aSBenjamin Herrenschmidt }
5707f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
57147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
57247c0bd1aSBenjamin Herrenschmidt {
57342bff234SMichael Ellerman 	unsigned long reason = regs->msr;
57447c0bd1aSBenjamin Herrenschmidt 
57514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
57714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
57814cf11afSPaul Mackerras 	case 0x80000:
57914cf11afSPaul Mackerras 		printk("Machine check signal\n");
58014cf11afSPaul Mackerras 		break;
58114cf11afSPaul Mackerras 	case 0:		/* for 601 */
58214cf11afSPaul Mackerras 	case 0x40000:
58314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
58414cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
58514cf11afSPaul Mackerras 		break;
58614cf11afSPaul Mackerras 	case 0x20000:
58714cf11afSPaul Mackerras 		printk("Data parity error signal\n");
58814cf11afSPaul Mackerras 		break;
58914cf11afSPaul Mackerras 	case 0x10000:
59014cf11afSPaul Mackerras 		printk("Address parity error signal\n");
59114cf11afSPaul Mackerras 		break;
59214cf11afSPaul Mackerras 	case 0x20000000:
59314cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
59414cf11afSPaul Mackerras 		break;
59514cf11afSPaul Mackerras 	case 0x40000000:
59614cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
59714cf11afSPaul Mackerras 		break;
59814cf11afSPaul Mackerras 	case 0x00100000:
59914cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
60014cf11afSPaul Mackerras 		break;
60114cf11afSPaul Mackerras 	default:
60214cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
60314cf11afSPaul Mackerras 	}
60475918a4bSOlof Johansson 	return 0;
60575918a4bSOlof Johansson }
60647c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
60775918a4bSOlof Johansson 
60875918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
60975918a4bSOlof Johansson {
610ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
61175918a4bSOlof Johansson 	int recover = 0;
61275918a4bSOlof Johansson 
613f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
614f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
61569111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
61689713ed1SAnton Blanchard 
617d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
618d93b0ac0SMahesh Salgaonkar 
61947c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
62047c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
62147c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
62247c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
62347c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
62447c0bd1aSBenjamin Herrenschmidt 	 */
62575918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
62675918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
62747c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
62847c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
62975918a4bSOlof Johansson 
63047c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
631ba12eedeSLi Zhong 		goto bail;
63275918a4bSOlof Johansson 
633a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
634ba12eedeSLi Zhong 		goto bail;
63575918a4bSOlof Johansson 
63675918a4bSOlof Johansson 	if (check_io_access(regs))
637ba12eedeSLi Zhong 		goto bail;
63875918a4bSOlof Johansson 
6398dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
64014cf11afSPaul Mackerras 
64114cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
64214cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
64314cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
644ba12eedeSLi Zhong 
645ba12eedeSLi Zhong bail:
646ba12eedeSLi Zhong 	exception_exit(prev_state);
64714cf11afSPaul Mackerras }
64814cf11afSPaul Mackerras 
64914cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
65014cf11afSPaul Mackerras {
65114cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
65214cf11afSPaul Mackerras }
65314cf11afSPaul Mackerras 
6540869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
6550869b6fdSMahesh Salgaonkar {
6560869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
6570869b6fdSMahesh Salgaonkar 
6580869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
6590869b6fdSMahesh Salgaonkar 	irq_enter();
6600869b6fdSMahesh Salgaonkar 
6610869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
6620869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
6630869b6fdSMahesh Salgaonkar 
6640869b6fdSMahesh Salgaonkar 	irq_exit();
6650869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
6660869b6fdSMahesh Salgaonkar }
6670869b6fdSMahesh Salgaonkar 
668dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
66914cf11afSPaul Mackerras {
670ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
671ba12eedeSLi Zhong 
67214cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
67314cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
67414cf11afSPaul Mackerras 
67514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
676ba12eedeSLi Zhong 
677ba12eedeSLi Zhong 	exception_exit(prev_state);
67814cf11afSPaul Mackerras }
67914cf11afSPaul Mackerras 
680dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
68114cf11afSPaul Mackerras {
682ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
683ba12eedeSLi Zhong 
68414cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
68514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
686ba12eedeSLi Zhong 		goto bail;
68714cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
688ba12eedeSLi Zhong 		goto bail;
68914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
690ba12eedeSLi Zhong 
691ba12eedeSLi Zhong bail:
692ba12eedeSLi Zhong 	exception_exit(prev_state);
69314cf11afSPaul Mackerras }
69414cf11afSPaul Mackerras 
69514cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
69614cf11afSPaul Mackerras {
69714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
69814cf11afSPaul Mackerras }
69914cf11afSPaul Mackerras 
70003465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
70114cf11afSPaul Mackerras {
702ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
703ba12eedeSLi Zhong 
7042538c2d0SK.Prasad 	clear_single_step(regs);
70514cf11afSPaul Mackerras 
7066cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
7076cc89badSNaveen N. Rao 		return;
7086cc89badSNaveen N. Rao 
70914cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
71014cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
711ba12eedeSLi Zhong 		goto bail;
71214cf11afSPaul Mackerras 	if (debugger_sstep(regs))
713ba12eedeSLi Zhong 		goto bail;
71414cf11afSPaul Mackerras 
71514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
716ba12eedeSLi Zhong 
717ba12eedeSLi Zhong bail:
718ba12eedeSLi Zhong 	exception_exit(prev_state);
71914cf11afSPaul Mackerras }
72003465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
72114cf11afSPaul Mackerras 
72214cf11afSPaul Mackerras /*
72314cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
72414cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
72514cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
72614cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
72714cf11afSPaul Mackerras  */
7288dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
72914cf11afSPaul Mackerras {
7302538c2d0SK.Prasad 	if (single_stepping(regs))
7312538c2d0SK.Prasad 		single_step_exception(regs);
73214cf11afSPaul Mackerras }
73314cf11afSPaul Mackerras 
7345fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
735dc1c1ca3SStephen Rothwell {
7365fad293bSKumar Gala 	int ret = 0;
737dc1c1ca3SStephen Rothwell 
738dc1c1ca3SStephen Rothwell 	/* Invalid operation */
739dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7405fad293bSKumar Gala 		ret = FPE_FLTINV;
741dc1c1ca3SStephen Rothwell 
742dc1c1ca3SStephen Rothwell 	/* Overflow */
743dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7445fad293bSKumar Gala 		ret = FPE_FLTOVF;
745dc1c1ca3SStephen Rothwell 
746dc1c1ca3SStephen Rothwell 	/* Underflow */
747dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7485fad293bSKumar Gala 		ret = FPE_FLTUND;
749dc1c1ca3SStephen Rothwell 
750dc1c1ca3SStephen Rothwell 	/* Divide by zero */
751dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7525fad293bSKumar Gala 		ret = FPE_FLTDIV;
753dc1c1ca3SStephen Rothwell 
754dc1c1ca3SStephen Rothwell 	/* Inexact result */
755dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7565fad293bSKumar Gala 		ret = FPE_FLTRES;
7575fad293bSKumar Gala 
7585fad293bSKumar Gala 	return ret;
7595fad293bSKumar Gala }
7605fad293bSKumar Gala 
7615fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7625fad293bSKumar Gala {
7635fad293bSKumar Gala 	int code = 0;
7645fad293bSKumar Gala 
7655fad293bSKumar Gala 	flush_fp_to_thread(current);
7665fad293bSKumar Gala 
767de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
768dc1c1ca3SStephen Rothwell 
769dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
770dc1c1ca3SStephen Rothwell }
771dc1c1ca3SStephen Rothwell 
772dc1c1ca3SStephen Rothwell /*
773dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
77414cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
77514cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
77614cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
77714cf11afSPaul Mackerras  *
77814cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
77914cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
78014cf11afSPaul Mackerras  * bits is faster and easier.
78186417780SPaul Mackerras  *
78214cf11afSPaul Mackerras  */
78314cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
78414cf11afSPaul Mackerras {
78514cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
78614cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
78714cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
78814cf11afSPaul Mackerras 	u32 num_bytes;
78914cf11afSPaul Mackerras 	unsigned long EA;
79014cf11afSPaul Mackerras 	int pos = 0;
79114cf11afSPaul Mackerras 
79214cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
79316c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
79414cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
79514cf11afSPaul Mackerras 			return -EINVAL;
79614cf11afSPaul Mackerras 
79714cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
79814cf11afSPaul Mackerras 
79916c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
80016c57b36SKumar Gala 		case PPC_INST_LSWX:
80116c57b36SKumar Gala 		case PPC_INST_STSWX:
80214cf11afSPaul Mackerras 			EA += NB_RB;
80314cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
80414cf11afSPaul Mackerras 			break;
80516c57b36SKumar Gala 		case PPC_INST_LSWI:
80616c57b36SKumar Gala 		case PPC_INST_STSWI:
80714cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
80814cf11afSPaul Mackerras 			break;
80914cf11afSPaul Mackerras 		default:
81014cf11afSPaul Mackerras 			return -EINVAL;
81114cf11afSPaul Mackerras 	}
81214cf11afSPaul Mackerras 
81314cf11afSPaul Mackerras 	while (num_bytes != 0)
81414cf11afSPaul Mackerras 	{
81514cf11afSPaul Mackerras 		u8 val;
81614cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
81714cf11afSPaul Mackerras 
81880aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
81980aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
82080aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
82180aa0fb4SJames Yang 
82216c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
82316c57b36SKumar Gala 			case PPC_INST_LSWX:
82416c57b36SKumar Gala 			case PPC_INST_LSWI:
82514cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
82614cf11afSPaul Mackerras 					return -EFAULT;
82714cf11afSPaul Mackerras 				/* first time updating this reg,
82814cf11afSPaul Mackerras 				 * zero it out */
82914cf11afSPaul Mackerras 				if (pos == 0)
83014cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
83114cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
83214cf11afSPaul Mackerras 				break;
83316c57b36SKumar Gala 			case PPC_INST_STSWI:
83416c57b36SKumar Gala 			case PPC_INST_STSWX:
83514cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
83614cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
83714cf11afSPaul Mackerras 					return -EFAULT;
83814cf11afSPaul Mackerras 				break;
83914cf11afSPaul Mackerras 		}
84014cf11afSPaul Mackerras 		/* move EA to next address */
84114cf11afSPaul Mackerras 		EA += 1;
84214cf11afSPaul Mackerras 		num_bytes--;
84314cf11afSPaul Mackerras 
84414cf11afSPaul Mackerras 		/* manage our position within the register */
84514cf11afSPaul Mackerras 		if (++pos == 4) {
84614cf11afSPaul Mackerras 			pos = 0;
84714cf11afSPaul Mackerras 			if (++rT == 32)
84814cf11afSPaul Mackerras 				rT = 0;
84914cf11afSPaul Mackerras 		}
85014cf11afSPaul Mackerras 	}
85114cf11afSPaul Mackerras 
85214cf11afSPaul Mackerras 	return 0;
85314cf11afSPaul Mackerras }
85414cf11afSPaul Mackerras 
855c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
856c3412dcbSWill Schmidt {
857c3412dcbSWill Schmidt 	u32 ra,rs;
858c3412dcbSWill Schmidt 	unsigned long tmp;
859c3412dcbSWill Schmidt 
860c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
861c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
862c3412dcbSWill Schmidt 
863c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
864c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
865c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
866c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
867c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
868c3412dcbSWill Schmidt 
869c3412dcbSWill Schmidt 	return 0;
870c3412dcbSWill Schmidt }
871c3412dcbSWill Schmidt 
872c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
873c1469f13SKumar Gala {
874c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
875c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
876c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
877c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
878c1469f13SKumar Gala 	u8 bit;
879c1469f13SKumar Gala 	unsigned long tmp;
880c1469f13SKumar Gala 
881c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
882c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
883c1469f13SKumar Gala 
884c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
885c1469f13SKumar Gala 
886c1469f13SKumar Gala 	return 0;
887c1469f13SKumar Gala }
888c1469f13SKumar Gala 
8896ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
8906ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
8916ce6c629SMichael Neuling {
8926ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
8936ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
8946ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
8956ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
8966ce6c629SMichael Neuling 	 */
8976ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
8986ce6c629SMichael Neuling 		tm_enable();
8996ce6c629SMichael Neuling 		tm_abort(cause);
9006ce6c629SMichael Neuling 		return true;
9016ce6c629SMichael Neuling 	}
9026ce6c629SMichael Neuling 	return false;
9036ce6c629SMichael Neuling }
9046ce6c629SMichael Neuling #else
9056ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
9066ce6c629SMichael Neuling {
9076ce6c629SMichael Neuling 	return false;
9086ce6c629SMichael Neuling }
9096ce6c629SMichael Neuling #endif
9106ce6c629SMichael Neuling 
91114cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
91214cf11afSPaul Mackerras {
91314cf11afSPaul Mackerras 	u32 instword;
91414cf11afSPaul Mackerras 	u32 rd;
91514cf11afSPaul Mackerras 
9164288e343SAnton Blanchard 	if (!user_mode(regs))
91714cf11afSPaul Mackerras 		return -EINVAL;
91814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
91914cf11afSPaul Mackerras 
92014cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
92114cf11afSPaul Mackerras 		return -EFAULT;
92214cf11afSPaul Mackerras 
92314cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
92416c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
925eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
92614cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
92714cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
92814cf11afSPaul Mackerras 		return 0;
92914cf11afSPaul Mackerras 	}
93014cf11afSPaul Mackerras 
93114cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
93280947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
933eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
93414cf11afSPaul Mackerras 		return 0;
93580947e7cSGeert Uytterhoeven 	}
93614cf11afSPaul Mackerras 
93714cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
93816c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
93986417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
94014cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
94114cf11afSPaul Mackerras 
942eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
94314cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
94414cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
94514cf11afSPaul Mackerras 		return 0;
94614cf11afSPaul Mackerras 	}
94714cf11afSPaul Mackerras 
94814cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
94980947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
9506ce6c629SMichael Neuling 		if (tm_abort_check(regs,
9516ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
9526ce6c629SMichael Neuling 			return -EINVAL;
953eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
95414cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
95580947e7cSGeert Uytterhoeven 	}
95614cf11afSPaul Mackerras 
957c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
95816c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
959eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
960c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
961c3412dcbSWill Schmidt 	}
962c3412dcbSWill Schmidt 
963c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
96416c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
965eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
966c1469f13SKumar Gala 		return emulate_isel(regs, instword);
967c1469f13SKumar Gala 	}
968c1469f13SKumar Gala 
9699863c28aSJames Yang 	/* Emulate sync instruction variants */
9709863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
9719863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
9729863c28aSJames Yang 		asm volatile("sync");
9739863c28aSJames Yang 		return 0;
9749863c28aSJames Yang 	}
9759863c28aSJames Yang 
976efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
977efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
97873d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
97973d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
98073d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
98173d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
982efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
983efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
984efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
985efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
986efcac658SAlexey Kardashevskiy 		return 0;
987efcac658SAlexey Kardashevskiy 	}
988efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
98973d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
99073d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
99173d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
99273d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
993efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
994efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
995efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
99600ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
997efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
99800ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
999efcac658SAlexey Kardashevskiy 		return 0;
1000efcac658SAlexey Kardashevskiy 	}
1001efcac658SAlexey Kardashevskiy #endif
1002efcac658SAlexey Kardashevskiy 
100314cf11afSPaul Mackerras 	return -EINVAL;
100414cf11afSPaul Mackerras }
100514cf11afSPaul Mackerras 
100673c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
100714cf11afSPaul Mackerras {
100873c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
100914cf11afSPaul Mackerras }
101014cf11afSPaul Mackerras 
10113a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
10123a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
10133a3b5aa6SKevin Hao {
10143a3b5aa6SKevin Hao 	int ret;
10153a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
10163a3b5aa6SKevin Hao 
10173a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
10183a3b5aa6SKevin Hao 	if (ret >= 0)
10193a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
10203a3b5aa6SKevin Hao 
10213a3b5aa6SKevin Hao 	switch (ret) {
10223a3b5aa6SKevin Hao 	case 0:
10233a3b5aa6SKevin Hao 		emulate_single_step(regs);
10243a3b5aa6SKevin Hao 		return 0;
10253a3b5aa6SKevin Hao 	case 1: {
10263a3b5aa6SKevin Hao 			int code = 0;
1027de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
10283a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
10293a3b5aa6SKevin Hao 			return 0;
10303a3b5aa6SKevin Hao 		}
10313a3b5aa6SKevin Hao 	case -EFAULT:
10323a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10333a3b5aa6SKevin Hao 		return 0;
10343a3b5aa6SKevin Hao 	}
10353a3b5aa6SKevin Hao 
10363a3b5aa6SKevin Hao 	return -1;
10373a3b5aa6SKevin Hao }
10383a3b5aa6SKevin Hao #else
10393a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
10403a3b5aa6SKevin Hao #endif
10413a3b5aa6SKevin Hao 
104203465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
104314cf11afSPaul Mackerras {
1044ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
104514cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
104614cf11afSPaul Mackerras 
1047aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
104804903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
104914cf11afSPaul Mackerras 
105014cf11afSPaul Mackerras 	if (reason & REASON_FP) {
105114cf11afSPaul Mackerras 		/* IEEE FP exception */
1052dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1053ba12eedeSLi Zhong 		goto bail;
10548dad3f92SPaul Mackerras 	}
10558dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1056a4c3f909SBalbir Singh 		unsigned long bugaddr;
1057ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1058ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1059ba797b28SJason Wessel 		if (debugger_bpt(regs))
1060ba12eedeSLi Zhong 			goto bail;
1061ba797b28SJason Wessel 
10626cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
10636cc89badSNaveen N. Rao 			goto bail;
10646cc89badSNaveen N. Rao 
106514cf11afSPaul Mackerras 		/* trap exception */
1066dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1067dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1068ba12eedeSLi Zhong 			goto bail;
106973c9ceabSJeremy Fitzhardinge 
1070a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1071a4c3f909SBalbir Singh 		/*
1072a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1073a4c3f909SBalbir Singh 		 */
1074a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1075a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1076a4c3f909SBalbir Singh 
107773c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1078a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
107914cf11afSPaul Mackerras 			regs->nip += 4;
1080ba12eedeSLi Zhong 			goto bail;
108114cf11afSPaul Mackerras 		}
10828dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1083ba12eedeSLi Zhong 		goto bail;
10848dad3f92SPaul Mackerras 	}
1085bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1086bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1087bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1088bc2a9408SMichael Neuling 		 * This occurs when:
1089bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1090bc2a9408SMichael Neuling 		 *    transition in TM states.
1091bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1092bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1093bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1094bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1095bc2a9408SMichael Neuling 		 */
1096bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1097bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1098bc2a9408SMichael Neuling 			regs->nip += 4;
1099ba12eedeSLi Zhong 			goto bail;
1100bc2a9408SMichael Neuling 		}
1101bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1102bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1103bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1104bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1105bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1106bc2a9408SMichael Neuling 		 */
1107bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1108bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1109ba12eedeSLi Zhong 			goto bail;
1110bc2a9408SMichael Neuling 		} else {
1111bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1112bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1113bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1114bc2a9408SMichael Neuling 		}
1115bc2a9408SMichael Neuling 	}
1116bc2a9408SMichael Neuling #endif
11178dad3f92SPaul Mackerras 
1118b3f6a459SMichael Ellerman 	/*
1119b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1120b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1121b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1122b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1123b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1124b3f6a459SMichael Ellerman 	 */
1125b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1126b3f6a459SMichael Ellerman 		goto sigill;
1127b3f6a459SMichael Ellerman 
1128a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1129a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1130cd8a5673SPaul Mackerras 		local_irq_enable();
1131cd8a5673SPaul Mackerras 
113204903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
113304903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
113404903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
113504903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
113604903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
113704903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
11384e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
11394e63f8edSBenjamin Herrenschmidt 	 */
11403a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1141ba12eedeSLi Zhong 		goto bail;
114204903a30SKumar Gala 
11438dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
11448dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
114514cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
114614cf11afSPaul Mackerras 		case 0:
114714cf11afSPaul Mackerras 			regs->nip += 4;
114814cf11afSPaul Mackerras 			emulate_single_step(regs);
1149ba12eedeSLi Zhong 			goto bail;
115014cf11afSPaul Mackerras 		case -EFAULT:
115114cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1152ba12eedeSLi Zhong 			goto bail;
11538dad3f92SPaul Mackerras 		}
11548dad3f92SPaul Mackerras 	}
11558dad3f92SPaul Mackerras 
1156b3f6a459SMichael Ellerman sigill:
115714cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
115814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
115914cf11afSPaul Mackerras 	else
116014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1161ba12eedeSLi Zhong 
1162ba12eedeSLi Zhong bail:
1163ba12eedeSLi Zhong 	exception_exit(prev_state);
116414cf11afSPaul Mackerras }
116503465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
116614cf11afSPaul Mackerras 
1167bf593907SPaul Mackerras /*
1168bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1169bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1170bf593907SPaul Mackerras  */
117103465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1172bf593907SPaul Mackerras {
1173bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1174bf593907SPaul Mackerras 	program_check_exception(regs);
1175bf593907SPaul Mackerras }
117603465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1177bf593907SPaul Mackerras 
1178dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
117914cf11afSPaul Mackerras {
1180ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
11814393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
118214cf11afSPaul Mackerras 
1183a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1184a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1185a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1186a3512b2dSBenjamin Herrenschmidt 
11876ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
11886ce6c629SMichael Neuling 		goto bail;
11896ce6c629SMichael Neuling 
1190e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1191e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
119214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
119314cf11afSPaul Mackerras 
119414cf11afSPaul Mackerras 	if (fixed == 1) {
119514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
119614cf11afSPaul Mackerras 		emulate_single_step(regs);
1197ba12eedeSLi Zhong 		goto bail;
119814cf11afSPaul Mackerras 	}
119914cf11afSPaul Mackerras 
120014cf11afSPaul Mackerras 	/* Operand address was bad */
120114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12024393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12034393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12044393c4f6SBenjamin Herrenschmidt 	} else {
12054393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12064393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
120714cf11afSPaul Mackerras 	}
12084393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12094393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
12104393c4f6SBenjamin Herrenschmidt 	else
12114393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1212ba12eedeSLi Zhong 
1213ba12eedeSLi Zhong bail:
1214ba12eedeSLi Zhong 	exception_exit(prev_state);
121514cf11afSPaul Mackerras }
121614cf11afSPaul Mackerras 
1217f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1218f0f558b1SPaul Mackerras {
1219f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1220f0f558b1SPaul Mackerras 
1221f0f558b1SPaul Mackerras 	if (user_mode(regs))
1222f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1223f0f558b1SPaul Mackerras 	else
1224f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1225f0f558b1SPaul Mackerras 
1226f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1227f0f558b1SPaul Mackerras }
1228f0f558b1SPaul Mackerras 
122914cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
123014cf11afSPaul Mackerras {
123114cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
123214cf11afSPaul Mackerras 	       current, regs->gpr[1]);
123314cf11afSPaul Mackerras 	debugger(regs);
123414cf11afSPaul Mackerras 	show_regs(regs);
123514cf11afSPaul Mackerras 	panic("kernel stack overflow");
123614cf11afSPaul Mackerras }
123714cf11afSPaul Mackerras 
123814cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
123914cf11afSPaul Mackerras {
124014cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
124114cf11afSPaul Mackerras 	       regs->nip, regs->msr);
124214cf11afSPaul Mackerras 	debugger(regs);
124314cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
124414cf11afSPaul Mackerras }
124514cf11afSPaul Mackerras 
1246dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1247dc1c1ca3SStephen Rothwell {
1248ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1249ba12eedeSLi Zhong 
1250dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1251dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1252dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1253ba12eedeSLi Zhong 
1254ba12eedeSLi Zhong 	exception_exit(prev_state);
1255dc1c1ca3SStephen Rothwell }
1256dc1c1ca3SStephen Rothwell 
1257dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1258dc1c1ca3SStephen Rothwell {
1259ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1260ba12eedeSLi Zhong 
1261dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1262dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1263dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1264dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1265ba12eedeSLi Zhong 		goto bail;
1266dc1c1ca3SStephen Rothwell 	}
12676c4841c2SAnton Blanchard 
1268dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1269dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1270dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1271ba12eedeSLi Zhong 
1272ba12eedeSLi Zhong bail:
1273ba12eedeSLi Zhong 	exception_exit(prev_state);
1274dc1c1ca3SStephen Rothwell }
1275dc1c1ca3SStephen Rothwell 
1276ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1277ce48b210SMichael Neuling {
1278ce48b210SMichael Neuling 	if (user_mode(regs)) {
1279ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1280ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1281ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1282ce48b210SMichael Neuling 		return;
1283ce48b210SMichael Neuling 	}
1284ce48b210SMichael Neuling 
1285ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1286ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1287ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1288ce48b210SMichael Neuling }
1289ce48b210SMichael Neuling 
12902517617eSMichael Neuling #ifdef CONFIG_PPC64
1291172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1292172f7aaaSCyril Bur {
12935d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
12945d176f75SCyril Bur 	if (user_mode(regs)) {
12955d176f75SCyril Bur 		current->thread.load_tm++;
12965d176f75SCyril Bur 		regs->msr |= MSR_TM;
12975d176f75SCyril Bur 		tm_enable();
12985d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
12995d176f75SCyril Bur 		return;
13005d176f75SCyril Bur 	}
13015d176f75SCyril Bur #endif
1302172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1303172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1304172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1305172f7aaaSCyril Bur }
1306172f7aaaSCyril Bur 
1307021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1308d0c0c9a1SMichael Neuling {
1309021424a1SMichael Ellerman 	static char *facility_strings[] = {
13102517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13112517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13122517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13132517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13142517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
13152517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
13162517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
13172517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1318794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
13199b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1320021424a1SMichael Ellerman 	};
13212517617eSMichael Neuling 	char *facility = "unknown";
1322021424a1SMichael Ellerman 	u64 value;
1323c952c1c4SAnshuman Khandual 	u32 instword, rd;
13242517617eSMichael Neuling 	u8 status;
13252517617eSMichael Neuling 	bool hv;
1326021424a1SMichael Ellerman 
13272517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
13282517617eSMichael Neuling 	if (hv)
1329b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
13302517617eSMichael Neuling 	else
13312517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
13322517617eSMichael Neuling 
13332517617eSMichael Neuling 	status = value >> 56;
13342517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1335c952c1c4SAnshuman Khandual 		/*
1336c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1337c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1338c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1339c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1340c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1341c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1342c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1343c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1344c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1345c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1346c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1347c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1348c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1349c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
13502517617eSMichael Neuling 		 */
1351c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1352c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1353c952c1c4SAnshuman Khandual 			return;
1354c952c1c4SAnshuman Khandual 		}
1355c952c1c4SAnshuman Khandual 
1356c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1357c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1358c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1359c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1360c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
13612517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1362b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1363b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1364c952c1c4SAnshuman Khandual 		}
1365c952c1c4SAnshuman Khandual 
1366c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1367c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1368c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1369c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1370c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1371c952c1c4SAnshuman Khandual 				return;
1372c952c1c4SAnshuman Khandual 			}
1373c952c1c4SAnshuman Khandual 			regs->nip += 4;
1374c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1375c952c1c4SAnshuman Khandual 		}
13762517617eSMichael Neuling 		return;
1377b14b6260SMichael Ellerman 	}
1378b14b6260SMichael Ellerman 
1379172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1380172f7aaaSCyril Bur 		/*
1381172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1382172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1383172f7aaaSCyril Bur 		 *
1384172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1385172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1386172f7aaaSCyril Bur 		 * support.
1387172f7aaaSCyril Bur 		 *
1388172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1389172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1390172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1391172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1392172f7aaaSCyril Bur 		 */
1393172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1394172f7aaaSCyril Bur 			goto out;
1395172f7aaaSCyril Bur 
1396172f7aaaSCyril Bur 		tm_unavailable(regs);
1397172f7aaaSCyril Bur 		return;
1398172f7aaaSCyril Bur 	}
1399172f7aaaSCyril Bur 
140093c2ec0fSBalbir Singh 	if ((hv || status >= 2) &&
140193c2ec0fSBalbir Singh 	    (status < ARRAY_SIZE(facility_strings)) &&
14022517617eSMichael Neuling 	    facility_strings[status])
14032517617eSMichael Neuling 		facility = facility_strings[status];
1404021424a1SMichael Ellerman 
1405d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1406d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1407d0c0c9a1SMichael Neuling 		local_irq_enable();
1408d0c0c9a1SMichael Neuling 
140993c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
141093c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1411d0c0c9a1SMichael Neuling 
1412172f7aaaSCyril Bur out:
1413d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1414d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1415d0c0c9a1SMichael Neuling 		return;
1416d0c0c9a1SMichael Neuling 	}
1417d0c0c9a1SMichael Neuling 
1418021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1419d0c0c9a1SMichael Neuling }
14202517617eSMichael Neuling #endif
1421d0c0c9a1SMichael Neuling 
1422f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1423f54db641SMichael Neuling 
1424f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1425f54db641SMichael Neuling {
1426f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1427f54db641SMichael Neuling 
1428f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1429f54db641SMichael Neuling 		 regs->nip, regs->msr);
1430f54db641SMichael Neuling 
1431f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1432f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1433f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1434f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1435f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1436f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1437f54db641SMichael Neuling 	 */
1438d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1439f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1440f54db641SMichael Neuling 
1441f54db641SMichael Neuling 	/* Enable FP for the task: */
1442f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1443f54db641SMichael Neuling 
1444f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1445f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1446f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
14473ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
14483ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1449f54db641SMichael Neuling 	 */
14503ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
14513ac8ff1cSPaul Mackerras 
14523ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
14533ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
1454dc310669SCyril Bur 		msr_check_and_set(MSR_VEC);
1455dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
14563ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
14573ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14583ac8ff1cSPaul Mackerras 	}
1459f54db641SMichael Neuling }
1460f54db641SMichael Neuling 
1461f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1462f54db641SMichael Neuling {
1463f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1464f54db641SMichael Neuling 	 * the same way.
1465f54db641SMichael Neuling 	 */
1466f54db641SMichael Neuling 
1467f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1468f54db641SMichael Neuling 		 "MSR=%lx\n",
1469f54db641SMichael Neuling 		 regs->nip, regs->msr);
1470d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1471f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
14723ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1473f54db641SMichael Neuling 	current->thread.used_vr = 1;
1474f54db641SMichael Neuling 
14753ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
1476dc310669SCyril Bur 		msr_check_and_set(MSR_FP);
1477dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
14783ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14793ac8ff1cSPaul Mackerras 	}
14803ac8ff1cSPaul Mackerras }
14813ac8ff1cSPaul Mackerras 
1482f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1483f54db641SMichael Neuling {
14843ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
14853ac8ff1cSPaul Mackerras 
1486f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1487f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1488f54db641SMichael Neuling 	 *
1489f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1490f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1491f54db641SMichael Neuling 	 */
1492f54db641SMichael Neuling 
1493f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1494f54db641SMichael Neuling 		 "MSR=%lx\n",
1495f54db641SMichael Neuling 		 regs->nip, regs->msr);
1496f54db641SMichael Neuling 
14973ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
14983ac8ff1cSPaul Mackerras 
14993ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
15003ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
15013ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15023ac8ff1cSPaul Mackerras 		return;
15033ac8ff1cSPaul Mackerras 	}
15043ac8ff1cSPaul Mackerras 
1505f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1506d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1507f54db641SMichael Neuling 
1508f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1509f54db641SMichael Neuling 		MSR_VSX;
15103ac8ff1cSPaul Mackerras 
15113ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
15123ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
15133ac8ff1cSPaul Mackerras 	 */
15143ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
15153ac8ff1cSPaul Mackerras 
1516dc310669SCyril Bur 	msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1517dc310669SCyril Bur 
15183ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
1519dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
15203ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
1521dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
1522f54db641SMichael Neuling }
1523f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1524f54db641SMichael Neuling 
1525dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1526dc1c1ca3SStephen Rothwell {
152769111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
152889713ed1SAnton Blanchard 
1529dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1530dc1c1ca3SStephen Rothwell }
1531dc1c1ca3SStephen Rothwell 
1532172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
15333bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
15343bffb652SDave Kleikamp {
15353bffb652SDave Kleikamp 	int changed = 0;
15363bffb652SDave Kleikamp 	/*
15373bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
15383bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
15393bffb652SDave Kleikamp 	 */
15403bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
15413bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
15423bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
154351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
15443bffb652SDave Kleikamp #endif
15453bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
15463bffb652SDave Kleikamp 			     5);
15473bffb652SDave Kleikamp 		changed |= 0x01;
15483bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
15493bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
15503bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
15513bffb652SDave Kleikamp 			     6);
15523bffb652SDave Kleikamp 		changed |= 0x01;
15533bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
155451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
15553bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
15563bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
15573bffb652SDave Kleikamp 			     1);
15583bffb652SDave Kleikamp 		changed |= 0x01;
15593bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
156051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
15613bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
15623bffb652SDave Kleikamp 			     2);
15633bffb652SDave Kleikamp 		changed |= 0x01;
15643bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
156551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
15663bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
15673bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
15683bffb652SDave Kleikamp 			     3);
15693bffb652SDave Kleikamp 		changed |= 0x01;
15703bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
157151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
15723bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
15733bffb652SDave Kleikamp 			     4);
15743bffb652SDave Kleikamp 		changed |= 0x01;
15753bffb652SDave Kleikamp 	}
15763bffb652SDave Kleikamp 	/*
15773bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
15783bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
15793bffb652SDave Kleikamp 	 * back on or not.
15803bffb652SDave Kleikamp 	 */
158151ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
158251ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
15833bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
15843bffb652SDave Kleikamp 	else
15853bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
158651ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
15873bffb652SDave Kleikamp 
15883bffb652SDave Kleikamp 	if (changed & 0x01)
158951ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
15903bffb652SDave Kleikamp }
159114cf11afSPaul Mackerras 
159203465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
159314cf11afSPaul Mackerras {
159451ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
15953bffb652SDave Kleikamp 
1596ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1597ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1598ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1599ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1600ec097c84SRoland McGrath 	 */
1601ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1602ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1603ec097c84SRoland McGrath 
1604ec097c84SRoland McGrath 		/* Disable BT */
1605ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1606ec097c84SRoland McGrath 		/* Clear the BT event */
1607ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1608ec097c84SRoland McGrath 
1609ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1610ec097c84SRoland McGrath 		if (user_mode(regs)) {
161151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
161251ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1613ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1614ec097c84SRoland McGrath 			return;
1615ec097c84SRoland McGrath 		}
1616ec097c84SRoland McGrath 
16176cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
16186cc89badSNaveen N. Rao 			return;
16196cc89badSNaveen N. Rao 
1620ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1621ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1622ec097c84SRoland McGrath 			return;
1623ec097c84SRoland McGrath 		}
1624ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1625ec097c84SRoland McGrath 			return;
1626ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
162714cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1628f8279621SKumar Gala 
162914cf11afSPaul Mackerras 		/* Disable instruction completion */
163014cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
163114cf11afSPaul Mackerras 		/* Clear the instruction completion event */
163214cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1633f8279621SKumar Gala 
16346cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
16356cc89badSNaveen N. Rao 			return;
16366cc89badSNaveen N. Rao 
1637f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1638f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
163914cf11afSPaul Mackerras 			return;
164014cf11afSPaul Mackerras 		}
1641f8279621SKumar Gala 
1642f8279621SKumar Gala 		if (debugger_sstep(regs))
1643f8279621SKumar Gala 			return;
1644f8279621SKumar Gala 
16453bffb652SDave Kleikamp 		if (user_mode(regs)) {
164651ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
164751ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
164851ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
16493bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
16503bffb652SDave Kleikamp 			else
16513bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
165251ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16533bffb652SDave Kleikamp 		}
1654f8279621SKumar Gala 
1655f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
16563bffb652SDave Kleikamp 	} else
16573bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
165814cf11afSPaul Mackerras }
165903465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1660172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
166114cf11afSPaul Mackerras 
166214cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
166314cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
166414cf11afSPaul Mackerras {
166514cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
166614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
166714cf11afSPaul Mackerras }
166814cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
166914cf11afSPaul Mackerras 
167014cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1671dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
167214cf11afSPaul Mackerras {
167314cf11afSPaul Mackerras 	int err;
167414cf11afSPaul Mackerras 
167514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
167614cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
167714cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
16788dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
167914cf11afSPaul Mackerras 	}
168014cf11afSPaul Mackerras 
1681dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1682dc1c1ca3SStephen Rothwell 
1683eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
168414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
168514cf11afSPaul Mackerras 	if (err == 0) {
168614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
168714cf11afSPaul Mackerras 		emulate_single_step(regs);
168814cf11afSPaul Mackerras 		return;
168914cf11afSPaul Mackerras 	}
169014cf11afSPaul Mackerras 
169114cf11afSPaul Mackerras 	if (err == -EFAULT) {
169214cf11afSPaul Mackerras 		/* got an error reading the instruction */
169314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
169414cf11afSPaul Mackerras 	} else {
169514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
169614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
169776462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
169814cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1699de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
170014cf11afSPaul Mackerras 	}
170114cf11afSPaul Mackerras }
170214cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
170314cf11afSPaul Mackerras 
170414cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
170514cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
170614cf11afSPaul Mackerras 			   unsigned long error_code)
170714cf11afSPaul Mackerras {
170814cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
170914cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
171014cf11afSPaul Mackerras 	 * something smarter
171114cf11afSPaul Mackerras 	 */
171214cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
171314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
171414cf11afSPaul Mackerras 	return;
171514cf11afSPaul Mackerras }
171614cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
171714cf11afSPaul Mackerras 
171814cf11afSPaul Mackerras #ifdef CONFIG_SPE
171914cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
172014cf11afSPaul Mackerras {
17216a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
172214cf11afSPaul Mackerras 	unsigned long spefscr;
172314cf11afSPaul Mackerras 	int fpexc_mode;
172414cf11afSPaul Mackerras 	int code = 0;
17256a800f36SLiu Yu 	int err;
17266a800f36SLiu Yu 
1727685659eeSyu liu 	flush_spe_to_thread(current);
172814cf11afSPaul Mackerras 
172914cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
173014cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
173114cf11afSPaul Mackerras 
173214cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
173314cf11afSPaul Mackerras 		code = FPE_FLTOVF;
173414cf11afSPaul Mackerras 	}
173514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
173614cf11afSPaul Mackerras 		code = FPE_FLTUND;
173714cf11afSPaul Mackerras 	}
173814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
173914cf11afSPaul Mackerras 		code = FPE_FLTDIV;
174014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
174114cf11afSPaul Mackerras 		code = FPE_FLTINV;
174214cf11afSPaul Mackerras 	}
174314cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
174414cf11afSPaul Mackerras 		code = FPE_FLTRES;
174514cf11afSPaul Mackerras 
17466a800f36SLiu Yu 	err = do_spe_mathemu(regs);
17476a800f36SLiu Yu 	if (err == 0) {
17486a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17496a800f36SLiu Yu 		emulate_single_step(regs);
175014cf11afSPaul Mackerras 		return;
175114cf11afSPaul Mackerras 	}
17526a800f36SLiu Yu 
17536a800f36SLiu Yu 	if (err == -EFAULT) {
17546a800f36SLiu Yu 		/* got an error reading the instruction */
17556a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17566a800f36SLiu Yu 	} else if (err == -EINVAL) {
17576a800f36SLiu Yu 		/* didn't recognize the instruction */
17586a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17596a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17606a800f36SLiu Yu 	} else {
17616a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
17626a800f36SLiu Yu 	}
17636a800f36SLiu Yu 
17646a800f36SLiu Yu 	return;
17656a800f36SLiu Yu }
17666a800f36SLiu Yu 
17676a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
17686a800f36SLiu Yu {
17696a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
17706a800f36SLiu Yu 	int err;
17716a800f36SLiu Yu 
17726a800f36SLiu Yu 	preempt_disable();
17736a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
17746a800f36SLiu Yu 		giveup_spe(current);
17756a800f36SLiu Yu 	preempt_enable();
17766a800f36SLiu Yu 
17776a800f36SLiu Yu 	regs->nip -= 4;
17786a800f36SLiu Yu 	err = speround_handler(regs);
17796a800f36SLiu Yu 	if (err == 0) {
17806a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17816a800f36SLiu Yu 		emulate_single_step(regs);
17826a800f36SLiu Yu 		return;
17836a800f36SLiu Yu 	}
17846a800f36SLiu Yu 
17856a800f36SLiu Yu 	if (err == -EFAULT) {
17866a800f36SLiu Yu 		/* got an error reading the instruction */
17876a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17886a800f36SLiu Yu 	} else if (err == -EINVAL) {
17896a800f36SLiu Yu 		/* didn't recognize the instruction */
17906a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17916a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17926a800f36SLiu Yu 	} else {
17936a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
17946a800f36SLiu Yu 		return;
17956a800f36SLiu Yu 	}
17966a800f36SLiu Yu }
179714cf11afSPaul Mackerras #endif
179814cf11afSPaul Mackerras 
1799dc1c1ca3SStephen Rothwell /*
1800dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1801dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1802dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1803dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1804dc1c1ca3SStephen Rothwell  */
1805dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1806dc1c1ca3SStephen Rothwell {
1807dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1808dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1809dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1810dc1c1ca3SStephen Rothwell }
181115770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
1812dc1c1ca3SStephen Rothwell 
18131e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
181414cf11afSPaul Mackerras /*
181514cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
181614cf11afSPaul Mackerras  * spins until a reboot occurs
181714cf11afSPaul Mackerras  */
181814cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
181914cf11afSPaul Mackerras {
182014cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
182114cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
182214cf11afSPaul Mackerras 	return;
182314cf11afSPaul Mackerras }
182414cf11afSPaul Mackerras 
182514cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
182614cf11afSPaul Mackerras {
182714cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
182814cf11afSPaul Mackerras 	WatchdogHandler(regs);
182914cf11afSPaul Mackerras }
183014cf11afSPaul Mackerras #endif
1831dc1c1ca3SStephen Rothwell 
1832dc1c1ca3SStephen Rothwell /*
1833dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1834dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1835dc1c1ca3SStephen Rothwell  */
1836dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1837dc1c1ca3SStephen Rothwell {
1838dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1839dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1840dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1841dc1c1ca3SStephen Rothwell }
184215770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
184314cf11afSPaul Mackerras 
184414cf11afSPaul Mackerras void __init trap_init(void)
184514cf11afSPaul Mackerras {
184614cf11afSPaul Mackerras }
184780947e7cSGeert Uytterhoeven 
184880947e7cSGeert Uytterhoeven 
184980947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
185080947e7cSGeert Uytterhoeven 
185180947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
185280947e7cSGeert Uytterhoeven 
185380947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
185480947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
185580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
185680947e7cSGeert Uytterhoeven #endif
185780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
185880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
185980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
186080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
186180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
186280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
186380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
186480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
186580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
186680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1867a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
186880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
186980947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
187080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
187180947e7cSGeert Uytterhoeven #endif
187280947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
187380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
187480947e7cSGeert Uytterhoeven #endif
1875efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1876efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1877efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1878f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
1879efcac658SAlexey Kardashevskiy #endif
188080947e7cSGeert Uytterhoeven };
188180947e7cSGeert Uytterhoeven 
188280947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
188380947e7cSGeert Uytterhoeven 
188480947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
188580947e7cSGeert Uytterhoeven {
188676462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
188780947e7cSGeert Uytterhoeven 			    type);
188880947e7cSGeert Uytterhoeven }
188980947e7cSGeert Uytterhoeven 
189080947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
189180947e7cSGeert Uytterhoeven {
189280947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
189380947e7cSGeert Uytterhoeven 	unsigned int i;
189480947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
189580947e7cSGeert Uytterhoeven 
189680947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
189780947e7cSGeert Uytterhoeven 		return -ENODEV;
189880947e7cSGeert Uytterhoeven 
189980947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
190080947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
190180947e7cSGeert Uytterhoeven 	if (!dir)
190280947e7cSGeert Uytterhoeven 		return -ENOMEM;
190380947e7cSGeert Uytterhoeven 
190480947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
190580947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
190680947e7cSGeert Uytterhoeven 	if (!d)
190780947e7cSGeert Uytterhoeven 		goto fail;
190880947e7cSGeert Uytterhoeven 
190980947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
191080947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
191180947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
191280947e7cSGeert Uytterhoeven 		if (!d)
191380947e7cSGeert Uytterhoeven 			goto fail;
191480947e7cSGeert Uytterhoeven 	}
191580947e7cSGeert Uytterhoeven 
191680947e7cSGeert Uytterhoeven 	return 0;
191780947e7cSGeert Uytterhoeven 
191880947e7cSGeert Uytterhoeven fail:
191980947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
192080947e7cSGeert Uytterhoeven 	return -ENOMEM;
192180947e7cSGeert Uytterhoeven }
192280947e7cSGeert Uytterhoeven 
192380947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
192480947e7cSGeert Uytterhoeven 
192580947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1926