FRV: Fix no-hardware-breakpoint case
[linux-2.6/cjktty.git] / arch / powerpc / kernel / traps.c
blob804f0f30f227f692d949d49117b3c35da908fc6f
1 /*
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>
20 #include <linux/mm.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>
42 #include <asm/io.h>
43 #include <asm/machdep.h>
44 #include <asm/rtas.h>
45 #include <asm/pmc.h>
46 #ifdef CONFIG_PPC32
47 #include <asm/reg.h>
48 #endif
49 #ifdef CONFIG_PMAC_BACKLIGHT
50 #include <asm/backlight.h>
51 #endif
52 #ifdef CONFIG_PPC64
53 #include <asm/firmware.h>
54 #include <asm/processor.h>
55 #endif
56 #include <asm/kexec.h>
57 #include <asm/ppc-opcode.h>
58 #ifdef CONFIG_FSL_BOOKE
59 #include <asm/dbell.h>
60 #endif
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);
78 #endif
81 * Trap & Exception support
84 #ifdef CONFIG_PMAC_BACKLIGHT
85 static void pmac_backlight_unblank(void)
87 mutex_lock(&pmac_backlight_mutex);
88 if (pmac_backlight) {
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);
98 #else
99 static inline void pmac_backlight_unblank(void) { }
100 #endif
102 int die(const char *str, struct pt_regs *regs, long err)
104 static struct {
105 spinlock_t lock;
106 u32 lock_owner;
107 int lock_owner_depth;
108 } die = {
109 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
110 .lock_owner = -1,
111 .lock_owner_depth = 0
113 static int die_counter;
114 unsigned long flags;
116 if (debugger(regs))
117 return 1;
119 oops_enter();
121 if (die.lock_owner != raw_smp_processor_id()) {
122 console_verbose();
123 spin_lock_irqsave(&die.lock, flags);
124 die.lock_owner = smp_processor_id();
125 die.lock_owner_depth = 0;
126 bust_spinlocks(1);
127 if (machine_is(powermac))
128 pmac_backlight_unblank();
129 } else {
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
136 printk("PREEMPT ");
137 #endif
138 #ifdef CONFIG_SMP
139 printk("SMP NR_CPUS=%d ", NR_CPUS);
140 #endif
141 #ifdef CONFIG_DEBUG_PAGEALLOC
142 printk("DEBUG_PAGEALLOC ");
143 #endif
144 #ifdef CONFIG_NUMA
145 printk("NUMA ");
146 #endif
147 printk("%s\n", ppc_md.name ? ppc_md.name : "");
149 print_modules();
150 show_regs(regs);
151 } else {
152 printk("Recursive die() failure, output suppressed\n");
155 bust_spinlocks(0);
156 die.lock_owner = -1;
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()))
162 crash_kexec(regs);
163 crash_kexec_secondary(regs);
165 if (in_interrupt())
166 panic("Fatal exception in interrupt");
168 if (panic_on_oops)
169 panic("Fatal exception");
171 oops_exit();
172 do_exit(err);
174 return 0;
177 void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
179 siginfo_t info;
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))
187 return;
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;
198 info.si_code = code;
199 info.si_addr = (void __user *) addr;
200 force_sig_info(signr, &info, current);
203 #ifdef CONFIG_PPC64
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))
209 return;
212 #ifdef CONFIG_KEXEC
213 cpu_set(smp_processor_id(), cpus_in_sr);
214 #endif
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. */
237 #endif
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
243 * table.
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.
247 * -- paulus.
249 static inline int check_io_access(struct pt_regs *regs)
251 #ifdef CONFIG_PPC32
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
264 * load or store.
266 if (*nip == 0x60000000) /* nop */
267 nip -= 2;
268 else if (*nip == 0x4c00012c) /* isync */
269 --nip;
270 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
271 /* sync or twi */
272 unsigned int rb;
274 --nip;
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);
279 regs->msr |= MSR_RI;
280 regs->nip = entry->fixup;
281 return 1;
284 #endif /* CONFIG_PPC32 */
285 return 0;
288 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
289 /* On 4xx, the reason for the machine check or program exception
290 is in the ESR. */
291 #define get_reason(regs) ((regs)->dsisr)
292 #ifndef CONFIG_FSL_BOOKE
293 #define get_mc_reason(regs) ((regs)->dsisr)
294 #else
295 #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
296 #endif
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)
306 #else
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)
318 #endif
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);
328 } else
329 printk("Data");
330 printk(" machine check in kernel mode.\n");
332 return 0;
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);
344 else {
345 u32 mcsr = mfspr(SPRN_MCSR);
346 if (mcsr & MCSR_IB)
347 printk("Instruction Read PLB Error\n");
348 if (mcsr & MCSR_DRB)
349 printk("Data Read PLB Error\n");
350 if (mcsr & MCSR_DWB)
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");
365 /* Clear MCSR */
366 mtspr(SPRN_MCSR, mcsr);
368 return 0;
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");
403 return 0;
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");
428 return 0;
430 #else
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) {
438 case 0x80000:
439 printk("Machine check signal\n");
440 break;
441 case 0: /* for 601 */
442 case 0x40000:
443 case 0x140000: /* 7450 MSS error and TEA */
444 printk("Transfer error ack signal\n");
445 break;
446 case 0x20000:
447 printk("Data parity error signal\n");
448 break;
449 case 0x10000:
450 printk("Address parity error signal\n");
451 break;
452 case 0x20000000:
453 printk("L1 Data Cache error\n");
454 break;
455 case 0x40000000:
456 printk("L1 Instruction Cache error\n");
457 break;
458 case 0x00100000:
459 printk("L2 data cache parity error\n");
460 break;
461 default:
462 printk("Unknown values in msr\n");
464 return 0;
466 #endif /* everything else */
468 void machine_check_exception(struct pt_regs *regs)
470 int recover = 0;
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);
483 if (recover > 0)
484 return;
486 if (user_mode(regs)) {
487 regs->msr |= MSR_RI;
488 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
489 return;
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.
497 * -- BenH
499 bad_page_fault(regs, regs->dar, SIGBUS);
500 return;
501 #endif
503 if (debugger_fault_handler(regs)) {
504 regs->msr |= MSR_RI;
505 return;
508 if (check_io_access(regs))
509 return;
511 if (debugger_fault_handler(regs))
512 return;
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)
537 return;
538 if (debugger_iabr_match(regs))
539 return;
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)
554 return;
555 if (debugger_sstep(regs))
556 return;
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)
577 int ret = 0;
579 /* Invalid operation */
580 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
581 ret = FPE_FLTINV;
583 /* Overflow */
584 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
585 ret = FPE_FLTOVF;
587 /* Underflow */
588 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
589 ret = FPE_FLTUND;
591 /* Divide by zero */
592 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
593 ret = FPE_FLTDIV;
595 /* Inexact result */
596 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
597 ret = FPE_FLTRES;
599 return ret;
602 static void parse_fpe(struct pt_regs *regs)
604 int code = 0;
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;
629 u32 num_bytes;
630 unsigned long EA;
631 int pos = 0;
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))
636 return -EINVAL;
638 EA = (rA == 0) ? 0 : regs->gpr[rA];
640 switch (instword & PPC_INST_STRING_MASK) {
641 case PPC_INST_LSWX:
642 case PPC_INST_STSWX:
643 EA += NB_RB;
644 num_bytes = regs->xer & 0x7f;
645 break;
646 case PPC_INST_LSWI:
647 case PPC_INST_STSWI:
648 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
649 break;
650 default:
651 return -EINVAL;
654 while (num_bytes != 0)
656 u8 val;
657 u32 shift = 8 * (3 - (pos & 0x3));
659 switch ((instword & PPC_INST_STRING_MASK)) {
660 case PPC_INST_LSWX:
661 case PPC_INST_LSWI:
662 if (get_user(val, (u8 __user *)EA))
663 return -EFAULT;
664 /* first time updating this reg,
665 * zero it out */
666 if (pos == 0)
667 regs->gpr[rT] = 0;
668 regs->gpr[rT] |= val << shift;
669 break;
670 case PPC_INST_STSWI:
671 case PPC_INST_STSWX:
672 val = regs->gpr[rT] >> shift;
673 if (put_user(val, (u8 __user *)EA))
674 return -EFAULT;
675 break;
677 /* move EA to next address */
678 EA += 1;
679 num_bytes--;
681 /* manage our position within the register */
682 if (++pos == 4) {
683 pos = 0;
684 if (++rT == 32)
685 rT = 0;
689 return 0;
692 static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
694 u32 ra,rs;
695 unsigned long tmp;
697 ra = (instword >> 16) & 0x1f;
698 rs = (instword >> 21) & 0x1f;
700 tmp = regs->gpr[rs];
701 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
702 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
703 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
704 regs->gpr[ra] = tmp;
706 return 0;
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;
715 u8 bit;
716 unsigned long tmp;
718 tmp = (rA == 0) ? 0 : regs->gpr[rA];
719 bit = (regs->ccr >> (31 - BC)) & 0x1;
721 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
723 return 0;
726 static int emulate_instruction(struct pt_regs *regs)
728 u32 instword;
729 u32 rd;
731 if (!user_mode(regs) || (regs->msr & MSR_LE))
732 return -EINVAL;
733 CHECK_FULL_REGS(regs);
735 if (get_user(instword, (u32 __user *)(regs->nip)))
736 return -EFAULT;
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);
743 return 0;
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);
749 return 0;
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;
760 return 0;
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);
781 return -EINVAL;
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 */
799 parse_fpe(regs);
800 return;
802 if (reason & REASON_TRAP) {
803 /* trap exception */
804 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
805 == NOTIFY_STOP)
806 return;
807 if (debugger_bpt(regs))
808 return;
810 if (!(regs->msr & MSR_PR) && /* not user-mode */
811 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
812 regs->nip += 4;
813 return;
815 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
816 return;
819 local_irq_enable();
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)) {
830 case 0:
831 emulate_single_step(regs);
832 return;
833 case 1: {
834 int code = 0;
835 code = __parse_fpscr(current->thread.fpscr.val);
836 _exception(SIGFPE, regs, code, regs->nip);
837 return;
839 case -EFAULT:
840 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
841 return;
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)) {
849 case 0:
850 regs->nip += 4;
851 emulate_single_step(regs);
852 return;
853 case -EFAULT:
854 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
855 return;
859 if (reason & REASON_PRIVILEGED)
860 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
861 else
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);
873 if (fixed == 1) {
874 regs->nip += 4; /* skip over emulated instruction */
875 emulate_single_step(regs);
876 return;
879 /* Operand address was bad */
880 if (fixed == -EFAULT) {
881 sig = SIGSEGV;
882 code = SEGV_ACCERR;
883 } else {
884 sig = SIGBUS;
885 code = BUS_ADRALN;
887 if (user_mode(regs))
888 _exception(sig, regs, code, regs->dar);
889 else
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]);
897 debugger(regs);
898 show_regs(regs);
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);
906 debugger(regs);
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);
930 return;
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);
944 return;
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)
954 perf_irq(regs);
957 #ifdef CONFIG_8xx
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)
963 int errcode;
964 #endif
966 CHECK_FULL_REGS(regs);
968 if (!user_mode(regs)) {
969 debugger(regs);
970 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
973 #ifdef CONFIG_MATH_EMULATION
974 errcode = do_mathemu(regs);
975 if (errcode >= 0)
976 PPC_WARN_EMULATED(math, regs);
978 switch (errcode) {
979 case 0:
980 emulate_single_step(regs);
981 return;
982 case 1: {
983 int code = 0;
984 code = __parse_fpscr(current->thread.fpscr.val);
985 _exception(SIGFPE, regs, code, regs->nip);
986 return;
988 case -EFAULT:
989 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
990 return;
991 default:
992 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
993 return;
996 #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
997 errcode = Soft_emulate_8xx(regs);
998 if (errcode >= 0)
999 PPC_WARN_EMULATED(8xx, regs);
1001 switch (errcode) {
1002 case 0:
1003 emulate_single_step(regs);
1004 return;
1005 case 1:
1006 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1007 return;
1008 case -EFAULT:
1009 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1010 return;
1012 #else
1013 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1014 #endif
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;
1030 /* Disable BT */
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;
1040 return;
1043 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1044 5, SIGTRAP) == NOTIFY_STOP) {
1045 return;
1047 if (debugger_sstep(regs))
1048 return;
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) {
1059 return;
1062 if (debugger_sstep(regs))
1063 return;
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 |
1074 DBCR0_IDM);
1075 } else {
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)
1100 int err;
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);
1112 if (err == 0) {
1113 regs->nip += 4; /* skip emulated instruction */
1114 emulate_single_step(regs);
1115 return;
1118 if (err == -EFAULT) {
1119 /* got an error reading the instruction */
1120 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1121 } else {
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 */
1132 #ifdef CONFIG_VSX
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)
1151 #ifdef CONFIG_SMP
1152 int cpu = smp_processor_id();
1153 int msg;
1155 if (num_online_cpus() < 2)
1156 return;
1158 for (msg = 0; msg < 4; msg++)
1159 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1160 smp_message_recv(msg);
1161 #else
1162 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1163 #endif
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
1171 * something smarter
1173 if (error_code & (ESR_DLK|ESR_ILK))
1174 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1175 return;
1177 #endif /* CONFIG_FSL_BOOKE */
1179 #ifdef CONFIG_SPE
1180 void SPEFloatingPointException(struct pt_regs *regs)
1182 extern int do_spe_mathemu(struct pt_regs *regs);
1183 unsigned long spefscr;
1184 int fpexc_mode;
1185 int code = 0;
1186 int err;
1188 preempt_disable();
1189 if (regs->msr & MSR_SPE)
1190 giveup_spe(current);
1191 preempt_enable();
1193 spefscr = current->thread.spefscr;
1194 fpexc_mode = current->thread.fpexc_mode;
1196 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1197 code = FPE_FLTOVF;
1199 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1200 code = FPE_FLTUND;
1202 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1203 code = FPE_FLTDIV;
1204 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1205 code = FPE_FLTINV;
1207 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1208 code = FPE_FLTRES;
1210 err = do_spe_mathemu(regs);
1211 if (err == 0) {
1212 regs->nip += 4; /* skip emulated instruction */
1213 emulate_single_step(regs);
1214 return;
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);
1224 } else {
1225 _exception(SIGFPE, regs, code, regs->nip);
1228 return;
1231 void SPEFloatingPointRoundException(struct pt_regs *regs)
1233 extern int speround_handler(struct pt_regs *regs);
1234 int err;
1236 preempt_disable();
1237 if (regs->msr & MSR_SPE)
1238 giveup_spe(current);
1239 preempt_enable();
1241 regs->nip -= 4;
1242 err = speround_handler(regs);
1243 if (err == 0) {
1244 regs->nip += 4; /* skip emulated instruction */
1245 emulate_single_step(regs);
1246 return;
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);
1256 } else {
1257 _exception(SIGFPE, regs, 0, regs->nip);
1258 return;
1261 #endif
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));
1285 return;
1288 void WatchdogException(struct pt_regs *regs)
1290 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1291 WatchdogHandler(regs);
1293 #endif
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),
1318 #endif
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),
1334 #endif
1335 #ifdef CONFIG_VSX
1336 WARN_EMULATED_SETUP(vsx),
1337 #endif
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,
1346 type);
1349 static int __init ppc_warn_emulated_init(void)
1351 struct dentry *dir, *d;
1352 unsigned int i;
1353 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1355 if (!powerpc_debugfs_root)
1356 return -ENODEV;
1358 dir = debugfs_create_dir("emulated_instructions",
1359 powerpc_debugfs_root);
1360 if (!dir)
1361 return -ENOMEM;
1363 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1364 &ppc_warn_emulated);
1365 if (!d)
1366 goto fail;
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);
1371 if (!d)
1372 goto fail;
1375 return 0;
1377 fail:
1378 debugfs_remove_recursive(dir);
1379 return -ENOMEM;
1382 device_initcall(ppc_warn_emulated_init);
1384 #endif /* CONFIG_PPC_EMULATED_STATS */