x86: Mark ptrace_get_debugreg() as static
[linux-2.6-block.git] / arch / x86 / kernel / ptrace.c
1 /* By Ross Biro 1/23/92 */
2 /*
3  * Pentium III FXSR, SSE support
4  *      Gareth Hughes <gareth@valinux.com>, May 2000
5  *
6  * BTS tracing
7  *      Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/signal.h>
24 #include <linux/workqueue.h>
25
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/processor.h>
30 #include <asm/i387.h>
31 #include <asm/debugreg.h>
32 #include <asm/ldt.h>
33 #include <asm/desc.h>
34 #include <asm/prctl.h>
35 #include <asm/proto.h>
36 #include <asm/ds.h>
37 #include <asm/hw_breakpoint.h>
38
39 #include <trace/syscall.h>
40
41 #include "tls.h"
42
43 enum x86_regset {
44         REGSET_GENERAL,
45         REGSET_FP,
46         REGSET_XFP,
47         REGSET_IOPERM64 = REGSET_XFP,
48         REGSET_TLS,
49         REGSET_IOPERM32,
50 };
51
52 /*
53  * does not yet catch signals sent when the child dies.
54  * in exit.c or in signal.c.
55  */
56
57 /*
58  * Determines which flags the user has access to [1 = access, 0 = no access].
59  */
60 #define FLAG_MASK_32            ((unsigned long)                        \
61                                  (X86_EFLAGS_CF | X86_EFLAGS_PF |       \
62                                   X86_EFLAGS_AF | X86_EFLAGS_ZF |       \
63                                   X86_EFLAGS_SF | X86_EFLAGS_TF |       \
64                                   X86_EFLAGS_DF | X86_EFLAGS_OF |       \
65                                   X86_EFLAGS_RF | X86_EFLAGS_AC))
66
67 /*
68  * Determines whether a value may be installed in a segment register.
69  */
70 static inline bool invalid_selector(u16 value)
71 {
72         return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
73 }
74
75 #ifdef CONFIG_X86_32
76
77 #define FLAG_MASK               FLAG_MASK_32
78
79 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
80 {
81         BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
82         return &regs->bx + (regno >> 2);
83 }
84
85 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
86 {
87         /*
88          * Returning the value truncates it to 16 bits.
89          */
90         unsigned int retval;
91         if (offset != offsetof(struct user_regs_struct, gs))
92                 retval = *pt_regs_access(task_pt_regs(task), offset);
93         else {
94                 if (task == current)
95                         retval = get_user_gs(task_pt_regs(task));
96                 else
97                         retval = task_user_gs(task);
98         }
99         return retval;
100 }
101
102 static int set_segment_reg(struct task_struct *task,
103                            unsigned long offset, u16 value)
104 {
105         /*
106          * The value argument was already truncated to 16 bits.
107          */
108         if (invalid_selector(value))
109                 return -EIO;
110
111         /*
112          * For %cs and %ss we cannot permit a null selector.
113          * We can permit a bogus selector as long as it has USER_RPL.
114          * Null selectors are fine for other segment registers, but
115          * we will never get back to user mode with invalid %cs or %ss
116          * and will take the trap in iret instead.  Much code relies
117          * on user_mode() to distinguish a user trap frame (which can
118          * safely use invalid selectors) from a kernel trap frame.
119          */
120         switch (offset) {
121         case offsetof(struct user_regs_struct, cs):
122         case offsetof(struct user_regs_struct, ss):
123                 if (unlikely(value == 0))
124                         return -EIO;
125
126         default:
127                 *pt_regs_access(task_pt_regs(task), offset) = value;
128                 break;
129
130         case offsetof(struct user_regs_struct, gs):
131                 if (task == current)
132                         set_user_gs(task_pt_regs(task), value);
133                 else
134                         task_user_gs(task) = value;
135         }
136
137         return 0;
138 }
139
140 #else  /* CONFIG_X86_64 */
141
142 #define FLAG_MASK               (FLAG_MASK_32 | X86_EFLAGS_NT)
143
144 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
145 {
146         BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
147         return &regs->r15 + (offset / sizeof(regs->r15));
148 }
149
150 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
151 {
152         /*
153          * Returning the value truncates it to 16 bits.
154          */
155         unsigned int seg;
156
157         switch (offset) {
158         case offsetof(struct user_regs_struct, fs):
159                 if (task == current) {
160                         /* Older gas can't assemble movq %?s,%r?? */
161                         asm("movl %%fs,%0" : "=r" (seg));
162                         return seg;
163                 }
164                 return task->thread.fsindex;
165         case offsetof(struct user_regs_struct, gs):
166                 if (task == current) {
167                         asm("movl %%gs,%0" : "=r" (seg));
168                         return seg;
169                 }
170                 return task->thread.gsindex;
171         case offsetof(struct user_regs_struct, ds):
172                 if (task == current) {
173                         asm("movl %%ds,%0" : "=r" (seg));
174                         return seg;
175                 }
176                 return task->thread.ds;
177         case offsetof(struct user_regs_struct, es):
178                 if (task == current) {
179                         asm("movl %%es,%0" : "=r" (seg));
180                         return seg;
181                 }
182                 return task->thread.es;
183
184         case offsetof(struct user_regs_struct, cs):
185         case offsetof(struct user_regs_struct, ss):
186                 break;
187         }
188         return *pt_regs_access(task_pt_regs(task), offset);
189 }
190
191 static int set_segment_reg(struct task_struct *task,
192                            unsigned long offset, u16 value)
193 {
194         /*
195          * The value argument was already truncated to 16 bits.
196          */
197         if (invalid_selector(value))
198                 return -EIO;
199
200         switch (offset) {
201         case offsetof(struct user_regs_struct,fs):
202                 /*
203                  * If this is setting fs as for normal 64-bit use but
204                  * setting fs_base has implicitly changed it, leave it.
205                  */
206                 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
207                      task->thread.fs != 0) ||
208                     (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
209                      task->thread.fs == 0))
210                         break;
211                 task->thread.fsindex = value;
212                 if (task == current)
213                         loadsegment(fs, task->thread.fsindex);
214                 break;
215         case offsetof(struct user_regs_struct,gs):
216                 /*
217                  * If this is setting gs as for normal 64-bit use but
218                  * setting gs_base has implicitly changed it, leave it.
219                  */
220                 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
221                      task->thread.gs != 0) ||
222                     (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
223                      task->thread.gs == 0))
224                         break;
225                 task->thread.gsindex = value;
226                 if (task == current)
227                         load_gs_index(task->thread.gsindex);
228                 break;
229         case offsetof(struct user_regs_struct,ds):
230                 task->thread.ds = value;
231                 if (task == current)
232                         loadsegment(ds, task->thread.ds);
233                 break;
234         case offsetof(struct user_regs_struct,es):
235                 task->thread.es = value;
236                 if (task == current)
237                         loadsegment(es, task->thread.es);
238                 break;
239
240                 /*
241                  * Can't actually change these in 64-bit mode.
242                  */
243         case offsetof(struct user_regs_struct,cs):
244                 if (unlikely(value == 0))
245                         return -EIO;
246 #ifdef CONFIG_IA32_EMULATION
247                 if (test_tsk_thread_flag(task, TIF_IA32))
248                         task_pt_regs(task)->cs = value;
249 #endif
250                 break;
251         case offsetof(struct user_regs_struct,ss):
252                 if (unlikely(value == 0))
253                         return -EIO;
254 #ifdef CONFIG_IA32_EMULATION
255                 if (test_tsk_thread_flag(task, TIF_IA32))
256                         task_pt_regs(task)->ss = value;
257 #endif
258                 break;
259         }
260
261         return 0;
262 }
263
264 #endif  /* CONFIG_X86_32 */
265
266 static unsigned long get_flags(struct task_struct *task)
267 {
268         unsigned long retval = task_pt_regs(task)->flags;
269
270         /*
271          * If the debugger set TF, hide it from the readout.
272          */
273         if (test_tsk_thread_flag(task, TIF_FORCED_TF))
274                 retval &= ~X86_EFLAGS_TF;
275
276         return retval;
277 }
278
279 static int set_flags(struct task_struct *task, unsigned long value)
280 {
281         struct pt_regs *regs = task_pt_regs(task);
282
283         /*
284          * If the user value contains TF, mark that
285          * it was not "us" (the debugger) that set it.
286          * If not, make sure it stays set if we had.
287          */
288         if (value & X86_EFLAGS_TF)
289                 clear_tsk_thread_flag(task, TIF_FORCED_TF);
290         else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
291                 value |= X86_EFLAGS_TF;
292
293         regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
294
295         return 0;
296 }
297
298 static int putreg(struct task_struct *child,
299                   unsigned long offset, unsigned long value)
300 {
301         switch (offset) {
302         case offsetof(struct user_regs_struct, cs):
303         case offsetof(struct user_regs_struct, ds):
304         case offsetof(struct user_regs_struct, es):
305         case offsetof(struct user_regs_struct, fs):
306         case offsetof(struct user_regs_struct, gs):
307         case offsetof(struct user_regs_struct, ss):
308                 return set_segment_reg(child, offset, value);
309
310         case offsetof(struct user_regs_struct, flags):
311                 return set_flags(child, value);
312
313 #ifdef CONFIG_X86_64
314         /*
315          * Orig_ax is really just a flag with small positive and
316          * negative values, so make sure to always sign-extend it
317          * from 32 bits so that it works correctly regardless of
318          * whether we come from a 32-bit environment or not.
319          */
320         case offsetof(struct user_regs_struct, orig_ax):
321                 value = (long) (s32) value;
322                 break;
323
324         case offsetof(struct user_regs_struct,fs_base):
325                 if (value >= TASK_SIZE_OF(child))
326                         return -EIO;
327                 /*
328                  * When changing the segment base, use do_arch_prctl
329                  * to set either thread.fs or thread.fsindex and the
330                  * corresponding GDT slot.
331                  */
332                 if (child->thread.fs != value)
333                         return do_arch_prctl(child, ARCH_SET_FS, value);
334                 return 0;
335         case offsetof(struct user_regs_struct,gs_base):
336                 /*
337                  * Exactly the same here as the %fs handling above.
338                  */
339                 if (value >= TASK_SIZE_OF(child))
340                         return -EIO;
341                 if (child->thread.gs != value)
342                         return do_arch_prctl(child, ARCH_SET_GS, value);
343                 return 0;
344 #endif
345         }
346
347         *pt_regs_access(task_pt_regs(child), offset) = value;
348         return 0;
349 }
350
351 static unsigned long getreg(struct task_struct *task, unsigned long offset)
352 {
353         switch (offset) {
354         case offsetof(struct user_regs_struct, cs):
355         case offsetof(struct user_regs_struct, ds):
356         case offsetof(struct user_regs_struct, es):
357         case offsetof(struct user_regs_struct, fs):
358         case offsetof(struct user_regs_struct, gs):
359         case offsetof(struct user_regs_struct, ss):
360                 return get_segment_reg(task, offset);
361
362         case offsetof(struct user_regs_struct, flags):
363                 return get_flags(task);
364
365 #ifdef CONFIG_X86_64
366         case offsetof(struct user_regs_struct, fs_base): {
367                 /*
368                  * do_arch_prctl may have used a GDT slot instead of
369                  * the MSR.  To userland, it appears the same either
370                  * way, except the %fs segment selector might not be 0.
371                  */
372                 unsigned int seg = task->thread.fsindex;
373                 if (task->thread.fs != 0)
374                         return task->thread.fs;
375                 if (task == current)
376                         asm("movl %%fs,%0" : "=r" (seg));
377                 if (seg != FS_TLS_SEL)
378                         return 0;
379                 return get_desc_base(&task->thread.tls_array[FS_TLS]);
380         }
381         case offsetof(struct user_regs_struct, gs_base): {
382                 /*
383                  * Exactly the same here as the %fs handling above.
384                  */
385                 unsigned int seg = task->thread.gsindex;
386                 if (task->thread.gs != 0)
387                         return task->thread.gs;
388                 if (task == current)
389                         asm("movl %%gs,%0" : "=r" (seg));
390                 if (seg != GS_TLS_SEL)
391                         return 0;
392                 return get_desc_base(&task->thread.tls_array[GS_TLS]);
393         }
394 #endif
395         }
396
397         return *pt_regs_access(task_pt_regs(task), offset);
398 }
399
400 static int genregs_get(struct task_struct *target,
401                        const struct user_regset *regset,
402                        unsigned int pos, unsigned int count,
403                        void *kbuf, void __user *ubuf)
404 {
405         if (kbuf) {
406                 unsigned long *k = kbuf;
407                 while (count > 0) {
408                         *k++ = getreg(target, pos);
409                         count -= sizeof(*k);
410                         pos += sizeof(*k);
411                 }
412         } else {
413                 unsigned long __user *u = ubuf;
414                 while (count > 0) {
415                         if (__put_user(getreg(target, pos), u++))
416                                 return -EFAULT;
417                         count -= sizeof(*u);
418                         pos += sizeof(*u);
419                 }
420         }
421
422         return 0;
423 }
424
425 static int genregs_set(struct task_struct *target,
426                        const struct user_regset *regset,
427                        unsigned int pos, unsigned int count,
428                        const void *kbuf, const void __user *ubuf)
429 {
430         int ret = 0;
431         if (kbuf) {
432                 const unsigned long *k = kbuf;
433                 while (count > 0 && !ret) {
434                         ret = putreg(target, pos, *k++);
435                         count -= sizeof(*k);
436                         pos += sizeof(*k);
437                 }
438         } else {
439                 const unsigned long  __user *u = ubuf;
440                 while (count > 0 && !ret) {
441                         unsigned long word;
442                         ret = __get_user(word, u++);
443                         if (ret)
444                                 break;
445                         ret = putreg(target, pos, word);
446                         count -= sizeof(*u);
447                         pos += sizeof(*u);
448                 }
449         }
450         return ret;
451 }
452
453 /*
454  * Decode the length and type bits for a particular breakpoint as
455  * stored in debug register 7.  Return the "enabled" status.
456  */
457 static int decode_dr7(unsigned long dr7, int bpnum, unsigned *len,
458                 unsigned *type)
459 {
460         int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
461
462         *len = (bp_info & 0xc) | 0x40;
463         *type = (bp_info & 0x3) | 0x80;
464         return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
465 }
466
467 static void ptrace_triggered(struct hw_breakpoint *bp, struct pt_regs *regs)
468 {
469         struct thread_struct *thread = &(current->thread);
470         int i;
471
472         /*
473          * Store in the virtual DR6 register the fact that the breakpoint
474          * was hit so the thread's debugger will see it.
475          */
476         for (i = 0; i < hbp_kernel_pos; i++)
477                 /*
478                  * We will check bp->info.address against the address stored in
479                  * thread's hbp structure and not debugreg[i]. This is to ensure
480                  * that the corresponding bit for 'i' in DR7 register is enabled
481                  */
482                 if (bp->info.address == thread->hbp[i]->info.address)
483                         break;
484
485         thread->debugreg6 |= (DR_TRAP0 << i);
486 }
487
488 /*
489  * Handle ptrace writes to debug register 7.
490  */
491 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
492 {
493         struct thread_struct *thread = &(tsk->thread);
494         unsigned long old_dr7 = thread->debugreg7;
495         int i, orig_ret = 0, rc = 0;
496         int enabled, second_pass = 0;
497         unsigned len, type;
498         struct hw_breakpoint *bp;
499
500         data &= ~DR_CONTROL_RESERVED;
501 restore:
502         /*
503          * Loop through all the hardware breakpoints, making the
504          * appropriate changes to each.
505          */
506         for (i = 0; i < HBP_NUM; i++) {
507                 enabled = decode_dr7(data, i, &len, &type);
508                 bp = thread->hbp[i];
509
510                 if (!enabled) {
511                         if (bp) {
512                                 /* Don't unregister the breakpoints right-away,
513                                  * unless all register_user_hw_breakpoint()
514                                  * requests have succeeded. This prevents
515                                  * any window of opportunity for debug
516                                  * register grabbing by other users.
517                                  */
518                                 if (!second_pass)
519                                         continue;
520                                 unregister_user_hw_breakpoint(tsk, bp);
521                                 kfree(bp);
522                         }
523                         continue;
524                 }
525                 if (!bp) {
526                         rc = -ENOMEM;
527                         bp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
528                         if (bp) {
529                                 bp->info.address = thread->debugreg[i];
530                                 bp->triggered = ptrace_triggered;
531                                 bp->info.len = len;
532                                 bp->info.type = type;
533                                 rc = register_user_hw_breakpoint(tsk, bp);
534                                 if (rc)
535                                         kfree(bp);
536                         }
537                 } else
538                         rc = modify_user_hw_breakpoint(tsk, bp);
539                 if (rc)
540                         break;
541         }
542         /*
543          * Make a second pass to free the remaining unused breakpoints
544          * or to restore the original breakpoints if an error occurred.
545          */
546         if (!second_pass) {
547                 second_pass = 1;
548                 if (rc < 0) {
549                         orig_ret = rc;
550                         data = old_dr7;
551                 }
552                 goto restore;
553         }
554         return ((orig_ret < 0) ? orig_ret : rc);
555 }
556
557 /*
558  * Handle PTRACE_PEEKUSR calls for the debug register area.
559  */
560 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
561 {
562         struct thread_struct *thread = &(tsk->thread);
563         unsigned long val = 0;
564
565         if (n < HBP_NUM)
566                 val = thread->debugreg[n];
567         else if (n == 6)
568                 val = thread->debugreg6;
569         else if (n == 7)
570                 val = thread->debugreg7;
571         return val;
572 }
573
574 /*
575  * Handle PTRACE_POKEUSR calls for the debug register area.
576  */
577 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
578 {
579         struct thread_struct *thread = &(tsk->thread);
580         int rc = 0;
581
582         /* There are no DR4 or DR5 registers */
583         if (n == 4 || n == 5)
584                 return -EIO;
585
586         if (n == 6) {
587                 tsk->thread.debugreg6 = val;
588                 goto ret_path;
589         }
590         if (n < HBP_NUM) {
591                 if (thread->hbp[n]) {
592                         if (arch_check_va_in_userspace(val,
593                                         thread->hbp[n]->info.len) == 0) {
594                                 rc = -EIO;
595                                 goto ret_path;
596                         }
597                         thread->hbp[n]->info.address = val;
598                 }
599                 thread->debugreg[n] = val;
600         }
601         /* All that's left is DR7 */
602         if (n == 7)
603                 rc = ptrace_write_dr7(tsk, val);
604
605 ret_path:
606         return rc;
607 }
608
609 /*
610  * These access the current or another (stopped) task's io permission
611  * bitmap for debugging or core dump.
612  */
613 static int ioperm_active(struct task_struct *target,
614                          const struct user_regset *regset)
615 {
616         return target->thread.io_bitmap_max / regset->size;
617 }
618
619 static int ioperm_get(struct task_struct *target,
620                       const struct user_regset *regset,
621                       unsigned int pos, unsigned int count,
622                       void *kbuf, void __user *ubuf)
623 {
624         if (!target->thread.io_bitmap_ptr)
625                 return -ENXIO;
626
627         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
628                                    target->thread.io_bitmap_ptr,
629                                    0, IO_BITMAP_BYTES);
630 }
631
632 #ifdef CONFIG_X86_PTRACE_BTS
633 /*
634  * A branch trace store context.
635  *
636  * Contexts may only be installed by ptrace_bts_config() and only for
637  * ptraced tasks.
638  *
639  * Contexts are destroyed when the tracee is detached from the tracer.
640  * The actual destruction work requires interrupts enabled, so the
641  * work is deferred and will be scheduled during __ptrace_unlink().
642  *
643  * Contexts hold an additional task_struct reference on the traced
644  * task, as well as a reference on the tracer's mm.
645  *
646  * Ptrace already holds a task_struct for the duration of ptrace operations,
647  * but since destruction is deferred, it may be executed after both
648  * tracer and tracee exited.
649  */
650 struct bts_context {
651         /* The branch trace handle. */
652         struct bts_tracer       *tracer;
653
654         /* The buffer used to store the branch trace and its size. */
655         void                    *buffer;
656         unsigned int            size;
657
658         /* The mm that paid for the above buffer. */
659         struct mm_struct        *mm;
660
661         /* The task this context belongs to. */
662         struct task_struct      *task;
663
664         /* The signal to send on a bts buffer overflow. */
665         unsigned int            bts_ovfl_signal;
666
667         /* The work struct to destroy a context. */
668         struct work_struct      work;
669 };
670
671 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
672 {
673         void *buffer = NULL;
674         int err = -ENOMEM;
675
676         err = account_locked_memory(current->mm, current->signal->rlim, size);
677         if (err < 0)
678                 return err;
679
680         buffer = kzalloc(size, GFP_KERNEL);
681         if (!buffer)
682                 goto out_refund;
683
684         context->buffer = buffer;
685         context->size = size;
686         context->mm = get_task_mm(current);
687
688         return 0;
689
690  out_refund:
691         refund_locked_memory(current->mm, size);
692         return err;
693 }
694
695 static inline void free_bts_buffer(struct bts_context *context)
696 {
697         if (!context->buffer)
698                 return;
699
700         kfree(context->buffer);
701         context->buffer = NULL;
702
703         refund_locked_memory(context->mm, context->size);
704         context->size = 0;
705
706         mmput(context->mm);
707         context->mm = NULL;
708 }
709
710 static void free_bts_context_work(struct work_struct *w)
711 {
712         struct bts_context *context;
713
714         context = container_of(w, struct bts_context, work);
715
716         ds_release_bts(context->tracer);
717         put_task_struct(context->task);
718         free_bts_buffer(context);
719         kfree(context);
720 }
721
722 static inline void free_bts_context(struct bts_context *context)
723 {
724         INIT_WORK(&context->work, free_bts_context_work);
725         schedule_work(&context->work);
726 }
727
728 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
729 {
730         struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
731         if (context) {
732                 context->task = task;
733                 task->bts = context;
734
735                 get_task_struct(task);
736         }
737
738         return context;
739 }
740
741 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
742                                   struct bts_struct __user *out)
743 {
744         struct bts_context *context;
745         const struct bts_trace *trace;
746         struct bts_struct bts;
747         const unsigned char *at;
748         int error;
749
750         context = child->bts;
751         if (!context)
752                 return -ESRCH;
753
754         trace = ds_read_bts(context->tracer);
755         if (!trace)
756                 return -ESRCH;
757
758         at = trace->ds.top - ((index + 1) * trace->ds.size);
759         if ((void *)at < trace->ds.begin)
760                 at += (trace->ds.n * trace->ds.size);
761
762         if (!trace->read)
763                 return -EOPNOTSUPP;
764
765         error = trace->read(context->tracer, at, &bts);
766         if (error < 0)
767                 return error;
768
769         if (copy_to_user(out, &bts, sizeof(bts)))
770                 return -EFAULT;
771
772         return sizeof(bts);
773 }
774
775 static int ptrace_bts_drain(struct task_struct *child,
776                             long size,
777                             struct bts_struct __user *out)
778 {
779         struct bts_context *context;
780         const struct bts_trace *trace;
781         const unsigned char *at;
782         int error, drained = 0;
783
784         context = child->bts;
785         if (!context)
786                 return -ESRCH;
787
788         trace = ds_read_bts(context->tracer);
789         if (!trace)
790                 return -ESRCH;
791
792         if (!trace->read)
793                 return -EOPNOTSUPP;
794
795         if (size < (trace->ds.top - trace->ds.begin))
796                 return -EIO;
797
798         for (at = trace->ds.begin; (void *)at < trace->ds.top;
799              out++, drained++, at += trace->ds.size) {
800                 struct bts_struct bts;
801
802                 error = trace->read(context->tracer, at, &bts);
803                 if (error < 0)
804                         return error;
805
806                 if (copy_to_user(out, &bts, sizeof(bts)))
807                         return -EFAULT;
808         }
809
810         memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
811
812         error = ds_reset_bts(context->tracer);
813         if (error < 0)
814                 return error;
815
816         return drained;
817 }
818
819 static int ptrace_bts_config(struct task_struct *child,
820                              long cfg_size,
821                              const struct ptrace_bts_config __user *ucfg)
822 {
823         struct bts_context *context;
824         struct ptrace_bts_config cfg;
825         unsigned int flags = 0;
826
827         if (cfg_size < sizeof(cfg))
828                 return -EIO;
829
830         if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
831                 return -EFAULT;
832
833         context = child->bts;
834         if (!context)
835                 context = alloc_bts_context(child);
836         if (!context)
837                 return -ENOMEM;
838
839         if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
840                 if (!cfg.signal)
841                         return -EINVAL;
842
843                 return -EOPNOTSUPP;
844                 context->bts_ovfl_signal = cfg.signal;
845         }
846
847         ds_release_bts(context->tracer);
848         context->tracer = NULL;
849
850         if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
851                 int err;
852
853                 free_bts_buffer(context);
854                 if (!cfg.size)
855                         return 0;
856
857                 err = alloc_bts_buffer(context, cfg.size);
858                 if (err < 0)
859                         return err;
860         }
861
862         if (cfg.flags & PTRACE_BTS_O_TRACE)
863                 flags |= BTS_USER;
864
865         if (cfg.flags & PTRACE_BTS_O_SCHED)
866                 flags |= BTS_TIMESTAMPS;
867
868         context->tracer =
869                 ds_request_bts_task(child, context->buffer, context->size,
870                                     NULL, (size_t)-1, flags);
871         if (unlikely(IS_ERR(context->tracer))) {
872                 int error = PTR_ERR(context->tracer);
873
874                 free_bts_buffer(context);
875                 context->tracer = NULL;
876                 return error;
877         }
878
879         return sizeof(cfg);
880 }
881
882 static int ptrace_bts_status(struct task_struct *child,
883                              long cfg_size,
884                              struct ptrace_bts_config __user *ucfg)
885 {
886         struct bts_context *context;
887         const struct bts_trace *trace;
888         struct ptrace_bts_config cfg;
889
890         context = child->bts;
891         if (!context)
892                 return -ESRCH;
893
894         if (cfg_size < sizeof(cfg))
895                 return -EIO;
896
897         trace = ds_read_bts(context->tracer);
898         if (!trace)
899                 return -ESRCH;
900
901         memset(&cfg, 0, sizeof(cfg));
902         cfg.size        = trace->ds.end - trace->ds.begin;
903         cfg.signal      = context->bts_ovfl_signal;
904         cfg.bts_size    = sizeof(struct bts_struct);
905
906         if (cfg.signal)
907                 cfg.flags |= PTRACE_BTS_O_SIGNAL;
908
909         if (trace->ds.flags & BTS_USER)
910                 cfg.flags |= PTRACE_BTS_O_TRACE;
911
912         if (trace->ds.flags & BTS_TIMESTAMPS)
913                 cfg.flags |= PTRACE_BTS_O_SCHED;
914
915         if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
916                 return -EFAULT;
917
918         return sizeof(cfg);
919 }
920
921 static int ptrace_bts_clear(struct task_struct *child)
922 {
923         struct bts_context *context;
924         const struct bts_trace *trace;
925
926         context = child->bts;
927         if (!context)
928                 return -ESRCH;
929
930         trace = ds_read_bts(context->tracer);
931         if (!trace)
932                 return -ESRCH;
933
934         memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
935
936         return ds_reset_bts(context->tracer);
937 }
938
939 static int ptrace_bts_size(struct task_struct *child)
940 {
941         struct bts_context *context;
942         const struct bts_trace *trace;
943
944         context = child->bts;
945         if (!context)
946                 return -ESRCH;
947
948         trace = ds_read_bts(context->tracer);
949         if (!trace)
950                 return -ESRCH;
951
952         return (trace->ds.top - trace->ds.begin) / trace->ds.size;
953 }
954
955 /*
956  * Called from __ptrace_unlink() after the child has been moved back
957  * to its original parent.
958  */
959 void ptrace_bts_untrace(struct task_struct *child)
960 {
961         if (unlikely(child->bts)) {
962                 free_bts_context(child->bts);
963                 child->bts = NULL;
964         }
965 }
966 #endif /* CONFIG_X86_PTRACE_BTS */
967
968 /*
969  * Called by kernel/ptrace.c when detaching..
970  *
971  * Make sure the single step bit is not set.
972  */
973 void ptrace_disable(struct task_struct *child)
974 {
975         user_disable_single_step(child);
976 #ifdef TIF_SYSCALL_EMU
977         clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
978 #endif
979 }
980
981 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
982 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
983 #endif
984
985 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
986 {
987         int ret;
988         unsigned long __user *datap = (unsigned long __user *)data;
989
990         switch (request) {
991         /* read the word at location addr in the USER area. */
992         case PTRACE_PEEKUSR: {
993                 unsigned long tmp;
994
995                 ret = -EIO;
996                 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
997                     addr >= sizeof(struct user))
998                         break;
999
1000                 tmp = 0;  /* Default return condition */
1001                 if (addr < sizeof(struct user_regs_struct))
1002                         tmp = getreg(child, addr);
1003                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1004                          addr <= offsetof(struct user, u_debugreg[7])) {
1005                         addr -= offsetof(struct user, u_debugreg[0]);
1006                         tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1007                 }
1008                 ret = put_user(tmp, datap);
1009                 break;
1010         }
1011
1012         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1013                 ret = -EIO;
1014                 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1015                     addr >= sizeof(struct user))
1016                         break;
1017
1018                 if (addr < sizeof(struct user_regs_struct))
1019                         ret = putreg(child, addr, data);
1020                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1021                          addr <= offsetof(struct user, u_debugreg[7])) {
1022                         addr -= offsetof(struct user, u_debugreg[0]);
1023                         ret = ptrace_set_debugreg(child,
1024                                                   addr / sizeof(data), data);
1025                 }
1026                 break;
1027
1028         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1029                 return copy_regset_to_user(child,
1030                                            task_user_regset_view(current),
1031                                            REGSET_GENERAL,
1032                                            0, sizeof(struct user_regs_struct),
1033                                            datap);
1034
1035         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1036                 return copy_regset_from_user(child,
1037                                              task_user_regset_view(current),
1038                                              REGSET_GENERAL,
1039                                              0, sizeof(struct user_regs_struct),
1040                                              datap);
1041
1042         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1043                 return copy_regset_to_user(child,
1044                                            task_user_regset_view(current),
1045                                            REGSET_FP,
1046                                            0, sizeof(struct user_i387_struct),
1047                                            datap);
1048
1049         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1050                 return copy_regset_from_user(child,
1051                                              task_user_regset_view(current),
1052                                              REGSET_FP,
1053                                              0, sizeof(struct user_i387_struct),
1054                                              datap);
1055
1056 #ifdef CONFIG_X86_32
1057         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1058                 return copy_regset_to_user(child, &user_x86_32_view,
1059                                            REGSET_XFP,
1060                                            0, sizeof(struct user_fxsr_struct),
1061                                            datap) ? -EIO : 0;
1062
1063         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1064                 return copy_regset_from_user(child, &user_x86_32_view,
1065                                              REGSET_XFP,
1066                                              0, sizeof(struct user_fxsr_struct),
1067                                              datap) ? -EIO : 0;
1068 #endif
1069
1070 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1071         case PTRACE_GET_THREAD_AREA:
1072                 if (addr < 0)
1073                         return -EIO;
1074                 ret = do_get_thread_area(child, addr,
1075                                          (struct user_desc __user *) data);
1076                 break;
1077
1078         case PTRACE_SET_THREAD_AREA:
1079                 if (addr < 0)
1080                         return -EIO;
1081                 ret = do_set_thread_area(child, addr,
1082                                          (struct user_desc __user *) data, 0);
1083                 break;
1084 #endif
1085
1086 #ifdef CONFIG_X86_64
1087                 /* normal 64bit interface to access TLS data.
1088                    Works just like arch_prctl, except that the arguments
1089                    are reversed. */
1090         case PTRACE_ARCH_PRCTL:
1091                 ret = do_arch_prctl(child, data, addr);
1092                 break;
1093 #endif
1094
1095         /*
1096          * These bits need more cooking - not enabled yet:
1097          */
1098 #ifdef CONFIG_X86_PTRACE_BTS
1099         case PTRACE_BTS_CONFIG:
1100                 ret = ptrace_bts_config
1101                         (child, data, (struct ptrace_bts_config __user *)addr);
1102                 break;
1103
1104         case PTRACE_BTS_STATUS:
1105                 ret = ptrace_bts_status
1106                         (child, data, (struct ptrace_bts_config __user *)addr);
1107                 break;
1108
1109         case PTRACE_BTS_SIZE:
1110                 ret = ptrace_bts_size(child);
1111                 break;
1112
1113         case PTRACE_BTS_GET:
1114                 ret = ptrace_bts_read_record
1115                         (child, data, (struct bts_struct __user *) addr);
1116                 break;
1117
1118         case PTRACE_BTS_CLEAR:
1119                 ret = ptrace_bts_clear(child);
1120                 break;
1121
1122         case PTRACE_BTS_DRAIN:
1123                 ret = ptrace_bts_drain
1124                         (child, data, (struct bts_struct __user *) addr);
1125                 break;
1126 #endif /* CONFIG_X86_PTRACE_BTS */
1127
1128         default:
1129                 ret = ptrace_request(child, request, addr, data);
1130                 break;
1131         }
1132
1133         return ret;
1134 }
1135
1136 #ifdef CONFIG_IA32_EMULATION
1137
1138 #include <linux/compat.h>
1139 #include <linux/syscalls.h>
1140 #include <asm/ia32.h>
1141 #include <asm/user32.h>
1142
1143 #define R32(l,q)                                                        \
1144         case offsetof(struct user32, regs.l):                           \
1145                 regs->q = value; break
1146
1147 #define SEG32(rs)                                                       \
1148         case offsetof(struct user32, regs.rs):                          \
1149                 return set_segment_reg(child,                           \
1150                                        offsetof(struct user_regs_struct, rs), \
1151                                        value);                          \
1152                 break
1153
1154 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1155 {
1156         struct pt_regs *regs = task_pt_regs(child);
1157
1158         switch (regno) {
1159
1160         SEG32(cs);
1161         SEG32(ds);
1162         SEG32(es);
1163         SEG32(fs);
1164         SEG32(gs);
1165         SEG32(ss);
1166
1167         R32(ebx, bx);
1168         R32(ecx, cx);
1169         R32(edx, dx);
1170         R32(edi, di);
1171         R32(esi, si);
1172         R32(ebp, bp);
1173         R32(eax, ax);
1174         R32(eip, ip);
1175         R32(esp, sp);
1176
1177         case offsetof(struct user32, regs.orig_eax):
1178                 /*
1179                  * Sign-extend the value so that orig_eax = -1
1180                  * causes (long)orig_ax < 0 tests to fire correctly.
1181                  */
1182                 regs->orig_ax = (long) (s32) value;
1183                 break;
1184
1185         case offsetof(struct user32, regs.eflags):
1186                 return set_flags(child, value);
1187
1188         case offsetof(struct user32, u_debugreg[0]) ...
1189                 offsetof(struct user32, u_debugreg[7]):
1190                 regno -= offsetof(struct user32, u_debugreg[0]);
1191                 return ptrace_set_debugreg(child, regno / 4, value);
1192
1193         default:
1194                 if (regno > sizeof(struct user32) || (regno & 3))
1195                         return -EIO;
1196
1197                 /*
1198                  * Other dummy fields in the virtual user structure
1199                  * are ignored
1200                  */
1201                 break;
1202         }
1203         return 0;
1204 }
1205
1206 #undef R32
1207 #undef SEG32
1208
1209 #define R32(l,q)                                                        \
1210         case offsetof(struct user32, regs.l):                           \
1211                 *val = regs->q; break
1212
1213 #define SEG32(rs)                                                       \
1214         case offsetof(struct user32, regs.rs):                          \
1215                 *val = get_segment_reg(child,                           \
1216                                        offsetof(struct user_regs_struct, rs)); \
1217                 break
1218
1219 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1220 {
1221         struct pt_regs *regs = task_pt_regs(child);
1222
1223         switch (regno) {
1224
1225         SEG32(ds);
1226         SEG32(es);
1227         SEG32(fs);
1228         SEG32(gs);
1229
1230         R32(cs, cs);
1231         R32(ss, ss);
1232         R32(ebx, bx);
1233         R32(ecx, cx);
1234         R32(edx, dx);
1235         R32(edi, di);
1236         R32(esi, si);
1237         R32(ebp, bp);
1238         R32(eax, ax);
1239         R32(orig_eax, orig_ax);
1240         R32(eip, ip);
1241         R32(esp, sp);
1242
1243         case offsetof(struct user32, regs.eflags):
1244                 *val = get_flags(child);
1245                 break;
1246
1247         case offsetof(struct user32, u_debugreg[0]) ...
1248                 offsetof(struct user32, u_debugreg[7]):
1249                 regno -= offsetof(struct user32, u_debugreg[0]);
1250                 *val = ptrace_get_debugreg(child, regno / 4);
1251                 break;
1252
1253         default:
1254                 if (regno > sizeof(struct user32) || (regno & 3))
1255                         return -EIO;
1256
1257                 /*
1258                  * Other dummy fields in the virtual user structure
1259                  * are ignored
1260                  */
1261                 *val = 0;
1262                 break;
1263         }
1264         return 0;
1265 }
1266
1267 #undef R32
1268 #undef SEG32
1269
1270 static int genregs32_get(struct task_struct *target,
1271                          const struct user_regset *regset,
1272                          unsigned int pos, unsigned int count,
1273                          void *kbuf, void __user *ubuf)
1274 {
1275         if (kbuf) {
1276                 compat_ulong_t *k = kbuf;
1277                 while (count > 0) {
1278                         getreg32(target, pos, k++);
1279                         count -= sizeof(*k);
1280                         pos += sizeof(*k);
1281                 }
1282         } else {
1283                 compat_ulong_t __user *u = ubuf;
1284                 while (count > 0) {
1285                         compat_ulong_t word;
1286                         getreg32(target, pos, &word);
1287                         if (__put_user(word, u++))
1288                                 return -EFAULT;
1289                         count -= sizeof(*u);
1290                         pos += sizeof(*u);
1291                 }
1292         }
1293
1294         return 0;
1295 }
1296
1297 static int genregs32_set(struct task_struct *target,
1298                          const struct user_regset *regset,
1299                          unsigned int pos, unsigned int count,
1300                          const void *kbuf, const void __user *ubuf)
1301 {
1302         int ret = 0;
1303         if (kbuf) {
1304                 const compat_ulong_t *k = kbuf;
1305                 while (count > 0 && !ret) {
1306                         ret = putreg32(target, pos, *k++);
1307                         count -= sizeof(*k);
1308                         pos += sizeof(*k);
1309                 }
1310         } else {
1311                 const compat_ulong_t __user *u = ubuf;
1312                 while (count > 0 && !ret) {
1313                         compat_ulong_t word;
1314                         ret = __get_user(word, u++);
1315                         if (ret)
1316                                 break;
1317                         ret = putreg32(target, pos, word);
1318                         count -= sizeof(*u);
1319                         pos += sizeof(*u);
1320                 }
1321         }
1322         return ret;
1323 }
1324
1325 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1326                         compat_ulong_t caddr, compat_ulong_t cdata)
1327 {
1328         unsigned long addr = caddr;
1329         unsigned long data = cdata;
1330         void __user *datap = compat_ptr(data);
1331         int ret;
1332         __u32 val;
1333
1334         switch (request) {
1335         case PTRACE_PEEKUSR:
1336                 ret = getreg32(child, addr, &val);
1337                 if (ret == 0)
1338                         ret = put_user(val, (__u32 __user *)datap);
1339                 break;
1340
1341         case PTRACE_POKEUSR:
1342                 ret = putreg32(child, addr, data);
1343                 break;
1344
1345         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1346                 return copy_regset_to_user(child, &user_x86_32_view,
1347                                            REGSET_GENERAL,
1348                                            0, sizeof(struct user_regs_struct32),
1349                                            datap);
1350
1351         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1352                 return copy_regset_from_user(child, &user_x86_32_view,
1353                                              REGSET_GENERAL, 0,
1354                                              sizeof(struct user_regs_struct32),
1355                                              datap);
1356
1357         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1358                 return copy_regset_to_user(child, &user_x86_32_view,
1359                                            REGSET_FP, 0,
1360                                            sizeof(struct user_i387_ia32_struct),
1361                                            datap);
1362
1363         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1364                 return copy_regset_from_user(
1365                         child, &user_x86_32_view, REGSET_FP,
1366                         0, sizeof(struct user_i387_ia32_struct), datap);
1367
1368         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1369                 return copy_regset_to_user(child, &user_x86_32_view,
1370                                            REGSET_XFP, 0,
1371                                            sizeof(struct user32_fxsr_struct),
1372                                            datap);
1373
1374         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1375                 return copy_regset_from_user(child, &user_x86_32_view,
1376                                              REGSET_XFP, 0,
1377                                              sizeof(struct user32_fxsr_struct),
1378                                              datap);
1379
1380         case PTRACE_GET_THREAD_AREA:
1381         case PTRACE_SET_THREAD_AREA:
1382 #ifdef CONFIG_X86_PTRACE_BTS
1383         case PTRACE_BTS_CONFIG:
1384         case PTRACE_BTS_STATUS:
1385         case PTRACE_BTS_SIZE:
1386         case PTRACE_BTS_GET:
1387         case PTRACE_BTS_CLEAR:
1388         case PTRACE_BTS_DRAIN:
1389 #endif /* CONFIG_X86_PTRACE_BTS */
1390                 return arch_ptrace(child, request, addr, data);
1391
1392         default:
1393                 return compat_ptrace_request(child, request, addr, data);
1394         }
1395
1396         return ret;
1397 }
1398
1399 #endif  /* CONFIG_IA32_EMULATION */
1400
1401 #ifdef CONFIG_X86_64
1402
1403 static const struct user_regset x86_64_regsets[] = {
1404         [REGSET_GENERAL] = {
1405                 .core_note_type = NT_PRSTATUS,
1406                 .n = sizeof(struct user_regs_struct) / sizeof(long),
1407                 .size = sizeof(long), .align = sizeof(long),
1408                 .get = genregs_get, .set = genregs_set
1409         },
1410         [REGSET_FP] = {
1411                 .core_note_type = NT_PRFPREG,
1412                 .n = sizeof(struct user_i387_struct) / sizeof(long),
1413                 .size = sizeof(long), .align = sizeof(long),
1414                 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1415         },
1416         [REGSET_IOPERM64] = {
1417                 .core_note_type = NT_386_IOPERM,
1418                 .n = IO_BITMAP_LONGS,
1419                 .size = sizeof(long), .align = sizeof(long),
1420                 .active = ioperm_active, .get = ioperm_get
1421         },
1422 };
1423
1424 static const struct user_regset_view user_x86_64_view = {
1425         .name = "x86_64", .e_machine = EM_X86_64,
1426         .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1427 };
1428
1429 #else  /* CONFIG_X86_32 */
1430
1431 #define user_regs_struct32      user_regs_struct
1432 #define genregs32_get           genregs_get
1433 #define genregs32_set           genregs_set
1434
1435 #define user_i387_ia32_struct   user_i387_struct
1436 #define user32_fxsr_struct      user_fxsr_struct
1437
1438 #endif  /* CONFIG_X86_64 */
1439
1440 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1441 static const struct user_regset x86_32_regsets[] = {
1442         [REGSET_GENERAL] = {
1443                 .core_note_type = NT_PRSTATUS,
1444                 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1445                 .size = sizeof(u32), .align = sizeof(u32),
1446                 .get = genregs32_get, .set = genregs32_set
1447         },
1448         [REGSET_FP] = {
1449                 .core_note_type = NT_PRFPREG,
1450                 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1451                 .size = sizeof(u32), .align = sizeof(u32),
1452                 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1453         },
1454         [REGSET_XFP] = {
1455                 .core_note_type = NT_PRXFPREG,
1456                 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1457                 .size = sizeof(u32), .align = sizeof(u32),
1458                 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1459         },
1460         [REGSET_TLS] = {
1461                 .core_note_type = NT_386_TLS,
1462                 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1463                 .size = sizeof(struct user_desc),
1464                 .align = sizeof(struct user_desc),
1465                 .active = regset_tls_active,
1466                 .get = regset_tls_get, .set = regset_tls_set
1467         },
1468         [REGSET_IOPERM32] = {
1469                 .core_note_type = NT_386_IOPERM,
1470                 .n = IO_BITMAP_BYTES / sizeof(u32),
1471                 .size = sizeof(u32), .align = sizeof(u32),
1472                 .active = ioperm_active, .get = ioperm_get
1473         },
1474 };
1475
1476 static const struct user_regset_view user_x86_32_view = {
1477         .name = "i386", .e_machine = EM_386,
1478         .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1479 };
1480 #endif
1481
1482 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1483 {
1484 #ifdef CONFIG_IA32_EMULATION
1485         if (test_tsk_thread_flag(task, TIF_IA32))
1486 #endif
1487 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1488                 return &user_x86_32_view;
1489 #endif
1490 #ifdef CONFIG_X86_64
1491         return &user_x86_64_view;
1492 #endif
1493 }
1494
1495 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1496                                          int error_code, int si_code)
1497 {
1498         struct siginfo info;
1499
1500         tsk->thread.trap_no = 1;
1501         tsk->thread.error_code = error_code;
1502
1503         memset(&info, 0, sizeof(info));
1504         info.si_signo = SIGTRAP;
1505         info.si_code = si_code;
1506
1507         /* User-mode ip? */
1508         info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1509
1510         /* Send us the fake SIGTRAP */
1511         force_sig_info(SIGTRAP, &info, tsk);
1512 }
1513
1514
1515 #ifdef CONFIG_X86_32
1516 # define IS_IA32        1
1517 #elif defined CONFIG_IA32_EMULATION
1518 # define IS_IA32        is_compat_task()
1519 #else
1520 # define IS_IA32        0
1521 #endif
1522
1523 /*
1524  * We must return the syscall number to actually look up in the table.
1525  * This can be -1L to skip running any syscall at all.
1526  */
1527 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1528 {
1529         long ret = 0;
1530
1531         /*
1532          * If we stepped into a sysenter/syscall insn, it trapped in
1533          * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1534          * If user-mode had set TF itself, then it's still clear from
1535          * do_debug() and we need to set it again to restore the user
1536          * state.  If we entered on the slow path, TF was already set.
1537          */
1538         if (test_thread_flag(TIF_SINGLESTEP))
1539                 regs->flags |= X86_EFLAGS_TF;
1540
1541         /* do the secure computing check first */
1542         secure_computing(regs->orig_ax);
1543
1544         if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1545                 ret = -1L;
1546
1547         if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1548             tracehook_report_syscall_entry(regs))
1549                 ret = -1L;
1550
1551         if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1552                 ftrace_syscall_enter(regs);
1553
1554         if (unlikely(current->audit_context)) {
1555                 if (IS_IA32)
1556                         audit_syscall_entry(AUDIT_ARCH_I386,
1557                                             regs->orig_ax,
1558                                             regs->bx, regs->cx,
1559                                             regs->dx, regs->si);
1560 #ifdef CONFIG_X86_64
1561                 else
1562                         audit_syscall_entry(AUDIT_ARCH_X86_64,
1563                                             regs->orig_ax,
1564                                             regs->di, regs->si,
1565                                             regs->dx, regs->r10);
1566 #endif
1567         }
1568
1569         return ret ?: regs->orig_ax;
1570 }
1571
1572 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1573 {
1574         if (unlikely(current->audit_context))
1575                 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1576
1577         if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1578                 ftrace_syscall_exit(regs);
1579
1580         if (test_thread_flag(TIF_SYSCALL_TRACE))
1581                 tracehook_report_syscall_exit(regs, 0);
1582
1583         /*
1584          * If TIF_SYSCALL_EMU is set, we only get here because of
1585          * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1586          * We already reported this syscall instruction in
1587          * syscall_trace_enter(), so don't do any more now.
1588          */
1589         if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1590                 return;
1591
1592         /*
1593          * If we are single-stepping, synthesize a trap to follow the
1594          * system call instruction.
1595          */
1596         if (test_thread_flag(TIF_SINGLESTEP) &&
1597             tracehook_consider_fatal_signal(current, SIGTRAP))
1598                 send_sigtrap(current, regs, 0, TRAP_BRKPT);
1599 }