xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision a443506b8598bbd784cfe403ad1db2c7083ff465)
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>
3714cf11afSPaul Mackerras 
3880947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
3914cf11afSPaul Mackerras #include <asm/pgtable.h>
4014cf11afSPaul Mackerras #include <asm/uaccess.h>
4114cf11afSPaul Mackerras #include <asm/system.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>
58dc1c1ca3SStephen Rothwell 
597dbb922cSOlof Johansson #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
605be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
615be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
625be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
635be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
645be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
655be3492fSAnton Blanchard int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
665be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
6714cf11afSPaul Mackerras 
6814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
6914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
7014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7514cf11afSPaul Mackerras #endif
7614cf11afSPaul Mackerras 
7714cf11afSPaul Mackerras /*
7814cf11afSPaul Mackerras  * Trap & Exception support
7914cf11afSPaul Mackerras  */
8014cf11afSPaul Mackerras 
816031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
826031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
836031d9d9Santon@samba.org {
846031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
856031d9d9Santon@samba.org 	if (pmac_backlight) {
866031d9d9Santon@samba.org 		struct backlight_properties *props;
876031d9d9Santon@samba.org 
886031d9d9Santon@samba.org 		props = &pmac_backlight->props;
896031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
906031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
916031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
926031d9d9Santon@samba.org 	}
936031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
946031d9d9Santon@samba.org }
956031d9d9Santon@samba.org #else
966031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
976031d9d9Santon@samba.org #endif
986031d9d9Santon@samba.org 
9914cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
10014cf11afSPaul Mackerras {
10134c2a14fSanton@samba.org 	static struct {
102b8f87782SThomas Gleixner 		raw_spinlock_t lock;
10334c2a14fSanton@samba.org 		u32 lock_owner;
10434c2a14fSanton@samba.org 		int lock_owner_depth;
10534c2a14fSanton@samba.org 	} die = {
106b8f87782SThomas Gleixner 		.lock =			__RAW_SPIN_LOCK_UNLOCKED(die.lock),
10734c2a14fSanton@samba.org 		.lock_owner =		-1,
10834c2a14fSanton@samba.org 		.lock_owner_depth =	0
10934c2a14fSanton@samba.org 	};
110c0ce7d08SDavid Wilder 	static int die_counter;
11134c2a14fSanton@samba.org 	unsigned long flags;
11214cf11afSPaul Mackerras 
11314cf11afSPaul Mackerras 	if (debugger(regs))
11414cf11afSPaul Mackerras 		return 1;
11514cf11afSPaul Mackerras 
116293e4688Santon@samba.org 	oops_enter();
117293e4688Santon@samba.org 
11834c2a14fSanton@samba.org 	if (die.lock_owner != raw_smp_processor_id()) {
11914cf11afSPaul Mackerras 		console_verbose();
120b8f87782SThomas Gleixner 		raw_spin_lock_irqsave(&die.lock, flags);
12134c2a14fSanton@samba.org 		die.lock_owner = smp_processor_id();
12234c2a14fSanton@samba.org 		die.lock_owner_depth = 0;
12314cf11afSPaul Mackerras 		bust_spinlocks(1);
1246031d9d9Santon@samba.org 		if (machine_is(powermac))
1256031d9d9Santon@samba.org 			pmac_backlight_unblank();
12634c2a14fSanton@samba.org 	} else {
12734c2a14fSanton@samba.org 		local_save_flags(flags);
12834c2a14fSanton@samba.org 	}
1295474c120SMichael Hanselmann 
13034c2a14fSanton@samba.org 	if (++die.lock_owner_depth < 3) {
13114cf11afSPaul Mackerras 		printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
13214cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
13314cf11afSPaul Mackerras 		printk("PREEMPT ");
13414cf11afSPaul Mackerras #endif
13514cf11afSPaul Mackerras #ifdef CONFIG_SMP
13614cf11afSPaul Mackerras 		printk("SMP NR_CPUS=%d ", NR_CPUS);
13714cf11afSPaul Mackerras #endif
13814cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
13914cf11afSPaul Mackerras 		printk("DEBUG_PAGEALLOC ");
14014cf11afSPaul Mackerras #endif
14114cf11afSPaul Mackerras #ifdef CONFIG_NUMA
14214cf11afSPaul Mackerras 		printk("NUMA ");
14314cf11afSPaul Mackerras #endif
144ae7f4463Santon@samba.org 		printk("%s\n", ppc_md.name ? ppc_md.name : "");
145e8222502SBenjamin Herrenschmidt 
14666fcb105SAnton Blanchard 		sysfs_printk_last_file();
14766fcb105SAnton Blanchard 		if (notify_die(DIE_OOPS, str, regs, err, 255,
14866fcb105SAnton Blanchard 			       SIGSEGV) == NOTIFY_STOP)
14966fcb105SAnton Blanchard 			return 1;
15066fcb105SAnton Blanchard 
15114cf11afSPaul Mackerras 		print_modules();
15214cf11afSPaul Mackerras 		show_regs(regs);
15334c2a14fSanton@samba.org 	} else {
15434c2a14fSanton@samba.org 		printk("Recursive die() failure, output suppressed\n");
15534c2a14fSanton@samba.org 	}
15634c2a14fSanton@samba.org 
15714cf11afSPaul Mackerras 	bust_spinlocks(0);
15834c2a14fSanton@samba.org 	die.lock_owner = -1;
159bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
160b8f87782SThomas Gleixner 	raw_spin_unlock_irqrestore(&die.lock, flags);
161cc532915SMichael Ellerman 
162c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
163c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
164cc532915SMichael Ellerman 		crash_kexec(regs);
165c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
16614cf11afSPaul Mackerras 
16714cf11afSPaul Mackerras 	if (in_interrupt())
16814cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
16914cf11afSPaul Mackerras 
170cea6a4baSHorms 	if (panic_on_oops)
171012c437dSHorms 		panic("Fatal exception");
172cea6a4baSHorms 
173293e4688Santon@samba.org 	oops_exit();
17414cf11afSPaul Mackerras 	do_exit(err);
17514cf11afSPaul Mackerras 
17614cf11afSPaul Mackerras 	return 0;
17714cf11afSPaul Mackerras }
17814cf11afSPaul Mackerras 
17925baa35bSOleg Nesterov void user_single_step_siginfo(struct task_struct *tsk,
18025baa35bSOleg Nesterov 				struct pt_regs *regs, siginfo_t *info)
18125baa35bSOleg Nesterov {
18225baa35bSOleg Nesterov 	memset(info, 0, sizeof(*info));
18325baa35bSOleg Nesterov 	info->si_signo = SIGTRAP;
18425baa35bSOleg Nesterov 	info->si_code = TRAP_TRACE;
18525baa35bSOleg Nesterov 	info->si_addr = (void __user *)regs->nip;
18625baa35bSOleg Nesterov }
18725baa35bSOleg Nesterov 
18814cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
18914cf11afSPaul Mackerras {
19014cf11afSPaul Mackerras 	siginfo_t info;
191d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
192d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
193d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
194d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
19514cf11afSPaul Mackerras 
19614cf11afSPaul Mackerras 	if (!user_mode(regs)) {
19714cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
19814cf11afSPaul Mackerras 			return;
199d0c3d534SOlof Johansson 	} else if (show_unhandled_signals &&
200d0c3d534SOlof Johansson 		    unhandled_signal(current, signr) &&
201d0c3d534SOlof Johansson 		    printk_ratelimit()) {
202d0c3d534SOlof Johansson 			printk(regs->msr & MSR_SF ? fmt64 : fmt32,
203d0c3d534SOlof Johansson 				current->comm, current->pid, signr,
204d0c3d534SOlof Johansson 				addr, regs->nip, regs->link, code);
20514cf11afSPaul Mackerras 		}
20614cf11afSPaul Mackerras 
20714cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
20814cf11afSPaul Mackerras 	info.si_signo = signr;
20914cf11afSPaul Mackerras 	info.si_code = code;
21014cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
21114cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
21214cf11afSPaul Mackerras }
21314cf11afSPaul Mackerras 
21414cf11afSPaul Mackerras #ifdef CONFIG_PPC64
21514cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
21614cf11afSPaul Mackerras {
21714cf11afSPaul Mackerras 	/* See if any machine dependent calls */
218c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
219c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
220c902be71SArnd Bergmann 			return;
221c902be71SArnd Bergmann 	}
22214cf11afSPaul Mackerras 
223c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
224c0ce7d08SDavid Wilder 	cpu_set(smp_processor_id(), cpus_in_sr);
225c0ce7d08SDavid Wilder #endif
226c0ce7d08SDavid Wilder 
2278dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
22814cf11afSPaul Mackerras 
229eac8392fSDavid Wilder 	/*
230eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
231eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
232eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
233eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
234eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
235eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
236eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
237eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
238eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
239eac8392fSDavid Wilder 	 */
240eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
241eac8392fSDavid Wilder 
24214cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
24314cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
24414cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
24514cf11afSPaul Mackerras 
24614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
24714cf11afSPaul Mackerras }
24814cf11afSPaul Mackerras #endif
24914cf11afSPaul Mackerras 
25014cf11afSPaul Mackerras /*
25114cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
25214cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
25314cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
25414cf11afSPaul Mackerras  * table.
25514cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
25614cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
25714cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
25814cf11afSPaul Mackerras  *  -- paulus.
25914cf11afSPaul Mackerras  */
26014cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
26114cf11afSPaul Mackerras {
26268a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
26314cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
26414cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
26514cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
26614cf11afSPaul Mackerras 
26714cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
26814cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
26914cf11afSPaul Mackerras 		/*
27014cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
27114cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
27214cf11afSPaul Mackerras 		 * As the address is in the exception table
27314cf11afSPaul Mackerras 		 * we should be able to read the instr there.
27414cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
27514cf11afSPaul Mackerras 		 * load or store.
27614cf11afSPaul Mackerras 		 */
27714cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
27814cf11afSPaul Mackerras 			nip -= 2;
27914cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
28014cf11afSPaul Mackerras 			--nip;
28114cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
28214cf11afSPaul Mackerras 			/* sync or twi */
28314cf11afSPaul Mackerras 			unsigned int rb;
28414cf11afSPaul Mackerras 
28514cf11afSPaul Mackerras 			--nip;
28614cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
28714cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
28814cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
28914cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
29014cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
29114cf11afSPaul Mackerras 			regs->nip = entry->fixup;
29214cf11afSPaul Mackerras 			return 1;
29314cf11afSPaul Mackerras 		}
29414cf11afSPaul Mackerras 	}
29568a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
29614cf11afSPaul Mackerras 	return 0;
29714cf11afSPaul Mackerras }
29814cf11afSPaul Mackerras 
299172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
30014cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
30114cf11afSPaul Mackerras    is in the ESR. */
30214cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
30314cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
30414cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
30514cf11afSPaul Mackerras #else
306fe04b112SScott Wood #define get_mc_reason(regs)	(mfspr(SPRN_MCSR))
30714cf11afSPaul Mackerras #endif
30814cf11afSPaul Mackerras #define REASON_FP		ESR_FP
30914cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
31014cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
31114cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
31214cf11afSPaul Mackerras 
31314cf11afSPaul Mackerras /* single-step stuff */
31414cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
31514cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
31614cf11afSPaul Mackerras 
31714cf11afSPaul Mackerras #else
31814cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
31914cf11afSPaul Mackerras    exception is in the MSR. */
32014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
32114cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
32214cf11afSPaul Mackerras #define REASON_FP		0x100000
32314cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
32414cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
32514cf11afSPaul Mackerras #define REASON_TRAP		0x20000
32614cf11afSPaul Mackerras 
32714cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
32814cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
32914cf11afSPaul Mackerras #endif
33014cf11afSPaul Mackerras 
33147c0bd1aSBenjamin Herrenschmidt #if defined(CONFIG_4xx)
33247c0bd1aSBenjamin Herrenschmidt int machine_check_4xx(struct pt_regs *regs)
33314cf11afSPaul Mackerras {
3341a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
33514cf11afSPaul Mackerras 
33614cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
33714cf11afSPaul Mackerras 		printk("Instruction");
33814cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
33914cf11afSPaul Mackerras 	} else
34014cf11afSPaul Mackerras 		printk("Data");
34114cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
34247c0bd1aSBenjamin Herrenschmidt 
34347c0bd1aSBenjamin Herrenschmidt 	return 0;
34447c0bd1aSBenjamin Herrenschmidt }
34547c0bd1aSBenjamin Herrenschmidt 
34647c0bd1aSBenjamin Herrenschmidt int machine_check_440A(struct pt_regs *regs)
34747c0bd1aSBenjamin Herrenschmidt {
34847c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
34947c0bd1aSBenjamin Herrenschmidt 
35014cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
35114cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
35214cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
35314cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35414cf11afSPaul Mackerras 	}
35514cf11afSPaul Mackerras 	else {
35614cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
35714cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
35814cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
35914cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
36014cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
36114cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
36214cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
36314cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
36414cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
36514cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
36614cf11afSPaul Mackerras 			flush_instruction_cache();
36714cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
36814cf11afSPaul Mackerras 		}
36914cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
37014cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
37114cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
37214cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
37314cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
37414cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
37514cf11afSPaul Mackerras 
37614cf11afSPaul Mackerras 		/* Clear MCSR */
37714cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
37814cf11afSPaul Mackerras 	}
37947c0bd1aSBenjamin Herrenschmidt 	return 0;
38047c0bd1aSBenjamin Herrenschmidt }
381fc5e7097SDave Kleikamp 
382fc5e7097SDave Kleikamp int machine_check_47x(struct pt_regs *regs)
383fc5e7097SDave Kleikamp {
384fc5e7097SDave Kleikamp 	unsigned long reason = get_mc_reason(regs);
385fc5e7097SDave Kleikamp 	u32 mcsr;
386fc5e7097SDave Kleikamp 
387fc5e7097SDave Kleikamp 	printk(KERN_ERR "Machine check in kernel mode.\n");
388fc5e7097SDave Kleikamp 	if (reason & ESR_IMCP) {
389fc5e7097SDave Kleikamp 		printk(KERN_ERR
390fc5e7097SDave Kleikamp 		       "Instruction Synchronous Machine Check exception\n");
391fc5e7097SDave Kleikamp 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
392fc5e7097SDave Kleikamp 		return 0;
393fc5e7097SDave Kleikamp 	}
394fc5e7097SDave Kleikamp 	mcsr = mfspr(SPRN_MCSR);
395fc5e7097SDave Kleikamp 	if (mcsr & MCSR_IB)
396fc5e7097SDave Kleikamp 		printk(KERN_ERR "Instruction Read PLB Error\n");
397fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DRB)
398fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Read PLB Error\n");
399fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DWB)
400fc5e7097SDave Kleikamp 		printk(KERN_ERR "Data Write PLB Error\n");
401fc5e7097SDave Kleikamp 	if (mcsr & MCSR_TLBP)
402fc5e7097SDave Kleikamp 		printk(KERN_ERR "TLB Parity Error\n");
403fc5e7097SDave Kleikamp 	if (mcsr & MCSR_ICP) {
404fc5e7097SDave Kleikamp 		flush_instruction_cache();
405fc5e7097SDave Kleikamp 		printk(KERN_ERR "I-Cache Parity Error\n");
406fc5e7097SDave Kleikamp 	}
407fc5e7097SDave Kleikamp 	if (mcsr & MCSR_DCSP)
408fc5e7097SDave Kleikamp 		printk(KERN_ERR "D-Cache Search Parity Error\n");
409fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_GPR)
410fc5e7097SDave Kleikamp 		printk(KERN_ERR "GPR Parity Error\n");
411fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_FPR)
412fc5e7097SDave Kleikamp 		printk(KERN_ERR "FPR Parity Error\n");
413fc5e7097SDave Kleikamp 	if (mcsr & PPC47x_MCSR_IPR)
414fc5e7097SDave Kleikamp 		printk(KERN_ERR "Machine Check exception is imprecise\n");
415fc5e7097SDave Kleikamp 
416fc5e7097SDave Kleikamp 	/* Clear MCSR */
417fc5e7097SDave Kleikamp 	mtspr(SPRN_MCSR, mcsr);
418fc5e7097SDave Kleikamp 
419fc5e7097SDave Kleikamp 	return 0;
420fc5e7097SDave Kleikamp }
42114cf11afSPaul Mackerras #elif defined(CONFIG_E500)
422fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
423fe04b112SScott Wood {
424fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
425fe04b112SScott Wood 	unsigned long reason = mcsr;
426fe04b112SScott Wood 	int recoverable = 1;
427fe04b112SScott Wood 
428fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
429fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
430fe04b112SScott Wood 
431fe04b112SScott Wood 	if (reason & MCSR_MCP)
432fe04b112SScott Wood 		printk("Machine Check Signal\n");
433fe04b112SScott Wood 
434fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
435fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
436fe04b112SScott Wood 
437fe04b112SScott Wood 		/*
438fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
439fe04b112SScott Wood 		 */
440fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
441fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
442fe04b112SScott Wood 			;
443fe04b112SScott Wood 
444fe04b112SScott Wood 		/*
445fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
446fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
447fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
448fe04b112SScott Wood 		 */
449fe04b112SScott Wood 		reason &= ~MCSR_IF;
450fe04b112SScott Wood 	}
451fe04b112SScott Wood 
452fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
453fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
454fe04b112SScott Wood 		recoverable = 0;
455fe04b112SScott Wood 	}
456fe04b112SScott Wood 
457fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
458fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
459fe04b112SScott Wood 		recoverable = 0;
460fe04b112SScott Wood 	}
461fe04b112SScott Wood 
462fe04b112SScott Wood 	if (reason & MCSR_NMI)
463fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
464fe04b112SScott Wood 
465fe04b112SScott Wood 	if (reason & MCSR_IF) {
466fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
467fe04b112SScott Wood 		recoverable = 0;
468fe04b112SScott Wood 	}
469fe04b112SScott Wood 
470fe04b112SScott Wood 	if (reason & MCSR_LD) {
471fe04b112SScott Wood 		printk("Load Error Report\n");
472fe04b112SScott Wood 		recoverable = 0;
473fe04b112SScott Wood 	}
474fe04b112SScott Wood 
475fe04b112SScott Wood 	if (reason & MCSR_ST) {
476fe04b112SScott Wood 		printk("Store Error Report\n");
477fe04b112SScott Wood 		recoverable = 0;
478fe04b112SScott Wood 	}
479fe04b112SScott Wood 
480fe04b112SScott Wood 	if (reason & MCSR_LDG) {
481fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
482fe04b112SScott Wood 		recoverable = 0;
483fe04b112SScott Wood 	}
484fe04b112SScott Wood 
485fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
486fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
487fe04b112SScott Wood 
488fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
489fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
490fe04b112SScott Wood 		recoverable = 0;
491fe04b112SScott Wood 	}
492fe04b112SScott Wood 
493fe04b112SScott Wood 	if (reason & MCSR_MAV) {
494fe04b112SScott Wood 		u64 addr;
495fe04b112SScott Wood 
496fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
497fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
498fe04b112SScott Wood 
499fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
500fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
501fe04b112SScott Wood 	}
502fe04b112SScott Wood 
503fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
504fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
505fe04b112SScott Wood }
506fe04b112SScott Wood 
50747c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
50847c0bd1aSBenjamin Herrenschmidt {
50947c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
51047c0bd1aSBenjamin Herrenschmidt 
51114cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
51214cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
51314cf11afSPaul Mackerras 
51414cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
51514cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
51614cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
51714cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
51814cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
51914cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
52014cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
52114cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
52214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
52314cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
52414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
52514cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
52614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
52714cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
52814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
52914cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
53014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
53114cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
53214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
53314cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
53414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
53514cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
53614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
53714cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
53847c0bd1aSBenjamin Herrenschmidt 
53947c0bd1aSBenjamin Herrenschmidt 	return 0;
54047c0bd1aSBenjamin Herrenschmidt }
5414490c06bSKumar Gala 
5424490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
5434490c06bSKumar Gala {
5444490c06bSKumar Gala 	return 0;
5454490c06bSKumar Gala }
54614cf11afSPaul Mackerras #elif defined(CONFIG_E200)
54747c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
54847c0bd1aSBenjamin Herrenschmidt {
54947c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
55047c0bd1aSBenjamin Herrenschmidt 
55114cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
55214cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
55314cf11afSPaul Mackerras 
55414cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
55514cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
55614cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
55714cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
55814cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
55914cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
56014cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
56114cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
56214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
56314cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
56414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
56514cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
56614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
56714cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
56847c0bd1aSBenjamin Herrenschmidt 
56947c0bd1aSBenjamin Herrenschmidt 	return 0;
57047c0bd1aSBenjamin Herrenschmidt }
57147c0bd1aSBenjamin Herrenschmidt #else
57247c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
57347c0bd1aSBenjamin Herrenschmidt {
57447c0bd1aSBenjamin Herrenschmidt 	unsigned long reason = get_mc_reason(regs);
57547c0bd1aSBenjamin Herrenschmidt 
57614cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
57714cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
57814cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
57914cf11afSPaul Mackerras 	case 0x80000:
58014cf11afSPaul Mackerras 		printk("Machine check signal\n");
58114cf11afSPaul Mackerras 		break;
58214cf11afSPaul Mackerras 	case 0:		/* for 601 */
58314cf11afSPaul Mackerras 	case 0x40000:
58414cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
58514cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
58614cf11afSPaul Mackerras 		break;
58714cf11afSPaul Mackerras 	case 0x20000:
58814cf11afSPaul Mackerras 		printk("Data parity error signal\n");
58914cf11afSPaul Mackerras 		break;
59014cf11afSPaul Mackerras 	case 0x10000:
59114cf11afSPaul Mackerras 		printk("Address parity error signal\n");
59214cf11afSPaul Mackerras 		break;
59314cf11afSPaul Mackerras 	case 0x20000000:
59414cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
59514cf11afSPaul Mackerras 		break;
59614cf11afSPaul Mackerras 	case 0x40000000:
59714cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
59814cf11afSPaul Mackerras 		break;
59914cf11afSPaul Mackerras 	case 0x00100000:
60014cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
60114cf11afSPaul Mackerras 		break;
60214cf11afSPaul Mackerras 	default:
60314cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
60414cf11afSPaul Mackerras 	}
60575918a4bSOlof Johansson 	return 0;
60675918a4bSOlof Johansson }
60747c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
60875918a4bSOlof Johansson 
60975918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
61075918a4bSOlof Johansson {
61175918a4bSOlof Johansson 	int recover = 0;
61275918a4bSOlof Johansson 
61389713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).mce_exceptions++;
61489713ed1SAnton Blanchard 
61547c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
61647c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
61747c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
61847c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
61947c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
62047c0bd1aSBenjamin Herrenschmidt 	 */
62175918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
62275918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
62347c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
62447c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
62575918a4bSOlof Johansson 
62647c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
62775918a4bSOlof Johansson 		return;
62875918a4bSOlof Johansson 
62975918a4bSOlof Johansson 	if (user_mode(regs)) {
63075918a4bSOlof Johansson 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
63175918a4bSOlof Johansson 		return;
63275918a4bSOlof Johansson 	}
63375918a4bSOlof Johansson 
63475918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
63547c0bd1aSBenjamin Herrenschmidt 	/* the qspan pci read routines can cause machine checks -- Cort
63647c0bd1aSBenjamin Herrenschmidt 	 *
63747c0bd1aSBenjamin Herrenschmidt 	 * yuck !!! that totally needs to go away ! There are better ways
63847c0bd1aSBenjamin Herrenschmidt 	 * to deal with that than having a wart in the mcheck handler.
63947c0bd1aSBenjamin Herrenschmidt 	 * -- BenH
64047c0bd1aSBenjamin Herrenschmidt 	 */
64175918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
64275918a4bSOlof Johansson 	return;
64375918a4bSOlof Johansson #endif
64475918a4bSOlof Johansson 
645*a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
64675918a4bSOlof Johansson 		return;
64775918a4bSOlof Johansson 
64875918a4bSOlof Johansson 	if (check_io_access(regs))
64975918a4bSOlof Johansson 		return;
65075918a4bSOlof Johansson 
65114cf11afSPaul Mackerras 	if (debugger_fault_handler(regs))
65214cf11afSPaul Mackerras 		return;
6538dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
65414cf11afSPaul Mackerras 
65514cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
65614cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
65714cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
65814cf11afSPaul Mackerras }
65914cf11afSPaul Mackerras 
66014cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
66114cf11afSPaul Mackerras {
66214cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
66314cf11afSPaul Mackerras }
66414cf11afSPaul Mackerras 
665dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
66614cf11afSPaul Mackerras {
66714cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
66814cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
66914cf11afSPaul Mackerras 
67014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
67114cf11afSPaul Mackerras }
67214cf11afSPaul Mackerras 
673dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
67414cf11afSPaul Mackerras {
67514cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
67614cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
67714cf11afSPaul Mackerras 		return;
67814cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
67914cf11afSPaul Mackerras 		return;
68014cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
68114cf11afSPaul Mackerras }
68214cf11afSPaul Mackerras 
68314cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
68414cf11afSPaul Mackerras {
68514cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
68614cf11afSPaul Mackerras }
68714cf11afSPaul Mackerras 
6888dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
68914cf11afSPaul Mackerras {
6902538c2d0SK.Prasad 	clear_single_step(regs);
69114cf11afSPaul Mackerras 
69214cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
69314cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
69414cf11afSPaul Mackerras 		return;
69514cf11afSPaul Mackerras 	if (debugger_sstep(regs))
69614cf11afSPaul Mackerras 		return;
69714cf11afSPaul Mackerras 
69814cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
69914cf11afSPaul Mackerras }
70014cf11afSPaul Mackerras 
70114cf11afSPaul Mackerras /*
70214cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
70314cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
70414cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
70514cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
70614cf11afSPaul Mackerras  */
7078dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
70814cf11afSPaul Mackerras {
7092538c2d0SK.Prasad 	if (single_stepping(regs))
7102538c2d0SK.Prasad 		single_step_exception(regs);
71114cf11afSPaul Mackerras }
71214cf11afSPaul Mackerras 
7135fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
714dc1c1ca3SStephen Rothwell {
7155fad293bSKumar Gala 	int ret = 0;
716dc1c1ca3SStephen Rothwell 
717dc1c1ca3SStephen Rothwell 	/* Invalid operation */
718dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
7195fad293bSKumar Gala 		ret = FPE_FLTINV;
720dc1c1ca3SStephen Rothwell 
721dc1c1ca3SStephen Rothwell 	/* Overflow */
722dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
7235fad293bSKumar Gala 		ret = FPE_FLTOVF;
724dc1c1ca3SStephen Rothwell 
725dc1c1ca3SStephen Rothwell 	/* Underflow */
726dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
7275fad293bSKumar Gala 		ret = FPE_FLTUND;
728dc1c1ca3SStephen Rothwell 
729dc1c1ca3SStephen Rothwell 	/* Divide by zero */
730dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
7315fad293bSKumar Gala 		ret = FPE_FLTDIV;
732dc1c1ca3SStephen Rothwell 
733dc1c1ca3SStephen Rothwell 	/* Inexact result */
734dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
7355fad293bSKumar Gala 		ret = FPE_FLTRES;
7365fad293bSKumar Gala 
7375fad293bSKumar Gala 	return ret;
7385fad293bSKumar Gala }
7395fad293bSKumar Gala 
7405fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
7415fad293bSKumar Gala {
7425fad293bSKumar Gala 	int code = 0;
7435fad293bSKumar Gala 
7445fad293bSKumar Gala 	flush_fp_to_thread(current);
7455fad293bSKumar Gala 
7465fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
747dc1c1ca3SStephen Rothwell 
748dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
749dc1c1ca3SStephen Rothwell }
750dc1c1ca3SStephen Rothwell 
751dc1c1ca3SStephen Rothwell /*
752dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
75314cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
75414cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
75514cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
75614cf11afSPaul Mackerras  *
75714cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
75814cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
75914cf11afSPaul Mackerras  * bits is faster and easier.
76086417780SPaul Mackerras  *
76114cf11afSPaul Mackerras  */
76214cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
76314cf11afSPaul Mackerras {
76414cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
76514cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
76614cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
76714cf11afSPaul Mackerras 	u32 num_bytes;
76814cf11afSPaul Mackerras 	unsigned long EA;
76914cf11afSPaul Mackerras 	int pos = 0;
77014cf11afSPaul Mackerras 
77114cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
77216c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
77314cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
77414cf11afSPaul Mackerras 			return -EINVAL;
77514cf11afSPaul Mackerras 
77614cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
77714cf11afSPaul Mackerras 
77816c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
77916c57b36SKumar Gala 		case PPC_INST_LSWX:
78016c57b36SKumar Gala 		case PPC_INST_STSWX:
78114cf11afSPaul Mackerras 			EA += NB_RB;
78214cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
78314cf11afSPaul Mackerras 			break;
78416c57b36SKumar Gala 		case PPC_INST_LSWI:
78516c57b36SKumar Gala 		case PPC_INST_STSWI:
78614cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
78714cf11afSPaul Mackerras 			break;
78814cf11afSPaul Mackerras 		default:
78914cf11afSPaul Mackerras 			return -EINVAL;
79014cf11afSPaul Mackerras 	}
79114cf11afSPaul Mackerras 
79214cf11afSPaul Mackerras 	while (num_bytes != 0)
79314cf11afSPaul Mackerras 	{
79414cf11afSPaul Mackerras 		u8 val;
79514cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
79614cf11afSPaul Mackerras 
79716c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
79816c57b36SKumar Gala 			case PPC_INST_LSWX:
79916c57b36SKumar Gala 			case PPC_INST_LSWI:
80014cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
80114cf11afSPaul Mackerras 					return -EFAULT;
80214cf11afSPaul Mackerras 				/* first time updating this reg,
80314cf11afSPaul Mackerras 				 * zero it out */
80414cf11afSPaul Mackerras 				if (pos == 0)
80514cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
80614cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
80714cf11afSPaul Mackerras 				break;
80816c57b36SKumar Gala 			case PPC_INST_STSWI:
80916c57b36SKumar Gala 			case PPC_INST_STSWX:
81014cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
81114cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
81214cf11afSPaul Mackerras 					return -EFAULT;
81314cf11afSPaul Mackerras 				break;
81414cf11afSPaul Mackerras 		}
81514cf11afSPaul Mackerras 		/* move EA to next address */
81614cf11afSPaul Mackerras 		EA += 1;
81714cf11afSPaul Mackerras 		num_bytes--;
81814cf11afSPaul Mackerras 
81914cf11afSPaul Mackerras 		/* manage our position within the register */
82014cf11afSPaul Mackerras 		if (++pos == 4) {
82114cf11afSPaul Mackerras 			pos = 0;
82214cf11afSPaul Mackerras 			if (++rT == 32)
82314cf11afSPaul Mackerras 				rT = 0;
82414cf11afSPaul Mackerras 		}
82514cf11afSPaul Mackerras 	}
82614cf11afSPaul Mackerras 
82714cf11afSPaul Mackerras 	return 0;
82814cf11afSPaul Mackerras }
82914cf11afSPaul Mackerras 
830c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
831c3412dcbSWill Schmidt {
832c3412dcbSWill Schmidt 	u32 ra,rs;
833c3412dcbSWill Schmidt 	unsigned long tmp;
834c3412dcbSWill Schmidt 
835c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
836c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
837c3412dcbSWill Schmidt 
838c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
839c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
840c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
841c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
842c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
843c3412dcbSWill Schmidt 
844c3412dcbSWill Schmidt 	return 0;
845c3412dcbSWill Schmidt }
846c3412dcbSWill Schmidt 
847c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
848c1469f13SKumar Gala {
849c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
850c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
851c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
852c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
853c1469f13SKumar Gala 	u8 bit;
854c1469f13SKumar Gala 	unsigned long tmp;
855c1469f13SKumar Gala 
856c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
857c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
858c1469f13SKumar Gala 
859c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
860c1469f13SKumar Gala 
861c1469f13SKumar Gala 	return 0;
862c1469f13SKumar Gala }
863c1469f13SKumar Gala 
86414cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
86514cf11afSPaul Mackerras {
86614cf11afSPaul Mackerras 	u32 instword;
86714cf11afSPaul Mackerras 	u32 rd;
86814cf11afSPaul Mackerras 
869fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
87014cf11afSPaul Mackerras 		return -EINVAL;
87114cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
87214cf11afSPaul Mackerras 
87314cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
87414cf11afSPaul Mackerras 		return -EFAULT;
87514cf11afSPaul Mackerras 
87614cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
87716c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
878eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
87914cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
88014cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
88114cf11afSPaul Mackerras 		return 0;
88214cf11afSPaul Mackerras 	}
88314cf11afSPaul Mackerras 
88414cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
88580947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
886eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
88714cf11afSPaul Mackerras 		return 0;
88880947e7cSGeert Uytterhoeven 	}
88914cf11afSPaul Mackerras 
89014cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
89116c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
89286417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
89314cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
89414cf11afSPaul Mackerras 
895eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
89614cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
89714cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
89814cf11afSPaul Mackerras 		return 0;
89914cf11afSPaul Mackerras 	}
90014cf11afSPaul Mackerras 
90114cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
90280947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
903eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
90414cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
90580947e7cSGeert Uytterhoeven 	}
90614cf11afSPaul Mackerras 
907c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
90816c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
909eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
910c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
911c3412dcbSWill Schmidt 	}
912c3412dcbSWill Schmidt 
913c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
91416c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
915eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
916c1469f13SKumar Gala 		return emulate_isel(regs, instword);
917c1469f13SKumar Gala 	}
918c1469f13SKumar Gala 
91914cf11afSPaul Mackerras 	return -EINVAL;
92014cf11afSPaul Mackerras }
92114cf11afSPaul Mackerras 
92273c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
92314cf11afSPaul Mackerras {
92473c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
92514cf11afSPaul Mackerras }
92614cf11afSPaul Mackerras 
9278dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
92814cf11afSPaul Mackerras {
92914cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
93014cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
93114cf11afSPaul Mackerras 
932aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
93304903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
93414cf11afSPaul Mackerras 
93514cf11afSPaul Mackerras 	if (reason & REASON_FP) {
93614cf11afSPaul Mackerras 		/* IEEE FP exception */
937dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
9388dad3f92SPaul Mackerras 		return;
9398dad3f92SPaul Mackerras 	}
9408dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
941ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
942ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
943ba797b28SJason Wessel 		if (debugger_bpt(regs))
944ba797b28SJason Wessel 			return;
945ba797b28SJason Wessel 
94614cf11afSPaul Mackerras 		/* trap exception */
947dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
948dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
949dc1c1ca3SStephen Rothwell 			return;
95073c9ceabSJeremy Fitzhardinge 
95173c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
952608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
95314cf11afSPaul Mackerras 			regs->nip += 4;
95414cf11afSPaul Mackerras 			return;
95514cf11afSPaul Mackerras 		}
9568dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
9578dad3f92SPaul Mackerras 		return;
9588dad3f92SPaul Mackerras 	}
9598dad3f92SPaul Mackerras 
960cd8a5673SPaul Mackerras 	local_irq_enable();
961cd8a5673SPaul Mackerras 
96204903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
96304903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
96404903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
96504903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
96604903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
96704903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
96804903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
96904903a30SKumar Gala 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
9705fad293bSKumar Gala 	switch (do_mathemu(regs)) {
9715fad293bSKumar Gala 	case 0:
97204903a30SKumar Gala 		emulate_single_step(regs);
97304903a30SKumar Gala 		return;
9745fad293bSKumar Gala 	case 1: {
9755fad293bSKumar Gala 			int code = 0;
9765fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
9775fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
9785fad293bSKumar Gala 			return;
97904903a30SKumar Gala 		}
9805fad293bSKumar Gala 	case -EFAULT:
9815fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9825fad293bSKumar Gala 		return;
9835fad293bSKumar Gala 	}
9845fad293bSKumar Gala 	/* fall through on any other errors */
98504903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
98604903a30SKumar Gala 
9878dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
9888dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
98914cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
99014cf11afSPaul Mackerras 		case 0:
99114cf11afSPaul Mackerras 			regs->nip += 4;
99214cf11afSPaul Mackerras 			emulate_single_step(regs);
9938dad3f92SPaul Mackerras 			return;
99414cf11afSPaul Mackerras 		case -EFAULT:
99514cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9968dad3f92SPaul Mackerras 			return;
9978dad3f92SPaul Mackerras 		}
9988dad3f92SPaul Mackerras 	}
9998dad3f92SPaul Mackerras 
100014cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
100114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
100214cf11afSPaul Mackerras 	else
100314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
100414cf11afSPaul Mackerras }
100514cf11afSPaul Mackerras 
1006dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
100714cf11afSPaul Mackerras {
10084393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
100914cf11afSPaul Mackerras 
1010e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1011e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
101214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
101314cf11afSPaul Mackerras 
101414cf11afSPaul Mackerras 	if (fixed == 1) {
101514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
101614cf11afSPaul Mackerras 		emulate_single_step(regs);
101714cf11afSPaul Mackerras 		return;
101814cf11afSPaul Mackerras 	}
101914cf11afSPaul Mackerras 
102014cf11afSPaul Mackerras 	/* Operand address was bad */
102114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
10224393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
10234393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
10244393c4f6SBenjamin Herrenschmidt 	} else {
10254393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
10264393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
102714cf11afSPaul Mackerras 	}
10284393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
10294393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
10304393c4f6SBenjamin Herrenschmidt 	else
10314393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
103214cf11afSPaul Mackerras }
103314cf11afSPaul Mackerras 
103414cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
103514cf11afSPaul Mackerras {
103614cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
103714cf11afSPaul Mackerras 	       current, regs->gpr[1]);
103814cf11afSPaul Mackerras 	debugger(regs);
103914cf11afSPaul Mackerras 	show_regs(regs);
104014cf11afSPaul Mackerras 	panic("kernel stack overflow");
104114cf11afSPaul Mackerras }
104214cf11afSPaul Mackerras 
104314cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
104414cf11afSPaul Mackerras {
104514cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
104614cf11afSPaul Mackerras 	       regs->nip, regs->msr);
104714cf11afSPaul Mackerras 	debugger(regs);
104814cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
104914cf11afSPaul Mackerras }
105014cf11afSPaul Mackerras 
105114cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
105214cf11afSPaul Mackerras {
105314cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
105419c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
105514cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
105614cf11afSPaul Mackerras }
105714cf11afSPaul Mackerras 
1058dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1059dc1c1ca3SStephen Rothwell {
1060dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1061dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1062dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1063dc1c1ca3SStephen Rothwell }
1064dc1c1ca3SStephen Rothwell 
1065dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1066dc1c1ca3SStephen Rothwell {
1067dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1068dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1069dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1070dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1071dc1c1ca3SStephen Rothwell 		return;
1072dc1c1ca3SStephen Rothwell 	}
10736c4841c2SAnton Blanchard 
1074dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1075dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1076dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1077dc1c1ca3SStephen Rothwell }
1078dc1c1ca3SStephen Rothwell 
1079ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1080ce48b210SMichael Neuling {
1081ce48b210SMichael Neuling 	if (user_mode(regs)) {
1082ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1083ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1084ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1085ce48b210SMichael Neuling 		return;
1086ce48b210SMichael Neuling 	}
1087ce48b210SMichael Neuling 
1088ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1089ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1090ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1091ce48b210SMichael Neuling }
1092ce48b210SMichael Neuling 
1093dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1094dc1c1ca3SStephen Rothwell {
109589713ed1SAnton Blanchard 	__get_cpu_var(irq_stat).pmu_irqs++;
109689713ed1SAnton Blanchard 
1097dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1098dc1c1ca3SStephen Rothwell }
1099dc1c1ca3SStephen Rothwell 
11008dad3f92SPaul Mackerras #ifdef CONFIG_8xx
110114cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
110214cf11afSPaul Mackerras {
110314cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
110414cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
11055dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
110614cf11afSPaul Mackerras 	int errcode;
11075dd57a13SScott Wood #endif
110814cf11afSPaul Mackerras 
110914cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
111014cf11afSPaul Mackerras 
111114cf11afSPaul Mackerras 	if (!user_mode(regs)) {
111214cf11afSPaul Mackerras 		debugger(regs);
111314cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
111414cf11afSPaul Mackerras 	}
111514cf11afSPaul Mackerras 
111614cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
111714cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
111880947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1119eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(math, regs);
11205fad293bSKumar Gala 
11215fad293bSKumar Gala 	switch (errcode) {
11225fad293bSKumar Gala 	case 0:
11235fad293bSKumar Gala 		emulate_single_step(regs);
11245fad293bSKumar Gala 		return;
11255fad293bSKumar Gala 	case 1: {
11265fad293bSKumar Gala 			int code = 0;
11275fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
11285fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
11295fad293bSKumar Gala 			return;
11305fad293bSKumar Gala 		}
11315fad293bSKumar Gala 	case -EFAULT:
11325fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11335fad293bSKumar Gala 		return;
11345fad293bSKumar Gala 	default:
11355fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11365fad293bSKumar Gala 		return;
11375fad293bSKumar Gala 	}
11385fad293bSKumar Gala 
11395dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
114014cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
114180947e7cSGeert Uytterhoeven 	if (errcode >= 0)
1142eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(8xx, regs);
114380947e7cSGeert Uytterhoeven 
11445fad293bSKumar Gala 	switch (errcode) {
11455fad293bSKumar Gala 	case 0:
114614cf11afSPaul Mackerras 		emulate_single_step(regs);
11475fad293bSKumar Gala 		return;
11485fad293bSKumar Gala 	case 1:
11495fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11505fad293bSKumar Gala 		return;
11515fad293bSKumar Gala 	case -EFAULT:
11525fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
11535fad293bSKumar Gala 		return;
11545fad293bSKumar Gala 	}
11555dd57a13SScott Wood #else
11565dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
11575fad293bSKumar Gala #endif
115814cf11afSPaul Mackerras }
11598dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
116014cf11afSPaul Mackerras 
1161172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
11623bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
11633bffb652SDave Kleikamp {
11643bffb652SDave Kleikamp 	int changed = 0;
11653bffb652SDave Kleikamp 	/*
11663bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
11673bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
11683bffb652SDave Kleikamp 	 */
11693bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
11703bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
11713bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
11723bffb652SDave Kleikamp 		current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
11733bffb652SDave Kleikamp #endif
11743bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
11753bffb652SDave Kleikamp 			     5);
11763bffb652SDave Kleikamp 		changed |= 0x01;
11773bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
11783bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
11793bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
11803bffb652SDave Kleikamp 			     6);
11813bffb652SDave Kleikamp 		changed |= 0x01;
11823bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
11833bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC1;
11843bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
11853bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
11863bffb652SDave Kleikamp 			     1);
11873bffb652SDave Kleikamp 		changed |= 0x01;
11883bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
11893bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC2;
11903bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
11913bffb652SDave Kleikamp 			     2);
11923bffb652SDave Kleikamp 		changed |= 0x01;
11933bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
11943bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC3;
11953bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
11963bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
11973bffb652SDave Kleikamp 			     3);
11983bffb652SDave Kleikamp 		changed |= 0x01;
11993bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
12003bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IAC4;
12013bffb652SDave Kleikamp 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
12023bffb652SDave Kleikamp 			     4);
12033bffb652SDave Kleikamp 		changed |= 0x01;
12043bffb652SDave Kleikamp 	}
12053bffb652SDave Kleikamp 	/*
12063bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
12073bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
12083bffb652SDave Kleikamp 	 * back on or not.
12093bffb652SDave Kleikamp 	 */
12103bffb652SDave Kleikamp 	if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
12113bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
12123bffb652SDave Kleikamp 	else
12133bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
12143bffb652SDave Kleikamp 		current->thread.dbcr0 &= ~DBCR0_IDM;
12153bffb652SDave Kleikamp 
12163bffb652SDave Kleikamp 	if (changed & 0x01)
12173bffb652SDave Kleikamp 		mtspr(SPRN_DBCR0, current->thread.dbcr0);
12183bffb652SDave Kleikamp }
121914cf11afSPaul Mackerras 
1220f8279621SKumar Gala void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
122114cf11afSPaul Mackerras {
12223bffb652SDave Kleikamp 	current->thread.dbsr = debug_status;
12233bffb652SDave Kleikamp 
1224ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1225ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1226ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1227ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1228ec097c84SRoland McGrath 	 */
1229ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1230ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1231ec097c84SRoland McGrath 
1232ec097c84SRoland McGrath 		/* Disable BT */
1233ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1234ec097c84SRoland McGrath 		/* Clear the BT event */
1235ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1236ec097c84SRoland McGrath 
1237ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1238ec097c84SRoland McGrath 		if (user_mode(regs)) {
1239ec097c84SRoland McGrath 			current->thread.dbcr0 &= ~DBCR0_BT;
1240ec097c84SRoland McGrath 			current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1241ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1242ec097c84SRoland McGrath 			return;
1243ec097c84SRoland McGrath 		}
1244ec097c84SRoland McGrath 
1245ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1246ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1247ec097c84SRoland McGrath 			return;
1248ec097c84SRoland McGrath 		}
1249ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1250ec097c84SRoland McGrath 			return;
1251ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
125214cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1253f8279621SKumar Gala 
125414cf11afSPaul Mackerras 		/* Disable instruction completion */
125514cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
125614cf11afSPaul Mackerras 		/* Clear the instruction completion event */
125714cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1258f8279621SKumar Gala 
1259f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1260f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
126114cf11afSPaul Mackerras 			return;
126214cf11afSPaul Mackerras 		}
1263f8279621SKumar Gala 
1264f8279621SKumar Gala 		if (debugger_sstep(regs))
1265f8279621SKumar Gala 			return;
1266f8279621SKumar Gala 
12673bffb652SDave Kleikamp 		if (user_mode(regs)) {
12683bffb652SDave Kleikamp 			current->thread.dbcr0 &= ~DBCR0_IC;
12693bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
12703bffb652SDave Kleikamp 			if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
12713bffb652SDave Kleikamp 					       current->thread.dbcr1))
12723bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
12733bffb652SDave Kleikamp 			else
12743bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
12753bffb652SDave Kleikamp 				current->thread.dbcr0 &= ~DBCR0_IDM;
12763bffb652SDave Kleikamp #endif
12773bffb652SDave Kleikamp 		}
1278f8279621SKumar Gala 
1279f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
12803bffb652SDave Kleikamp 	} else
12813bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
128214cf11afSPaul Mackerras }
1283172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
128414cf11afSPaul Mackerras 
128514cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
128614cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
128714cf11afSPaul Mackerras {
128814cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
128914cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
129014cf11afSPaul Mackerras }
129114cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
129214cf11afSPaul Mackerras 
129314cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1294dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
129514cf11afSPaul Mackerras {
129614cf11afSPaul Mackerras 	int err;
129714cf11afSPaul Mackerras 
129814cf11afSPaul Mackerras 	if (!user_mode(regs)) {
129914cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
130014cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
13018dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
130214cf11afSPaul Mackerras 	}
130314cf11afSPaul Mackerras 
1304dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1305dc1c1ca3SStephen Rothwell 
1306eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
130714cf11afSPaul Mackerras 	err = emulate_altivec(regs);
130814cf11afSPaul Mackerras 	if (err == 0) {
130914cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
131014cf11afSPaul Mackerras 		emulate_single_step(regs);
131114cf11afSPaul Mackerras 		return;
131214cf11afSPaul Mackerras 	}
131314cf11afSPaul Mackerras 
131414cf11afSPaul Mackerras 	if (err == -EFAULT) {
131514cf11afSPaul Mackerras 		/* got an error reading the instruction */
131614cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
131714cf11afSPaul Mackerras 	} else {
131814cf11afSPaul Mackerras 		/* didn't recognize the instruction */
131914cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
132014cf11afSPaul Mackerras 		if (printk_ratelimit())
132114cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
132214cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
132314cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
132414cf11afSPaul Mackerras 	}
132514cf11afSPaul Mackerras }
132614cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
132714cf11afSPaul Mackerras 
1328ce48b210SMichael Neuling #ifdef CONFIG_VSX
1329ce48b210SMichael Neuling void vsx_assist_exception(struct pt_regs *regs)
1330ce48b210SMichael Neuling {
1331ce48b210SMichael Neuling 	if (!user_mode(regs)) {
1332ce48b210SMichael Neuling 		printk(KERN_EMERG "VSX assist exception in kernel mode"
1333ce48b210SMichael Neuling 		       " at %lx\n", regs->nip);
1334ce48b210SMichael Neuling 		die("Kernel VSX assist exception", regs, SIGILL);
1335ce48b210SMichael Neuling 	}
1336ce48b210SMichael Neuling 
1337ce48b210SMichael Neuling 	flush_vsx_to_thread(current);
1338ce48b210SMichael Neuling 	printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1339ce48b210SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1340ce48b210SMichael Neuling }
1341ce48b210SMichael Neuling #endif /* CONFIG_VSX */
1342ce48b210SMichael Neuling 
134314cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
134414cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
134514cf11afSPaul Mackerras 			   unsigned long error_code)
134614cf11afSPaul Mackerras {
134714cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
134814cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
134914cf11afSPaul Mackerras 	 * something smarter
135014cf11afSPaul Mackerras 	 */
135114cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
135214cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
135314cf11afSPaul Mackerras 	return;
135414cf11afSPaul Mackerras }
135514cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
135614cf11afSPaul Mackerras 
135714cf11afSPaul Mackerras #ifdef CONFIG_SPE
135814cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
135914cf11afSPaul Mackerras {
13606a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
136114cf11afSPaul Mackerras 	unsigned long spefscr;
136214cf11afSPaul Mackerras 	int fpexc_mode;
136314cf11afSPaul Mackerras 	int code = 0;
13646a800f36SLiu Yu 	int err;
13656a800f36SLiu Yu 
13666a800f36SLiu Yu 	preempt_disable();
13676a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
13686a800f36SLiu Yu 		giveup_spe(current);
13696a800f36SLiu Yu 	preempt_enable();
137014cf11afSPaul Mackerras 
137114cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
137214cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
137314cf11afSPaul Mackerras 
137414cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
137514cf11afSPaul Mackerras 		code = FPE_FLTOVF;
137614cf11afSPaul Mackerras 	}
137714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
137814cf11afSPaul Mackerras 		code = FPE_FLTUND;
137914cf11afSPaul Mackerras 	}
138014cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
138114cf11afSPaul Mackerras 		code = FPE_FLTDIV;
138214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
138314cf11afSPaul Mackerras 		code = FPE_FLTINV;
138414cf11afSPaul Mackerras 	}
138514cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
138614cf11afSPaul Mackerras 		code = FPE_FLTRES;
138714cf11afSPaul Mackerras 
13886a800f36SLiu Yu 	err = do_spe_mathemu(regs);
13896a800f36SLiu Yu 	if (err == 0) {
13906a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
13916a800f36SLiu Yu 		emulate_single_step(regs);
139214cf11afSPaul Mackerras 		return;
139314cf11afSPaul Mackerras 	}
13946a800f36SLiu Yu 
13956a800f36SLiu Yu 	if (err == -EFAULT) {
13966a800f36SLiu Yu 		/* got an error reading the instruction */
13976a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
13986a800f36SLiu Yu 	} else if (err == -EINVAL) {
13996a800f36SLiu Yu 		/* didn't recognize the instruction */
14006a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14016a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14026a800f36SLiu Yu 	} else {
14036a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
14046a800f36SLiu Yu 	}
14056a800f36SLiu Yu 
14066a800f36SLiu Yu 	return;
14076a800f36SLiu Yu }
14086a800f36SLiu Yu 
14096a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
14106a800f36SLiu Yu {
14116a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
14126a800f36SLiu Yu 	int err;
14136a800f36SLiu Yu 
14146a800f36SLiu Yu 	preempt_disable();
14156a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
14166a800f36SLiu Yu 		giveup_spe(current);
14176a800f36SLiu Yu 	preempt_enable();
14186a800f36SLiu Yu 
14196a800f36SLiu Yu 	regs->nip -= 4;
14206a800f36SLiu Yu 	err = speround_handler(regs);
14216a800f36SLiu Yu 	if (err == 0) {
14226a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
14236a800f36SLiu Yu 		emulate_single_step(regs);
14246a800f36SLiu Yu 		return;
14256a800f36SLiu Yu 	}
14266a800f36SLiu Yu 
14276a800f36SLiu Yu 	if (err == -EFAULT) {
14286a800f36SLiu Yu 		/* got an error reading the instruction */
14296a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
14306a800f36SLiu Yu 	} else if (err == -EINVAL) {
14316a800f36SLiu Yu 		/* didn't recognize the instruction */
14326a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
14336a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
14346a800f36SLiu Yu 	} else {
14356a800f36SLiu Yu 		_exception(SIGFPE, regs, 0, regs->nip);
14366a800f36SLiu Yu 		return;
14376a800f36SLiu Yu 	}
14386a800f36SLiu Yu }
143914cf11afSPaul Mackerras #endif
144014cf11afSPaul Mackerras 
1441dc1c1ca3SStephen Rothwell /*
1442dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1443dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1444dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1445dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1446dc1c1ca3SStephen Rothwell  */
1447dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1448dc1c1ca3SStephen Rothwell {
1449dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1450dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1451dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1452dc1c1ca3SStephen Rothwell }
1453dc1c1ca3SStephen Rothwell 
145414cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
145514cf11afSPaul Mackerras /*
145614cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
145714cf11afSPaul Mackerras  * spins until a reboot occurs
145814cf11afSPaul Mackerras  */
145914cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
146014cf11afSPaul Mackerras {
146114cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
146214cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
146314cf11afSPaul Mackerras 	return;
146414cf11afSPaul Mackerras }
146514cf11afSPaul Mackerras 
146614cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
146714cf11afSPaul Mackerras {
146814cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
146914cf11afSPaul Mackerras 	WatchdogHandler(regs);
147014cf11afSPaul Mackerras }
147114cf11afSPaul Mackerras #endif
1472dc1c1ca3SStephen Rothwell 
1473dc1c1ca3SStephen Rothwell /*
1474dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1475dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1476dc1c1ca3SStephen Rothwell  */
1477dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1478dc1c1ca3SStephen Rothwell {
1479dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1480dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1481dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1482dc1c1ca3SStephen Rothwell }
148314cf11afSPaul Mackerras 
148414cf11afSPaul Mackerras void __init trap_init(void)
148514cf11afSPaul Mackerras {
148614cf11afSPaul Mackerras }
148780947e7cSGeert Uytterhoeven 
148880947e7cSGeert Uytterhoeven 
148980947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
149080947e7cSGeert Uytterhoeven 
149180947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
149280947e7cSGeert Uytterhoeven 
149380947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
149480947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
149580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
149680947e7cSGeert Uytterhoeven #endif
149780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
149880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
149980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
150080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
150180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
150280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
150380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
150480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
150580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
150680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
150780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
150880947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
150980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
151080947e7cSGeert Uytterhoeven #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
151180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(8xx),
151280947e7cSGeert Uytterhoeven #endif
151380947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
151480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
151580947e7cSGeert Uytterhoeven #endif
151680947e7cSGeert Uytterhoeven };
151780947e7cSGeert Uytterhoeven 
151880947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
151980947e7cSGeert Uytterhoeven 
152080947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
152180947e7cSGeert Uytterhoeven {
152280947e7cSGeert Uytterhoeven 	if (printk_ratelimit())
152380947e7cSGeert Uytterhoeven 		pr_warning("%s used emulated %s instruction\n", current->comm,
152480947e7cSGeert Uytterhoeven 			   type);
152580947e7cSGeert Uytterhoeven }
152680947e7cSGeert Uytterhoeven 
152780947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
152880947e7cSGeert Uytterhoeven {
152980947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
153080947e7cSGeert Uytterhoeven 	unsigned int i;
153180947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
153280947e7cSGeert Uytterhoeven 
153380947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
153480947e7cSGeert Uytterhoeven 		return -ENODEV;
153580947e7cSGeert Uytterhoeven 
153680947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
153780947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
153880947e7cSGeert Uytterhoeven 	if (!dir)
153980947e7cSGeert Uytterhoeven 		return -ENOMEM;
154080947e7cSGeert Uytterhoeven 
154180947e7cSGeert Uytterhoeven 	d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
154280947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
154380947e7cSGeert Uytterhoeven 	if (!d)
154480947e7cSGeert Uytterhoeven 		goto fail;
154580947e7cSGeert Uytterhoeven 
154680947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
154780947e7cSGeert Uytterhoeven 		d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
154880947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
154980947e7cSGeert Uytterhoeven 		if (!d)
155080947e7cSGeert Uytterhoeven 			goto fail;
155180947e7cSGeert Uytterhoeven 	}
155280947e7cSGeert Uytterhoeven 
155380947e7cSGeert Uytterhoeven 	return 0;
155480947e7cSGeert Uytterhoeven 
155580947e7cSGeert Uytterhoeven fail:
155680947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
155780947e7cSGeert Uytterhoeven 	return -ENOMEM;
155880947e7cSGeert Uytterhoeven }
155980947e7cSGeert Uytterhoeven 
156080947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
156180947e7cSGeert Uytterhoeven 
156280947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
1563