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
);
316 * Orig_ax is really just a flag with small positive and
317 * negative values, so make sure to always sign-extend it
318 * from 32 bits so that it works correctly regardless of
319 * whether we come from a 32-bit environment or not.
321 case offsetof(struct user_regs_struct
, orig_ax
):
322 value
= (long) (s32
) value
;
325 case offsetof(struct user_regs_struct
,fs_base
):
326 if (value
>= TASK_SIZE_OF(child
))
329 * When changing the segment base, use do_arch_prctl
330 * to set either thread.fs or thread.fsindex and the
331 * corresponding GDT slot.
333 if (child
->thread
.fs
!= value
)
334 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
336 case offsetof(struct user_regs_struct
,gs_base
):
338 * Exactly the same here as the %fs handling above.
340 if (value
>= TASK_SIZE_OF(child
))
342 if (child
->thread
.gs
!= value
)
343 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
348 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
352 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
355 case offsetof(struct user_regs_struct
, cs
):
356 case offsetof(struct user_regs_struct
, ds
):
357 case offsetof(struct user_regs_struct
, es
):
358 case offsetof(struct user_regs_struct
, fs
):
359 case offsetof(struct user_regs_struct
, gs
):
360 case offsetof(struct user_regs_struct
, ss
):
361 return get_segment_reg(task
, offset
);
363 case offsetof(struct user_regs_struct
, flags
):
364 return get_flags(task
);
367 case offsetof(struct user_regs_struct
, fs_base
): {
369 * do_arch_prctl may have used a GDT slot instead of
370 * the MSR. To userland, it appears the same either
371 * way, except the %fs segment selector might not be 0.
373 unsigned int seg
= task
->thread
.fsindex
;
374 if (task
->thread
.fs
!= 0)
375 return task
->thread
.fs
;
377 asm("movl %%fs,%0" : "=r" (seg
));
378 if (seg
!= FS_TLS_SEL
)
380 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
382 case offsetof(struct user_regs_struct
, gs_base
): {
384 * Exactly the same here as the %fs handling above.
386 unsigned int seg
= task
->thread
.gsindex
;
387 if (task
->thread
.gs
!= 0)
388 return task
->thread
.gs
;
390 asm("movl %%gs,%0" : "=r" (seg
));
391 if (seg
!= GS_TLS_SEL
)
393 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
398 return *pt_regs_access(task_pt_regs(task
), offset
);
401 static int genregs_get(struct task_struct
*target
,
402 const struct user_regset
*regset
,
403 unsigned int pos
, unsigned int count
,
404 void *kbuf
, void __user
*ubuf
)
407 unsigned long *k
= kbuf
;
409 *k
++ = getreg(target
, pos
);
414 unsigned long __user
*u
= ubuf
;
416 if (__put_user(getreg(target
, pos
), u
++))
426 static int genregs_set(struct task_struct
*target
,
427 const struct user_regset
*regset
,
428 unsigned int pos
, unsigned int count
,
429 const void *kbuf
, const void __user
*ubuf
)
433 const unsigned long *k
= kbuf
;
434 while (count
> 0 && !ret
) {
435 ret
= putreg(target
, pos
, *k
++);
440 const unsigned long __user
*u
= ubuf
;
441 while (count
> 0 && !ret
) {
443 ret
= __get_user(word
, u
++);
446 ret
= putreg(target
, pos
, word
);
455 * Decode the length and type bits for a particular breakpoint as
456 * stored in debug register 7. Return the "enabled" status.
458 static int decode_dr7(unsigned long dr7
, int bpnum
, unsigned *len
,
461 int bp_info
= dr7
>> (DR_CONTROL_SHIFT
+ bpnum
* DR_CONTROL_SIZE
);
463 *len
= (bp_info
& 0xc) | 0x40;
464 *type
= (bp_info
& 0x3) | 0x80;
465 return (dr7
>> (bpnum
* DR_ENABLE_SIZE
)) & 0x3;
468 static void ptrace_triggered(struct hw_breakpoint
*bp
, struct pt_regs
*regs
)
470 struct thread_struct
*thread
= &(current
->thread
);
474 * Store in the virtual DR6 register the fact that the breakpoint
475 * was hit so the thread's debugger will see it.
477 for (i
= 0; i
< hbp_kernel_pos
; i
++)
479 * We will check bp->info.address against the address stored in
480 * thread's hbp structure and not debugreg[i]. This is to ensure
481 * that the corresponding bit for 'i' in DR7 register is enabled
483 if (bp
->info
.address
== thread
->hbp
[i
]->info
.address
)
486 thread
->debugreg6
|= (DR_TRAP0
<< i
);
490 * Handle ptrace writes to debug register 7.
492 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
494 struct thread_struct
*thread
= &(tsk
->thread
);
495 unsigned long old_dr7
= thread
->debugreg7
;
496 int i
, orig_ret
= 0, rc
= 0;
497 int enabled
, second_pass
= 0;
499 struct hw_breakpoint
*bp
;
501 data
&= ~DR_CONTROL_RESERVED
;
504 * Loop through all the hardware breakpoints, making the
505 * appropriate changes to each.
507 for (i
= 0; i
< HBP_NUM
; i
++) {
508 enabled
= decode_dr7(data
, i
, &len
, &type
);
513 /* Don't unregister the breakpoints right-away,
514 * unless all register_user_hw_breakpoint()
515 * requests have succeeded. This prevents
516 * any window of opportunity for debug
517 * register grabbing by other users.
521 unregister_user_hw_breakpoint(tsk
, bp
);
528 bp
= kzalloc(sizeof(struct hw_breakpoint
), GFP_KERNEL
);
530 bp
->info
.address
= thread
->debugreg
[i
];
531 bp
->triggered
= ptrace_triggered
;
533 bp
->info
.type
= type
;
534 rc
= register_user_hw_breakpoint(tsk
, bp
);
539 rc
= modify_user_hw_breakpoint(tsk
, bp
);
544 * Make a second pass to free the remaining unused breakpoints
545 * or to restore the original breakpoints if an error occurred.
555 return ((orig_ret
< 0) ? orig_ret
: rc
);
559 * Handle PTRACE_PEEKUSR calls for the debug register area.
561 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
563 struct thread_struct
*thread
= &(tsk
->thread
);
564 unsigned long val
= 0;
567 val
= thread
->debugreg
[n
];
569 val
= thread
->debugreg6
;
571 val
= thread
->debugreg7
;
576 * Handle PTRACE_POKEUSR calls for the debug register area.
578 int ptrace_set_debugreg(struct task_struct
*tsk
, int n
, unsigned long val
)
580 struct thread_struct
*thread
= &(tsk
->thread
);
583 /* There are no DR4 or DR5 registers */
584 if (n
== 4 || n
== 5)
588 tsk
->thread
.debugreg6
= val
;
592 if (thread
->hbp
[n
]) {
593 if (arch_check_va_in_userspace(val
,
594 thread
->hbp
[n
]->info
.len
) == 0) {
598 thread
->hbp
[n
]->info
.address
= val
;
600 thread
->debugreg
[n
] = val
;
602 /* All that's left is DR7 */
604 rc
= ptrace_write_dr7(tsk
, val
);
611 * These access the current or another (stopped) task's io permission
612 * bitmap for debugging or core dump.
614 static int ioperm_active(struct task_struct
*target
,
615 const struct user_regset
*regset
)
617 return target
->thread
.io_bitmap_max
/ regset
->size
;
620 static int ioperm_get(struct task_struct
*target
,
621 const struct user_regset
*regset
,
622 unsigned int pos
, unsigned int count
,
623 void *kbuf
, void __user
*ubuf
)
625 if (!target
->thread
.io_bitmap_ptr
)
628 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
629 target
->thread
.io_bitmap_ptr
,
633 #ifdef CONFIG_X86_PTRACE_BTS
635 * A branch trace store context.
637 * Contexts may only be installed by ptrace_bts_config() and only for
640 * Contexts are destroyed when the tracee is detached from the tracer.
641 * The actual destruction work requires interrupts enabled, so the
642 * work is deferred and will be scheduled during __ptrace_unlink().
644 * Contexts hold an additional task_struct reference on the traced
645 * task, as well as a reference on the tracer's mm.
647 * Ptrace already holds a task_struct for the duration of ptrace operations,
648 * but since destruction is deferred, it may be executed after both
649 * tracer and tracee exited.
652 /* The branch trace handle. */
653 struct bts_tracer
*tracer
;
655 /* The buffer used to store the branch trace and its size. */
659 /* The mm that paid for the above buffer. */
660 struct mm_struct
*mm
;
662 /* The task this context belongs to. */
663 struct task_struct
*task
;
665 /* The signal to send on a bts buffer overflow. */
666 unsigned int bts_ovfl_signal
;
668 /* The work struct to destroy a context. */
669 struct work_struct work
;
672 static int alloc_bts_buffer(struct bts_context
*context
, unsigned int size
)
677 err
= account_locked_memory(current
->mm
, current
->signal
->rlim
, size
);
681 buffer
= kzalloc(size
, GFP_KERNEL
);
685 context
->buffer
= buffer
;
686 context
->size
= size
;
687 context
->mm
= get_task_mm(current
);
692 refund_locked_memory(current
->mm
, size
);
696 static inline void free_bts_buffer(struct bts_context
*context
)
698 if (!context
->buffer
)
701 kfree(context
->buffer
);
702 context
->buffer
= NULL
;
704 refund_locked_memory(context
->mm
, context
->size
);
711 static void free_bts_context_work(struct work_struct
*w
)
713 struct bts_context
*context
;
715 context
= container_of(w
, struct bts_context
, work
);
717 ds_release_bts(context
->tracer
);
718 put_task_struct(context
->task
);
719 free_bts_buffer(context
);
723 static inline void free_bts_context(struct bts_context
*context
)
725 INIT_WORK(&context
->work
, free_bts_context_work
);
726 schedule_work(&context
->work
);
729 static inline struct bts_context
*alloc_bts_context(struct task_struct
*task
)
731 struct bts_context
*context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
733 context
->task
= task
;
736 get_task_struct(task
);
742 static int ptrace_bts_read_record(struct task_struct
*child
, size_t index
,
743 struct bts_struct __user
*out
)
745 struct bts_context
*context
;
746 const struct bts_trace
*trace
;
747 struct bts_struct bts
;
748 const unsigned char *at
;
751 context
= child
->bts
;
755 trace
= ds_read_bts(context
->tracer
);
759 at
= trace
->ds
.top
- ((index
+ 1) * trace
->ds
.size
);
760 if ((void *)at
< trace
->ds
.begin
)
761 at
+= (trace
->ds
.n
* trace
->ds
.size
);
766 error
= trace
->read(context
->tracer
, at
, &bts
);
770 if (copy_to_user(out
, &bts
, sizeof(bts
)))
776 static int ptrace_bts_drain(struct task_struct
*child
,
778 struct bts_struct __user
*out
)
780 struct bts_context
*context
;
781 const struct bts_trace
*trace
;
782 const unsigned char *at
;
783 int error
, drained
= 0;
785 context
= child
->bts
;
789 trace
= ds_read_bts(context
->tracer
);
796 if (size
< (trace
->ds
.top
- trace
->ds
.begin
))
799 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
800 out
++, drained
++, at
+= trace
->ds
.size
) {
801 struct bts_struct bts
;
803 error
= trace
->read(context
->tracer
, at
, &bts
);
807 if (copy_to_user(out
, &bts
, sizeof(bts
)))
811 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
813 error
= ds_reset_bts(context
->tracer
);
820 static int ptrace_bts_config(struct task_struct
*child
,
822 const struct ptrace_bts_config __user
*ucfg
)
824 struct bts_context
*context
;
825 struct ptrace_bts_config cfg
;
826 unsigned int flags
= 0;
828 if (cfg_size
< sizeof(cfg
))
831 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
834 context
= child
->bts
;
836 context
= alloc_bts_context(child
);
840 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
845 context
->bts_ovfl_signal
= cfg
.signal
;
848 ds_release_bts(context
->tracer
);
849 context
->tracer
= NULL
;
851 if ((cfg
.flags
& PTRACE_BTS_O_ALLOC
) && (cfg
.size
!= context
->size
)) {
854 free_bts_buffer(context
);
858 err
= alloc_bts_buffer(context
, cfg
.size
);
863 if (cfg
.flags
& PTRACE_BTS_O_TRACE
)
866 if (cfg
.flags
& PTRACE_BTS_O_SCHED
)
867 flags
|= BTS_TIMESTAMPS
;
870 ds_request_bts_task(child
, context
->buffer
, context
->size
,
871 NULL
, (size_t)-1, flags
);
872 if (unlikely(IS_ERR(context
->tracer
))) {
873 int error
= PTR_ERR(context
->tracer
);
875 free_bts_buffer(context
);
876 context
->tracer
= NULL
;
883 static int ptrace_bts_status(struct task_struct
*child
,
885 struct ptrace_bts_config __user
*ucfg
)
887 struct bts_context
*context
;
888 const struct bts_trace
*trace
;
889 struct ptrace_bts_config cfg
;
891 context
= child
->bts
;
895 if (cfg_size
< sizeof(cfg
))
898 trace
= ds_read_bts(context
->tracer
);
902 memset(&cfg
, 0, sizeof(cfg
));
903 cfg
.size
= trace
->ds
.end
- trace
->ds
.begin
;
904 cfg
.signal
= context
->bts_ovfl_signal
;
905 cfg
.bts_size
= sizeof(struct bts_struct
);
908 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
910 if (trace
->ds
.flags
& BTS_USER
)
911 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
913 if (trace
->ds
.flags
& BTS_TIMESTAMPS
)
914 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
916 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
922 static int ptrace_bts_clear(struct task_struct
*child
)
924 struct bts_context
*context
;
925 const struct bts_trace
*trace
;
927 context
= child
->bts
;
931 trace
= ds_read_bts(context
->tracer
);
935 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
937 return ds_reset_bts(context
->tracer
);
940 static int ptrace_bts_size(struct task_struct
*child
)
942 struct bts_context
*context
;
943 const struct bts_trace
*trace
;
945 context
= child
->bts
;
949 trace
= ds_read_bts(context
->tracer
);
953 return (trace
->ds
.top
- trace
->ds
.begin
) / trace
->ds
.size
;
957 * Called from __ptrace_unlink() after the child has been moved back
958 * to its original parent.
960 void ptrace_bts_untrace(struct task_struct
*child
)
962 if (unlikely(child
->bts
)) {
963 free_bts_context(child
->bts
);
967 #endif /* CONFIG_X86_PTRACE_BTS */
970 * Called by kernel/ptrace.c when detaching..
972 * Make sure the single step bit is not set.
974 void ptrace_disable(struct task_struct
*child
)
976 user_disable_single_step(child
);
977 #ifdef TIF_SYSCALL_EMU
978 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
982 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
983 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
986 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
989 unsigned long __user
*datap
= (unsigned long __user
*)data
;
992 /* read the word at location addr in the USER area. */
993 case PTRACE_PEEKUSR
: {
997 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
998 addr
>= sizeof(struct user
))
1001 tmp
= 0; /* Default return condition */
1002 if (addr
< sizeof(struct user_regs_struct
))
1003 tmp
= getreg(child
, addr
);
1004 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1005 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1006 addr
-= offsetof(struct user
, u_debugreg
[0]);
1007 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1009 ret
= put_user(tmp
, datap
);
1013 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
1015 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
1016 addr
>= sizeof(struct user
))
1019 if (addr
< sizeof(struct user_regs_struct
))
1020 ret
= putreg(child
, addr
, data
);
1021 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1022 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1023 addr
-= offsetof(struct user
, u_debugreg
[0]);
1024 ret
= ptrace_set_debugreg(child
,
1025 addr
/ sizeof(data
), data
);
1029 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1030 return copy_regset_to_user(child
,
1031 task_user_regset_view(current
),
1033 0, sizeof(struct user_regs_struct
),
1036 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1037 return copy_regset_from_user(child
,
1038 task_user_regset_view(current
),
1040 0, sizeof(struct user_regs_struct
),
1043 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1044 return copy_regset_to_user(child
,
1045 task_user_regset_view(current
),
1047 0, sizeof(struct user_i387_struct
),
1050 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1051 return copy_regset_from_user(child
,
1052 task_user_regset_view(current
),
1054 0, sizeof(struct user_i387_struct
),
1057 #ifdef CONFIG_X86_32
1058 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1059 return copy_regset_to_user(child
, &user_x86_32_view
,
1061 0, sizeof(struct user_fxsr_struct
),
1064 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1065 return copy_regset_from_user(child
, &user_x86_32_view
,
1067 0, sizeof(struct user_fxsr_struct
),
1071 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1072 case PTRACE_GET_THREAD_AREA
:
1075 ret
= do_get_thread_area(child
, addr
,
1076 (struct user_desc __user
*) data
);
1079 case PTRACE_SET_THREAD_AREA
:
1082 ret
= do_set_thread_area(child
, addr
,
1083 (struct user_desc __user
*) data
, 0);
1087 #ifdef CONFIG_X86_64
1088 /* normal 64bit interface to access TLS data.
1089 Works just like arch_prctl, except that the arguments
1091 case PTRACE_ARCH_PRCTL
:
1092 ret
= do_arch_prctl(child
, data
, addr
);
1097 * These bits need more cooking - not enabled yet:
1099 #ifdef CONFIG_X86_PTRACE_BTS
1100 case PTRACE_BTS_CONFIG
:
1101 ret
= ptrace_bts_config
1102 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1105 case PTRACE_BTS_STATUS
:
1106 ret
= ptrace_bts_status
1107 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1110 case PTRACE_BTS_SIZE
:
1111 ret
= ptrace_bts_size(child
);
1114 case PTRACE_BTS_GET
:
1115 ret
= ptrace_bts_read_record
1116 (child
, data
, (struct bts_struct __user
*) addr
);
1119 case PTRACE_BTS_CLEAR
:
1120 ret
= ptrace_bts_clear(child
);
1123 case PTRACE_BTS_DRAIN
:
1124 ret
= ptrace_bts_drain
1125 (child
, data
, (struct bts_struct __user
*) addr
);
1127 #endif /* CONFIG_X86_PTRACE_BTS */
1130 ret
= ptrace_request(child
, request
, addr
, data
);
1137 #ifdef CONFIG_IA32_EMULATION
1139 #include <linux/compat.h>
1140 #include <linux/syscalls.h>
1141 #include <asm/ia32.h>
1142 #include <asm/user32.h>
1145 case offsetof(struct user32, regs.l): \
1146 regs->q = value; break
1149 case offsetof(struct user32, regs.rs): \
1150 return set_segment_reg(child, \
1151 offsetof(struct user_regs_struct, rs), \
1155 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
1157 struct pt_regs
*regs
= task_pt_regs(child
);
1178 case offsetof(struct user32
, regs
.orig_eax
):
1180 * Sign-extend the value so that orig_eax = -1
1181 * causes (long)orig_ax < 0 tests to fire correctly.
1183 regs
->orig_ax
= (long) (s32
) value
;
1186 case offsetof(struct user32
, regs
.eflags
):
1187 return set_flags(child
, value
);
1189 case offsetof(struct user32
, u_debugreg
[0]) ...
1190 offsetof(struct user32
, u_debugreg
[7]):
1191 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1192 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1195 if (regno
> sizeof(struct user32
) || (regno
& 3))
1199 * Other dummy fields in the virtual user structure
1211 case offsetof(struct user32, regs.l): \
1212 *val = regs->q; break
1215 case offsetof(struct user32, regs.rs): \
1216 *val = get_segment_reg(child, \
1217 offsetof(struct user_regs_struct, rs)); \
1220 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1222 struct pt_regs
*regs
= task_pt_regs(child
);
1240 R32(orig_eax
, orig_ax
);
1244 case offsetof(struct user32
, regs
.eflags
):
1245 *val
= get_flags(child
);
1248 case offsetof(struct user32
, u_debugreg
[0]) ...
1249 offsetof(struct user32
, u_debugreg
[7]):
1250 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1251 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1255 if (regno
> sizeof(struct user32
) || (regno
& 3))
1259 * Other dummy fields in the virtual user structure
1271 static int genregs32_get(struct task_struct
*target
,
1272 const struct user_regset
*regset
,
1273 unsigned int pos
, unsigned int count
,
1274 void *kbuf
, void __user
*ubuf
)
1277 compat_ulong_t
*k
= kbuf
;
1279 getreg32(target
, pos
, k
++);
1280 count
-= sizeof(*k
);
1284 compat_ulong_t __user
*u
= ubuf
;
1286 compat_ulong_t word
;
1287 getreg32(target
, pos
, &word
);
1288 if (__put_user(word
, u
++))
1290 count
-= sizeof(*u
);
1298 static int genregs32_set(struct task_struct
*target
,
1299 const struct user_regset
*regset
,
1300 unsigned int pos
, unsigned int count
,
1301 const void *kbuf
, const void __user
*ubuf
)
1305 const compat_ulong_t
*k
= kbuf
;
1306 while (count
> 0 && !ret
) {
1307 ret
= putreg32(target
, pos
, *k
++);
1308 count
-= sizeof(*k
);
1312 const compat_ulong_t __user
*u
= ubuf
;
1313 while (count
> 0 && !ret
) {
1314 compat_ulong_t word
;
1315 ret
= __get_user(word
, u
++);
1318 ret
= putreg32(target
, pos
, word
);
1319 count
-= sizeof(*u
);
1326 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1327 compat_ulong_t caddr
, compat_ulong_t cdata
)
1329 unsigned long addr
= caddr
;
1330 unsigned long data
= cdata
;
1331 void __user
*datap
= compat_ptr(data
);
1336 case PTRACE_PEEKUSR
:
1337 ret
= getreg32(child
, addr
, &val
);
1339 ret
= put_user(val
, (__u32 __user
*)datap
);
1342 case PTRACE_POKEUSR
:
1343 ret
= putreg32(child
, addr
, data
);
1346 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1347 return copy_regset_to_user(child
, &user_x86_32_view
,
1349 0, sizeof(struct user_regs_struct32
),
1352 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1353 return copy_regset_from_user(child
, &user_x86_32_view
,
1355 sizeof(struct user_regs_struct32
),
1358 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1359 return copy_regset_to_user(child
, &user_x86_32_view
,
1361 sizeof(struct user_i387_ia32_struct
),
1364 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1365 return copy_regset_from_user(
1366 child
, &user_x86_32_view
, REGSET_FP
,
1367 0, sizeof(struct user_i387_ia32_struct
), datap
);
1369 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1370 return copy_regset_to_user(child
, &user_x86_32_view
,
1372 sizeof(struct user32_fxsr_struct
),
1375 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1376 return copy_regset_from_user(child
, &user_x86_32_view
,
1378 sizeof(struct user32_fxsr_struct
),
1381 case PTRACE_GET_THREAD_AREA
:
1382 case PTRACE_SET_THREAD_AREA
:
1383 #ifdef CONFIG_X86_PTRACE_BTS
1384 case PTRACE_BTS_CONFIG
:
1385 case PTRACE_BTS_STATUS
:
1386 case PTRACE_BTS_SIZE
:
1387 case PTRACE_BTS_GET
:
1388 case PTRACE_BTS_CLEAR
:
1389 case PTRACE_BTS_DRAIN
:
1390 #endif /* CONFIG_X86_PTRACE_BTS */
1391 return arch_ptrace(child
, request
, addr
, data
);
1394 return compat_ptrace_request(child
, request
, addr
, data
);
1400 #endif /* CONFIG_IA32_EMULATION */
1402 #ifdef CONFIG_X86_64
1404 static const struct user_regset x86_64_regsets
[] = {
1405 [REGSET_GENERAL
] = {
1406 .core_note_type
= NT_PRSTATUS
,
1407 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1408 .size
= sizeof(long), .align
= sizeof(long),
1409 .get
= genregs_get
, .set
= genregs_set
1412 .core_note_type
= NT_PRFPREG
,
1413 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1414 .size
= sizeof(long), .align
= sizeof(long),
1415 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1417 [REGSET_IOPERM64
] = {
1418 .core_note_type
= NT_386_IOPERM
,
1419 .n
= IO_BITMAP_LONGS
,
1420 .size
= sizeof(long), .align
= sizeof(long),
1421 .active
= ioperm_active
, .get
= ioperm_get
1425 static const struct user_regset_view user_x86_64_view
= {
1426 .name
= "x86_64", .e_machine
= EM_X86_64
,
1427 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1430 #else /* CONFIG_X86_32 */
1432 #define user_regs_struct32 user_regs_struct
1433 #define genregs32_get genregs_get
1434 #define genregs32_set genregs_set
1436 #define user_i387_ia32_struct user_i387_struct
1437 #define user32_fxsr_struct user_fxsr_struct
1439 #endif /* CONFIG_X86_64 */
1441 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1442 static const struct user_regset x86_32_regsets
[] = {
1443 [REGSET_GENERAL
] = {
1444 .core_note_type
= NT_PRSTATUS
,
1445 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1446 .size
= sizeof(u32
), .align
= sizeof(u32
),
1447 .get
= genregs32_get
, .set
= genregs32_set
1450 .core_note_type
= NT_PRFPREG
,
1451 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1452 .size
= sizeof(u32
), .align
= sizeof(u32
),
1453 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1456 .core_note_type
= NT_PRXFPREG
,
1457 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1458 .size
= sizeof(u32
), .align
= sizeof(u32
),
1459 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1462 .core_note_type
= NT_386_TLS
,
1463 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1464 .size
= sizeof(struct user_desc
),
1465 .align
= sizeof(struct user_desc
),
1466 .active
= regset_tls_active
,
1467 .get
= regset_tls_get
, .set
= regset_tls_set
1469 [REGSET_IOPERM32
] = {
1470 .core_note_type
= NT_386_IOPERM
,
1471 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1472 .size
= sizeof(u32
), .align
= sizeof(u32
),
1473 .active
= ioperm_active
, .get
= ioperm_get
1477 static const struct user_regset_view user_x86_32_view
= {
1478 .name
= "i386", .e_machine
= EM_386
,
1479 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1483 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1485 #ifdef CONFIG_IA32_EMULATION
1486 if (test_tsk_thread_flag(task
, TIF_IA32
))
1488 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1489 return &user_x86_32_view
;
1491 #ifdef CONFIG_X86_64
1492 return &user_x86_64_view
;
1496 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1497 int error_code
, int si_code
)
1499 struct siginfo info
;
1501 tsk
->thread
.trap_no
= 1;
1502 tsk
->thread
.error_code
= error_code
;
1504 memset(&info
, 0, sizeof(info
));
1505 info
.si_signo
= SIGTRAP
;
1506 info
.si_code
= si_code
;
1509 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1511 /* Send us the fake SIGTRAP */
1512 force_sig_info(SIGTRAP
, &info
, tsk
);
1516 #ifdef CONFIG_X86_32
1518 #elif defined CONFIG_IA32_EMULATION
1519 # define IS_IA32 is_compat_task()
1525 * We must return the syscall number to actually look up in the table.
1526 * This can be -1L to skip running any syscall at all.
1528 asmregparm
long syscall_trace_enter(struct pt_regs
*regs
)
1533 * If we stepped into a sysenter/syscall insn, it trapped in
1534 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1535 * If user-mode had set TF itself, then it's still clear from
1536 * do_debug() and we need to set it again to restore the user
1537 * state. If we entered on the slow path, TF was already set.
1539 if (test_thread_flag(TIF_SINGLESTEP
))
1540 regs
->flags
|= X86_EFLAGS_TF
;
1542 /* do the secure computing check first */
1543 secure_computing(regs
->orig_ax
);
1545 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1548 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1549 tracehook_report_syscall_entry(regs
))
1552 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1553 trace_sys_enter(regs
, regs
->orig_ax
);
1555 if (unlikely(current
->audit_context
)) {
1557 audit_syscall_entry(AUDIT_ARCH_I386
,
1560 regs
->dx
, regs
->si
);
1561 #ifdef CONFIG_X86_64
1563 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1566 regs
->dx
, regs
->r10
);
1570 return ret
?: regs
->orig_ax
;
1573 asmregparm
void syscall_trace_leave(struct pt_regs
*regs
)
1575 if (unlikely(current
->audit_context
))
1576 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1578 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1579 trace_sys_exit(regs
, regs
->ax
);
1581 if (test_thread_flag(TIF_SYSCALL_TRACE
))
1582 tracehook_report_syscall_exit(regs
, 0);
1585 * If TIF_SYSCALL_EMU is set, we only get here because of
1586 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1587 * We already reported this syscall instruction in
1588 * syscall_trace_enter(), so don't do any more now.
1590 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1594 * If we are single-stepping, synthesize a trap to follow the
1595 * system call instruction.
1597 if (test_thread_flag(TIF_SINGLESTEP
) &&
1598 tracehook_consider_fatal_signal(current
, SIGTRAP
))
1599 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);