blob: 4e5908264d1a98819a6713fe23f4d5a23032db41 [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);
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000161 raw_spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700162
163 if (kexec_should_crash(current) ||
164 kexec_sr_activated(smp_processor_id()))
165 crash_kexec(regs);
166 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000167
168 if (in_interrupt())
169 panic("Fatal exception in interrupt");
170
Hormscea6a4b2006-07-30 03:03:34 -0700171 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700172 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700173
anton@samba.org293e4682007-03-20 20:38:11 -0500174 oops_exit();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000175 do_exit(err);
176
177 return 0;
178}
179
Oleg Nesterov25baa352009-12-15 16:47:18 -0800180void user_single_step_siginfo(struct task_struct *tsk,
181 struct pt_regs *regs, siginfo_t *info)
182{
183 memset(info, 0, sizeof(*info));
184 info->si_signo = SIGTRAP;
185 info->si_code = TRAP_TRACE;
186 info->si_addr = (void __user *)regs->nip;
187}
188
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000189void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
190{
191 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000192 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
193 "at %08lx nip %08lx lr %08lx code %x\n";
194 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
195 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000196
197 if (!user_mode(regs)) {
198 if (die("Exception in kernel mode", regs, signr))
199 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000200 } else if (show_unhandled_signals &&
Christian Dietrich76462232011-06-04 05:36:54 +0000201 unhandled_signal(current, signr)) {
202 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
203 current->comm, current->pid, signr,
204 addr, regs->nip, regs->link, code);
205 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000206
207 memset(&info, 0, sizeof(info));
208 info.si_signo = signr;
209 info.si_code = code;
210 info.si_addr = (void __user *) addr;
211 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000212}
213
214#ifdef CONFIG_PPC64
215void system_reset_exception(struct pt_regs *regs)
216{
217 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000218 if (ppc_md.system_reset_exception) {
219 if (ppc_md.system_reset_exception(regs))
220 return;
221 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000222
David Wilderc0ce7d02006-06-23 15:29:34 -0700223#ifdef CONFIG_KEXEC
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000224 cpumask_set_cpu(smp_processor_id(), &cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700225#endif
226
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000227 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000228
David Wildereac83922006-06-29 15:17:30 -0700229 /*
230 * Some CPUs when released from the debugger will execute this path.
231 * These CPUs entered the debugger via a soft-reset. If the CPU was
232 * hung before entering the debugger it will return to the hung
233 * state when exiting this function. This causes a problem in
234 * kdump since the hung CPU(s) will not respond to the IPI sent
235 * from kdump. To prevent the problem we call crash_kexec_secondary()
236 * here. If a kdump had not been initiated or we exit the debugger
237 * with the "exit and recover" command (x) crash_kexec_secondary()
238 * will return after 5ms and the CPU returns to its previous state.
239 */
240 crash_kexec_secondary(regs);
241
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000242 /* Must die if the interrupt is not recoverable */
243 if (!(regs->msr & MSR_RI))
244 panic("Unrecoverable System Reset");
245
246 /* What should we do here? We could issue a shutdown or hard reset. */
247}
248#endif
249
250/*
251 * I/O accesses can cause machine checks on powermacs.
252 * Check if the NIP corresponds to the address of a sync
253 * instruction for which there is an entry in the exception
254 * table.
255 * Note that the 601 only takes a machine check on TEA
256 * (transfer error ack) signal assertion, and does not
257 * set any of the top 16 bits of SRR1.
258 * -- paulus.
259 */
260static inline int check_io_access(struct pt_regs *regs)
261{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100262#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000263 unsigned long msr = regs->msr;
264 const struct exception_table_entry *entry;
265 unsigned int *nip = (unsigned int *)regs->nip;
266
267 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
268 && (entry = search_exception_tables(regs->nip)) != NULL) {
269 /*
270 * Check that it's a sync instruction, or somewhere
271 * in the twi; isync; nop sequence that inb/inw/inl uses.
272 * As the address is in the exception table
273 * we should be able to read the instr there.
274 * For the debug message, we look at the preceding
275 * load or store.
276 */
277 if (*nip == 0x60000000) /* nop */
278 nip -= 2;
279 else if (*nip == 0x4c00012c) /* isync */
280 --nip;
281 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
282 /* sync or twi */
283 unsigned int rb;
284
285 --nip;
286 rb = (*nip >> 11) & 0x1f;
287 printk(KERN_DEBUG "%s bad port %lx at %p\n",
288 (*nip & 0x100)? "OUT to": "IN from",
289 regs->gpr[rb] - _IO_BASE, nip);
290 regs->msr |= MSR_RI;
291 regs->nip = entry->fixup;
292 return 1;
293 }
294 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100295#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296 return 0;
297}
298
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000299#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000300/* On 4xx, the reason for the machine check or program exception
301 is in the ESR. */
302#define get_reason(regs) ((regs)->dsisr)
303#ifndef CONFIG_FSL_BOOKE
304#define get_mc_reason(regs) ((regs)->dsisr)
305#else
Scott Woodfe04b112010-04-08 00:38:22 -0500306#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000307#endif
308#define REASON_FP ESR_FP
309#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
310#define REASON_PRIVILEGED ESR_PPR
311#define REASON_TRAP ESR_PTR
312
313/* single-step stuff */
314#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
315#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
316
317#else
318/* On non-4xx, the reason for the machine check or program
319 exception is in the MSR. */
320#define get_reason(regs) ((regs)->msr)
321#define get_mc_reason(regs) ((regs)->msr)
322#define REASON_FP 0x100000
323#define REASON_ILLEGAL 0x80000
324#define REASON_PRIVILEGED 0x40000
325#define REASON_TRAP 0x20000
326
327#define single_stepping(regs) ((regs)->msr & MSR_SE)
328#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
329#endif
330
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100331#if defined(CONFIG_4xx)
332int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000333{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600334 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000335
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000336 if (reason & ESR_IMCP) {
337 printk("Instruction");
338 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
339 } else
340 printk("Data");
341 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100342
343 return 0;
344}
345
346int machine_check_440A(struct pt_regs *regs)
347{
348 unsigned long reason = get_mc_reason(regs);
349
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000350 printk("Machine check in kernel mode.\n");
351 if (reason & ESR_IMCP){
352 printk("Instruction Synchronous Machine Check exception\n");
353 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
354 }
355 else {
356 u32 mcsr = mfspr(SPRN_MCSR);
357 if (mcsr & MCSR_IB)
358 printk("Instruction Read PLB Error\n");
359 if (mcsr & MCSR_DRB)
360 printk("Data Read PLB Error\n");
361 if (mcsr & MCSR_DWB)
362 printk("Data Write PLB Error\n");
363 if (mcsr & MCSR_TLBP)
364 printk("TLB Parity Error\n");
365 if (mcsr & MCSR_ICP){
366 flush_instruction_cache();
367 printk("I-Cache Parity Error\n");
368 }
369 if (mcsr & MCSR_DCSP)
370 printk("D-Cache Search Parity Error\n");
371 if (mcsr & MCSR_DCFP)
372 printk("D-Cache Flush Parity Error\n");
373 if (mcsr & MCSR_IMPE)
374 printk("Machine Check exception is imprecise\n");
375
376 /* Clear MCSR */
377 mtspr(SPRN_MCSR, mcsr);
378 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100379 return 0;
380}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000381
382int machine_check_47x(struct pt_regs *regs)
383{
384 unsigned long reason = get_mc_reason(regs);
385 u32 mcsr;
386
387 printk(KERN_ERR "Machine check in kernel mode.\n");
388 if (reason & ESR_IMCP) {
389 printk(KERN_ERR
390 "Instruction Synchronous Machine Check exception\n");
391 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
392 return 0;
393 }
394 mcsr = mfspr(SPRN_MCSR);
395 if (mcsr & MCSR_IB)
396 printk(KERN_ERR "Instruction Read PLB Error\n");
397 if (mcsr & MCSR_DRB)
398 printk(KERN_ERR "Data Read PLB Error\n");
399 if (mcsr & MCSR_DWB)
400 printk(KERN_ERR "Data Write PLB Error\n");
401 if (mcsr & MCSR_TLBP)
402 printk(KERN_ERR "TLB Parity Error\n");
403 if (mcsr & MCSR_ICP) {
404 flush_instruction_cache();
405 printk(KERN_ERR "I-Cache Parity Error\n");
406 }
407 if (mcsr & MCSR_DCSP)
408 printk(KERN_ERR "D-Cache Search Parity Error\n");
409 if (mcsr & PPC47x_MCSR_GPR)
410 printk(KERN_ERR "GPR Parity Error\n");
411 if (mcsr & PPC47x_MCSR_FPR)
412 printk(KERN_ERR "FPR Parity Error\n");
413 if (mcsr & PPC47x_MCSR_IPR)
414 printk(KERN_ERR "Machine Check exception is imprecise\n");
415
416 /* Clear MCSR */
417 mtspr(SPRN_MCSR, mcsr);
418
419 return 0;
420}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100421#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500422int machine_check_e500mc(struct pt_regs *regs)
423{
424 unsigned long mcsr = mfspr(SPRN_MCSR);
425 unsigned long reason = mcsr;
426 int recoverable = 1;
427
Scott Wood82a9a482011-06-16 14:09:17 -0500428 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800429 recoverable = fsl_rio_mcheck_exception(regs);
430 if (recoverable == 1)
431 goto silent_out;
432 }
433
Scott Woodfe04b112010-04-08 00:38:22 -0500434 printk("Machine check in kernel mode.\n");
435 printk("Caused by (from MCSR=%lx): ", reason);
436
437 if (reason & MCSR_MCP)
438 printk("Machine Check Signal\n");
439
440 if (reason & MCSR_ICPERR) {
441 printk("Instruction Cache Parity Error\n");
442
443 /*
444 * This is recoverable by invalidating the i-cache.
445 */
446 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
447 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
448 ;
449
450 /*
451 * This will generally be accompanied by an instruction
452 * fetch error report -- only treat MCSR_IF as fatal
453 * if it wasn't due to an L1 parity error.
454 */
455 reason &= ~MCSR_IF;
456 }
457
458 if (reason & MCSR_DCPERR_MC) {
459 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500460
461 /*
462 * In write shadow mode we auto-recover from the error, but it
463 * may still get logged and cause a machine check. We should
464 * only treat the non-write shadow case as non-recoverable.
465 */
466 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
467 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500468 }
469
470 if (reason & MCSR_L2MMU_MHIT) {
471 printk("Hit on multiple TLB entries\n");
472 recoverable = 0;
473 }
474
475 if (reason & MCSR_NMI)
476 printk("Non-maskable interrupt\n");
477
478 if (reason & MCSR_IF) {
479 printk("Instruction Fetch Error Report\n");
480 recoverable = 0;
481 }
482
483 if (reason & MCSR_LD) {
484 printk("Load Error Report\n");
485 recoverable = 0;
486 }
487
488 if (reason & MCSR_ST) {
489 printk("Store Error Report\n");
490 recoverable = 0;
491 }
492
493 if (reason & MCSR_LDG) {
494 printk("Guarded Load Error Report\n");
495 recoverable = 0;
496 }
497
498 if (reason & MCSR_TLBSYNC)
499 printk("Simultaneous tlbsync operations\n");
500
501 if (reason & MCSR_BSL2_ERR) {
502 printk("Level 2 Cache Error\n");
503 recoverable = 0;
504 }
505
506 if (reason & MCSR_MAV) {
507 u64 addr;
508
509 addr = mfspr(SPRN_MCAR);
510 addr |= (u64)mfspr(SPRN_MCARU) << 32;
511
512 printk("Machine Check %s Address: %#llx\n",
513 reason & MCSR_MEA ? "Effective" : "Physical", addr);
514 }
515
Shaohui Xiecce1f102010-11-18 14:57:32 +0800516silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500517 mtspr(SPRN_MCSR, mcsr);
518 return mfspr(SPRN_MCSR) == 0 && recoverable;
519}
520
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100521int machine_check_e500(struct pt_regs *regs)
522{
523 unsigned long reason = get_mc_reason(regs);
524
Shaohui Xiecce1f102010-11-18 14:57:32 +0800525 if (reason & MCSR_BUS_RBERR) {
526 if (fsl_rio_mcheck_exception(regs))
527 return 1;
528 }
529
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000530 printk("Machine check in kernel mode.\n");
531 printk("Caused by (from MCSR=%lx): ", reason);
532
533 if (reason & MCSR_MCP)
534 printk("Machine Check Signal\n");
535 if (reason & MCSR_ICPERR)
536 printk("Instruction Cache Parity Error\n");
537 if (reason & MCSR_DCP_PERR)
538 printk("Data Cache Push Parity Error\n");
539 if (reason & MCSR_DCPERR)
540 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000541 if (reason & MCSR_BUS_IAERR)
542 printk("Bus - Instruction Address Error\n");
543 if (reason & MCSR_BUS_RAERR)
544 printk("Bus - Read Address Error\n");
545 if (reason & MCSR_BUS_WAERR)
546 printk("Bus - Write Address Error\n");
547 if (reason & MCSR_BUS_IBERR)
548 printk("Bus - Instruction Data Error\n");
549 if (reason & MCSR_BUS_RBERR)
550 printk("Bus - Read Data Bus Error\n");
551 if (reason & MCSR_BUS_WBERR)
552 printk("Bus - Read Data Bus Error\n");
553 if (reason & MCSR_BUS_IPERR)
554 printk("Bus - Instruction Parity Error\n");
555 if (reason & MCSR_BUS_RPERR)
556 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100557
558 return 0;
559}
Kumar Gala4490c062010-10-08 08:32:11 -0500560
561int machine_check_generic(struct pt_regs *regs)
562{
563 return 0;
564}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100565#elif defined(CONFIG_E200)
566int machine_check_e200(struct pt_regs *regs)
567{
568 unsigned long reason = get_mc_reason(regs);
569
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000570 printk("Machine check in kernel mode.\n");
571 printk("Caused by (from MCSR=%lx): ", reason);
572
573 if (reason & MCSR_MCP)
574 printk("Machine Check Signal\n");
575 if (reason & MCSR_CP_PERR)
576 printk("Cache Push Parity Error\n");
577 if (reason & MCSR_CPERR)
578 printk("Cache Parity Error\n");
579 if (reason & MCSR_EXCP_ERR)
580 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
581 if (reason & MCSR_BUS_IRERR)
582 printk("Bus - Read Bus Error on instruction fetch\n");
583 if (reason & MCSR_BUS_DRERR)
584 printk("Bus - Read Bus Error on data load\n");
585 if (reason & MCSR_BUS_WRERR)
586 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100587
588 return 0;
589}
590#else
591int machine_check_generic(struct pt_regs *regs)
592{
593 unsigned long reason = get_mc_reason(regs);
594
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000595 printk("Machine check in kernel mode.\n");
596 printk("Caused by (from SRR1=%lx): ", reason);
597 switch (reason & 0x601F0000) {
598 case 0x80000:
599 printk("Machine check signal\n");
600 break;
601 case 0: /* for 601 */
602 case 0x40000:
603 case 0x140000: /* 7450 MSS error and TEA */
604 printk("Transfer error ack signal\n");
605 break;
606 case 0x20000:
607 printk("Data parity error signal\n");
608 break;
609 case 0x10000:
610 printk("Address parity error signal\n");
611 break;
612 case 0x20000000:
613 printk("L1 Data Cache error\n");
614 break;
615 case 0x40000000:
616 printk("L1 Instruction Cache error\n");
617 break;
618 case 0x00100000:
619 printk("L2 data cache parity error\n");
620 break;
621 default:
622 printk("Unknown values in msr\n");
623 }
Olof Johansson75918a42007-09-21 05:11:20 +1000624 return 0;
625}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100626#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000627
628void machine_check_exception(struct pt_regs *regs)
629{
630 int recover = 0;
631
Anton Blanchard89713ed2010-01-31 20:34:06 +0000632 __get_cpu_var(irq_stat).mce_exceptions++;
633
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100634 /* See if any machine dependent calls. In theory, we would want
635 * to call the CPU first, and call the ppc_md. one if the CPU
636 * one returns a positive number. However there is existing code
637 * that assumes the board gets a first chance, so let's keep it
638 * that way for now and fix things later. --BenH.
639 */
Olof Johansson75918a42007-09-21 05:11:20 +1000640 if (ppc_md.machine_check_exception)
641 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100642 else if (cur_cpu_spec->machine_check)
643 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000644
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100645 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000646 return;
647
Olof Johansson75918a42007-09-21 05:11:20 +1000648#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100649 /* the qspan pci read routines can cause machine checks -- Cort
650 *
651 * yuck !!! that totally needs to go away ! There are better ways
652 * to deal with that than having a wart in the mcheck handler.
653 * -- BenH
654 */
Olof Johansson75918a42007-09-21 05:11:20 +1000655 bad_page_fault(regs, regs->dar, SIGBUS);
656 return;
657#endif
658
Anton Blancharda4435062011-01-11 19:45:31 +0000659 if (debugger_fault_handler(regs))
Olof Johansson75918a42007-09-21 05:11:20 +1000660 return;
Olof Johansson75918a42007-09-21 05:11:20 +1000661
662 if (check_io_access(regs))
663 return;
664
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000665 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000666
667 /* Must die if the interrupt is not recoverable */
668 if (!(regs->msr & MSR_RI))
669 panic("Unrecoverable Machine check");
670}
671
672void SMIException(struct pt_regs *regs)
673{
674 die("System Management Interrupt", regs, SIGABRT);
675}
676
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000677void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000678{
679 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
680 regs->nip, regs->msr, regs->trap);
681
682 _exception(SIGTRAP, regs, 0, 0);
683}
684
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000685void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000686{
687 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
688 5, SIGTRAP) == NOTIFY_STOP)
689 return;
690 if (debugger_iabr_match(regs))
691 return;
692 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
693}
694
695void RunModeException(struct pt_regs *regs)
696{
697 _exception(SIGTRAP, regs, 0, 0);
698}
699
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000700void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000701{
K.Prasad2538c2d2010-06-15 11:35:31 +0530702 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000703
704 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
705 5, SIGTRAP) == NOTIFY_STOP)
706 return;
707 if (debugger_sstep(regs))
708 return;
709
710 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
711}
712
713/*
714 * After we have successfully emulated an instruction, we have to
715 * check if the instruction was being single-stepped, and if so,
716 * pretend we got a single-step exception. This was pointed out
717 * by Kumar Gala. -- paulus
718 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000719static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000720{
K.Prasad2538c2d2010-06-15 11:35:31 +0530721 if (single_stepping(regs))
722 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000723}
724
Kumar Gala5fad2932007-02-07 01:47:59 -0600725static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000726{
Kumar Gala5fad2932007-02-07 01:47:59 -0600727 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000728
729 /* Invalid operation */
730 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600731 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000732
733 /* Overflow */
734 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600735 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000736
737 /* Underflow */
738 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600739 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000740
741 /* Divide by zero */
742 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600743 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000744
745 /* Inexact result */
746 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600747 ret = FPE_FLTRES;
748
749 return ret;
750}
751
752static void parse_fpe(struct pt_regs *regs)
753{
754 int code = 0;
755
756 flush_fp_to_thread(current);
757
758 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000759
760 _exception(SIGFPE, regs, code, regs->nip);
761}
762
763/*
764 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000765 * provide the PVR to user applications using the mfspr rd, PVR.
766 * Return non-zero if we can't emulate, or -EFAULT if the associated
767 * memory access caused an access fault. Return zero on success.
768 *
769 * There are a couple of ways to do this, either "decode" the instruction
770 * or directly match lots of bits. In this case, matching lots of
771 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000772 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000773 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000774static int emulate_string_inst(struct pt_regs *regs, u32 instword)
775{
776 u8 rT = (instword >> 21) & 0x1f;
777 u8 rA = (instword >> 16) & 0x1f;
778 u8 NB_RB = (instword >> 11) & 0x1f;
779 u32 num_bytes;
780 unsigned long EA;
781 int pos = 0;
782
783 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000784 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000785 if ((rT == rA) || (rT == NB_RB))
786 return -EINVAL;
787
788 EA = (rA == 0) ? 0 : regs->gpr[rA];
789
Kumar Gala16c57b32009-02-10 20:10:44 +0000790 switch (instword & PPC_INST_STRING_MASK) {
791 case PPC_INST_LSWX:
792 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000793 EA += NB_RB;
794 num_bytes = regs->xer & 0x7f;
795 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000796 case PPC_INST_LSWI:
797 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000798 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
799 break;
800 default:
801 return -EINVAL;
802 }
803
804 while (num_bytes != 0)
805 {
806 u8 val;
807 u32 shift = 8 * (3 - (pos & 0x3));
808
Kumar Gala16c57b32009-02-10 20:10:44 +0000809 switch ((instword & PPC_INST_STRING_MASK)) {
810 case PPC_INST_LSWX:
811 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812 if (get_user(val, (u8 __user *)EA))
813 return -EFAULT;
814 /* first time updating this reg,
815 * zero it out */
816 if (pos == 0)
817 regs->gpr[rT] = 0;
818 regs->gpr[rT] |= val << shift;
819 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000820 case PPC_INST_STSWI:
821 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000822 val = regs->gpr[rT] >> shift;
823 if (put_user(val, (u8 __user *)EA))
824 return -EFAULT;
825 break;
826 }
827 /* move EA to next address */
828 EA += 1;
829 num_bytes--;
830
831 /* manage our position within the register */
832 if (++pos == 4) {
833 pos = 0;
834 if (++rT == 32)
835 rT = 0;
836 }
837 }
838
839 return 0;
840}
841
Will Schmidtc3412dc2006-08-30 13:11:38 -0500842static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
843{
844 u32 ra,rs;
845 unsigned long tmp;
846
847 ra = (instword >> 16) & 0x1f;
848 rs = (instword >> 21) & 0x1f;
849
850 tmp = regs->gpr[rs];
851 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
852 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
853 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
854 regs->gpr[ra] = tmp;
855
856 return 0;
857}
858
Kumar Galac1469f12007-11-19 21:35:29 -0600859static int emulate_isel(struct pt_regs *regs, u32 instword)
860{
861 u8 rT = (instword >> 21) & 0x1f;
862 u8 rA = (instword >> 16) & 0x1f;
863 u8 rB = (instword >> 11) & 0x1f;
864 u8 BC = (instword >> 6) & 0x1f;
865 u8 bit;
866 unsigned long tmp;
867
868 tmp = (rA == 0) ? 0 : regs->gpr[rA];
869 bit = (regs->ccr >> (31 - BC)) & 0x1;
870
871 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
872
873 return 0;
874}
875
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000876static int emulate_instruction(struct pt_regs *regs)
877{
878 u32 instword;
879 u32 rd;
880
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000881 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000882 return -EINVAL;
883 CHECK_FULL_REGS(regs);
884
885 if (get_user(instword, (u32 __user *)(regs->nip)))
886 return -EFAULT;
887
888 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000889 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000890 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000891 rd = (instword >> 21) & 0x1f;
892 regs->gpr[rd] = mfspr(SPRN_PVR);
893 return 0;
894 }
895
896 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000897 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000898 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000899 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000900 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000901
902 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000903 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000904 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000905 unsigned long msk = 0xf0000000UL >> shift;
906
Anton Blanchardeecff812009-10-27 18:46:55 +0000907 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000908 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
909 regs->xer &= ~0xf0000000UL;
910 return 0;
911 }
912
913 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000914 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000915 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000916 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000917 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000918
Will Schmidtc3412dc2006-08-30 13:11:38 -0500919 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000920 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000921 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500922 return emulate_popcntb_inst(regs, instword);
923 }
924
Kumar Galac1469f12007-11-19 21:35:29 -0600925 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000926 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000927 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600928 return emulate_isel(regs, instword);
929 }
930
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000931#ifdef CONFIG_PPC64
932 /* Emulate the mfspr rD, DSCR. */
933 if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
934 cpu_has_feature(CPU_FTR_DSCR)) {
935 PPC_WARN_EMULATED(mfdscr, regs);
936 rd = (instword >> 21) & 0x1f;
937 regs->gpr[rd] = mfspr(SPRN_DSCR);
938 return 0;
939 }
940 /* Emulate the mtspr DSCR, rD. */
941 if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
942 cpu_has_feature(CPU_FTR_DSCR)) {
943 PPC_WARN_EMULATED(mtdscr, regs);
944 rd = (instword >> 21) & 0x1f;
945 mtspr(SPRN_DSCR, regs->gpr[rd]);
946 current->thread.dscr_inherit = 1;
947 return 0;
948 }
949#endif
950
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000951 return -EINVAL;
952}
953
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800954int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000955{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800956 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000957}
958
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000959void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000960{
961 unsigned int reason = get_reason(regs);
962 extern int do_mathemu(struct pt_regs *regs);
963
Kim Phillipsaa42c692006-12-08 02:43:30 -0600964 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600965 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000966
967 if (reason & REASON_FP) {
968 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000969 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000970 return;
971 }
972 if (reason & REASON_TRAP) {
Jason Wesselba797b22010-05-20 21:04:25 -0500973 /* Debugger is first in line to stop recursive faults in
974 * rcu_lock, notify_die, or atomic_notifier_call_chain */
975 if (debugger_bpt(regs))
976 return;
977
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000978 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000979 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
980 == NOTIFY_STOP)
981 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800982
983 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700984 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000985 regs->nip += 4;
986 return;
987 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000988 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
989 return;
990 }
991
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100992 local_irq_enable();
993
Kumar Gala04903a32007-02-07 01:13:32 -0600994#ifdef CONFIG_MATH_EMULATION
995 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
996 * but there seems to be a hardware bug on the 405GP (RevD)
997 * that means ESR is sometimes set incorrectly - either to
998 * ESR_DST (!?) or 0. In the process of chasing this with the
999 * hardware people - not sure if it can happen on any illegal
1000 * instruction or only on FP instructions, whether there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001001 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -06001002 switch (do_mathemu(regs)) {
1003 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -06001004 emulate_single_step(regs);
1005 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001006 case 1: {
1007 int code = 0;
1008 code = __parse_fpscr(current->thread.fpscr.val);
1009 _exception(SIGFPE, regs, code, regs->nip);
1010 return;
1011 }
1012 case -EFAULT:
1013 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1014 return;
Kumar Gala04903a32007-02-07 01:13:32 -06001015 }
Kumar Gala5fad2932007-02-07 01:47:59 -06001016 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -06001017#endif /* CONFIG_MATH_EMULATION */
1018
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001019 /* Try to emulate it if we should. */
1020 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001021 switch (emulate_instruction(regs)) {
1022 case 0:
1023 regs->nip += 4;
1024 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001025 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001026 case -EFAULT:
1027 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001028 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001029 }
1030 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001031
1032 if (reason & REASON_PRIVILEGED)
1033 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1034 else
1035 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001036}
1037
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001038void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001039{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001040 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001041
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001042 /* we don't implement logging of alignment exceptions */
1043 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1044 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001045
1046 if (fixed == 1) {
1047 regs->nip += 4; /* skip over emulated instruction */
1048 emulate_single_step(regs);
1049 return;
1050 }
1051
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001052 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001053 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001054 sig = SIGSEGV;
1055 code = SEGV_ACCERR;
1056 } else {
1057 sig = SIGBUS;
1058 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001059 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001060 if (user_mode(regs))
1061 _exception(sig, regs, code, regs->dar);
1062 else
1063 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001064}
1065
1066void StackOverflow(struct pt_regs *regs)
1067{
1068 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1069 current, regs->gpr[1]);
1070 debugger(regs);
1071 show_regs(regs);
1072 panic("kernel stack overflow");
1073}
1074
1075void nonrecoverable_exception(struct pt_regs *regs)
1076{
1077 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1078 regs->nip, regs->msr);
1079 debugger(regs);
1080 die("nonrecoverable exception", regs, SIGKILL);
1081}
1082
1083void trace_syscall(struct pt_regs *regs)
1084{
1085 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -07001086 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001087 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1088}
1089
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001090void kernel_fp_unavailable_exception(struct pt_regs *regs)
1091{
1092 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1093 "%lx at %lx\n", regs->trap, regs->nip);
1094 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1095}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001096
1097void altivec_unavailable_exception(struct pt_regs *regs)
1098{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001099 if (user_mode(regs)) {
1100 /* A user program has executed an altivec instruction,
1101 but this kernel doesn't support altivec. */
1102 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1103 return;
1104 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001105
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001106 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1107 "%lx at %lx\n", regs->trap, regs->nip);
1108 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001109}
1110
Michael Neulingce48b212008-06-25 14:07:18 +10001111void vsx_unavailable_exception(struct pt_regs *regs)
1112{
1113 if (user_mode(regs)) {
1114 /* A user program has executed an vsx instruction,
1115 but this kernel doesn't support vsx. */
1116 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1117 return;
1118 }
1119
1120 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1121 "%lx at %lx\n", regs->trap, regs->nip);
1122 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1123}
1124
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001125void performance_monitor_exception(struct pt_regs *regs)
1126{
Anton Blanchard89713ed2010-01-31 20:34:06 +00001127 __get_cpu_var(irq_stat).pmu_irqs++;
1128
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001129 perf_irq(regs);
1130}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001131
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001132#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001133void SoftwareEmulation(struct pt_regs *regs)
1134{
1135 extern int do_mathemu(struct pt_regs *);
1136 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -05001137#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001138 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -05001139#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001140
1141 CHECK_FULL_REGS(regs);
1142
1143 if (!user_mode(regs)) {
1144 debugger(regs);
1145 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1146 }
1147
1148#ifdef CONFIG_MATH_EMULATION
1149 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001150 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001151 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001152
1153 switch (errcode) {
1154 case 0:
1155 emulate_single_step(regs);
1156 return;
1157 case 1: {
1158 int code = 0;
1159 code = __parse_fpscr(current->thread.fpscr.val);
1160 _exception(SIGFPE, regs, code, regs->nip);
1161 return;
1162 }
1163 case -EFAULT:
1164 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1165 return;
1166 default:
1167 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1168 return;
1169 }
1170
Scott Wood5dd57a12007-09-18 15:29:35 -05001171#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001172 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001173 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001174 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001175
Kumar Gala5fad2932007-02-07 01:47:59 -06001176 switch (errcode) {
1177 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001178 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001179 return;
1180 case 1:
1181 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1182 return;
1183 case -EFAULT:
1184 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1185 return;
1186 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001187#else
1188 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001189#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001190}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001191#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001192
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001193#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001194static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1195{
1196 int changed = 0;
1197 /*
1198 * Determine the cause of the debug event, clear the
1199 * event flags and send a trap to the handler. Torez
1200 */
1201 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1202 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1203#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1204 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1205#endif
1206 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1207 5);
1208 changed |= 0x01;
1209 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1210 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1211 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1212 6);
1213 changed |= 0x01;
1214 } else if (debug_status & DBSR_IAC1) {
1215 current->thread.dbcr0 &= ~DBCR0_IAC1;
1216 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1217 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1218 1);
1219 changed |= 0x01;
1220 } else if (debug_status & DBSR_IAC2) {
1221 current->thread.dbcr0 &= ~DBCR0_IAC2;
1222 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1223 2);
1224 changed |= 0x01;
1225 } else if (debug_status & DBSR_IAC3) {
1226 current->thread.dbcr0 &= ~DBCR0_IAC3;
1227 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1228 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1229 3);
1230 changed |= 0x01;
1231 } else if (debug_status & DBSR_IAC4) {
1232 current->thread.dbcr0 &= ~DBCR0_IAC4;
1233 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1234 4);
1235 changed |= 0x01;
1236 }
1237 /*
1238 * At the point this routine was called, the MSR(DE) was turned off.
1239 * Check all other debug flags and see if that bit needs to be turned
1240 * back on or not.
1241 */
1242 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1243 regs->msr |= MSR_DE;
1244 else
1245 /* Make sure the IDM flag is off */
1246 current->thread.dbcr0 &= ~DBCR0_IDM;
1247
1248 if (changed & 0x01)
1249 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1250}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001251
Kumar Galaf8279622008-06-26 02:01:37 -05001252void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001253{
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001254 current->thread.dbsr = debug_status;
1255
Roland McGrathec097c82009-05-28 21:26:38 +00001256 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1257 * on server, it stops on the target of the branch. In order to simulate
1258 * the server behaviour, we thus restart right away with a single step
1259 * instead of stopping here when hitting a BT
1260 */
1261 if (debug_status & DBSR_BT) {
1262 regs->msr &= ~MSR_DE;
1263
1264 /* Disable BT */
1265 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1266 /* Clear the BT event */
1267 mtspr(SPRN_DBSR, DBSR_BT);
1268
1269 /* Do the single step trick only when coming from userspace */
1270 if (user_mode(regs)) {
1271 current->thread.dbcr0 &= ~DBCR0_BT;
1272 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1273 regs->msr |= MSR_DE;
1274 return;
1275 }
1276
1277 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1278 5, SIGTRAP) == NOTIFY_STOP) {
1279 return;
1280 }
1281 if (debugger_sstep(regs))
1282 return;
1283 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001284 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001285
1286 /* Disable instruction completion */
1287 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1288 /* Clear the instruction completion event */
1289 mtspr(SPRN_DBSR, DBSR_IC);
1290
1291 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1292 5, SIGTRAP) == NOTIFY_STOP) {
1293 return;
1294 }
1295
1296 if (debugger_sstep(regs))
1297 return;
1298
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001299 if (user_mode(regs)) {
1300 current->thread.dbcr0 &= ~DBCR0_IC;
1301#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1302 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1303 current->thread.dbcr1))
1304 regs->msr |= MSR_DE;
1305 else
1306 /* Make sure the IDM bit is off */
1307 current->thread.dbcr0 &= ~DBCR0_IDM;
1308#endif
1309 }
Kumar Galaf8279622008-06-26 02:01:37 -05001310
1311 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001312 } else
1313 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001314}
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001315#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001316
1317#if !defined(CONFIG_TAU_INT)
1318void TAUException(struct pt_regs *regs)
1319{
1320 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1321 regs->nip, regs->msr, regs->trap, print_tainted());
1322}
1323#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001324
1325#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001326void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001327{
1328 int err;
1329
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001330 if (!user_mode(regs)) {
1331 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1332 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001333 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001334 }
1335
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001336 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001337
Anton Blanchardeecff812009-10-27 18:46:55 +00001338 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001339 err = emulate_altivec(regs);
1340 if (err == 0) {
1341 regs->nip += 4; /* skip emulated instruction */
1342 emulate_single_step(regs);
1343 return;
1344 }
1345
1346 if (err == -EFAULT) {
1347 /* got an error reading the instruction */
1348 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1349 } else {
1350 /* didn't recognize the instruction */
1351 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001352 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1353 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001354 current->thread.vscr.u[3] |= 0x10000;
1355 }
1356}
1357#endif /* CONFIG_ALTIVEC */
1358
Michael Neulingce48b212008-06-25 14:07:18 +10001359#ifdef CONFIG_VSX
1360void vsx_assist_exception(struct pt_regs *regs)
1361{
1362 if (!user_mode(regs)) {
1363 printk(KERN_EMERG "VSX assist exception in kernel mode"
1364 " at %lx\n", regs->nip);
1365 die("Kernel VSX assist exception", regs, SIGILL);
1366 }
1367
1368 flush_vsx_to_thread(current);
1369 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1370 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1371}
1372#endif /* CONFIG_VSX */
1373
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001374#ifdef CONFIG_FSL_BOOKE
1375void CacheLockingException(struct pt_regs *regs, unsigned long address,
1376 unsigned long error_code)
1377{
1378 /* We treat cache locking instructions from the user
1379 * as priv ops, in the future we could try to do
1380 * something smarter
1381 */
1382 if (error_code & (ESR_DLK|ESR_ILK))
1383 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1384 return;
1385}
1386#endif /* CONFIG_FSL_BOOKE */
1387
1388#ifdef CONFIG_SPE
1389void SPEFloatingPointException(struct pt_regs *regs)
1390{
Liu Yu6a800f32008-10-28 11:50:21 +08001391 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001392 unsigned long spefscr;
1393 int fpexc_mode;
1394 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001395 int err;
1396
yu liu685659e2011-06-14 18:34:25 -05001397 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001398
1399 spefscr = current->thread.spefscr;
1400 fpexc_mode = current->thread.fpexc_mode;
1401
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001402 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1403 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001404 }
1405 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1406 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001407 }
1408 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1409 code = FPE_FLTDIV;
1410 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1411 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001412 }
1413 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1414 code = FPE_FLTRES;
1415
Liu Yu6a800f32008-10-28 11:50:21 +08001416 err = do_spe_mathemu(regs);
1417 if (err == 0) {
1418 regs->nip += 4; /* skip emulated instruction */
1419 emulate_single_step(regs);
1420 return;
1421 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001422
Liu Yu6a800f32008-10-28 11:50:21 +08001423 if (err == -EFAULT) {
1424 /* got an error reading the instruction */
1425 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1426 } else if (err == -EINVAL) {
1427 /* didn't recognize the instruction */
1428 printk(KERN_ERR "unrecognized spe instruction "
1429 "in %s at %lx\n", current->comm, regs->nip);
1430 } else {
1431 _exception(SIGFPE, regs, code, regs->nip);
1432 }
1433
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001434 return;
1435}
Liu Yu6a800f32008-10-28 11:50:21 +08001436
1437void SPEFloatingPointRoundException(struct pt_regs *regs)
1438{
1439 extern int speround_handler(struct pt_regs *regs);
1440 int err;
1441
1442 preempt_disable();
1443 if (regs->msr & MSR_SPE)
1444 giveup_spe(current);
1445 preempt_enable();
1446
1447 regs->nip -= 4;
1448 err = speround_handler(regs);
1449 if (err == 0) {
1450 regs->nip += 4; /* skip emulated instruction */
1451 emulate_single_step(regs);
1452 return;
1453 }
1454
1455 if (err == -EFAULT) {
1456 /* got an error reading the instruction */
1457 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1458 } else if (err == -EINVAL) {
1459 /* didn't recognize the instruction */
1460 printk(KERN_ERR "unrecognized spe instruction "
1461 "in %s at %lx\n", current->comm, regs->nip);
1462 } else {
1463 _exception(SIGFPE, regs, 0, regs->nip);
1464 return;
1465 }
1466}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001467#endif
1468
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001469/*
1470 * We enter here if we get an unrecoverable exception, that is, one
1471 * that happened at a point where the RI (recoverable interrupt) bit
1472 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1473 * we therefore lost state by taking this exception.
1474 */
1475void unrecoverable_exception(struct pt_regs *regs)
1476{
1477 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1478 regs->trap, regs->nip);
1479 die("Unrecoverable exception", regs, SIGABRT);
1480}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001481
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001482#ifdef CONFIG_BOOKE_WDT
1483/*
1484 * Default handler for a Watchdog exception,
1485 * spins until a reboot occurs
1486 */
1487void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1488{
1489 /* Generic WatchdogHandler, implement your own */
1490 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1491 return;
1492}
1493
1494void WatchdogException(struct pt_regs *regs)
1495{
1496 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1497 WatchdogHandler(regs);
1498}
1499#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001500
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001501/*
1502 * We enter here if we discover during exception entry that we are
1503 * running in supervisor mode with a userspace value in the stack pointer.
1504 */
1505void kernel_bad_stack(struct pt_regs *regs)
1506{
1507 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1508 regs->gpr[1], regs->nip);
1509 die("Bad kernel stack pointer", regs, SIGABRT);
1510}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001511
1512void __init trap_init(void)
1513{
1514}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001515
1516
1517#ifdef CONFIG_PPC_EMULATED_STATS
1518
1519#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1520
1521struct ppc_emulated ppc_emulated = {
1522#ifdef CONFIG_ALTIVEC
1523 WARN_EMULATED_SETUP(altivec),
1524#endif
1525 WARN_EMULATED_SETUP(dcba),
1526 WARN_EMULATED_SETUP(dcbz),
1527 WARN_EMULATED_SETUP(fp_pair),
1528 WARN_EMULATED_SETUP(isel),
1529 WARN_EMULATED_SETUP(mcrxr),
1530 WARN_EMULATED_SETUP(mfpvr),
1531 WARN_EMULATED_SETUP(multiple),
1532 WARN_EMULATED_SETUP(popcntb),
1533 WARN_EMULATED_SETUP(spe),
1534 WARN_EMULATED_SETUP(string),
1535 WARN_EMULATED_SETUP(unaligned),
1536#ifdef CONFIG_MATH_EMULATION
1537 WARN_EMULATED_SETUP(math),
1538#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1539 WARN_EMULATED_SETUP(8xx),
1540#endif
1541#ifdef CONFIG_VSX
1542 WARN_EMULATED_SETUP(vsx),
1543#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001544#ifdef CONFIG_PPC64
1545 WARN_EMULATED_SETUP(mfdscr),
1546 WARN_EMULATED_SETUP(mtdscr),
1547#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001548};
1549
1550u32 ppc_warn_emulated;
1551
1552void ppc_warn_emulated_print(const char *type)
1553{
Christian Dietrich76462232011-06-04 05:36:54 +00001554 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1555 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001556}
1557
1558static int __init ppc_warn_emulated_init(void)
1559{
1560 struct dentry *dir, *d;
1561 unsigned int i;
1562 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1563
1564 if (!powerpc_debugfs_root)
1565 return -ENODEV;
1566
1567 dir = debugfs_create_dir("emulated_instructions",
1568 powerpc_debugfs_root);
1569 if (!dir)
1570 return -ENOMEM;
1571
1572 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1573 &ppc_warn_emulated);
1574 if (!d)
1575 goto fail;
1576
1577 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1578 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1579 (u32 *)&entries[i].val.counter);
1580 if (!d)
1581 goto fail;
1582 }
1583
1584 return 0;
1585
1586fail:
1587 debugfs_remove_recursive(dir);
1588 return -ENOMEM;
1589}
1590
1591device_initcall(ppc_warn_emulated_init);
1592
1593#endif /* CONFIG_PPC_EMULATED_STATS */