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 * The maximal size of a BTS buffer per traced task in number of BTS
39 #define PTRACE_BTS_BUFFER_MAX 4000
42 * does not yet catch signals sent when the child dies.
43 * in exit.c or in signal.c.
47 * Determines which flags the user has access to [1 = access, 0 = no access].
49 #define FLAG_MASK_32 ((unsigned long) \
50 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
51 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
52 X86_EFLAGS_SF | X86_EFLAGS_TF | \
53 X86_EFLAGS_DF | X86_EFLAGS_OF | \
54 X86_EFLAGS_RF | X86_EFLAGS_AC))
57 * Determines whether a value may be installed in a segment register.
59 static inline bool invalid_selector(u16 value
)
61 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
66 #define FLAG_MASK FLAG_MASK_32
68 static long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
70 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
74 return ®s
->bx
+ regno
;
77 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
80 * Returning the value truncates it to 16 bits.
83 if (offset
!= offsetof(struct user_regs_struct
, gs
))
84 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
86 retval
= task
->thread
.gs
;
88 savesegment(gs
, retval
);
93 static int set_segment_reg(struct task_struct
*task
,
94 unsigned long offset
, u16 value
)
97 * The value argument was already truncated to 16 bits.
99 if (invalid_selector(value
))
102 if (offset
!= offsetof(struct user_regs_struct
, gs
))
103 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
105 task
->thread
.gs
= value
;
108 * The user-mode %gs is not affected by
109 * kernel entry, so we must update the CPU.
111 loadsegment(gs
, value
);
117 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
119 return TASK_SIZE
- 3;
122 #else /* CONFIG_X86_64 */
124 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
126 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
128 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
129 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
132 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
135 * Returning the value truncates it to 16 bits.
140 case offsetof(struct user_regs_struct
, fs
):
141 if (task
== current
) {
142 /* Older gas can't assemble movq %?s,%r?? */
143 asm("movl %%fs,%0" : "=r" (seg
));
146 return task
->thread
.fsindex
;
147 case offsetof(struct user_regs_struct
, gs
):
148 if (task
== current
) {
149 asm("movl %%gs,%0" : "=r" (seg
));
152 return task
->thread
.gsindex
;
153 case offsetof(struct user_regs_struct
, ds
):
154 if (task
== current
) {
155 asm("movl %%ds,%0" : "=r" (seg
));
158 return task
->thread
.ds
;
159 case offsetof(struct user_regs_struct
, es
):
160 if (task
== current
) {
161 asm("movl %%es,%0" : "=r" (seg
));
164 return task
->thread
.es
;
166 case offsetof(struct user_regs_struct
, cs
):
167 case offsetof(struct user_regs_struct
, ss
):
170 return *pt_regs_access(task_pt_regs(task
), offset
);
173 static int set_segment_reg(struct task_struct
*task
,
174 unsigned long offset
, u16 value
)
177 * The value argument was already truncated to 16 bits.
179 if (invalid_selector(value
))
183 case offsetof(struct user_regs_struct
,fs
):
185 * If this is setting fs as for normal 64-bit use but
186 * setting fs_base has implicitly changed it, leave it.
188 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
189 task
->thread
.fs
!= 0) ||
190 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
191 task
->thread
.fs
== 0))
193 task
->thread
.fsindex
= value
;
195 loadsegment(fs
, task
->thread
.fsindex
);
197 case offsetof(struct user_regs_struct
,gs
):
199 * If this is setting gs as for normal 64-bit use but
200 * setting gs_base has implicitly changed it, leave it.
202 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
203 task
->thread
.gs
!= 0) ||
204 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
205 task
->thread
.gs
== 0))
207 task
->thread
.gsindex
= value
;
209 load_gs_index(task
->thread
.gsindex
);
211 case offsetof(struct user_regs_struct
,ds
):
212 task
->thread
.ds
= value
;
214 loadsegment(ds
, task
->thread
.ds
);
216 case offsetof(struct user_regs_struct
,es
):
217 task
->thread
.es
= value
;
219 loadsegment(es
, task
->thread
.es
);
223 * Can't actually change these in 64-bit mode.
225 case offsetof(struct user_regs_struct
,cs
):
226 #ifdef CONFIG_IA32_EMULATION
227 if (test_tsk_thread_flag(task
, TIF_IA32
))
228 task_pt_regs(task
)->cs
= value
;
231 case offsetof(struct user_regs_struct
,ss
):
232 #ifdef CONFIG_IA32_EMULATION
233 if (test_tsk_thread_flag(task
, TIF_IA32
))
234 task_pt_regs(task
)->ss
= value
;
242 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
244 #ifdef CONFIG_IA32_EMULATION
245 if (test_tsk_thread_flag(task
, TIF_IA32
))
246 return IA32_PAGE_OFFSET
- 3;
248 return TASK_SIZE64
- 7;
251 #endif /* CONFIG_X86_32 */
253 static unsigned long get_flags(struct task_struct
*task
)
255 unsigned long retval
= task_pt_regs(task
)->flags
;
258 * If the debugger set TF, hide it from the readout.
260 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
261 retval
&= ~X86_EFLAGS_TF
;
266 static int set_flags(struct task_struct
*task
, unsigned long value
)
268 struct pt_regs
*regs
= task_pt_regs(task
);
271 * If the user value contains TF, mark that
272 * it was not "us" (the debugger) that set it.
273 * If not, make sure it stays set if we had.
275 if (value
& X86_EFLAGS_TF
)
276 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
277 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
278 value
|= X86_EFLAGS_TF
;
280 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
285 static int putreg(struct task_struct
*child
,
286 unsigned long offset
, unsigned long value
)
289 case offsetof(struct user_regs_struct
, cs
):
290 case offsetof(struct user_regs_struct
, ds
):
291 case offsetof(struct user_regs_struct
, es
):
292 case offsetof(struct user_regs_struct
, fs
):
293 case offsetof(struct user_regs_struct
, gs
):
294 case offsetof(struct user_regs_struct
, ss
):
295 return set_segment_reg(child
, offset
, value
);
297 case offsetof(struct user_regs_struct
, flags
):
298 return set_flags(child
, value
);
301 case offsetof(struct user_regs_struct
,fs_base
):
302 if (value
>= TASK_SIZE_OF(child
))
305 * When changing the segment base, use do_arch_prctl
306 * to set either thread.fs or thread.fsindex and the
307 * corresponding GDT slot.
309 if (child
->thread
.fs
!= value
)
310 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
312 case offsetof(struct user_regs_struct
,gs_base
):
314 * Exactly the same here as the %fs handling above.
316 if (value
>= TASK_SIZE_OF(child
))
318 if (child
->thread
.gs
!= value
)
319 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
324 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
328 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
331 case offsetof(struct user_regs_struct
, cs
):
332 case offsetof(struct user_regs_struct
, ds
):
333 case offsetof(struct user_regs_struct
, es
):
334 case offsetof(struct user_regs_struct
, fs
):
335 case offsetof(struct user_regs_struct
, gs
):
336 case offsetof(struct user_regs_struct
, ss
):
337 return get_segment_reg(task
, offset
);
339 case offsetof(struct user_regs_struct
, flags
):
340 return get_flags(task
);
343 case offsetof(struct user_regs_struct
, fs_base
): {
345 * do_arch_prctl may have used a GDT slot instead of
346 * the MSR. To userland, it appears the same either
347 * way, except the %fs segment selector might not be 0.
349 unsigned int seg
= task
->thread
.fsindex
;
350 if (task
->thread
.fs
!= 0)
351 return task
->thread
.fs
;
353 asm("movl %%fs,%0" : "=r" (seg
));
354 if (seg
!= FS_TLS_SEL
)
356 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
358 case offsetof(struct user_regs_struct
, gs_base
): {
360 * Exactly the same here as the %fs handling above.
362 unsigned int seg
= task
->thread
.gsindex
;
363 if (task
->thread
.gs
!= 0)
364 return task
->thread
.gs
;
366 asm("movl %%gs,%0" : "=r" (seg
));
367 if (seg
!= GS_TLS_SEL
)
369 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
374 return *pt_regs_access(task_pt_regs(task
), offset
);
378 * This function is trivial and will be inlined by the compiler.
379 * Having it separates the implementation details of debug
380 * registers from the interface details of ptrace.
382 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
385 case 0: return child
->thread
.debugreg0
;
386 case 1: return child
->thread
.debugreg1
;
387 case 2: return child
->thread
.debugreg2
;
388 case 3: return child
->thread
.debugreg3
;
389 case 6: return child
->thread
.debugreg6
;
390 case 7: return child
->thread
.debugreg7
;
395 static int ptrace_set_debugreg(struct task_struct
*child
,
396 int n
, unsigned long data
)
400 if (unlikely(n
== 4 || n
== 5))
403 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
407 case 0: child
->thread
.debugreg0
= data
; break;
408 case 1: child
->thread
.debugreg1
= data
; break;
409 case 2: child
->thread
.debugreg2
= data
; break;
410 case 3: child
->thread
.debugreg3
= data
; break;
413 if ((data
& ~0xffffffffUL
) != 0)
415 child
->thread
.debugreg6
= data
;
420 * Sanity-check data. Take one half-byte at once with
421 * check = (val >> (16 + 4*i)) & 0xf. It contains the
422 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
423 * 2 and 3 are LENi. Given a list of invalid values,
424 * we do mask |= 1 << invalid_value, so that
425 * (mask >> check) & 1 is a correct test for invalid
428 * R/Wi contains the type of the breakpoint /
429 * watchpoint, LENi contains the length of the watched
430 * data in the watchpoint case.
432 * The invalid values are:
433 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
434 * - R/Wi == 0x10 (break on I/O reads or writes), so
436 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
439 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
441 * See the Intel Manual "System Programming Guide",
444 * Note that LENi == 0x10 is defined on x86_64 in long
445 * mode (i.e. even for 32-bit userspace software, but
446 * 64-bit kernel), so the x86_64 mask value is 0x5454.
447 * See the AMD manual no. 24593 (AMD64 System Programming)
450 #define DR7_MASK 0x5f54
452 #define DR7_MASK 0x5554
454 data
&= ~DR_CONTROL_RESERVED
;
455 for (i
= 0; i
< 4; i
++)
456 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
458 child
->thread
.debugreg7
= data
;
460 set_tsk_thread_flag(child
, TIF_DEBUG
);
462 clear_tsk_thread_flag(child
, TIF_DEBUG
);
469 static int ptrace_bts_max_buffer_size(void)
471 return PTRACE_BTS_BUFFER_MAX
;
474 static int ptrace_bts_get_buffer_size(struct task_struct
*child
)
476 if (!child
->thread
.ds_area_msr
)
479 return ds_get_bts_size((void *)child
->thread
.ds_area_msr
);
482 static int ptrace_bts_read_record(struct task_struct
*child
,
484 struct bts_struct __user
*out
)
486 struct bts_struct ret
;
491 if (!child
->thread
.ds_area_msr
)
497 bts_size
= ds_get_bts_size((void *)child
->thread
.ds_area_msr
);
498 if (bts_size
<= index
)
501 /* translate the ptrace bts index into the ds bts index */
502 bts_index
= ds_get_bts_index((void *)child
->thread
.ds_area_msr
);
503 bts_index
-= (index
+ 1);
505 bts_index
+= bts_size
;
507 retval
= ds_read_bts((void *)child
->thread
.ds_area_msr
,
512 if (copy_to_user(out
, &ret
, sizeof(ret
)))
518 static int ptrace_bts_write_record(struct task_struct
*child
,
519 const struct bts_struct
*in
)
523 if (!child
->thread
.ds_area_msr
)
526 retval
= ds_write_bts((void *)child
->thread
.ds_area_msr
, in
);
533 static int ptrace_bts_config(struct task_struct
*child
,
534 unsigned long options
)
536 unsigned long debugctl_mask
= ds_debugctl_mask();
539 retval
= ptrace_bts_get_buffer_size(child
);
545 if (options
& PTRACE_BTS_O_TRACE_TASK
) {
546 child
->thread
.debugctlmsr
|= debugctl_mask
;
547 set_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
549 /* there is no way for us to check whether we 'own'
550 * the respective bits in the DEBUGCTL MSR, we're
552 child
->thread
.debugctlmsr
&= ~debugctl_mask
;
554 if (!child
->thread
.debugctlmsr
)
555 clear_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
558 if (options
& PTRACE_BTS_O_TIMESTAMPS
)
559 set_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
561 clear_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
);
566 static int ptrace_bts_status(struct task_struct
*child
)
568 unsigned long debugctl_mask
= ds_debugctl_mask();
569 int retval
, status
= 0;
571 retval
= ptrace_bts_get_buffer_size(child
);
577 if (ptrace_bts_get_buffer_size(child
) <= 0)
580 if (test_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
) &&
581 child
->thread
.debugctlmsr
& debugctl_mask
)
582 status
|= PTRACE_BTS_O_TRACE_TASK
;
583 if (test_tsk_thread_flag(child
, TIF_BTS_TRACE_TS
))
584 status
|= PTRACE_BTS_O_TIMESTAMPS
;
589 static int ptrace_bts_allocate_bts(struct task_struct
*child
,
595 if (size_in_records
< 0)
598 if (size_in_records
> ptrace_bts_max_buffer_size())
601 if (size_in_records
== 0) {
602 ptrace_bts_config(child
, /* options = */ 0);
604 retval
= ds_allocate(&ds
, size_in_records
);
609 if (child
->thread
.ds_area_msr
)
610 ds_free((void **)&child
->thread
.ds_area_msr
);
612 child
->thread
.ds_area_msr
= (unsigned long)ds
;
613 if (child
->thread
.ds_area_msr
)
614 set_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
616 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
621 void ptrace_bts_take_timestamp(struct task_struct
*tsk
,
622 enum bts_qualifier qualifier
)
624 struct bts_struct rec
= {
625 .qualifier
= qualifier
,
626 .variant
.jiffies
= jiffies
629 if (ptrace_bts_get_buffer_size(tsk
) <= 0)
632 ptrace_bts_write_record(tsk
, &rec
);
636 * Called by kernel/ptrace.c when detaching..
638 * Make sure the single step bit is not set.
640 void ptrace_disable(struct task_struct
*child
)
642 user_disable_single_step(child
);
643 #ifdef TIF_SYSCALL_EMU
644 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
646 ptrace_bts_config(child
, /* options = */ 0);
647 if (child
->thread
.ds_area_msr
) {
648 ds_free((void **)&child
->thread
.ds_area_msr
);
649 clear_tsk_thread_flag(child
, TIF_DS_AREA_MSR
);
653 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
656 unsigned long __user
*datap
= (unsigned long __user
*)data
;
659 /* when I and D space are separate, these will need to be fixed. */
660 case PTRACE_PEEKTEXT
: /* read word at location addr. */
661 case PTRACE_PEEKDATA
:
662 ret
= generic_ptrace_peekdata(child
, addr
, data
);
665 /* read the word at location addr in the USER area. */
666 case PTRACE_PEEKUSR
: {
670 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
671 addr
>= sizeof(struct user
))
674 tmp
= 0; /* Default return condition */
675 if (addr
< sizeof(struct user_regs_struct
))
676 tmp
= getreg(child
, addr
);
677 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
678 addr
<= offsetof(struct user
, u_debugreg
[7])) {
679 addr
-= offsetof(struct user
, u_debugreg
[0]);
680 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
682 ret
= put_user(tmp
, datap
);
686 /* when I and D space are separate, this will have to be fixed. */
687 case PTRACE_POKETEXT
: /* write the word at location addr. */
688 case PTRACE_POKEDATA
:
689 ret
= generic_ptrace_pokedata(child
, addr
, data
);
692 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
694 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
695 addr
>= sizeof(struct user
))
698 if (addr
< sizeof(struct user_regs_struct
))
699 ret
= putreg(child
, addr
, data
);
700 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
701 addr
<= offsetof(struct user
, u_debugreg
[7])) {
702 addr
-= offsetof(struct user
, u_debugreg
[0]);
703 ret
= ptrace_set_debugreg(child
,
704 addr
/ sizeof(data
), data
);
708 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
709 if (!access_ok(VERIFY_WRITE
, datap
, sizeof(struct user_regs_struct
))) {
713 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
714 __put_user(getreg(child
, i
), datap
);
721 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
723 if (!access_ok(VERIFY_READ
, datap
, sizeof(struct user_regs_struct
))) {
727 for (i
= 0; i
< sizeof(struct user_regs_struct
); i
+= sizeof(long)) {
728 __get_user(tmp
, datap
);
729 putreg(child
, i
, tmp
);
736 case PTRACE_GETFPREGS
: { /* Get the child FPU state. */
737 if (!access_ok(VERIFY_WRITE
, datap
,
738 sizeof(struct user_i387_struct
))) {
743 if (!tsk_used_math(child
))
745 get_fpregs((struct user_i387_struct __user
*)data
, child
);
749 case PTRACE_SETFPREGS
: { /* Set the child FPU state. */
750 if (!access_ok(VERIFY_READ
, datap
,
751 sizeof(struct user_i387_struct
))) {
755 set_stopped_child_used_math(child
);
756 set_fpregs(child
, (struct user_i387_struct __user
*)data
);
762 case PTRACE_GETFPXREGS
: { /* Get the child extended FPU state. */
763 if (!access_ok(VERIFY_WRITE
, datap
,
764 sizeof(struct user_fxsr_struct
))) {
768 if (!tsk_used_math(child
))
770 ret
= get_fpxregs((struct user_fxsr_struct __user
*)data
, child
);
774 case PTRACE_SETFPXREGS
: { /* Set the child extended FPU state. */
775 if (!access_ok(VERIFY_READ
, datap
,
776 sizeof(struct user_fxsr_struct
))) {
780 set_stopped_child_used_math(child
);
781 ret
= set_fpxregs(child
, (struct user_fxsr_struct __user
*)data
);
786 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
787 case PTRACE_GET_THREAD_AREA
:
790 ret
= do_get_thread_area(child
, addr
,
791 (struct user_desc __user
*) data
);
794 case PTRACE_SET_THREAD_AREA
:
797 ret
= do_set_thread_area(child
, addr
,
798 (struct user_desc __user
*) data
, 0);
803 /* normal 64bit interface to access TLS data.
804 Works just like arch_prctl, except that the arguments
806 case PTRACE_ARCH_PRCTL
:
807 ret
= do_arch_prctl(child
, data
, addr
);
811 case PTRACE_BTS_MAX_BUFFER_SIZE
:
812 ret
= ptrace_bts_max_buffer_size();
815 case PTRACE_BTS_ALLOCATE_BUFFER
:
816 ret
= ptrace_bts_allocate_bts(child
, data
);
819 case PTRACE_BTS_GET_BUFFER_SIZE
:
820 ret
= ptrace_bts_get_buffer_size(child
);
823 case PTRACE_BTS_READ_RECORD
:
824 ret
= ptrace_bts_read_record
826 (struct bts_struct __user
*) addr
);
829 case PTRACE_BTS_CONFIG
:
830 ret
= ptrace_bts_config(child
, data
);
833 case PTRACE_BTS_STATUS
:
834 ret
= ptrace_bts_status(child
);
838 ret
= ptrace_request(child
, request
, addr
, data
);
845 #ifdef CONFIG_IA32_EMULATION
847 #include <linux/compat.h>
848 #include <linux/syscalls.h>
849 #include <asm/ia32.h>
850 #include <asm/fpu32.h>
851 #include <asm/user32.h>
854 case offsetof(struct user32, regs.l): \
855 regs->q = value; break
858 case offsetof(struct user32, regs.rs): \
859 return set_segment_reg(child, \
860 offsetof(struct user_regs_struct, rs), \
864 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
866 struct pt_regs
*regs
= task_pt_regs(child
);
884 R32(orig_eax
, orig_ax
);
888 case offsetof(struct user32
, regs
.eflags
):
889 return set_flags(child
, value
);
891 case offsetof(struct user32
, u_debugreg
[0]) ...
892 offsetof(struct user32
, u_debugreg
[7]):
893 regno
-= offsetof(struct user32
, u_debugreg
[0]);
894 return ptrace_set_debugreg(child
, regno
/ 4, value
);
897 if (regno
> sizeof(struct user32
) || (regno
& 3))
901 * Other dummy fields in the virtual user structure
913 case offsetof(struct user32, regs.l): \
914 *val = regs->q; break
917 case offsetof(struct user32, regs.rs): \
918 *val = get_segment_reg(child, \
919 offsetof(struct user_regs_struct, rs)); \
922 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
924 struct pt_regs
*regs
= task_pt_regs(child
);
942 R32(orig_eax
, orig_ax
);
946 case offsetof(struct user32
, regs
.eflags
):
947 *val
= get_flags(child
);
950 case offsetof(struct user32
, u_debugreg
[0]) ...
951 offsetof(struct user32
, u_debugreg
[7]):
952 regno
-= offsetof(struct user32
, u_debugreg
[0]);
953 *val
= ptrace_get_debugreg(child
, regno
/ 4);
957 if (regno
> sizeof(struct user32
) || (regno
& 3))
961 * Other dummy fields in the virtual user structure
973 static long ptrace32_siginfo(unsigned request
, u32 pid
, u32 addr
, u32 data
)
975 siginfo_t __user
*si
= compat_alloc_user_space(sizeof(siginfo_t
));
976 compat_siginfo_t __user
*si32
= compat_ptr(data
);
980 if (request
== PTRACE_SETSIGINFO
) {
981 memset(&ssi
, 0, sizeof(siginfo_t
));
982 ret
= copy_siginfo_from_user32(&ssi
, si32
);
985 if (copy_to_user(si
, &ssi
, sizeof(siginfo_t
)))
988 ret
= sys_ptrace(request
, pid
, addr
, (unsigned long)si
);
991 if (request
== PTRACE_GETSIGINFO
) {
992 if (copy_from_user(&ssi
, si
, sizeof(siginfo_t
)))
994 ret
= copy_siginfo_to_user32(si32
, &ssi
);
999 asmlinkage
long sys32_ptrace(long request
, u32 pid
, u32 addr
, u32 data
)
1001 struct task_struct
*child
;
1002 struct pt_regs
*childregs
;
1003 void __user
*datap
= compat_ptr(data
);
1008 case PTRACE_TRACEME
:
1012 case PTRACE_SINGLESTEP
:
1013 case PTRACE_SINGLEBLOCK
:
1015 case PTRACE_SYSCALL
:
1016 case PTRACE_OLDSETOPTIONS
:
1017 case PTRACE_SETOPTIONS
:
1018 case PTRACE_SET_THREAD_AREA
:
1019 case PTRACE_GET_THREAD_AREA
:
1020 case PTRACE_BTS_MAX_BUFFER_SIZE
:
1021 case PTRACE_BTS_ALLOCATE_BUFFER
:
1022 case PTRACE_BTS_GET_BUFFER_SIZE
:
1023 case PTRACE_BTS_READ_RECORD
:
1024 case PTRACE_BTS_CONFIG
:
1025 case PTRACE_BTS_STATUS
:
1026 return sys_ptrace(request
, pid
, addr
, data
);
1031 case PTRACE_PEEKTEXT
:
1032 case PTRACE_PEEKDATA
:
1033 case PTRACE_POKEDATA
:
1034 case PTRACE_POKETEXT
:
1035 case PTRACE_POKEUSR
:
1036 case PTRACE_PEEKUSR
:
1037 case PTRACE_GETREGS
:
1038 case PTRACE_SETREGS
:
1039 case PTRACE_SETFPREGS
:
1040 case PTRACE_GETFPREGS
:
1041 case PTRACE_SETFPXREGS
:
1042 case PTRACE_GETFPXREGS
:
1043 case PTRACE_GETEVENTMSG
:
1046 case PTRACE_SETSIGINFO
:
1047 case PTRACE_GETSIGINFO
:
1048 return ptrace32_siginfo(request
, pid
, addr
, data
);
1051 child
= ptrace_get_task_struct(pid
);
1053 return PTR_ERR(child
);
1055 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
1059 childregs
= task_pt_regs(child
);
1062 case PTRACE_PEEKDATA
:
1063 case PTRACE_PEEKTEXT
:
1065 if (access_process_vm(child
, addr
, &val
, sizeof(u32
), 0) !=
1069 ret
= put_user(val
, (unsigned int __user
*)datap
);
1072 case PTRACE_POKEDATA
:
1073 case PTRACE_POKETEXT
:
1075 if (access_process_vm(child
, addr
, &data
, sizeof(u32
), 1) !=
1080 case PTRACE_PEEKUSR
:
1081 ret
= getreg32(child
, addr
, &val
);
1083 ret
= put_user(val
, (__u32 __user
*)datap
);
1086 case PTRACE_POKEUSR
:
1087 ret
= putreg32(child
, addr
, data
);
1090 case PTRACE_GETREGS
: { /* Get all gp regs from the child. */
1093 if (!access_ok(VERIFY_WRITE
, datap
, 16*4)) {
1098 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(__u32
)) {
1099 getreg32(child
, i
, &val
);
1100 ret
|= __put_user(val
, (u32 __user
*)datap
);
1101 datap
+= sizeof(u32
);
1106 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
1110 if (!access_ok(VERIFY_READ
, datap
, 16*4)) {
1115 for (i
= 0; i
< sizeof(struct user_regs_struct32
); i
+= sizeof(u32
)) {
1116 ret
|= __get_user(tmp
, (u32 __user
*)datap
);
1117 putreg32(child
, i
, tmp
);
1118 datap
+= sizeof(u32
);
1123 case PTRACE_GETFPREGS
:
1125 if (!access_ok(VERIFY_READ
, compat_ptr(data
),
1126 sizeof(struct user_i387_struct
)))
1128 save_i387_ia32(child
, datap
, childregs
, 1);
1132 case PTRACE_SETFPREGS
:
1134 if (!access_ok(VERIFY_WRITE
, datap
,
1135 sizeof(struct user_i387_struct
)))
1138 /* don't check EFAULT to be bug-to-bug compatible to i386 */
1139 restore_i387_ia32(child
, datap
, 1);
1142 case PTRACE_GETFPXREGS
: {
1143 struct user32_fxsr_struct __user
*u
= datap
;
1147 if (!access_ok(VERIFY_WRITE
, u
, sizeof(*u
)))
1150 if (__copy_to_user(u
, &child
->thread
.i387
.fxsave
, sizeof(*u
)))
1152 ret
= __put_user(childregs
->cs
, &u
->fcs
);
1153 ret
|= __put_user(child
->thread
.ds
, &u
->fos
);
1156 case PTRACE_SETFPXREGS
: {
1157 struct user32_fxsr_struct __user
*u
= datap
;
1161 if (!access_ok(VERIFY_READ
, u
, sizeof(*u
)))
1164 * no checking to be bug-to-bug compatible with i386.
1165 * but silence warning
1167 if (__copy_from_user(&child
->thread
.i387
.fxsave
, u
, sizeof(*u
)))
1169 set_stopped_child_used_math(child
);
1170 child
->thread
.i387
.fxsave
.mxcsr
&= mxcsr_feature_mask
;
1175 case PTRACE_GETEVENTMSG
:
1176 ret
= put_user(child
->ptrace_message
,
1177 (unsigned int __user
*)compat_ptr(data
));
1185 put_task_struct(child
);
1189 #endif /* CONFIG_IA32_EMULATION */
1191 #ifdef CONFIG_X86_32
1193 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
, int error_code
)
1195 struct siginfo info
;
1197 tsk
->thread
.trap_no
= 1;
1198 tsk
->thread
.error_code
= error_code
;
1200 memset(&info
, 0, sizeof(info
));
1201 info
.si_signo
= SIGTRAP
;
1202 info
.si_code
= TRAP_BRKPT
;
1205 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1207 /* Send us the fake SIGTRAP */
1208 force_sig_info(SIGTRAP
, &info
, tsk
);
1211 /* notification of system call entry/exit
1212 * - triggered by current->work.syscall_trace
1214 __attribute__((regparm(3)))
1215 int do_syscall_trace(struct pt_regs
*regs
, int entryexit
)
1217 int is_sysemu
= test_thread_flag(TIF_SYSCALL_EMU
);
1219 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1222 int is_singlestep
= !is_sysemu
&& test_thread_flag(TIF_SINGLESTEP
);
1225 /* do the secure computing check first */
1227 secure_computing(regs
->orig_ax
);
1229 if (unlikely(current
->audit_context
)) {
1231 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
),
1233 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1234 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1235 * not used, entry.S will call us only on syscall exit, not
1236 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1237 * calling send_sigtrap() on syscall entry.
1239 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1240 * is_singlestep is false, despite his name, so we will still do
1241 * the correct thing.
1243 else if (is_singlestep
)
1247 if (!(current
->ptrace
& PT_PTRACED
))
1250 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1251 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1252 * here. We have to check this and return */
1253 if (is_sysemu
&& entryexit
)
1256 /* Fake a debug trap */
1258 send_sigtrap(current
, regs
, 0);
1260 if (!test_thread_flag(TIF_SYSCALL_TRACE
) && !is_sysemu
)
1263 /* the 0x80 provides a way for the tracing parent to distinguish
1264 between a syscall stop and SIGTRAP delivery */
1265 /* Note that the debugger could change the result of test_thread_flag!*/
1266 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80:0));
1269 * this isn't the same as continuing with a signal, but it will do
1270 * for normal use. strace only continues with a signal if the
1271 * stopping signal is not SIGTRAP. -brl
1273 if (current
->exit_code
) {
1274 send_sig(current
->exit_code
, current
, 1);
1275 current
->exit_code
= 0;
1279 if (unlikely(current
->audit_context
) && !entryexit
)
1280 audit_syscall_entry(AUDIT_ARCH_I386
, regs
->orig_ax
,
1281 regs
->bx
, regs
->cx
, regs
->dx
, regs
->si
);
1285 regs
->orig_ax
= -1; /* force skip of syscall restarting */
1286 if (unlikely(current
->audit_context
))
1287 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1291 #else /* CONFIG_X86_64 */
1293 static void syscall_trace(struct pt_regs
*regs
)
1297 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1299 regs
->ip
, regs
->sp
, regs
->ax
, regs
->orig_ax
, __builtin_return_address(0),
1300 current_thread_info()->flags
, current
->ptrace
);
1303 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
1306 * this isn't the same as continuing with a signal, but it will do
1307 * for normal use. strace only continues with a signal if the
1308 * stopping signal is not SIGTRAP. -brl
1310 if (current
->exit_code
) {
1311 send_sig(current
->exit_code
, current
, 1);
1312 current
->exit_code
= 0;
1316 asmlinkage
void syscall_trace_enter(struct pt_regs
*regs
)
1318 /* do the secure computing check first */
1319 secure_computing(regs
->orig_ax
);
1321 if (test_thread_flag(TIF_SYSCALL_TRACE
)
1322 && (current
->ptrace
& PT_PTRACED
))
1323 syscall_trace(regs
);
1325 if (unlikely(current
->audit_context
)) {
1326 if (test_thread_flag(TIF_IA32
)) {
1327 audit_syscall_entry(AUDIT_ARCH_I386
,
1330 regs
->dx
, regs
->si
);
1332 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1335 regs
->dx
, regs
->r10
);
1340 asmlinkage
void syscall_trace_leave(struct pt_regs
*regs
)
1342 if (unlikely(current
->audit_context
))
1343 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1345 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
1346 || test_thread_flag(TIF_SINGLESTEP
))
1347 && (current
->ptrace
& PT_PTRACED
))
1348 syscall_trace(regs
);
1351 #endif /* CONFIG_X86_32 */