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>
43 REGSET_IOPERM64
= REGSET_XFP
,
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
54 * Determines which flags the user has access to [1 = access, 0 = no access].
56 #define FLAG_MASK_32 ((unsigned long) \
57 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
58 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
59 X86_EFLAGS_SF | X86_EFLAGS_TF | \
60 X86_EFLAGS_DF | X86_EFLAGS_OF | \
61 X86_EFLAGS_RF | X86_EFLAGS_AC))
64 * Determines whether a value may be installed in a segment register.
66 static inline bool invalid_selector(u16 value
)
68 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
73 #define FLAG_MASK FLAG_MASK_32
75 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
77 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
78 return ®s
->bx
+ (regno
>> 2);
81 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
84 * Returning the value truncates it to 16 bits.
87 if (offset
!= offsetof(struct user_regs_struct
, gs
))
88 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
91 retval
= get_user_gs(task_pt_regs(task
));
93 retval
= task_user_gs(task
);
98 static int set_segment_reg(struct task_struct
*task
,
99 unsigned long offset
, u16 value
)
102 * The value argument was already truncated to 16 bits.
104 if (invalid_selector(value
))
108 * For %cs and %ss we cannot permit a null selector.
109 * We can permit a bogus selector as long as it has USER_RPL.
110 * Null selectors are fine for other segment registers, but
111 * we will never get back to user mode with invalid %cs or %ss
112 * and will take the trap in iret instead. Much code relies
113 * on user_mode() to distinguish a user trap frame (which can
114 * safely use invalid selectors) from a kernel trap frame.
117 case offsetof(struct user_regs_struct
, cs
):
118 case offsetof(struct user_regs_struct
, ss
):
119 if (unlikely(value
== 0))
123 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
126 case offsetof(struct user_regs_struct
, gs
):
128 set_user_gs(task_pt_regs(task
), value
);
130 task_user_gs(task
) = value
;
136 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
138 return TASK_SIZE
- 3;
141 #else /* CONFIG_X86_64 */
143 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
145 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
147 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
148 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
151 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
154 * Returning the value truncates it to 16 bits.
159 case offsetof(struct user_regs_struct
, fs
):
160 if (task
== current
) {
161 /* Older gas can't assemble movq %?s,%r?? */
162 asm("movl %%fs,%0" : "=r" (seg
));
165 return task
->thread
.fsindex
;
166 case offsetof(struct user_regs_struct
, gs
):
167 if (task
== current
) {
168 asm("movl %%gs,%0" : "=r" (seg
));
171 return task
->thread
.gsindex
;
172 case offsetof(struct user_regs_struct
, ds
):
173 if (task
== current
) {
174 asm("movl %%ds,%0" : "=r" (seg
));
177 return task
->thread
.ds
;
178 case offsetof(struct user_regs_struct
, es
):
179 if (task
== current
) {
180 asm("movl %%es,%0" : "=r" (seg
));
183 return task
->thread
.es
;
185 case offsetof(struct user_regs_struct
, cs
):
186 case offsetof(struct user_regs_struct
, ss
):
189 return *pt_regs_access(task_pt_regs(task
), offset
);
192 static int set_segment_reg(struct task_struct
*task
,
193 unsigned long offset
, u16 value
)
196 * The value argument was already truncated to 16 bits.
198 if (invalid_selector(value
))
202 case offsetof(struct user_regs_struct
,fs
):
204 * If this is setting fs as for normal 64-bit use but
205 * setting fs_base has implicitly changed it, leave it.
207 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
208 task
->thread
.fs
!= 0) ||
209 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
210 task
->thread
.fs
== 0))
212 task
->thread
.fsindex
= value
;
214 loadsegment(fs
, task
->thread
.fsindex
);
216 case offsetof(struct user_regs_struct
,gs
):
218 * If this is setting gs as for normal 64-bit use but
219 * setting gs_base has implicitly changed it, leave it.
221 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
222 task
->thread
.gs
!= 0) ||
223 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
224 task
->thread
.gs
== 0))
226 task
->thread
.gsindex
= value
;
228 load_gs_index(task
->thread
.gsindex
);
230 case offsetof(struct user_regs_struct
,ds
):
231 task
->thread
.ds
= value
;
233 loadsegment(ds
, task
->thread
.ds
);
235 case offsetof(struct user_regs_struct
,es
):
236 task
->thread
.es
= value
;
238 loadsegment(es
, task
->thread
.es
);
242 * Can't actually change these in 64-bit mode.
244 case offsetof(struct user_regs_struct
,cs
):
245 if (unlikely(value
== 0))
247 #ifdef CONFIG_IA32_EMULATION
248 if (test_tsk_thread_flag(task
, TIF_IA32
))
249 task_pt_regs(task
)->cs
= value
;
252 case offsetof(struct user_regs_struct
,ss
):
253 if (unlikely(value
== 0))
255 #ifdef CONFIG_IA32_EMULATION
256 if (test_tsk_thread_flag(task
, TIF_IA32
))
257 task_pt_regs(task
)->ss
= value
;
265 static unsigned long debugreg_addr_limit(struct task_struct
*task
)
267 #ifdef CONFIG_IA32_EMULATION
268 if (test_tsk_thread_flag(task
, TIF_IA32
))
269 return IA32_PAGE_OFFSET
- 3;
271 return TASK_SIZE_MAX
- 7;
274 #endif /* CONFIG_X86_32 */
276 static unsigned long get_flags(struct task_struct
*task
)
278 unsigned long retval
= task_pt_regs(task
)->flags
;
281 * If the debugger set TF, hide it from the readout.
283 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
284 retval
&= ~X86_EFLAGS_TF
;
289 static int set_flags(struct task_struct
*task
, unsigned long value
)
291 struct pt_regs
*regs
= task_pt_regs(task
);
294 * If the user value contains TF, mark that
295 * it was not "us" (the debugger) that set it.
296 * If not, make sure it stays set if we had.
298 if (value
& X86_EFLAGS_TF
)
299 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
300 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
301 value
|= X86_EFLAGS_TF
;
303 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
308 static int putreg(struct task_struct
*child
,
309 unsigned long offset
, unsigned long value
)
312 case offsetof(struct user_regs_struct
, cs
):
313 case offsetof(struct user_regs_struct
, ds
):
314 case offsetof(struct user_regs_struct
, es
):
315 case offsetof(struct user_regs_struct
, fs
):
316 case offsetof(struct user_regs_struct
, gs
):
317 case offsetof(struct user_regs_struct
, ss
):
318 return set_segment_reg(child
, offset
, value
);
320 case offsetof(struct user_regs_struct
, flags
):
321 return set_flags(child
, value
);
325 * Orig_ax is really just a flag with small positive and
326 * negative values, so make sure to always sign-extend it
327 * from 32 bits so that it works correctly regardless of
328 * whether we come from a 32-bit environment or not.
330 case offsetof(struct user_regs_struct
, orig_ax
):
331 value
= (long) (s32
) value
;
334 case offsetof(struct user_regs_struct
,fs_base
):
335 if (value
>= TASK_SIZE_OF(child
))
338 * When changing the segment base, use do_arch_prctl
339 * to set either thread.fs or thread.fsindex and the
340 * corresponding GDT slot.
342 if (child
->thread
.fs
!= value
)
343 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
345 case offsetof(struct user_regs_struct
,gs_base
):
347 * Exactly the same here as the %fs handling above.
349 if (value
>= TASK_SIZE_OF(child
))
351 if (child
->thread
.gs
!= value
)
352 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
357 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
361 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
364 case offsetof(struct user_regs_struct
, cs
):
365 case offsetof(struct user_regs_struct
, ds
):
366 case offsetof(struct user_regs_struct
, es
):
367 case offsetof(struct user_regs_struct
, fs
):
368 case offsetof(struct user_regs_struct
, gs
):
369 case offsetof(struct user_regs_struct
, ss
):
370 return get_segment_reg(task
, offset
);
372 case offsetof(struct user_regs_struct
, flags
):
373 return get_flags(task
);
376 case offsetof(struct user_regs_struct
, fs_base
): {
378 * do_arch_prctl may have used a GDT slot instead of
379 * the MSR. To userland, it appears the same either
380 * way, except the %fs segment selector might not be 0.
382 unsigned int seg
= task
->thread
.fsindex
;
383 if (task
->thread
.fs
!= 0)
384 return task
->thread
.fs
;
386 asm("movl %%fs,%0" : "=r" (seg
));
387 if (seg
!= FS_TLS_SEL
)
389 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
391 case offsetof(struct user_regs_struct
, gs_base
): {
393 * Exactly the same here as the %fs handling above.
395 unsigned int seg
= task
->thread
.gsindex
;
396 if (task
->thread
.gs
!= 0)
397 return task
->thread
.gs
;
399 asm("movl %%gs,%0" : "=r" (seg
));
400 if (seg
!= GS_TLS_SEL
)
402 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
407 return *pt_regs_access(task_pt_regs(task
), offset
);
410 static int genregs_get(struct task_struct
*target
,
411 const struct user_regset
*regset
,
412 unsigned int pos
, unsigned int count
,
413 void *kbuf
, void __user
*ubuf
)
416 unsigned long *k
= kbuf
;
418 *k
++ = getreg(target
, pos
);
423 unsigned long __user
*u
= ubuf
;
425 if (__put_user(getreg(target
, pos
), u
++))
435 static int genregs_set(struct task_struct
*target
,
436 const struct user_regset
*regset
,
437 unsigned int pos
, unsigned int count
,
438 const void *kbuf
, const void __user
*ubuf
)
442 const unsigned long *k
= kbuf
;
443 while (count
> 0 && !ret
) {
444 ret
= putreg(target
, pos
, *k
++);
449 const unsigned long __user
*u
= ubuf
;
450 while (count
> 0 && !ret
) {
452 ret
= __get_user(word
, u
++);
455 ret
= putreg(target
, pos
, word
);
464 * This function is trivial and will be inlined by the compiler.
465 * Having it separates the implementation details of debug
466 * registers from the interface details of ptrace.
468 static unsigned long ptrace_get_debugreg(struct task_struct
*child
, int n
)
471 case 0: return child
->thread
.debugreg0
;
472 case 1: return child
->thread
.debugreg1
;
473 case 2: return child
->thread
.debugreg2
;
474 case 3: return child
->thread
.debugreg3
;
475 case 6: return child
->thread
.debugreg6
;
476 case 7: return child
->thread
.debugreg7
;
481 static int ptrace_set_debugreg(struct task_struct
*child
,
482 int n
, unsigned long data
)
486 if (unlikely(n
== 4 || n
== 5))
489 if (n
< 4 && unlikely(data
>= debugreg_addr_limit(child
)))
493 case 0: child
->thread
.debugreg0
= data
; break;
494 case 1: child
->thread
.debugreg1
= data
; break;
495 case 2: child
->thread
.debugreg2
= data
; break;
496 case 3: child
->thread
.debugreg3
= data
; break;
499 if ((data
& ~0xffffffffUL
) != 0)
501 child
->thread
.debugreg6
= data
;
506 * Sanity-check data. Take one half-byte at once with
507 * check = (val >> (16 + 4*i)) & 0xf. It contains the
508 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
509 * 2 and 3 are LENi. Given a list of invalid values,
510 * we do mask |= 1 << invalid_value, so that
511 * (mask >> check) & 1 is a correct test for invalid
514 * R/Wi contains the type of the breakpoint /
515 * watchpoint, LENi contains the length of the watched
516 * data in the watchpoint case.
518 * The invalid values are:
519 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
520 * - R/Wi == 0x10 (break on I/O reads or writes), so
522 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
525 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
527 * See the Intel Manual "System Programming Guide",
530 * Note that LENi == 0x10 is defined on x86_64 in long
531 * mode (i.e. even for 32-bit userspace software, but
532 * 64-bit kernel), so the x86_64 mask value is 0x5454.
533 * See the AMD manual no. 24593 (AMD64 System Programming)
536 #define DR7_MASK 0x5f54
538 #define DR7_MASK 0x5554
540 data
&= ~DR_CONTROL_RESERVED
;
541 for (i
= 0; i
< 4; i
++)
542 if ((DR7_MASK
>> ((data
>> (16 + 4*i
)) & 0xf)) & 1)
544 child
->thread
.debugreg7
= data
;
546 set_tsk_thread_flag(child
, TIF_DEBUG
);
548 clear_tsk_thread_flag(child
, TIF_DEBUG
);
556 * These access the current or another (stopped) task's io permission
557 * bitmap for debugging or core dump.
559 static int ioperm_active(struct task_struct
*target
,
560 const struct user_regset
*regset
)
562 return target
->thread
.io_bitmap_max
/ regset
->size
;
565 static int ioperm_get(struct task_struct
*target
,
566 const struct user_regset
*regset
,
567 unsigned int pos
, unsigned int count
,
568 void *kbuf
, void __user
*ubuf
)
570 if (!target
->thread
.io_bitmap_ptr
)
573 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
574 target
->thread
.io_bitmap_ptr
,
578 #ifdef CONFIG_X86_PTRACE_BTS
579 static int ptrace_bts_read_record(struct task_struct
*child
, size_t index
,
580 struct bts_struct __user
*out
)
582 const struct bts_trace
*trace
;
583 struct bts_struct bts
;
584 const unsigned char *at
;
587 trace
= ds_read_bts(child
->bts
);
591 at
= trace
->ds
.top
- ((index
+ 1) * trace
->ds
.size
);
592 if ((void *)at
< trace
->ds
.begin
)
593 at
+= (trace
->ds
.n
* trace
->ds
.size
);
598 error
= trace
->read(child
->bts
, at
, &bts
);
602 if (copy_to_user(out
, &bts
, sizeof(bts
)))
608 static int ptrace_bts_drain(struct task_struct
*child
,
610 struct bts_struct __user
*out
)
612 const struct bts_trace
*trace
;
613 const unsigned char *at
;
614 int error
, drained
= 0;
616 trace
= ds_read_bts(child
->bts
);
623 if (size
< (trace
->ds
.top
- trace
->ds
.begin
))
626 for (at
= trace
->ds
.begin
; (void *)at
< trace
->ds
.top
;
627 out
++, drained
++, at
+= trace
->ds
.size
) {
628 struct bts_struct bts
;
631 error
= trace
->read(child
->bts
, at
, &bts
);
635 if (copy_to_user(out
, &bts
, sizeof(bts
)))
639 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
641 error
= ds_reset_bts(child
->bts
);
648 static int ptrace_bts_allocate_buffer(struct task_struct
*child
, size_t size
)
650 child
->bts_buffer
= alloc_locked_buffer(size
);
651 if (!child
->bts_buffer
)
654 child
->bts_size
= size
;
659 static void ptrace_bts_free_buffer(struct task_struct
*child
)
661 free_locked_buffer(child
->bts_buffer
, child
->bts_size
);
662 child
->bts_buffer
= NULL
;
666 static int ptrace_bts_config(struct task_struct
*child
,
668 const struct ptrace_bts_config __user
*ucfg
)
670 struct ptrace_bts_config cfg
;
671 unsigned int flags
= 0;
673 if (cfg_size
< sizeof(cfg
))
676 if (copy_from_user(&cfg
, ucfg
, sizeof(cfg
)))
680 ds_release_bts(child
->bts
);
684 if (cfg
.flags
& PTRACE_BTS_O_SIGNAL
) {
690 child
->thread
.bts_ovfl_signal
= cfg
.signal
;
693 if ((cfg
.flags
& PTRACE_BTS_O_ALLOC
) &&
694 (cfg
.size
!= child
->bts_size
)) {
697 ptrace_bts_free_buffer(child
);
699 error
= ptrace_bts_allocate_buffer(child
, cfg
.size
);
704 if (cfg
.flags
& PTRACE_BTS_O_TRACE
)
707 if (cfg
.flags
& PTRACE_BTS_O_SCHED
)
708 flags
|= BTS_TIMESTAMPS
;
710 child
->bts
= ds_request_bts(child
, child
->bts_buffer
, child
->bts_size
,
711 /* ovfl = */ NULL
, /* th = */ (size_t)-1,
713 if (IS_ERR(child
->bts
)) {
714 int error
= PTR_ERR(child
->bts
);
716 ptrace_bts_free_buffer(child
);
725 static int ptrace_bts_status(struct task_struct
*child
,
727 struct ptrace_bts_config __user
*ucfg
)
729 const struct bts_trace
*trace
;
730 struct ptrace_bts_config cfg
;
732 if (cfg_size
< sizeof(cfg
))
735 trace
= ds_read_bts(child
->bts
);
739 memset(&cfg
, 0, sizeof(cfg
));
740 cfg
.size
= trace
->ds
.end
- trace
->ds
.begin
;
741 cfg
.signal
= child
->thread
.bts_ovfl_signal
;
742 cfg
.bts_size
= sizeof(struct bts_struct
);
745 cfg
.flags
|= PTRACE_BTS_O_SIGNAL
;
747 if (trace
->ds
.flags
& BTS_USER
)
748 cfg
.flags
|= PTRACE_BTS_O_TRACE
;
750 if (trace
->ds
.flags
& BTS_TIMESTAMPS
)
751 cfg
.flags
|= PTRACE_BTS_O_SCHED
;
753 if (copy_to_user(ucfg
, &cfg
, sizeof(cfg
)))
759 static int ptrace_bts_clear(struct task_struct
*child
)
761 const struct bts_trace
*trace
;
763 trace
= ds_read_bts(child
->bts
);
767 memset(trace
->ds
.begin
, 0, trace
->ds
.n
* trace
->ds
.size
);
769 return ds_reset_bts(child
->bts
);
772 static int ptrace_bts_size(struct task_struct
*child
)
774 const struct bts_trace
*trace
;
776 trace
= ds_read_bts(child
->bts
);
780 return (trace
->ds
.top
- trace
->ds
.begin
) / trace
->ds
.size
;
783 static void ptrace_bts_fork(struct task_struct
*tsk
)
786 tsk
->bts_buffer
= NULL
;
788 tsk
->thread
.bts_ovfl_signal
= 0;
791 static void ptrace_bts_untrace(struct task_struct
*child
)
793 if (unlikely(child
->bts
)) {
794 ds_release_bts(child
->bts
);
797 /* We cannot update total_vm and locked_vm since
798 child's mm is already gone. But we can reclaim the
800 kfree(child
->bts_buffer
);
801 child
->bts_buffer
= NULL
;
806 static void ptrace_bts_detach(struct task_struct
*child
)
809 * Ptrace_detach() races with ptrace_untrace() in case
810 * the child dies and is reaped by another thread.
812 * We only do the memory accounting at this point and
813 * leave the buffer deallocation and the bts tracer
814 * release to ptrace_bts_untrace() which will be called
815 * later on with tasklist_lock held.
817 release_locked_buffer(child
->bts_buffer
, child
->bts_size
);
820 static inline void ptrace_bts_fork(struct task_struct
*tsk
) {}
821 static inline void ptrace_bts_detach(struct task_struct
*child
) {}
822 static inline void ptrace_bts_untrace(struct task_struct
*child
) {}
823 #endif /* CONFIG_X86_PTRACE_BTS */
825 void x86_ptrace_fork(struct task_struct
*child
, unsigned long clone_flags
)
827 ptrace_bts_fork(child
);
830 void x86_ptrace_untrace(struct task_struct
*child
)
832 ptrace_bts_untrace(child
);
836 * Called by kernel/ptrace.c when detaching..
838 * Make sure the single step bit is not set.
840 void ptrace_disable(struct task_struct
*child
)
842 user_disable_single_step(child
);
843 #ifdef TIF_SYSCALL_EMU
844 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
846 ptrace_bts_detach(child
);
849 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
850 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
853 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
856 unsigned long __user
*datap
= (unsigned long __user
*)data
;
859 /* read the word at location addr in the USER area. */
860 case PTRACE_PEEKUSR
: {
864 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
865 addr
>= sizeof(struct user
))
868 tmp
= 0; /* Default return condition */
869 if (addr
< sizeof(struct user_regs_struct
))
870 tmp
= getreg(child
, addr
);
871 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
872 addr
<= offsetof(struct user
, u_debugreg
[7])) {
873 addr
-= offsetof(struct user
, u_debugreg
[0]);
874 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
876 ret
= put_user(tmp
, datap
);
880 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
882 if ((addr
& (sizeof(data
) - 1)) || addr
< 0 ||
883 addr
>= sizeof(struct user
))
886 if (addr
< sizeof(struct user_regs_struct
))
887 ret
= putreg(child
, addr
, data
);
888 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
889 addr
<= offsetof(struct user
, u_debugreg
[7])) {
890 addr
-= offsetof(struct user
, u_debugreg
[0]);
891 ret
= ptrace_set_debugreg(child
,
892 addr
/ sizeof(data
), data
);
896 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
897 return copy_regset_to_user(child
,
898 task_user_regset_view(current
),
900 0, sizeof(struct user_regs_struct
),
903 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
904 return copy_regset_from_user(child
,
905 task_user_regset_view(current
),
907 0, sizeof(struct user_regs_struct
),
910 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
911 return copy_regset_to_user(child
,
912 task_user_regset_view(current
),
914 0, sizeof(struct user_i387_struct
),
917 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
918 return copy_regset_from_user(child
,
919 task_user_regset_view(current
),
921 0, sizeof(struct user_i387_struct
),
925 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
926 return copy_regset_to_user(child
, &user_x86_32_view
,
928 0, sizeof(struct user_fxsr_struct
),
931 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
932 return copy_regset_from_user(child
, &user_x86_32_view
,
934 0, sizeof(struct user_fxsr_struct
),
938 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
939 case PTRACE_GET_THREAD_AREA
:
942 ret
= do_get_thread_area(child
, addr
,
943 (struct user_desc __user
*) data
);
946 case PTRACE_SET_THREAD_AREA
:
949 ret
= do_set_thread_area(child
, addr
,
950 (struct user_desc __user
*) data
, 0);
955 /* normal 64bit interface to access TLS data.
956 Works just like arch_prctl, except that the arguments
958 case PTRACE_ARCH_PRCTL
:
959 ret
= do_arch_prctl(child
, data
, addr
);
964 * These bits need more cooking - not enabled yet:
966 #ifdef CONFIG_X86_PTRACE_BTS
967 case PTRACE_BTS_CONFIG
:
968 ret
= ptrace_bts_config
969 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
972 case PTRACE_BTS_STATUS
:
973 ret
= ptrace_bts_status
974 (child
, data
, (struct ptrace_bts_config __user
*)addr
);
977 case PTRACE_BTS_SIZE
:
978 ret
= ptrace_bts_size(child
);
982 ret
= ptrace_bts_read_record
983 (child
, data
, (struct bts_struct __user
*) addr
);
986 case PTRACE_BTS_CLEAR
:
987 ret
= ptrace_bts_clear(child
);
990 case PTRACE_BTS_DRAIN
:
991 ret
= ptrace_bts_drain
992 (child
, data
, (struct bts_struct __user
*) addr
);
994 #endif /* CONFIG_X86_PTRACE_BTS */
997 ret
= ptrace_request(child
, request
, addr
, data
);
1004 #ifdef CONFIG_IA32_EMULATION
1006 #include <linux/compat.h>
1007 #include <linux/syscalls.h>
1008 #include <asm/ia32.h>
1009 #include <asm/user32.h>
1012 case offsetof(struct user32, regs.l): \
1013 regs->q = value; break
1016 case offsetof(struct user32, regs.rs): \
1017 return set_segment_reg(child, \
1018 offsetof(struct user_regs_struct, rs), \
1022 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
1024 struct pt_regs
*regs
= task_pt_regs(child
);
1045 case offsetof(struct user32
, regs
.orig_eax
):
1047 * Sign-extend the value so that orig_eax = -1
1048 * causes (long)orig_ax < 0 tests to fire correctly.
1050 regs
->orig_ax
= (long) (s32
) value
;
1053 case offsetof(struct user32
, regs
.eflags
):
1054 return set_flags(child
, value
);
1056 case offsetof(struct user32
, u_debugreg
[0]) ...
1057 offsetof(struct user32
, u_debugreg
[7]):
1058 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1059 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1062 if (regno
> sizeof(struct user32
) || (regno
& 3))
1066 * Other dummy fields in the virtual user structure
1078 case offsetof(struct user32, regs.l): \
1079 *val = regs->q; break
1082 case offsetof(struct user32, regs.rs): \
1083 *val = get_segment_reg(child, \
1084 offsetof(struct user_regs_struct, rs)); \
1087 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1089 struct pt_regs
*regs
= task_pt_regs(child
);
1107 R32(orig_eax
, orig_ax
);
1111 case offsetof(struct user32
, regs
.eflags
):
1112 *val
= get_flags(child
);
1115 case offsetof(struct user32
, u_debugreg
[0]) ...
1116 offsetof(struct user32
, u_debugreg
[7]):
1117 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1118 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1122 if (regno
> sizeof(struct user32
) || (regno
& 3))
1126 * Other dummy fields in the virtual user structure
1138 static int genregs32_get(struct task_struct
*target
,
1139 const struct user_regset
*regset
,
1140 unsigned int pos
, unsigned int count
,
1141 void *kbuf
, void __user
*ubuf
)
1144 compat_ulong_t
*k
= kbuf
;
1146 getreg32(target
, pos
, k
++);
1147 count
-= sizeof(*k
);
1151 compat_ulong_t __user
*u
= ubuf
;
1153 compat_ulong_t word
;
1154 getreg32(target
, pos
, &word
);
1155 if (__put_user(word
, u
++))
1157 count
-= sizeof(*u
);
1165 static int genregs32_set(struct task_struct
*target
,
1166 const struct user_regset
*regset
,
1167 unsigned int pos
, unsigned int count
,
1168 const void *kbuf
, const void __user
*ubuf
)
1172 const compat_ulong_t
*k
= kbuf
;
1173 while (count
> 0 && !ret
) {
1174 ret
= putreg32(target
, pos
, *k
++);
1175 count
-= sizeof(*k
);
1179 const compat_ulong_t __user
*u
= ubuf
;
1180 while (count
> 0 && !ret
) {
1181 compat_ulong_t word
;
1182 ret
= __get_user(word
, u
++);
1185 ret
= putreg32(target
, pos
, word
);
1186 count
-= sizeof(*u
);
1193 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1194 compat_ulong_t caddr
, compat_ulong_t cdata
)
1196 unsigned long addr
= caddr
;
1197 unsigned long data
= cdata
;
1198 void __user
*datap
= compat_ptr(data
);
1203 case PTRACE_PEEKUSR
:
1204 ret
= getreg32(child
, addr
, &val
);
1206 ret
= put_user(val
, (__u32 __user
*)datap
);
1209 case PTRACE_POKEUSR
:
1210 ret
= putreg32(child
, addr
, data
);
1213 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1214 return copy_regset_to_user(child
, &user_x86_32_view
,
1216 0, sizeof(struct user_regs_struct32
),
1219 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1220 return copy_regset_from_user(child
, &user_x86_32_view
,
1222 sizeof(struct user_regs_struct32
),
1225 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1226 return copy_regset_to_user(child
, &user_x86_32_view
,
1228 sizeof(struct user_i387_ia32_struct
),
1231 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1232 return copy_regset_from_user(
1233 child
, &user_x86_32_view
, REGSET_FP
,
1234 0, sizeof(struct user_i387_ia32_struct
), datap
);
1236 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1237 return copy_regset_to_user(child
, &user_x86_32_view
,
1239 sizeof(struct user32_fxsr_struct
),
1242 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1243 return copy_regset_from_user(child
, &user_x86_32_view
,
1245 sizeof(struct user32_fxsr_struct
),
1248 case PTRACE_GET_THREAD_AREA
:
1249 case PTRACE_SET_THREAD_AREA
:
1250 #ifdef CONFIG_X86_PTRACE_BTS
1251 case PTRACE_BTS_CONFIG
:
1252 case PTRACE_BTS_STATUS
:
1253 case PTRACE_BTS_SIZE
:
1254 case PTRACE_BTS_GET
:
1255 case PTRACE_BTS_CLEAR
:
1256 case PTRACE_BTS_DRAIN
:
1257 #endif /* CONFIG_X86_PTRACE_BTS */
1258 return arch_ptrace(child
, request
, addr
, data
);
1261 return compat_ptrace_request(child
, request
, addr
, data
);
1267 #endif /* CONFIG_IA32_EMULATION */
1269 #ifdef CONFIG_X86_64
1271 static const struct user_regset x86_64_regsets
[] = {
1272 [REGSET_GENERAL
] = {
1273 .core_note_type
= NT_PRSTATUS
,
1274 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1275 .size
= sizeof(long), .align
= sizeof(long),
1276 .get
= genregs_get
, .set
= genregs_set
1279 .core_note_type
= NT_PRFPREG
,
1280 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1281 .size
= sizeof(long), .align
= sizeof(long),
1282 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1284 [REGSET_IOPERM64
] = {
1285 .core_note_type
= NT_386_IOPERM
,
1286 .n
= IO_BITMAP_LONGS
,
1287 .size
= sizeof(long), .align
= sizeof(long),
1288 .active
= ioperm_active
, .get
= ioperm_get
1292 static const struct user_regset_view user_x86_64_view
= {
1293 .name
= "x86_64", .e_machine
= EM_X86_64
,
1294 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1297 #else /* CONFIG_X86_32 */
1299 #define user_regs_struct32 user_regs_struct
1300 #define genregs32_get genregs_get
1301 #define genregs32_set genregs_set
1303 #define user_i387_ia32_struct user_i387_struct
1304 #define user32_fxsr_struct user_fxsr_struct
1306 #endif /* CONFIG_X86_64 */
1308 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1309 static const struct user_regset x86_32_regsets
[] = {
1310 [REGSET_GENERAL
] = {
1311 .core_note_type
= NT_PRSTATUS
,
1312 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1313 .size
= sizeof(u32
), .align
= sizeof(u32
),
1314 .get
= genregs32_get
, .set
= genregs32_set
1317 .core_note_type
= NT_PRFPREG
,
1318 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1319 .size
= sizeof(u32
), .align
= sizeof(u32
),
1320 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1323 .core_note_type
= NT_PRXFPREG
,
1324 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1325 .size
= sizeof(u32
), .align
= sizeof(u32
),
1326 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1329 .core_note_type
= NT_386_TLS
,
1330 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1331 .size
= sizeof(struct user_desc
),
1332 .align
= sizeof(struct user_desc
),
1333 .active
= regset_tls_active
,
1334 .get
= regset_tls_get
, .set
= regset_tls_set
1336 [REGSET_IOPERM32
] = {
1337 .core_note_type
= NT_386_IOPERM
,
1338 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1339 .size
= sizeof(u32
), .align
= sizeof(u32
),
1340 .active
= ioperm_active
, .get
= ioperm_get
1344 static const struct user_regset_view user_x86_32_view
= {
1345 .name
= "i386", .e_machine
= EM_386
,
1346 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1350 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1352 #ifdef CONFIG_IA32_EMULATION
1353 if (test_tsk_thread_flag(task
, TIF_IA32
))
1355 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1356 return &user_x86_32_view
;
1358 #ifdef CONFIG_X86_64
1359 return &user_x86_64_view
;
1363 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1364 int error_code
, int si_code
)
1366 struct siginfo info
;
1368 tsk
->thread
.trap_no
= 1;
1369 tsk
->thread
.error_code
= error_code
;
1371 memset(&info
, 0, sizeof(info
));
1372 info
.si_signo
= SIGTRAP
;
1373 info
.si_code
= si_code
;
1376 info
.si_addr
= user_mode_vm(regs
) ? (void __user
*) regs
->ip
: NULL
;
1378 /* Send us the fake SIGTRAP */
1379 force_sig_info(SIGTRAP
, &info
, tsk
);
1383 #ifdef CONFIG_X86_32
1385 #elif defined CONFIG_IA32_EMULATION
1386 # define IS_IA32 is_compat_task()
1392 * We must return the syscall number to actually look up in the table.
1393 * This can be -1L to skip running any syscall at all.
1395 asmregparm
long syscall_trace_enter(struct pt_regs
*regs
)
1400 * If we stepped into a sysenter/syscall insn, it trapped in
1401 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1402 * If user-mode had set TF itself, then it's still clear from
1403 * do_debug() and we need to set it again to restore the user
1404 * state. If we entered on the slow path, TF was already set.
1406 if (test_thread_flag(TIF_SINGLESTEP
))
1407 regs
->flags
|= X86_EFLAGS_TF
;
1409 /* do the secure computing check first */
1410 secure_computing(regs
->orig_ax
);
1412 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1415 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1416 tracehook_report_syscall_entry(regs
))
1419 if (unlikely(current
->audit_context
)) {
1421 audit_syscall_entry(AUDIT_ARCH_I386
,
1424 regs
->dx
, regs
->si
);
1425 #ifdef CONFIG_X86_64
1427 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1430 regs
->dx
, regs
->r10
);
1434 return ret
?: regs
->orig_ax
;
1437 asmregparm
void syscall_trace_leave(struct pt_regs
*regs
)
1439 if (unlikely(current
->audit_context
))
1440 audit_syscall_exit(AUDITSC_RESULT(regs
->ax
), regs
->ax
);
1442 if (test_thread_flag(TIF_SYSCALL_TRACE
))
1443 tracehook_report_syscall_exit(regs
, 0);
1446 * If TIF_SYSCALL_EMU is set, we only get here because of
1447 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1448 * We already reported this syscall instruction in
1449 * syscall_trace_enter(), so don't do any more now.
1451 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1455 * If we are single-stepping, synthesize a trap to follow the
1456 * system call instruction.
1458 if (test_thread_flag(TIF_SINGLESTEP
) &&
1459 tracehook_consider_fatal_signal(current
, SIGTRAP
, SIG_DFL
))
1460 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);