[POWERPC] ptrace accessors for special regs MSR and TRAP
[deliverable/linux.git] / arch / powerpc / kernel / ptrace.c
index 8b056d2295cc07ed4f4403e4683fe4cc17734f8b..4edc1186f6ac9ddb5ed8ca414d7c6539157bef29 100644 (file)
@@ -21,6 +21,8 @@
 #include <linux/smp.h>
 #include <linux/errno.h>
 #include <linux/ptrace.h>
+#include <linux/regset.h>
+#include <linux/elf.h>
 #include <linux/user.h>
 #include <linux/security.h>
 #include <linux/signal.h>
 #define PT_MAX_PUT_REG PT_CCR
 #endif
 
+static unsigned long get_user_msr(struct task_struct *task)
+{
+       return task->thread.regs->msr | task->thread.fpexc_mode;
+}
+
+static int set_user_msr(struct task_struct *task, unsigned long msr)
+{
+       task->thread.regs->msr &= ~MSR_DEBUGCHANGE;
+       task->thread.regs->msr |= msr & MSR_DEBUGCHANGE;
+       return 0;
+}
+
+/*
+ * We prevent mucking around with the reserved area of trap
+ * which are used internally by the kernel.
+ */
+static int set_user_trap(struct task_struct *task, unsigned long trap)
+{
+       task->thread.regs->trap = trap & 0xfff0;
+       return 0;
+}
+
 /*
  * Get contents of register REGNO in task TASK.
  */
 unsigned long ptrace_get_reg(struct task_struct *task, int regno)
 {
-       unsigned long tmp = 0;
-
        if (task->thread.regs == NULL)
                return -EIO;
 
-       if (regno == PT_MSR) {
-               tmp = ((unsigned long *)task->thread.regs)[PT_MSR];
-               return tmp | task->thread.fpexc_mode;
-       }
+       if (regno == PT_MSR)
+               return get_user_msr(task);
 
        if (regno < (sizeof(struct pt_regs) / sizeof(unsigned long)))
                return ((unsigned long *)task->thread.regs)[regno];
@@ -87,15 +107,12 @@ int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
        if (task->thread.regs == NULL)
                return -EIO;
 
-       if (regno <= PT_MAX_PUT_REG || regno == PT_TRAP) {
-               if (regno == PT_MSR)
-                       data = (data & MSR_DEBUGCHANGE)
-                               | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
-               /* We prevent mucking around with the reserved area of trap
-                * which are used internally by the kernel
-                */
-               if (regno == PT_TRAP)
-                       data &= 0xfff0;
+       if (regno == PT_MSR)
+               return set_user_msr(task, data);
+       if (regno == PT_TRAP)
+               return set_user_trap(task, data);
+
+       if (regno <= PT_MAX_PUT_REG) {
                ((unsigned long *)task->thread.regs)[regno] = data;
                return 0;
        }
@@ -103,24 +120,48 @@ int ptrace_put_reg(struct task_struct *task, int regno, unsigned long data)
 }
 
 
+static int fpr_get(struct task_struct *target, const struct user_regset *regset,
+                  unsigned int pos, unsigned int count,
+                  void *kbuf, void __user *ubuf)
+{
+       flush_fp_to_thread(target);
+
+       BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
+                    offsetof(struct thread_struct, fpr[32]));
+
+       return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                  &target->thread.fpr, 0, -1);
+}
+
+static int fpr_set(struct task_struct *target, const struct user_regset *regset,
+                  unsigned int pos, unsigned int count,
+                  const void *kbuf, const void __user *ubuf)
+{
+       flush_fp_to_thread(target);
+
+       BUILD_BUG_ON(offsetof(struct thread_struct, fpscr) !=
+                    offsetof(struct thread_struct, fpr[32]));
+
+       return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                 &target->thread.fpr, 0, -1);
+}
+
 static int get_fpregs(void __user *data, struct task_struct *task,
                      int has_fpscr)
 {
        unsigned int count = has_fpscr ? 33 : 32;
-
-       if (copy_to_user(data, task->thread.fpr, count * sizeof(double)))
+       if (!access_ok(VERIFY_WRITE, data, count * sizeof(double)))
                return -EFAULT;
-       return 0;
+       return fpr_get(task, NULL, 0, count * sizeof(double), NULL, data);
 }
 
 static int set_fpregs(void __user *data, struct task_struct *task,
                      int has_fpscr)
 {
        unsigned int count = has_fpscr ? 33 : 32;
-
-       if (copy_from_user(task->thread.fpr, data, count * sizeof(double)))
+       if (!access_ok(VERIFY_READ, data, count * sizeof(double)))
                return -EFAULT;
-       return 0;
+       return fpr_set(task, NULL, 0, count * sizeof(double), NULL, data);
 }
 
 
