mm, x86, ptrace, bts: defer branch trace stopping
[linux-2.6/kvm.git] / arch / x86 / kernel / ptrace.c
blob7c21d1e8cae79915bcf7b130056ad246a2de033c
1 /* By Ross Biro 1/23/92 */
2 /*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
8 */
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/ftrace.h>
25 #include <linux/workqueue.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/system.h>
30 #include <asm/processor.h>
31 #include <asm/i387.h>
32 #include <asm/debugreg.h>
33 #include <asm/ldt.h>
34 #include <asm/desc.h>
35 #include <asm/prctl.h>
36 #include <asm/proto.h>
37 #include <asm/ds.h>
39 #include "tls.h"
41 enum x86_regset {
42 REGSET_GENERAL,
43 REGSET_FP,
44 REGSET_XFP,
45 REGSET_IOPERM64 = REGSET_XFP,
46 REGSET_TLS,
47 REGSET_IOPERM32,
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
56 * Determines which flags the user has access to [1 = access, 0 = no access].
58 #define FLAG_MASK_32 ((unsigned long) \
59 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
60 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
61 X86_EFLAGS_SF | X86_EFLAGS_TF | \
62 X86_EFLAGS_DF | X86_EFLAGS_OF | \
63 X86_EFLAGS_RF | X86_EFLAGS_AC))
66 * Determines whether a value may be installed in a segment register.
68 static inline bool invalid_selector(u16 value)
70 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
73 #ifdef CONFIG_X86_32
75 #define FLAG_MASK FLAG_MASK_32
77 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
79 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
80 return &regs->bx + (regno >> 2);
83 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
86 * Returning the value truncates it to 16 bits.
88 unsigned int retval;
89 if (offset != offsetof(struct user_regs_struct, gs))
90 retval = *pt_regs_access(task_pt_regs(task), offset);
91 else {
92 if (task == current)
93 retval = get_user_gs(task_pt_regs(task));
94 else
95 retval = task_user_gs(task);
97 return retval;
100 static int set_segment_reg(struct task_struct *task,
101 unsigned long offset, u16 value)
104 * The value argument was already truncated to 16 bits.
106 if (invalid_selector(value))
107 return -EIO;
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.
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;
124 default:
125 *pt_regs_access(task_pt_regs(task), offset) = value;
126 break;
128 case offsetof(struct user_regs_struct, gs):
129 if (task == current)
130 set_user_gs(task_pt_regs(task), value);
131 else
132 task_user_gs(task) = value;
135 return 0;
138 static unsigned long debugreg_addr_limit(struct task_struct *task)
140 return TASK_SIZE - 3;
143 #else /* CONFIG_X86_64 */
145 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
147 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
149 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
150 return &regs->r15 + (offset / sizeof(regs->r15));
153 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
156 * Returning the value truncates it to 16 bits.
158 unsigned int seg;
160 switch (offset) {
161 case offsetof(struct user_regs_struct, fs):
162 if (task == current) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg));
165 return seg;
167 return task->thread.fsindex;
168 case offsetof(struct user_regs_struct, gs):
169 if (task == current) {
170 asm("movl %%gs,%0" : "=r" (seg));
171 return seg;
173 return task->thread.gsindex;
174 case offsetof(struct user_regs_struct, ds):
175 if (task == current) {
176 asm("movl %%ds,%0" : "=r" (seg));
177 return seg;
179 return task->thread.ds;
180 case offsetof(struct user_regs_struct, es):
181 if (task == current) {
182 asm("movl %%es,%0" : "=r" (seg));
183 return seg;
185 return task->thread.es;
187 case offsetof(struct user_regs_struct, cs):
188 case offsetof(struct user_regs_struct, ss):
189 break;
191 return *pt_regs_access(task_pt_regs(task), offset);
194 static int set_segment_reg(struct task_struct *task,
195 unsigned long offset, u16 value)
198 * The value argument was already truncated to 16 bits.
200 if (invalid_selector(value))
201 return -EIO;
203 switch (offset) {
204 case offsetof(struct user_regs_struct,fs):
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
209 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
210 task->thread.fs != 0) ||
211 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
212 task->thread.fs == 0))
213 break;
214 task->thread.fsindex = value;
215 if (task == current)
216 loadsegment(fs, task->thread.fsindex);
217 break;
218 case offsetof(struct user_regs_struct,gs):
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
223 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
224 task->thread.gs != 0) ||
225 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
226 task->thread.gs == 0))
227 break;
228 task->thread.gsindex = value;
229 if (task == current)
230 load_gs_index(task->thread.gsindex);
231 break;
232 case offsetof(struct user_regs_struct,ds):
233 task->thread.ds = value;
234 if (task == current)
235 loadsegment(ds, task->thread.ds);
236 break;
237 case offsetof(struct user_regs_struct,es):
238 task->thread.es = value;
239 if (task == current)
240 loadsegment(es, task->thread.es);
241 break;
244 * Can't actually change these in 64-bit mode.
246 case offsetof(struct user_regs_struct,cs):
247 if (unlikely(value == 0))
248 return -EIO;
249 #ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task, TIF_IA32))
251 task_pt_regs(task)->cs = value;
252 #endif
253 break;
254 case offsetof(struct user_regs_struct,ss):
255 if (unlikely(value == 0))
256 return -EIO;
257 #ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task, TIF_IA32))
259 task_pt_regs(task)->ss = value;
260 #endif
261 break;
264 return 0;
267 static unsigned long debugreg_addr_limit(struct task_struct *task)
269 #ifdef CONFIG_IA32_EMULATION
270 if (test_tsk_thread_flag(task, TIF_IA32))
271 return IA32_PAGE_OFFSET - 3;
272 #endif
273 return TASK_SIZE_MAX - 7;
276 #endif /* CONFIG_X86_32 */
278 static unsigned long get_flags(struct task_struct *task)
280 unsigned long retval = task_pt_regs(task)->flags;
283 * If the debugger set TF, hide it from the readout.
285 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
286 retval &= ~X86_EFLAGS_TF;
288 return retval;
291 static int set_flags(struct task_struct *task, unsigned long value)
293 struct pt_regs *regs = task_pt_regs(task);
296 * If the user value contains TF, mark that
297 * it was not "us" (the debugger) that set it.
298 * If not, make sure it stays set if we had.
300 if (value & X86_EFLAGS_TF)
301 clear_tsk_thread_flag(task, TIF_FORCED_TF);
302 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
303 value |= X86_EFLAGS_TF;
305 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
307 return 0;
310 static int putreg(struct task_struct *child,
311 unsigned long offset, unsigned long value)
313 switch (offset) {
314 case offsetof(struct user_regs_struct, cs):
315 case offsetof(struct user_regs_struct, ds):
316 case offsetof(struct user_regs_struct, es):
317 case offsetof(struct user_regs_struct, fs):
318 case offsetof(struct user_regs_struct, gs):
319 case offsetof(struct user_regs_struct, ss):
320 return set_segment_reg(child, offset, value);
322 case offsetof(struct user_regs_struct, flags):
323 return set_flags(child, value);
325 #ifdef CONFIG_X86_64
327 * Orig_ax is really just a flag with small positive and
328 * negative values, so make sure to always sign-extend it
329 * from 32 bits so that it works correctly regardless of
330 * whether we come from a 32-bit environment or not.
332 case offsetof(struct user_regs_struct, orig_ax):
333 value = (long) (s32) value;
334 break;
336 case offsetof(struct user_regs_struct,fs_base):
337 if (value >= TASK_SIZE_OF(child))
338 return -EIO;
340 * When changing the segment base, use do_arch_prctl
341 * to set either thread.fs or thread.fsindex and the
342 * corresponding GDT slot.
344 if (child->thread.fs != value)
345 return do_arch_prctl(child, ARCH_SET_FS, value);
346 return 0;
347 case offsetof(struct user_regs_struct,gs_base):
349 * Exactly the same here as the %fs handling above.
351 if (value >= TASK_SIZE_OF(child))
352 return -EIO;
353 if (child->thread.gs != value)
354 return do_arch_prctl(child, ARCH_SET_GS, value);
355 return 0;
356 #endif
359 *pt_regs_access(task_pt_regs(child), offset) = value;
360 return 0;
363 static unsigned long getreg(struct task_struct *task, unsigned long offset)
365 switch (offset) {
366 case offsetof(struct user_regs_struct, cs):
367 case offsetof(struct user_regs_struct, ds):
368 case offsetof(struct user_regs_struct, es):
369 case offsetof(struct user_regs_struct, fs):
370 case offsetof(struct user_regs_struct, gs):
371 case offsetof(struct user_regs_struct, ss):
372 return get_segment_reg(task, offset);
374 case offsetof(struct user_regs_struct, flags):
375 return get_flags(task);
377 #ifdef CONFIG_X86_64
378 case offsetof(struct user_regs_struct, fs_base): {
380 * do_arch_prctl may have used a GDT slot instead of
381 * the MSR. To userland, it appears the same either
382 * way, except the %fs segment selector might not be 0.
384 unsigned int seg = task->thread.fsindex;
385 if (task->thread.fs != 0)
386 return task->thread.fs;
387 if (task == current)
388 asm("movl %%fs,%0" : "=r" (seg));
389 if (seg != FS_TLS_SEL)
390 return 0;
391 return get_desc_base(&task->thread.tls_array[FS_TLS]);
393 case offsetof(struct user_regs_struct, gs_base): {
395 * Exactly the same here as the %fs handling above.
397 unsigned int seg = task->thread.gsindex;
398 if (task->thread.gs != 0)
399 return task->thread.gs;
400 if (task == current)
401 asm("movl %%gs,%0" : "=r" (seg));
402 if (seg != GS_TLS_SEL)
403 return 0;
404 return get_desc_base(&task->thread.tls_array[GS_TLS]);
406 #endif
409 return *pt_regs_access(task_pt_regs(task), offset);
412 static int genregs_get(struct task_struct *target,
413 const struct user_regset *regset,
414 unsigned int pos, unsigned int count,
415 void *kbuf, void __user *ubuf)
417 if (kbuf) {
418 unsigned long *k = kbuf;
419 while (count > 0) {
420 *k++ = getreg(target, pos);
421 count -= sizeof(*k);
422 pos += sizeof(*k);
424 } else {
425 unsigned long __user *u = ubuf;
426 while (count > 0) {
427 if (__put_user(getreg(target, pos), u++))
428 return -EFAULT;
429 count -= sizeof(*u);
430 pos += sizeof(*u);
434 return 0;
437 static int genregs_set(struct task_struct *target,
438 const struct user_regset *regset,
439 unsigned int pos, unsigned int count,
440 const void *kbuf, const void __user *ubuf)
442 int ret = 0;
443 if (kbuf) {
444 const unsigned long *k = kbuf;
445 while (count > 0 && !ret) {
446 ret = putreg(target, pos, *k++);
447 count -= sizeof(*k);
448 pos += sizeof(*k);
450 } else {
451 const unsigned long __user *u = ubuf;
452 while (count > 0 && !ret) {
453 unsigned long word;
454 ret = __get_user(word, u++);
455 if (ret)
456 break;
457 ret = putreg(target, pos, word);
458 count -= sizeof(*u);
459 pos += sizeof(*u);
462 return ret;
466 * This function is trivial and will be inlined by the compiler.
467 * Having it separates the implementation details of debug
468 * registers from the interface details of ptrace.
470 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
472 switch (n) {
473 case 0: return child->thread.debugreg0;
474 case 1: return child->thread.debugreg1;
475 case 2: return child->thread.debugreg2;
476 case 3: return child->thread.debugreg3;
477 case 6: return child->thread.debugreg6;
478 case 7: return child->thread.debugreg7;
480 return 0;
483 static int ptrace_set_debugreg(struct task_struct *child,
484 int n, unsigned long data)
486 int i;
488 if (unlikely(n == 4 || n == 5))
489 return -EIO;
491 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
492 return -EIO;
494 switch (n) {
495 case 0: child->thread.debugreg0 = data; break;
496 case 1: child->thread.debugreg1 = data; break;
497 case 2: child->thread.debugreg2 = data; break;
498 case 3: child->thread.debugreg3 = data; break;
500 case 6:
501 if ((data & ~0xffffffffUL) != 0)
502 return -EIO;
503 child->thread.debugreg6 = data;
504 break;
506 case 7:
508 * Sanity-check data. Take one half-byte at once with
509 * check = (val >> (16 + 4*i)) & 0xf. It contains the
510 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
511 * 2 and 3 are LENi. Given a list of invalid values,
512 * we do mask |= 1 << invalid_value, so that
513 * (mask >> check) & 1 is a correct test for invalid
514 * values.
516 * R/Wi contains the type of the breakpoint /
517 * watchpoint, LENi contains the length of the watched
518 * data in the watchpoint case.
520 * The invalid values are:
521 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
522 * - R/Wi == 0x10 (break on I/O reads or writes), so
523 * mask |= 0x4444.
524 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
525 * 0x1110.
527 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
529 * See the Intel Manual "System Programming Guide",
530 * 15.2.4
532 * Note that LENi == 0x10 is defined on x86_64 in long
533 * mode (i.e. even for 32-bit userspace software, but
534 * 64-bit kernel), so the x86_64 mask value is 0x5454.
535 * See the AMD manual no. 24593 (AMD64 System Programming)
537 #ifdef CONFIG_X86_32
538 #define DR7_MASK 0x5f54
539 #else
540 #define DR7_MASK 0x5554
541 #endif
542 data &= ~DR_CONTROL_RESERVED;
543 for (i = 0; i < 4; i++)
544 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
545 return -EIO;
546 child->thread.debugreg7 = data;
547 if (data)
548 set_tsk_thread_flag(child, TIF_DEBUG);
549 else
550 clear_tsk_thread_flag(child, TIF_DEBUG);
551 break;
554 return 0;
558 * These access the current or another (stopped) task's io permission
559 * bitmap for debugging or core dump.
561 static int ioperm_active(struct task_struct *target,
562 const struct user_regset *regset)
564 return target->thread.io_bitmap_max / regset->size;
567 static int ioperm_get(struct task_struct *target,
568 const struct user_regset *regset,
569 unsigned int pos, unsigned int count,
570 void *kbuf, void __user *ubuf)
572 if (!target->thread.io_bitmap_ptr)
573 return -ENXIO;
575 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
576 target->thread.io_bitmap_ptr,
577 0, IO_BITMAP_BYTES);
580 #ifdef CONFIG_X86_PTRACE_BTS
582 * A branch trace store context.
584 * Contexts may only be installed by ptrace_bts_config() and only for
585 * ptraced tasks.
587 * Contexts are destroyed when the tracee is detached from the tracer.
588 * The actual destruction work requires interrupts enabled, so the
589 * work is deferred and will be scheduled during __ptrace_unlink().
591 * Contexts hold an additional task_struct reference on the traced
592 * task, as well as a reference on the tracer's mm.
594 * Ptrace already holds a task_struct for the duration of ptrace operations,
595 * but since destruction is deferred, it may be executed after both
596 * tracer and tracee exited.
598 struct bts_context {
599 /* The branch trace handle. */
600 struct bts_tracer *tracer;
602 /* The buffer used to store the branch trace and its size. */
603 void *buffer;
604 unsigned int size;
606 /* The mm that paid for the above buffer. */
607 struct mm_struct *mm;
609 /* The task this context belongs to. */
610 struct task_struct *task;
612 /* The signal to send on a bts buffer overflow. */
613 unsigned int bts_ovfl_signal;
615 /* The work struct to destroy a context. */
616 struct work_struct work;
619 static inline void alloc_bts_buffer(struct bts_context *context,
620 unsigned int size)
622 void *buffer;
624 buffer = alloc_locked_buffer(size);
625 if (buffer) {
626 context->buffer = buffer;
627 context->size = size;
628 context->mm = get_task_mm(current);
632 static inline void free_bts_buffer(struct bts_context *context)
634 if (!context->buffer)
635 return;
637 kfree(context->buffer);
638 context->buffer = NULL;
640 refund_locked_buffer_memory(context->mm, context->size);
641 context->size = 0;
643 mmput(context->mm);
644 context->mm = NULL;
647 static void free_bts_context_work(struct work_struct *w)
649 struct bts_context *context;
651 context = container_of(w, struct bts_context, work);
653 ds_release_bts(context->tracer);
654 put_task_struct(context->task);
655 free_bts_buffer(context);
656 kfree(context);
659 static inline void free_bts_context(struct bts_context *context)
661 INIT_WORK(&context->work, free_bts_context_work);
662 schedule_work(&context->work);
665 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
667 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
668 if (context) {
669 context->task = task;
670 task->bts = context;
672 get_task_struct(task);
675 return context;
678 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
679 struct bts_struct __user *out)
681 struct bts_context *context;
682 const struct bts_trace *trace;
683 struct bts_struct bts;
684 const unsigned char *at;
685 int error;
687 context = child->bts;
688 if (!context)
689 return -ESRCH;
691 trace = ds_read_bts(context->tracer);
692 if (!trace)
693 return -ESRCH;
695 at = trace->ds.top - ((index + 1) * trace->ds.size);
696 if ((void *)at < trace->ds.begin)
697 at += (trace->ds.n * trace->ds.size);
699 if (!trace->read)
700 return -EOPNOTSUPP;
702 error = trace->read(context->tracer, at, &bts);
703 if (error < 0)
704 return error;
706 if (copy_to_user(out, &bts, sizeof(bts)))
707 return -EFAULT;
709 return sizeof(bts);
712 static int ptrace_bts_drain(struct task_struct *child,
713 long size,
714 struct bts_struct __user *out)
716 struct bts_context *context;
717 const struct bts_trace *trace;
718 const unsigned char *at;
719 int error, drained = 0;
721 context = child->bts;
722 if (!context)
723 return -ESRCH;
725 trace = ds_read_bts(context->tracer);
726 if (!trace)
727 return -ESRCH;
729 if (!trace->read)
730 return -EOPNOTSUPP;
732 if (size < (trace->ds.top - trace->ds.begin))
733 return -EIO;
735 for (at = trace->ds.begin; (void *)at < trace->ds.top;
736 out++, drained++, at += trace->ds.size) {
737 struct bts_struct bts;
739 error = trace->read(context->tracer, at, &bts);
740 if (error < 0)
741 return error;
743 if (copy_to_user(out, &bts, sizeof(bts)))
744 return -EFAULT;
747 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
749 error = ds_reset_bts(context->tracer);
750 if (error < 0)
751 return error;
753 return drained;
756 static int ptrace_bts_config(struct task_struct *child,
757 long cfg_size,
758 const struct ptrace_bts_config __user *ucfg)
760 struct bts_context *context;
761 struct ptrace_bts_config cfg;
762 unsigned int flags = 0;
764 if (cfg_size < sizeof(cfg))
765 return -EIO;
767 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
768 return -EFAULT;
770 context = child->bts;
771 if (!context)
772 context = alloc_bts_context(child);
773 if (!context)
774 return -ENOMEM;
776 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
777 if (!cfg.signal)
778 return -EINVAL;
780 return -EOPNOTSUPP;
781 context->bts_ovfl_signal = cfg.signal;
784 ds_release_bts(context->tracer);
785 context->tracer = NULL;
787 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
788 free_bts_buffer(context);
789 if (!cfg.size)
790 return 0;
792 alloc_bts_buffer(context, cfg.size);
793 if (!context->buffer)
794 return -ENOMEM;
797 if (cfg.flags & PTRACE_BTS_O_TRACE)
798 flags |= BTS_USER;
800 if (cfg.flags & PTRACE_BTS_O_SCHED)
801 flags |= BTS_TIMESTAMPS;
803 context->tracer = ds_request_bts(child, context->buffer, context->size,
804 NULL, (size_t)-1, flags);
805 if (unlikely(IS_ERR(context->tracer))) {
806 int error = PTR_ERR(context->tracer);
808 free_bts_buffer(context);
809 context->tracer = NULL;
810 return error;
813 return sizeof(cfg);
816 static int ptrace_bts_status(struct task_struct *child,
817 long cfg_size,
818 struct ptrace_bts_config __user *ucfg)
820 struct bts_context *context;
821 const struct bts_trace *trace;
822 struct ptrace_bts_config cfg;
824 context = child->bts;
825 if (!context)
826 return -ESRCH;
828 if (cfg_size < sizeof(cfg))
829 return -EIO;
831 trace = ds_read_bts(context->tracer);
832 if (!trace)
833 return -ESRCH;
835 memset(&cfg, 0, sizeof(cfg));
836 cfg.size = trace->ds.end - trace->ds.begin;
837 cfg.signal = context->bts_ovfl_signal;
838 cfg.bts_size = sizeof(struct bts_struct);
840 if (cfg.signal)
841 cfg.flags |= PTRACE_BTS_O_SIGNAL;
843 if (trace->ds.flags & BTS_USER)
844 cfg.flags |= PTRACE_BTS_O_TRACE;
846 if (trace->ds.flags & BTS_TIMESTAMPS)
847 cfg.flags |= PTRACE_BTS_O_SCHED;
849 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
850 return -EFAULT;
852 return sizeof(cfg);
855 static int ptrace_bts_clear(struct task_struct *child)
857 struct bts_context *context;
858 const struct bts_trace *trace;
860 context = child->bts;
861 if (!context)
862 return -ESRCH;
864 trace = ds_read_bts(context->tracer);
865 if (!trace)
866 return -ESRCH;
868 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
870 return ds_reset_bts(context->tracer);
873 static int ptrace_bts_size(struct task_struct *child)
875 struct bts_context *context;
876 const struct bts_trace *trace;
878 context = child->bts;
879 if (!context)
880 return -ESRCH;
882 trace = ds_read_bts(context->tracer);
883 if (!trace)
884 return -ESRCH;
886 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
889 static inline void ptrace_bts_fork(struct task_struct *tsk)
891 tsk->bts = NULL;
895 * Called from __ptrace_unlink() after the child has been moved back
896 * to its original parent.
898 static inline void ptrace_bts_untrace(struct task_struct *child)
900 if (unlikely(child->bts)) {
901 free_bts_context(child->bts);
902 child->bts = NULL;
905 #else
906 static inline void ptrace_bts_fork(struct task_struct *tsk) {}
907 static inline void ptrace_bts_untrace(struct task_struct *child) {}
908 #endif /* CONFIG_X86_PTRACE_BTS */
910 void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
912 ptrace_bts_fork(child);
915 void x86_ptrace_untrace(struct task_struct *child)
917 ptrace_bts_untrace(child);
921 * Called by kernel/ptrace.c when detaching..
923 * Make sure the single step bit is not set.
925 void ptrace_disable(struct task_struct *child)
927 user_disable_single_step(child);
928 #ifdef TIF_SYSCALL_EMU
929 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
930 #endif
933 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
934 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
935 #endif
937 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
939 int ret;
940 unsigned long __user *datap = (unsigned long __user *)data;
942 switch (request) {
943 /* read the word at location addr in the USER area. */
944 case PTRACE_PEEKUSR: {
945 unsigned long tmp;
947 ret = -EIO;
948 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
949 addr >= sizeof(struct user))
950 break;
952 tmp = 0; /* Default return condition */
953 if (addr < sizeof(struct user_regs_struct))
954 tmp = getreg(child, addr);
955 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
956 addr <= offsetof(struct user, u_debugreg[7])) {
957 addr -= offsetof(struct user, u_debugreg[0]);
958 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
960 ret = put_user(tmp, datap);
961 break;
964 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
965 ret = -EIO;
966 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
967 addr >= sizeof(struct user))
968 break;
970 if (addr < sizeof(struct user_regs_struct))
971 ret = putreg(child, addr, data);
972 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
973 addr <= offsetof(struct user, u_debugreg[7])) {
974 addr -= offsetof(struct user, u_debugreg[0]);
975 ret = ptrace_set_debugreg(child,
976 addr / sizeof(data), data);
978 break;
980 case PTRACE_GETREGS: /* Get all gp regs from the child. */
981 return copy_regset_to_user(child,
982 task_user_regset_view(current),
983 REGSET_GENERAL,
984 0, sizeof(struct user_regs_struct),
985 datap);
987 case PTRACE_SETREGS: /* Set all gp regs in the child. */
988 return copy_regset_from_user(child,
989 task_user_regset_view(current),
990 REGSET_GENERAL,
991 0, sizeof(struct user_regs_struct),
992 datap);
994 case PTRACE_GETFPREGS: /* Get the child FPU state. */
995 return copy_regset_to_user(child,
996 task_user_regset_view(current),
997 REGSET_FP,
998 0, sizeof(struct user_i387_struct),
999 datap);
1001 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1002 return copy_regset_from_user(child,
1003 task_user_regset_view(current),
1004 REGSET_FP,
1005 0, sizeof(struct user_i387_struct),
1006 datap);
1008 #ifdef CONFIG_X86_32
1009 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1010 return copy_regset_to_user(child, &user_x86_32_view,
1011 REGSET_XFP,
1012 0, sizeof(struct user_fxsr_struct),
1013 datap) ? -EIO : 0;
1015 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1016 return copy_regset_from_user(child, &user_x86_32_view,
1017 REGSET_XFP,
1018 0, sizeof(struct user_fxsr_struct),
1019 datap) ? -EIO : 0;
1020 #endif
1022 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1023 case PTRACE_GET_THREAD_AREA:
1024 if (addr < 0)
1025 return -EIO;
1026 ret = do_get_thread_area(child, addr,
1027 (struct user_desc __user *) data);
1028 break;
1030 case PTRACE_SET_THREAD_AREA:
1031 if (addr < 0)
1032 return -EIO;
1033 ret = do_set_thread_area(child, addr,
1034 (struct user_desc __user *) data, 0);
1035 break;
1036 #endif
1038 #ifdef CONFIG_X86_64
1039 /* normal 64bit interface to access TLS data.
1040 Works just like arch_prctl, except that the arguments
1041 are reversed. */
1042 case PTRACE_ARCH_PRCTL:
1043 ret = do_arch_prctl(child, data, addr);
1044 break;
1045 #endif
1048 * These bits need more cooking - not enabled yet:
1050 #ifdef CONFIG_X86_PTRACE_BTS
1051 case PTRACE_BTS_CONFIG:
1052 ret = ptrace_bts_config
1053 (child, data, (struct ptrace_bts_config __user *)addr);
1054 break;
1056 case PTRACE_BTS_STATUS:
1057 ret = ptrace_bts_status
1058 (child, data, (struct ptrace_bts_config __user *)addr);
1059 break;
1061 case PTRACE_BTS_SIZE:
1062 ret = ptrace_bts_size(child);
1063 break;
1065 case PTRACE_BTS_GET:
1066 ret = ptrace_bts_read_record
1067 (child, data, (struct bts_struct __user *) addr);
1068 break;
1070 case PTRACE_BTS_CLEAR:
1071 ret = ptrace_bts_clear(child);
1072 break;
1074 case PTRACE_BTS_DRAIN:
1075 ret = ptrace_bts_drain
1076 (child, data, (struct bts_struct __user *) addr);
1077 break;
1078 #endif /* CONFIG_X86_PTRACE_BTS */
1080 default:
1081 ret = ptrace_request(child, request, addr, data);
1082 break;
1085 return ret;
1088 #ifdef CONFIG_IA32_EMULATION
1090 #include <linux/compat.h>
1091 #include <linux/syscalls.h>
1092 #include <asm/ia32.h>
1093 #include <asm/user32.h>
1095 #define R32(l,q) \
1096 case offsetof(struct user32, regs.l): \
1097 regs->q = value; break
1099 #define SEG32(rs) \
1100 case offsetof(struct user32, regs.rs): \
1101 return set_segment_reg(child, \
1102 offsetof(struct user_regs_struct, rs), \
1103 value); \
1104 break
1106 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1108 struct pt_regs *regs = task_pt_regs(child);
1110 switch (regno) {
1112 SEG32(cs);
1113 SEG32(ds);
1114 SEG32(es);
1115 SEG32(fs);
1116 SEG32(gs);
1117 SEG32(ss);
1119 R32(ebx, bx);
1120 R32(ecx, cx);
1121 R32(edx, dx);
1122 R32(edi, di);
1123 R32(esi, si);
1124 R32(ebp, bp);
1125 R32(eax, ax);
1126 R32(eip, ip);
1127 R32(esp, sp);
1129 case offsetof(struct user32, regs.orig_eax):
1131 * Sign-extend the value so that orig_eax = -1
1132 * causes (long)orig_ax < 0 tests to fire correctly.
1134 regs->orig_ax = (long) (s32) value;
1135 break;
1137 case offsetof(struct user32, regs.eflags):
1138 return set_flags(child, value);
1140 case offsetof(struct user32, u_debugreg[0]) ...
1141 offsetof(struct user32, u_debugreg[7]):
1142 regno -= offsetof(struct user32, u_debugreg[0]);
1143 return ptrace_set_debugreg(child, regno / 4, value);
1145 default:
1146 if (regno > sizeof(struct user32) || (regno & 3))
1147 return -EIO;
1150 * Other dummy fields in the virtual user structure
1151 * are ignored
1153 break;
1155 return 0;
1158 #undef R32
1159 #undef SEG32
1161 #define R32(l,q) \
1162 case offsetof(struct user32, regs.l): \
1163 *val = regs->q; break
1165 #define SEG32(rs) \
1166 case offsetof(struct user32, regs.rs): \
1167 *val = get_segment_reg(child, \
1168 offsetof(struct user_regs_struct, rs)); \
1169 break
1171 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1173 struct pt_regs *regs = task_pt_regs(child);
1175 switch (regno) {
1177 SEG32(ds);
1178 SEG32(es);
1179 SEG32(fs);
1180 SEG32(gs);
1182 R32(cs, cs);
1183 R32(ss, ss);
1184 R32(ebx, bx);
1185 R32(ecx, cx);
1186 R32(edx, dx);
1187 R32(edi, di);
1188 R32(esi, si);
1189 R32(ebp, bp);
1190 R32(eax, ax);
1191 R32(orig_eax, orig_ax);
1192 R32(eip, ip);
1193 R32(esp, sp);
1195 case offsetof(struct user32, regs.eflags):
1196 *val = get_flags(child);
1197 break;
1199 case offsetof(struct user32, u_debugreg[0]) ...
1200 offsetof(struct user32, u_debugreg[7]):
1201 regno -= offsetof(struct user32, u_debugreg[0]);
1202 *val = ptrace_get_debugreg(child, regno / 4);
1203 break;
1205 default:
1206 if (regno > sizeof(struct user32) || (regno & 3))
1207 return -EIO;
1210 * Other dummy fields in the virtual user structure
1211 * are ignored
1213 *val = 0;
1214 break;
1216 return 0;
1219 #undef R32
1220 #undef SEG32
1222 static int genregs32_get(struct task_struct *target,
1223 const struct user_regset *regset,
1224 unsigned int pos, unsigned int count,
1225 void *kbuf, void __user *ubuf)
1227 if (kbuf) {
1228 compat_ulong_t *k = kbuf;
1229 while (count > 0) {
1230 getreg32(target, pos, k++);
1231 count -= sizeof(*k);
1232 pos += sizeof(*k);
1234 } else {
1235 compat_ulong_t __user *u = ubuf;
1236 while (count > 0) {
1237 compat_ulong_t word;
1238 getreg32(target, pos, &word);
1239 if (__put_user(word, u++))
1240 return -EFAULT;
1241 count -= sizeof(*u);
1242 pos += sizeof(*u);
1246 return 0;
1249 static int genregs32_set(struct task_struct *target,
1250 const struct user_regset *regset,
1251 unsigned int pos, unsigned int count,
1252 const void *kbuf, const void __user *ubuf)
1254 int ret = 0;
1255 if (kbuf) {
1256 const compat_ulong_t *k = kbuf;
1257 while (count > 0 && !ret) {
1258 ret = putreg32(target, pos, *k++);
1259 count -= sizeof(*k);
1260 pos += sizeof(*k);
1262 } else {
1263 const compat_ulong_t __user *u = ubuf;
1264 while (count > 0 && !ret) {
1265 compat_ulong_t word;
1266 ret = __get_user(word, u++);
1267 if (ret)
1268 break;
1269 ret = putreg32(target, pos, word);
1270 count -= sizeof(*u);
1271 pos += sizeof(*u);
1274 return ret;
1277 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1278 compat_ulong_t caddr, compat_ulong_t cdata)
1280 unsigned long addr = caddr;
1281 unsigned long data = cdata;
1282 void __user *datap = compat_ptr(data);
1283 int ret;
1284 __u32 val;
1286 switch (request) {
1287 case PTRACE_PEEKUSR:
1288 ret = getreg32(child, addr, &val);
1289 if (ret == 0)
1290 ret = put_user(val, (__u32 __user *)datap);
1291 break;
1293 case PTRACE_POKEUSR:
1294 ret = putreg32(child, addr, data);
1295 break;
1297 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1298 return copy_regset_to_user(child, &user_x86_32_view,
1299 REGSET_GENERAL,
1300 0, sizeof(struct user_regs_struct32),
1301 datap);
1303 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1304 return copy_regset_from_user(child, &user_x86_32_view,
1305 REGSET_GENERAL, 0,
1306 sizeof(struct user_regs_struct32),
1307 datap);
1309 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1310 return copy_regset_to_user(child, &user_x86_32_view,
1311 REGSET_FP, 0,
1312 sizeof(struct user_i387_ia32_struct),
1313 datap);
1315 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1316 return copy_regset_from_user(
1317 child, &user_x86_32_view, REGSET_FP,
1318 0, sizeof(struct user_i387_ia32_struct), datap);
1320 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1321 return copy_regset_to_user(child, &user_x86_32_view,
1322 REGSET_XFP, 0,
1323 sizeof(struct user32_fxsr_struct),
1324 datap);
1326 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1327 return copy_regset_from_user(child, &user_x86_32_view,
1328 REGSET_XFP, 0,
1329 sizeof(struct user32_fxsr_struct),
1330 datap);
1332 case PTRACE_GET_THREAD_AREA:
1333 case PTRACE_SET_THREAD_AREA:
1334 #ifdef CONFIG_X86_PTRACE_BTS
1335 case PTRACE_BTS_CONFIG:
1336 case PTRACE_BTS_STATUS:
1337 case PTRACE_BTS_SIZE:
1338 case PTRACE_BTS_GET:
1339 case PTRACE_BTS_CLEAR:
1340 case PTRACE_BTS_DRAIN:
1341 #endif /* CONFIG_X86_PTRACE_BTS */
1342 return arch_ptrace(child, request, addr, data);
1344 default:
1345 return compat_ptrace_request(child, request, addr, data);
1348 return ret;
1351 #endif /* CONFIG_IA32_EMULATION */
1353 #ifdef CONFIG_X86_64
1355 static const struct user_regset x86_64_regsets[] = {
1356 [REGSET_GENERAL] = {
1357 .core_note_type = NT_PRSTATUS,
1358 .n = sizeof(struct user_regs_struct) / sizeof(long),
1359 .size = sizeof(long), .align = sizeof(long),
1360 .get = genregs_get, .set = genregs_set
1362 [REGSET_FP] = {
1363 .core_note_type = NT_PRFPREG,
1364 .n = sizeof(struct user_i387_struct) / sizeof(long),
1365 .size = sizeof(long), .align = sizeof(long),
1366 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1368 [REGSET_IOPERM64] = {
1369 .core_note_type = NT_386_IOPERM,
1370 .n = IO_BITMAP_LONGS,
1371 .size = sizeof(long), .align = sizeof(long),
1372 .active = ioperm_active, .get = ioperm_get
1376 static const struct user_regset_view user_x86_64_view = {
1377 .name = "x86_64", .e_machine = EM_X86_64,
1378 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1381 #else /* CONFIG_X86_32 */
1383 #define user_regs_struct32 user_regs_struct
1384 #define genregs32_get genregs_get
1385 #define genregs32_set genregs_set
1387 #define user_i387_ia32_struct user_i387_struct
1388 #define user32_fxsr_struct user_fxsr_struct
1390 #endif /* CONFIG_X86_64 */
1392 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1393 static const struct user_regset x86_32_regsets[] = {
1394 [REGSET_GENERAL] = {
1395 .core_note_type = NT_PRSTATUS,
1396 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1397 .size = sizeof(u32), .align = sizeof(u32),
1398 .get = genregs32_get, .set = genregs32_set
1400 [REGSET_FP] = {
1401 .core_note_type = NT_PRFPREG,
1402 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1403 .size = sizeof(u32), .align = sizeof(u32),
1404 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1406 [REGSET_XFP] = {
1407 .core_note_type = NT_PRXFPREG,
1408 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1409 .size = sizeof(u32), .align = sizeof(u32),
1410 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1412 [REGSET_TLS] = {
1413 .core_note_type = NT_386_TLS,
1414 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1415 .size = sizeof(struct user_desc),
1416 .align = sizeof(struct user_desc),
1417 .active = regset_tls_active,
1418 .get = regset_tls_get, .set = regset_tls_set
1420 [REGSET_IOPERM32] = {
1421 .core_note_type = NT_386_IOPERM,
1422 .n = IO_BITMAP_BYTES / sizeof(u32),
1423 .size = sizeof(u32), .align = sizeof(u32),
1424 .active = ioperm_active, .get = ioperm_get
1428 static const struct user_regset_view user_x86_32_view = {
1429 .name = "i386", .e_machine = EM_386,
1430 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1432 #endif
1434 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1436 #ifdef CONFIG_IA32_EMULATION
1437 if (test_tsk_thread_flag(task, TIF_IA32))
1438 #endif
1439 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1440 return &user_x86_32_view;
1441 #endif
1442 #ifdef CONFIG_X86_64
1443 return &user_x86_64_view;
1444 #endif
1447 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1448 int error_code, int si_code)
1450 struct siginfo info;
1452 tsk->thread.trap_no = 1;
1453 tsk->thread.error_code = error_code;
1455 memset(&info, 0, sizeof(info));
1456 info.si_signo = SIGTRAP;
1457 info.si_code = si_code;
1459 /* User-mode ip? */
1460 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1462 /* Send us the fake SIGTRAP */
1463 force_sig_info(SIGTRAP, &info, tsk);
1467 #ifdef CONFIG_X86_32
1468 # define IS_IA32 1
1469 #elif defined CONFIG_IA32_EMULATION
1470 # define IS_IA32 is_compat_task()
1471 #else
1472 # define IS_IA32 0
1473 #endif
1476 * We must return the syscall number to actually look up in the table.
1477 * This can be -1L to skip running any syscall at all.
1479 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1481 long ret = 0;
1484 * If we stepped into a sysenter/syscall insn, it trapped in
1485 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1486 * If user-mode had set TF itself, then it's still clear from
1487 * do_debug() and we need to set it again to restore the user
1488 * state. If we entered on the slow path, TF was already set.
1490 if (test_thread_flag(TIF_SINGLESTEP))
1491 regs->flags |= X86_EFLAGS_TF;
1493 /* do the secure computing check first */
1494 secure_computing(regs->orig_ax);
1496 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1497 ret = -1L;
1499 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1500 tracehook_report_syscall_entry(regs))
1501 ret = -1L;
1503 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1504 ftrace_syscall_enter(regs);
1506 if (unlikely(current->audit_context)) {
1507 if (IS_IA32)
1508 audit_syscall_entry(AUDIT_ARCH_I386,
1509 regs->orig_ax,
1510 regs->bx, regs->cx,
1511 regs->dx, regs->si);
1512 #ifdef CONFIG_X86_64
1513 else
1514 audit_syscall_entry(AUDIT_ARCH_X86_64,
1515 regs->orig_ax,
1516 regs->di, regs->si,
1517 regs->dx, regs->r10);
1518 #endif
1521 return ret ?: regs->orig_ax;
1524 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1526 if (unlikely(current->audit_context))
1527 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1529 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
1530 ftrace_syscall_exit(regs);
1532 if (test_thread_flag(TIF_SYSCALL_TRACE))
1533 tracehook_report_syscall_exit(regs, 0);
1536 * If TIF_SYSCALL_EMU is set, we only get here because of
1537 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1538 * We already reported this syscall instruction in
1539 * syscall_trace_enter(), so don't do any more now.
1541 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1542 return;
1545 * If we are single-stepping, synthesize a trap to follow the
1546 * system call instruction.
1548 if (test_thread_flag(TIF_SINGLESTEP) &&
1549 tracehook_consider_fatal_signal(current, SIGTRAP))
1550 send_sigtrap(current, regs, 0, TRAP_BRKPT);