blob: 6858d910b47e8c9199067da44a7e8719d0ef8730 [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
Nicholas Piggin6fcd6ba2017-07-19 16:59:11 +1000117/*
118 * If oops/die is expected to crash the machine, return true here.
119 *
120 * This should not be expected to be 100% accurate, there may be
121 * notifiers registered or other unexpected conditions that may bring
122 * down the kernel. Or if the current process in the kernel is holding
123 * locks or has other critical state, the kernel may become effectively
124 * unusable anyway.
125 */
126bool die_will_crash(void)
127{
128 if (should_fadump_crash())
129 return true;
130 if (kexec_should_crash(current))
131 return true;
132 if (in_interrupt() || panic_on_oops ||
133 !current->pid || is_global_init(current))
134 return true;
135
136 return false;
137}
138
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000139static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
140static int die_owner = -1;
141static unsigned int die_nest_count;
142static int die_counter;
143
Nicholas Piggin03465f82016-09-16 20:48:08 +1000144static unsigned long oops_begin(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145{
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000146 int cpu;
anton@samba.org34c2a142007-03-20 20:38:13 -0500147 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000148
anton@samba.org293e4682007-03-20 20:38:11 -0500149 oops_enter();
150
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000151 /* racy, but better than risking deadlock. */
152 raw_local_irq_save(flags);
153 cpu = smp_processor_id();
154 if (!arch_spin_trylock(&die_lock)) {
155 if (cpu == die_owner)
156 /* nested oops. should stop eventually */;
157 else
158 arch_spin_lock(&die_lock);
anton@samba.org34c2a142007-03-20 20:38:13 -0500159 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000160 die_nest_count++;
161 die_owner = cpu;
162 console_verbose();
163 bust_spinlocks(1);
164 if (machine_is(powermac))
165 pmac_backlight_unblank();
166 return flags;
167}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000168NOKPROBE_SYMBOL(oops_begin);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700169
Nicholas Piggin03465f82016-09-16 20:48:08 +1000170static void oops_end(unsigned long flags, struct pt_regs *regs,
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000171 int signr)
172{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000173 bust_spinlocks(0);
Rusty Russell373d4d02013-01-21 17:17:39 +1030174 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000175 die_nest_count--;
Anton Blanchard58154c82011-11-30 00:23:09 +0000176 oops_exit();
177 printk("\n");
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100178 if (!die_nest_count) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000179 /* Nest count reaches zero, release the lock. */
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100180 die_owner = -1;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000181 arch_spin_unlock(&die_lock);
Nicholas Piggin7458e8b2016-11-08 23:14:45 +1100182 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000183 raw_local_irq_restore(flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700184
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000185 crash_fadump(regs, "die oops");
186
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000187 if (kexec_should_crash(current))
David Wilderc0ce7d02006-06-23 15:29:34 -0700188 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000189
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000190 if (!signr)
191 return;
192
Anton Blanchard58154c82011-11-30 00:23:09 +0000193 /*
194 * While our oops output is serialised by a spinlock, output
195 * from panic() called below can race and corrupt it. If we
196 * know we are going to panic, delay for 1 second so we have a
197 * chance to get clean backtraces from all CPUs that are oopsing.
198 */
199 if (in_interrupt() || panic_on_oops || !current->pid ||
200 is_global_init(current)) {
201 mdelay(MSEC_PER_SEC);
202 }
203
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000204 if (in_interrupt())
205 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700206 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700207 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000208 do_exit(signr);
209}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000210NOKPROBE_SYMBOL(oops_end);
Hormscea6a4b2006-07-30 03:03:34 -0700211
Nicholas Piggin03465f82016-09-16 20:48:08 +1000212static int __die(const char *str, struct pt_regs *regs, long err)
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000213{
214 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Michael Ellerman2e82ca32017-08-23 23:56:21 +1000215
216 if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
217 printk("LE ");
218 else
219 printk("BE ");
220
Michael Ellerman1c56cd82017-08-23 23:56:22 +1000221 if (IS_ENABLED(CONFIG_PREEMPT))
222 pr_cont("PREEMPT ");
223
224 if (IS_ENABLED(CONFIG_SMP))
225 pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
226
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700227 if (debug_pagealloc_enabled())
Michael Ellerman72c0d9e2017-08-23 23:56:20 +1000228 pr_cont("DEBUG_PAGEALLOC ");
Michael Ellerman1c56cd82017-08-23 23:56:22 +1000229
230 if (IS_ENABLED(CONFIG_NUMA))
231 pr_cont("NUMA ");
232
Michael Ellerman72c0d9e2017-08-23 23:56:20 +1000233 pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000234
235 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
236 return 1;
237
238 print_modules();
239 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000240
241 return 0;
242}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000243NOKPROBE_SYMBOL(__die);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000244
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000245void die(const char *str, struct pt_regs *regs, long err)
246{
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100247 unsigned long flags;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000248
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100249 if (debugger(regs))
250 return;
251
252 flags = oops_begin(regs);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000253 if (__die(str, regs, err))
254 err = 0;
255 oops_end(flags, regs, err);
256}
Naveen N. Rao15770a12017-06-29 23:19:19 +0530257NOKPROBE_SYMBOL(die);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000258
Oleg Nesterov25baa352009-12-15 16:47:18 -0800259void user_single_step_siginfo(struct task_struct *tsk,
260 struct pt_regs *regs, siginfo_t *info)
261{
262 memset(info, 0, sizeof(*info));
263 info->si_signo = SIGTRAP;
264 info->si_code = TRAP_TRACE;
265 info->si_addr = (void __user *)regs->nip;
266}
267
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
269{
270 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000271 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
272 "at %08lx nip %08lx lr %08lx code %x\n";
273 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
274 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000275
276 if (!user_mode(regs)) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000277 die("Exception in kernel mode", regs, signr);
278 return;
279 }
280
281 if (show_unhandled_signals && unhandled_signal(current, signr)) {
Christian Dietrich76462232011-06-04 05:36:54 +0000282 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
283 current->comm, current->pid, signr,
284 addr, regs->nip, regs->link, code);
285 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000286
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +1000287 if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
Benjamin Herrenschmidt9f2f79e2012-03-01 15:47:44 +1100288 local_irq_enable();
289
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000290 current->thread.trap_nr = code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000291 memset(&info, 0, sizeof(info));
292 info.si_signo = signr;
293 info.si_code = code;
294 info.si_addr = (void __user *) addr;
295 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296}
297
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000298void system_reset_exception(struct pt_regs *regs)
299{
Nicholas Piggin2b4f3ac2016-12-20 04:30:07 +1000300 /*
301 * Avoid crashes in case of nested NMI exceptions. Recoverability
302 * is determined by RI and in_nmi
303 */
304 bool nested = in_nmi();
305 if (!nested)
306 nmi_enter();
307
Nicholas Pigginca41ad42017-08-01 22:00:53 +1000308 __this_cpu_inc(irq_stat.sreset_irqs);
309
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000310 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000311 if (ppc_md.system_reset_exception) {
312 if (ppc_md.system_reset_exception(regs))
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000313 goto out;
Arnd Bergmannc902be72006-01-04 19:55:53 +0000314 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000315
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000316 if (debugger(regs))
317 goto out;
318
319 /*
320 * A system reset is a request to dump, so we always send
321 * it through the crashdump code (if fadump or kdump are
322 * registered).
323 */
324 crash_fadump(regs, "System Reset");
325
326 crash_kexec(regs);
327
328 /*
329 * We aren't the primary crash CPU. We need to send it
330 * to a holding pattern to avoid it ending up in the panic
331 * code.
332 */
333 crash_kexec_secondary(regs);
334
335 /*
336 * No debugger or crash dump registered, print logs then
337 * panic.
338 */
339 __die("System Reset", regs, SIGABRT);
340
341 mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
342 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
343 nmi_panic(regs, "System Reset");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000344
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000345out:
346#ifdef CONFIG_PPC_BOOK3S_64
347 BUG_ON(get_paca()->in_nmi == 0);
348 if (get_paca()->in_nmi > 1)
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000349 nmi_panic(regs, "Unrecoverable nested System Reset");
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000350#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000351 /* Must die if the interrupt is not recoverable */
352 if (!(regs->msr & MSR_RI))
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000353 nmi_panic(regs, "Unrecoverable System Reset");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000354
Nicholas Piggin2b4f3ac2016-12-20 04:30:07 +1000355 if (!nested)
356 nmi_exit();
357
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000358 /* What should we do here? We could issue a shutdown or hard reset. */
359}
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530360
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000361/*
362 * I/O accesses can cause machine checks on powermacs.
363 * Check if the NIP corresponds to the address of a sync
364 * instruction for which there is an entry in the exception
365 * table.
366 * Note that the 601 only takes a machine check on TEA
367 * (transfer error ack) signal assertion, and does not
368 * set any of the top 16 bits of SRR1.
369 * -- paulus.
370 */
371static inline int check_io_access(struct pt_regs *regs)
372{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100373#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000374 unsigned long msr = regs->msr;
375 const struct exception_table_entry *entry;
376 unsigned int *nip = (unsigned int *)regs->nip;
377
378 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
379 && (entry = search_exception_tables(regs->nip)) != NULL) {
380 /*
381 * Check that it's a sync instruction, or somewhere
382 * in the twi; isync; nop sequence that inb/inw/inl uses.
383 * As the address is in the exception table
384 * we should be able to read the instr there.
385 * For the debug message, we look at the preceding
386 * load or store.
387 */
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200388 if (*nip == PPC_INST_NOP)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000389 nip -= 2;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200390 else if (*nip == PPC_INST_ISYNC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000391 --nip;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200392 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000393 unsigned int rb;
394
395 --nip;
396 rb = (*nip >> 11) & 0x1f;
397 printk(KERN_DEBUG "%s bad port %lx at %p\n",
398 (*nip & 0x100)? "OUT to": "IN from",
399 regs->gpr[rb] - _IO_BASE, nip);
400 regs->msr |= MSR_RI;
Nicholas Piggin61a92f72016-10-14 16:47:31 +1100401 regs->nip = extable_fixup(entry);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000402 return 1;
403 }
404 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100405#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000406 return 0;
407}
408
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000409#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000410/* On 4xx, the reason for the machine check or program exception
411 is in the ESR. */
412#define get_reason(regs) ((regs)->dsisr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000413#define REASON_FP ESR_FP
414#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
415#define REASON_PRIVILEGED ESR_PPR
416#define REASON_TRAP ESR_PTR
417
418/* single-step stuff */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530419#define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
420#define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000421
422#else
423/* On non-4xx, the reason for the machine check or program
424 exception is in the MSR. */
425#define get_reason(regs) ((regs)->msr)
Michael Ellermand30a5a52017-08-08 16:39:25 +1000426#define REASON_TM SRR1_PROGTM
427#define REASON_FP SRR1_PROGFPE
428#define REASON_ILLEGAL SRR1_PROGILL
429#define REASON_PRIVILEGED SRR1_PROGPRIV
430#define REASON_TRAP SRR1_PROGTRAP
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000431
432#define single_stepping(regs) ((regs)->msr & MSR_SE)
433#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
434#endif
435
Michael Ellerman0d0935b2017-08-08 16:39:21 +1000436#if defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500437int machine_check_e500mc(struct pt_regs *regs)
438{
439 unsigned long mcsr = mfspr(SPRN_MCSR);
440 unsigned long reason = mcsr;
441 int recoverable = 1;
442
Scott Wood82a9a482011-06-16 14:09:17 -0500443 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800444 recoverable = fsl_rio_mcheck_exception(regs);
445 if (recoverable == 1)
446 goto silent_out;
447 }
448
Scott Woodfe04b112010-04-08 00:38:22 -0500449 printk("Machine check in kernel mode.\n");
450 printk("Caused by (from MCSR=%lx): ", reason);
451
452 if (reason & MCSR_MCP)
453 printk("Machine Check Signal\n");
454
455 if (reason & MCSR_ICPERR) {
456 printk("Instruction Cache Parity Error\n");
457
458 /*
459 * This is recoverable by invalidating the i-cache.
460 */
461 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
462 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
463 ;
464
465 /*
466 * This will generally be accompanied by an instruction
467 * fetch error report -- only treat MCSR_IF as fatal
468 * if it wasn't due to an L1 parity error.
469 */
470 reason &= ~MCSR_IF;
471 }
472
473 if (reason & MCSR_DCPERR_MC) {
474 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500475
476 /*
477 * In write shadow mode we auto-recover from the error, but it
478 * may still get logged and cause a machine check. We should
479 * only treat the non-write shadow case as non-recoverable.
480 */
481 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
482 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500483 }
484
485 if (reason & MCSR_L2MMU_MHIT) {
486 printk("Hit on multiple TLB entries\n");
487 recoverable = 0;
488 }
489
490 if (reason & MCSR_NMI)
491 printk("Non-maskable interrupt\n");
492
493 if (reason & MCSR_IF) {
494 printk("Instruction Fetch Error Report\n");
495 recoverable = 0;
496 }
497
498 if (reason & MCSR_LD) {
499 printk("Load Error Report\n");
500 recoverable = 0;
501 }
502
503 if (reason & MCSR_ST) {
504 printk("Store Error Report\n");
505 recoverable = 0;
506 }
507
508 if (reason & MCSR_LDG) {
509 printk("Guarded Load Error Report\n");
510 recoverable = 0;
511 }
512
513 if (reason & MCSR_TLBSYNC)
514 printk("Simultaneous tlbsync operations\n");
515
516 if (reason & MCSR_BSL2_ERR) {
517 printk("Level 2 Cache Error\n");
518 recoverable = 0;
519 }
520
521 if (reason & MCSR_MAV) {
522 u64 addr;
523
524 addr = mfspr(SPRN_MCAR);
525 addr |= (u64)mfspr(SPRN_MCARU) << 32;
526
527 printk("Machine Check %s Address: %#llx\n",
528 reason & MCSR_MEA ? "Effective" : "Physical", addr);
529 }
530
Shaohui Xiecce1f102010-11-18 14:57:32 +0800531silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500532 mtspr(SPRN_MCSR, mcsr);
533 return mfspr(SPRN_MCSR) == 0 && recoverable;
534}
535
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100536int machine_check_e500(struct pt_regs *regs)
537{
Michael Ellerman42bff232017-08-08 16:39:22 +1000538 unsigned long reason = mfspr(SPRN_MCSR);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100539
Shaohui Xiecce1f102010-11-18 14:57:32 +0800540 if (reason & MCSR_BUS_RBERR) {
541 if (fsl_rio_mcheck_exception(regs))
542 return 1;
Hongtao Jia4e0e3432013-04-28 13:20:08 +0800543 if (fsl_pci_mcheck_exception(regs))
544 return 1;
Shaohui Xiecce1f102010-11-18 14:57:32 +0800545 }
546
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000547 printk("Machine check in kernel mode.\n");
548 printk("Caused by (from MCSR=%lx): ", reason);
549
550 if (reason & MCSR_MCP)
551 printk("Machine Check Signal\n");
552 if (reason & MCSR_ICPERR)
553 printk("Instruction Cache Parity Error\n");
554 if (reason & MCSR_DCP_PERR)
555 printk("Data Cache Push Parity Error\n");
556 if (reason & MCSR_DCPERR)
557 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000558 if (reason & MCSR_BUS_IAERR)
559 printk("Bus - Instruction Address Error\n");
560 if (reason & MCSR_BUS_RAERR)
561 printk("Bus - Read Address Error\n");
562 if (reason & MCSR_BUS_WAERR)
563 printk("Bus - Write Address Error\n");
564 if (reason & MCSR_BUS_IBERR)
565 printk("Bus - Instruction Data Error\n");
566 if (reason & MCSR_BUS_RBERR)
567 printk("Bus - Read Data Bus Error\n");
568 if (reason & MCSR_BUS_WBERR)
Wladislav Wiebec1528332014-06-17 15:30:53 +0200569 printk("Bus - Write Data Bus Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000570 if (reason & MCSR_BUS_IPERR)
571 printk("Bus - Instruction Parity Error\n");
572 if (reason & MCSR_BUS_RPERR)
573 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100574
575 return 0;
576}
Kumar Gala4490c062010-10-08 08:32:11 -0500577
578int machine_check_generic(struct pt_regs *regs)
579{
580 return 0;
581}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100582#elif defined(CONFIG_E200)
583int machine_check_e200(struct pt_regs *regs)
584{
Michael Ellerman42bff232017-08-08 16:39:22 +1000585 unsigned long reason = mfspr(SPRN_MCSR);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100586
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000587 printk("Machine check in kernel mode.\n");
588 printk("Caused by (from MCSR=%lx): ", reason);
589
590 if (reason & MCSR_MCP)
591 printk("Machine Check Signal\n");
592 if (reason & MCSR_CP_PERR)
593 printk("Cache Push Parity Error\n");
594 if (reason & MCSR_CPERR)
595 printk("Cache Parity Error\n");
596 if (reason & MCSR_EXCP_ERR)
597 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
598 if (reason & MCSR_BUS_IRERR)
599 printk("Bus - Read Bus Error on instruction fetch\n");
600 if (reason & MCSR_BUS_DRERR)
601 printk("Bus - Read Bus Error on data load\n");
602 if (reason & MCSR_BUS_WRERR)
603 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100604
605 return 0;
606}
Michael Ellerman7f3f8192017-08-08 16:39:23 +1000607#elif defined(CONFIG_PPC32)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100608int machine_check_generic(struct pt_regs *regs)
609{
Michael Ellerman42bff232017-08-08 16:39:22 +1000610 unsigned long reason = regs->msr;
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100611
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000612 printk("Machine check in kernel mode.\n");
613 printk("Caused by (from SRR1=%lx): ", reason);
614 switch (reason & 0x601F0000) {
615 case 0x80000:
616 printk("Machine check signal\n");
617 break;
618 case 0: /* for 601 */
619 case 0x40000:
620 case 0x140000: /* 7450 MSS error and TEA */
621 printk("Transfer error ack signal\n");
622 break;
623 case 0x20000:
624 printk("Data parity error signal\n");
625 break;
626 case 0x10000:
627 printk("Address parity error signal\n");
628 break;
629 case 0x20000000:
630 printk("L1 Data Cache error\n");
631 break;
632 case 0x40000000:
633 printk("L1 Instruction Cache error\n");
634 break;
635 case 0x00100000:
636 printk("L2 data cache parity error\n");
637 break;
638 default:
639 printk("Unknown values in msr\n");
640 }
Olof Johansson75918a42007-09-21 05:11:20 +1000641 return 0;
642}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100643#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000644
645void machine_check_exception(struct pt_regs *regs)
646{
Li Zhongba12eed2013-05-13 16:16:41 +0000647 enum ctx_state prev_state = exception_enter();
Olof Johansson75918a42007-09-21 05:11:20 +1000648 int recover = 0;
649
Nicholas Pigginf886f0f2017-08-01 22:00:51 +1000650 /* 64s accounts the mce in machine_check_early when in HVMODE */
651 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
652 __this_cpu_inc(irq_stat.mce_exceptions);
Anton Blanchard89713ed2010-01-31 20:34:06 +0000653
Mahesh Salgaonkard93b0ac2017-04-18 22:08:17 +0530654 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
655
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100656 /* See if any machine dependent calls. In theory, we would want
657 * to call the CPU first, and call the ppc_md. one if the CPU
658 * one returns a positive number. However there is existing code
659 * that assumes the board gets a first chance, so let's keep it
660 * that way for now and fix things later. --BenH.
661 */
Olof Johansson75918a42007-09-21 05:11:20 +1000662 if (ppc_md.machine_check_exception)
663 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100664 else if (cur_cpu_spec->machine_check)
665 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000666
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100667 if (recover > 0)
Li Zhongba12eed2013-05-13 16:16:41 +0000668 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000669
Anton Blancharda4435062011-01-11 19:45:31 +0000670 if (debugger_fault_handler(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000671 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000672
673 if (check_io_access(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000674 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000675
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000676 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000677
678 /* Must die if the interrupt is not recoverable */
679 if (!(regs->msr & MSR_RI))
680 panic("Unrecoverable Machine check");
Li Zhongba12eed2013-05-13 16:16:41 +0000681
682bail:
683 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000684}
685
686void SMIException(struct pt_regs *regs)
687{
688 die("System Management Interrupt", regs, SIGABRT);
689}
690
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530691void handle_hmi_exception(struct pt_regs *regs)
692{
693 struct pt_regs *old_regs;
694
695 old_regs = set_irq_regs(regs);
696 irq_enter();
697
698 if (ppc_md.handle_hmi_exception)
699 ppc_md.handle_hmi_exception(regs);
700
701 irq_exit();
702 set_irq_regs(old_regs);
703}
704
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000705void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000706{
Li Zhongba12eed2013-05-13 16:16:41 +0000707 enum ctx_state prev_state = exception_enter();
708
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000709 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
710 regs->nip, regs->msr, regs->trap);
711
712 _exception(SIGTRAP, regs, 0, 0);
Li Zhongba12eed2013-05-13 16:16:41 +0000713
714 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000715}
716
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000717void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000718{
Li Zhongba12eed2013-05-13 16:16:41 +0000719 enum ctx_state prev_state = exception_enter();
720
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000721 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
722 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000723 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000724 if (debugger_iabr_match(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000725 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000726 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000727
728bail:
729 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000730}
731
732void RunModeException(struct pt_regs *regs)
733{
734 _exception(SIGTRAP, regs, 0, 0);
735}
736
Nicholas Piggin03465f82016-09-16 20:48:08 +1000737void single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000738{
Li Zhongba12eed2013-05-13 16:16:41 +0000739 enum ctx_state prev_state = exception_enter();
740
K.Prasad2538c2d2010-06-15 11:35:31 +0530741 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000742
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +0530743 if (kprobe_post_handler(regs))
744 return;
745
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000746 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
747 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000748 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000749 if (debugger_sstep(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000750 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000751
752 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000753
754bail:
755 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000756}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000757NOKPROBE_SYMBOL(single_step_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000758
759/*
760 * After we have successfully emulated an instruction, we have to
761 * check if the instruction was being single-stepped, and if so,
762 * pretend we got a single-step exception. This was pointed out
763 * by Kumar Gala. -- paulus
764 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000765static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000766{
K.Prasad2538c2d2010-06-15 11:35:31 +0530767 if (single_stepping(regs))
768 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000769}
770
Kumar Gala5fad2932007-02-07 01:47:59 -0600771static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000772{
Kumar Gala5fad2932007-02-07 01:47:59 -0600773 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000774
775 /* Invalid operation */
776 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600777 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000778
779 /* Overflow */
780 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600781 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000782
783 /* Underflow */
784 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600785 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000786
787 /* Divide by zero */
788 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600789 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000790
791 /* Inexact result */
792 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600793 ret = FPE_FLTRES;
794
795 return ret;
796}
797
798static void parse_fpe(struct pt_regs *regs)
799{
800 int code = 0;
801
802 flush_fp_to_thread(current);
803
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000804 code = __parse_fpscr(current->thread.fp_state.fpscr);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000805
806 _exception(SIGFPE, regs, code, regs->nip);
807}
808
809/*
810 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000811 * provide the PVR to user applications using the mfspr rd, PVR.
812 * Return non-zero if we can't emulate, or -EFAULT if the associated
813 * memory access caused an access fault. Return zero on success.
814 *
815 * There are a couple of ways to do this, either "decode" the instruction
816 * or directly match lots of bits. In this case, matching lots of
817 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000818 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000819 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000820static int emulate_string_inst(struct pt_regs *regs, u32 instword)
821{
822 u8 rT = (instword >> 21) & 0x1f;
823 u8 rA = (instword >> 16) & 0x1f;
824 u8 NB_RB = (instword >> 11) & 0x1f;
825 u32 num_bytes;
826 unsigned long EA;
827 int pos = 0;
828
829 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000830 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000831 if ((rT == rA) || (rT == NB_RB))
832 return -EINVAL;
833
834 EA = (rA == 0) ? 0 : regs->gpr[rA];
835
Kumar Gala16c57b32009-02-10 20:10:44 +0000836 switch (instword & PPC_INST_STRING_MASK) {
837 case PPC_INST_LSWX:
838 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000839 EA += NB_RB;
840 num_bytes = regs->xer & 0x7f;
841 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000842 case PPC_INST_LSWI:
843 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000844 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
845 break;
846 default:
847 return -EINVAL;
848 }
849
850 while (num_bytes != 0)
851 {
852 u8 val;
853 u32 shift = 8 * (3 - (pos & 0x3));
854
James Yang80aa0fb2013-06-25 11:41:05 -0500855 /* if process is 32-bit, clear upper 32 bits of EA */
856 if ((regs->msr & MSR_64BIT) == 0)
857 EA &= 0xFFFFFFFF;
858
Kumar Gala16c57b32009-02-10 20:10:44 +0000859 switch ((instword & PPC_INST_STRING_MASK)) {
860 case PPC_INST_LSWX:
861 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000862 if (get_user(val, (u8 __user *)EA))
863 return -EFAULT;
864 /* first time updating this reg,
865 * zero it out */
866 if (pos == 0)
867 regs->gpr[rT] = 0;
868 regs->gpr[rT] |= val << shift;
869 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000870 case PPC_INST_STSWI:
871 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000872 val = regs->gpr[rT] >> shift;
873 if (put_user(val, (u8 __user *)EA))
874 return -EFAULT;
875 break;
876 }
877 /* move EA to next address */
878 EA += 1;
879 num_bytes--;
880
881 /* manage our position within the register */
882 if (++pos == 4) {
883 pos = 0;
884 if (++rT == 32)
885 rT = 0;
886 }
887 }
888
889 return 0;
890}
891
Will Schmidtc3412dc2006-08-30 13:11:38 -0500892static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
893{
894 u32 ra,rs;
895 unsigned long tmp;
896
897 ra = (instword >> 16) & 0x1f;
898 rs = (instword >> 21) & 0x1f;
899
900 tmp = regs->gpr[rs];
901 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
902 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
903 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
904 regs->gpr[ra] = tmp;
905
906 return 0;
907}
908
Kumar Galac1469f12007-11-19 21:35:29 -0600909static int emulate_isel(struct pt_regs *regs, u32 instword)
910{
911 u8 rT = (instword >> 21) & 0x1f;
912 u8 rA = (instword >> 16) & 0x1f;
913 u8 rB = (instword >> 11) & 0x1f;
914 u8 BC = (instword >> 6) & 0x1f;
915 u8 bit;
916 unsigned long tmp;
917
918 tmp = (rA == 0) ? 0 : regs->gpr[rA];
919 bit = (regs->ccr >> (31 - BC)) & 0x1;
920
921 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
922
923 return 0;
924}
925
Michael Neuling6ce6c622013-05-26 18:09:39 +0000926#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
927static inline bool tm_abort_check(struct pt_regs *regs, int cause)
928{
929 /* If we're emulating a load/store in an active transaction, we cannot
930 * emulate it as the kernel operates in transaction suspended context.
931 * We need to abort the transaction. This creates a persistent TM
932 * abort so tell the user what caused it with a new code.
933 */
934 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
935 tm_enable();
936 tm_abort(cause);
937 return true;
938 }
939 return false;
940}
941#else
942static inline bool tm_abort_check(struct pt_regs *regs, int reason)
943{
944 return false;
945}
946#endif
947
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000948static int emulate_instruction(struct pt_regs *regs)
949{
950 u32 instword;
951 u32 rd;
952
Anton Blanchard4288e342013-08-07 02:01:47 +1000953 if (!user_mode(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000954 return -EINVAL;
955 CHECK_FULL_REGS(regs);
956
957 if (get_user(instword, (u32 __user *)(regs->nip)))
958 return -EFAULT;
959
960 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000961 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000962 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000963 rd = (instword >> 21) & 0x1f;
964 regs->gpr[rd] = mfspr(SPRN_PVR);
965 return 0;
966 }
967
968 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000969 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000970 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000971 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000972 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000973
974 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000975 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000976 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000977 unsigned long msk = 0xf0000000UL >> shift;
978
Anton Blanchardeecff812009-10-27 18:46:55 +0000979 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000980 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
981 regs->xer &= ~0xf0000000UL;
982 return 0;
983 }
984
985 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000986 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Michael Neuling6ce6c622013-05-26 18:09:39 +0000987 if (tm_abort_check(regs,
988 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
989 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +0000990 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000991 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000992 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000993
Will Schmidtc3412dc2006-08-30 13:11:38 -0500994 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000995 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000996 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500997 return emulate_popcntb_inst(regs, instword);
998 }
999
Kumar Galac1469f12007-11-19 21:35:29 -06001000 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +00001001 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001002 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -06001003 return emulate_isel(regs, instword);
1004 }
1005
James Yang9863c282013-07-03 16:26:47 -05001006 /* Emulate sync instruction variants */
1007 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1008 PPC_WARN_EMULATED(sync, regs);
1009 asm volatile("sync");
1010 return 0;
1011 }
1012
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001013#ifdef CONFIG_PPC64
1014 /* Emulate the mfspr rD, DSCR. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001015 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1016 PPC_INST_MFSPR_DSCR_USER) ||
1017 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1018 PPC_INST_MFSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001019 cpu_has_feature(CPU_FTR_DSCR)) {
1020 PPC_WARN_EMULATED(mfdscr, regs);
1021 rd = (instword >> 21) & 0x1f;
1022 regs->gpr[rd] = mfspr(SPRN_DSCR);
1023 return 0;
1024 }
1025 /* Emulate the mtspr DSCR, rD. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001026 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1027 PPC_INST_MTSPR_DSCR_USER) ||
1028 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1029 PPC_INST_MTSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001030 cpu_has_feature(CPU_FTR_DSCR)) {
1031 PPC_WARN_EMULATED(mtdscr, regs);
1032 rd = (instword >> 21) & 0x1f;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001033 current->thread.dscr = regs->gpr[rd];
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001034 current->thread.dscr_inherit = 1;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001035 mtspr(SPRN_DSCR, current->thread.dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001036 return 0;
1037 }
1038#endif
1039
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001040 return -EINVAL;
1041}
1042
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001043int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001044{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001045 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001046}
1047
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001048#ifdef CONFIG_MATH_EMULATION
1049static int emulate_math(struct pt_regs *regs)
1050{
1051 int ret;
1052 extern int do_mathemu(struct pt_regs *regs);
1053
1054 ret = do_mathemu(regs);
1055 if (ret >= 0)
1056 PPC_WARN_EMULATED(math, regs);
1057
1058 switch (ret) {
1059 case 0:
1060 emulate_single_step(regs);
1061 return 0;
1062 case 1: {
1063 int code = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001064 code = __parse_fpscr(current->thread.fp_state.fpscr);
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001065 _exception(SIGFPE, regs, code, regs->nip);
1066 return 0;
1067 }
1068 case -EFAULT:
1069 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1070 return 0;
1071 }
1072
1073 return -1;
1074}
1075#else
1076static inline int emulate_math(struct pt_regs *regs) { return -1; }
1077#endif
1078
Nicholas Piggin03465f82016-09-16 20:48:08 +10001079void program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001080{
Li Zhongba12eed2013-05-13 16:16:41 +00001081 enum ctx_state prev_state = exception_enter();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001082 unsigned int reason = get_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001083
Kim Phillipsaa42c692006-12-08 02:43:30 -06001084 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -06001085 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001086
1087 if (reason & REASON_FP) {
1088 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001089 parse_fpe(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001090 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001091 }
1092 if (reason & REASON_TRAP) {
Balbir Singha4c3f902016-02-18 13:48:01 +11001093 unsigned long bugaddr;
Jason Wesselba797b22010-05-20 21:04:25 -05001094 /* Debugger is first in line to stop recursive faults in
1095 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1096 if (debugger_bpt(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001097 goto bail;
Jason Wesselba797b22010-05-20 21:04:25 -05001098
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301099 if (kprobe_handler(regs))
1100 goto bail;
1101
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001102 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001103 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1104 == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +00001105 goto bail;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001106
Balbir Singha4c3f902016-02-18 13:48:01 +11001107 bugaddr = regs->nip;
1108 /*
1109 * Fixup bugaddr for BUG_ON() in real mode
1110 */
1111 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1112 bugaddr += PAGE_OFFSET;
1113
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001114 if (!(regs->msr & MSR_PR) && /* not user-mode */
Balbir Singha4c3f902016-02-18 13:48:01 +11001115 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001116 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001117 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001118 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001119 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001120 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001121 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001122#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1123 if (reason & REASON_TM) {
1124 /* This is a TM "Bad Thing Exception" program check.
1125 * This occurs when:
1126 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1127 * transition in TM states.
1128 * - A trechkpt is attempted when transactional.
1129 * - A treclaim is attempted when non transactional.
1130 * - A tend is illegally attempted.
1131 * - writing a TM SPR when transactional.
1132 */
1133 if (!user_mode(regs) &&
1134 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1135 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001136 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001137 }
1138 /* If usermode caused this, it's done something illegal and
1139 * gets a SIGILL slap on the wrist. We call it an illegal
1140 * operand to distinguish from the instruction just being bad
1141 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1142 * illegal /placement/ of a valid instruction.
1143 */
1144 if (user_mode(regs)) {
1145 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001146 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001147 } else {
1148 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1149 "at %lx (msr 0x%x)\n", regs->nip, reason);
1150 die("Unrecoverable exception", regs, SIGABRT);
1151 }
1152 }
1153#endif
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001154
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001155 /*
1156 * If we took the program check in the kernel skip down to sending a
1157 * SIGILL. The subsequent cases all relate to emulating instructions
1158 * which we should only do for userspace. We also do not want to enable
1159 * interrupts for kernel faults because that might lead to further
1160 * faults, and loose the context of the original exception.
1161 */
1162 if (!user_mode(regs))
1163 goto sigill;
1164
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001165 /* We restore the interrupt state now */
1166 if (!arch_irq_disabled_regs(regs))
1167 local_irq_enable();
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001168
Kumar Gala04903a32007-02-07 01:13:32 -06001169 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1170 * but there seems to be a hardware bug on the 405GP (RevD)
1171 * that means ESR is sometimes set incorrectly - either to
1172 * ESR_DST (!?) or 0. In the process of chasing this with the
1173 * hardware people - not sure if it can happen on any illegal
1174 * instruction or only on FP instructions, whether there is a
Benjamin Herrenschmidt4e63f8e2013-06-09 17:01:24 +10001175 * pattern to occurrences etc. -dgibson 31/Mar/2003
1176 */
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001177 if (!emulate_math(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001178 goto bail;
Kumar Gala04903a32007-02-07 01:13:32 -06001179
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001180 /* Try to emulate it if we should. */
1181 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001182 switch (emulate_instruction(regs)) {
1183 case 0:
1184 regs->nip += 4;
1185 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001186 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001187 case -EFAULT:
1188 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001189 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001190 }
1191 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001192
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001193sigill:
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001194 if (reason & REASON_PRIVILEGED)
1195 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1196 else
1197 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001198
1199bail:
1200 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001201}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001202NOKPROBE_SYMBOL(program_check_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001203
Paul Mackerrasbf593902013-06-14 20:07:41 +10001204/*
1205 * This occurs when running in hypervisor mode on POWER6 or later
1206 * and an illegal instruction is encountered.
1207 */
Nicholas Piggin03465f82016-09-16 20:48:08 +10001208void emulation_assist_interrupt(struct pt_regs *regs)
Paul Mackerrasbf593902013-06-14 20:07:41 +10001209{
1210 regs->msr |= REASON_ILLEGAL;
1211 program_check_exception(regs);
1212}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001213NOKPROBE_SYMBOL(emulation_assist_interrupt);
Paul Mackerrasbf593902013-06-14 20:07:41 +10001214
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001215void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001216{
Li Zhongba12eed2013-05-13 16:16:41 +00001217 enum ctx_state prev_state = exception_enter();
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001218 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001219
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001220 /* We restore the interrupt state now */
1221 if (!arch_irq_disabled_regs(regs))
1222 local_irq_enable();
1223
Michael Neuling6ce6c622013-05-26 18:09:39 +00001224 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1225 goto bail;
1226
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001227 /* we don't implement logging of alignment exceptions */
1228 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1229 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001230
1231 if (fixed == 1) {
1232 regs->nip += 4; /* skip over emulated instruction */
1233 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001234 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001235 }
1236
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001237 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001238 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001239 sig = SIGSEGV;
1240 code = SEGV_ACCERR;
1241 } else {
1242 sig = SIGBUS;
1243 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001244 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001245 if (user_mode(regs))
1246 _exception(sig, regs, code, regs->dar);
1247 else
1248 bad_page_fault(regs, regs->dar, sig);
Li Zhongba12eed2013-05-13 16:16:41 +00001249
1250bail:
1251 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001252}
1253
Paul Mackerrasf0f558b2016-09-02 21:49:21 +10001254void slb_miss_bad_addr(struct pt_regs *regs)
1255{
1256 enum ctx_state prev_state = exception_enter();
1257
1258 if (user_mode(regs))
1259 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1260 else
1261 bad_page_fault(regs, regs->dar, SIGSEGV);
1262
1263 exception_exit(prev_state);
1264}
1265
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001266void StackOverflow(struct pt_regs *regs)
1267{
1268 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1269 current, regs->gpr[1]);
1270 debugger(regs);
1271 show_regs(regs);
1272 panic("kernel stack overflow");
1273}
1274
1275void nonrecoverable_exception(struct pt_regs *regs)
1276{
1277 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1278 regs->nip, regs->msr);
1279 debugger(regs);
1280 die("nonrecoverable exception", regs, SIGKILL);
1281}
1282
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001283void kernel_fp_unavailable_exception(struct pt_regs *regs)
1284{
Li Zhongba12eed2013-05-13 16:16:41 +00001285 enum ctx_state prev_state = exception_enter();
1286
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001287 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1288 "%lx at %lx\n", regs->trap, regs->nip);
1289 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001290
1291 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001292}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001293
1294void altivec_unavailable_exception(struct pt_regs *regs)
1295{
Li Zhongba12eed2013-05-13 16:16:41 +00001296 enum ctx_state prev_state = exception_enter();
1297
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001298 if (user_mode(regs)) {
1299 /* A user program has executed an altivec instruction,
1300 but this kernel doesn't support altivec. */
1301 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001302 goto bail;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001303 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001304
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001305 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1306 "%lx at %lx\n", regs->trap, regs->nip);
1307 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001308
1309bail:
1310 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001311}
1312
Michael Neulingce48b212008-06-25 14:07:18 +10001313void vsx_unavailable_exception(struct pt_regs *regs)
1314{
1315 if (user_mode(regs)) {
1316 /* A user program has executed an vsx instruction,
1317 but this kernel doesn't support vsx. */
1318 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1319 return;
1320 }
1321
1322 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1323 "%lx at %lx\n", regs->trap, regs->nip);
1324 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1325}
1326
Michael Neuling25176172013-08-09 17:29:29 +10001327#ifdef CONFIG_PPC64
Cyril Bur172f7aa2016-09-14 18:02:15 +10001328static void tm_unavailable(struct pt_regs *regs)
1329{
Cyril Bur5d176f72016-09-14 18:02:16 +10001330#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1331 if (user_mode(regs)) {
1332 current->thread.load_tm++;
1333 regs->msr |= MSR_TM;
1334 tm_enable();
1335 tm_restore_sprs(&current->thread);
1336 return;
1337 }
1338#endif
Cyril Bur172f7aa2016-09-14 18:02:15 +10001339 pr_emerg("Unrecoverable TM Unavailable Exception "
1340 "%lx at %lx\n", regs->trap, regs->nip);
1341 die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1342}
1343
Michael Ellerman021424a2013-06-25 17:47:56 +10001344void facility_unavailable_exception(struct pt_regs *regs)
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001345{
Michael Ellerman021424a2013-06-25 17:47:56 +10001346 static char *facility_strings[] = {
Michael Neuling25176172013-08-09 17:29:29 +10001347 [FSCR_FP_LG] = "FPU",
1348 [FSCR_VECVSX_LG] = "VMX/VSX",
1349 [FSCR_DSCR_LG] = "DSCR",
1350 [FSCR_PM_LG] = "PMU SPRs",
1351 [FSCR_BHRB_LG] = "BHRB",
1352 [FSCR_TM_LG] = "TM",
1353 [FSCR_EBB_LG] = "EBB",
1354 [FSCR_TAR_LG] = "TAR",
Nicholas Piggin794464f2017-04-07 11:27:43 +10001355 [FSCR_MSGP_LG] = "MSGP",
Nicholas Piggin9b7ff0c2017-04-07 11:27:44 +10001356 [FSCR_SCV_LG] = "SCV",
Michael Ellerman021424a2013-06-25 17:47:56 +10001357 };
Michael Neuling25176172013-08-09 17:29:29 +10001358 char *facility = "unknown";
Michael Ellerman021424a2013-06-25 17:47:56 +10001359 u64 value;
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301360 u32 instword, rd;
Michael Neuling25176172013-08-09 17:29:29 +10001361 u8 status;
1362 bool hv;
Michael Ellerman021424a2013-06-25 17:47:56 +10001363
Michael Neuling25176172013-08-09 17:29:29 +10001364 hv = (regs->trap == 0xf80);
1365 if (hv)
Michael Ellermanb14b6262013-06-25 17:47:57 +10001366 value = mfspr(SPRN_HFSCR);
Michael Neuling25176172013-08-09 17:29:29 +10001367 else
1368 value = mfspr(SPRN_FSCR);
1369
1370 status = value >> 56;
1371 if (status == FSCR_DSCR_LG) {
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301372 /*
1373 * User is accessing the DSCR register using the problem
1374 * state only SPR number (0x03) either through a mfspr or
1375 * a mtspr instruction. If it is a write attempt through
1376 * a mtspr, then we set the inherit bit. This also allows
1377 * the user to write or read the register directly in the
1378 * future by setting via the FSCR DSCR bit. But in case it
1379 * is a read DSCR attempt through a mfspr instruction, we
1380 * just emulate the instruction instead. This code path will
1381 * always emulate all the mfspr instructions till the user
Adam Buchbinder446957b2016-02-24 10:51:11 -08001382 * has attempted at least one mtspr instruction. This way it
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301383 * preserves the same behaviour when the user is accessing
1384 * the DSCR through privilege level only SPR number (0x11)
1385 * which is emulated through illegal instruction exception.
1386 * We always leave HFSCR DSCR set.
Michael Neuling25176172013-08-09 17:29:29 +10001387 */
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301388 if (get_user(instword, (u32 __user *)(regs->nip))) {
1389 pr_err("Failed to fetch the user instruction\n");
1390 return;
1391 }
1392
1393 /* Write into DSCR (mtspr 0x03, RS) */
1394 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1395 == PPC_INST_MTSPR_DSCR_USER) {
1396 rd = (instword >> 21) & 0x1f;
1397 current->thread.dscr = regs->gpr[rd];
1398 current->thread.dscr_inherit = 1;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001399 current->thread.fscr |= FSCR_DSCR;
1400 mtspr(SPRN_FSCR, current->thread.fscr);
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301401 }
1402
1403 /* Read from DSCR (mfspr RT, 0x03) */
1404 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1405 == PPC_INST_MFSPR_DSCR_USER) {
1406 if (emulate_instruction(regs)) {
1407 pr_err("DSCR based mfspr emulation failed\n");
1408 return;
1409 }
1410 regs->nip += 4;
1411 emulate_single_step(regs);
1412 }
Michael Neuling25176172013-08-09 17:29:29 +10001413 return;
Michael Ellermanb14b6262013-06-25 17:47:57 +10001414 }
1415
Cyril Bur172f7aa2016-09-14 18:02:15 +10001416 if (status == FSCR_TM_LG) {
1417 /*
1418 * If we're here then the hardware is TM aware because it
1419 * generated an exception with FSRM_TM set.
1420 *
1421 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1422 * told us not to do TM, or the kernel is not built with TM
1423 * support.
1424 *
1425 * If both of those things are true, then userspace can spam the
1426 * console by triggering the printk() below just by continually
1427 * doing tbegin (or any TM instruction). So in that case just
1428 * send the process a SIGILL immediately.
1429 */
1430 if (!cpu_has_feature(CPU_FTR_TM))
1431 goto out;
1432
1433 tm_unavailable(regs);
1434 return;
1435 }
1436
Balbir Singh93c2ec02016-11-30 17:45:09 +11001437 if ((hv || status >= 2) &&
1438 (status < ARRAY_SIZE(facility_strings)) &&
Michael Neuling25176172013-08-09 17:29:29 +10001439 facility_strings[status])
1440 facility = facility_strings[status];
Michael Ellerman021424a2013-06-25 17:47:56 +10001441
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001442 /* We restore the interrupt state now */
1443 if (!arch_irq_disabled_regs(regs))
1444 local_irq_enable();
1445
Balbir Singh93c2ec02016-11-30 17:45:09 +11001446 pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
1447 hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001448
Cyril Bur172f7aa2016-09-14 18:02:15 +10001449out:
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001450 if (user_mode(regs)) {
1451 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1452 return;
1453 }
1454
Michael Ellerman021424a2013-06-25 17:47:56 +10001455 die("Unexpected facility unavailable exception", regs, SIGABRT);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001456}
Michael Neuling25176172013-08-09 17:29:29 +10001457#endif
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001458
Michael Neulingf54db642013-02-13 16:21:39 +00001459#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1460
Michael Neulingf54db642013-02-13 16:21:39 +00001461void fp_unavailable_tm(struct pt_regs *regs)
1462{
1463 /* Note: This does not handle any kind of FP laziness. */
1464
1465 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1466 regs->nip, regs->msr);
Michael Neulingf54db642013-02-13 16:21:39 +00001467
1468 /* We can only have got here if the task started using FP after
1469 * beginning the transaction. So, the transactional regs are just a
1470 * copy of the checkpointed ones. But, we still need to recheckpoint
1471 * as we're enabling FP for the process; it will return, abort the
1472 * transaction, and probably retry but now with FP enabled. So the
1473 * checkpointed FP registers need to be loaded.
1474 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001475 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001476 /* Reclaim didn't save out any FPRs to transact_fprs. */
1477
1478 /* Enable FP for the task: */
1479 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1480
1481 /* This loads and recheckpoints the FP registers from
1482 * thread.fpr[]. They will remain in registers after the
1483 * checkpoint so we don't need to reload them after.
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001484 * If VMX is in use, the VRs now hold checkpointed values,
1485 * so we don't want to load the VRs from the thread_struct.
Michael Neulingf54db642013-02-13 16:21:39 +00001486 */
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001487 tm_recheckpoint(&current->thread, MSR_FP);
1488
1489 /* If VMX is in use, get the transactional values back */
1490 if (regs->msr & MSR_VEC) {
Cyril Burdc310662016-09-23 16:18:24 +10001491 msr_check_and_set(MSR_VEC);
1492 load_vr_state(&current->thread.vr_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001493 /* At this point all the VSX state is loaded, so enable it */
1494 regs->msr |= MSR_VSX;
1495 }
Michael Neulingf54db642013-02-13 16:21:39 +00001496}
1497
Michael Neulingf54db642013-02-13 16:21:39 +00001498void altivec_unavailable_tm(struct pt_regs *regs)
1499{
1500 /* See the comments in fp_unavailable_tm(). This function operates
1501 * the same way.
1502 */
1503
1504 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1505 "MSR=%lx\n",
1506 regs->nip, regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001507 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001508 regs->msr |= MSR_VEC;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001509 tm_recheckpoint(&current->thread, MSR_VEC);
Michael Neulingf54db642013-02-13 16:21:39 +00001510 current->thread.used_vr = 1;
Michael Neulingf54db642013-02-13 16:21:39 +00001511
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001512 if (regs->msr & MSR_FP) {
Cyril Burdc310662016-09-23 16:18:24 +10001513 msr_check_and_set(MSR_FP);
1514 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001515 regs->msr |= MSR_VSX;
1516 }
1517}
1518
Michael Neulingf54db642013-02-13 16:21:39 +00001519void vsx_unavailable_tm(struct pt_regs *regs)
1520{
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001521 unsigned long orig_msr = regs->msr;
1522
Michael Neulingf54db642013-02-13 16:21:39 +00001523 /* See the comments in fp_unavailable_tm(). This works similarly,
1524 * though we're loading both FP and VEC registers in here.
1525 *
1526 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1527 * regs. Either way, set MSR_VSX.
1528 */
1529
1530 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1531 "MSR=%lx\n",
1532 regs->nip, regs->msr);
1533
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001534 current->thread.used_vsr = 1;
1535
1536 /* If FP and VMX are already loaded, we have all the state we need */
1537 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1538 regs->msr |= MSR_VSX;
1539 return;
1540 }
1541
Michael Neulingf54db642013-02-13 16:21:39 +00001542 /* This reclaims FP and/or VR regs if they're already enabled */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001543 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001544
1545 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1546 MSR_VSX;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001547
1548 /* This loads & recheckpoints FP and VRs; but we have
1549 * to be sure not to overwrite previously-valid state.
1550 */
1551 tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1552
Cyril Burdc310662016-09-23 16:18:24 +10001553 msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1554
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001555 if (orig_msr & MSR_FP)
Cyril Burdc310662016-09-23 16:18:24 +10001556 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001557 if (orig_msr & MSR_VEC)
Cyril Burdc310662016-09-23 16:18:24 +10001558 load_vr_state(&current->thread.vr_state);
Michael Neulingf54db642013-02-13 16:21:39 +00001559}
Michael Neulingf54db642013-02-13 16:21:39 +00001560#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1561
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001562void performance_monitor_exception(struct pt_regs *regs)
1563{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001564 __this_cpu_inc(irq_stat.pmu_irqs);
Anton Blanchard89713ed2010-01-31 20:34:06 +00001565
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001566 perf_irq(regs);
1567}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001568
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001569#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001570static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1571{
1572 int changed = 0;
1573 /*
1574 * Determine the cause of the debug event, clear the
1575 * event flags and send a trap to the handler. Torez
1576 */
1577 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1578 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1579#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301580 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001581#endif
1582 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1583 5);
1584 changed |= 0x01;
1585 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1586 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1587 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1588 6);
1589 changed |= 0x01;
1590 } else if (debug_status & DBSR_IAC1) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301591 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001592 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1593 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1594 1);
1595 changed |= 0x01;
1596 } else if (debug_status & DBSR_IAC2) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301597 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001598 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1599 2);
1600 changed |= 0x01;
1601 } else if (debug_status & DBSR_IAC3) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301602 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001603 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1604 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1605 3);
1606 changed |= 0x01;
1607 } else if (debug_status & DBSR_IAC4) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301608 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001609 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1610 4);
1611 changed |= 0x01;
1612 }
1613 /*
1614 * At the point this routine was called, the MSR(DE) was turned off.
1615 * Check all other debug flags and see if that bit needs to be turned
1616 * back on or not.
1617 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301618 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
Bharat Bhushan95791982013-06-26 11:12:22 +05301619 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001620 regs->msr |= MSR_DE;
1621 else
1622 /* Make sure the IDM flag is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301623 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001624
1625 if (changed & 0x01)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301626 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001627}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001628
Nicholas Piggin03465f82016-09-16 20:48:08 +10001629void DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001630{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301631 current->thread.debug.dbsr = debug_status;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001632
Roland McGrathec097c82009-05-28 21:26:38 +00001633 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1634 * on server, it stops on the target of the branch. In order to simulate
1635 * the server behaviour, we thus restart right away with a single step
1636 * instead of stopping here when hitting a BT
1637 */
1638 if (debug_status & DBSR_BT) {
1639 regs->msr &= ~MSR_DE;
1640
1641 /* Disable BT */
1642 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1643 /* Clear the BT event */
1644 mtspr(SPRN_DBSR, DBSR_BT);
1645
1646 /* Do the single step trick only when coming from userspace */
1647 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301648 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1649 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Roland McGrathec097c82009-05-28 21:26:38 +00001650 regs->msr |= MSR_DE;
1651 return;
1652 }
1653
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301654 if (kprobe_post_handler(regs))
1655 return;
1656
Roland McGrathec097c82009-05-28 21:26:38 +00001657 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1658 5, SIGTRAP) == NOTIFY_STOP) {
1659 return;
1660 }
1661 if (debugger_sstep(regs))
1662 return;
1663 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001664 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001665
1666 /* Disable instruction completion */
1667 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1668 /* Clear the instruction completion event */
1669 mtspr(SPRN_DBSR, DBSR_IC);
1670
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301671 if (kprobe_post_handler(regs))
1672 return;
1673
Kumar Galaf8279622008-06-26 02:01:37 -05001674 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1675 5, SIGTRAP) == NOTIFY_STOP) {
1676 return;
1677 }
1678
1679 if (debugger_sstep(regs))
1680 return;
1681
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001682 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301683 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1684 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1685 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001686 regs->msr |= MSR_DE;
1687 else
1688 /* Make sure the IDM bit is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301689 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001690 }
Kumar Galaf8279622008-06-26 02:01:37 -05001691
1692 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001693 } else
1694 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001695}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001696NOKPROBE_SYMBOL(DebugException);
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001697#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001698
1699#if !defined(CONFIG_TAU_INT)
1700void TAUException(struct pt_regs *regs)
1701{
1702 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1703 regs->nip, regs->msr, regs->trap, print_tainted());
1704}
1705#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001706
1707#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001708void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001709{
1710 int err;
1711
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001712 if (!user_mode(regs)) {
1713 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1714 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001715 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001716 }
1717
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001718 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001719
Anton Blanchardeecff812009-10-27 18:46:55 +00001720 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001721 err = emulate_altivec(regs);
1722 if (err == 0) {
1723 regs->nip += 4; /* skip emulated instruction */
1724 emulate_single_step(regs);
1725 return;
1726 }
1727
1728 if (err == -EFAULT) {
1729 /* got an error reading the instruction */
1730 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1731 } else {
1732 /* didn't recognize the instruction */
1733 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001734 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1735 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001736 current->thread.vr_state.vscr.u[3] |= 0x10000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001737 }
1738}
1739#endif /* CONFIG_ALTIVEC */
1740
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001741#ifdef CONFIG_FSL_BOOKE
1742void CacheLockingException(struct pt_regs *regs, unsigned long address,
1743 unsigned long error_code)
1744{
1745 /* We treat cache locking instructions from the user
1746 * as priv ops, in the future we could try to do
1747 * something smarter
1748 */
1749 if (error_code & (ESR_DLK|ESR_ILK))
1750 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1751 return;
1752}
1753#endif /* CONFIG_FSL_BOOKE */
1754
1755#ifdef CONFIG_SPE
1756void SPEFloatingPointException(struct pt_regs *regs)
1757{
Liu Yu6a800f32008-10-28 11:50:21 +08001758 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001759 unsigned long spefscr;
1760 int fpexc_mode;
1761 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001762 int err;
1763
yu liu685659e2011-06-14 18:34:25 -05001764 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001765
1766 spefscr = current->thread.spefscr;
1767 fpexc_mode = current->thread.fpexc_mode;
1768
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001769 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1770 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001771 }
1772 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1773 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001774 }
1775 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1776 code = FPE_FLTDIV;
1777 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1778 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001779 }
1780 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1781 code = FPE_FLTRES;
1782
Liu Yu6a800f32008-10-28 11:50:21 +08001783 err = do_spe_mathemu(regs);
1784 if (err == 0) {
1785 regs->nip += 4; /* skip emulated instruction */
1786 emulate_single_step(regs);
1787 return;
1788 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001789
Liu Yu6a800f32008-10-28 11:50:21 +08001790 if (err == -EFAULT) {
1791 /* got an error reading the instruction */
1792 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1793 } else if (err == -EINVAL) {
1794 /* didn't recognize the instruction */
1795 printk(KERN_ERR "unrecognized spe instruction "
1796 "in %s at %lx\n", current->comm, regs->nip);
1797 } else {
1798 _exception(SIGFPE, regs, code, regs->nip);
1799 }
1800
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001801 return;
1802}
Liu Yu6a800f32008-10-28 11:50:21 +08001803
1804void SPEFloatingPointRoundException(struct pt_regs *regs)
1805{
1806 extern int speround_handler(struct pt_regs *regs);
1807 int err;
1808
1809 preempt_disable();
1810 if (regs->msr & MSR_SPE)
1811 giveup_spe(current);
1812 preempt_enable();
1813
1814 regs->nip -= 4;
1815 err = speround_handler(regs);
1816 if (err == 0) {
1817 regs->nip += 4; /* skip emulated instruction */
1818 emulate_single_step(regs);
1819 return;
1820 }
1821
1822 if (err == -EFAULT) {
1823 /* got an error reading the instruction */
1824 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1825 } else if (err == -EINVAL) {
1826 /* didn't recognize the instruction */
1827 printk(KERN_ERR "unrecognized spe instruction "
1828 "in %s at %lx\n", current->comm, regs->nip);
1829 } else {
1830 _exception(SIGFPE, regs, 0, regs->nip);
1831 return;
1832 }
1833}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001834#endif
1835
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001836/*
1837 * We enter here if we get an unrecoverable exception, that is, one
1838 * that happened at a point where the RI (recoverable interrupt) bit
1839 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1840 * we therefore lost state by taking this exception.
1841 */
1842void unrecoverable_exception(struct pt_regs *regs)
1843{
1844 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1845 regs->trap, regs->nip);
1846 die("Unrecoverable exception", regs, SIGABRT);
1847}
Naveen N. Rao15770a12017-06-29 23:19:19 +05301848NOKPROBE_SYMBOL(unrecoverable_exception);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001849
Jason Gunthorpe1e18c172012-10-05 08:07:15 +00001850#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001851/*
1852 * Default handler for a Watchdog exception,
1853 * spins until a reboot occurs
1854 */
1855void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1856{
1857 /* Generic WatchdogHandler, implement your own */
1858 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1859 return;
1860}
1861
1862void WatchdogException(struct pt_regs *regs)
1863{
1864 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1865 WatchdogHandler(regs);
1866}
1867#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001868
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001869/*
1870 * We enter here if we discover during exception entry that we are
1871 * running in supervisor mode with a userspace value in the stack pointer.
1872 */
1873void kernel_bad_stack(struct pt_regs *regs)
1874{
1875 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1876 regs->gpr[1], regs->nip);
1877 die("Bad kernel stack pointer", regs, SIGABRT);
1878}
Naveen N. Rao15770a12017-06-29 23:19:19 +05301879NOKPROBE_SYMBOL(kernel_bad_stack);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001880
1881void __init trap_init(void)
1882{
1883}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001884
1885
1886#ifdef CONFIG_PPC_EMULATED_STATS
1887
1888#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1889
1890struct ppc_emulated ppc_emulated = {
1891#ifdef CONFIG_ALTIVEC
1892 WARN_EMULATED_SETUP(altivec),
1893#endif
1894 WARN_EMULATED_SETUP(dcba),
1895 WARN_EMULATED_SETUP(dcbz),
1896 WARN_EMULATED_SETUP(fp_pair),
1897 WARN_EMULATED_SETUP(isel),
1898 WARN_EMULATED_SETUP(mcrxr),
1899 WARN_EMULATED_SETUP(mfpvr),
1900 WARN_EMULATED_SETUP(multiple),
1901 WARN_EMULATED_SETUP(popcntb),
1902 WARN_EMULATED_SETUP(spe),
1903 WARN_EMULATED_SETUP(string),
Scott Wooda3821b22013-10-28 22:07:59 -05001904 WARN_EMULATED_SETUP(sync),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001905 WARN_EMULATED_SETUP(unaligned),
1906#ifdef CONFIG_MATH_EMULATION
1907 WARN_EMULATED_SETUP(math),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001908#endif
1909#ifdef CONFIG_VSX
1910 WARN_EMULATED_SETUP(vsx),
1911#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001912#ifdef CONFIG_PPC64
1913 WARN_EMULATED_SETUP(mfdscr),
1914 WARN_EMULATED_SETUP(mtdscr),
Anton Blanchardf83319d2014-03-28 17:01:23 +11001915 WARN_EMULATED_SETUP(lq_stq),
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001916#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001917};
1918
1919u32 ppc_warn_emulated;
1920
1921void ppc_warn_emulated_print(const char *type)
1922{
Christian Dietrich76462232011-06-04 05:36:54 +00001923 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1924 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001925}
1926
1927static int __init ppc_warn_emulated_init(void)
1928{
1929 struct dentry *dir, *d;
1930 unsigned int i;
1931 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1932
1933 if (!powerpc_debugfs_root)
1934 return -ENODEV;
1935
1936 dir = debugfs_create_dir("emulated_instructions",
1937 powerpc_debugfs_root);
1938 if (!dir)
1939 return -ENOMEM;
1940
1941 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1942 &ppc_warn_emulated);
1943 if (!d)
1944 goto fail;
1945
1946 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1947 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1948 (u32 *)&entries[i].val.counter);
1949 if (!d)
1950 goto fail;
1951 }
1952
1953 return 0;
1954
1955fail:
1956 debugfs_remove_recursive(dir);
1957 return -ENOMEM;
1958}
1959
1960device_initcall(ppc_warn_emulated_init);
1961
1962#endif /* CONFIG_PPC_EMULATED_STATS */