1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
7 #include <linux/kernel.h>
8 #include <linux/sched.h>
9 #include <linux/sched/task_stack.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/ptrace.h>
15 #include <linux/tracehook.h>
16 #include <linux/user.h>
17 #include <linux/elf.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/seccomp.h>
21 #include <linux/signal.h>
22 #include <linux/perf_event.h>
23 #include <linux/hw_breakpoint.h>
24 #include <linux/rcupdate.h>
25 #include <linux/export.h>
26 #include <linux/context_tracking.h>
28 #include <linux/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_64
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_64(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_64(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 struct perf_event *bp = thread->ptrace_bps[n];
659 val = bp->hw.info.address;
661 val = thread->debugreg6;
663 val = thread->ptrace_dr7;
668 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
671 struct thread_struct *t = &tsk->thread;
672 struct perf_event *bp = t->ptrace_bps[nr];
677 * Put stub len and type to create an inactive but correct bp.
679 * CHECKME: the previous code returned -EIO if the addr wasn't
680 * a valid task virtual addr. The new one will return -EINVAL in
682 * -EINVAL may be what we want for in-kernel breakpoints users,
683 * but -EIO looks better for ptrace, since we refuse a register
684 * writing for the user. And anyway this is the previous
687 bp = ptrace_register_breakpoint(tsk,
688 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
693 t->ptrace_bps[nr] = bp;
695 struct perf_event_attr attr = bp->attr;
698 err = modify_user_hw_breakpoint(bp, &attr);
705 * Handle PTRACE_POKEUSR calls for the debug register area.
707 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
710 struct thread_struct *thread = &tsk->thread;
711 /* There are no DR4 or DR5 registers */
715 rc = ptrace_set_breakpoint_addr(tsk, n, val);
717 thread->debugreg6 = val;
720 rc = ptrace_write_dr7(tsk, val);
722 thread->ptrace_dr7 = val;
728 * These access the current or another (stopped) task's io permission
729 * bitmap for debugging or core dump.
731 static int ioperm_active(struct task_struct *target,
732 const struct user_regset *regset)
734 return target->thread.io_bitmap_max / regset->size;
737 static int ioperm_get(struct task_struct *target,
738 const struct user_regset *regset,
739 unsigned int pos, unsigned int count,
740 void *kbuf, void __user *ubuf)
742 if (!target->thread.io_bitmap_ptr)
745 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
746 target->thread.io_bitmap_ptr,
751 * Called by kernel/ptrace.c when detaching..
753 * Make sure the single step bit is not set.
755 void ptrace_disable(struct task_struct *child)
757 user_disable_single_step(child);
758 #ifdef TIF_SYSCALL_EMU
759 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
763 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
764 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
767 long arch_ptrace(struct task_struct *child, long request,
768 unsigned long addr, unsigned long data)
771 unsigned long __user *datap = (unsigned long __user *)data;
774 /* read the word at location addr in the USER area. */
775 case PTRACE_PEEKUSR: {
779 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
782 tmp = 0; /* Default return condition */
783 if (addr < sizeof(struct user_regs_struct))
784 tmp = getreg(child, addr);
785 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
786 addr <= offsetof(struct user, u_debugreg[7])) {
787 addr -= offsetof(struct user, u_debugreg[0]);
788 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
790 ret = put_user(tmp, datap);
794 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
796 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
799 if (addr < sizeof(struct user_regs_struct))
800 ret = putreg(child, addr, data);
801 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
802 addr <= offsetof(struct user, u_debugreg[7])) {
803 addr -= offsetof(struct user, u_debugreg[0]);
804 ret = ptrace_set_debugreg(child,
805 addr / sizeof(data), data);
809 case PTRACE_GETREGS: /* Get all gp regs from the child. */
810 return copy_regset_to_user(child,
811 task_user_regset_view(current),
813 0, sizeof(struct user_regs_struct),
816 case PTRACE_SETREGS: /* Set all gp regs in the child. */
817 return copy_regset_from_user(child,
818 task_user_regset_view(current),
820 0, sizeof(struct user_regs_struct),
823 case PTRACE_GETFPREGS: /* Get the child FPU state. */
824 return copy_regset_to_user(child,
825 task_user_regset_view(current),
827 0, sizeof(struct user_i387_struct),
830 case PTRACE_SETFPREGS: /* Set the child FPU state. */
831 return copy_regset_from_user(child,
832 task_user_regset_view(current),
834 0, sizeof(struct user_i387_struct),
838 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
839 return copy_regset_to_user(child, &user_x86_32_view,
841 0, sizeof(struct user_fxsr_struct),
844 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
845 return copy_regset_from_user(child, &user_x86_32_view,
847 0, sizeof(struct user_fxsr_struct),
851 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
852 case PTRACE_GET_THREAD_AREA:
855 ret = do_get_thread_area(child, addr,
856 (struct user_desc __user *)data);
859 case PTRACE_SET_THREAD_AREA:
862 ret = do_set_thread_area(child, addr,
863 (struct user_desc __user *)data, 0);
868 /* normal 64bit interface to access TLS data.
869 Works just like arch_prctl, except that the arguments
871 case PTRACE_ARCH_PRCTL:
872 ret = do_arch_prctl_64(child, data, addr);
877 ret = ptrace_request(child, request, addr, data);
884 #ifdef CONFIG_IA32_EMULATION
886 #include <linux/compat.h>
887 #include <linux/syscalls.h>
888 #include <asm/ia32.h>
889 #include <asm/user32.h>
892 case offsetof(struct user32, regs.l): \
893 regs->q = value; break
896 case offsetof(struct user32, regs.rs): \
897 return set_segment_reg(child, \
898 offsetof(struct user_regs_struct, rs), \
902 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
904 struct pt_regs *regs = task_pt_regs(child);
925 case offsetof(struct user32, regs.orig_eax):
927 * Warning: bizarre corner case fixup here. A 32-bit
928 * debugger setting orig_eax to -1 wants to disable
929 * syscall restart. Make sure that the syscall
930 * restart code sign-extends orig_ax. Also make sure
931 * we interpret the -ERESTART* codes correctly if
932 * loaded into regs->ax in case the task is not
933 * actually still sitting at the exit from a 32-bit
934 * syscall with TS_COMPAT still set.
936 regs->orig_ax = value;
937 if (syscall_get_nr(child, regs) >= 0)
938 child->thread.status |= TS_I386_REGS_POKED;
941 case offsetof(struct user32, regs.eflags):
942 return set_flags(child, value);
944 case offsetof(struct user32, u_debugreg[0]) ...
945 offsetof(struct user32, u_debugreg[7]):
946 regno -= offsetof(struct user32, u_debugreg[0]);
947 return ptrace_set_debugreg(child, regno / 4, value);
950 if (regno > sizeof(struct user32) || (regno & 3))
954 * Other dummy fields in the virtual user structure
966 case offsetof(struct user32, regs.l): \
967 *val = regs->q; break
970 case offsetof(struct user32, regs.rs): \
971 *val = get_segment_reg(child, \
972 offsetof(struct user_regs_struct, rs)); \
975 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
977 struct pt_regs *regs = task_pt_regs(child);
995 R32(orig_eax, orig_ax);
999 case offsetof(struct user32, regs.eflags):
1000 *val = get_flags(child);
1003 case offsetof(struct user32, u_debugreg[0]) ...
1004 offsetof(struct user32, u_debugreg[7]):
1005 regno -= offsetof(struct user32, u_debugreg[0]);
1006 *val = ptrace_get_debugreg(child, regno / 4);
1010 if (regno > sizeof(struct user32) || (regno & 3))
1014 * Other dummy fields in the virtual user structure
1026 static int genregs32_get(struct task_struct *target,
1027 const struct user_regset *regset,
1028 unsigned int pos, unsigned int count,
1029 void *kbuf, void __user *ubuf)
1032 compat_ulong_t *k = kbuf;
1033 while (count >= sizeof(*k)) {
1034 getreg32(target, pos, k++);
1035 count -= sizeof(*k);
1039 compat_ulong_t __user *u = ubuf;
1040 while (count >= sizeof(*u)) {
1041 compat_ulong_t word;
1042 getreg32(target, pos, &word);
1043 if (__put_user(word, u++))
1045 count -= sizeof(*u);
1053 static int genregs32_set(struct task_struct *target,
1054 const struct user_regset *regset,
1055 unsigned int pos, unsigned int count,
1056 const void *kbuf, const void __user *ubuf)
1060 const compat_ulong_t *k = kbuf;
1061 while (count >= sizeof(*k) && !ret) {
1062 ret = putreg32(target, pos, *k++);
1063 count -= sizeof(*k);
1067 const compat_ulong_t __user *u = ubuf;
1068 while (count >= sizeof(*u) && !ret) {
1069 compat_ulong_t word;
1070 ret = __get_user(word, u++);
1073 ret = putreg32(target, pos, word);
1074 count -= sizeof(*u);
1081 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1082 compat_ulong_t caddr, compat_ulong_t cdata)
1084 unsigned long addr = caddr;
1085 unsigned long data = cdata;
1086 void __user *datap = compat_ptr(data);
1091 case PTRACE_PEEKUSR:
1092 ret = getreg32(child, addr, &val);
1094 ret = put_user(val, (__u32 __user *)datap);
1097 case PTRACE_POKEUSR:
1098 ret = putreg32(child, addr, data);
1101 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1102 return copy_regset_to_user(child, &user_x86_32_view,
1104 0, sizeof(struct user_regs_struct32),
1107 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1108 return copy_regset_from_user(child, &user_x86_32_view,
1110 sizeof(struct user_regs_struct32),
1113 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1114 return copy_regset_to_user(child, &user_x86_32_view,
1116 sizeof(struct user_i387_ia32_struct),
1119 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1120 return copy_regset_from_user(
1121 child, &user_x86_32_view, REGSET_FP,
1122 0, sizeof(struct user_i387_ia32_struct), datap);
1124 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1125 return copy_regset_to_user(child, &user_x86_32_view,
1127 sizeof(struct user32_fxsr_struct),
1130 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1131 return copy_regset_from_user(child, &user_x86_32_view,
1133 sizeof(struct user32_fxsr_struct),
1136 case PTRACE_GET_THREAD_AREA:
1137 case PTRACE_SET_THREAD_AREA:
1138 return arch_ptrace(child, request, addr, data);
1141 return compat_ptrace_request(child, request, addr, data);
1146 #endif /* CONFIG_IA32_EMULATION */
1148 #ifdef CONFIG_X86_X32_ABI
1149 static long x32_arch_ptrace(struct task_struct *child,
1150 compat_long_t request, compat_ulong_t caddr,
1151 compat_ulong_t cdata)
1153 unsigned long addr = caddr;
1154 unsigned long data = cdata;
1155 void __user *datap = compat_ptr(data);
1159 /* Read 32bits at location addr in the USER area. Only allow
1160 to return the lower 32bits of segment and debug registers. */
1161 case PTRACE_PEEKUSR: {
1165 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1166 addr < offsetof(struct user_regs_struct, cs))
1169 tmp = 0; /* Default return condition */
1170 if (addr < sizeof(struct user_regs_struct))
1171 tmp = getreg(child, addr);
1172 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1173 addr <= offsetof(struct user, u_debugreg[7])) {
1174 addr -= offsetof(struct user, u_debugreg[0]);
1175 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1177 ret = put_user(tmp, (__u32 __user *)datap);
1181 /* Write the word at location addr in the USER area. Only allow
1182 to update segment and debug registers with the upper 32bits
1184 case PTRACE_POKEUSR:
1186 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1187 addr < offsetof(struct user_regs_struct, cs))
1190 if (addr < sizeof(struct user_regs_struct))
1191 ret = putreg(child, addr, data);
1192 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1193 addr <= offsetof(struct user, u_debugreg[7])) {
1194 addr -= offsetof(struct user, u_debugreg[0]);
1195 ret = ptrace_set_debugreg(child,
1196 addr / sizeof(data), data);
1200 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1201 return copy_regset_to_user(child,
1202 task_user_regset_view(current),
1204 0, sizeof(struct user_regs_struct),
1207 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1208 return copy_regset_from_user(child,
1209 task_user_regset_view(current),
1211 0, sizeof(struct user_regs_struct),
1214 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1215 return copy_regset_to_user(child,
1216 task_user_regset_view(current),
1218 0, sizeof(struct user_i387_struct),
1221 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1222 return copy_regset_from_user(child,
1223 task_user_regset_view(current),
1225 0, sizeof(struct user_i387_struct),
1229 return compat_ptrace_request(child, request, addr, data);
1236 #ifdef CONFIG_COMPAT
1237 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1238 compat_ulong_t caddr, compat_ulong_t cdata)
1240 #ifdef CONFIG_X86_X32_ABI
1241 if (!in_ia32_syscall())
1242 return x32_arch_ptrace(child, request, caddr, cdata);
1244 #ifdef CONFIG_IA32_EMULATION
1245 return ia32_arch_ptrace(child, request, caddr, cdata);
1250 #endif /* CONFIG_COMPAT */
1252 #ifdef CONFIG_X86_64
1254 static struct user_regset x86_64_regsets[] __ro_after_init = {
1255 [REGSET_GENERAL] = {
1256 .core_note_type = NT_PRSTATUS,
1257 .n = sizeof(struct user_regs_struct) / sizeof(long),
1258 .size = sizeof(long), .align = sizeof(long),
1259 .get = genregs_get, .set = genregs_set
1262 .core_note_type = NT_PRFPREG,
1263 .n = sizeof(struct user_i387_struct) / sizeof(long),
1264 .size = sizeof(long), .align = sizeof(long),
1265 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1268 .core_note_type = NT_X86_XSTATE,
1269 .size = sizeof(u64), .align = sizeof(u64),
1270 .active = xstateregs_active, .get = xstateregs_get,
1271 .set = xstateregs_set
1273 [REGSET_IOPERM64] = {
1274 .core_note_type = NT_386_IOPERM,
1275 .n = IO_BITMAP_LONGS,
1276 .size = sizeof(long), .align = sizeof(long),
1277 .active = ioperm_active, .get = ioperm_get
1281 static const struct user_regset_view user_x86_64_view = {
1282 .name = "x86_64", .e_machine = EM_X86_64,
1283 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1286 #else /* CONFIG_X86_32 */
1288 #define user_regs_struct32 user_regs_struct
1289 #define genregs32_get genregs_get
1290 #define genregs32_set genregs_set
1292 #endif /* CONFIG_X86_64 */
1294 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1295 static struct user_regset x86_32_regsets[] __ro_after_init = {
1296 [REGSET_GENERAL] = {
1297 .core_note_type = NT_PRSTATUS,
1298 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1299 .size = sizeof(u32), .align = sizeof(u32),
1300 .get = genregs32_get, .set = genregs32_set
1303 .core_note_type = NT_PRFPREG,
1304 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1305 .size = sizeof(u32), .align = sizeof(u32),
1306 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1309 .core_note_type = NT_PRXFPREG,
1310 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1311 .size = sizeof(u32), .align = sizeof(u32),
1312 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1315 .core_note_type = NT_X86_XSTATE,
1316 .size = sizeof(u64), .align = sizeof(u64),
1317 .active = xstateregs_active, .get = xstateregs_get,
1318 .set = xstateregs_set
1321 .core_note_type = NT_386_TLS,
1322 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1323 .size = sizeof(struct user_desc),
1324 .align = sizeof(struct user_desc),
1325 .active = regset_tls_active,
1326 .get = regset_tls_get, .set = regset_tls_set
1328 [REGSET_IOPERM32] = {
1329 .core_note_type = NT_386_IOPERM,
1330 .n = IO_BITMAP_BYTES / sizeof(u32),
1331 .size = sizeof(u32), .align = sizeof(u32),
1332 .active = ioperm_active, .get = ioperm_get
1336 static const struct user_regset_view user_x86_32_view = {
1337 .name = "i386", .e_machine = EM_386,
1338 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1343 * This represents bytes 464..511 in the memory layout exported through
1344 * the REGSET_XSTATE interface.
1346 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1348 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1350 #ifdef CONFIG_X86_64
1351 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1353 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1356 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1359 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1361 #ifdef CONFIG_IA32_EMULATION
1362 if (!user_64bit_mode(task_pt_regs(task)))
1364 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1365 return &user_x86_32_view;
1367 #ifdef CONFIG_X86_64
1368 return &user_x86_64_view;
1372 static void fill_sigtrap_info(struct task_struct *tsk,
1373 struct pt_regs *regs,
1374 int error_code, int si_code,
1375 struct siginfo *info)
1377 tsk->thread.trap_nr = X86_TRAP_DB;
1378 tsk->thread.error_code = error_code;
1380 memset(info, 0, sizeof(*info));
1381 info->si_signo = SIGTRAP;
1382 info->si_code = si_code;
1383 info->si_addr = user_mode(regs) ? (void __user *)regs->ip : NULL;
1386 void user_single_step_siginfo(struct task_struct *tsk,
1387 struct pt_regs *regs,
1388 struct siginfo *info)
1390 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1393 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1394 int error_code, int si_code)
1396 struct siginfo info;
1398 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1399 /* Send us the fake SIGTRAP */
1400 force_sig_info(SIGTRAP, &info, tsk);