blob: 804f0f30f227f692d949d49117b3c35da908fc6f [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)
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Modified by Cort Dougan (cort@cs.nmt.edu)
10 * and Paul Mackerras (paulus@samba.org)
11 */
12
13/*
14 * This file handles the architecture-dependent parts of hardware exceptions
15 */
16
Paul Mackerras14cf11a2005-09-26 16:04:21 +100017#include <linux/errno.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/stddef.h>
22#include <linux/unistd.h>
Paul Mackerras8dad3f92005-10-06 13:27:05 +100023#include <linux/ptrace.h>
Paul Mackerras14cf11a2005-09-26 16:04:21 +100024#include <linux/slab.h>
25#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>
Kumar Gala620165f2009-02-12 13:54:53 +000058#ifdef CONFIG_FSL_BOOKE
59#include <asm/dbell.h>
60#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +100061
Olof Johansson7dbb9222008-01-31 14:34:47 +110062#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
Paul Mackerras14cf11a2005-09-26 16:04:21 +100063int (*__debugger)(struct pt_regs *regs);
64int (*__debugger_ipi)(struct pt_regs *regs);
65int (*__debugger_bpt)(struct pt_regs *regs);
66int (*__debugger_sstep)(struct pt_regs *regs);
67int (*__debugger_iabr_match)(struct pt_regs *regs);
68int (*__debugger_dabr_match)(struct pt_regs *regs);
69int (*__debugger_fault_handler)(struct pt_regs *regs);
70
71EXPORT_SYMBOL(__debugger);
72EXPORT_SYMBOL(__debugger_ipi);
73EXPORT_SYMBOL(__debugger_bpt);
74EXPORT_SYMBOL(__debugger_sstep);
75EXPORT_SYMBOL(__debugger_iabr_match);
76EXPORT_SYMBOL(__debugger_dabr_match);
77EXPORT_SYMBOL(__debugger_fault_handler);
78#endif
79
Paul Mackerras14cf11a2005-09-26 16:04:21 +100080/*
81 * Trap & Exception support
82 */
83
anton@samba.org6031d9d2007-03-20 20:38:12 -050084#ifdef CONFIG_PMAC_BACKLIGHT
85static void pmac_backlight_unblank(void)
86{
87 mutex_lock(&pmac_backlight_mutex);
88 if (pmac_backlight) {
89 struct backlight_properties *props;
90
91 props = &pmac_backlight->props;
92 props->brightness = props->max_brightness;
93 props->power = FB_BLANK_UNBLANK;
94 backlight_update_status(pmac_backlight);
95 }
96 mutex_unlock(&pmac_backlight_mutex);
97}
98#else
99static inline void pmac_backlight_unblank(void) { }
100#endif
101
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000102int die(const char *str, struct pt_regs *regs, long err)
103{
anton@samba.org34c2a142007-03-20 20:38:13 -0500104 static struct {
105 spinlock_t lock;
106 u32 lock_owner;
107 int lock_owner_depth;
108 } die = {
109 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
110 .lock_owner = -1,
111 .lock_owner_depth = 0
112 };
David Wilderc0ce7d02006-06-23 15:29:34 -0700113 static int die_counter;
anton@samba.org34c2a142007-03-20 20:38:13 -0500114 unsigned long flags;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000115
116 if (debugger(regs))
117 return 1;
118
anton@samba.org293e4682007-03-20 20:38:11 -0500119 oops_enter();
120
anton@samba.org34c2a142007-03-20 20:38:13 -0500121 if (die.lock_owner != raw_smp_processor_id()) {
122 console_verbose();
123 spin_lock_irqsave(&die.lock, flags);
124 die.lock_owner = smp_processor_id();
125 die.lock_owner_depth = 0;
126 bust_spinlocks(1);
127 if (machine_is(powermac))
128 pmac_backlight_unblank();
129 } else {
130 local_save_flags(flags);
131 }
Michael Hanselmann5474c122006-06-25 05:47:08 -0700132
anton@samba.org34c2a142007-03-20 20:38:13 -0500133 if (++die.lock_owner_depth < 3) {
134 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000135#ifdef CONFIG_PREEMPT
anton@samba.org34c2a142007-03-20 20:38:13 -0500136 printk("PREEMPT ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000137#endif
138#ifdef CONFIG_SMP
anton@samba.org34c2a142007-03-20 20:38:13 -0500139 printk("SMP NR_CPUS=%d ", NR_CPUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000140#endif
141#ifdef CONFIG_DEBUG_PAGEALLOC
anton@samba.org34c2a142007-03-20 20:38:13 -0500142 printk("DEBUG_PAGEALLOC ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000143#endif
144#ifdef CONFIG_NUMA
anton@samba.org34c2a142007-03-20 20:38:13 -0500145 printk("NUMA ");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000146#endif
anton@samba.orgae7f4462007-03-20 20:38:14 -0500147 printk("%s\n", ppc_md.name ? ppc_md.name : "");
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100148
anton@samba.org34c2a142007-03-20 20:38:13 -0500149 print_modules();
150 show_regs(regs);
151 } else {
152 printk("Recursive die() failure, output suppressed\n");
153 }
154
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000155 bust_spinlocks(0);
anton@samba.org34c2a142007-03-20 20:38:13 -0500156 die.lock_owner = -1;
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700157 add_taint(TAINT_DIE);
anton@samba.org34c2a142007-03-20 20:38:13 -0500158 spin_unlock_irqrestore(&die.lock, flags);
David Wilderc0ce7d02006-06-23 15:29:34 -0700159
160 if (kexec_should_crash(current) ||
161 kexec_sr_activated(smp_processor_id()))
162 crash_kexec(regs);
163 crash_kexec_secondary(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000164
165 if (in_interrupt())
166 panic("Fatal exception in interrupt");
167
Hormscea6a4b2006-07-30 03:03:34 -0700168 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700169 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700170
anton@samba.org293e4682007-03-20 20:38:11 -0500171 oops_exit();
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000172 do_exit(err);
173
174 return 0;
175}
176
177void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
178{
179 siginfo_t info;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000180 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
181 "at %08lx nip %08lx lr %08lx code %x\n";
182 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
183 "at %016lx nip %016lx lr %016lx code %x\n";
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000184
185 if (!user_mode(regs)) {
186 if (die("Exception in kernel mode", regs, signr))
187 return;
Olof Johanssond0c3d532007-10-12 10:20:07 +1000188 } else if (show_unhandled_signals &&
189 unhandled_signal(current, signr) &&
190 printk_ratelimit()) {
191 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
192 current->comm, current->pid, signr,
193 addr, regs->nip, regs->link, code);
194 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000195
196 memset(&info, 0, sizeof(info));
197 info.si_signo = signr;
198 info.si_code = code;
199 info.si_addr = (void __user *) addr;
200 force_sig_info(signr, &info, current);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000201}
202
203#ifdef CONFIG_PPC64
204void system_reset_exception(struct pt_regs *regs)
205{
206 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000207 if (ppc_md.system_reset_exception) {
208 if (ppc_md.system_reset_exception(regs))
209 return;
210 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000211
David Wilderc0ce7d02006-06-23 15:29:34 -0700212#ifdef CONFIG_KEXEC
213 cpu_set(smp_processor_id(), cpus_in_sr);
214#endif
215
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000216 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000217
David Wildereac83922006-06-29 15:17:30 -0700218 /*
219 * Some CPUs when released from the debugger will execute this path.
220 * These CPUs entered the debugger via a soft-reset. If the CPU was
221 * hung before entering the debugger it will return to the hung
222 * state when exiting this function. This causes a problem in
223 * kdump since the hung CPU(s) will not respond to the IPI sent
224 * from kdump. To prevent the problem we call crash_kexec_secondary()
225 * here. If a kdump had not been initiated or we exit the debugger
226 * with the "exit and recover" command (x) crash_kexec_secondary()
227 * will return after 5ms and the CPU returns to its previous state.
228 */
229 crash_kexec_secondary(regs);
230
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000231 /* Must die if the interrupt is not recoverable */
232 if (!(regs->msr & MSR_RI))
233 panic("Unrecoverable System Reset");
234
235 /* What should we do here? We could issue a shutdown or hard reset. */
236}
237#endif
238
239/*
240 * I/O accesses can cause machine checks on powermacs.
241 * Check if the NIP corresponds to the address of a sync
242 * instruction for which there is an entry in the exception
243 * table.
244 * Note that the 601 only takes a machine check on TEA
245 * (transfer error ack) signal assertion, and does not
246 * set any of the top 16 bits of SRR1.
247 * -- paulus.
248 */
249static inline int check_io_access(struct pt_regs *regs)
250{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100251#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000252 unsigned long msr = regs->msr;
253 const struct exception_table_entry *entry;
254 unsigned int *nip = (unsigned int *)regs->nip;
255
256 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
257 && (entry = search_exception_tables(regs->nip)) != NULL) {
258 /*
259 * Check that it's a sync instruction, or somewhere
260 * in the twi; isync; nop sequence that inb/inw/inl uses.
261 * As the address is in the exception table
262 * we should be able to read the instr there.
263 * For the debug message, we look at the preceding
264 * load or store.
265 */
266 if (*nip == 0x60000000) /* nop */
267 nip -= 2;
268 else if (*nip == 0x4c00012c) /* isync */
269 --nip;
270 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
271 /* sync or twi */
272 unsigned int rb;
273
274 --nip;
275 rb = (*nip >> 11) & 0x1f;
276 printk(KERN_DEBUG "%s bad port %lx at %p\n",
277 (*nip & 0x100)? "OUT to": "IN from",
278 regs->gpr[rb] - _IO_BASE, nip);
279 regs->msr |= MSR_RI;
280 regs->nip = entry->fixup;
281 return 1;
282 }
283 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100284#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000285 return 0;
286}
287
288#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
289/* On 4xx, the reason for the machine check or program exception
290 is in the ESR. */
291#define get_reason(regs) ((regs)->dsisr)
292#ifndef CONFIG_FSL_BOOKE
293#define get_mc_reason(regs) ((regs)->dsisr)
294#else
Becky Bruce86d7a9a2007-08-02 15:37:15 -0500295#define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000296#endif
297#define REASON_FP ESR_FP
298#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
299#define REASON_PRIVILEGED ESR_PPR
300#define REASON_TRAP ESR_PTR
301
302/* single-step stuff */
303#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
304#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
305
306#else
307/* On non-4xx, the reason for the machine check or program
308 exception is in the MSR. */
309#define get_reason(regs) ((regs)->msr)
310#define get_mc_reason(regs) ((regs)->msr)
311#define REASON_FP 0x100000
312#define REASON_ILLEGAL 0x80000
313#define REASON_PRIVILEGED 0x40000
314#define REASON_TRAP 0x20000
315
316#define single_stepping(regs) ((regs)->msr & MSR_SE)
317#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
318#endif
319
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100320#if defined(CONFIG_4xx)
321int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000322{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600323 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000324
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000325 if (reason & ESR_IMCP) {
326 printk("Instruction");
327 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
328 } else
329 printk("Data");
330 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100331
332 return 0;
333}
334
335int machine_check_440A(struct pt_regs *regs)
336{
337 unsigned long reason = get_mc_reason(regs);
338
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000339 printk("Machine check in kernel mode.\n");
340 if (reason & ESR_IMCP){
341 printk("Instruction Synchronous Machine Check exception\n");
342 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
343 }
344 else {
345 u32 mcsr = mfspr(SPRN_MCSR);
346 if (mcsr & MCSR_IB)
347 printk("Instruction Read PLB Error\n");
348 if (mcsr & MCSR_DRB)
349 printk("Data Read PLB Error\n");
350 if (mcsr & MCSR_DWB)
351 printk("Data Write PLB Error\n");
352 if (mcsr & MCSR_TLBP)
353 printk("TLB Parity Error\n");
354 if (mcsr & MCSR_ICP){
355 flush_instruction_cache();
356 printk("I-Cache Parity Error\n");
357 }
358 if (mcsr & MCSR_DCSP)
359 printk("D-Cache Search Parity Error\n");
360 if (mcsr & MCSR_DCFP)
361 printk("D-Cache Flush Parity Error\n");
362 if (mcsr & MCSR_IMPE)
363 printk("Machine Check exception is imprecise\n");
364
365 /* Clear MCSR */
366 mtspr(SPRN_MCSR, mcsr);
367 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100368 return 0;
369}
370#elif defined(CONFIG_E500)
371int machine_check_e500(struct pt_regs *regs)
372{
373 unsigned long reason = get_mc_reason(regs);
374
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000375 printk("Machine check in kernel mode.\n");
376 printk("Caused by (from MCSR=%lx): ", reason);
377
378 if (reason & MCSR_MCP)
379 printk("Machine Check Signal\n");
380 if (reason & MCSR_ICPERR)
381 printk("Instruction Cache Parity Error\n");
382 if (reason & MCSR_DCP_PERR)
383 printk("Data Cache Push Parity Error\n");
384 if (reason & MCSR_DCPERR)
385 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000386 if (reason & MCSR_BUS_IAERR)
387 printk("Bus - Instruction Address Error\n");
388 if (reason & MCSR_BUS_RAERR)
389 printk("Bus - Read Address Error\n");
390 if (reason & MCSR_BUS_WAERR)
391 printk("Bus - Write Address Error\n");
392 if (reason & MCSR_BUS_IBERR)
393 printk("Bus - Instruction Data Error\n");
394 if (reason & MCSR_BUS_RBERR)
395 printk("Bus - Read Data Bus Error\n");
396 if (reason & MCSR_BUS_WBERR)
397 printk("Bus - Read Data Bus Error\n");
398 if (reason & MCSR_BUS_IPERR)
399 printk("Bus - Instruction Parity Error\n");
400 if (reason & MCSR_BUS_RPERR)
401 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100402
403 return 0;
404}
405#elif defined(CONFIG_E200)
406int machine_check_e200(struct pt_regs *regs)
407{
408 unsigned long reason = get_mc_reason(regs);
409
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000410 printk("Machine check in kernel mode.\n");
411 printk("Caused by (from MCSR=%lx): ", reason);
412
413 if (reason & MCSR_MCP)
414 printk("Machine Check Signal\n");
415 if (reason & MCSR_CP_PERR)
416 printk("Cache Push Parity Error\n");
417 if (reason & MCSR_CPERR)
418 printk("Cache Parity Error\n");
419 if (reason & MCSR_EXCP_ERR)
420 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
421 if (reason & MCSR_BUS_IRERR)
422 printk("Bus - Read Bus Error on instruction fetch\n");
423 if (reason & MCSR_BUS_DRERR)
424 printk("Bus - Read Bus Error on data load\n");
425 if (reason & MCSR_BUS_WRERR)
426 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100427
428 return 0;
429}
430#else
431int machine_check_generic(struct pt_regs *regs)
432{
433 unsigned long reason = get_mc_reason(regs);
434
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000435 printk("Machine check in kernel mode.\n");
436 printk("Caused by (from SRR1=%lx): ", reason);
437 switch (reason & 0x601F0000) {
438 case 0x80000:
439 printk("Machine check signal\n");
440 break;
441 case 0: /* for 601 */
442 case 0x40000:
443 case 0x140000: /* 7450 MSS error and TEA */
444 printk("Transfer error ack signal\n");
445 break;
446 case 0x20000:
447 printk("Data parity error signal\n");
448 break;
449 case 0x10000:
450 printk("Address parity error signal\n");
451 break;
452 case 0x20000000:
453 printk("L1 Data Cache error\n");
454 break;
455 case 0x40000000:
456 printk("L1 Instruction Cache error\n");
457 break;
458 case 0x00100000:
459 printk("L2 data cache parity error\n");
460 break;
461 default:
462 printk("Unknown values in msr\n");
463 }
Olof Johansson75918a42007-09-21 05:11:20 +1000464 return 0;
465}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100466#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000467
468void machine_check_exception(struct pt_regs *regs)
469{
470 int recover = 0;
471
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100472 /* See if any machine dependent calls. In theory, we would want
473 * to call the CPU first, and call the ppc_md. one if the CPU
474 * one returns a positive number. However there is existing code
475 * that assumes the board gets a first chance, so let's keep it
476 * that way for now and fix things later. --BenH.
477 */
Olof Johansson75918a42007-09-21 05:11:20 +1000478 if (ppc_md.machine_check_exception)
479 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100480 else if (cur_cpu_spec->machine_check)
481 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000482
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100483 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000484 return;
485
486 if (user_mode(regs)) {
487 regs->msr |= MSR_RI;
488 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
489 return;
490 }
491
492#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100493 /* the qspan pci read routines can cause machine checks -- Cort
494 *
495 * yuck !!! that totally needs to go away ! There are better ways
496 * to deal with that than having a wart in the mcheck handler.
497 * -- BenH
498 */
Olof Johansson75918a42007-09-21 05:11:20 +1000499 bad_page_fault(regs, regs->dar, SIGBUS);
500 return;
501#endif
502
503 if (debugger_fault_handler(regs)) {
504 regs->msr |= MSR_RI;
505 return;
506 }
507
508 if (check_io_access(regs))
509 return;
510
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000511 if (debugger_fault_handler(regs))
512 return;
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000513 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000514
515 /* Must die if the interrupt is not recoverable */
516 if (!(regs->msr & MSR_RI))
517 panic("Unrecoverable Machine check");
518}
519
520void SMIException(struct pt_regs *regs)
521{
522 die("System Management Interrupt", regs, SIGABRT);
523}
524
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000525void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000526{
527 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
528 regs->nip, regs->msr, regs->trap);
529
530 _exception(SIGTRAP, regs, 0, 0);
531}
532
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000533void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000534{
535 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
536 5, SIGTRAP) == NOTIFY_STOP)
537 return;
538 if (debugger_iabr_match(regs))
539 return;
540 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
541}
542
543void RunModeException(struct pt_regs *regs)
544{
545 _exception(SIGTRAP, regs, 0, 0);
546}
547
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000548void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000549{
550 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
551
552 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
553 5, SIGTRAP) == NOTIFY_STOP)
554 return;
555 if (debugger_sstep(regs))
556 return;
557
558 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
559}
560
561/*
562 * After we have successfully emulated an instruction, we have to
563 * check if the instruction was being single-stepped, and if so,
564 * pretend we got a single-step exception. This was pointed out
565 * by Kumar Gala. -- paulus
566 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000567static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000568{
569 if (single_stepping(regs)) {
570 clear_single_step(regs);
571 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
572 }
573}
574
Kumar Gala5fad2932007-02-07 01:47:59 -0600575static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000576{
Kumar Gala5fad2932007-02-07 01:47:59 -0600577 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000578
579 /* Invalid operation */
580 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600581 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000582
583 /* Overflow */
584 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600585 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000586
587 /* Underflow */
588 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600589 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000590
591 /* Divide by zero */
592 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600593 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000594
595 /* Inexact result */
596 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600597 ret = FPE_FLTRES;
598
599 return ret;
600}
601
602static void parse_fpe(struct pt_regs *regs)
603{
604 int code = 0;
605
606 flush_fp_to_thread(current);
607
608 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000609
610 _exception(SIGFPE, regs, code, regs->nip);
611}
612
613/*
614 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000615 * provide the PVR to user applications using the mfspr rd, PVR.
616 * Return non-zero if we can't emulate, or -EFAULT if the associated
617 * memory access caused an access fault. Return zero on success.
618 *
619 * There are a couple of ways to do this, either "decode" the instruction
620 * or directly match lots of bits. In this case, matching lots of
621 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000622 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000623 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000624static int emulate_string_inst(struct pt_regs *regs, u32 instword)
625{
626 u8 rT = (instword >> 21) & 0x1f;
627 u8 rA = (instword >> 16) & 0x1f;
628 u8 NB_RB = (instword >> 11) & 0x1f;
629 u32 num_bytes;
630 unsigned long EA;
631 int pos = 0;
632
633 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000634 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000635 if ((rT == rA) || (rT == NB_RB))
636 return -EINVAL;
637
638 EA = (rA == 0) ? 0 : regs->gpr[rA];
639
Kumar Gala16c57b32009-02-10 20:10:44 +0000640 switch (instword & PPC_INST_STRING_MASK) {
641 case PPC_INST_LSWX:
642 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000643 EA += NB_RB;
644 num_bytes = regs->xer & 0x7f;
645 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000646 case PPC_INST_LSWI:
647 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000648 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
649 break;
650 default:
651 return -EINVAL;
652 }
653
654 while (num_bytes != 0)
655 {
656 u8 val;
657 u32 shift = 8 * (3 - (pos & 0x3));
658
Kumar Gala16c57b32009-02-10 20:10:44 +0000659 switch ((instword & PPC_INST_STRING_MASK)) {
660 case PPC_INST_LSWX:
661 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000662 if (get_user(val, (u8 __user *)EA))
663 return -EFAULT;
664 /* first time updating this reg,
665 * zero it out */
666 if (pos == 0)
667 regs->gpr[rT] = 0;
668 regs->gpr[rT] |= val << shift;
669 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000670 case PPC_INST_STSWI:
671 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000672 val = regs->gpr[rT] >> shift;
673 if (put_user(val, (u8 __user *)EA))
674 return -EFAULT;
675 break;
676 }
677 /* move EA to next address */
678 EA += 1;
679 num_bytes--;
680
681 /* manage our position within the register */
682 if (++pos == 4) {
683 pos = 0;
684 if (++rT == 32)
685 rT = 0;
686 }
687 }
688
689 return 0;
690}
691
Will Schmidtc3412dc2006-08-30 13:11:38 -0500692static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
693{
694 u32 ra,rs;
695 unsigned long tmp;
696
697 ra = (instword >> 16) & 0x1f;
698 rs = (instword >> 21) & 0x1f;
699
700 tmp = regs->gpr[rs];
701 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
702 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
703 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
704 regs->gpr[ra] = tmp;
705
706 return 0;
707}
708
Kumar Galac1469f12007-11-19 21:35:29 -0600709static int emulate_isel(struct pt_regs *regs, u32 instword)
710{
711 u8 rT = (instword >> 21) & 0x1f;
712 u8 rA = (instword >> 16) & 0x1f;
713 u8 rB = (instword >> 11) & 0x1f;
714 u8 BC = (instword >> 6) & 0x1f;
715 u8 bit;
716 unsigned long tmp;
717
718 tmp = (rA == 0) ? 0 : regs->gpr[rA];
719 bit = (regs->ccr >> (31 - BC)) & 0x1;
720
721 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
722
723 return 0;
724}
725
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000726static int emulate_instruction(struct pt_regs *regs)
727{
728 u32 instword;
729 u32 rd;
730
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000731 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000732 return -EINVAL;
733 CHECK_FULL_REGS(regs);
734
735 if (get_user(instword, (u32 __user *)(regs->nip)))
736 return -EFAULT;
737
738 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000739 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000740 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000741 rd = (instword >> 21) & 0x1f;
742 regs->gpr[rd] = mfspr(SPRN_PVR);
743 return 0;
744 }
745
746 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000747 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000748 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000749 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000750 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000751
752 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000753 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000754 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000755 unsigned long msk = 0xf0000000UL >> shift;
756
Anton Blanchardeecff812009-10-27 18:46:55 +0000757 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000758 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
759 regs->xer &= ~0xf0000000UL;
760 return 0;
761 }
762
763 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000764 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000765 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000766 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000767 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000768
Will Schmidtc3412dc2006-08-30 13:11:38 -0500769 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000770 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000771 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500772 return emulate_popcntb_inst(regs, instword);
773 }
774
Kumar Galac1469f12007-11-19 21:35:29 -0600775 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000776 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000777 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600778 return emulate_isel(regs, instword);
779 }
780
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000781 return -EINVAL;
782}
783
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800784int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000785{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800786 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000787}
788
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000789void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790{
791 unsigned int reason = get_reason(regs);
792 extern int do_mathemu(struct pt_regs *regs);
793
Kim Phillipsaa42c692006-12-08 02:43:30 -0600794 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600795 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000796
797 if (reason & REASON_FP) {
798 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000799 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000800 return;
801 }
802 if (reason & REASON_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000804 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
805 == NOTIFY_STOP)
806 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000807 if (debugger_bpt(regs))
808 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800809
810 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700811 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812 regs->nip += 4;
813 return;
814 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000815 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
816 return;
817 }
818
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100819 local_irq_enable();
820
Kumar Gala04903a32007-02-07 01:13:32 -0600821#ifdef CONFIG_MATH_EMULATION
822 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
823 * but there seems to be a hardware bug on the 405GP (RevD)
824 * that means ESR is sometimes set incorrectly - either to
825 * ESR_DST (!?) or 0. In the process of chasing this with the
826 * hardware people - not sure if it can happen on any illegal
827 * instruction or only on FP instructions, whether there is a
828 * pattern to occurences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -0600829 switch (do_mathemu(regs)) {
830 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -0600831 emulate_single_step(regs);
832 return;
Kumar Gala5fad2932007-02-07 01:47:59 -0600833 case 1: {
834 int code = 0;
835 code = __parse_fpscr(current->thread.fpscr.val);
836 _exception(SIGFPE, regs, code, regs->nip);
837 return;
838 }
839 case -EFAULT:
840 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
841 return;
Kumar Gala04903a32007-02-07 01:13:32 -0600842 }
Kumar Gala5fad2932007-02-07 01:47:59 -0600843 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -0600844#endif /* CONFIG_MATH_EMULATION */
845
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000846 /* Try to emulate it if we should. */
847 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000848 switch (emulate_instruction(regs)) {
849 case 0:
850 regs->nip += 4;
851 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000852 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000853 case -EFAULT:
854 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000855 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000856 }
857 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000858
859 if (reason & REASON_PRIVILEGED)
860 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
861 else
862 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000863}
864
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000865void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000866{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100867 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000868
Paul Mackerrase9370ae2006-06-07 16:15:39 +1000869 /* we don't implement logging of alignment exceptions */
870 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
871 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000872
873 if (fixed == 1) {
874 regs->nip += 4; /* skip over emulated instruction */
875 emulate_single_step(regs);
876 return;
877 }
878
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000879 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000880 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100881 sig = SIGSEGV;
882 code = SEGV_ACCERR;
883 } else {
884 sig = SIGBUS;
885 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000886 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100887 if (user_mode(regs))
888 _exception(sig, regs, code, regs->dar);
889 else
890 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000891}
892
893void StackOverflow(struct pt_regs *regs)
894{
895 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
896 current, regs->gpr[1]);
897 debugger(regs);
898 show_regs(regs);
899 panic("kernel stack overflow");
900}
901
902void nonrecoverable_exception(struct pt_regs *regs)
903{
904 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
905 regs->nip, regs->msr);
906 debugger(regs);
907 die("nonrecoverable exception", regs, SIGKILL);
908}
909
910void trace_syscall(struct pt_regs *regs)
911{
912 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700913 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000914 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
915}
916
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000917void kernel_fp_unavailable_exception(struct pt_regs *regs)
918{
919 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
920 "%lx at %lx\n", regs->trap, regs->nip);
921 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
922}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000923
924void altivec_unavailable_exception(struct pt_regs *regs)
925{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000926 if (user_mode(regs)) {
927 /* A user program has executed an altivec instruction,
928 but this kernel doesn't support altivec. */
929 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
930 return;
931 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +1000932
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000933 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
934 "%lx at %lx\n", regs->trap, regs->nip);
935 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000936}
937
Michael Neulingce48b212008-06-25 14:07:18 +1000938void vsx_unavailable_exception(struct pt_regs *regs)
939{
940 if (user_mode(regs)) {
941 /* A user program has executed an vsx instruction,
942 but this kernel doesn't support vsx. */
943 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
944 return;
945 }
946
947 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
948 "%lx at %lx\n", regs->trap, regs->nip);
949 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
950}
951
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000952void performance_monitor_exception(struct pt_regs *regs)
953{
954 perf_irq(regs);
955}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000956
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000957#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000958void SoftwareEmulation(struct pt_regs *regs)
959{
960 extern int do_mathemu(struct pt_regs *);
961 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -0500962#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000963 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -0500964#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000965
966 CHECK_FULL_REGS(regs);
967
968 if (!user_mode(regs)) {
969 debugger(regs);
970 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
971 }
972
973#ifdef CONFIG_MATH_EMULATION
974 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000975 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +0000976 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -0600977
978 switch (errcode) {
979 case 0:
980 emulate_single_step(regs);
981 return;
982 case 1: {
983 int code = 0;
984 code = __parse_fpscr(current->thread.fpscr.val);
985 _exception(SIGFPE, regs, code, regs->nip);
986 return;
987 }
988 case -EFAULT:
989 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
990 return;
991 default:
992 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
993 return;
994 }
995
Scott Wood5dd57a12007-09-18 15:29:35 -0500996#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000997 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000998 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +0000999 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001000
Kumar Gala5fad2932007-02-07 01:47:59 -06001001 switch (errcode) {
1002 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001003 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001004 return;
1005 case 1:
1006 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1007 return;
1008 case -EFAULT:
1009 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1010 return;
1011 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001012#else
1013 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001014#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001015}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001016#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001017
1018#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1019
Kumar Galaf8279622008-06-26 02:01:37 -05001020void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001021{
Roland McGrathec097c82009-05-28 21:26:38 +00001022 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1023 * on server, it stops on the target of the branch. In order to simulate
1024 * the server behaviour, we thus restart right away with a single step
1025 * instead of stopping here when hitting a BT
1026 */
1027 if (debug_status & DBSR_BT) {
1028 regs->msr &= ~MSR_DE;
1029
1030 /* Disable BT */
1031 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1032 /* Clear the BT event */
1033 mtspr(SPRN_DBSR, DBSR_BT);
1034
1035 /* Do the single step trick only when coming from userspace */
1036 if (user_mode(regs)) {
1037 current->thread.dbcr0 &= ~DBCR0_BT;
1038 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1039 regs->msr |= MSR_DE;
1040 return;
1041 }
1042
1043 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1044 5, SIGTRAP) == NOTIFY_STOP) {
1045 return;
1046 }
1047 if (debugger_sstep(regs))
1048 return;
1049 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001050 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001051
1052 /* Disable instruction completion */
1053 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1054 /* Clear the instruction completion event */
1055 mtspr(SPRN_DBSR, DBSR_IC);
1056
1057 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1058 5, SIGTRAP) == NOTIFY_STOP) {
1059 return;
1060 }
1061
1062 if (debugger_sstep(regs))
1063 return;
1064
Roland McGrathec097c82009-05-28 21:26:38 +00001065 if (user_mode(regs))
1066 current->thread.dbcr0 &= ~(DBCR0_IC);
Kumar Galaf8279622008-06-26 02:01:37 -05001067
1068 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001069 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1070 regs->msr &= ~MSR_DE;
1071
1072 if (user_mode(regs)) {
1073 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1074 DBCR0_IDM);
1075 } else {
1076 /* Disable DAC interupts */
1077 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1078 DBSR_DAC1W | DBCR0_IDM));
1079
1080 /* Clear the DAC event */
1081 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1082 }
1083 /* Setup and send the trap to the handler */
1084 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001085 }
1086}
1087#endif /* CONFIG_4xx || CONFIG_BOOKE */
1088
1089#if !defined(CONFIG_TAU_INT)
1090void TAUException(struct pt_regs *regs)
1091{
1092 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1093 regs->nip, regs->msr, regs->trap, print_tainted());
1094}
1095#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001096
1097#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001098void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001099{
1100 int err;
1101
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001102 if (!user_mode(regs)) {
1103 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1104 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001105 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001106 }
1107
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001108 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001109
Anton Blanchardeecff812009-10-27 18:46:55 +00001110 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001111 err = emulate_altivec(regs);
1112 if (err == 0) {
1113 regs->nip += 4; /* skip emulated instruction */
1114 emulate_single_step(regs);
1115 return;
1116 }
1117
1118 if (err == -EFAULT) {
1119 /* got an error reading the instruction */
1120 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1121 } else {
1122 /* didn't recognize the instruction */
1123 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1124 if (printk_ratelimit())
1125 printk(KERN_ERR "Unrecognized altivec instruction "
1126 "in %s at %lx\n", current->comm, regs->nip);
1127 current->thread.vscr.u[3] |= 0x10000;
1128 }
1129}
1130#endif /* CONFIG_ALTIVEC */
1131
Michael Neulingce48b212008-06-25 14:07:18 +10001132#ifdef CONFIG_VSX
1133void vsx_assist_exception(struct pt_regs *regs)
1134{
1135 if (!user_mode(regs)) {
1136 printk(KERN_EMERG "VSX assist exception in kernel mode"
1137 " at %lx\n", regs->nip);
1138 die("Kernel VSX assist exception", regs, SIGILL);
1139 }
1140
1141 flush_vsx_to_thread(current);
1142 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1143 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1144}
1145#endif /* CONFIG_VSX */
1146
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001147#ifdef CONFIG_FSL_BOOKE
Kumar Gala620165f2009-02-12 13:54:53 +00001148
1149void doorbell_exception(struct pt_regs *regs)
1150{
1151#ifdef CONFIG_SMP
1152 int cpu = smp_processor_id();
1153 int msg;
1154
1155 if (num_online_cpus() < 2)
1156 return;
1157
1158 for (msg = 0; msg < 4; msg++)
1159 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1160 smp_message_recv(msg);
1161#else
1162 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1163#endif
1164}
1165
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001166void CacheLockingException(struct pt_regs *regs, unsigned long address,
1167 unsigned long error_code)
1168{
1169 /* We treat cache locking instructions from the user
1170 * as priv ops, in the future we could try to do
1171 * something smarter
1172 */
1173 if (error_code & (ESR_DLK|ESR_ILK))
1174 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1175 return;
1176}
1177#endif /* CONFIG_FSL_BOOKE */
1178
1179#ifdef CONFIG_SPE
1180void SPEFloatingPointException(struct pt_regs *regs)
1181{
Liu Yu6a800f32008-10-28 11:50:21 +08001182 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001183 unsigned long spefscr;
1184 int fpexc_mode;
1185 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001186 int err;
1187
1188 preempt_disable();
1189 if (regs->msr & MSR_SPE)
1190 giveup_spe(current);
1191 preempt_enable();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001192
1193 spefscr = current->thread.spefscr;
1194 fpexc_mode = current->thread.fpexc_mode;
1195
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001196 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1197 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001198 }
1199 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1200 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001201 }
1202 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1203 code = FPE_FLTDIV;
1204 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1205 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001206 }
1207 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1208 code = FPE_FLTRES;
1209
Liu Yu6a800f32008-10-28 11:50:21 +08001210 err = do_spe_mathemu(regs);
1211 if (err == 0) {
1212 regs->nip += 4; /* skip emulated instruction */
1213 emulate_single_step(regs);
1214 return;
1215 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001216
Liu Yu6a800f32008-10-28 11:50:21 +08001217 if (err == -EFAULT) {
1218 /* got an error reading the instruction */
1219 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1220 } else if (err == -EINVAL) {
1221 /* didn't recognize the instruction */
1222 printk(KERN_ERR "unrecognized spe instruction "
1223 "in %s at %lx\n", current->comm, regs->nip);
1224 } else {
1225 _exception(SIGFPE, regs, code, regs->nip);
1226 }
1227
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001228 return;
1229}
Liu Yu6a800f32008-10-28 11:50:21 +08001230
1231void SPEFloatingPointRoundException(struct pt_regs *regs)
1232{
1233 extern int speround_handler(struct pt_regs *regs);
1234 int err;
1235
1236 preempt_disable();
1237 if (regs->msr & MSR_SPE)
1238 giveup_spe(current);
1239 preempt_enable();
1240
1241 regs->nip -= 4;
1242 err = speround_handler(regs);
1243 if (err == 0) {
1244 regs->nip += 4; /* skip emulated instruction */
1245 emulate_single_step(regs);
1246 return;
1247 }
1248
1249 if (err == -EFAULT) {
1250 /* got an error reading the instruction */
1251 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1252 } else if (err == -EINVAL) {
1253 /* didn't recognize the instruction */
1254 printk(KERN_ERR "unrecognized spe instruction "
1255 "in %s at %lx\n", current->comm, regs->nip);
1256 } else {
1257 _exception(SIGFPE, regs, 0, regs->nip);
1258 return;
1259 }
1260}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001261#endif
1262
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001263/*
1264 * We enter here if we get an unrecoverable exception, that is, one
1265 * that happened at a point where the RI (recoverable interrupt) bit
1266 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1267 * we therefore lost state by taking this exception.
1268 */
1269void unrecoverable_exception(struct pt_regs *regs)
1270{
1271 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1272 regs->trap, regs->nip);
1273 die("Unrecoverable exception", regs, SIGABRT);
1274}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001275
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001276#ifdef CONFIG_BOOKE_WDT
1277/*
1278 * Default handler for a Watchdog exception,
1279 * spins until a reboot occurs
1280 */
1281void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1282{
1283 /* Generic WatchdogHandler, implement your own */
1284 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1285 return;
1286}
1287
1288void WatchdogException(struct pt_regs *regs)
1289{
1290 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1291 WatchdogHandler(regs);
1292}
1293#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001294
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001295/*
1296 * We enter here if we discover during exception entry that we are
1297 * running in supervisor mode with a userspace value in the stack pointer.
1298 */
1299void kernel_bad_stack(struct pt_regs *regs)
1300{
1301 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1302 regs->gpr[1], regs->nip);
1303 die("Bad kernel stack pointer", regs, SIGABRT);
1304}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001305
1306void __init trap_init(void)
1307{
1308}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001309
1310
1311#ifdef CONFIG_PPC_EMULATED_STATS
1312
1313#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1314
1315struct ppc_emulated ppc_emulated = {
1316#ifdef CONFIG_ALTIVEC
1317 WARN_EMULATED_SETUP(altivec),
1318#endif
1319 WARN_EMULATED_SETUP(dcba),
1320 WARN_EMULATED_SETUP(dcbz),
1321 WARN_EMULATED_SETUP(fp_pair),
1322 WARN_EMULATED_SETUP(isel),
1323 WARN_EMULATED_SETUP(mcrxr),
1324 WARN_EMULATED_SETUP(mfpvr),
1325 WARN_EMULATED_SETUP(multiple),
1326 WARN_EMULATED_SETUP(popcntb),
1327 WARN_EMULATED_SETUP(spe),
1328 WARN_EMULATED_SETUP(string),
1329 WARN_EMULATED_SETUP(unaligned),
1330#ifdef CONFIG_MATH_EMULATION
1331 WARN_EMULATED_SETUP(math),
1332#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1333 WARN_EMULATED_SETUP(8xx),
1334#endif
1335#ifdef CONFIG_VSX
1336 WARN_EMULATED_SETUP(vsx),
1337#endif
1338};
1339
1340u32 ppc_warn_emulated;
1341
1342void ppc_warn_emulated_print(const char *type)
1343{
1344 if (printk_ratelimit())
1345 pr_warning("%s used emulated %s instruction\n", current->comm,
1346 type);
1347}
1348
1349static int __init ppc_warn_emulated_init(void)
1350{
1351 struct dentry *dir, *d;
1352 unsigned int i;
1353 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1354
1355 if (!powerpc_debugfs_root)
1356 return -ENODEV;
1357
1358 dir = debugfs_create_dir("emulated_instructions",
1359 powerpc_debugfs_root);
1360 if (!dir)
1361 return -ENOMEM;
1362
1363 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1364 &ppc_warn_emulated);
1365 if (!d)
1366 goto fail;
1367
1368 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1369 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1370 (u32 *)&entries[i].val.counter);
1371 if (!d)
1372 goto fail;
1373 }
1374
1375 return 0;
1376
1377fail:
1378 debugfs_remove_recursive(dir);
1379 return -ENOMEM;
1380}
1381
1382device_initcall(ppc_warn_emulated_init);
1383
1384#endif /* CONFIG_PPC_EMULATED_STATS */