]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/sparc64/kernel/ptrace.c
[SPARC]: Fix several regset and ptrace bugs.
[linux-2.6-omap-h63xx.git] / arch / sparc64 / kernel / ptrace.c
index 51f012410f9d1118197ba4480da764eac6b3622b..e9fc0aa2da386e8161a7a4badedb8f77b8ce7789 100644 (file)
@@ -35,6 +35,9 @@
 #include <asm/spitfire.h>
 #include <asm/page.h>
 #include <asm/cpudata.h>
+#include <asm/cacheflush.h>
+
+#include "entry.h"
 
 /* #define ALLOW_INIT_TRACING */
 
@@ -67,6 +70,8 @@ void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
        if (tlb_type == hypervisor)
                return;
 
+       preempt_disable();
+
 #ifdef DCACHE_ALIASING_POSSIBLE
        /* If bit 13 of the kernel address we used to access the
         * user page is the same as the virtual address that page
@@ -105,6 +110,87 @@ void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
                for (; start < end; start += icache_line_size)
                        flushi(start);
        }
+
+       preempt_enable();
+}
+
+static int get_from_target(struct task_struct *target, unsigned long uaddr,
+                          void *kbuf, int len)
+{
+       if (target == current) {
+               if (copy_from_user(kbuf, (void __user *) uaddr, len))
+                       return -EFAULT;
+       } else {
+               int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
+               if (len2 != len)
+                       return -EFAULT;
+       }
+       return 0;
+}
+
+static int set_to_target(struct task_struct *target, unsigned long uaddr,
+                        void *kbuf, int len)
+{
+       if (target == current) {
+               if (copy_to_user((void __user *) uaddr, kbuf, len))
+                       return -EFAULT;
+       } else {
+               int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
+               if (len2 != len)
+                       return -EFAULT;
+       }
+       return 0;
+}
+
+static int regwindow64_get(struct task_struct *target,
+                          const struct pt_regs *regs,
+                          struct reg_window *wbuf)
+{
+       unsigned long rw_addr = regs->u_regs[UREG_I6];
+
+       if (test_tsk_thread_flag(current, TIF_32BIT)) {
+               struct reg_window32 win32;
+               int i;
+
+               if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
+                       return -EFAULT;
+               for (i = 0; i < 8; i++)
+                       wbuf->locals[i] = win32.locals[i];
+               for (i = 0; i < 8; i++)
+                       wbuf->ins[i] = win32.ins[i];
+       } else {
+               rw_addr += STACK_BIAS;
+               if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
+                       return -EFAULT;
+       }
+
+       return 0;
+}
+
+static int regwindow64_set(struct task_struct *target,
+                          const struct pt_regs *regs,
+                          struct reg_window *wbuf)
+{
+       unsigned long rw_addr = regs->u_regs[UREG_I6];
+
+       if (test_tsk_thread_flag(current, TIF_32BIT)) {
+               struct reg_window32 win32;
+               int i;
+
+               for (i = 0; i < 8; i++)
+                       win32.locals[i] = wbuf->locals[i];
+               for (i = 0; i < 8; i++)
+                       win32.ins[i] = wbuf->ins[i];
+
+               if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
+                       return -EFAULT;
+       } else {
+               rw_addr += STACK_BIAS;
+               if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
+                       return -EFAULT;
+       }
+
+       return 0;
 }
 
 enum sparc_regset {
@@ -126,16 +212,13 @@ static int genregs64_get(struct task_struct *target,
        ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
                                  regs->u_regs,
                                  0, 16 * sizeof(u64));
-       if (!ret) {
-               unsigned long __user *reg_window = (unsigned long __user *)
-                       (regs->u_regs[UREG_I6] + STACK_BIAS);
-               unsigned long window[16];
+       if (!ret && count && pos < (32 * sizeof(u64))) {
+               struct reg_window window;
 
-               if (copy_from_user(window, reg_window, sizeof(window)))
+               if (regwindow64_get(target, regs, &window))
                        return -EFAULT;
-
                ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
-                                         window,
+                                         &window,
                                          16 * sizeof(u64),
                                          32 * sizeof(u64));
        }
@@ -157,10 +240,11 @@ static int genregs64_get(struct task_struct *target,
                                          36 * sizeof(u64));
        }
 
-       if (!ret)
+       if (!ret) {
                ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
                                               36 * sizeof(u64), -1);
 
+       }
        return ret;
 }
 
@@ -178,20 +262,19 @@ static int genregs64_set(struct task_struct *target,
        ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
                                 regs->u_regs,
                                 0, 16 * sizeof(u64));
-       if (!ret && count > 0) {
-               unsigned long __user *reg_window = (unsigned long __user *)
-                       (regs->u_regs[UREG_I6] + STACK_BIAS);
-               unsigned long window[16];
+       if (!ret && count && pos < (32 * sizeof(u64))) {
+               struct reg_window window;
 
-               if (copy_from_user(window, reg_window, sizeof(window)))
+               if (regwindow64_get(target, regs, &window))
                        return -EFAULT;
 
                ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
-                                        window,
+                                        &window,
                                         16 * sizeof(u64),
                                         32 * sizeof(u64));
+
                if (!ret &&
-                   copy_to_user(reg_window, window, sizeof(window)))
+                   regwindow64_set(target, regs, &window))
                        return -EFAULT;
        }
 
@@ -382,6 +465,7 @@ static const struct user_regset_view user_sparc64_view = {
        .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
 };
 
+#ifdef CONFIG_COMPAT
 static int genregs32_get(struct task_struct *target,
                         const struct user_regset *regset,
                         unsigned int pos, unsigned int count,
@@ -404,9 +488,22 @@ static int genregs32_get(struct task_struct *target,
                        *k++ = regs->u_regs[pos++];
 
                reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
-               for (; count > 0 && pos < 32; count--) {
-                       if (get_user(*k++, &reg_window[pos++]))
-                               return -EFAULT;
+               if (target == current) {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (get_user(*k++, &reg_window[pos++]))
+                                       return -EFAULT;
+                       }
+               } else {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (access_process_vm(target,
+                                                     (unsigned long)
+                                                     &reg_window[pos],
+                                                     k, sizeof(*k), 0)
+                                   != sizeof(*k))
+                                       return -EFAULT;
+                               k++;
+                               pos++;
+                       }
                }
        } else {
                for (; count > 0 && pos < 16; count--) {
@@ -415,10 +512,28 @@ static int genregs32_get(struct task_struct *target,
                }
 
                reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
-               for (; count > 0 && pos < 32; count--) {
-                       if (get_user(reg, &reg_window[pos++]) ||
-                           put_user(reg, u++))
-                               return -EFAULT;
+               if (target == current) {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (get_user(reg, &reg_window[pos++]) ||
+                                   put_user(reg, u++))
+                                       return -EFAULT;
+                       }
+               } else {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (access_process_vm(target,
+                                                     (unsigned long)
+                                                     &reg_window[pos],
+                                                     &reg, sizeof(reg), 0)
+                                   != sizeof(reg))
+                                       return -EFAULT;
+                               if (access_process_vm(target,
+                                                     (unsigned long) u,
+                                                     &reg, sizeof(reg), 1)
+                                   != sizeof(reg))
+                                       return -EFAULT;
+                               pos++;
+                               u++;
+                       }
                }
        }
        while (count > 0) {
@@ -480,9 +595,23 @@ static int genregs32_set(struct task_struct *target,
                        regs->u_regs[pos++] = *k++;
 
                reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
-               for (; count > 0 && pos < 32; count--) {
-                       if (put_user(*k++, &reg_window[pos++]))
-                               return -EFAULT;
+               if (target == current) {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (put_user(*k++, &reg_window[pos++]))
+                                       return -EFAULT;
+                       }
+               } else {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (access_process_vm(target,
+                                                     (unsigned long)
+                                                     &reg_window[pos],
+                                                     (void *) k,
+                                                     sizeof(*k), 1)
+                                   != sizeof(*k))
+                                       return -EFAULT;
+                               k++;
+                               pos++;
+                       }
                }
        } else {
                for (; count > 0 && pos < 16; count--) {
@@ -492,10 +621,29 @@ static int genregs32_set(struct task_struct *target,
                }
 
                reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
-               for (; count > 0 && pos < 32; count--) {
-                       if (get_user(reg, u++) ||
-                           put_user(reg, &reg_window[pos++]))
-                               return -EFAULT;
+               if (target == current) {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (get_user(reg, u++) ||
+                                   put_user(reg, &reg_window[pos++]))
+                                       return -EFAULT;
+                       }
+               } else {
+                       for (; count > 0 && pos < 32; count--) {
+                               if (access_process_vm(target,
+                                                     (unsigned long)
+                                                     u,
+                                                     &reg, sizeof(reg), 0)
+                                   != sizeof(reg))
+                                       return -EFAULT;
+                               if (access_process_vm(target,
+                                                     (unsigned long)
+                                                     &reg_window[pos],
+                                                     &reg, sizeof(reg), 1)
+                                   != sizeof(reg))
+                                       return -EFAULT;
+                               pos++;
+                               u++;
+                       }
                }
        }
        while (count > 0) {
@@ -676,80 +824,51 @@ static const struct user_regset_view user_sparc32_view = {
        .name = "sparc", .e_machine = EM_SPARC,
        .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
 };
+#endif /* CONFIG_COMPAT */
 
 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
 {
+#ifdef CONFIG_COMPAT
        if (test_tsk_thread_flag(task, TIF_32BIT))
                return &user_sparc32_view;
+#endif
        return &user_sparc64_view;
 }
 