@@ -138,30 +179,87 @@ static int set_fpregs(void __user *data, struct task_struct *task,
  * (combined (32- and 64-bit) gdb.
  */
 
+static int vr_active(struct task_struct *target,
+                    const struct user_regset *regset)
+{
+       flush_altivec_to_thread(target);
+       return target->thread.used_vr ? regset->n : 0;
+}
+
+static int vr_get(struct task_struct *target, const struct user_regset *regset,
+                 unsigned int pos, unsigned int count,
+                 void *kbuf, void __user *ubuf)
+{
+       int ret;
+
+       flush_altivec_to_thread(target);
+
+       BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
+                    offsetof(struct thread_struct, vr[32]));
+
+       ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                 &target->thread.vr, 0,
+                                 33 * sizeof(vector128));
+       if (!ret) {
+               /*
+                * Copy out only the low-order word of vrsave.
+                */
+               union {
+                       elf_vrreg_t reg;
+                       u32 word;
+               } vrsave;
+               memset(&vrsave, 0, sizeof(vrsave));
+               vrsave.word = target->thread.vrsave;
+               ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &vrsave,
+                                         33 * sizeof(vector128), -1);
+       }
+
+       return ret;
+}
+
+static int vr_set(struct task_struct *target, const struct user_regset *regset,
+                 unsigned int pos, unsigned int count,
+                 const void *kbuf, const void __user *ubuf)
+{
+       int ret;
+
+       flush_altivec_to_thread(target);
+
+       BUILD_BUG_ON(offsetof(struct thread_struct, vscr) !=
+                    offsetof(struct thread_struct, vr[32]));
+
+       ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                &target->thread.vr, 0, 33 * sizeof(vector128));
+       if (!ret && count > 0) {
+               /*
+                * We use only the first word of vrsave.
+                */
+               union {
+                       elf_vrreg_t reg;
+                       u32 word;
+               } vrsave;
+               memset(&vrsave, 0, sizeof(vrsave));
+               vrsave.word = target->thread.vrsave;
+               ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
+                                        33 * sizeof(vector128), -1);
+               if (!ret)
+                       target->thread.vrsave = vrsave.word;
+       }
+
+       return ret;
+}
+
 /*
  * Get contents of AltiVec register state in task TASK
  */
 static int get_vrregs(unsigned long __user *data, struct task_struct *task)
 {
-       unsigned long regsize;
-
-       /* copy AltiVec registers VR[0] .. VR[31] */
-       regsize = 32 * sizeof(vector128);
-       if (copy_to_user(data, task->thread.vr, regsize))
+       if (!access_ok(VERIFY_WRITE, data,
+                      33 * sizeof(vector128) + sizeof(u32)))
                return -EFAULT;
-       data += (regsize / sizeof(unsigned long));
 
-       /* copy VSCR */
-       regsize = 1 * sizeof(vector128);
-       if (copy_to_user(data, &task->thread.vscr, regsize))
-               return -EFAULT;
-       data += (regsize / sizeof(unsigned long));
-
-       /* copy VRSAVE */
-       if (put_user(task->thread.vrsave, (u32 __user *)data))
-               return -EFAULT;
-
-       return 0;
+       return vr_get(task, NULL, 0, 33 * sizeof(vector128) + sizeof(u32),
+                     NULL, data);
 }
 
 /*
@@ -169,25 +267,11 @@ static int get_vrregs(unsigned long __user *data, struct task_struct *task)
  */
 static int set_vrregs(struct task_struct *task, unsigned long __user *data)
 {
-       unsigned long regsize;
-
-       /* copy AltiVec registers VR[0] .. VR[31] */
-       regsize = 32 * sizeof(vector128);
-       if (copy_from_user(task->thread.vr, data, regsize))
+       if (!access_ok(VERIFY_READ, data, 33 * sizeof(vector128) + sizeof(u32)))
                return -EFAULT;
-       data += (regsize / sizeof(unsigned long));
 
-       /* copy VSCR */
-       regsize = 1 * sizeof(vector128);
-       if (copy_from_user(&task->thread.vscr, data, regsize))
-               return -EFAULT;
-       data += (regsize / sizeof(unsigned long));
-
-       /* copy VRSAVE */
-       if (get_user(task->thread.vrsave, (u32 __user *)data))
-               return -EFAULT;
-
-       return 0;
+       return vr_set(task, NULL, 0, 33 * sizeof(vector128) + sizeof(u32),
+                     NULL, data);
 }
 #endif /* CONFIG_ALTIVEC */
 
