1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/signal.h>
24 #include <linux/workqueue.h>
25 #include <linux/perf_event.h>
26 #include <linux/hw_breakpoint.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
33 #include <asm/debugreg.h>
36 #include <asm/prctl.h>
37 #include <asm/proto.h>
39 #include <asm/hw_breakpoint.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
50 REGSET_IOPERM64
= REGSET_XFP
,
56 * does not yet catch signals sent when the child dies.
57 * in exit.c or in signal.c.
61 * Determines which flags the user has access to [1 = access, 0 = no access].
63 #define FLAG_MASK_32 ((unsigned long) \
64 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
65 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
66 X86_EFLAGS_SF | X86_EFLAGS_TF | \
67 X86_EFLAGS_DF | X86_EFLAGS_OF | \
68 X86_EFLAGS_RF | X86_EFLAGS_AC))
71 * Determines whether a value may be installed in a segment register.
73 static inline bool invalid_selector(u16 value
)
75 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
80 #define FLAG_MASK FLAG_MASK_32
82 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
84 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
85 return ®s
->bx
+ (regno
>> 2);
88 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
91 * Returning the value truncates it to 16 bits.
94 if (offset
!= offsetof(struct user_regs_struct
, gs
))
95 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
98 retval
= get_user_gs(task_pt_regs(task
));
100 retval
= task_user_gs(task
);
105 static int set_segment_reg(struct task_struct
*task
,
106 unsigned long offset
, u16 value
)
109 * The value argument was already truncated to 16 bits.
111 if (invalid_selector(value
))
115 * For %cs and %ss we cannot permit a null selector.
116 * We can permit a bogus selector as long as it has USER_RPL.
117 * Null selectors are fine for other segment registers, but
118 * we will never get back to user mode with invalid %cs or %ss
119 * and will take the trap in iret instead. Much code relies
120 * on user_mode() to distinguish a user trap frame (which can
121 * safely use invalid selectors) from a kernel trap frame.
124 case offsetof(struct user_regs_struct
, cs
):
125 case offsetof(struct user_regs_struct
, ss
):
126 if (unlikely(value
== 0))
130 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
133 case offsetof(struct user_regs_struct
, gs
):
135 set_user_gs(task_pt_regs(task
), value
);
137 task_user_gs(task
) = value
;
143 #else /* CONFIG_X86_64 */
145 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
147 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
149 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
150 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
153 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
156 * Returning the value truncates it to 16 bits.
161 case offsetof(struct user_regs_struct
, fs
):
162 if (task
== current
) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg
));
167 return task
->thread
.fsindex
;
168 case offsetof(struct user_regs_struct
, gs
):
169 if (task
== current
) {
170 asm("movl %%gs,%0" : "=r" (seg
));
173 return task
->thread
.gsindex
;
174 case offsetof(struct user_regs_struct
, ds
):
175 if (task
== current
) {
176 asm("movl %%ds,%0" : "=r" (seg
));
179 return task
->thread
.ds
;
180 case offsetof(struct user_regs_struct
, es
):
181 if (task
== current
) {
182 asm("movl %%es,%0" : "=r" (seg
));
185 return task
->thread
.es
;
187 case offsetof(struct user_regs_struct
, cs
):
188 case offsetof(struct user_regs_struct
, ss
):
191 return *pt_regs_access(task_pt_regs(task
), offset
);
194 static int set_segment_reg(struct task_struct
*task
,
195 unsigned long offset
, u16 value
)
198 * The value argument was already truncated to 16 bits.
200 if (invalid_selector(value
))
204 case offsetof(struct user_regs_struct
,fs
):
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
209 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
210 task
->thread
.fs
!= 0) ||
211 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
212 task
->thread
.fs
== 0))
214 task
->thread
.fsindex
= value
;
216 loadsegment(fs
, task
->thread
.fsindex
);
218 case offsetof(struct user_regs_struct
,gs
):
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
223 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
224 task
->thread
.gs
!= 0) ||
225 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
226 task
->thread
.gs
== 0))
228 task
->thread
.gsindex
= value
;
230 load_gs_index(task
->thread
.gsindex
);
232 case offsetof(struct user_regs_struct
,ds
):
233 task
->thread
.ds
= value
;
235 loadsegment(ds
, task
->thread
.ds
);
237 case offsetof(struct user_regs_struct
,es
):
238 task
->thread
.es
= value
;
240 loadsegment(es
, task
->thread
.es
);
244 * Can't actually change these in 64-bit mode.
246 case offsetof(struct user_regs_struct
,cs
):
247 if (unlikely(value
== 0))
249 #ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task
, TIF_IA32
))
251 task_pt_regs(task
)->cs
= value
;
254 case offsetof(struct user_regs_struct
,ss
):
255 if (unlikely(value
== 0))
257 #ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task
, TIF_IA32
))
259 task_pt_regs(task
)->ss
= value
;
267 #endif /* CONFIG_X86_32 */
269 static unsigned long get_flags(struct task_struct
*task
)
271 unsigned long retval
= task_pt_regs(task
)->flags
;
274 * If the debugger set TF, hide it from the readout.
276 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
277 retval
&= ~X86_EFLAGS_TF
;
282 static int set_flags(struct task_struct
*task
, unsigned long value
)
284 struct pt_regs
*regs
= task_pt_regs(task
);
287 * If the user value contains TF, mark that
288 * it was not "us" (the debugger) that set it.
289 * If not, make sure it stays set if we had.
291 if (value
& X86_EFLAGS_TF
)
292 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
293 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
294 value
|= X86_EFLAGS_TF
;
296 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
301 static int putreg(struct task_struct
*child
,
302 unsigned long offset
, unsigned long value
)
305 case offsetof(struct user_regs_struct
, cs
):
306 case offsetof(struct user_regs_struct
, ds
):
307 case offsetof(struct user_regs_struct
, es
):
308 case offsetof(struct user_regs_struct
, fs
):
309 case offsetof(struct user_regs_struct
, gs
):
310 case offsetof(struct user_regs_struct
, ss
):
311 return set_segment_reg(child
, offset
, value
);
313 case offsetof(struct user_regs_struct
, flags
):
314 return set_flags(child
, value
);
317 case offsetof(struct user_regs_struct
,fs_base
):
318 if (value
>= TASK_SIZE_OF(child
))
321 * When changing the segment base, use do_arch_prctl
322 * to set either thread.fs or thread.fsindex and the
323 * corresponding GDT slot.
325 if (child
->thread
.fs
!= value
)
326 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
328 case offsetof(struct user_regs_struct
,gs_base
):
330 * Exactly the same here as the %fs handling above.
332 if (value
>= TASK_SIZE_OF(child
))
334 if (child
->thread
.gs
!= value
)
335 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
340 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
344 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
347 case offsetof(struct user_regs_struct
, cs
):
348 case offsetof(struct user_regs_struct
, ds
):
349 case offsetof(struct user_regs_struct
, es
):
350 case offsetof(struct user_regs_struct
, fs
):
351 case offsetof(struct user_regs_struct
, gs
):
352 case offsetof(struct user_regs_struct
, ss
):
353 return get_segment_reg(task
, offset
);
355 case offsetof(struct user_regs_struct
, flags
):
356 return get_flags(task
);
359 case offsetof(struct user_regs_struct
, fs_base
): {
361 * do_arch_prctl may have used a GDT slot instead of
362 * the MSR. To userland, it appears the same either
363 * way, except the %fs segment selector might not be 0.
365 unsigned int seg
= task
->thread
.fsindex
;
366 if (task
->thread
.fs
!= 0)
367 return task
->thread
.fs
;
369 asm("movl %%fs,%0" : "=r" (seg
));
370 if (seg
!= FS_TLS_SEL
)
372 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
374 case offsetof(struct user_regs_struct
, gs_base
): {
376 * Exactly the same here as the %fs handling above.
378 unsigned int seg
= task
->thread
.gsindex
;
379 if (task
->thread
.gs
!= 0)
380 return task
->thread
.gs
;
382 asm("movl %%gs,%0" : "=r" (seg
));
383 if (seg
!= GS_TLS_SEL
)
385 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
390 return *pt_regs_access(task_pt_regs(task
), offset
);
393 static int genregs_get(struct task_struct
*target
,
394 const struct user_regset
*regset
,
395 unsigned int pos
, unsigned int count
,
396 void *kbuf
, void __user
*ubuf
)
399 unsigned long *k
= kbuf
;
401 *k
++ = getreg(target
, pos
);
406 unsigned long __user
*u
= ubuf
;
408 if (__put_user(getreg(target
, pos
), u
++))
418 static int genregs_set(struct task_struct
*target
,
419 const struct user_regset
*regset
,
420 unsigned int pos
, unsigned int count
,
421 const void *kbuf
, const void __user
*ubuf
)
425 const unsigned long *k
= kbuf
;
426 while (count
> 0 && !ret
) {
427 ret
= putreg(target
, pos
, *k
++);
432 const unsigned long __user
*u
= ubuf
;
433 while (count
> 0 && !ret
) {
435 ret
= __get_user(word
, u
++);
438 ret
= putreg(target
, pos
, word
);
446 static void ptrace_triggered(struct perf_event
*bp
, void *data
)
449 struct thread_struct
*thread
= &(current
->thread
);
452 * Store in the virtual DR6 register the fact that the breakpoint
453 * was hit so the thread's debugger will see it.
455 for (i
= 0; i
< HBP_NUM
; i
++) {
456 if (thread
->ptrace_bps
[i
] == bp
)
460 thread
->debugreg6
|= (DR_TRAP0
<< i
);
464 * Walk through every ptrace breakpoints for this thread and
465 * build the dr7 value on top of their attributes.
468 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
472 struct arch_hw_breakpoint
*info
;
474 for (i
= 0; i
< HBP_NUM
; i
++) {
475 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
476 info
= counter_arch_bp(bp
[i
]);
477 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
485 * Handle ptrace writes to debug register 7.
487 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
489 struct thread_struct
*thread
= &(tsk
->thread
);
490 unsigned long old_dr7
;
491 int i
, orig_ret
= 0, rc
= 0;
492 int enabled
, second_pass
= 0;
494 int gen_len
, gen_type
;
495 struct perf_event
*bp
;
497 data
&= ~DR_CONTROL_RESERVED
;
498 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
501 * Loop through all the hardware breakpoints, making the
502 * appropriate changes to each.
504 for (i
= 0; i
< HBP_NUM
; i
++) {
505 enabled
= decode_dr7(data
, i
, &len
, &type
);
506 bp
= thread
->ptrace_bps
[i
];
511 * Don't unregister the breakpoints right-away,
512 * unless all register_user_hw_breakpoint()
513 * requests have succeeded. This prevents
514 * any window of opportunity for debug
515 * register grabbing by other users.
519 thread
->ptrace_bps
[i
] = NULL
;
520 unregister_hw_breakpoint(bp
);
526 * We shoud have at least an inactive breakpoint at this
527 * slot. It means the user is writing dr7 without having
528 * written the address register first
535 rc
= arch_bp_generic_fields(len
, type
, &gen_len
, &gen_type
);
540 * This is a temporary thing as bp is unregistered/registered
541 * to simulate modification
543 bp
= modify_user_hw_breakpoint(bp
, bp
->attr
.bp_addr
, gen_len
,
544 gen_type
, bp
->callback
,
546 thread
->ptrace_bps
[i
] = NULL
;
548 if (!bp
) { /* incorrect bp, or we have a bug in bp API */
557 thread
->ptrace_bps
[i
] = bp
;
560 * Make a second pass to free the remaining unused breakpoints
561 * or to restore the original breakpoints if an error occurred.
571 return ((orig_ret
< 0) ? orig_ret
: rc
);
575 * Handle PTRACE_PEEKUSR calls for the debug register area.
577 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
579 struct thread_struct
*thread
= &(tsk
->thread
);
580 unsigned long val
= 0;
583 struct perf_event
*bp
;
584 bp
= thread
->ptrace_bps
[n
];
587 val
= bp
->hw
.info
.address
;
589 val
= thread
->debugreg6
;
591 val
= ptrace_get_dr7(thread
->ptrace_bps
);
596 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
599 struct perf_event
*bp
;
600 struct thread_struct
*t
= &tsk
->thread
;
602 if (!t
->ptrace_bps
[nr
]) {
604 * Put stub len and type to register (reserve) an inactive but
607 bp
= register_user_hw_breakpoint(addr
, HW_BREAKPOINT_LEN_1
,
609 ptrace_triggered
, tsk
,
612 bp
= t
->ptrace_bps
[nr
];
613 t
->ptrace_bps
[nr
] = NULL
;
614 bp
= modify_user_hw_breakpoint(bp
, addr
, bp
->attr
.bp_len
,
624 * CHECKME: the previous code returned -EIO if the addr wasn't a
625 * valid task virtual addr. The new one will return -EINVAL in this
627 * -EINVAL may be what we want for in-kernel breakpoints users, but
628 * -EIO looks better for ptrace, since we refuse a register writing
629 * for the user. And anyway this is the previous behaviour.
634 t
->ptrace_bps
[nr
] = bp
;
640 * Handle PTRACE_POKEUSR calls for the debug register area.
642 int ptrace_set_debugreg(struct task_struct
*tsk
, int n
, unsigned long val
)
644 struct thread_struct
*thread
= &(tsk
->thread
);
647 /* There are no DR4 or DR5 registers */
648 if (n
== 4 || n
== 5)
652 thread
->debugreg6
= val
;
656 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
660 /* All that's left is DR7 */
662 rc
= ptrace_write_dr7(tsk
, val
);
669 * These access the current or another (stopped) task's io permission
670 * bitmap for debugging or core dump.
672 static int ioperm_active(struct task_struct
*target
,
673 const struct user_regset
*regset
)
675 return target
->thread
.io_bitmap_max
/ regset
->size
;
678 static int ioperm_get(struct task_struct
*target
,
679 const struct user_regset
*regset
,
680 unsigned int pos
, unsigned int count
,
681 void *kbuf
, void __user
*ubuf
)
683 if (!target
->thread
.io_bitmap_ptr
)
686 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
687 target
->thread
.io_bitmap_ptr
,
691 #ifdef CONFIG_X86_PTRACE_BTS
693 * A branch trace store context.
695 * Contexts may only be installed by ptrace_bts_config() and only for
698 * Contexts are destroyed when the tracee is detached from the tracer.
699 * The actual destruction work requires interrupts enabled, so the
700 * work is deferred and will be scheduled during __ptrace_unlink().
702 * Contexts hold an additional task_struct reference on the traced
703 * task, as well as a reference on the tracer's mm.
705 * Ptrace already holds a task_struct for the duration of ptrace operations,
706 * but since destruction is deferred, it may be executed after both
707 * tracer and tracee exited.
710 /* The branch trace handle. */
711 struct bts_tracer
*tracer
;
713 /* The buffer used to store the branch trace and its size. */
717 /* The mm that paid for the above buffer. */
718 struct mm_struct
*mm
;
720 /* The task this context belongs to. */
721 struct task_struct
*task
;
723 /* The signal to send on a bts buffer overflow. */
724 unsigned int bts_ovfl_signal
;
726 /* The work struct to destroy a context. */
727 struct work_struct work
;
730 static int alloc_bts_buffer(struct bts_context
*context
, unsigned int size
)
735 err
= account_locked_memory(current
->mm
, current
->signal
->rlim
, size
);
739 buffer
= kzalloc(size
, GFP_KERNEL
);
743 context
->buffer
= buffer
;
744 context
->size
= size
;
745 context
->mm
= get_task_mm(current
);
750 refund_locked_memory(current
->mm
, size
);
754 static inline void free_bts_buffer(struct bts_context
*context
)
756 if (!context
->buffer
)
759 kfree(context
->buffer
);
760 context
->buffer
= NULL
;
762 refund_locked_memory(context
->mm
, context
->size
);
769 static void free_bts_context_work(struct work_struct
*w
)
771 struct bts_context
*context
;
773 context
= container_of(w
, struct bts_context
, work
);
775 ds_release_bts(context
->tracer
);
776 put_task_struct(context
->task
);
777 free_bts_buffer(context
);
781 static inline void free_bts_context(struct bts_context
*context
)
783 INIT_WORK(&context
->work
, free_bts_context_work
);
784 schedule_work(&context
->work
);
787 static inline struct bts_context
*alloc_bts_context(struct task_struct
*task
)
789 struct bts_context
*context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
791 context
->task
= task
;
794 get_task_struct(task
);
800 static int ptrace_bts_read_record(struct task_struct
*child
, size_t index
,
801 struct bts_struct __user
*out
)
803 struct bts_context
*context
;
804 const struct bts_trace
*trace
;
805 struct bts_struct bts
;
806 const unsigned char *at
;
809 context
= child
->bts
;
813 trace
= ds_read_bts(context
->tracer
);
817 at
= trace
->ds
.top
- ((index
+ 1) * trace
->ds
.size
);
818 if ((void *)at
< trace
->ds
.begin
)
819 at
+= (trace
->ds
.n
* trace
->ds
.size
);
824 error
= trace
->read(context
->tracer
, at
, &bts
);
828 if (copy_to_user(out
, &bts
, sizeof(bts
)))
834 static int ptrace_bts_drain(struct task_struct
*child
,
836 struct bts_struct __user
*out
)
838 struct bts_context
*context
;
839 const struct bts_trace
*trace
;
840 const unsigned char *at
;
841 int error
, drained
= 0;
843 context
= child
->bts
;
847 trace
= ds_read_bts(context
->tracer
);
854 if (size
< (trace
->ds
.top
- trace
->ds
.begin
))
857 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
858 out
++, drained
++, at
+= trace
->ds
.size
) {
859 struct bts_struct bts
;
861 error
= trace
->read(context
->tracer
, at
, &bts
);
865 if (copy_to_user(out
, &bts
, sizeof(bts
)))
869 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
871 error
= ds_reset_bts(context
->tracer
);
878 static int ptrace_bts_config(struct task_struct
*child
,
880 const struct ptrace_bts_config __user
*ucfg
)
882 struct bts_context
*context
;
883 struct ptrace_bts_config cfg
;
884 unsigned int flags
= 0;
886 if (cfg_size
< sizeof(cfg
))
889 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
892 context
= child
->bts
;
894 context
= alloc_bts_context(child
);
898 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
903 context
->bts_ovfl_signal
= cfg
.signal
;
906 ds_release_bts(context
->tracer
);
907 context
->tracer
= NULL
;
909 if ((cfg
.flags
& PTRACE_BTS_O_ALLOC
) && (cfg
.size
!= context
->size
)) {
912 free_bts_buffer(context
);
916 err
= alloc_bts_buffer(context
, cfg
.size
);
921 if (cfg
.flags
& PTRACE_BTS_O_TRACE
)
924 if (cfg
.flags
& PTRACE_BTS_O_SCHED
)
925 flags
|= BTS_TIMESTAMPS
;
928 ds_request_bts_task(child
, context
->buffer
, context
->size
,
929 NULL
, (size_t)-1, flags
);
930 if (unlikely(IS_ERR(context
->tracer
))) {
931 int error
= PTR_ERR(context
->tracer
);
933 free_bts_buffer(context
);
934 context
->tracer
= NULL
;
941 static int ptrace_bts_status(struct task_struct
*child
,
943 struct ptrace_bts_config __user
*ucfg
)
945 struct bts_context
*context
;
946 const struct bts_trace
*trace
;
947 struct ptrace_bts_config cfg
;
949 context
= child
->bts
;
953 if (cfg_size
< sizeof(cfg
))
956 trace
= ds_read_bts(context
->tracer
);
960 memset(&cfg
, 0, sizeof(cfg
));
961 cfg
.size
= trace
->ds
.end
- trace
->ds
.begin
;
962 cfg
.signal
= context
->bts_ovfl_signal
;
963 cfg
.bts_size
= sizeof(struct bts_struct
);
966 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
968 if (trace
->ds
.flags
& BTS_USER
)
969 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
971 if (trace
->ds
.flags
& BTS_TIMESTAMPS
)
972 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
974 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
980 static int ptrace_bts_clear(struct task_struct
*child
)
982 struct bts_context
*context
;
983 const struct bts_trace
*trace
;
985 context
= child
->bts
;
989 trace
= ds_read_bts(context
->tracer
);
993 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
995 return ds_reset_bts(context
->tracer
);
998 static int ptrace_bts_size(struct task_struct
*child
)
1000 struct bts_context
*context
;
1001 const struct bts_trace
*trace
;
1003 context
= child
->bts
;
1007 trace
= ds_read_bts(context
->tracer
);
1011 return (trace
->ds
.top
- trace
->ds
.begin
) / trace
->ds
.size
;
1015 * Called from __ptrace_unlink() after the child has been moved back
1016 * to its original parent.
1018 void ptrace_bts_untrace(struct task_struct
*child
)
1020 if (unlikely(child
->bts
)) {
1021 free_bts_context(child
->bts
);
1025 #endif /* CONFIG_X86_PTRACE_BTS */
1028 * Called by kernel/ptrace.c when detaching..
1030 * Make sure the single step bit is not set.
1032 void ptrace_disable(struct task_struct
*child
)
1034 user_disable_single_step(child
);
1035 #ifdef TIF_SYSCALL_EMU
1036 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
1040 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1041 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
1044 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
1047 unsigned long __user
*datap
= (unsigned long __user
*)data
;
1050 /* read the word at location addr in the USER area. */
1051 case PTRACE_PEEKUSR
: {
1055 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
1056 addr
>= sizeof(struct user
))
1059 tmp
= 0; /* Default return condition */
1060 if (addr
< sizeof(struct user_regs_struct
))
1061 tmp
= getreg(child
, addr
);
1062 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1063 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1064 addr
-= offsetof(struct user
, u_debugreg
[0]);
1065 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1067 ret
= put_user(tmp
, datap
);
1071 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
1073 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
1074 addr
>= sizeof(struct user
))
1077 if (addr
< sizeof(struct user_regs_struct
))
1078 ret
= putreg(child
, addr
, data
);
1079 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1080 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1081 addr
-= offsetof(struct user
, u_debugreg
[0]);
1082 ret
= ptrace_set_debugreg(child
,
1083 addr
/ sizeof(data
), data
);
1087 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1088 return copy_regset_to_user(child
,
1089 task_user_regset_view(current
),
1091 0, sizeof(struct user_regs_struct
),
1094 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1095 return copy_regset_from_user(child
,
1096 task_user_regset_view(current
),
1098 0, sizeof(struct user_regs_struct
),
1101 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1102 return copy_regset_to_user(child
,
1103 task_user_regset_view(current
),
1105 0, sizeof(struct user_i387_struct
),
1108 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1109 return copy_regset_from_user(child
,
1110 task_user_regset_view(current
),
1112 0, sizeof(struct user_i387_struct
),
1115 #ifdef CONFIG_X86_32
1116 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1117 return copy_regset_to_user(child
, &user_x86_32_view
,
1119 0, sizeof(struct user_fxsr_struct
),
1122 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1123 return copy_regset_from_user(child
, &user_x86_32_view
,
1125 0, sizeof(struct user_fxsr_struct
),
1129 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1130 case PTRACE_GET_THREAD_AREA
:
1133 ret
= do_get_thread_area(child
, addr
,
1134 (struct user_desc __user
*) data
);
1137 case PTRACE_SET_THREAD_AREA
:
1140 ret
= do_set_thread_area(child
, addr
,
1141 (struct user_desc __user
*) data
, 0);
1145 #ifdef CONFIG_X86_64
1146 /* normal 64bit interface to access TLS data.
1147 Works just like arch_prctl, except that the arguments
1149 case PTRACE_ARCH_PRCTL
:
1150 ret
= do_arch_prctl(child
, data
, addr
);
1155 * These bits need more cooking - not enabled yet:
1157 #ifdef CONFIG_X86_PTRACE_BTS
1158 case PTRACE_BTS_CONFIG
:
1159 ret
= ptrace_bts_config
1160 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1163 case PTRACE_BTS_STATUS
:
1164 ret
= ptrace_bts_status
1165 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1168 case PTRACE_BTS_SIZE
:
1169 ret
= ptrace_bts_size(child
);
1172 case PTRACE_BTS_GET
:
1173 ret
= ptrace_bts_read_record
1174 (child
, data
, (struct bts_struct __user
*) addr
);
1177 case PTRACE_BTS_CLEAR
:
1178 ret
= ptrace_bts_clear(child
);
1181 case PTRACE_BTS_DRAIN
:
1182 ret
= ptrace_bts_drain
1183 (child
, data
, (struct bts_struct __user
*) addr
);
1185 #endif /* CONFIG_X86_PTRACE_BTS */
1188 ret
= ptrace_request(child
, request
, addr
, data
);
1195 #ifdef CONFIG_IA32_EMULATION
1197 #include <linux/compat.h>
1198 #include <linux/syscalls.h>
1199 #include <asm/ia32.h>
1200 #include <asm/user32.h>
1203 case offsetof(struct user32, regs.l): \
1204 regs->q = value; break
1207 case offsetof(struct user32, regs.rs): \
1208 return set_segment_reg(child, \
1209 offsetof(struct user_regs_struct, rs), \
1213 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
1215 struct pt_regs
*regs
= task_pt_regs(child
);
1236 case offsetof(struct user32
, regs
.orig_eax
):
1238 * A 32-bit debugger setting orig_eax means to restore
1239 * the state of the task restarting a 32-bit syscall.
1240 * Make sure we interpret the -ERESTART* codes correctly
1241 * in case the task is not actually still sitting at the
1242 * exit from a 32-bit syscall with TS_COMPAT still set.
1244 regs
->orig_ax
= value
;
1245 if (syscall_get_nr(child
, regs
) >= 0)
1246 task_thread_info(child
)->status
|= TS_COMPAT
;
1249 case offsetof(struct user32
, regs
.eflags
):
1250 return set_flags(child
, value
);
1252 case offsetof(struct user32
, u_debugreg
[0]) ...
1253 offsetof(struct user32
, u_debugreg
[7]):
1254 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1255 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1258 if (regno
> sizeof(struct user32
) || (regno
& 3))
1262 * Other dummy fields in the virtual user structure
1274 case offsetof(struct user32, regs.l): \
1275 *val = regs->q; break
1278 case offsetof(struct user32, regs.rs): \
1279 *val = get_segment_reg(child, \
1280 offsetof(struct user_regs_struct, rs)); \
1283 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1285 struct pt_regs
*regs
= task_pt_regs(child
);
1303 R32(orig_eax
, orig_ax
);
1307 case offsetof(struct user32
, regs
.eflags
):
1308 *val
= get_flags(child
);
1311 case offsetof(struct user32
, u_debugreg
[0]) ...
1312 offsetof(struct user32
, u_debugreg
[7]):
1313 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1314 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1318 if (regno
> sizeof(struct user32
) || (regno
& 3))
1322 * Other dummy fields in the virtual user structure
1334 static int genregs32_get(struct task_struct
*target
,
1335 const struct user_regset
*regset
,
1336 unsigned int pos
, unsigned int count
,
1337 void *kbuf
, void __user
*ubuf
)
1340 compat_ulong_t
*k
= kbuf
;
1342 getreg32(target
, pos
, k
++);
1343 count
-= sizeof(*k
);
1347 compat_ulong_t __user
*u
= ubuf
;
1349 compat_ulong_t word
;
1350 getreg32(target
, pos
, &word
);
1351 if (__put_user(word
, u
++))
1353 count
-= sizeof(*u
);
1361 static int genregs32_set(struct task_struct
*target
,
1362 const struct user_regset
*regset
,
1363 unsigned int pos
, unsigned int count
,
1364 const void *kbuf
, const void __user
*ubuf
)
1368 const compat_ulong_t
*k
= kbuf
;
1369 while (count
> 0 && !ret
) {
1370 ret
= putreg32(target
, pos
, *k
++);
1371 count
-= sizeof(*k
);
1375 const compat_ulong_t __user
*u
= ubuf
;
1376 while (count
> 0 && !ret
) {
1377 compat_ulong_t word
;
1378 ret
= __get_user(word
, u
++);
1381 ret
= putreg32(target
, pos
, word
);
1382 count
-= sizeof(*u
);
1389 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1390 compat_ulong_t caddr
, compat_ulong_t cdata
)
1392 unsigned long addr
= caddr
;
1393 unsigned long data
= cdata
;
1394 void __user
*datap
= compat_ptr(data
);
1399 case PTRACE_PEEKUSR
:
1400 ret
= getreg32(child
, addr
, &val
);
1402 ret
= put_user(val
, (__u32 __user
*)datap
);
1405 case PTRACE_POKEUSR
:
1406 ret
= putreg32(child
, addr
, data
);
1409 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1410 return copy_regset_to_user(child
, &user_x86_32_view
,
1412 0, sizeof(struct user_regs_struct32
),
1415 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1416 return copy_regset_from_user(child
, &user_x86_32_view
,
1418 sizeof(struct user_regs_struct32
),
1421 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1422 return copy_regset_to_user(child
, &user_x86_32_view
,
1424 sizeof(struct user_i387_ia32_struct
),
1427 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1428 return copy_regset_from_user(
1429 child
, &user_x86_32_view
, REGSET_FP
,
1430 0, sizeof(struct user_i387_ia32_struct
), datap
);
1432 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1433 return copy_regset_to_user(child
, &user_x86_32_view
,
1435 sizeof(struct user32_fxsr_struct
),
1438 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1439 return copy_regset_from_user(child
, &user_x86_32_view
,
1441 sizeof(struct user32_fxsr_struct
),
1444 case PTRACE_GET_THREAD_AREA
:
1445 case PTRACE_SET_THREAD_AREA
:
1446 #ifdef CONFIG_X86_PTRACE_BTS
1447 case PTRACE_BTS_CONFIG
:
1448 case PTRACE_BTS_STATUS
:
1449 case PTRACE_BTS_SIZE
:
1450 case PTRACE_BTS_GET
:
1451 case PTRACE_BTS_CLEAR
:
1452 case PTRACE_BTS_DRAIN
:
1453 #endif /* CONFIG_X86_PTRACE_BTS */
1454 return arch_ptrace(child
, request
, addr
, data
);
1457 return compat_ptrace_request(child
, request
, addr
, data
);
1463 #endif /* CONFIG_IA32_EMULATION */
1465 #ifdef CONFIG_X86_64
1467 static const struct user_regset x86_64_regsets
[] = {
1468 [REGSET_GENERAL
] = {
1469 .core_note_type
= NT_PRSTATUS
,
1470 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1471 .size
= sizeof(long), .align
= sizeof(long),
1472 .get
= genregs_get
, .set
= genregs_set
1475 .core_note_type
= NT_PRFPREG
,
1476 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1477 .size
= sizeof(long), .align
= sizeof(long),
1478 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1480 [REGSET_IOPERM64
] = {
1481 .core_note_type
= NT_386_IOPERM
,
1482 .n
= IO_BITMAP_LONGS
,
1483 .size
= sizeof(long), .align
= sizeof(long),
1484 .active
= ioperm_active
, .get
= ioperm_get
1488 static const struct user_regset_view user_x86_64_view
= {
1489 .name
= "x86_64", .e_machine
= EM_X86_64
,
1490 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1493 #else /* CONFIG_X86_32 */
1495 #define user_regs_struct32 user_regs_struct
1496 #define genregs32_get genregs_get
1497 #define genregs32_set genregs_set
1499 #define user_i387_ia32_struct user_i387_struct
1500 #define user32_fxsr_struct user_fxsr_struct
1502 #endif /* CONFIG_X86_64 */
1504 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1505 static const struct user_regset x86_32_regsets
[] = {
1506 [REGSET_GENERAL
] = {
1507 .core_note_type
= NT_PRSTATUS
,
1508 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1509 .size
= sizeof(u32
), .align
= sizeof(u32
),
1510 .get
= genregs32_get
, .set
= genregs32_set
1513 .core_note_type
= NT_PRFPREG
,
1514 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1515 .size
= sizeof(u32
), .align
= sizeof(u32
),
1516 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1519 .core_note_type
= NT_PRXFPREG
,
1520 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1521 .size
= sizeof(u32
), .align
= sizeof(u32
),
1522 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1525 .core_note_type
= NT_386_TLS
,
1526 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1527 .size
= sizeof(struct user_desc
),
1528 .align
= sizeof(struct user_desc
),
1529 .active
= regset_tls_active
,
1530 .get
= regset_tls_get
, .set
= regset_tls_set
1532 [REGSET_IOPERM32
] = {
1533 .core_note_type
= NT_386_IOPERM
,
1534 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1535 .size
= sizeof(u32
), .align
= sizeof(u32
),
1536 .active
= ioperm_active
, .get
= ioperm_get
1540 static const struct user_regset_view user_x86_32_view
= {
1541 .name
= "i386", .e_machine
= EM_386
,
1542 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1546 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1548 #ifdef CONFIG_IA32_EMULATION
1549 if (test_tsk_thread_flag(task
, TIF_IA32
))
1551 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1552 return &user_x86_32_view
;
1554 #ifdef CONFIG_X86_64
1555 return &user_x86_64_view
;
1559 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1560 int error_code
, int si_code
)
1562 struct siginfo info
;
1564 tsk
->thread
.trap_no
= 1;
1565 tsk
->thread
.error_code
= error_code
;
1567 memset(&info
, 0, sizeof(info
));
1568 info
.si_signo
= SIGTRAP
;
1569 info
.si_code
= si_code
;
1572 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1574 /* Send us the fake SIGTRAP */
1575 force_sig_info(SIGTRAP
, &info
, tsk
);
1579 #ifdef CONFIG_X86_32
1581 #elif defined CONFIG_IA32_EMULATION
1582 # define IS_IA32 is_compat_task()
1588 * We must return the syscall number to actually look up in the table.
1589 * This can be -1L to skip running any syscall at all.
1591 asmregparm
long syscall_trace_enter(struct pt_regs
*regs
)
1596 * If we stepped into a sysenter/syscall insn, it trapped in
1597 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1598 * If user-mode had set TF itself, then it's still clear from
1599 * do_debug() and we need to set it again to restore the user
1600 * state. If we entered on the slow path, TF was already set.
1602 if (test_thread_flag(TIF_SINGLESTEP
))
1603 regs
->flags
|= X86_EFLAGS_TF
;
1605 /* do the secure computing check first */
1606 secure_computing(regs
->orig_ax
);
1608 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1611 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1612 tracehook_report_syscall_entry(regs
))
1615 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1616 trace_sys_enter(regs
, regs
->orig_ax
);
1618 if (unlikely(current
->audit_context
)) {
1620 audit_syscall_entry(AUDIT_ARCH_I386
,
1623 regs
->dx
, regs
->si
);
1624 #ifdef CONFIG_X86_64
1626 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1629 regs
->dx
, regs
->r10
);
1633 return ret
?: regs
->orig_ax
;
1636 asmregparm
void syscall_trace_leave(struct pt_regs
*regs
)
1638 if (unlikely(current
->audit_context
))
1639 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1641 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1642 trace_sys_exit(regs
, regs
->ax
);
1644 if (test_thread_flag(TIF_SYSCALL_TRACE
))
1645 tracehook_report_syscall_exit(regs
, 0);
1648 * If TIF_SYSCALL_EMU is set, we only get here because of
1649 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1650 * We already reported this syscall instruction in
1651 * syscall_trace_enter(), so don't do any more now.
1653 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1657 * If we are single-stepping, synthesize a trap to follow the
1658 * system call instruction.
1660 if (test_thread_flag(TIF_SINGLESTEP
) &&
1661 tracehook_consider_fatal_signal(current
, SIGTRAP
))
1662 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);