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>
37 #include <asm/pgtable.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
41 #include <asm/machdep.h>
47 #ifdef CONFIG_PMAC_BACKLIGHT
48 #include <asm/backlight.h>
51 #include <asm/firmware.h>
52 #include <asm/processor.h>
54 #include <asm/kexec.h>
56 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
57 int (*__debugger
)(struct pt_regs
*regs
);
58 int (*__debugger_ipi
)(struct pt_regs
*regs
);
59 int (*__debugger_bpt
)(struct pt_regs
*regs
);
60 int (*__debugger_sstep
)(struct pt_regs
*regs
);
61 int (*__debugger_iabr_match
)(struct pt_regs
*regs
);
62 int (*__debugger_dabr_match
)(struct pt_regs
*regs
);
63 int (*__debugger_fault_handler
)(struct pt_regs
*regs
);
65 EXPORT_SYMBOL(__debugger
);
66 EXPORT_SYMBOL(__debugger_ipi
);
67 EXPORT_SYMBOL(__debugger_bpt
);
68 EXPORT_SYMBOL(__debugger_sstep
);
69 EXPORT_SYMBOL(__debugger_iabr_match
);
70 EXPORT_SYMBOL(__debugger_dabr_match
);
71 EXPORT_SYMBOL(__debugger_fault_handler
);
75 * Trap & Exception support
78 #ifdef CONFIG_PMAC_BACKLIGHT
79 static void pmac_backlight_unblank(void)
81 mutex_lock(&pmac_backlight_mutex
);
83 struct backlight_properties
*props
;
85 props
= &pmac_backlight
->props
;
86 props
->brightness
= props
->max_brightness
;
87 props
->power
= FB_BLANK_UNBLANK
;
88 backlight_update_status(pmac_backlight
);
90 mutex_unlock(&pmac_backlight_mutex
);
93 static inline void pmac_backlight_unblank(void) { }
96 int die(const char *str
, struct pt_regs
*regs
, long err
)
101 int lock_owner_depth
;
103 .lock
= __SPIN_LOCK_UNLOCKED(die
.lock
),
105 .lock_owner_depth
= 0
107 static int die_counter
;
115 if (die
.lock_owner
!= raw_smp_processor_id()) {
117 spin_lock_irqsave(&die
.lock
, flags
);
118 die
.lock_owner
= smp_processor_id();
119 die
.lock_owner_depth
= 0;
121 if (machine_is(powermac
))
122 pmac_backlight_unblank();
124 local_save_flags(flags
);
127 if (++die
.lock_owner_depth
< 3) {
128 printk("Oops: %s, sig: %ld [#%d]\n", str
, err
, ++die_counter
);
129 #ifdef CONFIG_PREEMPT
133 printk("SMP NR_CPUS=%d ", NR_CPUS
);
135 #ifdef CONFIG_DEBUG_PAGEALLOC
136 printk("DEBUG_PAGEALLOC ");
141 printk("%s\n", ppc_md
.name
? ppc_md
.name
: "");
146 printk("Recursive die() failure, output suppressed\n");
151 add_taint(TAINT_DIE
);
152 spin_unlock_irqrestore(&die
.lock
, flags
);
154 if (kexec_should_crash(current
) ||
155 kexec_sr_activated(smp_processor_id()))
157 crash_kexec_secondary(regs
);
160 panic("Fatal exception in interrupt");
163 panic("Fatal exception");
171 void _exception(int signr
, struct pt_regs
*regs
, int code
, unsigned long addr
)
174 const char fmt32
[] = KERN_INFO
"%s[%d]: unhandled signal %d " \
175 "at %08lx nip %08lx lr %08lx code %x\n";
176 const char fmt64
[] = KERN_INFO
"%s[%d]: unhandled signal %d " \
177 "at %016lx nip %016lx lr %016lx code %x\n";
179 if (!user_mode(regs
)) {
180 if (die("Exception in kernel mode", regs
, signr
))
182 } else if (show_unhandled_signals
&&
183 unhandled_signal(current
, signr
) &&
184 printk_ratelimit()) {
185 printk(regs
->msr
& MSR_SF
? fmt64
: fmt32
,
186 current
->comm
, current
->pid
, signr
,
187 addr
, regs
->nip
, regs
->link
, code
);
190 memset(&info
, 0, sizeof(info
));
191 info
.si_signo
= signr
;
193 info
.si_addr
= (void __user
*) addr
;
194 force_sig_info(signr
, &info
, current
);
197 * Init gets no signals that it doesn't have a handler for.
198 * That's all very well, but if it has caused a synchronous
199 * exception and we ignore the resulting signal, it will just
200 * generate the same exception over and over again and we get
201 * nowhere. Better to kill it and let the kernel panic.
203 if (is_global_init(current
)) {
204 __sighandler_t handler
;
206 spin_lock_irq(¤t
->sighand
->siglock
);
207 handler
= current
->sighand
->action
[signr
-1].sa
.sa_handler
;
208 spin_unlock_irq(¤t
->sighand
->siglock
);
209 if (handler
== SIG_DFL
) {
210 /* init has generated a synchronous exception
211 and it doesn't have a handler for the signal */
212 printk(KERN_CRIT
"init has generated signal %d "
213 "but has no handler for it\n", signr
);
220 void system_reset_exception(struct pt_regs
*regs
)
222 /* See if any machine dependent calls */
223 if (ppc_md
.system_reset_exception
) {
224 if (ppc_md
.system_reset_exception(regs
))
229 cpu_set(smp_processor_id(), cpus_in_sr
);
232 die("System Reset", regs
, SIGABRT
);
235 * Some CPUs when released from the debugger will execute this path.
236 * These CPUs entered the debugger via a soft-reset. If the CPU was
237 * hung before entering the debugger it will return to the hung
238 * state when exiting this function. This causes a problem in
239 * kdump since the hung CPU(s) will not respond to the IPI sent
240 * from kdump. To prevent the problem we call crash_kexec_secondary()
241 * here. If a kdump had not been initiated or we exit the debugger
242 * with the "exit and recover" command (x) crash_kexec_secondary()
243 * will return after 5ms and the CPU returns to its previous state.
245 crash_kexec_secondary(regs
);
247 /* Must die if the interrupt is not recoverable */
248 if (!(regs
->msr
& MSR_RI
))
249 panic("Unrecoverable System Reset");
251 /* What should we do here? We could issue a shutdown or hard reset. */
256 * I/O accesses can cause machine checks on powermacs.
257 * Check if the NIP corresponds to the address of a sync
258 * instruction for which there is an entry in the exception
260 * Note that the 601 only takes a machine check on TEA
261 * (transfer error ack) signal assertion, and does not
262 * set any of the top 16 bits of SRR1.
265 static inline int check_io_access(struct pt_regs
*regs
)
268 unsigned long msr
= regs
->msr
;
269 const struct exception_table_entry
*entry
;
270 unsigned int *nip
= (unsigned int *)regs
->nip
;
272 if (((msr
& 0xffff0000) == 0 || (msr
& (0x80000 | 0x40000)))
273 && (entry
= search_exception_tables(regs
->nip
)) != NULL
) {
275 * Check that it's a sync instruction, or somewhere
276 * in the twi; isync; nop sequence that inb/inw/inl uses.
277 * As the address is in the exception table
278 * we should be able to read the instr there.
279 * For the debug message, we look at the preceding
282 if (*nip
== 0x60000000) /* nop */
284 else if (*nip
== 0x4c00012c) /* isync */
286 if (*nip
== 0x7c0004ac || (*nip
>> 26) == 3) {
291 rb
= (*nip
>> 11) & 0x1f;
292 printk(KERN_DEBUG
"%s bad port %lx at %p\n",
293 (*nip
& 0x100)? "OUT to": "IN from",
294 regs
->gpr
[rb
] - _IO_BASE
, nip
);
296 regs
->nip
= entry
->fixup
;
300 #endif /* CONFIG_PPC32 */
304 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
305 /* On 4xx, the reason for the machine check or program exception
307 #define get_reason(regs) ((regs)->dsisr)
308 #ifndef CONFIG_FSL_BOOKE
309 #define get_mc_reason(regs) ((regs)->dsisr)
311 #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
313 #define REASON_FP ESR_FP
314 #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
315 #define REASON_PRIVILEGED ESR_PPR
316 #define REASON_TRAP ESR_PTR
318 /* single-step stuff */
319 #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
320 #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
323 /* On non-4xx, the reason for the machine check or program
324 exception is in the MSR. */
325 #define get_reason(regs) ((regs)->msr)
326 #define get_mc_reason(regs) ((regs)->msr)
327 #define REASON_FP 0x100000
328 #define REASON_ILLEGAL 0x80000
329 #define REASON_PRIVILEGED 0x40000
330 #define REASON_TRAP 0x20000
332 #define single_stepping(regs) ((regs)->msr & MSR_SE)
333 #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
336 #if defined(CONFIG_4xx)
337 int machine_check_4xx(struct pt_regs
*regs
)
339 unsigned long reason
= get_mc_reason(regs
);
341 if (reason
& ESR_IMCP
) {
342 printk("Instruction");
343 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
346 printk(" machine check in kernel mode.\n");
351 int machine_check_440A(struct pt_regs
*regs
)
353 unsigned long reason
= get_mc_reason(regs
);
355 printk("Machine check in kernel mode.\n");
356 if (reason
& ESR_IMCP
){
357 printk("Instruction Synchronous Machine Check exception\n");
358 mtspr(SPRN_ESR
, reason
& ~ESR_IMCP
);
361 u32 mcsr
= mfspr(SPRN_MCSR
);
363 printk("Instruction Read PLB Error\n");
365 printk("Data Read PLB Error\n");
367 printk("Data Write PLB Error\n");
368 if (mcsr
& MCSR_TLBP
)
369 printk("TLB Parity Error\n");
370 if (mcsr
& MCSR_ICP
){
371 flush_instruction_cache();
372 printk("I-Cache Parity Error\n");
374 if (mcsr
& MCSR_DCSP
)
375 printk("D-Cache Search Parity Error\n");
376 if (mcsr
& MCSR_DCFP
)
377 printk("D-Cache Flush Parity Error\n");
378 if (mcsr
& MCSR_IMPE
)
379 printk("Machine Check exception is imprecise\n");
382 mtspr(SPRN_MCSR
, mcsr
);
386 #elif defined(CONFIG_E500)
387 int machine_check_e500(struct pt_regs
*regs
)
389 unsigned long reason
= get_mc_reason(regs
);
391 printk("Machine check in kernel mode.\n");
392 printk("Caused by (from MCSR=%lx): ", reason
);
394 if (reason
& MCSR_MCP
)
395 printk("Machine Check Signal\n");
396 if (reason
& MCSR_ICPERR
)
397 printk("Instruction Cache Parity Error\n");
398 if (reason
& MCSR_DCP_PERR
)
399 printk("Data Cache Push Parity Error\n");
400 if (reason
& MCSR_DCPERR
)
401 printk("Data Cache Parity Error\n");
402 if (reason
& MCSR_BUS_IAERR
)
403 printk("Bus - Instruction Address Error\n");
404 if (reason
& MCSR_BUS_RAERR
)
405 printk("Bus - Read Address Error\n");
406 if (reason
& MCSR_BUS_WAERR
)
407 printk("Bus - Write Address Error\n");
408 if (reason
& MCSR_BUS_IBERR
)
409 printk("Bus - Instruction Data Error\n");
410 if (reason
& MCSR_BUS_RBERR
)
411 printk("Bus - Read Data Bus Error\n");
412 if (reason
& MCSR_BUS_WBERR
)
413 printk("Bus - Read Data Bus Error\n");
414 if (reason
& MCSR_BUS_IPERR
)
415 printk("Bus - Instruction Parity Error\n");
416 if (reason
& MCSR_BUS_RPERR
)
417 printk("Bus - Read Parity Error\n");
421 #elif defined(CONFIG_E200)
422 int machine_check_e200(struct pt_regs
*regs
)
424 unsigned long reason
= get_mc_reason(regs
);
426 printk("Machine check in kernel mode.\n");
427 printk("Caused by (from MCSR=%lx): ", reason
);
429 if (reason
& MCSR_MCP
)
430 printk("Machine Check Signal\n");
431 if (reason
& MCSR_CP_PERR
)
432 printk("Cache Push Parity Error\n");
433 if (reason
& MCSR_CPERR
)
434 printk("Cache Parity Error\n");
435 if (reason
& MCSR_EXCP_ERR
)
436 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
437 if (reason
& MCSR_BUS_IRERR
)
438 printk("Bus - Read Bus Error on instruction fetch\n");
439 if (reason
& MCSR_BUS_DRERR
)
440 printk("Bus - Read Bus Error on data load\n");
441 if (reason
& MCSR_BUS_WRERR
)
442 printk("Bus - Write Bus Error on buffered store or cache line push\n");
447 int machine_check_generic(struct pt_regs
*regs
)
449 unsigned long reason
= get_mc_reason(regs
);
451 printk("Machine check in kernel mode.\n");
452 printk("Caused by (from SRR1=%lx): ", reason
);
453 switch (reason
& 0x601F0000) {
455 printk("Machine check signal\n");
457 case 0: /* for 601 */
459 case 0x140000: /* 7450 MSS error and TEA */
460 printk("Transfer error ack signal\n");
463 printk("Data parity error signal\n");
466 printk("Address parity error signal\n");
469 printk("L1 Data Cache error\n");
472 printk("L1 Instruction Cache error\n");
475 printk("L2 data cache parity error\n");
478 printk("Unknown values in msr\n");
482 #endif /* everything else */
484 void machine_check_exception(struct pt_regs
*regs
)
488 /* See if any machine dependent calls. In theory, we would want
489 * to call the CPU first, and call the ppc_md. one if the CPU
490 * one returns a positive number. However there is existing code
491 * that assumes the board gets a first chance, so let's keep it
492 * that way for now and fix things later. --BenH.
494 if (ppc_md
.machine_check_exception
)
495 recover
= ppc_md
.machine_check_exception(regs
);
496 else if (cur_cpu_spec
->machine_check
)
497 recover
= cur_cpu_spec
->machine_check(regs
);
502 if (user_mode(regs
)) {
504 _exception(SIGBUS
, regs
, BUS_ADRERR
, regs
->nip
);
508 #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
509 /* the qspan pci read routines can cause machine checks -- Cort
511 * yuck !!! that totally needs to go away ! There are better ways
512 * to deal with that than having a wart in the mcheck handler.
515 bad_page_fault(regs
, regs
->dar
, SIGBUS
);
519 if (debugger_fault_handler(regs
)) {
524 if (check_io_access(regs
))
527 if (debugger_fault_handler(regs
))
529 die("Machine check", regs
, SIGBUS
);
531 /* Must die if the interrupt is not recoverable */
532 if (!(regs
->msr
& MSR_RI
))
533 panic("Unrecoverable Machine check");
536 void SMIException(struct pt_regs
*regs
)
538 die("System Management Interrupt", regs
, SIGABRT
);
541 void unknown_exception(struct pt_regs
*regs
)
543 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
544 regs
->nip
, regs
->msr
, regs
->trap
);
546 _exception(SIGTRAP
, regs
, 0, 0);
549 void instruction_breakpoint_exception(struct pt_regs
*regs
)
551 if (notify_die(DIE_IABR_MATCH
, "iabr_match", regs
, 5,
552 5, SIGTRAP
) == NOTIFY_STOP
)
554 if (debugger_iabr_match(regs
))
556 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
559 void RunModeException(struct pt_regs
*regs
)
561 _exception(SIGTRAP
, regs
, 0, 0);
564 void __kprobes
single_step_exception(struct pt_regs
*regs
)
566 regs
->msr
&= ~(MSR_SE
| MSR_BE
); /* Turn off 'trace' bits */
568 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
569 5, SIGTRAP
) == NOTIFY_STOP
)
571 if (debugger_sstep(regs
))
574 _exception(SIGTRAP
, regs
, TRAP_TRACE
, regs
->nip
);
578 * After we have successfully emulated an instruction, we have to
579 * check if the instruction was being single-stepped, and if so,
580 * pretend we got a single-step exception. This was pointed out
581 * by Kumar Gala. -- paulus
583 static void emulate_single_step(struct pt_regs
*regs
)
585 if (single_stepping(regs
)) {
586 clear_single_step(regs
);
587 _exception(SIGTRAP
, regs
, TRAP_TRACE
, 0);
591 static inline int __parse_fpscr(unsigned long fpscr
)
595 /* Invalid operation */
596 if ((fpscr
& FPSCR_VE
) && (fpscr
& FPSCR_VX
))
600 else if ((fpscr
& FPSCR_OE
) && (fpscr
& FPSCR_OX
))
604 else if ((fpscr
& FPSCR_UE
) && (fpscr
& FPSCR_UX
))
608 else if ((fpscr
& FPSCR_ZE
) && (fpscr
& FPSCR_ZX
))
612 else if ((fpscr
& FPSCR_XE
) && (fpscr
& FPSCR_XX
))
618 static void parse_fpe(struct pt_regs
*regs
)
622 flush_fp_to_thread(current
);
624 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
626 _exception(SIGFPE
, regs
, code
, regs
->nip
);
630 * Illegal instruction emulation support. Originally written to
631 * provide the PVR to user applications using the mfspr rd, PVR.
632 * Return non-zero if we can't emulate, or -EFAULT if the associated
633 * memory access caused an access fault. Return zero on success.
635 * There are a couple of ways to do this, either "decode" the instruction
636 * or directly match lots of bits. In this case, matching lots of
637 * bits is faster and easier.
640 #define INST_MFSPR_PVR 0x7c1f42a6
641 #define INST_MFSPR_PVR_MASK 0xfc1fffff
643 #define INST_DCBA 0x7c0005ec
644 #define INST_DCBA_MASK 0xfc0007fe
646 #define INST_MCRXR 0x7c000400
647 #define INST_MCRXR_MASK 0xfc0007fe
649 #define INST_STRING 0x7c00042a
650 #define INST_STRING_MASK 0xfc0007fe
651 #define INST_STRING_GEN_MASK 0xfc00067e
652 #define INST_LSWI 0x7c0004aa
653 #define INST_LSWX 0x7c00042a
654 #define INST_STSWI 0x7c0005aa
655 #define INST_STSWX 0x7c00052a
657 #define INST_POPCNTB 0x7c0000f4
658 #define INST_POPCNTB_MASK 0xfc0007fe
660 #define INST_ISEL 0x7c00001e
661 #define INST_ISEL_MASK 0xfc00003e
663 static int emulate_string_inst(struct pt_regs
*regs
, u32 instword
)
665 u8 rT
= (instword
>> 21) & 0x1f;
666 u8 rA
= (instword
>> 16) & 0x1f;
667 u8 NB_RB
= (instword
>> 11) & 0x1f;
672 /* Early out if we are an invalid form of lswx */
673 if ((instword
& INST_STRING_MASK
) == INST_LSWX
)
674 if ((rT
== rA
) || (rT
== NB_RB
))
677 EA
= (rA
== 0) ? 0 : regs
->gpr
[rA
];
679 switch (instword
& INST_STRING_MASK
) {
683 num_bytes
= regs
->xer
& 0x7f;
687 num_bytes
= (NB_RB
== 0) ? 32 : NB_RB
;
693 while (num_bytes
!= 0)
696 u32 shift
= 8 * (3 - (pos
& 0x3));
698 switch ((instword
& INST_STRING_MASK
)) {
701 if (get_user(val
, (u8 __user
*)EA
))
703 /* first time updating this reg,
707 regs
->gpr
[rT
] |= val
<< shift
;
711 val
= regs
->gpr
[rT
] >> shift
;
712 if (put_user(val
, (u8 __user
*)EA
))
716 /* move EA to next address */
720 /* manage our position within the register */
731 static int emulate_popcntb_inst(struct pt_regs
*regs
, u32 instword
)
736 ra
= (instword
>> 16) & 0x1f;
737 rs
= (instword
>> 21) & 0x1f;
740 tmp
= tmp
- ((tmp
>> 1) & 0x5555555555555555ULL
);
741 tmp
= (tmp
& 0x3333333333333333ULL
) + ((tmp
>> 2) & 0x3333333333333333ULL
);
742 tmp
= (tmp
+ (tmp
>> 4)) & 0x0f0f0f0f0f0f0f0fULL
;
748 static int emulate_isel(struct pt_regs
*regs
, u32 instword
)
750 u8 rT
= (instword
>> 21) & 0x1f;
751 u8 rA
= (instword
>> 16) & 0x1f;
752 u8 rB
= (instword
>> 11) & 0x1f;
753 u8 BC
= (instword
>> 6) & 0x1f;
757 tmp
= (rA
== 0) ? 0 : regs
->gpr
[rA
];
758 bit
= (regs
->ccr
>> (31 - BC
)) & 0x1;
760 regs
->gpr
[rT
] = bit
? tmp
: regs
->gpr
[rB
];
765 static int emulate_instruction(struct pt_regs
*regs
)
770 if (!user_mode(regs
) || (regs
->msr
& MSR_LE
))
772 CHECK_FULL_REGS(regs
);
774 if (get_user(instword
, (u32 __user
*)(regs
->nip
)))
777 /* Emulate the mfspr rD, PVR. */
778 if ((instword
& INST_MFSPR_PVR_MASK
) == INST_MFSPR_PVR
) {
779 rd
= (instword
>> 21) & 0x1f;
780 regs
->gpr
[rd
] = mfspr(SPRN_PVR
);
784 /* Emulating the dcba insn is just a no-op. */
785 if ((instword
& INST_DCBA_MASK
) == INST_DCBA
)
788 /* Emulate the mcrxr insn. */
789 if ((instword
& INST_MCRXR_MASK
) == INST_MCRXR
) {
790 int shift
= (instword
>> 21) & 0x1c;
791 unsigned long msk
= 0xf0000000UL
>> shift
;
793 regs
->ccr
= (regs
->ccr
& ~msk
) | ((regs
->xer
>> shift
) & msk
);
794 regs
->xer
&= ~0xf0000000UL
;
798 /* Emulate load/store string insn. */
799 if ((instword
& INST_STRING_GEN_MASK
) == INST_STRING
)
800 return emulate_string_inst(regs
, instword
);
802 /* Emulate the popcntb (Population Count Bytes) instruction. */
803 if ((instword
& INST_POPCNTB_MASK
) == INST_POPCNTB
) {
804 return emulate_popcntb_inst(regs
, instword
);
807 /* Emulate isel (Integer Select) instruction */
808 if ((instword
& INST_ISEL_MASK
) == INST_ISEL
) {
809 return emulate_isel(regs
, instword
);
815 int is_valid_bugaddr(unsigned long addr
)
817 return is_kernel_addr(addr
);
820 void __kprobes
program_check_exception(struct pt_regs
*regs
)
822 unsigned int reason
= get_reason(regs
);
823 extern int do_mathemu(struct pt_regs
*regs
);
825 /* We can now get here via a FP Unavailable exception if the core
826 * has no FPU, in that case the reason flags will be 0 */
828 if (reason
& REASON_FP
) {
829 /* IEEE FP exception */
833 if (reason
& REASON_TRAP
) {
835 if (notify_die(DIE_BPT
, "breakpoint", regs
, 5, 5, SIGTRAP
)
838 if (debugger_bpt(regs
))
841 if (!(regs
->msr
& MSR_PR
) && /* not user-mode */
842 report_bug(regs
->nip
, regs
) == BUG_TRAP_TYPE_WARN
) {
846 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
852 #ifdef CONFIG_MATH_EMULATION
853 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
854 * but there seems to be a hardware bug on the 405GP (RevD)
855 * that means ESR is sometimes set incorrectly - either to
856 * ESR_DST (!?) or 0. In the process of chasing this with the
857 * hardware people - not sure if it can happen on any illegal
858 * instruction or only on FP instructions, whether there is a
859 * pattern to occurences etc. -dgibson 31/Mar/2003 */
860 switch (do_mathemu(regs
)) {
862 emulate_single_step(regs
);
866 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
867 _exception(SIGFPE
, regs
, code
, regs
->nip
);
871 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
874 /* fall through on any other errors */
875 #endif /* CONFIG_MATH_EMULATION */
877 /* Try to emulate it if we should. */
878 if (reason
& (REASON_ILLEGAL
| REASON_PRIVILEGED
)) {
879 switch (emulate_instruction(regs
)) {
882 emulate_single_step(regs
);
885 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
890 if (reason
& REASON_PRIVILEGED
)
891 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
893 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
896 void alignment_exception(struct pt_regs
*regs
)
898 int sig
, code
, fixed
= 0;
900 /* we don't implement logging of alignment exceptions */
901 if (!(current
->thread
.align_ctl
& PR_UNALIGN_SIGBUS
))
902 fixed
= fix_alignment(regs
);
905 regs
->nip
+= 4; /* skip over emulated instruction */
906 emulate_single_step(regs
);
910 /* Operand address was bad */
911 if (fixed
== -EFAULT
) {
919 _exception(sig
, regs
, code
, regs
->dar
);
921 bad_page_fault(regs
, regs
->dar
, sig
);
924 void StackOverflow(struct pt_regs
*regs
)
926 printk(KERN_CRIT
"Kernel stack overflow in process %p, r1=%lx\n",
927 current
, regs
->gpr
[1]);
930 panic("kernel stack overflow");
933 void nonrecoverable_exception(struct pt_regs
*regs
)
935 printk(KERN_ERR
"Non-recoverable exception at PC=%lx MSR=%lx\n",
936 regs
->nip
, regs
->msr
);
938 die("nonrecoverable exception", regs
, SIGKILL
);
941 void trace_syscall(struct pt_regs
*regs
)
943 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
944 current
, task_pid_nr(current
), regs
->nip
, regs
->link
, regs
->gpr
[0],
945 regs
->ccr
&0x10000000?"Error=":"", regs
->gpr
[3], print_tainted());
948 void kernel_fp_unavailable_exception(struct pt_regs
*regs
)
950 printk(KERN_EMERG
"Unrecoverable FP Unavailable Exception "
951 "%lx at %lx\n", regs
->trap
, regs
->nip
);
952 die("Unrecoverable FP Unavailable Exception", regs
, SIGABRT
);
955 void altivec_unavailable_exception(struct pt_regs
*regs
)
957 if (user_mode(regs
)) {
958 /* A user program has executed an altivec instruction,
959 but this kernel doesn't support altivec. */
960 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
964 printk(KERN_EMERG
"Unrecoverable VMX/Altivec Unavailable Exception "
965 "%lx at %lx\n", regs
->trap
, regs
->nip
);
966 die("Unrecoverable VMX/Altivec Unavailable Exception", regs
, SIGABRT
);
969 void vsx_unavailable_exception(struct pt_regs
*regs
)
971 if (user_mode(regs
)) {
972 /* A user program has executed an vsx instruction,
973 but this kernel doesn't support vsx. */
974 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
978 printk(KERN_EMERG
"Unrecoverable VSX Unavailable Exception "
979 "%lx at %lx\n", regs
->trap
, regs
->nip
);
980 die("Unrecoverable VSX Unavailable Exception", regs
, SIGABRT
);
983 void performance_monitor_exception(struct pt_regs
*regs
)
989 void SoftwareEmulation(struct pt_regs
*regs
)
991 extern int do_mathemu(struct pt_regs
*);
992 extern int Soft_emulate_8xx(struct pt_regs
*);
993 #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
997 CHECK_FULL_REGS(regs
);
999 if (!user_mode(regs
)) {
1001 die("Kernel Mode Software FPU Emulation", regs
, SIGFPE
);
1004 #ifdef CONFIG_MATH_EMULATION
1005 errcode
= do_mathemu(regs
);
1009 emulate_single_step(regs
);
1013 code
= __parse_fpscr(current
->thread
.fpscr
.val
);
1014 _exception(SIGFPE
, regs
, code
, regs
->nip
);
1018 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
1021 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1025 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1026 errcode
= Soft_emulate_8xx(regs
);
1029 emulate_single_step(regs
);
1032 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1035 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
1039 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1042 #endif /* CONFIG_8xx */
1044 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
1046 void __kprobes
DebugException(struct pt_regs
*regs
, unsigned long debug_status
)
1048 if (debug_status
& DBSR_IC
) { /* instruction completion */
1049 regs
->msr
&= ~MSR_DE
;
1051 /* Disable instruction completion */
1052 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) & ~DBCR0_IC
);
1053 /* Clear the instruction completion event */
1054 mtspr(SPRN_DBSR
, DBSR_IC
);
1056 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
1057 5, SIGTRAP
) == NOTIFY_STOP
) {
1061 if (debugger_sstep(regs
))
1064 if (user_mode(regs
)) {
1065 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 err
= emulate_altivec(regs
);
1112 regs
->nip
+= 4; /* skip emulated instruction */
1113 emulate_single_step(regs
);
1117 if (err
== -EFAULT
) {
1118 /* got an error reading the instruction */
1119 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1121 /* didn't recognize the instruction */
1122 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1123 if (printk_ratelimit())
1124 printk(KERN_ERR
"Unrecognized altivec instruction "
1125 "in %s at %lx\n", current
->comm
, regs
->nip
);
1126 current
->thread
.vscr
.u
[3] |= 0x10000;
1129 #endif /* CONFIG_ALTIVEC */
1132 void vsx_assist_exception(struct pt_regs
*regs
)
1134 if (!user_mode(regs
)) {
1135 printk(KERN_EMERG
"VSX assist exception in kernel mode"
1136 " at %lx\n", regs
->nip
);
1137 die("Kernel VSX assist exception", regs
, SIGILL
);
1140 flush_vsx_to_thread(current
);
1141 printk(KERN_INFO
"VSX assist not supported at %lx\n", regs
->nip
);
1142 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
1144 #endif /* CONFIG_VSX */
1146 #ifdef CONFIG_FSL_BOOKE
1147 void CacheLockingException(struct pt_regs
*regs
, unsigned long address
,
1148 unsigned long error_code
)
1150 /* We treat cache locking instructions from the user
1151 * as priv ops, in the future we could try to do
1154 if (error_code
& (ESR_DLK
|ESR_ILK
))
1155 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
1158 #endif /* CONFIG_FSL_BOOKE */
1161 void SPEFloatingPointException(struct pt_regs
*regs
)
1163 extern int do_spe_mathemu(struct pt_regs
*regs
);
1164 unsigned long spefscr
;
1170 if (regs
->msr
& MSR_SPE
)
1171 giveup_spe(current
);
1174 spefscr
= current
->thread
.spefscr
;
1175 fpexc_mode
= current
->thread
.fpexc_mode
;
1177 if ((spefscr
& SPEFSCR_FOVF
) && (fpexc_mode
& PR_FP_EXC_OVF
)) {
1180 else if ((spefscr
& SPEFSCR_FUNF
) && (fpexc_mode
& PR_FP_EXC_UND
)) {
1183 else if ((spefscr
& SPEFSCR_FDBZ
) && (fpexc_mode
& PR_FP_EXC_DIV
))
1185 else if ((spefscr
& SPEFSCR_FINV
) && (fpexc_mode
& PR_FP_EXC_INV
)) {
1188 else if ((spefscr
& (SPEFSCR_FG
| SPEFSCR_FX
)) && (fpexc_mode
& PR_FP_EXC_RES
))
1191 err
= do_spe_mathemu(regs
);
1193 regs
->nip
+= 4; /* skip emulated instruction */
1194 emulate_single_step(regs
);
1198 if (err
== -EFAULT
) {
1199 /* got an error reading the instruction */
1200 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1201 } else if (err
== -EINVAL
) {
1202 /* didn't recognize the instruction */
1203 printk(KERN_ERR
"unrecognized spe instruction "
1204 "in %s at %lx\n", current
->comm
, regs
->nip
);
1206 _exception(SIGFPE
, regs
, code
, regs
->nip
);
1212 void SPEFloatingPointRoundException(struct pt_regs
*regs
)
1214 extern int speround_handler(struct pt_regs
*regs
);
1218 if (regs
->msr
& MSR_SPE
)
1219 giveup_spe(current
);
1223 err
= speround_handler(regs
);
1225 regs
->nip
+= 4; /* skip emulated instruction */
1226 emulate_single_step(regs
);
1230 if (err
== -EFAULT
) {
1231 /* got an error reading the instruction */
1232 _exception(SIGSEGV
, regs
, SEGV_ACCERR
, regs
->nip
);
1233 } else if (err
== -EINVAL
) {
1234 /* didn't recognize the instruction */
1235 printk(KERN_ERR
"unrecognized spe instruction "
1236 "in %s at %lx\n", current
->comm
, regs
->nip
);
1238 _exception(SIGFPE
, regs
, 0, regs
->nip
);
1245 * We enter here if we get an unrecoverable exception, that is, one
1246 * that happened at a point where the RI (recoverable interrupt) bit
1247 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1248 * we therefore lost state by taking this exception.
1250 void unrecoverable_exception(struct pt_regs
*regs
)
1252 printk(KERN_EMERG
"Unrecoverable exception %lx at %lx\n",
1253 regs
->trap
, regs
->nip
);
1254 die("Unrecoverable exception", regs
, SIGABRT
);
1257 #ifdef CONFIG_BOOKE_WDT
1259 * Default handler for a Watchdog exception,
1260 * spins until a reboot occurs
1262 void __attribute__ ((weak
)) WatchdogHandler(struct pt_regs
*regs
)
1264 /* Generic WatchdogHandler, implement your own */
1265 mtspr(SPRN_TCR
, mfspr(SPRN_TCR
)&(~TCR_WIE
));
1269 void WatchdogException(struct pt_regs
*regs
)
1271 printk (KERN_EMERG
"PowerPC Book-E Watchdog Exception\n");
1272 WatchdogHandler(regs
);
1277 * We enter here if we discover during exception entry that we are
1278 * running in supervisor mode with a userspace value in the stack pointer.
1280 void kernel_bad_stack(struct pt_regs
*regs
)
1282 printk(KERN_EMERG
"Bad kernel stack pointer %lx at %lx\n",
1283 regs
->gpr
[1], regs
->nip
);
1284 die("Bad kernel stack pointer", regs
, SIGABRT
);
1287 void __init
trap_init(void)