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/user.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
23 #include <asm/uaccess.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
28 #include <asm/debugreg.h>
31 #include <asm/prctl.h>
32 #include <asm/proto.h>
37 * does not yet catch signals sent when the child dies.
38 * in exit.c or in signal.c.
42 * Determines which flags the user has access to [1 = access, 0 = no access].
44 #define FLAG_MASK_32 ((unsigned long) \
45 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
46 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
47 X86_EFLAGS_SF | X86_EFLAGS_TF | \
48 X86_EFLAGS_DF | X86_EFLAGS_OF | \
49 X86_EFLAGS_RF | X86_EFLAGS_AC))
52 * Determines whether a value may be installed in a segment register.
54 static inline bool invalid_selector(u16 value
)
56 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
61 #define FLAG_MASK FLAG_MASK_32
63 static long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
65 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
69 return ®s
->bx
+ regno
;
72 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
75 * Returning the value truncates it to 16 bits.
78 if (offset
!= offsetof(struct user_regs_struct
, gs
))
79 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
81 retval
= task
->thread
.gs
;
83 savesegment(gs
, retval
);
88 static int set_segment_reg(struct task_struct
*task
,
89 unsigned long offset
, u16 value
)
92 * The value argument was already truncated to 16 bits.
94 if (invalid_selector(value
))
97 if (offset
!= offsetof(struct user_regs_struct
, gs
))
98 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
100 task
->thread
.gs
= value
;
103 * The user-mode %gs is not affected by
104 * kernel entry, so we must update the CPU.
106 loadsegment(gs
, value
);
112 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
114 return TASK_SIZE
- 3;
117 #else /* CONFIG_X86_64 */
119 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
121 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
123 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
124 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
127 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
130 * Returning the value truncates it to 16 bits.
135 case offsetof(struct user_regs_struct
, fs
):
136 if (task
== current
) {
137 /* Older gas can't assemble movq %?s,%r?? */
138 asm("movl %%fs,%0" : "=r" (seg
));
141 return task
->thread
.fsindex
;
142 case offsetof(struct user_regs_struct
, gs
):
143 if (task
== current
) {
144 asm("movl %%gs,%0" : "=r" (seg
));
147 return task
->thread
.gsindex
;
148 case offsetof(struct user_regs_struct
, ds
):
149 if (task
== current
) {
150 asm("movl %%ds,%0" : "=r" (seg
));
153 return task
->thread
.ds
;
154 case offsetof(struct user_regs_struct
, es
):
155 if (task
== current
) {
156 asm("movl %%es,%0" : "=r" (seg
));
159 return task
->thread
.es
;
161 case offsetof(struct user_regs_struct
, cs
):
162 case offsetof(struct user_regs_struct
, ss
):
165 return *pt_regs_access(task_pt_regs(task
), offset
);
168 static int set_segment_reg(struct task_struct
*task
,
169 unsigned long offset
, u16 value
)
172 * The value argument was already truncated to 16 bits.
174 if (invalid_selector(value
))
178 case offsetof(struct user_regs_struct
,fs
):
180 * If this is setting fs as for normal 64-bit use but
181 * setting fs_base has implicitly changed it, leave it.
183 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
184 task
->thread
.fs
!= 0) ||
185 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
186 task
->thread
.fs
== 0))
188 task
->thread
.fsindex
= value
;
190 loadsegment(fs
, task
->thread
.fsindex
);
192 case offsetof(struct user_regs_struct
,gs
):
194 * If this is setting gs as for normal 64-bit use but
195 * setting gs_base has implicitly changed it, leave it.
197 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
198 task
->thread
.gs
!= 0) ||
199 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
200 task
->thread
.gs
== 0))
202 task
->thread
.gsindex
= value
;
204 load_gs_index(task
->thread
.gsindex
);
206 case offsetof(struct user_regs_struct
,ds
):
207 task
->thread
.ds
= value
;
209 loadsegment(ds
, task
->thread
.ds
);
211 case offsetof(struct user_regs_struct
,es
):
212 task
->thread
.es
= value
;
214 loadsegment(es
, task
->thread
.es
);
218 * Can't actually change these in 64-bit mode.
220 case offsetof(struct user_regs_struct
,cs
):
221 #ifdef CONFIG_IA32_EMULATION
222 if (test_tsk_thread_flag(task
, TIF_IA32
))
223 task_pt_regs(task
)->cs
= value
;
226 case offsetof(struct user_regs_struct
,ss
):
227 #ifdef CONFIG_IA32_EMULATION
228 if (test_tsk_thread_flag(task
, TIF_IA32
))
229 task_pt_regs(task
)->ss
= value
;
237 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
239 #ifdef CONFIG_IA32_EMULATION
240 if (test_tsk_thread_flag(task
, TIF_IA32
))
241 return IA32_PAGE_OFFSET
- 3;
243 return TASK_SIZE64
- 7;
246 #endif /* CONFIG_X86_32 */
248 static unsigned long get_flags(struct task_struct
*task
)
250 unsigned long retval
= task_pt_regs(task
)->flags
;
253 * If the debugger set TF, hide it from the readout.
255 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
256 retval
&= ~X86_EFLAGS_TF
;
261 static int set_flags(struct task_struct
*task
, unsigned long value
)
263 struct pt_regs
*regs
= task_pt_regs(task
);
266 * If the user value contains TF, mark that
267 * it was not "us" (the debugger) that set it.
268 * If not, make sure it stays set if we had.
270 if (value
& X86_EFLAGS_TF
)
271 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
272 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
273 value
|= X86_EFLAGS_TF
;
275 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
280 static int putreg(struct task_struct
*child
,
281 unsigned long offset
, unsigned long value
)
284 case offsetof(struct user_regs_struct
, cs
):
285 case offsetof(struct user_regs_struct
, ds
):
286 case offsetof(struct user_regs_struct
, es
):
287 case offsetof(struct user_regs_struct
, fs
):
288 case offsetof(struct user_regs_struct
, gs
):
289 case offsetof(struct user_regs_struct
, ss
):
290 return set_segment_reg(child
, offset
, value
);
292 case offsetof(struct user_regs_struct
, flags
):
293 return set_flags(child
, value
);
296 case offsetof(struct user_regs_struct
,fs_base
):
297 if (value
>= TASK_SIZE_OF(child
))
300 * When changing the segment base, use do_arch_prctl
301 * to set either thread.fs or thread.fsindex and the
302 * corresponding GDT slot.
304 if (child
->thread
.fs
!= value
)
305 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
307 case offsetof(struct user_regs_struct
,gs_base
):
309 * Exactly the same here as the %fs handling above.
311 if (value
>= TASK_SIZE_OF(child
))
313 if (child
->thread
.gs
!= value
)
314 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
319 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
323 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
326 case offsetof(struct user_regs_struct
, cs
):
327 case offsetof(struct user_regs_struct
, ds
):
328 case offsetof(struct user_regs_struct
, es
):
329 case offsetof(struct user_regs_struct
, fs
):
330 case offsetof(struct user_regs_struct
, gs
):
331 case offsetof(struct user_regs_struct
, ss
):
332 return get_segment_reg(task
, offset
);
334 case offsetof(struct user_regs_struct
, flags
):
335 return get_flags(task
);
338 case offsetof(struct user_regs_struct
, fs_base
): {
340 * do_arch_prctl may have used a GDT slot instead of
341 * the MSR. To userland, it appears the same either
342 * way, except the %fs segment selector might not be 0.
344 unsigned int seg
= task
->thread
.fsindex
;
345 if (task
->thread
.fs
!= 0)
346 return task
->thread
.fs
;
348 asm("movl %%fs,%0" : "=r" (seg
));
349 if (seg
!= FS_TLS_SEL
)
351 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
353 case offsetof(struct user_regs_struct
, gs_base
): {
355 * Exactly the same here as the %fs handling above.
357 unsigned int seg
= task
->thread
.gsindex
;
358 if (task
->thread
.gs
!= 0)
359 return task
->thread
.gs
;
361 asm("movl %%gs,%0" : "=r" (seg
));
362 if (seg
!= GS_TLS_SEL
)
364 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
369 return *pt_regs_access(task_pt_regs(task
), offset
);
372 static int genregs_get(struct task_struct
*target
,
373 const struct user_regset
*regset
,
374 unsigned int pos
, unsigned int count
,
375 void *kbuf
, void __user
*ubuf
)
378 unsigned long *k
= kbuf
;
380 *k
++ = getreg(target
, pos
);
385 unsigned long __user
*u
= ubuf
;
387 if (__put_user(getreg(target
, pos
), u
++))
397 static int genregs_set(struct task_struct
*target
,
398 const struct user_regset
*regset
,
399 unsigned int pos
, unsigned int count
,
400 const void *kbuf
, const void __user
*ubuf
)
404 const unsigned long *k
= kbuf
;
405 while (count
> 0 && !ret
) {
406 ret
= putreg(target
, pos
, *k
++);
411 const unsigned long __user
*u
= ubuf
;
412 while (count
> 0 && !ret
) {
414 ret
= __get_user(word
, u
++);
417 ret
= putreg(target
, pos
, word
);
426 * This function is trivial and will be inlined by the compiler.
427 * Having it separates the implementation details of debug
428 * registers from the interface details of ptrace.
430 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
433 case 0: return child
->thread
.debugreg0
;
434 case 1: return child
->thread
.debugreg1
;
435 case 2: return child
->thread
.debugreg2
;
436 case 3: return child
->thread
.debugreg3
;
437 case 6: return child
->thread
.debugreg6
;
438 case 7: return child
->thread
.debugreg7
;
443 static int ptrace_set_debugreg(struct task_struct
*child
,
444 int n
, unsigned long data
)
448 if (unlikely(n
== 4 || n
== 5))
451 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
455 case 0: child
->thread
.debugreg0
= data
; break;
456 case 1: child
->thread
.debugreg1
= data
; break;
457 case 2: child
->thread
.debugreg2
= data
; break;
458 case 3: child
->thread
.debugreg3
= data
; break;
461 if ((data
& ~0xffffffffUL
) != 0)
463 child
->thread
.debugreg6
= data
;
468 * Sanity-check data. Take one half-byte at once with
469 * check = (val >> (16 + 4*i)) & 0xf. It contains the
470 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
471 * 2 and 3 are LENi. Given a list of invalid values,
472 * we do mask |= 1 << invalid_value, so that
473 * (mask >> check) & 1 is a correct test for invalid
476 * R/Wi contains the type of the breakpoint /
477 * watchpoint, LENi contains the length of the watched
478 * data in the watchpoint case.
480 * The invalid values are:
481 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
482 * - R/Wi == 0x10 (break on I/O reads or writes), so
484 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
487 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
489 * See the Intel Manual "System Programming Guide",
492 * Note that LENi == 0x10 is defined on x86_64 in long
493 * mode (i.e. even for 32-bit userspace software, but
494 * 64-bit kernel), so the x86_64 mask value is 0x5454.
495 * See the AMD manual no. 24593 (AMD64 System Programming)
498 #define DR7_MASK 0x5f54
500 #define DR7_MASK 0x5554
502 data
&= ~DR_CONTROL_RESERVED
;
503 for (i
= 0; i
< 4; i
++)
504 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
506 child
->thread
.debugreg7
= data
;
508 set_tsk_thread_flag(child
, TIF_DEBUG
);
510 clear_tsk_thread_flag(child
, TIF_DEBUG
);
517 static int ptrace_bts_get_size(struct task_struct
*child
)
519 if (!child
->thread
.ds_area_msr
)
522 return ds_get_bts_index((void *)child
->thread
.ds_area_msr
);
525 static int ptrace_bts_read_record(struct task_struct
*child
,
527 struct bts_struct __user
*out
)
529 struct bts_struct ret
;
534 if (!child
->thread
.ds_area_msr
)
540 bts_end
= ds_get_bts_end((void *)child
->thread
.ds_area_msr
);
541 if (bts_end
<= index
)
544 /* translate the ptrace bts index into the ds bts index */
545 bts_index
= ds_get_bts_index((void *)child
->thread
.ds_area_msr
);
546 bts_index
-= (index
+ 1);
548 bts_index
+= bts_end
;
550 retval
= ds_read_bts((void *)child
->thread
.ds_area_msr
,
555 if (copy_to_user(out
, &ret
, sizeof(ret
)))
561 static int ptrace_bts_write_record(struct task_struct
*child
,
562 const struct bts_struct
*in
)
566 if (!child
->thread
.ds_area_msr
)
569 retval
= ds_write_bts((void *)child
->thread
.ds_area_msr
, in
);
576 static int ptrace_bts_clear(struct task_struct
*child
)
578 if (!child
->thread
.ds_area_msr
)
581 return ds_clear((void *)child
->thread
.ds_area_msr
);
584 static int ptrace_bts_drain(struct task_struct
*child
,
585 struct bts_struct __user
*out
)
588 void *ds
= (void *)child
->thread
.ds_area_msr
;
593 end
= ds_get_bts_index(ds
);
597 for (i
= 0; i
< end
; i
++, out
++) {
598 struct bts_struct ret
;
601 retval
= ds_read_bts(ds
, i
, &ret
);
605 if (copy_to_user(out
, &ret
, sizeof(ret
)))
614 static int ptrace_bts_config(struct task_struct
*child
,
615 const struct ptrace_bts_config __user
*ucfg
)
617 struct ptrace_bts_config cfg
;
618 unsigned long debugctl_mask
;
622 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
626 ds
= (void *)child
->thread
.ds_area_msr
;
628 bts_size
= ds_get_bts_size(ds
);
633 if (bts_size
!= cfg
.size
) {
634 ret
= ds_free((void **)&child
->thread
.ds_area_msr
);
639 ret
= ds_allocate((void **)&child
->thread
.ds_area_msr
,
641 ds
= (void *)child
->thread
.ds_area_msr
;
643 set_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
645 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
650 bts_size
= ds_get_bts_size(ds
);
656 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
657 ret
= ds_set_overflow(ds
, DS_O_SIGNAL
);
659 ret
= ds_set_overflow(ds
, DS_O_WRAP
);
665 debugctl_mask
= ds_debugctl_mask();
666 if (ds
&& (cfg
.flags
& PTRACE_BTS_O_TRACE
)) {
667 child
->thread
.debugctlmsr
|= debugctl_mask
;
668 set_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
670 /* there is no way for us to check whether we 'own'
671 * the respective bits in the DEBUGCTL MSR, we're
673 child
->thread
.debugctlmsr
&= ~debugctl_mask
;
675 if (!child
->thread
.debugctlmsr
)
676 clear_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
679 if (ds
&& (cfg
.flags
& PTRACE_BTS_O_SCHED
))
680 set_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
682 clear_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
687 static int ptrace_bts_status(struct task_struct
*child
,
688 struct ptrace_bts_config __user
*ucfg
)
690 void *ds
= (void *)child
->thread
.ds_area_msr
;
691 struct ptrace_bts_config cfg
;
693 memset(&cfg
, 0, sizeof(cfg
));
696 cfg
.size
= ds_get_bts_size(ds
);
698 if (ds_get_overflow(ds
) == DS_O_SIGNAL
)
699 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
701 if (test_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
) &&
702 child
->thread
.debugctlmsr
& ds_debugctl_mask())
703 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
705 if (test_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
))
706 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
709 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
715 void ptrace_bts_take_timestamp(struct task_struct
*tsk
,
716 enum bts_qualifier qualifier
)
718 struct bts_struct rec
= {
719 .qualifier
= qualifier
,
720 .variant
.jiffies
= jiffies
723 ptrace_bts_write_record(tsk
, &rec
);
727 * Called by kernel/ptrace.c when detaching..
729 * Make sure the single step bit is not set.
731 void ptrace_disable(struct task_struct
*child
)
733 user_disable_single_step(child
);
734 #ifdef TIF_SYSCALL_EMU
735 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
737 ptrace_bts_config(child
, /* options = */ 0);
738 if (child
->thread
.ds_area_msr
) {
739 ds_free((void **)&child
->thread
.ds_area_msr
);
740 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
744 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
747 unsigned long __user
*datap
= (unsigned long __user
*)data
;
750 /* when I and D space are separate, these will need to be fixed. */
751 case PTRACE_PEEKTEXT
: /* read word at location addr. */
752 case PTRACE_PEEKDATA
:
753 ret
= generic_ptrace_peekdata(child
, addr
, data
);
756 /* read the word at location addr in the USER area. */
757 case PTRACE_PEEKUSR
: {
761 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
762 addr
>= sizeof(struct user
))
765 tmp
= 0; /* Default return condition */
766 if (addr
< sizeof(struct user_regs_struct
))
767 tmp
= getreg(child
, addr
);
768 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
769 addr
<= offsetof(struct user
, u_debugreg
[7])) {
770 addr
-= offsetof(struct user
, u_debugreg
[0]);
771 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
773 ret
= put_user(tmp
, datap
);
777 /* when I and D space are separate, this will have to be fixed. */
778 case PTRACE_POKETEXT
: /* write the word at location addr. */
779 case PTRACE_POKEDATA
:
780 ret
= generic_ptrace_pokedata(child
, addr
, data
);
783 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
785 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
786 addr
>= sizeof(struct user
))
789 if (addr
< sizeof(struct user_regs_struct
))
790 ret
= putreg(child
, addr
, data
);
791 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
792 addr
<= offsetof(struct user
, u_debugreg
[7])) {
793 addr
-= offsetof(struct user
, u_debugreg
[0]);
794 ret
= ptrace_set_debugreg(child
,
795 addr
/ sizeof(data
), data
);
799 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
800 if (!access_ok(VERIFY_WRITE
, datap
, sizeof(struct user_regs_struct
))) {
804 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
805 __put_user(getreg(child
, i
), datap
);
812 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
814 if (!access_ok(VERIFY_READ
, datap
, sizeof(struct user_regs_struct
))) {
818 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
819 __get_user(tmp
, datap
);
820 putreg(child
, i
, tmp
);
827 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
828 if (!access_ok(VERIFY_WRITE
, datap
,
829 sizeof(struct user_i387_struct
))) {
834 if (!tsk_used_math(child
))
836 get_fpregs((struct user_i387_struct __user
*)data
, child
);
840 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
841 if (!access_ok(VERIFY_READ
, datap
,
842 sizeof(struct user_i387_struct
))) {
846 set_stopped_child_used_math(child
);
847 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
853 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
854 if (!access_ok(VERIFY_WRITE
, datap
,
855 sizeof(struct user_fxsr_struct
))) {
859 if (!tsk_used_math(child
))
861 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
865 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
866 if (!access_ok(VERIFY_READ
, datap
,
867 sizeof(struct user_fxsr_struct
))) {
871 set_stopped_child_used_math(child
);
872 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
877 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
878 case PTRACE_GET_THREAD_AREA
:
881 ret
= do_get_thread_area(child
, addr
,
882 (struct user_desc __user
*) data
);
885 case PTRACE_SET_THREAD_AREA
:
888 ret
= do_set_thread_area(child
, addr
,
889 (struct user_desc __user
*) data
, 0);
894 /* normal 64bit interface to access TLS data.
895 Works just like arch_prctl, except that the arguments
897 case PTRACE_ARCH_PRCTL
:
898 ret
= do_arch_prctl(child
, data
, addr
);
902 case PTRACE_BTS_CONFIG
:
903 ret
= ptrace_bts_config
904 (child
, (struct ptrace_bts_config __user
*)addr
);
907 case PTRACE_BTS_STATUS
:
908 ret
= ptrace_bts_status
909 (child
, (struct ptrace_bts_config __user
*)addr
);
912 case PTRACE_BTS_SIZE
:
913 ret
= ptrace_bts_get_size(child
);
917 ret
= ptrace_bts_read_record
918 (child
, data
, (struct bts_struct __user
*) addr
);
921 case PTRACE_BTS_CLEAR
:
922 ret
= ptrace_bts_clear(child
);
925 case PTRACE_BTS_DRAIN
:
926 ret
= ptrace_bts_drain
927 (child
, (struct bts_struct __user
*) addr
);
931 ret
= ptrace_request(child
, request
, addr
, data
);
938 #ifdef CONFIG_IA32_EMULATION
940 #include <linux/compat.h>
941 #include <linux/syscalls.h>
942 #include <asm/ia32.h>
943 #include <asm/user32.h>
946 case offsetof(struct user32, regs.l): \
947 regs->q = value; break
950 case offsetof(struct user32, regs.rs): \
951 return set_segment_reg(child, \
952 offsetof(struct user_regs_struct, rs), \
956 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
958 struct pt_regs
*regs
= task_pt_regs(child
);
976 R32(orig_eax
, orig_ax
);
980 case offsetof(struct user32
, regs
.eflags
):
981 return set_flags(child
, value
);
983 case offsetof(struct user32
, u_debugreg
[0]) ...
984 offsetof(struct user32
, u_debugreg
[7]):
985 regno
-= offsetof(struct user32
, u_debugreg
[0]);
986 return ptrace_set_debugreg(child
, regno
/ 4, value
);
989 if (regno
> sizeof(struct user32
) || (regno
& 3))
993 * Other dummy fields in the virtual user structure
1005 case offsetof(struct user32, regs.l): \
1006 *val = regs->q; break
1009 case offsetof(struct user32, regs.rs): \
1010 *val = get_segment_reg(child, \
1011 offsetof(struct user_regs_struct, rs)); \
1014 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1016 struct pt_regs
*regs
= task_pt_regs(child
);
1034 R32(orig_eax
, orig_ax
);
1038 case offsetof(struct user32
, regs
.eflags
):
1039 *val
= get_flags(child
);
1042 case offsetof(struct user32
, u_debugreg
[0]) ...
1043 offsetof(struct user32
, u_debugreg
[7]):
1044 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1045 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1049 if (regno
> sizeof(struct user32
) || (regno
& 3))
1053 * Other dummy fields in the virtual user structure
1065 static int genregs32_get(struct task_struct
*target
,
1066 const struct user_regset
*regset
,
1067 unsigned int pos
, unsigned int count
,
1068 void *kbuf
, void __user
*ubuf
)
1071 compat_ulong_t
*k
= kbuf
;
1073 getreg32(target
, pos
, k
++);
1074 count
-= sizeof(*k
);
1078 compat_ulong_t __user
*u
= ubuf
;
1080 compat_ulong_t word
;
1081 getreg32(target
, pos
, &word
);
1082 if (__put_user(word
, u
++))
1084 count
-= sizeof(*u
);
1092 static int genregs32_set(struct task_struct
*target
,
1093 const struct user_regset
*regset
,
1094 unsigned int pos
, unsigned int count
,
1095 const void *kbuf
, const void __user
*ubuf
)
1099 const compat_ulong_t
*k
= kbuf
;
1100 while (count
> 0 && !ret
) {
1101 ret
= putreg(target
, pos
, *k
++);
1102 count
-= sizeof(*k
);
1106 const compat_ulong_t __user
*u
= ubuf
;
1107 while (count
> 0 && !ret
) {
1108 compat_ulong_t word
;
1109 ret
= __get_user(word
, u
++);
1112 ret
= putreg(target
, pos
, word
);
1113 count
-= sizeof(*u
);
1120 static long ptrace32_siginfo(unsigned request
, u32 pid
, u32 addr
, u32 data
)
1122 siginfo_t __user
*si
= compat_alloc_user_space(sizeof(siginfo_t
));
1123 compat_siginfo_t __user
*si32
= compat_ptr(data
);
1127 if (request
== PTRACE_SETSIGINFO
) {
1128 memset(&ssi
, 0, sizeof(siginfo_t
));
1129 ret
= copy_siginfo_from_user32(&ssi
, si32
);
1132 if (copy_to_user(si
, &ssi
, sizeof(siginfo_t
)))
1135 ret
= sys_ptrace(request
, pid
, addr
, (unsigned long)si
);
1138 if (request
== PTRACE_GETSIGINFO
) {
1139 if (copy_from_user(&ssi
, si
, sizeof(siginfo_t
)))
1141 ret
= copy_siginfo_to_user32(si32
, &ssi
);
1146 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
1148 struct task_struct
*child
;
1149 struct pt_regs
*childregs
;
1150 void __user
*datap
= compat_ptr(data
);
1155 case PTRACE_TRACEME
:
1159 case PTRACE_SINGLESTEP
:
1160 case PTRACE_SINGLEBLOCK
:
1162 case PTRACE_SYSCALL
:
1163 case PTRACE_OLDSETOPTIONS
:
1164 case PTRACE_SETOPTIONS
:
1165 case PTRACE_SET_THREAD_AREA
:
1166 case PTRACE_GET_THREAD_AREA
:
1167 case PTRACE_BTS_CONFIG
:
1168 case PTRACE_BTS_STATUS
:
1169 case PTRACE_BTS_SIZE
:
1170 case PTRACE_BTS_GET
:
1171 case PTRACE_BTS_CLEAR
:
1172 case PTRACE_BTS_DRAIN
:
1173 return sys_ptrace(request
, pid
, addr
, data
);
1178 case PTRACE_PEEKTEXT
:
1179 case PTRACE_PEEKDATA
:
1180 case PTRACE_POKEDATA
:
1181 case PTRACE_POKETEXT
:
1182 case PTRACE_POKEUSR
:
1183 case PTRACE_PEEKUSR
:
1184 case PTRACE_GETREGS
:
1185 case PTRACE_SETREGS
:
1186 case PTRACE_SETFPREGS
:
1187 case PTRACE_GETFPREGS
:
1188 case PTRACE_SETFPXREGS
:
1189 case PTRACE_GETFPXREGS
:
1190 case PTRACE_GETEVENTMSG
:
1193 case PTRACE_SETSIGINFO
:
1194 case PTRACE_GETSIGINFO
:
1195 return ptrace32_siginfo(request
, pid
, addr
, data
);
1198 child
= ptrace_get_task_struct(pid
);
1200 return PTR_ERR(child
);
1202 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
1206 childregs
= task_pt_regs(child
);
1209 case PTRACE_PEEKDATA
:
1210 case PTRACE_PEEKTEXT
:
1212 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0) !=
1216 ret
= put_user(val
, (unsigned int __user
*)datap
);
1219 case PTRACE_POKEDATA
:
1220 case PTRACE_POKETEXT
:
1222 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1) !=
1227 case PTRACE_PEEKUSR
:
1228 ret
= getreg32(child
, addr
, &val
);
1230 ret
= put_user(val
, (__u32 __user
*)datap
);
1233 case PTRACE_POKEUSR
:
1234 ret
= putreg32(child
, addr
, data
);
1237 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
1240 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
1245 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(__u32
)) {
1246 getreg32(child
, i
, &val
);
1247 ret
|= __put_user(val
, (u32 __user
*)datap
);
1248 datap
+= sizeof(u32
);
1253 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
1257 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
1262 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(u32
)) {
1263 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
1264 putreg32(child
, i
, tmp
);
1265 datap
+= sizeof(u32
);
1270 case PTRACE_GETFPREGS
:
1272 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
1273 sizeof(struct user_i387_struct
)))
1275 save_i387_ia32(child
, datap
, childregs
, 1);
1279 case PTRACE_SETFPREGS
:
1281 if (!access_ok(VERIFY_WRITE
, datap
,
1282 sizeof(struct user_i387_struct
)))
1285 /* don't check EFAULT to be bug-to-bug compatible to i386 */
1286 restore_i387_ia32(child
, datap
, 1);
1289 case PTRACE_GETFPXREGS
: {
1290 struct user32_fxsr_struct __user
*u
= datap
;
1294 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
1297 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
1299 ret
= __put_user(childregs
->cs
, &u
->fcs
);
1300 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
1303 case PTRACE_SETFPXREGS
: {
1304 struct user32_fxsr_struct __user
*u
= datap
;
1308 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
1311 * no checking to be bug-to-bug compatible with i386.
1312 * but silence warning
1314 if (__copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
)))
1316 set_stopped_child_used_math(child
);
1317 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
1322 case PTRACE_GETEVENTMSG
:
1323 ret
= put_user(child
->ptrace_message
,
1324 (unsigned int __user
*)compat_ptr(data
));
1332 put_task_struct(child
);
1336 #endif /* CONFIG_IA32_EMULATION */
1338 #ifdef CONFIG_X86_32
1340 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
, int error_code
)
1342 struct siginfo info
;
1344 tsk
->thread
.trap_no
= 1;
1345 tsk
->thread
.error_code
= error_code
;
1347 memset(&info
, 0, sizeof(info
));
1348 info
.si_signo
= SIGTRAP
;
1349 info
.si_code
= TRAP_BRKPT
;
1352 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1354 /* Send us the fake SIGTRAP */
1355 force_sig_info(SIGTRAP
, &info
, tsk
);
1358 /* notification of system call entry/exit
1359 * - triggered by current->work.syscall_trace
1361 __attribute__((regparm(3)))
1362 int do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
1364 int is_sysemu
= test_thread_flag(TIF_SYSCALL_EMU
);
1366 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1369 int is_singlestep
= !is_sysemu
&& test_thread_flag(TIF_SINGLESTEP
);
1372 /* do the secure computing check first */
1374 secure_computing(regs
->orig_ax
);
1376 if (unlikely(current
->audit_context
)) {
1378 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
),
1380 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1381 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1382 * not used, entry.S will call us only on syscall exit, not
1383 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1384 * calling send_sigtrap() on syscall entry.
1386 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1387 * is_singlestep is false, despite his name, so we will still do
1388 * the correct thing.
1390 else if (is_singlestep
)
1394 if (!(current
->ptrace
& PT_PTRACED
))
1397 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1398 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1399 * here. We have to check this and return */
1400 if (is_sysemu
&& entryexit
)
1403 /* Fake a debug trap */
1405 send_sigtrap(current
, regs
, 0);
1407 if (!test_thread_flag(TIF_SYSCALL_TRACE
) && !is_sysemu
)
1410 /* the 0x80 provides a way for the tracing parent to distinguish
1411 between a syscall stop and SIGTRAP delivery */
1412 /* Note that the debugger could change the result of test_thread_flag!*/
1413 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80:0));
1416 * this isn't the same as continuing with a signal, but it will do
1417 * for normal use. strace only continues with a signal if the
1418 * stopping signal is not SIGTRAP. -brl
1420 if (current
->exit_code
) {
1421 send_sig(current
->exit_code
, current
, 1);
1422 current
->exit_code
= 0;
1426 if (unlikely(current
->audit_context
) && !entryexit
)
1427 audit_syscall_entry(AUDIT_ARCH_I386
, regs
->orig_ax
,
1428 regs
->bx
, regs
->cx
, regs
->dx
, regs
->si
);
1432 regs
->orig_ax
= -1; /* force skip of syscall restarting */
1433 if (unlikely(current
->audit_context
))
1434 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1438 #else /* CONFIG_X86_64 */
1440 static void syscall_trace(struct pt_regs
*regs
)
1444 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1446 regs
->ip
, regs
->sp
, regs
->ax
, regs
->orig_ax
, __builtin_return_address(0),
1447 current_thread_info()->flags
, current
->ptrace
);
1450 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
1453 * this isn't the same as continuing with a signal, but it will do
1454 * for normal use. strace only continues with a signal if the
1455 * stopping signal is not SIGTRAP. -brl
1457 if (current
->exit_code
) {
1458 send_sig(current
->exit_code
, current
, 1);
1459 current
->exit_code
= 0;
1463 asmlinkage
void syscall_trace_enter(struct pt_regs
*regs
)
1465 /* do the secure computing check first */
1466 secure_computing(regs
->orig_ax
);
1468 if (test_thread_flag(TIF_SYSCALL_TRACE
)
1469 && (current
->ptrace
& PT_PTRACED
))
1470 syscall_trace(regs
);
1472 if (unlikely(current
->audit_context
)) {
1473 if (test_thread_flag(TIF_IA32
)) {
1474 audit_syscall_entry(AUDIT_ARCH_I386
,
1477 regs
->dx
, regs
->si
);
1479 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1482 regs
->dx
, regs
->r10
);
1487 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
1489 if (unlikely(current
->audit_context
))
1490 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1492 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
1493 || test_thread_flag(TIF_SINGLESTEP
))
1494 && (current
->ptrace
& PT_PTRACED
))
1495 syscall_trace(regs
);
1498 #endif /* CONFIG_X86_32 */