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