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>
10 #include <linux/smp.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/ptrace.h>
14 #include <linux/tracehook.h>
15 #include <linux/user.h>
16 #include <linux/elf.h>
17 #include <linux/security.h>
18 #include <linux/audit.h>
19 #include <linux/seccomp.h>
20 #include <linux/signal.h>
21 #include <linux/perf_event.h>
22 #include <linux/hw_breakpoint.h>
23 #include <linux/rcupdate.h>
24 #include <linux/export.h>
25 #include <linux/context_tracking.h>
26 #include <linux/nospec.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/processor.h>
31 #include <asm/fpu/internal.h>
32 #include <asm/fpu/signal.h>
33 #include <asm/fpu/regset.h>
34 #include <asm/debugreg.h>
37 #include <asm/prctl.h>
38 #include <asm/proto.h>
39 #include <asm/hw_breakpoint.h>
40 #include <asm/traps.h>
41 #include <asm/syscall.h>
49 REGSET_IOPERM64 = REGSET_XFP,
55 struct pt_regs_offset {
60 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61 #define REG_OFFSET_END {.name = NULL, .offset = 0}
63 static const struct pt_regs_offset regoffset_table[] = {
87 REG_OFFSET_NAME(orig_ax),
90 REG_OFFSET_NAME(flags),
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
103 int regs_query_register_offset(const char *name)
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
119 const char *regs_query_register_name(unsigned int offset)
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
129 * does not yet catch signals sent when the child dies.
130 * in exit.c or in signal.c.
134 * Determines which flags the user has access to [1 = access, 0 = no access].
136 #define FLAG_MASK_32 ((unsigned long) \
137 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
138 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
139 X86_EFLAGS_SF | X86_EFLAGS_TF | \
140 X86_EFLAGS_DF | X86_EFLAGS_OF | \
141 X86_EFLAGS_RF | X86_EFLAGS_AC))
144 * Determines whether a value may be installed in a segment register.
146 static inline bool invalid_selector(u16 value)
148 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
153 #define FLAG_MASK FLAG_MASK_32
156 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
157 * when it traps. The previous stack will be directly underneath the saved
158 * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'.
160 * Now, if the stack is empty, '®s->sp' is out of range. In this
161 * case we try to take the previous stack. To always return a non-null
162 * stack pointer we fall back to regs as stack if no previous stack
165 * This is valid only for kernel mode traps.
167 unsigned long kernel_stack_pointer(struct pt_regs *regs)
169 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
170 unsigned long sp = (unsigned long)®s->sp;
173 if (context == (sp & ~(THREAD_SIZE - 1)))
176 prev_esp = (u32 *)(context);
178 return (unsigned long)*prev_esp;
180 return (unsigned long)regs;
182 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
184 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
186 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
187 return ®s->bx + (regno >> 2);
190 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
193 * Returning the value truncates it to 16 bits.
196 if (offset != offsetof(struct user_regs_struct, gs))
197 retval = *pt_regs_access(task_pt_regs(task), offset);
200 retval = get_user_gs(task_pt_regs(task));
202 retval = task_user_gs(task);
207 static int set_segment_reg(struct task_struct *task,
208 unsigned long offset, u16 value)
211 * The value argument was already truncated to 16 bits.
213 if (invalid_selector(value))
217 * For %cs and %ss we cannot permit a null selector.
218 * We can permit a bogus selector as long as it has USER_RPL.
219 * Null selectors are fine for other segment registers, but
220 * we will never get back to user mode with invalid %cs or %ss
221 * and will take the trap in iret instead. Much code relies
222 * on user_mode() to distinguish a user trap frame (which can
223 * safely use invalid selectors) from a kernel trap frame.
226 case offsetof(struct user_regs_struct, cs):
227 case offsetof(struct user_regs_struct, ss):
228 if (unlikely(value == 0))
232 *pt_regs_access(task_pt_regs(task), offset) = value;
235 case offsetof(struct user_regs_struct, gs):
237 set_user_gs(task_pt_regs(task), value);
239 task_user_gs(task) = value;
245 #else /* CONFIG_X86_64 */
247 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
249 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
251 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
252 return ®s->r15 + (offset / sizeof(regs->r15));
255 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
258 * Returning the value truncates it to 16 bits.
263 case offsetof(struct user_regs_struct, fs):
264 if (task == current) {
265 /* Older gas can't assemble movq %?s,%r?? */
266 asm("movl %%fs,%0" : "=r" (seg));
269 return task->thread.fsindex;
270 case offsetof(struct user_regs_struct, gs):
271 if (task == current) {
272 asm("movl %%gs,%0" : "=r" (seg));
275 return task->thread.gsindex;
276 case offsetof(struct user_regs_struct, ds):
277 if (task == current) {
278 asm("movl %%ds,%0" : "=r" (seg));
281 return task->thread.ds;
282 case offsetof(struct user_regs_struct, es):
283 if (task == current) {
284 asm("movl %%es,%0" : "=r" (seg));
287 return task->thread.es;
289 case offsetof(struct user_regs_struct, cs):
290 case offsetof(struct user_regs_struct, ss):
293 return *pt_regs_access(task_pt_regs(task), offset);
296 static int set_segment_reg(struct task_struct *task,
297 unsigned long offset, u16 value)
300 * The value argument was already truncated to 16 bits.
302 if (invalid_selector(value))
306 case offsetof(struct user_regs_struct,fs):
307 task->thread.fsindex = value;
309 loadsegment(fs, task->thread.fsindex);
311 case offsetof(struct user_regs_struct,gs):
312 task->thread.gsindex = value;
314 load_gs_index(task->thread.gsindex);
316 case offsetof(struct user_regs_struct,ds):
317 task->thread.ds = value;
319 loadsegment(ds, task->thread.ds);
321 case offsetof(struct user_regs_struct,es):
322 task->thread.es = value;
324 loadsegment(es, task->thread.es);
328 * Can't actually change these in 64-bit mode.
330 case offsetof(struct user_regs_struct,cs):
331 if (unlikely(value == 0))
333 task_pt_regs(task)->cs = value;
335 case offsetof(struct user_regs_struct,ss):
336 if (unlikely(value == 0))
338 task_pt_regs(task)->ss = value;
345 #endif /* CONFIG_X86_32 */
347 static unsigned long get_flags(struct task_struct *task)
349 unsigned long retval = task_pt_regs(task)->flags;
352 * If the debugger set TF, hide it from the readout.
354 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
355 retval &= ~X86_EFLAGS_TF;
360 static int set_flags(struct task_struct *task, unsigned long value)
362 struct pt_regs *regs = task_pt_regs(task);
365 * If the user value contains TF, mark that
366 * it was not "us" (the debugger) that set it.
367 * If not, make sure it stays set if we had.
369 if (value & X86_EFLAGS_TF)
370 clear_tsk_thread_flag(task, TIF_FORCED_TF);
371 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
372 value |= X86_EFLAGS_TF;
374 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
379 static int putreg(struct task_struct *child,
380 unsigned long offset, unsigned long value)
383 case offsetof(struct user_regs_struct, cs):
384 case offsetof(struct user_regs_struct, ds):
385 case offsetof(struct user_regs_struct, es):
386 case offsetof(struct user_regs_struct, fs):
387 case offsetof(struct user_regs_struct, gs):
388 case offsetof(struct user_regs_struct, ss):
389 return set_segment_reg(child, offset, value);
391 case offsetof(struct user_regs_struct, flags):
392 return set_flags(child, value);
395 case offsetof(struct user_regs_struct,fs_base):
396 if (value >= TASK_SIZE_MAX)
399 * When changing the segment base, use do_arch_prctl
400 * to set either thread.fs or thread.fsindex and the
401 * corresponding GDT slot.
403 if (child->thread.fsbase != value)
404 return do_arch_prctl(child, ARCH_SET_FS, value);
406 case offsetof(struct user_regs_struct,gs_base):
408 * Exactly the same here as the %fs handling above.
410 if (value >= TASK_SIZE_MAX)
412 if (child->thread.gsbase != value)
413 return do_arch_prctl(child, ARCH_SET_GS, value);
418 *pt_regs_access(task_pt_regs(child), offset) = value;
422 static unsigned long getreg(struct task_struct *task, unsigned long offset)
425 case offsetof(struct user_regs_struct, cs):
426 case offsetof(struct user_regs_struct, ds):
427 case offsetof(struct user_regs_struct, es):
428 case offsetof(struct user_regs_struct, fs):
429 case offsetof(struct user_regs_struct, gs):
430 case offsetof(struct user_regs_struct, ss):
431 return get_segment_reg(task, offset);
433 case offsetof(struct user_regs_struct, flags):
434 return get_flags(task);
437 case offsetof(struct user_regs_struct, fs_base): {
439 * XXX: This will not behave as expected if called on
440 * current or if fsindex != 0.
442 return task->thread.fsbase;
444 case offsetof(struct user_regs_struct, gs_base): {
446 * XXX: This will not behave as expected if called on
447 * current or if fsindex != 0.
449 return task->thread.gsbase;
454 return *pt_regs_access(task_pt_regs(task), offset);
457 static int genregs_get(struct task_struct *target,
458 const struct user_regset *regset,
459 unsigned int pos, unsigned int count,
460 void *kbuf, void __user *ubuf)
463 unsigned long *k = kbuf;
464 while (count >= sizeof(*k)) {
465 *k++ = getreg(target, pos);
470 unsigned long __user *u = ubuf;
471 while (count >= sizeof(*u)) {
472 if (__put_user(getreg(target, pos), u++))
482 static int genregs_set(struct task_struct *target,
483 const struct user_regset *regset,
484 unsigned int pos, unsigned int count,
485 const void *kbuf, const void __user *ubuf)
489 const unsigned long *k = kbuf;
490 while (count >= sizeof(*k) && !ret) {
491 ret = putreg(target, pos, *k++);
496 const unsigned long __user *u = ubuf;
497 while (count >= sizeof(*u) && !ret) {
499 ret = __get_user(word, u++);
502 ret = putreg(target, pos, word);
510 static void ptrace_triggered(struct perf_event *bp,
511 struct perf_sample_data *data,
512 struct pt_regs *regs)
515 struct thread_struct *thread = &(current->thread);
518 * Store in the virtual DR6 register the fact that the breakpoint
519 * was hit so the thread's debugger will see it.
521 for (i = 0; i < HBP_NUM; i++) {
522 if (thread->ptrace_bps[i] == bp)
526 thread->debugreg6 |= (DR_TRAP0 << i);
530 * Walk through every ptrace breakpoints for this thread and
531 * build the dr7 value on top of their attributes.
534 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
538 struct arch_hw_breakpoint *info;
540 for (i = 0; i < HBP_NUM; i++) {
541 if (bp[i] && !bp[i]->attr.disabled) {
542 info = counter_arch_bp(bp[i]);
543 dr7 |= encode_dr7(i, info->len, info->type);
550 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
551 int len, int type, bool disabled)
553 int err, bp_len, bp_type;
555 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
557 attr->bp_len = bp_len;
558 attr->bp_type = bp_type;
559 attr->disabled = disabled;
565 static struct perf_event *
566 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
567 unsigned long addr, bool disabled)
569 struct perf_event_attr attr;
572 ptrace_breakpoint_init(&attr);
575 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
579 return register_user_hw_breakpoint(&attr, ptrace_triggered,
583 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
586 struct perf_event_attr attr = bp->attr;
589 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
593 return modify_user_hw_breakpoint(bp, &attr);
597 * Handle ptrace writes to debug register 7.
599 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
601 struct thread_struct *thread = &tsk->thread;
602 unsigned long old_dr7;
603 bool second_pass = false;
606 data &= ~DR_CONTROL_RESERVED;
607 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
611 for (i = 0; i < HBP_NUM; i++) {
613 bool disabled = !decode_dr7(data, i, &len, &type);
614 struct perf_event *bp = thread->ptrace_bps[i];
620 bp = ptrace_register_breakpoint(tsk,
621 len, type, 0, disabled);
627 thread->ptrace_bps[i] = bp;
631 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
636 /* Restore if the first pass failed, second_pass shouldn't fail. */
637 if (rc && !WARN_ON(second_pass)) {
648 * Handle PTRACE_PEEKUSR calls for the debug register area.
650 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
652 struct thread_struct *thread = &tsk->thread;
653 unsigned long val = 0;
656 int index = array_index_nospec(n, HBP_NUM);
657 struct perf_event *bp = thread->ptrace_bps[index];
660 val = bp->hw.info.address;
662 val = thread->debugreg6;
664 val = thread->ptrace_dr7;
669 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
672 struct thread_struct *t = &tsk->thread;
673 struct perf_event *bp = t->ptrace_bps[nr];
678 * Put stub len and type to create an inactive but correct bp.
680 * CHECKME: the previous code returned -EIO if the addr wasn't
681 * a valid task virtual addr. The new one will return -EINVAL in
683 * -EINVAL may be what we want for in-kernel breakpoints users,
684 * but -EIO looks better for ptrace, since we refuse a register
685 * writing for the user. And anyway this is the previous
688 bp = ptrace_register_breakpoint(tsk,
689 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
694 t->ptrace_bps[nr] = bp;
696 struct perf_event_attr attr = bp->attr;
699 err = modify_user_hw_breakpoint(bp, &attr);
706 * Handle PTRACE_POKEUSR calls for the debug register area.
708 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
711 struct thread_struct *thread = &tsk->thread;
712 /* There are no DR4 or DR5 registers */
716 rc = ptrace_set_breakpoint_addr(tsk, n, val);
718 thread->debugreg6 = val;
721 rc = ptrace_write_dr7(tsk, val);
723 thread->ptrace_dr7 = val;
729 * These access the current or another (stopped) task's io permission
730 * bitmap for debugging or core dump.
732 static int ioperm_active(struct task_struct *target,
733 const struct user_regset *regset)
735 return target->thread.io_bitmap_max / regset->size;
738 static int ioperm_get(struct task_struct *target,
739 const struct user_regset *regset,
740 unsigned int pos, unsigned int count,
741 void *kbuf, void __user *ubuf)
743 if (!target->thread.io_bitmap_ptr)
746 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
747 target->thread.io_bitmap_ptr,
752 * Called by kernel/ptrace.c when detaching..
754 * Make sure the single step bit is not set.
756 void ptrace_disable(struct task_struct *child)
758 user_disable_single_step(child);
759 #ifdef TIF_SYSCALL_EMU
760 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
764 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
765 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
768 long arch_ptrace(struct task_struct *child, long request,
769 unsigned long addr, unsigned long data)
772 unsigned long __user *datap = (unsigned long __user *)data;
775 /* read the word at location addr in the USER area. */
776 case PTRACE_PEEKUSR: {
780 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
783 tmp = 0; /* Default return condition */
784 if (addr < sizeof(struct user_regs_struct))
785 tmp = getreg(child, addr);
786 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
787 addr <= offsetof(struct user, u_debugreg[7])) {
788 addr -= offsetof(struct user, u_debugreg[0]);
789 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
791 ret = put_user(tmp, datap);
795 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
797 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
800 if (addr < sizeof(struct user_regs_struct))
801 ret = putreg(child, addr, data);
802 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
803 addr <= offsetof(struct user, u_debugreg[7])) {
804 addr -= offsetof(struct user, u_debugreg[0]);
805 ret = ptrace_set_debugreg(child,
806 addr / sizeof(data), data);
810 case PTRACE_GETREGS: /* Get all gp regs from the child. */
811 return copy_regset_to_user(child,
812 task_user_regset_view(current),
814 0, sizeof(struct user_regs_struct),
817 case PTRACE_SETREGS: /* Set all gp regs in the child. */
818 return copy_regset_from_user(child,
819 task_user_regset_view(current),
821 0, sizeof(struct user_regs_struct),
824 case PTRACE_GETFPREGS: /* Get the child FPU state. */
825 return copy_regset_to_user(child,
826 task_user_regset_view(current),
828 0, sizeof(struct user_i387_struct),
831 case PTRACE_SETFPREGS: /* Set the child FPU state. */
832 return copy_regset_from_user(child,
833 task_user_regset_view(current),
835 0, sizeof(struct user_i387_struct),
839 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
840 return copy_regset_to_user(child, &user_x86_32_view,
842 0, sizeof(struct user_fxsr_struct),
845 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
846 return copy_regset_from_user(child, &user_x86_32_view,
848 0, sizeof(struct user_fxsr_struct),
852 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
853 case PTRACE_GET_THREAD_AREA:
856 ret = do_get_thread_area(child, addr,
857 (struct user_desc __user *)data);
860 case PTRACE_SET_THREAD_AREA:
863 ret = do_set_thread_area(child, addr,
864 (struct user_desc __user *)data, 0);
869 /* normal 64bit interface to access TLS data.
870 Works just like arch_prctl, except that the arguments
872 case PTRACE_ARCH_PRCTL:
873 ret = do_arch_prctl(child, data, addr);
878 ret = ptrace_request(child, request, addr, data);
885 #ifdef CONFIG_IA32_EMULATION
887 #include <linux/compat.h>
888 #include <linux/syscalls.h>
889 #include <asm/ia32.h>
890 #include <asm/user32.h>
893 case offsetof(struct user32, regs.l): \
894 regs->q = value; break
897 case offsetof(struct user32, regs.rs): \
898 return set_segment_reg(child, \
899 offsetof(struct user_regs_struct, rs), \
903 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
905 struct pt_regs *regs = task_pt_regs(child);
926 case offsetof(struct user32, regs.orig_eax):
928 * Warning: bizarre corner case fixup here. A 32-bit
929 * debugger setting orig_eax to -1 wants to disable
930 * syscall restart. Make sure that the syscall
931 * restart code sign-extends orig_ax. Also make sure
932 * we interpret the -ERESTART* codes correctly if
933 * loaded into regs->ax in case the task is not
934 * actually still sitting at the exit from a 32-bit
935 * syscall with TS_COMPAT still set.
937 regs->orig_ax = value;
938 if (syscall_get_nr(child, regs) >= 0)
939 child->thread_info.status |= TS_I386_REGS_POKED;
942 case offsetof(struct user32, regs.eflags):
943 return set_flags(child, value);
945 case offsetof(struct user32, u_debugreg[0]) ...
946 offsetof(struct user32, u_debugreg[7]):
947 regno -= offsetof(struct user32, u_debugreg[0]);
948 return ptrace_set_debugreg(child, regno / 4, value);
951 if (regno > sizeof(struct user32) || (regno & 3))
955 * Other dummy fields in the virtual user structure
967 case offsetof(struct user32, regs.l): \
968 *val = regs->q; break
971 case offsetof(struct user32, regs.rs): \
972 *val = get_segment_reg(child, \
973 offsetof(struct user_regs_struct, rs)); \
976 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
978 struct pt_regs *regs = task_pt_regs(child);
996 R32(orig_eax, orig_ax);
1000 case offsetof(struct user32, regs.eflags):
1001 *val = get_flags(child);
1004 case offsetof(struct user32, u_debugreg[0]) ...
1005 offsetof(struct user32, u_debugreg[7]):
1006 regno -= offsetof(struct user32, u_debugreg[0]);
1007 *val = ptrace_get_debugreg(child, regno / 4);
1011 if (regno > sizeof(struct user32) || (regno & 3))
1015 * Other dummy fields in the virtual user structure
1027 static int genregs32_get(struct task_struct *target,
1028 const struct user_regset *regset,
1029 unsigned int pos, unsigned int count,
1030 void *kbuf, void __user *ubuf)
1033 compat_ulong_t *k = kbuf;
1034 while (count >= sizeof(*k)) {
1035 getreg32(target, pos, k++);
1036 count -= sizeof(*k);
1040 compat_ulong_t __user *u = ubuf;
1041 while (count >= sizeof(*u)) {
1042 compat_ulong_t word;
1043 getreg32(target, pos, &word);
1044 if (__put_user(word, u++))
1046 count -= sizeof(*u);
1054 static int genregs32_set(struct task_struct *target,
1055 const struct user_regset *regset,
1056 unsigned int pos, unsigned int count,
1057 const void *kbuf, const void __user *ubuf)
1061 const compat_ulong_t *k = kbuf;
1062 while (count >= sizeof(*k) && !ret) {
1063 ret = putreg32(target, pos, *k++);
1064 count -= sizeof(*k);
1068 const compat_ulong_t __user *u = ubuf;
1069 while (count >= sizeof(*u) && !ret) {
1070 compat_ulong_t word;
1071 ret = __get_user(word, u++);
1074 ret = putreg32(target, pos, word);
1075 count -= sizeof(*u);
1082 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1083 compat_ulong_t caddr, compat_ulong_t cdata)
1085 unsigned long addr = caddr;
1086 unsigned long data = cdata;
1087 void __user *datap = compat_ptr(data);
1092 case PTRACE_PEEKUSR:
1093 ret = getreg32(child, addr, &val);
1095 ret = put_user(val, (__u32 __user *)datap);
1098 case PTRACE_POKEUSR:
1099 ret = putreg32(child, addr, data);
1102 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1103 return copy_regset_to_user(child, &user_x86_32_view,
1105 0, sizeof(struct user_regs_struct32),
1108 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1109 return copy_regset_from_user(child, &user_x86_32_view,
1111 sizeof(struct user_regs_struct32),
1114 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1115 return copy_regset_to_user(child, &user_x86_32_view,
1117 sizeof(struct user_i387_ia32_struct),
1120 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1121 return copy_regset_from_user(
1122 child, &user_x86_32_view, REGSET_FP,
1123 0, sizeof(struct user_i387_ia32_struct), datap);
1125 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1126 return copy_regset_to_user(child, &user_x86_32_view,
1128 sizeof(struct user32_fxsr_struct),
1131 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1132 return copy_regset_from_user(child, &user_x86_32_view,
1134 sizeof(struct user32_fxsr_struct),
1137 case PTRACE_GET_THREAD_AREA:
1138 case PTRACE_SET_THREAD_AREA:
1139 return arch_ptrace(child, request, addr, data);
1142 return compat_ptrace_request(child, request, addr, data);
1147 #endif /* CONFIG_IA32_EMULATION */
1149 #ifdef CONFIG_X86_X32_ABI
1150 static long x32_arch_ptrace(struct task_struct *child,
1151 compat_long_t request, compat_ulong_t caddr,
1152 compat_ulong_t cdata)
1154 unsigned long addr = caddr;
1155 unsigned long data = cdata;
1156 void __user *datap = compat_ptr(data);
1160 /* Read 32bits at location addr in the USER area. Only allow
1161 to return the lower 32bits of segment and debug registers. */
1162 case PTRACE_PEEKUSR: {
1166 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1167 addr < offsetof(struct user_regs_struct, cs))
1170 tmp = 0; /* Default return condition */
1171 if (addr < sizeof(struct user_regs_struct))
1172 tmp = getreg(child, addr);
1173 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1174 addr <= offsetof(struct user, u_debugreg[7])) {
1175 addr -= offsetof(struct user, u_debugreg[0]);
1176 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1178 ret = put_user(tmp, (__u32 __user *)datap);
1182 /* Write the word at location addr in the USER area. Only allow
1183 to update segment and debug registers with the upper 32bits
1185 case PTRACE_POKEUSR:
1187 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1188 addr < offsetof(struct user_regs_struct, cs))
1191 if (addr < sizeof(struct user_regs_struct))
1192 ret = putreg(child, addr, data);
1193 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1194 addr <= offsetof(struct user, u_debugreg[7])) {
1195 addr -= offsetof(struct user, u_debugreg[0]);
1196 ret = ptrace_set_debugreg(child,
1197 addr / sizeof(data), data);
1201 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1202 return copy_regset_to_user(child,
1203 task_user_regset_view(current),
1205 0, sizeof(struct user_regs_struct),
1208 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1209 return copy_regset_from_user(child,
1210 task_user_regset_view(current),
1212 0, sizeof(struct user_regs_struct),
1215 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1216 return copy_regset_to_user(child,
1217 task_user_regset_view(current),
1219 0, sizeof(struct user_i387_struct),
1222 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1223 return copy_regset_from_user(child,
1224 task_user_regset_view(current),
1226 0, sizeof(struct user_i387_struct),
1230 return compat_ptrace_request(child, request, addr, data);
1237 #ifdef CONFIG_COMPAT
1238 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1239 compat_ulong_t caddr, compat_ulong_t cdata)
1241 #ifdef CONFIG_X86_X32_ABI
1242 if (!in_ia32_syscall())
1243 return x32_arch_ptrace(child, request, caddr, cdata);
1245 #ifdef CONFIG_IA32_EMULATION
1246 return ia32_arch_ptrace(child, request, caddr, cdata);
1251 #endif /* CONFIG_COMPAT */
1253 #ifdef CONFIG_X86_64
1255 static struct user_regset x86_64_regsets[] __ro_after_init = {
1256 [REGSET_GENERAL] = {
1257 .core_note_type = NT_PRSTATUS,
1258 .n = sizeof(struct user_regs_struct) / sizeof(long),
1259 .size = sizeof(long), .align = sizeof(long),
1260 .get = genregs_get, .set = genregs_set
1263 .core_note_type = NT_PRFPREG,
1264 .n = sizeof(struct user_i387_struct) / sizeof(long),
1265 .size = sizeof(long), .align = sizeof(long),
1266 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1269 .core_note_type = NT_X86_XSTATE,
1270 .size = sizeof(u64), .align = sizeof(u64),
1271 .active = xstateregs_active, .get = xstateregs_get,
1272 .set = xstateregs_set
1274 [REGSET_IOPERM64] = {
1275 .core_note_type = NT_386_IOPERM,
1276 .n = IO_BITMAP_LONGS,
1277 .size = sizeof(long), .align = sizeof(long),
1278 .active = ioperm_active, .get = ioperm_get
1282 static const struct user_regset_view user_x86_64_view = {
1283 .name = "x86_64", .e_machine = EM_X86_64,
1284 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1287 #else /* CONFIG_X86_32 */
1289 #define user_regs_struct32 user_regs_struct
1290 #define genregs32_get genregs_get
1291 #define genregs32_set genregs_set
1293 #endif /* CONFIG_X86_64 */
1295 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1296 static struct user_regset x86_32_regsets[] __ro_after_init = {
1297 [REGSET_GENERAL] = {
1298 .core_note_type = NT_PRSTATUS,
1299 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1300 .size = sizeof(u32), .align = sizeof(u32),
1301 .get = genregs32_get, .set = genregs32_set
1304 .core_note_type = NT_PRFPREG,
1305 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1306 .size = sizeof(u32), .align = sizeof(u32),
1307 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1310 .core_note_type = NT_PRXFPREG,
1311 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1312 .size = sizeof(u32), .align = sizeof(u32),
1313 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1316 .core_note_type = NT_X86_XSTATE,
1317 .size = sizeof(u64), .align = sizeof(u64),
1318 .active = xstateregs_active, .get = xstateregs_get,
1319 .set = xstateregs_set
1322 .core_note_type = NT_386_TLS,
1323 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1324 .size = sizeof(struct user_desc),
1325 .align = sizeof(struct user_desc),
1326 .active = regset_tls_active,
1327 .get = regset_tls_get, .set = regset_tls_set
1329 [REGSET_IOPERM32] = {
1330 .core_note_type = NT_386_IOPERM,
1331 .n = IO_BITMAP_BYTES / sizeof(u32),
1332 .size = sizeof(u32), .align = sizeof(u32),
1333 .active = ioperm_active, .get = ioperm_get
1337 static const struct user_regset_view user_x86_32_view = {
1338 .name = "i386", .e_machine = EM_386,
1339 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1344 * This represents bytes 464..511 in the memory layout exported through
1345 * the REGSET_XSTATE interface.
1347 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1349 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1351 #ifdef CONFIG_X86_64
1352 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1354 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1355 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1357 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1360 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1362 #ifdef CONFIG_IA32_EMULATION
1363 if (!user_64bit_mode(task_pt_regs(task)))
1365 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1366 return &user_x86_32_view;
1368 #ifdef CONFIG_X86_64
1369 return &user_x86_64_view;
1373 static void fill_sigtrap_info(struct task_struct *tsk,
1374 struct pt_regs *regs,
1375 int error_code, int si_code,
1376 struct siginfo *info)
1378 tsk->thread.trap_nr = X86_TRAP_DB;
1379 tsk->thread.error_code = error_code;
1381 memset(info, 0, sizeof(*info));
1382 info->si_signo = SIGTRAP;
1383 info->si_code = si_code;
1384 info->si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
1387 void user_single_step_siginfo(struct task_struct *tsk,
1388 struct pt_regs *regs,
1389 struct siginfo *info)
1391 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1394 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1395 int error_code, int si_code)
1397 struct siginfo info;
1399 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1400 /* Send us the fake SIGTRAP */
1401 force_sig_info(SIGTRAP, &info, tsk);