xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 9f2f79e3a3c19ae745d0439d6e0eed31df28de3c)
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/system.h>
4314cf11afSPaul Mackerras #include <asm/io.h>
4486417780SPaul Mackerras #include <asm/machdep.h>
4586417780SPaul Mackerras #include <asm/rtas.h>
46f7f6f4feSDavid Gibson #include <asm/pmc.h>
47dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4814cf11afSPaul Mackerras #include <asm/reg.h>
4986417780SPaul Mackerras #endif
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
56dc1c1ca3SStephen Rothwell #endif
57c0ce7d08SDavid Wilder #include <asm/kexec.h>
5816c57b36SKumar Gala #include <asm/ppc-opcode.h>
59cce1f106SShaohui Xie #include <asm/rio.h>
60ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
61dc1c1ca3SStephen Rothwell 
627dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
635be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
645be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
655be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
695be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7014cf11afSPaul Mackerras 
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7814cf11afSPaul Mackerras #endif
7914cf11afSPaul Mackerras 
8014cf11afSPaul Mackerras /*
8114cf11afSPaul Mackerras  * Trap & Exception support
8214cf11afSPaul Mackerras  */
8314cf11afSPaul Mackerras 
846031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
856031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
866031d9d9Santon@samba.org {
876031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
886031d9d9Santon@samba.org 	if (pmac_backlight) {
896031d9d9Santon@samba.org 		struct backlight_properties *props;
906031d9d9Santon@samba.org 
916031d9d9Santon@samba.org 		props = &pmac_backlight->props;
926031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
936031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
946031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
956031d9d9Santon@samba.org 	}
966031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
976031d9d9Santon@samba.org }
986031d9d9Santon@samba.org #else
996031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1006031d9d9Santon@samba.org #endif
1016031d9d9Santon@samba.org 
102760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
103760ca4dcSAnton Blanchard static int die_owner = -1;
104760ca4dcSAnton Blanchard static unsigned int die_nest_count;
105c0ce7d08SDavid Wilder static int die_counter;
106760ca4dcSAnton Blanchard 
107760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
108760ca4dcSAnton Blanchard {
109760ca4dcSAnton Blanchard 	int cpu;
11034c2a14fSanton@samba.org 	unsigned long flags;
11114cf11afSPaul Mackerras 
11214cf11afSPaul Mackerras 	if (debugger(regs))
11314cf11afSPaul Mackerras 		return 1;
11414cf11afSPaul Mackerras 
115293e4688Santon@samba.org 	oops_enter();
116293e4688Santon@samba.org 
117760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
118760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
119760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
120760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
121760ca4dcSAnton Blanchard 		if (cpu == die_owner)
122760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
123760ca4dcSAnton Blanchard 		else
124760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
125760ca4dcSAnton Blanchard 	}
126760ca4dcSAnton Blanchard 	die_nest_count++;
127760ca4dcSAnton Blanchard 	die_owner = cpu;
12814cf11afSPaul Mackerras 	console_verbose();
12914cf11afSPaul Mackerras 	bust_spinlocks(1);
1306031d9d9Santon@samba.org 	if (machine_is(powermac))
1316031d9d9Santon@samba.org 		pmac_backlight_unblank();
132760ca4dcSAnton Blanchard 	return flags;
13334c2a14fSanton@samba.org }
1345474c120SMichael Hanselmann 
135760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
136760ca4dcSAnton Blanchard 			       int signr)
137760ca4dcSAnton Blanchard {
13814cf11afSPaul Mackerras 	bust_spinlocks(0);
139760ca4dcSAnton Blanchard 	die_owner = -1;
140bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
141760ca4dcSAnton Blanchard 	die_nest_count--;
14258154c8cSAnton Blanchard 	oops_exit();
14358154c8cSAnton Blanchard 	printk("\n");
144760ca4dcSAnton Blanchard 	if (!die_nest_count)
145760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
146760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
147760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
148cc532915SMichael Ellerman 
149ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
150ebaeb5aeSMahesh Salgaonkar 
1519b00ac06SAnton Blanchard 	/*
1529b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1539b00ac06SAnton Blanchard 	 * it through the crashdump code.
1549b00ac06SAnton Blanchard 	 */
1559b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
156cc532915SMichael Ellerman 		crash_kexec(regs);
1579b00ac06SAnton Blanchard 
1589b00ac06SAnton Blanchard 		/*
1599b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1609b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1619b00ac06SAnton Blanchard 		 * code.
1629b00ac06SAnton Blanchard 		 */
163c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1649b00ac06SAnton Blanchard 	}
16514cf11afSPaul Mackerras 
166760ca4dcSAnton Blanchard 	if (!signr)
167760ca4dcSAnton Blanchard 		return;
168760ca4dcSAnton Blanchard 
16958154c8cSAnton Blanchard 	/*
17058154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
17158154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
17258154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
17358154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
17458154c8cSAnton Blanchard 	 */
17558154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
17658154c8cSAnton Blanchard 	    is_global_init(current)) {
17758154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
17858154c8cSAnton Blanchard 	}
17958154c8cSAnton Blanchard 
18014cf11afSPaul Mackerras 	if (in_interrupt())
18114cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
182cea6a4baSHorms 	if (panic_on_oops)
183012c437dSHorms 		panic("Fatal exception");
184760ca4dcSAnton Blanchard 	do_exit(signr);
185760ca4dcSAnton Blanchard }
186cea6a4baSHorms 
187760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
188760ca4dcSAnton Blanchard {
189760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
190760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
191760ca4dcSAnton Blanchard 	printk("PREEMPT ");
192760ca4dcSAnton Blanchard #endif
193760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
194760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
195760ca4dcSAnton Blanchard #endif
196760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
197760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
198760ca4dcSAnton Blanchard #endif
199760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
200760ca4dcSAnton Blanchard 	printk("NUMA ");
201760ca4dcSAnton Blanchard #endif
202760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
203760ca4dcSAnton Blanchard 
204760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
205760ca4dcSAnton Blanchard 		return 1;
206760ca4dcSAnton Blanchard 
207760ca4dcSAnton Blanchard 	print_modules();
208760ca4dcSAnton Blanchard 	show_regs(regs);
20914cf11afSPaul Mackerras 
21014cf11afSPaul Mackerras 	return 0;
21114cf11afSPaul Mackerras }
21214cf11afSPaul Mackerras 
213760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
214760ca4dcSAnton Blanchard {
215760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
216760ca4dcSAnton Blanchard 
217760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
218760ca4dcSAnton Blanchard 		err = 0;
219760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
220760ca4dcSAnton Blanchard }
221760ca4dcSAnton Blanchard 
22225baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
22325baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
22425baa35bSOleg Nesterov {
22525baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
22625baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
22725baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
22825baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
22925baa35bSOleg Nesterov }
23025baa35bSOleg Nesterov 
23114cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
23214cf11afSPaul Mackerras {
23314cf11afSPaul Mackerras 	siginfo_t info;
234d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
235d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
236d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
237d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
23814cf11afSPaul Mackerras 
23914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
240760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
24114cf11afSPaul Mackerras 		return;
242760ca4dcSAnton Blanchard 	}
243760ca4dcSAnton Blanchard 
244760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
24576462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
246d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
247d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
24814cf11afSPaul Mackerras 	}
24914cf11afSPaul Mackerras 
250*9f2f79e3SBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
251*9f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
252*9f2f79e3SBenjamin Herrenschmidt 
25314cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
25414cf11afSPaul Mackerras 	info.si_signo = signr;
25514cf11afSPaul Mackerras 	info.si_code = code;
25614cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
25714cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
25814cf11afSPaul Mackerras }
25914cf11afSPaul Mackerras 
26014cf11afSPaul Mackerras #ifdef CONFIG_PPC64
26114cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
26214cf11afSPaul Mackerras {
26314cf11afSPaul Mackerras 	/* See if any machine dependent calls */
264c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
265c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
266c902be71SArnd Bergmann 			return;
267c902be71SArnd Bergmann 	}
26814cf11afSPaul Mackerras 
2698dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
27014cf11afSPaul Mackerras 
27114cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
27214cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
27314cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
27414cf11afSPaul Mackerras 
27514cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
27614cf11afSPaul Mackerras }
27714cf11afSPaul Mackerras #endif
27814cf11afSPaul Mackerras 
27914cf11afSPaul Mackerras /*
28014cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
28114cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
28214cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
28314cf11afSPaul Mackerras  * table.
28414cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
28514cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
28614cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
28714cf11afSPaul Mackerras  *  -- paulus.
28814cf11afSPaul Mackerras  */
28914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
29014cf11afSPaul Mackerras {
29168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
29214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
29314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
29414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
29514cf11afSPaul Mackerras 
29614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
29714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
29814cf11afSPaul Mackerras 		/*
29914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
30014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
30114cf11afSPaul Mackerras 		 * As the address is in the exception table
30214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
30314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
30414cf11afSPaul Mackerras 		 * load or store.
30514cf11afSPaul Mackerras 		 */
30614cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
30714cf11afSPaul Mackerras 			nip -= 2;
30814cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
30914cf11afSPaul Mackerras 			--nip;
31014cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
31114cf11afSPaul Mackerras 			/* sync or twi */
31214cf11afSPaul Mackerras 			unsigned int rb;
31314cf11afSPaul Mackerras 
31414cf11afSPaul Mackerras 			--nip;
31514cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
31614cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
31714cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
31814cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
31914cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
32014cf11afSPaul Mackerras 			regs->nip = entry->fixup;
32114cf11afSPaul Mackerras 			return 1;
32214cf11afSPaul Mackerras 		}
32314cf11afSPaul Mackerras 	}
32468a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
32514cf11afSPaul Mackerras 	return 0;
32614cf11afSPaul Mackerras }
32714cf11afSPaul Mackerras 
328172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
32914cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
33014cf11afSPaul Mackerras    is in the ESR. */
33114cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
33214cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
33314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
33414cf11afSPaul Mackerras #else
335fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
33614cf11afSPaul Mackerras #endif
33714cf11afSPaul Mackerras #define REASON_FP		ESR_FP
33814cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
33914cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
34014cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
34114cf11afSPaul Mackerras 
34214cf11afSPaul Mackerras /* single-step stuff */
34314cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
34414cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
34514cf11afSPaul Mackerras 
34614cf11afSPaul Mackerras #else
34714cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
34814cf11afSPaul Mackerras    exception is in the MSR. */
34914cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
35014cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
35114cf11afSPaul Mackerras #define REASON_FP		0x100000
35214cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
35314cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
35414cf11afSPaul Mackerras #define REASON_TRAP		0x20000
35514cf11afSPaul Mackerras 
35614cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
35714cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
35814cf11afSPaul Mackerras #endif
35914cf11afSPaul Mackerras 
36047c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
36147c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
36214cf11afSPaul Mackerras {
3631a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
36414cf11afSPaul Mackerras 
36514cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
36614cf11afSPaul Mackerras 		printk("Instruction");
36714cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
36814cf11afSPaul Mackerras 	} else
36914cf11afSPaul Mackerras 		printk("Data");
37014cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
37147c0bd1aSBenjamin Herrenschmidt 
37247c0bd1aSBenjamin Herrenschmidt 	return 0;
37347c0bd1aSBenjamin Herrenschmidt }
37447c0bd1aSBenjamin Herrenschmidt 
37547c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
37647c0bd1aSBenjamin Herrenschmidt {
37747c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
37847c0bd1aSBenjamin Herrenschmidt 
37914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
38014cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
38114cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
38214cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
38314cf11afSPaul Mackerras 	}
38414cf11afSPaul Mackerras 	else {
38514cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
38614cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
38714cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
38814cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
38914cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
39014cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
39114cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
39214cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
39314cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
39414cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
39514cf11afSPaul Mackerras 			flush_instruction_cache();
39614cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
39714cf11afSPaul Mackerras 		}
39814cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
39914cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
40014cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
40114cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
40214cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
40314cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
40414cf11afSPaul Mackerras 
40514cf11afSPaul Mackerras 		/* Clear MCSR */
40614cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
40714cf11afSPaul Mackerras 	}
40847c0bd1aSBenjamin Herrenschmidt 	return 0;
40947c0bd1aSBenjamin Herrenschmidt }
410fc5e7097SDave Kleikamp 
411fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
412fc5e7097SDave Kleikamp {
413fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
414fc5e7097SDave Kleikamp 	u32 mcsr;
415fc5e7097SDave Kleikamp 
416fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
417fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
418fc5e7097SDave Kleikamp 		printk(KERN_ERR
419fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
420fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
421fc5e7097SDave Kleikamp 		return 0;
422fc5e7097SDave Kleikamp 	}
423fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
424fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
425fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
426fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
427fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
428fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
429fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
430fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
431fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
432fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
433fc5e7097SDave Kleikamp 		flush_instruction_cache();
434fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
435fc5e7097SDave Kleikamp 	}
436fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
437fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
438fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
439fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
440fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
441fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
442fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
443fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
444fc5e7097SDave Kleikamp 
445fc5e7097SDave Kleikamp 	/* Clear MCSR */
446fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
447fc5e7097SDave Kleikamp 
448fc5e7097SDave Kleikamp 	return 0;
449fc5e7097SDave Kleikamp }
45014cf11afSPaul Mackerras #elif defined(CONFIG_E500)
451fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
452fe04b112SScott Wood {
453fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
454fe04b112SScott Wood 	unsigned long reason = mcsr;
455fe04b112SScott Wood 	int recoverable = 1;
456fe04b112SScott Wood 
45782a9a480SScott Wood 	if (reason & MCSR_LD) {
458cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
459cce1f106SShaohui Xie 		if (recoverable == 1)
460cce1f106SShaohui Xie 			goto silent_out;
461cce1f106SShaohui Xie 	}
462cce1f106SShaohui Xie 
463fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
464fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
465fe04b112SScott Wood 
466fe04b112SScott Wood 	if (reason & MCSR_MCP)
467fe04b112SScott Wood 		printk("Machine Check Signal\n");
468fe04b112SScott Wood 
469fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
470fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
471fe04b112SScott Wood 
472fe04b112SScott Wood 		/*
473fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
474fe04b112SScott Wood 		 */
475fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
476fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
477fe04b112SScott Wood 			;
478fe04b112SScott Wood 
479fe04b112SScott Wood 		/*
480fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
481fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
482fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
483fe04b112SScott Wood 		 */
484fe04b112SScott Wood 		reason &= ~MCSR_IF;
485fe04b112SScott Wood 	}
486fe04b112SScott Wood 
487fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
488fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
48937caf9f2SKumar Gala 
49037caf9f2SKumar Gala 		/*
49137caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
49237caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
49337caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
49437caf9f2SKumar Gala 		 */
49537caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
496fe04b112SScott Wood 			recoverable = 0;
497fe04b112SScott Wood 	}
498fe04b112SScott Wood 
499fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
500fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
501fe04b112SScott Wood 		recoverable = 0;
502fe04b112SScott Wood 	}
503fe04b112SScott Wood 
504fe04b112SScott Wood 	if (reason & MCSR_NMI)
505fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
506fe04b112SScott Wood 
507fe04b112SScott Wood 	if (reason & MCSR_IF) {
508fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
509fe04b112SScott Wood 		recoverable = 0;
510fe04b112SScott Wood 	}
511fe04b112SScott Wood 
512fe04b112SScott Wood 	if (reason & MCSR_LD) {
513fe04b112SScott Wood 		printk("Load Error Report\n");
514fe04b112SScott Wood 		recoverable = 0;
515fe04b112SScott Wood 	}
516fe04b112SScott Wood 
517fe04b112SScott Wood 	if (reason & MCSR_ST) {
518fe04b112SScott Wood 		printk("Store Error Report\n");
519fe04b112SScott Wood 		recoverable = 0;
520fe04b112SScott Wood 	}
521fe04b112SScott Wood 
522fe04b112SScott Wood 	if (reason & MCSR_LDG) {
523fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
524fe04b112SScott Wood 		recoverable = 0;
525fe04b112SScott Wood 	}
526fe04b112SScott Wood 
527fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
528fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
529fe04b112SScott Wood 
530fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
531fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
532fe04b112SScott Wood 		recoverable = 0;
533fe04b112SScott Wood 	}
534fe04b112SScott Wood 
535fe04b112SScott Wood 	if (reason & MCSR_MAV) {
536fe04b112SScott Wood 		u64 addr;
537fe04b112SScott Wood 
538fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
539fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
540fe04b112SScott Wood 
541fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
542fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
543fe04b112SScott Wood 	}
544fe04b112SScott Wood 
545cce1f106SShaohui Xie silent_out:
546fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
547fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
548fe04b112SScott Wood }
549fe04b112SScott Wood 
55047c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
55147c0bd1aSBenjamin Herrenschmidt {
55247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
55347c0bd1aSBenjamin Herrenschmidt 
554cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
555cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
556cce1f106SShaohui Xie 			return 1;
557cce1f106SShaohui Xie 	}
558cce1f106SShaohui Xie 
55914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
56014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
56114cf11afSPaul Mackerras 
56214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
56314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
56414cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
56514cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
56614cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
56714cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
56814cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
56914cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
57014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
57114cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
57214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
57314cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
57414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
57514cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
57614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
57714cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
57814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
57914cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
58014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
58114cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
58214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
58314cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
58414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
58514cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
58647c0bd1aSBenjamin Herrenschmidt 
58747c0bd1aSBenjamin Herrenschmidt 	return 0;
58847c0bd1aSBenjamin Herrenschmidt }
5894490c06bSKumar Gala 
5904490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5914490c06bSKumar Gala {
5924490c06bSKumar Gala 	return 0;
5934490c06bSKumar Gala }
59414cf11afSPaul Mackerras #elif defined(CONFIG_E200)
59547c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
59647c0bd1aSBenjamin Herrenschmidt {
59747c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
59847c0bd1aSBenjamin Herrenschmidt 
59914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
60014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
60114cf11afSPaul Mackerras 
60214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
60314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
60414cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
60514cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
60614cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
60714cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
60814cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
60914cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
61014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
61114cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
61214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
61314cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
61414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
61514cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
61647c0bd1aSBenjamin Herrenschmidt 
61747c0bd1aSBenjamin Herrenschmidt 	return 0;
61847c0bd1aSBenjamin Herrenschmidt }
61947c0bd1aSBenjamin Herrenschmidt #else
62047c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
62147c0bd1aSBenjamin Herrenschmidt {
62247c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
62347c0bd1aSBenjamin Herrenschmidt 
62414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62514cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
62614cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
62714cf11afSPaul Mackerras 	case 0x80000:
62814cf11afSPaul Mackerras 		printk("Machine check signal\n");
62914cf11afSPaul Mackerras 		break;
63014cf11afSPaul Mackerras 	case 0:		/* for 601 */
63114cf11afSPaul Mackerras 	case 0x40000:
63214cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
63314cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
63414cf11afSPaul Mackerras 		break;
63514cf11afSPaul Mackerras 	case 0x20000:
63614cf11afSPaul Mackerras 		printk("Data parity error signal\n");
63714cf11afSPaul Mackerras 		break;
63814cf11afSPaul Mackerras 	case 0x10000:
63914cf11afSPaul Mackerras 		printk("Address parity error signal\n");
64014cf11afSPaul Mackerras 		break;
64114cf11afSPaul Mackerras 	case 0x20000000:
64214cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
64314cf11afSPaul Mackerras 		break;
64414cf11afSPaul Mackerras 	case 0x40000000:
64514cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
64614cf11afSPaul Mackerras 		break;
64714cf11afSPaul Mackerras 	case 0x00100000:
64814cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
64914cf11afSPaul Mackerras 		break;
65014cf11afSPaul Mackerras 	default:
65114cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
65214cf11afSPaul Mackerras 	}
65375918a4bSOlof Johansson 	return 0;
65475918a4bSOlof Johansson }
65547c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
65675918a4bSOlof Johansson 
65775918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
65875918a4bSOlof Johansson {
65975918a4bSOlof Johansson 	int recover = 0;
66075918a4bSOlof Johansson 
66189713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
66289713ed1SAnton Blanchard 
66347c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
66447c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
66547c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
66647c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
66747c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
66847c0bd1aSBenjamin Herrenschmidt 	 */
66975918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
67075918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
67147c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
67247c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
67375918a4bSOlof Johansson 
67447c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
67575918a4bSOlof Johansson 		return;
67675918a4bSOlof Johansson 
67775918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
67847c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
67947c0bd1aSBenjamin Herrenschmidt 	 *
68047c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
68147c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
68247c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
68347c0bd1aSBenjamin Herrenschmidt 	 */
68475918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
68575918a4bSOlof Johansson 	return;
68675918a4bSOlof Johansson #endif
68775918a4bSOlof Johansson 
688a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
68975918a4bSOlof Johansson 		return;
69075918a4bSOlof Johansson 
69175918a4bSOlof Johansson 	if (check_io_access(regs))
69275918a4bSOlof Johansson 		return;
69375918a4bSOlof Johansson 
6948dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
69514cf11afSPaul Mackerras 
69614cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
69714cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
69814cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
69914cf11afSPaul Mackerras }
70014cf11afSPaul Mackerras 
70114cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
70214cf11afSPaul Mackerras {
70314cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
70414cf11afSPaul Mackerras }
70514cf11afSPaul Mackerras 
706dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
70714cf11afSPaul Mackerras {
70814cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
70914cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
71014cf11afSPaul Mackerras 
71114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
71214cf11afSPaul Mackerras }
71314cf11afSPaul Mackerras 
714dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
71514cf11afSPaul Mackerras {
71614cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
71714cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
71814cf11afSPaul Mackerras 		return;
71914cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
72014cf11afSPaul Mackerras 		return;
72114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
72214cf11afSPaul Mackerras }
72314cf11afSPaul Mackerras 
72414cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
72514cf11afSPaul Mackerras {
72614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
72714cf11afSPaul Mackerras }
72814cf11afSPaul Mackerras 
7298dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
73014cf11afSPaul Mackerras {
7312538c2d0SK.Prasad 	clear_single_step(regs);
73214cf11afSPaul Mackerras 
73314cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
73414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
73514cf11afSPaul Mackerras 		return;
73614cf11afSPaul Mackerras 	if (debugger_sstep(regs))
73714cf11afSPaul Mackerras 		return;
73814cf11afSPaul Mackerras 
73914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
74014cf11afSPaul Mackerras }
74114cf11afSPaul Mackerras 
74214cf11afSPaul Mackerras /*
74314cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
74414cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
74514cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
74614cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
74714cf11afSPaul Mackerras  */
7488dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
74914cf11afSPaul Mackerras {
7502538c2d0SK.Prasad 	if (single_stepping(regs))
7512538c2d0SK.Prasad 		single_step_exception(regs);
75214cf11afSPaul Mackerras }
75314cf11afSPaul Mackerras 
7545fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
755dc1c1ca3SStephen Rothwell {
7565fad293bSKumar Gala 	int ret = 0;
757dc1c1ca3SStephen Rothwell 
758dc1c1ca3SStephen Rothwell 	/* Invalid operation */
759dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7605fad293bSKumar Gala 		ret = FPE_FLTINV;
761dc1c1ca3SStephen Rothwell 
762dc1c1ca3SStephen Rothwell 	/* Overflow */
763dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7645fad293bSKumar Gala 		ret = FPE_FLTOVF;
765dc1c1ca3SStephen Rothwell 
766dc1c1ca3SStephen Rothwell 	/* Underflow */
767dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7685fad293bSKumar Gala 		ret = FPE_FLTUND;
769dc1c1ca3SStephen Rothwell 
770dc1c1ca3SStephen Rothwell 	/* Divide by zero */
771dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7725fad293bSKumar Gala 		ret = FPE_FLTDIV;
773dc1c1ca3SStephen Rothwell 
774dc1c1ca3SStephen Rothwell 	/* Inexact result */
775dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7765fad293bSKumar Gala 		ret = FPE_FLTRES;
7775fad293bSKumar Gala 
7785fad293bSKumar Gala 	return ret;
7795fad293bSKumar Gala }
7805fad293bSKumar Gala 
7815fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7825fad293bSKumar Gala {
7835fad293bSKumar Gala 	int code = 0;
7845fad293bSKumar Gala 
7855fad293bSKumar Gala 	flush_fp_to_thread(current);
7865fad293bSKumar Gala 
7875fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
788dc1c1ca3SStephen Rothwell 
789dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
790dc1c1ca3SStephen Rothwell }
791dc1c1ca3SStephen Rothwell 
792dc1c1ca3SStephen Rothwell /*
793dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
79414cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
79514cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
79614cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
79714cf11afSPaul Mackerras  *
79814cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
79914cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
80014cf11afSPaul Mackerras  * bits is faster and easier.
80186417780SPaul Mackerras  *
80214cf11afSPaul Mackerras  */
80314cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
80414cf11afSPaul Mackerras {
80514cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
80614cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
80714cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
80814cf11afSPaul Mackerras 	u32 num_bytes;
80914cf11afSPaul Mackerras 	unsigned long EA;
81014cf11afSPaul Mackerras 	int pos = 0;
81114cf11afSPaul Mackerras 
81214cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
81316c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
81414cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
81514cf11afSPaul Mackerras 			return -EINVAL;
81614cf11afSPaul Mackerras 
81714cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
81814cf11afSPaul Mackerras 
81916c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
82016c57b36SKumar Gala 		case PPC_INST_LSWX:
82116c57b36SKumar Gala 		case PPC_INST_STSWX:
82214cf11afSPaul Mackerras 			EA += NB_RB;
82314cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
82414cf11afSPaul Mackerras 			break;
82516c57b36SKumar Gala 		case PPC_INST_LSWI:
82616c57b36SKumar Gala 		case PPC_INST_STSWI:
82714cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
82814cf11afSPaul Mackerras 			break;
82914cf11afSPaul Mackerras 		default:
83014cf11afSPaul Mackerras 			return -EINVAL;
83114cf11afSPaul Mackerras 	}
83214cf11afSPaul Mackerras 
83314cf11afSPaul Mackerras 	while (num_bytes != 0)
83414cf11afSPaul Mackerras 	{
83514cf11afSPaul Mackerras 		u8 val;
83614cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
83714cf11afSPaul Mackerras 
83816c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
83916c57b36SKumar Gala 			case PPC_INST_LSWX:
84016c57b36SKumar Gala 			case PPC_INST_LSWI:
84114cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
84214cf11afSPaul Mackerras 					return -EFAULT;
84314cf11afSPaul Mackerras 				/* first time updating this reg,
84414cf11afSPaul Mackerras 				 * zero it out */
84514cf11afSPaul Mackerras 				if (pos == 0)
84614cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
84714cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
84814cf11afSPaul Mackerras 				break;
84916c57b36SKumar Gala 			case PPC_INST_STSWI:
85016c57b36SKumar Gala 			case PPC_INST_STSWX:
85114cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
85214cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
85314cf11afSPaul Mackerras 					return -EFAULT;
85414cf11afSPaul Mackerras 				break;
85514cf11afSPaul Mackerras 		}
85614cf11afSPaul Mackerras 		/* move EA to next address */
85714cf11afSPaul Mackerras 		EA += 1;
85814cf11afSPaul Mackerras 		num_bytes--;
85914cf11afSPaul Mackerras 
86014cf11afSPaul Mackerras 		/* manage our position within the register */
86114cf11afSPaul Mackerras 		if (++pos == 4) {
86214cf11afSPaul Mackerras 			pos = 0;
86314cf11afSPaul Mackerras 			if (++rT == 32)
86414cf11afSPaul Mackerras 				rT = 0;
86514cf11afSPaul Mackerras 		}
86614cf11afSPaul Mackerras 	}
86714cf11afSPaul Mackerras 
86814cf11afSPaul Mackerras 	return 0;
86914cf11afSPaul Mackerras }
87014cf11afSPaul Mackerras 
871c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
872c3412dcbSWill Schmidt {
873c3412dcbSWill Schmidt 	u32 ra,rs;
874c3412dcbSWill Schmidt 	unsigned long tmp;
875c3412dcbSWill Schmidt 
876c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
877c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
878c3412dcbSWill Schmidt 
879c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
880c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
881c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
882c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
883c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
884c3412dcbSWill Schmidt 
885c3412dcbSWill Schmidt 	return 0;
886c3412dcbSWill Schmidt }
887c3412dcbSWill Schmidt 
888c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
889c1469f13SKumar Gala {
890c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
891c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
892c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
893c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
894c1469f13SKumar Gala 	u8 bit;
895c1469f13SKumar Gala 	unsigned long tmp;
896c1469f13SKumar Gala 
897c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
898c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
899c1469f13SKumar Gala 
900c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
901c1469f13SKumar Gala 
902c1469f13SKumar Gala 	return 0;
903c1469f13SKumar Gala }
904c1469f13SKumar Gala 
90514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
90614cf11afSPaul Mackerras {
90714cf11afSPaul Mackerras 	u32 instword;
90814cf11afSPaul Mackerras 	u32 rd;
90914cf11afSPaul Mackerras 
910fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
91114cf11afSPaul Mackerras 		return -EINVAL;
91214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
91314cf11afSPaul Mackerras 
91414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
91514cf11afSPaul Mackerras 		return -EFAULT;
91614cf11afSPaul Mackerras 
91714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
91816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
919eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
92014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
92114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
92214cf11afSPaul Mackerras 		return 0;
92314cf11afSPaul Mackerras 	}
92414cf11afSPaul Mackerras 
92514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
92680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
927eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
92814cf11afSPaul Mackerras 		return 0;
92980947e7cSGeert Uytterhoeven 	}
93014cf11afSPaul Mackerras 
93114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
93216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
93386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
93414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
93514cf11afSPaul Mackerras 
936eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
93714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
93814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
93914cf11afSPaul Mackerras 		return 0;
94014cf11afSPaul Mackerras 	}
94114cf11afSPaul Mackerras 
94214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
94380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
944eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
94514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
94680947e7cSGeert Uytterhoeven 	}
94714cf11afSPaul Mackerras 
948c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
94916c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
950eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
951c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
952c3412dcbSWill Schmidt 	}
953c3412dcbSWill Schmidt 
954c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
95516c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
956eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
957c1469f13SKumar Gala 		return emulate_isel(regs, instword);
958c1469f13SKumar Gala 	}
959c1469f13SKumar Gala 
960efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
961efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
962efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
963efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
964efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
965efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
966efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
967efcac658SAlexey Kardashevskiy 		return 0;
968efcac658SAlexey Kardashevskiy 	}
969efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
970efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
971efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
972efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
973efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
974efcac658SAlexey Kardashevskiy 		mtspr(SPRN_DSCR, regs->gpr[rd]);
975efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
976efcac658SAlexey Kardashevskiy 		return 0;
977efcac658SAlexey Kardashevskiy 	}
978efcac658SAlexey Kardashevskiy #endif
979efcac658SAlexey Kardashevskiy 
98014cf11afSPaul Mackerras 	return -EINVAL;
98114cf11afSPaul Mackerras }
98214cf11afSPaul Mackerras 
98373c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
98414cf11afSPaul Mackerras {
98573c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
98614cf11afSPaul Mackerras }
98714cf11afSPaul Mackerras 
9888dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
98914cf11afSPaul Mackerras {
99014cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
99114cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
99214cf11afSPaul Mackerras 
993aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
99404903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
99514cf11afSPaul Mackerras 
99614cf11afSPaul Mackerras 	if (reason & REASON_FP) {
99714cf11afSPaul Mackerras 		/* IEEE FP exception */
998dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
9998dad3f92SPaul Mackerras 		return;
10008dad3f92SPaul Mackerras 	}
10018dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1002ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1003ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1004ba797b28SJason Wessel 		if (debugger_bpt(regs))
1005ba797b28SJason Wessel 			return;
1006ba797b28SJason Wessel 
100714cf11afSPaul Mackerras 		/* trap exception */
1008dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1009dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1010dc1c1ca3SStephen Rothwell 			return;
101173c9ceabSJeremy Fitzhardinge 
101273c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1013608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
101414cf11afSPaul Mackerras 			regs->nip += 4;
101514cf11afSPaul Mackerras 			return;
101614cf11afSPaul Mackerras 		}
10178dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10188dad3f92SPaul Mackerras 		return;
10198dad3f92SPaul Mackerras 	}
10208dad3f92SPaul Mackerras 
1021cd8a5673SPaul Mackerras 	local_irq_enable();
1022cd8a5673SPaul Mackerras 
102304903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
102404903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
102504903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
102604903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
102704903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
102804903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
102904903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
103025985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10315fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10325fad293bSKumar Gala 	case 0:
103304903a30SKumar Gala 		emulate_single_step(regs);
103404903a30SKumar Gala 		return;
10355fad293bSKumar Gala 	case 1: {
10365fad293bSKumar Gala 			int code = 0;
10375fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10385fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10395fad293bSKumar Gala 			return;
104004903a30SKumar Gala 		}
10415fad293bSKumar Gala 	case -EFAULT:
10425fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10435fad293bSKumar Gala 		return;
10445fad293bSKumar Gala 	}
10455fad293bSKumar Gala 	/* fall through on any other errors */
104604903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
104704903a30SKumar Gala 
10488dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
10498dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
105014cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
105114cf11afSPaul Mackerras 		case 0:
105214cf11afSPaul Mackerras 			regs->nip += 4;
105314cf11afSPaul Mackerras 			emulate_single_step(regs);
10548dad3f92SPaul Mackerras 			return;
105514cf11afSPaul Mackerras 		case -EFAULT:
105614cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10578dad3f92SPaul Mackerras 			return;
10588dad3f92SPaul Mackerras 		}
10598dad3f92SPaul Mackerras 	}
10608dad3f92SPaul Mackerras 
106114cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
106214cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
106314cf11afSPaul Mackerras 	else
106414cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
106514cf11afSPaul Mackerras }
106614cf11afSPaul Mackerras 
1067dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
106814cf11afSPaul Mackerras {
10694393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
107014cf11afSPaul Mackerras 
1071e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1072e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
107314cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
107414cf11afSPaul Mackerras 
107514cf11afSPaul Mackerras 	if (fixed == 1) {
107614cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
107714cf11afSPaul Mackerras 		emulate_single_step(regs);
107814cf11afSPaul Mackerras 		return;
107914cf11afSPaul Mackerras 	}
108014cf11afSPaul Mackerras 
108114cf11afSPaul Mackerras 	/* Operand address was bad */
108214cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
10834393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
10844393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
10854393c4f6SBenjamin Herrenschmidt 	} else {
10864393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
10874393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
108814cf11afSPaul Mackerras 	}
10894393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
10904393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
10914393c4f6SBenjamin Herrenschmidt 	else
10924393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
109314cf11afSPaul Mackerras }
109414cf11afSPaul Mackerras 
109514cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
109614cf11afSPaul Mackerras {
109714cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
109814cf11afSPaul Mackerras 	       current, regs->gpr[1]);
109914cf11afSPaul Mackerras 	debugger(regs);
110014cf11afSPaul Mackerras 	show_regs(regs);
110114cf11afSPaul Mackerras 	panic("kernel stack overflow");
110214cf11afSPaul Mackerras }
110314cf11afSPaul Mackerras 
110414cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
110514cf11afSPaul Mackerras {
110614cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
110714cf11afSPaul Mackerras 	       regs->nip, regs->msr);
110814cf11afSPaul Mackerras 	debugger(regs);
110914cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
111014cf11afSPaul Mackerras }
111114cf11afSPaul Mackerras 
111214cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
111314cf11afSPaul Mackerras {
111414cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
111519c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
111614cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
111714cf11afSPaul Mackerras }
111814cf11afSPaul Mackerras 
1119dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1120dc1c1ca3SStephen Rothwell {
1121dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1122dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1123dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1124dc1c1ca3SStephen Rothwell }
1125dc1c1ca3SStephen Rothwell 
1126dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1127dc1c1ca3SStephen Rothwell {
1128dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1129dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1130dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1131dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1132dc1c1ca3SStephen Rothwell 		return;
1133dc1c1ca3SStephen Rothwell 	}
11346c4841c2SAnton Blanchard 
1135dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1136dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1137dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1138dc1c1ca3SStephen Rothwell }
1139dc1c1ca3SStephen Rothwell 
1140ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1141ce48b210SMichael Neuling {
1142ce48b210SMichael Neuling 	if (user_mode(regs)) {
1143ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1144ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1145ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1146ce48b210SMichael Neuling 		return;
1147ce48b210SMichael Neuling 	}
1148ce48b210SMichael Neuling 
1149ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1150ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1151ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1152ce48b210SMichael Neuling }
1153ce48b210SMichael Neuling 
1154dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1155dc1c1ca3SStephen Rothwell {
115689713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
115789713ed1SAnton Blanchard 
1158dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1159dc1c1ca3SStephen Rothwell }
1160dc1c1ca3SStephen Rothwell 
11618dad3f92SPaul Mackerras #ifdef CONFIG_8xx
116214cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
116314cf11afSPaul Mackerras {
116414cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
116514cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
11665dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
116714cf11afSPaul Mackerras 	int errcode;
11685dd57a13SScott Wood #endif
116914cf11afSPaul Mackerras 
117014cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
117114cf11afSPaul Mackerras 
117214cf11afSPaul Mackerras 	if (!user_mode(regs)) {
117314cf11afSPaul Mackerras 		debugger(regs);
117414cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
117514cf11afSPaul Mackerras 	}
117614cf11afSPaul Mackerras 
117714cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
117814cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
117980947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1180eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
11815fad293bSKumar Gala 
11825fad293bSKumar Gala 	switch (errcode) {
11835fad293bSKumar Gala 	case 0:
11845fad293bSKumar Gala 		emulate_single_step(regs);
11855fad293bSKumar Gala 		return;
11865fad293bSKumar Gala 	case 1: {
11875fad293bSKumar Gala 			int code = 0;
11885fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11895fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
11905fad293bSKumar Gala 			return;
11915fad293bSKumar Gala 		}
11925fad293bSKumar Gala 	case -EFAULT:
11935fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11945fad293bSKumar Gala 		return;
11955fad293bSKumar Gala 	default:
11965fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11975fad293bSKumar Gala 		return;
11985fad293bSKumar Gala 	}
11995fad293bSKumar Gala 
12005dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
120114cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
120280947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1203eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
120480947e7cSGeert Uytterhoeven 
12055fad293bSKumar Gala 	switch (errcode) {
12065fad293bSKumar Gala 	case 0:
120714cf11afSPaul Mackerras 		emulate_single_step(regs);
12085fad293bSKumar Gala 		return;
12095fad293bSKumar Gala 	case 1:
12105fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12115fad293bSKumar Gala 		return;
12125fad293bSKumar Gala 	case -EFAULT:
12135fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12145fad293bSKumar Gala 		return;
12155fad293bSKumar Gala 	}
12165dd57a13SScott Wood #else
12175dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12185fad293bSKumar Gala #endif
121914cf11afSPaul Mackerras }
12208dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
122114cf11afSPaul Mackerras 
1222172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12233bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
12243bffb652SDave Kleikamp {
12253bffb652SDave Kleikamp 	int changed = 0;
12263bffb652SDave Kleikamp 	/*
12273bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
12283bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
12293bffb652SDave Kleikamp 	 */
12303bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
12313bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
12323bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
12333bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
12343bffb652SDave Kleikamp #endif
12353bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
12363bffb652SDave Kleikamp 			     5);
12373bffb652SDave Kleikamp 		changed |= 0x01;
12383bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
12393bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
12403bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
12413bffb652SDave Kleikamp 			     6);
12423bffb652SDave Kleikamp 		changed |= 0x01;
12433bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
12443bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
12453bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
12463bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
12473bffb652SDave Kleikamp 			     1);
12483bffb652SDave Kleikamp 		changed |= 0x01;
12493bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
12503bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
12513bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
12523bffb652SDave Kleikamp 			     2);
12533bffb652SDave Kleikamp 		changed |= 0x01;
12543bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
12553bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
12563bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
12573bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
12583bffb652SDave Kleikamp 			     3);
12593bffb652SDave Kleikamp 		changed |= 0x01;
12603bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12613bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
12623bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
12633bffb652SDave Kleikamp 			     4);
12643bffb652SDave Kleikamp 		changed |= 0x01;
12653bffb652SDave Kleikamp 	}
12663bffb652SDave Kleikamp 	/*
12673bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
12683bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
12693bffb652SDave Kleikamp 	 * back on or not.
12703bffb652SDave Kleikamp 	 */
12713bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
12723bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
12733bffb652SDave Kleikamp 	else
12743bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
12753bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
12763bffb652SDave Kleikamp 
12773bffb652SDave Kleikamp 	if (changed & 0x01)
12783bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
12793bffb652SDave Kleikamp }
128014cf11afSPaul Mackerras 
1281f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
128214cf11afSPaul Mackerras {
12833bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
12843bffb652SDave Kleikamp 
1285ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1286ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1287ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1288ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1289ec097c84SRoland McGrath 	 */
1290ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1291ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1292ec097c84SRoland McGrath 
1293ec097c84SRoland McGrath 		/* Disable BT */
1294ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1295ec097c84SRoland McGrath 		/* Clear the BT event */
1296ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1297ec097c84SRoland McGrath 
1298ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1299ec097c84SRoland McGrath 		if (user_mode(regs)) {
1300ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1301ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1302ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1303ec097c84SRoland McGrath 			return;
1304ec097c84SRoland McGrath 		}
1305ec097c84SRoland McGrath 
1306ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1307ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1308ec097c84SRoland McGrath 			return;
1309ec097c84SRoland McGrath 		}
1310ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1311ec097c84SRoland McGrath 			return;
1312ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
131314cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1314f8279621SKumar Gala 
131514cf11afSPaul Mackerras 		/* Disable instruction completion */
131614cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
131714cf11afSPaul Mackerras 		/* Clear the instruction completion event */
131814cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1319f8279621SKumar Gala 
1320f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1321f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
132214cf11afSPaul Mackerras 			return;
132314cf11afSPaul Mackerras 		}
1324f8279621SKumar Gala 
1325f8279621SKumar Gala 		if (debugger_sstep(regs))
1326f8279621SKumar Gala 			return;
1327f8279621SKumar Gala 
13283bffb652SDave Kleikamp 		if (user_mode(regs)) {
13293bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
13303bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
13313bffb652SDave Kleikamp 					       current->thread.dbcr1))
13323bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
13333bffb652SDave Kleikamp 			else
13343bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
13353bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
13363bffb652SDave Kleikamp 		}
1337f8279621SKumar Gala 
1338f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
13393bffb652SDave Kleikamp 	} else
13403bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
134114cf11afSPaul Mackerras }
1342172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
134314cf11afSPaul Mackerras 
134414cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
134514cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
134614cf11afSPaul Mackerras {
134714cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
134814cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
134914cf11afSPaul Mackerras }
135014cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
135114cf11afSPaul Mackerras 
135214cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1353dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
135414cf11afSPaul Mackerras {
135514cf11afSPaul Mackerras 	int err;
135614cf11afSPaul Mackerras 
135714cf11afSPaul Mackerras 	if (!user_mode(regs)) {
135814cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
135914cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13608dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
136114cf11afSPaul Mackerras 	}
136214cf11afSPaul Mackerras 
1363dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1364dc1c1ca3SStephen Rothwell 
1365eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
136614cf11afSPaul Mackerras 	err = emulate_altivec(regs);
136714cf11afSPaul Mackerras 	if (err == 0) {
136814cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
136914cf11afSPaul Mackerras 		emulate_single_step(regs);
137014cf11afSPaul Mackerras 		return;
137114cf11afSPaul Mackerras 	}
137214cf11afSPaul Mackerras 
137314cf11afSPaul Mackerras 	if (err == -EFAULT) {
137414cf11afSPaul Mackerras 		/* got an error reading the instruction */
137514cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
137614cf11afSPaul Mackerras 	} else {
137714cf11afSPaul Mackerras 		/* didn't recognize the instruction */
137814cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
137976462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
138014cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
138114cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
138214cf11afSPaul Mackerras 	}
138314cf11afSPaul Mackerras }
138414cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
138514cf11afSPaul Mackerras 
1386ce48b210SMichael Neuling #ifdef CONFIG_VSX
1387ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1388ce48b210SMichael Neuling {
1389ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1390ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1391ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1392ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1393ce48b210SMichael Neuling 	}
1394ce48b210SMichael Neuling 
1395ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1396ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1397ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1398ce48b210SMichael Neuling }
1399ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1400ce48b210SMichael Neuling 
140114cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
140214cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
140314cf11afSPaul Mackerras 			   unsigned long error_code)
140414cf11afSPaul Mackerras {
140514cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
140614cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
140714cf11afSPaul Mackerras 	 * something smarter
140814cf11afSPaul Mackerras 	 */
140914cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
141014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
141114cf11afSPaul Mackerras 	return;
141214cf11afSPaul Mackerras }
141314cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
141414cf11afSPaul Mackerras 
141514cf11afSPaul Mackerras #ifdef CONFIG_SPE
141614cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
141714cf11afSPaul Mackerras {
14186a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
141914cf11afSPaul Mackerras 	unsigned long spefscr;
142014cf11afSPaul Mackerras 	int fpexc_mode;
142114cf11afSPaul Mackerras 	int code = 0;
14226a800f36SLiu Yu 	int err;
14236a800f36SLiu Yu 
1424685659eeSyu liu 	flush_spe_to_thread(current);
142514cf11afSPaul Mackerras 
142614cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
142714cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
142814cf11afSPaul Mackerras 
142914cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
143014cf11afSPaul Mackerras 		code = FPE_FLTOVF;
143114cf11afSPaul Mackerras 	}
143214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
143314cf11afSPaul Mackerras 		code = FPE_FLTUND;
143414cf11afSPaul Mackerras 	}
143514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
143614cf11afSPaul Mackerras 		code = FPE_FLTDIV;
143714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
143814cf11afSPaul Mackerras 		code = FPE_FLTINV;
143914cf11afSPaul Mackerras 	}
144014cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
144114cf11afSPaul Mackerras 		code = FPE_FLTRES;
144214cf11afSPaul Mackerras 
14436a800f36SLiu Yu 	err = do_spe_mathemu(regs);
14446a800f36SLiu Yu 	if (err == 0) {
14456a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14466a800f36SLiu Yu 		emulate_single_step(regs);
144714cf11afSPaul Mackerras 		return;
144814cf11afSPaul Mackerras 	}
14496a800f36SLiu Yu 
14506a800f36SLiu Yu 	if (err == -EFAULT) {
14516a800f36SLiu Yu 		/* got an error reading the instruction */
14526a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14536a800f36SLiu Yu 	} else if (err == -EINVAL) {
14546a800f36SLiu Yu 		/* didn't recognize the instruction */
14556a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14566a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14576a800f36SLiu Yu 	} else {
14586a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14596a800f36SLiu Yu 	}
14606a800f36SLiu Yu 
14616a800f36SLiu Yu 	return;
14626a800f36SLiu Yu }
14636a800f36SLiu Yu 
14646a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
14656a800f36SLiu Yu {
14666a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
14676a800f36SLiu Yu 	int err;
14686a800f36SLiu Yu 
14696a800f36SLiu Yu 	preempt_disable();
14706a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
14716a800f36SLiu Yu 		giveup_spe(current);
14726a800f36SLiu Yu 	preempt_enable();
14736a800f36SLiu Yu 
14746a800f36SLiu Yu 	regs->nip -= 4;
14756a800f36SLiu Yu 	err = speround_handler(regs);
14766a800f36SLiu Yu 	if (err == 0) {
14776a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14786a800f36SLiu Yu 		emulate_single_step(regs);
14796a800f36SLiu Yu 		return;
14806a800f36SLiu Yu 	}
14816a800f36SLiu Yu 
14826a800f36SLiu Yu 	if (err == -EFAULT) {
14836a800f36SLiu Yu 		/* got an error reading the instruction */
14846a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14856a800f36SLiu Yu 	} else if (err == -EINVAL) {
14866a800f36SLiu Yu 		/* didn't recognize the instruction */
14876a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14886a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14896a800f36SLiu Yu 	} else {
14906a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
14916a800f36SLiu Yu 		return;
14926a800f36SLiu Yu 	}
14936a800f36SLiu Yu }
149414cf11afSPaul Mackerras #endif
149514cf11afSPaul Mackerras 
1496dc1c1ca3SStephen Rothwell /*
1497dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1498dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1499dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1500dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1501dc1c1ca3SStephen Rothwell  */
1502dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1503dc1c1ca3SStephen Rothwell {
1504dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1505dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1506dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1507dc1c1ca3SStephen Rothwell }
1508dc1c1ca3SStephen Rothwell 
150914cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
151014cf11afSPaul Mackerras /*
151114cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
151214cf11afSPaul Mackerras  * spins until a reboot occurs
151314cf11afSPaul Mackerras  */
151414cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
151514cf11afSPaul Mackerras {
151614cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
151714cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
151814cf11afSPaul Mackerras 	return;
151914cf11afSPaul Mackerras }
152014cf11afSPaul Mackerras 
152114cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
152214cf11afSPaul Mackerras {
152314cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
152414cf11afSPaul Mackerras 	WatchdogHandler(regs);
152514cf11afSPaul Mackerras }
152614cf11afSPaul Mackerras #endif
1527dc1c1ca3SStephen Rothwell 
1528dc1c1ca3SStephen Rothwell /*
1529dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1530dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1531dc1c1ca3SStephen Rothwell  */
1532dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1533dc1c1ca3SStephen Rothwell {
1534dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1535dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1536dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1537dc1c1ca3SStephen Rothwell }
153814cf11afSPaul Mackerras 
153914cf11afSPaul Mackerras void __init trap_init(void)
154014cf11afSPaul Mackerras {
154114cf11afSPaul Mackerras }
154280947e7cSGeert Uytterhoeven 
154380947e7cSGeert Uytterhoeven 
154480947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
154580947e7cSGeert Uytterhoeven 
154680947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
154780947e7cSGeert Uytterhoeven 
154880947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
154980947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
155080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
155180947e7cSGeert Uytterhoeven #endif
155280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
155380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
155480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
155580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
155680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
155780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
155880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
155980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
156080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
156180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
156280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
156380947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
156480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
156580947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
156680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
156780947e7cSGeert Uytterhoeven #endif
156880947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
156980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
157080947e7cSGeert Uytterhoeven #endif
1571efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1572efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1573efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1574efcac658SAlexey Kardashevskiy #endif
157580947e7cSGeert Uytterhoeven };
157680947e7cSGeert Uytterhoeven 
157780947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
157880947e7cSGeert Uytterhoeven 
157980947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
158080947e7cSGeert Uytterhoeven {
158176462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
158280947e7cSGeert Uytterhoeven 			    type);
158380947e7cSGeert Uytterhoeven }
158480947e7cSGeert Uytterhoeven 
158580947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
158680947e7cSGeert Uytterhoeven {
158780947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
158880947e7cSGeert Uytterhoeven 	unsigned int i;
158980947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
159080947e7cSGeert Uytterhoeven 
159180947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
159280947e7cSGeert Uytterhoeven 		return -ENODEV;
159380947e7cSGeert Uytterhoeven 
159480947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
159580947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
159680947e7cSGeert Uytterhoeven 	if (!dir)
159780947e7cSGeert Uytterhoeven 		return -ENOMEM;
159880947e7cSGeert Uytterhoeven 
159980947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
160080947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
160180947e7cSGeert Uytterhoeven 	if (!d)
160280947e7cSGeert Uytterhoeven 		goto fail;
160380947e7cSGeert Uytterhoeven 
160480947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
160580947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
160680947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
160780947e7cSGeert Uytterhoeven 		if (!d)
160880947e7cSGeert Uytterhoeven 			goto fail;
160980947e7cSGeert Uytterhoeven 	}
161080947e7cSGeert Uytterhoeven 
161180947e7cSGeert Uytterhoeven 	return 0;
161280947e7cSGeert Uytterhoeven 
161380947e7cSGeert Uytterhoeven fail:
161480947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
161580947e7cSGeert Uytterhoeven 	return -ENOMEM;
161680947e7cSGeert Uytterhoeven }
161780947e7cSGeert Uytterhoeven 
161880947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
161980947e7cSGeert Uytterhoeven 
162080947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1621