blob: 014f88f03d3f90d6c68a55b87c1c8f6ee172b5c9 [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>
28#include <linux/module.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100029#include <linux/prctl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100030#include <linux/delay.h>
31#include <linux/kprobes.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032#include <linux/kexec.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070033#include <linux/backlight.h>
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -080034#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070035#include <linux/kdebug.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000036#include <linux/debugfs.h>
Christian Dietrich76462232011-06-04 05:36:54 +000037#include <linux/ratelimit.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000039#include <asm/emulated_ops.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040#include <asm/pgtable.h>
41#include <asm/uaccess.h>
42#include <asm/system.h>
43#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100044#include <asm/machdep.h>
45#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100046#include <asm/pmc.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100047#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048#include <asm/reg.h>
Paul Mackerras86417782005-10-10 22:37:57 +100049#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100050#ifdef CONFIG_PMAC_BACKLIGHT
51#include <asm/backlight.h>
52#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100053#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100054#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100055#include <asm/processor.h>
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>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100060
Olof Johansson7dbb9222008-01-31 14:34:47 +110061#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000062int (*__debugger)(struct pt_regs *regs) __read_mostly;
63int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
67int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
68int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069
70EXPORT_SYMBOL(__debugger);
71EXPORT_SYMBOL(__debugger_ipi);
72EXPORT_SYMBOL(__debugger_bpt);
73EXPORT_SYMBOL(__debugger_sstep);
74EXPORT_SYMBOL(__debugger_iabr_match);
75EXPORT_SYMBOL(__debugger_dabr_match);
76EXPORT_SYMBOL(__debugger_fault_handler);
77#endif
78
Paul Mackerras14cf11a2005-09-26 16:04:21 +100079/*
80 * Trap & Exception support
81 */
82
anton@samba.org6031d9d2007-03-20 20:38:12 -050083#ifdef CONFIG_PMAC_BACKLIGHT
84static void pmac_backlight_unblank(void)
85{
86 mutex_lock(&pmac_backlight_mutex);
87 if (pmac_backlight) {
88 struct backlight_properties *props;
89
90 props = &pmac_backlight->props;
91 props->brightness = props->max_brightness;
92 props->power = FB_BLANK_UNBLANK;
93 backlight_update_status(pmac_backlight);
94 }
95 mutex_unlock(&pmac_backlight_mutex);
96}
97#else
98static inline void pmac_backlight_unblank(void) { }
99#endif
100
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000101int die(const char *str, struct pt_regs *regs, long err)
102{
anton@samba.org34c2a142007-03-20 20:38:13 -0500103 static struct {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000104 raw_spinlock_t lock;
anton@samba.org34c2a142007-03-20 20:38:13 -0500105 u32 lock_owner;
106 int lock_owner_depth;
107 } die = {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000108 .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
anton@samba.org34c2a142007-03-20 20:38:13 -0500109 .lock_owner = -1,
110 .lock_owner_depth = 0
111 };
David Wilderc0ce7d02006-06-23 15:29:34 -0700112 static int die_counter;
anton@samba.org34c2a142007-03-20 20:38:13 -0500113 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000114
115 if (debugger(regs))
116 return 1;
117
anton@samba.org293e4682007-03-20 20:38:11 -0500118 oops_enter();
119
anton@samba.org34c2a142007-03-20 20:38:13 -0500120 if (die.lock_owner != raw_smp_processor_id()) {
121 console_verbose();
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000122 raw_spin_lock_irqsave(&die.lock, flags);
anton@samba.org34c2a142007-03-20 20:38:13 -0500123 die.lock_owner = smp_processor_id();
124 die.lock_owner_depth = 0;
125 bust_spinlocks(1);
126 if (machine_is(powermac))
127 pmac_backlight_unblank();
128 } else {
129 local_save_flags(flags);
130 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700131
anton@samba.org34c2a142007-03-20 20:38:13 -0500132 if (++die.lock_owner_depth < 3) {
133 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000134#ifdef CONFIG_PREEMPT
anton@samba.org34c2a142007-03-20 20:38:13 -0500135 printk("PREEMPT ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000136#endif
137#ifdef CONFIG_SMP
anton@samba.org34c2a142007-03-20 20:38:13 -0500138 printk("SMP NR_CPUS=%d ", NR_CPUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000139#endif
140#ifdef CONFIG_DEBUG_PAGEALLOC
anton@samba.org34c2a142007-03-20 20:38:13 -0500141 printk("DEBUG_PAGEALLOC ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000142#endif
143#ifdef CONFIG_NUMA
anton@samba.org34c2a142007-03-20 20:38:13 -0500144 printk("NUMA ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145#endif
anton@samba.orgae7f4462007-03-20 20:38:14 -0500146 printk("%s\n", ppc_md.name ? ppc_md.name : "");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100147
Anton Blanchard66fcb102010-02-07 14:44:16 +0000148 if (notify_die(DIE_OOPS, str, regs, err, 255,
149 SIGSEGV) == NOTIFY_STOP)
150 return 1;
151
anton@samba.org34c2a142007-03-20 20:38:13 -0500152 print_modules();
153 show_regs(regs);
154 } else {
155 printk("Recursive die() failure, output suppressed\n");
156 }
157
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000158 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500159 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700160 add_taint(TAINT_DIE);
Anton Blanchard58154c82011-11-30 00:23:09 +0000161 oops_exit();
162 printk("\n");
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000163 raw_spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700164
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 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");
193
Hormscea6a4b2006-07-30 03:03:34 -0700194 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700195 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700196
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000197 do_exit(err);
198
199 return 0;
200}
201
Oleg Nesterov25baa352009-12-15 16:47:18 -0800202void user_single_step_siginfo(struct task_struct *tsk,
203 struct pt_regs *regs, siginfo_t *info)
204{
205 memset(info, 0, sizeof(*info));
206 info->si_signo = SIGTRAP;
207 info->si_code = TRAP_TRACE;
208 info->si_addr = (void __user *)regs->nip;
209}
210
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000211void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
212{
213 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000214 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
215 "at %08lx nip %08lx lr %08lx code %x\n";
216 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
217 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218
219 if (!user_mode(regs)) {
220 if (die("Exception in kernel mode", regs, signr))
221 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000222 } else if (show_unhandled_signals &&
Christian Dietrich76462232011-06-04 05:36:54 +0000223 unhandled_signal(current, signr)) {
224 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
225 current->comm, current->pid, signr,
226 addr, regs->nip, regs->link, code);
227 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000228
229 memset(&info, 0, sizeof(info));
230 info.si_signo = signr;
231 info.si_code = code;
232 info.si_addr = (void __user *) addr;
233 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000234}
235
236#ifdef CONFIG_PPC64
237void system_reset_exception(struct pt_regs *regs)
238{
239 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000240 if (ppc_md.system_reset_exception) {
241 if (ppc_md.system_reset_exception(regs))
242 return;
243 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000244
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000245 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000246
247 /* Must die if the interrupt is not recoverable */
248 if (!(regs->msr & MSR_RI))
249 panic("Unrecoverable System Reset");
250
251 /* What should we do here? We could issue a shutdown or hard reset. */
252}
253#endif
254
255/*
256 * I/O accesses can cause machine checks on powermacs.
257 * Check if the NIP corresponds to the address of a sync
258 * instruction for which there is an entry in the exception
259 * table.
260 * Note that the 601 only takes a machine check on TEA
261 * (transfer error ack) signal assertion, and does not
262 * set any of the top 16 bits of SRR1.
263 * -- paulus.
264 */
265static inline int check_io_access(struct pt_regs *regs)
266{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100267#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000268 unsigned long msr = regs->msr;
269 const struct exception_table_entry *entry;
270 unsigned int *nip = (unsigned int *)regs->nip;
271
272 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
273 && (entry = search_exception_tables(regs->nip)) != NULL) {
274 /*
275 * Check that it's a sync instruction, or somewhere
276 * in the twi; isync; nop sequence that inb/inw/inl uses.
277 * As the address is in the exception table
278 * we should be able to read the instr there.
279 * For the debug message, we look at the preceding
280 * load or store.
281 */
282 if (*nip == 0x60000000) /* nop */
283 nip -= 2;
284 else if (*nip == 0x4c00012c) /* isync */
285 --nip;
286 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
287 /* sync or twi */
288 unsigned int rb;
289
290 --nip;
291 rb = (*nip >> 11) & 0x1f;
292 printk(KERN_DEBUG "%s bad port %lx at %p\n",
293 (*nip & 0x100)? "OUT to": "IN from",
294 regs->gpr[rb] - _IO_BASE, nip);
295 regs->msr |= MSR_RI;
296 regs->nip = entry->fixup;
297 return 1;
298 }
299 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100300#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000301 return 0;
302}
303
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000304#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000305/* On 4xx, the reason for the machine check or program exception
306 is in the ESR. */
307#define get_reason(regs) ((regs)->dsisr)
308#ifndef CONFIG_FSL_BOOKE
309#define get_mc_reason(regs) ((regs)->dsisr)
310#else
Scott Woodfe04b112010-04-08 00:38:22 -0500311#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000312#endif
313#define REASON_FP ESR_FP
314#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
315#define REASON_PRIVILEGED ESR_PPR
316#define REASON_TRAP ESR_PTR
317
318/* single-step stuff */
319#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
320#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
321
322#else
323/* On non-4xx, the reason for the machine check or program
324 exception is in the MSR. */
325#define get_reason(regs) ((regs)->msr)
326#define get_mc_reason(regs) ((regs)->msr)
327#define REASON_FP 0x100000
328#define REASON_ILLEGAL 0x80000
329#define REASON_PRIVILEGED 0x40000
330#define REASON_TRAP 0x20000
331
332#define single_stepping(regs) ((regs)->msr & MSR_SE)
333#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
334#endif
335
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100336#if defined(CONFIG_4xx)
337int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000338{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600339 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000340
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000341 if (reason & ESR_IMCP) {
342 printk("Instruction");
343 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
344 } else
345 printk("Data");
346 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100347
348 return 0;
349}
350
351int machine_check_440A(struct pt_regs *regs)
352{
353 unsigned long reason = get_mc_reason(regs);
354
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000355 printk("Machine check in kernel mode.\n");
356 if (reason & ESR_IMCP){
357 printk("Instruction Synchronous Machine Check exception\n");
358 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
359 }
360 else {
361 u32 mcsr = mfspr(SPRN_MCSR);
362 if (mcsr & MCSR_IB)
363 printk("Instruction Read PLB Error\n");
364 if (mcsr & MCSR_DRB)
365 printk("Data Read PLB Error\n");
366 if (mcsr & MCSR_DWB)
367 printk("Data Write PLB Error\n");
368 if (mcsr & MCSR_TLBP)
369 printk("TLB Parity Error\n");
370 if (mcsr & MCSR_ICP){
371 flush_instruction_cache();
372 printk("I-Cache Parity Error\n");
373 }
374 if (mcsr & MCSR_DCSP)
375 printk("D-Cache Search Parity Error\n");
376 if (mcsr & MCSR_DCFP)
377 printk("D-Cache Flush Parity Error\n");
378 if (mcsr & MCSR_IMPE)
379 printk("Machine Check exception is imprecise\n");
380
381 /* Clear MCSR */
382 mtspr(SPRN_MCSR, mcsr);
383 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100384 return 0;
385}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000386
387int machine_check_47x(struct pt_regs *regs)
388{
389 unsigned long reason = get_mc_reason(regs);
390 u32 mcsr;
391
392 printk(KERN_ERR "Machine check in kernel mode.\n");
393 if (reason & ESR_IMCP) {
394 printk(KERN_ERR
395 "Instruction Synchronous Machine Check exception\n");
396 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
397 return 0;
398 }
399 mcsr = mfspr(SPRN_MCSR);
400 if (mcsr & MCSR_IB)
401 printk(KERN_ERR "Instruction Read PLB Error\n");
402 if (mcsr & MCSR_DRB)
403 printk(KERN_ERR "Data Read PLB Error\n");
404 if (mcsr & MCSR_DWB)
405 printk(KERN_ERR "Data Write PLB Error\n");
406 if (mcsr & MCSR_TLBP)
407 printk(KERN_ERR "TLB Parity Error\n");
408 if (mcsr & MCSR_ICP) {
409 flush_instruction_cache();
410 printk(KERN_ERR "I-Cache Parity Error\n");
411 }
412 if (mcsr & MCSR_DCSP)
413 printk(KERN_ERR "D-Cache Search Parity Error\n");
414 if (mcsr & PPC47x_MCSR_GPR)
415 printk(KERN_ERR "GPR Parity Error\n");
416 if (mcsr & PPC47x_MCSR_FPR)
417 printk(KERN_ERR "FPR Parity Error\n");
418 if (mcsr & PPC47x_MCSR_IPR)
419 printk(KERN_ERR "Machine Check exception is imprecise\n");
420
421 /* Clear MCSR */
422 mtspr(SPRN_MCSR, mcsr);
423
424 return 0;
425}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100426#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500427int machine_check_e500mc(struct pt_regs *regs)
428{
429 unsigned long mcsr = mfspr(SPRN_MCSR);
430 unsigned long reason = mcsr;
431 int recoverable = 1;
432
Scott Wood82a9a482011-06-16 14:09:17 -0500433 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800434 recoverable = fsl_rio_mcheck_exception(regs);
435 if (recoverable == 1)
436 goto silent_out;
437 }
438
Scott Woodfe04b112010-04-08 00:38:22 -0500439 printk("Machine check in kernel mode.\n");
440 printk("Caused by (from MCSR=%lx): ", reason);
441
442 if (reason & MCSR_MCP)
443 printk("Machine Check Signal\n");
444
445 if (reason & MCSR_ICPERR) {
446 printk("Instruction Cache Parity Error\n");
447
448 /*
449 * This is recoverable by invalidating the i-cache.
450 */
451 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
452 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
453 ;
454
455 /*
456 * This will generally be accompanied by an instruction
457 * fetch error report -- only treat MCSR_IF as fatal
458 * if it wasn't due to an L1 parity error.
459 */
460 reason &= ~MCSR_IF;
461 }
462
463 if (reason & MCSR_DCPERR_MC) {
464 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500465
466 /*
467 * In write shadow mode we auto-recover from the error, but it
468 * may still get logged and cause a machine check. We should
469 * only treat the non-write shadow case as non-recoverable.
470 */
471 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
472 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500473 }
474
475 if (reason & MCSR_L2MMU_MHIT) {
476 printk("Hit on multiple TLB entries\n");
477 recoverable = 0;
478 }
479
480 if (reason & MCSR_NMI)
481 printk("Non-maskable interrupt\n");
482
483 if (reason & MCSR_IF) {
484 printk("Instruction Fetch Error Report\n");
485 recoverable = 0;
486 }
487
488 if (reason & MCSR_LD) {
489 printk("Load Error Report\n");
490 recoverable = 0;
491 }
492
493 if (reason & MCSR_ST) {
494 printk("Store Error Report\n");
495 recoverable = 0;
496 }
497
498 if (reason & MCSR_LDG) {
499 printk("Guarded Load Error Report\n");
500 recoverable = 0;
501 }
502
503 if (reason & MCSR_TLBSYNC)
504 printk("Simultaneous tlbsync operations\n");
505
506 if (reason & MCSR_BSL2_ERR) {
507 printk("Level 2 Cache Error\n");
508 recoverable = 0;
509 }
510
511 if (reason & MCSR_MAV) {
512 u64 addr;
513
514 addr = mfspr(SPRN_MCAR);
515 addr |= (u64)mfspr(SPRN_MCARU) << 32;
516
517 printk("Machine Check %s Address: %#llx\n",
518 reason & MCSR_MEA ? "Effective" : "Physical", addr);
519 }
520
Shaohui Xiecce1f102010-11-18 14:57:32 +0800521silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500522 mtspr(SPRN_MCSR, mcsr);
523 return mfspr(SPRN_MCSR) == 0 && recoverable;
524}
525
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100526int machine_check_e500(struct pt_regs *regs)
527{
528 unsigned long reason = get_mc_reason(regs);
529
Shaohui Xiecce1f102010-11-18 14:57:32 +0800530 if (reason & MCSR_BUS_RBERR) {
531 if (fsl_rio_mcheck_exception(regs))
532 return 1;
533 }
534
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000535 printk("Machine check in kernel mode.\n");
536 printk("Caused by (from MCSR=%lx): ", reason);
537
538 if (reason & MCSR_MCP)
539 printk("Machine Check Signal\n");
540 if (reason & MCSR_ICPERR)
541 printk("Instruction Cache Parity Error\n");
542 if (reason & MCSR_DCP_PERR)
543 printk("Data Cache Push Parity Error\n");
544 if (reason & MCSR_DCPERR)
545 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000546 if (reason & MCSR_BUS_IAERR)
547 printk("Bus - Instruction Address Error\n");
548 if (reason & MCSR_BUS_RAERR)
549 printk("Bus - Read Address Error\n");
550 if (reason & MCSR_BUS_WAERR)
551 printk("Bus - Write Address Error\n");
552 if (reason & MCSR_BUS_IBERR)
553 printk("Bus - Instruction Data Error\n");
554 if (reason & MCSR_BUS_RBERR)
555 printk("Bus - Read Data Bus Error\n");
556 if (reason & MCSR_BUS_WBERR)
557 printk("Bus - Read Data Bus Error\n");
558 if (reason & MCSR_BUS_IPERR)
559 printk("Bus - Instruction Parity Error\n");
560 if (reason & MCSR_BUS_RPERR)
561 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100562
563 return 0;
564}
Kumar Gala4490c062010-10-08 08:32:11 -0500565
566int machine_check_generic(struct pt_regs *regs)
567{
568 return 0;
569}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100570#elif defined(CONFIG_E200)
571int machine_check_e200(struct pt_regs *regs)
572{
573 unsigned long reason = get_mc_reason(regs);
574
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000575 printk("Machine check in kernel mode.\n");
576 printk("Caused by (from MCSR=%lx): ", reason);
577
578 if (reason & MCSR_MCP)
579 printk("Machine Check Signal\n");
580 if (reason & MCSR_CP_PERR)
581 printk("Cache Push Parity Error\n");
582 if (reason & MCSR_CPERR)
583 printk("Cache Parity Error\n");
584 if (reason & MCSR_EXCP_ERR)
585 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
586 if (reason & MCSR_BUS_IRERR)
587 printk("Bus - Read Bus Error on instruction fetch\n");
588 if (reason & MCSR_BUS_DRERR)
589 printk("Bus - Read Bus Error on data load\n");
590 if (reason & MCSR_BUS_WRERR)
591 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100592
593 return 0;
594}
595#else
596int machine_check_generic(struct pt_regs *regs)
597{
598 unsigned long reason = get_mc_reason(regs);
599
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000600 printk("Machine check in kernel mode.\n");
601 printk("Caused by (from SRR1=%lx): ", reason);
602 switch (reason & 0x601F0000) {
603 case 0x80000:
604 printk("Machine check signal\n");
605 break;
606 case 0: /* for 601 */
607 case 0x40000:
608 case 0x140000: /* 7450 MSS error and TEA */
609 printk("Transfer error ack signal\n");
610 break;
611 case 0x20000:
612 printk("Data parity error signal\n");
613 break;
614 case 0x10000:
615 printk("Address parity error signal\n");
616 break;
617 case 0x20000000:
618 printk("L1 Data Cache error\n");
619 break;
620 case 0x40000000:
621 printk("L1 Instruction Cache error\n");
622 break;
623 case 0x00100000:
624 printk("L2 data cache parity error\n");
625 break;
626 default:
627 printk("Unknown values in msr\n");
628 }
Olof Johansson75918a42007-09-21 05:11:20 +1000629 return 0;
630}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100631#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000632
633void machine_check_exception(struct pt_regs *regs)
634{
635 int recover = 0;
636
Anton Blanchard89713ed2010-01-31 20:34:06 +0000637 __get_cpu_var(irq_stat).mce_exceptions++;
638
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100639 /* See if any machine dependent calls. In theory, we would want
640 * to call the CPU first, and call the ppc_md. one if the CPU
641 * one returns a positive number. However there is existing code
642 * that assumes the board gets a first chance, so let's keep it
643 * that way for now and fix things later. --BenH.
644 */
Olof Johansson75918a42007-09-21 05:11:20 +1000645 if (ppc_md.machine_check_exception)
646 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100647 else if (cur_cpu_spec->machine_check)
648 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000649
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100650 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000651 return;
652
Olof Johansson75918a42007-09-21 05:11:20 +1000653#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100654 /* the qspan pci read routines can cause machine checks -- Cort
655 *
656 * yuck !!! that totally needs to go away ! There are better ways
657 * to deal with that than having a wart in the mcheck handler.
658 * -- BenH
659 */
Olof Johansson75918a42007-09-21 05:11:20 +1000660 bad_page_fault(regs, regs->dar, SIGBUS);
661 return;
662#endif
663
Anton Blancharda4435062011-01-11 19:45:31 +0000664 if (debugger_fault_handler(regs))
Olof Johansson75918a42007-09-21 05:11:20 +1000665 return;
Olof Johansson75918a42007-09-21 05:11:20 +1000666
667 if (check_io_access(regs))
668 return;
669
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000670 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000671
672 /* Must die if the interrupt is not recoverable */
673 if (!(regs->msr & MSR_RI))
674 panic("Unrecoverable Machine check");
675}
676
677void SMIException(struct pt_regs *regs)
678{
679 die("System Management Interrupt", regs, SIGABRT);
680}
681
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000682void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000683{
684 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
685 regs->nip, regs->msr, regs->trap);
686
687 _exception(SIGTRAP, regs, 0, 0);
688}
689
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000690void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000691{
692 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
693 5, SIGTRAP) == NOTIFY_STOP)
694 return;
695 if (debugger_iabr_match(regs))
696 return;
697 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
698}
699
700void RunModeException(struct pt_regs *regs)
701{
702 _exception(SIGTRAP, regs, 0, 0);
703}
704
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000705void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000706{
K.Prasad2538c2d2010-06-15 11:35:31 +0530707 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000708
709 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
710 5, SIGTRAP) == NOTIFY_STOP)
711 return;
712 if (debugger_sstep(regs))
713 return;
714
715 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
716}
717
718/*
719 * After we have successfully emulated an instruction, we have to
720 * check if the instruction was being single-stepped, and if so,
721 * pretend we got a single-step exception. This was pointed out
722 * by Kumar Gala. -- paulus
723 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000724static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000725{
K.Prasad2538c2d2010-06-15 11:35:31 +0530726 if (single_stepping(regs))
727 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000728}
729
Kumar Gala5fad2932007-02-07 01:47:59 -0600730static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000731{
Kumar Gala5fad2932007-02-07 01:47:59 -0600732 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000733
734 /* Invalid operation */
735 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600736 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000737
738 /* Overflow */
739 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600740 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000741
742 /* Underflow */
743 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600744 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000745
746 /* Divide by zero */
747 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600748 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000749
750 /* Inexact result */
751 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600752 ret = FPE_FLTRES;
753
754 return ret;
755}
756
757static void parse_fpe(struct pt_regs *regs)
758{
759 int code = 0;
760
761 flush_fp_to_thread(current);
762
763 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000764
765 _exception(SIGFPE, regs, code, regs->nip);
766}
767
768/*
769 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000770 * provide the PVR to user applications using the mfspr rd, PVR.
771 * Return non-zero if we can't emulate, or -EFAULT if the associated
772 * memory access caused an access fault. Return zero on success.
773 *
774 * There are a couple of ways to do this, either "decode" the instruction
775 * or directly match lots of bits. In this case, matching lots of
776 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000777 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000778 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000779static int emulate_string_inst(struct pt_regs *regs, u32 instword)
780{
781 u8 rT = (instword >> 21) & 0x1f;
782 u8 rA = (instword >> 16) & 0x1f;
783 u8 NB_RB = (instword >> 11) & 0x1f;
784 u32 num_bytes;
785 unsigned long EA;
786 int pos = 0;
787
788 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000789 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790 if ((rT == rA) || (rT == NB_RB))
791 return -EINVAL;
792
793 EA = (rA == 0) ? 0 : regs->gpr[rA];
794
Kumar Gala16c57b32009-02-10 20:10:44 +0000795 switch (instword & PPC_INST_STRING_MASK) {
796 case PPC_INST_LSWX:
797 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000798 EA += NB_RB;
799 num_bytes = regs->xer & 0x7f;
800 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000801 case PPC_INST_LSWI:
802 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
804 break;
805 default:
806 return -EINVAL;
807 }
808
809 while (num_bytes != 0)
810 {
811 u8 val;
812 u32 shift = 8 * (3 - (pos & 0x3));
813
Kumar Gala16c57b32009-02-10 20:10:44 +0000814 switch ((instword & PPC_INST_STRING_MASK)) {
815 case PPC_INST_LSWX:
816 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000817 if (get_user(val, (u8 __user *)EA))
818 return -EFAULT;
819 /* first time updating this reg,
820 * zero it out */
821 if (pos == 0)
822 regs->gpr[rT] = 0;
823 regs->gpr[rT] |= val << shift;
824 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000825 case PPC_INST_STSWI:
826 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000827 val = regs->gpr[rT] >> shift;
828 if (put_user(val, (u8 __user *)EA))
829 return -EFAULT;
830 break;
831 }
832 /* move EA to next address */
833 EA += 1;
834 num_bytes--;
835
836 /* manage our position within the register */
837 if (++pos == 4) {
838 pos = 0;
839 if (++rT == 32)
840 rT = 0;
841 }
842 }
843
844 return 0;
845}
846
Will Schmidtc3412dc2006-08-30 13:11:38 -0500847static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
848{
849 u32 ra,rs;
850 unsigned long tmp;
851
852 ra = (instword >> 16) & 0x1f;
853 rs = (instword >> 21) & 0x1f;
854
855 tmp = regs->gpr[rs];
856 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
857 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
858 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
859 regs->gpr[ra] = tmp;
860
861 return 0;
862}
863
Kumar Galac1469f12007-11-19 21:35:29 -0600864static int emulate_isel(struct pt_regs *regs, u32 instword)
865{
866 u8 rT = (instword >> 21) & 0x1f;
867 u8 rA = (instword >> 16) & 0x1f;
868 u8 rB = (instword >> 11) & 0x1f;
869 u8 BC = (instword >> 6) & 0x1f;
870 u8 bit;
871 unsigned long tmp;
872
873 tmp = (rA == 0) ? 0 : regs->gpr[rA];
874 bit = (regs->ccr >> (31 - BC)) & 0x1;
875
876 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
877
878 return 0;
879}
880
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000881static int emulate_instruction(struct pt_regs *regs)
882{
883 u32 instword;
884 u32 rd;
885
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000886 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000887 return -EINVAL;
888 CHECK_FULL_REGS(regs);
889
890 if (get_user(instword, (u32 __user *)(regs->nip)))
891 return -EFAULT;
892
893 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000894 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000895 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000896 rd = (instword >> 21) & 0x1f;
897 regs->gpr[rd] = mfspr(SPRN_PVR);
898 return 0;
899 }
900
901 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000902 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000903 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000904 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000905 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000906
907 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000908 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000909 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000910 unsigned long msk = 0xf0000000UL >> shift;
911
Anton Blanchardeecff812009-10-27 18:46:55 +0000912 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
914 regs->xer &= ~0xf0000000UL;
915 return 0;
916 }
917
918 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000919 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000920 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000921 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000922 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000923
Will Schmidtc3412dc2006-08-30 13:11:38 -0500924 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000925 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000926 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500927 return emulate_popcntb_inst(regs, instword);
928 }
929
Kumar Galac1469f12007-11-19 21:35:29 -0600930 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000931 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000932 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600933 return emulate_isel(regs, instword);
934 }
935
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000936#ifdef CONFIG_PPC64
937 /* Emulate the mfspr rD, DSCR. */
938 if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
939 cpu_has_feature(CPU_FTR_DSCR)) {
940 PPC_WARN_EMULATED(mfdscr, regs);
941 rd = (instword >> 21) & 0x1f;
942 regs->gpr[rd] = mfspr(SPRN_DSCR);
943 return 0;
944 }
945 /* Emulate the mtspr DSCR, rD. */
946 if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
947 cpu_has_feature(CPU_FTR_DSCR)) {
948 PPC_WARN_EMULATED(mtdscr, regs);
949 rd = (instword >> 21) & 0x1f;
950 mtspr(SPRN_DSCR, regs->gpr[rd]);
951 current->thread.dscr_inherit = 1;
952 return 0;
953 }
954#endif
955
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000956 return -EINVAL;
957}
958
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800959int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000960{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800961 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000962}
963
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000964void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000965{
966 unsigned int reason = get_reason(regs);
967 extern int do_mathemu(struct pt_regs *regs);
968
Kim Phillipsaa42c692006-12-08 02:43:30 -0600969 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600970 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000971
972 if (reason & REASON_FP) {
973 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000974 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000975 return;
976 }
977 if (reason & REASON_TRAP) {
Jason Wesselba797b22010-05-20 21:04:25 -0500978 /* Debugger is first in line to stop recursive faults in
979 * rcu_lock, notify_die, or atomic_notifier_call_chain */
980 if (debugger_bpt(regs))
981 return;
982
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000983 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000984 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
985 == NOTIFY_STOP)
986 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800987
988 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700989 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000990 regs->nip += 4;
991 return;
992 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000993 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
994 return;
995 }
996
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100997 local_irq_enable();
998
Kumar Gala04903a32007-02-07 01:13:32 -0600999#ifdef CONFIG_MATH_EMULATION
1000 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1001 * but there seems to be a hardware bug on the 405GP (RevD)
1002 * that means ESR is sometimes set incorrectly - either to
1003 * ESR_DST (!?) or 0. In the process of chasing this with the
1004 * hardware people - not sure if it can happen on any illegal
1005 * instruction or only on FP instructions, whether there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001006 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -06001007 switch (do_mathemu(regs)) {
1008 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -06001009 emulate_single_step(regs);
1010 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001011 case 1: {
1012 int code = 0;
1013 code = __parse_fpscr(current->thread.fpscr.val);
1014 _exception(SIGFPE, regs, code, regs->nip);
1015 return;
1016 }
1017 case -EFAULT:
1018 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1019 return;
Kumar Gala04903a32007-02-07 01:13:32 -06001020 }
Kumar Gala5fad2932007-02-07 01:47:59 -06001021 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -06001022#endif /* CONFIG_MATH_EMULATION */
1023
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001024 /* Try to emulate it if we should. */
1025 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001026 switch (emulate_instruction(regs)) {
1027 case 0:
1028 regs->nip += 4;
1029 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001030 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001031 case -EFAULT:
1032 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001033 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001034 }
1035 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001036
1037 if (reason & REASON_PRIVILEGED)
1038 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1039 else
1040 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001041}
1042
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001043void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001044{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001045 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001046
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001047 /* we don't implement logging of alignment exceptions */
1048 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1049 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001050
1051 if (fixed == 1) {
1052 regs->nip += 4; /* skip over emulated instruction */
1053 emulate_single_step(regs);
1054 return;
1055 }
1056
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001057 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001058 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001059 sig = SIGSEGV;
1060 code = SEGV_ACCERR;
1061 } else {
1062 sig = SIGBUS;
1063 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001064 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001065 if (user_mode(regs))
1066 _exception(sig, regs, code, regs->dar);
1067 else
1068 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001069}
1070
1071void StackOverflow(struct pt_regs *regs)
1072{
1073 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1074 current, regs->gpr[1]);
1075 debugger(regs);
1076 show_regs(regs);
1077 panic("kernel stack overflow");
1078}
1079
1080void nonrecoverable_exception(struct pt_regs *regs)
1081{
1082 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1083 regs->nip, regs->msr);
1084 debugger(regs);
1085 die("nonrecoverable exception", regs, SIGKILL);
1086}
1087
1088void trace_syscall(struct pt_regs *regs)
1089{
1090 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -07001091 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001092 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1093}
1094
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001095void kernel_fp_unavailable_exception(struct pt_regs *regs)
1096{
1097 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1098 "%lx at %lx\n", regs->trap, regs->nip);
1099 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1100}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001101
1102void altivec_unavailable_exception(struct pt_regs *regs)
1103{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001104 if (user_mode(regs)) {
1105 /* A user program has executed an altivec instruction,
1106 but this kernel doesn't support altivec. */
1107 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1108 return;
1109 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001110
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001111 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1112 "%lx at %lx\n", regs->trap, regs->nip);
1113 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001114}
1115
Michael Neulingce48b212008-06-25 14:07:18 +10001116void vsx_unavailable_exception(struct pt_regs *regs)
1117{
1118 if (user_mode(regs)) {
1119 /* A user program has executed an vsx instruction,
1120 but this kernel doesn't support vsx. */
1121 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1122 return;
1123 }
1124
1125 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1126 "%lx at %lx\n", regs->trap, regs->nip);
1127 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1128}
1129
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001130void performance_monitor_exception(struct pt_regs *regs)
1131{
Anton Blanchard89713ed2010-01-31 20:34:06 +00001132 __get_cpu_var(irq_stat).pmu_irqs++;
1133
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001134 perf_irq(regs);
1135}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001136
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001137#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001138void SoftwareEmulation(struct pt_regs *regs)
1139{
1140 extern int do_mathemu(struct pt_regs *);
1141 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -05001142#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001143 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -05001144#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001145
1146 CHECK_FULL_REGS(regs);
1147
1148 if (!user_mode(regs)) {
1149 debugger(regs);
1150 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1151 }
1152
1153#ifdef CONFIG_MATH_EMULATION
1154 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001155 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001156 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001157
1158 switch (errcode) {
1159 case 0:
1160 emulate_single_step(regs);
1161 return;
1162 case 1: {
1163 int code = 0;
1164 code = __parse_fpscr(current->thread.fpscr.val);
1165 _exception(SIGFPE, regs, code, regs->nip);
1166 return;
1167 }
1168 case -EFAULT:
1169 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1170 return;
1171 default:
1172 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1173 return;
1174 }
1175
Scott Wood5dd57a12007-09-18 15:29:35 -05001176#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001177 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001178 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001179 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001180
Kumar Gala5fad2932007-02-07 01:47:59 -06001181 switch (errcode) {
1182 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001183 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001184 return;
1185 case 1:
1186 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1187 return;
1188 case -EFAULT:
1189 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1190 return;
1191 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001192#else
1193 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001194#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001195}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001196#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001197
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001198#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001199static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1200{
1201 int changed = 0;
1202 /*
1203 * Determine the cause of the debug event, clear the
1204 * event flags and send a trap to the handler. Torez
1205 */
1206 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1207 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1208#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1209 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1210#endif
1211 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1212 5);
1213 changed |= 0x01;
1214 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1215 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1216 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1217 6);
1218 changed |= 0x01;
1219 } else if (debug_status & DBSR_IAC1) {
1220 current->thread.dbcr0 &= ~DBCR0_IAC1;
1221 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1222 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1223 1);
1224 changed |= 0x01;
1225 } else if (debug_status & DBSR_IAC2) {
1226 current->thread.dbcr0 &= ~DBCR0_IAC2;
1227 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1228 2);
1229 changed |= 0x01;
1230 } else if (debug_status & DBSR_IAC3) {
1231 current->thread.dbcr0 &= ~DBCR0_IAC3;
1232 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1233 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1234 3);
1235 changed |= 0x01;
1236 } else if (debug_status & DBSR_IAC4) {
1237 current->thread.dbcr0 &= ~DBCR0_IAC4;
1238 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1239 4);
1240 changed |= 0x01;
1241 }
1242 /*
1243 * At the point this routine was called, the MSR(DE) was turned off.
1244 * Check all other debug flags and see if that bit needs to be turned
1245 * back on or not.
1246 */
1247 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1248 regs->msr |= MSR_DE;
1249 else
1250 /* Make sure the IDM flag is off */
1251 current->thread.dbcr0 &= ~DBCR0_IDM;
1252
1253 if (changed & 0x01)
1254 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1255}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001256
Kumar Galaf8279622008-06-26 02:01:37 -05001257void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001258{
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001259 current->thread.dbsr = debug_status;
1260
Roland McGrathec097c82009-05-28 21:26:38 +00001261 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1262 * on server, it stops on the target of the branch. In order to simulate
1263 * the server behaviour, we thus restart right away with a single step
1264 * instead of stopping here when hitting a BT
1265 */
1266 if (debug_status & DBSR_BT) {
1267 regs->msr &= ~MSR_DE;
1268
1269 /* Disable BT */
1270 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1271 /* Clear the BT event */
1272 mtspr(SPRN_DBSR, DBSR_BT);
1273
1274 /* Do the single step trick only when coming from userspace */
1275 if (user_mode(regs)) {
1276 current->thread.dbcr0 &= ~DBCR0_BT;
1277 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1278 regs->msr |= MSR_DE;
1279 return;
1280 }
1281
1282 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1283 5, SIGTRAP) == NOTIFY_STOP) {
1284 return;
1285 }
1286 if (debugger_sstep(regs))
1287 return;
1288 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001289 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001290
1291 /* Disable instruction completion */
1292 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1293 /* Clear the instruction completion event */
1294 mtspr(SPRN_DBSR, DBSR_IC);
1295
1296 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1297 5, SIGTRAP) == NOTIFY_STOP) {
1298 return;
1299 }
1300
1301 if (debugger_sstep(regs))
1302 return;
1303
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001304 if (user_mode(regs)) {
1305 current->thread.dbcr0 &= ~DBCR0_IC;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001306 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1307 current->thread.dbcr1))
1308 regs->msr |= MSR_DE;
1309 else
1310 /* Make sure the IDM bit is off */
1311 current->thread.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001312 }
Kumar Galaf8279622008-06-26 02:01:37 -05001313
1314 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001315 } else
1316 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001317}
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001318#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001319
1320#if !defined(CONFIG_TAU_INT)
1321void TAUException(struct pt_regs *regs)
1322{
1323 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1324 regs->nip, regs->msr, regs->trap, print_tainted());
1325}
1326#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001327
1328#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001329void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001330{
1331 int err;
1332
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001333 if (!user_mode(regs)) {
1334 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1335 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001336 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001337 }
1338
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001339 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001340
Anton Blanchardeecff812009-10-27 18:46:55 +00001341 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001342 err = emulate_altivec(regs);
1343 if (err == 0) {
1344 regs->nip += 4; /* skip emulated instruction */
1345 emulate_single_step(regs);
1346 return;
1347 }
1348
1349 if (err == -EFAULT) {
1350 /* got an error reading the instruction */
1351 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1352 } else {
1353 /* didn't recognize the instruction */
1354 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001355 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1356 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001357 current->thread.vscr.u[3] |= 0x10000;
1358 }
1359}
1360#endif /* CONFIG_ALTIVEC */
1361
Michael Neulingce48b212008-06-25 14:07:18 +10001362#ifdef CONFIG_VSX
1363void vsx_assist_exception(struct pt_regs *regs)
1364{
1365 if (!user_mode(regs)) {
1366 printk(KERN_EMERG "VSX assist exception in kernel mode"
1367 " at %lx\n", regs->nip);
1368 die("Kernel VSX assist exception", regs, SIGILL);
1369 }
1370
1371 flush_vsx_to_thread(current);
1372 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1373 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1374}
1375#endif /* CONFIG_VSX */
1376
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001377#ifdef CONFIG_FSL_BOOKE
1378void CacheLockingException(struct pt_regs *regs, unsigned long address,
1379 unsigned long error_code)
1380{
1381 /* We treat cache locking instructions from the user
1382 * as priv ops, in the future we could try to do
1383 * something smarter
1384 */
1385 if (error_code & (ESR_DLK|ESR_ILK))
1386 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1387 return;
1388}
1389#endif /* CONFIG_FSL_BOOKE */
1390
1391#ifdef CONFIG_SPE
1392void SPEFloatingPointException(struct pt_regs *regs)
1393{
Liu Yu6a800f32008-10-28 11:50:21 +08001394 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001395 unsigned long spefscr;
1396 int fpexc_mode;
1397 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001398 int err;
1399
yu liu685659e2011-06-14 18:34:25 -05001400 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001401
1402 spefscr = current->thread.spefscr;
1403 fpexc_mode = current->thread.fpexc_mode;
1404
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001405 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1406 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001407 }
1408 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1409 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001410 }
1411 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1412 code = FPE_FLTDIV;
1413 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1414 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001415 }
1416 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1417 code = FPE_FLTRES;
1418
Liu Yu6a800f32008-10-28 11:50:21 +08001419 err = do_spe_mathemu(regs);
1420 if (err == 0) {
1421 regs->nip += 4; /* skip emulated instruction */
1422 emulate_single_step(regs);
1423 return;
1424 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001425
Liu Yu6a800f32008-10-28 11:50:21 +08001426 if (err == -EFAULT) {
1427 /* got an error reading the instruction */
1428 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1429 } else if (err == -EINVAL) {
1430 /* didn't recognize the instruction */
1431 printk(KERN_ERR "unrecognized spe instruction "
1432 "in %s at %lx\n", current->comm, regs->nip);
1433 } else {
1434 _exception(SIGFPE, regs, code, regs->nip);
1435 }
1436
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001437 return;
1438}
Liu Yu6a800f32008-10-28 11:50:21 +08001439
1440void SPEFloatingPointRoundException(struct pt_regs *regs)
1441{
1442 extern int speround_handler(struct pt_regs *regs);
1443 int err;
1444
1445 preempt_disable();
1446 if (regs->msr & MSR_SPE)
1447 giveup_spe(current);
1448 preempt_enable();
1449
1450 regs->nip -= 4;
1451 err = speround_handler(regs);
1452 if (err == 0) {
1453 regs->nip += 4; /* skip emulated instruction */
1454 emulate_single_step(regs);
1455 return;
1456 }
1457
1458 if (err == -EFAULT) {
1459 /* got an error reading the instruction */
1460 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1461 } else if (err == -EINVAL) {
1462 /* didn't recognize the instruction */
1463 printk(KERN_ERR "unrecognized spe instruction "
1464 "in %s at %lx\n", current->comm, regs->nip);
1465 } else {
1466 _exception(SIGFPE, regs, 0, regs->nip);
1467 return;
1468 }
1469}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001470#endif
1471
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001472/*
1473 * We enter here if we get an unrecoverable exception, that is, one
1474 * that happened at a point where the RI (recoverable interrupt) bit
1475 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1476 * we therefore lost state by taking this exception.
1477 */
1478void unrecoverable_exception(struct pt_regs *regs)
1479{
1480 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1481 regs->trap, regs->nip);
1482 die("Unrecoverable exception", regs, SIGABRT);
1483}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001484
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001485#ifdef CONFIG_BOOKE_WDT
1486/*
1487 * Default handler for a Watchdog exception,
1488 * spins until a reboot occurs
1489 */
1490void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1491{
1492 /* Generic WatchdogHandler, implement your own */
1493 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1494 return;
1495}
1496
1497void WatchdogException(struct pt_regs *regs)
1498{
1499 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1500 WatchdogHandler(regs);
1501}
1502#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001503
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001504/*
1505 * We enter here if we discover during exception entry that we are
1506 * running in supervisor mode with a userspace value in the stack pointer.
1507 */
1508void kernel_bad_stack(struct pt_regs *regs)
1509{
1510 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1511 regs->gpr[1], regs->nip);
1512 die("Bad kernel stack pointer", regs, SIGABRT);
1513}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001514
1515void __init trap_init(void)
1516{
1517}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001518
1519
1520#ifdef CONFIG_PPC_EMULATED_STATS
1521
1522#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1523
1524struct ppc_emulated ppc_emulated = {
1525#ifdef CONFIG_ALTIVEC
1526 WARN_EMULATED_SETUP(altivec),
1527#endif
1528 WARN_EMULATED_SETUP(dcba),
1529 WARN_EMULATED_SETUP(dcbz),
1530 WARN_EMULATED_SETUP(fp_pair),
1531 WARN_EMULATED_SETUP(isel),
1532 WARN_EMULATED_SETUP(mcrxr),
1533 WARN_EMULATED_SETUP(mfpvr),
1534 WARN_EMULATED_SETUP(multiple),
1535 WARN_EMULATED_SETUP(popcntb),
1536 WARN_EMULATED_SETUP(spe),
1537 WARN_EMULATED_SETUP(string),
1538 WARN_EMULATED_SETUP(unaligned),
1539#ifdef CONFIG_MATH_EMULATION
1540 WARN_EMULATED_SETUP(math),
1541#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1542 WARN_EMULATED_SETUP(8xx),
1543#endif
1544#ifdef CONFIG_VSX
1545 WARN_EMULATED_SETUP(vsx),
1546#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001547#ifdef CONFIG_PPC64
1548 WARN_EMULATED_SETUP(mfdscr),
1549 WARN_EMULATED_SETUP(mtdscr),
1550#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001551};
1552
1553u32 ppc_warn_emulated;
1554
1555void ppc_warn_emulated_print(const char *type)
1556{
Christian Dietrich76462232011-06-04 05:36:54 +00001557 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1558 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001559}
1560
1561static int __init ppc_warn_emulated_init(void)
1562{
1563 struct dentry *dir, *d;
1564 unsigned int i;
1565 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1566
1567 if (!powerpc_debugfs_root)
1568 return -ENODEV;
1569
1570 dir = debugfs_create_dir("emulated_instructions",
1571 powerpc_debugfs_root);
1572 if (!dir)
1573 return -ENOMEM;
1574
1575 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1576 &ppc_warn_emulated);
1577 if (!d)
1578 goto fail;
1579
1580 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1581 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1582 (u32 *)&entries[i].val.counter);
1583 if (!d)
1584 goto fail;
1585 }
1586
1587 return 0;
1588
1589fail:
1590 debugfs_remove_recursive(dir);
1591 return -ENOMEM;
1592}
1593
1594device_initcall(ppc_warn_emulated_init);
1595
1596#endif /* CONFIG_PPC_EMULATED_STATS */