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