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