@@ -203,55 +287,79 @@ static int set_vrregs(struct task_struct *task, unsigned long __user *data)
  * }
  */
 
-/*
- * Get contents of SPE register state in task TASK.
- */
-static int get_evrregs(unsigned long *data, struct task_struct *task)
+static int evr_active(struct task_struct *target,
+                     const struct user_regset *regset)
 {
-       int i;
+       flush_spe_to_thread(target);
+       return target->thread.used_spe ? regset->n : 0;
+}
 
-       if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
-               return -EFAULT;
+static int evr_get(struct task_struct *target, const struct user_regset *regset,
+                  unsigned int pos, unsigned int count,
+                  void *kbuf, void __user *ubuf)
+{
+       int ret;
 
-       /* copy SPEFSCR */
-       if (__put_user(task->thread.spefscr, &data[34]))
-               return -EFAULT;
+       flush_spe_to_thread(target);
 
-       /* copy SPE registers EVR[0] .. EVR[31] */
-       for (i = 0; i < 32; i++, data++)
-               if (__put_user(task->thread.evr[i], data))
-                       return -EFAULT;
+       ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                 &target->thread.evr,
+                                 0, sizeof(target->thread.evr));
 
-       /* copy ACC */
-       if (__put_user64(task->thread.acc, (unsigned long long *)data))
-               return -EFAULT;
+       BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
+                    offsetof(struct thread_struct, spefscr));
 
-       return 0;
+       if (!ret)
+               ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                         &target->thread.acc,
+                                         sizeof(target->thread.evr), -1);
+
+       return ret;
+}
+
+static int evr_set(struct task_struct *target, const struct user_regset *regset,
+                  unsigned int pos, unsigned int count,
+                  const void *kbuf, const void __user *ubuf)
+{
+       int ret;
+
+       flush_spe_to_thread(target);
+
+       ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                &target->thread.evr,
+                                0, sizeof(target->thread.evr));
+
+       BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
+                    offsetof(struct thread_struct, spefscr));
+
+       if (!ret)
+               ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                        &target->thread.acc,
+                                        sizeof(target->thread.evr), -1);
+
+       return ret;
 }
 
 /*
- * Write contents of SPE register state into task TASK.
+ * Get contents of SPE register state in task TASK.
  */
-static int set_evrregs(struct task_struct *task, unsigned long *data)
+static int get_evrregs(unsigned long __user *data, struct task_struct *task)
 {
-       int i;
-
-       if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
+       if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(u32)))
                return -EFAULT;
 
-       /* copy SPEFSCR */
-       if (__get_user(task->thread.spefscr, &data[34]))
-               return -EFAULT;
+       return evr_get(task, NULL, 0, 35 * sizeof(u32), NULL, data);
+}
 
-       /* copy SPE registers EVR[0] .. EVR[31] */
-       for (i = 0; i < 32; i++, data++)
-               if (__get_user(task->thread.evr[i], data))
-                       return -EFAULT;
-       /* copy ACC */
-       if (__get_user64(task->thread.acc, (unsigned long long*)data))
+/*
+ * Write contents of SPE register state into task TASK.
+ */
+static int set_evrregs(struct task_struct *task, unsigned long *data)
+{
+       if (!access_ok(VERIFY_READ, data, 35 * sizeof(u32)))
                return -EFAULT;
 
-       return 0;
+       return evr_set(task, NULL, 0, 35 * sizeof(u32), NULL, data);
 }
 #endif /* CONFIG_SPE */
 
This page took 0.037991 seconds and 5 git commands to generate.