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
159 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
160 * when it traps. The previous stack will be directly underneath the saved
161 * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'.
163 * Now, if the stack is empty, '®s->sp' is out of range. In this
164 * case we try to take the previous stack. To always return a non-null
165 * stack pointer we fall back to regs as stack if no previous stack
168 * This is valid only for kernel mode traps.
170 unsigned long kernel_stack_pointer(struct pt_regs *regs)
172 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
173 unsigned long sp = (unsigned long)®s->sp;
176 if (context == (sp & ~(THREAD_SIZE - 1)))
179 prev_esp = (u32 *)(context);
181 return (unsigned long)*prev_esp;
183 return (unsigned long)regs;
185 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
187 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
189 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
190 return ®s->bx + (regno >> 2);
193 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
196 * Returning the value truncates it to 16 bits.
199 if (offset != offsetof(struct user_regs_struct, gs))
200 retval = *pt_regs_access(task_pt_regs(task), offset);
203 retval = get_user_gs(task_pt_regs(task));
205 retval = task_user_gs(task);
210 static int set_segment_reg(struct task_struct *task,
211 unsigned long offset, u16 value)
214 * The value argument was already truncated to 16 bits.
216 if (invalid_selector(value))
220 * For %cs and %ss we cannot permit a null selector.
221 * We can permit a bogus selector as long as it has USER_RPL.
222 * Null selectors are fine for other segment registers, but
223 * we will never get back to user mode with invalid %cs or %ss
224 * and will take the trap in iret instead. Much code relies
225 * on user_mode() to distinguish a user trap frame (which can
226 * safely use invalid selectors) from a kernel trap frame.
229 case offsetof(struct user_regs_struct, cs):
230 case offsetof(struct user_regs_struct, ss):
231 if (unlikely(value == 0))
235 *pt_regs_access(task_pt_regs(task), offset) = value;
238 case offsetof(struct user_regs_struct, gs):
240 set_user_gs(task_pt_regs(task), value);
242 task_user_gs(task) = value;
248 #else /* CONFIG_X86_64 */
250 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
252 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
254 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
255 return ®s->r15 + (offset / sizeof(regs->r15));
258 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
261 * Returning the value truncates it to 16 bits.
266 case offsetof(struct user_regs_struct, fs):
267 if (task == current) {
268 /* Older gas can't assemble movq %?s,%r?? */
269 asm("movl %%fs,%0" : "=r" (seg));
272 return task->thread.fsindex;
273 case offsetof(struct user_regs_struct, gs):
274 if (task == current) {
275 asm("movl %%gs,%0" : "=r" (seg));
278 return task->thread.gsindex;
279 case offsetof(struct user_regs_struct, ds):
280 if (task == current) {
281 asm("movl %%ds,%0" : "=r" (seg));
284 return task->thread.ds;
285 case offsetof(struct user_regs_struct, es):
286 if (task == current) {
287 asm("movl %%es,%0" : "=r" (seg));
290 return task->thread.es;
292 case offsetof(struct user_regs_struct, cs):
293 case offsetof(struct user_regs_struct, ss):
296 return *pt_regs_access(task_pt_regs(task), offset);
299 static int set_segment_reg(struct task_struct *task,
300 unsigned long offset, u16 value)
303 * The value argument was already truncated to 16 bits.
305 if (invalid_selector(value))
309 case offsetof(struct user_regs_struct,fs):
310 task->thread.fsindex = value;
312 loadsegment(fs, task->thread.fsindex);
314 case offsetof(struct user_regs_struct,gs):
315 task->thread.gsindex = value;
317 load_gs_index(task->thread.gsindex);
319 case offsetof(struct user_regs_struct,ds):
320 task->thread.ds = value;
322 loadsegment(ds, task->thread.ds);
324 case offsetof(struct user_regs_struct,es):
325 task->thread.es = value;
327 loadsegment(es, task->thread.es);
331 * Can't actually change these in 64-bit mode.
333 case offsetof(struct user_regs_struct,cs):
334 if (unlikely(value == 0))
336 task_pt_regs(task)->cs = value;
338 case offsetof(struct user_regs_struct,ss):
339 if (unlikely(value == 0))
341 task_pt_regs(task)->ss = value;
348 #endif /* CONFIG_X86_32 */
350 static unsigned long get_flags(struct task_struct *task)
352 unsigned long retval = task_pt_regs(task)->flags;
355 * If the debugger set TF, hide it from the readout.
357 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
358 retval &= ~X86_EFLAGS_TF;
363 static int set_flags(struct task_struct *task, unsigned long value)
365 struct pt_regs *regs = task_pt_regs(task);
368 * If the user value contains TF, mark that
369 * it was not "us" (the debugger) that set it.
370 * If not, make sure it stays set if we had.
372 if (value & X86_EFLAGS_TF)
373 clear_tsk_thread_flag(task, TIF_FORCED_TF);
374 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
375 value |= X86_EFLAGS_TF;
377 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
382 static int putreg(struct task_struct *child,
383 unsigned long offset, unsigned long value)
386 case offsetof(struct user_regs_struct, cs):
387 case offsetof(struct user_regs_struct, ds):
388 case offsetof(struct user_regs_struct, es):
389 case offsetof(struct user_regs_struct, fs):
390 case offsetof(struct user_regs_struct, gs):
391 case offsetof(struct user_regs_struct, ss):
392 return set_segment_reg(child, offset, value);
394 case offsetof(struct user_regs_struct, flags):
395 return set_flags(child, value);
398 case offsetof(struct user_regs_struct,fs_base):
399 if (value >= TASK_SIZE_MAX)
402 * When changing the FS base, use do_arch_prctl_64()
403 * to set the index to zero and to set the base
406 if (child->thread.fsbase != value)
407 return do_arch_prctl_64(child, ARCH_SET_FS, value);
409 case offsetof(struct user_regs_struct,gs_base):
411 * Exactly the same here as the %fs handling above.
413 if (value >= TASK_SIZE_MAX)
415 if (child->thread.gsbase != value)
416 return do_arch_prctl_64(child, ARCH_SET_GS, value);
421 *pt_regs_access(task_pt_regs(child), offset) = value;
425 static unsigned long getreg(struct task_struct *task, unsigned long offset)
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 get_segment_reg(task, offset);
436 case offsetof(struct user_regs_struct, flags):
437 return get_flags(task);
440 case offsetof(struct user_regs_struct, fs_base):
441 return x86_fsbase_read_task(task);
442 case offsetof(struct user_regs_struct, gs_base):
443 return x86_gsbase_read_task(task);
447 return *pt_regs_access(task_pt_regs(task), offset);
450 static int genregs_get(struct task_struct *target,
451 const struct user_regset *regset,
452 unsigned int pos, unsigned int count,
453 void *kbuf, void __user *ubuf)
456 unsigned long *k = kbuf;
457 while (count >= sizeof(*k)) {
458 *k++ = getreg(target, pos);
463 unsigned long __user *u = ubuf;
464 while (count >= sizeof(*u)) {
465 if (__put_user(getreg(target, pos), u++))
475 static int genregs_set(struct task_struct *target,
476 const struct user_regset *regset,
477 unsigned int pos, unsigned int count,
478 const void *kbuf, const void __user *ubuf)
482 const unsigned long *k = kbuf;
483 while (count >= sizeof(*k) && !ret) {
484 ret = putreg(target, pos, *k++);
489 const unsigned long __user *u = ubuf;
490 while (count >= sizeof(*u) && !ret) {
492 ret = __get_user(word, u++);
495 ret = putreg(target, pos, word);
503 static void ptrace_triggered(struct perf_event *bp,
504 struct perf_sample_data *data,
505 struct pt_regs *regs)
508 struct thread_struct *thread = &(current->thread);
511 * Store in the virtual DR6 register the fact that the breakpoint
512 * was hit so the thread's debugger will see it.
514 for (i = 0; i < HBP_NUM; i++) {
515 if (thread->ptrace_bps[i] == bp)
519 thread->debugreg6 |= (DR_TRAP0 << i);
523 * Walk through every ptrace breakpoints for this thread and
524 * build the dr7 value on top of their attributes.
527 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
531 struct arch_hw_breakpoint *info;
533 for (i = 0; i < HBP_NUM; i++) {
534 if (bp[i] && !bp[i]->attr.disabled) {
535 info = counter_arch_bp(bp[i]);
536 dr7 |= encode_dr7(i, info->len, info->type);
543 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
544 int len, int type, bool disabled)
546 int err, bp_len, bp_type;
548 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
550 attr->bp_len = bp_len;
551 attr->bp_type = bp_type;
552 attr->disabled = disabled;
558 static struct perf_event *
559 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
560 unsigned long addr, bool disabled)
562 struct perf_event_attr attr;
565 ptrace_breakpoint_init(&attr);
568 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
572 return register_user_hw_breakpoint(&attr, ptrace_triggered,
576 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
579 struct perf_event_attr attr = bp->attr;
582 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
586 return modify_user_hw_breakpoint(bp, &attr);
590 * Handle ptrace writes to debug register 7.
592 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
594 struct thread_struct *thread = &tsk->thread;
595 unsigned long old_dr7;
596 bool second_pass = false;
599 data &= ~DR_CONTROL_RESERVED;
600 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
604 for (i = 0; i < HBP_NUM; i++) {
606 bool disabled = !decode_dr7(data, i, &len, &type);
607 struct perf_event *bp = thread->ptrace_bps[i];
613 bp = ptrace_register_breakpoint(tsk,
614 len, type, 0, disabled);
620 thread->ptrace_bps[i] = bp;
624 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
629 /* Restore if the first pass failed, second_pass shouldn't fail. */
630 if (rc && !WARN_ON(second_pass)) {
641 * Handle PTRACE_PEEKUSR calls for the debug register area.
643 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
645 struct thread_struct *thread = &tsk->thread;
646 unsigned long val = 0;
650 index = array_index_nospec(index, HBP_NUM);
651 struct perf_event *bp = thread->ptrace_bps[index];
654 val = bp->hw.info.address;
656 val = thread->debugreg6;
658 val = thread->ptrace_dr7;
663 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
666 struct thread_struct *t = &tsk->thread;
667 struct perf_event *bp = t->ptrace_bps[nr];
672 * Put stub len and type to create an inactive but correct bp.
674 * CHECKME: the previous code returned -EIO if the addr wasn't
675 * a valid task virtual addr. The new one will return -EINVAL in
677 * -EINVAL may be what we want for in-kernel breakpoints users,
678 * but -EIO looks better for ptrace, since we refuse a register
679 * writing for the user. And anyway this is the previous
682 bp = ptrace_register_breakpoint(tsk,
683 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
688 t->ptrace_bps[nr] = bp;
690 struct perf_event_attr attr = bp->attr;
693 err = modify_user_hw_breakpoint(bp, &attr);
700 * Handle PTRACE_POKEUSR calls for the debug register area.
702 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
705 struct thread_struct *thread = &tsk->thread;
706 /* There are no DR4 or DR5 registers */
710 rc = ptrace_set_breakpoint_addr(tsk, n, val);
712 thread->debugreg6 = val;
715 rc = ptrace_write_dr7(tsk, val);
717 thread->ptrace_dr7 = val;
723 * These access the current or another (stopped) task's io permission
724 * bitmap for debugging or core dump.
726 static int ioperm_active(struct task_struct *target,
727 const struct user_regset *regset)
729 return target->thread.io_bitmap_max / regset->size;
732 static int ioperm_get(struct task_struct *target,
733 const struct user_regset *regset,
734 unsigned int pos, unsigned int count,
735 void *kbuf, void __user *ubuf)
737 if (!target->thread.io_bitmap_ptr)
740 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
741 target->thread.io_bitmap_ptr,
746 * Called by kernel/ptrace.c when detaching..
748 * Make sure the single step bit is not set.
750 void ptrace_disable(struct task_struct *child)
752 user_disable_single_step(child);
753 #ifdef TIF_SYSCALL_EMU
754 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
758 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
759 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
762 long arch_ptrace(struct task_struct *child, long request,
763 unsigned long addr, unsigned long data)
766 unsigned long __user *datap = (unsigned long __user *)data;
769 /* read the word at location addr in the USER area. */
770 case PTRACE_PEEKUSR: {
774 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
777 tmp = 0; /* Default return condition */
778 if (addr < sizeof(struct user_regs_struct))
779 tmp = getreg(child, addr);
780 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
781 addr <= offsetof(struct user, u_debugreg[7])) {
782 addr -= offsetof(struct user, u_debugreg[0]);
783 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
785 ret = put_user(tmp, datap);
789 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
791 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
794 if (addr < sizeof(struct user_regs_struct))
795 ret = putreg(child, addr, data);
796 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
797 addr <= offsetof(struct user, u_debugreg[7])) {
798 addr -= offsetof(struct user, u_debugreg[0]);
799 ret = ptrace_set_debugreg(child,
800 addr / sizeof(data), data);
804 case PTRACE_GETREGS: /* Get all gp regs from the child. */
805 return copy_regset_to_user(child,
806 task_user_regset_view(current),
808 0, sizeof(struct user_regs_struct),
811 case PTRACE_SETREGS: /* Set all gp regs in the child. */
812 return copy_regset_from_user(child,
813 task_user_regset_view(current),
815 0, sizeof(struct user_regs_struct),
818 case PTRACE_GETFPREGS: /* Get the child FPU state. */
819 return copy_regset_to_user(child,
820 task_user_regset_view(current),
822 0, sizeof(struct user_i387_struct),
825 case PTRACE_SETFPREGS: /* Set the child FPU state. */
826 return copy_regset_from_user(child,
827 task_user_regset_view(current),
829 0, sizeof(struct user_i387_struct),
833 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
834 return copy_regset_to_user(child, &user_x86_32_view,
836 0, sizeof(struct user_fxsr_struct),
839 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
840 return copy_regset_from_user(child, &user_x86_32_view,
842 0, sizeof(struct user_fxsr_struct),
846 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
847 case PTRACE_GET_THREAD_AREA:
850 ret = do_get_thread_area(child, addr,
851 (struct user_desc __user *)data);
854 case PTRACE_SET_THREAD_AREA:
857 ret = do_set_thread_area(child, addr,
858 (struct user_desc __user *)data, 0);
863 /* normal 64bit interface to access TLS data.
864 Works just like arch_prctl, except that the arguments
866 case PTRACE_ARCH_PRCTL:
867 ret = do_arch_prctl_64(child, data, addr);
872 ret = ptrace_request(child, request, addr, data);
879 #ifdef CONFIG_IA32_EMULATION
881 #include <linux/compat.h>
882 #include <linux/syscalls.h>
883 #include <asm/ia32.h>
884 #include <asm/user32.h>
887 case offsetof(struct user32, regs.l): \
888 regs->q = value; break
891 case offsetof(struct user32, regs.rs): \
892 return set_segment_reg(child, \
893 offsetof(struct user_regs_struct, rs), \
897 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
899 struct pt_regs *regs = task_pt_regs(child);
920 case offsetof(struct user32, regs.orig_eax):
922 * Warning: bizarre corner case fixup here. A 32-bit
923 * debugger setting orig_eax to -1 wants to disable
924 * syscall restart. Make sure that the syscall
925 * restart code sign-extends orig_ax. Also make sure
926 * we interpret the -ERESTART* codes correctly if
927 * loaded into regs->ax in case the task is not
928 * actually still sitting at the exit from a 32-bit
929 * syscall with TS_COMPAT still set.
931 regs->orig_ax = value;
932 if (syscall_get_nr(child, regs) >= 0)
933 child->thread_info.status |= TS_I386_REGS_POKED;
936 case offsetof(struct user32, regs.eflags):
937 return set_flags(child, value);
939 case offsetof(struct user32, u_debugreg[0]) ...
940 offsetof(struct user32, u_debugreg[7]):
941 regno -= offsetof(struct user32, u_debugreg[0]);
942 return ptrace_set_debugreg(child, regno / 4, value);
945 if (regno > sizeof(struct user32) || (regno & 3))
949 * Other dummy fields in the virtual user structure
961 case offsetof(struct user32, regs.l): \
962 *val = regs->q; break
965 case offsetof(struct user32, regs.rs): \
966 *val = get_segment_reg(child, \
967 offsetof(struct user_regs_struct, rs)); \
970 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
972 struct pt_regs *regs = task_pt_regs(child);
990 R32(orig_eax, orig_ax);
994 case offsetof(struct user32, regs.eflags):
995 *val = get_flags(child);
998 case offsetof(struct user32, u_debugreg[0]) ...
999 offsetof(struct user32, u_debugreg[7]):
1000 regno -= offsetof(struct user32, u_debugreg[0]);
1001 *val = ptrace_get_debugreg(child, regno / 4);
1005 if (regno > sizeof(struct user32) || (regno & 3))
1009 * Other dummy fields in the virtual user structure
1021 static int genregs32_get(struct task_struct *target,
1022 const struct user_regset *regset,
1023 unsigned int pos, unsigned int count,
1024 void *kbuf, void __user *ubuf)
1027 compat_ulong_t *k = kbuf;
1028 while (count >= sizeof(*k)) {
1029 getreg32(target, pos, k++);
1030 count -= sizeof(*k);
1034 compat_ulong_t __user *u = ubuf;
1035 while (count >= sizeof(*u)) {
1036 compat_ulong_t word;
1037 getreg32(target, pos, &word);
1038 if (__put_user(word, u++))
1040 count -= sizeof(*u);
1048 static int genregs32_set(struct task_struct *target,
1049 const struct user_regset *regset,
1050 unsigned int pos, unsigned int count,
1051 const void *kbuf, const void __user *ubuf)
1055 const compat_ulong_t *k = kbuf;
1056 while (count >= sizeof(*k) && !ret) {
1057 ret = putreg32(target, pos, *k++);
1058 count -= sizeof(*k);
1062 const compat_ulong_t __user *u = ubuf;
1063 while (count >= sizeof(*u) && !ret) {
1064 compat_ulong_t word;
1065 ret = __get_user(word, u++);
1068 ret = putreg32(target, pos, word);
1069 count -= sizeof(*u);
1076 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1077 compat_ulong_t caddr, compat_ulong_t cdata)
1079 unsigned long addr = caddr;
1080 unsigned long data = cdata;
1081 void __user *datap = compat_ptr(data);
1086 case PTRACE_PEEKUSR:
1087 ret = getreg32(child, addr, &val);
1089 ret = put_user(val, (__u32 __user *)datap);
1092 case PTRACE_POKEUSR:
1093 ret = putreg32(child, addr, data);
1096 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1097 return copy_regset_to_user(child, &user_x86_32_view,
1099 0, sizeof(struct user_regs_struct32),
1102 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1103 return copy_regset_from_user(child, &user_x86_32_view,
1105 sizeof(struct user_regs_struct32),
1108 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1109 return copy_regset_to_user(child, &user_x86_32_view,
1111 sizeof(struct user_i387_ia32_struct),
1114 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1115 return copy_regset_from_user(
1116 child, &user_x86_32_view, REGSET_FP,
1117 0, sizeof(struct user_i387_ia32_struct), datap);
1119 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1120 return copy_regset_to_user(child, &user_x86_32_view,
1122 sizeof(struct user32_fxsr_struct),
1125 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1126 return copy_regset_from_user(child, &user_x86_32_view,
1128 sizeof(struct user32_fxsr_struct),
1131 case PTRACE_GET_THREAD_AREA:
1132 case PTRACE_SET_THREAD_AREA:
1133 return arch_ptrace(child, request, addr, data);
1136 return compat_ptrace_request(child, request, addr, data);
1141 #endif /* CONFIG_IA32_EMULATION */
1143 #ifdef CONFIG_X86_X32_ABI
1144 static long x32_arch_ptrace(struct task_struct *child,
1145 compat_long_t request, compat_ulong_t caddr,
1146 compat_ulong_t cdata)
1148 unsigned long addr = caddr;
1149 unsigned long data = cdata;
1150 void __user *datap = compat_ptr(data);
1154 /* Read 32bits at location addr in the USER area. Only allow
1155 to return the lower 32bits of segment and debug registers. */
1156 case PTRACE_PEEKUSR: {
1160 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1161 addr < offsetof(struct user_regs_struct, cs))
1164 tmp = 0; /* Default return condition */
1165 if (addr < sizeof(struct user_regs_struct))
1166 tmp = getreg(child, addr);
1167 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1168 addr <= offsetof(struct user, u_debugreg[7])) {
1169 addr -= offsetof(struct user, u_debugreg[0]);
1170 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1172 ret = put_user(tmp, (__u32 __user *)datap);
1176 /* Write the word at location addr in the USER area. Only allow
1177 to update segment and debug registers with the upper 32bits
1179 case PTRACE_POKEUSR:
1181 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1182 addr < offsetof(struct user_regs_struct, cs))
1185 if (addr < sizeof(struct user_regs_struct))
1186 ret = putreg(child, addr, data);
1187 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1188 addr <= offsetof(struct user, u_debugreg[7])) {
1189 addr -= offsetof(struct user, u_debugreg[0]);
1190 ret = ptrace_set_debugreg(child,
1191 addr / sizeof(data), data);
1195 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1196 return copy_regset_to_user(child,
1197 task_user_regset_view(current),
1199 0, sizeof(struct user_regs_struct),
1202 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1203 return copy_regset_from_user(child,
1204 task_user_regset_view(current),
1206 0, sizeof(struct user_regs_struct),
1209 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1210 return copy_regset_to_user(child,
1211 task_user_regset_view(current),
1213 0, sizeof(struct user_i387_struct),
1216 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1217 return copy_regset_from_user(child,
1218 task_user_regset_view(current),
1220 0, sizeof(struct user_i387_struct),
1224 return compat_ptrace_request(child, request, addr, data);
1231 #ifdef CONFIG_COMPAT
1232 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1233 compat_ulong_t caddr, compat_ulong_t cdata)
1235 #ifdef CONFIG_X86_X32_ABI
1236 if (!in_ia32_syscall())
1237 return x32_arch_ptrace(child, request, caddr, cdata);
1239 #ifdef CONFIG_IA32_EMULATION
1240 return ia32_arch_ptrace(child, request, caddr, cdata);
1245 #endif /* CONFIG_COMPAT */
1247 #ifdef CONFIG_X86_64
1249 static struct user_regset x86_64_regsets[] __ro_after_init = {
1250 [REGSET_GENERAL] = {
1251 .core_note_type = NT_PRSTATUS,
1252 .n = sizeof(struct user_regs_struct) / sizeof(long),
1253 .size = sizeof(long), .align = sizeof(long),
1254 .get = genregs_get, .set = genregs_set
1257 .core_note_type = NT_PRFPREG,
1258 .n = sizeof(struct user_i387_struct) / sizeof(long),
1259 .size = sizeof(long), .align = sizeof(long),
1260 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1263 .core_note_type = NT_X86_XSTATE,
1264 .size = sizeof(u64), .align = sizeof(u64),
1265 .active = xstateregs_active, .get = xstateregs_get,
1266 .set = xstateregs_set
1268 [REGSET_IOPERM64] = {
1269 .core_note_type = NT_386_IOPERM,
1270 .n = IO_BITMAP_LONGS,
1271 .size = sizeof(long), .align = sizeof(long),
1272 .active = ioperm_active, .get = ioperm_get
1276 static const struct user_regset_view user_x86_64_view = {
1277 .name = "x86_64", .e_machine = EM_X86_64,
1278 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1281 #else /* CONFIG_X86_32 */
1283 #define user_regs_struct32 user_regs_struct
1284 #define genregs32_get genregs_get
1285 #define genregs32_set genregs_set
1287 #endif /* CONFIG_X86_64 */
1289 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1290 static struct user_regset x86_32_regsets[] __ro_after_init = {
1291 [REGSET_GENERAL] = {
1292 .core_note_type = NT_PRSTATUS,
1293 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1294 .size = sizeof(u32), .align = sizeof(u32),
1295 .get = genregs32_get, .set = genregs32_set
1298 .core_note_type = NT_PRFPREG,
1299 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1300 .size = sizeof(u32), .align = sizeof(u32),
1301 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1304 .core_note_type = NT_PRXFPREG,
1305 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1306 .size = sizeof(u32), .align = sizeof(u32),
1307 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1310 .core_note_type = NT_X86_XSTATE,
1311 .size = sizeof(u64), .align = sizeof(u64),
1312 .active = xstateregs_active, .get = xstateregs_get,
1313 .set = xstateregs_set
1316 .core_note_type = NT_386_TLS,
1317 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1318 .size = sizeof(struct user_desc),
1319 .align = sizeof(struct user_desc),
1320 .active = regset_tls_active,
1321 .get = regset_tls_get, .set = regset_tls_set
1323 [REGSET_IOPERM32] = {
1324 .core_note_type = NT_386_IOPERM,
1325 .n = IO_BITMAP_BYTES / sizeof(u32),
1326 .size = sizeof(u32), .align = sizeof(u32),
1327 .active = ioperm_active, .get = ioperm_get
1331 static const struct user_regset_view user_x86_32_view = {
1332 .name = "i386", .e_machine = EM_386,
1333 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1338 * This represents bytes 464..511 in the memory layout exported through
1339 * the REGSET_XSTATE interface.
1341 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1343 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1345 #ifdef CONFIG_X86_64
1346 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1348 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1349 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1351 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1354 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1356 #ifdef CONFIG_IA32_EMULATION
1357 if (!user_64bit_mode(task_pt_regs(task)))
1359 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1360 return &user_x86_32_view;
1362 #ifdef CONFIG_X86_64
1363 return &user_x86_64_view;
1367 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1368 int error_code, int si_code)
1370 tsk->thread.trap_nr = X86_TRAP_DB;
1371 tsk->thread.error_code = error_code;
1373 /* Send us the fake SIGTRAP */
1374 force_sig_fault(SIGTRAP, si_code,
1375 user_mode(regs) ? (void __user *)regs->ip : NULL, tsk);
1378 void user_single_step_report(struct pt_regs *regs)
1380 send_sigtrap(current, regs, 0, TRAP_BRKPT);