xref: /linux/arch/powerpc/kernel/traps.c (revision a3512b2dd57cb653bb33645ca9c934436e547e3c)
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;
695be3492fSAnton Blanchard int (*__debugger_dabr_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);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7914cf11afSPaul Mackerras #endif
8014cf11afSPaul Mackerras 
8114cf11afSPaul Mackerras /*
8214cf11afSPaul Mackerras  * Trap & Exception support
8314cf11afSPaul Mackerras  */
8414cf11afSPaul Mackerras 
856031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
866031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
876031d9d9Santon@samba.org {
886031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
896031d9d9Santon@samba.org 	if (pmac_backlight) {
906031d9d9Santon@samba.org 		struct backlight_properties *props;
916031d9d9Santon@samba.org 
926031d9d9Santon@samba.org 		props = &pmac_backlight->props;
936031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
946031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
956031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
966031d9d9Santon@samba.org 	}
976031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
986031d9d9Santon@samba.org }
996031d9d9Santon@samba.org #else
1006031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1016031d9d9Santon@samba.org #endif
1026031d9d9Santon@samba.org 
103760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
104760ca4dcSAnton Blanchard static int die_owner = -1;
105760ca4dcSAnton Blanchard static unsigned int die_nest_count;
106c0ce7d08SDavid Wilder static int die_counter;
107760ca4dcSAnton Blanchard 
108760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
109760ca4dcSAnton Blanchard {
110760ca4dcSAnton Blanchard 	int cpu;
11134c2a14fSanton@samba.org 	unsigned long flags;
11214cf11afSPaul Mackerras 
11314cf11afSPaul Mackerras 	if (debugger(regs))
11414cf11afSPaul Mackerras 		return 1;
11514cf11afSPaul Mackerras 
116293e4688Santon@samba.org 	oops_enter();
117293e4688Santon@samba.org 
118760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
119760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
120760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
121760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
122760ca4dcSAnton Blanchard 		if (cpu == die_owner)
123760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
124760ca4dcSAnton Blanchard 		else
125760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
126760ca4dcSAnton Blanchard 	}
127760ca4dcSAnton Blanchard 	die_nest_count++;
128760ca4dcSAnton Blanchard 	die_owner = cpu;
12914cf11afSPaul Mackerras 	console_verbose();
13014cf11afSPaul Mackerras 	bust_spinlocks(1);
1316031d9d9Santon@samba.org 	if (machine_is(powermac))
1326031d9d9Santon@samba.org 		pmac_backlight_unblank();
133760ca4dcSAnton Blanchard 	return flags;
13434c2a14fSanton@samba.org }
1355474c120SMichael Hanselmann 
136760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
137760ca4dcSAnton Blanchard 			       int signr)
138760ca4dcSAnton Blanchard {
13914cf11afSPaul Mackerras 	bust_spinlocks(0);
140760ca4dcSAnton Blanchard 	die_owner = -1;
141bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
142760ca4dcSAnton Blanchard 	die_nest_count--;
14358154c8cSAnton Blanchard 	oops_exit();
14458154c8cSAnton Blanchard 	printk("\n");
145760ca4dcSAnton Blanchard 	if (!die_nest_count)
146760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
147760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
148760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
149cc532915SMichael Ellerman 
150ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
151ebaeb5aeSMahesh Salgaonkar 
1529b00ac06SAnton Blanchard 	/*
1539b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1549b00ac06SAnton Blanchard 	 * it through the crashdump code.
1559b00ac06SAnton Blanchard 	 */
1569b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
157cc532915SMichael Ellerman 		crash_kexec(regs);
1589b00ac06SAnton Blanchard 
1599b00ac06SAnton Blanchard 		/*
1609b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1619b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1629b00ac06SAnton Blanchard 		 * code.
1639b00ac06SAnton Blanchard 		 */
164c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1659b00ac06SAnton Blanchard 	}
16614cf11afSPaul Mackerras 
167760ca4dcSAnton Blanchard 	if (!signr)
168760ca4dcSAnton Blanchard 		return;
169760ca4dcSAnton Blanchard 
17058154c8cSAnton Blanchard 	/*
17158154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
17258154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
17358154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
17458154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
17558154c8cSAnton Blanchard 	 */
17658154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
17758154c8cSAnton Blanchard 	    is_global_init(current)) {
17858154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
17958154c8cSAnton Blanchard 	}
18058154c8cSAnton Blanchard 
18114cf11afSPaul Mackerras 	if (in_interrupt())
18214cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
183cea6a4baSHorms 	if (panic_on_oops)
184012c437dSHorms 		panic("Fatal exception");
185760ca4dcSAnton Blanchard 	do_exit(signr);
186760ca4dcSAnton Blanchard }
187cea6a4baSHorms 
188760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
189760ca4dcSAnton Blanchard {
190760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
191760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
192760ca4dcSAnton Blanchard 	printk("PREEMPT ");
193760ca4dcSAnton Blanchard #endif
194760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
195760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
196760ca4dcSAnton Blanchard #endif
197760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
198760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
199760ca4dcSAnton Blanchard #endif
200760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
201760ca4dcSAnton Blanchard 	printk("NUMA ");
202760ca4dcSAnton Blanchard #endif
203760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
204760ca4dcSAnton Blanchard 
205760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
206760ca4dcSAnton Blanchard 		return 1;
207760ca4dcSAnton Blanchard 
208760ca4dcSAnton Blanchard 	print_modules();
209760ca4dcSAnton Blanchard 	show_regs(regs);
21014cf11afSPaul Mackerras 
21114cf11afSPaul Mackerras 	return 0;
21214cf11afSPaul Mackerras }
21314cf11afSPaul Mackerras 
214760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
215760ca4dcSAnton Blanchard {
216760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
217760ca4dcSAnton Blanchard 
218760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
219760ca4dcSAnton Blanchard 		err = 0;
220760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
221760ca4dcSAnton Blanchard }
222760ca4dcSAnton Blanchard 
22325baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
22425baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
22525baa35bSOleg Nesterov {
22625baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
22725baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
22825baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
22925baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
23025baa35bSOleg Nesterov }
23125baa35bSOleg Nesterov 
23214cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
23314cf11afSPaul Mackerras {
23414cf11afSPaul Mackerras 	siginfo_t info;
235d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
236d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
237d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
238d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
23914cf11afSPaul Mackerras 
24014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
241760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
24214cf11afSPaul Mackerras 		return;
243760ca4dcSAnton Blanchard 	}
244760ca4dcSAnton Blanchard 
245760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
24676462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
247d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
248d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
24914cf11afSPaul Mackerras 	}
25014cf11afSPaul Mackerras 
251*a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2529f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2539f2f79e3SBenjamin Herrenschmidt 
25414cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
25514cf11afSPaul Mackerras 	info.si_signo = signr;
25614cf11afSPaul Mackerras 	info.si_code = code;
25714cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
25814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
25914cf11afSPaul Mackerras }
26014cf11afSPaul Mackerras 
26114cf11afSPaul Mackerras #ifdef CONFIG_PPC64
26214cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
26314cf11afSPaul Mackerras {
26414cf11afSPaul Mackerras 	/* See if any machine dependent calls */
265c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
266c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
267c902be71SArnd Bergmann 			return;
268c902be71SArnd Bergmann 	}
26914cf11afSPaul Mackerras 
2708dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
27114cf11afSPaul Mackerras 
27214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
27314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
27414cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
27514cf11afSPaul Mackerras 
27614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
27714cf11afSPaul Mackerras }
27814cf11afSPaul Mackerras #endif
27914cf11afSPaul Mackerras 
28014cf11afSPaul Mackerras /*
28114cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
28214cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
28314cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
28414cf11afSPaul Mackerras  * table.
28514cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
28614cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
28714cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
28814cf11afSPaul Mackerras  *  -- paulus.
28914cf11afSPaul Mackerras  */
29014cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
29114cf11afSPaul Mackerras {
29268a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
29314cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
29414cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
29514cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
29614cf11afSPaul Mackerras 
29714cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
29814cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
29914cf11afSPaul Mackerras 		/*
30014cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
30114cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
30214cf11afSPaul Mackerras 		 * As the address is in the exception table
30314cf11afSPaul Mackerras 		 * we should be able to read the instr there.
30414cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
30514cf11afSPaul Mackerras 		 * load or store.
30614cf11afSPaul Mackerras 		 */
30714cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
30814cf11afSPaul Mackerras 			nip -= 2;
30914cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
31014cf11afSPaul Mackerras 			--nip;
31114cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
31214cf11afSPaul Mackerras 			/* sync or twi */
31314cf11afSPaul Mackerras 			unsigned int rb;
31414cf11afSPaul Mackerras 
31514cf11afSPaul Mackerras 			--nip;
31614cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
31714cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
31814cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
31914cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
32014cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
32114cf11afSPaul Mackerras 			regs->nip = entry->fixup;
32214cf11afSPaul Mackerras 			return 1;
32314cf11afSPaul Mackerras 		}
32414cf11afSPaul Mackerras 	}
32568a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
32614cf11afSPaul Mackerras 	return 0;
32714cf11afSPaul Mackerras }
32814cf11afSPaul Mackerras 
329172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
33014cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
33114cf11afSPaul Mackerras    is in the ESR. */
33214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
33314cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
33414cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
33514cf11afSPaul Mackerras #else
336fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
33714cf11afSPaul Mackerras #endif
33814cf11afSPaul Mackerras #define REASON_FP		ESR_FP
33914cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
34014cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
34114cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
34214cf11afSPaul Mackerras 
34314cf11afSPaul Mackerras /* single-step stuff */
34414cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
34514cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
34614cf11afSPaul Mackerras 
34714cf11afSPaul Mackerras #else
34814cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
34914cf11afSPaul Mackerras    exception is in the MSR. */
35014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
35114cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
35214cf11afSPaul Mackerras #define REASON_FP		0x100000
35314cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
35414cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
35514cf11afSPaul Mackerras #define REASON_TRAP		0x20000
35614cf11afSPaul Mackerras 
35714cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
35814cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
35914cf11afSPaul Mackerras #endif
36014cf11afSPaul Mackerras 
36147c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
36247c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
36314cf11afSPaul Mackerras {
3641a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
36514cf11afSPaul Mackerras 
36614cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
36714cf11afSPaul Mackerras 		printk("Instruction");
36814cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
36914cf11afSPaul Mackerras 	} else
37014cf11afSPaul Mackerras 		printk("Data");
37114cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
37247c0bd1aSBenjamin Herrenschmidt 
37347c0bd1aSBenjamin Herrenschmidt 	return 0;
37447c0bd1aSBenjamin Herrenschmidt }
37547c0bd1aSBenjamin Herrenschmidt 
37647c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
37747c0bd1aSBenjamin Herrenschmidt {
37847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
37947c0bd1aSBenjamin Herrenschmidt 
38014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
38114cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
38214cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
38314cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
38414cf11afSPaul Mackerras 	}
38514cf11afSPaul Mackerras 	else {
38614cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
38714cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
38814cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
38914cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
39014cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
39114cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
39214cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
39314cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
39414cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
39514cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
39614cf11afSPaul Mackerras 			flush_instruction_cache();
39714cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
39814cf11afSPaul Mackerras 		}
39914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
40014cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
40114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
40214cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
40314cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
40414cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
40514cf11afSPaul Mackerras 
40614cf11afSPaul Mackerras 		/* Clear MCSR */
40714cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
40814cf11afSPaul Mackerras 	}
40947c0bd1aSBenjamin Herrenschmidt 	return 0;
41047c0bd1aSBenjamin Herrenschmidt }
411fc5e7097SDave Kleikamp 
412fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
413fc5e7097SDave Kleikamp {
414fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
415fc5e7097SDave Kleikamp 	u32 mcsr;
416fc5e7097SDave Kleikamp 
417fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
418fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
419fc5e7097SDave Kleikamp 		printk(KERN_ERR
420fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
421fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
422fc5e7097SDave Kleikamp 		return 0;
423fc5e7097SDave Kleikamp 	}
424fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
425fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
426fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
427fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
428fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
429fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
430fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
431fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
432fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
433fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
434fc5e7097SDave Kleikamp 		flush_instruction_cache();
435fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
436fc5e7097SDave Kleikamp 	}
437fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
438fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
439fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
440fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
441fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
442fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
443fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
444fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
445fc5e7097SDave Kleikamp 
446fc5e7097SDave Kleikamp 	/* Clear MCSR */
447fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
448fc5e7097SDave Kleikamp 
449fc5e7097SDave Kleikamp 	return 0;
450fc5e7097SDave Kleikamp }
45114cf11afSPaul Mackerras #elif defined(CONFIG_E500)
452fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
453fe04b112SScott Wood {
454fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
455fe04b112SScott Wood 	unsigned long reason = mcsr;
456fe04b112SScott Wood 	int recoverable = 1;
457fe04b112SScott Wood 
45882a9a480SScott Wood 	if (reason & MCSR_LD) {
459cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
460cce1f106SShaohui Xie 		if (recoverable == 1)
461cce1f106SShaohui Xie 			goto silent_out;
462cce1f106SShaohui Xie 	}
463cce1f106SShaohui Xie 
464fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
465fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
466fe04b112SScott Wood 
467fe04b112SScott Wood 	if (reason & MCSR_MCP)
468fe04b112SScott Wood 		printk("Machine Check Signal\n");
469fe04b112SScott Wood 
470fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
471fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
472fe04b112SScott Wood 
473fe04b112SScott Wood 		/*
474fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
475fe04b112SScott Wood 		 */
476fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
477fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
478fe04b112SScott Wood 			;
479fe04b112SScott Wood 
480fe04b112SScott Wood 		/*
481fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
482fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
483fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
484fe04b112SScott Wood 		 */
485fe04b112SScott Wood 		reason &= ~MCSR_IF;
486fe04b112SScott Wood 	}
487fe04b112SScott Wood 
488fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
489fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
49037caf9f2SKumar Gala 
49137caf9f2SKumar Gala 		/*
49237caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
49337caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
49437caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
49537caf9f2SKumar Gala 		 */
49637caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
497fe04b112SScott Wood 			recoverable = 0;
498fe04b112SScott Wood 	}
499fe04b112SScott Wood 
500fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
501fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
502fe04b112SScott Wood 		recoverable = 0;
503fe04b112SScott Wood 	}
504fe04b112SScott Wood 
505fe04b112SScott Wood 	if (reason & MCSR_NMI)
506fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
507fe04b112SScott Wood 
508fe04b112SScott Wood 	if (reason & MCSR_IF) {
509fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
510fe04b112SScott Wood 		recoverable = 0;
511fe04b112SScott Wood 	}
512fe04b112SScott Wood 
513fe04b112SScott Wood 	if (reason & MCSR_LD) {
514fe04b112SScott Wood 		printk("Load Error Report\n");
515fe04b112SScott Wood 		recoverable = 0;
516fe04b112SScott Wood 	}
517fe04b112SScott Wood 
518fe04b112SScott Wood 	if (reason & MCSR_ST) {
519fe04b112SScott Wood 		printk("Store Error Report\n");
520fe04b112SScott Wood 		recoverable = 0;
521fe04b112SScott Wood 	}
522fe04b112SScott Wood 
523fe04b112SScott Wood 	if (reason & MCSR_LDG) {
524fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
525fe04b112SScott Wood 		recoverable = 0;
526fe04b112SScott Wood 	}
527fe04b112SScott Wood 
528fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
529fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
530fe04b112SScott Wood 
531fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
532fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
533fe04b112SScott Wood 		recoverable = 0;
534fe04b112SScott Wood 	}
535fe04b112SScott Wood 
536fe04b112SScott Wood 	if (reason & MCSR_MAV) {
537fe04b112SScott Wood 		u64 addr;
538fe04b112SScott Wood 
539fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
540fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
541fe04b112SScott Wood 
542fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
543fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
544fe04b112SScott Wood 	}
545fe04b112SScott Wood 
546cce1f106SShaohui Xie silent_out:
547fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
548fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
549fe04b112SScott Wood }
550fe04b112SScott Wood 
55147c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
55247c0bd1aSBenjamin Herrenschmidt {
55347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
55447c0bd1aSBenjamin Herrenschmidt 
555cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
556cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
557cce1f106SShaohui Xie 			return 1;
558cce1f106SShaohui Xie 	}
559cce1f106SShaohui Xie 
56014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
56114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
56214cf11afSPaul Mackerras 
56314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
56414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
56514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
56614cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
56714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
56814cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
56914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
57014cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
57114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
57214cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
57314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
57414cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
57514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
57614cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
57714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
57814cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
57914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
58014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
58114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
58214cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
58314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
58414cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
58514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
58614cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
58747c0bd1aSBenjamin Herrenschmidt 
58847c0bd1aSBenjamin Herrenschmidt 	return 0;
58947c0bd1aSBenjamin Herrenschmidt }
5904490c06bSKumar Gala 
5914490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5924490c06bSKumar Gala {
5934490c06bSKumar Gala 	return 0;
5944490c06bSKumar Gala }
59514cf11afSPaul Mackerras #elif defined(CONFIG_E200)
59647c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
59747c0bd1aSBenjamin Herrenschmidt {
59847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
59947c0bd1aSBenjamin Herrenschmidt 
60014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
60114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
60214cf11afSPaul Mackerras 
60314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
60414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
60514cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
60614cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
60714cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
60814cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
60914cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
61014cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
61114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
61214cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
61314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
61414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
61514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
61614cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
61747c0bd1aSBenjamin Herrenschmidt 
61847c0bd1aSBenjamin Herrenschmidt 	return 0;
61947c0bd1aSBenjamin Herrenschmidt }
62047c0bd1aSBenjamin Herrenschmidt #else
62147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
62247c0bd1aSBenjamin Herrenschmidt {
62347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
62447c0bd1aSBenjamin Herrenschmidt 
62514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
62714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
62814cf11afSPaul Mackerras 	case 0x80000:
62914cf11afSPaul Mackerras 		printk("Machine check signal\n");
63014cf11afSPaul Mackerras 		break;
63114cf11afSPaul Mackerras 	case 0:		/* for 601 */
63214cf11afSPaul Mackerras 	case 0x40000:
63314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
63414cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
63514cf11afSPaul Mackerras 		break;
63614cf11afSPaul Mackerras 	case 0x20000:
63714cf11afSPaul Mackerras 		printk("Data parity error signal\n");
63814cf11afSPaul Mackerras 		break;
63914cf11afSPaul Mackerras 	case 0x10000:
64014cf11afSPaul Mackerras 		printk("Address parity error signal\n");
64114cf11afSPaul Mackerras 		break;
64214cf11afSPaul Mackerras 	case 0x20000000:
64314cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
64414cf11afSPaul Mackerras 		break;
64514cf11afSPaul Mackerras 	case 0x40000000:
64614cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
64714cf11afSPaul Mackerras 		break;
64814cf11afSPaul Mackerras 	case 0x00100000:
64914cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
65014cf11afSPaul Mackerras 		break;
65114cf11afSPaul Mackerras 	default:
65214cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
65314cf11afSPaul Mackerras 	}
65475918a4bSOlof Johansson 	return 0;
65575918a4bSOlof Johansson }
65647c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
65775918a4bSOlof Johansson 
65875918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
65975918a4bSOlof Johansson {
66075918a4bSOlof Johansson 	int recover = 0;
66175918a4bSOlof Johansson 
66289713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
66389713ed1SAnton Blanchard 
66447c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
66547c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
66647c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
66747c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
66847c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
66947c0bd1aSBenjamin Herrenschmidt 	 */
67075918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
67175918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
67247c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
67347c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
67475918a4bSOlof Johansson 
67547c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
67675918a4bSOlof Johansson 		return;
67775918a4bSOlof Johansson 
67875918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
67947c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
68047c0bd1aSBenjamin Herrenschmidt 	 *
68147c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
68247c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
68347c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
68447c0bd1aSBenjamin Herrenschmidt 	 */
68575918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
68675918a4bSOlof Johansson 	return;
68775918a4bSOlof Johansson #endif
68875918a4bSOlof Johansson 
689a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
69075918a4bSOlof Johansson 		return;
69175918a4bSOlof Johansson 
69275918a4bSOlof Johansson 	if (check_io_access(regs))
69375918a4bSOlof Johansson 		return;
69475918a4bSOlof Johansson 
6958dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
69614cf11afSPaul Mackerras 
69714cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
69814cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
69914cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
70014cf11afSPaul Mackerras }
70114cf11afSPaul Mackerras 
70214cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
70314cf11afSPaul Mackerras {
70414cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
70514cf11afSPaul Mackerras }
70614cf11afSPaul Mackerras 
707dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
70814cf11afSPaul Mackerras {
70914cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
71014cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
71114cf11afSPaul Mackerras 
71214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
71314cf11afSPaul Mackerras }
71414cf11afSPaul Mackerras 
715dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
71614cf11afSPaul Mackerras {
71714cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
71814cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
71914cf11afSPaul Mackerras 		return;
72014cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
72114cf11afSPaul Mackerras 		return;
72214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
72314cf11afSPaul Mackerras }
72414cf11afSPaul Mackerras 
72514cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
72614cf11afSPaul Mackerras {
72714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
72814cf11afSPaul Mackerras }
72914cf11afSPaul Mackerras 
7308dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
73114cf11afSPaul Mackerras {
7322538c2d0SK.Prasad 	clear_single_step(regs);
73314cf11afSPaul Mackerras 
73414cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
73514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
73614cf11afSPaul Mackerras 		return;
73714cf11afSPaul Mackerras 	if (debugger_sstep(regs))
73814cf11afSPaul Mackerras 		return;
73914cf11afSPaul Mackerras 
74014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
74114cf11afSPaul Mackerras }
74214cf11afSPaul Mackerras 
74314cf11afSPaul Mackerras /*
74414cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
74514cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
74614cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
74714cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
74814cf11afSPaul Mackerras  */
7498dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
75014cf11afSPaul Mackerras {
7512538c2d0SK.Prasad 	if (single_stepping(regs))
7522538c2d0SK.Prasad 		single_step_exception(regs);
75314cf11afSPaul Mackerras }
75414cf11afSPaul Mackerras 
7555fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
756dc1c1ca3SStephen Rothwell {
7575fad293bSKumar Gala 	int ret = 0;
758dc1c1ca3SStephen Rothwell 
759dc1c1ca3SStephen Rothwell 	/* Invalid operation */
760dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7615fad293bSKumar Gala 		ret = FPE_FLTINV;
762dc1c1ca3SStephen Rothwell 
763dc1c1ca3SStephen Rothwell 	/* Overflow */
764dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7655fad293bSKumar Gala 		ret = FPE_FLTOVF;
766dc1c1ca3SStephen Rothwell 
767dc1c1ca3SStephen Rothwell 	/* Underflow */
768dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7695fad293bSKumar Gala 		ret = FPE_FLTUND;
770dc1c1ca3SStephen Rothwell 
771dc1c1ca3SStephen Rothwell 	/* Divide by zero */
772dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7735fad293bSKumar Gala 		ret = FPE_FLTDIV;
774dc1c1ca3SStephen Rothwell 
775dc1c1ca3SStephen Rothwell 	/* Inexact result */
776dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7775fad293bSKumar Gala 		ret = FPE_FLTRES;
7785fad293bSKumar Gala 
7795fad293bSKumar Gala 	return ret;
7805fad293bSKumar Gala }
7815fad293bSKumar Gala 
7825fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7835fad293bSKumar Gala {
7845fad293bSKumar Gala 	int code = 0;
7855fad293bSKumar Gala 
7865fad293bSKumar Gala 	flush_fp_to_thread(current);
7875fad293bSKumar Gala 
7885fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
789dc1c1ca3SStephen Rothwell 
790dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
791dc1c1ca3SStephen Rothwell }
792dc1c1ca3SStephen Rothwell 
793dc1c1ca3SStephen Rothwell /*
794dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
79514cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
79614cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
79714cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
79814cf11afSPaul Mackerras  *
79914cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
80014cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
80114cf11afSPaul Mackerras  * bits is faster and easier.
80286417780SPaul Mackerras  *
80314cf11afSPaul Mackerras  */
80414cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
80514cf11afSPaul Mackerras {
80614cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
80714cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
80814cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
80914cf11afSPaul Mackerras 	u32 num_bytes;
81014cf11afSPaul Mackerras 	unsigned long EA;
81114cf11afSPaul Mackerras 	int pos = 0;
81214cf11afSPaul Mackerras 
81314cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
81416c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
81514cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
81614cf11afSPaul Mackerras 			return -EINVAL;
81714cf11afSPaul Mackerras 
81814cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
81914cf11afSPaul Mackerras 
82016c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
82116c57b36SKumar Gala 		case PPC_INST_LSWX:
82216c57b36SKumar Gala 		case PPC_INST_STSWX:
82314cf11afSPaul Mackerras 			EA += NB_RB;
82414cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
82514cf11afSPaul Mackerras 			break;
82616c57b36SKumar Gala 		case PPC_INST_LSWI:
82716c57b36SKumar Gala 		case PPC_INST_STSWI:
82814cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
82914cf11afSPaul Mackerras 			break;
83014cf11afSPaul Mackerras 		default:
83114cf11afSPaul Mackerras 			return -EINVAL;
83214cf11afSPaul Mackerras 	}
83314cf11afSPaul Mackerras 
83414cf11afSPaul Mackerras 	while (num_bytes != 0)
83514cf11afSPaul Mackerras 	{
83614cf11afSPaul Mackerras 		u8 val;
83714cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
83814cf11afSPaul Mackerras 
83916c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
84016c57b36SKumar Gala 			case PPC_INST_LSWX:
84116c57b36SKumar Gala 			case PPC_INST_LSWI:
84214cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
84314cf11afSPaul Mackerras 					return -EFAULT;
84414cf11afSPaul Mackerras 				/* first time updating this reg,
84514cf11afSPaul Mackerras 				 * zero it out */
84614cf11afSPaul Mackerras 				if (pos == 0)
84714cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
84814cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
84914cf11afSPaul Mackerras 				break;
85016c57b36SKumar Gala 			case PPC_INST_STSWI:
85116c57b36SKumar Gala 			case PPC_INST_STSWX:
85214cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
85314cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
85414cf11afSPaul Mackerras 					return -EFAULT;
85514cf11afSPaul Mackerras 				break;
85614cf11afSPaul Mackerras 		}
85714cf11afSPaul Mackerras 		/* move EA to next address */
85814cf11afSPaul Mackerras 		EA += 1;
85914cf11afSPaul Mackerras 		num_bytes--;
86014cf11afSPaul Mackerras 
86114cf11afSPaul Mackerras 		/* manage our position within the register */
86214cf11afSPaul Mackerras 		if (++pos == 4) {
86314cf11afSPaul Mackerras 			pos = 0;
86414cf11afSPaul Mackerras 			if (++rT == 32)
86514cf11afSPaul Mackerras 				rT = 0;
86614cf11afSPaul Mackerras 		}
86714cf11afSPaul Mackerras 	}
86814cf11afSPaul Mackerras 
86914cf11afSPaul Mackerras 	return 0;
87014cf11afSPaul Mackerras }
87114cf11afSPaul Mackerras 
872c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
873c3412dcbSWill Schmidt {
874c3412dcbSWill Schmidt 	u32 ra,rs;
875c3412dcbSWill Schmidt 	unsigned long tmp;
876c3412dcbSWill Schmidt 
877c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
878c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
879c3412dcbSWill Schmidt 
880c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
881c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
882c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
883c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
884c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
885c3412dcbSWill Schmidt 
886c3412dcbSWill Schmidt 	return 0;
887c3412dcbSWill Schmidt }
888c3412dcbSWill Schmidt 
889c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
890c1469f13SKumar Gala {
891c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
892c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
893c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
894c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
895c1469f13SKumar Gala 	u8 bit;
896c1469f13SKumar Gala 	unsigned long tmp;
897c1469f13SKumar Gala 
898c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
899c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
900c1469f13SKumar Gala 
901c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
902c1469f13SKumar Gala 
903c1469f13SKumar Gala 	return 0;
904c1469f13SKumar Gala }
905c1469f13SKumar Gala 
90614cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
90714cf11afSPaul Mackerras {
90814cf11afSPaul Mackerras 	u32 instword;
90914cf11afSPaul Mackerras 	u32 rd;
91014cf11afSPaul Mackerras 
911fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
91214cf11afSPaul Mackerras 		return -EINVAL;
91314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
91414cf11afSPaul Mackerras 
91514cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
91614cf11afSPaul Mackerras 		return -EFAULT;
91714cf11afSPaul Mackerras 
91814cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
91916c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
920eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
92114cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
92214cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
92314cf11afSPaul Mackerras 		return 0;
92414cf11afSPaul Mackerras 	}
92514cf11afSPaul Mackerras 
92614cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
92780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
928eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
92914cf11afSPaul Mackerras 		return 0;
93080947e7cSGeert Uytterhoeven 	}
93114cf11afSPaul Mackerras 
93214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
93316c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
93486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
93514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
93614cf11afSPaul Mackerras 
937eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
93814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
93914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
94014cf11afSPaul Mackerras 		return 0;
94114cf11afSPaul Mackerras 	}
94214cf11afSPaul Mackerras 
94314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
94480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
945eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
94614cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
94780947e7cSGeert Uytterhoeven 	}
94814cf11afSPaul Mackerras 
949c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
95016c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
951eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
952c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
953c3412dcbSWill Schmidt 	}
954c3412dcbSWill Schmidt 
955c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
95616c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
957eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
958c1469f13SKumar Gala 		return emulate_isel(regs, instword);
959c1469f13SKumar Gala 	}
960c1469f13SKumar Gala 
961efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
962efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
963efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
964efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
965efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
966efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
967efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
968efcac658SAlexey Kardashevskiy 		return 0;
969efcac658SAlexey Kardashevskiy 	}
970efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
971efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
972efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
973efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
974efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
975efcac658SAlexey Kardashevskiy 		mtspr(SPRN_DSCR, regs->gpr[rd]);
976efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
977efcac658SAlexey Kardashevskiy 		return 0;
978efcac658SAlexey Kardashevskiy 	}
979efcac658SAlexey Kardashevskiy #endif
980efcac658SAlexey Kardashevskiy 
98114cf11afSPaul Mackerras 	return -EINVAL;
98214cf11afSPaul Mackerras }
98314cf11afSPaul Mackerras 
98473c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
98514cf11afSPaul Mackerras {
98673c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
98714cf11afSPaul Mackerras }
98814cf11afSPaul Mackerras 
9898dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
99014cf11afSPaul Mackerras {
99114cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
99214cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
99314cf11afSPaul Mackerras 
994aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
99504903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
99614cf11afSPaul Mackerras 
99714cf11afSPaul Mackerras 	if (reason & REASON_FP) {
99814cf11afSPaul Mackerras 		/* IEEE FP exception */
999dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
10008dad3f92SPaul Mackerras 		return;
10018dad3f92SPaul Mackerras 	}
10028dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1003ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1004ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1005ba797b28SJason Wessel 		if (debugger_bpt(regs))
1006ba797b28SJason Wessel 			return;
1007ba797b28SJason Wessel 
100814cf11afSPaul Mackerras 		/* trap exception */
1009dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1010dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1011dc1c1ca3SStephen Rothwell 			return;
101273c9ceabSJeremy Fitzhardinge 
101373c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1014608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
101514cf11afSPaul Mackerras 			regs->nip += 4;
101614cf11afSPaul Mackerras 			return;
101714cf11afSPaul Mackerras 		}
10188dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10198dad3f92SPaul Mackerras 		return;
10208dad3f92SPaul Mackerras 	}
10218dad3f92SPaul Mackerras 
1022*a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1023*a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1024cd8a5673SPaul Mackerras 		local_irq_enable();
1025cd8a5673SPaul Mackerras 
102604903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
102704903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
102804903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
102904903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
103004903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
103104903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
103204903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
103325985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10345fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10355fad293bSKumar Gala 	case 0:
103604903a30SKumar Gala 		emulate_single_step(regs);
103704903a30SKumar Gala 		return;
10385fad293bSKumar Gala 	case 1: {
10395fad293bSKumar Gala 			int code = 0;
10405fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10415fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10425fad293bSKumar Gala 			return;
104304903a30SKumar Gala 		}
10445fad293bSKumar Gala 	case -EFAULT:
10455fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10465fad293bSKumar Gala 		return;
10475fad293bSKumar Gala 	}
10485fad293bSKumar Gala 	/* fall through on any other errors */
104904903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
105004903a30SKumar Gala 
10518dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
10528dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
105314cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
105414cf11afSPaul Mackerras 		case 0:
105514cf11afSPaul Mackerras 			regs->nip += 4;
105614cf11afSPaul Mackerras 			emulate_single_step(regs);
10578dad3f92SPaul Mackerras 			return;
105814cf11afSPaul Mackerras 		case -EFAULT:
105914cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10608dad3f92SPaul Mackerras 			return;
10618dad3f92SPaul Mackerras 		}
10628dad3f92SPaul Mackerras 	}
10638dad3f92SPaul Mackerras 
106414cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
106514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
106614cf11afSPaul Mackerras 	else
106714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
106814cf11afSPaul Mackerras }
106914cf11afSPaul Mackerras 
1070dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
107114cf11afSPaul Mackerras {
10724393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
107314cf11afSPaul Mackerras 
1074*a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1075*a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1076*a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1077*a3512b2dSBenjamin Herrenschmidt 
1078e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1079e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
108014cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
108114cf11afSPaul Mackerras 
108214cf11afSPaul Mackerras 	if (fixed == 1) {
108314cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
108414cf11afSPaul Mackerras 		emulate_single_step(regs);
108514cf11afSPaul Mackerras 		return;
108614cf11afSPaul Mackerras 	}
108714cf11afSPaul Mackerras 
108814cf11afSPaul Mackerras 	/* Operand address was bad */
108914cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
10904393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
10914393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
10924393c4f6SBenjamin Herrenschmidt 	} else {
10934393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
10944393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
109514cf11afSPaul Mackerras 	}
10964393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
10974393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
10984393c4f6SBenjamin Herrenschmidt 	else
10994393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
110014cf11afSPaul Mackerras }
110114cf11afSPaul Mackerras 
110214cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
110314cf11afSPaul Mackerras {
110414cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
110514cf11afSPaul Mackerras 	       current, regs->gpr[1]);
110614cf11afSPaul Mackerras 	debugger(regs);
110714cf11afSPaul Mackerras 	show_regs(regs);
110814cf11afSPaul Mackerras 	panic("kernel stack overflow");
110914cf11afSPaul Mackerras }
111014cf11afSPaul Mackerras 
111114cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
111214cf11afSPaul Mackerras {
111314cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
111414cf11afSPaul Mackerras 	       regs->nip, regs->msr);
111514cf11afSPaul Mackerras 	debugger(regs);
111614cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
111714cf11afSPaul Mackerras }
111814cf11afSPaul Mackerras 
111914cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
112014cf11afSPaul Mackerras {
112114cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
112219c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
112314cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
112414cf11afSPaul Mackerras }
112514cf11afSPaul Mackerras 
1126dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1127dc1c1ca3SStephen Rothwell {
1128dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1129dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1130dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1131dc1c1ca3SStephen Rothwell }
1132dc1c1ca3SStephen Rothwell 
1133dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1134dc1c1ca3SStephen Rothwell {
1135dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1136dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1137dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1138dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1139dc1c1ca3SStephen Rothwell 		return;
1140dc1c1ca3SStephen Rothwell 	}
11416c4841c2SAnton Blanchard 
1142dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1143dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1144dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1145dc1c1ca3SStephen Rothwell }
1146dc1c1ca3SStephen Rothwell 
1147ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1148ce48b210SMichael Neuling {
1149ce48b210SMichael Neuling 	if (user_mode(regs)) {
1150ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1151ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1152ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1153ce48b210SMichael Neuling 		return;
1154ce48b210SMichael Neuling 	}
1155ce48b210SMichael Neuling 
1156ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1157ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1158ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1159ce48b210SMichael Neuling }
1160ce48b210SMichael Neuling 
1161dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1162dc1c1ca3SStephen Rothwell {
116389713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
116489713ed1SAnton Blanchard 
1165dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1166dc1c1ca3SStephen Rothwell }
1167dc1c1ca3SStephen Rothwell 
11688dad3f92SPaul Mackerras #ifdef CONFIG_8xx
116914cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
117014cf11afSPaul Mackerras {
117114cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
117214cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
11735dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
117414cf11afSPaul Mackerras 	int errcode;
11755dd57a13SScott Wood #endif
117614cf11afSPaul Mackerras 
117714cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
117814cf11afSPaul Mackerras 
117914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
118014cf11afSPaul Mackerras 		debugger(regs);
118114cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
118214cf11afSPaul Mackerras 	}
118314cf11afSPaul Mackerras 
118414cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
118514cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
118680947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1187eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
11885fad293bSKumar Gala 
11895fad293bSKumar Gala 	switch (errcode) {
11905fad293bSKumar Gala 	case 0:
11915fad293bSKumar Gala 		emulate_single_step(regs);
11925fad293bSKumar Gala 		return;
11935fad293bSKumar Gala 	case 1: {
11945fad293bSKumar Gala 			int code = 0;
11955fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11965fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
11975fad293bSKumar Gala 			return;
11985fad293bSKumar Gala 		}
11995fad293bSKumar Gala 	case -EFAULT:
12005fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12015fad293bSKumar Gala 		return;
12025fad293bSKumar Gala 	default:
12035fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12045fad293bSKumar Gala 		return;
12055fad293bSKumar Gala 	}
12065fad293bSKumar Gala 
12075dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
120814cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
120980947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1210eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
121180947e7cSGeert Uytterhoeven 
12125fad293bSKumar Gala 	switch (errcode) {
12135fad293bSKumar Gala 	case 0:
121414cf11afSPaul Mackerras 		emulate_single_step(regs);
12155fad293bSKumar Gala 		return;
12165fad293bSKumar Gala 	case 1:
12175fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12185fad293bSKumar Gala 		return;
12195fad293bSKumar Gala 	case -EFAULT:
12205fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12215fad293bSKumar Gala 		return;
12225fad293bSKumar Gala 	}
12235dd57a13SScott Wood #else
12245dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12255fad293bSKumar Gala #endif
122614cf11afSPaul Mackerras }
12278dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
122814cf11afSPaul Mackerras 
1229172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12303bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
12313bffb652SDave Kleikamp {
12323bffb652SDave Kleikamp 	int changed = 0;
12333bffb652SDave Kleikamp 	/*
12343bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
12353bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
12363bffb652SDave Kleikamp 	 */
12373bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
12383bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
12393bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
12403bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
12413bffb652SDave Kleikamp #endif
12423bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
12433bffb652SDave Kleikamp 			     5);
12443bffb652SDave Kleikamp 		changed |= 0x01;
12453bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
12463bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
12473bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
12483bffb652SDave Kleikamp 			     6);
12493bffb652SDave Kleikamp 		changed |= 0x01;
12503bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
12513bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
12523bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
12533bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
12543bffb652SDave Kleikamp 			     1);
12553bffb652SDave Kleikamp 		changed |= 0x01;
12563bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
12573bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
12583bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
12593bffb652SDave Kleikamp 			     2);
12603bffb652SDave Kleikamp 		changed |= 0x01;
12613bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
12623bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
12633bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
12643bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
12653bffb652SDave Kleikamp 			     3);
12663bffb652SDave Kleikamp 		changed |= 0x01;
12673bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12683bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
12693bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
12703bffb652SDave Kleikamp 			     4);
12713bffb652SDave Kleikamp 		changed |= 0x01;
12723bffb652SDave Kleikamp 	}
12733bffb652SDave Kleikamp 	/*
12743bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
12753bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
12763bffb652SDave Kleikamp 	 * back on or not.
12773bffb652SDave Kleikamp 	 */
12783bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
12793bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
12803bffb652SDave Kleikamp 	else
12813bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
12823bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
12833bffb652SDave Kleikamp 
12843bffb652SDave Kleikamp 	if (changed & 0x01)
12853bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
12863bffb652SDave Kleikamp }
128714cf11afSPaul Mackerras 
1288f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
128914cf11afSPaul Mackerras {
12903bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
12913bffb652SDave Kleikamp 
1292ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1293ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1294ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1295ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1296ec097c84SRoland McGrath 	 */
1297ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1298ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1299ec097c84SRoland McGrath 
1300ec097c84SRoland McGrath 		/* Disable BT */
1301ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1302ec097c84SRoland McGrath 		/* Clear the BT event */
1303ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1304ec097c84SRoland McGrath 
1305ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1306ec097c84SRoland McGrath 		if (user_mode(regs)) {
1307ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1308ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1309ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1310ec097c84SRoland McGrath 			return;
1311ec097c84SRoland McGrath 		}
1312ec097c84SRoland McGrath 
1313ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1314ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1315ec097c84SRoland McGrath 			return;
1316ec097c84SRoland McGrath 		}
1317ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1318ec097c84SRoland McGrath 			return;
1319ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
132014cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1321f8279621SKumar Gala 
132214cf11afSPaul Mackerras 		/* Disable instruction completion */
132314cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
132414cf11afSPaul Mackerras 		/* Clear the instruction completion event */
132514cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1326f8279621SKumar Gala 
1327f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1328f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
132914cf11afSPaul Mackerras 			return;
133014cf11afSPaul Mackerras 		}
1331f8279621SKumar Gala 
1332f8279621SKumar Gala 		if (debugger_sstep(regs))
1333f8279621SKumar Gala 			return;
1334f8279621SKumar Gala 
13353bffb652SDave Kleikamp 		if (user_mode(regs)) {
13363bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
13373bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
13383bffb652SDave Kleikamp 					       current->thread.dbcr1))
13393bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
13403bffb652SDave Kleikamp 			else
13413bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
13423bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
13433bffb652SDave Kleikamp 		}
1344f8279621SKumar Gala 
1345f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
13463bffb652SDave Kleikamp 	} else
13473bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
134814cf11afSPaul Mackerras }
1349172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
135014cf11afSPaul Mackerras 
135114cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
135214cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
135314cf11afSPaul Mackerras {
135414cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
135514cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
135614cf11afSPaul Mackerras }
135714cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
135814cf11afSPaul Mackerras 
135914cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1360dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
136114cf11afSPaul Mackerras {
136214cf11afSPaul Mackerras 	int err;
136314cf11afSPaul Mackerras 
136414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
136514cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
136614cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13678dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
136814cf11afSPaul Mackerras 	}
136914cf11afSPaul Mackerras 
1370dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1371dc1c1ca3SStephen Rothwell 
1372eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
137314cf11afSPaul Mackerras 	err = emulate_altivec(regs);
137414cf11afSPaul Mackerras 	if (err == 0) {
137514cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
137614cf11afSPaul Mackerras 		emulate_single_step(regs);
137714cf11afSPaul Mackerras 		return;
137814cf11afSPaul Mackerras 	}
137914cf11afSPaul Mackerras 
138014cf11afSPaul Mackerras 	if (err == -EFAULT) {
138114cf11afSPaul Mackerras 		/* got an error reading the instruction */
138214cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
138314cf11afSPaul Mackerras 	} else {
138414cf11afSPaul Mackerras 		/* didn't recognize the instruction */
138514cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
138676462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
138714cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
138814cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
138914cf11afSPaul Mackerras 	}
139014cf11afSPaul Mackerras }
139114cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
139214cf11afSPaul Mackerras 
1393ce48b210SMichael Neuling #ifdef CONFIG_VSX
1394ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1395ce48b210SMichael Neuling {
1396ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1397ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1398ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1399ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1400ce48b210SMichael Neuling 	}
1401ce48b210SMichael Neuling 
1402ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1403ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1404ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1405ce48b210SMichael Neuling }
1406ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1407ce48b210SMichael Neuling 
140814cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
140914cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
141014cf11afSPaul Mackerras 			   unsigned long error_code)
141114cf11afSPaul Mackerras {
141214cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
141314cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
141414cf11afSPaul Mackerras 	 * something smarter
141514cf11afSPaul Mackerras 	 */
141614cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
141714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
141814cf11afSPaul Mackerras 	return;
141914cf11afSPaul Mackerras }
142014cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
142114cf11afSPaul Mackerras 
142214cf11afSPaul Mackerras #ifdef CONFIG_SPE
142314cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
142414cf11afSPaul Mackerras {
14256a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
142614cf11afSPaul Mackerras 	unsigned long spefscr;
142714cf11afSPaul Mackerras 	int fpexc_mode;
142814cf11afSPaul Mackerras 	int code = 0;
14296a800f36SLiu Yu 	int err;
14306a800f36SLiu Yu 
1431685659eeSyu liu 	flush_spe_to_thread(current);
143214cf11afSPaul Mackerras 
143314cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
143414cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
143514cf11afSPaul Mackerras 
143614cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
143714cf11afSPaul Mackerras 		code = FPE_FLTOVF;
143814cf11afSPaul Mackerras 	}
143914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
144014cf11afSPaul Mackerras 		code = FPE_FLTUND;
144114cf11afSPaul Mackerras 	}
144214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
144314cf11afSPaul Mackerras 		code = FPE_FLTDIV;
144414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
144514cf11afSPaul Mackerras 		code = FPE_FLTINV;
144614cf11afSPaul Mackerras 	}
144714cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
144814cf11afSPaul Mackerras 		code = FPE_FLTRES;
144914cf11afSPaul Mackerras 
14506a800f36SLiu Yu 	err = do_spe_mathemu(regs);
14516a800f36SLiu Yu 	if (err == 0) {
14526a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14536a800f36SLiu Yu 		emulate_single_step(regs);
145414cf11afSPaul Mackerras 		return;
145514cf11afSPaul Mackerras 	}
14566a800f36SLiu Yu 
14576a800f36SLiu Yu 	if (err == -EFAULT) {
14586a800f36SLiu Yu 		/* got an error reading the instruction */
14596a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14606a800f36SLiu Yu 	} else if (err == -EINVAL) {
14616a800f36SLiu Yu 		/* didn't recognize the instruction */
14626a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14636a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14646a800f36SLiu Yu 	} else {
14656a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14666a800f36SLiu Yu 	}
14676a800f36SLiu Yu 
14686a800f36SLiu Yu 	return;
14696a800f36SLiu Yu }
14706a800f36SLiu Yu 
14716a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
14726a800f36SLiu Yu {
14736a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
14746a800f36SLiu Yu 	int err;
14756a800f36SLiu Yu 
14766a800f36SLiu Yu 	preempt_disable();
14776a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
14786a800f36SLiu Yu 		giveup_spe(current);
14796a800f36SLiu Yu 	preempt_enable();
14806a800f36SLiu Yu 
14816a800f36SLiu Yu 	regs->nip -= 4;
14826a800f36SLiu Yu 	err = speround_handler(regs);
14836a800f36SLiu Yu 	if (err == 0) {
14846a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14856a800f36SLiu Yu 		emulate_single_step(regs);
14866a800f36SLiu Yu 		return;
14876a800f36SLiu Yu 	}
14886a800f36SLiu Yu 
14896a800f36SLiu Yu 	if (err == -EFAULT) {
14906a800f36SLiu Yu 		/* got an error reading the instruction */
14916a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14926a800f36SLiu Yu 	} else if (err == -EINVAL) {
14936a800f36SLiu Yu 		/* didn't recognize the instruction */
14946a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14956a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14966a800f36SLiu Yu 	} else {
14976a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
14986a800f36SLiu Yu 		return;
14996a800f36SLiu Yu 	}
15006a800f36SLiu Yu }
150114cf11afSPaul Mackerras #endif
150214cf11afSPaul Mackerras 
1503dc1c1ca3SStephen Rothwell /*
1504dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1505dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1506dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1507dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1508dc1c1ca3SStephen Rothwell  */
1509dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1510dc1c1ca3SStephen Rothwell {
1511dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1512dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1513dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1514dc1c1ca3SStephen Rothwell }
1515dc1c1ca3SStephen Rothwell 
151614cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
151714cf11afSPaul Mackerras /*
151814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
151914cf11afSPaul Mackerras  * spins until a reboot occurs
152014cf11afSPaul Mackerras  */
152114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
152214cf11afSPaul Mackerras {
152314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
152414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
152514cf11afSPaul Mackerras 	return;
152614cf11afSPaul Mackerras }
152714cf11afSPaul Mackerras 
152814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
152914cf11afSPaul Mackerras {
153014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
153114cf11afSPaul Mackerras 	WatchdogHandler(regs);
153214cf11afSPaul Mackerras }
153314cf11afSPaul Mackerras #endif
1534dc1c1ca3SStephen Rothwell 
1535dc1c1ca3SStephen Rothwell /*
1536dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1537dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1538dc1c1ca3SStephen Rothwell  */
1539dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1540dc1c1ca3SStephen Rothwell {
1541dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1542dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1543dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1544dc1c1ca3SStephen Rothwell }
154514cf11afSPaul Mackerras 
154614cf11afSPaul Mackerras void __init trap_init(void)
154714cf11afSPaul Mackerras {
154814cf11afSPaul Mackerras }
154980947e7cSGeert Uytterhoeven 
155080947e7cSGeert Uytterhoeven 
155180947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
155280947e7cSGeert Uytterhoeven 
155380947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
155480947e7cSGeert Uytterhoeven 
155580947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
155680947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
155780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
155880947e7cSGeert Uytterhoeven #endif
155980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
156080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
156180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
156280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
156380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
156480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
156580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
156680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
156780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
156880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
156980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
157080947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
157180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
157280947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
157380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
157480947e7cSGeert Uytterhoeven #endif
157580947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
157680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
157780947e7cSGeert Uytterhoeven #endif
1578efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1579efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1580efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1581efcac658SAlexey Kardashevskiy #endif
158280947e7cSGeert Uytterhoeven };
158380947e7cSGeert Uytterhoeven 
158480947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
158580947e7cSGeert Uytterhoeven 
158680947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
158780947e7cSGeert Uytterhoeven {
158876462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
158980947e7cSGeert Uytterhoeven 			    type);
159080947e7cSGeert Uytterhoeven }
159180947e7cSGeert Uytterhoeven 
159280947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
159380947e7cSGeert Uytterhoeven {
159480947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
159580947e7cSGeert Uytterhoeven 	unsigned int i;
159680947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
159780947e7cSGeert Uytterhoeven 
159880947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
159980947e7cSGeert Uytterhoeven 		return -ENODEV;
160080947e7cSGeert Uytterhoeven 
160180947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
160280947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
160380947e7cSGeert Uytterhoeven 	if (!dir)
160480947e7cSGeert Uytterhoeven 		return -ENOMEM;
160580947e7cSGeert Uytterhoeven 
160680947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
160780947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
160880947e7cSGeert Uytterhoeven 	if (!d)
160980947e7cSGeert Uytterhoeven 		goto fail;
161080947e7cSGeert Uytterhoeven 
161180947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
161280947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
161380947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
161480947e7cSGeert Uytterhoeven 		if (!d)
161580947e7cSGeert Uytterhoeven 			goto fail;
161680947e7cSGeert Uytterhoeven 	}
161780947e7cSGeert Uytterhoeven 
161880947e7cSGeert Uytterhoeven 	return 0;
161980947e7cSGeert Uytterhoeven 
162080947e7cSGeert Uytterhoeven fail:
162180947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
162280947e7cSGeert Uytterhoeven 	return -ENOMEM;
162380947e7cSGeert Uytterhoeven }
162480947e7cSGeert Uytterhoeven 
162580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
162680947e7cSGeert Uytterhoeven 
162780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1628