1 // SPDX-License-Identifier: GPL-2.0-only
2 /* By Ross Biro 1/23/92 */
4 * Pentium III FXSR, SSE support
5 * Gareth Hughes <gareth@valinux.com>, May 2000
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task_stack.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/ptrace.h>
16 #include <linux/tracehook.h>
17 #include <linux/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
23 #include <linux/perf_event.h>
24 #include <linux/hw_breakpoint.h>
25 #include <linux/rcupdate.h>
26 #include <linux/export.h>
27 #include <linux/context_tracking.h>
28 #include <linux/nospec.h>
30 #include <linux/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/processor.h>
33 #include <asm/fpu/internal.h>
34 #include <asm/fpu/signal.h>
35 #include <asm/fpu/regset.h>
36 #include <asm/debugreg.h>
39 #include <asm/prctl.h>
40 #include <asm/proto.h>
41 #include <asm/hw_breakpoint.h>
42 #include <asm/traps.h>
43 #include <asm/syscall.h>
44 #include <asm/fsgsbase.h>
45 #include <asm/io_bitmap.h>
53 REGSET_IOPERM64 = REGSET_XFP,
59 struct pt_regs_offset {
64 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
65 #define REG_OFFSET_END {.name = NULL, .offset = 0}
67 static const struct pt_regs_offset regoffset_table[] = {
91 REG_OFFSET_NAME(orig_ax),
94 REG_OFFSET_NAME(flags),
101 * regs_query_register_offset() - query register offset from its name
102 * @name: the name of a register
104 * regs_query_register_offset() returns the offset of a register in struct
105 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
107 int regs_query_register_offset(const char *name)
109 const struct pt_regs_offset *roff;
110 for (roff = regoffset_table; roff->name != NULL; roff++)
111 if (!strcmp(roff->name, name))
117 * regs_query_register_name() - query register name from its offset
118 * @offset: the offset of a register in struct pt_regs.
120 * regs_query_register_name() returns the name of a register from its
121 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
123 const char *regs_query_register_name(unsigned int offset)
125 const struct pt_regs_offset *roff;
126 for (roff = regoffset_table; roff->name != NULL; roff++)
127 if (roff->offset == offset)
133 * does not yet catch signals sent when the child dies.
134 * in exit.c or in signal.c.
138 * Determines which flags the user has access to [1 = access, 0 = no access].
140 #define FLAG_MASK_32 ((unsigned long) \
141 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
142 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
143 X86_EFLAGS_SF | X86_EFLAGS_TF | \
144 X86_EFLAGS_DF | X86_EFLAGS_OF | \
145 X86_EFLAGS_RF | X86_EFLAGS_AC))
148 * Determines whether a value may be installed in a segment register.
150 static inline bool invalid_selector(u16 value)
152 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
157 #define FLAG_MASK FLAG_MASK_32
159 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
161 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
162 return ®s->bx + (regno >> 2);
165 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
168 * Returning the value truncates it to 16 bits.
171 if (offset != offsetof(struct user_regs_struct, gs))
172 retval = *pt_regs_access(task_pt_regs(task), offset);
175 retval = get_user_gs(task_pt_regs(task));
177 retval = task_user_gs(task);
182 static int set_segment_reg(struct task_struct *task,
183 unsigned long offset, u16 value)
186 * The value argument was already truncated to 16 bits.
188 if (invalid_selector(value))
192 * For %cs and %ss we cannot permit a null selector.
193 * We can permit a bogus selector as long as it has USER_RPL.
194 * Null selectors are fine for other segment registers, but
195 * we will never get back to user mode with invalid %cs or %ss
196 * and will take the trap in iret instead. Much code relies
197 * on user_mode() to distinguish a user trap frame (which can
198 * safely use invalid selectors) from a kernel trap frame.
201 case offsetof(struct user_regs_struct, cs):
202 case offsetof(struct user_regs_struct, ss):
203 if (unlikely(value == 0))
205 /* Else, fall through */
208 *pt_regs_access(task_pt_regs(task), offset) = value;
211 case offsetof(struct user_regs_struct, gs):
213 set_user_gs(task_pt_regs(task), value);
215 task_user_gs(task) = value;
221 #else /* CONFIG_X86_64 */
223 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
225 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
227 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
228 return ®s->r15 + (offset / sizeof(regs->r15));
231 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
234 * Returning the value truncates it to 16 bits.
239 case offsetof(struct user_regs_struct, fs):
240 if (task == current) {
241 /* Older gas can't assemble movq %?s,%r?? */
242 asm("movl %%fs,%0" : "=r" (seg));
245 return task->thread.fsindex;
246 case offsetof(struct user_regs_struct, gs):
247 if (task == current) {
248 asm("movl %%gs,%0" : "=r" (seg));
251 return task->thread.gsindex;
252 case offsetof(struct user_regs_struct, ds):
253 if (task == current) {
254 asm("movl %%ds,%0" : "=r" (seg));
257 return task->thread.ds;
258 case offsetof(struct user_regs_struct, es):
259 if (task == current) {
260 asm("movl %%es,%0" : "=r" (seg));
263 return task->thread.es;
265 case offsetof(struct user_regs_struct, cs):
266 case offsetof(struct user_regs_struct, ss):
269 return *pt_regs_access(task_pt_regs(task), offset);
272 static int set_segment_reg(struct task_struct *task,
273 unsigned long offset, u16 value)
276 * The value argument was already truncated to 16 bits.
278 if (invalid_selector(value))
282 case offsetof(struct user_regs_struct,fs):
283 task->thread.fsindex = value;
285 loadsegment(fs, task->thread.fsindex);
287 case offsetof(struct user_regs_struct,gs):
288 task->thread.gsindex = value;
290 load_gs_index(task->thread.gsindex);
292 case offsetof(struct user_regs_struct,ds):
293 task->thread.ds = value;
295 loadsegment(ds, task->thread.ds);
297 case offsetof(struct user_regs_struct,es):
298 task->thread.es = value;
300 loadsegment(es, task->thread.es);
304 * Can't actually change these in 64-bit mode.
306 case offsetof(struct user_regs_struct,cs):
307 if (unlikely(value == 0))
309 task_pt_regs(task)->cs = value;
311 case offsetof(struct user_regs_struct,ss):
312 if (unlikely(value == 0))
314 task_pt_regs(task)->ss = value;
321 #endif /* CONFIG_X86_32 */
323 static unsigned long get_flags(struct task_struct *task)
325 unsigned long retval = task_pt_regs(task)->flags;
328 * If the debugger set TF, hide it from the readout.
330 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
331 retval &= ~X86_EFLAGS_TF;
336 static int set_flags(struct task_struct *task, unsigned long value)
338 struct pt_regs *regs = task_pt_regs(task);
341 * If the user value contains TF, mark that
342 * it was not "us" (the debugger) that set it.
343 * If not, make sure it stays set if we had.
345 if (value & X86_EFLAGS_TF)
346 clear_tsk_thread_flag(task, TIF_FORCED_TF);
347 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
348 value |= X86_EFLAGS_TF;
350 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
355 static int putreg(struct task_struct *child,
356 unsigned long offset, unsigned long value)
359 case offsetof(struct user_regs_struct, cs):
360 case offsetof(struct user_regs_struct, ds):
361 case offsetof(struct user_regs_struct, es):
362 case offsetof(struct user_regs_struct, fs):
363 case offsetof(struct user_regs_struct, gs):
364 case offsetof(struct user_regs_struct, ss):
365 return set_segment_reg(child, offset, value);
367 case offsetof(struct user_regs_struct, flags):
368 return set_flags(child, value);
371 case offsetof(struct user_regs_struct,fs_base):
372 if (value >= TASK_SIZE_MAX)
375 * When changing the FS base, use do_arch_prctl_64()
376 * to set the index to zero and to set the base
379 if (child->thread.fsbase != value)
380 return do_arch_prctl_64(child, ARCH_SET_FS, value);
382 case offsetof(struct user_regs_struct,gs_base):
384 * Exactly the same here as the %fs handling above.
386 if (value >= TASK_SIZE_MAX)
388 if (child->thread.gsbase != value)
389 return do_arch_prctl_64(child, ARCH_SET_GS, value);
394 *pt_regs_access(task_pt_regs(child), offset) = value;
398 static unsigned long getreg(struct task_struct *task, unsigned long offset)
401 case offsetof(struct user_regs_struct, cs):
402 case offsetof(struct user_regs_struct, ds):
403 case offsetof(struct user_regs_struct, es):
404 case offsetof(struct user_regs_struct, fs):
405 case offsetof(struct user_regs_struct, gs):
406 case offsetof(struct user_regs_struct, ss):
407 return get_segment_reg(task, offset);
409 case offsetof(struct user_regs_struct, flags):
410 return get_flags(task);
413 case offsetof(struct user_regs_struct, fs_base):
414 return x86_fsbase_read_task(task);
415 case offsetof(struct user_regs_struct, gs_base):
416 return x86_gsbase_read_task(task);
420 return *pt_regs_access(task_pt_regs(task), offset);
423 static int genregs_get(struct task_struct *target,
424 const struct user_regset *regset,
425 unsigned int pos, unsigned int count,
426 void *kbuf, void __user *ubuf)
429 unsigned long *k = kbuf;
430 while (count >= sizeof(*k)) {
431 *k++ = getreg(target, pos);
436 unsigned long __user *u = ubuf;
437 while (count >= sizeof(*u)) {
438 if (__put_user(getreg(target, pos), u++))
448 static int genregs_set(struct task_struct *target,
449 const struct user_regset *regset,
450 unsigned int pos, unsigned int count,
451 const void *kbuf, const void __user *ubuf)
455 const unsigned long *k = kbuf;
456 while (count >= sizeof(*k) && !ret) {
457 ret = putreg(target, pos, *k++);
462 const unsigned long __user *u = ubuf;
463 while (count >= sizeof(*u) && !ret) {
465 ret = __get_user(word, u++);
468 ret = putreg(target, pos, word);
476 static void ptrace_triggered(struct perf_event *bp,
477 struct perf_sample_data *data,
478 struct pt_regs *regs)
481 struct thread_struct *thread = &(current->thread);
484 * Store in the virtual DR6 register the fact that the breakpoint
485 * was hit so the thread's debugger will see it.
487 for (i = 0; i < HBP_NUM; i++) {
488 if (thread->ptrace_bps[i] == bp)
492 thread->debugreg6 |= (DR_TRAP0 << i);
496 * Walk through every ptrace breakpoints for this thread and
497 * build the dr7 value on top of their attributes.
500 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
504 struct arch_hw_breakpoint *info;
506 for (i = 0; i < HBP_NUM; i++) {
507 if (bp[i] && !bp[i]->attr.disabled) {
508 info = counter_arch_bp(bp[i]);
509 dr7 |= encode_dr7(i, info->len, info->type);
516 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
517 int len, int type, bool disabled)
519 int err, bp_len, bp_type;
521 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
523 attr->bp_len = bp_len;
524 attr->bp_type = bp_type;
525 attr->disabled = disabled;
531 static struct perf_event *
532 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
533 unsigned long addr, bool disabled)
535 struct perf_event_attr attr;
538 ptrace_breakpoint_init(&attr);
541 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
545 return register_user_hw_breakpoint(&attr, ptrace_triggered,
549 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
552 struct perf_event_attr attr = bp->attr;
555 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
559 return modify_user_hw_breakpoint(bp, &attr);
563 * Handle ptrace writes to debug register 7.
565 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
567 struct thread_struct *thread = &tsk->thread;
568 unsigned long old_dr7;
569 bool second_pass = false;
572 data &= ~DR_CONTROL_RESERVED;
573 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
577 for (i = 0; i < HBP_NUM; i++) {
579 bool disabled = !decode_dr7(data, i, &len, &type);
580 struct perf_event *bp = thread->ptrace_bps[i];
586 bp = ptrace_register_breakpoint(tsk,
587 len, type, 0, disabled);
593 thread->ptrace_bps[i] = bp;
597 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
602 /* Restore if the first pass failed, second_pass shouldn't fail. */
603 if (rc && !WARN_ON(second_pass)) {
614 * Handle PTRACE_PEEKUSR calls for the debug register area.
616 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
618 struct thread_struct *thread = &tsk->thread;
619 unsigned long val = 0;
622 int index = array_index_nospec(n, HBP_NUM);
623 struct perf_event *bp = thread->ptrace_bps[index];
626 val = bp->hw.info.address;
628 val = thread->debugreg6;
630 val = thread->ptrace_dr7;
635 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
638 struct thread_struct *t = &tsk->thread;
639 struct perf_event *bp = t->ptrace_bps[nr];
644 * Put stub len and type to create an inactive but correct bp.
646 * CHECKME: the previous code returned -EIO if the addr wasn't
647 * a valid task virtual addr. The new one will return -EINVAL in
649 * -EINVAL may be what we want for in-kernel breakpoints users,
650 * but -EIO looks better for ptrace, since we refuse a register
651 * writing for the user. And anyway this is the previous
654 bp = ptrace_register_breakpoint(tsk,
655 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
660 t->ptrace_bps[nr] = bp;
662 struct perf_event_attr attr = bp->attr;
665 err = modify_user_hw_breakpoint(bp, &attr);
672 * Handle PTRACE_POKEUSR calls for the debug register area.
674 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
677 struct thread_struct *thread = &tsk->thread;
678 /* There are no DR4 or DR5 registers */
682 rc = ptrace_set_breakpoint_addr(tsk, n, val);
684 thread->debugreg6 = val;
687 rc = ptrace_write_dr7(tsk, val);
689 thread->ptrace_dr7 = val;
695 * These access the current or another (stopped) task's io permission
696 * bitmap for debugging or core dump.
698 static int ioperm_active(struct task_struct *target,
699 const struct user_regset *regset)
701 struct io_bitmap *iobm = target->thread.io_bitmap;
703 return iobm ? DIV_ROUND_UP(iobm->max, regset->size) : 0;
706 static int ioperm_get(struct task_struct *target,
707 const struct user_regset *regset,
708 unsigned int pos, unsigned int count,
709 void *kbuf, void __user *ubuf)
711 struct io_bitmap *iobm = target->thread.io_bitmap;
716 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
717 iobm->bitmap, 0, IO_BITMAP_BYTES);
721 * Called by kernel/ptrace.c when detaching..
723 * Make sure the single step bit is not set.
725 void ptrace_disable(struct task_struct *child)
727 user_disable_single_step(child);
730 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
731 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
734 long arch_ptrace(struct task_struct *child, long request,
735 unsigned long addr, unsigned long data)
738 unsigned long __user *datap = (unsigned long __user *)data;
741 /* read the word at location addr in the USER area. */
742 case PTRACE_PEEKUSR: {
746 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
749 tmp = 0; /* Default return condition */
750 if (addr < sizeof(struct user_regs_struct))
751 tmp = getreg(child, addr);
752 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
753 addr <= offsetof(struct user, u_debugreg[7])) {
754 addr -= offsetof(struct user, u_debugreg[0]);
755 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
757 ret = put_user(tmp, datap);
761 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
763 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
766 if (addr < sizeof(struct user_regs_struct))
767 ret = putreg(child, addr, data);
768 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
769 addr <= offsetof(struct user, u_debugreg[7])) {
770 addr -= offsetof(struct user, u_debugreg[0]);
771 ret = ptrace_set_debugreg(child,
772 addr / sizeof(data), data);
776 case PTRACE_GETREGS: /* Get all gp regs from the child. */
777 return copy_regset_to_user(child,
778 task_user_regset_view(current),
780 0, sizeof(struct user_regs_struct),
783 case PTRACE_SETREGS: /* Set all gp regs in the child. */
784 return copy_regset_from_user(child,
785 task_user_regset_view(current),
787 0, sizeof(struct user_regs_struct),
790 case PTRACE_GETFPREGS: /* Get the child FPU state. */
791 return copy_regset_to_user(child,
792 task_user_regset_view(current),
794 0, sizeof(struct user_i387_struct),
797 case PTRACE_SETFPREGS: /* Set the child FPU state. */
798 return copy_regset_from_user(child,
799 task_user_regset_view(current),
801 0, sizeof(struct user_i387_struct),
805 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
806 return copy_regset_to_user(child, &user_x86_32_view,
808 0, sizeof(struct user_fxsr_struct),
811 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
812 return copy_regset_from_user(child, &user_x86_32_view,
814 0, sizeof(struct user_fxsr_struct),
818 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
819 case PTRACE_GET_THREAD_AREA:
822 ret = do_get_thread_area(child, addr,
823 (struct user_desc __user *)data);
826 case PTRACE_SET_THREAD_AREA:
829 ret = do_set_thread_area(child, addr,
830 (struct user_desc __user *)data, 0);
835 /* normal 64bit interface to access TLS data.
836 Works just like arch_prctl, except that the arguments
838 case PTRACE_ARCH_PRCTL:
839 ret = do_arch_prctl_64(child, data, addr);
844 ret = ptrace_request(child, request, addr, data);
851 #ifdef CONFIG_IA32_EMULATION
853 #include <linux/compat.h>
854 #include <linux/syscalls.h>
855 #include <asm/ia32.h>
856 #include <asm/user32.h>
859 case offsetof(struct user32, regs.l): \
860 regs->q = value; break
863 case offsetof(struct user32, regs.rs): \
864 return set_segment_reg(child, \
865 offsetof(struct user_regs_struct, rs), \
869 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
871 struct pt_regs *regs = task_pt_regs(child);
892 case offsetof(struct user32, regs.orig_eax):
894 * Warning: bizarre corner case fixup here. A 32-bit
895 * debugger setting orig_eax to -1 wants to disable
896 * syscall restart. Make sure that the syscall
897 * restart code sign-extends orig_ax. Also make sure
898 * we interpret the -ERESTART* codes correctly if
899 * loaded into regs->ax in case the task is not
900 * actually still sitting at the exit from a 32-bit
901 * syscall with TS_COMPAT still set.
903 regs->orig_ax = value;
904 if (syscall_get_nr(child, regs) >= 0)
905 child->thread_info.status |= TS_I386_REGS_POKED;
908 case offsetof(struct user32, regs.eflags):
909 return set_flags(child, value);
911 case offsetof(struct user32, u_debugreg[0]) ...
912 offsetof(struct user32, u_debugreg[7]):
913 regno -= offsetof(struct user32, u_debugreg[0]);
914 return ptrace_set_debugreg(child, regno / 4, value);
917 if (regno > sizeof(struct user32) || (regno & 3))
921 * Other dummy fields in the virtual user structure
933 case offsetof(struct user32, regs.l): \
934 *val = regs->q; break
937 case offsetof(struct user32, regs.rs): \
938 *val = get_segment_reg(child, \
939 offsetof(struct user_regs_struct, rs)); \
942 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
944 struct pt_regs *regs = task_pt_regs(child);
962 R32(orig_eax, orig_ax);
966 case offsetof(struct user32, regs.eflags):
967 *val = get_flags(child);
970 case offsetof(struct user32, u_debugreg[0]) ...
971 offsetof(struct user32, u_debugreg[7]):
972 regno -= offsetof(struct user32, u_debugreg[0]);
973 *val = ptrace_get_debugreg(child, regno / 4);
977 if (regno > sizeof(struct user32) || (regno & 3))
981 * Other dummy fields in the virtual user structure
993 static int genregs32_get(struct task_struct *target,
994 const struct user_regset *regset,
995 unsigned int pos, unsigned int count,
996 void *kbuf, void __user *ubuf)
999 compat_ulong_t *k = kbuf;
1000 while (count >= sizeof(*k)) {
1001 getreg32(target, pos, k++);
1002 count -= sizeof(*k);
1006 compat_ulong_t __user *u = ubuf;
1007 while (count >= sizeof(*u)) {
1008 compat_ulong_t word;
1009 getreg32(target, pos, &word);
1010 if (__put_user(word, u++))
1012 count -= sizeof(*u);
1020 static int genregs32_set(struct task_struct *target,
1021 const struct user_regset *regset,
1022 unsigned int pos, unsigned int count,
1023 const void *kbuf, const void __user *ubuf)
1027 const compat_ulong_t *k = kbuf;
1028 while (count >= sizeof(*k) && !ret) {
1029 ret = putreg32(target, pos, *k++);
1030 count -= sizeof(*k);
1034 const compat_ulong_t __user *u = ubuf;
1035 while (count >= sizeof(*u) && !ret) {
1036 compat_ulong_t word;
1037 ret = __get_user(word, u++);
1040 ret = putreg32(target, pos, word);
1041 count -= sizeof(*u);
1048 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1049 compat_ulong_t caddr, compat_ulong_t cdata)
1051 unsigned long addr = caddr;
1052 unsigned long data = cdata;
1053 void __user *datap = compat_ptr(data);
1058 case PTRACE_PEEKUSR:
1059 ret = getreg32(child, addr, &val);
1061 ret = put_user(val, (__u32 __user *)datap);
1064 case PTRACE_POKEUSR:
1065 ret = putreg32(child, addr, data);
1068 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1069 return copy_regset_to_user(child, &user_x86_32_view,
1071 0, sizeof(struct user_regs_struct32),
1074 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1075 return copy_regset_from_user(child, &user_x86_32_view,
1077 sizeof(struct user_regs_struct32),
1080 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1081 return copy_regset_to_user(child, &user_x86_32_view,
1083 sizeof(struct user_i387_ia32_struct),
1086 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1087 return copy_regset_from_user(
1088 child, &user_x86_32_view, REGSET_FP,
1089 0, sizeof(struct user_i387_ia32_struct), datap);
1091 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1092 return copy_regset_to_user(child, &user_x86_32_view,
1094 sizeof(struct user32_fxsr_struct),
1097 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1098 return copy_regset_from_user(child, &user_x86_32_view,
1100 sizeof(struct user32_fxsr_struct),
1103 case PTRACE_GET_THREAD_AREA:
1104 case PTRACE_SET_THREAD_AREA:
1105 return arch_ptrace(child, request, addr, data);
1108 return compat_ptrace_request(child, request, addr, data);
1113 #endif /* CONFIG_IA32_EMULATION */
1115 #ifdef CONFIG_X86_X32_ABI
1116 static long x32_arch_ptrace(struct task_struct *child,
1117 compat_long_t request, compat_ulong_t caddr,
1118 compat_ulong_t cdata)
1120 unsigned long addr = caddr;
1121 unsigned long data = cdata;
1122 void __user *datap = compat_ptr(data);
1126 /* Read 32bits at location addr in the USER area. Only allow
1127 to return the lower 32bits of segment and debug registers. */
1128 case PTRACE_PEEKUSR: {
1132 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1133 addr < offsetof(struct user_regs_struct, cs))
1136 tmp = 0; /* Default return condition */
1137 if (addr < sizeof(struct user_regs_struct))
1138 tmp = getreg(child, addr);
1139 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1140 addr <= offsetof(struct user, u_debugreg[7])) {
1141 addr -= offsetof(struct user, u_debugreg[0]);
1142 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1144 ret = put_user(tmp, (__u32 __user *)datap);
1148 /* Write the word at location addr in the USER area. Only allow
1149 to update segment and debug registers with the upper 32bits
1151 case PTRACE_POKEUSR:
1153 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1154 addr < offsetof(struct user_regs_struct, cs))
1157 if (addr < sizeof(struct user_regs_struct))
1158 ret = putreg(child, addr, data);
1159 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1160 addr <= offsetof(struct user, u_debugreg[7])) {
1161 addr -= offsetof(struct user, u_debugreg[0]);
1162 ret = ptrace_set_debugreg(child,
1163 addr / sizeof(data), data);
1167 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1168 return copy_regset_to_user(child,
1169 task_user_regset_view(current),
1171 0, sizeof(struct user_regs_struct),
1174 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1175 return copy_regset_from_user(child,
1176 task_user_regset_view(current),
1178 0, sizeof(struct user_regs_struct),
1181 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1182 return copy_regset_to_user(child,
1183 task_user_regset_view(current),
1185 0, sizeof(struct user_i387_struct),
1188 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1189 return copy_regset_from_user(child,
1190 task_user_regset_view(current),
1192 0, sizeof(struct user_i387_struct),
1196 return compat_ptrace_request(child, request, addr, data);
1203 #ifdef CONFIG_COMPAT
1204 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1205 compat_ulong_t caddr, compat_ulong_t cdata)
1207 #ifdef CONFIG_X86_X32_ABI
1208 if (!in_ia32_syscall())
1209 return x32_arch_ptrace(child, request, caddr, cdata);
1211 #ifdef CONFIG_IA32_EMULATION
1212 return ia32_arch_ptrace(child, request, caddr, cdata);
1217 #endif /* CONFIG_COMPAT */
1219 #ifdef CONFIG_X86_64
1221 static struct user_regset x86_64_regsets[] __ro_after_init = {
1222 [REGSET_GENERAL] = {
1223 .core_note_type = NT_PRSTATUS,
1224 .n = sizeof(struct user_regs_struct) / sizeof(long),
1225 .size = sizeof(long), .align = sizeof(long),
1226 .get = genregs_get, .set = genregs_set
1229 .core_note_type = NT_PRFPREG,
1230 .n = sizeof(struct user_i387_struct) / sizeof(long),
1231 .size = sizeof(long), .align = sizeof(long),
1232 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1235 .core_note_type = NT_X86_XSTATE,
1236 .size = sizeof(u64), .align = sizeof(u64),
1237 .active = xstateregs_active, .get = xstateregs_get,
1238 .set = xstateregs_set
1240 [REGSET_IOPERM64] = {
1241 .core_note_type = NT_386_IOPERM,
1242 .n = IO_BITMAP_LONGS,
1243 .size = sizeof(long), .align = sizeof(long),
1244 .active = ioperm_active, .get = ioperm_get
1248 static const struct user_regset_view user_x86_64_view = {
1249 .name = "x86_64", .e_machine = EM_X86_64,
1250 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1253 #else /* CONFIG_X86_32 */
1255 #define user_regs_struct32 user_regs_struct
1256 #define genregs32_get genregs_get
1257 #define genregs32_set genregs_set
1259 #endif /* CONFIG_X86_64 */
1261 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1262 static struct user_regset x86_32_regsets[] __ro_after_init = {
1263 [REGSET_GENERAL] = {
1264 .core_note_type = NT_PRSTATUS,
1265 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1266 .size = sizeof(u32), .align = sizeof(u32),
1267 .get = genregs32_get, .set = genregs32_set
1270 .core_note_type = NT_PRFPREG,
1271 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1272 .size = sizeof(u32), .align = sizeof(u32),
1273 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1276 .core_note_type = NT_PRXFPREG,
1277 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1278 .size = sizeof(u32), .align = sizeof(u32),
1279 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1282 .core_note_type = NT_X86_XSTATE,
1283 .size = sizeof(u64), .align = sizeof(u64),
1284 .active = xstateregs_active, .get = xstateregs_get,
1285 .set = xstateregs_set
1288 .core_note_type = NT_386_TLS,
1289 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1290 .size = sizeof(struct user_desc),
1291 .align = sizeof(struct user_desc),
1292 .active = regset_tls_active,
1293 .get = regset_tls_get, .set = regset_tls_set
1295 [REGSET_IOPERM32] = {
1296 .core_note_type = NT_386_IOPERM,
1297 .n = IO_BITMAP_BYTES / sizeof(u32),
1298 .size = sizeof(u32), .align = sizeof(u32),
1299 .active = ioperm_active, .get = ioperm_get
1303 static const struct user_regset_view user_x86_32_view = {
1304 .name = "i386", .e_machine = EM_386,
1305 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1310 * This represents bytes 464..511 in the memory layout exported through
1311 * the REGSET_XSTATE interface.
1313 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1315 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1317 #ifdef CONFIG_X86_64
1318 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1320 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1321 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1323 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1326 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1328 #ifdef CONFIG_IA32_EMULATION
1329 if (!user_64bit_mode(task_pt_regs(task)))
1331 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1332 return &user_x86_32_view;
1334 #ifdef CONFIG_X86_64
1335 return &user_x86_64_view;
1339 void send_sigtrap(struct pt_regs *regs, int error_code, int si_code)
1341 struct task_struct *tsk = current;
1343 tsk->thread.trap_nr = X86_TRAP_DB;
1344 tsk->thread.error_code = error_code;
1346 /* Send us the fake SIGTRAP */
1347 force_sig_fault(SIGTRAP, si_code,
1348 user_mode(regs) ? (void __user *)regs->ip : NULL);
1351 void user_single_step_report(struct pt_regs *regs)
1353 send_sigtrap(regs, 0, TRAP_BRKPT);