2 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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.
9 * Modified by Cort Dougan (cort@cs.nmt.edu)
10 * and Paul Mackerras (paulus@samba.org)
14 * This file handles the architecture-dependent parts of hardware exceptions
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/slab.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>
43 #include <asm/machdep.h>
49 #ifdef CONFIG_PMAC_BACKLIGHT
50 #include <asm/backlight.h>
53 #include <asm/firmware.h>
54 #include <asm/processor.h>
56 #include <asm/kexec.h>
57 #include <asm/ppc-opcode.h>
58 #ifdef CONFIG_FSL_BOOKE
59 #include <asm/dbell.h>
62 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
63 int (*__debugger)(struct pt_regs *regs);
64 int (*__debugger_ipi)(struct pt_regs *regs);
65 int (*__debugger_bpt)(struct pt_regs *regs);
66 int (*__debugger_sstep)(struct pt_regs *regs);
67 int (*__debugger_iabr_match)(struct pt_regs *regs);
68 int (*__debugger_dabr_match)(struct pt_regs *regs);
69 int (*__debugger_fault_handler)(struct pt_regs *regs);
71 EXPORT_SYMBOL(__debugger);
72 EXPORT_SYMBOL(__debugger_ipi);
73 EXPORT_SYMBOL(__debugger_bpt);
74 EXPORT_SYMBOL(__debugger_sstep);
75 EXPORT_SYMBOL(__debugger_iabr_match);
76 EXPORT_SYMBOL(__debugger_dabr_match);
77 EXPORT_SYMBOL(__debugger_fault_handler);
81 * Trap & Exception support
84 #ifdef CONFIG_PMAC_BACKLIGHT
85 static void pmac_backlight_unblank(void)
87 mutex_lock(&pmac_backlight_mutex);
89 struct backlight_properties *props;
91 props = &pmac_backlight->props;
92 props->brightness = props->max_brightness;
93 props->power = FB_BLANK_UNBLANK;
94 backlight_update_status(pmac_backlight);
96 mutex_unlock(&pmac_backlight_mutex);
99 static inline void pmac_backlight_unblank(void) { }
102 int die(const char *str, struct pt_regs *regs, long err)
107 int lock_owner_depth;
109 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
111 .lock_owner_depth = 0
113 static int die_counter;
121 if (die.lock_owner != raw_smp_processor_id()) {
123 spin_lock_irqsave(&die.lock, flags);
124 die.lock_owner = smp_processor_id();
125 die.lock_owner_depth = 0;
127 if (machine_is(powermac))
128 pmac_backlight_unblank();
130 local_save_flags(flags);
133 if (++die.lock_owner_depth < 3) {
134 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
135 #ifdef CONFIG_PREEMPT
139 printk("SMP NR_CPUS=%d ", NR_CPUS);
141 #ifdef CONFIG_DEBUG_PAGEALLOC
142 printk("DEBUG_PAGEALLOC ");
147 printk("%s\n", ppc_md.name ? ppc_md.name : "");
152 printk("Recursive die() failure, output suppressed\n");
157 add_taint(TAINT_DIE);
158 spin_unlock_irqrestore(&die.lock, flags);
160 if (kexec_should_crash(current) ||
161 kexec_sr_activated(smp_processor_id()))
163 crash_kexec_secondary(regs);
166 panic("Fatal exception in interrupt");
169 panic("Fatal exception");
177 void user_single_step_siginfo(struct task_struct *tsk,
178 struct pt_regs *regs, siginfo_t *info)
180 memset(info, 0, sizeof(*info));
181 info->si_signo = SIGTRAP;
182 info->si_code = TRAP_TRACE;
183 info->si_addr = (void __user *)regs->nip;
186 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
189 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
190 "at %08lx nip %08lx lr %08lx code %x\n";
191 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
192 "at %016lx nip %016lx lr %016lx code %x\n";
194 if (!user_mode(regs)) {
195 if (die("Exception in kernel mode", regs, signr))
197 } else if (show_unhandled_signals &&
198 unhandled_signal(current, signr) &&
199 printk_ratelimit()) {
200 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
201 current->comm, current->pid, signr,
202 addr, regs->nip, regs->link, code);
205 memset(&info, 0, sizeof(info));
206 info.si_signo = signr;
208 info.si_addr = (void __user *) addr;
209 force_sig_info(signr, &info, current);
213 void system_reset_exception(struct pt_regs *regs)
215 /* See if any machine dependent calls */
216 if (ppc_md.system_reset_exception) {
217 if (ppc_md.system_reset_exception(regs))
222 cpu_set(smp_processor_id(), cpus_in_sr);
225 die("System Reset", regs, SIGABRT);
228 * Some CPUs when released from the debugger will execute this path.
229 * These CPUs entered the debugger via a soft-reset. If the CPU was
230 * hung before entering the debugger it will return to the hung
231 * state when exiting this function. This causes a problem in
232 * kdump since the hung CPU(s) will not respond to the IPI sent
233 * from kdump. To prevent the problem we call crash_kexec_secondary()
234 * here. If a kdump had not been initiated or we exit the debugger
235 * with the "exit and recover" command (x) crash_kexec_secondary()
236 * will return after 5ms and the CPU returns to its previous state.
238 crash_kexec_secondary(regs);
240 /* Must die if the interrupt is not recoverable */
241 if (!(regs->msr & MSR_RI))
242 panic("Unrecoverable System Reset");
244 /* What should we do here? We could issue a shutdown or hard reset. */
249 * I/O accesses can cause machine checks on powermacs.
250 * Check if the NIP corresponds to the address of a sync
251 * instruction for which there is an entry in the exception
253 * Note that the 601 only takes a machine check on TEA
254 * (transfer error ack) signal assertion, and does not
255 * set any of the top 16 bits of SRR1.
258 static inline int check_io_access(struct pt_regs *regs)
261 unsigned long msr = regs->msr;
262 const struct exception_table_entry *entry;
263 unsigned int *nip = (unsigned int *)regs->nip;
265 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
266 && (entry = search_exception_tables(regs->nip)) != NULL) {
268 * Check that it's a sync instruction, or somewhere
269 * in the twi; isync; nop sequence that inb/inw/inl uses.
270 * As the address is in the exception table
271 * we should be able to read the instr there.
272 * For the debug message, we look at the preceding
275 if (*nip == 0x60000000) /* nop */
277 else if (*nip == 0x4c00012c) /* isync */
279 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
284 rb = (*nip >> 11) & 0x1f;
285 printk(KERN_DEBUG "%s bad port %lx at %p\n",
286 (*nip & 0x100)? "OUT to": "IN from",
287 regs->gpr[rb] - _IO_BASE, nip);
289 regs->nip = entry->fixup;
293 #endif /* CONFIG_PPC32 */
297 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
298 /* On 4xx, the reason for the machine check or program exception
300 #define get_reason(regs) ((regs)->dsisr)
301 #ifndef CONFIG_FSL_BOOKE
302 #define get_mc_reason(regs) ((regs)->dsisr)
304 #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
306 #define REASON_FP ESR_FP
307 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
308 #define REASON_PRIVILEGED ESR_PPR
309 #define REASON_TRAP ESR_PTR
311 /* single-step stuff */
312 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
313 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
316 /* On non-4xx, the reason for the machine check or program
317 exception is in the MSR. */
318 #define get_reason(regs) ((regs)->msr)
319 #define get_mc_reason(regs) ((regs)->msr)
320 #define REASON_FP 0x100000
321 #define REASON_ILLEGAL 0x80000
322 #define REASON_PRIVILEGED 0x40000
323 #define REASON_TRAP 0x20000
325 #define single_stepping(regs) ((regs)->msr & MSR_SE)
326 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
329 #if defined(CONFIG_4xx)
330 int machine_check_4xx(struct pt_regs *regs)
332 unsigned long reason = get_mc_reason(regs);
334 if (reason & ESR_IMCP) {
335 printk("Instruction");
336 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
339 printk(" machine check in kernel mode.\n");
344 int machine_check_440A(struct pt_regs *regs)
346 unsigned long reason = get_mc_reason(regs);
348 printk("Machine check in kernel mode.\n");
349 if (reason & ESR_IMCP){
350 printk("Instruction Synchronous Machine Check exception\n");
351 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
354 u32 mcsr = mfspr(SPRN_MCSR);
356 printk("Instruction Read PLB Error\n");
358 printk("Data Read PLB Error\n");
360 printk("Data Write PLB Error\n");
361 if (mcsr & MCSR_TLBP)
362 printk("TLB Parity Error\n");
363 if (mcsr & MCSR_ICP){
364 flush_instruction_cache();
365 printk("I-Cache Parity Error\n");
367 if (mcsr & MCSR_DCSP)
368 printk("D-Cache Search Parity Error\n");
369 if (mcsr & MCSR_DCFP)
370 printk("D-Cache Flush Parity Error\n");
371 if (mcsr & MCSR_IMPE)
372 printk("Machine Check exception is imprecise\n");
375 mtspr(SPRN_MCSR, mcsr);
379 #elif defined(CONFIG_E500)
380 int machine_check_e500(struct pt_regs *regs)
382 unsigned long reason = get_mc_reason(regs);
384 printk("Machine check in kernel mode.\n");
385 printk("Caused by (from MCSR=%lx): ", reason);
387 if (reason & MCSR_MCP)
388 printk("Machine Check Signal\n");
389 if (reason & MCSR_ICPERR)
390 printk("Instruction Cache Parity Error\n");
391 if (reason & MCSR_DCP_PERR)
392 printk("Data Cache Push Parity Error\n");
393 if (reason & MCSR_DCPERR)
394 printk("Data Cache Parity Error\n");
395 if (reason & MCSR_BUS_IAERR)
396 printk("Bus - Instruction Address Error\n");
397 if (reason & MCSR_BUS_RAERR)
398 printk("Bus - Read Address Error\n");
399 if (reason & MCSR_BUS_WAERR)
400 printk("Bus - Write Address Error\n");
401 if (reason & MCSR_BUS_IBERR)
402 printk("Bus - Instruction Data Error\n");
403 if (reason & MCSR_BUS_RBERR)
404 printk("Bus - Read Data Bus Error\n");
405 if (reason & MCSR_BUS_WBERR)
406 printk("Bus - Read Data Bus Error\n");
407 if (reason & MCSR_BUS_IPERR)
408 printk("Bus - Instruction Parity Error\n");
409 if (reason & MCSR_BUS_RPERR)
410 printk("Bus - Read Parity Error\n");
414 #elif defined(CONFIG_E200)
415 int machine_check_e200(struct pt_regs *regs)
417 unsigned long reason = get_mc_reason(regs);
419 printk("Machine check in kernel mode.\n");
420 printk("Caused by (from MCSR=%lx): ", reason);
422 if (reason & MCSR_MCP)
423 printk("Machine Check Signal\n");
424 if (reason & MCSR_CP_PERR)
425 printk("Cache Push Parity Error\n");
426 if (reason & MCSR_CPERR)
427 printk("Cache Parity Error\n");
428 if (reason & MCSR_EXCP_ERR)
429 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
430 if (reason & MCSR_BUS_IRERR)
431 printk("Bus - Read Bus Error on instruction fetch\n");
432 if (reason & MCSR_BUS_DRERR)
433 printk("Bus - Read Bus Error on data load\n");
434 if (reason & MCSR_BUS_WRERR)
435 printk("Bus - Write Bus Error on buffered store or cache line push\n");
440 int machine_check_generic(struct pt_regs *regs)
442 unsigned long reason = get_mc_reason(regs);
444 printk("Machine check in kernel mode.\n");
445 printk("Caused by (from SRR1=%lx): ", reason);
446 switch (reason & 0x601F0000) {
448 printk("Machine check signal\n");
450 case 0: /* for 601 */
452 case 0x140000: /* 7450 MSS error and TEA */
453 printk("Transfer error ack signal\n");
456 printk("Data parity error signal\n");
459 printk("Address parity error signal\n");
462 printk("L1 Data Cache error\n");
465 printk("L1 Instruction Cache error\n");
468 printk("L2 data cache parity error\n");
471 printk("Unknown values in msr\n");
475 #endif /* everything else */
477 void machine_check_exception(struct pt_regs *regs)
481 /* See if any machine dependent calls. In theory, we would want
482 * to call the CPU first, and call the ppc_md. one if the CPU
483 * one returns a positive number. However there is existing code
484 * that assumes the board gets a first chance, so let's keep it
485 * that way for now and fix things later. --BenH.
487 if (ppc_md.machine_check_exception)
488 recover = ppc_md.machine_check_exception(regs);
489 else if (cur_cpu_spec->machine_check)
490 recover = cur_cpu_spec->machine_check(regs);
495 if (user_mode(regs)) {
497 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
501 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
502 /* the qspan pci read routines can cause machine checks -- Cort
504 * yuck !!! that totally needs to go away ! There are better ways
505 * to deal with that than having a wart in the mcheck handler.
508 bad_page_fault(regs, regs->dar, SIGBUS);
512 if (debugger_fault_handler(regs)) {
517 if (check_io_access(regs))
520 if (debugger_fault_handler(regs))
522 die("Machine check", regs, SIGBUS);
524 /* Must die if the interrupt is not recoverable */
525 if (!(regs->msr & MSR_RI))
526 panic("Unrecoverable Machine check");
529 void SMIException(struct pt_regs *regs)
531 die("System Management Interrupt", regs, SIGABRT);
534 void unknown_exception(struct pt_regs *regs)
536 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
537 regs->nip, regs->msr, regs->trap);
539 _exception(SIGTRAP, regs, 0, 0);
542 void instruction_breakpoint_exception(struct pt_regs *regs)
544 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
545 5, SIGTRAP) == NOTIFY_STOP)
547 if (debugger_iabr_match(regs))
549 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
552 void RunModeException(struct pt_regs *regs)
554 _exception(SIGTRAP, regs, 0, 0);
557 void __kprobes single_step_exception(struct pt_regs *regs)
559 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
561 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
562 5, SIGTRAP) == NOTIFY_STOP)
564 if (debugger_sstep(regs))
567 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
571 * After we have successfully emulated an instruction, we have to
572 * check if the instruction was being single-stepped, and if so,
573 * pretend we got a single-step exception. This was pointed out
574 * by Kumar Gala. -- paulus
576 static void emulate_single_step(struct pt_regs *regs)
578 if (single_stepping(regs)) {
579 clear_single_step(regs);
580 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
584 static inline int __parse_fpscr(unsigned long fpscr)
588 /* Invalid operation */
589 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
593 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
597 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
601 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
605 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
611 static void parse_fpe(struct pt_regs *regs)
615 flush_fp_to_thread(current);
617 code = __parse_fpscr(current->thread.fpscr.val);
619 _exception(SIGFPE, regs, code, regs->nip);
623 * Illegal instruction emulation support. Originally written to
624 * provide the PVR to user applications using the mfspr rd, PVR.
625 * Return non-zero if we can't emulate, or -EFAULT if the associated
626 * memory access caused an access fault. Return zero on success.
628 * There are a couple of ways to do this, either "decode" the instruction
629 * or directly match lots of bits. In this case, matching lots of
630 * bits is faster and easier.
633 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
635 u8 rT = (instword >> 21) & 0x1f;
636 u8 rA = (instword >> 16) & 0x1f;
637 u8 NB_RB = (instword >> 11) & 0x1f;
642 /* Early out if we are an invalid form of lswx */
643 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
644 if ((rT == rA) || (rT == NB_RB))
647 EA = (rA == 0) ? 0 : regs->gpr[rA];
649 switch (instword & PPC_INST_STRING_MASK) {
653 num_bytes = regs->xer & 0x7f;
657 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
663 while (num_bytes != 0)
666 u32 shift = 8 * (3 - (pos & 0x3));
668 switch ((instword & PPC_INST_STRING_MASK)) {
671 if (get_user(val, (u8 __user *)EA))
673 /* first time updating this reg,
677 regs->gpr[rT] |= val << shift;
681 val = regs->gpr[rT] >> shift;
682 if (put_user(val, (u8 __user *)EA))
686 /* move EA to next address */
690 /* manage our position within the register */
701 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
706 ra = (instword >> 16) & 0x1f;
707 rs = (instword >> 21) & 0x1f;
710 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
711 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
712 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
718 static int emulate_isel(struct pt_regs *regs, u32 instword)
720 u8 rT = (instword >> 21) & 0x1f;
721 u8 rA = (instword >> 16) & 0x1f;
722 u8 rB = (instword >> 11) & 0x1f;
723 u8 BC = (instword >> 6) & 0x1f;
727 tmp = (rA == 0) ? 0 : regs->gpr[rA];
728 bit = (regs->ccr >> (31 - BC)) & 0x1;
730 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
735 static int emulate_instruction(struct pt_regs *regs)
740 if (!user_mode(regs) || (regs->msr & MSR_LE))
742 CHECK_FULL_REGS(regs);
744 if (get_user(instword, (u32 __user *)(regs->nip)))
747 /* Emulate the mfspr rD, PVR. */
748 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
749 PPC_WARN_EMULATED(mfpvr, regs);
750 rd = (instword >> 21) & 0x1f;
751 regs->gpr[rd] = mfspr(SPRN_PVR);
755 /* Emulating the dcba insn is just a no-op. */
756 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
757 PPC_WARN_EMULATED(dcba, regs);
761 /* Emulate the mcrxr insn. */
762 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
763 int shift = (instword >> 21) & 0x1c;
764 unsigned long msk = 0xf0000000UL >> shift;
766 PPC_WARN_EMULATED(mcrxr, regs);
767 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
768 regs->xer &= ~0xf0000000UL;
772 /* Emulate load/store string insn. */
773 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
774 PPC_WARN_EMULATED(string, regs);
775 return emulate_string_inst(regs, instword);
778 /* Emulate the popcntb (Population Count Bytes) instruction. */
779 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
780 PPC_WARN_EMULATED(popcntb, regs);
781 return emulate_popcntb_inst(regs, instword);
784 /* Emulate isel (Integer Select) instruction */
785 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
786 PPC_WARN_EMULATED(isel, regs);
787 return emulate_isel(regs, instword);
793 int is_valid_bugaddr(unsigned long addr)
795 return is_kernel_addr(addr);
798 void __kprobes program_check_exception(struct pt_regs *regs)
800 unsigned int reason = get_reason(regs);
801 extern int do_mathemu(struct pt_regs *regs);
803 /* We can now get here via a FP Unavailable exception if the core
804 * has no FPU, in that case the reason flags will be 0 */
806 if (reason & REASON_FP) {
807 /* IEEE FP exception */
811 if (reason & REASON_TRAP) {
813 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
816 if (debugger_bpt(regs))
819 if (!(regs->msr & MSR_PR) && /* not user-mode */
820 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
824 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
830 #ifdef CONFIG_MATH_EMULATION
831 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
832 * but there seems to be a hardware bug on the 405GP (RevD)
833 * that means ESR is sometimes set incorrectly - either to
834 * ESR_DST (!?) or 0. In the process of chasing this with the
835 * hardware people - not sure if it can happen on any illegal
836 * instruction or only on FP instructions, whether there is a
837 * pattern to occurences etc. -dgibson 31/Mar/2003 */
838 switch (do_mathemu(regs)) {
840 emulate_single_step(regs);
844 code = __parse_fpscr(current->thread.fpscr.val);
845 _exception(SIGFPE, regs, code, regs->nip);
849 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
852 /* fall through on any other errors */
853 #endif /* CONFIG_MATH_EMULATION */
855 /* Try to emulate it if we should. */
856 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
857 switch (emulate_instruction(regs)) {
860 emulate_single_step(regs);
863 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
868 if (reason & REASON_PRIVILEGED)
869 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
871 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
874 void alignment_exception(struct pt_regs *regs)
876 int sig, code, fixed = 0;
878 /* we don't implement logging of alignment exceptions */
879 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
880 fixed = fix_alignment(regs);
883 regs->nip += 4; /* skip over emulated instruction */
884 emulate_single_step(regs);
888 /* Operand address was bad */
889 if (fixed == -EFAULT) {
897 _exception(sig, regs, code, regs->dar);
899 bad_page_fault(regs, regs->dar, sig);
902 void StackOverflow(struct pt_regs *regs)
904 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
905 current, regs->gpr[1]);
908 panic("kernel stack overflow");
911 void nonrecoverable_exception(struct pt_regs *regs)
913 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
914 regs->nip, regs->msr);
916 die("nonrecoverable exception", regs, SIGKILL);
919 void trace_syscall(struct pt_regs *regs)
921 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
922 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
923 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
926 void kernel_fp_unavailable_exception(struct pt_regs *regs)
928 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
929 "%lx at %lx\n", regs->trap, regs->nip);
930 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
933 void altivec_unavailable_exception(struct pt_regs *regs)
935 if (user_mode(regs)) {
936 /* A user program has executed an altivec instruction,
937 but this kernel doesn't support altivec. */
938 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
942 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
943 "%lx at %lx\n", regs->trap, regs->nip);
944 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
947 void vsx_unavailable_exception(struct pt_regs *regs)
949 if (user_mode(regs)) {
950 /* A user program has executed an vsx instruction,
951 but this kernel doesn't support vsx. */
952 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
956 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
957 "%lx at %lx\n", regs->trap, regs->nip);
958 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
961 void performance_monitor_exception(struct pt_regs *regs)
967 void SoftwareEmulation(struct pt_regs *regs)
969 extern int do_mathemu(struct pt_regs *);
970 extern int Soft_emulate_8xx(struct pt_regs *);
971 #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
975 CHECK_FULL_REGS(regs);
977 if (!user_mode(regs)) {
979 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
982 #ifdef CONFIG_MATH_EMULATION
983 errcode = do_mathemu(regs);
985 PPC_WARN_EMULATED(math, regs);
989 emulate_single_step(regs);
993 code = __parse_fpscr(current->thread.fpscr.val);
994 _exception(SIGFPE, regs, code, regs->nip);
998 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1001 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1005 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1006 errcode = Soft_emulate_8xx(regs);
1008 PPC_WARN_EMULATED(8xx, regs);
1012 emulate_single_step(regs);
1015 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1018 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1022 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1025 #endif /* CONFIG_8xx */
1027 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1029 void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
1031 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1032 * on server, it stops on the target of the branch. In order to simulate
1033 * the server behaviour, we thus restart right away with a single step
1034 * instead of stopping here when hitting a BT
1036 if (debug_status & DBSR_BT) {
1037 regs->msr &= ~MSR_DE;
1040 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1041 /* Clear the BT event */
1042 mtspr(SPRN_DBSR, DBSR_BT);
1044 /* Do the single step trick only when coming from userspace */
1045 if (user_mode(regs)) {
1046 current->thread.dbcr0 &= ~DBCR0_BT;
1047 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1048 regs->msr |= MSR_DE;
1052 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1053 5, SIGTRAP) == NOTIFY_STOP) {
1056 if (debugger_sstep(regs))
1058 } else if (debug_status & DBSR_IC) { /* Instruction complete */
1059 regs->msr &= ~MSR_DE;
1061 /* Disable instruction completion */
1062 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1063 /* Clear the instruction completion event */
1064 mtspr(SPRN_DBSR, DBSR_IC);
1066 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1067 5, SIGTRAP) == NOTIFY_STOP) {
1071 if (debugger_sstep(regs))
1074 if (user_mode(regs))
1075 current->thread.dbcr0 &= ~(DBCR0_IC);
1077 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1078 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1079 regs->msr &= ~MSR_DE;
1081 if (user_mode(regs)) {
1082 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1085 /* Disable DAC interupts */
1086 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1087 DBSR_DAC1W | DBCR0_IDM));
1089 /* Clear the DAC event */
1090 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1092 /* Setup and send the trap to the handler */
1093 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
1096 #endif /* CONFIG_4xx || CONFIG_BOOKE */
1098 #if !defined(CONFIG_TAU_INT)
1099 void TAUException(struct pt_regs *regs)
1101 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1102 regs->nip, regs->msr, regs->trap, print_tainted());
1104 #endif /* CONFIG_INT_TAU */
1106 #ifdef CONFIG_ALTIVEC
1107 void altivec_assist_exception(struct pt_regs *regs)
1111 if (!user_mode(regs)) {
1112 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1113 " at %lx\n", regs->nip);
1114 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1117 flush_altivec_to_thread(current);
1119 PPC_WARN_EMULATED(altivec, regs);
1120 err = emulate_altivec(regs);
1122 regs->nip += 4; /* skip emulated instruction */
1123 emulate_single_step(regs);
1127 if (err == -EFAULT) {
1128 /* got an error reading the instruction */
1129 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1131 /* didn't recognize the instruction */
1132 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1133 if (printk_ratelimit())
1134 printk(KERN_ERR "Unrecognized altivec instruction "
1135 "in %s at %lx\n", current->comm, regs->nip);
1136 current->thread.vscr.u[3] |= 0x10000;
1139 #endif /* CONFIG_ALTIVEC */
1142 void vsx_assist_exception(struct pt_regs *regs)
1144 if (!user_mode(regs)) {
1145 printk(KERN_EMERG "VSX assist exception in kernel mode"
1146 " at %lx\n", regs->nip);
1147 die("Kernel VSX assist exception", regs, SIGILL);
1150 flush_vsx_to_thread(current);
1151 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1152 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1154 #endif /* CONFIG_VSX */
1156 #ifdef CONFIG_FSL_BOOKE
1158 void doorbell_exception(struct pt_regs *regs)
1161 int cpu = smp_processor_id();
1164 if (num_online_cpus() < 2)
1167 for (msg = 0; msg < 4; msg++)
1168 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1169 smp_message_recv(msg);
1171 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1175 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1176 unsigned long error_code)
1178 /* We treat cache locking instructions from the user
1179 * as priv ops, in the future we could try to do
1182 if (error_code & (ESR_DLK|ESR_ILK))
1183 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1186 #endif /* CONFIG_FSL_BOOKE */
1189 void SPEFloatingPointException(struct pt_regs *regs)
1191 extern int do_spe_mathemu(struct pt_regs *regs);
1192 unsigned long spefscr;
1198 if (regs->msr & MSR_SPE)
1199 giveup_spe(current);
1202 spefscr = current->thread.spefscr;
1203 fpexc_mode = current->thread.fpexc_mode;
1205 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1208 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1211 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1213 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1216 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1219 err = do_spe_mathemu(regs);
1221 regs->nip += 4; /* skip emulated instruction */
1222 emulate_single_step(regs);
1226 if (err == -EFAULT) {
1227 /* got an error reading the instruction */
1228 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1229 } else if (err == -EINVAL) {
1230 /* didn't recognize the instruction */
1231 printk(KERN_ERR "unrecognized spe instruction "
1232 "in %s at %lx\n", current->comm, regs->nip);
1234 _exception(SIGFPE, regs, code, regs->nip);
1240 void SPEFloatingPointRoundException(struct pt_regs *regs)
1242 extern int speround_handler(struct pt_regs *regs);
1246 if (regs->msr & MSR_SPE)
1247 giveup_spe(current);
1251 err = speround_handler(regs);
1253 regs->nip += 4; /* skip emulated instruction */
1254 emulate_single_step(regs);
1258 if (err == -EFAULT) {
1259 /* got an error reading the instruction */
1260 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1261 } else if (err == -EINVAL) {
1262 /* didn't recognize the instruction */
1263 printk(KERN_ERR "unrecognized spe instruction "
1264 "in %s at %lx\n", current->comm, regs->nip);
1266 _exception(SIGFPE, regs, 0, regs->nip);
1273 * We enter here if we get an unrecoverable exception, that is, one
1274 * that happened at a point where the RI (recoverable interrupt) bit
1275 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1276 * we therefore lost state by taking this exception.
1278 void unrecoverable_exception(struct pt_regs *regs)
1280 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1281 regs->trap, regs->nip);
1282 die("Unrecoverable exception", regs, SIGABRT);
1285 #ifdef CONFIG_BOOKE_WDT
1287 * Default handler for a Watchdog exception,
1288 * spins until a reboot occurs
1290 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1292 /* Generic WatchdogHandler, implement your own */
1293 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1297 void WatchdogException(struct pt_regs *regs)
1299 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1300 WatchdogHandler(regs);
1305 * We enter here if we discover during exception entry that we are
1306 * running in supervisor mode with a userspace value in the stack pointer.
1308 void kernel_bad_stack(struct pt_regs *regs)
1310 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1311 regs->gpr[1], regs->nip);
1312 die("Bad kernel stack pointer", regs, SIGABRT);
1315 void __init trap_init(void)
1320 #ifdef CONFIG_PPC_EMULATED_STATS
1322 #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1324 struct ppc_emulated ppc_emulated = {
1325 #ifdef CONFIG_ALTIVEC
1326 WARN_EMULATED_SETUP(altivec),
1328 WARN_EMULATED_SETUP(dcba),
1329 WARN_EMULATED_SETUP(dcbz),
1330 WARN_EMULATED_SETUP(fp_pair),
1331 WARN_EMULATED_SETUP(isel),
1332 WARN_EMULATED_SETUP(mcrxr),
1333 WARN_EMULATED_SETUP(mfpvr),
1334 WARN_EMULATED_SETUP(multiple),
1335 WARN_EMULATED_SETUP(popcntb),
1336 WARN_EMULATED_SETUP(spe),
1337 WARN_EMULATED_SETUP(string),
1338 WARN_EMULATED_SETUP(unaligned),
1339 #ifdef CONFIG_MATH_EMULATION
1340 WARN_EMULATED_SETUP(math),
1341 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1342 WARN_EMULATED_SETUP(8xx),
1345 WARN_EMULATED_SETUP(vsx),
1349 u32 ppc_warn_emulated;
1351 void ppc_warn_emulated_print(const char *type)
1353 if (printk_ratelimit())
1354 pr_warning("%s used emulated %s instruction\n", current->comm,
1358 static int __init ppc_warn_emulated_init(void)
1360 struct dentry *dir, *d;
1362 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1364 if (!powerpc_debugfs_root)
1367 dir = debugfs_create_dir("emulated_instructions",
1368 powerpc_debugfs_root);
1372 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1373 &ppc_warn_emulated);
1377 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1378 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1379 (u32 *)&entries[i].val.counter);
1387 debugfs_remove_recursive(dir);
1391 device_initcall(ppc_warn_emulated_init);
1393 #endif /* CONFIG_PPC_EMULATED_STATS */