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