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>
42 #include "../math-emu/math-emu.h" /* for handle_fpe() */
44 #define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
45 /* dumped to the console via printk) */
47 int printbinary(char *buf, unsigned long x, int nbits)
49 unsigned long mask = 1UL << (nbits - 1);
51 *buf++ = (mask & x ? '1' : '0');
65 void show_regs(struct pt_regs *regs)
73 level = user_mode(regs) ? KERN_DEBUG : KERN_CRIT;
75 printk("%s\n", level); /* don't want to have that pretty register dump messed up */
77 printk("%s YZrvWESTHLNXBCVMcbcbcbcbOGFRQPDI\n", level);
78 printbinary(buf, regs->gr[0], 32);
79 printk("%sPSW: %s %s\n", level, buf, print_tainted());
81 for (i = 0; i < 32; i += 4) {
84 p += sprintf(p, "%sr%02d-%02d ", level, i, i + 3);
85 for (j = 0; j < 4; j++) {
86 p += sprintf(p, " " RFMT, (i+j) == 0 ? 0 : regs->gr[i + j]);
91 for (i = 0; i < 8; i += 4) {
94 p += sprintf(p, "%ssr%d-%d ", level, i, i + 3);
95 for (j = 0; j < 4; j++) {
96 p += sprintf(p, " " RFMT, regs->sr[i + j]);
101 #if RIDICULOUSLY_VERBOSE
102 for (i = 0; i < 32; i += 2)
103 printk("%sFR%02d : %016lx FR%2d : %016lx", level, i,
104 regs->fr[i], i+1, regs->fr[i+1]);
109 printk("%s\n", level);
110 printk("%sIASQ: " RFMT " " RFMT " IAOQ: " RFMT " " RFMT "\n",
111 level, regs->iasq[0], regs->iasq[1], regs->iaoq[0], regs->iaoq[1]);
112 printk("%s IIR: %08lx ISR: " RFMT " IOR: " RFMT "\n",
113 level, regs->iir, regs->isr, regs->ior);
114 printk("%s CPU: %8d CR30: " RFMT " CR31: " RFMT "\n",
115 level, current_thread_info()->cpu, cr30, cr31);
116 printk("%s ORIG_R28: " RFMT "\n", level, regs->orig_r28);
118 print_symbol(" IAOQ[0]: %s\n", regs->iaoq[0]);
120 print_symbol(" IAOQ[1]: %s\n", regs->iaoq[1]);
122 print_symbol(" RP(r2): %s\n", regs->gr[2]);
126 void dump_stack(void)
129 show_trace(current, &stack);
132 EXPORT_SYMBOL(dump_stack);
135 static int kstack_depth_to_print = 64 * 4;
137 static int kstack_depth_to_print = 128 * 4;
140 void show_stack(struct task_struct *task, unsigned long *sp)
142 unsigned long *stack;
146 * debugging aid: "show_stack(NULL);" prints the
147 * back trace for this cpu.
150 sp = (unsigned long*)&sp;
152 sp = (unsigned long*)task->thread.regs.ksp;
155 printk("\n" KERN_CRIT "Stack Dump:\n");
156 printk(KERN_CRIT " " RFMT ": ", (unsigned long) stack);
157 for (i=0; i < kstack_depth_to_print; i++) {
158 if (((long) stack & (THREAD_SIZE-1)) == 0)
160 if (i && ((i & 0x03) == 0))
161 printk("\n" KERN_CRIT " " RFMT ": ",
162 (unsigned long) stack);
163 printk(RFMT " ", *stack--);
165 printk("\n" KERN_CRIT "\n");
166 show_trace(task, sp);
170 void show_trace(struct task_struct *task, unsigned long *stack)
172 unsigned long *startstack;
176 startstack = (unsigned long *)((unsigned long)stack & ~(THREAD_SIZE - 1));
178 stack = (long *)((long)(stack + 32) &~ (FRAME_SIZE-1)); /* Align */
179 printk("Kernel addresses on the stack:\n");
180 while (stack > startstack) {
181 stack -= 16; /* Stack frames are a multiple of 16 words */
182 addr = stack[16 - RP_OFFSET / sizeof(long)];
184 * If the address is either in the text segment of the
185 * kernel, or in the region which contains vmalloc'ed
186 * memory, it *may* be the address of a calling
187 * routine; if so, print it so that someone tracing
188 * down the cause of the crash will be able to figure
189 * out the call path that was taken.
191 if (__kernel_text_address(addr)) {
192 printk(" [<" RFMT ">] ", addr);
193 #ifdef CONFIG_KALLSYMS
194 print_symbol("%s\n", addr);
205 void die_if_kernel(char *str, struct pt_regs *regs, long err)
207 if (user_mode(regs)) {
211 printk(KERN_CRIT "%s (pid %d): %s (code %ld) at " RFMT "\n",
212 current->comm, current->pid, str, err, regs->iaoq[0]);
213 #ifdef PRINT_USER_FAULTS
214 /* XXX for debugging only */
220 oops_in_progress = 1;
222 /* Amuse the user in a SPARC fashion */
224 " _______________________________ \n"
225 " < Your System ate a SPARC! Gah! >\n"
226 " ------------------------------- \n"
228 " \\ (xx)\\_______\n"
233 /* unlock the pdc lock if necessary */
234 pdc_emergency_unlock();
236 /* maybe the kernel hasn't booted very far yet and hasn't been able
237 * to initialize the serial or STI console. In that case we should
238 * re-enable the pdc console, so that the user will be able to
239 * identify the problem. */
240 if (!console_drivers)
241 pdc_console_restart();
243 printk(KERN_CRIT "%s (pid %d): %s (code %ld)\n",
244 current->comm, current->pid, str, err);
247 /* Wot's wrong wif bein' racy? */
248 if (current->thread.flags & PARISC_KERNEL_DEATH) {
249 printk(KERN_CRIT "%s() recursion detected.\n", __FUNCTION__);
254 current->thread.flags |= PARISC_KERNEL_DEATH;
258 int syscall_ipi(int (*syscall) (struct pt_regs *), struct pt_regs *regs)
260 return syscall(regs);
263 /* gdb uses break 4,8 */
264 #define GDB_BREAK_INSN 0x10004
265 void handle_gdb_break(struct pt_regs *regs, int wot)
270 si.si_addr = (void *) (regs->iaoq[0] & ~3);
271 si.si_signo = SIGTRAP;
273 force_sig_info(SIGTRAP, &si, current);
276 void handle_break(unsigned iir, struct pt_regs *regs)
282 #ifdef PRINT_USER_FAULTS
283 printk(KERN_DEBUG "break 0,0: pid=%d command='%s'\n",
284 current->pid, current->comm);
286 die_if_kernel("Breakpoint", regs, 0);
287 #ifdef PRINT_USER_FAULTS
290 si.si_code = TRAP_BRKPT;
291 si.si_addr = (void *) (regs->iaoq[0] & ~3);
292 si.si_signo = SIGTRAP;
293 force_sig_info(SIGTRAP, &si, current);
297 die_if_kernel("Breakpoint", regs, 0);
298 handle_gdb_break(regs, TRAP_BRKPT);
302 #ifdef PRINT_USER_FAULTS
303 printk(KERN_DEBUG "break %#08x: pid=%d command='%s'\n",
304 iir, current->pid, current->comm);
307 si.si_signo = SIGTRAP;
308 si.si_code = TRAP_BRKPT;
309 si.si_addr = (void *) (regs->iaoq[0] & ~3);
310 force_sig_info(SIGTRAP, &si, current);
318 printk(KERN_CRIT "TOC call.\n");
322 static void default_trap(int code, struct pt_regs *regs)
324 printk(KERN_ERR "Trap %d on CPU %d\n", code, smp_processor_id());
328 void (*cpu_lpmc) (int code, struct pt_regs *regs) = default_trap;
331 void transfer_pim_to_trap_frame(struct pt_regs *regs)
334 extern unsigned int hpmc_pim_data[];
335 struct pdc_hpmc_pim_11 *pim_narrow;
336 struct pdc_hpmc_pim_20 *pim_wide;
338 if (boot_cpu_data.cpu_type >= pcxu) {
340 pim_wide = (struct pdc_hpmc_pim_20 *)hpmc_pim_data;
343 * Note: The following code will probably generate a
344 * bunch of truncation error warnings from the compiler.
345 * Could be handled with an ifdef, but perhaps there
349 regs->gr[0] = pim_wide->cr[22];
351 for (i = 1; i < 32; i++)
352 regs->gr[i] = pim_wide->gr[i];
354 for (i = 0; i < 32; i++)
355 regs->fr[i] = pim_wide->fr[i];
357 for (i = 0; i < 8; i++)
358 regs->sr[i] = pim_wide->sr[i];
360 regs->iasq[0] = pim_wide->cr[17];
361 regs->iasq[1] = pim_wide->iasq_back;
362 regs->iaoq[0] = pim_wide->cr[18];
363 regs->iaoq[1] = pim_wide->iaoq_back;
365 regs->sar = pim_wide->cr[11];
366 regs->iir = pim_wide->cr[19];
367 regs->isr = pim_wide->cr[20];
368 regs->ior = pim_wide->cr[21];
371 pim_narrow = (struct pdc_hpmc_pim_11 *)hpmc_pim_data;
373 regs->gr[0] = pim_narrow->cr[22];
375 for (i = 1; i < 32; i++)
376 regs->gr[i] = pim_narrow->gr[i];
378 for (i = 0; i < 32; i++)
379 regs->fr[i] = pim_narrow->fr[i];
381 for (i = 0; i < 8; i++)
382 regs->sr[i] = pim_narrow->sr[i];
384 regs->iasq[0] = pim_narrow->cr[17];
385 regs->iasq[1] = pim_narrow->iasq_back;
386 regs->iaoq[0] = pim_narrow->cr[18];
387 regs->iaoq[1] = pim_narrow->iaoq_back;
389 regs->sar = pim_narrow->cr[11];
390 regs->iir = pim_narrow->cr[19];
391 regs->isr = pim_narrow->cr[20];
392 regs->ior = pim_narrow->cr[21];
396 * The following fields only have meaning if we came through
397 * another path. So just zero them here.
407 * This routine handles page faults. It determines the address,
408 * and the problem, and then passes it off to one of the appropriate
411 void parisc_terminate(char *msg, struct pt_regs *regs, int code, unsigned long offset)
413 static spinlock_t terminate_lock = SPIN_LOCK_UNLOCKED;
415 oops_in_progress = 1;
419 spin_lock(&terminate_lock);
421 /* unlock the pdc lock if necessary */
422 pdc_emergency_unlock();
424 /* restart pdc console if necessary */
425 if (!console_drivers)
426 pdc_console_restart();
428 /* Not all paths will gutter the processor... */
432 transfer_pim_to_trap_frame(regs);
441 show_stack(NULL, (unsigned long *)regs->gr[30]);
444 printk(KERN_CRIT "%s: Code=%d regs=%p (Addr=" RFMT ")\n",
445 msg, code, regs, offset);
448 spin_unlock(&terminate_lock);
450 /* put soft power button back under hardware control;
451 * if the user had pressed it once at any time, the
452 * system will shut down immediately right here. */
453 pdc_soft_power_button(0);
455 /* Gutter the processor! */
460 void handle_interruption(int code, struct pt_regs *regs)
462 unsigned long fault_address = 0;
463 unsigned long fault_space = 0;
467 pdc_console_restart(); /* switch back to pdc if HPMC */
472 printk(KERN_CRIT "Interruption # %d\n", code);
478 /* High-priority machine check (HPMC) */
480 /* set up a new led state on systems shipped with a LED State panel */
481 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_HPMC);
483 parisc_terminate("High Priority Machine Check (HPMC)",
488 /* Power failure interrupt */
489 printk(KERN_CRIT "Power failure interrupt !\n");
493 /* Recovery counter trap */
494 regs->gr[0] &= ~PSW_R;
496 handle_gdb_break(regs, TRAP_TRACE);
497 /* else this must be the start of a syscall - just let it run */
501 /* Low-priority machine check */
502 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_LPMC);
509 /* Instruction TLB miss fault/Instruction page fault */
510 fault_address = regs->iaoq[0];
511 fault_space = regs->iasq[0];
515 /* Illegal instruction trap */
516 die_if_kernel("Illegal instruction", regs, code);
517 si.si_code = ILL_ILLOPC;
521 /* Break instruction trap */
522 handle_break(regs->iir,regs);
526 /* Privileged operation trap */
527 die_if_kernel("Privileged operation", regs, code);
528 si.si_code = ILL_PRVOPC;
532 /* Privileged register trap */
533 if ((regs->iir & 0xffdfffe0) == 0x034008a0) {
535 /* This is a MFCTL cr26/cr27 to gr instruction.
536 * PCXS traps on this, so we need to emulate it.
539 if (regs->iir & 0x00200000)
540 regs->gr[regs->iir & 0x1f] = mfctl(27);
542 regs->gr[regs->iir & 0x1f] = mfctl(26);
544 regs->iaoq[0] = regs->iaoq[1];
546 regs->iasq[0] = regs->iasq[1];
550 die_if_kernel("Privileged register usage", regs, code);
551 si.si_code = ILL_PRVREG;
553 si.si_signo = SIGILL;
555 si.si_addr = (void *) regs->iaoq[0];
556 force_sig_info(SIGILL, &si, current);
560 /* Overflow Trap, let the userland signal handler do the cleanup */
561 si.si_signo = SIGFPE;
562 si.si_code = FPE_INTOVF;
563 si.si_addr = (void *) regs->iaoq[0];
564 force_sig_info(SIGFPE, &si, current);
569 The condition succees in an instruction which traps
572 si.si_signo = SIGFPE;
573 /* Set to zero, and let the userspace app figure it out from
574 the insn pointed to by si_addr */
576 si.si_addr = (void *) regs->iaoq[0];
577 force_sig_info(SIGFPE, &si, current);
580 /* The kernel doesn't want to handle condition codes */
584 /* Assist Exception Trap, i.e. floating point exception. */
585 die_if_kernel("Floating point exception", regs, 0); /* quiet */
590 /* Data TLB miss fault/Data page fault */
593 /* Non-access instruction TLB miss fault */
594 /* The instruction TLB entry needed for the target address of the FIC
595 is absent, and hardware can't find it, so we get to cleanup */
598 /* Non-access data TLB miss fault/Non-access data page fault */
599 /* TODO: Still need to add slow path emulation code here */
600 /* TODO: Understand what is meant by the TODO listed
601 above this one. (Carlos) */
602 fault_address = regs->ior;
603 fault_space = regs->isr;
607 /* PCXS only -- later cpu's split this into types 26,27 & 28 */
608 /* Check for unaligned access */
609 if (check_unaligned(regs)) {
610 handle_unaligned(regs);
615 /* PCXL: Data memory access rights trap */
616 fault_address = regs->ior;
617 fault_space = regs->isr;
621 /* Data memory break trap */
622 regs->gr[0] |= PSW_X; /* So we can single-step over the trap */
625 /* Page reference trap */
626 handle_gdb_break(regs, TRAP_HWBKPT);
630 /* Taken branch trap */
631 regs->gr[0] &= ~PSW_T;
633 handle_gdb_break(regs, TRAP_BRANCH);
634 /* else this must be the start of a syscall - just let it
640 /* Instruction access rights */
641 /* PCXL: Instruction memory protection trap */
644 * This could be caused by either: 1) a process attempting
645 * to execute within a vma that does not have execute
646 * permission, or 2) an access rights violation caused by a
647 * flush only translation set up by ptep_get_and_clear().
648 * So we check the vma permissions to differentiate the two.
649 * If the vma indicates we have execute permission, then
650 * the cause is the latter one. In this case, we need to
651 * call do_page_fault() to fix the problem.
654 if (user_mode(regs)) {
655 struct vm_area_struct *vma;
657 down_read(¤t->mm->mmap_sem);
658 vma = find_vma(current->mm,regs->iaoq[0]);
659 if (vma && (regs->iaoq[0] >= vma->vm_start)
660 && (vma->vm_flags & VM_EXEC)) {
662 fault_address = regs->iaoq[0];
663 fault_space = regs->iasq[0];
665 up_read(¤t->mm->mmap_sem);
666 break; /* call do_page_fault() */
668 up_read(¤t->mm->mmap_sem);
672 /* Data memory protection ID trap */
673 die_if_kernel("Protection id trap", regs, code);
674 si.si_code = SEGV_MAPERR;
675 si.si_signo = SIGSEGV;
678 si.si_addr = (void *) regs->iaoq[0];
680 si.si_addr = (void *) regs->ior;
681 force_sig_info(SIGSEGV, &si, current);
685 /* Unaligned data reference trap */
686 handle_unaligned(regs);
690 if (user_mode(regs)) {
691 #ifdef PRINT_USER_FAULTS
692 printk(KERN_DEBUG "\nhandle_interruption() pid=%d command='%s'\n",
693 current->pid, current->comm);
696 /* SIGBUS, for lack of a better one. */
697 si.si_signo = SIGBUS;
698 si.si_code = BUS_OBJERR;
700 si.si_addr = (void *) regs->ior;
701 force_sig_info(SIGBUS, &si, current);
704 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
706 parisc_terminate("Unexpected interruption", regs, code, 0);
710 if (user_mode(regs)) {
711 if ((fault_space>>SPACEID_SHIFT) != (regs->sr[7] >> SPACEID_SHIFT)) {
712 #ifdef PRINT_USER_FAULTS
713 if (fault_space == 0)
714 printk(KERN_DEBUG "User Fault on Kernel Space ");
716 printk(KERN_DEBUG "User Fault (long pointer) (fault %d) ",
718 printk("pid=%d command='%s'\n", current->pid, current->comm);
721 si.si_signo = SIGSEGV;
723 si.si_code = SEGV_MAPERR;
724 si.si_addr = (void *) regs->ior;
725 force_sig_info(SIGSEGV, &si, current);
732 * The kernel should never fault on its own address space.
735 if (fault_space == 0)
737 pdc_chassis_send_status(PDC_CHASSIS_DIRECT_PANIC);
738 parisc_terminate("Kernel Fault", regs, code, fault_address);
743 do_page_fault(regs, code, fault_address);
747 int __init check_ivt(void *iva)
754 extern void os_hpmc(void);
755 extern void os_hpmc_end(void);
757 if (strcmp((char *)iva, "cows can fly"))
762 for (i = 0; i < 8; i++)
765 /* Compute Checksum for HPMC handler */
767 length = (u32)((unsigned long)os_hpmc_end - (unsigned long)os_hpmc);
770 hpmcp = (u32 *)os_hpmc;
772 for (i=0; i<length/4; i++)
784 extern const void fault_vector_11;
786 extern const void fault_vector_20;
788 void __init trap_init(void)
792 if (boot_cpu_data.cpu_type >= pcxu)
793 iva = (void *) &fault_vector_20;
796 panic("Can't boot 64-bit OS on PA1.1 processor!");
798 iva = (void *) &fault_vector_11;
802 panic("IVT invalid");