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,
55 struct pt_regs_offset {
60 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61 #define REG_OFFSET_END {.name = NULL, .offset = 0}
63 static const struct pt_regs_offset regoffset_table[] = {
87 REG_OFFSET_NAME(orig_ax),
90 REG_OFFSET_NAME(flags),
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
103 int regs_query_register_offset(const char *name)
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
119 const char *regs_query_register_name(unsigned int offset)
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
128 static const int arg_offs_table[] = {
130 [0] = offsetof(struct pt_regs, ax),
131 [1] = offsetof(struct pt_regs, dx),
132 [2] = offsetof(struct pt_regs, cx)
133 #else /* CONFIG_X86_64 */
134 [0] = offsetof(struct pt_regs, di),
135 [1] = offsetof(struct pt_regs, si),
136 [2] = offsetof(struct pt_regs, dx),
137 [3] = offsetof(struct pt_regs, cx),
138 [4] = offsetof(struct pt_regs, r8),
139 [5] = offsetof(struct pt_regs, r9)
144 * regs_get_argument_nth() - get Nth argument at function call
145 * @regs: pt_regs which contains registers at function entry.
146 * @n: argument number.
148 * regs_get_argument_nth() returns @n th argument of a function call.
149 * Since usually the kernel stack will be changed right after function entry,
150 * you must use this at function entry. If the @n th entry is NOT in the
151 * kernel stack or pt_regs, this returns 0.
153 unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
155 if (n < ARRAY_SIZE(arg_offs_table))
156 return *(unsigned long *)((char *)regs + arg_offs_table[n]);
159 * The typical case: arg n is on the stack.
160 * (Note: stack[0] = return address, so skip it)
162 n -= ARRAY_SIZE(arg_offs_table);
163 return regs_get_kernel_stack_nth(regs, 1 + n);
168 * does not yet catch signals sent when the child dies.
169 * in exit.c or in signal.c.
173 * Determines which flags the user has access to [1 = access, 0 = no access].
175 #define FLAG_MASK_32 ((unsigned long) \
176 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
177 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
178 X86_EFLAGS_SF | X86_EFLAGS_TF | \
179 X86_EFLAGS_DF | X86_EFLAGS_OF | \
180 X86_EFLAGS_RF | X86_EFLAGS_AC))
183 * Determines whether a value may be installed in a segment register.
185 static inline bool invalid_selector(u16 value)
187 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
192 #define FLAG_MASK FLAG_MASK_32
194 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
196 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
197 return ®s->bx + (regno >> 2);
200 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
203 * Returning the value truncates it to 16 bits.
206 if (offset != offsetof(struct user_regs_struct, gs))
207 retval = *pt_regs_access(task_pt_regs(task), offset);
210 retval = get_user_gs(task_pt_regs(task));
212 retval = task_user_gs(task);
217 static int set_segment_reg(struct task_struct *task,
218 unsigned long offset, u16 value)
221 * The value argument was already truncated to 16 bits.
223 if (invalid_selector(value))
227 * For %cs and %ss we cannot permit a null selector.
228 * We can permit a bogus selector as long as it has USER_RPL.
229 * Null selectors are fine for other segment registers, but
230 * we will never get back to user mode with invalid %cs or %ss
231 * and will take the trap in iret instead. Much code relies
232 * on user_mode() to distinguish a user trap frame (which can
233 * safely use invalid selectors) from a kernel trap frame.
236 case offsetof(struct user_regs_struct, cs):
237 case offsetof(struct user_regs_struct, ss):
238 if (unlikely(value == 0))
242 *pt_regs_access(task_pt_regs(task), offset) = value;
245 case offsetof(struct user_regs_struct, gs):
247 set_user_gs(task_pt_regs(task), value);
249 task_user_gs(task) = value;
255 #else /* CONFIG_X86_64 */
257 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
259 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
261 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
262 return ®s->r15 + (offset / sizeof(regs->r15));
265 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
268 * Returning the value truncates it to 16 bits.
273 case offsetof(struct user_regs_struct, fs):
274 if (task == current) {
275 /* Older gas can't assemble movq %?s,%r?? */
276 asm("movl %%fs,%0" : "=r" (seg));
279 return task->thread.fsindex;
280 case offsetof(struct user_regs_struct, gs):
281 if (task == current) {
282 asm("movl %%gs,%0" : "=r" (seg));
285 return task->thread.gsindex;
286 case offsetof(struct user_regs_struct, ds):
287 if (task == current) {
288 asm("movl %%ds,%0" : "=r" (seg));
291 return task->thread.ds;
292 case offsetof(struct user_regs_struct, es):
293 if (task == current) {
294 asm("movl %%es,%0" : "=r" (seg));
297 return task->thread.es;
299 case offsetof(struct user_regs_struct, cs):
300 case offsetof(struct user_regs_struct, ss):
303 return *pt_regs_access(task_pt_regs(task), offset);
306 static int set_segment_reg(struct task_struct *task,
307 unsigned long offset, u16 value)
310 * The value argument was already truncated to 16 bits.
312 if (invalid_selector(value))
316 case offsetof(struct user_regs_struct,fs):
318 * If this is setting fs as for normal 64-bit use but
319 * setting fs_base has implicitly changed it, leave it.
321 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
322 task->thread.fs != 0) ||
323 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
324 task->thread.fs == 0))
326 task->thread.fsindex = value;
328 loadsegment(fs, task->thread.fsindex);
330 case offsetof(struct user_regs_struct,gs):
332 * If this is setting gs as for normal 64-bit use but
333 * setting gs_base has implicitly changed it, leave it.
335 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
336 task->thread.gs != 0) ||
337 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
338 task->thread.gs == 0))
340 task->thread.gsindex = value;
342 load_gs_index(task->thread.gsindex);
344 case offsetof(struct user_regs_struct,ds):
345 task->thread.ds = value;
347 loadsegment(ds, task->thread.ds);
349 case offsetof(struct user_regs_struct,es):
350 task->thread.es = value;
352 loadsegment(es, task->thread.es);
356 * Can't actually change these in 64-bit mode.
358 case offsetof(struct user_regs_struct,cs):
359 if (unlikely(value == 0))
361 #ifdef CONFIG_IA32_EMULATION
362 if (test_tsk_thread_flag(task, TIF_IA32))
363 task_pt_regs(task)->cs = value;
366 case offsetof(struct user_regs_struct,ss):
367 if (unlikely(value == 0))
369 #ifdef CONFIG_IA32_EMULATION
370 if (test_tsk_thread_flag(task, TIF_IA32))
371 task_pt_regs(task)->ss = value;
379 #endif /* CONFIG_X86_32 */
381 static unsigned long get_flags(struct task_struct *task)
383 unsigned long retval = task_pt_regs(task)->flags;
386 * If the debugger set TF, hide it from the readout.
388 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
389 retval &= ~X86_EFLAGS_TF;
394 static int set_flags(struct task_struct *task, unsigned long value)
396 struct pt_regs *regs = task_pt_regs(task);
399 * If the user value contains TF, mark that
400 * it was not "us" (the debugger) that set it.
401 * If not, make sure it stays set if we had.
403 if (value & X86_EFLAGS_TF)
404 clear_tsk_thread_flag(task, TIF_FORCED_TF);
405 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
406 value |= X86_EFLAGS_TF;
408 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
413 static int putreg(struct task_struct *child,
414 unsigned long offset, unsigned long value)
417 case offsetof(struct user_regs_struct, cs):
418 case offsetof(struct user_regs_struct, ds):
419 case offsetof(struct user_regs_struct, es):
420 case offsetof(struct user_regs_struct, fs):
421 case offsetof(struct user_regs_struct, gs):
422 case offsetof(struct user_regs_struct, ss):
423 return set_segment_reg(child, offset, value);
425 case offsetof(struct user_regs_struct, flags):
426 return set_flags(child, value);
429 case offsetof(struct user_regs_struct,fs_base):
430 if (value >= TASK_SIZE_OF(child))
433 * When changing the segment base, use do_arch_prctl
434 * to set either thread.fs or thread.fsindex and the
435 * corresponding GDT slot.
437 if (child->thread.fs != value)
438 return do_arch_prctl(child, ARCH_SET_FS, value);
440 case offsetof(struct user_regs_struct,gs_base):
442 * Exactly the same here as the %fs handling above.
444 if (value >= TASK_SIZE_OF(child))
446 if (child->thread.gs != value)
447 return do_arch_prctl(child, ARCH_SET_GS, value);
452 *pt_regs_access(task_pt_regs(child), offset) = value;
456 static unsigned long getreg(struct task_struct *task, unsigned long offset)
459 case offsetof(struct user_regs_struct, cs):
460 case offsetof(struct user_regs_struct, ds):
461 case offsetof(struct user_regs_struct, es):
462 case offsetof(struct user_regs_struct, fs):
463 case offsetof(struct user_regs_struct, gs):
464 case offsetof(struct user_regs_struct, ss):
465 return get_segment_reg(task, offset);
467 case offsetof(struct user_regs_struct, flags):
468 return get_flags(task);
471 case offsetof(struct user_regs_struct, fs_base): {
473 * do_arch_prctl may have used a GDT slot instead of
474 * the MSR. To userland, it appears the same either
475 * way, except the %fs segment selector might not be 0.
477 unsigned int seg = task->thread.fsindex;
478 if (task->thread.fs != 0)
479 return task->thread.fs;
481 asm("movl %%fs,%0" : "=r" (seg));
482 if (seg != FS_TLS_SEL)
484 return get_desc_base(&task->thread.tls_array[FS_TLS]);
486 case offsetof(struct user_regs_struct, gs_base): {
488 * Exactly the same here as the %fs handling above.
490 unsigned int seg = task->thread.gsindex;
491 if (task->thread.gs != 0)
492 return task->thread.gs;
494 asm("movl %%gs,%0" : "=r" (seg));
495 if (seg != GS_TLS_SEL)
497 return get_desc_base(&task->thread.tls_array[GS_TLS]);
502 return *pt_regs_access(task_pt_regs(task), offset);
505 static int genregs_get(struct task_struct *target,
506 const struct user_regset *regset,
507 unsigned int pos, unsigned int count,
508 void *kbuf, void __user *ubuf)
511 unsigned long *k = kbuf;
513 *k++ = getreg(target, pos);
518 unsigned long __user *u = ubuf;
520 if (__put_user(getreg(target, pos), u++))
530 static int genregs_set(struct task_struct *target,
531 const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
537 const unsigned long *k = kbuf;
538 while (count > 0 && !ret) {
539 ret = putreg(target, pos, *k++);
544 const unsigned long __user *u = ubuf;
545 while (count > 0 && !ret) {
547 ret = __get_user(word, u++);
550 ret = putreg(target, pos, word);
558 static void ptrace_triggered(struct perf_event *bp, void *data)
561 struct thread_struct *thread = &(current->thread);
564 * Store in the virtual DR6 register the fact that the breakpoint
565 * was hit so the thread's debugger will see it.
567 for (i = 0; i < HBP_NUM; i++) {
568 if (thread->ptrace_bps[i] == bp)
572 thread->debugreg6 |= (DR_TRAP0 << i);
576 * Walk through every ptrace breakpoints for this thread and
577 * build the dr7 value on top of their attributes.
580 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
584 struct arch_hw_breakpoint *info;
586 for (i = 0; i < HBP_NUM; i++) {
587 if (bp[i] && !bp[i]->attr.disabled) {
588 info = counter_arch_bp(bp[i]);
589 dr7 |= encode_dr7(i, info->len, info->type);
597 * Handle ptrace writes to debug register 7.
599 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
601 struct thread_struct *thread = &(tsk->thread);
602 unsigned long old_dr7;
603 int i, orig_ret = 0, rc = 0;
604 int enabled, second_pass = 0;
606 int gen_len, gen_type;
607 struct perf_event *bp;
609 data &= ~DR_CONTROL_RESERVED;
610 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
613 * Loop through all the hardware breakpoints, making the
614 * appropriate changes to each.
616 for (i = 0; i < HBP_NUM; i++) {
617 enabled = decode_dr7(data, i, &len, &type);
618 bp = thread->ptrace_bps[i];
623 * Don't unregister the breakpoints right-away,
624 * unless all register_user_hw_breakpoint()
625 * requests have succeeded. This prevents
626 * any window of opportunity for debug
627 * register grabbing by other users.
631 thread->ptrace_bps[i] = NULL;
632 unregister_hw_breakpoint(bp);
638 * We shoud have at least an inactive breakpoint at this
639 * slot. It means the user is writing dr7 without having
640 * written the address register first
647 rc = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
652 * This is a temporary thing as bp is unregistered/registered
653 * to simulate modification
655 bp = modify_user_hw_breakpoint(bp, bp->attr.bp_addr, gen_len,
656 gen_type, bp->callback,
658 thread->ptrace_bps[i] = NULL;
660 /* Incorrect bp, or we have a bug in bp API */
666 thread->ptrace_bps[i] = bp;
669 * Make a second pass to free the remaining unused breakpoints
670 * or to restore the original breakpoints if an error occurred.
680 return ((orig_ret < 0) ? orig_ret : rc);
684 * Handle PTRACE_PEEKUSR calls for the debug register area.
686 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
688 struct thread_struct *thread = &(tsk->thread);
689 unsigned long val = 0;
692 struct perf_event *bp;
693 bp = thread->ptrace_bps[n];
696 val = bp->hw.info.address;
698 val = thread->debugreg6;
700 val = ptrace_get_dr7(thread->ptrace_bps);
705 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
708 struct perf_event *bp;
709 struct thread_struct *t = &tsk->thread;
711 if (!t->ptrace_bps[nr]) {
713 * Put stub len and type to register (reserve) an inactive but
716 bp = register_user_hw_breakpoint(addr, HW_BREAKPOINT_LEN_1,
718 ptrace_triggered, tsk,
721 bp = t->ptrace_bps[nr];
722 t->ptrace_bps[nr] = NULL;
723 bp = modify_user_hw_breakpoint(bp, addr, bp->attr.bp_len,
730 * CHECKME: the previous code returned -EIO if the addr wasn't a
731 * valid task virtual addr. The new one will return -EINVAL in this
733 * -EINVAL may be what we want for in-kernel breakpoints users, but
734 * -EIO looks better for ptrace, since we refuse a register writing
735 * for the user. And anyway this is the previous behaviour.
740 t->ptrace_bps[nr] = bp;
746 * Handle PTRACE_POKEUSR calls for the debug register area.
748 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
750 struct thread_struct *thread = &(tsk->thread);
753 /* There are no DR4 or DR5 registers */
754 if (n == 4 || n == 5)
758 thread->debugreg6 = val;
762 rc = ptrace_set_breakpoint_addr(tsk, n, val);
766 /* All that's left is DR7 */
768 rc = ptrace_write_dr7(tsk, val);
775 * These access the current or another (stopped) task's io permission
776 * bitmap for debugging or core dump.
778 static int ioperm_active(struct task_struct *target,
779 const struct user_regset *regset)
781 return target->thread.io_bitmap_max / regset->size;
784 static int ioperm_get(struct task_struct *target,
785 const struct user_regset *regset,
786 unsigned int pos, unsigned int count,
787 void *kbuf, void __user *ubuf)
789 if (!target->thread.io_bitmap_ptr)
792 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
793 target->thread.io_bitmap_ptr,
797 #ifdef CONFIG_X86_PTRACE_BTS
799 * A branch trace store context.
801 * Contexts may only be installed by ptrace_bts_config() and only for
804 * Contexts are destroyed when the tracee is detached from the tracer.
805 * The actual destruction work requires interrupts enabled, so the
806 * work is deferred and will be scheduled during __ptrace_unlink().
808 * Contexts hold an additional task_struct reference on the traced
809 * task, as well as a reference on the tracer's mm.
811 * Ptrace already holds a task_struct for the duration of ptrace operations,
812 * but since destruction is deferred, it may be executed after both
813 * tracer and tracee exited.
816 /* The branch trace handle. */
817 struct bts_tracer *tracer;
819 /* The buffer used to store the branch trace and its size. */
823 /* The mm that paid for the above buffer. */
824 struct mm_struct *mm;
826 /* The task this context belongs to. */
827 struct task_struct *task;
829 /* The signal to send on a bts buffer overflow. */
830 unsigned int bts_ovfl_signal;
832 /* The work struct to destroy a context. */
833 struct work_struct work;
836 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
841 err = account_locked_memory(current->mm, current->signal->rlim, size);
845 buffer = kzalloc(size, GFP_KERNEL);
849 context->buffer = buffer;
850 context->size = size;
851 context->mm = get_task_mm(current);
856 refund_locked_memory(current->mm, size);
860 static inline void free_bts_buffer(struct bts_context *context)
862 if (!context->buffer)
865 kfree(context->buffer);
866 context->buffer = NULL;
868 refund_locked_memory(context->mm, context->size);
875 static void free_bts_context_work(struct work_struct *w)
877 struct bts_context *context;
879 context = container_of(w, struct bts_context, work);
881 ds_release_bts(context->tracer);
882 put_task_struct(context->task);
883 free_bts_buffer(context);
887 static inline void free_bts_context(struct bts_context *context)
889 INIT_WORK(&context->work, free_bts_context_work);
890 schedule_work(&context->work);
893 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
895 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
897 context->task = task;
900 get_task_struct(task);
906 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
907 struct bts_struct __user *out)
909 struct bts_context *context;
910 const struct bts_trace *trace;
911 struct bts_struct bts;
912 const unsigned char *at;
915 context = child->bts;
919 trace = ds_read_bts(context->tracer);
923 at = trace->ds.top - ((index + 1) * trace->ds.size);
924 if ((void *)at < trace->ds.begin)
925 at += (trace->ds.n * trace->ds.size);
930 error = trace->read(context->tracer, at, &bts);
934 if (copy_to_user(out, &bts, sizeof(bts)))
940 static int ptrace_bts_drain(struct task_struct *child,
942 struct bts_struct __user *out)
944 struct bts_context *context;
945 const struct bts_trace *trace;
946 const unsigned char *at;
947 int error, drained = 0;
949 context = child->bts;
953 trace = ds_read_bts(context->tracer);
960 if (size < (trace->ds.top - trace->ds.begin))
963 for (at = trace->ds.begin; (void *)at < trace->ds.top;
964 out++, drained++, at += trace->ds.size) {
965 struct bts_struct bts;
967 error = trace->read(context->tracer, at, &bts);
971 if (copy_to_user(out, &bts, sizeof(bts)))
975 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
977 error = ds_reset_bts(context->tracer);
984 static int ptrace_bts_config(struct task_struct *child,
986 const struct ptrace_bts_config __user *ucfg)
988 struct bts_context *context;
989 struct ptrace_bts_config cfg;
990 unsigned int flags = 0;
992 if (cfg_size < sizeof(cfg))
995 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
998 context = child->bts;
1000 context = alloc_bts_context(child);
1004 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
1009 context->bts_ovfl_signal = cfg.signal;
1012 ds_release_bts(context->tracer);
1013 context->tracer = NULL;
1015 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1018 free_bts_buffer(context);
1022 err = alloc_bts_buffer(context, cfg.size);
1027 if (cfg.flags & PTRACE_BTS_O_TRACE)
1030 if (cfg.flags & PTRACE_BTS_O_SCHED)
1031 flags |= BTS_TIMESTAMPS;
1034 ds_request_bts_task(child, context->buffer, context->size,
1035 NULL, (size_t)-1, flags);
1036 if (unlikely(IS_ERR(context->tracer))) {
1037 int error = PTR_ERR(context->tracer);
1039 free_bts_buffer(context);
1040 context->tracer = NULL;
1047 static int ptrace_bts_status(struct task_struct *child,
1049 struct ptrace_bts_config __user *ucfg)
1051 struct bts_context *context;
1052 const struct bts_trace *trace;
1053 struct ptrace_bts_config cfg;
1055 context = child->bts;
1059 if (cfg_size < sizeof(cfg))
1062 trace = ds_read_bts(context->tracer);
1066 memset(&cfg, 0, sizeof(cfg));
1067 cfg.size = trace->ds.end - trace->ds.begin;
1068 cfg.signal = context->bts_ovfl_signal;
1069 cfg.bts_size = sizeof(struct bts_struct);
1072 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1074 if (trace->ds.flags & BTS_USER)
1075 cfg.flags |= PTRACE_BTS_O_TRACE;
1077 if (trace->ds.flags & BTS_TIMESTAMPS)
1078 cfg.flags |= PTRACE_BTS_O_SCHED;
1080 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1086 static int ptrace_bts_clear(struct task_struct *child)
1088 struct bts_context *context;
1089 const struct bts_trace *trace;
1091 context = child->bts;
1095 trace = ds_read_bts(context->tracer);
1099 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
1101 return ds_reset_bts(context->tracer);
1104 static int ptrace_bts_size(struct task_struct *child)
1106 struct bts_context *context;
1107 const struct bts_trace *trace;
1109 context = child->bts;
1113 trace = ds_read_bts(context->tracer);
1117 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1121 * Called from __ptrace_unlink() after the child has been moved back
1122 * to its original parent.
1124 void ptrace_bts_untrace(struct task_struct *child)
1126 if (unlikely(child->bts)) {
1127 free_bts_context(child->bts);
1131 #endif /* CONFIG_X86_PTRACE_BTS */
1134 * Called by kernel/ptrace.c when detaching..
1136 * Make sure the single step bit is not set.
1138 void ptrace_disable(struct task_struct *child)
1140 user_disable_single_step(child);
1141 #ifdef TIF_SYSCALL_EMU
1142 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1146 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1147 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1150 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1153 unsigned long __user *datap = (unsigned long __user *)data;
1156 /* read the word at location addr in the USER area. */
1157 case PTRACE_PEEKUSR: {
1161 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1162 addr >= sizeof(struct user))
1165 tmp = 0; /* Default return condition */
1166 if (addr < sizeof(struct user_regs_struct))
1167 tmp = getreg(child, addr);
1168 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1169 addr <= offsetof(struct user, u_debugreg[7])) {
1170 addr -= offsetof(struct user, u_debugreg[0]);
1171 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1173 ret = put_user(tmp, datap);
1177 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1179 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1180 addr >= sizeof(struct user))
1183 if (addr < sizeof(struct user_regs_struct))
1184 ret = putreg(child, addr, data);
1185 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1186 addr <= offsetof(struct user, u_debugreg[7])) {
1187 addr -= offsetof(struct user, u_debugreg[0]);
1188 ret = ptrace_set_debugreg(child,
1189 addr / sizeof(data), data);
1193 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1194 return copy_regset_to_user(child,
1195 task_user_regset_view(current),
1197 0, sizeof(struct user_regs_struct),
1200 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1201 return copy_regset_from_user(child,
1202 task_user_regset_view(current),
1204 0, sizeof(struct user_regs_struct),
1207 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1208 return copy_regset_to_user(child,
1209 task_user_regset_view(current),
1211 0, sizeof(struct user_i387_struct),
1214 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1215 return copy_regset_from_user(child,
1216 task_user_regset_view(current),
1218 0, sizeof(struct user_i387_struct),
1221 #ifdef CONFIG_X86_32
1222 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1223 return copy_regset_to_user(child, &user_x86_32_view,
1225 0, sizeof(struct user_fxsr_struct),
1228 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1229 return copy_regset_from_user(child, &user_x86_32_view,
1231 0, sizeof(struct user_fxsr_struct),
1235 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1236 case PTRACE_GET_THREAD_AREA:
1239 ret = do_get_thread_area(child, addr,
1240 (struct user_desc __user *) data);
1243 case PTRACE_SET_THREAD_AREA:
1246 ret = do_set_thread_area(child, addr,
1247 (struct user_desc __user *) data, 0);
1251 #ifdef CONFIG_X86_64
1252 /* normal 64bit interface to access TLS data.
1253 Works just like arch_prctl, except that the arguments
1255 case PTRACE_ARCH_PRCTL:
1256 ret = do_arch_prctl(child, data, addr);
1261 * These bits need more cooking - not enabled yet:
1263 #ifdef CONFIG_X86_PTRACE_BTS
1264 case PTRACE_BTS_CONFIG:
1265 ret = ptrace_bts_config
1266 (child, data, (struct ptrace_bts_config __user *)addr);
1269 case PTRACE_BTS_STATUS:
1270 ret = ptrace_bts_status
1271 (child, data, (struct ptrace_bts_config __user *)addr);
1274 case PTRACE_BTS_SIZE:
1275 ret = ptrace_bts_size(child);
1278 case PTRACE_BTS_GET:
1279 ret = ptrace_bts_read_record
1280 (child, data, (struct bts_struct __user *) addr);
1283 case PTRACE_BTS_CLEAR:
1284 ret = ptrace_bts_clear(child);
1287 case PTRACE_BTS_DRAIN:
1288 ret = ptrace_bts_drain
1289 (child, data, (struct bts_struct __user *) addr);
1291 #endif /* CONFIG_X86_PTRACE_BTS */
1294 ret = ptrace_request(child, request, addr, data);
1301 #ifdef CONFIG_IA32_EMULATION
1303 #include <linux/compat.h>
1304 #include <linux/syscalls.h>
1305 #include <asm/ia32.h>
1306 #include <asm/user32.h>
1309 case offsetof(struct user32, regs.l): \
1310 regs->q = value; break
1313 case offsetof(struct user32, regs.rs): \
1314 return set_segment_reg(child, \
1315 offsetof(struct user_regs_struct, rs), \
1319 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1321 struct pt_regs *regs = task_pt_regs(child);
1342 case offsetof(struct user32, regs.orig_eax):
1344 * A 32-bit debugger setting orig_eax means to restore
1345 * the state of the task restarting a 32-bit syscall.
1346 * Make sure we interpret the -ERESTART* codes correctly
1347 * in case the task is not actually still sitting at the
1348 * exit from a 32-bit syscall with TS_COMPAT still set.
1350 regs->orig_ax = value;
1351 if (syscall_get_nr(child, regs) >= 0)
1352 task_thread_info(child)->status |= TS_COMPAT;
1355 case offsetof(struct user32, regs.eflags):
1356 return set_flags(child, value);
1358 case offsetof(struct user32, u_debugreg[0]) ...
1359 offsetof(struct user32, u_debugreg[7]):
1360 regno -= offsetof(struct user32, u_debugreg[0]);
1361 return ptrace_set_debugreg(child, regno / 4, value);
1364 if (regno > sizeof(struct user32) || (regno & 3))
1368 * Other dummy fields in the virtual user structure
1380 case offsetof(struct user32, regs.l): \
1381 *val = regs->q; break
1384 case offsetof(struct user32, regs.rs): \
1385 *val = get_segment_reg(child, \
1386 offsetof(struct user_regs_struct, rs)); \
1389 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1391 struct pt_regs *regs = task_pt_regs(child);
1409 R32(orig_eax, orig_ax);
1413 case offsetof(struct user32, regs.eflags):
1414 *val = get_flags(child);
1417 case offsetof(struct user32, u_debugreg[0]) ...
1418 offsetof(struct user32, u_debugreg[7]):
1419 regno -= offsetof(struct user32, u_debugreg[0]);
1420 *val = ptrace_get_debugreg(child, regno / 4);
1424 if (regno > sizeof(struct user32) || (regno & 3))
1428 * Other dummy fields in the virtual user structure
1440 static int genregs32_get(struct task_struct *target,
1441 const struct user_regset *regset,
1442 unsigned int pos, unsigned int count,
1443 void *kbuf, void __user *ubuf)
1446 compat_ulong_t *k = kbuf;
1448 getreg32(target, pos, k++);
1449 count -= sizeof(*k);
1453 compat_ulong_t __user *u = ubuf;
1455 compat_ulong_t word;
1456 getreg32(target, pos, &word);
1457 if (__put_user(word, u++))
1459 count -= sizeof(*u);
1467 static int genregs32_set(struct task_struct *target,
1468 const struct user_regset *regset,
1469 unsigned int pos, unsigned int count,
1470 const void *kbuf, const void __user *ubuf)
1474 const compat_ulong_t *k = kbuf;
1475 while (count > 0 && !ret) {
1476 ret = putreg32(target, pos, *k++);
1477 count -= sizeof(*k);
1481 const compat_ulong_t __user *u = ubuf;
1482 while (count > 0 && !ret) {
1483 compat_ulong_t word;
1484 ret = __get_user(word, u++);
1487 ret = putreg32(target, pos, word);
1488 count -= sizeof(*u);
1495 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1496 compat_ulong_t caddr, compat_ulong_t cdata)
1498 unsigned long addr = caddr;
1499 unsigned long data = cdata;
1500 void __user *datap = compat_ptr(data);
1505 case PTRACE_PEEKUSR:
1506 ret = getreg32(child, addr, &val);
1508 ret = put_user(val, (__u32 __user *)datap);
1511 case PTRACE_POKEUSR:
1512 ret = putreg32(child, addr, data);
1515 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1516 return copy_regset_to_user(child, &user_x86_32_view,
1518 0, sizeof(struct user_regs_struct32),
1521 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1522 return copy_regset_from_user(child, &user_x86_32_view,
1524 sizeof(struct user_regs_struct32),
1527 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1528 return copy_regset_to_user(child, &user_x86_32_view,
1530 sizeof(struct user_i387_ia32_struct),
1533 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1534 return copy_regset_from_user(
1535 child, &user_x86_32_view, REGSET_FP,
1536 0, sizeof(struct user_i387_ia32_struct), datap);
1538 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1539 return copy_regset_to_user(child, &user_x86_32_view,
1541 sizeof(struct user32_fxsr_struct),
1544 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1545 return copy_regset_from_user(child, &user_x86_32_view,
1547 sizeof(struct user32_fxsr_struct),
1550 case PTRACE_GET_THREAD_AREA:
1551 case PTRACE_SET_THREAD_AREA:
1552 #ifdef CONFIG_X86_PTRACE_BTS
1553 case PTRACE_BTS_CONFIG:
1554 case PTRACE_BTS_STATUS:
1555 case PTRACE_BTS_SIZE:
1556 case PTRACE_BTS_GET:
1557 case PTRACE_BTS_CLEAR:
1558 case PTRACE_BTS_DRAIN:
1559 #endif /* CONFIG_X86_PTRACE_BTS */
1560 return arch_ptrace(child, request, addr, data);
1563 return compat_ptrace_request(child, request, addr, data);
1569 #endif /* CONFIG_IA32_EMULATION */
1571 #ifdef CONFIG_X86_64
1573 static const struct user_regset x86_64_regsets[] = {
1574 [REGSET_GENERAL] = {
1575 .core_note_type = NT_PRSTATUS,
1576 .n = sizeof(struct user_regs_struct) / sizeof(long),
1577 .size = sizeof(long), .align = sizeof(long),
1578 .get = genregs_get, .set = genregs_set
1581 .core_note_type = NT_PRFPREG,
1582 .n = sizeof(struct user_i387_struct) / sizeof(long),
1583 .size = sizeof(long), .align = sizeof(long),
1584 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_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 const struct user_regset x86_32_regsets[] = {
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_386_TLS,
1632 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1633 .size = sizeof(struct user_desc),
1634 .align = sizeof(struct user_desc),
1635 .active = regset_tls_active,
1636 .get = regset_tls_get, .set = regset_tls_set
1638 [REGSET_IOPERM32] = {
1639 .core_note_type = NT_386_IOPERM,
1640 .n = IO_BITMAP_BYTES / sizeof(u32),
1641 .size = sizeof(u32), .align = sizeof(u32),
1642 .active = ioperm_active, .get = ioperm_get
1646 static const struct user_regset_view user_x86_32_view = {
1647 .name = "i386", .e_machine = EM_386,
1648 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1652 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1654 #ifdef CONFIG_IA32_EMULATION
1655 if (test_tsk_thread_flag(task, TIF_IA32))
1657 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1658 return &user_x86_32_view;
1660 #ifdef CONFIG_X86_64
1661 return &user_x86_64_view;
1665 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1666 int error_code, int si_code)
1668 struct siginfo info;
1670 tsk->thread.trap_no = 1;
1671 tsk->thread.error_code = error_code;
1673 memset(&info, 0, sizeof(info));
1674 info.si_signo = SIGTRAP;
1675 info.si_code = si_code;
1678 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1680 /* Send us the fake SIGTRAP */
1681 force_sig_info(SIGTRAP, &info, tsk);
1685 #ifdef CONFIG_X86_32
1687 #elif defined CONFIG_IA32_EMULATION
1688 # define IS_IA32 is_compat_task()
1694 * We must return the syscall number to actually look up in the table.
1695 * This can be -1L to skip running any syscall at all.
1697 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1702 * If we stepped into a sysenter/syscall insn, it trapped in
1703 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1704 * If user-mode had set TF itself, then it's still clear from
1705 * do_debug() and we need to set it again to restore the user
1706 * state. If we entered on the slow path, TF was already set.
1708 if (test_thread_flag(TIF_SINGLESTEP))
1709 regs->flags |= X86_EFLAGS_TF;
1711 /* do the secure computing check first */
1712 secure_computing(regs->orig_ax);
1714 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1717 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1718 tracehook_report_syscall_entry(regs))
1721 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1722 trace_sys_enter(regs, regs->orig_ax);
1724 if (unlikely(current->audit_context)) {
1726 audit_syscall_entry(AUDIT_ARCH_I386,
1729 regs->dx, regs->si);
1730 #ifdef CONFIG_X86_64
1732 audit_syscall_entry(AUDIT_ARCH_X86_64,
1735 regs->dx, regs->r10);
1739 return ret ?: regs->orig_ax;
1742 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1744 if (unlikely(current->audit_context))
1745 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1747 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1748 trace_sys_exit(regs, regs->ax);
1750 if (test_thread_flag(TIF_SYSCALL_TRACE))
1751 tracehook_report_syscall_exit(regs, 0);
1754 * If TIF_SYSCALL_EMU is set, we only get here because of
1755 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1756 * We already reported this syscall instruction in
1757 * syscall_trace_enter(), so don't do any more now.
1759 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1763 * If we are single-stepping, synthesize a trap to follow the
1764 * system call instruction.
1766 if (test_thread_flag(TIF_SINGLESTEP) &&
1767 tracehook_consider_fatal_signal(current, SIGTRAP))
1768 send_sigtrap(current, regs, 0, TRAP_BRKPT);