blob: 13ca8b687e8820410255357e676105725f99d629 [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
anton@samba.org293e4682007-03-20 20:38:11 -0500125 oops_enter();
126
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000127 /* racy, but better than risking deadlock. */
128 raw_local_irq_save(flags);
129 cpu = smp_processor_id();
130 if (!arch_spin_trylock(&die_lock)) {
131 if (cpu == die_owner)
132 /* nested oops. should stop eventually */;
133 else
134 arch_spin_lock(&die_lock);
anton@samba.org34c2a142007-03-20 20:38:13 -0500135 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000136 die_nest_count++;
137 die_owner = cpu;
138 console_verbose();
139 bust_spinlocks(1);
140 if (machine_is(powermac))
141 pmac_backlight_unblank();
142 return flags;
143}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000144NOKPROBE_SYMBOL(oops_begin);
Michael Hanselmann5474c122006-06-25 05:47:08 -0700145
Nicholas Piggin03465f82016-09-16 20:48:08 +1000146static void oops_end(unsigned long flags, struct pt_regs *regs,
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000147 int signr)
148{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000149 bust_spinlocks(0);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000150 die_owner = -1;
Rusty Russell373d4d02013-01-21 17:17:39 +1030151 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000152 die_nest_count--;
Anton Blanchard58154c82011-11-30 00:23:09 +0000153 oops_exit();
154 printk("\n");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000155 if (!die_nest_count)
156 /* Nest count reaches zero, release the lock. */
157 arch_spin_unlock(&die_lock);
158 raw_local_irq_restore(flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700159
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000160 crash_fadump(regs, "die oops");
161
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000162 /*
163 * A system reset (0x100) is a request to dump, so we always send
164 * it through the crashdump code.
165 */
166 if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700167 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000168
169 /*
170 * We aren't the primary crash CPU. We need to send it
171 * to a holding pattern to avoid it ending up in the panic
172 * code.
173 */
174 crash_kexec_secondary(regs);
175 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000176
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000177 if (!signr)
178 return;
179
Anton Blanchard58154c82011-11-30 00:23:09 +0000180 /*
181 * While our oops output is serialised by a spinlock, output
182 * from panic() called below can race and corrupt it. If we
183 * know we are going to panic, delay for 1 second so we have a
184 * chance to get clean backtraces from all CPUs that are oopsing.
185 */
186 if (in_interrupt() || panic_on_oops || !current->pid ||
187 is_global_init(current)) {
188 mdelay(MSEC_PER_SEC);
189 }
190
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000191 if (in_interrupt())
192 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700193 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700194 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000195 do_exit(signr);
196}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000197NOKPROBE_SYMBOL(oops_end);
Hormscea6a4b2006-07-30 03:03:34 -0700198
Nicholas Piggin03465f82016-09-16 20:48:08 +1000199static int __die(const char *str, struct pt_regs *regs, long err)
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000200{
201 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
202#ifdef CONFIG_PREEMPT
203 printk("PREEMPT ");
204#endif
205#ifdef CONFIG_SMP
206 printk("SMP NR_CPUS=%d ", NR_CPUS);
207#endif
Joonsoo Kime7df0d82016-03-17 14:17:59 -0700208 if (debug_pagealloc_enabled())
209 printk("DEBUG_PAGEALLOC ");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000210#ifdef CONFIG_NUMA
211 printk("NUMA ");
212#endif
213 printk("%s\n", ppc_md.name ? ppc_md.name : "");
214
215 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
216 return 1;
217
218 print_modules();
219 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000220
221 return 0;
222}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000223NOKPROBE_SYMBOL(__die);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000224
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000225void die(const char *str, struct pt_regs *regs, long err)
226{
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100227 unsigned long flags;
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000228
Nicholas Piggin6f44b202016-11-08 23:14:44 +1100229 if (debugger(regs))
230 return;
231
232 flags = oops_begin(regs);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000233 if (__die(str, regs, err))
234 err = 0;
235 oops_end(flags, regs, err);
236}
237
Oleg Nesterov25baa352009-12-15 16:47:18 -0800238void user_single_step_siginfo(struct task_struct *tsk,
239 struct pt_regs *regs, siginfo_t *info)
240{
241 memset(info, 0, sizeof(*info));
242 info->si_signo = SIGTRAP;
243 info->si_code = TRAP_TRACE;
244 info->si_addr = (void __user *)regs->nip;
245}
246
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000247void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
248{
249 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000250 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
251 "at %08lx nip %08lx lr %08lx code %x\n";
252 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
253 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000254
255 if (!user_mode(regs)) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000256 die("Exception in kernel mode", regs, signr);
257 return;
258 }
259
260 if (show_unhandled_signals && unhandled_signal(current, signr)) {
Christian Dietrich76462232011-06-04 05:36:54 +0000261 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
262 current->comm, current->pid, signr,
263 addr, regs->nip, regs->link, code);
264 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000265
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +1000266 if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
Benjamin Herrenschmidt9f2f79e2012-03-01 15:47:44 +1100267 local_irq_enable();
268
Ananth N Mavinakayanahalli41ab5262012-08-23 21:27:09 +0000269 current->thread.trap_nr = code;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000270 memset(&info, 0, sizeof(info));
271 info.si_signo = signr;
272 info.si_code = code;
273 info.si_addr = (void __user *) addr;
274 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000275}
276
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000277void system_reset_exception(struct pt_regs *regs)
278{
279 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000280 if (ppc_md.system_reset_exception) {
281 if (ppc_md.system_reset_exception(regs))
282 return;
283 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000284
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000285 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000286
287 /* Must die if the interrupt is not recoverable */
288 if (!(regs->msr & MSR_RI))
289 panic("Unrecoverable System Reset");
290
291 /* What should we do here? We could issue a shutdown or hard reset. */
292}
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530293
Christophe Leroyf3079392016-09-05 08:42:31 +0200294#ifdef CONFIG_PPC64
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530295/*
296 * This function is called in real mode. Strictly no printk's please.
297 *
298 * regs->nip and regs->msr contains srr0 and ssr1.
299 */
300long machine_check_early(struct pt_regs *regs)
301{
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530302 long handled = 0;
303
Christoph Lameter69111ba2014-10-21 15:23:25 -0500304 __this_cpu_inc(irq_stat.mce_exceptions);
Mahesh Salgaonkare6654d52014-06-11 14:18:07 +0530305
Daniel Axtens27ea2c42015-06-15 13:25:19 +1000306 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
307
Mahesh Salgaonkar4c703412013-10-30 20:04:40 +0530308 if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
309 handled = cur_cpu_spec->machine_check_early(regs);
310 return handled;
Mahesh Salgaonkar1e9b4502013-10-30 20:04:08 +0530311}
312
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530313long hmi_exception_realmode(struct pt_regs *regs)
314{
Christoph Lameter69111ba2014-10-21 15:23:25 -0500315 __this_cpu_inc(irq_stat.hmi_exceptions);
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530316
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530317 wait_for_subcore_guest_exit();
318
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530319 if (ppc_md.hmi_exception_early)
320 ppc_md.hmi_exception_early(regs);
321
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +0530322 wait_for_tb_resync();
323
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530324 return 0;
325}
326
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000327#endif
328
329/*
330 * I/O accesses can cause machine checks on powermacs.
331 * Check if the NIP corresponds to the address of a sync
332 * instruction for which there is an entry in the exception
333 * table.
334 * Note that the 601 only takes a machine check on TEA
335 * (transfer error ack) signal assertion, and does not
336 * set any of the top 16 bits of SRR1.
337 * -- paulus.
338 */
339static inline int check_io_access(struct pt_regs *regs)
340{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100341#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000342 unsigned long msr = regs->msr;
343 const struct exception_table_entry *entry;
344 unsigned int *nip = (unsigned int *)regs->nip;
345
346 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
347 && (entry = search_exception_tables(regs->nip)) != NULL) {
348 /*
349 * Check that it's a sync instruction, or somewhere
350 * in the twi; isync; nop sequence that inb/inw/inl uses.
351 * As the address is in the exception table
352 * we should be able to read the instr there.
353 * For the debug message, we look at the preceding
354 * load or store.
355 */
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200356 if (*nip == PPC_INST_NOP)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000357 nip -= 2;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200358 else if (*nip == PPC_INST_ISYNC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000359 --nip;
Christophe Leroyddc6cd02016-05-17 14:01:39 +0200360 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000361 unsigned int rb;
362
363 --nip;
364 rb = (*nip >> 11) & 0x1f;
365 printk(KERN_DEBUG "%s bad port %lx at %p\n",
366 (*nip & 0x100)? "OUT to": "IN from",
367 regs->gpr[rb] - _IO_BASE, nip);
368 regs->msr |= MSR_RI;
Nicholas Piggin61a92f72016-10-14 16:47:31 +1100369 regs->nip = extable_fixup(entry);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000370 return 1;
371 }
372 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100373#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000374 return 0;
375}
376
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000377#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000378/* On 4xx, the reason for the machine check or program exception
379 is in the ESR. */
380#define get_reason(regs) ((regs)->dsisr)
381#ifndef CONFIG_FSL_BOOKE
382#define get_mc_reason(regs) ((regs)->dsisr)
383#else
Scott Woodfe04b112010-04-08 00:38:22 -0500384#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000385#endif
386#define REASON_FP ESR_FP
387#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
388#define REASON_PRIVILEGED ESR_PPR
389#define REASON_TRAP ESR_PTR
390
391/* single-step stuff */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530392#define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
393#define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000394
395#else
396/* On non-4xx, the reason for the machine check or program
397 exception is in the MSR. */
398#define get_reason(regs) ((regs)->msr)
399#define get_mc_reason(regs) ((regs)->msr)
Michael Neuling8b3c34c2013-02-13 16:21:32 +0000400#define REASON_TM 0x200000
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000401#define REASON_FP 0x100000
402#define REASON_ILLEGAL 0x80000
403#define REASON_PRIVILEGED 0x40000
404#define REASON_TRAP 0x20000
405
406#define single_stepping(regs) ((regs)->msr & MSR_SE)
407#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
408#endif
409
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100410#if defined(CONFIG_4xx)
411int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000412{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600413 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000414
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000415 if (reason & ESR_IMCP) {
416 printk("Instruction");
417 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
418 } else
419 printk("Data");
420 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100421
422 return 0;
423}
424
425int machine_check_440A(struct pt_regs *regs)
426{
427 unsigned long reason = get_mc_reason(regs);
428
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000429 printk("Machine check in kernel mode.\n");
430 if (reason & ESR_IMCP){
431 printk("Instruction Synchronous Machine Check exception\n");
432 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
433 }
434 else {
435 u32 mcsr = mfspr(SPRN_MCSR);
436 if (mcsr & MCSR_IB)
437 printk("Instruction Read PLB Error\n");
438 if (mcsr & MCSR_DRB)
439 printk("Data Read PLB Error\n");
440 if (mcsr & MCSR_DWB)
441 printk("Data Write PLB Error\n");
442 if (mcsr & MCSR_TLBP)
443 printk("TLB Parity Error\n");
444 if (mcsr & MCSR_ICP){
445 flush_instruction_cache();
446 printk("I-Cache Parity Error\n");
447 }
448 if (mcsr & MCSR_DCSP)
449 printk("D-Cache Search Parity Error\n");
450 if (mcsr & MCSR_DCFP)
451 printk("D-Cache Flush Parity Error\n");
452 if (mcsr & MCSR_IMPE)
453 printk("Machine Check exception is imprecise\n");
454
455 /* Clear MCSR */
456 mtspr(SPRN_MCSR, mcsr);
457 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100458 return 0;
459}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000460
461int machine_check_47x(struct pt_regs *regs)
462{
463 unsigned long reason = get_mc_reason(regs);
464 u32 mcsr;
465
466 printk(KERN_ERR "Machine check in kernel mode.\n");
467 if (reason & ESR_IMCP) {
468 printk(KERN_ERR
469 "Instruction Synchronous Machine Check exception\n");
470 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
471 return 0;
472 }
473 mcsr = mfspr(SPRN_MCSR);
474 if (mcsr & MCSR_IB)
475 printk(KERN_ERR "Instruction Read PLB Error\n");
476 if (mcsr & MCSR_DRB)
477 printk(KERN_ERR "Data Read PLB Error\n");
478 if (mcsr & MCSR_DWB)
479 printk(KERN_ERR "Data Write PLB Error\n");
480 if (mcsr & MCSR_TLBP)
481 printk(KERN_ERR "TLB Parity Error\n");
482 if (mcsr & MCSR_ICP) {
483 flush_instruction_cache();
484 printk(KERN_ERR "I-Cache Parity Error\n");
485 }
486 if (mcsr & MCSR_DCSP)
487 printk(KERN_ERR "D-Cache Search Parity Error\n");
488 if (mcsr & PPC47x_MCSR_GPR)
489 printk(KERN_ERR "GPR Parity Error\n");
490 if (mcsr & PPC47x_MCSR_FPR)
491 printk(KERN_ERR "FPR Parity Error\n");
492 if (mcsr & PPC47x_MCSR_IPR)
493 printk(KERN_ERR "Machine Check exception is imprecise\n");
494
495 /* Clear MCSR */
496 mtspr(SPRN_MCSR, mcsr);
497
498 return 0;
499}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100500#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500501int machine_check_e500mc(struct pt_regs *regs)
502{
503 unsigned long mcsr = mfspr(SPRN_MCSR);
504 unsigned long reason = mcsr;
505 int recoverable = 1;
506
Scott Wood82a9a482011-06-16 14:09:17 -0500507 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800508 recoverable = fsl_rio_mcheck_exception(regs);
509 if (recoverable == 1)
510 goto silent_out;
511 }
512
Scott Woodfe04b112010-04-08 00:38:22 -0500513 printk("Machine check in kernel mode.\n");
514 printk("Caused by (from MCSR=%lx): ", reason);
515
516 if (reason & MCSR_MCP)
517 printk("Machine Check Signal\n");
518
519 if (reason & MCSR_ICPERR) {
520 printk("Instruction Cache Parity Error\n");
521
522 /*
523 * This is recoverable by invalidating the i-cache.
524 */
525 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
526 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
527 ;
528
529 /*
530 * This will generally be accompanied by an instruction
531 * fetch error report -- only treat MCSR_IF as fatal
532 * if it wasn't due to an L1 parity error.
533 */
534 reason &= ~MCSR_IF;
535 }
536
537 if (reason & MCSR_DCPERR_MC) {
538 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500539
540 /*
541 * In write shadow mode we auto-recover from the error, but it
542 * may still get logged and cause a machine check. We should
543 * only treat the non-write shadow case as non-recoverable.
544 */
545 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
546 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500547 }
548
549 if (reason & MCSR_L2MMU_MHIT) {
550 printk("Hit on multiple TLB entries\n");
551 recoverable = 0;
552 }
553
554 if (reason & MCSR_NMI)
555 printk("Non-maskable interrupt\n");
556
557 if (reason & MCSR_IF) {
558 printk("Instruction Fetch Error Report\n");
559 recoverable = 0;
560 }
561
562 if (reason & MCSR_LD) {
563 printk("Load Error Report\n");
564 recoverable = 0;
565 }
566
567 if (reason & MCSR_ST) {
568 printk("Store Error Report\n");
569 recoverable = 0;
570 }
571
572 if (reason & MCSR_LDG) {
573 printk("Guarded Load Error Report\n");
574 recoverable = 0;
575 }
576
577 if (reason & MCSR_TLBSYNC)
578 printk("Simultaneous tlbsync operations\n");
579
580 if (reason & MCSR_BSL2_ERR) {
581 printk("Level 2 Cache Error\n");
582 recoverable = 0;
583 }
584
585 if (reason & MCSR_MAV) {
586 u64 addr;
587
588 addr = mfspr(SPRN_MCAR);
589 addr |= (u64)mfspr(SPRN_MCARU) << 32;
590
591 printk("Machine Check %s Address: %#llx\n",
592 reason & MCSR_MEA ? "Effective" : "Physical", addr);
593 }
594
Shaohui Xiecce1f102010-11-18 14:57:32 +0800595silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500596 mtspr(SPRN_MCSR, mcsr);
597 return mfspr(SPRN_MCSR) == 0 && recoverable;
598}
599
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100600int machine_check_e500(struct pt_regs *regs)
601{
602 unsigned long reason = get_mc_reason(regs);
603
Shaohui Xiecce1f102010-11-18 14:57:32 +0800604 if (reason & MCSR_BUS_RBERR) {
605 if (fsl_rio_mcheck_exception(regs))
606 return 1;
Hongtao Jia4e0e3432013-04-28 13:20:08 +0800607 if (fsl_pci_mcheck_exception(regs))
608 return 1;
Shaohui Xiecce1f102010-11-18 14:57:32 +0800609 }
610
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000611 printk("Machine check in kernel mode.\n");
612 printk("Caused by (from MCSR=%lx): ", reason);
613
614 if (reason & MCSR_MCP)
615 printk("Machine Check Signal\n");
616 if (reason & MCSR_ICPERR)
617 printk("Instruction Cache Parity Error\n");
618 if (reason & MCSR_DCP_PERR)
619 printk("Data Cache Push Parity Error\n");
620 if (reason & MCSR_DCPERR)
621 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000622 if (reason & MCSR_BUS_IAERR)
623 printk("Bus - Instruction Address Error\n");
624 if (reason & MCSR_BUS_RAERR)
625 printk("Bus - Read Address Error\n");
626 if (reason & MCSR_BUS_WAERR)
627 printk("Bus - Write Address Error\n");
628 if (reason & MCSR_BUS_IBERR)
629 printk("Bus - Instruction Data Error\n");
630 if (reason & MCSR_BUS_RBERR)
631 printk("Bus - Read Data Bus Error\n");
632 if (reason & MCSR_BUS_WBERR)
Wladislav Wiebec1528332014-06-17 15:30:53 +0200633 printk("Bus - Write Data Bus Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000634 if (reason & MCSR_BUS_IPERR)
635 printk("Bus - Instruction Parity Error\n");
636 if (reason & MCSR_BUS_RPERR)
637 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100638
639 return 0;
640}
Kumar Gala4490c062010-10-08 08:32:11 -0500641
642int machine_check_generic(struct pt_regs *regs)
643{
644 return 0;
645}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100646#elif defined(CONFIG_E200)
647int machine_check_e200(struct pt_regs *regs)
648{
649 unsigned long reason = get_mc_reason(regs);
650
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000651 printk("Machine check in kernel mode.\n");
652 printk("Caused by (from MCSR=%lx): ", reason);
653
654 if (reason & MCSR_MCP)
655 printk("Machine Check Signal\n");
656 if (reason & MCSR_CP_PERR)
657 printk("Cache Push Parity Error\n");
658 if (reason & MCSR_CPERR)
659 printk("Cache Parity Error\n");
660 if (reason & MCSR_EXCP_ERR)
661 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
662 if (reason & MCSR_BUS_IRERR)
663 printk("Bus - Read Bus Error on instruction fetch\n");
664 if (reason & MCSR_BUS_DRERR)
665 printk("Bus - Read Bus Error on data load\n");
666 if (reason & MCSR_BUS_WRERR)
667 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100668
669 return 0;
670}
Christophe Leroye627f8d2016-09-16 10:23:11 +0200671#elif defined(CONFIG_PPC_8xx)
672int machine_check_8xx(struct pt_regs *regs)
673{
674 unsigned long reason = get_mc_reason(regs);
675
676 pr_err("Machine check in kernel mode.\n");
677 pr_err("Caused by (from SRR1=%lx): ", reason);
678 if (reason & 0x40000000)
679 pr_err("Fetch error at address %lx\n", regs->nip);
680 else
681 pr_err("Data access error at address %lx\n", regs->dar);
682
683#ifdef CONFIG_PCI
684 /* the qspan pci read routines can cause machine checks -- Cort
685 *
686 * yuck !!! that totally needs to go away ! There are better ways
687 * to deal with that than having a wart in the mcheck handler.
688 * -- BenH
689 */
690 bad_page_fault(regs, regs->dar, SIGBUS);
691 return 1;
692#else
693 return 0;
694#endif
695}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100696#else
697int machine_check_generic(struct pt_regs *regs)
698{
699 unsigned long reason = get_mc_reason(regs);
700
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000701 printk("Machine check in kernel mode.\n");
702 printk("Caused by (from SRR1=%lx): ", reason);
703 switch (reason & 0x601F0000) {
704 case 0x80000:
705 printk("Machine check signal\n");
706 break;
707 case 0: /* for 601 */
708 case 0x40000:
709 case 0x140000: /* 7450 MSS error and TEA */
710 printk("Transfer error ack signal\n");
711 break;
712 case 0x20000:
713 printk("Data parity error signal\n");
714 break;
715 case 0x10000:
716 printk("Address parity error signal\n");
717 break;
718 case 0x20000000:
719 printk("L1 Data Cache error\n");
720 break;
721 case 0x40000000:
722 printk("L1 Instruction Cache error\n");
723 break;
724 case 0x00100000:
725 printk("L2 data cache parity error\n");
726 break;
727 default:
728 printk("Unknown values in msr\n");
729 }
Olof Johansson75918a42007-09-21 05:11:20 +1000730 return 0;
731}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100732#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000733
734void machine_check_exception(struct pt_regs *regs)
735{
Li Zhongba12eed2013-05-13 16:16:41 +0000736 enum ctx_state prev_state = exception_enter();
Olof Johansson75918a42007-09-21 05:11:20 +1000737 int recover = 0;
738
Christoph Lameter69111ba2014-10-21 15:23:25 -0500739 __this_cpu_inc(irq_stat.mce_exceptions);
Anton Blanchard89713ed2010-01-31 20:34:06 +0000740
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100741 /* See if any machine dependent calls. In theory, we would want
742 * to call the CPU first, and call the ppc_md. one if the CPU
743 * one returns a positive number. However there is existing code
744 * that assumes the board gets a first chance, so let's keep it
745 * that way for now and fix things later. --BenH.
746 */
Olof Johansson75918a42007-09-21 05:11:20 +1000747 if (ppc_md.machine_check_exception)
748 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100749 else if (cur_cpu_spec->machine_check)
750 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000751
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100752 if (recover > 0)
Li Zhongba12eed2013-05-13 16:16:41 +0000753 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000754
Anton Blancharda4435062011-01-11 19:45:31 +0000755 if (debugger_fault_handler(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000756 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000757
758 if (check_io_access(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000759 goto bail;
Olof Johansson75918a42007-09-21 05:11:20 +1000760
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000761 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000762
763 /* Must die if the interrupt is not recoverable */
764 if (!(regs->msr & MSR_RI))
765 panic("Unrecoverable Machine check");
Li Zhongba12eed2013-05-13 16:16:41 +0000766
767bail:
768 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000769}
770
771void SMIException(struct pt_regs *regs)
772{
773 die("System Management Interrupt", regs, SIGABRT);
774}
775
Mahesh Salgaonkar0869b6f2014-07-29 18:40:01 +0530776void handle_hmi_exception(struct pt_regs *regs)
777{
778 struct pt_regs *old_regs;
779
780 old_regs = set_irq_regs(regs);
781 irq_enter();
782
783 if (ppc_md.handle_hmi_exception)
784 ppc_md.handle_hmi_exception(regs);
785
786 irq_exit();
787 set_irq_regs(old_regs);
788}
789
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000790void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000791{
Li Zhongba12eed2013-05-13 16:16:41 +0000792 enum ctx_state prev_state = exception_enter();
793
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000794 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
795 regs->nip, regs->msr, regs->trap);
796
797 _exception(SIGTRAP, regs, 0, 0);
Li Zhongba12eed2013-05-13 16:16:41 +0000798
799 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000800}
801
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000802void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803{
Li Zhongba12eed2013-05-13 16:16:41 +0000804 enum ctx_state prev_state = exception_enter();
805
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000806 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
807 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000808 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000809 if (debugger_iabr_match(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000810 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000811 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000812
813bail:
814 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000815}
816
817void RunModeException(struct pt_regs *regs)
818{
819 _exception(SIGTRAP, regs, 0, 0);
820}
821
Nicholas Piggin03465f82016-09-16 20:48:08 +1000822void single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000823{
Li Zhongba12eed2013-05-13 16:16:41 +0000824 enum ctx_state prev_state = exception_enter();
825
K.Prasad2538c2d2010-06-15 11:35:31 +0530826 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000827
828 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
829 5, SIGTRAP) == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +0000830 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000831 if (debugger_sstep(regs))
Li Zhongba12eed2013-05-13 16:16:41 +0000832 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000833
834 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +0000835
836bail:
837 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000838}
Nicholas Piggin03465f82016-09-16 20:48:08 +1000839NOKPROBE_SYMBOL(single_step_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000840
841/*
842 * After we have successfully emulated an instruction, we have to
843 * check if the instruction was being single-stepped, and if so,
844 * pretend we got a single-step exception. This was pointed out
845 * by Kumar Gala. -- paulus
846 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000847static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000848{
K.Prasad2538c2d2010-06-15 11:35:31 +0530849 if (single_stepping(regs))
850 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000851}
852
Kumar Gala5fad2932007-02-07 01:47:59 -0600853static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000854{
Kumar Gala5fad2932007-02-07 01:47:59 -0600855 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000856
857 /* Invalid operation */
858 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600859 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000860
861 /* Overflow */
862 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600863 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000864
865 /* Underflow */
866 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600867 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000868
869 /* Divide by zero */
870 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600871 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000872
873 /* Inexact result */
874 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600875 ret = FPE_FLTRES;
876
877 return ret;
878}
879
880static void parse_fpe(struct pt_regs *regs)
881{
882 int code = 0;
883
884 flush_fp_to_thread(current);
885
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000886 code = __parse_fpscr(current->thread.fp_state.fpscr);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000887
888 _exception(SIGFPE, regs, code, regs->nip);
889}
890
891/*
892 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000893 * provide the PVR to user applications using the mfspr rd, PVR.
894 * Return non-zero if we can't emulate, or -EFAULT if the associated
895 * memory access caused an access fault. Return zero on success.
896 *
897 * There are a couple of ways to do this, either "decode" the instruction
898 * or directly match lots of bits. In this case, matching lots of
899 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000900 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000901 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000902static int emulate_string_inst(struct pt_regs *regs, u32 instword)
903{
904 u8 rT = (instword >> 21) & 0x1f;
905 u8 rA = (instword >> 16) & 0x1f;
906 u8 NB_RB = (instword >> 11) & 0x1f;
907 u32 num_bytes;
908 unsigned long EA;
909 int pos = 0;
910
911 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000912 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913 if ((rT == rA) || (rT == NB_RB))
914 return -EINVAL;
915
916 EA = (rA == 0) ? 0 : regs->gpr[rA];
917
Kumar Gala16c57b32009-02-10 20:10:44 +0000918 switch (instword & PPC_INST_STRING_MASK) {
919 case PPC_INST_LSWX:
920 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000921 EA += NB_RB;
922 num_bytes = regs->xer & 0x7f;
923 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000924 case PPC_INST_LSWI:
925 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000926 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
927 break;
928 default:
929 return -EINVAL;
930 }
931
932 while (num_bytes != 0)
933 {
934 u8 val;
935 u32 shift = 8 * (3 - (pos & 0x3));
936
James Yang80aa0fb2013-06-25 11:41:05 -0500937 /* if process is 32-bit, clear upper 32 bits of EA */
938 if ((regs->msr & MSR_64BIT) == 0)
939 EA &= 0xFFFFFFFF;
940
Kumar Gala16c57b32009-02-10 20:10:44 +0000941 switch ((instword & PPC_INST_STRING_MASK)) {
942 case PPC_INST_LSWX:
943 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000944 if (get_user(val, (u8 __user *)EA))
945 return -EFAULT;
946 /* first time updating this reg,
947 * zero it out */
948 if (pos == 0)
949 regs->gpr[rT] = 0;
950 regs->gpr[rT] |= val << shift;
951 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000952 case PPC_INST_STSWI:
953 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000954 val = regs->gpr[rT] >> shift;
955 if (put_user(val, (u8 __user *)EA))
956 return -EFAULT;
957 break;
958 }
959 /* move EA to next address */
960 EA += 1;
961 num_bytes--;
962
963 /* manage our position within the register */
964 if (++pos == 4) {
965 pos = 0;
966 if (++rT == 32)
967 rT = 0;
968 }
969 }
970
971 return 0;
972}
973
Will Schmidtc3412dc2006-08-30 13:11:38 -0500974static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
975{
976 u32 ra,rs;
977 unsigned long tmp;
978
979 ra = (instword >> 16) & 0x1f;
980 rs = (instword >> 21) & 0x1f;
981
982 tmp = regs->gpr[rs];
983 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
984 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
985 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
986 regs->gpr[ra] = tmp;
987
988 return 0;
989}
990
Kumar Galac1469f12007-11-19 21:35:29 -0600991static int emulate_isel(struct pt_regs *regs, u32 instword)
992{
993 u8 rT = (instword >> 21) & 0x1f;
994 u8 rA = (instword >> 16) & 0x1f;
995 u8 rB = (instword >> 11) & 0x1f;
996 u8 BC = (instword >> 6) & 0x1f;
997 u8 bit;
998 unsigned long tmp;
999
1000 tmp = (rA == 0) ? 0 : regs->gpr[rA];
1001 bit = (regs->ccr >> (31 - BC)) & 0x1;
1002
1003 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1004
1005 return 0;
1006}
1007
Michael Neuling6ce6c622013-05-26 18:09:39 +00001008#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1009static inline bool tm_abort_check(struct pt_regs *regs, int cause)
1010{
1011 /* If we're emulating a load/store in an active transaction, we cannot
1012 * emulate it as the kernel operates in transaction suspended context.
1013 * We need to abort the transaction. This creates a persistent TM
1014 * abort so tell the user what caused it with a new code.
1015 */
1016 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
1017 tm_enable();
1018 tm_abort(cause);
1019 return true;
1020 }
1021 return false;
1022}
1023#else
1024static inline bool tm_abort_check(struct pt_regs *regs, int reason)
1025{
1026 return false;
1027}
1028#endif
1029
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001030static int emulate_instruction(struct pt_regs *regs)
1031{
1032 u32 instword;
1033 u32 rd;
1034
Anton Blanchard4288e342013-08-07 02:01:47 +10001035 if (!user_mode(regs))
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001036 return -EINVAL;
1037 CHECK_FULL_REGS(regs);
1038
1039 if (get_user(instword, (u32 __user *)(regs->nip)))
1040 return -EFAULT;
1041
1042 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001043 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001044 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001045 rd = (instword >> 21) & 0x1f;
1046 regs->gpr[rd] = mfspr(SPRN_PVR);
1047 return 0;
1048 }
1049
1050 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001051 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001052 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001053 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001054 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001055
1056 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001057 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +10001058 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001059 unsigned long msk = 0xf0000000UL >> shift;
1060
Anton Blanchardeecff812009-10-27 18:46:55 +00001061 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001062 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
1063 regs->xer &= ~0xf0000000UL;
1064 return 0;
1065 }
1066
1067 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001068 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Michael Neuling6ce6c622013-05-26 18:09:39 +00001069 if (tm_abort_check(regs,
1070 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
1071 return -EINVAL;
Anton Blanchardeecff812009-10-27 18:46:55 +00001072 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001073 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001074 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001075
Will Schmidtc3412dc2006-08-30 13:11:38 -05001076 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +00001077 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001078 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -05001079 return emulate_popcntb_inst(regs, instword);
1080 }
1081
Kumar Galac1469f12007-11-19 21:35:29 -06001082 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +00001083 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +00001084 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -06001085 return emulate_isel(regs, instword);
1086 }
1087
James Yang9863c282013-07-03 16:26:47 -05001088 /* Emulate sync instruction variants */
1089 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1090 PPC_WARN_EMULATED(sync, regs);
1091 asm volatile("sync");
1092 return 0;
1093 }
1094
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001095#ifdef CONFIG_PPC64
1096 /* Emulate the mfspr rD, DSCR. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001097 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1098 PPC_INST_MFSPR_DSCR_USER) ||
1099 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1100 PPC_INST_MFSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001101 cpu_has_feature(CPU_FTR_DSCR)) {
1102 PPC_WARN_EMULATED(mfdscr, regs);
1103 rd = (instword >> 21) & 0x1f;
1104 regs->gpr[rd] = mfspr(SPRN_DSCR);
1105 return 0;
1106 }
1107 /* Emulate the mtspr DSCR, rD. */
Anton Blanchard73d2fb72013-05-01 20:06:33 +00001108 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1109 PPC_INST_MTSPR_DSCR_USER) ||
1110 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1111 PPC_INST_MTSPR_DSCR)) &&
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001112 cpu_has_feature(CPU_FTR_DSCR)) {
1113 PPC_WARN_EMULATED(mtdscr, regs);
1114 rd = (instword >> 21) & 0x1f;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001115 current->thread.dscr = regs->gpr[rd];
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001116 current->thread.dscr_inherit = 1;
Anton Blanchard00ca0de2012-09-03 16:48:46 +00001117 mtspr(SPRN_DSCR, current->thread.dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001118 return 0;
1119 }
1120#endif
1121
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001122 return -EINVAL;
1123}
1124
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001125int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001126{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001127 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001128}
1129
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001130#ifdef CONFIG_MATH_EMULATION
1131static int emulate_math(struct pt_regs *regs)
1132{
1133 int ret;
1134 extern int do_mathemu(struct pt_regs *regs);
1135
1136 ret = do_mathemu(regs);
1137 if (ret >= 0)
1138 PPC_WARN_EMULATED(math, regs);
1139
1140 switch (ret) {
1141 case 0:
1142 emulate_single_step(regs);
1143 return 0;
1144 case 1: {
1145 int code = 0;
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001146 code = __parse_fpscr(current->thread.fp_state.fpscr);
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001147 _exception(SIGFPE, regs, code, regs->nip);
1148 return 0;
1149 }
1150 case -EFAULT:
1151 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1152 return 0;
1153 }
1154
1155 return -1;
1156}
1157#else
1158static inline int emulate_math(struct pt_regs *regs) { return -1; }
1159#endif
1160
Nicholas Piggin03465f82016-09-16 20:48:08 +10001161void program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001162{
Li Zhongba12eed2013-05-13 16:16:41 +00001163 enum ctx_state prev_state = exception_enter();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001164 unsigned int reason = get_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001165
Kim Phillipsaa42c692006-12-08 02:43:30 -06001166 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -06001167 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001168
1169 if (reason & REASON_FP) {
1170 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001171 parse_fpe(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001172 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001173 }
1174 if (reason & REASON_TRAP) {
Balbir Singha4c3f902016-02-18 13:48:01 +11001175 unsigned long bugaddr;
Jason Wesselba797b22010-05-20 21:04:25 -05001176 /* Debugger is first in line to stop recursive faults in
1177 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1178 if (debugger_bpt(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001179 goto bail;
Jason Wesselba797b22010-05-20 21:04:25 -05001180
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001181 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001182 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1183 == NOTIFY_STOP)
Li Zhongba12eed2013-05-13 16:16:41 +00001184 goto bail;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001185
Balbir Singha4c3f902016-02-18 13:48:01 +11001186 bugaddr = regs->nip;
1187 /*
1188 * Fixup bugaddr for BUG_ON() in real mode
1189 */
1190 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1191 bugaddr += PAGE_OFFSET;
1192
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001193 if (!(regs->msr & MSR_PR) && /* not user-mode */
Balbir Singha4c3f902016-02-18 13:48:01 +11001194 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001195 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001196 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001197 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001198 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001199 goto bail;
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001200 }
Michael Neulingbc2a9402013-02-13 16:21:40 +00001201#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1202 if (reason & REASON_TM) {
1203 /* This is a TM "Bad Thing Exception" program check.
1204 * This occurs when:
1205 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1206 * transition in TM states.
1207 * - A trechkpt is attempted when transactional.
1208 * - A treclaim is attempted when non transactional.
1209 * - A tend is illegally attempted.
1210 * - writing a TM SPR when transactional.
1211 */
1212 if (!user_mode(regs) &&
1213 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1214 regs->nip += 4;
Li Zhongba12eed2013-05-13 16:16:41 +00001215 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001216 }
1217 /* If usermode caused this, it's done something illegal and
1218 * gets a SIGILL slap on the wrist. We call it an illegal
1219 * operand to distinguish from the instruction just being bad
1220 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1221 * illegal /placement/ of a valid instruction.
1222 */
1223 if (user_mode(regs)) {
1224 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001225 goto bail;
Michael Neulingbc2a9402013-02-13 16:21:40 +00001226 } else {
1227 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1228 "at %lx (msr 0x%x)\n", regs->nip, reason);
1229 die("Unrecoverable exception", regs, SIGABRT);
1230 }
1231 }
1232#endif
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001233
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001234 /*
1235 * If we took the program check in the kernel skip down to sending a
1236 * SIGILL. The subsequent cases all relate to emulating instructions
1237 * which we should only do for userspace. We also do not want to enable
1238 * interrupts for kernel faults because that might lead to further
1239 * faults, and loose the context of the original exception.
1240 */
1241 if (!user_mode(regs))
1242 goto sigill;
1243
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001244 /* We restore the interrupt state now */
1245 if (!arch_irq_disabled_regs(regs))
1246 local_irq_enable();
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001247
Kumar Gala04903a32007-02-07 01:13:32 -06001248 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1249 * but there seems to be a hardware bug on the 405GP (RevD)
1250 * that means ESR is sometimes set incorrectly - either to
1251 * ESR_DST (!?) or 0. In the process of chasing this with the
1252 * hardware people - not sure if it can happen on any illegal
1253 * instruction or only on FP instructions, whether there is a
Benjamin Herrenschmidt4e63f8e2013-06-09 17:01:24 +10001254 * pattern to occurrences etc. -dgibson 31/Mar/2003
1255 */
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001256 if (!emulate_math(regs))
Li Zhongba12eed2013-05-13 16:16:41 +00001257 goto bail;
Kumar Gala04903a32007-02-07 01:13:32 -06001258
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001259 /* Try to emulate it if we should. */
1260 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001261 switch (emulate_instruction(regs)) {
1262 case 0:
1263 regs->nip += 4;
1264 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001265 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001266 case -EFAULT:
1267 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001268 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001269 }
1270 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001271
Michael Ellermanb3f6a452013-08-15 15:22:19 +10001272sigill:
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001273 if (reason & REASON_PRIVILEGED)
1274 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1275 else
1276 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001277
1278bail:
1279 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001280}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001281NOKPROBE_SYMBOL(program_check_exception);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001282
Paul Mackerrasbf593902013-06-14 20:07:41 +10001283/*
1284 * This occurs when running in hypervisor mode on POWER6 or later
1285 * and an illegal instruction is encountered.
1286 */
Nicholas Piggin03465f82016-09-16 20:48:08 +10001287void emulation_assist_interrupt(struct pt_regs *regs)
Paul Mackerrasbf593902013-06-14 20:07:41 +10001288{
1289 regs->msr |= REASON_ILLEGAL;
1290 program_check_exception(regs);
1291}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001292NOKPROBE_SYMBOL(emulation_assist_interrupt);
Paul Mackerrasbf593902013-06-14 20:07:41 +10001293
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001294void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001295{
Li Zhongba12eed2013-05-13 16:16:41 +00001296 enum ctx_state prev_state = exception_enter();
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001297 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001298
Benjamin Herrenschmidta3512b22012-05-08 13:38:50 +10001299 /* We restore the interrupt state now */
1300 if (!arch_irq_disabled_regs(regs))
1301 local_irq_enable();
1302
Michael Neuling6ce6c622013-05-26 18:09:39 +00001303 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1304 goto bail;
1305
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001306 /* we don't implement logging of alignment exceptions */
1307 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1308 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001309
1310 if (fixed == 1) {
1311 regs->nip += 4; /* skip over emulated instruction */
1312 emulate_single_step(regs);
Li Zhongba12eed2013-05-13 16:16:41 +00001313 goto bail;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001314 }
1315
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001316 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001317 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001318 sig = SIGSEGV;
1319 code = SEGV_ACCERR;
1320 } else {
1321 sig = SIGBUS;
1322 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001323 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001324 if (user_mode(regs))
1325 _exception(sig, regs, code, regs->dar);
1326 else
1327 bad_page_fault(regs, regs->dar, sig);
Li Zhongba12eed2013-05-13 16:16:41 +00001328
1329bail:
1330 exception_exit(prev_state);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001331}
1332
Paul Mackerrasf0f558b2016-09-02 21:49:21 +10001333void slb_miss_bad_addr(struct pt_regs *regs)
1334{
1335 enum ctx_state prev_state = exception_enter();
1336
1337 if (user_mode(regs))
1338 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1339 else
1340 bad_page_fault(regs, regs->dar, SIGSEGV);
1341
1342 exception_exit(prev_state);
1343}
1344
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001345void StackOverflow(struct pt_regs *regs)
1346{
1347 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1348 current, regs->gpr[1]);
1349 debugger(regs);
1350 show_regs(regs);
1351 panic("kernel stack overflow");
1352}
1353
1354void nonrecoverable_exception(struct pt_regs *regs)
1355{
1356 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1357 regs->nip, regs->msr);
1358 debugger(regs);
1359 die("nonrecoverable exception", regs, SIGKILL);
1360}
1361
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001362void kernel_fp_unavailable_exception(struct pt_regs *regs)
1363{
Li Zhongba12eed2013-05-13 16:16:41 +00001364 enum ctx_state prev_state = exception_enter();
1365
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001366 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1367 "%lx at %lx\n", regs->trap, regs->nip);
1368 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001369
1370 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001371}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001372
1373void altivec_unavailable_exception(struct pt_regs *regs)
1374{
Li Zhongba12eed2013-05-13 16:16:41 +00001375 enum ctx_state prev_state = exception_enter();
1376
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001377 if (user_mode(regs)) {
1378 /* A user program has executed an altivec instruction,
1379 but this kernel doesn't support altivec. */
1380 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Li Zhongba12eed2013-05-13 16:16:41 +00001381 goto bail;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001382 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001383
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001384 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1385 "%lx at %lx\n", regs->trap, regs->nip);
1386 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Li Zhongba12eed2013-05-13 16:16:41 +00001387
1388bail:
1389 exception_exit(prev_state);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001390}
1391
Michael Neulingce48b212008-06-25 14:07:18 +10001392void vsx_unavailable_exception(struct pt_regs *regs)
1393{
1394 if (user_mode(regs)) {
1395 /* A user program has executed an vsx instruction,
1396 but this kernel doesn't support vsx. */
1397 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1398 return;
1399 }
1400
1401 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1402 "%lx at %lx\n", regs->trap, regs->nip);
1403 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1404}
1405
Michael Neuling25176172013-08-09 17:29:29 +10001406#ifdef CONFIG_PPC64
Cyril Bur172f7aa2016-09-14 18:02:15 +10001407static void tm_unavailable(struct pt_regs *regs)
1408{
Cyril Bur5d176f72016-09-14 18:02:16 +10001409#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1410 if (user_mode(regs)) {
1411 current->thread.load_tm++;
1412 regs->msr |= MSR_TM;
1413 tm_enable();
1414 tm_restore_sprs(&current->thread);
1415 return;
1416 }
1417#endif
Cyril Bur172f7aa2016-09-14 18:02:15 +10001418 pr_emerg("Unrecoverable TM Unavailable Exception "
1419 "%lx at %lx\n", regs->trap, regs->nip);
1420 die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1421}
1422
Michael Ellerman021424a2013-06-25 17:47:56 +10001423void facility_unavailable_exception(struct pt_regs *regs)
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001424{
Michael Ellerman021424a2013-06-25 17:47:56 +10001425 static char *facility_strings[] = {
Michael Neuling25176172013-08-09 17:29:29 +10001426 [FSCR_FP_LG] = "FPU",
1427 [FSCR_VECVSX_LG] = "VMX/VSX",
1428 [FSCR_DSCR_LG] = "DSCR",
1429 [FSCR_PM_LG] = "PMU SPRs",
1430 [FSCR_BHRB_LG] = "BHRB",
1431 [FSCR_TM_LG] = "TM",
1432 [FSCR_EBB_LG] = "EBB",
1433 [FSCR_TAR_LG] = "TAR",
Michael Ellerman021424a2013-06-25 17:47:56 +10001434 };
Michael Neuling25176172013-08-09 17:29:29 +10001435 char *facility = "unknown";
Michael Ellerman021424a2013-06-25 17:47:56 +10001436 u64 value;
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301437 u32 instword, rd;
Michael Neuling25176172013-08-09 17:29:29 +10001438 u8 status;
1439 bool hv;
Michael Ellerman021424a2013-06-25 17:47:56 +10001440
Michael Neuling25176172013-08-09 17:29:29 +10001441 hv = (regs->trap == 0xf80);
1442 if (hv)
Michael Ellermanb14b6262013-06-25 17:47:57 +10001443 value = mfspr(SPRN_HFSCR);
Michael Neuling25176172013-08-09 17:29:29 +10001444 else
1445 value = mfspr(SPRN_FSCR);
1446
1447 status = value >> 56;
1448 if (status == FSCR_DSCR_LG) {
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301449 /*
1450 * User is accessing the DSCR register using the problem
1451 * state only SPR number (0x03) either through a mfspr or
1452 * a mtspr instruction. If it is a write attempt through
1453 * a mtspr, then we set the inherit bit. This also allows
1454 * the user to write or read the register directly in the
1455 * future by setting via the FSCR DSCR bit. But in case it
1456 * is a read DSCR attempt through a mfspr instruction, we
1457 * just emulate the instruction instead. This code path will
1458 * always emulate all the mfspr instructions till the user
Adam Buchbinder446957b2016-02-24 10:51:11 -08001459 * has attempted at least one mtspr instruction. This way it
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301460 * preserves the same behaviour when the user is accessing
1461 * the DSCR through privilege level only SPR number (0x11)
1462 * which is emulated through illegal instruction exception.
1463 * We always leave HFSCR DSCR set.
Michael Neuling25176172013-08-09 17:29:29 +10001464 */
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301465 if (get_user(instword, (u32 __user *)(regs->nip))) {
1466 pr_err("Failed to fetch the user instruction\n");
1467 return;
1468 }
1469
1470 /* Write into DSCR (mtspr 0x03, RS) */
1471 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1472 == PPC_INST_MTSPR_DSCR_USER) {
1473 rd = (instword >> 21) & 0x1f;
1474 current->thread.dscr = regs->gpr[rd];
1475 current->thread.dscr_inherit = 1;
Michael Neulingb57bd2d2016-06-09 12:31:08 +10001476 current->thread.fscr |= FSCR_DSCR;
1477 mtspr(SPRN_FSCR, current->thread.fscr);
Anshuman Khandualc952c1c2015-05-21 12:13:01 +05301478 }
1479
1480 /* Read from DSCR (mfspr RT, 0x03) */
1481 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1482 == PPC_INST_MFSPR_DSCR_USER) {
1483 if (emulate_instruction(regs)) {
1484 pr_err("DSCR based mfspr emulation failed\n");
1485 return;
1486 }
1487 regs->nip += 4;
1488 emulate_single_step(regs);
1489 }
Michael Neuling25176172013-08-09 17:29:29 +10001490 return;
Michael Ellermanb14b6262013-06-25 17:47:57 +10001491 }
1492
Cyril Bur172f7aa2016-09-14 18:02:15 +10001493 if (status == FSCR_TM_LG) {
1494 /*
1495 * If we're here then the hardware is TM aware because it
1496 * generated an exception with FSRM_TM set.
1497 *
1498 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1499 * told us not to do TM, or the kernel is not built with TM
1500 * support.
1501 *
1502 * If both of those things are true, then userspace can spam the
1503 * console by triggering the printk() below just by continually
1504 * doing tbegin (or any TM instruction). So in that case just
1505 * send the process a SIGILL immediately.
1506 */
1507 if (!cpu_has_feature(CPU_FTR_TM))
1508 goto out;
1509
1510 tm_unavailable(regs);
1511 return;
1512 }
1513
Michael Neuling25176172013-08-09 17:29:29 +10001514 if ((status < ARRAY_SIZE(facility_strings)) &&
1515 facility_strings[status])
1516 facility = facility_strings[status];
Michael Ellerman021424a2013-06-25 17:47:56 +10001517
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001518 /* We restore the interrupt state now */
1519 if (!arch_irq_disabled_regs(regs))
1520 local_irq_enable();
1521
Michael Neulingee4ed6f2014-03-14 17:03:58 +11001522 pr_err_ratelimited(
1523 "%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
1524 hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001525
Cyril Bur172f7aa2016-09-14 18:02:15 +10001526out:
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001527 if (user_mode(regs)) {
1528 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1529 return;
1530 }
1531
Michael Ellerman021424a2013-06-25 17:47:56 +10001532 die("Unexpected facility unavailable exception", regs, SIGABRT);
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001533}
Michael Neuling25176172013-08-09 17:29:29 +10001534#endif
Michael Neulingd0c0c9a2013-02-13 16:21:38 +00001535
Michael Neulingf54db642013-02-13 16:21:39 +00001536#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1537
Michael Neulingf54db642013-02-13 16:21:39 +00001538void fp_unavailable_tm(struct pt_regs *regs)
1539{
1540 /* Note: This does not handle any kind of FP laziness. */
1541
1542 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1543 regs->nip, regs->msr);
Michael Neulingf54db642013-02-13 16:21:39 +00001544
1545 /* We can only have got here if the task started using FP after
1546 * beginning the transaction. So, the transactional regs are just a
1547 * copy of the checkpointed ones. But, we still need to recheckpoint
1548 * as we're enabling FP for the process; it will return, abort the
1549 * transaction, and probably retry but now with FP enabled. So the
1550 * checkpointed FP registers need to be loaded.
1551 */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001552 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001553 /* Reclaim didn't save out any FPRs to transact_fprs. */
1554
1555 /* Enable FP for the task: */
1556 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1557
1558 /* This loads and recheckpoints the FP registers from
1559 * thread.fpr[]. They will remain in registers after the
1560 * checkpoint so we don't need to reload them after.
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001561 * If VMX is in use, the VRs now hold checkpointed values,
1562 * so we don't want to load the VRs from the thread_struct.
Michael Neulingf54db642013-02-13 16:21:39 +00001563 */
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001564 tm_recheckpoint(&current->thread, MSR_FP);
1565
1566 /* If VMX is in use, get the transactional values back */
1567 if (regs->msr & MSR_VEC) {
Cyril Burdc310662016-09-23 16:18:24 +10001568 msr_check_and_set(MSR_VEC);
1569 load_vr_state(&current->thread.vr_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001570 /* At this point all the VSX state is loaded, so enable it */
1571 regs->msr |= MSR_VSX;
1572 }
Michael Neulingf54db642013-02-13 16:21:39 +00001573}
1574
Michael Neulingf54db642013-02-13 16:21:39 +00001575void altivec_unavailable_tm(struct pt_regs *regs)
1576{
1577 /* See the comments in fp_unavailable_tm(). This function operates
1578 * the same way.
1579 */
1580
1581 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1582 "MSR=%lx\n",
1583 regs->nip, regs->msr);
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001584 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001585 regs->msr |= MSR_VEC;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001586 tm_recheckpoint(&current->thread, MSR_VEC);
Michael Neulingf54db642013-02-13 16:21:39 +00001587 current->thread.used_vr = 1;
Michael Neulingf54db642013-02-13 16:21:39 +00001588
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001589 if (regs->msr & MSR_FP) {
Cyril Burdc310662016-09-23 16:18:24 +10001590 msr_check_and_set(MSR_FP);
1591 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001592 regs->msr |= MSR_VSX;
1593 }
1594}
1595
Michael Neulingf54db642013-02-13 16:21:39 +00001596void vsx_unavailable_tm(struct pt_regs *regs)
1597{
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001598 unsigned long orig_msr = regs->msr;
1599
Michael Neulingf54db642013-02-13 16:21:39 +00001600 /* See the comments in fp_unavailable_tm(). This works similarly,
1601 * though we're loading both FP and VEC registers in here.
1602 *
1603 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1604 * regs. Either way, set MSR_VSX.
1605 */
1606
1607 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1608 "MSR=%lx\n",
1609 regs->nip, regs->msr);
1610
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001611 current->thread.used_vsr = 1;
1612
1613 /* If FP and VMX are already loaded, we have all the state we need */
1614 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1615 regs->msr |= MSR_VSX;
1616 return;
1617 }
1618
Michael Neulingf54db642013-02-13 16:21:39 +00001619 /* This reclaims FP and/or VR regs if they're already enabled */
Paul Mackerrasd31626f2014-01-13 15:56:29 +11001620 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
Michael Neulingf54db642013-02-13 16:21:39 +00001621
1622 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1623 MSR_VSX;
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001624
1625 /* This loads & recheckpoints FP and VRs; but we have
1626 * to be sure not to overwrite previously-valid state.
1627 */
1628 tm_recheckpoint(&current->thread, regs->msr & ~orig_msr);
1629
Cyril Burdc310662016-09-23 16:18:24 +10001630 msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1631
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001632 if (orig_msr & MSR_FP)
Cyril Burdc310662016-09-23 16:18:24 +10001633 load_fp_state(&current->thread.fp_state);
Paul Mackerras3ac8ff12014-01-13 15:56:30 +11001634 if (orig_msr & MSR_VEC)
Cyril Burdc310662016-09-23 16:18:24 +10001635 load_vr_state(&current->thread.vr_state);
Michael Neulingf54db642013-02-13 16:21:39 +00001636}
Michael Neulingf54db642013-02-13 16:21:39 +00001637#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1638
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001639void performance_monitor_exception(struct pt_regs *regs)
1640{
Christoph Lameter69111ba2014-10-21 15:23:25 -05001641 __this_cpu_inc(irq_stat.pmu_irqs);
Anton Blanchard89713ed2010-01-31 20:34:06 +00001642
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001643 perf_irq(regs);
1644}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001645
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001646#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001647void SoftwareEmulation(struct pt_regs *regs)
1648{
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001649 CHECK_FULL_REGS(regs);
1650
1651 if (!user_mode(regs)) {
1652 debugger(regs);
LEROY Christophe1eb28192013-08-28 16:19:17 +02001653 die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
1654 regs, SIGFPE);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001655 }
1656
Kevin Hao3a3b5aa2013-07-14 16:40:07 +08001657 if (!emulate_math(regs))
1658 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001659
Scott Wood5dd57a12007-09-18 15:29:35 -05001660 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001661}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001662#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001663
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001664#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001665static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1666{
1667 int changed = 0;
1668 /*
1669 * Determine the cause of the debug event, clear the
1670 * event flags and send a trap to the handler. Torez
1671 */
1672 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1673 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1674#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301675 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001676#endif
1677 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1678 5);
1679 changed |= 0x01;
1680 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1681 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1682 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1683 6);
1684 changed |= 0x01;
1685 } else if (debug_status & DBSR_IAC1) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301686 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001687 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1688 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1689 1);
1690 changed |= 0x01;
1691 } else if (debug_status & DBSR_IAC2) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301692 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001693 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1694 2);
1695 changed |= 0x01;
1696 } else if (debug_status & DBSR_IAC3) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301697 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001698 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1699 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1700 3);
1701 changed |= 0x01;
1702 } else if (debug_status & DBSR_IAC4) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301703 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001704 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1705 4);
1706 changed |= 0x01;
1707 }
1708 /*
1709 * At the point this routine was called, the MSR(DE) was turned off.
1710 * Check all other debug flags and see if that bit needs to be turned
1711 * back on or not.
1712 */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301713 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
Bharat Bhushan95791982013-06-26 11:12:22 +05301714 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001715 regs->msr |= MSR_DE;
1716 else
1717 /* Make sure the IDM flag is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301718 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001719
1720 if (changed & 0x01)
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301721 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001722}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001723
Nicholas Piggin03465f82016-09-16 20:48:08 +10001724void DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001725{
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301726 current->thread.debug.dbsr = debug_status;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001727
Roland McGrathec097c82009-05-28 21:26:38 +00001728 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1729 * on server, it stops on the target of the branch. In order to simulate
1730 * the server behaviour, we thus restart right away with a single step
1731 * instead of stopping here when hitting a BT
1732 */
1733 if (debug_status & DBSR_BT) {
1734 regs->msr &= ~MSR_DE;
1735
1736 /* Disable BT */
1737 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1738 /* Clear the BT event */
1739 mtspr(SPRN_DBSR, DBSR_BT);
1740
1741 /* Do the single step trick only when coming from userspace */
1742 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301743 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1744 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Roland McGrathec097c82009-05-28 21:26:38 +00001745 regs->msr |= MSR_DE;
1746 return;
1747 }
1748
1749 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1750 5, SIGTRAP) == NOTIFY_STOP) {
1751 return;
1752 }
1753 if (debugger_sstep(regs))
1754 return;
1755 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001756 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001757
1758 /* Disable instruction completion */
1759 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1760 /* Clear the instruction completion event */
1761 mtspr(SPRN_DBSR, DBSR_IC);
1762
1763 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1764 5, SIGTRAP) == NOTIFY_STOP) {
1765 return;
1766 }
1767
1768 if (debugger_sstep(regs))
1769 return;
1770
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001771 if (user_mode(regs)) {
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301772 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1773 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1774 current->thread.debug.dbcr1))
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001775 regs->msr |= MSR_DE;
1776 else
1777 /* Make sure the IDM bit is off */
Bharat Bhushan51ae8d42013-07-04 11:45:46 +05301778 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001779 }
Kumar Galaf8279622008-06-26 02:01:37 -05001780
1781 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001782 } else
1783 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001784}
Nicholas Piggin03465f82016-09-16 20:48:08 +10001785NOKPROBE_SYMBOL(DebugException);
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001786#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001787
1788#if !defined(CONFIG_TAU_INT)
1789void TAUException(struct pt_regs *regs)
1790{
1791 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1792 regs->nip, regs->msr, regs->trap, print_tainted());
1793}
1794#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001795
1796#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001797void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001798{
1799 int err;
1800
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001801 if (!user_mode(regs)) {
1802 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1803 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001804 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001805 }
1806
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001807 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001808
Anton Blanchardeecff812009-10-27 18:46:55 +00001809 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001810 err = emulate_altivec(regs);
1811 if (err == 0) {
1812 regs->nip += 4; /* skip emulated instruction */
1813 emulate_single_step(regs);
1814 return;
1815 }
1816
1817 if (err == -EFAULT) {
1818 /* got an error reading the instruction */
1819 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1820 } else {
1821 /* didn't recognize the instruction */
1822 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001823 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1824 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerrasde79f7b2013-09-10 20:20:42 +10001825 current->thread.vr_state.vscr.u[3] |= 0x10000;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001826 }
1827}
1828#endif /* CONFIG_ALTIVEC */
1829
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001830#ifdef CONFIG_FSL_BOOKE
1831void CacheLockingException(struct pt_regs *regs, unsigned long address,
1832 unsigned long error_code)
1833{
1834 /* We treat cache locking instructions from the user
1835 * as priv ops, in the future we could try to do
1836 * something smarter
1837 */
1838 if (error_code & (ESR_DLK|ESR_ILK))
1839 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1840 return;
1841}
1842#endif /* CONFIG_FSL_BOOKE */
1843
1844#ifdef CONFIG_SPE
1845void SPEFloatingPointException(struct pt_regs *regs)
1846{
Liu Yu6a800f32008-10-28 11:50:21 +08001847 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001848 unsigned long spefscr;
1849 int fpexc_mode;
1850 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001851 int err;
1852
yu liu685659e2011-06-14 18:34:25 -05001853 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001854
1855 spefscr = current->thread.spefscr;
1856 fpexc_mode = current->thread.fpexc_mode;
1857
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001858 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1859 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001860 }
1861 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1862 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001863 }
1864 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1865 code = FPE_FLTDIV;
1866 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1867 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001868 }
1869 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1870 code = FPE_FLTRES;
1871
Liu Yu6a800f32008-10-28 11:50:21 +08001872 err = do_spe_mathemu(regs);
1873 if (err == 0) {
1874 regs->nip += 4; /* skip emulated instruction */
1875 emulate_single_step(regs);
1876 return;
1877 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001878
Liu Yu6a800f32008-10-28 11:50:21 +08001879 if (err == -EFAULT) {
1880 /* got an error reading the instruction */
1881 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1882 } else if (err == -EINVAL) {
1883 /* didn't recognize the instruction */
1884 printk(KERN_ERR "unrecognized spe instruction "
1885 "in %s at %lx\n", current->comm, regs->nip);
1886 } else {
1887 _exception(SIGFPE, regs, code, regs->nip);
1888 }
1889
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001890 return;
1891}
Liu Yu6a800f32008-10-28 11:50:21 +08001892
1893void SPEFloatingPointRoundException(struct pt_regs *regs)
1894{
1895 extern int speround_handler(struct pt_regs *regs);
1896 int err;
1897
1898 preempt_disable();
1899 if (regs->msr & MSR_SPE)
1900 giveup_spe(current);
1901 preempt_enable();
1902
1903 regs->nip -= 4;
1904 err = speround_handler(regs);
1905 if (err == 0) {
1906 regs->nip += 4; /* skip emulated instruction */
1907 emulate_single_step(regs);
1908 return;
1909 }
1910
1911 if (err == -EFAULT) {
1912 /* got an error reading the instruction */
1913 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1914 } else if (err == -EINVAL) {
1915 /* didn't recognize the instruction */
1916 printk(KERN_ERR "unrecognized spe instruction "
1917 "in %s at %lx\n", current->comm, regs->nip);
1918 } else {
1919 _exception(SIGFPE, regs, 0, regs->nip);
1920 return;
1921 }
1922}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001923#endif
1924
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001925/*
1926 * We enter here if we get an unrecoverable exception, that is, one
1927 * that happened at a point where the RI (recoverable interrupt) bit
1928 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1929 * we therefore lost state by taking this exception.
1930 */
1931void unrecoverable_exception(struct pt_regs *regs)
1932{
1933 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1934 regs->trap, regs->nip);
1935 die("Unrecoverable exception", regs, SIGABRT);
1936}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001937
Jason Gunthorpe1e18c172012-10-05 08:07:15 +00001938#if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001939/*
1940 * Default handler for a Watchdog exception,
1941 * spins until a reboot occurs
1942 */
1943void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1944{
1945 /* Generic WatchdogHandler, implement your own */
1946 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1947 return;
1948}
1949
1950void WatchdogException(struct pt_regs *regs)
1951{
1952 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1953 WatchdogHandler(regs);
1954}
1955#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001956
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001957/*
1958 * We enter here if we discover during exception entry that we are
1959 * running in supervisor mode with a userspace value in the stack pointer.
1960 */
1961void kernel_bad_stack(struct pt_regs *regs)
1962{
1963 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1964 regs->gpr[1], regs->nip);
1965 die("Bad kernel stack pointer", regs, SIGABRT);
1966}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001967
1968void __init trap_init(void)
1969{
1970}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001971
1972
1973#ifdef CONFIG_PPC_EMULATED_STATS
1974
1975#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1976
1977struct ppc_emulated ppc_emulated = {
1978#ifdef CONFIG_ALTIVEC
1979 WARN_EMULATED_SETUP(altivec),
1980#endif
1981 WARN_EMULATED_SETUP(dcba),
1982 WARN_EMULATED_SETUP(dcbz),
1983 WARN_EMULATED_SETUP(fp_pair),
1984 WARN_EMULATED_SETUP(isel),
1985 WARN_EMULATED_SETUP(mcrxr),
1986 WARN_EMULATED_SETUP(mfpvr),
1987 WARN_EMULATED_SETUP(multiple),
1988 WARN_EMULATED_SETUP(popcntb),
1989 WARN_EMULATED_SETUP(spe),
1990 WARN_EMULATED_SETUP(string),
Scott Wooda3821b22013-10-28 22:07:59 -05001991 WARN_EMULATED_SETUP(sync),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001992 WARN_EMULATED_SETUP(unaligned),
1993#ifdef CONFIG_MATH_EMULATION
1994 WARN_EMULATED_SETUP(math),
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001995#endif
1996#ifdef CONFIG_VSX
1997 WARN_EMULATED_SETUP(vsx),
1998#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001999#ifdef CONFIG_PPC64
2000 WARN_EMULATED_SETUP(mfdscr),
2001 WARN_EMULATED_SETUP(mtdscr),
Anton Blanchardf83319d2014-03-28 17:01:23 +11002002 WARN_EMULATED_SETUP(lq_stq),
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00002003#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002004};
2005
2006u32 ppc_warn_emulated;
2007
2008void ppc_warn_emulated_print(const char *type)
2009{
Christian Dietrich76462232011-06-04 05:36:54 +00002010 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
2011 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00002012}
2013
2014static int __init ppc_warn_emulated_init(void)
2015{
2016 struct dentry *dir, *d;
2017 unsigned int i;
2018 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
2019
2020 if (!powerpc_debugfs_root)
2021 return -ENODEV;
2022
2023 dir = debugfs_create_dir("emulated_instructions",
2024 powerpc_debugfs_root);
2025 if (!dir)
2026 return -ENOMEM;
2027
2028 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
2029 &ppc_warn_emulated);
2030 if (!d)
2031 goto fail;
2032
2033 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
2034 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
2035 (u32 *)&entries[i].val.counter);
2036 if (!d)
2037 goto fail;
2038 }
2039
2040 return 0;
2041
2042fail:
2043 debugfs_remove_recursive(dir);
2044 return -ENOMEM;
2045}
2046
2047device_initcall(ppc_warn_emulated_init);
2048
2049#endif /* CONFIG_PPC_EMULATED_STATS */