blob: 9d1f9354d6cafcf12fd7719d599615e31e55b2d9 [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);
201
202 /*
203 * Init gets no signals that it doesn't have a handler for.
204 * That's all very well, but if it has caused a synchronous
205 * exception and we ignore the resulting signal, it will just
206 * generate the same exception over and over again and we get
207 * nowhere. Better to kill it and let the kernel panic.
208 */
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700209 if (is_global_init(current)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000210 __sighandler_t handler;
211
212 spin_lock_irq(&current->sighand->siglock);
213 handler = current->sighand->action[signr-1].sa.sa_handler;
214 spin_unlock_irq(&current->sighand->siglock);
215 if (handler == SIG_DFL) {
216 /* init has generated a synchronous exception
217 and it doesn't have a handler for the signal */
218 printk(KERN_CRIT "init has generated signal %d "
219 "but has no handler for it\n", signr);
220 do_exit(signr);
221 }
222 }
223}
224
225#ifdef CONFIG_PPC64
226void system_reset_exception(struct pt_regs *regs)
227{
228 /* See if any machine dependent calls */
Arnd Bergmannc902be72006-01-04 19:55:53 +0000229 if (ppc_md.system_reset_exception) {
230 if (ppc_md.system_reset_exception(regs))
231 return;
232 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000233
David Wilderc0ce7d02006-06-23 15:29:34 -0700234#ifdef CONFIG_KEXEC
235 cpu_set(smp_processor_id(), cpus_in_sr);
236#endif
237
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000238 die("System Reset", regs, SIGABRT);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000239
David Wildereac83922006-06-29 15:17:30 -0700240 /*
241 * Some CPUs when released from the debugger will execute this path.
242 * These CPUs entered the debugger via a soft-reset. If the CPU was
243 * hung before entering the debugger it will return to the hung
244 * state when exiting this function. This causes a problem in
245 * kdump since the hung CPU(s) will not respond to the IPI sent
246 * from kdump. To prevent the problem we call crash_kexec_secondary()
247 * here. If a kdump had not been initiated or we exit the debugger
248 * with the "exit and recover" command (x) crash_kexec_secondary()
249 * will return after 5ms and the CPU returns to its previous state.
250 */
251 crash_kexec_secondary(regs);
252
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000253 /* Must die if the interrupt is not recoverable */
254 if (!(regs->msr & MSR_RI))
255 panic("Unrecoverable System Reset");
256
257 /* What should we do here? We could issue a shutdown or hard reset. */
258}
259#endif
260
261/*
262 * I/O accesses can cause machine checks on powermacs.
263 * Check if the NIP corresponds to the address of a sync
264 * instruction for which there is an entry in the exception
265 * table.
266 * Note that the 601 only takes a machine check on TEA
267 * (transfer error ack) signal assertion, and does not
268 * set any of the top 16 bits of SRR1.
269 * -- paulus.
270 */
271static inline int check_io_access(struct pt_regs *regs)
272{
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100273#ifdef CONFIG_PPC32
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000274 unsigned long msr = regs->msr;
275 const struct exception_table_entry *entry;
276 unsigned int *nip = (unsigned int *)regs->nip;
277
278 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
279 && (entry = search_exception_tables(regs->nip)) != NULL) {
280 /*
281 * Check that it's a sync instruction, or somewhere
282 * in the twi; isync; nop sequence that inb/inw/inl uses.
283 * As the address is in the exception table
284 * we should be able to read the instr there.
285 * For the debug message, we look at the preceding
286 * load or store.
287 */
288 if (*nip == 0x60000000) /* nop */
289 nip -= 2;
290 else if (*nip == 0x4c00012c) /* isync */
291 --nip;
292 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
293 /* sync or twi */
294 unsigned int rb;
295
296 --nip;
297 rb = (*nip >> 11) & 0x1f;
298 printk(KERN_DEBUG "%s bad port %lx at %p\n",
299 (*nip & 0x100)? "OUT to": "IN from",
300 regs->gpr[rb] - _IO_BASE, nip);
301 regs->msr |= MSR_RI;
302 regs->nip = entry->fixup;
303 return 1;
304 }
305 }
Benjamin Herrenschmidt68a64352006-11-13 09:27:39 +1100306#endif /* CONFIG_PPC32 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000307 return 0;
308}
309
310#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
311/* On 4xx, the reason for the machine check or program exception
312 is in the ESR. */
313#define get_reason(regs) ((regs)->dsisr)
314#ifndef CONFIG_FSL_BOOKE
315#define get_mc_reason(regs) ((regs)->dsisr)
316#else
Becky Bruce86d7a9a2007-08-02 15:37:15 -0500317#define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000318#endif
319#define REASON_FP ESR_FP
320#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
321#define REASON_PRIVILEGED ESR_PPR
322#define REASON_TRAP ESR_PTR
323
324/* single-step stuff */
325#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
326#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
327
328#else
329/* On non-4xx, the reason for the machine check or program
330 exception is in the MSR. */
331#define get_reason(regs) ((regs)->msr)
332#define get_mc_reason(regs) ((regs)->msr)
333#define REASON_FP 0x100000
334#define REASON_ILLEGAL 0x80000
335#define REASON_PRIVILEGED 0x40000
336#define REASON_TRAP 0x20000
337
338#define single_stepping(regs) ((regs)->msr & MSR_SE)
339#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
340#endif
341
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100342#if defined(CONFIG_4xx)
343int machine_check_4xx(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000344{
Kumar Gala1a6a4ff2006-03-30 21:11:15 -0600345 unsigned long reason = get_mc_reason(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000346
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000347 if (reason & ESR_IMCP) {
348 printk("Instruction");
349 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
350 } else
351 printk("Data");
352 printk(" machine check in kernel mode.\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100353
354 return 0;
355}
356
357int machine_check_440A(struct pt_regs *regs)
358{
359 unsigned long reason = get_mc_reason(regs);
360
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000361 printk("Machine check in kernel mode.\n");
362 if (reason & ESR_IMCP){
363 printk("Instruction Synchronous Machine Check exception\n");
364 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
365 }
366 else {
367 u32 mcsr = mfspr(SPRN_MCSR);
368 if (mcsr & MCSR_IB)
369 printk("Instruction Read PLB Error\n");
370 if (mcsr & MCSR_DRB)
371 printk("Data Read PLB Error\n");
372 if (mcsr & MCSR_DWB)
373 printk("Data Write PLB Error\n");
374 if (mcsr & MCSR_TLBP)
375 printk("TLB Parity Error\n");
376 if (mcsr & MCSR_ICP){
377 flush_instruction_cache();
378 printk("I-Cache Parity Error\n");
379 }
380 if (mcsr & MCSR_DCSP)
381 printk("D-Cache Search Parity Error\n");
382 if (mcsr & MCSR_DCFP)
383 printk("D-Cache Flush Parity Error\n");
384 if (mcsr & MCSR_IMPE)
385 printk("Machine Check exception is imprecise\n");
386
387 /* Clear MCSR */
388 mtspr(SPRN_MCSR, mcsr);
389 }
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100390 return 0;
391}
392#elif defined(CONFIG_E500)
393int machine_check_e500(struct pt_regs *regs)
394{
395 unsigned long reason = get_mc_reason(regs);
396
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000397 printk("Machine check in kernel mode.\n");
398 printk("Caused by (from MCSR=%lx): ", reason);
399
400 if (reason & MCSR_MCP)
401 printk("Machine Check Signal\n");
402 if (reason & MCSR_ICPERR)
403 printk("Instruction Cache Parity Error\n");
404 if (reason & MCSR_DCP_PERR)
405 printk("Data Cache Push Parity Error\n");
406 if (reason & MCSR_DCPERR)
407 printk("Data Cache Parity Error\n");
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000408 if (reason & MCSR_BUS_IAERR)
409 printk("Bus - Instruction Address Error\n");
410 if (reason & MCSR_BUS_RAERR)
411 printk("Bus - Read Address Error\n");
412 if (reason & MCSR_BUS_WAERR)
413 printk("Bus - Write Address Error\n");
414 if (reason & MCSR_BUS_IBERR)
415 printk("Bus - Instruction Data Error\n");
416 if (reason & MCSR_BUS_RBERR)
417 printk("Bus - Read Data Bus Error\n");
418 if (reason & MCSR_BUS_WBERR)
419 printk("Bus - Read Data Bus Error\n");
420 if (reason & MCSR_BUS_IPERR)
421 printk("Bus - Instruction Parity Error\n");
422 if (reason & MCSR_BUS_RPERR)
423 printk("Bus - Read Parity Error\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100424
425 return 0;
426}
427#elif defined(CONFIG_E200)
428int machine_check_e200(struct pt_regs *regs)
429{
430 unsigned long reason = get_mc_reason(regs);
431
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000432 printk("Machine check in kernel mode.\n");
433 printk("Caused by (from MCSR=%lx): ", reason);
434
435 if (reason & MCSR_MCP)
436 printk("Machine Check Signal\n");
437 if (reason & MCSR_CP_PERR)
438 printk("Cache Push Parity Error\n");
439 if (reason & MCSR_CPERR)
440 printk("Cache Parity Error\n");
441 if (reason & MCSR_EXCP_ERR)
442 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
443 if (reason & MCSR_BUS_IRERR)
444 printk("Bus - Read Bus Error on instruction fetch\n");
445 if (reason & MCSR_BUS_DRERR)
446 printk("Bus - Read Bus Error on data load\n");
447 if (reason & MCSR_BUS_WRERR)
448 printk("Bus - Write Bus Error on buffered store or cache line push\n");
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100449
450 return 0;
451}
452#else
453int machine_check_generic(struct pt_regs *regs)
454{
455 unsigned long reason = get_mc_reason(regs);
456
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000457 printk("Machine check in kernel mode.\n");
458 printk("Caused by (from SRR1=%lx): ", reason);
459 switch (reason & 0x601F0000) {
460 case 0x80000:
461 printk("Machine check signal\n");
462 break;
463 case 0: /* for 601 */
464 case 0x40000:
465 case 0x140000: /* 7450 MSS error and TEA */
466 printk("Transfer error ack signal\n");
467 break;
468 case 0x20000:
469 printk("Data parity error signal\n");
470 break;
471 case 0x10000:
472 printk("Address parity error signal\n");
473 break;
474 case 0x20000000:
475 printk("L1 Data Cache error\n");
476 break;
477 case 0x40000000:
478 printk("L1 Instruction Cache error\n");
479 break;
480 case 0x00100000:
481 printk("L2 data cache parity error\n");
482 break;
483 default:
484 printk("Unknown values in msr\n");
485 }
Olof Johansson75918a42007-09-21 05:11:20 +1000486 return 0;
487}
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100488#endif /* everything else */
Olof Johansson75918a42007-09-21 05:11:20 +1000489
490void machine_check_exception(struct pt_regs *regs)
491{
492 int recover = 0;
493
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100494 /* See if any machine dependent calls. In theory, we would want
495 * to call the CPU first, and call the ppc_md. one if the CPU
496 * one returns a positive number. However there is existing code
497 * that assumes the board gets a first chance, so let's keep it
498 * that way for now and fix things later. --BenH.
499 */
Olof Johansson75918a42007-09-21 05:11:20 +1000500 if (ppc_md.machine_check_exception)
501 recover = ppc_md.machine_check_exception(regs);
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100502 else if (cur_cpu_spec->machine_check)
503 recover = cur_cpu_spec->machine_check(regs);
Olof Johansson75918a42007-09-21 05:11:20 +1000504
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100505 if (recover > 0)
Olof Johansson75918a42007-09-21 05:11:20 +1000506 return;
507
508 if (user_mode(regs)) {
509 regs->msr |= MSR_RI;
510 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
511 return;
512 }
513
514#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
Benjamin Herrenschmidt47c0bd12007-12-21 15:39:21 +1100515 /* the qspan pci read routines can cause machine checks -- Cort
516 *
517 * yuck !!! that totally needs to go away ! There are better ways
518 * to deal with that than having a wart in the mcheck handler.
519 * -- BenH
520 */
Olof Johansson75918a42007-09-21 05:11:20 +1000521 bad_page_fault(regs, regs->dar, SIGBUS);
522 return;
523#endif
524
525 if (debugger_fault_handler(regs)) {
526 regs->msr |= MSR_RI;
527 return;
528 }
529
530 if (check_io_access(regs))
531 return;
532
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000533 if (debugger_fault_handler(regs))
534 return;
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000535 die("Machine check", regs, SIGBUS);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000536
537 /* Must die if the interrupt is not recoverable */
538 if (!(regs->msr & MSR_RI))
539 panic("Unrecoverable Machine check");
540}
541
542void SMIException(struct pt_regs *regs)
543{
544 die("System Management Interrupt", regs, SIGABRT);
545}
546
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000547void unknown_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000548{
549 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
550 regs->nip, regs->msr, regs->trap);
551
552 _exception(SIGTRAP, regs, 0, 0);
553}
554
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000555void instruction_breakpoint_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000556{
557 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
558 5, SIGTRAP) == NOTIFY_STOP)
559 return;
560 if (debugger_iabr_match(regs))
561 return;
562 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
563}
564
565void RunModeException(struct pt_regs *regs)
566{
567 _exception(SIGTRAP, regs, 0, 0);
568}
569
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000570void __kprobes single_step_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000571{
572 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
573
574 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
575 5, SIGTRAP) == NOTIFY_STOP)
576 return;
577 if (debugger_sstep(regs))
578 return;
579
580 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
581}
582
583/*
584 * After we have successfully emulated an instruction, we have to
585 * check if the instruction was being single-stepped, and if so,
586 * pretend we got a single-step exception. This was pointed out
587 * by Kumar Gala. -- paulus
588 */
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000589static void emulate_single_step(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000590{
591 if (single_stepping(regs)) {
592 clear_single_step(regs);
593 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
594 }
595}
596
Kumar Gala5fad2932007-02-07 01:47:59 -0600597static inline int __parse_fpscr(unsigned long fpscr)
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000598{
Kumar Gala5fad2932007-02-07 01:47:59 -0600599 int ret = 0;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000600
601 /* Invalid operation */
602 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600603 ret = FPE_FLTINV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000604
605 /* Overflow */
606 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600607 ret = FPE_FLTOVF;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000608
609 /* Underflow */
610 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600611 ret = FPE_FLTUND;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000612
613 /* Divide by zero */
614 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600615 ret = FPE_FLTDIV;
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000616
617 /* Inexact result */
618 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
Kumar Gala5fad2932007-02-07 01:47:59 -0600619 ret = FPE_FLTRES;
620
621 return ret;
622}
623
624static void parse_fpe(struct pt_regs *regs)
625{
626 int code = 0;
627
628 flush_fp_to_thread(current);
629
630 code = __parse_fpscr(current->thread.fpscr.val);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000631
632 _exception(SIGFPE, regs, code, regs->nip);
633}
634
635/*
636 * Illegal instruction emulation support. Originally written to
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000637 * provide the PVR to user applications using the mfspr rd, PVR.
638 * Return non-zero if we can't emulate, or -EFAULT if the associated
639 * memory access caused an access fault. Return zero on success.
640 *
641 * There are a couple of ways to do this, either "decode" the instruction
642 * or directly match lots of bits. In this case, matching lots of
643 * bits is faster and easier.
Paul Mackerras86417782005-10-10 22:37:57 +1000644 *
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000645 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000646static int emulate_string_inst(struct pt_regs *regs, u32 instword)
647{
648 u8 rT = (instword >> 21) & 0x1f;
649 u8 rA = (instword >> 16) & 0x1f;
650 u8 NB_RB = (instword >> 11) & 0x1f;
651 u32 num_bytes;
652 unsigned long EA;
653 int pos = 0;
654
655 /* Early out if we are an invalid form of lswx */
Kumar Gala16c57b32009-02-10 20:10:44 +0000656 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000657 if ((rT == rA) || (rT == NB_RB))
658 return -EINVAL;
659
660 EA = (rA == 0) ? 0 : regs->gpr[rA];
661
Kumar Gala16c57b32009-02-10 20:10:44 +0000662 switch (instword & PPC_INST_STRING_MASK) {
663 case PPC_INST_LSWX:
664 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000665 EA += NB_RB;
666 num_bytes = regs->xer & 0x7f;
667 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000668 case PPC_INST_LSWI:
669 case PPC_INST_STSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000670 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
671 break;
672 default:
673 return -EINVAL;
674 }
675
676 while (num_bytes != 0)
677 {
678 u8 val;
679 u32 shift = 8 * (3 - (pos & 0x3));
680
Kumar Gala16c57b32009-02-10 20:10:44 +0000681 switch ((instword & PPC_INST_STRING_MASK)) {
682 case PPC_INST_LSWX:
683 case PPC_INST_LSWI:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000684 if (get_user(val, (u8 __user *)EA))
685 return -EFAULT;
686 /* first time updating this reg,
687 * zero it out */
688 if (pos == 0)
689 regs->gpr[rT] = 0;
690 regs->gpr[rT] |= val << shift;
691 break;
Kumar Gala16c57b32009-02-10 20:10:44 +0000692 case PPC_INST_STSWI:
693 case PPC_INST_STSWX:
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000694 val = regs->gpr[rT] >> shift;
695 if (put_user(val, (u8 __user *)EA))
696 return -EFAULT;
697 break;
698 }
699 /* move EA to next address */
700 EA += 1;
701 num_bytes--;
702
703 /* manage our position within the register */
704 if (++pos == 4) {
705 pos = 0;
706 if (++rT == 32)
707 rT = 0;
708 }
709 }
710
711 return 0;
712}
713
Will Schmidtc3412dc2006-08-30 13:11:38 -0500714static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
715{
716 u32 ra,rs;
717 unsigned long tmp;
718
719 ra = (instword >> 16) & 0x1f;
720 rs = (instword >> 21) & 0x1f;
721
722 tmp = regs->gpr[rs];
723 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
724 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
725 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
726 regs->gpr[ra] = tmp;
727
728 return 0;
729}
730
Kumar Galac1469f12007-11-19 21:35:29 -0600731static int emulate_isel(struct pt_regs *regs, u32 instword)
732{
733 u8 rT = (instword >> 21) & 0x1f;
734 u8 rA = (instword >> 16) & 0x1f;
735 u8 rB = (instword >> 11) & 0x1f;
736 u8 BC = (instword >> 6) & 0x1f;
737 u8 bit;
738 unsigned long tmp;
739
740 tmp = (rA == 0) ? 0 : regs->gpr[rA];
741 bit = (regs->ccr >> (31 - BC)) & 0x1;
742
743 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
744
745 return 0;
746}
747
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000748static int emulate_instruction(struct pt_regs *regs)
749{
750 u32 instword;
751 u32 rd;
752
Paul Mackerrasfab5db92006-06-07 16:14:40 +1000753 if (!user_mode(regs) || (regs->msr & MSR_LE))
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000754 return -EINVAL;
755 CHECK_FULL_REGS(regs);
756
757 if (get_user(instword, (u32 __user *)(regs->nip)))
758 return -EFAULT;
759
760 /* Emulate the mfspr rD, PVR. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000761 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000762 PPC_WARN_EMULATED(mfpvr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000763 rd = (instword >> 21) & 0x1f;
764 regs->gpr[rd] = mfspr(SPRN_PVR);
765 return 0;
766 }
767
768 /* Emulating the dcba insn is just a no-op. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000769 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000770 PPC_WARN_EMULATED(dcba, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000771 return 0;
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000772 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000773
774 /* Emulate the mcrxr insn. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000775 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
Paul Mackerras86417782005-10-10 22:37:57 +1000776 int shift = (instword >> 21) & 0x1c;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000777 unsigned long msk = 0xf0000000UL >> shift;
778
Anton Blanchardeecff812009-10-27 18:46:55 +0000779 PPC_WARN_EMULATED(mcrxr, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000780 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
781 regs->xer &= ~0xf0000000UL;
782 return 0;
783 }
784
785 /* Emulate load/store string insn. */
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000786 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000787 PPC_WARN_EMULATED(string, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000788 return emulate_string_inst(regs, instword);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000789 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000790
Will Schmidtc3412dc2006-08-30 13:11:38 -0500791 /* Emulate the popcntb (Population Count Bytes) instruction. */
Kumar Gala16c57b32009-02-10 20:10:44 +0000792 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000793 PPC_WARN_EMULATED(popcntb, regs);
Will Schmidtc3412dc2006-08-30 13:11:38 -0500794 return emulate_popcntb_inst(regs, instword);
795 }
796
Kumar Galac1469f12007-11-19 21:35:29 -0600797 /* Emulate isel (Integer Select) instruction */
Kumar Gala16c57b32009-02-10 20:10:44 +0000798 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
Anton Blanchardeecff812009-10-27 18:46:55 +0000799 PPC_WARN_EMULATED(isel, regs);
Kumar Galac1469f12007-11-19 21:35:29 -0600800 return emulate_isel(regs, instword);
801 }
802
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000803 return -EINVAL;
804}
805
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800806int is_valid_bugaddr(unsigned long addr)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000807{
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800808 return is_kernel_addr(addr);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000809}
810
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000811void __kprobes program_check_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000812{
813 unsigned int reason = get_reason(regs);
814 extern int do_mathemu(struct pt_regs *regs);
815
Kim Phillipsaa42c692006-12-08 02:43:30 -0600816 /* We can now get here via a FP Unavailable exception if the core
Kumar Gala04903a32007-02-07 01:13:32 -0600817 * has no FPU, in that case the reason flags will be 0 */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000818
819 if (reason & REASON_FP) {
820 /* IEEE FP exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000821 parse_fpe(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000822 return;
823 }
824 if (reason & REASON_TRAP) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000825 /* trap exception */
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000826 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
827 == NOTIFY_STOP)
828 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000829 if (debugger_bpt(regs))
830 return;
Jeremy Fitzhardinge73c9cea2006-12-08 03:30:41 -0800831
832 if (!(regs->msr & MSR_PR) && /* not user-mode */
Heiko Carstens608e2612007-07-15 23:41:39 -0700833 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000834 regs->nip += 4;
835 return;
836 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000837 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
838 return;
839 }
840
Paul Mackerrascd8a5672006-03-03 17:11:40 +1100841 local_irq_enable();
842
Kumar Gala04903a32007-02-07 01:13:32 -0600843#ifdef CONFIG_MATH_EMULATION
844 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
845 * but there seems to be a hardware bug on the 405GP (RevD)
846 * that means ESR is sometimes set incorrectly - either to
847 * ESR_DST (!?) or 0. In the process of chasing this with the
848 * hardware people - not sure if it can happen on any illegal
849 * instruction or only on FP instructions, whether there is a
850 * pattern to occurences etc. -dgibson 31/Mar/2003 */
Kumar Gala5fad2932007-02-07 01:47:59 -0600851 switch (do_mathemu(regs)) {
852 case 0:
Kumar Gala04903a32007-02-07 01:13:32 -0600853 emulate_single_step(regs);
854 return;
Kumar Gala5fad2932007-02-07 01:47:59 -0600855 case 1: {
856 int code = 0;
857 code = __parse_fpscr(current->thread.fpscr.val);
858 _exception(SIGFPE, regs, code, regs->nip);
859 return;
860 }
861 case -EFAULT:
862 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
863 return;
Kumar Gala04903a32007-02-07 01:13:32 -0600864 }
Kumar Gala5fad2932007-02-07 01:47:59 -0600865 /* fall through on any other errors */
Kumar Gala04903a32007-02-07 01:13:32 -0600866#endif /* CONFIG_MATH_EMULATION */
867
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000868 /* Try to emulate it if we should. */
869 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000870 switch (emulate_instruction(regs)) {
871 case 0:
872 regs->nip += 4;
873 emulate_single_step(regs);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000874 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000875 case -EFAULT:
876 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000877 return;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000878 }
879 }
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000880
881 if (reason & REASON_PRIVILEGED)
882 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
883 else
884 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000885}
886
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000887void alignment_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000888{
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100889 int sig, code, fixed = 0;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000890
Paul Mackerrase9370ae2006-06-07 16:15:39 +1000891 /* we don't implement logging of alignment exceptions */
892 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
893 fixed = fix_alignment(regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000894
895 if (fixed == 1) {
896 regs->nip += 4; /* skip over emulated instruction */
897 emulate_single_step(regs);
898 return;
899 }
900
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000901 /* Operand address was bad */
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000902 if (fixed == -EFAULT) {
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100903 sig = SIGSEGV;
904 code = SEGV_ACCERR;
905 } else {
906 sig = SIGBUS;
907 code = BUS_ADRALN;
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000908 }
Benjamin Herrenschmidt4393c4f2006-11-01 15:11:39 +1100909 if (user_mode(regs))
910 _exception(sig, regs, code, regs->dar);
911 else
912 bad_page_fault(regs, regs->dar, sig);
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000913}
914
915void StackOverflow(struct pt_regs *regs)
916{
917 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
918 current, regs->gpr[1]);
919 debugger(regs);
920 show_regs(regs);
921 panic("kernel stack overflow");
922}
923
924void nonrecoverable_exception(struct pt_regs *regs)
925{
926 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
927 regs->nip, regs->msr);
928 debugger(regs);
929 die("nonrecoverable exception", regs, SIGKILL);
930}
931
932void trace_syscall(struct pt_regs *regs)
933{
934 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700935 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000936 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
937}
938
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000939void kernel_fp_unavailable_exception(struct pt_regs *regs)
940{
941 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
942 "%lx at %lx\n", regs->trap, regs->nip);
943 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
944}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000945
946void altivec_unavailable_exception(struct pt_regs *regs)
947{
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000948 if (user_mode(regs)) {
949 /* A user program has executed an altivec instruction,
950 but this kernel doesn't support altivec. */
951 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
952 return;
953 }
Anton Blanchard6c4841c2006-10-13 11:41:00 +1000954
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000955 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
956 "%lx at %lx\n", regs->trap, regs->nip);
957 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000958}
959
Michael Neulingce48b212008-06-25 14:07:18 +1000960void vsx_unavailable_exception(struct pt_regs *regs)
961{
962 if (user_mode(regs)) {
963 /* A user program has executed an vsx instruction,
964 but this kernel doesn't support vsx. */
965 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
966 return;
967 }
968
969 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
970 "%lx at %lx\n", regs->trap, regs->nip);
971 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
972}
973
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000974void performance_monitor_exception(struct pt_regs *regs)
975{
976 perf_irq(regs);
977}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +1000978
Paul Mackerras8dad3f92005-10-06 13:27:05 +1000979#ifdef CONFIG_8xx
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000980void SoftwareEmulation(struct pt_regs *regs)
981{
982 extern int do_mathemu(struct pt_regs *);
983 extern int Soft_emulate_8xx(struct pt_regs *);
Scott Wood5dd57a12007-09-18 15:29:35 -0500984#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000985 int errcode;
Scott Wood5dd57a12007-09-18 15:29:35 -0500986#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +1000987
988 CHECK_FULL_REGS(regs);
989
990 if (!user_mode(regs)) {
991 debugger(regs);
992 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
993 }
994
995#ifdef CONFIG_MATH_EMULATION
996 errcode = do_mathemu(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +0000997 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +0000998 PPC_WARN_EMULATED(math, regs);
Kumar Gala5fad2932007-02-07 01:47:59 -0600999
1000 switch (errcode) {
1001 case 0:
1002 emulate_single_step(regs);
1003 return;
1004 case 1: {
1005 int code = 0;
1006 code = __parse_fpscr(current->thread.fpscr.val);
1007 _exception(SIGFPE, regs, code, regs->nip);
1008 return;
1009 }
1010 case -EFAULT:
1011 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1012 return;
1013 default:
1014 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1015 return;
1016 }
1017
Scott Wood5dd57a12007-09-18 15:29:35 -05001018#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001019 errcode = Soft_emulate_8xx(regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001020 if (errcode >= 0)
Anton Blanchardeecff812009-10-27 18:46:55 +00001021 PPC_WARN_EMULATED(8xx, regs);
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001022
Kumar Gala5fad2932007-02-07 01:47:59 -06001023 switch (errcode) {
1024 case 0:
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001025 emulate_single_step(regs);
Kumar Gala5fad2932007-02-07 01:47:59 -06001026 return;
1027 case 1:
1028 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1029 return;
1030 case -EFAULT:
1031 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1032 return;
1033 }
Scott Wood5dd57a12007-09-18 15:29:35 -05001034#else
1035 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
Kumar Gala5fad2932007-02-07 01:47:59 -06001036#endif
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001037}
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001038#endif /* CONFIG_8xx */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001039
1040#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1041
Kumar Galaf8279622008-06-26 02:01:37 -05001042void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001043{
Roland McGrathec097c82009-05-28 21:26:38 +00001044 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1045 * on server, it stops on the target of the branch. In order to simulate
1046 * the server behaviour, we thus restart right away with a single step
1047 * instead of stopping here when hitting a BT
1048 */
1049 if (debug_status & DBSR_BT) {
1050 regs->msr &= ~MSR_DE;
1051
1052 /* Disable BT */
1053 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1054 /* Clear the BT event */
1055 mtspr(SPRN_DBSR, DBSR_BT);
1056
1057 /* Do the single step trick only when coming from userspace */
1058 if (user_mode(regs)) {
1059 current->thread.dbcr0 &= ~DBCR0_BT;
1060 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1061 regs->msr |= MSR_DE;
1062 return;
1063 }
1064
1065 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1066 5, SIGTRAP) == NOTIFY_STOP) {
1067 return;
1068 }
1069 if (debugger_sstep(regs))
1070 return;
1071 } else if (debug_status & DBSR_IC) { /* Instruction complete */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001072 regs->msr &= ~MSR_DE;
Kumar Galaf8279622008-06-26 02:01:37 -05001073
1074 /* Disable instruction completion */
1075 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1076 /* Clear the instruction completion event */
1077 mtspr(SPRN_DBSR, DBSR_IC);
1078
1079 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1080 5, SIGTRAP) == NOTIFY_STOP) {
1081 return;
1082 }
1083
1084 if (debugger_sstep(regs))
1085 return;
1086
Roland McGrathec097c82009-05-28 21:26:38 +00001087 if (user_mode(regs))
1088 current->thread.dbcr0 &= ~(DBCR0_IC);
Kumar Galaf8279622008-06-26 02:01:37 -05001089
1090 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
Luis Machadod6a61bf2008-07-24 02:10:41 +10001091 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1092 regs->msr &= ~MSR_DE;
1093
1094 if (user_mode(regs)) {
1095 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1096 DBCR0_IDM);
1097 } else {
1098 /* Disable DAC interupts */
1099 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1100 DBSR_DAC1W | DBCR0_IDM));
1101
1102 /* Clear the DAC event */
1103 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1104 }
1105 /* Setup and send the trap to the handler */
1106 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001107 }
1108}
1109#endif /* CONFIG_4xx || CONFIG_BOOKE */
1110
1111#if !defined(CONFIG_TAU_INT)
1112void TAUException(struct pt_regs *regs)
1113{
1114 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1115 regs->nip, regs->msr, regs->trap, print_tainted());
1116}
1117#endif /* CONFIG_INT_TAU */
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001118
1119#ifdef CONFIG_ALTIVEC
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001120void altivec_assist_exception(struct pt_regs *regs)
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001121{
1122 int err;
1123
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001124 if (!user_mode(regs)) {
1125 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1126 " at %lx\n", regs->nip);
Paul Mackerras8dad3f92005-10-06 13:27:05 +10001127 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001128 }
1129
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001130 flush_altivec_to_thread(current);
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001131
Anton Blanchardeecff812009-10-27 18:46:55 +00001132 PPC_WARN_EMULATED(altivec, regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001133 err = emulate_altivec(regs);
1134 if (err == 0) {
1135 regs->nip += 4; /* skip emulated instruction */
1136 emulate_single_step(regs);
1137 return;
1138 }
1139
1140 if (err == -EFAULT) {
1141 /* got an error reading the instruction */
1142 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1143 } else {
1144 /* didn't recognize the instruction */
1145 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1146 if (printk_ratelimit())
1147 printk(KERN_ERR "Unrecognized altivec instruction "
1148 "in %s at %lx\n", current->comm, regs->nip);
1149 current->thread.vscr.u[3] |= 0x10000;
1150 }
1151}
1152#endif /* CONFIG_ALTIVEC */
1153
Michael Neulingce48b212008-06-25 14:07:18 +10001154#ifdef CONFIG_VSX
1155void vsx_assist_exception(struct pt_regs *regs)
1156{
1157 if (!user_mode(regs)) {
1158 printk(KERN_EMERG "VSX assist exception in kernel mode"
1159 " at %lx\n", regs->nip);
1160 die("Kernel VSX assist exception", regs, SIGILL);
1161 }
1162
1163 flush_vsx_to_thread(current);
1164 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1165 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1166}
1167#endif /* CONFIG_VSX */
1168
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001169#ifdef CONFIG_FSL_BOOKE
Kumar Gala620165f2009-02-12 13:54:53 +00001170
1171void doorbell_exception(struct pt_regs *regs)
1172{
1173#ifdef CONFIG_SMP
1174 int cpu = smp_processor_id();
1175 int msg;
1176
1177 if (num_online_cpus() < 2)
1178 return;
1179
1180 for (msg = 0; msg < 4; msg++)
1181 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1182 smp_message_recv(msg);
1183#else
1184 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1185#endif
1186}
1187
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001188void CacheLockingException(struct pt_regs *regs, unsigned long address,
1189 unsigned long error_code)
1190{
1191 /* We treat cache locking instructions from the user
1192 * as priv ops, in the future we could try to do
1193 * something smarter
1194 */
1195 if (error_code & (ESR_DLK|ESR_ILK))
1196 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1197 return;
1198}
1199#endif /* CONFIG_FSL_BOOKE */
1200
1201#ifdef CONFIG_SPE
1202void SPEFloatingPointException(struct pt_regs *regs)
1203{
Liu Yu6a800f32008-10-28 11:50:21 +08001204 extern int do_spe_mathemu(struct pt_regs *regs);
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001205 unsigned long spefscr;
1206 int fpexc_mode;
1207 int code = 0;
Liu Yu6a800f32008-10-28 11:50:21 +08001208 int err;
1209
1210 preempt_disable();
1211 if (regs->msr & MSR_SPE)
1212 giveup_spe(current);
1213 preempt_enable();
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001214
1215 spefscr = current->thread.spefscr;
1216 fpexc_mode = current->thread.fpexc_mode;
1217
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001218 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1219 code = FPE_FLTOVF;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001220 }
1221 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1222 code = FPE_FLTUND;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001223 }
1224 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1225 code = FPE_FLTDIV;
1226 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1227 code = FPE_FLTINV;
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001228 }
1229 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1230 code = FPE_FLTRES;
1231
Liu Yu6a800f32008-10-28 11:50:21 +08001232 err = do_spe_mathemu(regs);
1233 if (err == 0) {
1234 regs->nip += 4; /* skip emulated instruction */
1235 emulate_single_step(regs);
1236 return;
1237 }
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001238
Liu Yu6a800f32008-10-28 11:50:21 +08001239 if (err == -EFAULT) {
1240 /* got an error reading the instruction */
1241 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1242 } else if (err == -EINVAL) {
1243 /* didn't recognize the instruction */
1244 printk(KERN_ERR "unrecognized spe instruction "
1245 "in %s at %lx\n", current->comm, regs->nip);
1246 } else {
1247 _exception(SIGFPE, regs, code, regs->nip);
1248 }
1249
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001250 return;
1251}
Liu Yu6a800f32008-10-28 11:50:21 +08001252
1253void SPEFloatingPointRoundException(struct pt_regs *regs)
1254{
1255 extern int speround_handler(struct pt_regs *regs);
1256 int err;
1257
1258 preempt_disable();
1259 if (regs->msr & MSR_SPE)
1260 giveup_spe(current);
1261 preempt_enable();
1262
1263 regs->nip -= 4;
1264 err = speround_handler(regs);
1265 if (err == 0) {
1266 regs->nip += 4; /* skip emulated instruction */
1267 emulate_single_step(regs);
1268 return;
1269 }
1270
1271 if (err == -EFAULT) {
1272 /* got an error reading the instruction */
1273 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1274 } else if (err == -EINVAL) {
1275 /* didn't recognize the instruction */
1276 printk(KERN_ERR "unrecognized spe instruction "
1277 "in %s at %lx\n", current->comm, regs->nip);
1278 } else {
1279 _exception(SIGFPE, regs, 0, regs->nip);
1280 return;
1281 }
1282}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001283#endif
1284
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001285/*
1286 * We enter here if we get an unrecoverable exception, that is, one
1287 * that happened at a point where the RI (recoverable interrupt) bit
1288 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1289 * we therefore lost state by taking this exception.
1290 */
1291void unrecoverable_exception(struct pt_regs *regs)
1292{
1293 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1294 regs->trap, regs->nip);
1295 die("Unrecoverable exception", regs, SIGABRT);
1296}
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001297
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001298#ifdef CONFIG_BOOKE_WDT
1299/*
1300 * Default handler for a Watchdog exception,
1301 * spins until a reboot occurs
1302 */
1303void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1304{
1305 /* Generic WatchdogHandler, implement your own */
1306 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1307 return;
1308}
1309
1310void WatchdogException(struct pt_regs *regs)
1311{
1312 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1313 WatchdogHandler(regs);
1314}
1315#endif
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001316
Stephen Rothwelldc1c1ca2005-10-01 18:43:42 +10001317/*
1318 * We enter here if we discover during exception entry that we are
1319 * running in supervisor mode with a userspace value in the stack pointer.
1320 */
1321void kernel_bad_stack(struct pt_regs *regs)
1322{
1323 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1324 regs->gpr[1], regs->nip);
1325 die("Bad kernel stack pointer", regs, SIGABRT);
1326}
Paul Mackerras14cf11a2005-09-26 16:04:21 +10001327
1328void __init trap_init(void)
1329{
1330}
Geert Uytterhoeven80947e72009-05-18 02:10:05 +00001331
1332
1333#ifdef CONFIG_PPC_EMULATED_STATS
1334
1335#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1336
1337struct ppc_emulated ppc_emulated = {
1338#ifdef CONFIG_ALTIVEC
1339 WARN_EMULATED_SETUP(altivec),
1340#endif
1341 WARN_EMULATED_SETUP(dcba),
1342 WARN_EMULATED_SETUP(dcbz),
1343 WARN_EMULATED_SETUP(fp_pair),
1344 WARN_EMULATED_SETUP(isel),
1345 WARN_EMULATED_SETUP(mcrxr),
1346 WARN_EMULATED_SETUP(mfpvr),
1347 WARN_EMULATED_SETUP(multiple),
1348 WARN_EMULATED_SETUP(popcntb),
1349 WARN_EMULATED_SETUP(spe),
1350 WARN_EMULATED_SETUP(string),
1351 WARN_EMULATED_SETUP(unaligned),
1352#ifdef CONFIG_MATH_EMULATION
1353 WARN_EMULATED_SETUP(math),
1354#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1355 WARN_EMULATED_SETUP(8xx),
1356#endif
1357#ifdef CONFIG_VSX
1358 WARN_EMULATED_SETUP(vsx),
1359#endif
1360};
1361
1362u32 ppc_warn_emulated;
1363
1364void ppc_warn_emulated_print(const char *type)
1365{
1366 if (printk_ratelimit())
1367 pr_warning("%s used emulated %s instruction\n", current->comm,
1368 type);
1369}
1370
1371static int __init ppc_warn_emulated_init(void)
1372{
1373 struct dentry *dir, *d;
1374 unsigned int i;
1375 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1376
1377 if (!powerpc_debugfs_root)
1378 return -ENODEV;
1379
1380 dir = debugfs_create_dir("emulated_instructions",
1381 powerpc_debugfs_root);
1382 if (!dir)
1383 return -ENOMEM;
1384
1385 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1386 &ppc_warn_emulated);
1387 if (!d)
1388 goto fail;
1389
1390 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1391 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1392 (u32 *)&entries[i].val.counter);
1393 if (!d)
1394 goto fail;
1395 }
1396
1397 return 0;
1398
1399fail:
1400 debugfs_remove_recursive(dir);
1401 return -ENOMEM;
1402}
1403
1404device_initcall(ppc_warn_emulated_init);
1405
1406#endif /* CONFIG_PPC_EMULATED_STATS */