]> err.no Git - linux-2.6/blobdiff - arch/x86/kernel/ptrace.c
Merge branch 'for-linus' of git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-2.6] / arch / x86 / kernel / ptrace.c
index ec86abaab530fb45e0a5ef15ca65c9fd16836442..702c33efea84dfad40b15d62cd7775958d653979 100644 (file)
@@ -13,7 +13,9 @@
 #include <linux/smp.h>
 #include <linux/errno.h>
 #include <linux/ptrace.h>
+#include <linux/regset.h>
 #include <linux/user.h>
+#include <linux/elf.h>
 #include <linux/security.h>
 #include <linux/audit.h>
 #include <linux/seccomp.h>
 #include <asm/proto.h>
 #include <asm/ds.h>
 
+#include "tls.h"
 
-/*
- * The maximal size of a BTS buffer per traced task in number of BTS
- * records.
- */
-#define PTRACE_BTS_BUFFER_MAX 4000
+enum x86_regset {
+       REGSET_GENERAL,
+       REGSET_FP,
+       REGSET_XFP,
+       REGSET_TLS,
+};
 
 /*
  * does not yet catch signals sent when the child dies.
@@ -99,9 +103,26 @@ static int set_segment_reg(struct task_struct *task,
        if (invalid_selector(value))
                return -EIO;
 
-       if (offset != offsetof(struct user_regs_struct, gs))
+       /*
+        * For %cs and %ss we cannot permit a null selector.
+        * We can permit a bogus selector as long as it has USER_RPL.
+        * Null selectors are fine for other segment registers, but
+        * we will never get back to user mode with invalid %cs or %ss
+        * and will take the trap in iret instead.  Much code relies
+        * on user_mode() to distinguish a user trap frame (which can
+        * safely use invalid selectors) from a kernel trap frame.
+        */
+       switch (offset) {
+       case offsetof(struct user_regs_struct, cs):
+       case offsetof(struct user_regs_struct, ss):
+               if (unlikely(value == 0))
+                       return -EIO;
+
+       default:
                *pt_regs_access(task_pt_regs(task), offset) = value;
-       else {
+               break;
+
+       case offsetof(struct user_regs_struct, gs):
                task->thread.gs = value;
                if (task == current)
                        /*
@@ -223,12 +244,16 @@ static int set_segment_reg(struct task_struct *task,
                 * Can't actually change these in 64-bit mode.
                 */
        case offsetof(struct user_regs_struct,cs):
+               if (unlikely(value == 0))
+                       return -EIO;
 #ifdef CONFIG_IA32_EMULATION
                if (test_tsk_thread_flag(task, TIF_IA32))
                        task_pt_regs(task)->cs = value;
 #endif
                break;
        case offsetof(struct user_regs_struct,ss):
+               if (unlikely(value == 0))
+                       return -EIO;
 #ifdef CONFIG_IA32_EMULATION
                if (test_tsk_thread_flag(task, TIF_IA32))
                        task_pt_regs(task)->ss = value;
@@ -374,6 +399,59 @@ static unsigned long getreg(struct task_struct *task, unsigned long offset)
        return *pt_regs_access(task_pt_regs(task), offset);
 }
 
+static int genregs_get(struct task_struct *target,
+                      const struct user_regset *regset,
+                      unsigned int pos, unsigned int count,
+                      void *kbuf, void __user *ubuf)
+{
+       if (kbuf) {
+               unsigned long *k = kbuf;
+               while (count > 0) {
+                       *k++ = getreg(target, pos);
+                       count -= sizeof(*k);
+                       pos += sizeof(*k);
+               }
+       } else {
+               unsigned long __user *u = ubuf;
+               while (count > 0) {
+                       if (__put_user(getreg(target, pos), u++))
+                               return -EFAULT;
+                       count -= sizeof(*u);
+                       pos += sizeof(*u);
+               }
+       }
+
+       return 0;
+}
+
+static int genregs_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 = 0;
+       if (kbuf) {
+               const unsigned long *k = kbuf;
+               while (count > 0 && !ret) {
+                       ret = putreg(target, pos, *k++);
+                       count -= sizeof(*k);
+                       pos += sizeof(*k);
+               }
+       } else {
+               const unsigned long  __user *u = ubuf;
+               while (count > 0 && !ret) {
+                       unsigned long word;
+                       ret = __get_user(word, u++);
+                       if (ret)
+                               break;
+                       ret = putreg(target, pos, word);
+                       count -= sizeof(*u);
+                       pos += sizeof(*u);
+               }
+       }
+       return ret;
+}
+
 /*
  * This function is trivial and will be inlined by the compiler.
  * Having it separates the implementation details of debug
@@ -466,20 +544,7 @@ static int ptrace_set_debugreg(struct task_struct *child,
        return 0;
 }
 
-static int ptrace_bts_max_buffer_size(void)
-{
-       return PTRACE_BTS_BUFFER_MAX;
-}
-
-static int ptrace_bts_get_buffer_size(struct task_struct *child)
-{
-       if (!child->thread.ds_area_msr)
-               return -ENXIO;
-
-       return ds_get_bts_size((void *)child->thread.ds_area_msr);
-}
-
-static int ptrace_bts_get_index(struct task_struct *child)
+static int ptrace_bts_get_size(struct task_struct *child)
 {
        if (!child->thread.ds_area_msr)
                return -ENXIO;
@@ -493,13 +558,28 @@ static int ptrace_bts_read_record(struct task_struct *child,
 {
        struct bts_struct ret;
        int retval;
+       int bts_end;
+       int bts_index;
 
        if (!child->thread.ds_area_msr)
                return -ENXIO;
 
+       if (index < 0)
+               return -EINVAL;
+
+       bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
+       if (bts_end <= index)
+               return -EINVAL;
+
+       /* translate the ptrace bts index into the ds bts index */
+       bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
+       bts_index -= (index + 1);
+       if (bts_index < 0)
+               bts_index += bts_end;
+
        retval = ds_read_bts((void *)child->thread.ds_area_msr,
-                            index, &ret);
-       if (retval)
+                            bts_index, &ret);
+       if (retval < 0)
                return retval;
 
        if (copy_to_user(out, &ret, sizeof(ret)))
