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