[SPARC]: Remove PTRACE_SUN* handling.
[powerpc.git] / arch / sparc / kernel / ptrace.c
index 1baf13e..29fa6e5 100644 (file)
@@ -1,11 +1,11 @@
 /* ptrace.c: Sparc process tracing support.
  *
- * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
+ * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
  *
  * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
  * and David Mosberger.
  *
- * Added Linux support -miguel (weird, eh?, the orignal code was meant
+ * Added Linux support -miguel (weird, eh?, the original code was meant
  * to emulate SunOS).
  */
 
@@ -19,6 +19,8 @@
 #include <linux/smp_lock.h>
 #include <linux/security.h>
 #include <linux/signal.h>
+#include <linux/regset.h>
+#include <linux/elf.h>
 
 #include <asm/pgtable.h>
 #include <asm/system.h>
@@ -155,7 +157,7 @@ static inline void read_sunos_user(struct pt_regs *regs, unsigned long offset,
                /* Rest of them are completely unsupported. */
        default:
                printk("%s [%d]: Wants to read user offset %ld\n",
-                      current->comm, current->pid, offset);
+                      current->comm, task_pid_nr(current), offset);
                pt_error_return(regs, EIO);
                return;
        }
@@ -222,7 +224,7 @@ static inline void write_sunos_user(struct pt_regs *regs, unsigned long offset,
                /* Rest of them are completely unsupported or "no-touch". */
        default:
                printk("%s [%d]: Wants to write user offset %ld\n",
-                      current->comm, current->pid, offset);
+                      current->comm, task_pid_nr(current), offset);
                goto failure;
        }
 success:
@@ -234,19 +236,6 @@ failure:
 }
 
 /* #define ALLOW_INIT_TRACING */
-/* #define DEBUG_PTRACE */
-
-#ifdef DEBUG_PTRACE
-char *pt_rq [] = {
-       /* 0  */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
-       /* 4  */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
-       /* 8  */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
-       /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
-       /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
-       /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
-       /* 24 */ "SYSCALL", ""
-};
-#endif
 
 /*
  * Called by kernel/ptrace.c when detaching..
@@ -258,6 +247,287 @@ void ptrace_disable(struct task_struct *child)
        /* nothing to do */
 }
 
