12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 214cf11afSPaul Mackerras /* 314cf11afSPaul Mackerras * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 4fe04b112SScott Wood * Copyright 2007-2010 Freescale Semiconductor, Inc. 514cf11afSPaul Mackerras * 614cf11afSPaul Mackerras * Modified by Cort Dougan (cort@cs.nmt.edu) 714cf11afSPaul Mackerras * and Paul Mackerras (paulus@samba.org) 814cf11afSPaul Mackerras */ 914cf11afSPaul Mackerras 1014cf11afSPaul Mackerras /* 1114cf11afSPaul Mackerras * This file handles the architecture-dependent parts of hardware exceptions 1214cf11afSPaul Mackerras */ 1314cf11afSPaul Mackerras 1414cf11afSPaul Mackerras #include <linux/errno.h> 1514cf11afSPaul Mackerras #include <linux/sched.h> 16b17b0153SIngo Molnar #include <linux/sched/debug.h> 1714cf11afSPaul Mackerras #include <linux/kernel.h> 1814cf11afSPaul Mackerras #include <linux/mm.h> 1999cd1302SRam Pai #include <linux/pkeys.h> 2014cf11afSPaul Mackerras #include <linux/stddef.h> 2114cf11afSPaul Mackerras #include <linux/unistd.h> 228dad3f92SPaul Mackerras #include <linux/ptrace.h> 2314cf11afSPaul Mackerras #include <linux/user.h> 2414cf11afSPaul Mackerras #include <linux/interrupt.h> 2514cf11afSPaul Mackerras #include <linux/init.h> 268a39b05fSPaul Gortmaker #include <linux/extable.h> 278a39b05fSPaul Gortmaker #include <linux/module.h> /* print_modules */ 288dad3f92SPaul Mackerras #include <linux/prctl.h> 2914cf11afSPaul Mackerras #include <linux/delay.h> 3014cf11afSPaul Mackerras #include <linux/kprobes.h> 31cc532915SMichael Ellerman #include <linux/kexec.h> 325474c120SMichael Hanselmann #include <linux/backlight.h> 3373c9ceabSJeremy Fitzhardinge #include <linux/bug.h> 341eeb66a1SChristoph Hellwig #include <linux/kdebug.h> 3576462232SChristian Dietrich #include <linux/ratelimit.h> 36ba12eedeSLi Zhong #include <linux/context_tracking.h> 375080332cSMichael Neuling #include <linux/smp.h> 3835adacd6SNicholas Piggin #include <linux/console.h> 3935adacd6SNicholas Piggin #include <linux/kmsg_dump.h> 4014cf11afSPaul Mackerras 4180947e7cSGeert Uytterhoeven #include <asm/emulated_ops.h> 427c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 437644d581SMichael Ellerman #include <asm/debugfs.h> 443a96570fSNicholas Piggin #include <asm/interrupt.h> 4514cf11afSPaul Mackerras #include <asm/io.h> 4686417780SPaul Mackerras #include <asm/machdep.h> 4786417780SPaul Mackerras #include <asm/rtas.h> 48f7f6f4feSDavid Gibson #include <asm/pmc.h> 4914cf11afSPaul Mackerras #include <asm/reg.h> 5014cf11afSPaul Mackerras #ifdef CONFIG_PMAC_BACKLIGHT 5114cf11afSPaul Mackerras #include <asm/backlight.h> 5214cf11afSPaul Mackerras #endif 53dc1c1ca3SStephen Rothwell #ifdef CONFIG_PPC64 5486417780SPaul Mackerras #include <asm/firmware.h> 55dc1c1ca3SStephen Rothwell #include <asm/processor.h> 566ce6c629SMichael Neuling #include <asm/tm.h> 57dc1c1ca3SStephen Rothwell #endif 58c0ce7d08SDavid Wilder #include <asm/kexec.h> 5916c57b36SKumar Gala #include <asm/ppc-opcode.h> 60cce1f106SShaohui Xie #include <asm/rio.h> 61ebaeb5aeSMahesh Salgaonkar #include <asm/fadump.h> 62ae3a197eSDavid Howells #include <asm/switch_to.h> 63f54db641SMichael Neuling #include <asm/tm.h> 64ae3a197eSDavid Howells #include <asm/debug.h> 6542f5b4caSDaniel Axtens #include <asm/asm-prototypes.h> 66fd7bacbcSMahesh Salgaonkar #include <asm/hmi.h> 674e0e3435SHongtao Jia #include <sysdev/fsl_pci.h> 686cc89badSNaveen N. Rao #include <asm/kprobes.h> 69a99b9c5eSMurilo Opsfelder Araujo #include <asm/stacktrace.h> 70de3c83c2SMathieu Malaterre #include <asm/nmi.h> 71dc1c1ca3SStephen Rothwell 72da665885SThiago Jung Bauermann #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE) 735be3492fSAnton Blanchard int (*__debugger)(struct pt_regs *regs) __read_mostly; 745be3492fSAnton Blanchard int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly; 755be3492fSAnton Blanchard int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly; 765be3492fSAnton Blanchard int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly; 775be3492fSAnton Blanchard int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly; 789422de3eSMichael Neuling int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly; 795be3492fSAnton Blanchard int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly; 8014cf11afSPaul Mackerras 8114cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger); 8214cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_ipi); 8314cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_bpt); 8414cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_sstep); 8514cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_iabr_match); 869422de3eSMichael Neuling EXPORT_SYMBOL(__debugger_break_match); 8714cf11afSPaul Mackerras EXPORT_SYMBOL(__debugger_fault_handler); 8814cf11afSPaul Mackerras #endif 8914cf11afSPaul Mackerras 908b3c34cfSMichael Neuling /* Transactional Memory trap debug */ 918b3c34cfSMichael Neuling #ifdef TM_DEBUG_SW 928b3c34cfSMichael Neuling #define TM_DEBUG(x...) printk(KERN_INFO x) 938b3c34cfSMichael Neuling #else 948b3c34cfSMichael Neuling #define TM_DEBUG(x...) do { } while(0) 958b3c34cfSMichael Neuling #endif 968b3c34cfSMichael Neuling 970f642d61SMurilo Opsfelder Araujo static const char *signame(int signr) 980f642d61SMurilo Opsfelder Araujo { 990f642d61SMurilo Opsfelder Araujo switch (signr) { 1000f642d61SMurilo Opsfelder Araujo case SIGBUS: return "bus error"; 1010f642d61SMurilo Opsfelder Araujo case SIGFPE: return "floating point exception"; 1020f642d61SMurilo Opsfelder Araujo case SIGILL: return "illegal instruction"; 1030f642d61SMurilo Opsfelder Araujo case SIGSEGV: return "segfault"; 1040f642d61SMurilo Opsfelder Araujo case SIGTRAP: return "unhandled trap"; 1050f642d61SMurilo Opsfelder Araujo } 1060f642d61SMurilo Opsfelder Araujo 1070f642d61SMurilo Opsfelder Araujo return "unknown signal"; 1080f642d61SMurilo Opsfelder Araujo } 1090f642d61SMurilo Opsfelder Araujo 11014cf11afSPaul Mackerras /* 11114cf11afSPaul Mackerras * Trap & Exception support 11214cf11afSPaul Mackerras */ 11314cf11afSPaul Mackerras 1146031d9d9Santon@samba.org #ifdef CONFIG_PMAC_BACKLIGHT 1156031d9d9Santon@samba.org static void pmac_backlight_unblank(void) 1166031d9d9Santon@samba.org { 1176031d9d9Santon@samba.org mutex_lock(&pmac_backlight_mutex); 1186031d9d9Santon@samba.org if (pmac_backlight) { 1196031d9d9Santon@samba.org struct backlight_properties *props; 1206031d9d9Santon@samba.org 1216031d9d9Santon@samba.org props = &pmac_backlight->props; 1226031d9d9Santon@samba.org props->brightness = props->max_brightness; 1236031d9d9Santon@samba.org props->power = FB_BLANK_UNBLANK; 1246031d9d9Santon@samba.org backlight_update_status(pmac_backlight); 1256031d9d9Santon@samba.org } 1266031d9d9Santon@samba.org mutex_unlock(&pmac_backlight_mutex); 1276031d9d9Santon@samba.org } 1286031d9d9Santon@samba.org #else 1296031d9d9Santon@samba.org static inline void pmac_backlight_unblank(void) { } 1306031d9d9Santon@samba.org #endif 1316031d9d9Santon@samba.org 1326fcd6baaSNicholas Piggin /* 1336fcd6baaSNicholas Piggin * If oops/die is expected to crash the machine, return true here. 1346fcd6baaSNicholas Piggin * 1356fcd6baaSNicholas Piggin * This should not be expected to be 100% accurate, there may be 1366fcd6baaSNicholas Piggin * notifiers registered or other unexpected conditions that may bring 1376fcd6baaSNicholas Piggin * down the kernel. Or if the current process in the kernel is holding 1386fcd6baaSNicholas Piggin * locks or has other critical state, the kernel may become effectively 1396fcd6baaSNicholas Piggin * unusable anyway. 1406fcd6baaSNicholas Piggin */ 1416fcd6baaSNicholas Piggin bool die_will_crash(void) 1426fcd6baaSNicholas Piggin { 1436fcd6baaSNicholas Piggin if (should_fadump_crash()) 1446fcd6baaSNicholas Piggin return true; 1456fcd6baaSNicholas Piggin if (kexec_should_crash(current)) 1466fcd6baaSNicholas Piggin return true; 1476fcd6baaSNicholas Piggin if (in_interrupt() || panic_on_oops || 1486fcd6baaSNicholas Piggin !current->pid || is_global_init(current)) 1496fcd6baaSNicholas Piggin return true; 1506fcd6baaSNicholas Piggin 1516fcd6baaSNicholas Piggin return false; 1526fcd6baaSNicholas Piggin } 1536fcd6baaSNicholas Piggin 154760ca4dcSAnton Blanchard static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED; 155760ca4dcSAnton Blanchard static int die_owner = -1; 156760ca4dcSAnton Blanchard static unsigned int die_nest_count; 157c0ce7d08SDavid Wilder static int die_counter; 158760ca4dcSAnton Blanchard 15935adacd6SNicholas Piggin extern void panic_flush_kmsg_start(void) 16035adacd6SNicholas Piggin { 16135adacd6SNicholas Piggin /* 16235adacd6SNicholas Piggin * These are mostly taken from kernel/panic.c, but tries to do 16335adacd6SNicholas Piggin * relatively minimal work. Don't use delay functions (TB may 16435adacd6SNicholas Piggin * be broken), don't crash dump (need to set a firmware log), 16535adacd6SNicholas Piggin * don't run notifiers. We do want to get some information to 16635adacd6SNicholas Piggin * Linux console. 16735adacd6SNicholas Piggin */ 16835adacd6SNicholas Piggin console_verbose(); 16935adacd6SNicholas Piggin bust_spinlocks(1); 17035adacd6SNicholas Piggin } 17135adacd6SNicholas Piggin 17235adacd6SNicholas Piggin extern void panic_flush_kmsg_end(void) 17335adacd6SNicholas Piggin { 17435adacd6SNicholas Piggin printk_safe_flush_on_panic(); 17535adacd6SNicholas Piggin kmsg_dump(KMSG_DUMP_PANIC); 17635adacd6SNicholas Piggin bust_spinlocks(0); 17735adacd6SNicholas Piggin debug_locks_off(); 178de6da1e8SFeng Tang console_flush_on_panic(CONSOLE_FLUSH_PENDING); 17935adacd6SNicholas Piggin } 18035adacd6SNicholas Piggin 18103465f89SNicholas Piggin static unsigned long oops_begin(struct pt_regs *regs) 182760ca4dcSAnton Blanchard { 183760ca4dcSAnton Blanchard int cpu; 18434c2a14fSanton@samba.org unsigned long flags; 18514cf11afSPaul Mackerras 186293e4688Santon@samba.org oops_enter(); 187293e4688Santon@samba.org 188760ca4dcSAnton Blanchard /* racy, but better than risking deadlock. */ 189760ca4dcSAnton Blanchard raw_local_irq_save(flags); 190760ca4dcSAnton Blanchard cpu = smp_processor_id(); 191760ca4dcSAnton Blanchard if (!arch_spin_trylock(&die_lock)) { 192760ca4dcSAnton Blanchard if (cpu == die_owner) 193760ca4dcSAnton Blanchard /* nested oops. should stop eventually */; 194760ca4dcSAnton Blanchard else 195760ca4dcSAnton Blanchard arch_spin_lock(&die_lock); 196760ca4dcSAnton Blanchard } 197760ca4dcSAnton Blanchard die_nest_count++; 198760ca4dcSAnton Blanchard die_owner = cpu; 19914cf11afSPaul Mackerras console_verbose(); 20014cf11afSPaul Mackerras bust_spinlocks(1); 2016031d9d9Santon@samba.org if (machine_is(powermac)) 2026031d9d9Santon@samba.org pmac_backlight_unblank(); 203760ca4dcSAnton Blanchard return flags; 20434c2a14fSanton@samba.org } 20503465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_begin); 2065474c120SMichael Hanselmann 20703465f89SNicholas Piggin static void oops_end(unsigned long flags, struct pt_regs *regs, 208760ca4dcSAnton Blanchard int signr) 209760ca4dcSAnton Blanchard { 21014cf11afSPaul Mackerras bust_spinlocks(0); 211373d4d09SRusty Russell add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 212760ca4dcSAnton Blanchard die_nest_count--; 21358154c8cSAnton Blanchard oops_exit(); 21458154c8cSAnton Blanchard printk("\n"); 2157458e8b2SNicholas Piggin if (!die_nest_count) { 216760ca4dcSAnton Blanchard /* Nest count reaches zero, release the lock. */ 2177458e8b2SNicholas Piggin die_owner = -1; 218760ca4dcSAnton Blanchard arch_spin_unlock(&die_lock); 2197458e8b2SNicholas Piggin } 220760ca4dcSAnton Blanchard raw_local_irq_restore(flags); 221cc532915SMichael Ellerman 222d40b6768SNicholas Piggin /* 223d40b6768SNicholas Piggin * system_reset_excption handles debugger, crash dump, panic, for 0x100 224d40b6768SNicholas Piggin */ 225d40b6768SNicholas Piggin if (TRAP(regs) == 0x100) 226d40b6768SNicholas Piggin return; 227d40b6768SNicholas Piggin 228ebaeb5aeSMahesh Salgaonkar crash_fadump(regs, "die oops"); 229ebaeb5aeSMahesh Salgaonkar 2304388c9b3SNicholas Piggin if (kexec_should_crash(current)) 231cc532915SMichael Ellerman crash_kexec(regs); 2329b00ac06SAnton Blanchard 233760ca4dcSAnton Blanchard if (!signr) 234760ca4dcSAnton Blanchard return; 235760ca4dcSAnton Blanchard 23658154c8cSAnton Blanchard /* 23758154c8cSAnton Blanchard * While our oops output is serialised by a spinlock, output 23858154c8cSAnton Blanchard * from panic() called below can race and corrupt it. If we 23958154c8cSAnton Blanchard * know we are going to panic, delay for 1 second so we have a 24058154c8cSAnton Blanchard * chance to get clean backtraces from all CPUs that are oopsing. 24158154c8cSAnton Blanchard */ 24258154c8cSAnton Blanchard if (in_interrupt() || panic_on_oops || !current->pid || 24358154c8cSAnton Blanchard is_global_init(current)) { 24458154c8cSAnton Blanchard mdelay(MSEC_PER_SEC); 24558154c8cSAnton Blanchard } 24658154c8cSAnton Blanchard 247cea6a4baSHorms if (panic_on_oops) 248012c437dSHorms panic("Fatal exception"); 249760ca4dcSAnton Blanchard do_exit(signr); 250760ca4dcSAnton Blanchard } 25103465f89SNicholas Piggin NOKPROBE_SYMBOL(oops_end); 252cea6a4baSHorms 253d7e02f7bSAneesh Kumar K.V static char *get_mmu_str(void) 254d7e02f7bSAneesh Kumar K.V { 255d7e02f7bSAneesh Kumar K.V if (early_radix_enabled()) 256d7e02f7bSAneesh Kumar K.V return " MMU=Radix"; 257d7e02f7bSAneesh Kumar K.V if (early_mmu_has_feature(MMU_FTR_HPTE_TABLE)) 258d7e02f7bSAneesh Kumar K.V return " MMU=Hash"; 259d7e02f7bSAneesh Kumar K.V return ""; 260d7e02f7bSAneesh Kumar K.V } 261d7e02f7bSAneesh Kumar K.V 26203465f89SNicholas Piggin static int __die(const char *str, struct pt_regs *regs, long err) 263760ca4dcSAnton Blanchard { 264760ca4dcSAnton Blanchard printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter); 2652e82ca3cSMichael Ellerman 266d7e02f7bSAneesh Kumar K.V printk("%s PAGE_SIZE=%luK%s%s%s%s%s%s %s\n", 26778227443SMichael Ellerman IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) ? "LE" : "BE", 268d7e02f7bSAneesh Kumar K.V PAGE_SIZE / 1024, get_mmu_str(), 26978227443SMichael Ellerman IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : "", 27078227443SMichael Ellerman IS_ENABLED(CONFIG_SMP) ? " SMP" : "", 27178227443SMichael Ellerman IS_ENABLED(CONFIG_SMP) ? (" NR_CPUS=" __stringify(NR_CPUS)) : "", 27278227443SMichael Ellerman debug_pagealloc_enabled() ? " DEBUG_PAGEALLOC" : "", 27378227443SMichael Ellerman IS_ENABLED(CONFIG_NUMA) ? " NUMA" : "", 27478227443SMichael Ellerman ppc_md.name ? ppc_md.name : ""); 275760ca4dcSAnton Blanchard 276760ca4dcSAnton Blanchard if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP) 277760ca4dcSAnton Blanchard return 1; 278760ca4dcSAnton Blanchard 279760ca4dcSAnton Blanchard print_modules(); 280760ca4dcSAnton Blanchard show_regs(regs); 28114cf11afSPaul Mackerras 28214cf11afSPaul Mackerras return 0; 28314cf11afSPaul Mackerras } 28403465f89SNicholas Piggin NOKPROBE_SYMBOL(__die); 28514cf11afSPaul Mackerras 286760ca4dcSAnton Blanchard void die(const char *str, struct pt_regs *regs, long err) 287760ca4dcSAnton Blanchard { 2886f44b20eSNicholas Piggin unsigned long flags; 289760ca4dcSAnton Blanchard 290d40b6768SNicholas Piggin /* 291d40b6768SNicholas Piggin * system_reset_excption handles debugger, crash dump, panic, for 0x100 292d40b6768SNicholas Piggin */ 293d40b6768SNicholas Piggin if (TRAP(regs) != 0x100) { 2946f44b20eSNicholas Piggin if (debugger(regs)) 2956f44b20eSNicholas Piggin return; 296d40b6768SNicholas Piggin } 2976f44b20eSNicholas Piggin 2986f44b20eSNicholas Piggin flags = oops_begin(regs); 299760ca4dcSAnton Blanchard if (__die(str, regs, err)) 300760ca4dcSAnton Blanchard err = 0; 301760ca4dcSAnton Blanchard oops_end(flags, regs, err); 302760ca4dcSAnton Blanchard } 30315770a13SNaveen N. Rao NOKPROBE_SYMBOL(die); 304760ca4dcSAnton Blanchard 305efc463adSEric W. Biederman void user_single_step_report(struct pt_regs *regs) 30625baa35bSOleg Nesterov { 3072e1661d2SEric W. Biederman force_sig_fault(SIGTRAP, TRAP_TRACE, (void __user *)regs->nip); 30825baa35bSOleg Nesterov } 30925baa35bSOleg Nesterov 310658b0f92SMurilo Opsfelder Araujo static void show_signal_msg(int signr, struct pt_regs *regs, int code, 311658b0f92SMurilo Opsfelder Araujo unsigned long addr) 31214cf11afSPaul Mackerras { 313997dd26cSMichael Ellerman static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 314997dd26cSMichael Ellerman DEFAULT_RATELIMIT_BURST); 315997dd26cSMichael Ellerman 316997dd26cSMichael Ellerman if (!show_unhandled_signals) 31735a52a10SMurilo Opsfelder Araujo return; 31835a52a10SMurilo Opsfelder Araujo 31935a52a10SMurilo Opsfelder Araujo if (!unhandled_signal(current, signr)) 32035a52a10SMurilo Opsfelder Araujo return; 32135a52a10SMurilo Opsfelder Araujo 322997dd26cSMichael Ellerman if (!__ratelimit(&rs)) 323997dd26cSMichael Ellerman return; 324997dd26cSMichael Ellerman 3250f642d61SMurilo Opsfelder Araujo pr_info("%s[%d]: %s (%d) at %lx nip %lx lr %lx code %x", 3260f642d61SMurilo Opsfelder Araujo current->comm, current->pid, signame(signr), signr, 327d0c3d534SOlof Johansson addr, regs->nip, regs->link, code); 3280f642d61SMurilo Opsfelder Araujo 3290f642d61SMurilo Opsfelder Araujo print_vma_addr(KERN_CONT " in ", regs->nip); 3300f642d61SMurilo Opsfelder Araujo 3310f642d61SMurilo Opsfelder Araujo pr_cont("\n"); 332a99b9c5eSMurilo Opsfelder Araujo 333a99b9c5eSMurilo Opsfelder Araujo show_user_instructions(regs); 33414cf11afSPaul Mackerras } 335658b0f92SMurilo Opsfelder Araujo 3362c44ce28SEric W. Biederman static bool exception_common(int signr, struct pt_regs *regs, int code, 3372c44ce28SEric W. Biederman unsigned long addr) 338658b0f92SMurilo Opsfelder Araujo { 339658b0f92SMurilo Opsfelder Araujo if (!user_mode(regs)) { 340658b0f92SMurilo Opsfelder Araujo die("Exception in kernel mode", regs, signr); 3412c44ce28SEric W. Biederman return false; 342658b0f92SMurilo Opsfelder Araujo } 343658b0f92SMurilo Opsfelder Araujo 344658b0f92SMurilo Opsfelder Araujo show_signal_msg(signr, regs, code, addr); 34514cf11afSPaul Mackerras 346e6f8a6c8SNicholas Piggin if (arch_irqs_disabled()) 347e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 3489f2f79e3SBenjamin Herrenschmidt 34941ab5266SAnanth N Mavinakayanahalli current->thread.trap_nr = code; 350c5cc1f4dSThiago Jung Bauermann 3512c44ce28SEric W. Biederman return true; 3522c44ce28SEric W. Biederman } 3532c44ce28SEric W. Biederman 3545d8fb8a5SEric W. Biederman void _exception_pkey(struct pt_regs *regs, unsigned long addr, int key) 3552c44ce28SEric W. Biederman { 3565d8fb8a5SEric W. Biederman if (!exception_common(SIGSEGV, regs, SEGV_PKUERR, addr)) 3572c44ce28SEric W. Biederman return; 3582c44ce28SEric W. Biederman 35977c70728SEric W. Biederman force_sig_pkuerr((void __user *) addr, key); 36014cf11afSPaul Mackerras } 36114cf11afSPaul Mackerras 36299cd1302SRam Pai void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr) 36399cd1302SRam Pai { 364c1c7c85cSEric W. Biederman if (!exception_common(signr, regs, code, addr)) 365c1c7c85cSEric W. Biederman return; 366c1c7c85cSEric W. Biederman 3672e1661d2SEric W. Biederman force_sig_fault(signr, code, (void __user *)addr); 36899cd1302SRam Pai } 36999cd1302SRam Pai 370ccd47702SNicholas Piggin /* 371ccd47702SNicholas Piggin * The interrupt architecture has a quirk in that the HV interrupts excluding 372ccd47702SNicholas Piggin * the NMIs (0x100 and 0x200) do not clear MSR[RI] at entry. The first thing 373ccd47702SNicholas Piggin * that an interrupt handler must do is save off a GPR into a scratch register, 374ccd47702SNicholas Piggin * and all interrupts on POWERNV (HV=1) use the HSPRG1 register as scratch. 375ccd47702SNicholas Piggin * Therefore an NMI can clobber an HV interrupt's live HSPRG1 without noticing 376ccd47702SNicholas Piggin * that it is non-reentrant, which leads to random data corruption. 377ccd47702SNicholas Piggin * 378ccd47702SNicholas Piggin * The solution is for NMI interrupts in HV mode to check if they originated 379ccd47702SNicholas Piggin * from these critical HV interrupt regions. If so, then mark them not 380ccd47702SNicholas Piggin * recoverable. 381ccd47702SNicholas Piggin * 382ccd47702SNicholas Piggin * An alternative would be for HV NMIs to use SPRG for scratch to avoid the 383ccd47702SNicholas Piggin * HSPRG1 clobber, however this would cause guest SPRG to be clobbered. Linux 384ccd47702SNicholas Piggin * guests should always have MSR[RI]=0 when its scratch SPRG is in use, so 385ccd47702SNicholas Piggin * that would work. However any other guest OS that may have the SPRG live 386ccd47702SNicholas Piggin * and MSR[RI]=1 could encounter silent corruption. 387ccd47702SNicholas Piggin * 388ccd47702SNicholas Piggin * Builds that do not support KVM could take this second option to increase 389ccd47702SNicholas Piggin * the recoverability of NMIs. 390ccd47702SNicholas Piggin */ 391ccd47702SNicholas Piggin void hv_nmi_check_nonrecoverable(struct pt_regs *regs) 392ccd47702SNicholas Piggin { 393ccd47702SNicholas Piggin #ifdef CONFIG_PPC_POWERNV 394ccd47702SNicholas Piggin unsigned long kbase = (unsigned long)_stext; 395ccd47702SNicholas Piggin unsigned long nip = regs->nip; 396ccd47702SNicholas Piggin 397ccd47702SNicholas Piggin if (!(regs->msr & MSR_RI)) 398ccd47702SNicholas Piggin return; 399ccd47702SNicholas Piggin if (!(regs->msr & MSR_HV)) 400ccd47702SNicholas Piggin return; 401ccd47702SNicholas Piggin if (regs->msr & MSR_PR) 402ccd47702SNicholas Piggin return; 403ccd47702SNicholas Piggin 404ccd47702SNicholas Piggin /* 405ccd47702SNicholas Piggin * Now test if the interrupt has hit a range that may be using 406ccd47702SNicholas Piggin * HSPRG1 without having RI=0 (i.e., an HSRR interrupt). The 407ccd47702SNicholas Piggin * problem ranges all run un-relocated. Test real and virt modes 408*5c4a4802SBhaskar Chowdhury * at the same time by dropping the high bit of the nip (virt mode 409ccd47702SNicholas Piggin * entry points still have the +0x4000 offset). 410ccd47702SNicholas Piggin */ 411ccd47702SNicholas Piggin nip &= ~0xc000000000000000ULL; 412ccd47702SNicholas Piggin if ((nip >= 0x500 && nip < 0x600) || (nip >= 0x4500 && nip < 0x4600)) 413ccd47702SNicholas Piggin goto nonrecoverable; 414ccd47702SNicholas Piggin if ((nip >= 0x980 && nip < 0xa00) || (nip >= 0x4980 && nip < 0x4a00)) 415ccd47702SNicholas Piggin goto nonrecoverable; 416ccd47702SNicholas Piggin if ((nip >= 0xe00 && nip < 0xec0) || (nip >= 0x4e00 && nip < 0x4ec0)) 417ccd47702SNicholas Piggin goto nonrecoverable; 418ccd47702SNicholas Piggin if ((nip >= 0xf80 && nip < 0xfa0) || (nip >= 0x4f80 && nip < 0x4fa0)) 419ccd47702SNicholas Piggin goto nonrecoverable; 420bd3524feSNicholas Piggin 421ccd47702SNicholas Piggin /* Trampoline code runs un-relocated so subtract kbase. */ 422bd3524feSNicholas Piggin if (nip >= (unsigned long)(start_real_trampolines - kbase) && 423bd3524feSNicholas Piggin nip < (unsigned long)(end_real_trampolines - kbase)) 424ccd47702SNicholas Piggin goto nonrecoverable; 425bd3524feSNicholas Piggin if (nip >= (unsigned long)(start_virt_trampolines - kbase) && 426bd3524feSNicholas Piggin nip < (unsigned long)(end_virt_trampolines - kbase)) 427ccd47702SNicholas Piggin goto nonrecoverable; 428ccd47702SNicholas Piggin return; 429ccd47702SNicholas Piggin 430ccd47702SNicholas Piggin nonrecoverable: 431ccd47702SNicholas Piggin regs->msr &= ~MSR_RI; 432ccd47702SNicholas Piggin #endif 433ccd47702SNicholas Piggin } 4343a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(system_reset_exception) 43514cf11afSPaul Mackerras { 436cbf2ba95SNicholas Piggin unsigned long hsrr0, hsrr1; 437cbf2ba95SNicholas Piggin bool saved_hsrrs = false; 4382b4f3ac5SNicholas Piggin 439cbf2ba95SNicholas Piggin /* 440cbf2ba95SNicholas Piggin * System reset can interrupt code where HSRRs are live and MSR[RI]=1. 441cbf2ba95SNicholas Piggin * The system reset interrupt itself may clobber HSRRs (e.g., to call 442cbf2ba95SNicholas Piggin * OPAL), so save them here and restore them before returning. 443cbf2ba95SNicholas Piggin * 444cbf2ba95SNicholas Piggin * Machine checks don't need to save HSRRs, as the real mode handler 445cbf2ba95SNicholas Piggin * is careful to avoid them, and the regular handler is not delivered 446cbf2ba95SNicholas Piggin * as an NMI. 447cbf2ba95SNicholas Piggin */ 448cbf2ba95SNicholas Piggin if (cpu_has_feature(CPU_FTR_HVMODE)) { 449cbf2ba95SNicholas Piggin hsrr0 = mfspr(SPRN_HSRR0); 450cbf2ba95SNicholas Piggin hsrr1 = mfspr(SPRN_HSRR1); 451cbf2ba95SNicholas Piggin saved_hsrrs = true; 452cbf2ba95SNicholas Piggin } 453cbf2ba95SNicholas Piggin 454ccd47702SNicholas Piggin hv_nmi_check_nonrecoverable(regs); 455ccd47702SNicholas Piggin 456ca41ad43SNicholas Piggin __this_cpu_inc(irq_stat.sreset_irqs); 457ca41ad43SNicholas Piggin 45814cf11afSPaul Mackerras /* See if any machine dependent calls */ 459c902be71SArnd Bergmann if (ppc_md.system_reset_exception) { 460c902be71SArnd Bergmann if (ppc_md.system_reset_exception(regs)) 461c4f3b52cSNicholas Piggin goto out; 462c902be71SArnd Bergmann } 46314cf11afSPaul Mackerras 4644388c9b3SNicholas Piggin if (debugger(regs)) 4654388c9b3SNicholas Piggin goto out; 4664388c9b3SNicholas Piggin 467e7ca44edSGanesh Goudar kmsg_dump(KMSG_DUMP_OOPS); 4684388c9b3SNicholas Piggin /* 4694388c9b3SNicholas Piggin * A system reset is a request to dump, so we always send 4704388c9b3SNicholas Piggin * it through the crashdump code (if fadump or kdump are 4714388c9b3SNicholas Piggin * registered). 4724388c9b3SNicholas Piggin */ 4734388c9b3SNicholas Piggin crash_fadump(regs, "System Reset"); 4744388c9b3SNicholas Piggin 4754388c9b3SNicholas Piggin crash_kexec(regs); 4764388c9b3SNicholas Piggin 4774388c9b3SNicholas Piggin /* 4784388c9b3SNicholas Piggin * We aren't the primary crash CPU. We need to send it 4794388c9b3SNicholas Piggin * to a holding pattern to avoid it ending up in the panic 4804388c9b3SNicholas Piggin * code. 4814388c9b3SNicholas Piggin */ 4824388c9b3SNicholas Piggin crash_kexec_secondary(regs); 4834388c9b3SNicholas Piggin 4844388c9b3SNicholas Piggin /* 4854388c9b3SNicholas Piggin * No debugger or crash dump registered, print logs then 4864388c9b3SNicholas Piggin * panic. 4874388c9b3SNicholas Piggin */ 4884552d128SNicholas Piggin die("System Reset", regs, SIGABRT); 4894388c9b3SNicholas Piggin 4904388c9b3SNicholas Piggin mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */ 4914388c9b3SNicholas Piggin add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 4924388c9b3SNicholas Piggin nmi_panic(regs, "System Reset"); 49314cf11afSPaul Mackerras 494c4f3b52cSNicholas Piggin out: 495c4f3b52cSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64 496c4f3b52cSNicholas Piggin BUG_ON(get_paca()->in_nmi == 0); 497c4f3b52cSNicholas Piggin if (get_paca()->in_nmi > 1) 498265d6e58SNicholas Piggin die("Unrecoverable nested System Reset", regs, SIGABRT); 499c4f3b52cSNicholas Piggin #endif 50014cf11afSPaul Mackerras /* Must die if the interrupt is not recoverable */ 50111cb0a25SNicholas Piggin if (!(regs->msr & MSR_RI)) { 50211cb0a25SNicholas Piggin /* For the reason explained in die_mce, nmi_exit before die */ 50311cb0a25SNicholas Piggin nmi_exit(); 504265d6e58SNicholas Piggin die("Unrecoverable System Reset", regs, SIGABRT); 50511cb0a25SNicholas Piggin } 50614cf11afSPaul Mackerras 507cbf2ba95SNicholas Piggin if (saved_hsrrs) { 508cbf2ba95SNicholas Piggin mtspr(SPRN_HSRR0, hsrr0); 509cbf2ba95SNicholas Piggin mtspr(SPRN_HSRR1, hsrr1); 510cbf2ba95SNicholas Piggin } 511cbf2ba95SNicholas Piggin 51214cf11afSPaul Mackerras /* What should we do here? We could issue a shutdown or hard reset. */ 5133a96570fSNicholas Piggin 5143a96570fSNicholas Piggin return 0; 51514cf11afSPaul Mackerras } 5161e9b4507SMahesh Salgaonkar 51714cf11afSPaul Mackerras /* 51814cf11afSPaul Mackerras * I/O accesses can cause machine checks on powermacs. 51914cf11afSPaul Mackerras * Check if the NIP corresponds to the address of a sync 52014cf11afSPaul Mackerras * instruction for which there is an entry in the exception 52114cf11afSPaul Mackerras * table. 52214cf11afSPaul Mackerras * -- paulus. 52314cf11afSPaul Mackerras */ 52414cf11afSPaul Mackerras static inline int check_io_access(struct pt_regs *regs) 52514cf11afSPaul Mackerras { 52668a64357SBenjamin Herrenschmidt #ifdef CONFIG_PPC32 52714cf11afSPaul Mackerras unsigned long msr = regs->msr; 52814cf11afSPaul Mackerras const struct exception_table_entry *entry; 52914cf11afSPaul Mackerras unsigned int *nip = (unsigned int *)regs->nip; 53014cf11afSPaul Mackerras 53114cf11afSPaul Mackerras if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000))) 53214cf11afSPaul Mackerras && (entry = search_exception_tables(regs->nip)) != NULL) { 53314cf11afSPaul Mackerras /* 53414cf11afSPaul Mackerras * Check that it's a sync instruction, or somewhere 53514cf11afSPaul Mackerras * in the twi; isync; nop sequence that inb/inw/inl uses. 53614cf11afSPaul Mackerras * As the address is in the exception table 53714cf11afSPaul Mackerras * we should be able to read the instr there. 53814cf11afSPaul Mackerras * For the debug message, we look at the preceding 53914cf11afSPaul Mackerras * load or store. 54014cf11afSPaul Mackerras */ 541ddc6cd0dSChristophe Leroy if (*nip == PPC_INST_NOP) 54214cf11afSPaul Mackerras nip -= 2; 543ddc6cd0dSChristophe Leroy else if (*nip == PPC_INST_ISYNC) 54414cf11afSPaul Mackerras --nip; 545ddc6cd0dSChristophe Leroy if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) { 54614cf11afSPaul Mackerras unsigned int rb; 54714cf11afSPaul Mackerras 54814cf11afSPaul Mackerras --nip; 54914cf11afSPaul Mackerras rb = (*nip >> 11) & 0x1f; 55014cf11afSPaul Mackerras printk(KERN_DEBUG "%s bad port %lx at %p\n", 55114cf11afSPaul Mackerras (*nip & 0x100)? "OUT to": "IN from", 55214cf11afSPaul Mackerras regs->gpr[rb] - _IO_BASE, nip); 55314cf11afSPaul Mackerras regs->msr |= MSR_RI; 55461a92f70SNicholas Piggin regs->nip = extable_fixup(entry); 55514cf11afSPaul Mackerras return 1; 55614cf11afSPaul Mackerras } 55714cf11afSPaul Mackerras } 55868a64357SBenjamin Herrenschmidt #endif /* CONFIG_PPC32 */ 55914cf11afSPaul Mackerras return 0; 56014cf11afSPaul Mackerras } 56114cf11afSPaul Mackerras 562172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS 56314cf11afSPaul Mackerras /* On 4xx, the reason for the machine check or program exception 56414cf11afSPaul Mackerras is in the ESR. */ 56514cf11afSPaul Mackerras #define get_reason(regs) ((regs)->dsisr) 56614cf11afSPaul Mackerras #define REASON_FP ESR_FP 56714cf11afSPaul Mackerras #define REASON_ILLEGAL (ESR_PIL | ESR_PUO) 56814cf11afSPaul Mackerras #define REASON_PRIVILEGED ESR_PPR 56914cf11afSPaul Mackerras #define REASON_TRAP ESR_PTR 5709409d2f9SJordan Niethe #define REASON_PREFIXED 0 5719409d2f9SJordan Niethe #define REASON_BOUNDARY 0 57214cf11afSPaul Mackerras 57314cf11afSPaul Mackerras /* single-step stuff */ 57451ae8d4aSBharat Bhushan #define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC) 57551ae8d4aSBharat Bhushan #define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC) 5760e524e76SMatt Evans #define clear_br_trace(regs) do {} while(0) 57714cf11afSPaul Mackerras #else 57814cf11afSPaul Mackerras /* On non-4xx, the reason for the machine check or program 57914cf11afSPaul Mackerras exception is in the MSR. */ 58014cf11afSPaul Mackerras #define get_reason(regs) ((regs)->msr) 581d30a5a52SMichael Ellerman #define REASON_TM SRR1_PROGTM 582d30a5a52SMichael Ellerman #define REASON_FP SRR1_PROGFPE 583d30a5a52SMichael Ellerman #define REASON_ILLEGAL SRR1_PROGILL 584d30a5a52SMichael Ellerman #define REASON_PRIVILEGED SRR1_PROGPRIV 585d30a5a52SMichael Ellerman #define REASON_TRAP SRR1_PROGTRAP 5869409d2f9SJordan Niethe #define REASON_PREFIXED SRR1_PREFIXED 5879409d2f9SJordan Niethe #define REASON_BOUNDARY SRR1_BOUNDARY 58814cf11afSPaul Mackerras 58914cf11afSPaul Mackerras #define single_stepping(regs) ((regs)->msr & MSR_SE) 59014cf11afSPaul Mackerras #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE) 5910e524e76SMatt Evans #define clear_br_trace(regs) ((regs)->msr &= ~MSR_BE) 59214cf11afSPaul Mackerras #endif 59314cf11afSPaul Mackerras 5949409d2f9SJordan Niethe #define inst_length(reason) (((reason) & REASON_PREFIXED) ? 8 : 4) 5959409d2f9SJordan Niethe 5960d0935b3SMichael Ellerman #if defined(CONFIG_E500) 597fe04b112SScott Wood int machine_check_e500mc(struct pt_regs *regs) 598fe04b112SScott Wood { 599fe04b112SScott Wood unsigned long mcsr = mfspr(SPRN_MCSR); 600a4e89ffbSMatt Weber unsigned long pvr = mfspr(SPRN_PVR); 601fe04b112SScott Wood unsigned long reason = mcsr; 602fe04b112SScott Wood int recoverable = 1; 603fe04b112SScott Wood 60482a9a480SScott Wood if (reason & MCSR_LD) { 605cce1f106SShaohui Xie recoverable = fsl_rio_mcheck_exception(regs); 606cce1f106SShaohui Xie if (recoverable == 1) 607cce1f106SShaohui Xie goto silent_out; 608cce1f106SShaohui Xie } 609cce1f106SShaohui Xie 610fe04b112SScott Wood printk("Machine check in kernel mode.\n"); 611fe04b112SScott Wood printk("Caused by (from MCSR=%lx): ", reason); 612fe04b112SScott Wood 613fe04b112SScott Wood if (reason & MCSR_MCP) 614422123ccSChristophe Leroy pr_cont("Machine Check Signal\n"); 615fe04b112SScott Wood 616fe04b112SScott Wood if (reason & MCSR_ICPERR) { 617422123ccSChristophe Leroy pr_cont("Instruction Cache Parity Error\n"); 618fe04b112SScott Wood 619fe04b112SScott Wood /* 620fe04b112SScott Wood * This is recoverable by invalidating the i-cache. 621fe04b112SScott Wood */ 622fe04b112SScott Wood mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI); 623fe04b112SScott Wood while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI) 624fe04b112SScott Wood ; 625fe04b112SScott Wood 626fe04b112SScott Wood /* 627fe04b112SScott Wood * This will generally be accompanied by an instruction 628fe04b112SScott Wood * fetch error report -- only treat MCSR_IF as fatal 629fe04b112SScott Wood * if it wasn't due to an L1 parity error. 630fe04b112SScott Wood */ 631fe04b112SScott Wood reason &= ~MCSR_IF; 632fe04b112SScott Wood } 633fe04b112SScott Wood 634fe04b112SScott Wood if (reason & MCSR_DCPERR_MC) { 635422123ccSChristophe Leroy pr_cont("Data Cache Parity Error\n"); 63637caf9f2SKumar Gala 63737caf9f2SKumar Gala /* 63837caf9f2SKumar Gala * In write shadow mode we auto-recover from the error, but it 63937caf9f2SKumar Gala * may still get logged and cause a machine check. We should 64037caf9f2SKumar Gala * only treat the non-write shadow case as non-recoverable. 64137caf9f2SKumar Gala */ 642a4e89ffbSMatt Weber /* On e6500 core, L1 DCWS (Data cache write shadow mode) bit 643a4e89ffbSMatt Weber * is not implemented but L1 data cache always runs in write 644a4e89ffbSMatt Weber * shadow mode. Hence on data cache parity errors HW will 645a4e89ffbSMatt Weber * automatically invalidate the L1 Data Cache. 646a4e89ffbSMatt Weber */ 647a4e89ffbSMatt Weber if (PVR_VER(pvr) != PVR_VER_E6500) { 64837caf9f2SKumar Gala if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS)) 649fe04b112SScott Wood recoverable = 0; 650fe04b112SScott Wood } 651a4e89ffbSMatt Weber } 652fe04b112SScott Wood 653fe04b112SScott Wood if (reason & MCSR_L2MMU_MHIT) { 654422123ccSChristophe Leroy pr_cont("Hit on multiple TLB entries\n"); 655fe04b112SScott Wood recoverable = 0; 656fe04b112SScott Wood } 657fe04b112SScott Wood 658fe04b112SScott Wood if (reason & MCSR_NMI) 659422123ccSChristophe Leroy pr_cont("Non-maskable interrupt\n"); 660fe04b112SScott Wood 661fe04b112SScott Wood if (reason & MCSR_IF) { 662422123ccSChristophe Leroy pr_cont("Instruction Fetch Error Report\n"); 663fe04b112SScott Wood recoverable = 0; 664fe04b112SScott Wood } 665fe04b112SScott Wood 666fe04b112SScott Wood if (reason & MCSR_LD) { 667422123ccSChristophe Leroy pr_cont("Load Error Report\n"); 668fe04b112SScott Wood recoverable = 0; 669fe04b112SScott Wood } 670fe04b112SScott Wood 671fe04b112SScott Wood if (reason & MCSR_ST) { 672422123ccSChristophe Leroy pr_cont("Store Error Report\n"); 673fe04b112SScott Wood recoverable = 0; 674fe04b112SScott Wood } 675fe04b112SScott Wood 676fe04b112SScott Wood if (reason & MCSR_LDG) { 677422123ccSChristophe Leroy pr_cont("Guarded Load Error Report\n"); 678fe04b112SScott Wood recoverable = 0; 679fe04b112SScott Wood } 680fe04b112SScott Wood 681fe04b112SScott Wood if (reason & MCSR_TLBSYNC) 682422123ccSChristophe Leroy pr_cont("Simultaneous tlbsync operations\n"); 683fe04b112SScott Wood 684fe04b112SScott Wood if (reason & MCSR_BSL2_ERR) { 685422123ccSChristophe Leroy pr_cont("Level 2 Cache Error\n"); 686fe04b112SScott Wood recoverable = 0; 687fe04b112SScott Wood } 688fe04b112SScott Wood 689fe04b112SScott Wood if (reason & MCSR_MAV) { 690fe04b112SScott Wood u64 addr; 691fe04b112SScott Wood 692fe04b112SScott Wood addr = mfspr(SPRN_MCAR); 693fe04b112SScott Wood addr |= (u64)mfspr(SPRN_MCARU) << 32; 694fe04b112SScott Wood 695422123ccSChristophe Leroy pr_cont("Machine Check %s Address: %#llx\n", 696fe04b112SScott Wood reason & MCSR_MEA ? "Effective" : "Physical", addr); 697fe04b112SScott Wood } 698fe04b112SScott Wood 699cce1f106SShaohui Xie silent_out: 700fe04b112SScott Wood mtspr(SPRN_MCSR, mcsr); 701fe04b112SScott Wood return mfspr(SPRN_MCSR) == 0 && recoverable; 702fe04b112SScott Wood } 703fe04b112SScott Wood 70447c0bd1aSBenjamin Herrenschmidt int machine_check_e500(struct pt_regs *regs) 70547c0bd1aSBenjamin Herrenschmidt { 70642bff234SMichael Ellerman unsigned long reason = mfspr(SPRN_MCSR); 70747c0bd1aSBenjamin Herrenschmidt 708cce1f106SShaohui Xie if (reason & MCSR_BUS_RBERR) { 709cce1f106SShaohui Xie if (fsl_rio_mcheck_exception(regs)) 710cce1f106SShaohui Xie return 1; 7114e0e3435SHongtao Jia if (fsl_pci_mcheck_exception(regs)) 7124e0e3435SHongtao Jia return 1; 713cce1f106SShaohui Xie } 714cce1f106SShaohui Xie 71514cf11afSPaul Mackerras printk("Machine check in kernel mode.\n"); 71614cf11afSPaul Mackerras printk("Caused by (from MCSR=%lx): ", reason); 71714cf11afSPaul Mackerras 71814cf11afSPaul Mackerras if (reason & MCSR_MCP) 719422123ccSChristophe Leroy pr_cont("Machine Check Signal\n"); 72014cf11afSPaul Mackerras if (reason & MCSR_ICPERR) 721422123ccSChristophe Leroy pr_cont("Instruction Cache Parity Error\n"); 72214cf11afSPaul Mackerras if (reason & MCSR_DCP_PERR) 723422123ccSChristophe Leroy pr_cont("Data Cache Push Parity Error\n"); 72414cf11afSPaul Mackerras if (reason & MCSR_DCPERR) 725422123ccSChristophe Leroy pr_cont("Data Cache Parity Error\n"); 72614cf11afSPaul Mackerras if (reason & MCSR_BUS_IAERR) 727422123ccSChristophe Leroy pr_cont("Bus - Instruction Address Error\n"); 72814cf11afSPaul Mackerras if (reason & MCSR_BUS_RAERR) 729422123ccSChristophe Leroy pr_cont("Bus - Read Address Error\n"); 73014cf11afSPaul Mackerras if (reason & MCSR_BUS_WAERR) 731422123ccSChristophe Leroy pr_cont("Bus - Write Address Error\n"); 73214cf11afSPaul Mackerras if (reason & MCSR_BUS_IBERR) 733422123ccSChristophe Leroy pr_cont("Bus - Instruction Data Error\n"); 73414cf11afSPaul Mackerras if (reason & MCSR_BUS_RBERR) 735422123ccSChristophe Leroy pr_cont("Bus - Read Data Bus Error\n"); 73614cf11afSPaul Mackerras if (reason & MCSR_BUS_WBERR) 737422123ccSChristophe Leroy pr_cont("Bus - Write Data Bus Error\n"); 73814cf11afSPaul Mackerras if (reason & MCSR_BUS_IPERR) 739422123ccSChristophe Leroy pr_cont("Bus - Instruction Parity Error\n"); 74014cf11afSPaul Mackerras if (reason & MCSR_BUS_RPERR) 741422123ccSChristophe Leroy pr_cont("Bus - Read Parity Error\n"); 74247c0bd1aSBenjamin Herrenschmidt 74347c0bd1aSBenjamin Herrenschmidt return 0; 74447c0bd1aSBenjamin Herrenschmidt } 7454490c06bSKumar Gala 7464490c06bSKumar Gala int machine_check_generic(struct pt_regs *regs) 7474490c06bSKumar Gala { 7484490c06bSKumar Gala return 0; 7494490c06bSKumar Gala } 7507f3f819eSMichael Ellerman #elif defined(CONFIG_PPC32) 75147c0bd1aSBenjamin Herrenschmidt int machine_check_generic(struct pt_regs *regs) 75247c0bd1aSBenjamin Herrenschmidt { 75342bff234SMichael Ellerman unsigned long reason = regs->msr; 75447c0bd1aSBenjamin Herrenschmidt 75514cf11afSPaul Mackerras printk("Machine check in kernel mode.\n"); 75614cf11afSPaul Mackerras printk("Caused by (from SRR1=%lx): ", reason); 75714cf11afSPaul Mackerras switch (reason & 0x601F0000) { 75814cf11afSPaul Mackerras case 0x80000: 759422123ccSChristophe Leroy pr_cont("Machine check signal\n"); 76014cf11afSPaul Mackerras break; 76114cf11afSPaul Mackerras case 0x40000: 76214cf11afSPaul Mackerras case 0x140000: /* 7450 MSS error and TEA */ 763422123ccSChristophe Leroy pr_cont("Transfer error ack signal\n"); 76414cf11afSPaul Mackerras break; 76514cf11afSPaul Mackerras case 0x20000: 766422123ccSChristophe Leroy pr_cont("Data parity error signal\n"); 76714cf11afSPaul Mackerras break; 76814cf11afSPaul Mackerras case 0x10000: 769422123ccSChristophe Leroy pr_cont("Address parity error signal\n"); 77014cf11afSPaul Mackerras break; 77114cf11afSPaul Mackerras case 0x20000000: 772422123ccSChristophe Leroy pr_cont("L1 Data Cache error\n"); 77314cf11afSPaul Mackerras break; 77414cf11afSPaul Mackerras case 0x40000000: 775422123ccSChristophe Leroy pr_cont("L1 Instruction Cache error\n"); 77614cf11afSPaul Mackerras break; 77714cf11afSPaul Mackerras case 0x00100000: 778422123ccSChristophe Leroy pr_cont("L2 data cache parity error\n"); 77914cf11afSPaul Mackerras break; 78014cf11afSPaul Mackerras default: 781422123ccSChristophe Leroy pr_cont("Unknown values in msr\n"); 78214cf11afSPaul Mackerras } 78375918a4bSOlof Johansson return 0; 78475918a4bSOlof Johansson } 78547c0bd1aSBenjamin Herrenschmidt #endif /* everything else */ 78675918a4bSOlof Johansson 787209e9d50SNicholas Piggin void die_mce(const char *str, struct pt_regs *regs, long err) 788209e9d50SNicholas Piggin { 789209e9d50SNicholas Piggin /* 790209e9d50SNicholas Piggin * The machine check wants to kill the interrupted context, but 791209e9d50SNicholas Piggin * do_exit() checks for in_interrupt() and panics in that case, so 792209e9d50SNicholas Piggin * exit the irq/nmi before calling die. 793209e9d50SNicholas Piggin */ 7941b1b6a6fSNicholas Piggin if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) 7951b1b6a6fSNicholas Piggin irq_exit(); 7961b1b6a6fSNicholas Piggin else 797209e9d50SNicholas Piggin nmi_exit(); 798209e9d50SNicholas Piggin die(str, regs, err); 799209e9d50SNicholas Piggin } 800209e9d50SNicholas Piggin 801118178e6SNicholas Piggin /* 802118178e6SNicholas Piggin * BOOK3S_64 does not call this handler as a non-maskable interrupt 803118178e6SNicholas Piggin * (it uses its own early real-mode handler to handle the MCE proper 804118178e6SNicholas Piggin * and then raises irq_work to call this handler when interrupts are 805118178e6SNicholas Piggin * enabled). 806118178e6SNicholas Piggin */ 8073a96570fSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64 8083a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(machine_check_exception) 8093a96570fSNicholas Piggin #else 8103a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(machine_check_exception) 8113a96570fSNicholas Piggin #endif 81275918a4bSOlof Johansson { 81375918a4bSOlof Johansson int recover = 0; 81469ea03b5SPeter Zijlstra 81569111bacSChristoph Lameter __this_cpu_inc(irq_stat.mce_exceptions); 81689713ed1SAnton Blanchard 817d93b0ac0SMahesh Salgaonkar add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE); 818d93b0ac0SMahesh Salgaonkar 81947c0bd1aSBenjamin Herrenschmidt /* See if any machine dependent calls. In theory, we would want 82047c0bd1aSBenjamin Herrenschmidt * to call the CPU first, and call the ppc_md. one if the CPU 82147c0bd1aSBenjamin Herrenschmidt * one returns a positive number. However there is existing code 82247c0bd1aSBenjamin Herrenschmidt * that assumes the board gets a first chance, so let's keep it 82347c0bd1aSBenjamin Herrenschmidt * that way for now and fix things later. --BenH. 82447c0bd1aSBenjamin Herrenschmidt */ 82575918a4bSOlof Johansson if (ppc_md.machine_check_exception) 82675918a4bSOlof Johansson recover = ppc_md.machine_check_exception(regs); 82747c0bd1aSBenjamin Herrenschmidt else if (cur_cpu_spec->machine_check) 82847c0bd1aSBenjamin Herrenschmidt recover = cur_cpu_spec->machine_check(regs); 82975918a4bSOlof Johansson 83047c0bd1aSBenjamin Herrenschmidt if (recover > 0) 831ba12eedeSLi Zhong goto bail; 83275918a4bSOlof Johansson 833a443506bSAnton Blanchard if (debugger_fault_handler(regs)) 834ba12eedeSLi Zhong goto bail; 83575918a4bSOlof Johansson 83675918a4bSOlof Johansson if (check_io_access(regs)) 837ba12eedeSLi Zhong goto bail; 83875918a4bSOlof Johansson 839209e9d50SNicholas Piggin die_mce("Machine check", regs, SIGBUS); 840daf00ae7SChristophe Leroy 841c538938fSNicholas Piggin bail: 8420bbea75cSChristophe Leroy /* Must die if the interrupt is not recoverable */ 8430bbea75cSChristophe Leroy if (!(regs->msr & MSR_RI)) 844209e9d50SNicholas Piggin die_mce("Unrecoverable Machine check", regs, SIGBUS); 845daf00ae7SChristophe Leroy 8463a96570fSNicholas Piggin #ifdef CONFIG_PPC_BOOK3S_64 8473a96570fSNicholas Piggin return; 8483a96570fSNicholas Piggin #else 8493a96570fSNicholas Piggin return 0; 8503a96570fSNicholas Piggin #endif 85114cf11afSPaul Mackerras } 85214cf11afSPaul Mackerras 8533a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SMIException) /* async? */ 85414cf11afSPaul Mackerras { 85514cf11afSPaul Mackerras die("System Management Interrupt", regs, SIGABRT); 85614cf11afSPaul Mackerras } 85714cf11afSPaul Mackerras 8585080332cSMichael Neuling #ifdef CONFIG_VSX 8595080332cSMichael Neuling static void p9_hmi_special_emu(struct pt_regs *regs) 8605080332cSMichael Neuling { 8615080332cSMichael Neuling unsigned int ra, rb, t, i, sel, instr, rc; 8625080332cSMichael Neuling const void __user *addr; 8631da4a027SMichael Neuling u8 vbuf[16] __aligned(16), *vdst; 8645080332cSMichael Neuling unsigned long ea, msr, msr_mask; 8655080332cSMichael Neuling bool swap; 8665080332cSMichael Neuling 8675080332cSMichael Neuling if (__get_user_inatomic(instr, (unsigned int __user *)regs->nip)) 8685080332cSMichael Neuling return; 8695080332cSMichael Neuling 8705080332cSMichael Neuling /* 8715080332cSMichael Neuling * lxvb16x opcode: 0x7c0006d8 8725080332cSMichael Neuling * lxvd2x opcode: 0x7c000698 8735080332cSMichael Neuling * lxvh8x opcode: 0x7c000658 8745080332cSMichael Neuling * lxvw4x opcode: 0x7c000618 8755080332cSMichael Neuling */ 8765080332cSMichael Neuling if ((instr & 0xfc00073e) != 0x7c000618) { 8775080332cSMichael Neuling pr_devel("HMI vec emu: not vector CI %i:%s[%d] nip=%016lx" 8785080332cSMichael Neuling " instr=%08x\n", 8795080332cSMichael Neuling smp_processor_id(), current->comm, current->pid, 8805080332cSMichael Neuling regs->nip, instr); 8815080332cSMichael Neuling return; 8825080332cSMichael Neuling } 8835080332cSMichael Neuling 8845080332cSMichael Neuling /* Grab vector registers into the task struct */ 8855080332cSMichael Neuling msr = regs->msr; /* Grab msr before we flush the bits */ 8865080332cSMichael Neuling flush_vsx_to_thread(current); 8875080332cSMichael Neuling enable_kernel_altivec(); 8885080332cSMichael Neuling 8895080332cSMichael Neuling /* 8905080332cSMichael Neuling * Is userspace running with a different endian (this is rare but 8915080332cSMichael Neuling * not impossible) 8925080332cSMichael Neuling */ 8935080332cSMichael Neuling swap = (msr & MSR_LE) != (MSR_KERNEL & MSR_LE); 8945080332cSMichael Neuling 8955080332cSMichael Neuling /* Decode the instruction */ 8965080332cSMichael Neuling ra = (instr >> 16) & 0x1f; 8975080332cSMichael Neuling rb = (instr >> 11) & 0x1f; 8985080332cSMichael Neuling t = (instr >> 21) & 0x1f; 8995080332cSMichael Neuling if (instr & 1) 9005080332cSMichael Neuling vdst = (u8 *)¤t->thread.vr_state.vr[t]; 9015080332cSMichael Neuling else 9025080332cSMichael Neuling vdst = (u8 *)¤t->thread.fp_state.fpr[t][0]; 9035080332cSMichael Neuling 9045080332cSMichael Neuling /* Grab the vector address */ 9055080332cSMichael Neuling ea = regs->gpr[rb] + (ra ? regs->gpr[ra] : 0); 9065080332cSMichael Neuling if (is_32bit_task()) 9075080332cSMichael Neuling ea &= 0xfffffffful; 9085080332cSMichael Neuling addr = (__force const void __user *)ea; 9095080332cSMichael Neuling 9105080332cSMichael Neuling /* Check it */ 91196d4f267SLinus Torvalds if (!access_ok(addr, 16)) { 9125080332cSMichael Neuling pr_devel("HMI vec emu: bad access %i:%s[%d] nip=%016lx" 9135080332cSMichael Neuling " instr=%08x addr=%016lx\n", 9145080332cSMichael Neuling smp_processor_id(), current->comm, current->pid, 9155080332cSMichael Neuling regs->nip, instr, (unsigned long)addr); 9165080332cSMichael Neuling return; 9175080332cSMichael Neuling } 9185080332cSMichael Neuling 9195080332cSMichael Neuling /* Read the vector */ 9205080332cSMichael Neuling rc = 0; 9215080332cSMichael Neuling if ((unsigned long)addr & 0xfUL) 9225080332cSMichael Neuling /* unaligned case */ 9235080332cSMichael Neuling rc = __copy_from_user_inatomic(vbuf, addr, 16); 9245080332cSMichael Neuling else 9255080332cSMichael Neuling __get_user_atomic_128_aligned(vbuf, addr, rc); 9265080332cSMichael Neuling if (rc) { 9275080332cSMichael Neuling pr_devel("HMI vec emu: page fault %i:%s[%d] nip=%016lx" 9285080332cSMichael Neuling " instr=%08x addr=%016lx\n", 9295080332cSMichael Neuling smp_processor_id(), current->comm, current->pid, 9305080332cSMichael Neuling regs->nip, instr, (unsigned long)addr); 9315080332cSMichael Neuling return; 9325080332cSMichael Neuling } 9335080332cSMichael Neuling 9345080332cSMichael Neuling pr_devel("HMI vec emu: emulated vector CI %i:%s[%d] nip=%016lx" 9355080332cSMichael Neuling " instr=%08x addr=%016lx\n", 9365080332cSMichael Neuling smp_processor_id(), current->comm, current->pid, regs->nip, 9375080332cSMichael Neuling instr, (unsigned long) addr); 9385080332cSMichael Neuling 9395080332cSMichael Neuling /* Grab instruction "selector" */ 9405080332cSMichael Neuling sel = (instr >> 6) & 3; 9415080332cSMichael Neuling 9425080332cSMichael Neuling /* 9435080332cSMichael Neuling * Check to make sure the facility is actually enabled. This 9445080332cSMichael Neuling * could happen if we get a false positive hit. 9455080332cSMichael Neuling * 9465080332cSMichael Neuling * lxvd2x/lxvw4x always check MSR VSX sel = 0,2 9475080332cSMichael Neuling * lxvh8x/lxvb16x check MSR VSX or VEC depending on VSR used sel = 1,3 9485080332cSMichael Neuling */ 9495080332cSMichael Neuling msr_mask = MSR_VSX; 9505080332cSMichael Neuling if ((sel & 1) && (instr & 1)) /* lxvh8x & lxvb16x + VSR >= 32 */ 9515080332cSMichael Neuling msr_mask = MSR_VEC; 9525080332cSMichael Neuling if (!(msr & msr_mask)) { 9535080332cSMichael Neuling pr_devel("HMI vec emu: MSR fac clear %i:%s[%d] nip=%016lx" 9545080332cSMichael Neuling " instr=%08x msr:%016lx\n", 9555080332cSMichael Neuling smp_processor_id(), current->comm, current->pid, 9565080332cSMichael Neuling regs->nip, instr, msr); 9575080332cSMichael Neuling return; 9585080332cSMichael Neuling } 9595080332cSMichael Neuling 9605080332cSMichael Neuling /* Do logging here before we modify sel based on endian */ 9615080332cSMichael Neuling switch (sel) { 9625080332cSMichael Neuling case 0: /* lxvw4x */ 9635080332cSMichael Neuling PPC_WARN_EMULATED(lxvw4x, regs); 9645080332cSMichael Neuling break; 9655080332cSMichael Neuling case 1: /* lxvh8x */ 9665080332cSMichael Neuling PPC_WARN_EMULATED(lxvh8x, regs); 9675080332cSMichael Neuling break; 9685080332cSMichael Neuling case 2: /* lxvd2x */ 9695080332cSMichael Neuling PPC_WARN_EMULATED(lxvd2x, regs); 9705080332cSMichael Neuling break; 9715080332cSMichael Neuling case 3: /* lxvb16x */ 9725080332cSMichael Neuling PPC_WARN_EMULATED(lxvb16x, regs); 9735080332cSMichael Neuling break; 9745080332cSMichael Neuling } 9755080332cSMichael Neuling 9765080332cSMichael Neuling #ifdef __LITTLE_ENDIAN__ 9775080332cSMichael Neuling /* 9785080332cSMichael Neuling * An LE kernel stores the vector in the task struct as an LE 9795080332cSMichael Neuling * byte array (effectively swapping both the components and 9805080332cSMichael Neuling * the content of the components). Those instructions expect 9815080332cSMichael Neuling * the components to remain in ascending address order, so we 9825080332cSMichael Neuling * swap them back. 9835080332cSMichael Neuling * 9845080332cSMichael Neuling * If we are running a BE user space, the expectation is that 9855080332cSMichael Neuling * of a simple memcpy, so forcing the emulation to look like 9865080332cSMichael Neuling * a lxvb16x should do the trick. 9875080332cSMichael Neuling */ 9885080332cSMichael Neuling if (swap) 9895080332cSMichael Neuling sel = 3; 9905080332cSMichael Neuling 9915080332cSMichael Neuling switch (sel) { 9925080332cSMichael Neuling case 0: /* lxvw4x */ 9935080332cSMichael Neuling for (i = 0; i < 4; i++) 9945080332cSMichael Neuling ((u32 *)vdst)[i] = ((u32 *)vbuf)[3-i]; 9955080332cSMichael Neuling break; 9965080332cSMichael Neuling case 1: /* lxvh8x */ 9975080332cSMichael Neuling for (i = 0; i < 8; i++) 9985080332cSMichael Neuling ((u16 *)vdst)[i] = ((u16 *)vbuf)[7-i]; 9995080332cSMichael Neuling break; 10005080332cSMichael Neuling case 2: /* lxvd2x */ 10015080332cSMichael Neuling for (i = 0; i < 2; i++) 10025080332cSMichael Neuling ((u64 *)vdst)[i] = ((u64 *)vbuf)[1-i]; 10035080332cSMichael Neuling break; 10045080332cSMichael Neuling case 3: /* lxvb16x */ 10055080332cSMichael Neuling for (i = 0; i < 16; i++) 10065080332cSMichael Neuling vdst[i] = vbuf[15-i]; 10075080332cSMichael Neuling break; 10085080332cSMichael Neuling } 10095080332cSMichael Neuling #else /* __LITTLE_ENDIAN__ */ 10105080332cSMichael Neuling /* On a big endian kernel, a BE userspace only needs a memcpy */ 10115080332cSMichael Neuling if (!swap) 10125080332cSMichael Neuling sel = 3; 10135080332cSMichael Neuling 10145080332cSMichael Neuling /* Otherwise, we need to swap the content of the components */ 10155080332cSMichael Neuling switch (sel) { 10165080332cSMichael Neuling case 0: /* lxvw4x */ 10175080332cSMichael Neuling for (i = 0; i < 4; i++) 10185080332cSMichael Neuling ((u32 *)vdst)[i] = cpu_to_le32(((u32 *)vbuf)[i]); 10195080332cSMichael Neuling break; 10205080332cSMichael Neuling case 1: /* lxvh8x */ 10215080332cSMichael Neuling for (i = 0; i < 8; i++) 10225080332cSMichael Neuling ((u16 *)vdst)[i] = cpu_to_le16(((u16 *)vbuf)[i]); 10235080332cSMichael Neuling break; 10245080332cSMichael Neuling case 2: /* lxvd2x */ 10255080332cSMichael Neuling for (i = 0; i < 2; i++) 10265080332cSMichael Neuling ((u64 *)vdst)[i] = cpu_to_le64(((u64 *)vbuf)[i]); 10275080332cSMichael Neuling break; 10285080332cSMichael Neuling case 3: /* lxvb16x */ 10295080332cSMichael Neuling memcpy(vdst, vbuf, 16); 10305080332cSMichael Neuling break; 10315080332cSMichael Neuling } 10325080332cSMichael Neuling #endif /* !__LITTLE_ENDIAN__ */ 10335080332cSMichael Neuling 10345080332cSMichael Neuling /* Go to next instruction */ 10355080332cSMichael Neuling regs->nip += 4; 10365080332cSMichael Neuling } 10375080332cSMichael Neuling #endif /* CONFIG_VSX */ 10385080332cSMichael Neuling 10393a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(handle_hmi_exception) 10400869b6fdSMahesh Salgaonkar { 10410869b6fdSMahesh Salgaonkar struct pt_regs *old_regs; 10420869b6fdSMahesh Salgaonkar 10430869b6fdSMahesh Salgaonkar old_regs = set_irq_regs(regs); 10440869b6fdSMahesh Salgaonkar 10455080332cSMichael Neuling #ifdef CONFIG_VSX 10465080332cSMichael Neuling /* Real mode flagged P9 special emu is needed */ 10475080332cSMichael Neuling if (local_paca->hmi_p9_special_emu) { 10485080332cSMichael Neuling local_paca->hmi_p9_special_emu = 0; 10495080332cSMichael Neuling 10505080332cSMichael Neuling /* 10515080332cSMichael Neuling * We don't want to take page faults while doing the 10525080332cSMichael Neuling * emulation, we just replay the instruction if necessary. 10535080332cSMichael Neuling */ 10545080332cSMichael Neuling pagefault_disable(); 10555080332cSMichael Neuling p9_hmi_special_emu(regs); 10565080332cSMichael Neuling pagefault_enable(); 10575080332cSMichael Neuling } 10585080332cSMichael Neuling #endif /* CONFIG_VSX */ 10595080332cSMichael Neuling 10600869b6fdSMahesh Salgaonkar if (ppc_md.handle_hmi_exception) 10610869b6fdSMahesh Salgaonkar ppc_md.handle_hmi_exception(regs); 10620869b6fdSMahesh Salgaonkar 10630869b6fdSMahesh Salgaonkar set_irq_regs(old_regs); 10640869b6fdSMahesh Salgaonkar } 10650869b6fdSMahesh Salgaonkar 10663a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(unknown_exception) 106714cf11afSPaul Mackerras { 106814cf11afSPaul Mackerras printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n", 106914cf11afSPaul Mackerras regs->nip, regs->msr, regs->trap); 107014cf11afSPaul Mackerras 1071e821fa42SEric W. Biederman _exception(SIGTRAP, regs, TRAP_UNK, 0); 107214cf11afSPaul Mackerras } 107314cf11afSPaul Mackerras 10743a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(unknown_async_exception) 10756c6aee00SNicholas Piggin { 10766c6aee00SNicholas Piggin printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n", 10776c6aee00SNicholas Piggin regs->nip, regs->msr, regs->trap); 10786c6aee00SNicholas Piggin 10796c6aee00SNicholas Piggin _exception(SIGTRAP, regs, TRAP_UNK, 0); 10806c6aee00SNicholas Piggin } 10816c6aee00SNicholas Piggin 10823a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(instruction_breakpoint_exception) 108314cf11afSPaul Mackerras { 108414cf11afSPaul Mackerras if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5, 108514cf11afSPaul Mackerras 5, SIGTRAP) == NOTIFY_STOP) 1086540d4d34SNicholas Piggin return; 108714cf11afSPaul Mackerras if (debugger_iabr_match(regs)) 1088540d4d34SNicholas Piggin return; 108914cf11afSPaul Mackerras _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip); 109014cf11afSPaul Mackerras } 109114cf11afSPaul Mackerras 10923a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(RunModeException) 109314cf11afSPaul Mackerras { 1094e821fa42SEric W. Biederman _exception(SIGTRAP, regs, TRAP_UNK, 0); 109514cf11afSPaul Mackerras } 109614cf11afSPaul Mackerras 10973a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(single_step_exception) 109814cf11afSPaul Mackerras { 10992538c2d0SK.Prasad clear_single_step(regs); 11000e524e76SMatt Evans clear_br_trace(regs); 110114cf11afSPaul Mackerras 11026cc89badSNaveen N. Rao if (kprobe_post_handler(regs)) 11036cc89badSNaveen N. Rao return; 11046cc89badSNaveen N. Rao 110514cf11afSPaul Mackerras if (notify_die(DIE_SSTEP, "single_step", regs, 5, 110614cf11afSPaul Mackerras 5, SIGTRAP) == NOTIFY_STOP) 1107540d4d34SNicholas Piggin return; 110814cf11afSPaul Mackerras if (debugger_sstep(regs)) 1109540d4d34SNicholas Piggin return; 111014cf11afSPaul Mackerras 111114cf11afSPaul Mackerras _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip); 111214cf11afSPaul Mackerras } 111314cf11afSPaul Mackerras 111414cf11afSPaul Mackerras /* 111514cf11afSPaul Mackerras * After we have successfully emulated an instruction, we have to 111614cf11afSPaul Mackerras * check if the instruction was being single-stepped, and if so, 111714cf11afSPaul Mackerras * pretend we got a single-step exception. This was pointed out 111814cf11afSPaul Mackerras * by Kumar Gala. -- paulus 111914cf11afSPaul Mackerras */ 11208dad3f92SPaul Mackerras static void emulate_single_step(struct pt_regs *regs) 112114cf11afSPaul Mackerras { 11222538c2d0SK.Prasad if (single_stepping(regs)) 11232538c2d0SK.Prasad single_step_exception(regs); 112414cf11afSPaul Mackerras } 112514cf11afSPaul Mackerras 11265fad293bSKumar Gala static inline int __parse_fpscr(unsigned long fpscr) 1127dc1c1ca3SStephen Rothwell { 1128aeb1c0f6SEric W. Biederman int ret = FPE_FLTUNK; 1129dc1c1ca3SStephen Rothwell 1130dc1c1ca3SStephen Rothwell /* Invalid operation */ 1131dc1c1ca3SStephen Rothwell if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX)) 11325fad293bSKumar Gala ret = FPE_FLTINV; 1133dc1c1ca3SStephen Rothwell 1134dc1c1ca3SStephen Rothwell /* Overflow */ 1135dc1c1ca3SStephen Rothwell else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX)) 11365fad293bSKumar Gala ret = FPE_FLTOVF; 1137dc1c1ca3SStephen Rothwell 1138dc1c1ca3SStephen Rothwell /* Underflow */ 1139dc1c1ca3SStephen Rothwell else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX)) 11405fad293bSKumar Gala ret = FPE_FLTUND; 1141dc1c1ca3SStephen Rothwell 1142dc1c1ca3SStephen Rothwell /* Divide by zero */ 1143dc1c1ca3SStephen Rothwell else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX)) 11445fad293bSKumar Gala ret = FPE_FLTDIV; 1145dc1c1ca3SStephen Rothwell 1146dc1c1ca3SStephen Rothwell /* Inexact result */ 1147dc1c1ca3SStephen Rothwell else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX)) 11485fad293bSKumar Gala ret = FPE_FLTRES; 11495fad293bSKumar Gala 11505fad293bSKumar Gala return ret; 11515fad293bSKumar Gala } 11525fad293bSKumar Gala 11535fad293bSKumar Gala static void parse_fpe(struct pt_regs *regs) 11545fad293bSKumar Gala { 11555fad293bSKumar Gala int code = 0; 11565fad293bSKumar Gala 11575fad293bSKumar Gala flush_fp_to_thread(current); 11585fad293bSKumar Gala 1159b6254cedSChristophe Leroy #ifdef CONFIG_PPC_FPU_REGS 1160de79f7b9SPaul Mackerras code = __parse_fpscr(current->thread.fp_state.fpscr); 1161b6254cedSChristophe Leroy #endif 1162dc1c1ca3SStephen Rothwell 1163dc1c1ca3SStephen Rothwell _exception(SIGFPE, regs, code, regs->nip); 1164dc1c1ca3SStephen Rothwell } 1165dc1c1ca3SStephen Rothwell 1166dc1c1ca3SStephen Rothwell /* 1167dc1c1ca3SStephen Rothwell * Illegal instruction emulation support. Originally written to 116814cf11afSPaul Mackerras * provide the PVR to user applications using the mfspr rd, PVR. 116914cf11afSPaul Mackerras * Return non-zero if we can't emulate, or -EFAULT if the associated 117014cf11afSPaul Mackerras * memory access caused an access fault. Return zero on success. 117114cf11afSPaul Mackerras * 117214cf11afSPaul Mackerras * There are a couple of ways to do this, either "decode" the instruction 117314cf11afSPaul Mackerras * or directly match lots of bits. In this case, matching lots of 117414cf11afSPaul Mackerras * bits is faster and easier. 117586417780SPaul Mackerras * 117614cf11afSPaul Mackerras */ 117714cf11afSPaul Mackerras static int emulate_string_inst(struct pt_regs *regs, u32 instword) 117814cf11afSPaul Mackerras { 117914cf11afSPaul Mackerras u8 rT = (instword >> 21) & 0x1f; 118014cf11afSPaul Mackerras u8 rA = (instword >> 16) & 0x1f; 118114cf11afSPaul Mackerras u8 NB_RB = (instword >> 11) & 0x1f; 118214cf11afSPaul Mackerras u32 num_bytes; 118314cf11afSPaul Mackerras unsigned long EA; 118414cf11afSPaul Mackerras int pos = 0; 118514cf11afSPaul Mackerras 118614cf11afSPaul Mackerras /* Early out if we are an invalid form of lswx */ 118716c57b36SKumar Gala if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX) 118814cf11afSPaul Mackerras if ((rT == rA) || (rT == NB_RB)) 118914cf11afSPaul Mackerras return -EINVAL; 119014cf11afSPaul Mackerras 119114cf11afSPaul Mackerras EA = (rA == 0) ? 0 : regs->gpr[rA]; 119214cf11afSPaul Mackerras 119316c57b36SKumar Gala switch (instword & PPC_INST_STRING_MASK) { 119416c57b36SKumar Gala case PPC_INST_LSWX: 119516c57b36SKumar Gala case PPC_INST_STSWX: 119614cf11afSPaul Mackerras EA += NB_RB; 119714cf11afSPaul Mackerras num_bytes = regs->xer & 0x7f; 119814cf11afSPaul Mackerras break; 119916c57b36SKumar Gala case PPC_INST_LSWI: 120016c57b36SKumar Gala case PPC_INST_STSWI: 120114cf11afSPaul Mackerras num_bytes = (NB_RB == 0) ? 32 : NB_RB; 120214cf11afSPaul Mackerras break; 120314cf11afSPaul Mackerras default: 120414cf11afSPaul Mackerras return -EINVAL; 120514cf11afSPaul Mackerras } 120614cf11afSPaul Mackerras 120714cf11afSPaul Mackerras while (num_bytes != 0) 120814cf11afSPaul Mackerras { 120914cf11afSPaul Mackerras u8 val; 121014cf11afSPaul Mackerras u32 shift = 8 * (3 - (pos & 0x3)); 121114cf11afSPaul Mackerras 121280aa0fb4SJames Yang /* if process is 32-bit, clear upper 32 bits of EA */ 121380aa0fb4SJames Yang if ((regs->msr & MSR_64BIT) == 0) 121480aa0fb4SJames Yang EA &= 0xFFFFFFFF; 121580aa0fb4SJames Yang 121616c57b36SKumar Gala switch ((instword & PPC_INST_STRING_MASK)) { 121716c57b36SKumar Gala case PPC_INST_LSWX: 121816c57b36SKumar Gala case PPC_INST_LSWI: 121914cf11afSPaul Mackerras if (get_user(val, (u8 __user *)EA)) 122014cf11afSPaul Mackerras return -EFAULT; 122114cf11afSPaul Mackerras /* first time updating this reg, 122214cf11afSPaul Mackerras * zero it out */ 122314cf11afSPaul Mackerras if (pos == 0) 122414cf11afSPaul Mackerras regs->gpr[rT] = 0; 122514cf11afSPaul Mackerras regs->gpr[rT] |= val << shift; 122614cf11afSPaul Mackerras break; 122716c57b36SKumar Gala case PPC_INST_STSWI: 122816c57b36SKumar Gala case PPC_INST_STSWX: 122914cf11afSPaul Mackerras val = regs->gpr[rT] >> shift; 123014cf11afSPaul Mackerras if (put_user(val, (u8 __user *)EA)) 123114cf11afSPaul Mackerras return -EFAULT; 123214cf11afSPaul Mackerras break; 123314cf11afSPaul Mackerras } 123414cf11afSPaul Mackerras /* move EA to next address */ 123514cf11afSPaul Mackerras EA += 1; 123614cf11afSPaul Mackerras num_bytes--; 123714cf11afSPaul Mackerras 123814cf11afSPaul Mackerras /* manage our position within the register */ 123914cf11afSPaul Mackerras if (++pos == 4) { 124014cf11afSPaul Mackerras pos = 0; 124114cf11afSPaul Mackerras if (++rT == 32) 124214cf11afSPaul Mackerras rT = 0; 124314cf11afSPaul Mackerras } 124414cf11afSPaul Mackerras } 124514cf11afSPaul Mackerras 124614cf11afSPaul Mackerras return 0; 124714cf11afSPaul Mackerras } 124814cf11afSPaul Mackerras 1249c3412dcbSWill Schmidt static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword) 1250c3412dcbSWill Schmidt { 1251c3412dcbSWill Schmidt u32 ra,rs; 1252c3412dcbSWill Schmidt unsigned long tmp; 1253c3412dcbSWill Schmidt 1254c3412dcbSWill Schmidt ra = (instword >> 16) & 0x1f; 1255c3412dcbSWill Schmidt rs = (instword >> 21) & 0x1f; 1256c3412dcbSWill Schmidt 1257c3412dcbSWill Schmidt tmp = regs->gpr[rs]; 1258c3412dcbSWill Schmidt tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL); 1259c3412dcbSWill Schmidt tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL); 1260c3412dcbSWill Schmidt tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL; 1261c3412dcbSWill Schmidt regs->gpr[ra] = tmp; 1262c3412dcbSWill Schmidt 1263c3412dcbSWill Schmidt return 0; 1264c3412dcbSWill Schmidt } 1265c3412dcbSWill Schmidt 1266c1469f13SKumar Gala static int emulate_isel(struct pt_regs *regs, u32 instword) 1267c1469f13SKumar Gala { 1268c1469f13SKumar Gala u8 rT = (instword >> 21) & 0x1f; 1269c1469f13SKumar Gala u8 rA = (instword >> 16) & 0x1f; 1270c1469f13SKumar Gala u8 rB = (instword >> 11) & 0x1f; 1271c1469f13SKumar Gala u8 BC = (instword >> 6) & 0x1f; 1272c1469f13SKumar Gala u8 bit; 1273c1469f13SKumar Gala unsigned long tmp; 1274c1469f13SKumar Gala 1275c1469f13SKumar Gala tmp = (rA == 0) ? 0 : regs->gpr[rA]; 1276c1469f13SKumar Gala bit = (regs->ccr >> (31 - BC)) & 0x1; 1277c1469f13SKumar Gala 1278c1469f13SKumar Gala regs->gpr[rT] = bit ? tmp : regs->gpr[rB]; 1279c1469f13SKumar Gala 1280c1469f13SKumar Gala return 0; 1281c1469f13SKumar Gala } 1282c1469f13SKumar Gala 12836ce6c629SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 12846ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int cause) 12856ce6c629SMichael Neuling { 12866ce6c629SMichael Neuling /* If we're emulating a load/store in an active transaction, we cannot 12876ce6c629SMichael Neuling * emulate it as the kernel operates in transaction suspended context. 12886ce6c629SMichael Neuling * We need to abort the transaction. This creates a persistent TM 12896ce6c629SMichael Neuling * abort so tell the user what caused it with a new code. 12906ce6c629SMichael Neuling */ 12916ce6c629SMichael Neuling if (MSR_TM_TRANSACTIONAL(regs->msr)) { 12926ce6c629SMichael Neuling tm_enable(); 12936ce6c629SMichael Neuling tm_abort(cause); 12946ce6c629SMichael Neuling return true; 12956ce6c629SMichael Neuling } 12966ce6c629SMichael Neuling return false; 12976ce6c629SMichael Neuling } 12986ce6c629SMichael Neuling #else 12996ce6c629SMichael Neuling static inline bool tm_abort_check(struct pt_regs *regs, int reason) 13006ce6c629SMichael Neuling { 13016ce6c629SMichael Neuling return false; 13026ce6c629SMichael Neuling } 13036ce6c629SMichael Neuling #endif 13046ce6c629SMichael Neuling 130514cf11afSPaul Mackerras static int emulate_instruction(struct pt_regs *regs) 130614cf11afSPaul Mackerras { 130714cf11afSPaul Mackerras u32 instword; 130814cf11afSPaul Mackerras u32 rd; 130914cf11afSPaul Mackerras 13104288e343SAnton Blanchard if (!user_mode(regs)) 131114cf11afSPaul Mackerras return -EINVAL; 131214cf11afSPaul Mackerras CHECK_FULL_REGS(regs); 131314cf11afSPaul Mackerras 131414cf11afSPaul Mackerras if (get_user(instword, (u32 __user *)(regs->nip))) 131514cf11afSPaul Mackerras return -EFAULT; 131614cf11afSPaul Mackerras 131714cf11afSPaul Mackerras /* Emulate the mfspr rD, PVR. */ 131816c57b36SKumar Gala if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) { 1319eecff81dSAnton Blanchard PPC_WARN_EMULATED(mfpvr, regs); 132014cf11afSPaul Mackerras rd = (instword >> 21) & 0x1f; 132114cf11afSPaul Mackerras regs->gpr[rd] = mfspr(SPRN_PVR); 132214cf11afSPaul Mackerras return 0; 132314cf11afSPaul Mackerras } 132414cf11afSPaul Mackerras 132514cf11afSPaul Mackerras /* Emulating the dcba insn is just a no-op. */ 132680947e7cSGeert Uytterhoeven if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) { 1327eecff81dSAnton Blanchard PPC_WARN_EMULATED(dcba, regs); 132814cf11afSPaul Mackerras return 0; 132980947e7cSGeert Uytterhoeven } 133014cf11afSPaul Mackerras 133114cf11afSPaul Mackerras /* Emulate the mcrxr insn. */ 133216c57b36SKumar Gala if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) { 133386417780SPaul Mackerras int shift = (instword >> 21) & 0x1c; 133414cf11afSPaul Mackerras unsigned long msk = 0xf0000000UL >> shift; 133514cf11afSPaul Mackerras 1336eecff81dSAnton Blanchard PPC_WARN_EMULATED(mcrxr, regs); 133714cf11afSPaul Mackerras regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk); 133814cf11afSPaul Mackerras regs->xer &= ~0xf0000000UL; 133914cf11afSPaul Mackerras return 0; 134014cf11afSPaul Mackerras } 134114cf11afSPaul Mackerras 134214cf11afSPaul Mackerras /* Emulate load/store string insn. */ 134380947e7cSGeert Uytterhoeven if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) { 13446ce6c629SMichael Neuling if (tm_abort_check(regs, 13456ce6c629SMichael Neuling TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT)) 13466ce6c629SMichael Neuling return -EINVAL; 1347eecff81dSAnton Blanchard PPC_WARN_EMULATED(string, regs); 134814cf11afSPaul Mackerras return emulate_string_inst(regs, instword); 134980947e7cSGeert Uytterhoeven } 135014cf11afSPaul Mackerras 1351c3412dcbSWill Schmidt /* Emulate the popcntb (Population Count Bytes) instruction. */ 135216c57b36SKumar Gala if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) { 1353eecff81dSAnton Blanchard PPC_WARN_EMULATED(popcntb, regs); 1354c3412dcbSWill Schmidt return emulate_popcntb_inst(regs, instword); 1355c3412dcbSWill Schmidt } 1356c3412dcbSWill Schmidt 1357c1469f13SKumar Gala /* Emulate isel (Integer Select) instruction */ 135816c57b36SKumar Gala if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) { 1359eecff81dSAnton Blanchard PPC_WARN_EMULATED(isel, regs); 1360c1469f13SKumar Gala return emulate_isel(regs, instword); 1361c1469f13SKumar Gala } 1362c1469f13SKumar Gala 13639863c28aSJames Yang /* Emulate sync instruction variants */ 13649863c28aSJames Yang if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) { 13659863c28aSJames Yang PPC_WARN_EMULATED(sync, regs); 13669863c28aSJames Yang asm volatile("sync"); 13679863c28aSJames Yang return 0; 13689863c28aSJames Yang } 13699863c28aSJames Yang 1370efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64 1371efcac658SAlexey Kardashevskiy /* Emulate the mfspr rD, DSCR. */ 137273d2fb75SAnton Blanchard if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) == 137373d2fb75SAnton Blanchard PPC_INST_MFSPR_DSCR_USER) || 137473d2fb75SAnton Blanchard ((instword & PPC_INST_MFSPR_DSCR_MASK) == 137573d2fb75SAnton Blanchard PPC_INST_MFSPR_DSCR)) && 1376efcac658SAlexey Kardashevskiy cpu_has_feature(CPU_FTR_DSCR)) { 1377efcac658SAlexey Kardashevskiy PPC_WARN_EMULATED(mfdscr, regs); 1378efcac658SAlexey Kardashevskiy rd = (instword >> 21) & 0x1f; 1379efcac658SAlexey Kardashevskiy regs->gpr[rd] = mfspr(SPRN_DSCR); 1380efcac658SAlexey Kardashevskiy return 0; 1381efcac658SAlexey Kardashevskiy } 1382efcac658SAlexey Kardashevskiy /* Emulate the mtspr DSCR, rD. */ 138373d2fb75SAnton Blanchard if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) == 138473d2fb75SAnton Blanchard PPC_INST_MTSPR_DSCR_USER) || 138573d2fb75SAnton Blanchard ((instword & PPC_INST_MTSPR_DSCR_MASK) == 138673d2fb75SAnton Blanchard PPC_INST_MTSPR_DSCR)) && 1387efcac658SAlexey Kardashevskiy cpu_has_feature(CPU_FTR_DSCR)) { 1388efcac658SAlexey Kardashevskiy PPC_WARN_EMULATED(mtdscr, regs); 1389efcac658SAlexey Kardashevskiy rd = (instword >> 21) & 0x1f; 139000ca0de0SAnton Blanchard current->thread.dscr = regs->gpr[rd]; 1391efcac658SAlexey Kardashevskiy current->thread.dscr_inherit = 1; 139200ca0de0SAnton Blanchard mtspr(SPRN_DSCR, current->thread.dscr); 1393efcac658SAlexey Kardashevskiy return 0; 1394efcac658SAlexey Kardashevskiy } 1395efcac658SAlexey Kardashevskiy #endif 1396efcac658SAlexey Kardashevskiy 139714cf11afSPaul Mackerras return -EINVAL; 139814cf11afSPaul Mackerras } 139914cf11afSPaul Mackerras 140073c9ceabSJeremy Fitzhardinge int is_valid_bugaddr(unsigned long addr) 140114cf11afSPaul Mackerras { 140273c9ceabSJeremy Fitzhardinge return is_kernel_addr(addr); 140314cf11afSPaul Mackerras } 140414cf11afSPaul Mackerras 14053a3b5aa6SKevin Hao #ifdef CONFIG_MATH_EMULATION 14063a3b5aa6SKevin Hao static int emulate_math(struct pt_regs *regs) 14073a3b5aa6SKevin Hao { 14083a3b5aa6SKevin Hao int ret; 14093a3b5aa6SKevin Hao extern int do_mathemu(struct pt_regs *regs); 14103a3b5aa6SKevin Hao 14113a3b5aa6SKevin Hao ret = do_mathemu(regs); 14123a3b5aa6SKevin Hao if (ret >= 0) 14133a3b5aa6SKevin Hao PPC_WARN_EMULATED(math, regs); 14143a3b5aa6SKevin Hao 14153a3b5aa6SKevin Hao switch (ret) { 14163a3b5aa6SKevin Hao case 0: 14173a3b5aa6SKevin Hao emulate_single_step(regs); 14183a3b5aa6SKevin Hao return 0; 14193a3b5aa6SKevin Hao case 1: { 14203a3b5aa6SKevin Hao int code = 0; 1421de79f7b9SPaul Mackerras code = __parse_fpscr(current->thread.fp_state.fpscr); 14223a3b5aa6SKevin Hao _exception(SIGFPE, regs, code, regs->nip); 14233a3b5aa6SKevin Hao return 0; 14243a3b5aa6SKevin Hao } 14253a3b5aa6SKevin Hao case -EFAULT: 14263a3b5aa6SKevin Hao _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip); 14273a3b5aa6SKevin Hao return 0; 14283a3b5aa6SKevin Hao } 14293a3b5aa6SKevin Hao 14303a3b5aa6SKevin Hao return -1; 14313a3b5aa6SKevin Hao } 14323a3b5aa6SKevin Hao #else 14333a3b5aa6SKevin Hao static inline int emulate_math(struct pt_regs *regs) { return -1; } 14343a3b5aa6SKevin Hao #endif 14353a3b5aa6SKevin Hao 1436fd3f1e0fSNicholas Piggin static void do_program_check(struct pt_regs *regs) 143714cf11afSPaul Mackerras { 143814cf11afSPaul Mackerras unsigned int reason = get_reason(regs); 143914cf11afSPaul Mackerras 1440aa42c69cSKim Phillips /* We can now get here via a FP Unavailable exception if the core 144104903a30SKumar Gala * has no FPU, in that case the reason flags will be 0 */ 144214cf11afSPaul Mackerras 144314cf11afSPaul Mackerras if (reason & REASON_FP) { 144414cf11afSPaul Mackerras /* IEEE FP exception */ 1445dc1c1ca3SStephen Rothwell parse_fpe(regs); 1446fd3f1e0fSNicholas Piggin return; 14478dad3f92SPaul Mackerras } 14488dad3f92SPaul Mackerras if (reason & REASON_TRAP) { 1449a4c3f909SBalbir Singh unsigned long bugaddr; 1450ba797b28SJason Wessel /* Debugger is first in line to stop recursive faults in 1451ba797b28SJason Wessel * rcu_lock, notify_die, or atomic_notifier_call_chain */ 1452ba797b28SJason Wessel if (debugger_bpt(regs)) 1453fd3f1e0fSNicholas Piggin return; 1454ba797b28SJason Wessel 14556cc89badSNaveen N. Rao if (kprobe_handler(regs)) 1456fd3f1e0fSNicholas Piggin return; 14576cc89badSNaveen N. Rao 145814cf11afSPaul Mackerras /* trap exception */ 1459dc1c1ca3SStephen Rothwell if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP) 1460dc1c1ca3SStephen Rothwell == NOTIFY_STOP) 1461fd3f1e0fSNicholas Piggin return; 146273c9ceabSJeremy Fitzhardinge 1463a4c3f909SBalbir Singh bugaddr = regs->nip; 1464a4c3f909SBalbir Singh /* 1465a4c3f909SBalbir Singh * Fixup bugaddr for BUG_ON() in real mode 1466a4c3f909SBalbir Singh */ 1467a4c3f909SBalbir Singh if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR)) 1468a4c3f909SBalbir Singh bugaddr += PAGE_OFFSET; 1469a4c3f909SBalbir Singh 147073c9ceabSJeremy Fitzhardinge if (!(regs->msr & MSR_PR) && /* not user-mode */ 1471a4c3f909SBalbir Singh report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) { 147214cf11afSPaul Mackerras regs->nip += 4; 1473fd3f1e0fSNicholas Piggin return; 147414cf11afSPaul Mackerras } 14758dad3f92SPaul Mackerras _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip); 1476fd3f1e0fSNicholas Piggin return; 14778dad3f92SPaul Mackerras } 1478bc2a9408SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1479bc2a9408SMichael Neuling if (reason & REASON_TM) { 1480bc2a9408SMichael Neuling /* This is a TM "Bad Thing Exception" program check. 1481bc2a9408SMichael Neuling * This occurs when: 1482bc2a9408SMichael Neuling * - An rfid/hrfid/mtmsrd attempts to cause an illegal 1483bc2a9408SMichael Neuling * transition in TM states. 1484bc2a9408SMichael Neuling * - A trechkpt is attempted when transactional. 1485bc2a9408SMichael Neuling * - A treclaim is attempted when non transactional. 1486bc2a9408SMichael Neuling * - A tend is illegally attempted. 1487bc2a9408SMichael Neuling * - writing a TM SPR when transactional. 1488632f0574SMichael Ellerman * 1489632f0574SMichael Ellerman * If usermode caused this, it's done something illegal and 1490bc2a9408SMichael Neuling * gets a SIGILL slap on the wrist. We call it an illegal 1491bc2a9408SMichael Neuling * operand to distinguish from the instruction just being bad 1492bc2a9408SMichael Neuling * (e.g. executing a 'tend' on a CPU without TM!); it's an 1493bc2a9408SMichael Neuling * illegal /placement/ of a valid instruction. 1494bc2a9408SMichael Neuling */ 1495bc2a9408SMichael Neuling if (user_mode(regs)) { 1496bc2a9408SMichael Neuling _exception(SIGILL, regs, ILL_ILLOPN, regs->nip); 1497fd3f1e0fSNicholas Piggin return; 1498bc2a9408SMichael Neuling } else { 1499bc2a9408SMichael Neuling printk(KERN_EMERG "Unexpected TM Bad Thing exception " 150011be3958SBreno Leitao "at %lx (msr 0x%lx) tm_scratch=%llx\n", 150111be3958SBreno Leitao regs->nip, regs->msr, get_paca()->tm_scratch); 1502bc2a9408SMichael Neuling die("Unrecoverable exception", regs, SIGABRT); 1503bc2a9408SMichael Neuling } 1504bc2a9408SMichael Neuling } 1505bc2a9408SMichael Neuling #endif 15068dad3f92SPaul Mackerras 1507b3f6a459SMichael Ellerman /* 1508b3f6a459SMichael Ellerman * If we took the program check in the kernel skip down to sending a 1509b3f6a459SMichael Ellerman * SIGILL. The subsequent cases all relate to emulating instructions 1510b3f6a459SMichael Ellerman * which we should only do for userspace. We also do not want to enable 1511b3f6a459SMichael Ellerman * interrupts for kernel faults because that might lead to further 1512b3f6a459SMichael Ellerman * faults, and loose the context of the original exception. 1513b3f6a459SMichael Ellerman */ 1514b3f6a459SMichael Ellerman if (!user_mode(regs)) 1515b3f6a459SMichael Ellerman goto sigill; 1516b3f6a459SMichael Ellerman 1517e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 1518cd8a5673SPaul Mackerras 151904903a30SKumar Gala /* (reason & REASON_ILLEGAL) would be the obvious thing here, 152004903a30SKumar Gala * but there seems to be a hardware bug on the 405GP (RevD) 152104903a30SKumar Gala * that means ESR is sometimes set incorrectly - either to 152204903a30SKumar Gala * ESR_DST (!?) or 0. In the process of chasing this with the 152304903a30SKumar Gala * hardware people - not sure if it can happen on any illegal 152404903a30SKumar Gala * instruction or only on FP instructions, whether there is a 15254e63f8edSBenjamin Herrenschmidt * pattern to occurrences etc. -dgibson 31/Mar/2003 15264e63f8edSBenjamin Herrenschmidt */ 15273a3b5aa6SKevin Hao if (!emulate_math(regs)) 1528fd3f1e0fSNicholas Piggin return; 152904903a30SKumar Gala 15308dad3f92SPaul Mackerras /* Try to emulate it if we should. */ 15318dad3f92SPaul Mackerras if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) { 153214cf11afSPaul Mackerras switch (emulate_instruction(regs)) { 153314cf11afSPaul Mackerras case 0: 153414cf11afSPaul Mackerras regs->nip += 4; 153514cf11afSPaul Mackerras emulate_single_step(regs); 1536fd3f1e0fSNicholas Piggin return; 153714cf11afSPaul Mackerras case -EFAULT: 153814cf11afSPaul Mackerras _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip); 1539fd3f1e0fSNicholas Piggin return; 15408dad3f92SPaul Mackerras } 15418dad3f92SPaul Mackerras } 15428dad3f92SPaul Mackerras 1543b3f6a459SMichael Ellerman sigill: 154414cf11afSPaul Mackerras if (reason & REASON_PRIVILEGED) 154514cf11afSPaul Mackerras _exception(SIGILL, regs, ILL_PRVOPC, regs->nip); 154614cf11afSPaul Mackerras else 154714cf11afSPaul Mackerras _exception(SIGILL, regs, ILL_ILLOPC, regs->nip); 1548ba12eedeSLi Zhong 1549fd3f1e0fSNicholas Piggin } 1550fd3f1e0fSNicholas Piggin 15513a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(program_check_exception) 1552fd3f1e0fSNicholas Piggin { 1553fd3f1e0fSNicholas Piggin do_program_check(regs); 155414cf11afSPaul Mackerras } 155514cf11afSPaul Mackerras 1556bf593907SPaul Mackerras /* 1557bf593907SPaul Mackerras * This occurs when running in hypervisor mode on POWER6 or later 1558bf593907SPaul Mackerras * and an illegal instruction is encountered. 1559bf593907SPaul Mackerras */ 15603a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(emulation_assist_interrupt) 1561bf593907SPaul Mackerras { 1562bf593907SPaul Mackerras regs->msr |= REASON_ILLEGAL; 1563fd3f1e0fSNicholas Piggin do_program_check(regs); 1564bf593907SPaul Mackerras } 1565bf593907SPaul Mackerras 15663a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(alignment_exception) 156714cf11afSPaul Mackerras { 15684393c4f6SBenjamin Herrenschmidt int sig, code, fixed = 0; 15699409d2f9SJordan Niethe unsigned long reason; 157014cf11afSPaul Mackerras 1571e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 1572a3512b2dSBenjamin Herrenschmidt 15739409d2f9SJordan Niethe reason = get_reason(regs); 15749409d2f9SJordan Niethe if (reason & REASON_BOUNDARY) { 15759409d2f9SJordan Niethe sig = SIGBUS; 15769409d2f9SJordan Niethe code = BUS_ADRALN; 15779409d2f9SJordan Niethe goto bad; 15789409d2f9SJordan Niethe } 15799409d2f9SJordan Niethe 15806ce6c629SMichael Neuling if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT)) 1581540d4d34SNicholas Piggin return; 15826ce6c629SMichael Neuling 1583e9370ae1SPaul Mackerras /* we don't implement logging of alignment exceptions */ 1584e9370ae1SPaul Mackerras if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS)) 158514cf11afSPaul Mackerras fixed = fix_alignment(regs); 158614cf11afSPaul Mackerras 158714cf11afSPaul Mackerras if (fixed == 1) { 15889409d2f9SJordan Niethe /* skip over emulated instruction */ 15899409d2f9SJordan Niethe regs->nip += inst_length(reason); 159014cf11afSPaul Mackerras emulate_single_step(regs); 1591540d4d34SNicholas Piggin return; 159214cf11afSPaul Mackerras } 159314cf11afSPaul Mackerras 159414cf11afSPaul Mackerras /* Operand address was bad */ 159514cf11afSPaul Mackerras if (fixed == -EFAULT) { 15964393c4f6SBenjamin Herrenschmidt sig = SIGSEGV; 15974393c4f6SBenjamin Herrenschmidt code = SEGV_ACCERR; 15984393c4f6SBenjamin Herrenschmidt } else { 15994393c4f6SBenjamin Herrenschmidt sig = SIGBUS; 16004393c4f6SBenjamin Herrenschmidt code = BUS_ADRALN; 160114cf11afSPaul Mackerras } 16029409d2f9SJordan Niethe bad: 16034393c4f6SBenjamin Herrenschmidt if (user_mode(regs)) 16044393c4f6SBenjamin Herrenschmidt _exception(sig, regs, code, regs->dar); 16054393c4f6SBenjamin Herrenschmidt else 16068458c628SNicholas Piggin bad_page_fault(regs, sig); 160714cf11afSPaul Mackerras } 160814cf11afSPaul Mackerras 16093a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(StackOverflow) 161014cf11afSPaul Mackerras { 16119bf3d3c4SChristophe Leroy pr_crit("Kernel stack overflow in process %s[%d], r1=%lx\n", 16129bf3d3c4SChristophe Leroy current->comm, task_pid_nr(current), regs->gpr[1]); 161314cf11afSPaul Mackerras debugger(regs); 161414cf11afSPaul Mackerras show_regs(regs); 161514cf11afSPaul Mackerras panic("kernel stack overflow"); 161614cf11afSPaul Mackerras } 161714cf11afSPaul Mackerras 16183a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(stack_overflow_exception) 16193978eb78SChristophe Leroy { 16203978eb78SChristophe Leroy die("Kernel stack overflow", regs, SIGSEGV); 16213978eb78SChristophe Leroy } 16223978eb78SChristophe Leroy 16233a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(kernel_fp_unavailable_exception) 1624dc1c1ca3SStephen Rothwell { 1625dc1c1ca3SStephen Rothwell printk(KERN_EMERG "Unrecoverable FP Unavailable Exception " 1626dc1c1ca3SStephen Rothwell "%lx at %lx\n", regs->trap, regs->nip); 1627dc1c1ca3SStephen Rothwell die("Unrecoverable FP Unavailable Exception", regs, SIGABRT); 1628dc1c1ca3SStephen Rothwell } 1629dc1c1ca3SStephen Rothwell 16303a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_unavailable_exception) 1631dc1c1ca3SStephen Rothwell { 1632dc1c1ca3SStephen Rothwell if (user_mode(regs)) { 1633dc1c1ca3SStephen Rothwell /* A user program has executed an altivec instruction, 1634dc1c1ca3SStephen Rothwell but this kernel doesn't support altivec. */ 1635dc1c1ca3SStephen Rothwell _exception(SIGILL, regs, ILL_ILLOPC, regs->nip); 1636540d4d34SNicholas Piggin return; 1637dc1c1ca3SStephen Rothwell } 16386c4841c2SAnton Blanchard 1639dc1c1ca3SStephen Rothwell printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception " 1640dc1c1ca3SStephen Rothwell "%lx at %lx\n", regs->trap, regs->nip); 1641dc1c1ca3SStephen Rothwell die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT); 1642dc1c1ca3SStephen Rothwell } 1643dc1c1ca3SStephen Rothwell 16443a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(vsx_unavailable_exception) 1645ce48b210SMichael Neuling { 1646ce48b210SMichael Neuling if (user_mode(regs)) { 1647ce48b210SMichael Neuling /* A user program has executed an vsx instruction, 1648ce48b210SMichael Neuling but this kernel doesn't support vsx. */ 1649ce48b210SMichael Neuling _exception(SIGILL, regs, ILL_ILLOPC, regs->nip); 1650ce48b210SMichael Neuling return; 1651ce48b210SMichael Neuling } 1652ce48b210SMichael Neuling 1653ce48b210SMichael Neuling printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception " 1654ce48b210SMichael Neuling "%lx at %lx\n", regs->trap, regs->nip); 1655ce48b210SMichael Neuling die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT); 1656ce48b210SMichael Neuling } 1657ce48b210SMichael Neuling 16582517617eSMichael Neuling #ifdef CONFIG_PPC64 1659172f7aaaSCyril Bur static void tm_unavailable(struct pt_regs *regs) 1660172f7aaaSCyril Bur { 16615d176f75SCyril Bur #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 16625d176f75SCyril Bur if (user_mode(regs)) { 16635d176f75SCyril Bur current->thread.load_tm++; 16645d176f75SCyril Bur regs->msr |= MSR_TM; 16655d176f75SCyril Bur tm_enable(); 16665d176f75SCyril Bur tm_restore_sprs(¤t->thread); 16675d176f75SCyril Bur return; 16685d176f75SCyril Bur } 16695d176f75SCyril Bur #endif 1670172f7aaaSCyril Bur pr_emerg("Unrecoverable TM Unavailable Exception " 1671172f7aaaSCyril Bur "%lx at %lx\n", regs->trap, regs->nip); 1672172f7aaaSCyril Bur die("Unrecoverable TM Unavailable Exception", regs, SIGABRT); 1673172f7aaaSCyril Bur } 1674172f7aaaSCyril Bur 16753a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(facility_unavailable_exception) 1676d0c0c9a1SMichael Neuling { 1677021424a1SMichael Ellerman static char *facility_strings[] = { 16782517617eSMichael Neuling [FSCR_FP_LG] = "FPU", 16792517617eSMichael Neuling [FSCR_VECVSX_LG] = "VMX/VSX", 16802517617eSMichael Neuling [FSCR_DSCR_LG] = "DSCR", 16812517617eSMichael Neuling [FSCR_PM_LG] = "PMU SPRs", 16822517617eSMichael Neuling [FSCR_BHRB_LG] = "BHRB", 16832517617eSMichael Neuling [FSCR_TM_LG] = "TM", 16842517617eSMichael Neuling [FSCR_EBB_LG] = "EBB", 16852517617eSMichael Neuling [FSCR_TAR_LG] = "TAR", 1686794464f4SNicholas Piggin [FSCR_MSGP_LG] = "MSGP", 16879b7ff0c6SNicholas Piggin [FSCR_SCV_LG] = "SCV", 16882aa6195eSAlistair Popple [FSCR_PREFIX_LG] = "PREFIX", 1689021424a1SMichael Ellerman }; 16902517617eSMichael Neuling char *facility = "unknown"; 1691021424a1SMichael Ellerman u64 value; 1692c952c1c4SAnshuman Khandual u32 instword, rd; 16932517617eSMichael Neuling u8 status; 16942517617eSMichael Neuling bool hv; 1695021424a1SMichael Ellerman 16962271db20SBenjamin Herrenschmidt hv = (TRAP(regs) == 0xf80); 16972517617eSMichael Neuling if (hv) 1698b14b6260SMichael Ellerman value = mfspr(SPRN_HFSCR); 16992517617eSMichael Neuling else 17002517617eSMichael Neuling value = mfspr(SPRN_FSCR); 17012517617eSMichael Neuling 17022517617eSMichael Neuling status = value >> 56; 1703709b973cSAnshuman Khandual if ((hv || status >= 2) && 1704709b973cSAnshuman Khandual (status < ARRAY_SIZE(facility_strings)) && 1705709b973cSAnshuman Khandual facility_strings[status]) 1706709b973cSAnshuman Khandual facility = facility_strings[status]; 1707709b973cSAnshuman Khandual 1708709b973cSAnshuman Khandual /* We should not have taken this interrupt in kernel */ 1709709b973cSAnshuman Khandual if (!user_mode(regs)) { 1710709b973cSAnshuman Khandual pr_emerg("Facility '%s' unavailable (%d) exception in kernel mode at %lx\n", 1711709b973cSAnshuman Khandual facility, status, regs->nip); 1712709b973cSAnshuman Khandual die("Unexpected facility unavailable exception", regs, SIGABRT); 1713709b973cSAnshuman Khandual } 1714709b973cSAnshuman Khandual 1715e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 1716709b973cSAnshuman Khandual 17172517617eSMichael Neuling if (status == FSCR_DSCR_LG) { 1718c952c1c4SAnshuman Khandual /* 1719c952c1c4SAnshuman Khandual * User is accessing the DSCR register using the problem 1720c952c1c4SAnshuman Khandual * state only SPR number (0x03) either through a mfspr or 1721c952c1c4SAnshuman Khandual * a mtspr instruction. If it is a write attempt through 1722c952c1c4SAnshuman Khandual * a mtspr, then we set the inherit bit. This also allows 1723c952c1c4SAnshuman Khandual * the user to write or read the register directly in the 1724c952c1c4SAnshuman Khandual * future by setting via the FSCR DSCR bit. But in case it 1725c952c1c4SAnshuman Khandual * is a read DSCR attempt through a mfspr instruction, we 1726c952c1c4SAnshuman Khandual * just emulate the instruction instead. This code path will 1727c952c1c4SAnshuman Khandual * always emulate all the mfspr instructions till the user 1728c952c1c4SAnshuman Khandual * has attempted at least one mtspr instruction. This way it 1729c952c1c4SAnshuman Khandual * preserves the same behaviour when the user is accessing 1730c952c1c4SAnshuman Khandual * the DSCR through privilege level only SPR number (0x11) 1731c952c1c4SAnshuman Khandual * which is emulated through illegal instruction exception. 1732c952c1c4SAnshuman Khandual * We always leave HFSCR DSCR set. 17332517617eSMichael Neuling */ 1734c952c1c4SAnshuman Khandual if (get_user(instword, (u32 __user *)(regs->nip))) { 1735c952c1c4SAnshuman Khandual pr_err("Failed to fetch the user instruction\n"); 1736c952c1c4SAnshuman Khandual return; 1737c952c1c4SAnshuman Khandual } 1738c952c1c4SAnshuman Khandual 1739c952c1c4SAnshuman Khandual /* Write into DSCR (mtspr 0x03, RS) */ 1740c952c1c4SAnshuman Khandual if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK) 1741c952c1c4SAnshuman Khandual == PPC_INST_MTSPR_DSCR_USER) { 1742c952c1c4SAnshuman Khandual rd = (instword >> 21) & 0x1f; 1743c952c1c4SAnshuman Khandual current->thread.dscr = regs->gpr[rd]; 17442517617eSMichael Neuling current->thread.dscr_inherit = 1; 1745b57bd2deSMichael Neuling current->thread.fscr |= FSCR_DSCR; 1746b57bd2deSMichael Neuling mtspr(SPRN_FSCR, current->thread.fscr); 1747c952c1c4SAnshuman Khandual } 1748c952c1c4SAnshuman Khandual 1749c952c1c4SAnshuman Khandual /* Read from DSCR (mfspr RT, 0x03) */ 1750c952c1c4SAnshuman Khandual if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK) 1751c952c1c4SAnshuman Khandual == PPC_INST_MFSPR_DSCR_USER) { 1752c952c1c4SAnshuman Khandual if (emulate_instruction(regs)) { 1753c952c1c4SAnshuman Khandual pr_err("DSCR based mfspr emulation failed\n"); 1754c952c1c4SAnshuman Khandual return; 1755c952c1c4SAnshuman Khandual } 1756c952c1c4SAnshuman Khandual regs->nip += 4; 1757c952c1c4SAnshuman Khandual emulate_single_step(regs); 1758c952c1c4SAnshuman Khandual } 17592517617eSMichael Neuling return; 1760b14b6260SMichael Ellerman } 1761b14b6260SMichael Ellerman 1762172f7aaaSCyril Bur if (status == FSCR_TM_LG) { 1763172f7aaaSCyril Bur /* 1764172f7aaaSCyril Bur * If we're here then the hardware is TM aware because it 1765172f7aaaSCyril Bur * generated an exception with FSRM_TM set. 1766172f7aaaSCyril Bur * 1767172f7aaaSCyril Bur * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware 1768172f7aaaSCyril Bur * told us not to do TM, or the kernel is not built with TM 1769172f7aaaSCyril Bur * support. 1770172f7aaaSCyril Bur * 1771172f7aaaSCyril Bur * If both of those things are true, then userspace can spam the 1772172f7aaaSCyril Bur * console by triggering the printk() below just by continually 1773172f7aaaSCyril Bur * doing tbegin (or any TM instruction). So in that case just 1774172f7aaaSCyril Bur * send the process a SIGILL immediately. 1775172f7aaaSCyril Bur */ 1776172f7aaaSCyril Bur if (!cpu_has_feature(CPU_FTR_TM)) 1777172f7aaaSCyril Bur goto out; 1778172f7aaaSCyril Bur 1779172f7aaaSCyril Bur tm_unavailable(regs); 1780172f7aaaSCyril Bur return; 1781172f7aaaSCyril Bur } 1782172f7aaaSCyril Bur 178393c2ec0fSBalbir Singh pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n", 178493c2ec0fSBalbir Singh hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr); 1785d0c0c9a1SMichael Neuling 1786172f7aaaSCyril Bur out: 1787d0c0c9a1SMichael Neuling _exception(SIGILL, regs, ILL_ILLOPC, regs->nip); 1788d0c0c9a1SMichael Neuling } 17892517617eSMichael Neuling #endif 1790d0c0c9a1SMichael Neuling 1791f54db641SMichael Neuling #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1792f54db641SMichael Neuling 17933a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(fp_unavailable_tm) 1794f54db641SMichael Neuling { 1795f54db641SMichael Neuling /* Note: This does not handle any kind of FP laziness. */ 1796f54db641SMichael Neuling 1797f54db641SMichael Neuling TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n", 1798f54db641SMichael Neuling regs->nip, regs->msr); 1799f54db641SMichael Neuling 1800f54db641SMichael Neuling /* We can only have got here if the task started using FP after 1801f54db641SMichael Neuling * beginning the transaction. So, the transactional regs are just a 1802f54db641SMichael Neuling * copy of the checkpointed ones. But, we still need to recheckpoint 1803f54db641SMichael Neuling * as we're enabling FP for the process; it will return, abort the 1804f54db641SMichael Neuling * transaction, and probably retry but now with FP enabled. So the 1805f54db641SMichael Neuling * checkpointed FP registers need to be loaded. 1806f54db641SMichael Neuling */ 1807d31626f7SPaul Mackerras tm_reclaim_current(TM_CAUSE_FAC_UNAV); 180896695563SBreno Leitao 180996695563SBreno Leitao /* 181096695563SBreno Leitao * Reclaim initially saved out bogus (lazy) FPRs to ckfp_state, and 181196695563SBreno Leitao * then it was overwrite by the thr->fp_state by tm_reclaim_thread(). 181296695563SBreno Leitao * 181396695563SBreno Leitao * At this point, ck{fp,vr}_state contains the exact values we want to 181496695563SBreno Leitao * recheckpoint. 181596695563SBreno Leitao */ 1816f54db641SMichael Neuling 1817f54db641SMichael Neuling /* Enable FP for the task: */ 1818a7771176SCyril Bur current->thread.load_fp = 1; 1819f54db641SMichael Neuling 182096695563SBreno Leitao /* 182196695563SBreno Leitao * Recheckpoint all the checkpointed ckpt, ck{fp, vr}_state registers. 1822f54db641SMichael Neuling */ 1823eb5c3f1cSCyril Bur tm_recheckpoint(¤t->thread); 1824f54db641SMichael Neuling } 1825f54db641SMichael Neuling 18263a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_unavailable_tm) 1827f54db641SMichael Neuling { 1828f54db641SMichael Neuling /* See the comments in fp_unavailable_tm(). This function operates 1829f54db641SMichael Neuling * the same way. 1830f54db641SMichael Neuling */ 1831f54db641SMichael Neuling 1832f54db641SMichael Neuling TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx," 1833f54db641SMichael Neuling "MSR=%lx\n", 1834f54db641SMichael Neuling regs->nip, regs->msr); 1835d31626f7SPaul Mackerras tm_reclaim_current(TM_CAUSE_FAC_UNAV); 1836a7771176SCyril Bur current->thread.load_vec = 1; 1837eb5c3f1cSCyril Bur tm_recheckpoint(¤t->thread); 1838f54db641SMichael Neuling current->thread.used_vr = 1; 18393ac8ff1cSPaul Mackerras } 18403ac8ff1cSPaul Mackerras 18413a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(vsx_unavailable_tm) 1842f54db641SMichael Neuling { 1843f54db641SMichael Neuling /* See the comments in fp_unavailable_tm(). This works similarly, 1844f54db641SMichael Neuling * though we're loading both FP and VEC registers in here. 1845f54db641SMichael Neuling * 1846f54db641SMichael Neuling * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC 1847f54db641SMichael Neuling * regs. Either way, set MSR_VSX. 1848f54db641SMichael Neuling */ 1849f54db641SMichael Neuling 1850f54db641SMichael Neuling TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx," 1851f54db641SMichael Neuling "MSR=%lx\n", 1852f54db641SMichael Neuling regs->nip, regs->msr); 1853f54db641SMichael Neuling 18543ac8ff1cSPaul Mackerras current->thread.used_vsr = 1; 18553ac8ff1cSPaul Mackerras 1856f54db641SMichael Neuling /* This reclaims FP and/or VR regs if they're already enabled */ 1857d31626f7SPaul Mackerras tm_reclaim_current(TM_CAUSE_FAC_UNAV); 1858f54db641SMichael Neuling 1859a7771176SCyril Bur current->thread.load_vec = 1; 1860a7771176SCyril Bur current->thread.load_fp = 1; 18613ac8ff1cSPaul Mackerras 1862eb5c3f1cSCyril Bur tm_recheckpoint(¤t->thread); 1863f54db641SMichael Neuling } 1864f54db641SMichael Neuling #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */ 1865f54db641SMichael Neuling 18663a96570fSNicholas Piggin #ifdef CONFIG_PPC64 18673a96570fSNicholas Piggin DECLARE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi); 18683a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_NMI(performance_monitor_exception_nmi) 1869dc1c1ca3SStephen Rothwell { 187069111bacSChristoph Lameter __this_cpu_inc(irq_stat.pmu_irqs); 187189713ed1SAnton Blanchard 1872dc1c1ca3SStephen Rothwell perf_irq(regs); 1873156b5371SNicholas Piggin 18743a96570fSNicholas Piggin return 0; 18753a96570fSNicholas Piggin } 18763a96570fSNicholas Piggin #endif 18773a96570fSNicholas Piggin 18783a96570fSNicholas Piggin DECLARE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async); 18793a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_ASYNC(performance_monitor_exception_async) 1880156b5371SNicholas Piggin { 1881156b5371SNicholas Piggin __this_cpu_inc(irq_stat.pmu_irqs); 1882156b5371SNicholas Piggin 1883156b5371SNicholas Piggin perf_irq(regs); 1884156b5371SNicholas Piggin } 1885156b5371SNicholas Piggin 18863a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER_RAW(performance_monitor_exception) 1887156b5371SNicholas Piggin { 1888156b5371SNicholas Piggin /* 1889156b5371SNicholas Piggin * On 64-bit, if perf interrupts hit in a local_irq_disable 1890156b5371SNicholas Piggin * (soft-masked) region, we consider them as NMIs. This is required to 1891156b5371SNicholas Piggin * prevent hash faults on user addresses when reading callchains (and 1892156b5371SNicholas Piggin * looks better from an irq tracing perspective). 1893156b5371SNicholas Piggin */ 1894156b5371SNicholas Piggin if (IS_ENABLED(CONFIG_PPC64) && unlikely(arch_irq_disabled_regs(regs))) 1895156b5371SNicholas Piggin performance_monitor_exception_nmi(regs); 1896156b5371SNicholas Piggin else 1897156b5371SNicholas Piggin performance_monitor_exception_async(regs); 18983a96570fSNicholas Piggin 18993a96570fSNicholas Piggin return 0; 1900dc1c1ca3SStephen Rothwell } 1901dc1c1ca3SStephen Rothwell 1902172ae2e7SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_REGS 19033bffb652SDave Kleikamp static void handle_debug(struct pt_regs *regs, unsigned long debug_status) 19043bffb652SDave Kleikamp { 19053bffb652SDave Kleikamp int changed = 0; 19063bffb652SDave Kleikamp /* 19073bffb652SDave Kleikamp * Determine the cause of the debug event, clear the 19083bffb652SDave Kleikamp * event flags and send a trap to the handler. Torez 19093bffb652SDave Kleikamp */ 19103bffb652SDave Kleikamp if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) { 19113bffb652SDave Kleikamp dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W); 19123bffb652SDave Kleikamp #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE 191351ae8d4aSBharat Bhushan current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE; 19143bffb652SDave Kleikamp #endif 191547355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, 19163bffb652SDave Kleikamp 5); 19173bffb652SDave Kleikamp changed |= 0x01; 19183bffb652SDave Kleikamp } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) { 19193bffb652SDave Kleikamp dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W); 192047355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, 19213bffb652SDave Kleikamp 6); 19223bffb652SDave Kleikamp changed |= 0x01; 19233bffb652SDave Kleikamp } else if (debug_status & DBSR_IAC1) { 192451ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IAC1; 19253bffb652SDave Kleikamp dbcr_iac_range(current) &= ~DBCR_IAC12MODE; 192647355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, 19273bffb652SDave Kleikamp 1); 19283bffb652SDave Kleikamp changed |= 0x01; 19293bffb652SDave Kleikamp } else if (debug_status & DBSR_IAC2) { 193051ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IAC2; 193147355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, 19323bffb652SDave Kleikamp 2); 19333bffb652SDave Kleikamp changed |= 0x01; 19343bffb652SDave Kleikamp } else if (debug_status & DBSR_IAC3) { 193551ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IAC3; 19363bffb652SDave Kleikamp dbcr_iac_range(current) &= ~DBCR_IAC34MODE; 193747355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, 19383bffb652SDave Kleikamp 3); 19393bffb652SDave Kleikamp changed |= 0x01; 19403bffb652SDave Kleikamp } else if (debug_status & DBSR_IAC4) { 194151ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IAC4; 194247355040SEric W. Biederman do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, 19433bffb652SDave Kleikamp 4); 19443bffb652SDave Kleikamp changed |= 0x01; 19453bffb652SDave Kleikamp } 19463bffb652SDave Kleikamp /* 19473bffb652SDave Kleikamp * At the point this routine was called, the MSR(DE) was turned off. 19483bffb652SDave Kleikamp * Check all other debug flags and see if that bit needs to be turned 19493bffb652SDave Kleikamp * back on or not. 19503bffb652SDave Kleikamp */ 195151ae8d4aSBharat Bhushan if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0, 195251ae8d4aSBharat Bhushan current->thread.debug.dbcr1)) 19533bffb652SDave Kleikamp regs->msr |= MSR_DE; 19543bffb652SDave Kleikamp else 19553bffb652SDave Kleikamp /* Make sure the IDM flag is off */ 195651ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IDM; 19573bffb652SDave Kleikamp 19583bffb652SDave Kleikamp if (changed & 0x01) 195951ae8d4aSBharat Bhushan mtspr(SPRN_DBCR0, current->thread.debug.dbcr0); 19603bffb652SDave Kleikamp } 196114cf11afSPaul Mackerras 19623a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(DebugException) 196314cf11afSPaul Mackerras { 1964755d6641SNicholas Piggin unsigned long debug_status = regs->dsisr; 1965755d6641SNicholas Piggin 196651ae8d4aSBharat Bhushan current->thread.debug.dbsr = debug_status; 19673bffb652SDave Kleikamp 1968ec097c84SRoland McGrath /* Hack alert: On BookE, Branch Taken stops on the branch itself, while 1969ec097c84SRoland McGrath * on server, it stops on the target of the branch. In order to simulate 1970ec097c84SRoland McGrath * the server behaviour, we thus restart right away with a single step 1971ec097c84SRoland McGrath * instead of stopping here when hitting a BT 1972ec097c84SRoland McGrath */ 1973ec097c84SRoland McGrath if (debug_status & DBSR_BT) { 1974ec097c84SRoland McGrath regs->msr &= ~MSR_DE; 1975ec097c84SRoland McGrath 1976ec097c84SRoland McGrath /* Disable BT */ 1977ec097c84SRoland McGrath mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT); 1978ec097c84SRoland McGrath /* Clear the BT event */ 1979ec097c84SRoland McGrath mtspr(SPRN_DBSR, DBSR_BT); 1980ec097c84SRoland McGrath 1981ec097c84SRoland McGrath /* Do the single step trick only when coming from userspace */ 1982ec097c84SRoland McGrath if (user_mode(regs)) { 198351ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_BT; 198451ae8d4aSBharat Bhushan current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC; 1985ec097c84SRoland McGrath regs->msr |= MSR_DE; 1986ec097c84SRoland McGrath return; 1987ec097c84SRoland McGrath } 1988ec097c84SRoland McGrath 19896cc89badSNaveen N. Rao if (kprobe_post_handler(regs)) 19906cc89badSNaveen N. Rao return; 19916cc89badSNaveen N. Rao 1992ec097c84SRoland McGrath if (notify_die(DIE_SSTEP, "block_step", regs, 5, 1993ec097c84SRoland McGrath 5, SIGTRAP) == NOTIFY_STOP) { 1994ec097c84SRoland McGrath return; 1995ec097c84SRoland McGrath } 1996ec097c84SRoland McGrath if (debugger_sstep(regs)) 1997ec097c84SRoland McGrath return; 1998ec097c84SRoland McGrath } else if (debug_status & DBSR_IC) { /* Instruction complete */ 199914cf11afSPaul Mackerras regs->msr &= ~MSR_DE; 2000f8279621SKumar Gala 200114cf11afSPaul Mackerras /* Disable instruction completion */ 200214cf11afSPaul Mackerras mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC); 200314cf11afSPaul Mackerras /* Clear the instruction completion event */ 200414cf11afSPaul Mackerras mtspr(SPRN_DBSR, DBSR_IC); 2005f8279621SKumar Gala 20066cc89badSNaveen N. Rao if (kprobe_post_handler(regs)) 20076cc89badSNaveen N. Rao return; 20086cc89badSNaveen N. Rao 2009f8279621SKumar Gala if (notify_die(DIE_SSTEP, "single_step", regs, 5, 2010f8279621SKumar Gala 5, SIGTRAP) == NOTIFY_STOP) { 201114cf11afSPaul Mackerras return; 201214cf11afSPaul Mackerras } 2013f8279621SKumar Gala 2014f8279621SKumar Gala if (debugger_sstep(regs)) 2015f8279621SKumar Gala return; 2016f8279621SKumar Gala 20173bffb652SDave Kleikamp if (user_mode(regs)) { 201851ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IC; 201951ae8d4aSBharat Bhushan if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0, 202051ae8d4aSBharat Bhushan current->thread.debug.dbcr1)) 20213bffb652SDave Kleikamp regs->msr |= MSR_DE; 20223bffb652SDave Kleikamp else 20233bffb652SDave Kleikamp /* Make sure the IDM bit is off */ 202451ae8d4aSBharat Bhushan current->thread.debug.dbcr0 &= ~DBCR0_IDM; 20253bffb652SDave Kleikamp } 2026f8279621SKumar Gala 2027f8279621SKumar Gala _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip); 20283bffb652SDave Kleikamp } else 20293bffb652SDave Kleikamp handle_debug(regs, debug_status); 203014cf11afSPaul Mackerras } 2031172ae2e7SDave Kleikamp #endif /* CONFIG_PPC_ADV_DEBUG_REGS */ 203214cf11afSPaul Mackerras 203314cf11afSPaul Mackerras #ifdef CONFIG_ALTIVEC 20343a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(altivec_assist_exception) 203514cf11afSPaul Mackerras { 203614cf11afSPaul Mackerras int err; 203714cf11afSPaul Mackerras 203814cf11afSPaul Mackerras if (!user_mode(regs)) { 203914cf11afSPaul Mackerras printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode" 204014cf11afSPaul Mackerras " at %lx\n", regs->nip); 20418dad3f92SPaul Mackerras die("Kernel VMX/Altivec assist exception", regs, SIGILL); 204214cf11afSPaul Mackerras } 204314cf11afSPaul Mackerras 2044dc1c1ca3SStephen Rothwell flush_altivec_to_thread(current); 2045dc1c1ca3SStephen Rothwell 2046eecff81dSAnton Blanchard PPC_WARN_EMULATED(altivec, regs); 204714cf11afSPaul Mackerras err = emulate_altivec(regs); 204814cf11afSPaul Mackerras if (err == 0) { 204914cf11afSPaul Mackerras regs->nip += 4; /* skip emulated instruction */ 205014cf11afSPaul Mackerras emulate_single_step(regs); 205114cf11afSPaul Mackerras return; 205214cf11afSPaul Mackerras } 205314cf11afSPaul Mackerras 205414cf11afSPaul Mackerras if (err == -EFAULT) { 205514cf11afSPaul Mackerras /* got an error reading the instruction */ 205614cf11afSPaul Mackerras _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip); 205714cf11afSPaul Mackerras } else { 205814cf11afSPaul Mackerras /* didn't recognize the instruction */ 205914cf11afSPaul Mackerras /* XXX quick hack for now: set the non-Java bit in the VSCR */ 206076462232SChristian Dietrich printk_ratelimited(KERN_ERR "Unrecognized altivec instruction " 206114cf11afSPaul Mackerras "in %s at %lx\n", current->comm, regs->nip); 2062de79f7b9SPaul Mackerras current->thread.vr_state.vscr.u[3] |= 0x10000; 206314cf11afSPaul Mackerras } 206414cf11afSPaul Mackerras } 206514cf11afSPaul Mackerras #endif /* CONFIG_ALTIVEC */ 206614cf11afSPaul Mackerras 206714cf11afSPaul Mackerras #ifdef CONFIG_FSL_BOOKE 20683a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(CacheLockingException) 206914cf11afSPaul Mackerras { 2070b4ced803SNicholas Piggin unsigned long error_code = regs->dsisr; 2071b4ced803SNicholas Piggin 207214cf11afSPaul Mackerras /* We treat cache locking instructions from the user 207314cf11afSPaul Mackerras * as priv ops, in the future we could try to do 207414cf11afSPaul Mackerras * something smarter 207514cf11afSPaul Mackerras */ 207614cf11afSPaul Mackerras if (error_code & (ESR_DLK|ESR_ILK)) 207714cf11afSPaul Mackerras _exception(SIGILL, regs, ILL_PRVOPC, regs->nip); 207814cf11afSPaul Mackerras return; 207914cf11afSPaul Mackerras } 208014cf11afSPaul Mackerras #endif /* CONFIG_FSL_BOOKE */ 208114cf11afSPaul Mackerras 208214cf11afSPaul Mackerras #ifdef CONFIG_SPE 20833a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SPEFloatingPointException) 208414cf11afSPaul Mackerras { 20856a800f36SLiu Yu extern int do_spe_mathemu(struct pt_regs *regs); 208614cf11afSPaul Mackerras unsigned long spefscr; 208714cf11afSPaul Mackerras int fpexc_mode; 2088aeb1c0f6SEric W. Biederman int code = FPE_FLTUNK; 20896a800f36SLiu Yu int err; 20906a800f36SLiu Yu 2091e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 2092ef429124SChristophe Leroy 2093685659eeSyu liu flush_spe_to_thread(current); 209414cf11afSPaul Mackerras 209514cf11afSPaul Mackerras spefscr = current->thread.spefscr; 209614cf11afSPaul Mackerras fpexc_mode = current->thread.fpexc_mode; 209714cf11afSPaul Mackerras 209814cf11afSPaul Mackerras if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) { 209914cf11afSPaul Mackerras code = FPE_FLTOVF; 210014cf11afSPaul Mackerras } 210114cf11afSPaul Mackerras else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) { 210214cf11afSPaul Mackerras code = FPE_FLTUND; 210314cf11afSPaul Mackerras } 210414cf11afSPaul Mackerras else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV)) 210514cf11afSPaul Mackerras code = FPE_FLTDIV; 210614cf11afSPaul Mackerras else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) { 210714cf11afSPaul Mackerras code = FPE_FLTINV; 210814cf11afSPaul Mackerras } 210914cf11afSPaul Mackerras else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES)) 211014cf11afSPaul Mackerras code = FPE_FLTRES; 211114cf11afSPaul Mackerras 21126a800f36SLiu Yu err = do_spe_mathemu(regs); 21136a800f36SLiu Yu if (err == 0) { 21146a800f36SLiu Yu regs->nip += 4; /* skip emulated instruction */ 21156a800f36SLiu Yu emulate_single_step(regs); 211614cf11afSPaul Mackerras return; 211714cf11afSPaul Mackerras } 21186a800f36SLiu Yu 21196a800f36SLiu Yu if (err == -EFAULT) { 21206a800f36SLiu Yu /* got an error reading the instruction */ 21216a800f36SLiu Yu _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip); 21226a800f36SLiu Yu } else if (err == -EINVAL) { 21236a800f36SLiu Yu /* didn't recognize the instruction */ 21246a800f36SLiu Yu printk(KERN_ERR "unrecognized spe instruction " 21256a800f36SLiu Yu "in %s at %lx\n", current->comm, regs->nip); 21266a800f36SLiu Yu } else { 21276a800f36SLiu Yu _exception(SIGFPE, regs, code, regs->nip); 21286a800f36SLiu Yu } 21296a800f36SLiu Yu 21306a800f36SLiu Yu return; 21316a800f36SLiu Yu } 21326a800f36SLiu Yu 21333a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(SPEFloatingPointRoundException) 21346a800f36SLiu Yu { 21356a800f36SLiu Yu extern int speround_handler(struct pt_regs *regs); 21366a800f36SLiu Yu int err; 21376a800f36SLiu Yu 2138e6f8a6c8SNicholas Piggin interrupt_cond_local_irq_enable(regs); 2139ef429124SChristophe Leroy 21406a800f36SLiu Yu preempt_disable(); 21416a800f36SLiu Yu if (regs->msr & MSR_SPE) 21426a800f36SLiu Yu giveup_spe(current); 21436a800f36SLiu Yu preempt_enable(); 21446a800f36SLiu Yu 21456a800f36SLiu Yu regs->nip -= 4; 21466a800f36SLiu Yu err = speround_handler(regs); 21476a800f36SLiu Yu if (err == 0) { 21486a800f36SLiu Yu regs->nip += 4; /* skip emulated instruction */ 21496a800f36SLiu Yu emulate_single_step(regs); 21506a800f36SLiu Yu return; 21516a800f36SLiu Yu } 21526a800f36SLiu Yu 21536a800f36SLiu Yu if (err == -EFAULT) { 21546a800f36SLiu Yu /* got an error reading the instruction */ 21556a800f36SLiu Yu _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip); 21566a800f36SLiu Yu } else if (err == -EINVAL) { 21576a800f36SLiu Yu /* didn't recognize the instruction */ 21586a800f36SLiu Yu printk(KERN_ERR "unrecognized spe instruction " 21596a800f36SLiu Yu "in %s at %lx\n", current->comm, regs->nip); 21606a800f36SLiu Yu } else { 2161aeb1c0f6SEric W. Biederman _exception(SIGFPE, regs, FPE_FLTUNK, regs->nip); 21626a800f36SLiu Yu return; 21636a800f36SLiu Yu } 21646a800f36SLiu Yu } 216514cf11afSPaul Mackerras #endif 216614cf11afSPaul Mackerras 2167dc1c1ca3SStephen Rothwell /* 2168dc1c1ca3SStephen Rothwell * We enter here if we get an unrecoverable exception, that is, one 2169dc1c1ca3SStephen Rothwell * that happened at a point where the RI (recoverable interrupt) bit 2170dc1c1ca3SStephen Rothwell * in the MSR is 0. This indicates that SRR0/1 are live, and that 2171dc1c1ca3SStephen Rothwell * we therefore lost state by taking this exception. 2172dc1c1ca3SStephen Rothwell */ 21730b736881SChristophe Leroy void unrecoverable_exception(struct pt_regs *regs) 2174dc1c1ca3SStephen Rothwell { 217551423a9cSChristophe Leroy pr_emerg("Unrecoverable exception %lx at %lx (msr=%lx)\n", 217651423a9cSChristophe Leroy regs->trap, regs->nip, regs->msr); 2177dc1c1ca3SStephen Rothwell die("Unrecoverable exception", regs, SIGABRT); 2178dc1c1ca3SStephen Rothwell } 2179dc1c1ca3SStephen Rothwell 21801e18c17aSJason Gunthorpe #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x) 218114cf11afSPaul Mackerras /* 218214cf11afSPaul Mackerras * Default handler for a Watchdog exception, 218314cf11afSPaul Mackerras * spins until a reboot occurs 218414cf11afSPaul Mackerras */ 218514cf11afSPaul Mackerras void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs) 218614cf11afSPaul Mackerras { 218714cf11afSPaul Mackerras /* Generic WatchdogHandler, implement your own */ 218814cf11afSPaul Mackerras mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE)); 218914cf11afSPaul Mackerras return; 219014cf11afSPaul Mackerras } 219114cf11afSPaul Mackerras 21923a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(WatchdogException) /* XXX NMI? async? */ 219314cf11afSPaul Mackerras { 219414cf11afSPaul Mackerras printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n"); 219514cf11afSPaul Mackerras WatchdogHandler(regs); 219614cf11afSPaul Mackerras } 219714cf11afSPaul Mackerras #endif 2198dc1c1ca3SStephen Rothwell 2199dc1c1ca3SStephen Rothwell /* 2200dc1c1ca3SStephen Rothwell * We enter here if we discover during exception entry that we are 2201dc1c1ca3SStephen Rothwell * running in supervisor mode with a userspace value in the stack pointer. 2202dc1c1ca3SStephen Rothwell */ 22033a96570fSNicholas Piggin DEFINE_INTERRUPT_HANDLER(kernel_bad_stack) 2204dc1c1ca3SStephen Rothwell { 2205dc1c1ca3SStephen Rothwell printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n", 2206dc1c1ca3SStephen Rothwell regs->gpr[1], regs->nip); 2207dc1c1ca3SStephen Rothwell die("Bad kernel stack pointer", regs, SIGABRT); 2208dc1c1ca3SStephen Rothwell } 220914cf11afSPaul Mackerras 221014cf11afSPaul Mackerras void __init trap_init(void) 221114cf11afSPaul Mackerras { 221214cf11afSPaul Mackerras } 221380947e7cSGeert Uytterhoeven 221480947e7cSGeert Uytterhoeven 221580947e7cSGeert Uytterhoeven #ifdef CONFIG_PPC_EMULATED_STATS 221680947e7cSGeert Uytterhoeven 221780947e7cSGeert Uytterhoeven #define WARN_EMULATED_SETUP(type) .type = { .name = #type } 221880947e7cSGeert Uytterhoeven 221980947e7cSGeert Uytterhoeven struct ppc_emulated ppc_emulated = { 222080947e7cSGeert Uytterhoeven #ifdef CONFIG_ALTIVEC 222180947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(altivec), 222280947e7cSGeert Uytterhoeven #endif 222380947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(dcba), 222480947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(dcbz), 222580947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(fp_pair), 222680947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(isel), 222780947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(mcrxr), 222880947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(mfpvr), 222980947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(multiple), 223080947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(popcntb), 223180947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(spe), 223280947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(string), 2233a3821b2aSScott Wood WARN_EMULATED_SETUP(sync), 223480947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(unaligned), 223580947e7cSGeert Uytterhoeven #ifdef CONFIG_MATH_EMULATION 223680947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(math), 223780947e7cSGeert Uytterhoeven #endif 223880947e7cSGeert Uytterhoeven #ifdef CONFIG_VSX 223980947e7cSGeert Uytterhoeven WARN_EMULATED_SETUP(vsx), 224080947e7cSGeert Uytterhoeven #endif 2241efcac658SAlexey Kardashevskiy #ifdef CONFIG_PPC64 2242efcac658SAlexey Kardashevskiy WARN_EMULATED_SETUP(mfdscr), 2243efcac658SAlexey Kardashevskiy WARN_EMULATED_SETUP(mtdscr), 2244f83319d7SAnton Blanchard WARN_EMULATED_SETUP(lq_stq), 22455080332cSMichael Neuling WARN_EMULATED_SETUP(lxvw4x), 22465080332cSMichael Neuling WARN_EMULATED_SETUP(lxvh8x), 22475080332cSMichael Neuling WARN_EMULATED_SETUP(lxvd2x), 22485080332cSMichael Neuling WARN_EMULATED_SETUP(lxvb16x), 2249efcac658SAlexey Kardashevskiy #endif 225080947e7cSGeert Uytterhoeven }; 225180947e7cSGeert Uytterhoeven 225280947e7cSGeert Uytterhoeven u32 ppc_warn_emulated; 225380947e7cSGeert Uytterhoeven 225480947e7cSGeert Uytterhoeven void ppc_warn_emulated_print(const char *type) 225580947e7cSGeert Uytterhoeven { 225676462232SChristian Dietrich pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm, 225780947e7cSGeert Uytterhoeven type); 225880947e7cSGeert Uytterhoeven } 225980947e7cSGeert Uytterhoeven 226080947e7cSGeert Uytterhoeven static int __init ppc_warn_emulated_init(void) 226180947e7cSGeert Uytterhoeven { 2262860286cfSGreg Kroah-Hartman struct dentry *dir; 226380947e7cSGeert Uytterhoeven unsigned int i; 226480947e7cSGeert Uytterhoeven struct ppc_emulated_entry *entries = (void *)&ppc_emulated; 226580947e7cSGeert Uytterhoeven 226680947e7cSGeert Uytterhoeven dir = debugfs_create_dir("emulated_instructions", 226780947e7cSGeert Uytterhoeven powerpc_debugfs_root); 226880947e7cSGeert Uytterhoeven 2269860286cfSGreg Kroah-Hartman debugfs_create_u32("do_warn", 0644, dir, &ppc_warn_emulated); 227080947e7cSGeert Uytterhoeven 2271860286cfSGreg Kroah-Hartman for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) 2272860286cfSGreg Kroah-Hartman debugfs_create_u32(entries[i].name, 0644, dir, 227380947e7cSGeert Uytterhoeven (u32 *)&entries[i].val.counter); 227480947e7cSGeert Uytterhoeven 227580947e7cSGeert Uytterhoeven return 0; 227680947e7cSGeert Uytterhoeven } 227780947e7cSGeert Uytterhoeven 227880947e7cSGeert Uytterhoeven device_initcall(ppc_warn_emulated_init); 227980947e7cSGeert Uytterhoeven 228080947e7cSGeert Uytterhoeven #endif /* CONFIG_PPC_EMULATED_STATS */ 2281