blob: 91377870b26a675c5e3827c4e7e8656c7ea1d608 [file] [log] [blame]
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001/*
Paul Mackerras14cf11a2005-09-26 16:04:21 +10002 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
Scott Woodfe04b112010-04-08 00:38:22 -05003 * Copyright 2007-2010 Freescale Semiconductor, Inc.
Paul Mackerras14cf11a2005-09-26 16:04:21 +10004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Modified by Cort Dougan (cort@cs.nmt.edu)
11 * and Paul Mackerras (paulus@samba.org)
12 */
13
14/*
15 * This file handles the architecture-dependent parts of hardware exceptions
16 */
17
Paul Mackerras14cf11a2005-09-26 16:04:21 +100018#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/stddef.h>
23#include <linux/unistd.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100024#include <linux/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100025#include <linux/user.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100026#include <linux/interrupt.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100027#include <linux/init.h>
28#include <linux/module.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100029#include <linux/prctl.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100030#include <linux/delay.h>
31#include <linux/kprobes.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032#include <linux/kexec.h>
Michael Hanselmann5474c122006-06-25 05:47:08 -070033#include <linux/backlight.h>
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -080034#include <linux/bug.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070035#include <linux/kdebug.h>
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000036#include <linux/debugfs.h>
Christian Dietrich76462232011-06-04 05:36:54 +000037#include <linux/ratelimit.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100038
Geert Uytterhoeven80947e72009-05-18 02:10:05 +000039#include <asm/emulated_ops.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100040#include <asm/pgtable.h>
41#include <asm/uaccess.h>
42#include <asm/system.h>
43#include <asm/io.h>
Paul Mackerras86417782005-10-10 22:37:57 +100044#include <asm/machdep.h>
45#include <asm/rtas.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100046#include <asm/pmc.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100047#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +100048#include <asm/reg.h>
Paul Mackerras86417782005-10-10 22:37:57 +100049#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +100050#ifdef CONFIG_PMAC_BACKLIGHT
51#include <asm/backlight.h>
52#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100053#ifdef CONFIG_PPC64
Paul Mackerras86417782005-10-10 22:37:57 +100054#include <asm/firmware.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100055#include <asm/processor.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100056#endif
David Wilderc0ce7d02006-06-23 15:29:34 -070057#include <asm/kexec.h>
Kumar Gala16c57b32009-02-10 20:10:44 +000058#include <asm/ppc-opcode.h>
Shaohui Xiecce1f102010-11-18 14:57:32 +080059#include <asm/rio.h>
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100060
Olof Johansson7dbb9222008-01-31 14:34:47 +110061#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Anton Blanchard5be34922010-01-12 00:50:14 +000062int (*__debugger)(struct pt_regs *regs) __read_mostly;
63int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
67int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
68int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
Paul Mackerras14cf11a2005-09-26 16:04:21 +100069
70EXPORT_SYMBOL(__debugger);
71EXPORT_SYMBOL(__debugger_ipi);
72EXPORT_SYMBOL(__debugger_bpt);
73EXPORT_SYMBOL(__debugger_sstep);
74EXPORT_SYMBOL(__debugger_iabr_match);
75EXPORT_SYMBOL(__debugger_dabr_match);
76EXPORT_SYMBOL(__debugger_fault_handler);
77#endif
78
Paul Mackerras14cf11a2005-09-26 16:04:21 +100079/*
80 * Trap & Exception support
81 */
82
anton@samba.org6031d9d2007-03-20 20:38:12 -050083#ifdef CONFIG_PMAC_BACKLIGHT
84static void pmac_backlight_unblank(void)
85{
86 mutex_lock(&pmac_backlight_mutex);
87 if (pmac_backlight) {
88 struct backlight_properties *props;
89
90 props = &pmac_backlight->props;
91 props->brightness = props->max_brightness;
92 props->power = FB_BLANK_UNBLANK;
93 backlight_update_status(pmac_backlight);
94 }
95 mutex_unlock(&pmac_backlight_mutex);
96}
97#else
98static inline void pmac_backlight_unblank(void) { }
99#endif
100
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000101int die(const char *str, struct pt_regs *regs, long err)
102{
anton@samba.org34c2a142007-03-20 20:38:13 -0500103 static struct {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000104 raw_spinlock_t lock;
anton@samba.org34c2a142007-03-20 20:38:13 -0500105 u32 lock_owner;
106 int lock_owner_depth;
107 } die = {
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000108 .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
anton@samba.org34c2a142007-03-20 20:38:13 -0500109 .lock_owner = -1,
110 .lock_owner_depth = 0
111 };
David Wilderc0ce7d02006-06-23 15:29:34 -0700112 static int die_counter;
anton@samba.org34c2a142007-03-20 20:38:13 -0500113 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000114
115 if (debugger(regs))
116 return 1;
117
anton@samba.org293e4682007-03-20 20:38:11 -0500118 oops_enter();
119
anton@samba.org34c2a142007-03-20 20:38:13 -0500120 if (die.lock_owner != raw_smp_processor_id()) {
121 console_verbose();
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000122 raw_spin_lock_irqsave(&die.lock, flags);
anton@samba.org34c2a142007-03-20 20:38:13 -0500123 die.lock_owner = smp_processor_id();
124 die.lock_owner_depth = 0;
125 bust_spinlocks(1);
126 if (machine_is(powermac))
127 pmac_backlight_unblank();
128 } else {
129 local_save_flags(flags);
130 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700131
anton@samba.org34c2a142007-03-20 20:38:13 -0500132 if (++die.lock_owner_depth < 3) {
133 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000134#ifdef CONFIG_PREEMPT
anton@samba.org34c2a142007-03-20 20:38:13 -0500135 printk("PREEMPT ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000136#endif
137#ifdef CONFIG_SMP
anton@samba.org34c2a142007-03-20 20:38:13 -0500138 printk("SMP NR_CPUS=%d ", NR_CPUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000139#endif
140#ifdef CONFIG_DEBUG_PAGEALLOC
anton@samba.org34c2a142007-03-20 20:38:13 -0500141 printk("DEBUG_PAGEALLOC ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000142#endif
143#ifdef CONFIG_NUMA
anton@samba.org34c2a142007-03-20 20:38:13 -0500144 printk("NUMA ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000145#endif
anton@samba.orgae7f4462007-03-20 20:38:14 -0500146 printk("%s\n", ppc_md.name ? ppc_md.name : "");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100147
Anton Blanchard66fcb102010-02-07 14:44:16 +0000148 if (notify_die(DIE_OOPS, str, regs, err, 255,
149 SIGSEGV) == NOTIFY_STOP)
150 return 1;
151
anton@samba.org34c2a142007-03-20 20:38:13 -0500152 print_modules();
153 show_regs(regs);
154 } else {
155 printk("Recursive die() failure, output suppressed\n");
156 }
157
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000158 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500159 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700160 add_taint(TAINT_DIE);
Anton Blanchard58154c82011-11-30 00:23:09 +0000161 oops_exit();
162 printk("\n");
Thomas Gleixnerb8f87782010-02-18 02:22:31 +0000163 raw_spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700164
165 if (kexec_should_crash(current) ||
166 kexec_sr_activated(smp_processor_id()))
167 crash_kexec(regs);
168 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000169
Anton Blanchard58154c82011-11-30 00:23:09 +0000170 /*
171 * While our oops output is serialised by a spinlock, output
172 * from panic() called below can race and corrupt it. If we
173 * know we are going to panic, delay for 1 second so we have a
174 * chance to get clean backtraces from all CPUs that are oopsing.
175 */
176 if (in_interrupt() || panic_on_oops || !current->pid ||
177 is_global_init(current)) {
178 mdelay(MSEC_PER_SEC);
179 }
180
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000181 if (in_interrupt())
182 panic("Fatal exception in interrupt");
183
Hormscea6a4b2006-07-30 03:03:34 -0700184 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700185 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700186
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000187 do_exit(err);
188
189 return 0;
190}
191
Oleg Nesterov25baa352009-12-15 16:47:18 -0800192void user_single_step_siginfo(struct task_struct *tsk,
193 struct pt_regs *regs, siginfo_t *info)
194{
195 memset(info, 0, sizeof(*info));
196 info->si_signo = SIGTRAP;
197 info->si_code = TRAP_TRACE;
198 info->si_addr = (void __user *)regs->nip;
199}
200
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000201void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
202{
203 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000204 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
205 "at %08lx nip %08lx lr %08lx code %x\n";
206 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
207 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000208
209 if (!user_mode(regs)) {
210 if (die("Exception in kernel mode", regs, signr))
211 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000212 } else if (show_unhandled_signals &&
Christian Dietrich76462232011-06-04 05:36:54 +0000213 unhandled_signal(current, signr)) {
214 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
215 current->comm, current->pid, signr,
216 addr, regs->nip, regs->link, code);
217 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000218
219 memset(&info, 0, sizeof(info));
220 info.si_signo = signr;
221 info.si_code = code;
222 info.si_addr = (void __user *) addr;
223 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000224}
225
226#ifdef CONFIG_PPC64
227void system_reset_exception(struct pt_regs *regs)
228{
229 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000230 if (ppc_md.system_reset_exception) {
231 if (ppc_md.system_reset_exception(regs))
232 return;
233 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000234
David Wilderc0ce7d02006-06-23 15:29:34 -0700235#ifdef CONFIG_KEXEC
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000236 cpumask_set_cpu(smp_processor_id(), &cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700237#endif
238
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000239 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000240
David Wildereac83922006-06-29 15:17:30 -0700241 /*
242 * Some CPUs when released from the debugger will execute this path.
243 * These CPUs entered the debugger via a soft-reset. If the CPU was
244 * hung before entering the debugger it will return to the hung
245 * state when exiting this function. This causes a problem in
246 * kdump since the hung CPU(s) will not respond to the IPI sent
247 * from kdump. To prevent the problem we call crash_kexec_secondary()
248 * here. If a kdump had not been initiated or we exit the debugger
249 * with the "exit and recover" command (x) crash_kexec_secondary()
250 * will return after 5ms and the CPU returns to its previous state.
251 */
252 crash_kexec_secondary(regs);
253
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000254 /* Must die if the interrupt is not recoverable */
255 if (!(regs->msr & MSR_RI))
256 panic("Unrecoverable System Reset");
257
258 /* What should we do here? We could issue a shutdown or hard reset. */
259}
260#endif
261
262/*
263 * I/O accesses can cause machine checks on powermacs.
264 * Check if the NIP corresponds to the address of a sync
265 * instruction for which there is an entry in the exception
266 * table.
267 * Note that the 601 only takes a machine check on TEA
268 * (transfer error ack) signal assertion, and does not
269 * set any of the top 16 bits of SRR1.
270 * -- paulus.
271 */
272static inline int check_io_access(struct pt_regs *regs)
273{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100274#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000275 unsigned long msr = regs->msr;
276 const struct exception_table_entry *entry;
277 unsigned int *nip = (unsigned int *)regs->nip;
278
279 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
280 && (entry = search_exception_tables(regs->nip)) != NULL) {
281 /*
282 * Check that it's a sync instruction, or somewhere
283 * in the twi; isync; nop sequence that inb/inw/inl uses.
284 * As the address is in the exception table
285 * we should be able to read the instr there.
286 * For the debug message, we look at the preceding
287 * load or store.
288 */
289 if (*nip == 0x60000000) /* nop */
290 nip -= 2;
291 else if (*nip == 0x4c00012c) /* isync */
292 --nip;
293 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
294 /* sync or twi */
295 unsigned int rb;
296
297 --nip;
298 rb = (*nip >> 11) & 0x1f;
299 printk(KERN_DEBUG "%s bad port %lx at %p\n",
300 (*nip & 0x100)? "OUT to": "IN from",
301 regs->gpr[rb] - _IO_BASE, nip);
302 regs->msr |= MSR_RI;
303 regs->nip = entry->fixup;
304 return 1;
305 }
306 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100307#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000308 return 0;
309}
310
Dave Kleikamp172ae2e2010-02-08 11:50:57 +0000311#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000312/* On 4xx, the reason for the machine check or program exception
313 is in the ESR. */
314#define get_reason(regs) ((regs)->dsisr)
315#ifndef CONFIG_FSL_BOOKE
316#define get_mc_reason(regs) ((regs)->dsisr)
317#else
Scott Woodfe04b112010-04-08 00:38:22 -0500318#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000319#endif
320#define REASON_FP ESR_FP
321#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
322#define REASON_PRIVILEGED ESR_PPR
323#define REASON_TRAP ESR_PTR
324
325/* single-step stuff */
326#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
327#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
328
329#else
330/* On non-4xx, the reason for the machine check or program
331 exception is in the MSR. */
332#define get_reason(regs) ((regs)->msr)
333#define get_mc_reason(regs) ((regs)->msr)
334#define REASON_FP 0x100000
335#define REASON_ILLEGAL 0x80000
336#define REASON_PRIVILEGED 0x40000
337#define REASON_TRAP 0x20000
338
339#define single_stepping(regs) ((regs)->msr & MSR_SE)
340#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
341#endif
342
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100343#if defined(CONFIG_4xx)
344int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000345{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600346 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000347
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000348 if (reason & ESR_IMCP) {
349 printk("Instruction");
350 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
351 } else
352 printk("Data");
353 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100354
355 return 0;
356}
357
358int machine_check_440A(struct pt_regs *regs)
359{
360 unsigned long reason = get_mc_reason(regs);
361
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000362 printk("Machine check in kernel mode.\n");
363 if (reason & ESR_IMCP){
364 printk("Instruction Synchronous Machine Check exception\n");
365 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
366 }
367 else {
368 u32 mcsr = mfspr(SPRN_MCSR);
369 if (mcsr & MCSR_IB)
370 printk("Instruction Read PLB Error\n");
371 if (mcsr & MCSR_DRB)
372 printk("Data Read PLB Error\n");
373 if (mcsr & MCSR_DWB)
374 printk("Data Write PLB Error\n");
375 if (mcsr & MCSR_TLBP)
376 printk("TLB Parity Error\n");
377 if (mcsr & MCSR_ICP){
378 flush_instruction_cache();
379 printk("I-Cache Parity Error\n");
380 }
381 if (mcsr & MCSR_DCSP)
382 printk("D-Cache Search Parity Error\n");
383 if (mcsr & MCSR_DCFP)
384 printk("D-Cache Flush Parity Error\n");
385 if (mcsr & MCSR_IMPE)
386 printk("Machine Check exception is imprecise\n");
387
388 /* Clear MCSR */
389 mtspr(SPRN_MCSR, mcsr);
390 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100391 return 0;
392}
Dave Kleikampfc5e7092010-03-05 03:43:18 +0000393
394int machine_check_47x(struct pt_regs *regs)
395{
396 unsigned long reason = get_mc_reason(regs);
397 u32 mcsr;
398
399 printk(KERN_ERR "Machine check in kernel mode.\n");
400 if (reason & ESR_IMCP) {
401 printk(KERN_ERR
402 "Instruction Synchronous Machine Check exception\n");
403 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
404 return 0;
405 }
406 mcsr = mfspr(SPRN_MCSR);
407 if (mcsr & MCSR_IB)
408 printk(KERN_ERR "Instruction Read PLB Error\n");
409 if (mcsr & MCSR_DRB)
410 printk(KERN_ERR "Data Read PLB Error\n");
411 if (mcsr & MCSR_DWB)
412 printk(KERN_ERR "Data Write PLB Error\n");
413 if (mcsr & MCSR_TLBP)
414 printk(KERN_ERR "TLB Parity Error\n");
415 if (mcsr & MCSR_ICP) {
416 flush_instruction_cache();
417 printk(KERN_ERR "I-Cache Parity Error\n");
418 }
419 if (mcsr & MCSR_DCSP)
420 printk(KERN_ERR "D-Cache Search Parity Error\n");
421 if (mcsr & PPC47x_MCSR_GPR)
422 printk(KERN_ERR "GPR Parity Error\n");
423 if (mcsr & PPC47x_MCSR_FPR)
424 printk(KERN_ERR "FPR Parity Error\n");
425 if (mcsr & PPC47x_MCSR_IPR)
426 printk(KERN_ERR "Machine Check exception is imprecise\n");
427
428 /* Clear MCSR */
429 mtspr(SPRN_MCSR, mcsr);
430
431 return 0;
432}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100433#elif defined(CONFIG_E500)
Scott Woodfe04b112010-04-08 00:38:22 -0500434int machine_check_e500mc(struct pt_regs *regs)
435{
436 unsigned long mcsr = mfspr(SPRN_MCSR);
437 unsigned long reason = mcsr;
438 int recoverable = 1;
439
Scott Wood82a9a482011-06-16 14:09:17 -0500440 if (reason & MCSR_LD) {
Shaohui Xiecce1f102010-11-18 14:57:32 +0800441 recoverable = fsl_rio_mcheck_exception(regs);
442 if (recoverable == 1)
443 goto silent_out;
444 }
445
Scott Woodfe04b112010-04-08 00:38:22 -0500446 printk("Machine check in kernel mode.\n");
447 printk("Caused by (from MCSR=%lx): ", reason);
448
449 if (reason & MCSR_MCP)
450 printk("Machine Check Signal\n");
451
452 if (reason & MCSR_ICPERR) {
453 printk("Instruction Cache Parity Error\n");
454
455 /*
456 * This is recoverable by invalidating the i-cache.
457 */
458 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
459 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
460 ;
461
462 /*
463 * This will generally be accompanied by an instruction
464 * fetch error report -- only treat MCSR_IF as fatal
465 * if it wasn't due to an L1 parity error.
466 */
467 reason &= ~MCSR_IF;
468 }
469
470 if (reason & MCSR_DCPERR_MC) {
471 printk("Data Cache Parity Error\n");
Kumar Gala37caf9f2011-08-27 06:14:23 -0500472
473 /*
474 * In write shadow mode we auto-recover from the error, but it
475 * may still get logged and cause a machine check. We should
476 * only treat the non-write shadow case as non-recoverable.
477 */
478 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
479 recoverable = 0;
Scott Woodfe04b112010-04-08 00:38:22 -0500480 }
481
482 if (reason & MCSR_L2MMU_MHIT) {
483 printk("Hit on multiple TLB entries\n");
484 recoverable = 0;
485 }
486
487 if (reason & MCSR_NMI)
488 printk("Non-maskable interrupt\n");
489
490 if (reason & MCSR_IF) {
491 printk("Instruction Fetch Error Report\n");
492 recoverable = 0;
493 }
494
495 if (reason & MCSR_LD) {
496 printk("Load Error Report\n");
497 recoverable = 0;
498 }
499
500 if (reason & MCSR_ST) {
501 printk("Store Error Report\n");
502 recoverable = 0;
503 }
504
505 if (reason & MCSR_LDG) {
506 printk("Guarded Load Error Report\n");
507 recoverable = 0;
508 }
509
510 if (reason & MCSR_TLBSYNC)
511 printk("Simultaneous tlbsync operations\n");
512
513 if (reason & MCSR_BSL2_ERR) {
514 printk("Level 2 Cache Error\n");
515 recoverable = 0;
516 }
517
518 if (reason & MCSR_MAV) {
519 u64 addr;
520
521 addr = mfspr(SPRN_MCAR);
522 addr |= (u64)mfspr(SPRN_MCARU) << 32;
523
524 printk("Machine Check %s Address: %#llx\n",
525 reason & MCSR_MEA ? "Effective" : "Physical", addr);
526 }
527
Shaohui Xiecce1f102010-11-18 14:57:32 +0800528silent_out:
Scott Woodfe04b112010-04-08 00:38:22 -0500529 mtspr(SPRN_MCSR, mcsr);
530 return mfspr(SPRN_MCSR) == 0 && recoverable;
531}
532
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100533int machine_check_e500(struct pt_regs *regs)
534{
535 unsigned long reason = get_mc_reason(regs);
536
Shaohui Xiecce1f102010-11-18 14:57:32 +0800537 if (reason & MCSR_BUS_RBERR) {
538 if (fsl_rio_mcheck_exception(regs))
539 return 1;
540 }
541
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000542 printk("Machine check in kernel mode.\n");
543 printk("Caused by (from MCSR=%lx): ", reason);
544
545 if (reason & MCSR_MCP)
546 printk("Machine Check Signal\n");
547 if (reason & MCSR_ICPERR)
548 printk("Instruction Cache Parity Error\n");
549 if (reason & MCSR_DCP_PERR)
550 printk("Data Cache Push Parity Error\n");
551 if (reason & MCSR_DCPERR)
552 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000553 if (reason & MCSR_BUS_IAERR)
554 printk("Bus - Instruction Address Error\n");
555 if (reason & MCSR_BUS_RAERR)
556 printk("Bus - Read Address Error\n");
557 if (reason & MCSR_BUS_WAERR)
558 printk("Bus - Write Address Error\n");
559 if (reason & MCSR_BUS_IBERR)
560 printk("Bus - Instruction Data Error\n");
561 if (reason & MCSR_BUS_RBERR)
562 printk("Bus - Read Data Bus Error\n");
563 if (reason & MCSR_BUS_WBERR)
564 printk("Bus - Read Data Bus Error\n");
565 if (reason & MCSR_BUS_IPERR)
566 printk("Bus - Instruction Parity Error\n");
567 if (reason & MCSR_BUS_RPERR)
568 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100569
570 return 0;
571}
Kumar Gala4490c062010-10-08 08:32:11 -0500572
573int machine_check_generic(struct pt_regs *regs)
574{
575 return 0;
576}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100577#elif defined(CONFIG_E200)
578int machine_check_e200(struct pt_regs *regs)
579{
580 unsigned long reason = get_mc_reason(regs);
581
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000582 printk("Machine check in kernel mode.\n");
583 printk("Caused by (from MCSR=%lx): ", reason);
584
585 if (reason & MCSR_MCP)
586 printk("Machine Check Signal\n");
587 if (reason & MCSR_CP_PERR)
588 printk("Cache Push Parity Error\n");
589 if (reason & MCSR_CPERR)
590 printk("Cache Parity Error\n");
591 if (reason & MCSR_EXCP_ERR)
592 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
593 if (reason & MCSR_BUS_IRERR)
594 printk("Bus - Read Bus Error on instruction fetch\n");
595 if (reason & MCSR_BUS_DRERR)
596 printk("Bus - Read Bus Error on data load\n");
597 if (reason & MCSR_BUS_WRERR)
598 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100599
600 return 0;
601}
602#else
603int machine_check_generic(struct pt_regs *regs)
604{
605 unsigned long reason = get_mc_reason(regs);
606
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000607 printk("Machine check in kernel mode.\n");
608 printk("Caused by (from SRR1=%lx): ", reason);
609 switch (reason & 0x601F0000) {
610 case 0x80000:
611 printk("Machine check signal\n");
612 break;
613 case 0: /* for 601 */
614 case 0x40000:
615 case 0x140000: /* 7450 MSS error and TEA */
616 printk("Transfer error ack signal\n");
617 break;
618 case 0x20000:
619 printk("Data parity error signal\n");
620 break;
621 case 0x10000:
622 printk("Address parity error signal\n");
623 break;
624 case 0x20000000:
625 printk("L1 Data Cache error\n");
626 break;
627 case 0x40000000:
628 printk("L1 Instruction Cache error\n");
629 break;
630 case 0x00100000:
631 printk("L2 data cache parity error\n");
632 break;
633 default:
634 printk("Unknown values in msr\n");
635 }
Olof Johansson75918a42007-09-21 05:11:20 +1000636 return 0;
637}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100638#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000639
640void machine_check_exception(struct pt_regs *regs)
641{
642 int recover = 0;
643
Anton Blanchard89713ed2010-01-31 20:34:06 +0000644 __get_cpu_var(irq_stat).mce_exceptions++;
645
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100646 /* See if any machine dependent calls. In theory, we would want
647 * to call the CPU first, and call the ppc_md. one if the CPU
648 * one returns a positive number. However there is existing code
649 * that assumes the board gets a first chance, so let's keep it
650 * that way for now and fix things later. --BenH.
651 */
Olof Johansson75918a42007-09-21 05:11:20 +1000652 if (ppc_md.machine_check_exception)
653 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100654 else if (cur_cpu_spec->machine_check)
655 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000656
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100657 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000658 return;
659
Olof Johansson75918a42007-09-21 05:11:20 +1000660#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100661 /* the qspan pci read routines can cause machine checks -- Cort
662 *
663 * yuck !!! that totally needs to go away ! There are better ways
664 * to deal with that than having a wart in the mcheck handler.
665 * -- BenH
666 */
Olof Johansson75918a42007-09-21 05:11:20 +1000667 bad_page_fault(regs, regs->dar, SIGBUS);
668 return;
669#endif
670
Anton Blancharda4435062011-01-11 19:45:31 +0000671 if (debugger_fault_handler(regs))
Olof Johansson75918a42007-09-21 05:11:20 +1000672 return;
Olof Johansson75918a42007-09-21 05:11:20 +1000673
674 if (check_io_access(regs))
675 return;
676
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000677 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000678
679 /* Must die if the interrupt is not recoverable */
680 if (!(regs->msr & MSR_RI))
681 panic("Unrecoverable Machine check");
682}
683
684void SMIException(struct pt_regs *regs)
685{
686 die("System Management Interrupt", regs, SIGABRT);
687}
688
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000689void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000690{
691 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
692 regs->nip, regs->msr, regs->trap);
693
694 _exception(SIGTRAP, regs, 0, 0);
695}
696
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000697void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000698{
699 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
700 5, SIGTRAP) == NOTIFY_STOP)
701 return;
702 if (debugger_iabr_match(regs))
703 return;
704 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
705}
706
707void RunModeException(struct pt_regs *regs)
708{
709 _exception(SIGTRAP, regs, 0, 0);
710}
711
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000712void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000713{
K.Prasad2538c2d2010-06-15 11:35:31 +0530714 clear_single_step(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000715
716 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
717 5, SIGTRAP) == NOTIFY_STOP)
718 return;
719 if (debugger_sstep(regs))
720 return;
721
722 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
723}
724
725/*
726 * After we have successfully emulated an instruction, we have to
727 * check if the instruction was being single-stepped, and if so,
728 * pretend we got a single-step exception. This was pointed out
729 * by Kumar Gala. -- paulus
730 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000731static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000732{
K.Prasad2538c2d2010-06-15 11:35:31 +0530733 if (single_stepping(regs))
734 single_step_exception(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000735}
736
Kumar Gala5fad2932007-02-07 01:47:59 -0600737static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000738{
Kumar Gala5fad2932007-02-07 01:47:59 -0600739 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000740
741 /* Invalid operation */
742 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600743 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000744
745 /* Overflow */
746 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600747 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000748
749 /* Underflow */
750 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600751 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000752
753 /* Divide by zero */
754 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600755 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000756
757 /* Inexact result */
758 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600759 ret = FPE_FLTRES;
760
761 return ret;
762}
763
764static void parse_fpe(struct pt_regs *regs)
765{
766 int code = 0;
767
768 flush_fp_to_thread(current);
769
770 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000771
772 _exception(SIGFPE, regs, code, regs->nip);
773}
774
775/*
776 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000777 * provide the PVR to user applications using the mfspr rd, PVR.
778 * Return non-zero if we can't emulate, or -EFAULT if the associated
779 * memory access caused an access fault. Return zero on success.
780 *
781 * There are a couple of ways to do this, either "decode" the instruction
782 * or directly match lots of bits. In this case, matching lots of
783 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000784 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000785 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000786static int emulate_string_inst(struct pt_regs *regs, u32 instword)
787{
788 u8 rT = (instword >> 21) & 0x1f;
789 u8 rA = (instword >> 16) & 0x1f;
790 u8 NB_RB = (instword >> 11) & 0x1f;
791 u32 num_bytes;
792 unsigned long EA;
793 int pos = 0;
794
795 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000796 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000797 if ((rT == rA) || (rT == NB_RB))
798 return -EINVAL;
799
800 EA = (rA == 0) ? 0 : regs->gpr[rA];
801
Kumar Gala16c57b32009-02-10 20:10:44 +0000802 switch (instword & PPC_INST_STRING_MASK) {
803 case PPC_INST_LSWX:
804 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000805 EA += NB_RB;
806 num_bytes = regs->xer & 0x7f;
807 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000808 case PPC_INST_LSWI:
809 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000810 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
811 break;
812 default:
813 return -EINVAL;
814 }
815
816 while (num_bytes != 0)
817 {
818 u8 val;
819 u32 shift = 8 * (3 - (pos & 0x3));
820
Kumar Gala16c57b32009-02-10 20:10:44 +0000821 switch ((instword & PPC_INST_STRING_MASK)) {
822 case PPC_INST_LSWX:
823 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000824 if (get_user(val, (u8 __user *)EA))
825 return -EFAULT;
826 /* first time updating this reg,
827 * zero it out */
828 if (pos == 0)
829 regs->gpr[rT] = 0;
830 regs->gpr[rT] |= val << shift;
831 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000832 case PPC_INST_STSWI:
833 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000834 val = regs->gpr[rT] >> shift;
835 if (put_user(val, (u8 __user *)EA))
836 return -EFAULT;
837 break;
838 }
839 /* move EA to next address */
840 EA += 1;
841 num_bytes--;
842
843 /* manage our position within the register */
844 if (++pos == 4) {
845 pos = 0;
846 if (++rT == 32)
847 rT = 0;
848 }
849 }
850
851 return 0;
852}
853
Will Schmidtc3412dc2006-08-30 13:11:38 -0500854static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
855{
856 u32 ra,rs;
857 unsigned long tmp;
858
859 ra = (instword >> 16) & 0x1f;
860 rs = (instword >> 21) & 0x1f;
861
862 tmp = regs->gpr[rs];
863 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
864 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
865 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
866 regs->gpr[ra] = tmp;
867
868 return 0;
869}
870
Kumar Galac1469f12007-11-19 21:35:29 -0600871static int emulate_isel(struct pt_regs *regs, u32 instword)
872{
873 u8 rT = (instword >> 21) & 0x1f;
874 u8 rA = (instword >> 16) & 0x1f;
875 u8 rB = (instword >> 11) & 0x1f;
876 u8 BC = (instword >> 6) & 0x1f;
877 u8 bit;
878 unsigned long tmp;
879
880 tmp = (rA == 0) ? 0 : regs->gpr[rA];
881 bit = (regs->ccr >> (31 - BC)) & 0x1;
882
883 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
884
885 return 0;
886}
887
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000888static int emulate_instruction(struct pt_regs *regs)
889{
890 u32 instword;
891 u32 rd;
892
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000893 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000894 return -EINVAL;
895 CHECK_FULL_REGS(regs);
896
897 if (get_user(instword, (u32 __user *)(regs->nip)))
898 return -EFAULT;
899
900 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000901 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000902 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000903 rd = (instword >> 21) & 0x1f;
904 regs->gpr[rd] = mfspr(SPRN_PVR);
905 return 0;
906 }
907
908 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000909 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000910 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000911 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000912 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913
914 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000915 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000916 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000917 unsigned long msk = 0xf0000000UL >> shift;
918
Anton Blanchardeecff812009-10-27 18:46:55 +0000919 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000920 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
921 regs->xer &= ~0xf0000000UL;
922 return 0;
923 }
924
925 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000926 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000927 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000928 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000929 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000930
Will Schmidtc3412dc2006-08-30 13:11:38 -0500931 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000932 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000933 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500934 return emulate_popcntb_inst(regs, instword);
935 }
936
Kumar Galac1469f12007-11-19 21:35:29 -0600937 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000938 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000939 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600940 return emulate_isel(regs, instword);
941 }
942
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000943#ifdef CONFIG_PPC64
944 /* Emulate the mfspr rD, DSCR. */
945 if (((instword & PPC_INST_MFSPR_DSCR_MASK) == PPC_INST_MFSPR_DSCR) &&
946 cpu_has_feature(CPU_FTR_DSCR)) {
947 PPC_WARN_EMULATED(mfdscr, regs);
948 rd = (instword >> 21) & 0x1f;
949 regs->gpr[rd] = mfspr(SPRN_DSCR);
950 return 0;
951 }
952 /* Emulate the mtspr DSCR, rD. */
953 if (((instword & PPC_INST_MTSPR_DSCR_MASK) == PPC_INST_MTSPR_DSCR) &&
954 cpu_has_feature(CPU_FTR_DSCR)) {
955 PPC_WARN_EMULATED(mtdscr, regs);
956 rd = (instword >> 21) & 0x1f;
957 mtspr(SPRN_DSCR, regs->gpr[rd]);
958 current->thread.dscr_inherit = 1;
959 return 0;
960 }
961#endif
962
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000963 return -EINVAL;
964}
965
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800966int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000967{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800968 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000969}
970
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000971void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000972{
973 unsigned int reason = get_reason(regs);
974 extern int do_mathemu(struct pt_regs *regs);
975
Kim Phillipsaa42c692006-12-08 02:43:30 -0600976 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600977 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000978
979 if (reason & REASON_FP) {
980 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000981 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000982 return;
983 }
984 if (reason & REASON_TRAP) {
Jason Wesselba797b22010-05-20 21:04:25 -0500985 /* Debugger is first in line to stop recursive faults in
986 * rcu_lock, notify_die, or atomic_notifier_call_chain */
987 if (debugger_bpt(regs))
988 return;
989
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000990 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000991 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
992 == NOTIFY_STOP)
993 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800994
995 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700996 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000997 regs->nip += 4;
998 return;
999 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001000 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1001 return;
1002 }
1003
Paul Mackerrascd8a5672006-03-03 17:11:40 +11001004 local_irq_enable();
1005
Kumar Gala04903a32007-02-07 01:13:32 -06001006#ifdef CONFIG_MATH_EMULATION
1007 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1008 * but there seems to be a hardware bug on the 405GP (RevD)
1009 * that means ESR is sometimes set incorrectly - either to
1010 * ESR_DST (!?) or 0. In the process of chasing this with the
1011 * hardware people - not sure if it can happen on any illegal
1012 * instruction or only on FP instructions, whether there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001013 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -06001014 switch (do_mathemu(regs)) {
1015 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -06001016 emulate_single_step(regs);
1017 return;
Kumar Gala5fad2932007-02-07 01:47:59 -06001018 case 1: {
1019 int code = 0;
1020 code = __parse_fpscr(current->thread.fpscr.val);
1021 _exception(SIGFPE, regs, code, regs->nip);
1022 return;
1023 }
1024 case -EFAULT:
1025 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1026 return;
Kumar Gala04903a32007-02-07 01:13:32 -06001027 }
Kumar Gala5fad2932007-02-07 01:47:59 -06001028 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -06001029#endif /* CONFIG_MATH_EMULATION */
1030
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001031 /* Try to emulate it if we should. */
1032 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001033 switch (emulate_instruction(regs)) {
1034 case 0:
1035 regs->nip += 4;
1036 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001037 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001038 case -EFAULT:
1039 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001040 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001041 }
1042 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001043
1044 if (reason & REASON_PRIVILEGED)
1045 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1046 else
1047 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001048}
1049
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001050void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001051{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001052 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001053
Paul Mackerrase9370ae2006-06-07 16:15:39 +10001054 /* we don't implement logging of alignment exceptions */
1055 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1056 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001057
1058 if (fixed == 1) {
1059 regs->nip += 4; /* skip over emulated instruction */
1060 emulate_single_step(regs);
1061 return;
1062 }
1063
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001064 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001065 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001066 sig = SIGSEGV;
1067 code = SEGV_ACCERR;
1068 } else {
1069 sig = SIGBUS;
1070 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001071 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +11001072 if (user_mode(regs))
1073 _exception(sig, regs, code, regs->dar);
1074 else
1075 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001076}
1077
1078void StackOverflow(struct pt_regs *regs)
1079{
1080 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1081 current, regs->gpr[1]);
1082 debugger(regs);
1083 show_regs(regs);
1084 panic("kernel stack overflow");
1085}
1086
1087void nonrecoverable_exception(struct pt_regs *regs)
1088{
1089 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1090 regs->nip, regs->msr);
1091 debugger(regs);
1092 die("nonrecoverable exception", regs, SIGKILL);
1093}
1094
1095void trace_syscall(struct pt_regs *regs)
1096{
1097 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -07001098 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001099 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1100}
1101
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001102void kernel_fp_unavailable_exception(struct pt_regs *regs)
1103{
1104 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1105 "%lx at %lx\n", regs->trap, regs->nip);
1106 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1107}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001108
1109void altivec_unavailable_exception(struct pt_regs *regs)
1110{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001111 if (user_mode(regs)) {
1112 /* A user program has executed an altivec instruction,
1113 but this kernel doesn't support altivec. */
1114 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1115 return;
1116 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +10001117
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001118 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1119 "%lx at %lx\n", regs->trap, regs->nip);
1120 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001121}
1122
Michael Neulingce48b212008-06-25 14:07:18 +10001123void vsx_unavailable_exception(struct pt_regs *regs)
1124{
1125 if (user_mode(regs)) {
1126 /* A user program has executed an vsx instruction,
1127 but this kernel doesn't support vsx. */
1128 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1129 return;
1130 }
1131
1132 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1133 "%lx at %lx\n", regs->trap, regs->nip);
1134 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1135}
1136
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001137void performance_monitor_exception(struct pt_regs *regs)
1138{
Anton Blanchard89713ed2010-01-31 20:34:06 +00001139 __get_cpu_var(irq_stat).pmu_irqs++;
1140
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001141 perf_irq(regs);
1142}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001143
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001144#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001145void SoftwareEmulation(struct pt_regs *regs)
1146{
1147 extern int do_mathemu(struct pt_regs *);
1148 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -05001149#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001150 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -05001151#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001152
1153 CHECK_FULL_REGS(regs);
1154
1155 if (!user_mode(regs)) {
1156 debugger(regs);
1157 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1158 }
1159
1160#ifdef CONFIG_MATH_EMULATION
1161 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001162 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001163 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001164
1165 switch (errcode) {
1166 case 0:
1167 emulate_single_step(regs);
1168 return;
1169 case 1: {
1170 int code = 0;
1171 code = __parse_fpscr(current->thread.fpscr.val);
1172 _exception(SIGFPE, regs, code, regs->nip);
1173 return;
1174 }
1175 case -EFAULT:
1176 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1177 return;
1178 default:
1179 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1180 return;
1181 }
1182
Scott Wood5dd57a12007-09-18 15:29:35 -05001183#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001184 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001185 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001186 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001187
Kumar Gala5fad2932007-02-07 01:47:59 -06001188 switch (errcode) {
1189 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001190 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001191 return;
1192 case 1:
1193 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1194 return;
1195 case -EFAULT:
1196 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1197 return;
1198 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001199#else
1200 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001201#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001202}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001203#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001204
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001205#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001206static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1207{
1208 int changed = 0;
1209 /*
1210 * Determine the cause of the debug event, clear the
1211 * event flags and send a trap to the handler. Torez
1212 */
1213 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1214 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1215#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1216 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1217#endif
1218 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1219 5);
1220 changed |= 0x01;
1221 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1222 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1223 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1224 6);
1225 changed |= 0x01;
1226 } else if (debug_status & DBSR_IAC1) {
1227 current->thread.dbcr0 &= ~DBCR0_IAC1;
1228 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1229 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1230 1);
1231 changed |= 0x01;
1232 } else if (debug_status & DBSR_IAC2) {
1233 current->thread.dbcr0 &= ~DBCR0_IAC2;
1234 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1235 2);
1236 changed |= 0x01;
1237 } else if (debug_status & DBSR_IAC3) {
1238 current->thread.dbcr0 &= ~DBCR0_IAC3;
1239 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1240 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1241 3);
1242 changed |= 0x01;
1243 } else if (debug_status & DBSR_IAC4) {
1244 current->thread.dbcr0 &= ~DBCR0_IAC4;
1245 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1246 4);
1247 changed |= 0x01;
1248 }
1249 /*
1250 * At the point this routine was called, the MSR(DE) was turned off.
1251 * Check all other debug flags and see if that bit needs to be turned
1252 * back on or not.
1253 */
1254 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1255 regs->msr |= MSR_DE;
1256 else
1257 /* Make sure the IDM flag is off */
1258 current->thread.dbcr0 &= ~DBCR0_IDM;
1259
1260 if (changed & 0x01)
1261 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1262}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001263
Kumar Galaf8279622008-06-26 02:01:37 -05001264void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001265{
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001266 current->thread.dbsr = debug_status;
1267
Roland McGrathec097c82009-05-28 21:26:38 +00001268 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1269 * on server, it stops on the target of the branch. In order to simulate
1270 * the server behaviour, we thus restart right away with a single step
1271 * instead of stopping here when hitting a BT
1272 */
1273 if (debug_status & DBSR_BT) {
1274 regs->msr &= ~MSR_DE;
1275
1276 /* Disable BT */
1277 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1278 /* Clear the BT event */
1279 mtspr(SPRN_DBSR, DBSR_BT);
1280
1281 /* Do the single step trick only when coming from userspace */
1282 if (user_mode(regs)) {
1283 current->thread.dbcr0 &= ~DBCR0_BT;
1284 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1285 regs->msr |= MSR_DE;
1286 return;
1287 }
1288
1289 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1290 5, SIGTRAP) == NOTIFY_STOP) {
1291 return;
1292 }
1293 if (debugger_sstep(regs))
1294 return;
1295 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001296 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001297
1298 /* Disable instruction completion */
1299 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1300 /* Clear the instruction completion event */
1301 mtspr(SPRN_DBSR, DBSR_IC);
1302
1303 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1304 5, SIGTRAP) == NOTIFY_STOP) {
1305 return;
1306 }
1307
1308 if (debugger_sstep(regs))
1309 return;
1310
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001311 if (user_mode(regs)) {
1312 current->thread.dbcr0 &= ~DBCR0_IC;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001313 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1314 current->thread.dbcr1))
1315 regs->msr |= MSR_DE;
1316 else
1317 /* Make sure the IDM bit is off */
1318 current->thread.dbcr0 &= ~DBCR0_IDM;
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001319 }
Kumar Galaf8279622008-06-26 02:01:37 -05001320
1321 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Dave Kleikamp3bffb652010-02-08 11:51:18 +00001322 } else
1323 handle_debug(regs, debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001324}
Dave Kleikamp172ae2e2010-02-08 11:50:57 +00001325#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001326
1327#if !defined(CONFIG_TAU_INT)
1328void TAUException(struct pt_regs *regs)
1329{
1330 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1331 regs->nip, regs->msr, regs->trap, print_tainted());
1332}
1333#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001334
1335#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001336void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001337{
1338 int err;
1339
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001340 if (!user_mode(regs)) {
1341 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1342 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001343 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001344 }
1345
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001346 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001347
Anton Blanchardeecff812009-10-27 18:46:55 +00001348 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001349 err = emulate_altivec(regs);
1350 if (err == 0) {
1351 regs->nip += 4; /* skip emulated instruction */
1352 emulate_single_step(regs);
1353 return;
1354 }
1355
1356 if (err == -EFAULT) {
1357 /* got an error reading the instruction */
1358 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1359 } else {
1360 /* didn't recognize the instruction */
1361 /* XXX quick hack for now: set the non-Java bit in the VSCR */
Christian Dietrich76462232011-06-04 05:36:54 +00001362 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1363 "in %s at %lx\n", current->comm, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001364 current->thread.vscr.u[3] |= 0x10000;
1365 }
1366}
1367#endif /* CONFIG_ALTIVEC */
1368
Michael Neulingce48b212008-06-25 14:07:18 +10001369#ifdef CONFIG_VSX
1370void vsx_assist_exception(struct pt_regs *regs)
1371{
1372 if (!user_mode(regs)) {
1373 printk(KERN_EMERG "VSX assist exception in kernel mode"
1374 " at %lx\n", regs->nip);
1375 die("Kernel VSX assist exception", regs, SIGILL);
1376 }
1377
1378 flush_vsx_to_thread(current);
1379 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1380 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1381}
1382#endif /* CONFIG_VSX */
1383
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001384#ifdef CONFIG_FSL_BOOKE
1385void CacheLockingException(struct pt_regs *regs, unsigned long address,
1386 unsigned long error_code)
1387{
1388 /* We treat cache locking instructions from the user
1389 * as priv ops, in the future we could try to do
1390 * something smarter
1391 */
1392 if (error_code & (ESR_DLK|ESR_ILK))
1393 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1394 return;
1395}
1396#endif /* CONFIG_FSL_BOOKE */
1397
1398#ifdef CONFIG_SPE
1399void SPEFloatingPointException(struct pt_regs *regs)
1400{
Liu Yu6a800f32008-10-28 11:50:21 +08001401 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001402 unsigned long spefscr;
1403 int fpexc_mode;
1404 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001405 int err;
1406
yu liu685659e2011-06-14 18:34:25 -05001407 flush_spe_to_thread(current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001408
1409 spefscr = current->thread.spefscr;
1410 fpexc_mode = current->thread.fpexc_mode;
1411
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001412 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1413 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001414 }
1415 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1416 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001417 }
1418 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1419 code = FPE_FLTDIV;
1420 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1421 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001422 }
1423 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1424 code = FPE_FLTRES;
1425
Liu Yu6a800f32008-10-28 11:50:21 +08001426 err = do_spe_mathemu(regs);
1427 if (err == 0) {
1428 regs->nip += 4; /* skip emulated instruction */
1429 emulate_single_step(regs);
1430 return;
1431 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001432
Liu Yu6a800f32008-10-28 11:50:21 +08001433 if (err == -EFAULT) {
1434 /* got an error reading the instruction */
1435 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1436 } else if (err == -EINVAL) {
1437 /* didn't recognize the instruction */
1438 printk(KERN_ERR "unrecognized spe instruction "
1439 "in %s at %lx\n", current->comm, regs->nip);
1440 } else {
1441 _exception(SIGFPE, regs, code, regs->nip);
1442 }
1443
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001444 return;
1445}
Liu Yu6a800f32008-10-28 11:50:21 +08001446
1447void SPEFloatingPointRoundException(struct pt_regs *regs)
1448{
1449 extern int speround_handler(struct pt_regs *regs);
1450 int err;
1451
1452 preempt_disable();
1453 if (regs->msr & MSR_SPE)
1454 giveup_spe(current);
1455 preempt_enable();
1456
1457 regs->nip -= 4;
1458 err = speround_handler(regs);
1459 if (err == 0) {
1460 regs->nip += 4; /* skip emulated instruction */
1461 emulate_single_step(regs);
1462 return;
1463 }
1464
1465 if (err == -EFAULT) {
1466 /* got an error reading the instruction */
1467 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1468 } else if (err == -EINVAL) {
1469 /* didn't recognize the instruction */
1470 printk(KERN_ERR "unrecognized spe instruction "
1471 "in %s at %lx\n", current->comm, regs->nip);
1472 } else {
1473 _exception(SIGFPE, regs, 0, regs->nip);
1474 return;
1475 }
1476}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001477#endif
1478
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001479/*
1480 * We enter here if we get an unrecoverable exception, that is, one
1481 * that happened at a point where the RI (recoverable interrupt) bit
1482 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1483 * we therefore lost state by taking this exception.
1484 */
1485void unrecoverable_exception(struct pt_regs *regs)
1486{
1487 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1488 regs->trap, regs->nip);
1489 die("Unrecoverable exception", regs, SIGABRT);
1490}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001491
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001492#ifdef CONFIG_BOOKE_WDT
1493/*
1494 * Default handler for a Watchdog exception,
1495 * spins until a reboot occurs
1496 */
1497void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1498{
1499 /* Generic WatchdogHandler, implement your own */
1500 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1501 return;
1502}
1503
1504void WatchdogException(struct pt_regs *regs)
1505{
1506 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1507 WatchdogHandler(regs);
1508}
1509#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001510
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001511/*
1512 * We enter here if we discover during exception entry that we are
1513 * running in supervisor mode with a userspace value in the stack pointer.
1514 */
1515void kernel_bad_stack(struct pt_regs *regs)
1516{
1517 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1518 regs->gpr[1], regs->nip);
1519 die("Bad kernel stack pointer", regs, SIGABRT);
1520}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001521
1522void __init trap_init(void)
1523{
1524}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001525
1526
1527#ifdef CONFIG_PPC_EMULATED_STATS
1528
1529#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1530
1531struct ppc_emulated ppc_emulated = {
1532#ifdef CONFIG_ALTIVEC
1533 WARN_EMULATED_SETUP(altivec),
1534#endif
1535 WARN_EMULATED_SETUP(dcba),
1536 WARN_EMULATED_SETUP(dcbz),
1537 WARN_EMULATED_SETUP(fp_pair),
1538 WARN_EMULATED_SETUP(isel),
1539 WARN_EMULATED_SETUP(mcrxr),
1540 WARN_EMULATED_SETUP(mfpvr),
1541 WARN_EMULATED_SETUP(multiple),
1542 WARN_EMULATED_SETUP(popcntb),
1543 WARN_EMULATED_SETUP(spe),
1544 WARN_EMULATED_SETUP(string),
1545 WARN_EMULATED_SETUP(unaligned),
1546#ifdef CONFIG_MATH_EMULATION
1547 WARN_EMULATED_SETUP(math),
1548#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1549 WARN_EMULATED_SETUP(8xx),
1550#endif
1551#ifdef CONFIG_VSX
1552 WARN_EMULATED_SETUP(vsx),
1553#endif
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001554#ifdef CONFIG_PPC64
1555 WARN_EMULATED_SETUP(mfdscr),
1556 WARN_EMULATED_SETUP(mtdscr),
1557#endif
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001558};
1559
1560u32 ppc_warn_emulated;
1561
1562void ppc_warn_emulated_print(const char *type)
1563{
Christian Dietrich76462232011-06-04 05:36:54 +00001564 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
1565 type);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001566}
1567
1568static int __init ppc_warn_emulated_init(void)
1569{
1570 struct dentry *dir, *d;
1571 unsigned int i;
1572 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1573
1574 if (!powerpc_debugfs_root)
1575 return -ENODEV;
1576
1577 dir = debugfs_create_dir("emulated_instructions",
1578 powerpc_debugfs_root);
1579 if (!dir)
1580 return -ENOMEM;
1581
1582 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1583 &ppc_warn_emulated);
1584 if (!d)
1585 goto fail;
1586
1587 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1588 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1589 (u32 *)&entries[i].val.counter);
1590 if (!d)
1591 goto fail;
1592 }
1593
1594 return 0;
1595
1596fail:
1597 debugfs_remove_recursive(dir);
1598 return -ENOMEM;
1599}
1600
1601device_initcall(ppc_warn_emulated_init);
1602
1603#endif /* CONFIG_PPC_EMULATED_STATS */