]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - arch/powerpc/kernel/signal_32.c
kprobes: improve kretprobe scalability with hashed locking
[karo-tx-linux.git] / arch / powerpc / kernel / signal_32.c
index f7fa395b9fb5da1d18446895d6bb111f7a17de73..3e80aa32b8b09e1daf432cb7e112d9a04be7ce4a 100644 (file)
 #define mcontext       mcontext32
 #define ucontext       ucontext32
 
+/*
+ * Userspace code may pass a ucontext which doesn't include VSX added
+ * at the end.  We need to check for this case.
+ */
+#define UCONTEXTSIZEWITHOUTVSX \
+               (sizeof(struct ucontext) - sizeof(elf_vsrreghalf_t32))
+
 /*
  * Returning 0 means we return to userspace via
  * ret_from_except and thus restore all user
@@ -328,6 +335,75 @@ struct rt_sigframe {
        int                     abigap[56];
 };
 
+#ifdef CONFIG_VSX
+unsigned long copy_fpr_to_user(void __user *to,
+                              struct task_struct *task)
+{
+       double buf[ELF_NFPREG];
+       int i;
+
+       /* save FPR copy to local buffer then write to the thread_struct */
+       for (i = 0; i < (ELF_NFPREG - 1) ; i++)
+               buf[i] = task->thread.TS_FPR(i);
+       memcpy(&buf[i], &task->thread.fpscr, sizeof(double));
+       return __copy_to_user(to, buf, ELF_NFPREG * sizeof(double));
+}
+
+unsigned long copy_fpr_from_user(struct task_struct *task,
+                                void __user *from)
+{
+       double buf[ELF_NFPREG];
+       int i;
+
+       if (__copy_from_user(buf, from, ELF_NFPREG * sizeof(double)))
+               return 1;
+       for (i = 0; i < (ELF_NFPREG - 1) ; i++)
+               task->thread.TS_FPR(i) = buf[i];
+       memcpy(&task->thread.fpscr, &buf[i], sizeof(double));
+
+       return 0;
+}
+
+unsigned long copy_vsx_to_user(void __user *to,
+                              struct task_struct *task)
+{
+       double buf[ELF_NVSRHALFREG];
+       int i;
+
+       /* save FPR copy to local buffer then write to the thread_struct */
+       for (i = 0; i < ELF_NVSRHALFREG; i++)
+               buf[i] = task->thread.fpr[i][TS_VSRLOWOFFSET];
+       return __copy_to_user(to, buf, ELF_NVSRHALFREG * sizeof(double));
+}
+
+unsigned long copy_vsx_from_user(struct task_struct *task,
+                                void __user *from)
+{
+       double buf[ELF_NVSRHALFREG];
+       int i;
+
+       if (__copy_from_user(buf, from, ELF_NVSRHALFREG * sizeof(double)))
+               return 1;
+       for (i = 0; i < ELF_NVSRHALFREG ; i++)
+               task->thread.fpr[i][TS_VSRLOWOFFSET] = buf[i];
+       return 0;
+}
+#else
+inline unsigned long copy_fpr_to_user(void __user *to,
+                                     struct task_struct *task)
+{
+       return __copy_to_user(to, task->thread.fpr,
+                             ELF_NFPREG * sizeof(double));
+}
+
+inline unsigned long copy_fpr_from_user(struct task_struct *task,
+                                       void __user *from)
+{
+       return __copy_from_user(task->thread.fpr, from,
+                             ELF_NFPREG * sizeof(double));
+}
+#endif
+
 /*
  * Save the current user registers on the user stack.
  * We only save the altivec/spe registers if the process has used
@@ -337,10 +413,6 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
                int sigret)
 {
        unsigned long msr = regs->msr;
-#ifdef CONFIG_VSX
-       double buf[32];
-       int i;
-#endif
 
        /* Make sure floating point registers are stored in regs */
        flush_fp_to_thread(current);
