1 // SPDX-License-Identifier: GPL-2.0-only
2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/ptrace.h>
16 #include <linux/tracehook.h>
17 #include <linux/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
23 #include <linux/perf_event.h>
24 #include <linux/hw_breakpoint.h>
25 #include <linux/rcupdate.h>
26 #include <linux/export.h>
27 #include <linux/context_tracking.h>
28 #include <linux/nospec.h>
30 #include <linux/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/processor.h>
33 #include <asm/fpu/internal.h>
34 #include <asm/fpu/signal.h>
35 #include <asm/fpu/regset.h>
36 #include <asm/debugreg.h>
39 #include <asm/prctl.h>
40 #include <asm/proto.h>
41 #include <asm/hw_breakpoint.h>
42 #include <asm/traps.h>
43 #include <asm/syscall.h>
44 #include <asm/fsgsbase.h>
45 #include <asm/io_bitmap.h>
53 REGSET_IOPERM64
= REGSET_XFP
,
59 struct pt_regs_offset
{
64 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
65 #define REG_OFFSET_END {.name = NULL, .offset = 0}
67 static const struct pt_regs_offset regoffset_table
[] = {
91 REG_OFFSET_NAME(orig_ax
),
94 REG_OFFSET_NAME(flags
),
101 * regs_query_register_offset() - query register offset from its name
102 * @name: the name of a register
104 * regs_query_register_offset() returns the offset of a register in struct
105 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
107 int regs_query_register_offset(const char *name
)
109 const struct pt_regs_offset
*roff
;
110 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
111 if (!strcmp(roff
->name
, name
))
117 * regs_query_register_name() - query register name from its offset
118 * @offset: the offset of a register in struct pt_regs.
120 * regs_query_register_name() returns the name of a register from its
121 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
123 const char *regs_query_register_name(unsigned int offset
)
125 const struct pt_regs_offset
*roff
;
126 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
127 if (roff
->offset
== offset
)
133 * does not yet catch signals sent when the child dies.
134 * in exit.c or in signal.c.
138 * Determines which flags the user has access to [1 = access, 0 = no access].
140 #define FLAG_MASK_32 ((unsigned long) \
141 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
142 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
143 X86_EFLAGS_SF | X86_EFLAGS_TF | \
144 X86_EFLAGS_DF | X86_EFLAGS_OF | \
145 X86_EFLAGS_RF | X86_EFLAGS_AC))
148 * Determines whether a value may be installed in a segment register.
150 static inline bool invalid_selector(u16 value
)
152 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
157 #define FLAG_MASK FLAG_MASK_32
159 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
161 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
162 return ®s
->bx
+ (regno
>> 2);
165 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
168 * Returning the value truncates it to 16 bits.
171 if (offset
!= offsetof(struct user_regs_struct
, gs
))
172 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
175 retval
= get_user_gs(task_pt_regs(task
));
177 retval
= task_user_gs(task
);
182 static int set_segment_reg(struct task_struct
*task
,
183 unsigned long offset
, u16 value
)
185 if (WARN_ON_ONCE(task
== current
))
189 * The value argument was already truncated to 16 bits.
191 if (invalid_selector(value
))
195 * For %cs and %ss we cannot permit a null selector.
196 * We can permit a bogus selector as long as it has USER_RPL.
197 * Null selectors are fine for other segment registers, but
198 * we will never get back to user mode with invalid %cs or %ss
199 * and will take the trap in iret instead. Much code relies
200 * on user_mode() to distinguish a user trap frame (which can
201 * safely use invalid selectors) from a kernel trap frame.
204 case offsetof(struct user_regs_struct
, cs
):
205 case offsetof(struct user_regs_struct
, ss
):
206 if (unlikely(value
== 0))
208 /* Else, fall through */
211 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
214 case offsetof(struct user_regs_struct
, gs
):
215 task_user_gs(task
) = value
;
221 #else /* CONFIG_X86_64 */
223 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
225 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
227 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
228 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
231 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
234 * Returning the value truncates it to 16 bits.
239 case offsetof(struct user_regs_struct
, fs
):
240 if (task
== current
) {
241 /* Older gas can't assemble movq %?s,%r?? */
242 asm("movl %%fs,%0" : "=r" (seg
));
245 return task
->thread
.fsindex
;
246 case offsetof(struct user_regs_struct
, gs
):
247 if (task
== current
) {
248 asm("movl %%gs,%0" : "=r" (seg
));
251 return task
->thread
.gsindex
;
252 case offsetof(struct user_regs_struct
, ds
):
253 if (task
== current
) {
254 asm("movl %%ds,%0" : "=r" (seg
));
257 return task
->thread
.ds
;
258 case offsetof(struct user_regs_struct
, es
):
259 if (task
== current
) {
260 asm("movl %%es,%0" : "=r" (seg
));
263 return task
->thread
.es
;
265 case offsetof(struct user_regs_struct
, cs
):
266 case offsetof(struct user_regs_struct
, ss
):
269 return *pt_regs_access(task_pt_regs(task
), offset
);
272 static int set_segment_reg(struct task_struct
*task
,
273 unsigned long offset
, u16 value
)
275 if (WARN_ON_ONCE(task
== current
))
279 * The value argument was already truncated to 16 bits.
281 if (invalid_selector(value
))
285 * This function has some ABI oddities.
287 * A 32-bit ptracer probably expects that writing FS or GS will change
288 * FSBASE or GSBASE respectively. In the absence of FSGSBASE support,
289 * this code indeed has that effect. When FSGSBASE is added, this
290 * will require a special case.
292 * For existing 64-bit ptracers, writing FS or GS *also* currently
293 * changes the base if the selector is nonzero the next time the task
294 * is run. This behavior may not be needed, and trying to preserve it
295 * when FSGSBASE is added would be complicated at best.
299 case offsetof(struct user_regs_struct
,fs
):
300 task
->thread
.fsindex
= value
;
302 case offsetof(struct user_regs_struct
,gs
):
303 task
->thread
.gsindex
= value
;
305 case offsetof(struct user_regs_struct
,ds
):
306 task
->thread
.ds
= value
;
308 case offsetof(struct user_regs_struct
,es
):
309 task
->thread
.es
= value
;
313 * Can't actually change these in 64-bit mode.
315 case offsetof(struct user_regs_struct
,cs
):
316 if (unlikely(value
== 0))
318 task_pt_regs(task
)->cs
= value
;
320 case offsetof(struct user_regs_struct
,ss
):
321 if (unlikely(value
== 0))
323 task_pt_regs(task
)->ss
= value
;
330 #endif /* CONFIG_X86_32 */
332 static unsigned long get_flags(struct task_struct
*task
)
334 unsigned long retval
= task_pt_regs(task
)->flags
;
337 * If the debugger set TF, hide it from the readout.
339 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
340 retval
&= ~X86_EFLAGS_TF
;
345 static int set_flags(struct task_struct
*task
, unsigned long value
)
347 struct pt_regs
*regs
= task_pt_regs(task
);
350 * If the user value contains TF, mark that
351 * it was not "us" (the debugger) that set it.
352 * If not, make sure it stays set if we had.
354 if (value
& X86_EFLAGS_TF
)
355 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
356 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
357 value
|= X86_EFLAGS_TF
;
359 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
364 static int putreg(struct task_struct
*child
,
365 unsigned long offset
, unsigned long value
)
368 case offsetof(struct user_regs_struct
, cs
):
369 case offsetof(struct user_regs_struct
, ds
):
370 case offsetof(struct user_regs_struct
, es
):
371 case offsetof(struct user_regs_struct
, fs
):
372 case offsetof(struct user_regs_struct
, gs
):
373 case offsetof(struct user_regs_struct
, ss
):
374 return set_segment_reg(child
, offset
, value
);
376 case offsetof(struct user_regs_struct
, flags
):
377 return set_flags(child
, value
);
380 case offsetof(struct user_regs_struct
,fs_base
):
381 if (value
>= TASK_SIZE_MAX
)
384 * When changing the FS base, use do_arch_prctl_64()
385 * to set the index to zero and to set the base
388 * NB: This behavior is nonsensical and likely needs to
389 * change when FSGSBASE support is added.
391 if (child
->thread
.fsbase
!= value
)
392 return do_arch_prctl_64(child
, ARCH_SET_FS
, value
);
394 case offsetof(struct user_regs_struct
,gs_base
):
396 * Exactly the same here as the %fs handling above.
398 if (value
>= TASK_SIZE_MAX
)
400 if (child
->thread
.gsbase
!= value
)
401 return do_arch_prctl_64(child
, ARCH_SET_GS
, value
);
406 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
410 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
413 case offsetof(struct user_regs_struct
, cs
):
414 case offsetof(struct user_regs_struct
, ds
):
415 case offsetof(struct user_regs_struct
, es
):
416 case offsetof(struct user_regs_struct
, fs
):
417 case offsetof(struct user_regs_struct
, gs
):
418 case offsetof(struct user_regs_struct
, ss
):
419 return get_segment_reg(task
, offset
);
421 case offsetof(struct user_regs_struct
, flags
):
422 return get_flags(task
);
425 case offsetof(struct user_regs_struct
, fs_base
):
426 return x86_fsbase_read_task(task
);
427 case offsetof(struct user_regs_struct
, gs_base
):
428 return x86_gsbase_read_task(task
);
432 return *pt_regs_access(task_pt_regs(task
), offset
);
435 static int genregs_get(struct task_struct
*target
,
436 const struct user_regset
*regset
,
437 unsigned int pos
, unsigned int count
,
438 void *kbuf
, void __user
*ubuf
)
441 unsigned long *k
= kbuf
;
442 while (count
>= sizeof(*k
)) {
443 *k
++ = getreg(target
, pos
);
448 unsigned long __user
*u
= ubuf
;
449 while (count
>= sizeof(*u
)) {
450 if (__put_user(getreg(target
, pos
), u
++))
460 static int genregs_set(struct task_struct
*target
,
461 const struct user_regset
*regset
,
462 unsigned int pos
, unsigned int count
,
463 const void *kbuf
, const void __user
*ubuf
)
467 const unsigned long *k
= kbuf
;
468 while (count
>= sizeof(*k
) && !ret
) {
469 ret
= putreg(target
, pos
, *k
++);
474 const unsigned long __user
*u
= ubuf
;
475 while (count
>= sizeof(*u
) && !ret
) {
477 ret
= __get_user(word
, u
++);
480 ret
= putreg(target
, pos
, word
);
488 static void ptrace_triggered(struct perf_event
*bp
,
489 struct perf_sample_data
*data
,
490 struct pt_regs
*regs
)
493 struct thread_struct
*thread
= &(current
->thread
);
496 * Store in the virtual DR6 register the fact that the breakpoint
497 * was hit so the thread's debugger will see it.
499 for (i
= 0; i
< HBP_NUM
; i
++) {
500 if (thread
->ptrace_bps
[i
] == bp
)
504 thread
->debugreg6
|= (DR_TRAP0
<< i
);
508 * Walk through every ptrace breakpoints for this thread and
509 * build the dr7 value on top of their attributes.
512 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
516 struct arch_hw_breakpoint
*info
;
518 for (i
= 0; i
< HBP_NUM
; i
++) {
519 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
520 info
= counter_arch_bp(bp
[i
]);
521 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
528 static int ptrace_fill_bp_fields(struct perf_event_attr
*attr
,
529 int len
, int type
, bool disabled
)
531 int err
, bp_len
, bp_type
;
533 err
= arch_bp_generic_fields(len
, type
, &bp_len
, &bp_type
);
535 attr
->bp_len
= bp_len
;
536 attr
->bp_type
= bp_type
;
537 attr
->disabled
= disabled
;
543 static struct perf_event
*
544 ptrace_register_breakpoint(struct task_struct
*tsk
, int len
, int type
,
545 unsigned long addr
, bool disabled
)
547 struct perf_event_attr attr
;
550 ptrace_breakpoint_init(&attr
);
553 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
557 return register_user_hw_breakpoint(&attr
, ptrace_triggered
,
561 static int ptrace_modify_breakpoint(struct perf_event
*bp
, int len
, int type
,
564 struct perf_event_attr attr
= bp
->attr
;
567 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
571 return modify_user_hw_breakpoint(bp
, &attr
);
575 * Handle ptrace writes to debug register 7.
577 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
579 struct thread_struct
*thread
= &tsk
->thread
;
580 unsigned long old_dr7
;
581 bool second_pass
= false;
584 data
&= ~DR_CONTROL_RESERVED
;
585 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
589 for (i
= 0; i
< HBP_NUM
; i
++) {
591 bool disabled
= !decode_dr7(data
, i
, &len
, &type
);
592 struct perf_event
*bp
= thread
->ptrace_bps
[i
];
598 bp
= ptrace_register_breakpoint(tsk
,
599 len
, type
, 0, disabled
);
605 thread
->ptrace_bps
[i
] = bp
;
609 rc
= ptrace_modify_breakpoint(bp
, len
, type
, disabled
);
614 /* Restore if the first pass failed, second_pass shouldn't fail. */
615 if (rc
&& !WARN_ON(second_pass
)) {
626 * Handle PTRACE_PEEKUSR calls for the debug register area.
628 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
630 struct thread_struct
*thread
= &tsk
->thread
;
631 unsigned long val
= 0;
634 int index
= array_index_nospec(n
, HBP_NUM
);
635 struct perf_event
*bp
= thread
->ptrace_bps
[index
];
638 val
= bp
->hw
.info
.address
;
640 val
= thread
->debugreg6
;
642 val
= thread
->ptrace_dr7
;
647 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
650 struct thread_struct
*t
= &tsk
->thread
;
651 struct perf_event
*bp
= t
->ptrace_bps
[nr
];
656 * Put stub len and type to create an inactive but correct bp.
658 * CHECKME: the previous code returned -EIO if the addr wasn't
659 * a valid task virtual addr. The new one will return -EINVAL in
661 * -EINVAL may be what we want for in-kernel breakpoints users,
662 * but -EIO looks better for ptrace, since we refuse a register
663 * writing for the user. And anyway this is the previous
666 bp
= ptrace_register_breakpoint(tsk
,
667 X86_BREAKPOINT_LEN_1
, X86_BREAKPOINT_WRITE
,
672 t
->ptrace_bps
[nr
] = bp
;
674 struct perf_event_attr attr
= bp
->attr
;
677 err
= modify_user_hw_breakpoint(bp
, &attr
);
684 * Handle PTRACE_POKEUSR calls for the debug register area.
686 static int ptrace_set_debugreg(struct task_struct
*tsk
, int n
,
689 struct thread_struct
*thread
= &tsk
->thread
;
690 /* There are no DR4 or DR5 registers */
694 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
696 thread
->debugreg6
= val
;
699 rc
= ptrace_write_dr7(tsk
, val
);
701 thread
->ptrace_dr7
= val
;
707 * These access the current or another (stopped) task's io permission
708 * bitmap for debugging or core dump.
710 static int ioperm_active(struct task_struct
*target
,
711 const struct user_regset
*regset
)
713 struct io_bitmap
*iobm
= target
->thread
.io_bitmap
;
715 return iobm
? DIV_ROUND_UP(iobm
->max
, regset
->size
) : 0;
718 static int ioperm_get(struct task_struct
*target
,
719 const struct user_regset
*regset
,
720 unsigned int pos
, unsigned int count
,
721 void *kbuf
, void __user
*ubuf
)
723 struct io_bitmap
*iobm
= target
->thread
.io_bitmap
;
728 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
729 iobm
->bitmap
, 0, IO_BITMAP_BYTES
);
733 * Called by kernel/ptrace.c when detaching..
735 * Make sure the single step bit is not set.
737 void ptrace_disable(struct task_struct
*child
)
739 user_disable_single_step(child
);
742 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
743 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
746 long arch_ptrace(struct task_struct
*child
, long request
,
747 unsigned long addr
, unsigned long data
)
750 unsigned long __user
*datap
= (unsigned long __user
*)data
;
753 /* read the word at location addr in the USER area. */
754 case PTRACE_PEEKUSR
: {
758 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
761 tmp
= 0; /* Default return condition */
762 if (addr
< sizeof(struct user_regs_struct
))
763 tmp
= getreg(child
, addr
);
764 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
765 addr
<= offsetof(struct user
, u_debugreg
[7])) {
766 addr
-= offsetof(struct user
, u_debugreg
[0]);
767 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
769 ret
= put_user(tmp
, datap
);
773 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
775 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
778 if (addr
< sizeof(struct user_regs_struct
))
779 ret
= putreg(child
, addr
, data
);
780 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
781 addr
<= offsetof(struct user
, u_debugreg
[7])) {
782 addr
-= offsetof(struct user
, u_debugreg
[0]);
783 ret
= ptrace_set_debugreg(child
,
784 addr
/ sizeof(data
), data
);
788 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
789 return copy_regset_to_user(child
,
790 task_user_regset_view(current
),
792 0, sizeof(struct user_regs_struct
),
795 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
796 return copy_regset_from_user(child
,
797 task_user_regset_view(current
),
799 0, sizeof(struct user_regs_struct
),
802 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
803 return copy_regset_to_user(child
,
804 task_user_regset_view(current
),
806 0, sizeof(struct user_i387_struct
),
809 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
810 return copy_regset_from_user(child
,
811 task_user_regset_view(current
),
813 0, sizeof(struct user_i387_struct
),
817 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
818 return copy_regset_to_user(child
, &user_x86_32_view
,
820 0, sizeof(struct user_fxsr_struct
),
823 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
824 return copy_regset_from_user(child
, &user_x86_32_view
,
826 0, sizeof(struct user_fxsr_struct
),
830 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
831 case PTRACE_GET_THREAD_AREA
:
834 ret
= do_get_thread_area(child
, addr
,
835 (struct user_desc __user
*)data
);
838 case PTRACE_SET_THREAD_AREA
:
841 ret
= do_set_thread_area(child
, addr
,
842 (struct user_desc __user
*)data
, 0);
847 /* normal 64bit interface to access TLS data.
848 Works just like arch_prctl, except that the arguments
850 case PTRACE_ARCH_PRCTL
:
851 ret
= do_arch_prctl_64(child
, data
, addr
);
856 ret
= ptrace_request(child
, request
, addr
, data
);
863 #ifdef CONFIG_IA32_EMULATION
865 #include <linux/compat.h>
866 #include <linux/syscalls.h>
867 #include <asm/ia32.h>
868 #include <asm/user32.h>
871 case offsetof(struct user32, regs.l): \
872 regs->q = value; break
875 case offsetof(struct user32, regs.rs): \
876 return set_segment_reg(child, \
877 offsetof(struct user_regs_struct, rs), \
881 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
883 struct pt_regs
*regs
= task_pt_regs(child
);
904 case offsetof(struct user32
, regs
.orig_eax
):
906 * Warning: bizarre corner case fixup here. A 32-bit
907 * debugger setting orig_eax to -1 wants to disable
908 * syscall restart. Make sure that the syscall
909 * restart code sign-extends orig_ax. Also make sure
910 * we interpret the -ERESTART* codes correctly if
911 * loaded into regs->ax in case the task is not
912 * actually still sitting at the exit from a 32-bit
913 * syscall with TS_COMPAT still set.
915 regs
->orig_ax
= value
;
916 if (syscall_get_nr(child
, regs
) >= 0)
917 child
->thread_info
.status
|= TS_I386_REGS_POKED
;
920 case offsetof(struct user32
, regs
.eflags
):
921 return set_flags(child
, value
);
923 case offsetof(struct user32
, u_debugreg
[0]) ...
924 offsetof(struct user32
, u_debugreg
[7]):
925 regno
-= offsetof(struct user32
, u_debugreg
[0]);
926 return ptrace_set_debugreg(child
, regno
/ 4, value
);
929 if (regno
> sizeof(struct user32
) || (regno
& 3))
933 * Other dummy fields in the virtual user structure
945 case offsetof(struct user32, regs.l): \
946 *val = regs->q; break
949 case offsetof(struct user32, regs.rs): \
950 *val = get_segment_reg(child, \
951 offsetof(struct user_regs_struct, rs)); \
954 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
956 struct pt_regs
*regs
= task_pt_regs(child
);
974 R32(orig_eax
, orig_ax
);
978 case offsetof(struct user32
, regs
.eflags
):
979 *val
= get_flags(child
);
982 case offsetof(struct user32
, u_debugreg
[0]) ...
983 offsetof(struct user32
, u_debugreg
[7]):
984 regno
-= offsetof(struct user32
, u_debugreg
[0]);
985 *val
= ptrace_get_debugreg(child
, regno
/ 4);
989 if (regno
> sizeof(struct user32
) || (regno
& 3))
993 * Other dummy fields in the virtual user structure
1005 static int genregs32_get(struct task_struct
*target
,
1006 const struct user_regset
*regset
,
1007 unsigned int pos
, unsigned int count
,
1008 void *kbuf
, void __user
*ubuf
)
1011 compat_ulong_t
*k
= kbuf
;
1012 while (count
>= sizeof(*k
)) {
1013 getreg32(target
, pos
, k
++);
1014 count
-= sizeof(*k
);
1018 compat_ulong_t __user
*u
= ubuf
;
1019 while (count
>= sizeof(*u
)) {
1020 compat_ulong_t word
;
1021 getreg32(target
, pos
, &word
);
1022 if (__put_user(word
, u
++))
1024 count
-= sizeof(*u
);
1032 static int genregs32_set(struct task_struct
*target
,
1033 const struct user_regset
*regset
,
1034 unsigned int pos
, unsigned int count
,
1035 const void *kbuf
, const void __user
*ubuf
)
1039 const compat_ulong_t
*k
= kbuf
;
1040 while (count
>= sizeof(*k
) && !ret
) {
1041 ret
= putreg32(target
, pos
, *k
++);
1042 count
-= sizeof(*k
);
1046 const compat_ulong_t __user
*u
= ubuf
;
1047 while (count
>= sizeof(*u
) && !ret
) {
1048 compat_ulong_t word
;
1049 ret
= __get_user(word
, u
++);
1052 ret
= putreg32(target
, pos
, word
);
1053 count
-= sizeof(*u
);
1060 static long ia32_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1061 compat_ulong_t caddr
, compat_ulong_t cdata
)
1063 unsigned long addr
= caddr
;
1064 unsigned long data
= cdata
;
1065 void __user
*datap
= compat_ptr(data
);
1070 case PTRACE_PEEKUSR
:
1071 ret
= getreg32(child
, addr
, &val
);
1073 ret
= put_user(val
, (__u32 __user
*)datap
);
1076 case PTRACE_POKEUSR
:
1077 ret
= putreg32(child
, addr
, data
);
1080 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1081 return copy_regset_to_user(child
, &user_x86_32_view
,
1083 0, sizeof(struct user_regs_struct32
),
1086 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1087 return copy_regset_from_user(child
, &user_x86_32_view
,
1089 sizeof(struct user_regs_struct32
),
1092 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1093 return copy_regset_to_user(child
, &user_x86_32_view
,
1095 sizeof(struct user_i387_ia32_struct
),
1098 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1099 return copy_regset_from_user(
1100 child
, &user_x86_32_view
, REGSET_FP
,
1101 0, sizeof(struct user_i387_ia32_struct
), datap
);
1103 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1104 return copy_regset_to_user(child
, &user_x86_32_view
,
1106 sizeof(struct user32_fxsr_struct
),
1109 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1110 return copy_regset_from_user(child
, &user_x86_32_view
,
1112 sizeof(struct user32_fxsr_struct
),
1115 case PTRACE_GET_THREAD_AREA
:
1116 case PTRACE_SET_THREAD_AREA
:
1117 return arch_ptrace(child
, request
, addr
, data
);
1120 return compat_ptrace_request(child
, request
, addr
, data
);
1125 #endif /* CONFIG_IA32_EMULATION */
1127 #ifdef CONFIG_X86_X32_ABI
1128 static long x32_arch_ptrace(struct task_struct
*child
,
1129 compat_long_t request
, compat_ulong_t caddr
,
1130 compat_ulong_t cdata
)
1132 unsigned long addr
= caddr
;
1133 unsigned long data
= cdata
;
1134 void __user
*datap
= compat_ptr(data
);
1138 /* Read 32bits at location addr in the USER area. Only allow
1139 to return the lower 32bits of segment and debug registers. */
1140 case PTRACE_PEEKUSR
: {
1144 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1145 addr
< offsetof(struct user_regs_struct
, cs
))
1148 tmp
= 0; /* Default return condition */
1149 if (addr
< sizeof(struct user_regs_struct
))
1150 tmp
= getreg(child
, addr
);
1151 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1152 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1153 addr
-= offsetof(struct user
, u_debugreg
[0]);
1154 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1156 ret
= put_user(tmp
, (__u32 __user
*)datap
);
1160 /* Write the word at location addr in the USER area. Only allow
1161 to update segment and debug registers with the upper 32bits
1163 case PTRACE_POKEUSR
:
1165 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1166 addr
< offsetof(struct user_regs_struct
, cs
))
1169 if (addr
< sizeof(struct user_regs_struct
))
1170 ret
= putreg(child
, addr
, data
);
1171 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1172 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1173 addr
-= offsetof(struct user
, u_debugreg
[0]);
1174 ret
= ptrace_set_debugreg(child
,
1175 addr
/ sizeof(data
), data
);
1179 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1180 return copy_regset_to_user(child
,
1181 task_user_regset_view(current
),
1183 0, sizeof(struct user_regs_struct
),
1186 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1187 return copy_regset_from_user(child
,
1188 task_user_regset_view(current
),
1190 0, sizeof(struct user_regs_struct
),
1193 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1194 return copy_regset_to_user(child
,
1195 task_user_regset_view(current
),
1197 0, sizeof(struct user_i387_struct
),
1200 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1201 return copy_regset_from_user(child
,
1202 task_user_regset_view(current
),
1204 0, sizeof(struct user_i387_struct
),
1208 return compat_ptrace_request(child
, request
, addr
, data
);
1215 #ifdef CONFIG_COMPAT
1216 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1217 compat_ulong_t caddr
, compat_ulong_t cdata
)
1219 #ifdef CONFIG_X86_X32_ABI
1220 if (!in_ia32_syscall())
1221 return x32_arch_ptrace(child
, request
, caddr
, cdata
);
1223 #ifdef CONFIG_IA32_EMULATION
1224 return ia32_arch_ptrace(child
, request
, caddr
, cdata
);
1229 #endif /* CONFIG_COMPAT */
1231 #ifdef CONFIG_X86_64
1233 static struct user_regset x86_64_regsets
[] __ro_after_init
= {
1234 [REGSET_GENERAL
] = {
1235 .core_note_type
= NT_PRSTATUS
,
1236 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1237 .size
= sizeof(long), .align
= sizeof(long),
1238 .get
= genregs_get
, .set
= genregs_set
1241 .core_note_type
= NT_PRFPREG
,
1242 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1243 .size
= sizeof(long), .align
= sizeof(long),
1244 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1247 .core_note_type
= NT_X86_XSTATE
,
1248 .size
= sizeof(u64
), .align
= sizeof(u64
),
1249 .active
= xstateregs_active
, .get
= xstateregs_get
,
1250 .set
= xstateregs_set
1252 [REGSET_IOPERM64
] = {
1253 .core_note_type
= NT_386_IOPERM
,
1254 .n
= IO_BITMAP_LONGS
,
1255 .size
= sizeof(long), .align
= sizeof(long),
1256 .active
= ioperm_active
, .get
= ioperm_get
1260 static const struct user_regset_view user_x86_64_view
= {
1261 .name
= "x86_64", .e_machine
= EM_X86_64
,
1262 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1265 #else /* CONFIG_X86_32 */
1267 #define user_regs_struct32 user_regs_struct
1268 #define genregs32_get genregs_get
1269 #define genregs32_set genregs_set
1271 #endif /* CONFIG_X86_64 */
1273 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1274 static struct user_regset x86_32_regsets
[] __ro_after_init
= {
1275 [REGSET_GENERAL
] = {
1276 .core_note_type
= NT_PRSTATUS
,
1277 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1278 .size
= sizeof(u32
), .align
= sizeof(u32
),
1279 .get
= genregs32_get
, .set
= genregs32_set
1282 .core_note_type
= NT_PRFPREG
,
1283 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1284 .size
= sizeof(u32
), .align
= sizeof(u32
),
1285 .active
= regset_fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1288 .core_note_type
= NT_PRXFPREG
,
1289 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1290 .size
= sizeof(u32
), .align
= sizeof(u32
),
1291 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1294 .core_note_type
= NT_X86_XSTATE
,
1295 .size
= sizeof(u64
), .align
= sizeof(u64
),
1296 .active
= xstateregs_active
, .get
= xstateregs_get
,
1297 .set
= xstateregs_set
1300 .core_note_type
= NT_386_TLS
,
1301 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1302 .size
= sizeof(struct user_desc
),
1303 .align
= sizeof(struct user_desc
),
1304 .active
= regset_tls_active
,
1305 .get
= regset_tls_get
, .set
= regset_tls_set
1307 [REGSET_IOPERM32
] = {
1308 .core_note_type
= NT_386_IOPERM
,
1309 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1310 .size
= sizeof(u32
), .align
= sizeof(u32
),
1311 .active
= ioperm_active
, .get
= ioperm_get
1315 static const struct user_regset_view user_x86_32_view
= {
1316 .name
= "i386", .e_machine
= EM_386
,
1317 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1322 * This represents bytes 464..511 in the memory layout exported through
1323 * the REGSET_XSTATE interface.
1325 u64 xstate_fx_sw_bytes
[USER_XSTATE_FX_SW_WORDS
];
1327 void __init
update_regset_xstate_info(unsigned int size
, u64 xstate_mask
)
1329 #ifdef CONFIG_X86_64
1330 x86_64_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1332 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1333 x86_32_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1335 xstate_fx_sw_bytes
[USER_XSTATE_XCR0_WORD
] = xstate_mask
;
1338 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1340 #ifdef CONFIG_IA32_EMULATION
1341 if (!user_64bit_mode(task_pt_regs(task
)))
1343 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1344 return &user_x86_32_view
;
1346 #ifdef CONFIG_X86_64
1347 return &user_x86_64_view
;
1351 void send_sigtrap(struct pt_regs
*regs
, int error_code
, int si_code
)
1353 struct task_struct
*tsk
= current
;
1355 tsk
->thread
.trap_nr
= X86_TRAP_DB
;
1356 tsk
->thread
.error_code
= error_code
;
1358 /* Send us the fake SIGTRAP */
1359 force_sig_fault(SIGTRAP
, si_code
,
1360 user_mode(regs
) ? (void __user
*)regs
->ip
: NULL
);
1363 void user_single_step_report(struct pt_regs
*regs
)
1365 send_sigtrap(regs
, 0, TRAP_BRKPT
);