vserver 2.0 rc7
[linux-2.6.git] / arch / parisc / kernel / traps.c
index 6f1f6f7..d2e5b22 100644 (file)
 #include <asm/smp.h>
 #include <asm/pdc.h>
 #include <asm/pdc_chassis.h>
+#include <asm/unwind.h>
 
 #include "../math-emu/math-emu.h"      /* for handle_fpe() */
 
 #define PRINT_USER_FAULTS /* (turn this on if you want user faults to be */
                          /*  dumped to the console via printk)          */
 
+#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
+DEFINE_SPINLOCK(pa_dbit_lock);
+#endif
+
 int printbinary(char *buf, unsigned long x, int nbits)
 {
        unsigned long mask = 1UL << (nbits - 1);
@@ -125,73 +130,24 @@ void show_regs(struct pt_regs *regs)
 
 void dump_stack(void)
 {
-       unsigned long stack;
-       show_trace(current, &stack);
+       show_stack(NULL, NULL);
 }
 
 EXPORT_SYMBOL(dump_stack);
 
-#ifndef __LP64__
-static int kstack_depth_to_print = 64 * 4;
-#else
-static int kstack_depth_to_print = 128 * 4;
-#endif
-
-void show_stack(struct task_struct *task, unsigned long *sp)
+static void do_show_stack(struct unwind_frame_info *info)
 {
-       unsigned long *stack;
-       int i;
+       int i = 1;
 
-       /*
-        * debugging aid: "show_stack(NULL);" prints the
-        * back trace for this cpu.
-        */
-       if (task==NULL)
-               sp = (unsigned long*)&sp;
-       else if(sp == NULL)
-               sp = (unsigned long*)task->thread.regs.ksp;
-
-       stack = sp;
-       printk("\n" KERN_CRIT "Stack Dump:\n");
-       printk(KERN_CRIT " " RFMT ":  ", (unsigned long) stack);
-       for (i=0; i < kstack_depth_to_print; i++) {
-               if (((long) stack & (THREAD_SIZE-1)) == 0)
+       printk("Backtrace:\n");
+       while (i <= 16) {
+               if (unwind_once(info) < 0 || info->ip == 0)
                        break;
-               if (i && ((i & 0x03) == 0))
-                       printk("\n" KERN_CRIT " " RFMT ":  ",
-                               (unsigned long) stack);
-               printk(RFMT " ", *stack--);
-       }
-       printk("\n" KERN_CRIT "\n");
-       show_trace(task, sp);
-}
-
-
-void show_trace(struct task_struct *task, unsigned long *stack)
-{
-       unsigned long *startstack;
-       unsigned long addr;
-       int i;
 
-       startstack = (unsigned long *)((unsigned long)stack & ~(THREAD_SIZE - 1));
-       i = 1;
-       stack = (long *)((long)(stack + 32) &~ (FRAME_SIZE-1)); /* Align */
-       printk("Kernel addresses on the stack:\n");
-       while (stack > startstack) {
-               stack -= 16;    /* Stack frames are a multiple of 16 words */
-               addr = stack[16 - RP_OFFSET / sizeof(long)];
-               /*
-                * If the address is either in the text segment of the
-                * kernel, or in the region which contains vmalloc'ed
-                * memory, it *may* be the address of a calling
-                * routine; if so, print it so that someone tracing
-                * down the cause of the crash will be able to figure
-                * out the call path that was taken.
-                */
-               if (kernel_text_address(addr)) {
-                       printk(" [<" RFMT ">] ", addr);
+               if (__kernel_text_address(info->ip)) {
+                       printk(" [<" RFMT ">] ", info->ip);
 #ifdef CONFIG_KALLSYMS
-                       print_symbol("%s\n", addr);
+                       print_symbol("%s\n", info->ip);
 #else
                        if ((i & 0x03) == 0)
                                printk("\n");
@@ -202,6 +158,32 @@ void show_trace(struct task_struct *task, unsigned long *stack)
        printk("\n");
 }
 
+void show_stack(struct task_struct *task, unsigned long *s)
+{
+       struct unwind_frame_info info;
+
+       if (!task) {
+               unsigned long sp;
+               struct pt_regs *r;
+
+HERE:
+               asm volatile ("copy %%r30, %0" : "=r"(sp));
+               r = (struct pt_regs *)kmalloc(sizeof(struct pt_regs), GFP_KERNEL);
+               if (!r)
+                       return;
+               memset(r, 0, sizeof(struct pt_regs));
+               r->iaoq[0] = (unsigned long)&&HERE;
+               r->gr[2] = (unsigned long)__builtin_return_address(0);
+               r->gr[30] = sp;
+               unwind_frame_init(&info, current, r);
+               kfree(r);
+       } else {
+               unwind_frame_init_from_blocked_task(&info, task);
+       }
+
+       do_show_stack(&info);
+}
+
 void die_if_kernel(char *str, struct pt_regs *regs, long err)
 {
        if (user_mode(regs)) {
@@ -267,7 +249,7 @@ void handle_gdb_break(struct pt_regs *regs, int wot)
        struct siginfo si;
 
        si.si_code = wot;
-       si.si_addr = (void *) (regs->iaoq[0] & ~3);
+       si.si_addr = (void __user *) (regs->iaoq[0] & ~3);
        si.si_signo = SIGTRAP;
        si.si_errno = 0;
        force_sig_info(SIGTRAP, &si, current);
@@ -288,7 +270,7 @@ void handle_break(unsigned iir, struct pt_regs *regs)
                show_regs(regs);
 #endif
                si.si_code = TRAP_BRKPT;
-               si.si_addr = (void *) (regs->iaoq[0] & ~3);
+               si.si_addr = (void __user *) (regs->iaoq[0] & ~3);
                si.si_signo = SIGTRAP;
                force_sig_info(SIGTRAP, &si, current);
                break;
@@ -306,7 +288,7 @@ void handle_break(unsigned iir, struct pt_regs *regs)
 #endif
                si.si_signo = SIGTRAP;
                si.si_code = TRAP_BRKPT;
-               si.si_addr = (void *) (regs->iaoq[0] & ~3);
+               si.si_addr = (void __user *) (regs->iaoq[0] & ~3);
                force_sig_info(SIGTRAP, &si, current);
                return;
        }
@@ -404,13 +386,13 @@ void transfer_pim_to_trap_frame(struct pt_regs *regs)
 
 
 /*
- * This routine handles page faults.  It determines the address,
- * and the problem, and then passes it off to one of the appropriate
- * routines.
+ * This routine is called as a last resort when everything else
+ * has gone clearly wrong. We get called for faults in kernel space,
+ * and HPMC's.
  */
 void parisc_terminate(char *msg, struct pt_regs *regs, int code, unsigned long offset)
 {
-       static spinlock_t terminate_lock = SPIN_LOCK_UNLOCKED;
+       static DEFINE_SPINLOCK(terminate_lock);
 
        oops_in_progress = 1;
 
@@ -438,7 +420,12 @@ void parisc_terminate(char *msg, struct pt_regs *regs, int code, unsigned long o
 
        }
            
-       show_stack(NULL, (unsigned long *)regs->gr[30]);
+       {
+               /* show_stack(NULL, (unsigned long *)regs->gr[30]); */
+               struct unwind_frame_info info;
+               unwind_frame_init(&info, current, regs);
+               do_show_stack(&info);
+       }
 
        printk("\n");
        printk(KERN_CRIT "%s: Code=%d regs=%p (Addr=" RFMT ")\n",
@@ -452,9 +439,16 @@ void parisc_terminate(char *msg, struct pt_regs *regs, int code, unsigned long o
         * system will shut down immediately right here. */
        pdc_soft_power_button(0);
        
-       /* Gutter the processor! */
-       for(;;)
-           ;
+       /* Call kernel panic() so reboot timeouts work properly 
+        * FIXME: This function should be on the list of
+        * panic notifiers, and we should call panic
+        * directly from the location that we wish. 
+        * e.g. We should not call panic from
+        * parisc_terminate, but rather the oter way around.
+        * This hack works, prints the panic message twice,
+        * and it enables reboot timers!
+        */
+       panic(msg);
 }
 
 void handle_interruption(int code, struct pt_regs *regs)
@@ -468,6 +462,36 @@ void handle_interruption(int code, struct pt_regs *regs)
        else
            local_irq_enable();
 
+       /* Security check:
+        * If the priority level is still user, and the
+        * faulting space is not equal to the active space
+        * then the user is attempting something in a space
+        * that does not belong to them. Kill the process.
+        *
+        * This is normally the situation when the user
+        * attempts to jump into the kernel space at the
+        * wrong offset, be it at the gateway page or a
+        * random location.
+        *
+        * We cannot normally signal the process because it
+        * could *be* on the gateway page, and processes
+        * executing on the gateway page can't have signals
+        * delivered.
+        * 
+        * We merely readjust the address into the users
+        * space, at a destination address of zero, and
+        * allow processing to continue.
+        */
+       if (((unsigned long)regs->iaoq[0] & 3) &&
+           ((unsigned long)regs->iasq[0] != (unsigned long)regs->sr[7])) { 
+               /* Kill the user process later */
+               regs->iaoq[0] = 0 | 3;
+               regs->iaoq[1] = regs->iaoq[0] + 4;
+               regs->iasq[0] = regs->iasq[0] = regs->sr[7];
+               regs->gr[0] &= ~PSW_B;
+               return;
+       }
+       
 #if 0
        printk(KERN_CRIT "Interruption # %d\n", code);
 #endif
@@ -492,7 +516,7 @@ void handle_interruption(int code, struct pt_regs *regs)
        case  3:
                /* Recovery counter trap */
                regs->gr[0] &= ~PSW_R;
-               if (regs->iasq[0])
+               if (user_space(regs))
                        handle_gdb_break(regs, TRAP_TRACE);
                /* else this must be the start of a syscall - just let it run */
                return;
@@ -552,7 +576,7 @@ void handle_interruption(int code, struct pt_regs *regs)
        give_sigill:
                si.si_signo = SIGILL;
                si.si_errno = 0;
-               si.si_addr = (void *) regs->iaoq[0];
+               si.si_addr = (void __user *) regs->iaoq[0];
                force_sig_info(SIGILL, &si, current);
                return;
 
@@ -560,7 +584,7 @@ void handle_interruption(int code, struct pt_regs *regs)
                /* Overflow Trap, let the userland signal handler do the cleanup */
                si.si_signo = SIGFPE;
                si.si_code = FPE_INTOVF;
-               si.si_addr = (void *) regs->iaoq[0];
+               si.si_addr = (void __user *) regs->iaoq[0];
                force_sig_info(SIGFPE, &si, current);
                return;
                
@@ -573,12 +597,12 @@ void handle_interruption(int code, struct pt_regs *regs)
                        /* Set to zero, and let the userspace app figure it out from
                           the insn pointed to by si_addr */
                        si.si_code = 0;
-                       si.si_addr = (void *) regs->iaoq[0];
+                       si.si_addr = (void __user *) regs->iaoq[0];
                        force_sig_info(SIGFPE, &si, current);
                        return;
-               } else 
-                       /* The kernel doesn't want to handle condition codes */
-                       break;
+               } 
+               /* The kernel doesn't want to handle condition codes */
+               break;
                
        case 14:
                /* Assist Exception Trap, i.e. floating point exception. */
@@ -596,9 +620,16 @@ void handle_interruption(int code, struct pt_regs *regs)
                /* Fall through */
        case 17:
                /* Non-access data TLB miss fault/Non-access data page fault */
-               /* TODO: Still need to add slow path emulation code here */
-               /* TODO: Understand what is meant by the TODO listed
-                        above this one. (Carlos) */
+               /* FIXME: 
+                        Still need to add slow path emulation code here!
+                        If the insn used a non-shadow register, then the tlb
+                        handlers could not have their side-effect (e.g. probe
+                        writing to a target register) emulated since rfir would
+                        erase the changes to said register. Instead we have to
+                        setup everything, call this function we are in, and emulate
+                        by hand. Technically we need to emulate:
+                        fdc,fdce,pdc,"fic,4f",prober,probeir,probew, probeiw
+               */                        
                fault_address = regs->ior;
                fault_space = regs->isr;
                break;
@@ -629,7 +660,7 @@ void handle_interruption(int code, struct pt_regs *regs)
        case 25:
                /* Taken branch trap */
                regs->gr[0] &= ~PSW_T;
-               if (regs->iasq[0])
+               if (user_space(regs))
                        handle_gdb_break(regs, TRAP_BRANCH);
                /* else this must be the start of a syscall - just let it
                 * run.
@@ -675,9 +706,9 @@ void handle_interruption(int code, struct pt_regs *regs)
                si.si_signo = SIGSEGV;
                si.si_errno = 0;
                if (code == 7)
-                   si.si_addr = (void *) regs->iaoq[0];
+                   si.si_addr = (void __user *) regs->iaoq[0];
                else
-                   si.si_addr = (void *) regs->ior;
+                   si.si_addr = (void __user *) regs->ior;
                force_sig_info(SIGSEGV, &si, current);
                return;
 
@@ -697,7 +728,7 @@ void handle_interruption(int code, struct pt_regs *regs)
                        si.si_signo = SIGBUS;
                        si.si_code = BUS_OBJERR;
                        si.si_errno = 0;
-                       si.si_addr = (void *) regs->ior;
+                       si.si_addr = (void __user *) regs->ior;
                        force_sig_info(SIGBUS, &si, current);
                        return;
                }
@@ -708,7 +739,7 @@ void handle_interruption(int code, struct pt_regs *regs)
        }
 
        if (user_mode(regs)) {
-           if ((fault_space>>SPACEID_SHIFT) != (regs->sr[7] >> SPACEID_SHIFT)) {
+           if ((fault_space >> SPACEID_SHIFT) != (regs->sr[7] >> SPACEID_SHIFT)) {
 #ifdef PRINT_USER_FAULTS
                if (fault_space == 0)
                        printk(KERN_DEBUG "User Fault on Kernel Space ");
@@ -721,7 +752,7 @@ void handle_interruption(int code, struct pt_regs *regs)
                si.si_signo = SIGSEGV;
                si.si_errno = 0;
                si.si_code = SEGV_MAPERR;
-               si.si_addr = (void *) regs->ior;
+               si.si_addr = (void __user *) regs->ior;
                force_sig_info(SIGSEGV, &si, current);
                return;
            }