@@ -370,19 +442,21 @@ static int save_user_regs(struct pt_regs *regs, struct mcontext __user *frame,
        if (__put_user(current->thread.vrsave, (u32 __user *)&frame->mc_vregs[32]))
                return 1;
 #endif /* CONFIG_ALTIVEC */
-#ifdef CONFIG_VSX
-       /* save FPR copy to local buffer then write to the thread_struct */
-       flush_fp_to_thread(current);
-       for (i = 0; i < 32 ; i++)
-               buf[i] = current->thread.TS_FPR(i);
-       memcpy(&buf[i], &current->thread.fpscr, sizeof(double));
-       if (__copy_to_user(&frame->mc_fregs, buf, ELF_NFPREG * sizeof(double)))
-               return 1;
-#else
-       /* save floating-point registers */
-       if (__copy_to_user(&frame->mc_fregs, current->thread.fpr,
-                   ELF_NFPREG * sizeof(double)))
+       if (copy_fpr_to_user(&frame->mc_fregs, current))
                return 1;
+#ifdef CONFIG_VSX
+       /*
+        * Copy VSR 0-31 upper half from thread_struct to local
+        * buffer, then write that to userspace.  Also set MSR_VSX in
+        * the saved MSR value to indicate that frame->mc_vregs
+        * contains valid data
+        */
+       if (current->thread.used_vsr) {
+               __giveup_vsx(current);
+               if (copy_vsx_to_user(&frame->mc_vsregs, current))
+                       return 1;
+               msr |= MSR_VSX;
+       }
 #endif /* CONFIG_VSX */
 #ifdef CONFIG_SPE
        /* save spe registers */
@@ -427,7 +501,6 @@ static long restore_user_regs(struct pt_regs *regs,
        unsigned int save_r2 = 0;
        unsigned long msr;
 #ifdef CONFIG_VSX
-       double buf[32];
        int i;
 #endif
 
@@ -475,17 +548,25 @@ static long restore_user_regs(struct pt_regs *regs,
        if (__get_user(current->thread.vrsave, (u32 __user *)&sr->mc_vregs[32]))
                return 1;
 #endif /* CONFIG_ALTIVEC */
+       if (copy_fpr_from_user(current, &sr->mc_fregs))
+               return 1;
 
 #ifdef CONFIG_VSX
-       if (__copy_from_user(buf, &sr->mc_fregs,sizeof(sr->mc_fregs)))
-               return 1;
-       for (i = 0; i < 32 ; i++)
-               current->thread.TS_FPR(i) = buf[i];
-       memcpy(&current->thread.fpscr, &buf[i], sizeof(double));
-#else
-       if (__copy_from_user(current->thread.fpr, &sr->mc_fregs,
-                            sizeof(sr->mc_fregs)))
-               return 1;
+       /*
+        * Force the process to reload the VSX registers from
+        * current->thread when it next does VSX instruction.
+        */
+       regs->msr &= ~MSR_VSX;
+       if (msr & MSR_VSX) {
+               /*
+                * Restore altivec registers from the stack to a local
+                * buffer, then write this out to the thread_struct
+                */
+               if (copy_vsx_from_user(current, &sr->mc_vsregs))
+                       return 1;
+       } else if (current->thread.used_vsr)
+               for (i = 0; i < 32 ; i++)
+                       current->thread.fpr[i][TS_VSRLOWOFFSET] = 0;
 #endif /* CONFIG_VSX */
        /*
         * force the process to reload the FP registers from
@@ -856,12 +937,42 @@ long sys_swapcontext(struct ucontext __user *old_ctx,
 {
        unsigned char tmp;
 
+#ifdef CONFIG_PPC64
+       unsigned long new_msr = 0;
+
+       if (new_ctx &&
+           __get_user(new_msr, &new_ctx->uc_mcontext.mc_gregs[PT_MSR]))
+               return -EFAULT;
+       /*
+        * Check that the context is not smaller than the original
+        * size (with VMX but without VSX)
+        */
+       if (ctx_size < UCONTEXTSIZEWITHOUTVSX)
+               return -EINVAL;
+       /*
+        * If the new context state sets the MSR VSX bits but
+        * it doesn't provide VSX state.
+        */
+       if ((ctx_size < sizeof(struct ucontext)) &&
+           (new_msr & MSR_VSX))
+               return -EINVAL;
+#ifdef CONFIG_VSX
+       /*
+        * If userspace doesn't provide enough room for VSX data,
+        * but current thread has used VSX, we don't have anywhere
+        * to store the full context back into.
+        */
+       if ((ctx_size < sizeof(struct ucontext)) &&
+           (current->thread.used_vsr && old_ctx))
+               return -EINVAL;
+#endif
+#else
        /* Context size is for future use. Right now, we only make sure
         * we are passed something we understand
         */
        if (ctx_size < sizeof(struct ucontext))
                return -EINVAL;
-
+#endif
        if (old_ctx != NULL) {
                struct mcontext __user *mctx;