xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision ee4ed6fa5af0009a1205498d1f754fecc7af7657)
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>
2814cf11afSPaul Mackerras #include <linux/module.h>
298dad3f92SPaul Mackerras #include <linux/prctl.h>
3014cf11afSPaul Mackerras #include <linux/delay.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
32cc532915SMichael Ellerman #include <linux/kexec.h>
335474c120SMichael Hanselmann #include <linux/backlight.h>
3473c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
351eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3680947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3776462232SChristian Dietrich #include <linux/ratelimit.h>
38ba12eedeSLi Zhong #include <linux/context_tracking.h>
3914cf11afSPaul Mackerras 
4080947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4114cf11afSPaul Mackerras #include <asm/pgtable.h>
4214cf11afSPaul Mackerras #include <asm/uaccess.h>
4314cf11afSPaul Mackerras #include <asm/io.h>
4486417780SPaul Mackerras #include <asm/machdep.h>
4586417780SPaul Mackerras #include <asm/rtas.h>
46f7f6f4feSDavid Gibson #include <asm/pmc.h>
4714cf11afSPaul Mackerras #include <asm/reg.h>
4814cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
4914cf11afSPaul Mackerras #include <asm/backlight.h>
5014cf11afSPaul Mackerras #endif
51dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5286417780SPaul Mackerras #include <asm/firmware.h>
53dc1c1ca3SStephen Rothwell #include <asm/processor.h>
546ce6c629SMichael Neuling #include <asm/tm.h>
55dc1c1ca3SStephen Rothwell #endif
56c0ce7d08SDavid Wilder #include <asm/kexec.h>
5716c57b36SKumar Gala #include <asm/ppc-opcode.h>
58cce1f106SShaohui Xie #include <asm/rio.h>
59ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
60ae3a197eSDavid Howells #include <asm/switch_to.h>
61f54db641SMichael Neuling #include <asm/tm.h>
62ae3a197eSDavid Howells #include <asm/debug.h>
634e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
64dc1c1ca3SStephen Rothwell 
657dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
665be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
695be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
705be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
719422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
725be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7314cf11afSPaul Mackerras 
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
799422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
8014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8114cf11afSPaul Mackerras #endif
8214cf11afSPaul Mackerras 
838b3c34cfSMichael Neuling /* Transactional Memory trap debug */
848b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
858b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
868b3c34cfSMichael Neuling #else
878b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
888b3c34cfSMichael Neuling #endif
898b3c34cfSMichael Neuling 
9014cf11afSPaul Mackerras /*
9114cf11afSPaul Mackerras  * Trap & Exception support
9214cf11afSPaul Mackerras  */
9314cf11afSPaul Mackerras 
946031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
956031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
966031d9d9Santon@samba.org {
976031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
986031d9d9Santon@samba.org 	if (pmac_backlight) {
996031d9d9Santon@samba.org 		struct backlight_properties *props;
1006031d9d9Santon@samba.org 
1016031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1026031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1036031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1046031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1056031d9d9Santon@samba.org 	}
1066031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1076031d9d9Santon@samba.org }
1086031d9d9Santon@samba.org #else
1096031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1106031d9d9Santon@samba.org #endif
1116031d9d9Santon@samba.org 
112760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
113760ca4dcSAnton Blanchard static int die_owner = -1;
114760ca4dcSAnton Blanchard static unsigned int die_nest_count;
115c0ce7d08SDavid Wilder static int die_counter;
116760ca4dcSAnton Blanchard 
117760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
118760ca4dcSAnton Blanchard {
119760ca4dcSAnton Blanchard 	int cpu;
12034c2a14fSanton@samba.org 	unsigned long flags;
12114cf11afSPaul Mackerras 
12214cf11afSPaul Mackerras 	if (debugger(regs))
12314cf11afSPaul Mackerras 		return 1;
12414cf11afSPaul Mackerras 
125293e4688Santon@samba.org 	oops_enter();
126293e4688Santon@samba.org 
127760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
128760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
129760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
130760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
131760ca4dcSAnton Blanchard 		if (cpu == die_owner)
132760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
133760ca4dcSAnton Blanchard 		else
134760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
135760ca4dcSAnton Blanchard 	}
136760ca4dcSAnton Blanchard 	die_nest_count++;
137760ca4dcSAnton Blanchard 	die_owner = cpu;
13814cf11afSPaul Mackerras 	console_verbose();
13914cf11afSPaul Mackerras 	bust_spinlocks(1);
1406031d9d9Santon@samba.org 	if (machine_is(powermac))
1416031d9d9Santon@samba.org 		pmac_backlight_unblank();
142760ca4dcSAnton Blanchard 	return flags;
14334c2a14fSanton@samba.org }
1445474c120SMichael Hanselmann 
145760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
146760ca4dcSAnton Blanchard 			       int signr)
147760ca4dcSAnton Blanchard {
14814cf11afSPaul Mackerras 	bust_spinlocks(0);
149760ca4dcSAnton Blanchard 	die_owner = -1;
150373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
151760ca4dcSAnton Blanchard 	die_nest_count--;
15258154c8cSAnton Blanchard 	oops_exit();
15358154c8cSAnton Blanchard 	printk("\n");
154760ca4dcSAnton Blanchard 	if (!die_nest_count)
155760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
156760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
157760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
158cc532915SMichael Ellerman 
159ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
160ebaeb5aeSMahesh Salgaonkar 
1619b00ac06SAnton Blanchard 	/*
1629b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1639b00ac06SAnton Blanchard 	 * it through the crashdump code.
1649b00ac06SAnton Blanchard 	 */
1659b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
166cc532915SMichael Ellerman 		crash_kexec(regs);
1679b00ac06SAnton Blanchard 
1689b00ac06SAnton Blanchard 		/*
1699b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1709b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1719b00ac06SAnton Blanchard 		 * code.
1729b00ac06SAnton Blanchard 		 */
173c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1749b00ac06SAnton Blanchard 	}
17514cf11afSPaul Mackerras 
176760ca4dcSAnton Blanchard 	if (!signr)
177760ca4dcSAnton Blanchard 		return;
178760ca4dcSAnton Blanchard 
17958154c8cSAnton Blanchard 	/*
18058154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
18158154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18258154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18358154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18458154c8cSAnton Blanchard 	 */
18558154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
18658154c8cSAnton Blanchard 	    is_global_init(current)) {
18758154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
18858154c8cSAnton Blanchard 	}
18958154c8cSAnton Blanchard 
19014cf11afSPaul Mackerras 	if (in_interrupt())
19114cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
192cea6a4baSHorms 	if (panic_on_oops)
193012c437dSHorms 		panic("Fatal exception");
194760ca4dcSAnton Blanchard 	do_exit(signr);
195760ca4dcSAnton Blanchard }
196cea6a4baSHorms 
197760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
198760ca4dcSAnton Blanchard {
199760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
200760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
201760ca4dcSAnton Blanchard 	printk("PREEMPT ");
202760ca4dcSAnton Blanchard #endif
203760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
204760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
205760ca4dcSAnton Blanchard #endif
206760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
207760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
208760ca4dcSAnton Blanchard #endif
209760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
210760ca4dcSAnton Blanchard 	printk("NUMA ");
211760ca4dcSAnton Blanchard #endif
212760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
213760ca4dcSAnton Blanchard 
214760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
215760ca4dcSAnton Blanchard 		return 1;
216760ca4dcSAnton Blanchard 
217760ca4dcSAnton Blanchard 	print_modules();
218760ca4dcSAnton Blanchard 	show_regs(regs);
21914cf11afSPaul Mackerras 
22014cf11afSPaul Mackerras 	return 0;
22114cf11afSPaul Mackerras }
22214cf11afSPaul Mackerras 
223760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
224760ca4dcSAnton Blanchard {
225760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
226760ca4dcSAnton Blanchard 
227760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
228760ca4dcSAnton Blanchard 		err = 0;
229760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
230760ca4dcSAnton Blanchard }
231760ca4dcSAnton Blanchard 
23225baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23325baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23425baa35bSOleg Nesterov {
23525baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
23625baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
23725baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
23825baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
23925baa35bSOleg Nesterov }
24025baa35bSOleg Nesterov 
24114cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24214cf11afSPaul Mackerras {
24314cf11afSPaul Mackerras 	siginfo_t info;
244d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
245d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
246d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
247d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
24814cf11afSPaul Mackerras 
24914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
250760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
25114cf11afSPaul Mackerras 		return;
252760ca4dcSAnton Blanchard 	}
253760ca4dcSAnton Blanchard 
254760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
25576462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
256d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
257d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
25814cf11afSPaul Mackerras 	}
25914cf11afSPaul Mackerras 
260a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2619f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2629f2f79e3SBenjamin Herrenschmidt 
26341ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26414cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
26514cf11afSPaul Mackerras 	info.si_signo = signr;
26614cf11afSPaul Mackerras 	info.si_code = code;
26714cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
26814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
26914cf11afSPaul Mackerras }
27014cf11afSPaul Mackerras 
27114cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27214cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27314cf11afSPaul Mackerras {
27414cf11afSPaul Mackerras 	/* See if any machine dependent calls */
275c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
276c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
277c902be71SArnd Bergmann 			return;
278c902be71SArnd Bergmann 	}
27914cf11afSPaul Mackerras 
2808dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
28114cf11afSPaul Mackerras 
28214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28414cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
28514cf11afSPaul Mackerras 
28614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
28714cf11afSPaul Mackerras }
2881e9b4507SMahesh Salgaonkar 
2891e9b4507SMahesh Salgaonkar /*
2901e9b4507SMahesh Salgaonkar  * This function is called in real mode. Strictly no printk's please.
2911e9b4507SMahesh Salgaonkar  *
2921e9b4507SMahesh Salgaonkar  * regs->nip and regs->msr contains srr0 and ssr1.
2931e9b4507SMahesh Salgaonkar  */
2941e9b4507SMahesh Salgaonkar long machine_check_early(struct pt_regs *regs)
2951e9b4507SMahesh Salgaonkar {
2964c703416SMahesh Salgaonkar 	long handled = 0;
2974c703416SMahesh Salgaonkar 
2984c703416SMahesh Salgaonkar 	if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
2994c703416SMahesh Salgaonkar 		handled = cur_cpu_spec->machine_check_early(regs);
3004c703416SMahesh Salgaonkar 	return handled;
3011e9b4507SMahesh Salgaonkar }
3021e9b4507SMahesh Salgaonkar 
30314cf11afSPaul Mackerras #endif
30414cf11afSPaul Mackerras 
30514cf11afSPaul Mackerras /*
30614cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
30714cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
30814cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
30914cf11afSPaul Mackerras  * table.
31014cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
31114cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
31214cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
31314cf11afSPaul Mackerras  *  -- paulus.
31414cf11afSPaul Mackerras  */
31514cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
31614cf11afSPaul Mackerras {
31768a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
31814cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
31914cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
32014cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
32114cf11afSPaul Mackerras 
32214cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
32314cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
32414cf11afSPaul Mackerras 		/*
32514cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
32614cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
32714cf11afSPaul Mackerras 		 * As the address is in the exception table
32814cf11afSPaul Mackerras 		 * we should be able to read the instr there.
32914cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
33014cf11afSPaul Mackerras 		 * load or store.
33114cf11afSPaul Mackerras 		 */
33214cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
33314cf11afSPaul Mackerras 			nip -= 2;
33414cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
33514cf11afSPaul Mackerras 			--nip;
33614cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
33714cf11afSPaul Mackerras 			/* sync or twi */
33814cf11afSPaul Mackerras 			unsigned int rb;
33914cf11afSPaul Mackerras 
34014cf11afSPaul Mackerras 			--nip;
34114cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
34214cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
34314cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
34414cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
34514cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
34614cf11afSPaul Mackerras 			regs->nip = entry->fixup;
34714cf11afSPaul Mackerras 			return 1;
34814cf11afSPaul Mackerras 		}
34914cf11afSPaul Mackerras 	}
35068a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
35114cf11afSPaul Mackerras 	return 0;
35214cf11afSPaul Mackerras }
35314cf11afSPaul Mackerras 
354172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
35514cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
35614cf11afSPaul Mackerras    is in the ESR. */
35714cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
35814cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
35914cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
36014cf11afSPaul Mackerras #else
361fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
36214cf11afSPaul Mackerras #endif
36314cf11afSPaul Mackerras #define REASON_FP		ESR_FP
36414cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
36514cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
36614cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
36714cf11afSPaul Mackerras 
36814cf11afSPaul Mackerras /* single-step stuff */
36951ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
37051ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
37114cf11afSPaul Mackerras 
37214cf11afSPaul Mackerras #else
37314cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
37414cf11afSPaul Mackerras    exception is in the MSR. */
37514cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
37614cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3778b3c34cfSMichael Neuling #define REASON_TM		0x200000
37814cf11afSPaul Mackerras #define REASON_FP		0x100000
37914cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
38014cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
38114cf11afSPaul Mackerras #define REASON_TRAP		0x20000
38214cf11afSPaul Mackerras 
38314cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
38414cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
38514cf11afSPaul Mackerras #endif
38614cf11afSPaul Mackerras 
38747c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
38847c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
38914cf11afSPaul Mackerras {
3901a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
39114cf11afSPaul Mackerras 
39214cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
39314cf11afSPaul Mackerras 		printk("Instruction");
39414cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
39514cf11afSPaul Mackerras 	} else
39614cf11afSPaul Mackerras 		printk("Data");
39714cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
39847c0bd1aSBenjamin Herrenschmidt 
39947c0bd1aSBenjamin Herrenschmidt 	return 0;
40047c0bd1aSBenjamin Herrenschmidt }
40147c0bd1aSBenjamin Herrenschmidt 
40247c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
40347c0bd1aSBenjamin Herrenschmidt {
40447c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
40547c0bd1aSBenjamin Herrenschmidt 
40614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
40714cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
40814cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
40914cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
41014cf11afSPaul Mackerras 	}
41114cf11afSPaul Mackerras 	else {
41214cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
41314cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
41414cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
41514cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
41614cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
41714cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
41814cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
41914cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
42014cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
42114cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
42214cf11afSPaul Mackerras 			flush_instruction_cache();
42314cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
42414cf11afSPaul Mackerras 		}
42514cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
42614cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
42714cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
42814cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
42914cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
43014cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
43114cf11afSPaul Mackerras 
43214cf11afSPaul Mackerras 		/* Clear MCSR */
43314cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
43414cf11afSPaul Mackerras 	}
43547c0bd1aSBenjamin Herrenschmidt 	return 0;
43647c0bd1aSBenjamin Herrenschmidt }
437fc5e7097SDave Kleikamp 
438fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
439fc5e7097SDave Kleikamp {
440fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
441fc5e7097SDave Kleikamp 	u32 mcsr;
442fc5e7097SDave Kleikamp 
443fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
444fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
445fc5e7097SDave Kleikamp 		printk(KERN_ERR
446fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
447fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
448fc5e7097SDave Kleikamp 		return 0;
449fc5e7097SDave Kleikamp 	}
450fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
451fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
452fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
453fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
454fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
455fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
456fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
457fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
458fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
459fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
460fc5e7097SDave Kleikamp 		flush_instruction_cache();
461fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
462fc5e7097SDave Kleikamp 	}
463fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
464fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
465fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
466fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
467fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
468fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
469fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
470fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
471fc5e7097SDave Kleikamp 
472fc5e7097SDave Kleikamp 	/* Clear MCSR */
473fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
474fc5e7097SDave Kleikamp 
475fc5e7097SDave Kleikamp 	return 0;
476fc5e7097SDave Kleikamp }
47714cf11afSPaul Mackerras #elif defined(CONFIG_E500)
478fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
479fe04b112SScott Wood {
480fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
481fe04b112SScott Wood 	unsigned long reason = mcsr;
482fe04b112SScott Wood 	int recoverable = 1;
483fe04b112SScott Wood 
48482a9a480SScott Wood 	if (reason & MCSR_LD) {
485cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
486cce1f106SShaohui Xie 		if (recoverable == 1)
487cce1f106SShaohui Xie 			goto silent_out;
488cce1f106SShaohui Xie 	}
489cce1f106SShaohui Xie 
490fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
491fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
492fe04b112SScott Wood 
493fe04b112SScott Wood 	if (reason & MCSR_MCP)
494fe04b112SScott Wood 		printk("Machine Check Signal\n");
495fe04b112SScott Wood 
496fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
497fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
498fe04b112SScott Wood 
499fe04b112SScott Wood 		/*
500fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
501fe04b112SScott Wood 		 */
502fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
503fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
504fe04b112SScott Wood 			;
505fe04b112SScott Wood 
506fe04b112SScott Wood 		/*
507fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
508fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
509fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
510fe04b112SScott Wood 		 */
511fe04b112SScott Wood 		reason &= ~MCSR_IF;
512fe04b112SScott Wood 	}
513fe04b112SScott Wood 
514fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
515fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
51637caf9f2SKumar Gala 
51737caf9f2SKumar Gala 		/*
51837caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
51937caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
52037caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
52137caf9f2SKumar Gala 		 */
52237caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
523fe04b112SScott Wood 			recoverable = 0;
524fe04b112SScott Wood 	}
525fe04b112SScott Wood 
526fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
527fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
528fe04b112SScott Wood 		recoverable = 0;
529fe04b112SScott Wood 	}
530fe04b112SScott Wood 
531fe04b112SScott Wood 	if (reason & MCSR_NMI)
532fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
533fe04b112SScott Wood 
534fe04b112SScott Wood 	if (reason & MCSR_IF) {
535fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
536fe04b112SScott Wood 		recoverable = 0;
537fe04b112SScott Wood 	}
538fe04b112SScott Wood 
539fe04b112SScott Wood 	if (reason & MCSR_LD) {
540fe04b112SScott Wood 		printk("Load Error Report\n");
541fe04b112SScott Wood 		recoverable = 0;
542fe04b112SScott Wood 	}
543fe04b112SScott Wood 
544fe04b112SScott Wood 	if (reason & MCSR_ST) {
545fe04b112SScott Wood 		printk("Store Error Report\n");
546fe04b112SScott Wood 		recoverable = 0;
547fe04b112SScott Wood 	}
548fe04b112SScott Wood 
549fe04b112SScott Wood 	if (reason & MCSR_LDG) {
550fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
551fe04b112SScott Wood 		recoverable = 0;
552fe04b112SScott Wood 	}
553fe04b112SScott Wood 
554fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
555fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
556fe04b112SScott Wood 
557fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
558fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
559fe04b112SScott Wood 		recoverable = 0;
560fe04b112SScott Wood 	}
561fe04b112SScott Wood 
562fe04b112SScott Wood 	if (reason & MCSR_MAV) {
563fe04b112SScott Wood 		u64 addr;
564fe04b112SScott Wood 
565fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
566fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
567fe04b112SScott Wood 
568fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
569fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
570fe04b112SScott Wood 	}
571fe04b112SScott Wood 
572cce1f106SShaohui Xie silent_out:
573fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
574fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
575fe04b112SScott Wood }
576fe04b112SScott Wood 
57747c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
57847c0bd1aSBenjamin Herrenschmidt {
57947c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
58047c0bd1aSBenjamin Herrenschmidt 
581cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
582cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
583cce1f106SShaohui Xie 			return 1;
5844e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
5854e0e3435SHongtao Jia 			return 1;
586cce1f106SShaohui Xie 	}
587cce1f106SShaohui Xie 
58814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
58914cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
59014cf11afSPaul Mackerras 
59114cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
59214cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
59314cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
59414cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
59514cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
59614cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
59714cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
59814cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
59914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
60014cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
60114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
60214cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
60314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
60414cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
60514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
60614cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
60714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
60814cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
60914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
61014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
61114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
61214cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
61314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
61414cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
61547c0bd1aSBenjamin Herrenschmidt 
61647c0bd1aSBenjamin Herrenschmidt 	return 0;
61747c0bd1aSBenjamin Herrenschmidt }
6184490c06bSKumar Gala 
6194490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6204490c06bSKumar Gala {
6214490c06bSKumar Gala 	return 0;
6224490c06bSKumar Gala }
62314cf11afSPaul Mackerras #elif defined(CONFIG_E200)
62447c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
62547c0bd1aSBenjamin Herrenschmidt {
62647c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
62747c0bd1aSBenjamin Herrenschmidt 
62814cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62914cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
63014cf11afSPaul Mackerras 
63114cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
63214cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
63314cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
63414cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
63514cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
63614cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
63714cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
63814cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
63914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
64014cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
64114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
64214cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
64314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
64414cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
64547c0bd1aSBenjamin Herrenschmidt 
64647c0bd1aSBenjamin Herrenschmidt 	return 0;
64747c0bd1aSBenjamin Herrenschmidt }
64847c0bd1aSBenjamin Herrenschmidt #else
64947c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
65047c0bd1aSBenjamin Herrenschmidt {
65147c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
65247c0bd1aSBenjamin Herrenschmidt 
65314cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
65414cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
65514cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
65614cf11afSPaul Mackerras 	case 0x80000:
65714cf11afSPaul Mackerras 		printk("Machine check signal\n");
65814cf11afSPaul Mackerras 		break;
65914cf11afSPaul Mackerras 	case 0:		/* for 601 */
66014cf11afSPaul Mackerras 	case 0x40000:
66114cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
66214cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
66314cf11afSPaul Mackerras 		break;
66414cf11afSPaul Mackerras 	case 0x20000:
66514cf11afSPaul Mackerras 		printk("Data parity error signal\n");
66614cf11afSPaul Mackerras 		break;
66714cf11afSPaul Mackerras 	case 0x10000:
66814cf11afSPaul Mackerras 		printk("Address parity error signal\n");
66914cf11afSPaul Mackerras 		break;
67014cf11afSPaul Mackerras 	case 0x20000000:
67114cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
67214cf11afSPaul Mackerras 		break;
67314cf11afSPaul Mackerras 	case 0x40000000:
67414cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
67514cf11afSPaul Mackerras 		break;
67614cf11afSPaul Mackerras 	case 0x00100000:
67714cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
67814cf11afSPaul Mackerras 		break;
67914cf11afSPaul Mackerras 	default:
68014cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
68114cf11afSPaul Mackerras 	}
68275918a4bSOlof Johansson 	return 0;
68375918a4bSOlof Johansson }
68447c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
68575918a4bSOlof Johansson 
68675918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
68775918a4bSOlof Johansson {
688ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
68975918a4bSOlof Johansson 	int recover = 0;
69075918a4bSOlof Johansson 
69189713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
69289713ed1SAnton Blanchard 
69347c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
69447c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
69547c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
69647c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
69747c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
69847c0bd1aSBenjamin Herrenschmidt 	 */
69975918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
70075918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
70147c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
70247c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
70375918a4bSOlof Johansson 
70447c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
705ba12eedeSLi Zhong 		goto bail;
70675918a4bSOlof Johansson 
70775918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
70847c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
70947c0bd1aSBenjamin Herrenschmidt 	 *
71047c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
71147c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
71247c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
71347c0bd1aSBenjamin Herrenschmidt 	 */
71475918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
715ba12eedeSLi Zhong 	goto bail;
71675918a4bSOlof Johansson #endif
71775918a4bSOlof Johansson 
718a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
719ba12eedeSLi Zhong 		goto bail;
72075918a4bSOlof Johansson 
72175918a4bSOlof Johansson 	if (check_io_access(regs))
722ba12eedeSLi Zhong 		goto bail;
72375918a4bSOlof Johansson 
7248dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
72514cf11afSPaul Mackerras 
72614cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
72714cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
72814cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
729ba12eedeSLi Zhong 
730ba12eedeSLi Zhong bail:
731ba12eedeSLi Zhong 	exception_exit(prev_state);
73214cf11afSPaul Mackerras }
73314cf11afSPaul Mackerras 
73414cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
73514cf11afSPaul Mackerras {
73614cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
73714cf11afSPaul Mackerras }
73814cf11afSPaul Mackerras 
739dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
74014cf11afSPaul Mackerras {
741ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
742ba12eedeSLi Zhong 
74314cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
74414cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
74514cf11afSPaul Mackerras 
74614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
747ba12eedeSLi Zhong 
748ba12eedeSLi Zhong 	exception_exit(prev_state);
74914cf11afSPaul Mackerras }
75014cf11afSPaul Mackerras 
751dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
75214cf11afSPaul Mackerras {
753ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
754ba12eedeSLi Zhong 
75514cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
75614cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
757ba12eedeSLi Zhong 		goto bail;
75814cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
759ba12eedeSLi Zhong 		goto bail;
76014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
761ba12eedeSLi Zhong 
762ba12eedeSLi Zhong bail:
763ba12eedeSLi Zhong 	exception_exit(prev_state);
76414cf11afSPaul Mackerras }
76514cf11afSPaul Mackerras 
76614cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
76714cf11afSPaul Mackerras {
76814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
76914cf11afSPaul Mackerras }
77014cf11afSPaul Mackerras 
7718dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
77214cf11afSPaul Mackerras {
773ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
774ba12eedeSLi Zhong 
7752538c2d0SK.Prasad 	clear_single_step(regs);
77614cf11afSPaul Mackerras 
77714cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
77814cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
779ba12eedeSLi Zhong 		goto bail;
78014cf11afSPaul Mackerras 	if (debugger_sstep(regs))
781ba12eedeSLi Zhong 		goto bail;
78214cf11afSPaul Mackerras 
78314cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
784ba12eedeSLi Zhong 
785ba12eedeSLi Zhong bail:
786ba12eedeSLi Zhong 	exception_exit(prev_state);
78714cf11afSPaul Mackerras }
78814cf11afSPaul Mackerras 
78914cf11afSPaul Mackerras /*
79014cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
79114cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
79214cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
79314cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
79414cf11afSPaul Mackerras  */
7958dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
79614cf11afSPaul Mackerras {
7972538c2d0SK.Prasad 	if (single_stepping(regs))
7982538c2d0SK.Prasad 		single_step_exception(regs);
79914cf11afSPaul Mackerras }
80014cf11afSPaul Mackerras 
8015fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
802dc1c1ca3SStephen Rothwell {
8035fad293bSKumar Gala 	int ret = 0;
804dc1c1ca3SStephen Rothwell 
805dc1c1ca3SStephen Rothwell 	/* Invalid operation */
806dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
8075fad293bSKumar Gala 		ret = FPE_FLTINV;
808dc1c1ca3SStephen Rothwell 
809dc1c1ca3SStephen Rothwell 	/* Overflow */
810dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
8115fad293bSKumar Gala 		ret = FPE_FLTOVF;
812dc1c1ca3SStephen Rothwell 
813dc1c1ca3SStephen Rothwell 	/* Underflow */
814dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
8155fad293bSKumar Gala 		ret = FPE_FLTUND;
816dc1c1ca3SStephen Rothwell 
817dc1c1ca3SStephen Rothwell 	/* Divide by zero */
818dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
8195fad293bSKumar Gala 		ret = FPE_FLTDIV;
820dc1c1ca3SStephen Rothwell 
821dc1c1ca3SStephen Rothwell 	/* Inexact result */
822dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
8235fad293bSKumar Gala 		ret = FPE_FLTRES;
8245fad293bSKumar Gala 
8255fad293bSKumar Gala 	return ret;
8265fad293bSKumar Gala }
8275fad293bSKumar Gala 
8285fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
8295fad293bSKumar Gala {
8305fad293bSKumar Gala 	int code = 0;
8315fad293bSKumar Gala 
8325fad293bSKumar Gala 	flush_fp_to_thread(current);
8335fad293bSKumar Gala 
834de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
835dc1c1ca3SStephen Rothwell 
836dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
837dc1c1ca3SStephen Rothwell }
838dc1c1ca3SStephen Rothwell 
839dc1c1ca3SStephen Rothwell /*
840dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
84114cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
84214cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
84314cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
84414cf11afSPaul Mackerras  *
84514cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
84614cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
84714cf11afSPaul Mackerras  * bits is faster and easier.
84886417780SPaul Mackerras  *
84914cf11afSPaul Mackerras  */
85014cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
85114cf11afSPaul Mackerras {
85214cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
85314cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
85414cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
85514cf11afSPaul Mackerras 	u32 num_bytes;
85614cf11afSPaul Mackerras 	unsigned long EA;
85714cf11afSPaul Mackerras 	int pos = 0;
85814cf11afSPaul Mackerras 
85914cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
86016c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
86114cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
86214cf11afSPaul Mackerras 			return -EINVAL;
86314cf11afSPaul Mackerras 
86414cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
86514cf11afSPaul Mackerras 
86616c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
86716c57b36SKumar Gala 		case PPC_INST_LSWX:
86816c57b36SKumar Gala 		case PPC_INST_STSWX:
86914cf11afSPaul Mackerras 			EA += NB_RB;
87014cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
87114cf11afSPaul Mackerras 			break;
87216c57b36SKumar Gala 		case PPC_INST_LSWI:
87316c57b36SKumar Gala 		case PPC_INST_STSWI:
87414cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
87514cf11afSPaul Mackerras 			break;
87614cf11afSPaul Mackerras 		default:
87714cf11afSPaul Mackerras 			return -EINVAL;
87814cf11afSPaul Mackerras 	}
87914cf11afSPaul Mackerras 
88014cf11afSPaul Mackerras 	while (num_bytes != 0)
88114cf11afSPaul Mackerras 	{
88214cf11afSPaul Mackerras 		u8 val;
88314cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
88414cf11afSPaul Mackerras 
88580aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
88680aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
88780aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
88880aa0fb4SJames Yang 
88916c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
89016c57b36SKumar Gala 			case PPC_INST_LSWX:
89116c57b36SKumar Gala 			case PPC_INST_LSWI:
89214cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
89314cf11afSPaul Mackerras 					return -EFAULT;
89414cf11afSPaul Mackerras 				/* first time updating this reg,
89514cf11afSPaul Mackerras 				 * zero it out */
89614cf11afSPaul Mackerras 				if (pos == 0)
89714cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
89814cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
89914cf11afSPaul Mackerras 				break;
90016c57b36SKumar Gala 			case PPC_INST_STSWI:
90116c57b36SKumar Gala 			case PPC_INST_STSWX:
90214cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
90314cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
90414cf11afSPaul Mackerras 					return -EFAULT;
90514cf11afSPaul Mackerras 				break;
90614cf11afSPaul Mackerras 		}
90714cf11afSPaul Mackerras 		/* move EA to next address */
90814cf11afSPaul Mackerras 		EA += 1;
90914cf11afSPaul Mackerras 		num_bytes--;
91014cf11afSPaul Mackerras 
91114cf11afSPaul Mackerras 		/* manage our position within the register */
91214cf11afSPaul Mackerras 		if (++pos == 4) {
91314cf11afSPaul Mackerras 			pos = 0;
91414cf11afSPaul Mackerras 			if (++rT == 32)
91514cf11afSPaul Mackerras 				rT = 0;
91614cf11afSPaul Mackerras 		}
91714cf11afSPaul Mackerras 	}
91814cf11afSPaul Mackerras 
91914cf11afSPaul Mackerras 	return 0;
92014cf11afSPaul Mackerras }
92114cf11afSPaul Mackerras 
922c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
923c3412dcbSWill Schmidt {
924c3412dcbSWill Schmidt 	u32 ra,rs;
925c3412dcbSWill Schmidt 	unsigned long tmp;
926c3412dcbSWill Schmidt 
927c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
928c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
929c3412dcbSWill Schmidt 
930c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
931c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
932c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
933c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
934c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
935c3412dcbSWill Schmidt 
936c3412dcbSWill Schmidt 	return 0;
937c3412dcbSWill Schmidt }
938c3412dcbSWill Schmidt 
939c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
940c1469f13SKumar Gala {
941c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
942c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
943c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
944c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
945c1469f13SKumar Gala 	u8 bit;
946c1469f13SKumar Gala 	unsigned long tmp;
947c1469f13SKumar Gala 
948c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
949c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
950c1469f13SKumar Gala 
951c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
952c1469f13SKumar Gala 
953c1469f13SKumar Gala 	return 0;
954c1469f13SKumar Gala }
955c1469f13SKumar Gala 
9566ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
9576ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
9586ce6c629SMichael Neuling {
9596ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
9606ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
9616ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
9626ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
9636ce6c629SMichael Neuling 	 */
9646ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
9656ce6c629SMichael Neuling 		tm_enable();
9666ce6c629SMichael Neuling 		tm_abort(cause);
9676ce6c629SMichael Neuling 		return true;
9686ce6c629SMichael Neuling 	}
9696ce6c629SMichael Neuling 	return false;
9706ce6c629SMichael Neuling }
9716ce6c629SMichael Neuling #else
9726ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
9736ce6c629SMichael Neuling {
9746ce6c629SMichael Neuling 	return false;
9756ce6c629SMichael Neuling }
9766ce6c629SMichael Neuling #endif
9776ce6c629SMichael Neuling 
97814cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
97914cf11afSPaul Mackerras {
98014cf11afSPaul Mackerras 	u32 instword;
98114cf11afSPaul Mackerras 	u32 rd;
98214cf11afSPaul Mackerras 
9834288e343SAnton Blanchard 	if (!user_mode(regs))
98414cf11afSPaul Mackerras 		return -EINVAL;
98514cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
98614cf11afSPaul Mackerras 
98714cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
98814cf11afSPaul Mackerras 		return -EFAULT;
98914cf11afSPaul Mackerras 
99014cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
99116c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
992eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
99314cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
99414cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
99514cf11afSPaul Mackerras 		return 0;
99614cf11afSPaul Mackerras 	}
99714cf11afSPaul Mackerras 
99814cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
99980947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1000eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
100114cf11afSPaul Mackerras 		return 0;
100280947e7cSGeert Uytterhoeven 	}
100314cf11afSPaul Mackerras 
100414cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
100516c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
100686417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
100714cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
100814cf11afSPaul Mackerras 
1009eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
101014cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
101114cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
101214cf11afSPaul Mackerras 		return 0;
101314cf11afSPaul Mackerras 	}
101414cf11afSPaul Mackerras 
101514cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
101680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
10176ce6c629SMichael Neuling 		if (tm_abort_check(regs,
10186ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
10196ce6c629SMichael Neuling 			return -EINVAL;
1020eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
102114cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
102280947e7cSGeert Uytterhoeven 	}
102314cf11afSPaul Mackerras 
1024c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
102516c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1026eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1027c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1028c3412dcbSWill Schmidt 	}
1029c3412dcbSWill Schmidt 
1030c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
103116c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1032eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1033c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1034c1469f13SKumar Gala 	}
1035c1469f13SKumar Gala 
10369863c28aSJames Yang 	/* Emulate sync instruction variants */
10379863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
10389863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
10399863c28aSJames Yang 		asm volatile("sync");
10409863c28aSJames Yang 		return 0;
10419863c28aSJames Yang 	}
10429863c28aSJames Yang 
1043efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1044efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
104573d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
104673d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
104773d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
104873d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1049efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1050efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1051efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1052efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1053efcac658SAlexey Kardashevskiy 		return 0;
1054efcac658SAlexey Kardashevskiy 	}
1055efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
105673d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
105773d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
105873d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
105973d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1060efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1061efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1062efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
106300ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1064efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
106500ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1066efcac658SAlexey Kardashevskiy 		return 0;
1067efcac658SAlexey Kardashevskiy 	}
1068efcac658SAlexey Kardashevskiy #endif
1069efcac658SAlexey Kardashevskiy 
107014cf11afSPaul Mackerras 	return -EINVAL;
107114cf11afSPaul Mackerras }
107214cf11afSPaul Mackerras 
107373c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
107414cf11afSPaul Mackerras {
107573c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
107614cf11afSPaul Mackerras }
107714cf11afSPaul Mackerras 
10783a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
10793a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
10803a3b5aa6SKevin Hao {
10813a3b5aa6SKevin Hao 	int ret;
10823a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
10833a3b5aa6SKevin Hao 
10843a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
10853a3b5aa6SKevin Hao 	if (ret >= 0)
10863a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
10873a3b5aa6SKevin Hao 
10883a3b5aa6SKevin Hao 	switch (ret) {
10893a3b5aa6SKevin Hao 	case 0:
10903a3b5aa6SKevin Hao 		emulate_single_step(regs);
10913a3b5aa6SKevin Hao 		return 0;
10923a3b5aa6SKevin Hao 	case 1: {
10933a3b5aa6SKevin Hao 			int code = 0;
1094de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
10953a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
10963a3b5aa6SKevin Hao 			return 0;
10973a3b5aa6SKevin Hao 		}
10983a3b5aa6SKevin Hao 	case -EFAULT:
10993a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11003a3b5aa6SKevin Hao 		return 0;
11013a3b5aa6SKevin Hao 	}
11023a3b5aa6SKevin Hao 
11033a3b5aa6SKevin Hao 	return -1;
11043a3b5aa6SKevin Hao }
11053a3b5aa6SKevin Hao #else
11063a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
11073a3b5aa6SKevin Hao #endif
11083a3b5aa6SKevin Hao 
11098dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
111014cf11afSPaul Mackerras {
1111ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
111214cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
111314cf11afSPaul Mackerras 
1114aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
111504903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
111614cf11afSPaul Mackerras 
111714cf11afSPaul Mackerras 	if (reason & REASON_FP) {
111814cf11afSPaul Mackerras 		/* IEEE FP exception */
1119dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1120ba12eedeSLi Zhong 		goto bail;
11218dad3f92SPaul Mackerras 	}
11228dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1123ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1124ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1125ba797b28SJason Wessel 		if (debugger_bpt(regs))
1126ba12eedeSLi Zhong 			goto bail;
1127ba797b28SJason Wessel 
112814cf11afSPaul Mackerras 		/* trap exception */
1129dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1130dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1131ba12eedeSLi Zhong 			goto bail;
113273c9ceabSJeremy Fitzhardinge 
113373c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1134608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
113514cf11afSPaul Mackerras 			regs->nip += 4;
1136ba12eedeSLi Zhong 			goto bail;
113714cf11afSPaul Mackerras 		}
11388dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1139ba12eedeSLi Zhong 		goto bail;
11408dad3f92SPaul Mackerras 	}
1141bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1142bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1143bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1144bc2a9408SMichael Neuling 		 * This occurs when:
1145bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1146bc2a9408SMichael Neuling 		 *    transition in TM states.
1147bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1148bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1149bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1150bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1151bc2a9408SMichael Neuling 		 */
1152bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1153bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1154bc2a9408SMichael Neuling 			regs->nip += 4;
1155ba12eedeSLi Zhong 			goto bail;
1156bc2a9408SMichael Neuling 		}
1157bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1158bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1159bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1160bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1161bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1162bc2a9408SMichael Neuling 		 */
1163bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1164bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1165ba12eedeSLi Zhong 			goto bail;
1166bc2a9408SMichael Neuling 		} else {
1167bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1168bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1169bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1170bc2a9408SMichael Neuling 		}
1171bc2a9408SMichael Neuling 	}
1172bc2a9408SMichael Neuling #endif
11738dad3f92SPaul Mackerras 
1174b3f6a459SMichael Ellerman 	/*
1175b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1176b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1177b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1178b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1179b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1180b3f6a459SMichael Ellerman 	 */
1181b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1182b3f6a459SMichael Ellerman 		goto sigill;
1183b3f6a459SMichael Ellerman 
1184a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1185a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1186cd8a5673SPaul Mackerras 		local_irq_enable();
1187cd8a5673SPaul Mackerras 
118804903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
118904903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
119004903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
119104903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
119204903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
119304903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
11944e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
11954e63f8edSBenjamin Herrenschmidt 	 */
11963a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1197ba12eedeSLi Zhong 		goto bail;
119804903a30SKumar Gala 
11998dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
12008dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
120114cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
120214cf11afSPaul Mackerras 		case 0:
120314cf11afSPaul Mackerras 			regs->nip += 4;
120414cf11afSPaul Mackerras 			emulate_single_step(regs);
1205ba12eedeSLi Zhong 			goto bail;
120614cf11afSPaul Mackerras 		case -EFAULT:
120714cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1208ba12eedeSLi Zhong 			goto bail;
12098dad3f92SPaul Mackerras 		}
12108dad3f92SPaul Mackerras 	}
12118dad3f92SPaul Mackerras 
1212b3f6a459SMichael Ellerman sigill:
121314cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
121414cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
121514cf11afSPaul Mackerras 	else
121614cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1217ba12eedeSLi Zhong 
1218ba12eedeSLi Zhong bail:
1219ba12eedeSLi Zhong 	exception_exit(prev_state);
122014cf11afSPaul Mackerras }
122114cf11afSPaul Mackerras 
1222bf593907SPaul Mackerras /*
1223bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1224bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1225bf593907SPaul Mackerras  */
1226bf593907SPaul Mackerras void __kprobes emulation_assist_interrupt(struct pt_regs *regs)
1227bf593907SPaul Mackerras {
1228bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1229bf593907SPaul Mackerras 	program_check_exception(regs);
1230bf593907SPaul Mackerras }
1231bf593907SPaul Mackerras 
1232dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
123314cf11afSPaul Mackerras {
1234ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
12354393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
123614cf11afSPaul Mackerras 
1237a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1238a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1239a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1240a3512b2dSBenjamin Herrenschmidt 
12416ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
12426ce6c629SMichael Neuling 		goto bail;
12436ce6c629SMichael Neuling 
1244e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1245e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
124614cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
124714cf11afSPaul Mackerras 
124814cf11afSPaul Mackerras 	if (fixed == 1) {
124914cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
125014cf11afSPaul Mackerras 		emulate_single_step(regs);
1251ba12eedeSLi Zhong 		goto bail;
125214cf11afSPaul Mackerras 	}
125314cf11afSPaul Mackerras 
125414cf11afSPaul Mackerras 	/* Operand address was bad */
125514cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
12564393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
12574393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
12584393c4f6SBenjamin Herrenschmidt 	} else {
12594393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
12604393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
126114cf11afSPaul Mackerras 	}
12624393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
12634393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
12644393c4f6SBenjamin Herrenschmidt 	else
12654393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1266ba12eedeSLi Zhong 
1267ba12eedeSLi Zhong bail:
1268ba12eedeSLi Zhong 	exception_exit(prev_state);
126914cf11afSPaul Mackerras }
127014cf11afSPaul Mackerras 
127114cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
127214cf11afSPaul Mackerras {
127314cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
127414cf11afSPaul Mackerras 	       current, regs->gpr[1]);
127514cf11afSPaul Mackerras 	debugger(regs);
127614cf11afSPaul Mackerras 	show_regs(regs);
127714cf11afSPaul Mackerras 	panic("kernel stack overflow");
127814cf11afSPaul Mackerras }
127914cf11afSPaul Mackerras 
128014cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
128114cf11afSPaul Mackerras {
128214cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
128314cf11afSPaul Mackerras 	       regs->nip, regs->msr);
128414cf11afSPaul Mackerras 	debugger(regs);
128514cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
128614cf11afSPaul Mackerras }
128714cf11afSPaul Mackerras 
128814cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
128914cf11afSPaul Mackerras {
129014cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
129119c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
129214cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
129314cf11afSPaul Mackerras }
129414cf11afSPaul Mackerras 
1295dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1296dc1c1ca3SStephen Rothwell {
1297ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1298ba12eedeSLi Zhong 
1299dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1300dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1301dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1302ba12eedeSLi Zhong 
1303ba12eedeSLi Zhong 	exception_exit(prev_state);
1304dc1c1ca3SStephen Rothwell }
1305dc1c1ca3SStephen Rothwell 
1306dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1307dc1c1ca3SStephen Rothwell {
1308ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1309ba12eedeSLi Zhong 
1310dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1311dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1312dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1313dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1314ba12eedeSLi Zhong 		goto bail;
1315dc1c1ca3SStephen Rothwell 	}
13166c4841c2SAnton Blanchard 
1317dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1318dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1319dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1320ba12eedeSLi Zhong 
1321ba12eedeSLi Zhong bail:
1322ba12eedeSLi Zhong 	exception_exit(prev_state);
1323dc1c1ca3SStephen Rothwell }
1324dc1c1ca3SStephen Rothwell 
1325ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1326ce48b210SMichael Neuling {
1327ce48b210SMichael Neuling 	if (user_mode(regs)) {
1328ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1329ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1330ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1331ce48b210SMichael Neuling 		return;
1332ce48b210SMichael Neuling 	}
1333ce48b210SMichael Neuling 
1334ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1335ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1336ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1337ce48b210SMichael Neuling }
1338ce48b210SMichael Neuling 
13392517617eSMichael Neuling #ifdef CONFIG_PPC64
1340021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1341d0c0c9a1SMichael Neuling {
1342021424a1SMichael Ellerman 	static char *facility_strings[] = {
13432517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
13442517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
13452517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
13462517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
13472517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
13482517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
13492517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
13502517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1351021424a1SMichael Ellerman 	};
13522517617eSMichael Neuling 	char *facility = "unknown";
1353021424a1SMichael Ellerman 	u64 value;
13542517617eSMichael Neuling 	u8 status;
13552517617eSMichael Neuling 	bool hv;
1356021424a1SMichael Ellerman 
13572517617eSMichael Neuling 	hv = (regs->trap == 0xf80);
13582517617eSMichael Neuling 	if (hv)
1359b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
13602517617eSMichael Neuling 	else
13612517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
13622517617eSMichael Neuling 
13632517617eSMichael Neuling 	status = value >> 56;
13642517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
13652517617eSMichael Neuling 		/* User is acessing the DSCR.  Set the inherit bit and allow
13662517617eSMichael Neuling 		 * the user to set it directly in future by setting via the
1367bc683a7eSMichael Neuling 		 * FSCR DSCR bit.  We always leave HFSCR DSCR set.
13682517617eSMichael Neuling 		 */
13692517617eSMichael Neuling 		current->thread.dscr_inherit = 1;
13702517617eSMichael Neuling 		mtspr(SPRN_FSCR, value | FSCR_DSCR);
13712517617eSMichael Neuling 		return;
1372b14b6260SMichael Ellerman 	}
1373b14b6260SMichael Ellerman 
13742517617eSMichael Neuling 	if ((status < ARRAY_SIZE(facility_strings)) &&
13752517617eSMichael Neuling 	    facility_strings[status])
13762517617eSMichael Neuling 		facility = facility_strings[status];
1377021424a1SMichael Ellerman 
1378d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1379d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1380d0c0c9a1SMichael Neuling 		local_irq_enable();
1381d0c0c9a1SMichael Neuling 
1382*ee4ed6faSMichael Neuling 	pr_err_ratelimited(
1383*ee4ed6faSMichael Neuling 		"%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
13842517617eSMichael Neuling 		hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
1385d0c0c9a1SMichael Neuling 
1386d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1387d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1388d0c0c9a1SMichael Neuling 		return;
1389d0c0c9a1SMichael Neuling 	}
1390d0c0c9a1SMichael Neuling 
1391021424a1SMichael Ellerman 	die("Unexpected facility unavailable exception", regs, SIGABRT);
1392d0c0c9a1SMichael Neuling }
13932517617eSMichael Neuling #endif
1394d0c0c9a1SMichael Neuling 
1395f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1396f54db641SMichael Neuling 
1397f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1398f54db641SMichael Neuling {
1399f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1400f54db641SMichael Neuling 
1401f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1402f54db641SMichael Neuling 		 regs->nip, regs->msr);
1403f54db641SMichael Neuling 
1404f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1405f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1406f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1407f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1408f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1409f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1410f54db641SMichael Neuling 	 */
1411d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1412f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1413f54db641SMichael Neuling 
1414f54db641SMichael Neuling 	/* Enable FP for the task: */
1415f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1416f54db641SMichael Neuling 
1417f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1418f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1419f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
14203ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
14213ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1422f54db641SMichael Neuling 	 */
14233ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_FP);
14243ac8ff1cSPaul Mackerras 
14253ac8ff1cSPaul Mackerras 	/* If VMX is in use, get the transactional values back */
14263ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_VEC) {
14273ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
14283ac8ff1cSPaul Mackerras 		/* At this point all the VSX state is loaded, so enable it */
14293ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14303ac8ff1cSPaul Mackerras 	}
1431f54db641SMichael Neuling }
1432f54db641SMichael Neuling 
1433f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1434f54db641SMichael Neuling {
1435f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1436f54db641SMichael Neuling 	 * the same way.
1437f54db641SMichael Neuling 	 */
1438f54db641SMichael Neuling 
1439f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1440f54db641SMichael Neuling 		 "MSR=%lx\n",
1441f54db641SMichael Neuling 		 regs->nip, regs->msr);
1442d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1443f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
14443ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, MSR_VEC);
1445f54db641SMichael Neuling 	current->thread.used_vr = 1;
1446f54db641SMichael Neuling 
14473ac8ff1cSPaul Mackerras 	if (regs->msr & MSR_FP) {
14483ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
14493ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14503ac8ff1cSPaul Mackerras 	}
14513ac8ff1cSPaul Mackerras }
14523ac8ff1cSPaul Mackerras 
1453f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1454f54db641SMichael Neuling {
14553ac8ff1cSPaul Mackerras 	unsigned long orig_msr = regs->msr;
14563ac8ff1cSPaul Mackerras 
1457f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1458f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1459f54db641SMichael Neuling 	 *
1460f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1461f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1462f54db641SMichael Neuling 	 */
1463f54db641SMichael Neuling 
1464f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1465f54db641SMichael Neuling 		 "MSR=%lx\n",
1466f54db641SMichael Neuling 		 regs->nip, regs->msr);
1467f54db641SMichael Neuling 
14683ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
14693ac8ff1cSPaul Mackerras 
14703ac8ff1cSPaul Mackerras 	/* If FP and VMX are already loaded, we have all the state we need */
14713ac8ff1cSPaul Mackerras 	if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
14723ac8ff1cSPaul Mackerras 		regs->msr |= MSR_VSX;
14733ac8ff1cSPaul Mackerras 		return;
14743ac8ff1cSPaul Mackerras 	}
14753ac8ff1cSPaul Mackerras 
1476f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1477d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1478f54db641SMichael Neuling 
1479f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1480f54db641SMichael Neuling 		MSR_VSX;
14813ac8ff1cSPaul Mackerras 
14823ac8ff1cSPaul Mackerras 	/* This loads & recheckpoints FP and VRs; but we have
14833ac8ff1cSPaul Mackerras 	 * to be sure not to overwrite previously-valid state.
14843ac8ff1cSPaul Mackerras 	 */
14853ac8ff1cSPaul Mackerras 	tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
14863ac8ff1cSPaul Mackerras 
14873ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_FP)
14883ac8ff1cSPaul Mackerras 		do_load_up_transact_fpu(&current->thread);
14893ac8ff1cSPaul Mackerras 	if (orig_msr & MSR_VEC)
14903ac8ff1cSPaul Mackerras 		do_load_up_transact_altivec(&current->thread);
1491f54db641SMichael Neuling }
1492f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1493f54db641SMichael Neuling 
1494dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1495dc1c1ca3SStephen Rothwell {
149689713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
149789713ed1SAnton Blanchard 
1498dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1499dc1c1ca3SStephen Rothwell }
1500dc1c1ca3SStephen Rothwell 
15018dad3f92SPaul Mackerras #ifdef CONFIG_8xx
150214cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
150314cf11afSPaul Mackerras {
150414cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
150514cf11afSPaul Mackerras 
150614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
150714cf11afSPaul Mackerras 		debugger(regs);
15081eb2819dSLEROY Christophe 		die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
15091eb2819dSLEROY Christophe 			regs, SIGFPE);
151014cf11afSPaul Mackerras 	}
151114cf11afSPaul Mackerras 
15123a3b5aa6SKevin Hao 	if (!emulate_math(regs))
15133a3b5aa6SKevin Hao 		return;
15145fad293bSKumar Gala 
15155fad293bSKumar Gala 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
151614cf11afSPaul Mackerras }
15178dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
151814cf11afSPaul Mackerras 
1519172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
15203bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
15213bffb652SDave Kleikamp {
15223bffb652SDave Kleikamp 	int changed = 0;
15233bffb652SDave Kleikamp 	/*
15243bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
15253bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
15263bffb652SDave Kleikamp 	 */
15273bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
15283bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
15293bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
153051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
15313bffb652SDave Kleikamp #endif
15323bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
15333bffb652SDave Kleikamp 			     5);
15343bffb652SDave Kleikamp 		changed |= 0x01;
15353bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
15363bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
15373bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
15383bffb652SDave Kleikamp 			     6);
15393bffb652SDave Kleikamp 		changed |= 0x01;
15403bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
154151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
15423bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
15433bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
15443bffb652SDave Kleikamp 			     1);
15453bffb652SDave Kleikamp 		changed |= 0x01;
15463bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
154751ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
15483bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
15493bffb652SDave Kleikamp 			     2);
15503bffb652SDave Kleikamp 		changed |= 0x01;
15513bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
155251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
15533bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
15543bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
15553bffb652SDave Kleikamp 			     3);
15563bffb652SDave Kleikamp 		changed |= 0x01;
15573bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
155851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
15593bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
15603bffb652SDave Kleikamp 			     4);
15613bffb652SDave Kleikamp 		changed |= 0x01;
15623bffb652SDave Kleikamp 	}
15633bffb652SDave Kleikamp 	/*
15643bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
15653bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
15663bffb652SDave Kleikamp 	 * back on or not.
15673bffb652SDave Kleikamp 	 */
156851ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
156951ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
15703bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
15713bffb652SDave Kleikamp 	else
15723bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
157351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
15743bffb652SDave Kleikamp 
15753bffb652SDave Kleikamp 	if (changed & 0x01)
157651ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
15773bffb652SDave Kleikamp }
157814cf11afSPaul Mackerras 
1579f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
158014cf11afSPaul Mackerras {
158151ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
15823bffb652SDave Kleikamp 
1583ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1584ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1585ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1586ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1587ec097c84SRoland McGrath 	 */
1588ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1589ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1590ec097c84SRoland McGrath 
1591ec097c84SRoland McGrath 		/* Disable BT */
1592ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1593ec097c84SRoland McGrath 		/* Clear the BT event */
1594ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1595ec097c84SRoland McGrath 
1596ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1597ec097c84SRoland McGrath 		if (user_mode(regs)) {
159851ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
159951ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1600ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1601ec097c84SRoland McGrath 			return;
1602ec097c84SRoland McGrath 		}
1603ec097c84SRoland McGrath 
1604ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1605ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1606ec097c84SRoland McGrath 			return;
1607ec097c84SRoland McGrath 		}
1608ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1609ec097c84SRoland McGrath 			return;
1610ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
161114cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1612f8279621SKumar Gala 
161314cf11afSPaul Mackerras 		/* Disable instruction completion */
161414cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
161514cf11afSPaul Mackerras 		/* Clear the instruction completion event */
161614cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1617f8279621SKumar Gala 
1618f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1619f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
162014cf11afSPaul Mackerras 			return;
162114cf11afSPaul Mackerras 		}
1622f8279621SKumar Gala 
1623f8279621SKumar Gala 		if (debugger_sstep(regs))
1624f8279621SKumar Gala 			return;
1625f8279621SKumar Gala 
16263bffb652SDave Kleikamp 		if (user_mode(regs)) {
162751ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
162851ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
162951ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
16303bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
16313bffb652SDave Kleikamp 			else
16323bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
163351ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
16343bffb652SDave Kleikamp 		}
1635f8279621SKumar Gala 
1636f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
16373bffb652SDave Kleikamp 	} else
16383bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
163914cf11afSPaul Mackerras }
1640172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
164114cf11afSPaul Mackerras 
164214cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
164314cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
164414cf11afSPaul Mackerras {
164514cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
164614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
164714cf11afSPaul Mackerras }
164814cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
164914cf11afSPaul Mackerras 
165014cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1651dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
165214cf11afSPaul Mackerras {
165314cf11afSPaul Mackerras 	int err;
165414cf11afSPaul Mackerras 
165514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
165614cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
165714cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
16588dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
165914cf11afSPaul Mackerras 	}
166014cf11afSPaul Mackerras 
1661dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1662dc1c1ca3SStephen Rothwell 
1663eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
166414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
166514cf11afSPaul Mackerras 	if (err == 0) {
166614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
166714cf11afSPaul Mackerras 		emulate_single_step(regs);
166814cf11afSPaul Mackerras 		return;
166914cf11afSPaul Mackerras 	}
167014cf11afSPaul Mackerras 
167114cf11afSPaul Mackerras 	if (err == -EFAULT) {
167214cf11afSPaul Mackerras 		/* got an error reading the instruction */
167314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
167414cf11afSPaul Mackerras 	} else {
167514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
167614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
167776462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
167814cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1679de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
168014cf11afSPaul Mackerras 	}
168114cf11afSPaul Mackerras }
168214cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
168314cf11afSPaul Mackerras 
1684ce48b210SMichael Neuling #ifdef CONFIG_VSX
1685ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1686ce48b210SMichael Neuling {
1687ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1688ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1689ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1690ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1691ce48b210SMichael Neuling 	}
1692ce48b210SMichael Neuling 
1693ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1694ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1695ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1696ce48b210SMichael Neuling }
1697ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1698ce48b210SMichael Neuling 
169914cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
170014cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
170114cf11afSPaul Mackerras 			   unsigned long error_code)
170214cf11afSPaul Mackerras {
170314cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
170414cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
170514cf11afSPaul Mackerras 	 * something smarter
170614cf11afSPaul Mackerras 	 */
170714cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
170814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
170914cf11afSPaul Mackerras 	return;
171014cf11afSPaul Mackerras }
171114cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
171214cf11afSPaul Mackerras 
171314cf11afSPaul Mackerras #ifdef CONFIG_SPE
171414cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
171514cf11afSPaul Mackerras {
17166a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
171714cf11afSPaul Mackerras 	unsigned long spefscr;
171814cf11afSPaul Mackerras 	int fpexc_mode;
171914cf11afSPaul Mackerras 	int code = 0;
17206a800f36SLiu Yu 	int err;
17216a800f36SLiu Yu 
1722685659eeSyu liu 	flush_spe_to_thread(current);
172314cf11afSPaul Mackerras 
172414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
172514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
172614cf11afSPaul Mackerras 
172714cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
172814cf11afSPaul Mackerras 		code = FPE_FLTOVF;
172914cf11afSPaul Mackerras 	}
173014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
173114cf11afSPaul Mackerras 		code = FPE_FLTUND;
173214cf11afSPaul Mackerras 	}
173314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
173414cf11afSPaul Mackerras 		code = FPE_FLTDIV;
173514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
173614cf11afSPaul Mackerras 		code = FPE_FLTINV;
173714cf11afSPaul Mackerras 	}
173814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
173914cf11afSPaul Mackerras 		code = FPE_FLTRES;
174014cf11afSPaul Mackerras 
17416a800f36SLiu Yu 	err = do_spe_mathemu(regs);
17426a800f36SLiu Yu 	if (err == 0) {
17436a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17446a800f36SLiu Yu 		emulate_single_step(regs);
174514cf11afSPaul Mackerras 		return;
174614cf11afSPaul Mackerras 	}
17476a800f36SLiu Yu 
17486a800f36SLiu Yu 	if (err == -EFAULT) {
17496a800f36SLiu Yu 		/* got an error reading the instruction */
17506a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17516a800f36SLiu Yu 	} else if (err == -EINVAL) {
17526a800f36SLiu Yu 		/* didn't recognize the instruction */
17536a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17546a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17556a800f36SLiu Yu 	} else {
17566a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
17576a800f36SLiu Yu 	}
17586a800f36SLiu Yu 
17596a800f36SLiu Yu 	return;
17606a800f36SLiu Yu }
17616a800f36SLiu Yu 
17626a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
17636a800f36SLiu Yu {
17646a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
17656a800f36SLiu Yu 	int err;
17666a800f36SLiu Yu 
17676a800f36SLiu Yu 	preempt_disable();
17686a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
17696a800f36SLiu Yu 		giveup_spe(current);
17706a800f36SLiu Yu 	preempt_enable();
17716a800f36SLiu Yu 
17726a800f36SLiu Yu 	regs->nip -= 4;
17736a800f36SLiu Yu 	err = speround_handler(regs);
17746a800f36SLiu Yu 	if (err == 0) {
17756a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
17766a800f36SLiu Yu 		emulate_single_step(regs);
17776a800f36SLiu Yu 		return;
17786a800f36SLiu Yu 	}
17796a800f36SLiu Yu 
17806a800f36SLiu Yu 	if (err == -EFAULT) {
17816a800f36SLiu Yu 		/* got an error reading the instruction */
17826a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
17836a800f36SLiu Yu 	} else if (err == -EINVAL) {
17846a800f36SLiu Yu 		/* didn't recognize the instruction */
17856a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
17866a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
17876a800f36SLiu Yu 	} else {
17886a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
17896a800f36SLiu Yu 		return;
17906a800f36SLiu Yu 	}
17916a800f36SLiu Yu }
179214cf11afSPaul Mackerras #endif
179314cf11afSPaul Mackerras 
1794dc1c1ca3SStephen Rothwell /*
1795dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1796dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1797dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1798dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1799dc1c1ca3SStephen Rothwell  */
1800dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1801dc1c1ca3SStephen Rothwell {
1802dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1803dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1804dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1805dc1c1ca3SStephen Rothwell }
1806dc1c1ca3SStephen Rothwell 
18071e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
180814cf11afSPaul Mackerras /*
180914cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
181014cf11afSPaul Mackerras  * spins until a reboot occurs
181114cf11afSPaul Mackerras  */
181214cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
181314cf11afSPaul Mackerras {
181414cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
181514cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
181614cf11afSPaul Mackerras 	return;
181714cf11afSPaul Mackerras }
181814cf11afSPaul Mackerras 
181914cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
182014cf11afSPaul Mackerras {
182114cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
182214cf11afSPaul Mackerras 	WatchdogHandler(regs);
182314cf11afSPaul Mackerras }
182414cf11afSPaul Mackerras #endif
1825dc1c1ca3SStephen Rothwell 
1826dc1c1ca3SStephen Rothwell /*
1827dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1828dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1829dc1c1ca3SStephen Rothwell  */
1830dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1831dc1c1ca3SStephen Rothwell {
1832dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1833dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1834dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1835dc1c1ca3SStephen Rothwell }
183614cf11afSPaul Mackerras 
183714cf11afSPaul Mackerras void __init trap_init(void)
183814cf11afSPaul Mackerras {
183914cf11afSPaul Mackerras }
184080947e7cSGeert Uytterhoeven 
184180947e7cSGeert Uytterhoeven 
184280947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
184380947e7cSGeert Uytterhoeven 
184480947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
184580947e7cSGeert Uytterhoeven 
184680947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
184780947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
184880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
184980947e7cSGeert Uytterhoeven #endif
185080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
185180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
185280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
185380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
185480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
185580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
185680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
185780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
185880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
185980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
1860a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
186180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
186280947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
186380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
186480947e7cSGeert Uytterhoeven #endif
186580947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
186680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
186780947e7cSGeert Uytterhoeven #endif
1868efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1869efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1870efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1871efcac658SAlexey Kardashevskiy #endif
187280947e7cSGeert Uytterhoeven };
187380947e7cSGeert Uytterhoeven 
187480947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
187580947e7cSGeert Uytterhoeven 
187680947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
187780947e7cSGeert Uytterhoeven {
187876462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
187980947e7cSGeert Uytterhoeven 			    type);
188080947e7cSGeert Uytterhoeven }
188180947e7cSGeert Uytterhoeven 
188280947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
188380947e7cSGeert Uytterhoeven {
188480947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
188580947e7cSGeert Uytterhoeven 	unsigned int i;
188680947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
188780947e7cSGeert Uytterhoeven 
188880947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
188980947e7cSGeert Uytterhoeven 		return -ENODEV;
189080947e7cSGeert Uytterhoeven 
189180947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
189280947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
189380947e7cSGeert Uytterhoeven 	if (!dir)
189480947e7cSGeert Uytterhoeven 		return -ENOMEM;
189580947e7cSGeert Uytterhoeven 
189680947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
189780947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
189880947e7cSGeert Uytterhoeven 	if (!d)
189980947e7cSGeert Uytterhoeven 		goto fail;
190080947e7cSGeert Uytterhoeven 
190180947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
190280947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
190380947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
190480947e7cSGeert Uytterhoeven 		if (!d)
190580947e7cSGeert Uytterhoeven 			goto fail;
190680947e7cSGeert Uytterhoeven 	}
190780947e7cSGeert Uytterhoeven 
190880947e7cSGeert Uytterhoeven 	return 0;
190980947e7cSGeert Uytterhoeven 
191080947e7cSGeert Uytterhoeven fail:
191180947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
191280947e7cSGeert Uytterhoeven 	return -ENOMEM;
191380947e7cSGeert Uytterhoeven }
191480947e7cSGeert Uytterhoeven 
191580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
191680947e7cSGeert Uytterhoeven 
191780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1918