Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / parisc / kernel / signal.c
index 1aed89d..05767e8 100644 (file)
@@ -32,6 +32,7 @@
 #include <asm/uaccess.h>
 #include <asm/pgalloc.h>
 #include <asm/cacheflush.h>
+#include <asm/asm-offsets.h>
 
 #ifdef CONFIG_COMPAT
 #include <linux/compat.h>
@@ -69,7 +70,7 @@ int do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall);
 #endif
 
 asmlinkage int
-sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
+sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize, struct pt_regs *regs)
 {
        sigset_t saveset, newset;
 #ifdef __LP64__
@@ -79,7 +80,7 @@ sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
                /* XXX: Don't preclude handling different sized sigset_t's.  */
                if (sigsetsize != sizeof(compat_sigset_t))
                        return -EINVAL;
-               if (copy_from_user(&newset32, (compat_sigset_t *)unewset, sizeof(newset32)))
+               if (copy_from_user(&newset32, (compat_sigset_t __user *)unewset, sizeof(newset32)))
                        return -EFAULT;
                sigset_32to64(&newset,&newset32);
                
@@ -125,7 +126,7 @@ sys_rt_sigsuspend(sigset_t *unewset, size_t sigsetsize, struct pt_regs *regs)
 #define INSN_DIE_HORRIBLY 0x68000ccc /* stw %r0,0x666(%sr0,%r0) */
 
 static long
-restore_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
+restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
 {
        long err = 0;
 
@@ -143,14 +144,14 @@ restore_sigcontext(struct sigcontext *sc, struct pt_regs *regs)
 void
 sys_rt_sigreturn(struct pt_regs *regs, int in_syscall)
 {
-       struct rt_sigframe *frame;
+       struct rt_sigframe __user *frame;
        struct siginfo si;
        sigset_t set;
        unsigned long usp = (regs->gr[30] & ~(0x01UL));
        unsigned long sigframe_size = PARISC_RT_SIGFRAME_SIZE;
 #ifdef __LP64__
        compat_sigset_t compat_set;
-       struct compat_rt_sigframe * compat_frame;
+       struct compat_rt_sigframe __user * compat_frame;
        
        if(personality(current->personality) == PER_LINUX32)
                sigframe_size = PARISC_RT_SIGFRAME_SIZE32;
@@ -158,12 +159,12 @@ sys_rt_sigreturn(struct pt_regs *regs, int in_syscall)
 
 
        /* Unwind the user stack to get the rt_sigframe structure. */
-       frame = (struct rt_sigframe *)
+       frame = (struct rt_sigframe __user *)
                (usp - sigframe_size);
        DBG(2,"sys_rt_sigreturn: frame is %p\n", frame);
 
 #ifdef __LP64__
-       compat_frame = (struct compat_rt_sigframe *)frame;
+       compat_frame = (struct compat_rt_sigframe __user *)frame;
        
        if(personality(current->personality) == PER_LINUX32){
                DBG(2,"sys_rt_sigreturn: ELF32 process.\n");
@@ -238,7 +239,7 @@ give_sigsegv:
  * Set up a signal frame.
  */
 
-static inline void *
+static inline void __user *
 get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
 {
        /*FIXME: ELF32 vs. ELF64 has different frame_size, but since we
@@ -251,11 +252,11 @@ get_sigframe(struct k_sigaction *ka, unsigned long sp, size_t frame_size)
                sp = current->sas_ss_sp; /* Stacks grow up! */
 
        DBG(1,"get_sigframe: Returning sp = %#lx\n", (unsigned long)sp);
-       return (void *) sp; /* Stacks grow up.  Fun. */
+       return (void __user *) sp; /* Stacks grow up.  Fun. */
 }
 
 static long
-setup_sigcontext(struct sigcontext *sc, struct pt_regs *regs, int in_syscall)
+setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs, int in_syscall)
                 
 {
        unsigned long flags = 0;
@@ -292,14 +293,13 @@ static long
 setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
               sigset_t *set, struct pt_regs *regs, int in_syscall)
 {
-       struct rt_sigframe *frame;
+       struct rt_sigframe __user *frame;
        unsigned long rp, usp;
        unsigned long haddr, sigframe_size;
-       struct siginfo si;
        int err = 0;
 #ifdef __LP64__
        compat_int_t compat_val;
-       struct compat_rt_sigframe * compat_frame;
+       struct compat_rt_sigframe __user * compat_frame;
        compat_sigset_t compat_set;
 #endif
        
@@ -313,11 +313,11 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
        
 #ifdef __LP64__
 
-       compat_frame = (struct compat_rt_sigframe *)frame;
+       compat_frame = (struct compat_rt_sigframe __user *)frame;
        
        if(personality(current->personality) == PER_LINUX32) {
                DBG(1,"setup_rt_frame: frame->info = 0x%p\n", &compat_frame->info);
-               err |= compat_copy_siginfo_to_user(&compat_frame->info, info);
+               err |= copy_siginfo_to_user32(&compat_frame->info, info);
                DBG(1,"SETUP_RT_FRAME: 1\n");
                compat_val = (compat_int_t)current->sas_ss_sp;
                err |= __put_user(compat_val, &compat_frame->uc.uc_stack.ss_sp);
@@ -353,12 +353,17 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
                goto give_sigsegv;
 
        /* Set up to return from userspace.  If provided, use a stub
-          already in userspace.  */
+          already in userspace. The first words of tramp are used to
+          save the previous sigrestartblock trampoline that might be
+          on the stack. We start the sigreturn trampoline at 
+          SIGRESTARTBLOCK_TRAMP+X. */
        err |= __put_user(in_syscall ? INSN_LDI_R25_1 : INSN_LDI_R25_0,
-                       &frame->tramp[SIGRETURN_TRAMP+0]);
-       err |= __put_user(INSN_LDI_R20, &frame->tramp[SIGRETURN_TRAMP+1]);
-       err |= __put_user(INSN_BLE_SR2_R0, &frame->tramp[SIGRETURN_TRAMP+2]);
-       err |= __put_user(INSN_NOP, &frame->tramp[SIGRETURN_TRAMP+3]);
+                       &frame->tramp[SIGRESTARTBLOCK_TRAMP+0]);
+       err |= __put_user(INSN_LDI_R20, 
+                       &frame->tramp[SIGRESTARTBLOCK_TRAMP+1]);
+       err |= __put_user(INSN_BLE_SR2_R0, 
+                       &frame->tramp[SIGRESTARTBLOCK_TRAMP+2]);
+       err |= __put_user(INSN_NOP, &frame->tramp[SIGRESTARTBLOCK_TRAMP+3]);
 
 #if DEBUG_SIG
        /* Assert that we're flushing in the correct space... */
@@ -370,12 +375,16 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
        }
 #endif
 
-       flush_user_dcache_range((unsigned long) &frame->tramp[SIGRETURN_TRAMP],
+       flush_user_dcache_range((unsigned long) &frame->tramp[0],
                           (unsigned long) &frame->tramp[TRAMP_SIZE]);
-       flush_user_icache_range((unsigned long) &frame->tramp[SIGRETURN_TRAMP],
+       flush_user_icache_range((unsigned long) &frame->tramp[0],
                           (unsigned long) &frame->tramp[TRAMP_SIZE]);
 
-       rp = (unsigned long) &frame->tramp[SIGRETURN_TRAMP];
+       /* TRAMP Words 0-4, Lenght 5 = SIGRESTARTBLOCK_TRAMP
+        * TRAMP Words 5-9, Length 4 = SIGRETURN_TRAMP
+        * So the SIGRETURN_TRAMP is at the end of SIGRESTARTBLOCK_TRAMP
+        */
+       rp = (unsigned long) &frame->tramp[SIGRESTARTBLOCK_TRAMP];
 
        if (err)
                goto give_sigsegv;
@@ -387,7 +396,7 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 #endif
                if (haddr & PA_PLABEL_FDESC) {
                        Elf32_Fdesc fdesc;
-                       Elf32_Fdesc *ufdesc = (Elf32_Fdesc *)A(haddr & ~3);
+                       Elf32_Fdesc __user *ufdesc = (Elf32_Fdesc __user *)A(haddr & ~3);
 
                        err = __copy_from_user(&fdesc, ufdesc, sizeof(fdesc));
 
@@ -400,7 +409,7 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 #ifdef __LP64__
        } else {
                Elf64_Fdesc fdesc;
-               Elf64_Fdesc *ufdesc = (Elf64_Fdesc *)A(haddr & ~3);
+               Elf64_Fdesc __user *ufdesc = (Elf64_Fdesc __user *)A(haddr & ~3);
                
                err = __copy_from_user(&fdesc, ufdesc, sizeof(fdesc));
                
@@ -434,6 +443,18 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
                        psw |= PSW_W;
 #endif
 
+               /* If we are singlestepping, arrange a trap to be delivered
+                  when we return to userspace. Note the semantics -- we
+                  should trap before the first insn in the handler is
+                  executed. Ref:
+                       http://sources.redhat.com/ml/gdb/2004-11/msg00245.html
+                */
+               if (pa_psw(current)->r) {
+                       pa_psw(current)->r = 0;
+                       psw |= PSW_R;
+                       mtctl(-1, 0);
+               }
+
                regs->gr[0] = psw;
                regs->iaoq[0] = haddr | 3;
                regs->iaoq[1] = regs->iaoq[0] + 4;
@@ -468,15 +489,7 @@ setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
 
 give_sigsegv:
        DBG(1,"setup_rt_frame: sending SIGSEGV\n");
-       if (sig == SIGSEGV)
-               ka->sa.sa_handler = SIG_DFL;
-       si.si_signo = SIGSEGV;
-       si.si_errno = 0;
-       si.si_code = SI_KERNEL;
-       si.si_pid = current->pid;
-       si.si_uid = current->uid;
-       si.si_addr = frame;
-       force_sig_info(SIGSEGV, &si, current);
+       force_sigsegv(sig, current);
        return 0;
 }
 
@@ -485,11 +498,9 @@ give_sigsegv:
  */    
 
 static long
-handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
-             struct pt_regs *regs, int in_syscall)
+handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka,
+               sigset_t *oldset, struct pt_regs *regs, int in_syscall)
 {
-       struct k_sigaction *ka = &current->sighand->action[sig-1];
-
        DBG(1,"handle_signal: sig=%ld, ka=%p, info=%p, oldset=%p, regs=%p\n",
               sig, ka, info, oldset, regs);
        
@@ -497,16 +508,12 @@ handle_signal(unsigned long sig, siginfo_t *info, sigset_t *oldset,
        if (!setup_rt_frame(sig, ka, info, oldset, regs, in_syscall))
                return 0;
 
-       if (ka->sa.sa_flags & SA_ONESHOT)
-               ka->sa.sa_handler = SIG_DFL;
-
-       if (!(ka->sa.sa_flags & SA_NODEFER)) {
-               spin_lock_irq(&current->sighand->siglock);
-               sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
+       spin_lock_irq(&current->sighand->siglock);
+       sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
+       if (!(ka->sa.sa_flags & SA_NODEFER))
                sigaddset(&current->blocked,sig);
-               recalc_sigpending();
-               spin_unlock_irq(&current->sighand->siglock);
-       }
+       recalc_sigpending();
+       spin_unlock_irq(&current->sighand->siglock);
        return 1;
 }
 
@@ -526,7 +533,7 @@ asmlinkage int
 do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
 {
        siginfo_t info;
-       struct k_sigaction *ka;
+       struct k_sigaction ka;
        int signr;
 
        DBG(1,"\ndo_signal: oldset=0x%p, regs=0x%p, sr7 %#lx, in_syscall=%d\n",
@@ -544,10 +551,15 @@ do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
                oldset->sig[0], oldset->sig[1]);
 
 
-       signr = get_signal_to_deliver(&info, regs, NULL);
-       DBG(3,"do_signal: signr = %d, regs->gr[28] = %ld\n", signr, regs->gr[28]); 
+       /* May need to force signal if handle_signal failed to deliver */
+       while (1) {
+         
+               signr = get_signal_to_deliver(&info, &ka, regs, NULL);
+               DBG(3,"do_signal: signr = %d, regs->gr[28] = %ld\n", signr, regs->gr[28]); 
        
-       if (signr > 0) {
+               if (signr <= 0)
+                 break;
+               
                /* Restart a system call if necessary. */
                if (in_syscall) {
                        /* Check the return code */
@@ -560,8 +572,7 @@ do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
                                break;
 
                        case -ERESTARTSYS:
-                               ka = &current->sighand->action[signr-1];
-                               if (!(ka->sa.sa_flags & SA_RESTART)) {
+                               if (!(ka.sa.sa_flags & SA_RESTART)) {
                                        DBG(1,"ERESTARTSYS: putting -EINTR\n");
                                        regs->gr[28] = -EINTR;
                                        break;
@@ -569,8 +580,7 @@ do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
                        /* fallthrough */
                        case -ERESTARTNOINTR:
                                /* A syscall is just a branch, so all
-                                   we have to do is fiddle the return
-                                   pointer. */
+                                  we have to do is fiddle the return pointer. */
                                regs->gr[31] -= 8; /* delayed branching */
                                /* Preserve original r28. */
                                regs->gr[28] = regs->orig_r28;
@@ -580,12 +590,13 @@ do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
                /* Whee!  Actually deliver the signal.  If the
                   delivery failed, we need to continue to iterate in
                   this loop so we can deliver the SIGSEGV... */
-               if (handle_signal(signr, &info, oldset, regs, in_syscall)) {
+               if (handle_signal(signr, &info, &ka, oldset, regs, in_syscall)) {
                        DBG(1,KERN_DEBUG "do_signal: Exit (success), regs->gr[28] = %ld\n",
                                regs->gr[28]);
                        return 1;
                }
        }
+       /* end of while(1) looping forever if we can't force a signal */
 
        /* Did we come from a system call? */
        if (in_syscall) {
@@ -613,10 +624,14 @@ do_signal(sigset_t *oldset, struct pt_regs *regs, int in_syscall)
                        put_user(0xe0008200, &usp[3]);
                        put_user(0x34140000, &usp[4]);
 
-                       /* Stack is 64-byte aligned, and we only 
-                        * need to flush 1 cache line */
-                       asm("fdc 0(%%sr3, %0)\n"
-                           "fic 0(%%sr3, %0)\n"
+                       /* Stack is 64-byte aligned, and we only need
+                        * to flush 1 cache line.
+                        * Flushing one cacheline is cheap.
+                        * "sync" on bigger (> 4 way) boxes is not.
+                        */
+                       asm("fdc %%r0(%%sr3, %0)\n"
+                           "sync\n"
+                           "fic %%r0(%%sr3, %0)\n"
                            "sync\n"
                            : : "r"(regs->gr[30]));