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 _exception(int signr
, struct pt_regs
*regs
, int code
, unsigned long addr
)
180 const char fmt32
[] = KERN_INFO
"%s[%d]: unhandled signal %d " \
181 "at %08lx nip %08lx lr %08lx code %x\n";
182 const char fmt64
[] = KERN_INFO
"%s[%d]: unhandled signal %d " \
183 "at %016lx nip %016lx lr %016lx code %x\n";
185 if (!user_mode(regs
)) {
186 if (die("Exception in kernel mode", regs
, signr
))
188 } else if (show_unhandled_signals
&&
189 unhandled_signal(current
, signr
) &&
190 printk_ratelimit()) {
191 printk(regs
->msr
& MSR_SF
? fmt64
: fmt32
,
192 current
->comm
, current
->pid
, signr
,
193 addr
, regs
->nip
, regs
->link
, code
);
196 memset(&info
, 0, sizeof(info
));
197 info
.si_signo
= signr
;
199 info
.si_addr
= (void __user
*) addr
;
200 force_sig_info(signr
, &info
, current
);
204 void system_reset_exception(struct pt_regs
*regs
)
206 /* See if any machine dependent calls */
207 if (ppc_md
.system_reset_exception
) {
208 if (ppc_md
.system_reset_exception(regs
))
213 cpu_set(smp_processor_id(), cpus_in_sr
);
216 die("System Reset", regs
, SIGABRT
);
219 * Some CPUs when released from the debugger will execute this path.
220 * These CPUs entered the debugger via a soft-reset. If the CPU was
221 * hung before entering the debugger it will return to the hung
222 * state when exiting this function. This causes a problem in
223 * kdump since the hung CPU(s) will not respond to the IPI sent
224 * from kdump. To prevent the problem we call crash_kexec_secondary()
225 * here. If a kdump had not been initiated or we exit the debugger
226 * with the "exit and recover" command (x) crash_kexec_secondary()
227 * will return after 5ms and the CPU returns to its previous state.
229 crash_kexec_secondary(regs
);
231 /* Must die if the interrupt is not recoverable */
232 if (!(regs
->msr
& MSR_RI
))
233 panic("Unrecoverable System Reset");
235 /* What should we do here? We could issue a shutdown or hard reset. */
240 * I/O accesses can cause machine checks on powermacs.
241 * Check if the NIP corresponds to the address of a sync
242 * instruction for which there is an entry in the exception
244 * Note that the 601 only takes a machine check on TEA
245 * (transfer error ack) signal assertion, and does not
246 * set any of the top 16 bits of SRR1.
249 static inline int check_io_access(struct pt_regs
*regs
)
252 unsigned long msr
= regs
->msr
;
253 const struct exception_table_entry
*entry
;
254 unsigned int *nip
= (unsigned int *)regs
->nip
;
256 if (((msr
& 0xffff0000) == 0 || (msr
& (0x80000 | 0x40000)))
257 && (entry
= search_exception_tables(regs
->nip
)) != NULL
) {
259 * Check that it's a sync instruction, or somewhere
260 * in the twi; isync; nop sequence that inb/inw/inl uses.
261 * As the address is in the exception table
262 * we should be able to read the instr there.
263 * For the debug message, we look at the preceding
266 if (*nip
== 0x60000000) /* nop */
268 else if (*nip
== 0x4c00012c) /* isync */
270 if (*nip
== 0x7c0004ac || (*nip
>> 26) == 3) {
275 rb
= (*nip
>> 11) & 0x1f;
276 printk(KERN_DEBUG
"%s bad port %lx at %p\n",
277 (*nip
& 0x100)? "OUT to": "IN from",
278 regs
->gpr
[rb
] - _IO_BASE
, nip
);
280 regs
->nip
= entry
->fixup
;
284 #endif /* CONFIG_PPC32 */
288 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
289 /* On 4xx, the reason for the machine check or program exception
291 #define get_reason(regs) ((regs)->dsisr)
292 #ifndef CONFIG_FSL_BOOKE
293 #define get_mc_reason(regs) ((regs)->dsisr)
295 #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
297 #define REASON_FP ESR_FP
298 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
299 #define REASON_PRIVILEGED ESR_PPR
300 #define REASON_TRAP ESR_PTR
302 /* single-step stuff */
303 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
304 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
307 /* On non-4xx, the reason for the machine check or program
308 exception is in the MSR. */
309 #define get_reason(regs) ((regs)->msr)
310 #define get_mc_reason(regs) ((regs)->msr)
311 #define REASON_FP 0x100000
312 #define REASON_ILLEGAL 0x80000
313 #define REASON_PRIVILEGED 0x40000
314 #define REASON_TRAP 0x20000
316 #define single_stepping(regs) ((regs)->msr & MSR_SE)
317 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
320 #if defined(CONFIG_4xx)
321 int machine_check_4xx(struct pt_regs
*regs
)
323 unsigned long reason
= get_mc_reason(regs
);
325 if (reason
& ESR_IMCP
) {
326 printk("Instruction");
327 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
330 printk(" machine check in kernel mode.\n");
335 int machine_check_440A(struct pt_regs
*regs
)
337 unsigned long reason
= get_mc_reason(regs
);
339 printk("Machine check in kernel mode.\n");
340 if (reason
& ESR_IMCP
){
341 printk("Instruction Synchronous Machine Check exception\n");
342 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
345 u32 mcsr
= mfspr(SPRN_MCSR
);
347 printk("Instruction Read PLB Error\n");
349 printk("Data Read PLB Error\n");
351 printk("Data Write PLB Error\n");
352 if (mcsr
& MCSR_TLBP
)
353 printk("TLB Parity Error\n");
354 if (mcsr
& MCSR_ICP
){
355 flush_instruction_cache();
356 printk("I-Cache Parity Error\n");
358 if (mcsr
& MCSR_DCSP
)
359 printk("D-Cache Search Parity Error\n");
360 if (mcsr
& MCSR_DCFP
)
361 printk("D-Cache Flush Parity Error\n");
362 if (mcsr
& MCSR_IMPE
)
363 printk("Machine Check exception is imprecise\n");
366 mtspr(SPRN_MCSR
, mcsr
);
370 #elif defined(CONFIG_E500)
371 int machine_check_e500(struct pt_regs
*regs
)
373 unsigned long reason
= get_mc_reason(regs
);
375 printk("Machine check in kernel mode.\n");
376 printk("Caused by (from MCSR=%lx): ", reason
);
378 if (reason
& MCSR_MCP
)
379 printk("Machine Check Signal\n");
380 if (reason
& MCSR_ICPERR
)
381 printk("Instruction Cache Parity Error\n");
382 if (reason
& MCSR_DCP_PERR
)
383 printk("Data Cache Push Parity Error\n");
384 if (reason
& MCSR_DCPERR
)
385 printk("Data Cache Parity Error\n");
386 if (reason
& MCSR_BUS_IAERR
)
387 printk("Bus - Instruction Address Error\n");
388 if (reason
& MCSR_BUS_RAERR
)
389 printk("Bus - Read Address Error\n");
390 if (reason
& MCSR_BUS_WAERR
)
391 printk("Bus - Write Address Error\n");
392 if (reason
& MCSR_BUS_IBERR
)
393 printk("Bus - Instruction Data Error\n");
394 if (reason
& MCSR_BUS_RBERR
)
395 printk("Bus - Read Data Bus Error\n");
396 if (reason
& MCSR_BUS_WBERR
)
397 printk("Bus - Read Data Bus Error\n");
398 if (reason
& MCSR_BUS_IPERR
)
399 printk("Bus - Instruction Parity Error\n");
400 if (reason
& MCSR_BUS_RPERR
)
401 printk("Bus - Read Parity Error\n");
405 #elif defined(CONFIG_E200)
406 int machine_check_e200(struct pt_regs
*regs
)
408 unsigned long reason
= get_mc_reason(regs
);
410 printk("Machine check in kernel mode.\n");
411 printk("Caused by (from MCSR=%lx): ", reason
);
413 if (reason
& MCSR_MCP
)
414 printk("Machine Check Signal\n");
415 if (reason
& MCSR_CP_PERR
)
416 printk("Cache Push Parity Error\n");
417 if (reason
& MCSR_CPERR
)
418 printk("Cache Parity Error\n");
419 if (reason
& MCSR_EXCP_ERR
)
420 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
421 if (reason
& MCSR_BUS_IRERR
)
422 printk("Bus - Read Bus Error on instruction fetch\n");
423 if (reason
& MCSR_BUS_DRERR
)
424 printk("Bus - Read Bus Error on data load\n");
425 if (reason
& MCSR_BUS_WRERR
)
426 printk("Bus - Write Bus Error on buffered store or cache line push\n");
431 int machine_check_generic(struct pt_regs
*regs
)
433 unsigned long reason
= get_mc_reason(regs
);
435 printk("Machine check in kernel mode.\n");
436 printk("Caused by (from SRR1=%lx): ", reason
);
437 switch (reason
& 0x601F0000) {
439 printk("Machine check signal\n");
441 case 0: /* for 601 */
443 case 0x140000: /* 7450 MSS error and TEA */
444 printk("Transfer error ack signal\n");
447 printk("Data parity error signal\n");
450 printk("Address parity error signal\n");
453 printk("L1 Data Cache error\n");
456 printk("L1 Instruction Cache error\n");
459 printk("L2 data cache parity error\n");
462 printk("Unknown values in msr\n");
466 #endif /* everything else */
468 void machine_check_exception(struct pt_regs
*regs
)
472 /* See if any machine dependent calls. In theory, we would want
473 * to call the CPU first, and call the ppc_md. one if the CPU
474 * one returns a positive number. However there is existing code
475 * that assumes the board gets a first chance, so let's keep it
476 * that way for now and fix things later. --BenH.
478 if (ppc_md
.machine_check_exception
)
479 recover
= ppc_md
.machine_check_exception(regs
);
480 else if (cur_cpu_spec
->machine_check
)
481 recover
= cur_cpu_spec
->machine_check(regs
);
486 if (user_mode(regs
)) {
488 _exception(SIGBUS
, regs
, BUS_ADRERR
, regs
->nip
);
492 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
493 /* the qspan pci read routines can cause machine checks -- Cort
495 * yuck !!! that totally needs to go away ! There are better ways
496 * to deal with that than having a wart in the mcheck handler.
499 bad_page_fault(regs
, regs
->dar
, SIGBUS
);
503 if (debugger_fault_handler(regs
)) {
508 if (check_io_access(regs
))
511 if (debugger_fault_handler(regs
))
513 die("Machine check", regs
, SIGBUS
);
515 /* Must die if the interrupt is not recoverable */
516 if (!(regs
->msr
& MSR_RI
))
517 panic("Unrecoverable Machine check");
520 void SMIException(struct pt_regs
*regs
)
522 die("System Management Interrupt", regs
, SIGABRT
);
525 void unknown_exception(struct pt_regs
*regs
)
527 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
528 regs
->nip
, regs
->msr
, regs
->trap
);
530 _exception(SIGTRAP
, regs
, 0, 0);
533 void instruction_breakpoint_exception(struct pt_regs
*regs
)
535 if (notify_die(DIE_IABR_MATCH
, "iabr_match", regs
, 5,
536 5, SIGTRAP
) == NOTIFY_STOP
)
538 if (debugger_iabr_match(regs
))
540 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
543 void RunModeException(struct pt_regs
*regs
)
545 _exception(SIGTRAP
, regs
, 0, 0);
548 void __kprobes
single_step_exception(struct pt_regs
*regs
)
550 regs
->msr
&= ~(MSR_SE
| MSR_BE
); /* Turn off 'trace' bits */
552 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
553 5, SIGTRAP
) == NOTIFY_STOP
)
555 if (debugger_sstep(regs
))
558 _exception(SIGTRAP
, regs
, TRAP_TRACE
, regs
->nip
);
562 * After we have successfully emulated an instruction, we have to
563 * check if the instruction was being single-stepped, and if so,
564 * pretend we got a single-step exception. This was pointed out
565 * by Kumar Gala. -- paulus
567 static void emulate_single_step(struct pt_regs
*regs
)
569 if (single_stepping(regs
)) {
570 clear_single_step(regs
);
571 _exception(SIGTRAP
, regs
, TRAP_TRACE
, 0);
575 static inline int __parse_fpscr(unsigned long fpscr
)
579 /* Invalid operation */
580 if ((fpscr
& FPSCR_VE
) && (fpscr
& FPSCR_VX
))
584 else if ((fpscr
& FPSCR_OE
) && (fpscr
& FPSCR_OX
))
588 else if ((fpscr
& FPSCR_UE
) && (fpscr
& FPSCR_UX
))
592 else if ((fpscr
& FPSCR_ZE
) && (fpscr
& FPSCR_ZX
))
596 else if ((fpscr
& FPSCR_XE
) && (fpscr
& FPSCR_XX
))
602 static void parse_fpe(struct pt_regs
*regs
)
606 flush_fp_to_thread(current
);
608 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
610 _exception(SIGFPE
, regs
, code
, regs
->nip
);
614 * Illegal instruction emulation support. Originally written to
615 * provide the PVR to user applications using the mfspr rd, PVR.
616 * Return non-zero if we can't emulate, or -EFAULT if the associated
617 * memory access caused an access fault. Return zero on success.
619 * There are a couple of ways to do this, either "decode" the instruction
620 * or directly match lots of bits. In this case, matching lots of
621 * bits is faster and easier.
624 static int emulate_string_inst(struct pt_regs
*regs
, u32 instword
)
626 u8 rT
= (instword
>> 21) & 0x1f;
627 u8 rA
= (instword
>> 16) & 0x1f;
628 u8 NB_RB
= (instword
>> 11) & 0x1f;
633 /* Early out if we are an invalid form of lswx */
634 if ((instword
& PPC_INST_STRING_MASK
) == PPC_INST_LSWX
)
635 if ((rT
== rA
) || (rT
== NB_RB
))
638 EA
= (rA
== 0) ? 0 : regs
->gpr
[rA
];
640 switch (instword
& PPC_INST_STRING_MASK
) {
644 num_bytes
= regs
->xer
& 0x7f;
648 num_bytes
= (NB_RB
== 0) ? 32 : NB_RB
;
654 while (num_bytes
!= 0)
657 u32 shift
= 8 * (3 - (pos
& 0x3));
659 switch ((instword
& PPC_INST_STRING_MASK
)) {
662 if (get_user(val
, (u8 __user
*)EA
))
664 /* first time updating this reg,
668 regs
->gpr
[rT
] |= val
<< shift
;
672 val
= regs
->gpr
[rT
] >> shift
;
673 if (put_user(val
, (u8 __user
*)EA
))
677 /* move EA to next address */
681 /* manage our position within the register */
692 static int emulate_popcntb_inst(struct pt_regs
*regs
, u32 instword
)
697 ra
= (instword
>> 16) & 0x1f;
698 rs
= (instword
>> 21) & 0x1f;
701 tmp
= tmp
- ((tmp
>> 1) & 0x5555555555555555ULL
);
702 tmp
= (tmp
& 0x3333333333333333ULL
) + ((tmp
>> 2) & 0x3333333333333333ULL
);
703 tmp
= (tmp
+ (tmp
>> 4)) & 0x0f0f0f0f0f0f0f0fULL
;
709 static int emulate_isel(struct pt_regs
*regs
, u32 instword
)
711 u8 rT
= (instword
>> 21) & 0x1f;
712 u8 rA
= (instword
>> 16) & 0x1f;
713 u8 rB
= (instword
>> 11) & 0x1f;
714 u8 BC
= (instword
>> 6) & 0x1f;
718 tmp
= (rA
== 0) ? 0 : regs
->gpr
[rA
];
719 bit
= (regs
->ccr
>> (31 - BC
)) & 0x1;
721 regs
->gpr
[rT
] = bit
? tmp
: regs
->gpr
[rB
];
726 static int emulate_instruction(struct pt_regs
*regs
)
731 if (!user_mode(regs
) || (regs
->msr
& MSR_LE
))
733 CHECK_FULL_REGS(regs
);
735 if (get_user(instword
, (u32 __user
*)(regs
->nip
)))
738 /* Emulate the mfspr rD, PVR. */
739 if ((instword
& PPC_INST_MFSPR_PVR_MASK
) == PPC_INST_MFSPR_PVR
) {
740 PPC_WARN_EMULATED(mfpvr
, regs
);
741 rd
= (instword
>> 21) & 0x1f;
742 regs
->gpr
[rd
] = mfspr(SPRN_PVR
);
746 /* Emulating the dcba insn is just a no-op. */
747 if ((instword
& PPC_INST_DCBA_MASK
) == PPC_INST_DCBA
) {
748 PPC_WARN_EMULATED(dcba
, regs
);
752 /* Emulate the mcrxr insn. */
753 if ((instword
& PPC_INST_MCRXR_MASK
) == PPC_INST_MCRXR
) {
754 int shift
= (instword
>> 21) & 0x1c;
755 unsigned long msk
= 0xf0000000UL
>> shift
;
757 PPC_WARN_EMULATED(mcrxr
, regs
);
758 regs
->ccr
= (regs
->ccr
& ~msk
) | ((regs
->xer
>> shift
) & msk
);
759 regs
->xer
&= ~0xf0000000UL
;
763 /* Emulate load/store string insn. */
764 if ((instword
& PPC_INST_STRING_GEN_MASK
) == PPC_INST_STRING
) {
765 PPC_WARN_EMULATED(string
, regs
);
766 return emulate_string_inst(regs
, instword
);
769 /* Emulate the popcntb (Population Count Bytes) instruction. */
770 if ((instword
& PPC_INST_POPCNTB_MASK
) == PPC_INST_POPCNTB
) {
771 PPC_WARN_EMULATED(popcntb
, regs
);
772 return emulate_popcntb_inst(regs
, instword
);
775 /* Emulate isel (Integer Select) instruction */
776 if ((instword
& PPC_INST_ISEL_MASK
) == PPC_INST_ISEL
) {
777 PPC_WARN_EMULATED(isel
, regs
);
778 return emulate_isel(regs
, instword
);
784 int is_valid_bugaddr(unsigned long addr
)
786 return is_kernel_addr(addr
);
789 void __kprobes
program_check_exception(struct pt_regs
*regs
)
791 unsigned int reason
= get_reason(regs
);
792 extern int do_mathemu(struct pt_regs
*regs
);
794 /* We can now get here via a FP Unavailable exception if the core
795 * has no FPU, in that case the reason flags will be 0 */
797 if (reason
& REASON_FP
) {
798 /* IEEE FP exception */
802 if (reason
& REASON_TRAP
) {
804 if (notify_die(DIE_BPT
, "breakpoint", regs
, 5, 5, SIGTRAP
)
807 if (debugger_bpt(regs
))
810 if (!(regs
->msr
& MSR_PR
) && /* not user-mode */
811 report_bug(regs
->nip
, regs
) == BUG_TRAP_TYPE_WARN
) {
815 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
821 #ifdef CONFIG_MATH_EMULATION
822 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
823 * but there seems to be a hardware bug on the 405GP (RevD)
824 * that means ESR is sometimes set incorrectly - either to
825 * ESR_DST (!?) or 0. In the process of chasing this with the
826 * hardware people - not sure if it can happen on any illegal
827 * instruction or only on FP instructions, whether there is a
828 * pattern to occurences etc. -dgibson 31/Mar/2003 */
829 switch (do_mathemu(regs
)) {
831 emulate_single_step(regs
);
835 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
836 _exception(SIGFPE
, regs
, code
, regs
->nip
);
840 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
843 /* fall through on any other errors */
844 #endif /* CONFIG_MATH_EMULATION */
846 /* Try to emulate it if we should. */
847 if (reason
& (REASON_ILLEGAL
| REASON_PRIVILEGED
)) {
848 switch (emulate_instruction(regs
)) {
851 emulate_single_step(regs
);
854 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
859 if (reason
& REASON_PRIVILEGED
)
860 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
862 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
865 void alignment_exception(struct pt_regs
*regs
)
867 int sig
, code
, fixed
= 0;
869 /* we don't implement logging of alignment exceptions */
870 if (!(current
->thread
.align_ctl
& PR_UNALIGN_SIGBUS
))
871 fixed
= fix_alignment(regs
);
874 regs
->nip
+= 4; /* skip over emulated instruction */
875 emulate_single_step(regs
);
879 /* Operand address was bad */
880 if (fixed
== -EFAULT
) {
888 _exception(sig
, regs
, code
, regs
->dar
);
890 bad_page_fault(regs
, regs
->dar
, sig
);
893 void StackOverflow(struct pt_regs
*regs
)
895 printk(KERN_CRIT
"Kernel stack overflow in process %p, r1=%lx\n",
896 current
, regs
->gpr
[1]);
899 panic("kernel stack overflow");
902 void nonrecoverable_exception(struct pt_regs
*regs
)
904 printk(KERN_ERR
"Non-recoverable exception at PC=%lx MSR=%lx\n",
905 regs
->nip
, regs
->msr
);
907 die("nonrecoverable exception", regs
, SIGKILL
);
910 void trace_syscall(struct pt_regs
*regs
)
912 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
913 current
, task_pid_nr(current
), regs
->nip
, regs
->link
, regs
->gpr
[0],
914 regs
->ccr
&0x10000000?"Error=":"", regs
->gpr
[3], print_tainted());
917 void kernel_fp_unavailable_exception(struct pt_regs
*regs
)
919 printk(KERN_EMERG
"Unrecoverable FP Unavailable Exception "
920 "%lx at %lx\n", regs
->trap
, regs
->nip
);
921 die("Unrecoverable FP Unavailable Exception", regs
, SIGABRT
);
924 void altivec_unavailable_exception(struct pt_regs
*regs
)
926 if (user_mode(regs
)) {
927 /* A user program has executed an altivec instruction,
928 but this kernel doesn't support altivec. */
929 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
933 printk(KERN_EMERG
"Unrecoverable VMX/Altivec Unavailable Exception "
934 "%lx at %lx\n", regs
->trap
, regs
->nip
);
935 die("Unrecoverable VMX/Altivec Unavailable Exception", regs
, SIGABRT
);
938 void vsx_unavailable_exception(struct pt_regs
*regs
)
940 if (user_mode(regs
)) {
941 /* A user program has executed an vsx instruction,
942 but this kernel doesn't support vsx. */
943 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
947 printk(KERN_EMERG
"Unrecoverable VSX Unavailable Exception "
948 "%lx at %lx\n", regs
->trap
, regs
->nip
);
949 die("Unrecoverable VSX Unavailable Exception", regs
, SIGABRT
);
952 void performance_monitor_exception(struct pt_regs
*regs
)
958 void SoftwareEmulation(struct pt_regs
*regs
)
960 extern int do_mathemu(struct pt_regs
*);
961 extern int Soft_emulate_8xx(struct pt_regs
*);
962 #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
966 CHECK_FULL_REGS(regs
);
968 if (!user_mode(regs
)) {
970 die("Kernel Mode Software FPU Emulation", regs
, SIGFPE
);
973 #ifdef CONFIG_MATH_EMULATION
974 errcode
= do_mathemu(regs
);
976 PPC_WARN_EMULATED(math
, regs
);
980 emulate_single_step(regs
);
984 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
985 _exception(SIGFPE
, regs
, code
, regs
->nip
);
989 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
992 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
996 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
997 errcode
= Soft_emulate_8xx(regs
);
999 PPC_WARN_EMULATED(8xx
, regs
);
1003 emulate_single_step(regs
);
1006 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1009 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
1013 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1016 #endif /* CONFIG_8xx */
1018 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1020 void __kprobes
DebugException(struct pt_regs
*regs
, unsigned long debug_status
)
1022 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1023 * on server, it stops on the target of the branch. In order to simulate
1024 * the server behaviour, we thus restart right away with a single step
1025 * instead of stopping here when hitting a BT
1027 if (debug_status
& DBSR_BT
) {
1028 regs
->msr
&= ~MSR_DE
;
1031 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) & ~DBCR0_BT
);
1032 /* Clear the BT event */
1033 mtspr(SPRN_DBSR
, DBSR_BT
);
1035 /* Do the single step trick only when coming from userspace */
1036 if (user_mode(regs
)) {
1037 current
->thread
.dbcr0
&= ~DBCR0_BT
;
1038 current
->thread
.dbcr0
|= DBCR0_IDM
| DBCR0_IC
;
1039 regs
->msr
|= MSR_DE
;
1043 if (notify_die(DIE_SSTEP
, "block_step", regs
, 5,
1044 5, SIGTRAP
) == NOTIFY_STOP
) {
1047 if (debugger_sstep(regs
))
1049 } else if (debug_status
& DBSR_IC
) { /* Instruction complete */
1050 regs
->msr
&= ~MSR_DE
;
1052 /* Disable instruction completion */
1053 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) & ~DBCR0_IC
);
1054 /* Clear the instruction completion event */
1055 mtspr(SPRN_DBSR
, DBSR_IC
);
1057 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
1058 5, SIGTRAP
) == NOTIFY_STOP
) {
1062 if (debugger_sstep(regs
))
1065 if (user_mode(regs
))
1066 current
->thread
.dbcr0
&= ~(DBCR0_IC
);
1068 _exception(SIGTRAP
, regs
, TRAP_TRACE
, regs
->nip
);
1069 } else if (debug_status
& (DBSR_DAC1R
| DBSR_DAC1W
)) {
1070 regs
->msr
&= ~MSR_DE
;
1072 if (user_mode(regs
)) {
1073 current
->thread
.dbcr0
&= ~(DBSR_DAC1R
| DBSR_DAC1W
|
1076 /* Disable DAC interupts */
1077 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) & ~(DBSR_DAC1R
|
1078 DBSR_DAC1W
| DBCR0_IDM
));
1080 /* Clear the DAC event */
1081 mtspr(SPRN_DBSR
, (DBSR_DAC1R
| DBSR_DAC1W
));
1083 /* Setup and send the trap to the handler */
1084 do_dabr(regs
, mfspr(SPRN_DAC1
), debug_status
);
1087 #endif /* CONFIG_4xx || CONFIG_BOOKE */
1089 #if !defined(CONFIG_TAU_INT)
1090 void TAUException(struct pt_regs
*regs
)
1092 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1093 regs
->nip
, regs
->msr
, regs
->trap
, print_tainted());
1095 #endif /* CONFIG_INT_TAU */
1097 #ifdef CONFIG_ALTIVEC
1098 void altivec_assist_exception(struct pt_regs
*regs
)
1102 if (!user_mode(regs
)) {
1103 printk(KERN_EMERG
"VMX/Altivec assist exception in kernel mode"
1104 " at %lx\n", regs
->nip
);
1105 die("Kernel VMX/Altivec assist exception", regs
, SIGILL
);
1108 flush_altivec_to_thread(current
);
1110 PPC_WARN_EMULATED(altivec
, regs
);
1111 err
= emulate_altivec(regs
);
1113 regs
->nip
+= 4; /* skip emulated instruction */
1114 emulate_single_step(regs
);
1118 if (err
== -EFAULT
) {
1119 /* got an error reading the instruction */
1120 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1122 /* didn't recognize the instruction */
1123 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1124 if (printk_ratelimit())
1125 printk(KERN_ERR
"Unrecognized altivec instruction "
1126 "in %s at %lx\n", current
->comm
, regs
->nip
);
1127 current
->thread
.vscr
.u
[3] |= 0x10000;
1130 #endif /* CONFIG_ALTIVEC */
1133 void vsx_assist_exception(struct pt_regs
*regs
)
1135 if (!user_mode(regs
)) {
1136 printk(KERN_EMERG
"VSX assist exception in kernel mode"
1137 " at %lx\n", regs
->nip
);
1138 die("Kernel VSX assist exception", regs
, SIGILL
);
1141 flush_vsx_to_thread(current
);
1142 printk(KERN_INFO
"VSX assist not supported at %lx\n", regs
->nip
);
1143 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1145 #endif /* CONFIG_VSX */
1147 #ifdef CONFIG_FSL_BOOKE
1149 void doorbell_exception(struct pt_regs
*regs
)
1152 int cpu
= smp_processor_id();
1155 if (num_online_cpus() < 2)
1158 for (msg
= 0; msg
< 4; msg
++)
1159 if (test_and_clear_bit(msg
, &dbell_smp_message
[cpu
]))
1160 smp_message_recv(msg
);
1162 printk(KERN_WARNING
"Received doorbell on non-smp system\n");
1166 void CacheLockingException(struct pt_regs
*regs
, unsigned long address
,
1167 unsigned long error_code
)
1169 /* We treat cache locking instructions from the user
1170 * as priv ops, in the future we could try to do
1173 if (error_code
& (ESR_DLK
|ESR_ILK
))
1174 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
1177 #endif /* CONFIG_FSL_BOOKE */
1180 void SPEFloatingPointException(struct pt_regs
*regs
)
1182 extern int do_spe_mathemu(struct pt_regs
*regs
);
1183 unsigned long spefscr
;
1189 if (regs
->msr
& MSR_SPE
)
1190 giveup_spe(current
);
1193 spefscr
= current
->thread
.spefscr
;
1194 fpexc_mode
= current
->thread
.fpexc_mode
;
1196 if ((spefscr
& SPEFSCR_FOVF
) && (fpexc_mode
& PR_FP_EXC_OVF
)) {
1199 else if ((spefscr
& SPEFSCR_FUNF
) && (fpexc_mode
& PR_FP_EXC_UND
)) {
1202 else if ((spefscr
& SPEFSCR_FDBZ
) && (fpexc_mode
& PR_FP_EXC_DIV
))
1204 else if ((spefscr
& SPEFSCR_FINV
) && (fpexc_mode
& PR_FP_EXC_INV
)) {
1207 else if ((spefscr
& (SPEFSCR_FG
| SPEFSCR_FX
)) && (fpexc_mode
& PR_FP_EXC_RES
))
1210 err
= do_spe_mathemu(regs
);
1212 regs
->nip
+= 4; /* skip emulated instruction */
1213 emulate_single_step(regs
);
1217 if (err
== -EFAULT
) {
1218 /* got an error reading the instruction */
1219 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1220 } else if (err
== -EINVAL
) {
1221 /* didn't recognize the instruction */
1222 printk(KERN_ERR
"unrecognized spe instruction "
1223 "in %s at %lx\n", current
->comm
, regs
->nip
);
1225 _exception(SIGFPE
, regs
, code
, regs
->nip
);
1231 void SPEFloatingPointRoundException(struct pt_regs
*regs
)
1233 extern int speround_handler(struct pt_regs
*regs
);
1237 if (regs
->msr
& MSR_SPE
)
1238 giveup_spe(current
);
1242 err
= speround_handler(regs
);
1244 regs
->nip
+= 4; /* skip emulated instruction */
1245 emulate_single_step(regs
);
1249 if (err
== -EFAULT
) {
1250 /* got an error reading the instruction */
1251 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1252 } else if (err
== -EINVAL
) {
1253 /* didn't recognize the instruction */
1254 printk(KERN_ERR
"unrecognized spe instruction "
1255 "in %s at %lx\n", current
->comm
, regs
->nip
);
1257 _exception(SIGFPE
, regs
, 0, regs
->nip
);
1264 * We enter here if we get an unrecoverable exception, that is, one
1265 * that happened at a point where the RI (recoverable interrupt) bit
1266 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1267 * we therefore lost state by taking this exception.
1269 void unrecoverable_exception(struct pt_regs
*regs
)
1271 printk(KERN_EMERG
"Unrecoverable exception %lx at %lx\n",
1272 regs
->trap
, regs
->nip
);
1273 die("Unrecoverable exception", regs
, SIGABRT
);
1276 #ifdef CONFIG_BOOKE_WDT
1278 * Default handler for a Watchdog exception,
1279 * spins until a reboot occurs
1281 void __attribute__ ((weak
)) WatchdogHandler(struct pt_regs
*regs
)
1283 /* Generic WatchdogHandler, implement your own */
1284 mtspr(SPRN_TCR
, mfspr(SPRN_TCR
)&(~TCR_WIE
));
1288 void WatchdogException(struct pt_regs
*regs
)
1290 printk (KERN_EMERG
"PowerPC Book-E Watchdog Exception\n");
1291 WatchdogHandler(regs
);
1296 * We enter here if we discover during exception entry that we are
1297 * running in supervisor mode with a userspace value in the stack pointer.
1299 void kernel_bad_stack(struct pt_regs
*regs
)
1301 printk(KERN_EMERG
"Bad kernel stack pointer %lx at %lx\n",
1302 regs
->gpr
[1], regs
->nip
);
1303 die("Bad kernel stack pointer", regs
, SIGABRT
);
1306 void __init
trap_init(void)
1311 #ifdef CONFIG_PPC_EMULATED_STATS
1313 #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1315 struct ppc_emulated ppc_emulated
= {
1316 #ifdef CONFIG_ALTIVEC
1317 WARN_EMULATED_SETUP(altivec
),
1319 WARN_EMULATED_SETUP(dcba
),
1320 WARN_EMULATED_SETUP(dcbz
),
1321 WARN_EMULATED_SETUP(fp_pair
),
1322 WARN_EMULATED_SETUP(isel
),
1323 WARN_EMULATED_SETUP(mcrxr
),
1324 WARN_EMULATED_SETUP(mfpvr
),
1325 WARN_EMULATED_SETUP(multiple
),
1326 WARN_EMULATED_SETUP(popcntb
),
1327 WARN_EMULATED_SETUP(spe
),
1328 WARN_EMULATED_SETUP(string
),
1329 WARN_EMULATED_SETUP(unaligned
),
1330 #ifdef CONFIG_MATH_EMULATION
1331 WARN_EMULATED_SETUP(math
),
1332 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1333 WARN_EMULATED_SETUP(8xx
),
1336 WARN_EMULATED_SETUP(vsx
),
1340 u32 ppc_warn_emulated
;
1342 void ppc_warn_emulated_print(const char *type
)
1344 if (printk_ratelimit())
1345 pr_warning("%s used emulated %s instruction\n", current
->comm
,
1349 static int __init
ppc_warn_emulated_init(void)
1351 struct dentry
*dir
, *d
;
1353 struct ppc_emulated_entry
*entries
= (void *)&ppc_emulated
;
1355 if (!powerpc_debugfs_root
)
1358 dir
= debugfs_create_dir("emulated_instructions",
1359 powerpc_debugfs_root
);
1363 d
= debugfs_create_u32("do_warn", S_IRUGO
| S_IWUSR
, dir
,
1364 &ppc_warn_emulated
);
1368 for (i
= 0; i
< sizeof(ppc_emulated
)/sizeof(*entries
); i
++) {
1369 d
= debugfs_create_u32(entries
[i
].name
, S_IRUGO
| S_IWUSR
, dir
,
1370 (u32
*)&entries
[i
].val
.counter
);
1378 debugfs_remove_recursive(dir
);
1382 device_initcall(ppc_warn_emulated_init
);
1384 #endif /* CONFIG_PPC_EMULATED_STATS */