blob: e493062937b00e10b15bc82c1ca84e8ffc73776f [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>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/stddef.h>
23#include <linux/unistd.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100024#include <linux/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025#include <linux/user.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <linux/init.h>
Paul Gortmaker8a39b052016-08-16 10:57:34 -040028#include <linux/extable.h>
29#include <linux/module.h> /* print_modules */
Paul Mackerras8dad3f92005-10-06 13:27:05 +100030#include <linux/prctl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100031#include <linux/delay.h>
32#include <linux/kprobes.h>
Michael Ellermancc532912005-12-04 18:39:43 +110033#include <linux/kexec.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070034#include <linux/backlight.h>
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -080035#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070036#include <linux/kdebug.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000037#include <linux/debugfs.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>
43#include <asm/uaccess.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100044#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100045#include <asm/machdep.h>
46#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100047#include <asm/pmc.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048#include <asm/reg.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#ifdef CONFIG_PMAC_BACKLIGHT
50#include <asm/backlight.h>
51#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100052#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100053#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100054#include <asm/processor.h>
Michael Neuling6ce6c622013-05-26 18:09:39 +000055#include <asm/tm.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100056#endif
David Wilderc0ce7d02006-06-23 15:29:34 -070057#include <asm/kexec.h>
Kumar Gala16c57b32009-02-10 20:10:44 +000058#include <asm/ppc-opcode.h>
Shaohui Xiecce1f102010-11-18 14:57:32 +080059#include <asm/rio.h>
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +000060#include <asm/fadump.h>
David Howellsae3a1972012-03-28 18:30:02 +010061#include <asm/switch_to.h>
Michael Neulingf54db642013-02-13 16:21:39 +000062#include <asm/tm.h>
David Howellsae3a1972012-03-28 18:30:02 +010063#include <asm/debug.h>
Daniel Axtens42f5b4c2016-05-18 11:16:50 +100064#include <asm/asm-prototypes.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053065#include <asm/hmi.h>
Hongtao Jia4e0e3432013-04-28 13:20:08 +080066#include <sysdev/fsl_pci.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100067
Olof Johansson7dbb9222008-01-31 14:34:47 +110068#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000069int (*__debugger)(struct pt_regs *regs) __read_mostly;
70int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
71int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
72int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
73int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
Michael Neuling9422de32012-12-20 14:06:44 +000074int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
Anton Blanchard5be34922010-01-12 00:50:14 +000075int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100076
77EXPORT_SYMBOL(__debugger);
78EXPORT_SYMBOL(__debugger_ipi);
79EXPORT_SYMBOL(__debugger_bpt);
80EXPORT_SYMBOL(__debugger_sstep);
81EXPORT_SYMBOL(__debugger_iabr_match);
Michael Neuling9422de32012-12-20 14:06:44 +000082EXPORT_SYMBOL(__debugger_break_match);
Paul Mackerras14cf11a2005-09-26 16:04:21 +100083EXPORT_SYMBOL(__debugger_fault_handler);
84#endif
85
Michael Neuling8b3c34c2013-02-13 16:21:32 +000086/* Transactional Memory trap debug */
87#ifdef TM_DEBUG_SW
88#define TM_DEBUG(x...) printk(KERN_INFO x)
89#else
90#define TM_DEBUG(x...) do { } while(0)
91#endif
92
Paul Mackerras14cf11a2005-09-26 16:04:21 +100093/*
94 * Trap & Exception support
95 */
96
anton@samba.org6031d9d2007-03-20 20:38:12 -050097#ifdef CONFIG_PMAC_BACKLIGHT
98static void pmac_backlight_unblank(void)
99{
100 mutex_lock(&pmac_backlight_mutex);
101 if (pmac_backlight) {
102 struct backlight_properties *props;
103
104 props = &pmac_backlight->props;
105 props->brightness = props->max_brightness;
106 props->power = FB_BLANK_UNBLANK;
107 backlight_update_status(pmac_backlight);
108 }
109 mutex_unlock(&pmac_backlight_mutex);
110}
111#else
112static inline void pmac_backlight_unblank(void) { }
113#endif
114
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000115static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
116static int die_owner = -1;
117static unsigned int die_nest_count;
118static int die_counter;
119
Nicholas Piggin03465f82016-09-16 20:48:08 +1000120static unsigned long oops_begin(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000121{
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000122 int cpu;
anton@samba.org34c2a142007-03-20 20:38:13 -0500123 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000124
125 if (debugger(regs))
126 return 1;
127
anton@samba.org293e4682007-03-20 20:38:11 -0500128 oops_enter();
129
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000130 /* racy, but better than risking deadlock. */
131 raw_local_irq_save(flags);
132 cpu = smp_processor_id();
133 if (!arch_spin_trylock(&die_lock)) {
134 if (cpu == die_owner)
135 /* nested oops. should stop eventually */;
136 else
137 arch_spin_lock(&die_lock);
anton@samba.org34c2a142007-03-20 20:38:13 -0500138 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000139 die_nest_count++;
140 die_owner = cpu;
141 console_verbose();
142 bust_spinlocks(1);
143 if (machine_is(powermac))
144 pmac_backlight_unblank();
145 return flags;
146}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000147NOKPROBE_SYMBOL(oops_begin);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700148
Nicholas Piggin03465f82016-09-16 20:48:08 +1000149static void oops_end(unsigned long flags, struct pt_regs *regs,
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000150 int signr)
151{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000152 bust_spinlocks(0);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000153 die_owner = -1;
Rusty Russell373d4d02013-01-21 17:17:39 +1030154 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000155 die_nest_count--;
Anton Blanchard58154c82011-11-30 00:23:09 +0000156 oops_exit();
157 printk("\n");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000158 if (!die_nest_count)
159 /* Nest count reaches zero, release the lock. */
160 arch_spin_unlock(&die_lock);
161 raw_local_irq_restore(flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700162
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000163 crash_fadump(regs, "die oops");
164
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000165 /*
166 * A system reset (0x100) is a request to dump, so we always send
167 * it through the crashdump code.
168 */
169 if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700170 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000171
172 /*
173 * We aren't the primary crash CPU. We need to send it
174 * to a holding pattern to avoid it ending up in the panic
175 * code.
176 */
177 crash_kexec_secondary(regs);
178 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000179
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000180 if (!signr)
181 return;
182
Anton Blanchard58154c82011-11-30 00:23:09 +0000183 /*
184 * While our oops output is serialised by a spinlock, output
185 * from panic() called below can race and corrupt it. If we
186 * know we are going to panic, delay for 1 second so we have a
187 * chance to get clean backtraces from all CPUs that are oopsing.
188 */
189 if (in_interrupt() || panic_on_oops || !current->pid ||
190 is_global_init(current)) {
191 mdelay(MSEC_PER_SEC);
192 }
193
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000194 if (in_interrupt())
195 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700196 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700197 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000198 do_exit(signr);
199}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000200NOKPROBE_SYMBOL(oops_end);
Hormscea6a4b2006-07-30 03:03:34 -0700201
Nicholas Piggin03465f82016-09-16 20:48:08 +1000202static int __die(const char *str, struct pt_regs *regs, long err)
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000203{
204 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
205#ifdef CONFIG_PREEMPT
206 printk("PREEMPT ");
207#endif
208#ifdef CONFIG_SMP
209 printk("SMP NR_CPUS=%d ", NR_CPUS);
210#endif
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700211 if (debug_pagealloc_enabled())
212 printk("DEBUG_PAGEALLOC ");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000213#ifdef CONFIG_NUMA
214 printk("NUMA ");
215#endif
216 printk("%s\n", ppc_md.name ? ppc_md.name : "");
217
218 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
219 return 1;
220
221 print_modules();
222 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000223
224 return 0;
225}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000226NOKPROBE_SYMBOL(__die);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000227
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000228void die(const char *str, struct pt_regs *regs, long err)
229{
230 unsigned long flags = oops_begin(regs);
231
232 if (__die(str, regs, err))
233 err = 0;
234 oops_end(flags, regs, err);
235}
236
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{
278 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000279 if (ppc_md.system_reset_exception) {
280 if (ppc_md.system_reset_exception(regs))
281 return;
282 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000283
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000284 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000285
286 /* Must die if the interrupt is not recoverable */
287 if (!(regs->msr & MSR_RI))
288 panic("Unrecoverable System Reset");
289
290 /* What should we do here? We could issue a shutdown or hard reset. */
291}
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530292
Christophe Leroyf3079392016-09-05 08:42:31 +0200293#ifdef CONFIG_PPC64
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530294/*
295 * This function is called in real mode. Strictly no printk's please.
296 *
297 * regs->nip and regs->msr contains srr0 and ssr1.
298 */
299long machine_check_early(struct pt_regs *regs)
300{
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530301 long handled = 0;
302
Christoph Lameter69111ba2014-10-21 15:23:25 -0500303 __this_cpu_inc(irq_stat.mce_exceptions);
Mahesh Salgaonkare6654d52014-06-11 14:18:07 +0530304
Daniel Axtens27ea2c42015-06-15 13:25:19 +1000305 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
306
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530307 if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
308 handled = cur_cpu_spec->machine_check_early(regs);
309 return handled;
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530310}
311
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530312long hmi_exception_realmode(struct pt_regs *regs)
313{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500314 __this_cpu_inc(irq_stat.hmi_exceptions);
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530315
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530316 wait_for_subcore_guest_exit();
317
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530318 if (ppc_md.hmi_exception_early)
319 ppc_md.hmi_exception_early(regs);
320
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530321 wait_for_tb_resync();
322
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530323 return 0;
324}
325
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000326#endif
327
328/*
329 * I/O accesses can cause machine checks on powermacs.
330 * Check if the NIP corresponds to the address of a sync
331 * instruction for which there is an entry in the exception
332 * table.
333 * Note that the 601 only takes a machine check on TEA
334 * (transfer error ack) signal assertion, and does not
335 * set any of the top 16 bits of SRR1.
336 * -- paulus.
337 */
338static inline int check_io_access(struct pt_regs *regs)
339{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100340#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000341 unsigned long msr = regs->msr;
342 const struct exception_table_entry *entry;
343 unsigned int *nip = (unsigned int *)regs->nip;
344
345 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
346 && (entry = search_exception_tables(regs->nip)) != NULL) {
347 /*
348 * Check that it's a sync instruction, or somewhere
349 * in the twi; isync; nop sequence that inb/inw/inl uses.
350 * As the address is in the exception table
351 * we should be able to read the instr there.
352 * For the debug message, we look at the preceding
353 * load or store.
354 */
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200355 if (*nip == PPC_INST_NOP)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000356 nip -= 2;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200357 else if (*nip == PPC_INST_ISYNC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000358 --nip;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200359 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000360 unsigned int rb;
361
362 --nip;
363 rb = (*nip >> 11) & 0x1f;
364 printk(KERN_DEBUG "%s bad port %lx at %p\n",
365 (*nip & 0x100)? "OUT to": "IN from",
366 regs->gpr[rb] - _IO_BASE, nip);
367 regs->msr |= MSR_RI;
368 regs->nip = entry->fixup;
369 return 1;
370 }
371 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100372#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000373 return 0;
374}
375
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000376#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000377/* On 4xx, the reason for the machine check or program exception
378 is in the ESR. */
379#define get_reason(regs) ((regs)->dsisr)
380#ifndef CONFIG_FSL_BOOKE
381#define get_mc_reason(regs) ((regs)->dsisr)
382#else
Scott Woodfe04b112010-04-08 00:38:22 -0500383#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000384#endif
385#define REASON_FP ESR_FP
386#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
387#define REASON_PRIVILEGED ESR_PPR
388#define REASON_TRAP ESR_PTR
389
390/* single-step stuff */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530391#define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
392#define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000393
394#else
395/* On non-4xx, the reason for the machine check or program
396 exception is in the MSR. */
397#define get_reason(regs) ((regs)->msr)
398#define get_mc_reason(regs) ((regs)->msr)
Michael Neuling8b3c34c2013-02-13 16:21:32 +0000399#define REASON_TM 0x200000
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000400#define REASON_FP 0x100000
401#define REASON_ILLEGAL 0x80000
402#define REASON_PRIVILEGED 0x40000
403#define REASON_TRAP 0x20000
404
405#define single_stepping(regs) ((regs)->msr & MSR_SE)
406#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
407#endif
408
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100409#if defined(CONFIG_4xx)
410int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000411{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600412 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000413
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000414 if (reason & ESR_IMCP) {
415 printk("Instruction");
416 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
417 } else
418 printk("Data");
419 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100420
421 return 0;
422}
423
424int machine_check_440A(struct pt_regs *regs)
425{
426 unsigned long reason = get_mc_reason(regs);
427
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000428 printk("Machine check in kernel mode.\n");
429 if (reason & ESR_IMCP){
430 printk("Instruction Synchronous Machine Check exception\n");
431 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
432 }
433 else {
434 u32 mcsr = mfspr(SPRN_MCSR);
435 if (mcsr & MCSR_IB)
436 printk("Instruction Read PLB Error\n");
437 if (mcsr & MCSR_DRB)
438 printk("Data Read PLB Error\n");
439 if (mcsr & MCSR_DWB)
440 printk("Data Write PLB Error\n");
441 if (mcsr & MCSR_TLBP)
442 printk("TLB Parity Error\n");
443 if (mcsr & MCSR_ICP){
444 flush_instruction_cache();
445 printk("I-Cache Parity Error\n");
446 }
447 if (mcsr & MCSR_DCSP)
448 printk("D-Cache Search Parity Error\n");
449 if (mcsr & MCSR_DCFP)
450 printk("D-Cache Flush Parity Error\n");
451 if (mcsr & MCSR_IMPE)
452 printk("Machine Check exception is imprecise\n");
453
454 /* Clear MCSR */
455 mtspr(SPRN_MCSR, mcsr);
456 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100457 return 0;
458}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000459
460int machine_check_47x(struct pt_regs *regs)
461{
462 unsigned long reason = get_mc_reason(regs);
463 u32 mcsr;
464
465 printk(KERN_ERR "Machine check in kernel mode.\n");
466 if (reason & ESR_IMCP) {
467 printk(KERN_ERR
468 "Instruction Synchronous Machine Check exception\n");
469 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
470 return 0;
471 }
472 mcsr = mfspr(SPRN_MCSR);
473 if (mcsr & MCSR_IB)
474 printk(KERN_ERR "Instruction Read PLB Error\n");
475 if (mcsr & MCSR_DRB)
476 printk(KERN_ERR "Data Read PLB Error\n");
477 if (mcsr & MCSR_DWB)
478 printk(KERN_ERR "Data Write PLB Error\n");
479 if (mcsr & MCSR_TLBP)
480 printk(KERN_ERR "TLB Parity Error\n");
481 if (mcsr & MCSR_ICP) {
482 flush_instruction_cache();
483 printk(KERN_ERR "I-Cache Parity Error\n");
484 }
485 if (mcsr & MCSR_DCSP)
486 printk(KERN_ERR "D-Cache Search Parity Error\n");
487 if (mcsr & PPC47x_MCSR_GPR)
488 printk(KERN_ERR "GPR Parity Error\n");
489 if (mcsr & PPC47x_MCSR_FPR)
490 printk(KERN_ERR "FPR Parity Error\n");
491 if (mcsr & PPC47x_MCSR_IPR)
492 printk(KERN_ERR "Machine Check exception is imprecise\n");
493
494 /* Clear MCSR */
495 mtspr(SPRN_MCSR, mcsr);
496
497 return 0;
498}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100499#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500500int machine_check_e500mc(struct pt_regs *regs)
501{
502 unsigned long mcsr = mfspr(SPRN_MCSR);
503 unsigned long reason = mcsr;
504 int recoverable = 1;
505
Scott Wood82a9a482011-06-16 14:09:17 -0500506 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800507 recoverable = fsl_rio_mcheck_exception(regs);
508 if (recoverable == 1)
509 goto silent_out;
510 }
511
Scott Woodfe04b112010-04-08 00:38:22 -0500512 printk("Machine check in kernel mode.\n");
513 printk("Caused by (from MCSR=%lx): ", reason);
514
515 if (reason & MCSR_MCP)
516 printk("Machine Check Signal\n");
517
518 if (reason & MCSR_ICPERR) {
519 printk("Instruction Cache Parity Error\n");
520
521 /*
522 * This is recoverable by invalidating the i-cache.
523 */
524 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
525 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
526 ;
527
528 /*
529 * This will generally be accompanied by an instruction
530 * fetch error report -- only treat MCSR_IF as fatal
531 * if it wasn't due to an L1 parity error.
532 */
533 reason &= ~MCSR_IF;
534 }
535
536 if (reason & MCSR_DCPERR_MC) {
537 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500538
539 /*
540 * In write shadow mode we auto-recover from the error, but it
541 * may still get logged and cause a machine check. We should
542 * only treat the non-write shadow case as non-recoverable.
543 */
544 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
545 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500546 }
547
548 if (reason & MCSR_L2MMU_MHIT) {
549 printk("Hit on multiple TLB entries\n");
550 recoverable = 0;
551 }
552
553 if (reason & MCSR_NMI)
554 printk("Non-maskable interrupt\n");
555
556 if (reason & MCSR_IF) {
557 printk("Instruction Fetch Error Report\n");
558 recoverable = 0;
559 }
560
561 if (reason & MCSR_LD) {
562 printk("Load Error Report\n");
563 recoverable = 0;
564 }
565
566 if (reason & MCSR_ST) {
567 printk("Store Error Report\n");
568 recoverable = 0;
569 }
570
571 if (reason & MCSR_LDG) {
572 printk("Guarded Load Error Report\n");
573 recoverable = 0;
574 }
575
576 if (reason & MCSR_TLBSYNC)
577 printk("Simultaneous tlbsync operations\n");
578
579 if (reason & MCSR_BSL2_ERR) {
580 printk("Level 2 Cache Error\n");
581 recoverable = 0;
582 }
583
584 if (reason & MCSR_MAV) {
585 u64 addr;
586
587 addr = mfspr(SPRN_MCAR);
588 addr |= (u64)mfspr(SPRN_MCARU) << 32;
589
590 printk("Machine Check %s Address: %#llx\n",
591 reason & MCSR_MEA ? "Effective" : "Physical", addr);
592 }
593
Shaohui Xiecce1f102010-11-18 14:57:32 +0800594silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500595 mtspr(SPRN_MCSR, mcsr);
596 return mfspr(SPRN_MCSR) == 0 && recoverable;
597}
598
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100599int machine_check_e500(struct pt_regs *regs)
600{
601 unsigned long reason = get_mc_reason(regs);
602
Shaohui Xiecce1f102010-11-18 14:57:32 +0800603 if (reason & MCSR_BUS_RBERR) {
604 if (fsl_rio_mcheck_exception(regs))
605 return 1;
Hongtao Jia4e0e3432013-04-28 13:20:08 +0800606 if (fsl_pci_mcheck_exception(regs))
607 return 1;
Shaohui Xiecce1f102010-11-18 14:57:32 +0800608 }
609
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000610 printk("Machine check in kernel mode.\n");
611 printk("Caused by (from MCSR=%lx): ", reason);
612
613 if (reason & MCSR_MCP)
614 printk("Machine Check Signal\n");
615 if (reason & MCSR_ICPERR)
616 printk("Instruction Cache Parity Error\n");
617 if (reason & MCSR_DCP_PERR)
618 printk("Data Cache Push Parity Error\n");
619 if (reason & MCSR_DCPERR)
620 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000621 if (reason & MCSR_BUS_IAERR)
622 printk("Bus - Instruction Address Error\n");
623 if (reason & MCSR_BUS_RAERR)
624 printk("Bus - Read Address Error\n");
625 if (reason & MCSR_BUS_WAERR)
626 printk("Bus - Write Address Error\n");
627 if (reason & MCSR_BUS_IBERR)
628 printk("Bus - Instruction Data Error\n");
629 if (reason & MCSR_BUS_RBERR)
630 printk("Bus - Read Data Bus Error\n");
631 if (reason & MCSR_BUS_WBERR)
Wladislav Wiebec1528332014-06-17 15:30:53 +0200632 printk("Bus - Write Data Bus Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000633 if (reason & MCSR_BUS_IPERR)
634 printk("Bus - Instruction Parity Error\n");
635 if (reason & MCSR_BUS_RPERR)
636 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100637
638 return 0;
639}
Kumar Gala4490c062010-10-08 08:32:11 -0500640
641int machine_check_generic(struct pt_regs *regs)
642{
643 return 0;
644}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100645#elif defined(CONFIG_E200)
646int machine_check_e200(struct pt_regs *regs)
647{
648 unsigned long reason = get_mc_reason(regs);
649
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000650 printk("Machine check in kernel mode.\n");
651 printk("Caused by (from MCSR=%lx): ", reason);
652
653 if (reason & MCSR_MCP)
654 printk("Machine Check Signal\n");
655 if (reason & MCSR_CP_PERR)
656 printk("Cache Push Parity Error\n");
657 if (reason & MCSR_CPERR)
658 printk("Cache Parity Error\n");
659 if (reason & MCSR_EXCP_ERR)
660 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
661 if (reason & MCSR_BUS_IRERR)
662 printk("Bus - Read Bus Error on instruction fetch\n");
663 if (reason & MCSR_BUS_DRERR)
664 printk("Bus - Read Bus Error on data load\n");
665 if (reason & MCSR_BUS_WRERR)
666 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100667
668 return 0;
669}
Christophe Leroye627f8d2016-09-16 10:23:11 +0200670#elif defined(CONFIG_PPC_8xx)
671int machine_check_8xx(struct pt_regs *regs)
672{
673 unsigned long reason = get_mc_reason(regs);
674
675 pr_err("Machine check in kernel mode.\n");
676 pr_err("Caused by (from SRR1=%lx): ", reason);
677 if (reason & 0x40000000)
678 pr_err("Fetch error at address %lx\n", regs->nip);
679 else
680 pr_err("Data access error at address %lx\n", regs->dar);
681
682#ifdef CONFIG_PCI
683 /* the qspan pci read routines can cause machine checks -- Cort
684 *
685 * yuck !!! that totally needs to go away ! There are better ways
686 * to deal with that than having a wart in the mcheck handler.
687 * -- BenH
688 */
689 bad_page_fault(regs, regs->dar, SIGBUS);
690 return 1;
691#else
692 return 0;
693#endif
694}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100695#else
696int machine_check_generic(struct pt_regs *regs)
697{
698 unsigned long reason = get_mc_reason(regs);
699
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000700 printk("Machine check in kernel mode.\n");
701 printk("Caused by (from SRR1=%lx): ", reason);
702 switch (reason & 0x601F0000) {
703 case 0x80000:
704 printk("Machine check signal\n");
705 break;
706 case 0: /* for 601 */
707 case 0x40000:
708 case 0x140000: /* 7450 MSS error and TEA */
709 printk("Transfer error ack signal\n");
710 break;
711 case 0x20000:
712 printk("Data parity error signal\n");
713 break;
714 case 0x10000:
715 printk("Address parity error signal\n");
716 break;
717 case 0x20000000:
718 printk("L1 Data Cache error\n");
719 break;
720 case 0x40000000:
721 printk("L1 Instruction Cache error\n");
722 break;
723 case 0x00100000:
724 printk("L2 data cache parity error\n");
725 break;
726 default:
727 printk("Unknown values in msr\n");
728 }
Olof Johansson75918a42007-09-21 05:11:20 +1000729 return 0;
730}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100731#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000732
733void machine_check_exception(struct pt_regs *regs)
734{
Li Zhongba12eed2013-05-13 16:16:41 +0000735 enum ctx_state prev_state = exception_enter();
Olof Johansson75918a42007-09-21 05:11:20 +1000736 int recover = 0;
737
Christoph Lameter69111ba2014-10-21 15:23:25 -0500738 __this_cpu_inc(irq_stat.mce_exceptions);
Anton Blanchard89713ed2010-01-31 20:34:06 +0000739
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100740 /* See if any machine dependent calls. In theory, we would want
741 * to call the CPU first, and call the ppc_md. one if the CPU
742 * one returns a positive number. However there is existing code
743 * that assumes the board gets a first chance, so let's keep it
744 * that way for now and fix things later. --BenH.
745 */
Olof Johansson75918a42007-09-21 05:11:20 +1000746 if (ppc_md.machine_check_exception)
747 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100748 else if (cur_cpu_spec->machine_check)
749 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000750
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100751 if (recover > 0)
Li Zhongba12eed2013-05-13 16:16:41 +0000752 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000753
Anton Blancharda4435062011-01-11 19:45:31 +0000754 if (debugger_fault_handler(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000755 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000756
757 if (check_io_access(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000758 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000759
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000760 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000761
762 /* Must die if the interrupt is not recoverable */
763 if (!(regs->msr & MSR_RI))
764 panic("Unrecoverable Machine check");
Li Zhongba12eed2013-05-13 16:16:41 +0000765
766bail:
767 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000768}
769
770void SMIException(struct pt_regs *regs)
771{
772 die("System Management Interrupt", regs, SIGABRT);
773}
774
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530775void handle_hmi_exception(struct pt_regs *regs)
776{
777 struct pt_regs *old_regs;
778
779 old_regs = set_irq_regs(regs);
780 irq_enter();
781
782 if (ppc_md.handle_hmi_exception)
783 ppc_md.handle_hmi_exception(regs);
784
785 irq_exit();
786 set_irq_regs(old_regs);
787}
788
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000789void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790{
Li Zhongba12eed2013-05-13 16:16:41 +0000791 enum ctx_state prev_state = exception_enter();
792
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000793 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
794 regs->nip, regs->msr, regs->trap);
795
796 _exception(SIGTRAP, regs, 0, 0);
Li Zhongba12eed2013-05-13 16:16:41 +0000797
798 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000799}
800
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000801void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000802{
Li Zhongba12eed2013-05-13 16:16:41 +0000803 enum ctx_state prev_state = exception_enter();
804
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000805 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
806 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000807 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000808 if (debugger_iabr_match(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000809 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000810 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000811
812bail:
813 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000814}
815
816void RunModeException(struct pt_regs *regs)
817{
818 _exception(SIGTRAP, regs, 0, 0);
819}
820
Nicholas Piggin03465f82016-09-16 20:48:08 +1000821void single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000822{
Li Zhongba12eed2013-05-13 16:16:41 +0000823 enum ctx_state prev_state = exception_enter();
824
K.Prasad2538c2d2010-06-15 11:35:31 +0530825 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000826
827 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
828 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000829 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000830 if (debugger_sstep(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000831 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000832
833 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000834
835bail:
836 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000837}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000838NOKPROBE_SYMBOL(single_step_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000839
840/*
841 * After we have successfully emulated an instruction, we have to
842 * check if the instruction was being single-stepped, and if so,
843 * pretend we got a single-step exception. This was pointed out
844 * by Kumar Gala. -- paulus
845 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000846static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000847{
K.Prasad2538c2d2010-06-15 11:35:31 +0530848 if (single_stepping(regs))
849 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000850}
851
Kumar Gala5fad2932007-02-07 01:47:59 -0600852static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000853{
Kumar Gala5fad2932007-02-07 01:47:59 -0600854 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000855
856 /* Invalid operation */
857 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600858 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000859
860 /* Overflow */
861 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600862 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000863
864 /* Underflow */
865 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600866 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000867
868 /* Divide by zero */
869 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600870 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000871
872 /* Inexact result */
873 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600874 ret = FPE_FLTRES;
875
876 return ret;
877}
878
879static void parse_fpe(struct pt_regs *regs)
880{
881 int code = 0;
882
883 flush_fp_to_thread(current);
884
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000885 code = __parse_fpscr(current->thread.fp_state.fpscr);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000886
887 _exception(SIGFPE, regs, code, regs->nip);
888}
889
890/*
891 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000892 * provide the PVR to user applications using the mfspr rd, PVR.
893 * Return non-zero if we can't emulate, or -EFAULT if the associated
894 * memory access caused an access fault. Return zero on success.
895 *
896 * There are a couple of ways to do this, either "decode" the instruction
897 * or directly match lots of bits. In this case, matching lots of
898 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000899 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000900 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000901static int emulate_string_inst(struct pt_regs *regs, u32 instword)
902{
903 u8 rT = (instword >> 21) & 0x1f;
904 u8 rA = (instword >> 16) & 0x1f;
905 u8 NB_RB = (instword >> 11) & 0x1f;
906 u32 num_bytes;
907 unsigned long EA;
908 int pos = 0;
909
910 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000911 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000912 if ((rT == rA) || (rT == NB_RB))
913 return -EINVAL;
914
915 EA = (rA == 0) ? 0 : regs->gpr[rA];
916
Kumar Gala16c57b32009-02-10 20:10:44 +0000917 switch (instword & PPC_INST_STRING_MASK) {
918 case PPC_INST_LSWX:
919 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000920 EA += NB_RB;
921 num_bytes = regs->xer & 0x7f;
922 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000923 case PPC_INST_LSWI:
924 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000925 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
926 break;
927 default:
928 return -EINVAL;
929 }
930
931 while (num_bytes != 0)
932 {
933 u8 val;
934 u32 shift = 8 * (3 - (pos & 0x3));
935
James Yang80aa0fb2013-06-25 11:41:05 -0500936 /* if process is 32-bit, clear upper 32 bits of EA */
937 if ((regs->msr & MSR_64BIT) == 0)
938 EA &= 0xFFFFFFFF;
939
Kumar Gala16c57b32009-02-10 20:10:44 +0000940 switch ((instword & PPC_INST_STRING_MASK)) {
941 case PPC_INST_LSWX:
942 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000943 if (get_user(val, (u8 __user *)EA))
944 return -EFAULT;
945 /* first time updating this reg,
946 * zero it out */
947 if (pos == 0)
948 regs->gpr[rT] = 0;
949 regs->gpr[rT] |= val << shift;
950 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000951 case PPC_INST_STSWI:
952 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000953 val = regs->gpr[rT] >> shift;
954 if (put_user(val, (u8 __user *)EA))
955 return -EFAULT;
956 break;
957 }
958 /* move EA to next address */
959 EA += 1;
960 num_bytes--;
961
962 /* manage our position within the register */
963 if (++pos == 4) {
964 pos = 0;
965 if (++rT == 32)
966 rT = 0;
967 }
968 }
969
970 return 0;
971}
972
Will Schmidtc3412dc2006-08-30 13:11:38 -0500973static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
974{
975 u32 ra,rs;
976 unsigned long tmp;
977
978 ra = (instword >> 16) & 0x1f;
979 rs = (instword >> 21) & 0x1f;
980
981 tmp = regs->gpr[rs];
982 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
983 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
984 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
985 regs->gpr[ra] = tmp;
986
987 return 0;
988}
989
Kumar Galac1469f12007-11-19 21:35:29 -0600990static int emulate_isel(struct pt_regs *regs, u32 instword)
991{
992 u8 rT = (instword >> 21) & 0x1f;
993 u8 rA = (instword >> 16) & 0x1f;
994 u8 rB = (instword >> 11) & 0x1f;
995 u8 BC = (instword >> 6) & 0x1f;
996 u8 bit;
997 unsigned long tmp;
998
999 tmp = (rA == 0) ? 0 : regs->gpr[rA];
1000 bit = (regs->ccr >> (31 - BC)) & 0x1;
1001
1002 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1003
1004 return 0;
1005}
1006
Michael Neuling6ce6c622013-05-26 18:09:39 +00001007#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1008static inline bool tm_abort_check(struct pt_regs *regs, int cause)
1009{
1010 /* If we're emulating a load/store in an active transaction, we cannot
1011 * emulate it as the kernel operates in transaction suspended context.
1012 * We need to abort the transaction. This creates a persistent TM
1013 * abort so tell the user what caused it with a new code.
1014 */
1015 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
1016 tm_enable();
1017 tm_abort(cause);
1018 return true;
1019 }
1020 return false;
1021}
1022#else
1023static inline bool tm_abort_check(struct pt_regs *regs, int reason)
1024{
1025 return false;
1026}
1027#endif
1028
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001029static int emulate_instruction(struct pt_regs *regs)
1030{
1031 u32 instword;
1032 u32 rd;
1033
Anton Blanchard4288e342013-08-07 02:01:47 +10001034 if (!user_mode(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001035 return -EINVAL;
1036 CHECK_FULL_REGS(regs);
1037
1038 if (get_user(instword, (u32 __user *)(regs->nip)))
1039 return -EFAULT;
1040
1041 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001042 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001043 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001044 rd = (instword >> 21) & 0x1f;
1045 regs->gpr[rd] = mfspr(SPRN_PVR);
1046 return 0;
1047 }
1048
1049 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001050 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001051 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001052 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001053 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001054
1055 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001056 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +10001057 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001058 unsigned long msk = 0xf0000000UL >> shift;
1059
Anton Blanchardeecff812009-10-27 18:46:55 +00001060 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001061 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
1062 regs->xer &= ~0xf0000000UL;
1063 return 0;
1064 }
1065
1066 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001067 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Michael Neuling6ce6c622013-05-26 18:09:39 +00001068 if (tm_abort_check(regs,
1069 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
1070 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +00001071 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001072 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001073 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001074
Will Schmidtc3412dc2006-08-30 13:11:38 -05001075 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001076 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001077 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -05001078 return emulate_popcntb_inst(regs, instword);
1079 }
1080
Kumar Galac1469f12007-11-19 21:35:29 -06001081 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +00001082 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001083 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -06001084 return emulate_isel(regs, instword);
1085 }
1086
James Yang9863c282013-07-03 16:26:47 -05001087 /* Emulate sync instruction variants */
1088 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1089 PPC_WARN_EMULATED(sync, regs);
1090 asm volatile("sync");
1091 return 0;
1092 }
1093
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001094#ifdef CONFIG_PPC64
1095 /* Emulate the mfspr rD, DSCR. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001096 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1097 PPC_INST_MFSPR_DSCR_USER) ||
1098 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1099 PPC_INST_MFSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001100 cpu_has_feature(CPU_FTR_DSCR)) {
1101 PPC_WARN_EMULATED(mfdscr, regs);
1102 rd = (instword >> 21) & 0x1f;
1103 regs->gpr[rd] = mfspr(SPRN_DSCR);
1104 return 0;
1105 }
1106 /* Emulate the mtspr DSCR, rD. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001107 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1108 PPC_INST_MTSPR_DSCR_USER) ||
1109 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1110 PPC_INST_MTSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001111 cpu_has_feature(CPU_FTR_DSCR)) {
1112 PPC_WARN_EMULATED(mtdscr, regs);
1113 rd = (instword >> 21) & 0x1f;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001114 current->thread.dscr = regs->gpr[rd];
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001115 current->thread.dscr_inherit = 1;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001116 mtspr(SPRN_DSCR, current->thread.dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001117 return 0;
1118 }
1119#endif
1120
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001121 return -EINVAL;
1122}
1123
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001124int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001125{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001126 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001127}
1128
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001129#ifdef CONFIG_MATH_EMULATION
1130static int emulate_math(struct pt_regs *regs)
1131{
1132 int ret;
1133 extern int do_mathemu(struct pt_regs *regs);
1134
1135 ret = do_mathemu(regs);
1136 if (ret >= 0)
1137 PPC_WARN_EMULATED(math, regs);
1138
1139 switch (ret) {
1140 case 0:
1141 emulate_single_step(regs);
1142 return 0;
1143 case 1: {
1144 int code = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001145 code = __parse_fpscr(current->thread.fp_state.fpscr);
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001146 _exception(SIGFPE, regs, code, regs->nip);
1147 return 0;
1148 }
1149 case -EFAULT:
1150 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1151 return 0;
1152 }
1153
1154 return -1;
1155}
1156#else
1157static inline int emulate_math(struct pt_regs *regs) { return -1; }
1158#endif
1159
Nicholas Piggin03465f82016-09-16 20:48:08 +10001160void program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001161{
Li Zhongba12eed2013-05-13 16:16:41 +00001162 enum ctx_state prev_state = exception_enter();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001163 unsigned int reason = get_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001164
Kim Phillipsaa42c692006-12-08 02:43:30 -06001165 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -06001166 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001167
1168 if (reason & REASON_FP) {
1169 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001170 parse_fpe(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001171 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001172 }
1173 if (reason & REASON_TRAP) {
Balbir Singha4c3f902016-02-18 13:48:01 +11001174 unsigned long bugaddr;
Jason Wesselba797b22010-05-20 21:04:25 -05001175 /* Debugger is first in line to stop recursive faults in
1176 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1177 if (debugger_bpt(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001178 goto bail;
Jason Wesselba797b22010-05-20 21:04:25 -05001179
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001180 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001181 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1182 == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +00001183 goto bail;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001184
Balbir Singha4c3f902016-02-18 13:48:01 +11001185 bugaddr = regs->nip;
1186 /*
1187 * Fixup bugaddr for BUG_ON() in real mode
1188 */
1189 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1190 bugaddr += PAGE_OFFSET;
1191
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001192 if (!(regs->msr & MSR_PR) && /* not user-mode */
Balbir Singha4c3f902016-02-18 13:48:01 +11001193 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001194 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001195 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001196 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001197 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001198 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001199 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001200#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1201 if (reason & REASON_TM) {
1202 /* This is a TM "Bad Thing Exception" program check.
1203 * This occurs when:
1204 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1205 * transition in TM states.
1206 * - A trechkpt is attempted when transactional.
1207 * - A treclaim is attempted when non transactional.
1208 * - A tend is illegally attempted.
1209 * - writing a TM SPR when transactional.
1210 */
1211 if (!user_mode(regs) &&
1212 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1213 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001214 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001215 }
1216 /* If usermode caused this, it's done something illegal and
1217 * gets a SIGILL slap on the wrist. We call it an illegal
1218 * operand to distinguish from the instruction just being bad
1219 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1220 * illegal /placement/ of a valid instruction.
1221 */
1222 if (user_mode(regs)) {
1223 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001224 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001225 } else {
1226 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1227 "at %lx (msr 0x%x)\n", regs->nip, reason);
1228 die("Unrecoverable exception", regs, SIGABRT);
1229 }
1230 }
1231#endif
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001232
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001233 /*
1234 * If we took the program check in the kernel skip down to sending a
1235 * SIGILL. The subsequent cases all relate to emulating instructions
1236 * which we should only do for userspace. We also do not want to enable
1237 * interrupts for kernel faults because that might lead to further
1238 * faults, and loose the context of the original exception.
1239 */
1240 if (!user_mode(regs))
1241 goto sigill;
1242
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001243 /* We restore the interrupt state now */
1244 if (!arch_irq_disabled_regs(regs))
1245 local_irq_enable();
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001246
Kumar Gala04903a32007-02-07 01:13:32 -06001247 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1248 * but there seems to be a hardware bug on the 405GP (RevD)
1249 * that means ESR is sometimes set incorrectly - either to
1250 * ESR_DST (!?) or 0. In the process of chasing this with the
1251 * hardware people - not sure if it can happen on any illegal
1252 * instruction or only on FP instructions, whether there is a
Benjamin Herrenschmidt4e63f8e2013-06-09 17:01:24 +10001253 * pattern to occurrences etc. -dgibson 31/Mar/2003
1254 */
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001255 if (!emulate_math(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001256 goto bail;
Kumar Gala04903a32007-02-07 01:13:32 -06001257
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001258 /* Try to emulate it if we should. */
1259 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001260 switch (emulate_instruction(regs)) {
1261 case 0:
1262 regs->nip += 4;
1263 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001264 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001265 case -EFAULT:
1266 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001267 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001268 }
1269 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001270
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001271sigill:
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001272 if (reason & REASON_PRIVILEGED)
1273 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1274 else
1275 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001276
1277bail:
1278 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001279}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001280NOKPROBE_SYMBOL(program_check_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001281
Paul Mackerrasbf593902013-06-14 20:07:41 +10001282/*
1283 * This occurs when running in hypervisor mode on POWER6 or later
1284 * and an illegal instruction is encountered.
1285 */
Nicholas Piggin03465f82016-09-16 20:48:08 +10001286void emulation_assist_interrupt(struct pt_regs *regs)
Paul Mackerrasbf593902013-06-14 20:07:41 +10001287{
1288 regs->msr |= REASON_ILLEGAL;
1289 program_check_exception(regs);
1290}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001291NOKPROBE_SYMBOL(emulation_assist_interrupt);
Paul Mackerrasbf593902013-06-14 20:07:41 +10001292
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001293void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001294{
Li Zhongba12eed2013-05-13 16:16:41 +00001295 enum ctx_state prev_state = exception_enter();
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001296 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001297
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001298 /* We restore the interrupt state now */
1299 if (!arch_irq_disabled_regs(regs))
1300 local_irq_enable();
1301
Michael Neuling6ce6c622013-05-26 18:09:39 +00001302 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1303 goto bail;
1304
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001305 /* we don't implement logging of alignment exceptions */
1306 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1307 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001308
1309 if (fixed == 1) {
1310 regs->nip += 4; /* skip over emulated instruction */
1311 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001312 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001313 }
1314
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001315 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001316 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001317 sig = SIGSEGV;
1318 code = SEGV_ACCERR;
1319 } else {
1320 sig = SIGBUS;
1321 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001322 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001323 if (user_mode(regs))
1324 _exception(sig, regs, code, regs->dar);
1325 else
1326 bad_page_fault(regs, regs->dar, sig);
Li Zhongba12eed2013-05-13 16:16:41 +00001327
1328bail:
1329 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001330}
1331
Paul Mackerrasf0f558b2016-09-02 21:49:21 +10001332void slb_miss_bad_addr(struct pt_regs *regs)
1333{
1334 enum ctx_state prev_state = exception_enter();
1335
1336 if (user_mode(regs))
1337 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1338 else
1339 bad_page_fault(regs, regs->dar, SIGSEGV);
1340
1341 exception_exit(prev_state);
1342}
1343
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001344void StackOverflow(struct pt_regs *regs)
1345{
1346 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1347 current, regs->gpr[1]);
1348 debugger(regs);
1349 show_regs(regs);
1350 panic("kernel stack overflow");
1351}
1352
1353void nonrecoverable_exception(struct pt_regs *regs)
1354{
1355 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1356 regs->nip, regs->msr);
1357 debugger(regs);
1358 die("nonrecoverable exception", regs, SIGKILL);
1359}
1360
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001361void kernel_fp_unavailable_exception(struct pt_regs *regs)
1362{
Li Zhongba12eed2013-05-13 16:16:41 +00001363 enum ctx_state prev_state = exception_enter();
1364
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001365 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1366 "%lx at %lx\n", regs->trap, regs->nip);
1367 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001368
1369 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001370}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001371
1372void altivec_unavailable_exception(struct pt_regs *regs)
1373{
Li Zhongba12eed2013-05-13 16:16:41 +00001374 enum ctx_state prev_state = exception_enter();
1375
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001376 if (user_mode(regs)) {
1377 /* A user program has executed an altivec instruction,
1378 but this kernel doesn't support altivec. */
1379 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001380 goto bail;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001381 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001382
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001383 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1384 "%lx at %lx\n", regs->trap, regs->nip);
1385 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001386
1387bail:
1388 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001389}
1390
Michael Neulingce48b212008-06-25 14:07:18 +10001391void vsx_unavailable_exception(struct pt_regs *regs)
1392{
1393 if (user_mode(regs)) {
1394 /* A user program has executed an vsx instruction,
1395 but this kernel doesn't support vsx. */
1396 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1397 return;
1398 }
1399
1400 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1401 "%lx at %lx\n", regs->trap, regs->nip);
1402 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1403}
1404
Michael Neuling25176172013-08-09 17:29:29 +10001405#ifdef CONFIG_PPC64
Michael Ellerman021424a2013-06-25 17:47:56 +10001406void facility_unavailable_exception(struct pt_regs *regs)
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001407{
Michael Ellerman021424a2013-06-25 17:47:56 +10001408 static char *facility_strings[] = {
Michael Neuling25176172013-08-09 17:29:29 +10001409 [FSCR_FP_LG] = "FPU",
1410 [FSCR_VECVSX_LG] = "VMX/VSX",
1411 [FSCR_DSCR_LG] = "DSCR",
1412 [FSCR_PM_LG] = "PMU SPRs",
1413 [FSCR_BHRB_LG] = "BHRB",
1414 [FSCR_TM_LG] = "TM",
1415 [FSCR_EBB_LG] = "EBB",
1416 [FSCR_TAR_LG] = "TAR",
Jack Millerbd3ea312016-06-09 12:31:09 +10001417 [FSCR_LM_LG] = "LM",
Michael Ellerman021424a2013-06-25 17:47:56 +10001418 };
Michael Neuling25176172013-08-09 17:29:29 +10001419 char *facility = "unknown";
Michael Ellerman021424a2013-06-25 17:47:56 +10001420 u64 value;
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301421 u32 instword, rd;
Michael Neuling25176172013-08-09 17:29:29 +10001422 u8 status;
1423 bool hv;
Michael Ellerman021424a2013-06-25 17:47:56 +10001424
Michael Neuling25176172013-08-09 17:29:29 +10001425 hv = (regs->trap == 0xf80);
1426 if (hv)
Michael Ellermanb14b6262013-06-25 17:47:57 +10001427 value = mfspr(SPRN_HFSCR);
Michael Neuling25176172013-08-09 17:29:29 +10001428 else
1429 value = mfspr(SPRN_FSCR);
1430
1431 status = value >> 56;
1432 if (status == FSCR_DSCR_LG) {
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301433 /*
1434 * User is accessing the DSCR register using the problem
1435 * state only SPR number (0x03) either through a mfspr or
1436 * a mtspr instruction. If it is a write attempt through
1437 * a mtspr, then we set the inherit bit. This also allows
1438 * the user to write or read the register directly in the
1439 * future by setting via the FSCR DSCR bit. But in case it
1440 * is a read DSCR attempt through a mfspr instruction, we
1441 * just emulate the instruction instead. This code path will
1442 * always emulate all the mfspr instructions till the user
Adam Buchbinder446957b2016-02-24 10:51:11 -08001443 * has attempted at least one mtspr instruction. This way it
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301444 * preserves the same behaviour when the user is accessing
1445 * the DSCR through privilege level only SPR number (0x11)
1446 * which is emulated through illegal instruction exception.
1447 * We always leave HFSCR DSCR set.
Michael Neuling25176172013-08-09 17:29:29 +10001448 */
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301449 if (get_user(instword, (u32 __user *)(regs->nip))) {
1450 pr_err("Failed to fetch the user instruction\n");
1451 return;
1452 }
1453
1454 /* Write into DSCR (mtspr 0x03, RS) */
1455 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1456 == PPC_INST_MTSPR_DSCR_USER) {
1457 rd = (instword >> 21) & 0x1f;
1458 current->thread.dscr = regs->gpr[rd];
1459 current->thread.dscr_inherit = 1;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001460 current->thread.fscr |= FSCR_DSCR;
1461 mtspr(SPRN_FSCR, current->thread.fscr);
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301462 }
1463
1464 /* Read from DSCR (mfspr RT, 0x03) */
1465 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1466 == PPC_INST_MFSPR_DSCR_USER) {
1467 if (emulate_instruction(regs)) {
1468 pr_err("DSCR based mfspr emulation failed\n");
1469 return;
1470 }
1471 regs->nip += 4;
1472 emulate_single_step(regs);
1473 }
Michael Neuling25176172013-08-09 17:29:29 +10001474 return;
Jack Millerbd3ea312016-06-09 12:31:09 +10001475 } else if ((status == FSCR_LM_LG) && cpu_has_feature(CPU_FTR_ARCH_300)) {
1476 /*
1477 * This process has touched LM, so turn it on forever
1478 * for this process
1479 */
1480 current->thread.fscr |= FSCR_LM;
1481 mtspr(SPRN_FSCR, current->thread.fscr);
1482 return;
Michael Ellermanb14b6262013-06-25 17:47:57 +10001483 }
1484
Michael Neuling25176172013-08-09 17:29:29 +10001485 if ((status < ARRAY_SIZE(facility_strings)) &&
1486 facility_strings[status])
1487 facility = facility_strings[status];
Michael Ellerman021424a2013-06-25 17:47:56 +10001488
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001489 /* We restore the interrupt state now */
1490 if (!arch_irq_disabled_regs(regs))
1491 local_irq_enable();
1492
Michael Neulingee4ed6f2014-03-14 17:03:58 +11001493 pr_err_ratelimited(
1494 "%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
1495 hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001496
1497 if (user_mode(regs)) {
1498 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1499 return;
1500 }
1501
Michael Ellerman021424a2013-06-25 17:47:56 +10001502 die("Unexpected facility unavailable exception", regs, SIGABRT);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001503}
Michael Neuling25176172013-08-09 17:29:29 +10001504#endif
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001505
Michael Neulingf54db642013-02-13 16:21:39 +00001506#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1507
Michael Neulingf54db642013-02-13 16:21:39 +00001508void fp_unavailable_tm(struct pt_regs *regs)
1509{
1510 /* Note: This does not handle any kind of FP laziness. */
1511
1512 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1513 regs->nip, regs->msr);
Michael Neulingf54db642013-02-13 16:21:39 +00001514
1515 /* We can only have got here if the task started using FP after
1516 * beginning the transaction. So, the transactional regs are just a
1517 * copy of the checkpointed ones. But, we still need to recheckpoint
1518 * as we're enabling FP for the process; it will return, abort the
1519 * transaction, and probably retry but now with FP enabled. So the
1520 * checkpointed FP registers need to be loaded.
1521 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001522 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001523 /* Reclaim didn't save out any FPRs to transact_fprs. */
1524
1525 /* Enable FP for the task: */
1526 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1527
1528 /* This loads and recheckpoints the FP registers from
1529 * thread.fpr[]. They will remain in registers after the
1530 * checkpoint so we don't need to reload them after.
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001531 * If VMX is in use, the VRs now hold checkpointed values,
1532 * so we don't want to load the VRs from the thread_struct.
Michael Neulingf54db642013-02-13 16:21:39 +00001533 */
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001534 tm_recheckpoint(&current->thread, MSR_FP);
1535
1536 /* If VMX is in use, get the transactional values back */
1537 if (regs->msr & MSR_VEC) {
1538 do_load_up_transact_altivec(&current->thread);
1539 /* At this point all the VSX state is loaded, so enable it */
1540 regs->msr |= MSR_VSX;
1541 }
Michael Neulingf54db642013-02-13 16:21:39 +00001542}
1543
Michael Neulingf54db642013-02-13 16:21:39 +00001544void altivec_unavailable_tm(struct pt_regs *regs)
1545{
1546 /* See the comments in fp_unavailable_tm(). This function operates
1547 * the same way.
1548 */
1549
1550 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1551 "MSR=%lx\n",
1552 regs->nip, regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001553 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001554 regs->msr |= MSR_VEC;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001555 tm_recheckpoint(&current->thread, MSR_VEC);
Michael Neulingf54db642013-02-13 16:21:39 +00001556 current->thread.used_vr = 1;
Michael Neulingf54db642013-02-13 16:21:39 +00001557
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001558 if (regs->msr & MSR_FP) {
1559 do_load_up_transact_fpu(&current->thread);
1560 regs->msr |= MSR_VSX;
1561 }
1562}
1563
Michael Neulingf54db642013-02-13 16:21:39 +00001564void vsx_unavailable_tm(struct pt_regs *regs)
1565{
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001566 unsigned long orig_msr = regs->msr;
1567
Michael Neulingf54db642013-02-13 16:21:39 +00001568 /* See the comments in fp_unavailable_tm(). This works similarly,
1569 * though we're loading both FP and VEC registers in here.
1570 *
1571 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1572 * regs. Either way, set MSR_VSX.
1573 */
1574
1575 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1576 "MSR=%lx\n",
1577 regs->nip, regs->msr);
1578
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001579 current->thread.used_vsr = 1;
1580
1581 /* If FP and VMX are already loaded, we have all the state we need */
1582 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1583 regs->msr |= MSR_VSX;
1584 return;
1585 }
1586
Michael Neulingf54db642013-02-13 16:21:39 +00001587 /* This reclaims FP and/or VR regs if they're already enabled */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001588 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001589
1590 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1591 MSR_VSX;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001592
1593 /* This loads & recheckpoints FP and VRs; but we have
1594 * to be sure not to overwrite previously-valid state.
1595 */
1596 tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1597
1598 if (orig_msr & MSR_FP)
1599 do_load_up_transact_fpu(&current->thread);
1600 if (orig_msr & MSR_VEC)
1601 do_load_up_transact_altivec(&current->thread);
Michael Neulingf54db642013-02-13 16:21:39 +00001602}
Michael Neulingf54db642013-02-13 16:21:39 +00001603#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1604
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001605void performance_monitor_exception(struct pt_regs *regs)
1606{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001607 __this_cpu_inc(irq_stat.pmu_irqs);
Anton Blanchard89713ed2010-01-31 20:34:06 +00001608
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001609 perf_irq(regs);
1610}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001611
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001612#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001613void SoftwareEmulation(struct pt_regs *regs)
1614{
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001615 CHECK_FULL_REGS(regs);
1616
1617 if (!user_mode(regs)) {
1618 debugger(regs);
LEROY Christophe1eb28192013-08-28 16:19:17 +02001619 die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
1620 regs, SIGFPE);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001621 }
1622
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001623 if (!emulate_math(regs))
1624 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001625
Scott Wood5dd57a12007-09-18 15:29:35 -05001626 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001627}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001628#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001629
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001630#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001631static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1632{
1633 int changed = 0;
1634 /*
1635 * Determine the cause of the debug event, clear the
1636 * event flags and send a trap to the handler. Torez
1637 */
1638 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1639 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1640#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301641 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001642#endif
1643 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1644 5);
1645 changed |= 0x01;
1646 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1647 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1648 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1649 6);
1650 changed |= 0x01;
1651 } else if (debug_status & DBSR_IAC1) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301652 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001653 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1654 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1655 1);
1656 changed |= 0x01;
1657 } else if (debug_status & DBSR_IAC2) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301658 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001659 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1660 2);
1661 changed |= 0x01;
1662 } else if (debug_status & DBSR_IAC3) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301663 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001664 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1665 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1666 3);
1667 changed |= 0x01;
1668 } else if (debug_status & DBSR_IAC4) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301669 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001670 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1671 4);
1672 changed |= 0x01;
1673 }
1674 /*
1675 * At the point this routine was called, the MSR(DE) was turned off.
1676 * Check all other debug flags and see if that bit needs to be turned
1677 * back on or not.
1678 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301679 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
Bharat Bhushan95791982013-06-26 11:12:22 +05301680 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001681 regs->msr |= MSR_DE;
1682 else
1683 /* Make sure the IDM flag is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301684 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001685
1686 if (changed & 0x01)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301687 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001688}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001689
Nicholas Piggin03465f82016-09-16 20:48:08 +10001690void DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001691{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301692 current->thread.debug.dbsr = debug_status;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001693
Roland McGrathec097c82009-05-28 21:26:38 +00001694 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1695 * on server, it stops on the target of the branch. In order to simulate
1696 * the server behaviour, we thus restart right away with a single step
1697 * instead of stopping here when hitting a BT
1698 */
1699 if (debug_status & DBSR_BT) {
1700 regs->msr &= ~MSR_DE;
1701
1702 /* Disable BT */
1703 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1704 /* Clear the BT event */
1705 mtspr(SPRN_DBSR, DBSR_BT);
1706
1707 /* Do the single step trick only when coming from userspace */
1708 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301709 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1710 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Roland McGrathec097c82009-05-28 21:26:38 +00001711 regs->msr |= MSR_DE;
1712 return;
1713 }
1714
1715 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1716 5, SIGTRAP) == NOTIFY_STOP) {
1717 return;
1718 }
1719 if (debugger_sstep(regs))
1720 return;
1721 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001722 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001723
1724 /* Disable instruction completion */
1725 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1726 /* Clear the instruction completion event */
1727 mtspr(SPRN_DBSR, DBSR_IC);
1728
1729 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1730 5, SIGTRAP) == NOTIFY_STOP) {
1731 return;
1732 }
1733
1734 if (debugger_sstep(regs))
1735 return;
1736
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001737 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301738 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1739 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1740 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001741 regs->msr |= MSR_DE;
1742 else
1743 /* Make sure the IDM bit is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301744 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001745 }
Kumar Galaf8279622008-06-26 02:01:37 -05001746
1747 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001748 } else
1749 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001750}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001751NOKPROBE_SYMBOL(DebugException);
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001752#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001753
1754#if !defined(CONFIG_TAU_INT)
1755void TAUException(struct pt_regs *regs)
1756{
1757 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1758 regs->nip, regs->msr, regs->trap, print_tainted());
1759}
1760#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001761
1762#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001763void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001764{
1765 int err;
1766
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001767 if (!user_mode(regs)) {
1768 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1769 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001770 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001771 }
1772
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001773 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001774
Anton Blanchardeecff812009-10-27 18:46:55 +00001775 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001776 err = emulate_altivec(regs);
1777 if (err == 0) {
1778 regs->nip += 4; /* skip emulated instruction */
1779 emulate_single_step(regs);
1780 return;
1781 }
1782
1783 if (err == -EFAULT) {
1784 /* got an error reading the instruction */
1785 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1786 } else {
1787 /* didn't recognize the instruction */
1788 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001789 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1790 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001791 current->thread.vr_state.vscr.u[3] |= 0x10000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001792 }
1793}
1794#endif /* CONFIG_ALTIVEC */
1795
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001796#ifdef CONFIG_FSL_BOOKE
1797void CacheLockingException(struct pt_regs *regs, unsigned long address,
1798 unsigned long error_code)
1799{
1800 /* We treat cache locking instructions from the user
1801 * as priv ops, in the future we could try to do
1802 * something smarter
1803 */
1804 if (error_code & (ESR_DLK|ESR_ILK))
1805 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1806 return;
1807}
1808#endif /* CONFIG_FSL_BOOKE */
1809
1810#ifdef CONFIG_SPE
1811void SPEFloatingPointException(struct pt_regs *regs)
1812{
Liu Yu6a800f32008-10-28 11:50:21 +08001813 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001814 unsigned long spefscr;
1815 int fpexc_mode;
1816 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001817 int err;
1818
yu liu685659e2011-06-14 18:34:25 -05001819 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001820
1821 spefscr = current->thread.spefscr;
1822 fpexc_mode = current->thread.fpexc_mode;
1823
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001824 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1825 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001826 }
1827 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1828 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001829 }
1830 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1831 code = FPE_FLTDIV;
1832 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1833 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001834 }
1835 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1836 code = FPE_FLTRES;
1837
Liu Yu6a800f32008-10-28 11:50:21 +08001838 err = do_spe_mathemu(regs);
1839 if (err == 0) {
1840 regs->nip += 4; /* skip emulated instruction */
1841 emulate_single_step(regs);
1842 return;
1843 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001844
Liu Yu6a800f32008-10-28 11:50:21 +08001845 if (err == -EFAULT) {
1846 /* got an error reading the instruction */
1847 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1848 } else if (err == -EINVAL) {
1849 /* didn't recognize the instruction */
1850 printk(KERN_ERR "unrecognized spe instruction "
1851 "in %s at %lx\n", current->comm, regs->nip);
1852 } else {
1853 _exception(SIGFPE, regs, code, regs->nip);
1854 }
1855
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001856 return;
1857}
Liu Yu6a800f32008-10-28 11:50:21 +08001858
1859void SPEFloatingPointRoundException(struct pt_regs *regs)
1860{
1861 extern int speround_handler(struct pt_regs *regs);
1862 int err;
1863
1864 preempt_disable();
1865 if (regs->msr & MSR_SPE)
1866 giveup_spe(current);
1867 preempt_enable();
1868
1869 regs->nip -= 4;
1870 err = speround_handler(regs);
1871 if (err == 0) {
1872 regs->nip += 4; /* skip emulated instruction */
1873 emulate_single_step(regs);
1874 return;
1875 }
1876
1877 if (err == -EFAULT) {
1878 /* got an error reading the instruction */
1879 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1880 } else if (err == -EINVAL) {
1881 /* didn't recognize the instruction */
1882 printk(KERN_ERR "unrecognized spe instruction "
1883 "in %s at %lx\n", current->comm, regs->nip);
1884 } else {
1885 _exception(SIGFPE, regs, 0, regs->nip);
1886 return;
1887 }
1888}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001889#endif
1890
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001891/*
1892 * We enter here if we get an unrecoverable exception, that is, one
1893 * that happened at a point where the RI (recoverable interrupt) bit
1894 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1895 * we therefore lost state by taking this exception.
1896 */
1897void unrecoverable_exception(struct pt_regs *regs)
1898{
1899 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1900 regs->trap, regs->nip);
1901 die("Unrecoverable exception", regs, SIGABRT);
1902}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001903
Jason Gunthorpe1e18c172012-10-05 08:07:15 +00001904#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001905/*
1906 * Default handler for a Watchdog exception,
1907 * spins until a reboot occurs
1908 */
1909void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1910{
1911 /* Generic WatchdogHandler, implement your own */
1912 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1913 return;
1914}
1915
1916void WatchdogException(struct pt_regs *regs)
1917{
1918 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1919 WatchdogHandler(regs);
1920}
1921#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001922
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001923/*
1924 * We enter here if we discover during exception entry that we are
1925 * running in supervisor mode with a userspace value in the stack pointer.
1926 */
1927void kernel_bad_stack(struct pt_regs *regs)
1928{
1929 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1930 regs->gpr[1], regs->nip);
1931 die("Bad kernel stack pointer", regs, SIGABRT);
1932}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001933
1934void __init trap_init(void)
1935{
1936}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001937
1938
1939#ifdef CONFIG_PPC_EMULATED_STATS
1940
1941#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1942
1943struct ppc_emulated ppc_emulated = {
1944#ifdef CONFIG_ALTIVEC
1945 WARN_EMULATED_SETUP(altivec),
1946#endif
1947 WARN_EMULATED_SETUP(dcba),
1948 WARN_EMULATED_SETUP(dcbz),
1949 WARN_EMULATED_SETUP(fp_pair),
1950 WARN_EMULATED_SETUP(isel),
1951 WARN_EMULATED_SETUP(mcrxr),
1952 WARN_EMULATED_SETUP(mfpvr),
1953 WARN_EMULATED_SETUP(multiple),
1954 WARN_EMULATED_SETUP(popcntb),
1955 WARN_EMULATED_SETUP(spe),
1956 WARN_EMULATED_SETUP(string),
Scott Wooda3821b22013-10-28 22:07:59 -05001957 WARN_EMULATED_SETUP(sync),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001958 WARN_EMULATED_SETUP(unaligned),
1959#ifdef CONFIG_MATH_EMULATION
1960 WARN_EMULATED_SETUP(math),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001961#endif
1962#ifdef CONFIG_VSX
1963 WARN_EMULATED_SETUP(vsx),
1964#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001965#ifdef CONFIG_PPC64
1966 WARN_EMULATED_SETUP(mfdscr),
1967 WARN_EMULATED_SETUP(mtdscr),
Anton Blanchardf83319d2014-03-28 17:01:23 +11001968 WARN_EMULATED_SETUP(lq_stq),
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001969#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001970};
1971
1972u32 ppc_warn_emulated;
1973
1974void ppc_warn_emulated_print(const char *type)
1975{
Christian Dietrich76462232011-06-04 05:36:54 +00001976 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1977 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001978}
1979
1980static int __init ppc_warn_emulated_init(void)
1981{
1982 struct dentry *dir, *d;
1983 unsigned int i;
1984 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1985
1986 if (!powerpc_debugfs_root)
1987 return -ENODEV;
1988
1989 dir = debugfs_create_dir("emulated_instructions",
1990 powerpc_debugfs_root);
1991 if (!dir)
1992 return -ENOMEM;
1993
1994 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1995 &ppc_warn_emulated);
1996 if (!d)
1997 goto fail;
1998
1999 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
2000 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
2001 (u32 *)&entries[i].val.counter);
2002 if (!d)
2003 goto fail;
2004 }
2005
2006 return 0;
2007
2008fail:
2009 debugfs_remove_recursive(dir);
2010 return -ENOMEM;
2011}
2012
2013device_initcall(ppc_warn_emulated_init);
2014
2015#endif /* CONFIG_PPC_EMULATED_STATS */