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