xref: /linux/arch/powerpc/kernel/traps.c (revision 7f3f819e94415b7c6d9bcd2d818595504069eae4)
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);
205760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
206760ca4dcSAnton Blanchard 	printk("PREEMPT ");
207760ca4dcSAnton Blanchard #endif
208760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
209760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
210760ca4dcSAnton Blanchard #endif
211e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
212760ca4dcSAnton Blanchard 		printk("DEBUG_PAGEALLOC ");
213760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
214760ca4dcSAnton Blanchard 	printk("NUMA ");
215760ca4dcSAnton Blanchard #endif
216760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
217760ca4dcSAnton Blanchard 
218760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
219760ca4dcSAnton Blanchard 		return 1;
220760ca4dcSAnton Blanchard 
221760ca4dcSAnton Blanchard 	print_modules();
222760ca4dcSAnton Blanchard 	show_regs(regs);
22314cf11afSPaul Mackerras 
22414cf11afSPaul Mackerras 	return 0;
22514cf11afSPaul Mackerras }
22603465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
22714cf11afSPaul Mackerras 
228760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
229760ca4dcSAnton Blanchard {
2306f44b20eSNicholas Piggin 	unsigned long flags;
231760ca4dcSAnton Blanchard 
2326f44b20eSNicholas Piggin 	if (debugger(regs))
2336f44b20eSNicholas Piggin 		return;
2346f44b20eSNicholas Piggin 
2356f44b20eSNicholas Piggin 	flags = oops_begin(regs);
236760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
237760ca4dcSAnton Blanchard 		err = 0;
238760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
239760ca4dcSAnton Blanchard }
24015770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
241760ca4dcSAnton Blanchard 
24225baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
24325baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
24425baa35bSOleg Nesterov {
24525baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
24625baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
24725baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
24825baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
24925baa35bSOleg Nesterov }
25025baa35bSOleg Nesterov 
25114cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
25214cf11afSPaul Mackerras {
25314cf11afSPaul Mackerras 	siginfo_t info;
254d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
255d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
256d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
257d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
25814cf11afSPaul Mackerras 
25914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
260760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
26114cf11afSPaul Mackerras 		return;
262760ca4dcSAnton Blanchard 	}
263760ca4dcSAnton Blanchard 
264760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
26576462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
266d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
267d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
26814cf11afSPaul Mackerras 	}
26914cf11afSPaul Mackerras 
270a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2719f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2729f2f79e3SBenjamin Herrenschmidt 
27341ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
27414cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
27514cf11afSPaul Mackerras 	info.si_signo = signr;
27614cf11afSPaul Mackerras 	info.si_code = code;
27714cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
27814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
27914cf11afSPaul Mackerras }
28014cf11afSPaul Mackerras 
28114cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
28214cf11afSPaul Mackerras {
2832b4f3ac5SNicholas Piggin 	/*
2842b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
2852b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
2862b4f3ac5SNicholas Piggin 	 */
2872b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
2882b4f3ac5SNicholas Piggin 	if (!nested)
2892b4f3ac5SNicholas Piggin 		nmi_enter();
2902b4f3ac5SNicholas Piggin 
291ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
292ca41ad43SNicholas Piggin 
29314cf11afSPaul Mackerras 	/* See if any machine dependent calls */
294c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
295c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
296c4f3b52cSNicholas Piggin 			goto out;
297c902be71SArnd Bergmann 	}
29814cf11afSPaul Mackerras 
2998dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
30014cf11afSPaul Mackerras 
301c4f3b52cSNicholas Piggin out:
302c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
303c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
304c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
305c4f3b52cSNicholas Piggin 		panic("Unrecoverable nested System Reset");
306c4f3b52cSNicholas Piggin #endif
30714cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
30814cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
30914cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
31014cf11afSPaul Mackerras 
3112b4f3ac5SNicholas Piggin 	if (!nested)
3122b4f3ac5SNicholas Piggin 		nmi_exit();
3132b4f3ac5SNicholas Piggin 
31414cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
31514cf11afSPaul Mackerras }
3161e9b4507SMahesh Salgaonkar 
317f307939fSChristophe Leroy #ifdef CONFIG_PPC64
3181e9b4507SMahesh Salgaonkar /*
3191e9b4507SMahesh Salgaonkar  * This function is called in real mode. Strictly no printk's please.
3201e9b4507SMahesh Salgaonkar  *
3211e9b4507SMahesh Salgaonkar  * regs->nip and regs->msr contains srr0 and ssr1.
3221e9b4507SMahesh Salgaonkar  */
3231e9b4507SMahesh Salgaonkar long machine_check_early(struct pt_regs *regs)
3241e9b4507SMahesh Salgaonkar {
3254c703416SMahesh Salgaonkar 	long handled = 0;
3264c703416SMahesh Salgaonkar 
32769111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
328e6654d5bSMahesh Salgaonkar 
3294c703416SMahesh Salgaonkar 	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
3304c703416SMahesh Salgaonkar 		handled = cur_cpu_spec->machine_check_early(regs);
3314c703416SMahesh Salgaonkar 	return handled;
3321e9b4507SMahesh Salgaonkar }
3331e9b4507SMahesh Salgaonkar 
3340869b6fdSMahesh Salgaonkar long hmi_exception_realmode(struct pt_regs *regs)
3350869b6fdSMahesh Salgaonkar {
33669111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.hmi_exceptions);
3370869b6fdSMahesh Salgaonkar 
338fd7bacbcSMahesh Salgaonkar 	wait_for_subcore_guest_exit();
339fd7bacbcSMahesh Salgaonkar 
3400869b6fdSMahesh Salgaonkar 	if (ppc_md.hmi_exception_early)
3410869b6fdSMahesh Salgaonkar 		ppc_md.hmi_exception_early(regs);
3420869b6fdSMahesh Salgaonkar 
343fd7bacbcSMahesh Salgaonkar 	wait_for_tb_resync();
344fd7bacbcSMahesh Salgaonkar 
3450869b6fdSMahesh Salgaonkar 	return 0;
3460869b6fdSMahesh Salgaonkar }
3470869b6fdSMahesh Salgaonkar 
34814cf11afSPaul Mackerras #endif
34914cf11afSPaul Mackerras 
35014cf11afSPaul Mackerras /*
35114cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
35214cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
35314cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
35414cf11afSPaul Mackerras  * table.
35514cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
35614cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
35714cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
35814cf11afSPaul Mackerras  *  -- paulus.
35914cf11afSPaul Mackerras  */
36014cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
36114cf11afSPaul Mackerras {
36268a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
36314cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
36414cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
36514cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
36614cf11afSPaul Mackerras 
36714cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
36814cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
36914cf11afSPaul Mackerras 		/*
37014cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
37114cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
37214cf11afSPaul Mackerras 		 * As the address is in the exception table
37314cf11afSPaul Mackerras 		 * we should be able to read the instr there.
37414cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
37514cf11afSPaul Mackerras 		 * load or store.
37614cf11afSPaul Mackerras 		 */
377ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
37814cf11afSPaul Mackerras 			nip -= 2;
379ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
38014cf11afSPaul Mackerras 			--nip;
381ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
38214cf11afSPaul Mackerras 			unsigned int rb;
38314cf11afSPaul Mackerras 
38414cf11afSPaul Mackerras 			--nip;
38514cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
38614cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
38714cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
38814cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
38914cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
39061a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
39114cf11afSPaul Mackerras 			return 1;
39214cf11afSPaul Mackerras 		}
39314cf11afSPaul Mackerras 	}
39468a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
39514cf11afSPaul Mackerras 	return 0;
39614cf11afSPaul Mackerras }
39714cf11afSPaul Mackerras 
398172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
39914cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
40014cf11afSPaul Mackerras    is in the ESR. */
40114cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
40214cf11afSPaul Mackerras #define REASON_FP		ESR_FP
40314cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
40414cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
40514cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
40614cf11afSPaul Mackerras 
40714cf11afSPaul Mackerras /* single-step stuff */
40851ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
40951ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
41014cf11afSPaul Mackerras 
41114cf11afSPaul Mackerras #else
41214cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
41314cf11afSPaul Mackerras    exception is in the MSR. */
41414cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
4158b3c34cfSMichael Neuling #define REASON_TM		0x200000
41614cf11afSPaul Mackerras #define REASON_FP		0x100000
41714cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
41814cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
41914cf11afSPaul Mackerras #define REASON_TRAP		0x20000
42014cf11afSPaul Mackerras 
42114cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
42214cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
42314cf11afSPaul Mackerras #endif
42414cf11afSPaul Mackerras 
4250d0935b3SMichael Ellerman #if defined(CONFIG_E500)
426fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
427fe04b112SScott Wood {
428fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
429fe04b112SScott Wood 	unsigned long reason = mcsr;
430fe04b112SScott Wood 	int recoverable = 1;
431fe04b112SScott Wood 
43282a9a480SScott Wood 	if (reason & MCSR_LD) {
433cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
434cce1f106SShaohui Xie 		if (recoverable == 1)
435cce1f106SShaohui Xie 			goto silent_out;
436cce1f106SShaohui Xie 	}
437cce1f106SShaohui Xie 
438fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
439fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
440fe04b112SScott Wood 
441fe04b112SScott Wood 	if (reason & MCSR_MCP)
442fe04b112SScott Wood 		printk("Machine Check Signal\n");
443fe04b112SScott Wood 
444fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
445fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
446fe04b112SScott Wood 
447fe04b112SScott Wood 		/*
448fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
449fe04b112SScott Wood 		 */
450fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
451fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
452fe04b112SScott Wood 			;
453fe04b112SScott Wood 
454fe04b112SScott Wood 		/*
455fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
456fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
457fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
458fe04b112SScott Wood 		 */
459fe04b112SScott Wood 		reason &= ~MCSR_IF;
460fe04b112SScott Wood 	}
461fe04b112SScott Wood 
462fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
463fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
46437caf9f2SKumar Gala 
46537caf9f2SKumar Gala 		/*
46637caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
46737caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
46837caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
46937caf9f2SKumar Gala 		 */
47037caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
471fe04b112SScott Wood 			recoverable = 0;
472fe04b112SScott Wood 	}
473fe04b112SScott Wood 
474fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
475fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
476fe04b112SScott Wood 		recoverable = 0;
477fe04b112SScott Wood 	}
478fe04b112SScott Wood 
479fe04b112SScott Wood 	if (reason & MCSR_NMI)
480fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
481fe04b112SScott Wood 
482fe04b112SScott Wood 	if (reason & MCSR_IF) {
483fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
484fe04b112SScott Wood 		recoverable = 0;
485fe04b112SScott Wood 	}
486fe04b112SScott Wood 
487fe04b112SScott Wood 	if (reason & MCSR_LD) {
488fe04b112SScott Wood 		printk("Load Error Report\n");
489fe04b112SScott Wood 		recoverable = 0;
490fe04b112SScott Wood 	}
491fe04b112SScott Wood 
492fe04b112SScott Wood 	if (reason & MCSR_ST) {
493fe04b112SScott Wood 		printk("Store Error Report\n");
494fe04b112SScott Wood 		recoverable = 0;
495fe04b112SScott Wood 	}
496fe04b112SScott Wood 
497fe04b112SScott Wood 	if (reason & MCSR_LDG) {
498fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
499fe04b112SScott Wood 		recoverable = 0;
500fe04b112SScott Wood 	}
501fe04b112SScott Wood 
502fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
503fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
504fe04b112SScott Wood 
505fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
506fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
507fe04b112SScott Wood 		recoverable = 0;
508fe04b112SScott Wood 	}
509fe04b112SScott Wood 
510fe04b112SScott Wood 	if (reason & MCSR_MAV) {
511fe04b112SScott Wood 		u64 addr;
512fe04b112SScott Wood 
513fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
514fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
515fe04b112SScott Wood 
516fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
517fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
518fe04b112SScott Wood 	}
519fe04b112SScott Wood 
520cce1f106SShaohui Xie silent_out:
521fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
522fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
523fe04b112SScott Wood }
524fe04b112SScott Wood 
52547c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
52647c0bd1aSBenjamin Herrenschmidt {
52742bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
52847c0bd1aSBenjamin Herrenschmidt 
529cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
530cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
531cce1f106SShaohui Xie 			return 1;
5324e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5334e0e3435SHongtao Jia 			return 1;
534cce1f106SShaohui Xie 	}
535cce1f106SShaohui Xie 
53614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
53714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
53814cf11afSPaul Mackerras 
53914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
54014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
54114cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
54214cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
54314cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
54414cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
54514cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
54614cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
54714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
54814cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
54914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
55014cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
55114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
55214cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
55314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
55414cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
55514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
55614cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
55714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
558c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
55914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
56014cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
56114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
56214cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
56347c0bd1aSBenjamin Herrenschmidt 
56447c0bd1aSBenjamin Herrenschmidt 	return 0;
56547c0bd1aSBenjamin Herrenschmidt }
5664490c06bSKumar Gala 
5674490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5684490c06bSKumar Gala {
5694490c06bSKumar Gala 	return 0;
5704490c06bSKumar Gala }
57114cf11afSPaul Mackerras #elif defined(CONFIG_E200)
57247c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
57347c0bd1aSBenjamin Herrenschmidt {
57442bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
57547c0bd1aSBenjamin Herrenschmidt 
57614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
57814cf11afSPaul Mackerras 
57914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
58014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
58114cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
58214cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
58314cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
58414cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
58514cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
58614cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
58714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
58814cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
58914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
59014cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
59114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
59214cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
59347c0bd1aSBenjamin Herrenschmidt 
59447c0bd1aSBenjamin Herrenschmidt 	return 0;
59547c0bd1aSBenjamin Herrenschmidt }
596e627f8dcSChristophe Leroy #elif defined(CONFIG_PPC_8xx)
597e627f8dcSChristophe Leroy int machine_check_8xx(struct pt_regs *regs)
598e627f8dcSChristophe Leroy {
59942bff234SMichael Ellerman 	unsigned long reason = regs->msr;
600e627f8dcSChristophe Leroy 
601e627f8dcSChristophe Leroy 	pr_err("Machine check in kernel mode.\n");
602e627f8dcSChristophe Leroy 	pr_err("Caused by (from SRR1=%lx): ", reason);
603e627f8dcSChristophe Leroy 	if (reason & 0x40000000)
604e627f8dcSChristophe Leroy 		pr_err("Fetch error at address %lx\n", regs->nip);
605e627f8dcSChristophe Leroy 	else
606e627f8dcSChristophe Leroy 		pr_err("Data access error at address %lx\n", regs->dar);
607e627f8dcSChristophe Leroy 
608e627f8dcSChristophe Leroy #ifdef CONFIG_PCI
609e627f8dcSChristophe Leroy 	/* the qspan pci read routines can cause machine checks -- Cort
610e627f8dcSChristophe Leroy 	 *
611e627f8dcSChristophe Leroy 	 * yuck !!! that totally needs to go away ! There are better ways
612e627f8dcSChristophe Leroy 	 * to deal with that than having a wart in the mcheck handler.
613e627f8dcSChristophe Leroy 	 * -- BenH
614e627f8dcSChristophe Leroy 	 */
615e627f8dcSChristophe Leroy 	bad_page_fault(regs, regs->dar, SIGBUS);
616e627f8dcSChristophe Leroy 	return 1;
617e627f8dcSChristophe Leroy #else
618e627f8dcSChristophe Leroy 	return 0;
619e627f8dcSChristophe Leroy #endif
620e627f8dcSChristophe Leroy }
621*7f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
62247c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
62347c0bd1aSBenjamin Herrenschmidt {
62442bff234SMichael Ellerman 	unsigned long reason = regs->msr;
62547c0bd1aSBenjamin Herrenschmidt 
62614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62714cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
62814cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
62914cf11afSPaul Mackerras 	case 0x80000:
63014cf11afSPaul Mackerras 		printk("Machine check signal\n");
63114cf11afSPaul Mackerras 		break;
63214cf11afSPaul Mackerras 	case 0:		/* for 601 */
63314cf11afSPaul Mackerras 	case 0x40000:
63414cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
63514cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
63614cf11afSPaul Mackerras 		break;
63714cf11afSPaul Mackerras 	case 0x20000:
63814cf11afSPaul Mackerras 		printk("Data parity error signal\n");
63914cf11afSPaul Mackerras 		break;
64014cf11afSPaul Mackerras 	case 0x10000:
64114cf11afSPaul Mackerras 		printk("Address parity error signal\n");
64214cf11afSPaul Mackerras 		break;
64314cf11afSPaul Mackerras 	case 0x20000000:
64414cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
64514cf11afSPaul Mackerras 		break;
64614cf11afSPaul Mackerras 	case 0x40000000:
64714cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
64814cf11afSPaul Mackerras 		break;
64914cf11afSPaul Mackerras 	case 0x00100000:
65014cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
65114cf11afSPaul Mackerras 		break;
65214cf11afSPaul Mackerras 	default:
65314cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
65414cf11afSPaul Mackerras 	}
65575918a4bSOlof Johansson 	return 0;
65675918a4bSOlof Johansson }
65747c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
65875918a4bSOlof Johansson 
65975918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
66075918a4bSOlof Johansson {
661ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
66275918a4bSOlof Johansson 	int recover = 0;
66375918a4bSOlof Johansson 
664f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
665f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
66669111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
66789713ed1SAnton Blanchard 
668d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
669d93b0ac0SMahesh Salgaonkar 
67047c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
67147c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
67247c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
67347c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
67447c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
67547c0bd1aSBenjamin Herrenschmidt 	 */
67675918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
67775918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
67847c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
67947c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
68075918a4bSOlof Johansson 
68147c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
682ba12eedeSLi Zhong 		goto bail;
68375918a4bSOlof Johansson 
684a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
685ba12eedeSLi Zhong 		goto bail;
68675918a4bSOlof Johansson 
68775918a4bSOlof Johansson 	if (check_io_access(regs))
688ba12eedeSLi Zhong 		goto bail;
68975918a4bSOlof Johansson 
6908dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
69114cf11afSPaul Mackerras 
69214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
69314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
69414cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
695ba12eedeSLi Zhong 
696ba12eedeSLi Zhong bail:
697ba12eedeSLi Zhong 	exception_exit(prev_state);
69814cf11afSPaul Mackerras }
69914cf11afSPaul Mackerras 
70014cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
70114cf11afSPaul Mackerras {
70214cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
70314cf11afSPaul Mackerras }
70414cf11afSPaul Mackerras 
7050869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
7060869b6fdSMahesh Salgaonkar {
7070869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
7080869b6fdSMahesh Salgaonkar 
7090869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
7100869b6fdSMahesh Salgaonkar 	irq_enter();
7110869b6fdSMahesh Salgaonkar 
7120869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
7130869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
7140869b6fdSMahesh Salgaonkar 
7150869b6fdSMahesh Salgaonkar 	irq_exit();
7160869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
7170869b6fdSMahesh Salgaonkar }
7180869b6fdSMahesh Salgaonkar 
719dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
72014cf11afSPaul Mackerras {
721ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
722ba12eedeSLi Zhong 
72314cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
72414cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
72514cf11afSPaul Mackerras 
72614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
727ba12eedeSLi Zhong 
728ba12eedeSLi Zhong 	exception_exit(prev_state);
72914cf11afSPaul Mackerras }
73014cf11afSPaul Mackerras 
731dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
73214cf11afSPaul Mackerras {
733ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
734ba12eedeSLi Zhong 
73514cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
73614cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
737ba12eedeSLi Zhong 		goto bail;
73814cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
739ba12eedeSLi Zhong 		goto bail;
74014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
741ba12eedeSLi Zhong 
742ba12eedeSLi Zhong bail:
743ba12eedeSLi Zhong 	exception_exit(prev_state);
74414cf11afSPaul Mackerras }
74514cf11afSPaul Mackerras 
74614cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
74714cf11afSPaul Mackerras {
74814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
74914cf11afSPaul Mackerras }
75014cf11afSPaul Mackerras 
75103465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
75214cf11afSPaul Mackerras {
753ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
754ba12eedeSLi Zhong 
7552538c2d0SK.Prasad 	clear_single_step(regs);
75614cf11afSPaul Mackerras 
7576cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
7586cc89badSNaveen N. Rao 		return;
7596cc89badSNaveen N. Rao 
76014cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
76114cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
762ba12eedeSLi Zhong 		goto bail;
76314cf11afSPaul Mackerras 	if (debugger_sstep(regs))
764ba12eedeSLi Zhong 		goto bail;
76514cf11afSPaul Mackerras 
76614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
767ba12eedeSLi Zhong 
768ba12eedeSLi Zhong bail:
769ba12eedeSLi Zhong 	exception_exit(prev_state);
77014cf11afSPaul Mackerras }
77103465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
77214cf11afSPaul Mackerras 
77314cf11afSPaul Mackerras /*
77414cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
77514cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
77614cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
77714cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
77814cf11afSPaul Mackerras  */
7798dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
78014cf11afSPaul Mackerras {
7812538c2d0SK.Prasad 	if (single_stepping(regs))
7822538c2d0SK.Prasad 		single_step_exception(regs);
78314cf11afSPaul Mackerras }
78414cf11afSPaul Mackerras 
7855fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
786dc1c1ca3SStephen Rothwell {
7875fad293bSKumar Gala 	int ret = 0;
788dc1c1ca3SStephen Rothwell 
789dc1c1ca3SStephen Rothwell 	/* Invalid operation */
790dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7915fad293bSKumar Gala 		ret = FPE_FLTINV;
792dc1c1ca3SStephen Rothwell 
793dc1c1ca3SStephen Rothwell 	/* Overflow */
794dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7955fad293bSKumar Gala 		ret = FPE_FLTOVF;
796dc1c1ca3SStephen Rothwell 
797dc1c1ca3SStephen Rothwell 	/* Underflow */
798dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7995fad293bSKumar Gala 		ret = FPE_FLTUND;
800dc1c1ca3SStephen Rothwell 
801dc1c1ca3SStephen Rothwell 	/* Divide by zero */
802dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
8035fad293bSKumar Gala 		ret = FPE_FLTDIV;
804dc1c1ca3SStephen Rothwell 
805dc1c1ca3SStephen Rothwell 	/* Inexact result */
806dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
8075fad293bSKumar Gala 		ret = FPE_FLTRES;
8085fad293bSKumar Gala 
8095fad293bSKumar Gala 	return ret;
8105fad293bSKumar Gala }
8115fad293bSKumar Gala 
8125fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8135fad293bSKumar Gala {
8145fad293bSKumar Gala 	int code = 0;
8155fad293bSKumar Gala 
8165fad293bSKumar Gala 	flush_fp_to_thread(current);
8175fad293bSKumar Gala 
818de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
819dc1c1ca3SStephen Rothwell 
820dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
821dc1c1ca3SStephen Rothwell }
822dc1c1ca3SStephen Rothwell 
823dc1c1ca3SStephen Rothwell /*
824dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
82514cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
82614cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
82714cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
82814cf11afSPaul Mackerras  *
82914cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
83014cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
83114cf11afSPaul Mackerras  * bits is faster and easier.
83286417780SPaul Mackerras  *
83314cf11afSPaul Mackerras  */
83414cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
83514cf11afSPaul Mackerras {
83614cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
83714cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
83814cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
83914cf11afSPaul Mackerras 	u32 num_bytes;
84014cf11afSPaul Mackerras 	unsigned long EA;
84114cf11afSPaul Mackerras 	int pos = 0;
84214cf11afSPaul Mackerras 
84314cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
84416c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
84514cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
84614cf11afSPaul Mackerras 			return -EINVAL;
84714cf11afSPaul Mackerras 
84814cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
84914cf11afSPaul Mackerras 
85016c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
85116c57b36SKumar Gala 		case PPC_INST_LSWX:
85216c57b36SKumar Gala 		case PPC_INST_STSWX:
85314cf11afSPaul Mackerras 			EA += NB_RB;
85414cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
85514cf11afSPaul Mackerras 			break;
85616c57b36SKumar Gala 		case PPC_INST_LSWI:
85716c57b36SKumar Gala 		case PPC_INST_STSWI:
85814cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
85914cf11afSPaul Mackerras 			break;
86014cf11afSPaul Mackerras 		default:
86114cf11afSPaul Mackerras 			return -EINVAL;
86214cf11afSPaul Mackerras 	}
86314cf11afSPaul Mackerras 
86414cf11afSPaul Mackerras 	while (num_bytes != 0)
86514cf11afSPaul Mackerras 	{
86614cf11afSPaul Mackerras 		u8 val;
86714cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
86814cf11afSPaul Mackerras 
86980aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
87080aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
87180aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
87280aa0fb4SJames Yang 
87316c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
87416c57b36SKumar Gala 			case PPC_INST_LSWX:
87516c57b36SKumar Gala 			case PPC_INST_LSWI:
87614cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
87714cf11afSPaul Mackerras 					return -EFAULT;
87814cf11afSPaul Mackerras 				/* first time updating this reg,
87914cf11afSPaul Mackerras 				 * zero it out */
88014cf11afSPaul Mackerras 				if (pos == 0)
88114cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
88214cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
88314cf11afSPaul Mackerras 				break;
88416c57b36SKumar Gala 			case PPC_INST_STSWI:
88516c57b36SKumar Gala 			case PPC_INST_STSWX:
88614cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
88714cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
88814cf11afSPaul Mackerras 					return -EFAULT;
88914cf11afSPaul Mackerras 				break;
89014cf11afSPaul Mackerras 		}
89114cf11afSPaul Mackerras 		/* move EA to next address */
89214cf11afSPaul Mackerras 		EA += 1;
89314cf11afSPaul Mackerras 		num_bytes--;
89414cf11afSPaul Mackerras 
89514cf11afSPaul Mackerras 		/* manage our position within the register */
89614cf11afSPaul Mackerras 		if (++pos == 4) {
89714cf11afSPaul Mackerras 			pos = 0;
89814cf11afSPaul Mackerras 			if (++rT == 32)
89914cf11afSPaul Mackerras 				rT = 0;
90014cf11afSPaul Mackerras 		}
90114cf11afSPaul Mackerras 	}
90214cf11afSPaul Mackerras 
90314cf11afSPaul Mackerras 	return 0;
90414cf11afSPaul Mackerras }
90514cf11afSPaul Mackerras 
906c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
907c3412dcbSWill Schmidt {
908c3412dcbSWill Schmidt 	u32 ra,rs;
909c3412dcbSWill Schmidt 	unsigned long tmp;
910c3412dcbSWill Schmidt 
911c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
912c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
913c3412dcbSWill Schmidt 
914c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
915c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
916c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
917c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
918c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
919c3412dcbSWill Schmidt 
920c3412dcbSWill Schmidt 	return 0;
921c3412dcbSWill Schmidt }
922c3412dcbSWill Schmidt 
923c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
924c1469f13SKumar Gala {
925c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
926c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
927c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
928c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
929c1469f13SKumar Gala 	u8 bit;
930c1469f13SKumar Gala 	unsigned long tmp;
931c1469f13SKumar Gala 
932c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
933c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
934c1469f13SKumar Gala 
935c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
936c1469f13SKumar Gala 
937c1469f13SKumar Gala 	return 0;
938c1469f13SKumar Gala }
939c1469f13SKumar Gala 
9406ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9416ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9426ce6c629SMichael Neuling {
9436ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9446ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9456ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9466ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
9476ce6c629SMichael Neuling 	 */
9486ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
9496ce6c629SMichael Neuling 		tm_enable();
9506ce6c629SMichael Neuling 		tm_abort(cause);
9516ce6c629SMichael Neuling 		return true;
9526ce6c629SMichael Neuling 	}
9536ce6c629SMichael Neuling 	return false;
9546ce6c629SMichael Neuling }
9556ce6c629SMichael Neuling #else
9566ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
9576ce6c629SMichael Neuling {
9586ce6c629SMichael Neuling 	return false;
9596ce6c629SMichael Neuling }
9606ce6c629SMichael Neuling #endif
9616ce6c629SMichael Neuling 
96214cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
96314cf11afSPaul Mackerras {
96414cf11afSPaul Mackerras 	u32 instword;
96514cf11afSPaul Mackerras 	u32 rd;
96614cf11afSPaul Mackerras 
9674288e343SAnton Blanchard 	if (!user_mode(regs))
96814cf11afSPaul Mackerras 		return -EINVAL;
96914cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
97014cf11afSPaul Mackerras 
97114cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
97214cf11afSPaul Mackerras 		return -EFAULT;
97314cf11afSPaul Mackerras 
97414cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
97516c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
976eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
97714cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
97814cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
97914cf11afSPaul Mackerras 		return 0;
98014cf11afSPaul Mackerras 	}
98114cf11afSPaul Mackerras 
98214cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
98380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
984eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
98514cf11afSPaul Mackerras 		return 0;
98680947e7cSGeert Uytterhoeven 	}
98714cf11afSPaul Mackerras 
98814cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
98916c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
99086417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
99114cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
99214cf11afSPaul Mackerras 
993eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
99414cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
99514cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
99614cf11afSPaul Mackerras 		return 0;
99714cf11afSPaul Mackerras 	}
99814cf11afSPaul Mackerras 
99914cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
100080947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
10016ce6c629SMichael Neuling 		if (tm_abort_check(regs,
10026ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
10036ce6c629SMichael Neuling 			return -EINVAL;
1004eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
100514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
100680947e7cSGeert Uytterhoeven 	}
100714cf11afSPaul Mackerras 
1008c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
100916c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1010eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1011c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1012c3412dcbSWill Schmidt 	}
1013c3412dcbSWill Schmidt 
1014c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
101516c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1016eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1017c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1018c1469f13SKumar Gala 	}
1019c1469f13SKumar Gala 
10209863c28aSJames Yang 	/* Emulate sync instruction variants */
10219863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
10229863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
10239863c28aSJames Yang 		asm volatile("sync");
10249863c28aSJames Yang 		return 0;
10259863c28aSJames Yang 	}
10269863c28aSJames Yang 
1027efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1028efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
102973d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
103073d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
103173d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
103273d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1033efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1034efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1035efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1036efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1037efcac658SAlexey Kardashevskiy 		return 0;
1038efcac658SAlexey Kardashevskiy 	}
1039efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
104073d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
104173d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
104273d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
104373d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1044efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1045efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1046efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
104700ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1048efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
104900ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1050efcac658SAlexey Kardashevskiy 		return 0;
1051efcac658SAlexey Kardashevskiy 	}
1052efcac658SAlexey Kardashevskiy #endif
1053efcac658SAlexey Kardashevskiy 
105414cf11afSPaul Mackerras 	return -EINVAL;
105514cf11afSPaul Mackerras }
105614cf11afSPaul Mackerras 
105773c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
105814cf11afSPaul Mackerras {
105973c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
106014cf11afSPaul Mackerras }
106114cf11afSPaul Mackerras 
10623a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
10633a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
10643a3b5aa6SKevin Hao {
10653a3b5aa6SKevin Hao 	int ret;
10663a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
10673a3b5aa6SKevin Hao 
10683a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
10693a3b5aa6SKevin Hao 	if (ret >= 0)
10703a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
10713a3b5aa6SKevin Hao 
10723a3b5aa6SKevin Hao 	switch (ret) {
10733a3b5aa6SKevin Hao 	case 0:
10743a3b5aa6SKevin Hao 		emulate_single_step(regs);
10753a3b5aa6SKevin Hao 		return 0;
10763a3b5aa6SKevin Hao 	case 1: {
10773a3b5aa6SKevin Hao 			int code = 0;
1078de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
10793a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
10803a3b5aa6SKevin Hao 			return 0;
10813a3b5aa6SKevin Hao 		}
10823a3b5aa6SKevin Hao 	case -EFAULT:
10833a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10843a3b5aa6SKevin Hao 		return 0;
10853a3b5aa6SKevin Hao 	}
10863a3b5aa6SKevin Hao 
10873a3b5aa6SKevin Hao 	return -1;
10883a3b5aa6SKevin Hao }
10893a3b5aa6SKevin Hao #else
10903a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
10913a3b5aa6SKevin Hao #endif
10923a3b5aa6SKevin Hao 
109303465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
109414cf11afSPaul Mackerras {
1095ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
109614cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
109714cf11afSPaul Mackerras 
1098aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
109904903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
110014cf11afSPaul Mackerras 
110114cf11afSPaul Mackerras 	if (reason & REASON_FP) {
110214cf11afSPaul Mackerras 		/* IEEE FP exception */
1103dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1104ba12eedeSLi Zhong 		goto bail;
11058dad3f92SPaul Mackerras 	}
11068dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1107a4c3f909SBalbir Singh 		unsigned long bugaddr;
1108ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1109ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1110ba797b28SJason Wessel 		if (debugger_bpt(regs))
1111ba12eedeSLi Zhong 			goto bail;
1112ba797b28SJason Wessel 
11136cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
11146cc89badSNaveen N. Rao 			goto bail;
11156cc89badSNaveen N. Rao 
111614cf11afSPaul Mackerras 		/* trap exception */
1117dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1118dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1119ba12eedeSLi Zhong 			goto bail;
112073c9ceabSJeremy Fitzhardinge 
1121a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1122a4c3f909SBalbir Singh 		/*
1123a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1124a4c3f909SBalbir Singh 		 */
1125a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1126a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1127a4c3f909SBalbir Singh 
112873c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1129a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
113014cf11afSPaul Mackerras 			regs->nip += 4;
1131ba12eedeSLi Zhong 			goto bail;
113214cf11afSPaul Mackerras 		}
11338dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1134ba12eedeSLi Zhong 		goto bail;
11358dad3f92SPaul Mackerras 	}
1136bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1137bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1138bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1139bc2a9408SMichael Neuling 		 * This occurs when:
1140bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1141bc2a9408SMichael Neuling 		 *    transition in TM states.
1142bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1143bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1144bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1145bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1146bc2a9408SMichael Neuling 		 */
1147bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1148bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1149bc2a9408SMichael Neuling 			regs->nip += 4;
1150ba12eedeSLi Zhong 			goto bail;
1151bc2a9408SMichael Neuling 		}
1152bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1153bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1154bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1155bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1156bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1157bc2a9408SMichael Neuling 		 */
1158bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1159bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1160ba12eedeSLi Zhong 			goto bail;
1161bc2a9408SMichael Neuling 		} else {
1162bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1163bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1164bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1165bc2a9408SMichael Neuling 		}
1166bc2a9408SMichael Neuling 	}
1167bc2a9408SMichael Neuling #endif
11688dad3f92SPaul Mackerras 
1169b3f6a459SMichael Ellerman 	/*
1170b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1171b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1172b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1173b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1174b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1175b3f6a459SMichael Ellerman 	 */
1176b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1177b3f6a459SMichael Ellerman 		goto sigill;
1178b3f6a459SMichael Ellerman 
1179a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1180a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1181cd8a5673SPaul Mackerras 		local_irq_enable();
1182cd8a5673SPaul Mackerras 
118304903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
118404903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
118504903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
118604903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
118704903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
118804903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
11894e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
11904e63f8edSBenjamin Herrenschmidt 	 */
11913a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1192ba12eedeSLi Zhong 		goto bail;
119304903a30SKumar Gala 
11948dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
11958dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
119614cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
119714cf11afSPaul Mackerras 		case 0:
119814cf11afSPaul Mackerras 			regs->nip += 4;
119914cf11afSPaul Mackerras 			emulate_single_step(regs);
1200ba12eedeSLi Zhong 			goto bail;
120114cf11afSPaul Mackerras 		case -EFAULT:
120214cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1203ba12eedeSLi Zhong 			goto bail;
12048dad3f92SPaul Mackerras 		}
12058dad3f92SPaul Mackerras 	}
12068dad3f92SPaul Mackerras 
1207b3f6a459SMichael Ellerman sigill:
120814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
120914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
121014cf11afSPaul Mackerras 	else
121114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1212ba12eedeSLi Zhong 
1213ba12eedeSLi Zhong bail:
1214ba12eedeSLi Zhong 	exception_exit(prev_state);
121514cf11afSPaul Mackerras }
121603465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
121714cf11afSPaul Mackerras 
1218bf593907SPaul Mackerras /*
1219bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1220bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1221bf593907SPaul Mackerras  */
122203465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1223bf593907SPaul Mackerras {
1224bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1225bf593907SPaul Mackerras 	program_check_exception(regs);
1226bf593907SPaul Mackerras }
122703465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1228bf593907SPaul Mackerras 
1229dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
123014cf11afSPaul Mackerras {
1231ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
12324393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
123314cf11afSPaul Mackerras 
1234a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1235a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1236a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1237a3512b2dSBenjamin Herrenschmidt 
12386ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12396ce6c629SMichael Neuling 		goto bail;
12406ce6c629SMichael Neuling 
1241e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1242e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
124314cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
124414cf11afSPaul Mackerras 
124514cf11afSPaul Mackerras 	if (fixed == 1) {
124614cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
124714cf11afSPaul Mackerras 		emulate_single_step(regs);
1248ba12eedeSLi Zhong 		goto bail;
124914cf11afSPaul Mackerras 	}
125014cf11afSPaul Mackerras 
125114cf11afSPaul Mackerras 	/* Operand address was bad */
125214cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12534393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12544393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12554393c4f6SBenjamin Herrenschmidt 	} else {
12564393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12574393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
125814cf11afSPaul Mackerras 	}
12594393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12604393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
12614393c4f6SBenjamin Herrenschmidt 	else
12624393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1263ba12eedeSLi Zhong 
1264ba12eedeSLi Zhong bail:
1265ba12eedeSLi Zhong 	exception_exit(prev_state);
126614cf11afSPaul Mackerras }
126714cf11afSPaul Mackerras 
1268f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1269f0f558b1SPaul Mackerras {
1270f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1271f0f558b1SPaul Mackerras 
1272f0f558b1SPaul Mackerras 	if (user_mode(regs))
1273f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1274f0f558b1SPaul Mackerras 	else
1275f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1276f0f558b1SPaul Mackerras 
1277f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1278f0f558b1SPaul Mackerras }
1279f0f558b1SPaul Mackerras 
128014cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
128114cf11afSPaul Mackerras {
128214cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
128314cf11afSPaul Mackerras 	       current, regs->gpr[1]);
128414cf11afSPaul Mackerras 	debugger(regs);
128514cf11afSPaul Mackerras 	show_regs(regs);
128614cf11afSPaul Mackerras 	panic("kernel stack overflow");
128714cf11afSPaul Mackerras }
128814cf11afSPaul Mackerras 
128914cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
129014cf11afSPaul Mackerras {
129114cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
129214cf11afSPaul Mackerras 	       regs->nip, regs->msr);
129314cf11afSPaul Mackerras 	debugger(regs);
129414cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
129514cf11afSPaul Mackerras }
129614cf11afSPaul Mackerras 
1297dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1298dc1c1ca3SStephen Rothwell {
1299ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1300ba12eedeSLi Zhong 
1301dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1302dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1303dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1304ba12eedeSLi Zhong 
1305ba12eedeSLi Zhong 	exception_exit(prev_state);
1306dc1c1ca3SStephen Rothwell }
1307dc1c1ca3SStephen Rothwell 
1308dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1309dc1c1ca3SStephen Rothwell {
1310ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1311ba12eedeSLi Zhong 
1312dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1313dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1314dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1315dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1316ba12eedeSLi Zhong 		goto bail;
1317dc1c1ca3SStephen Rothwell 	}
13186c4841c2SAnton Blanchard 
1319dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1320dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1321dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1322ba12eedeSLi Zhong 
1323ba12eedeSLi Zhong bail:
1324ba12eedeSLi Zhong 	exception_exit(prev_state);
1325dc1c1ca3SStephen Rothwell }
1326dc1c1ca3SStephen Rothwell 
1327ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1328ce48b210SMichael Neuling {
1329ce48b210SMichael Neuling 	if (user_mode(regs)) {
1330ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1331ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1332ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1333ce48b210SMichael Neuling 		return;
1334ce48b210SMichael Neuling 	}
1335ce48b210SMichael Neuling 
1336ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1337ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1338ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1339ce48b210SMichael Neuling }
1340ce48b210SMichael Neuling 
13412517617eSMichael Neuling #ifdef CONFIG_PPC64
1342172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1343172f7aaaSCyril Bur {
13445d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
13455d176f75SCyril Bur 	if (user_mode(regs)) {
13465d176f75SCyril Bur 		current->thread.load_tm++;
13475d176f75SCyril Bur 		regs->msr |= MSR_TM;
13485d176f75SCyril Bur 		tm_enable();
13495d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
13505d176f75SCyril Bur 		return;
13515d176f75SCyril Bur 	}
13525d176f75SCyril Bur #endif
1353172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1354172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1355172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1356172f7aaaSCyril Bur }
1357172f7aaaSCyril Bur 
1358021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1359d0c0c9a1SMichael Neuling {
1360021424a1SMichael Ellerman 	static char *facility_strings[] = {
13612517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13622517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13632517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13642517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13652517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
13662517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
13672517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
13682517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1369794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
13709b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1371021424a1SMichael Ellerman 	};
13722517617eSMichael Neuling 	char *facility = "unknown";
1373021424a1SMichael Ellerman 	u64 value;
1374c952c1c4SAnshuman Khandual 	u32 instword, rd;
13752517617eSMichael Neuling 	u8 status;
13762517617eSMichael Neuling 	bool hv;
1377021424a1SMichael Ellerman 
13782517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
13792517617eSMichael Neuling 	if (hv)
1380b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
13812517617eSMichael Neuling 	else
13822517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
13832517617eSMichael Neuling 
13842517617eSMichael Neuling 	status = value >> 56;
13852517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1386c952c1c4SAnshuman Khandual 		/*
1387c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1388c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1389c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1390c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1391c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1392c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1393c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1394c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1395c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1396c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1397c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1398c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1399c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1400c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
14012517617eSMichael Neuling 		 */
1402c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1403c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1404c952c1c4SAnshuman Khandual 			return;
1405c952c1c4SAnshuman Khandual 		}
1406c952c1c4SAnshuman Khandual 
1407c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1408c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1409c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1410c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1411c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
14122517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1413b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1414b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1415c952c1c4SAnshuman Khandual 		}
1416c952c1c4SAnshuman Khandual 
1417c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1418c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1419c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1420c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1421c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1422c952c1c4SAnshuman Khandual 				return;
1423c952c1c4SAnshuman Khandual 			}
1424c952c1c4SAnshuman Khandual 			regs->nip += 4;
1425c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1426c952c1c4SAnshuman Khandual 		}
14272517617eSMichael Neuling 		return;
1428b14b6260SMichael Ellerman 	}
1429b14b6260SMichael Ellerman 
1430172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1431172f7aaaSCyril Bur 		/*
1432172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1433172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1434172f7aaaSCyril Bur 		 *
1435172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1436172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1437172f7aaaSCyril Bur 		 * support.
1438172f7aaaSCyril Bur 		 *
1439172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1440172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1441172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1442172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1443172f7aaaSCyril Bur 		 */
1444172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1445172f7aaaSCyril Bur 			goto out;
1446172f7aaaSCyril Bur 
1447172f7aaaSCyril Bur 		tm_unavailable(regs);
1448172f7aaaSCyril Bur 		return;
1449172f7aaaSCyril Bur 	}
1450172f7aaaSCyril Bur 
145193c2ec0fSBalbir Singh 	if ((hv || status >= 2) &&
145293c2ec0fSBalbir Singh 	    (status < ARRAY_SIZE(facility_strings)) &&
14532517617eSMichael Neuling 	    facility_strings[status])
14542517617eSMichael Neuling 		facility = facility_strings[status];
1455021424a1SMichael Ellerman 
1456d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1457d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1458d0c0c9a1SMichael Neuling 		local_irq_enable();
1459d0c0c9a1SMichael Neuling 
146093c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
146193c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1462d0c0c9a1SMichael Neuling 
1463172f7aaaSCyril Bur out:
1464d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1465d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1466d0c0c9a1SMichael Neuling 		return;
1467d0c0c9a1SMichael Neuling 	}
1468d0c0c9a1SMichael Neuling 
1469021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1470d0c0c9a1SMichael Neuling }
14712517617eSMichael Neuling #endif
1472d0c0c9a1SMichael Neuling 
1473f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1474f54db641SMichael Neuling 
1475f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1476f54db641SMichael Neuling {
1477f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1478f54db641SMichael Neuling 
1479f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1480f54db641SMichael Neuling 		 regs->nip, regs->msr);
1481f54db641SMichael Neuling 
1482f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1483f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1484f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1485f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1486f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1487f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1488f54db641SMichael Neuling 	 */
1489d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1490f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1491f54db641SMichael Neuling 
1492f54db641SMichael Neuling 	/* Enable FP for the task: */
1493f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1494f54db641SMichael Neuling 
1495f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1496f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1497f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
14983ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
14993ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1500f54db641SMichael Neuling 	 */
15013ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
15023ac8ff1cSPaul Mackerras 
15033ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
15043ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
1505dc310669SCyril Bur 		msr_check_and_set(MSR_VEC);
1506dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
15073ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
15083ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15093ac8ff1cSPaul Mackerras 	}
1510f54db641SMichael Neuling }
1511f54db641SMichael Neuling 
1512f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1513f54db641SMichael Neuling {
1514f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1515f54db641SMichael Neuling 	 * the same way.
1516f54db641SMichael Neuling 	 */
1517f54db641SMichael Neuling 
1518f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1519f54db641SMichael Neuling 		 "MSR=%lx\n",
1520f54db641SMichael Neuling 		 regs->nip, regs->msr);
1521d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1522f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
15233ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1524f54db641SMichael Neuling 	current->thread.used_vr = 1;
1525f54db641SMichael Neuling 
15263ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
1527dc310669SCyril Bur 		msr_check_and_set(MSR_FP);
1528dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
15293ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15303ac8ff1cSPaul Mackerras 	}
15313ac8ff1cSPaul Mackerras }
15323ac8ff1cSPaul Mackerras 
1533f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1534f54db641SMichael Neuling {
15353ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
15363ac8ff1cSPaul Mackerras 
1537f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1538f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1539f54db641SMichael Neuling 	 *
1540f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1541f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1542f54db641SMichael Neuling 	 */
1543f54db641SMichael Neuling 
1544f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1545f54db641SMichael Neuling 		 "MSR=%lx\n",
1546f54db641SMichael Neuling 		 regs->nip, regs->msr);
1547f54db641SMichael Neuling 
15483ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
15493ac8ff1cSPaul Mackerras 
15503ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
15513ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
15523ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15533ac8ff1cSPaul Mackerras 		return;
15543ac8ff1cSPaul Mackerras 	}
15553ac8ff1cSPaul Mackerras 
1556f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1557d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1558f54db641SMichael Neuling 
1559f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1560f54db641SMichael Neuling 		MSR_VSX;
15613ac8ff1cSPaul Mackerras 
15623ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
15633ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
15643ac8ff1cSPaul Mackerras 	 */
15653ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
15663ac8ff1cSPaul Mackerras 
1567dc310669SCyril Bur 	msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1568dc310669SCyril Bur 
15693ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
1570dc310669SCyril Bur 		load_fp_state(&current->thread.fp_state);
15713ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
1572dc310669SCyril Bur 		load_vr_state(&current->thread.vr_state);
1573f54db641SMichael Neuling }
1574f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1575f54db641SMichael Neuling 
1576dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1577dc1c1ca3SStephen Rothwell {
157869111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
157989713ed1SAnton Blanchard 
1580dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1581dc1c1ca3SStephen Rothwell }
1582dc1c1ca3SStephen Rothwell 
15838dad3f92SPaul Mackerras #ifdef CONFIG_8xx
158414cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
158514cf11afSPaul Mackerras {
158614cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
158714cf11afSPaul Mackerras 
158814cf11afSPaul Mackerras 	if (!user_mode(regs)) {
158914cf11afSPaul Mackerras 		debugger(regs);
15901eb2819dSLEROY Christophe 		die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
15911eb2819dSLEROY Christophe 			regs, SIGFPE);
159214cf11afSPaul Mackerras 	}
159314cf11afSPaul Mackerras 
15943a3b5aa6SKevin Hao 	if (!emulate_math(regs))
15953a3b5aa6SKevin Hao 		return;
15965fad293bSKumar Gala 
15975fad293bSKumar Gala 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
159814cf11afSPaul Mackerras }
15998dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
160014cf11afSPaul Mackerras 
1601172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
16023bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
16033bffb652SDave Kleikamp {
16043bffb652SDave Kleikamp 	int changed = 0;
16053bffb652SDave Kleikamp 	/*
16063bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
16073bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
16083bffb652SDave Kleikamp 	 */
16093bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
16103bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
16113bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
161251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
16133bffb652SDave Kleikamp #endif
16143bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
16153bffb652SDave Kleikamp 			     5);
16163bffb652SDave Kleikamp 		changed |= 0x01;
16173bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
16183bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
16193bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
16203bffb652SDave Kleikamp 			     6);
16213bffb652SDave Kleikamp 		changed |= 0x01;
16223bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
162351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
16243bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
16253bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
16263bffb652SDave Kleikamp 			     1);
16273bffb652SDave Kleikamp 		changed |= 0x01;
16283bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
162951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
16303bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
16313bffb652SDave Kleikamp 			     2);
16323bffb652SDave Kleikamp 		changed |= 0x01;
16333bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
163451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
16353bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
16363bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
16373bffb652SDave Kleikamp 			     3);
16383bffb652SDave Kleikamp 		changed |= 0x01;
16393bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
164051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
16413bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
16423bffb652SDave Kleikamp 			     4);
16433bffb652SDave Kleikamp 		changed |= 0x01;
16443bffb652SDave Kleikamp 	}
16453bffb652SDave Kleikamp 	/*
16463bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
16473bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
16483bffb652SDave Kleikamp 	 * back on or not.
16493bffb652SDave Kleikamp 	 */
165051ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
165151ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
16523bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
16533bffb652SDave Kleikamp 	else
16543bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
165551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16563bffb652SDave Kleikamp 
16573bffb652SDave Kleikamp 	if (changed & 0x01)
165851ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
16593bffb652SDave Kleikamp }
166014cf11afSPaul Mackerras 
166103465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
166214cf11afSPaul Mackerras {
166351ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
16643bffb652SDave Kleikamp 
1665ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1666ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1667ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1668ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1669ec097c84SRoland McGrath 	 */
1670ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1671ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1672ec097c84SRoland McGrath 
1673ec097c84SRoland McGrath 		/* Disable BT */
1674ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1675ec097c84SRoland McGrath 		/* Clear the BT event */
1676ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1677ec097c84SRoland McGrath 
1678ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1679ec097c84SRoland McGrath 		if (user_mode(regs)) {
168051ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
168151ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1682ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1683ec097c84SRoland McGrath 			return;
1684ec097c84SRoland McGrath 		}
1685ec097c84SRoland McGrath 
16866cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
16876cc89badSNaveen N. Rao 			return;
16886cc89badSNaveen N. Rao 
1689ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1690ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1691ec097c84SRoland McGrath 			return;
1692ec097c84SRoland McGrath 		}
1693ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1694ec097c84SRoland McGrath 			return;
1695ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
169614cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1697f8279621SKumar Gala 
169814cf11afSPaul Mackerras 		/* Disable instruction completion */
169914cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
170014cf11afSPaul Mackerras 		/* Clear the instruction completion event */
170114cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1702f8279621SKumar Gala 
17036cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
17046cc89badSNaveen N. Rao 			return;
17056cc89badSNaveen N. Rao 
1706f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1707f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
170814cf11afSPaul Mackerras 			return;
170914cf11afSPaul Mackerras 		}
1710f8279621SKumar Gala 
1711f8279621SKumar Gala 		if (debugger_sstep(regs))
1712f8279621SKumar Gala 			return;
1713f8279621SKumar Gala 
17143bffb652SDave Kleikamp 		if (user_mode(regs)) {
171551ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
171651ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
171751ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
17183bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
17193bffb652SDave Kleikamp 			else
17203bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
172151ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
17223bffb652SDave Kleikamp 		}
1723f8279621SKumar Gala 
1724f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
17253bffb652SDave Kleikamp 	} else
17263bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
172714cf11afSPaul Mackerras }
172803465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1729172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
173014cf11afSPaul Mackerras 
173114cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
173214cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
173314cf11afSPaul Mackerras {
173414cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
173514cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
173614cf11afSPaul Mackerras }
173714cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
173814cf11afSPaul Mackerras 
173914cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1740dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
174114cf11afSPaul Mackerras {
174214cf11afSPaul Mackerras 	int err;
174314cf11afSPaul Mackerras 
174414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
174514cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
174614cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
17478dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
174814cf11afSPaul Mackerras 	}
174914cf11afSPaul Mackerras 
1750dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1751dc1c1ca3SStephen Rothwell 
1752eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
175314cf11afSPaul Mackerras 	err = emulate_altivec(regs);
175414cf11afSPaul Mackerras 	if (err == 0) {
175514cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
175614cf11afSPaul Mackerras 		emulate_single_step(regs);
175714cf11afSPaul Mackerras 		return;
175814cf11afSPaul Mackerras 	}
175914cf11afSPaul Mackerras 
176014cf11afSPaul Mackerras 	if (err == -EFAULT) {
176114cf11afSPaul Mackerras 		/* got an error reading the instruction */
176214cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
176314cf11afSPaul Mackerras 	} else {
176414cf11afSPaul Mackerras 		/* didn't recognize the instruction */
176514cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
176676462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
176714cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1768de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
176914cf11afSPaul Mackerras 	}
177014cf11afSPaul Mackerras }
177114cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
177214cf11afSPaul Mackerras 
177314cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
177414cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
177514cf11afSPaul Mackerras 			   unsigned long error_code)
177614cf11afSPaul Mackerras {
177714cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
177814cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
177914cf11afSPaul Mackerras 	 * something smarter
178014cf11afSPaul Mackerras 	 */
178114cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
178214cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
178314cf11afSPaul Mackerras 	return;
178414cf11afSPaul Mackerras }
178514cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
178614cf11afSPaul Mackerras 
178714cf11afSPaul Mackerras #ifdef CONFIG_SPE
178814cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
178914cf11afSPaul Mackerras {
17906a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
179114cf11afSPaul Mackerras 	unsigned long spefscr;
179214cf11afSPaul Mackerras 	int fpexc_mode;
179314cf11afSPaul Mackerras 	int code = 0;
17946a800f36SLiu Yu 	int err;
17956a800f36SLiu Yu 
1796685659eeSyu liu 	flush_spe_to_thread(current);
179714cf11afSPaul Mackerras 
179814cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
179914cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
180014cf11afSPaul Mackerras 
180114cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
180214cf11afSPaul Mackerras 		code = FPE_FLTOVF;
180314cf11afSPaul Mackerras 	}
180414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
180514cf11afSPaul Mackerras 		code = FPE_FLTUND;
180614cf11afSPaul Mackerras 	}
180714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
180814cf11afSPaul Mackerras 		code = FPE_FLTDIV;
180914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
181014cf11afSPaul Mackerras 		code = FPE_FLTINV;
181114cf11afSPaul Mackerras 	}
181214cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
181314cf11afSPaul Mackerras 		code = FPE_FLTRES;
181414cf11afSPaul Mackerras 
18156a800f36SLiu Yu 	err = do_spe_mathemu(regs);
18166a800f36SLiu Yu 	if (err == 0) {
18176a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18186a800f36SLiu Yu 		emulate_single_step(regs);
181914cf11afSPaul Mackerras 		return;
182014cf11afSPaul Mackerras 	}
18216a800f36SLiu Yu 
18226a800f36SLiu Yu 	if (err == -EFAULT) {
18236a800f36SLiu Yu 		/* got an error reading the instruction */
18246a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18256a800f36SLiu Yu 	} else if (err == -EINVAL) {
18266a800f36SLiu Yu 		/* didn't recognize the instruction */
18276a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18286a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18296a800f36SLiu Yu 	} else {
18306a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
18316a800f36SLiu Yu 	}
18326a800f36SLiu Yu 
18336a800f36SLiu Yu 	return;
18346a800f36SLiu Yu }
18356a800f36SLiu Yu 
18366a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
18376a800f36SLiu Yu {
18386a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
18396a800f36SLiu Yu 	int err;
18406a800f36SLiu Yu 
18416a800f36SLiu Yu 	preempt_disable();
18426a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
18436a800f36SLiu Yu 		giveup_spe(current);
18446a800f36SLiu Yu 	preempt_enable();
18456a800f36SLiu Yu 
18466a800f36SLiu Yu 	regs->nip -= 4;
18476a800f36SLiu Yu 	err = speround_handler(regs);
18486a800f36SLiu Yu 	if (err == 0) {
18496a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18506a800f36SLiu Yu 		emulate_single_step(regs);
18516a800f36SLiu Yu 		return;
18526a800f36SLiu Yu 	}
18536a800f36SLiu Yu 
18546a800f36SLiu Yu 	if (err == -EFAULT) {
18556a800f36SLiu Yu 		/* got an error reading the instruction */
18566a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18576a800f36SLiu Yu 	} else if (err == -EINVAL) {
18586a800f36SLiu Yu 		/* didn't recognize the instruction */
18596a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18606a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18616a800f36SLiu Yu 	} else {
18626a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
18636a800f36SLiu Yu 		return;
18646a800f36SLiu Yu 	}
18656a800f36SLiu Yu }
186614cf11afSPaul Mackerras #endif
186714cf11afSPaul Mackerras 
1868dc1c1ca3SStephen Rothwell /*
1869dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1870dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1871dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1872dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1873dc1c1ca3SStephen Rothwell  */
1874dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1875dc1c1ca3SStephen Rothwell {
1876dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1877dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1878dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1879dc1c1ca3SStephen Rothwell }
188015770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
1881dc1c1ca3SStephen Rothwell 
18821e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
188314cf11afSPaul Mackerras /*
188414cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
188514cf11afSPaul Mackerras  * spins until a reboot occurs
188614cf11afSPaul Mackerras  */
188714cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
188814cf11afSPaul Mackerras {
188914cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
189014cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
189114cf11afSPaul Mackerras 	return;
189214cf11afSPaul Mackerras }
189314cf11afSPaul Mackerras 
189414cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
189514cf11afSPaul Mackerras {
189614cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
189714cf11afSPaul Mackerras 	WatchdogHandler(regs);
189814cf11afSPaul Mackerras }
189914cf11afSPaul Mackerras #endif
1900dc1c1ca3SStephen Rothwell 
1901dc1c1ca3SStephen Rothwell /*
1902dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1903dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1904dc1c1ca3SStephen Rothwell  */
1905dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1906dc1c1ca3SStephen Rothwell {
1907dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1908dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1909dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1910dc1c1ca3SStephen Rothwell }
191115770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
191214cf11afSPaul Mackerras 
191314cf11afSPaul Mackerras void __init trap_init(void)
191414cf11afSPaul Mackerras {
191514cf11afSPaul Mackerras }
191680947e7cSGeert Uytterhoeven 
191780947e7cSGeert Uytterhoeven 
191880947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
191980947e7cSGeert Uytterhoeven 
192080947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
192180947e7cSGeert Uytterhoeven 
192280947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
192380947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
192480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
192580947e7cSGeert Uytterhoeven #endif
192680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
192780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
192880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
192980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
193080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
193180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
193280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
193380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
193480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
193580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1936a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
193780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
193880947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
193980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
194080947e7cSGeert Uytterhoeven #endif
194180947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
194280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
194380947e7cSGeert Uytterhoeven #endif
1944efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1945efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1946efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1947f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
1948efcac658SAlexey Kardashevskiy #endif
194980947e7cSGeert Uytterhoeven };
195080947e7cSGeert Uytterhoeven 
195180947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
195280947e7cSGeert Uytterhoeven 
195380947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
195480947e7cSGeert Uytterhoeven {
195576462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
195680947e7cSGeert Uytterhoeven 			    type);
195780947e7cSGeert Uytterhoeven }
195880947e7cSGeert Uytterhoeven 
195980947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
196080947e7cSGeert Uytterhoeven {
196180947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
196280947e7cSGeert Uytterhoeven 	unsigned int i;
196380947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
196480947e7cSGeert Uytterhoeven 
196580947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
196680947e7cSGeert Uytterhoeven 		return -ENODEV;
196780947e7cSGeert Uytterhoeven 
196880947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
196980947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
197080947e7cSGeert Uytterhoeven 	if (!dir)
197180947e7cSGeert Uytterhoeven 		return -ENOMEM;
197280947e7cSGeert Uytterhoeven 
197380947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
197480947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
197580947e7cSGeert Uytterhoeven 	if (!d)
197680947e7cSGeert Uytterhoeven 		goto fail;
197780947e7cSGeert Uytterhoeven 
197880947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
197980947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
198080947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
198180947e7cSGeert Uytterhoeven 		if (!d)
198280947e7cSGeert Uytterhoeven 			goto fail;
198380947e7cSGeert Uytterhoeven 	}
198480947e7cSGeert Uytterhoeven 
198580947e7cSGeert Uytterhoeven 	return 0;
198680947e7cSGeert Uytterhoeven 
198780947e7cSGeert Uytterhoeven fail:
198880947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
198980947e7cSGeert Uytterhoeven 	return -ENOMEM;
199080947e7cSGeert Uytterhoeven }
199180947e7cSGeert Uytterhoeven 
199280947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
199380947e7cSGeert Uytterhoeven 
199480947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1995