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/slab.h>
16 #include <linux/ptrace.h>
17 #include <linux/regset.h>
18 #include <linux/tracehook.h>
19 #include <linux/user.h>
20 #include <linux/elf.h>
21 #include <linux/security.h>
22 #include <linux/audit.h>
23 #include <linux/seccomp.h>
24 #include <linux/signal.h>
25 #include <linux/workqueue.h>
26 #include <linux/perf_event.h>
27 #include <linux/hw_breakpoint.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/processor.h>
34 #include <asm/debugreg.h>
37 #include <asm/prctl.h>
38 #include <asm/proto.h>
40 #include <asm/hw_breakpoint.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/syscalls.h>
51 REGSET_IOPERM64 = REGSET_XFP,
57 struct pt_regs_offset {
62 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
63 #define REG_OFFSET_END {.name = NULL, .offset = 0}
65 static const struct pt_regs_offset regoffset_table[] = {
89 REG_OFFSET_NAME(orig_ax),
92 REG_OFFSET_NAME(flags),
99 * regs_query_register_offset() - query register offset from its name
100 * @name: the name of a register
102 * regs_query_register_offset() returns the offset of a register in struct
103 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
105 int regs_query_register_offset(const char *name)
107 const struct pt_regs_offset *roff;
108 for (roff = regoffset_table; roff->name != NULL; roff++)
109 if (!strcmp(roff->name, name))
115 * regs_query_register_name() - query register name from its offset
116 * @offset: the offset of a register in struct pt_regs.
118 * regs_query_register_name() returns the name of a register from its
119 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
121 const char *regs_query_register_name(unsigned int offset)
123 const struct pt_regs_offset *roff;
124 for (roff = regoffset_table; roff->name != NULL; roff++)
125 if (roff->offset == offset)
130 static const int arg_offs_table[] = {
132 [0] = offsetof(struct pt_regs, ax),
133 [1] = offsetof(struct pt_regs, dx),
134 [2] = offsetof(struct pt_regs, cx)
135 #else /* CONFIG_X86_64 */
136 [0] = offsetof(struct pt_regs, di),
137 [1] = offsetof(struct pt_regs, si),
138 [2] = offsetof(struct pt_regs, dx),
139 [3] = offsetof(struct pt_regs, cx),
140 [4] = offsetof(struct pt_regs, r8),
141 [5] = offsetof(struct pt_regs, r9)
146 * does not yet catch signals sent when the child dies.
147 * in exit.c or in signal.c.
151 * Determines which flags the user has access to [1 = access, 0 = no access].
153 #define FLAG_MASK_32 ((unsigned long) \
154 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
155 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
156 X86_EFLAGS_SF | X86_EFLAGS_TF | \
157 X86_EFLAGS_DF | X86_EFLAGS_OF | \
158 X86_EFLAGS_RF | X86_EFLAGS_AC))
161 * Determines whether a value may be installed in a segment register.
163 static inline bool invalid_selector(u16 value)
165 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
170 #define FLAG_MASK FLAG_MASK_32
172 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
174 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
175 return ®s->bx + (regno >> 2);
178 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
181 * Returning the value truncates it to 16 bits.
184 if (offset != offsetof(struct user_regs_struct, gs))
185 retval = *pt_regs_access(task_pt_regs(task), offset);
188 retval = get_user_gs(task_pt_regs(task));
190 retval = task_user_gs(task);
195 static int set_segment_reg(struct task_struct *task,
196 unsigned long offset, u16 value)
199 * The value argument was already truncated to 16 bits.
201 if (invalid_selector(value))
205 * For %cs and %ss we cannot permit a null selector.
206 * We can permit a bogus selector as long as it has USER_RPL.
207 * Null selectors are fine for other segment registers, but
208 * we will never get back to user mode with invalid %cs or %ss
209 * and will take the trap in iret instead. Much code relies
210 * on user_mode() to distinguish a user trap frame (which can
211 * safely use invalid selectors) from a kernel trap frame.
214 case offsetof(struct user_regs_struct, cs):
215 case offsetof(struct user_regs_struct, ss):
216 if (unlikely(value == 0))
220 *pt_regs_access(task_pt_regs(task), offset) = value;
223 case offsetof(struct user_regs_struct, gs):
225 set_user_gs(task_pt_regs(task), value);
227 task_user_gs(task) = value;
233 #else /* CONFIG_X86_64 */
235 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
237 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
239 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
240 return ®s->r15 + (offset / sizeof(regs->r15));
243 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
246 * Returning the value truncates it to 16 bits.
251 case offsetof(struct user_regs_struct, fs):
252 if (task == current) {
253 /* Older gas can't assemble movq %?s,%r?? */
254 asm("movl %%fs,%0" : "=r" (seg));
257 return task->thread.fsindex;
258 case offsetof(struct user_regs_struct, gs):
259 if (task == current) {
260 asm("movl %%gs,%0" : "=r" (seg));
263 return task->thread.gsindex;
264 case offsetof(struct user_regs_struct, ds):
265 if (task == current) {
266 asm("movl %%ds,%0" : "=r" (seg));
269 return task->thread.ds;
270 case offsetof(struct user_regs_struct, es):
271 if (task == current) {
272 asm("movl %%es,%0" : "=r" (seg));
275 return task->thread.es;
277 case offsetof(struct user_regs_struct, cs):
278 case offsetof(struct user_regs_struct, ss):
281 return *pt_regs_access(task_pt_regs(task), offset);
284 static int set_segment_reg(struct task_struct *task,
285 unsigned long offset, u16 value)
288 * The value argument was already truncated to 16 bits.
290 if (invalid_selector(value))
294 case offsetof(struct user_regs_struct,fs):
296 * If this is setting fs as for normal 64-bit use but
297 * setting fs_base has implicitly changed it, leave it.
299 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
300 task->thread.fs != 0) ||
301 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
302 task->thread.fs == 0))
304 task->thread.fsindex = value;
306 loadsegment(fs, task->thread.fsindex);
308 case offsetof(struct user_regs_struct,gs):
310 * If this is setting gs as for normal 64-bit use but
311 * setting gs_base has implicitly changed it, leave it.
313 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
314 task->thread.gs != 0) ||
315 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
316 task->thread.gs == 0))
318 task->thread.gsindex = value;
320 load_gs_index(task->thread.gsindex);
322 case offsetof(struct user_regs_struct,ds):
323 task->thread.ds = value;
325 loadsegment(ds, task->thread.ds);
327 case offsetof(struct user_regs_struct,es):
328 task->thread.es = value;
330 loadsegment(es, task->thread.es);
334 * Can't actually change these in 64-bit mode.
336 case offsetof(struct user_regs_struct,cs):
337 if (unlikely(value == 0))
339 #ifdef CONFIG_IA32_EMULATION
340 if (test_tsk_thread_flag(task, TIF_IA32))
341 task_pt_regs(task)->cs = value;
344 case offsetof(struct user_regs_struct,ss):
345 if (unlikely(value == 0))
347 #ifdef CONFIG_IA32_EMULATION
348 if (test_tsk_thread_flag(task, TIF_IA32))
349 task_pt_regs(task)->ss = value;
357 #endif /* CONFIG_X86_32 */
359 static unsigned long get_flags(struct task_struct *task)
361 unsigned long retval = task_pt_regs(task)->flags;
364 * If the debugger set TF, hide it from the readout.
366 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
367 retval &= ~X86_EFLAGS_TF;
372 static int set_flags(struct task_struct *task, unsigned long value)
374 struct pt_regs *regs = task_pt_regs(task);
377 * If the user value contains TF, mark that
378 * it was not "us" (the debugger) that set it.
379 * If not, make sure it stays set if we had.
381 if (value & X86_EFLAGS_TF)
382 clear_tsk_thread_flag(task, TIF_FORCED_TF);
383 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
384 value |= X86_EFLAGS_TF;
386 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
391 static int putreg(struct task_struct *child,
392 unsigned long offset, unsigned long value)
395 case offsetof(struct user_regs_struct, cs):
396 case offsetof(struct user_regs_struct, ds):
397 case offsetof(struct user_regs_struct, es):
398 case offsetof(struct user_regs_struct, fs):
399 case offsetof(struct user_regs_struct, gs):
400 case offsetof(struct user_regs_struct, ss):
401 return set_segment_reg(child, offset, value);
403 case offsetof(struct user_regs_struct, flags):
404 return set_flags(child, value);
407 case offsetof(struct user_regs_struct,fs_base):
408 if (value >= TASK_SIZE_OF(child))
411 * When changing the segment base, use do_arch_prctl
412 * to set either thread.fs or thread.fsindex and the
413 * corresponding GDT slot.
415 if (child->thread.fs != value)
416 return do_arch_prctl(child, ARCH_SET_FS, value);
418 case offsetof(struct user_regs_struct,gs_base):
420 * Exactly the same here as the %fs handling above.
422 if (value >= TASK_SIZE_OF(child))
424 if (child->thread.gs != value)
425 return do_arch_prctl(child, ARCH_SET_GS, value);
430 *pt_regs_access(task_pt_regs(child), offset) = value;
434 static unsigned long getreg(struct task_struct *task, unsigned long offset)
437 case offsetof(struct user_regs_struct, cs):
438 case offsetof(struct user_regs_struct, ds):
439 case offsetof(struct user_regs_struct, es):
440 case offsetof(struct user_regs_struct, fs):
441 case offsetof(struct user_regs_struct, gs):
442 case offsetof(struct user_regs_struct, ss):
443 return get_segment_reg(task, offset);
445 case offsetof(struct user_regs_struct, flags):
446 return get_flags(task);
449 case offsetof(struct user_regs_struct, fs_base): {
451 * do_arch_prctl may have used a GDT slot instead of
452 * the MSR. To userland, it appears the same either
453 * way, except the %fs segment selector might not be 0.
455 unsigned int seg = task->thread.fsindex;
456 if (task->thread.fs != 0)
457 return task->thread.fs;
459 asm("movl %%fs,%0" : "=r" (seg));
460 if (seg != FS_TLS_SEL)
462 return get_desc_base(&task->thread.tls_array[FS_TLS]);
464 case offsetof(struct user_regs_struct, gs_base): {
466 * Exactly the same here as the %fs handling above.
468 unsigned int seg = task->thread.gsindex;
469 if (task->thread.gs != 0)
470 return task->thread.gs;
472 asm("movl %%gs,%0" : "=r" (seg));
473 if (seg != GS_TLS_SEL)
475 return get_desc_base(&task->thread.tls_array[GS_TLS]);
480 return *pt_regs_access(task_pt_regs(task), offset);
483 static int genregs_get(struct task_struct *target,
484 const struct user_regset *regset,
485 unsigned int pos, unsigned int count,
486 void *kbuf, void __user *ubuf)
489 unsigned long *k = kbuf;
490 while (count >= sizeof(*k)) {
491 *k++ = getreg(target, pos);
496 unsigned long __user *u = ubuf;
497 while (count >= sizeof(*u)) {
498 if (__put_user(getreg(target, pos), u++))
508 static int genregs_set(struct task_struct *target,
509 const struct user_regset *regset,
510 unsigned int pos, unsigned int count,
511 const void *kbuf, const void __user *ubuf)
515 const unsigned long *k = kbuf;
516 while (count >= sizeof(*k) && !ret) {
517 ret = putreg(target, pos, *k++);
522 const unsigned long __user *u = ubuf;
523 while (count >= sizeof(*u) && !ret) {
525 ret = __get_user(word, u++);
528 ret = putreg(target, pos, word);
536 static void ptrace_triggered(struct perf_event *bp, int nmi,
537 struct perf_sample_data *data,
538 struct pt_regs *regs)
541 struct thread_struct *thread = &(current->thread);
544 * Store in the virtual DR6 register the fact that the breakpoint
545 * was hit so the thread's debugger will see it.
547 for (i = 0; i < HBP_NUM; i++) {
548 if (thread->ptrace_bps[i] == bp)
552 thread->debugreg6 |= (DR_TRAP0 << i);
556 * Walk through every ptrace breakpoints for this thread and
557 * build the dr7 value on top of their attributes.
560 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
564 struct arch_hw_breakpoint *info;
566 for (i = 0; i < HBP_NUM; i++) {
567 if (bp[i] && !bp[i]->attr.disabled) {
568 info = counter_arch_bp(bp[i]);
569 dr7 |= encode_dr7(i, info->len, info->type);
577 ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
578 struct task_struct *tsk, int disabled)
581 int gen_len, gen_type;
582 struct perf_event_attr attr;
585 * We should have at least an inactive breakpoint at this
586 * slot. It means the user is writing dr7 without having
587 * written the address register first
592 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
597 attr.bp_len = gen_len;
598 attr.bp_type = gen_type;
599 attr.disabled = disabled;
601 return modify_user_hw_breakpoint(bp, &attr);
605 * Handle ptrace writes to debug register 7.
607 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
609 struct thread_struct *thread = &(tsk->thread);
610 unsigned long old_dr7;
611 int i, orig_ret = 0, rc = 0;
612 int enabled, second_pass = 0;
614 struct perf_event *bp;
616 data &= ~DR_CONTROL_RESERVED;
617 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
620 * Loop through all the hardware breakpoints, making the
621 * appropriate changes to each.
623 for (i = 0; i < HBP_NUM; i++) {
624 enabled = decode_dr7(data, i, &len, &type);
625 bp = thread->ptrace_bps[i];
630 * Don't unregister the breakpoints right-away,
631 * unless all register_user_hw_breakpoint()
632 * requests have succeeded. This prevents
633 * any window of opportunity for debug
634 * register grabbing by other users.
639 rc = ptrace_modify_breakpoint(bp, len, type,
647 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
652 * Make a second pass to free the remaining unused breakpoints
653 * or to restore the original breakpoints if an error occurred.
663 return ((orig_ret < 0) ? orig_ret : rc);
667 * Handle PTRACE_PEEKUSR calls for the debug register area.
669 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
671 struct thread_struct *thread = &(tsk->thread);
672 unsigned long val = 0;
675 struct perf_event *bp;
676 bp = thread->ptrace_bps[n];
679 val = bp->hw.info.address;
681 val = thread->debugreg6;
683 val = thread->ptrace_dr7;
688 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
691 struct perf_event *bp;
692 struct thread_struct *t = &tsk->thread;
693 struct perf_event_attr attr;
695 if (!t->ptrace_bps[nr]) {
696 hw_breakpoint_init(&attr);
698 * Put stub len and type to register (reserve) an inactive but
702 attr.bp_len = HW_BREAKPOINT_LEN_1;
703 attr.bp_type = HW_BREAKPOINT_W;
706 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
709 * CHECKME: the previous code returned -EIO if the addr wasn't
710 * a valid task virtual addr. The new one will return -EINVAL in
712 * -EINVAL may be what we want for in-kernel breakpoints users,
713 * but -EIO looks better for ptrace, since we refuse a register
714 * writing for the user. And anyway this is the previous
720 t->ptrace_bps[nr] = bp;
724 bp = t->ptrace_bps[nr];
728 err = modify_user_hw_breakpoint(bp, &attr);
738 * Handle PTRACE_POKEUSR calls for the debug register area.
740 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
742 struct thread_struct *thread = &(tsk->thread);
745 /* There are no DR4 or DR5 registers */
746 if (n == 4 || n == 5)
750 thread->debugreg6 = val;
754 rc = ptrace_set_breakpoint_addr(tsk, n, val);
758 /* All that's left is DR7 */
760 rc = ptrace_write_dr7(tsk, val);
762 thread->ptrace_dr7 = val;
770 * These access the current or another (stopped) task's io permission
771 * bitmap for debugging or core dump.
773 static int ioperm_active(struct task_struct *target,
774 const struct user_regset *regset)
776 return target->thread.io_bitmap_max / regset->size;
779 static int ioperm_get(struct task_struct *target,
780 const struct user_regset *regset,
781 unsigned int pos, unsigned int count,
782 void *kbuf, void __user *ubuf)
784 if (!target->thread.io_bitmap_ptr)
787 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
788 target->thread.io_bitmap_ptr,
792 #ifdef CONFIG_X86_PTRACE_BTS
794 * A branch trace store context.
796 * Contexts may only be installed by ptrace_bts_config() and only for
799 * Contexts are destroyed when the tracee is detached from the tracer.
800 * The actual destruction work requires interrupts enabled, so the
801 * work is deferred and will be scheduled during __ptrace_unlink().
803 * Contexts hold an additional task_struct reference on the traced
804 * task, as well as a reference on the tracer's mm.
806 * Ptrace already holds a task_struct for the duration of ptrace operations,
807 * but since destruction is deferred, it may be executed after both
808 * tracer and tracee exited.
811 /* The branch trace handle. */
812 struct bts_tracer *tracer;
814 /* The buffer used to store the branch trace and its size. */
818 /* The mm that paid for the above buffer. */
819 struct mm_struct *mm;
821 /* The task this context belongs to. */
822 struct task_struct *task;
824 /* The signal to send on a bts buffer overflow. */
825 unsigned int bts_ovfl_signal;
827 /* The work struct to destroy a context. */
828 struct work_struct work;
831 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
836 err = account_locked_memory(current->mm, current->signal->rlim, size);
840 buffer = kzalloc(size, GFP_KERNEL);
844 context->buffer = buffer;
845 context->size = size;
846 context->mm = get_task_mm(current);
851 refund_locked_memory(current->mm, size);
855 static inline void free_bts_buffer(struct bts_context *context)
857 if (!context->buffer)
860 kfree(context->buffer);
861 context->buffer = NULL;
863 refund_locked_memory(context->mm, context->size);
870 static void free_bts_context_work(struct work_struct *w)
872 struct bts_context *context;
874 context = container_of(w, struct bts_context, work);
876 ds_release_bts(context->tracer);
877 put_task_struct(context->task);
878 free_bts_buffer(context);
882 static inline void free_bts_context(struct bts_context *context)
884 INIT_WORK(&context->work, free_bts_context_work);
885 schedule_work(&context->work);
888 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
890 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
892 context->task = task;
895 get_task_struct(task);
901 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
902 struct bts_struct __user *out)
904 struct bts_context *context;
905 const struct bts_trace *trace;
906 struct bts_struct bts;
907 const unsigned char *at;
910 context = child->bts;
914 trace = ds_read_bts(context->tracer);
918 at = trace->ds.top - ((index + 1) * trace->ds.size);
919 if ((void *)at < trace->ds.begin)
920 at += (trace->ds.n * trace->ds.size);
925 error = trace->read(context->tracer, at, &bts);
929 if (copy_to_user(out, &bts, sizeof(bts)))
935 static int ptrace_bts_drain(struct task_struct *child,
937 struct bts_struct __user *out)
939 struct bts_context *context;
940 const struct bts_trace *trace;
941 const unsigned char *at;
942 int error, drained = 0;
944 context = child->bts;
948 trace = ds_read_bts(context->tracer);
955 if (size < (trace->ds.top - trace->ds.begin))
958 for (at = trace->ds.begin; (void *)at < trace->ds.top;
959 out++, drained++, at += trace->ds.size) {
960 struct bts_struct bts;
962 error = trace->read(context->tracer, at, &bts);
966 if (copy_to_user(out, &bts, sizeof(bts)))
970 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
972 error = ds_reset_bts(context->tracer);
979 static int ptrace_bts_config(struct task_struct *child,
981 const struct ptrace_bts_config __user *ucfg)
983 struct bts_context *context;
984 struct ptrace_bts_config cfg;
985 unsigned int flags = 0;
987 if (cfg_size < sizeof(cfg))
990 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
993 context = child->bts;
995 context = alloc_bts_context(child);
999 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1004 context->bts_ovfl_signal = cfg.signal;
1007 ds_release_bts(context->tracer);
1008 context->tracer = NULL;
1010 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1013 free_bts_buffer(context);
1017 err = alloc_bts_buffer(context, cfg.size);
1022 if (cfg.flags & PTRACE_BTS_O_TRACE)
1025 if (cfg.flags & PTRACE_BTS_O_SCHED)
1026 flags |= BTS_TIMESTAMPS;
1029 ds_request_bts_task(child, context->buffer, context->size,
1030 NULL, (size_t)-1, flags);
1031 if (unlikely(IS_ERR(context->tracer))) {
1032 int error = PTR_ERR(context->tracer);
1034 free_bts_buffer(context);
1035 context->tracer = NULL;
1042 static int ptrace_bts_status(struct task_struct *child,
1044 struct ptrace_bts_config __user *ucfg)
1046 struct bts_context *context;
1047 const struct bts_trace *trace;
1048 struct ptrace_bts_config cfg;
1050 context = child->bts;
1054 if (cfg_size < sizeof(cfg))
1057 trace = ds_read_bts(context->tracer);
1061 memset(&cfg, 0, sizeof(cfg));
1062 cfg.size = trace->ds.end - trace->ds.begin;
1063 cfg.signal = context->bts_ovfl_signal;
1064 cfg.bts_size = sizeof(struct bts_struct);
1067 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1069 if (trace->ds.flags & BTS_USER)
1070 cfg.flags |= PTRACE_BTS_O_TRACE;
1072 if (trace->ds.flags & BTS_TIMESTAMPS)
1073 cfg.flags |= PTRACE_BTS_O_SCHED;
1075 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1081 static int ptrace_bts_clear(struct task_struct *child)
1083 struct bts_context *context;
1084 const struct bts_trace *trace;
1086 context = child->bts;
1090 trace = ds_read_bts(context->tracer);
1094 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
1096 return ds_reset_bts(context->tracer);
1099 static int ptrace_bts_size(struct task_struct *child)
1101 struct bts_context *context;
1102 const struct bts_trace *trace;
1104 context = child->bts;
1108 trace = ds_read_bts(context->tracer);
1112 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1116 * Called from __ptrace_unlink() after the child has been moved back
1117 * to its original parent.
1119 void ptrace_bts_untrace(struct task_struct *child)
1121 if (unlikely(child->bts)) {
1122 free_bts_context(child->bts);
1126 #endif /* CONFIG_X86_PTRACE_BTS */
1129 * Called by kernel/ptrace.c when detaching..
1131 * Make sure the single step bit is not set.
1133 void ptrace_disable(struct task_struct *child)
1135 user_disable_single_step(child);
1136 #ifdef TIF_SYSCALL_EMU
1137 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1141 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1142 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1145 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1148 unsigned long __user *datap = (unsigned long __user *)data;
1151 /* read the word at location addr in the USER area. */
1152 case PTRACE_PEEKUSR: {
1156 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1157 addr >= sizeof(struct user))
1160 tmp = 0; /* Default return condition */
1161 if (addr < sizeof(struct user_regs_struct))
1162 tmp = getreg(child, addr);
1163 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1164 addr <= offsetof(struct user, u_debugreg[7])) {
1165 addr -= offsetof(struct user, u_debugreg[0]);
1166 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1168 ret = put_user(tmp, datap);
1172 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1174 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1175 addr >= sizeof(struct user))
1178 if (addr < sizeof(struct user_regs_struct))
1179 ret = putreg(child, addr, data);
1180 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1181 addr <= offsetof(struct user, u_debugreg[7])) {
1182 addr -= offsetof(struct user, u_debugreg[0]);
1183 ret = ptrace_set_debugreg(child,
1184 addr / sizeof(data), data);
1188 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1189 return copy_regset_to_user(child,
1190 task_user_regset_view(current),
1192 0, sizeof(struct user_regs_struct),
1195 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1196 return copy_regset_from_user(child,
1197 task_user_regset_view(current),
1199 0, sizeof(struct user_regs_struct),
1202 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1203 return copy_regset_to_user(child,
1204 task_user_regset_view(current),
1206 0, sizeof(struct user_i387_struct),
1209 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1210 return copy_regset_from_user(child,
1211 task_user_regset_view(current),
1213 0, sizeof(struct user_i387_struct),
1216 #ifdef CONFIG_X86_32
1217 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1218 return copy_regset_to_user(child, &user_x86_32_view,
1220 0, sizeof(struct user_fxsr_struct),
1223 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1224 return copy_regset_from_user(child, &user_x86_32_view,
1226 0, sizeof(struct user_fxsr_struct),
1230 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1231 case PTRACE_GET_THREAD_AREA:
1234 ret = do_get_thread_area(child, addr,
1235 (struct user_desc __user *) data);
1238 case PTRACE_SET_THREAD_AREA:
1241 ret = do_set_thread_area(child, addr,
1242 (struct user_desc __user *) data, 0);
1246 #ifdef CONFIG_X86_64
1247 /* normal 64bit interface to access TLS data.
1248 Works just like arch_prctl, except that the arguments
1250 case PTRACE_ARCH_PRCTL:
1251 ret = do_arch_prctl(child, data, addr);
1256 * These bits need more cooking - not enabled yet:
1258 #ifdef CONFIG_X86_PTRACE_BTS
1259 case PTRACE_BTS_CONFIG:
1260 ret = ptrace_bts_config
1261 (child, data, (struct ptrace_bts_config __user *)addr);
1264 case PTRACE_BTS_STATUS:
1265 ret = ptrace_bts_status
1266 (child, data, (struct ptrace_bts_config __user *)addr);
1269 case PTRACE_BTS_SIZE:
1270 ret = ptrace_bts_size(child);
1273 case PTRACE_BTS_GET:
1274 ret = ptrace_bts_read_record
1275 (child, data, (struct bts_struct __user *) addr);
1278 case PTRACE_BTS_CLEAR:
1279 ret = ptrace_bts_clear(child);
1282 case PTRACE_BTS_DRAIN:
1283 ret = ptrace_bts_drain
1284 (child, data, (struct bts_struct __user *) addr);
1286 #endif /* CONFIG_X86_PTRACE_BTS */
1289 ret = ptrace_request(child, request, addr, data);
1296 #ifdef CONFIG_IA32_EMULATION
1298 #include <linux/compat.h>
1299 #include <linux/syscalls.h>
1300 #include <asm/ia32.h>
1301 #include <asm/user32.h>
1304 case offsetof(struct user32, regs.l): \
1305 regs->q = value; break
1308 case offsetof(struct user32, regs.rs): \
1309 return set_segment_reg(child, \
1310 offsetof(struct user_regs_struct, rs), \
1314 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1316 struct pt_regs *regs = task_pt_regs(child);
1337 case offsetof(struct user32, regs.orig_eax):
1339 * A 32-bit debugger setting orig_eax means to restore
1340 * the state of the task restarting a 32-bit syscall.
1341 * Make sure we interpret the -ERESTART* codes correctly
1342 * in case the task is not actually still sitting at the
1343 * exit from a 32-bit syscall with TS_COMPAT still set.
1345 regs->orig_ax = value;
1346 if (syscall_get_nr(child, regs) >= 0)
1347 task_thread_info(child)->status |= TS_COMPAT;
1350 case offsetof(struct user32, regs.eflags):
1351 return set_flags(child, value);
1353 case offsetof(struct user32, u_debugreg[0]) ...
1354 offsetof(struct user32, u_debugreg[7]):
1355 regno -= offsetof(struct user32, u_debugreg[0]);
1356 return ptrace_set_debugreg(child, regno / 4, value);
1359 if (regno > sizeof(struct user32) || (regno & 3))
1363 * Other dummy fields in the virtual user structure
1375 case offsetof(struct user32, regs.l): \
1376 *val = regs->q; break
1379 case offsetof(struct user32, regs.rs): \
1380 *val = get_segment_reg(child, \
1381 offsetof(struct user_regs_struct, rs)); \
1384 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1386 struct pt_regs *regs = task_pt_regs(child);
1404 R32(orig_eax, orig_ax);
1408 case offsetof(struct user32, regs.eflags):
1409 *val = get_flags(child);
1412 case offsetof(struct user32, u_debugreg[0]) ...
1413 offsetof(struct user32, u_debugreg[7]):
1414 regno -= offsetof(struct user32, u_debugreg[0]);
1415 *val = ptrace_get_debugreg(child, regno / 4);
1419 if (regno > sizeof(struct user32) || (regno & 3))
1423 * Other dummy fields in the virtual user structure
1435 static int genregs32_get(struct task_struct *target,
1436 const struct user_regset *regset,
1437 unsigned int pos, unsigned int count,
1438 void *kbuf, void __user *ubuf)
1441 compat_ulong_t *k = kbuf;
1442 while (count >= sizeof(*k)) {
1443 getreg32(target, pos, k++);
1444 count -= sizeof(*k);
1448 compat_ulong_t __user *u = ubuf;
1449 while (count >= sizeof(*u)) {
1450 compat_ulong_t word;
1451 getreg32(target, pos, &word);
1452 if (__put_user(word, u++))
1454 count -= sizeof(*u);
1462 static int genregs32_set(struct task_struct *target,
1463 const struct user_regset *regset,
1464 unsigned int pos, unsigned int count,
1465 const void *kbuf, const void __user *ubuf)
1469 const compat_ulong_t *k = kbuf;
1470 while (count >= sizeof(*k) && !ret) {
1471 ret = putreg32(target, pos, *k++);
1472 count -= sizeof(*k);
1476 const compat_ulong_t __user *u = ubuf;
1477 while (count >= sizeof(*u) && !ret) {
1478 compat_ulong_t word;
1479 ret = __get_user(word, u++);
1482 ret = putreg32(target, pos, word);
1483 count -= sizeof(*u);
1490 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1491 compat_ulong_t caddr, compat_ulong_t cdata)
1493 unsigned long addr = caddr;
1494 unsigned long data = cdata;
1495 void __user *datap = compat_ptr(data);
1500 case PTRACE_PEEKUSR:
1501 ret = getreg32(child, addr, &val);
1503 ret = put_user(val, (__u32 __user *)datap);
1506 case PTRACE_POKEUSR:
1507 ret = putreg32(child, addr, data);
1510 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1511 return copy_regset_to_user(child, &user_x86_32_view,
1513 0, sizeof(struct user_regs_struct32),
1516 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1517 return copy_regset_from_user(child, &user_x86_32_view,
1519 sizeof(struct user_regs_struct32),
1522 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1523 return copy_regset_to_user(child, &user_x86_32_view,
1525 sizeof(struct user_i387_ia32_struct),
1528 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1529 return copy_regset_from_user(
1530 child, &user_x86_32_view, REGSET_FP,
1531 0, sizeof(struct user_i387_ia32_struct), datap);
1533 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1534 return copy_regset_to_user(child, &user_x86_32_view,
1536 sizeof(struct user32_fxsr_struct),
1539 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1540 return copy_regset_from_user(child, &user_x86_32_view,
1542 sizeof(struct user32_fxsr_struct),
1545 case PTRACE_GET_THREAD_AREA:
1546 case PTRACE_SET_THREAD_AREA:
1547 #ifdef CONFIG_X86_PTRACE_BTS
1548 case PTRACE_BTS_CONFIG:
1549 case PTRACE_BTS_STATUS:
1550 case PTRACE_BTS_SIZE:
1551 case PTRACE_BTS_GET:
1552 case PTRACE_BTS_CLEAR:
1553 case PTRACE_BTS_DRAIN:
1554 #endif /* CONFIG_X86_PTRACE_BTS */
1555 return arch_ptrace(child, request, addr, data);
1558 return compat_ptrace_request(child, request, addr, data);
1564 #endif /* CONFIG_IA32_EMULATION */
1566 #ifdef CONFIG_X86_64
1568 static struct user_regset x86_64_regsets[] __read_mostly = {
1569 [REGSET_GENERAL] = {
1570 .core_note_type = NT_PRSTATUS,
1571 .n = sizeof(struct user_regs_struct) / sizeof(long),
1572 .size = sizeof(long), .align = sizeof(long),
1573 .get = genregs_get, .set = genregs_set
1576 .core_note_type = NT_PRFPREG,
1577 .n = sizeof(struct user_i387_struct) / sizeof(long),
1578 .size = sizeof(long), .align = sizeof(long),
1579 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1582 .core_note_type = NT_X86_XSTATE,
1583 .size = sizeof(u64), .align = sizeof(u64),
1584 .active = xstateregs_active, .get = xstateregs_get,
1585 .set = xstateregs_set
1587 [REGSET_IOPERM64] = {
1588 .core_note_type = NT_386_IOPERM,
1589 .n = IO_BITMAP_LONGS,
1590 .size = sizeof(long), .align = sizeof(long),
1591 .active = ioperm_active, .get = ioperm_get
1595 static const struct user_regset_view user_x86_64_view = {
1596 .name = "x86_64", .e_machine = EM_X86_64,
1597 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1600 #else /* CONFIG_X86_32 */
1602 #define user_regs_struct32 user_regs_struct
1603 #define genregs32_get genregs_get
1604 #define genregs32_set genregs_set
1606 #define user_i387_ia32_struct user_i387_struct
1607 #define user32_fxsr_struct user_fxsr_struct
1609 #endif /* CONFIG_X86_64 */
1611 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1612 static struct user_regset x86_32_regsets[] __read_mostly = {
1613 [REGSET_GENERAL] = {
1614 .core_note_type = NT_PRSTATUS,
1615 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1616 .size = sizeof(u32), .align = sizeof(u32),
1617 .get = genregs32_get, .set = genregs32_set
1620 .core_note_type = NT_PRFPREG,
1621 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1622 .size = sizeof(u32), .align = sizeof(u32),
1623 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1626 .core_note_type = NT_PRXFPREG,
1627 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1628 .size = sizeof(u32), .align = sizeof(u32),
1629 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1632 .core_note_type = NT_X86_XSTATE,
1633 .size = sizeof(u64), .align = sizeof(u64),
1634 .active = xstateregs_active, .get = xstateregs_get,
1635 .set = xstateregs_set
1638 .core_note_type = NT_386_TLS,
1639 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1640 .size = sizeof(struct user_desc),
1641 .align = sizeof(struct user_desc),
1642 .active = regset_tls_active,
1643 .get = regset_tls_get, .set = regset_tls_set
1645 [REGSET_IOPERM32] = {
1646 .core_note_type = NT_386_IOPERM,
1647 .n = IO_BITMAP_BYTES / sizeof(u32),
1648 .size = sizeof(u32), .align = sizeof(u32),
1649 .active = ioperm_active, .get = ioperm_get
1653 static const struct user_regset_view user_x86_32_view = {
1654 .name = "i386", .e_machine = EM_386,
1655 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1660 * This represents bytes 464..511 in the memory layout exported through
1661 * the REGSET_XSTATE interface.
1663 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1665 void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1667 #ifdef CONFIG_X86_64
1668 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1670 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1671 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1673 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1676 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1678 #ifdef CONFIG_IA32_EMULATION
1679 if (test_tsk_thread_flag(task, TIF_IA32))
1681 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1682 return &user_x86_32_view;
1684 #ifdef CONFIG_X86_64
1685 return &user_x86_64_view;
1689 static void fill_sigtrap_info(struct task_struct *tsk,
1690 struct pt_regs *regs,
1691 int error_code, int si_code,
1692 struct siginfo *info)
1694 tsk->thread.trap_no = 1;
1695 tsk->thread.error_code = error_code;
1697 memset(info, 0, sizeof(*info));
1698 info->si_signo = SIGTRAP;
1699 info->si_code = si_code;
1700 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1703 void user_single_step_siginfo(struct task_struct *tsk,
1704 struct pt_regs *regs,
1705 struct siginfo *info)
1707 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1710 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1711 int error_code, int si_code)
1713 struct siginfo info;
1715 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1716 /* Send us the fake SIGTRAP */
1717 force_sig_info(SIGTRAP, &info, tsk);
1721 #ifdef CONFIG_X86_32
1723 #elif defined CONFIG_IA32_EMULATION
1724 # define IS_IA32 is_compat_task()
1730 * We must return the syscall number to actually look up in the table.
1731 * This can be -1L to skip running any syscall at all.
1733 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1738 * If we stepped into a sysenter/syscall insn, it trapped in
1739 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1740 * If user-mode had set TF itself, then it's still clear from
1741 * do_debug() and we need to set it again to restore the user
1742 * state. If we entered on the slow path, TF was already set.
1744 if (test_thread_flag(TIF_SINGLESTEP))
1745 regs->flags |= X86_EFLAGS_TF;
1747 /* do the secure computing check first */
1748 secure_computing(regs->orig_ax);
1750 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1753 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1754 tracehook_report_syscall_entry(regs))
1757 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1758 trace_sys_enter(regs, regs->orig_ax);
1760 if (unlikely(current->audit_context)) {
1762 audit_syscall_entry(AUDIT_ARCH_I386,
1765 regs->dx, regs->si);
1766 #ifdef CONFIG_X86_64
1768 audit_syscall_entry(AUDIT_ARCH_X86_64,
1771 regs->dx, regs->r10);
1775 return ret ?: regs->orig_ax;
1778 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1782 if (unlikely(current->audit_context))
1783 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1785 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1786 trace_sys_exit(regs, regs->ax);
1789 * If TIF_SYSCALL_EMU is set, we only get here because of
1790 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1791 * We already reported this syscall instruction in
1792 * syscall_trace_enter().
1794 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1795 !test_thread_flag(TIF_SYSCALL_EMU);
1796 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1797 tracehook_report_syscall_exit(regs, step);