[POWERPC] ptrace accessors for special regs MSR and TRAP
[deliverable/linux.git] / arch / powerpc / kernel / ptrace.c
index 8a177bd9eab4ff5746963e484e22b021d9d2578d..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))
-               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))
+       if (!access_ok(VERIFY_READ, data, 33 * sizeof(vector128) + sizeof(u32)))
                return -EFAULT;
 
-       return 0;
+       return vr_set(task, NULL, 0, 33 * sizeof(vector128) + sizeof(u32),
+                     NULL, data);
 }
 #endif /* CONFIG_ALTIVEC */
 
@@ -203,60 +287,84 @@ 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)
+{
+       flush_spe_to_thread(target);
+       return target->thread.used_spe ? regset->n : 0;
+}
+
+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 i;
+       int ret;
 
-       if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
-               return -EFAULT;
+       flush_spe_to_thread(target);
 
-       /* copy SPEFSCR */
-       if (__put_user(task->thread.spefscr, &data[34]))
-               return -EFAULT;
+       ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                 &target->thread.evr,
+                                 0, sizeof(target->thread.evr));
 
-       /* copy SPE registers EVR[0] .. EVR[31] */
-       for (i = 0; i < 32; i++, data++)
-               if (__put_user(task->thread.evr[i], data))
-                       return -EFAULT;
+       BUILD_BUG_ON(offsetof(struct thread_struct, acc) + sizeof(u64) !=
+                    offsetof(struct thread_struct, spefscr));
 
-       /* copy ACC */
-       if (__put_user64(task->thread.acc, (unsigned long long *)data))
-               return -EFAULT;
+       if (!ret)
+               ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                         &target->thread.acc,
+                                         sizeof(target->thread.evr), -1);
 
-       return 0;
+       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 */
 
 
-static void set_single_step(struct task_struct *task)
+void user_enable_single_step(struct task_struct *task)
 {
        struct pt_regs *regs = task->thread.regs;
 
@@ -271,7 +379,7 @@ static void set_single_step(struct task_struct *task)
        set_tsk_thread_flag(task, TIF_SINGLESTEP);
 }
 
-static void clear_single_step(struct task_struct *task)
+void user_disable_single_step(struct task_struct *task)
 {
        struct pt_regs *regs = task->thread.regs;
 
@@ -313,7 +421,7 @@ static int ptrace_set_debugreg(struct task_struct *task, unsigned long addr,
 void ptrace_disable(struct task_struct *child)
 {
        /* make sure the single step bit is not set. */
-       clear_single_step(child);
+       user_disable_single_step(child);
 }
 
 /*
@@ -331,6 +439,7 @@ static long arch_ptrace_old(struct task_struct *child, long request, long addr,
                unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
                unsigned long __user *tmp = (unsigned long __user *)addr;
 
+               CHECK_FULL_REGS(child->thread.regs);
                for (i = 0; i < 32; i++) {
                        ret = put_user(*reg, tmp);
                        if (ret)
@@ -346,6 +455,7 @@ static long arch_ptrace_old(struct task_struct *child, long request, long addr,
                unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
                unsigned long __user *tmp = (unsigned long __user *)addr;
 
+               CHECK_FULL_REGS(child->thread.regs);
                for (i = 0; i < 32; i++) {
                        ret = get_user(*reg, tmp);
                        if (ret)
@@ -443,52 +553,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                break;
        }
 
-       case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
-       case PTRACE_CONT: { /* restart after signal. */
-               ret = -EIO;
-               if (!valid_signal(data))
-                       break;
-               if (request == PTRACE_SYSCALL)
-                       set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-               else
-                       clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-               child->exit_code = data;
-               /* make sure the single step bit is not set. */
-               clear_single_step(child);
-               wake_up_process(child);
-               ret = 0;
-               break;
-       }
-
-/*
- * make the child exit.  Best I can do is send it a sigkill.
- * perhaps it should be put in the status that it wants to
- * exit.
- */
-       case PTRACE_KILL: {
-               ret = 0;
-               if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
-                       break;
-               child->exit_code = SIGKILL;
-               /* make sure the single step bit is not set. */
-               clear_single_step(child);
-               wake_up_process(child);
-               break;
-       }
-
-       case PTRACE_SINGLESTEP: {  /* set the trap flag. */
-               ret = -EIO;
-               if (!valid_signal(data))
-                       break;
-               clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-               set_single_step(child);
-               child->exit_code = data;
-               /* give it a chance to run. */
-               wake_up_process(child);
-               ret = 0;
-               break;
-       }
-
        case PTRACE_GET_DEBUGREG: {
                ret = -EINVAL;
                /* We only support one DABR and no IABRS at the moment */
@@ -503,10 +567,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                ret = ptrace_set_debugreg(child, addr, data);
                break;
 
-       case PTRACE_DETACH:
-               ret = ptrace_detach(child, data);
-               break;
-
 #ifdef CONFIG_PPC64
        case PTRACE_GETREGS64:
 #endif
@@ -517,6 +577,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                        ret = -EIO;
                        break;
                }
+               CHECK_FULL_REGS(child->thread.regs);
                ret = 0;
                for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
                        ret |= __put_user(ptrace_get_reg(child, ui),
@@ -537,6 +598,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                        ret = -EIO;
                        break;
                }
+               CHECK_FULL_REGS(child->thread.regs);
                ret = 0;
                for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
                        ret = __get_user(tmp, (unsigned long __user *) data);
@@ -576,8 +638,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 #ifdef CONFIG_SPE
        case PTRACE_GETEVRREGS:
                /* Get the child spe register state. */
-               if (child->thread.regs->msr & MSR_SPE)
-                       giveup_spe(child);
+               flush_spe_to_thread(child);
                ret = get_evrregs((unsigned long __user *)data, child);
                break;
 
@@ -585,8 +646,7 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                /* Set the child spe register state. */
                /* this is to clear the MSR_SPE bit to force a reload
                 * of register state from memory */
-               if (child->thread.regs->msr & MSR_SPE)
-                       giveup_spe(child);
+               flush_spe_to_thread(child);
                ret = set_evrregs(child, (unsigned long __user *)data);
                break;
 #endif
This page took 0.036819 seconds and 5 git commands to generate.