1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
10 #include <linux/smp.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/ptrace.h>
14 #include <linux/regset.h>
15 #include <linux/tracehook.h>
16 #include <linux/user.h>
17 #include <linux/elf.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
22 #include <linux/perf_event.h>
23 #include <linux/hw_breakpoint.h>
24 #include <linux/rcupdate.h>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/processor.h>
30 #include <asm/fpu-internal.h>
31 #include <asm/debugreg.h>
34 #include <asm/prctl.h>
35 #include <asm/proto.h>
36 #include <asm/hw_breakpoint.h>
37 #include <asm/traps.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/syscalls.h>
48 REGSET_IOPERM64
= REGSET_XFP
,
54 struct pt_regs_offset
{
59 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
60 #define REG_OFFSET_END {.name = NULL, .offset = 0}
62 static const struct pt_regs_offset regoffset_table
[] = {
86 REG_OFFSET_NAME(orig_ax
),
89 REG_OFFSET_NAME(flags
),
96 * regs_query_register_offset() - query register offset from its name
97 * @name: the name of a register
99 * regs_query_register_offset() returns the offset of a register in struct
100 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
102 int regs_query_register_offset(const char *name
)
104 const struct pt_regs_offset
*roff
;
105 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
106 if (!strcmp(roff
->name
, name
))
112 * regs_query_register_name() - query register name from its offset
113 * @offset: the offset of a register in struct pt_regs.
115 * regs_query_register_name() returns the name of a register from its
116 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
118 const char *regs_query_register_name(unsigned int offset
)
120 const struct pt_regs_offset
*roff
;
121 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
122 if (roff
->offset
== offset
)
127 static const int arg_offs_table
[] = {
129 [0] = offsetof(struct pt_regs
, ax
),
130 [1] = offsetof(struct pt_regs
, dx
),
131 [2] = offsetof(struct pt_regs
, cx
)
132 #else /* CONFIG_X86_64 */
133 [0] = offsetof(struct pt_regs
, di
),
134 [1] = offsetof(struct pt_regs
, si
),
135 [2] = offsetof(struct pt_regs
, dx
),
136 [3] = offsetof(struct pt_regs
, cx
),
137 [4] = offsetof(struct pt_regs
, r8
),
138 [5] = offsetof(struct pt_regs
, r9
)
143 * does not yet catch signals sent when the child dies.
144 * in exit.c or in signal.c.
148 * Determines which flags the user has access to [1 = access, 0 = no access].
150 #define FLAG_MASK_32 ((unsigned long) \
151 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
152 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
153 X86_EFLAGS_SF | X86_EFLAGS_TF | \
154 X86_EFLAGS_DF | X86_EFLAGS_OF | \
155 X86_EFLAGS_RF | X86_EFLAGS_AC))
158 * Determines whether a value may be installed in a segment register.
160 static inline bool invalid_selector(u16 value
)
162 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
167 #define FLAG_MASK FLAG_MASK_32
169 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
171 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
172 return ®s
->bx
+ (regno
>> 2);
175 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
178 * Returning the value truncates it to 16 bits.
181 if (offset
!= offsetof(struct user_regs_struct
, gs
))
182 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
185 retval
= get_user_gs(task_pt_regs(task
));
187 retval
= task_user_gs(task
);
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 * For %cs and %ss we cannot permit a null selector.
203 * We can permit a bogus selector as long as it has USER_RPL.
204 * Null selectors are fine for other segment registers, but
205 * we will never get back to user mode with invalid %cs or %ss
206 * and will take the trap in iret instead. Much code relies
207 * on user_mode() to distinguish a user trap frame (which can
208 * safely use invalid selectors) from a kernel trap frame.
211 case offsetof(struct user_regs_struct
, cs
):
212 case offsetof(struct user_regs_struct
, ss
):
213 if (unlikely(value
== 0))
217 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
220 case offsetof(struct user_regs_struct
, gs
):
222 set_user_gs(task_pt_regs(task
), value
);
224 task_user_gs(task
) = value
;
230 #else /* CONFIG_X86_64 */
232 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
234 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
236 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
237 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
240 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
243 * Returning the value truncates it to 16 bits.
248 case offsetof(struct user_regs_struct
, fs
):
249 if (task
== current
) {
250 /* Older gas can't assemble movq %?s,%r?? */
251 asm("movl %%fs,%0" : "=r" (seg
));
254 return task
->thread
.fsindex
;
255 case offsetof(struct user_regs_struct
, gs
):
256 if (task
== current
) {
257 asm("movl %%gs,%0" : "=r" (seg
));
260 return task
->thread
.gsindex
;
261 case offsetof(struct user_regs_struct
, ds
):
262 if (task
== current
) {
263 asm("movl %%ds,%0" : "=r" (seg
));
266 return task
->thread
.ds
;
267 case offsetof(struct user_regs_struct
, es
):
268 if (task
== current
) {
269 asm("movl %%es,%0" : "=r" (seg
));
272 return task
->thread
.es
;
274 case offsetof(struct user_regs_struct
, cs
):
275 case offsetof(struct user_regs_struct
, ss
):
278 return *pt_regs_access(task_pt_regs(task
), offset
);
281 static int set_segment_reg(struct task_struct
*task
,
282 unsigned long offset
, u16 value
)
285 * The value argument was already truncated to 16 bits.
287 if (invalid_selector(value
))
291 case offsetof(struct user_regs_struct
,fs
):
293 * If this is setting fs as for normal 64-bit use but
294 * setting fs_base has implicitly changed it, leave it.
296 if ((value
== FS_TLS_SEL
&& task
->thread
.fsindex
== 0 &&
297 task
->thread
.fs
!= 0) ||
298 (value
== 0 && task
->thread
.fsindex
== FS_TLS_SEL
&&
299 task
->thread
.fs
== 0))
301 task
->thread
.fsindex
= value
;
303 loadsegment(fs
, task
->thread
.fsindex
);
305 case offsetof(struct user_regs_struct
,gs
):
307 * If this is setting gs as for normal 64-bit use but
308 * setting gs_base has implicitly changed it, leave it.
310 if ((value
== GS_TLS_SEL
&& task
->thread
.gsindex
== 0 &&
311 task
->thread
.gs
!= 0) ||
312 (value
== 0 && task
->thread
.gsindex
== GS_TLS_SEL
&&
313 task
->thread
.gs
== 0))
315 task
->thread
.gsindex
= value
;
317 load_gs_index(task
->thread
.gsindex
);
319 case offsetof(struct user_regs_struct
,ds
):
320 task
->thread
.ds
= value
;
322 loadsegment(ds
, task
->thread
.ds
);
324 case offsetof(struct user_regs_struct
,es
):
325 task
->thread
.es
= value
;
327 loadsegment(es
, task
->thread
.es
);
331 * Can't actually change these in 64-bit mode.
333 case offsetof(struct user_regs_struct
,cs
):
334 if (unlikely(value
== 0))
336 #ifdef CONFIG_IA32_EMULATION
337 if (test_tsk_thread_flag(task
, TIF_IA32
))
338 task_pt_regs(task
)->cs
= value
;
341 case offsetof(struct user_regs_struct
,ss
):
342 if (unlikely(value
== 0))
344 #ifdef CONFIG_IA32_EMULATION
345 if (test_tsk_thread_flag(task
, TIF_IA32
))
346 task_pt_regs(task
)->ss
= value
;
354 #endif /* CONFIG_X86_32 */
356 static unsigned long get_flags(struct task_struct
*task
)
358 unsigned long retval
= task_pt_regs(task
)->flags
;
361 * If the debugger set TF, hide it from the readout.
363 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
364 retval
&= ~X86_EFLAGS_TF
;
369 static int set_flags(struct task_struct
*task
, unsigned long value
)
371 struct pt_regs
*regs
= task_pt_regs(task
);
374 * If the user value contains TF, mark that
375 * it was not "us" (the debugger) that set it.
376 * If not, make sure it stays set if we had.
378 if (value
& X86_EFLAGS_TF
)
379 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
380 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
381 value
|= X86_EFLAGS_TF
;
383 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
388 static int putreg(struct task_struct
*child
,
389 unsigned long offset
, unsigned long value
)
392 case offsetof(struct user_regs_struct
, cs
):
393 case offsetof(struct user_regs_struct
, ds
):
394 case offsetof(struct user_regs_struct
, es
):
395 case offsetof(struct user_regs_struct
, fs
):
396 case offsetof(struct user_regs_struct
, gs
):
397 case offsetof(struct user_regs_struct
, ss
):
398 return set_segment_reg(child
, offset
, value
);
400 case offsetof(struct user_regs_struct
, flags
):
401 return set_flags(child
, value
);
404 case offsetof(struct user_regs_struct
,fs_base
):
405 if (value
>= TASK_SIZE_OF(child
))
408 * When changing the segment base, use do_arch_prctl
409 * to set either thread.fs or thread.fsindex and the
410 * corresponding GDT slot.
412 if (child
->thread
.fs
!= value
)
413 return do_arch_prctl(child
, ARCH_SET_FS
, value
);
415 case offsetof(struct user_regs_struct
,gs_base
):
417 * Exactly the same here as the %fs handling above.
419 if (value
>= TASK_SIZE_OF(child
))
421 if (child
->thread
.gs
!= value
)
422 return do_arch_prctl(child
, ARCH_SET_GS
, value
);
427 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
431 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
434 case offsetof(struct user_regs_struct
, cs
):
435 case offsetof(struct user_regs_struct
, ds
):
436 case offsetof(struct user_regs_struct
, es
):
437 case offsetof(struct user_regs_struct
, fs
):
438 case offsetof(struct user_regs_struct
, gs
):
439 case offsetof(struct user_regs_struct
, ss
):
440 return get_segment_reg(task
, offset
);
442 case offsetof(struct user_regs_struct
, flags
):
443 return get_flags(task
);
446 case offsetof(struct user_regs_struct
, fs_base
): {
448 * do_arch_prctl may have used a GDT slot instead of
449 * the MSR. To userland, it appears the same either
450 * way, except the %fs segment selector might not be 0.
452 unsigned int seg
= task
->thread
.fsindex
;
453 if (task
->thread
.fs
!= 0)
454 return task
->thread
.fs
;
456 asm("movl %%fs,%0" : "=r" (seg
));
457 if (seg
!= FS_TLS_SEL
)
459 return get_desc_base(&task
->thread
.tls_array
[FS_TLS
]);
461 case offsetof(struct user_regs_struct
, gs_base
): {
463 * Exactly the same here as the %fs handling above.
465 unsigned int seg
= task
->thread
.gsindex
;
466 if (task
->thread
.gs
!= 0)
467 return task
->thread
.gs
;
469 asm("movl %%gs,%0" : "=r" (seg
));
470 if (seg
!= GS_TLS_SEL
)
472 return get_desc_base(&task
->thread
.tls_array
[GS_TLS
]);
477 return *pt_regs_access(task_pt_regs(task
), offset
);
480 static int genregs_get(struct task_struct
*target
,
481 const struct user_regset
*regset
,
482 unsigned int pos
, unsigned int count
,
483 void *kbuf
, void __user
*ubuf
)
486 unsigned long *k
= kbuf
;
487 while (count
>= sizeof(*k
)) {
488 *k
++ = getreg(target
, pos
);
493 unsigned long __user
*u
= ubuf
;
494 while (count
>= sizeof(*u
)) {
495 if (__put_user(getreg(target
, pos
), u
++))
505 static int genregs_set(struct task_struct
*target
,
506 const struct user_regset
*regset
,
507 unsigned int pos
, unsigned int count
,
508 const void *kbuf
, const void __user
*ubuf
)
512 const unsigned long *k
= kbuf
;
513 while (count
>= sizeof(*k
) && !ret
) {
514 ret
= putreg(target
, pos
, *k
++);
519 const unsigned long __user
*u
= ubuf
;
520 while (count
>= sizeof(*u
) && !ret
) {
522 ret
= __get_user(word
, u
++);
525 ret
= putreg(target
, pos
, word
);
533 static void ptrace_triggered(struct perf_event
*bp
,
534 struct perf_sample_data
*data
,
535 struct pt_regs
*regs
)
538 struct thread_struct
*thread
= &(current
->thread
);
541 * Store in the virtual DR6 register the fact that the breakpoint
542 * was hit so the thread's debugger will see it.
544 for (i
= 0; i
< HBP_NUM
; i
++) {
545 if (thread
->ptrace_bps
[i
] == bp
)
549 thread
->debugreg6
|= (DR_TRAP0
<< i
);
553 * Walk through every ptrace breakpoints for this thread and
554 * build the dr7 value on top of their attributes.
557 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
561 struct arch_hw_breakpoint
*info
;
563 for (i
= 0; i
< HBP_NUM
; i
++) {
564 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
565 info
= counter_arch_bp(bp
[i
]);
566 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
574 ptrace_modify_breakpoint(struct perf_event
*bp
, int len
, int type
,
575 struct task_struct
*tsk
, int disabled
)
578 int gen_len
, gen_type
;
579 struct perf_event_attr attr
;
582 * We should have at least an inactive breakpoint at this
583 * slot. It means the user is writing dr7 without having
584 * written the address register first
589 err
= arch_bp_generic_fields(len
, type
, &gen_len
, &gen_type
);
594 attr
.bp_len
= gen_len
;
595 attr
.bp_type
= gen_type
;
596 attr
.disabled
= disabled
;
598 return modify_user_hw_breakpoint(bp
, &attr
);
602 * Handle ptrace writes to debug register 7.
604 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
606 struct thread_struct
*thread
= &(tsk
->thread
);
607 unsigned long old_dr7
;
608 int i
, orig_ret
= 0, rc
= 0;
609 int enabled
, second_pass
= 0;
611 struct perf_event
*bp
;
613 if (ptrace_get_breakpoints(tsk
) < 0)
616 data
&= ~DR_CONTROL_RESERVED
;
617 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
620 * Loop through all the hardware breakpoints, making the
621 * appropriate changes to each.
623 for (i
= 0; i
< HBP_NUM
; i
++) {
624 enabled
= decode_dr7(data
, i
, &len
, &type
);
625 bp
= thread
->ptrace_bps
[i
];
630 * Don't unregister the breakpoints right-away,
631 * unless all register_user_hw_breakpoint()
632 * requests have succeeded. This prevents
633 * any window of opportunity for debug
634 * register grabbing by other users.
639 rc
= ptrace_modify_breakpoint(bp
, len
, type
,
647 rc
= ptrace_modify_breakpoint(bp
, len
, type
, tsk
, 0);
652 * Make a second pass to free the remaining unused breakpoints
653 * or to restore the original breakpoints if an error occurred.
664 ptrace_put_breakpoints(tsk
);
666 return ((orig_ret
< 0) ? orig_ret
: rc
);
670 * Handle PTRACE_PEEKUSR calls for the debug register area.
672 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
674 struct thread_struct
*thread
= &(tsk
->thread
);
675 unsigned long val
= 0;
678 struct perf_event
*bp
;
680 if (ptrace_get_breakpoints(tsk
) < 0)
683 bp
= thread
->ptrace_bps
[n
];
687 val
= bp
->hw
.info
.address
;
689 ptrace_put_breakpoints(tsk
);
691 val
= thread
->debugreg6
;
693 val
= thread
->ptrace_dr7
;
698 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
701 struct perf_event
*bp
;
702 struct thread_struct
*t
= &tsk
->thread
;
703 struct perf_event_attr attr
;
706 if (ptrace_get_breakpoints(tsk
) < 0)
709 if (!t
->ptrace_bps
[nr
]) {
710 ptrace_breakpoint_init(&attr
);
712 * Put stub len and type to register (reserve) an inactive but
716 attr
.bp_len
= HW_BREAKPOINT_LEN_1
;
717 attr
.bp_type
= HW_BREAKPOINT_W
;
720 bp
= register_user_hw_breakpoint(&attr
, ptrace_triggered
,
724 * CHECKME: the previous code returned -EIO if the addr wasn't
725 * a valid task virtual addr. The new one will return -EINVAL in
727 * -EINVAL may be what we want for in-kernel breakpoints users,
728 * but -EIO looks better for ptrace, since we refuse a register
729 * writing for the user. And anyway this is the previous
737 t
->ptrace_bps
[nr
] = bp
;
739 bp
= t
->ptrace_bps
[nr
];
743 err
= modify_user_hw_breakpoint(bp
, &attr
);
747 ptrace_put_breakpoints(tsk
);
752 * Handle PTRACE_POKEUSR calls for the debug register area.
754 static int ptrace_set_debugreg(struct task_struct
*tsk
, int n
,
757 struct thread_struct
*thread
= &(tsk
->thread
);
760 /* There are no DR4 or DR5 registers */
761 if (n
== 4 || n
== 5)
765 thread
->debugreg6
= val
;
769 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
773 /* All that's left is DR7 */
775 rc
= ptrace_write_dr7(tsk
, val
);
777 thread
->ptrace_dr7
= val
;
785 * These access the current or another (stopped) task's io permission
786 * bitmap for debugging or core dump.
788 static int ioperm_active(struct task_struct
*target
,
789 const struct user_regset
*regset
)
791 return target
->thread
.io_bitmap_max
/ regset
->size
;
794 static int ioperm_get(struct task_struct
*target
,
795 const struct user_regset
*regset
,
796 unsigned int pos
, unsigned int count
,
797 void *kbuf
, void __user
*ubuf
)
799 if (!target
->thread
.io_bitmap_ptr
)
802 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
803 target
->thread
.io_bitmap_ptr
,
808 * Called by kernel/ptrace.c when detaching..
810 * Make sure the single step bit is not set.
812 void ptrace_disable(struct task_struct
*child
)
814 user_disable_single_step(child
);
815 #ifdef TIF_SYSCALL_EMU
816 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
820 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
821 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
824 long arch_ptrace(struct task_struct
*child
, long request
,
825 unsigned long addr
, unsigned long data
)
828 unsigned long __user
*datap
= (unsigned long __user
*)data
;
831 /* read the word at location addr in the USER area. */
832 case PTRACE_PEEKUSR
: {
836 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
839 tmp
= 0; /* Default return condition */
840 if (addr
< sizeof(struct user_regs_struct
))
841 tmp
= getreg(child
, addr
);
842 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
843 addr
<= offsetof(struct user
, u_debugreg
[7])) {
844 addr
-= offsetof(struct user
, u_debugreg
[0]);
845 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
847 ret
= put_user(tmp
, datap
);
851 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
853 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
856 if (addr
< sizeof(struct user_regs_struct
))
857 ret
= putreg(child
, addr
, data
);
858 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
859 addr
<= offsetof(struct user
, u_debugreg
[7])) {
860 addr
-= offsetof(struct user
, u_debugreg
[0]);
861 ret
= ptrace_set_debugreg(child
,
862 addr
/ sizeof(data
), data
);
866 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
867 return copy_regset_to_user(child
,
868 task_user_regset_view(current
),
870 0, sizeof(struct user_regs_struct
),
873 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
874 return copy_regset_from_user(child
,
875 task_user_regset_view(current
),
877 0, sizeof(struct user_regs_struct
),
880 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
881 return copy_regset_to_user(child
,
882 task_user_regset_view(current
),
884 0, sizeof(struct user_i387_struct
),
887 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
888 return copy_regset_from_user(child
,
889 task_user_regset_view(current
),
891 0, sizeof(struct user_i387_struct
),
895 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
896 return copy_regset_to_user(child
, &user_x86_32_view
,
898 0, sizeof(struct user_fxsr_struct
),
901 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
902 return copy_regset_from_user(child
, &user_x86_32_view
,
904 0, sizeof(struct user_fxsr_struct
),
908 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
909 case PTRACE_GET_THREAD_AREA
:
912 ret
= do_get_thread_area(child
, addr
,
913 (struct user_desc __user
*)data
);
916 case PTRACE_SET_THREAD_AREA
:
919 ret
= do_set_thread_area(child
, addr
,
920 (struct user_desc __user
*)data
, 0);
925 /* normal 64bit interface to access TLS data.
926 Works just like arch_prctl, except that the arguments
928 case PTRACE_ARCH_PRCTL
:
929 ret
= do_arch_prctl(child
, data
, addr
);
934 ret
= ptrace_request(child
, request
, addr
, data
);
941 #ifdef CONFIG_IA32_EMULATION
943 #include <linux/compat.h>
944 #include <linux/syscalls.h>
945 #include <asm/ia32.h>
946 #include <asm/user32.h>
949 case offsetof(struct user32, regs.l): \
950 regs->q = value; break
953 case offsetof(struct user32, regs.rs): \
954 return set_segment_reg(child, \
955 offsetof(struct user_regs_struct, rs), \
959 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
961 struct pt_regs
*regs
= task_pt_regs(child
);
982 case offsetof(struct user32
, regs
.orig_eax
):
984 * A 32-bit debugger setting orig_eax means to restore
985 * the state of the task restarting a 32-bit syscall.
986 * Make sure we interpret the -ERESTART* codes correctly
987 * in case the task is not actually still sitting at the
988 * exit from a 32-bit syscall with TS_COMPAT still set.
990 regs
->orig_ax
= value
;
991 if (syscall_get_nr(child
, regs
) >= 0)
992 task_thread_info(child
)->status
|= TS_COMPAT
;
995 case offsetof(struct user32
, regs
.eflags
):
996 return set_flags(child
, value
);
998 case offsetof(struct user32
, u_debugreg
[0]) ...
999 offsetof(struct user32
, u_debugreg
[7]):
1000 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1001 return ptrace_set_debugreg(child
, regno
/ 4, value
);
1004 if (regno
> sizeof(struct user32
) || (regno
& 3))
1008 * Other dummy fields in the virtual user structure
1020 case offsetof(struct user32, regs.l): \
1021 *val = regs->q; break
1024 case offsetof(struct user32, regs.rs): \
1025 *val = get_segment_reg(child, \
1026 offsetof(struct user_regs_struct, rs)); \
1029 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1031 struct pt_regs
*regs
= task_pt_regs(child
);
1049 R32(orig_eax
, orig_ax
);
1053 case offsetof(struct user32
, regs
.eflags
):
1054 *val
= get_flags(child
);
1057 case offsetof(struct user32
, u_debugreg
[0]) ...
1058 offsetof(struct user32
, u_debugreg
[7]):
1059 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1060 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1064 if (regno
> sizeof(struct user32
) || (regno
& 3))
1068 * Other dummy fields in the virtual user structure
1080 static int genregs32_get(struct task_struct
*target
,
1081 const struct user_regset
*regset
,
1082 unsigned int pos
, unsigned int count
,
1083 void *kbuf
, void __user
*ubuf
)
1086 compat_ulong_t
*k
= kbuf
;
1087 while (count
>= sizeof(*k
)) {
1088 getreg32(target
, pos
, k
++);
1089 count
-= sizeof(*k
);
1093 compat_ulong_t __user
*u
= ubuf
;
1094 while (count
>= sizeof(*u
)) {
1095 compat_ulong_t word
;
1096 getreg32(target
, pos
, &word
);
1097 if (__put_user(word
, u
++))
1099 count
-= sizeof(*u
);
1107 static int genregs32_set(struct task_struct
*target
,
1108 const struct user_regset
*regset
,
1109 unsigned int pos
, unsigned int count
,
1110 const void *kbuf
, const void __user
*ubuf
)
1114 const compat_ulong_t
*k
= kbuf
;
1115 while (count
>= sizeof(*k
) && !ret
) {
1116 ret
= putreg32(target
, pos
, *k
++);
1117 count
-= sizeof(*k
);
1121 const compat_ulong_t __user
*u
= ubuf
;
1122 while (count
>= sizeof(*u
) && !ret
) {
1123 compat_ulong_t word
;
1124 ret
= __get_user(word
, u
++);
1127 ret
= putreg32(target
, pos
, word
);
1128 count
-= sizeof(*u
);
1135 #ifdef CONFIG_X86_X32_ABI
1136 static long x32_arch_ptrace(struct task_struct
*child
,
1137 compat_long_t request
, compat_ulong_t caddr
,
1138 compat_ulong_t cdata
)
1140 unsigned long addr
= caddr
;
1141 unsigned long data
= cdata
;
1142 void __user
*datap
= compat_ptr(data
);
1146 /* Read 32bits at location addr in the USER area. Only allow
1147 to return the lower 32bits of segment and debug registers. */
1148 case PTRACE_PEEKUSR
: {
1152 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1153 addr
< offsetof(struct user_regs_struct
, cs
))
1156 tmp
= 0; /* Default return condition */
1157 if (addr
< sizeof(struct user_regs_struct
))
1158 tmp
= getreg(child
, addr
);
1159 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1160 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1161 addr
-= offsetof(struct user
, u_debugreg
[0]);
1162 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1164 ret
= put_user(tmp
, (__u32 __user
*)datap
);
1168 /* Write the word at location addr in the USER area. Only allow
1169 to update segment and debug registers with the upper 32bits
1171 case PTRACE_POKEUSR
:
1173 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1174 addr
< offsetof(struct user_regs_struct
, cs
))
1177 if (addr
< sizeof(struct user_regs_struct
))
1178 ret
= putreg(child
, addr
, data
);
1179 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1180 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1181 addr
-= offsetof(struct user
, u_debugreg
[0]);
1182 ret
= ptrace_set_debugreg(child
,
1183 addr
/ sizeof(data
), data
);
1187 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1188 return copy_regset_to_user(child
,
1189 task_user_regset_view(current
),
1191 0, sizeof(struct user_regs_struct
),
1194 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1195 return copy_regset_from_user(child
,
1196 task_user_regset_view(current
),
1198 0, sizeof(struct user_regs_struct
),
1201 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1202 return copy_regset_to_user(child
,
1203 task_user_regset_view(current
),
1205 0, sizeof(struct user_i387_struct
),
1208 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1209 return copy_regset_from_user(child
,
1210 task_user_regset_view(current
),
1212 0, sizeof(struct user_i387_struct
),
1216 return compat_ptrace_request(child
, request
, addr
, data
);
1223 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1224 compat_ulong_t caddr
, compat_ulong_t cdata
)
1226 unsigned long addr
= caddr
;
1227 unsigned long data
= cdata
;
1228 void __user
*datap
= compat_ptr(data
);
1232 #ifdef CONFIG_X86_X32_ABI
1233 if (!is_ia32_task())
1234 return x32_arch_ptrace(child
, request
, caddr
, cdata
);
1238 case PTRACE_PEEKUSR
:
1239 ret
= getreg32(child
, addr
, &val
);
1241 ret
= put_user(val
, (__u32 __user
*)datap
);
1244 case PTRACE_POKEUSR
:
1245 ret
= putreg32(child
, addr
, data
);
1248 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1249 return copy_regset_to_user(child
, &user_x86_32_view
,
1251 0, sizeof(struct user_regs_struct32
),
1254 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1255 return copy_regset_from_user(child
, &user_x86_32_view
,
1257 sizeof(struct user_regs_struct32
),
1260 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1261 return copy_regset_to_user(child
, &user_x86_32_view
,
1263 sizeof(struct user_i387_ia32_struct
),
1266 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1267 return copy_regset_from_user(
1268 child
, &user_x86_32_view
, REGSET_FP
,
1269 0, sizeof(struct user_i387_ia32_struct
), datap
);
1271 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1272 return copy_regset_to_user(child
, &user_x86_32_view
,
1274 sizeof(struct user32_fxsr_struct
),
1277 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1278 return copy_regset_from_user(child
, &user_x86_32_view
,
1280 sizeof(struct user32_fxsr_struct
),
1283 case PTRACE_GET_THREAD_AREA
:
1284 case PTRACE_SET_THREAD_AREA
:
1285 return arch_ptrace(child
, request
, addr
, data
);
1288 return compat_ptrace_request(child
, request
, addr
, data
);
1294 #endif /* CONFIG_IA32_EMULATION */
1296 #ifdef CONFIG_X86_64
1298 static struct user_regset x86_64_regsets
[] __read_mostly
= {
1299 [REGSET_GENERAL
] = {
1300 .core_note_type
= NT_PRSTATUS
,
1301 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1302 .size
= sizeof(long), .align
= sizeof(long),
1303 .get
= genregs_get
, .set
= genregs_set
1306 .core_note_type
= NT_PRFPREG
,
1307 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1308 .size
= sizeof(long), .align
= sizeof(long),
1309 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1312 .core_note_type
= NT_X86_XSTATE
,
1313 .size
= sizeof(u64
), .align
= sizeof(u64
),
1314 .active
= xstateregs_active
, .get
= xstateregs_get
,
1315 .set
= xstateregs_set
1317 [REGSET_IOPERM64
] = {
1318 .core_note_type
= NT_386_IOPERM
,
1319 .n
= IO_BITMAP_LONGS
,
1320 .size
= sizeof(long), .align
= sizeof(long),
1321 .active
= ioperm_active
, .get
= ioperm_get
1325 static const struct user_regset_view user_x86_64_view
= {
1326 .name
= "x86_64", .e_machine
= EM_X86_64
,
1327 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1330 #else /* CONFIG_X86_32 */
1332 #define user_regs_struct32 user_regs_struct
1333 #define genregs32_get genregs_get
1334 #define genregs32_set genregs_set
1336 #endif /* CONFIG_X86_64 */
1338 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1339 static struct user_regset x86_32_regsets
[] __read_mostly
= {
1340 [REGSET_GENERAL
] = {
1341 .core_note_type
= NT_PRSTATUS
,
1342 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1343 .size
= sizeof(u32
), .align
= sizeof(u32
),
1344 .get
= genregs32_get
, .set
= genregs32_set
1347 .core_note_type
= NT_PRFPREG
,
1348 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1349 .size
= sizeof(u32
), .align
= sizeof(u32
),
1350 .active
= fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1353 .core_note_type
= NT_PRXFPREG
,
1354 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1355 .size
= sizeof(u32
), .align
= sizeof(u32
),
1356 .active
= xfpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1359 .core_note_type
= NT_X86_XSTATE
,
1360 .size
= sizeof(u64
), .align
= sizeof(u64
),
1361 .active
= xstateregs_active
, .get
= xstateregs_get
,
1362 .set
= xstateregs_set
1365 .core_note_type
= NT_386_TLS
,
1366 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1367 .size
= sizeof(struct user_desc
),
1368 .align
= sizeof(struct user_desc
),
1369 .active
= regset_tls_active
,
1370 .get
= regset_tls_get
, .set
= regset_tls_set
1372 [REGSET_IOPERM32
] = {
1373 .core_note_type
= NT_386_IOPERM
,
1374 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1375 .size
= sizeof(u32
), .align
= sizeof(u32
),
1376 .active
= ioperm_active
, .get
= ioperm_get
1380 static const struct user_regset_view user_x86_32_view
= {
1381 .name
= "i386", .e_machine
= EM_386
,
1382 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1387 * This represents bytes 464..511 in the memory layout exported through
1388 * the REGSET_XSTATE interface.
1390 u64 xstate_fx_sw_bytes
[USER_XSTATE_FX_SW_WORDS
];
1392 void update_regset_xstate_info(unsigned int size
, u64 xstate_mask
)
1394 #ifdef CONFIG_X86_64
1395 x86_64_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1397 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1398 x86_32_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1400 xstate_fx_sw_bytes
[USER_XSTATE_XCR0_WORD
] = xstate_mask
;
1403 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1405 #ifdef CONFIG_IA32_EMULATION
1406 if (test_tsk_thread_flag(task
, TIF_IA32
))
1408 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1409 return &user_x86_32_view
;
1411 #ifdef CONFIG_X86_64
1412 return &user_x86_64_view
;
1416 static void fill_sigtrap_info(struct task_struct
*tsk
,
1417 struct pt_regs
*regs
,
1418 int error_code
, int si_code
,
1419 struct siginfo
*info
)
1421 tsk
->thread
.trap_nr
= X86_TRAP_DB
;
1422 tsk
->thread
.error_code
= error_code
;
1424 memset(info
, 0, sizeof(*info
));
1425 info
->si_signo
= SIGTRAP
;
1426 info
->si_code
= si_code
;
1427 info
->si_addr
= user_mode_vm(regs
) ? (void __user
*)regs
->ip
: NULL
;
1430 void user_single_step_siginfo(struct task_struct
*tsk
,
1431 struct pt_regs
*regs
,
1432 struct siginfo
*info
)
1434 fill_sigtrap_info(tsk
, regs
, 0, TRAP_BRKPT
, info
);
1437 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1438 int error_code
, int si_code
)
1440 struct siginfo info
;
1442 fill_sigtrap_info(tsk
, regs
, error_code
, si_code
, &info
);
1443 /* Send us the fake SIGTRAP */
1444 force_sig_info(SIGTRAP
, &info
, tsk
);
1448 #ifdef CONFIG_X86_32
1450 #elif defined CONFIG_IA32_EMULATION
1451 # define IS_IA32 is_compat_task()
1457 * We must return the syscall number to actually look up in the table.
1458 * This can be -1L to skip running any syscall at all.
1460 long syscall_trace_enter(struct pt_regs
*regs
)
1467 * If we stepped into a sysenter/syscall insn, it trapped in
1468 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1469 * If user-mode had set TF itself, then it's still clear from
1470 * do_debug() and we need to set it again to restore the user
1471 * state. If we entered on the slow path, TF was already set.
1473 if (test_thread_flag(TIF_SINGLESTEP
))
1474 regs
->flags
|= X86_EFLAGS_TF
;
1476 /* do the secure computing check first */
1477 if (secure_computing(regs
->orig_ax
)) {
1478 /* seccomp failures shouldn't expose any additional code. */
1483 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU
)))
1486 if ((ret
|| test_thread_flag(TIF_SYSCALL_TRACE
)) &&
1487 tracehook_report_syscall_entry(regs
))
1490 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1491 trace_sys_enter(regs
, regs
->orig_ax
);
1494 audit_syscall_entry(AUDIT_ARCH_I386
,
1497 regs
->dx
, regs
->si
);
1498 #ifdef CONFIG_X86_64
1500 audit_syscall_entry(AUDIT_ARCH_X86_64
,
1503 regs
->dx
, regs
->r10
);
1507 return ret
?: regs
->orig_ax
;
1510 void syscall_trace_leave(struct pt_regs
*regs
)
1514 audit_syscall_exit(regs
);
1516 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
1517 trace_sys_exit(regs
, regs
->ax
);
1520 * If TIF_SYSCALL_EMU is set, we only get here because of
1521 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1522 * We already reported this syscall instruction in
1523 * syscall_trace_enter().
1525 step
= unlikely(test_thread_flag(TIF_SINGLESTEP
)) &&
1526 !test_thread_flag(TIF_SYSCALL_EMU
);
1527 if (step
|| test_thread_flag(TIF_SYSCALL_TRACE
))
1528 tracehook_report_syscall_exit(regs
, step
);