1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/signal.h>
24 #include <linux/workqueue.h>
25 #include <linux/perf_event.h>
26 #include <linux/hw_breakpoint.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
33 #include <asm/debugreg.h>
36 #include <asm/prctl.h>
37 #include <asm/proto.h>
39 #include <asm/hw_breakpoint.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
50 REGSET_IOPERM64 = REGSET_XFP,
56 struct pt_regs_offset {
61 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64 static const struct pt_regs_offset regoffset_table[] = {
88 REG_OFFSET_NAME(orig_ax),
91 REG_OFFSET_NAME(flags),
98 * regs_query_register_offset() - query register offset from its name
99 * @name: the name of a register
101 * regs_query_register_offset() returns the offset of a register in struct
102 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
104 int regs_query_register_offset(const char *name)
106 const struct pt_regs_offset *roff;
107 for (roff = regoffset_table; roff->name != NULL; roff++)
108 if (!strcmp(roff->name, name))
114 * regs_query_register_name() - query register name from its offset
115 * @offset: the offset of a register in struct pt_regs.
117 * regs_query_register_name() returns the name of a register from its
118 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
120 const char *regs_query_register_name(unsigned int offset)
122 const struct pt_regs_offset *roff;
123 for (roff = regoffset_table; roff->name != NULL; roff++)
124 if (roff->offset == offset)
129 static const int arg_offs_table[] = {
131 [0] = offsetof(struct pt_regs, ax),
132 [1] = offsetof(struct pt_regs, dx),
133 [2] = offsetof(struct pt_regs, cx)
134 #else /* CONFIG_X86_64 */
135 [0] = offsetof(struct pt_regs, di),
136 [1] = offsetof(struct pt_regs, si),
137 [2] = offsetof(struct pt_regs, dx),
138 [3] = offsetof(struct pt_regs, cx),
139 [4] = offsetof(struct pt_regs, r8),
140 [5] = offsetof(struct pt_regs, r9)
145 * does not yet catch signals sent when the child dies.
146 * in exit.c or in signal.c.
150 * Determines which flags the user has access to [1 = access, 0 = no access].
152 #define FLAG_MASK_32 ((unsigned long) \
153 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
154 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
155 X86_EFLAGS_SF | X86_EFLAGS_TF | \
156 X86_EFLAGS_DF | X86_EFLAGS_OF | \
157 X86_EFLAGS_RF | X86_EFLAGS_AC))
160 * Determines whether a value may be installed in a segment register.
162 static inline bool invalid_selector(u16 value)
164 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
169 #define FLAG_MASK FLAG_MASK_32
171 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
173 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
174 return ®s->bx + (regno >> 2);
177 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
180 * Returning the value truncates it to 16 bits.
183 if (offset != offsetof(struct user_regs_struct, gs))
184 retval = *pt_regs_access(task_pt_regs(task), offset);
187 retval = get_user_gs(task_pt_regs(task));
189 retval = task_user_gs(task);
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))
204 * For %cs and %ss we cannot permit a null selector.
205 * We can permit a bogus selector as long as it has USER_RPL.
206 * Null selectors are fine for other segment registers, but
207 * we will never get back to user mode with invalid %cs or %ss
208 * and will take the trap in iret instead. Much code relies
209 * on user_mode() to distinguish a user trap frame (which can
210 * safely use invalid selectors) from a kernel trap frame.
213 case offsetof(struct user_regs_struct, cs):
214 case offsetof(struct user_regs_struct, ss):
215 if (unlikely(value == 0))
219 *pt_regs_access(task_pt_regs(task), offset) = value;
222 case offsetof(struct user_regs_struct, gs):
224 set_user_gs(task_pt_regs(task), value);
226 task_user_gs(task) = value;
232 #else /* CONFIG_X86_64 */
234 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
236 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
238 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
239 return ®s->r15 + (offset / sizeof(regs->r15));
242 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
245 * Returning the value truncates it to 16 bits.
250 case offsetof(struct user_regs_struct, fs):
251 if (task == current) {
252 /* Older gas can't assemble movq %?s,%r?? */
253 asm("movl %%fs,%0" : "=r" (seg));
256 return task->thread.fsindex;
257 case offsetof(struct user_regs_struct, gs):
258 if (task == current) {
259 asm("movl %%gs,%0" : "=r" (seg));
262 return task->thread.gsindex;
263 case offsetof(struct user_regs_struct, ds):
264 if (task == current) {
265 asm("movl %%ds,%0" : "=r" (seg));
268 return task->thread.ds;
269 case offsetof(struct user_regs_struct, es):
270 if (task == current) {
271 asm("movl %%es,%0" : "=r" (seg));
274 return task->thread.es;
276 case offsetof(struct user_regs_struct, cs):
277 case offsetof(struct user_regs_struct, ss):
280 return *pt_regs_access(task_pt_regs(task), offset);
283 static int set_segment_reg(struct task_struct *task,
284 unsigned long offset, u16 value)
287 * The value argument was already truncated to 16 bits.
289 if (invalid_selector(value))
293 case offsetof(struct user_regs_struct,fs):
295 * If this is setting fs as for normal 64-bit use but
296 * setting fs_base has implicitly changed it, leave it.
298 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
299 task->thread.fs != 0) ||
300 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
301 task->thread.fs == 0))
303 task->thread.fsindex = value;
305 loadsegment(fs, task->thread.fsindex);
307 case offsetof(struct user_regs_struct,gs):
309 * If this is setting gs as for normal 64-bit use but
310 * setting gs_base has implicitly changed it, leave it.
312 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
313 task->thread.gs != 0) ||
314 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
315 task->thread.gs == 0))
317 task->thread.gsindex = value;
319 load_gs_index(task->thread.gsindex);
321 case offsetof(struct user_regs_struct,ds):
322 task->thread.ds = value;
324 loadsegment(ds, task->thread.ds);
326 case offsetof(struct user_regs_struct,es):
327 task->thread.es = value;
329 loadsegment(es, task->thread.es);
333 * Can't actually change these in 64-bit mode.
335 case offsetof(struct user_regs_struct,cs):
336 if (unlikely(value == 0))
338 #ifdef CONFIG_IA32_EMULATION
339 if (test_tsk_thread_flag(task, TIF_IA32))
340 task_pt_regs(task)->cs = value;
343 case offsetof(struct user_regs_struct,ss):
344 if (unlikely(value == 0))
346 #ifdef CONFIG_IA32_EMULATION
347 if (test_tsk_thread_flag(task, TIF_IA32))
348 task_pt_regs(task)->ss = value;
356 #endif /* CONFIG_X86_32 */
358 static unsigned long get_flags(struct task_struct *task)
360 unsigned long retval = task_pt_regs(task)->flags;
363 * If the debugger set TF, hide it from the readout.
365 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
366 retval &= ~X86_EFLAGS_TF;
371 static int set_flags(struct task_struct *task, unsigned long value)
373 struct pt_regs *regs = task_pt_regs(task);
376 * If the user value contains TF, mark that
377 * it was not "us" (the debugger) that set it.
378 * If not, make sure it stays set if we had.
380 if (value & X86_EFLAGS_TF)
381 clear_tsk_thread_flag(task, TIF_FORCED_TF);
382 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
383 value |= X86_EFLAGS_TF;
385 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
390 static int putreg(struct task_struct *child,
391 unsigned long offset, unsigned long value)
394 case offsetof(struct user_regs_struct, cs):
395 case offsetof(struct user_regs_struct, ds):
396 case offsetof(struct user_regs_struct, es):
397 case offsetof(struct user_regs_struct, fs):
398 case offsetof(struct user_regs_struct, gs):
399 case offsetof(struct user_regs_struct, ss):
400 return set_segment_reg(child, offset, value);
402 case offsetof(struct user_regs_struct, flags):
403 return set_flags(child, value);
406 case offsetof(struct user_regs_struct,fs_base):
407 if (value >= TASK_SIZE_OF(child))
410 * When changing the segment base, use do_arch_prctl
411 * to set either thread.fs or thread.fsindex and the
412 * corresponding GDT slot.
414 if (child->thread.fs != value)
415 return do_arch_prctl(child, ARCH_SET_FS, value);
417 case offsetof(struct user_regs_struct,gs_base):
419 * Exactly the same here as the %fs handling above.
421 if (value >= TASK_SIZE_OF(child))
423 if (child->thread.gs != value)
424 return do_arch_prctl(child, ARCH_SET_GS, value);
429 *pt_regs_access(task_pt_regs(child), offset) = value;
433 static unsigned long getreg(struct task_struct *task, unsigned long offset)
436 case offsetof(struct user_regs_struct, cs):
437 case offsetof(struct user_regs_struct, ds):
438 case offsetof(struct user_regs_struct, es):
439 case offsetof(struct user_regs_struct, fs):
440 case offsetof(struct user_regs_struct, gs):
441 case offsetof(struct user_regs_struct, ss):
442 return get_segment_reg(task, offset);
444 case offsetof(struct user_regs_struct, flags):
445 return get_flags(task);
448 case offsetof(struct user_regs_struct, fs_base): {
450 * do_arch_prctl may have used a GDT slot instead of
451 * the MSR. To userland, it appears the same either
452 * way, except the %fs segment selector might not be 0.
454 unsigned int seg = task->thread.fsindex;
455 if (task->thread.fs != 0)
456 return task->thread.fs;
458 asm("movl %%fs,%0" : "=r" (seg));
459 if (seg != FS_TLS_SEL)
461 return get_desc_base(&task->thread.tls_array[FS_TLS]);
463 case offsetof(struct user_regs_struct, gs_base): {
465 * Exactly the same here as the %fs handling above.
467 unsigned int seg = task->thread.gsindex;
468 if (task->thread.gs != 0)
469 return task->thread.gs;
471 asm("movl %%gs,%0" : "=r" (seg));
472 if (seg != GS_TLS_SEL)
474 return get_desc_base(&task->thread.tls_array[GS_TLS]);
479 return *pt_regs_access(task_pt_regs(task), offset);
482 static int genregs_get(struct task_struct *target,
483 const struct user_regset *regset,
484 unsigned int pos, unsigned int count,
485 void *kbuf, void __user *ubuf)
488 unsigned long *k = kbuf;
489 while (count >= sizeof(*k)) {
490 *k++ = getreg(target, pos);
495 unsigned long __user *u = ubuf;
496 while (count >= sizeof(*u)) {
497 if (__put_user(getreg(target, pos), u++))
507 static int genregs_set(struct task_struct *target,
508 const struct user_regset *regset,
509 unsigned int pos, unsigned int count,
510 const void *kbuf, const void __user *ubuf)
514 const unsigned long *k = kbuf;
515 while (count >= sizeof(*k) && !ret) {
516 ret = putreg(target, pos, *k++);
521 const unsigned long __user *u = ubuf;
522 while (count >= sizeof(*u) && !ret) {
524 ret = __get_user(word, u++);
527 ret = putreg(target, pos, word);
535 static void ptrace_triggered(struct perf_event *bp, int nmi,
536 struct perf_sample_data *data,
537 struct pt_regs *regs)
540 struct thread_struct *thread = &(current->thread);
543 * Store in the virtual DR6 register the fact that the breakpoint
544 * was hit so the thread's debugger will see it.
546 for (i = 0; i < HBP_NUM; i++) {
547 if (thread->ptrace_bps[i] == bp)
551 thread->debugreg6 |= (DR_TRAP0 << i);
555 * Walk through every ptrace breakpoints for this thread and
556 * build the dr7 value on top of their attributes.
559 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
563 struct arch_hw_breakpoint *info;
565 for (i = 0; i < HBP_NUM; i++) {
566 if (bp[i] && !bp[i]->attr.disabled) {
567 info = counter_arch_bp(bp[i]);
568 dr7 |= encode_dr7(i, info->len, info->type);
576 ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
577 struct task_struct *tsk, int disabled)
580 int gen_len, gen_type;
581 struct perf_event_attr attr;
584 * We shoud have at least an inactive breakpoint at this
585 * slot. It means the user is writing dr7 without having
586 * written the address register first
591 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
596 attr.bp_len = gen_len;
597 attr.bp_type = gen_type;
598 attr.disabled = disabled;
600 return modify_user_hw_breakpoint(bp, &attr);
604 * Handle ptrace writes to debug register 7.
606 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
608 struct thread_struct *thread = &(tsk->thread);
609 unsigned long old_dr7;
610 int i, orig_ret = 0, rc = 0;
611 int enabled, second_pass = 0;
613 struct perf_event *bp;
615 data &= ~DR_CONTROL_RESERVED;
616 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
619 * Loop through all the hardware breakpoints, making the
620 * appropriate changes to each.
622 for (i = 0; i < HBP_NUM; i++) {
623 enabled = decode_dr7(data, i, &len, &type);
624 bp = thread->ptrace_bps[i];
629 * Don't unregister the breakpoints right-away,
630 * unless all register_user_hw_breakpoint()
631 * requests have succeeded. This prevents
632 * any window of opportunity for debug
633 * register grabbing by other users.
638 rc = ptrace_modify_breakpoint(bp, len, type,
646 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
651 * Make a second pass to free the remaining unused breakpoints
652 * or to restore the original breakpoints if an error occurred.
662 return ((orig_ret < 0) ? orig_ret : rc);
666 * Handle PTRACE_PEEKUSR calls for the debug register area.
668 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
670 struct thread_struct *thread = &(tsk->thread);
671 unsigned long val = 0;
674 struct perf_event *bp;
675 bp = thread->ptrace_bps[n];
678 val = bp->hw.info.address;
680 val = thread->debugreg6;
682 val = thread->ptrace_dr7;
687 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
690 struct perf_event *bp;
691 struct thread_struct *t = &tsk->thread;
692 struct perf_event_attr attr;
694 if (!t->ptrace_bps[nr]) {
695 hw_breakpoint_init(&attr);
697 * Put stub len and type to register (reserve) an inactive but
701 attr.bp_len = HW_BREAKPOINT_LEN_1;
702 attr.bp_type = HW_BREAKPOINT_W;
705 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
708 * CHECKME: the previous code returned -EIO if the addr wasn't
709 * a valid task virtual addr. The new one will return -EINVAL in
711 * -EINVAL may be what we want for in-kernel breakpoints users,
712 * but -EIO looks better for ptrace, since we refuse a register
713 * writing for the user. And anyway this is the previous
719 t->ptrace_bps[nr] = bp;
723 bp = t->ptrace_bps[nr];
727 err = modify_user_hw_breakpoint(bp, &attr);
737 * Handle PTRACE_POKEUSR calls for the debug register area.
739 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
741 struct thread_struct *thread = &(tsk->thread);
744 /* There are no DR4 or DR5 registers */
745 if (n == 4 || n == 5)
749 thread->debugreg6 = val;
753 rc = ptrace_set_breakpoint_addr(tsk, n, val);
757 /* All that's left is DR7 */
759 rc = ptrace_write_dr7(tsk, val);
761 thread->ptrace_dr7 = val;
769 * These access the current or another (stopped) task's io permission
770 * bitmap for debugging or core dump.
772 static int ioperm_active(struct task_struct *target,
773 const struct user_regset *regset)
775 return target->thread.io_bitmap_max / regset->size;
778 static int ioperm_get(struct task_struct *target,
779 const struct user_regset *regset,
780 unsigned int pos, unsigned int count,
781 void *kbuf, void __user *ubuf)
783 if (!target->thread.io_bitmap_ptr)
786 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
787 target->thread.io_bitmap_ptr,
791 #ifdef CONFIG_X86_PTRACE_BTS
793 * A branch trace store context.
795 * Contexts may only be installed by ptrace_bts_config() and only for
798 * Contexts are destroyed when the tracee is detached from the tracer.
799 * The actual destruction work requires interrupts enabled, so the
800 * work is deferred and will be scheduled during __ptrace_unlink().
802 * Contexts hold an additional task_struct reference on the traced
803 * task, as well as a reference on the tracer's mm.
805 * Ptrace already holds a task_struct for the duration of ptrace operations,
806 * but since destruction is deferred, it may be executed after both
807 * tracer and tracee exited.
810 /* The branch trace handle. */
811 struct bts_tracer *tracer;
813 /* The buffer used to store the branch trace and its size. */
817 /* The mm that paid for the above buffer. */
818 struct mm_struct *mm;
820 /* The task this context belongs to. */
821 struct task_struct *task;
823 /* The signal to send on a bts buffer overflow. */
824 unsigned int bts_ovfl_signal;
826 /* The work struct to destroy a context. */
827 struct work_struct work;
830 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
835 err = account_locked_memory(current->mm, current->signal->rlim, size);
839 buffer = kzalloc(size, GFP_KERNEL);
843 context->buffer = buffer;
844 context->size = size;
845 context->mm = get_task_mm(current);
850 refund_locked_memory(current->mm, size);
854 static inline void free_bts_buffer(struct bts_context *context)
856 if (!context->buffer)
859 kfree(context->buffer);
860 context->buffer = NULL;
862 refund_locked_memory(context->mm, context->size);
869 static void free_bts_context_work(struct work_struct *w)
871 struct bts_context *context;
873 context = container_of(w, struct bts_context, work);
875 ds_release_bts(context->tracer);
876 put_task_struct(context->task);
877 free_bts_buffer(context);
881 static inline void free_bts_context(struct bts_context *context)
883 INIT_WORK(&context->work, free_bts_context_work);
884 schedule_work(&context->work);
887 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
889 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
891 context->task = task;
894 get_task_struct(task);
900 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
901 struct bts_struct __user *out)
903 struct bts_context *context;
904 const struct bts_trace *trace;
905 struct bts_struct bts;
906 const unsigned char *at;
909 context = child->bts;
913 trace = ds_read_bts(context->tracer);
917 at = trace->ds.top - ((index + 1) * trace->ds.size);
918 if ((void *)at < trace->ds.begin)
919 at += (trace->ds.n * trace->ds.size);
924 error = trace->read(context->tracer, at, &bts);
928 if (copy_to_user(out, &bts, sizeof(bts)))
934 static int ptrace_bts_drain(struct task_struct *child,
936 struct bts_struct __user *out)
938 struct bts_context *context;
939 const struct bts_trace *trace;
940 const unsigned char *at;
941 int error, drained = 0;
943 context = child->bts;
947 trace = ds_read_bts(context->tracer);
954 if (size < (trace->ds.top - trace->ds.begin))
957 for (at = trace->ds.begin; (void *)at < trace->ds.top;
958 out++, drained++, at += trace->ds.size) {
959 struct bts_struct bts;
961 error = trace->read(context->tracer, at, &bts);
965 if (copy_to_user(out, &bts, sizeof(bts)))
969 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
971 error = ds_reset_bts(context->tracer);
978 static int ptrace_bts_config(struct task_struct *child,
980 const struct ptrace_bts_config __user *ucfg)
982 struct bts_context *context;
983 struct ptrace_bts_config cfg;
984 unsigned int flags = 0;
986 if (cfg_size < sizeof(cfg))
989 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
992 context = child->bts;
994 context = alloc_bts_context(child);
998 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1003 context->bts_ovfl_signal = cfg.signal;
1006 ds_release_bts(context->tracer);
1007 context->tracer = NULL;
1009 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1012 free_bts_buffer(context);
1016 err = alloc_bts_buffer(context, cfg.size);
1021 if (cfg.flags & PTRACE_BTS_O_TRACE)
1024 if (cfg.flags & PTRACE_BTS_O_SCHED)
1025 flags |= BTS_TIMESTAMPS;
1028 ds_request_bts_task(child, context->buffer, context->size,
1029 NULL, (size_t)-1, flags);
1030 if (unlikely(IS_ERR(context->tracer))) {
1031 int error = PTR_ERR(context->tracer);
1033 free_bts_buffer(context);
1034 context->tracer = NULL;
1041 static int ptrace_bts_status(struct task_struct *child,
1043 struct ptrace_bts_config __user *ucfg)
1045 struct bts_context *context;
1046 const struct bts_trace *trace;
1047 struct ptrace_bts_config cfg;
1049 context = child->bts;
1053 if (cfg_size < sizeof(cfg))
1056 trace = ds_read_bts(context->tracer);
1060 memset(&cfg, 0, sizeof(cfg));
1061 cfg.size = trace->ds.end - trace->ds.begin;
1062 cfg.signal = context->bts_ovfl_signal;
1063 cfg.bts_size = sizeof(struct bts_struct);
1066 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1068 if (trace->ds.flags & BTS_USER)
1069 cfg.flags |= PTRACE_BTS_O_TRACE;
1071 if (trace->ds.flags & BTS_TIMESTAMPS)
1072 cfg.flags |= PTRACE_BTS_O_SCHED;
1074 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1080 static int ptrace_bts_clear(struct task_struct *child)
1082 struct bts_context *context;
1083 const struct bts_trace *trace;
1085 context = child->bts;
1089 trace = ds_read_bts(context->tracer);
1093 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
1095 return ds_reset_bts(context->tracer);
1098 static int ptrace_bts_size(struct task_struct *child)
1100 struct bts_context *context;
1101 const struct bts_trace *trace;
1103 context = child->bts;
1107 trace = ds_read_bts(context->tracer);
1111 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1115 * Called from __ptrace_unlink() after the child has been moved back
1116 * to its original parent.
1118 void ptrace_bts_untrace(struct task_struct *child)
1120 if (unlikely(child->bts)) {
1121 free_bts_context(child->bts);
1125 #endif /* CONFIG_X86_PTRACE_BTS */
1128 * Called by kernel/ptrace.c when detaching..
1130 * Make sure the single step bit is not set.
1132 void ptrace_disable(struct task_struct *child)
1134 user_disable_single_step(child);
1135 #ifdef TIF_SYSCALL_EMU
1136 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1140 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1141 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1144 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1147 unsigned long __user *datap = (unsigned long __user *)data;
1150 /* read the word at location addr in the USER area. */
1151 case PTRACE_PEEKUSR: {
1155 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1156 addr >= sizeof(struct user))
1159 tmp = 0; /* Default return condition */
1160 if (addr < sizeof(struct user_regs_struct))
1161 tmp = getreg(child, addr);
1162 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1163 addr <= offsetof(struct user, u_debugreg[7])) {
1164 addr -= offsetof(struct user, u_debugreg[0]);
1165 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1167 ret = put_user(tmp, datap);
1171 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1173 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1174 addr >= sizeof(struct user))
1177 if (addr < sizeof(struct user_regs_struct))
1178 ret = putreg(child, addr, data);
1179 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1180 addr <= offsetof(struct user, u_debugreg[7])) {
1181 addr -= offsetof(struct user, u_debugreg[0]);
1182 ret = ptrace_set_debugreg(child,
1183 addr / sizeof(data), data);
1187 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1188 return copy_regset_to_user(child,
1189 task_user_regset_view(current),
1191 0, sizeof(struct user_regs_struct),
1194 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1195 return copy_regset_from_user(child,
1196 task_user_regset_view(current),
1198 0, sizeof(struct user_regs_struct),
1201 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1202 return copy_regset_to_user(child,
1203 task_user_regset_view(current),
1205 0, sizeof(struct user_i387_struct),
1208 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1209 return copy_regset_from_user(child,
1210 task_user_regset_view(current),
1212 0, sizeof(struct user_i387_struct),
1215 #ifdef CONFIG_X86_32
1216 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1217 return copy_regset_to_user(child, &user_x86_32_view,
1219 0, sizeof(struct user_fxsr_struct),
1222 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1223 return copy_regset_from_user(child, &user_x86_32_view,
1225 0, sizeof(struct user_fxsr_struct),
1229 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1230 case PTRACE_GET_THREAD_AREA:
1233 ret = do_get_thread_area(child, addr,
1234 (struct user_desc __user *) data);
1237 case PTRACE_SET_THREAD_AREA:
1240 ret = do_set_thread_area(child, addr,
1241 (struct user_desc __user *) data, 0);
1245 #ifdef CONFIG_X86_64
1246 /* normal 64bit interface to access TLS data.
1247 Works just like arch_prctl, except that the arguments
1249 case PTRACE_ARCH_PRCTL:
1250 ret = do_arch_prctl(child, data, addr);
1255 * These bits need more cooking - not enabled yet:
1257 #ifdef CONFIG_X86_PTRACE_BTS
1258 case PTRACE_BTS_CONFIG:
1259 ret = ptrace_bts_config
1260 (child, data, (struct ptrace_bts_config __user *)addr);
1263 case PTRACE_BTS_STATUS:
1264 ret = ptrace_bts_status
1265 (child, data, (struct ptrace_bts_config __user *)addr);
1268 case PTRACE_BTS_SIZE:
1269 ret = ptrace_bts_size(child);
1272 case PTRACE_BTS_GET:
1273 ret = ptrace_bts_read_record
1274 (child, data, (struct bts_struct __user *) addr);
1277 case PTRACE_BTS_CLEAR:
1278 ret = ptrace_bts_clear(child);
1281 case PTRACE_BTS_DRAIN:
1282 ret = ptrace_bts_drain
1283 (child, data, (struct bts_struct __user *) addr);
1285 #endif /* CONFIG_X86_PTRACE_BTS */
1288 ret = ptrace_request(child, request, addr, data);
1295 #ifdef CONFIG_IA32_EMULATION
1297 #include <linux/compat.h>
1298 #include <linux/syscalls.h>
1299 #include <asm/ia32.h>
1300 #include <asm/user32.h>
1303 case offsetof(struct user32, regs.l): \
1304 regs->q = value; break
1307 case offsetof(struct user32, regs.rs): \
1308 return set_segment_reg(child, \
1309 offsetof(struct user_regs_struct, rs), \
1313 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1315 struct pt_regs *regs = task_pt_regs(child);
1336 case offsetof(struct user32, regs.orig_eax):
1338 * A 32-bit debugger setting orig_eax means to restore
1339 * the state of the task restarting a 32-bit syscall.
1340 * Make sure we interpret the -ERESTART* codes correctly
1341 * in case the task is not actually still sitting at the
1342 * exit from a 32-bit syscall with TS_COMPAT still set.
1344 regs->orig_ax = value;
1345 if (syscall_get_nr(child, regs) >= 0)
1346 task_thread_info(child)->status |= TS_COMPAT;
1349 case offsetof(struct user32, regs.eflags):
1350 return set_flags(child, value);
1352 case offsetof(struct user32, u_debugreg[0]) ...
1353 offsetof(struct user32, u_debugreg[7]):
1354 regno -= offsetof(struct user32, u_debugreg[0]);
1355 return ptrace_set_debugreg(child, regno / 4, value);
1358 if (regno > sizeof(struct user32) || (regno & 3))
1362 * Other dummy fields in the virtual user structure
1374 case offsetof(struct user32, regs.l): \
1375 *val = regs->q; break
1378 case offsetof(struct user32, regs.rs): \
1379 *val = get_segment_reg(child, \
1380 offsetof(struct user_regs_struct, rs)); \
1383 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1385 struct pt_regs *regs = task_pt_regs(child);
1403 R32(orig_eax, orig_ax);
1407 case offsetof(struct user32, regs.eflags):
1408 *val = get_flags(child);
1411 case offsetof(struct user32, u_debugreg[0]) ...
1412 offsetof(struct user32, u_debugreg[7]):
1413 regno -= offsetof(struct user32, u_debugreg[0]);
1414 *val = ptrace_get_debugreg(child, regno / 4);
1418 if (regno > sizeof(struct user32) || (regno & 3))
1422 * Other dummy fields in the virtual user structure
1434 static int genregs32_get(struct task_struct *target,
1435 const struct user_regset *regset,
1436 unsigned int pos, unsigned int count,
1437 void *kbuf, void __user *ubuf)
1440 compat_ulong_t *k = kbuf;
1441 while (count >= sizeof(*k)) {
1442 getreg32(target, pos, k++);
1443 count -= sizeof(*k);
1447 compat_ulong_t __user *u = ubuf;
1448 while (count >= sizeof(*u)) {
1449 compat_ulong_t word;
1450 getreg32(target, pos, &word);
1451 if (__put_user(word, u++))
1453 count -= sizeof(*u);
1461 static int genregs32_set(struct task_struct *target,
1462 const struct user_regset *regset,
1463 unsigned int pos, unsigned int count,
1464 const void *kbuf, const void __user *ubuf)
1468 const compat_ulong_t *k = kbuf;
1469 while (count >= sizeof(*k) && !ret) {
1470 ret = putreg32(target, pos, *k++);
1471 count -= sizeof(*k);
1475 const compat_ulong_t __user *u = ubuf;
1476 while (count >= sizeof(*u) && !ret) {
1477 compat_ulong_t word;
1478 ret = __get_user(word, u++);
1481 ret = putreg32(target, pos, word);
1482 count -= sizeof(*u);
1489 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1490 compat_ulong_t caddr, compat_ulong_t cdata)
1492 unsigned long addr = caddr;
1493 unsigned long data = cdata;
1494 void __user *datap = compat_ptr(data);
1499 case PTRACE_PEEKUSR:
1500 ret = getreg32(child, addr, &val);
1502 ret = put_user(val, (__u32 __user *)datap);
1505 case PTRACE_POKEUSR:
1506 ret = putreg32(child, addr, data);
1509 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1510 return copy_regset_to_user(child, &user_x86_32_view,
1512 0, sizeof(struct user_regs_struct32),
1515 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1516 return copy_regset_from_user(child, &user_x86_32_view,
1518 sizeof(struct user_regs_struct32),
1521 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1522 return copy_regset_to_user(child, &user_x86_32_view,
1524 sizeof(struct user_i387_ia32_struct),
1527 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1528 return copy_regset_from_user(
1529 child, &user_x86_32_view, REGSET_FP,
1530 0, sizeof(struct user_i387_ia32_struct), datap);
1532 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1533 return copy_regset_to_user(child, &user_x86_32_view,
1535 sizeof(struct user32_fxsr_struct),
1538 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1539 return copy_regset_from_user(child, &user_x86_32_view,
1541 sizeof(struct user32_fxsr_struct),
1544 case PTRACE_GET_THREAD_AREA:
1545 case PTRACE_SET_THREAD_AREA:
1546 #ifdef CONFIG_X86_PTRACE_BTS
1547 case PTRACE_BTS_CONFIG:
1548 case PTRACE_BTS_STATUS:
1549 case PTRACE_BTS_SIZE:
1550 case PTRACE_BTS_GET:
1551 case PTRACE_BTS_CLEAR:
1552 case PTRACE_BTS_DRAIN:
1553 #endif /* CONFIG_X86_PTRACE_BTS */
1554 return arch_ptrace(child, request, addr, data);
1557 return compat_ptrace_request(child, request, addr, data);
1563 #endif /* CONFIG_IA32_EMULATION */
1565 #ifdef CONFIG_X86_64
1567 static struct user_regset x86_64_regsets[] __read_mostly = {
1568 [REGSET_GENERAL] = {
1569 .core_note_type = NT_PRSTATUS,
1570 .n = sizeof(struct user_regs_struct) / sizeof(long),
1571 .size = sizeof(long), .align = sizeof(long),
1572 .get = genregs_get, .set = genregs_set
1575 .core_note_type = NT_PRFPREG,
1576 .n = sizeof(struct user_i387_struct) / sizeof(long),
1577 .size = sizeof(long), .align = sizeof(long),
1578 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1581 .core_note_type = NT_X86_XSTATE,
1582 .size = sizeof(u64), .align = sizeof(u64),
1583 .active = xstateregs_active, .get = xstateregs_get,
1584 .set = xstateregs_set
1586 [REGSET_IOPERM64] = {
1587 .core_note_type = NT_386_IOPERM,
1588 .n = IO_BITMAP_LONGS,
1589 .size = sizeof(long), .align = sizeof(long),
1590 .active = ioperm_active, .get = ioperm_get
1594 static const struct user_regset_view user_x86_64_view = {
1595 .name = "x86_64", .e_machine = EM_X86_64,
1596 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1599 #else /* CONFIG_X86_32 */
1601 #define user_regs_struct32 user_regs_struct
1602 #define genregs32_get genregs_get
1603 #define genregs32_set genregs_set
1605 #define user_i387_ia32_struct user_i387_struct
1606 #define user32_fxsr_struct user_fxsr_struct
1608 #endif /* CONFIG_X86_64 */
1610 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1611 static struct user_regset x86_32_regsets[] __read_mostly = {
1612 [REGSET_GENERAL] = {
1613 .core_note_type = NT_PRSTATUS,
1614 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1615 .size = sizeof(u32), .align = sizeof(u32),
1616 .get = genregs32_get, .set = genregs32_set
1619 .core_note_type = NT_PRFPREG,
1620 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1621 .size = sizeof(u32), .align = sizeof(u32),
1622 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1625 .core_note_type = NT_PRXFPREG,
1626 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1627 .size = sizeof(u32), .align = sizeof(u32),
1628 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1631 .core_note_type = NT_X86_XSTATE,
1632 .size = sizeof(u64), .align = sizeof(u64),
1633 .active = xstateregs_active, .get = xstateregs_get,
1634 .set = xstateregs_set
1637 .core_note_type = NT_386_TLS,
1638 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1639 .size = sizeof(struct user_desc),
1640 .align = sizeof(struct user_desc),
1641 .active = regset_tls_active,
1642 .get = regset_tls_get, .set = regset_tls_set
1644 [REGSET_IOPERM32] = {
1645 .core_note_type = NT_386_IOPERM,
1646 .n = IO_BITMAP_BYTES / sizeof(u32),
1647 .size = sizeof(u32), .align = sizeof(u32),
1648 .active = ioperm_active, .get = ioperm_get
1652 static const struct user_regset_view user_x86_32_view = {
1653 .name = "i386", .e_machine = EM_386,
1654 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1659 * This represents bytes 464..511 in the memory layout exported through
1660 * the REGSET_XSTATE interface.
1662 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1664 void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1666 #ifdef CONFIG_X86_64
1667 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1669 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1670 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1672 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1675 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1677 #ifdef CONFIG_IA32_EMULATION
1678 if (test_tsk_thread_flag(task, TIF_IA32))
1680 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1681 return &user_x86_32_view;
1683 #ifdef CONFIG_X86_64
1684 return &user_x86_64_view;
1688 static void fill_sigtrap_info(struct task_struct *tsk,
1689 struct pt_regs *regs,
1690 int error_code, int si_code,
1691 struct siginfo *info)
1693 tsk->thread.trap_no = 1;
1694 tsk->thread.error_code = error_code;
1696 memset(info, 0, sizeof(*info));
1697 info->si_signo = SIGTRAP;
1698 info->si_code = si_code;
1699 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1702 void user_single_step_siginfo(struct task_struct *tsk,
1703 struct pt_regs *regs,
1704 struct siginfo *info)
1706 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1709 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1710 int error_code, int si_code)
1712 struct siginfo info;
1714 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1715 /* Send us the fake SIGTRAP */
1716 force_sig_info(SIGTRAP, &info, tsk);
1720 #ifdef CONFIG_X86_32
1722 #elif defined CONFIG_IA32_EMULATION
1723 # define IS_IA32 is_compat_task()
1729 * We must return the syscall number to actually look up in the table.
1730 * This can be -1L to skip running any syscall at all.
1732 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1737 * If we stepped into a sysenter/syscall insn, it trapped in
1738 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1739 * If user-mode had set TF itself, then it's still clear from
1740 * do_debug() and we need to set it again to restore the user
1741 * state. If we entered on the slow path, TF was already set.
1743 if (test_thread_flag(TIF_SINGLESTEP))
1744 regs->flags |= X86_EFLAGS_TF;
1746 /* do the secure computing check first */
1747 secure_computing(regs->orig_ax);
1749 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1752 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1753 tracehook_report_syscall_entry(regs))
1756 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1757 trace_sys_enter(regs, regs->orig_ax);
1759 if (unlikely(current->audit_context)) {
1761 audit_syscall_entry(AUDIT_ARCH_I386,
1764 regs->dx, regs->si);
1765 #ifdef CONFIG_X86_64
1767 audit_syscall_entry(AUDIT_ARCH_X86_64,
1770 regs->dx, regs->r10);
1774 return ret ?: regs->orig_ax;
1777 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1781 if (unlikely(current->audit_context))
1782 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1784 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1785 trace_sys_exit(regs, regs->ax);
1788 * If TIF_SYSCALL_EMU is set, we only get here because of
1789 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1790 * We already reported this syscall instruction in
1791 * syscall_trace_enter().
1793 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1794 !test_thread_flag(TIF_SYSCALL_EMU);
1795 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1796 tracehook_report_syscall_exit(regs, step);