xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision d0c0c9a13f682157e8610565b6125a31d24434bc)
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>
3814cf11afSPaul Mackerras 
3980947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4014cf11afSPaul Mackerras #include <asm/pgtable.h>
4114cf11afSPaul Mackerras #include <asm/uaccess.h>
4214cf11afSPaul Mackerras #include <asm/io.h>
4386417780SPaul Mackerras #include <asm/machdep.h>
4486417780SPaul Mackerras #include <asm/rtas.h>
45f7f6f4feSDavid Gibson #include <asm/pmc.h>
46dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4714cf11afSPaul Mackerras #include <asm/reg.h>
4886417780SPaul Mackerras #endif
4914cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5014cf11afSPaul Mackerras #include <asm/backlight.h>
5114cf11afSPaul Mackerras #endif
52dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5386417780SPaul Mackerras #include <asm/firmware.h>
54dc1c1ca3SStephen Rothwell #include <asm/processor.h>
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>
61ae3a197eSDavid Howells #include <asm/debug.h>
62dc1c1ca3SStephen Rothwell 
637dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
645be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
655be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
699422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
705be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7114cf11afSPaul Mackerras 
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
779422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7914cf11afSPaul Mackerras #endif
8014cf11afSPaul Mackerras 
818b3c34cfSMichael Neuling /* Transactional Memory trap debug */
828b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
838b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
848b3c34cfSMichael Neuling #else
858b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
868b3c34cfSMichael Neuling #endif
878b3c34cfSMichael Neuling 
8814cf11afSPaul Mackerras /*
8914cf11afSPaul Mackerras  * Trap & Exception support
9014cf11afSPaul Mackerras  */
9114cf11afSPaul Mackerras 
926031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
936031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
946031d9d9Santon@samba.org {
956031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
966031d9d9Santon@samba.org 	if (pmac_backlight) {
976031d9d9Santon@samba.org 		struct backlight_properties *props;
986031d9d9Santon@samba.org 
996031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1006031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1016031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1026031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1036031d9d9Santon@samba.org 	}
1046031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1056031d9d9Santon@samba.org }
1066031d9d9Santon@samba.org #else
1076031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1086031d9d9Santon@samba.org #endif
1096031d9d9Santon@samba.org 
110760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
111760ca4dcSAnton Blanchard static int die_owner = -1;
112760ca4dcSAnton Blanchard static unsigned int die_nest_count;
113c0ce7d08SDavid Wilder static int die_counter;
114760ca4dcSAnton Blanchard 
115760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
116760ca4dcSAnton Blanchard {
117760ca4dcSAnton Blanchard 	int cpu;
11834c2a14fSanton@samba.org 	unsigned long flags;
11914cf11afSPaul Mackerras 
12014cf11afSPaul Mackerras 	if (debugger(regs))
12114cf11afSPaul Mackerras 		return 1;
12214cf11afSPaul Mackerras 
123293e4688Santon@samba.org 	oops_enter();
124293e4688Santon@samba.org 
125760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
126760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
127760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
128760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
129760ca4dcSAnton Blanchard 		if (cpu == die_owner)
130760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
131760ca4dcSAnton Blanchard 		else
132760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
133760ca4dcSAnton Blanchard 	}
134760ca4dcSAnton Blanchard 	die_nest_count++;
135760ca4dcSAnton Blanchard 	die_owner = cpu;
13614cf11afSPaul Mackerras 	console_verbose();
13714cf11afSPaul Mackerras 	bust_spinlocks(1);
1386031d9d9Santon@samba.org 	if (machine_is(powermac))
1396031d9d9Santon@samba.org 		pmac_backlight_unblank();
140760ca4dcSAnton Blanchard 	return flags;
14134c2a14fSanton@samba.org }
1425474c120SMichael Hanselmann 
143760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
144760ca4dcSAnton Blanchard 			       int signr)
145760ca4dcSAnton Blanchard {
14614cf11afSPaul Mackerras 	bust_spinlocks(0);
147760ca4dcSAnton Blanchard 	die_owner = -1;
148bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
149760ca4dcSAnton Blanchard 	die_nest_count--;
15058154c8cSAnton Blanchard 	oops_exit();
15158154c8cSAnton Blanchard 	printk("\n");
152760ca4dcSAnton Blanchard 	if (!die_nest_count)
153760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
154760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
155760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
156cc532915SMichael Ellerman 
157ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
158ebaeb5aeSMahesh Salgaonkar 
1599b00ac06SAnton Blanchard 	/*
1609b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1619b00ac06SAnton Blanchard 	 * it through the crashdump code.
1629b00ac06SAnton Blanchard 	 */
1639b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
164cc532915SMichael Ellerman 		crash_kexec(regs);
1659b00ac06SAnton Blanchard 
1669b00ac06SAnton Blanchard 		/*
1679b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1689b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1699b00ac06SAnton Blanchard 		 * code.
1709b00ac06SAnton Blanchard 		 */
171c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1729b00ac06SAnton Blanchard 	}
17314cf11afSPaul Mackerras 
174760ca4dcSAnton Blanchard 	if (!signr)
175760ca4dcSAnton Blanchard 		return;
176760ca4dcSAnton Blanchard 
17758154c8cSAnton Blanchard 	/*
17858154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
17958154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18058154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18158154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18258154c8cSAnton Blanchard 	 */
18358154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
18458154c8cSAnton Blanchard 	    is_global_init(current)) {
18558154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
18658154c8cSAnton Blanchard 	}
18758154c8cSAnton Blanchard 
18814cf11afSPaul Mackerras 	if (in_interrupt())
18914cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
190cea6a4baSHorms 	if (panic_on_oops)
191012c437dSHorms 		panic("Fatal exception");
192760ca4dcSAnton Blanchard 	do_exit(signr);
193760ca4dcSAnton Blanchard }
194cea6a4baSHorms 
195760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
196760ca4dcSAnton Blanchard {
197760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
198760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
199760ca4dcSAnton Blanchard 	printk("PREEMPT ");
200760ca4dcSAnton Blanchard #endif
201760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
202760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
203760ca4dcSAnton Blanchard #endif
204760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
205760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
206760ca4dcSAnton Blanchard #endif
207760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
208760ca4dcSAnton Blanchard 	printk("NUMA ");
209760ca4dcSAnton Blanchard #endif
210760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
211760ca4dcSAnton Blanchard 
212760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
213760ca4dcSAnton Blanchard 		return 1;
214760ca4dcSAnton Blanchard 
215760ca4dcSAnton Blanchard 	print_modules();
216760ca4dcSAnton Blanchard 	show_regs(regs);
21714cf11afSPaul Mackerras 
21814cf11afSPaul Mackerras 	return 0;
21914cf11afSPaul Mackerras }
22014cf11afSPaul Mackerras 
221760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
222760ca4dcSAnton Blanchard {
223760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
224760ca4dcSAnton Blanchard 
225760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
226760ca4dcSAnton Blanchard 		err = 0;
227760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
228760ca4dcSAnton Blanchard }
229760ca4dcSAnton Blanchard 
23025baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23125baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23225baa35bSOleg Nesterov {
23325baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
23425baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
23525baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
23625baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
23725baa35bSOleg Nesterov }
23825baa35bSOleg Nesterov 
23914cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24014cf11afSPaul Mackerras {
24114cf11afSPaul Mackerras 	siginfo_t info;
242d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
243d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
244d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
245d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
24614cf11afSPaul Mackerras 
24714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
248760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
24914cf11afSPaul Mackerras 		return;
250760ca4dcSAnton Blanchard 	}
251760ca4dcSAnton Blanchard 
252760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
25376462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
254d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
255d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
25614cf11afSPaul Mackerras 	}
25714cf11afSPaul Mackerras 
258a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2599f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2609f2f79e3SBenjamin Herrenschmidt 
26141ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26214cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
26314cf11afSPaul Mackerras 	info.si_signo = signr;
26414cf11afSPaul Mackerras 	info.si_code = code;
26514cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
26614cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
26714cf11afSPaul Mackerras }
26814cf11afSPaul Mackerras 
26914cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27014cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27114cf11afSPaul Mackerras {
27214cf11afSPaul Mackerras 	/* See if any machine dependent calls */
273c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
274c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
275c902be71SArnd Bergmann 			return;
276c902be71SArnd Bergmann 	}
27714cf11afSPaul Mackerras 
2788dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
27914cf11afSPaul Mackerras 
28014cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28114cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28214cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
28314cf11afSPaul Mackerras 
28414cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
28514cf11afSPaul Mackerras }
28614cf11afSPaul Mackerras #endif
28714cf11afSPaul Mackerras 
28814cf11afSPaul Mackerras /*
28914cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
29014cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
29114cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
29214cf11afSPaul Mackerras  * table.
29314cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
29414cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
29514cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
29614cf11afSPaul Mackerras  *  -- paulus.
29714cf11afSPaul Mackerras  */
29814cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
29914cf11afSPaul Mackerras {
30068a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
30114cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
30214cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
30314cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
30414cf11afSPaul Mackerras 
30514cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
30614cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
30714cf11afSPaul Mackerras 		/*
30814cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
30914cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
31014cf11afSPaul Mackerras 		 * As the address is in the exception table
31114cf11afSPaul Mackerras 		 * we should be able to read the instr there.
31214cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
31314cf11afSPaul Mackerras 		 * load or store.
31414cf11afSPaul Mackerras 		 */
31514cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
31614cf11afSPaul Mackerras 			nip -= 2;
31714cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
31814cf11afSPaul Mackerras 			--nip;
31914cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
32014cf11afSPaul Mackerras 			/* sync or twi */
32114cf11afSPaul Mackerras 			unsigned int rb;
32214cf11afSPaul Mackerras 
32314cf11afSPaul Mackerras 			--nip;
32414cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
32514cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
32614cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
32714cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
32814cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
32914cf11afSPaul Mackerras 			regs->nip = entry->fixup;
33014cf11afSPaul Mackerras 			return 1;
33114cf11afSPaul Mackerras 		}
33214cf11afSPaul Mackerras 	}
33368a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
33414cf11afSPaul Mackerras 	return 0;
33514cf11afSPaul Mackerras }
33614cf11afSPaul Mackerras 
337172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
33814cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
33914cf11afSPaul Mackerras    is in the ESR. */
34014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
34114cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
34214cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
34314cf11afSPaul Mackerras #else
344fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
34514cf11afSPaul Mackerras #endif
34614cf11afSPaul Mackerras #define REASON_FP		ESR_FP
34714cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
34814cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
34914cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
35014cf11afSPaul Mackerras 
35114cf11afSPaul Mackerras /* single-step stuff */
35214cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
35314cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
35414cf11afSPaul Mackerras 
35514cf11afSPaul Mackerras #else
35614cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
35714cf11afSPaul Mackerras    exception is in the MSR. */
35814cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
35914cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3608b3c34cfSMichael Neuling #define REASON_TM		0x200000
36114cf11afSPaul Mackerras #define REASON_FP		0x100000
36214cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
36314cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
36414cf11afSPaul Mackerras #define REASON_TRAP		0x20000
36514cf11afSPaul Mackerras 
36614cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
36714cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
36814cf11afSPaul Mackerras #endif
36914cf11afSPaul Mackerras 
37047c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
37147c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
37214cf11afSPaul Mackerras {
3731a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
37414cf11afSPaul Mackerras 
37514cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
37614cf11afSPaul Mackerras 		printk("Instruction");
37714cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
37814cf11afSPaul Mackerras 	} else
37914cf11afSPaul Mackerras 		printk("Data");
38014cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
38147c0bd1aSBenjamin Herrenschmidt 
38247c0bd1aSBenjamin Herrenschmidt 	return 0;
38347c0bd1aSBenjamin Herrenschmidt }
38447c0bd1aSBenjamin Herrenschmidt 
38547c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
38647c0bd1aSBenjamin Herrenschmidt {
38747c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
38847c0bd1aSBenjamin Herrenschmidt 
38914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
39014cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
39114cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
39214cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
39314cf11afSPaul Mackerras 	}
39414cf11afSPaul Mackerras 	else {
39514cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
39614cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
39714cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
39814cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
39914cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
40014cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
40114cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
40214cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
40314cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
40414cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
40514cf11afSPaul Mackerras 			flush_instruction_cache();
40614cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
40714cf11afSPaul Mackerras 		}
40814cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
40914cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
41014cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
41114cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
41214cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
41314cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
41414cf11afSPaul Mackerras 
41514cf11afSPaul Mackerras 		/* Clear MCSR */
41614cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
41714cf11afSPaul Mackerras 	}
41847c0bd1aSBenjamin Herrenschmidt 	return 0;
41947c0bd1aSBenjamin Herrenschmidt }
420fc5e7097SDave Kleikamp 
421fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
422fc5e7097SDave Kleikamp {
423fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
424fc5e7097SDave Kleikamp 	u32 mcsr;
425fc5e7097SDave Kleikamp 
426fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
427fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
428fc5e7097SDave Kleikamp 		printk(KERN_ERR
429fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
430fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
431fc5e7097SDave Kleikamp 		return 0;
432fc5e7097SDave Kleikamp 	}
433fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
434fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
435fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
436fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
437fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
438fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
439fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
440fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
441fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
442fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
443fc5e7097SDave Kleikamp 		flush_instruction_cache();
444fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
445fc5e7097SDave Kleikamp 	}
446fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
447fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
448fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
449fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
450fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
451fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
452fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
453fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
454fc5e7097SDave Kleikamp 
455fc5e7097SDave Kleikamp 	/* Clear MCSR */
456fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
457fc5e7097SDave Kleikamp 
458fc5e7097SDave Kleikamp 	return 0;
459fc5e7097SDave Kleikamp }
46014cf11afSPaul Mackerras #elif defined(CONFIG_E500)
461fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
462fe04b112SScott Wood {
463fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
464fe04b112SScott Wood 	unsigned long reason = mcsr;
465fe04b112SScott Wood 	int recoverable = 1;
466fe04b112SScott Wood 
46782a9a480SScott Wood 	if (reason & MCSR_LD) {
468cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
469cce1f106SShaohui Xie 		if (recoverable == 1)
470cce1f106SShaohui Xie 			goto silent_out;
471cce1f106SShaohui Xie 	}
472cce1f106SShaohui Xie 
473fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
474fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
475fe04b112SScott Wood 
476fe04b112SScott Wood 	if (reason & MCSR_MCP)
477fe04b112SScott Wood 		printk("Machine Check Signal\n");
478fe04b112SScott Wood 
479fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
480fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
481fe04b112SScott Wood 
482fe04b112SScott Wood 		/*
483fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
484fe04b112SScott Wood 		 */
485fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
486fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
487fe04b112SScott Wood 			;
488fe04b112SScott Wood 
489fe04b112SScott Wood 		/*
490fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
491fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
492fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
493fe04b112SScott Wood 		 */
494fe04b112SScott Wood 		reason &= ~MCSR_IF;
495fe04b112SScott Wood 	}
496fe04b112SScott Wood 
497fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
498fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
49937caf9f2SKumar Gala 
50037caf9f2SKumar Gala 		/*
50137caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
50237caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
50337caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
50437caf9f2SKumar Gala 		 */
50537caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
506fe04b112SScott Wood 			recoverable = 0;
507fe04b112SScott Wood 	}
508fe04b112SScott Wood 
509fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
510fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
511fe04b112SScott Wood 		recoverable = 0;
512fe04b112SScott Wood 	}
513fe04b112SScott Wood 
514fe04b112SScott Wood 	if (reason & MCSR_NMI)
515fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
516fe04b112SScott Wood 
517fe04b112SScott Wood 	if (reason & MCSR_IF) {
518fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
519fe04b112SScott Wood 		recoverable = 0;
520fe04b112SScott Wood 	}
521fe04b112SScott Wood 
522fe04b112SScott Wood 	if (reason & MCSR_LD) {
523fe04b112SScott Wood 		printk("Load Error Report\n");
524fe04b112SScott Wood 		recoverable = 0;
525fe04b112SScott Wood 	}
526fe04b112SScott Wood 
527fe04b112SScott Wood 	if (reason & MCSR_ST) {
528fe04b112SScott Wood 		printk("Store Error Report\n");
529fe04b112SScott Wood 		recoverable = 0;
530fe04b112SScott Wood 	}
531fe04b112SScott Wood 
532fe04b112SScott Wood 	if (reason & MCSR_LDG) {
533fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
534fe04b112SScott Wood 		recoverable = 0;
535fe04b112SScott Wood 	}
536fe04b112SScott Wood 
537fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
538fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
539fe04b112SScott Wood 
540fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
541fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
542fe04b112SScott Wood 		recoverable = 0;
543fe04b112SScott Wood 	}
544fe04b112SScott Wood 
545fe04b112SScott Wood 	if (reason & MCSR_MAV) {
546fe04b112SScott Wood 		u64 addr;
547fe04b112SScott Wood 
548fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
549fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
550fe04b112SScott Wood 
551fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
552fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
553fe04b112SScott Wood 	}
554fe04b112SScott Wood 
555cce1f106SShaohui Xie silent_out:
556fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
557fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
558fe04b112SScott Wood }
559fe04b112SScott Wood 
56047c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
56147c0bd1aSBenjamin Herrenschmidt {
56247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
56347c0bd1aSBenjamin Herrenschmidt 
564cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
565cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
566cce1f106SShaohui Xie 			return 1;
567cce1f106SShaohui Xie 	}
568cce1f106SShaohui Xie 
56914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
57114cf11afSPaul Mackerras 
57214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
57314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
57414cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
57514cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
57614cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
57714cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
57814cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
57914cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
58014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
58114cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
58214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
58314cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
58414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
58514cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
58614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
58714cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
58814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
58914cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
59114cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
59314cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
59414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
59514cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
59647c0bd1aSBenjamin Herrenschmidt 
59747c0bd1aSBenjamin Herrenschmidt 	return 0;
59847c0bd1aSBenjamin Herrenschmidt }
5994490c06bSKumar Gala 
6004490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6014490c06bSKumar Gala {
6024490c06bSKumar Gala 	return 0;
6034490c06bSKumar Gala }
60414cf11afSPaul Mackerras #elif defined(CONFIG_E200)
60547c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
60647c0bd1aSBenjamin Herrenschmidt {
60747c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
60847c0bd1aSBenjamin Herrenschmidt 
60914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
61114cf11afSPaul Mackerras 
61214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
61314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
61414cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
61514cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
61614cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
61714cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
61814cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
61914cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
62014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
62114cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
62214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
62314cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
62414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
62514cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
62647c0bd1aSBenjamin Herrenschmidt 
62747c0bd1aSBenjamin Herrenschmidt 	return 0;
62847c0bd1aSBenjamin Herrenschmidt }
62947c0bd1aSBenjamin Herrenschmidt #else
63047c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
63147c0bd1aSBenjamin Herrenschmidt {
63247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
63347c0bd1aSBenjamin Herrenschmidt 
63414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
63514cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
63614cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
63714cf11afSPaul Mackerras 	case 0x80000:
63814cf11afSPaul Mackerras 		printk("Machine check signal\n");
63914cf11afSPaul Mackerras 		break;
64014cf11afSPaul Mackerras 	case 0:		/* for 601 */
64114cf11afSPaul Mackerras 	case 0x40000:
64214cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
64314cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
64414cf11afSPaul Mackerras 		break;
64514cf11afSPaul Mackerras 	case 0x20000:
64614cf11afSPaul Mackerras 		printk("Data parity error signal\n");
64714cf11afSPaul Mackerras 		break;
64814cf11afSPaul Mackerras 	case 0x10000:
64914cf11afSPaul Mackerras 		printk("Address parity error signal\n");
65014cf11afSPaul Mackerras 		break;
65114cf11afSPaul Mackerras 	case 0x20000000:
65214cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
65314cf11afSPaul Mackerras 		break;
65414cf11afSPaul Mackerras 	case 0x40000000:
65514cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
65614cf11afSPaul Mackerras 		break;
65714cf11afSPaul Mackerras 	case 0x00100000:
65814cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
65914cf11afSPaul Mackerras 		break;
66014cf11afSPaul Mackerras 	default:
66114cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
66214cf11afSPaul Mackerras 	}
66375918a4bSOlof Johansson 	return 0;
66475918a4bSOlof Johansson }
66547c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
66675918a4bSOlof Johansson 
66775918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
66875918a4bSOlof Johansson {
66975918a4bSOlof Johansson 	int recover = 0;
67075918a4bSOlof Johansson 
67189713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
67289713ed1SAnton Blanchard 
67347c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
67447c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
67547c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
67647c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
67747c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
67847c0bd1aSBenjamin Herrenschmidt 	 */
67975918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
68075918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
68147c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
68247c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
68375918a4bSOlof Johansson 
68447c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
68575918a4bSOlof Johansson 		return;
68675918a4bSOlof Johansson 
68775918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
68847c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
68947c0bd1aSBenjamin Herrenschmidt 	 *
69047c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
69147c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
69247c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
69347c0bd1aSBenjamin Herrenschmidt 	 */
69475918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
69575918a4bSOlof Johansson 	return;
69675918a4bSOlof Johansson #endif
69775918a4bSOlof Johansson 
698a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
69975918a4bSOlof Johansson 		return;
70075918a4bSOlof Johansson 
70175918a4bSOlof Johansson 	if (check_io_access(regs))
70275918a4bSOlof Johansson 		return;
70375918a4bSOlof Johansson 
7048dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
70514cf11afSPaul Mackerras 
70614cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
70714cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
70814cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
70914cf11afSPaul Mackerras }
71014cf11afSPaul Mackerras 
71114cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
71214cf11afSPaul Mackerras {
71314cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
71414cf11afSPaul Mackerras }
71514cf11afSPaul Mackerras 
716dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
71714cf11afSPaul Mackerras {
71814cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
71914cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
72014cf11afSPaul Mackerras 
72114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
72214cf11afSPaul Mackerras }
72314cf11afSPaul Mackerras 
724dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
72514cf11afSPaul Mackerras {
72614cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
72714cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
72814cf11afSPaul Mackerras 		return;
72914cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
73014cf11afSPaul Mackerras 		return;
73114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
73214cf11afSPaul Mackerras }
73314cf11afSPaul Mackerras 
73414cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
73514cf11afSPaul Mackerras {
73614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
73714cf11afSPaul Mackerras }
73814cf11afSPaul Mackerras 
7398dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
74014cf11afSPaul Mackerras {
7412538c2d0SK.Prasad 	clear_single_step(regs);
74214cf11afSPaul Mackerras 
74314cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
74414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
74514cf11afSPaul Mackerras 		return;
74614cf11afSPaul Mackerras 	if (debugger_sstep(regs))
74714cf11afSPaul Mackerras 		return;
74814cf11afSPaul Mackerras 
74914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
75014cf11afSPaul Mackerras }
75114cf11afSPaul Mackerras 
75214cf11afSPaul Mackerras /*
75314cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
75414cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
75514cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
75614cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
75714cf11afSPaul Mackerras  */
7588dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
75914cf11afSPaul Mackerras {
7602538c2d0SK.Prasad 	if (single_stepping(regs))
7612538c2d0SK.Prasad 		single_step_exception(regs);
76214cf11afSPaul Mackerras }
76314cf11afSPaul Mackerras 
7645fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
765dc1c1ca3SStephen Rothwell {
7665fad293bSKumar Gala 	int ret = 0;
767dc1c1ca3SStephen Rothwell 
768dc1c1ca3SStephen Rothwell 	/* Invalid operation */
769dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7705fad293bSKumar Gala 		ret = FPE_FLTINV;
771dc1c1ca3SStephen Rothwell 
772dc1c1ca3SStephen Rothwell 	/* Overflow */
773dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7745fad293bSKumar Gala 		ret = FPE_FLTOVF;
775dc1c1ca3SStephen Rothwell 
776dc1c1ca3SStephen Rothwell 	/* Underflow */
777dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7785fad293bSKumar Gala 		ret = FPE_FLTUND;
779dc1c1ca3SStephen Rothwell 
780dc1c1ca3SStephen Rothwell 	/* Divide by zero */
781dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7825fad293bSKumar Gala 		ret = FPE_FLTDIV;
783dc1c1ca3SStephen Rothwell 
784dc1c1ca3SStephen Rothwell 	/* Inexact result */
785dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7865fad293bSKumar Gala 		ret = FPE_FLTRES;
7875fad293bSKumar Gala 
7885fad293bSKumar Gala 	return ret;
7895fad293bSKumar Gala }
7905fad293bSKumar Gala 
7915fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7925fad293bSKumar Gala {
7935fad293bSKumar Gala 	int code = 0;
7945fad293bSKumar Gala 
7955fad293bSKumar Gala 	flush_fp_to_thread(current);
7965fad293bSKumar Gala 
7975fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
798dc1c1ca3SStephen Rothwell 
799dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
800dc1c1ca3SStephen Rothwell }
801dc1c1ca3SStephen Rothwell 
802dc1c1ca3SStephen Rothwell /*
803dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
80414cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
80514cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
80614cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
80714cf11afSPaul Mackerras  *
80814cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
80914cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
81014cf11afSPaul Mackerras  * bits is faster and easier.
81186417780SPaul Mackerras  *
81214cf11afSPaul Mackerras  */
81314cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
81414cf11afSPaul Mackerras {
81514cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
81614cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
81714cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
81814cf11afSPaul Mackerras 	u32 num_bytes;
81914cf11afSPaul Mackerras 	unsigned long EA;
82014cf11afSPaul Mackerras 	int pos = 0;
82114cf11afSPaul Mackerras 
82214cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
82316c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
82414cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
82514cf11afSPaul Mackerras 			return -EINVAL;
82614cf11afSPaul Mackerras 
82714cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
82814cf11afSPaul Mackerras 
82916c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
83016c57b36SKumar Gala 		case PPC_INST_LSWX:
83116c57b36SKumar Gala 		case PPC_INST_STSWX:
83214cf11afSPaul Mackerras 			EA += NB_RB;
83314cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
83414cf11afSPaul Mackerras 			break;
83516c57b36SKumar Gala 		case PPC_INST_LSWI:
83616c57b36SKumar Gala 		case PPC_INST_STSWI:
83714cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
83814cf11afSPaul Mackerras 			break;
83914cf11afSPaul Mackerras 		default:
84014cf11afSPaul Mackerras 			return -EINVAL;
84114cf11afSPaul Mackerras 	}
84214cf11afSPaul Mackerras 
84314cf11afSPaul Mackerras 	while (num_bytes != 0)
84414cf11afSPaul Mackerras 	{
84514cf11afSPaul Mackerras 		u8 val;
84614cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
84714cf11afSPaul Mackerras 
84816c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
84916c57b36SKumar Gala 			case PPC_INST_LSWX:
85016c57b36SKumar Gala 			case PPC_INST_LSWI:
85114cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
85214cf11afSPaul Mackerras 					return -EFAULT;
85314cf11afSPaul Mackerras 				/* first time updating this reg,
85414cf11afSPaul Mackerras 				 * zero it out */
85514cf11afSPaul Mackerras 				if (pos == 0)
85614cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
85714cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
85814cf11afSPaul Mackerras 				break;
85916c57b36SKumar Gala 			case PPC_INST_STSWI:
86016c57b36SKumar Gala 			case PPC_INST_STSWX:
86114cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
86214cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
86314cf11afSPaul Mackerras 					return -EFAULT;
86414cf11afSPaul Mackerras 				break;
86514cf11afSPaul Mackerras 		}
86614cf11afSPaul Mackerras 		/* move EA to next address */
86714cf11afSPaul Mackerras 		EA += 1;
86814cf11afSPaul Mackerras 		num_bytes--;
86914cf11afSPaul Mackerras 
87014cf11afSPaul Mackerras 		/* manage our position within the register */
87114cf11afSPaul Mackerras 		if (++pos == 4) {
87214cf11afSPaul Mackerras 			pos = 0;
87314cf11afSPaul Mackerras 			if (++rT == 32)
87414cf11afSPaul Mackerras 				rT = 0;
87514cf11afSPaul Mackerras 		}
87614cf11afSPaul Mackerras 	}
87714cf11afSPaul Mackerras 
87814cf11afSPaul Mackerras 	return 0;
87914cf11afSPaul Mackerras }
88014cf11afSPaul Mackerras 
881c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
882c3412dcbSWill Schmidt {
883c3412dcbSWill Schmidt 	u32 ra,rs;
884c3412dcbSWill Schmidt 	unsigned long tmp;
885c3412dcbSWill Schmidt 
886c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
887c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
888c3412dcbSWill Schmidt 
889c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
890c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
891c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
892c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
893c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
894c3412dcbSWill Schmidt 
895c3412dcbSWill Schmidt 	return 0;
896c3412dcbSWill Schmidt }
897c3412dcbSWill Schmidt 
898c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
899c1469f13SKumar Gala {
900c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
901c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
902c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
903c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
904c1469f13SKumar Gala 	u8 bit;
905c1469f13SKumar Gala 	unsigned long tmp;
906c1469f13SKumar Gala 
907c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
908c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
909c1469f13SKumar Gala 
910c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
911c1469f13SKumar Gala 
912c1469f13SKumar Gala 	return 0;
913c1469f13SKumar Gala }
914c1469f13SKumar Gala 
91514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
91614cf11afSPaul Mackerras {
91714cf11afSPaul Mackerras 	u32 instword;
91814cf11afSPaul Mackerras 	u32 rd;
91914cf11afSPaul Mackerras 
920fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
92114cf11afSPaul Mackerras 		return -EINVAL;
92214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
92314cf11afSPaul Mackerras 
92414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
92514cf11afSPaul Mackerras 		return -EFAULT;
92614cf11afSPaul Mackerras 
92714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
92816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
929eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
93014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
93114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
93214cf11afSPaul Mackerras 		return 0;
93314cf11afSPaul Mackerras 	}
93414cf11afSPaul Mackerras 
93514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
93680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
937eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
93814cf11afSPaul Mackerras 		return 0;
93980947e7cSGeert Uytterhoeven 	}
94014cf11afSPaul Mackerras 
94114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
94216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
94386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
94414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
94514cf11afSPaul Mackerras 
946eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
94714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
94814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
94914cf11afSPaul Mackerras 		return 0;
95014cf11afSPaul Mackerras 	}
95114cf11afSPaul Mackerras 
95214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
95380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
954eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
95514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
95680947e7cSGeert Uytterhoeven 	}
95714cf11afSPaul Mackerras 
958c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
95916c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
960eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
961c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
962c3412dcbSWill Schmidt 	}
963c3412dcbSWill Schmidt 
964c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
96516c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
966eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
967c1469f13SKumar Gala 		return emulate_isel(regs, instword);
968c1469f13SKumar Gala 	}
969c1469f13SKumar Gala 
970efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
971efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
972efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
973efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
974efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
975efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
976efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
977efcac658SAlexey Kardashevskiy 		return 0;
978efcac658SAlexey Kardashevskiy 	}
979efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
980efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
981efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
982efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
983efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
98400ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
985efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
98600ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
987efcac658SAlexey Kardashevskiy 		return 0;
988efcac658SAlexey Kardashevskiy 	}
989efcac658SAlexey Kardashevskiy #endif
990efcac658SAlexey Kardashevskiy 
99114cf11afSPaul Mackerras 	return -EINVAL;
99214cf11afSPaul Mackerras }
99314cf11afSPaul Mackerras 
99473c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
99514cf11afSPaul Mackerras {
99673c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
99714cf11afSPaul Mackerras }
99814cf11afSPaul Mackerras 
9998dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
100014cf11afSPaul Mackerras {
100114cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
100214cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
100314cf11afSPaul Mackerras 
1004aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
100504903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
100614cf11afSPaul Mackerras 
100714cf11afSPaul Mackerras 	if (reason & REASON_FP) {
100814cf11afSPaul Mackerras 		/* IEEE FP exception */
1009dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
10108dad3f92SPaul Mackerras 		return;
10118dad3f92SPaul Mackerras 	}
10128dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1013ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1014ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1015ba797b28SJason Wessel 		if (debugger_bpt(regs))
1016ba797b28SJason Wessel 			return;
1017ba797b28SJason Wessel 
101814cf11afSPaul Mackerras 		/* trap exception */
1019dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1020dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1021dc1c1ca3SStephen Rothwell 			return;
102273c9ceabSJeremy Fitzhardinge 
102373c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1024608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
102514cf11afSPaul Mackerras 			regs->nip += 4;
102614cf11afSPaul Mackerras 			return;
102714cf11afSPaul Mackerras 		}
10288dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10298dad3f92SPaul Mackerras 		return;
10308dad3f92SPaul Mackerras 	}
10318dad3f92SPaul Mackerras 
1032a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1033a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1034cd8a5673SPaul Mackerras 		local_irq_enable();
1035cd8a5673SPaul Mackerras 
103604903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
103704903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
103804903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
103904903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
104004903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
104104903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
104204903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
104325985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10445fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10455fad293bSKumar Gala 	case 0:
104604903a30SKumar Gala 		emulate_single_step(regs);
104704903a30SKumar Gala 		return;
10485fad293bSKumar Gala 	case 1: {
10495fad293bSKumar Gala 			int code = 0;
10505fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10515fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10525fad293bSKumar Gala 			return;
105304903a30SKumar Gala 		}
10545fad293bSKumar Gala 	case -EFAULT:
10555fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10565fad293bSKumar Gala 		return;
10575fad293bSKumar Gala 	}
10585fad293bSKumar Gala 	/* fall through on any other errors */
105904903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
106004903a30SKumar Gala 
10618dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
10628dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
106314cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
106414cf11afSPaul Mackerras 		case 0:
106514cf11afSPaul Mackerras 			regs->nip += 4;
106614cf11afSPaul Mackerras 			emulate_single_step(regs);
10678dad3f92SPaul Mackerras 			return;
106814cf11afSPaul Mackerras 		case -EFAULT:
106914cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10708dad3f92SPaul Mackerras 			return;
10718dad3f92SPaul Mackerras 		}
10728dad3f92SPaul Mackerras 	}
10738dad3f92SPaul Mackerras 
107414cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
107514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
107614cf11afSPaul Mackerras 	else
107714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
107814cf11afSPaul Mackerras }
107914cf11afSPaul Mackerras 
1080dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
108114cf11afSPaul Mackerras {
10824393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
108314cf11afSPaul Mackerras 
1084a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1085a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1086a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1087a3512b2dSBenjamin Herrenschmidt 
1088e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1089e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
109014cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
109114cf11afSPaul Mackerras 
109214cf11afSPaul Mackerras 	if (fixed == 1) {
109314cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
109414cf11afSPaul Mackerras 		emulate_single_step(regs);
109514cf11afSPaul Mackerras 		return;
109614cf11afSPaul Mackerras 	}
109714cf11afSPaul Mackerras 
109814cf11afSPaul Mackerras 	/* Operand address was bad */
109914cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
11004393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
11014393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
11024393c4f6SBenjamin Herrenschmidt 	} else {
11034393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
11044393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
110514cf11afSPaul Mackerras 	}
11064393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
11074393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
11084393c4f6SBenjamin Herrenschmidt 	else
11094393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
111014cf11afSPaul Mackerras }
111114cf11afSPaul Mackerras 
111214cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
111314cf11afSPaul Mackerras {
111414cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
111514cf11afSPaul Mackerras 	       current, regs->gpr[1]);
111614cf11afSPaul Mackerras 	debugger(regs);
111714cf11afSPaul Mackerras 	show_regs(regs);
111814cf11afSPaul Mackerras 	panic("kernel stack overflow");
111914cf11afSPaul Mackerras }
112014cf11afSPaul Mackerras 
112114cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
112214cf11afSPaul Mackerras {
112314cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
112414cf11afSPaul Mackerras 	       regs->nip, regs->msr);
112514cf11afSPaul Mackerras 	debugger(regs);
112614cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
112714cf11afSPaul Mackerras }
112814cf11afSPaul Mackerras 
112914cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
113014cf11afSPaul Mackerras {
113114cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
113219c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
113314cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
113414cf11afSPaul Mackerras }
113514cf11afSPaul Mackerras 
1136dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1137dc1c1ca3SStephen Rothwell {
1138dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1139dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1140dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1141dc1c1ca3SStephen Rothwell }
1142dc1c1ca3SStephen Rothwell 
1143dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1144dc1c1ca3SStephen Rothwell {
1145dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1146dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1147dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1148dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1149dc1c1ca3SStephen Rothwell 		return;
1150dc1c1ca3SStephen Rothwell 	}
11516c4841c2SAnton Blanchard 
1152dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1153dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1154dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1155dc1c1ca3SStephen Rothwell }
1156dc1c1ca3SStephen Rothwell 
1157ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1158ce48b210SMichael Neuling {
1159ce48b210SMichael Neuling 	if (user_mode(regs)) {
1160ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1161ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1162ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1163ce48b210SMichael Neuling 		return;
1164ce48b210SMichael Neuling 	}
1165ce48b210SMichael Neuling 
1166ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1167ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1168ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1169ce48b210SMichael Neuling }
1170ce48b210SMichael Neuling 
1171*d0c0c9a1SMichael Neuling void tm_unavailable_exception(struct pt_regs *regs)
1172*d0c0c9a1SMichael Neuling {
1173*d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1174*d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1175*d0c0c9a1SMichael Neuling 		local_irq_enable();
1176*d0c0c9a1SMichael Neuling 
1177*d0c0c9a1SMichael Neuling 	/* Currently we never expect a TMU exception.  Catch
1178*d0c0c9a1SMichael Neuling 	 * this and kill the process!
1179*d0c0c9a1SMichael Neuling 	 */
1180*d0c0c9a1SMichael Neuling 	printk(KERN_EMERG "Unexpected TM unavailable exception at %lx "
1181*d0c0c9a1SMichael Neuling 	       "(msr %lx)\n",
1182*d0c0c9a1SMichael Neuling 	       regs->nip, regs->msr);
1183*d0c0c9a1SMichael Neuling 
1184*d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1185*d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1186*d0c0c9a1SMichael Neuling 		return;
1187*d0c0c9a1SMichael Neuling 	}
1188*d0c0c9a1SMichael Neuling 
1189*d0c0c9a1SMichael Neuling 	die("Unexpected TM unavailable exception", regs, SIGABRT);
1190*d0c0c9a1SMichael Neuling }
1191*d0c0c9a1SMichael Neuling 
1192dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1193dc1c1ca3SStephen Rothwell {
119489713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
119589713ed1SAnton Blanchard 
1196dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1197dc1c1ca3SStephen Rothwell }
1198dc1c1ca3SStephen Rothwell 
11998dad3f92SPaul Mackerras #ifdef CONFIG_8xx
120014cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
120114cf11afSPaul Mackerras {
120214cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
120314cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
12045dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
120514cf11afSPaul Mackerras 	int errcode;
12065dd57a13SScott Wood #endif
120714cf11afSPaul Mackerras 
120814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
120914cf11afSPaul Mackerras 
121014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
121114cf11afSPaul Mackerras 		debugger(regs);
121214cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
121314cf11afSPaul Mackerras 	}
121414cf11afSPaul Mackerras 
121514cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
121614cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
121780947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1218eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
12195fad293bSKumar Gala 
12205fad293bSKumar Gala 	switch (errcode) {
12215fad293bSKumar Gala 	case 0:
12225fad293bSKumar Gala 		emulate_single_step(regs);
12235fad293bSKumar Gala 		return;
12245fad293bSKumar Gala 	case 1: {
12255fad293bSKumar Gala 			int code = 0;
12265fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
12275fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
12285fad293bSKumar Gala 			return;
12295fad293bSKumar Gala 		}
12305fad293bSKumar Gala 	case -EFAULT:
12315fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12325fad293bSKumar Gala 		return;
12335fad293bSKumar Gala 	default:
12345fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12355fad293bSKumar Gala 		return;
12365fad293bSKumar Gala 	}
12375fad293bSKumar Gala 
12385dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
123914cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
124080947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1241eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
124280947e7cSGeert Uytterhoeven 
12435fad293bSKumar Gala 	switch (errcode) {
12445fad293bSKumar Gala 	case 0:
124514cf11afSPaul Mackerras 		emulate_single_step(regs);
12465fad293bSKumar Gala 		return;
12475fad293bSKumar Gala 	case 1:
12485fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12495fad293bSKumar Gala 		return;
12505fad293bSKumar Gala 	case -EFAULT:
12515fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12525fad293bSKumar Gala 		return;
12535fad293bSKumar Gala 	}
12545dd57a13SScott Wood #else
12555dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12565fad293bSKumar Gala #endif
125714cf11afSPaul Mackerras }
12588dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
125914cf11afSPaul Mackerras 
1260172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12613bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
12623bffb652SDave Kleikamp {
12633bffb652SDave Kleikamp 	int changed = 0;
12643bffb652SDave Kleikamp 	/*
12653bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
12663bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
12673bffb652SDave Kleikamp 	 */
12683bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
12693bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
12703bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
12713bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
12723bffb652SDave Kleikamp #endif
12733bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
12743bffb652SDave Kleikamp 			     5);
12753bffb652SDave Kleikamp 		changed |= 0x01;
12763bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
12773bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
12783bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
12793bffb652SDave Kleikamp 			     6);
12803bffb652SDave Kleikamp 		changed |= 0x01;
12813bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
12823bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
12833bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
12843bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
12853bffb652SDave Kleikamp 			     1);
12863bffb652SDave Kleikamp 		changed |= 0x01;
12873bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
12883bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
12893bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
12903bffb652SDave Kleikamp 			     2);
12913bffb652SDave Kleikamp 		changed |= 0x01;
12923bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
12933bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
12943bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
12953bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
12963bffb652SDave Kleikamp 			     3);
12973bffb652SDave Kleikamp 		changed |= 0x01;
12983bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12993bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
13003bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
13013bffb652SDave Kleikamp 			     4);
13023bffb652SDave Kleikamp 		changed |= 0x01;
13033bffb652SDave Kleikamp 	}
13043bffb652SDave Kleikamp 	/*
13053bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
13063bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
13073bffb652SDave Kleikamp 	 * back on or not.
13083bffb652SDave Kleikamp 	 */
13093bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
13103bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
13113bffb652SDave Kleikamp 	else
13123bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
13133bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
13143bffb652SDave Kleikamp 
13153bffb652SDave Kleikamp 	if (changed & 0x01)
13163bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
13173bffb652SDave Kleikamp }
131814cf11afSPaul Mackerras 
1319f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
132014cf11afSPaul Mackerras {
13213bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
13223bffb652SDave Kleikamp 
1323ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1324ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1325ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1326ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1327ec097c84SRoland McGrath 	 */
1328ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1329ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1330ec097c84SRoland McGrath 
1331ec097c84SRoland McGrath 		/* Disable BT */
1332ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1333ec097c84SRoland McGrath 		/* Clear the BT event */
1334ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1335ec097c84SRoland McGrath 
1336ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1337ec097c84SRoland McGrath 		if (user_mode(regs)) {
1338ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1339ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1340ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1341ec097c84SRoland McGrath 			return;
1342ec097c84SRoland McGrath 		}
1343ec097c84SRoland McGrath 
1344ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1345ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1346ec097c84SRoland McGrath 			return;
1347ec097c84SRoland McGrath 		}
1348ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1349ec097c84SRoland McGrath 			return;
1350ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
135114cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1352f8279621SKumar Gala 
135314cf11afSPaul Mackerras 		/* Disable instruction completion */
135414cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
135514cf11afSPaul Mackerras 		/* Clear the instruction completion event */
135614cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1357f8279621SKumar Gala 
1358f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1359f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
136014cf11afSPaul Mackerras 			return;
136114cf11afSPaul Mackerras 		}
1362f8279621SKumar Gala 
1363f8279621SKumar Gala 		if (debugger_sstep(regs))
1364f8279621SKumar Gala 			return;
1365f8279621SKumar Gala 
13663bffb652SDave Kleikamp 		if (user_mode(regs)) {
13673bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
13683bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
13693bffb652SDave Kleikamp 					       current->thread.dbcr1))
13703bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
13713bffb652SDave Kleikamp 			else
13723bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
13733bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
13743bffb652SDave Kleikamp 		}
1375f8279621SKumar Gala 
1376f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
13773bffb652SDave Kleikamp 	} else
13783bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
137914cf11afSPaul Mackerras }
1380172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
138114cf11afSPaul Mackerras 
138214cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
138314cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
138414cf11afSPaul Mackerras {
138514cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
138614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
138714cf11afSPaul Mackerras }
138814cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
138914cf11afSPaul Mackerras 
139014cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1391dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
139214cf11afSPaul Mackerras {
139314cf11afSPaul Mackerras 	int err;
139414cf11afSPaul Mackerras 
139514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
139614cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
139714cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13988dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
139914cf11afSPaul Mackerras 	}
140014cf11afSPaul Mackerras 
1401dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1402dc1c1ca3SStephen Rothwell 
1403eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
140414cf11afSPaul Mackerras 	err = emulate_altivec(regs);
140514cf11afSPaul Mackerras 	if (err == 0) {
140614cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
140714cf11afSPaul Mackerras 		emulate_single_step(regs);
140814cf11afSPaul Mackerras 		return;
140914cf11afSPaul Mackerras 	}
141014cf11afSPaul Mackerras 
141114cf11afSPaul Mackerras 	if (err == -EFAULT) {
141214cf11afSPaul Mackerras 		/* got an error reading the instruction */
141314cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
141414cf11afSPaul Mackerras 	} else {
141514cf11afSPaul Mackerras 		/* didn't recognize the instruction */
141614cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
141776462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
141814cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
141914cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
142014cf11afSPaul Mackerras 	}
142114cf11afSPaul Mackerras }
142214cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
142314cf11afSPaul Mackerras 
1424ce48b210SMichael Neuling #ifdef CONFIG_VSX
1425ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1426ce48b210SMichael Neuling {
1427ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1428ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1429ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1430ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1431ce48b210SMichael Neuling 	}
1432ce48b210SMichael Neuling 
1433ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1434ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1435ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1436ce48b210SMichael Neuling }
1437ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1438ce48b210SMichael Neuling 
143914cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
144014cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
144114cf11afSPaul Mackerras 			   unsigned long error_code)
144214cf11afSPaul Mackerras {
144314cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
144414cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
144514cf11afSPaul Mackerras 	 * something smarter
144614cf11afSPaul Mackerras 	 */
144714cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
144814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
144914cf11afSPaul Mackerras 	return;
145014cf11afSPaul Mackerras }
145114cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
145214cf11afSPaul Mackerras 
145314cf11afSPaul Mackerras #ifdef CONFIG_SPE
145414cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
145514cf11afSPaul Mackerras {
14566a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
145714cf11afSPaul Mackerras 	unsigned long spefscr;
145814cf11afSPaul Mackerras 	int fpexc_mode;
145914cf11afSPaul Mackerras 	int code = 0;
14606a800f36SLiu Yu 	int err;
14616a800f36SLiu Yu 
1462685659eeSyu liu 	flush_spe_to_thread(current);
146314cf11afSPaul Mackerras 
146414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
146514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
146614cf11afSPaul Mackerras 
146714cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
146814cf11afSPaul Mackerras 		code = FPE_FLTOVF;
146914cf11afSPaul Mackerras 	}
147014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
147114cf11afSPaul Mackerras 		code = FPE_FLTUND;
147214cf11afSPaul Mackerras 	}
147314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
147414cf11afSPaul Mackerras 		code = FPE_FLTDIV;
147514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
147614cf11afSPaul Mackerras 		code = FPE_FLTINV;
147714cf11afSPaul Mackerras 	}
147814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
147914cf11afSPaul Mackerras 		code = FPE_FLTRES;
148014cf11afSPaul Mackerras 
14816a800f36SLiu Yu 	err = do_spe_mathemu(regs);
14826a800f36SLiu Yu 	if (err == 0) {
14836a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14846a800f36SLiu Yu 		emulate_single_step(regs);
148514cf11afSPaul Mackerras 		return;
148614cf11afSPaul Mackerras 	}
14876a800f36SLiu Yu 
14886a800f36SLiu Yu 	if (err == -EFAULT) {
14896a800f36SLiu Yu 		/* got an error reading the instruction */
14906a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14916a800f36SLiu Yu 	} else if (err == -EINVAL) {
14926a800f36SLiu Yu 		/* didn't recognize the instruction */
14936a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14946a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14956a800f36SLiu Yu 	} else {
14966a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14976a800f36SLiu Yu 	}
14986a800f36SLiu Yu 
14996a800f36SLiu Yu 	return;
15006a800f36SLiu Yu }
15016a800f36SLiu Yu 
15026a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
15036a800f36SLiu Yu {
15046a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
15056a800f36SLiu Yu 	int err;
15066a800f36SLiu Yu 
15076a800f36SLiu Yu 	preempt_disable();
15086a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
15096a800f36SLiu Yu 		giveup_spe(current);
15106a800f36SLiu Yu 	preempt_enable();
15116a800f36SLiu Yu 
15126a800f36SLiu Yu 	regs->nip -= 4;
15136a800f36SLiu Yu 	err = speround_handler(regs);
15146a800f36SLiu Yu 	if (err == 0) {
15156a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
15166a800f36SLiu Yu 		emulate_single_step(regs);
15176a800f36SLiu Yu 		return;
15186a800f36SLiu Yu 	}
15196a800f36SLiu Yu 
15206a800f36SLiu Yu 	if (err == -EFAULT) {
15216a800f36SLiu Yu 		/* got an error reading the instruction */
15226a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
15236a800f36SLiu Yu 	} else if (err == -EINVAL) {
15246a800f36SLiu Yu 		/* didn't recognize the instruction */
15256a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
15266a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
15276a800f36SLiu Yu 	} else {
15286a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
15296a800f36SLiu Yu 		return;
15306a800f36SLiu Yu 	}
15316a800f36SLiu Yu }
153214cf11afSPaul Mackerras #endif
153314cf11afSPaul Mackerras 
1534dc1c1ca3SStephen Rothwell /*
1535dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1536dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1537dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1538dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1539dc1c1ca3SStephen Rothwell  */
1540dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1541dc1c1ca3SStephen Rothwell {
1542dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1543dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1544dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1545dc1c1ca3SStephen Rothwell }
1546dc1c1ca3SStephen Rothwell 
15471e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
154814cf11afSPaul Mackerras /*
154914cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
155014cf11afSPaul Mackerras  * spins until a reboot occurs
155114cf11afSPaul Mackerras  */
155214cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
155314cf11afSPaul Mackerras {
155414cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
155514cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
155614cf11afSPaul Mackerras 	return;
155714cf11afSPaul Mackerras }
155814cf11afSPaul Mackerras 
155914cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
156014cf11afSPaul Mackerras {
156114cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
156214cf11afSPaul Mackerras 	WatchdogHandler(regs);
156314cf11afSPaul Mackerras }
156414cf11afSPaul Mackerras #endif
1565dc1c1ca3SStephen Rothwell 
1566dc1c1ca3SStephen Rothwell /*
1567dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1568dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1569dc1c1ca3SStephen Rothwell  */
1570dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1571dc1c1ca3SStephen Rothwell {
1572dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1573dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1574dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1575dc1c1ca3SStephen Rothwell }
157614cf11afSPaul Mackerras 
157714cf11afSPaul Mackerras void __init trap_init(void)
157814cf11afSPaul Mackerras {
157914cf11afSPaul Mackerras }
158080947e7cSGeert Uytterhoeven 
158180947e7cSGeert Uytterhoeven 
158280947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
158380947e7cSGeert Uytterhoeven 
158480947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
158580947e7cSGeert Uytterhoeven 
158680947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
158780947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
158880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
158980947e7cSGeert Uytterhoeven #endif
159080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
159180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
159280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
159380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
159480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
159580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
159680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
159780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
159880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
159980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
160080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
160180947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
160280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
160380947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
160480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
160580947e7cSGeert Uytterhoeven #endif
160680947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
160780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
160880947e7cSGeert Uytterhoeven #endif
1609efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1610efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1611efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1612efcac658SAlexey Kardashevskiy #endif
161380947e7cSGeert Uytterhoeven };
161480947e7cSGeert Uytterhoeven 
161580947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
161680947e7cSGeert Uytterhoeven 
161780947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
161880947e7cSGeert Uytterhoeven {
161976462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
162080947e7cSGeert Uytterhoeven 			    type);
162180947e7cSGeert Uytterhoeven }
162280947e7cSGeert Uytterhoeven 
162380947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
162480947e7cSGeert Uytterhoeven {
162580947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
162680947e7cSGeert Uytterhoeven 	unsigned int i;
162780947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
162880947e7cSGeert Uytterhoeven 
162980947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
163080947e7cSGeert Uytterhoeven 		return -ENODEV;
163180947e7cSGeert Uytterhoeven 
163280947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
163380947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
163480947e7cSGeert Uytterhoeven 	if (!dir)
163580947e7cSGeert Uytterhoeven 		return -ENOMEM;
163680947e7cSGeert Uytterhoeven 
163780947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
163880947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
163980947e7cSGeert Uytterhoeven 	if (!d)
164080947e7cSGeert Uytterhoeven 		goto fail;
164180947e7cSGeert Uytterhoeven 
164280947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
164380947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
164480947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
164580947e7cSGeert Uytterhoeven 		if (!d)
164680947e7cSGeert Uytterhoeven 			goto fail;
164780947e7cSGeert Uytterhoeven 	}
164880947e7cSGeert Uytterhoeven 
164980947e7cSGeert Uytterhoeven 	return 0;
165080947e7cSGeert Uytterhoeven 
165180947e7cSGeert Uytterhoeven fail:
165280947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
165380947e7cSGeert Uytterhoeven 	return -ENOMEM;
165480947e7cSGeert Uytterhoeven }
165580947e7cSGeert Uytterhoeven 
165680947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
165780947e7cSGeert Uytterhoeven 
165880947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1659