xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 73d2fb758e678c93bc76d40876c2359f0729b0ef)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
614cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
714cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
814cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1114cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras /*
1514cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
2014cf11afSPaul Mackerras #include <linux/kernel.h>
2114cf11afSPaul Mackerras #include <linux/mm.h>
2214cf11afSPaul Mackerras #include <linux/stddef.h>
2314cf11afSPaul Mackerras #include <linux/unistd.h>
248dad3f92SPaul Mackerras #include <linux/ptrace.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/init.h>
2814cf11afSPaul Mackerras #include <linux/module.h>
298dad3f92SPaul Mackerras #include <linux/prctl.h>
3014cf11afSPaul Mackerras #include <linux/delay.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
32cc532915SMichael Ellerman #include <linux/kexec.h>
335474c120SMichael Hanselmann #include <linux/backlight.h>
3473c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
351eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3680947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3776462232SChristian Dietrich #include <linux/ratelimit.h>
3814cf11afSPaul Mackerras 
3980947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4014cf11afSPaul Mackerras #include <asm/pgtable.h>
4114cf11afSPaul Mackerras #include <asm/uaccess.h>
4214cf11afSPaul Mackerras #include <asm/io.h>
4386417780SPaul Mackerras #include <asm/machdep.h>
4486417780SPaul Mackerras #include <asm/rtas.h>
45f7f6f4feSDavid Gibson #include <asm/pmc.h>
46dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4714cf11afSPaul Mackerras #include <asm/reg.h>
4886417780SPaul Mackerras #endif
4914cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5014cf11afSPaul Mackerras #include <asm/backlight.h>
5114cf11afSPaul Mackerras #endif
52dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5386417780SPaul Mackerras #include <asm/firmware.h>
54dc1c1ca3SStephen Rothwell #include <asm/processor.h>
55dc1c1ca3SStephen Rothwell #endif
56c0ce7d08SDavid Wilder #include <asm/kexec.h>
5716c57b36SKumar Gala #include <asm/ppc-opcode.h>
58cce1f106SShaohui Xie #include <asm/rio.h>
59ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
60ae3a197eSDavid Howells #include <asm/switch_to.h>
61f54db641SMichael Neuling #include <asm/tm.h>
62ae3a197eSDavid Howells #include <asm/debug.h>
63dc1c1ca3SStephen Rothwell 
647dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
655be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
695be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
709422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
715be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
7214cf11afSPaul Mackerras 
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
789422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
7914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
8014cf11afSPaul Mackerras #endif
8114cf11afSPaul Mackerras 
828b3c34cfSMichael Neuling /* Transactional Memory trap debug */
838b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
848b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
858b3c34cfSMichael Neuling #else
868b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
878b3c34cfSMichael Neuling #endif
888b3c34cfSMichael Neuling 
8914cf11afSPaul Mackerras /*
9014cf11afSPaul Mackerras  * Trap & Exception support
9114cf11afSPaul Mackerras  */
9214cf11afSPaul Mackerras 
936031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
946031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
956031d9d9Santon@samba.org {
966031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
976031d9d9Santon@samba.org 	if (pmac_backlight) {
986031d9d9Santon@samba.org 		struct backlight_properties *props;
996031d9d9Santon@samba.org 
1006031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1016031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1026031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1036031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1046031d9d9Santon@samba.org 	}
1056031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1066031d9d9Santon@samba.org }
1076031d9d9Santon@samba.org #else
1086031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1096031d9d9Santon@samba.org #endif
1106031d9d9Santon@samba.org 
111760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
112760ca4dcSAnton Blanchard static int die_owner = -1;
113760ca4dcSAnton Blanchard static unsigned int die_nest_count;
114c0ce7d08SDavid Wilder static int die_counter;
115760ca4dcSAnton Blanchard 
116760ca4dcSAnton Blanchard static unsigned __kprobes long oops_begin(struct pt_regs *regs)
117760ca4dcSAnton Blanchard {
118760ca4dcSAnton Blanchard 	int cpu;
11934c2a14fSanton@samba.org 	unsigned long flags;
12014cf11afSPaul Mackerras 
12114cf11afSPaul Mackerras 	if (debugger(regs))
12214cf11afSPaul Mackerras 		return 1;
12314cf11afSPaul Mackerras 
124293e4688Santon@samba.org 	oops_enter();
125293e4688Santon@samba.org 
126760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
127760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
128760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
129760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
130760ca4dcSAnton Blanchard 		if (cpu == die_owner)
131760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
132760ca4dcSAnton Blanchard 		else
133760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
134760ca4dcSAnton Blanchard 	}
135760ca4dcSAnton Blanchard 	die_nest_count++;
136760ca4dcSAnton Blanchard 	die_owner = cpu;
13714cf11afSPaul Mackerras 	console_verbose();
13814cf11afSPaul Mackerras 	bust_spinlocks(1);
1396031d9d9Santon@samba.org 	if (machine_is(powermac))
1406031d9d9Santon@samba.org 		pmac_backlight_unblank();
141760ca4dcSAnton Blanchard 	return flags;
14234c2a14fSanton@samba.org }
1435474c120SMichael Hanselmann 
144760ca4dcSAnton Blanchard static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
145760ca4dcSAnton Blanchard 			       int signr)
146760ca4dcSAnton Blanchard {
14714cf11afSPaul Mackerras 	bust_spinlocks(0);
148760ca4dcSAnton Blanchard 	die_owner = -1;
149373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
150760ca4dcSAnton Blanchard 	die_nest_count--;
15158154c8cSAnton Blanchard 	oops_exit();
15258154c8cSAnton Blanchard 	printk("\n");
153760ca4dcSAnton Blanchard 	if (!die_nest_count)
154760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
155760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
156760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
157cc532915SMichael Ellerman 
158ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
159ebaeb5aeSMahesh Salgaonkar 
1609b00ac06SAnton Blanchard 	/*
1619b00ac06SAnton Blanchard 	 * A system reset (0x100) is a request to dump, so we always send
1629b00ac06SAnton Blanchard 	 * it through the crashdump code.
1639b00ac06SAnton Blanchard 	 */
1649b00ac06SAnton Blanchard 	if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
165cc532915SMichael Ellerman 		crash_kexec(regs);
1669b00ac06SAnton Blanchard 
1679b00ac06SAnton Blanchard 		/*
1689b00ac06SAnton Blanchard 		 * We aren't the primary crash CPU. We need to send it
1699b00ac06SAnton Blanchard 		 * to a holding pattern to avoid it ending up in the panic
1709b00ac06SAnton Blanchard 		 * code.
1719b00ac06SAnton Blanchard 		 */
172c0ce7d08SDavid Wilder 		crash_kexec_secondary(regs);
1739b00ac06SAnton Blanchard 	}
17414cf11afSPaul Mackerras 
175760ca4dcSAnton Blanchard 	if (!signr)
176760ca4dcSAnton Blanchard 		return;
177760ca4dcSAnton Blanchard 
17858154c8cSAnton Blanchard 	/*
17958154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
18058154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
18158154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
18258154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
18358154c8cSAnton Blanchard 	 */
18458154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
18558154c8cSAnton Blanchard 	    is_global_init(current)) {
18658154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
18758154c8cSAnton Blanchard 	}
18858154c8cSAnton Blanchard 
18914cf11afSPaul Mackerras 	if (in_interrupt())
19014cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
191cea6a4baSHorms 	if (panic_on_oops)
192012c437dSHorms 		panic("Fatal exception");
193760ca4dcSAnton Blanchard 	do_exit(signr);
194760ca4dcSAnton Blanchard }
195cea6a4baSHorms 
196760ca4dcSAnton Blanchard static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
197760ca4dcSAnton Blanchard {
198760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
199760ca4dcSAnton Blanchard #ifdef CONFIG_PREEMPT
200760ca4dcSAnton Blanchard 	printk("PREEMPT ");
201760ca4dcSAnton Blanchard #endif
202760ca4dcSAnton Blanchard #ifdef CONFIG_SMP
203760ca4dcSAnton Blanchard 	printk("SMP NR_CPUS=%d ", NR_CPUS);
204760ca4dcSAnton Blanchard #endif
205760ca4dcSAnton Blanchard #ifdef CONFIG_DEBUG_PAGEALLOC
206760ca4dcSAnton Blanchard 	printk("DEBUG_PAGEALLOC ");
207760ca4dcSAnton Blanchard #endif
208760ca4dcSAnton Blanchard #ifdef CONFIG_NUMA
209760ca4dcSAnton Blanchard 	printk("NUMA ");
210760ca4dcSAnton Blanchard #endif
211760ca4dcSAnton Blanchard 	printk("%s\n", ppc_md.name ? ppc_md.name : "");
212760ca4dcSAnton Blanchard 
213760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
214760ca4dcSAnton Blanchard 		return 1;
215760ca4dcSAnton Blanchard 
216760ca4dcSAnton Blanchard 	print_modules();
217760ca4dcSAnton Blanchard 	show_regs(regs);
21814cf11afSPaul Mackerras 
21914cf11afSPaul Mackerras 	return 0;
22014cf11afSPaul Mackerras }
22114cf11afSPaul Mackerras 
222760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
223760ca4dcSAnton Blanchard {
224760ca4dcSAnton Blanchard 	unsigned long flags = oops_begin(regs);
225760ca4dcSAnton Blanchard 
226760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
227760ca4dcSAnton Blanchard 		err = 0;
228760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
229760ca4dcSAnton Blanchard }
230760ca4dcSAnton Blanchard 
23125baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
23225baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
23325baa35bSOleg Nesterov {
23425baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
23525baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
23625baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
23725baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
23825baa35bSOleg Nesterov }
23925baa35bSOleg Nesterov 
24014cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
24114cf11afSPaul Mackerras {
24214cf11afSPaul Mackerras 	siginfo_t info;
243d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
244d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
245d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
246d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
24714cf11afSPaul Mackerras 
24814cf11afSPaul Mackerras 	if (!user_mode(regs)) {
249760ca4dcSAnton Blanchard 		die("Exception in kernel mode", regs, signr);
25014cf11afSPaul Mackerras 		return;
251760ca4dcSAnton Blanchard 	}
252760ca4dcSAnton Blanchard 
253760ca4dcSAnton Blanchard 	if (show_unhandled_signals && unhandled_signal(current, signr)) {
25476462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
255d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
256d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
25714cf11afSPaul Mackerras 	}
25814cf11afSPaul Mackerras 
259a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
2609f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
2619f2f79e3SBenjamin Herrenschmidt 
26241ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
26314cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
26414cf11afSPaul Mackerras 	info.si_signo = signr;
26514cf11afSPaul Mackerras 	info.si_code = code;
26614cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
26714cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
26814cf11afSPaul Mackerras }
26914cf11afSPaul Mackerras 
27014cf11afSPaul Mackerras #ifdef CONFIG_PPC64
27114cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
27214cf11afSPaul Mackerras {
27314cf11afSPaul Mackerras 	/* See if any machine dependent calls */
274c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
275c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
276c902be71SArnd Bergmann 			return;
277c902be71SArnd Bergmann 	}
27814cf11afSPaul Mackerras 
2798dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
28014cf11afSPaul Mackerras 
28114cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
28214cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
28314cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
28414cf11afSPaul Mackerras 
28514cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
28614cf11afSPaul Mackerras }
28714cf11afSPaul Mackerras #endif
28814cf11afSPaul Mackerras 
28914cf11afSPaul Mackerras /*
29014cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
29114cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
29214cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
29314cf11afSPaul Mackerras  * table.
29414cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
29514cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
29614cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
29714cf11afSPaul Mackerras  *  -- paulus.
29814cf11afSPaul Mackerras  */
29914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
30014cf11afSPaul Mackerras {
30168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
30214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
30314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
30414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
30514cf11afSPaul Mackerras 
30614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
30714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
30814cf11afSPaul Mackerras 		/*
30914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
31014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
31114cf11afSPaul Mackerras 		 * As the address is in the exception table
31214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
31314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
31414cf11afSPaul Mackerras 		 * load or store.
31514cf11afSPaul Mackerras 		 */
31614cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
31714cf11afSPaul Mackerras 			nip -= 2;
31814cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
31914cf11afSPaul Mackerras 			--nip;
32014cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
32114cf11afSPaul Mackerras 			/* sync or twi */
32214cf11afSPaul Mackerras 			unsigned int rb;
32314cf11afSPaul Mackerras 
32414cf11afSPaul Mackerras 			--nip;
32514cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
32614cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
32714cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
32814cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
32914cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
33014cf11afSPaul Mackerras 			regs->nip = entry->fixup;
33114cf11afSPaul Mackerras 			return 1;
33214cf11afSPaul Mackerras 		}
33314cf11afSPaul Mackerras 	}
33468a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
33514cf11afSPaul Mackerras 	return 0;
33614cf11afSPaul Mackerras }
33714cf11afSPaul Mackerras 
338172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
33914cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
34014cf11afSPaul Mackerras    is in the ESR. */
34114cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
34214cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
34314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
34414cf11afSPaul Mackerras #else
345fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
34614cf11afSPaul Mackerras #endif
34714cf11afSPaul Mackerras #define REASON_FP		ESR_FP
34814cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
34914cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
35014cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
35114cf11afSPaul Mackerras 
35214cf11afSPaul Mackerras /* single-step stuff */
35314cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
35414cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
35514cf11afSPaul Mackerras 
35614cf11afSPaul Mackerras #else
35714cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
35814cf11afSPaul Mackerras    exception is in the MSR. */
35914cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
36014cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
3618b3c34cfSMichael Neuling #define REASON_TM		0x200000
36214cf11afSPaul Mackerras #define REASON_FP		0x100000
36314cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
36414cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
36514cf11afSPaul Mackerras #define REASON_TRAP		0x20000
36614cf11afSPaul Mackerras 
36714cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
36814cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
36914cf11afSPaul Mackerras #endif
37014cf11afSPaul Mackerras 
37147c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
37247c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
37314cf11afSPaul Mackerras {
3741a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
37514cf11afSPaul Mackerras 
37614cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
37714cf11afSPaul Mackerras 		printk("Instruction");
37814cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
37914cf11afSPaul Mackerras 	} else
38014cf11afSPaul Mackerras 		printk("Data");
38114cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
38247c0bd1aSBenjamin Herrenschmidt 
38347c0bd1aSBenjamin Herrenschmidt 	return 0;
38447c0bd1aSBenjamin Herrenschmidt }
38547c0bd1aSBenjamin Herrenschmidt 
38647c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
38747c0bd1aSBenjamin Herrenschmidt {
38847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
38947c0bd1aSBenjamin Herrenschmidt 
39014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
39114cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
39214cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
39314cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
39414cf11afSPaul Mackerras 	}
39514cf11afSPaul Mackerras 	else {
39614cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
39714cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
39814cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
39914cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
40014cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
40114cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
40214cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
40314cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
40414cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
40514cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
40614cf11afSPaul Mackerras 			flush_instruction_cache();
40714cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
40814cf11afSPaul Mackerras 		}
40914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
41014cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
41114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
41214cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
41314cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
41414cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
41514cf11afSPaul Mackerras 
41614cf11afSPaul Mackerras 		/* Clear MCSR */
41714cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
41814cf11afSPaul Mackerras 	}
41947c0bd1aSBenjamin Herrenschmidt 	return 0;
42047c0bd1aSBenjamin Herrenschmidt }
421fc5e7097SDave Kleikamp 
422fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
423fc5e7097SDave Kleikamp {
424fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
425fc5e7097SDave Kleikamp 	u32 mcsr;
426fc5e7097SDave Kleikamp 
427fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
428fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
429fc5e7097SDave Kleikamp 		printk(KERN_ERR
430fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
431fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
432fc5e7097SDave Kleikamp 		return 0;
433fc5e7097SDave Kleikamp 	}
434fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
435fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
436fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
437fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
438fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
439fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
440fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
441fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
442fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
443fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
444fc5e7097SDave Kleikamp 		flush_instruction_cache();
445fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
446fc5e7097SDave Kleikamp 	}
447fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
448fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
449fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
450fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
451fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
452fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
453fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
454fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
455fc5e7097SDave Kleikamp 
456fc5e7097SDave Kleikamp 	/* Clear MCSR */
457fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
458fc5e7097SDave Kleikamp 
459fc5e7097SDave Kleikamp 	return 0;
460fc5e7097SDave Kleikamp }
46114cf11afSPaul Mackerras #elif defined(CONFIG_E500)
462fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
463fe04b112SScott Wood {
464fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
465fe04b112SScott Wood 	unsigned long reason = mcsr;
466fe04b112SScott Wood 	int recoverable = 1;
467fe04b112SScott Wood 
46882a9a480SScott Wood 	if (reason & MCSR_LD) {
469cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
470cce1f106SShaohui Xie 		if (recoverable == 1)
471cce1f106SShaohui Xie 			goto silent_out;
472cce1f106SShaohui Xie 	}
473cce1f106SShaohui Xie 
474fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
475fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
476fe04b112SScott Wood 
477fe04b112SScott Wood 	if (reason & MCSR_MCP)
478fe04b112SScott Wood 		printk("Machine Check Signal\n");
479fe04b112SScott Wood 
480fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
481fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
482fe04b112SScott Wood 
483fe04b112SScott Wood 		/*
484fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
485fe04b112SScott Wood 		 */
486fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
487fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
488fe04b112SScott Wood 			;
489fe04b112SScott Wood 
490fe04b112SScott Wood 		/*
491fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
492fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
493fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
494fe04b112SScott Wood 		 */
495fe04b112SScott Wood 		reason &= ~MCSR_IF;
496fe04b112SScott Wood 	}
497fe04b112SScott Wood 
498fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
499fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
50037caf9f2SKumar Gala 
50137caf9f2SKumar Gala 		/*
50237caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
50337caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
50437caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
50537caf9f2SKumar Gala 		 */
50637caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
507fe04b112SScott Wood 			recoverable = 0;
508fe04b112SScott Wood 	}
509fe04b112SScott Wood 
510fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
511fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
512fe04b112SScott Wood 		recoverable = 0;
513fe04b112SScott Wood 	}
514fe04b112SScott Wood 
515fe04b112SScott Wood 	if (reason & MCSR_NMI)
516fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
517fe04b112SScott Wood 
518fe04b112SScott Wood 	if (reason & MCSR_IF) {
519fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
520fe04b112SScott Wood 		recoverable = 0;
521fe04b112SScott Wood 	}
522fe04b112SScott Wood 
523fe04b112SScott Wood 	if (reason & MCSR_LD) {
524fe04b112SScott Wood 		printk("Load Error Report\n");
525fe04b112SScott Wood 		recoverable = 0;
526fe04b112SScott Wood 	}
527fe04b112SScott Wood 
528fe04b112SScott Wood 	if (reason & MCSR_ST) {
529fe04b112SScott Wood 		printk("Store Error Report\n");
530fe04b112SScott Wood 		recoverable = 0;
531fe04b112SScott Wood 	}
532fe04b112SScott Wood 
533fe04b112SScott Wood 	if (reason & MCSR_LDG) {
534fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
535fe04b112SScott Wood 		recoverable = 0;
536fe04b112SScott Wood 	}
537fe04b112SScott Wood 
538fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
539fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
540fe04b112SScott Wood 
541fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
542fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
543fe04b112SScott Wood 		recoverable = 0;
544fe04b112SScott Wood 	}
545fe04b112SScott Wood 
546fe04b112SScott Wood 	if (reason & MCSR_MAV) {
547fe04b112SScott Wood 		u64 addr;
548fe04b112SScott Wood 
549fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
550fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
551fe04b112SScott Wood 
552fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
553fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
554fe04b112SScott Wood 	}
555fe04b112SScott Wood 
556cce1f106SShaohui Xie silent_out:
557fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
558fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
559fe04b112SScott Wood }
560fe04b112SScott Wood 
56147c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
56247c0bd1aSBenjamin Herrenschmidt {
56347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
56447c0bd1aSBenjamin Herrenschmidt 
565cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
566cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
567cce1f106SShaohui Xie 			return 1;
568cce1f106SShaohui Xie 	}
569cce1f106SShaohui Xie 
57014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
57214cf11afSPaul Mackerras 
57314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
57414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
57514cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
57614cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
57714cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
57814cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
57914cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
58014cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
58114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
58214cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
58314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
58414cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
58514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
58614cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
58714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
58814cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
58914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
59014cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
59214cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
59314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
59414cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
59514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
59614cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
59747c0bd1aSBenjamin Herrenschmidt 
59847c0bd1aSBenjamin Herrenschmidt 	return 0;
59947c0bd1aSBenjamin Herrenschmidt }
6004490c06bSKumar Gala 
6014490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6024490c06bSKumar Gala {
6034490c06bSKumar Gala 	return 0;
6044490c06bSKumar Gala }
60514cf11afSPaul Mackerras #elif defined(CONFIG_E200)
60647c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
60747c0bd1aSBenjamin Herrenschmidt {
60847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
60947c0bd1aSBenjamin Herrenschmidt 
61014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
61114cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
61214cf11afSPaul Mackerras 
61314cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
61414cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
61514cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
61614cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
61714cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
61814cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
61914cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
62014cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
62114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
62214cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
62314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
62414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
62514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
62614cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
62747c0bd1aSBenjamin Herrenschmidt 
62847c0bd1aSBenjamin Herrenschmidt 	return 0;
62947c0bd1aSBenjamin Herrenschmidt }
63047c0bd1aSBenjamin Herrenschmidt #else
63147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
63247c0bd1aSBenjamin Herrenschmidt {
63347c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
63447c0bd1aSBenjamin Herrenschmidt 
63514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
63614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
63714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
63814cf11afSPaul Mackerras 	case 0x80000:
63914cf11afSPaul Mackerras 		printk("Machine check signal\n");
64014cf11afSPaul Mackerras 		break;
64114cf11afSPaul Mackerras 	case 0:		/* for 601 */
64214cf11afSPaul Mackerras 	case 0x40000:
64314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
64414cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
64514cf11afSPaul Mackerras 		break;
64614cf11afSPaul Mackerras 	case 0x20000:
64714cf11afSPaul Mackerras 		printk("Data parity error signal\n");
64814cf11afSPaul Mackerras 		break;
64914cf11afSPaul Mackerras 	case 0x10000:
65014cf11afSPaul Mackerras 		printk("Address parity error signal\n");
65114cf11afSPaul Mackerras 		break;
65214cf11afSPaul Mackerras 	case 0x20000000:
65314cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
65414cf11afSPaul Mackerras 		break;
65514cf11afSPaul Mackerras 	case 0x40000000:
65614cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
65714cf11afSPaul Mackerras 		break;
65814cf11afSPaul Mackerras 	case 0x00100000:
65914cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
66014cf11afSPaul Mackerras 		break;
66114cf11afSPaul Mackerras 	default:
66214cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
66314cf11afSPaul Mackerras 	}
66475918a4bSOlof Johansson 	return 0;
66575918a4bSOlof Johansson }
66647c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
66775918a4bSOlof Johansson 
66875918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
66975918a4bSOlof Johansson {
67075918a4bSOlof Johansson 	int recover = 0;
67175918a4bSOlof Johansson 
67289713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
67389713ed1SAnton Blanchard 
67447c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
67547c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
67647c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
67747c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
67847c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
67947c0bd1aSBenjamin Herrenschmidt 	 */
68075918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
68175918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
68247c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
68347c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
68475918a4bSOlof Johansson 
68547c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
68675918a4bSOlof Johansson 		return;
68775918a4bSOlof Johansson 
68875918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
68947c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
69047c0bd1aSBenjamin Herrenschmidt 	 *
69147c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
69247c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
69347c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
69447c0bd1aSBenjamin Herrenschmidt 	 */
69575918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
69675918a4bSOlof Johansson 	return;
69775918a4bSOlof Johansson #endif
69875918a4bSOlof Johansson 
699a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
70075918a4bSOlof Johansson 		return;
70175918a4bSOlof Johansson 
70275918a4bSOlof Johansson 	if (check_io_access(regs))
70375918a4bSOlof Johansson 		return;
70475918a4bSOlof Johansson 
7058dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
70614cf11afSPaul Mackerras 
70714cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
70814cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
70914cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
71014cf11afSPaul Mackerras }
71114cf11afSPaul Mackerras 
71214cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
71314cf11afSPaul Mackerras {
71414cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
71514cf11afSPaul Mackerras }
71614cf11afSPaul Mackerras 
717dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
71814cf11afSPaul Mackerras {
71914cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
72014cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
72114cf11afSPaul Mackerras 
72214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
72314cf11afSPaul Mackerras }
72414cf11afSPaul Mackerras 
725dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
72614cf11afSPaul Mackerras {
72714cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
72814cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
72914cf11afSPaul Mackerras 		return;
73014cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
73114cf11afSPaul Mackerras 		return;
73214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
73314cf11afSPaul Mackerras }
73414cf11afSPaul Mackerras 
73514cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
73614cf11afSPaul Mackerras {
73714cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
73814cf11afSPaul Mackerras }
73914cf11afSPaul Mackerras 
7408dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
74114cf11afSPaul Mackerras {
7422538c2d0SK.Prasad 	clear_single_step(regs);
74314cf11afSPaul Mackerras 
74414cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
74514cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
74614cf11afSPaul Mackerras 		return;
74714cf11afSPaul Mackerras 	if (debugger_sstep(regs))
74814cf11afSPaul Mackerras 		return;
74914cf11afSPaul Mackerras 
75014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
75114cf11afSPaul Mackerras }
75214cf11afSPaul Mackerras 
75314cf11afSPaul Mackerras /*
75414cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
75514cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
75614cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
75714cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
75814cf11afSPaul Mackerras  */
7598dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
76014cf11afSPaul Mackerras {
7612538c2d0SK.Prasad 	if (single_stepping(regs))
7622538c2d0SK.Prasad 		single_step_exception(regs);
76314cf11afSPaul Mackerras }
76414cf11afSPaul Mackerras 
7655fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
766dc1c1ca3SStephen Rothwell {
7675fad293bSKumar Gala 	int ret = 0;
768dc1c1ca3SStephen Rothwell 
769dc1c1ca3SStephen Rothwell 	/* Invalid operation */
770dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7715fad293bSKumar Gala 		ret = FPE_FLTINV;
772dc1c1ca3SStephen Rothwell 
773dc1c1ca3SStephen Rothwell 	/* Overflow */
774dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7755fad293bSKumar Gala 		ret = FPE_FLTOVF;
776dc1c1ca3SStephen Rothwell 
777dc1c1ca3SStephen Rothwell 	/* Underflow */
778dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7795fad293bSKumar Gala 		ret = FPE_FLTUND;
780dc1c1ca3SStephen Rothwell 
781dc1c1ca3SStephen Rothwell 	/* Divide by zero */
782dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7835fad293bSKumar Gala 		ret = FPE_FLTDIV;
784dc1c1ca3SStephen Rothwell 
785dc1c1ca3SStephen Rothwell 	/* Inexact result */
786dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7875fad293bSKumar Gala 		ret = FPE_FLTRES;
7885fad293bSKumar Gala 
7895fad293bSKumar Gala 	return ret;
7905fad293bSKumar Gala }
7915fad293bSKumar Gala 
7925fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7935fad293bSKumar Gala {
7945fad293bSKumar Gala 	int code = 0;
7955fad293bSKumar Gala 
7965fad293bSKumar Gala 	flush_fp_to_thread(current);
7975fad293bSKumar Gala 
7985fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
799dc1c1ca3SStephen Rothwell 
800dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
801dc1c1ca3SStephen Rothwell }
802dc1c1ca3SStephen Rothwell 
803dc1c1ca3SStephen Rothwell /*
804dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
80514cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
80614cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
80714cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
80814cf11afSPaul Mackerras  *
80914cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
81014cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
81114cf11afSPaul Mackerras  * bits is faster and easier.
81286417780SPaul Mackerras  *
81314cf11afSPaul Mackerras  */
81414cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
81514cf11afSPaul Mackerras {
81614cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
81714cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
81814cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
81914cf11afSPaul Mackerras 	u32 num_bytes;
82014cf11afSPaul Mackerras 	unsigned long EA;
82114cf11afSPaul Mackerras 	int pos = 0;
82214cf11afSPaul Mackerras 
82314cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
82416c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
82514cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
82614cf11afSPaul Mackerras 			return -EINVAL;
82714cf11afSPaul Mackerras 
82814cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
82914cf11afSPaul Mackerras 
83016c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
83116c57b36SKumar Gala 		case PPC_INST_LSWX:
83216c57b36SKumar Gala 		case PPC_INST_STSWX:
83314cf11afSPaul Mackerras 			EA += NB_RB;
83414cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
83514cf11afSPaul Mackerras 			break;
83616c57b36SKumar Gala 		case PPC_INST_LSWI:
83716c57b36SKumar Gala 		case PPC_INST_STSWI:
83814cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
83914cf11afSPaul Mackerras 			break;
84014cf11afSPaul Mackerras 		default:
84114cf11afSPaul Mackerras 			return -EINVAL;
84214cf11afSPaul Mackerras 	}
84314cf11afSPaul Mackerras 
84414cf11afSPaul Mackerras 	while (num_bytes != 0)
84514cf11afSPaul Mackerras 	{
84614cf11afSPaul Mackerras 		u8 val;
84714cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
84814cf11afSPaul Mackerras 
84916c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
85016c57b36SKumar Gala 			case PPC_INST_LSWX:
85116c57b36SKumar Gala 			case PPC_INST_LSWI:
85214cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
85314cf11afSPaul Mackerras 					return -EFAULT;
85414cf11afSPaul Mackerras 				/* first time updating this reg,
85514cf11afSPaul Mackerras 				 * zero it out */
85614cf11afSPaul Mackerras 				if (pos == 0)
85714cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
85814cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
85914cf11afSPaul Mackerras 				break;
86016c57b36SKumar Gala 			case PPC_INST_STSWI:
86116c57b36SKumar Gala 			case PPC_INST_STSWX:
86214cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
86314cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
86414cf11afSPaul Mackerras 					return -EFAULT;
86514cf11afSPaul Mackerras 				break;
86614cf11afSPaul Mackerras 		}
86714cf11afSPaul Mackerras 		/* move EA to next address */
86814cf11afSPaul Mackerras 		EA += 1;
86914cf11afSPaul Mackerras 		num_bytes--;
87014cf11afSPaul Mackerras 
87114cf11afSPaul Mackerras 		/* manage our position within the register */
87214cf11afSPaul Mackerras 		if (++pos == 4) {
87314cf11afSPaul Mackerras 			pos = 0;
87414cf11afSPaul Mackerras 			if (++rT == 32)
87514cf11afSPaul Mackerras 				rT = 0;
87614cf11afSPaul Mackerras 		}
87714cf11afSPaul Mackerras 	}
87814cf11afSPaul Mackerras 
87914cf11afSPaul Mackerras 	return 0;
88014cf11afSPaul Mackerras }
88114cf11afSPaul Mackerras 
882c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
883c3412dcbSWill Schmidt {
884c3412dcbSWill Schmidt 	u32 ra,rs;
885c3412dcbSWill Schmidt 	unsigned long tmp;
886c3412dcbSWill Schmidt 
887c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
888c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
889c3412dcbSWill Schmidt 
890c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
891c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
892c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
893c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
894c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
895c3412dcbSWill Schmidt 
896c3412dcbSWill Schmidt 	return 0;
897c3412dcbSWill Schmidt }
898c3412dcbSWill Schmidt 
899c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
900c1469f13SKumar Gala {
901c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
902c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
903c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
904c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
905c1469f13SKumar Gala 	u8 bit;
906c1469f13SKumar Gala 	unsigned long tmp;
907c1469f13SKumar Gala 
908c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
909c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
910c1469f13SKumar Gala 
911c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
912c1469f13SKumar Gala 
913c1469f13SKumar Gala 	return 0;
914c1469f13SKumar Gala }
915c1469f13SKumar Gala 
91614cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
91714cf11afSPaul Mackerras {
91814cf11afSPaul Mackerras 	u32 instword;
91914cf11afSPaul Mackerras 	u32 rd;
92014cf11afSPaul Mackerras 
921fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
92214cf11afSPaul Mackerras 		return -EINVAL;
92314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
92414cf11afSPaul Mackerras 
92514cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
92614cf11afSPaul Mackerras 		return -EFAULT;
92714cf11afSPaul Mackerras 
92814cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
92916c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
930eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
93114cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
93214cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
93314cf11afSPaul Mackerras 		return 0;
93414cf11afSPaul Mackerras 	}
93514cf11afSPaul Mackerras 
93614cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
93780947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
938eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
93914cf11afSPaul Mackerras 		return 0;
94080947e7cSGeert Uytterhoeven 	}
94114cf11afSPaul Mackerras 
94214cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
94316c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
94486417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
94514cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
94614cf11afSPaul Mackerras 
947eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
94814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
94914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
95014cf11afSPaul Mackerras 		return 0;
95114cf11afSPaul Mackerras 	}
95214cf11afSPaul Mackerras 
95314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
95480947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
955eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
95614cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
95780947e7cSGeert Uytterhoeven 	}
95814cf11afSPaul Mackerras 
959c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
96016c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
961eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
962c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
963c3412dcbSWill Schmidt 	}
964c3412dcbSWill Schmidt 
965c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
96616c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
967eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
968c1469f13SKumar Gala 		return emulate_isel(regs, instword);
969c1469f13SKumar Gala 	}
970c1469f13SKumar Gala 
971efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
972efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
973*73d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
974*73d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
975*73d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
976*73d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
977efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
978efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
979efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
980efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
981efcac658SAlexey Kardashevskiy 		return 0;
982efcac658SAlexey Kardashevskiy 	}
983efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
984*73d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
985*73d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
986*73d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
987*73d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
988efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
989efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
990efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
99100ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
992efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
99300ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
994efcac658SAlexey Kardashevskiy 		return 0;
995efcac658SAlexey Kardashevskiy 	}
996efcac658SAlexey Kardashevskiy #endif
997efcac658SAlexey Kardashevskiy 
99814cf11afSPaul Mackerras 	return -EINVAL;
99914cf11afSPaul Mackerras }
100014cf11afSPaul Mackerras 
100173c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
100214cf11afSPaul Mackerras {
100373c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
100414cf11afSPaul Mackerras }
100514cf11afSPaul Mackerras 
10068dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
100714cf11afSPaul Mackerras {
100814cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
100914cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
101014cf11afSPaul Mackerras 
1011aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
101204903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
101314cf11afSPaul Mackerras 
101414cf11afSPaul Mackerras 	if (reason & REASON_FP) {
101514cf11afSPaul Mackerras 		/* IEEE FP exception */
1016dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
10178dad3f92SPaul Mackerras 		return;
10188dad3f92SPaul Mackerras 	}
10198dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1020ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1021ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1022ba797b28SJason Wessel 		if (debugger_bpt(regs))
1023ba797b28SJason Wessel 			return;
1024ba797b28SJason Wessel 
102514cf11afSPaul Mackerras 		/* trap exception */
1026dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1027dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1028dc1c1ca3SStephen Rothwell 			return;
102973c9ceabSJeremy Fitzhardinge 
103073c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1031608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
103214cf11afSPaul Mackerras 			regs->nip += 4;
103314cf11afSPaul Mackerras 			return;
103414cf11afSPaul Mackerras 		}
10358dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10368dad3f92SPaul Mackerras 		return;
10378dad3f92SPaul Mackerras 	}
1038bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1039bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1040bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1041bc2a9408SMichael Neuling 		 * This occurs when:
1042bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1043bc2a9408SMichael Neuling 		 *    transition in TM states.
1044bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1045bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1046bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1047bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1048bc2a9408SMichael Neuling 		 */
1049bc2a9408SMichael Neuling 		if (!user_mode(regs) &&
1050bc2a9408SMichael Neuling 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1051bc2a9408SMichael Neuling 			regs->nip += 4;
1052bc2a9408SMichael Neuling 			return;
1053bc2a9408SMichael Neuling 		}
1054bc2a9408SMichael Neuling 		/* If usermode caused this, it's done something illegal and
1055bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1056bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1057bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1058bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1059bc2a9408SMichael Neuling 		 */
1060bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1061bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1062bc2a9408SMichael Neuling 			return;
1063bc2a9408SMichael Neuling 		} else {
1064bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1065bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1066bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1067bc2a9408SMichael Neuling 		}
1068bc2a9408SMichael Neuling 	}
1069bc2a9408SMichael Neuling #endif
10708dad3f92SPaul Mackerras 
1071a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1072a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1073cd8a5673SPaul Mackerras 		local_irq_enable();
1074cd8a5673SPaul Mackerras 
107504903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
107604903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
107704903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
107804903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
107904903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
108004903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
108104903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
108225985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10835fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10845fad293bSKumar Gala 	case 0:
108504903a30SKumar Gala 		emulate_single_step(regs);
108604903a30SKumar Gala 		return;
10875fad293bSKumar Gala 	case 1: {
10885fad293bSKumar Gala 			int code = 0;
10895fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10905fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10915fad293bSKumar Gala 			return;
109204903a30SKumar Gala 		}
10935fad293bSKumar Gala 	case -EFAULT:
10945fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10955fad293bSKumar Gala 		return;
10965fad293bSKumar Gala 	}
10975fad293bSKumar Gala 	/* fall through on any other errors */
109804903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
109904903a30SKumar Gala 
11008dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
11018dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
110214cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
110314cf11afSPaul Mackerras 		case 0:
110414cf11afSPaul Mackerras 			regs->nip += 4;
110514cf11afSPaul Mackerras 			emulate_single_step(regs);
11068dad3f92SPaul Mackerras 			return;
110714cf11afSPaul Mackerras 		case -EFAULT:
110814cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11098dad3f92SPaul Mackerras 			return;
11108dad3f92SPaul Mackerras 		}
11118dad3f92SPaul Mackerras 	}
11128dad3f92SPaul Mackerras 
111314cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
111414cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
111514cf11afSPaul Mackerras 	else
111614cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
111714cf11afSPaul Mackerras }
111814cf11afSPaul Mackerras 
1119dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
112014cf11afSPaul Mackerras {
11214393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
112214cf11afSPaul Mackerras 
1123a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1124a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1125a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1126a3512b2dSBenjamin Herrenschmidt 
1127e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1128e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
112914cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
113014cf11afSPaul Mackerras 
113114cf11afSPaul Mackerras 	if (fixed == 1) {
113214cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
113314cf11afSPaul Mackerras 		emulate_single_step(regs);
113414cf11afSPaul Mackerras 		return;
113514cf11afSPaul Mackerras 	}
113614cf11afSPaul Mackerras 
113714cf11afSPaul Mackerras 	/* Operand address was bad */
113814cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
11394393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
11404393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
11414393c4f6SBenjamin Herrenschmidt 	} else {
11424393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
11434393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
114414cf11afSPaul Mackerras 	}
11454393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
11464393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
11474393c4f6SBenjamin Herrenschmidt 	else
11484393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
114914cf11afSPaul Mackerras }
115014cf11afSPaul Mackerras 
115114cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
115214cf11afSPaul Mackerras {
115314cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
115414cf11afSPaul Mackerras 	       current, regs->gpr[1]);
115514cf11afSPaul Mackerras 	debugger(regs);
115614cf11afSPaul Mackerras 	show_regs(regs);
115714cf11afSPaul Mackerras 	panic("kernel stack overflow");
115814cf11afSPaul Mackerras }
115914cf11afSPaul Mackerras 
116014cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
116114cf11afSPaul Mackerras {
116214cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
116314cf11afSPaul Mackerras 	       regs->nip, regs->msr);
116414cf11afSPaul Mackerras 	debugger(regs);
116514cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
116614cf11afSPaul Mackerras }
116714cf11afSPaul Mackerras 
116814cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
116914cf11afSPaul Mackerras {
117014cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
117119c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
117214cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
117314cf11afSPaul Mackerras }
117414cf11afSPaul Mackerras 
1175dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1176dc1c1ca3SStephen Rothwell {
1177dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1178dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1179dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1180dc1c1ca3SStephen Rothwell }
1181dc1c1ca3SStephen Rothwell 
1182dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1183dc1c1ca3SStephen Rothwell {
1184dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1185dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1186dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1187dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1188dc1c1ca3SStephen Rothwell 		return;
1189dc1c1ca3SStephen Rothwell 	}
11906c4841c2SAnton Blanchard 
1191dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1192dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1193dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1194dc1c1ca3SStephen Rothwell }
1195dc1c1ca3SStephen Rothwell 
1196ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1197ce48b210SMichael Neuling {
1198ce48b210SMichael Neuling 	if (user_mode(regs)) {
1199ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1200ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1201ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1202ce48b210SMichael Neuling 		return;
1203ce48b210SMichael Neuling 	}
1204ce48b210SMichael Neuling 
1205ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1206ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1207ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1208ce48b210SMichael Neuling }
1209ce48b210SMichael Neuling 
1210d0c0c9a1SMichael Neuling void tm_unavailable_exception(struct pt_regs *regs)
1211d0c0c9a1SMichael Neuling {
1212d0c0c9a1SMichael Neuling 	/* We restore the interrupt state now */
1213d0c0c9a1SMichael Neuling 	if (!arch_irq_disabled_regs(regs))
1214d0c0c9a1SMichael Neuling 		local_irq_enable();
1215d0c0c9a1SMichael Neuling 
1216d0c0c9a1SMichael Neuling 	/* Currently we never expect a TMU exception.  Catch
1217d0c0c9a1SMichael Neuling 	 * this and kill the process!
1218d0c0c9a1SMichael Neuling 	 */
1219d0c0c9a1SMichael Neuling 	printk(KERN_EMERG "Unexpected TM unavailable exception at %lx "
1220d0c0c9a1SMichael Neuling 	       "(msr %lx)\n",
1221d0c0c9a1SMichael Neuling 	       regs->nip, regs->msr);
1222d0c0c9a1SMichael Neuling 
1223d0c0c9a1SMichael Neuling 	if (user_mode(regs)) {
1224d0c0c9a1SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1225d0c0c9a1SMichael Neuling 		return;
1226d0c0c9a1SMichael Neuling 	}
1227d0c0c9a1SMichael Neuling 
1228d0c0c9a1SMichael Neuling 	die("Unexpected TM unavailable exception", regs, SIGABRT);
1229d0c0c9a1SMichael Neuling }
1230d0c0c9a1SMichael Neuling 
1231f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1232f54db641SMichael Neuling 
1233f54db641SMichael Neuling extern void do_load_up_fpu(struct pt_regs *regs);
1234f54db641SMichael Neuling 
1235f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1236f54db641SMichael Neuling {
1237f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1238f54db641SMichael Neuling 
1239f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1240f54db641SMichael Neuling 		 regs->nip, regs->msr);
1241f54db641SMichael Neuling 	tm_enable();
1242f54db641SMichael Neuling 
1243f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1244f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1245f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1246f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1247f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1248f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1249f54db641SMichael Neuling 	 */
1250f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1251f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1252f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1253f54db641SMichael Neuling 
1254f54db641SMichael Neuling 	/* Enable FP for the task: */
1255f54db641SMichael Neuling 	regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1256f54db641SMichael Neuling 
1257f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1258f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1259f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
1260f54db641SMichael Neuling 	 */
1261f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1262f54db641SMichael Neuling }
1263f54db641SMichael Neuling 
1264f54db641SMichael Neuling #ifdef CONFIG_ALTIVEC
1265f54db641SMichael Neuling extern void do_load_up_altivec(struct pt_regs *regs);
1266f54db641SMichael Neuling 
1267f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1268f54db641SMichael Neuling {
1269f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1270f54db641SMichael Neuling 	 * the same way.
1271f54db641SMichael Neuling 	 */
1272f54db641SMichael Neuling 
1273f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1274f54db641SMichael Neuling 		 "MSR=%lx\n",
1275f54db641SMichael Neuling 		 regs->nip, regs->msr);
1276f54db641SMichael Neuling 	tm_enable();
1277f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1278f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1279f54db641SMichael Neuling 	regs->msr |= MSR_VEC;
1280f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1281f54db641SMichael Neuling 	current->thread.used_vr = 1;
1282f54db641SMichael Neuling }
1283f54db641SMichael Neuling #endif
1284f54db641SMichael Neuling 
1285f54db641SMichael Neuling #ifdef CONFIG_VSX
1286f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1287f54db641SMichael Neuling {
1288f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1289f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1290f54db641SMichael Neuling 	 *
1291f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1292f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1293f54db641SMichael Neuling 	 */
1294f54db641SMichael Neuling 
1295f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1296f54db641SMichael Neuling 		 "MSR=%lx\n",
1297f54db641SMichael Neuling 		 regs->nip, regs->msr);
1298f54db641SMichael Neuling 
1299f54db641SMichael Neuling 	tm_enable();
1300f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1301f54db641SMichael Neuling 	tm_reclaim(&current->thread, current->thread.regs->msr,
1302f54db641SMichael Neuling 		   TM_CAUSE_FAC_UNAV);
1303f54db641SMichael Neuling 
1304f54db641SMichael Neuling 	regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1305f54db641SMichael Neuling 		MSR_VSX;
1306f54db641SMichael Neuling 	/* This loads & recheckpoints FP and VRs. */
1307f54db641SMichael Neuling 	tm_recheckpoint(&current->thread, regs->msr);
1308f54db641SMichael Neuling 	current->thread.used_vsr = 1;
1309f54db641SMichael Neuling }
1310f54db641SMichael Neuling #endif
1311f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1312f54db641SMichael Neuling 
1313dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1314dc1c1ca3SStephen Rothwell {
131589713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
131689713ed1SAnton Blanchard 
1317dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1318dc1c1ca3SStephen Rothwell }
1319dc1c1ca3SStephen Rothwell 
13208dad3f92SPaul Mackerras #ifdef CONFIG_8xx
132114cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
132214cf11afSPaul Mackerras {
132314cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
132414cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
13255dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
132614cf11afSPaul Mackerras 	int errcode;
13275dd57a13SScott Wood #endif
132814cf11afSPaul Mackerras 
132914cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
133014cf11afSPaul Mackerras 
133114cf11afSPaul Mackerras 	if (!user_mode(regs)) {
133214cf11afSPaul Mackerras 		debugger(regs);
133314cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
133414cf11afSPaul Mackerras 	}
133514cf11afSPaul Mackerras 
133614cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
133714cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
133880947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1339eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
13405fad293bSKumar Gala 
13415fad293bSKumar Gala 	switch (errcode) {
13425fad293bSKumar Gala 	case 0:
13435fad293bSKumar Gala 		emulate_single_step(regs);
13445fad293bSKumar Gala 		return;
13455fad293bSKumar Gala 	case 1: {
13465fad293bSKumar Gala 			int code = 0;
13475fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
13485fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
13495fad293bSKumar Gala 			return;
13505fad293bSKumar Gala 		}
13515fad293bSKumar Gala 	case -EFAULT:
13525fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
13535fad293bSKumar Gala 		return;
13545fad293bSKumar Gala 	default:
13555fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
13565fad293bSKumar Gala 		return;
13575fad293bSKumar Gala 	}
13585fad293bSKumar Gala 
13595dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
136014cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
136180947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1362eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
136380947e7cSGeert Uytterhoeven 
13645fad293bSKumar Gala 	switch (errcode) {
13655fad293bSKumar Gala 	case 0:
136614cf11afSPaul Mackerras 		emulate_single_step(regs);
13675fad293bSKumar Gala 		return;
13685fad293bSKumar Gala 	case 1:
13695fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
13705fad293bSKumar Gala 		return;
13715fad293bSKumar Gala 	case -EFAULT:
13725fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
13735fad293bSKumar Gala 		return;
13745fad293bSKumar Gala 	}
13755dd57a13SScott Wood #else
13765dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
13775fad293bSKumar Gala #endif
137814cf11afSPaul Mackerras }
13798dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
138014cf11afSPaul Mackerras 
1381172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
13823bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
13833bffb652SDave Kleikamp {
13843bffb652SDave Kleikamp 	int changed = 0;
13853bffb652SDave Kleikamp 	/*
13863bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
13873bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
13883bffb652SDave Kleikamp 	 */
13893bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
13903bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
13913bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
13923bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
13933bffb652SDave Kleikamp #endif
13943bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
13953bffb652SDave Kleikamp 			     5);
13963bffb652SDave Kleikamp 		changed |= 0x01;
13973bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
13983bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
13993bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
14003bffb652SDave Kleikamp 			     6);
14013bffb652SDave Kleikamp 		changed |= 0x01;
14023bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
14033bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
14043bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
14053bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
14063bffb652SDave Kleikamp 			     1);
14073bffb652SDave Kleikamp 		changed |= 0x01;
14083bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
14093bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
14103bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
14113bffb652SDave Kleikamp 			     2);
14123bffb652SDave Kleikamp 		changed |= 0x01;
14133bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
14143bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
14153bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
14163bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
14173bffb652SDave Kleikamp 			     3);
14183bffb652SDave Kleikamp 		changed |= 0x01;
14193bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
14203bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
14213bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
14223bffb652SDave Kleikamp 			     4);
14233bffb652SDave Kleikamp 		changed |= 0x01;
14243bffb652SDave Kleikamp 	}
14253bffb652SDave Kleikamp 	/*
14263bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
14273bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
14283bffb652SDave Kleikamp 	 * back on or not.
14293bffb652SDave Kleikamp 	 */
14303bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
14313bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
14323bffb652SDave Kleikamp 	else
14333bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
14343bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
14353bffb652SDave Kleikamp 
14363bffb652SDave Kleikamp 	if (changed & 0x01)
14373bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
14383bffb652SDave Kleikamp }
143914cf11afSPaul Mackerras 
1440f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
144114cf11afSPaul Mackerras {
14423bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
14433bffb652SDave Kleikamp 
1444ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1445ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1446ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1447ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1448ec097c84SRoland McGrath 	 */
1449ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1450ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1451ec097c84SRoland McGrath 
1452ec097c84SRoland McGrath 		/* Disable BT */
1453ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1454ec097c84SRoland McGrath 		/* Clear the BT event */
1455ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1456ec097c84SRoland McGrath 
1457ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1458ec097c84SRoland McGrath 		if (user_mode(regs)) {
1459ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1460ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1461ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1462ec097c84SRoland McGrath 			return;
1463ec097c84SRoland McGrath 		}
1464ec097c84SRoland McGrath 
1465ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1466ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1467ec097c84SRoland McGrath 			return;
1468ec097c84SRoland McGrath 		}
1469ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1470ec097c84SRoland McGrath 			return;
1471ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
147214cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1473f8279621SKumar Gala 
147414cf11afSPaul Mackerras 		/* Disable instruction completion */
147514cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
147614cf11afSPaul Mackerras 		/* Clear the instruction completion event */
147714cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1478f8279621SKumar Gala 
1479f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1480f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
148114cf11afSPaul Mackerras 			return;
148214cf11afSPaul Mackerras 		}
1483f8279621SKumar Gala 
1484f8279621SKumar Gala 		if (debugger_sstep(regs))
1485f8279621SKumar Gala 			return;
1486f8279621SKumar Gala 
14873bffb652SDave Kleikamp 		if (user_mode(regs)) {
14883bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
14893bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
14903bffb652SDave Kleikamp 					       current->thread.dbcr1))
14913bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
14923bffb652SDave Kleikamp 			else
14933bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
14943bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
14953bffb652SDave Kleikamp 		}
1496f8279621SKumar Gala 
1497f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
14983bffb652SDave Kleikamp 	} else
14993bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
150014cf11afSPaul Mackerras }
1501172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
150214cf11afSPaul Mackerras 
150314cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
150414cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
150514cf11afSPaul Mackerras {
150614cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
150714cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
150814cf11afSPaul Mackerras }
150914cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
151014cf11afSPaul Mackerras 
151114cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1512dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
151314cf11afSPaul Mackerras {
151414cf11afSPaul Mackerras 	int err;
151514cf11afSPaul Mackerras 
151614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
151714cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
151814cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
15198dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
152014cf11afSPaul Mackerras 	}
152114cf11afSPaul Mackerras 
1522dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1523dc1c1ca3SStephen Rothwell 
1524eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
152514cf11afSPaul Mackerras 	err = emulate_altivec(regs);
152614cf11afSPaul Mackerras 	if (err == 0) {
152714cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
152814cf11afSPaul Mackerras 		emulate_single_step(regs);
152914cf11afSPaul Mackerras 		return;
153014cf11afSPaul Mackerras 	}
153114cf11afSPaul Mackerras 
153214cf11afSPaul Mackerras 	if (err == -EFAULT) {
153314cf11afSPaul Mackerras 		/* got an error reading the instruction */
153414cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
153514cf11afSPaul Mackerras 	} else {
153614cf11afSPaul Mackerras 		/* didn't recognize the instruction */
153714cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
153876462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
153914cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
154014cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
154114cf11afSPaul Mackerras 	}
154214cf11afSPaul Mackerras }
154314cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
154414cf11afSPaul Mackerras 
1545ce48b210SMichael Neuling #ifdef CONFIG_VSX
1546ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1547ce48b210SMichael Neuling {
1548ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1549ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1550ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1551ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1552ce48b210SMichael Neuling 	}
1553ce48b210SMichael Neuling 
1554ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1555ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1556ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1557ce48b210SMichael Neuling }
1558ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1559ce48b210SMichael Neuling 
156014cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
156114cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
156214cf11afSPaul Mackerras 			   unsigned long error_code)
156314cf11afSPaul Mackerras {
156414cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
156514cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
156614cf11afSPaul Mackerras 	 * something smarter
156714cf11afSPaul Mackerras 	 */
156814cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
156914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
157014cf11afSPaul Mackerras 	return;
157114cf11afSPaul Mackerras }
157214cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
157314cf11afSPaul Mackerras 
157414cf11afSPaul Mackerras #ifdef CONFIG_SPE
157514cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
157614cf11afSPaul Mackerras {
15776a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
157814cf11afSPaul Mackerras 	unsigned long spefscr;
157914cf11afSPaul Mackerras 	int fpexc_mode;
158014cf11afSPaul Mackerras 	int code = 0;
15816a800f36SLiu Yu 	int err;
15826a800f36SLiu Yu 
1583685659eeSyu liu 	flush_spe_to_thread(current);
158414cf11afSPaul Mackerras 
158514cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
158614cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
158714cf11afSPaul Mackerras 
158814cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
158914cf11afSPaul Mackerras 		code = FPE_FLTOVF;
159014cf11afSPaul Mackerras 	}
159114cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
159214cf11afSPaul Mackerras 		code = FPE_FLTUND;
159314cf11afSPaul Mackerras 	}
159414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
159514cf11afSPaul Mackerras 		code = FPE_FLTDIV;
159614cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
159714cf11afSPaul Mackerras 		code = FPE_FLTINV;
159814cf11afSPaul Mackerras 	}
159914cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
160014cf11afSPaul Mackerras 		code = FPE_FLTRES;
160114cf11afSPaul Mackerras 
16026a800f36SLiu Yu 	err = do_spe_mathemu(regs);
16036a800f36SLiu Yu 	if (err == 0) {
16046a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
16056a800f36SLiu Yu 		emulate_single_step(regs);
160614cf11afSPaul Mackerras 		return;
160714cf11afSPaul Mackerras 	}
16086a800f36SLiu Yu 
16096a800f36SLiu Yu 	if (err == -EFAULT) {
16106a800f36SLiu Yu 		/* got an error reading the instruction */
16116a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
16126a800f36SLiu Yu 	} else if (err == -EINVAL) {
16136a800f36SLiu Yu 		/* didn't recognize the instruction */
16146a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
16156a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
16166a800f36SLiu Yu 	} else {
16176a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
16186a800f36SLiu Yu 	}
16196a800f36SLiu Yu 
16206a800f36SLiu Yu 	return;
16216a800f36SLiu Yu }
16226a800f36SLiu Yu 
16236a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
16246a800f36SLiu Yu {
16256a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
16266a800f36SLiu Yu 	int err;
16276a800f36SLiu Yu 
16286a800f36SLiu Yu 	preempt_disable();
16296a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
16306a800f36SLiu Yu 		giveup_spe(current);
16316a800f36SLiu Yu 	preempt_enable();
16326a800f36SLiu Yu 
16336a800f36SLiu Yu 	regs->nip -= 4;
16346a800f36SLiu Yu 	err = speround_handler(regs);
16356a800f36SLiu Yu 	if (err == 0) {
16366a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
16376a800f36SLiu Yu 		emulate_single_step(regs);
16386a800f36SLiu Yu 		return;
16396a800f36SLiu Yu 	}
16406a800f36SLiu Yu 
16416a800f36SLiu Yu 	if (err == -EFAULT) {
16426a800f36SLiu Yu 		/* got an error reading the instruction */
16436a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
16446a800f36SLiu Yu 	} else if (err == -EINVAL) {
16456a800f36SLiu Yu 		/* didn't recognize the instruction */
16466a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
16476a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
16486a800f36SLiu Yu 	} else {
16496a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
16506a800f36SLiu Yu 		return;
16516a800f36SLiu Yu 	}
16526a800f36SLiu Yu }
165314cf11afSPaul Mackerras #endif
165414cf11afSPaul Mackerras 
1655dc1c1ca3SStephen Rothwell /*
1656dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1657dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1658dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1659dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1660dc1c1ca3SStephen Rothwell  */
1661dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1662dc1c1ca3SStephen Rothwell {
1663dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1664dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1665dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1666dc1c1ca3SStephen Rothwell }
1667dc1c1ca3SStephen Rothwell 
16681e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
166914cf11afSPaul Mackerras /*
167014cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
167114cf11afSPaul Mackerras  * spins until a reboot occurs
167214cf11afSPaul Mackerras  */
167314cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
167414cf11afSPaul Mackerras {
167514cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
167614cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
167714cf11afSPaul Mackerras 	return;
167814cf11afSPaul Mackerras }
167914cf11afSPaul Mackerras 
168014cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
168114cf11afSPaul Mackerras {
168214cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
168314cf11afSPaul Mackerras 	WatchdogHandler(regs);
168414cf11afSPaul Mackerras }
168514cf11afSPaul Mackerras #endif
1686dc1c1ca3SStephen Rothwell 
1687dc1c1ca3SStephen Rothwell /*
1688dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1689dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1690dc1c1ca3SStephen Rothwell  */
1691dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1692dc1c1ca3SStephen Rothwell {
1693dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1694dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1695dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1696dc1c1ca3SStephen Rothwell }
169714cf11afSPaul Mackerras 
169814cf11afSPaul Mackerras void __init trap_init(void)
169914cf11afSPaul Mackerras {
170014cf11afSPaul Mackerras }
170180947e7cSGeert Uytterhoeven 
170280947e7cSGeert Uytterhoeven 
170380947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
170480947e7cSGeert Uytterhoeven 
170580947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
170680947e7cSGeert Uytterhoeven 
170780947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
170880947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
170980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
171080947e7cSGeert Uytterhoeven #endif
171180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
171280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
171380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
171480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
171580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
171680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
171780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
171880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
171980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
172080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
172180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
172280947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
172380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
172480947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
172580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
172680947e7cSGeert Uytterhoeven #endif
172780947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
172880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
172980947e7cSGeert Uytterhoeven #endif
1730efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1731efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1732efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1733efcac658SAlexey Kardashevskiy #endif
173480947e7cSGeert Uytterhoeven };
173580947e7cSGeert Uytterhoeven 
173680947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
173780947e7cSGeert Uytterhoeven 
173880947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
173980947e7cSGeert Uytterhoeven {
174076462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
174180947e7cSGeert Uytterhoeven 			    type);
174280947e7cSGeert Uytterhoeven }
174380947e7cSGeert Uytterhoeven 
174480947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
174580947e7cSGeert Uytterhoeven {
174680947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
174780947e7cSGeert Uytterhoeven 	unsigned int i;
174880947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
174980947e7cSGeert Uytterhoeven 
175080947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
175180947e7cSGeert Uytterhoeven 		return -ENODEV;
175280947e7cSGeert Uytterhoeven 
175380947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
175480947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
175580947e7cSGeert Uytterhoeven 	if (!dir)
175680947e7cSGeert Uytterhoeven 		return -ENOMEM;
175780947e7cSGeert Uytterhoeven 
175880947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
175980947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
176080947e7cSGeert Uytterhoeven 	if (!d)
176180947e7cSGeert Uytterhoeven 		goto fail;
176280947e7cSGeert Uytterhoeven 
176380947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
176480947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
176580947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
176680947e7cSGeert Uytterhoeven 		if (!d)
176780947e7cSGeert Uytterhoeven 			goto fail;
176880947e7cSGeert Uytterhoeven 	}
176980947e7cSGeert Uytterhoeven 
177080947e7cSGeert Uytterhoeven 	return 0;
177180947e7cSGeert Uytterhoeven 
177280947e7cSGeert Uytterhoeven fail:
177380947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
177480947e7cSGeert Uytterhoeven 	return -ENOMEM;
177580947e7cSGeert Uytterhoeven }
177680947e7cSGeert Uytterhoeven 
177780947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
177880947e7cSGeert Uytterhoeven 
177980947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1780