+enum sparc_regset {
+       REGSET_GENERAL,
+       REGSET_FP,
+};
+
+static int genregs32_get(struct task_struct *target,
+                        const struct user_regset *regset,
+                        unsigned int pos, unsigned int count,
+                        void *kbuf, void __user *ubuf)
+{
+       const struct pt_regs *regs = target->thread.kregs;
+       unsigned long __user *reg_window;
+       unsigned long *k = kbuf;
+       unsigned long __user *u = ubuf;
+       unsigned long reg;
+
+       if (target == current)
+               flush_user_windows();
+
+       pos /= sizeof(reg);
+       count /= sizeof(reg);
+
+       if (kbuf) {
+               for (; count > 0 && pos < 16; count--)
+                       *k++ = regs->u_regs[pos++];
+
+               reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
+               for (; count > 0 && pos < 32; count--) {
+                       if (get_user(*k++, &reg_window[pos++]))
+                               return -EFAULT;
+               }
+       } else {
+               for (; count > 0 && pos < 16; count--) {
+                       if (put_user(regs->u_regs[pos++], u++))
+                               return -EFAULT;
+               }
+
+               reg_window = (unsigned long __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;
+               }
+       }
+       while (count > 0) {
+               switch (pos) {
+               case 32: /* PSR */
+                       reg = regs->psr;
+                       break;
+               case 33: /* PC */
+                       reg = regs->pc;
+                       break;
+               case 34: /* NPC */
+                       reg = regs->npc;
+                       break;
+               case 35: /* Y */
+                       reg = regs->y;
+                       break;
+               case 36: /* WIM */
+               case 37: /* TBR */
+                       reg = 0;
+                       break;
+               default:
+                       goto finish;
+               }
+
+               if (kbuf)
+                       *k++ = reg;
+               else if (put_user(reg, u++))
+                       return -EFAULT;
+               pos++;
+               count--;
+       }
+finish:
+       pos *= sizeof(reg);
+       count *= sizeof(reg);
+
+       return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
+                                       38 * sizeof(reg), -1);
+}
+
+static int genregs32_set(struct task_struct *target,
+                        const struct user_regset *regset,
+                        unsigned int pos, unsigned int count,
+                        const void *kbuf, const void __user *ubuf)
+{
+       struct pt_regs *regs = target->thread.kregs;
+       unsigned long __user *reg_window;
+       const unsigned long *k = kbuf;
+       const unsigned long __user *u = ubuf;
+       unsigned long reg;
+
+       if (target == current)
+               flush_user_windows();
+
+       pos /= sizeof(reg);
+       count /= sizeof(reg);
+
+       if (kbuf) {
+               for (; count > 0 && pos < 16; count--)
+                       regs->u_regs[pos++] = *k++;
+
+               reg_window = (unsigned long __user *) regs->u_regs[UREG_I6];
+               for (; count > 0 && pos < 32; count--) {
+                       if (put_user(*k++, &reg_window[pos++]))
+                               return -EFAULT;
+               }
+       } else {
+               for (; count > 0 && pos < 16; count--) {
+                       if (get_user(reg, u++))
+                               return -EFAULT;
+                       regs->u_regs[pos++] = reg;
+               }
+
+               reg_window = (unsigned long __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;
+               }
+       }
+       while (count > 0) {
+               unsigned long psr;
+
+               if (kbuf)
+                       reg = *k++;
+               else if (get_user(reg, u++))
+                       return -EFAULT;
+
+               switch (pos) {
+               case 32: /* PSR */
+                       psr = regs->psr;
+                       psr &= ~PSR_ICC;
+                       psr |= (reg & PSR_ICC);
+                       regs->psr = psr;
+                       break;
+               case 33: /* PC */
+                       regs->pc = reg;
+                       break;
+               case 34: /* NPC */
+                       regs->npc = reg;
+                       break;
+               case 35: /* Y */
+                       regs->y = reg;
+                       break;
+               case 36: /* WIM */
+               case 37: /* TBR */
+                       break;
+               default:
+                       goto finish;
+               }
+
+               pos++;
+               count--;
+       }
+finish:
+       pos *= sizeof(reg);
+       count *= sizeof(reg);
+
+       return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
+                                        38 * sizeof(reg), -1);
+}
+
+static int fpregs32_get(struct task_struct *target,
+                       const struct user_regset *regset,
+                       unsigned int pos, unsigned int count,
+                       void *kbuf, void __user *ubuf)
+{
+       const unsigned long *fpregs = target->thread.float_regs;
+       int ret = 0;
+
+#if 0
+       if (target == current)
+               save_and_clear_fpu();
+#endif
+
+       ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                 fpregs,
+                                 0, 32 * sizeof(u32));
+
+       if (!ret)
+               ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
+                                              32 * sizeof(u32),
+                                              33 * sizeof(u32));
+       if (!ret)
+               ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                         &target->thread.fsr,
+                                         33 * sizeof(u32),
+                                         34 * sizeof(u32));
+
+       if (!ret) {
+               unsigned long val;
+
+               val = (1 << 8) | (8 << 16);
+               ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
+                                         &val,
+                                         34 * sizeof(u32),
+                                         35 * sizeof(u32));
+       }
+
+       if (!ret)
+               ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
+                                              35 * sizeof(u32), -1);
+
+       return ret;
+}
+
+static int fpregs32_set(struct task_struct *target,
+                       const struct user_regset *regset,
+                       unsigned int pos, unsigned int count,
+                       const void *kbuf, const void __user *ubuf)
+{
+       unsigned long *fpregs = target->thread.float_regs;
+       int ret;
+
+#if 0
+       if (target == current)
+               save_and_clear_fpu();
+#endif
+       ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                fpregs,
+                                0, 32 * sizeof(u32));
+       if (!ret)
+               user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
+                                         32 * sizeof(u32),
+                                         33 * sizeof(u32));
+       if (!ret && count > 0) {
+               ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
+                                        &target->thread.fsr,
+                                        33 * sizeof(u32),
+                                        34 * sizeof(u32));
+       }
+
+       if (!ret)
+               ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
+                                               34 * sizeof(u32), -1);
+       return ret;
+}
+
+static const struct user_regset sparc32_regsets[] = {
+       /* Format is:
+        *      G0 --> G7
+        *      O0 --> O7
+        *      L0 --> L7
+        *      I0 --> I7
+        *      PSR, PC, nPC, Y, WIM, TBR
+        */
+       [REGSET_GENERAL] = {
+               .core_note_type = NT_PRSTATUS,
+               .n = 38 * sizeof(u32),
+               .size = sizeof(u32), .align = sizeof(u32),
+               .get = genregs32_get, .set = genregs32_set
+       },
+       /* Format is:
+        *      F0 --> F31
+        *      empty 32-bit word
+        *      FSR (32--bit word)
+        *      FPU QUEUE COUNT (8-bit char)
+        *      FPU QUEUE ENTRYSIZE (8-bit char)
+        *      FPU ENABLED (8-bit char)
+        *      empty 8-bit char
+        *      FPU QUEUE (64 32-bit ints)
+        */
+       [REGSET_FP] = {
+               .core_note_type = NT_PRFPREG,
+               .n = 99 * sizeof(u32),
+               .size = sizeof(u32), .align = sizeof(u32),
+               .get = fpregs32_get, .set = fpregs32_set
+       },
+};
+
+static const struct user_regset_view user_sparc32_view = {
+       .name = "sparc", .e_machine = EM_SPARC,
+       .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
+};
+
+const struct user_regset_view *task_user_regset_view(struct task_struct *task)
+{
+       return &user_sparc32_view;
+}
+
 asmlinkage void do_ptrace(struct pt_regs *regs)
 {
        unsigned long request = regs->u_regs[UREG_I0];
@@ -269,27 +539,13 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
        int ret;
 
        lock_kernel();
-#ifdef DEBUG_PTRACE
-       {
-               char *s;
-
-               if ((request >= 0) && (request <= 24))
-                       s = pt_rq [request];
-               else
-                       s = "unknown";
-
-               if (request == PTRACE_POKEDATA && data == 0x91d02001){
-                       printk ("do_ptrace: breakpoint pid=%d, addr=%08lx addr2=%08lx\n",
-                               pid, addr, addr2);
-               } else 
-                       printk("do_ptrace: rq=%s(%d) pid=%d addr=%08lx data=%08lx addr2=%08lx\n",
-                              s, (int) request, (int) pid, addr, data, addr2);
-       }
-#endif
 
        if (request == PTRACE_TRACEME) {
                ret = ptrace_traceme();
-               pt_succ_return(regs, 0);
+               if (ret < 0)
+                       pt_error_return(regs, -ret);
+               else
+                       pt_succ_return(regs, 0);
                goto out;
        }
 
@@ -300,8 +556,7 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
                goto out;
        }
 
