vserver 2.0-rc4
[linux-2.6.git] / arch / sparc64 / kernel / signal32.c
index a9a5b31..7f88532 100644 (file)
@@ -20,9 +20,9 @@
 #include <linux/smp_lock.h>
 #include <linux/binfmts.h>
 #include <linux/compat.h>
+#include <linux/bitops.h>
 
 #include <asm/uaccess.h>
-#include <asm/bitops.h>
 #include <asm/ptrace.h>
 #include <asm/svr4.h>
 #include <asm/pgtable.h>
@@ -59,6 +59,16 @@ struct signal_sframe32 {
        unsigned int extramask[_COMPAT_NSIG_WORDS - 1];
 };
 
+/* This magic should be in g_upper[0] for all upper parts
+ * to be valid.
+ */
+#define SIGINFO_EXTRA_V8PLUS_MAGIC     0x130e269
+typedef struct {
+       unsigned int g_upper[8];
+       unsigned int o_upper[8];
+       unsigned int asi;
+} siginfo_extra_v8plus_t;
+
 /* 
  * And the new one, intended to be used for Linux applications only
  * (we have enough in there to work with clone).
@@ -76,9 +86,61 @@ struct new_signal_frame32 {
        __siginfo_fpu_t         fpu_state;
 };
 
+typedef struct compat_siginfo{
+       int si_signo;
+       int si_errno;
+       int si_code;
+
+       union {
+               int _pad[SI_PAD_SIZE32];
+
+               /* kill() */
+               struct {
+                       compat_pid_t _pid;              /* sender's pid */
+                       unsigned int _uid;              /* sender's uid */
+               } _kill;
+
+               /* POSIX.1b timers */
+               struct {
+                       timer_t _tid;                   /* timer id */
+                       int _overrun;                   /* overrun count */
+                       sigval_t32 _sigval;             /* same as below */
+                       int _sys_private;               /* not to be passed to user */
+               } _timer;
+
+               /* POSIX.1b signals */
+               struct {
+                       compat_pid_t _pid;              /* sender's pid */
+                       unsigned int _uid;              /* sender's uid */
+                       sigval_t32 _sigval;
+               } _rt;
+
+               /* SIGCHLD */
+               struct {
+                       compat_pid_t _pid;              /* which child */
+                       unsigned int _uid;              /* sender's uid */
+                       int _status;                    /* exit code */
+                       compat_clock_t _utime;
+                       compat_clock_t _stime;
+               } _sigchld;
+
+               /* SIGILL, SIGFPE, SIGSEGV, SIGBUS, SIGEMT */
+               struct {
+                       u32 _addr; /* faulting insn/memory ref. */
+                       int _trapno;
+               } _sigfault;
+
+               /* SIGPOLL */
+               struct {
+                       int _band;      /* POLL_IN, POLL_OUT, POLL_MSG */
+                       int _fd;
+               } _sigpoll;
+       } _sifields;
+}compat_siginfo_t;
+
 struct rt_signal_frame32 {
        struct sparc_stackf32   ss;
-       siginfo_t32             info;
+       compat_siginfo_t        info;
        struct pt_regs32        regs;
        compat_sigset_t         mask;
        /* __siginfo_fpu32_t * */ u32 fpu_save;
@@ -95,11 +157,11 @@ struct rt_signal_frame32 {
 #define NF_ALIGNEDSZ  (((sizeof(struct new_signal_frame32) + 7) & (~7)))
 #define RT_ALIGNEDSZ  (((sizeof(struct rt_signal_frame32) + 7) & (~7)))
 
-int copy_siginfo_to_user32(siginfo_t32 __user *to, siginfo_t *from)
+int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
 {
        int err;
 
-       if (!access_ok(VERIFY_WRITE, to, sizeof(siginfo_t32)))
+       if (!access_ok(VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
                return -EFAULT;
 
        /* If you change siginfo_t structure, please be sure
@@ -119,7 +181,7 @@ int copy_siginfo_to_user32(siginfo_t32 __user *to, siginfo_t *from)
                case __SI_TIMER >> 16:
                        err |= __put_user(from->si_tid, &to->si_tid);
                        err |= __put_user(from->si_overrun, &to->si_overrun);
-                       err |= __put_user((u32)(u64)from->si_ptr, &to->si_ptr);
+                       err |= __put_user(from->si_int, &to->si_int);
                        break;
                case __SI_CHLD >> 16:
                        err |= __put_user(from->si_utime, &to->si_utime);
@@ -130,9 +192,12 @@ int copy_siginfo_to_user32(siginfo_t32 __user *to, siginfo_t *from)
                        err |= __put_user(from->si_uid, &to->si_uid);
                        break;
                case __SI_FAULT >> 16:
-               case __SI_POLL >> 16:
                        err |= __put_user(from->si_trapno, &to->si_trapno);
-                       err |= __put_user((long)from->si_addr, &to->si_addr);
+                       err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
+                       break;
+               case __SI_POLL >> 16:
+                       err |= __put_user(from->si_band, &to->si_band);
+                       err |= __put_user(from->si_fd, &to->si_fd);
                        break;
                case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
                case __SI_MESGQ >> 16:
@@ -145,6 +210,22 @@ int copy_siginfo_to_user32(siginfo_t32 __user *to, siginfo_t *from)
        return err;
 }
 
+/* CAUTION: This is just a very minimalist implementation for the
+ *          sake of compat_sys_rt_sigqueueinfo()
+ */
+int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
+{
+       if (!access_ok(VERIFY_WRITE, from, sizeof(compat_siginfo_t)))
+               return -EFAULT;
+
+       if (copy_from_user(to, from, 3*sizeof(int)) ||
+           copy_from_user(to->_sifields._pad, from->_sifields._pad,
+                          SI_PAD_SIZE))
+               return -EFAULT;
+
+       return 0;
+}
+
 /*
  * atomically swap in the new signal mask, and wait for a signal.
  * This is really tricky on the Sparc, watch out...
@@ -197,7 +278,7 @@ asmlinkage void do_rt_sigsuspend32(u32 uset, size_t sigsetsize, struct pt_regs *
                regs->u_regs[UREG_I0] = EINVAL;
                return;
        }
-       if (copy_from_user(&set32, (void __user *)(long)uset, sizeof(set32))) {
+       if (copy_from_user(&set32, compat_ptr(uset), sizeof(set32))) {
                regs->tstate |= TSTATE_ICARRY;
                regs->u_regs[UREG_I0] = EFAULT;
                return;
@@ -299,8 +380,13 @@ void do_new_sigreturn32(struct pt_regs *regs)
        if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
                err |= __get_user(i, &sf->v8plus.g_upper[0]);
                if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
+                       unsigned long asi;
+
                        for (i = UREG_G1; i <= UREG_I7; i++)
                                err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
+                       err |= __get_user(asi, &sf->v8plus.asi);
+                       regs->tstate &= ~TSTATE_ASI;
+                       regs->tstate |= ((asi & 0xffUL) << 24UL);
                }
        }
 
@@ -330,7 +416,7 @@ void do_new_sigreturn32(struct pt_regs *regs)
        return;
 
 segv:
-       do_exit(SIGSEGV);
+       force_sig(SIGSEGV, current);
 }
 
 asmlinkage void do_sigreturn32(struct pt_regs *regs)
@@ -400,7 +486,7 @@ asmlinkage void do_sigreturn32(struct pt_regs *regs)
        return;
 
 segv:
-       do_exit(SIGSEGV);
+       force_sig(SIGSEGV, current);
 }
 
 asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
@@ -447,8 +533,13 @@ asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
        if ((psr & (PSR_VERS|PSR_IMPL)) == PSR_V8PLUS) {
                err |= __get_user(i, &sf->v8plus.g_upper[0]);
                if (i == SIGINFO_EXTRA_V8PLUS_MAGIC) {
+                       unsigned long asi;
+
                        for (i = UREG_G1; i <= UREG_I7; i++)
                                err |= __get_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
+                       err |= __get_user(asi, &sf->v8plus.asi);
+                       regs->tstate &= ~TSTATE_ASI;
+                       regs->tstate |= ((asi & 0xffUL) << 24UL);
                }
        }
 
@@ -487,7 +578,7 @@ asmlinkage void do_rt_sigreturn32(struct pt_regs *regs)
        spin_unlock_irq(&current->sighand->siglock);
        return;
 segv:
-       do_exit(SIGSEGV);
+       force_sig(SIGSEGV, current);
 }
 
 /* Checks if the fp is valid */
@@ -632,9 +723,9 @@ setup_frame32(struct sigaction *sa, struct pt_regs *regs, int signr, sigset_t *o
                        sig_address = NULL;
                }
        }
-       err |= __put_user((long)sig_address, &sframep->sig_address);
+       err |= __put_user(ptr_to_compat(sig_address), &sframep->sig_address);
        err |= __put_user(sig_code, &sframep->sig_code);
-       err |= __put_user((u64)sc, &sframep->sig_scptr);
+       err |= __put_user(ptr_to_compat(sc), &sframep->sig_scptr);
        if (err)
                goto sigsegv;
 
@@ -648,7 +739,7 @@ setup_frame32(struct sigaction *sa, struct pt_regs *regs, int signr, sigset_t *o
        return;
 
 sigsegv:
-       do_exit(SIGSEGV);
+       force_sigsegv(signr, current);
 }
 
 
@@ -715,7 +806,10 @@ static void new_setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
        err |= __put_user(sizeof(siginfo_extra_v8plus_t), &sf->extra_size);
        err |= __put_user(SIGINFO_EXTRA_V8PLUS_MAGIC, &sf->v8plus.g_upper[0]);
        for (i = 1; i < 16; i++)
-               err |= __put_user(((u32 *)regs->u_regs)[2*i], &sf->v8plus.g_upper[i]);
+               err |= __put_user(((u32 *)regs->u_regs)[2*i],
+                                 &sf->v8plus.g_upper[i]);
+       err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
+                         &sf->v8plus.asi);
 
        if (psr & PSR_EF) {
                err |= save_fpu_state32(regs, &sf->fpu_state);
@@ -749,6 +843,7 @@ static void new_setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
        regs->u_regs[UREG_FP] = (unsigned long) sf;
        regs->u_regs[UREG_I0] = signo;
        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
+       regs->u_regs[UREG_I2] = (unsigned long) &sf->info;
 
        /* 4. signal handler */
        regs->tpc = (unsigned long) ka->sa.sa_handler;
@@ -765,7 +860,8 @@ static void new_setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
                /* Flush instruction space. */
                unsigned long address = ((unsigned long)&(sf->insns[0]));
                pgd_t *pgdp = pgd_offset(current->mm, address);
-               pmd_t *pmdp = pmd_offset(pgdp, address);
+               pud_t *pudp = pud_offset(pgdp, address);
+               pmd_t *pmdp = pmd_offset(pudp, address);
                pte_t *ptep;
 
                regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
@@ -795,7 +891,7 @@ static void new_setup_frame32(struct k_sigaction *ka, struct pt_regs *regs,
 sigill:
        do_exit(SIGILL);
 sigsegv:
-       do_exit(SIGSEGV);
+       force_sigsegv(signo, current);
 }
 
 /* Setup a Solaris stack frame */
@@ -875,7 +971,7 @@ setup_svr4_frame32(struct sigaction *sa, unsigned long pc, unsigned long npc,
        /* Save the currently window file: */
 
        /* 1. Link sfp->uc->gwins to our windows */
-       err |= __put_user((u32)(long)gw, &mc->gwin);
+       err |= __put_user(ptr_to_compat(gw), &mc->gwin);
            
        /* 2. Number of windows to restore at setcontext (): */
        err |= __put_user(get_thread_wsaved(), &gw->count);
@@ -919,7 +1015,7 @@ setup_svr4_frame32(struct sigaction *sa, unsigned long pc, unsigned long npc,
        return;
 
 sigsegv:
-       do_exit(SIGSEGV);
+       force_sigsegv(signr, current);
 }
 
 asmlinkage int
@@ -1070,7 +1166,7 @@ asmlinkage int svr4_setcontext(svr4_ucontext_t __user *c, struct pt_regs *regs)
 
        return -EINTR;
 sigsegv:
-       do_exit(SIGSEGV);
+       return -EFAULT;
 }
 
 static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
@@ -1119,6 +1215,8 @@ static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
        for (i = 1; i < 16; i++)
                err |= __put_user(((u32 *)regs->u_regs)[2*i],
                                  &sf->v8plus.g_upper[i]);
+       err |= __put_user((regs->tstate & TSTATE_ASI) >> 24UL,
+                         &sf->v8plus.asi);
 
        if (psr & PSR_EF) {
                err |= save_fpu_state32(regs, &sf->fpu_state);
@@ -1157,6 +1255,7 @@ static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
        regs->u_regs[UREG_FP] = (unsigned long) sf;
        regs->u_regs[UREG_I0] = signr;
        regs->u_regs[UREG_I1] = (unsigned long) &sf->info;
+       regs->u_regs[UREG_I2] = (unsigned long) &sf->regs;
 
        /* 4. signal handler */
        regs->tpc = (unsigned long) ka->sa.sa_handler;
@@ -1173,7 +1272,8 @@ static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
                /* Flush instruction space. */
                unsigned long address = ((unsigned long)&(sf->insns[0]));
                pgd_t *pgdp = pgd_offset(current->mm, address);
-               pmd_t *pmdp = pmd_offset(pgdp, address);
+               pud_t *pudp = pud_offset(pgdp, address);
+               pmd_t *pmdp = pmd_offset(pudp, address);
                pte_t *ptep;
 
                regs->u_regs[UREG_I7] = (unsigned long) (&(sf->insns[0]) - 2);
@@ -1206,7 +1306,7 @@ static void setup_rt_frame32(struct k_sigaction *ka, struct pt_regs *regs,
 sigill:
        do_exit(SIGILL);
 sigsegv:
-       do_exit(SIGSEGV);
+       force_sigsegv(signr, current);
 }
 
 static inline void handle_signal32(unsigned long signr, struct k_sigaction *ka,
@@ -1225,8 +1325,6 @@ static inline void handle_signal32(unsigned long signr, struct k_sigaction *ka,
                else
                        setup_frame32(&ka->sa, regs, signr, oldset, info);
        }
-       if (ka->sa.sa_flags & SA_ONESHOT)
-               ka->sa.sa_handler = SIG_DFL;
        if (!(ka->sa.sa_flags & SA_NOMASK)) {
                spin_lock_irq(&current->sighand->siglock);
                sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
@@ -1266,21 +1364,19 @@ int do_signal32(sigset_t *oldset, struct pt_regs * regs,
 {
        siginfo_t info;
        struct signal_deliver_cookie cookie;
+       struct k_sigaction ka;
        int signr;
        int svr4_signal = current->personality == PER_SVR4;
        
        cookie.restart_syscall = restart_syscall;
        cookie.orig_i0 = orig_i0;
 
-       signr = get_signal_to_deliver(&info, regs, &cookie);
+       signr = get_signal_to_deliver(&info, &ka, regs, &cookie);
        if (signr > 0) {
-               struct k_sigaction *ka;
-
-               ka = &current->sighand->action[signr-1];
-
                if (cookie.restart_syscall)
-                       syscall_restart32(orig_i0, regs, &ka->sa);
-               handle_signal32(signr, ka, &info, oldset, regs, svr4_signal);
+                       syscall_restart32(orig_i0, regs, &ka.sa);
+               handle_signal32(signr, &ka, &info, oldset,
+                               regs, svr4_signal);
                return 1;
        }
        if (cookie.restart_syscall &&
@@ -1355,10 +1451,12 @@ asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
        u32 u_ss_sp = 0;
        int ret;
        mm_segment_t old_fs;
+       stack_t32 __user *uss32 = compat_ptr(ussa);
+       stack_t32 __user *uoss32 = compat_ptr(uossa);
        
-       if (ussa && (get_user(u_ss_sp, &((stack_t32 __user *)(long)ussa)->ss_sp) ||
-                   __get_user(uss.ss_flags, &((stack_t32 __user *)(long)ussa)->ss_flags) ||
-                   __get_user(uss.ss_size, &((stack_t32 __user *)(long)ussa)->ss_size)))
+       if (ussa && (get_user(u_ss_sp, &uss32->ss_sp) ||
+                   __get_user(uss.ss_flags, &uss32->ss_flags) ||
+                   __get_user(uss.ss_size, &uss32->ss_size)))
                return -EFAULT;
        uss.ss_sp = compat_ptr(u_ss_sp);
        old_fs = get_fs();
@@ -1366,9 +1464,9 @@ asmlinkage long do_sys32_sigaltstack(u32 ussa, u32 uossa, unsigned long sp)
        ret = do_sigaltstack(ussa ? (stack_t __user *) &uss : NULL,
                             uossa ? (stack_t __user *) &uoss : NULL, sp);
        set_fs(old_fs);
-       if (!ret && uossa && (put_user((long)uoss.ss_sp, &((stack_t32 __user *)(long)uossa)->ss_sp) ||
-                   __put_user(uoss.ss_flags, &((stack_t32 __user *)(long)uossa)->ss_flags) ||
-                   __put_user(uoss.ss_size, &((stack_t32 __user *)(long)uossa)->ss_size)))
+       if (!ret && uossa && (put_user(ptr_to_compat(uoss.ss_sp), &uoss32->ss_sp) ||
+                   __put_user(uoss.ss_flags, &uoss32->ss_flags) ||
+                   __put_user(uoss.ss_size, &uoss32->ss_size)))
                return -EFAULT;
        return ret;
 }