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