sysfs: remove "last sysfs file:" line from the oops messages
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / kernel / traps.c
blobd782cd71c07ca57c1e0b7f4047ae125df6f8dac3
1 /*
2 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
3 * Copyright 2007-2010 Freescale Semiconductor, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Modified by Cort Dougan (cort@cs.nmt.edu)
11 * and Paul Mackerras (paulus@samba.org)
15 * This file handles the architecture-dependent parts of hardware exceptions
18 #include <linux/errno.h>
19 #include <linux/sched.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/stddef.h>
23 #include <linux/unistd.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/prctl.h>
30 #include <linux/delay.h>
31 #include <linux/kprobes.h>
32 #include <linux/kexec.h>
33 #include <linux/backlight.h>
34 #include <linux/bug.h>
35 #include <linux/kdebug.h>
36 #include <linux/debugfs.h>
38 #include <asm/emulated_ops.h>
39 #include <asm/pgtable.h>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/io.h>
43 #include <asm/machdep.h>
44 #include <asm/rtas.h>
45 #include <asm/pmc.h>
46 #ifdef CONFIG_PPC32
47 #include <asm/reg.h>
48 #endif
49 #ifdef CONFIG_PMAC_BACKLIGHT
50 #include <asm/backlight.h>
51 #endif
52 #ifdef CONFIG_PPC64
53 #include <asm/firmware.h>
54 #include <asm/processor.h>
55 #endif
56 #include <asm/kexec.h>
57 #include <asm/ppc-opcode.h>
59 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
60 int (*__debugger)(struct pt_regs *regs) __read_mostly;
61 int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
62 int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
63 int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
64 int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
65 int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
66 int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
68 EXPORT_SYMBOL(__debugger);
69 EXPORT_SYMBOL(__debugger_ipi);
70 EXPORT_SYMBOL(__debugger_bpt);
71 EXPORT_SYMBOL(__debugger_sstep);
72 EXPORT_SYMBOL(__debugger_iabr_match);
73 EXPORT_SYMBOL(__debugger_dabr_match);
74 EXPORT_SYMBOL(__debugger_fault_handler);
75 #endif
78 * Trap & Exception support
81 #ifdef CONFIG_PMAC_BACKLIGHT
82 static void pmac_backlight_unblank(void)
84 mutex_lock(&pmac_backlight_mutex);
85 if (pmac_backlight) {
86 struct backlight_properties *props;
88 props = &pmac_backlight->props;
89 props->brightness = props->max_brightness;
90 props->power = FB_BLANK_UNBLANK;
91 backlight_update_status(pmac_backlight);
93 mutex_unlock(&pmac_backlight_mutex);
95 #else
96 static inline void pmac_backlight_unblank(void) { }
97 #endif
99 int die(const char *str, struct pt_regs *regs, long err)
101 static struct {
102 raw_spinlock_t lock;
103 u32 lock_owner;
104 int lock_owner_depth;
105 } die = {
106 .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
107 .lock_owner = -1,
108 .lock_owner_depth = 0
110 static int die_counter;
111 unsigned long flags;
113 if (debugger(regs))
114 return 1;
116 oops_enter();
118 if (die.lock_owner != raw_smp_processor_id()) {
119 console_verbose();
120 raw_spin_lock_irqsave(&die.lock, flags);
121 die.lock_owner = smp_processor_id();
122 die.lock_owner_depth = 0;
123 bust_spinlocks(1);
124 if (machine_is(powermac))
125 pmac_backlight_unblank();
126 } else {
127 local_save_flags(flags);
130 if (++die.lock_owner_depth < 3) {
131 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
132 #ifdef CONFIG_PREEMPT
133 printk("PREEMPT ");
134 #endif
135 #ifdef CONFIG_SMP
136 printk("SMP NR_CPUS=%d ", NR_CPUS);
137 #endif
138 #ifdef CONFIG_DEBUG_PAGEALLOC
139 printk("DEBUG_PAGEALLOC ");
140 #endif
141 #ifdef CONFIG_NUMA
142 printk("NUMA ");
143 #endif
144 printk("%s\n", ppc_md.name ? ppc_md.name : "");
146 if (notify_die(DIE_OOPS, str, regs, err, 255,
147 SIGSEGV) == NOTIFY_STOP)
148 return 1;
150 print_modules();
151 show_regs(regs);
152 } else {
153 printk("Recursive die() failure, output suppressed\n");
156 bust_spinlocks(0);
157 die.lock_owner = -1;
158 add_taint(TAINT_DIE);
159 raw_spin_unlock_irqrestore(&die.lock, flags);
161 if (kexec_should_crash(current) ||
162 kexec_sr_activated(smp_processor_id()))
163 crash_kexec(regs);
164 crash_kexec_secondary(regs);
166 if (in_interrupt())
167 panic("Fatal exception in interrupt");
169 if (panic_on_oops)
170 panic("Fatal exception");
172 oops_exit();
173 do_exit(err);
175 return 0;
178 void user_single_step_siginfo(struct task_struct *tsk,
179 struct pt_regs *regs, siginfo_t *info)
181 memset(info, 0, sizeof(*info));
182 info->si_signo = SIGTRAP;
183 info->si_code = TRAP_TRACE;
184 info->si_addr = (void __user *)regs->nip;
187 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
189 siginfo_t info;
190 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
191 "at %08lx nip %08lx lr %08lx code %x\n";
192 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
193 "at %016lx nip %016lx lr %016lx code %x\n";
195 if (!user_mode(regs)) {
196 if (die("Exception in kernel mode", regs, signr))
197 return;
198 } else if (show_unhandled_signals &&
199 unhandled_signal(current, signr) &&
200 printk_ratelimit()) {
201 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
202 current->comm, current->pid, signr,
203 addr, regs->nip, regs->link, code);
206 memset(&info, 0, sizeof(info));
207 info.si_signo = signr;
208 info.si_code = code;
209 info.si_addr = (void __user *) addr;
210 force_sig_info(signr, &info, current);
213 #ifdef CONFIG_PPC64
214 void system_reset_exception(struct pt_regs *regs)
216 /* See if any machine dependent calls */
217 if (ppc_md.system_reset_exception) {
218 if (ppc_md.system_reset_exception(regs))
219 return;
222 #ifdef CONFIG_KEXEC
223 cpu_set(smp_processor_id(), cpus_in_sr);
224 #endif
226 die("System Reset", regs, SIGABRT);
229 * Some CPUs when released from the debugger will execute this path.
230 * These CPUs entered the debugger via a soft-reset. If the CPU was
231 * hung before entering the debugger it will return to the hung
232 * state when exiting this function. This causes a problem in
233 * kdump since the hung CPU(s) will not respond to the IPI sent
234 * from kdump. To prevent the problem we call crash_kexec_secondary()
235 * here. If a kdump had not been initiated or we exit the debugger
236 * with the "exit and recover" command (x) crash_kexec_secondary()
237 * will return after 5ms and the CPU returns to its previous state.
239 crash_kexec_secondary(regs);
241 /* Must die if the interrupt is not recoverable */
242 if (!(regs->msr & MSR_RI))
243 panic("Unrecoverable System Reset");
245 /* What should we do here? We could issue a shutdown or hard reset. */
247 #endif
250 * I/O accesses can cause machine checks on powermacs.
251 * Check if the NIP corresponds to the address of a sync
252 * instruction for which there is an entry in the exception
253 * table.
254 * Note that the 601 only takes a machine check on TEA
255 * (transfer error ack) signal assertion, and does not
256 * set any of the top 16 bits of SRR1.
257 * -- paulus.
259 static inline int check_io_access(struct pt_regs *regs)
261 #ifdef CONFIG_PPC32
262 unsigned long msr = regs->msr;
263 const struct exception_table_entry *entry;
264 unsigned int *nip = (unsigned int *)regs->nip;
266 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
267 && (entry = search_exception_tables(regs->nip)) != NULL) {
269 * Check that it's a sync instruction, or somewhere
270 * in the twi; isync; nop sequence that inb/inw/inl uses.
271 * As the address is in the exception table
272 * we should be able to read the instr there.
273 * For the debug message, we look at the preceding
274 * load or store.
276 if (*nip == 0x60000000) /* nop */
277 nip -= 2;
278 else if (*nip == 0x4c00012c) /* isync */
279 --nip;
280 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
281 /* sync or twi */
282 unsigned int rb;
284 --nip;
285 rb = (*nip >> 11) & 0x1f;
286 printk(KERN_DEBUG "%s bad port %lx at %p\n",
287 (*nip & 0x100)? "OUT to": "IN from",
288 regs->gpr[rb] - _IO_BASE, nip);
289 regs->msr |= MSR_RI;
290 regs->nip = entry->fixup;
291 return 1;
294 #endif /* CONFIG_PPC32 */
295 return 0;
298 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
299 /* On 4xx, the reason for the machine check or program exception
300 is in the ESR. */
301 #define get_reason(regs) ((regs)->dsisr)
302 #ifndef CONFIG_FSL_BOOKE
303 #define get_mc_reason(regs) ((regs)->dsisr)
304 #else
305 #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
306 #endif
307 #define REASON_FP ESR_FP
308 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
309 #define REASON_PRIVILEGED ESR_PPR
310 #define REASON_TRAP ESR_PTR
312 /* single-step stuff */
313 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
314 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
316 #else
317 /* On non-4xx, the reason for the machine check or program
318 exception is in the MSR. */
319 #define get_reason(regs) ((regs)->msr)
320 #define get_mc_reason(regs) ((regs)->msr)
321 #define REASON_FP 0x100000
322 #define REASON_ILLEGAL 0x80000
323 #define REASON_PRIVILEGED 0x40000
324 #define REASON_TRAP 0x20000
326 #define single_stepping(regs) ((regs)->msr & MSR_SE)
327 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
328 #endif
330 #if defined(CONFIG_4xx)
331 int machine_check_4xx(struct pt_regs *regs)
333 unsigned long reason = get_mc_reason(regs);
335 if (reason & ESR_IMCP) {
336 printk("Instruction");
337 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
338 } else
339 printk("Data");
340 printk(" machine check in kernel mode.\n");
342 return 0;
345 int machine_check_440A(struct pt_regs *regs)
347 unsigned long reason = get_mc_reason(regs);
349 printk("Machine check in kernel mode.\n");
350 if (reason & ESR_IMCP){
351 printk("Instruction Synchronous Machine Check exception\n");
352 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
354 else {
355 u32 mcsr = mfspr(SPRN_MCSR);
356 if (mcsr & MCSR_IB)
357 printk("Instruction Read PLB Error\n");
358 if (mcsr & MCSR_DRB)
359 printk("Data Read PLB Error\n");
360 if (mcsr & MCSR_DWB)
361 printk("Data Write PLB Error\n");
362 if (mcsr & MCSR_TLBP)
363 printk("TLB Parity Error\n");
364 if (mcsr & MCSR_ICP){
365 flush_instruction_cache();
366 printk("I-Cache Parity Error\n");
368 if (mcsr & MCSR_DCSP)
369 printk("D-Cache Search Parity Error\n");
370 if (mcsr & MCSR_DCFP)
371 printk("D-Cache Flush Parity Error\n");
372 if (mcsr & MCSR_IMPE)
373 printk("Machine Check exception is imprecise\n");
375 /* Clear MCSR */
376 mtspr(SPRN_MCSR, mcsr);
378 return 0;
381 int machine_check_47x(struct pt_regs *regs)
383 unsigned long reason = get_mc_reason(regs);
384 u32 mcsr;
386 printk(KERN_ERR "Machine check in kernel mode.\n");
387 if (reason & ESR_IMCP) {
388 printk(KERN_ERR
389 "Instruction Synchronous Machine Check exception\n");
390 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
391 return 0;
393 mcsr = mfspr(SPRN_MCSR);
394 if (mcsr & MCSR_IB)
395 printk(KERN_ERR "Instruction Read PLB Error\n");
396 if (mcsr & MCSR_DRB)
397 printk(KERN_ERR "Data Read PLB Error\n");
398 if (mcsr & MCSR_DWB)
399 printk(KERN_ERR "Data Write PLB Error\n");
400 if (mcsr & MCSR_TLBP)
401 printk(KERN_ERR "TLB Parity Error\n");
402 if (mcsr & MCSR_ICP) {
403 flush_instruction_cache();
404 printk(KERN_ERR "I-Cache Parity Error\n");
406 if (mcsr & MCSR_DCSP)
407 printk(KERN_ERR "D-Cache Search Parity Error\n");
408 if (mcsr & PPC47x_MCSR_GPR)
409 printk(KERN_ERR "GPR Parity Error\n");
410 if (mcsr & PPC47x_MCSR_FPR)
411 printk(KERN_ERR "FPR Parity Error\n");
412 if (mcsr & PPC47x_MCSR_IPR)
413 printk(KERN_ERR "Machine Check exception is imprecise\n");
415 /* Clear MCSR */
416 mtspr(SPRN_MCSR, mcsr);
418 return 0;
420 #elif defined(CONFIG_E500)
421 int machine_check_e500mc(struct pt_regs *regs)
423 unsigned long mcsr = mfspr(SPRN_MCSR);
424 unsigned long reason = mcsr;
425 int recoverable = 1;
427 printk("Machine check in kernel mode.\n");
428 printk("Caused by (from MCSR=%lx): ", reason);
430 if (reason & MCSR_MCP)
431 printk("Machine Check Signal\n");
433 if (reason & MCSR_ICPERR) {
434 printk("Instruction Cache Parity Error\n");
437 * This is recoverable by invalidating the i-cache.
439 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
440 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
444 * This will generally be accompanied by an instruction
445 * fetch error report -- only treat MCSR_IF as fatal
446 * if it wasn't due to an L1 parity error.
448 reason &= ~MCSR_IF;
451 if (reason & MCSR_DCPERR_MC) {
452 printk("Data Cache Parity Error\n");
453 recoverable = 0;
456 if (reason & MCSR_L2MMU_MHIT) {
457 printk("Hit on multiple TLB entries\n");
458 recoverable = 0;
461 if (reason & MCSR_NMI)
462 printk("Non-maskable interrupt\n");
464 if (reason & MCSR_IF) {
465 printk("Instruction Fetch Error Report\n");
466 recoverable = 0;
469 if (reason & MCSR_LD) {
470 printk("Load Error Report\n");
471 recoverable = 0;
474 if (reason & MCSR_ST) {
475 printk("Store Error Report\n");
476 recoverable = 0;
479 if (reason & MCSR_LDG) {
480 printk("Guarded Load Error Report\n");
481 recoverable = 0;
484 if (reason & MCSR_TLBSYNC)
485 printk("Simultaneous tlbsync operations\n");
487 if (reason & MCSR_BSL2_ERR) {
488 printk("Level 2 Cache Error\n");
489 recoverable = 0;
492 if (reason & MCSR_MAV) {
493 u64 addr;
495 addr = mfspr(SPRN_MCAR);
496 addr |= (u64)mfspr(SPRN_MCARU) << 32;
498 printk("Machine Check %s Address: %#llx\n",
499 reason & MCSR_MEA ? "Effective" : "Physical", addr);
502 mtspr(SPRN_MCSR, mcsr);
503 return mfspr(SPRN_MCSR) == 0 && recoverable;
506 int machine_check_e500(struct pt_regs *regs)
508 unsigned long reason = get_mc_reason(regs);
510 printk("Machine check in kernel mode.\n");
511 printk("Caused by (from MCSR=%lx): ", reason);
513 if (reason & MCSR_MCP)
514 printk("Machine Check Signal\n");
515 if (reason & MCSR_ICPERR)
516 printk("Instruction Cache Parity Error\n");
517 if (reason & MCSR_DCP_PERR)
518 printk("Data Cache Push Parity Error\n");
519 if (reason & MCSR_DCPERR)
520 printk("Data Cache Parity Error\n");
521 if (reason & MCSR_BUS_IAERR)
522 printk("Bus - Instruction Address Error\n");
523 if (reason & MCSR_BUS_RAERR)
524 printk("Bus - Read Address Error\n");
525 if (reason & MCSR_BUS_WAERR)
526 printk("Bus - Write Address Error\n");
527 if (reason & MCSR_BUS_IBERR)
528 printk("Bus - Instruction Data Error\n");
529 if (reason & MCSR_BUS_RBERR)
530 printk("Bus - Read Data Bus Error\n");
531 if (reason & MCSR_BUS_WBERR)
532 printk("Bus - Read Data Bus Error\n");
533 if (reason & MCSR_BUS_IPERR)
534 printk("Bus - Instruction Parity Error\n");
535 if (reason & MCSR_BUS_RPERR)
536 printk("Bus - Read Parity Error\n");
538 return 0;
541 int machine_check_generic(struct pt_regs *regs)
543 return 0;
545 #elif defined(CONFIG_E200)
546 int machine_check_e200(struct pt_regs *regs)
548 unsigned long reason = get_mc_reason(regs);
550 printk("Machine check in kernel mode.\n");
551 printk("Caused by (from MCSR=%lx): ", reason);
553 if (reason & MCSR_MCP)
554 printk("Machine Check Signal\n");
555 if (reason & MCSR_CP_PERR)
556 printk("Cache Push Parity Error\n");
557 if (reason & MCSR_CPERR)
558 printk("Cache Parity Error\n");
559 if (reason & MCSR_EXCP_ERR)
560 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
561 if (reason & MCSR_BUS_IRERR)
562 printk("Bus - Read Bus Error on instruction fetch\n");
563 if (reason & MCSR_BUS_DRERR)
564 printk("Bus - Read Bus Error on data load\n");
565 if (reason & MCSR_BUS_WRERR)
566 printk("Bus - Write Bus Error on buffered store or cache line push\n");
568 return 0;
570 #else
571 int machine_check_generic(struct pt_regs *regs)
573 unsigned long reason = get_mc_reason(regs);
575 printk("Machine check in kernel mode.\n");
576 printk("Caused by (from SRR1=%lx): ", reason);
577 switch (reason & 0x601F0000) {
578 case 0x80000:
579 printk("Machine check signal\n");
580 break;
581 case 0: /* for 601 */
582 case 0x40000:
583 case 0x140000: /* 7450 MSS error and TEA */
584 printk("Transfer error ack signal\n");
585 break;
586 case 0x20000:
587 printk("Data parity error signal\n");
588 break;
589 case 0x10000:
590 printk("Address parity error signal\n");
591 break;
592 case 0x20000000:
593 printk("L1 Data Cache error\n");
594 break;
595 case 0x40000000:
596 printk("L1 Instruction Cache error\n");
597 break;
598 case 0x00100000:
599 printk("L2 data cache parity error\n");
600 break;
601 default:
602 printk("Unknown values in msr\n");
604 return 0;
606 #endif /* everything else */
608 void machine_check_exception(struct pt_regs *regs)
610 int recover = 0;
612 __get_cpu_var(irq_stat).mce_exceptions++;
614 /* See if any machine dependent calls. In theory, we would want
615 * to call the CPU first, and call the ppc_md. one if the CPU
616 * one returns a positive number. However there is existing code
617 * that assumes the board gets a first chance, so let's keep it
618 * that way for now and fix things later. --BenH.
620 if (ppc_md.machine_check_exception)
621 recover = ppc_md.machine_check_exception(regs);
622 else if (cur_cpu_spec->machine_check)
623 recover = cur_cpu_spec->machine_check(regs);
625 if (recover > 0)
626 return;
628 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
629 /* the qspan pci read routines can cause machine checks -- Cort
631 * yuck !!! that totally needs to go away ! There are better ways
632 * to deal with that than having a wart in the mcheck handler.
633 * -- BenH
635 bad_page_fault(regs, regs->dar, SIGBUS);
636 return;
637 #endif
639 if (debugger_fault_handler(regs))
640 return;
642 if (check_io_access(regs))
643 return;
645 die("Machine check", regs, SIGBUS);
647 /* Must die if the interrupt is not recoverable */
648 if (!(regs->msr & MSR_RI))
649 panic("Unrecoverable Machine check");
652 void SMIException(struct pt_regs *regs)
654 die("System Management Interrupt", regs, SIGABRT);
657 void unknown_exception(struct pt_regs *regs)
659 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
660 regs->nip, regs->msr, regs->trap);
662 _exception(SIGTRAP, regs, 0, 0);
665 void instruction_breakpoint_exception(struct pt_regs *regs)
667 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
668 5, SIGTRAP) == NOTIFY_STOP)
669 return;
670 if (debugger_iabr_match(regs))
671 return;
672 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
675 void RunModeException(struct pt_regs *regs)
677 _exception(SIGTRAP, regs, 0, 0);
680 void __kprobes single_step_exception(struct pt_regs *regs)
682 clear_single_step(regs);
684 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
685 5, SIGTRAP) == NOTIFY_STOP)
686 return;
687 if (debugger_sstep(regs))
688 return;
690 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
694 * After we have successfully emulated an instruction, we have to
695 * check if the instruction was being single-stepped, and if so,
696 * pretend we got a single-step exception. This was pointed out
697 * by Kumar Gala. -- paulus
699 static void emulate_single_step(struct pt_regs *regs)
701 if (single_stepping(regs))
702 single_step_exception(regs);
705 static inline int __parse_fpscr(unsigned long fpscr)
707 int ret = 0;
709 /* Invalid operation */
710 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
711 ret = FPE_FLTINV;
713 /* Overflow */
714 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
715 ret = FPE_FLTOVF;
717 /* Underflow */
718 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
719 ret = FPE_FLTUND;
721 /* Divide by zero */
722 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
723 ret = FPE_FLTDIV;
725 /* Inexact result */
726 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
727 ret = FPE_FLTRES;
729 return ret;
732 static void parse_fpe(struct pt_regs *regs)
734 int code = 0;
736 flush_fp_to_thread(current);
738 code = __parse_fpscr(current->thread.fpscr.val);
740 _exception(SIGFPE, regs, code, regs->nip);
744 * Illegal instruction emulation support. Originally written to
745 * provide the PVR to user applications using the mfspr rd, PVR.
746 * Return non-zero if we can't emulate, or -EFAULT if the associated
747 * memory access caused an access fault. Return zero on success.
749 * There are a couple of ways to do this, either "decode" the instruction
750 * or directly match lots of bits. In this case, matching lots of
751 * bits is faster and easier.
754 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
756 u8 rT = (instword >> 21) & 0x1f;
757 u8 rA = (instword >> 16) & 0x1f;
758 u8 NB_RB = (instword >> 11) & 0x1f;
759 u32 num_bytes;
760 unsigned long EA;
761 int pos = 0;
763 /* Early out if we are an invalid form of lswx */
764 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
765 if ((rT == rA) || (rT == NB_RB))
766 return -EINVAL;
768 EA = (rA == 0) ? 0 : regs->gpr[rA];
770 switch (instword & PPC_INST_STRING_MASK) {
771 case PPC_INST_LSWX:
772 case PPC_INST_STSWX:
773 EA += NB_RB;
774 num_bytes = regs->xer & 0x7f;
775 break;
776 case PPC_INST_LSWI:
777 case PPC_INST_STSWI:
778 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
779 break;
780 default:
781 return -EINVAL;
784 while (num_bytes != 0)
786 u8 val;
787 u32 shift = 8 * (3 - (pos & 0x3));
789 switch ((instword & PPC_INST_STRING_MASK)) {
790 case PPC_INST_LSWX:
791 case PPC_INST_LSWI:
792 if (get_user(val, (u8 __user *)EA))
793 return -EFAULT;
794 /* first time updating this reg,
795 * zero it out */
796 if (pos == 0)
797 regs->gpr[rT] = 0;
798 regs->gpr[rT] |= val << shift;
799 break;
800 case PPC_INST_STSWI:
801 case PPC_INST_STSWX:
802 val = regs->gpr[rT] >> shift;
803 if (put_user(val, (u8 __user *)EA))
804 return -EFAULT;
805 break;
807 /* move EA to next address */
808 EA += 1;
809 num_bytes--;
811 /* manage our position within the register */
812 if (++pos == 4) {
813 pos = 0;
814 if (++rT == 32)
815 rT = 0;
819 return 0;
822 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
824 u32 ra,rs;
825 unsigned long tmp;
827 ra = (instword >> 16) & 0x1f;
828 rs = (instword >> 21) & 0x1f;
830 tmp = regs->gpr[rs];
831 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
832 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
833 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
834 regs->gpr[ra] = tmp;
836 return 0;
839 static int emulate_isel(struct pt_regs *regs, u32 instword)
841 u8 rT = (instword >> 21) & 0x1f;
842 u8 rA = (instword >> 16) & 0x1f;
843 u8 rB = (instword >> 11) & 0x1f;
844 u8 BC = (instword >> 6) & 0x1f;
845 u8 bit;
846 unsigned long tmp;
848 tmp = (rA == 0) ? 0 : regs->gpr[rA];
849 bit = (regs->ccr >> (31 - BC)) & 0x1;
851 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
853 return 0;
856 static int emulate_instruction(struct pt_regs *regs)
858 u32 instword;
859 u32 rd;
861 if (!user_mode(regs) || (regs->msr & MSR_LE))
862 return -EINVAL;
863 CHECK_FULL_REGS(regs);
865 if (get_user(instword, (u32 __user *)(regs->nip)))
866 return -EFAULT;
868 /* Emulate the mfspr rD, PVR. */
869 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
870 PPC_WARN_EMULATED(mfpvr, regs);
871 rd = (instword >> 21) & 0x1f;
872 regs->gpr[rd] = mfspr(SPRN_PVR);
873 return 0;
876 /* Emulating the dcba insn is just a no-op. */
877 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
878 PPC_WARN_EMULATED(dcba, regs);
879 return 0;
882 /* Emulate the mcrxr insn. */
883 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
884 int shift = (instword >> 21) & 0x1c;
885 unsigned long msk = 0xf0000000UL >> shift;
887 PPC_WARN_EMULATED(mcrxr, regs);
888 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
889 regs->xer &= ~0xf0000000UL;
890 return 0;
893 /* Emulate load/store string insn. */
894 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
895 PPC_WARN_EMULATED(string, regs);
896 return emulate_string_inst(regs, instword);
899 /* Emulate the popcntb (Population Count Bytes) instruction. */
900 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
901 PPC_WARN_EMULATED(popcntb, regs);
902 return emulate_popcntb_inst(regs, instword);
905 /* Emulate isel (Integer Select) instruction */
906 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
907 PPC_WARN_EMULATED(isel, regs);
908 return emulate_isel(regs, instword);
911 return -EINVAL;
914 int is_valid_bugaddr(unsigned long addr)
916 return is_kernel_addr(addr);
919 void __kprobes program_check_exception(struct pt_regs *regs)
921 unsigned int reason = get_reason(regs);
922 extern int do_mathemu(struct pt_regs *regs);
924 /* We can now get here via a FP Unavailable exception if the core
925 * has no FPU, in that case the reason flags will be 0 */
927 if (reason & REASON_FP) {
928 /* IEEE FP exception */
929 parse_fpe(regs);
930 return;
932 if (reason & REASON_TRAP) {
933 /* Debugger is first in line to stop recursive faults in
934 * rcu_lock, notify_die, or atomic_notifier_call_chain */
935 if (debugger_bpt(regs))
936 return;
938 /* trap exception */
939 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
940 == NOTIFY_STOP)
941 return;
943 if (!(regs->msr & MSR_PR) && /* not user-mode */
944 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
945 regs->nip += 4;
946 return;
948 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
949 return;
952 local_irq_enable();
954 #ifdef CONFIG_MATH_EMULATION
955 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
956 * but there seems to be a hardware bug on the 405GP (RevD)
957 * that means ESR is sometimes set incorrectly - either to
958 * ESR_DST (!?) or 0. In the process of chasing this with the
959 * hardware people - not sure if it can happen on any illegal
960 * instruction or only on FP instructions, whether there is a
961 * pattern to occurrences etc. -dgibson 31/Mar/2003 */
962 switch (do_mathemu(regs)) {
963 case 0:
964 emulate_single_step(regs);
965 return;
966 case 1: {
967 int code = 0;
968 code = __parse_fpscr(current->thread.fpscr.val);
969 _exception(SIGFPE, regs, code, regs->nip);
970 return;
972 case -EFAULT:
973 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
974 return;
976 /* fall through on any other errors */
977 #endif /* CONFIG_MATH_EMULATION */
979 /* Try to emulate it if we should. */
980 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
981 switch (emulate_instruction(regs)) {
982 case 0:
983 regs->nip += 4;
984 emulate_single_step(regs);
985 return;
986 case -EFAULT:
987 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
988 return;
992 if (reason & REASON_PRIVILEGED)
993 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
994 else
995 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
998 void alignment_exception(struct pt_regs *regs)
1000 int sig, code, fixed = 0;
1002 /* we don't implement logging of alignment exceptions */
1003 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1004 fixed = fix_alignment(regs);
1006 if (fixed == 1) {
1007 regs->nip += 4; /* skip over emulated instruction */
1008 emulate_single_step(regs);
1009 return;
1012 /* Operand address was bad */
1013 if (fixed == -EFAULT) {
1014 sig = SIGSEGV;
1015 code = SEGV_ACCERR;
1016 } else {
1017 sig = SIGBUS;
1018 code = BUS_ADRALN;
1020 if (user_mode(regs))
1021 _exception(sig, regs, code, regs->dar);
1022 else
1023 bad_page_fault(regs, regs->dar, sig);
1026 void StackOverflow(struct pt_regs *regs)
1028 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1029 current, regs->gpr[1]);
1030 debugger(regs);
1031 show_regs(regs);
1032 panic("kernel stack overflow");
1035 void nonrecoverable_exception(struct pt_regs *regs)
1037 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1038 regs->nip, regs->msr);
1039 debugger(regs);
1040 die("nonrecoverable exception", regs, SIGKILL);
1043 void trace_syscall(struct pt_regs *regs)
1045 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
1046 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
1047 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1050 void kernel_fp_unavailable_exception(struct pt_regs *regs)
1052 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1053 "%lx at %lx\n", regs->trap, regs->nip);
1054 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1057 void altivec_unavailable_exception(struct pt_regs *regs)
1059 if (user_mode(regs)) {
1060 /* A user program has executed an altivec instruction,
1061 but this kernel doesn't support altivec. */
1062 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1063 return;
1066 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1067 "%lx at %lx\n", regs->trap, regs->nip);
1068 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1071 void vsx_unavailable_exception(struct pt_regs *regs)
1073 if (user_mode(regs)) {
1074 /* A user program has executed an vsx instruction,
1075 but this kernel doesn't support vsx. */
1076 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1077 return;
1080 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1081 "%lx at %lx\n", regs->trap, regs->nip);
1082 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1085 void performance_monitor_exception(struct pt_regs *regs)
1087 __get_cpu_var(irq_stat).pmu_irqs++;
1089 perf_irq(regs);
1092 #ifdef CONFIG_8xx
1093 void SoftwareEmulation(struct pt_regs *regs)
1095 extern int do_mathemu(struct pt_regs *);
1096 extern int Soft_emulate_8xx(struct pt_regs *);
1097 #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
1098 int errcode;
1099 #endif
1101 CHECK_FULL_REGS(regs);
1103 if (!user_mode(regs)) {
1104 debugger(regs);
1105 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1108 #ifdef CONFIG_MATH_EMULATION
1109 errcode = do_mathemu(regs);
1110 if (errcode >= 0)
1111 PPC_WARN_EMULATED(math, regs);
1113 switch (errcode) {
1114 case 0:
1115 emulate_single_step(regs);
1116 return;
1117 case 1: {
1118 int code = 0;
1119 code = __parse_fpscr(current->thread.fpscr.val);
1120 _exception(SIGFPE, regs, code, regs->nip);
1121 return;
1123 case -EFAULT:
1124 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1125 return;
1126 default:
1127 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1128 return;
1131 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1132 errcode = Soft_emulate_8xx(regs);
1133 if (errcode >= 0)
1134 PPC_WARN_EMULATED(8xx, regs);
1136 switch (errcode) {
1137 case 0:
1138 emulate_single_step(regs);
1139 return;
1140 case 1:
1141 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1142 return;
1143 case -EFAULT:
1144 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1145 return;
1147 #else
1148 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1149 #endif
1151 #endif /* CONFIG_8xx */
1153 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1154 static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1156 int changed = 0;
1158 * Determine the cause of the debug event, clear the
1159 * event flags and send a trap to the handler. Torez
1161 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1162 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1163 #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1164 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1165 #endif
1166 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1168 changed |= 0x01;
1169 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1170 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1171 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1173 changed |= 0x01;
1174 } else if (debug_status & DBSR_IAC1) {
1175 current->thread.dbcr0 &= ~DBCR0_IAC1;
1176 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1177 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1179 changed |= 0x01;
1180 } else if (debug_status & DBSR_IAC2) {
1181 current->thread.dbcr0 &= ~DBCR0_IAC2;
1182 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1184 changed |= 0x01;
1185 } else if (debug_status & DBSR_IAC3) {
1186 current->thread.dbcr0 &= ~DBCR0_IAC3;
1187 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1188 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1190 changed |= 0x01;
1191 } else if (debug_status & DBSR_IAC4) {
1192 current->thread.dbcr0 &= ~DBCR0_IAC4;
1193 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1195 changed |= 0x01;
1198 * At the point this routine was called, the MSR(DE) was turned off.
1199 * Check all other debug flags and see if that bit needs to be turned
1200 * back on or not.
1202 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1203 regs->msr |= MSR_DE;
1204 else
1205 /* Make sure the IDM flag is off */
1206 current->thread.dbcr0 &= ~DBCR0_IDM;
1208 if (changed & 0x01)
1209 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1212 void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
1214 current->thread.dbsr = debug_status;
1216 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1217 * on server, it stops on the target of the branch. In order to simulate
1218 * the server behaviour, we thus restart right away with a single step
1219 * instead of stopping here when hitting a BT
1221 if (debug_status & DBSR_BT) {
1222 regs->msr &= ~MSR_DE;
1224 /* Disable BT */
1225 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1226 /* Clear the BT event */
1227 mtspr(SPRN_DBSR, DBSR_BT);
1229 /* Do the single step trick only when coming from userspace */
1230 if (user_mode(regs)) {
1231 current->thread.dbcr0 &= ~DBCR0_BT;
1232 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1233 regs->msr |= MSR_DE;
1234 return;
1237 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1238 5, SIGTRAP) == NOTIFY_STOP) {
1239 return;
1241 if (debugger_sstep(regs))
1242 return;
1243 } else if (debug_status & DBSR_IC) { /* Instruction complete */
1244 regs->msr &= ~MSR_DE;
1246 /* Disable instruction completion */
1247 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1248 /* Clear the instruction completion event */
1249 mtspr(SPRN_DBSR, DBSR_IC);
1251 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1252 5, SIGTRAP) == NOTIFY_STOP) {
1253 return;
1256 if (debugger_sstep(regs))
1257 return;
1259 if (user_mode(regs)) {
1260 current->thread.dbcr0 &= ~DBCR0_IC;
1261 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1262 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1263 current->thread.dbcr1))
1264 regs->msr |= MSR_DE;
1265 else
1266 /* Make sure the IDM bit is off */
1267 current->thread.dbcr0 &= ~DBCR0_IDM;
1268 #endif
1271 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1272 } else
1273 handle_debug(regs, debug_status);
1275 #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1277 #if !defined(CONFIG_TAU_INT)
1278 void TAUException(struct pt_regs *regs)
1280 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1281 regs->nip, regs->msr, regs->trap, print_tainted());
1283 #endif /* CONFIG_INT_TAU */
1285 #ifdef CONFIG_ALTIVEC
1286 void altivec_assist_exception(struct pt_regs *regs)
1288 int err;
1290 if (!user_mode(regs)) {
1291 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1292 " at %lx\n", regs->nip);
1293 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1296 flush_altivec_to_thread(current);
1298 PPC_WARN_EMULATED(altivec, regs);
1299 err = emulate_altivec(regs);
1300 if (err == 0) {
1301 regs->nip += 4; /* skip emulated instruction */
1302 emulate_single_step(regs);
1303 return;
1306 if (err == -EFAULT) {
1307 /* got an error reading the instruction */
1308 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1309 } else {
1310 /* didn't recognize the instruction */
1311 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1312 if (printk_ratelimit())
1313 printk(KERN_ERR "Unrecognized altivec instruction "
1314 "in %s at %lx\n", current->comm, regs->nip);
1315 current->thread.vscr.u[3] |= 0x10000;
1318 #endif /* CONFIG_ALTIVEC */
1320 #ifdef CONFIG_VSX
1321 void vsx_assist_exception(struct pt_regs *regs)
1323 if (!user_mode(regs)) {
1324 printk(KERN_EMERG "VSX assist exception in kernel mode"
1325 " at %lx\n", regs->nip);
1326 die("Kernel VSX assist exception", regs, SIGILL);
1329 flush_vsx_to_thread(current);
1330 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1331 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1333 #endif /* CONFIG_VSX */
1335 #ifdef CONFIG_FSL_BOOKE
1336 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1337 unsigned long error_code)
1339 /* We treat cache locking instructions from the user
1340 * as priv ops, in the future we could try to do
1341 * something smarter
1343 if (error_code & (ESR_DLK|ESR_ILK))
1344 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1345 return;
1347 #endif /* CONFIG_FSL_BOOKE */
1349 #ifdef CONFIG_SPE
1350 void SPEFloatingPointException(struct pt_regs *regs)
1352 extern int do_spe_mathemu(struct pt_regs *regs);
1353 unsigned long spefscr;
1354 int fpexc_mode;
1355 int code = 0;
1356 int err;
1358 preempt_disable();
1359 if (regs->msr & MSR_SPE)
1360 giveup_spe(current);
1361 preempt_enable();
1363 spefscr = current->thread.spefscr;
1364 fpexc_mode = current->thread.fpexc_mode;
1366 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1367 code = FPE_FLTOVF;
1369 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1370 code = FPE_FLTUND;
1372 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1373 code = FPE_FLTDIV;
1374 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1375 code = FPE_FLTINV;
1377 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1378 code = FPE_FLTRES;
1380 err = do_spe_mathemu(regs);
1381 if (err == 0) {
1382 regs->nip += 4; /* skip emulated instruction */
1383 emulate_single_step(regs);
1384 return;
1387 if (err == -EFAULT) {
1388 /* got an error reading the instruction */
1389 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1390 } else if (err == -EINVAL) {
1391 /* didn't recognize the instruction */
1392 printk(KERN_ERR "unrecognized spe instruction "
1393 "in %s at %lx\n", current->comm, regs->nip);
1394 } else {
1395 _exception(SIGFPE, regs, code, regs->nip);
1398 return;
1401 void SPEFloatingPointRoundException(struct pt_regs *regs)
1403 extern int speround_handler(struct pt_regs *regs);
1404 int err;
1406 preempt_disable();
1407 if (regs->msr & MSR_SPE)
1408 giveup_spe(current);
1409 preempt_enable();
1411 regs->nip -= 4;
1412 err = speround_handler(regs);
1413 if (err == 0) {
1414 regs->nip += 4; /* skip emulated instruction */
1415 emulate_single_step(regs);
1416 return;
1419 if (err == -EFAULT) {
1420 /* got an error reading the instruction */
1421 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1422 } else if (err == -EINVAL) {
1423 /* didn't recognize the instruction */
1424 printk(KERN_ERR "unrecognized spe instruction "
1425 "in %s at %lx\n", current->comm, regs->nip);
1426 } else {
1427 _exception(SIGFPE, regs, 0, regs->nip);
1428 return;
1431 #endif
1434 * We enter here if we get an unrecoverable exception, that is, one
1435 * that happened at a point where the RI (recoverable interrupt) bit
1436 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1437 * we therefore lost state by taking this exception.
1439 void unrecoverable_exception(struct pt_regs *regs)
1441 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1442 regs->trap, regs->nip);
1443 die("Unrecoverable exception", regs, SIGABRT);
1446 #ifdef CONFIG_BOOKE_WDT
1448 * Default handler for a Watchdog exception,
1449 * spins until a reboot occurs
1451 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1453 /* Generic WatchdogHandler, implement your own */
1454 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1455 return;
1458 void WatchdogException(struct pt_regs *regs)
1460 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1461 WatchdogHandler(regs);
1463 #endif
1466 * We enter here if we discover during exception entry that we are
1467 * running in supervisor mode with a userspace value in the stack pointer.
1469 void kernel_bad_stack(struct pt_regs *regs)
1471 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1472 regs->gpr[1], regs->nip);
1473 die("Bad kernel stack pointer", regs, SIGABRT);
1476 void __init trap_init(void)
1481 #ifdef CONFIG_PPC_EMULATED_STATS
1483 #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1485 struct ppc_emulated ppc_emulated = {
1486 #ifdef CONFIG_ALTIVEC
1487 WARN_EMULATED_SETUP(altivec),
1488 #endif
1489 WARN_EMULATED_SETUP(dcba),
1490 WARN_EMULATED_SETUP(dcbz),
1491 WARN_EMULATED_SETUP(fp_pair),
1492 WARN_EMULATED_SETUP(isel),
1493 WARN_EMULATED_SETUP(mcrxr),
1494 WARN_EMULATED_SETUP(mfpvr),
1495 WARN_EMULATED_SETUP(multiple),
1496 WARN_EMULATED_SETUP(popcntb),
1497 WARN_EMULATED_SETUP(spe),
1498 WARN_EMULATED_SETUP(string),
1499 WARN_EMULATED_SETUP(unaligned),
1500 #ifdef CONFIG_MATH_EMULATION
1501 WARN_EMULATED_SETUP(math),
1502 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1503 WARN_EMULATED_SETUP(8xx),
1504 #endif
1505 #ifdef CONFIG_VSX
1506 WARN_EMULATED_SETUP(vsx),
1507 #endif
1510 u32 ppc_warn_emulated;
1512 void ppc_warn_emulated_print(const char *type)
1514 if (printk_ratelimit())
1515 pr_warning("%s used emulated %s instruction\n", current->comm,
1516 type);
1519 static int __init ppc_warn_emulated_init(void)
1521 struct dentry *dir, *d;
1522 unsigned int i;
1523 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1525 if (!powerpc_debugfs_root)
1526 return -ENODEV;
1528 dir = debugfs_create_dir("emulated_instructions",
1529 powerpc_debugfs_root);
1530 if (!dir)
1531 return -ENOMEM;
1533 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1534 &ppc_warn_emulated);
1535 if (!d)
1536 goto fail;
1538 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1539 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1540 (u32 *)&entries[i].val.counter);
1541 if (!d)
1542 goto fail;
1545 return 0;
1547 fail:
1548 debugfs_remove_recursive(dir);
1549 return -ENOMEM;
1552 device_initcall(ppc_warn_emulated_init);
1554 #endif /* CONFIG_PPC_EMULATED_STATS */