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) __read_mostly;
64 int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
65 int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
66 int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
67 int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
68 int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
69 int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
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 : "");
149 sysfs_printk_last_file();
150 if (notify_die(DIE_OOPS, str, regs, err, 255,
151 SIGSEGV) == NOTIFY_STOP)
157 printk("Recursive die() failure, output suppressed\n");
162 add_taint(TAINT_DIE);
163 spin_unlock_irqrestore(&die.lock, flags);
165 if (kexec_should_crash(current) ||
166 kexec_sr_activated(smp_processor_id()))
168 crash_kexec_secondary(regs);
171 panic("Fatal exception in interrupt");
174 panic("Fatal exception");
182 void user_single_step_siginfo(struct task_struct *tsk,
183 struct pt_regs *regs, siginfo_t *info)
185 memset(info, 0, sizeof(*info));
186 info->si_signo = SIGTRAP;
187 info->si_code = TRAP_TRACE;
188 info->si_addr = (void __user *)regs->nip;
191 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
194 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
195 "at %08lx nip %08lx lr %08lx code %x\n";
196 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
197 "at %016lx nip %016lx lr %016lx code %x\n";
199 if (!user_mode(regs)) {
200 if (die("Exception in kernel mode", regs, signr))
202 } else if (show_unhandled_signals &&
203 unhandled_signal(current, signr) &&
204 printk_ratelimit()) {
205 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
206 current->comm, current->pid, signr,
207 addr, regs->nip, regs->link, code);
210 memset(&info, 0, sizeof(info));
211 info.si_signo = signr;
213 info.si_addr = (void __user *) addr;
214 force_sig_info(signr, &info, current);
218 void system_reset_exception(struct pt_regs *regs)
220 /* See if any machine dependent calls */
221 if (ppc_md.system_reset_exception) {
222 if (ppc_md.system_reset_exception(regs))
227 cpu_set(smp_processor_id(), cpus_in_sr);
230 die("System Reset", regs, SIGABRT);
233 * Some CPUs when released from the debugger will execute this path.
234 * These CPUs entered the debugger via a soft-reset. If the CPU was
235 * hung before entering the debugger it will return to the hung
236 * state when exiting this function. This causes a problem in
237 * kdump since the hung CPU(s) will not respond to the IPI sent
238 * from kdump. To prevent the problem we call crash_kexec_secondary()
239 * here. If a kdump had not been initiated or we exit the debugger
240 * with the "exit and recover" command (x) crash_kexec_secondary()
241 * will return after 5ms and the CPU returns to its previous state.
243 crash_kexec_secondary(regs);
245 /* Must die if the interrupt is not recoverable */
246 if (!(regs->msr & MSR_RI))
247 panic("Unrecoverable System Reset");
249 /* What should we do here? We could issue a shutdown or hard reset. */
254 * I/O accesses can cause machine checks on powermacs.
255 * Check if the NIP corresponds to the address of a sync
256 * instruction for which there is an entry in the exception
258 * Note that the 601 only takes a machine check on TEA
259 * (transfer error ack) signal assertion, and does not
260 * set any of the top 16 bits of SRR1.
263 static inline int check_io_access(struct pt_regs *regs)
266 unsigned long msr = regs->msr;
267 const struct exception_table_entry *entry;
268 unsigned int *nip = (unsigned int *)regs->nip;
270 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
271 && (entry = search_exception_tables(regs->nip)) != NULL) {
273 * Check that it's a sync instruction, or somewhere
274 * in the twi; isync; nop sequence that inb/inw/inl uses.
275 * As the address is in the exception table
276 * we should be able to read the instr there.
277 * For the debug message, we look at the preceding
280 if (*nip == 0x60000000) /* nop */
282 else if (*nip == 0x4c00012c) /* isync */
284 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
289 rb = (*nip >> 11) & 0x1f;
290 printk(KERN_DEBUG "%s bad port %lx at %p\n",
291 (*nip & 0x100)? "OUT to": "IN from",
292 regs->gpr[rb] - _IO_BASE, nip);
294 regs->nip = entry->fixup;
298 #endif /* CONFIG_PPC32 */
302 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
303 /* On 4xx, the reason for the machine check or program exception
305 #define get_reason(regs) ((regs)->dsisr)
306 #ifndef CONFIG_FSL_BOOKE
307 #define get_mc_reason(regs) ((regs)->dsisr)
309 #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
311 #define REASON_FP ESR_FP
312 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
313 #define REASON_PRIVILEGED ESR_PPR
314 #define REASON_TRAP ESR_PTR
316 /* single-step stuff */
317 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
318 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
321 /* On non-4xx, the reason for the machine check or program
322 exception is in the MSR. */
323 #define get_reason(regs) ((regs)->msr)
324 #define get_mc_reason(regs) ((regs)->msr)
325 #define REASON_FP 0x100000
326 #define REASON_ILLEGAL 0x80000
327 #define REASON_PRIVILEGED 0x40000
328 #define REASON_TRAP 0x20000
330 #define single_stepping(regs) ((regs)->msr & MSR_SE)
331 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
334 #if defined(CONFIG_4xx)
335 int machine_check_4xx(struct pt_regs *regs)
337 unsigned long reason = get_mc_reason(regs);
339 if (reason & ESR_IMCP) {
340 printk("Instruction");
341 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
344 printk(" machine check in kernel mode.\n");
349 int machine_check_440A(struct pt_regs *regs)
351 unsigned long reason = get_mc_reason(regs);
353 printk("Machine check in kernel mode.\n");
354 if (reason & ESR_IMCP){
355 printk("Instruction Synchronous Machine Check exception\n");
356 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
359 u32 mcsr = mfspr(SPRN_MCSR);
361 printk("Instruction Read PLB Error\n");
363 printk("Data Read PLB Error\n");
365 printk("Data Write PLB Error\n");
366 if (mcsr & MCSR_TLBP)
367 printk("TLB Parity Error\n");
368 if (mcsr & MCSR_ICP){
369 flush_instruction_cache();
370 printk("I-Cache Parity Error\n");
372 if (mcsr & MCSR_DCSP)
373 printk("D-Cache Search Parity Error\n");
374 if (mcsr & MCSR_DCFP)
375 printk("D-Cache Flush Parity Error\n");
376 if (mcsr & MCSR_IMPE)
377 printk("Machine Check exception is imprecise\n");
380 mtspr(SPRN_MCSR, mcsr);
384 #elif defined(CONFIG_E500)
385 int machine_check_e500(struct pt_regs *regs)
387 unsigned long reason = get_mc_reason(regs);
389 printk("Machine check in kernel mode.\n");
390 printk("Caused by (from MCSR=%lx): ", reason);
392 if (reason & MCSR_MCP)
393 printk("Machine Check Signal\n");
394 if (reason & MCSR_ICPERR)
395 printk("Instruction Cache Parity Error\n");
396 if (reason & MCSR_DCP_PERR)
397 printk("Data Cache Push Parity Error\n");
398 if (reason & MCSR_DCPERR)
399 printk("Data Cache Parity Error\n");
400 if (reason & MCSR_BUS_IAERR)
401 printk("Bus - Instruction Address Error\n");
402 if (reason & MCSR_BUS_RAERR)
403 printk("Bus - Read Address Error\n");
404 if (reason & MCSR_BUS_WAERR)
405 printk("Bus - Write Address Error\n");
406 if (reason & MCSR_BUS_IBERR)
407 printk("Bus - Instruction Data Error\n");
408 if (reason & MCSR_BUS_RBERR)
409 printk("Bus - Read Data Bus Error\n");
410 if (reason & MCSR_BUS_WBERR)
411 printk("Bus - Read Data Bus Error\n");
412 if (reason & MCSR_BUS_IPERR)
413 printk("Bus - Instruction Parity Error\n");
414 if (reason & MCSR_BUS_RPERR)
415 printk("Bus - Read Parity Error\n");
419 #elif defined(CONFIG_E200)
420 int machine_check_e200(struct pt_regs *regs)
422 unsigned long reason = get_mc_reason(regs);
424 printk("Machine check in kernel mode.\n");
425 printk("Caused by (from MCSR=%lx): ", reason);
427 if (reason & MCSR_MCP)
428 printk("Machine Check Signal\n");
429 if (reason & MCSR_CP_PERR)
430 printk("Cache Push Parity Error\n");
431 if (reason & MCSR_CPERR)
432 printk("Cache Parity Error\n");
433 if (reason & MCSR_EXCP_ERR)
434 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
435 if (reason & MCSR_BUS_IRERR)
436 printk("Bus - Read Bus Error on instruction fetch\n");
437 if (reason & MCSR_BUS_DRERR)
438 printk("Bus - Read Bus Error on data load\n");
439 if (reason & MCSR_BUS_WRERR)
440 printk("Bus - Write Bus Error on buffered store or cache line push\n");
445 int machine_check_generic(struct pt_regs *regs)
447 unsigned long reason = get_mc_reason(regs);
449 printk("Machine check in kernel mode.\n");
450 printk("Caused by (from SRR1=%lx): ", reason);
451 switch (reason & 0x601F0000) {
453 printk("Machine check signal\n");
455 case 0: /* for 601 */
457 case 0x140000: /* 7450 MSS error and TEA */
458 printk("Transfer error ack signal\n");
461 printk("Data parity error signal\n");
464 printk("Address parity error signal\n");
467 printk("L1 Data Cache error\n");
470 printk("L1 Instruction Cache error\n");
473 printk("L2 data cache parity error\n");
476 printk("Unknown values in msr\n");
480 #endif /* everything else */
482 void machine_check_exception(struct pt_regs *regs)
486 /* See if any machine dependent calls. In theory, we would want
487 * to call the CPU first, and call the ppc_md. one if the CPU
488 * one returns a positive number. However there is existing code
489 * that assumes the board gets a first chance, so let's keep it
490 * that way for now and fix things later. --BenH.
492 if (ppc_md.machine_check_exception)
493 recover = ppc_md.machine_check_exception(regs);
494 else if (cur_cpu_spec->machine_check)
495 recover = cur_cpu_spec->machine_check(regs);
500 if (user_mode(regs)) {
502 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
506 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
507 /* the qspan pci read routines can cause machine checks -- Cort
509 * yuck !!! that totally needs to go away ! There are better ways
510 * to deal with that than having a wart in the mcheck handler.
513 bad_page_fault(regs, regs->dar, SIGBUS);
517 if (debugger_fault_handler(regs)) {
522 if (check_io_access(regs))
525 if (debugger_fault_handler(regs))
527 die("Machine check", regs, SIGBUS);
529 /* Must die if the interrupt is not recoverable */
530 if (!(regs->msr & MSR_RI))
531 panic("Unrecoverable Machine check");
534 void SMIException(struct pt_regs *regs)
536 die("System Management Interrupt", regs, SIGABRT);
539 void unknown_exception(struct pt_regs *regs)
541 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
542 regs->nip, regs->msr, regs->trap);
544 _exception(SIGTRAP, regs, 0, 0);
547 void instruction_breakpoint_exception(struct pt_regs *regs)
549 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
550 5, SIGTRAP) == NOTIFY_STOP)
552 if (debugger_iabr_match(regs))
554 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
557 void RunModeException(struct pt_regs *regs)
559 _exception(SIGTRAP, regs, 0, 0);
562 void __kprobes single_step_exception(struct pt_regs *regs)
564 regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
566 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
567 5, SIGTRAP) == NOTIFY_STOP)
569 if (debugger_sstep(regs))
572 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
576 * After we have successfully emulated an instruction, we have to
577 * check if the instruction was being single-stepped, and if so,
578 * pretend we got a single-step exception. This was pointed out
579 * by Kumar Gala. -- paulus
581 static void emulate_single_step(struct pt_regs *regs)
583 if (single_stepping(regs)) {
584 clear_single_step(regs);
585 _exception(SIGTRAP, regs, TRAP_TRACE, 0);
589 static inline int __parse_fpscr(unsigned long fpscr)
593 /* Invalid operation */
594 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
598 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
602 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
606 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
610 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
616 static void parse_fpe(struct pt_regs *regs)
620 flush_fp_to_thread(current);
622 code = __parse_fpscr(current->thread.fpscr.val);
624 _exception(SIGFPE, regs, code, regs->nip);
628 * Illegal instruction emulation support. Originally written to
629 * provide the PVR to user applications using the mfspr rd, PVR.
630 * Return non-zero if we can't emulate, or -EFAULT if the associated
631 * memory access caused an access fault. Return zero on success.
633 * There are a couple of ways to do this, either "decode" the instruction
634 * or directly match lots of bits. In this case, matching lots of
635 * bits is faster and easier.
638 static int emulate_string_inst(struct pt_regs *regs, u32 instword)
640 u8 rT = (instword >> 21) & 0x1f;
641 u8 rA = (instword >> 16) & 0x1f;
642 u8 NB_RB = (instword >> 11) & 0x1f;
647 /* Early out if we are an invalid form of lswx */
648 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
649 if ((rT == rA) || (rT == NB_RB))
652 EA = (rA == 0) ? 0 : regs->gpr[rA];
654 switch (instword & PPC_INST_STRING_MASK) {
658 num_bytes = regs->xer & 0x7f;
662 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
668 while (num_bytes != 0)
671 u32 shift = 8 * (3 - (pos & 0x3));
673 switch ((instword & PPC_INST_STRING_MASK)) {
676 if (get_user(val, (u8 __user *)EA))
678 /* first time updating this reg,
682 regs->gpr[rT] |= val << shift;
686 val = regs->gpr[rT] >> shift;
687 if (put_user(val, (u8 __user *)EA))
691 /* move EA to next address */
695 /* manage our position within the register */
706 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
711 ra = (instword >> 16) & 0x1f;
712 rs = (instword >> 21) & 0x1f;
715 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
716 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
717 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
723 static int emulate_isel(struct pt_regs *regs, u32 instword)
725 u8 rT = (instword >> 21) & 0x1f;
726 u8 rA = (instword >> 16) & 0x1f;
727 u8 rB = (instword >> 11) & 0x1f;
728 u8 BC = (instword >> 6) & 0x1f;
732 tmp = (rA == 0) ? 0 : regs->gpr[rA];
733 bit = (regs->ccr >> (31 - BC)) & 0x1;
735 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
740 static int emulate_instruction(struct pt_regs *regs)
745 if (!user_mode(regs) || (regs->msr & MSR_LE))
747 CHECK_FULL_REGS(regs);
749 if (get_user(instword, (u32 __user *)(regs->nip)))
752 /* Emulate the mfspr rD, PVR. */
753 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
754 PPC_WARN_EMULATED(mfpvr, regs);
755 rd = (instword >> 21) & 0x1f;
756 regs->gpr[rd] = mfspr(SPRN_PVR);
760 /* Emulating the dcba insn is just a no-op. */
761 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
762 PPC_WARN_EMULATED(dcba, regs);
766 /* Emulate the mcrxr insn. */
767 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
768 int shift = (instword >> 21) & 0x1c;
769 unsigned long msk = 0xf0000000UL >> shift;
771 PPC_WARN_EMULATED(mcrxr, regs);
772 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
773 regs->xer &= ~0xf0000000UL;
777 /* Emulate load/store string insn. */
778 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
779 PPC_WARN_EMULATED(string, regs);
780 return emulate_string_inst(regs, instword);
783 /* Emulate the popcntb (Population Count Bytes) instruction. */
784 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
785 PPC_WARN_EMULATED(popcntb, regs);
786 return emulate_popcntb_inst(regs, instword);
789 /* Emulate isel (Integer Select) instruction */
790 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
791 PPC_WARN_EMULATED(isel, regs);
792 return emulate_isel(regs, instword);
798 int is_valid_bugaddr(unsigned long addr)
800 return is_kernel_addr(addr);
803 void __kprobes program_check_exception(struct pt_regs *regs)
805 unsigned int reason = get_reason(regs);
806 extern int do_mathemu(struct pt_regs *regs);
808 /* We can now get here via a FP Unavailable exception if the core
809 * has no FPU, in that case the reason flags will be 0 */
811 if (reason & REASON_FP) {
812 /* IEEE FP exception */
816 if (reason & REASON_TRAP) {
818 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
821 if (debugger_bpt(regs))
824 if (!(regs->msr & MSR_PR) && /* not user-mode */
825 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
829 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
835 #ifdef CONFIG_MATH_EMULATION
836 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
837 * but there seems to be a hardware bug on the 405GP (RevD)
838 * that means ESR is sometimes set incorrectly - either to
839 * ESR_DST (!?) or 0. In the process of chasing this with the
840 * hardware people - not sure if it can happen on any illegal
841 * instruction or only on FP instructions, whether there is a
842 * pattern to occurences etc. -dgibson 31/Mar/2003 */
843 switch (do_mathemu(regs)) {
845 emulate_single_step(regs);
849 code = __parse_fpscr(current->thread.fpscr.val);
850 _exception(SIGFPE, regs, code, regs->nip);
854 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
857 /* fall through on any other errors */
858 #endif /* CONFIG_MATH_EMULATION */
860 /* Try to emulate it if we should. */
861 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
862 switch (emulate_instruction(regs)) {
865 emulate_single_step(regs);
868 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
873 if (reason & REASON_PRIVILEGED)
874 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
876 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
879 void alignment_exception(struct pt_regs *regs)
881 int sig, code, fixed = 0;
883 /* we don't implement logging of alignment exceptions */
884 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
885 fixed = fix_alignment(regs);
888 regs->nip += 4; /* skip over emulated instruction */
889 emulate_single_step(regs);
893 /* Operand address was bad */
894 if (fixed == -EFAULT) {
902 _exception(sig, regs, code, regs->dar);
904 bad_page_fault(regs, regs->dar, sig);
907 void StackOverflow(struct pt_regs *regs)
909 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
910 current, regs->gpr[1]);
913 panic("kernel stack overflow");
916 void nonrecoverable_exception(struct pt_regs *regs)
918 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
919 regs->nip, regs->msr);
921 die("nonrecoverable exception", regs, SIGKILL);
924 void trace_syscall(struct pt_regs *regs)
926 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
927 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
928 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
931 void kernel_fp_unavailable_exception(struct pt_regs *regs)
933 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
934 "%lx at %lx\n", regs->trap, regs->nip);
935 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
938 void altivec_unavailable_exception(struct pt_regs *regs)
940 if (user_mode(regs)) {
941 /* A user program has executed an altivec instruction,
942 but this kernel doesn't support altivec. */
943 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
947 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
948 "%lx at %lx\n", regs->trap, regs->nip);
949 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
952 void vsx_unavailable_exception(struct pt_regs *regs)
954 if (user_mode(regs)) {
955 /* A user program has executed an vsx instruction,
956 but this kernel doesn't support vsx. */
957 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
961 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
962 "%lx at %lx\n", regs->trap, regs->nip);
963 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
966 void performance_monitor_exception(struct pt_regs *regs)
972 void SoftwareEmulation(struct pt_regs *regs)
974 extern int do_mathemu(struct pt_regs *);
975 extern int Soft_emulate_8xx(struct pt_regs *);
976 #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
980 CHECK_FULL_REGS(regs);
982 if (!user_mode(regs)) {
984 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
987 #ifdef CONFIG_MATH_EMULATION
988 errcode = do_mathemu(regs);
990 PPC_WARN_EMULATED(math, regs);
994 emulate_single_step(regs);
998 code = __parse_fpscr(current->thread.fpscr.val);
999 _exception(SIGFPE, regs, code, regs->nip);
1003 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1006 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1010 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1011 errcode = Soft_emulate_8xx(regs);
1013 PPC_WARN_EMULATED(8xx, regs);
1017 emulate_single_step(regs);
1020 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1023 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1027 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1030 #endif /* CONFIG_8xx */
1032 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1034 void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
1036 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1037 * on server, it stops on the target of the branch. In order to simulate
1038 * the server behaviour, we thus restart right away with a single step
1039 * instead of stopping here when hitting a BT
1041 if (debug_status & DBSR_BT) {
1042 regs->msr &= ~MSR_DE;
1045 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1046 /* Clear the BT event */
1047 mtspr(SPRN_DBSR, DBSR_BT);
1049 /* Do the single step trick only when coming from userspace */
1050 if (user_mode(regs)) {
1051 current->thread.dbcr0 &= ~DBCR0_BT;
1052 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1053 regs->msr |= MSR_DE;
1057 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1058 5, SIGTRAP) == NOTIFY_STOP) {
1061 if (debugger_sstep(regs))
1063 } else if (debug_status & DBSR_IC) { /* Instruction complete */
1064 regs->msr &= ~MSR_DE;
1066 /* Disable instruction completion */
1067 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1068 /* Clear the instruction completion event */
1069 mtspr(SPRN_DBSR, DBSR_IC);
1071 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1072 5, SIGTRAP) == NOTIFY_STOP) {
1076 if (debugger_sstep(regs))
1079 if (user_mode(regs))
1080 current->thread.dbcr0 &= ~(DBCR0_IC);
1082 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1083 } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1084 regs->msr &= ~MSR_DE;
1086 if (user_mode(regs)) {
1087 current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
1090 /* Disable DAC interupts */
1091 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
1092 DBSR_DAC1W | DBCR0_IDM));
1094 /* Clear the DAC event */
1095 mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
1097 /* Setup and send the trap to the handler */
1098 do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
1101 #endif /* CONFIG_4xx || CONFIG_BOOKE */
1103 #if !defined(CONFIG_TAU_INT)
1104 void TAUException(struct pt_regs *regs)
1106 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1107 regs->nip, regs->msr, regs->trap, print_tainted());
1109 #endif /* CONFIG_INT_TAU */
1111 #ifdef CONFIG_ALTIVEC
1112 void altivec_assist_exception(struct pt_regs *regs)
1116 if (!user_mode(regs)) {
1117 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1118 " at %lx\n", regs->nip);
1119 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
1122 flush_altivec_to_thread(current);
1124 PPC_WARN_EMULATED(altivec, regs);
1125 err = emulate_altivec(regs);
1127 regs->nip += 4; /* skip emulated instruction */
1128 emulate_single_step(regs);
1132 if (err == -EFAULT) {
1133 /* got an error reading the instruction */
1134 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1136 /* didn't recognize the instruction */
1137 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1138 if (printk_ratelimit())
1139 printk(KERN_ERR "Unrecognized altivec instruction "
1140 "in %s at %lx\n", current->comm, regs->nip);
1141 current->thread.vscr.u[3] |= 0x10000;
1144 #endif /* CONFIG_ALTIVEC */
1147 void vsx_assist_exception(struct pt_regs *regs)
1149 if (!user_mode(regs)) {
1150 printk(KERN_EMERG "VSX assist exception in kernel mode"
1151 " at %lx\n", regs->nip);
1152 die("Kernel VSX assist exception", regs, SIGILL);
1155 flush_vsx_to_thread(current);
1156 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1157 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1159 #endif /* CONFIG_VSX */
1161 #ifdef CONFIG_FSL_BOOKE
1163 void doorbell_exception(struct pt_regs *regs)
1166 int cpu = smp_processor_id();
1169 if (num_online_cpus() < 2)
1172 for (msg = 0; msg < 4; msg++)
1173 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1174 smp_message_recv(msg);
1176 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1180 void CacheLockingException(struct pt_regs *regs, unsigned long address,
1181 unsigned long error_code)
1183 /* We treat cache locking instructions from the user
1184 * as priv ops, in the future we could try to do
1187 if (error_code & (ESR_DLK|ESR_ILK))
1188 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1191 #endif /* CONFIG_FSL_BOOKE */
1194 void SPEFloatingPointException(struct pt_regs *regs)
1196 extern int do_spe_mathemu(struct pt_regs *regs);
1197 unsigned long spefscr;
1203 if (regs->msr & MSR_SPE)
1204 giveup_spe(current);
1207 spefscr = current->thread.spefscr;
1208 fpexc_mode = current->thread.fpexc_mode;
1210 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1213 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1216 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1218 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1221 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1224 err = do_spe_mathemu(regs);
1226 regs->nip += 4; /* skip emulated instruction */
1227 emulate_single_step(regs);
1231 if (err == -EFAULT) {
1232 /* got an error reading the instruction */
1233 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1234 } else if (err == -EINVAL) {
1235 /* didn't recognize the instruction */
1236 printk(KERN_ERR "unrecognized spe instruction "
1237 "in %s at %lx\n", current->comm, regs->nip);
1239 _exception(SIGFPE, regs, code, regs->nip);
1245 void SPEFloatingPointRoundException(struct pt_regs *regs)
1247 extern int speround_handler(struct pt_regs *regs);
1251 if (regs->msr & MSR_SPE)
1252 giveup_spe(current);
1256 err = speround_handler(regs);
1258 regs->nip += 4; /* skip emulated instruction */
1259 emulate_single_step(regs);
1263 if (err == -EFAULT) {
1264 /* got an error reading the instruction */
1265 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1266 } else if (err == -EINVAL) {
1267 /* didn't recognize the instruction */
1268 printk(KERN_ERR "unrecognized spe instruction "
1269 "in %s at %lx\n", current->comm, regs->nip);
1271 _exception(SIGFPE, regs, 0, regs->nip);
1278 * We enter here if we get an unrecoverable exception, that is, one
1279 * that happened at a point where the RI (recoverable interrupt) bit
1280 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1281 * we therefore lost state by taking this exception.
1283 void unrecoverable_exception(struct pt_regs *regs)
1285 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1286 regs->trap, regs->nip);
1287 die("Unrecoverable exception", regs, SIGABRT);
1290 #ifdef CONFIG_BOOKE_WDT
1292 * Default handler for a Watchdog exception,
1293 * spins until a reboot occurs
1295 void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1297 /* Generic WatchdogHandler, implement your own */
1298 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1302 void WatchdogException(struct pt_regs *regs)
1304 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1305 WatchdogHandler(regs);
1310 * We enter here if we discover during exception entry that we are
1311 * running in supervisor mode with a userspace value in the stack pointer.
1313 void kernel_bad_stack(struct pt_regs *regs)
1315 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1316 regs->gpr[1], regs->nip);
1317 die("Bad kernel stack pointer", regs, SIGABRT);
1320 void __init trap_init(void)
1325 #ifdef CONFIG_PPC_EMULATED_STATS
1327 #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1329 struct ppc_emulated ppc_emulated = {
1330 #ifdef CONFIG_ALTIVEC
1331 WARN_EMULATED_SETUP(altivec),
1333 WARN_EMULATED_SETUP(dcba),
1334 WARN_EMULATED_SETUP(dcbz),
1335 WARN_EMULATED_SETUP(fp_pair),
1336 WARN_EMULATED_SETUP(isel),
1337 WARN_EMULATED_SETUP(mcrxr),
1338 WARN_EMULATED_SETUP(mfpvr),
1339 WARN_EMULATED_SETUP(multiple),
1340 WARN_EMULATED_SETUP(popcntb),
1341 WARN_EMULATED_SETUP(spe),
1342 WARN_EMULATED_SETUP(string),
1343 WARN_EMULATED_SETUP(unaligned),
1344 #ifdef CONFIG_MATH_EMULATION
1345 WARN_EMULATED_SETUP(math),
1346 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1347 WARN_EMULATED_SETUP(8xx),
1350 WARN_EMULATED_SETUP(vsx),
1354 u32 ppc_warn_emulated;
1356 void ppc_warn_emulated_print(const char *type)
1358 if (printk_ratelimit())
1359 pr_warning("%s used emulated %s instruction\n", current->comm,
1363 static int __init ppc_warn_emulated_init(void)
1365 struct dentry *dir, *d;
1367 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1369 if (!powerpc_debugfs_root)
1372 dir = debugfs_create_dir("emulated_instructions",
1373 powerpc_debugfs_root);
1377 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1378 &ppc_warn_emulated);
1382 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1383 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1384 (u32 *)&entries[i].val.counter);
1392 debugfs_remove_recursive(dir);
1396 device_initcall(ppc_warn_emulated_init);
1398 #endif /* CONFIG_PPC_EMULATED_STATS */