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 <asm/kdebug.h>
35 #include <asm/pgtable.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
39 #include <asm/processor.h>
40 #include <asm/ppcdebug.h>
42 #include <asm/systemcfg.h>
43 #include <asm/machdep.h>
46 #ifdef CONFIG_DEBUGGER
47 int (*__debugger
)(struct pt_regs
*regs
);
48 int (*__debugger_ipi
)(struct pt_regs
*regs
);
49 int (*__debugger_bpt
)(struct pt_regs
*regs
);
50 int (*__debugger_sstep
)(struct pt_regs
*regs
);
51 int (*__debugger_iabr_match
)(struct pt_regs
*regs
);
52 int (*__debugger_dabr_match
)(struct pt_regs
*regs
);
53 int (*__debugger_fault_handler
)(struct pt_regs
*regs
);
55 EXPORT_SYMBOL(__debugger
);
56 EXPORT_SYMBOL(__debugger_ipi
);
57 EXPORT_SYMBOL(__debugger_bpt
);
58 EXPORT_SYMBOL(__debugger_sstep
);
59 EXPORT_SYMBOL(__debugger_iabr_match
);
60 EXPORT_SYMBOL(__debugger_dabr_match
);
61 EXPORT_SYMBOL(__debugger_fault_handler
);
64 struct notifier_block
*ppc64_die_chain
;
65 static DEFINE_SPINLOCK(die_notifier_lock
);
67 int register_die_notifier(struct notifier_block
*nb
)
72 spin_lock_irqsave(&die_notifier_lock
, flags
);
73 err
= notifier_chain_register(&ppc64_die_chain
, nb
);
74 spin_unlock_irqrestore(&die_notifier_lock
, flags
);
79 * Trap & Exception support
82 static DEFINE_SPINLOCK(die_lock
);
84 int die(const char *str
, struct pt_regs
*regs
, long err
)
86 static int die_counter
;
93 spin_lock_irq(&die_lock
);
95 printk("Oops: %s, sig: %ld [#%d]\n", str
, err
, ++die_counter
);
101 printk("SMP NR_CPUS=%d ", NR_CPUS
);
104 #ifdef CONFIG_DEBUG_PAGEALLOC
105 printk("DEBUG_PAGEALLOC ");
112 switch(systemcfg
->platform
) {
113 case PLATFORM_PSERIES
:
117 case PLATFORM_PSERIES_LPAR
:
118 printk("PSERIES LPAR ");
121 case PLATFORM_ISERIES_LPAR
:
122 printk("ISERIES LPAR ");
125 case PLATFORM_POWERMAC
:
139 spin_unlock_irq(&die_lock
);
142 panic("Fatal exception in interrupt");
145 printk(KERN_EMERG
"Fatal exception: panic in 5 seconds\n");
147 panic("Fatal exception");
154 void _exception(int signr
, struct pt_regs
*regs
, int code
, unsigned long addr
)
158 if (!user_mode(regs
)) {
159 if (die("Exception in kernel mode", regs
, signr
))
163 memset(&info
, 0, sizeof(info
));
164 info
.si_signo
= signr
;
166 info
.si_addr
= (void __user
*) addr
;
167 force_sig_info(signr
, &info
, current
);
170 void system_reset_exception(struct pt_regs
*regs
)
172 /* See if any machine dependent calls */
173 if (ppc_md
.system_reset_exception
)
174 ppc_md
.system_reset_exception(regs
);
176 die("System Reset", regs
, 0);
178 /* Must die if the interrupt is not recoverable */
179 if (!(regs
->msr
& MSR_RI
))
180 panic("Unrecoverable System Reset");
182 /* What should we do here? We could issue a shutdown or hard reset. */
185 void machine_check_exception(struct pt_regs
*regs
)
189 /* See if any machine dependent calls */
190 if (ppc_md
.machine_check_exception
)
191 recover
= ppc_md
.machine_check_exception(regs
);
196 if (debugger_fault_handler(regs
))
198 die("Machine check", regs
, 0);
200 /* Must die if the interrupt is not recoverable */
201 if (!(regs
->msr
& MSR_RI
))
202 panic("Unrecoverable Machine check");
205 void unknown_exception(struct pt_regs
*regs
)
207 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
208 regs
->nip
, regs
->msr
, regs
->trap
);
210 _exception(SIGTRAP
, regs
, 0, 0);
213 void instruction_breakpoint_exception(struct pt_regs
*regs
)
215 if (notify_die(DIE_IABR_MATCH
, "iabr_match", regs
, 5,
216 5, SIGTRAP
) == NOTIFY_STOP
)
218 if (debugger_iabr_match(regs
))
220 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
223 void single_step_exception(struct pt_regs
*regs
)
225 regs
->msr
&= ~MSR_SE
; /* Turn off 'trace' bit */
227 if (notify_die(DIE_SSTEP
, "single_step", regs
, 5,
228 5, SIGTRAP
) == NOTIFY_STOP
)
230 if (debugger_sstep(regs
))
233 _exception(SIGTRAP
, regs
, TRAP_TRACE
, regs
->nip
);
237 * After we have successfully emulated an instruction, we have to
238 * check if the instruction was being single-stepped, and if so,
239 * pretend we got a single-step exception. This was pointed out
240 * by Kumar Gala. -- paulus
242 static inline void emulate_single_step(struct pt_regs
*regs
)
244 if (regs
->msr
& MSR_SE
)
245 single_step_exception(regs
);
248 static void parse_fpe(struct pt_regs
*regs
)
253 flush_fp_to_thread(current
);
255 fpscr
= current
->thread
.fpscr
;
257 /* Invalid operation */
258 if ((fpscr
& FPSCR_VE
) && (fpscr
& FPSCR_VX
))
262 else if ((fpscr
& FPSCR_OE
) && (fpscr
& FPSCR_OX
))
266 else if ((fpscr
& FPSCR_UE
) && (fpscr
& FPSCR_UX
))
270 else if ((fpscr
& FPSCR_ZE
) && (fpscr
& FPSCR_ZX
))
274 else if ((fpscr
& FPSCR_XE
) && (fpscr
& FPSCR_XX
))
277 _exception(SIGFPE
, regs
, code
, regs
->nip
);
281 * Illegal instruction emulation support. Return non-zero if we can't
282 * emulate, or -EFAULT if the associated memory access caused an access
283 * fault. Return zero on success.
286 #define INST_MFSPR_PVR 0x7c1f42a6
287 #define INST_MFSPR_PVR_MASK 0xfc1fffff
289 #define INST_DCBA 0x7c0005ec
290 #define INST_DCBA_MASK 0x7c0007fe
292 #define INST_MCRXR 0x7c000400
293 #define INST_MCRXR_MASK 0x7c0007fe
295 static int emulate_instruction(struct pt_regs
*regs
)
297 unsigned int instword
;
299 if (!user_mode(regs
))
302 CHECK_FULL_REGS(regs
);
304 if (get_user(instword
, (unsigned int __user
*)(regs
->nip
)))
307 /* Emulate the mfspr rD, PVR. */
308 if ((instword
& INST_MFSPR_PVR_MASK
) == INST_MFSPR_PVR
) {
311 rd
= (instword
>> 21) & 0x1f;
312 regs
->gpr
[rd
] = mfspr(SPRN_PVR
);
316 /* Emulating the dcba insn is just a no-op. */
317 if ((instword
& INST_DCBA_MASK
) == INST_DCBA
) {
322 "process %d (%s) uses obsolete 'dcba' insn\n",
323 current
->pid
, current
->comm
);
329 /* Emulate the mcrxr insn. */
330 if ((instword
& INST_MCRXR_MASK
) == INST_MCRXR
) {
336 "process %d (%s) uses obsolete 'mcrxr' insn\n",
337 current
->pid
, current
->comm
);
341 shift
= (instword
>> 21) & 0x1c;
342 regs
->ccr
&= ~(0xf0000000 >> shift
);
343 regs
->ccr
|= (regs
->xer
& 0xf0000000) >> shift
;
344 regs
->xer
&= ~0xf0000000;
352 * Look through the list of trap instructions that are used for BUG(),
353 * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
354 * that the exception was caused by a trap instruction of some kind.
355 * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
358 extern struct bug_entry __start___bug_table
[], __stop___bug_table
[];
360 #ifndef CONFIG_MODULES
361 #define module_find_bug(x) NULL
364 struct bug_entry
*find_bug(unsigned long bugaddr
)
366 struct bug_entry
*bug
;
368 for (bug
= __start___bug_table
; bug
< __stop___bug_table
; ++bug
)
369 if (bugaddr
== bug
->bug_addr
)
371 return module_find_bug(bugaddr
);
375 check_bug_trap(struct pt_regs
*regs
)
377 struct bug_entry
*bug
;
380 if (regs
->msr
& MSR_PR
)
381 return 0; /* not in kernel */
382 addr
= regs
->nip
; /* address of trap instruction */
383 if (addr
< PAGE_OFFSET
)
385 bug
= find_bug(regs
->nip
);
388 if (bug
->line
& BUG_WARNING_TRAP
) {
389 /* this is a WARN_ON rather than BUG/BUG_ON */
390 printk(KERN_ERR
"Badness in %s at %s:%d\n",
391 bug
->function
, bug
->file
,
392 (unsigned int)bug
->line
& ~BUG_WARNING_TRAP
);
393 show_stack(current
, (void *)regs
->gpr
[1]);
396 printk(KERN_CRIT
"kernel BUG in %s at %s:%d!\n",
397 bug
->function
, bug
->file
, (unsigned int)bug
->line
);
401 void program_check_exception(struct pt_regs
*regs
)
403 if (debugger_fault_handler(regs
))
406 if (regs
->msr
& 0x100000) {
407 /* IEEE FP exception */
409 } else if (regs
->msr
& 0x20000) {
412 if (notify_die(DIE_BPT
, "breakpoint", regs
, 5,
413 5, SIGTRAP
) == NOTIFY_STOP
)
415 if (debugger_bpt(regs
))
418 if (check_bug_trap(regs
)) {
422 _exception(SIGTRAP
, regs
, TRAP_BRKPT
, regs
->nip
);
425 /* Privileged or illegal instruction; try to emulate it. */
426 switch (emulate_instruction(regs
)) {
429 emulate_single_step(regs
);
433 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->nip
);
437 if (regs
->msr
& 0x40000)
439 _exception(SIGILL
, regs
, ILL_PRVOPC
, regs
->nip
);
442 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
448 void kernel_fp_unavailable_exception(struct pt_regs
*regs
)
450 printk(KERN_EMERG
"Unrecoverable FP Unavailable Exception "
451 "%lx at %lx\n", regs
->trap
, regs
->nip
);
452 die("Unrecoverable FP Unavailable Exception", regs
, SIGABRT
);
455 void altivec_unavailable_exception(struct pt_regs
*regs
)
457 if (user_mode(regs
)) {
458 /* A user program has executed an altivec instruction,
459 but this kernel doesn't support altivec. */
460 _exception(SIGILL
, regs
, ILL_ILLOPC
, regs
->nip
);
463 printk(KERN_EMERG
"Unrecoverable VMX/Altivec Unavailable Exception "
464 "%lx at %lx\n", regs
->trap
, regs
->nip
);
465 die("Unrecoverable VMX/Altivec Unavailable Exception", regs
, SIGABRT
);
468 extern perf_irq_t perf_irq
;
470 void performance_monitor_exception(struct pt_regs
*regs
)
475 void alignment_exception(struct pt_regs
*regs
)
479 fixed
= fix_alignment(regs
);
482 regs
->nip
+= 4; /* skip over emulated instruction */
483 emulate_single_step(regs
);
487 /* Operand address was bad */
488 if (fixed
== -EFAULT
) {
489 if (user_mode(regs
)) {
490 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->dar
);
492 /* Search exception table */
493 bad_page_fault(regs
, regs
->dar
, SIGSEGV
);
499 _exception(SIGBUS
, regs
, BUS_ADRALN
, regs
->nip
);
502 #ifdef CONFIG_ALTIVEC
503 void altivec_assist_exception(struct pt_regs
*regs
)
508 if (!user_mode(regs
)) {
509 printk(KERN_EMERG
"VMX/Altivec assist exception in kernel mode"
510 " at %lx\n", regs
->nip
);
511 die("Kernel VMX/Altivec assist exception", regs
, SIGILL
);
514 flush_altivec_to_thread(current
);
516 err
= emulate_altivec(regs
);
518 regs
->nip
+= 4; /* skip emulated instruction */
519 emulate_single_step(regs
);
523 if (err
== -EFAULT
) {
524 /* got an error reading the instruction */
525 info
.si_signo
= SIGSEGV
;
527 info
.si_code
= SEGV_MAPERR
;
528 info
.si_addr
= (void __user
*) regs
->nip
;
529 force_sig_info(SIGSEGV
, &info
, current
);
531 /* didn't recognize the instruction */
532 /* XXX quick hack for now: set the non-Java bit in the VSCR */
533 if (printk_ratelimit())
534 printk(KERN_ERR
"Unrecognized altivec instruction "
535 "in %s at %lx\n", current
->comm
, regs
->nip
);
536 current
->thread
.vscr
.u
[3] |= 0x10000;
539 #endif /* CONFIG_ALTIVEC */
542 * We enter here if we get an unrecoverable exception, that is, one
543 * that happened at a point where the RI (recoverable interrupt) bit
544 * in the MSR is 0. This indicates that SRR0/1 are live, and that
545 * we therefore lost state by taking this exception.
547 void unrecoverable_exception(struct pt_regs
*regs
)
549 printk(KERN_EMERG
"Unrecoverable exception %lx at %lx\n",
550 regs
->trap
, regs
->nip
);
551 die("Unrecoverable exception", regs
, SIGABRT
);
555 * We enter here if we discover during exception entry that we are
556 * running in supervisor mode with a userspace value in the stack pointer.
558 void kernel_bad_stack(struct pt_regs
*regs
)
560 printk(KERN_EMERG
"Bad kernel stack pointer %lx at %lx\n",
561 regs
->gpr
[1], regs
->nip
);
562 die("Bad kernel stack pointer", regs
, SIGABRT
);
565 void __init
trap_init(void)