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>
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/fsgsbase.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 #endif /* CONFIG_X86_32 */
349 static unsigned long get_flags(struct task_struct *task)
351 unsigned long retval = task_pt_regs(task)->flags;
354 * If the debugger set TF, hide it from the readout.
356 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
357 retval &= ~X86_EFLAGS_TF;
362 static int set_flags(struct task_struct *task, unsigned long value)
364 struct pt_regs *regs = task_pt_regs(task);
367 * If the user value contains TF, mark that
368 * it was not "us" (the debugger) that set it.
369 * If not, make sure it stays set if we had.
371 if (value & X86_EFLAGS_TF)
372 clear_tsk_thread_flag(task, TIF_FORCED_TF);
373 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
374 value |= X86_EFLAGS_TF;
376 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
381 static int putreg(struct task_struct *child,
382 unsigned long offset, unsigned long value)
385 case offsetof(struct user_regs_struct, cs):
386 case offsetof(struct user_regs_struct, ds):
387 case offsetof(struct user_regs_struct, es):
388 case offsetof(struct user_regs_struct, fs):
389 case offsetof(struct user_regs_struct, gs):
390 case offsetof(struct user_regs_struct, ss):
391 return set_segment_reg(child, offset, value);
393 case offsetof(struct user_regs_struct, flags):
394 return set_flags(child, value);
397 case offsetof(struct user_regs_struct,fs_base):
398 if (value >= TASK_SIZE_MAX)
401 * When changing the FS base, use do_arch_prctl_64()
402 * to set the index to zero and to set the base
405 if (child->thread.fsbase != value)
406 return do_arch_prctl_64(child, ARCH_SET_FS, value);
408 case offsetof(struct user_regs_struct,gs_base):
410 * Exactly the same here as the %fs handling above.
412 if (value >= TASK_SIZE_MAX)
414 if (child->thread.gsbase != value)
415 return do_arch_prctl_64(child, ARCH_SET_GS, value);
420 *pt_regs_access(task_pt_regs(child), offset) = value;
424 static unsigned long getreg(struct task_struct *task, unsigned long offset)
427 case offsetof(struct user_regs_struct, cs):
428 case offsetof(struct user_regs_struct, ds):
429 case offsetof(struct user_regs_struct, es):
430 case offsetof(struct user_regs_struct, fs):
431 case offsetof(struct user_regs_struct, gs):
432 case offsetof(struct user_regs_struct, ss):
433 return get_segment_reg(task, offset);
435 case offsetof(struct user_regs_struct, flags):
436 return get_flags(task);
439 case offsetof(struct user_regs_struct, fs_base):
440 return x86_fsbase_read_task(task);
441 case offsetof(struct user_regs_struct, gs_base):
442 return x86_gsbase_read_task(task);
446 return *pt_regs_access(task_pt_regs(task), offset);
449 static int genregs_get(struct task_struct *target,
450 const struct user_regset *regset,
451 unsigned int pos, unsigned int count,
452 void *kbuf, void __user *ubuf)
455 unsigned long *k = kbuf;
456 while (count >= sizeof(*k)) {
457 *k++ = getreg(target, pos);
462 unsigned long __user *u = ubuf;
463 while (count >= sizeof(*u)) {
464 if (__put_user(getreg(target, pos), u++))
474 static int genregs_set(struct task_struct *target,
475 const struct user_regset *regset,
476 unsigned int pos, unsigned int count,
477 const void *kbuf, const void __user *ubuf)
481 const unsigned long *k = kbuf;
482 while (count >= sizeof(*k) && !ret) {
483 ret = putreg(target, pos, *k++);
488 const unsigned long __user *u = ubuf;
489 while (count >= sizeof(*u) && !ret) {
491 ret = __get_user(word, u++);
494 ret = putreg(target, pos, word);
502 static void ptrace_triggered(struct perf_event *bp,
503 struct perf_sample_data *data,
504 struct pt_regs *regs)
507 struct thread_struct *thread = &(current->thread);
510 * Store in the virtual DR6 register the fact that the breakpoint
511 * was hit so the thread's debugger will see it.
513 for (i = 0; i < HBP_NUM; i++) {
514 if (thread->ptrace_bps[i] == bp)
518 thread->debugreg6 |= (DR_TRAP0 << i);
522 * Walk through every ptrace breakpoints for this thread and
523 * build the dr7 value on top of their attributes.
526 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
530 struct arch_hw_breakpoint *info;
532 for (i = 0; i < HBP_NUM; i++) {
533 if (bp[i] && !bp[i]->attr.disabled) {
534 info = counter_arch_bp(bp[i]);
535 dr7 |= encode_dr7(i, info->len, info->type);
542 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
543 int len, int type, bool disabled)
545 int err, bp_len, bp_type;
547 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
549 attr->bp_len = bp_len;
550 attr->bp_type = bp_type;
551 attr->disabled = disabled;
557 static struct perf_event *
558 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
559 unsigned long addr, bool disabled)
561 struct perf_event_attr attr;
564 ptrace_breakpoint_init(&attr);
567 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
571 return register_user_hw_breakpoint(&attr, ptrace_triggered,
575 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
578 struct perf_event_attr attr = bp->attr;
581 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
585 return modify_user_hw_breakpoint(bp, &attr);
589 * Handle ptrace writes to debug register 7.
591 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
593 struct thread_struct *thread = &tsk->thread;
594 unsigned long old_dr7;
595 bool second_pass = false;
598 data &= ~DR_CONTROL_RESERVED;
599 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
603 for (i = 0; i < HBP_NUM; i++) {
605 bool disabled = !decode_dr7(data, i, &len, &type);
606 struct perf_event *bp = thread->ptrace_bps[i];
612 bp = ptrace_register_breakpoint(tsk,
613 len, type, 0, disabled);
619 thread->ptrace_bps[i] = bp;
623 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
628 /* Restore if the first pass failed, second_pass shouldn't fail. */
629 if (rc && !WARN_ON(second_pass)) {
640 * Handle PTRACE_PEEKUSR calls for the debug register area.
642 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
644 struct thread_struct *thread = &tsk->thread;
645 unsigned long val = 0;
648 struct perf_event *bp = thread->ptrace_bps[n];
651 val = bp->hw.info.address;
653 val = thread->debugreg6;
655 val = thread->ptrace_dr7;
660 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
663 struct thread_struct *t = &tsk->thread;
664 struct perf_event *bp = t->ptrace_bps[nr];
669 * Put stub len and type to create an inactive but correct bp.
671 * CHECKME: the previous code returned -EIO if the addr wasn't
672 * a valid task virtual addr. The new one will return -EINVAL in
674 * -EINVAL may be what we want for in-kernel breakpoints users,
675 * but -EIO looks better for ptrace, since we refuse a register
676 * writing for the user. And anyway this is the previous
679 bp = ptrace_register_breakpoint(tsk,
680 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
685 t->ptrace_bps[nr] = bp;
687 struct perf_event_attr attr = bp->attr;
690 err = modify_user_hw_breakpoint(bp, &attr);
697 * Handle PTRACE_POKEUSR calls for the debug register area.
699 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
702 struct thread_struct *thread = &tsk->thread;
703 /* There are no DR4 or DR5 registers */
707 rc = ptrace_set_breakpoint_addr(tsk, n, val);
709 thread->debugreg6 = val;
712 rc = ptrace_write_dr7(tsk, val);
714 thread->ptrace_dr7 = val;
720 * These access the current or another (stopped) task's io permission
721 * bitmap for debugging or core dump.
723 static int ioperm_active(struct task_struct *target,
724 const struct user_regset *regset)
726 return target->thread.io_bitmap_max / regset->size;
729 static int ioperm_get(struct task_struct *target,
730 const struct user_regset *regset,
731 unsigned int pos, unsigned int count,
732 void *kbuf, void __user *ubuf)
734 if (!target->thread.io_bitmap_ptr)
737 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
738 target->thread.io_bitmap_ptr,
743 * Called by kernel/ptrace.c when detaching..
745 * Make sure the single step bit is not set.
747 void ptrace_disable(struct task_struct *child)
749 user_disable_single_step(child);
752 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
753 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
756 long arch_ptrace(struct task_struct *child, long request,
757 unsigned long addr, unsigned long data)
760 unsigned long __user *datap = (unsigned long __user *)data;
763 /* read the word at location addr in the USER area. */
764 case PTRACE_PEEKUSR: {
768 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
771 tmp = 0; /* Default return condition */
772 if (addr < sizeof(struct user_regs_struct))
773 tmp = getreg(child, addr);
774 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
775 addr <= offsetof(struct user, u_debugreg[7])) {
776 addr -= offsetof(struct user, u_debugreg[0]);
777 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
779 ret = put_user(tmp, datap);
783 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
785 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
788 if (addr < sizeof(struct user_regs_struct))
789 ret = putreg(child, addr, data);
790 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
791 addr <= offsetof(struct user, u_debugreg[7])) {
792 addr -= offsetof(struct user, u_debugreg[0]);
793 ret = ptrace_set_debugreg(child,
794 addr / sizeof(data), data);
798 case PTRACE_GETREGS: /* Get all gp regs from the child. */
799 return copy_regset_to_user(child,
800 task_user_regset_view(current),
802 0, sizeof(struct user_regs_struct),
805 case PTRACE_SETREGS: /* Set all gp regs in the child. */
806 return copy_regset_from_user(child,
807 task_user_regset_view(current),
809 0, sizeof(struct user_regs_struct),
812 case PTRACE_GETFPREGS: /* Get the child FPU state. */
813 return copy_regset_to_user(child,
814 task_user_regset_view(current),
816 0, sizeof(struct user_i387_struct),
819 case PTRACE_SETFPREGS: /* Set the child FPU state. */
820 return copy_regset_from_user(child,
821 task_user_regset_view(current),
823 0, sizeof(struct user_i387_struct),
827 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
828 return copy_regset_to_user(child, &user_x86_32_view,
830 0, sizeof(struct user_fxsr_struct),
833 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
834 return copy_regset_from_user(child, &user_x86_32_view,
836 0, sizeof(struct user_fxsr_struct),
840 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
841 case PTRACE_GET_THREAD_AREA:
844 ret = do_get_thread_area(child, addr,
845 (struct user_desc __user *)data);
848 case PTRACE_SET_THREAD_AREA:
851 ret = do_set_thread_area(child, addr,
852 (struct user_desc __user *)data, 0);
857 /* normal 64bit interface to access TLS data.
858 Works just like arch_prctl, except that the arguments
860 case PTRACE_ARCH_PRCTL:
861 ret = do_arch_prctl_64(child, data, addr);
866 ret = ptrace_request(child, request, addr, data);
873 #ifdef CONFIG_IA32_EMULATION
875 #include <linux/compat.h>
876 #include <linux/syscalls.h>
877 #include <asm/ia32.h>
878 #include <asm/user32.h>
881 case offsetof(struct user32, regs.l): \
882 regs->q = value; break
885 case offsetof(struct user32, regs.rs): \
886 return set_segment_reg(child, \
887 offsetof(struct user_regs_struct, rs), \
891 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
893 struct pt_regs *regs = task_pt_regs(child);
914 case offsetof(struct user32, regs.orig_eax):
916 * Warning: bizarre corner case fixup here. A 32-bit
917 * debugger setting orig_eax to -1 wants to disable
918 * syscall restart. Make sure that the syscall
919 * restart code sign-extends orig_ax. Also make sure
920 * we interpret the -ERESTART* codes correctly if
921 * loaded into regs->ax in case the task is not
922 * actually still sitting at the exit from a 32-bit
923 * syscall with TS_COMPAT still set.
925 regs->orig_ax = value;
926 if (syscall_get_nr(child, regs) >= 0)
927 child->thread_info.status |= TS_I386_REGS_POKED;
930 case offsetof(struct user32, regs.eflags):
931 return set_flags(child, value);
933 case offsetof(struct user32, u_debugreg[0]) ...
934 offsetof(struct user32, u_debugreg[7]):
935 regno -= offsetof(struct user32, u_debugreg[0]);
936 return ptrace_set_debugreg(child, regno / 4, value);
939 if (regno > sizeof(struct user32) || (regno & 3))
943 * Other dummy fields in the virtual user structure
955 case offsetof(struct user32, regs.l): \
956 *val = regs->q; break
959 case offsetof(struct user32, regs.rs): \
960 *val = get_segment_reg(child, \
961 offsetof(struct user_regs_struct, rs)); \
964 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
966 struct pt_regs *regs = task_pt_regs(child);
984 R32(orig_eax, orig_ax);
988 case offsetof(struct user32, regs.eflags):
989 *val = get_flags(child);
992 case offsetof(struct user32, u_debugreg[0]) ...
993 offsetof(struct user32, u_debugreg[7]):
994 regno -= offsetof(struct user32, u_debugreg[0]);
995 *val = ptrace_get_debugreg(child, regno / 4);
999 if (regno > sizeof(struct user32) || (regno & 3))
1003 * Other dummy fields in the virtual user structure
1015 static int genregs32_get(struct task_struct *target,
1016 const struct user_regset *regset,
1017 unsigned int pos, unsigned int count,
1018 void *kbuf, void __user *ubuf)
1021 compat_ulong_t *k = kbuf;
1022 while (count >= sizeof(*k)) {
1023 getreg32(target, pos, k++);
1024 count -= sizeof(*k);
1028 compat_ulong_t __user *u = ubuf;
1029 while (count >= sizeof(*u)) {
1030 compat_ulong_t word;
1031 getreg32(target, pos, &word);
1032 if (__put_user(word, u++))
1034 count -= sizeof(*u);
1042 static int genregs32_set(struct task_struct *target,
1043 const struct user_regset *regset,
1044 unsigned int pos, unsigned int count,
1045 const void *kbuf, const void __user *ubuf)
1049 const compat_ulong_t *k = kbuf;
1050 while (count >= sizeof(*k) && !ret) {
1051 ret = putreg32(target, pos, *k++);
1052 count -= sizeof(*k);
1056 const compat_ulong_t __user *u = ubuf;
1057 while (count >= sizeof(*u) && !ret) {
1058 compat_ulong_t word;
1059 ret = __get_user(word, u++);
1062 ret = putreg32(target, pos, word);
1063 count -= sizeof(*u);
1070 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1071 compat_ulong_t caddr, compat_ulong_t cdata)
1073 unsigned long addr = caddr;
1074 unsigned long data = cdata;
1075 void __user *datap = compat_ptr(data);
1080 case PTRACE_PEEKUSR:
1081 ret = getreg32(child, addr, &val);
1083 ret = put_user(val, (__u32 __user *)datap);
1086 case PTRACE_POKEUSR:
1087 ret = putreg32(child, addr, data);
1090 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1091 return copy_regset_to_user(child, &user_x86_32_view,
1093 0, sizeof(struct user_regs_struct32),
1096 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1097 return copy_regset_from_user(child, &user_x86_32_view,
1099 sizeof(struct user_regs_struct32),
1102 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1103 return copy_regset_to_user(child, &user_x86_32_view,
1105 sizeof(struct user_i387_ia32_struct),
1108 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1109 return copy_regset_from_user(
1110 child, &user_x86_32_view, REGSET_FP,
1111 0, sizeof(struct user_i387_ia32_struct), datap);
1113 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1114 return copy_regset_to_user(child, &user_x86_32_view,
1116 sizeof(struct user32_fxsr_struct),
1119 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1120 return copy_regset_from_user(child, &user_x86_32_view,
1122 sizeof(struct user32_fxsr_struct),
1125 case PTRACE_GET_THREAD_AREA:
1126 case PTRACE_SET_THREAD_AREA:
1127 return arch_ptrace(child, request, addr, data);
1130 return compat_ptrace_request(child, request, addr, data);
1135 #endif /* CONFIG_IA32_EMULATION */
1137 #ifdef CONFIG_X86_X32_ABI
1138 static long x32_arch_ptrace(struct task_struct *child,
1139 compat_long_t request, compat_ulong_t caddr,
1140 compat_ulong_t cdata)
1142 unsigned long addr = caddr;
1143 unsigned long data = cdata;
1144 void __user *datap = compat_ptr(data);
1148 /* Read 32bits at location addr in the USER area. Only allow
1149 to return the lower 32bits of segment and debug registers. */
1150 case PTRACE_PEEKUSR: {
1154 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1155 addr < offsetof(struct user_regs_struct, cs))
1158 tmp = 0; /* Default return condition */
1159 if (addr < sizeof(struct user_regs_struct))
1160 tmp = getreg(child, addr);
1161 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1162 addr <= offsetof(struct user, u_debugreg[7])) {
1163 addr -= offsetof(struct user, u_debugreg[0]);
1164 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1166 ret = put_user(tmp, (__u32 __user *)datap);
1170 /* Write the word at location addr in the USER area. Only allow
1171 to update segment and debug registers with the upper 32bits
1173 case PTRACE_POKEUSR:
1175 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1176 addr < offsetof(struct user_regs_struct, cs))
1179 if (addr < sizeof(struct user_regs_struct))
1180 ret = putreg(child, addr, data);
1181 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1182 addr <= offsetof(struct user, u_debugreg[7])) {
1183 addr -= offsetof(struct user, u_debugreg[0]);
1184 ret = ptrace_set_debugreg(child,
1185 addr / sizeof(data), data);
1189 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1190 return copy_regset_to_user(child,
1191 task_user_regset_view(current),
1193 0, sizeof(struct user_regs_struct),
1196 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1197 return copy_regset_from_user(child,
1198 task_user_regset_view(current),
1200 0, sizeof(struct user_regs_struct),
1203 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1204 return copy_regset_to_user(child,
1205 task_user_regset_view(current),
1207 0, sizeof(struct user_i387_struct),
1210 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1211 return copy_regset_from_user(child,
1212 task_user_regset_view(current),
1214 0, sizeof(struct user_i387_struct),
1218 return compat_ptrace_request(child, request, addr, data);
1225 #ifdef CONFIG_COMPAT
1226 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1227 compat_ulong_t caddr, compat_ulong_t cdata)
1229 #ifdef CONFIG_X86_X32_ABI
1230 if (!in_ia32_syscall())
1231 return x32_arch_ptrace(child, request, caddr, cdata);
1233 #ifdef CONFIG_IA32_EMULATION
1234 return ia32_arch_ptrace(child, request, caddr, cdata);
1239 #endif /* CONFIG_COMPAT */
1241 #ifdef CONFIG_X86_64
1243 static struct user_regset x86_64_regsets[] __ro_after_init = {
1244 [REGSET_GENERAL] = {
1245 .core_note_type = NT_PRSTATUS,
1246 .n = sizeof(struct user_regs_struct) / sizeof(long),
1247 .size = sizeof(long), .align = sizeof(long),
1248 .get = genregs_get, .set = genregs_set
1251 .core_note_type = NT_PRFPREG,
1252 .n = sizeof(struct user_i387_struct) / sizeof(long),
1253 .size = sizeof(long), .align = sizeof(long),
1254 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1257 .core_note_type = NT_X86_XSTATE,
1258 .size = sizeof(u64), .align = sizeof(u64),
1259 .active = xstateregs_active, .get = xstateregs_get,
1260 .set = xstateregs_set
1262 [REGSET_IOPERM64] = {
1263 .core_note_type = NT_386_IOPERM,
1264 .n = IO_BITMAP_LONGS,
1265 .size = sizeof(long), .align = sizeof(long),
1266 .active = ioperm_active, .get = ioperm_get
1270 static const struct user_regset_view user_x86_64_view = {
1271 .name = "x86_64", .e_machine = EM_X86_64,
1272 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1275 #else /* CONFIG_X86_32 */
1277 #define user_regs_struct32 user_regs_struct
1278 #define genregs32_get genregs_get
1279 #define genregs32_set genregs_set
1281 #endif /* CONFIG_X86_64 */
1283 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1284 static struct user_regset x86_32_regsets[] __ro_after_init = {
1285 [REGSET_GENERAL] = {
1286 .core_note_type = NT_PRSTATUS,
1287 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1288 .size = sizeof(u32), .align = sizeof(u32),
1289 .get = genregs32_get, .set = genregs32_set
1292 .core_note_type = NT_PRFPREG,
1293 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1294 .size = sizeof(u32), .align = sizeof(u32),
1295 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1298 .core_note_type = NT_PRXFPREG,
1299 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1300 .size = sizeof(u32), .align = sizeof(u32),
1301 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1304 .core_note_type = NT_X86_XSTATE,
1305 .size = sizeof(u64), .align = sizeof(u64),
1306 .active = xstateregs_active, .get = xstateregs_get,
1307 .set = xstateregs_set
1310 .core_note_type = NT_386_TLS,
1311 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1312 .size = sizeof(struct user_desc),
1313 .align = sizeof(struct user_desc),
1314 .active = regset_tls_active,
1315 .get = regset_tls_get, .set = regset_tls_set
1317 [REGSET_IOPERM32] = {
1318 .core_note_type = NT_386_IOPERM,
1319 .n = IO_BITMAP_BYTES / sizeof(u32),
1320 .size = sizeof(u32), .align = sizeof(u32),
1321 .active = ioperm_active, .get = ioperm_get
1325 static const struct user_regset_view user_x86_32_view = {
1326 .name = "i386", .e_machine = EM_386,
1327 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1332 * This represents bytes 464..511 in the memory layout exported through
1333 * the REGSET_XSTATE interface.
1335 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1337 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1339 #ifdef CONFIG_X86_64
1340 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1342 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1343 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1345 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1350 #ifdef CONFIG_IA32_EMULATION
1351 if (!user_64bit_mode(task_pt_regs(task)))
1353 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 return &user_x86_32_view;
1356 #ifdef CONFIG_X86_64
1357 return &user_x86_64_view;
1361 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1362 int error_code, int si_code)
1364 tsk->thread.trap_nr = X86_TRAP_DB;
1365 tsk->thread.error_code = error_code;
1367 /* Send us the fake SIGTRAP */
1368 force_sig_fault(SIGTRAP, si_code,
1369 user_mode(regs) ? (void __user *)regs->ip : NULL, tsk);
1372 void user_single_step_report(struct pt_regs *regs)
1374 send_sigtrap(current, regs, 0, TRAP_BRKPT);