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>
40 #define CREATE_TRACE_POINTS
41 #include <trace/events/syscalls.h>
47 REGSET_IOPERM64
= REGSET_XFP
,
53 * does not yet catch signals sent when the child dies.
54 * in exit.c or in signal.c.
58 * Determines which flags the user has access to [1 = access, 0 = no access].
60 #define FLAG_MASK_32 ((unsigned long) \
61 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
62 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
63 X86_EFLAGS_SF | X86_EFLAGS_TF | \
64 X86_EFLAGS_DF | X86_EFLAGS_OF | \
65 X86_EFLAGS_RF | X86_EFLAGS_AC))
68 * Determines whether a value may be installed in a segment register.
70 static inline bool invalid_selector(u16 value
)
72 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
77 #define FLAG_MASK FLAG_MASK_32
79 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
81 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
82 return ®s
->bx
+ (regno
>> 2);
85 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
88 * Returning the value truncates it to 16 bits.
91 if (offset
!= offsetof(struct user_regs_struct
, gs
))
92 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
95 retval
= get_user_gs(task_pt_regs(task
));
97 retval
= task_user_gs(task
);
102 static int set_segment_reg(struct task_struct
*task
,
103 unsigned long offset
, u16 value
)
106 * The value argument was already truncated to 16 bits.
108 if (invalid_selector(value
))
112 * For %cs and %ss we cannot permit a null selector.
113 * We can permit a bogus selector as long as it has USER_RPL.
114 * Null selectors are fine for other segment registers, but
115 * we will never get back to user mode with invalid %cs or %ss
116 * and will take the trap in iret instead. Much code relies
117 * on user_mode() to distinguish a user trap frame (which can
118 * safely use invalid selectors) from a kernel trap frame.
121 case offsetof(struct user_regs_struct
, cs
):
122 case offsetof(struct user_regs_struct
, ss
):
123 if (unlikely(value
== 0))
127 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
130 case offsetof(struct user_regs_struct
, gs
):
132 set_user_gs(task_pt_regs(task
), value
);
134 task_user_gs(task
) = value
;
140 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
142 return TASK_SIZE
- 3;
145 #else /* CONFIG_X86_64 */
147 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
149 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
151 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
152 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
155 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
158 * Returning the value truncates it to 16 bits.
163 case offsetof(struct user_regs_struct
, fs
):
164 if (task
== current
) {
165 /* Older gas can't assemble movq %?s,%r?? */
166 asm("movl %%fs,%0" : "=r" (seg
));
169 return task
->thread
.fsindex
;
170 case offsetof(struct user_regs_struct
, gs
):
171 if (task
== current
) {
172 asm("movl %%gs,%0" : "=r" (seg
));
175 return task
->thread
.gsindex
;
176 case offsetof(struct user_regs_struct
, ds
):
177 if (task
== current
) {
178 asm("movl %%ds,%0" : "=r" (seg
));
181 return task
->thread
.ds
;
182 case offsetof(struct user_regs_struct
, es
):
183 if (task
== current
) {
184 asm("movl %%es,%0" : "=r" (seg
));
187 return task
->thread
.es
;
189 case offsetof(struct user_regs_struct
, cs
):
190 case offsetof(struct user_regs_struct
, ss
):
193 return *pt_regs_access(task_pt_regs(task
), offset
);
196 static int set_segment_reg(struct task_struct
*task
,
197 unsigned long offset
, u16 value
)
200 * The value argument was already truncated to 16 bits.
202 if (invalid_selector(value
))
206 case offsetof(struct user_regs_struct
,fs
):
208 * If this is setting fs as for normal 64-bit use but
209 * setting fs_base has implicitly changed it, leave it.
211 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
212 task
->thread
.fs
!= 0) ||
213 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
214 task
->thread
.fs
== 0))
216 task
->thread
.fsindex
= value
;
218 loadsegment(fs
, task
->thread
.fsindex
);
220 case offsetof(struct user_regs_struct
,gs
):
222 * If this is setting gs as for normal 64-bit use but
223 * setting gs_base has implicitly changed it, leave it.
225 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
226 task
->thread
.gs
!= 0) ||
227 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
228 task
->thread
.gs
== 0))
230 task
->thread
.gsindex
= value
;
232 load_gs_index(task
->thread
.gsindex
);
234 case offsetof(struct user_regs_struct
,ds
):
235 task
->thread
.ds
= value
;
237 loadsegment(ds
, task
->thread
.ds
);
239 case offsetof(struct user_regs_struct
,es
):
240 task
->thread
.es
= value
;
242 loadsegment(es
, task
->thread
.es
);
246 * Can't actually change these in 64-bit mode.
248 case offsetof(struct user_regs_struct
,cs
):
249 if (unlikely(value
== 0))
251 #ifdef CONFIG_IA32_EMULATION
252 if (test_tsk_thread_flag(task
, TIF_IA32
))
253 task_pt_regs(task
)->cs
= value
;
256 case offsetof(struct user_regs_struct
,ss
):
257 if (unlikely(value
== 0))
259 #ifdef CONFIG_IA32_EMULATION
260 if (test_tsk_thread_flag(task
, TIF_IA32
))
261 task_pt_regs(task
)->ss
= value
;
269 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
271 #ifdef CONFIG_IA32_EMULATION
272 if (test_tsk_thread_flag(task
, TIF_IA32
))
273 return IA32_PAGE_OFFSET
- 3;
275 return TASK_SIZE_MAX
- 7;
278 #endif /* CONFIG_X86_32 */
280 static unsigned long get_flags(struct task_struct
*task
)
282 unsigned long retval
= task_pt_regs(task
)->flags
;
285 * If the debugger set TF, hide it from the readout.
287 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
288 retval
&= ~X86_EFLAGS_TF
;
293 static int set_flags(struct task_struct
*task
, unsigned long value
)
295 struct pt_regs
*regs
= task_pt_regs(task
);
298 * If the user value contains TF, mark that
299 * it was not "us" (the debugger) that set it.
300 * If not, make sure it stays set if we had.
302 if (value
& X86_EFLAGS_TF
)
303 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
304 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
305 value
|= X86_EFLAGS_TF
;
307 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
312 static int putreg(struct task_struct
*child
,
313 unsigned long offset
, unsigned long value
)
316 case offsetof(struct user_regs_struct
, cs
):
317 case offsetof(struct user_regs_struct
, ds
):
318 case offsetof(struct user_regs_struct
, es
):
319 case offsetof(struct user_regs_struct
, fs
):
320 case offsetof(struct user_regs_struct
, gs
):
321 case offsetof(struct user_regs_struct
, ss
):
322 return set_segment_reg(child
, offset
, value
);
324 case offsetof(struct user_regs_struct
, flags
):
325 return set_flags(child
, value
);
329 * Orig_ax is really just a flag with small positive and
330 * negative values, so make sure to always sign-extend it
331 * from 32 bits so that it works correctly regardless of
332 * whether we come from a 32-bit environment or not.
334 case offsetof(struct user_regs_struct
, orig_ax
):
335 value
= (long) (s32
) value
;
338 case offsetof(struct user_regs_struct
,fs_base
):
339 if (value
>= TASK_SIZE_OF(child
))
342 * When changing the segment base, use do_arch_prctl
343 * to set either thread.fs or thread.fsindex and the
344 * corresponding GDT slot.
346 if (child
->thread
.fs
!= value
)
347 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
349 case offsetof(struct user_regs_struct
,gs_base
):
351 * Exactly the same here as the %fs handling above.
353 if (value
>= TASK_SIZE_OF(child
))
355 if (child
->thread
.gs
!= value
)
356 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
361 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
365 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
368 case offsetof(struct user_regs_struct
, cs
):
369 case offsetof(struct user_regs_struct
, ds
):
370 case offsetof(struct user_regs_struct
, es
):
371 case offsetof(struct user_regs_struct
, fs
):
372 case offsetof(struct user_regs_struct
, gs
):
373 case offsetof(struct user_regs_struct
, ss
):
374 return get_segment_reg(task
, offset
);
376 case offsetof(struct user_regs_struct
, flags
):
377 return get_flags(task
);
380 case offsetof(struct user_regs_struct
, fs_base
): {
382 * do_arch_prctl may have used a GDT slot instead of
383 * the MSR. To userland, it appears the same either
384 * way, except the %fs segment selector might not be 0.
386 unsigned int seg
= task
->thread
.fsindex
;
387 if (task
->thread
.fs
!= 0)
388 return task
->thread
.fs
;
390 asm("movl %%fs,%0" : "=r" (seg
));
391 if (seg
!= FS_TLS_SEL
)
393 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
395 case offsetof(struct user_regs_struct
, gs_base
): {
397 * Exactly the same here as the %fs handling above.
399 unsigned int seg
= task
->thread
.gsindex
;
400 if (task
->thread
.gs
!= 0)
401 return task
->thread
.gs
;
403 asm("movl %%gs,%0" : "=r" (seg
));
404 if (seg
!= GS_TLS_SEL
)
406 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
411 return *pt_regs_access(task_pt_regs(task
), offset
);
414 static int genregs_get(struct task_struct
*target
,
415 const struct user_regset
*regset
,
416 unsigned int pos
, unsigned int count
,
417 void *kbuf
, void __user
*ubuf
)
420 unsigned long *k
= kbuf
;
422 *k
++ = getreg(target
, pos
);
427 unsigned long __user
*u
= ubuf
;
429 if (__put_user(getreg(target
, pos
), u
++))
439 static int genregs_set(struct task_struct
*target
,
440 const struct user_regset
*regset
,
441 unsigned int pos
, unsigned int count
,
442 const void *kbuf
, const void __user
*ubuf
)
446 const unsigned long *k
= kbuf
;
447 while (count
> 0 && !ret
) {
448 ret
= putreg(target
, pos
, *k
++);
453 const unsigned long __user
*u
= ubuf
;
454 while (count
> 0 && !ret
) {
456 ret
= __get_user(word
, u
++);
459 ret
= putreg(target
, pos
, word
);
468 * This function is trivial and will be inlined by the compiler.
469 * Having it separates the implementation details of debug
470 * registers from the interface details of ptrace.
472 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
475 case 0: return child
->thread
.debugreg0
;
476 case 1: return child
->thread
.debugreg1
;
477 case 2: return child
->thread
.debugreg2
;
478 case 3: return child
->thread
.debugreg3
;
479 case 6: return child
->thread
.debugreg6
;
480 case 7: return child
->thread
.debugreg7
;
485 static int ptrace_set_debugreg(struct task_struct
*child
,
486 int n
, unsigned long data
)
490 if (unlikely(n
== 4 || n
== 5))
493 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
497 case 0: child
->thread
.debugreg0
= data
; break;
498 case 1: child
->thread
.debugreg1
= data
; break;
499 case 2: child
->thread
.debugreg2
= data
; break;
500 case 3: child
->thread
.debugreg3
= data
; break;
503 if ((data
& ~0xffffffffUL
) != 0)
505 child
->thread
.debugreg6
= data
;
510 * Sanity-check data. Take one half-byte at once with
511 * check = (val >> (16 + 4*i)) & 0xf. It contains the
512 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
513 * 2 and 3 are LENi. Given a list of invalid values,
514 * we do mask |= 1 << invalid_value, so that
515 * (mask >> check) & 1 is a correct test for invalid
518 * R/Wi contains the type of the breakpoint /
519 * watchpoint, LENi contains the length of the watched
520 * data in the watchpoint case.
522 * The invalid values are:
523 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
524 * - R/Wi == 0x10 (break on I/O reads or writes), so
526 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
529 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
531 * See the Intel Manual "System Programming Guide",
534 * Note that LENi == 0x10 is defined on x86_64 in long
535 * mode (i.e. even for 32-bit userspace software, but
536 * 64-bit kernel), so the x86_64 mask value is 0x5454.
537 * See the AMD manual no. 24593 (AMD64 System Programming)
540 #define DR7_MASK 0x5f54
542 #define DR7_MASK 0x5554
544 data
&= ~DR_CONTROL_RESERVED
;
545 for (i
= 0; i
< 4; i
++)
546 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
548 child
->thread
.debugreg7
= data
;
550 set_tsk_thread_flag(child
, TIF_DEBUG
);
552 clear_tsk_thread_flag(child
, TIF_DEBUG
);
560 * These access the current or another (stopped) task's io permission
561 * bitmap for debugging or core dump.
563 static int ioperm_active(struct task_struct
*target
,
564 const struct user_regset
*regset
)
566 return target
->thread
.io_bitmap_max
/ regset
->size
;
569 static int ioperm_get(struct task_struct
*target
,
570 const struct user_regset
*regset
,
571 unsigned int pos
, unsigned int count
,
572 void *kbuf
, void __user
*ubuf
)
574 if (!target
->thread
.io_bitmap_ptr
)
577 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
578 target
->thread
.io_bitmap_ptr
,
582 #ifdef CONFIG_X86_PTRACE_BTS
584 * A branch trace store context.
586 * Contexts may only be installed by ptrace_bts_config() and only for
589 * Contexts are destroyed when the tracee is detached from the tracer.
590 * The actual destruction work requires interrupts enabled, so the
591 * work is deferred and will be scheduled during __ptrace_unlink().
593 * Contexts hold an additional task_struct reference on the traced
594 * task, as well as a reference on the tracer's mm.
596 * Ptrace already holds a task_struct for the duration of ptrace operations,
597 * but since destruction is deferred, it may be executed after both
598 * tracer and tracee exited.
601 /* The branch trace handle. */
602 struct bts_tracer
*tracer
;
604 /* The buffer used to store the branch trace and its size. */
608 /* The mm that paid for the above buffer. */
609 struct mm_struct
*mm
;
611 /* The task this context belongs to. */
612 struct task_struct
*task
;
614 /* The signal to send on a bts buffer overflow. */
615 unsigned int bts_ovfl_signal
;
617 /* The work struct to destroy a context. */
618 struct work_struct work
;
621 static int alloc_bts_buffer(struct bts_context
*context
, unsigned int size
)
626 err
= account_locked_memory(current
->mm
, current
->signal
->rlim
, size
);
630 buffer
= kzalloc(size
, GFP_KERNEL
);
634 context
->buffer
= buffer
;
635 context
->size
= size
;
636 context
->mm
= get_task_mm(current
);
641 refund_locked_memory(current
->mm
, size
);
645 static inline void free_bts_buffer(struct bts_context
*context
)
647 if (!context
->buffer
)
650 kfree(context
->buffer
);
651 context
->buffer
= NULL
;
653 refund_locked_memory(context
->mm
, context
->size
);
660 static void free_bts_context_work(struct work_struct
*w
)
662 struct bts_context
*context
;
664 context
= container_of(w
, struct bts_context
, work
);
666 ds_release_bts(context
->tracer
);
667 put_task_struct(context
->task
);
668 free_bts_buffer(context
);
672 static inline void free_bts_context(struct bts_context
*context
)
674 INIT_WORK(&context
->work
, free_bts_context_work
);
675 schedule_work(&context
->work
);
678 static inline struct bts_context
*alloc_bts_context(struct task_struct
*task
)
680 struct bts_context
*context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
682 context
->task
= task
;
685 get_task_struct(task
);
691 static int ptrace_bts_read_record(struct task_struct
*child
, size_t index
,
692 struct bts_struct __user
*out
)
694 struct bts_context
*context
;
695 const struct bts_trace
*trace
;
696 struct bts_struct bts
;
697 const unsigned char *at
;
700 context
= child
->bts
;
704 trace
= ds_read_bts(context
->tracer
);
708 at
= trace
->ds
.top
- ((index
+ 1) * trace
->ds
.size
);
709 if ((void *)at
< trace
->ds
.begin
)
710 at
+= (trace
->ds
.n
* trace
->ds
.size
);
715 error
= trace
->read(context
->tracer
, at
, &bts
);
719 if (copy_to_user(out
, &bts
, sizeof(bts
)))
725 static int ptrace_bts_drain(struct task_struct
*child
,
727 struct bts_struct __user
*out
)
729 struct bts_context
*context
;
730 const struct bts_trace
*trace
;
731 const unsigned char *at
;
732 int error
, drained
= 0;
734 context
= child
->bts
;
738 trace
= ds_read_bts(context
->tracer
);
745 if (size
< (trace
->ds
.top
- trace
->ds
.begin
))
748 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
749 out
++, drained
++, at
+= trace
->ds
.size
) {
750 struct bts_struct bts
;
752 error
= trace
->read(context
->tracer
, at
, &bts
);
756 if (copy_to_user(out
, &bts
, sizeof(bts
)))
760 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
762 error
= ds_reset_bts(context
->tracer
);
769 static int ptrace_bts_config(struct task_struct
*child
,
771 const struct ptrace_bts_config __user
*ucfg
)
773 struct bts_context
*context
;
774 struct ptrace_bts_config cfg
;
775 unsigned int flags
= 0;
777 if (cfg_size
< sizeof(cfg
))
780 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
783 context
= child
->bts
;
785 context
= alloc_bts_context(child
);
789 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
794 context
->bts_ovfl_signal
= cfg
.signal
;
797 ds_release_bts(context
->tracer
);
798 context
->tracer
= NULL
;
800 if ((cfg
.flags
& PTRACE_BTS_O_ALLOC
) && (cfg
.size
!= context
->size
)) {
803 free_bts_buffer(context
);
807 err
= alloc_bts_buffer(context
, cfg
.size
);
812 if (cfg
.flags
& PTRACE_BTS_O_TRACE
)
815 if (cfg
.flags
& PTRACE_BTS_O_SCHED
)
816 flags
|= BTS_TIMESTAMPS
;
819 ds_request_bts_task(child
, context
->buffer
, context
->size
,
820 NULL
, (size_t)-1, flags
);
821 if (unlikely(IS_ERR(context
->tracer
))) {
822 int error
= PTR_ERR(context
->tracer
);
824 free_bts_buffer(context
);
825 context
->tracer
= NULL
;
832 static int ptrace_bts_status(struct task_struct
*child
,
834 struct ptrace_bts_config __user
*ucfg
)
836 struct bts_context
*context
;
837 const struct bts_trace
*trace
;
838 struct ptrace_bts_config cfg
;
840 context
= child
->bts
;
844 if (cfg_size
< sizeof(cfg
))
847 trace
= ds_read_bts(context
->tracer
);
851 memset(&cfg
, 0, sizeof(cfg
));
852 cfg
.size
= trace
->ds
.end
- trace
->ds
.begin
;
853 cfg
.signal
= context
->bts_ovfl_signal
;
854 cfg
.bts_size
= sizeof(struct bts_struct
);
857 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
859 if (trace
->ds
.flags
& BTS_USER
)
860 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
862 if (trace
->ds
.flags
& BTS_TIMESTAMPS
)
863 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
865 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
871 static int ptrace_bts_clear(struct task_struct
*child
)
873 struct bts_context
*context
;
874 const struct bts_trace
*trace
;
876 context
= child
->bts
;
880 trace
= ds_read_bts(context
->tracer
);
884 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
886 return ds_reset_bts(context
->tracer
);
889 static int ptrace_bts_size(struct task_struct
*child
)
891 struct bts_context
*context
;
892 const struct bts_trace
*trace
;
894 context
= child
->bts
;
898 trace
= ds_read_bts(context
->tracer
);
902 return (trace
->ds
.top
- trace
->ds
.begin
) / trace
->ds
.size
;
906 * Called from __ptrace_unlink() after the child has been moved back
907 * to its original parent.
909 void ptrace_bts_untrace(struct task_struct
*child
)
911 if (unlikely(child
->bts
)) {
912 free_bts_context(child
->bts
);
916 #endif /* CONFIG_X86_PTRACE_BTS */
919 * Called by kernel/ptrace.c when detaching..
921 * Make sure the single step bit is not set.
923 void ptrace_disable(struct task_struct
*child
)
925 user_disable_single_step(child
);
926 #ifdef TIF_SYSCALL_EMU
927 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
931 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
932 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
935 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
938 unsigned long __user
*datap
= (unsigned long __user
*)data
;
941 /* read the word at location addr in the USER area. */
942 case PTRACE_PEEKUSR
: {
946 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
947 addr
>= sizeof(struct user
))
950 tmp
= 0; /* Default return condition */
951 if (addr
< sizeof(struct user_regs_struct
))
952 tmp
= getreg(child
, addr
);
953 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
954 addr
<= offsetof(struct user
, u_debugreg
[7])) {
955 addr
-= offsetof(struct user
, u_debugreg
[0]);
956 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
958 ret
= put_user(tmp
, datap
);
962 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
964 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
965 addr
>= sizeof(struct user
))
968 if (addr
< sizeof(struct user_regs_struct
))
969 ret
= putreg(child
, addr
, data
);
970 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
971 addr
<= offsetof(struct user
, u_debugreg
[7])) {
972 addr
-= offsetof(struct user
, u_debugreg
[0]);
973 ret
= ptrace_set_debugreg(child
,
974 addr
/ sizeof(data
), data
);
978 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
979 return copy_regset_to_user(child
,
980 task_user_regset_view(current
),
982 0, sizeof(struct user_regs_struct
),
985 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
986 return copy_regset_from_user(child
,
987 task_user_regset_view(current
),
989 0, sizeof(struct user_regs_struct
),
992 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
993 return copy_regset_to_user(child
,
994 task_user_regset_view(current
),
996 0, sizeof(struct user_i387_struct
),
999 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1000 return copy_regset_from_user(child
,
1001 task_user_regset_view(current
),
1003 0, sizeof(struct user_i387_struct
),
1006 #ifdef CONFIG_X86_32
1007 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1008 return copy_regset_to_user(child
, &user_x86_32_view
,
1010 0, sizeof(struct user_fxsr_struct
),
1013 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1014 return copy_regset_from_user(child
, &user_x86_32_view
,
1016 0, sizeof(struct user_fxsr_struct
),
1020 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1021 case PTRACE_GET_THREAD_AREA
:
1024 ret
= do_get_thread_area(child
, addr
,
1025 (struct user_desc __user
*) data
);
1028 case PTRACE_SET_THREAD_AREA
:
1031 ret
= do_set_thread_area(child
, addr
,
1032 (struct user_desc __user
*) data
, 0);
1036 #ifdef CONFIG_X86_64
1037 /* normal 64bit interface to access TLS data.
1038 Works just like arch_prctl, except that the arguments
1040 case PTRACE_ARCH_PRCTL
:
1041 ret
= do_arch_prctl(child
, data
, addr
);
1046 * These bits need more cooking - not enabled yet:
1048 #ifdef CONFIG_X86_PTRACE_BTS
1049 case PTRACE_BTS_CONFIG
:
1050 ret
= ptrace_bts_config
1051 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1054 case PTRACE_BTS_STATUS
:
1055 ret
= ptrace_bts_status
1056 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
1059 case PTRACE_BTS_SIZE
:
1060 ret
= ptrace_bts_size(child
);
1063 case PTRACE_BTS_GET
:
1064 ret
= ptrace_bts_read_record
1065 (child
, data
, (struct bts_struct __user
*) addr
);
1068 case PTRACE_BTS_CLEAR
:
1069 ret
= ptrace_bts_clear(child
);
1072 case PTRACE_BTS_DRAIN
:
1073 ret
= ptrace_bts_drain
1074 (child
, data
, (struct bts_struct __user
*) addr
);
1076 #endif /* CONFIG_X86_PTRACE_BTS */
1079 ret
= ptrace_request(child
, request
, addr
, data
);
1086 #ifdef CONFIG_IA32_EMULATION
1088 #include <linux/compat.h>
1089 #include <linux/syscalls.h>
1090 #include <asm/ia32.h>
1091 #include <asm/user32.h>
1094 case offsetof(struct user32, regs.l): \
1095 regs->q = value; break
1098 case offsetof(struct user32, regs.rs): \
1099 return set_segment_reg(child, \
1100 offsetof(struct user_regs_struct, rs), \
1104 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
1106 struct pt_regs
*regs
= task_pt_regs(child
);
1127 case offsetof(struct user32
, regs
.orig_eax
):
1129 * Sign-extend the value so that orig_eax = -1
1130 * causes (long)orig_ax < 0 tests to fire correctly.
1132 regs
->orig_ax
= (long) (s32
) value
;
1135 case offsetof(struct user32
, regs
.eflags
):
1136 return set_flags(child
, value
);
1138 case offsetof(struct user32
, u_debugreg
[0]) ...
1139 offsetof(struct user32
, u_debugreg
[7]):
1140 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1141 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1144 if (regno
> sizeof(struct user32
) || (regno
& 3))
1148 * Other dummy fields in the virtual user structure
1160 case offsetof(struct user32, regs.l): \
1161 *val = regs->q; break
1164 case offsetof(struct user32, regs.rs): \
1165 *val = get_segment_reg(child, \
1166 offsetof(struct user_regs_struct, rs)); \
1169 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1171 struct pt_regs
*regs
= task_pt_regs(child
);
1189 R32(orig_eax
, orig_ax
);
1193 case offsetof(struct user32
, regs
.eflags
):
1194 *val
= get_flags(child
);
1197 case offsetof(struct user32
, u_debugreg
[0]) ...
1198 offsetof(struct user32
, u_debugreg
[7]):
1199 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1200 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1204 if (regno
> sizeof(struct user32
) || (regno
& 3))
1208 * Other dummy fields in the virtual user structure
1220 static int genregs32_get(struct task_struct
*target
,
1221 const struct user_regset
*regset
,
1222 unsigned int pos
, unsigned int count
,
1223 void *kbuf
, void __user
*ubuf
)
1226 compat_ulong_t
*k
= kbuf
;
1228 getreg32(target
, pos
, k
++);
1229 count
-= sizeof(*k
);
1233 compat_ulong_t __user
*u
= ubuf
;
1235 compat_ulong_t word
;
1236 getreg32(target
, pos
, &word
);
1237 if (__put_user(word
, u
++))
1239 count
-= sizeof(*u
);
1247 static int genregs32_set(struct task_struct
*target
,
1248 const struct user_regset
*regset
,
1249 unsigned int pos
, unsigned int count
,
1250 const void *kbuf
, const void __user
*ubuf
)
1254 const compat_ulong_t
*k
= kbuf
;
1255 while (count
> 0 && !ret
) {
1256 ret
= putreg32(target
, pos
, *k
++);
1257 count
-= sizeof(*k
);
1261 const compat_ulong_t __user
*u
= ubuf
;
1262 while (count
> 0 && !ret
) {
1263 compat_ulong_t word
;
1264 ret
= __get_user(word
, u
++);
1267 ret
= putreg32(target
, pos
, word
);
1268 count
-= sizeof(*u
);
1275 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1276 compat_ulong_t caddr
, compat_ulong_t cdata
)
1278 unsigned long addr
= caddr
;
1279 unsigned long data
= cdata
;
1280 void __user
*datap
= compat_ptr(data
);
1285 case PTRACE_PEEKUSR
:
1286 ret
= getreg32(child
, addr
, &val
);
1288 ret
= put_user(val
, (__u32 __user
*)datap
);
1291 case PTRACE_POKEUSR
:
1292 ret
= putreg32(child
, addr
, data
);
1295 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1296 return copy_regset_to_user(child
, &user_x86_32_view
,
1298 0, sizeof(struct user_regs_struct32
),
1301 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1302 return copy_regset_from_user(child
, &user_x86_32_view
,
1304 sizeof(struct user_regs_struct32
),
1307 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1308 return copy_regset_to_user(child
, &user_x86_32_view
,
1310 sizeof(struct user_i387_ia32_struct
),
1313 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1314 return copy_regset_from_user(
1315 child
, &user_x86_32_view
, REGSET_FP
,
1316 0, sizeof(struct user_i387_ia32_struct
), datap
);
1318 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1319 return copy_regset_to_user(child
, &user_x86_32_view
,
1321 sizeof(struct user32_fxsr_struct
),
1324 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1325 return copy_regset_from_user(child
, &user_x86_32_view
,
1327 sizeof(struct user32_fxsr_struct
),
1330 case PTRACE_GET_THREAD_AREA
:
1331 case PTRACE_SET_THREAD_AREA
:
1332 #ifdef CONFIG_X86_PTRACE_BTS
1333 case PTRACE_BTS_CONFIG
:
1334 case PTRACE_BTS_STATUS
:
1335 case PTRACE_BTS_SIZE
:
1336 case PTRACE_BTS_GET
:
1337 case PTRACE_BTS_CLEAR
:
1338 case PTRACE_BTS_DRAIN
:
1339 #endif /* CONFIG_X86_PTRACE_BTS */
1340 return arch_ptrace(child
, request
, addr
, data
);
1343 return compat_ptrace_request(child
, request
, addr
, data
);
1349 #endif /* CONFIG_IA32_EMULATION */
1351 #ifdef CONFIG_X86_64
1353 static const struct user_regset x86_64_regsets
[] = {
1354 [REGSET_GENERAL
] = {
1355 .core_note_type
= NT_PRSTATUS
,
1356 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1357 .size
= sizeof(long), .align
= sizeof(long),
1358 .get
= genregs_get
, .set
= genregs_set
1361 .core_note_type
= NT_PRFPREG
,
1362 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1363 .size
= sizeof(long), .align
= sizeof(long),
1364 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1366 [REGSET_IOPERM64
] = {
1367 .core_note_type
= NT_386_IOPERM
,
1368 .n
= IO_BITMAP_LONGS
,
1369 .size
= sizeof(long), .align
= sizeof(long),
1370 .active
= ioperm_active
, .get
= ioperm_get
1374 static const struct user_regset_view user_x86_64_view
= {
1375 .name
= "x86_64", .e_machine
= EM_X86_64
,
1376 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1379 #else /* CONFIG_X86_32 */
1381 #define user_regs_struct32 user_regs_struct
1382 #define genregs32_get genregs_get
1383 #define genregs32_set genregs_set
1385 #define user_i387_ia32_struct user_i387_struct
1386 #define user32_fxsr_struct user_fxsr_struct
1388 #endif /* CONFIG_X86_64 */
1390 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1391 static const struct user_regset x86_32_regsets
[] = {
1392 [REGSET_GENERAL
] = {
1393 .core_note_type
= NT_PRSTATUS
,
1394 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1395 .size
= sizeof(u32
), .align
= sizeof(u32
),
1396 .get
= genregs32_get
, .set
= genregs32_set
1399 .core_note_type
= NT_PRFPREG
,
1400 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1401 .size
= sizeof(u32
), .align
= sizeof(u32
),
1402 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1405 .core_note_type
= NT_PRXFPREG
,
1406 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1407 .size
= sizeof(u32
), .align
= sizeof(u32
),
1408 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1411 .core_note_type
= NT_386_TLS
,
1412 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1413 .size
= sizeof(struct user_desc
),
1414 .align
= sizeof(struct user_desc
),
1415 .active
= regset_tls_active
,
1416 .get
= regset_tls_get
, .set
= regset_tls_set
1418 [REGSET_IOPERM32
] = {
1419 .core_note_type
= NT_386_IOPERM
,
1420 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1421 .size
= sizeof(u32
), .align
= sizeof(u32
),
1422 .active
= ioperm_active
, .get
= ioperm_get
1426 static const struct user_regset_view user_x86_32_view
= {
1427 .name
= "i386", .e_machine
= EM_386
,
1428 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1432 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1434 #ifdef CONFIG_IA32_EMULATION
1435 if (test_tsk_thread_flag(task
, TIF_IA32
))
1437 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1438 return &user_x86_32_view
;
1440 #ifdef CONFIG_X86_64
1441 return &user_x86_64_view
;
1445 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1446 int error_code
, int si_code
)
1448 struct siginfo info
;
1450 tsk
->thread
.trap_no
= 1;
1451 tsk
->thread
.error_code
= error_code
;
1453 memset(&info
, 0, sizeof(info
));
1454 info
.si_signo
= SIGTRAP
;
1455 info
.si_code
= si_code
;
1458 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1460 /* Send us the fake SIGTRAP */
1461 force_sig_info(SIGTRAP
, &info
, tsk
);
1465 #ifdef CONFIG_X86_32
1467 #elif defined CONFIG_IA32_EMULATION
1468 # define IS_IA32 is_compat_task()
1474 * We must return the syscall number to actually look up in the table.
1475 * This can be -1L to skip running any syscall at all.
1477 asmregparm
long syscall_trace_enter(struct pt_regs
*regs
)
1482 * If we stepped into a sysenter/syscall insn, it trapped in
1483 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1484 * If user-mode had set TF itself, then it's still clear from
1485 * do_debug() and we need to set it again to restore the user
1486 * state. If we entered on the slow path, TF was already set.
1488 if (test_thread_flag(TIF_SINGLESTEP
))
1489 regs
->flags
|= X86_EFLAGS_TF
;
1491 /* do the secure computing check first */
1492 secure_computing(regs
->orig_ax
);
1494 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1497 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1498 tracehook_report_syscall_entry(regs
))
1501 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1502 trace_sys_enter(regs
, regs
->orig_ax
);
1504 if (unlikely(current
->audit_context
)) {
1506 audit_syscall_entry(AUDIT_ARCH_I386
,
1509 regs
->dx
, regs
->si
);
1510 #ifdef CONFIG_X86_64
1512 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1515 regs
->dx
, regs
->r10
);
1519 return ret
?: regs
->orig_ax
;
1522 asmregparm
void syscall_trace_leave(struct pt_regs
*regs
)
1524 if (unlikely(current
->audit_context
))
1525 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1527 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1528 trace_sys_exit(regs
, regs
->ax
);
1530 if (test_thread_flag(TIF_SYSCALL_TRACE
))
1531 tracehook_report_syscall_exit(regs
, 0);
1534 * If TIF_SYSCALL_EMU is set, we only get here because of
1535 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1536 * We already reported this syscall instruction in
1537 * syscall_trace_enter(), so don't do any more now.
1539 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1543 * If we are single-stepping, synthesize a trap to follow the
1544 * system call instruction.
1546 if (test_thread_flag(TIF_SINGLESTEP
) &&
1547 tracehook_consider_fatal_signal(current
, SIGTRAP
))
1548 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);