xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision ddc6cd0d70a6f958b2c01758ee53279d36db2234)
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>
2014cf11afSPaul Mackerras #include <linux/kernel.h>
2114cf11afSPaul Mackerras #include <linux/mm.h>
2214cf11afSPaul Mackerras #include <linux/stddef.h>
2314cf11afSPaul Mackerras #include <linux/unistd.h>
248dad3f92SPaul Mackerras #include <linux/ptrace.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/init.h>
288a39b05fSPaul Gortmaker #include <linux/extable.h>
298a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
308dad3f92SPaul Mackerras #include <linux/prctl.h>
3114cf11afSPaul Mackerras #include <linux/delay.h>
3214cf11afSPaul Mackerras #include <linux/kprobes.h>
33cc532915SMichael Ellerman #include <linux/kexec.h>
345474c120SMichael Hanselmann #include <linux/backlight.h>
3573c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
361eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3780947e7cSGeert Uytterhoeven #include <linux/debugfs.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>
4314cf11afSPaul Mackerras #include <asm/uaccess.h>
4414cf11afSPaul Mackerras #include <asm/io.h>
4586417780SPaul Mackerras #include <asm/machdep.h>
4686417780SPaul Mackerras #include <asm/rtas.h>
47f7f6f4feSDavid Gibson #include <asm/pmc.h>
4814cf11afSPaul Mackerras #include <asm/reg.h>
4914cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5014cf11afSPaul Mackerras #include <asm/backlight.h>
5114cf11afSPaul Mackerras #endif
52dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5386417780SPaul Mackerras #include <asm/firmware.h>
54dc1c1ca3SStephen Rothwell #include <asm/processor.h>
556ce6c629SMichael Neuling #include <asm/tm.h>
56dc1c1ca3SStephen Rothwell #endif
57c0ce7d08SDavid Wilder #include <asm/kexec.h>
5816c57b36SKumar Gala #include <asm/ppc-opcode.h>
59cce1f106SShaohui Xie #include <asm/rio.h>
60ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
61ae3a197eSDavid Howells #include <asm/switch_to.h>
62f54db641SMichael Neuling #include <asm/tm.h>
63ae3a197eSDavid Howells #include <asm/debug.h>
6442f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
65fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
664e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
67dc1c1ca3SStephen Rothwell 
687dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
695be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
705be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
715be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
725be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
735be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
749422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
755be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7614cf11afSPaul Mackerras 
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
829422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8414cf11afSPaul Mackerras #endif
8514cf11afSPaul Mackerras 
868b3c34cfSMichael Neuling /* Transactional Memory trap debug */
878b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
888b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
898b3c34cfSMichael Neuling #else
908b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
918b3c34cfSMichael Neuling #endif
928b3c34cfSMichael Neuling 
9314cf11afSPaul Mackerras /*
9414cf11afSPaul Mackerras  * Trap & Exception support
9514cf11afSPaul Mackerras  */
9614cf11afSPaul Mackerras 
976031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
986031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
996031d9d9Santon@samba.org {
1006031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1016031d9d9Santon@samba.org 	if (pmac_backlight) {
1026031d9d9Santon@samba.org 		struct backlight_properties *props;
1036031d9d9Santon@samba.org 
1046031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1056031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1066031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1076031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1086031d9d9Santon@samba.org 	}
1096031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1106031d9d9Santon@samba.org }
1116031d9d9Santon@samba.org #else
1126031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1136031d9d9Santon@samba.org #endif
1146031d9d9Santon@samba.org 
115760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
116760ca4dcSAnton Blanchard static int die_owner = -1;
117760ca4dcSAnton Blanchard static unsigned int die_nest_count;
118c0ce7d08SDavid Wilder static int die_counter;
119760ca4dcSAnton Blanchard 
12003465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
121760ca4dcSAnton Blanchard {
122760ca4dcSAnton Blanchard 	int cpu;
12334c2a14fSanton@samba.org 	unsigned long flags;
12414cf11afSPaul Mackerras 
12514cf11afSPaul Mackerras 	if (debugger(regs))
12614cf11afSPaul Mackerras 		return 1;
12714cf11afSPaul Mackerras 
128293e4688Santon@samba.org 	oops_enter();
129293e4688Santon@samba.org 
130760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
131760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
132760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
133760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
134760ca4dcSAnton Blanchard 		if (cpu == die_owner)
135760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
136760ca4dcSAnton Blanchard 		else
137760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
138760ca4dcSAnton Blanchard 	}
139760ca4dcSAnton Blanchard 	die_nest_count++;
140760ca4dcSAnton Blanchard 	die_owner = cpu;
14114cf11afSPaul Mackerras 	console_verbose();
14214cf11afSPaul Mackerras 	bust_spinlocks(1);
1436031d9d9Santon@samba.org 	if (machine_is(powermac))
1446031d9d9Santon@samba.org 		pmac_backlight_unblank();
145760ca4dcSAnton Blanchard 	return flags;
14634c2a14fSanton@samba.org }
14703465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
1485474c120SMichael Hanselmann 
14903465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
150760ca4dcSAnton Blanchard 			       int signr)
151760ca4dcSAnton Blanchard {
15214cf11afSPaul Mackerras 	bust_spinlocks(0);
153760ca4dcSAnton Blanchard 	die_owner = -1;
154373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
155760ca4dcSAnton Blanchard 	die_nest_count--;
15658154c8cSAnton Blanchard 	oops_exit();
15758154c8cSAnton Blanchard 	printk("\n");
158760ca4dcSAnton Blanchard 	if (!die_nest_count)
159760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
160760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
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 {
230760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
231760ca4dcSAnton Blanchard 
232760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
233760ca4dcSAnton Blanchard 		err = 0;
234760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
235760ca4dcSAnton Blanchard }
236760ca4dcSAnton Blanchard 
23725baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23825baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23925baa35bSOleg Nesterov {
24025baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
24125baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
24225baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
24325baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
24425baa35bSOleg Nesterov }
24525baa35bSOleg Nesterov 
24614cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24714cf11afSPaul Mackerras {
24814cf11afSPaul Mackerras 	siginfo_t info;
249d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
250d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
251d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
252d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
25314cf11afSPaul Mackerras 
25414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
255760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
25614cf11afSPaul Mackerras 		return;
257760ca4dcSAnton Blanchard 	}
258760ca4dcSAnton Blanchard 
259760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
26076462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
261d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
262d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
26314cf11afSPaul Mackerras 	}
26414cf11afSPaul Mackerras 
265a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2669f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2679f2f79e3SBenjamin Herrenschmidt 
26841ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26914cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
27014cf11afSPaul Mackerras 	info.si_signo = signr;
27114cf11afSPaul Mackerras 	info.si_code = code;
27214cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
27314cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
27414cf11afSPaul Mackerras }
27514cf11afSPaul Mackerras 
27614cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27714cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27814cf11afSPaul Mackerras {
27914cf11afSPaul Mackerras 	/* See if any machine dependent calls */
280c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
281c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
282c902be71SArnd Bergmann 			return;
283c902be71SArnd Bergmann 	}
28414cf11afSPaul Mackerras 
2858dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
28614cf11afSPaul Mackerras 
28714cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28814cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28914cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
29014cf11afSPaul Mackerras 
29114cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
29214cf11afSPaul Mackerras }
2931e9b4507SMahesh Salgaonkar 
2941e9b4507SMahesh Salgaonkar /*
2951e9b4507SMahesh Salgaonkar  * This function is called in real mode. Strictly no printk's please.
2961e9b4507SMahesh Salgaonkar  *
2971e9b4507SMahesh Salgaonkar  * regs->nip and regs->msr contains srr0 and ssr1.
2981e9b4507SMahesh Salgaonkar  */
2991e9b4507SMahesh Salgaonkar long machine_check_early(struct pt_regs *regs)
3001e9b4507SMahesh Salgaonkar {
3014c703416SMahesh Salgaonkar 	long handled = 0;
3024c703416SMahesh Salgaonkar 
30369111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
304e6654d5bSMahesh Salgaonkar 
30527ea2c42SDaniel Axtens 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
30627ea2c42SDaniel Axtens 
3074c703416SMahesh Salgaonkar 	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
3084c703416SMahesh Salgaonkar 		handled = cur_cpu_spec->machine_check_early(regs);
3094c703416SMahesh Salgaonkar 	return handled;
3101e9b4507SMahesh Salgaonkar }
3111e9b4507SMahesh Salgaonkar 
3120869b6fdSMahesh Salgaonkar long hmi_exception_realmode(struct pt_regs *regs)
3130869b6fdSMahesh Salgaonkar {
31469111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.hmi_exceptions);
3150869b6fdSMahesh Salgaonkar 
316fd7bacbcSMahesh Salgaonkar 	wait_for_subcore_guest_exit();
317fd7bacbcSMahesh Salgaonkar 
3180869b6fdSMahesh Salgaonkar 	if (ppc_md.hmi_exception_early)
3190869b6fdSMahesh Salgaonkar 		ppc_md.hmi_exception_early(regs);
3200869b6fdSMahesh Salgaonkar 
321fd7bacbcSMahesh Salgaonkar 	wait_for_tb_resync();
322fd7bacbcSMahesh Salgaonkar 
3230869b6fdSMahesh Salgaonkar 	return 0;
3240869b6fdSMahesh Salgaonkar }
3250869b6fdSMahesh Salgaonkar 
32614cf11afSPaul Mackerras #endif
32714cf11afSPaul Mackerras 
32814cf11afSPaul Mackerras /*
32914cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
33014cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
33114cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
33214cf11afSPaul Mackerras  * table.
33314cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
33414cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
33514cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
33614cf11afSPaul Mackerras  *  -- paulus.
33714cf11afSPaul Mackerras  */
33814cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
33914cf11afSPaul Mackerras {
34068a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
34114cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
34214cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
34314cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
34414cf11afSPaul Mackerras 
34514cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
34614cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
34714cf11afSPaul Mackerras 		/*
34814cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
34914cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
35014cf11afSPaul Mackerras 		 * As the address is in the exception table
35114cf11afSPaul Mackerras 		 * we should be able to read the instr there.
35214cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
35314cf11afSPaul Mackerras 		 * load or store.
35414cf11afSPaul Mackerras 		 */
355*ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
35614cf11afSPaul Mackerras 			nip -= 2;
357*ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
35814cf11afSPaul Mackerras 			--nip;
359*ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
36014cf11afSPaul Mackerras 			unsigned int rb;
36114cf11afSPaul Mackerras 
36214cf11afSPaul Mackerras 			--nip;
36314cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
36414cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
36514cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
36614cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
36714cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
36814cf11afSPaul Mackerras 			regs->nip = entry->fixup;
36914cf11afSPaul Mackerras 			return 1;
37014cf11afSPaul Mackerras 		}
37114cf11afSPaul Mackerras 	}
37268a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
37314cf11afSPaul Mackerras 	return 0;
37414cf11afSPaul Mackerras }
37514cf11afSPaul Mackerras 
376172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
37714cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
37814cf11afSPaul Mackerras    is in the ESR. */
37914cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
38014cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
38114cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
38214cf11afSPaul Mackerras #else
383fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
38414cf11afSPaul Mackerras #endif
38514cf11afSPaul Mackerras #define REASON_FP		ESR_FP
38614cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
38714cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
38814cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
38914cf11afSPaul Mackerras 
39014cf11afSPaul Mackerras /* single-step stuff */
39151ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
39251ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
39314cf11afSPaul Mackerras 
39414cf11afSPaul Mackerras #else
39514cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
39614cf11afSPaul Mackerras    exception is in the MSR. */
39714cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
39814cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3998b3c34cfSMichael Neuling #define REASON_TM		0x200000
40014cf11afSPaul Mackerras #define REASON_FP		0x100000
40114cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
40214cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
40314cf11afSPaul Mackerras #define REASON_TRAP		0x20000
40414cf11afSPaul Mackerras 
40514cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
40614cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
40714cf11afSPaul Mackerras #endif
40814cf11afSPaul Mackerras 
40947c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
41047c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
41114cf11afSPaul Mackerras {
4121a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
41314cf11afSPaul Mackerras 
41414cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
41514cf11afSPaul Mackerras 		printk("Instruction");
41614cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
41714cf11afSPaul Mackerras 	} else
41814cf11afSPaul Mackerras 		printk("Data");
41914cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
42047c0bd1aSBenjamin Herrenschmidt 
42147c0bd1aSBenjamin Herrenschmidt 	return 0;
42247c0bd1aSBenjamin Herrenschmidt }
42347c0bd1aSBenjamin Herrenschmidt 
42447c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
42547c0bd1aSBenjamin Herrenschmidt {
42647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
42747c0bd1aSBenjamin Herrenschmidt 
42814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42914cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
43014cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
43114cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
43214cf11afSPaul Mackerras 	}
43314cf11afSPaul Mackerras 	else {
43414cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
43514cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
43614cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
43714cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
43814cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
43914cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
44014cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
44114cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
44214cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
44314cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
44414cf11afSPaul Mackerras 			flush_instruction_cache();
44514cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
44614cf11afSPaul Mackerras 		}
44714cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
44814cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
44914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
45014cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
45114cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
45214cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
45314cf11afSPaul Mackerras 
45414cf11afSPaul Mackerras 		/* Clear MCSR */
45514cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
45614cf11afSPaul Mackerras 	}
45747c0bd1aSBenjamin Herrenschmidt 	return 0;
45847c0bd1aSBenjamin Herrenschmidt }
459fc5e7097SDave Kleikamp 
460fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
461fc5e7097SDave Kleikamp {
462fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
463fc5e7097SDave Kleikamp 	u32 mcsr;
464fc5e7097SDave Kleikamp 
465fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
466fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
467fc5e7097SDave Kleikamp 		printk(KERN_ERR
468fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
469fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
470fc5e7097SDave Kleikamp 		return 0;
471fc5e7097SDave Kleikamp 	}
472fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
473fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
474fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
475fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
476fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
477fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
478fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
479fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
480fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
481fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
482fc5e7097SDave Kleikamp 		flush_instruction_cache();
483fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
484fc5e7097SDave Kleikamp 	}
485fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
486fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
487fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
488fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
489fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
490fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
491fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
492fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
493fc5e7097SDave Kleikamp 
494fc5e7097SDave Kleikamp 	/* Clear MCSR */
495fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
496fc5e7097SDave Kleikamp 
497fc5e7097SDave Kleikamp 	return 0;
498fc5e7097SDave Kleikamp }
49914cf11afSPaul Mackerras #elif defined(CONFIG_E500)
500fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
501fe04b112SScott Wood {
502fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
503fe04b112SScott Wood 	unsigned long reason = mcsr;
504fe04b112SScott Wood 	int recoverable = 1;
505fe04b112SScott Wood 
50682a9a480SScott Wood 	if (reason & MCSR_LD) {
507cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
508cce1f106SShaohui Xie 		if (recoverable == 1)
509cce1f106SShaohui Xie 			goto silent_out;
510cce1f106SShaohui Xie 	}
511cce1f106SShaohui Xie 
512fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
513fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
514fe04b112SScott Wood 
515fe04b112SScott Wood 	if (reason & MCSR_MCP)
516fe04b112SScott Wood 		printk("Machine Check Signal\n");
517fe04b112SScott Wood 
518fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
519fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
520fe04b112SScott Wood 
521fe04b112SScott Wood 		/*
522fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
523fe04b112SScott Wood 		 */
524fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
525fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
526fe04b112SScott Wood 			;
527fe04b112SScott Wood 
528fe04b112SScott Wood 		/*
529fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
530fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
531fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
532fe04b112SScott Wood 		 */
533fe04b112SScott Wood 		reason &= ~MCSR_IF;
534fe04b112SScott Wood 	}
535fe04b112SScott Wood 
536fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
537fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
53837caf9f2SKumar Gala 
53937caf9f2SKumar Gala 		/*
54037caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
54137caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
54237caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
54337caf9f2SKumar Gala 		 */
54437caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
545fe04b112SScott Wood 			recoverable = 0;
546fe04b112SScott Wood 	}
547fe04b112SScott Wood 
548fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
549fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
550fe04b112SScott Wood 		recoverable = 0;
551fe04b112SScott Wood 	}
552fe04b112SScott Wood 
553fe04b112SScott Wood 	if (reason & MCSR_NMI)
554fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
555fe04b112SScott Wood 
556fe04b112SScott Wood 	if (reason & MCSR_IF) {
557fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
558fe04b112SScott Wood 		recoverable = 0;
559fe04b112SScott Wood 	}
560fe04b112SScott Wood 
561fe04b112SScott Wood 	if (reason & MCSR_LD) {
562fe04b112SScott Wood 		printk("Load Error Report\n");
563fe04b112SScott Wood 		recoverable = 0;
564fe04b112SScott Wood 	}
565fe04b112SScott Wood 
566fe04b112SScott Wood 	if (reason & MCSR_ST) {
567fe04b112SScott Wood 		printk("Store Error Report\n");
568fe04b112SScott Wood 		recoverable = 0;
569fe04b112SScott Wood 	}
570fe04b112SScott Wood 
571fe04b112SScott Wood 	if (reason & MCSR_LDG) {
572fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
573fe04b112SScott Wood 		recoverable = 0;
574fe04b112SScott Wood 	}
575fe04b112SScott Wood 
576fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
577fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
578fe04b112SScott Wood 
579fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
580fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
581fe04b112SScott Wood 		recoverable = 0;
582fe04b112SScott Wood 	}
583fe04b112SScott Wood 
584fe04b112SScott Wood 	if (reason & MCSR_MAV) {
585fe04b112SScott Wood 		u64 addr;
586fe04b112SScott Wood 
587fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
588fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
589fe04b112SScott Wood 
590fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
591fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
592fe04b112SScott Wood 	}
593fe04b112SScott Wood 
594cce1f106SShaohui Xie silent_out:
595fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
596fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
597fe04b112SScott Wood }
598fe04b112SScott Wood 
59947c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
60047c0bd1aSBenjamin Herrenschmidt {
60147c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
60247c0bd1aSBenjamin Herrenschmidt 
603cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
604cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
605cce1f106SShaohui Xie 			return 1;
6064e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
6074e0e3435SHongtao Jia 			return 1;
608cce1f106SShaohui Xie 	}
609cce1f106SShaohui Xie 
61014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
61214cf11afSPaul Mackerras 
61314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
61414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
61514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
61614cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
61714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
61814cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
61914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
62014cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
62114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
62214cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
62314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
62414cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
62514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
62614cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
62714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
62814cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
62914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
63014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
63114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
632c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
63314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
63414cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
63514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
63614cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
63747c0bd1aSBenjamin Herrenschmidt 
63847c0bd1aSBenjamin Herrenschmidt 	return 0;
63947c0bd1aSBenjamin Herrenschmidt }
6404490c06bSKumar Gala 
6414490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6424490c06bSKumar Gala {
6434490c06bSKumar Gala 	return 0;
6444490c06bSKumar Gala }
64514cf11afSPaul Mackerras #elif defined(CONFIG_E200)
64647c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
64747c0bd1aSBenjamin Herrenschmidt {
64847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
64947c0bd1aSBenjamin Herrenschmidt 
65014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
65114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
65214cf11afSPaul Mackerras 
65314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
65414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
65514cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
65614cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
65714cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
65814cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
65914cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
66014cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
66114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
66214cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
66314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
66414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
66514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
66614cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
66747c0bd1aSBenjamin Herrenschmidt 
66847c0bd1aSBenjamin Herrenschmidt 	return 0;
66947c0bd1aSBenjamin Herrenschmidt }
67047c0bd1aSBenjamin Herrenschmidt #else
67147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
67247c0bd1aSBenjamin Herrenschmidt {
67347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
67447c0bd1aSBenjamin Herrenschmidt 
67514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
67614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
67714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
67814cf11afSPaul Mackerras 	case 0x80000:
67914cf11afSPaul Mackerras 		printk("Machine check signal\n");
68014cf11afSPaul Mackerras 		break;
68114cf11afSPaul Mackerras 	case 0:		/* for 601 */
68214cf11afSPaul Mackerras 	case 0x40000:
68314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
68414cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
68514cf11afSPaul Mackerras 		break;
68614cf11afSPaul Mackerras 	case 0x20000:
68714cf11afSPaul Mackerras 		printk("Data parity error signal\n");
68814cf11afSPaul Mackerras 		break;
68914cf11afSPaul Mackerras 	case 0x10000:
69014cf11afSPaul Mackerras 		printk("Address parity error signal\n");
69114cf11afSPaul Mackerras 		break;
69214cf11afSPaul Mackerras 	case 0x20000000:
69314cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
69414cf11afSPaul Mackerras 		break;
69514cf11afSPaul Mackerras 	case 0x40000000:
69614cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
69714cf11afSPaul Mackerras 		break;
69814cf11afSPaul Mackerras 	case 0x00100000:
69914cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
70014cf11afSPaul Mackerras 		break;
70114cf11afSPaul Mackerras 	default:
70214cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
70314cf11afSPaul Mackerras 	}
70475918a4bSOlof Johansson 	return 0;
70575918a4bSOlof Johansson }
70647c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
70775918a4bSOlof Johansson 
70875918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
70975918a4bSOlof Johansson {
710ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
71175918a4bSOlof Johansson 	int recover = 0;
71275918a4bSOlof Johansson 
71369111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.mce_exceptions);
71489713ed1SAnton Blanchard 
71547c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
71647c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
71747c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
71847c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
71947c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
72047c0bd1aSBenjamin Herrenschmidt 	 */
72175918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
72275918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
72347c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
72447c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
72575918a4bSOlof Johansson 
72647c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
727ba12eedeSLi Zhong 		goto bail;
72875918a4bSOlof Johansson 
72975918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
73047c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
73147c0bd1aSBenjamin Herrenschmidt 	 *
73247c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
73347c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
73447c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
73547c0bd1aSBenjamin Herrenschmidt 	 */
73675918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
737ba12eedeSLi Zhong 	goto bail;
73875918a4bSOlof Johansson #endif
73975918a4bSOlof Johansson 
740a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
741ba12eedeSLi Zhong 		goto bail;
74275918a4bSOlof Johansson 
74375918a4bSOlof Johansson 	if (check_io_access(regs))
744ba12eedeSLi Zhong 		goto bail;
74575918a4bSOlof Johansson 
7468dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
74714cf11afSPaul Mackerras 
74814cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
74914cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
75014cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
751ba12eedeSLi Zhong 
752ba12eedeSLi Zhong bail:
753ba12eedeSLi Zhong 	exception_exit(prev_state);
75414cf11afSPaul Mackerras }
75514cf11afSPaul Mackerras 
75614cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
75714cf11afSPaul Mackerras {
75814cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
75914cf11afSPaul Mackerras }
76014cf11afSPaul Mackerras 
7610869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
7620869b6fdSMahesh Salgaonkar {
7630869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
7640869b6fdSMahesh Salgaonkar 
7650869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
7660869b6fdSMahesh Salgaonkar 	irq_enter();
7670869b6fdSMahesh Salgaonkar 
7680869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
7690869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
7700869b6fdSMahesh Salgaonkar 
7710869b6fdSMahesh Salgaonkar 	irq_exit();
7720869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
7730869b6fdSMahesh Salgaonkar }
7740869b6fdSMahesh Salgaonkar 
775dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
77614cf11afSPaul Mackerras {
777ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
778ba12eedeSLi Zhong 
77914cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
78014cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
78114cf11afSPaul Mackerras 
78214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
783ba12eedeSLi Zhong 
784ba12eedeSLi Zhong 	exception_exit(prev_state);
78514cf11afSPaul Mackerras }
78614cf11afSPaul Mackerras 
787dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
78814cf11afSPaul Mackerras {
789ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
790ba12eedeSLi Zhong 
79114cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
79214cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
793ba12eedeSLi Zhong 		goto bail;
79414cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
795ba12eedeSLi Zhong 		goto bail;
79614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
797ba12eedeSLi Zhong 
798ba12eedeSLi Zhong bail:
799ba12eedeSLi Zhong 	exception_exit(prev_state);
80014cf11afSPaul Mackerras }
80114cf11afSPaul Mackerras 
80214cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
80314cf11afSPaul Mackerras {
80414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
80514cf11afSPaul Mackerras }
80614cf11afSPaul Mackerras 
80703465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
80814cf11afSPaul Mackerras {
809ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
810ba12eedeSLi Zhong 
8112538c2d0SK.Prasad 	clear_single_step(regs);
81214cf11afSPaul Mackerras 
81314cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
81414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
815ba12eedeSLi Zhong 		goto bail;
81614cf11afSPaul Mackerras 	if (debugger_sstep(regs))
817ba12eedeSLi Zhong 		goto bail;
81814cf11afSPaul Mackerras 
81914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
820ba12eedeSLi Zhong 
821ba12eedeSLi Zhong bail:
822ba12eedeSLi Zhong 	exception_exit(prev_state);
82314cf11afSPaul Mackerras }
82403465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
82514cf11afSPaul Mackerras 
82614cf11afSPaul Mackerras /*
82714cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
82814cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
82914cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
83014cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
83114cf11afSPaul Mackerras  */
8328dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
83314cf11afSPaul Mackerras {
8342538c2d0SK.Prasad 	if (single_stepping(regs))
8352538c2d0SK.Prasad 		single_step_exception(regs);
83614cf11afSPaul Mackerras }
83714cf11afSPaul Mackerras 
8385fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
839dc1c1ca3SStephen Rothwell {
8405fad293bSKumar Gala 	int ret = 0;
841dc1c1ca3SStephen Rothwell 
842dc1c1ca3SStephen Rothwell 	/* Invalid operation */
843dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
8445fad293bSKumar Gala 		ret = FPE_FLTINV;
845dc1c1ca3SStephen Rothwell 
846dc1c1ca3SStephen Rothwell 	/* Overflow */
847dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
8485fad293bSKumar Gala 		ret = FPE_FLTOVF;
849dc1c1ca3SStephen Rothwell 
850dc1c1ca3SStephen Rothwell 	/* Underflow */
851dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
8525fad293bSKumar Gala 		ret = FPE_FLTUND;
853dc1c1ca3SStephen Rothwell 
854dc1c1ca3SStephen Rothwell 	/* Divide by zero */
855dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
8565fad293bSKumar Gala 		ret = FPE_FLTDIV;
857dc1c1ca3SStephen Rothwell 
858dc1c1ca3SStephen Rothwell 	/* Inexact result */
859dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
8605fad293bSKumar Gala 		ret = FPE_FLTRES;
8615fad293bSKumar Gala 
8625fad293bSKumar Gala 	return ret;
8635fad293bSKumar Gala }
8645fad293bSKumar Gala 
8655fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8665fad293bSKumar Gala {
8675fad293bSKumar Gala 	int code = 0;
8685fad293bSKumar Gala 
8695fad293bSKumar Gala 	flush_fp_to_thread(current);
8705fad293bSKumar Gala 
871de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
872dc1c1ca3SStephen Rothwell 
873dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
874dc1c1ca3SStephen Rothwell }
875dc1c1ca3SStephen Rothwell 
876dc1c1ca3SStephen Rothwell /*
877dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
87814cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
87914cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
88014cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
88114cf11afSPaul Mackerras  *
88214cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
88314cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
88414cf11afSPaul Mackerras  * bits is faster and easier.
88586417780SPaul Mackerras  *
88614cf11afSPaul Mackerras  */
88714cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
88814cf11afSPaul Mackerras {
88914cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
89014cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
89114cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
89214cf11afSPaul Mackerras 	u32 num_bytes;
89314cf11afSPaul Mackerras 	unsigned long EA;
89414cf11afSPaul Mackerras 	int pos = 0;
89514cf11afSPaul Mackerras 
89614cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
89716c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
89814cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
89914cf11afSPaul Mackerras 			return -EINVAL;
90014cf11afSPaul Mackerras 
90114cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
90214cf11afSPaul Mackerras 
90316c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
90416c57b36SKumar Gala 		case PPC_INST_LSWX:
90516c57b36SKumar Gala 		case PPC_INST_STSWX:
90614cf11afSPaul Mackerras 			EA += NB_RB;
90714cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
90814cf11afSPaul Mackerras 			break;
90916c57b36SKumar Gala 		case PPC_INST_LSWI:
91016c57b36SKumar Gala 		case PPC_INST_STSWI:
91114cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
91214cf11afSPaul Mackerras 			break;
91314cf11afSPaul Mackerras 		default:
91414cf11afSPaul Mackerras 			return -EINVAL;
91514cf11afSPaul Mackerras 	}
91614cf11afSPaul Mackerras 
91714cf11afSPaul Mackerras 	while (num_bytes != 0)
91814cf11afSPaul Mackerras 	{
91914cf11afSPaul Mackerras 		u8 val;
92014cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
92114cf11afSPaul Mackerras 
92280aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
92380aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
92480aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
92580aa0fb4SJames Yang 
92616c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
92716c57b36SKumar Gala 			case PPC_INST_LSWX:
92816c57b36SKumar Gala 			case PPC_INST_LSWI:
92914cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
93014cf11afSPaul Mackerras 					return -EFAULT;
93114cf11afSPaul Mackerras 				/* first time updating this reg,
93214cf11afSPaul Mackerras 				 * zero it out */
93314cf11afSPaul Mackerras 				if (pos == 0)
93414cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
93514cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
93614cf11afSPaul Mackerras 				break;
93716c57b36SKumar Gala 			case PPC_INST_STSWI:
93816c57b36SKumar Gala 			case PPC_INST_STSWX:
93914cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
94014cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
94114cf11afSPaul Mackerras 					return -EFAULT;
94214cf11afSPaul Mackerras 				break;
94314cf11afSPaul Mackerras 		}
94414cf11afSPaul Mackerras 		/* move EA to next address */
94514cf11afSPaul Mackerras 		EA += 1;
94614cf11afSPaul Mackerras 		num_bytes--;
94714cf11afSPaul Mackerras 
94814cf11afSPaul Mackerras 		/* manage our position within the register */
94914cf11afSPaul Mackerras 		if (++pos == 4) {
95014cf11afSPaul Mackerras 			pos = 0;
95114cf11afSPaul Mackerras 			if (++rT == 32)
95214cf11afSPaul Mackerras 				rT = 0;
95314cf11afSPaul Mackerras 		}
95414cf11afSPaul Mackerras 	}
95514cf11afSPaul Mackerras 
95614cf11afSPaul Mackerras 	return 0;
95714cf11afSPaul Mackerras }
95814cf11afSPaul Mackerras 
959c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
960c3412dcbSWill Schmidt {
961c3412dcbSWill Schmidt 	u32 ra,rs;
962c3412dcbSWill Schmidt 	unsigned long tmp;
963c3412dcbSWill Schmidt 
964c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
965c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
966c3412dcbSWill Schmidt 
967c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
968c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
969c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
970c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
971c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
972c3412dcbSWill Schmidt 
973c3412dcbSWill Schmidt 	return 0;
974c3412dcbSWill Schmidt }
975c3412dcbSWill Schmidt 
976c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
977c1469f13SKumar Gala {
978c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
979c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
980c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
981c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
982c1469f13SKumar Gala 	u8 bit;
983c1469f13SKumar Gala 	unsigned long tmp;
984c1469f13SKumar Gala 
985c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
986c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
987c1469f13SKumar Gala 
988c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
989c1469f13SKumar Gala 
990c1469f13SKumar Gala 	return 0;
991c1469f13SKumar Gala }
992c1469f13SKumar Gala 
9936ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9946ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9956ce6c629SMichael Neuling {
9966ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9976ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9986ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9996ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
10006ce6c629SMichael Neuling 	 */
10016ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
10026ce6c629SMichael Neuling 		tm_enable();
10036ce6c629SMichael Neuling 		tm_abort(cause);
10046ce6c629SMichael Neuling 		return true;
10056ce6c629SMichael Neuling 	}
10066ce6c629SMichael Neuling 	return false;
10076ce6c629SMichael Neuling }
10086ce6c629SMichael Neuling #else
10096ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
10106ce6c629SMichael Neuling {
10116ce6c629SMichael Neuling 	return false;
10126ce6c629SMichael Neuling }
10136ce6c629SMichael Neuling #endif
10146ce6c629SMichael Neuling 
101514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
101614cf11afSPaul Mackerras {
101714cf11afSPaul Mackerras 	u32 instword;
101814cf11afSPaul Mackerras 	u32 rd;
101914cf11afSPaul Mackerras 
10204288e343SAnton Blanchard 	if (!user_mode(regs))
102114cf11afSPaul Mackerras 		return -EINVAL;
102214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
102314cf11afSPaul Mackerras 
102414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
102514cf11afSPaul Mackerras 		return -EFAULT;
102614cf11afSPaul Mackerras 
102714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
102816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1029eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
103014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
103114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
103214cf11afSPaul Mackerras 		return 0;
103314cf11afSPaul Mackerras 	}
103414cf11afSPaul Mackerras 
103514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
103680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1037eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
103814cf11afSPaul Mackerras 		return 0;
103980947e7cSGeert Uytterhoeven 	}
104014cf11afSPaul Mackerras 
104114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
104216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
104386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
104414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
104514cf11afSPaul Mackerras 
1046eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
104714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
104814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
104914cf11afSPaul Mackerras 		return 0;
105014cf11afSPaul Mackerras 	}
105114cf11afSPaul Mackerras 
105214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
105380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
10546ce6c629SMichael Neuling 		if (tm_abort_check(regs,
10556ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
10566ce6c629SMichael Neuling 			return -EINVAL;
1057eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
105814cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
105980947e7cSGeert Uytterhoeven 	}
106014cf11afSPaul Mackerras 
1061c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
106216c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1063eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1064c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1065c3412dcbSWill Schmidt 	}
1066c3412dcbSWill Schmidt 
1067c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
106816c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1069eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1070c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1071c1469f13SKumar Gala 	}
1072c1469f13SKumar Gala 
10739863c28aSJames Yang 	/* Emulate sync instruction variants */
10749863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
10759863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
10769863c28aSJames Yang 		asm volatile("sync");
10779863c28aSJames Yang 		return 0;
10789863c28aSJames Yang 	}
10799863c28aSJames Yang 
1080efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1081efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
108273d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
108373d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
108473d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
108573d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1086efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1087efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1088efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1089efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1090efcac658SAlexey Kardashevskiy 		return 0;
1091efcac658SAlexey Kardashevskiy 	}
1092efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
109373d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
109473d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
109573d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
109673d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1097efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1098efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1099efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
110000ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1101efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
110200ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1103efcac658SAlexey Kardashevskiy 		return 0;
1104efcac658SAlexey Kardashevskiy 	}
1105efcac658SAlexey Kardashevskiy #endif
1106efcac658SAlexey Kardashevskiy 
110714cf11afSPaul Mackerras 	return -EINVAL;
110814cf11afSPaul Mackerras }
110914cf11afSPaul Mackerras 
111073c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
111114cf11afSPaul Mackerras {
111273c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
111314cf11afSPaul Mackerras }
111414cf11afSPaul Mackerras 
11153a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
11163a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
11173a3b5aa6SKevin Hao {
11183a3b5aa6SKevin Hao 	int ret;
11193a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
11203a3b5aa6SKevin Hao 
11213a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
11223a3b5aa6SKevin Hao 	if (ret >= 0)
11233a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
11243a3b5aa6SKevin Hao 
11253a3b5aa6SKevin Hao 	switch (ret) {
11263a3b5aa6SKevin Hao 	case 0:
11273a3b5aa6SKevin Hao 		emulate_single_step(regs);
11283a3b5aa6SKevin Hao 		return 0;
11293a3b5aa6SKevin Hao 	case 1: {
11303a3b5aa6SKevin Hao 			int code = 0;
1131de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
11323a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
11333a3b5aa6SKevin Hao 			return 0;
11343a3b5aa6SKevin Hao 		}
11353a3b5aa6SKevin Hao 	case -EFAULT:
11363a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11373a3b5aa6SKevin Hao 		return 0;
11383a3b5aa6SKevin Hao 	}
11393a3b5aa6SKevin Hao 
11403a3b5aa6SKevin Hao 	return -1;
11413a3b5aa6SKevin Hao }
11423a3b5aa6SKevin Hao #else
11433a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
11443a3b5aa6SKevin Hao #endif
11453a3b5aa6SKevin Hao 
114603465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
114714cf11afSPaul Mackerras {
1148ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
114914cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
115014cf11afSPaul Mackerras 
1151aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
115204903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
115314cf11afSPaul Mackerras 
115414cf11afSPaul Mackerras 	if (reason & REASON_FP) {
115514cf11afSPaul Mackerras 		/* IEEE FP exception */
1156dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1157ba12eedeSLi Zhong 		goto bail;
11588dad3f92SPaul Mackerras 	}
11598dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1160a4c3f909SBalbir Singh 		unsigned long bugaddr;
1161ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1162ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1163ba797b28SJason Wessel 		if (debugger_bpt(regs))
1164ba12eedeSLi Zhong 			goto bail;
1165ba797b28SJason Wessel 
116614cf11afSPaul Mackerras 		/* trap exception */
1167dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1168dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1169ba12eedeSLi Zhong 			goto bail;
117073c9ceabSJeremy Fitzhardinge 
1171a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1172a4c3f909SBalbir Singh 		/*
1173a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1174a4c3f909SBalbir Singh 		 */
1175a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1176a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1177a4c3f909SBalbir Singh 
117873c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1179a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
118014cf11afSPaul Mackerras 			regs->nip += 4;
1181ba12eedeSLi Zhong 			goto bail;
118214cf11afSPaul Mackerras 		}
11838dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1184ba12eedeSLi Zhong 		goto bail;
11858dad3f92SPaul Mackerras 	}
1186bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1187bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1188bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1189bc2a9408SMichael Neuling 		 * This occurs when:
1190bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1191bc2a9408SMichael Neuling 		 *    transition in TM states.
1192bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1193bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1194bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1195bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1196bc2a9408SMichael Neuling 		 */
1197bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1198bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1199bc2a9408SMichael Neuling 			regs->nip += 4;
1200ba12eedeSLi Zhong 			goto bail;
1201bc2a9408SMichael Neuling 		}
1202bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1203bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1204bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1205bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1206bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1207bc2a9408SMichael Neuling 		 */
1208bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1209bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1210ba12eedeSLi Zhong 			goto bail;
1211bc2a9408SMichael Neuling 		} else {
1212bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1213bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1214bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1215bc2a9408SMichael Neuling 		}
1216bc2a9408SMichael Neuling 	}
1217bc2a9408SMichael Neuling #endif
12188dad3f92SPaul Mackerras 
1219b3f6a459SMichael Ellerman 	/*
1220b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1221b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1222b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1223b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1224b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1225b3f6a459SMichael Ellerman 	 */
1226b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1227b3f6a459SMichael Ellerman 		goto sigill;
1228b3f6a459SMichael Ellerman 
1229a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1230a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1231cd8a5673SPaul Mackerras 		local_irq_enable();
1232cd8a5673SPaul Mackerras 
123304903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
123404903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
123504903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
123604903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
123704903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
123804903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
12394e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
12404e63f8edSBenjamin Herrenschmidt 	 */
12413a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1242ba12eedeSLi Zhong 		goto bail;
124304903a30SKumar Gala 
12448dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
12458dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
124614cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
124714cf11afSPaul Mackerras 		case 0:
124814cf11afSPaul Mackerras 			regs->nip += 4;
124914cf11afSPaul Mackerras 			emulate_single_step(regs);
1250ba12eedeSLi Zhong 			goto bail;
125114cf11afSPaul Mackerras 		case -EFAULT:
125214cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1253ba12eedeSLi Zhong 			goto bail;
12548dad3f92SPaul Mackerras 		}
12558dad3f92SPaul Mackerras 	}
12568dad3f92SPaul Mackerras 
1257b3f6a459SMichael Ellerman sigill:
125814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
125914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
126014cf11afSPaul Mackerras 	else
126114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1262ba12eedeSLi Zhong 
1263ba12eedeSLi Zhong bail:
1264ba12eedeSLi Zhong 	exception_exit(prev_state);
126514cf11afSPaul Mackerras }
126603465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
126714cf11afSPaul Mackerras 
1268bf593907SPaul Mackerras /*
1269bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1270bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1271bf593907SPaul Mackerras  */
127203465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1273bf593907SPaul Mackerras {
1274bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1275bf593907SPaul Mackerras 	program_check_exception(regs);
1276bf593907SPaul Mackerras }
127703465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1278bf593907SPaul Mackerras 
1279dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
128014cf11afSPaul Mackerras {
1281ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
12824393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
128314cf11afSPaul Mackerras 
1284a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1285a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1286a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1287a3512b2dSBenjamin Herrenschmidt 
12886ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12896ce6c629SMichael Neuling 		goto bail;
12906ce6c629SMichael Neuling 
1291e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1292e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
129314cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
129414cf11afSPaul Mackerras 
129514cf11afSPaul Mackerras 	if (fixed == 1) {
129614cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
129714cf11afSPaul Mackerras 		emulate_single_step(regs);
1298ba12eedeSLi Zhong 		goto bail;
129914cf11afSPaul Mackerras 	}
130014cf11afSPaul Mackerras 
130114cf11afSPaul Mackerras 	/* Operand address was bad */
130214cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
13034393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
13044393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
13054393c4f6SBenjamin Herrenschmidt 	} else {
13064393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
13074393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
130814cf11afSPaul Mackerras 	}
13094393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
13104393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
13114393c4f6SBenjamin Herrenschmidt 	else
13124393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1313ba12eedeSLi Zhong 
1314ba12eedeSLi Zhong bail:
1315ba12eedeSLi Zhong 	exception_exit(prev_state);
131614cf11afSPaul Mackerras }
131714cf11afSPaul Mackerras 
1318f0f558b1SPaul Mackerras void slb_miss_bad_addr(struct pt_regs *regs)
1319f0f558b1SPaul Mackerras {
1320f0f558b1SPaul Mackerras 	enum ctx_state prev_state = exception_enter();
1321f0f558b1SPaul Mackerras 
1322f0f558b1SPaul Mackerras 	if (user_mode(regs))
1323f0f558b1SPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1324f0f558b1SPaul Mackerras 	else
1325f0f558b1SPaul Mackerras 		bad_page_fault(regs, regs->dar, SIGSEGV);
1326f0f558b1SPaul Mackerras 
1327f0f558b1SPaul Mackerras 	exception_exit(prev_state);
1328f0f558b1SPaul Mackerras }
1329f0f558b1SPaul Mackerras 
133014cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
133114cf11afSPaul Mackerras {
133214cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
133314cf11afSPaul Mackerras 	       current, regs->gpr[1]);
133414cf11afSPaul Mackerras 	debugger(regs);
133514cf11afSPaul Mackerras 	show_regs(regs);
133614cf11afSPaul Mackerras 	panic("kernel stack overflow");
133714cf11afSPaul Mackerras }
133814cf11afSPaul Mackerras 
133914cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
134014cf11afSPaul Mackerras {
134114cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
134214cf11afSPaul Mackerras 	       regs->nip, regs->msr);
134314cf11afSPaul Mackerras 	debugger(regs);
134414cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
134514cf11afSPaul Mackerras }
134614cf11afSPaul Mackerras 
1347dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1348dc1c1ca3SStephen Rothwell {
1349ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1350ba12eedeSLi Zhong 
1351dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1352dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1353dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1354ba12eedeSLi Zhong 
1355ba12eedeSLi Zhong 	exception_exit(prev_state);
1356dc1c1ca3SStephen Rothwell }
1357dc1c1ca3SStephen Rothwell 
1358dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1359dc1c1ca3SStephen Rothwell {
1360ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1361ba12eedeSLi Zhong 
1362dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1363dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1364dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1365dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1366ba12eedeSLi Zhong 		goto bail;
1367dc1c1ca3SStephen Rothwell 	}
13686c4841c2SAnton Blanchard 
1369dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1370dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1371dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1372ba12eedeSLi Zhong 
1373ba12eedeSLi Zhong bail:
1374ba12eedeSLi Zhong 	exception_exit(prev_state);
1375dc1c1ca3SStephen Rothwell }
1376dc1c1ca3SStephen Rothwell 
1377ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1378ce48b210SMichael Neuling {
1379ce48b210SMichael Neuling 	if (user_mode(regs)) {
1380ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1381ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1382ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1383ce48b210SMichael Neuling 		return;
1384ce48b210SMichael Neuling 	}
1385ce48b210SMichael Neuling 
1386ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1387ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1388ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1389ce48b210SMichael Neuling }
1390ce48b210SMichael Neuling 
13912517617eSMichael Neuling #ifdef CONFIG_PPC64
1392021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1393d0c0c9a1SMichael Neuling {
1394021424a1SMichael Ellerman 	static char *facility_strings[] = {
13952517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13962517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13972517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13982517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13992517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
14002517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
14012517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
14022517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1403bd3ea317SJack Miller 		[FSCR_LM_LG] = "LM",
1404021424a1SMichael Ellerman 	};
14052517617eSMichael Neuling 	char *facility = "unknown";
1406021424a1SMichael Ellerman 	u64 value;
1407c952c1c4SAnshuman Khandual 	u32 instword, rd;
14082517617eSMichael Neuling 	u8 status;
14092517617eSMichael Neuling 	bool hv;
1410021424a1SMichael Ellerman 
14112517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
14122517617eSMichael Neuling 	if (hv)
1413b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
14142517617eSMichael Neuling 	else
14152517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
14162517617eSMichael Neuling 
14172517617eSMichael Neuling 	status = value >> 56;
14182517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1419c952c1c4SAnshuman Khandual 		/*
1420c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1421c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1422c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1423c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1424c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1425c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1426c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1427c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1428c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1429c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1430c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1431c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1432c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1433c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
14342517617eSMichael Neuling 		 */
1435c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1436c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1437c952c1c4SAnshuman Khandual 			return;
1438c952c1c4SAnshuman Khandual 		}
1439c952c1c4SAnshuman Khandual 
1440c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1441c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1442c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1443c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1444c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
14452517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1446b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1447b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1448c952c1c4SAnshuman Khandual 		}
1449c952c1c4SAnshuman Khandual 
1450c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1451c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1452c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1453c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1454c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1455c952c1c4SAnshuman Khandual 				return;
1456c952c1c4SAnshuman Khandual 			}
1457c952c1c4SAnshuman Khandual 			regs->nip += 4;
1458c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1459c952c1c4SAnshuman Khandual 		}
14602517617eSMichael Neuling 		return;
1461bd3ea317SJack Miller 	} else if ((status == FSCR_LM_LG) && cpu_has_feature(CPU_FTR_ARCH_300)) {
1462bd3ea317SJack Miller 		/*
1463bd3ea317SJack Miller 		 * This process has touched LM, so turn it on forever
1464bd3ea317SJack Miller 		 * for this process
1465bd3ea317SJack Miller 		 */
1466bd3ea317SJack Miller 		current->thread.fscr |= FSCR_LM;
1467bd3ea317SJack Miller 		mtspr(SPRN_FSCR, current->thread.fscr);
1468bd3ea317SJack Miller 		return;
1469b14b6260SMichael Ellerman 	}
1470b14b6260SMichael Ellerman 
14712517617eSMichael Neuling 	if ((status < ARRAY_SIZE(facility_strings)) &&
14722517617eSMichael Neuling 	    facility_strings[status])
14732517617eSMichael Neuling 		facility = facility_strings[status];
1474021424a1SMichael Ellerman 
1475d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1476d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1477d0c0c9a1SMichael Neuling 		local_irq_enable();
1478d0c0c9a1SMichael Neuling 
1479ee4ed6faSMichael Neuling 	pr_err_ratelimited(
1480ee4ed6faSMichael Neuling 		"%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
14812517617eSMichael Neuling 		hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
1482d0c0c9a1SMichael Neuling 
1483d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1484d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1485d0c0c9a1SMichael Neuling 		return;
1486d0c0c9a1SMichael Neuling 	}
1487d0c0c9a1SMichael Neuling 
1488021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1489d0c0c9a1SMichael Neuling }
14902517617eSMichael Neuling #endif
1491d0c0c9a1SMichael Neuling 
1492f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1493f54db641SMichael Neuling 
1494f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1495f54db641SMichael Neuling {
1496f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1497f54db641SMichael Neuling 
1498f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1499f54db641SMichael Neuling 		 regs->nip, regs->msr);
1500f54db641SMichael Neuling 
1501f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1502f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1503f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1504f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1505f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1506f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1507f54db641SMichael Neuling 	 */
1508d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1509f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1510f54db641SMichael Neuling 
1511f54db641SMichael Neuling 	/* Enable FP for the task: */
1512f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1513f54db641SMichael Neuling 
1514f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1515f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1516f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
15173ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
15183ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1519f54db641SMichael Neuling 	 */
15203ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
15213ac8ff1cSPaul Mackerras 
15223ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
15233ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
15243ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
15253ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
15263ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15273ac8ff1cSPaul Mackerras 	}
1528f54db641SMichael Neuling }
1529f54db641SMichael Neuling 
1530f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1531f54db641SMichael Neuling {
1532f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1533f54db641SMichael Neuling 	 * the same way.
1534f54db641SMichael Neuling 	 */
1535f54db641SMichael Neuling 
1536f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1537f54db641SMichael Neuling 		 "MSR=%lx\n",
1538f54db641SMichael Neuling 		 regs->nip, regs->msr);
1539d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1540f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
15413ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1542f54db641SMichael Neuling 	current->thread.used_vr = 1;
1543f54db641SMichael Neuling 
15443ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
15453ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
15463ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15473ac8ff1cSPaul Mackerras 	}
15483ac8ff1cSPaul Mackerras }
15493ac8ff1cSPaul Mackerras 
1550f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1551f54db641SMichael Neuling {
15523ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
15533ac8ff1cSPaul Mackerras 
1554f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1555f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1556f54db641SMichael Neuling 	 *
1557f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1558f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1559f54db641SMichael Neuling 	 */
1560f54db641SMichael Neuling 
1561f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1562f54db641SMichael Neuling 		 "MSR=%lx\n",
1563f54db641SMichael Neuling 		 regs->nip, regs->msr);
1564f54db641SMichael Neuling 
15653ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
15663ac8ff1cSPaul Mackerras 
15673ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
15683ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
15693ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
15703ac8ff1cSPaul Mackerras 		return;
15713ac8ff1cSPaul Mackerras 	}
15723ac8ff1cSPaul Mackerras 
1573f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1574d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1575f54db641SMichael Neuling 
1576f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1577f54db641SMichael Neuling 		MSR_VSX;
15783ac8ff1cSPaul Mackerras 
15793ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
15803ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
15813ac8ff1cSPaul Mackerras 	 */
15823ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
15833ac8ff1cSPaul Mackerras 
15843ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
15853ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
15863ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
15873ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
1588f54db641SMichael Neuling }
1589f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1590f54db641SMichael Neuling 
1591dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1592dc1c1ca3SStephen Rothwell {
159369111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
159489713ed1SAnton Blanchard 
1595dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1596dc1c1ca3SStephen Rothwell }
1597dc1c1ca3SStephen Rothwell 
15988dad3f92SPaul Mackerras #ifdef CONFIG_8xx
159914cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
160014cf11afSPaul Mackerras {
160114cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
160214cf11afSPaul Mackerras 
160314cf11afSPaul Mackerras 	if (!user_mode(regs)) {
160414cf11afSPaul Mackerras 		debugger(regs);
16051eb2819dSLEROY Christophe 		die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
16061eb2819dSLEROY Christophe 			regs, SIGFPE);
160714cf11afSPaul Mackerras 	}
160814cf11afSPaul Mackerras 
16093a3b5aa6SKevin Hao 	if (!emulate_math(regs))
16103a3b5aa6SKevin Hao 		return;
16115fad293bSKumar Gala 
16125fad293bSKumar Gala 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
161314cf11afSPaul Mackerras }
16148dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
161514cf11afSPaul Mackerras 
1616172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
16173bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
16183bffb652SDave Kleikamp {
16193bffb652SDave Kleikamp 	int changed = 0;
16203bffb652SDave Kleikamp 	/*
16213bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
16223bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
16233bffb652SDave Kleikamp 	 */
16243bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
16253bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
16263bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
162751ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
16283bffb652SDave Kleikamp #endif
16293bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
16303bffb652SDave Kleikamp 			     5);
16313bffb652SDave Kleikamp 		changed |= 0x01;
16323bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
16333bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
16343bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
16353bffb652SDave Kleikamp 			     6);
16363bffb652SDave Kleikamp 		changed |= 0x01;
16373bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
163851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
16393bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
16403bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
16413bffb652SDave Kleikamp 			     1);
16423bffb652SDave Kleikamp 		changed |= 0x01;
16433bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
164451ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
16453bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
16463bffb652SDave Kleikamp 			     2);
16473bffb652SDave Kleikamp 		changed |= 0x01;
16483bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
164951ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
16503bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
16513bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
16523bffb652SDave Kleikamp 			     3);
16533bffb652SDave Kleikamp 		changed |= 0x01;
16543bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
165551ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
16563bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
16573bffb652SDave Kleikamp 			     4);
16583bffb652SDave Kleikamp 		changed |= 0x01;
16593bffb652SDave Kleikamp 	}
16603bffb652SDave Kleikamp 	/*
16613bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
16623bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
16633bffb652SDave Kleikamp 	 * back on or not.
16643bffb652SDave Kleikamp 	 */
166551ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
166651ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
16673bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
16683bffb652SDave Kleikamp 	else
16693bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
167051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16713bffb652SDave Kleikamp 
16723bffb652SDave Kleikamp 	if (changed & 0x01)
167351ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
16743bffb652SDave Kleikamp }
167514cf11afSPaul Mackerras 
167603465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
167714cf11afSPaul Mackerras {
167851ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
16793bffb652SDave Kleikamp 
1680ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1681ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1682ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1683ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1684ec097c84SRoland McGrath 	 */
1685ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1686ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1687ec097c84SRoland McGrath 
1688ec097c84SRoland McGrath 		/* Disable BT */
1689ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1690ec097c84SRoland McGrath 		/* Clear the BT event */
1691ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1692ec097c84SRoland McGrath 
1693ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1694ec097c84SRoland McGrath 		if (user_mode(regs)) {
169551ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
169651ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1697ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1698ec097c84SRoland McGrath 			return;
1699ec097c84SRoland McGrath 		}
1700ec097c84SRoland McGrath 
1701ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1702ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1703ec097c84SRoland McGrath 			return;
1704ec097c84SRoland McGrath 		}
1705ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1706ec097c84SRoland McGrath 			return;
1707ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
170814cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1709f8279621SKumar Gala 
171014cf11afSPaul Mackerras 		/* Disable instruction completion */
171114cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
171214cf11afSPaul Mackerras 		/* Clear the instruction completion event */
171314cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1714f8279621SKumar Gala 
1715f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1716f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
171714cf11afSPaul Mackerras 			return;
171814cf11afSPaul Mackerras 		}
1719f8279621SKumar Gala 
1720f8279621SKumar Gala 		if (debugger_sstep(regs))
1721f8279621SKumar Gala 			return;
1722f8279621SKumar Gala 
17233bffb652SDave Kleikamp 		if (user_mode(regs)) {
172451ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
172551ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
172651ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
17273bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
17283bffb652SDave Kleikamp 			else
17293bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
173051ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
17313bffb652SDave Kleikamp 		}
1732f8279621SKumar Gala 
1733f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
17343bffb652SDave Kleikamp 	} else
17353bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
173614cf11afSPaul Mackerras }
173703465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1738172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
173914cf11afSPaul Mackerras 
174014cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
174114cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
174214cf11afSPaul Mackerras {
174314cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
174414cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
174514cf11afSPaul Mackerras }
174614cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
174714cf11afSPaul Mackerras 
174814cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1749dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
175014cf11afSPaul Mackerras {
175114cf11afSPaul Mackerras 	int err;
175214cf11afSPaul Mackerras 
175314cf11afSPaul Mackerras 	if (!user_mode(regs)) {
175414cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
175514cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
17568dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
175714cf11afSPaul Mackerras 	}
175814cf11afSPaul Mackerras 
1759dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1760dc1c1ca3SStephen Rothwell 
1761eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
176214cf11afSPaul Mackerras 	err = emulate_altivec(regs);
176314cf11afSPaul Mackerras 	if (err == 0) {
176414cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
176514cf11afSPaul Mackerras 		emulate_single_step(regs);
176614cf11afSPaul Mackerras 		return;
176714cf11afSPaul Mackerras 	}
176814cf11afSPaul Mackerras 
176914cf11afSPaul Mackerras 	if (err == -EFAULT) {
177014cf11afSPaul Mackerras 		/* got an error reading the instruction */
177114cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
177214cf11afSPaul Mackerras 	} else {
177314cf11afSPaul Mackerras 		/* didn't recognize the instruction */
177414cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
177576462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
177614cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1777de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
177814cf11afSPaul Mackerras 	}
177914cf11afSPaul Mackerras }
178014cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
178114cf11afSPaul Mackerras 
178214cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
178314cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
178414cf11afSPaul Mackerras 			   unsigned long error_code)
178514cf11afSPaul Mackerras {
178614cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
178714cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
178814cf11afSPaul Mackerras 	 * something smarter
178914cf11afSPaul Mackerras 	 */
179014cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
179114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
179214cf11afSPaul Mackerras 	return;
179314cf11afSPaul Mackerras }
179414cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
179514cf11afSPaul Mackerras 
179614cf11afSPaul Mackerras #ifdef CONFIG_SPE
179714cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
179814cf11afSPaul Mackerras {
17996a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
180014cf11afSPaul Mackerras 	unsigned long spefscr;
180114cf11afSPaul Mackerras 	int fpexc_mode;
180214cf11afSPaul Mackerras 	int code = 0;
18036a800f36SLiu Yu 	int err;
18046a800f36SLiu Yu 
1805685659eeSyu liu 	flush_spe_to_thread(current);
180614cf11afSPaul Mackerras 
180714cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
180814cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
180914cf11afSPaul Mackerras 
181014cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
181114cf11afSPaul Mackerras 		code = FPE_FLTOVF;
181214cf11afSPaul Mackerras 	}
181314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
181414cf11afSPaul Mackerras 		code = FPE_FLTUND;
181514cf11afSPaul Mackerras 	}
181614cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
181714cf11afSPaul Mackerras 		code = FPE_FLTDIV;
181814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
181914cf11afSPaul Mackerras 		code = FPE_FLTINV;
182014cf11afSPaul Mackerras 	}
182114cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
182214cf11afSPaul Mackerras 		code = FPE_FLTRES;
182314cf11afSPaul Mackerras 
18246a800f36SLiu Yu 	err = do_spe_mathemu(regs);
18256a800f36SLiu Yu 	if (err == 0) {
18266a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18276a800f36SLiu Yu 		emulate_single_step(regs);
182814cf11afSPaul Mackerras 		return;
182914cf11afSPaul Mackerras 	}
18306a800f36SLiu Yu 
18316a800f36SLiu Yu 	if (err == -EFAULT) {
18326a800f36SLiu Yu 		/* got an error reading the instruction */
18336a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18346a800f36SLiu Yu 	} else if (err == -EINVAL) {
18356a800f36SLiu Yu 		/* didn't recognize the instruction */
18366a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18376a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18386a800f36SLiu Yu 	} else {
18396a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
18406a800f36SLiu Yu 	}
18416a800f36SLiu Yu 
18426a800f36SLiu Yu 	return;
18436a800f36SLiu Yu }
18446a800f36SLiu Yu 
18456a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
18466a800f36SLiu Yu {
18476a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
18486a800f36SLiu Yu 	int err;
18496a800f36SLiu Yu 
18506a800f36SLiu Yu 	preempt_disable();
18516a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
18526a800f36SLiu Yu 		giveup_spe(current);
18536a800f36SLiu Yu 	preempt_enable();
18546a800f36SLiu Yu 
18556a800f36SLiu Yu 	regs->nip -= 4;
18566a800f36SLiu Yu 	err = speround_handler(regs);
18576a800f36SLiu Yu 	if (err == 0) {
18586a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
18596a800f36SLiu Yu 		emulate_single_step(regs);
18606a800f36SLiu Yu 		return;
18616a800f36SLiu Yu 	}
18626a800f36SLiu Yu 
18636a800f36SLiu Yu 	if (err == -EFAULT) {
18646a800f36SLiu Yu 		/* got an error reading the instruction */
18656a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
18666a800f36SLiu Yu 	} else if (err == -EINVAL) {
18676a800f36SLiu Yu 		/* didn't recognize the instruction */
18686a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
18696a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
18706a800f36SLiu Yu 	} else {
18716a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
18726a800f36SLiu Yu 		return;
18736a800f36SLiu Yu 	}
18746a800f36SLiu Yu }
187514cf11afSPaul Mackerras #endif
187614cf11afSPaul Mackerras 
1877dc1c1ca3SStephen Rothwell /*
1878dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1879dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1880dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1881dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1882dc1c1ca3SStephen Rothwell  */
1883dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1884dc1c1ca3SStephen Rothwell {
1885dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1886dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1887dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1888dc1c1ca3SStephen Rothwell }
1889dc1c1ca3SStephen Rothwell 
18901e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
189114cf11afSPaul Mackerras /*
189214cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
189314cf11afSPaul Mackerras  * spins until a reboot occurs
189414cf11afSPaul Mackerras  */
189514cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
189614cf11afSPaul Mackerras {
189714cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
189814cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
189914cf11afSPaul Mackerras 	return;
190014cf11afSPaul Mackerras }
190114cf11afSPaul Mackerras 
190214cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
190314cf11afSPaul Mackerras {
190414cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
190514cf11afSPaul Mackerras 	WatchdogHandler(regs);
190614cf11afSPaul Mackerras }
190714cf11afSPaul Mackerras #endif
1908dc1c1ca3SStephen Rothwell 
1909dc1c1ca3SStephen Rothwell /*
1910dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1911dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1912dc1c1ca3SStephen Rothwell  */
1913dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1914dc1c1ca3SStephen Rothwell {
1915dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1916dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1917dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1918dc1c1ca3SStephen Rothwell }
191914cf11afSPaul Mackerras 
192014cf11afSPaul Mackerras void __init trap_init(void)
192114cf11afSPaul Mackerras {
192214cf11afSPaul Mackerras }
192380947e7cSGeert Uytterhoeven 
192480947e7cSGeert Uytterhoeven 
192580947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
192680947e7cSGeert Uytterhoeven 
192780947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
192880947e7cSGeert Uytterhoeven 
192980947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
193080947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
193180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
193280947e7cSGeert Uytterhoeven #endif
193380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
193480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
193580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
193680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
193780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
193880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
193980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
194080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
194180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
194280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1943a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
194480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
194580947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
194680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
194780947e7cSGeert Uytterhoeven #endif
194880947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
194980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
195080947e7cSGeert Uytterhoeven #endif
1951efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1952efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1953efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1954f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
1955efcac658SAlexey Kardashevskiy #endif
195680947e7cSGeert Uytterhoeven };
195780947e7cSGeert Uytterhoeven 
195880947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
195980947e7cSGeert Uytterhoeven 
196080947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
196180947e7cSGeert Uytterhoeven {
196276462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
196380947e7cSGeert Uytterhoeven 			    type);
196480947e7cSGeert Uytterhoeven }
196580947e7cSGeert Uytterhoeven 
196680947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
196780947e7cSGeert Uytterhoeven {
196880947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
196980947e7cSGeert Uytterhoeven 	unsigned int i;
197080947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
197180947e7cSGeert Uytterhoeven 
197280947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
197380947e7cSGeert Uytterhoeven 		return -ENODEV;
197480947e7cSGeert Uytterhoeven 
197580947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
197680947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
197780947e7cSGeert Uytterhoeven 	if (!dir)
197880947e7cSGeert Uytterhoeven 		return -ENOMEM;
197980947e7cSGeert Uytterhoeven 
198080947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
198180947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
198280947e7cSGeert Uytterhoeven 	if (!d)
198380947e7cSGeert Uytterhoeven 		goto fail;
198480947e7cSGeert Uytterhoeven 
198580947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
198680947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
198780947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
198880947e7cSGeert Uytterhoeven 		if (!d)
198980947e7cSGeert Uytterhoeven 			goto fail;
199080947e7cSGeert Uytterhoeven 	}
199180947e7cSGeert Uytterhoeven 
199280947e7cSGeert Uytterhoeven 	return 0;
199380947e7cSGeert Uytterhoeven 
199480947e7cSGeert Uytterhoeven fail:
199580947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
199680947e7cSGeert Uytterhoeven 	return -ENOMEM;
199780947e7cSGeert Uytterhoeven }
199880947e7cSGeert Uytterhoeven 
199980947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
200080947e7cSGeert Uytterhoeven 
200180947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2002