2 * linux/arch/ppc64/kernel/traps.c
4 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * Modified by Cort Dougan (cort@cs.nmt.edu)
12 * and Paul Mackerras (paulus@cs.anu.edu.au)
16 * This file handles the architecture-dependent parts of hardware exceptions
19 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
24 #include <linux/stddef.h>
25 #include <linux/unistd.h>
26 #include <linux/slab.h>
27 #include <linux/user.h>
28 #include <linux/a.out.h>
29 #include <linux/interrupt.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/kprobes.h>
34 #include <asm/kdebug.h>
36 #include <asm/pgtable.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
40 #include <asm/processor.h>
41 #include <asm/ppcdebug.h>
43 #include <asm/systemcfg.h>
44 #include <asm/machdep.h>
47 #ifdef CONFIG_DEBUGGER
48 int (*__debugger
)(struct pt_regs
*regs
);
49 int (*__debugger_ipi
)(struct pt_regs
*regs
);
50 int (*__debugger_bpt
)(struct pt_regs
*regs
);
51 int (*__debugger_sstep
)(struct pt_regs
*regs
);
52 int (*__debugger_iabr_match
)(struct pt_regs
*regs
);
53 int (*__debugger_dabr_match
)(struct pt_regs
*regs
);
54 int (*__debugger_fault_handler
)(struct pt_regs
*regs
);
56 EXPORT_SYMBOL(__debugger
);
57 EXPORT_SYMBOL(__debugger_ipi
);
58 EXPORT_SYMBOL(__debugger_bpt
);
59 EXPORT_SYMBOL(__debugger_sstep
);
60 EXPORT_SYMBOL(__debugger_iabr_match
);
61 EXPORT_SYMBOL(__debugger_dabr_match
);
62 EXPORT_SYMBOL(__debugger_fault_handler
);
65 struct notifier_block
*ppc64_die_chain
;
66 static DEFINE_SPINLOCK(die_notifier_lock
);
68 int register_die_notifier(struct notifier_block
*nb
)
73 spin_lock_irqsave(&die_notifier_lock
, flags
);
74 err
= notifier_chain_register(&ppc64_die_chain
, nb
);
75 spin_unlock_irqrestore(&die_notifier_lock
, flags
);
80 * Trap & Exception support
83 static DEFINE_SPINLOCK(die_lock
);
85 int die(const char *str
, struct pt_regs
*regs
, long err
)
87 static int die_counter
;
94 spin_lock_irq(&die_lock
);
96 printk("Oops: %s, sig: %ld [#%d]\n", str
, err
, ++die_counter
);
102 printk("SMP NR_CPUS=%d ", NR_CPUS
);
105 #ifdef CONFIG_DEBUG_PAGEALLOC
106 printk("DEBUG_PAGEALLOC ");
113 switch(systemcfg
->platform
) {
114 case PLATFORM_PSERIES
:
118 case PLATFORM_PSERIES_LPAR
:
119 printk("PSERIES LPAR ");
122 case PLATFORM_ISERIES_LPAR
:
123 printk("ISERIES LPAR ");
126 case PLATFORM_POWERMAC
:
140 spin_unlock_irq(&die_lock
);
143 panic("Fatal exception in interrupt");
146 printk(KERN_EMERG
"Fatal exception: panic in 5 seconds\n");
148 panic("Fatal exception");
155 void _exception(int signr
, struct pt_regs
*regs
, int code
, unsigned long addr
)
159 if (!user_mode(regs
)) {
160 if (die("Exception in kernel mode", regs
, signr
))
164 memset(&info
, 0, sizeof(info
));
165 info
.si_signo
= signr
;
167 info
.si_addr
= (void __user
*) addr
;
168 force_sig_info(signr
, &info
, current
);
171 void system_reset_exception(struct pt_regs
*regs
)
173 /* See if any machine dependent calls */
174 if (ppc_md
.system_reset_exception
)
175 ppc_md
.system_reset_exception(regs
);
177 die("System Reset", regs
, 0);
179 /* Must die if the interrupt is not recoverable */
180 if (!(regs
->msr
& MSR_RI
))
181 panic("Unrecoverable System Reset");
183 /* What should we do here? We could issue a shutdown or hard reset. */
186 void machine_check_exception(struct pt_regs
*regs
)
190 /* See if any machine dependent calls */
191 if (ppc_md
.machine_check_exception
)
192 recover
= ppc_md
.machine_check_exception(regs
);
197 if (debugger_fault_handler(regs
))
199 die("Machine check", regs
, 0);
201 /* Must die if the interrupt is not recoverable */
202 if (!(regs
->msr
& MSR_RI
))
203 panic("Unrecoverable Machine check");
206 void unknown_exception(struct pt_regs
*regs
)
208 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
209 regs
->nip
, regs
->msr
, regs
->trap
);
211 _exception(SIGTRAP
, regs
, 0, 0);
214 void instruction_breakpoint_exception(struct pt_regs
*regs
)
216 if (notify_die(DIE_IABR_MATCH
, "iabr_match", regs
, 5,
217 5, SIGTRAP
) == NOTIFY_STOP
)
219 if (debugger_iabr_match(regs
))
221 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
224 void __kprobes
single_step_exception(struct pt_regs
*regs
)
226 regs
->msr
&= ~MSR_SE
; /* Turn off 'trace' bit */
228 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
229 5, SIGTRAP
) == NOTIFY_STOP
)
231 if (debugger_sstep(regs
))
234 _exception(SIGTRAP
, regs
, TRAP_TRACE
, regs
->nip
);
238 * After we have successfully emulated an instruction, we have to
239 * check if the instruction was being single-stepped, and if so,
240 * pretend we got a single-step exception. This was pointed out
241 * by Kumar Gala. -- paulus
243 static inline void emulate_single_step(struct pt_regs
*regs
)
245 if (regs
->msr
& MSR_SE
)
246 single_step_exception(regs
);
249 static void parse_fpe(struct pt_regs
*regs
)
254 flush_fp_to_thread(current
);
256 fpscr
= current
->thread
.fpscr
;
258 /* Invalid operation */
259 if ((fpscr
& FPSCR_VE
) && (fpscr
& FPSCR_VX
))
263 else if ((fpscr
& FPSCR_OE
) && (fpscr
& FPSCR_OX
))
267 else if ((fpscr
& FPSCR_UE
) && (fpscr
& FPSCR_UX
))
271 else if ((fpscr
& FPSCR_ZE
) && (fpscr
& FPSCR_ZX
))
275 else if ((fpscr
& FPSCR_XE
) && (fpscr
& FPSCR_XX
))
278 _exception(SIGFPE
, regs
, code
, regs
->nip
);
282 * Illegal instruction emulation support. Return non-zero if we can't
283 * emulate, or -EFAULT if the associated memory access caused an access
284 * fault. Return zero on success.
287 #define INST_MFSPR_PVR 0x7c1f42a6
288 #define INST_MFSPR_PVR_MASK 0xfc1fffff
290 #define INST_DCBA 0x7c0005ec
291 #define INST_DCBA_MASK 0x7c0007fe
293 #define INST_MCRXR 0x7c000400
294 #define INST_MCRXR_MASK 0x7c0007fe
296 static int emulate_instruction(struct pt_regs
*regs
)
298 unsigned int instword
;
300 if (!user_mode(regs
))
303 CHECK_FULL_REGS(regs
);
305 if (get_user(instword
, (unsigned int __user
*)(regs
->nip
)))
308 /* Emulate the mfspr rD, PVR. */
309 if ((instword
& INST_MFSPR_PVR_MASK
) == INST_MFSPR_PVR
) {
312 rd
= (instword
>> 21) & 0x1f;
313 regs
->gpr
[rd
] = mfspr(SPRN_PVR
);
317 /* Emulating the dcba insn is just a no-op. */
318 if ((instword
& INST_DCBA_MASK
) == INST_DCBA
) {
323 "process %d (%s) uses obsolete 'dcba' insn\n",
324 current
->pid
, current
->comm
);
330 /* Emulate the mcrxr insn. */
331 if ((instword
& INST_MCRXR_MASK
) == INST_MCRXR
) {
337 "process %d (%s) uses obsolete 'mcrxr' insn\n",
338 current
->pid
, current
->comm
);
342 shift
= (instword
>> 21) & 0x1c;
343 regs
->ccr
&= ~(0xf0000000 >> shift
);
344 regs
->ccr
|= (regs
->xer
& 0xf0000000) >> shift
;
345 regs
->xer
&= ~0xf0000000;
353 * Look through the list of trap instructions that are used for BUG(),
354 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
355 * that the exception was caused by a trap instruction of some kind.
356 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
359 extern struct bug_entry __start___bug_table
[], __stop___bug_table
[];
361 #ifndef CONFIG_MODULES
362 #define module_find_bug(x) NULL
365 struct bug_entry
*find_bug(unsigned long bugaddr
)
367 struct bug_entry
*bug
;
369 for (bug
= __start___bug_table
; bug
< __stop___bug_table
; ++bug
)
370 if (bugaddr
== bug
->bug_addr
)
372 return module_find_bug(bugaddr
);
376 check_bug_trap(struct pt_regs
*regs
)
378 struct bug_entry
*bug
;
381 if (regs
->msr
& MSR_PR
)
382 return 0; /* not in kernel */
383 addr
= regs
->nip
; /* address of trap instruction */
384 if (addr
< PAGE_OFFSET
)
386 bug
= find_bug(regs
->nip
);
389 if (bug
->line
& BUG_WARNING_TRAP
) {
390 /* this is a WARN_ON rather than BUG/BUG_ON */
391 printk(KERN_ERR
"Badness in %s at %s:%d\n",
392 bug
->function
, bug
->file
,
393 (unsigned int)bug
->line
& ~BUG_WARNING_TRAP
);
394 show_stack(current
, (void *)regs
->gpr
[1]);
397 printk(KERN_CRIT
"kernel BUG in %s at %s:%d!\n",
398 bug
->function
, bug
->file
, (unsigned int)bug
->line
);
402 void __kprobes
program_check_exception(struct pt_regs
*regs
)
404 if (debugger_fault_handler(regs
))
407 if (regs
->msr
& 0x100000) {
408 /* IEEE FP exception */
410 } else if (regs
->msr
& 0x20000) {
413 if (notify_die(DIE_BPT
, "breakpoint", regs
, 5,
414 5, SIGTRAP
) == NOTIFY_STOP
)
416 if (debugger_bpt(regs
))
419 if (check_bug_trap(regs
)) {
423 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
426 /* Privileged or illegal instruction; try to emulate it. */
427 switch (emulate_instruction(regs
)) {
430 emulate_single_step(regs
);
434 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
438 if (regs
->msr
& 0x40000)
440 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
443 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
449 void kernel_fp_unavailable_exception(struct pt_regs
*regs
)
451 printk(KERN_EMERG
"Unrecoverable FP Unavailable Exception "
452 "%lx at %lx\n", regs
->trap
, regs
->nip
);
453 die("Unrecoverable FP Unavailable Exception", regs
, SIGABRT
);
456 void altivec_unavailable_exception(struct pt_regs
*regs
)
458 if (user_mode(regs
)) {
459 /* A user program has executed an altivec instruction,
460 but this kernel doesn't support altivec. */
461 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
464 printk(KERN_EMERG
"Unrecoverable VMX/Altivec Unavailable Exception "
465 "%lx at %lx\n", regs
->trap
, regs
->nip
);
466 die("Unrecoverable VMX/Altivec Unavailable Exception", regs
, SIGABRT
);
469 extern perf_irq_t perf_irq
;
471 void performance_monitor_exception(struct pt_regs
*regs
)
476 void alignment_exception(struct pt_regs
*regs
)
480 fixed
= fix_alignment(regs
);
483 regs
->nip
+= 4; /* skip over emulated instruction */
484 emulate_single_step(regs
);
488 /* Operand address was bad */
489 if (fixed
== -EFAULT
) {
490 if (user_mode(regs
)) {
491 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->dar
);
493 /* Search exception table */
494 bad_page_fault(regs
, regs
->dar
, SIGSEGV
);
500 _exception(SIGBUS
, regs
, BUS_ADRALN
, regs
->nip
);
503 #ifdef CONFIG_ALTIVEC
504 void altivec_assist_exception(struct pt_regs
*regs
)
509 if (!user_mode(regs
)) {
510 printk(KERN_EMERG
"VMX/Altivec assist exception in kernel mode"
511 " at %lx\n", regs
->nip
);
512 die("Kernel VMX/Altivec assist exception", regs
, SIGILL
);
515 flush_altivec_to_thread(current
);
517 err
= emulate_altivec(regs
);
519 regs
->nip
+= 4; /* skip emulated instruction */
520 emulate_single_step(regs
);
524 if (err
== -EFAULT
) {
525 /* got an error reading the instruction */
526 info
.si_signo
= SIGSEGV
;
528 info
.si_code
= SEGV_MAPERR
;
529 info
.si_addr
= (void __user
*) regs
->nip
;
530 force_sig_info(SIGSEGV
, &info
, current
);
532 /* didn't recognize the instruction */
533 /* XXX quick hack for now: set the non-Java bit in the VSCR */
534 if (printk_ratelimit())
535 printk(KERN_ERR
"Unrecognized altivec instruction "
536 "in %s at %lx\n", current
->comm
, regs
->nip
);
537 current
->thread
.vscr
.u
[3] |= 0x10000;
540 #endif /* CONFIG_ALTIVEC */
543 * We enter here if we get an unrecoverable exception, that is, one
544 * that happened at a point where the RI (recoverable interrupt) bit
545 * in the MSR is 0. This indicates that SRR0/1 are live, and that
546 * we therefore lost state by taking this exception.
548 void unrecoverable_exception(struct pt_regs
*regs
)
550 printk(KERN_EMERG
"Unrecoverable exception %lx at %lx\n",
551 regs
->trap
, regs
->nip
);
552 die("Unrecoverable exception", regs
, SIGABRT
);
556 * We enter here if we discover during exception entry that we are
557 * running in supervisor mode with a userspace value in the stack pointer.
559 void kernel_bad_stack(struct pt_regs
*regs
)
561 printk(KERN_EMERG
"Bad kernel stack pointer %lx at %lx\n",
562 regs
->gpr
[1], regs
->nip
);
563 die("Bad kernel stack pointer", regs
, SIGABRT
);
566 void __init
trap_init(void)