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>
9 #include <linux/sched/task_stack.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/ptrace.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>
25 #include <linux/export.h>
26 #include <linux/context_tracking.h>
27 #include <linux/nospec.h>
29 #include <linux/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/processor.h>
32 #include <asm/fpu/internal.h>
33 #include <asm/fpu/signal.h>
34 #include <asm/fpu/regset.h>
35 #include <asm/debugreg.h>
38 #include <asm/prctl.h>
39 #include <asm/proto.h>
40 #include <asm/hw_breakpoint.h>
41 #include <asm/traps.h>
42 #include <asm/syscall.h>
43 #include <asm/mmu_context.h>
51 REGSET_IOPERM64
= REGSET_XFP
,
57 struct pt_regs_offset
{
62 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
63 #define REG_OFFSET_END {.name = NULL, .offset = 0}
65 static const struct pt_regs_offset regoffset_table
[] = {
89 REG_OFFSET_NAME(orig_ax
),
92 REG_OFFSET_NAME(flags
),
99 * regs_query_register_offset() - query register offset from its name
100 * @name: the name of a register
102 * regs_query_register_offset() returns the offset of a register in struct
103 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
105 int regs_query_register_offset(const char *name
)
107 const struct pt_regs_offset
*roff
;
108 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
109 if (!strcmp(roff
->name
, name
))
115 * regs_query_register_name() - query register name from its offset
116 * @offset: the offset of a register in struct pt_regs.
118 * regs_query_register_name() returns the name of a register from its
119 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
121 const char *regs_query_register_name(unsigned int offset
)
123 const struct pt_regs_offset
*roff
;
124 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
125 if (roff
->offset
== offset
)
131 * does not yet catch signals sent when the child dies.
132 * in exit.c or in signal.c.
136 * Determines which flags the user has access to [1 = access, 0 = no access].
138 #define FLAG_MASK_32 ((unsigned long) \
139 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
140 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
141 X86_EFLAGS_SF | X86_EFLAGS_TF | \
142 X86_EFLAGS_DF | X86_EFLAGS_OF | \
143 X86_EFLAGS_RF | X86_EFLAGS_AC))
146 * Determines whether a value may be installed in a segment register.
148 static inline bool invalid_selector(u16 value
)
150 return unlikely(value
!= 0 && (value
& SEGMENT_RPL_MASK
) != USER_RPL
);
155 #define FLAG_MASK FLAG_MASK_32
158 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
159 * when it traps. The previous stack will be directly underneath the saved
160 * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'.
162 * Now, if the stack is empty, '®s->sp' is out of range. In this
163 * case we try to take the previous stack. To always return a non-null
164 * stack pointer we fall back to regs as stack if no previous stack
167 * This is valid only for kernel mode traps.
169 unsigned long kernel_stack_pointer(struct pt_regs
*regs
)
171 unsigned long context
= (unsigned long)regs
& ~(THREAD_SIZE
- 1);
172 unsigned long sp
= (unsigned long)®s
->sp
;
175 if (context
== (sp
& ~(THREAD_SIZE
- 1)))
178 prev_esp
= (u32
*)(context
);
180 return (unsigned long)*prev_esp
;
182 return (unsigned long)regs
;
184 EXPORT_SYMBOL_GPL(kernel_stack_pointer
);
186 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long regno
)
188 BUILD_BUG_ON(offsetof(struct pt_regs
, bx
) != 0);
189 return ®s
->bx
+ (regno
>> 2);
192 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
195 * Returning the value truncates it to 16 bits.
198 if (offset
!= offsetof(struct user_regs_struct
, gs
))
199 retval
= *pt_regs_access(task_pt_regs(task
), offset
);
202 retval
= get_user_gs(task_pt_regs(task
));
204 retval
= task_user_gs(task
);
209 static int set_segment_reg(struct task_struct
*task
,
210 unsigned long offset
, u16 value
)
213 * The value argument was already truncated to 16 bits.
215 if (invalid_selector(value
))
219 * For %cs and %ss we cannot permit a null selector.
220 * We can permit a bogus selector as long as it has USER_RPL.
221 * Null selectors are fine for other segment registers, but
222 * we will never get back to user mode with invalid %cs or %ss
223 * and will take the trap in iret instead. Much code relies
224 * on user_mode() to distinguish a user trap frame (which can
225 * safely use invalid selectors) from a kernel trap frame.
228 case offsetof(struct user_regs_struct
, cs
):
229 case offsetof(struct user_regs_struct
, ss
):
230 if (unlikely(value
== 0))
234 *pt_regs_access(task_pt_regs(task
), offset
) = value
;
237 case offsetof(struct user_regs_struct
, gs
):
239 set_user_gs(task_pt_regs(task
), value
);
241 task_user_gs(task
) = value
;
247 #else /* CONFIG_X86_64 */
249 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
251 static unsigned long *pt_regs_access(struct pt_regs
*regs
, unsigned long offset
)
253 BUILD_BUG_ON(offsetof(struct pt_regs
, r15
) != 0);
254 return ®s
->r15
+ (offset
/ sizeof(regs
->r15
));
257 static u16
get_segment_reg(struct task_struct
*task
, unsigned long offset
)
260 * Returning the value truncates it to 16 bits.
265 case offsetof(struct user_regs_struct
, fs
):
266 if (task
== current
) {
267 /* Older gas can't assemble movq %?s,%r?? */
268 asm("movl %%fs,%0" : "=r" (seg
));
271 return task
->thread
.fsindex
;
272 case offsetof(struct user_regs_struct
, gs
):
273 if (task
== current
) {
274 asm("movl %%gs,%0" : "=r" (seg
));
277 return task
->thread
.gsindex
;
278 case offsetof(struct user_regs_struct
, ds
):
279 if (task
== current
) {
280 asm("movl %%ds,%0" : "=r" (seg
));
283 return task
->thread
.ds
;
284 case offsetof(struct user_regs_struct
, es
):
285 if (task
== current
) {
286 asm("movl %%es,%0" : "=r" (seg
));
289 return task
->thread
.es
;
291 case offsetof(struct user_regs_struct
, cs
):
292 case offsetof(struct user_regs_struct
, ss
):
295 return *pt_regs_access(task_pt_regs(task
), offset
);
298 static int set_segment_reg(struct task_struct
*task
,
299 unsigned long offset
, u16 value
)
302 * The value argument was already truncated to 16 bits.
304 if (invalid_selector(value
))
308 case offsetof(struct user_regs_struct
,fs
):
309 task
->thread
.fsindex
= value
;
311 loadsegment(fs
, task
->thread
.fsindex
);
313 case offsetof(struct user_regs_struct
,gs
):
314 task
->thread
.gsindex
= value
;
316 load_gs_index(task
->thread
.gsindex
);
318 case offsetof(struct user_regs_struct
,ds
):
319 task
->thread
.ds
= value
;
321 loadsegment(ds
, task
->thread
.ds
);
323 case offsetof(struct user_regs_struct
,es
):
324 task
->thread
.es
= value
;
326 loadsegment(es
, task
->thread
.es
);
330 * Can't actually change these in 64-bit mode.
332 case offsetof(struct user_regs_struct
,cs
):
333 if (unlikely(value
== 0))
335 task_pt_regs(task
)->cs
= value
;
337 case offsetof(struct user_regs_struct
,ss
):
338 if (unlikely(value
== 0))
340 task_pt_regs(task
)->ss
= value
;
347 static unsigned long task_seg_base(struct task_struct
*task
,
348 unsigned short selector
)
350 unsigned short idx
= selector
>> 3;
353 if (likely((selector
& SEGMENT_TI_MASK
) == 0)) {
354 if (unlikely(idx
>= GDT_ENTRIES
))
358 * There are no user segments in the GDT with nonzero bases
359 * other than the TLS segments.
361 if (idx
< GDT_ENTRY_TLS_MIN
|| idx
> GDT_ENTRY_TLS_MAX
)
364 idx
-= GDT_ENTRY_TLS_MIN
;
365 base
= get_desc_base(&task
->thread
.tls_array
[idx
]);
367 #ifdef CONFIG_MODIFY_LDT_SYSCALL
368 struct ldt_struct
*ldt
;
371 * If performance here mattered, we could protect the LDT
372 * with RCU. This is a slow path, though, so we can just
375 mutex_lock(&task
->mm
->context
.lock
);
376 ldt
= task
->mm
->context
.ldt
;
377 if (unlikely(!ldt
|| idx
>= ldt
->nr_entries
))
380 base
= get_desc_base(ldt
->entries
+ idx
);
381 mutex_unlock(&task
->mm
->context
.lock
);
390 #endif /* CONFIG_X86_32 */
392 static unsigned long get_flags(struct task_struct
*task
)
394 unsigned long retval
= task_pt_regs(task
)->flags
;
397 * If the debugger set TF, hide it from the readout.
399 if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
400 retval
&= ~X86_EFLAGS_TF
;
405 static int set_flags(struct task_struct
*task
, unsigned long value
)
407 struct pt_regs
*regs
= task_pt_regs(task
);
410 * If the user value contains TF, mark that
411 * it was not "us" (the debugger) that set it.
412 * If not, make sure it stays set if we had.
414 if (value
& X86_EFLAGS_TF
)
415 clear_tsk_thread_flag(task
, TIF_FORCED_TF
);
416 else if (test_tsk_thread_flag(task
, TIF_FORCED_TF
))
417 value
|= X86_EFLAGS_TF
;
419 regs
->flags
= (regs
->flags
& ~FLAG_MASK
) | (value
& FLAG_MASK
);
424 static int putreg(struct task_struct
*child
,
425 unsigned long offset
, unsigned long value
)
428 case offsetof(struct user_regs_struct
, cs
):
429 case offsetof(struct user_regs_struct
, ds
):
430 case offsetof(struct user_regs_struct
, es
):
431 case offsetof(struct user_regs_struct
, fs
):
432 case offsetof(struct user_regs_struct
, gs
):
433 case offsetof(struct user_regs_struct
, ss
):
434 return set_segment_reg(child
, offset
, value
);
436 case offsetof(struct user_regs_struct
, flags
):
437 return set_flags(child
, value
);
440 case offsetof(struct user_regs_struct
,fs_base
):
441 if (value
>= TASK_SIZE_MAX
)
444 * When changing the segment base, use do_arch_prctl_64
445 * to set either thread.fs or thread.fsindex and the
446 * corresponding GDT slot.
448 if (child
->thread
.fsbase
!= value
)
449 return do_arch_prctl_64(child
, ARCH_SET_FS
, value
);
451 case offsetof(struct user_regs_struct
,gs_base
):
453 * Exactly the same here as the %fs handling above.
455 if (value
>= TASK_SIZE_MAX
)
457 if (child
->thread
.gsbase
!= value
)
458 return do_arch_prctl_64(child
, ARCH_SET_GS
, value
);
463 *pt_regs_access(task_pt_regs(child
), offset
) = value
;
467 static unsigned long getreg(struct task_struct
*task
, unsigned long offset
)
470 case offsetof(struct user_regs_struct
, cs
):
471 case offsetof(struct user_regs_struct
, ds
):
472 case offsetof(struct user_regs_struct
, es
):
473 case offsetof(struct user_regs_struct
, fs
):
474 case offsetof(struct user_regs_struct
, gs
):
475 case offsetof(struct user_regs_struct
, ss
):
476 return get_segment_reg(task
, offset
);
478 case offsetof(struct user_regs_struct
, flags
):
479 return get_flags(task
);
482 case offsetof(struct user_regs_struct
, fs_base
): {
483 if (task
->thread
.fsindex
== 0)
484 return task
->thread
.fsbase
;
486 return task_seg_base(task
, task
->thread
.fsindex
);
488 case offsetof(struct user_regs_struct
, gs_base
): {
489 if (task
->thread
.gsindex
== 0)
490 return task
->thread
.gsbase
;
492 return task_seg_base(task
, task
->thread
.gsindex
);
497 return *pt_regs_access(task_pt_regs(task
), offset
);
500 static int genregs_get(struct task_struct
*target
,
501 const struct user_regset
*regset
,
502 unsigned int pos
, unsigned int count
,
503 void *kbuf
, void __user
*ubuf
)
506 unsigned long *k
= kbuf
;
507 while (count
>= sizeof(*k
)) {
508 *k
++ = getreg(target
, pos
);
513 unsigned long __user
*u
= ubuf
;
514 while (count
>= sizeof(*u
)) {
515 if (__put_user(getreg(target
, pos
), u
++))
525 static int genregs_set(struct task_struct
*target
,
526 const struct user_regset
*regset
,
527 unsigned int pos
, unsigned int count
,
528 const void *kbuf
, const void __user
*ubuf
)
532 const unsigned long *k
= kbuf
;
533 while (count
>= sizeof(*k
) && !ret
) {
534 ret
= putreg(target
, pos
, *k
++);
539 const unsigned long __user
*u
= ubuf
;
540 while (count
>= sizeof(*u
) && !ret
) {
542 ret
= __get_user(word
, u
++);
545 ret
= putreg(target
, pos
, word
);
553 static void ptrace_triggered(struct perf_event
*bp
,
554 struct perf_sample_data
*data
,
555 struct pt_regs
*regs
)
558 struct thread_struct
*thread
= &(current
->thread
);
561 * Store in the virtual DR6 register the fact that the breakpoint
562 * was hit so the thread's debugger will see it.
564 for (i
= 0; i
< HBP_NUM
; i
++) {
565 if (thread
->ptrace_bps
[i
] == bp
)
569 thread
->debugreg6
|= (DR_TRAP0
<< i
);
573 * Walk through every ptrace breakpoints for this thread and
574 * build the dr7 value on top of their attributes.
577 static unsigned long ptrace_get_dr7(struct perf_event
*bp
[])
581 struct arch_hw_breakpoint
*info
;
583 for (i
= 0; i
< HBP_NUM
; i
++) {
584 if (bp
[i
] && !bp
[i
]->attr
.disabled
) {
585 info
= counter_arch_bp(bp
[i
]);
586 dr7
|= encode_dr7(i
, info
->len
, info
->type
);
593 static int ptrace_fill_bp_fields(struct perf_event_attr
*attr
,
594 int len
, int type
, bool disabled
)
596 int err
, bp_len
, bp_type
;
598 err
= arch_bp_generic_fields(len
, type
, &bp_len
, &bp_type
);
600 attr
->bp_len
= bp_len
;
601 attr
->bp_type
= bp_type
;
602 attr
->disabled
= disabled
;
608 static struct perf_event
*
609 ptrace_register_breakpoint(struct task_struct
*tsk
, int len
, int type
,
610 unsigned long addr
, bool disabled
)
612 struct perf_event_attr attr
;
615 ptrace_breakpoint_init(&attr
);
618 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
622 return register_user_hw_breakpoint(&attr
, ptrace_triggered
,
626 static int ptrace_modify_breakpoint(struct perf_event
*bp
, int len
, int type
,
629 struct perf_event_attr attr
= bp
->attr
;
632 err
= ptrace_fill_bp_fields(&attr
, len
, type
, disabled
);
636 return modify_user_hw_breakpoint(bp
, &attr
);
640 * Handle ptrace writes to debug register 7.
642 static int ptrace_write_dr7(struct task_struct
*tsk
, unsigned long data
)
644 struct thread_struct
*thread
= &tsk
->thread
;
645 unsigned long old_dr7
;
646 bool second_pass
= false;
649 data
&= ~DR_CONTROL_RESERVED
;
650 old_dr7
= ptrace_get_dr7(thread
->ptrace_bps
);
654 for (i
= 0; i
< HBP_NUM
; i
++) {
656 bool disabled
= !decode_dr7(data
, i
, &len
, &type
);
657 struct perf_event
*bp
= thread
->ptrace_bps
[i
];
663 bp
= ptrace_register_breakpoint(tsk
,
664 len
, type
, 0, disabled
);
670 thread
->ptrace_bps
[i
] = bp
;
674 rc
= ptrace_modify_breakpoint(bp
, len
, type
, disabled
);
679 /* Restore if the first pass failed, second_pass shouldn't fail. */
680 if (rc
&& !WARN_ON(second_pass
)) {
691 * Handle PTRACE_PEEKUSR calls for the debug register area.
693 static unsigned long ptrace_get_debugreg(struct task_struct
*tsk
, int n
)
695 struct thread_struct
*thread
= &tsk
->thread
;
696 unsigned long val
= 0;
699 int index
= array_index_nospec(n
, HBP_NUM
);
700 struct perf_event
*bp
= thread
->ptrace_bps
[index
];
703 val
= bp
->hw
.info
.address
;
705 val
= thread
->debugreg6
;
707 val
= thread
->ptrace_dr7
;
712 static int ptrace_set_breakpoint_addr(struct task_struct
*tsk
, int nr
,
715 struct thread_struct
*t
= &tsk
->thread
;
716 struct perf_event
*bp
= t
->ptrace_bps
[nr
];
721 * Put stub len and type to create an inactive but correct bp.
723 * CHECKME: the previous code returned -EIO if the addr wasn't
724 * a valid task virtual addr. The new one will return -EINVAL in
726 * -EINVAL may be what we want for in-kernel breakpoints users,
727 * but -EIO looks better for ptrace, since we refuse a register
728 * writing for the user. And anyway this is the previous
731 bp
= ptrace_register_breakpoint(tsk
,
732 X86_BREAKPOINT_LEN_1
, X86_BREAKPOINT_WRITE
,
737 t
->ptrace_bps
[nr
] = bp
;
739 struct perf_event_attr attr
= bp
->attr
;
742 err
= modify_user_hw_breakpoint(bp
, &attr
);
749 * Handle PTRACE_POKEUSR calls for the debug register area.
751 static int ptrace_set_debugreg(struct task_struct
*tsk
, int n
,
754 struct thread_struct
*thread
= &tsk
->thread
;
755 /* There are no DR4 or DR5 registers */
759 rc
= ptrace_set_breakpoint_addr(tsk
, n
, val
);
761 thread
->debugreg6
= val
;
764 rc
= ptrace_write_dr7(tsk
, val
);
766 thread
->ptrace_dr7
= val
;
772 * These access the current or another (stopped) task's io permission
773 * bitmap for debugging or core dump.
775 static int ioperm_active(struct task_struct
*target
,
776 const struct user_regset
*regset
)
778 return target
->thread
.io_bitmap_max
/ regset
->size
;
781 static int ioperm_get(struct task_struct
*target
,
782 const struct user_regset
*regset
,
783 unsigned int pos
, unsigned int count
,
784 void *kbuf
, void __user
*ubuf
)
786 if (!target
->thread
.io_bitmap_ptr
)
789 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
790 target
->thread
.io_bitmap_ptr
,
795 * Called by kernel/ptrace.c when detaching..
797 * Make sure the single step bit is not set.
799 void ptrace_disable(struct task_struct
*child
)
801 user_disable_single_step(child
);
802 #ifdef TIF_SYSCALL_EMU
803 clear_tsk_thread_flag(child
, TIF_SYSCALL_EMU
);
807 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
808 static const struct user_regset_view user_x86_32_view
; /* Initialized below. */
811 long arch_ptrace(struct task_struct
*child
, long request
,
812 unsigned long addr
, unsigned long data
)
815 unsigned long __user
*datap
= (unsigned long __user
*)data
;
818 /* read the word at location addr in the USER area. */
819 case PTRACE_PEEKUSR
: {
823 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
826 tmp
= 0; /* Default return condition */
827 if (addr
< sizeof(struct user_regs_struct
))
828 tmp
= getreg(child
, addr
);
829 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
830 addr
<= offsetof(struct user
, u_debugreg
[7])) {
831 addr
-= offsetof(struct user
, u_debugreg
[0]);
832 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
834 ret
= put_user(tmp
, datap
);
838 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
840 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
))
843 if (addr
< sizeof(struct user_regs_struct
))
844 ret
= putreg(child
, addr
, data
);
845 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
846 addr
<= offsetof(struct user
, u_debugreg
[7])) {
847 addr
-= offsetof(struct user
, u_debugreg
[0]);
848 ret
= ptrace_set_debugreg(child
,
849 addr
/ sizeof(data
), data
);
853 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
854 return copy_regset_to_user(child
,
855 task_user_regset_view(current
),
857 0, sizeof(struct user_regs_struct
),
860 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
861 return copy_regset_from_user(child
,
862 task_user_regset_view(current
),
864 0, sizeof(struct user_regs_struct
),
867 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
868 return copy_regset_to_user(child
,
869 task_user_regset_view(current
),
871 0, sizeof(struct user_i387_struct
),
874 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
875 return copy_regset_from_user(child
,
876 task_user_regset_view(current
),
878 0, sizeof(struct user_i387_struct
),
882 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
883 return copy_regset_to_user(child
, &user_x86_32_view
,
885 0, sizeof(struct user_fxsr_struct
),
888 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
889 return copy_regset_from_user(child
, &user_x86_32_view
,
891 0, sizeof(struct user_fxsr_struct
),
895 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
896 case PTRACE_GET_THREAD_AREA
:
899 ret
= do_get_thread_area(child
, addr
,
900 (struct user_desc __user
*)data
);
903 case PTRACE_SET_THREAD_AREA
:
906 ret
= do_set_thread_area(child
, addr
,
907 (struct user_desc __user
*)data
, 0);
912 /* normal 64bit interface to access TLS data.
913 Works just like arch_prctl, except that the arguments
915 case PTRACE_ARCH_PRCTL
:
916 ret
= do_arch_prctl_64(child
, data
, addr
);
921 ret
= ptrace_request(child
, request
, addr
, data
);
928 #ifdef CONFIG_IA32_EMULATION
930 #include <linux/compat.h>
931 #include <linux/syscalls.h>
932 #include <asm/ia32.h>
933 #include <asm/user32.h>
936 case offsetof(struct user32, regs.l): \
937 regs->q = value; break
940 case offsetof(struct user32, regs.rs): \
941 return set_segment_reg(child, \
942 offsetof(struct user_regs_struct, rs), \
946 static int putreg32(struct task_struct
*child
, unsigned regno
, u32 value
)
948 struct pt_regs
*regs
= task_pt_regs(child
);
969 case offsetof(struct user32
, regs
.orig_eax
):
971 * Warning: bizarre corner case fixup here. A 32-bit
972 * debugger setting orig_eax to -1 wants to disable
973 * syscall restart. Make sure that the syscall
974 * restart code sign-extends orig_ax. Also make sure
975 * we interpret the -ERESTART* codes correctly if
976 * loaded into regs->ax in case the task is not
977 * actually still sitting at the exit from a 32-bit
978 * syscall with TS_COMPAT still set.
980 regs
->orig_ax
= value
;
981 if (syscall_get_nr(child
, regs
) >= 0)
982 child
->thread_info
.status
|= TS_I386_REGS_POKED
;
985 case offsetof(struct user32
, regs
.eflags
):
986 return set_flags(child
, value
);
988 case offsetof(struct user32
, u_debugreg
[0]) ...
989 offsetof(struct user32
, u_debugreg
[7]):
990 regno
-= offsetof(struct user32
, u_debugreg
[0]);
991 return ptrace_set_debugreg(child
, regno
/ 4, value
);
994 if (regno
> sizeof(struct user32
) || (regno
& 3))
998 * Other dummy fields in the virtual user structure
1010 case offsetof(struct user32, regs.l): \
1011 *val = regs->q; break
1014 case offsetof(struct user32, regs.rs): \
1015 *val = get_segment_reg(child, \
1016 offsetof(struct user_regs_struct, rs)); \
1019 static int getreg32(struct task_struct
*child
, unsigned regno
, u32
*val
)
1021 struct pt_regs
*regs
= task_pt_regs(child
);
1039 R32(orig_eax
, orig_ax
);
1043 case offsetof(struct user32
, regs
.eflags
):
1044 *val
= get_flags(child
);
1047 case offsetof(struct user32
, u_debugreg
[0]) ...
1048 offsetof(struct user32
, u_debugreg
[7]):
1049 regno
-= offsetof(struct user32
, u_debugreg
[0]);
1050 *val
= ptrace_get_debugreg(child
, regno
/ 4);
1054 if (regno
> sizeof(struct user32
) || (regno
& 3))
1058 * Other dummy fields in the virtual user structure
1070 static int genregs32_get(struct task_struct
*target
,
1071 const struct user_regset
*regset
,
1072 unsigned int pos
, unsigned int count
,
1073 void *kbuf
, void __user
*ubuf
)
1076 compat_ulong_t
*k
= kbuf
;
1077 while (count
>= sizeof(*k
)) {
1078 getreg32(target
, pos
, k
++);
1079 count
-= sizeof(*k
);
1083 compat_ulong_t __user
*u
= ubuf
;
1084 while (count
>= sizeof(*u
)) {
1085 compat_ulong_t word
;
1086 getreg32(target
, pos
, &word
);
1087 if (__put_user(word
, u
++))
1089 count
-= sizeof(*u
);
1097 static int genregs32_set(struct task_struct
*target
,
1098 const struct user_regset
*regset
,
1099 unsigned int pos
, unsigned int count
,
1100 const void *kbuf
, const void __user
*ubuf
)
1104 const compat_ulong_t
*k
= kbuf
;
1105 while (count
>= sizeof(*k
) && !ret
) {
1106 ret
= putreg32(target
, pos
, *k
++);
1107 count
-= sizeof(*k
);
1111 const compat_ulong_t __user
*u
= ubuf
;
1112 while (count
>= sizeof(*u
) && !ret
) {
1113 compat_ulong_t word
;
1114 ret
= __get_user(word
, u
++);
1117 ret
= putreg32(target
, pos
, word
);
1118 count
-= sizeof(*u
);
1125 static long ia32_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1126 compat_ulong_t caddr
, compat_ulong_t cdata
)
1128 unsigned long addr
= caddr
;
1129 unsigned long data
= cdata
;
1130 void __user
*datap
= compat_ptr(data
);
1135 case PTRACE_PEEKUSR
:
1136 ret
= getreg32(child
, addr
, &val
);
1138 ret
= put_user(val
, (__u32 __user
*)datap
);
1141 case PTRACE_POKEUSR
:
1142 ret
= putreg32(child
, addr
, data
);
1145 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1146 return copy_regset_to_user(child
, &user_x86_32_view
,
1148 0, sizeof(struct user_regs_struct32
),
1151 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1152 return copy_regset_from_user(child
, &user_x86_32_view
,
1154 sizeof(struct user_regs_struct32
),
1157 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1158 return copy_regset_to_user(child
, &user_x86_32_view
,
1160 sizeof(struct user_i387_ia32_struct
),
1163 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1164 return copy_regset_from_user(
1165 child
, &user_x86_32_view
, REGSET_FP
,
1166 0, sizeof(struct user_i387_ia32_struct
), datap
);
1168 case PTRACE_GETFPXREGS
: /* Get the child extended FPU state. */
1169 return copy_regset_to_user(child
, &user_x86_32_view
,
1171 sizeof(struct user32_fxsr_struct
),
1174 case PTRACE_SETFPXREGS
: /* Set the child extended FPU state. */
1175 return copy_regset_from_user(child
, &user_x86_32_view
,
1177 sizeof(struct user32_fxsr_struct
),
1180 case PTRACE_GET_THREAD_AREA
:
1181 case PTRACE_SET_THREAD_AREA
:
1182 return arch_ptrace(child
, request
, addr
, data
);
1185 return compat_ptrace_request(child
, request
, addr
, data
);
1190 #endif /* CONFIG_IA32_EMULATION */
1192 #ifdef CONFIG_X86_X32_ABI
1193 static long x32_arch_ptrace(struct task_struct
*child
,
1194 compat_long_t request
, compat_ulong_t caddr
,
1195 compat_ulong_t cdata
)
1197 unsigned long addr
= caddr
;
1198 unsigned long data
= cdata
;
1199 void __user
*datap
= compat_ptr(data
);
1203 /* Read 32bits at location addr in the USER area. Only allow
1204 to return the lower 32bits of segment and debug registers. */
1205 case PTRACE_PEEKUSR
: {
1209 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1210 addr
< offsetof(struct user_regs_struct
, cs
))
1213 tmp
= 0; /* Default return condition */
1214 if (addr
< sizeof(struct user_regs_struct
))
1215 tmp
= getreg(child
, addr
);
1216 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1217 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1218 addr
-= offsetof(struct user
, u_debugreg
[0]);
1219 tmp
= ptrace_get_debugreg(child
, addr
/ sizeof(data
));
1221 ret
= put_user(tmp
, (__u32 __user
*)datap
);
1225 /* Write the word at location addr in the USER area. Only allow
1226 to update segment and debug registers with the upper 32bits
1228 case PTRACE_POKEUSR
:
1230 if ((addr
& (sizeof(data
) - 1)) || addr
>= sizeof(struct user
) ||
1231 addr
< offsetof(struct user_regs_struct
, cs
))
1234 if (addr
< sizeof(struct user_regs_struct
))
1235 ret
= putreg(child
, addr
, data
);
1236 else if (addr
>= offsetof(struct user
, u_debugreg
[0]) &&
1237 addr
<= offsetof(struct user
, u_debugreg
[7])) {
1238 addr
-= offsetof(struct user
, u_debugreg
[0]);
1239 ret
= ptrace_set_debugreg(child
,
1240 addr
/ sizeof(data
), data
);
1244 case PTRACE_GETREGS
: /* Get all gp regs from the child. */
1245 return copy_regset_to_user(child
,
1246 task_user_regset_view(current
),
1248 0, sizeof(struct user_regs_struct
),
1251 case PTRACE_SETREGS
: /* Set all gp regs in the child. */
1252 return copy_regset_from_user(child
,
1253 task_user_regset_view(current
),
1255 0, sizeof(struct user_regs_struct
),
1258 case PTRACE_GETFPREGS
: /* Get the child FPU state. */
1259 return copy_regset_to_user(child
,
1260 task_user_regset_view(current
),
1262 0, sizeof(struct user_i387_struct
),
1265 case PTRACE_SETFPREGS
: /* Set the child FPU state. */
1266 return copy_regset_from_user(child
,
1267 task_user_regset_view(current
),
1269 0, sizeof(struct user_i387_struct
),
1273 return compat_ptrace_request(child
, request
, addr
, data
);
1280 #ifdef CONFIG_COMPAT
1281 long compat_arch_ptrace(struct task_struct
*child
, compat_long_t request
,
1282 compat_ulong_t caddr
, compat_ulong_t cdata
)
1284 #ifdef CONFIG_X86_X32_ABI
1285 if (!in_ia32_syscall())
1286 return x32_arch_ptrace(child
, request
, caddr
, cdata
);
1288 #ifdef CONFIG_IA32_EMULATION
1289 return ia32_arch_ptrace(child
, request
, caddr
, cdata
);
1294 #endif /* CONFIG_COMPAT */
1296 #ifdef CONFIG_X86_64
1298 static struct user_regset x86_64_regsets
[] __ro_after_init
= {
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
= regset_xregset_fpregs_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
[] __ro_after_init
= {
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
= regset_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
= regset_xregset_fpregs_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 __init
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 (!user_64bit_mode(task_pt_regs(task
)))
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(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
);