blob: cb71cf29edea3e80bd3fe2d698adb92063116a84 [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>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100037
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000038#include <asm/emulated_ops.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100039#include <asm/pgtable.h>
40#include <asm/uaccess.h>
41#include <asm/system.h>
42#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100043#include <asm/machdep.h>
44#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100045#include <asm/pmc.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100046#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +100047#include <asm/reg.h>
Paul Mackerras86417782005-10-10 22:37:57 +100048#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100049#ifdef CONFIG_PMAC_BACKLIGHT
50#include <asm/backlight.h>
51#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100052#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100053#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100054#include <asm/processor.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100055#endif
David Wilderc0ce7d02006-06-23 15:29:34 -070056#include <asm/kexec.h>
Kumar Gala16c57b32009-02-10 20:10:44 +000057#include <asm/ppc-opcode.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100058
Olof Johansson7dbb9222008-01-31 14:34:47 +110059#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000060int (*__debugger)(struct pt_regs *regs) __read_mostly;
61int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
62int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
63int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100067
68EXPORT_SYMBOL(__debugger);
69EXPORT_SYMBOL(__debugger_ipi);
70EXPORT_SYMBOL(__debugger_bpt);
71EXPORT_SYMBOL(__debugger_sstep);
72EXPORT_SYMBOL(__debugger_iabr_match);
73EXPORT_SYMBOL(__debugger_dabr_match);
74EXPORT_SYMBOL(__debugger_fault_handler);
75#endif
76
Paul Mackerras14cf11a2005-09-26 16:04:21 +100077/*
78 * Trap & Exception support
79 */
80
anton@samba.org6031d9d2007-03-20 20:38:12 -050081#ifdef CONFIG_PMAC_BACKLIGHT
82static void pmac_backlight_unblank(void)
83{
84 mutex_lock(&pmac_backlight_mutex);
85 if (pmac_backlight) {
86 struct backlight_properties *props;
87
88 props = &pmac_backlight->props;
89 props->brightness = props->max_brightness;
90 props->power = FB_BLANK_UNBLANK;
91 backlight_update_status(pmac_backlight);
92 }
93 mutex_unlock(&pmac_backlight_mutex);
94}
95#else
96static inline void pmac_backlight_unblank(void) { }
97#endif
98
Paul Mackerras14cf11a2005-09-26 16:04:21 +100099int die(const char *str, struct pt_regs *regs, long err)
100{
anton@samba.org34c2a142007-03-20 20:38:13 -0500101 static struct {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000102 raw_spinlock_t lock;
anton@samba.org34c2a142007-03-20 20:38:13 -0500103 u32 lock_owner;
104 int lock_owner_depth;
105 } die = {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000106 .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
anton@samba.org34c2a142007-03-20 20:38:13 -0500107 .lock_owner = -1,
108 .lock_owner_depth = 0
109 };
David Wilderc0ce7d02006-06-23 15:29:34 -0700110 static int die_counter;
anton@samba.org34c2a142007-03-20 20:38:13 -0500111 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000112
113 if (debugger(regs))
114 return 1;
115
anton@samba.org293e4682007-03-20 20:38:11 -0500116 oops_enter();
117
anton@samba.org34c2a142007-03-20 20:38:13 -0500118 if (die.lock_owner != raw_smp_processor_id()) {
119 console_verbose();
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000120 raw_spin_lock_irqsave(&die.lock, flags);
anton@samba.org34c2a142007-03-20 20:38:13 -0500121 die.lock_owner = smp_processor_id();
122 die.lock_owner_depth = 0;
123 bust_spinlocks(1);
124 if (machine_is(powermac))
125 pmac_backlight_unblank();
126 } else {
127 local_save_flags(flags);
128 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700129
anton@samba.org34c2a142007-03-20 20:38:13 -0500130 if (++die.lock_owner_depth < 3) {
131 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000132#ifdef CONFIG_PREEMPT
anton@samba.org34c2a142007-03-20 20:38:13 -0500133 printk("PREEMPT ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000134#endif
135#ifdef CONFIG_SMP
anton@samba.org34c2a142007-03-20 20:38:13 -0500136 printk("SMP NR_CPUS=%d ", NR_CPUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137#endif
138#ifdef CONFIG_DEBUG_PAGEALLOC
anton@samba.org34c2a142007-03-20 20:38:13 -0500139 printk("DEBUG_PAGEALLOC ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000140#endif
141#ifdef CONFIG_NUMA
anton@samba.org34c2a142007-03-20 20:38:13 -0500142 printk("NUMA ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000143#endif
anton@samba.orgae7f4462007-03-20 20:38:14 -0500144 printk("%s\n", ppc_md.name ? ppc_md.name : "");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100145
Anton Blanchard66fcb102010-02-07 14:44:16 +0000146 sysfs_printk_last_file();
147 if (notify_die(DIE_OOPS, str, regs, err, 255,
148 SIGSEGV) == NOTIFY_STOP)
149 return 1;
150
anton@samba.org34c2a142007-03-20 20:38:13 -0500151 print_modules();
152 show_regs(regs);
153 } else {
154 printk("Recursive die() failure, output suppressed\n");
155 }
156
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000157 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500158 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700159 add_taint(TAINT_DIE);
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000160 raw_spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700161
162 if (kexec_should_crash(current) ||
163 kexec_sr_activated(smp_processor_id()))
164 crash_kexec(regs);
165 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000166
167 if (in_interrupt())
168 panic("Fatal exception in interrupt");
169
Hormscea6a4b2006-07-30 03:03:34 -0700170 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700171 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700172
anton@samba.org293e4682007-03-20 20:38:11 -0500173 oops_exit();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000174 do_exit(err);
175
176 return 0;
177}
178
Oleg Nesterov25baa352009-12-15 16:47:18 -0800179void user_single_step_siginfo(struct task_struct *tsk,
180 struct pt_regs *regs, siginfo_t *info)
181{
182 memset(info, 0, sizeof(*info));
183 info->si_signo = SIGTRAP;
184 info->si_code = TRAP_TRACE;
185 info->si_addr = (void __user *)regs->nip;
186}
187
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000188void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
189{
190 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000191 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
192 "at %08lx nip %08lx lr %08lx code %x\n";
193 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
194 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000195
196 if (!user_mode(regs)) {
197 if (die("Exception in kernel mode", regs, signr))
198 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000199 } else if (show_unhandled_signals &&
200 unhandled_signal(current, signr) &&
201 printk_ratelimit()) {
202 printk(regs->msr & MSR_SF ? 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
224 cpu_set(smp_processor_id(), cpus_in_sr);
225#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
428 printk("Machine check in kernel mode.\n");
429 printk("Caused by (from MCSR=%lx): ", reason);
430
431 if (reason & MCSR_MCP)
432 printk("Machine Check Signal\n");
433
434 if (reason & MCSR_ICPERR) {
435 printk("Instruction Cache Parity Error\n");
436
437 /*
438 * This is recoverable by invalidating the i-cache.
439 */
440 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
441 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
442 ;
443
444 /*
445 * This will generally be accompanied by an instruction
446 * fetch error report -- only treat MCSR_IF as fatal
447 * if it wasn't due to an L1 parity error.
448 */
449 reason &= ~MCSR_IF;
450 }
451
452 if (reason & MCSR_DCPERR_MC) {
453 printk("Data Cache Parity Error\n");
454 recoverable = 0;
455 }
456
457 if (reason & MCSR_L2MMU_MHIT) {
458 printk("Hit on multiple TLB entries\n");
459 recoverable = 0;
460 }
461
462 if (reason & MCSR_NMI)
463 printk("Non-maskable interrupt\n");
464
465 if (reason & MCSR_IF) {
466 printk("Instruction Fetch Error Report\n");
467 recoverable = 0;
468 }
469
470 if (reason & MCSR_LD) {
471 printk("Load Error Report\n");
472 recoverable = 0;
473 }
474
475 if (reason & MCSR_ST) {
476 printk("Store Error Report\n");
477 recoverable = 0;
478 }
479
480 if (reason & MCSR_LDG) {
481 printk("Guarded Load Error Report\n");
482 recoverable = 0;
483 }
484
485 if (reason & MCSR_TLBSYNC)
486 printk("Simultaneous tlbsync operations\n");
487
488 if (reason & MCSR_BSL2_ERR) {
489 printk("Level 2 Cache Error\n");
490 recoverable = 0;
491 }
492
493 if (reason & MCSR_MAV) {
494 u64 addr;
495
496 addr = mfspr(SPRN_MCAR);
497 addr |= (u64)mfspr(SPRN_MCARU) << 32;
498
499 printk("Machine Check %s Address: %#llx\n",
500 reason & MCSR_MEA ? "Effective" : "Physical", addr);
501 }
502
503 mtspr(SPRN_MCSR, mcsr);
504 return mfspr(SPRN_MCSR) == 0 && recoverable;
505}
506
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100507int machine_check_e500(struct pt_regs *regs)
508{
509 unsigned long reason = get_mc_reason(regs);
510
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000511 printk("Machine check in kernel mode.\n");
512 printk("Caused by (from MCSR=%lx): ", reason);
513
514 if (reason & MCSR_MCP)
515 printk("Machine Check Signal\n");
516 if (reason & MCSR_ICPERR)
517 printk("Instruction Cache Parity Error\n");
518 if (reason & MCSR_DCP_PERR)
519 printk("Data Cache Push Parity Error\n");
520 if (reason & MCSR_DCPERR)
521 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000522 if (reason & MCSR_BUS_IAERR)
523 printk("Bus - Instruction Address Error\n");
524 if (reason & MCSR_BUS_RAERR)
525 printk("Bus - Read Address Error\n");
526 if (reason & MCSR_BUS_WAERR)
527 printk("Bus - Write Address Error\n");
528 if (reason & MCSR_BUS_IBERR)
529 printk("Bus - Instruction Data Error\n");
530 if (reason & MCSR_BUS_RBERR)
531 printk("Bus - Read Data Bus Error\n");
532 if (reason & MCSR_BUS_WBERR)
533 printk("Bus - Read Data Bus Error\n");
534 if (reason & MCSR_BUS_IPERR)
535 printk("Bus - Instruction Parity Error\n");
536 if (reason & MCSR_BUS_RPERR)
537 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100538
539 return 0;
540}
Kumar Gala4490c062010-10-08 08:32:11 -0500541
542int machine_check_generic(struct pt_regs *regs)
543{
544 return 0;
545}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100546#elif defined(CONFIG_E200)
547int machine_check_e200(struct pt_regs *regs)
548{
549 unsigned long reason = get_mc_reason(regs);
550
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000551 printk("Machine check in kernel mode.\n");
552 printk("Caused by (from MCSR=%lx): ", reason);
553
554 if (reason & MCSR_MCP)
555 printk("Machine Check Signal\n");
556 if (reason & MCSR_CP_PERR)
557 printk("Cache Push Parity Error\n");
558 if (reason & MCSR_CPERR)
559 printk("Cache Parity Error\n");
560 if (reason & MCSR_EXCP_ERR)
561 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
562 if (reason & MCSR_BUS_IRERR)
563 printk("Bus - Read Bus Error on instruction fetch\n");
564 if (reason & MCSR_BUS_DRERR)
565 printk("Bus - Read Bus Error on data load\n");
566 if (reason & MCSR_BUS_WRERR)
567 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100568
569 return 0;
570}
571#else
572int machine_check_generic(struct pt_regs *regs)
573{
574 unsigned long reason = get_mc_reason(regs);
575
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000576 printk("Machine check in kernel mode.\n");
577 printk("Caused by (from SRR1=%lx): ", reason);
578 switch (reason & 0x601F0000) {
579 case 0x80000:
580 printk("Machine check signal\n");
581 break;
582 case 0: /* for 601 */
583 case 0x40000:
584 case 0x140000: /* 7450 MSS error and TEA */
585 printk("Transfer error ack signal\n");
586 break;
587 case 0x20000:
588 printk("Data parity error signal\n");
589 break;
590 case 0x10000:
591 printk("Address parity error signal\n");
592 break;
593 case 0x20000000:
594 printk("L1 Data Cache error\n");
595 break;
596 case 0x40000000:
597 printk("L1 Instruction Cache error\n");
598 break;
599 case 0x00100000:
600 printk("L2 data cache parity error\n");
601 break;
602 default:
603 printk("Unknown values in msr\n");
604 }
Olof Johansson75918a42007-09-21 05:11:20 +1000605 return 0;
606}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100607#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000608
609void machine_check_exception(struct pt_regs *regs)
610{
611 int recover = 0;
612
Anton Blanchard89713ed2010-01-31 20:34:06 +0000613 __get_cpu_var(irq_stat).mce_exceptions++;
614
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100615 /* See if any machine dependent calls. In theory, we would want
616 * to call the CPU first, and call the ppc_md. one if the CPU
617 * one returns a positive number. However there is existing code
618 * that assumes the board gets a first chance, so let's keep it
619 * that way for now and fix things later. --BenH.
620 */
Olof Johansson75918a42007-09-21 05:11:20 +1000621 if (ppc_md.machine_check_exception)
622 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100623 else if (cur_cpu_spec->machine_check)
624 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000625
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100626 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000627 return;
628
Olof Johansson75918a42007-09-21 05:11:20 +1000629#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100630 /* the qspan pci read routines can cause machine checks -- Cort
631 *
632 * yuck !!! that totally needs to go away ! There are better ways
633 * to deal with that than having a wart in the mcheck handler.
634 * -- BenH
635 */
Olof Johansson75918a42007-09-21 05:11:20 +1000636 bad_page_fault(regs, regs->dar, SIGBUS);
637 return;
638#endif
639
Anton Blancharda4435062011-01-11 19:45:31 +0000640 if (debugger_fault_handler(regs))
Olof Johansson75918a42007-09-21 05:11:20 +1000641 return;
Olof Johansson75918a42007-09-21 05:11:20 +1000642
643 if (check_io_access(regs))
644 return;
645
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000646 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000647
648 /* Must die if the interrupt is not recoverable */
649 if (!(regs->msr & MSR_RI))
650 panic("Unrecoverable Machine check");
651}
652
653void SMIException(struct pt_regs *regs)
654{
655 die("System Management Interrupt", regs, SIGABRT);
656}
657
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000658void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000659{
660 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
661 regs->nip, regs->msr, regs->trap);
662
663 _exception(SIGTRAP, regs, 0, 0);
664}
665
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000666void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000667{
668 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
669 5, SIGTRAP) == NOTIFY_STOP)
670 return;
671 if (debugger_iabr_match(regs))
672 return;
673 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
674}
675
676void RunModeException(struct pt_regs *regs)
677{
678 _exception(SIGTRAP, regs, 0, 0);
679}
680
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000681void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000682{
K.Prasad2538c2d2010-06-15 11:35:31 +0530683 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000684
685 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
686 5, SIGTRAP) == NOTIFY_STOP)
687 return;
688 if (debugger_sstep(regs))
689 return;
690
691 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
692}
693
694/*
695 * After we have successfully emulated an instruction, we have to
696 * check if the instruction was being single-stepped, and if so,
697 * pretend we got a single-step exception. This was pointed out
698 * by Kumar Gala. -- paulus
699 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000700static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000701{
K.Prasad2538c2d2010-06-15 11:35:31 +0530702 if (single_stepping(regs))
703 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000704}
705
Kumar Gala5fad2932007-02-07 01:47:59 -0600706static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000707{
Kumar Gala5fad2932007-02-07 01:47:59 -0600708 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000709
710 /* Invalid operation */
711 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600712 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000713
714 /* Overflow */
715 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600716 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000717
718 /* Underflow */
719 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600720 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000721
722 /* Divide by zero */
723 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600724 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000725
726 /* Inexact result */
727 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600728 ret = FPE_FLTRES;
729
730 return ret;
731}
732
733static void parse_fpe(struct pt_regs *regs)
734{
735 int code = 0;
736
737 flush_fp_to_thread(current);
738
739 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000740
741 _exception(SIGFPE, regs, code, regs->nip);
742}
743
744/*
745 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000746 * provide the PVR to user applications using the mfspr rd, PVR.
747 * Return non-zero if we can't emulate, or -EFAULT if the associated
748 * memory access caused an access fault. Return zero on success.
749 *
750 * There are a couple of ways to do this, either "decode" the instruction
751 * or directly match lots of bits. In this case, matching lots of
752 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000753 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000754 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000755static int emulate_string_inst(struct pt_regs *regs, u32 instword)
756{
757 u8 rT = (instword >> 21) & 0x1f;
758 u8 rA = (instword >> 16) & 0x1f;
759 u8 NB_RB = (instword >> 11) & 0x1f;
760 u32 num_bytes;
761 unsigned long EA;
762 int pos = 0;
763
764 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000765 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000766 if ((rT == rA) || (rT == NB_RB))
767 return -EINVAL;
768
769 EA = (rA == 0) ? 0 : regs->gpr[rA];
770
Kumar Gala16c57b32009-02-10 20:10:44 +0000771 switch (instword & PPC_INST_STRING_MASK) {
772 case PPC_INST_LSWX:
773 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000774 EA += NB_RB;
775 num_bytes = regs->xer & 0x7f;
776 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000777 case PPC_INST_LSWI:
778 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000779 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
780 break;
781 default:
782 return -EINVAL;
783 }
784
785 while (num_bytes != 0)
786 {
787 u8 val;
788 u32 shift = 8 * (3 - (pos & 0x3));
789
Kumar Gala16c57b32009-02-10 20:10:44 +0000790 switch ((instword & PPC_INST_STRING_MASK)) {
791 case PPC_INST_LSWX:
792 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000793 if (get_user(val, (u8 __user *)EA))
794 return -EFAULT;
795 /* first time updating this reg,
796 * zero it out */
797 if (pos == 0)
798 regs->gpr[rT] = 0;
799 regs->gpr[rT] |= val << shift;
800 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000801 case PPC_INST_STSWI:
802 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803 val = regs->gpr[rT] >> shift;
804 if (put_user(val, (u8 __user *)EA))
805 return -EFAULT;
806 break;
807 }
808 /* move EA to next address */
809 EA += 1;
810 num_bytes--;
811
812 /* manage our position within the register */
813 if (++pos == 4) {
814 pos = 0;
815 if (++rT == 32)
816 rT = 0;
817 }
818 }
819
820 return 0;
821}
822
Will Schmidtc3412dc2006-08-30 13:11:38 -0500823static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
824{
825 u32 ra,rs;
826 unsigned long tmp;
827
828 ra = (instword >> 16) & 0x1f;
829 rs = (instword >> 21) & 0x1f;
830
831 tmp = regs->gpr[rs];
832 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
833 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
834 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
835 regs->gpr[ra] = tmp;
836
837 return 0;
838}
839
Kumar Galac1469f12007-11-19 21:35:29 -0600840static int emulate_isel(struct pt_regs *regs, u32 instword)
841{
842 u8 rT = (instword >> 21) & 0x1f;
843 u8 rA = (instword >> 16) & 0x1f;
844 u8 rB = (instword >> 11) & 0x1f;
845 u8 BC = (instword >> 6) & 0x1f;
846 u8 bit;
847 unsigned long tmp;
848
849 tmp = (rA == 0) ? 0 : regs->gpr[rA];
850 bit = (regs->ccr >> (31 - BC)) & 0x1;
851
852 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
853
854 return 0;
855}
856
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000857static int emulate_instruction(struct pt_regs *regs)
858{
859 u32 instword;
860 u32 rd;
861
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000862 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000863 return -EINVAL;
864 CHECK_FULL_REGS(regs);
865
866 if (get_user(instword, (u32 __user *)(regs->nip)))
867 return -EFAULT;
868
869 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000870 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000871 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000872 rd = (instword >> 21) & 0x1f;
873 regs->gpr[rd] = mfspr(SPRN_PVR);
874 return 0;
875 }
876
877 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000878 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000879 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000880 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000881 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000882
883 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000884 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000885 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000886 unsigned long msk = 0xf0000000UL >> shift;
887
Anton Blanchardeecff812009-10-27 18:46:55 +0000888 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000889 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
890 regs->xer &= ~0xf0000000UL;
891 return 0;
892 }
893
894 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000895 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000896 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000897 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000898 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000899
Will Schmidtc3412dc2006-08-30 13:11:38 -0500900 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000901 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000902 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500903 return emulate_popcntb_inst(regs, instword);
904 }
905
Kumar Galac1469f12007-11-19 21:35:29 -0600906 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000907 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000908 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600909 return emulate_isel(regs, instword);
910 }
911
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000912#ifdef CONFIG_PPC64
913 /* Emulate the mfspr rD, DSCR. */
914 if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
915 cpu_has_feature(CPU_FTR_DSCR)) {
916 PPC_WARN_EMULATED(mfdscr, regs);
917 rd = (instword >> 21) & 0x1f;
918 regs->gpr[rd] = mfspr(SPRN_DSCR);
919 return 0;
920 }
921 /* Emulate the mtspr DSCR, rD. */
922 if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
923 cpu_has_feature(CPU_FTR_DSCR)) {
924 PPC_WARN_EMULATED(mtdscr, regs);
925 rd = (instword >> 21) & 0x1f;
926 mtspr(SPRN_DSCR, regs->gpr[rd]);
927 current->thread.dscr_inherit = 1;
928 return 0;
929 }
930#endif
931
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000932 return -EINVAL;
933}
934
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800935int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000936{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800937 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000938}
939
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000940void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000941{
942 unsigned int reason = get_reason(regs);
943 extern int do_mathemu(struct pt_regs *regs);
944
Kim Phillipsaa42c692006-12-08 02:43:30 -0600945 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600946 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000947
948 if (reason & REASON_FP) {
949 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000950 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000951 return;
952 }
953 if (reason & REASON_TRAP) {
Jason Wesselba797b22010-05-20 21:04:25 -0500954 /* Debugger is first in line to stop recursive faults in
955 * rcu_lock, notify_die, or atomic_notifier_call_chain */
956 if (debugger_bpt(regs))
957 return;
958
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000959 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000960 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
961 == NOTIFY_STOP)
962 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800963
964 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700965 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000966 regs->nip += 4;
967 return;
968 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000969 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
970 return;
971 }
972
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100973 local_irq_enable();
974
Kumar Gala04903a32007-02-07 01:13:32 -0600975#ifdef CONFIG_MATH_EMULATION
976 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
977 * but there seems to be a hardware bug on the 405GP (RevD)
978 * that means ESR is sometimes set incorrectly - either to
979 * ESR_DST (!?) or 0. In the process of chasing this with the
980 * hardware people - not sure if it can happen on any illegal
981 * instruction or only on FP instructions, whether there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300982 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -0600983 switch (do_mathemu(regs)) {
984 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -0600985 emulate_single_step(regs);
986 return;
Kumar Gala5fad2932007-02-07 01:47:59 -0600987 case 1: {
988 int code = 0;
989 code = __parse_fpscr(current->thread.fpscr.val);
990 _exception(SIGFPE, regs, code, regs->nip);
991 return;
992 }
993 case -EFAULT:
994 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
995 return;
Kumar Gala04903a32007-02-07 01:13:32 -0600996 }
Kumar Gala5fad2932007-02-07 01:47:59 -0600997 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -0600998#endif /* CONFIG_MATH_EMULATION */
999
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001000 /* Try to emulate it if we should. */
1001 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001002 switch (emulate_instruction(regs)) {
1003 case 0:
1004 regs->nip += 4;
1005 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001006 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001007 case -EFAULT:
1008 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001009 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001010 }
1011 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001012
1013 if (reason & REASON_PRIVILEGED)
1014 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1015 else
1016 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001017}
1018
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001019void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001020{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001021 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001022
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001023 /* we don't implement logging of alignment exceptions */
1024 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1025 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001026
1027 if (fixed == 1) {
1028 regs->nip += 4; /* skip over emulated instruction */
1029 emulate_single_step(regs);
1030 return;
1031 }
1032
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001033 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001034 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001035 sig = SIGSEGV;
1036 code = SEGV_ACCERR;
1037 } else {
1038 sig = SIGBUS;
1039 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001040 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001041 if (user_mode(regs))
1042 _exception(sig, regs, code, regs->dar);
1043 else
1044 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001045}
1046
1047void StackOverflow(struct pt_regs *regs)
1048{
1049 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1050 current, regs->gpr[1]);
1051 debugger(regs);
1052 show_regs(regs);
1053 panic("kernel stack overflow");
1054}
1055
1056void nonrecoverable_exception(struct pt_regs *regs)
1057{
1058 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1059 regs->nip, regs->msr);
1060 debugger(regs);
1061 die("nonrecoverable exception", regs, SIGKILL);
1062}
1063
1064void trace_syscall(struct pt_regs *regs)
1065{
1066 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -07001067 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001068 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1069}
1070
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001071void kernel_fp_unavailable_exception(struct pt_regs *regs)
1072{
1073 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1074 "%lx at %lx\n", regs->trap, regs->nip);
1075 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1076}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001077
1078void altivec_unavailable_exception(struct pt_regs *regs)
1079{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001080 if (user_mode(regs)) {
1081 /* A user program has executed an altivec instruction,
1082 but this kernel doesn't support altivec. */
1083 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1084 return;
1085 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001086
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001087 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1088 "%lx at %lx\n", regs->trap, regs->nip);
1089 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001090}
1091
Michael Neulingce48b212008-06-25 14:07:18 +10001092void vsx_unavailable_exception(struct pt_regs *regs)
1093{
1094 if (user_mode(regs)) {
1095 /* A user program has executed an vsx instruction,
1096 but this kernel doesn't support vsx. */
1097 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1098 return;
1099 }
1100
1101 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1102 "%lx at %lx\n", regs->trap, regs->nip);
1103 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1104}
1105
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001106void performance_monitor_exception(struct pt_regs *regs)
1107{
Anton Blanchard89713ed2010-01-31 20:34:06 +00001108 __get_cpu_var(irq_stat).pmu_irqs++;
1109
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001110 perf_irq(regs);
1111}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001112
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001113#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001114void SoftwareEmulation(struct pt_regs *regs)
1115{
1116 extern int do_mathemu(struct pt_regs *);
1117 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -05001118#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001119 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -05001120#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001121
1122 CHECK_FULL_REGS(regs);
1123
1124 if (!user_mode(regs)) {
1125 debugger(regs);
1126 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1127 }
1128
1129#ifdef CONFIG_MATH_EMULATION
1130 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001131 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001132 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001133
1134 switch (errcode) {
1135 case 0:
1136 emulate_single_step(regs);
1137 return;
1138 case 1: {
1139 int code = 0;
1140 code = __parse_fpscr(current->thread.fpscr.val);
1141 _exception(SIGFPE, regs, code, regs->nip);
1142 return;
1143 }
1144 case -EFAULT:
1145 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1146 return;
1147 default:
1148 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1149 return;
1150 }
1151
Scott Wood5dd57a12007-09-18 15:29:35 -05001152#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001153 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001154 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001155 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001156
Kumar Gala5fad2932007-02-07 01:47:59 -06001157 switch (errcode) {
1158 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001159 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001160 return;
1161 case 1:
1162 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1163 return;
1164 case -EFAULT:
1165 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1166 return;
1167 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001168#else
1169 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001170#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001171}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001172#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001173
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001174#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001175static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1176{
1177 int changed = 0;
1178 /*
1179 * Determine the cause of the debug event, clear the
1180 * event flags and send a trap to the handler. Torez
1181 */
1182 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1183 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1184#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1185 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1186#endif
1187 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1188 5);
1189 changed |= 0x01;
1190 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1191 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1192 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1193 6);
1194 changed |= 0x01;
1195 } else if (debug_status & DBSR_IAC1) {
1196 current->thread.dbcr0 &= ~DBCR0_IAC1;
1197 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1198 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1199 1);
1200 changed |= 0x01;
1201 } else if (debug_status & DBSR_IAC2) {
1202 current->thread.dbcr0 &= ~DBCR0_IAC2;
1203 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1204 2);
1205 changed |= 0x01;
1206 } else if (debug_status & DBSR_IAC3) {
1207 current->thread.dbcr0 &= ~DBCR0_IAC3;
1208 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1209 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1210 3);
1211 changed |= 0x01;
1212 } else if (debug_status & DBSR_IAC4) {
1213 current->thread.dbcr0 &= ~DBCR0_IAC4;
1214 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1215 4);
1216 changed |= 0x01;
1217 }
1218 /*
1219 * At the point this routine was called, the MSR(DE) was turned off.
1220 * Check all other debug flags and see if that bit needs to be turned
1221 * back on or not.
1222 */
1223 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1224 regs->msr |= MSR_DE;
1225 else
1226 /* Make sure the IDM flag is off */
1227 current->thread.dbcr0 &= ~DBCR0_IDM;
1228
1229 if (changed & 0x01)
1230 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1231}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001232
Kumar Galaf8279622008-06-26 02:01:37 -05001233void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001234{
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001235 current->thread.dbsr = debug_status;
1236
Roland McGrathec097c82009-05-28 21:26:38 +00001237 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1238 * on server, it stops on the target of the branch. In order to simulate
1239 * the server behaviour, we thus restart right away with a single step
1240 * instead of stopping here when hitting a BT
1241 */
1242 if (debug_status & DBSR_BT) {
1243 regs->msr &= ~MSR_DE;
1244
1245 /* Disable BT */
1246 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1247 /* Clear the BT event */
1248 mtspr(SPRN_DBSR, DBSR_BT);
1249
1250 /* Do the single step trick only when coming from userspace */
1251 if (user_mode(regs)) {
1252 current->thread.dbcr0 &= ~DBCR0_BT;
1253 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1254 regs->msr |= MSR_DE;
1255 return;
1256 }
1257
1258 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1259 5, SIGTRAP) == NOTIFY_STOP) {
1260 return;
1261 }
1262 if (debugger_sstep(regs))
1263 return;
1264 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001265 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001266
1267 /* Disable instruction completion */
1268 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1269 /* Clear the instruction completion event */
1270 mtspr(SPRN_DBSR, DBSR_IC);
1271
1272 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1273 5, SIGTRAP) == NOTIFY_STOP) {
1274 return;
1275 }
1276
1277 if (debugger_sstep(regs))
1278 return;
1279
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001280 if (user_mode(regs)) {
1281 current->thread.dbcr0 &= ~DBCR0_IC;
1282#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1283 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1284 current->thread.dbcr1))
1285 regs->msr |= MSR_DE;
1286 else
1287 /* Make sure the IDM bit is off */
1288 current->thread.dbcr0 &= ~DBCR0_IDM;
1289#endif
1290 }
Kumar Galaf8279622008-06-26 02:01:37 -05001291
1292 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001293 } else
1294 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001295}
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001296#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001297
1298#if !defined(CONFIG_TAU_INT)
1299void TAUException(struct pt_regs *regs)
1300{
1301 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1302 regs->nip, regs->msr, regs->trap, print_tainted());
1303}
1304#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001305
1306#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001307void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001308{
1309 int err;
1310
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001311 if (!user_mode(regs)) {
1312 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1313 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001314 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001315 }
1316
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001317 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001318
Anton Blanchardeecff812009-10-27 18:46:55 +00001319 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001320 err = emulate_altivec(regs);
1321 if (err == 0) {
1322 regs->nip += 4; /* skip emulated instruction */
1323 emulate_single_step(regs);
1324 return;
1325 }
1326
1327 if (err == -EFAULT) {
1328 /* got an error reading the instruction */
1329 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1330 } else {
1331 /* didn't recognize the instruction */
1332 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1333 if (printk_ratelimit())
1334 printk(KERN_ERR "Unrecognized altivec instruction "
1335 "in %s at %lx\n", current->comm, regs->nip);
1336 current->thread.vscr.u[3] |= 0x10000;
1337 }
1338}
1339#endif /* CONFIG_ALTIVEC */
1340
Michael Neulingce48b212008-06-25 14:07:18 +10001341#ifdef CONFIG_VSX
1342void vsx_assist_exception(struct pt_regs *regs)
1343{
1344 if (!user_mode(regs)) {
1345 printk(KERN_EMERG "VSX assist exception in kernel mode"
1346 " at %lx\n", regs->nip);
1347 die("Kernel VSX assist exception", regs, SIGILL);
1348 }
1349
1350 flush_vsx_to_thread(current);
1351 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1352 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1353}
1354#endif /* CONFIG_VSX */
1355
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001356#ifdef CONFIG_FSL_BOOKE
1357void CacheLockingException(struct pt_regs *regs, unsigned long address,
1358 unsigned long error_code)
1359{
1360 /* We treat cache locking instructions from the user
1361 * as priv ops, in the future we could try to do
1362 * something smarter
1363 */
1364 if (error_code & (ESR_DLK|ESR_ILK))
1365 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1366 return;
1367}
1368#endif /* CONFIG_FSL_BOOKE */
1369
1370#ifdef CONFIG_SPE
1371void SPEFloatingPointException(struct pt_regs *regs)
1372{
Liu Yu6a800f32008-10-28 11:50:21 +08001373 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001374 unsigned long spefscr;
1375 int fpexc_mode;
1376 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001377 int err;
1378
1379 preempt_disable();
1380 if (regs->msr & MSR_SPE)
1381 giveup_spe(current);
1382 preempt_enable();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001383
1384 spefscr = current->thread.spefscr;
1385 fpexc_mode = current->thread.fpexc_mode;
1386
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001387 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1388 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001389 }
1390 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1391 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001392 }
1393 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1394 code = FPE_FLTDIV;
1395 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1396 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001397 }
1398 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1399 code = FPE_FLTRES;
1400
Liu Yu6a800f32008-10-28 11:50:21 +08001401 err = do_spe_mathemu(regs);
1402 if (err == 0) {
1403 regs->nip += 4; /* skip emulated instruction */
1404 emulate_single_step(regs);
1405 return;
1406 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001407
Liu Yu6a800f32008-10-28 11:50:21 +08001408 if (err == -EFAULT) {
1409 /* got an error reading the instruction */
1410 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1411 } else if (err == -EINVAL) {
1412 /* didn't recognize the instruction */
1413 printk(KERN_ERR "unrecognized spe instruction "
1414 "in %s at %lx\n", current->comm, regs->nip);
1415 } else {
1416 _exception(SIGFPE, regs, code, regs->nip);
1417 }
1418
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001419 return;
1420}
Liu Yu6a800f32008-10-28 11:50:21 +08001421
1422void SPEFloatingPointRoundException(struct pt_regs *regs)
1423{
1424 extern int speround_handler(struct pt_regs *regs);
1425 int err;
1426
1427 preempt_disable();
1428 if (regs->msr & MSR_SPE)
1429 giveup_spe(current);
1430 preempt_enable();
1431
1432 regs->nip -= 4;
1433 err = speround_handler(regs);
1434 if (err == 0) {
1435 regs->nip += 4; /* skip emulated instruction */
1436 emulate_single_step(regs);
1437 return;
1438 }
1439
1440 if (err == -EFAULT) {
1441 /* got an error reading the instruction */
1442 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1443 } else if (err == -EINVAL) {
1444 /* didn't recognize the instruction */
1445 printk(KERN_ERR "unrecognized spe instruction "
1446 "in %s at %lx\n", current->comm, regs->nip);
1447 } else {
1448 _exception(SIGFPE, regs, 0, regs->nip);
1449 return;
1450 }
1451}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001452#endif
1453
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001454/*
1455 * We enter here if we get an unrecoverable exception, that is, one
1456 * that happened at a point where the RI (recoverable interrupt) bit
1457 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1458 * we therefore lost state by taking this exception.
1459 */
1460void unrecoverable_exception(struct pt_regs *regs)
1461{
1462 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1463 regs->trap, regs->nip);
1464 die("Unrecoverable exception", regs, SIGABRT);
1465}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001466
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001467#ifdef CONFIG_BOOKE_WDT
1468/*
1469 * Default handler for a Watchdog exception,
1470 * spins until a reboot occurs
1471 */
1472void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1473{
1474 /* Generic WatchdogHandler, implement your own */
1475 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1476 return;
1477}
1478
1479void WatchdogException(struct pt_regs *regs)
1480{
1481 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1482 WatchdogHandler(regs);
1483}
1484#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001485
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001486/*
1487 * We enter here if we discover during exception entry that we are
1488 * running in supervisor mode with a userspace value in the stack pointer.
1489 */
1490void kernel_bad_stack(struct pt_regs *regs)
1491{
1492 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1493 regs->gpr[1], regs->nip);
1494 die("Bad kernel stack pointer", regs, SIGABRT);
1495}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001496
1497void __init trap_init(void)
1498{
1499}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001500
1501
1502#ifdef CONFIG_PPC_EMULATED_STATS
1503
1504#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1505
1506struct ppc_emulated ppc_emulated = {
1507#ifdef CONFIG_ALTIVEC
1508 WARN_EMULATED_SETUP(altivec),
1509#endif
1510 WARN_EMULATED_SETUP(dcba),
1511 WARN_EMULATED_SETUP(dcbz),
1512 WARN_EMULATED_SETUP(fp_pair),
1513 WARN_EMULATED_SETUP(isel),
1514 WARN_EMULATED_SETUP(mcrxr),
1515 WARN_EMULATED_SETUP(mfpvr),
1516 WARN_EMULATED_SETUP(multiple),
1517 WARN_EMULATED_SETUP(popcntb),
1518 WARN_EMULATED_SETUP(spe),
1519 WARN_EMULATED_SETUP(string),
1520 WARN_EMULATED_SETUP(unaligned),
1521#ifdef CONFIG_MATH_EMULATION
1522 WARN_EMULATED_SETUP(math),
1523#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1524 WARN_EMULATED_SETUP(8xx),
1525#endif
1526#ifdef CONFIG_VSX
1527 WARN_EMULATED_SETUP(vsx),
1528#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001529#ifdef CONFIG_PPC64
1530 WARN_EMULATED_SETUP(mfdscr),
1531 WARN_EMULATED_SETUP(mtdscr),
1532#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001533};
1534
1535u32 ppc_warn_emulated;
1536
1537void ppc_warn_emulated_print(const char *type)
1538{
1539 if (printk_ratelimit())
1540 pr_warning("%s used emulated %s instruction\n", current->comm,
1541 type);
1542}
1543
1544static int __init ppc_warn_emulated_init(void)
1545{
1546 struct dentry *dir, *d;
1547 unsigned int i;
1548 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1549
1550 if (!powerpc_debugfs_root)
1551 return -ENODEV;
1552
1553 dir = debugfs_create_dir("emulated_instructions",
1554 powerpc_debugfs_root);
1555 if (!dir)
1556 return -ENOMEM;
1557
1558 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1559 &ppc_warn_emulated);
1560 if (!d)
1561 goto fail;
1562
1563 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1564 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1565 (u32 *)&entries[i].val.counter);
1566 if (!d)
1567 goto fail;
1568 }
1569
1570 return 0;
1571
1572fail:
1573 debugfs_remove_recursive(dir);
1574 return -ENOMEM;
1575}
1576
1577device_initcall(ppc_warn_emulated_init);
1578
1579#endif /* CONFIG_PPC_EMULATED_STATS */