@@ -523,92 +603,217 @@ static int ptrace_bts_write_record(struct task_struct *child,
        return sizeof(*in);
 }
 
-static int ptrace_bts_config(struct task_struct *child,
-                            unsigned long options)
+static int ptrace_bts_clear(struct task_struct *child)
 {
-       unsigned long debugctl_mask = ds_debugctl_mask();
-       int retval;
+       if (!child->thread.ds_area_msr)
+               return -ENXIO;
 
-       retval = ptrace_bts_get_buffer_size(child);
-       if (retval < 0)
-               return retval;
-       if (retval == 0)
+       return ds_clear((void *)child->thread.ds_area_msr);
+}
+
+static int ptrace_bts_drain(struct task_struct *child,
+                           long size,
+                           struct bts_struct __user *out)
+{
+       int end, i;
+       void *ds = (void *)child->thread.ds_area_msr;
+
+       if (!ds)
                return -ENXIO;
 
-       if (options & PTRACE_BTS_O_TRACE_TASK) {
-               child->thread.debugctlmsr |= debugctl_mask;
-               set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
-       } else {
-               /* there is no way for us to check whether we 'own'
-                * the respective bits in the DEBUGCTL MSR, we're
-                * about to clear */
-               child->thread.debugctlmsr &= ~debugctl_mask;
+       end = ds_get_bts_index(ds);
+       if (end <= 0)
+               return end;
 
-               if (!child->thread.debugctlmsr)
-                       clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
+       if (size < (end * sizeof(struct bts_struct)))
+               return -EIO;
+
+       for (i = 0; i < end; i++, out++) {
+               struct bts_struct ret;
+               int retval;
+
+               retval = ds_read_bts(ds, i, &ret);
+               if (retval < 0)
+                       return retval;
+
+               if (copy_to_user(out, &ret, sizeof(ret)))
+                       return -EFAULT;
        }
 
-       if (options & PTRACE_BTS_O_TIMESTAMPS)
-               set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
-       else
-               clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
+       ds_clear(ds);
 
-       return 0;
+       return end;
 }
 
-static int ptrace_bts_status(struct task_struct *child)
+static int ptrace_bts_realloc(struct task_struct *child,
+                             int size, int reduce_size)
 {
-       unsigned long debugctl_mask = ds_debugctl_mask();
-       int retval, status = 0;
+       unsigned long rlim, vm;
+       int ret, old_size;
 
-       retval = ptrace_bts_get_buffer_size(child);
-       if (retval < 0)
-               return retval;
-       if (retval == 0)
-               return -ENXIO;
+       if (size < 0)
+               return -EINVAL;
 
-       if (ptrace_bts_get_buffer_size(child) <= 0)
-               return -ENXIO;
+       old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
+       if (old_size < 0)
+               return old_size;
+
+       ret = ds_free((void **)&child->thread.ds_area_msr);
+       if (ret < 0)
+               goto out;
+
+       size >>= PAGE_SHIFT;
+       old_size >>= PAGE_SHIFT;
+
+       current->mm->total_vm  -= old_size;
+       current->mm->locked_vm -= old_size;
+
+       if (size == 0)
+               goto out;
+
+       rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
+       vm = current->mm->total_vm  + size;
+       if (rlim < vm) {
+               ret = -ENOMEM;
+
+               if (!reduce_size)
+                       goto out;
 
-       if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
-           child->thread.debugctlmsr & debugctl_mask)
-               status |= PTRACE_BTS_O_TRACE_TASK;
-       if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
-               status |= PTRACE_BTS_O_TIMESTAMPS;
+               size = rlim - current->mm->total_vm;
+               if (size <= 0)
+                       goto out;
+       }
+
+       rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
+       vm = current->mm->locked_vm  + size;
+       if (rlim < vm) {
+               ret = -ENOMEM;
+
+               if (!reduce_size)
+                       goto out;
+
+               size = rlim - current->mm->locked_vm;
+               if (size <= 0)
+                       goto out;
+       }
+
+       ret = ds_allocate((void **)&child->thread.ds_area_msr,
+                         size << PAGE_SHIFT);
+       if (ret < 0)
+               goto out;
+
+       current->mm->total_vm  += size;
+       current->mm->locked_vm += size;
+
+out:
+       if (child->thread.ds_area_msr)
+               set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
+       else
+               clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
 
-       return status;
+       return ret;
 }
 
