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>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/processor.h>
31 #include <asm/debugreg.h>
34 #include <asm/prctl.h>
35 #include <asm/proto.h>
37 #include <asm/hw_breakpoint.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/syscalls.h>
48 REGSET_IOPERM64 = REGSET_XFP,
54 * does not yet catch signals sent when the child dies.
55 * in exit.c or in signal.c.
59 * Determines which flags the user has access to [1 = access, 0 = no access].
61 #define FLAG_MASK_32 ((unsigned long) \
62 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
63 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
64 X86_EFLAGS_SF | X86_EFLAGS_TF | \
65 X86_EFLAGS_DF | X86_EFLAGS_OF | \
66 X86_EFLAGS_RF | X86_EFLAGS_AC))
69 * Determines whether a value may be installed in a segment register.
71 static inline bool invalid_selector(u16 value)
73 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
78 #define FLAG_MASK FLAG_MASK_32
80 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
82 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
83 return ®s->bx + (regno >> 2);
86 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
89 * Returning the value truncates it to 16 bits.
92 if (offset != offsetof(struct user_regs_struct, gs))
93 retval = *pt_regs_access(task_pt_regs(task), offset);
96 retval = get_user_gs(task_pt_regs(task));
98 retval = task_user_gs(task);
103 static int set_segment_reg(struct task_struct *task,
104 unsigned long offset, u16 value)
107 * The value argument was already truncated to 16 bits.
109 if (invalid_selector(value))
113 * For %cs and %ss we cannot permit a null selector.
114 * We can permit a bogus selector as long as it has USER_RPL.
115 * Null selectors are fine for other segment registers, but
116 * we will never get back to user mode with invalid %cs or %ss
117 * and will take the trap in iret instead. Much code relies
118 * on user_mode() to distinguish a user trap frame (which can
119 * safely use invalid selectors) from a kernel trap frame.
122 case offsetof(struct user_regs_struct, cs):
123 case offsetof(struct user_regs_struct, ss):
124 if (unlikely(value == 0))
128 *pt_regs_access(task_pt_regs(task), offset) = value;
131 case offsetof(struct user_regs_struct, gs):
133 set_user_gs(task_pt_regs(task), value);
135 task_user_gs(task) = value;
141 #else /* CONFIG_X86_64 */
143 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
145 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
147 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
148 return ®s->r15 + (offset / sizeof(regs->r15));
151 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
154 * Returning the value truncates it to 16 bits.
159 case offsetof(struct user_regs_struct, fs):
160 if (task == current) {
161 /* Older gas can't assemble movq %?s,%r?? */
162 asm("movl %%fs,%0" : "=r" (seg));
165 return task->thread.fsindex;
166 case offsetof(struct user_regs_struct, gs):
167 if (task == current) {
168 asm("movl %%gs,%0" : "=r" (seg));
171 return task->thread.gsindex;
172 case offsetof(struct user_regs_struct, ds):
173 if (task == current) {
174 asm("movl %%ds,%0" : "=r" (seg));
177 return task->thread.ds;
178 case offsetof(struct user_regs_struct, es):
179 if (task == current) {
180 asm("movl %%es,%0" : "=r" (seg));
183 return task->thread.es;
185 case offsetof(struct user_regs_struct, cs):
186 case offsetof(struct user_regs_struct, ss):
189 return *pt_regs_access(task_pt_regs(task), offset);
192 static int set_segment_reg(struct task_struct *task,
193 unsigned long offset, u16 value)
196 * The value argument was already truncated to 16 bits.
198 if (invalid_selector(value))
202 case offsetof(struct user_regs_struct,fs):
204 * If this is setting fs as for normal 64-bit use but
205 * setting fs_base has implicitly changed it, leave it.
207 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
208 task->thread.fs != 0) ||
209 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
210 task->thread.fs == 0))
212 task->thread.fsindex = value;
214 loadsegment(fs, task->thread.fsindex);
216 case offsetof(struct user_regs_struct,gs):
218 * If this is setting gs as for normal 64-bit use but
219 * setting gs_base has implicitly changed it, leave it.
221 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
222 task->thread.gs != 0) ||
223 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
224 task->thread.gs == 0))
226 task->thread.gsindex = value;
228 load_gs_index(task->thread.gsindex);
230 case offsetof(struct user_regs_struct,ds):
231 task->thread.ds = value;
233 loadsegment(ds, task->thread.ds);
235 case offsetof(struct user_regs_struct,es):
236 task->thread.es = value;
238 loadsegment(es, task->thread.es);
242 * Can't actually change these in 64-bit mode.
244 case offsetof(struct user_regs_struct,cs):
245 if (unlikely(value == 0))
247 #ifdef CONFIG_IA32_EMULATION
248 if (test_tsk_thread_flag(task, TIF_IA32))
249 task_pt_regs(task)->cs = value;
252 case offsetof(struct user_regs_struct,ss):
253 if (unlikely(value == 0))
255 #ifdef CONFIG_IA32_EMULATION
256 if (test_tsk_thread_flag(task, TIF_IA32))
257 task_pt_regs(task)->ss = value;
265 #endif /* CONFIG_X86_32 */
267 static unsigned long get_flags(struct task_struct *task)
269 unsigned long retval = task_pt_regs(task)->flags;
272 * If the debugger set TF, hide it from the readout.
274 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
275 retval &= ~X86_EFLAGS_TF;
280 static int set_flags(struct task_struct *task, unsigned long value)
282 struct pt_regs *regs = task_pt_regs(task);
285 * If the user value contains TF, mark that
286 * it was not "us" (the debugger) that set it.
287 * If not, make sure it stays set if we had.
289 if (value & X86_EFLAGS_TF)
290 clear_tsk_thread_flag(task, TIF_FORCED_TF);
291 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
292 value |= X86_EFLAGS_TF;
294 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
299 static int putreg(struct task_struct *child,
300 unsigned long offset, unsigned long value)
303 case offsetof(struct user_regs_struct, cs):
304 case offsetof(struct user_regs_struct, ds):
305 case offsetof(struct user_regs_struct, es):
306 case offsetof(struct user_regs_struct, fs):
307 case offsetof(struct user_regs_struct, gs):
308 case offsetof(struct user_regs_struct, ss):
309 return set_segment_reg(child, offset, value);
311 case offsetof(struct user_regs_struct, flags):
312 return set_flags(child, value);
315 case offsetof(struct user_regs_struct,fs_base):
316 if (value >= TASK_SIZE_OF(child))
319 * When changing the segment base, use do_arch_prctl
320 * to set either thread.fs or thread.fsindex and the
321 * corresponding GDT slot.
323 if (child->thread.fs != value)
324 return do_arch_prctl(child, ARCH_SET_FS, value);
326 case offsetof(struct user_regs_struct,gs_base):
328 * Exactly the same here as the %fs handling above.
330 if (value >= TASK_SIZE_OF(child))
332 if (child->thread.gs != value)
333 return do_arch_prctl(child, ARCH_SET_GS, value);
338 *pt_regs_access(task_pt_regs(child), offset) = value;
342 static unsigned long getreg(struct task_struct *task, unsigned long offset)
345 case offsetof(struct user_regs_struct, cs):
346 case offsetof(struct user_regs_struct, ds):
347 case offsetof(struct user_regs_struct, es):
348 case offsetof(struct user_regs_struct, fs):
349 case offsetof(struct user_regs_struct, gs):
350 case offsetof(struct user_regs_struct, ss):
351 return get_segment_reg(task, offset);
353 case offsetof(struct user_regs_struct, flags):
354 return get_flags(task);
357 case offsetof(struct user_regs_struct, fs_base): {
359 * do_arch_prctl may have used a GDT slot instead of
360 * the MSR. To userland, it appears the same either
361 * way, except the %fs segment selector might not be 0.
363 unsigned int seg = task->thread.fsindex;
364 if (task->thread.fs != 0)
365 return task->thread.fs;
367 asm("movl %%fs,%0" : "=r" (seg));
368 if (seg != FS_TLS_SEL)
370 return get_desc_base(&task->thread.tls_array[FS_TLS]);
372 case offsetof(struct user_regs_struct, gs_base): {
374 * Exactly the same here as the %fs handling above.
376 unsigned int seg = task->thread.gsindex;
377 if (task->thread.gs != 0)
378 return task->thread.gs;
380 asm("movl %%gs,%0" : "=r" (seg));
381 if (seg != GS_TLS_SEL)
383 return get_desc_base(&task->thread.tls_array[GS_TLS]);
388 return *pt_regs_access(task_pt_regs(task), offset);
391 static int genregs_get(struct task_struct *target,
392 const struct user_regset *regset,
393 unsigned int pos, unsigned int count,
394 void *kbuf, void __user *ubuf)
397 unsigned long *k = kbuf;
399 *k++ = getreg(target, pos);
404 unsigned long __user *u = ubuf;
406 if (__put_user(getreg(target, pos), u++))
416 static int genregs_set(struct task_struct *target,
417 const struct user_regset *regset,
418 unsigned int pos, unsigned int count,
419 const void *kbuf, const void __user *ubuf)
423 const unsigned long *k = kbuf;
424 while (count > 0 && !ret) {
425 ret = putreg(target, pos, *k++);
430 const unsigned long __user *u = ubuf;
431 while (count > 0 && !ret) {
433 ret = __get_user(word, u++);
436 ret = putreg(target, pos, word);
445 * Decode the length and type bits for a particular breakpoint as
446 * stored in debug register 7. Return the "enabled" status.
448 static int decode_dr7(unsigned long dr7, int bpnum, unsigned *len,
451 int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
453 *len = (bp_info & 0xc) | 0x40;
454 *type = (bp_info & 0x3) | 0x80;
455 return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
458 static void ptrace_triggered(struct hw_breakpoint *bp, struct pt_regs *regs)
460 struct thread_struct *thread = &(current->thread);
464 * Store in the virtual DR6 register the fact that the breakpoint
465 * was hit so the thread's debugger will see it.
467 for (i = 0; i < hbp_kernel_pos; i++)
469 * We will check bp->info.address against the address stored in
470 * thread's hbp structure and not debugreg[i]. This is to ensure
471 * that the corresponding bit for 'i' in DR7 register is enabled
473 if (bp->info.address == thread->hbp[i]->info.address)
476 thread->debugreg6 |= (DR_TRAP0 << i);
480 * Handle ptrace writes to debug register 7.
482 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
484 struct thread_struct *thread = &(tsk->thread);
485 unsigned long old_dr7 = thread->debugreg7;
486 int i, orig_ret = 0, rc = 0;
487 int enabled, second_pass = 0;
489 struct hw_breakpoint *bp;
491 data &= ~DR_CONTROL_RESERVED;
494 * Loop through all the hardware breakpoints, making the
495 * appropriate changes to each.
497 for (i = 0; i < HBP_NUM; i++) {
498 enabled = decode_dr7(data, i, &len, &type);
503 /* Don't unregister the breakpoints right-away,
504 * unless all register_user_hw_breakpoint()
505 * requests have succeeded. This prevents
506 * any window of opportunity for debug
507 * register grabbing by other users.
511 unregister_user_hw_breakpoint(tsk, bp);
518 bp = kzalloc(sizeof(struct hw_breakpoint), GFP_KERNEL);
520 bp->info.address = thread->debugreg[i];
521 bp->triggered = ptrace_triggered;
523 bp->info.type = type;
524 rc = register_user_hw_breakpoint(tsk, bp);
529 rc = modify_user_hw_breakpoint(tsk, bp);
534 * Make a second pass to free the remaining unused breakpoints
535 * or to restore the original breakpoints if an error occurred.
545 return ((orig_ret < 0) ? orig_ret : rc);
549 * Handle PTRACE_PEEKUSR calls for the debug register area.
551 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
553 struct thread_struct *thread = &(tsk->thread);
554 unsigned long val = 0;
557 val = thread->debugreg[n];
559 val = thread->debugreg6;
561 val = thread->debugreg7;
566 * Handle PTRACE_POKEUSR calls for the debug register area.
568 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
570 struct thread_struct *thread = &(tsk->thread);
573 /* There are no DR4 or DR5 registers */
574 if (n == 4 || n == 5)
578 tsk->thread.debugreg6 = val;
582 if (thread->hbp[n]) {
583 if (arch_check_va_in_userspace(val,
584 thread->hbp[n]->info.len) == 0) {
588 thread->hbp[n]->info.address = val;
590 thread->debugreg[n] = val;
592 /* All that's left is DR7 */
594 rc = ptrace_write_dr7(tsk, val);
601 * These access the current or another (stopped) task's io permission
602 * bitmap for debugging or core dump.
604 static int ioperm_active(struct task_struct *target,
605 const struct user_regset *regset)
607 return target->thread.io_bitmap_max / regset->size;
610 static int ioperm_get(struct task_struct *target,
611 const struct user_regset *regset,
612 unsigned int pos, unsigned int count,
613 void *kbuf, void __user *ubuf)
615 if (!target->thread.io_bitmap_ptr)
618 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
619 target->thread.io_bitmap_ptr,
623 #ifdef CONFIG_X86_PTRACE_BTS
625 * A branch trace store context.
627 * Contexts may only be installed by ptrace_bts_config() and only for
630 * Contexts are destroyed when the tracee is detached from the tracer.
631 * The actual destruction work requires interrupts enabled, so the
632 * work is deferred and will be scheduled during __ptrace_unlink().
634 * Contexts hold an additional task_struct reference on the traced
635 * task, as well as a reference on the tracer's mm.
637 * Ptrace already holds a task_struct for the duration of ptrace operations,
638 * but since destruction is deferred, it may be executed after both
639 * tracer and tracee exited.
642 /* The branch trace handle. */
643 struct bts_tracer *tracer;
645 /* The buffer used to store the branch trace and its size. */
649 /* The mm that paid for the above buffer. */
650 struct mm_struct *mm;
652 /* The task this context belongs to. */
653 struct task_struct *task;
655 /* The signal to send on a bts buffer overflow. */
656 unsigned int bts_ovfl_signal;
658 /* The work struct to destroy a context. */
659 struct work_struct work;
662 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
667 err = account_locked_memory(current->mm, current->signal->rlim, size);
671 buffer = kzalloc(size, GFP_KERNEL);
675 context->buffer = buffer;
676 context->size = size;
677 context->mm = get_task_mm(current);
682 refund_locked_memory(current->mm, size);
686 static inline void free_bts_buffer(struct bts_context *context)
688 if (!context->buffer)
691 kfree(context->buffer);
692 context->buffer = NULL;
694 refund_locked_memory(context->mm, context->size);
701 static void free_bts_context_work(struct work_struct *w)
703 struct bts_context *context;
705 context = container_of(w, struct bts_context, work);
707 ds_release_bts(context->tracer);
708 put_task_struct(context->task);
709 free_bts_buffer(context);
713 static inline void free_bts_context(struct bts_context *context)
715 INIT_WORK(&context->work, free_bts_context_work);
716 schedule_work(&context->work);
719 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
721 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
723 context->task = task;
726 get_task_struct(task);
732 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
733 struct bts_struct __user *out)
735 struct bts_context *context;
736 const struct bts_trace *trace;
737 struct bts_struct bts;
738 const unsigned char *at;
741 context = child->bts;
745 trace = ds_read_bts(context->tracer);
749 at = trace->ds.top - ((index + 1) * trace->ds.size);
750 if ((void *)at < trace->ds.begin)
751 at += (trace->ds.n * trace->ds.size);
756 error = trace->read(context->tracer, at, &bts);
760 if (copy_to_user(out, &bts, sizeof(bts)))
766 static int ptrace_bts_drain(struct task_struct *child,
768 struct bts_struct __user *out)
770 struct bts_context *context;
771 const struct bts_trace *trace;
772 const unsigned char *at;
773 int error, drained = 0;
775 context = child->bts;
779 trace = ds_read_bts(context->tracer);
786 if (size < (trace->ds.top - trace->ds.begin))
789 for (at = trace->ds.begin; (void *)at < trace->ds.top;
790 out++, drained++, at += trace->ds.size) {
791 struct bts_struct bts;
793 error = trace->read(context->tracer, at, &bts);
797 if (copy_to_user(out, &bts, sizeof(bts)))
801 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
803 error = ds_reset_bts(context->tracer);
810 static int ptrace_bts_config(struct task_struct *child,
812 const struct ptrace_bts_config __user *ucfg)
814 struct bts_context *context;
815 struct ptrace_bts_config cfg;
816 unsigned int flags = 0;
818 if (cfg_size < sizeof(cfg))
821 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
824 context = child->bts;
826 context = alloc_bts_context(child);
830 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
835 context->bts_ovfl_signal = cfg.signal;
838 ds_release_bts(context->tracer);
839 context->tracer = NULL;
841 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
844 free_bts_buffer(context);
848 err = alloc_bts_buffer(context, cfg.size);
853 if (cfg.flags & PTRACE_BTS_O_TRACE)
856 if (cfg.flags & PTRACE_BTS_O_SCHED)
857 flags |= BTS_TIMESTAMPS;
860 ds_request_bts_task(child, context->buffer, context->size,
861 NULL, (size_t)-1, flags);
862 if (unlikely(IS_ERR(context->tracer))) {
863 int error = PTR_ERR(context->tracer);
865 free_bts_buffer(context);
866 context->tracer = NULL;
873 static int ptrace_bts_status(struct task_struct *child,
875 struct ptrace_bts_config __user *ucfg)
877 struct bts_context *context;
878 const struct bts_trace *trace;
879 struct ptrace_bts_config cfg;
881 context = child->bts;
885 if (cfg_size < sizeof(cfg))
888 trace = ds_read_bts(context->tracer);
892 memset(&cfg, 0, sizeof(cfg));
893 cfg.size = trace->ds.end - trace->ds.begin;
894 cfg.signal = context->bts_ovfl_signal;
895 cfg.bts_size = sizeof(struct bts_struct);
898 cfg.flags |= PTRACE_BTS_O_SIGNAL;
900 if (trace->ds.flags & BTS_USER)
901 cfg.flags |= PTRACE_BTS_O_TRACE;
903 if (trace->ds.flags & BTS_TIMESTAMPS)
904 cfg.flags |= PTRACE_BTS_O_SCHED;
906 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
912 static int ptrace_bts_clear(struct task_struct *child)
914 struct bts_context *context;
915 const struct bts_trace *trace;
917 context = child->bts;
921 trace = ds_read_bts(context->tracer);
925 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
927 return ds_reset_bts(context->tracer);
930 static int ptrace_bts_size(struct task_struct *child)
932 struct bts_context *context;
933 const struct bts_trace *trace;
935 context = child->bts;
939 trace = ds_read_bts(context->tracer);
943 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
947 * Called from __ptrace_unlink() after the child has been moved back
948 * to its original parent.
950 void ptrace_bts_untrace(struct task_struct *child)
952 if (unlikely(child->bts)) {
953 free_bts_context(child->bts);
957 #endif /* CONFIG_X86_PTRACE_BTS */
960 * Called by kernel/ptrace.c when detaching..
962 * Make sure the single step bit is not set.
964 void ptrace_disable(struct task_struct *child)
966 user_disable_single_step(child);
967 #ifdef TIF_SYSCALL_EMU
968 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
972 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
973 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
976 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
979 unsigned long __user *datap = (unsigned long __user *)data;
982 /* read the word at location addr in the USER area. */
983 case PTRACE_PEEKUSR: {
987 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
988 addr >= sizeof(struct user))
991 tmp = 0; /* Default return condition */
992 if (addr < sizeof(struct user_regs_struct))
993 tmp = getreg(child, addr);
994 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
995 addr <= offsetof(struct user, u_debugreg[7])) {
996 addr -= offsetof(struct user, u_debugreg[0]);
997 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
999 ret = put_user(tmp, datap);
1003 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1005 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1006 addr >= sizeof(struct user))
1009 if (addr < sizeof(struct user_regs_struct))
1010 ret = putreg(child, addr, data);
1011 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1012 addr <= offsetof(struct user, u_debugreg[7])) {
1013 addr -= offsetof(struct user, u_debugreg[0]);
1014 ret = ptrace_set_debugreg(child,
1015 addr / sizeof(data), data);
1019 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1020 return copy_regset_to_user(child,
1021 task_user_regset_view(current),
1023 0, sizeof(struct user_regs_struct),
1026 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1027 return copy_regset_from_user(child,
1028 task_user_regset_view(current),
1030 0, sizeof(struct user_regs_struct),
1033 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1034 return copy_regset_to_user(child,
1035 task_user_regset_view(current),
1037 0, sizeof(struct user_i387_struct),
1040 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1041 return copy_regset_from_user(child,
1042 task_user_regset_view(current),
1044 0, sizeof(struct user_i387_struct),
1047 #ifdef CONFIG_X86_32
1048 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1049 return copy_regset_to_user(child, &user_x86_32_view,
1051 0, sizeof(struct user_fxsr_struct),
1054 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1055 return copy_regset_from_user(child, &user_x86_32_view,
1057 0, sizeof(struct user_fxsr_struct),
1061 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1062 case PTRACE_GET_THREAD_AREA:
1065 ret = do_get_thread_area(child, addr,
1066 (struct user_desc __user *) data);
1069 case PTRACE_SET_THREAD_AREA:
1072 ret = do_set_thread_area(child, addr,
1073 (struct user_desc __user *) data, 0);
1077 #ifdef CONFIG_X86_64
1078 /* normal 64bit interface to access TLS data.
1079 Works just like arch_prctl, except that the arguments
1081 case PTRACE_ARCH_PRCTL:
1082 ret = do_arch_prctl(child, data, addr);
1087 * These bits need more cooking - not enabled yet:
1089 #ifdef CONFIG_X86_PTRACE_BTS
1090 case PTRACE_BTS_CONFIG:
1091 ret = ptrace_bts_config
1092 (child, data, (struct ptrace_bts_config __user *)addr);
1095 case PTRACE_BTS_STATUS:
1096 ret = ptrace_bts_status
1097 (child, data, (struct ptrace_bts_config __user *)addr);
1100 case PTRACE_BTS_SIZE:
1101 ret = ptrace_bts_size(child);
1104 case PTRACE_BTS_GET:
1105 ret = ptrace_bts_read_record
1106 (child, data, (struct bts_struct __user *) addr);
1109 case PTRACE_BTS_CLEAR:
1110 ret = ptrace_bts_clear(child);
1113 case PTRACE_BTS_DRAIN:
1114 ret = ptrace_bts_drain
1115 (child, data, (struct bts_struct __user *) addr);
1117 #endif /* CONFIG_X86_PTRACE_BTS */
1120 ret = ptrace_request(child, request, addr, data);
1127 #ifdef CONFIG_IA32_EMULATION
1129 #include <linux/compat.h>
1130 #include <linux/syscalls.h>
1131 #include <asm/ia32.h>
1132 #include <asm/user32.h>
1135 case offsetof(struct user32, regs.l): \
1136 regs->q = value; break
1139 case offsetof(struct user32, regs.rs): \
1140 return set_segment_reg(child, \
1141 offsetof(struct user_regs_struct, rs), \
1145 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1147 struct pt_regs *regs = task_pt_regs(child);
1168 case offsetof(struct user32, regs.orig_eax):
1170 * A 32-bit debugger setting orig_eax means to restore
1171 * the state of the task restarting a 32-bit syscall.
1172 * Make sure we interpret the -ERESTART* codes correctly
1173 * in case the task is not actually still sitting at the
1174 * exit from a 32-bit syscall with TS_COMPAT still set.
1176 regs->orig_ax = value;
1177 if (syscall_get_nr(child, regs) >= 0)
1178 task_thread_info(child)->status |= TS_COMPAT;
1181 case offsetof(struct user32, regs.eflags):
1182 return set_flags(child, value);
1184 case offsetof(struct user32, u_debugreg[0]) ...
1185 offsetof(struct user32, u_debugreg[7]):
1186 regno -= offsetof(struct user32, u_debugreg[0]);
1187 return ptrace_set_debugreg(child, regno / 4, value);
1190 if (regno > sizeof(struct user32) || (regno & 3))
1194 * Other dummy fields in the virtual user structure
1206 case offsetof(struct user32, regs.l): \
1207 *val = regs->q; break
1210 case offsetof(struct user32, regs.rs): \
1211 *val = get_segment_reg(child, \
1212 offsetof(struct user_regs_struct, rs)); \
1215 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1217 struct pt_regs *regs = task_pt_regs(child);
1235 R32(orig_eax, orig_ax);
1239 case offsetof(struct user32, regs.eflags):
1240 *val = get_flags(child);
1243 case offsetof(struct user32, u_debugreg[0]) ...
1244 offsetof(struct user32, u_debugreg[7]):
1245 regno -= offsetof(struct user32, u_debugreg[0]);
1246 *val = ptrace_get_debugreg(child, regno / 4);
1250 if (regno > sizeof(struct user32) || (regno & 3))
1254 * Other dummy fields in the virtual user structure
1266 static int genregs32_get(struct task_struct *target,
1267 const struct user_regset *regset,
1268 unsigned int pos, unsigned int count,
1269 void *kbuf, void __user *ubuf)
1272 compat_ulong_t *k = kbuf;
1274 getreg32(target, pos, k++);
1275 count -= sizeof(*k);
1279 compat_ulong_t __user *u = ubuf;
1281 compat_ulong_t word;
1282 getreg32(target, pos, &word);
1283 if (__put_user(word, u++))
1285 count -= sizeof(*u);
1293 static int genregs32_set(struct task_struct *target,
1294 const struct user_regset *regset,
1295 unsigned int pos, unsigned int count,
1296 const void *kbuf, const void __user *ubuf)
1300 const compat_ulong_t *k = kbuf;
1301 while (count > 0 && !ret) {
1302 ret = putreg32(target, pos, *k++);
1303 count -= sizeof(*k);
1307 const compat_ulong_t __user *u = ubuf;
1308 while (count > 0 && !ret) {
1309 compat_ulong_t word;
1310 ret = __get_user(word, u++);
1313 ret = putreg32(target, pos, word);
1314 count -= sizeof(*u);
1321 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1322 compat_ulong_t caddr, compat_ulong_t cdata)
1324 unsigned long addr = caddr;
1325 unsigned long data = cdata;
1326 void __user *datap = compat_ptr(data);
1331 case PTRACE_PEEKUSR:
1332 ret = getreg32(child, addr, &val);
1334 ret = put_user(val, (__u32 __user *)datap);
1337 case PTRACE_POKEUSR:
1338 ret = putreg32(child, addr, data);
1341 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1342 return copy_regset_to_user(child, &user_x86_32_view,
1344 0, sizeof(struct user_regs_struct32),
1347 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1348 return copy_regset_from_user(child, &user_x86_32_view,
1350 sizeof(struct user_regs_struct32),
1353 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1354 return copy_regset_to_user(child, &user_x86_32_view,
1356 sizeof(struct user_i387_ia32_struct),
1359 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1360 return copy_regset_from_user(
1361 child, &user_x86_32_view, REGSET_FP,
1362 0, sizeof(struct user_i387_ia32_struct), datap);
1364 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1365 return copy_regset_to_user(child, &user_x86_32_view,
1367 sizeof(struct user32_fxsr_struct),
1370 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1371 return copy_regset_from_user(child, &user_x86_32_view,
1373 sizeof(struct user32_fxsr_struct),
1376 case PTRACE_GET_THREAD_AREA:
1377 case PTRACE_SET_THREAD_AREA:
1378 #ifdef CONFIG_X86_PTRACE_BTS
1379 case PTRACE_BTS_CONFIG:
1380 case PTRACE_BTS_STATUS:
1381 case PTRACE_BTS_SIZE:
1382 case PTRACE_BTS_GET:
1383 case PTRACE_BTS_CLEAR:
1384 case PTRACE_BTS_DRAIN:
1385 #endif /* CONFIG_X86_PTRACE_BTS */
1386 return arch_ptrace(child, request, addr, data);
1389 return compat_ptrace_request(child, request, addr, data);
1395 #endif /* CONFIG_IA32_EMULATION */
1397 #ifdef CONFIG_X86_64
1399 static const struct user_regset x86_64_regsets[] = {
1400 [REGSET_GENERAL] = {
1401 .core_note_type = NT_PRSTATUS,
1402 .n = sizeof(struct user_regs_struct) / sizeof(long),
1403 .size = sizeof(long), .align = sizeof(long),
1404 .get = genregs_get, .set = genregs_set
1407 .core_note_type = NT_PRFPREG,
1408 .n = sizeof(struct user_i387_struct) / sizeof(long),
1409 .size = sizeof(long), .align = sizeof(long),
1410 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1412 [REGSET_IOPERM64] = {
1413 .core_note_type = NT_386_IOPERM,
1414 .n = IO_BITMAP_LONGS,
1415 .size = sizeof(long), .align = sizeof(long),
1416 .active = ioperm_active, .get = ioperm_get
1420 static const struct user_regset_view user_x86_64_view = {
1421 .name = "x86_64", .e_machine = EM_X86_64,
1422 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1425 #else /* CONFIG_X86_32 */
1427 #define user_regs_struct32 user_regs_struct
1428 #define genregs32_get genregs_get
1429 #define genregs32_set genregs_set
1431 #define user_i387_ia32_struct user_i387_struct
1432 #define user32_fxsr_struct user_fxsr_struct
1434 #endif /* CONFIG_X86_64 */
1436 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1437 static const struct user_regset x86_32_regsets[] = {
1438 [REGSET_GENERAL] = {
1439 .core_note_type = NT_PRSTATUS,
1440 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1441 .size = sizeof(u32), .align = sizeof(u32),
1442 .get = genregs32_get, .set = genregs32_set
1445 .core_note_type = NT_PRFPREG,
1446 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1447 .size = sizeof(u32), .align = sizeof(u32),
1448 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1451 .core_note_type = NT_PRXFPREG,
1452 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1453 .size = sizeof(u32), .align = sizeof(u32),
1454 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1457 .core_note_type = NT_386_TLS,
1458 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1459 .size = sizeof(struct user_desc),
1460 .align = sizeof(struct user_desc),
1461 .active = regset_tls_active,
1462 .get = regset_tls_get, .set = regset_tls_set
1464 [REGSET_IOPERM32] = {
1465 .core_note_type = NT_386_IOPERM,
1466 .n = IO_BITMAP_BYTES / sizeof(u32),
1467 .size = sizeof(u32), .align = sizeof(u32),
1468 .active = ioperm_active, .get = ioperm_get
1472 static const struct user_regset_view user_x86_32_view = {
1473 .name = "i386", .e_machine = EM_386,
1474 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1478 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1480 #ifdef CONFIG_IA32_EMULATION
1481 if (test_tsk_thread_flag(task, TIF_IA32))
1483 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1484 return &user_x86_32_view;
1486 #ifdef CONFIG_X86_64
1487 return &user_x86_64_view;
1491 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1492 int error_code, int si_code)
1494 struct siginfo info;
1496 tsk->thread.trap_no = 1;
1497 tsk->thread.error_code = error_code;
1499 memset(&info, 0, sizeof(info));
1500 info.si_signo = SIGTRAP;
1501 info.si_code = si_code;
1504 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1506 /* Send us the fake SIGTRAP */
1507 force_sig_info(SIGTRAP, &info, tsk);
1511 #ifdef CONFIG_X86_32
1513 #elif defined CONFIG_IA32_EMULATION
1514 # define IS_IA32 is_compat_task()
1520 * We must return the syscall number to actually look up in the table.
1521 * This can be -1L to skip running any syscall at all.
1523 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1528 * If we stepped into a sysenter/syscall insn, it trapped in
1529 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1530 * If user-mode had set TF itself, then it's still clear from
1531 * do_debug() and we need to set it again to restore the user
1532 * state. If we entered on the slow path, TF was already set.
1534 if (test_thread_flag(TIF_SINGLESTEP))
1535 regs->flags |= X86_EFLAGS_TF;
1537 /* do the secure computing check first */
1538 secure_computing(regs->orig_ax);
1540 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1543 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1544 tracehook_report_syscall_entry(regs))
1547 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1548 trace_sys_enter(regs, regs->orig_ax);
1550 if (unlikely(current->audit_context)) {
1552 audit_syscall_entry(AUDIT_ARCH_I386,
1555 regs->dx, regs->si);
1556 #ifdef CONFIG_X86_64
1558 audit_syscall_entry(AUDIT_ARCH_X86_64,
1561 regs->dx, regs->r10);
1565 return ret ?: regs->orig_ax;
1568 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1570 if (unlikely(current->audit_context))
1571 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1573 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1574 trace_sys_exit(regs, regs->ax);
1576 if (test_thread_flag(TIF_SYSCALL_TRACE))
1577 tracehook_report_syscall_exit(regs, 0);
1580 * If TIF_SYSCALL_EMU is set, we only get here because of
1581 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1582 * We already reported this syscall instruction in
1583 * syscall_trace_enter(), so don't do any more now.
1585 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1589 * If we are single-stepping, synthesize a trap to follow the
1590 * system call instruction.
1592 if (test_thread_flag(TIF_SINGLESTEP) &&
1593 tracehook_consider_fatal_signal(current, SIGTRAP))
1594 send_sigtrap(current, regs, 0, TRAP_BRKPT);