xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 58154c8ce71a7854d969d73468fd00e5eeeab708)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
614cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
714cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
814cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1114cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras /*
1514cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
2014cf11afSPaul Mackerras #include <linux/kernel.h>
2114cf11afSPaul Mackerras #include <linux/mm.h>
2214cf11afSPaul Mackerras #include <linux/stddef.h>
2314cf11afSPaul Mackerras #include <linux/unistd.h>
248dad3f92SPaul Mackerras #include <linux/ptrace.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/interrupt.h>
2714cf11afSPaul Mackerras #include <linux/init.h>
2814cf11afSPaul Mackerras #include <linux/module.h>
298dad3f92SPaul Mackerras #include <linux/prctl.h>
3014cf11afSPaul Mackerras #include <linux/delay.h>
3114cf11afSPaul Mackerras #include <linux/kprobes.h>
32cc532915SMichael Ellerman #include <linux/kexec.h>
335474c120SMichael Hanselmann #include <linux/backlight.h>
3473c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
351eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3680947e7cSGeert Uytterhoeven #include <linux/debugfs.h>
3776462232SChristian Dietrich #include <linux/ratelimit.h>
3814cf11afSPaul Mackerras 
3980947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4014cf11afSPaul Mackerras #include <asm/pgtable.h>
4114cf11afSPaul Mackerras #include <asm/uaccess.h>
4214cf11afSPaul Mackerras #include <asm/system.h>
4314cf11afSPaul Mackerras #include <asm/io.h>
4486417780SPaul Mackerras #include <asm/machdep.h>
4586417780SPaul Mackerras #include <asm/rtas.h>
46f7f6f4feSDavid Gibson #include <asm/pmc.h>
47dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4814cf11afSPaul Mackerras #include <asm/reg.h>
4986417780SPaul Mackerras #endif
5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5114cf11afSPaul Mackerras #include <asm/backlight.h>
5214cf11afSPaul Mackerras #endif
53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5486417780SPaul Mackerras #include <asm/firmware.h>
55dc1c1ca3SStephen Rothwell #include <asm/processor.h>
56dc1c1ca3SStephen Rothwell #endif
57c0ce7d08SDavid Wilder #include <asm/kexec.h>
5816c57b36SKumar Gala #include <asm/ppc-opcode.h>
59cce1f106SShaohui Xie #include <asm/rio.h>
60dc1c1ca3SStephen Rothwell 
617dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
625be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
635be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
645be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
655be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
675be3492fSAnton Blanchard int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
685be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
6914cf11afSPaul Mackerras 
7014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7714cf11afSPaul Mackerras #endif
7814cf11afSPaul Mackerras 
7914cf11afSPaul Mackerras /*
8014cf11afSPaul Mackerras  * Trap & Exception support
8114cf11afSPaul Mackerras  */
8214cf11afSPaul Mackerras 
836031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
846031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
856031d9d9Santon@samba.org {
866031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
876031d9d9Santon@samba.org 	if (pmac_backlight) {
886031d9d9Santon@samba.org 		struct backlight_properties *props;
896031d9d9Santon@samba.org 
906031d9d9Santon@samba.org 		props = &pmac_backlight->props;
916031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
926031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
936031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
946031d9d9Santon@samba.org 	}
956031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
966031d9d9Santon@samba.org }
976031d9d9Santon@samba.org #else
986031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
996031d9d9Santon@samba.org #endif
1006031d9d9Santon@samba.org 
10114cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
10214cf11afSPaul Mackerras {
10334c2a14fSanton@samba.org 	static struct {
104b8f87782SThomas Gleixner 		raw_spinlock_t lock;
10534c2a14fSanton@samba.org 		u32 lock_owner;
10634c2a14fSanton@samba.org 		int lock_owner_depth;
10734c2a14fSanton@samba.org 	} die = {
108b8f87782SThomas Gleixner 		.lock =			__RAW_SPIN_LOCK_UNLOCKED(die.lock),
10934c2a14fSanton@samba.org 		.lock_owner =		-1,
11034c2a14fSanton@samba.org 		.lock_owner_depth =	0
11134c2a14fSanton@samba.org 	};
112c0ce7d08SDavid Wilder 	static int die_counter;
11334c2a14fSanton@samba.org 	unsigned long flags;
11414cf11afSPaul Mackerras 
11514cf11afSPaul Mackerras 	if (debugger(regs))
11614cf11afSPaul Mackerras 		return 1;
11714cf11afSPaul Mackerras 
118293e4688Santon@samba.org 	oops_enter();
119293e4688Santon@samba.org 
12034c2a14fSanton@samba.org 	if (die.lock_owner != raw_smp_processor_id()) {
12114cf11afSPaul Mackerras 		console_verbose();
122b8f87782SThomas Gleixner 		raw_spin_lock_irqsave(&die.lock, flags);
12334c2a14fSanton@samba.org 		die.lock_owner = smp_processor_id();
12434c2a14fSanton@samba.org 		die.lock_owner_depth = 0;
12514cf11afSPaul Mackerras 		bust_spinlocks(1);
1266031d9d9Santon@samba.org 		if (machine_is(powermac))
1276031d9d9Santon@samba.org 			pmac_backlight_unblank();
12834c2a14fSanton@samba.org 	} else {
12934c2a14fSanton@samba.org 		local_save_flags(flags);
13034c2a14fSanton@samba.org 	}
1315474c120SMichael Hanselmann 
13234c2a14fSanton@samba.org 	if (++die.lock_owner_depth < 3) {
13314cf11afSPaul Mackerras 		printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
13414cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
13514cf11afSPaul Mackerras 		printk("PREEMPT ");
13614cf11afSPaul Mackerras #endif
13714cf11afSPaul Mackerras #ifdef CONFIG_SMP
13814cf11afSPaul Mackerras 		printk("SMP NR_CPUS=%d ", NR_CPUS);
13914cf11afSPaul Mackerras #endif
14014cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
14114cf11afSPaul Mackerras 		printk("DEBUG_PAGEALLOC ");
14214cf11afSPaul Mackerras #endif
14314cf11afSPaul Mackerras #ifdef CONFIG_NUMA
14414cf11afSPaul Mackerras 		printk("NUMA ");
14514cf11afSPaul Mackerras #endif
146ae7f4463Santon@samba.org 		printk("%s\n", ppc_md.name ? ppc_md.name : "");
147e8222502SBenjamin Herrenschmidt 
14866fcb105SAnton Blanchard 		if (notify_die(DIE_OOPS, str, regs, err, 255,
14966fcb105SAnton Blanchard 			       SIGSEGV) == NOTIFY_STOP)
15066fcb105SAnton Blanchard 			return 1;
15166fcb105SAnton Blanchard 
15214cf11afSPaul Mackerras 		print_modules();
15314cf11afSPaul Mackerras 		show_regs(regs);
15434c2a14fSanton@samba.org 	} else {
15534c2a14fSanton@samba.org 		printk("Recursive die() failure, output suppressed\n");
15634c2a14fSanton@samba.org 	}
15734c2a14fSanton@samba.org 
15814cf11afSPaul Mackerras 	bust_spinlocks(0);
15934c2a14fSanton@samba.org 	die.lock_owner = -1;
160bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
161*58154c8cSAnton Blanchard 	oops_exit();
162*58154c8cSAnton Blanchard 	printk("\n");
163b8f87782SThomas Gleixner 	raw_spin_unlock_irqrestore(&die.lock, flags);
164cc532915SMichael Ellerman 
165c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
166c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
167cc532915SMichael Ellerman 		crash_kexec(regs);
168c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
16914cf11afSPaul Mackerras 
170*58154c8cSAnton Blanchard 	/*
171*58154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
172*58154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
173*58154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
174*58154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
175*58154c8cSAnton Blanchard 	 */
176*58154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
177*58154c8cSAnton Blanchard 	    is_global_init(current)) {
178*58154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
179*58154c8cSAnton Blanchard 	}
180*58154c8cSAnton Blanchard 
18114cf11afSPaul Mackerras 	if (in_interrupt())
18214cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
18314cf11afSPaul Mackerras 
184cea6a4baSHorms 	if (panic_on_oops)
185012c437dSHorms 		panic("Fatal exception");
186cea6a4baSHorms 
18714cf11afSPaul Mackerras 	do_exit(err);
18814cf11afSPaul Mackerras 
18914cf11afSPaul Mackerras 	return 0;
19014cf11afSPaul Mackerras }
19114cf11afSPaul Mackerras 
19225baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
19325baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
19425baa35bSOleg Nesterov {
19525baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
19625baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
19725baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
19825baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
19925baa35bSOleg Nesterov }
20025baa35bSOleg Nesterov 
20114cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
20214cf11afSPaul Mackerras {
20314cf11afSPaul Mackerras 	siginfo_t info;
204d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
205d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
206d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
207d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
20814cf11afSPaul Mackerras 
20914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
21014cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
21114cf11afSPaul Mackerras 			return;
212d0c3d534SOlof Johansson 	} else if (show_unhandled_signals &&
21376462232SChristian Dietrich 		   unhandled_signal(current, signr)) {
21476462232SChristian Dietrich 		printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
215d0c3d534SOlof Johansson 				   current->comm, current->pid, signr,
216d0c3d534SOlof Johansson 				   addr, regs->nip, regs->link, code);
21714cf11afSPaul Mackerras 	}
21814cf11afSPaul Mackerras 
21914cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
22014cf11afSPaul Mackerras 	info.si_signo = signr;
22114cf11afSPaul Mackerras 	info.si_code = code;
22214cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
22314cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
22414cf11afSPaul Mackerras }
22514cf11afSPaul Mackerras 
22614cf11afSPaul Mackerras #ifdef CONFIG_PPC64
22714cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
22814cf11afSPaul Mackerras {
22914cf11afSPaul Mackerras 	/* See if any machine dependent calls */
230c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
231c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
232c902be71SArnd Bergmann 			return;
233c902be71SArnd Bergmann 	}
23414cf11afSPaul Mackerras 
235c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
236104699c0SKOSAKI Motohiro 	cpumask_set_cpu(smp_processor_id(), &cpus_in_sr);
237c0ce7d08SDavid Wilder #endif
238c0ce7d08SDavid Wilder 
2398dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
24014cf11afSPaul Mackerras 
241eac8392fSDavid Wilder 	/*
242eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
243eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
244eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
245eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
246eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
247eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
248eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
249eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
250eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
251eac8392fSDavid Wilder 	 */
252eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
253eac8392fSDavid Wilder 
25414cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
25514cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
25614cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
25714cf11afSPaul Mackerras 
25814cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
25914cf11afSPaul Mackerras }
26014cf11afSPaul Mackerras #endif
26114cf11afSPaul Mackerras 
26214cf11afSPaul Mackerras /*
26314cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
26414cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
26514cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
26614cf11afSPaul Mackerras  * table.
26714cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
26814cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
26914cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
27014cf11afSPaul Mackerras  *  -- paulus.
27114cf11afSPaul Mackerras  */
27214cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
27314cf11afSPaul Mackerras {
27468a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
27514cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
27614cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
27714cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
27814cf11afSPaul Mackerras 
27914cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
28014cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
28114cf11afSPaul Mackerras 		/*
28214cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
28314cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
28414cf11afSPaul Mackerras 		 * As the address is in the exception table
28514cf11afSPaul Mackerras 		 * we should be able to read the instr there.
28614cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
28714cf11afSPaul Mackerras 		 * load or store.
28814cf11afSPaul Mackerras 		 */
28914cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
29014cf11afSPaul Mackerras 			nip -= 2;
29114cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
29214cf11afSPaul Mackerras 			--nip;
29314cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
29414cf11afSPaul Mackerras 			/* sync or twi */
29514cf11afSPaul Mackerras 			unsigned int rb;
29614cf11afSPaul Mackerras 
29714cf11afSPaul Mackerras 			--nip;
29814cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
29914cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
30014cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
30114cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
30214cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
30314cf11afSPaul Mackerras 			regs->nip = entry->fixup;
30414cf11afSPaul Mackerras 			return 1;
30514cf11afSPaul Mackerras 		}
30614cf11afSPaul Mackerras 	}
30768a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
30814cf11afSPaul Mackerras 	return 0;
30914cf11afSPaul Mackerras }
31014cf11afSPaul Mackerras 
311172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
31214cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
31314cf11afSPaul Mackerras    is in the ESR. */
31414cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
31514cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
31614cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
31714cf11afSPaul Mackerras #else
318fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
31914cf11afSPaul Mackerras #endif
32014cf11afSPaul Mackerras #define REASON_FP		ESR_FP
32114cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
32214cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
32314cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
32414cf11afSPaul Mackerras 
32514cf11afSPaul Mackerras /* single-step stuff */
32614cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
32714cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
32814cf11afSPaul Mackerras 
32914cf11afSPaul Mackerras #else
33014cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
33114cf11afSPaul Mackerras    exception is in the MSR. */
33214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
33314cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
33414cf11afSPaul Mackerras #define REASON_FP		0x100000
33514cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
33614cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
33714cf11afSPaul Mackerras #define REASON_TRAP		0x20000
33814cf11afSPaul Mackerras 
33914cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
34014cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
34114cf11afSPaul Mackerras #endif
34214cf11afSPaul Mackerras 
34347c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
34447c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
34514cf11afSPaul Mackerras {
3461a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
34714cf11afSPaul Mackerras 
34814cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
34914cf11afSPaul Mackerras 		printk("Instruction");
35014cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35114cf11afSPaul Mackerras 	} else
35214cf11afSPaul Mackerras 		printk("Data");
35314cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
35447c0bd1aSBenjamin Herrenschmidt 
35547c0bd1aSBenjamin Herrenschmidt 	return 0;
35647c0bd1aSBenjamin Herrenschmidt }
35747c0bd1aSBenjamin Herrenschmidt 
35847c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
35947c0bd1aSBenjamin Herrenschmidt {
36047c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
36147c0bd1aSBenjamin Herrenschmidt 
36214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
36314cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
36414cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
36514cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
36614cf11afSPaul Mackerras 	}
36714cf11afSPaul Mackerras 	else {
36814cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
36914cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
37014cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
37114cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
37214cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
37314cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
37414cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
37514cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
37614cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
37714cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
37814cf11afSPaul Mackerras 			flush_instruction_cache();
37914cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
38014cf11afSPaul Mackerras 		}
38114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
38214cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
38314cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
38414cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
38514cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
38614cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
38714cf11afSPaul Mackerras 
38814cf11afSPaul Mackerras 		/* Clear MCSR */
38914cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
39014cf11afSPaul Mackerras 	}
39147c0bd1aSBenjamin Herrenschmidt 	return 0;
39247c0bd1aSBenjamin Herrenschmidt }
393fc5e7097SDave Kleikamp 
394fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
395fc5e7097SDave Kleikamp {
396fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
397fc5e7097SDave Kleikamp 	u32 mcsr;
398fc5e7097SDave Kleikamp 
399fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
400fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
401fc5e7097SDave Kleikamp 		printk(KERN_ERR
402fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
403fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
404fc5e7097SDave Kleikamp 		return 0;
405fc5e7097SDave Kleikamp 	}
406fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
407fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
408fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
409fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
410fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
411fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
412fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
413fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
414fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
415fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
416fc5e7097SDave Kleikamp 		flush_instruction_cache();
417fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
418fc5e7097SDave Kleikamp 	}
419fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
420fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
421fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
422fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
423fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
424fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
425fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
426fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
427fc5e7097SDave Kleikamp 
428fc5e7097SDave Kleikamp 	/* Clear MCSR */
429fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
430fc5e7097SDave Kleikamp 
431fc5e7097SDave Kleikamp 	return 0;
432fc5e7097SDave Kleikamp }
43314cf11afSPaul Mackerras #elif defined(CONFIG_E500)
434fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
435fe04b112SScott Wood {
436fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
437fe04b112SScott Wood 	unsigned long reason = mcsr;
438fe04b112SScott Wood 	int recoverable = 1;
439fe04b112SScott Wood 
44082a9a480SScott Wood 	if (reason & MCSR_LD) {
441cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
442cce1f106SShaohui Xie 		if (recoverable == 1)
443cce1f106SShaohui Xie 			goto silent_out;
444cce1f106SShaohui Xie 	}
445cce1f106SShaohui Xie 
446fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
447fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
448fe04b112SScott Wood 
449fe04b112SScott Wood 	if (reason & MCSR_MCP)
450fe04b112SScott Wood 		printk("Machine Check Signal\n");
451fe04b112SScott Wood 
452fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
453fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
454fe04b112SScott Wood 
455fe04b112SScott Wood 		/*
456fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
457fe04b112SScott Wood 		 */
458fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
459fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
460fe04b112SScott Wood 			;
461fe04b112SScott Wood 
462fe04b112SScott Wood 		/*
463fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
464fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
465fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
466fe04b112SScott Wood 		 */
467fe04b112SScott Wood 		reason &= ~MCSR_IF;
468fe04b112SScott Wood 	}
469fe04b112SScott Wood 
470fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
471fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
47237caf9f2SKumar Gala 
47337caf9f2SKumar Gala 		/*
47437caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
47537caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
47637caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
47737caf9f2SKumar Gala 		 */
47837caf9f2SKumar Gala 		if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
479fe04b112SScott Wood 			recoverable = 0;
480fe04b112SScott Wood 	}
481fe04b112SScott Wood 
482fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
483fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
484fe04b112SScott Wood 		recoverable = 0;
485fe04b112SScott Wood 	}
486fe04b112SScott Wood 
487fe04b112SScott Wood 	if (reason & MCSR_NMI)
488fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
489fe04b112SScott Wood 
490fe04b112SScott Wood 	if (reason & MCSR_IF) {
491fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
492fe04b112SScott Wood 		recoverable = 0;
493fe04b112SScott Wood 	}
494fe04b112SScott Wood 
495fe04b112SScott Wood 	if (reason & MCSR_LD) {
496fe04b112SScott Wood 		printk("Load Error Report\n");
497fe04b112SScott Wood 		recoverable = 0;
498fe04b112SScott Wood 	}
499fe04b112SScott Wood 
500fe04b112SScott Wood 	if (reason & MCSR_ST) {
501fe04b112SScott Wood 		printk("Store Error Report\n");
502fe04b112SScott Wood 		recoverable = 0;
503fe04b112SScott Wood 	}
504fe04b112SScott Wood 
505fe04b112SScott Wood 	if (reason & MCSR_LDG) {
506fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
507fe04b112SScott Wood 		recoverable = 0;
508fe04b112SScott Wood 	}
509fe04b112SScott Wood 
510fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
511fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
512fe04b112SScott Wood 
513fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
514fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
515fe04b112SScott Wood 		recoverable = 0;
516fe04b112SScott Wood 	}
517fe04b112SScott Wood 
518fe04b112SScott Wood 	if (reason & MCSR_MAV) {
519fe04b112SScott Wood 		u64 addr;
520fe04b112SScott Wood 
521fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
522fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
523fe04b112SScott Wood 
524fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
525fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
526fe04b112SScott Wood 	}
527fe04b112SScott Wood 
528cce1f106SShaohui Xie silent_out:
529fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
530fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
531fe04b112SScott Wood }
532fe04b112SScott Wood 
53347c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
53447c0bd1aSBenjamin Herrenschmidt {
53547c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
53647c0bd1aSBenjamin Herrenschmidt 
537cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
538cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
539cce1f106SShaohui Xie 			return 1;
540cce1f106SShaohui Xie 	}
541cce1f106SShaohui Xie 
54214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
54314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
54414cf11afSPaul Mackerras 
54514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
54614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
54714cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
54814cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
54914cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
55014cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
55114cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
55214cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
55314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
55414cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
55514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
55614cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
55714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
55814cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
55914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
56014cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
56114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
56214cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
56314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
56414cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
56514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
56614cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
56714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
56814cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
56947c0bd1aSBenjamin Herrenschmidt 
57047c0bd1aSBenjamin Herrenschmidt 	return 0;
57147c0bd1aSBenjamin Herrenschmidt }
5724490c06bSKumar Gala 
5734490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5744490c06bSKumar Gala {
5754490c06bSKumar Gala 	return 0;
5764490c06bSKumar Gala }
57714cf11afSPaul Mackerras #elif defined(CONFIG_E200)
57847c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
57947c0bd1aSBenjamin Herrenschmidt {
58047c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
58147c0bd1aSBenjamin Herrenschmidt 
58214cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
58314cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
58414cf11afSPaul Mackerras 
58514cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
58614cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
58714cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
58814cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
58914cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
59014cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
59114cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
59214cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
59314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
59414cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
59514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
59614cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
59714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
59814cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
59947c0bd1aSBenjamin Herrenschmidt 
60047c0bd1aSBenjamin Herrenschmidt 	return 0;
60147c0bd1aSBenjamin Herrenschmidt }
60247c0bd1aSBenjamin Herrenschmidt #else
60347c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
60447c0bd1aSBenjamin Herrenschmidt {
60547c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
60647c0bd1aSBenjamin Herrenschmidt 
60714cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
60814cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
60914cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
61014cf11afSPaul Mackerras 	case 0x80000:
61114cf11afSPaul Mackerras 		printk("Machine check signal\n");
61214cf11afSPaul Mackerras 		break;
61314cf11afSPaul Mackerras 	case 0:		/* for 601 */
61414cf11afSPaul Mackerras 	case 0x40000:
61514cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
61614cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
61714cf11afSPaul Mackerras 		break;
61814cf11afSPaul Mackerras 	case 0x20000:
61914cf11afSPaul Mackerras 		printk("Data parity error signal\n");
62014cf11afSPaul Mackerras 		break;
62114cf11afSPaul Mackerras 	case 0x10000:
62214cf11afSPaul Mackerras 		printk("Address parity error signal\n");
62314cf11afSPaul Mackerras 		break;
62414cf11afSPaul Mackerras 	case 0x20000000:
62514cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
62614cf11afSPaul Mackerras 		break;
62714cf11afSPaul Mackerras 	case 0x40000000:
62814cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
62914cf11afSPaul Mackerras 		break;
63014cf11afSPaul Mackerras 	case 0x00100000:
63114cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
63214cf11afSPaul Mackerras 		break;
63314cf11afSPaul Mackerras 	default:
63414cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
63514cf11afSPaul Mackerras 	}
63675918a4bSOlof Johansson 	return 0;
63775918a4bSOlof Johansson }
63847c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
63975918a4bSOlof Johansson 
64075918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
64175918a4bSOlof Johansson {
64275918a4bSOlof Johansson 	int recover = 0;
64375918a4bSOlof Johansson 
64489713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
64589713ed1SAnton Blanchard 
64647c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
64747c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
64847c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
64947c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
65047c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
65147c0bd1aSBenjamin Herrenschmidt 	 */
65275918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
65375918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
65447c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
65547c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
65675918a4bSOlof Johansson 
65747c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
65875918a4bSOlof Johansson 		return;
65975918a4bSOlof Johansson 
66075918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
66147c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
66247c0bd1aSBenjamin Herrenschmidt 	 *
66347c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
66447c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
66547c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
66647c0bd1aSBenjamin Herrenschmidt 	 */
66775918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
66875918a4bSOlof Johansson 	return;
66975918a4bSOlof Johansson #endif
67075918a4bSOlof Johansson 
671a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
67275918a4bSOlof Johansson 		return;
67375918a4bSOlof Johansson 
67475918a4bSOlof Johansson 	if (check_io_access(regs))
67575918a4bSOlof Johansson 		return;
67675918a4bSOlof Johansson 
6778dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
67814cf11afSPaul Mackerras 
67914cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
68014cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
68114cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
68214cf11afSPaul Mackerras }
68314cf11afSPaul Mackerras 
68414cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
68514cf11afSPaul Mackerras {
68614cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
68714cf11afSPaul Mackerras }
68814cf11afSPaul Mackerras 
689dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
69014cf11afSPaul Mackerras {
69114cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
69214cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
69314cf11afSPaul Mackerras 
69414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
69514cf11afSPaul Mackerras }
69614cf11afSPaul Mackerras 
697dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
69814cf11afSPaul Mackerras {
69914cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
70014cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
70114cf11afSPaul Mackerras 		return;
70214cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
70314cf11afSPaul Mackerras 		return;
70414cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
70514cf11afSPaul Mackerras }
70614cf11afSPaul Mackerras 
70714cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
70814cf11afSPaul Mackerras {
70914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
71014cf11afSPaul Mackerras }
71114cf11afSPaul Mackerras 
7128dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
71314cf11afSPaul Mackerras {
7142538c2d0SK.Prasad 	clear_single_step(regs);
71514cf11afSPaul Mackerras 
71614cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
71714cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
71814cf11afSPaul Mackerras 		return;
71914cf11afSPaul Mackerras 	if (debugger_sstep(regs))
72014cf11afSPaul Mackerras 		return;
72114cf11afSPaul Mackerras 
72214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
72314cf11afSPaul Mackerras }
72414cf11afSPaul Mackerras 
72514cf11afSPaul Mackerras /*
72614cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
72714cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
72814cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
72914cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
73014cf11afSPaul Mackerras  */
7318dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
73214cf11afSPaul Mackerras {
7332538c2d0SK.Prasad 	if (single_stepping(regs))
7342538c2d0SK.Prasad 		single_step_exception(regs);
73514cf11afSPaul Mackerras }
73614cf11afSPaul Mackerras 
7375fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
738dc1c1ca3SStephen Rothwell {
7395fad293bSKumar Gala 	int ret = 0;
740dc1c1ca3SStephen Rothwell 
741dc1c1ca3SStephen Rothwell 	/* Invalid operation */
742dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7435fad293bSKumar Gala 		ret = FPE_FLTINV;
744dc1c1ca3SStephen Rothwell 
745dc1c1ca3SStephen Rothwell 	/* Overflow */
746dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7475fad293bSKumar Gala 		ret = FPE_FLTOVF;
748dc1c1ca3SStephen Rothwell 
749dc1c1ca3SStephen Rothwell 	/* Underflow */
750dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7515fad293bSKumar Gala 		ret = FPE_FLTUND;
752dc1c1ca3SStephen Rothwell 
753dc1c1ca3SStephen Rothwell 	/* Divide by zero */
754dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7555fad293bSKumar Gala 		ret = FPE_FLTDIV;
756dc1c1ca3SStephen Rothwell 
757dc1c1ca3SStephen Rothwell 	/* Inexact result */
758dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7595fad293bSKumar Gala 		ret = FPE_FLTRES;
7605fad293bSKumar Gala 
7615fad293bSKumar Gala 	return ret;
7625fad293bSKumar Gala }
7635fad293bSKumar Gala 
7645fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7655fad293bSKumar Gala {
7665fad293bSKumar Gala 	int code = 0;
7675fad293bSKumar Gala 
7685fad293bSKumar Gala 	flush_fp_to_thread(current);
7695fad293bSKumar Gala 
7705fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
771dc1c1ca3SStephen Rothwell 
772dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
773dc1c1ca3SStephen Rothwell }
774dc1c1ca3SStephen Rothwell 
775dc1c1ca3SStephen Rothwell /*
776dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
77714cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
77814cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
77914cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
78014cf11afSPaul Mackerras  *
78114cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
78214cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
78314cf11afSPaul Mackerras  * bits is faster and easier.
78486417780SPaul Mackerras  *
78514cf11afSPaul Mackerras  */
78614cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
78714cf11afSPaul Mackerras {
78814cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
78914cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
79014cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
79114cf11afSPaul Mackerras 	u32 num_bytes;
79214cf11afSPaul Mackerras 	unsigned long EA;
79314cf11afSPaul Mackerras 	int pos = 0;
79414cf11afSPaul Mackerras 
79514cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
79616c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
79714cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
79814cf11afSPaul Mackerras 			return -EINVAL;
79914cf11afSPaul Mackerras 
80014cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
80114cf11afSPaul Mackerras 
80216c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
80316c57b36SKumar Gala 		case PPC_INST_LSWX:
80416c57b36SKumar Gala 		case PPC_INST_STSWX:
80514cf11afSPaul Mackerras 			EA += NB_RB;
80614cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
80714cf11afSPaul Mackerras 			break;
80816c57b36SKumar Gala 		case PPC_INST_LSWI:
80916c57b36SKumar Gala 		case PPC_INST_STSWI:
81014cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
81114cf11afSPaul Mackerras 			break;
81214cf11afSPaul Mackerras 		default:
81314cf11afSPaul Mackerras 			return -EINVAL;
81414cf11afSPaul Mackerras 	}
81514cf11afSPaul Mackerras 
81614cf11afSPaul Mackerras 	while (num_bytes != 0)
81714cf11afSPaul Mackerras 	{
81814cf11afSPaul Mackerras 		u8 val;
81914cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
82014cf11afSPaul Mackerras 
82116c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
82216c57b36SKumar Gala 			case PPC_INST_LSWX:
82316c57b36SKumar Gala 			case PPC_INST_LSWI:
82414cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
82514cf11afSPaul Mackerras 					return -EFAULT;
82614cf11afSPaul Mackerras 				/* first time updating this reg,
82714cf11afSPaul Mackerras 				 * zero it out */
82814cf11afSPaul Mackerras 				if (pos == 0)
82914cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
83014cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
83114cf11afSPaul Mackerras 				break;
83216c57b36SKumar Gala 			case PPC_INST_STSWI:
83316c57b36SKumar Gala 			case PPC_INST_STSWX:
83414cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
83514cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
83614cf11afSPaul Mackerras 					return -EFAULT;
83714cf11afSPaul Mackerras 				break;
83814cf11afSPaul Mackerras 		}
83914cf11afSPaul Mackerras 		/* move EA to next address */
84014cf11afSPaul Mackerras 		EA += 1;
84114cf11afSPaul Mackerras 		num_bytes--;
84214cf11afSPaul Mackerras 
84314cf11afSPaul Mackerras 		/* manage our position within the register */
84414cf11afSPaul Mackerras 		if (++pos == 4) {
84514cf11afSPaul Mackerras 			pos = 0;
84614cf11afSPaul Mackerras 			if (++rT == 32)
84714cf11afSPaul Mackerras 				rT = 0;
84814cf11afSPaul Mackerras 		}
84914cf11afSPaul Mackerras 	}
85014cf11afSPaul Mackerras 
85114cf11afSPaul Mackerras 	return 0;
85214cf11afSPaul Mackerras }
85314cf11afSPaul Mackerras 
854c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
855c3412dcbSWill Schmidt {
856c3412dcbSWill Schmidt 	u32 ra,rs;
857c3412dcbSWill Schmidt 	unsigned long tmp;
858c3412dcbSWill Schmidt 
859c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
860c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
861c3412dcbSWill Schmidt 
862c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
863c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
864c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
865c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
866c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
867c3412dcbSWill Schmidt 
868c3412dcbSWill Schmidt 	return 0;
869c3412dcbSWill Schmidt }
870c3412dcbSWill Schmidt 
871c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
872c1469f13SKumar Gala {
873c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
874c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
875c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
876c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
877c1469f13SKumar Gala 	u8 bit;
878c1469f13SKumar Gala 	unsigned long tmp;
879c1469f13SKumar Gala 
880c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
881c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
882c1469f13SKumar Gala 
883c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
884c1469f13SKumar Gala 
885c1469f13SKumar Gala 	return 0;
886c1469f13SKumar Gala }
887c1469f13SKumar Gala 
88814cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
88914cf11afSPaul Mackerras {
89014cf11afSPaul Mackerras 	u32 instword;
89114cf11afSPaul Mackerras 	u32 rd;
89214cf11afSPaul Mackerras 
893fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
89414cf11afSPaul Mackerras 		return -EINVAL;
89514cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
89614cf11afSPaul Mackerras 
89714cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
89814cf11afSPaul Mackerras 		return -EFAULT;
89914cf11afSPaul Mackerras 
90014cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
90116c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
902eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
90314cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
90414cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
90514cf11afSPaul Mackerras 		return 0;
90614cf11afSPaul Mackerras 	}
90714cf11afSPaul Mackerras 
90814cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
90980947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
910eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
91114cf11afSPaul Mackerras 		return 0;
91280947e7cSGeert Uytterhoeven 	}
91314cf11afSPaul Mackerras 
91414cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
91516c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
91686417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
91714cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
91814cf11afSPaul Mackerras 
919eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
92014cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
92114cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
92214cf11afSPaul Mackerras 		return 0;
92314cf11afSPaul Mackerras 	}
92414cf11afSPaul Mackerras 
92514cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
92680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
927eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
92814cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
92980947e7cSGeert Uytterhoeven 	}
93014cf11afSPaul Mackerras 
931c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
93216c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
933eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
934c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
935c3412dcbSWill Schmidt 	}
936c3412dcbSWill Schmidt 
937c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
93816c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
939eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
940c1469f13SKumar Gala 		return emulate_isel(regs, instword);
941c1469f13SKumar Gala 	}
942c1469f13SKumar Gala 
943efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
944efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
945efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
946efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
947efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
948efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
949efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
950efcac658SAlexey Kardashevskiy 		return 0;
951efcac658SAlexey Kardashevskiy 	}
952efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
953efcac658SAlexey Kardashevskiy 	if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
954efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
955efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
956efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
957efcac658SAlexey Kardashevskiy 		mtspr(SPRN_DSCR, regs->gpr[rd]);
958efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
959efcac658SAlexey Kardashevskiy 		return 0;
960efcac658SAlexey Kardashevskiy 	}
961efcac658SAlexey Kardashevskiy #endif
962efcac658SAlexey Kardashevskiy 
96314cf11afSPaul Mackerras 	return -EINVAL;
96414cf11afSPaul Mackerras }
96514cf11afSPaul Mackerras 
96673c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
96714cf11afSPaul Mackerras {
96873c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
96914cf11afSPaul Mackerras }
97014cf11afSPaul Mackerras 
9718dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
97214cf11afSPaul Mackerras {
97314cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
97414cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
97514cf11afSPaul Mackerras 
976aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
97704903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
97814cf11afSPaul Mackerras 
97914cf11afSPaul Mackerras 	if (reason & REASON_FP) {
98014cf11afSPaul Mackerras 		/* IEEE FP exception */
981dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
9828dad3f92SPaul Mackerras 		return;
9838dad3f92SPaul Mackerras 	}
9848dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
985ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
986ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
987ba797b28SJason Wessel 		if (debugger_bpt(regs))
988ba797b28SJason Wessel 			return;
989ba797b28SJason Wessel 
99014cf11afSPaul Mackerras 		/* trap exception */
991dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
992dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
993dc1c1ca3SStephen Rothwell 			return;
99473c9ceabSJeremy Fitzhardinge 
99573c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
996608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
99714cf11afSPaul Mackerras 			regs->nip += 4;
99814cf11afSPaul Mackerras 			return;
99914cf11afSPaul Mackerras 		}
10008dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
10018dad3f92SPaul Mackerras 		return;
10028dad3f92SPaul Mackerras 	}
10038dad3f92SPaul Mackerras 
1004cd8a5673SPaul Mackerras 	local_irq_enable();
1005cd8a5673SPaul Mackerras 
100604903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
100704903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
100804903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
100904903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
101004903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
101104903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
101204903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
101325985edcSLucas De Marchi 	 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
10145fad293bSKumar Gala 	switch (do_mathemu(regs)) {
10155fad293bSKumar Gala 	case 0:
101604903a30SKumar Gala 		emulate_single_step(regs);
101704903a30SKumar Gala 		return;
10185fad293bSKumar Gala 	case 1: {
10195fad293bSKumar Gala 			int code = 0;
10205fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
10215fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
10225fad293bSKumar Gala 			return;
102304903a30SKumar Gala 		}
10245fad293bSKumar Gala 	case -EFAULT:
10255fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10265fad293bSKumar Gala 		return;
10275fad293bSKumar Gala 	}
10285fad293bSKumar Gala 	/* fall through on any other errors */
102904903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
103004903a30SKumar Gala 
10318dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
10328dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
103314cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
103414cf11afSPaul Mackerras 		case 0:
103514cf11afSPaul Mackerras 			regs->nip += 4;
103614cf11afSPaul Mackerras 			emulate_single_step(regs);
10378dad3f92SPaul Mackerras 			return;
103814cf11afSPaul Mackerras 		case -EFAULT:
103914cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
10408dad3f92SPaul Mackerras 			return;
10418dad3f92SPaul Mackerras 		}
10428dad3f92SPaul Mackerras 	}
10438dad3f92SPaul Mackerras 
104414cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
104514cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
104614cf11afSPaul Mackerras 	else
104714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
104814cf11afSPaul Mackerras }
104914cf11afSPaul Mackerras 
1050dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
105114cf11afSPaul Mackerras {
10524393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
105314cf11afSPaul Mackerras 
1054e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1055e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
105614cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
105714cf11afSPaul Mackerras 
105814cf11afSPaul Mackerras 	if (fixed == 1) {
105914cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
106014cf11afSPaul Mackerras 		emulate_single_step(regs);
106114cf11afSPaul Mackerras 		return;
106214cf11afSPaul Mackerras 	}
106314cf11afSPaul Mackerras 
106414cf11afSPaul Mackerras 	/* Operand address was bad */
106514cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
10664393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
10674393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
10684393c4f6SBenjamin Herrenschmidt 	} else {
10694393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
10704393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
107114cf11afSPaul Mackerras 	}
10724393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
10734393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
10744393c4f6SBenjamin Herrenschmidt 	else
10754393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
107614cf11afSPaul Mackerras }
107714cf11afSPaul Mackerras 
107814cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
107914cf11afSPaul Mackerras {
108014cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
108114cf11afSPaul Mackerras 	       current, regs->gpr[1]);
108214cf11afSPaul Mackerras 	debugger(regs);
108314cf11afSPaul Mackerras 	show_regs(regs);
108414cf11afSPaul Mackerras 	panic("kernel stack overflow");
108514cf11afSPaul Mackerras }
108614cf11afSPaul Mackerras 
108714cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
108814cf11afSPaul Mackerras {
108914cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
109014cf11afSPaul Mackerras 	       regs->nip, regs->msr);
109114cf11afSPaul Mackerras 	debugger(regs);
109214cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
109314cf11afSPaul Mackerras }
109414cf11afSPaul Mackerras 
109514cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
109614cf11afSPaul Mackerras {
109714cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
109819c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
109914cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
110014cf11afSPaul Mackerras }
110114cf11afSPaul Mackerras 
1102dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1103dc1c1ca3SStephen Rothwell {
1104dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1105dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1106dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1107dc1c1ca3SStephen Rothwell }
1108dc1c1ca3SStephen Rothwell 
1109dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1110dc1c1ca3SStephen Rothwell {
1111dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1112dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1113dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1114dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1115dc1c1ca3SStephen Rothwell 		return;
1116dc1c1ca3SStephen Rothwell 	}
11176c4841c2SAnton Blanchard 
1118dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1119dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1120dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1121dc1c1ca3SStephen Rothwell }
1122dc1c1ca3SStephen Rothwell 
1123ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1124ce48b210SMichael Neuling {
1125ce48b210SMichael Neuling 	if (user_mode(regs)) {
1126ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1127ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1128ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1129ce48b210SMichael Neuling 		return;
1130ce48b210SMichael Neuling 	}
1131ce48b210SMichael Neuling 
1132ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1133ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1134ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1135ce48b210SMichael Neuling }
1136ce48b210SMichael Neuling 
1137dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1138dc1c1ca3SStephen Rothwell {
113989713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
114089713ed1SAnton Blanchard 
1141dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1142dc1c1ca3SStephen Rothwell }
1143dc1c1ca3SStephen Rothwell 
11448dad3f92SPaul Mackerras #ifdef CONFIG_8xx
114514cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
114614cf11afSPaul Mackerras {
114714cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
114814cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
11495dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
115014cf11afSPaul Mackerras 	int errcode;
11515dd57a13SScott Wood #endif
115214cf11afSPaul Mackerras 
115314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
115414cf11afSPaul Mackerras 
115514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
115614cf11afSPaul Mackerras 		debugger(regs);
115714cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
115814cf11afSPaul Mackerras 	}
115914cf11afSPaul Mackerras 
116014cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
116114cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
116280947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1163eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
11645fad293bSKumar Gala 
11655fad293bSKumar Gala 	switch (errcode) {
11665fad293bSKumar Gala 	case 0:
11675fad293bSKumar Gala 		emulate_single_step(regs);
11685fad293bSKumar Gala 		return;
11695fad293bSKumar Gala 	case 1: {
11705fad293bSKumar Gala 			int code = 0;
11715fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11725fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
11735fad293bSKumar Gala 			return;
11745fad293bSKumar Gala 		}
11755fad293bSKumar Gala 	case -EFAULT:
11765fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11775fad293bSKumar Gala 		return;
11785fad293bSKumar Gala 	default:
11795fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11805fad293bSKumar Gala 		return;
11815fad293bSKumar Gala 	}
11825fad293bSKumar Gala 
11835dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
118414cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
118580947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1186eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
118780947e7cSGeert Uytterhoeven 
11885fad293bSKumar Gala 	switch (errcode) {
11895fad293bSKumar Gala 	case 0:
119014cf11afSPaul Mackerras 		emulate_single_step(regs);
11915fad293bSKumar Gala 		return;
11925fad293bSKumar Gala 	case 1:
11935fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11945fad293bSKumar Gala 		return;
11955fad293bSKumar Gala 	case -EFAULT:
11965fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11975fad293bSKumar Gala 		return;
11985fad293bSKumar Gala 	}
11995dd57a13SScott Wood #else
12005dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
12015fad293bSKumar Gala #endif
120214cf11afSPaul Mackerras }
12038dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
120414cf11afSPaul Mackerras 
1205172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12063bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
12073bffb652SDave Kleikamp {
12083bffb652SDave Kleikamp 	int changed = 0;
12093bffb652SDave Kleikamp 	/*
12103bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
12113bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
12123bffb652SDave Kleikamp 	 */
12133bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
12143bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
12153bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
12163bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
12173bffb652SDave Kleikamp #endif
12183bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
12193bffb652SDave Kleikamp 			     5);
12203bffb652SDave Kleikamp 		changed |= 0x01;
12213bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
12223bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
12233bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
12243bffb652SDave Kleikamp 			     6);
12253bffb652SDave Kleikamp 		changed |= 0x01;
12263bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
12273bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
12283bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
12293bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
12303bffb652SDave Kleikamp 			     1);
12313bffb652SDave Kleikamp 		changed |= 0x01;
12323bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
12333bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
12343bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
12353bffb652SDave Kleikamp 			     2);
12363bffb652SDave Kleikamp 		changed |= 0x01;
12373bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
12383bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
12393bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
12403bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
12413bffb652SDave Kleikamp 			     3);
12423bffb652SDave Kleikamp 		changed |= 0x01;
12433bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12443bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
12453bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
12463bffb652SDave Kleikamp 			     4);
12473bffb652SDave Kleikamp 		changed |= 0x01;
12483bffb652SDave Kleikamp 	}
12493bffb652SDave Kleikamp 	/*
12503bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
12513bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
12523bffb652SDave Kleikamp 	 * back on or not.
12533bffb652SDave Kleikamp 	 */
12543bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
12553bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
12563bffb652SDave Kleikamp 	else
12573bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
12583bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
12593bffb652SDave Kleikamp 
12603bffb652SDave Kleikamp 	if (changed & 0x01)
12613bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
12623bffb652SDave Kleikamp }
126314cf11afSPaul Mackerras 
1264f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
126514cf11afSPaul Mackerras {
12663bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
12673bffb652SDave Kleikamp 
1268ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1269ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1270ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1271ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1272ec097c84SRoland McGrath 	 */
1273ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1274ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1275ec097c84SRoland McGrath 
1276ec097c84SRoland McGrath 		/* Disable BT */
1277ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1278ec097c84SRoland McGrath 		/* Clear the BT event */
1279ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1280ec097c84SRoland McGrath 
1281ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1282ec097c84SRoland McGrath 		if (user_mode(regs)) {
1283ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1284ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1285ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1286ec097c84SRoland McGrath 			return;
1287ec097c84SRoland McGrath 		}
1288ec097c84SRoland McGrath 
1289ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1290ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1291ec097c84SRoland McGrath 			return;
1292ec097c84SRoland McGrath 		}
1293ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1294ec097c84SRoland McGrath 			return;
1295ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
129614cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1297f8279621SKumar Gala 
129814cf11afSPaul Mackerras 		/* Disable instruction completion */
129914cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
130014cf11afSPaul Mackerras 		/* Clear the instruction completion event */
130114cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1302f8279621SKumar Gala 
1303f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1304f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
130514cf11afSPaul Mackerras 			return;
130614cf11afSPaul Mackerras 		}
1307f8279621SKumar Gala 
1308f8279621SKumar Gala 		if (debugger_sstep(regs))
1309f8279621SKumar Gala 			return;
1310f8279621SKumar Gala 
13113bffb652SDave Kleikamp 		if (user_mode(regs)) {
13123bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
13133bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
13143bffb652SDave Kleikamp 					       current->thread.dbcr1))
13153bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
13163bffb652SDave Kleikamp 			else
13173bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
13183bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
13193bffb652SDave Kleikamp 		}
1320f8279621SKumar Gala 
1321f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
13223bffb652SDave Kleikamp 	} else
13233bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
132414cf11afSPaul Mackerras }
1325172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
132614cf11afSPaul Mackerras 
132714cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
132814cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
132914cf11afSPaul Mackerras {
133014cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
133114cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
133214cf11afSPaul Mackerras }
133314cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
133414cf11afSPaul Mackerras 
133514cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1336dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
133714cf11afSPaul Mackerras {
133814cf11afSPaul Mackerras 	int err;
133914cf11afSPaul Mackerras 
134014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
134114cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
134214cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13438dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
134414cf11afSPaul Mackerras 	}
134514cf11afSPaul Mackerras 
1346dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1347dc1c1ca3SStephen Rothwell 
1348eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
134914cf11afSPaul Mackerras 	err = emulate_altivec(regs);
135014cf11afSPaul Mackerras 	if (err == 0) {
135114cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
135214cf11afSPaul Mackerras 		emulate_single_step(regs);
135314cf11afSPaul Mackerras 		return;
135414cf11afSPaul Mackerras 	}
135514cf11afSPaul Mackerras 
135614cf11afSPaul Mackerras 	if (err == -EFAULT) {
135714cf11afSPaul Mackerras 		/* got an error reading the instruction */
135814cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
135914cf11afSPaul Mackerras 	} else {
136014cf11afSPaul Mackerras 		/* didn't recognize the instruction */
136114cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
136276462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
136314cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
136414cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
136514cf11afSPaul Mackerras 	}
136614cf11afSPaul Mackerras }
136714cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
136814cf11afSPaul Mackerras 
1369ce48b210SMichael Neuling #ifdef CONFIG_VSX
1370ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1371ce48b210SMichael Neuling {
1372ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1373ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1374ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1375ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1376ce48b210SMichael Neuling 	}
1377ce48b210SMichael Neuling 
1378ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1379ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1380ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1381ce48b210SMichael Neuling }
1382ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1383ce48b210SMichael Neuling 
138414cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
138514cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
138614cf11afSPaul Mackerras 			   unsigned long error_code)
138714cf11afSPaul Mackerras {
138814cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
138914cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
139014cf11afSPaul Mackerras 	 * something smarter
139114cf11afSPaul Mackerras 	 */
139214cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
139314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
139414cf11afSPaul Mackerras 	return;
139514cf11afSPaul Mackerras }
139614cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
139714cf11afSPaul Mackerras 
139814cf11afSPaul Mackerras #ifdef CONFIG_SPE
139914cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
140014cf11afSPaul Mackerras {
14016a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
140214cf11afSPaul Mackerras 	unsigned long spefscr;
140314cf11afSPaul Mackerras 	int fpexc_mode;
140414cf11afSPaul Mackerras 	int code = 0;
14056a800f36SLiu Yu 	int err;
14066a800f36SLiu Yu 
1407685659eeSyu liu 	flush_spe_to_thread(current);
140814cf11afSPaul Mackerras 
140914cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
141014cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
141114cf11afSPaul Mackerras 
141214cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
141314cf11afSPaul Mackerras 		code = FPE_FLTOVF;
141414cf11afSPaul Mackerras 	}
141514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
141614cf11afSPaul Mackerras 		code = FPE_FLTUND;
141714cf11afSPaul Mackerras 	}
141814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
141914cf11afSPaul Mackerras 		code = FPE_FLTDIV;
142014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
142114cf11afSPaul Mackerras 		code = FPE_FLTINV;
142214cf11afSPaul Mackerras 	}
142314cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
142414cf11afSPaul Mackerras 		code = FPE_FLTRES;
142514cf11afSPaul Mackerras 
14266a800f36SLiu Yu 	err = do_spe_mathemu(regs);
14276a800f36SLiu Yu 	if (err == 0) {
14286a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14296a800f36SLiu Yu 		emulate_single_step(regs);
143014cf11afSPaul Mackerras 		return;
143114cf11afSPaul Mackerras 	}
14326a800f36SLiu Yu 
14336a800f36SLiu Yu 	if (err == -EFAULT) {
14346a800f36SLiu Yu 		/* got an error reading the instruction */
14356a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14366a800f36SLiu Yu 	} else if (err == -EINVAL) {
14376a800f36SLiu Yu 		/* didn't recognize the instruction */
14386a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14396a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14406a800f36SLiu Yu 	} else {
14416a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14426a800f36SLiu Yu 	}
14436a800f36SLiu Yu 
14446a800f36SLiu Yu 	return;
14456a800f36SLiu Yu }
14466a800f36SLiu Yu 
14476a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
14486a800f36SLiu Yu {
14496a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
14506a800f36SLiu Yu 	int err;
14516a800f36SLiu Yu 
14526a800f36SLiu Yu 	preempt_disable();
14536a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
14546a800f36SLiu Yu 		giveup_spe(current);
14556a800f36SLiu Yu 	preempt_enable();
14566a800f36SLiu Yu 
14576a800f36SLiu Yu 	regs->nip -= 4;
14586a800f36SLiu Yu 	err = speround_handler(regs);
14596a800f36SLiu Yu 	if (err == 0) {
14606a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14616a800f36SLiu Yu 		emulate_single_step(regs);
14626a800f36SLiu Yu 		return;
14636a800f36SLiu Yu 	}
14646a800f36SLiu Yu 
14656a800f36SLiu Yu 	if (err == -EFAULT) {
14666a800f36SLiu Yu 		/* got an error reading the instruction */
14676a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14686a800f36SLiu Yu 	} else if (err == -EINVAL) {
14696a800f36SLiu Yu 		/* didn't recognize the instruction */
14706a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14716a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14726a800f36SLiu Yu 	} else {
14736a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
14746a800f36SLiu Yu 		return;
14756a800f36SLiu Yu 	}
14766a800f36SLiu Yu }
147714cf11afSPaul Mackerras #endif
147814cf11afSPaul Mackerras 
1479dc1c1ca3SStephen Rothwell /*
1480dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1481dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1482dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1483dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1484dc1c1ca3SStephen Rothwell  */
1485dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1486dc1c1ca3SStephen Rothwell {
1487dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1488dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1489dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1490dc1c1ca3SStephen Rothwell }
1491dc1c1ca3SStephen Rothwell 
149214cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
149314cf11afSPaul Mackerras /*
149414cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
149514cf11afSPaul Mackerras  * spins until a reboot occurs
149614cf11afSPaul Mackerras  */
149714cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
149814cf11afSPaul Mackerras {
149914cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
150014cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
150114cf11afSPaul Mackerras 	return;
150214cf11afSPaul Mackerras }
150314cf11afSPaul Mackerras 
150414cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
150514cf11afSPaul Mackerras {
150614cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
150714cf11afSPaul Mackerras 	WatchdogHandler(regs);
150814cf11afSPaul Mackerras }
150914cf11afSPaul Mackerras #endif
1510dc1c1ca3SStephen Rothwell 
1511dc1c1ca3SStephen Rothwell /*
1512dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1513dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1514dc1c1ca3SStephen Rothwell  */
1515dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1516dc1c1ca3SStephen Rothwell {
1517dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1518dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1519dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1520dc1c1ca3SStephen Rothwell }
152114cf11afSPaul Mackerras 
152214cf11afSPaul Mackerras void __init trap_init(void)
152314cf11afSPaul Mackerras {
152414cf11afSPaul Mackerras }
152580947e7cSGeert Uytterhoeven 
152680947e7cSGeert Uytterhoeven 
152780947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
152880947e7cSGeert Uytterhoeven 
152980947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
153080947e7cSGeert Uytterhoeven 
153180947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
153280947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
153380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
153480947e7cSGeert Uytterhoeven #endif
153580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
153680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
153780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
153880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
153980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
154080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
154180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
154280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
154380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
154480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
154580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
154680947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
154780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
154880947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
154980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
155080947e7cSGeert Uytterhoeven #endif
155180947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
155280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
155380947e7cSGeert Uytterhoeven #endif
1554efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1555efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
1556efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
1557efcac658SAlexey Kardashevskiy #endif
155880947e7cSGeert Uytterhoeven };
155980947e7cSGeert Uytterhoeven 
156080947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
156180947e7cSGeert Uytterhoeven 
156280947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
156380947e7cSGeert Uytterhoeven {
156476462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
156580947e7cSGeert Uytterhoeven 			    type);
156680947e7cSGeert Uytterhoeven }
156780947e7cSGeert Uytterhoeven 
156880947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
156980947e7cSGeert Uytterhoeven {
157080947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
157180947e7cSGeert Uytterhoeven 	unsigned int i;
157280947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
157380947e7cSGeert Uytterhoeven 
157480947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
157580947e7cSGeert Uytterhoeven 		return -ENODEV;
157680947e7cSGeert Uytterhoeven 
157780947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
157880947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
157980947e7cSGeert Uytterhoeven 	if (!dir)
158080947e7cSGeert Uytterhoeven 		return -ENOMEM;
158180947e7cSGeert Uytterhoeven 
158280947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
158380947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
158480947e7cSGeert Uytterhoeven 	if (!d)
158580947e7cSGeert Uytterhoeven 		goto fail;
158680947e7cSGeert Uytterhoeven 
158780947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
158880947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
158980947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
159080947e7cSGeert Uytterhoeven 		if (!d)
159180947e7cSGeert Uytterhoeven 			goto fail;
159280947e7cSGeert Uytterhoeven 	}
159380947e7cSGeert Uytterhoeven 
159480947e7cSGeert Uytterhoeven 	return 0;
159580947e7cSGeert Uytterhoeven 
159680947e7cSGeert Uytterhoeven fail:
159780947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
159880947e7cSGeert Uytterhoeven 	return -ENOMEM;
159980947e7cSGeert Uytterhoeven }
160080947e7cSGeert Uytterhoeven 
160180947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
160280947e7cSGeert Uytterhoeven 
160380947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1604