xref: /openbmc/linux/arch/powerpc/kernel/traps.c (revision 2c44ce285fdd21713769bace4c27336d134be8a5)
114cf11afSPaul Mackerras /*
214cf11afSPaul Mackerras  *  Copyright (C) 1995-1996  Gary Thomas (gdt@linuxppc.org)
3fe04b112SScott Wood  *  Copyright 2007-2010 Freescale Semiconductor, Inc.
414cf11afSPaul Mackerras  *
514cf11afSPaul Mackerras  *  This program is free software; you can redistribute it and/or
614cf11afSPaul Mackerras  *  modify it under the terms of the GNU General Public License
714cf11afSPaul Mackerras  *  as published by the Free Software Foundation; either version
814cf11afSPaul Mackerras  *  2 of the License, or (at your option) any later version.
914cf11afSPaul Mackerras  *
1014cf11afSPaul Mackerras  *  Modified by Cort Dougan (cort@cs.nmt.edu)
1114cf11afSPaul Mackerras  *  and Paul Mackerras (paulus@samba.org)
1214cf11afSPaul Mackerras  */
1314cf11afSPaul Mackerras 
1414cf11afSPaul Mackerras /*
1514cf11afSPaul Mackerras  * This file handles the architecture-dependent parts of hardware exceptions
1614cf11afSPaul Mackerras  */
1714cf11afSPaul Mackerras 
1814cf11afSPaul Mackerras #include <linux/errno.h>
1914cf11afSPaul Mackerras #include <linux/sched.h>
20b17b0153SIngo Molnar #include <linux/sched/debug.h>
2114cf11afSPaul Mackerras #include <linux/kernel.h>
2214cf11afSPaul Mackerras #include <linux/mm.h>
2399cd1302SRam Pai #include <linux/pkeys.h>
2414cf11afSPaul Mackerras #include <linux/stddef.h>
2514cf11afSPaul Mackerras #include <linux/unistd.h>
268dad3f92SPaul Mackerras #include <linux/ptrace.h>
2714cf11afSPaul Mackerras #include <linux/user.h>
2814cf11afSPaul Mackerras #include <linux/interrupt.h>
2914cf11afSPaul Mackerras #include <linux/init.h>
308a39b05fSPaul Gortmaker #include <linux/extable.h>
318a39b05fSPaul Gortmaker #include <linux/module.h>	/* print_modules */
328dad3f92SPaul Mackerras #include <linux/prctl.h>
3314cf11afSPaul Mackerras #include <linux/delay.h>
3414cf11afSPaul Mackerras #include <linux/kprobes.h>
35cc532915SMichael Ellerman #include <linux/kexec.h>
365474c120SMichael Hanselmann #include <linux/backlight.h>
3773c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
381eeb66a1SChristoph Hellwig #include <linux/kdebug.h>
3976462232SChristian Dietrich #include <linux/ratelimit.h>
40ba12eedeSLi Zhong #include <linux/context_tracking.h>
415080332cSMichael Neuling #include <linux/smp.h>
4235adacd6SNicholas Piggin #include <linux/console.h>
4335adacd6SNicholas Piggin #include <linux/kmsg_dump.h>
4414cf11afSPaul Mackerras 
4580947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h>
4614cf11afSPaul Mackerras #include <asm/pgtable.h>
477c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
487644d581SMichael Ellerman #include <asm/debugfs.h>
4914cf11afSPaul Mackerras #include <asm/io.h>
5086417780SPaul Mackerras #include <asm/machdep.h>
5186417780SPaul Mackerras #include <asm/rtas.h>
52f7f6f4feSDavid Gibson #include <asm/pmc.h>
5314cf11afSPaul Mackerras #include <asm/reg.h>
5414cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT
5514cf11afSPaul Mackerras #include <asm/backlight.h>
5614cf11afSPaul Mackerras #endif
57dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64
5886417780SPaul Mackerras #include <asm/firmware.h>
59dc1c1ca3SStephen Rothwell #include <asm/processor.h>
606ce6c629SMichael Neuling #include <asm/tm.h>
61dc1c1ca3SStephen Rothwell #endif
62c0ce7d08SDavid Wilder #include <asm/kexec.h>
6316c57b36SKumar Gala #include <asm/ppc-opcode.h>
64cce1f106SShaohui Xie #include <asm/rio.h>
65ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h>
66ae3a197eSDavid Howells #include <asm/switch_to.h>
67f54db641SMichael Neuling #include <asm/tm.h>
68ae3a197eSDavid Howells #include <asm/debug.h>
6942f5b4caSDaniel Axtens #include <asm/asm-prototypes.h>
70fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h>
714e0e3435SHongtao Jia #include <sysdev/fsl_pci.h>
726cc89badSNaveen N. Rao #include <asm/kprobes.h>
73a99b9c5eSMurilo Opsfelder Araujo #include <asm/stacktrace.h>
74dc1c1ca3SStephen Rothwell 
75da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
765be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly;
775be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
785be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
795be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
805be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
819422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
825be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
8314cf11afSPaul Mackerras 
8414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger);
8514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi);
8614cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt);
8714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep);
8814cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match);
899422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match);
9014cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler);
9114cf11afSPaul Mackerras #endif
9214cf11afSPaul Mackerras 
938b3c34cfSMichael Neuling /* Transactional Memory trap debug */
948b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW
958b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x)
968b3c34cfSMichael Neuling #else
978b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0)
988b3c34cfSMichael Neuling #endif
998b3c34cfSMichael Neuling 
1000f642d61SMurilo Opsfelder Araujo static const char *signame(int signr)
1010f642d61SMurilo Opsfelder Araujo {
1020f642d61SMurilo Opsfelder Araujo 	switch (signr) {
1030f642d61SMurilo Opsfelder Araujo 	case SIGBUS:	return "bus error";
1040f642d61SMurilo Opsfelder Araujo 	case SIGFPE:	return "floating point exception";
1050f642d61SMurilo Opsfelder Araujo 	case SIGILL:	return "illegal instruction";
1060f642d61SMurilo Opsfelder Araujo 	case SIGSEGV:	return "segfault";
1070f642d61SMurilo Opsfelder Araujo 	case SIGTRAP:	return "unhandled trap";
1080f642d61SMurilo Opsfelder Araujo 	}
1090f642d61SMurilo Opsfelder Araujo 
1100f642d61SMurilo Opsfelder Araujo 	return "unknown signal";
1110f642d61SMurilo Opsfelder Araujo }
1120f642d61SMurilo Opsfelder Araujo 
11314cf11afSPaul Mackerras /*
11414cf11afSPaul Mackerras  * Trap & Exception support
11514cf11afSPaul Mackerras  */
11614cf11afSPaul Mackerras 
1176031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT
1186031d9d9Santon@samba.org static void pmac_backlight_unblank(void)
1196031d9d9Santon@samba.org {
1206031d9d9Santon@samba.org 	mutex_lock(&pmac_backlight_mutex);
1216031d9d9Santon@samba.org 	if (pmac_backlight) {
1226031d9d9Santon@samba.org 		struct backlight_properties *props;
1236031d9d9Santon@samba.org 
1246031d9d9Santon@samba.org 		props = &pmac_backlight->props;
1256031d9d9Santon@samba.org 		props->brightness = props->max_brightness;
1266031d9d9Santon@samba.org 		props->power = FB_BLANK_UNBLANK;
1276031d9d9Santon@samba.org 		backlight_update_status(pmac_backlight);
1286031d9d9Santon@samba.org 	}
1296031d9d9Santon@samba.org 	mutex_unlock(&pmac_backlight_mutex);
1306031d9d9Santon@samba.org }
1316031d9d9Santon@samba.org #else
1326031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { }
1336031d9d9Santon@samba.org #endif
1346031d9d9Santon@samba.org 
1356fcd6baaSNicholas Piggin /*
1366fcd6baaSNicholas Piggin  * If oops/die is expected to crash the machine, return true here.
1376fcd6baaSNicholas Piggin  *
1386fcd6baaSNicholas Piggin  * This should not be expected to be 100% accurate, there may be
1396fcd6baaSNicholas Piggin  * notifiers registered or other unexpected conditions that may bring
1406fcd6baaSNicholas Piggin  * down the kernel. Or if the current process in the kernel is holding
1416fcd6baaSNicholas Piggin  * locks or has other critical state, the kernel may become effectively
1426fcd6baaSNicholas Piggin  * unusable anyway.
1436fcd6baaSNicholas Piggin  */
1446fcd6baaSNicholas Piggin bool die_will_crash(void)
1456fcd6baaSNicholas Piggin {
1466fcd6baaSNicholas Piggin 	if (should_fadump_crash())
1476fcd6baaSNicholas Piggin 		return true;
1486fcd6baaSNicholas Piggin 	if (kexec_should_crash(current))
1496fcd6baaSNicholas Piggin 		return true;
1506fcd6baaSNicholas Piggin 	if (in_interrupt() || panic_on_oops ||
1516fcd6baaSNicholas Piggin 			!current->pid || is_global_init(current))
1526fcd6baaSNicholas Piggin 		return true;
1536fcd6baaSNicholas Piggin 
1546fcd6baaSNicholas Piggin 	return false;
1556fcd6baaSNicholas Piggin }
1566fcd6baaSNicholas Piggin 
157760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
158760ca4dcSAnton Blanchard static int die_owner = -1;
159760ca4dcSAnton Blanchard static unsigned int die_nest_count;
160c0ce7d08SDavid Wilder static int die_counter;
161760ca4dcSAnton Blanchard 
16235adacd6SNicholas Piggin extern void panic_flush_kmsg_start(void)
16335adacd6SNicholas Piggin {
16435adacd6SNicholas Piggin 	/*
16535adacd6SNicholas Piggin 	 * These are mostly taken from kernel/panic.c, but tries to do
16635adacd6SNicholas Piggin 	 * relatively minimal work. Don't use delay functions (TB may
16735adacd6SNicholas Piggin 	 * be broken), don't crash dump (need to set a firmware log),
16835adacd6SNicholas Piggin 	 * don't run notifiers. We do want to get some information to
16935adacd6SNicholas Piggin 	 * Linux console.
17035adacd6SNicholas Piggin 	 */
17135adacd6SNicholas Piggin 	console_verbose();
17235adacd6SNicholas Piggin 	bust_spinlocks(1);
17335adacd6SNicholas Piggin }
17435adacd6SNicholas Piggin 
17535adacd6SNicholas Piggin extern void panic_flush_kmsg_end(void)
17635adacd6SNicholas Piggin {
17735adacd6SNicholas Piggin 	printk_safe_flush_on_panic();
17835adacd6SNicholas Piggin 	kmsg_dump(KMSG_DUMP_PANIC);
17935adacd6SNicholas Piggin 	bust_spinlocks(0);
18035adacd6SNicholas Piggin 	debug_locks_off();
18135adacd6SNicholas Piggin 	console_flush_on_panic();
18235adacd6SNicholas Piggin }
18335adacd6SNicholas Piggin 
18403465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs)
185760ca4dcSAnton Blanchard {
186760ca4dcSAnton Blanchard 	int cpu;
18734c2a14fSanton@samba.org 	unsigned long flags;
18814cf11afSPaul Mackerras 
189293e4688Santon@samba.org 	oops_enter();
190293e4688Santon@samba.org 
191760ca4dcSAnton Blanchard 	/* racy, but better than risking deadlock. */
192760ca4dcSAnton Blanchard 	raw_local_irq_save(flags);
193760ca4dcSAnton Blanchard 	cpu = smp_processor_id();
194760ca4dcSAnton Blanchard 	if (!arch_spin_trylock(&die_lock)) {
195760ca4dcSAnton Blanchard 		if (cpu == die_owner)
196760ca4dcSAnton Blanchard 			/* nested oops. should stop eventually */;
197760ca4dcSAnton Blanchard 		else
198760ca4dcSAnton Blanchard 			arch_spin_lock(&die_lock);
199760ca4dcSAnton Blanchard 	}
200760ca4dcSAnton Blanchard 	die_nest_count++;
201760ca4dcSAnton Blanchard 	die_owner = cpu;
20214cf11afSPaul Mackerras 	console_verbose();
20314cf11afSPaul Mackerras 	bust_spinlocks(1);
2046031d9d9Santon@samba.org 	if (machine_is(powermac))
2056031d9d9Santon@samba.org 		pmac_backlight_unblank();
206760ca4dcSAnton Blanchard 	return flags;
20734c2a14fSanton@samba.org }
20803465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin);
2095474c120SMichael Hanselmann 
21003465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs,
211760ca4dcSAnton Blanchard 			       int signr)
212760ca4dcSAnton Blanchard {
21314cf11afSPaul Mackerras 	bust_spinlocks(0);
214373d4d09SRusty Russell 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
215760ca4dcSAnton Blanchard 	die_nest_count--;
21658154c8cSAnton Blanchard 	oops_exit();
21758154c8cSAnton Blanchard 	printk("\n");
2187458e8b2SNicholas Piggin 	if (!die_nest_count) {
219760ca4dcSAnton Blanchard 		/* Nest count reaches zero, release the lock. */
2207458e8b2SNicholas Piggin 		die_owner = -1;
221760ca4dcSAnton Blanchard 		arch_spin_unlock(&die_lock);
2227458e8b2SNicholas Piggin 	}
223760ca4dcSAnton Blanchard 	raw_local_irq_restore(flags);
224cc532915SMichael Ellerman 
225d40b6768SNicholas Piggin 	/*
226d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
227d40b6768SNicholas Piggin 	 */
228d40b6768SNicholas Piggin 	if (TRAP(regs) == 0x100)
229d40b6768SNicholas Piggin 		return;
230d40b6768SNicholas Piggin 
231ebaeb5aeSMahesh Salgaonkar 	crash_fadump(regs, "die oops");
232ebaeb5aeSMahesh Salgaonkar 
2334388c9b3SNicholas Piggin 	if (kexec_should_crash(current))
234cc532915SMichael Ellerman 		crash_kexec(regs);
2359b00ac06SAnton Blanchard 
236760ca4dcSAnton Blanchard 	if (!signr)
237760ca4dcSAnton Blanchard 		return;
238760ca4dcSAnton Blanchard 
23958154c8cSAnton Blanchard 	/*
24058154c8cSAnton Blanchard 	 * While our oops output is serialised by a spinlock, output
24158154c8cSAnton Blanchard 	 * from panic() called below can race and corrupt it. If we
24258154c8cSAnton Blanchard 	 * know we are going to panic, delay for 1 second so we have a
24358154c8cSAnton Blanchard 	 * chance to get clean backtraces from all CPUs that are oopsing.
24458154c8cSAnton Blanchard 	 */
24558154c8cSAnton Blanchard 	if (in_interrupt() || panic_on_oops || !current->pid ||
24658154c8cSAnton Blanchard 	    is_global_init(current)) {
24758154c8cSAnton Blanchard 		mdelay(MSEC_PER_SEC);
24858154c8cSAnton Blanchard 	}
24958154c8cSAnton Blanchard 
25014cf11afSPaul Mackerras 	if (in_interrupt())
25114cf11afSPaul Mackerras 		panic("Fatal exception in interrupt");
252cea6a4baSHorms 	if (panic_on_oops)
253012c437dSHorms 		panic("Fatal exception");
254760ca4dcSAnton Blanchard 	do_exit(signr);
255760ca4dcSAnton Blanchard }
25603465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end);
257cea6a4baSHorms 
25803465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err)
259760ca4dcSAnton Blanchard {
260760ca4dcSAnton Blanchard 	printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
2612e82ca3cSMichael Ellerman 
2622e82ca3cSMichael Ellerman 	if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
2632e82ca3cSMichael Ellerman 		printk("LE ");
2642e82ca3cSMichael Ellerman 	else
2652e82ca3cSMichael Ellerman 		printk("BE ");
2662e82ca3cSMichael Ellerman 
2671c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_PREEMPT))
26872c0d9eeSMichael Ellerman 		pr_cont("PREEMPT ");
2691c56cd8eSMichael Ellerman 
2701c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_SMP))
27172c0d9eeSMichael Ellerman 		pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
2721c56cd8eSMichael Ellerman 
273e7df0d88SJoonsoo Kim 	if (debug_pagealloc_enabled())
27472c0d9eeSMichael Ellerman 		pr_cont("DEBUG_PAGEALLOC ");
2751c56cd8eSMichael Ellerman 
2761c56cd8eSMichael Ellerman 	if (IS_ENABLED(CONFIG_NUMA))
27772c0d9eeSMichael Ellerman 		pr_cont("NUMA ");
2781c56cd8eSMichael Ellerman 
27972c0d9eeSMichael Ellerman 	pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
280760ca4dcSAnton Blanchard 
281760ca4dcSAnton Blanchard 	if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
282760ca4dcSAnton Blanchard 		return 1;
283760ca4dcSAnton Blanchard 
284760ca4dcSAnton Blanchard 	print_modules();
285760ca4dcSAnton Blanchard 	show_regs(regs);
28614cf11afSPaul Mackerras 
28714cf11afSPaul Mackerras 	return 0;
28814cf11afSPaul Mackerras }
28903465f89SNicholas Piggin NOKPROBE_SYMBOL(__die);
29014cf11afSPaul Mackerras 
291760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err)
292760ca4dcSAnton Blanchard {
2936f44b20eSNicholas Piggin 	unsigned long flags;
294760ca4dcSAnton Blanchard 
295d40b6768SNicholas Piggin 	/*
296d40b6768SNicholas Piggin 	 * system_reset_excption handles debugger, crash dump, panic, for 0x100
297d40b6768SNicholas Piggin 	 */
298d40b6768SNicholas Piggin 	if (TRAP(regs) != 0x100) {
2996f44b20eSNicholas Piggin 		if (debugger(regs))
3006f44b20eSNicholas Piggin 			return;
301d40b6768SNicholas Piggin 	}
3026f44b20eSNicholas Piggin 
3036f44b20eSNicholas Piggin 	flags = oops_begin(regs);
304760ca4dcSAnton Blanchard 	if (__die(str, regs, err))
305760ca4dcSAnton Blanchard 		err = 0;
306760ca4dcSAnton Blanchard 	oops_end(flags, regs, err);
307760ca4dcSAnton Blanchard }
30815770a13SNaveen N. Rao NOKPROBE_SYMBOL(die);
309760ca4dcSAnton Blanchard 
310efc463adSEric W. Biederman void user_single_step_report(struct pt_regs *regs)
31125baa35bSOleg Nesterov {
312efc463adSEric W. Biederman 	force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)regs->nip, current);
31325baa35bSOleg Nesterov }
31425baa35bSOleg Nesterov 
315658b0f92SMurilo Opsfelder Araujo static void show_signal_msg(int signr, struct pt_regs *regs, int code,
316658b0f92SMurilo Opsfelder Araujo 			    unsigned long addr)
31714cf11afSPaul Mackerras {
318997dd26cSMichael Ellerman 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
319997dd26cSMichael Ellerman 				      DEFAULT_RATELIMIT_BURST);
320997dd26cSMichael Ellerman 
321997dd26cSMichael Ellerman 	if (!show_unhandled_signals)
32235a52a10SMurilo Opsfelder Araujo 		return;
32335a52a10SMurilo Opsfelder Araujo 
32435a52a10SMurilo Opsfelder Araujo 	if (!unhandled_signal(current, signr))
32535a52a10SMurilo Opsfelder Araujo 		return;
32635a52a10SMurilo Opsfelder Araujo 
327997dd26cSMichael Ellerman 	if (!__ratelimit(&rs))
328997dd26cSMichael Ellerman 		return;
329997dd26cSMichael Ellerman 
3300f642d61SMurilo Opsfelder Araujo 	pr_info("%s[%d]: %s (%d) at %lx nip %lx lr %lx code %x",
3310f642d61SMurilo Opsfelder Araujo 		current->comm, current->pid, signame(signr), signr,
332d0c3d534SOlof Johansson 		addr, regs->nip, regs->link, code);
3330f642d61SMurilo Opsfelder Araujo 
3340f642d61SMurilo Opsfelder Araujo 	print_vma_addr(KERN_CONT " in ", regs->nip);
3350f642d61SMurilo Opsfelder Araujo 
3360f642d61SMurilo Opsfelder Araujo 	pr_cont("\n");
337a99b9c5eSMurilo Opsfelder Araujo 
338a99b9c5eSMurilo Opsfelder Araujo 	show_user_instructions(regs);
33914cf11afSPaul Mackerras }
340658b0f92SMurilo Opsfelder Araujo 
341*2c44ce28SEric W. Biederman static bool exception_common(int signr, struct pt_regs *regs, int code,
342*2c44ce28SEric W. Biederman 			      unsigned long addr)
343658b0f92SMurilo Opsfelder Araujo {
344658b0f92SMurilo Opsfelder Araujo 	if (!user_mode(regs)) {
345658b0f92SMurilo Opsfelder Araujo 		die("Exception in kernel mode", regs, signr);
346*2c44ce28SEric W. Biederman 		return false;
347658b0f92SMurilo Opsfelder Araujo 	}
348658b0f92SMurilo Opsfelder Araujo 
349658b0f92SMurilo Opsfelder Araujo 	show_signal_msg(signr, regs, code, addr);
35014cf11afSPaul Mackerras 
351a3512b2dSBenjamin Herrenschmidt 	if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
3529f2f79e3SBenjamin Herrenschmidt 		local_irq_enable();
3539f2f79e3SBenjamin Herrenschmidt 
35441ab5266SAnanth N Mavinakayanahalli 	current->thread.trap_nr = code;
355c5cc1f4dSThiago Jung Bauermann 
356c5cc1f4dSThiago Jung Bauermann 	/*
357c5cc1f4dSThiago Jung Bauermann 	 * Save all the pkey registers AMR/IAMR/UAMOR. Eg: Core dumps need
358c5cc1f4dSThiago Jung Bauermann 	 * to capture the content, if the task gets killed.
359c5cc1f4dSThiago Jung Bauermann 	 */
360c5cc1f4dSThiago Jung Bauermann 	thread_pkey_regs_save(&current->thread);
361c5cc1f4dSThiago Jung Bauermann 
362*2c44ce28SEric W. Biederman 	return true;
363*2c44ce28SEric W. Biederman }
364*2c44ce28SEric W. Biederman 
365*2c44ce28SEric W. Biederman void _exception_pkey(int signr, struct pt_regs *regs, int code, unsigned long addr, int key)
366*2c44ce28SEric W. Biederman {
367*2c44ce28SEric W. Biederman 	siginfo_t info;
368*2c44ce28SEric W. Biederman 
369*2c44ce28SEric W. Biederman 	if (!exception_common(signr, regs, code, addr))
370*2c44ce28SEric W. Biederman 		return;
371*2c44ce28SEric W. Biederman 
3723eb0f519SEric W. Biederman 	clear_siginfo(&info);
37314cf11afSPaul Mackerras 	info.si_signo = signr;
37414cf11afSPaul Mackerras 	info.si_code = code;
37514cf11afSPaul Mackerras 	info.si_addr = (void __user *) addr;
37699cd1302SRam Pai 	info.si_pkey = key;
37799cd1302SRam Pai 
37814cf11afSPaul Mackerras 	force_sig_info(signr, &info, current);
37914cf11afSPaul Mackerras }
38014cf11afSPaul Mackerras 
38199cd1302SRam Pai void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
38299cd1302SRam Pai {
38399cd1302SRam Pai 	_exception_pkey(signr, regs, code, addr, 0);
38499cd1302SRam Pai }
38599cd1302SRam Pai 
38614cf11afSPaul Mackerras void system_reset_exception(struct pt_regs *regs)
38714cf11afSPaul Mackerras {
3882b4f3ac5SNicholas Piggin 	/*
3892b4f3ac5SNicholas Piggin 	 * Avoid crashes in case of nested NMI exceptions. Recoverability
3902b4f3ac5SNicholas Piggin 	 * is determined by RI and in_nmi
3912b4f3ac5SNicholas Piggin 	 */
3922b4f3ac5SNicholas Piggin 	bool nested = in_nmi();
3932b4f3ac5SNicholas Piggin 	if (!nested)
3942b4f3ac5SNicholas Piggin 		nmi_enter();
3952b4f3ac5SNicholas Piggin 
396ca41ad43SNicholas Piggin 	__this_cpu_inc(irq_stat.sreset_irqs);
397ca41ad43SNicholas Piggin 
39814cf11afSPaul Mackerras 	/* See if any machine dependent calls */
399c902be71SArnd Bergmann 	if (ppc_md.system_reset_exception) {
400c902be71SArnd Bergmann 		if (ppc_md.system_reset_exception(regs))
401c4f3b52cSNicholas Piggin 			goto out;
402c902be71SArnd Bergmann 	}
40314cf11afSPaul Mackerras 
4044388c9b3SNicholas Piggin 	if (debugger(regs))
4054388c9b3SNicholas Piggin 		goto out;
4064388c9b3SNicholas Piggin 
4074388c9b3SNicholas Piggin 	/*
4084388c9b3SNicholas Piggin 	 * A system reset is a request to dump, so we always send
4094388c9b3SNicholas Piggin 	 * it through the crashdump code (if fadump or kdump are
4104388c9b3SNicholas Piggin 	 * registered).
4114388c9b3SNicholas Piggin 	 */
4124388c9b3SNicholas Piggin 	crash_fadump(regs, "System Reset");
4134388c9b3SNicholas Piggin 
4144388c9b3SNicholas Piggin 	crash_kexec(regs);
4154388c9b3SNicholas Piggin 
4164388c9b3SNicholas Piggin 	/*
4174388c9b3SNicholas Piggin 	 * We aren't the primary crash CPU. We need to send it
4184388c9b3SNicholas Piggin 	 * to a holding pattern to avoid it ending up in the panic
4194388c9b3SNicholas Piggin 	 * code.
4204388c9b3SNicholas Piggin 	 */
4214388c9b3SNicholas Piggin 	crash_kexec_secondary(regs);
4224388c9b3SNicholas Piggin 
4234388c9b3SNicholas Piggin 	/*
4244388c9b3SNicholas Piggin 	 * No debugger or crash dump registered, print logs then
4254388c9b3SNicholas Piggin 	 * panic.
4264388c9b3SNicholas Piggin 	 */
4274552d128SNicholas Piggin 	die("System Reset", regs, SIGABRT);
4284388c9b3SNicholas Piggin 
4294388c9b3SNicholas Piggin 	mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
4304388c9b3SNicholas Piggin 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
4314388c9b3SNicholas Piggin 	nmi_panic(regs, "System Reset");
43214cf11afSPaul Mackerras 
433c4f3b52cSNicholas Piggin out:
434c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64
435c4f3b52cSNicholas Piggin 	BUG_ON(get_paca()->in_nmi == 0);
436c4f3b52cSNicholas Piggin 	if (get_paca()->in_nmi > 1)
4374388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable nested System Reset");
438c4f3b52cSNicholas Piggin #endif
43914cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
44014cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
4414388c9b3SNicholas Piggin 		nmi_panic(regs, "Unrecoverable System Reset");
44214cf11afSPaul Mackerras 
4432b4f3ac5SNicholas Piggin 	if (!nested)
4442b4f3ac5SNicholas Piggin 		nmi_exit();
4452b4f3ac5SNicholas Piggin 
44614cf11afSPaul Mackerras 	/* What should we do here? We could issue a shutdown or hard reset. */
44714cf11afSPaul Mackerras }
4481e9b4507SMahesh Salgaonkar 
44914cf11afSPaul Mackerras /*
45014cf11afSPaul Mackerras  * I/O accesses can cause machine checks on powermacs.
45114cf11afSPaul Mackerras  * Check if the NIP corresponds to the address of a sync
45214cf11afSPaul Mackerras  * instruction for which there is an entry in the exception
45314cf11afSPaul Mackerras  * table.
45414cf11afSPaul Mackerras  * Note that the 601 only takes a machine check on TEA
45514cf11afSPaul Mackerras  * (transfer error ack) signal assertion, and does not
45614cf11afSPaul Mackerras  * set any of the top 16 bits of SRR1.
45714cf11afSPaul Mackerras  *  -- paulus.
45814cf11afSPaul Mackerras  */
45914cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs)
46014cf11afSPaul Mackerras {
46168a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
46214cf11afSPaul Mackerras 	unsigned long msr = regs->msr;
46314cf11afSPaul Mackerras 	const struct exception_table_entry *entry;
46414cf11afSPaul Mackerras 	unsigned int *nip = (unsigned int *)regs->nip;
46514cf11afSPaul Mackerras 
46614cf11afSPaul Mackerras 	if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
46714cf11afSPaul Mackerras 	    && (entry = search_exception_tables(regs->nip)) != NULL) {
46814cf11afSPaul Mackerras 		/*
46914cf11afSPaul Mackerras 		 * Check that it's a sync instruction, or somewhere
47014cf11afSPaul Mackerras 		 * in the twi; isync; nop sequence that inb/inw/inl uses.
47114cf11afSPaul Mackerras 		 * As the address is in the exception table
47214cf11afSPaul Mackerras 		 * we should be able to read the instr there.
47314cf11afSPaul Mackerras 		 * For the debug message, we look at the preceding
47414cf11afSPaul Mackerras 		 * load or store.
47514cf11afSPaul Mackerras 		 */
476ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_NOP)
47714cf11afSPaul Mackerras 			nip -= 2;
478ddc6cd0dSChristophe Leroy 		else if (*nip == PPC_INST_ISYNC)
47914cf11afSPaul Mackerras 			--nip;
480ddc6cd0dSChristophe Leroy 		if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
48114cf11afSPaul Mackerras 			unsigned int rb;
48214cf11afSPaul Mackerras 
48314cf11afSPaul Mackerras 			--nip;
48414cf11afSPaul Mackerras 			rb = (*nip >> 11) & 0x1f;
48514cf11afSPaul Mackerras 			printk(KERN_DEBUG "%s bad port %lx at %p\n",
48614cf11afSPaul Mackerras 			       (*nip & 0x100)? "OUT to": "IN from",
48714cf11afSPaul Mackerras 			       regs->gpr[rb] - _IO_BASE, nip);
48814cf11afSPaul Mackerras 			regs->msr |= MSR_RI;
48961a92f70SNicholas Piggin 			regs->nip = extable_fixup(entry);
49014cf11afSPaul Mackerras 			return 1;
49114cf11afSPaul Mackerras 		}
49214cf11afSPaul Mackerras 	}
49368a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */
49414cf11afSPaul Mackerras 	return 0;
49514cf11afSPaul Mackerras }
49614cf11afSPaul Mackerras 
497172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
49814cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception
49914cf11afSPaul Mackerras    is in the ESR. */
50014cf11afSPaul Mackerras #define get_reason(regs)	((regs)->dsisr)
50114cf11afSPaul Mackerras #define REASON_FP		ESR_FP
50214cf11afSPaul Mackerras #define REASON_ILLEGAL		(ESR_PIL | ESR_PUO)
50314cf11afSPaul Mackerras #define REASON_PRIVILEGED	ESR_PPR
50414cf11afSPaul Mackerras #define REASON_TRAP		ESR_PTR
50514cf11afSPaul Mackerras 
50614cf11afSPaul Mackerras /* single-step stuff */
50751ae8d4aSBharat Bhushan #define single_stepping(regs)	(current->thread.debug.dbcr0 & DBCR0_IC)
50851ae8d4aSBharat Bhushan #define clear_single_step(regs)	(current->thread.debug.dbcr0 &= ~DBCR0_IC)
5090e524e76SMatt Evans #define clear_br_trace(regs)	do {} while(0)
51014cf11afSPaul Mackerras #else
51114cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program
51214cf11afSPaul Mackerras    exception is in the MSR. */
51314cf11afSPaul Mackerras #define get_reason(regs)	((regs)->msr)
514d30a5a52SMichael Ellerman #define REASON_TM		SRR1_PROGTM
515d30a5a52SMichael Ellerman #define REASON_FP		SRR1_PROGFPE
516d30a5a52SMichael Ellerman #define REASON_ILLEGAL		SRR1_PROGILL
517d30a5a52SMichael Ellerman #define REASON_PRIVILEGED	SRR1_PROGPRIV
518d30a5a52SMichael Ellerman #define REASON_TRAP		SRR1_PROGTRAP
51914cf11afSPaul Mackerras 
52014cf11afSPaul Mackerras #define single_stepping(regs)	((regs)->msr & MSR_SE)
52114cf11afSPaul Mackerras #define clear_single_step(regs)	((regs)->msr &= ~MSR_SE)
5220e524e76SMatt Evans #define clear_br_trace(regs)	((regs)->msr &= ~MSR_BE)
52314cf11afSPaul Mackerras #endif
52414cf11afSPaul Mackerras 
5250d0935b3SMichael Ellerman #if defined(CONFIG_E500)
526fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs)
527fe04b112SScott Wood {
528fe04b112SScott Wood 	unsigned long mcsr = mfspr(SPRN_MCSR);
529a4e89ffbSMatt Weber 	unsigned long pvr = mfspr(SPRN_PVR);
530fe04b112SScott Wood 	unsigned long reason = mcsr;
531fe04b112SScott Wood 	int recoverable = 1;
532fe04b112SScott Wood 
53382a9a480SScott Wood 	if (reason & MCSR_LD) {
534cce1f106SShaohui Xie 		recoverable = fsl_rio_mcheck_exception(regs);
535cce1f106SShaohui Xie 		if (recoverable == 1)
536cce1f106SShaohui Xie 			goto silent_out;
537cce1f106SShaohui Xie 	}
538cce1f106SShaohui Xie 
539fe04b112SScott Wood 	printk("Machine check in kernel mode.\n");
540fe04b112SScott Wood 	printk("Caused by (from MCSR=%lx): ", reason);
541fe04b112SScott Wood 
542fe04b112SScott Wood 	if (reason & MCSR_MCP)
543fe04b112SScott Wood 		printk("Machine Check Signal\n");
544fe04b112SScott Wood 
545fe04b112SScott Wood 	if (reason & MCSR_ICPERR) {
546fe04b112SScott Wood 		printk("Instruction Cache Parity Error\n");
547fe04b112SScott Wood 
548fe04b112SScott Wood 		/*
549fe04b112SScott Wood 		 * This is recoverable by invalidating the i-cache.
550fe04b112SScott Wood 		 */
551fe04b112SScott Wood 		mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
552fe04b112SScott Wood 		while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
553fe04b112SScott Wood 			;
554fe04b112SScott Wood 
555fe04b112SScott Wood 		/*
556fe04b112SScott Wood 		 * This will generally be accompanied by an instruction
557fe04b112SScott Wood 		 * fetch error report -- only treat MCSR_IF as fatal
558fe04b112SScott Wood 		 * if it wasn't due to an L1 parity error.
559fe04b112SScott Wood 		 */
560fe04b112SScott Wood 		reason &= ~MCSR_IF;
561fe04b112SScott Wood 	}
562fe04b112SScott Wood 
563fe04b112SScott Wood 	if (reason & MCSR_DCPERR_MC) {
564fe04b112SScott Wood 		printk("Data Cache Parity Error\n");
56537caf9f2SKumar Gala 
56637caf9f2SKumar Gala 		/*
56737caf9f2SKumar Gala 		 * In write shadow mode we auto-recover from the error, but it
56837caf9f2SKumar Gala 		 * may still get logged and cause a machine check.  We should
56937caf9f2SKumar Gala 		 * only treat the non-write shadow case as non-recoverable.
57037caf9f2SKumar Gala 		 */
571a4e89ffbSMatt Weber 		/* On e6500 core, L1 DCWS (Data cache write shadow mode) bit
572a4e89ffbSMatt Weber 		 * is not implemented but L1 data cache always runs in write
573a4e89ffbSMatt Weber 		 * shadow mode. Hence on data cache parity errors HW will
574a4e89ffbSMatt Weber 		 * automatically invalidate the L1 Data Cache.
575a4e89ffbSMatt Weber 		 */
576a4e89ffbSMatt Weber 		if (PVR_VER(pvr) != PVR_VER_E6500) {
57737caf9f2SKumar Gala 			if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
578fe04b112SScott Wood 				recoverable = 0;
579fe04b112SScott Wood 		}
580a4e89ffbSMatt Weber 	}
581fe04b112SScott Wood 
582fe04b112SScott Wood 	if (reason & MCSR_L2MMU_MHIT) {
583fe04b112SScott Wood 		printk("Hit on multiple TLB entries\n");
584fe04b112SScott Wood 		recoverable = 0;
585fe04b112SScott Wood 	}
586fe04b112SScott Wood 
587fe04b112SScott Wood 	if (reason & MCSR_NMI)
588fe04b112SScott Wood 		printk("Non-maskable interrupt\n");
589fe04b112SScott Wood 
590fe04b112SScott Wood 	if (reason & MCSR_IF) {
591fe04b112SScott Wood 		printk("Instruction Fetch Error Report\n");
592fe04b112SScott Wood 		recoverable = 0;
593fe04b112SScott Wood 	}
594fe04b112SScott Wood 
595fe04b112SScott Wood 	if (reason & MCSR_LD) {
596fe04b112SScott Wood 		printk("Load Error Report\n");
597fe04b112SScott Wood 		recoverable = 0;
598fe04b112SScott Wood 	}
599fe04b112SScott Wood 
600fe04b112SScott Wood 	if (reason & MCSR_ST) {
601fe04b112SScott Wood 		printk("Store Error Report\n");
602fe04b112SScott Wood 		recoverable = 0;
603fe04b112SScott Wood 	}
604fe04b112SScott Wood 
605fe04b112SScott Wood 	if (reason & MCSR_LDG) {
606fe04b112SScott Wood 		printk("Guarded Load Error Report\n");
607fe04b112SScott Wood 		recoverable = 0;
608fe04b112SScott Wood 	}
609fe04b112SScott Wood 
610fe04b112SScott Wood 	if (reason & MCSR_TLBSYNC)
611fe04b112SScott Wood 		printk("Simultaneous tlbsync operations\n");
612fe04b112SScott Wood 
613fe04b112SScott Wood 	if (reason & MCSR_BSL2_ERR) {
614fe04b112SScott Wood 		printk("Level 2 Cache Error\n");
615fe04b112SScott Wood 		recoverable = 0;
616fe04b112SScott Wood 	}
617fe04b112SScott Wood 
618fe04b112SScott Wood 	if (reason & MCSR_MAV) {
619fe04b112SScott Wood 		u64 addr;
620fe04b112SScott Wood 
621fe04b112SScott Wood 		addr = mfspr(SPRN_MCAR);
622fe04b112SScott Wood 		addr |= (u64)mfspr(SPRN_MCARU) << 32;
623fe04b112SScott Wood 
624fe04b112SScott Wood 		printk("Machine Check %s Address: %#llx\n",
625fe04b112SScott Wood 		       reason & MCSR_MEA ? "Effective" : "Physical", addr);
626fe04b112SScott Wood 	}
627fe04b112SScott Wood 
628cce1f106SShaohui Xie silent_out:
629fe04b112SScott Wood 	mtspr(SPRN_MCSR, mcsr);
630fe04b112SScott Wood 	return mfspr(SPRN_MCSR) == 0 && recoverable;
631fe04b112SScott Wood }
632fe04b112SScott Wood 
63347c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs)
63447c0bd1aSBenjamin Herrenschmidt {
63542bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
63647c0bd1aSBenjamin Herrenschmidt 
637cce1f106SShaohui Xie 	if (reason & MCSR_BUS_RBERR) {
638cce1f106SShaohui Xie 		if (fsl_rio_mcheck_exception(regs))
639cce1f106SShaohui Xie 			return 1;
6404e0e3435SHongtao Jia 		if (fsl_pci_mcheck_exception(regs))
6414e0e3435SHongtao Jia 			return 1;
642cce1f106SShaohui Xie 	}
643cce1f106SShaohui Xie 
64414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
64514cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
64614cf11afSPaul Mackerras 
64714cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
64814cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
64914cf11afSPaul Mackerras 	if (reason & MCSR_ICPERR)
65014cf11afSPaul Mackerras 		printk("Instruction Cache Parity Error\n");
65114cf11afSPaul Mackerras 	if (reason & MCSR_DCP_PERR)
65214cf11afSPaul Mackerras 		printk("Data Cache Push Parity Error\n");
65314cf11afSPaul Mackerras 	if (reason & MCSR_DCPERR)
65414cf11afSPaul Mackerras 		printk("Data Cache Parity Error\n");
65514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IAERR)
65614cf11afSPaul Mackerras 		printk("Bus - Instruction Address Error\n");
65714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RAERR)
65814cf11afSPaul Mackerras 		printk("Bus - Read Address Error\n");
65914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WAERR)
66014cf11afSPaul Mackerras 		printk("Bus - Write Address Error\n");
66114cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IBERR)
66214cf11afSPaul Mackerras 		printk("Bus - Instruction Data Error\n");
66314cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RBERR)
66414cf11afSPaul Mackerras 		printk("Bus - Read Data Bus Error\n");
66514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WBERR)
666c1528339SWladislav Wiebe 		printk("Bus - Write Data Bus Error\n");
66714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IPERR)
66814cf11afSPaul Mackerras 		printk("Bus - Instruction Parity Error\n");
66914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_RPERR)
67014cf11afSPaul Mackerras 		printk("Bus - Read Parity Error\n");
67147c0bd1aSBenjamin Herrenschmidt 
67247c0bd1aSBenjamin Herrenschmidt 	return 0;
67347c0bd1aSBenjamin Herrenschmidt }
6744490c06bSKumar Gala 
6754490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs)
6764490c06bSKumar Gala {
6774490c06bSKumar Gala 	return 0;
6784490c06bSKumar Gala }
67914cf11afSPaul Mackerras #elif defined(CONFIG_E200)
68047c0bd1aSBenjamin Herrenschmidt int machine_check_e200(struct pt_regs *regs)
68147c0bd1aSBenjamin Herrenschmidt {
68242bff234SMichael Ellerman 	unsigned long reason = mfspr(SPRN_MCSR);
68347c0bd1aSBenjamin Herrenschmidt 
68414cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
68514cf11afSPaul Mackerras 	printk("Caused by (from MCSR=%lx): ", reason);
68614cf11afSPaul Mackerras 
68714cf11afSPaul Mackerras 	if (reason & MCSR_MCP)
68814cf11afSPaul Mackerras 		printk("Machine Check Signal\n");
68914cf11afSPaul Mackerras 	if (reason & MCSR_CP_PERR)
69014cf11afSPaul Mackerras 		printk("Cache Push Parity Error\n");
69114cf11afSPaul Mackerras 	if (reason & MCSR_CPERR)
69214cf11afSPaul Mackerras 		printk("Cache Parity Error\n");
69314cf11afSPaul Mackerras 	if (reason & MCSR_EXCP_ERR)
69414cf11afSPaul Mackerras 		printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
69514cf11afSPaul Mackerras 	if (reason & MCSR_BUS_IRERR)
69614cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on instruction fetch\n");
69714cf11afSPaul Mackerras 	if (reason & MCSR_BUS_DRERR)
69814cf11afSPaul Mackerras 		printk("Bus - Read Bus Error on data load\n");
69914cf11afSPaul Mackerras 	if (reason & MCSR_BUS_WRERR)
70014cf11afSPaul Mackerras 		printk("Bus - Write Bus Error on buffered store or cache line push\n");
70147c0bd1aSBenjamin Herrenschmidt 
70247c0bd1aSBenjamin Herrenschmidt 	return 0;
70347c0bd1aSBenjamin Herrenschmidt }
7047f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32)
70547c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs)
70647c0bd1aSBenjamin Herrenschmidt {
70742bff234SMichael Ellerman 	unsigned long reason = regs->msr;
70847c0bd1aSBenjamin Herrenschmidt 
70914cf11afSPaul Mackerras 	printk("Machine check in kernel mode.\n");
71014cf11afSPaul Mackerras 	printk("Caused by (from SRR1=%lx): ", reason);
71114cf11afSPaul Mackerras 	switch (reason & 0x601F0000) {
71214cf11afSPaul Mackerras 	case 0x80000:
71314cf11afSPaul Mackerras 		printk("Machine check signal\n");
71414cf11afSPaul Mackerras 		break;
71514cf11afSPaul Mackerras 	case 0:		/* for 601 */
71614cf11afSPaul Mackerras 	case 0x40000:
71714cf11afSPaul Mackerras 	case 0x140000:	/* 7450 MSS error and TEA */
71814cf11afSPaul Mackerras 		printk("Transfer error ack signal\n");
71914cf11afSPaul Mackerras 		break;
72014cf11afSPaul Mackerras 	case 0x20000:
72114cf11afSPaul Mackerras 		printk("Data parity error signal\n");
72214cf11afSPaul Mackerras 		break;
72314cf11afSPaul Mackerras 	case 0x10000:
72414cf11afSPaul Mackerras 		printk("Address parity error signal\n");
72514cf11afSPaul Mackerras 		break;
72614cf11afSPaul Mackerras 	case 0x20000000:
72714cf11afSPaul Mackerras 		printk("L1 Data Cache error\n");
72814cf11afSPaul Mackerras 		break;
72914cf11afSPaul Mackerras 	case 0x40000000:
73014cf11afSPaul Mackerras 		printk("L1 Instruction Cache error\n");
73114cf11afSPaul Mackerras 		break;
73214cf11afSPaul Mackerras 	case 0x00100000:
73314cf11afSPaul Mackerras 		printk("L2 data cache parity error\n");
73414cf11afSPaul Mackerras 		break;
73514cf11afSPaul Mackerras 	default:
73614cf11afSPaul Mackerras 		printk("Unknown values in msr\n");
73714cf11afSPaul Mackerras 	}
73875918a4bSOlof Johansson 	return 0;
73975918a4bSOlof Johansson }
74047c0bd1aSBenjamin Herrenschmidt #endif /* everything else */
74175918a4bSOlof Johansson 
74275918a4bSOlof Johansson void machine_check_exception(struct pt_regs *regs)
74375918a4bSOlof Johansson {
74475918a4bSOlof Johansson 	int recover = 0;
745b96672ddSNicholas Piggin 	bool nested = in_nmi();
746b96672ddSNicholas Piggin 	if (!nested)
747b96672ddSNicholas Piggin 		nmi_enter();
74875918a4bSOlof Johansson 
749f886f0f6SNicholas Piggin 	/* 64s accounts the mce in machine_check_early when in HVMODE */
750f886f0f6SNicholas Piggin 	if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
75169111bacSChristoph Lameter 		__this_cpu_inc(irq_stat.mce_exceptions);
75289713ed1SAnton Blanchard 
753d93b0ac0SMahesh Salgaonkar 	add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
754d93b0ac0SMahesh Salgaonkar 
75547c0bd1aSBenjamin Herrenschmidt 	/* See if any machine dependent calls. In theory, we would want
75647c0bd1aSBenjamin Herrenschmidt 	 * to call the CPU first, and call the ppc_md. one if the CPU
75747c0bd1aSBenjamin Herrenschmidt 	 * one returns a positive number. However there is existing code
75847c0bd1aSBenjamin Herrenschmidt 	 * that assumes the board gets a first chance, so let's keep it
75947c0bd1aSBenjamin Herrenschmidt 	 * that way for now and fix things later. --BenH.
76047c0bd1aSBenjamin Herrenschmidt 	 */
76175918a4bSOlof Johansson 	if (ppc_md.machine_check_exception)
76275918a4bSOlof Johansson 		recover = ppc_md.machine_check_exception(regs);
76347c0bd1aSBenjamin Herrenschmidt 	else if (cur_cpu_spec->machine_check)
76447c0bd1aSBenjamin Herrenschmidt 		recover = cur_cpu_spec->machine_check(regs);
76575918a4bSOlof Johansson 
76647c0bd1aSBenjamin Herrenschmidt 	if (recover > 0)
767ba12eedeSLi Zhong 		goto bail;
76875918a4bSOlof Johansson 
769a443506bSAnton Blanchard 	if (debugger_fault_handler(regs))
770ba12eedeSLi Zhong 		goto bail;
77175918a4bSOlof Johansson 
77275918a4bSOlof Johansson 	if (check_io_access(regs))
773ba12eedeSLi Zhong 		goto bail;
77475918a4bSOlof Johansson 
7758dad3f92SPaul Mackerras 	die("Machine check", regs, SIGBUS);
77614cf11afSPaul Mackerras 
77714cf11afSPaul Mackerras 	/* Must die if the interrupt is not recoverable */
77814cf11afSPaul Mackerras 	if (!(regs->msr & MSR_RI))
779b96672ddSNicholas Piggin 		nmi_panic(regs, "Unrecoverable Machine check");
780ba12eedeSLi Zhong 
781ba12eedeSLi Zhong bail:
782b96672ddSNicholas Piggin 	if (!nested)
783b96672ddSNicholas Piggin 		nmi_exit();
78414cf11afSPaul Mackerras }
78514cf11afSPaul Mackerras 
78614cf11afSPaul Mackerras void SMIException(struct pt_regs *regs)
78714cf11afSPaul Mackerras {
78814cf11afSPaul Mackerras 	die("System Management Interrupt", regs, SIGABRT);
78914cf11afSPaul Mackerras }
79014cf11afSPaul Mackerras 
7915080332cSMichael Neuling #ifdef CONFIG_VSX
7925080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs)
7935080332cSMichael Neuling {
7945080332cSMichael Neuling 	unsigned int ra, rb, t, i, sel, instr, rc;
7955080332cSMichael Neuling 	const void __user *addr;
7965080332cSMichael Neuling 	u8 vbuf[16], *vdst;
7975080332cSMichael Neuling 	unsigned long ea, msr, msr_mask;
7985080332cSMichael Neuling 	bool swap;
7995080332cSMichael Neuling 
8005080332cSMichael Neuling 	if (__get_user_inatomic(instr, (unsigned int __user *)regs->nip))
8015080332cSMichael Neuling 		return;
8025080332cSMichael Neuling 
8035080332cSMichael Neuling 	/*
8045080332cSMichael Neuling 	 * lxvb16x	opcode: 0x7c0006d8
8055080332cSMichael Neuling 	 * lxvd2x	opcode: 0x7c000698
8065080332cSMichael Neuling 	 * lxvh8x	opcode: 0x7c000658
8075080332cSMichael Neuling 	 * lxvw4x	opcode: 0x7c000618
8085080332cSMichael Neuling 	 */
8095080332cSMichael Neuling 	if ((instr & 0xfc00073e) != 0x7c000618) {
8105080332cSMichael Neuling 		pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx"
8115080332cSMichael Neuling 			 " instr=%08x\n",
8125080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8135080332cSMichael Neuling 			 regs->nip, instr);
8145080332cSMichael Neuling 		return;
8155080332cSMichael Neuling 	}
8165080332cSMichael Neuling 
8175080332cSMichael Neuling 	/* Grab vector registers into the task struct */
8185080332cSMichael Neuling 	msr = regs->msr; /* Grab msr before we flush the bits */
8195080332cSMichael Neuling 	flush_vsx_to_thread(current);
8205080332cSMichael Neuling 	enable_kernel_altivec();
8215080332cSMichael Neuling 
8225080332cSMichael Neuling 	/*
8235080332cSMichael Neuling 	 * Is userspace running with a different endian (this is rare but
8245080332cSMichael Neuling 	 * not impossible)
8255080332cSMichael Neuling 	 */
8265080332cSMichael Neuling 	swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE);
8275080332cSMichael Neuling 
8285080332cSMichael Neuling 	/* Decode the instruction */
8295080332cSMichael Neuling 	ra = (instr >> 16) & 0x1f;
8305080332cSMichael Neuling 	rb = (instr >> 11) & 0x1f;
8315080332cSMichael Neuling 	t = (instr >> 21) & 0x1f;
8325080332cSMichael Neuling 	if (instr & 1)
8335080332cSMichael Neuling 		vdst = (u8 *)&current->thread.vr_state.vr[t];
8345080332cSMichael Neuling 	else
8355080332cSMichael Neuling 		vdst = (u8 *)&current->thread.fp_state.fpr[t][0];
8365080332cSMichael Neuling 
8375080332cSMichael Neuling 	/* Grab the vector address */
8385080332cSMichael Neuling 	ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0);
8395080332cSMichael Neuling 	if (is_32bit_task())
8405080332cSMichael Neuling 		ea &= 0xfffffffful;
8415080332cSMichael Neuling 	addr = (__force const void __user *)ea;
8425080332cSMichael Neuling 
8435080332cSMichael Neuling 	/* Check it */
8445080332cSMichael Neuling 	if (!access_ok(VERIFY_READ, addr, 16)) {
8455080332cSMichael Neuling 		pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx"
8465080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
8475080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8485080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
8495080332cSMichael Neuling 		return;
8505080332cSMichael Neuling 	}
8515080332cSMichael Neuling 
8525080332cSMichael Neuling 	/* Read the vector */
8535080332cSMichael Neuling 	rc = 0;
8545080332cSMichael Neuling 	if ((unsigned long)addr & 0xfUL)
8555080332cSMichael Neuling 		/* unaligned case */
8565080332cSMichael Neuling 		rc = __copy_from_user_inatomic(vbuf, addr, 16);
8575080332cSMichael Neuling 	else
8585080332cSMichael Neuling 		__get_user_atomic_128_aligned(vbuf, addr, rc);
8595080332cSMichael Neuling 	if (rc) {
8605080332cSMichael Neuling 		pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx"
8615080332cSMichael Neuling 			 " instr=%08x addr=%016lx\n",
8625080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8635080332cSMichael Neuling 			 regs->nip, instr, (unsigned long)addr);
8645080332cSMichael Neuling 		return;
8655080332cSMichael Neuling 	}
8665080332cSMichael Neuling 
8675080332cSMichael Neuling 	pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx"
8685080332cSMichael Neuling 		 " instr=%08x addr=%016lx\n",
8695080332cSMichael Neuling 		 smp_processor_id(), current->comm, current->pid, regs->nip,
8705080332cSMichael Neuling 		 instr, (unsigned long) addr);
8715080332cSMichael Neuling 
8725080332cSMichael Neuling 	/* Grab instruction "selector" */
8735080332cSMichael Neuling 	sel = (instr >> 6) & 3;
8745080332cSMichael Neuling 
8755080332cSMichael Neuling 	/*
8765080332cSMichael Neuling 	 * Check to make sure the facility is actually enabled. This
8775080332cSMichael Neuling 	 * could happen if we get a false positive hit.
8785080332cSMichael Neuling 	 *
8795080332cSMichael Neuling 	 * lxvd2x/lxvw4x always check MSR VSX sel = 0,2
8805080332cSMichael Neuling 	 * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3
8815080332cSMichael Neuling 	 */
8825080332cSMichael Neuling 	msr_mask = MSR_VSX;
8835080332cSMichael Neuling 	if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */
8845080332cSMichael Neuling 		msr_mask = MSR_VEC;
8855080332cSMichael Neuling 	if (!(msr & msr_mask)) {
8865080332cSMichael Neuling 		pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx"
8875080332cSMichael Neuling 			 " instr=%08x msr:%016lx\n",
8885080332cSMichael Neuling 			 smp_processor_id(), current->comm, current->pid,
8895080332cSMichael Neuling 			 regs->nip, instr, msr);
8905080332cSMichael Neuling 		return;
8915080332cSMichael Neuling 	}
8925080332cSMichael Neuling 
8935080332cSMichael Neuling 	/* Do logging here before we modify sel based on endian */
8945080332cSMichael Neuling 	switch (sel) {
8955080332cSMichael Neuling 	case 0:	/* lxvw4x */
8965080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvw4x, regs);
8975080332cSMichael Neuling 		break;
8985080332cSMichael Neuling 	case 1: /* lxvh8x */
8995080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvh8x, regs);
9005080332cSMichael Neuling 		break;
9015080332cSMichael Neuling 	case 2: /* lxvd2x */
9025080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvd2x, regs);
9035080332cSMichael Neuling 		break;
9045080332cSMichael Neuling 	case 3: /* lxvb16x */
9055080332cSMichael Neuling 		PPC_WARN_EMULATED(lxvb16x, regs);
9065080332cSMichael Neuling 		break;
9075080332cSMichael Neuling 	}
9085080332cSMichael Neuling 
9095080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__
9105080332cSMichael Neuling 	/*
9115080332cSMichael Neuling 	 * An LE kernel stores the vector in the task struct as an LE
9125080332cSMichael Neuling 	 * byte array (effectively swapping both the components and
9135080332cSMichael Neuling 	 * the content of the components). Those instructions expect
9145080332cSMichael Neuling 	 * the components to remain in ascending address order, so we
9155080332cSMichael Neuling 	 * swap them back.
9165080332cSMichael Neuling 	 *
9175080332cSMichael Neuling 	 * If we are running a BE user space, the expectation is that
9185080332cSMichael Neuling 	 * of a simple memcpy, so forcing the emulation to look like
9195080332cSMichael Neuling 	 * a lxvb16x should do the trick.
9205080332cSMichael Neuling 	 */
9215080332cSMichael Neuling 	if (swap)
9225080332cSMichael Neuling 		sel = 3;
9235080332cSMichael Neuling 
9245080332cSMichael Neuling 	switch (sel) {
9255080332cSMichael Neuling 	case 0:	/* lxvw4x */
9265080332cSMichael Neuling 		for (i = 0; i < 4; i++)
9275080332cSMichael Neuling 			((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i];
9285080332cSMichael Neuling 		break;
9295080332cSMichael Neuling 	case 1: /* lxvh8x */
9305080332cSMichael Neuling 		for (i = 0; i < 8; i++)
9315080332cSMichael Neuling 			((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i];
9325080332cSMichael Neuling 		break;
9335080332cSMichael Neuling 	case 2: /* lxvd2x */
9345080332cSMichael Neuling 		for (i = 0; i < 2; i++)
9355080332cSMichael Neuling 			((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i];
9365080332cSMichael Neuling 		break;
9375080332cSMichael Neuling 	case 3: /* lxvb16x */
9385080332cSMichael Neuling 		for (i = 0; i < 16; i++)
9395080332cSMichael Neuling 			vdst[i] = vbuf[15-i];
9405080332cSMichael Neuling 		break;
9415080332cSMichael Neuling 	}
9425080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */
9435080332cSMichael Neuling 	/* On a big endian kernel, a BE userspace only needs a memcpy */
9445080332cSMichael Neuling 	if (!swap)
9455080332cSMichael Neuling 		sel = 3;
9465080332cSMichael Neuling 
9475080332cSMichael Neuling 	/* Otherwise, we need to swap the content of the components */
9485080332cSMichael Neuling 	switch (sel) {
9495080332cSMichael Neuling 	case 0:	/* lxvw4x */
9505080332cSMichael Neuling 		for (i = 0; i < 4; i++)
9515080332cSMichael Neuling 			((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]);
9525080332cSMichael Neuling 		break;
9535080332cSMichael Neuling 	case 1: /* lxvh8x */
9545080332cSMichael Neuling 		for (i = 0; i < 8; i++)
9555080332cSMichael Neuling 			((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]);
9565080332cSMichael Neuling 		break;
9575080332cSMichael Neuling 	case 2: /* lxvd2x */
9585080332cSMichael Neuling 		for (i = 0; i < 2; i++)
9595080332cSMichael Neuling 			((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]);
9605080332cSMichael Neuling 		break;
9615080332cSMichael Neuling 	case 3: /* lxvb16x */
9625080332cSMichael Neuling 		memcpy(vdst, vbuf, 16);
9635080332cSMichael Neuling 		break;
9645080332cSMichael Neuling 	}
9655080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */
9665080332cSMichael Neuling 
9675080332cSMichael Neuling 	/* Go to next instruction */
9685080332cSMichael Neuling 	regs->nip += 4;
9695080332cSMichael Neuling }
9705080332cSMichael Neuling #endif /* CONFIG_VSX */
9715080332cSMichael Neuling 
9720869b6fdSMahesh Salgaonkar void handle_hmi_exception(struct pt_regs *regs)
9730869b6fdSMahesh Salgaonkar {
9740869b6fdSMahesh Salgaonkar 	struct pt_regs *old_regs;
9750869b6fdSMahesh Salgaonkar 
9760869b6fdSMahesh Salgaonkar 	old_regs = set_irq_regs(regs);
9770869b6fdSMahesh Salgaonkar 	irq_enter();
9780869b6fdSMahesh Salgaonkar 
9795080332cSMichael Neuling #ifdef CONFIG_VSX
9805080332cSMichael Neuling 	/* Real mode flagged P9 special emu is needed */
9815080332cSMichael Neuling 	if (local_paca->hmi_p9_special_emu) {
9825080332cSMichael Neuling 		local_paca->hmi_p9_special_emu = 0;
9835080332cSMichael Neuling 
9845080332cSMichael Neuling 		/*
9855080332cSMichael Neuling 		 * We don't want to take page faults while doing the
9865080332cSMichael Neuling 		 * emulation, we just replay the instruction if necessary.
9875080332cSMichael Neuling 		 */
9885080332cSMichael Neuling 		pagefault_disable();
9895080332cSMichael Neuling 		p9_hmi_special_emu(regs);
9905080332cSMichael Neuling 		pagefault_enable();
9915080332cSMichael Neuling 	}
9925080332cSMichael Neuling #endif /* CONFIG_VSX */
9935080332cSMichael Neuling 
9940869b6fdSMahesh Salgaonkar 	if (ppc_md.handle_hmi_exception)
9950869b6fdSMahesh Salgaonkar 		ppc_md.handle_hmi_exception(regs);
9960869b6fdSMahesh Salgaonkar 
9970869b6fdSMahesh Salgaonkar 	irq_exit();
9980869b6fdSMahesh Salgaonkar 	set_irq_regs(old_regs);
9990869b6fdSMahesh Salgaonkar }
10000869b6fdSMahesh Salgaonkar 
1001dc1c1ca3SStephen Rothwell void unknown_exception(struct pt_regs *regs)
100214cf11afSPaul Mackerras {
1003ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1004ba12eedeSLi Zhong 
100514cf11afSPaul Mackerras 	printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
100614cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap);
100714cf11afSPaul Mackerras 
1008e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
1009ba12eedeSLi Zhong 
1010ba12eedeSLi Zhong 	exception_exit(prev_state);
101114cf11afSPaul Mackerras }
101214cf11afSPaul Mackerras 
1013dc1c1ca3SStephen Rothwell void instruction_breakpoint_exception(struct pt_regs *regs)
101414cf11afSPaul Mackerras {
1015ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1016ba12eedeSLi Zhong 
101714cf11afSPaul Mackerras 	if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
101814cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1019ba12eedeSLi Zhong 		goto bail;
102014cf11afSPaul Mackerras 	if (debugger_iabr_match(regs))
1021ba12eedeSLi Zhong 		goto bail;
102214cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1023ba12eedeSLi Zhong 
1024ba12eedeSLi Zhong bail:
1025ba12eedeSLi Zhong 	exception_exit(prev_state);
102614cf11afSPaul Mackerras }
102714cf11afSPaul Mackerras 
102814cf11afSPaul Mackerras void RunModeException(struct pt_regs *regs)
102914cf11afSPaul Mackerras {
1030e821fa42SEric W. Biederman 	_exception(SIGTRAP, regs, TRAP_UNK, 0);
103114cf11afSPaul Mackerras }
103214cf11afSPaul Mackerras 
103303465f89SNicholas Piggin void single_step_exception(struct pt_regs *regs)
103414cf11afSPaul Mackerras {
1035ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1036ba12eedeSLi Zhong 
10372538c2d0SK.Prasad 	clear_single_step(regs);
10380e524e76SMatt Evans 	clear_br_trace(regs);
103914cf11afSPaul Mackerras 
10406cc89badSNaveen N. Rao 	if (kprobe_post_handler(regs))
10416cc89badSNaveen N. Rao 		return;
10426cc89badSNaveen N. Rao 
104314cf11afSPaul Mackerras 	if (notify_die(DIE_SSTEP, "single_step", regs, 5,
104414cf11afSPaul Mackerras 					5, SIGTRAP) == NOTIFY_STOP)
1045ba12eedeSLi Zhong 		goto bail;
104614cf11afSPaul Mackerras 	if (debugger_sstep(regs))
1047ba12eedeSLi Zhong 		goto bail;
104814cf11afSPaul Mackerras 
104914cf11afSPaul Mackerras 	_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1050ba12eedeSLi Zhong 
1051ba12eedeSLi Zhong bail:
1052ba12eedeSLi Zhong 	exception_exit(prev_state);
105314cf11afSPaul Mackerras }
105403465f89SNicholas Piggin NOKPROBE_SYMBOL(single_step_exception);
105514cf11afSPaul Mackerras 
105614cf11afSPaul Mackerras /*
105714cf11afSPaul Mackerras  * After we have successfully emulated an instruction, we have to
105814cf11afSPaul Mackerras  * check if the instruction was being single-stepped, and if so,
105914cf11afSPaul Mackerras  * pretend we got a single-step exception.  This was pointed out
106014cf11afSPaul Mackerras  * by Kumar Gala.  -- paulus
106114cf11afSPaul Mackerras  */
10628dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs)
106314cf11afSPaul Mackerras {
10642538c2d0SK.Prasad 	if (single_stepping(regs))
10652538c2d0SK.Prasad 		single_step_exception(regs);
106614cf11afSPaul Mackerras }
106714cf11afSPaul Mackerras 
10685fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr)
1069dc1c1ca3SStephen Rothwell {
1070aeb1c0f6SEric W. Biederman 	int ret = FPE_FLTUNK;
1071dc1c1ca3SStephen Rothwell 
1072dc1c1ca3SStephen Rothwell 	/* Invalid operation */
1073dc1c1ca3SStephen Rothwell 	if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
10745fad293bSKumar Gala 		ret = FPE_FLTINV;
1075dc1c1ca3SStephen Rothwell 
1076dc1c1ca3SStephen Rothwell 	/* Overflow */
1077dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
10785fad293bSKumar Gala 		ret = FPE_FLTOVF;
1079dc1c1ca3SStephen Rothwell 
1080dc1c1ca3SStephen Rothwell 	/* Underflow */
1081dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
10825fad293bSKumar Gala 		ret = FPE_FLTUND;
1083dc1c1ca3SStephen Rothwell 
1084dc1c1ca3SStephen Rothwell 	/* Divide by zero */
1085dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
10865fad293bSKumar Gala 		ret = FPE_FLTDIV;
1087dc1c1ca3SStephen Rothwell 
1088dc1c1ca3SStephen Rothwell 	/* Inexact result */
1089dc1c1ca3SStephen Rothwell 	else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
10905fad293bSKumar Gala 		ret = FPE_FLTRES;
10915fad293bSKumar Gala 
10925fad293bSKumar Gala 	return ret;
10935fad293bSKumar Gala }
10945fad293bSKumar Gala 
10955fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs)
10965fad293bSKumar Gala {
10975fad293bSKumar Gala 	int code = 0;
10985fad293bSKumar Gala 
10995fad293bSKumar Gala 	flush_fp_to_thread(current);
11005fad293bSKumar Gala 
1101de79f7b9SPaul Mackerras 	code = __parse_fpscr(current->thread.fp_state.fpscr);
1102dc1c1ca3SStephen Rothwell 
1103dc1c1ca3SStephen Rothwell 	_exception(SIGFPE, regs, code, regs->nip);
1104dc1c1ca3SStephen Rothwell }
1105dc1c1ca3SStephen Rothwell 
1106dc1c1ca3SStephen Rothwell /*
1107dc1c1ca3SStephen Rothwell  * Illegal instruction emulation support.  Originally written to
110814cf11afSPaul Mackerras  * provide the PVR to user applications using the mfspr rd, PVR.
110914cf11afSPaul Mackerras  * Return non-zero if we can't emulate, or -EFAULT if the associated
111014cf11afSPaul Mackerras  * memory access caused an access fault.  Return zero on success.
111114cf11afSPaul Mackerras  *
111214cf11afSPaul Mackerras  * There are a couple of ways to do this, either "decode" the instruction
111314cf11afSPaul Mackerras  * or directly match lots of bits.  In this case, matching lots of
111414cf11afSPaul Mackerras  * bits is faster and easier.
111586417780SPaul Mackerras  *
111614cf11afSPaul Mackerras  */
111714cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword)
111814cf11afSPaul Mackerras {
111914cf11afSPaul Mackerras 	u8 rT = (instword >> 21) & 0x1f;
112014cf11afSPaul Mackerras 	u8 rA = (instword >> 16) & 0x1f;
112114cf11afSPaul Mackerras 	u8 NB_RB = (instword >> 11) & 0x1f;
112214cf11afSPaul Mackerras 	u32 num_bytes;
112314cf11afSPaul Mackerras 	unsigned long EA;
112414cf11afSPaul Mackerras 	int pos = 0;
112514cf11afSPaul Mackerras 
112614cf11afSPaul Mackerras 	/* Early out if we are an invalid form of lswx */
112716c57b36SKumar Gala 	if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
112814cf11afSPaul Mackerras 		if ((rT == rA) || (rT == NB_RB))
112914cf11afSPaul Mackerras 			return -EINVAL;
113014cf11afSPaul Mackerras 
113114cf11afSPaul Mackerras 	EA = (rA == 0) ? 0 : regs->gpr[rA];
113214cf11afSPaul Mackerras 
113316c57b36SKumar Gala 	switch (instword & PPC_INST_STRING_MASK) {
113416c57b36SKumar Gala 		case PPC_INST_LSWX:
113516c57b36SKumar Gala 		case PPC_INST_STSWX:
113614cf11afSPaul Mackerras 			EA += NB_RB;
113714cf11afSPaul Mackerras 			num_bytes = regs->xer & 0x7f;
113814cf11afSPaul Mackerras 			break;
113916c57b36SKumar Gala 		case PPC_INST_LSWI:
114016c57b36SKumar Gala 		case PPC_INST_STSWI:
114114cf11afSPaul Mackerras 			num_bytes = (NB_RB == 0) ? 32 : NB_RB;
114214cf11afSPaul Mackerras 			break;
114314cf11afSPaul Mackerras 		default:
114414cf11afSPaul Mackerras 			return -EINVAL;
114514cf11afSPaul Mackerras 	}
114614cf11afSPaul Mackerras 
114714cf11afSPaul Mackerras 	while (num_bytes != 0)
114814cf11afSPaul Mackerras 	{
114914cf11afSPaul Mackerras 		u8 val;
115014cf11afSPaul Mackerras 		u32 shift = 8 * (3 - (pos & 0x3));
115114cf11afSPaul Mackerras 
115280aa0fb4SJames Yang 		/* if process is 32-bit, clear upper 32 bits of EA */
115380aa0fb4SJames Yang 		if ((regs->msr & MSR_64BIT) == 0)
115480aa0fb4SJames Yang 			EA &= 0xFFFFFFFF;
115580aa0fb4SJames Yang 
115616c57b36SKumar Gala 		switch ((instword & PPC_INST_STRING_MASK)) {
115716c57b36SKumar Gala 			case PPC_INST_LSWX:
115816c57b36SKumar Gala 			case PPC_INST_LSWI:
115914cf11afSPaul Mackerras 				if (get_user(val, (u8 __user *)EA))
116014cf11afSPaul Mackerras 					return -EFAULT;
116114cf11afSPaul Mackerras 				/* first time updating this reg,
116214cf11afSPaul Mackerras 				 * zero it out */
116314cf11afSPaul Mackerras 				if (pos == 0)
116414cf11afSPaul Mackerras 					regs->gpr[rT] = 0;
116514cf11afSPaul Mackerras 				regs->gpr[rT] |= val << shift;
116614cf11afSPaul Mackerras 				break;
116716c57b36SKumar Gala 			case PPC_INST_STSWI:
116816c57b36SKumar Gala 			case PPC_INST_STSWX:
116914cf11afSPaul Mackerras 				val = regs->gpr[rT] >> shift;
117014cf11afSPaul Mackerras 				if (put_user(val, (u8 __user *)EA))
117114cf11afSPaul Mackerras 					return -EFAULT;
117214cf11afSPaul Mackerras 				break;
117314cf11afSPaul Mackerras 		}
117414cf11afSPaul Mackerras 		/* move EA to next address */
117514cf11afSPaul Mackerras 		EA += 1;
117614cf11afSPaul Mackerras 		num_bytes--;
117714cf11afSPaul Mackerras 
117814cf11afSPaul Mackerras 		/* manage our position within the register */
117914cf11afSPaul Mackerras 		if (++pos == 4) {
118014cf11afSPaul Mackerras 			pos = 0;
118114cf11afSPaul Mackerras 			if (++rT == 32)
118214cf11afSPaul Mackerras 				rT = 0;
118314cf11afSPaul Mackerras 		}
118414cf11afSPaul Mackerras 	}
118514cf11afSPaul Mackerras 
118614cf11afSPaul Mackerras 	return 0;
118714cf11afSPaul Mackerras }
118814cf11afSPaul Mackerras 
1189c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
1190c3412dcbSWill Schmidt {
1191c3412dcbSWill Schmidt 	u32 ra,rs;
1192c3412dcbSWill Schmidt 	unsigned long tmp;
1193c3412dcbSWill Schmidt 
1194c3412dcbSWill Schmidt 	ra = (instword >> 16) & 0x1f;
1195c3412dcbSWill Schmidt 	rs = (instword >> 21) & 0x1f;
1196c3412dcbSWill Schmidt 
1197c3412dcbSWill Schmidt 	tmp = regs->gpr[rs];
1198c3412dcbSWill Schmidt 	tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
1199c3412dcbSWill Schmidt 	tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
1200c3412dcbSWill Schmidt 	tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
1201c3412dcbSWill Schmidt 	regs->gpr[ra] = tmp;
1202c3412dcbSWill Schmidt 
1203c3412dcbSWill Schmidt 	return 0;
1204c3412dcbSWill Schmidt }
1205c3412dcbSWill Schmidt 
1206c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword)
1207c1469f13SKumar Gala {
1208c1469f13SKumar Gala 	u8 rT = (instword >> 21) & 0x1f;
1209c1469f13SKumar Gala 	u8 rA = (instword >> 16) & 0x1f;
1210c1469f13SKumar Gala 	u8 rB = (instword >> 11) & 0x1f;
1211c1469f13SKumar Gala 	u8 BC = (instword >> 6) & 0x1f;
1212c1469f13SKumar Gala 	u8 bit;
1213c1469f13SKumar Gala 	unsigned long tmp;
1214c1469f13SKumar Gala 
1215c1469f13SKumar Gala 	tmp = (rA == 0) ? 0 : regs->gpr[rA];
1216c1469f13SKumar Gala 	bit = (regs->ccr >> (31 - BC)) & 0x1;
1217c1469f13SKumar Gala 
1218c1469f13SKumar Gala 	regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1219c1469f13SKumar Gala 
1220c1469f13SKumar Gala 	return 0;
1221c1469f13SKumar Gala }
1222c1469f13SKumar Gala 
12236ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
12246ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause)
12256ce6c629SMichael Neuling {
12266ce6c629SMichael Neuling         /* If we're emulating a load/store in an active transaction, we cannot
12276ce6c629SMichael Neuling          * emulate it as the kernel operates in transaction suspended context.
12286ce6c629SMichael Neuling          * We need to abort the transaction.  This creates a persistent TM
12296ce6c629SMichael Neuling          * abort so tell the user what caused it with a new code.
12306ce6c629SMichael Neuling 	 */
12316ce6c629SMichael Neuling 	if (MSR_TM_TRANSACTIONAL(regs->msr)) {
12326ce6c629SMichael Neuling 		tm_enable();
12336ce6c629SMichael Neuling 		tm_abort(cause);
12346ce6c629SMichael Neuling 		return true;
12356ce6c629SMichael Neuling 	}
12366ce6c629SMichael Neuling 	return false;
12376ce6c629SMichael Neuling }
12386ce6c629SMichael Neuling #else
12396ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason)
12406ce6c629SMichael Neuling {
12416ce6c629SMichael Neuling 	return false;
12426ce6c629SMichael Neuling }
12436ce6c629SMichael Neuling #endif
12446ce6c629SMichael Neuling 
124514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs)
124614cf11afSPaul Mackerras {
124714cf11afSPaul Mackerras 	u32 instword;
124814cf11afSPaul Mackerras 	u32 rd;
124914cf11afSPaul Mackerras 
12504288e343SAnton Blanchard 	if (!user_mode(regs))
125114cf11afSPaul Mackerras 		return -EINVAL;
125214cf11afSPaul Mackerras 	CHECK_FULL_REGS(regs);
125314cf11afSPaul Mackerras 
125414cf11afSPaul Mackerras 	if (get_user(instword, (u32 __user *)(regs->nip)))
125514cf11afSPaul Mackerras 		return -EFAULT;
125614cf11afSPaul Mackerras 
125714cf11afSPaul Mackerras 	/* Emulate the mfspr rD, PVR. */
125816c57b36SKumar Gala 	if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1259eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mfpvr, regs);
126014cf11afSPaul Mackerras 		rd = (instword >> 21) & 0x1f;
126114cf11afSPaul Mackerras 		regs->gpr[rd] = mfspr(SPRN_PVR);
126214cf11afSPaul Mackerras 		return 0;
126314cf11afSPaul Mackerras 	}
126414cf11afSPaul Mackerras 
126514cf11afSPaul Mackerras 	/* Emulating the dcba insn is just a no-op.  */
126680947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1267eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(dcba, regs);
126814cf11afSPaul Mackerras 		return 0;
126980947e7cSGeert Uytterhoeven 	}
127014cf11afSPaul Mackerras 
127114cf11afSPaul Mackerras 	/* Emulate the mcrxr insn.  */
127216c57b36SKumar Gala 	if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
127386417780SPaul Mackerras 		int shift = (instword >> 21) & 0x1c;
127414cf11afSPaul Mackerras 		unsigned long msk = 0xf0000000UL >> shift;
127514cf11afSPaul Mackerras 
1276eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(mcrxr, regs);
127714cf11afSPaul Mackerras 		regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
127814cf11afSPaul Mackerras 		regs->xer &= ~0xf0000000UL;
127914cf11afSPaul Mackerras 		return 0;
128014cf11afSPaul Mackerras 	}
128114cf11afSPaul Mackerras 
128214cf11afSPaul Mackerras 	/* Emulate load/store string insn. */
128380947e7cSGeert Uytterhoeven 	if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
12846ce6c629SMichael Neuling 		if (tm_abort_check(regs,
12856ce6c629SMichael Neuling 				   TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
12866ce6c629SMichael Neuling 			return -EINVAL;
1287eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(string, regs);
128814cf11afSPaul Mackerras 		return emulate_string_inst(regs, instword);
128980947e7cSGeert Uytterhoeven 	}
129014cf11afSPaul Mackerras 
1291c3412dcbSWill Schmidt 	/* Emulate the popcntb (Population Count Bytes) instruction. */
129216c57b36SKumar Gala 	if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1293eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(popcntb, regs);
1294c3412dcbSWill Schmidt 		return emulate_popcntb_inst(regs, instword);
1295c3412dcbSWill Schmidt 	}
1296c3412dcbSWill Schmidt 
1297c1469f13SKumar Gala 	/* Emulate isel (Integer Select) instruction */
129816c57b36SKumar Gala 	if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1299eecff81dSAnton Blanchard 		PPC_WARN_EMULATED(isel, regs);
1300c1469f13SKumar Gala 		return emulate_isel(regs, instword);
1301c1469f13SKumar Gala 	}
1302c1469f13SKumar Gala 
13039863c28aSJames Yang 	/* Emulate sync instruction variants */
13049863c28aSJames Yang 	if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
13059863c28aSJames Yang 		PPC_WARN_EMULATED(sync, regs);
13069863c28aSJames Yang 		asm volatile("sync");
13079863c28aSJames Yang 		return 0;
13089863c28aSJames Yang 	}
13099863c28aSJames Yang 
1310efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
1311efcac658SAlexey Kardashevskiy 	/* Emulate the mfspr rD, DSCR. */
131273d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
131373d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR_USER) ||
131473d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
131573d2fb75SAnton Blanchard 		PPC_INST_MFSPR_DSCR)) &&
1316efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1317efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mfdscr, regs);
1318efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
1319efcac658SAlexey Kardashevskiy 		regs->gpr[rd] = mfspr(SPRN_DSCR);
1320efcac658SAlexey Kardashevskiy 		return 0;
1321efcac658SAlexey Kardashevskiy 	}
1322efcac658SAlexey Kardashevskiy 	/* Emulate the mtspr DSCR, rD. */
132373d2fb75SAnton Blanchard 	if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
132473d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR_USER) ||
132573d2fb75SAnton Blanchard 	     ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
132673d2fb75SAnton Blanchard 		PPC_INST_MTSPR_DSCR)) &&
1327efcac658SAlexey Kardashevskiy 			cpu_has_feature(CPU_FTR_DSCR)) {
1328efcac658SAlexey Kardashevskiy 		PPC_WARN_EMULATED(mtdscr, regs);
1329efcac658SAlexey Kardashevskiy 		rd = (instword >> 21) & 0x1f;
133000ca0de0SAnton Blanchard 		current->thread.dscr = regs->gpr[rd];
1331efcac658SAlexey Kardashevskiy 		current->thread.dscr_inherit = 1;
133200ca0de0SAnton Blanchard 		mtspr(SPRN_DSCR, current->thread.dscr);
1333efcac658SAlexey Kardashevskiy 		return 0;
1334efcac658SAlexey Kardashevskiy 	}
1335efcac658SAlexey Kardashevskiy #endif
1336efcac658SAlexey Kardashevskiy 
133714cf11afSPaul Mackerras 	return -EINVAL;
133814cf11afSPaul Mackerras }
133914cf11afSPaul Mackerras 
134073c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr)
134114cf11afSPaul Mackerras {
134273c9ceabSJeremy Fitzhardinge 	return is_kernel_addr(addr);
134314cf11afSPaul Mackerras }
134414cf11afSPaul Mackerras 
13453a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION
13463a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs)
13473a3b5aa6SKevin Hao {
13483a3b5aa6SKevin Hao 	int ret;
13493a3b5aa6SKevin Hao 	extern int do_mathemu(struct pt_regs *regs);
13503a3b5aa6SKevin Hao 
13513a3b5aa6SKevin Hao 	ret = do_mathemu(regs);
13523a3b5aa6SKevin Hao 	if (ret >= 0)
13533a3b5aa6SKevin Hao 		PPC_WARN_EMULATED(math, regs);
13543a3b5aa6SKevin Hao 
13553a3b5aa6SKevin Hao 	switch (ret) {
13563a3b5aa6SKevin Hao 	case 0:
13573a3b5aa6SKevin Hao 		emulate_single_step(regs);
13583a3b5aa6SKevin Hao 		return 0;
13593a3b5aa6SKevin Hao 	case 1: {
13603a3b5aa6SKevin Hao 			int code = 0;
1361de79f7b9SPaul Mackerras 			code = __parse_fpscr(current->thread.fp_state.fpscr);
13623a3b5aa6SKevin Hao 			_exception(SIGFPE, regs, code, regs->nip);
13633a3b5aa6SKevin Hao 			return 0;
13643a3b5aa6SKevin Hao 		}
13653a3b5aa6SKevin Hao 	case -EFAULT:
13663a3b5aa6SKevin Hao 		_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
13673a3b5aa6SKevin Hao 		return 0;
13683a3b5aa6SKevin Hao 	}
13693a3b5aa6SKevin Hao 
13703a3b5aa6SKevin Hao 	return -1;
13713a3b5aa6SKevin Hao }
13723a3b5aa6SKevin Hao #else
13733a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; }
13743a3b5aa6SKevin Hao #endif
13753a3b5aa6SKevin Hao 
137603465f89SNicholas Piggin void program_check_exception(struct pt_regs *regs)
137714cf11afSPaul Mackerras {
1378ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
137914cf11afSPaul Mackerras 	unsigned int reason = get_reason(regs);
138014cf11afSPaul Mackerras 
1381aa42c69cSKim Phillips 	/* We can now get here via a FP Unavailable exception if the core
138204903a30SKumar Gala 	 * has no FPU, in that case the reason flags will be 0 */
138314cf11afSPaul Mackerras 
138414cf11afSPaul Mackerras 	if (reason & REASON_FP) {
138514cf11afSPaul Mackerras 		/* IEEE FP exception */
1386dc1c1ca3SStephen Rothwell 		parse_fpe(regs);
1387ba12eedeSLi Zhong 		goto bail;
13888dad3f92SPaul Mackerras 	}
13898dad3f92SPaul Mackerras 	if (reason & REASON_TRAP) {
1390a4c3f909SBalbir Singh 		unsigned long bugaddr;
1391ba797b28SJason Wessel 		/* Debugger is first in line to stop recursive faults in
1392ba797b28SJason Wessel 		 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1393ba797b28SJason Wessel 		if (debugger_bpt(regs))
1394ba12eedeSLi Zhong 			goto bail;
1395ba797b28SJason Wessel 
13966cc89badSNaveen N. Rao 		if (kprobe_handler(regs))
13976cc89badSNaveen N. Rao 			goto bail;
13986cc89badSNaveen N. Rao 
139914cf11afSPaul Mackerras 		/* trap exception */
1400dc1c1ca3SStephen Rothwell 		if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1401dc1c1ca3SStephen Rothwell 				== NOTIFY_STOP)
1402ba12eedeSLi Zhong 			goto bail;
140373c9ceabSJeremy Fitzhardinge 
1404a4c3f909SBalbir Singh 		bugaddr = regs->nip;
1405a4c3f909SBalbir Singh 		/*
1406a4c3f909SBalbir Singh 		 * Fixup bugaddr for BUG_ON() in real mode
1407a4c3f909SBalbir Singh 		 */
1408a4c3f909SBalbir Singh 		if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1409a4c3f909SBalbir Singh 			bugaddr += PAGE_OFFSET;
1410a4c3f909SBalbir Singh 
141173c9ceabSJeremy Fitzhardinge 		if (!(regs->msr & MSR_PR) &&  /* not user-mode */
1412a4c3f909SBalbir Singh 		    report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
141314cf11afSPaul Mackerras 			regs->nip += 4;
1414ba12eedeSLi Zhong 			goto bail;
141514cf11afSPaul Mackerras 		}
14168dad3f92SPaul Mackerras 		_exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1417ba12eedeSLi Zhong 		goto bail;
14188dad3f92SPaul Mackerras 	}
1419bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1420bc2a9408SMichael Neuling 	if (reason & REASON_TM) {
1421bc2a9408SMichael Neuling 		/* This is a TM "Bad Thing Exception" program check.
1422bc2a9408SMichael Neuling 		 * This occurs when:
1423bc2a9408SMichael Neuling 		 * -  An rfid/hrfid/mtmsrd attempts to cause an illegal
1424bc2a9408SMichael Neuling 		 *    transition in TM states.
1425bc2a9408SMichael Neuling 		 * -  A trechkpt is attempted when transactional.
1426bc2a9408SMichael Neuling 		 * -  A treclaim is attempted when non transactional.
1427bc2a9408SMichael Neuling 		 * -  A tend is illegally attempted.
1428bc2a9408SMichael Neuling 		 * -  writing a TM SPR when transactional.
1429632f0574SMichael Ellerman 		 *
1430632f0574SMichael Ellerman 		 * If usermode caused this, it's done something illegal and
1431bc2a9408SMichael Neuling 		 * gets a SIGILL slap on the wrist.  We call it an illegal
1432bc2a9408SMichael Neuling 		 * operand to distinguish from the instruction just being bad
1433bc2a9408SMichael Neuling 		 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1434bc2a9408SMichael Neuling 		 * illegal /placement/ of a valid instruction.
1435bc2a9408SMichael Neuling 		 */
1436bc2a9408SMichael Neuling 		if (user_mode(regs)) {
1437bc2a9408SMichael Neuling 			_exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1438ba12eedeSLi Zhong 			goto bail;
1439bc2a9408SMichael Neuling 		} else {
1440bc2a9408SMichael Neuling 			printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1441bc2a9408SMichael Neuling 			       "at %lx (msr 0x%x)\n", regs->nip, reason);
1442bc2a9408SMichael Neuling 			die("Unrecoverable exception", regs, SIGABRT);
1443bc2a9408SMichael Neuling 		}
1444bc2a9408SMichael Neuling 	}
1445bc2a9408SMichael Neuling #endif
14468dad3f92SPaul Mackerras 
1447b3f6a459SMichael Ellerman 	/*
1448b3f6a459SMichael Ellerman 	 * If we took the program check in the kernel skip down to sending a
1449b3f6a459SMichael Ellerman 	 * SIGILL. The subsequent cases all relate to emulating instructions
1450b3f6a459SMichael Ellerman 	 * which we should only do for userspace. We also do not want to enable
1451b3f6a459SMichael Ellerman 	 * interrupts for kernel faults because that might lead to further
1452b3f6a459SMichael Ellerman 	 * faults, and loose the context of the original exception.
1453b3f6a459SMichael Ellerman 	 */
1454b3f6a459SMichael Ellerman 	if (!user_mode(regs))
1455b3f6a459SMichael Ellerman 		goto sigill;
1456b3f6a459SMichael Ellerman 
1457a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1458a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1459cd8a5673SPaul Mackerras 		local_irq_enable();
1460cd8a5673SPaul Mackerras 
146104903a30SKumar Gala 	/* (reason & REASON_ILLEGAL) would be the obvious thing here,
146204903a30SKumar Gala 	 * but there seems to be a hardware bug on the 405GP (RevD)
146304903a30SKumar Gala 	 * that means ESR is sometimes set incorrectly - either to
146404903a30SKumar Gala 	 * ESR_DST (!?) or 0.  In the process of chasing this with the
146504903a30SKumar Gala 	 * hardware people - not sure if it can happen on any illegal
146604903a30SKumar Gala 	 * instruction or only on FP instructions, whether there is a
14674e63f8edSBenjamin Herrenschmidt 	 * pattern to occurrences etc. -dgibson 31/Mar/2003
14684e63f8edSBenjamin Herrenschmidt 	 */
14693a3b5aa6SKevin Hao 	if (!emulate_math(regs))
1470ba12eedeSLi Zhong 		goto bail;
147104903a30SKumar Gala 
14728dad3f92SPaul Mackerras 	/* Try to emulate it if we should. */
14738dad3f92SPaul Mackerras 	if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
147414cf11afSPaul Mackerras 		switch (emulate_instruction(regs)) {
147514cf11afSPaul Mackerras 		case 0:
147614cf11afSPaul Mackerras 			regs->nip += 4;
147714cf11afSPaul Mackerras 			emulate_single_step(regs);
1478ba12eedeSLi Zhong 			goto bail;
147914cf11afSPaul Mackerras 		case -EFAULT:
148014cf11afSPaul Mackerras 			_exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1481ba12eedeSLi Zhong 			goto bail;
14828dad3f92SPaul Mackerras 		}
14838dad3f92SPaul Mackerras 	}
14848dad3f92SPaul Mackerras 
1485b3f6a459SMichael Ellerman sigill:
148614cf11afSPaul Mackerras 	if (reason & REASON_PRIVILEGED)
148714cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
148814cf11afSPaul Mackerras 	else
148914cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1490ba12eedeSLi Zhong 
1491ba12eedeSLi Zhong bail:
1492ba12eedeSLi Zhong 	exception_exit(prev_state);
149314cf11afSPaul Mackerras }
149403465f89SNicholas Piggin NOKPROBE_SYMBOL(program_check_exception);
149514cf11afSPaul Mackerras 
1496bf593907SPaul Mackerras /*
1497bf593907SPaul Mackerras  * This occurs when running in hypervisor mode on POWER6 or later
1498bf593907SPaul Mackerras  * and an illegal instruction is encountered.
1499bf593907SPaul Mackerras  */
150003465f89SNicholas Piggin void emulation_assist_interrupt(struct pt_regs *regs)
1501bf593907SPaul Mackerras {
1502bf593907SPaul Mackerras 	regs->msr |= REASON_ILLEGAL;
1503bf593907SPaul Mackerras 	program_check_exception(regs);
1504bf593907SPaul Mackerras }
150503465f89SNicholas Piggin NOKPROBE_SYMBOL(emulation_assist_interrupt);
1506bf593907SPaul Mackerras 
1507dc1c1ca3SStephen Rothwell void alignment_exception(struct pt_regs *regs)
150814cf11afSPaul Mackerras {
1509ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
15104393c4f6SBenjamin Herrenschmidt 	int sig, code, fixed = 0;
151114cf11afSPaul Mackerras 
1512a3512b2dSBenjamin Herrenschmidt 	/* We restore the interrupt state now */
1513a3512b2dSBenjamin Herrenschmidt 	if (!arch_irq_disabled_regs(regs))
1514a3512b2dSBenjamin Herrenschmidt 		local_irq_enable();
1515a3512b2dSBenjamin Herrenschmidt 
15166ce6c629SMichael Neuling 	if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
15176ce6c629SMichael Neuling 		goto bail;
15186ce6c629SMichael Neuling 
1519e9370ae1SPaul Mackerras 	/* we don't implement logging of alignment exceptions */
1520e9370ae1SPaul Mackerras 	if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
152114cf11afSPaul Mackerras 		fixed = fix_alignment(regs);
152214cf11afSPaul Mackerras 
152314cf11afSPaul Mackerras 	if (fixed == 1) {
152414cf11afSPaul Mackerras 		regs->nip += 4;	/* skip over emulated instruction */
152514cf11afSPaul Mackerras 		emulate_single_step(regs);
1526ba12eedeSLi Zhong 		goto bail;
152714cf11afSPaul Mackerras 	}
152814cf11afSPaul Mackerras 
152914cf11afSPaul Mackerras 	/* Operand address was bad */
153014cf11afSPaul Mackerras 	if (fixed == -EFAULT) {
15314393c4f6SBenjamin Herrenschmidt 		sig = SIGSEGV;
15324393c4f6SBenjamin Herrenschmidt 		code = SEGV_ACCERR;
15334393c4f6SBenjamin Herrenschmidt 	} else {
15344393c4f6SBenjamin Herrenschmidt 		sig = SIGBUS;
15354393c4f6SBenjamin Herrenschmidt 		code = BUS_ADRALN;
153614cf11afSPaul Mackerras 	}
15374393c4f6SBenjamin Herrenschmidt 	if (user_mode(regs))
15384393c4f6SBenjamin Herrenschmidt 		_exception(sig, regs, code, regs->dar);
15394393c4f6SBenjamin Herrenschmidt 	else
15404393c4f6SBenjamin Herrenschmidt 		bad_page_fault(regs, regs->dar, sig);
1541ba12eedeSLi Zhong 
1542ba12eedeSLi Zhong bail:
1543ba12eedeSLi Zhong 	exception_exit(prev_state);
154414cf11afSPaul Mackerras }
154514cf11afSPaul Mackerras 
154614cf11afSPaul Mackerras void StackOverflow(struct pt_regs *regs)
154714cf11afSPaul Mackerras {
154814cf11afSPaul Mackerras 	printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
154914cf11afSPaul Mackerras 	       current, regs->gpr[1]);
155014cf11afSPaul Mackerras 	debugger(regs);
155114cf11afSPaul Mackerras 	show_regs(regs);
155214cf11afSPaul Mackerras 	panic("kernel stack overflow");
155314cf11afSPaul Mackerras }
155414cf11afSPaul Mackerras 
155514cf11afSPaul Mackerras void nonrecoverable_exception(struct pt_regs *regs)
155614cf11afSPaul Mackerras {
155714cf11afSPaul Mackerras 	printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
155814cf11afSPaul Mackerras 	       regs->nip, regs->msr);
155914cf11afSPaul Mackerras 	debugger(regs);
156014cf11afSPaul Mackerras 	die("nonrecoverable exception", regs, SIGKILL);
156114cf11afSPaul Mackerras }
156214cf11afSPaul Mackerras 
1563dc1c1ca3SStephen Rothwell void kernel_fp_unavailable_exception(struct pt_regs *regs)
1564dc1c1ca3SStephen Rothwell {
1565ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1566ba12eedeSLi Zhong 
1567dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1568dc1c1ca3SStephen Rothwell 			  "%lx at %lx\n", regs->trap, regs->nip);
1569dc1c1ca3SStephen Rothwell 	die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1570ba12eedeSLi Zhong 
1571ba12eedeSLi Zhong 	exception_exit(prev_state);
1572dc1c1ca3SStephen Rothwell }
1573dc1c1ca3SStephen Rothwell 
1574dc1c1ca3SStephen Rothwell void altivec_unavailable_exception(struct pt_regs *regs)
1575dc1c1ca3SStephen Rothwell {
1576ba12eedeSLi Zhong 	enum ctx_state prev_state = exception_enter();
1577ba12eedeSLi Zhong 
1578dc1c1ca3SStephen Rothwell 	if (user_mode(regs)) {
1579dc1c1ca3SStephen Rothwell 		/* A user program has executed an altivec instruction,
1580dc1c1ca3SStephen Rothwell 		   but this kernel doesn't support altivec. */
1581dc1c1ca3SStephen Rothwell 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1582ba12eedeSLi Zhong 		goto bail;
1583dc1c1ca3SStephen Rothwell 	}
15846c4841c2SAnton Blanchard 
1585dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1586dc1c1ca3SStephen Rothwell 			"%lx at %lx\n", regs->trap, regs->nip);
1587dc1c1ca3SStephen Rothwell 	die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1588ba12eedeSLi Zhong 
1589ba12eedeSLi Zhong bail:
1590ba12eedeSLi Zhong 	exception_exit(prev_state);
1591dc1c1ca3SStephen Rothwell }
1592dc1c1ca3SStephen Rothwell 
1593ce48b210SMichael Neuling void vsx_unavailable_exception(struct pt_regs *regs)
1594ce48b210SMichael Neuling {
1595ce48b210SMichael Neuling 	if (user_mode(regs)) {
1596ce48b210SMichael Neuling 		/* A user program has executed an vsx instruction,
1597ce48b210SMichael Neuling 		   but this kernel doesn't support vsx. */
1598ce48b210SMichael Neuling 		_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1599ce48b210SMichael Neuling 		return;
1600ce48b210SMichael Neuling 	}
1601ce48b210SMichael Neuling 
1602ce48b210SMichael Neuling 	printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1603ce48b210SMichael Neuling 			"%lx at %lx\n", regs->trap, regs->nip);
1604ce48b210SMichael Neuling 	die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1605ce48b210SMichael Neuling }
1606ce48b210SMichael Neuling 
16072517617eSMichael Neuling #ifdef CONFIG_PPC64
1608172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs)
1609172f7aaaSCyril Bur {
16105d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
16115d176f75SCyril Bur 	if (user_mode(regs)) {
16125d176f75SCyril Bur 		current->thread.load_tm++;
16135d176f75SCyril Bur 		regs->msr |= MSR_TM;
16145d176f75SCyril Bur 		tm_enable();
16155d176f75SCyril Bur 		tm_restore_sprs(&current->thread);
16165d176f75SCyril Bur 		return;
16175d176f75SCyril Bur 	}
16185d176f75SCyril Bur #endif
1619172f7aaaSCyril Bur 	pr_emerg("Unrecoverable TM Unavailable Exception "
1620172f7aaaSCyril Bur 			"%lx at %lx\n", regs->trap, regs->nip);
1621172f7aaaSCyril Bur 	die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1622172f7aaaSCyril Bur }
1623172f7aaaSCyril Bur 
1624021424a1SMichael Ellerman void facility_unavailable_exception(struct pt_regs *regs)
1625d0c0c9a1SMichael Neuling {
1626021424a1SMichael Ellerman 	static char *facility_strings[] = {
16272517617eSMichael Neuling 		[FSCR_FP_LG] = "FPU",
16282517617eSMichael Neuling 		[FSCR_VECVSX_LG] = "VMX/VSX",
16292517617eSMichael Neuling 		[FSCR_DSCR_LG] = "DSCR",
16302517617eSMichael Neuling 		[FSCR_PM_LG] = "PMU SPRs",
16312517617eSMichael Neuling 		[FSCR_BHRB_LG] = "BHRB",
16322517617eSMichael Neuling 		[FSCR_TM_LG] = "TM",
16332517617eSMichael Neuling 		[FSCR_EBB_LG] = "EBB",
16342517617eSMichael Neuling 		[FSCR_TAR_LG] = "TAR",
1635794464f4SNicholas Piggin 		[FSCR_MSGP_LG] = "MSGP",
16369b7ff0c6SNicholas Piggin 		[FSCR_SCV_LG] = "SCV",
1637021424a1SMichael Ellerman 	};
16382517617eSMichael Neuling 	char *facility = "unknown";
1639021424a1SMichael Ellerman 	u64 value;
1640c952c1c4SAnshuman Khandual 	u32 instword, rd;
16412517617eSMichael Neuling 	u8 status;
16422517617eSMichael Neuling 	bool hv;
1643021424a1SMichael Ellerman 
16442271db20SBenjamin Herrenschmidt 	hv = (TRAP(regs) == 0xf80);
16452517617eSMichael Neuling 	if (hv)
1646b14b6260SMichael Ellerman 		value = mfspr(SPRN_HFSCR);
16472517617eSMichael Neuling 	else
16482517617eSMichael Neuling 		value = mfspr(SPRN_FSCR);
16492517617eSMichael Neuling 
16502517617eSMichael Neuling 	status = value >> 56;
1651709b973cSAnshuman Khandual 	if ((hv || status >= 2) &&
1652709b973cSAnshuman Khandual 	    (status < ARRAY_SIZE(facility_strings)) &&
1653709b973cSAnshuman Khandual 	    facility_strings[status])
1654709b973cSAnshuman Khandual 		facility = facility_strings[status];
1655709b973cSAnshuman Khandual 
1656709b973cSAnshuman Khandual 	/* We should not have taken this interrupt in kernel */
1657709b973cSAnshuman Khandual 	if (!user_mode(regs)) {
1658709b973cSAnshuman Khandual 		pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n",
1659709b973cSAnshuman Khandual 			 facility, status, regs->nip);
1660709b973cSAnshuman Khandual 		die("Unexpected facility unavailable exception", regs, SIGABRT);
1661709b973cSAnshuman Khandual 	}
1662709b973cSAnshuman Khandual 
1663709b973cSAnshuman Khandual 	/* We restore the interrupt state now */
1664709b973cSAnshuman Khandual 	if (!arch_irq_disabled_regs(regs))
1665709b973cSAnshuman Khandual 		local_irq_enable();
1666709b973cSAnshuman Khandual 
16672517617eSMichael Neuling 	if (status == FSCR_DSCR_LG) {
1668c952c1c4SAnshuman Khandual 		/*
1669c952c1c4SAnshuman Khandual 		 * User is accessing the DSCR register using the problem
1670c952c1c4SAnshuman Khandual 		 * state only SPR number (0x03) either through a mfspr or
1671c952c1c4SAnshuman Khandual 		 * a mtspr instruction. If it is a write attempt through
1672c952c1c4SAnshuman Khandual 		 * a mtspr, then we set the inherit bit. This also allows
1673c952c1c4SAnshuman Khandual 		 * the user to write or read the register directly in the
1674c952c1c4SAnshuman Khandual 		 * future by setting via the FSCR DSCR bit. But in case it
1675c952c1c4SAnshuman Khandual 		 * is a read DSCR attempt through a mfspr instruction, we
1676c952c1c4SAnshuman Khandual 		 * just emulate the instruction instead. This code path will
1677c952c1c4SAnshuman Khandual 		 * always emulate all the mfspr instructions till the user
1678c952c1c4SAnshuman Khandual 		 * has attempted at least one mtspr instruction. This way it
1679c952c1c4SAnshuman Khandual 		 * preserves the same behaviour when the user is accessing
1680c952c1c4SAnshuman Khandual 		 * the DSCR through privilege level only SPR number (0x11)
1681c952c1c4SAnshuman Khandual 		 * which is emulated through illegal instruction exception.
1682c952c1c4SAnshuman Khandual 		 * We always leave HFSCR DSCR set.
16832517617eSMichael Neuling 		 */
1684c952c1c4SAnshuman Khandual 		if (get_user(instword, (u32 __user *)(regs->nip))) {
1685c952c1c4SAnshuman Khandual 			pr_err("Failed to fetch the user instruction\n");
1686c952c1c4SAnshuman Khandual 			return;
1687c952c1c4SAnshuman Khandual 		}
1688c952c1c4SAnshuman Khandual 
1689c952c1c4SAnshuman Khandual 		/* Write into DSCR (mtspr 0x03, RS) */
1690c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1691c952c1c4SAnshuman Khandual 				== PPC_INST_MTSPR_DSCR_USER) {
1692c952c1c4SAnshuman Khandual 			rd = (instword >> 21) & 0x1f;
1693c952c1c4SAnshuman Khandual 			current->thread.dscr = regs->gpr[rd];
16942517617eSMichael Neuling 			current->thread.dscr_inherit = 1;
1695b57bd2deSMichael Neuling 			current->thread.fscr |= FSCR_DSCR;
1696b57bd2deSMichael Neuling 			mtspr(SPRN_FSCR, current->thread.fscr);
1697c952c1c4SAnshuman Khandual 		}
1698c952c1c4SAnshuman Khandual 
1699c952c1c4SAnshuman Khandual 		/* Read from DSCR (mfspr RT, 0x03) */
1700c952c1c4SAnshuman Khandual 		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1701c952c1c4SAnshuman Khandual 				== PPC_INST_MFSPR_DSCR_USER) {
1702c952c1c4SAnshuman Khandual 			if (emulate_instruction(regs)) {
1703c952c1c4SAnshuman Khandual 				pr_err("DSCR based mfspr emulation failed\n");
1704c952c1c4SAnshuman Khandual 				return;
1705c952c1c4SAnshuman Khandual 			}
1706c952c1c4SAnshuman Khandual 			regs->nip += 4;
1707c952c1c4SAnshuman Khandual 			emulate_single_step(regs);
1708c952c1c4SAnshuman Khandual 		}
17092517617eSMichael Neuling 		return;
1710b14b6260SMichael Ellerman 	}
1711b14b6260SMichael Ellerman 
1712172f7aaaSCyril Bur 	if (status == FSCR_TM_LG) {
1713172f7aaaSCyril Bur 		/*
1714172f7aaaSCyril Bur 		 * If we're here then the hardware is TM aware because it
1715172f7aaaSCyril Bur 		 * generated an exception with FSRM_TM set.
1716172f7aaaSCyril Bur 		 *
1717172f7aaaSCyril Bur 		 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1718172f7aaaSCyril Bur 		 * told us not to do TM, or the kernel is not built with TM
1719172f7aaaSCyril Bur 		 * support.
1720172f7aaaSCyril Bur 		 *
1721172f7aaaSCyril Bur 		 * If both of those things are true, then userspace can spam the
1722172f7aaaSCyril Bur 		 * console by triggering the printk() below just by continually
1723172f7aaaSCyril Bur 		 * doing tbegin (or any TM instruction). So in that case just
1724172f7aaaSCyril Bur 		 * send the process a SIGILL immediately.
1725172f7aaaSCyril Bur 		 */
1726172f7aaaSCyril Bur 		if (!cpu_has_feature(CPU_FTR_TM))
1727172f7aaaSCyril Bur 			goto out;
1728172f7aaaSCyril Bur 
1729172f7aaaSCyril Bur 		tm_unavailable(regs);
1730172f7aaaSCyril Bur 		return;
1731172f7aaaSCyril Bur 	}
1732172f7aaaSCyril Bur 
173393c2ec0fSBalbir Singh 	pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
173493c2ec0fSBalbir Singh 		hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
1735d0c0c9a1SMichael Neuling 
1736172f7aaaSCyril Bur out:
1737d0c0c9a1SMichael Neuling 	_exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1738d0c0c9a1SMichael Neuling }
17392517617eSMichael Neuling #endif
1740d0c0c9a1SMichael Neuling 
1741f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1742f54db641SMichael Neuling 
1743f54db641SMichael Neuling void fp_unavailable_tm(struct pt_regs *regs)
1744f54db641SMichael Neuling {
1745f54db641SMichael Neuling 	/* Note:  This does not handle any kind of FP laziness. */
1746f54db641SMichael Neuling 
1747f54db641SMichael Neuling 	TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1748f54db641SMichael Neuling 		 regs->nip, regs->msr);
1749f54db641SMichael Neuling 
1750f54db641SMichael Neuling         /* We can only have got here if the task started using FP after
1751f54db641SMichael Neuling          * beginning the transaction.  So, the transactional regs are just a
1752f54db641SMichael Neuling          * copy of the checkpointed ones.  But, we still need to recheckpoint
1753f54db641SMichael Neuling          * as we're enabling FP for the process; it will return, abort the
1754f54db641SMichael Neuling          * transaction, and probably retry but now with FP enabled.  So the
1755f54db641SMichael Neuling          * checkpointed FP registers need to be loaded.
1756f54db641SMichael Neuling 	 */
1757d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1758f54db641SMichael Neuling 	/* Reclaim didn't save out any FPRs to transact_fprs. */
1759f54db641SMichael Neuling 
1760f54db641SMichael Neuling 	/* Enable FP for the task: */
1761a7771176SCyril Bur 	current->thread.load_fp = 1;
1762f54db641SMichael Neuling 
1763f54db641SMichael Neuling 	/* This loads and recheckpoints the FP registers from
1764f54db641SMichael Neuling 	 * thread.fpr[].  They will remain in registers after the
1765f54db641SMichael Neuling 	 * checkpoint so we don't need to reload them after.
17663ac8ff1cSPaul Mackerras 	 * If VMX is in use, the VRs now hold checkpointed values,
17673ac8ff1cSPaul Mackerras 	 * so we don't want to load the VRs from the thread_struct.
1768f54db641SMichael Neuling 	 */
1769eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1770f54db641SMichael Neuling }
1771f54db641SMichael Neuling 
1772f54db641SMichael Neuling void altivec_unavailable_tm(struct pt_regs *regs)
1773f54db641SMichael Neuling {
1774f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This function operates
1775f54db641SMichael Neuling 	 * the same way.
1776f54db641SMichael Neuling 	 */
1777f54db641SMichael Neuling 
1778f54db641SMichael Neuling 	TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1779f54db641SMichael Neuling 		 "MSR=%lx\n",
1780f54db641SMichael Neuling 		 regs->nip, regs->msr);
1781d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1782a7771176SCyril Bur 	current->thread.load_vec = 1;
1783eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1784f54db641SMichael Neuling 	current->thread.used_vr = 1;
17853ac8ff1cSPaul Mackerras }
17863ac8ff1cSPaul Mackerras 
1787f54db641SMichael Neuling void vsx_unavailable_tm(struct pt_regs *regs)
1788f54db641SMichael Neuling {
1789f54db641SMichael Neuling 	/* See the comments in fp_unavailable_tm().  This works similarly,
1790f54db641SMichael Neuling 	 * though we're loading both FP and VEC registers in here.
1791f54db641SMichael Neuling 	 *
1792f54db641SMichael Neuling 	 * If FP isn't in use, load FP regs.  If VEC isn't in use, load VEC
1793f54db641SMichael Neuling 	 * regs.  Either way, set MSR_VSX.
1794f54db641SMichael Neuling 	 */
1795f54db641SMichael Neuling 
1796f54db641SMichael Neuling 	TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1797f54db641SMichael Neuling 		 "MSR=%lx\n",
1798f54db641SMichael Neuling 		 regs->nip, regs->msr);
1799f54db641SMichael Neuling 
18003ac8ff1cSPaul Mackerras 	current->thread.used_vsr = 1;
18013ac8ff1cSPaul Mackerras 
1802f54db641SMichael Neuling 	/* This reclaims FP and/or VR regs if they're already enabled */
1803d31626f7SPaul Mackerras 	tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1804f54db641SMichael Neuling 
1805a7771176SCyril Bur 	current->thread.load_vec = 1;
1806a7771176SCyril Bur 	current->thread.load_fp = 1;
18073ac8ff1cSPaul Mackerras 
1808eb5c3f1cSCyril Bur 	tm_recheckpoint(&current->thread);
1809f54db641SMichael Neuling }
1810f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1811f54db641SMichael Neuling 
1812dc1c1ca3SStephen Rothwell void performance_monitor_exception(struct pt_regs *regs)
1813dc1c1ca3SStephen Rothwell {
181469111bacSChristoph Lameter 	__this_cpu_inc(irq_stat.pmu_irqs);
181589713ed1SAnton Blanchard 
1816dc1c1ca3SStephen Rothwell 	perf_irq(regs);
1817dc1c1ca3SStephen Rothwell }
1818dc1c1ca3SStephen Rothwell 
1819172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS
18203bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
18213bffb652SDave Kleikamp {
18223bffb652SDave Kleikamp 	int changed = 0;
18233bffb652SDave Kleikamp 	/*
18243bffb652SDave Kleikamp 	 * Determine the cause of the debug event, clear the
18253bffb652SDave Kleikamp 	 * event flags and send a trap to the handler. Torez
18263bffb652SDave Kleikamp 	 */
18273bffb652SDave Kleikamp 	if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
18283bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
18293bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
183051ae8d4aSBharat Bhushan 		current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
18313bffb652SDave Kleikamp #endif
183247355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC1), debug_status,
18333bffb652SDave Kleikamp 			     5);
18343bffb652SDave Kleikamp 		changed |= 0x01;
18353bffb652SDave Kleikamp 	}  else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
18363bffb652SDave Kleikamp 		dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
183747355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_DAC2), debug_status,
18383bffb652SDave Kleikamp 			     6);
18393bffb652SDave Kleikamp 		changed |= 0x01;
18403bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC1) {
184151ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
18423bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
184347355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC1), debug_status,
18443bffb652SDave Kleikamp 			     1);
18453bffb652SDave Kleikamp 		changed |= 0x01;
18463bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC2) {
184751ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
184847355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC2), debug_status,
18493bffb652SDave Kleikamp 			     2);
18503bffb652SDave Kleikamp 		changed |= 0x01;
18513bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC3) {
185251ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
18533bffb652SDave Kleikamp 		dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
185447355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC3), debug_status,
18553bffb652SDave Kleikamp 			     3);
18563bffb652SDave Kleikamp 		changed |= 0x01;
18573bffb652SDave Kleikamp 	}  else if (debug_status & DBSR_IAC4) {
185851ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
185947355040SEric W. Biederman 		do_send_trap(regs, mfspr(SPRN_IAC4), debug_status,
18603bffb652SDave Kleikamp 			     4);
18613bffb652SDave Kleikamp 		changed |= 0x01;
18623bffb652SDave Kleikamp 	}
18633bffb652SDave Kleikamp 	/*
18643bffb652SDave Kleikamp 	 * At the point this routine was called, the MSR(DE) was turned off.
18653bffb652SDave Kleikamp 	 * Check all other debug flags and see if that bit needs to be turned
18663bffb652SDave Kleikamp 	 * back on or not.
18673bffb652SDave Kleikamp 	 */
186851ae8d4aSBharat Bhushan 	if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
186951ae8d4aSBharat Bhushan 			       current->thread.debug.dbcr1))
18703bffb652SDave Kleikamp 		regs->msr |= MSR_DE;
18713bffb652SDave Kleikamp 	else
18723bffb652SDave Kleikamp 		/* Make sure the IDM flag is off */
187351ae8d4aSBharat Bhushan 		current->thread.debug.dbcr0 &= ~DBCR0_IDM;
18743bffb652SDave Kleikamp 
18753bffb652SDave Kleikamp 	if (changed & 0x01)
187651ae8d4aSBharat Bhushan 		mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
18773bffb652SDave Kleikamp }
187814cf11afSPaul Mackerras 
187903465f89SNicholas Piggin void DebugException(struct pt_regs *regs, unsigned long debug_status)
188014cf11afSPaul Mackerras {
188151ae8d4aSBharat Bhushan 	current->thread.debug.dbsr = debug_status;
18823bffb652SDave Kleikamp 
1883ec097c84SRoland McGrath 	/* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1884ec097c84SRoland McGrath 	 * on server, it stops on the target of the branch. In order to simulate
1885ec097c84SRoland McGrath 	 * the server behaviour, we thus restart right away with a single step
1886ec097c84SRoland McGrath 	 * instead of stopping here when hitting a BT
1887ec097c84SRoland McGrath 	 */
1888ec097c84SRoland McGrath 	if (debug_status & DBSR_BT) {
1889ec097c84SRoland McGrath 		regs->msr &= ~MSR_DE;
1890ec097c84SRoland McGrath 
1891ec097c84SRoland McGrath 		/* Disable BT */
1892ec097c84SRoland McGrath 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1893ec097c84SRoland McGrath 		/* Clear the BT event */
1894ec097c84SRoland McGrath 		mtspr(SPRN_DBSR, DBSR_BT);
1895ec097c84SRoland McGrath 
1896ec097c84SRoland McGrath 		/* Do the single step trick only when coming from userspace */
1897ec097c84SRoland McGrath 		if (user_mode(regs)) {
189851ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_BT;
189951ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1900ec097c84SRoland McGrath 			regs->msr |= MSR_DE;
1901ec097c84SRoland McGrath 			return;
1902ec097c84SRoland McGrath 		}
1903ec097c84SRoland McGrath 
19046cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
19056cc89badSNaveen N. Rao 			return;
19066cc89badSNaveen N. Rao 
1907ec097c84SRoland McGrath 		if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1908ec097c84SRoland McGrath 			       5, SIGTRAP) == NOTIFY_STOP) {
1909ec097c84SRoland McGrath 			return;
1910ec097c84SRoland McGrath 		}
1911ec097c84SRoland McGrath 		if (debugger_sstep(regs))
1912ec097c84SRoland McGrath 			return;
1913ec097c84SRoland McGrath 	} else if (debug_status & DBSR_IC) { 	/* Instruction complete */
191414cf11afSPaul Mackerras 		regs->msr &= ~MSR_DE;
1915f8279621SKumar Gala 
191614cf11afSPaul Mackerras 		/* Disable instruction completion */
191714cf11afSPaul Mackerras 		mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
191814cf11afSPaul Mackerras 		/* Clear the instruction completion event */
191914cf11afSPaul Mackerras 		mtspr(SPRN_DBSR, DBSR_IC);
1920f8279621SKumar Gala 
19216cc89badSNaveen N. Rao 		if (kprobe_post_handler(regs))
19226cc89badSNaveen N. Rao 			return;
19236cc89badSNaveen N. Rao 
1924f8279621SKumar Gala 		if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1925f8279621SKumar Gala 			       5, SIGTRAP) == NOTIFY_STOP) {
192614cf11afSPaul Mackerras 			return;
192714cf11afSPaul Mackerras 		}
1928f8279621SKumar Gala 
1929f8279621SKumar Gala 		if (debugger_sstep(regs))
1930f8279621SKumar Gala 			return;
1931f8279621SKumar Gala 
19323bffb652SDave Kleikamp 		if (user_mode(regs)) {
193351ae8d4aSBharat Bhushan 			current->thread.debug.dbcr0 &= ~DBCR0_IC;
193451ae8d4aSBharat Bhushan 			if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
193551ae8d4aSBharat Bhushan 					       current->thread.debug.dbcr1))
19363bffb652SDave Kleikamp 				regs->msr |= MSR_DE;
19373bffb652SDave Kleikamp 			else
19383bffb652SDave Kleikamp 				/* Make sure the IDM bit is off */
193951ae8d4aSBharat Bhushan 				current->thread.debug.dbcr0 &= ~DBCR0_IDM;
19403bffb652SDave Kleikamp 		}
1941f8279621SKumar Gala 
1942f8279621SKumar Gala 		_exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
19433bffb652SDave Kleikamp 	} else
19443bffb652SDave Kleikamp 		handle_debug(regs, debug_status);
194514cf11afSPaul Mackerras }
194603465f89SNicholas Piggin NOKPROBE_SYMBOL(DebugException);
1947172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
194814cf11afSPaul Mackerras 
194914cf11afSPaul Mackerras #if !defined(CONFIG_TAU_INT)
195014cf11afSPaul Mackerras void TAUException(struct pt_regs *regs)
195114cf11afSPaul Mackerras {
195214cf11afSPaul Mackerras 	printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx    %s\n",
195314cf11afSPaul Mackerras 	       regs->nip, regs->msr, regs->trap, print_tainted());
195414cf11afSPaul Mackerras }
195514cf11afSPaul Mackerras #endif /* CONFIG_INT_TAU */
195614cf11afSPaul Mackerras 
195714cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC
1958dc1c1ca3SStephen Rothwell void altivec_assist_exception(struct pt_regs *regs)
195914cf11afSPaul Mackerras {
196014cf11afSPaul Mackerras 	int err;
196114cf11afSPaul Mackerras 
196214cf11afSPaul Mackerras 	if (!user_mode(regs)) {
196314cf11afSPaul Mackerras 		printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
196414cf11afSPaul Mackerras 		       " at %lx\n", regs->nip);
19658dad3f92SPaul Mackerras 		die("Kernel VMX/Altivec assist exception", regs, SIGILL);
196614cf11afSPaul Mackerras 	}
196714cf11afSPaul Mackerras 
1968dc1c1ca3SStephen Rothwell 	flush_altivec_to_thread(current);
1969dc1c1ca3SStephen Rothwell 
1970eecff81dSAnton Blanchard 	PPC_WARN_EMULATED(altivec, regs);
197114cf11afSPaul Mackerras 	err = emulate_altivec(regs);
197214cf11afSPaul Mackerras 	if (err == 0) {
197314cf11afSPaul Mackerras 		regs->nip += 4;		/* skip emulated instruction */
197414cf11afSPaul Mackerras 		emulate_single_step(regs);
197514cf11afSPaul Mackerras 		return;
197614cf11afSPaul Mackerras 	}
197714cf11afSPaul Mackerras 
197814cf11afSPaul Mackerras 	if (err == -EFAULT) {
197914cf11afSPaul Mackerras 		/* got an error reading the instruction */
198014cf11afSPaul Mackerras 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
198114cf11afSPaul Mackerras 	} else {
198214cf11afSPaul Mackerras 		/* didn't recognize the instruction */
198314cf11afSPaul Mackerras 		/* XXX quick hack for now: set the non-Java bit in the VSCR */
198476462232SChristian Dietrich 		printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
198514cf11afSPaul Mackerras 				   "in %s at %lx\n", current->comm, regs->nip);
1986de79f7b9SPaul Mackerras 		current->thread.vr_state.vscr.u[3] |= 0x10000;
198714cf11afSPaul Mackerras 	}
198814cf11afSPaul Mackerras }
198914cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */
199014cf11afSPaul Mackerras 
199114cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE
199214cf11afSPaul Mackerras void CacheLockingException(struct pt_regs *regs, unsigned long address,
199314cf11afSPaul Mackerras 			   unsigned long error_code)
199414cf11afSPaul Mackerras {
199514cf11afSPaul Mackerras 	/* We treat cache locking instructions from the user
199614cf11afSPaul Mackerras 	 * as priv ops, in the future we could try to do
199714cf11afSPaul Mackerras 	 * something smarter
199814cf11afSPaul Mackerras 	 */
199914cf11afSPaul Mackerras 	if (error_code & (ESR_DLK|ESR_ILK))
200014cf11afSPaul Mackerras 		_exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
200114cf11afSPaul Mackerras 	return;
200214cf11afSPaul Mackerras }
200314cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */
200414cf11afSPaul Mackerras 
200514cf11afSPaul Mackerras #ifdef CONFIG_SPE
200614cf11afSPaul Mackerras void SPEFloatingPointException(struct pt_regs *regs)
200714cf11afSPaul Mackerras {
20086a800f36SLiu Yu 	extern int do_spe_mathemu(struct pt_regs *regs);
200914cf11afSPaul Mackerras 	unsigned long spefscr;
201014cf11afSPaul Mackerras 	int fpexc_mode;
2011aeb1c0f6SEric W. Biederman 	int code = FPE_FLTUNK;
20126a800f36SLiu Yu 	int err;
20136a800f36SLiu Yu 
2014685659eeSyu liu 	flush_spe_to_thread(current);
201514cf11afSPaul Mackerras 
201614cf11afSPaul Mackerras 	spefscr = current->thread.spefscr;
201714cf11afSPaul Mackerras 	fpexc_mode = current->thread.fpexc_mode;
201814cf11afSPaul Mackerras 
201914cf11afSPaul Mackerras 	if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
202014cf11afSPaul Mackerras 		code = FPE_FLTOVF;
202114cf11afSPaul Mackerras 	}
202214cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
202314cf11afSPaul Mackerras 		code = FPE_FLTUND;
202414cf11afSPaul Mackerras 	}
202514cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
202614cf11afSPaul Mackerras 		code = FPE_FLTDIV;
202714cf11afSPaul Mackerras 	else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
202814cf11afSPaul Mackerras 		code = FPE_FLTINV;
202914cf11afSPaul Mackerras 	}
203014cf11afSPaul Mackerras 	else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
203114cf11afSPaul Mackerras 		code = FPE_FLTRES;
203214cf11afSPaul Mackerras 
20336a800f36SLiu Yu 	err = do_spe_mathemu(regs);
20346a800f36SLiu Yu 	if (err == 0) {
20356a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
20366a800f36SLiu Yu 		emulate_single_step(regs);
203714cf11afSPaul Mackerras 		return;
203814cf11afSPaul Mackerras 	}
20396a800f36SLiu Yu 
20406a800f36SLiu Yu 	if (err == -EFAULT) {
20416a800f36SLiu Yu 		/* got an error reading the instruction */
20426a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
20436a800f36SLiu Yu 	} else if (err == -EINVAL) {
20446a800f36SLiu Yu 		/* didn't recognize the instruction */
20456a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
20466a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
20476a800f36SLiu Yu 	} else {
20486a800f36SLiu Yu 		_exception(SIGFPE, regs, code, regs->nip);
20496a800f36SLiu Yu 	}
20506a800f36SLiu Yu 
20516a800f36SLiu Yu 	return;
20526a800f36SLiu Yu }
20536a800f36SLiu Yu 
20546a800f36SLiu Yu void SPEFloatingPointRoundException(struct pt_regs *regs)
20556a800f36SLiu Yu {
20566a800f36SLiu Yu 	extern int speround_handler(struct pt_regs *regs);
20576a800f36SLiu Yu 	int err;
20586a800f36SLiu Yu 
20596a800f36SLiu Yu 	preempt_disable();
20606a800f36SLiu Yu 	if (regs->msr & MSR_SPE)
20616a800f36SLiu Yu 		giveup_spe(current);
20626a800f36SLiu Yu 	preempt_enable();
20636a800f36SLiu Yu 
20646a800f36SLiu Yu 	regs->nip -= 4;
20656a800f36SLiu Yu 	err = speround_handler(regs);
20666a800f36SLiu Yu 	if (err == 0) {
20676a800f36SLiu Yu 		regs->nip += 4;		/* skip emulated instruction */
20686a800f36SLiu Yu 		emulate_single_step(regs);
20696a800f36SLiu Yu 		return;
20706a800f36SLiu Yu 	}
20716a800f36SLiu Yu 
20726a800f36SLiu Yu 	if (err == -EFAULT) {
20736a800f36SLiu Yu 		/* got an error reading the instruction */
20746a800f36SLiu Yu 		_exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
20756a800f36SLiu Yu 	} else if (err == -EINVAL) {
20766a800f36SLiu Yu 		/* didn't recognize the instruction */
20776a800f36SLiu Yu 		printk(KERN_ERR "unrecognized spe instruction "
20786a800f36SLiu Yu 		       "in %s at %lx\n", current->comm, regs->nip);
20796a800f36SLiu Yu 	} else {
2080aeb1c0f6SEric W. Biederman 		_exception(SIGFPE, regs, FPE_FLTUNK, regs->nip);
20816a800f36SLiu Yu 		return;
20826a800f36SLiu Yu 	}
20836a800f36SLiu Yu }
208414cf11afSPaul Mackerras #endif
208514cf11afSPaul Mackerras 
2086dc1c1ca3SStephen Rothwell /*
2087dc1c1ca3SStephen Rothwell  * We enter here if we get an unrecoverable exception, that is, one
2088dc1c1ca3SStephen Rothwell  * that happened at a point where the RI (recoverable interrupt) bit
2089dc1c1ca3SStephen Rothwell  * in the MSR is 0.  This indicates that SRR0/1 are live, and that
2090dc1c1ca3SStephen Rothwell  * we therefore lost state by taking this exception.
2091dc1c1ca3SStephen Rothwell  */
2092dc1c1ca3SStephen Rothwell void unrecoverable_exception(struct pt_regs *regs)
2093dc1c1ca3SStephen Rothwell {
2094dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
2095dc1c1ca3SStephen Rothwell 	       regs->trap, regs->nip);
2096dc1c1ca3SStephen Rothwell 	die("Unrecoverable exception", regs, SIGABRT);
2097dc1c1ca3SStephen Rothwell }
209815770a13SNaveen N. Rao NOKPROBE_SYMBOL(unrecoverable_exception);
2099dc1c1ca3SStephen Rothwell 
21001e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
210114cf11afSPaul Mackerras /*
210214cf11afSPaul Mackerras  * Default handler for a Watchdog exception,
210314cf11afSPaul Mackerras  * spins until a reboot occurs
210414cf11afSPaul Mackerras  */
210514cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
210614cf11afSPaul Mackerras {
210714cf11afSPaul Mackerras 	/* Generic WatchdogHandler, implement your own */
210814cf11afSPaul Mackerras 	mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
210914cf11afSPaul Mackerras 	return;
211014cf11afSPaul Mackerras }
211114cf11afSPaul Mackerras 
211214cf11afSPaul Mackerras void WatchdogException(struct pt_regs *regs)
211314cf11afSPaul Mackerras {
211414cf11afSPaul Mackerras 	printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
211514cf11afSPaul Mackerras 	WatchdogHandler(regs);
211614cf11afSPaul Mackerras }
211714cf11afSPaul Mackerras #endif
2118dc1c1ca3SStephen Rothwell 
2119dc1c1ca3SStephen Rothwell /*
2120dc1c1ca3SStephen Rothwell  * We enter here if we discover during exception entry that we are
2121dc1c1ca3SStephen Rothwell  * running in supervisor mode with a userspace value in the stack pointer.
2122dc1c1ca3SStephen Rothwell  */
2123dc1c1ca3SStephen Rothwell void kernel_bad_stack(struct pt_regs *regs)
2124dc1c1ca3SStephen Rothwell {
2125dc1c1ca3SStephen Rothwell 	printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
2126dc1c1ca3SStephen Rothwell 	       regs->gpr[1], regs->nip);
2127dc1c1ca3SStephen Rothwell 	die("Bad kernel stack pointer", regs, SIGABRT);
2128dc1c1ca3SStephen Rothwell }
212915770a13SNaveen N. Rao NOKPROBE_SYMBOL(kernel_bad_stack);
213014cf11afSPaul Mackerras 
213114cf11afSPaul Mackerras void __init trap_init(void)
213214cf11afSPaul Mackerras {
213314cf11afSPaul Mackerras }
213480947e7cSGeert Uytterhoeven 
213580947e7cSGeert Uytterhoeven 
213680947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS
213780947e7cSGeert Uytterhoeven 
213880947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type)	.type = { .name = #type }
213980947e7cSGeert Uytterhoeven 
214080947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = {
214180947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC
214280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(altivec),
214380947e7cSGeert Uytterhoeven #endif
214480947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcba),
214580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(dcbz),
214680947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(fp_pair),
214780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(isel),
214880947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mcrxr),
214980947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(mfpvr),
215080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(multiple),
215180947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(popcntb),
215280947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(spe),
215380947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(string),
2154a3821b2aSScott Wood 	WARN_EMULATED_SETUP(sync),
215580947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(unaligned),
215680947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION
215780947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(math),
215880947e7cSGeert Uytterhoeven #endif
215980947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX
216080947e7cSGeert Uytterhoeven 	WARN_EMULATED_SETUP(vsx),
216180947e7cSGeert Uytterhoeven #endif
2162efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64
2163efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mfdscr),
2164efcac658SAlexey Kardashevskiy 	WARN_EMULATED_SETUP(mtdscr),
2165f83319d7SAnton Blanchard 	WARN_EMULATED_SETUP(lq_stq),
21665080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvw4x),
21675080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvh8x),
21685080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvd2x),
21695080332cSMichael Neuling 	WARN_EMULATED_SETUP(lxvb16x),
2170efcac658SAlexey Kardashevskiy #endif
217180947e7cSGeert Uytterhoeven };
217280947e7cSGeert Uytterhoeven 
217380947e7cSGeert Uytterhoeven u32 ppc_warn_emulated;
217480947e7cSGeert Uytterhoeven 
217580947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type)
217680947e7cSGeert Uytterhoeven {
217776462232SChristian Dietrich 	pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
217880947e7cSGeert Uytterhoeven 			    type);
217980947e7cSGeert Uytterhoeven }
218080947e7cSGeert Uytterhoeven 
218180947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void)
218280947e7cSGeert Uytterhoeven {
218380947e7cSGeert Uytterhoeven 	struct dentry *dir, *d;
218480947e7cSGeert Uytterhoeven 	unsigned int i;
218580947e7cSGeert Uytterhoeven 	struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
218680947e7cSGeert Uytterhoeven 
218780947e7cSGeert Uytterhoeven 	if (!powerpc_debugfs_root)
218880947e7cSGeert Uytterhoeven 		return -ENODEV;
218980947e7cSGeert Uytterhoeven 
219080947e7cSGeert Uytterhoeven 	dir = debugfs_create_dir("emulated_instructions",
219180947e7cSGeert Uytterhoeven 				 powerpc_debugfs_root);
219280947e7cSGeert Uytterhoeven 	if (!dir)
219380947e7cSGeert Uytterhoeven 		return -ENOMEM;
219480947e7cSGeert Uytterhoeven 
219557ad583fSRussell Currey 	d = debugfs_create_u32("do_warn", 0644, dir,
219680947e7cSGeert Uytterhoeven 			       &ppc_warn_emulated);
219780947e7cSGeert Uytterhoeven 	if (!d)
219880947e7cSGeert Uytterhoeven 		goto fail;
219980947e7cSGeert Uytterhoeven 
220080947e7cSGeert Uytterhoeven 	for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
220157ad583fSRussell Currey 		d = debugfs_create_u32(entries[i].name, 0644, dir,
220280947e7cSGeert Uytterhoeven 				       (u32 *)&entries[i].val.counter);
220380947e7cSGeert Uytterhoeven 		if (!d)
220480947e7cSGeert Uytterhoeven 			goto fail;
220580947e7cSGeert Uytterhoeven 	}
220680947e7cSGeert Uytterhoeven 
220780947e7cSGeert Uytterhoeven 	return 0;
220880947e7cSGeert Uytterhoeven 
220980947e7cSGeert Uytterhoeven fail:
221080947e7cSGeert Uytterhoeven 	debugfs_remove_recursive(dir);
221180947e7cSGeert Uytterhoeven 	return -ENOMEM;
221280947e7cSGeert Uytterhoeven }
221380947e7cSGeert Uytterhoeven 
221480947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init);
221580947e7cSGeert Uytterhoeven 
221680947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */
2217