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>
42 #include <asm/fsgsbase.h>
50 REGSET_IOPERM64 = REGSET_XFP,
56 struct pt_regs_offset {
61 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64 static const struct pt_regs_offset regoffset_table[] = {
88 REG_OFFSET_NAME(orig_ax),
91 REG_OFFSET_NAME(flags),
98 * regs_query_register_offset() - query register offset from its name
99 * @name: the name of a register
101 * regs_query_register_offset() returns the offset of a register in struct
102 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
104 int regs_query_register_offset(const char *name)
106 const struct pt_regs_offset *roff;
107 for (roff = regoffset_table; roff->name != NULL; roff++)
108 if (!strcmp(roff->name, name))
114 * regs_query_register_name() - query register name from its offset
115 * @offset: the offset of a register in struct pt_regs.
117 * regs_query_register_name() returns the name of a register from its
118 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
120 const char *regs_query_register_name(unsigned int offset)
122 const struct pt_regs_offset *roff;
123 for (roff = regoffset_table; roff->name != NULL; roff++)
124 if (roff->offset == offset)
130 * does not yet catch signals sent when the child dies.
131 * in exit.c or in signal.c.
135 * Determines which flags the user has access to [1 = access, 0 = no access].
137 #define FLAG_MASK_32 ((unsigned long) \
138 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
139 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
140 X86_EFLAGS_SF | X86_EFLAGS_TF | \
141 X86_EFLAGS_DF | X86_EFLAGS_OF | \
142 X86_EFLAGS_RF | X86_EFLAGS_AC))
145 * Determines whether a value may be installed in a segment register.
147 static inline bool invalid_selector(u16 value)
149 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
154 #define FLAG_MASK FLAG_MASK_32
157 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
158 * when it traps. The previous stack will be directly underneath the saved
159 * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'.
161 * Now, if the stack is empty, '®s->sp' is out of range. In this
162 * case we try to take the previous stack. To always return a non-null
163 * stack pointer we fall back to regs as stack if no previous stack
166 * This is valid only for kernel mode traps.
168 unsigned long kernel_stack_pointer(struct pt_regs *regs)
170 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
171 unsigned long sp = (unsigned long)®s->sp;
174 if (context == (sp & ~(THREAD_SIZE - 1)))
177 prev_esp = (u32 *)(context);
179 return (unsigned long)*prev_esp;
181 return (unsigned long)regs;
183 EXPORT_SYMBOL_GPL(kernel_stack_pointer);
185 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
187 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
188 return ®s->bx + (regno >> 2);
191 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
194 * Returning the value truncates it to 16 bits.
197 if (offset != offsetof(struct user_regs_struct, gs))
198 retval = *pt_regs_access(task_pt_regs(task), offset);
201 retval = get_user_gs(task_pt_regs(task));
203 retval = task_user_gs(task);
208 static int set_segment_reg(struct task_struct *task,
209 unsigned long offset, u16 value)
212 * The value argument was already truncated to 16 bits.
214 if (invalid_selector(value))
218 * For %cs and %ss we cannot permit a null selector.
219 * We can permit a bogus selector as long as it has USER_RPL.
220 * Null selectors are fine for other segment registers, but
221 * we will never get back to user mode with invalid %cs or %ss
222 * and will take the trap in iret instead. Much code relies
223 * on user_mode() to distinguish a user trap frame (which can
224 * safely use invalid selectors) from a kernel trap frame.
227 case offsetof(struct user_regs_struct, cs):
228 case offsetof(struct user_regs_struct, ss):
229 if (unlikely(value == 0))
233 *pt_regs_access(task_pt_regs(task), offset) = value;
236 case offsetof(struct user_regs_struct, gs):
238 set_user_gs(task_pt_regs(task), value);
240 task_user_gs(task) = value;
246 #else /* CONFIG_X86_64 */
248 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
250 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
252 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
253 return ®s->r15 + (offset / sizeof(regs->r15));
256 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
259 * Returning the value truncates it to 16 bits.
264 case offsetof(struct user_regs_struct, fs):
265 if (task == current) {
266 /* Older gas can't assemble movq %?s,%r?? */
267 asm("movl %%fs,%0" : "=r" (seg));
270 return task->thread.fsindex;
271 case offsetof(struct user_regs_struct, gs):
272 if (task == current) {
273 asm("movl %%gs,%0" : "=r" (seg));
276 return task->thread.gsindex;
277 case offsetof(struct user_regs_struct, ds):
278 if (task == current) {
279 asm("movl %%ds,%0" : "=r" (seg));
282 return task->thread.ds;
283 case offsetof(struct user_regs_struct, es):
284 if (task == current) {
285 asm("movl %%es,%0" : "=r" (seg));
288 return task->thread.es;
290 case offsetof(struct user_regs_struct, cs):
291 case offsetof(struct user_regs_struct, ss):
294 return *pt_regs_access(task_pt_regs(task), offset);
297 static int set_segment_reg(struct task_struct *task,
298 unsigned long offset, u16 value)
301 * The value argument was already truncated to 16 bits.
303 if (invalid_selector(value))
307 case offsetof(struct user_regs_struct,fs):
308 task->thread.fsindex = value;
310 loadsegment(fs, task->thread.fsindex);
312 case offsetof(struct user_regs_struct,gs):
313 task->thread.gsindex = value;
315 load_gs_index(task->thread.gsindex);
317 case offsetof(struct user_regs_struct,ds):
318 task->thread.ds = value;
320 loadsegment(ds, task->thread.ds);
322 case offsetof(struct user_regs_struct,es):
323 task->thread.es = value;
325 loadsegment(es, task->thread.es);
329 * Can't actually change these in 64-bit mode.
331 case offsetof(struct user_regs_struct,cs):
332 if (unlikely(value == 0))
334 task_pt_regs(task)->cs = value;
336 case offsetof(struct user_regs_struct,ss):
337 if (unlikely(value == 0))
339 task_pt_regs(task)->ss = value;
346 #endif /* CONFIG_X86_32 */
348 static unsigned long get_flags(struct task_struct *task)
350 unsigned long retval = task_pt_regs(task)->flags;
353 * If the debugger set TF, hide it from the readout.
355 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
356 retval &= ~X86_EFLAGS_TF;
361 static int set_flags(struct task_struct *task, unsigned long value)
363 struct pt_regs *regs = task_pt_regs(task);
366 * If the user value contains TF, mark that
367 * it was not "us" (the debugger) that set it.
368 * If not, make sure it stays set if we had.
370 if (value & X86_EFLAGS_TF)
371 clear_tsk_thread_flag(task, TIF_FORCED_TF);
372 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
373 value |= X86_EFLAGS_TF;
375 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
380 static int putreg(struct task_struct *child,
381 unsigned long offset, unsigned long value)
384 case offsetof(struct user_regs_struct, cs):
385 case offsetof(struct user_regs_struct, ds):
386 case offsetof(struct user_regs_struct, es):
387 case offsetof(struct user_regs_struct, fs):
388 case offsetof(struct user_regs_struct, gs):
389 case offsetof(struct user_regs_struct, ss):
390 return set_segment_reg(child, offset, value);
392 case offsetof(struct user_regs_struct, flags):
393 return set_flags(child, value);
396 case offsetof(struct user_regs_struct,fs_base):
397 if (value >= TASK_SIZE_MAX)
400 * When changing the FS base, use the same
401 * mechanism as for do_arch_prctl_64().
403 if (child->thread.fsbase != value)
404 return x86_fsbase_write_task(child, 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 x86_gsbase_write_task(child, 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):
438 return x86_fsbase_read_task(task);
439 case offsetof(struct user_regs_struct, gs_base):
440 return x86_gsbase_read_task(task);
444 return *pt_regs_access(task_pt_regs(task), offset);
447 static int genregs_get(struct task_struct *target,
448 const struct user_regset *regset,
449 unsigned int pos, unsigned int count,
450 void *kbuf, void __user *ubuf)
453 unsigned long *k = kbuf;
454 while (count >= sizeof(*k)) {
455 *k++ = getreg(target, pos);
460 unsigned long __user *u = ubuf;
461 while (count >= sizeof(*u)) {
462 if (__put_user(getreg(target, pos), u++))
472 static int genregs_set(struct task_struct *target,
473 const struct user_regset *regset,
474 unsigned int pos, unsigned int count,
475 const void *kbuf, const void __user *ubuf)
479 const unsigned long *k = kbuf;
480 while (count >= sizeof(*k) && !ret) {
481 ret = putreg(target, pos, *k++);
486 const unsigned long __user *u = ubuf;
487 while (count >= sizeof(*u) && !ret) {
489 ret = __get_user(word, u++);
492 ret = putreg(target, pos, word);
500 static void ptrace_triggered(struct perf_event *bp,
501 struct perf_sample_data *data,
502 struct pt_regs *regs)
505 struct thread_struct *thread = &(current->thread);
508 * Store in the virtual DR6 register the fact that the breakpoint
509 * was hit so the thread's debugger will see it.
511 for (i = 0; i < HBP_NUM; i++) {
512 if (thread->ptrace_bps[i] == bp)
516 thread->debugreg6 |= (DR_TRAP0 << i);
520 * Walk through every ptrace breakpoints for this thread and
521 * build the dr7 value on top of their attributes.
524 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
528 struct arch_hw_breakpoint *info;
530 for (i = 0; i < HBP_NUM; i++) {
531 if (bp[i] && !bp[i]->attr.disabled) {
532 info = counter_arch_bp(bp[i]);
533 dr7 |= encode_dr7(i, info->len, info->type);
540 static int ptrace_fill_bp_fields(struct perf_event_attr *attr,
541 int len, int type, bool disabled)
543 int err, bp_len, bp_type;
545 err = arch_bp_generic_fields(len, type, &bp_len, &bp_type);
547 attr->bp_len = bp_len;
548 attr->bp_type = bp_type;
549 attr->disabled = disabled;
555 static struct perf_event *
556 ptrace_register_breakpoint(struct task_struct *tsk, int len, int type,
557 unsigned long addr, bool disabled)
559 struct perf_event_attr attr;
562 ptrace_breakpoint_init(&attr);
565 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
569 return register_user_hw_breakpoint(&attr, ptrace_triggered,
573 static int ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
576 struct perf_event_attr attr = bp->attr;
579 err = ptrace_fill_bp_fields(&attr, len, type, disabled);
583 return modify_user_hw_breakpoint(bp, &attr);
587 * Handle ptrace writes to debug register 7.
589 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
591 struct thread_struct *thread = &tsk->thread;
592 unsigned long old_dr7;
593 bool second_pass = false;
596 data &= ~DR_CONTROL_RESERVED;
597 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
601 for (i = 0; i < HBP_NUM; i++) {
603 bool disabled = !decode_dr7(data, i, &len, &type);
604 struct perf_event *bp = thread->ptrace_bps[i];
610 bp = ptrace_register_breakpoint(tsk,
611 len, type, 0, disabled);
617 thread->ptrace_bps[i] = bp;
621 rc = ptrace_modify_breakpoint(bp, len, type, disabled);
626 /* Restore if the first pass failed, second_pass shouldn't fail. */
627 if (rc && !WARN_ON(second_pass)) {
638 * Handle PTRACE_PEEKUSR calls for the debug register area.
640 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
642 struct thread_struct *thread = &tsk->thread;
643 unsigned long val = 0;
646 struct perf_event *bp = thread->ptrace_bps[n];
649 val = bp->hw.info.address;
651 val = thread->debugreg6;
653 val = thread->ptrace_dr7;
658 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
661 struct thread_struct *t = &tsk->thread;
662 struct perf_event *bp = t->ptrace_bps[nr];
667 * Put stub len and type to create an inactive but correct bp.
669 * CHECKME: the previous code returned -EIO if the addr wasn't
670 * a valid task virtual addr. The new one will return -EINVAL in
672 * -EINVAL may be what we want for in-kernel breakpoints users,
673 * but -EIO looks better for ptrace, since we refuse a register
674 * writing for the user. And anyway this is the previous
677 bp = ptrace_register_breakpoint(tsk,
678 X86_BREAKPOINT_LEN_1, X86_BREAKPOINT_WRITE,
683 t->ptrace_bps[nr] = bp;
685 struct perf_event_attr attr = bp->attr;
688 err = modify_user_hw_breakpoint(bp, &attr);
695 * Handle PTRACE_POKEUSR calls for the debug register area.
697 static int ptrace_set_debugreg(struct task_struct *tsk, int n,
700 struct thread_struct *thread = &tsk->thread;
701 /* There are no DR4 or DR5 registers */
705 rc = ptrace_set_breakpoint_addr(tsk, n, val);
707 thread->debugreg6 = val;
710 rc = ptrace_write_dr7(tsk, val);
712 thread->ptrace_dr7 = val;
718 * These access the current or another (stopped) task's io permission
719 * bitmap for debugging or core dump.
721 static int ioperm_active(struct task_struct *target,
722 const struct user_regset *regset)
724 return target->thread.io_bitmap_max / regset->size;
727 static int ioperm_get(struct task_struct *target,
728 const struct user_regset *regset,
729 unsigned int pos, unsigned int count,
730 void *kbuf, void __user *ubuf)
732 if (!target->thread.io_bitmap_ptr)
735 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
736 target->thread.io_bitmap_ptr,
741 * Called by kernel/ptrace.c when detaching..
743 * Make sure the single step bit is not set.
745 void ptrace_disable(struct task_struct *child)
747 user_disable_single_step(child);
748 #ifdef TIF_SYSCALL_EMU
749 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
753 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
754 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
757 long arch_ptrace(struct task_struct *child, long request,
758 unsigned long addr, unsigned long data)
761 unsigned long __user *datap = (unsigned long __user *)data;
764 /* read the word at location addr in the USER area. */
765 case PTRACE_PEEKUSR: {
769 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
772 tmp = 0; /* Default return condition */
773 if (addr < sizeof(struct user_regs_struct))
774 tmp = getreg(child, addr);
775 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
776 addr <= offsetof(struct user, u_debugreg[7])) {
777 addr -= offsetof(struct user, u_debugreg[0]);
778 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
780 ret = put_user(tmp, datap);
784 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
786 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
789 if (addr < sizeof(struct user_regs_struct))
790 ret = putreg(child, addr, data);
791 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
792 addr <= offsetof(struct user, u_debugreg[7])) {
793 addr -= offsetof(struct user, u_debugreg[0]);
794 ret = ptrace_set_debugreg(child,
795 addr / sizeof(data), data);
799 case PTRACE_GETREGS: /* Get all gp regs from the child. */
800 return copy_regset_to_user(child,
801 task_user_regset_view(current),
803 0, sizeof(struct user_regs_struct),
806 case PTRACE_SETREGS: /* Set all gp regs in the child. */
807 return copy_regset_from_user(child,
808 task_user_regset_view(current),
810 0, sizeof(struct user_regs_struct),
813 case PTRACE_GETFPREGS: /* Get the child FPU state. */
814 return copy_regset_to_user(child,
815 task_user_regset_view(current),
817 0, sizeof(struct user_i387_struct),
820 case PTRACE_SETFPREGS: /* Set the child FPU state. */
821 return copy_regset_from_user(child,
822 task_user_regset_view(current),
824 0, sizeof(struct user_i387_struct),
828 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
829 return copy_regset_to_user(child, &user_x86_32_view,
831 0, sizeof(struct user_fxsr_struct),
834 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
835 return copy_regset_from_user(child, &user_x86_32_view,
837 0, sizeof(struct user_fxsr_struct),
841 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
842 case PTRACE_GET_THREAD_AREA:
845 ret = do_get_thread_area(child, addr,
846 (struct user_desc __user *)data);
849 case PTRACE_SET_THREAD_AREA:
852 ret = do_set_thread_area(child, addr,
853 (struct user_desc __user *)data, 0);
858 /* normal 64bit interface to access TLS data.
859 Works just like arch_prctl, except that the arguments
861 case PTRACE_ARCH_PRCTL:
862 ret = do_arch_prctl_64(child, data, addr);
867 ret = ptrace_request(child, request, addr, data);
874 #ifdef CONFIG_IA32_EMULATION
876 #include <linux/compat.h>
877 #include <linux/syscalls.h>
878 #include <asm/ia32.h>
879 #include <asm/user32.h>
882 case offsetof(struct user32, regs.l): \
883 regs->q = value; break
886 case offsetof(struct user32, regs.rs): \
887 return set_segment_reg(child, \
888 offsetof(struct user_regs_struct, rs), \
892 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
894 struct pt_regs *regs = task_pt_regs(child);
915 case offsetof(struct user32, regs.orig_eax):
917 * Warning: bizarre corner case fixup here. A 32-bit
918 * debugger setting orig_eax to -1 wants to disable
919 * syscall restart. Make sure that the syscall
920 * restart code sign-extends orig_ax. Also make sure
921 * we interpret the -ERESTART* codes correctly if
922 * loaded into regs->ax in case the task is not
923 * actually still sitting at the exit from a 32-bit
924 * syscall with TS_COMPAT still set.
926 regs->orig_ax = value;
927 if (syscall_get_nr(child, regs) >= 0)
928 child->thread_info.status |= TS_I386_REGS_POKED;
931 case offsetof(struct user32, regs.eflags):
932 return set_flags(child, value);
934 case offsetof(struct user32, u_debugreg[0]) ...
935 offsetof(struct user32, u_debugreg[7]):
936 regno -= offsetof(struct user32, u_debugreg[0]);
937 return ptrace_set_debugreg(child, regno / 4, value);
940 if (regno > sizeof(struct user32) || (regno & 3))
944 * Other dummy fields in the virtual user structure
956 case offsetof(struct user32, regs.l): \
957 *val = regs->q; break
960 case offsetof(struct user32, regs.rs): \
961 *val = get_segment_reg(child, \
962 offsetof(struct user_regs_struct, rs)); \
965 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
967 struct pt_regs *regs = task_pt_regs(child);
985 R32(orig_eax, orig_ax);
989 case offsetof(struct user32, regs.eflags):
990 *val = get_flags(child);
993 case offsetof(struct user32, u_debugreg[0]) ...
994 offsetof(struct user32, u_debugreg[7]):
995 regno -= offsetof(struct user32, u_debugreg[0]);
996 *val = ptrace_get_debugreg(child, regno / 4);
1000 if (regno > sizeof(struct user32) || (regno & 3))
1004 * Other dummy fields in the virtual user structure
1016 static int genregs32_get(struct task_struct *target,
1017 const struct user_regset *regset,
1018 unsigned int pos, unsigned int count,
1019 void *kbuf, void __user *ubuf)
1022 compat_ulong_t *k = kbuf;
1023 while (count >= sizeof(*k)) {
1024 getreg32(target, pos, k++);
1025 count -= sizeof(*k);
1029 compat_ulong_t __user *u = ubuf;
1030 while (count >= sizeof(*u)) {
1031 compat_ulong_t word;
1032 getreg32(target, pos, &word);
1033 if (__put_user(word, u++))
1035 count -= sizeof(*u);
1043 static int genregs32_set(struct task_struct *target,
1044 const struct user_regset *regset,
1045 unsigned int pos, unsigned int count,
1046 const void *kbuf, const void __user *ubuf)
1050 const compat_ulong_t *k = kbuf;
1051 while (count >= sizeof(*k) && !ret) {
1052 ret = putreg32(target, pos, *k++);
1053 count -= sizeof(*k);
1057 const compat_ulong_t __user *u = ubuf;
1058 while (count >= sizeof(*u) && !ret) {
1059 compat_ulong_t word;
1060 ret = __get_user(word, u++);
1063 ret = putreg32(target, pos, word);
1064 count -= sizeof(*u);
1071 static long ia32_arch_ptrace(struct task_struct *child, compat_long_t request,
1072 compat_ulong_t caddr, compat_ulong_t cdata)
1074 unsigned long addr = caddr;
1075 unsigned long data = cdata;
1076 void __user *datap = compat_ptr(data);
1081 case PTRACE_PEEKUSR:
1082 ret = getreg32(child, addr, &val);
1084 ret = put_user(val, (__u32 __user *)datap);
1087 case PTRACE_POKEUSR:
1088 ret = putreg32(child, addr, data);
1091 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1092 return copy_regset_to_user(child, &user_x86_32_view,
1094 0, sizeof(struct user_regs_struct32),
1097 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1098 return copy_regset_from_user(child, &user_x86_32_view,
1100 sizeof(struct user_regs_struct32),
1103 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1104 return copy_regset_to_user(child, &user_x86_32_view,
1106 sizeof(struct user_i387_ia32_struct),
1109 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1110 return copy_regset_from_user(
1111 child, &user_x86_32_view, REGSET_FP,
1112 0, sizeof(struct user_i387_ia32_struct), datap);
1114 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1115 return copy_regset_to_user(child, &user_x86_32_view,
1117 sizeof(struct user32_fxsr_struct),
1120 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1121 return copy_regset_from_user(child, &user_x86_32_view,
1123 sizeof(struct user32_fxsr_struct),
1126 case PTRACE_GET_THREAD_AREA:
1127 case PTRACE_SET_THREAD_AREA:
1128 return arch_ptrace(child, request, addr, data);
1131 return compat_ptrace_request(child, request, addr, data);
1136 #endif /* CONFIG_IA32_EMULATION */
1138 #ifdef CONFIG_X86_X32_ABI
1139 static long x32_arch_ptrace(struct task_struct *child,
1140 compat_long_t request, compat_ulong_t caddr,
1141 compat_ulong_t cdata)
1143 unsigned long addr = caddr;
1144 unsigned long data = cdata;
1145 void __user *datap = compat_ptr(data);
1149 /* Read 32bits at location addr in the USER area. Only allow
1150 to return the lower 32bits of segment and debug registers. */
1151 case PTRACE_PEEKUSR: {
1155 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1156 addr < offsetof(struct user_regs_struct, cs))
1159 tmp = 0; /* Default return condition */
1160 if (addr < sizeof(struct user_regs_struct))
1161 tmp = getreg(child, addr);
1162 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1163 addr <= offsetof(struct user, u_debugreg[7])) {
1164 addr -= offsetof(struct user, u_debugreg[0]);
1165 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1167 ret = put_user(tmp, (__u32 __user *)datap);
1171 /* Write the word at location addr in the USER area. Only allow
1172 to update segment and debug registers with the upper 32bits
1174 case PTRACE_POKEUSR:
1176 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user) ||
1177 addr < offsetof(struct user_regs_struct, cs))
1180 if (addr < sizeof(struct user_regs_struct))
1181 ret = putreg(child, addr, data);
1182 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1183 addr <= offsetof(struct user, u_debugreg[7])) {
1184 addr -= offsetof(struct user, u_debugreg[0]);
1185 ret = ptrace_set_debugreg(child,
1186 addr / sizeof(data), data);
1190 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1191 return copy_regset_to_user(child,
1192 task_user_regset_view(current),
1194 0, sizeof(struct user_regs_struct),
1197 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1198 return copy_regset_from_user(child,
1199 task_user_regset_view(current),
1201 0, sizeof(struct user_regs_struct),
1204 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1205 return copy_regset_to_user(child,
1206 task_user_regset_view(current),
1208 0, sizeof(struct user_i387_struct),
1211 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1212 return copy_regset_from_user(child,
1213 task_user_regset_view(current),
1215 0, sizeof(struct user_i387_struct),
1219 return compat_ptrace_request(child, request, addr, data);
1226 #ifdef CONFIG_COMPAT
1227 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1228 compat_ulong_t caddr, compat_ulong_t cdata)
1230 #ifdef CONFIG_X86_X32_ABI
1231 if (!in_ia32_syscall())
1232 return x32_arch_ptrace(child, request, caddr, cdata);
1234 #ifdef CONFIG_IA32_EMULATION
1235 return ia32_arch_ptrace(child, request, caddr, cdata);
1240 #endif /* CONFIG_COMPAT */
1242 #ifdef CONFIG_X86_64
1244 static struct user_regset x86_64_regsets[] __ro_after_init = {
1245 [REGSET_GENERAL] = {
1246 .core_note_type = NT_PRSTATUS,
1247 .n = sizeof(struct user_regs_struct) / sizeof(long),
1248 .size = sizeof(long), .align = sizeof(long),
1249 .get = genregs_get, .set = genregs_set
1252 .core_note_type = NT_PRFPREG,
1253 .n = sizeof(struct user_i387_struct) / sizeof(long),
1254 .size = sizeof(long), .align = sizeof(long),
1255 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1258 .core_note_type = NT_X86_XSTATE,
1259 .size = sizeof(u64), .align = sizeof(u64),
1260 .active = xstateregs_active, .get = xstateregs_get,
1261 .set = xstateregs_set
1263 [REGSET_IOPERM64] = {
1264 .core_note_type = NT_386_IOPERM,
1265 .n = IO_BITMAP_LONGS,
1266 .size = sizeof(long), .align = sizeof(long),
1267 .active = ioperm_active, .get = ioperm_get
1271 static const struct user_regset_view user_x86_64_view = {
1272 .name = "x86_64", .e_machine = EM_X86_64,
1273 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1276 #else /* CONFIG_X86_32 */
1278 #define user_regs_struct32 user_regs_struct
1279 #define genregs32_get genregs_get
1280 #define genregs32_set genregs_set
1282 #endif /* CONFIG_X86_64 */
1284 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1285 static struct user_regset x86_32_regsets[] __ro_after_init = {
1286 [REGSET_GENERAL] = {
1287 .core_note_type = NT_PRSTATUS,
1288 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1289 .size = sizeof(u32), .align = sizeof(u32),
1290 .get = genregs32_get, .set = genregs32_set
1293 .core_note_type = NT_PRFPREG,
1294 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1295 .size = sizeof(u32), .align = sizeof(u32),
1296 .active = regset_fpregs_active, .get = fpregs_get, .set = fpregs_set
1299 .core_note_type = NT_PRXFPREG,
1300 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1301 .size = sizeof(u32), .align = sizeof(u32),
1302 .active = regset_xregset_fpregs_active, .get = xfpregs_get, .set = xfpregs_set
1305 .core_note_type = NT_X86_XSTATE,
1306 .size = sizeof(u64), .align = sizeof(u64),
1307 .active = xstateregs_active, .get = xstateregs_get,
1308 .set = xstateregs_set
1311 .core_note_type = NT_386_TLS,
1312 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1313 .size = sizeof(struct user_desc),
1314 .align = sizeof(struct user_desc),
1315 .active = regset_tls_active,
1316 .get = regset_tls_get, .set = regset_tls_set
1318 [REGSET_IOPERM32] = {
1319 .core_note_type = NT_386_IOPERM,
1320 .n = IO_BITMAP_BYTES / sizeof(u32),
1321 .size = sizeof(u32), .align = sizeof(u32),
1322 .active = ioperm_active, .get = ioperm_get
1326 static const struct user_regset_view user_x86_32_view = {
1327 .name = "i386", .e_machine = EM_386,
1328 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1333 * This represents bytes 464..511 in the memory layout exported through
1334 * the REGSET_XSTATE interface.
1336 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1338 void __init update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1340 #ifdef CONFIG_X86_64
1341 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1343 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1344 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1346 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1349 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1351 #ifdef CONFIG_IA32_EMULATION
1352 if (!user_64bit_mode(task_pt_regs(task)))
1354 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1355 return &user_x86_32_view;
1357 #ifdef CONFIG_X86_64
1358 return &user_x86_64_view;
1362 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1363 int error_code, int si_code)
1365 tsk->thread.trap_nr = X86_TRAP_DB;
1366 tsk->thread.error_code = error_code;
1368 /* Send us the fake SIGTRAP */
1369 force_sig_fault(SIGTRAP, si_code,
1370 user_mode(regs) ? (void __user *)regs->ip : NULL, tsk);
1373 void user_single_step_report(struct pt_regs *regs)
1375 send_sigtrap(current, regs, 0, TRAP_BRKPT);