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>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/debugreg.h>
33 #include <asm/prctl.h>
34 #include <asm/proto.h>
37 #include <trace/syscall.h>
45 REGSET_IOPERM64
= REGSET_XFP
,
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
56 * Determines which flags the user has access to [1 = access, 0 = no access].
58 #define FLAG_MASK_32 ((unsigned long) \
59 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
60 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
61 X86_EFLAGS_SF | X86_EFLAGS_TF | \
62 X86_EFLAGS_DF | X86_EFLAGS_OF | \
63 X86_EFLAGS_RF | X86_EFLAGS_AC))
66 * Determines whether a value may be installed in a segment register.
68 static inline bool invalid_selector(u16 value
)
70 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
75 #define FLAG_MASK FLAG_MASK_32
77 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
79 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
80 return ®s
->bx
+ (regno
>> 2);
83 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
86 * Returning the value truncates it to 16 bits.
89 if (offset
!= offsetof(struct user_regs_struct
, gs
))
90 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
93 retval
= get_user_gs(task_pt_regs(task
));
95 retval
= task_user_gs(task
);
100 static int set_segment_reg(struct task_struct
*task
,
101 unsigned long offset
, u16 value
)
104 * The value argument was already truncated to 16 bits.
106 if (invalid_selector(value
))
110 * For %cs and %ss we cannot permit a null selector.
111 * We can permit a bogus selector as long as it has USER_RPL.
112 * Null selectors are fine for other segment registers, but
113 * we will never get back to user mode with invalid %cs or %ss
114 * and will take the trap in iret instead. Much code relies
115 * on user_mode() to distinguish a user trap frame (which can
116 * safely use invalid selectors) from a kernel trap frame.
119 case offsetof(struct user_regs_struct
, cs
):
120 case offsetof(struct user_regs_struct
, ss
):
121 if (unlikely(value
== 0))
125 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
128 case offsetof(struct user_regs_struct
, gs
):
130 set_user_gs(task_pt_regs(task
), value
);
132 task_user_gs(task
) = value
;
138 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
140 return TASK_SIZE
- 3;
143 #else /* CONFIG_X86_64 */
145 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
147 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
149 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
150 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
153 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
156 * Returning the value truncates it to 16 bits.
161 case offsetof(struct user_regs_struct
, fs
):
162 if (task
== current
) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg
));
167 return task
->thread
.fsindex
;
168 case offsetof(struct user_regs_struct
, gs
):
169 if (task
== current
) {
170 asm("movl %%gs,%0" : "=r" (seg
));
173 return task
->thread
.gsindex
;
174 case offsetof(struct user_regs_struct
, ds
):
175 if (task
== current
) {
176 asm("movl %%ds,%0" : "=r" (seg
));
179 return task
->thread
.ds
;
180 case offsetof(struct user_regs_struct
, es
):
181 if (task
== current
) {
182 asm("movl %%es,%0" : "=r" (seg
));
185 return task
->thread
.es
;
187 case offsetof(struct user_regs_struct
, cs
):
188 case offsetof(struct user_regs_struct
, ss
):
191 return *pt_regs_access(task_pt_regs(task
), offset
);
194 static int set_segment_reg(struct task_struct
*task
,
195 unsigned long offset
, u16 value
)
198 * The value argument was already truncated to 16 bits.
200 if (invalid_selector(value
))
204 case offsetof(struct user_regs_struct
,fs
):
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
209 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
210 task
->thread
.fs
!= 0) ||
211 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
212 task
->thread
.fs
== 0))
214 task
->thread
.fsindex
= value
;
216 loadsegment(fs
, task
->thread
.fsindex
);
218 case offsetof(struct user_regs_struct
,gs
):
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
223 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
224 task
->thread
.gs
!= 0) ||
225 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
226 task
->thread
.gs
== 0))
228 task
->thread
.gsindex
= value
;
230 load_gs_index(task
->thread
.gsindex
);
232 case offsetof(struct user_regs_struct
,ds
):
233 task
->thread
.ds
= value
;
235 loadsegment(ds
, task
->thread
.ds
);
237 case offsetof(struct user_regs_struct
,es
):
238 task
->thread
.es
= value
;
240 loadsegment(es
, task
->thread
.es
);
244 * Can't actually change these in 64-bit mode.
246 case offsetof(struct user_regs_struct
,cs
):
247 if (unlikely(value
== 0))
249 #ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task
, TIF_IA32
))
251 task_pt_regs(task
)->cs
= value
;
254 case offsetof(struct user_regs_struct
,ss
):
255 if (unlikely(value
== 0))
257 #ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task
, TIF_IA32
))
259 task_pt_regs(task
)->ss
= value
;
267 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
269 #ifdef CONFIG_IA32_EMULATION
270 if (test_tsk_thread_flag(task
, TIF_IA32
))
271 return IA32_PAGE_OFFSET
- 3;
273 return TASK_SIZE_MAX
- 7;
276 #endif /* CONFIG_X86_32 */
278 static unsigned long get_flags(struct task_struct
*task
)
280 unsigned long retval
= task_pt_regs(task
)->flags
;
283 * If the debugger set TF, hide it from the readout.
285 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
286 retval
&= ~X86_EFLAGS_TF
;
291 static int set_flags(struct task_struct
*task
, unsigned long value
)
293 struct pt_regs
*regs
= task_pt_regs(task
);
296 * If the user value contains TF, mark that
297 * it was not "us" (the debugger) that set it.
298 * If not, make sure it stays set if we had.
300 if (value
& X86_EFLAGS_TF
)
301 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
302 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
303 value
|= X86_EFLAGS_TF
;
305 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
310 static int putreg(struct task_struct
*child
,
311 unsigned long offset
, unsigned long value
)
314 case offsetof(struct user_regs_struct
, cs
):
315 case offsetof(struct user_regs_struct
, ds
):
316 case offsetof(struct user_regs_struct
, es
):
317 case offsetof(struct user_regs_struct
, fs
):
318 case offsetof(struct user_regs_struct
, gs
):
319 case offsetof(struct user_regs_struct
, ss
):
320 return set_segment_reg(child
, offset
, value
);
322 case offsetof(struct user_regs_struct
, flags
):
323 return set_flags(child
, value
);
327 * Orig_ax is really just a flag with small positive and
328 * negative values, so make sure to always sign-extend it
329 * from 32 bits so that it works correctly regardless of
330 * whether we come from a 32-bit environment or not.
332 case offsetof(struct user_regs_struct
, orig_ax
):
333 value
= (long) (s32
) value
;
336 case offsetof(struct user_regs_struct
,fs_base
):
337 if (value
>= TASK_SIZE_OF(child
))
340 * When changing the segment base, use do_arch_prctl
341 * to set either thread.fs or thread.fsindex and the
342 * corresponding GDT slot.
344 if (child
->thread
.fs
!= value
)
345 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
347 case offsetof(struct user_regs_struct
,gs_base
):
349 * Exactly the same here as the %fs handling above.
351 if (value
>= TASK_SIZE_OF(child
))
353 if (child
->thread
.gs
!= value
)
354 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
359 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
363 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
366 case offsetof(struct user_regs_struct
, cs
):
367 case offsetof(struct user_regs_struct
, ds
):
368 case offsetof(struct user_regs_struct
, es
):
369 case offsetof(struct user_regs_struct
, fs
):
370 case offsetof(struct user_regs_struct
, gs
):
371 case offsetof(struct user_regs_struct
, ss
):
372 return get_segment_reg(task
, offset
);
374 case offsetof(struct user_regs_struct
, flags
):
375 return get_flags(task
);
378 case offsetof(struct user_regs_struct
, fs_base
): {
380 * do_arch_prctl may have used a GDT slot instead of
381 * the MSR. To userland, it appears the same either
382 * way, except the %fs segment selector might not be 0.
384 unsigned int seg
= task
->thread
.fsindex
;
385 if (task
->thread
.fs
!= 0)
386 return task
->thread
.fs
;
388 asm("movl %%fs,%0" : "=r" (seg
));
389 if (seg
!= FS_TLS_SEL
)
391 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
393 case offsetof(struct user_regs_struct
, gs_base
): {
395 * Exactly the same here as the %fs handling above.
397 unsigned int seg
= task
->thread
.gsindex
;
398 if (task
->thread
.gs
!= 0)
399 return task
->thread
.gs
;
401 asm("movl %%gs,%0" : "=r" (seg
));
402 if (seg
!= GS_TLS_SEL
)
404 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
409 return *pt_regs_access(task_pt_regs(task
), offset
);
412 static int genregs_get(struct task_struct
*target
,
413 const struct user_regset
*regset
,
414 unsigned int pos
, unsigned int count
,
415 void *kbuf
, void __user
*ubuf
)
418 unsigned long *k
= kbuf
;
420 *k
++ = getreg(target
, pos
);
425 unsigned long __user
*u
= ubuf
;
427 if (__put_user(getreg(target
, pos
), u
++))
437 static int genregs_set(struct task_struct
*target
,
438 const struct user_regset
*regset
,
439 unsigned int pos
, unsigned int count
,
440 const void *kbuf
, const void __user
*ubuf
)
444 const unsigned long *k
= kbuf
;
445 while (count
> 0 && !ret
) {
446 ret
= putreg(target
, pos
, *k
++);
451 const unsigned long __user
*u
= ubuf
;
452 while (count
> 0 && !ret
) {
454 ret
= __get_user(word
, u
++);
457 ret
= putreg(target
, pos
, word
);
466 * This function is trivial and will be inlined by the compiler.
467 * Having it separates the implementation details of debug
468 * registers from the interface details of ptrace.
470 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
473 case 0: return child
->thread
.debugreg0
;
474 case 1: return child
->thread
.debugreg1
;
475 case 2: return child
->thread
.debugreg2
;
476 case 3: return child
->thread
.debugreg3
;
477 case 6: return child
->thread
.debugreg6
;
478 case 7: return child
->thread
.debugreg7
;
483 static int ptrace_set_debugreg(struct task_struct
*child
,
484 int n
, unsigned long data
)
488 if (unlikely(n
== 4 || n
== 5))
491 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
495 case 0: child
->thread
.debugreg0
= data
; break;
496 case 1: child
->thread
.debugreg1
= data
; break;
497 case 2: child
->thread
.debugreg2
= data
; break;
498 case 3: child
->thread
.debugreg3
= data
; break;
501 if ((data
& ~0xffffffffUL
) != 0)
503 child
->thread
.debugreg6
= data
;
508 * Sanity-check data. Take one half-byte at once with
509 * check = (val >> (16 + 4*i)) & 0xf. It contains the
510 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
511 * 2 and 3 are LENi. Given a list of invalid values,
512 * we do mask |= 1 << invalid_value, so that
513 * (mask >> check) & 1 is a correct test for invalid
516 * R/Wi contains the type of the breakpoint /
517 * watchpoint, LENi contains the length of the watched
518 * data in the watchpoint case.
520 * The invalid values are:
521 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
522 * - R/Wi == 0x10 (break on I/O reads or writes), so
524 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
527 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
529 * See the Intel Manual "System Programming Guide",
532 * Note that LENi == 0x10 is defined on x86_64 in long
533 * mode (i.e. even for 32-bit userspace software, but
534 * 64-bit kernel), so the x86_64 mask value is 0x5454.
535 * See the AMD manual no. 24593 (AMD64 System Programming)
538 #define DR7_MASK 0x5f54
540 #define DR7_MASK 0x5554
542 data
&= ~DR_CONTROL_RESERVED
;
543 for (i
= 0; i
< 4; i
++)
544 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
546 child
->thread
.debugreg7
= data
;
548 set_tsk_thread_flag(child
, TIF_DEBUG
);
550 clear_tsk_thread_flag(child
, TIF_DEBUG
);
558 * These access the current or another (stopped) task's io permission
559 * bitmap for debugging or core dump.
561 static int ioperm_active(struct task_struct
*target
,
562 const struct user_regset
*regset
)
564 return target
->thread
.io_bitmap_max
/ regset
->size
;
567 static int ioperm_get(struct task_struct
*target
,
568 const struct user_regset
*regset
,
569 unsigned int pos
, unsigned int count
,
570 void *kbuf
, void __user
*ubuf
)
572 if (!target
->thread
.io_bitmap_ptr
)
575 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
576 target
->thread
.io_bitmap_ptr
,
580 #ifdef CONFIG_X86_PTRACE_BTS
581 static int ptrace_bts_read_record(struct task_struct
*child
, size_t index
,
582 struct bts_struct __user
*out
)
584 const struct bts_trace
*trace
;
585 struct bts_struct bts
;
586 const unsigned char *at
;
589 trace
= ds_read_bts(child
->bts
);
593 at
= trace
->ds
.top
- ((index
+ 1) * trace
->ds
.size
);
594 if ((void *)at
< trace
->ds
.begin
)
595 at
+= (trace
->ds
.n
* trace
->ds
.size
);
600 error
= trace
->read(child
->bts
, at
, &bts
);
604 if (copy_to_user(out
, &bts
, sizeof(bts
)))
610 static int ptrace_bts_drain(struct task_struct
*child
,
612 struct bts_struct __user
*out
)
614 const struct bts_trace
*trace
;
615 const unsigned char *at
;
616 int error
, drained
= 0;
618 trace
= ds_read_bts(child
->bts
);
625 if (size
< (trace
->ds
.top
- trace
->ds
.begin
))
628 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
629 out
++, drained
++, at
+= trace
->ds
.size
) {
630 struct bts_struct bts
;
633 error
= trace
->read(child
->bts
, at
, &bts
);
637 if (copy_to_user(out
, &bts
, sizeof(bts
)))
641 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
643 error
= ds_reset_bts(child
->bts
);
650 static int ptrace_bts_allocate_buffer(struct task_struct
*child
, size_t size
)
652 child
->bts_buffer
= alloc_locked_buffer(size
);
653 if (!child
->bts_buffer
)
656 child
->bts_size
= size
;
661 static void ptrace_bts_free_buffer(struct task_struct
*child
)
663 free_locked_buffer(child
->bts_buffer
, child
->bts_size
);
664 child
->bts_buffer
= NULL
;
668 static int ptrace_bts_config(struct task_struct
*child
,
670 const struct ptrace_bts_config __user
*ucfg
)
672 struct ptrace_bts_config cfg
;
673 unsigned int flags
= 0;
675 if (cfg_size
< sizeof(cfg
))
678 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
682 ds_release_bts(child
->bts
);
686 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
690 child
->thread
.bts_ovfl_signal
= cfg
.signal
;
694 if ((cfg
.flags
& PTRACE_BTS_O_ALLOC
) &&
695 (cfg
.size
!= child
->bts_size
)) {
698 ptrace_bts_free_buffer(child
);
700 error
= ptrace_bts_allocate_buffer(child
, cfg
.size
);
705 if (cfg
.flags
& PTRACE_BTS_O_TRACE
)
708 if (cfg
.flags
& PTRACE_BTS_O_SCHED
)
709 flags
|= BTS_TIMESTAMPS
;
711 child
->bts
= ds_request_bts(child
, child
->bts_buffer
, child
->bts_size
,
712 /* ovfl = */ NULL
, /* th = */ (size_t)-1,
714 if (IS_ERR(child
->bts
)) {
715 int error
= PTR_ERR(child
->bts
);
717 ptrace_bts_free_buffer(child
);
726 static int ptrace_bts_status(struct task_struct
*child
,
728 struct ptrace_bts_config __user
*ucfg
)
730 const struct bts_trace
*trace
;
731 struct ptrace_bts_config cfg
;
733 if (cfg_size
< sizeof(cfg
))
736 trace
= ds_read_bts(child
->bts
);
740 memset(&cfg
, 0, sizeof(cfg
));
741 cfg
.size
= trace
->ds
.end
- trace
->ds
.begin
;
742 cfg
.signal
= child
->thread
.bts_ovfl_signal
;
743 cfg
.bts_size
= sizeof(struct bts_struct
);
746 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
748 if (trace
->ds
.flags
& BTS_USER
)
749 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
751 if (trace
->ds
.flags
& BTS_TIMESTAMPS
)
752 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
754 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
760 static int ptrace_bts_clear(struct task_struct
*child
)
762 const struct bts_trace
*trace
;
764 trace
= ds_read_bts(child
->bts
);
768 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
770 return ds_reset_bts(child
->bts
);
773 static int ptrace_bts_size(struct task_struct
*child
)
775 const struct bts_trace
*trace
;
777 trace
= ds_read_bts(child
->bts
);
781 return (trace
->ds
.top
- trace
->ds
.begin
) / trace
->ds
.size
;
784 static void ptrace_bts_fork(struct task_struct
*tsk
)
787 tsk
->bts_buffer
= NULL
;
789 tsk
->thread
.bts_ovfl_signal
= 0;
792 static void ptrace_bts_untrace(struct task_struct
*child
)
794 if (unlikely(child
->bts
)) {
795 ds_release_bts(child
->bts
);
798 /* We cannot update total_vm and locked_vm since
799 child's mm is already gone. But we can reclaim the
801 kfree(child
->bts_buffer
);
802 child
->bts_buffer
= NULL
;
807 static void ptrace_bts_detach(struct task_struct
*child
)
810 * Ptrace_detach() races with ptrace_untrace() in case
811 * the child dies and is reaped by another thread.
813 * We only do the memory accounting at this point and
814 * leave the buffer deallocation and the bts tracer
815 * release to ptrace_bts_untrace() which will be called
816 * later on with tasklist_lock held.
818 release_locked_buffer(child
->bts_buffer
, child
->bts_size
);
821 static inline void ptrace_bts_fork(struct task_struct
*tsk
) {}
822 static inline void ptrace_bts_detach(struct task_struct
*child
) {}
823 static inline void ptrace_bts_untrace(struct task_struct
*child
) {}
824 #endif /* CONFIG_X86_PTRACE_BTS */
826 void x86_ptrace_fork(struct task_struct
*child
, unsigned long clone_flags
)
828 ptrace_bts_fork(child
);
831 void x86_ptrace_untrace(struct task_struct
*child
)
833 ptrace_bts_untrace(child
);
837 * Called by kernel/ptrace.c when detaching..
839 * Make sure the single step bit is not set.
841 void ptrace_disable(struct task_struct
*child
)
843 user_disable_single_step(child
);
844 #ifdef TIF_SYSCALL_EMU
845 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
847 ptrace_bts_detach(child
);
850 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
851 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
854 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
857 unsigned long __user
*datap
= (unsigned long __user
*)data
;
860 /* read the word at location addr in the USER area. */
861 case PTRACE_PEEKUSR
: {
865 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
866 addr
>= sizeof(struct user
))
869 tmp
= 0; /* Default return condition */
870 if (addr
< sizeof(struct user_regs_struct
))
871 tmp
= getreg(child
, addr
);
872 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
873 addr
<= offsetof(struct user
, u_debugreg
[7])) {
874 addr
-= offsetof(struct user
, u_debugreg
[0]);
875 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
877 ret
= put_user(tmp
, datap
);
881 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
883 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
884 addr
>= sizeof(struct user
))
887 if (addr
< sizeof(struct user_regs_struct
))
888 ret
= putreg(child
, addr
, data
);
889 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
890 addr
<= offsetof(struct user
, u_debugreg
[7])) {
891 addr
-= offsetof(struct user
, u_debugreg
[0]);
892 ret
= ptrace_set_debugreg(child
,
893 addr
/ sizeof(data
), data
);
897 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
898 return copy_regset_to_user(child
,
899 task_user_regset_view(current
),
901 0, sizeof(struct user_regs_struct
),
904 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
905 return copy_regset_from_user(child
,
906 task_user_regset_view(current
),
908 0, sizeof(struct user_regs_struct
),
911 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
912 return copy_regset_to_user(child
,
913 task_user_regset_view(current
),
915 0, sizeof(struct user_i387_struct
),
918 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
919 return copy_regset_from_user(child
,
920 task_user_regset_view(current
),
922 0, sizeof(struct user_i387_struct
),
926 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
927 return copy_regset_to_user(child
, &user_x86_32_view
,
929 0, sizeof(struct user_fxsr_struct
),
932 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
933 return copy_regset_from_user(child
, &user_x86_32_view
,
935 0, sizeof(struct user_fxsr_struct
),
939 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
940 case PTRACE_GET_THREAD_AREA
:
943 ret
= do_get_thread_area(child
, addr
,
944 (struct user_desc __user
*) data
);
947 case PTRACE_SET_THREAD_AREA
:
950 ret
= do_set_thread_area(child
, addr
,
951 (struct user_desc __user
*) data
, 0);
956 /* normal 64bit interface to access TLS data.
957 Works just like arch_prctl, except that the arguments
959 case PTRACE_ARCH_PRCTL
:
960 ret
= do_arch_prctl(child
, data
, addr
);
965 * These bits need more cooking - not enabled yet:
967 #ifdef CONFIG_X86_PTRACE_BTS
968 case PTRACE_BTS_CONFIG
:
969 ret
= ptrace_bts_config
970 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
973 case PTRACE_BTS_STATUS
:
974 ret
= ptrace_bts_status
975 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
978 case PTRACE_BTS_SIZE
:
979 ret
= ptrace_bts_size(child
);
983 ret
= ptrace_bts_read_record
984 (child
, data
, (struct bts_struct __user
*) addr
);
987 case PTRACE_BTS_CLEAR
:
988 ret
= ptrace_bts_clear(child
);
991 case PTRACE_BTS_DRAIN
:
992 ret
= ptrace_bts_drain
993 (child
, data
, (struct bts_struct __user
*) addr
);
995 #endif /* CONFIG_X86_PTRACE_BTS */
998 ret
= ptrace_request(child
, request
, addr
, data
);
1005 #ifdef CONFIG_IA32_EMULATION
1007 #include <linux/compat.h>
1008 #include <linux/syscalls.h>
1009 #include <asm/ia32.h>
1010 #include <asm/user32.h>
1013 case offsetof(struct user32, regs.l): \
1014 regs->q = value; break
1017 case offsetof(struct user32, regs.rs): \
1018 return set_segment_reg(child, \
1019 offsetof(struct user_regs_struct, rs), \
1023 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
1025 struct pt_regs
*regs
= task_pt_regs(child
);
1046 case offsetof(struct user32
, regs
.orig_eax
):
1048 * Sign-extend the value so that orig_eax = -1
1049 * causes (long)orig_ax < 0 tests to fire correctly.
1051 regs
->orig_ax
= (long) (s32
) value
;
1054 case offsetof(struct user32
, regs
.eflags
):
1055 return set_flags(child
, value
);
1057 case offsetof(struct user32
, u_debugreg
[0]) ...
1058 offsetof(struct user32
, u_debugreg
[7]):
1059 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1060 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1063 if (regno
> sizeof(struct user32
) || (regno
& 3))
1067 * Other dummy fields in the virtual user structure
1079 case offsetof(struct user32, regs.l): \
1080 *val = regs->q; break
1083 case offsetof(struct user32, regs.rs): \
1084 *val = get_segment_reg(child, \
1085 offsetof(struct user_regs_struct, rs)); \
1088 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1090 struct pt_regs
*regs
= task_pt_regs(child
);
1108 R32(orig_eax
, orig_ax
);
1112 case offsetof(struct user32
, regs
.eflags
):
1113 *val
= get_flags(child
);
1116 case offsetof(struct user32
, u_debugreg
[0]) ...
1117 offsetof(struct user32
, u_debugreg
[7]):
1118 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1119 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1123 if (regno
> sizeof(struct user32
) || (regno
& 3))
1127 * Other dummy fields in the virtual user structure
1139 static int genregs32_get(struct task_struct
*target
,
1140 const struct user_regset
*regset
,
1141 unsigned int pos
, unsigned int count
,
1142 void *kbuf
, void __user
*ubuf
)
1145 compat_ulong_t
*k
= kbuf
;
1147 getreg32(target
, pos
, k
++);
1148 count
-= sizeof(*k
);
1152 compat_ulong_t __user
*u
= ubuf
;
1154 compat_ulong_t word
;
1155 getreg32(target
, pos
, &word
);
1156 if (__put_user(word
, u
++))
1158 count
-= sizeof(*u
);
1166 static int genregs32_set(struct task_struct
*target
,
1167 const struct user_regset
*regset
,
1168 unsigned int pos
, unsigned int count
,
1169 const void *kbuf
, const void __user
*ubuf
)
1173 const compat_ulong_t
*k
= kbuf
;
1174 while (count
> 0 && !ret
) {
1175 ret
= putreg32(target
, pos
, *k
++);
1176 count
-= sizeof(*k
);
1180 const compat_ulong_t __user
*u
= ubuf
;
1181 while (count
> 0 && !ret
) {
1182 compat_ulong_t word
;
1183 ret
= __get_user(word
, u
++);
1186 ret
= putreg32(target
, pos
, word
);
1187 count
-= sizeof(*u
);
1194 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1195 compat_ulong_t caddr
, compat_ulong_t cdata
)
1197 unsigned long addr
= caddr
;
1198 unsigned long data
= cdata
;
1199 void __user
*datap
= compat_ptr(data
);
1204 case PTRACE_PEEKUSR
:
1205 ret
= getreg32(child
, addr
, &val
);
1207 ret
= put_user(val
, (__u32 __user
*)datap
);
1210 case PTRACE_POKEUSR
:
1211 ret
= putreg32(child
, addr
, data
);
1214 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1215 return copy_regset_to_user(child
, &user_x86_32_view
,
1217 0, sizeof(struct user_regs_struct32
),
1220 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1221 return copy_regset_from_user(child
, &user_x86_32_view
,
1223 sizeof(struct user_regs_struct32
),
1226 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1227 return copy_regset_to_user(child
, &user_x86_32_view
,
1229 sizeof(struct user_i387_ia32_struct
),
1232 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1233 return copy_regset_from_user(
1234 child
, &user_x86_32_view
, REGSET_FP
,
1235 0, sizeof(struct user_i387_ia32_struct
), datap
);
1237 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1238 return copy_regset_to_user(child
, &user_x86_32_view
,
1240 sizeof(struct user32_fxsr_struct
),
1243 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1244 return copy_regset_from_user(child
, &user_x86_32_view
,
1246 sizeof(struct user32_fxsr_struct
),
1249 case PTRACE_GET_THREAD_AREA
:
1250 case PTRACE_SET_THREAD_AREA
:
1251 #ifdef CONFIG_X86_PTRACE_BTS
1252 case PTRACE_BTS_CONFIG
:
1253 case PTRACE_BTS_STATUS
:
1254 case PTRACE_BTS_SIZE
:
1255 case PTRACE_BTS_GET
:
1256 case PTRACE_BTS_CLEAR
:
1257 case PTRACE_BTS_DRAIN
:
1258 #endif /* CONFIG_X86_PTRACE_BTS */
1259 return arch_ptrace(child
, request
, addr
, data
);
1262 return compat_ptrace_request(child
, request
, addr
, data
);
1268 #endif /* CONFIG_IA32_EMULATION */
1270 #ifdef CONFIG_X86_64
1272 static const struct user_regset x86_64_regsets
[] = {
1273 [REGSET_GENERAL
] = {
1274 .core_note_type
= NT_PRSTATUS
,
1275 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1276 .size
= sizeof(long), .align
= sizeof(long),
1277 .get
= genregs_get
, .set
= genregs_set
1280 .core_note_type
= NT_PRFPREG
,
1281 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1282 .size
= sizeof(long), .align
= sizeof(long),
1283 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1285 [REGSET_IOPERM64
] = {
1286 .core_note_type
= NT_386_IOPERM
,
1287 .n
= IO_BITMAP_LONGS
,
1288 .size
= sizeof(long), .align
= sizeof(long),
1289 .active
= ioperm_active
, .get
= ioperm_get
1293 static const struct user_regset_view user_x86_64_view
= {
1294 .name
= "x86_64", .e_machine
= EM_X86_64
,
1295 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1298 #else /* CONFIG_X86_32 */
1300 #define user_regs_struct32 user_regs_struct
1301 #define genregs32_get genregs_get
1302 #define genregs32_set genregs_set
1304 #define user_i387_ia32_struct user_i387_struct
1305 #define user32_fxsr_struct user_fxsr_struct
1307 #endif /* CONFIG_X86_64 */
1309 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1310 static const struct user_regset x86_32_regsets
[] = {
1311 [REGSET_GENERAL
] = {
1312 .core_note_type
= NT_PRSTATUS
,
1313 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1314 .size
= sizeof(u32
), .align
= sizeof(u32
),
1315 .get
= genregs32_get
, .set
= genregs32_set
1318 .core_note_type
= NT_PRFPREG
,
1319 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1320 .size
= sizeof(u32
), .align
= sizeof(u32
),
1321 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1324 .core_note_type
= NT_PRXFPREG
,
1325 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1326 .size
= sizeof(u32
), .align
= sizeof(u32
),
1327 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1330 .core_note_type
= NT_386_TLS
,
1331 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1332 .size
= sizeof(struct user_desc
),
1333 .align
= sizeof(struct user_desc
),
1334 .active
= regset_tls_active
,
1335 .get
= regset_tls_get
, .set
= regset_tls_set
1337 [REGSET_IOPERM32
] = {
1338 .core_note_type
= NT_386_IOPERM
,
1339 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1340 .size
= sizeof(u32
), .align
= sizeof(u32
),
1341 .active
= ioperm_active
, .get
= ioperm_get
1345 static const struct user_regset_view user_x86_32_view
= {
1346 .name
= "i386", .e_machine
= EM_386
,
1347 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1351 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1353 #ifdef CONFIG_IA32_EMULATION
1354 if (test_tsk_thread_flag(task
, TIF_IA32
))
1356 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1357 return &user_x86_32_view
;
1359 #ifdef CONFIG_X86_64
1360 return &user_x86_64_view
;
1364 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1365 int error_code
, int si_code
)
1367 struct siginfo info
;
1369 tsk
->thread
.trap_no
= 1;
1370 tsk
->thread
.error_code
= error_code
;
1372 memset(&info
, 0, sizeof(info
));
1373 info
.si_signo
= SIGTRAP
;
1374 info
.si_code
= si_code
;
1377 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1379 /* Send us the fake SIGTRAP */
1380 force_sig_info(SIGTRAP
, &info
, tsk
);
1384 #ifdef CONFIG_X86_32
1386 #elif defined CONFIG_IA32_EMULATION
1387 # define IS_IA32 is_compat_task()
1393 * We must return the syscall number to actually look up in the table.
1394 * This can be -1L to skip running any syscall at all.
1396 asmregparm
long syscall_trace_enter(struct pt_regs
*regs
)
1401 * If we stepped into a sysenter/syscall insn, it trapped in
1402 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1403 * If user-mode had set TF itself, then it's still clear from
1404 * do_debug() and we need to set it again to restore the user
1405 * state. If we entered on the slow path, TF was already set.
1407 if (test_thread_flag(TIF_SINGLESTEP
))
1408 regs
->flags
|= X86_EFLAGS_TF
;
1410 /* do the secure computing check first */
1411 secure_computing(regs
->orig_ax
);
1413 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1416 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1417 tracehook_report_syscall_entry(regs
))
1420 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE
)))
1421 ftrace_syscall_enter(regs
);
1423 if (unlikely(current
->audit_context
)) {
1425 audit_syscall_entry(AUDIT_ARCH_I386
,
1428 regs
->dx
, regs
->si
);
1429 #ifdef CONFIG_X86_64
1431 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1434 regs
->dx
, regs
->r10
);
1438 return ret
?: regs
->orig_ax
;
1441 asmregparm
void syscall_trace_leave(struct pt_regs
*regs
)
1443 if (unlikely(current
->audit_context
))
1444 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1446 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE
)))
1447 ftrace_syscall_exit(regs
);
1449 if (test_thread_flag(TIF_SYSCALL_TRACE
))
1450 tracehook_report_syscall_exit(regs
, 0);
1453 * If TIF_SYSCALL_EMU is set, we only get here because of
1454 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1455 * We already reported this syscall instruction in
1456 * syscall_trace_enter(), so don't do any more now.
1458 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1462 * If we are single-stepping, synthesize a trap to follow the
1463 * system call instruction.
1465 if (test_thread_flag(TIF_SINGLESTEP
) &&
1466 tracehook_consider_fatal_signal(current
, SIGTRAP
))
1467 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);