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