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/user.h>
17 #include <linux/security.h>
18 #include <linux/audit.h>
19 #include <linux/seccomp.h>
20 #include <linux/signal.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
27 #include <asm/debugreg.h>
30 #include <asm/prctl.h>
31 #include <asm/proto.h>
36 * does not yet catch signals sent when the child dies.
37 * in exit.c or in signal.c.
41 * Determines which flags the user has access to [1 = access, 0 = no access].
43 #define FLAG_MASK_32 ((unsigned long) \
44 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
45 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
46 X86_EFLAGS_SF | X86_EFLAGS_TF | \
47 X86_EFLAGS_DF | X86_EFLAGS_OF | \
48 X86_EFLAGS_RF | X86_EFLAGS_AC))
51 * Determines whether a value may be installed in a segment register.
53 static inline bool invalid_selector(u16 value
)
55 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
60 #define FLAG_MASK FLAG_MASK_32
62 static long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
64 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
68 return ®s
->bx
+ regno
;
71 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
74 * Returning the value truncates it to 16 bits.
77 if (offset
!= offsetof(struct user_regs_struct
, gs
))
78 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
80 retval
= task
->thread
.gs
;
82 savesegment(gs
, retval
);
87 static int set_segment_reg(struct task_struct
*task
,
88 unsigned long offset
, u16 value
)
91 * The value argument was already truncated to 16 bits.
93 if (invalid_selector(value
))
96 if (offset
!= offsetof(struct user_regs_struct
, gs
))
97 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
99 task
->thread
.gs
= value
;
102 * The user-mode %gs is not affected by
103 * kernel entry, so we must update the CPU.
105 loadsegment(gs
, value
);
111 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
113 return TASK_SIZE
- 3;
116 #else /* CONFIG_X86_64 */
118 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
120 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
122 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
123 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
126 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
129 * Returning the value truncates it to 16 bits.
134 case offsetof(struct user_regs_struct
, fs
):
135 if (task
== current
) {
136 /* Older gas can't assemble movq %?s,%r?? */
137 asm("movl %%fs,%0" : "=r" (seg
));
140 return task
->thread
.fsindex
;
141 case offsetof(struct user_regs_struct
, gs
):
142 if (task
== current
) {
143 asm("movl %%gs,%0" : "=r" (seg
));
146 return task
->thread
.gsindex
;
147 case offsetof(struct user_regs_struct
, ds
):
148 if (task
== current
) {
149 asm("movl %%ds,%0" : "=r" (seg
));
152 return task
->thread
.ds
;
153 case offsetof(struct user_regs_struct
, es
):
154 if (task
== current
) {
155 asm("movl %%es,%0" : "=r" (seg
));
158 return task
->thread
.es
;
160 case offsetof(struct user_regs_struct
, cs
):
161 case offsetof(struct user_regs_struct
, ss
):
164 return *pt_regs_access(task_pt_regs(task
), offset
);
167 static int set_segment_reg(struct task_struct
*task
,
168 unsigned long offset
, u16 value
)
171 * The value argument was already truncated to 16 bits.
173 if (invalid_selector(value
))
177 case offsetof(struct user_regs_struct
,fs
):
179 * If this is setting fs as for normal 64-bit use but
180 * setting fs_base has implicitly changed it, leave it.
182 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
183 task
->thread
.fs
!= 0) ||
184 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
185 task
->thread
.fs
== 0))
187 task
->thread
.fsindex
= value
;
189 loadsegment(fs
, task
->thread
.fsindex
);
191 case offsetof(struct user_regs_struct
,gs
):
193 * If this is setting gs as for normal 64-bit use but
194 * setting gs_base has implicitly changed it, leave it.
196 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
197 task
->thread
.gs
!= 0) ||
198 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
199 task
->thread
.gs
== 0))
201 task
->thread
.gsindex
= value
;
203 load_gs_index(task
->thread
.gsindex
);
205 case offsetof(struct user_regs_struct
,ds
):
206 task
->thread
.ds
= value
;
208 loadsegment(ds
, task
->thread
.ds
);
210 case offsetof(struct user_regs_struct
,es
):
211 task
->thread
.es
= value
;
213 loadsegment(es
, task
->thread
.es
);
217 * Can't actually change these in 64-bit mode.
219 case offsetof(struct user_regs_struct
,cs
):
220 #ifdef CONFIG_IA32_EMULATION
221 if (test_tsk_thread_flag(task
, TIF_IA32
))
222 task_pt_regs(task
)->cs
= value
;
225 case offsetof(struct user_regs_struct
,ss
):
226 #ifdef CONFIG_IA32_EMULATION
227 if (test_tsk_thread_flag(task
, TIF_IA32
))
228 task_pt_regs(task
)->ss
= value
;
236 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
238 #ifdef CONFIG_IA32_EMULATION
239 if (test_tsk_thread_flag(task
, TIF_IA32
))
240 return IA32_PAGE_OFFSET
- 3;
242 return TASK_SIZE64
- 7;
245 #endif /* CONFIG_X86_32 */
247 static unsigned long get_flags(struct task_struct
*task
)
249 unsigned long retval
= task_pt_regs(task
)->flags
;
252 * If the debugger set TF, hide it from the readout.
254 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
255 retval
&= ~X86_EFLAGS_TF
;
260 static int set_flags(struct task_struct
*task
, unsigned long value
)
262 struct pt_regs
*regs
= task_pt_regs(task
);
265 * If the user value contains TF, mark that
266 * it was not "us" (the debugger) that set it.
267 * If not, make sure it stays set if we had.
269 if (value
& X86_EFLAGS_TF
)
270 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
271 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
272 value
|= X86_EFLAGS_TF
;
274 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
279 static int putreg(struct task_struct
*child
,
280 unsigned long offset
, unsigned long value
)
283 case offsetof(struct user_regs_struct
, cs
):
284 case offsetof(struct user_regs_struct
, ds
):
285 case offsetof(struct user_regs_struct
, es
):
286 case offsetof(struct user_regs_struct
, fs
):
287 case offsetof(struct user_regs_struct
, gs
):
288 case offsetof(struct user_regs_struct
, ss
):
289 return set_segment_reg(child
, offset
, value
);
291 case offsetof(struct user_regs_struct
, flags
):
292 return set_flags(child
, value
);
295 case offsetof(struct user_regs_struct
,fs_base
):
296 if (value
>= TASK_SIZE_OF(child
))
299 * When changing the segment base, use do_arch_prctl
300 * to set either thread.fs or thread.fsindex and the
301 * corresponding GDT slot.
303 if (child
->thread
.fs
!= value
)
304 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
306 case offsetof(struct user_regs_struct
,gs_base
):
308 * Exactly the same here as the %fs handling above.
310 if (value
>= TASK_SIZE_OF(child
))
312 if (child
->thread
.gs
!= value
)
313 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
318 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
322 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
325 case offsetof(struct user_regs_struct
, cs
):
326 case offsetof(struct user_regs_struct
, ds
):
327 case offsetof(struct user_regs_struct
, es
):
328 case offsetof(struct user_regs_struct
, fs
):
329 case offsetof(struct user_regs_struct
, gs
):
330 case offsetof(struct user_regs_struct
, ss
):
331 return get_segment_reg(task
, offset
);
333 case offsetof(struct user_regs_struct
, flags
):
334 return get_flags(task
);
337 case offsetof(struct user_regs_struct
, fs_base
): {
339 * do_arch_prctl may have used a GDT slot instead of
340 * the MSR. To userland, it appears the same either
341 * way, except the %fs segment selector might not be 0.
343 unsigned int seg
= task
->thread
.fsindex
;
344 if (task
->thread
.fs
!= 0)
345 return task
->thread
.fs
;
347 asm("movl %%fs,%0" : "=r" (seg
));
348 if (seg
!= FS_TLS_SEL
)
350 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
352 case offsetof(struct user_regs_struct
, gs_base
): {
354 * Exactly the same here as the %fs handling above.
356 unsigned int seg
= task
->thread
.gsindex
;
357 if (task
->thread
.gs
!= 0)
358 return task
->thread
.gs
;
360 asm("movl %%gs,%0" : "=r" (seg
));
361 if (seg
!= GS_TLS_SEL
)
363 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
368 return *pt_regs_access(task_pt_regs(task
), offset
);
372 * This function is trivial and will be inlined by the compiler.
373 * Having it separates the implementation details of debug
374 * registers from the interface details of ptrace.
376 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
379 case 0: return child
->thread
.debugreg0
;
380 case 1: return child
->thread
.debugreg1
;
381 case 2: return child
->thread
.debugreg2
;
382 case 3: return child
->thread
.debugreg3
;
383 case 6: return child
->thread
.debugreg6
;
384 case 7: return child
->thread
.debugreg7
;
389 static int ptrace_set_debugreg(struct task_struct
*child
,
390 int n
, unsigned long data
)
394 if (unlikely(n
== 4 || n
== 5))
397 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
401 case 0: child
->thread
.debugreg0
= data
; break;
402 case 1: child
->thread
.debugreg1
= data
; break;
403 case 2: child
->thread
.debugreg2
= data
; break;
404 case 3: child
->thread
.debugreg3
= data
; break;
407 if ((data
& ~0xffffffffUL
) != 0)
409 child
->thread
.debugreg6
= data
;
414 * Sanity-check data. Take one half-byte at once with
415 * check = (val >> (16 + 4*i)) & 0xf. It contains the
416 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
417 * 2 and 3 are LENi. Given a list of invalid values,
418 * we do mask |= 1 << invalid_value, so that
419 * (mask >> check) & 1 is a correct test for invalid
422 * R/Wi contains the type of the breakpoint /
423 * watchpoint, LENi contains the length of the watched
424 * data in the watchpoint case.
426 * The invalid values are:
427 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
428 * - R/Wi == 0x10 (break on I/O reads or writes), so
430 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
433 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
435 * See the Intel Manual "System Programming Guide",
438 * Note that LENi == 0x10 is defined on x86_64 in long
439 * mode (i.e. even for 32-bit userspace software, but
440 * 64-bit kernel), so the x86_64 mask value is 0x5454.
441 * See the AMD manual no. 24593 (AMD64 System Programming)
444 #define DR7_MASK 0x5f54
446 #define DR7_MASK 0x5554
448 data
&= ~DR_CONTROL_RESERVED
;
449 for (i
= 0; i
< 4; i
++)
450 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
452 child
->thread
.debugreg7
= data
;
454 set_tsk_thread_flag(child
, TIF_DEBUG
);
456 clear_tsk_thread_flag(child
, TIF_DEBUG
);
463 static int ptrace_bts_get_size(struct task_struct
*child
)
465 if (!child
->thread
.ds_area_msr
)
468 return ds_get_bts_index((void *)child
->thread
.ds_area_msr
);
471 static int ptrace_bts_read_record(struct task_struct
*child
,
473 struct bts_struct __user
*out
)
475 struct bts_struct ret
;
480 if (!child
->thread
.ds_area_msr
)
486 bts_end
= ds_get_bts_end((void *)child
->thread
.ds_area_msr
);
487 if (bts_end
<= index
)
490 /* translate the ptrace bts index into the ds bts index */
491 bts_index
= ds_get_bts_index((void *)child
->thread
.ds_area_msr
);
492 bts_index
-= (index
+ 1);
494 bts_index
+= bts_end
;
496 retval
= ds_read_bts((void *)child
->thread
.ds_area_msr
,
501 if (copy_to_user(out
, &ret
, sizeof(ret
)))
507 static int ptrace_bts_write_record(struct task_struct
*child
,
508 const struct bts_struct
*in
)
512 if (!child
->thread
.ds_area_msr
)
515 retval
= ds_write_bts((void *)child
->thread
.ds_area_msr
, in
);
522 static int ptrace_bts_clear(struct task_struct
*child
)
524 if (!child
->thread
.ds_area_msr
)
527 return ds_clear((void *)child
->thread
.ds_area_msr
);
530 static int ptrace_bts_drain(struct task_struct
*child
,
531 struct bts_struct __user
*out
)
534 void *ds
= (void *)child
->thread
.ds_area_msr
;
539 end
= ds_get_bts_index(ds
);
543 for (i
= 0; i
< end
; i
++, out
++) {
544 struct bts_struct ret
;
547 retval
= ds_read_bts(ds
, i
, &ret
);
551 if (copy_to_user(out
, &ret
, sizeof(ret
)))
560 static int ptrace_bts_config(struct task_struct
*child
,
561 const struct ptrace_bts_config __user
*ucfg
)
563 struct ptrace_bts_config cfg
;
564 unsigned long debugctl_mask
;
568 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
572 ds
= (void *)child
->thread
.ds_area_msr
;
574 bts_size
= ds_get_bts_size(ds
);
579 if (bts_size
!= cfg
.size
) {
580 ret
= ds_free((void **)&child
->thread
.ds_area_msr
);
585 ret
= ds_allocate((void **)&child
->thread
.ds_area_msr
,
587 ds
= (void *)child
->thread
.ds_area_msr
;
589 set_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
591 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
596 bts_size
= ds_get_bts_size(ds
);
602 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
603 ret
= ds_set_overflow(ds
, DS_O_SIGNAL
);
605 ret
= ds_set_overflow(ds
, DS_O_WRAP
);
611 debugctl_mask
= ds_debugctl_mask();
612 if (ds
&& (cfg
.flags
& PTRACE_BTS_O_TRACE
)) {
613 child
->thread
.debugctlmsr
|= debugctl_mask
;
614 set_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
616 /* there is no way for us to check whether we 'own'
617 * the respective bits in the DEBUGCTL MSR, we're
619 child
->thread
.debugctlmsr
&= ~debugctl_mask
;
621 if (!child
->thread
.debugctlmsr
)
622 clear_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
625 if (ds
&& (cfg
.flags
& PTRACE_BTS_O_SCHED
))
626 set_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
628 clear_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
633 static int ptrace_bts_status(struct task_struct
*child
,
634 struct ptrace_bts_config __user
*ucfg
)
636 void *ds
= (void *)child
->thread
.ds_area_msr
;
637 struct ptrace_bts_config cfg
;
639 memset(&cfg
, 0, sizeof(cfg
));
642 cfg
.size
= ds_get_bts_size(ds
);
644 if (ds_get_overflow(ds
) == DS_O_SIGNAL
)
645 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
647 if (test_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
) &&
648 child
->thread
.debugctlmsr
& ds_debugctl_mask())
649 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
651 if (test_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
))
652 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
655 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
661 void ptrace_bts_take_timestamp(struct task_struct
*tsk
,
662 enum bts_qualifier qualifier
)
664 struct bts_struct rec
= {
665 .qualifier
= qualifier
,
666 .variant
.jiffies
= jiffies
669 ptrace_bts_write_record(tsk
, &rec
);
673 * Called by kernel/ptrace.c when detaching..
675 * Make sure the single step bit is not set.
677 void ptrace_disable(struct task_struct
*child
)
679 user_disable_single_step(child
);
680 #ifdef TIF_SYSCALL_EMU
681 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
683 ptrace_bts_config(child
, /* options = */ 0);
684 if (child
->thread
.ds_area_msr
) {
685 ds_free((void **)&child
->thread
.ds_area_msr
);
686 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
690 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
693 unsigned long __user
*datap
= (unsigned long __user
*)data
;
696 /* when I and D space are separate, these will need to be fixed. */
697 case PTRACE_PEEKTEXT
: /* read word at location addr. */
698 case PTRACE_PEEKDATA
:
699 ret
= generic_ptrace_peekdata(child
, addr
, data
);
702 /* read the word at location addr in the USER area. */
703 case PTRACE_PEEKUSR
: {
707 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
708 addr
>= sizeof(struct user
))
711 tmp
= 0; /* Default return condition */
712 if (addr
< sizeof(struct user_regs_struct
))
713 tmp
= getreg(child
, addr
);
714 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
715 addr
<= offsetof(struct user
, u_debugreg
[7])) {
716 addr
-= offsetof(struct user
, u_debugreg
[0]);
717 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
719 ret
= put_user(tmp
, datap
);
723 /* when I and D space are separate, this will have to be fixed. */
724 case PTRACE_POKETEXT
: /* write the word at location addr. */
725 case PTRACE_POKEDATA
:
726 ret
= generic_ptrace_pokedata(child
, addr
, data
);
729 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
731 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
732 addr
>= sizeof(struct user
))
735 if (addr
< sizeof(struct user_regs_struct
))
736 ret
= putreg(child
, addr
, data
);
737 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
738 addr
<= offsetof(struct user
, u_debugreg
[7])) {
739 addr
-= offsetof(struct user
, u_debugreg
[0]);
740 ret
= ptrace_set_debugreg(child
,
741 addr
/ sizeof(data
), data
);
745 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
746 if (!access_ok(VERIFY_WRITE
, datap
, sizeof(struct user_regs_struct
))) {
750 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
751 __put_user(getreg(child
, i
), datap
);
758 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
760 if (!access_ok(VERIFY_READ
, datap
, sizeof(struct user_regs_struct
))) {
764 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
765 __get_user(tmp
, datap
);
766 putreg(child
, i
, tmp
);
773 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
774 if (!access_ok(VERIFY_WRITE
, datap
,
775 sizeof(struct user_i387_struct
))) {
780 if (!tsk_used_math(child
))
782 get_fpregs((struct user_i387_struct __user
*)data
, child
);
786 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
787 if (!access_ok(VERIFY_READ
, datap
,
788 sizeof(struct user_i387_struct
))) {
792 set_stopped_child_used_math(child
);
793 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
799 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
800 if (!access_ok(VERIFY_WRITE
, datap
,
801 sizeof(struct user_fxsr_struct
))) {
805 if (!tsk_used_math(child
))
807 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
811 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
812 if (!access_ok(VERIFY_READ
, datap
,
813 sizeof(struct user_fxsr_struct
))) {
817 set_stopped_child_used_math(child
);
818 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
823 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
824 case PTRACE_GET_THREAD_AREA
:
827 ret
= do_get_thread_area(child
, addr
,
828 (struct user_desc __user
*) data
);
831 case PTRACE_SET_THREAD_AREA
:
834 ret
= do_set_thread_area(child
, addr
,
835 (struct user_desc __user
*) data
, 0);
840 /* normal 64bit interface to access TLS data.
841 Works just like arch_prctl, except that the arguments
843 case PTRACE_ARCH_PRCTL
:
844 ret
= do_arch_prctl(child
, data
, addr
);
848 case PTRACE_BTS_CONFIG
:
849 ret
= ptrace_bts_config
850 (child
, (struct ptrace_bts_config __user
*)addr
);
853 case PTRACE_BTS_STATUS
:
854 ret
= ptrace_bts_status
855 (child
, (struct ptrace_bts_config __user
*)addr
);
858 case PTRACE_BTS_SIZE
:
859 ret
= ptrace_bts_get_size(child
);
863 ret
= ptrace_bts_read_record
864 (child
, data
, (struct bts_struct __user
*) addr
);
867 case PTRACE_BTS_CLEAR
:
868 ret
= ptrace_bts_clear(child
);
871 case PTRACE_BTS_DRAIN
:
872 ret
= ptrace_bts_drain
873 (child
, (struct bts_struct __user
*) addr
);
877 ret
= ptrace_request(child
, request
, addr
, data
);
884 #ifdef CONFIG_IA32_EMULATION
886 #include <linux/compat.h>
887 #include <linux/syscalls.h>
888 #include <asm/ia32.h>
889 #include <asm/user32.h>
892 case offsetof(struct user32, regs.l): \
893 regs->q = value; break
896 case offsetof(struct user32, regs.rs): \
897 return set_segment_reg(child, \
898 offsetof(struct user_regs_struct, rs), \
902 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
904 struct pt_regs
*regs
= task_pt_regs(child
);
922 R32(orig_eax
, orig_ax
);
926 case offsetof(struct user32
, regs
.eflags
):
927 return set_flags(child
, value
);
929 case offsetof(struct user32
, u_debugreg
[0]) ...
930 offsetof(struct user32
, u_debugreg
[7]):
931 regno
-= offsetof(struct user32
, u_debugreg
[0]);
932 return ptrace_set_debugreg(child
, regno
/ 4, value
);
935 if (regno
> sizeof(struct user32
) || (regno
& 3))
939 * Other dummy fields in the virtual user structure
951 case offsetof(struct user32, regs.l): \
952 *val = regs->q; break
955 case offsetof(struct user32, regs.rs): \
956 *val = get_segment_reg(child, \
957 offsetof(struct user_regs_struct, rs)); \
960 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
962 struct pt_regs
*regs
= task_pt_regs(child
);
980 R32(orig_eax
, orig_ax
);
984 case offsetof(struct user32
, regs
.eflags
):
985 *val
= get_flags(child
);
988 case offsetof(struct user32
, u_debugreg
[0]) ...
989 offsetof(struct user32
, u_debugreg
[7]):
990 regno
-= offsetof(struct user32
, u_debugreg
[0]);
991 *val
= ptrace_get_debugreg(child
, regno
/ 4);
995 if (regno
> sizeof(struct user32
) || (regno
& 3))
999 * Other dummy fields in the virtual user structure
1011 static long ptrace32_siginfo(unsigned request
, u32 pid
, u32 addr
, u32 data
)
1013 siginfo_t __user
*si
= compat_alloc_user_space(sizeof(siginfo_t
));
1014 compat_siginfo_t __user
*si32
= compat_ptr(data
);
1018 if (request
== PTRACE_SETSIGINFO
) {
1019 memset(&ssi
, 0, sizeof(siginfo_t
));
1020 ret
= copy_siginfo_from_user32(&ssi
, si32
);
1023 if (copy_to_user(si
, &ssi
, sizeof(siginfo_t
)))
1026 ret
= sys_ptrace(request
, pid
, addr
, (unsigned long)si
);
1029 if (request
== PTRACE_GETSIGINFO
) {
1030 if (copy_from_user(&ssi
, si
, sizeof(siginfo_t
)))
1032 ret
= copy_siginfo_to_user32(si32
, &ssi
);
1037 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
1039 struct task_struct
*child
;
1040 struct pt_regs
*childregs
;
1041 void __user
*datap
= compat_ptr(data
);
1046 case PTRACE_TRACEME
:
1050 case PTRACE_SINGLESTEP
:
1051 case PTRACE_SINGLEBLOCK
:
1053 case PTRACE_SYSCALL
:
1054 case PTRACE_OLDSETOPTIONS
:
1055 case PTRACE_SETOPTIONS
:
1056 case PTRACE_SET_THREAD_AREA
:
1057 case PTRACE_GET_THREAD_AREA
:
1058 case PTRACE_BTS_CONFIG
:
1059 case PTRACE_BTS_STATUS
:
1060 case PTRACE_BTS_SIZE
:
1061 case PTRACE_BTS_GET
:
1062 case PTRACE_BTS_CLEAR
:
1063 case PTRACE_BTS_DRAIN
:
1064 return sys_ptrace(request
, pid
, addr
, data
);
1069 case PTRACE_PEEKTEXT
:
1070 case PTRACE_PEEKDATA
:
1071 case PTRACE_POKEDATA
:
1072 case PTRACE_POKETEXT
:
1073 case PTRACE_POKEUSR
:
1074 case PTRACE_PEEKUSR
:
1075 case PTRACE_GETREGS
:
1076 case PTRACE_SETREGS
:
1077 case PTRACE_SETFPREGS
:
1078 case PTRACE_GETFPREGS
:
1079 case PTRACE_SETFPXREGS
:
1080 case PTRACE_GETFPXREGS
:
1081 case PTRACE_GETEVENTMSG
:
1084 case PTRACE_SETSIGINFO
:
1085 case PTRACE_GETSIGINFO
:
1086 return ptrace32_siginfo(request
, pid
, addr
, data
);
1089 child
= ptrace_get_task_struct(pid
);
1091 return PTR_ERR(child
);
1093 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
1097 childregs
= task_pt_regs(child
);
1100 case PTRACE_PEEKDATA
:
1101 case PTRACE_PEEKTEXT
:
1103 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0) !=
1107 ret
= put_user(val
, (unsigned int __user
*)datap
);
1110 case PTRACE_POKEDATA
:
1111 case PTRACE_POKETEXT
:
1113 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1) !=
1118 case PTRACE_PEEKUSR
:
1119 ret
= getreg32(child
, addr
, &val
);
1121 ret
= put_user(val
, (__u32 __user
*)datap
);
1124 case PTRACE_POKEUSR
:
1125 ret
= putreg32(child
, addr
, data
);
1128 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
1131 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
1136 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(__u32
)) {
1137 getreg32(child
, i
, &val
);
1138 ret
|= __put_user(val
, (u32 __user
*)datap
);
1139 datap
+= sizeof(u32
);
1144 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
1148 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
1153 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(u32
)) {
1154 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
1155 putreg32(child
, i
, tmp
);
1156 datap
+= sizeof(u32
);
1161 case PTRACE_GETFPREGS
:
1163 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
1164 sizeof(struct user_i387_struct
)))
1166 save_i387_ia32(child
, datap
, childregs
, 1);
1170 case PTRACE_SETFPREGS
:
1172 if (!access_ok(VERIFY_WRITE
, datap
,
1173 sizeof(struct user_i387_struct
)))
1176 /* don't check EFAULT to be bug-to-bug compatible to i386 */
1177 restore_i387_ia32(child
, datap
, 1);
1180 case PTRACE_GETFPXREGS
: {
1181 struct user32_fxsr_struct __user
*u
= datap
;
1185 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
1188 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
1190 ret
= __put_user(childregs
->cs
, &u
->fcs
);
1191 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
1194 case PTRACE_SETFPXREGS
: {
1195 struct user32_fxsr_struct __user
*u
= datap
;
1199 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
1202 * no checking to be bug-to-bug compatible with i386.
1203 * but silence warning
1205 if (__copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
)))
1207 set_stopped_child_used_math(child
);
1208 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
1213 case PTRACE_GETEVENTMSG
:
1214 ret
= put_user(child
->ptrace_message
,
1215 (unsigned int __user
*)compat_ptr(data
));
1223 put_task_struct(child
);
1227 #endif /* CONFIG_IA32_EMULATION */
1229 #ifdef CONFIG_X86_32
1231 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
, int error_code
)
1233 struct siginfo info
;
1235 tsk
->thread
.trap_no
= 1;
1236 tsk
->thread
.error_code
= error_code
;
1238 memset(&info
, 0, sizeof(info
));
1239 info
.si_signo
= SIGTRAP
;
1240 info
.si_code
= TRAP_BRKPT
;
1243 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1245 /* Send us the fake SIGTRAP */
1246 force_sig_info(SIGTRAP
, &info
, tsk
);
1249 /* notification of system call entry/exit
1250 * - triggered by current->work.syscall_trace
1252 __attribute__((regparm(3)))
1253 int do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
1255 int is_sysemu
= test_thread_flag(TIF_SYSCALL_EMU
);
1257 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1260 int is_singlestep
= !is_sysemu
&& test_thread_flag(TIF_SINGLESTEP
);
1263 /* do the secure computing check first */
1265 secure_computing(regs
->orig_ax
);
1267 if (unlikely(current
->audit_context
)) {
1269 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
),
1271 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1272 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1273 * not used, entry.S will call us only on syscall exit, not
1274 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1275 * calling send_sigtrap() on syscall entry.
1277 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1278 * is_singlestep is false, despite his name, so we will still do
1279 * the correct thing.
1281 else if (is_singlestep
)
1285 if (!(current
->ptrace
& PT_PTRACED
))
1288 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1289 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1290 * here. We have to check this and return */
1291 if (is_sysemu
&& entryexit
)
1294 /* Fake a debug trap */
1296 send_sigtrap(current
, regs
, 0);
1298 if (!test_thread_flag(TIF_SYSCALL_TRACE
) && !is_sysemu
)
1301 /* the 0x80 provides a way for the tracing parent to distinguish
1302 between a syscall stop and SIGTRAP delivery */
1303 /* Note that the debugger could change the result of test_thread_flag!*/
1304 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80:0));
1307 * this isn't the same as continuing with a signal, but it will do
1308 * for normal use. strace only continues with a signal if the
1309 * stopping signal is not SIGTRAP. -brl
1311 if (current
->exit_code
) {
1312 send_sig(current
->exit_code
, current
, 1);
1313 current
->exit_code
= 0;
1317 if (unlikely(current
->audit_context
) && !entryexit
)
1318 audit_syscall_entry(AUDIT_ARCH_I386
, regs
->orig_ax
,
1319 regs
->bx
, regs
->cx
, regs
->dx
, regs
->si
);
1323 regs
->orig_ax
= -1; /* force skip of syscall restarting */
1324 if (unlikely(current
->audit_context
))
1325 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1329 #else /* CONFIG_X86_64 */
1331 static void syscall_trace(struct pt_regs
*regs
)
1335 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1337 regs
->ip
, regs
->sp
, regs
->ax
, regs
->orig_ax
, __builtin_return_address(0),
1338 current_thread_info()->flags
, current
->ptrace
);
1341 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
1344 * this isn't the same as continuing with a signal, but it will do
1345 * for normal use. strace only continues with a signal if the
1346 * stopping signal is not SIGTRAP. -brl
1348 if (current
->exit_code
) {
1349 send_sig(current
->exit_code
, current
, 1);
1350 current
->exit_code
= 0;
1354 asmlinkage
void syscall_trace_enter(struct pt_regs
*regs
)
1356 /* do the secure computing check first */
1357 secure_computing(regs
->orig_ax
);
1359 if (test_thread_flag(TIF_SYSCALL_TRACE
)
1360 && (current
->ptrace
& PT_PTRACED
))
1361 syscall_trace(regs
);
1363 if (unlikely(current
->audit_context
)) {
1364 if (test_thread_flag(TIF_IA32
)) {
1365 audit_syscall_entry(AUDIT_ARCH_I386
,
1368 regs
->dx
, regs
->si
);
1370 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1373 regs
->dx
, regs
->r10
);
1378 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
1380 if (unlikely(current
->audit_context
))
1381 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1383 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
1384 || test_thread_flag(TIF_SINGLESTEP
))
1385 && (current
->ptrace
& PT_PTRACED
))
1386 syscall_trace(regs
);
1389 #endif /* CONFIG_X86_32 */