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>
52 REGSET_IOPERM64
= REGSET_XFP
,
58 struct pt_regs_offset
{
63 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
64 #define REG_OFFSET_END {.name = NULL, .offset = 0}
66 static const struct pt_regs_offset regoffset_table
[] = {
90 REG_OFFSET_NAME(orig_ax
),
93 REG_OFFSET_NAME(flags
),
100 * regs_query_register_offset() - query register offset from its name
101 * @name: the name of a register
103 * regs_query_register_offset() returns the offset of a register in struct
104 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
106 int regs_query_register_offset(const char *name
)
108 const struct pt_regs_offset
*roff
;
109 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
110 if (!strcmp(roff
->name
, name
))
116 * regs_query_register_name() - query register name from its offset
117 * @offset: the offset of a register in struct pt_regs.
119 * regs_query_register_name() returns the name of a register from its
120 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
122 const char *regs_query_register_name(unsigned int offset
)
124 const struct pt_regs_offset
*roff
;
125 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
126 if (roff
->offset
== offset
)
132 * does not yet catch signals sent when the child dies.
133 * in exit.c or in signal.c.
137 * Determines which flags the user has access to [1 = access, 0 = no access].
139 #define FLAG_MASK_32 ((unsigned long) \
140 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
141 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
142 X86_EFLAGS_SF | X86_EFLAGS_TF | \
143 X86_EFLAGS_DF | X86_EFLAGS_OF | \
144 X86_EFLAGS_RF | X86_EFLAGS_AC))
147 * Determines whether a value may be installed in a segment register.
149 static inline bool invalid_selector(u16 value
)
151 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
156 #define FLAG_MASK FLAG_MASK_32
158 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
160 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
161 return ®s
->bx
+ (regno
>> 2);
164 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
167 * Returning the value truncates it to 16 bits.
170 if (offset
!= offsetof(struct user_regs_struct
, gs
))
171 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
174 retval
= get_user_gs(task_pt_regs(task
));
176 retval
= task_user_gs(task
);
181 static int set_segment_reg(struct task_struct
*task
,
182 unsigned long offset
, u16 value
)
185 * The value argument was already truncated to 16 bits.
187 if (invalid_selector(value
))
191 * For %cs and %ss we cannot permit a null selector.
192 * We can permit a bogus selector as long as it has USER_RPL.
193 * Null selectors are fine for other segment registers, but
194 * we will never get back to user mode with invalid %cs or %ss
195 * and will take the trap in iret instead. Much code relies
196 * on user_mode() to distinguish a user trap frame (which can
197 * safely use invalid selectors) from a kernel trap frame.
200 case offsetof(struct user_regs_struct
, cs
):
201 case offsetof(struct user_regs_struct
, ss
):
202 if (unlikely(value
== 0))
206 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
209 case offsetof(struct user_regs_struct
, gs
):
211 set_user_gs(task_pt_regs(task
), value
);
213 task_user_gs(task
) = value
;
219 #else /* CONFIG_X86_64 */
221 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
223 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
225 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
226 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
229 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
232 * Returning the value truncates it to 16 bits.
237 case offsetof(struct user_regs_struct
, fs
):
238 if (task
== current
) {
239 /* Older gas can't assemble movq %?s,%r?? */
240 asm("movl %%fs,%0" : "=r" (seg
));
243 return task
->thread
.fsindex
;
244 case offsetof(struct user_regs_struct
, gs
):
245 if (task
== current
) {
246 asm("movl %%gs,%0" : "=r" (seg
));
249 return task
->thread
.gsindex
;
250 case offsetof(struct user_regs_struct
, ds
):
251 if (task
== current
) {
252 asm("movl %%ds,%0" : "=r" (seg
));
255 return task
->thread
.ds
;
256 case offsetof(struct user_regs_struct
, es
):
257 if (task
== current
) {
258 asm("movl %%es,%0" : "=r" (seg
));
261 return task
->thread
.es
;
263 case offsetof(struct user_regs_struct
, cs
):
264 case offsetof(struct user_regs_struct
, ss
):
267 return *pt_regs_access(task_pt_regs(task
), offset
);
270 static int set_segment_reg(struct task_struct
*task
,
271 unsigned long offset
, u16 value
)
274 * The value argument was already truncated to 16 bits.
276 if (invalid_selector(value
))
280 case offsetof(struct user_regs_struct
,fs
):
281 task
->thread
.fsindex
= value
;
283 loadsegment(fs
, task
->thread
.fsindex
);
285 case offsetof(struct user_regs_struct
,gs
):
286 task
->thread
.gsindex
= value
;
288 load_gs_index(task
->thread
.gsindex
);
290 case offsetof(struct user_regs_struct
,ds
):
291 task
->thread
.ds
= value
;
293 loadsegment(ds
, task
->thread
.ds
);
295 case offsetof(struct user_regs_struct
,es
):
296 task
->thread
.es
= value
;
298 loadsegment(es
, task
->thread
.es
);
302 * Can't actually change these in 64-bit mode.
304 case offsetof(struct user_regs_struct
,cs
):
305 if (unlikely(value
== 0))
307 task_pt_regs(task
)->cs
= value
;
309 case offsetof(struct user_regs_struct
,ss
):
310 if (unlikely(value
== 0))
312 task_pt_regs(task
)->ss
= value
;
319 #endif /* CONFIG_X86_32 */
321 static unsigned long get_flags(struct task_struct
*task
)
323 unsigned long retval
= task_pt_regs(task
)->flags
;
326 * If the debugger set TF, hide it from the readout.
328 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
329 retval
&= ~X86_EFLAGS_TF
;
334 static int set_flags(struct task_struct
*task
, unsigned long value
)
336 struct pt_regs
*regs
= task_pt_regs(task
);
339 * If the user value contains TF, mark that
340 * it was not "us" (the debugger) that set it.
341 * If not, make sure it stays set if we had.
343 if (value
& X86_EFLAGS_TF
)
344 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
345 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
346 value
|= X86_EFLAGS_TF
;
348 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
353 static int putreg(struct task_struct
*child
,
354 unsigned long offset
, unsigned long value
)
357 case offsetof(struct user_regs_struct
, cs
):
358 case offsetof(struct user_regs_struct
, ds
):
359 case offsetof(struct user_regs_struct
, es
):
360 case offsetof(struct user_regs_struct
, fs
):
361 case offsetof(struct user_regs_struct
, gs
):
362 case offsetof(struct user_regs_struct
, ss
):
363 return set_segment_reg(child
, offset
, value
);
365 case offsetof(struct user_regs_struct
, flags
):
366 return set_flags(child
, value
);
369 case offsetof(struct user_regs_struct
,fs_base
):
370 if (value
>= TASK_SIZE_MAX
)
373 * When changing the FS base, use do_arch_prctl_64()
374 * to set the index to zero and to set the base
377 if (child
->thread
.fsbase
!= value
)
378 return do_arch_prctl_64(child
, ARCH_SET_FS
, value
);
380 case offsetof(struct user_regs_struct
,gs_base
):
382 * Exactly the same here as the %fs handling above.
384 if (value
>= TASK_SIZE_MAX
)
386 if (child
->thread
.gsbase
!= value
)
387 return do_arch_prctl_64(child
, ARCH_SET_GS
, value
);
392 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
396 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
399 case offsetof(struct user_regs_struct
, cs
):
400 case offsetof(struct user_regs_struct
, ds
):
401 case offsetof(struct user_regs_struct
, es
):
402 case offsetof(struct user_regs_struct
, fs
):
403 case offsetof(struct user_regs_struct
, gs
):
404 case offsetof(struct user_regs_struct
, ss
):
405 return get_segment_reg(task
, offset
);
407 case offsetof(struct user_regs_struct
, flags
):
408 return get_flags(task
);
411 case offsetof(struct user_regs_struct
, fs_base
):
412 return x86_fsbase_read_task(task
);
413 case offsetof(struct user_regs_struct
, gs_base
):
414 return x86_gsbase_read_task(task
);
418 return *pt_regs_access(task_pt_regs(task
), offset
);
421 static int genregs_get(struct task_struct
*target
,
422 const struct user_regset
*regset
,
423 unsigned int pos
, unsigned int count
,
424 void *kbuf
, void __user
*ubuf
)
427 unsigned long *k
= kbuf
;
428 while (count
>= sizeof(*k
)) {
429 *k
++ = getreg(target
, pos
);
434 unsigned long __user
*u
= ubuf
;
435 while (count
>= sizeof(*u
)) {
436 if (__put_user(getreg(target
, pos
), u
++))
446 static int genregs_set(struct task_struct
*target
,
447 const struct user_regset
*regset
,
448 unsigned int pos
, unsigned int count
,
449 const void *kbuf
, const void __user
*ubuf
)
453 const unsigned long *k
= kbuf
;
454 while (count
>= sizeof(*k
) && !ret
) {
455 ret
= putreg(target
, pos
, *k
++);
460 const unsigned long __user
*u
= ubuf
;
461 while (count
>= sizeof(*u
) && !ret
) {
463 ret
= __get_user(word
, u
++);
466 ret
= putreg(target
, pos
, word
);
474 static void ptrace_triggered(struct perf_event
*bp
,
475 struct perf_sample_data
*data
,
476 struct pt_regs
*regs
)
479 struct thread_struct
*thread
= &(current
->thread
);
482 * Store in the virtual DR6 register the fact that the breakpoint
483 * was hit so the thread's debugger will see it.
485 for (i
= 0; i
< HBP_NUM
; i
++) {
486 if (thread
->ptrace_bps
[i
] == bp
)
490 thread
->debugreg6
|= (DR_TRAP0
<< i
);
494 * Walk through every ptrace breakpoints for this thread and
495 * build the dr7 value on top of their attributes.
498 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
502 struct arch_hw_breakpoint
*info
;
504 for (i
= 0; i
< HBP_NUM
; i
++) {
505 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
506 info
= counter_arch_bp(bp
[i
]);
507 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
514 static int ptrace_fill_bp_fields(struct perf_event_attr
*attr
,
515 int len
, int type
, bool disabled
)
517 int err
, bp_len
, bp_type
;
519 err
= arch_bp_generic_fields(len
, type
, &bp_len
, &bp_type
);
521 attr
->bp_len
= bp_len
;
522 attr
->bp_type
= bp_type
;
523 attr
->disabled
= disabled
;
529 static struct perf_event
*
530 ptrace_register_breakpoint(struct task_struct
*tsk
, int len
, int type
,
531 unsigned long addr
, bool disabled
)
533 struct perf_event_attr attr
;
536 ptrace_breakpoint_init(&attr
);
539 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
543 return register_user_hw_breakpoint(&attr
, ptrace_triggered
,
547 static int ptrace_modify_breakpoint(struct perf_event
*bp
, int len
, int type
,
550 struct perf_event_attr attr
= bp
->attr
;
553 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
557 return modify_user_hw_breakpoint(bp
, &attr
);
561 * Handle ptrace writes to debug register 7.
563 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
565 struct thread_struct
*thread
= &tsk
->thread
;
566 unsigned long old_dr7
;
567 bool second_pass
= false;
570 data
&= ~DR_CONTROL_RESERVED
;
571 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
575 for (i
= 0; i
< HBP_NUM
; i
++) {
577 bool disabled
= !decode_dr7(data
, i
, &len
, &type
);
578 struct perf_event
*bp
= thread
->ptrace_bps
[i
];
584 bp
= ptrace_register_breakpoint(tsk
,
585 len
, type
, 0, disabled
);
591 thread
->ptrace_bps
[i
] = bp
;
595 rc
= ptrace_modify_breakpoint(bp
, len
, type
, disabled
);
600 /* Restore if the first pass failed, second_pass shouldn't fail. */
601 if (rc
&& !WARN_ON(second_pass
)) {
612 * Handle PTRACE_PEEKUSR calls for the debug register area.
614 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
616 struct thread_struct
*thread
= &tsk
->thread
;
617 unsigned long val
= 0;
620 int index
= array_index_nospec(n
, HBP_NUM
);
621 struct perf_event
*bp
= thread
->ptrace_bps
[index
];
624 val
= bp
->hw
.info
.address
;
626 val
= thread
->debugreg6
;
628 val
= thread
->ptrace_dr7
;
633 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
636 struct thread_struct
*t
= &tsk
->thread
;
637 struct perf_event
*bp
= t
->ptrace_bps
[nr
];
642 * Put stub len and type to create an inactive but correct bp.
644 * CHECKME: the previous code returned -EIO if the addr wasn't
645 * a valid task virtual addr. The new one will return -EINVAL in
647 * -EINVAL may be what we want for in-kernel breakpoints users,
648 * but -EIO looks better for ptrace, since we refuse a register
649 * writing for the user. And anyway this is the previous
652 bp
= ptrace_register_breakpoint(tsk
,
653 X86_BREAKPOINT_LEN_1
, X86_BREAKPOINT_WRITE
,
658 t
->ptrace_bps
[nr
] = bp
;
660 struct perf_event_attr attr
= bp
->attr
;
663 err
= modify_user_hw_breakpoint(bp
, &attr
);
670 * Handle PTRACE_POKEUSR calls for the debug register area.
672 static int ptrace_set_debugreg(struct task_struct
*tsk
, int n
,
675 struct thread_struct
*thread
= &tsk
->thread
;
676 /* There are no DR4 or DR5 registers */
680 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
682 thread
->debugreg6
= val
;
685 rc
= ptrace_write_dr7(tsk
, val
);
687 thread
->ptrace_dr7
= val
;
693 * These access the current or another (stopped) task's io permission
694 * bitmap for debugging or core dump.
696 static int ioperm_active(struct task_struct
*target
,
697 const struct user_regset
*regset
)
699 return target
->thread
.io_bitmap_max
/ regset
->size
;
702 static int ioperm_get(struct task_struct
*target
,
703 const struct user_regset
*regset
,
704 unsigned int pos
, unsigned int count
,
705 void *kbuf
, void __user
*ubuf
)
707 if (!target
->thread
.io_bitmap_ptr
)
710 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
711 target
->thread
.io_bitmap_ptr
,
716 * Called by kernel/ptrace.c when detaching..
718 * Make sure the single step bit is not set.
720 void ptrace_disable(struct task_struct
*child
)
722 user_disable_single_step(child
);
725 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
726 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
729 long arch_ptrace(struct task_struct
*child
, long request
,
730 unsigned long addr
, unsigned long data
)
733 unsigned long __user
*datap
= (unsigned long __user
*)data
;
736 /* read the word at location addr in the USER area. */
737 case PTRACE_PEEKUSR
: {
741 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
744 tmp
= 0; /* Default return condition */
745 if (addr
< sizeof(struct user_regs_struct
))
746 tmp
= getreg(child
, addr
);
747 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
748 addr
<= offsetof(struct user
, u_debugreg
[7])) {
749 addr
-= offsetof(struct user
, u_debugreg
[0]);
750 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
752 ret
= put_user(tmp
, datap
);
756 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
758 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
761 if (addr
< sizeof(struct user_regs_struct
))
762 ret
= putreg(child
, addr
, data
);
763 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
764 addr
<= offsetof(struct user
, u_debugreg
[7])) {
765 addr
-= offsetof(struct user
, u_debugreg
[0]);
766 ret
= ptrace_set_debugreg(child
,
767 addr
/ sizeof(data
), data
);
771 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
772 return copy_regset_to_user(child
,
773 task_user_regset_view(current
),
775 0, sizeof(struct user_regs_struct
),
778 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
779 return copy_regset_from_user(child
,
780 task_user_regset_view(current
),
782 0, sizeof(struct user_regs_struct
),
785 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
786 return copy_regset_to_user(child
,
787 task_user_regset_view(current
),
789 0, sizeof(struct user_i387_struct
),
792 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
793 return copy_regset_from_user(child
,
794 task_user_regset_view(current
),
796 0, sizeof(struct user_i387_struct
),
800 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
801 return copy_regset_to_user(child
, &user_x86_32_view
,
803 0, sizeof(struct user_fxsr_struct
),
806 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
807 return copy_regset_from_user(child
, &user_x86_32_view
,
809 0, sizeof(struct user_fxsr_struct
),
813 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
814 case PTRACE_GET_THREAD_AREA
:
817 ret
= do_get_thread_area(child
, addr
,
818 (struct user_desc __user
*)data
);
821 case PTRACE_SET_THREAD_AREA
:
824 ret
= do_set_thread_area(child
, addr
,
825 (struct user_desc __user
*)data
, 0);
830 /* normal 64bit interface to access TLS data.
831 Works just like arch_prctl, except that the arguments
833 case PTRACE_ARCH_PRCTL
:
834 ret
= do_arch_prctl_64(child
, data
, addr
);
839 ret
= ptrace_request(child
, request
, addr
, data
);
846 #ifdef CONFIG_IA32_EMULATION
848 #include <linux/compat.h>
849 #include <linux/syscalls.h>
850 #include <asm/ia32.h>
851 #include <asm/user32.h>
854 case offsetof(struct user32, regs.l): \
855 regs->q = value; break
858 case offsetof(struct user32, regs.rs): \
859 return set_segment_reg(child, \
860 offsetof(struct user_regs_struct, rs), \
864 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
866 struct pt_regs
*regs
= task_pt_regs(child
);
887 case offsetof(struct user32
, regs
.orig_eax
):
889 * Warning: bizarre corner case fixup here. A 32-bit
890 * debugger setting orig_eax to -1 wants to disable
891 * syscall restart. Make sure that the syscall
892 * restart code sign-extends orig_ax. Also make sure
893 * we interpret the -ERESTART* codes correctly if
894 * loaded into regs->ax in case the task is not
895 * actually still sitting at the exit from a 32-bit
896 * syscall with TS_COMPAT still set.
898 regs
->orig_ax
= value
;
899 if (syscall_get_nr(child
, regs
) >= 0)
900 child
->thread_info
.status
|= TS_I386_REGS_POKED
;
903 case offsetof(struct user32
, regs
.eflags
):
904 return set_flags(child
, value
);
906 case offsetof(struct user32
, u_debugreg
[0]) ...
907 offsetof(struct user32
, u_debugreg
[7]):
908 regno
-= offsetof(struct user32
, u_debugreg
[0]);
909 return ptrace_set_debugreg(child
, regno
/ 4, value
);
912 if (regno
> sizeof(struct user32
) || (regno
& 3))
916 * Other dummy fields in the virtual user structure
928 case offsetof(struct user32, regs.l): \
929 *val = regs->q; break
932 case offsetof(struct user32, regs.rs): \
933 *val = get_segment_reg(child, \
934 offsetof(struct user_regs_struct, rs)); \
937 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
939 struct pt_regs
*regs
= task_pt_regs(child
);
957 R32(orig_eax
, orig_ax
);
961 case offsetof(struct user32
, regs
.eflags
):
962 *val
= get_flags(child
);
965 case offsetof(struct user32
, u_debugreg
[0]) ...
966 offsetof(struct user32
, u_debugreg
[7]):
967 regno
-= offsetof(struct user32
, u_debugreg
[0]);
968 *val
= ptrace_get_debugreg(child
, regno
/ 4);
972 if (regno
> sizeof(struct user32
) || (regno
& 3))
976 * Other dummy fields in the virtual user structure
988 static int genregs32_get(struct task_struct
*target
,
989 const struct user_regset
*regset
,
990 unsigned int pos
, unsigned int count
,
991 void *kbuf
, void __user
*ubuf
)
994 compat_ulong_t
*k
= kbuf
;
995 while (count
>= sizeof(*k
)) {
996 getreg32(target
, pos
, k
++);
1001 compat_ulong_t __user
*u
= ubuf
;
1002 while (count
>= sizeof(*u
)) {
1003 compat_ulong_t word
;
1004 getreg32(target
, pos
, &word
);
1005 if (__put_user(word
, u
++))
1007 count
-= sizeof(*u
);
1015 static int genregs32_set(struct task_struct
*target
,
1016 const struct user_regset
*regset
,
1017 unsigned int pos
, unsigned int count
,
1018 const void *kbuf
, const void __user
*ubuf
)
1022 const compat_ulong_t
*k
= kbuf
;
1023 while (count
>= sizeof(*k
) && !ret
) {
1024 ret
= putreg32(target
, pos
, *k
++);
1025 count
-= sizeof(*k
);
1029 const compat_ulong_t __user
*u
= ubuf
;
1030 while (count
>= sizeof(*u
) && !ret
) {
1031 compat_ulong_t word
;
1032 ret
= __get_user(word
, u
++);
1035 ret
= putreg32(target
, pos
, word
);
1036 count
-= sizeof(*u
);
1043 static long ia32_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1044 compat_ulong_t caddr
, compat_ulong_t cdata
)
1046 unsigned long addr
= caddr
;
1047 unsigned long data
= cdata
;
1048 void __user
*datap
= compat_ptr(data
);
1053 case PTRACE_PEEKUSR
:
1054 ret
= getreg32(child
, addr
, &val
);
1056 ret
= put_user(val
, (__u32 __user
*)datap
);
1059 case PTRACE_POKEUSR
:
1060 ret
= putreg32(child
, addr
, data
);
1063 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1064 return copy_regset_to_user(child
, &user_x86_32_view
,
1066 0, sizeof(struct user_regs_struct32
),
1069 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1070 return copy_regset_from_user(child
, &user_x86_32_view
,
1072 sizeof(struct user_regs_struct32
),
1075 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1076 return copy_regset_to_user(child
, &user_x86_32_view
,
1078 sizeof(struct user_i387_ia32_struct
),
1081 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1082 return copy_regset_from_user(
1083 child
, &user_x86_32_view
, REGSET_FP
,
1084 0, sizeof(struct user_i387_ia32_struct
), datap
);
1086 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1087 return copy_regset_to_user(child
, &user_x86_32_view
,
1089 sizeof(struct user32_fxsr_struct
),
1092 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1093 return copy_regset_from_user(child
, &user_x86_32_view
,
1095 sizeof(struct user32_fxsr_struct
),
1098 case PTRACE_GET_THREAD_AREA
:
1099 case PTRACE_SET_THREAD_AREA
:
1100 return arch_ptrace(child
, request
, addr
, data
);
1103 return compat_ptrace_request(child
, request
, addr
, data
);
1108 #endif /* CONFIG_IA32_EMULATION */
1110 #ifdef CONFIG_X86_X32_ABI
1111 static long x32_arch_ptrace(struct task_struct
*child
,
1112 compat_long_t request
, compat_ulong_t caddr
,
1113 compat_ulong_t cdata
)
1115 unsigned long addr
= caddr
;
1116 unsigned long data
= cdata
;
1117 void __user
*datap
= compat_ptr(data
);
1121 /* Read 32bits at location addr in the USER area. Only allow
1122 to return the lower 32bits of segment and debug registers. */
1123 case PTRACE_PEEKUSR
: {
1127 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1128 addr
< offsetof(struct user_regs_struct
, cs
))
1131 tmp
= 0; /* Default return condition */
1132 if (addr
< sizeof(struct user_regs_struct
))
1133 tmp
= getreg(child
, addr
);
1134 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1135 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1136 addr
-= offsetof(struct user
, u_debugreg
[0]);
1137 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1139 ret
= put_user(tmp
, (__u32 __user
*)datap
);
1143 /* Write the word at location addr in the USER area. Only allow
1144 to update segment and debug registers with the upper 32bits
1146 case PTRACE_POKEUSR
:
1148 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1149 addr
< offsetof(struct user_regs_struct
, cs
))
1152 if (addr
< sizeof(struct user_regs_struct
))
1153 ret
= putreg(child
, addr
, data
);
1154 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1155 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1156 addr
-= offsetof(struct user
, u_debugreg
[0]);
1157 ret
= ptrace_set_debugreg(child
,
1158 addr
/ sizeof(data
), data
);
1162 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1163 return copy_regset_to_user(child
,
1164 task_user_regset_view(current
),
1166 0, sizeof(struct user_regs_struct
),
1169 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1170 return copy_regset_from_user(child
,
1171 task_user_regset_view(current
),
1173 0, sizeof(struct user_regs_struct
),
1176 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1177 return copy_regset_to_user(child
,
1178 task_user_regset_view(current
),
1180 0, sizeof(struct user_i387_struct
),
1183 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1184 return copy_regset_from_user(child
,
1185 task_user_regset_view(current
),
1187 0, sizeof(struct user_i387_struct
),
1191 return compat_ptrace_request(child
, request
, addr
, data
);
1198 #ifdef CONFIG_COMPAT
1199 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1200 compat_ulong_t caddr
, compat_ulong_t cdata
)
1202 #ifdef CONFIG_X86_X32_ABI
1203 if (!in_ia32_syscall())
1204 return x32_arch_ptrace(child
, request
, caddr
, cdata
);
1206 #ifdef CONFIG_IA32_EMULATION
1207 return ia32_arch_ptrace(child
, request
, caddr
, cdata
);
1212 #endif /* CONFIG_COMPAT */
1214 #ifdef CONFIG_X86_64
1216 static struct user_regset x86_64_regsets
[] __ro_after_init
= {
1217 [REGSET_GENERAL
] = {
1218 .core_note_type
= NT_PRSTATUS
,
1219 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1220 .size
= sizeof(long), .align
= sizeof(long),
1221 .get
= genregs_get
, .set
= genregs_set
1224 .core_note_type
= NT_PRFPREG
,
1225 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1226 .size
= sizeof(long), .align
= sizeof(long),
1227 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1230 .core_note_type
= NT_X86_XSTATE
,
1231 .size
= sizeof(u64
), .align
= sizeof(u64
),
1232 .active
= xstateregs_active
, .get
= xstateregs_get
,
1233 .set
= xstateregs_set
1235 [REGSET_IOPERM64
] = {
1236 .core_note_type
= NT_386_IOPERM
,
1237 .n
= IO_BITMAP_LONGS
,
1238 .size
= sizeof(long), .align
= sizeof(long),
1239 .active
= ioperm_active
, .get
= ioperm_get
1243 static const struct user_regset_view user_x86_64_view
= {
1244 .name
= "x86_64", .e_machine
= EM_X86_64
,
1245 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1248 #else /* CONFIG_X86_32 */
1250 #define user_regs_struct32 user_regs_struct
1251 #define genregs32_get genregs_get
1252 #define genregs32_set genregs_set
1254 #endif /* CONFIG_X86_64 */
1256 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1257 static struct user_regset x86_32_regsets
[] __ro_after_init
= {
1258 [REGSET_GENERAL
] = {
1259 .core_note_type
= NT_PRSTATUS
,
1260 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1261 .size
= sizeof(u32
), .align
= sizeof(u32
),
1262 .get
= genregs32_get
, .set
= genregs32_set
1265 .core_note_type
= NT_PRFPREG
,
1266 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1267 .size
= sizeof(u32
), .align
= sizeof(u32
),
1268 .active
= regset_fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1271 .core_note_type
= NT_PRXFPREG
,
1272 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1273 .size
= sizeof(u32
), .align
= sizeof(u32
),
1274 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1277 .core_note_type
= NT_X86_XSTATE
,
1278 .size
= sizeof(u64
), .align
= sizeof(u64
),
1279 .active
= xstateregs_active
, .get
= xstateregs_get
,
1280 .set
= xstateregs_set
1283 .core_note_type
= NT_386_TLS
,
1284 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1285 .size
= sizeof(struct user_desc
),
1286 .align
= sizeof(struct user_desc
),
1287 .active
= regset_tls_active
,
1288 .get
= regset_tls_get
, .set
= regset_tls_set
1290 [REGSET_IOPERM32
] = {
1291 .core_note_type
= NT_386_IOPERM
,
1292 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1293 .size
= sizeof(u32
), .align
= sizeof(u32
),
1294 .active
= ioperm_active
, .get
= ioperm_get
1298 static const struct user_regset_view user_x86_32_view
= {
1299 .name
= "i386", .e_machine
= EM_386
,
1300 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1305 * This represents bytes 464..511 in the memory layout exported through
1306 * the REGSET_XSTATE interface.
1308 u64 xstate_fx_sw_bytes
[USER_XSTATE_FX_SW_WORDS
];
1310 void __init
update_regset_xstate_info(unsigned int size
, u64 xstate_mask
)
1312 #ifdef CONFIG_X86_64
1313 x86_64_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1315 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1316 x86_32_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1318 xstate_fx_sw_bytes
[USER_XSTATE_XCR0_WORD
] = xstate_mask
;
1321 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1323 #ifdef CONFIG_IA32_EMULATION
1324 if (!user_64bit_mode(task_pt_regs(task
)))
1326 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1327 return &user_x86_32_view
;
1329 #ifdef CONFIG_X86_64
1330 return &user_x86_64_view
;
1334 void send_sigtrap(struct pt_regs
*regs
, int error_code
, int si_code
)
1336 struct task_struct
*tsk
= current
;
1338 tsk
->thread
.trap_nr
= X86_TRAP_DB
;
1339 tsk
->thread
.error_code
= error_code
;
1341 /* Send us the fake SIGTRAP */
1342 force_sig_fault(SIGTRAP
, si_code
,
1343 user_mode(regs
) ? (void __user
*)regs
->ip
: NULL
);
1346 void user_single_step_report(struct pt_regs
*regs
)
1348 send_sigtrap(regs
, 0, TRAP_BRKPT
);