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>
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/extable.h>
29 #include <linux/module.h> /* print_modules */
30 #include <linux/prctl.h>
31 #include <linux/delay.h>
32 #include <linux/kprobes.h>
33 #include <linux/kexec.h>
34 #include <linux/backlight.h>
35 #include <linux/bug.h>
36 #include <linux/kdebug.h>
37 #include <linux/debugfs.h>
38 #include <linux/ratelimit.h>
39 #include <linux/context_tracking.h>
41 #include <asm/emulated_ops.h>
42 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
45 #include <asm/machdep.h>
49 #ifdef CONFIG_PMAC_BACKLIGHT
50 #include <asm/backlight.h>
53 #include <asm/firmware.h>
54 #include <asm/processor.h>
57 #include <asm/kexec.h>
58 #include <asm/ppc-opcode.h>
60 #include <asm/fadump.h>
61 #include <asm/switch_to.h>
63 #include <asm/debug.h>
64 #include <asm/asm-prototypes.h>
66 #include <sysdev/fsl_pci.h>
68 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
69 int (*__debugger)(struct pt_regs *regs) __read_mostly;
70 int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
71 int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
72 int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
73 int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
74 int (*__debugger_break_match)(struct pt_regs *regs) __read_mostly;
75 int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
77 EXPORT_SYMBOL(__debugger);
78 EXPORT_SYMBOL(__debugger_ipi);
79 EXPORT_SYMBOL(__debugger_bpt);
80 EXPORT_SYMBOL(__debugger_sstep);
81 EXPORT_SYMBOL(__debugger_iabr_match);
82 EXPORT_SYMBOL(__debugger_break_match);
83 EXPORT_SYMBOL(__debugger_fault_handler);
86 /* Transactional Memory trap debug */
88 #define TM_DEBUG(x...) printk(KERN_INFO x)
90 #define TM_DEBUG(x...) do { } while(0)
94 * Trap & Exception support
97 #ifdef CONFIG_PMAC_BACKLIGHT
98 static void pmac_backlight_unblank(void)
100 mutex_lock(&pmac_backlight_mutex);
101 if (pmac_backlight) {
102 struct backlight_properties *props;
104 props = &pmac_backlight->props;
105 props->brightness = props->max_brightness;
106 props->power = FB_BLANK_UNBLANK;
107 backlight_update_status(pmac_backlight);
109 mutex_unlock(&pmac_backlight_mutex);
112 static inline void pmac_backlight_unblank(void) { }
115 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
116 static int die_owner = -1;
117 static unsigned int die_nest_count;
118 static int die_counter;
120 static unsigned long oops_begin(struct pt_regs *regs)
127 /* racy, but better than risking deadlock. */
128 raw_local_irq_save(flags);
129 cpu = smp_processor_id();
130 if (!arch_spin_trylock(&die_lock)) {
131 if (cpu == die_owner)
132 /* nested oops. should stop eventually */;
134 arch_spin_lock(&die_lock);
140 if (machine_is(powermac))
141 pmac_backlight_unblank();
144 NOKPROBE_SYMBOL(oops_begin);
146 static void oops_end(unsigned long flags, struct pt_regs *regs,
151 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
156 /* Nest count reaches zero, release the lock. */
157 arch_spin_unlock(&die_lock);
158 raw_local_irq_restore(flags);
160 crash_fadump(regs, "die oops");
163 * A system reset (0x100) is a request to dump, so we always send
164 * it through the crashdump code.
166 if (kexec_should_crash(current) || (TRAP(regs) == 0x100)) {
170 * We aren't the primary crash CPU. We need to send it
171 * to a holding pattern to avoid it ending up in the panic
174 crash_kexec_secondary(regs);
181 * While our oops output is serialised by a spinlock, output
182 * from panic() called below can race and corrupt it. If we
183 * know we are going to panic, delay for 1 second so we have a
184 * chance to get clean backtraces from all CPUs that are oopsing.
186 if (in_interrupt() || panic_on_oops || !current->pid ||
187 is_global_init(current)) {
188 mdelay(MSEC_PER_SEC);
192 panic("Fatal exception in interrupt");
194 panic("Fatal exception");
197 NOKPROBE_SYMBOL(oops_end);
199 static int __die(const char *str, struct pt_regs *regs, long err)
201 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
202 #ifdef CONFIG_PREEMPT
206 printk("SMP NR_CPUS=%d ", NR_CPUS);
208 if (debug_pagealloc_enabled())
209 printk("DEBUG_PAGEALLOC ");
213 printk("%s\n", ppc_md.name ? ppc_md.name : "");
215 if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV) == NOTIFY_STOP)
223 NOKPROBE_SYMBOL(__die);
225 void die(const char *str, struct pt_regs *regs, long err)
232 flags = oops_begin(regs);
233 if (__die(str, regs, err))
235 oops_end(flags, regs, err);
238 void user_single_step_siginfo(struct task_struct *tsk,
239 struct pt_regs *regs, siginfo_t *info)
241 memset(info, 0, sizeof(*info));
242 info->si_signo = SIGTRAP;
243 info->si_code = TRAP_TRACE;
244 info->si_addr = (void __user *)regs->nip;
247 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
250 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
251 "at %08lx nip %08lx lr %08lx code %x\n";
252 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
253 "at %016lx nip %016lx lr %016lx code %x\n";
255 if (!user_mode(regs)) {
256 die("Exception in kernel mode", regs, signr);
260 if (show_unhandled_signals && unhandled_signal(current, signr)) {
261 printk_ratelimited(regs->msr & MSR_64BIT ? fmt64 : fmt32,
262 current->comm, current->pid, signr,
263 addr, regs->nip, regs->link, code);
266 if (arch_irqs_disabled() && !arch_irq_disabled_regs(regs))
269 current->thread.trap_nr = code;
270 memset(&info, 0, sizeof(info));
271 info.si_signo = signr;
273 info.si_addr = (void __user *) addr;
274 force_sig_info(signr, &info, current);
277 void system_reset_exception(struct pt_regs *regs)
279 /* See if any machine dependent calls */
280 if (ppc_md.system_reset_exception) {
281 if (ppc_md.system_reset_exception(regs))
285 die("System Reset", regs, SIGABRT);
287 /* Must die if the interrupt is not recoverable */
288 if (!(regs->msr & MSR_RI))
289 panic("Unrecoverable System Reset");
291 /* What should we do here? We could issue a shutdown or hard reset. */
296 * This function is called in real mode. Strictly no printk's please.
298 * regs->nip and regs->msr contains srr0 and ssr1.
300 long machine_check_early(struct pt_regs *regs)
304 __this_cpu_inc(irq_stat.mce_exceptions);
306 add_taint(TAINT_MACHINE_CHECK, LOCKDEP_NOW_UNRELIABLE);
308 if (cur_cpu_spec && cur_cpu_spec->machine_check_early)
309 handled = cur_cpu_spec->machine_check_early(regs);
313 long hmi_exception_realmode(struct pt_regs *regs)
315 __this_cpu_inc(irq_stat.hmi_exceptions);
317 wait_for_subcore_guest_exit();
319 if (ppc_md.hmi_exception_early)
320 ppc_md.hmi_exception_early(regs);
322 wait_for_tb_resync();
330 * I/O accesses can cause machine checks on powermacs.
331 * Check if the NIP corresponds to the address of a sync
332 * instruction for which there is an entry in the exception
334 * Note that the 601 only takes a machine check on TEA
335 * (transfer error ack) signal assertion, and does not
336 * set any of the top 16 bits of SRR1.
339 static inline int check_io_access(struct pt_regs *regs)
342 unsigned long msr = regs->msr;
343 const struct exception_table_entry *entry;
344 unsigned int *nip = (unsigned int *)regs->nip;
346 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
347 && (entry = search_exception_tables(regs->nip)) != NULL) {
349 * Check that it's a sync instruction, or somewhere
350 * in the twi; isync; nop sequence that inb/inw/inl uses.
351 * As the address is in the exception table
352 * we should be able to read the instr there.
353 * For the debug message, we look at the preceding
356 if (*nip == PPC_INST_NOP)
358 else if (*nip == PPC_INST_ISYNC)
360 if (*nip == PPC_INST_SYNC || (*nip >> 26) == OP_TRAP) {
364 rb = (*nip >> 11) & 0x1f;
365 printk(KERN_DEBUG "%s bad port %lx at %p\n",
366 (*nip & 0x100)? "OUT to": "IN from",
367 regs->gpr[rb] - _IO_BASE, nip);
369 regs->nip = extable_fixup(entry);
373 #endif /* CONFIG_PPC32 */
377 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
378 /* On 4xx, the reason for the machine check or program exception
380 #define get_reason(regs) ((regs)->dsisr)
381 #ifndef CONFIG_FSL_BOOKE
382 #define get_mc_reason(regs) ((regs)->dsisr)
384 #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
386 #define REASON_FP ESR_FP
387 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
388 #define REASON_PRIVILEGED ESR_PPR
389 #define REASON_TRAP ESR_PTR
391 /* single-step stuff */
392 #define single_stepping(regs) (current->thread.debug.dbcr0 & DBCR0_IC)
393 #define clear_single_step(regs) (current->thread.debug.dbcr0 &= ~DBCR0_IC)
396 /* On non-4xx, the reason for the machine check or program
397 exception is in the MSR. */
398 #define get_reason(regs) ((regs)->msr)
399 #define get_mc_reason(regs) ((regs)->msr)
400 #define REASON_TM 0x200000
401 #define REASON_FP 0x100000
402 #define REASON_ILLEGAL 0x80000
403 #define REASON_PRIVILEGED 0x40000
404 #define REASON_TRAP 0x20000
406 #define single_stepping(regs) ((regs)->msr & MSR_SE)
407 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
410 #if defined(CONFIG_4xx)
411 int machine_check_4xx(struct pt_regs *regs)
413 unsigned long reason = get_mc_reason(regs);
415 if (reason & ESR_IMCP) {
416 printk("Instruction");
417 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
420 printk(" machine check in kernel mode.\n");
425 int machine_check_440A(struct pt_regs *regs)
427 unsigned long reason = get_mc_reason(regs);
429 printk("Machine check in kernel mode.\n");
430 if (reason & ESR_IMCP){
431 printk("Instruction Synchronous Machine Check exception\n");
432 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
435 u32 mcsr = mfspr(SPRN_MCSR);
437 printk("Instruction Read PLB Error\n");
439 printk("Data Read PLB Error\n");
441 printk("Data Write PLB Error\n");
442 if (mcsr & MCSR_TLBP)
443 printk("TLB Parity Error\n");
444 if (mcsr & MCSR_ICP){
445 flush_instruction_cache();
446 printk("I-Cache Parity Error\n");
448 if (mcsr & MCSR_DCSP)
449 printk("D-Cache Search Parity Error\n");
450 if (mcsr & MCSR_DCFP)
451 printk("D-Cache Flush Parity Error\n");
452 if (mcsr & MCSR_IMPE)
453 printk("Machine Check exception is imprecise\n");
456 mtspr(SPRN_MCSR, mcsr);
461 int machine_check_47x(struct pt_regs *regs)
463 unsigned long reason = get_mc_reason(regs);
466 printk(KERN_ERR "Machine check in kernel mode.\n");
467 if (reason & ESR_IMCP) {
469 "Instruction Synchronous Machine Check exception\n");
470 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
473 mcsr = mfspr(SPRN_MCSR);
475 printk(KERN_ERR "Instruction Read PLB Error\n");
477 printk(KERN_ERR "Data Read PLB Error\n");
479 printk(KERN_ERR "Data Write PLB Error\n");
480 if (mcsr & MCSR_TLBP)
481 printk(KERN_ERR "TLB Parity Error\n");
482 if (mcsr & MCSR_ICP) {
483 flush_instruction_cache();
484 printk(KERN_ERR "I-Cache Parity Error\n");
486 if (mcsr & MCSR_DCSP)
487 printk(KERN_ERR "D-Cache Search Parity Error\n");
488 if (mcsr & PPC47x_MCSR_GPR)
489 printk(KERN_ERR "GPR Parity Error\n");
490 if (mcsr & PPC47x_MCSR_FPR)
491 printk(KERN_ERR "FPR Parity Error\n");
492 if (mcsr & PPC47x_MCSR_IPR)
493 printk(KERN_ERR "Machine Check exception is imprecise\n");
496 mtspr(SPRN_MCSR, mcsr);
500 #elif defined(CONFIG_E500)
501 int machine_check_e500mc(struct pt_regs *regs)
503 unsigned long mcsr = mfspr(SPRN_MCSR);
504 unsigned long reason = mcsr;
507 if (reason & MCSR_LD) {
508 recoverable = fsl_rio_mcheck_exception(regs);
509 if (recoverable == 1)
513 printk("Machine check in kernel mode.\n");
514 printk("Caused by (from MCSR=%lx): ", reason);
516 if (reason & MCSR_MCP)
517 printk("Machine Check Signal\n");
519 if (reason & MCSR_ICPERR) {
520 printk("Instruction Cache Parity Error\n");
523 * This is recoverable by invalidating the i-cache.
525 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
526 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
530 * This will generally be accompanied by an instruction
531 * fetch error report -- only treat MCSR_IF as fatal
532 * if it wasn't due to an L1 parity error.
537 if (reason & MCSR_DCPERR_MC) {
538 printk("Data Cache Parity Error\n");
541 * In write shadow mode we auto-recover from the error, but it
542 * may still get logged and cause a machine check. We should
543 * only treat the non-write shadow case as non-recoverable.
545 if (!(mfspr(SPRN_L1CSR2) & L1CSR2_DCWS))
549 if (reason & MCSR_L2MMU_MHIT) {
550 printk("Hit on multiple TLB entries\n");
554 if (reason & MCSR_NMI)
555 printk("Non-maskable interrupt\n");
557 if (reason & MCSR_IF) {
558 printk("Instruction Fetch Error Report\n");
562 if (reason & MCSR_LD) {
563 printk("Load Error Report\n");
567 if (reason & MCSR_ST) {
568 printk("Store Error Report\n");
572 if (reason & MCSR_LDG) {
573 printk("Guarded Load Error Report\n");
577 if (reason & MCSR_TLBSYNC)
578 printk("Simultaneous tlbsync operations\n");
580 if (reason & MCSR_BSL2_ERR) {
581 printk("Level 2 Cache Error\n");
585 if (reason & MCSR_MAV) {
588 addr = mfspr(SPRN_MCAR);
589 addr |= (u64)mfspr(SPRN_MCARU) << 32;
591 printk("Machine Check %s Address: %#llx\n",
592 reason & MCSR_MEA ? "Effective" : "Physical", addr);
596 mtspr(SPRN_MCSR, mcsr);
597 return mfspr(SPRN_MCSR) == 0 && recoverable;
600 int machine_check_e500(struct pt_regs *regs)
602 unsigned long reason = get_mc_reason(regs);
604 if (reason & MCSR_BUS_RBERR) {
605 if (fsl_rio_mcheck_exception(regs))
607 if (fsl_pci_mcheck_exception(regs))
611 printk("Machine check in kernel mode.\n");
612 printk("Caused by (from MCSR=%lx): ", reason);
614 if (reason & MCSR_MCP)
615 printk("Machine Check Signal\n");
616 if (reason & MCSR_ICPERR)
617 printk("Instruction Cache Parity Error\n");
618 if (reason & MCSR_DCP_PERR)
619 printk("Data Cache Push Parity Error\n");
620 if (reason & MCSR_DCPERR)
621 printk("Data Cache Parity Error\n");
622 if (reason & MCSR_BUS_IAERR)
623 printk("Bus - Instruction Address Error\n");
624 if (reason & MCSR_BUS_RAERR)
625 printk("Bus - Read Address Error\n");
626 if (reason & MCSR_BUS_WAERR)
627 printk("Bus - Write Address Error\n");
628 if (reason & MCSR_BUS_IBERR)
629 printk("Bus - Instruction Data Error\n");
630 if (reason & MCSR_BUS_RBERR)
631 printk("Bus - Read Data Bus Error\n");
632 if (reason & MCSR_BUS_WBERR)
633 printk("Bus - Write Data Bus Error\n");
634 if (reason & MCSR_BUS_IPERR)
635 printk("Bus - Instruction Parity Error\n");
636 if (reason & MCSR_BUS_RPERR)
637 printk("Bus - Read Parity Error\n");
642 int machine_check_generic(struct pt_regs *regs)
646 #elif defined(CONFIG_E200)
647 int machine_check_e200(struct pt_regs *regs)
649 unsigned long reason = get_mc_reason(regs);
651 printk("Machine check in kernel mode.\n");
652 printk("Caused by (from MCSR=%lx): ", reason);
654 if (reason & MCSR_MCP)
655 printk("Machine Check Signal\n");
656 if (reason & MCSR_CP_PERR)
657 printk("Cache Push Parity Error\n");
658 if (reason & MCSR_CPERR)
659 printk("Cache Parity Error\n");
660 if (reason & MCSR_EXCP_ERR)
661 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
662 if (reason & MCSR_BUS_IRERR)
663 printk("Bus - Read Bus Error on instruction fetch\n");
664 if (reason & MCSR_BUS_DRERR)
665 printk("Bus - Read Bus Error on data load\n");
666 if (reason & MCSR_BUS_WRERR)
667 printk("Bus - Write Bus Error on buffered store or cache line push\n");
671 #elif defined(CONFIG_PPC_8xx)
672 int machine_check_8xx(struct pt_regs *regs)
674 unsigned long reason = get_mc_reason(regs);
676 pr_err("Machine check in kernel mode.\n");
677 pr_err("Caused by (from SRR1=%lx): ", reason);
678 if (reason & 0x40000000)
679 pr_err("Fetch error at address %lx\n", regs->nip);
681 pr_err("Data access error at address %lx\n", regs->dar);
684 /* the qspan pci read routines can cause machine checks -- Cort
686 * yuck !!! that totally needs to go away ! There are better ways
687 * to deal with that than having a wart in the mcheck handler.
690 bad_page_fault(regs, regs->dar, SIGBUS);
697 int machine_check_generic(struct pt_regs *regs)
699 unsigned long reason = get_mc_reason(regs);
701 printk("Machine check in kernel mode.\n");
702 printk("Caused by (from SRR1=%lx): ", reason);
703 switch (reason & 0x601F0000) {
705 printk("Machine check signal\n");
707 case 0: /* for 601 */
709 case 0x140000: /* 7450 MSS error and TEA */
710 printk("Transfer error ack signal\n");
713 printk("Data parity error signal\n");
716 printk("Address parity error signal\n");
719 printk("L1 Data Cache error\n");
722 printk("L1 Instruction Cache error\n");
725 printk("L2 data cache parity error\n");
728 printk("Unknown values in msr\n");
732 #endif /* everything else */
734 void machine_check_exception(struct pt_regs *regs)
736 enum ctx_state prev_state = exception_enter();
739 __this_cpu_inc(irq_stat.mce_exceptions);
741 /* See if any machine dependent calls. In theory, we would want
742 * to call the CPU first, and call the ppc_md. one if the CPU
743 * one returns a positive number. However there is existing code
744 * that assumes the board gets a first chance, so let's keep it
745 * that way for now and fix things later. --BenH.
747 if (ppc_md.machine_check_exception)
748 recover = ppc_md.machine_check_exception(regs);
749 else if (cur_cpu_spec->machine_check)
750 recover = cur_cpu_spec->machine_check(regs);
755 if (debugger_fault_handler(regs))
758 if (check_io_access(regs))
761 die("Machine check", regs, SIGBUS);
763 /* Must die if the interrupt is not recoverable */
764 if (!(regs->msr & MSR_RI))
765 panic("Unrecoverable Machine check");
768 exception_exit(prev_state);
771 void SMIException(struct pt_regs *regs)
773 die("System Management Interrupt", regs, SIGABRT);
776 void handle_hmi_exception(struct pt_regs *regs)
778 struct pt_regs *old_regs;
780 old_regs = set_irq_regs(regs);
783 if (ppc_md.handle_hmi_exception)
784 ppc_md.handle_hmi_exception(regs);
787 set_irq_regs(old_regs);
790 void unknown_exception(struct pt_regs *regs)
792 enum ctx_state prev_state = exception_enter();
794 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
795 regs->nip, regs->msr, regs->trap);
797 _exception(SIGTRAP, regs, 0, 0);
799 exception_exit(prev_state);
802 void instruction_breakpoint_exception(struct pt_regs *regs)
804 enum ctx_state prev_state = exception_enter();
806 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
807 5, SIGTRAP) == NOTIFY_STOP)
809 if (debugger_iabr_match(regs))
811 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
814 exception_exit(prev_state);
817 void RunModeException(struct pt_regs *regs)
819 _exception(SIGTRAP, regs, 0, 0);
822 void single_step_exception(struct pt_regs *regs)
824 enum ctx_state prev_state = exception_enter();
826 clear_single_step(regs);
828 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
829 5, SIGTRAP) == NOTIFY_STOP)
831 if (debugger_sstep(regs))
834 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
837 exception_exit(prev_state);
839 NOKPROBE_SYMBOL(single_step_exception);
842 * After we have successfully emulated an instruction, we have to
843 * check if the instruction was being single-stepped, and if so,
844 * pretend we got a single-step exception. This was pointed out
845 * by Kumar Gala. -- paulus
847 static void emulate_single_step(struct pt_regs *regs)
849 if (single_stepping(regs))
850 single_step_exception(regs);
853 static inline int __parse_fpscr(unsigned long fpscr)
857 /* Invalid operation */
858 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
862 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
866 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
870 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
874 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
880 static void parse_fpe(struct pt_regs *regs)
884 flush_fp_to_thread(current);
886 code = __parse_fpscr(current->thread.fp_state.fpscr);
888 _exception(SIGFPE, regs, code, regs->nip);
892 * Illegal instruction emulation support. Originally written to
893 * provide the PVR to user applications using the mfspr rd, PVR.
894 * Return non-zero if we can't emulate, or -EFAULT if the associated
895 * memory access caused an access fault. Return zero on success.
897 * There are a couple of ways to do this, either "decode" the instruction
898 * or directly match lots of bits. In this case, matching lots of
899 * bits is faster and easier.
902 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
904 u8 rT = (instword >> 21) & 0x1f;
905 u8 rA = (instword >> 16) & 0x1f;
906 u8 NB_RB = (instword >> 11) & 0x1f;
911 /* Early out if we are an invalid form of lswx */
912 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
913 if ((rT == rA) || (rT == NB_RB))
916 EA = (rA == 0) ? 0 : regs->gpr[rA];
918 switch (instword & PPC_INST_STRING_MASK) {
922 num_bytes = regs->xer & 0x7f;
926 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
932 while (num_bytes != 0)
935 u32 shift = 8 * (3 - (pos & 0x3));
937 /* if process is 32-bit, clear upper 32 bits of EA */
938 if ((regs->msr & MSR_64BIT) == 0)
941 switch ((instword & PPC_INST_STRING_MASK)) {
944 if (get_user(val, (u8 __user *)EA))
946 /* first time updating this reg,
950 regs->gpr[rT] |= val << shift;
954 val = regs->gpr[rT] >> shift;
955 if (put_user(val, (u8 __user *)EA))
959 /* move EA to next address */
963 /* manage our position within the register */
974 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
979 ra = (instword >> 16) & 0x1f;
980 rs = (instword >> 21) & 0x1f;
983 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
984 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
985 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
991 static int emulate_isel(struct pt_regs *regs, u32 instword)
993 u8 rT = (instword >> 21) & 0x1f;
994 u8 rA = (instword >> 16) & 0x1f;
995 u8 rB = (instword >> 11) & 0x1f;
996 u8 BC = (instword >> 6) & 0x1f;
1000 tmp = (rA == 0) ? 0 : regs->gpr[rA];
1001 bit = (regs->ccr >> (31 - BC)) & 0x1;
1003 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
1008 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1009 static inline bool tm_abort_check(struct pt_regs *regs, int cause)
1011 /* If we're emulating a load/store in an active transaction, we cannot
1012 * emulate it as the kernel operates in transaction suspended context.
1013 * We need to abort the transaction. This creates a persistent TM
1014 * abort so tell the user what caused it with a new code.
1016 if (MSR_TM_TRANSACTIONAL(regs->msr)) {
1024 static inline bool tm_abort_check(struct pt_regs *regs, int reason)
1030 static int emulate_instruction(struct pt_regs *regs)
1035 if (!user_mode(regs))
1037 CHECK_FULL_REGS(regs);
1039 if (get_user(instword, (u32 __user *)(regs->nip)))
1042 /* Emulate the mfspr rD, PVR. */
1043 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
1044 PPC_WARN_EMULATED(mfpvr, regs);
1045 rd = (instword >> 21) & 0x1f;
1046 regs->gpr[rd] = mfspr(SPRN_PVR);
1050 /* Emulating the dcba insn is just a no-op. */
1051 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
1052 PPC_WARN_EMULATED(dcba, regs);
1056 /* Emulate the mcrxr insn. */
1057 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
1058 int shift = (instword >> 21) & 0x1c;
1059 unsigned long msk = 0xf0000000UL >> shift;
1061 PPC_WARN_EMULATED(mcrxr, regs);
1062 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
1063 regs->xer &= ~0xf0000000UL;
1067 /* Emulate load/store string insn. */
1068 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
1069 if (tm_abort_check(regs,
1070 TM_CAUSE_EMULATE | TM_CAUSE_PERSISTENT))
1072 PPC_WARN_EMULATED(string, regs);
1073 return emulate_string_inst(regs, instword);
1076 /* Emulate the popcntb (Population Count Bytes) instruction. */
1077 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
1078 PPC_WARN_EMULATED(popcntb, regs);
1079 return emulate_popcntb_inst(regs, instword);
1082 /* Emulate isel (Integer Select) instruction */
1083 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
1084 PPC_WARN_EMULATED(isel, regs);
1085 return emulate_isel(regs, instword);
1088 /* Emulate sync instruction variants */
1089 if ((instword & PPC_INST_SYNC_MASK) == PPC_INST_SYNC) {
1090 PPC_WARN_EMULATED(sync, regs);
1091 asm volatile("sync");
1096 /* Emulate the mfspr rD, DSCR. */
1097 if ((((instword & PPC_INST_MFSPR_DSCR_USER_MASK) ==
1098 PPC_INST_MFSPR_DSCR_USER) ||
1099 ((instword & PPC_INST_MFSPR_DSCR_MASK) ==
1100 PPC_INST_MFSPR_DSCR)) &&
1101 cpu_has_feature(CPU_FTR_DSCR)) {
1102 PPC_WARN_EMULATED(mfdscr, regs);
1103 rd = (instword >> 21) & 0x1f;
1104 regs->gpr[rd] = mfspr(SPRN_DSCR);
1107 /* Emulate the mtspr DSCR, rD. */
1108 if ((((instword & PPC_INST_MTSPR_DSCR_USER_MASK) ==
1109 PPC_INST_MTSPR_DSCR_USER) ||
1110 ((instword & PPC_INST_MTSPR_DSCR_MASK) ==
1111 PPC_INST_MTSPR_DSCR)) &&
1112 cpu_has_feature(CPU_FTR_DSCR)) {
1113 PPC_WARN_EMULATED(mtdscr, regs);
1114 rd = (instword >> 21) & 0x1f;
1115 current->thread.dscr = regs->gpr[rd];
1116 current->thread.dscr_inherit = 1;
1117 mtspr(SPRN_DSCR, current->thread.dscr);
1125 int is_valid_bugaddr(unsigned long addr)
1127 return is_kernel_addr(addr);
1130 #ifdef CONFIG_MATH_EMULATION
1131 static int emulate_math(struct pt_regs *regs)
1134 extern int do_mathemu(struct pt_regs *regs);
1136 ret = do_mathemu(regs);
1138 PPC_WARN_EMULATED(math, regs);
1142 emulate_single_step(regs);
1146 code = __parse_fpscr(current->thread.fp_state.fpscr);
1147 _exception(SIGFPE, regs, code, regs->nip);
1151 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1158 static inline int emulate_math(struct pt_regs *regs) { return -1; }
1161 void program_check_exception(struct pt_regs *regs)
1163 enum ctx_state prev_state = exception_enter();
1164 unsigned int reason = get_reason(regs);
1166 /* We can now get here via a FP Unavailable exception if the core
1167 * has no FPU, in that case the reason flags will be 0 */
1169 if (reason & REASON_FP) {
1170 /* IEEE FP exception */
1174 if (reason & REASON_TRAP) {
1175 unsigned long bugaddr;
1176 /* Debugger is first in line to stop recursive faults in
1177 * rcu_lock, notify_die, or atomic_notifier_call_chain */
1178 if (debugger_bpt(regs))
1181 /* trap exception */
1182 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
1186 bugaddr = regs->nip;
1188 * Fixup bugaddr for BUG_ON() in real mode
1190 if (!is_kernel_addr(bugaddr) && !(regs->msr & MSR_IR))
1191 bugaddr += PAGE_OFFSET;
1193 if (!(regs->msr & MSR_PR) && /* not user-mode */
1194 report_bug(bugaddr, regs) == BUG_TRAP_TYPE_WARN) {
1198 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
1201 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1202 if (reason & REASON_TM) {
1203 /* This is a TM "Bad Thing Exception" program check.
1205 * - An rfid/hrfid/mtmsrd attempts to cause an illegal
1206 * transition in TM states.
1207 * - A trechkpt is attempted when transactional.
1208 * - A treclaim is attempted when non transactional.
1209 * - A tend is illegally attempted.
1210 * - writing a TM SPR when transactional.
1212 if (!user_mode(regs) &&
1213 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
1217 /* If usermode caused this, it's done something illegal and
1218 * gets a SIGILL slap on the wrist. We call it an illegal
1219 * operand to distinguish from the instruction just being bad
1220 * (e.g. executing a 'tend' on a CPU without TM!); it's an
1221 * illegal /placement/ of a valid instruction.
1223 if (user_mode(regs)) {
1224 _exception(SIGILL, regs, ILL_ILLOPN, regs->nip);
1227 printk(KERN_EMERG "Unexpected TM Bad Thing exception "
1228 "at %lx (msr 0x%x)\n", regs->nip, reason);
1229 die("Unrecoverable exception", regs, SIGABRT);
1235 * If we took the program check in the kernel skip down to sending a
1236 * SIGILL. The subsequent cases all relate to emulating instructions
1237 * which we should only do for userspace. We also do not want to enable
1238 * interrupts for kernel faults because that might lead to further
1239 * faults, and loose the context of the original exception.
1241 if (!user_mode(regs))
1244 /* We restore the interrupt state now */
1245 if (!arch_irq_disabled_regs(regs))
1248 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
1249 * but there seems to be a hardware bug on the 405GP (RevD)
1250 * that means ESR is sometimes set incorrectly - either to
1251 * ESR_DST (!?) or 0. In the process of chasing this with the
1252 * hardware people - not sure if it can happen on any illegal
1253 * instruction or only on FP instructions, whether there is a
1254 * pattern to occurrences etc. -dgibson 31/Mar/2003
1256 if (!emulate_math(regs))
1259 /* Try to emulate it if we should. */
1260 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
1261 switch (emulate_instruction(regs)) {
1264 emulate_single_step(regs);
1267 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1273 if (reason & REASON_PRIVILEGED)
1274 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1276 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1279 exception_exit(prev_state);
1281 NOKPROBE_SYMBOL(program_check_exception);
1284 * This occurs when running in hypervisor mode on POWER6 or later
1285 * and an illegal instruction is encountered.
1287 void emulation_assist_interrupt(struct pt_regs *regs)
1289 regs->msr |= REASON_ILLEGAL;
1290 program_check_exception(regs);
1292 NOKPROBE_SYMBOL(emulation_assist_interrupt);
1294 void alignment_exception(struct pt_regs *regs)
1296 enum ctx_state prev_state = exception_enter();
1297 int sig, code, fixed = 0;
1299 /* We restore the interrupt state now */
1300 if (!arch_irq_disabled_regs(regs))
1303 if (tm_abort_check(regs, TM_CAUSE_ALIGNMENT | TM_CAUSE_PERSISTENT))
1306 /* we don't implement logging of alignment exceptions */
1307 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1308 fixed = fix_alignment(regs);
1311 regs->nip += 4; /* skip over emulated instruction */
1312 emulate_single_step(regs);
1316 /* Operand address was bad */
1317 if (fixed == -EFAULT) {
1324 if (user_mode(regs))
1325 _exception(sig, regs, code, regs->dar);
1327 bad_page_fault(regs, regs->dar, sig);
1330 exception_exit(prev_state);
1333 void slb_miss_bad_addr(struct pt_regs *regs)
1335 enum ctx_state prev_state = exception_enter();
1337 if (user_mode(regs))
1338 _exception(SIGSEGV, regs, SEGV_BNDERR, regs->dar);
1340 bad_page_fault(regs, regs->dar, SIGSEGV);
1342 exception_exit(prev_state);
1345 void StackOverflow(struct pt_regs *regs)
1347 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1348 current, regs->gpr[1]);
1351 panic("kernel stack overflow");
1354 void nonrecoverable_exception(struct pt_regs *regs)
1356 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1357 regs->nip, regs->msr);
1359 die("nonrecoverable exception", regs, SIGKILL);
1362 void kernel_fp_unavailable_exception(struct pt_regs *regs)
1364 enum ctx_state prev_state = exception_enter();
1366 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1367 "%lx at %lx\n", regs->trap, regs->nip);
1368 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1370 exception_exit(prev_state);
1373 void altivec_unavailable_exception(struct pt_regs *regs)
1375 enum ctx_state prev_state = exception_enter();
1377 if (user_mode(regs)) {
1378 /* A user program has executed an altivec instruction,
1379 but this kernel doesn't support altivec. */
1380 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1384 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1385 "%lx at %lx\n", regs->trap, regs->nip);
1386 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
1389 exception_exit(prev_state);
1392 void vsx_unavailable_exception(struct pt_regs *regs)
1394 if (user_mode(regs)) {
1395 /* A user program has executed an vsx instruction,
1396 but this kernel doesn't support vsx. */
1397 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1401 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1402 "%lx at %lx\n", regs->trap, regs->nip);
1403 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1407 static void tm_unavailable(struct pt_regs *regs)
1409 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1410 if (user_mode(regs)) {
1411 current->thread.load_tm++;
1412 regs->msr |= MSR_TM;
1414 tm_restore_sprs(¤t->thread);
1418 pr_emerg("Unrecoverable TM Unavailable Exception "
1419 "%lx at %lx\n", regs->trap, regs->nip);
1420 die("Unrecoverable TM Unavailable Exception", regs, SIGABRT);
1423 void facility_unavailable_exception(struct pt_regs *regs)
1425 static char *facility_strings[] = {
1426 [FSCR_FP_LG] = "FPU",
1427 [FSCR_VECVSX_LG] = "VMX/VSX",
1428 [FSCR_DSCR_LG] = "DSCR",
1429 [FSCR_PM_LG] = "PMU SPRs",
1430 [FSCR_BHRB_LG] = "BHRB",
1431 [FSCR_TM_LG] = "TM",
1432 [FSCR_EBB_LG] = "EBB",
1433 [FSCR_TAR_LG] = "TAR",
1435 char *facility = "unknown";
1441 hv = (regs->trap == 0xf80);
1443 value = mfspr(SPRN_HFSCR);
1445 value = mfspr(SPRN_FSCR);
1447 status = value >> 56;
1448 if (status == FSCR_DSCR_LG) {
1450 * User is accessing the DSCR register using the problem
1451 * state only SPR number (0x03) either through a mfspr or
1452 * a mtspr instruction. If it is a write attempt through
1453 * a mtspr, then we set the inherit bit. This also allows
1454 * the user to write or read the register directly in the
1455 * future by setting via the FSCR DSCR bit. But in case it
1456 * is a read DSCR attempt through a mfspr instruction, we
1457 * just emulate the instruction instead. This code path will
1458 * always emulate all the mfspr instructions till the user
1459 * has attempted at least one mtspr instruction. This way it
1460 * preserves the same behaviour when the user is accessing
1461 * the DSCR through privilege level only SPR number (0x11)
1462 * which is emulated through illegal instruction exception.
1463 * We always leave HFSCR DSCR set.
1465 if (get_user(instword, (u32 __user *)(regs->nip))) {
1466 pr_err("Failed to fetch the user instruction\n");
1470 /* Write into DSCR (mtspr 0x03, RS) */
1471 if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
1472 == PPC_INST_MTSPR_DSCR_USER) {
1473 rd = (instword >> 21) & 0x1f;
1474 current->thread.dscr = regs->gpr[rd];
1475 current->thread.dscr_inherit = 1;
1476 current->thread.fscr |= FSCR_DSCR;
1477 mtspr(SPRN_FSCR, current->thread.fscr);
1480 /* Read from DSCR (mfspr RT, 0x03) */
1481 if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
1482 == PPC_INST_MFSPR_DSCR_USER) {
1483 if (emulate_instruction(regs)) {
1484 pr_err("DSCR based mfspr emulation failed\n");
1488 emulate_single_step(regs);
1493 if (status == FSCR_TM_LG) {
1495 * If we're here then the hardware is TM aware because it
1496 * generated an exception with FSRM_TM set.
1498 * If cpu_has_feature(CPU_FTR_TM) is false, then either firmware
1499 * told us not to do TM, or the kernel is not built with TM
1502 * If both of those things are true, then userspace can spam the
1503 * console by triggering the printk() below just by continually
1504 * doing tbegin (or any TM instruction). So in that case just
1505 * send the process a SIGILL immediately.
1507 if (!cpu_has_feature(CPU_FTR_TM))
1510 tm_unavailable(regs);
1514 if ((status < ARRAY_SIZE(facility_strings)) &&
1515 facility_strings[status])
1516 facility = facility_strings[status];
1518 /* We restore the interrupt state now */
1519 if (!arch_irq_disabled_regs(regs))
1523 "%sFacility '%s' unavailable, exception at 0x%lx, MSR=%lx\n",
1524 hv ? "Hypervisor " : "", facility, regs->nip, regs->msr);
1527 if (user_mode(regs)) {
1528 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1532 die("Unexpected facility unavailable exception", regs, SIGABRT);
1536 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1538 void fp_unavailable_tm(struct pt_regs *regs)
1540 /* Note: This does not handle any kind of FP laziness. */
1542 TM_DEBUG("FP Unavailable trap whilst transactional at 0x%lx, MSR=%lx\n",
1543 regs->nip, regs->msr);
1545 /* We can only have got here if the task started using FP after
1546 * beginning the transaction. So, the transactional regs are just a
1547 * copy of the checkpointed ones. But, we still need to recheckpoint
1548 * as we're enabling FP for the process; it will return, abort the
1549 * transaction, and probably retry but now with FP enabled. So the
1550 * checkpointed FP registers need to be loaded.
1552 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1553 /* Reclaim didn't save out any FPRs to transact_fprs. */
1555 /* Enable FP for the task: */
1556 regs->msr |= (MSR_FP | current->thread.fpexc_mode);
1558 /* This loads and recheckpoints the FP registers from
1559 * thread.fpr[]. They will remain in registers after the
1560 * checkpoint so we don't need to reload them after.
1561 * If VMX is in use, the VRs now hold checkpointed values,
1562 * so we don't want to load the VRs from the thread_struct.
1564 tm_recheckpoint(¤t->thread, MSR_FP);
1566 /* If VMX is in use, get the transactional values back */
1567 if (regs->msr & MSR_VEC) {
1568 msr_check_and_set(MSR_VEC);
1569 load_vr_state(¤t->thread.vr_state);
1570 /* At this point all the VSX state is loaded, so enable it */
1571 regs->msr |= MSR_VSX;
1575 void altivec_unavailable_tm(struct pt_regs *regs)
1577 /* See the comments in fp_unavailable_tm(). This function operates
1581 TM_DEBUG("Vector Unavailable trap whilst transactional at 0x%lx,"
1583 regs->nip, regs->msr);
1584 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1585 regs->msr |= MSR_VEC;
1586 tm_recheckpoint(¤t->thread, MSR_VEC);
1587 current->thread.used_vr = 1;
1589 if (regs->msr & MSR_FP) {
1590 msr_check_and_set(MSR_FP);
1591 load_fp_state(¤t->thread.fp_state);
1592 regs->msr |= MSR_VSX;
1596 void vsx_unavailable_tm(struct pt_regs *regs)
1598 unsigned long orig_msr = regs->msr;
1600 /* See the comments in fp_unavailable_tm(). This works similarly,
1601 * though we're loading both FP and VEC registers in here.
1603 * If FP isn't in use, load FP regs. If VEC isn't in use, load VEC
1604 * regs. Either way, set MSR_VSX.
1607 TM_DEBUG("VSX Unavailable trap whilst transactional at 0x%lx,"
1609 regs->nip, regs->msr);
1611 current->thread.used_vsr = 1;
1613 /* If FP and VMX are already loaded, we have all the state we need */
1614 if ((orig_msr & (MSR_FP | MSR_VEC)) == (MSR_FP | MSR_VEC)) {
1615 regs->msr |= MSR_VSX;
1619 /* This reclaims FP and/or VR regs if they're already enabled */
1620 tm_reclaim_current(TM_CAUSE_FAC_UNAV);
1622 regs->msr |= MSR_VEC | MSR_FP | current->thread.fpexc_mode |
1625 /* This loads & recheckpoints FP and VRs; but we have
1626 * to be sure not to overwrite previously-valid state.
1628 tm_recheckpoint(¤t->thread, regs->msr & ~orig_msr);
1630 msr_check_and_set(orig_msr & (MSR_FP | MSR_VEC));
1632 if (orig_msr & MSR_FP)
1633 load_fp_state(¤t->thread.fp_state);
1634 if (orig_msr & MSR_VEC)
1635 load_vr_state(¤t->thread.vr_state);
1637 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
1639 void performance_monitor_exception(struct pt_regs *regs)
1641 __this_cpu_inc(irq_stat.pmu_irqs);
1647 void SoftwareEmulation(struct pt_regs *regs)
1649 CHECK_FULL_REGS(regs);
1651 if (!user_mode(regs)) {
1653 die("Kernel Mode Unimplemented Instruction or SW FPU Emulation",
1657 if (!emulate_math(regs))
1660 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1662 #endif /* CONFIG_8xx */
1664 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
1665 static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1669 * Determine the cause of the debug event, clear the
1670 * event flags and send a trap to the handler. Torez
1672 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1673 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1674 #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1675 current->thread.debug.dbcr2 &= ~DBCR2_DAC12MODE;
1677 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1680 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1681 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1682 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1685 } else if (debug_status & DBSR_IAC1) {
1686 current->thread.debug.dbcr0 &= ~DBCR0_IAC1;
1687 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1688 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1691 } else if (debug_status & DBSR_IAC2) {
1692 current->thread.debug.dbcr0 &= ~DBCR0_IAC2;
1693 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1696 } else if (debug_status & DBSR_IAC3) {
1697 current->thread.debug.dbcr0 &= ~DBCR0_IAC3;
1698 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1699 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1702 } else if (debug_status & DBSR_IAC4) {
1703 current->thread.debug.dbcr0 &= ~DBCR0_IAC4;
1704 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1709 * At the point this routine was called, the MSR(DE) was turned off.
1710 * Check all other debug flags and see if that bit needs to be turned
1713 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1714 current->thread.debug.dbcr1))
1715 regs->msr |= MSR_DE;
1717 /* Make sure the IDM flag is off */
1718 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1721 mtspr(SPRN_DBCR0, current->thread.debug.dbcr0);
1724 void DebugException(struct pt_regs *regs, unsigned long debug_status)
1726 current->thread.debug.dbsr = debug_status;
1728 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1729 * on server, it stops on the target of the branch. In order to simulate
1730 * the server behaviour, we thus restart right away with a single step
1731 * instead of stopping here when hitting a BT
1733 if (debug_status & DBSR_BT) {
1734 regs->msr &= ~MSR_DE;
1737 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1738 /* Clear the BT event */
1739 mtspr(SPRN_DBSR, DBSR_BT);
1741 /* Do the single step trick only when coming from userspace */
1742 if (user_mode(regs)) {
1743 current->thread.debug.dbcr0 &= ~DBCR0_BT;
1744 current->thread.debug.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1745 regs->msr |= MSR_DE;
1749 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1750 5, SIGTRAP) == NOTIFY_STOP) {
1753 if (debugger_sstep(regs))
1755 } else if (debug_status & DBSR_IC) { /* Instruction complete */
1756 regs->msr &= ~MSR_DE;
1758 /* Disable instruction completion */
1759 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1760 /* Clear the instruction completion event */
1761 mtspr(SPRN_DBSR, DBSR_IC);
1763 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1764 5, SIGTRAP) == NOTIFY_STOP) {
1768 if (debugger_sstep(regs))
1771 if (user_mode(regs)) {
1772 current->thread.debug.dbcr0 &= ~DBCR0_IC;
1773 if (DBCR_ACTIVE_EVENTS(current->thread.debug.dbcr0,
1774 current->thread.debug.dbcr1))
1775 regs->msr |= MSR_DE;
1777 /* Make sure the IDM bit is off */
1778 current->thread.debug.dbcr0 &= ~DBCR0_IDM;
1781 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1783 handle_debug(regs, debug_status);
1785 NOKPROBE_SYMBOL(DebugException);
1786 #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
1788 #if !defined(CONFIG_TAU_INT)
1789 void TAUException(struct pt_regs *regs)
1791 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1792 regs->nip, regs->msr, regs->trap, print_tainted());
1794 #endif /* CONFIG_INT_TAU */
1796 #ifdef CONFIG_ALTIVEC
1797 void altivec_assist_exception(struct pt_regs *regs)
1801 if (!user_mode(regs)) {
1802 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1803 " at %lx\n", regs->nip);
1804 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1807 flush_altivec_to_thread(current);
1809 PPC_WARN_EMULATED(altivec, regs);
1810 err = emulate_altivec(regs);
1812 regs->nip += 4; /* skip emulated instruction */
1813 emulate_single_step(regs);
1817 if (err == -EFAULT) {
1818 /* got an error reading the instruction */
1819 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1821 /* didn't recognize the instruction */
1822 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1823 printk_ratelimited(KERN_ERR "Unrecognized altivec instruction "
1824 "in %s at %lx\n", current->comm, regs->nip);
1825 current->thread.vr_state.vscr.u[3] |= 0x10000;
1828 #endif /* CONFIG_ALTIVEC */
1830 #ifdef CONFIG_FSL_BOOKE
1831 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1832 unsigned long error_code)
1834 /* We treat cache locking instructions from the user
1835 * as priv ops, in the future we could try to do
1838 if (error_code & (ESR_DLK|ESR_ILK))
1839 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1842 #endif /* CONFIG_FSL_BOOKE */
1845 void SPEFloatingPointException(struct pt_regs *regs)
1847 extern int do_spe_mathemu(struct pt_regs *regs);
1848 unsigned long spefscr;
1853 flush_spe_to_thread(current);
1855 spefscr = current->thread.spefscr;
1856 fpexc_mode = current->thread.fpexc_mode;
1858 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1861 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1864 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1866 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1869 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1872 err = do_spe_mathemu(regs);
1874 regs->nip += 4; /* skip emulated instruction */
1875 emulate_single_step(regs);
1879 if (err == -EFAULT) {
1880 /* got an error reading the instruction */
1881 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1882 } else if (err == -EINVAL) {
1883 /* didn't recognize the instruction */
1884 printk(KERN_ERR "unrecognized spe instruction "
1885 "in %s at %lx\n", current->comm, regs->nip);
1887 _exception(SIGFPE, regs, code, regs->nip);
1893 void SPEFloatingPointRoundException(struct pt_regs *regs)
1895 extern int speround_handler(struct pt_regs *regs);
1899 if (regs->msr & MSR_SPE)
1900 giveup_spe(current);
1904 err = speround_handler(regs);
1906 regs->nip += 4; /* skip emulated instruction */
1907 emulate_single_step(regs);
1911 if (err == -EFAULT) {
1912 /* got an error reading the instruction */
1913 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1914 } else if (err == -EINVAL) {
1915 /* didn't recognize the instruction */
1916 printk(KERN_ERR "unrecognized spe instruction "
1917 "in %s at %lx\n", current->comm, regs->nip);
1919 _exception(SIGFPE, regs, 0, regs->nip);
1926 * We enter here if we get an unrecoverable exception, that is, one
1927 * that happened at a point where the RI (recoverable interrupt) bit
1928 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1929 * we therefore lost state by taking this exception.
1931 void unrecoverable_exception(struct pt_regs *regs)
1933 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1934 regs->trap, regs->nip);
1935 die("Unrecoverable exception", regs, SIGABRT);
1938 #if defined(CONFIG_BOOKE_WDT) || defined(CONFIG_40x)
1940 * Default handler for a Watchdog exception,
1941 * spins until a reboot occurs
1943 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1945 /* Generic WatchdogHandler, implement your own */
1946 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1950 void WatchdogException(struct pt_regs *regs)
1952 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1953 WatchdogHandler(regs);
1958 * We enter here if we discover during exception entry that we are
1959 * running in supervisor mode with a userspace value in the stack pointer.
1961 void kernel_bad_stack(struct pt_regs *regs)
1963 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1964 regs->gpr[1], regs->nip);
1965 die("Bad kernel stack pointer", regs, SIGABRT);
1968 void __init trap_init(void)
1973 #ifdef CONFIG_PPC_EMULATED_STATS
1975 #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1977 struct ppc_emulated ppc_emulated = {
1978 #ifdef CONFIG_ALTIVEC
1979 WARN_EMULATED_SETUP(altivec),
1981 WARN_EMULATED_SETUP(dcba),
1982 WARN_EMULATED_SETUP(dcbz),
1983 WARN_EMULATED_SETUP(fp_pair),
1984 WARN_EMULATED_SETUP(isel),
1985 WARN_EMULATED_SETUP(mcrxr),
1986 WARN_EMULATED_SETUP(mfpvr),
1987 WARN_EMULATED_SETUP(multiple),
1988 WARN_EMULATED_SETUP(popcntb),
1989 WARN_EMULATED_SETUP(spe),
1990 WARN_EMULATED_SETUP(string),
1991 WARN_EMULATED_SETUP(sync),
1992 WARN_EMULATED_SETUP(unaligned),
1993 #ifdef CONFIG_MATH_EMULATION
1994 WARN_EMULATED_SETUP(math),
1997 WARN_EMULATED_SETUP(vsx),
2000 WARN_EMULATED_SETUP(mfdscr),
2001 WARN_EMULATED_SETUP(mtdscr),
2002 WARN_EMULATED_SETUP(lq_stq),
2006 u32 ppc_warn_emulated;
2008 void ppc_warn_emulated_print(const char *type)
2010 pr_warn_ratelimited("%s used emulated %s instruction\n", current->comm,
2014 static int __init ppc_warn_emulated_init(void)
2016 struct dentry *dir, *d;
2018 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
2020 if (!powerpc_debugfs_root)
2023 dir = debugfs_create_dir("emulated_instructions",
2024 powerpc_debugfs_root);
2028 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
2029 &ppc_warn_emulated);
2033 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
2034 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
2035 (u32 *)&entries[i].val.counter);
2043 debugfs_remove_recursive(dir);
2047 device_initcall(ppc_warn_emulated_init);
2049 #endif /* CONFIG_PPC_EMULATED_STATS */