-       if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
-           || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
+       if (request == PTRACE_ATTACH) {
                if (ptrace_attach(child)) {
                        pt_error_return(regs, EPERM);
                        goto out_tsk;
@@ -364,9 +619,6 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
                for(rval = 1; rval < 16; rval++)
                        __put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]));
                pt_succ_return(regs, 0);
-#ifdef DEBUG_PTRACE
-               printk ("PC=%x nPC=%x o7=%x\n", cregs->pc, cregs->npc, cregs->u_regs [15]);
-#endif
                goto out_tsk;
        }
 
@@ -515,12 +767,6 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
                        clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
 
                child->exit_code = data;
-#ifdef DEBUG_PTRACE
-               printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n",
-                       child->comm, child->pid, child->exit_code,
-                       child->thread.kregs->pc,
-                       child->thread.kregs->npc);
-#endif
                wake_up_process(child);
                pt_succ_return(regs, 0);
                goto out_tsk;
@@ -542,18 +788,6 @@ asmlinkage void do_ptrace(struct pt_regs *regs)
                goto out_tsk;
        }
 
-       case PTRACE_SUNDETACH: { /* detach a process that was attached. */
-               int err = ptrace_detach(child, data);
-               if (err) {
-                       pt_error_return(regs, EIO);
-                       goto out_tsk;
-               }
-               pt_succ_return(regs, 0);
-               goto out_tsk;
-       }
-
-       /* PTRACE_DUMPCORE unsupported... */
-
        default: {
                int err = ptrace_request(child, request, addr, data);
                if (err)
@@ -572,9 +806,6 @@ out:
 
 asmlinkage void syscall_trace(void)
 {
-#ifdef DEBUG_PTRACE
-       printk("%s [%d]: syscall_trace\n", current->comm, current->pid);
-#endif
        if (!test_thread_flag(TIF_SYSCALL_TRACE))
                return;
        if (!(current->ptrace & PT_PTRACED))
@@ -587,10 +818,6 @@ asmlinkage void syscall_trace(void)
         * for normal use.  strace only continues with a signal if the
         * stopping signal is not SIGTRAP.  -brl
         */
-#ifdef DEBUG_PTRACE
-       printk("%s [%d]: syscall_trace exit= %x\n", current->comm,
-               current->pid, current->exit_code);
-#endif
        if (current->exit_code) {
                send_sig (current->exit_code, current, 1);
                current->exit_code = 0;