blob: 2393d3c462360e0e95713eba195427e373b4f566 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
Scott Woodfe04b112010-04-08 00:38:22 -05003 * Copyright 2007-2010 Freescale Semiconductor, Inc.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Modified by Cort Dougan (cort@cs.nmt.edu)
11 * and Paul Mackerras (paulus@samba.org)
12 */
13
14/*
15 * This file handles the architecture-dependent parts of hardware exceptions
16 */
17
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018#include <linux/errno.h>
19#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100021#include <linux/kernel.h>
22#include <linux/mm.h>
23#include <linux/stddef.h>
24#include <linux/unistd.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100025#include <linux/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/user.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100028#include <linux/init.h>
Paul Gortmaker8a39b052016-08-16 10:57:34 -040029#include <linux/extable.h>
30#include <linux/module.h> /* print_modules */
Paul Mackerras8dad3f92005-10-06 13:27:05 +100031#include <linux/prctl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100032#include <linux/delay.h>
33#include <linux/kprobes.h>
Michael Ellermancc532912005-12-04 18:39:43 +110034#include <linux/kexec.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070035#include <linux/backlight.h>
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -080036#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070037#include <linux/kdebug.h>
Christian Dietrich76462232011-06-04 05:36:54 +000038#include <linux/ratelimit.h>
Li Zhongba12eed2013-05-13 16:16:41 +000039#include <linux/context_tracking.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000041#include <asm/emulated_ops.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100042#include <asm/pgtable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Michael Ellerman7644d582017-02-10 12:04:56 +110044#include <asm/debugfs.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100045#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100046#include <asm/machdep.h>
47#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100048#include <asm/pmc.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#include <asm/reg.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100050#ifdef CONFIG_PMAC_BACKLIGHT
51#include <asm/backlight.h>
52#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100053#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100054#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100055#include <asm/processor.h>
Michael Neuling6ce6c622013-05-26 18:09:39 +000056#include <asm/tm.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100057#endif
David Wilderc0ce7d02006-06-23 15:29:34 -070058#include <asm/kexec.h>
Kumar Gala16c57b32009-02-10 20:10:44 +000059#include <asm/ppc-opcode.h>
Shaohui Xiecce1f102010-11-18 14:57:32 +080060#include <asm/rio.h>
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +000061#include <asm/fadump.h>
David Howellsae3a1972012-03-28 18:30:02 +010062#include <asm/switch_to.h>
Michael Neulingf54db642013-02-13 16:21:39 +000063#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010064#include <asm/debug.h>
Daniel Axtens42f5b4c2016-05-18 11:16:50 +100065#include <asm/asm-prototypes.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053066#include <asm/hmi.h>
Hongtao Jia4e0e3432013-04-28 13:20:08 +080067#include <sysdev/fsl_pci.h>
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +053068#include <asm/kprobes.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100069
Thiago Jung Bauermannda665882016-11-29 23:45:50 +110070#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC_CORE)
Anton Blanchard5be34922010-01-12 00:50:14 +000071int (*__debugger)(struct pt_regs *regs) __read_mostly;
72int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
73int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
74int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
75int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
Michael Neuling9422de32012-12-20 14:06:44 +000076int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
Anton Blanchard5be34922010-01-12 00:50:14 +000077int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100078
79EXPORT_SYMBOL(__debugger);
80EXPORT_SYMBOL(__debugger_ipi);
81EXPORT_SYMBOL(__debugger_bpt);
82EXPORT_SYMBOL(__debugger_sstep);
83EXPORT_SYMBOL(__debugger_iabr_match);
Michael Neuling9422de32012-12-20 14:06:44 +000084EXPORT_SYMBOL(__debugger_break_match);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100085EXPORT_SYMBOL(__debugger_fault_handler);
86#endif
87
Michael Neuling8b3c34c2013-02-13 16:21:32 +000088/* Transactional Memory trap debug */
89#ifdef TM_DEBUG_SW
90#define TM_DEBUG(x...) printk(KERN_INFO x)
91#else
92#define TM_DEBUG(x...) do { } while(0)
93#endif
94
Paul Mackerras14cf11a2005-09-26 16:04:21 +100095/*
96 * Trap & Exception support
97 */
98
anton@samba.org6031d9d2007-03-20 20:38:12 -050099#ifdef CONFIG_PMAC_BACKLIGHT
100static void pmac_backlight_unblank(void)
101{
102 mutex_lock(&pmac_backlight_mutex);
103 if (pmac_backlight) {
104 struct backlight_properties *props;
105
106 props = &pmac_backlight->props;
107 props->brightness = props->max_brightness;
108 props->power = FB_BLANK_UNBLANK;
109 backlight_update_status(pmac_backlight);
110 }
111 mutex_unlock(&pmac_backlight_mutex);
112}
113#else
114static inline void pmac_backlight_unblank(void) { }
115#endif
116
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000117static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
118static int die_owner = -1;
119static unsigned int die_nest_count;
120static int die_counter;
121
Nicholas Piggin03465f82016-09-16 20:48:08 +1000122static unsigned long oops_begin(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000123{
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000124 int cpu;
anton@samba.org34c2a142007-03-20 20:38:13 -0500125 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000126
anton@samba.org293e4682007-03-20 20:38:11 -0500127 oops_enter();
128
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000129 /* racy, but better than risking deadlock. */
130 raw_local_irq_save(flags);
131 cpu = smp_processor_id();
132 if (!arch_spin_trylock(&die_lock)) {
133 if (cpu == die_owner)
134 /* nested oops. should stop eventually */;
135 else
136 arch_spin_lock(&die_lock);
anton@samba.org34c2a142007-03-20 20:38:13 -0500137 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000138 die_nest_count++;
139 die_owner = cpu;
140 console_verbose();
141 bust_spinlocks(1);
142 if (machine_is(powermac))
143 pmac_backlight_unblank();
144 return flags;
145}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000146NOKPROBE_SYMBOL(oops_begin);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700147
Nicholas Piggin03465f82016-09-16 20:48:08 +1000148static void oops_end(unsigned long flags, struct pt_regs *regs,
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000149 int signr)
150{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000151 bust_spinlocks(0);
Rusty Russell373d4d02013-01-21 17:17:39 +1030152 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000153 die_nest_count--;
Anton Blanchard58154c82011-11-30 00:23:09 +0000154 oops_exit();
155 printk("\n");
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100156 if (!die_nest_count) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000157 /* Nest count reaches zero, release the lock. */
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100158 die_owner = -1;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000159 arch_spin_unlock(&die_lock);
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100160 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000161 raw_local_irq_restore(flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700162
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000163 crash_fadump(regs, "die oops");
164
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000165 /*
166 * A system reset (0x100) is a request to dump, so we always send
167 * it through the crashdump code.
168 */
169 if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700170 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000171
172 /*
173 * We aren't the primary crash CPU. We need to send it
174 * to a holding pattern to avoid it ending up in the panic
175 * code.
176 */
177 crash_kexec_secondary(regs);
178 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000179
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000180 if (!signr)
181 return;
182
Anton Blanchard58154c82011-11-30 00:23:09 +0000183 /*
184 * While our oops output is serialised by a spinlock, output
185 * from panic() called below can race and corrupt it. If we
186 * know we are going to panic, delay for 1 second so we have a
187 * chance to get clean backtraces from all CPUs that are oopsing.
188 */
189 if (in_interrupt() || panic_on_oops || !current->pid ||
190 is_global_init(current)) {
191 mdelay(MSEC_PER_SEC);
192 }
193
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000194 if (in_interrupt())
195 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700196 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700197 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000198 do_exit(signr);
199}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000200NOKPROBE_SYMBOL(oops_end);
Hormscea6a4b2006-07-30 03:03:34 -0700201
Nicholas Piggin03465f82016-09-16 20:48:08 +1000202static int __die(const char *str, struct pt_regs *regs, long err)
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000203{
204 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
205#ifdef CONFIG_PREEMPT
206 printk("PREEMPT ");
207#endif
208#ifdef CONFIG_SMP
209 printk("SMP NR_CPUS=%d ", NR_CPUS);
210#endif
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700211 if (debug_pagealloc_enabled())
212 printk("DEBUG_PAGEALLOC ");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000213#ifdef CONFIG_NUMA
214 printk("NUMA ");
215#endif
216 printk("%s\n", ppc_md.name ? ppc_md.name : "");
217
218 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
219 return 1;
220
221 print_modules();
222 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000223
224 return 0;
225}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000226NOKPROBE_SYMBOL(__die);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000228void die(const char *str, struct pt_regs *regs, long err)
229{
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100230 unsigned long flags;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000231
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100232 if (debugger(regs))
233 return;
234
235 flags = oops_begin(regs);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000236 if (__die(str, regs, err))
237 err = 0;
238 oops_end(flags, regs, err);
239}
240
Oleg Nesterov25baa352009-12-15 16:47:18 -0800241void user_single_step_siginfo(struct task_struct *tsk,
242 struct pt_regs *regs, siginfo_t *info)
243{
244 memset(info, 0, sizeof(*info));
245 info->si_signo = SIGTRAP;
246 info->si_code = TRAP_TRACE;
247 info->si_addr = (void __user *)regs->nip;
248}
249
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000250void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
251{
252 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000253 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
254 "at %08lx nip %08lx lr %08lx code %x\n";
255 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
256 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000257
258 if (!user_mode(regs)) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000259 die("Exception in kernel mode", regs, signr);
260 return;
261 }
262
263 if (show_unhandled_signals && unhandled_signal(current, signr)) {
Christian Dietrich76462232011-06-04 05:36:54 +0000264 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
265 current->comm, current->pid, signr,
266 addr, regs->nip, regs->link, code);
267 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +1000269 if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
Benjamin Herrenschmidt9f2f79e2012-03-01 15:47:44 +1100270 local_irq_enable();
271
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000272 current->thread.trap_nr = code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000273 memset(&info, 0, sizeof(info));
274 info.si_signo = signr;
275 info.si_code = code;
276 info.si_addr = (void __user *) addr;
277 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000278}
279
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000280void system_reset_exception(struct pt_regs *regs)
281{
282 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000283 if (ppc_md.system_reset_exception) {
284 if (ppc_md.system_reset_exception(regs))
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000285 goto out;
Arnd Bergmannc902be72006-01-04 19:55:53 +0000286 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000287
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000288 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000289
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000290out:
291#ifdef CONFIG_PPC_BOOK3S_64
292 BUG_ON(get_paca()->in_nmi == 0);
293 if (get_paca()->in_nmi > 1)
294 panic("Unrecoverable nested System Reset");
295#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296 /* Must die if the interrupt is not recoverable */
297 if (!(regs->msr & MSR_RI))
298 panic("Unrecoverable System Reset");
299
300 /* What should we do here? We could issue a shutdown or hard reset. */
301}
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530302
Christophe Leroyf3079392016-09-05 08:42:31 +0200303#ifdef CONFIG_PPC64
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530304/*
305 * This function is called in real mode. Strictly no printk's please.
306 *
307 * regs->nip and regs->msr contains srr0 and ssr1.
308 */
309long machine_check_early(struct pt_regs *regs)
310{
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530311 long handled = 0;
312
Christoph Lameter69111ba2014-10-21 15:23:25 -0500313 __this_cpu_inc(irq_stat.mce_exceptions);
Mahesh Salgaonkare6654d52014-06-11 14:18:07 +0530314
Daniel Axtens27ea2c42015-06-15 13:25:19 +1000315 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
316
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530317 if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
318 handled = cur_cpu_spec->machine_check_early(regs);
319 return handled;
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530320}
321
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530322long hmi_exception_realmode(struct pt_regs *regs)
323{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500324 __this_cpu_inc(irq_stat.hmi_exceptions);
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530325
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530326 wait_for_subcore_guest_exit();
327
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530328 if (ppc_md.hmi_exception_early)
329 ppc_md.hmi_exception_early(regs);
330
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530331 wait_for_tb_resync();
332
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530333 return 0;
334}
335
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336#endif
337
338/*
339 * I/O accesses can cause machine checks on powermacs.
340 * Check if the NIP corresponds to the address of a sync
341 * instruction for which there is an entry in the exception
342 * table.
343 * Note that the 601 only takes a machine check on TEA
344 * (transfer error ack) signal assertion, and does not
345 * set any of the top 16 bits of SRR1.
346 * -- paulus.
347 */
348static inline int check_io_access(struct pt_regs *regs)
349{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100350#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000351 unsigned long msr = regs->msr;
352 const struct exception_table_entry *entry;
353 unsigned int *nip = (unsigned int *)regs->nip;
354
355 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
356 && (entry = search_exception_tables(regs->nip)) != NULL) {
357 /*
358 * Check that it's a sync instruction, or somewhere
359 * in the twi; isync; nop sequence that inb/inw/inl uses.
360 * As the address is in the exception table
361 * we should be able to read the instr there.
362 * For the debug message, we look at the preceding
363 * load or store.
364 */
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200365 if (*nip == PPC_INST_NOP)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000366 nip -= 2;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200367 else if (*nip == PPC_INST_ISYNC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000368 --nip;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200369 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370 unsigned int rb;
371
372 --nip;
373 rb = (*nip >> 11) & 0x1f;
374 printk(KERN_DEBUG "%s bad port %lx at %p\n",
375 (*nip & 0x100)? "OUT to": "IN from",
376 regs->gpr[rb] - _IO_BASE, nip);
377 regs->msr |= MSR_RI;
Nicholas Piggin61a92f72016-10-14 16:47:31 +1100378 regs->nip = extable_fixup(entry);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000379 return 1;
380 }
381 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100382#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000383 return 0;
384}
385
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000386#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000387/* On 4xx, the reason for the machine check or program exception
388 is in the ESR. */
389#define get_reason(regs) ((regs)->dsisr)
390#ifndef CONFIG_FSL_BOOKE
391#define get_mc_reason(regs) ((regs)->dsisr)
392#else
Scott Woodfe04b112010-04-08 00:38:22 -0500393#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000394#endif
395#define REASON_FP ESR_FP
396#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
397#define REASON_PRIVILEGED ESR_PPR
398#define REASON_TRAP ESR_PTR
399
400/* single-step stuff */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530401#define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
402#define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000403
404#else
405/* On non-4xx, the reason for the machine check or program
406 exception is in the MSR. */
407#define get_reason(regs) ((regs)->msr)
408#define get_mc_reason(regs) ((regs)->msr)
Michael Neuling8b3c34c2013-02-13 16:21:32 +0000409#define REASON_TM 0x200000
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000410#define REASON_FP 0x100000
411#define REASON_ILLEGAL 0x80000
412#define REASON_PRIVILEGED 0x40000
413#define REASON_TRAP 0x20000
414
415#define single_stepping(regs) ((regs)->msr & MSR_SE)
416#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
417#endif
418
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100419#if defined(CONFIG_4xx)
420int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000421{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600422 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000423
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000424 if (reason & ESR_IMCP) {
425 printk("Instruction");
426 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
427 } else
428 printk("Data");
429 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100430
431 return 0;
432}
433
434int machine_check_440A(struct pt_regs *regs)
435{
436 unsigned long reason = get_mc_reason(regs);
437
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000438 printk("Machine check in kernel mode.\n");
439 if (reason & ESR_IMCP){
440 printk("Instruction Synchronous Machine Check exception\n");
441 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
442 }
443 else {
444 u32 mcsr = mfspr(SPRN_MCSR);
445 if (mcsr & MCSR_IB)
446 printk("Instruction Read PLB Error\n");
447 if (mcsr & MCSR_DRB)
448 printk("Data Read PLB Error\n");
449 if (mcsr & MCSR_DWB)
450 printk("Data Write PLB Error\n");
451 if (mcsr & MCSR_TLBP)
452 printk("TLB Parity Error\n");
453 if (mcsr & MCSR_ICP){
454 flush_instruction_cache();
455 printk("I-Cache Parity Error\n");
456 }
457 if (mcsr & MCSR_DCSP)
458 printk("D-Cache Search Parity Error\n");
459 if (mcsr & MCSR_DCFP)
460 printk("D-Cache Flush Parity Error\n");
461 if (mcsr & MCSR_IMPE)
462 printk("Machine Check exception is imprecise\n");
463
464 /* Clear MCSR */
465 mtspr(SPRN_MCSR, mcsr);
466 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100467 return 0;
468}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000469
470int machine_check_47x(struct pt_regs *regs)
471{
472 unsigned long reason = get_mc_reason(regs);
473 u32 mcsr;
474
475 printk(KERN_ERR "Machine check in kernel mode.\n");
476 if (reason & ESR_IMCP) {
477 printk(KERN_ERR
478 "Instruction Synchronous Machine Check exception\n");
479 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
480 return 0;
481 }
482 mcsr = mfspr(SPRN_MCSR);
483 if (mcsr & MCSR_IB)
484 printk(KERN_ERR "Instruction Read PLB Error\n");
485 if (mcsr & MCSR_DRB)
486 printk(KERN_ERR "Data Read PLB Error\n");
487 if (mcsr & MCSR_DWB)
488 printk(KERN_ERR "Data Write PLB Error\n");
489 if (mcsr & MCSR_TLBP)
490 printk(KERN_ERR "TLB Parity Error\n");
491 if (mcsr & MCSR_ICP) {
492 flush_instruction_cache();
493 printk(KERN_ERR "I-Cache Parity Error\n");
494 }
495 if (mcsr & MCSR_DCSP)
496 printk(KERN_ERR "D-Cache Search Parity Error\n");
497 if (mcsr & PPC47x_MCSR_GPR)
498 printk(KERN_ERR "GPR Parity Error\n");
499 if (mcsr & PPC47x_MCSR_FPR)
500 printk(KERN_ERR "FPR Parity Error\n");
501 if (mcsr & PPC47x_MCSR_IPR)
502 printk(KERN_ERR "Machine Check exception is imprecise\n");
503
504 /* Clear MCSR */
505 mtspr(SPRN_MCSR, mcsr);
506
507 return 0;
508}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100509#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500510int machine_check_e500mc(struct pt_regs *regs)
511{
512 unsigned long mcsr = mfspr(SPRN_MCSR);
513 unsigned long reason = mcsr;
514 int recoverable = 1;
515
Scott Wood82a9a482011-06-16 14:09:17 -0500516 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800517 recoverable = fsl_rio_mcheck_exception(regs);
518 if (recoverable == 1)
519 goto silent_out;
520 }
521
Scott Woodfe04b112010-04-08 00:38:22 -0500522 printk("Machine check in kernel mode.\n");
523 printk("Caused by (from MCSR=%lx): ", reason);
524
525 if (reason & MCSR_MCP)
526 printk("Machine Check Signal\n");
527
528 if (reason & MCSR_ICPERR) {
529 printk("Instruction Cache Parity Error\n");
530
531 /*
532 * This is recoverable by invalidating the i-cache.
533 */
534 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
535 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
536 ;
537
538 /*
539 * This will generally be accompanied by an instruction
540 * fetch error report -- only treat MCSR_IF as fatal
541 * if it wasn't due to an L1 parity error.
542 */
543 reason &= ~MCSR_IF;
544 }
545
546 if (reason & MCSR_DCPERR_MC) {
547 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500548
549 /*
550 * In write shadow mode we auto-recover from the error, but it
551 * may still get logged and cause a machine check. We should
552 * only treat the non-write shadow case as non-recoverable.
553 */
554 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
555 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500556 }
557
558 if (reason & MCSR_L2MMU_MHIT) {
559 printk("Hit on multiple TLB entries\n");
560 recoverable = 0;
561 }
562
563 if (reason & MCSR_NMI)
564 printk("Non-maskable interrupt\n");
565
566 if (reason & MCSR_IF) {
567 printk("Instruction Fetch Error Report\n");
568 recoverable = 0;
569 }
570
571 if (reason & MCSR_LD) {
572 printk("Load Error Report\n");
573 recoverable = 0;
574 }
575
576 if (reason & MCSR_ST) {
577 printk("Store Error Report\n");
578 recoverable = 0;
579 }
580
581 if (reason & MCSR_LDG) {
582 printk("Guarded Load Error Report\n");
583 recoverable = 0;
584 }
585
586 if (reason & MCSR_TLBSYNC)
587 printk("Simultaneous tlbsync operations\n");
588
589 if (reason & MCSR_BSL2_ERR) {
590 printk("Level 2 Cache Error\n");
591 recoverable = 0;
592 }
593
594 if (reason & MCSR_MAV) {
595 u64 addr;
596
597 addr = mfspr(SPRN_MCAR);
598 addr |= (u64)mfspr(SPRN_MCARU) << 32;
599
600 printk("Machine Check %s Address: %#llx\n",
601 reason & MCSR_MEA ? "Effective" : "Physical", addr);
602 }
603
Shaohui Xiecce1f102010-11-18 14:57:32 +0800604silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500605 mtspr(SPRN_MCSR, mcsr);
606 return mfspr(SPRN_MCSR) == 0 && recoverable;
607}
608
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100609int machine_check_e500(struct pt_regs *regs)
610{
611 unsigned long reason = get_mc_reason(regs);
612
Shaohui Xiecce1f102010-11-18 14:57:32 +0800613 if (reason & MCSR_BUS_RBERR) {
614 if (fsl_rio_mcheck_exception(regs))
615 return 1;
Hongtao Jia4e0e3432013-04-28 13:20:08 +0800616 if (fsl_pci_mcheck_exception(regs))
617 return 1;
Shaohui Xiecce1f102010-11-18 14:57:32 +0800618 }
619
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000620 printk("Machine check in kernel mode.\n");
621 printk("Caused by (from MCSR=%lx): ", reason);
622
623 if (reason & MCSR_MCP)
624 printk("Machine Check Signal\n");
625 if (reason & MCSR_ICPERR)
626 printk("Instruction Cache Parity Error\n");
627 if (reason & MCSR_DCP_PERR)
628 printk("Data Cache Push Parity Error\n");
629 if (reason & MCSR_DCPERR)
630 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000631 if (reason & MCSR_BUS_IAERR)
632 printk("Bus - Instruction Address Error\n");
633 if (reason & MCSR_BUS_RAERR)
634 printk("Bus - Read Address Error\n");
635 if (reason & MCSR_BUS_WAERR)
636 printk("Bus - Write Address Error\n");
637 if (reason & MCSR_BUS_IBERR)
638 printk("Bus - Instruction Data Error\n");
639 if (reason & MCSR_BUS_RBERR)
640 printk("Bus - Read Data Bus Error\n");
641 if (reason & MCSR_BUS_WBERR)
Wladislav Wiebec1528332014-06-17 15:30:53 +0200642 printk("Bus - Write Data Bus Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000643 if (reason & MCSR_BUS_IPERR)
644 printk("Bus - Instruction Parity Error\n");
645 if (reason & MCSR_BUS_RPERR)
646 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100647
648 return 0;
649}
Kumar Gala4490c062010-10-08 08:32:11 -0500650
651int machine_check_generic(struct pt_regs *regs)
652{
653 return 0;
654}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100655#elif defined(CONFIG_E200)
656int machine_check_e200(struct pt_regs *regs)
657{
658 unsigned long reason = get_mc_reason(regs);
659
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000660 printk("Machine check in kernel mode.\n");
661 printk("Caused by (from MCSR=%lx): ", reason);
662
663 if (reason & MCSR_MCP)
664 printk("Machine Check Signal\n");
665 if (reason & MCSR_CP_PERR)
666 printk("Cache Push Parity Error\n");
667 if (reason & MCSR_CPERR)
668 printk("Cache Parity Error\n");
669 if (reason & MCSR_EXCP_ERR)
670 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
671 if (reason & MCSR_BUS_IRERR)
672 printk("Bus - Read Bus Error on instruction fetch\n");
673 if (reason & MCSR_BUS_DRERR)
674 printk("Bus - Read Bus Error on data load\n");
675 if (reason & MCSR_BUS_WRERR)
676 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100677
678 return 0;
679}
Christophe Leroye627f8d2016-09-16 10:23:11 +0200680#elif defined(CONFIG_PPC_8xx)
681int machine_check_8xx(struct pt_regs *regs)
682{
683 unsigned long reason = get_mc_reason(regs);
684
685 pr_err("Machine check in kernel mode.\n");
686 pr_err("Caused by (from SRR1=%lx): ", reason);
687 if (reason & 0x40000000)
688 pr_err("Fetch error at address %lx\n", regs->nip);
689 else
690 pr_err("Data access error at address %lx\n", regs->dar);
691
692#ifdef CONFIG_PCI
693 /* the qspan pci read routines can cause machine checks -- Cort
694 *
695 * yuck !!! that totally needs to go away ! There are better ways
696 * to deal with that than having a wart in the mcheck handler.
697 * -- BenH
698 */
699 bad_page_fault(regs, regs->dar, SIGBUS);
700 return 1;
701#else
702 return 0;
703#endif
704}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100705#else
706int machine_check_generic(struct pt_regs *regs)
707{
708 unsigned long reason = get_mc_reason(regs);
709
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000710 printk("Machine check in kernel mode.\n");
711 printk("Caused by (from SRR1=%lx): ", reason);
712 switch (reason & 0x601F0000) {
713 case 0x80000:
714 printk("Machine check signal\n");
715 break;
716 case 0: /* for 601 */
717 case 0x40000:
718 case 0x140000: /* 7450 MSS error and TEA */
719 printk("Transfer error ack signal\n");
720 break;
721 case 0x20000:
722 printk("Data parity error signal\n");
723 break;
724 case 0x10000:
725 printk("Address parity error signal\n");
726 break;
727 case 0x20000000:
728 printk("L1 Data Cache error\n");
729 break;
730 case 0x40000000:
731 printk("L1 Instruction Cache error\n");
732 break;
733 case 0x00100000:
734 printk("L2 data cache parity error\n");
735 break;
736 default:
737 printk("Unknown values in msr\n");
738 }
Olof Johansson75918a42007-09-21 05:11:20 +1000739 return 0;
740}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100741#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000742
743void machine_check_exception(struct pt_regs *regs)
744{
Li Zhongba12eed2013-05-13 16:16:41 +0000745 enum ctx_state prev_state = exception_enter();
Olof Johansson75918a42007-09-21 05:11:20 +1000746 int recover = 0;
747
Christoph Lameter69111ba2014-10-21 15:23:25 -0500748 __this_cpu_inc(irq_stat.mce_exceptions);
Anton Blanchard89713ed2010-01-31 20:34:06 +0000749
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100750 /* See if any machine dependent calls. In theory, we would want
751 * to call the CPU first, and call the ppc_md. one if the CPU
752 * one returns a positive number. However there is existing code
753 * that assumes the board gets a first chance, so let's keep it
754 * that way for now and fix things later. --BenH.
755 */
Olof Johansson75918a42007-09-21 05:11:20 +1000756 if (ppc_md.machine_check_exception)
757 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100758 else if (cur_cpu_spec->machine_check)
759 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000760
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100761 if (recover > 0)
Li Zhongba12eed2013-05-13 16:16:41 +0000762 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000763
Anton Blancharda4435062011-01-11 19:45:31 +0000764 if (debugger_fault_handler(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000765 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000766
767 if (check_io_access(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000768 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000769
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000770 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000771
772 /* Must die if the interrupt is not recoverable */
773 if (!(regs->msr & MSR_RI))
774 panic("Unrecoverable Machine check");
Li Zhongba12eed2013-05-13 16:16:41 +0000775
776bail:
777 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000778}
779
780void SMIException(struct pt_regs *regs)
781{
782 die("System Management Interrupt", regs, SIGABRT);
783}
784
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530785void handle_hmi_exception(struct pt_regs *regs)
786{
787 struct pt_regs *old_regs;
788
789 old_regs = set_irq_regs(regs);
790 irq_enter();
791
792 if (ppc_md.handle_hmi_exception)
793 ppc_md.handle_hmi_exception(regs);
794
795 irq_exit();
796 set_irq_regs(old_regs);
797}
798
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000799void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000800{
Li Zhongba12eed2013-05-13 16:16:41 +0000801 enum ctx_state prev_state = exception_enter();
802
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
804 regs->nip, regs->msr, regs->trap);
805
806 _exception(SIGTRAP, regs, 0, 0);
Li Zhongba12eed2013-05-13 16:16:41 +0000807
808 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000809}
810
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000811void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812{
Li Zhongba12eed2013-05-13 16:16:41 +0000813 enum ctx_state prev_state = exception_enter();
814
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000815 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
816 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000817 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000818 if (debugger_iabr_match(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000819 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000820 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000821
822bail:
823 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000824}
825
826void RunModeException(struct pt_regs *regs)
827{
828 _exception(SIGTRAP, regs, 0, 0);
829}
830
Nicholas Piggin03465f82016-09-16 20:48:08 +1000831void single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000832{
Li Zhongba12eed2013-05-13 16:16:41 +0000833 enum ctx_state prev_state = exception_enter();
834
K.Prasad2538c2d2010-06-15 11:35:31 +0530835 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000836
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +0530837 if (kprobe_post_handler(regs))
838 return;
839
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000840 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
841 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000842 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000843 if (debugger_sstep(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000844 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000845
846 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000847
848bail:
849 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000850}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000851NOKPROBE_SYMBOL(single_step_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000852
853/*
854 * After we have successfully emulated an instruction, we have to
855 * check if the instruction was being single-stepped, and if so,
856 * pretend we got a single-step exception. This was pointed out
857 * by Kumar Gala. -- paulus
858 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000859static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000860{
K.Prasad2538c2d2010-06-15 11:35:31 +0530861 if (single_stepping(regs))
862 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000863}
864
Kumar Gala5fad2932007-02-07 01:47:59 -0600865static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000866{
Kumar Gala5fad2932007-02-07 01:47:59 -0600867 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000868
869 /* Invalid operation */
870 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600871 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000872
873 /* Overflow */
874 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600875 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000876
877 /* Underflow */
878 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600879 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000880
881 /* Divide by zero */
882 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600883 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000884
885 /* Inexact result */
886 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600887 ret = FPE_FLTRES;
888
889 return ret;
890}
891
892static void parse_fpe(struct pt_regs *regs)
893{
894 int code = 0;
895
896 flush_fp_to_thread(current);
897
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000898 code = __parse_fpscr(current->thread.fp_state.fpscr);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000899
900 _exception(SIGFPE, regs, code, regs->nip);
901}
902
903/*
904 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000905 * provide the PVR to user applications using the mfspr rd, PVR.
906 * Return non-zero if we can't emulate, or -EFAULT if the associated
907 * memory access caused an access fault. Return zero on success.
908 *
909 * There are a couple of ways to do this, either "decode" the instruction
910 * or directly match lots of bits. In this case, matching lots of
911 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000912 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000914static int emulate_string_inst(struct pt_regs *regs, u32 instword)
915{
916 u8 rT = (instword >> 21) & 0x1f;
917 u8 rA = (instword >> 16) & 0x1f;
918 u8 NB_RB = (instword >> 11) & 0x1f;
919 u32 num_bytes;
920 unsigned long EA;
921 int pos = 0;
922
923 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000924 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000925 if ((rT == rA) || (rT == NB_RB))
926 return -EINVAL;
927
928 EA = (rA == 0) ? 0 : regs->gpr[rA];
929
Kumar Gala16c57b32009-02-10 20:10:44 +0000930 switch (instword & PPC_INST_STRING_MASK) {
931 case PPC_INST_LSWX:
932 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000933 EA += NB_RB;
934 num_bytes = regs->xer & 0x7f;
935 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000936 case PPC_INST_LSWI:
937 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000938 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
939 break;
940 default:
941 return -EINVAL;
942 }
943
944 while (num_bytes != 0)
945 {
946 u8 val;
947 u32 shift = 8 * (3 - (pos & 0x3));
948
James Yang80aa0fb2013-06-25 11:41:05 -0500949 /* if process is 32-bit, clear upper 32 bits of EA */
950 if ((regs->msr & MSR_64BIT) == 0)
951 EA &= 0xFFFFFFFF;
952
Kumar Gala16c57b32009-02-10 20:10:44 +0000953 switch ((instword & PPC_INST_STRING_MASK)) {
954 case PPC_INST_LSWX:
955 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000956 if (get_user(val, (u8 __user *)EA))
957 return -EFAULT;
958 /* first time updating this reg,
959 * zero it out */
960 if (pos == 0)
961 regs->gpr[rT] = 0;
962 regs->gpr[rT] |= val << shift;
963 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000964 case PPC_INST_STSWI:
965 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000966 val = regs->gpr[rT] >> shift;
967 if (put_user(val, (u8 __user *)EA))
968 return -EFAULT;
969 break;
970 }
971 /* move EA to next address */
972 EA += 1;
973 num_bytes--;
974
975 /* manage our position within the register */
976 if (++pos == 4) {
977 pos = 0;
978 if (++rT == 32)
979 rT = 0;
980 }
981 }
982
983 return 0;
984}
985
Will Schmidtc3412dc2006-08-30 13:11:38 -0500986static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
987{
988 u32 ra,rs;
989 unsigned long tmp;
990
991 ra = (instword >> 16) & 0x1f;
992 rs = (instword >> 21) & 0x1f;
993
994 tmp = regs->gpr[rs];
995 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
996 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
997 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
998 regs->gpr[ra] = tmp;
999
1000 return 0;
1001}
1002
Kumar Galac1469f12007-11-19 21:35:29 -06001003static int emulate_isel(struct pt_regs *regs, u32 instword)
1004{
1005 u8 rT = (instword >> 21) & 0x1f;
1006 u8 rA = (instword >> 16) & 0x1f;
1007 u8 rB = (instword >> 11) & 0x1f;
1008 u8 BC = (instword >> 6) & 0x1f;
1009 u8 bit;
1010 unsigned long tmp;
1011
1012 tmp = (rA == 0) ? 0 : regs->gpr[rA];
1013 bit = (regs->ccr >> (31 - BC)) & 0x1;
1014
1015 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1016
1017 return 0;
1018}
1019
Michael Neuling6ce6c622013-05-26 18:09:39 +00001020#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1021static inline bool tm_abort_check(struct pt_regs *regs, int cause)
1022{
1023 /* If we're emulating a load/store in an active transaction, we cannot
1024 * emulate it as the kernel operates in transaction suspended context.
1025 * We need to abort the transaction. This creates a persistent TM
1026 * abort so tell the user what caused it with a new code.
1027 */
1028 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
1029 tm_enable();
1030 tm_abort(cause);
1031 return true;
1032 }
1033 return false;
1034}
1035#else
1036static inline bool tm_abort_check(struct pt_regs *regs, int reason)
1037{
1038 return false;
1039}
1040#endif
1041
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001042static int emulate_instruction(struct pt_regs *regs)
1043{
1044 u32 instword;
1045 u32 rd;
1046
Anton Blanchard4288e342013-08-07 02:01:47 +10001047 if (!user_mode(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001048 return -EINVAL;
1049 CHECK_FULL_REGS(regs);
1050
1051 if (get_user(instword, (u32 __user *)(regs->nip)))
1052 return -EFAULT;
1053
1054 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001055 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001056 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001057 rd = (instword >> 21) & 0x1f;
1058 regs->gpr[rd] = mfspr(SPRN_PVR);
1059 return 0;
1060 }
1061
1062 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001063 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001064 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001065 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001066 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001067
1068 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001069 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +10001070 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001071 unsigned long msk = 0xf0000000UL >> shift;
1072
Anton Blanchardeecff812009-10-27 18:46:55 +00001073 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001074 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
1075 regs->xer &= ~0xf0000000UL;
1076 return 0;
1077 }
1078
1079 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001080 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Michael Neuling6ce6c622013-05-26 18:09:39 +00001081 if (tm_abort_check(regs,
1082 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
1083 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +00001084 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001085 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001086 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001087
Will Schmidtc3412dc2006-08-30 13:11:38 -05001088 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001089 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001090 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -05001091 return emulate_popcntb_inst(regs, instword);
1092 }
1093
Kumar Galac1469f12007-11-19 21:35:29 -06001094 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +00001095 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001096 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -06001097 return emulate_isel(regs, instword);
1098 }
1099
James Yang9863c282013-07-03 16:26:47 -05001100 /* Emulate sync instruction variants */
1101 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1102 PPC_WARN_EMULATED(sync, regs);
1103 asm volatile("sync");
1104 return 0;
1105 }
1106
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001107#ifdef CONFIG_PPC64
1108 /* Emulate the mfspr rD, DSCR. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001109 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1110 PPC_INST_MFSPR_DSCR_USER) ||
1111 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1112 PPC_INST_MFSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001113 cpu_has_feature(CPU_FTR_DSCR)) {
1114 PPC_WARN_EMULATED(mfdscr, regs);
1115 rd = (instword >> 21) & 0x1f;
1116 regs->gpr[rd] = mfspr(SPRN_DSCR);
1117 return 0;
1118 }
1119 /* Emulate the mtspr DSCR, rD. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001120 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1121 PPC_INST_MTSPR_DSCR_USER) ||
1122 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1123 PPC_INST_MTSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001124 cpu_has_feature(CPU_FTR_DSCR)) {
1125 PPC_WARN_EMULATED(mtdscr, regs);
1126 rd = (instword >> 21) & 0x1f;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001127 current->thread.dscr = regs->gpr[rd];
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001128 current->thread.dscr_inherit = 1;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001129 mtspr(SPRN_DSCR, current->thread.dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001130 return 0;
1131 }
1132#endif
1133
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001134 return -EINVAL;
1135}
1136
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001137int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001138{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001139 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001140}
1141
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001142#ifdef CONFIG_MATH_EMULATION
1143static int emulate_math(struct pt_regs *regs)
1144{
1145 int ret;
1146 extern int do_mathemu(struct pt_regs *regs);
1147
1148 ret = do_mathemu(regs);
1149 if (ret >= 0)
1150 PPC_WARN_EMULATED(math, regs);
1151
1152 switch (ret) {
1153 case 0:
1154 emulate_single_step(regs);
1155 return 0;
1156 case 1: {
1157 int code = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001158 code = __parse_fpscr(current->thread.fp_state.fpscr);
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001159 _exception(SIGFPE, regs, code, regs->nip);
1160 return 0;
1161 }
1162 case -EFAULT:
1163 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1164 return 0;
1165 }
1166
1167 return -1;
1168}
1169#else
1170static inline int emulate_math(struct pt_regs *regs) { return -1; }
1171#endif
1172
Nicholas Piggin03465f82016-09-16 20:48:08 +10001173void program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001174{
Li Zhongba12eed2013-05-13 16:16:41 +00001175 enum ctx_state prev_state = exception_enter();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001176 unsigned int reason = get_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001177
Kim Phillipsaa42c692006-12-08 02:43:30 -06001178 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -06001179 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001180
1181 if (reason & REASON_FP) {
1182 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001183 parse_fpe(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001184 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001185 }
1186 if (reason & REASON_TRAP) {
Balbir Singha4c3f902016-02-18 13:48:01 +11001187 unsigned long bugaddr;
Jason Wesselba797b22010-05-20 21:04:25 -05001188 /* Debugger is first in line to stop recursive faults in
1189 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1190 if (debugger_bpt(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001191 goto bail;
Jason Wesselba797b22010-05-20 21:04:25 -05001192
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301193 if (kprobe_handler(regs))
1194 goto bail;
1195
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001196 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001197 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1198 == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +00001199 goto bail;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001200
Balbir Singha4c3f902016-02-18 13:48:01 +11001201 bugaddr = regs->nip;
1202 /*
1203 * Fixup bugaddr for BUG_ON() in real mode
1204 */
1205 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1206 bugaddr += PAGE_OFFSET;
1207
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001208 if (!(regs->msr & MSR_PR) && /* not user-mode */
Balbir Singha4c3f902016-02-18 13:48:01 +11001209 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001210 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001211 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001212 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001213 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001214 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001215 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001216#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1217 if (reason & REASON_TM) {
1218 /* This is a TM "Bad Thing Exception" program check.
1219 * This occurs when:
1220 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1221 * transition in TM states.
1222 * - A trechkpt is attempted when transactional.
1223 * - A treclaim is attempted when non transactional.
1224 * - A tend is illegally attempted.
1225 * - writing a TM SPR when transactional.
1226 */
1227 if (!user_mode(regs) &&
1228 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1229 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001230 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001231 }
1232 /* If usermode caused this, it's done something illegal and
1233 * gets a SIGILL slap on the wrist. We call it an illegal
1234 * operand to distinguish from the instruction just being bad
1235 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1236 * illegal /placement/ of a valid instruction.
1237 */
1238 if (user_mode(regs)) {
1239 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001240 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001241 } else {
1242 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1243 "at %lx (msr 0x%x)\n", regs->nip, reason);
1244 die("Unrecoverable exception", regs, SIGABRT);
1245 }
1246 }
1247#endif
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001248
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001249 /*
1250 * If we took the program check in the kernel skip down to sending a
1251 * SIGILL. The subsequent cases all relate to emulating instructions
1252 * which we should only do for userspace. We also do not want to enable
1253 * interrupts for kernel faults because that might lead to further
1254 * faults, and loose the context of the original exception.
1255 */
1256 if (!user_mode(regs))
1257 goto sigill;
1258
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001259 /* We restore the interrupt state now */
1260 if (!arch_irq_disabled_regs(regs))
1261 local_irq_enable();
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001262
Kumar Gala04903a32007-02-07 01:13:32 -06001263 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1264 * but there seems to be a hardware bug on the 405GP (RevD)
1265 * that means ESR is sometimes set incorrectly - either to
1266 * ESR_DST (!?) or 0. In the process of chasing this with the
1267 * hardware people - not sure if it can happen on any illegal
1268 * instruction or only on FP instructions, whether there is a
Benjamin Herrenschmidt4e63f8e2013-06-09 17:01:24 +10001269 * pattern to occurrences etc. -dgibson 31/Mar/2003
1270 */
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001271 if (!emulate_math(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001272 goto bail;
Kumar Gala04903a32007-02-07 01:13:32 -06001273
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001274 /* Try to emulate it if we should. */
1275 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001276 switch (emulate_instruction(regs)) {
1277 case 0:
1278 regs->nip += 4;
1279 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001280 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001281 case -EFAULT:
1282 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001283 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001284 }
1285 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001286
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001287sigill:
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001288 if (reason & REASON_PRIVILEGED)
1289 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1290 else
1291 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001292
1293bail:
1294 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001295}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001296NOKPROBE_SYMBOL(program_check_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001297
Paul Mackerrasbf593902013-06-14 20:07:41 +10001298/*
1299 * This occurs when running in hypervisor mode on POWER6 or later
1300 * and an illegal instruction is encountered.
1301 */
Nicholas Piggin03465f82016-09-16 20:48:08 +10001302void emulation_assist_interrupt(struct pt_regs *regs)
Paul Mackerrasbf593902013-06-14 20:07:41 +10001303{
1304 regs->msr |= REASON_ILLEGAL;
1305 program_check_exception(regs);
1306}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001307NOKPROBE_SYMBOL(emulation_assist_interrupt);
Paul Mackerrasbf593902013-06-14 20:07:41 +10001308
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001309void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001310{
Li Zhongba12eed2013-05-13 16:16:41 +00001311 enum ctx_state prev_state = exception_enter();
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001312 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001313
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001314 /* We restore the interrupt state now */
1315 if (!arch_irq_disabled_regs(regs))
1316 local_irq_enable();
1317
Michael Neuling6ce6c622013-05-26 18:09:39 +00001318 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1319 goto bail;
1320
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001321 /* we don't implement logging of alignment exceptions */
1322 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1323 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001324
1325 if (fixed == 1) {
1326 regs->nip += 4; /* skip over emulated instruction */
1327 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001328 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001329 }
1330
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001331 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001332 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001333 sig = SIGSEGV;
1334 code = SEGV_ACCERR;
1335 } else {
1336 sig = SIGBUS;
1337 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001338 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001339 if (user_mode(regs))
1340 _exception(sig, regs, code, regs->dar);
1341 else
1342 bad_page_fault(regs, regs->dar, sig);
Li Zhongba12eed2013-05-13 16:16:41 +00001343
1344bail:
1345 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001346}
1347
Paul Mackerrasf0f558b2016-09-02 21:49:21 +10001348void slb_miss_bad_addr(struct pt_regs *regs)
1349{
1350 enum ctx_state prev_state = exception_enter();
1351
1352 if (user_mode(regs))
1353 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1354 else
1355 bad_page_fault(regs, regs->dar, SIGSEGV);
1356
1357 exception_exit(prev_state);
1358}
1359
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001360void StackOverflow(struct pt_regs *regs)
1361{
1362 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1363 current, regs->gpr[1]);
1364 debugger(regs);
1365 show_regs(regs);
1366 panic("kernel stack overflow");
1367}
1368
1369void nonrecoverable_exception(struct pt_regs *regs)
1370{
1371 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1372 regs->nip, regs->msr);
1373 debugger(regs);
1374 die("nonrecoverable exception", regs, SIGKILL);
1375}
1376
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001377void kernel_fp_unavailable_exception(struct pt_regs *regs)
1378{
Li Zhongba12eed2013-05-13 16:16:41 +00001379 enum ctx_state prev_state = exception_enter();
1380
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001381 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1382 "%lx at %lx\n", regs->trap, regs->nip);
1383 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001384
1385 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001386}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001387
1388void altivec_unavailable_exception(struct pt_regs *regs)
1389{
Li Zhongba12eed2013-05-13 16:16:41 +00001390 enum ctx_state prev_state = exception_enter();
1391
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001392 if (user_mode(regs)) {
1393 /* A user program has executed an altivec instruction,
1394 but this kernel doesn't support altivec. */
1395 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001396 goto bail;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001397 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001398
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001399 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1400 "%lx at %lx\n", regs->trap, regs->nip);
1401 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001402
1403bail:
1404 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001405}
1406
Michael Neulingce48b212008-06-25 14:07:18 +10001407void vsx_unavailable_exception(struct pt_regs *regs)
1408{
1409 if (user_mode(regs)) {
1410 /* A user program has executed an vsx instruction,
1411 but this kernel doesn't support vsx. */
1412 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1413 return;
1414 }
1415
1416 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1417 "%lx at %lx\n", regs->trap, regs->nip);
1418 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1419}
1420
Michael Neuling25176172013-08-09 17:29:29 +10001421#ifdef CONFIG_PPC64
Cyril Bur172f7aa2016-09-14 18:02:15 +10001422static void tm_unavailable(struct pt_regs *regs)
1423{
Cyril Bur5d176f72016-09-14 18:02:16 +10001424#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1425 if (user_mode(regs)) {
1426 current->thread.load_tm++;
1427 regs->msr |= MSR_TM;
1428 tm_enable();
1429 tm_restore_sprs(&current->thread);
1430 return;
1431 }
1432#endif
Cyril Bur172f7aa2016-09-14 18:02:15 +10001433 pr_emerg("Unrecoverable TM Unavailable Exception "
1434 "%lx at %lx\n", regs->trap, regs->nip);
1435 die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1436}
1437
Michael Ellerman021424a2013-06-25 17:47:56 +10001438void facility_unavailable_exception(struct pt_regs *regs)
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001439{
Michael Ellerman021424a2013-06-25 17:47:56 +10001440 static char *facility_strings[] = {
Michael Neuling25176172013-08-09 17:29:29 +10001441 [FSCR_FP_LG] = "FPU",
1442 [FSCR_VECVSX_LG] = "VMX/VSX",
1443 [FSCR_DSCR_LG] = "DSCR",
1444 [FSCR_PM_LG] = "PMU SPRs",
1445 [FSCR_BHRB_LG] = "BHRB",
1446 [FSCR_TM_LG] = "TM",
1447 [FSCR_EBB_LG] = "EBB",
1448 [FSCR_TAR_LG] = "TAR",
Nicholas Piggin794464f2017-04-07 11:27:43 +10001449 [FSCR_MSGP_LG] = "MSGP",
Nicholas Piggin9b7ff0c2017-04-07 11:27:44 +10001450 [FSCR_SCV_LG] = "SCV",
Michael Ellerman021424a2013-06-25 17:47:56 +10001451 };
Michael Neuling25176172013-08-09 17:29:29 +10001452 char *facility = "unknown";
Michael Ellerman021424a2013-06-25 17:47:56 +10001453 u64 value;
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301454 u32 instword, rd;
Michael Neuling25176172013-08-09 17:29:29 +10001455 u8 status;
1456 bool hv;
Michael Ellerman021424a2013-06-25 17:47:56 +10001457
Michael Neuling25176172013-08-09 17:29:29 +10001458 hv = (regs->trap == 0xf80);
1459 if (hv)
Michael Ellermanb14b6262013-06-25 17:47:57 +10001460 value = mfspr(SPRN_HFSCR);
Michael Neuling25176172013-08-09 17:29:29 +10001461 else
1462 value = mfspr(SPRN_FSCR);
1463
1464 status = value >> 56;
1465 if (status == FSCR_DSCR_LG) {
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301466 /*
1467 * User is accessing the DSCR register using the problem
1468 * state only SPR number (0x03) either through a mfspr or
1469 * a mtspr instruction. If it is a write attempt through
1470 * a mtspr, then we set the inherit bit. This also allows
1471 * the user to write or read the register directly in the
1472 * future by setting via the FSCR DSCR bit. But in case it
1473 * is a read DSCR attempt through a mfspr instruction, we
1474 * just emulate the instruction instead. This code path will
1475 * always emulate all the mfspr instructions till the user
Adam Buchbinder446957b2016-02-24 10:51:11 -08001476 * has attempted at least one mtspr instruction. This way it
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301477 * preserves the same behaviour when the user is accessing
1478 * the DSCR through privilege level only SPR number (0x11)
1479 * which is emulated through illegal instruction exception.
1480 * We always leave HFSCR DSCR set.
Michael Neuling25176172013-08-09 17:29:29 +10001481 */
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301482 if (get_user(instword, (u32 __user *)(regs->nip))) {
1483 pr_err("Failed to fetch the user instruction\n");
1484 return;
1485 }
1486
1487 /* Write into DSCR (mtspr 0x03, RS) */
1488 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1489 == PPC_INST_MTSPR_DSCR_USER) {
1490 rd = (instword >> 21) & 0x1f;
1491 current->thread.dscr = regs->gpr[rd];
1492 current->thread.dscr_inherit = 1;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001493 current->thread.fscr |= FSCR_DSCR;
1494 mtspr(SPRN_FSCR, current->thread.fscr);
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301495 }
1496
1497 /* Read from DSCR (mfspr RT, 0x03) */
1498 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1499 == PPC_INST_MFSPR_DSCR_USER) {
1500 if (emulate_instruction(regs)) {
1501 pr_err("DSCR based mfspr emulation failed\n");
1502 return;
1503 }
1504 regs->nip += 4;
1505 emulate_single_step(regs);
1506 }
Michael Neuling25176172013-08-09 17:29:29 +10001507 return;
Michael Ellermanb14b6262013-06-25 17:47:57 +10001508 }
1509
Cyril Bur172f7aa2016-09-14 18:02:15 +10001510 if (status == FSCR_TM_LG) {
1511 /*
1512 * If we're here then the hardware is TM aware because it
1513 * generated an exception with FSRM_TM set.
1514 *
1515 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1516 * told us not to do TM, or the kernel is not built with TM
1517 * support.
1518 *
1519 * If both of those things are true, then userspace can spam the
1520 * console by triggering the printk() below just by continually
1521 * doing tbegin (or any TM instruction). So in that case just
1522 * send the process a SIGILL immediately.
1523 */
1524 if (!cpu_has_feature(CPU_FTR_TM))
1525 goto out;
1526
1527 tm_unavailable(regs);
1528 return;
1529 }
1530
Balbir Singh93c2ec02016-11-30 17:45:09 +11001531 if ((hv || status >= 2) &&
1532 (status < ARRAY_SIZE(facility_strings)) &&
Michael Neuling25176172013-08-09 17:29:29 +10001533 facility_strings[status])
1534 facility = facility_strings[status];
Michael Ellerman021424a2013-06-25 17:47:56 +10001535
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001536 /* We restore the interrupt state now */
1537 if (!arch_irq_disabled_regs(regs))
1538 local_irq_enable();
1539
Balbir Singh93c2ec02016-11-30 17:45:09 +11001540 pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
1541 hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001542
Cyril Bur172f7aa2016-09-14 18:02:15 +10001543out:
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001544 if (user_mode(regs)) {
1545 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1546 return;
1547 }
1548
Michael Ellerman021424a2013-06-25 17:47:56 +10001549 die("Unexpected facility unavailable exception", regs, SIGABRT);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001550}
Michael Neuling25176172013-08-09 17:29:29 +10001551#endif
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001552
Michael Neulingf54db642013-02-13 16:21:39 +00001553#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1554
Michael Neulingf54db642013-02-13 16:21:39 +00001555void fp_unavailable_tm(struct pt_regs *regs)
1556{
1557 /* Note: This does not handle any kind of FP laziness. */
1558
1559 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1560 regs->nip, regs->msr);
Michael Neulingf54db642013-02-13 16:21:39 +00001561
1562 /* We can only have got here if the task started using FP after
1563 * beginning the transaction. So, the transactional regs are just a
1564 * copy of the checkpointed ones. But, we still need to recheckpoint
1565 * as we're enabling FP for the process; it will return, abort the
1566 * transaction, and probably retry but now with FP enabled. So the
1567 * checkpointed FP registers need to be loaded.
1568 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001569 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001570 /* Reclaim didn't save out any FPRs to transact_fprs. */
1571
1572 /* Enable FP for the task: */
1573 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1574
1575 /* This loads and recheckpoints the FP registers from
1576 * thread.fpr[]. They will remain in registers after the
1577 * checkpoint so we don't need to reload them after.
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001578 * If VMX is in use, the VRs now hold checkpointed values,
1579 * so we don't want to load the VRs from the thread_struct.
Michael Neulingf54db642013-02-13 16:21:39 +00001580 */
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001581 tm_recheckpoint(&current->thread, MSR_FP);
1582
1583 /* If VMX is in use, get the transactional values back */
1584 if (regs->msr & MSR_VEC) {
Cyril Burdc310662016-09-23 16:18:24 +10001585 msr_check_and_set(MSR_VEC);
1586 load_vr_state(&current->thread.vr_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001587 /* At this point all the VSX state is loaded, so enable it */
1588 regs->msr |= MSR_VSX;
1589 }
Michael Neulingf54db642013-02-13 16:21:39 +00001590}
1591
Michael Neulingf54db642013-02-13 16:21:39 +00001592void altivec_unavailable_tm(struct pt_regs *regs)
1593{
1594 /* See the comments in fp_unavailable_tm(). This function operates
1595 * the same way.
1596 */
1597
1598 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1599 "MSR=%lx\n",
1600 regs->nip, regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001601 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001602 regs->msr |= MSR_VEC;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001603 tm_recheckpoint(&current->thread, MSR_VEC);
Michael Neulingf54db642013-02-13 16:21:39 +00001604 current->thread.used_vr = 1;
Michael Neulingf54db642013-02-13 16:21:39 +00001605
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001606 if (regs->msr & MSR_FP) {
Cyril Burdc310662016-09-23 16:18:24 +10001607 msr_check_and_set(MSR_FP);
1608 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001609 regs->msr |= MSR_VSX;
1610 }
1611}
1612
Michael Neulingf54db642013-02-13 16:21:39 +00001613void vsx_unavailable_tm(struct pt_regs *regs)
1614{
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001615 unsigned long orig_msr = regs->msr;
1616
Michael Neulingf54db642013-02-13 16:21:39 +00001617 /* See the comments in fp_unavailable_tm(). This works similarly,
1618 * though we're loading both FP and VEC registers in here.
1619 *
1620 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1621 * regs. Either way, set MSR_VSX.
1622 */
1623
1624 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1625 "MSR=%lx\n",
1626 regs->nip, regs->msr);
1627
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001628 current->thread.used_vsr = 1;
1629
1630 /* If FP and VMX are already loaded, we have all the state we need */
1631 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1632 regs->msr |= MSR_VSX;
1633 return;
1634 }
1635
Michael Neulingf54db642013-02-13 16:21:39 +00001636 /* This reclaims FP and/or VR regs if they're already enabled */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001637 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001638
1639 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1640 MSR_VSX;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001641
1642 /* This loads & recheckpoints FP and VRs; but we have
1643 * to be sure not to overwrite previously-valid state.
1644 */
1645 tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1646
Cyril Burdc310662016-09-23 16:18:24 +10001647 msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1648
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001649 if (orig_msr & MSR_FP)
Cyril Burdc310662016-09-23 16:18:24 +10001650 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001651 if (orig_msr & MSR_VEC)
Cyril Burdc310662016-09-23 16:18:24 +10001652 load_vr_state(&current->thread.vr_state);
Michael Neulingf54db642013-02-13 16:21:39 +00001653}
Michael Neulingf54db642013-02-13 16:21:39 +00001654#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1655
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001656void performance_monitor_exception(struct pt_regs *regs)
1657{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001658 __this_cpu_inc(irq_stat.pmu_irqs);
Anton Blanchard89713ed2010-01-31 20:34:06 +00001659
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001660 perf_irq(regs);
1661}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001662
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001663#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001664void SoftwareEmulation(struct pt_regs *regs)
1665{
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001666 CHECK_FULL_REGS(regs);
1667
1668 if (!user_mode(regs)) {
1669 debugger(regs);
LEROY Christophe1eb28192013-08-28 16:19:17 +02001670 die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
1671 regs, SIGFPE);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001672 }
1673
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001674 if (!emulate_math(regs))
1675 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001676
Scott Wood5dd57a12007-09-18 15:29:35 -05001677 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001678}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001679#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001680
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001681#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001682static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1683{
1684 int changed = 0;
1685 /*
1686 * Determine the cause of the debug event, clear the
1687 * event flags and send a trap to the handler. Torez
1688 */
1689 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1690 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1691#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301692 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001693#endif
1694 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1695 5);
1696 changed |= 0x01;
1697 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1698 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1699 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1700 6);
1701 changed |= 0x01;
1702 } else if (debug_status & DBSR_IAC1) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301703 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001704 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1705 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1706 1);
1707 changed |= 0x01;
1708 } else if (debug_status & DBSR_IAC2) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301709 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001710 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1711 2);
1712 changed |= 0x01;
1713 } else if (debug_status & DBSR_IAC3) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301714 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001715 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1716 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1717 3);
1718 changed |= 0x01;
1719 } else if (debug_status & DBSR_IAC4) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301720 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001721 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1722 4);
1723 changed |= 0x01;
1724 }
1725 /*
1726 * At the point this routine was called, the MSR(DE) was turned off.
1727 * Check all other debug flags and see if that bit needs to be turned
1728 * back on or not.
1729 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301730 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
Bharat Bhushan95791982013-06-26 11:12:22 +05301731 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001732 regs->msr |= MSR_DE;
1733 else
1734 /* Make sure the IDM flag is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301735 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001736
1737 if (changed & 0x01)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301738 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001739}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001740
Nicholas Piggin03465f82016-09-16 20:48:08 +10001741void DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001742{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301743 current->thread.debug.dbsr = debug_status;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001744
Roland McGrathec097c82009-05-28 21:26:38 +00001745 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1746 * on server, it stops on the target of the branch. In order to simulate
1747 * the server behaviour, we thus restart right away with a single step
1748 * instead of stopping here when hitting a BT
1749 */
1750 if (debug_status & DBSR_BT) {
1751 regs->msr &= ~MSR_DE;
1752
1753 /* Disable BT */
1754 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1755 /* Clear the BT event */
1756 mtspr(SPRN_DBSR, DBSR_BT);
1757
1758 /* Do the single step trick only when coming from userspace */
1759 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301760 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1761 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Roland McGrathec097c82009-05-28 21:26:38 +00001762 regs->msr |= MSR_DE;
1763 return;
1764 }
1765
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301766 if (kprobe_post_handler(regs))
1767 return;
1768
Roland McGrathec097c82009-05-28 21:26:38 +00001769 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1770 5, SIGTRAP) == NOTIFY_STOP) {
1771 return;
1772 }
1773 if (debugger_sstep(regs))
1774 return;
1775 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001777
1778 /* Disable instruction completion */
1779 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1780 /* Clear the instruction completion event */
1781 mtspr(SPRN_DBSR, DBSR_IC);
1782
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301783 if (kprobe_post_handler(regs))
1784 return;
1785
Kumar Galaf8279622008-06-26 02:01:37 -05001786 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1787 5, SIGTRAP) == NOTIFY_STOP) {
1788 return;
1789 }
1790
1791 if (debugger_sstep(regs))
1792 return;
1793
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001794 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301795 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1796 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1797 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001798 regs->msr |= MSR_DE;
1799 else
1800 /* Make sure the IDM bit is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301801 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001802 }
Kumar Galaf8279622008-06-26 02:01:37 -05001803
1804 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001805 } else
1806 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001807}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001808NOKPROBE_SYMBOL(DebugException);
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001809#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001810
1811#if !defined(CONFIG_TAU_INT)
1812void TAUException(struct pt_regs *regs)
1813{
1814 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1815 regs->nip, regs->msr, regs->trap, print_tainted());
1816}
1817#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001818
1819#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001820void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001821{
1822 int err;
1823
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001824 if (!user_mode(regs)) {
1825 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1826 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001827 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001828 }
1829
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001830 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001831
Anton Blanchardeecff812009-10-27 18:46:55 +00001832 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001833 err = emulate_altivec(regs);
1834 if (err == 0) {
1835 regs->nip += 4; /* skip emulated instruction */
1836 emulate_single_step(regs);
1837 return;
1838 }
1839
1840 if (err == -EFAULT) {
1841 /* got an error reading the instruction */
1842 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1843 } else {
1844 /* didn't recognize the instruction */
1845 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001846 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1847 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001848 current->thread.vr_state.vscr.u[3] |= 0x10000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001849 }
1850}
1851#endif /* CONFIG_ALTIVEC */
1852
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001853#ifdef CONFIG_FSL_BOOKE
1854void CacheLockingException(struct pt_regs *regs, unsigned long address,
1855 unsigned long error_code)
1856{
1857 /* We treat cache locking instructions from the user
1858 * as priv ops, in the future we could try to do
1859 * something smarter
1860 */
1861 if (error_code & (ESR_DLK|ESR_ILK))
1862 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1863 return;
1864}
1865#endif /* CONFIG_FSL_BOOKE */
1866
1867#ifdef CONFIG_SPE
1868void SPEFloatingPointException(struct pt_regs *regs)
1869{
Liu Yu6a800f32008-10-28 11:50:21 +08001870 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001871 unsigned long spefscr;
1872 int fpexc_mode;
1873 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001874 int err;
1875
yu liu685659e2011-06-14 18:34:25 -05001876 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001877
1878 spefscr = current->thread.spefscr;
1879 fpexc_mode = current->thread.fpexc_mode;
1880
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001881 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1882 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001883 }
1884 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1885 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001886 }
1887 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1888 code = FPE_FLTDIV;
1889 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1890 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001891 }
1892 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1893 code = FPE_FLTRES;
1894
Liu Yu6a800f32008-10-28 11:50:21 +08001895 err = do_spe_mathemu(regs);
1896 if (err == 0) {
1897 regs->nip += 4; /* skip emulated instruction */
1898 emulate_single_step(regs);
1899 return;
1900 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001901
Liu Yu6a800f32008-10-28 11:50:21 +08001902 if (err == -EFAULT) {
1903 /* got an error reading the instruction */
1904 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1905 } else if (err == -EINVAL) {
1906 /* didn't recognize the instruction */
1907 printk(KERN_ERR "unrecognized spe instruction "
1908 "in %s at %lx\n", current->comm, regs->nip);
1909 } else {
1910 _exception(SIGFPE, regs, code, regs->nip);
1911 }
1912
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001913 return;
1914}
Liu Yu6a800f32008-10-28 11:50:21 +08001915
1916void SPEFloatingPointRoundException(struct pt_regs *regs)
1917{
1918 extern int speround_handler(struct pt_regs *regs);
1919 int err;
1920
1921 preempt_disable();
1922 if (regs->msr & MSR_SPE)
1923 giveup_spe(current);
1924 preempt_enable();
1925
1926 regs->nip -= 4;
1927 err = speround_handler(regs);
1928 if (err == 0) {
1929 regs->nip += 4; /* skip emulated instruction */
1930 emulate_single_step(regs);
1931 return;
1932 }
1933
1934 if (err == -EFAULT) {
1935 /* got an error reading the instruction */
1936 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1937 } else if (err == -EINVAL) {
1938 /* didn't recognize the instruction */
1939 printk(KERN_ERR "unrecognized spe instruction "
1940 "in %s at %lx\n", current->comm, regs->nip);
1941 } else {
1942 _exception(SIGFPE, regs, 0, regs->nip);
1943 return;
1944 }
1945}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001946#endif
1947
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001948/*
1949 * We enter here if we get an unrecoverable exception, that is, one
1950 * that happened at a point where the RI (recoverable interrupt) bit
1951 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1952 * we therefore lost state by taking this exception.
1953 */
1954void unrecoverable_exception(struct pt_regs *regs)
1955{
1956 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1957 regs->trap, regs->nip);
1958 die("Unrecoverable exception", regs, SIGABRT);
1959}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001960
Jason Gunthorpe1e18c172012-10-05 08:07:15 +00001961#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001962/*
1963 * Default handler for a Watchdog exception,
1964 * spins until a reboot occurs
1965 */
1966void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1967{
1968 /* Generic WatchdogHandler, implement your own */
1969 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1970 return;
1971}
1972
1973void WatchdogException(struct pt_regs *regs)
1974{
1975 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1976 WatchdogHandler(regs);
1977}
1978#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001979
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001980/*
1981 * We enter here if we discover during exception entry that we are
1982 * running in supervisor mode with a userspace value in the stack pointer.
1983 */
1984void kernel_bad_stack(struct pt_regs *regs)
1985{
1986 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1987 regs->gpr[1], regs->nip);
1988 die("Bad kernel stack pointer", regs, SIGABRT);
1989}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001990
1991void __init trap_init(void)
1992{
1993}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001994
1995
1996#ifdef CONFIG_PPC_EMULATED_STATS
1997
1998#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1999
2000struct ppc_emulated ppc_emulated = {
2001#ifdef CONFIG_ALTIVEC
2002 WARN_EMULATED_SETUP(altivec),
2003#endif
2004 WARN_EMULATED_SETUP(dcba),
2005 WARN_EMULATED_SETUP(dcbz),
2006 WARN_EMULATED_SETUP(fp_pair),
2007 WARN_EMULATED_SETUP(isel),
2008 WARN_EMULATED_SETUP(mcrxr),
2009 WARN_EMULATED_SETUP(mfpvr),
2010 WARN_EMULATED_SETUP(multiple),
2011 WARN_EMULATED_SETUP(popcntb),
2012 WARN_EMULATED_SETUP(spe),
2013 WARN_EMULATED_SETUP(string),
Scott Wooda3821b22013-10-28 22:07:59 -05002014 WARN_EMULATED_SETUP(sync),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002015 WARN_EMULATED_SETUP(unaligned),
2016#ifdef CONFIG_MATH_EMULATION
2017 WARN_EMULATED_SETUP(math),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002018#endif
2019#ifdef CONFIG_VSX
2020 WARN_EMULATED_SETUP(vsx),
2021#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00002022#ifdef CONFIG_PPC64
2023 WARN_EMULATED_SETUP(mfdscr),
2024 WARN_EMULATED_SETUP(mtdscr),
Anton Blanchardf83319d2014-03-28 17:01:23 +11002025 WARN_EMULATED_SETUP(lq_stq),
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00002026#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002027};
2028
2029u32 ppc_warn_emulated;
2030
2031void ppc_warn_emulated_print(const char *type)
2032{
Christian Dietrich76462232011-06-04 05:36:54 +00002033 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
2034 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002035}
2036
2037static int __init ppc_warn_emulated_init(void)
2038{
2039 struct dentry *dir, *d;
2040 unsigned int i;
2041 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
2042
2043 if (!powerpc_debugfs_root)
2044 return -ENODEV;
2045
2046 dir = debugfs_create_dir("emulated_instructions",
2047 powerpc_debugfs_root);
2048 if (!dir)
2049 return -ENOMEM;
2050
2051 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
2052 &ppc_warn_emulated);
2053 if (!d)
2054 goto fail;
2055
2056 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
2057 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
2058 (u32 *)&entries[i].val.counter);
2059 if (!d)
2060 goto fail;
2061 }
2062
2063 return 0;
2064
2065fail:
2066 debugfs_remove_recursive(dir);
2067 return -ENOMEM;
2068}
2069
2070device_initcall(ppc_warn_emulated_init);
2071
2072#endif /* CONFIG_PPC_EMULATED_STATS */