-long arch_ptrace(struct task_struct *child, long request, long addr, long data)
+#ifdef CONFIG_COMPAT
+struct compat_fps {
+       unsigned int regs[32];
+       unsigned int fsr;
+       unsigned int flags;
+       unsigned int extra;
+       unsigned int fpqd;
+       struct compat_fq {
+               unsigned int insnaddr;
+               unsigned int insn;
+       } fpq[16];
+};
+
+long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
+                       compat_ulong_t caddr, compat_ulong_t cdata)
 {
-       long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
-       const struct user_regset_view *view;
+       const struct user_regset_view *view = task_user_regset_view(current);
+       compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
+       struct pt_regs32 __user *pregs;
+       struct compat_fps __user *fps;
+       unsigned long addr2 = caddr2;
+       unsigned long addr = caddr;
+       unsigned long data = cdata;
        int ret;
 
-       if (test_thread_flag(TIF_32BIT))
-               addr2 &= 0xffffffffUL;
-
-       view = task_user_regset_view(child);
+       pregs = (struct pt_regs32 __user *) addr;
+       fps = (struct compat_fps __user *) addr;
 
-       switch(request) {
+       switch (request) {
        case PTRACE_PEEKUSR:
                ret = (addr != 0) ? -EIO : 0;
                break;
 
-       case PTRACE_PEEKTEXT: /* read word at location addr. */ 
-       case PTRACE_PEEKDATA: {
-               unsigned long tmp64;
-               unsigned int tmp32;
-               int copied;
-
-               ret = -EIO;
-               if (test_thread_flag(TIF_32BIT)) {
-                       copied = access_process_vm(child, addr,
-                                                  &tmp32, sizeof(tmp32), 0);
-                       if (copied == sizeof(tmp32))
-                               ret = put_user(tmp32,
-                                              (unsigned int __user *) data);
-               } else {
-                       copied = access_process_vm(child, addr,
-                                                  &tmp64, sizeof(tmp64), 0);
-                       if (copied == sizeof(tmp64))
-                               ret = put_user(tmp64,
-                                              (unsigned long __user *) data);
-               }
-               break;
-       }
-
-       case PTRACE_POKETEXT: /* write the word at location addr. */
-       case PTRACE_POKEDATA: {
-               unsigned long tmp64;
-               unsigned int tmp32;
-               int copied;
-
-               ret = -EIO;
-               if (test_thread_flag(TIF_32BIT)) {
-                       tmp32 = data;
-                       copied = access_process_vm(child, addr,
-                                                  &tmp32, sizeof(tmp32), 1);
-                       if (copied == sizeof(tmp32))
-                               ret = 0;
-               } else {
-                       tmp64 = data;
-                       copied = access_process_vm(child, addr,
-                                                  &tmp64, sizeof(tmp64), 1);
-                       if (copied == sizeof(tmp64))
-                               ret = 0;
-               }
-               break;
-       }
-
-       case PTRACE_GETREGS: {
-               struct pt_regs32 __user *pregs =
-                       (struct pt_regs32 __user *) addr;
-
+       case PTRACE_GETREGS:
                ret = copy_regset_to_user(child, view, REGSET_GENERAL,
                                          32 * sizeof(u32),
                                          4 * sizeof(u32),
@@ -760,29 +879,8 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                                                  15 * sizeof(u32),
                                                  &pregs->u_regs[0]);
                break;
-       }
-
-       case PTRACE_GETREGS64: {
-               struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
-
-               ret = copy_regset_to_user(child, view, REGSET_GENERAL,
-                                         1 * sizeof(u64),
-                                         15 * sizeof(u64),
-                                         &pregs->u_regs[0]);
-               if (!ret) {
-                       /* XXX doesn't handle 'y' register correctly XXX */
-                       ret = copy_regset_to_user(child, view, REGSET_GENERAL,
-                                                 32 * sizeof(u64),
-                                                 4 * sizeof(u64),
-                                                 &pregs->tstate);
-               }
-               break;
-       }
-
-       case PTRACE_SETREGS: {
-               struct pt_regs32 __user *pregs =
-                       (struct pt_regs32 __user *) addr;
 
+       case PTRACE_SETREGS:
                ret = copy_regset_from_user(child, view, REGSET_GENERAL,
                                            32 * sizeof(u32),
                                            4 * sizeof(u32),
@@ -793,39 +891,8 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                                                    15 * sizeof(u32),
                                                    &pregs->u_regs[0]);
                break;
-       }
-
-       case PTRACE_SETREGS64: {
-               struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
-
-               ret = copy_regset_from_user(child, view, REGSET_GENERAL,
-                                           1 * sizeof(u64),
-                                           15 * sizeof(u64),
-                                           &pregs->u_regs[0]);
-               if (!ret) {
-                       /* XXX doesn't handle 'y' register correctly XXX */
-                       ret = copy_regset_from_user(child, view, REGSET_GENERAL,
-                                                   32 * sizeof(u64),
-                                                   4 * sizeof(u64),
-                                                   &pregs->tstate);
-               }
-               break;
-       }
-
-       case PTRACE_GETFPREGS: {
-               struct fps {
-                       unsigned int regs[32];
-                       unsigned int fsr;
-                       unsigned int flags;
-                       unsigned int extra;
-                       unsigned int fpqd;
-                       struct fq {
-                               unsigned int insnaddr;
-                               unsigned int insn;
-                       } fpq[16];
-               };
-               struct fps __user *fps = (struct fps __user *) addr;
 
+       case PTRACE_GETFPREGS:
                ret = copy_regset_to_user(child, view, REGSET_FP,
                                          0 * sizeof(u32),
                                          32 * sizeof(u32),
@@ -843,36 +910,8 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                                ret = -EFAULT;
                }
                break;
-       }
-
-       case PTRACE_GETFPREGS64: {
-               struct fps {
-                       unsigned int regs[64];
-                       unsigned long fsr;
-               };
-               struct fps __user *fps = (struct fps __user *) addr;
-
-               ret = copy_regset_to_user(child, view, REGSET_FP,
-                                         0 * sizeof(u64),
-                                         33 * sizeof(u64),
-                                         fps);
-               break;
-       }
-
-       case PTRACE_SETFPREGS: {
-               struct fps {
-                       unsigned int regs[32];
-                       unsigned int fsr;
-                       unsigned int flags;
-                       unsigned int extra;
-                       unsigned int fpqd;
-                       struct fq {
-                               unsigned int insnaddr;
-                               unsigned int insn;
-                       } fpq[16];
-               };
-               struct fps __user *fps = (struct fps __user *) addr;
 
+       case PTRACE_SETFPREGS:
                ret = copy_regset_from_user(child, view, REGSET_FP,
                                            0 * sizeof(u32),
                                            32 * sizeof(u32),
@@ -883,21 +922,98 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                                                    1 * sizeof(u32),
                                                    &fps->fsr);
                break;
+
+       case PTRACE_READTEXT:
+       case PTRACE_READDATA:
+               ret = ptrace_readdata(child, addr,
+                                     (char __user *)addr2, data);
+               if (ret == data)
+                       ret = 0;
+               else if (ret >= 0)
+                       ret = -EIO;
+               break;
+
+       case PTRACE_WRITETEXT:
+       case PTRACE_WRITEDATA:
+               ret = ptrace_writedata(child, (char __user *) addr2,
+                                      addr, data);
+               if (ret == data)
+                       ret = 0;
+               else if (ret >= 0)
+                       ret = -EIO;
+               break;
+
+       default:
+               ret = compat_ptrace_request(child, request, addr, data);
+               break;
        }
 
-       case PTRACE_SETFPREGS64: {
-               struct fps {
-                       unsigned int regs[64];
-                       unsigned long fsr;
-               };
-               struct fps __user *fps = (struct fps __user *) addr;
+       return ret;
+}
+#endif /* CONFIG_COMPAT */
+
+struct fps {
+       unsigned int regs[64];
+       unsigned long fsr;
+};
 
+long arch_ptrace(struct task_struct *child, long request, long addr, long data)
+{
+       const struct user_regset_view *view = task_user_regset_view(current);
+       unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
+       struct pt_regs __user *pregs;
+       struct fps __user *fps;
+       int ret;
+
+       pregs = (struct pt_regs __user *) (unsigned long) addr;
+       fps = (struct fps __user *) (unsigned long) addr;
+
+       switch (request) {
+       case PTRACE_PEEKUSR:
+               ret = (addr != 0) ? -EIO : 0;
+               break;
+
+       case PTRACE_GETREGS64:
+               ret = copy_regset_to_user(child, view, REGSET_GENERAL,
+                                         1 * sizeof(u64),
+                                         15 * sizeof(u64),
+                                         &pregs->u_regs[0]);
+               if (!ret) {
+                       /* XXX doesn't handle 'y' register correctly XXX */
+                       ret = copy_regset_to_user(child, view, REGSET_GENERAL,
+                                                 32 * sizeof(u64),
+                                                 4 * sizeof(u64),
+                                                 &pregs->tstate);
+               }
+               break;
+
+       case PTRACE_SETREGS64:
+               ret = copy_regset_from_user(child, view, REGSET_GENERAL,
+                                           1 * sizeof(u64),
+                                           15 * sizeof(u64),
+                                           &pregs->u_regs[0]);
+               if (!ret) {
+                       /* XXX doesn't handle 'y' register correctly XXX */
+                       ret = copy_regset_from_user(child, view, REGSET_GENERAL,
+                                                   32 * sizeof(u64),
+                                                   4 * sizeof(u64),
+                                                   &pregs->tstate);
+               }
+               break;
+
+       case PTRACE_GETFPREGS64:
+               ret = copy_regset_to_user(child, view, REGSET_FP,
+                                         0 * sizeof(u64),
+                                         33 * sizeof(u64),
+                                         fps);
+               break;
+
+       case PTRACE_SETFPREGS64:
                ret = copy_regset_to_user(child, view, REGSET_FP,
                                          0 * sizeof(u64),
                                          33 * sizeof(u64),
                                          fps);
                break;
-       }
 
        case PTRACE_READTEXT:
        case PTRACE_READDATA:
@@ -919,16 +1035,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                        ret = -EIO;
                break;
 
-       case PTRACE_GETEVENTMSG: {
-               if (test_thread_flag(TIF_32BIT))
-                       ret = put_user(child->ptrace_message,
-                                      (unsigned int __user *) data);
-               else
-                       ret = put_user(child->ptrace_message,
-                                      (unsigned long __user *) data);
-               break;
-       }
-
        default:
                ret = ptrace_request(child, request, addr, data);
                break;