xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision c1469f13de275fc92b051328ea5764a32a5a43c9)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
314cf11afSPaul Mackerras  *
414cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
514cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
614cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
714cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
814cf11afSPaul Mackerras  *
914cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1014cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1114cf11afSPaul Mackerras  */
1214cf11afSPaul Mackerras 
1314cf11afSPaul Mackerras /*
1414cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1514cf11afSPaul Mackerras  */
1614cf11afSPaul Mackerras 
1714cf11afSPaul Mackerras #include <linux/errno.h>
1814cf11afSPaul Mackerras #include <linux/sched.h>
1914cf11afSPaul Mackerras #include <linux/kernel.h>
2014cf11afSPaul Mackerras #include <linux/mm.h>
2114cf11afSPaul Mackerras #include <linux/stddef.h>
2214cf11afSPaul Mackerras #include <linux/unistd.h>
238dad3f92SPaul Mackerras #include <linux/ptrace.h>
2414cf11afSPaul Mackerras #include <linux/slab.h>
2514cf11afSPaul Mackerras #include <linux/user.h>
2614cf11afSPaul Mackerras #include <linux/a.out.h>
2714cf11afSPaul Mackerras #include <linux/interrupt.h>
2814cf11afSPaul Mackerras #include <linux/init.h>
2914cf11afSPaul Mackerras #include <linux/module.h>
308dad3f92SPaul Mackerras #include <linux/prctl.h>
3114cf11afSPaul Mackerras #include <linux/delay.h>
3214cf11afSPaul Mackerras #include <linux/kprobes.h>
33cc532915SMichael Ellerman #include <linux/kexec.h>
345474c120SMichael Hanselmann #include <linux/backlight.h>
3573c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
361eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3714cf11afSPaul Mackerras 
3814cf11afSPaul Mackerras #include <asm/pgtable.h>
3914cf11afSPaul Mackerras #include <asm/uaccess.h>
4014cf11afSPaul Mackerras #include <asm/system.h>
4114cf11afSPaul Mackerras #include <asm/io.h>
4286417780SPaul Mackerras #include <asm/machdep.h>
4386417780SPaul Mackerras #include <asm/rtas.h>
44f7f6f4feSDavid Gibson #include <asm/pmc.h>
45dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC32
4614cf11afSPaul Mackerras #include <asm/reg.h>
4786417780SPaul Mackerras #endif
4814cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
4914cf11afSPaul Mackerras #include <asm/backlight.h>
5014cf11afSPaul Mackerras #endif
51dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5286417780SPaul Mackerras #include <asm/firmware.h>
53dc1c1ca3SStephen Rothwell #include <asm/processor.h>
54dc1c1ca3SStephen Rothwell #endif
55c0ce7d08SDavid Wilder #include <asm/kexec.h>
56dc1c1ca3SStephen Rothwell 
5714cf11afSPaul Mackerras #ifdef CONFIG_DEBUGGER
5814cf11afSPaul Mackerras int (*__debugger)(struct pt_regs *regs);
5914cf11afSPaul Mackerras int (*__debugger_ipi)(struct pt_regs *regs);
6014cf11afSPaul Mackerras int (*__debugger_bpt)(struct pt_regs *regs);
6114cf11afSPaul Mackerras int (*__debugger_sstep)(struct pt_regs *regs);
6214cf11afSPaul Mackerras int (*__debugger_iabr_match)(struct pt_regs *regs);
6314cf11afSPaul Mackerras int (*__debugger_dabr_match)(struct pt_regs *regs);
6414cf11afSPaul Mackerras int (*__debugger_fault_handler)(struct pt_regs *regs);
6514cf11afSPaul Mackerras 
6614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
6714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
6814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
6914cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
7014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
7114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_dabr_match);
7214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
7314cf11afSPaul Mackerras #endif
7414cf11afSPaul Mackerras 
7514cf11afSPaul Mackerras /*
7614cf11afSPaul Mackerras  * Trap & Exception support
7714cf11afSPaul Mackerras  */
7814cf11afSPaul Mackerras 
796031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
806031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
816031d9d9Santon@samba.org {
826031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
836031d9d9Santon@samba.org 	if (pmac_backlight) {
846031d9d9Santon@samba.org 		struct backlight_properties *props;
856031d9d9Santon@samba.org 
866031d9d9Santon@samba.org 		props = &pmac_backlight->props;
876031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
886031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
896031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
906031d9d9Santon@samba.org 	}
916031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
926031d9d9Santon@samba.org }
936031d9d9Santon@samba.org #else
946031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
956031d9d9Santon@samba.org #endif
966031d9d9Santon@samba.org 
9714cf11afSPaul Mackerras int die(const char *str, struct pt_regs *regs, long err)
9814cf11afSPaul Mackerras {
9934c2a14fSanton@samba.org 	static struct {
10034c2a14fSanton@samba.org 		spinlock_t lock;
10134c2a14fSanton@samba.org 		u32 lock_owner;
10234c2a14fSanton@samba.org 		int lock_owner_depth;
10334c2a14fSanton@samba.org 	} die = {
10434c2a14fSanton@samba.org 		.lock =			__SPIN_LOCK_UNLOCKED(die.lock),
10534c2a14fSanton@samba.org 		.lock_owner =		-1,
10634c2a14fSanton@samba.org 		.lock_owner_depth =	0
10734c2a14fSanton@samba.org 	};
108c0ce7d08SDavid Wilder 	static int die_counter;
10934c2a14fSanton@samba.org 	unsigned long flags;
11014cf11afSPaul Mackerras 
11114cf11afSPaul Mackerras 	if (debugger(regs))
11214cf11afSPaul Mackerras 		return 1;
11314cf11afSPaul Mackerras 
114293e4688Santon@samba.org 	oops_enter();
115293e4688Santon@samba.org 
11634c2a14fSanton@samba.org 	if (die.lock_owner != raw_smp_processor_id()) {
11714cf11afSPaul Mackerras 		console_verbose();
11834c2a14fSanton@samba.org 		spin_lock_irqsave(&die.lock, flags);
11934c2a14fSanton@samba.org 		die.lock_owner = smp_processor_id();
12034c2a14fSanton@samba.org 		die.lock_owner_depth = 0;
12114cf11afSPaul Mackerras 		bust_spinlocks(1);
1226031d9d9Santon@samba.org 		if (machine_is(powermac))
1236031d9d9Santon@samba.org 			pmac_backlight_unblank();
12434c2a14fSanton@samba.org 	} else {
12534c2a14fSanton@samba.org 		local_save_flags(flags);
12634c2a14fSanton@samba.org 	}
1275474c120SMichael Hanselmann 
12834c2a14fSanton@samba.org 	if (++die.lock_owner_depth < 3) {
12914cf11afSPaul Mackerras 		printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
13014cf11afSPaul Mackerras #ifdef CONFIG_PREEMPT
13114cf11afSPaul Mackerras 		printk("PREEMPT ");
13214cf11afSPaul Mackerras #endif
13314cf11afSPaul Mackerras #ifdef CONFIG_SMP
13414cf11afSPaul Mackerras 		printk("SMP NR_CPUS=%d ", NR_CPUS);
13514cf11afSPaul Mackerras #endif
13614cf11afSPaul Mackerras #ifdef CONFIG_DEBUG_PAGEALLOC
13714cf11afSPaul Mackerras 		printk("DEBUG_PAGEALLOC ");
13814cf11afSPaul Mackerras #endif
13914cf11afSPaul Mackerras #ifdef CONFIG_NUMA
14014cf11afSPaul Mackerras 		printk("NUMA ");
14114cf11afSPaul Mackerras #endif
142ae7f4463Santon@samba.org 		printk("%s\n", ppc_md.name ? ppc_md.name : "");
143e8222502SBenjamin Herrenschmidt 
14414cf11afSPaul Mackerras 		print_modules();
14514cf11afSPaul Mackerras 		show_regs(regs);
14634c2a14fSanton@samba.org 	} else {
14734c2a14fSanton@samba.org 		printk("Recursive die() failure, output suppressed\n");
14834c2a14fSanton@samba.org 	}
14934c2a14fSanton@samba.org 
15014cf11afSPaul Mackerras 	bust_spinlocks(0);
15134c2a14fSanton@samba.org 	die.lock_owner = -1;
152bcdcd8e7SPavel Emelianov 	add_taint(TAINT_DIE);
15334c2a14fSanton@samba.org 	spin_unlock_irqrestore(&die.lock, flags);
154cc532915SMichael Ellerman 
155c0ce7d08SDavid Wilder 	if (kexec_should_crash(current) ||
156c0ce7d08SDavid Wilder 		kexec_sr_activated(smp_processor_id()))
157cc532915SMichael Ellerman 		crash_kexec(regs);
158c0ce7d08SDavid Wilder 	crash_kexec_secondary(regs);
15914cf11afSPaul Mackerras 
16014cf11afSPaul Mackerras 	if (in_interrupt())
16114cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
16214cf11afSPaul Mackerras 
163cea6a4baSHorms 	if (panic_on_oops)
164012c437dSHorms 		panic("Fatal exception");
165cea6a4baSHorms 
166293e4688Santon@samba.org 	oops_exit();
16714cf11afSPaul Mackerras 	do_exit(err);
16814cf11afSPaul Mackerras 
16914cf11afSPaul Mackerras 	return 0;
17014cf11afSPaul Mackerras }
17114cf11afSPaul Mackerras 
17214cf11afSPaul Mackerras void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
17314cf11afSPaul Mackerras {
17414cf11afSPaul Mackerras 	siginfo_t info;
175d0c3d534SOlof Johansson 	const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
176d0c3d534SOlof Johansson 			"at %08lx nip %08lx lr %08lx code %x\n";
177d0c3d534SOlof Johansson 	const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
178d0c3d534SOlof Johansson 			"at %016lx nip %016lx lr %016lx code %x\n";
17914cf11afSPaul Mackerras 
18014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
18114cf11afSPaul Mackerras 		if (die("Exception in kernel mode", regs, signr))
18214cf11afSPaul Mackerras 			return;
183d0c3d534SOlof Johansson 	} else if (show_unhandled_signals &&
184d0c3d534SOlof Johansson 		    unhandled_signal(current, signr) &&
185d0c3d534SOlof Johansson 		    printk_ratelimit()) {
186d0c3d534SOlof Johansson 			printk(regs->msr & MSR_SF ? fmt64 : fmt32,
187d0c3d534SOlof Johansson 				current->comm, current->pid, signr,
188d0c3d534SOlof Johansson 				addr, regs->nip, regs->link, code);
18914cf11afSPaul Mackerras 		}
19014cf11afSPaul Mackerras 
19114cf11afSPaul Mackerras 	memset(&info, 0, sizeof(info));
19214cf11afSPaul Mackerras 	info.si_signo = signr;
19314cf11afSPaul Mackerras 	info.si_code = code;
19414cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
19514cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
19614cf11afSPaul Mackerras 
19714cf11afSPaul Mackerras 	/*
19814cf11afSPaul Mackerras 	 * Init gets no signals that it doesn't have a handler for.
19914cf11afSPaul Mackerras 	 * That's all very well, but if it has caused a synchronous
20014cf11afSPaul Mackerras 	 * exception and we ignore the resulting signal, it will just
20114cf11afSPaul Mackerras 	 * generate the same exception over and over again and we get
20214cf11afSPaul Mackerras 	 * nowhere.  Better to kill it and let the kernel panic.
20314cf11afSPaul Mackerras 	 */
204b460cbc5SSerge E. Hallyn 	if (is_global_init(current)) {
20514cf11afSPaul Mackerras 		__sighandler_t handler;
20614cf11afSPaul Mackerras 
20714cf11afSPaul Mackerras 		spin_lock_irq(&current->sighand->siglock);
20814cf11afSPaul Mackerras 		handler = current->sighand->action[signr-1].sa.sa_handler;
20914cf11afSPaul Mackerras 		spin_unlock_irq(&current->sighand->siglock);
21014cf11afSPaul Mackerras 		if (handler == SIG_DFL) {
21114cf11afSPaul Mackerras 			/* init has generated a synchronous exception
21214cf11afSPaul Mackerras 			   and it doesn't have a handler for the signal */
21314cf11afSPaul Mackerras 			printk(KERN_CRIT "init has generated signal %d "
21414cf11afSPaul Mackerras 			       "but has no handler for it\n", signr);
21514cf11afSPaul Mackerras 			do_exit(signr);
21614cf11afSPaul Mackerras 		}
21714cf11afSPaul Mackerras 	}
21814cf11afSPaul Mackerras }
21914cf11afSPaul Mackerras 
22014cf11afSPaul Mackerras #ifdef CONFIG_PPC64
22114cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
22214cf11afSPaul Mackerras {
22314cf11afSPaul Mackerras 	/* See if any machine dependent calls */
224c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
225c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
226c902be71SArnd Bergmann 			return;
227c902be71SArnd Bergmann 	}
22814cf11afSPaul Mackerras 
229c0ce7d08SDavid Wilder #ifdef CONFIG_KEXEC
230c0ce7d08SDavid Wilder 	cpu_set(smp_processor_id(), cpus_in_sr);
231c0ce7d08SDavid Wilder #endif
232c0ce7d08SDavid Wilder 
2338dad3f92SPaul Mackerras 	die("System Reset", regs, SIGABRT);
23414cf11afSPaul Mackerras 
235eac8392fSDavid Wilder 	/*
236eac8392fSDavid Wilder 	 * Some CPUs when released from the debugger will execute this path.
237eac8392fSDavid Wilder 	 * These CPUs entered the debugger via a soft-reset. If the CPU was
238eac8392fSDavid Wilder 	 * hung before entering the debugger it will return to the hung
239eac8392fSDavid Wilder 	 * state when exiting this function.  This causes a problem in
240eac8392fSDavid Wilder 	 * kdump since the hung CPU(s) will not respond to the IPI sent
241eac8392fSDavid Wilder 	 * from kdump. To prevent the problem we call crash_kexec_secondary()
242eac8392fSDavid Wilder 	 * here. If a kdump had not been initiated or we exit the debugger
243eac8392fSDavid Wilder 	 * with the "exit and recover" command (x) crash_kexec_secondary()
244eac8392fSDavid Wilder 	 * will return after 5ms and the CPU returns to its previous state.
245eac8392fSDavid Wilder 	 */
246eac8392fSDavid Wilder 	crash_kexec_secondary(regs);
247eac8392fSDavid Wilder 
24814cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
24914cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
25014cf11afSPaul Mackerras 		panic("Unrecoverable System Reset");
25114cf11afSPaul Mackerras 
25214cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
25314cf11afSPaul Mackerras }
25414cf11afSPaul Mackerras #endif
25514cf11afSPaul Mackerras 
25614cf11afSPaul Mackerras /*
25714cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
25814cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
25914cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
26014cf11afSPaul Mackerras  * table.
26114cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
26214cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
26314cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
26414cf11afSPaul Mackerras  *  -- paulus.
26514cf11afSPaul Mackerras  */
26614cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
26714cf11afSPaul Mackerras {
26868a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
26914cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
27014cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
27114cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
27214cf11afSPaul Mackerras 
27314cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
27414cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
27514cf11afSPaul Mackerras 		/*
27614cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
27714cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
27814cf11afSPaul Mackerras 		 * As the address is in the exception table
27914cf11afSPaul Mackerras 		 * we should be able to read the instr there.
28014cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
28114cf11afSPaul Mackerras 		 * load or store.
28214cf11afSPaul Mackerras 		 */
28314cf11afSPaul Mackerras 		if (*nip == 0x60000000)		/* nop */
28414cf11afSPaul Mackerras 			nip -= 2;
28514cf11afSPaul Mackerras 		else if (*nip == 0x4c00012c)	/* isync */
28614cf11afSPaul Mackerras 			--nip;
28714cf11afSPaul Mackerras 		if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
28814cf11afSPaul Mackerras 			/* sync or twi */
28914cf11afSPaul Mackerras 			unsigned int rb;
29014cf11afSPaul Mackerras 
29114cf11afSPaul Mackerras 			--nip;
29214cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
29314cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
29414cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
29514cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
29614cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
29714cf11afSPaul Mackerras 			regs->nip = entry->fixup;
29814cf11afSPaul Mackerras 			return 1;
29914cf11afSPaul Mackerras 		}
30014cf11afSPaul Mackerras 	}
30168a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
30214cf11afSPaul Mackerras 	return 0;
30314cf11afSPaul Mackerras }
30414cf11afSPaul Mackerras 
30514cf11afSPaul Mackerras #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
30614cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
30714cf11afSPaul Mackerras    is in the ESR. */
30814cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
30914cf11afSPaul Mackerras #ifndef CONFIG_FSL_BOOKE
31014cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->dsisr)
31114cf11afSPaul Mackerras #else
31286d7a9a9SBecky Bruce #define get_mc_reason(regs)	(mfspr(SPRN_MCSR) & MCSR_MASK)
31314cf11afSPaul Mackerras #endif
31414cf11afSPaul Mackerras #define REASON_FP		ESR_FP
31514cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
31614cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
31714cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
31814cf11afSPaul Mackerras 
31914cf11afSPaul Mackerras /* single-step stuff */
32014cf11afSPaul Mackerras #define single_stepping(regs)	(current->thread.dbcr0 & DBCR0_IC)
32114cf11afSPaul Mackerras #define clear_single_step(regs)	(current->thread.dbcr0 &= ~DBCR0_IC)
32214cf11afSPaul Mackerras 
32314cf11afSPaul Mackerras #else
32414cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
32514cf11afSPaul Mackerras    exception is in the MSR. */
32614cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
32714cf11afSPaul Mackerras #define get_mc_reason(regs)	((regs)->msr)
32814cf11afSPaul Mackerras #define REASON_FP		0x100000
32914cf11afSPaul Mackerras #define REASON_ILLEGAL		0x80000
33014cf11afSPaul Mackerras #define REASON_PRIVILEGED	0x40000
33114cf11afSPaul Mackerras #define REASON_TRAP		0x20000
33214cf11afSPaul Mackerras 
33314cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
33414cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
33514cf11afSPaul Mackerras #endif
33614cf11afSPaul Mackerras 
33775918a4bSOlof Johansson static int generic_machine_check_exception(struct pt_regs *regs)
33814cf11afSPaul Mackerras {
3391a6a4ffeSKumar Gala 	unsigned long reason = get_mc_reason(regs);
34014cf11afSPaul Mackerras 
34114cf11afSPaul Mackerras #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
34214cf11afSPaul Mackerras 	if (reason & ESR_IMCP) {
34314cf11afSPaul Mackerras 		printk("Instruction");
34414cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
34514cf11afSPaul Mackerras 	} else
34614cf11afSPaul Mackerras 		printk("Data");
34714cf11afSPaul Mackerras 	printk(" machine check in kernel mode.\n");
34814cf11afSPaul Mackerras #elif defined(CONFIG_440A)
34914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
35014cf11afSPaul Mackerras 	if (reason & ESR_IMCP){
35114cf11afSPaul Mackerras 		printk("Instruction Synchronous Machine Check exception\n");
35214cf11afSPaul Mackerras 		mtspr(SPRN_ESR, reason & ~ESR_IMCP);
35314cf11afSPaul Mackerras 	}
35414cf11afSPaul Mackerras 	else {
35514cf11afSPaul Mackerras 		u32 mcsr = mfspr(SPRN_MCSR);
35614cf11afSPaul Mackerras 		if (mcsr & MCSR_IB)
35714cf11afSPaul Mackerras 			printk("Instruction Read PLB Error\n");
35814cf11afSPaul Mackerras 		if (mcsr & MCSR_DRB)
35914cf11afSPaul Mackerras 			printk("Data Read PLB Error\n");
36014cf11afSPaul Mackerras 		if (mcsr & MCSR_DWB)
36114cf11afSPaul Mackerras 			printk("Data Write PLB Error\n");
36214cf11afSPaul Mackerras 		if (mcsr & MCSR_TLBP)
36314cf11afSPaul Mackerras 			printk("TLB Parity Error\n");
36414cf11afSPaul Mackerras 		if (mcsr & MCSR_ICP){
36514cf11afSPaul Mackerras 			flush_instruction_cache();
36614cf11afSPaul Mackerras 			printk("I-Cache Parity Error\n");
36714cf11afSPaul Mackerras 		}
36814cf11afSPaul Mackerras 		if (mcsr & MCSR_DCSP)
36914cf11afSPaul Mackerras 			printk("D-Cache Search Parity Error\n");
37014cf11afSPaul Mackerras 		if (mcsr & MCSR_DCFP)
37114cf11afSPaul Mackerras 			printk("D-Cache Flush Parity Error\n");
37214cf11afSPaul Mackerras 		if (mcsr & MCSR_IMPE)
37314cf11afSPaul Mackerras 			printk("Machine Check exception is imprecise\n");
37414cf11afSPaul Mackerras 
37514cf11afSPaul Mackerras 		/* Clear MCSR */
37614cf11afSPaul Mackerras 		mtspr(SPRN_MCSR, mcsr);
37714cf11afSPaul Mackerras 	}
37814cf11afSPaul Mackerras #elif defined (CONFIG_E500)
37914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
38014cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
38114cf11afSPaul Mackerras 
38214cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
38314cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
38414cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
38514cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
38614cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
38714cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
38814cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
38914cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
39014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
39114cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
39214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
39314cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
39414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
39514cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
39614cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
39714cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
39814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
39914cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
40014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
40114cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
40214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
40314cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
40414cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
40514cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
40614cf11afSPaul Mackerras #elif defined (CONFIG_E200)
40714cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
40814cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
40914cf11afSPaul Mackerras 
41014cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
41114cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
41214cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
41314cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
41414cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
41514cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
41614cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
41714cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
41814cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
41914cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
42014cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
42114cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
42214cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
42314cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
42414cf11afSPaul Mackerras #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
42514cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
42614cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
42714cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
42814cf11afSPaul Mackerras 	case 0x80000:
42914cf11afSPaul Mackerras 		printk("Machine check signal\n");
43014cf11afSPaul Mackerras 		break;
43114cf11afSPaul Mackerras 	case 0:		/* for 601 */
43214cf11afSPaul Mackerras 	case 0x40000:
43314cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
43414cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
43514cf11afSPaul Mackerras 		break;
43614cf11afSPaul Mackerras 	case 0x20000:
43714cf11afSPaul Mackerras 		printk("Data parity error signal\n");
43814cf11afSPaul Mackerras 		break;
43914cf11afSPaul Mackerras 	case 0x10000:
44014cf11afSPaul Mackerras 		printk("Address parity error signal\n");
44114cf11afSPaul Mackerras 		break;
44214cf11afSPaul Mackerras 	case 0x20000000:
44314cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
44414cf11afSPaul Mackerras 		break;
44514cf11afSPaul Mackerras 	case 0x40000000:
44614cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
44714cf11afSPaul Mackerras 		break;
44814cf11afSPaul Mackerras 	case 0x00100000:
44914cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
45014cf11afSPaul Mackerras 		break;
45114cf11afSPaul Mackerras 	default:
45214cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
45314cf11afSPaul Mackerras 	}
45414cf11afSPaul Mackerras #endif /* CONFIG_4xx */
45514cf11afSPaul Mackerras 
45675918a4bSOlof Johansson 	return 0;
45775918a4bSOlof Johansson }
45875918a4bSOlof Johansson 
45975918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
46075918a4bSOlof Johansson {
46175918a4bSOlof Johansson 	int recover = 0;
46275918a4bSOlof Johansson 
46375918a4bSOlof Johansson 	/* See if any machine dependent calls */
46475918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
46575918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
46675918a4bSOlof Johansson 	else
46775918a4bSOlof Johansson 		recover = generic_machine_check_exception(regs);
46875918a4bSOlof Johansson 
46975918a4bSOlof Johansson 	if (recover)
47075918a4bSOlof Johansson 		return;
47175918a4bSOlof Johansson 
47275918a4bSOlof Johansson 	if (user_mode(regs)) {
47375918a4bSOlof Johansson 		regs->msr |= MSR_RI;
47475918a4bSOlof Johansson 		_exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
47575918a4bSOlof Johansson 		return;
47675918a4bSOlof Johansson 	}
47775918a4bSOlof Johansson 
47875918a4bSOlof Johansson #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
47975918a4bSOlof Johansson 	/* the qspan pci read routines can cause machine checks -- Cort */
48075918a4bSOlof Johansson 	bad_page_fault(regs, regs->dar, SIGBUS);
48175918a4bSOlof Johansson 	return;
48275918a4bSOlof Johansson #endif
48375918a4bSOlof Johansson 
48475918a4bSOlof Johansson 	if (debugger_fault_handler(regs)) {
48575918a4bSOlof Johansson 		regs->msr |= MSR_RI;
48675918a4bSOlof Johansson 		return;
48775918a4bSOlof Johansson 	}
48875918a4bSOlof Johansson 
48975918a4bSOlof Johansson 	if (check_io_access(regs))
49075918a4bSOlof Johansson 		return;
49175918a4bSOlof Johansson 
49214cf11afSPaul Mackerras 	if (debugger_fault_handler(regs))
49314cf11afSPaul Mackerras 		return;
4948dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
49514cf11afSPaul Mackerras 
49614cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
49714cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
49814cf11afSPaul Mackerras 		panic("Unrecoverable Machine check");
49914cf11afSPaul Mackerras }
50014cf11afSPaul Mackerras 
50114cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
50214cf11afSPaul Mackerras {
50314cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
50414cf11afSPaul Mackerras }
50514cf11afSPaul Mackerras 
506dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
50714cf11afSPaul Mackerras {
50814cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
50914cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
51014cf11afSPaul Mackerras 
51114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
51214cf11afSPaul Mackerras }
51314cf11afSPaul Mackerras 
514dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
51514cf11afSPaul Mackerras {
51614cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
51714cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
51814cf11afSPaul Mackerras 		return;
51914cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
52014cf11afSPaul Mackerras 		return;
52114cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
52214cf11afSPaul Mackerras }
52314cf11afSPaul Mackerras 
52414cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
52514cf11afSPaul Mackerras {
52614cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, 0, 0);
52714cf11afSPaul Mackerras }
52814cf11afSPaul Mackerras 
5298dad3f92SPaul Mackerras void __kprobes single_step_exception(struct pt_regs *regs)
53014cf11afSPaul Mackerras {
53114cf11afSPaul Mackerras 	regs->msr &= ~(MSR_SE | MSR_BE);  /* Turn off 'trace' bits */
53214cf11afSPaul Mackerras 
53314cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
53414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
53514cf11afSPaul Mackerras 		return;
53614cf11afSPaul Mackerras 	if (debugger_sstep(regs))
53714cf11afSPaul Mackerras 		return;
53814cf11afSPaul Mackerras 
53914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
54014cf11afSPaul Mackerras }
54114cf11afSPaul Mackerras 
54214cf11afSPaul Mackerras /*
54314cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
54414cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
54514cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
54614cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
54714cf11afSPaul Mackerras  */
5488dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
54914cf11afSPaul Mackerras {
55014cf11afSPaul Mackerras 	if (single_stepping(regs)) {
55114cf11afSPaul Mackerras 		clear_single_step(regs);
55214cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
55314cf11afSPaul Mackerras 	}
55414cf11afSPaul Mackerras }
55514cf11afSPaul Mackerras 
5565fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
557dc1c1ca3SStephen Rothwell {
5585fad293bSKumar Gala 	int ret = 0;
559dc1c1ca3SStephen Rothwell 
560dc1c1ca3SStephen Rothwell 	/* Invalid operation */
561dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
5625fad293bSKumar Gala 		ret = FPE_FLTINV;
563dc1c1ca3SStephen Rothwell 
564dc1c1ca3SStephen Rothwell 	/* Overflow */
565dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
5665fad293bSKumar Gala 		ret = FPE_FLTOVF;
567dc1c1ca3SStephen Rothwell 
568dc1c1ca3SStephen Rothwell 	/* Underflow */
569dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
5705fad293bSKumar Gala 		ret = FPE_FLTUND;
571dc1c1ca3SStephen Rothwell 
572dc1c1ca3SStephen Rothwell 	/* Divide by zero */
573dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
5745fad293bSKumar Gala 		ret = FPE_FLTDIV;
575dc1c1ca3SStephen Rothwell 
576dc1c1ca3SStephen Rothwell 	/* Inexact result */
577dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
5785fad293bSKumar Gala 		ret = FPE_FLTRES;
5795fad293bSKumar Gala 
5805fad293bSKumar Gala 	return ret;
5815fad293bSKumar Gala }
5825fad293bSKumar Gala 
5835fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
5845fad293bSKumar Gala {
5855fad293bSKumar Gala 	int code = 0;
5865fad293bSKumar Gala 
5875fad293bSKumar Gala 	flush_fp_to_thread(current);
5885fad293bSKumar Gala 
5895fad293bSKumar Gala 	code = __parse_fpscr(current->thread.fpscr.val);
590dc1c1ca3SStephen Rothwell 
591dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
592dc1c1ca3SStephen Rothwell }
593dc1c1ca3SStephen Rothwell 
594dc1c1ca3SStephen Rothwell /*
595dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
59614cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
59714cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
59814cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
59914cf11afSPaul Mackerras  *
60014cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
60114cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
60214cf11afSPaul Mackerras  * bits is faster and easier.
60386417780SPaul Mackerras  *
60414cf11afSPaul Mackerras  */
60514cf11afSPaul Mackerras #define INST_MFSPR_PVR		0x7c1f42a6
60614cf11afSPaul Mackerras #define INST_MFSPR_PVR_MASK	0xfc1fffff
60714cf11afSPaul Mackerras 
60814cf11afSPaul Mackerras #define INST_DCBA		0x7c0005ec
60987589f08SPaul Mackerras #define INST_DCBA_MASK		0xfc0007fe
61014cf11afSPaul Mackerras 
61114cf11afSPaul Mackerras #define INST_MCRXR		0x7c000400
61287589f08SPaul Mackerras #define INST_MCRXR_MASK		0xfc0007fe
61314cf11afSPaul Mackerras 
61414cf11afSPaul Mackerras #define INST_STRING		0x7c00042a
61587589f08SPaul Mackerras #define INST_STRING_MASK	0xfc0007fe
61687589f08SPaul Mackerras #define INST_STRING_GEN_MASK	0xfc00067e
61714cf11afSPaul Mackerras #define INST_LSWI		0x7c0004aa
61814cf11afSPaul Mackerras #define INST_LSWX		0x7c00042a
61914cf11afSPaul Mackerras #define INST_STSWI		0x7c0005aa
62014cf11afSPaul Mackerras #define INST_STSWX		0x7c00052a
62114cf11afSPaul Mackerras 
622c3412dcbSWill Schmidt #define INST_POPCNTB		0x7c0000f4
623c3412dcbSWill Schmidt #define INST_POPCNTB_MASK	0xfc0007fe
624c3412dcbSWill Schmidt 
625*c1469f13SKumar Gala #define INST_ISEL		0x7c00001e
626*c1469f13SKumar Gala #define INST_ISEL_MASK		0xfc00003e
627*c1469f13SKumar Gala 
62814cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
62914cf11afSPaul Mackerras {
63014cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
63114cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
63214cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
63314cf11afSPaul Mackerras 	u32 num_bytes;
63414cf11afSPaul Mackerras 	unsigned long EA;
63514cf11afSPaul Mackerras 	int pos = 0;
63614cf11afSPaul Mackerras 
63714cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
63814cf11afSPaul Mackerras 	if ((instword & INST_STRING_MASK) == INST_LSWX)
63914cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
64014cf11afSPaul Mackerras 			return -EINVAL;
64114cf11afSPaul Mackerras 
64214cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
64314cf11afSPaul Mackerras 
64414cf11afSPaul Mackerras 	switch (instword & INST_STRING_MASK) {
64514cf11afSPaul Mackerras 		case INST_LSWX:
64614cf11afSPaul Mackerras 		case INST_STSWX:
64714cf11afSPaul Mackerras 			EA += NB_RB;
64814cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
64914cf11afSPaul Mackerras 			break;
65014cf11afSPaul Mackerras 		case INST_LSWI:
65114cf11afSPaul Mackerras 		case INST_STSWI:
65214cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
65314cf11afSPaul Mackerras 			break;
65414cf11afSPaul Mackerras 		default:
65514cf11afSPaul Mackerras 			return -EINVAL;
65614cf11afSPaul Mackerras 	}
65714cf11afSPaul Mackerras 
65814cf11afSPaul Mackerras 	while (num_bytes != 0)
65914cf11afSPaul Mackerras 	{
66014cf11afSPaul Mackerras 		u8 val;
66114cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
66214cf11afSPaul Mackerras 
66314cf11afSPaul Mackerras 		switch ((instword & INST_STRING_MASK)) {
66414cf11afSPaul Mackerras 			case INST_LSWX:
66514cf11afSPaul Mackerras 			case INST_LSWI:
66614cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
66714cf11afSPaul Mackerras 					return -EFAULT;
66814cf11afSPaul Mackerras 				/* first time updating this reg,
66914cf11afSPaul Mackerras 				 * zero it out */
67014cf11afSPaul Mackerras 				if (pos == 0)
67114cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
67214cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
67314cf11afSPaul Mackerras 				break;
67414cf11afSPaul Mackerras 			case INST_STSWI:
67514cf11afSPaul Mackerras 			case INST_STSWX:
67614cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
67714cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
67814cf11afSPaul Mackerras 					return -EFAULT;
67914cf11afSPaul Mackerras 				break;
68014cf11afSPaul Mackerras 		}
68114cf11afSPaul Mackerras 		/* move EA to next address */
68214cf11afSPaul Mackerras 		EA += 1;
68314cf11afSPaul Mackerras 		num_bytes--;
68414cf11afSPaul Mackerras 
68514cf11afSPaul Mackerras 		/* manage our position within the register */
68614cf11afSPaul Mackerras 		if (++pos == 4) {
68714cf11afSPaul Mackerras 			pos = 0;
68814cf11afSPaul Mackerras 			if (++rT == 32)
68914cf11afSPaul Mackerras 				rT = 0;
69014cf11afSPaul Mackerras 		}
69114cf11afSPaul Mackerras 	}
69214cf11afSPaul Mackerras 
69314cf11afSPaul Mackerras 	return 0;
69414cf11afSPaul Mackerras }
69514cf11afSPaul Mackerras 
696c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
697c3412dcbSWill Schmidt {
698c3412dcbSWill Schmidt 	u32 ra,rs;
699c3412dcbSWill Schmidt 	unsigned long tmp;
700c3412dcbSWill Schmidt 
701c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
702c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
703c3412dcbSWill Schmidt 
704c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
705c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
706c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
707c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
708c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
709c3412dcbSWill Schmidt 
710c3412dcbSWill Schmidt 	return 0;
711c3412dcbSWill Schmidt }
712c3412dcbSWill Schmidt 
713*c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
714*c1469f13SKumar Gala {
715*c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
716*c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
717*c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
718*c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
719*c1469f13SKumar Gala 	u8 bit;
720*c1469f13SKumar Gala 	unsigned long tmp;
721*c1469f13SKumar Gala 
722*c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
723*c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
724*c1469f13SKumar Gala 
725*c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
726*c1469f13SKumar Gala 
727*c1469f13SKumar Gala 	return 0;
728*c1469f13SKumar Gala }
729*c1469f13SKumar Gala 
73014cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
73114cf11afSPaul Mackerras {
73214cf11afSPaul Mackerras 	u32 instword;
73314cf11afSPaul Mackerras 	u32 rd;
73414cf11afSPaul Mackerras 
735fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
73614cf11afSPaul Mackerras 		return -EINVAL;
73714cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
73814cf11afSPaul Mackerras 
73914cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
74014cf11afSPaul Mackerras 		return -EFAULT;
74114cf11afSPaul Mackerras 
74214cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
74314cf11afSPaul Mackerras 	if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
74414cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
74514cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
74614cf11afSPaul Mackerras 		return 0;
74714cf11afSPaul Mackerras 	}
74814cf11afSPaul Mackerras 
74914cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
7508dad3f92SPaul Mackerras 	if ((instword & INST_DCBA_MASK) == INST_DCBA)
75114cf11afSPaul Mackerras 		return 0;
75214cf11afSPaul Mackerras 
75314cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
75414cf11afSPaul Mackerras 	if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
75586417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
75614cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
75714cf11afSPaul Mackerras 
75814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
75914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
76014cf11afSPaul Mackerras 		return 0;
76114cf11afSPaul Mackerras 	}
76214cf11afSPaul Mackerras 
76314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
76414cf11afSPaul Mackerras 	if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
76514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
76614cf11afSPaul Mackerras 
767c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
768c3412dcbSWill Schmidt 	if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
769c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
770c3412dcbSWill Schmidt 	}
771c3412dcbSWill Schmidt 
772*c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
773*c1469f13SKumar Gala 	if ((instword & INST_ISEL_MASK) == INST_ISEL) {
774*c1469f13SKumar Gala 		return emulate_isel(regs, instword);
775*c1469f13SKumar Gala 	}
776*c1469f13SKumar Gala 
77714cf11afSPaul Mackerras 	return -EINVAL;
77814cf11afSPaul Mackerras }
77914cf11afSPaul Mackerras 
78073c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
78114cf11afSPaul Mackerras {
78273c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
78314cf11afSPaul Mackerras }
78414cf11afSPaul Mackerras 
7858dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
78614cf11afSPaul Mackerras {
78714cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
78814cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
78914cf11afSPaul Mackerras 
790aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
79104903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
79214cf11afSPaul Mackerras 
79314cf11afSPaul Mackerras 	if (reason & REASON_FP) {
79414cf11afSPaul Mackerras 		/* IEEE FP exception */
795dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
7968dad3f92SPaul Mackerras 		return;
7978dad3f92SPaul Mackerras 	}
7988dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
79914cf11afSPaul Mackerras 		/* trap exception */
800dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
801dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
802dc1c1ca3SStephen Rothwell 			return;
80314cf11afSPaul Mackerras 		if (debugger_bpt(regs))
80414cf11afSPaul Mackerras 			return;
80573c9ceabSJeremy Fitzhardinge 
80673c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
807608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
80814cf11afSPaul Mackerras 			regs->nip += 4;
80914cf11afSPaul Mackerras 			return;
81014cf11afSPaul Mackerras 		}
8118dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
8128dad3f92SPaul Mackerras 		return;
8138dad3f92SPaul Mackerras 	}
8148dad3f92SPaul Mackerras 
815cd8a5673SPaul Mackerras 	local_irq_enable();
816cd8a5673SPaul Mackerras 
81704903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
81804903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
81904903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
82004903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
82104903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
82204903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
82304903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
82404903a30SKumar Gala 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
8255fad293bSKumar Gala 	switch (do_mathemu(regs)) {
8265fad293bSKumar Gala 	case 0:
82704903a30SKumar Gala 		emulate_single_step(regs);
82804903a30SKumar Gala 		return;
8295fad293bSKumar Gala 	case 1: {
8305fad293bSKumar Gala 			int code = 0;
8315fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
8325fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
8335fad293bSKumar Gala 			return;
83404903a30SKumar Gala 		}
8355fad293bSKumar Gala 	case -EFAULT:
8365fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8375fad293bSKumar Gala 		return;
8385fad293bSKumar Gala 	}
8395fad293bSKumar Gala 	/* fall through on any other errors */
84004903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
84104903a30SKumar Gala 
8428dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
8438dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
84414cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
84514cf11afSPaul Mackerras 		case 0:
84614cf11afSPaul Mackerras 			regs->nip += 4;
84714cf11afSPaul Mackerras 			emulate_single_step(regs);
8488dad3f92SPaul Mackerras 			return;
84914cf11afSPaul Mackerras 		case -EFAULT:
85014cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8518dad3f92SPaul Mackerras 			return;
8528dad3f92SPaul Mackerras 		}
8538dad3f92SPaul Mackerras 	}
8548dad3f92SPaul Mackerras 
85514cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
85614cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
85714cf11afSPaul Mackerras 	else
85814cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
85914cf11afSPaul Mackerras }
86014cf11afSPaul Mackerras 
861dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
86214cf11afSPaul Mackerras {
8634393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
86414cf11afSPaul Mackerras 
865e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
866e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
86714cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
86814cf11afSPaul Mackerras 
86914cf11afSPaul Mackerras 	if (fixed == 1) {
87014cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
87114cf11afSPaul Mackerras 		emulate_single_step(regs);
87214cf11afSPaul Mackerras 		return;
87314cf11afSPaul Mackerras 	}
87414cf11afSPaul Mackerras 
87514cf11afSPaul Mackerras 	/* Operand address was bad */
87614cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
8774393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
8784393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
8794393c4f6SBenjamin Herrenschmidt 	} else {
8804393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
8814393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
88214cf11afSPaul Mackerras 	}
8834393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
8844393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
8854393c4f6SBenjamin Herrenschmidt 	else
8864393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
88714cf11afSPaul Mackerras }
88814cf11afSPaul Mackerras 
88914cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
89014cf11afSPaul Mackerras {
89114cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
89214cf11afSPaul Mackerras 	       current, regs->gpr[1]);
89314cf11afSPaul Mackerras 	debugger(regs);
89414cf11afSPaul Mackerras 	show_regs(regs);
89514cf11afSPaul Mackerras 	panic("kernel stack overflow");
89614cf11afSPaul Mackerras }
89714cf11afSPaul Mackerras 
89814cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
89914cf11afSPaul Mackerras {
90014cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
90114cf11afSPaul Mackerras 	       regs->nip, regs->msr);
90214cf11afSPaul Mackerras 	debugger(regs);
90314cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
90414cf11afSPaul Mackerras }
90514cf11afSPaul Mackerras 
90614cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
90714cf11afSPaul Mackerras {
90814cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
90919c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
91014cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
91114cf11afSPaul Mackerras }
91214cf11afSPaul Mackerras 
913dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
914dc1c1ca3SStephen Rothwell {
915dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
916dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
917dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
918dc1c1ca3SStephen Rothwell }
919dc1c1ca3SStephen Rothwell 
920dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
921dc1c1ca3SStephen Rothwell {
922dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
923dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
924dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
925dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
926dc1c1ca3SStephen Rothwell 		return;
927dc1c1ca3SStephen Rothwell 	}
9286c4841c2SAnton Blanchard 
929dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
930dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
931dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
932dc1c1ca3SStephen Rothwell }
933dc1c1ca3SStephen Rothwell 
934dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
935dc1c1ca3SStephen Rothwell {
936dc1c1ca3SStephen Rothwell 	perf_irq(regs);
937dc1c1ca3SStephen Rothwell }
938dc1c1ca3SStephen Rothwell 
9398dad3f92SPaul Mackerras #ifdef CONFIG_8xx
94014cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
94114cf11afSPaul Mackerras {
94214cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
94314cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
9445dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
94514cf11afSPaul Mackerras 	int errcode;
9465dd57a13SScott Wood #endif
94714cf11afSPaul Mackerras 
94814cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
94914cf11afSPaul Mackerras 
95014cf11afSPaul Mackerras 	if (!user_mode(regs)) {
95114cf11afSPaul Mackerras 		debugger(regs);
95214cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
95314cf11afSPaul Mackerras 	}
95414cf11afSPaul Mackerras 
95514cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
95614cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
9575fad293bSKumar Gala 
9585fad293bSKumar Gala 	switch (errcode) {
9595fad293bSKumar Gala 	case 0:
9605fad293bSKumar Gala 		emulate_single_step(regs);
9615fad293bSKumar Gala 		return;
9625fad293bSKumar Gala 	case 1: {
9635fad293bSKumar Gala 			int code = 0;
9645fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
9655fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
9665fad293bSKumar Gala 			return;
9675fad293bSKumar Gala 		}
9685fad293bSKumar Gala 	case -EFAULT:
9695fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9705fad293bSKumar Gala 		return;
9715fad293bSKumar Gala 	default:
9725fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9735fad293bSKumar Gala 		return;
9745fad293bSKumar Gala 	}
9755fad293bSKumar Gala 
9765dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
97714cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
9785fad293bSKumar Gala 	switch (errcode) {
9795fad293bSKumar Gala 	case 0:
98014cf11afSPaul Mackerras 		emulate_single_step(regs);
9815fad293bSKumar Gala 		return;
9825fad293bSKumar Gala 	case 1:
9835fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9845fad293bSKumar Gala 		return;
9855fad293bSKumar Gala 	case -EFAULT:
9865fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9875fad293bSKumar Gala 		return;
9885fad293bSKumar Gala 	}
9895dd57a13SScott Wood #else
9905dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9915fad293bSKumar Gala #endif
99214cf11afSPaul Mackerras }
9938dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
99414cf11afSPaul Mackerras 
99514cf11afSPaul Mackerras #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
99614cf11afSPaul Mackerras 
99714cf11afSPaul Mackerras void DebugException(struct pt_regs *regs, unsigned long debug_status)
99814cf11afSPaul Mackerras {
99914cf11afSPaul Mackerras 	if (debug_status & DBSR_IC) {	/* instruction completion */
100014cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
100114cf11afSPaul Mackerras 		if (user_mode(regs)) {
100214cf11afSPaul Mackerras 			current->thread.dbcr0 &= ~DBCR0_IC;
100314cf11afSPaul Mackerras 		} else {
100414cf11afSPaul Mackerras 			/* Disable instruction completion */
100514cf11afSPaul Mackerras 			mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
100614cf11afSPaul Mackerras 			/* Clear the instruction completion event */
100714cf11afSPaul Mackerras 			mtspr(SPRN_DBSR, DBSR_IC);
100814cf11afSPaul Mackerras 			if (debugger_sstep(regs))
100914cf11afSPaul Mackerras 				return;
101014cf11afSPaul Mackerras 		}
101114cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
101214cf11afSPaul Mackerras 	}
101314cf11afSPaul Mackerras }
101414cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
101514cf11afSPaul Mackerras 
101614cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
101714cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
101814cf11afSPaul Mackerras {
101914cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
102014cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
102114cf11afSPaul Mackerras }
102214cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
102314cf11afSPaul Mackerras 
102414cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1025dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
102614cf11afSPaul Mackerras {
102714cf11afSPaul Mackerras 	int err;
102814cf11afSPaul Mackerras 
102914cf11afSPaul Mackerras 	if (!user_mode(regs)) {
103014cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
103114cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
10328dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
103314cf11afSPaul Mackerras 	}
103414cf11afSPaul Mackerras 
1035dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1036dc1c1ca3SStephen Rothwell 
103714cf11afSPaul Mackerras 	err = emulate_altivec(regs);
103814cf11afSPaul Mackerras 	if (err == 0) {
103914cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
104014cf11afSPaul Mackerras 		emulate_single_step(regs);
104114cf11afSPaul Mackerras 		return;
104214cf11afSPaul Mackerras 	}
104314cf11afSPaul Mackerras 
104414cf11afSPaul Mackerras 	if (err == -EFAULT) {
104514cf11afSPaul Mackerras 		/* got an error reading the instruction */
104614cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
104714cf11afSPaul Mackerras 	} else {
104814cf11afSPaul Mackerras 		/* didn't recognize the instruction */
104914cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
105014cf11afSPaul Mackerras 		if (printk_ratelimit())
105114cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
105214cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
105314cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
105414cf11afSPaul Mackerras 	}
105514cf11afSPaul Mackerras }
105614cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
105714cf11afSPaul Mackerras 
105814cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
105914cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
106014cf11afSPaul Mackerras 			   unsigned long error_code)
106114cf11afSPaul Mackerras {
106214cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
106314cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
106414cf11afSPaul Mackerras 	 * something smarter
106514cf11afSPaul Mackerras 	 */
106614cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
106714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
106814cf11afSPaul Mackerras 	return;
106914cf11afSPaul Mackerras }
107014cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
107114cf11afSPaul Mackerras 
107214cf11afSPaul Mackerras #ifdef CONFIG_SPE
107314cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
107414cf11afSPaul Mackerras {
107514cf11afSPaul Mackerras 	unsigned long spefscr;
107614cf11afSPaul Mackerras 	int fpexc_mode;
107714cf11afSPaul Mackerras 	int code = 0;
107814cf11afSPaul Mackerras 
107914cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
108014cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
108114cf11afSPaul Mackerras 
108214cf11afSPaul Mackerras 	/* Hardware does not neccessarily set sticky
108314cf11afSPaul Mackerras 	 * underflow/overflow/invalid flags */
108414cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
108514cf11afSPaul Mackerras 		code = FPE_FLTOVF;
108614cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FOVFS;
108714cf11afSPaul Mackerras 	}
108814cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
108914cf11afSPaul Mackerras 		code = FPE_FLTUND;
109014cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FUNFS;
109114cf11afSPaul Mackerras 	}
109214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
109314cf11afSPaul Mackerras 		code = FPE_FLTDIV;
109414cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
109514cf11afSPaul Mackerras 		code = FPE_FLTINV;
109614cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FINVS;
109714cf11afSPaul Mackerras 	}
109814cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
109914cf11afSPaul Mackerras 		code = FPE_FLTRES;
110014cf11afSPaul Mackerras 
110114cf11afSPaul Mackerras 	current->thread.spefscr = spefscr;
110214cf11afSPaul Mackerras 
110314cf11afSPaul Mackerras 	_exception(SIGFPE, regs, code, regs->nip);
110414cf11afSPaul Mackerras 	return;
110514cf11afSPaul Mackerras }
110614cf11afSPaul Mackerras #endif
110714cf11afSPaul Mackerras 
1108dc1c1ca3SStephen Rothwell /*
1109dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1110dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1111dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1112dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1113dc1c1ca3SStephen Rothwell  */
1114dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1115dc1c1ca3SStephen Rothwell {
1116dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1117dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1118dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1119dc1c1ca3SStephen Rothwell }
1120dc1c1ca3SStephen Rothwell 
112114cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
112214cf11afSPaul Mackerras /*
112314cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
112414cf11afSPaul Mackerras  * spins until a reboot occurs
112514cf11afSPaul Mackerras  */
112614cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
112714cf11afSPaul Mackerras {
112814cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
112914cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
113014cf11afSPaul Mackerras 	return;
113114cf11afSPaul Mackerras }
113214cf11afSPaul Mackerras 
113314cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
113414cf11afSPaul Mackerras {
113514cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
113614cf11afSPaul Mackerras 	WatchdogHandler(regs);
113714cf11afSPaul Mackerras }
113814cf11afSPaul Mackerras #endif
1139dc1c1ca3SStephen Rothwell 
1140dc1c1ca3SStephen Rothwell /*
1141dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1142dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1143dc1c1ca3SStephen Rothwell  */
1144dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1145dc1c1ca3SStephen Rothwell {
1146dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1147dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1148dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1149dc1c1ca3SStephen Rothwell }
115014cf11afSPaul Mackerras 
115114cf11afSPaul Mackerras void __init trap_init(void)
115214cf11afSPaul Mackerras {
115314cf11afSPaul Mackerras }
1154