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
)
372 x86_fsbase_write_task(child
, value
);
374 case offsetof(struct user_regs_struct
,gs_base
):
375 if (value
>= TASK_SIZE_MAX
)
377 x86_gsbase_write_task(child
, value
);
382 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
386 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
389 case offsetof(struct user_regs_struct
, cs
):
390 case offsetof(struct user_regs_struct
, ds
):
391 case offsetof(struct user_regs_struct
, es
):
392 case offsetof(struct user_regs_struct
, fs
):
393 case offsetof(struct user_regs_struct
, gs
):
394 case offsetof(struct user_regs_struct
, ss
):
395 return get_segment_reg(task
, offset
);
397 case offsetof(struct user_regs_struct
, flags
):
398 return get_flags(task
);
401 case offsetof(struct user_regs_struct
, fs_base
):
402 return x86_fsbase_read_task(task
);
403 case offsetof(struct user_regs_struct
, gs_base
):
404 return x86_gsbase_read_task(task
);
408 return *pt_regs_access(task_pt_regs(task
), offset
);
411 static int genregs_get(struct task_struct
*target
,
412 const struct user_regset
*regset
,
413 unsigned int pos
, unsigned int count
,
414 void *kbuf
, void __user
*ubuf
)
417 unsigned long *k
= kbuf
;
418 while (count
>= sizeof(*k
)) {
419 *k
++ = getreg(target
, pos
);
424 unsigned long __user
*u
= ubuf
;
425 while (count
>= sizeof(*u
)) {
426 if (__put_user(getreg(target
, pos
), u
++))
436 static int genregs_set(struct task_struct
*target
,
437 const struct user_regset
*regset
,
438 unsigned int pos
, unsigned int count
,
439 const void *kbuf
, const void __user
*ubuf
)
443 const unsigned long *k
= kbuf
;
444 while (count
>= sizeof(*k
) && !ret
) {
445 ret
= putreg(target
, pos
, *k
++);
450 const unsigned long __user
*u
= ubuf
;
451 while (count
>= sizeof(*u
) && !ret
) {
453 ret
= __get_user(word
, u
++);
456 ret
= putreg(target
, pos
, word
);
464 static void ptrace_triggered(struct perf_event
*bp
,
465 struct perf_sample_data
*data
,
466 struct pt_regs
*regs
)
469 struct thread_struct
*thread
= &(current
->thread
);
472 * Store in the virtual DR6 register the fact that the breakpoint
473 * was hit so the thread's debugger will see it.
475 for (i
= 0; i
< HBP_NUM
; i
++) {
476 if (thread
->ptrace_bps
[i
] == bp
)
480 thread
->debugreg6
|= (DR_TRAP0
<< i
);
484 * Walk through every ptrace breakpoints for this thread and
485 * build the dr7 value on top of their attributes.
488 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
492 struct arch_hw_breakpoint
*info
;
494 for (i
= 0; i
< HBP_NUM
; i
++) {
495 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
496 info
= counter_arch_bp(bp
[i
]);
497 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
504 static int ptrace_fill_bp_fields(struct perf_event_attr
*attr
,
505 int len
, int type
, bool disabled
)
507 int err
, bp_len
, bp_type
;
509 err
= arch_bp_generic_fields(len
, type
, &bp_len
, &bp_type
);
511 attr
->bp_len
= bp_len
;
512 attr
->bp_type
= bp_type
;
513 attr
->disabled
= disabled
;
519 static struct perf_event
*
520 ptrace_register_breakpoint(struct task_struct
*tsk
, int len
, int type
,
521 unsigned long addr
, bool disabled
)
523 struct perf_event_attr attr
;
526 ptrace_breakpoint_init(&attr
);
529 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
533 return register_user_hw_breakpoint(&attr
, ptrace_triggered
,
537 static int ptrace_modify_breakpoint(struct perf_event
*bp
, int len
, int type
,
540 struct perf_event_attr attr
= bp
->attr
;
543 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
547 return modify_user_hw_breakpoint(bp
, &attr
);
551 * Handle ptrace writes to debug register 7.
553 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
555 struct thread_struct
*thread
= &tsk
->thread
;
556 unsigned long old_dr7
;
557 bool second_pass
= false;
560 data
&= ~DR_CONTROL_RESERVED
;
561 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
565 for (i
= 0; i
< HBP_NUM
; i
++) {
567 bool disabled
= !decode_dr7(data
, i
, &len
, &type
);
568 struct perf_event
*bp
= thread
->ptrace_bps
[i
];
574 bp
= ptrace_register_breakpoint(tsk
,
575 len
, type
, 0, disabled
);
581 thread
->ptrace_bps
[i
] = bp
;
585 rc
= ptrace_modify_breakpoint(bp
, len
, type
, disabled
);
590 /* Restore if the first pass failed, second_pass shouldn't fail. */
591 if (rc
&& !WARN_ON(second_pass
)) {
602 * Handle PTRACE_PEEKUSR calls for the debug register area.
604 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
606 struct thread_struct
*thread
= &tsk
->thread
;
607 unsigned long val
= 0;
610 int index
= array_index_nospec(n
, HBP_NUM
);
611 struct perf_event
*bp
= thread
->ptrace_bps
[index
];
614 val
= bp
->hw
.info
.address
;
616 val
= thread
->debugreg6
;
618 val
= thread
->ptrace_dr7
;
623 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
626 struct thread_struct
*t
= &tsk
->thread
;
627 struct perf_event
*bp
= t
->ptrace_bps
[nr
];
632 * Put stub len and type to create an inactive but correct bp.
634 * CHECKME: the previous code returned -EIO if the addr wasn't
635 * a valid task virtual addr. The new one will return -EINVAL in
637 * -EINVAL may be what we want for in-kernel breakpoints users,
638 * but -EIO looks better for ptrace, since we refuse a register
639 * writing for the user. And anyway this is the previous
642 bp
= ptrace_register_breakpoint(tsk
,
643 X86_BREAKPOINT_LEN_1
, X86_BREAKPOINT_WRITE
,
648 t
->ptrace_bps
[nr
] = bp
;
650 struct perf_event_attr attr
= bp
->attr
;
653 err
= modify_user_hw_breakpoint(bp
, &attr
);
660 * Handle PTRACE_POKEUSR calls for the debug register area.
662 static int ptrace_set_debugreg(struct task_struct
*tsk
, int n
,
665 struct thread_struct
*thread
= &tsk
->thread
;
666 /* There are no DR4 or DR5 registers */
670 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
672 thread
->debugreg6
= val
;
675 rc
= ptrace_write_dr7(tsk
, val
);
677 thread
->ptrace_dr7
= val
;
683 * These access the current or another (stopped) task's io permission
684 * bitmap for debugging or core dump.
686 static int ioperm_active(struct task_struct
*target
,
687 const struct user_regset
*regset
)
689 return target
->thread
.io_bitmap_max
/ regset
->size
;
692 static int ioperm_get(struct task_struct
*target
,
693 const struct user_regset
*regset
,
694 unsigned int pos
, unsigned int count
,
695 void *kbuf
, void __user
*ubuf
)
697 if (!target
->thread
.io_bitmap_ptr
)
700 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
701 target
->thread
.io_bitmap_ptr
,
706 * Called by kernel/ptrace.c when detaching..
708 * Make sure the single step bit is not set.
710 void ptrace_disable(struct task_struct
*child
)
712 user_disable_single_step(child
);
715 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
716 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
719 long arch_ptrace(struct task_struct
*child
, long request
,
720 unsigned long addr
, unsigned long data
)
723 unsigned long __user
*datap
= (unsigned long __user
*)data
;
726 /* read the word at location addr in the USER area. */
727 case PTRACE_PEEKUSR
: {
731 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
734 tmp
= 0; /* Default return condition */
735 if (addr
< sizeof(struct user_regs_struct
))
736 tmp
= getreg(child
, addr
);
737 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
738 addr
<= offsetof(struct user
, u_debugreg
[7])) {
739 addr
-= offsetof(struct user
, u_debugreg
[0]);
740 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
742 ret
= put_user(tmp
, datap
);
746 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
748 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
751 if (addr
< sizeof(struct user_regs_struct
))
752 ret
= putreg(child
, addr
, data
);
753 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
754 addr
<= offsetof(struct user
, u_debugreg
[7])) {
755 addr
-= offsetof(struct user
, u_debugreg
[0]);
756 ret
= ptrace_set_debugreg(child
,
757 addr
/ sizeof(data
), data
);
761 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
762 return copy_regset_to_user(child
,
763 task_user_regset_view(current
),
765 0, sizeof(struct user_regs_struct
),
768 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
769 return copy_regset_from_user(child
,
770 task_user_regset_view(current
),
772 0, sizeof(struct user_regs_struct
),
775 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
776 return copy_regset_to_user(child
,
777 task_user_regset_view(current
),
779 0, sizeof(struct user_i387_struct
),
782 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
783 return copy_regset_from_user(child
,
784 task_user_regset_view(current
),
786 0, sizeof(struct user_i387_struct
),
790 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
791 return copy_regset_to_user(child
, &user_x86_32_view
,
793 0, sizeof(struct user_fxsr_struct
),
796 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
797 return copy_regset_from_user(child
, &user_x86_32_view
,
799 0, sizeof(struct user_fxsr_struct
),
803 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
804 case PTRACE_GET_THREAD_AREA
:
807 ret
= do_get_thread_area(child
, addr
,
808 (struct user_desc __user
*)data
);
811 case PTRACE_SET_THREAD_AREA
:
814 ret
= do_set_thread_area(child
, addr
,
815 (struct user_desc __user
*)data
, 0);
820 /* normal 64bit interface to access TLS data.
821 Works just like arch_prctl, except that the arguments
823 case PTRACE_ARCH_PRCTL
:
824 ret
= do_arch_prctl_64(child
, data
, addr
);
829 ret
= ptrace_request(child
, request
, addr
, data
);
836 #ifdef CONFIG_IA32_EMULATION
838 #include <linux/compat.h>
839 #include <linux/syscalls.h>
840 #include <asm/ia32.h>
841 #include <asm/user32.h>
844 case offsetof(struct user32, regs.l): \
845 regs->q = value; break
848 case offsetof(struct user32, regs.rs): \
849 return set_segment_reg(child, \
850 offsetof(struct user_regs_struct, rs), \
854 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
856 struct pt_regs
*regs
= task_pt_regs(child
);
877 case offsetof(struct user32
, regs
.orig_eax
):
879 * Warning: bizarre corner case fixup here. A 32-bit
880 * debugger setting orig_eax to -1 wants to disable
881 * syscall restart. Make sure that the syscall
882 * restart code sign-extends orig_ax. Also make sure
883 * we interpret the -ERESTART* codes correctly if
884 * loaded into regs->ax in case the task is not
885 * actually still sitting at the exit from a 32-bit
886 * syscall with TS_COMPAT still set.
888 regs
->orig_ax
= value
;
889 if (syscall_get_nr(child
, regs
) >= 0)
890 child
->thread_info
.status
|= TS_I386_REGS_POKED
;
893 case offsetof(struct user32
, regs
.eflags
):
894 return set_flags(child
, value
);
896 case offsetof(struct user32
, u_debugreg
[0]) ...
897 offsetof(struct user32
, u_debugreg
[7]):
898 regno
-= offsetof(struct user32
, u_debugreg
[0]);
899 return ptrace_set_debugreg(child
, regno
/ 4, value
);
902 if (regno
> sizeof(struct user32
) || (regno
& 3))
906 * Other dummy fields in the virtual user structure
918 case offsetof(struct user32, regs.l): \
919 *val = regs->q; break
922 case offsetof(struct user32, regs.rs): \
923 *val = get_segment_reg(child, \
924 offsetof(struct user_regs_struct, rs)); \
927 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
929 struct pt_regs
*regs
= task_pt_regs(child
);
947 R32(orig_eax
, orig_ax
);
951 case offsetof(struct user32
, regs
.eflags
):
952 *val
= get_flags(child
);
955 case offsetof(struct user32
, u_debugreg
[0]) ...
956 offsetof(struct user32
, u_debugreg
[7]):
957 regno
-= offsetof(struct user32
, u_debugreg
[0]);
958 *val
= ptrace_get_debugreg(child
, regno
/ 4);
962 if (regno
> sizeof(struct user32
) || (regno
& 3))
966 * Other dummy fields in the virtual user structure
978 static int genregs32_get(struct task_struct
*target
,
979 const struct user_regset
*regset
,
980 unsigned int pos
, unsigned int count
,
981 void *kbuf
, void __user
*ubuf
)
984 compat_ulong_t
*k
= kbuf
;
985 while (count
>= sizeof(*k
)) {
986 getreg32(target
, pos
, k
++);
991 compat_ulong_t __user
*u
= ubuf
;
992 while (count
>= sizeof(*u
)) {
994 getreg32(target
, pos
, &word
);
995 if (__put_user(word
, u
++))
1005 static int genregs32_set(struct task_struct
*target
,
1006 const struct user_regset
*regset
,
1007 unsigned int pos
, unsigned int count
,
1008 const void *kbuf
, const void __user
*ubuf
)
1012 const compat_ulong_t
*k
= kbuf
;
1013 while (count
>= sizeof(*k
) && !ret
) {
1014 ret
= putreg32(target
, pos
, *k
++);
1015 count
-= sizeof(*k
);
1019 const compat_ulong_t __user
*u
= ubuf
;
1020 while (count
>= sizeof(*u
) && !ret
) {
1021 compat_ulong_t word
;
1022 ret
= __get_user(word
, u
++);
1025 ret
= putreg32(target
, pos
, word
);
1026 count
-= sizeof(*u
);
1033 static long ia32_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1034 compat_ulong_t caddr
, compat_ulong_t cdata
)
1036 unsigned long addr
= caddr
;
1037 unsigned long data
= cdata
;
1038 void __user
*datap
= compat_ptr(data
);
1043 case PTRACE_PEEKUSR
:
1044 ret
= getreg32(child
, addr
, &val
);
1046 ret
= put_user(val
, (__u32 __user
*)datap
);
1049 case PTRACE_POKEUSR
:
1050 ret
= putreg32(child
, addr
, data
);
1053 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1054 return copy_regset_to_user(child
, &user_x86_32_view
,
1056 0, sizeof(struct user_regs_struct32
),
1059 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1060 return copy_regset_from_user(child
, &user_x86_32_view
,
1062 sizeof(struct user_regs_struct32
),
1065 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1066 return copy_regset_to_user(child
, &user_x86_32_view
,
1068 sizeof(struct user_i387_ia32_struct
),
1071 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1072 return copy_regset_from_user(
1073 child
, &user_x86_32_view
, REGSET_FP
,
1074 0, sizeof(struct user_i387_ia32_struct
), datap
);
1076 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1077 return copy_regset_to_user(child
, &user_x86_32_view
,
1079 sizeof(struct user32_fxsr_struct
),
1082 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1083 return copy_regset_from_user(child
, &user_x86_32_view
,
1085 sizeof(struct user32_fxsr_struct
),
1088 case PTRACE_GET_THREAD_AREA
:
1089 case PTRACE_SET_THREAD_AREA
:
1090 return arch_ptrace(child
, request
, addr
, data
);
1093 return compat_ptrace_request(child
, request
, addr
, data
);
1098 #endif /* CONFIG_IA32_EMULATION */
1100 #ifdef CONFIG_X86_X32_ABI
1101 static long x32_arch_ptrace(struct task_struct
*child
,
1102 compat_long_t request
, compat_ulong_t caddr
,
1103 compat_ulong_t cdata
)
1105 unsigned long addr
= caddr
;
1106 unsigned long data
= cdata
;
1107 void __user
*datap
= compat_ptr(data
);
1111 /* Read 32bits at location addr in the USER area. Only allow
1112 to return the lower 32bits of segment and debug registers. */
1113 case PTRACE_PEEKUSR
: {
1117 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1118 addr
< offsetof(struct user_regs_struct
, cs
))
1121 tmp
= 0; /* Default return condition */
1122 if (addr
< sizeof(struct user_regs_struct
))
1123 tmp
= getreg(child
, addr
);
1124 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1125 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1126 addr
-= offsetof(struct user
, u_debugreg
[0]);
1127 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1129 ret
= put_user(tmp
, (__u32 __user
*)datap
);
1133 /* Write the word at location addr in the USER area. Only allow
1134 to update segment and debug registers with the upper 32bits
1136 case PTRACE_POKEUSR
:
1138 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1139 addr
< offsetof(struct user_regs_struct
, cs
))
1142 if (addr
< sizeof(struct user_regs_struct
))
1143 ret
= putreg(child
, addr
, data
);
1144 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1145 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1146 addr
-= offsetof(struct user
, u_debugreg
[0]);
1147 ret
= ptrace_set_debugreg(child
,
1148 addr
/ sizeof(data
), data
);
1152 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1153 return copy_regset_to_user(child
,
1154 task_user_regset_view(current
),
1156 0, sizeof(struct user_regs_struct
),
1159 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1160 return copy_regset_from_user(child
,
1161 task_user_regset_view(current
),
1163 0, sizeof(struct user_regs_struct
),
1166 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1167 return copy_regset_to_user(child
,
1168 task_user_regset_view(current
),
1170 0, sizeof(struct user_i387_struct
),
1173 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1174 return copy_regset_from_user(child
,
1175 task_user_regset_view(current
),
1177 0, sizeof(struct user_i387_struct
),
1181 return compat_ptrace_request(child
, request
, addr
, data
);
1188 #ifdef CONFIG_COMPAT
1189 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1190 compat_ulong_t caddr
, compat_ulong_t cdata
)
1192 #ifdef CONFIG_X86_X32_ABI
1193 if (!in_ia32_syscall())
1194 return x32_arch_ptrace(child
, request
, caddr
, cdata
);
1196 #ifdef CONFIG_IA32_EMULATION
1197 return ia32_arch_ptrace(child
, request
, caddr
, cdata
);
1202 #endif /* CONFIG_COMPAT */
1204 #ifdef CONFIG_X86_64
1206 static struct user_regset x86_64_regsets
[] __ro_after_init
= {
1207 [REGSET_GENERAL
] = {
1208 .core_note_type
= NT_PRSTATUS
,
1209 .n
= sizeof(struct user_regs_struct
) / sizeof(long),
1210 .size
= sizeof(long), .align
= sizeof(long),
1211 .get
= genregs_get
, .set
= genregs_set
1214 .core_note_type
= NT_PRFPREG
,
1215 .n
= sizeof(struct user_i387_struct
) / sizeof(long),
1216 .size
= sizeof(long), .align
= sizeof(long),
1217 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1220 .core_note_type
= NT_X86_XSTATE
,
1221 .size
= sizeof(u64
), .align
= sizeof(u64
),
1222 .active
= xstateregs_active
, .get
= xstateregs_get
,
1223 .set
= xstateregs_set
1225 [REGSET_IOPERM64
] = {
1226 .core_note_type
= NT_386_IOPERM
,
1227 .n
= IO_BITMAP_LONGS
,
1228 .size
= sizeof(long), .align
= sizeof(long),
1229 .active
= ioperm_active
, .get
= ioperm_get
1233 static const struct user_regset_view user_x86_64_view
= {
1234 .name
= "x86_64", .e_machine
= EM_X86_64
,
1235 .regsets
= x86_64_regsets
, .n
= ARRAY_SIZE(x86_64_regsets
)
1238 #else /* CONFIG_X86_32 */
1240 #define user_regs_struct32 user_regs_struct
1241 #define genregs32_get genregs_get
1242 #define genregs32_set genregs_set
1244 #endif /* CONFIG_X86_64 */
1246 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1247 static struct user_regset x86_32_regsets
[] __ro_after_init
= {
1248 [REGSET_GENERAL
] = {
1249 .core_note_type
= NT_PRSTATUS
,
1250 .n
= sizeof(struct user_regs_struct32
) / sizeof(u32
),
1251 .size
= sizeof(u32
), .align
= sizeof(u32
),
1252 .get
= genregs32_get
, .set
= genregs32_set
1255 .core_note_type
= NT_PRFPREG
,
1256 .n
= sizeof(struct user_i387_ia32_struct
) / sizeof(u32
),
1257 .size
= sizeof(u32
), .align
= sizeof(u32
),
1258 .active
= regset_fpregs_active
, .get
= fpregs_get
, .set
= fpregs_set
1261 .core_note_type
= NT_PRXFPREG
,
1262 .n
= sizeof(struct user32_fxsr_struct
) / sizeof(u32
),
1263 .size
= sizeof(u32
), .align
= sizeof(u32
),
1264 .active
= regset_xregset_fpregs_active
, .get
= xfpregs_get
, .set
= xfpregs_set
1267 .core_note_type
= NT_X86_XSTATE
,
1268 .size
= sizeof(u64
), .align
= sizeof(u64
),
1269 .active
= xstateregs_active
, .get
= xstateregs_get
,
1270 .set
= xstateregs_set
1273 .core_note_type
= NT_386_TLS
,
1274 .n
= GDT_ENTRY_TLS_ENTRIES
, .bias
= GDT_ENTRY_TLS_MIN
,
1275 .size
= sizeof(struct user_desc
),
1276 .align
= sizeof(struct user_desc
),
1277 .active
= regset_tls_active
,
1278 .get
= regset_tls_get
, .set
= regset_tls_set
1280 [REGSET_IOPERM32
] = {
1281 .core_note_type
= NT_386_IOPERM
,
1282 .n
= IO_BITMAP_BYTES
/ sizeof(u32
),
1283 .size
= sizeof(u32
), .align
= sizeof(u32
),
1284 .active
= ioperm_active
, .get
= ioperm_get
1288 static const struct user_regset_view user_x86_32_view
= {
1289 .name
= "i386", .e_machine
= EM_386
,
1290 .regsets
= x86_32_regsets
, .n
= ARRAY_SIZE(x86_32_regsets
)
1295 * This represents bytes 464..511 in the memory layout exported through
1296 * the REGSET_XSTATE interface.
1298 u64 xstate_fx_sw_bytes
[USER_XSTATE_FX_SW_WORDS
];
1300 void __init
update_regset_xstate_info(unsigned int size
, u64 xstate_mask
)
1302 #ifdef CONFIG_X86_64
1303 x86_64_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1305 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1306 x86_32_regsets
[REGSET_XSTATE
].n
= size
/ sizeof(u64
);
1308 xstate_fx_sw_bytes
[USER_XSTATE_XCR0_WORD
] = xstate_mask
;
1311 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
1313 #ifdef CONFIG_IA32_EMULATION
1314 if (!user_64bit_mode(task_pt_regs(task
)))
1316 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1317 return &user_x86_32_view
;
1319 #ifdef CONFIG_X86_64
1320 return &user_x86_64_view
;
1324 void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
1325 int error_code
, int si_code
)
1327 tsk
->thread
.trap_nr
= X86_TRAP_DB
;
1328 tsk
->thread
.error_code
= error_code
;
1330 /* Send us the fake SIGTRAP */
1331 force_sig_fault(SIGTRAP
, si_code
,
1332 user_mode(regs
) ? (void __user
*)regs
->ip
: NULL
, tsk
);
1335 void user_single_step_report(struct pt_regs
*regs
)
1337 send_sigtrap(current
, regs
, 0, TRAP_BRKPT
);