blob: 5e4db2fc2add2f9826ed4800a7a2591e45d10cec [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
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000165 if (kexec_should_crash(current))
David Wilderc0ce7d02006-06-23 15:29:34 -0700166 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000167
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000168 if (!signr)
169 return;
170
Anton Blanchard58154c82011-11-30 00:23:09 +0000171 /*
172 * While our oops output is serialised by a spinlock, output
173 * from panic() called below can race and corrupt it. If we
174 * know we are going to panic, delay for 1 second so we have a
175 * chance to get clean backtraces from all CPUs that are oopsing.
176 */
177 if (in_interrupt() || panic_on_oops || !current->pid ||
178 is_global_init(current)) {
179 mdelay(MSEC_PER_SEC);
180 }
181
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000182 if (in_interrupt())
183 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700184 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700185 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000186 do_exit(signr);
187}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000188NOKPROBE_SYMBOL(oops_end);
Hormscea6a4b2006-07-30 03:03:34 -0700189
Nicholas Piggin03465f82016-09-16 20:48:08 +1000190static int __die(const char *str, struct pt_regs *regs, long err)
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000191{
192 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Michael Ellerman2e82ca32017-08-23 23:56:21 +1000193
194 if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
195 printk("LE ");
196 else
197 printk("BE ");
198
Michael Ellerman1c56cd82017-08-23 23:56:22 +1000199 if (IS_ENABLED(CONFIG_PREEMPT))
200 pr_cont("PREEMPT ");
201
202 if (IS_ENABLED(CONFIG_SMP))
203 pr_cont("SMP NR_CPUS=%d ", NR_CPUS);
204
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700205 if (debug_pagealloc_enabled())
Michael Ellerman72c0d9e2017-08-23 23:56:20 +1000206 pr_cont("DEBUG_PAGEALLOC ");
Michael Ellerman1c56cd82017-08-23 23:56:22 +1000207
208 if (IS_ENABLED(CONFIG_NUMA))
209 pr_cont("NUMA ");
210
Michael Ellerman72c0d9e2017-08-23 23:56:20 +1000211 pr_cont("%s\n", ppc_md.name ? ppc_md.name : "");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000212
213 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
214 return 1;
215
216 print_modules();
217 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218
219 return 0;
220}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000221NOKPROBE_SYMBOL(__die);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000222
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000223void die(const char *str, struct pt_regs *regs, long err)
224{
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100225 unsigned long flags;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000226
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100227 if (debugger(regs))
228 return;
229
230 flags = oops_begin(regs);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000231 if (__die(str, regs, err))
232 err = 0;
233 oops_end(flags, regs, err);
234}
Naveen N. Rao15770a12017-06-29 23:19:19 +0530235NOKPROBE_SYMBOL(die);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000236
Oleg Nesterov25baa352009-12-15 16:47:18 -0800237void user_single_step_siginfo(struct task_struct *tsk,
238 struct pt_regs *regs, siginfo_t *info)
239{
240 memset(info, 0, sizeof(*info));
241 info->si_signo = SIGTRAP;
242 info->si_code = TRAP_TRACE;
243 info->si_addr = (void __user *)regs->nip;
244}
245
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000246void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
247{
248 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000249 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
250 "at %08lx nip %08lx lr %08lx code %x\n";
251 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
252 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000253
254 if (!user_mode(regs)) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000255 die("Exception in kernel mode", regs, signr);
256 return;
257 }
258
259 if (show_unhandled_signals && unhandled_signal(current, signr)) {
Christian Dietrich76462232011-06-04 05:36:54 +0000260 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
261 current->comm, current->pid, signr,
262 addr, regs->nip, regs->link, code);
263 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000264
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +1000265 if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
Benjamin Herrenschmidt9f2f79e2012-03-01 15:47:44 +1100266 local_irq_enable();
267
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000268 current->thread.trap_nr = code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000269 memset(&info, 0, sizeof(info));
270 info.si_signo = signr;
271 info.si_code = code;
272 info.si_addr = (void __user *) addr;
273 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000274}
275
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000276void system_reset_exception(struct pt_regs *regs)
277{
Nicholas Piggin2b4f3ac2016-12-20 04:30:07 +1000278 /*
279 * Avoid crashes in case of nested NMI exceptions. Recoverability
280 * is determined by RI and in_nmi
281 */
282 bool nested = in_nmi();
283 if (!nested)
284 nmi_enter();
285
Nicholas Pigginca41ad42017-08-01 22:00:53 +1000286 __this_cpu_inc(irq_stat.sreset_irqs);
287
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000288 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000289 if (ppc_md.system_reset_exception) {
290 if (ppc_md.system_reset_exception(regs))
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000291 goto out;
Arnd Bergmannc902be72006-01-04 19:55:53 +0000292 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000293
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000294 if (debugger(regs))
295 goto out;
296
297 /*
298 * A system reset is a request to dump, so we always send
299 * it through the crashdump code (if fadump or kdump are
300 * registered).
301 */
302 crash_fadump(regs, "System Reset");
303
304 crash_kexec(regs);
305
306 /*
307 * We aren't the primary crash CPU. We need to send it
308 * to a holding pattern to avoid it ending up in the panic
309 * code.
310 */
311 crash_kexec_secondary(regs);
312
313 /*
314 * No debugger or crash dump registered, print logs then
315 * panic.
316 */
317 __die("System Reset", regs, SIGABRT);
318
319 mdelay(2*MSEC_PER_SEC); /* Wait a little while for others to print */
320 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
321 nmi_panic(regs, "System Reset");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000323out:
324#ifdef CONFIG_PPC_BOOK3S_64
325 BUG_ON(get_paca()->in_nmi == 0);
326 if (get_paca()->in_nmi > 1)
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000327 nmi_panic(regs, "Unrecoverable nested System Reset");
Nicholas Pigginc4f3b522016-12-20 04:30:05 +1000328#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000329 /* Must die if the interrupt is not recoverable */
330 if (!(regs->msr & MSR_RI))
Nicholas Piggin4388c9b2017-07-05 13:56:27 +1000331 nmi_panic(regs, "Unrecoverable System Reset");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000332
Nicholas Piggin2b4f3ac2016-12-20 04:30:07 +1000333 if (!nested)
334 nmi_exit();
335
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336 /* What should we do here? We could issue a shutdown or hard reset. */
337}
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530338
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000339/*
340 * I/O accesses can cause machine checks on powermacs.
341 * Check if the NIP corresponds to the address of a sync
342 * instruction for which there is an entry in the exception
343 * table.
344 * Note that the 601 only takes a machine check on TEA
345 * (transfer error ack) signal assertion, and does not
346 * set any of the top 16 bits of SRR1.
347 * -- paulus.
348 */
349static inline int check_io_access(struct pt_regs *regs)
350{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100351#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000352 unsigned long msr = regs->msr;
353 const struct exception_table_entry *entry;
354 unsigned int *nip = (unsigned int *)regs->nip;
355
356 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
357 && (entry = search_exception_tables(regs->nip)) != NULL) {
358 /*
359 * Check that it's a sync instruction, or somewhere
360 * in the twi; isync; nop sequence that inb/inw/inl uses.
361 * As the address is in the exception table
362 * we should be able to read the instr there.
363 * For the debug message, we look at the preceding
364 * load or store.
365 */
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200366 if (*nip == PPC_INST_NOP)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000367 nip -= 2;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200368 else if (*nip == PPC_INST_ISYNC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000369 --nip;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200370 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000371 unsigned int rb;
372
373 --nip;
374 rb = (*nip >> 11) & 0x1f;
375 printk(KERN_DEBUG "%s bad port %lx at %p\n",
376 (*nip & 0x100)? "OUT to": "IN from",
377 regs->gpr[rb] - _IO_BASE, nip);
378 regs->msr |= MSR_RI;
Nicholas Piggin61a92f72016-10-14 16:47:31 +1100379 regs->nip = extable_fixup(entry);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000380 return 1;
381 }
382 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100383#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000384 return 0;
385}
386
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000387#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000388/* On 4xx, the reason for the machine check or program exception
389 is in the ESR. */
390#define get_reason(regs) ((regs)->dsisr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000391#define REASON_FP ESR_FP
392#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
393#define REASON_PRIVILEGED ESR_PPR
394#define REASON_TRAP ESR_PTR
395
396/* single-step stuff */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530397#define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
398#define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000399
400#else
401/* On non-4xx, the reason for the machine check or program
402 exception is in the MSR. */
403#define get_reason(regs) ((regs)->msr)
Michael Ellermand30a5a52017-08-08 16:39:25 +1000404#define REASON_TM SRR1_PROGTM
405#define REASON_FP SRR1_PROGFPE
406#define REASON_ILLEGAL SRR1_PROGILL
407#define REASON_PRIVILEGED SRR1_PROGPRIV
408#define REASON_TRAP SRR1_PROGTRAP
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000409
410#define single_stepping(regs) ((regs)->msr & MSR_SE)
411#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
412#endif
413
Michael Ellerman0d0935b2017-08-08 16:39:21 +1000414#if defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500415int machine_check_e500mc(struct pt_regs *regs)
416{
417 unsigned long mcsr = mfspr(SPRN_MCSR);
418 unsigned long reason = mcsr;
419 int recoverable = 1;
420
Scott Wood82a9a482011-06-16 14:09:17 -0500421 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800422 recoverable = fsl_rio_mcheck_exception(regs);
423 if (recoverable == 1)
424 goto silent_out;
425 }
426
Scott Woodfe04b112010-04-08 00:38:22 -0500427 printk("Machine check in kernel mode.\n");
428 printk("Caused by (from MCSR=%lx): ", reason);
429
430 if (reason & MCSR_MCP)
431 printk("Machine Check Signal\n");
432
433 if (reason & MCSR_ICPERR) {
434 printk("Instruction Cache Parity Error\n");
435
436 /*
437 * This is recoverable by invalidating the i-cache.
438 */
439 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
440 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
441 ;
442
443 /*
444 * This will generally be accompanied by an instruction
445 * fetch error report -- only treat MCSR_IF as fatal
446 * if it wasn't due to an L1 parity error.
447 */
448 reason &= ~MCSR_IF;
449 }
450
451 if (reason & MCSR_DCPERR_MC) {
452 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500453
454 /*
455 * In write shadow mode we auto-recover from the error, but it
456 * may still get logged and cause a machine check. We should
457 * only treat the non-write shadow case as non-recoverable.
458 */
459 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
460 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500461 }
462
463 if (reason & MCSR_L2MMU_MHIT) {
464 printk("Hit on multiple TLB entries\n");
465 recoverable = 0;
466 }
467
468 if (reason & MCSR_NMI)
469 printk("Non-maskable interrupt\n");
470
471 if (reason & MCSR_IF) {
472 printk("Instruction Fetch Error Report\n");
473 recoverable = 0;
474 }
475
476 if (reason & MCSR_LD) {
477 printk("Load Error Report\n");
478 recoverable = 0;
479 }
480
481 if (reason & MCSR_ST) {
482 printk("Store Error Report\n");
483 recoverable = 0;
484 }
485
486 if (reason & MCSR_LDG) {
487 printk("Guarded Load Error Report\n");
488 recoverable = 0;
489 }
490
491 if (reason & MCSR_TLBSYNC)
492 printk("Simultaneous tlbsync operations\n");
493
494 if (reason & MCSR_BSL2_ERR) {
495 printk("Level 2 Cache Error\n");
496 recoverable = 0;
497 }
498
499 if (reason & MCSR_MAV) {
500 u64 addr;
501
502 addr = mfspr(SPRN_MCAR);
503 addr |= (u64)mfspr(SPRN_MCARU) << 32;
504
505 printk("Machine Check %s Address: %#llx\n",
506 reason & MCSR_MEA ? "Effective" : "Physical", addr);
507 }
508
Shaohui Xiecce1f102010-11-18 14:57:32 +0800509silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500510 mtspr(SPRN_MCSR, mcsr);
511 return mfspr(SPRN_MCSR) == 0 && recoverable;
512}
513
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100514int machine_check_e500(struct pt_regs *regs)
515{
Michael Ellerman42bff232017-08-08 16:39:22 +1000516 unsigned long reason = mfspr(SPRN_MCSR);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100517
Shaohui Xiecce1f102010-11-18 14:57:32 +0800518 if (reason & MCSR_BUS_RBERR) {
519 if (fsl_rio_mcheck_exception(regs))
520 return 1;
Hongtao Jia4e0e3432013-04-28 13:20:08 +0800521 if (fsl_pci_mcheck_exception(regs))
522 return 1;
Shaohui Xiecce1f102010-11-18 14:57:32 +0800523 }
524
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000525 printk("Machine check in kernel mode.\n");
526 printk("Caused by (from MCSR=%lx): ", reason);
527
528 if (reason & MCSR_MCP)
529 printk("Machine Check Signal\n");
530 if (reason & MCSR_ICPERR)
531 printk("Instruction Cache Parity Error\n");
532 if (reason & MCSR_DCP_PERR)
533 printk("Data Cache Push Parity Error\n");
534 if (reason & MCSR_DCPERR)
535 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000536 if (reason & MCSR_BUS_IAERR)
537 printk("Bus - Instruction Address Error\n");
538 if (reason & MCSR_BUS_RAERR)
539 printk("Bus - Read Address Error\n");
540 if (reason & MCSR_BUS_WAERR)
541 printk("Bus - Write Address Error\n");
542 if (reason & MCSR_BUS_IBERR)
543 printk("Bus - Instruction Data Error\n");
544 if (reason & MCSR_BUS_RBERR)
545 printk("Bus - Read Data Bus Error\n");
546 if (reason & MCSR_BUS_WBERR)
Wladislav Wiebec1528332014-06-17 15:30:53 +0200547 printk("Bus - Write Data Bus Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000548 if (reason & MCSR_BUS_IPERR)
549 printk("Bus - Instruction Parity Error\n");
550 if (reason & MCSR_BUS_RPERR)
551 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100552
553 return 0;
554}
Kumar Gala4490c062010-10-08 08:32:11 -0500555
556int machine_check_generic(struct pt_regs *regs)
557{
558 return 0;
559}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100560#elif defined(CONFIG_E200)
561int machine_check_e200(struct pt_regs *regs)
562{
Michael Ellerman42bff232017-08-08 16:39:22 +1000563 unsigned long reason = mfspr(SPRN_MCSR);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100564
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000565 printk("Machine check in kernel mode.\n");
566 printk("Caused by (from MCSR=%lx): ", reason);
567
568 if (reason & MCSR_MCP)
569 printk("Machine Check Signal\n");
570 if (reason & MCSR_CP_PERR)
571 printk("Cache Push Parity Error\n");
572 if (reason & MCSR_CPERR)
573 printk("Cache Parity Error\n");
574 if (reason & MCSR_EXCP_ERR)
575 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
576 if (reason & MCSR_BUS_IRERR)
577 printk("Bus - Read Bus Error on instruction fetch\n");
578 if (reason & MCSR_BUS_DRERR)
579 printk("Bus - Read Bus Error on data load\n");
580 if (reason & MCSR_BUS_WRERR)
581 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100582
583 return 0;
584}
Michael Ellerman7f3f8192017-08-08 16:39:23 +1000585#elif defined(CONFIG_PPC32)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100586int machine_check_generic(struct pt_regs *regs)
587{
Michael Ellerman42bff232017-08-08 16:39:22 +1000588 unsigned long reason = regs->msr;
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100589
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000590 printk("Machine check in kernel mode.\n");
591 printk("Caused by (from SRR1=%lx): ", reason);
592 switch (reason & 0x601F0000) {
593 case 0x80000:
594 printk("Machine check signal\n");
595 break;
596 case 0: /* for 601 */
597 case 0x40000:
598 case 0x140000: /* 7450 MSS error and TEA */
599 printk("Transfer error ack signal\n");
600 break;
601 case 0x20000:
602 printk("Data parity error signal\n");
603 break;
604 case 0x10000:
605 printk("Address parity error signal\n");
606 break;
607 case 0x20000000:
608 printk("L1 Data Cache error\n");
609 break;
610 case 0x40000000:
611 printk("L1 Instruction Cache error\n");
612 break;
613 case 0x00100000:
614 printk("L2 data cache parity error\n");
615 break;
616 default:
617 printk("Unknown values in msr\n");
618 }
Olof Johansson75918a42007-09-21 05:11:20 +1000619 return 0;
620}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100621#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000622
623void machine_check_exception(struct pt_regs *regs)
624{
Li Zhongba12eed2013-05-13 16:16:41 +0000625 enum ctx_state prev_state = exception_enter();
Olof Johansson75918a42007-09-21 05:11:20 +1000626 int recover = 0;
627
Nicholas Pigginf886f0f2017-08-01 22:00:51 +1000628 /* 64s accounts the mce in machine_check_early when in HVMODE */
629 if (!IS_ENABLED(CONFIG_PPC_BOOK3S_64) || !cpu_has_feature(CPU_FTR_HVMODE))
630 __this_cpu_inc(irq_stat.mce_exceptions);
Anton Blanchard89713ed2010-01-31 20:34:06 +0000631
Mahesh Salgaonkard93b0ac2017-04-18 22:08:17 +0530632 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
633
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100634 /* See if any machine dependent calls. In theory, we would want
635 * to call the CPU first, and call the ppc_md. one if the CPU
636 * one returns a positive number. However there is existing code
637 * that assumes the board gets a first chance, so let's keep it
638 * that way for now and fix things later. --BenH.
639 */
Olof Johansson75918a42007-09-21 05:11:20 +1000640 if (ppc_md.machine_check_exception)
641 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100642 else if (cur_cpu_spec->machine_check)
643 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000644
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100645 if (recover > 0)
Li Zhongba12eed2013-05-13 16:16:41 +0000646 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000647
Anton Blancharda4435062011-01-11 19:45:31 +0000648 if (debugger_fault_handler(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000649 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000650
651 if (check_io_access(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000652 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000653
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000654 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000655
656 /* Must die if the interrupt is not recoverable */
657 if (!(regs->msr & MSR_RI))
658 panic("Unrecoverable Machine check");
Li Zhongba12eed2013-05-13 16:16:41 +0000659
660bail:
661 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000662}
663
664void SMIException(struct pt_regs *regs)
665{
666 die("System Management Interrupt", regs, SIGABRT);
667}
668
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530669void handle_hmi_exception(struct pt_regs *regs)
670{
671 struct pt_regs *old_regs;
672
673 old_regs = set_irq_regs(regs);
674 irq_enter();
675
676 if (ppc_md.handle_hmi_exception)
677 ppc_md.handle_hmi_exception(regs);
678
679 irq_exit();
680 set_irq_regs(old_regs);
681}
682
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000683void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000684{
Li Zhongba12eed2013-05-13 16:16:41 +0000685 enum ctx_state prev_state = exception_enter();
686
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000687 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
688 regs->nip, regs->msr, regs->trap);
689
690 _exception(SIGTRAP, regs, 0, 0);
Li Zhongba12eed2013-05-13 16:16:41 +0000691
692 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000693}
694
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000695void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000696{
Li Zhongba12eed2013-05-13 16:16:41 +0000697 enum ctx_state prev_state = exception_enter();
698
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000699 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
700 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000701 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000702 if (debugger_iabr_match(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000703 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000704 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000705
706bail:
707 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000708}
709
710void RunModeException(struct pt_regs *regs)
711{
712 _exception(SIGTRAP, regs, 0, 0);
713}
714
Nicholas Piggin03465f82016-09-16 20:48:08 +1000715void single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000716{
Li Zhongba12eed2013-05-13 16:16:41 +0000717 enum ctx_state prev_state = exception_enter();
718
K.Prasad2538c2d2010-06-15 11:35:31 +0530719 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000720
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +0530721 if (kprobe_post_handler(regs))
722 return;
723
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000724 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
725 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000726 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000727 if (debugger_sstep(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000728 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000729
730 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000731
732bail:
733 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000734}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000735NOKPROBE_SYMBOL(single_step_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000736
737/*
738 * After we have successfully emulated an instruction, we have to
739 * check if the instruction was being single-stepped, and if so,
740 * pretend we got a single-step exception. This was pointed out
741 * by Kumar Gala. -- paulus
742 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000743static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000744{
K.Prasad2538c2d2010-06-15 11:35:31 +0530745 if (single_stepping(regs))
746 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000747}
748
Kumar Gala5fad2932007-02-07 01:47:59 -0600749static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000750{
Kumar Gala5fad2932007-02-07 01:47:59 -0600751 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000752
753 /* Invalid operation */
754 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600755 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000756
757 /* Overflow */
758 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600759 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000760
761 /* Underflow */
762 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600763 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000764
765 /* Divide by zero */
766 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600767 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000768
769 /* Inexact result */
770 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600771 ret = FPE_FLTRES;
772
773 return ret;
774}
775
776static void parse_fpe(struct pt_regs *regs)
777{
778 int code = 0;
779
780 flush_fp_to_thread(current);
781
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000782 code = __parse_fpscr(current->thread.fp_state.fpscr);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000783
784 _exception(SIGFPE, regs, code, regs->nip);
785}
786
787/*
788 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000789 * provide the PVR to user applications using the mfspr rd, PVR.
790 * Return non-zero if we can't emulate, or -EFAULT if the associated
791 * memory access caused an access fault. Return zero on success.
792 *
793 * There are a couple of ways to do this, either "decode" the instruction
794 * or directly match lots of bits. In this case, matching lots of
795 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000796 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000797 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000798static int emulate_string_inst(struct pt_regs *regs, u32 instword)
799{
800 u8 rT = (instword >> 21) & 0x1f;
801 u8 rA = (instword >> 16) & 0x1f;
802 u8 NB_RB = (instword >> 11) & 0x1f;
803 u32 num_bytes;
804 unsigned long EA;
805 int pos = 0;
806
807 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000808 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000809 if ((rT == rA) || (rT == NB_RB))
810 return -EINVAL;
811
812 EA = (rA == 0) ? 0 : regs->gpr[rA];
813
Kumar Gala16c57b32009-02-10 20:10:44 +0000814 switch (instword & PPC_INST_STRING_MASK) {
815 case PPC_INST_LSWX:
816 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000817 EA += NB_RB;
818 num_bytes = regs->xer & 0x7f;
819 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000820 case PPC_INST_LSWI:
821 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000822 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
823 break;
824 default:
825 return -EINVAL;
826 }
827
828 while (num_bytes != 0)
829 {
830 u8 val;
831 u32 shift = 8 * (3 - (pos & 0x3));
832
James Yang80aa0fb2013-06-25 11:41:05 -0500833 /* if process is 32-bit, clear upper 32 bits of EA */
834 if ((regs->msr & MSR_64BIT) == 0)
835 EA &= 0xFFFFFFFF;
836
Kumar Gala16c57b32009-02-10 20:10:44 +0000837 switch ((instword & PPC_INST_STRING_MASK)) {
838 case PPC_INST_LSWX:
839 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000840 if (get_user(val, (u8 __user *)EA))
841 return -EFAULT;
842 /* first time updating this reg,
843 * zero it out */
844 if (pos == 0)
845 regs->gpr[rT] = 0;
846 regs->gpr[rT] |= val << shift;
847 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000848 case PPC_INST_STSWI:
849 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000850 val = regs->gpr[rT] >> shift;
851 if (put_user(val, (u8 __user *)EA))
852 return -EFAULT;
853 break;
854 }
855 /* move EA to next address */
856 EA += 1;
857 num_bytes--;
858
859 /* manage our position within the register */
860 if (++pos == 4) {
861 pos = 0;
862 if (++rT == 32)
863 rT = 0;
864 }
865 }
866
867 return 0;
868}
869
Will Schmidtc3412dc2006-08-30 13:11:38 -0500870static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
871{
872 u32 ra,rs;
873 unsigned long tmp;
874
875 ra = (instword >> 16) & 0x1f;
876 rs = (instword >> 21) & 0x1f;
877
878 tmp = regs->gpr[rs];
879 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
880 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
881 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
882 regs->gpr[ra] = tmp;
883
884 return 0;
885}
886
Kumar Galac1469f12007-11-19 21:35:29 -0600887static int emulate_isel(struct pt_regs *regs, u32 instword)
888{
889 u8 rT = (instword >> 21) & 0x1f;
890 u8 rA = (instword >> 16) & 0x1f;
891 u8 rB = (instword >> 11) & 0x1f;
892 u8 BC = (instword >> 6) & 0x1f;
893 u8 bit;
894 unsigned long tmp;
895
896 tmp = (rA == 0) ? 0 : regs->gpr[rA];
897 bit = (regs->ccr >> (31 - BC)) & 0x1;
898
899 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
900
901 return 0;
902}
903
Michael Neuling6ce6c622013-05-26 18:09:39 +0000904#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
905static inline bool tm_abort_check(struct pt_regs *regs, int cause)
906{
907 /* If we're emulating a load/store in an active transaction, we cannot
908 * emulate it as the kernel operates in transaction suspended context.
909 * We need to abort the transaction. This creates a persistent TM
910 * abort so tell the user what caused it with a new code.
911 */
912 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
913 tm_enable();
914 tm_abort(cause);
915 return true;
916 }
917 return false;
918}
919#else
920static inline bool tm_abort_check(struct pt_regs *regs, int reason)
921{
922 return false;
923}
924#endif
925
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000926static int emulate_instruction(struct pt_regs *regs)
927{
928 u32 instword;
929 u32 rd;
930
Anton Blanchard4288e342013-08-07 02:01:47 +1000931 if (!user_mode(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000932 return -EINVAL;
933 CHECK_FULL_REGS(regs);
934
935 if (get_user(instword, (u32 __user *)(regs->nip)))
936 return -EFAULT;
937
938 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000939 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000940 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000941 rd = (instword >> 21) & 0x1f;
942 regs->gpr[rd] = mfspr(SPRN_PVR);
943 return 0;
944 }
945
946 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000947 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000948 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000949 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000950 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000951
952 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000953 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000954 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000955 unsigned long msk = 0xf0000000UL >> shift;
956
Anton Blanchardeecff812009-10-27 18:46:55 +0000957 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000958 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
959 regs->xer &= ~0xf0000000UL;
960 return 0;
961 }
962
963 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000964 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Michael Neuling6ce6c622013-05-26 18:09:39 +0000965 if (tm_abort_check(regs,
966 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
967 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +0000968 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000969 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000970 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000971
Will Schmidtc3412dc2006-08-30 13:11:38 -0500972 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000973 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000974 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500975 return emulate_popcntb_inst(regs, instword);
976 }
977
Kumar Galac1469f12007-11-19 21:35:29 -0600978 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000979 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000980 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600981 return emulate_isel(regs, instword);
982 }
983
James Yang9863c282013-07-03 16:26:47 -0500984 /* Emulate sync instruction variants */
985 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
986 PPC_WARN_EMULATED(sync, regs);
987 asm volatile("sync");
988 return 0;
989 }
990
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000991#ifdef CONFIG_PPC64
992 /* Emulate the mfspr rD, DSCR. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +0000993 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
994 PPC_INST_MFSPR_DSCR_USER) ||
995 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
996 PPC_INST_MFSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000997 cpu_has_feature(CPU_FTR_DSCR)) {
998 PPC_WARN_EMULATED(mfdscr, regs);
999 rd = (instword >> 21) & 0x1f;
1000 regs->gpr[rd] = mfspr(SPRN_DSCR);
1001 return 0;
1002 }
1003 /* Emulate the mtspr DSCR, rD. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001004 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1005 PPC_INST_MTSPR_DSCR_USER) ||
1006 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1007 PPC_INST_MTSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001008 cpu_has_feature(CPU_FTR_DSCR)) {
1009 PPC_WARN_EMULATED(mtdscr, regs);
1010 rd = (instword >> 21) & 0x1f;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001011 current->thread.dscr = regs->gpr[rd];
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001012 current->thread.dscr_inherit = 1;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001013 mtspr(SPRN_DSCR, current->thread.dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001014 return 0;
1015 }
1016#endif
1017
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001018 return -EINVAL;
1019}
1020
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001021int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001022{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001023 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001024}
1025
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001026#ifdef CONFIG_MATH_EMULATION
1027static int emulate_math(struct pt_regs *regs)
1028{
1029 int ret;
1030 extern int do_mathemu(struct pt_regs *regs);
1031
1032 ret = do_mathemu(regs);
1033 if (ret >= 0)
1034 PPC_WARN_EMULATED(math, regs);
1035
1036 switch (ret) {
1037 case 0:
1038 emulate_single_step(regs);
1039 return 0;
1040 case 1: {
1041 int code = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001042 code = __parse_fpscr(current->thread.fp_state.fpscr);
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001043 _exception(SIGFPE, regs, code, regs->nip);
1044 return 0;
1045 }
1046 case -EFAULT:
1047 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1048 return 0;
1049 }
1050
1051 return -1;
1052}
1053#else
1054static inline int emulate_math(struct pt_regs *regs) { return -1; }
1055#endif
1056
Nicholas Piggin03465f82016-09-16 20:48:08 +10001057void program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001058{
Li Zhongba12eed2013-05-13 16:16:41 +00001059 enum ctx_state prev_state = exception_enter();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001060 unsigned int reason = get_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001061
Kim Phillipsaa42c692006-12-08 02:43:30 -06001062 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -06001063 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001064
1065 if (reason & REASON_FP) {
1066 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001067 parse_fpe(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001068 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001069 }
1070 if (reason & REASON_TRAP) {
Balbir Singha4c3f902016-02-18 13:48:01 +11001071 unsigned long bugaddr;
Jason Wesselba797b22010-05-20 21:04:25 -05001072 /* Debugger is first in line to stop recursive faults in
1073 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1074 if (debugger_bpt(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001075 goto bail;
Jason Wesselba797b22010-05-20 21:04:25 -05001076
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301077 if (kprobe_handler(regs))
1078 goto bail;
1079
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001080 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001081 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1082 == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +00001083 goto bail;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001084
Balbir Singha4c3f902016-02-18 13:48:01 +11001085 bugaddr = regs->nip;
1086 /*
1087 * Fixup bugaddr for BUG_ON() in real mode
1088 */
1089 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1090 bugaddr += PAGE_OFFSET;
1091
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001092 if (!(regs->msr & MSR_PR) && /* not user-mode */
Balbir Singha4c3f902016-02-18 13:48:01 +11001093 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001094 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001095 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001096 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001097 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001098 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001099 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001100#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1101 if (reason & REASON_TM) {
1102 /* This is a TM "Bad Thing Exception" program check.
1103 * This occurs when:
1104 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1105 * transition in TM states.
1106 * - A trechkpt is attempted when transactional.
1107 * - A treclaim is attempted when non transactional.
1108 * - A tend is illegally attempted.
1109 * - writing a TM SPR when transactional.
1110 */
1111 if (!user_mode(regs) &&
1112 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1113 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001114 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001115 }
1116 /* If usermode caused this, it's done something illegal and
1117 * gets a SIGILL slap on the wrist. We call it an illegal
1118 * operand to distinguish from the instruction just being bad
1119 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1120 * illegal /placement/ of a valid instruction.
1121 */
1122 if (user_mode(regs)) {
1123 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001124 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001125 } else {
1126 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1127 "at %lx (msr 0x%x)\n", regs->nip, reason);
1128 die("Unrecoverable exception", regs, SIGABRT);
1129 }
1130 }
1131#endif
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001132
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001133 /*
1134 * If we took the program check in the kernel skip down to sending a
1135 * SIGILL. The subsequent cases all relate to emulating instructions
1136 * which we should only do for userspace. We also do not want to enable
1137 * interrupts for kernel faults because that might lead to further
1138 * faults, and loose the context of the original exception.
1139 */
1140 if (!user_mode(regs))
1141 goto sigill;
1142
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001143 /* We restore the interrupt state now */
1144 if (!arch_irq_disabled_regs(regs))
1145 local_irq_enable();
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001146
Kumar Gala04903a32007-02-07 01:13:32 -06001147 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1148 * but there seems to be a hardware bug on the 405GP (RevD)
1149 * that means ESR is sometimes set incorrectly - either to
1150 * ESR_DST (!?) or 0. In the process of chasing this with the
1151 * hardware people - not sure if it can happen on any illegal
1152 * instruction or only on FP instructions, whether there is a
Benjamin Herrenschmidt4e63f8e2013-06-09 17:01:24 +10001153 * pattern to occurrences etc. -dgibson 31/Mar/2003
1154 */
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001155 if (!emulate_math(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001156 goto bail;
Kumar Gala04903a32007-02-07 01:13:32 -06001157
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001158 /* Try to emulate it if we should. */
1159 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001160 switch (emulate_instruction(regs)) {
1161 case 0:
1162 regs->nip += 4;
1163 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001164 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001165 case -EFAULT:
1166 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001167 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001168 }
1169 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001170
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001171sigill:
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001172 if (reason & REASON_PRIVILEGED)
1173 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1174 else
1175 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001176
1177bail:
1178 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001179}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001180NOKPROBE_SYMBOL(program_check_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001181
Paul Mackerrasbf593902013-06-14 20:07:41 +10001182/*
1183 * This occurs when running in hypervisor mode on POWER6 or later
1184 * and an illegal instruction is encountered.
1185 */
Nicholas Piggin03465f82016-09-16 20:48:08 +10001186void emulation_assist_interrupt(struct pt_regs *regs)
Paul Mackerrasbf593902013-06-14 20:07:41 +10001187{
1188 regs->msr |= REASON_ILLEGAL;
1189 program_check_exception(regs);
1190}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001191NOKPROBE_SYMBOL(emulation_assist_interrupt);
Paul Mackerrasbf593902013-06-14 20:07:41 +10001192
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001193void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001194{
Li Zhongba12eed2013-05-13 16:16:41 +00001195 enum ctx_state prev_state = exception_enter();
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001196 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001197
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001198 /* We restore the interrupt state now */
1199 if (!arch_irq_disabled_regs(regs))
1200 local_irq_enable();
1201
Michael Neuling6ce6c622013-05-26 18:09:39 +00001202 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1203 goto bail;
1204
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001205 /* we don't implement logging of alignment exceptions */
1206 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1207 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001208
1209 if (fixed == 1) {
1210 regs->nip += 4; /* skip over emulated instruction */
1211 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001212 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001213 }
1214
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001215 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001216 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001217 sig = SIGSEGV;
1218 code = SEGV_ACCERR;
1219 } else {
1220 sig = SIGBUS;
1221 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001222 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001223 if (user_mode(regs))
1224 _exception(sig, regs, code, regs->dar);
1225 else
1226 bad_page_fault(regs, regs->dar, sig);
Li Zhongba12eed2013-05-13 16:16:41 +00001227
1228bail:
1229 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001230}
1231
Paul Mackerrasf0f558b2016-09-02 21:49:21 +10001232void slb_miss_bad_addr(struct pt_regs *regs)
1233{
1234 enum ctx_state prev_state = exception_enter();
1235
1236 if (user_mode(regs))
1237 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1238 else
1239 bad_page_fault(regs, regs->dar, SIGSEGV);
1240
1241 exception_exit(prev_state);
1242}
1243
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001244void StackOverflow(struct pt_regs *regs)
1245{
1246 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1247 current, regs->gpr[1]);
1248 debugger(regs);
1249 show_regs(regs);
1250 panic("kernel stack overflow");
1251}
1252
1253void nonrecoverable_exception(struct pt_regs *regs)
1254{
1255 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1256 regs->nip, regs->msr);
1257 debugger(regs);
1258 die("nonrecoverable exception", regs, SIGKILL);
1259}
1260
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001261void kernel_fp_unavailable_exception(struct pt_regs *regs)
1262{
Li Zhongba12eed2013-05-13 16:16:41 +00001263 enum ctx_state prev_state = exception_enter();
1264
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001265 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1266 "%lx at %lx\n", regs->trap, regs->nip);
1267 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001268
1269 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001270}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001271
1272void altivec_unavailable_exception(struct pt_regs *regs)
1273{
Li Zhongba12eed2013-05-13 16:16:41 +00001274 enum ctx_state prev_state = exception_enter();
1275
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001276 if (user_mode(regs)) {
1277 /* A user program has executed an altivec instruction,
1278 but this kernel doesn't support altivec. */
1279 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001280 goto bail;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001281 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001282
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001283 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1284 "%lx at %lx\n", regs->trap, regs->nip);
1285 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001286
1287bail:
1288 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001289}
1290
Michael Neulingce48b212008-06-25 14:07:18 +10001291void vsx_unavailable_exception(struct pt_regs *regs)
1292{
1293 if (user_mode(regs)) {
1294 /* A user program has executed an vsx instruction,
1295 but this kernel doesn't support vsx. */
1296 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1297 return;
1298 }
1299
1300 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1301 "%lx at %lx\n", regs->trap, regs->nip);
1302 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1303}
1304
Michael Neuling25176172013-08-09 17:29:29 +10001305#ifdef CONFIG_PPC64
Cyril Bur172f7aa2016-09-14 18:02:15 +10001306static void tm_unavailable(struct pt_regs *regs)
1307{
Cyril Bur5d176f72016-09-14 18:02:16 +10001308#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1309 if (user_mode(regs)) {
1310 current->thread.load_tm++;
1311 regs->msr |= MSR_TM;
1312 tm_enable();
1313 tm_restore_sprs(&current->thread);
1314 return;
1315 }
1316#endif
Cyril Bur172f7aa2016-09-14 18:02:15 +10001317 pr_emerg("Unrecoverable TM Unavailable Exception "
1318 "%lx at %lx\n", regs->trap, regs->nip);
1319 die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1320}
1321
Michael Ellerman021424a2013-06-25 17:47:56 +10001322void facility_unavailable_exception(struct pt_regs *regs)
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001323{
Michael Ellerman021424a2013-06-25 17:47:56 +10001324 static char *facility_strings[] = {
Michael Neuling25176172013-08-09 17:29:29 +10001325 [FSCR_FP_LG] = "FPU",
1326 [FSCR_VECVSX_LG] = "VMX/VSX",
1327 [FSCR_DSCR_LG] = "DSCR",
1328 [FSCR_PM_LG] = "PMU SPRs",
1329 [FSCR_BHRB_LG] = "BHRB",
1330 [FSCR_TM_LG] = "TM",
1331 [FSCR_EBB_LG] = "EBB",
1332 [FSCR_TAR_LG] = "TAR",
Nicholas Piggin794464f2017-04-07 11:27:43 +10001333 [FSCR_MSGP_LG] = "MSGP",
Nicholas Piggin9b7ff0c2017-04-07 11:27:44 +10001334 [FSCR_SCV_LG] = "SCV",
Michael Ellerman021424a2013-06-25 17:47:56 +10001335 };
Michael Neuling25176172013-08-09 17:29:29 +10001336 char *facility = "unknown";
Michael Ellerman021424a2013-06-25 17:47:56 +10001337 u64 value;
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301338 u32 instword, rd;
Michael Neuling25176172013-08-09 17:29:29 +10001339 u8 status;
1340 bool hv;
Michael Ellerman021424a2013-06-25 17:47:56 +10001341
Michael Neuling25176172013-08-09 17:29:29 +10001342 hv = (regs->trap == 0xf80);
1343 if (hv)
Michael Ellermanb14b6262013-06-25 17:47:57 +10001344 value = mfspr(SPRN_HFSCR);
Michael Neuling25176172013-08-09 17:29:29 +10001345 else
1346 value = mfspr(SPRN_FSCR);
1347
1348 status = value >> 56;
1349 if (status == FSCR_DSCR_LG) {
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301350 /*
1351 * User is accessing the DSCR register using the problem
1352 * state only SPR number (0x03) either through a mfspr or
1353 * a mtspr instruction. If it is a write attempt through
1354 * a mtspr, then we set the inherit bit. This also allows
1355 * the user to write or read the register directly in the
1356 * future by setting via the FSCR DSCR bit. But in case it
1357 * is a read DSCR attempt through a mfspr instruction, we
1358 * just emulate the instruction instead. This code path will
1359 * always emulate all the mfspr instructions till the user
Adam Buchbinder446957b2016-02-24 10:51:11 -08001360 * has attempted at least one mtspr instruction. This way it
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301361 * preserves the same behaviour when the user is accessing
1362 * the DSCR through privilege level only SPR number (0x11)
1363 * which is emulated through illegal instruction exception.
1364 * We always leave HFSCR DSCR set.
Michael Neuling25176172013-08-09 17:29:29 +10001365 */
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301366 if (get_user(instword, (u32 __user *)(regs->nip))) {
1367 pr_err("Failed to fetch the user instruction\n");
1368 return;
1369 }
1370
1371 /* Write into DSCR (mtspr 0x03, RS) */
1372 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1373 == PPC_INST_MTSPR_DSCR_USER) {
1374 rd = (instword >> 21) & 0x1f;
1375 current->thread.dscr = regs->gpr[rd];
1376 current->thread.dscr_inherit = 1;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001377 current->thread.fscr |= FSCR_DSCR;
1378 mtspr(SPRN_FSCR, current->thread.fscr);
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301379 }
1380
1381 /* Read from DSCR (mfspr RT, 0x03) */
1382 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1383 == PPC_INST_MFSPR_DSCR_USER) {
1384 if (emulate_instruction(regs)) {
1385 pr_err("DSCR based mfspr emulation failed\n");
1386 return;
1387 }
1388 regs->nip += 4;
1389 emulate_single_step(regs);
1390 }
Michael Neuling25176172013-08-09 17:29:29 +10001391 return;
Michael Ellermanb14b6262013-06-25 17:47:57 +10001392 }
1393
Cyril Bur172f7aa2016-09-14 18:02:15 +10001394 if (status == FSCR_TM_LG) {
1395 /*
1396 * If we're here then the hardware is TM aware because it
1397 * generated an exception with FSRM_TM set.
1398 *
1399 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1400 * told us not to do TM, or the kernel is not built with TM
1401 * support.
1402 *
1403 * If both of those things are true, then userspace can spam the
1404 * console by triggering the printk() below just by continually
1405 * doing tbegin (or any TM instruction). So in that case just
1406 * send the process a SIGILL immediately.
1407 */
1408 if (!cpu_has_feature(CPU_FTR_TM))
1409 goto out;
1410
1411 tm_unavailable(regs);
1412 return;
1413 }
1414
Balbir Singh93c2ec02016-11-30 17:45:09 +11001415 if ((hv || status >= 2) &&
1416 (status < ARRAY_SIZE(facility_strings)) &&
Michael Neuling25176172013-08-09 17:29:29 +10001417 facility_strings[status])
1418 facility = facility_strings[status];
Michael Ellerman021424a2013-06-25 17:47:56 +10001419
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001420 /* We restore the interrupt state now */
1421 if (!arch_irq_disabled_regs(regs))
1422 local_irq_enable();
1423
Balbir Singh93c2ec02016-11-30 17:45:09 +11001424 pr_err_ratelimited("%sFacility '%s' unavailable (%d), exception at 0x%lx, MSR=%lx\n",
1425 hv ? "Hypervisor " : "", facility, status, regs->nip, regs->msr);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001426
Cyril Bur172f7aa2016-09-14 18:02:15 +10001427out:
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001428 if (user_mode(regs)) {
1429 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1430 return;
1431 }
1432
Michael Ellerman021424a2013-06-25 17:47:56 +10001433 die("Unexpected facility unavailable exception", regs, SIGABRT);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001434}
Michael Neuling25176172013-08-09 17:29:29 +10001435#endif
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001436
Michael Neulingf54db642013-02-13 16:21:39 +00001437#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1438
Michael Neulingf54db642013-02-13 16:21:39 +00001439void fp_unavailable_tm(struct pt_regs *regs)
1440{
1441 /* Note: This does not handle any kind of FP laziness. */
1442
1443 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1444 regs->nip, regs->msr);
Michael Neulingf54db642013-02-13 16:21:39 +00001445
1446 /* We can only have got here if the task started using FP after
1447 * beginning the transaction. So, the transactional regs are just a
1448 * copy of the checkpointed ones. But, we still need to recheckpoint
1449 * as we're enabling FP for the process; it will return, abort the
1450 * transaction, and probably retry but now with FP enabled. So the
1451 * checkpointed FP registers need to be loaded.
1452 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001453 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001454 /* Reclaim didn't save out any FPRs to transact_fprs. */
1455
1456 /* Enable FP for the task: */
1457 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1458
1459 /* This loads and recheckpoints the FP registers from
1460 * thread.fpr[]. They will remain in registers after the
1461 * checkpoint so we don't need to reload them after.
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001462 * If VMX is in use, the VRs now hold checkpointed values,
1463 * so we don't want to load the VRs from the thread_struct.
Michael Neulingf54db642013-02-13 16:21:39 +00001464 */
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001465 tm_recheckpoint(&current->thread, MSR_FP);
1466
1467 /* If VMX is in use, get the transactional values back */
1468 if (regs->msr & MSR_VEC) {
Cyril Burdc310662016-09-23 16:18:24 +10001469 msr_check_and_set(MSR_VEC);
1470 load_vr_state(&current->thread.vr_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001471 /* At this point all the VSX state is loaded, so enable it */
1472 regs->msr |= MSR_VSX;
1473 }
Michael Neulingf54db642013-02-13 16:21:39 +00001474}
1475
Michael Neulingf54db642013-02-13 16:21:39 +00001476void altivec_unavailable_tm(struct pt_regs *regs)
1477{
1478 /* See the comments in fp_unavailable_tm(). This function operates
1479 * the same way.
1480 */
1481
1482 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1483 "MSR=%lx\n",
1484 regs->nip, regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001485 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001486 regs->msr |= MSR_VEC;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001487 tm_recheckpoint(&current->thread, MSR_VEC);
Michael Neulingf54db642013-02-13 16:21:39 +00001488 current->thread.used_vr = 1;
Michael Neulingf54db642013-02-13 16:21:39 +00001489
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001490 if (regs->msr & MSR_FP) {
Cyril Burdc310662016-09-23 16:18:24 +10001491 msr_check_and_set(MSR_FP);
1492 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001493 regs->msr |= MSR_VSX;
1494 }
1495}
1496
Michael Neulingf54db642013-02-13 16:21:39 +00001497void vsx_unavailable_tm(struct pt_regs *regs)
1498{
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001499 unsigned long orig_msr = regs->msr;
1500
Michael Neulingf54db642013-02-13 16:21:39 +00001501 /* See the comments in fp_unavailable_tm(). This works similarly,
1502 * though we're loading both FP and VEC registers in here.
1503 *
1504 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1505 * regs. Either way, set MSR_VSX.
1506 */
1507
1508 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1509 "MSR=%lx\n",
1510 regs->nip, regs->msr);
1511
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001512 current->thread.used_vsr = 1;
1513
1514 /* If FP and VMX are already loaded, we have all the state we need */
1515 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1516 regs->msr |= MSR_VSX;
1517 return;
1518 }
1519
Michael Neulingf54db642013-02-13 16:21:39 +00001520 /* This reclaims FP and/or VR regs if they're already enabled */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001521 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001522
1523 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1524 MSR_VSX;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001525
1526 /* This loads & recheckpoints FP and VRs; but we have
1527 * to be sure not to overwrite previously-valid state.
1528 */
1529 tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1530
Cyril Burdc310662016-09-23 16:18:24 +10001531 msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1532
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001533 if (orig_msr & MSR_FP)
Cyril Burdc310662016-09-23 16:18:24 +10001534 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001535 if (orig_msr & MSR_VEC)
Cyril Burdc310662016-09-23 16:18:24 +10001536 load_vr_state(&current->thread.vr_state);
Michael Neulingf54db642013-02-13 16:21:39 +00001537}
Michael Neulingf54db642013-02-13 16:21:39 +00001538#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1539
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001540void performance_monitor_exception(struct pt_regs *regs)
1541{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001542 __this_cpu_inc(irq_stat.pmu_irqs);
Anton Blanchard89713ed2010-01-31 20:34:06 +00001543
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001544 perf_irq(regs);
1545}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001546
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001547#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001548static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1549{
1550 int changed = 0;
1551 /*
1552 * Determine the cause of the debug event, clear the
1553 * event flags and send a trap to the handler. Torez
1554 */
1555 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1556 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1557#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301558 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001559#endif
1560 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1561 5);
1562 changed |= 0x01;
1563 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1564 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1565 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1566 6);
1567 changed |= 0x01;
1568 } else if (debug_status & DBSR_IAC1) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301569 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001570 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1571 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1572 1);
1573 changed |= 0x01;
1574 } else if (debug_status & DBSR_IAC2) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301575 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001576 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1577 2);
1578 changed |= 0x01;
1579 } else if (debug_status & DBSR_IAC3) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301580 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001581 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1582 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1583 3);
1584 changed |= 0x01;
1585 } else if (debug_status & DBSR_IAC4) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301586 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001587 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1588 4);
1589 changed |= 0x01;
1590 }
1591 /*
1592 * At the point this routine was called, the MSR(DE) was turned off.
1593 * Check all other debug flags and see if that bit needs to be turned
1594 * back on or not.
1595 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301596 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
Bharat Bhushan95791982013-06-26 11:12:22 +05301597 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001598 regs->msr |= MSR_DE;
1599 else
1600 /* Make sure the IDM flag is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301601 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001602
1603 if (changed & 0x01)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301604 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001605}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001606
Nicholas Piggin03465f82016-09-16 20:48:08 +10001607void DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001608{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301609 current->thread.debug.dbsr = debug_status;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001610
Roland McGrathec097c82009-05-28 21:26:38 +00001611 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1612 * on server, it stops on the target of the branch. In order to simulate
1613 * the server behaviour, we thus restart right away with a single step
1614 * instead of stopping here when hitting a BT
1615 */
1616 if (debug_status & DBSR_BT) {
1617 regs->msr &= ~MSR_DE;
1618
1619 /* Disable BT */
1620 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1621 /* Clear the BT event */
1622 mtspr(SPRN_DBSR, DBSR_BT);
1623
1624 /* Do the single step trick only when coming from userspace */
1625 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301626 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1627 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Roland McGrathec097c82009-05-28 21:26:38 +00001628 regs->msr |= MSR_DE;
1629 return;
1630 }
1631
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301632 if (kprobe_post_handler(regs))
1633 return;
1634
Roland McGrathec097c82009-05-28 21:26:38 +00001635 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1636 5, SIGTRAP) == NOTIFY_STOP) {
1637 return;
1638 }
1639 if (debugger_sstep(regs))
1640 return;
1641 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001642 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001643
1644 /* Disable instruction completion */
1645 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1646 /* Clear the instruction completion event */
1647 mtspr(SPRN_DBSR, DBSR_IC);
1648
Naveen N. Rao6cc89ba2016-11-21 22:36:41 +05301649 if (kprobe_post_handler(regs))
1650 return;
1651
Kumar Galaf8279622008-06-26 02:01:37 -05001652 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1653 5, SIGTRAP) == NOTIFY_STOP) {
1654 return;
1655 }
1656
1657 if (debugger_sstep(regs))
1658 return;
1659
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001660 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301661 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1662 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1663 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001664 regs->msr |= MSR_DE;
1665 else
1666 /* Make sure the IDM bit is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301667 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001668 }
Kumar Galaf8279622008-06-26 02:01:37 -05001669
1670 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001671 } else
1672 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001673}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001674NOKPROBE_SYMBOL(DebugException);
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001675#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001676
1677#if !defined(CONFIG_TAU_INT)
1678void TAUException(struct pt_regs *regs)
1679{
1680 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1681 regs->nip, regs->msr, regs->trap, print_tainted());
1682}
1683#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001684
1685#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001686void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001687{
1688 int err;
1689
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001690 if (!user_mode(regs)) {
1691 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1692 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001693 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001694 }
1695
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001696 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001697
Anton Blanchardeecff812009-10-27 18:46:55 +00001698 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001699 err = emulate_altivec(regs);
1700 if (err == 0) {
1701 regs->nip += 4; /* skip emulated instruction */
1702 emulate_single_step(regs);
1703 return;
1704 }
1705
1706 if (err == -EFAULT) {
1707 /* got an error reading the instruction */
1708 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1709 } else {
1710 /* didn't recognize the instruction */
1711 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001712 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1713 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001714 current->thread.vr_state.vscr.u[3] |= 0x10000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001715 }
1716}
1717#endif /* CONFIG_ALTIVEC */
1718
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001719#ifdef CONFIG_FSL_BOOKE
1720void CacheLockingException(struct pt_regs *regs, unsigned long address,
1721 unsigned long error_code)
1722{
1723 /* We treat cache locking instructions from the user
1724 * as priv ops, in the future we could try to do
1725 * something smarter
1726 */
1727 if (error_code & (ESR_DLK|ESR_ILK))
1728 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1729 return;
1730}
1731#endif /* CONFIG_FSL_BOOKE */
1732
1733#ifdef CONFIG_SPE
1734void SPEFloatingPointException(struct pt_regs *regs)
1735{
Liu Yu6a800f32008-10-28 11:50:21 +08001736 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001737 unsigned long spefscr;
1738 int fpexc_mode;
1739 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001740 int err;
1741
yu liu685659e2011-06-14 18:34:25 -05001742 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001743
1744 spefscr = current->thread.spefscr;
1745 fpexc_mode = current->thread.fpexc_mode;
1746
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001747 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1748 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001749 }
1750 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1751 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001752 }
1753 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1754 code = FPE_FLTDIV;
1755 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1756 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001757 }
1758 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1759 code = FPE_FLTRES;
1760
Liu Yu6a800f32008-10-28 11:50:21 +08001761 err = do_spe_mathemu(regs);
1762 if (err == 0) {
1763 regs->nip += 4; /* skip emulated instruction */
1764 emulate_single_step(regs);
1765 return;
1766 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001767
Liu Yu6a800f32008-10-28 11:50:21 +08001768 if (err == -EFAULT) {
1769 /* got an error reading the instruction */
1770 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1771 } else if (err == -EINVAL) {
1772 /* didn't recognize the instruction */
1773 printk(KERN_ERR "unrecognized spe instruction "
1774 "in %s at %lx\n", current->comm, regs->nip);
1775 } else {
1776 _exception(SIGFPE, regs, code, regs->nip);
1777 }
1778
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001779 return;
1780}
Liu Yu6a800f32008-10-28 11:50:21 +08001781
1782void SPEFloatingPointRoundException(struct pt_regs *regs)
1783{
1784 extern int speround_handler(struct pt_regs *regs);
1785 int err;
1786
1787 preempt_disable();
1788 if (regs->msr & MSR_SPE)
1789 giveup_spe(current);
1790 preempt_enable();
1791
1792 regs->nip -= 4;
1793 err = speround_handler(regs);
1794 if (err == 0) {
1795 regs->nip += 4; /* skip emulated instruction */
1796 emulate_single_step(regs);
1797 return;
1798 }
1799
1800 if (err == -EFAULT) {
1801 /* got an error reading the instruction */
1802 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1803 } else if (err == -EINVAL) {
1804 /* didn't recognize the instruction */
1805 printk(KERN_ERR "unrecognized spe instruction "
1806 "in %s at %lx\n", current->comm, regs->nip);
1807 } else {
1808 _exception(SIGFPE, regs, 0, regs->nip);
1809 return;
1810 }
1811}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001812#endif
1813
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001814/*
1815 * We enter here if we get an unrecoverable exception, that is, one
1816 * that happened at a point where the RI (recoverable interrupt) bit
1817 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1818 * we therefore lost state by taking this exception.
1819 */
1820void unrecoverable_exception(struct pt_regs *regs)
1821{
1822 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1823 regs->trap, regs->nip);
1824 die("Unrecoverable exception", regs, SIGABRT);
1825}
Naveen N. Rao15770a12017-06-29 23:19:19 +05301826NOKPROBE_SYMBOL(unrecoverable_exception);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001827
Jason Gunthorpe1e18c172012-10-05 08:07:15 +00001828#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001829/*
1830 * Default handler for a Watchdog exception,
1831 * spins until a reboot occurs
1832 */
1833void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1834{
1835 /* Generic WatchdogHandler, implement your own */
1836 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1837 return;
1838}
1839
1840void WatchdogException(struct pt_regs *regs)
1841{
1842 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1843 WatchdogHandler(regs);
1844}
1845#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001846
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001847/*
1848 * We enter here if we discover during exception entry that we are
1849 * running in supervisor mode with a userspace value in the stack pointer.
1850 */
1851void kernel_bad_stack(struct pt_regs *regs)
1852{
1853 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1854 regs->gpr[1], regs->nip);
1855 die("Bad kernel stack pointer", regs, SIGABRT);
1856}
Naveen N. Rao15770a12017-06-29 23:19:19 +05301857NOKPROBE_SYMBOL(kernel_bad_stack);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001858
1859void __init trap_init(void)
1860{
1861}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001862
1863
1864#ifdef CONFIG_PPC_EMULATED_STATS
1865
1866#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1867
1868struct ppc_emulated ppc_emulated = {
1869#ifdef CONFIG_ALTIVEC
1870 WARN_EMULATED_SETUP(altivec),
1871#endif
1872 WARN_EMULATED_SETUP(dcba),
1873 WARN_EMULATED_SETUP(dcbz),
1874 WARN_EMULATED_SETUP(fp_pair),
1875 WARN_EMULATED_SETUP(isel),
1876 WARN_EMULATED_SETUP(mcrxr),
1877 WARN_EMULATED_SETUP(mfpvr),
1878 WARN_EMULATED_SETUP(multiple),
1879 WARN_EMULATED_SETUP(popcntb),
1880 WARN_EMULATED_SETUP(spe),
1881 WARN_EMULATED_SETUP(string),
Scott Wooda3821b22013-10-28 22:07:59 -05001882 WARN_EMULATED_SETUP(sync),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001883 WARN_EMULATED_SETUP(unaligned),
1884#ifdef CONFIG_MATH_EMULATION
1885 WARN_EMULATED_SETUP(math),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001886#endif
1887#ifdef CONFIG_VSX
1888 WARN_EMULATED_SETUP(vsx),
1889#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001890#ifdef CONFIG_PPC64
1891 WARN_EMULATED_SETUP(mfdscr),
1892 WARN_EMULATED_SETUP(mtdscr),
Anton Blanchardf83319d2014-03-28 17:01:23 +11001893 WARN_EMULATED_SETUP(lq_stq),
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001894#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001895};
1896
1897u32 ppc_warn_emulated;
1898
1899void ppc_warn_emulated_print(const char *type)
1900{
Christian Dietrich76462232011-06-04 05:36:54 +00001901 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1902 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001903}
1904
1905static int __init ppc_warn_emulated_init(void)
1906{
1907 struct dentry *dir, *d;
1908 unsigned int i;
1909 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1910
1911 if (!powerpc_debugfs_root)
1912 return -ENODEV;
1913
1914 dir = debugfs_create_dir("emulated_instructions",
1915 powerpc_debugfs_root);
1916 if (!dir)
1917 return -ENOMEM;
1918
1919 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1920 &ppc_warn_emulated);
1921 if (!d)
1922 goto fail;
1923
1924 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1925 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1926 (u32 *)&entries[i].val.counter);
1927 if (!d)
1928 goto fail;
1929 }
1930
1931 return 0;
1932
1933fail:
1934 debugfs_remove_recursive(dir);
1935 return -ENOMEM;
1936}
1937
1938device_initcall(ppc_warn_emulated_init);
1939
1940#endif /* CONFIG_PPC_EMULATED_STATS */