xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision ebaeb5ae24379b5b635dc1d1fa6df904bc95b4d9)
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>
60*ebaeb5aeSMahesh 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 
149*ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
150*ebaeb5aeSMahesh 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 
25014cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
25114cf11afSPaul Mackerras 	info.si_signo = signr;
25214cf11afSPaul Mackerras 	info.si_code = code;
25314cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
25414cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
25514cf11afSPaul Mackerras }
25614cf11afSPaul Mackerras 
25714cf11afSPaul Mackerras #ifdef CONFIG_PPC64
25814cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
25914cf11afSPaul Mackerras {
26014cf11afSPaul Mackerras 	/* See if any machine dependent calls */
261c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
262c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
263c902be71SArnd Bergmann 			return;
264c902be71SArnd Bergmann 	}
26514cf11afSPaul Mackerras 
2668dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
26714cf11afSPaul Mackerras 
26814cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
26914cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
27014cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
27114cf11afSPaul Mackerras 
27214cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
27314cf11afSPaul Mackerras }
27414cf11afSPaul Mackerras #endif
27514cf11afSPaul Mackerras 
27614cf11afSPaul Mackerras /*
27714cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
27814cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
27914cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
28014cf11afSPaul Mackerras  * table.
28114cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
28214cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
28314cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
28414cf11afSPaul Mackerras  *  -- paulus.
28514cf11afSPaul Mackerras  */
28614cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
28714cf11afSPaul Mackerras {
28868a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
28914cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
29014cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
29114cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
29214cf11afSPaul Mackerras 
29314cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
29414cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
29514cf11afSPaul Mackerras 		/*
29614cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
29714cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
29814cf11afSPaul Mackerras 		 * As the address is in the exception table
29914cf11afSPaul Mackerras 		 * we should be able to read the instr there.
30014cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
30114cf11afSPaul Mackerras 		 * load or store.
30214cf11afSPaul Mackerras 		 */
30314cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
30414cf11afSPaul Mackerras 			nip -= 2;
30514cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
30614cf11afSPaul Mackerras 			--nip;
30714cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
30814cf11afSPaul Mackerras 			/* sync or twi */
30914cf11afSPaul Mackerras 			unsigned int rb;
31014cf11afSPaul Mackerras 
31114cf11afSPaul Mackerras 			--nip;
31214cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
31314cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
31414cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
31514cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
31614cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
31714cf11afSPaul Mackerras 			regs->nip = entry->fixup;
31814cf11afSPaul Mackerras 			return 1;
31914cf11afSPaul Mackerras 		}
32014cf11afSPaul Mackerras 	}
32168a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
32214cf11afSPaul Mackerras 	return 0;
32314cf11afSPaul Mackerras }
32414cf11afSPaul Mackerras 
325172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
32614cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
32714cf11afSPaul Mackerras    is in the ESR. */
32814cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
32914cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
33014cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
33114cf11afSPaul Mackerras #else
332fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
33314cf11afSPaul Mackerras #endif
33414cf11afSPaul Mackerras #define REASON_FP		ESR_FP
33514cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
33614cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
33714cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
33814cf11afSPaul Mackerras 
33914cf11afSPaul Mackerras /* single-step stuff */
34014cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
34114cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
34214cf11afSPaul Mackerras 
34314cf11afSPaul Mackerras #else
34414cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
34514cf11afSPaul Mackerras    exception is in the MSR. */
34614cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
34714cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
34814cf11afSPaul Mackerras #define REASON_FP		0x100000
34914cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
35014cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
35114cf11afSPaul Mackerras #define REASON_TRAP		0x20000
35214cf11afSPaul Mackerras 
35314cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
35414cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
35514cf11afSPaul Mackerras #endif
35614cf11afSPaul Mackerras 
35747c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
35847c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
35914cf11afSPaul Mackerras {
3601a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
36114cf11afSPaul Mackerras 
36214cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
36314cf11afSPaul Mackerras 		printk("Instruction");
36414cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
36514cf11afSPaul Mackerras 	} else
36614cf11afSPaul Mackerras 		printk("Data");
36714cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
36847c0bd1aSBenjamin Herrenschmidt 
36947c0bd1aSBenjamin Herrenschmidt 	return 0;
37047c0bd1aSBenjamin Herrenschmidt }
37147c0bd1aSBenjamin Herrenschmidt 
37247c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
37347c0bd1aSBenjamin Herrenschmidt {
37447c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
37547c0bd1aSBenjamin Herrenschmidt 
37614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
37714cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
37814cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
37914cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
38014cf11afSPaul Mackerras 	}
38114cf11afSPaul Mackerras 	else {
38214cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
38314cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
38414cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
38514cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
38614cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
38714cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
38814cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
38914cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
39014cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
39114cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
39214cf11afSPaul Mackerras 			flush_instruction_cache();
39314cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
39414cf11afSPaul Mackerras 		}
39514cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
39614cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
39714cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
39814cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
39914cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
40014cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
40114cf11afSPaul Mackerras 
40214cf11afSPaul Mackerras 		/* Clear MCSR */
40314cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
40414cf11afSPaul Mackerras 	}
40547c0bd1aSBenjamin Herrenschmidt 	return 0;
40647c0bd1aSBenjamin Herrenschmidt }
407fc5e7097SDave Kleikamp 
408fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
409fc5e7097SDave Kleikamp {
410fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
411fc5e7097SDave Kleikamp 	u32 mcsr;
412fc5e7097SDave Kleikamp 
413fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
414fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
415fc5e7097SDave Kleikamp 		printk(KERN_ERR
416fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
417fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
418fc5e7097SDave Kleikamp 		return 0;
419fc5e7097SDave Kleikamp 	}
420fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
421fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
422fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
423fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
424fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
425fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
426fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
427fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
428fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
429fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
430fc5e7097SDave Kleikamp 		flush_instruction_cache();
431fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
432fc5e7097SDave Kleikamp 	}
433fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
434fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
435fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
436fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
437fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
438fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
439fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
440fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
441fc5e7097SDave Kleikamp 
442fc5e7097SDave Kleikamp 	/* Clear MCSR */
443fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
444fc5e7097SDave Kleikamp 
445fc5e7097SDave Kleikamp 	return 0;
446fc5e7097SDave Kleikamp }
44714cf11afSPaul Mackerras #elif defined(CONFIG_E500)
448fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
449fe04b112SScott Wood {
450fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
451fe04b112SScott Wood 	unsigned long reason = mcsr;
452fe04b112SScott Wood 	int recoverable = 1;
453fe04b112SScott Wood 
45482a9a480SScott Wood 	if (reason & MCSR_LD) {
455cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
456cce1f106SShaohui Xie 		if (recoverable == 1)
457cce1f106SShaohui Xie 			goto silent_out;
458cce1f106SShaohui Xie 	}
459cce1f106SShaohui Xie 
460fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
461fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
462fe04b112SScott Wood 
463fe04b112SScott Wood 	if (reason & MCSR_MCP)
464fe04b112SScott Wood 		printk("Machine Check Signal\n");
465fe04b112SScott Wood 
466fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
467fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
468fe04b112SScott Wood 
469fe04b112SScott Wood 		/*
470fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
471fe04b112SScott Wood 		 */
472fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
473fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
474fe04b112SScott Wood 			;
475fe04b112SScott Wood 
476fe04b112SScott Wood 		/*
477fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
478fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
479fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
480fe04b112SScott Wood 		 */
481fe04b112SScott Wood 		reason &= ~MCSR_IF;
482fe04b112SScott Wood 	}
483fe04b112SScott Wood 
484fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
485fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
48637caf9f2SKumar Gala 
48737caf9f2SKumar Gala 		/*
48837caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
48937caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
49037caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
49137caf9f2SKumar Gala 		 */
49237caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
493fe04b112SScott Wood 			recoverable = 0;
494fe04b112SScott Wood 	}
495fe04b112SScott Wood 
496fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
497fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
498fe04b112SScott Wood 		recoverable = 0;
499fe04b112SScott Wood 	}
500fe04b112SScott Wood 
501fe04b112SScott Wood 	if (reason & MCSR_NMI)
502fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
503fe04b112SScott Wood 
504fe04b112SScott Wood 	if (reason & MCSR_IF) {
505fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
506fe04b112SScott Wood 		recoverable = 0;
507fe04b112SScott Wood 	}
508fe04b112SScott Wood 
509fe04b112SScott Wood 	if (reason & MCSR_LD) {
510fe04b112SScott Wood 		printk("Load Error Report\n");
511fe04b112SScott Wood 		recoverable = 0;
512fe04b112SScott Wood 	}
513fe04b112SScott Wood 
514fe04b112SScott Wood 	if (reason & MCSR_ST) {
515fe04b112SScott Wood 		printk("Store Error Report\n");
516fe04b112SScott Wood 		recoverable = 0;
517fe04b112SScott Wood 	}
518fe04b112SScott Wood 
519fe04b112SScott Wood 	if (reason & MCSR_LDG) {
520fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
521fe04b112SScott Wood 		recoverable = 0;
522fe04b112SScott Wood 	}
523fe04b112SScott Wood 
524fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
525fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
526fe04b112SScott Wood 
527fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
528fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
529fe04b112SScott Wood 		recoverable = 0;
530fe04b112SScott Wood 	}
531fe04b112SScott Wood 
532fe04b112SScott Wood 	if (reason & MCSR_MAV) {
533fe04b112SScott Wood 		u64 addr;
534fe04b112SScott Wood 
535fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
536fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
537fe04b112SScott Wood 
538fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
539fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
540fe04b112SScott Wood 	}
541fe04b112SScott Wood 
542cce1f106SShaohui Xie silent_out:
543fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
544fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
545fe04b112SScott Wood }
546fe04b112SScott Wood 
54747c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
54847c0bd1aSBenjamin Herrenschmidt {
54947c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
55047c0bd1aSBenjamin Herrenschmidt 
551cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
552cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
553cce1f106SShaohui Xie 			return 1;
554cce1f106SShaohui Xie 	}
555cce1f106SShaohui Xie 
55614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
55714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
55814cf11afSPaul Mackerras 
55914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
56014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
56114cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
56214cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
56314cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
56414cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
56514cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
56614cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
56714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
56814cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
56914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
57014cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
57114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
57214cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
57314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
57414cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
57514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
57614cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
57714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
57814cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
57914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
58014cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
58114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
58214cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
58347c0bd1aSBenjamin Herrenschmidt 
58447c0bd1aSBenjamin Herrenschmidt 	return 0;
58547c0bd1aSBenjamin Herrenschmidt }
5864490c06bSKumar Gala 
5874490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5884490c06bSKumar Gala {
5894490c06bSKumar Gala 	return 0;
5904490c06bSKumar Gala }
59114cf11afSPaul Mackerras #elif defined(CONFIG_E200)
59247c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
59347c0bd1aSBenjamin Herrenschmidt {
59447c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
59547c0bd1aSBenjamin Herrenschmidt 
59614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
59714cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
59814cf11afSPaul Mackerras 
59914cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
60014cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
60114cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
60214cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
60314cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
60414cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
60514cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
60614cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
60714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
60814cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
60914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
61014cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
61114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
61214cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
61347c0bd1aSBenjamin Herrenschmidt 
61447c0bd1aSBenjamin Herrenschmidt 	return 0;
61547c0bd1aSBenjamin Herrenschmidt }
61647c0bd1aSBenjamin Herrenschmidt #else
61747c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
61847c0bd1aSBenjamin Herrenschmidt {
61947c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
62047c0bd1aSBenjamin Herrenschmidt 
62114cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
62214cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
62314cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
62414cf11afSPaul Mackerras 	case 0x80000:
62514cf11afSPaul Mackerras 		printk("Machine check signal\n");
62614cf11afSPaul Mackerras 		break;
62714cf11afSPaul Mackerras 	case 0:		/* for 601 */
62814cf11afSPaul Mackerras 	case 0x40000:
62914cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
63014cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
63114cf11afSPaul Mackerras 		break;
63214cf11afSPaul Mackerras 	case 0x20000:
63314cf11afSPaul Mackerras 		printk("Data parity error signal\n");
63414cf11afSPaul Mackerras 		break;
63514cf11afSPaul Mackerras 	case 0x10000:
63614cf11afSPaul Mackerras 		printk("Address parity error signal\n");
63714cf11afSPaul Mackerras 		break;
63814cf11afSPaul Mackerras 	case 0x20000000:
63914cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
64014cf11afSPaul Mackerras 		break;
64114cf11afSPaul Mackerras 	case 0x40000000:
64214cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
64314cf11afSPaul Mackerras 		break;
64414cf11afSPaul Mackerras 	case 0x00100000:
64514cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
64614cf11afSPaul Mackerras 		break;
64714cf11afSPaul Mackerras 	default:
64814cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
64914cf11afSPaul Mackerras 	}
65075918a4bSOlof Johansson 	return 0;
65175918a4bSOlof Johansson }
65247c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
65375918a4bSOlof Johansson 
65475918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
65575918a4bSOlof Johansson {
65675918a4bSOlof Johansson 	int recover = 0;
65775918a4bSOlof Johansson 
65889713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
65989713ed1SAnton Blanchard 
66047c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
66147c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
66247c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
66347c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
66447c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
66547c0bd1aSBenjamin Herrenschmidt 	 */
66675918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
66775918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
66847c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
66947c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
67075918a4bSOlof Johansson 
67147c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
67275918a4bSOlof Johansson 		return;
67375918a4bSOlof Johansson 
67475918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
67547c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
67647c0bd1aSBenjamin Herrenschmidt 	 *
67747c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
67847c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
67947c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
68047c0bd1aSBenjamin Herrenschmidt 	 */
68175918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
68275918a4bSOlof Johansson 	return;
68375918a4bSOlof Johansson #endif
68475918a4bSOlof Johansson 
685a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
68675918a4bSOlof Johansson 		return;
68775918a4bSOlof Johansson 
68875918a4bSOlof Johansson 	if (check_io_access(regs))
68975918a4bSOlof Johansson 		return;
69075918a4bSOlof Johansson 
6918dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
69214cf11afSPaul Mackerras 
69314cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
69414cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
69514cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
69614cf11afSPaul Mackerras }
69714cf11afSPaul Mackerras 
69814cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
69914cf11afSPaul Mackerras {
70014cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
70114cf11afSPaul Mackerras }
70214cf11afSPaul Mackerras 
703dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
70414cf11afSPaul Mackerras {
70514cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
70614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
70714cf11afSPaul Mackerras 
70814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
70914cf11afSPaul Mackerras }
71014cf11afSPaul Mackerras 
711dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
71214cf11afSPaul Mackerras {
71314cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
71414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
71514cf11afSPaul Mackerras 		return;
71614cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
71714cf11afSPaul Mackerras 		return;
71814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
71914cf11afSPaul Mackerras }
72014cf11afSPaul Mackerras 
72114cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
72214cf11afSPaul Mackerras {
72314cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
72414cf11afSPaul Mackerras }
72514cf11afSPaul Mackerras 
7268dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
72714cf11afSPaul Mackerras {
7282538c2d0SK.Prasad 	clear_single_step(regs);
72914cf11afSPaul Mackerras 
73014cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
73114cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
73214cf11afSPaul Mackerras 		return;
73314cf11afSPaul Mackerras 	if (debugger_sstep(regs))
73414cf11afSPaul Mackerras 		return;
73514cf11afSPaul Mackerras 
73614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
73714cf11afSPaul Mackerras }
73814cf11afSPaul Mackerras 
73914cf11afSPaul Mackerras /*
74014cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
74114cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
74214cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
74314cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
74414cf11afSPaul Mackerras  */
7458dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
74614cf11afSPaul Mackerras {
7472538c2d0SK.Prasad 	if (single_stepping(regs))
7482538c2d0SK.Prasad 		single_step_exception(regs);
74914cf11afSPaul Mackerras }
75014cf11afSPaul Mackerras 
7515fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
752dc1c1ca3SStephen Rothwell {
7535fad293bSKumar Gala 	int ret = 0;
754dc1c1ca3SStephen Rothwell 
755dc1c1ca3SStephen Rothwell 	/* Invalid operation */
756dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7575fad293bSKumar Gala 		ret = FPE_FLTINV;
758dc1c1ca3SStephen Rothwell 
759dc1c1ca3SStephen Rothwell 	/* Overflow */
760dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7615fad293bSKumar Gala 		ret = FPE_FLTOVF;
762dc1c1ca3SStephen Rothwell 
763dc1c1ca3SStephen Rothwell 	/* Underflow */
764dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7655fad293bSKumar Gala 		ret = FPE_FLTUND;
766dc1c1ca3SStephen Rothwell 
767dc1c1ca3SStephen Rothwell 	/* Divide by zero */
768dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7695fad293bSKumar Gala 		ret = FPE_FLTDIV;
770dc1c1ca3SStephen Rothwell 
771dc1c1ca3SStephen Rothwell 	/* Inexact result */
772dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7735fad293bSKumar Gala 		ret = FPE_FLTRES;
7745fad293bSKumar Gala 
7755fad293bSKumar Gala 	return ret;
7765fad293bSKumar Gala }
7775fad293bSKumar Gala 
7785fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7795fad293bSKumar Gala {
7805fad293bSKumar Gala 	int code = 0;
7815fad293bSKumar Gala 
7825fad293bSKumar Gala 	flush_fp_to_thread(current);
7835fad293bSKumar Gala 
7845fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
785dc1c1ca3SStephen Rothwell 
786dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
787dc1c1ca3SStephen Rothwell }
788dc1c1ca3SStephen Rothwell 
789dc1c1ca3SStephen Rothwell /*
790dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
79114cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
79214cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
79314cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
79414cf11afSPaul Mackerras  *
79514cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
79614cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
79714cf11afSPaul Mackerras  * bits is faster and easier.
79886417780SPaul Mackerras  *
79914cf11afSPaul Mackerras  */
80014cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
80114cf11afSPaul Mackerras {
80214cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
80314cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
80414cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
80514cf11afSPaul Mackerras 	u32 num_bytes;
80614cf11afSPaul Mackerras 	unsigned long EA;
80714cf11afSPaul Mackerras 	int pos = 0;
80814cf11afSPaul Mackerras 
80914cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
81016c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
81114cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
81214cf11afSPaul Mackerras 			return -EINVAL;
81314cf11afSPaul Mackerras 
81414cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
81514cf11afSPaul Mackerras 
81616c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
81716c57b36SKumar Gala 		case PPC_INST_LSWX:
81816c57b36SKumar Gala 		case PPC_INST_STSWX:
81914cf11afSPaul Mackerras 			EA += NB_RB;
82014cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
82114cf11afSPaul Mackerras 			break;
82216c57b36SKumar Gala 		case PPC_INST_LSWI:
82316c57b36SKumar Gala 		case PPC_INST_STSWI:
82414cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
82514cf11afSPaul Mackerras 			break;
82614cf11afSPaul Mackerras 		default:
82714cf11afSPaul Mackerras 			return -EINVAL;
82814cf11afSPaul Mackerras 	}
82914cf11afSPaul Mackerras 
83014cf11afSPaul Mackerras 	while (num_bytes != 0)
83114cf11afSPaul Mackerras 	{
83214cf11afSPaul Mackerras 		u8 val;
83314cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
83414cf11afSPaul Mackerras 
83516c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
83616c57b36SKumar Gala 			case PPC_INST_LSWX:
83716c57b36SKumar Gala 			case PPC_INST_LSWI:
83814cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
83914cf11afSPaul Mackerras 					return -EFAULT;
84014cf11afSPaul Mackerras 				/* first time updating this reg,
84114cf11afSPaul Mackerras 				 * zero it out */
84214cf11afSPaul Mackerras 				if (pos == 0)
84314cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
84414cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
84514cf11afSPaul Mackerras 				break;
84616c57b36SKumar Gala 			case PPC_INST_STSWI:
84716c57b36SKumar Gala 			case PPC_INST_STSWX:
84814cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
84914cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
85014cf11afSPaul Mackerras 					return -EFAULT;
85114cf11afSPaul Mackerras 				break;
85214cf11afSPaul Mackerras 		}
85314cf11afSPaul Mackerras 		/* move EA to next address */
85414cf11afSPaul Mackerras 		EA += 1;
85514cf11afSPaul Mackerras 		num_bytes--;
85614cf11afSPaul Mackerras 
85714cf11afSPaul Mackerras 		/* manage our position within the register */
85814cf11afSPaul Mackerras 		if (++pos == 4) {
85914cf11afSPaul Mackerras 			pos = 0;
86014cf11afSPaul Mackerras 			if (++rT == 32)
86114cf11afSPaul Mackerras 				rT = 0;
86214cf11afSPaul Mackerras 		}
86314cf11afSPaul Mackerras 	}
86414cf11afSPaul Mackerras 
86514cf11afSPaul Mackerras 	return 0;
86614cf11afSPaul Mackerras }
86714cf11afSPaul Mackerras 
868c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
869c3412dcbSWill Schmidt {
870c3412dcbSWill Schmidt 	u32 ra,rs;
871c3412dcbSWill Schmidt 	unsigned long tmp;
872c3412dcbSWill Schmidt 
873c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
874c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
875c3412dcbSWill Schmidt 
876c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
877c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
878c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
879c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
880c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
881c3412dcbSWill Schmidt 
882c3412dcbSWill Schmidt 	return 0;
883c3412dcbSWill Schmidt }
884c3412dcbSWill Schmidt 
885c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
886c1469f13SKumar Gala {
887c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
888c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
889c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
890c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
891c1469f13SKumar Gala 	u8 bit;
892c1469f13SKumar Gala 	unsigned long tmp;
893c1469f13SKumar Gala 
894c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
895c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
896c1469f13SKumar Gala 
897c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
898c1469f13SKumar Gala 
899c1469f13SKumar Gala 	return 0;
900c1469f13SKumar Gala }
901c1469f13SKumar Gala 
90214cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
90314cf11afSPaul Mackerras {
90414cf11afSPaul Mackerras 	u32 instword;
90514cf11afSPaul Mackerras 	u32 rd;
90614cf11afSPaul Mackerras 
907fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
90814cf11afSPaul Mackerras 		return -EINVAL;
90914cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
91014cf11afSPaul Mackerras 
91114cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
91214cf11afSPaul Mackerras 		return -EFAULT;
91314cf11afSPaul Mackerras 
91414cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
91516c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
916eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
91714cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
91814cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
91914cf11afSPaul Mackerras 		return 0;
92014cf11afSPaul Mackerras 	}
92114cf11afSPaul Mackerras 
92214cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
92380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
924eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
92514cf11afSPaul Mackerras 		return 0;
92680947e7cSGeert Uytterhoeven 	}
92714cf11afSPaul Mackerras 
92814cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
92916c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
93086417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
93114cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
93214cf11afSPaul Mackerras 
933eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
93414cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
93514cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
93614cf11afSPaul Mackerras 		return 0;
93714cf11afSPaul Mackerras 	}
93814cf11afSPaul Mackerras 
93914cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
94080947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
941eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
94214cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
94380947e7cSGeert Uytterhoeven 	}
94414cf11afSPaul Mackerras 
945c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
94616c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
947eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
948c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
949c3412dcbSWill Schmidt 	}
950c3412dcbSWill Schmidt 
951c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
95216c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
953eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
954c1469f13SKumar Gala 		return emulate_isel(regs, instword);
955c1469f13SKumar Gala 	}
956c1469f13SKumar Gala 
957efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
958efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
959efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
960efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
961efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
962efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
963efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
964efcac658SAlexey Kardashevskiy 		return 0;
965efcac658SAlexey Kardashevskiy 	}
966efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
967efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
968efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
969efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
970efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
971efcac658SAlexey Kardashevskiy 		mtspr(SPRN_DSCR, regs->gpr[rd]);
972efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
973efcac658SAlexey Kardashevskiy 		return 0;
974efcac658SAlexey Kardashevskiy 	}
975efcac658SAlexey Kardashevskiy #endif
976efcac658SAlexey Kardashevskiy 
97714cf11afSPaul Mackerras 	return -EINVAL;
97814cf11afSPaul Mackerras }
97914cf11afSPaul Mackerras 
98073c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
98114cf11afSPaul Mackerras {
98273c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
98314cf11afSPaul Mackerras }
98414cf11afSPaul Mackerras 
9858dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
98614cf11afSPaul Mackerras {
98714cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
98814cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
98914cf11afSPaul Mackerras 
990aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
99104903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
99214cf11afSPaul Mackerras 
99314cf11afSPaul Mackerras 	if (reason & REASON_FP) {
99414cf11afSPaul Mackerras 		/* IEEE FP exception */
995dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
9968dad3f92SPaul Mackerras 		return;
9978dad3f92SPaul Mackerras 	}
9988dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
999ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1000ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1001ba797b28SJason Wessel 		if (debugger_bpt(regs))
1002ba797b28SJason Wessel 			return;
1003ba797b28SJason Wessel 
100414cf11afSPaul Mackerras 		/* trap exception */
1005dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1006dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1007dc1c1ca3SStephen Rothwell 			return;
100873c9ceabSJeremy Fitzhardinge 
100973c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1010608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
101114cf11afSPaul Mackerras 			regs->nip += 4;
101214cf11afSPaul Mackerras 			return;
101314cf11afSPaul Mackerras 		}
10148dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10158dad3f92SPaul Mackerras 		return;
10168dad3f92SPaul Mackerras 	}
10178dad3f92SPaul Mackerras 
1018cd8a5673SPaul Mackerras 	local_irq_enable();
1019cd8a5673SPaul Mackerras 
102004903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
102104903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
102204903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
102304903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
102404903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
102504903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
102604903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
102725985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10285fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10295fad293bSKumar Gala 	case 0:
103004903a30SKumar Gala 		emulate_single_step(regs);
103104903a30SKumar Gala 		return;
10325fad293bSKumar Gala 	case 1: {
10335fad293bSKumar Gala 			int code = 0;
10345fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10355fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10365fad293bSKumar Gala 			return;
103704903a30SKumar Gala 		}
10385fad293bSKumar Gala 	case -EFAULT:
10395fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10405fad293bSKumar Gala 		return;
10415fad293bSKumar Gala 	}
10425fad293bSKumar Gala 	/* fall through on any other errors */
104304903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
104404903a30SKumar Gala 
10458dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
10468dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
104714cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
104814cf11afSPaul Mackerras 		case 0:
104914cf11afSPaul Mackerras 			regs->nip += 4;
105014cf11afSPaul Mackerras 			emulate_single_step(regs);
10518dad3f92SPaul Mackerras 			return;
105214cf11afSPaul Mackerras 		case -EFAULT:
105314cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10548dad3f92SPaul Mackerras 			return;
10558dad3f92SPaul Mackerras 		}
10568dad3f92SPaul Mackerras 	}
10578dad3f92SPaul Mackerras 
105814cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
105914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
106014cf11afSPaul Mackerras 	else
106114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
106214cf11afSPaul Mackerras }
106314cf11afSPaul Mackerras 
1064dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
106514cf11afSPaul Mackerras {
10664393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
106714cf11afSPaul Mackerras 
1068e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1069e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
107014cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
107114cf11afSPaul Mackerras 
107214cf11afSPaul Mackerras 	if (fixed == 1) {
107314cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
107414cf11afSPaul Mackerras 		emulate_single_step(regs);
107514cf11afSPaul Mackerras 		return;
107614cf11afSPaul Mackerras 	}
107714cf11afSPaul Mackerras 
107814cf11afSPaul Mackerras 	/* Operand address was bad */
107914cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
10804393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
10814393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
10824393c4f6SBenjamin Herrenschmidt 	} else {
10834393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
10844393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
108514cf11afSPaul Mackerras 	}
10864393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
10874393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
10884393c4f6SBenjamin Herrenschmidt 	else
10894393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
109014cf11afSPaul Mackerras }
109114cf11afSPaul Mackerras 
109214cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
109314cf11afSPaul Mackerras {
109414cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
109514cf11afSPaul Mackerras 	       current, regs->gpr[1]);
109614cf11afSPaul Mackerras 	debugger(regs);
109714cf11afSPaul Mackerras 	show_regs(regs);
109814cf11afSPaul Mackerras 	panic("kernel stack overflow");
109914cf11afSPaul Mackerras }
110014cf11afSPaul Mackerras 
110114cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
110214cf11afSPaul Mackerras {
110314cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
110414cf11afSPaul Mackerras 	       regs->nip, regs->msr);
110514cf11afSPaul Mackerras 	debugger(regs);
110614cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
110714cf11afSPaul Mackerras }
110814cf11afSPaul Mackerras 
110914cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
111014cf11afSPaul Mackerras {
111114cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
111219c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
111314cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
111414cf11afSPaul Mackerras }
111514cf11afSPaul Mackerras 
1116dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1117dc1c1ca3SStephen Rothwell {
1118dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1119dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1120dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1121dc1c1ca3SStephen Rothwell }
1122dc1c1ca3SStephen Rothwell 
1123dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1124dc1c1ca3SStephen Rothwell {
1125dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1126dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1127dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1128dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1129dc1c1ca3SStephen Rothwell 		return;
1130dc1c1ca3SStephen Rothwell 	}
11316c4841c2SAnton Blanchard 
1132dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1133dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1134dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1135dc1c1ca3SStephen Rothwell }
1136dc1c1ca3SStephen Rothwell 
1137ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1138ce48b210SMichael Neuling {
1139ce48b210SMichael Neuling 	if (user_mode(regs)) {
1140ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1141ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1142ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1143ce48b210SMichael Neuling 		return;
1144ce48b210SMichael Neuling 	}
1145ce48b210SMichael Neuling 
1146ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1147ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1148ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1149ce48b210SMichael Neuling }
1150ce48b210SMichael Neuling 
1151dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1152dc1c1ca3SStephen Rothwell {
115389713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
115489713ed1SAnton Blanchard 
1155dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1156dc1c1ca3SStephen Rothwell }
1157dc1c1ca3SStephen Rothwell 
11588dad3f92SPaul Mackerras #ifdef CONFIG_8xx
115914cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
116014cf11afSPaul Mackerras {
116114cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
116214cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
11635dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
116414cf11afSPaul Mackerras 	int errcode;
11655dd57a13SScott Wood #endif
116614cf11afSPaul Mackerras 
116714cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
116814cf11afSPaul Mackerras 
116914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
117014cf11afSPaul Mackerras 		debugger(regs);
117114cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
117214cf11afSPaul Mackerras 	}
117314cf11afSPaul Mackerras 
117414cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
117514cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
117680947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1177eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
11785fad293bSKumar Gala 
11795fad293bSKumar Gala 	switch (errcode) {
11805fad293bSKumar Gala 	case 0:
11815fad293bSKumar Gala 		emulate_single_step(regs);
11825fad293bSKumar Gala 		return;
11835fad293bSKumar Gala 	case 1: {
11845fad293bSKumar Gala 			int code = 0;
11855fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11865fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
11875fad293bSKumar Gala 			return;
11885fad293bSKumar Gala 		}
11895fad293bSKumar Gala 	case -EFAULT:
11905fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11915fad293bSKumar Gala 		return;
11925fad293bSKumar Gala 	default:
11935fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11945fad293bSKumar Gala 		return;
11955fad293bSKumar Gala 	}
11965fad293bSKumar Gala 
11975dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
119814cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
119980947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1200eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
120180947e7cSGeert Uytterhoeven 
12025fad293bSKumar Gala 	switch (errcode) {
12035fad293bSKumar Gala 	case 0:
120414cf11afSPaul Mackerras 		emulate_single_step(regs);
12055fad293bSKumar Gala 		return;
12065fad293bSKumar Gala 	case 1:
12075fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12085fad293bSKumar Gala 		return;
12095fad293bSKumar Gala 	case -EFAULT:
12105fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
12115fad293bSKumar Gala 		return;
12125fad293bSKumar Gala 	}
12135dd57a13SScott Wood #else
12145dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12155fad293bSKumar Gala #endif
121614cf11afSPaul Mackerras }
12178dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
121814cf11afSPaul Mackerras 
1219172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12203bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
12213bffb652SDave Kleikamp {
12223bffb652SDave Kleikamp 	int changed = 0;
12233bffb652SDave Kleikamp 	/*
12243bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
12253bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
12263bffb652SDave Kleikamp 	 */
12273bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
12283bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
12293bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
12303bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
12313bffb652SDave Kleikamp #endif
12323bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
12333bffb652SDave Kleikamp 			     5);
12343bffb652SDave Kleikamp 		changed |= 0x01;
12353bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
12363bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
12373bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
12383bffb652SDave Kleikamp 			     6);
12393bffb652SDave Kleikamp 		changed |= 0x01;
12403bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
12413bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
12423bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
12433bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
12443bffb652SDave Kleikamp 			     1);
12453bffb652SDave Kleikamp 		changed |= 0x01;
12463bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
12473bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
12483bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
12493bffb652SDave Kleikamp 			     2);
12503bffb652SDave Kleikamp 		changed |= 0x01;
12513bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
12523bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
12533bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
12543bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
12553bffb652SDave Kleikamp 			     3);
12563bffb652SDave Kleikamp 		changed |= 0x01;
12573bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12583bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
12593bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
12603bffb652SDave Kleikamp 			     4);
12613bffb652SDave Kleikamp 		changed |= 0x01;
12623bffb652SDave Kleikamp 	}
12633bffb652SDave Kleikamp 	/*
12643bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
12653bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
12663bffb652SDave Kleikamp 	 * back on or not.
12673bffb652SDave Kleikamp 	 */
12683bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
12693bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
12703bffb652SDave Kleikamp 	else
12713bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
12723bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
12733bffb652SDave Kleikamp 
12743bffb652SDave Kleikamp 	if (changed & 0x01)
12753bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
12763bffb652SDave Kleikamp }
127714cf11afSPaul Mackerras 
1278f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
127914cf11afSPaul Mackerras {
12803bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
12813bffb652SDave Kleikamp 
1282ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1283ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1284ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1285ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1286ec097c84SRoland McGrath 	 */
1287ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1288ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1289ec097c84SRoland McGrath 
1290ec097c84SRoland McGrath 		/* Disable BT */
1291ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1292ec097c84SRoland McGrath 		/* Clear the BT event */
1293ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1294ec097c84SRoland McGrath 
1295ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1296ec097c84SRoland McGrath 		if (user_mode(regs)) {
1297ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1298ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1299ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1300ec097c84SRoland McGrath 			return;
1301ec097c84SRoland McGrath 		}
1302ec097c84SRoland McGrath 
1303ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1304ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1305ec097c84SRoland McGrath 			return;
1306ec097c84SRoland McGrath 		}
1307ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1308ec097c84SRoland McGrath 			return;
1309ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
131014cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1311f8279621SKumar Gala 
131214cf11afSPaul Mackerras 		/* Disable instruction completion */
131314cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
131414cf11afSPaul Mackerras 		/* Clear the instruction completion event */
131514cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1316f8279621SKumar Gala 
1317f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1318f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
131914cf11afSPaul Mackerras 			return;
132014cf11afSPaul Mackerras 		}
1321f8279621SKumar Gala 
1322f8279621SKumar Gala 		if (debugger_sstep(regs))
1323f8279621SKumar Gala 			return;
1324f8279621SKumar Gala 
13253bffb652SDave Kleikamp 		if (user_mode(regs)) {
13263bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
13273bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
13283bffb652SDave Kleikamp 					       current->thread.dbcr1))
13293bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
13303bffb652SDave Kleikamp 			else
13313bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
13323bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
13333bffb652SDave Kleikamp 		}
1334f8279621SKumar Gala 
1335f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
13363bffb652SDave Kleikamp 	} else
13373bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
133814cf11afSPaul Mackerras }
1339172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
134014cf11afSPaul Mackerras 
134114cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
134214cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
134314cf11afSPaul Mackerras {
134414cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
134514cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
134614cf11afSPaul Mackerras }
134714cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
134814cf11afSPaul Mackerras 
134914cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1350dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
135114cf11afSPaul Mackerras {
135214cf11afSPaul Mackerras 	int err;
135314cf11afSPaul Mackerras 
135414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
135514cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
135614cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13578dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
135814cf11afSPaul Mackerras 	}
135914cf11afSPaul Mackerras 
1360dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1361dc1c1ca3SStephen Rothwell 
1362eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
136314cf11afSPaul Mackerras 	err = emulate_altivec(regs);
136414cf11afSPaul Mackerras 	if (err == 0) {
136514cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
136614cf11afSPaul Mackerras 		emulate_single_step(regs);
136714cf11afSPaul Mackerras 		return;
136814cf11afSPaul Mackerras 	}
136914cf11afSPaul Mackerras 
137014cf11afSPaul Mackerras 	if (err == -EFAULT) {
137114cf11afSPaul Mackerras 		/* got an error reading the instruction */
137214cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
137314cf11afSPaul Mackerras 	} else {
137414cf11afSPaul Mackerras 		/* didn't recognize the instruction */
137514cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
137676462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
137714cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
137814cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
137914cf11afSPaul Mackerras 	}
138014cf11afSPaul Mackerras }
138114cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
138214cf11afSPaul Mackerras 
1383ce48b210SMichael Neuling #ifdef CONFIG_VSX
1384ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1385ce48b210SMichael Neuling {
1386ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1387ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1388ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1389ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1390ce48b210SMichael Neuling 	}
1391ce48b210SMichael Neuling 
1392ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1393ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1394ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1395ce48b210SMichael Neuling }
1396ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1397ce48b210SMichael Neuling 
139814cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
139914cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
140014cf11afSPaul Mackerras 			   unsigned long error_code)
140114cf11afSPaul Mackerras {
140214cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
140314cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
140414cf11afSPaul Mackerras 	 * something smarter
140514cf11afSPaul Mackerras 	 */
140614cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
140714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
140814cf11afSPaul Mackerras 	return;
140914cf11afSPaul Mackerras }
141014cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
141114cf11afSPaul Mackerras 
141214cf11afSPaul Mackerras #ifdef CONFIG_SPE
141314cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
141414cf11afSPaul Mackerras {
14156a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
141614cf11afSPaul Mackerras 	unsigned long spefscr;
141714cf11afSPaul Mackerras 	int fpexc_mode;
141814cf11afSPaul Mackerras 	int code = 0;
14196a800f36SLiu Yu 	int err;
14206a800f36SLiu Yu 
1421685659eeSyu liu 	flush_spe_to_thread(current);
142214cf11afSPaul Mackerras 
142314cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
142414cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
142514cf11afSPaul Mackerras 
142614cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
142714cf11afSPaul Mackerras 		code = FPE_FLTOVF;
142814cf11afSPaul Mackerras 	}
142914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
143014cf11afSPaul Mackerras 		code = FPE_FLTUND;
143114cf11afSPaul Mackerras 	}
143214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
143314cf11afSPaul Mackerras 		code = FPE_FLTDIV;
143414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
143514cf11afSPaul Mackerras 		code = FPE_FLTINV;
143614cf11afSPaul Mackerras 	}
143714cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
143814cf11afSPaul Mackerras 		code = FPE_FLTRES;
143914cf11afSPaul Mackerras 
14406a800f36SLiu Yu 	err = do_spe_mathemu(regs);
14416a800f36SLiu Yu 	if (err == 0) {
14426a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14436a800f36SLiu Yu 		emulate_single_step(regs);
144414cf11afSPaul Mackerras 		return;
144514cf11afSPaul Mackerras 	}
14466a800f36SLiu Yu 
14476a800f36SLiu Yu 	if (err == -EFAULT) {
14486a800f36SLiu Yu 		/* got an error reading the instruction */
14496a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14506a800f36SLiu Yu 	} else if (err == -EINVAL) {
14516a800f36SLiu Yu 		/* didn't recognize the instruction */
14526a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14536a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14546a800f36SLiu Yu 	} else {
14556a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14566a800f36SLiu Yu 	}
14576a800f36SLiu Yu 
14586a800f36SLiu Yu 	return;
14596a800f36SLiu Yu }
14606a800f36SLiu Yu 
14616a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
14626a800f36SLiu Yu {
14636a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
14646a800f36SLiu Yu 	int err;
14656a800f36SLiu Yu 
14666a800f36SLiu Yu 	preempt_disable();
14676a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
14686a800f36SLiu Yu 		giveup_spe(current);
14696a800f36SLiu Yu 	preempt_enable();
14706a800f36SLiu Yu 
14716a800f36SLiu Yu 	regs->nip -= 4;
14726a800f36SLiu Yu 	err = speround_handler(regs);
14736a800f36SLiu Yu 	if (err == 0) {
14746a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14756a800f36SLiu Yu 		emulate_single_step(regs);
14766a800f36SLiu Yu 		return;
14776a800f36SLiu Yu 	}
14786a800f36SLiu Yu 
14796a800f36SLiu Yu 	if (err == -EFAULT) {
14806a800f36SLiu Yu 		/* got an error reading the instruction */
14816a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14826a800f36SLiu Yu 	} else if (err == -EINVAL) {
14836a800f36SLiu Yu 		/* didn't recognize the instruction */
14846a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14856a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14866a800f36SLiu Yu 	} else {
14876a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
14886a800f36SLiu Yu 		return;
14896a800f36SLiu Yu 	}
14906a800f36SLiu Yu }
149114cf11afSPaul Mackerras #endif
149214cf11afSPaul Mackerras 
1493dc1c1ca3SStephen Rothwell /*
1494dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1495dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1496dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1497dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1498dc1c1ca3SStephen Rothwell  */
1499dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1500dc1c1ca3SStephen Rothwell {
1501dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1502dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1503dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1504dc1c1ca3SStephen Rothwell }
1505dc1c1ca3SStephen Rothwell 
150614cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
150714cf11afSPaul Mackerras /*
150814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
150914cf11afSPaul Mackerras  * spins until a reboot occurs
151014cf11afSPaul Mackerras  */
151114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
151214cf11afSPaul Mackerras {
151314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
151414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
151514cf11afSPaul Mackerras 	return;
151614cf11afSPaul Mackerras }
151714cf11afSPaul Mackerras 
151814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
151914cf11afSPaul Mackerras {
152014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
152114cf11afSPaul Mackerras 	WatchdogHandler(regs);
152214cf11afSPaul Mackerras }
152314cf11afSPaul Mackerras #endif
1524dc1c1ca3SStephen Rothwell 
1525dc1c1ca3SStephen Rothwell /*
1526dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1527dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1528dc1c1ca3SStephen Rothwell  */
1529dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1530dc1c1ca3SStephen Rothwell {
1531dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1532dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1533dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1534dc1c1ca3SStephen Rothwell }
153514cf11afSPaul Mackerras 
153614cf11afSPaul Mackerras void __init trap_init(void)
153714cf11afSPaul Mackerras {
153814cf11afSPaul Mackerras }
153980947e7cSGeert Uytterhoeven 
154080947e7cSGeert Uytterhoeven 
154180947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
154280947e7cSGeert Uytterhoeven 
154380947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
154480947e7cSGeert Uytterhoeven 
154580947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
154680947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
154780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
154880947e7cSGeert Uytterhoeven #endif
154980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
155080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
155180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
155280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
155380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
155480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
155580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
155680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
155780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
155880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
155980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
156080947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
156180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
156280947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
156380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
156480947e7cSGeert Uytterhoeven #endif
156580947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
156680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
156780947e7cSGeert Uytterhoeven #endif
1568efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1569efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1570efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1571efcac658SAlexey Kardashevskiy #endif
157280947e7cSGeert Uytterhoeven };
157380947e7cSGeert Uytterhoeven 
157480947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
157580947e7cSGeert Uytterhoeven 
157680947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
157780947e7cSGeert Uytterhoeven {
157876462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
157980947e7cSGeert Uytterhoeven 			    type);
158080947e7cSGeert Uytterhoeven }
158180947e7cSGeert Uytterhoeven 
158280947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
158380947e7cSGeert Uytterhoeven {
158480947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
158580947e7cSGeert Uytterhoeven 	unsigned int i;
158680947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
158780947e7cSGeert Uytterhoeven 
158880947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
158980947e7cSGeert Uytterhoeven 		return -ENODEV;
159080947e7cSGeert Uytterhoeven 
159180947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
159280947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
159380947e7cSGeert Uytterhoeven 	if (!dir)
159480947e7cSGeert Uytterhoeven 		return -ENOMEM;
159580947e7cSGeert Uytterhoeven 
159680947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
159780947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
159880947e7cSGeert Uytterhoeven 	if (!d)
159980947e7cSGeert Uytterhoeven 		goto fail;
160080947e7cSGeert Uytterhoeven 
160180947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
160280947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
160380947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
160480947e7cSGeert Uytterhoeven 		if (!d)
160580947e7cSGeert Uytterhoeven 			goto fail;
160680947e7cSGeert Uytterhoeven 	}
160780947e7cSGeert Uytterhoeven 
160880947e7cSGeert Uytterhoeven 	return 0;
160980947e7cSGeert Uytterhoeven 
161080947e7cSGeert Uytterhoeven fail:
161180947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
161280947e7cSGeert Uytterhoeven 	return -ENOMEM;
161380947e7cSGeert Uytterhoeven }
161480947e7cSGeert Uytterhoeven 
161580947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
161680947e7cSGeert Uytterhoeven 
161780947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1618