xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 19c5870c0eefd27c6d09d867465e0571262e05d0)
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 
62514cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
62614cf11afSPaul Mackerras {
62714cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
62814cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
62914cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
63014cf11afSPaul Mackerras 	u32 num_bytes;
63114cf11afSPaul Mackerras 	unsigned long EA;
63214cf11afSPaul Mackerras 	int pos = 0;
63314cf11afSPaul Mackerras 
63414cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
63514cf11afSPaul Mackerras 	if ((instword & INST_STRING_MASK) == INST_LSWX)
63614cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
63714cf11afSPaul Mackerras 			return -EINVAL;
63814cf11afSPaul Mackerras 
63914cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
64014cf11afSPaul Mackerras 
64114cf11afSPaul Mackerras 	switch (instword & INST_STRING_MASK) {
64214cf11afSPaul Mackerras 		case INST_LSWX:
64314cf11afSPaul Mackerras 		case INST_STSWX:
64414cf11afSPaul Mackerras 			EA += NB_RB;
64514cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
64614cf11afSPaul Mackerras 			break;
64714cf11afSPaul Mackerras 		case INST_LSWI:
64814cf11afSPaul Mackerras 		case INST_STSWI:
64914cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
65014cf11afSPaul Mackerras 			break;
65114cf11afSPaul Mackerras 		default:
65214cf11afSPaul Mackerras 			return -EINVAL;
65314cf11afSPaul Mackerras 	}
65414cf11afSPaul Mackerras 
65514cf11afSPaul Mackerras 	while (num_bytes != 0)
65614cf11afSPaul Mackerras 	{
65714cf11afSPaul Mackerras 		u8 val;
65814cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
65914cf11afSPaul Mackerras 
66014cf11afSPaul Mackerras 		switch ((instword & INST_STRING_MASK)) {
66114cf11afSPaul Mackerras 			case INST_LSWX:
66214cf11afSPaul Mackerras 			case INST_LSWI:
66314cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
66414cf11afSPaul Mackerras 					return -EFAULT;
66514cf11afSPaul Mackerras 				/* first time updating this reg,
66614cf11afSPaul Mackerras 				 * zero it out */
66714cf11afSPaul Mackerras 				if (pos == 0)
66814cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
66914cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
67014cf11afSPaul Mackerras 				break;
67114cf11afSPaul Mackerras 			case INST_STSWI:
67214cf11afSPaul Mackerras 			case INST_STSWX:
67314cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
67414cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
67514cf11afSPaul Mackerras 					return -EFAULT;
67614cf11afSPaul Mackerras 				break;
67714cf11afSPaul Mackerras 		}
67814cf11afSPaul Mackerras 		/* move EA to next address */
67914cf11afSPaul Mackerras 		EA += 1;
68014cf11afSPaul Mackerras 		num_bytes--;
68114cf11afSPaul Mackerras 
68214cf11afSPaul Mackerras 		/* manage our position within the register */
68314cf11afSPaul Mackerras 		if (++pos == 4) {
68414cf11afSPaul Mackerras 			pos = 0;
68514cf11afSPaul Mackerras 			if (++rT == 32)
68614cf11afSPaul Mackerras 				rT = 0;
68714cf11afSPaul Mackerras 		}
68814cf11afSPaul Mackerras 	}
68914cf11afSPaul Mackerras 
69014cf11afSPaul Mackerras 	return 0;
69114cf11afSPaul Mackerras }
69214cf11afSPaul Mackerras 
693c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
694c3412dcbSWill Schmidt {
695c3412dcbSWill Schmidt 	u32 ra,rs;
696c3412dcbSWill Schmidt 	unsigned long tmp;
697c3412dcbSWill Schmidt 
698c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
699c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
700c3412dcbSWill Schmidt 
701c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
702c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
703c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
704c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
705c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
706c3412dcbSWill Schmidt 
707c3412dcbSWill Schmidt 	return 0;
708c3412dcbSWill Schmidt }
709c3412dcbSWill Schmidt 
71014cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
71114cf11afSPaul Mackerras {
71214cf11afSPaul Mackerras 	u32 instword;
71314cf11afSPaul Mackerras 	u32 rd;
71414cf11afSPaul Mackerras 
715fab5db97SPaul Mackerras 	if (!user_mode(regs) || (regs->msr & MSR_LE))
71614cf11afSPaul Mackerras 		return -EINVAL;
71714cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
71814cf11afSPaul Mackerras 
71914cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
72014cf11afSPaul Mackerras 		return -EFAULT;
72114cf11afSPaul Mackerras 
72214cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
72314cf11afSPaul Mackerras 	if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
72414cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
72514cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
72614cf11afSPaul Mackerras 		return 0;
72714cf11afSPaul Mackerras 	}
72814cf11afSPaul Mackerras 
72914cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
7308dad3f92SPaul Mackerras 	if ((instword & INST_DCBA_MASK) == INST_DCBA)
73114cf11afSPaul Mackerras 		return 0;
73214cf11afSPaul Mackerras 
73314cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
73414cf11afSPaul Mackerras 	if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
73586417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
73614cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
73714cf11afSPaul Mackerras 
73814cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
73914cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
74014cf11afSPaul Mackerras 		return 0;
74114cf11afSPaul Mackerras 	}
74214cf11afSPaul Mackerras 
74314cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
74414cf11afSPaul Mackerras 	if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
74514cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
74614cf11afSPaul Mackerras 
747c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
748c3412dcbSWill Schmidt 	if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
749c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
750c3412dcbSWill Schmidt 	}
751c3412dcbSWill Schmidt 
75214cf11afSPaul Mackerras 	return -EINVAL;
75314cf11afSPaul Mackerras }
75414cf11afSPaul Mackerras 
75573c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
75614cf11afSPaul Mackerras {
75773c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
75814cf11afSPaul Mackerras }
75914cf11afSPaul Mackerras 
7608dad3f92SPaul Mackerras void __kprobes program_check_exception(struct pt_regs *regs)
76114cf11afSPaul Mackerras {
76214cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
76314cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *regs);
76414cf11afSPaul Mackerras 
765aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
76604903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
76714cf11afSPaul Mackerras 
76814cf11afSPaul Mackerras 	if (reason & REASON_FP) {
76914cf11afSPaul Mackerras 		/* IEEE FP exception */
770dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
7718dad3f92SPaul Mackerras 		return;
7728dad3f92SPaul Mackerras 	}
7738dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
77414cf11afSPaul Mackerras 		/* trap exception */
775dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
776dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
777dc1c1ca3SStephen Rothwell 			return;
77814cf11afSPaul Mackerras 		if (debugger_bpt(regs))
77914cf11afSPaul Mackerras 			return;
78073c9ceabSJeremy Fitzhardinge 
78173c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
782608e2619SHeiko Carstens 		    report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
78314cf11afSPaul Mackerras 			regs->nip += 4;
78414cf11afSPaul Mackerras 			return;
78514cf11afSPaul Mackerras 		}
7868dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
7878dad3f92SPaul Mackerras 		return;
7888dad3f92SPaul Mackerras 	}
7898dad3f92SPaul Mackerras 
790cd8a5673SPaul Mackerras 	local_irq_enable();
791cd8a5673SPaul Mackerras 
79204903a30SKumar Gala #ifdef CONFIG_MATH_EMULATION
79304903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
79404903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
79504903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
79604903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
79704903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
79804903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
79904903a30SKumar Gala 	 * pattern to occurences etc. -dgibson 31/Mar/2003 */
8005fad293bSKumar Gala 	switch (do_mathemu(regs)) {
8015fad293bSKumar Gala 	case 0:
80204903a30SKumar Gala 		emulate_single_step(regs);
80304903a30SKumar Gala 		return;
8045fad293bSKumar Gala 	case 1: {
8055fad293bSKumar Gala 			int code = 0;
8065fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
8075fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
8085fad293bSKumar Gala 			return;
80904903a30SKumar Gala 		}
8105fad293bSKumar Gala 	case -EFAULT:
8115fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8125fad293bSKumar Gala 		return;
8135fad293bSKumar Gala 	}
8145fad293bSKumar Gala 	/* fall through on any other errors */
81504903a30SKumar Gala #endif /* CONFIG_MATH_EMULATION */
81604903a30SKumar Gala 
8178dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
8188dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
81914cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
82014cf11afSPaul Mackerras 		case 0:
82114cf11afSPaul Mackerras 			regs->nip += 4;
82214cf11afSPaul Mackerras 			emulate_single_step(regs);
8238dad3f92SPaul Mackerras 			return;
82414cf11afSPaul Mackerras 		case -EFAULT:
82514cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8268dad3f92SPaul Mackerras 			return;
8278dad3f92SPaul Mackerras 		}
8288dad3f92SPaul Mackerras 	}
8298dad3f92SPaul Mackerras 
83014cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
83114cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
83214cf11afSPaul Mackerras 	else
83314cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
83414cf11afSPaul Mackerras }
83514cf11afSPaul Mackerras 
836dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
83714cf11afSPaul Mackerras {
8384393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
83914cf11afSPaul Mackerras 
840e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
841e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
84214cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
84314cf11afSPaul Mackerras 
84414cf11afSPaul Mackerras 	if (fixed == 1) {
84514cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
84614cf11afSPaul Mackerras 		emulate_single_step(regs);
84714cf11afSPaul Mackerras 		return;
84814cf11afSPaul Mackerras 	}
84914cf11afSPaul Mackerras 
85014cf11afSPaul Mackerras 	/* Operand address was bad */
85114cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
8524393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
8534393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
8544393c4f6SBenjamin Herrenschmidt 	} else {
8554393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
8564393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
85714cf11afSPaul Mackerras 	}
8584393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
8594393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
8604393c4f6SBenjamin Herrenschmidt 	else
8614393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
86214cf11afSPaul Mackerras }
86314cf11afSPaul Mackerras 
86414cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
86514cf11afSPaul Mackerras {
86614cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
86714cf11afSPaul Mackerras 	       current, regs->gpr[1]);
86814cf11afSPaul Mackerras 	debugger(regs);
86914cf11afSPaul Mackerras 	show_regs(regs);
87014cf11afSPaul Mackerras 	panic("kernel stack overflow");
87114cf11afSPaul Mackerras }
87214cf11afSPaul Mackerras 
87314cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
87414cf11afSPaul Mackerras {
87514cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
87614cf11afSPaul Mackerras 	       regs->nip, regs->msr);
87714cf11afSPaul Mackerras 	debugger(regs);
87814cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
87914cf11afSPaul Mackerras }
88014cf11afSPaul Mackerras 
88114cf11afSPaul Mackerras void trace_syscall(struct pt_regs *regs)
88214cf11afSPaul Mackerras {
88314cf11afSPaul Mackerras 	printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld    %s\n",
884*19c5870cSAlexey Dobriyan 	       current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
88514cf11afSPaul Mackerras 	       regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
88614cf11afSPaul Mackerras }
88714cf11afSPaul Mackerras 
888dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
889dc1c1ca3SStephen Rothwell {
890dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
891dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
892dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
893dc1c1ca3SStephen Rothwell }
894dc1c1ca3SStephen Rothwell 
895dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
896dc1c1ca3SStephen Rothwell {
897dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
898dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
899dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
900dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
901dc1c1ca3SStephen Rothwell 		return;
902dc1c1ca3SStephen Rothwell 	}
9036c4841c2SAnton Blanchard 
904dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
905dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
906dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
907dc1c1ca3SStephen Rothwell }
908dc1c1ca3SStephen Rothwell 
909dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
910dc1c1ca3SStephen Rothwell {
911dc1c1ca3SStephen Rothwell 	perf_irq(regs);
912dc1c1ca3SStephen Rothwell }
913dc1c1ca3SStephen Rothwell 
9148dad3f92SPaul Mackerras #ifdef CONFIG_8xx
91514cf11afSPaul Mackerras void SoftwareEmulation(struct pt_regs *regs)
91614cf11afSPaul Mackerras {
91714cf11afSPaul Mackerras 	extern int do_mathemu(struct pt_regs *);
91814cf11afSPaul Mackerras 	extern int Soft_emulate_8xx(struct pt_regs *);
9195dd57a13SScott Wood #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
92014cf11afSPaul Mackerras 	int errcode;
9215dd57a13SScott Wood #endif
92214cf11afSPaul Mackerras 
92314cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
92414cf11afSPaul Mackerras 
92514cf11afSPaul Mackerras 	if (!user_mode(regs)) {
92614cf11afSPaul Mackerras 		debugger(regs);
92714cf11afSPaul Mackerras 		die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
92814cf11afSPaul Mackerras 	}
92914cf11afSPaul Mackerras 
93014cf11afSPaul Mackerras #ifdef CONFIG_MATH_EMULATION
93114cf11afSPaul Mackerras 	errcode = do_mathemu(regs);
9325fad293bSKumar Gala 
9335fad293bSKumar Gala 	switch (errcode) {
9345fad293bSKumar Gala 	case 0:
9355fad293bSKumar Gala 		emulate_single_step(regs);
9365fad293bSKumar Gala 		return;
9375fad293bSKumar Gala 	case 1: {
9385fad293bSKumar Gala 			int code = 0;
9395fad293bSKumar Gala 			code = __parse_fpscr(current->thread.fpscr.val);
9405fad293bSKumar Gala 			_exception(SIGFPE, regs, code, regs->nip);
9415fad293bSKumar Gala 			return;
9425fad293bSKumar Gala 		}
9435fad293bSKumar Gala 	case -EFAULT:
9445fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9455fad293bSKumar Gala 		return;
9465fad293bSKumar Gala 	default:
9475fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9485fad293bSKumar Gala 		return;
9495fad293bSKumar Gala 	}
9505fad293bSKumar Gala 
9515dd57a13SScott Wood #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
95214cf11afSPaul Mackerras 	errcode = Soft_emulate_8xx(regs);
9535fad293bSKumar Gala 	switch (errcode) {
9545fad293bSKumar Gala 	case 0:
95514cf11afSPaul Mackerras 		emulate_single_step(regs);
9565fad293bSKumar Gala 		return;
9575fad293bSKumar Gala 	case 1:
9585fad293bSKumar Gala 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9595fad293bSKumar Gala 		return;
9605fad293bSKumar Gala 	case -EFAULT:
9615fad293bSKumar Gala 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
9625fad293bSKumar Gala 		return;
9635fad293bSKumar Gala 	}
9645dd57a13SScott Wood #else
9655dd57a13SScott Wood 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
9665fad293bSKumar Gala #endif
96714cf11afSPaul Mackerras }
9688dad3f92SPaul Mackerras #endif /* CONFIG_8xx */
96914cf11afSPaul Mackerras 
97014cf11afSPaul Mackerras #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
97114cf11afSPaul Mackerras 
97214cf11afSPaul Mackerras void DebugException(struct pt_regs *regs, unsigned long debug_status)
97314cf11afSPaul Mackerras {
97414cf11afSPaul Mackerras 	if (debug_status & DBSR_IC) {	/* instruction completion */
97514cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
97614cf11afSPaul Mackerras 		if (user_mode(regs)) {
97714cf11afSPaul Mackerras 			current->thread.dbcr0 &= ~DBCR0_IC;
97814cf11afSPaul Mackerras 		} else {
97914cf11afSPaul Mackerras 			/* Disable instruction completion */
98014cf11afSPaul Mackerras 			mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
98114cf11afSPaul Mackerras 			/* Clear the instruction completion event */
98214cf11afSPaul Mackerras 			mtspr(SPRN_DBSR, DBSR_IC);
98314cf11afSPaul Mackerras 			if (debugger_sstep(regs))
98414cf11afSPaul Mackerras 				return;
98514cf11afSPaul Mackerras 		}
98614cf11afSPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_TRACE, 0);
98714cf11afSPaul Mackerras 	}
98814cf11afSPaul Mackerras }
98914cf11afSPaul Mackerras #endif /* CONFIG_4xx || CONFIG_BOOKE */
99014cf11afSPaul Mackerras 
99114cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
99214cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
99314cf11afSPaul Mackerras {
99414cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
99514cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
99614cf11afSPaul Mackerras }
99714cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
99814cf11afSPaul Mackerras 
99914cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1000dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
100114cf11afSPaul Mackerras {
100214cf11afSPaul Mackerras 	int err;
100314cf11afSPaul Mackerras 
100414cf11afSPaul Mackerras 	if (!user_mode(regs)) {
100514cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
100614cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
10078dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
100814cf11afSPaul Mackerras 	}
100914cf11afSPaul Mackerras 
1010dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1011dc1c1ca3SStephen Rothwell 
101214cf11afSPaul Mackerras 	err = emulate_altivec(regs);
101314cf11afSPaul Mackerras 	if (err == 0) {
101414cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
101514cf11afSPaul Mackerras 		emulate_single_step(regs);
101614cf11afSPaul Mackerras 		return;
101714cf11afSPaul Mackerras 	}
101814cf11afSPaul Mackerras 
101914cf11afSPaul Mackerras 	if (err == -EFAULT) {
102014cf11afSPaul Mackerras 		/* got an error reading the instruction */
102114cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
102214cf11afSPaul Mackerras 	} else {
102314cf11afSPaul Mackerras 		/* didn't recognize the instruction */
102414cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
102514cf11afSPaul Mackerras 		if (printk_ratelimit())
102614cf11afSPaul Mackerras 			printk(KERN_ERR "Unrecognized altivec instruction "
102714cf11afSPaul Mackerras 			       "in %s at %lx\n", current->comm, regs->nip);
102814cf11afSPaul Mackerras 		current->thread.vscr.u[3] |= 0x10000;
102914cf11afSPaul Mackerras 	}
103014cf11afSPaul Mackerras }
103114cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
103214cf11afSPaul Mackerras 
103314cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
103414cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
103514cf11afSPaul Mackerras 			   unsigned long error_code)
103614cf11afSPaul Mackerras {
103714cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
103814cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
103914cf11afSPaul Mackerras 	 * something smarter
104014cf11afSPaul Mackerras 	 */
104114cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
104214cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
104314cf11afSPaul Mackerras 	return;
104414cf11afSPaul Mackerras }
104514cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
104614cf11afSPaul Mackerras 
104714cf11afSPaul Mackerras #ifdef CONFIG_SPE
104814cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
104914cf11afSPaul Mackerras {
105014cf11afSPaul Mackerras 	unsigned long spefscr;
105114cf11afSPaul Mackerras 	int fpexc_mode;
105214cf11afSPaul Mackerras 	int code = 0;
105314cf11afSPaul Mackerras 
105414cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
105514cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
105614cf11afSPaul Mackerras 
105714cf11afSPaul Mackerras 	/* Hardware does not neccessarily set sticky
105814cf11afSPaul Mackerras 	 * underflow/overflow/invalid flags */
105914cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
106014cf11afSPaul Mackerras 		code = FPE_FLTOVF;
106114cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FOVFS;
106214cf11afSPaul Mackerras 	}
106314cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
106414cf11afSPaul Mackerras 		code = FPE_FLTUND;
106514cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FUNFS;
106614cf11afSPaul Mackerras 	}
106714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
106814cf11afSPaul Mackerras 		code = FPE_FLTDIV;
106914cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
107014cf11afSPaul Mackerras 		code = FPE_FLTINV;
107114cf11afSPaul Mackerras 		spefscr |= SPEFSCR_FINVS;
107214cf11afSPaul Mackerras 	}
107314cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
107414cf11afSPaul Mackerras 		code = FPE_FLTRES;
107514cf11afSPaul Mackerras 
107614cf11afSPaul Mackerras 	current->thread.spefscr = spefscr;
107714cf11afSPaul Mackerras 
107814cf11afSPaul Mackerras 	_exception(SIGFPE, regs, code, regs->nip);
107914cf11afSPaul Mackerras 	return;
108014cf11afSPaul Mackerras }
108114cf11afSPaul Mackerras #endif
108214cf11afSPaul Mackerras 
1083dc1c1ca3SStephen Rothwell /*
1084dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
1085dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
1086dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
1087dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
1088dc1c1ca3SStephen Rothwell  */
1089dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
1090dc1c1ca3SStephen Rothwell {
1091dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1092dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
1093dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
1094dc1c1ca3SStephen Rothwell }
1095dc1c1ca3SStephen Rothwell 
109614cf11afSPaul Mackerras #ifdef CONFIG_BOOKE_WDT
109714cf11afSPaul Mackerras /*
109814cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
109914cf11afSPaul Mackerras  * spins until a reboot occurs
110014cf11afSPaul Mackerras  */
110114cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
110214cf11afSPaul Mackerras {
110314cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
110414cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
110514cf11afSPaul Mackerras 	return;
110614cf11afSPaul Mackerras }
110714cf11afSPaul Mackerras 
110814cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
110914cf11afSPaul Mackerras {
111014cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
111114cf11afSPaul Mackerras 	WatchdogHandler(regs);
111214cf11afSPaul Mackerras }
111314cf11afSPaul Mackerras #endif
1114dc1c1ca3SStephen Rothwell 
1115dc1c1ca3SStephen Rothwell /*
1116dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
1117dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
1118dc1c1ca3SStephen Rothwell  */
1119dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
1120dc1c1ca3SStephen Rothwell {
1121dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1122dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
1123dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
1124dc1c1ca3SStephen Rothwell }
112514cf11afSPaul Mackerras 
112614cf11afSPaul Mackerras void __init trap_init(void)
112714cf11afSPaul Mackerras {
112814cf11afSPaul Mackerras }
1129