blob: 5d40e592ffcb904ca01cc94e31a46964f38accaf [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>
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +000060#include <asm/fadump.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100061
Olof Johansson7dbb9222008-01-31 14:34:47 +110062#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000063int (*__debugger)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
67int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
68int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
69int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100070
71EXPORT_SYMBOL(__debugger);
72EXPORT_SYMBOL(__debugger_ipi);
73EXPORT_SYMBOL(__debugger_bpt);
74EXPORT_SYMBOL(__debugger_sstep);
75EXPORT_SYMBOL(__debugger_iabr_match);
76EXPORT_SYMBOL(__debugger_dabr_match);
77EXPORT_SYMBOL(__debugger_fault_handler);
78#endif
79
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080/*
81 * Trap & Exception support
82 */
83
anton@samba.org6031d9d2007-03-20 20:38:12 -050084#ifdef CONFIG_PMAC_BACKLIGHT
85static void pmac_backlight_unblank(void)
86{
87 mutex_lock(&pmac_backlight_mutex);
88 if (pmac_backlight) {
89 struct backlight_properties *props;
90
91 props = &pmac_backlight->props;
92 props->brightness = props->max_brightness;
93 props->power = FB_BLANK_UNBLANK;
94 backlight_update_status(pmac_backlight);
95 }
96 mutex_unlock(&pmac_backlight_mutex);
97}
98#else
99static inline void pmac_backlight_unblank(void) { }
100#endif
101
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000102static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
103static int die_owner = -1;
104static unsigned int die_nest_count;
105static int die_counter;
106
107static unsigned __kprobes long oops_begin(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000108{
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000109 int cpu;
anton@samba.org34c2a142007-03-20 20:38:13 -0500110 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000111
112 if (debugger(regs))
113 return 1;
114
anton@samba.org293e4682007-03-20 20:38:11 -0500115 oops_enter();
116
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000117 /* racy, but better than risking deadlock. */
118 raw_local_irq_save(flags);
119 cpu = smp_processor_id();
120 if (!arch_spin_trylock(&die_lock)) {
121 if (cpu == die_owner)
122 /* nested oops. should stop eventually */;
123 else
124 arch_spin_lock(&die_lock);
anton@samba.org34c2a142007-03-20 20:38:13 -0500125 }
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000126 die_nest_count++;
127 die_owner = cpu;
128 console_verbose();
129 bust_spinlocks(1);
130 if (machine_is(powermac))
131 pmac_backlight_unblank();
132 return flags;
133}
Michael Hanselmann5474c122006-06-25 05:47:08 -0700134
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000135static void __kprobes oops_end(unsigned long flags, struct pt_regs *regs,
136 int signr)
137{
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000138 bust_spinlocks(0);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000139 die_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700140 add_taint(TAINT_DIE);
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000141 die_nest_count--;
Anton Blanchard58154c82011-11-30 00:23:09 +0000142 oops_exit();
143 printk("\n");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000144 if (!die_nest_count)
145 /* Nest count reaches zero, release the lock. */
146 arch_spin_unlock(&die_lock);
147 raw_local_irq_restore(flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700148
Mahesh Salgaonkarebaeb5a2012-02-16 01:14:45 +0000149 crash_fadump(regs, "die oops");
150
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000151 /*
152 * A system reset (0x100) is a request to dump, so we always send
153 * it through the crashdump code.
154 */
155 if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700156 crash_kexec(regs);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000157
158 /*
159 * We aren't the primary crash CPU. We need to send it
160 * to a holding pattern to avoid it ending up in the panic
161 * code.
162 */
163 crash_kexec_secondary(regs);
164 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000165
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000166 if (!signr)
167 return;
168
Anton Blanchard58154c82011-11-30 00:23:09 +0000169 /*
170 * While our oops output is serialised by a spinlock, output
171 * from panic() called below can race and corrupt it. If we
172 * know we are going to panic, delay for 1 second so we have a
173 * chance to get clean backtraces from all CPUs that are oopsing.
174 */
175 if (in_interrupt() || panic_on_oops || !current->pid ||
176 is_global_init(current)) {
177 mdelay(MSEC_PER_SEC);
178 }
179
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000180 if (in_interrupt())
181 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700182 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700183 panic("Fatal exception");
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000184 do_exit(signr);
185}
Hormscea6a4b2006-07-30 03:03:34 -0700186
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000187static int __kprobes __die(const char *str, struct pt_regs *regs, long err)
188{
189 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
190#ifdef CONFIG_PREEMPT
191 printk("PREEMPT ");
192#endif
193#ifdef CONFIG_SMP
194 printk("SMP NR_CPUS=%d ", NR_CPUS);
195#endif
196#ifdef CONFIG_DEBUG_PAGEALLOC
197 printk("DEBUG_PAGEALLOC ");
198#endif
199#ifdef CONFIG_NUMA
200 printk("NUMA ");
201#endif
202 printk("%s\n", ppc_md.name ? ppc_md.name : "");
203
204 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
205 return 1;
206
207 print_modules();
208 show_regs(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000209
210 return 0;
211}
212
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000213void die(const char *str, struct pt_regs *regs, long err)
214{
215 unsigned long flags = oops_begin(regs);
216
217 if (__die(str, regs, err))
218 err = 0;
219 oops_end(flags, regs, err);
220}
221
Oleg Nesterov25baa352009-12-15 16:47:18 -0800222void user_single_step_siginfo(struct task_struct *tsk,
223 struct pt_regs *regs, siginfo_t *info)
224{
225 memset(info, 0, sizeof(*info));
226 info->si_signo = SIGTRAP;
227 info->si_code = TRAP_TRACE;
228 info->si_addr = (void __user *)regs->nip;
229}
230
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
232{
233 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000234 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
235 "at %08lx nip %08lx lr %08lx code %x\n";
236 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
237 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000238
239 if (!user_mode(regs)) {
Anton Blanchard760ca4d2011-11-30 00:23:13 +0000240 die("Exception in kernel mode", regs, signr);
241 return;
242 }
243
244 if (show_unhandled_signals && unhandled_signal(current, signr)) {
Christian Dietrich76462232011-06-04 05:36:54 +0000245 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
246 current->comm, current->pid, signr,
247 addr, regs->nip, regs->link, code);
248 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000249
250 memset(&info, 0, sizeof(info));
251 info.si_signo = signr;
252 info.si_code = code;
253 info.si_addr = (void __user *) addr;
254 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000255}
256
257#ifdef CONFIG_PPC64
258void system_reset_exception(struct pt_regs *regs)
259{
260 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000261 if (ppc_md.system_reset_exception) {
262 if (ppc_md.system_reset_exception(regs))
263 return;
264 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000265
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000266 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000267
268 /* Must die if the interrupt is not recoverable */
269 if (!(regs->msr & MSR_RI))
270 panic("Unrecoverable System Reset");
271
272 /* What should we do here? We could issue a shutdown or hard reset. */
273}
274#endif
275
276/*
277 * I/O accesses can cause machine checks on powermacs.
278 * Check if the NIP corresponds to the address of a sync
279 * instruction for which there is an entry in the exception
280 * table.
281 * Note that the 601 only takes a machine check on TEA
282 * (transfer error ack) signal assertion, and does not
283 * set any of the top 16 bits of SRR1.
284 * -- paulus.
285 */
286static inline int check_io_access(struct pt_regs *regs)
287{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100288#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000289 unsigned long msr = regs->msr;
290 const struct exception_table_entry *entry;
291 unsigned int *nip = (unsigned int *)regs->nip;
292
293 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
294 && (entry = search_exception_tables(regs->nip)) != NULL) {
295 /*
296 * Check that it's a sync instruction, or somewhere
297 * in the twi; isync; nop sequence that inb/inw/inl uses.
298 * As the address is in the exception table
299 * we should be able to read the instr there.
300 * For the debug message, we look at the preceding
301 * load or store.
302 */
303 if (*nip == 0x60000000) /* nop */
304 nip -= 2;
305 else if (*nip == 0x4c00012c) /* isync */
306 --nip;
307 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
308 /* sync or twi */
309 unsigned int rb;
310
311 --nip;
312 rb = (*nip >> 11) & 0x1f;
313 printk(KERN_DEBUG "%s bad port %lx at %p\n",
314 (*nip & 0x100)? "OUT to": "IN from",
315 regs->gpr[rb] - _IO_BASE, nip);
316 regs->msr |= MSR_RI;
317 regs->nip = entry->fixup;
318 return 1;
319 }
320 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100321#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322 return 0;
323}
324
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000325#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000326/* On 4xx, the reason for the machine check or program exception
327 is in the ESR. */
328#define get_reason(regs) ((regs)->dsisr)
329#ifndef CONFIG_FSL_BOOKE
330#define get_mc_reason(regs) ((regs)->dsisr)
331#else
Scott Woodfe04b112010-04-08 00:38:22 -0500332#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000333#endif
334#define REASON_FP ESR_FP
335#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
336#define REASON_PRIVILEGED ESR_PPR
337#define REASON_TRAP ESR_PTR
338
339/* single-step stuff */
340#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
341#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
342
343#else
344/* On non-4xx, the reason for the machine check or program
345 exception is in the MSR. */
346#define get_reason(regs) ((regs)->msr)
347#define get_mc_reason(regs) ((regs)->msr)
348#define REASON_FP 0x100000
349#define REASON_ILLEGAL 0x80000
350#define REASON_PRIVILEGED 0x40000
351#define REASON_TRAP 0x20000
352
353#define single_stepping(regs) ((regs)->msr & MSR_SE)
354#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
355#endif
356
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100357#if defined(CONFIG_4xx)
358int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000359{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600360 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000361
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000362 if (reason & ESR_IMCP) {
363 printk("Instruction");
364 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
365 } else
366 printk("Data");
367 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100368
369 return 0;
370}
371
372int machine_check_440A(struct pt_regs *regs)
373{
374 unsigned long reason = get_mc_reason(regs);
375
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000376 printk("Machine check in kernel mode.\n");
377 if (reason & ESR_IMCP){
378 printk("Instruction Synchronous Machine Check exception\n");
379 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
380 }
381 else {
382 u32 mcsr = mfspr(SPRN_MCSR);
383 if (mcsr & MCSR_IB)
384 printk("Instruction Read PLB Error\n");
385 if (mcsr & MCSR_DRB)
386 printk("Data Read PLB Error\n");
387 if (mcsr & MCSR_DWB)
388 printk("Data Write PLB Error\n");
389 if (mcsr & MCSR_TLBP)
390 printk("TLB Parity Error\n");
391 if (mcsr & MCSR_ICP){
392 flush_instruction_cache();
393 printk("I-Cache Parity Error\n");
394 }
395 if (mcsr & MCSR_DCSP)
396 printk("D-Cache Search Parity Error\n");
397 if (mcsr & MCSR_DCFP)
398 printk("D-Cache Flush Parity Error\n");
399 if (mcsr & MCSR_IMPE)
400 printk("Machine Check exception is imprecise\n");
401
402 /* Clear MCSR */
403 mtspr(SPRN_MCSR, mcsr);
404 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100405 return 0;
406}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000407
408int machine_check_47x(struct pt_regs *regs)
409{
410 unsigned long reason = get_mc_reason(regs);
411 u32 mcsr;
412
413 printk(KERN_ERR "Machine check in kernel mode.\n");
414 if (reason & ESR_IMCP) {
415 printk(KERN_ERR
416 "Instruction Synchronous Machine Check exception\n");
417 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
418 return 0;
419 }
420 mcsr = mfspr(SPRN_MCSR);
421 if (mcsr & MCSR_IB)
422 printk(KERN_ERR "Instruction Read PLB Error\n");
423 if (mcsr & MCSR_DRB)
424 printk(KERN_ERR "Data Read PLB Error\n");
425 if (mcsr & MCSR_DWB)
426 printk(KERN_ERR "Data Write PLB Error\n");
427 if (mcsr & MCSR_TLBP)
428 printk(KERN_ERR "TLB Parity Error\n");
429 if (mcsr & MCSR_ICP) {
430 flush_instruction_cache();
431 printk(KERN_ERR "I-Cache Parity Error\n");
432 }
433 if (mcsr & MCSR_DCSP)
434 printk(KERN_ERR "D-Cache Search Parity Error\n");
435 if (mcsr & PPC47x_MCSR_GPR)
436 printk(KERN_ERR "GPR Parity Error\n");
437 if (mcsr & PPC47x_MCSR_FPR)
438 printk(KERN_ERR "FPR Parity Error\n");
439 if (mcsr & PPC47x_MCSR_IPR)
440 printk(KERN_ERR "Machine Check exception is imprecise\n");
441
442 /* Clear MCSR */
443 mtspr(SPRN_MCSR, mcsr);
444
445 return 0;
446}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100447#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500448int machine_check_e500mc(struct pt_regs *regs)
449{
450 unsigned long mcsr = mfspr(SPRN_MCSR);
451 unsigned long reason = mcsr;
452 int recoverable = 1;
453
Scott Wood82a9a482011-06-16 14:09:17 -0500454 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800455 recoverable = fsl_rio_mcheck_exception(regs);
456 if (recoverable == 1)
457 goto silent_out;
458 }
459
Scott Woodfe04b112010-04-08 00:38:22 -0500460 printk("Machine check in kernel mode.\n");
461 printk("Caused by (from MCSR=%lx): ", reason);
462
463 if (reason & MCSR_MCP)
464 printk("Machine Check Signal\n");
465
466 if (reason & MCSR_ICPERR) {
467 printk("Instruction Cache Parity Error\n");
468
469 /*
470 * This is recoverable by invalidating the i-cache.
471 */
472 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
473 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
474 ;
475
476 /*
477 * This will generally be accompanied by an instruction
478 * fetch error report -- only treat MCSR_IF as fatal
479 * if it wasn't due to an L1 parity error.
480 */
481 reason &= ~MCSR_IF;
482 }
483
484 if (reason & MCSR_DCPERR_MC) {
485 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500486
487 /*
488 * In write shadow mode we auto-recover from the error, but it
489 * may still get logged and cause a machine check. We should
490 * only treat the non-write shadow case as non-recoverable.
491 */
492 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
493 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500494 }
495
496 if (reason & MCSR_L2MMU_MHIT) {
497 printk("Hit on multiple TLB entries\n");
498 recoverable = 0;
499 }
500
501 if (reason & MCSR_NMI)
502 printk("Non-maskable interrupt\n");
503
504 if (reason & MCSR_IF) {
505 printk("Instruction Fetch Error Report\n");
506 recoverable = 0;
507 }
508
509 if (reason & MCSR_LD) {
510 printk("Load Error Report\n");
511 recoverable = 0;
512 }
513
514 if (reason & MCSR_ST) {
515 printk("Store Error Report\n");
516 recoverable = 0;
517 }
518
519 if (reason & MCSR_LDG) {
520 printk("Guarded Load Error Report\n");
521 recoverable = 0;
522 }
523
524 if (reason & MCSR_TLBSYNC)
525 printk("Simultaneous tlbsync operations\n");
526
527 if (reason & MCSR_BSL2_ERR) {
528 printk("Level 2 Cache Error\n");
529 recoverable = 0;
530 }
531
532 if (reason & MCSR_MAV) {
533 u64 addr;
534
535 addr = mfspr(SPRN_MCAR);
536 addr |= (u64)mfspr(SPRN_MCARU) << 32;
537
538 printk("Machine Check %s Address: %#llx\n",
539 reason & MCSR_MEA ? "Effective" : "Physical", addr);
540 }
541
Shaohui Xiecce1f102010-11-18 14:57:32 +0800542silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500543 mtspr(SPRN_MCSR, mcsr);
544 return mfspr(SPRN_MCSR) == 0 && recoverable;
545}
546
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100547int machine_check_e500(struct pt_regs *regs)
548{
549 unsigned long reason = get_mc_reason(regs);
550
Shaohui Xiecce1f102010-11-18 14:57:32 +0800551 if (reason & MCSR_BUS_RBERR) {
552 if (fsl_rio_mcheck_exception(regs))
553 return 1;
554 }
555
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000556 printk("Machine check in kernel mode.\n");
557 printk("Caused by (from MCSR=%lx): ", reason);
558
559 if (reason & MCSR_MCP)
560 printk("Machine Check Signal\n");
561 if (reason & MCSR_ICPERR)
562 printk("Instruction Cache Parity Error\n");
563 if (reason & MCSR_DCP_PERR)
564 printk("Data Cache Push Parity Error\n");
565 if (reason & MCSR_DCPERR)
566 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000567 if (reason & MCSR_BUS_IAERR)
568 printk("Bus - Instruction Address Error\n");
569 if (reason & MCSR_BUS_RAERR)
570 printk("Bus - Read Address Error\n");
571 if (reason & MCSR_BUS_WAERR)
572 printk("Bus - Write Address Error\n");
573 if (reason & MCSR_BUS_IBERR)
574 printk("Bus - Instruction Data Error\n");
575 if (reason & MCSR_BUS_RBERR)
576 printk("Bus - Read Data Bus Error\n");
577 if (reason & MCSR_BUS_WBERR)
578 printk("Bus - Read Data Bus Error\n");
579 if (reason & MCSR_BUS_IPERR)
580 printk("Bus - Instruction Parity Error\n");
581 if (reason & MCSR_BUS_RPERR)
582 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100583
584 return 0;
585}
Kumar Gala4490c062010-10-08 08:32:11 -0500586
587int machine_check_generic(struct pt_regs *regs)
588{
589 return 0;
590}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100591#elif defined(CONFIG_E200)
592int machine_check_e200(struct pt_regs *regs)
593{
594 unsigned long reason = get_mc_reason(regs);
595
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000596 printk("Machine check in kernel mode.\n");
597 printk("Caused by (from MCSR=%lx): ", reason);
598
599 if (reason & MCSR_MCP)
600 printk("Machine Check Signal\n");
601 if (reason & MCSR_CP_PERR)
602 printk("Cache Push Parity Error\n");
603 if (reason & MCSR_CPERR)
604 printk("Cache Parity Error\n");
605 if (reason & MCSR_EXCP_ERR)
606 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
607 if (reason & MCSR_BUS_IRERR)
608 printk("Bus - Read Bus Error on instruction fetch\n");
609 if (reason & MCSR_BUS_DRERR)
610 printk("Bus - Read Bus Error on data load\n");
611 if (reason & MCSR_BUS_WRERR)
612 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100613
614 return 0;
615}
616#else
617int machine_check_generic(struct pt_regs *regs)
618{
619 unsigned long reason = get_mc_reason(regs);
620
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000621 printk("Machine check in kernel mode.\n");
622 printk("Caused by (from SRR1=%lx): ", reason);
623 switch (reason & 0x601F0000) {
624 case 0x80000:
625 printk("Machine check signal\n");
626 break;
627 case 0: /* for 601 */
628 case 0x40000:
629 case 0x140000: /* 7450 MSS error and TEA */
630 printk("Transfer error ack signal\n");
631 break;
632 case 0x20000:
633 printk("Data parity error signal\n");
634 break;
635 case 0x10000:
636 printk("Address parity error signal\n");
637 break;
638 case 0x20000000:
639 printk("L1 Data Cache error\n");
640 break;
641 case 0x40000000:
642 printk("L1 Instruction Cache error\n");
643 break;
644 case 0x00100000:
645 printk("L2 data cache parity error\n");
646 break;
647 default:
648 printk("Unknown values in msr\n");
649 }
Olof Johansson75918a42007-09-21 05:11:20 +1000650 return 0;
651}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100652#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000653
654void machine_check_exception(struct pt_regs *regs)
655{
656 int recover = 0;
657
Anton Blanchard89713ed2010-01-31 20:34:06 +0000658 __get_cpu_var(irq_stat).mce_exceptions++;
659
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100660 /* See if any machine dependent calls. In theory, we would want
661 * to call the CPU first, and call the ppc_md. one if the CPU
662 * one returns a positive number. However there is existing code
663 * that assumes the board gets a first chance, so let's keep it
664 * that way for now and fix things later. --BenH.
665 */
Olof Johansson75918a42007-09-21 05:11:20 +1000666 if (ppc_md.machine_check_exception)
667 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100668 else if (cur_cpu_spec->machine_check)
669 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000670
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100671 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000672 return;
673
Olof Johansson75918a42007-09-21 05:11:20 +1000674#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100675 /* the qspan pci read routines can cause machine checks -- Cort
676 *
677 * yuck !!! that totally needs to go away ! There are better ways
678 * to deal with that than having a wart in the mcheck handler.
679 * -- BenH
680 */
Olof Johansson75918a42007-09-21 05:11:20 +1000681 bad_page_fault(regs, regs->dar, SIGBUS);
682 return;
683#endif
684
Anton Blancharda4435062011-01-11 19:45:31 +0000685 if (debugger_fault_handler(regs))
Olof Johansson75918a42007-09-21 05:11:20 +1000686 return;
Olof Johansson75918a42007-09-21 05:11:20 +1000687
688 if (check_io_access(regs))
689 return;
690
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000691 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000692
693 /* Must die if the interrupt is not recoverable */
694 if (!(regs->msr & MSR_RI))
695 panic("Unrecoverable Machine check");
696}
697
698void SMIException(struct pt_regs *regs)
699{
700 die("System Management Interrupt", regs, SIGABRT);
701}
702
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000703void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000704{
705 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
706 regs->nip, regs->msr, regs->trap);
707
708 _exception(SIGTRAP, regs, 0, 0);
709}
710
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000711void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000712{
713 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
714 5, SIGTRAP) == NOTIFY_STOP)
715 return;
716 if (debugger_iabr_match(regs))
717 return;
718 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
719}
720
721void RunModeException(struct pt_regs *regs)
722{
723 _exception(SIGTRAP, regs, 0, 0);
724}
725
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000726void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000727{
K.Prasad2538c2d2010-06-15 11:35:31 +0530728 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000729
730 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
731 5, SIGTRAP) == NOTIFY_STOP)
732 return;
733 if (debugger_sstep(regs))
734 return;
735
736 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
737}
738
739/*
740 * After we have successfully emulated an instruction, we have to
741 * check if the instruction was being single-stepped, and if so,
742 * pretend we got a single-step exception. This was pointed out
743 * by Kumar Gala. -- paulus
744 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000745static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000746{
K.Prasad2538c2d2010-06-15 11:35:31 +0530747 if (single_stepping(regs))
748 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000749}
750
Kumar Gala5fad2932007-02-07 01:47:59 -0600751static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000752{
Kumar Gala5fad2932007-02-07 01:47:59 -0600753 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000754
755 /* Invalid operation */
756 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600757 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000758
759 /* Overflow */
760 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600761 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000762
763 /* Underflow */
764 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600765 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000766
767 /* Divide by zero */
768 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600769 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000770
771 /* Inexact result */
772 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600773 ret = FPE_FLTRES;
774
775 return ret;
776}
777
778static void parse_fpe(struct pt_regs *regs)
779{
780 int code = 0;
781
782 flush_fp_to_thread(current);
783
784 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000785
786 _exception(SIGFPE, regs, code, regs->nip);
787}
788
789/*
790 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000791 * provide the PVR to user applications using the mfspr rd, PVR.
792 * Return non-zero if we can't emulate, or -EFAULT if the associated
793 * memory access caused an access fault. Return zero on success.
794 *
795 * There are a couple of ways to do this, either "decode" the instruction
796 * or directly match lots of bits. In this case, matching lots of
797 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000798 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000799 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000800static int emulate_string_inst(struct pt_regs *regs, u32 instword)
801{
802 u8 rT = (instword >> 21) & 0x1f;
803 u8 rA = (instword >> 16) & 0x1f;
804 u8 NB_RB = (instword >> 11) & 0x1f;
805 u32 num_bytes;
806 unsigned long EA;
807 int pos = 0;
808
809 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000810 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000811 if ((rT == rA) || (rT == NB_RB))
812 return -EINVAL;
813
814 EA = (rA == 0) ? 0 : regs->gpr[rA];
815
Kumar Gala16c57b32009-02-10 20:10:44 +0000816 switch (instword & PPC_INST_STRING_MASK) {
817 case PPC_INST_LSWX:
818 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000819 EA += NB_RB;
820 num_bytes = regs->xer & 0x7f;
821 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000822 case PPC_INST_LSWI:
823 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000824 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
825 break;
826 default:
827 return -EINVAL;
828 }
829
830 while (num_bytes != 0)
831 {
832 u8 val;
833 u32 shift = 8 * (3 - (pos & 0x3));
834
Kumar Gala16c57b32009-02-10 20:10:44 +0000835 switch ((instword & PPC_INST_STRING_MASK)) {
836 case PPC_INST_LSWX:
837 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000838 if (get_user(val, (u8 __user *)EA))
839 return -EFAULT;
840 /* first time updating this reg,
841 * zero it out */
842 if (pos == 0)
843 regs->gpr[rT] = 0;
844 regs->gpr[rT] |= val << shift;
845 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000846 case PPC_INST_STSWI:
847 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000848 val = regs->gpr[rT] >> shift;
849 if (put_user(val, (u8 __user *)EA))
850 return -EFAULT;
851 break;
852 }
853 /* move EA to next address */
854 EA += 1;
855 num_bytes--;
856
857 /* manage our position within the register */
858 if (++pos == 4) {
859 pos = 0;
860 if (++rT == 32)
861 rT = 0;
862 }
863 }
864
865 return 0;
866}
867
Will Schmidtc3412dc2006-08-30 13:11:38 -0500868static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
869{
870 u32 ra,rs;
871 unsigned long tmp;
872
873 ra = (instword >> 16) & 0x1f;
874 rs = (instword >> 21) & 0x1f;
875
876 tmp = regs->gpr[rs];
877 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
878 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
879 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
880 regs->gpr[ra] = tmp;
881
882 return 0;
883}
884
Kumar Galac1469f12007-11-19 21:35:29 -0600885static int emulate_isel(struct pt_regs *regs, u32 instword)
886{
887 u8 rT = (instword >> 21) & 0x1f;
888 u8 rA = (instword >> 16) & 0x1f;
889 u8 rB = (instword >> 11) & 0x1f;
890 u8 BC = (instword >> 6) & 0x1f;
891 u8 bit;
892 unsigned long tmp;
893
894 tmp = (rA == 0) ? 0 : regs->gpr[rA];
895 bit = (regs->ccr >> (31 - BC)) & 0x1;
896
897 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
898
899 return 0;
900}
901
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000902static int emulate_instruction(struct pt_regs *regs)
903{
904 u32 instword;
905 u32 rd;
906
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000907 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000908 return -EINVAL;
909 CHECK_FULL_REGS(regs);
910
911 if (get_user(instword, (u32 __user *)(regs->nip)))
912 return -EFAULT;
913
914 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000915 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000916 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000917 rd = (instword >> 21) & 0x1f;
918 regs->gpr[rd] = mfspr(SPRN_PVR);
919 return 0;
920 }
921
922 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000923 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000924 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000925 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000926 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000927
928 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000929 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000930 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000931 unsigned long msk = 0xf0000000UL >> shift;
932
Anton Blanchardeecff812009-10-27 18:46:55 +0000933 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000934 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
935 regs->xer &= ~0xf0000000UL;
936 return 0;
937 }
938
939 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000940 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000941 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000942 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000943 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000944
Will Schmidtc3412dc2006-08-30 13:11:38 -0500945 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000946 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000947 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500948 return emulate_popcntb_inst(regs, instword);
949 }
950
Kumar Galac1469f12007-11-19 21:35:29 -0600951 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000952 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000953 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600954 return emulate_isel(regs, instword);
955 }
956
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000957#ifdef CONFIG_PPC64
958 /* Emulate the mfspr rD, DSCR. */
959 if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
960 cpu_has_feature(CPU_FTR_DSCR)) {
961 PPC_WARN_EMULATED(mfdscr, regs);
962 rd = (instword >> 21) & 0x1f;
963 regs->gpr[rd] = mfspr(SPRN_DSCR);
964 return 0;
965 }
966 /* Emulate the mtspr DSCR, rD. */
967 if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
968 cpu_has_feature(CPU_FTR_DSCR)) {
969 PPC_WARN_EMULATED(mtdscr, regs);
970 rd = (instword >> 21) & 0x1f;
971 mtspr(SPRN_DSCR, regs->gpr[rd]);
972 current->thread.dscr_inherit = 1;
973 return 0;
974 }
975#endif
976
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000977 return -EINVAL;
978}
979
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800980int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000981{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800982 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000983}
984
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000985void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000986{
987 unsigned int reason = get_reason(regs);
988 extern int do_mathemu(struct pt_regs *regs);
989
Kim Phillipsaa42c692006-12-08 02:43:30 -0600990 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600991 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000992
993 if (reason & REASON_FP) {
994 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000995 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000996 return;
997 }
998 if (reason & REASON_TRAP) {
Jason Wesselba797b22010-05-20 21:04:25 -0500999 /* Debugger is first in line to stop recursive faults in
1000 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1001 if (debugger_bpt(regs))
1002 return;
1003
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001004 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001005 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1006 == NOTIFY_STOP)
1007 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -08001008
1009 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -07001010 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001011 regs->nip += 4;
1012 return;
1013 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001014 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1015 return;
1016 }
1017
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001018 local_irq_enable();
1019
Kumar Gala04903a32007-02-07 01:13:32 -06001020#ifdef CONFIG_MATH_EMULATION
1021 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1022 * but there seems to be a hardware bug on the 405GP (RevD)
1023 * that means ESR is sometimes set incorrectly - either to
1024 * ESR_DST (!?) or 0. In the process of chasing this with the
1025 * hardware people - not sure if it can happen on any illegal
1026 * instruction or only on FP instructions, whether there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001027 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -06001028 switch (do_mathemu(regs)) {
1029 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -06001030 emulate_single_step(regs);
1031 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001032 case 1: {
1033 int code = 0;
1034 code = __parse_fpscr(current->thread.fpscr.val);
1035 _exception(SIGFPE, regs, code, regs->nip);
1036 return;
1037 }
1038 case -EFAULT:
1039 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1040 return;
Kumar Gala04903a32007-02-07 01:13:32 -06001041 }
Kumar Gala5fad2932007-02-07 01:47:59 -06001042 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -06001043#endif /* CONFIG_MATH_EMULATION */
1044
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001045 /* Try to emulate it if we should. */
1046 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001047 switch (emulate_instruction(regs)) {
1048 case 0:
1049 regs->nip += 4;
1050 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001051 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001052 case -EFAULT:
1053 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001054 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001055 }
1056 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001057
1058 if (reason & REASON_PRIVILEGED)
1059 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1060 else
1061 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001062}
1063
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001064void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001065{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001066 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001067
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001068 /* we don't implement logging of alignment exceptions */
1069 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1070 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001071
1072 if (fixed == 1) {
1073 regs->nip += 4; /* skip over emulated instruction */
1074 emulate_single_step(regs);
1075 return;
1076 }
1077
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001078 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001079 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001080 sig = SIGSEGV;
1081 code = SEGV_ACCERR;
1082 } else {
1083 sig = SIGBUS;
1084 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001085 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001086 if (user_mode(regs))
1087 _exception(sig, regs, code, regs->dar);
1088 else
1089 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001090}
1091
1092void StackOverflow(struct pt_regs *regs)
1093{
1094 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1095 current, regs->gpr[1]);
1096 debugger(regs);
1097 show_regs(regs);
1098 panic("kernel stack overflow");
1099}
1100
1101void nonrecoverable_exception(struct pt_regs *regs)
1102{
1103 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1104 regs->nip, regs->msr);
1105 debugger(regs);
1106 die("nonrecoverable exception", regs, SIGKILL);
1107}
1108
1109void trace_syscall(struct pt_regs *regs)
1110{
1111 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -07001112 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001113 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1114}
1115
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001116void kernel_fp_unavailable_exception(struct pt_regs *regs)
1117{
1118 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1119 "%lx at %lx\n", regs->trap, regs->nip);
1120 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1121}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001122
1123void altivec_unavailable_exception(struct pt_regs *regs)
1124{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001125 if (user_mode(regs)) {
1126 /* A user program has executed an altivec instruction,
1127 but this kernel doesn't support altivec. */
1128 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1129 return;
1130 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001131
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001132 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1133 "%lx at %lx\n", regs->trap, regs->nip);
1134 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001135}
1136
Michael Neulingce48b212008-06-25 14:07:18 +10001137void vsx_unavailable_exception(struct pt_regs *regs)
1138{
1139 if (user_mode(regs)) {
1140 /* A user program has executed an vsx instruction,
1141 but this kernel doesn't support vsx. */
1142 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1143 return;
1144 }
1145
1146 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1147 "%lx at %lx\n", regs->trap, regs->nip);
1148 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1149}
1150
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001151void performance_monitor_exception(struct pt_regs *regs)
1152{
Anton Blanchard89713ed2010-01-31 20:34:06 +00001153 __get_cpu_var(irq_stat).pmu_irqs++;
1154
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001155 perf_irq(regs);
1156}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001157
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001158#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001159void SoftwareEmulation(struct pt_regs *regs)
1160{
1161 extern int do_mathemu(struct pt_regs *);
1162 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -05001163#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001164 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -05001165#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001166
1167 CHECK_FULL_REGS(regs);
1168
1169 if (!user_mode(regs)) {
1170 debugger(regs);
1171 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1172 }
1173
1174#ifdef CONFIG_MATH_EMULATION
1175 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001176 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001177 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001178
1179 switch (errcode) {
1180 case 0:
1181 emulate_single_step(regs);
1182 return;
1183 case 1: {
1184 int code = 0;
1185 code = __parse_fpscr(current->thread.fpscr.val);
1186 _exception(SIGFPE, regs, code, regs->nip);
1187 return;
1188 }
1189 case -EFAULT:
1190 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1191 return;
1192 default:
1193 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1194 return;
1195 }
1196
Scott Wood5dd57a12007-09-18 15:29:35 -05001197#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001198 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001199 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001200 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001201
Kumar Gala5fad2932007-02-07 01:47:59 -06001202 switch (errcode) {
1203 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001204 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001205 return;
1206 case 1:
1207 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1208 return;
1209 case -EFAULT:
1210 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1211 return;
1212 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001213#else
1214 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001215#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001216}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001217#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001218
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001219#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001220static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1221{
1222 int changed = 0;
1223 /*
1224 * Determine the cause of the debug event, clear the
1225 * event flags and send a trap to the handler. Torez
1226 */
1227 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1228 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1229#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1230 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1231#endif
1232 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1233 5);
1234 changed |= 0x01;
1235 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1236 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1237 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1238 6);
1239 changed |= 0x01;
1240 } else if (debug_status & DBSR_IAC1) {
1241 current->thread.dbcr0 &= ~DBCR0_IAC1;
1242 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1243 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1244 1);
1245 changed |= 0x01;
1246 } else if (debug_status & DBSR_IAC2) {
1247 current->thread.dbcr0 &= ~DBCR0_IAC2;
1248 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1249 2);
1250 changed |= 0x01;
1251 } else if (debug_status & DBSR_IAC3) {
1252 current->thread.dbcr0 &= ~DBCR0_IAC3;
1253 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1254 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1255 3);
1256 changed |= 0x01;
1257 } else if (debug_status & DBSR_IAC4) {
1258 current->thread.dbcr0 &= ~DBCR0_IAC4;
1259 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1260 4);
1261 changed |= 0x01;
1262 }
1263 /*
1264 * At the point this routine was called, the MSR(DE) was turned off.
1265 * Check all other debug flags and see if that bit needs to be turned
1266 * back on or not.
1267 */
1268 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1269 regs->msr |= MSR_DE;
1270 else
1271 /* Make sure the IDM flag is off */
1272 current->thread.dbcr0 &= ~DBCR0_IDM;
1273
1274 if (changed & 0x01)
1275 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1276}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001277
Kumar Galaf8279622008-06-26 02:01:37 -05001278void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001279{
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001280 current->thread.dbsr = debug_status;
1281
Roland McGrathec097c82009-05-28 21:26:38 +00001282 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1283 * on server, it stops on the target of the branch. In order to simulate
1284 * the server behaviour, we thus restart right away with a single step
1285 * instead of stopping here when hitting a BT
1286 */
1287 if (debug_status & DBSR_BT) {
1288 regs->msr &= ~MSR_DE;
1289
1290 /* Disable BT */
1291 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1292 /* Clear the BT event */
1293 mtspr(SPRN_DBSR, DBSR_BT);
1294
1295 /* Do the single step trick only when coming from userspace */
1296 if (user_mode(regs)) {
1297 current->thread.dbcr0 &= ~DBCR0_BT;
1298 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1299 regs->msr |= MSR_DE;
1300 return;
1301 }
1302
1303 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1304 5, SIGTRAP) == NOTIFY_STOP) {
1305 return;
1306 }
1307 if (debugger_sstep(regs))
1308 return;
1309 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001310 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001311
1312 /* Disable instruction completion */
1313 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1314 /* Clear the instruction completion event */
1315 mtspr(SPRN_DBSR, DBSR_IC);
1316
1317 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1318 5, SIGTRAP) == NOTIFY_STOP) {
1319 return;
1320 }
1321
1322 if (debugger_sstep(regs))
1323 return;
1324
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001325 if (user_mode(regs)) {
1326 current->thread.dbcr0 &= ~DBCR0_IC;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001327 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1328 current->thread.dbcr1))
1329 regs->msr |= MSR_DE;
1330 else
1331 /* Make sure the IDM bit is off */
1332 current->thread.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001333 }
Kumar Galaf8279622008-06-26 02:01:37 -05001334
1335 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001336 } else
1337 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001338}
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001339#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001340
1341#if !defined(CONFIG_TAU_INT)
1342void TAUException(struct pt_regs *regs)
1343{
1344 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1345 regs->nip, regs->msr, regs->trap, print_tainted());
1346}
1347#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001348
1349#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001350void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001351{
1352 int err;
1353
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001354 if (!user_mode(regs)) {
1355 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1356 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001357 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001358 }
1359
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001360 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001361
Anton Blanchardeecff812009-10-27 18:46:55 +00001362 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001363 err = emulate_altivec(regs);
1364 if (err == 0) {
1365 regs->nip += 4; /* skip emulated instruction */
1366 emulate_single_step(regs);
1367 return;
1368 }
1369
1370 if (err == -EFAULT) {
1371 /* got an error reading the instruction */
1372 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1373 } else {
1374 /* didn't recognize the instruction */
1375 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001376 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1377 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001378 current->thread.vscr.u[3] |= 0x10000;
1379 }
1380}
1381#endif /* CONFIG_ALTIVEC */
1382
Michael Neulingce48b212008-06-25 14:07:18 +10001383#ifdef CONFIG_VSX
1384void vsx_assist_exception(struct pt_regs *regs)
1385{
1386 if (!user_mode(regs)) {
1387 printk(KERN_EMERG "VSX assist exception in kernel mode"
1388 " at %lx\n", regs->nip);
1389 die("Kernel VSX assist exception", regs, SIGILL);
1390 }
1391
1392 flush_vsx_to_thread(current);
1393 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1394 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1395}
1396#endif /* CONFIG_VSX */
1397
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001398#ifdef CONFIG_FSL_BOOKE
1399void CacheLockingException(struct pt_regs *regs, unsigned long address,
1400 unsigned long error_code)
1401{
1402 /* We treat cache locking instructions from the user
1403 * as priv ops, in the future we could try to do
1404 * something smarter
1405 */
1406 if (error_code & (ESR_DLK|ESR_ILK))
1407 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1408 return;
1409}
1410#endif /* CONFIG_FSL_BOOKE */
1411
1412#ifdef CONFIG_SPE
1413void SPEFloatingPointException(struct pt_regs *regs)
1414{
Liu Yu6a800f32008-10-28 11:50:21 +08001415 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001416 unsigned long spefscr;
1417 int fpexc_mode;
1418 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001419 int err;
1420
yu liu685659e2011-06-14 18:34:25 -05001421 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001422
1423 spefscr = current->thread.spefscr;
1424 fpexc_mode = current->thread.fpexc_mode;
1425
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001426 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1427 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001428 }
1429 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1430 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001431 }
1432 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1433 code = FPE_FLTDIV;
1434 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1435 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001436 }
1437 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1438 code = FPE_FLTRES;
1439
Liu Yu6a800f32008-10-28 11:50:21 +08001440 err = do_spe_mathemu(regs);
1441 if (err == 0) {
1442 regs->nip += 4; /* skip emulated instruction */
1443 emulate_single_step(regs);
1444 return;
1445 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001446
Liu Yu6a800f32008-10-28 11:50:21 +08001447 if (err == -EFAULT) {
1448 /* got an error reading the instruction */
1449 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1450 } else if (err == -EINVAL) {
1451 /* didn't recognize the instruction */
1452 printk(KERN_ERR "unrecognized spe instruction "
1453 "in %s at %lx\n", current->comm, regs->nip);
1454 } else {
1455 _exception(SIGFPE, regs, code, regs->nip);
1456 }
1457
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001458 return;
1459}
Liu Yu6a800f32008-10-28 11:50:21 +08001460
1461void SPEFloatingPointRoundException(struct pt_regs *regs)
1462{
1463 extern int speround_handler(struct pt_regs *regs);
1464 int err;
1465
1466 preempt_disable();
1467 if (regs->msr & MSR_SPE)
1468 giveup_spe(current);
1469 preempt_enable();
1470
1471 regs->nip -= 4;
1472 err = speround_handler(regs);
1473 if (err == 0) {
1474 regs->nip += 4; /* skip emulated instruction */
1475 emulate_single_step(regs);
1476 return;
1477 }
1478
1479 if (err == -EFAULT) {
1480 /* got an error reading the instruction */
1481 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1482 } else if (err == -EINVAL) {
1483 /* didn't recognize the instruction */
1484 printk(KERN_ERR "unrecognized spe instruction "
1485 "in %s at %lx\n", current->comm, regs->nip);
1486 } else {
1487 _exception(SIGFPE, regs, 0, regs->nip);
1488 return;
1489 }
1490}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001491#endif
1492
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001493/*
1494 * We enter here if we get an unrecoverable exception, that is, one
1495 * that happened at a point where the RI (recoverable interrupt) bit
1496 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1497 * we therefore lost state by taking this exception.
1498 */
1499void unrecoverable_exception(struct pt_regs *regs)
1500{
1501 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1502 regs->trap, regs->nip);
1503 die("Unrecoverable exception", regs, SIGABRT);
1504}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001505
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001506#ifdef CONFIG_BOOKE_WDT
1507/*
1508 * Default handler for a Watchdog exception,
1509 * spins until a reboot occurs
1510 */
1511void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1512{
1513 /* Generic WatchdogHandler, implement your own */
1514 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1515 return;
1516}
1517
1518void WatchdogException(struct pt_regs *regs)
1519{
1520 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1521 WatchdogHandler(regs);
1522}
1523#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001524
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001525/*
1526 * We enter here if we discover during exception entry that we are
1527 * running in supervisor mode with a userspace value in the stack pointer.
1528 */
1529void kernel_bad_stack(struct pt_regs *regs)
1530{
1531 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1532 regs->gpr[1], regs->nip);
1533 die("Bad kernel stack pointer", regs, SIGABRT);
1534}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001535
1536void __init trap_init(void)
1537{
1538}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001539
1540
1541#ifdef CONFIG_PPC_EMULATED_STATS
1542
1543#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1544
1545struct ppc_emulated ppc_emulated = {
1546#ifdef CONFIG_ALTIVEC
1547 WARN_EMULATED_SETUP(altivec),
1548#endif
1549 WARN_EMULATED_SETUP(dcba),
1550 WARN_EMULATED_SETUP(dcbz),
1551 WARN_EMULATED_SETUP(fp_pair),
1552 WARN_EMULATED_SETUP(isel),
1553 WARN_EMULATED_SETUP(mcrxr),
1554 WARN_EMULATED_SETUP(mfpvr),
1555 WARN_EMULATED_SETUP(multiple),
1556 WARN_EMULATED_SETUP(popcntb),
1557 WARN_EMULATED_SETUP(spe),
1558 WARN_EMULATED_SETUP(string),
1559 WARN_EMULATED_SETUP(unaligned),
1560#ifdef CONFIG_MATH_EMULATION
1561 WARN_EMULATED_SETUP(math),
1562#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1563 WARN_EMULATED_SETUP(8xx),
1564#endif
1565#ifdef CONFIG_VSX
1566 WARN_EMULATED_SETUP(vsx),
1567#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001568#ifdef CONFIG_PPC64
1569 WARN_EMULATED_SETUP(mfdscr),
1570 WARN_EMULATED_SETUP(mtdscr),
1571#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001572};
1573
1574u32 ppc_warn_emulated;
1575
1576void ppc_warn_emulated_print(const char *type)
1577{
Christian Dietrich76462232011-06-04 05:36:54 +00001578 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1579 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001580}
1581
1582static int __init ppc_warn_emulated_init(void)
1583{
1584 struct dentry *dir, *d;
1585 unsigned int i;
1586 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1587
1588 if (!powerpc_debugfs_root)
1589 return -ENODEV;
1590
1591 dir = debugfs_create_dir("emulated_instructions",
1592 powerpc_debugfs_root);
1593 if (!dir)
1594 return -ENOMEM;
1595
1596 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1597 &ppc_warn_emulated);
1598 if (!d)
1599 goto fail;
1600
1601 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1602 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1603 (u32 *)&entries[i].val.counter);
1604 if (!d)
1605 goto fail;
1606 }
1607
1608 return 0;
1609
1610fail:
1611 debugfs_remove_recursive(dir);
1612 return -ENOMEM;
1613}
1614
1615device_initcall(ppc_warn_emulated_init);
1616
1617#endif /* CONFIG_PPC_EMULATED_STATS */