-static int ptrace_bts_allocate_bts(struct task_struct *child,
-                                  int size_in_records)
+static int ptrace_bts_config(struct task_struct *child,
+                            long cfg_size,
+                            const struct ptrace_bts_config __user *ucfg)
 {
-       int retval = 0;
+       struct ptrace_bts_config cfg;
+       int bts_size, ret = 0;
        void *ds;
 
-       if (size_in_records < 0)
-               return -EINVAL;
+       if (cfg_size < sizeof(cfg))
+               return -EIO;
 
-       if (size_in_records > ptrace_bts_max_buffer_size())
+       if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
+               return -EFAULT;
+
+       if ((int)cfg.size < 0)
                return -EINVAL;
 
-       if (size_in_records == 0) {
-               ptrace_bts_config(child, /* options = */ 0);
-       } else {
-               retval = ds_allocate(&ds, size_in_records);
-               if (retval)
-                       return retval;
+       bts_size = 0;
+       ds = (void *)child->thread.ds_area_msr;
+       if (ds) {
+               bts_size = ds_get_bts_size(ds);
+               if (bts_size < 0)
+                       return bts_size;
        }
+       cfg.size = PAGE_ALIGN(cfg.size);
 
-       if (child->thread.ds_area_msr)
-               ds_free((void **)&child->thread.ds_area_msr);
+       if (bts_size != cfg.size) {
+               ret = ptrace_bts_realloc(child, cfg.size,
+                                        cfg.flags & PTRACE_BTS_O_CUT_SIZE);
+               if (ret < 0)
+                       goto errout;
 
-       child->thread.ds_area_msr = (unsigned long)ds;
-       if (child->thread.ds_area_msr)
-               set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
+               ds = (void *)child->thread.ds_area_msr;
+       }
+
+       if (cfg.flags & PTRACE_BTS_O_SIGNAL)
+               ret = ds_set_overflow(ds, DS_O_SIGNAL);
        else
-               clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
+               ret = ds_set_overflow(ds, DS_O_WRAP);
+       if (ret < 0)
+               goto errout;
 
-       return retval;
+       if (cfg.flags & PTRACE_BTS_O_TRACE)
+               child->thread.debugctlmsr |= ds_debugctl_mask();
+       else
+               child->thread.debugctlmsr &= ~ds_debugctl_mask();
+
+       if (cfg.flags & PTRACE_BTS_O_SCHED)
+               set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
+       else
+               clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
+
+       ret = sizeof(cfg);
+
+out:
+       if (child->thread.debugctlmsr)
+               set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
+       else
+               clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
+
+       return ret;
+
+errout:
+       child->thread.debugctlmsr &= ~ds_debugctl_mask();
+       clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
+       goto out;
+}
+
+static int ptrace_bts_status(struct task_struct *child,
+                            long cfg_size,
+                            struct ptrace_bts_config __user *ucfg)
+{
+       void *ds = (void *)child->thread.ds_area_msr;
+       struct ptrace_bts_config cfg;
+
+       if (cfg_size < sizeof(cfg))
+               return -EIO;
+
+       memset(&cfg, 0, sizeof(cfg));
+
+       if (ds) {
+               cfg.size = ds_get_bts_size(ds);
+
+               if (ds_get_overflow(ds) == DS_O_SIGNAL)
+                       cfg.flags |= PTRACE_BTS_O_SIGNAL;
+
+               if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
+                   child->thread.debugctlmsr & ds_debugctl_mask())
+                       cfg.flags |= PTRACE_BTS_O_TRACE;
+
+               if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
+                       cfg.flags |= PTRACE_BTS_O_SCHED;
+       }
+
+       cfg.bts_size = sizeof(struct bts_struct);
+
+       if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
+               return -EFAULT;
+
+       return sizeof(cfg);
 }
 
 void ptrace_bts_take_timestamp(struct task_struct *tsk,
@@ -616,12 +821,9 @@ void ptrace_bts_take_timestamp(struct task_struct *tsk,
 {
        struct bts_struct rec = {
                .qualifier = qualifier,
-               .variant.jiffies = jiffies
+               .variant.jiffies = jiffies_64
        };
 
-       if (ptrace_bts_get_buffer_size(tsk) <= 0)
-               return;
-
        ptrace_bts_write_record(tsk, &rec);
 }
 
@@ -636,25 +838,25 @@ void ptrace_disable(struct task_struct *child)
 #ifdef TIF_SYSCALL_EMU
        clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
 #endif
-       ptrace_bts_config(child, /* options = */ 0);
        if (child->thread.ds_area_msr) {
-           ds_free((void **)&child->thread.ds_area_msr);
-           clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
+               ptrace_bts_realloc(child, 0, 0);
+               child->thread.debugctlmsr &= ~ds_debugctl_mask();
+               if (!child->thread.debugctlmsr)
+                       clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
+               clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
        }
 }
 
+#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
+static const struct user_regset_view user_x86_32_view; /* Initialized below. */
+#endif
+
 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 {
-       int i, ret;
+       int ret;
        unsigned long __user *datap = (unsigned long __user *)data;
 
        switch (request) {
-       /* when I and D space are separate, these will need to be fixed. */
-       case PTRACE_PEEKTEXT: /* read word at location addr. */
-       case PTRACE_PEEKDATA:
-               ret = generic_ptrace_peekdata(child, addr, data);
-               break;
-
        /* read the word at location addr in the USER area. */
        case PTRACE_PEEKUSR: {
                unsigned long tmp;
@@ -676,12 +878,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                break;
        }
 
-       /* when I and D space are separate, this will have to be fixed. */
-       case PTRACE_POKETEXT: /* write the word at location addr. */
-       case PTRACE_POKEDATA:
-               ret = generic_ptrace_pokedata(child, addr, data);
-               break;
-
        case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
                ret = -EIO;
                if ((addr & (sizeof(data) - 1)) || addr < 0 ||
@@ -698,82 +894,46 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                }
                break;
 
-       case PTRACE_GETREGS: { /* Get all gp regs from the child. */
-               if (!access_ok(VERIFY_WRITE, datap, sizeof(struct user_regs_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
-                       __put_user(getreg(child, i), datap);
-                       datap++;
-               }
-               ret = 0;
-               break;
-       }
-
-       case PTRACE_SETREGS: { /* Set all gp regs in the child. */
-               unsigned long tmp;
-               if (!access_ok(VERIFY_READ, datap, sizeof(struct user_regs_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               for (i = 0; i < sizeof(struct user_regs_struct); i += sizeof(long)) {
-                       __get_user(tmp, datap);
-                       putreg(child, i, tmp);
-                       datap++;
-               }
-               ret = 0;
-               break;
-       }
-
-       case PTRACE_GETFPREGS: { /* Get the child FPU state. */
-               if (!access_ok(VERIFY_WRITE, datap,
-                              sizeof(struct user_i387_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               ret = 0;
-               if (!tsk_used_math(child))
-                       init_fpu(child);
-               get_fpregs((struct user_i387_struct __user *)data, child);
-               break;
-       }
-
-       case PTRACE_SETFPREGS: { /* Set the child FPU state. */
-               if (!access_ok(VERIFY_READ, datap,
-                              sizeof(struct user_i387_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               set_stopped_child_used_math(child);
-               set_fpregs(child, (struct user_i387_struct __user *)data);
-               ret = 0;
-               break;
-       }
+       case PTRACE_GETREGS:    /* Get all gp regs from the child. */
+               return copy_regset_to_user(child,
+                                          task_user_regset_view(current),
+                                          REGSET_GENERAL,
+                                          0, sizeof(struct user_regs_struct),
+                                          datap);
+
+       case PTRACE_SETREGS:    /* Set all gp regs in the child. */
+               return copy_regset_from_user(child,
+                                            task_user_regset_view(current),
+                                            REGSET_GENERAL,
+                                            0, sizeof(struct user_regs_struct),
+                                            datap);
+
+       case PTRACE_GETFPREGS:  /* Get the child FPU state. */
+               return copy_regset_to_user(child,
+                                          task_user_regset_view(current),
+                                          REGSET_FP,
+                                          0, sizeof(struct user_i387_struct),
+                                          datap);
+
+       case PTRACE_SETFPREGS:  /* Set the child FPU state. */
+               return copy_regset_from_user(child,
+                                            task_user_regset_view(current),
+                                            REGSET_FP,
+                                            0, sizeof(struct user_i387_struct),
+                                            datap);
 
 #ifdef CONFIG_X86_32
-       case PTRACE_GETFPXREGS: { /* Get the child extended FPU state. */
-               if (!access_ok(VERIFY_WRITE, datap,
-                              sizeof(struct user_fxsr_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               if (!tsk_used_math(child))
-                       init_fpu(child);
-               ret = get_fpxregs((struct user_fxsr_struct __user *)data, child);
-               break;
-       }
-
-       case PTRACE_SETFPXREGS: { /* Set the child extended FPU state. */
-               if (!access_ok(VERIFY_READ, datap,
-                              sizeof(struct user_fxsr_struct))) {
-                       ret = -EIO;
-                       break;
-               }
-               set_stopped_child_used_math(child);
-               ret = set_fpxregs(child, (struct user_fxsr_struct __user *)data);
-               break;
-       }
+       case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
+               return copy_regset_to_user(child, &user_x86_32_view,
+                                          REGSET_XFP,
+                                          0, sizeof(struct user_fxsr_struct),
+                                          datap);
+
+       case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
+               return copy_regset_from_user(child, &user_x86_32_view,
+                                            REGSET_XFP,
+                                            0, sizeof(struct user_fxsr_struct),
+                                            datap);
 #endif
 
 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
@@ -801,34 +961,32 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
                break;
 #endif
 
-       case PTRACE_BTS_MAX_BUFFER_SIZE:
-               ret = ptrace_bts_max_buffer_size();
-               break;
-
-       case PTRACE_BTS_ALLOCATE_BUFFER:
-               ret = ptrace_bts_allocate_bts(child, data);
+       case PTRACE_BTS_CONFIG:
+               ret = ptrace_bts_config
+                       (child, data, (struct ptrace_bts_config __user *)addr);
                break;
 
-       case PTRACE_BTS_GET_BUFFER_SIZE:
-               ret = ptrace_bts_get_buffer_size(child);
+       case PTRACE_BTS_STATUS:
+               ret = ptrace_bts_status
+                       (child, data, (struct ptrace_bts_config __user *)addr);
                break;
 
-       case PTRACE_BTS_GET_INDEX:
-               ret = ptrace_bts_get_index(child);
+       case PTRACE_BTS_SIZE:
+               ret = ptrace_bts_get_size(child);
                break;
 
-       case PTRACE_BTS_READ_RECORD:
+       case PTRACE_BTS_GET:
                ret = ptrace_bts_read_record
-                       (child, data,
-                        (struct bts_struct __user *) addr);
+                       (child, data, (struct bts_struct __user *) addr);
                break;
 
-       case PTRACE_BTS_CONFIG:
-               ret = ptrace_bts_config(child, data);
+       case PTRACE_BTS_CLEAR:
+               ret = ptrace_bts_clear(child);
                break;
 
-       case PTRACE_BTS_STATUS:
-               ret = ptrace_bts_status(child);
+       case PTRACE_BTS_DRAIN:
+               ret = ptrace_bts_drain
+                       (child, data, (struct bts_struct __user *) addr);
                break;
 
        default:
@@ -844,7 +1002,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 #include <linux/compat.h>
 #include <linux/syscalls.h>
 #include <asm/ia32.h>
-#include <asm/fpu32.h>
 #include <asm/user32.h>
 
 #define R32(l,q)                                                       \
@@ -967,6 +1124,61 @@ static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
 #undef R32
 #undef SEG32
 
+static int genregs32_get(struct task_struct *target,
+                        const struct user_regset *regset,
+                        unsigned int pos, unsigned int count,
+                        void *kbuf, void __user *ubuf)
+{
+       if (kbuf) {
+               compat_ulong_t *k = kbuf;
+               while (count > 0) {
+                       getreg32(target, pos, k++);
+                       count -= sizeof(*k);
+                       pos += sizeof(*k);
+               }
+       } else {
+               compat_ulong_t __user *u = ubuf;
+               while (count > 0) {
+                       compat_ulong_t word;
+                       getreg32(target, pos, &word);
+                       if (__put_user(word, u++))
+                               return -EFAULT;
+                       count -= sizeof(*u);
+                       pos += sizeof(*u);
+               }
+       }
+
+       return 0;
+}
+
+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)
+{
+       int ret = 0;
+       if (kbuf) {
+               const compat_ulong_t *k = kbuf;
+               while (count > 0 && !ret) {
+                       ret = putreg(target, pos, *k++);
+                       count -= sizeof(*k);
+                       pos += sizeof(*k);
+               }
+       } else {
+               const compat_ulong_t __user *u = ubuf;
+               while (count > 0 && !ret) {
+                       compat_ulong_t word;
+                       ret = __get_user(word, u++);
+                       if (ret)
+                               break;
+                       ret = putreg(target, pos, word);
+                       count -= sizeof(*u);
+                       pos += sizeof(*u);
+               }
+       }
+       return ret;
+}
+
 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
 {
        siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
@@ -1014,13 +1226,12 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
        case PTRACE_SETOPTIONS:
        case PTRACE_SET_THREAD_AREA:
        case PTRACE_GET_THREAD_AREA:
-       case PTRACE_BTS_MAX_BUFFER_SIZE:
-       case PTRACE_BTS_ALLOCATE_BUFFER:
-       case PTRACE_BTS_GET_BUFFER_SIZE:
-       case PTRACE_BTS_GET_INDEX:
-       case PTRACE_BTS_READ_RECORD:
        case PTRACE_BTS_CONFIG:
        case PTRACE_BTS_STATUS:
+       case PTRACE_BTS_SIZE:
+       case PTRACE_BTS_GET:
+       case PTRACE_BTS_CLEAR:
+       case PTRACE_BTS_DRAIN:
                return sys_ptrace(request, pid, addr, data);
 
        default:
@@ -1057,24 +1268,6 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
        childregs = task_pt_regs(child);
 
        switch (request) {
-       case PTRACE_PEEKDATA:
-       case PTRACE_PEEKTEXT:
-               ret = 0;
-               if (access_process_vm(child, addr, &val, sizeof(u32), 0) !=
-                   sizeof(u32))
-                       ret = -EIO;
-               else
-                       ret = put_user(val, (unsigned int __user *)datap);
-               break;
-
-       case PTRACE_POKEDATA:
-       case PTRACE_POKETEXT:
-               ret = 0;
-               if (access_process_vm(child, addr, &data, sizeof(u32), 1) !=
-                   sizeof(u32))
-                       ret = -EIO;
-               break;
-
        case PTRACE_PEEKUSR:
                ret = getreg32(child, addr, &val);
                if (ret == 0)
@@ -1085,98 +1278,43 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
                ret = putreg32(child, addr, data);
                break;
 
-       case PTRACE_GETREGS: { /* Get all gp regs from the child. */
-               int i;
-
-               if (!access_ok(VERIFY_WRITE, datap, 16*4)) {
-                       ret = -EIO;
-                       break;
-               }
-               ret = 0;
-               for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(__u32)) {
-                       getreg32(child, i, &val);
-                       ret |= __put_user(val, (u32 __user *)datap);
-                       datap += sizeof(u32);
-               }
-               break;
-       }
-
-       case PTRACE_SETREGS: { /* Set all gp regs in the child. */
-               unsigned long tmp;
-               int i;
-
-               if (!access_ok(VERIFY_READ, datap, 16*4)) {
-                       ret = -EIO;
-                       break;
-               }
-               ret = 0;
-               for (i = 0; i < sizeof(struct user_regs_struct32); i += sizeof(u32)) {
-                       ret |= __get_user(tmp, (u32 __user *)datap);
-                       putreg32(child, i, tmp);
-                       datap += sizeof(u32);
-               }
-               break;
-       }
-
-       case PTRACE_GETFPREGS:
-               ret = -EIO;
-               if (!access_ok(VERIFY_READ, compat_ptr(data),
-                              sizeof(struct user_i387_struct)))
-                       break;
-               save_i387_ia32(child, datap, childregs, 1);
-               ret = 0;
-                       break;
-
-       case PTRACE_SETFPREGS:
-               ret = -EIO;
-               if (!access_ok(VERIFY_WRITE, datap,
-                              sizeof(struct user_i387_struct)))
-                       break;
-               ret = 0;
-               /* don't check EFAULT to be bug-to-bug compatible to i386 */
-               restore_i387_ia32(child, datap, 1);
-               break;
-
-       case PTRACE_GETFPXREGS: {
-               struct user32_fxsr_struct __user *u = datap;
-
-               init_fpu(child);
-               ret = -EIO;
-               if (!access_ok(VERIFY_WRITE, u, sizeof(*u)))
-                       break;
-                       ret = -EFAULT;
-               if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u)))
-                       break;
-               ret = __put_user(childregs->cs, &u->fcs);
-               ret |= __put_user(child->thread.ds, &u->fos);
-               break;
-       }
-       case PTRACE_SETFPXREGS: {
-               struct user32_fxsr_struct __user *u = datap;
-
-               unlazy_fpu(child);
-               ret = -EIO;
-               if (!access_ok(VERIFY_READ, u, sizeof(*u)))
-                       break;
-               /*
-                * no checking to be bug-to-bug compatible with i386.
-                * but silence warning
-                */
-               if (__copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)))
-                       ;
-               set_stopped_child_used_math(child);
-               child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask;
-               ret = 0;
-               break;
-       }
-
-       case PTRACE_GETEVENTMSG:
-               ret = put_user(child->ptrace_message,
-                              (unsigned int __user *)compat_ptr(data));
-               break;
+       case PTRACE_GETREGS:    /* Get all gp regs from the child. */
+               return copy_regset_to_user(child, &user_x86_32_view,
+                                          REGSET_GENERAL,
+                                          0, sizeof(struct user_regs_struct32),
+                                          datap);
+
+       case PTRACE_SETREGS:    /* Set all gp regs in the child. */
+               return copy_regset_from_user(child, &user_x86_32_view,
+                                            REGSET_GENERAL, 0,
+                                            sizeof(struct user_regs_struct32),
+                                            datap);
+
+       case PTRACE_GETFPREGS:  /* Get the child FPU state. */
+               return copy_regset_to_user(child, &user_x86_32_view,
+                                          REGSET_FP, 0,
+                                          sizeof(struct user_i387_ia32_struct),
+                                          datap);
+
+       case PTRACE_SETFPREGS:  /* Set the child FPU state. */
+               return copy_regset_from_user(
+                       child, &user_x86_32_view, REGSET_FP,
+                       0, sizeof(struct user_i387_ia32_struct), datap);
+
+       case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
+               return copy_regset_to_user(child, &user_x86_32_view,
+                                          REGSET_XFP, 0,
+                                          sizeof(struct user32_fxsr_struct),
+                                          datap);
+
+       case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
+               return copy_regset_from_user(child, &user_x86_32_view,
+                                            REGSET_XFP, 0,
+                                            sizeof(struct user32_fxsr_struct),
+                                            datap);
 
        default:
-               BUG();
+               return compat_ptrace_request(child, request, addr, data);
        }
 
  out:
@@ -1186,6 +1324,85 @@ asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
 
 #endif /* CONFIG_IA32_EMULATION */
 
+#ifdef CONFIG_X86_64
+
+static const struct user_regset x86_64_regsets[] = {
+       [REGSET_GENERAL] = {
+               .core_note_type = NT_PRSTATUS,
+               .n = sizeof(struct user_regs_struct) / sizeof(long),
+               .size = sizeof(long), .align = sizeof(long),
+               .get = genregs_get, .set = genregs_set
+       },
+       [REGSET_FP] = {
+               .core_note_type = NT_PRFPREG,
+               .n = sizeof(struct user_i387_struct) / sizeof(long),
+               .size = sizeof(long), .align = sizeof(long),
+               .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
+       },
+};
+
+static const struct user_regset_view user_x86_64_view = {
+       .name = "x86_64", .e_machine = EM_X86_64,
+       .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
+};
+
+#else  /* CONFIG_X86_32 */
+
+#define user_regs_struct32     user_regs_struct
+#define genregs32_get          genregs_get
+#define genregs32_set          genregs_set
+
+#endif /* CONFIG_X86_64 */
+
+#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
+static const struct user_regset x86_32_regsets[] = {
+       [REGSET_GENERAL] = {
+               .core_note_type = NT_PRSTATUS,
+               .n = sizeof(struct user_regs_struct32) / sizeof(u32),
+               .size = sizeof(u32), .align = sizeof(u32),
+               .get = genregs32_get, .set = genregs32_set
+       },
+       [REGSET_FP] = {
+               .core_note_type = NT_PRFPREG,
+               .n = sizeof(struct user_i387_struct) / sizeof(u32),
+               .size = sizeof(u32), .align = sizeof(u32),
+               .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
+       },
+       [REGSET_XFP] = {
+               .core_note_type = NT_PRXFPREG,
+               .n = sizeof(struct user_i387_struct) / sizeof(u32),
+               .size = sizeof(u32), .align = sizeof(u32),
+               .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
+       },
+       [REGSET_TLS] = {
+               .core_note_type = NT_386_TLS,
+               .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
+               .size = sizeof(struct user_desc),
+               .align = sizeof(struct user_desc),
+               .active = regset_tls_active,
+               .get = regset_tls_get, .set = regset_tls_set
+       },
+};
+
+static const struct user_regset_view user_x86_32_view = {
+       .name = "i386", .e_machine = EM_386,
+       .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
+};
+#endif
+
+const struct user_regset_view *task_user_regset_view(struct task_struct *task)
+{
+#ifdef CONFIG_IA32_EMULATION
+       if (test_tsk_thread_flag(task, TIF_IA32))
+#endif
+#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
+               return &user_x86_32_view;
+#endif
+#ifdef CONFIG_X86_64
+       return &user_x86_64_view;
+#endif
+}
+
 #ifdef CONFIG_X86_32
 
 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)