Merge branches 'release', 'bugzilla-8570', 'bugzilla-9966', 'bugzilla-9998', 'bugzill...
[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/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
28 #include <asm/i387.h>
29 #include <asm/debugreg.h>
30 #include <asm/ldt.h>
31 #include <asm/desc.h>
32 #include <asm/prctl.h>
33 #include <asm/proto.h>
34 #include <asm/ds.h>
35
36 #include "tls.h"
37
38 enum x86_regset {
39         REGSET_GENERAL,
40         REGSET_FP,
41         REGSET_XFP,
42         REGSET_TLS,
43 };
44
45 /*
46  * does not yet catch signals sent when the child dies.
47  * in exit.c or in signal.c.
48  */
49
50 /*
51  * Determines which flags the user has access to [1 = access, 0 = no access].
52  */
53 #define FLAG_MASK_32            ((unsigned long)                        \
54                                  (X86_EFLAGS_CF | X86_EFLAGS_PF |       \
55                                   X86_EFLAGS_AF | X86_EFLAGS_ZF |       \
56                                   X86_EFLAGS_SF | X86_EFLAGS_TF |       \
57                                   X86_EFLAGS_DF | X86_EFLAGS_OF |       \
58                                   X86_EFLAGS_RF | X86_EFLAGS_AC))
59
60 /*
61  * Determines whether a value may be installed in a segment register.
62  */
63 static inline bool invalid_selector(u16 value)
64 {
65         return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
66 }
67
68 #ifdef CONFIG_X86_32
69
70 #define FLAG_MASK               FLAG_MASK_32
71
72 static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
73 {
74         BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
75         regno >>= 2;
76         if (regno > FS)
77                 --regno;
78         return &regs->bx + regno;
79 }
80
81 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
82 {
83         /*
84          * Returning the value truncates it to 16 bits.
85          */
86         unsigned int retval;
87         if (offset != offsetof(struct user_regs_struct, gs))
88                 retval = *pt_regs_access(task_pt_regs(task), offset);
89         else {
90                 retval = task->thread.gs;
91                 if (task == current)
92                         savesegment(gs, retval);
93         }
94         return retval;
95 }
96
97 static int set_segment_reg(struct task_struct *task,
98                            unsigned long offset, u16 value)
99 {
100         /*
101          * The value argument was already truncated to 16 bits.
102          */
103         if (invalid_selector(value))
104                 return -EIO;
105
106         /*
107          * For %cs and %ss we cannot permit a null selector.
108          * We can permit a bogus selector as long as it has USER_RPL.
109          * Null selectors are fine for other segment registers, but
110          * we will never get back to user mode with invalid %cs or %ss
111          * and will take the trap in iret instead.  Much code relies
112          * on user_mode() to distinguish a user trap frame (which can
113          * safely use invalid selectors) from a kernel trap frame.
114          */
115         switch (offset) {
116         case offsetof(struct user_regs_struct, cs):
117         case offsetof(struct user_regs_struct, ss):
118                 if (unlikely(value == 0))
119                         return -EIO;
120
121         default:
122                 *pt_regs_access(task_pt_regs(task), offset) = value;
123                 break;
124
125         case offsetof(struct user_regs_struct, gs):
126                 task->thread.gs = value;
127                 if (task == current)
128                         /*
129                          * The user-mode %gs is not affected by
130                          * kernel entry, so we must update the CPU.
131                          */
132                         loadsegment(gs, value);
133         }
134
135         return 0;
136 }
137
138 static unsigned long debugreg_addr_limit(struct task_struct *task)
139 {
140         return TASK_SIZE - 3;
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 static unsigned long debugreg_addr_limit(struct task_struct *task)
268 {
269 #ifdef CONFIG_IA32_EMULATION
270         if (test_tsk_thread_flag(task, TIF_IA32))
271                 return IA32_PAGE_OFFSET - 3;
272 #endif
273         return TASK_SIZE64 - 7;
274 }
275
276 #endif  /* CONFIG_X86_32 */
277
278 static unsigned long get_flags(struct task_struct *task)
279 {
280         unsigned long retval = task_pt_regs(task)->flags;
281
282         /*
283          * If the debugger set TF, hide it from the readout.
284          */
285         if (test_tsk_thread_flag(task, TIF_FORCED_TF))
286                 retval &= ~X86_EFLAGS_TF;
287
288         return retval;
289 }
290
291 static int set_flags(struct task_struct *task, unsigned long value)
292 {
293         struct pt_regs *regs = task_pt_regs(task);
294
295         /*
296          * If the user value contains TF, mark that
297          * it was not "us" (the debugger) that set it.
298          * If not, make sure it stays set if we had.
299          */
300         if (value & X86_EFLAGS_TF)
301                 clear_tsk_thread_flag(task, TIF_FORCED_TF);
302         else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
303                 value |= X86_EFLAGS_TF;
304
305         regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
306
307         return 0;
308 }
309
310 static int putreg(struct task_struct *child,
311                   unsigned long offset, unsigned long value)
312 {
313         switch (offset) {
314         case offsetof(struct user_regs_struct, cs):
315         case offsetof(struct user_regs_struct, ds):
316         case offsetof(struct user_regs_struct, es):
317         case offsetof(struct user_regs_struct, fs):
318         case offsetof(struct user_regs_struct, gs):
319         case offsetof(struct user_regs_struct, ss):
320                 return set_segment_reg(child, offset, value);
321
322         case offsetof(struct user_regs_struct, flags):
323                 return set_flags(child, value);
324
325 #ifdef CONFIG_X86_64
326         /*
327          * Orig_ax is really just a flag with small positive and
328          * negative values, so make sure to always sign-extend it
329          * from 32 bits so that it works correctly regardless of
330          * whether we come from a 32-bit environment or not.
331          */
332         case offsetof(struct user_regs_struct, orig_ax):
333                 value = (long) (s32) value;
334                 break;
335
336         case offsetof(struct user_regs_struct,fs_base):
337                 if (value >= TASK_SIZE_OF(child))
338                         return -EIO;
339                 /*
340                  * When changing the segment base, use do_arch_prctl
341                  * to set either thread.fs or thread.fsindex and the
342                  * corresponding GDT slot.
343                  */
344                 if (child->thread.fs != value)
345                         return do_arch_prctl(child, ARCH_SET_FS, value);
346                 return 0;
347         case offsetof(struct user_regs_struct,gs_base):
348                 /*
349                  * Exactly the same here as the %fs handling above.
350                  */
351                 if (value >= TASK_SIZE_OF(child))
352                         return -EIO;
353                 if (child->thread.gs != value)
354                         return do_arch_prctl(child, ARCH_SET_GS, value);
355                 return 0;
356 #endif
357         }
358
359         *pt_regs_access(task_pt_regs(child), offset) = value;
360         return 0;
361 }
362
363 static unsigned long getreg(struct task_struct *task, unsigned long offset)
364 {
365         switch (offset) {
366         case offsetof(struct user_regs_struct, cs):
367         case offsetof(struct user_regs_struct, ds):
368         case offsetof(struct user_regs_struct, es):
369         case offsetof(struct user_regs_struct, fs):
370         case offsetof(struct user_regs_struct, gs):
371         case offsetof(struct user_regs_struct, ss):
372                 return get_segment_reg(task, offset);
373
374         case offsetof(struct user_regs_struct, flags):
375                 return get_flags(task);
376
377 #ifdef CONFIG_X86_64
378         case offsetof(struct user_regs_struct, fs_base): {
379                 /*
380                  * do_arch_prctl may have used a GDT slot instead of
381                  * the MSR.  To userland, it appears the same either
382                  * way, except the %fs segment selector might not be 0.
383                  */
384                 unsigned int seg = task->thread.fsindex;
385                 if (task->thread.fs != 0)
386                         return task->thread.fs;
387                 if (task == current)
388                         asm("movl %%fs,%0" : "=r" (seg));
389                 if (seg != FS_TLS_SEL)
390                         return 0;
391                 return get_desc_base(&task->thread.tls_array[FS_TLS]);
392         }
393         case offsetof(struct user_regs_struct, gs_base): {
394                 /*
395                  * Exactly the same here as the %fs handling above.
396                  */
397                 unsigned int seg = task->thread.gsindex;
398                 if (task->thread.gs != 0)
399                         return task->thread.gs;
400                 if (task == current)
401                         asm("movl %%gs,%0" : "=r" (seg));
402                 if (seg != GS_TLS_SEL)
403                         return 0;
404                 return get_desc_base(&task->thread.tls_array[GS_TLS]);
405         }
406 #endif
407         }
408
409         return *pt_regs_access(task_pt_regs(task), offset);
410 }
411
412 static int genregs_get(struct task_struct *target,
413                        const struct user_regset *regset,
414                        unsigned int pos, unsigned int count,
415                        void *kbuf, void __user *ubuf)
416 {
417         if (kbuf) {
418                 unsigned long *k = kbuf;
419                 while (count > 0) {
420                         *k++ = getreg(target, pos);
421                         count -= sizeof(*k);
422                         pos += sizeof(*k);
423                 }
424         } else {
425                 unsigned long __user *u = ubuf;
426                 while (count > 0) {
427                         if (__put_user(getreg(target, pos), u++))
428                                 return -EFAULT;
429                         count -= sizeof(*u);
430                         pos += sizeof(*u);
431                 }
432         }
433
434         return 0;
435 }
436
437 static int genregs_set(struct task_struct *target,
438                        const struct user_regset *regset,
439                        unsigned int pos, unsigned int count,
440                        const void *kbuf, const void __user *ubuf)
441 {
442         int ret = 0;
443         if (kbuf) {
444                 const unsigned long *k = kbuf;
445                 while (count > 0 && !ret) {
446                         ret = putreg(target, pos, *k++);
447                         count -= sizeof(*k);
448                         pos += sizeof(*k);
449                 }
450         } else {
451                 const unsigned long  __user *u = ubuf;
452                 while (count > 0 && !ret) {
453                         unsigned long word;
454                         ret = __get_user(word, u++);
455                         if (ret)
456                                 break;
457                         ret = putreg(target, pos, word);
458                         count -= sizeof(*u);
459                         pos += sizeof(*u);
460                 }
461         }
462         return ret;
463 }
464
465 /*
466  * This function is trivial and will be inlined by the compiler.
467  * Having it separates the implementation details of debug
468  * registers from the interface details of ptrace.
469  */
470 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
471 {
472         switch (n) {
473         case 0:         return child->thread.debugreg0;
474         case 1:         return child->thread.debugreg1;
475         case 2:         return child->thread.debugreg2;
476         case 3:         return child->thread.debugreg3;
477         case 6:         return child->thread.debugreg6;
478         case 7:         return child->thread.debugreg7;
479         }
480         return 0;
481 }
482
483 static int ptrace_set_debugreg(struct task_struct *child,
484                                int n, unsigned long data)
485 {
486         int i;
487
488         if (unlikely(n == 4 || n == 5))
489                 return -EIO;
490
491         if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
492                 return -EIO;
493
494         switch (n) {
495         case 0:         child->thread.debugreg0 = data; break;
496         case 1:         child->thread.debugreg1 = data; break;
497         case 2:         child->thread.debugreg2 = data; break;
498         case 3:         child->thread.debugreg3 = data; break;
499
500         case 6:
501                 if ((data & ~0xffffffffUL) != 0)
502                         return -EIO;
503                 child->thread.debugreg6 = data;
504                 break;
505
506         case 7:
507                 /*
508                  * Sanity-check data. Take one half-byte at once with
509                  * check = (val >> (16 + 4*i)) & 0xf. It contains the
510                  * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
511                  * 2 and 3 are LENi. Given a list of invalid values,
512                  * we do mask |= 1 << invalid_value, so that
513                  * (mask >> check) & 1 is a correct test for invalid
514                  * values.
515                  *
516                  * R/Wi contains the type of the breakpoint /
517                  * watchpoint, LENi contains the length of the watched
518                  * data in the watchpoint case.
519                  *
520                  * The invalid values are:
521                  * - LENi == 0x10 (undefined), so mask |= 0x0f00.       [32-bit]
522                  * - R/Wi == 0x10 (break on I/O reads or writes), so
523                  *   mask |= 0x4444.
524                  * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
525                  *   0x1110.
526                  *
527                  * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
528                  *
529                  * See the Intel Manual "System Programming Guide",
530                  * 15.2.4
531                  *
532                  * Note that LENi == 0x10 is defined on x86_64 in long
533                  * mode (i.e. even for 32-bit userspace software, but
534                  * 64-bit kernel), so the x86_64 mask value is 0x5454.
535                  * See the AMD manual no. 24593 (AMD64 System Programming)
536                  */
537 #ifdef CONFIG_X86_32
538 #define DR7_MASK        0x5f54
539 #else
540 #define DR7_MASK        0x5554
541 #endif
542                 data &= ~DR_CONTROL_RESERVED;
543                 for (i = 0; i < 4; i++)
544                         if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
545                                 return -EIO;
546                 child->thread.debugreg7 = data;
547                 if (data)
548                         set_tsk_thread_flag(child, TIF_DEBUG);
549                 else
550                         clear_tsk_thread_flag(child, TIF_DEBUG);
551                 break;
552         }
553
554         return 0;
555 }
556
557 #ifdef X86_BTS
558
559 static int ptrace_bts_get_size(struct task_struct *child)
560 {
561         if (!child->thread.ds_area_msr)
562                 return -ENXIO;
563
564         return ds_get_bts_index((void *)child->thread.ds_area_msr);
565 }
566
567 static int ptrace_bts_read_record(struct task_struct *child,
568                                   long index,
569                                   struct bts_struct __user *out)
570 {
571         struct bts_struct ret;
572         int retval;
573         int bts_end;
574         int bts_index;
575
576         if (!child->thread.ds_area_msr)
577                 return -ENXIO;
578
579         if (index < 0)
580                 return -EINVAL;
581
582         bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
583         if (bts_end <= index)
584                 return -EINVAL;
585
586         /* translate the ptrace bts index into the ds bts index */
587         bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
588         bts_index -= (index + 1);
589         if (bts_index < 0)
590                 bts_index += bts_end;
591
592         retval = ds_read_bts((void *)child->thread.ds_area_msr,
593                              bts_index, &ret);
594         if (retval < 0)
595                 return retval;
596
597         if (copy_to_user(out, &ret, sizeof(ret)))
598                 return -EFAULT;
599
600         return sizeof(ret);
601 }
602
603 static int ptrace_bts_write_record(struct task_struct *child,
604                                    const struct bts_struct *in)
605 {
606         int retval;
607
608         if (!child->thread.ds_area_msr)
609                 return -ENXIO;
610
611         retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
612         if (retval)
613                 return retval;
614
615         return sizeof(*in);
616 }
617
618 static int ptrace_bts_clear(struct task_struct *child)
619 {
620         if (!child->thread.ds_area_msr)
621                 return -ENXIO;
622
623         return ds_clear((void *)child->thread.ds_area_msr);
624 }
625
626 static int ptrace_bts_drain(struct task_struct *child,
627                             long size,
628                             struct bts_struct __user *out)
629 {
630         int end, i;
631         void *ds = (void *)child->thread.ds_area_msr;
632
633         if (!ds)
634                 return -ENXIO;
635
636         end = ds_get_bts_index(ds);
637         if (end <= 0)
638                 return end;
639
640         if (size < (end * sizeof(struct bts_struct)))
641                 return -EIO;
642
643         for (i = 0; i < end; i++, out++) {
644                 struct bts_struct ret;
645                 int retval;
646
647                 retval = ds_read_bts(ds, i, &ret);
648                 if (retval < 0)
649                         return retval;
650
651                 if (copy_to_user(out, &ret, sizeof(ret)))
652                         return -EFAULT;
653         }
654
655         ds_clear(ds);
656
657         return end;
658 }
659
660 static int ptrace_bts_realloc(struct task_struct *child,
661                               int size, int reduce_size)
662 {
663         unsigned long rlim, vm;
664         int ret, old_size;
665
666         if (size < 0)
667                 return -EINVAL;
668
669         old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
670         if (old_size < 0)
671                 return old_size;
672
673         ret = ds_free((void **)&child->thread.ds_area_msr);
674         if (ret < 0)
675                 goto out;
676
677         size >>= PAGE_SHIFT;
678         old_size >>= PAGE_SHIFT;
679
680         current->mm->total_vm  -= old_size;
681         current->mm->locked_vm -= old_size;
682
683         if (size == 0)
684                 goto out;
685
686         rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
687         vm = current->mm->total_vm  + size;
688         if (rlim < vm) {
689                 ret = -ENOMEM;
690
691                 if (!reduce_size)
692                         goto out;
693
694                 size = rlim - current->mm->total_vm;
695                 if (size <= 0)
696                         goto out;
697         }
698
699         rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
700         vm = current->mm->locked_vm  + size;
701         if (rlim < vm) {
702                 ret = -ENOMEM;
703
704                 if (!reduce_size)
705                         goto out;
706
707                 size = rlim - current->mm->locked_vm;
708                 if (size <= 0)
709                         goto out;
710         }
711
712         ret = ds_allocate((void **)&child->thread.ds_area_msr,
713                           size << PAGE_SHIFT);
714         if (ret < 0)
715                 goto out;
716
717         current->mm->total_vm  += size;
718         current->mm->locked_vm += size;
719
720 out:
721         if (child->thread.ds_area_msr)
722                 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
723         else
724                 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
725
726         return ret;
727 }
728
729 static int ptrace_bts_config(struct task_struct *child,
730                              long cfg_size,
731                              const struct ptrace_bts_config __user *ucfg)
732 {
733         struct ptrace_bts_config cfg;
734         int bts_size, ret = 0;
735         void *ds;
736
737         if (cfg_size < sizeof(cfg))
738                 return -EIO;
739
740         if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
741                 return -EFAULT;
742
743         if ((int)cfg.size < 0)
744                 return -EINVAL;
745
746         bts_size = 0;
747         ds = (void *)child->thread.ds_area_msr;
748         if (ds) {
749                 bts_size = ds_get_bts_size(ds);
750                 if (bts_size < 0)
751                         return bts_size;
752         }
753         cfg.size = PAGE_ALIGN(cfg.size);
754
755         if (bts_size != cfg.size) {
756                 ret = ptrace_bts_realloc(child, cfg.size,
757                                          cfg.flags & PTRACE_BTS_O_CUT_SIZE);
758                 if (ret < 0)
759                         goto errout;
760
761                 ds = (void *)child->thread.ds_area_msr;
762         }
763
764         if (cfg.flags & PTRACE_BTS_O_SIGNAL)
765                 ret = ds_set_overflow(ds, DS_O_SIGNAL);
766         else
767                 ret = ds_set_overflow(ds, DS_O_WRAP);
768         if (ret < 0)
769                 goto errout;
770
771         if (cfg.flags & PTRACE_BTS_O_TRACE)
772                 child->thread.debugctlmsr |= ds_debugctl_mask();
773         else
774                 child->thread.debugctlmsr &= ~ds_debugctl_mask();
775
776         if (cfg.flags & PTRACE_BTS_O_SCHED)
777                 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
778         else
779                 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
780
781         ret = sizeof(cfg);
782
783 out:
784         if (child->thread.debugctlmsr)
785                 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
786         else
787                 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
788
789         return ret;
790
791 errout:
792         child->thread.debugctlmsr &= ~ds_debugctl_mask();
793         clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
794         goto out;
795 }
796
797 static int ptrace_bts_status(struct task_struct *child,
798                              long cfg_size,
799                              struct ptrace_bts_config __user *ucfg)
800 {
801         void *ds = (void *)child->thread.ds_area_msr;
802         struct ptrace_bts_config cfg;
803
804         if (cfg_size < sizeof(cfg))
805                 return -EIO;
806
807         memset(&cfg, 0, sizeof(cfg));
808
809         if (ds) {
810                 cfg.size = ds_get_bts_size(ds);
811
812                 if (ds_get_overflow(ds) == DS_O_SIGNAL)
813                         cfg.flags |= PTRACE_BTS_O_SIGNAL;
814
815                 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
816                     child->thread.debugctlmsr & ds_debugctl_mask())
817                         cfg.flags |= PTRACE_BTS_O_TRACE;
818
819                 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
820                         cfg.flags |= PTRACE_BTS_O_SCHED;
821         }
822
823         cfg.bts_size = sizeof(struct bts_struct);
824
825         if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
826                 return -EFAULT;
827
828         return sizeof(cfg);
829 }
830
831 void ptrace_bts_take_timestamp(struct task_struct *tsk,
832                                enum bts_qualifier qualifier)
833 {
834         struct bts_struct rec = {
835                 .qualifier = qualifier,
836                 .variant.jiffies = jiffies_64
837         };
838
839         ptrace_bts_write_record(tsk, &rec);
840 }
841 #endif /* X86_BTS */
842
843 /*
844  * Called by kernel/ptrace.c when detaching..
845  *
846  * Make sure the single step bit is not set.
847  */
848 void ptrace_disable(struct task_struct *child)
849 {
850         user_disable_single_step(child);
851 #ifdef TIF_SYSCALL_EMU
852         clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
853 #endif
854         if (child->thread.ds_area_msr) {
855 #ifdef X86_BTS
856                 ptrace_bts_realloc(child, 0, 0);
857 #endif
858                 child->thread.debugctlmsr &= ~ds_debugctl_mask();
859                 if (!child->thread.debugctlmsr)
860                         clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
861                 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
862         }
863 }
864
865 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
866 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
867 #endif
868
869 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
870 {
871         int ret;
872         unsigned long __user *datap = (unsigned long __user *)data;
873
874         switch (request) {
875         /* read the word at location addr in the USER area. */
876         case PTRACE_PEEKUSR: {
877                 unsigned long tmp;
878
879                 ret = -EIO;
880                 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
881                     addr >= sizeof(struct user))
882                         break;
883
884                 tmp = 0;  /* Default return condition */
885                 if (addr < sizeof(struct user_regs_struct))
886                         tmp = getreg(child, addr);
887                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
888                          addr <= offsetof(struct user, u_debugreg[7])) {
889                         addr -= offsetof(struct user, u_debugreg[0]);
890                         tmp = ptrace_get_debugreg(child, addr / sizeof(data));
891                 }
892                 ret = put_user(tmp, datap);
893                 break;
894         }
895
896         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
897                 ret = -EIO;
898                 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
899                     addr >= sizeof(struct user))
900                         break;
901
902                 if (addr < sizeof(struct user_regs_struct))
903                         ret = putreg(child, addr, data);
904                 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
905                          addr <= offsetof(struct user, u_debugreg[7])) {
906                         addr -= offsetof(struct user, u_debugreg[0]);
907                         ret = ptrace_set_debugreg(child,
908                                                   addr / sizeof(data), data);
909                 }
910                 break;
911
912         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
913                 return copy_regset_to_user(child,
914                                            task_user_regset_view(current),
915                                            REGSET_GENERAL,
916                                            0, sizeof(struct user_regs_struct),
917                                            datap);
918
919         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
920                 return copy_regset_from_user(child,
921                                              task_user_regset_view(current),
922                                              REGSET_GENERAL,
923                                              0, sizeof(struct user_regs_struct),
924                                              datap);
925
926         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
927                 return copy_regset_to_user(child,
928                                            task_user_regset_view(current),
929                                            REGSET_FP,
930                                            0, sizeof(struct user_i387_struct),
931                                            datap);
932
933         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
934                 return copy_regset_from_user(child,
935                                              task_user_regset_view(current),
936                                              REGSET_FP,
937                                              0, sizeof(struct user_i387_struct),
938                                              datap);
939
940 #ifdef CONFIG_X86_32
941         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
942                 return copy_regset_to_user(child, &user_x86_32_view,
943                                            REGSET_XFP,
944                                            0, sizeof(struct user_fxsr_struct),
945                                            datap);
946
947         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
948                 return copy_regset_from_user(child, &user_x86_32_view,
949                                              REGSET_XFP,
950                                              0, sizeof(struct user_fxsr_struct),
951                                              datap);
952 #endif
953
954 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
955         case PTRACE_GET_THREAD_AREA:
956                 if (addr < 0)
957                         return -EIO;
958                 ret = do_get_thread_area(child, addr,
959                                          (struct user_desc __user *) data);
960                 break;
961
962         case PTRACE_SET_THREAD_AREA:
963                 if (addr < 0)
964                         return -EIO;
965                 ret = do_set_thread_area(child, addr,
966                                          (struct user_desc __user *) data, 0);
967                 break;
968 #endif
969
970 #ifdef CONFIG_X86_64
971                 /* normal 64bit interface to access TLS data.
972                    Works just like arch_prctl, except that the arguments
973                    are reversed. */
974         case PTRACE_ARCH_PRCTL:
975                 ret = do_arch_prctl(child, data, addr);
976                 break;
977 #endif
978
979         /*
980          * These bits need more cooking - not enabled yet:
981          */
982 #ifdef X86_BTS
983         case PTRACE_BTS_CONFIG:
984                 ret = ptrace_bts_config
985                         (child, data, (struct ptrace_bts_config __user *)addr);
986                 break;
987
988         case PTRACE_BTS_STATUS:
989                 ret = ptrace_bts_status
990                         (child, data, (struct ptrace_bts_config __user *)addr);
991                 break;
992
993         case PTRACE_BTS_SIZE:
994                 ret = ptrace_bts_get_size(child);
995                 break;
996
997         case PTRACE_BTS_GET:
998                 ret = ptrace_bts_read_record
999                         (child, data, (struct bts_struct __user *) addr);
1000                 break;
1001
1002         case PTRACE_BTS_CLEAR:
1003                 ret = ptrace_bts_clear(child);
1004                 break;
1005
1006         case PTRACE_BTS_DRAIN:
1007                 ret = ptrace_bts_drain
1008                         (child, data, (struct bts_struct __user *) addr);
1009                 break;
1010 #endif
1011
1012         default:
1013                 ret = ptrace_request(child, request, addr, data);
1014                 break;
1015         }
1016
1017         return ret;
1018 }
1019
1020 #ifdef CONFIG_IA32_EMULATION
1021
1022 #include <linux/compat.h>
1023 #include <linux/syscalls.h>
1024 #include <asm/ia32.h>
1025 #include <asm/user32.h>
1026
1027 #define R32(l,q)                                                        \
1028         case offsetof(struct user32, regs.l):                           \
1029                 regs->q = value; break
1030
1031 #define SEG32(rs)                                                       \
1032         case offsetof(struct user32, regs.rs):                          \
1033                 return set_segment_reg(child,                           \
1034                                        offsetof(struct user_regs_struct, rs), \
1035                                        value);                          \
1036                 break
1037
1038 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1039 {
1040         struct pt_regs *regs = task_pt_regs(child);
1041
1042         switch (regno) {
1043
1044         SEG32(cs);
1045         SEG32(ds);
1046         SEG32(es);
1047         SEG32(fs);
1048         SEG32(gs);
1049         SEG32(ss);
1050
1051         R32(ebx, bx);
1052         R32(ecx, cx);
1053         R32(edx, dx);
1054         R32(edi, di);
1055         R32(esi, si);
1056         R32(ebp, bp);
1057         R32(eax, ax);
1058         R32(eip, ip);
1059         R32(esp, sp);
1060
1061         case offsetof(struct user32, regs.orig_eax):
1062                 /*
1063                  * Sign-extend the value so that orig_eax = -1
1064                  * causes (long)orig_ax < 0 tests to fire correctly.
1065                  */
1066                 regs->orig_ax = (long) (s32) value;
1067                 break;
1068
1069         case offsetof(struct user32, regs.eflags):
1070                 return set_flags(child, value);
1071
1072         case offsetof(struct user32, u_debugreg[0]) ...
1073                 offsetof(struct user32, u_debugreg[7]):
1074                 regno -= offsetof(struct user32, u_debugreg[0]);
1075                 return ptrace_set_debugreg(child, regno / 4, value);
1076
1077         default:
1078                 if (regno > sizeof(struct user32) || (regno & 3))
1079                         return -EIO;
1080
1081                 /*
1082                  * Other dummy fields in the virtual user structure
1083                  * are ignored
1084                  */
1085                 break;
1086         }
1087         return 0;
1088 }
1089
1090 #undef R32
1091 #undef SEG32
1092
1093 #define R32(l,q)                                                        \
1094         case offsetof(struct user32, regs.l):                           \
1095                 *val = regs->q; break
1096
1097 #define SEG32(rs)                                                       \
1098         case offsetof(struct user32, regs.rs):                          \
1099                 *val = get_segment_reg(child,                           \
1100                                        offsetof(struct user_regs_struct, rs)); \
1101                 break
1102
1103 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1104 {
1105         struct pt_regs *regs = task_pt_regs(child);
1106
1107         switch (regno) {
1108
1109         SEG32(ds);
1110         SEG32(es);
1111         SEG32(fs);
1112         SEG32(gs);
1113
1114         R32(cs, cs);
1115         R32(ss, ss);
1116         R32(ebx, bx);
1117         R32(ecx, cx);
1118         R32(edx, dx);
1119         R32(edi, di);
1120         R32(esi, si);
1121         R32(ebp, bp);
1122         R32(eax, ax);
1123         R32(orig_eax, orig_ax);
1124         R32(eip, ip);
1125         R32(esp, sp);
1126
1127         case offsetof(struct user32, regs.eflags):
1128                 *val = get_flags(child);
1129                 break;
1130
1131         case offsetof(struct user32, u_debugreg[0]) ...
1132                 offsetof(struct user32, u_debugreg[7]):
1133                 regno -= offsetof(struct user32, u_debugreg[0]);
1134                 *val = ptrace_get_debugreg(child, regno / 4);
1135                 break;
1136
1137         default:
1138                 if (regno > sizeof(struct user32) || (regno & 3))
1139                         return -EIO;
1140
1141                 /*
1142                  * Other dummy fields in the virtual user structure
1143                  * are ignored
1144                  */
1145                 *val = 0;
1146                 break;
1147         }
1148         return 0;
1149 }
1150
1151 #undef R32
1152 #undef SEG32
1153
1154 static int genregs32_get(struct task_struct *target,
1155                          const struct user_regset *regset,
1156                          unsigned int pos, unsigned int count,
1157                          void *kbuf, void __user *ubuf)
1158 {
1159         if (kbuf) {
1160                 compat_ulong_t *k = kbuf;
1161                 while (count > 0) {
1162                         getreg32(target, pos, k++);
1163                         count -= sizeof(*k);
1164                         pos += sizeof(*k);
1165                 }
1166         } else {
1167                 compat_ulong_t __user *u = ubuf;
1168                 while (count > 0) {
1169                         compat_ulong_t word;
1170                         getreg32(target, pos, &word);
1171                         if (__put_user(word, u++))
1172                                 return -EFAULT;
1173                         count -= sizeof(*u);
1174                         pos += sizeof(*u);
1175                 }
1176         }
1177
1178         return 0;
1179 }
1180
1181 static int genregs32_set(struct task_struct *target,
1182                          const struct user_regset *regset,
1183                          unsigned int pos, unsigned int count,
1184                          const void *kbuf, const void __user *ubuf)
1185 {
1186         int ret = 0;
1187         if (kbuf) {
1188                 const compat_ulong_t *k = kbuf;
1189                 while (count > 0 && !ret) {
1190                         ret = putreg32(target, pos, *k++);
1191                         count -= sizeof(*k);
1192                         pos += sizeof(*k);
1193                 }
1194         } else {
1195                 const compat_ulong_t __user *u = ubuf;
1196                 while (count > 0 && !ret) {
1197                         compat_ulong_t word;
1198                         ret = __get_user(word, u++);
1199                         if (ret)
1200                                 break;
1201                         ret = putreg32(target, pos, word);
1202                         count -= sizeof(*u);
1203                         pos += sizeof(*u);
1204                 }
1205         }
1206         return ret;
1207 }
1208
1209 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
1210 {
1211         siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
1212         compat_siginfo_t __user *si32 = compat_ptr(data);
1213         siginfo_t ssi;
1214         int ret;
1215
1216         if (request == PTRACE_SETSIGINFO) {
1217                 memset(&ssi, 0, sizeof(siginfo_t));
1218                 ret = copy_siginfo_from_user32(&ssi, si32);
1219                 if (ret)
1220                         return ret;
1221                 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
1222                         return -EFAULT;
1223         }
1224         ret = sys_ptrace(request, pid, addr, (unsigned long)si);
1225         if (ret)
1226                 return ret;
1227         if (request == PTRACE_GETSIGINFO) {
1228                 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
1229                         return -EFAULT;
1230                 ret = copy_siginfo_to_user32(si32, &ssi);
1231         }
1232         return ret;
1233 }
1234
1235 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1236 {
1237         struct task_struct *child;
1238         struct pt_regs *childregs;
1239         void __user *datap = compat_ptr(data);
1240         int ret;
1241         __u32 val;
1242
1243         switch (request) {
1244         case PTRACE_TRACEME:
1245         case PTRACE_ATTACH:
1246         case PTRACE_KILL:
1247         case PTRACE_CONT:
1248         case PTRACE_SINGLESTEP:
1249         case PTRACE_SINGLEBLOCK:
1250         case PTRACE_DETACH:
1251         case PTRACE_SYSCALL:
1252         case PTRACE_OLDSETOPTIONS:
1253         case PTRACE_SETOPTIONS:
1254         case PTRACE_SET_THREAD_AREA:
1255         case PTRACE_GET_THREAD_AREA:
1256 #ifdef X86_BTS
1257         case PTRACE_BTS_CONFIG:
1258         case PTRACE_BTS_STATUS:
1259         case PTRACE_BTS_SIZE:
1260         case PTRACE_BTS_GET:
1261         case PTRACE_BTS_CLEAR:
1262         case PTRACE_BTS_DRAIN:
1263 #endif
1264                 return sys_ptrace(request, pid, addr, data);
1265
1266         default:
1267                 return -EINVAL;
1268
1269         case PTRACE_PEEKTEXT:
1270         case PTRACE_PEEKDATA:
1271         case PTRACE_POKEDATA:
1272         case PTRACE_POKETEXT:
1273         case PTRACE_POKEUSR:
1274         case PTRACE_PEEKUSR:
1275         case PTRACE_GETREGS:
1276         case PTRACE_SETREGS:
1277         case PTRACE_SETFPREGS:
1278         case PTRACE_GETFPREGS:
1279         case PTRACE_SETFPXREGS:
1280         case PTRACE_GETFPXREGS:
1281         case PTRACE_GETEVENTMSG:
1282                 break;
1283
1284         case PTRACE_SETSIGINFO:
1285         case PTRACE_GETSIGINFO:
1286                 return ptrace32_siginfo(request, pid, addr, data);
1287         }
1288
1289         child = ptrace_get_task_struct(pid);
1290         if (IS_ERR(child))
1291                 return PTR_ERR(child);
1292
1293         ret = ptrace_check_attach(child, request == PTRACE_KILL);
1294         if (ret < 0)
1295                 goto out;
1296
1297         childregs = task_pt_regs(child);
1298
1299         switch (request) {
1300         case PTRACE_PEEKUSR:
1301                 ret = getreg32(child, addr, &val);
1302                 if (ret == 0)
1303                         ret = put_user(val, (__u32 __user *)datap);
1304                 break;
1305
1306         case PTRACE_POKEUSR:
1307                 ret = putreg32(child, addr, data);
1308                 break;
1309
1310         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
1311                 return copy_regset_to_user(child, &user_x86_32_view,
1312                                            REGSET_GENERAL,
1313                                            0, sizeof(struct user_regs_struct32),
1314                                            datap);
1315
1316         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
1317                 return copy_regset_from_user(child, &user_x86_32_view,
1318                                              REGSET_GENERAL, 0,
1319                                              sizeof(struct user_regs_struct32),
1320                                              datap);
1321
1322         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
1323                 return copy_regset_to_user(child, &user_x86_32_view,
1324                                            REGSET_FP, 0,
1325                                            sizeof(struct user_i387_ia32_struct),
1326                                            datap);
1327
1328         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
1329                 return copy_regset_from_user(
1330                         child, &user_x86_32_view, REGSET_FP,
1331                         0, sizeof(struct user_i387_ia32_struct), datap);
1332
1333         case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1334                 return copy_regset_to_user(child, &user_x86_32_view,
1335                                            REGSET_XFP, 0,
1336                                            sizeof(struct user32_fxsr_struct),
1337                                            datap);
1338
1339         case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1340                 return copy_regset_from_user(child, &user_x86_32_view,
1341                                              REGSET_XFP, 0,
1342                                              sizeof(struct user32_fxsr_struct),
1343                                              datap);
1344
1345         default:
1346                 return compat_ptrace_request(child, request, addr, data);
1347         }
1348
1349  out:
1350         put_task_struct(child);
1351         return ret;
1352 }
1353
1354 #endif  /* CONFIG_IA32_EMULATION */
1355
1356 #ifdef CONFIG_X86_64
1357
1358 static const struct user_regset x86_64_regsets[] = {
1359         [REGSET_GENERAL] = {
1360                 .core_note_type = NT_PRSTATUS,
1361                 .n = sizeof(struct user_regs_struct) / sizeof(long),
1362                 .size = sizeof(long), .align = sizeof(long),
1363                 .get = genregs_get, .set = genregs_set
1364         },
1365         [REGSET_FP] = {
1366                 .core_note_type = NT_PRFPREG,
1367                 .n = sizeof(struct user_i387_struct) / sizeof(long),
1368                 .size = sizeof(long), .align = sizeof(long),
1369                 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1370         },
1371 };
1372
1373 static const struct user_regset_view user_x86_64_view = {
1374         .name = "x86_64", .e_machine = EM_X86_64,
1375         .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1376 };
1377
1378 #else  /* CONFIG_X86_32 */
1379
1380 #define user_regs_struct32      user_regs_struct
1381 #define genregs32_get           genregs_get
1382 #define genregs32_set           genregs_set
1383
1384 #endif  /* CONFIG_X86_64 */
1385
1386 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1387 static const struct user_regset x86_32_regsets[] = {
1388         [REGSET_GENERAL] = {
1389                 .core_note_type = NT_PRSTATUS,
1390                 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1391                 .size = sizeof(u32), .align = sizeof(u32),
1392                 .get = genregs32_get, .set = genregs32_set
1393         },
1394         [REGSET_FP] = {
1395                 .core_note_type = NT_PRFPREG,
1396                 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1397                 .size = sizeof(u32), .align = sizeof(u32),
1398                 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1399         },
1400         [REGSET_XFP] = {
1401                 .core_note_type = NT_PRXFPREG,
1402                 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1403                 .size = sizeof(u32), .align = sizeof(u32),
1404                 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1405         },
1406         [REGSET_TLS] = {
1407                 .core_note_type = NT_386_TLS,
1408                 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1409                 .size = sizeof(struct user_desc),
1410                 .align = sizeof(struct user_desc),
1411                 .active = regset_tls_active,
1412                 .get = regset_tls_get, .set = regset_tls_set
1413         },
1414 };
1415
1416 static const struct user_regset_view user_x86_32_view = {
1417         .name = "i386", .e_machine = EM_386,
1418         .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1419 };
1420 #endif
1421
1422 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1423 {
1424 #ifdef CONFIG_IA32_EMULATION
1425         if (test_tsk_thread_flag(task, TIF_IA32))
1426 #endif
1427 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1428                 return &user_x86_32_view;
1429 #endif
1430 #ifdef CONFIG_X86_64
1431         return &user_x86_64_view;
1432 #endif
1433 }
1434
1435 #ifdef CONFIG_X86_32
1436
1437 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1438 {
1439         struct siginfo info;
1440
1441         tsk->thread.trap_no = 1;
1442         tsk->thread.error_code = error_code;
1443
1444         memset(&info, 0, sizeof(info));
1445         info.si_signo = SIGTRAP;
1446         info.si_code = TRAP_BRKPT;
1447
1448         /* User-mode ip? */
1449         info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1450
1451         /* Send us the fake SIGTRAP */
1452         force_sig_info(SIGTRAP, &info, tsk);
1453 }
1454
1455 /* notification of system call entry/exit
1456  * - triggered by current->work.syscall_trace
1457  */
1458 __attribute__((regparm(3)))
1459 int do_syscall_trace(struct pt_regs *regs, int entryexit)
1460 {
1461         int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1462         /*
1463          * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1464          * interception
1465          */
1466         int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
1467         int ret = 0;
1468
1469         /* do the secure computing check first */
1470         if (!entryexit)
1471                 secure_computing(regs->orig_ax);
1472
1473         if (unlikely(current->audit_context)) {
1474                 if (entryexit)
1475                         audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1476                                                 regs->ax);
1477                 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1478                  * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1479                  * not used, entry.S will call us only on syscall exit, not
1480                  * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1481                  * calling send_sigtrap() on syscall entry.
1482                  *
1483                  * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1484                  * is_singlestep is false, despite his name, so we will still do
1485                  * the correct thing.
1486                  */
1487                 else if (is_singlestep)
1488                         goto out;
1489         }
1490
1491         if (!(current->ptrace & PT_PTRACED))
1492                 goto out;
1493
1494         /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1495          * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1496          * here. We have to check this and return */
1497         if (is_sysemu && entryexit)
1498                 return 0;
1499
1500         /* Fake a debug trap */
1501         if (is_singlestep)
1502                 send_sigtrap(current, regs, 0);
1503
1504         if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
1505                 goto out;
1506
1507         /* the 0x80 provides a way for the tracing parent to distinguish
1508            between a syscall stop and SIGTRAP delivery */
1509         /* Note that the debugger could change the result of test_thread_flag!*/
1510         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
1511
1512         /*
1513          * this isn't the same as continuing with a signal, but it will do
1514          * for normal use.  strace only continues with a signal if the
1515          * stopping signal is not SIGTRAP.  -brl
1516          */
1517         if (current->exit_code) {
1518                 send_sig(current->exit_code, current, 1);
1519                 current->exit_code = 0;
1520         }
1521         ret = is_sysemu;
1522 out:
1523         if (unlikely(current->audit_context) && !entryexit)
1524                 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1525                                     regs->bx, regs->cx, regs->dx, regs->si);
1526         if (ret == 0)
1527                 return 0;
1528
1529         regs->orig_ax = -1; /* force skip of syscall restarting */
1530         if (unlikely(current->audit_context))
1531                 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1532         return 1;
1533 }
1534
1535 #else  /* CONFIG_X86_64 */
1536
1537 static void syscall_trace(struct pt_regs *regs)
1538 {
1539
1540 #if 0
1541         printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1542                current->comm,
1543                regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1544                current_thread_info()->flags, current->ptrace);
1545 #endif
1546
1547         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1548                                 ? 0x80 : 0));
1549         /*
1550          * this isn't the same as continuing with a signal, but it will do
1551          * for normal use.  strace only continues with a signal if the
1552          * stopping signal is not SIGTRAP.  -brl
1553          */
1554         if (current->exit_code) {
1555                 send_sig(current->exit_code, current, 1);
1556                 current->exit_code = 0;
1557         }
1558 }
1559
1560 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1561 {
1562         /* do the secure computing check first */
1563         secure_computing(regs->orig_ax);
1564
1565         if (test_thread_flag(TIF_SYSCALL_TRACE)
1566             && (current->ptrace & PT_PTRACED))
1567                 syscall_trace(regs);
1568
1569         if (unlikely(current->audit_context)) {
1570                 if (test_thread_flag(TIF_IA32)) {
1571                         audit_syscall_entry(AUDIT_ARCH_I386,
1572                                             regs->orig_ax,
1573                                             regs->bx, regs->cx,
1574                                             regs->dx, regs->si);
1575                 } else {
1576                         audit_syscall_entry(AUDIT_ARCH_X86_64,
1577                                             regs->orig_ax,
1578                                             regs->di, regs->si,
1579                                             regs->dx, regs->r10);
1580                 }
1581         }
1582 }
1583
1584 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1585 {
1586         if (unlikely(current->audit_context))
1587                 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1588
1589         if ((test_thread_flag(TIF_SYSCALL_TRACE)
1590              || test_thread_flag(TIF_SINGLESTEP))
1591             && (current->ptrace & PT_PTRACED))
1592                 syscall_trace(regs);
1593 }
1594
1595 #endif  /* CONFIG_X86_32 */