2 * linux/arch/parisc/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1999, 2000 Philipp Rumpf <prumpf@tux.org>
9 * 'Traps.c' handles hardware traps and faults after we have saved some
13 #include <linux/config.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/timer.h>
21 #include <linux/module.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/spinlock.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/console.h>
28 #include <linux/kallsyms.h>
30 #include <asm/assembly.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
35 #include <asm/traps.h>
36 #include <asm/unaligned.h>
37 #include <asm/atomic.h>
40 #include <asm/pdc_chassis.h>
41 #include <asm/unwind.h>
43 #include "../math-emu/math-emu.h" /* for handle_fpe() */
45 #define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
46 /* dumped to the console via printk) */
48 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
49 DEFINE_SPINLOCK(pa_dbit_lock);
52 int printbinary(char *buf, unsigned long x, int nbits)
54 unsigned long mask = 1UL << (nbits - 1);
56 *buf++ = (mask & x ? '1' : '0');
70 void show_regs(struct pt_regs *regs)
78 level = user_mode(regs) ? KERN_DEBUG : KERN_CRIT;
80 printk("%s\n", level); /* don't want to have that pretty register dump messed up */
82 printk("%s YZrvWESTHLNXBCVMcbcbcbcbOGFRQPDI\n", level);
83 printbinary(buf, regs->gr[0], 32);
84 printk("%sPSW: %s %s\n", level, buf, print_tainted());
86 for (i = 0; i < 32; i += 4) {
89 p += sprintf(p, "%sr%02d-%02d ", level, i, i + 3);
90 for (j = 0; j < 4; j++) {
91 p += sprintf(p, " " RFMT, (i+j) == 0 ? 0 : regs->gr[i + j]);
96 for (i = 0; i < 8; i += 4) {
99 p += sprintf(p, "%ssr%d-%d ", level, i, i + 3);
100 for (j = 0; j < 4; j++) {
101 p += sprintf(p, " " RFMT, regs->sr[i + j]);
106 #if RIDICULOUSLY_VERBOSE
107 for (i = 0; i < 32; i += 2)
108 printk("%sFR%02d : %016lx FR%2d : %016lx", level, i,
109 regs->fr[i], i+1, regs->fr[i+1]);
114 printk("%s\n", level);
115 printk("%sIASQ: " RFMT " " RFMT " IAOQ: " RFMT " " RFMT "\n",
116 level, regs->iasq[0], regs->iasq[1], regs->iaoq[0], regs->iaoq[1]);
117 printk("%s IIR: %08lx ISR: " RFMT " IOR: " RFMT "\n",
118 level, regs->iir, regs->isr, regs->ior);
119 printk("%s CPU: %8d CR30: " RFMT " CR31: " RFMT "\n",
120 level, current_thread_info()->cpu, cr30, cr31);
121 printk("%s ORIG_R28: " RFMT "\n", level, regs->orig_r28);
123 print_symbol(" IAOQ[0]: %s\n", regs->iaoq[0]);
125 print_symbol(" IAOQ[1]: %s\n", regs->iaoq[1]);
127 print_symbol(" RP(r2): %s\n", regs->gr[2]);
131 void dump_stack(void)
133 show_stack(NULL, NULL);
136 EXPORT_SYMBOL(dump_stack);
138 static void do_show_stack(struct unwind_frame_info *info)
142 printk("Backtrace:\n");
144 if (unwind_once(info) < 0 || info->ip == 0)
147 if (__kernel_text_address(info->ip)) {
148 printk(" [<" RFMT ">] ", info->ip);
149 #ifdef CONFIG_KALLSYMS
150 print_symbol("%s\n", info->ip);
161 void show_stack(struct task_struct *task, unsigned long *s)
163 struct unwind_frame_info info;
166 unsigned long sp, ip, rp;
169 asm volatile ("copy %%r30, %0" : "=r"(sp));
170 ip = (unsigned long)&&HERE;
171 rp = (unsigned long)__builtin_return_address(0);
172 unwind_frame_init(&info, current, sp, ip, rp);
174 unwind_frame_init_from_blocked_task(&info, task);
177 do_show_stack(&info);
180 void die_if_kernel(char *str, struct pt_regs *regs, long err)
182 if (user_mode(regs)) {
186 printk(KERN_CRIT "%s (pid %d): %s (code %ld) at " RFMT "\n",
187 current->comm, current->pid, str, err, regs->iaoq[0]);
188 #ifdef PRINT_USER_FAULTS
189 /* XXX for debugging only */
195 oops_in_progress = 1;
197 /* Amuse the user in a SPARC fashion */
199 " _______________________________ \n"
200 " < Your System ate a SPARC! Gah! >\n"
201 " ------------------------------- \n"
203 " \\ (xx)\\_______\n"
208 /* unlock the pdc lock if necessary */
209 pdc_emergency_unlock();
211 /* maybe the kernel hasn't booted very far yet and hasn't been able
212 * to initialize the serial or STI console. In that case we should
213 * re-enable the pdc console, so that the user will be able to
214 * identify the problem. */
215 if (!console_drivers)
216 pdc_console_restart();
218 printk(KERN_CRIT "%s (pid %d): %s (code %ld)\n",
219 current->comm, current->pid, str, err);
222 /* Wot's wrong wif bein' racy? */
223 if (current->thread.flags & PARISC_KERNEL_DEATH) {
224 printk(KERN_CRIT "%s() recursion detected.\n", __FUNCTION__);
229 current->thread.flags |= PARISC_KERNEL_DEATH;
233 int syscall_ipi(int (*syscall) (struct pt_regs *), struct pt_regs *regs)
235 return syscall(regs);
238 /* gdb uses break 4,8 */
239 #define GDB_BREAK_INSN 0x10004
240 void handle_gdb_break(struct pt_regs *regs, int wot)
245 si.si_addr = (void *) (regs->iaoq[0] & ~3);
246 si.si_signo = SIGTRAP;
248 force_sig_info(SIGTRAP, &si, current);
251 void handle_break(unsigned iir, struct pt_regs *regs)
257 #ifdef PRINT_USER_FAULTS
258 printk(KERN_DEBUG "break 0,0: pid=%d command='%s'\n",
259 current->pid, current->comm);
261 die_if_kernel("Breakpoint", regs, 0);
262 #ifdef PRINT_USER_FAULTS
265 si.si_code = TRAP_BRKPT;
266 si.si_addr = (void *) (regs->iaoq[0] & ~3);
267 si.si_signo = SIGTRAP;
268 force_sig_info(SIGTRAP, &si, current);
272 die_if_kernel("Breakpoint", regs, 0);
273 handle_gdb_break(regs, TRAP_BRKPT);
277 #ifdef PRINT_USER_FAULTS
278 printk(KERN_DEBUG "break %#08x: pid=%d command='%s'\n",
279 iir, current->pid, current->comm);
282 si.si_signo = SIGTRAP;
283 si.si_code = TRAP_BRKPT;
284 si.si_addr = (void *) (regs->iaoq[0] & ~3);
285 force_sig_info(SIGTRAP, &si, current);
293 printk(KERN_CRIT "TOC call.\n");
297 static void default_trap(int code, struct pt_regs *regs)
299 printk(KERN_ERR "Trap %d on CPU %d\n", code, smp_processor_id());
303 void (*cpu_lpmc) (int code, struct pt_regs *regs) = default_trap;
306 void transfer_pim_to_trap_frame(struct pt_regs *regs)
309 extern unsigned int hpmc_pim_data[];
310 struct pdc_hpmc_pim_11 *pim_narrow;
311 struct pdc_hpmc_pim_20 *pim_wide;
313 if (boot_cpu_data.cpu_type >= pcxu) {
315 pim_wide = (struct pdc_hpmc_pim_20 *)hpmc_pim_data;
318 * Note: The following code will probably generate a
319 * bunch of truncation error warnings from the compiler.
320 * Could be handled with an ifdef, but perhaps there
324 regs->gr[0] = pim_wide->cr[22];
326 for (i = 1; i < 32; i++)
327 regs->gr[i] = pim_wide->gr[i];
329 for (i = 0; i < 32; i++)
330 regs->fr[i] = pim_wide->fr[i];
332 for (i = 0; i < 8; i++)
333 regs->sr[i] = pim_wide->sr[i];
335 regs->iasq[0] = pim_wide->cr[17];
336 regs->iasq[1] = pim_wide->iasq_back;
337 regs->iaoq[0] = pim_wide->cr[18];
338 regs->iaoq[1] = pim_wide->iaoq_back;
340 regs->sar = pim_wide->cr[11];
341 regs->iir = pim_wide->cr[19];
342 regs->isr = pim_wide->cr[20];
343 regs->ior = pim_wide->cr[21];
346 pim_narrow = (struct pdc_hpmc_pim_11 *)hpmc_pim_data;
348 regs->gr[0] = pim_narrow->cr[22];
350 for (i = 1; i < 32; i++)
351 regs->gr[i] = pim_narrow->gr[i];
353 for (i = 0; i < 32; i++)
354 regs->fr[i] = pim_narrow->fr[i];
356 for (i = 0; i < 8; i++)
357 regs->sr[i] = pim_narrow->sr[i];
359 regs->iasq[0] = pim_narrow->cr[17];
360 regs->iasq[1] = pim_narrow->iasq_back;
361 regs->iaoq[0] = pim_narrow->cr[18];
362 regs->iaoq[1] = pim_narrow->iaoq_back;
364 regs->sar = pim_narrow->cr[11];
365 regs->iir = pim_narrow->cr[19];
366 regs->isr = pim_narrow->cr[20];
367 regs->ior = pim_narrow->cr[21];
371 * The following fields only have meaning if we came through
372 * another path. So just zero them here.
382 * This routine is called as a last resort when everything else
383 * has gone clearly wrong. We get called for faults in kernel space,
386 void parisc_terminate(char *msg, struct pt_regs *regs, int code, unsigned long offset)
388 static DEFINE_SPINLOCK(terminate_lock);
390 oops_in_progress = 1;
394 spin_lock(&terminate_lock);
396 /* unlock the pdc lock if necessary */
397 pdc_emergency_unlock();
399 /* restart pdc console if necessary */
400 if (!console_drivers)
401 pdc_console_restart();
403 /* Not all paths will gutter the processor... */
407 transfer_pim_to_trap_frame(regs);
417 /* show_stack(NULL, (unsigned long *)regs->gr[30]); */
418 struct unwind_frame_info info;
419 unwind_frame_init(&info, current, regs->gr[30], regs->iaoq[0], regs->gr[2]);
420 do_show_stack(&info);
424 printk(KERN_CRIT "%s: Code=%d regs=%p (Addr=" RFMT ")\n",
425 msg, code, regs, offset);
428 spin_unlock(&terminate_lock);
430 /* put soft power button back under hardware control;
431 * if the user had pressed it once at any time, the
432 * system will shut down immediately right here. */
433 pdc_soft_power_button(0);
435 /* Call kernel panic() so reboot timeouts work properly
436 * FIXME: This function should be on the list of
437 * panic notifiers, and we should call panic
438 * directly from the location that we wish.
439 * e.g. We should not call panic from
440 * parisc_terminate, but rather the oter way around.
441 * This hack works, prints the panic message twice,
442 * and it enables reboot timers!
447 void handle_interruption(int code, struct pt_regs *regs)
449 unsigned long fault_address = 0;
450 unsigned long fault_space = 0;
454 pdc_console_restart(); /* switch back to pdc if HPMC */
459 * If the priority level is still user, and the
460 * faulting space is not equal to the active space
461 * then the user is attempting something in a space
462 * that does not belong to them. Kill the process.
464 * This is normally the situation when the user
465 * attempts to jump into the kernel space at the
466 * wrong offset, be it at the gateway page or a
469 * We cannot normally signal the process because it
470 * could *be* on the gateway page, and processes
471 * executing on the gateway page can't have signals
474 * We merely readjust the address into the users
475 * space, at a destination address of zero, and
476 * allow processing to continue.
478 if (((unsigned long)regs->iaoq[0] & 3) &&
479 ((unsigned long)regs->iasq[0] != (unsigned long)regs->sr[7])) {
480 /* Kill the user process later */
481 regs->iaoq[0] = 0 | 3;
482 regs->iaoq[1] = regs->iaoq[0] + 4;
483 regs->iasq[0] = regs->iasq[0] = regs->sr[7];
484 regs->gr[0] &= ~PSW_B;
489 printk(KERN_CRIT "Interruption # %d\n", code);
495 /* High-priority machine check (HPMC) */
497 /* set up a new led state on systems shipped with a LED State panel */
498 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_HPMC);
500 parisc_terminate("High Priority Machine Check (HPMC)",
505 /* Power failure interrupt */
506 printk(KERN_CRIT "Power failure interrupt !\n");
510 /* Recovery counter trap */
511 regs->gr[0] &= ~PSW_R;
512 if (user_space(regs))
513 handle_gdb_break(regs, TRAP_TRACE);
514 /* else this must be the start of a syscall - just let it run */
518 /* Low-priority machine check */
519 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_LPMC);
526 /* Instruction TLB miss fault/Instruction page fault */
527 fault_address = regs->iaoq[0];
528 fault_space = regs->iasq[0];
532 /* Illegal instruction trap */
533 die_if_kernel("Illegal instruction", regs, code);
534 si.si_code = ILL_ILLOPC;
538 /* Break instruction trap */
539 handle_break(regs->iir,regs);
543 /* Privileged operation trap */
544 die_if_kernel("Privileged operation", regs, code);
545 si.si_code = ILL_PRVOPC;
549 /* Privileged register trap */
550 if ((regs->iir & 0xffdfffe0) == 0x034008a0) {
552 /* This is a MFCTL cr26/cr27 to gr instruction.
553 * PCXS traps on this, so we need to emulate it.
556 if (regs->iir & 0x00200000)
557 regs->gr[regs->iir & 0x1f] = mfctl(27);
559 regs->gr[regs->iir & 0x1f] = mfctl(26);
561 regs->iaoq[0] = regs->iaoq[1];
563 regs->iasq[0] = regs->iasq[1];
567 die_if_kernel("Privileged register usage", regs, code);
568 si.si_code = ILL_PRVREG;
570 si.si_signo = SIGILL;
572 si.si_addr = (void *) regs->iaoq[0];
573 force_sig_info(SIGILL, &si, current);
577 /* Overflow Trap, let the userland signal handler do the cleanup */
578 si.si_signo = SIGFPE;
579 si.si_code = FPE_INTOVF;
580 si.si_addr = (void *) regs->iaoq[0];
581 force_sig_info(SIGFPE, &si, current);
586 The condition succees in an instruction which traps
589 si.si_signo = SIGFPE;
590 /* Set to zero, and let the userspace app figure it out from
591 the insn pointed to by si_addr */
593 si.si_addr = (void __user *) regs->iaoq[0];
594 force_sig_info(SIGFPE, &si, current);
597 /* The kernel doesn't want to handle condition codes */
601 /* Assist Exception Trap, i.e. floating point exception. */
602 die_if_kernel("Floating point exception", regs, 0); /* quiet */
607 /* Data TLB miss fault/Data page fault */
610 /* Non-access instruction TLB miss fault */
611 /* The instruction TLB entry needed for the target address of the FIC
612 is absent, and hardware can't find it, so we get to cleanup */
615 /* Non-access data TLB miss fault/Non-access data page fault */
617 Still need to add slow path emulation code here!
618 If the insn used a non-shadow register, then the tlb
619 handlers could not have their side-effect (e.g. probe
620 writing to a target register) emulated since rfir would
621 erase the changes to said register. Instead we have to
622 setup everything, call this function we are in, and emulate
623 by hand. Technically we need to emulate:
624 fdc,fdce,pdc,"fic,4f",prober,probeir,probew, probeiw
626 fault_address = regs->ior;
627 fault_space = regs->isr;
631 /* PCXS only -- later cpu's split this into types 26,27 & 28 */
632 /* Check for unaligned access */
633 if (check_unaligned(regs)) {
634 handle_unaligned(regs);
639 /* PCXL: Data memory access rights trap */
640 fault_address = regs->ior;
641 fault_space = regs->isr;
645 /* Data memory break trap */
646 regs->gr[0] |= PSW_X; /* So we can single-step over the trap */
649 /* Page reference trap */
650 handle_gdb_break(regs, TRAP_HWBKPT);
654 /* Taken branch trap */
655 regs->gr[0] &= ~PSW_T;
656 if (user_space(regs))
657 handle_gdb_break(regs, TRAP_BRANCH);
658 /* else this must be the start of a syscall - just let it
664 /* Instruction access rights */
665 /* PCXL: Instruction memory protection trap */
668 * This could be caused by either: 1) a process attempting
669 * to execute within a vma that does not have execute
670 * permission, or 2) an access rights violation caused by a
671 * flush only translation set up by ptep_get_and_clear().
672 * So we check the vma permissions to differentiate the two.
673 * If the vma indicates we have execute permission, then
674 * the cause is the latter one. In this case, we need to
675 * call do_page_fault() to fix the problem.
678 if (user_mode(regs)) {
679 struct vm_area_struct *vma;
681 down_read(¤t->mm->mmap_sem);
682 vma = find_vma(current->mm,regs->iaoq[0]);
683 if (vma && (regs->iaoq[0] >= vma->vm_start)
684 && (vma->vm_flags & VM_EXEC)) {
686 fault_address = regs->iaoq[0];
687 fault_space = regs->iasq[0];
689 up_read(¤t->mm->mmap_sem);
690 break; /* call do_page_fault() */
692 up_read(¤t->mm->mmap_sem);
696 /* Data memory protection ID trap */
697 die_if_kernel("Protection id trap", regs, code);
698 si.si_code = SEGV_MAPERR;
699 si.si_signo = SIGSEGV;
702 si.si_addr = (void *) regs->iaoq[0];
704 si.si_addr = (void *) regs->ior;
705 force_sig_info(SIGSEGV, &si, current);
709 /* Unaligned data reference trap */
710 handle_unaligned(regs);
714 if (user_mode(regs)) {
715 #ifdef PRINT_USER_FAULTS
716 printk(KERN_DEBUG "\nhandle_interruption() pid=%d command='%s'\n",
717 current->pid, current->comm);
720 /* SIGBUS, for lack of a better one. */
721 si.si_signo = SIGBUS;
722 si.si_code = BUS_OBJERR;
724 si.si_addr = (void *) regs->ior;
725 force_sig_info(SIGBUS, &si, current);
728 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
730 parisc_terminate("Unexpected interruption", regs, code, 0);
734 if (user_mode(regs)) {
735 if ((fault_space>>SPACEID_SHIFT) != (regs->sr[7] >> SPACEID_SHIFT)) {
736 #ifdef PRINT_USER_FAULTS
737 if (fault_space == 0)
738 printk(KERN_DEBUG "User Fault on Kernel Space ");
740 printk(KERN_DEBUG "User Fault (long pointer) (fault %d) ",
742 printk("pid=%d command='%s'\n", current->pid, current->comm);
745 si.si_signo = SIGSEGV;
747 si.si_code = SEGV_MAPERR;
748 si.si_addr = (void *) regs->ior;
749 force_sig_info(SIGSEGV, &si, current);
756 * The kernel should never fault on its own address space.
759 if (fault_space == 0)
761 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
762 parisc_terminate("Kernel Fault", regs, code, fault_address);
767 do_page_fault(regs, code, fault_address);
771 int __init check_ivt(void *iva)
778 extern void os_hpmc(void);
779 extern void os_hpmc_end(void);
781 if (strcmp((char *)iva, "cows can fly"))
786 for (i = 0; i < 8; i++)
789 /* Compute Checksum for HPMC handler */
791 length = (u32)((unsigned long)os_hpmc_end - (unsigned long)os_hpmc);
794 hpmcp = (u32 *)os_hpmc;
796 for (i=0; i<length/4; i++)
808 extern const void fault_vector_11;
810 extern const void fault_vector_20;
812 void __init trap_init(void)
816 if (boot_cpu_data.cpu_type >= pcxu)
817 iva = (void *) &fault_vector_20;
820 panic("Can't boot 64-bit OS on PA1.1 processor!");
822 iva = (void *) &fault_vector_11;
826 panic("IVT invalid");