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/regset.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>
25 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/debugreg.h>
33 #include <asm/prctl.h>
34 #include <asm/proto.h>
35 #include <asm/hw_breakpoint.h>
39 #define CREATE_TRACE_POINTS
40 #include <trace/events/syscalls.h>
46 REGSET_IOPERM64 = REGSET_XFP,
52 struct pt_regs_offset {
57 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
58 #define REG_OFFSET_END {.name = NULL, .offset = 0}
60 static const struct pt_regs_offset regoffset_table[] = {
84 REG_OFFSET_NAME(orig_ax),
87 REG_OFFSET_NAME(flags),
94 * regs_query_register_offset() - query register offset from its name
95 * @name: the name of a register
97 * regs_query_register_offset() returns the offset of a register in struct
98 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
100 int regs_query_register_offset(const char *name)
102 const struct pt_regs_offset *roff;
103 for (roff = regoffset_table; roff->name != NULL; roff++)
104 if (!strcmp(roff->name, name))
110 * regs_query_register_name() - query register name from its offset
111 * @offset: the offset of a register in struct pt_regs.
113 * regs_query_register_name() returns the name of a register from its
114 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
116 const char *regs_query_register_name(unsigned int offset)
118 const struct pt_regs_offset *roff;
119 for (roff = regoffset_table; roff->name != NULL; roff++)
120 if (roff->offset == offset)
125 static const int arg_offs_table[] = {
127 [0] = offsetof(struct pt_regs, ax),
128 [1] = offsetof(struct pt_regs, dx),
129 [2] = offsetof(struct pt_regs, cx)
130 #else /* CONFIG_X86_64 */
131 [0] = offsetof(struct pt_regs, di),
132 [1] = offsetof(struct pt_regs, si),
133 [2] = offsetof(struct pt_regs, dx),
134 [3] = offsetof(struct pt_regs, cx),
135 [4] = offsetof(struct pt_regs, r8),
136 [5] = offsetof(struct pt_regs, r9)
141 * does not yet catch signals sent when the child dies.
142 * in exit.c or in signal.c.
146 * Determines which flags the user has access to [1 = access, 0 = no access].
148 #define FLAG_MASK_32 ((unsigned long) \
149 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
150 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
151 X86_EFLAGS_SF | X86_EFLAGS_TF | \
152 X86_EFLAGS_DF | X86_EFLAGS_OF | \
153 X86_EFLAGS_RF | X86_EFLAGS_AC))
156 * Determines whether a value may be installed in a segment register.
158 static inline bool invalid_selector(u16 value)
160 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
165 #define FLAG_MASK FLAG_MASK_32
168 * X86_32 CPUs don't save ss and esp if the CPU is already in kernel mode
169 * when it traps. The previous stack will be directly underneath the saved
170 * registers, and 'sp/ss' won't even have been saved. Thus the '®s->sp'.
172 * Now, if the stack is empty, '®s->sp' is out of range. In this
173 * case we try to take the previous stack. To always return a non-null
174 * stack pointer we fall back to regs as stack if no previous stack
177 * This is valid only for kernel mode traps.
179 unsigned long kernel_stack_pointer(struct pt_regs *regs)
181 unsigned long context = (unsigned long)regs & ~(THREAD_SIZE - 1);
182 unsigned long sp = (unsigned long)®s->sp;
183 struct thread_info *tinfo;
185 if (context == (sp & ~(THREAD_SIZE - 1)))
188 tinfo = (struct thread_info *)context;
189 if (tinfo->previous_esp)
190 return tinfo->previous_esp;
192 return (unsigned long)regs;
195 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
197 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
198 return ®s->bx + (regno >> 2);
201 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
204 * Returning the value truncates it to 16 bits.
207 if (offset != offsetof(struct user_regs_struct, gs))
208 retval = *pt_regs_access(task_pt_regs(task), offset);
211 retval = get_user_gs(task_pt_regs(task));
213 retval = task_user_gs(task);
218 static int set_segment_reg(struct task_struct *task,
219 unsigned long offset, u16 value)
222 * The value argument was already truncated to 16 bits.
224 if (invalid_selector(value))
228 * For %cs and %ss we cannot permit a null selector.
229 * We can permit a bogus selector as long as it has USER_RPL.
230 * Null selectors are fine for other segment registers, but
231 * we will never get back to user mode with invalid %cs or %ss
232 * and will take the trap in iret instead. Much code relies
233 * on user_mode() to distinguish a user trap frame (which can
234 * safely use invalid selectors) from a kernel trap frame.
237 case offsetof(struct user_regs_struct, cs):
238 case offsetof(struct user_regs_struct, ss):
239 if (unlikely(value == 0))
243 *pt_regs_access(task_pt_regs(task), offset) = value;
246 case offsetof(struct user_regs_struct, gs):
248 set_user_gs(task_pt_regs(task), value);
250 task_user_gs(task) = value;
256 #else /* CONFIG_X86_64 */
258 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
260 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
262 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
263 return ®s->r15 + (offset / sizeof(regs->r15));
266 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
269 * Returning the value truncates it to 16 bits.
274 case offsetof(struct user_regs_struct, fs):
275 if (task == current) {
276 /* Older gas can't assemble movq %?s,%r?? */
277 asm("movl %%fs,%0" : "=r" (seg));
280 return task->thread.fsindex;
281 case offsetof(struct user_regs_struct, gs):
282 if (task == current) {
283 asm("movl %%gs,%0" : "=r" (seg));
286 return task->thread.gsindex;
287 case offsetof(struct user_regs_struct, ds):
288 if (task == current) {
289 asm("movl %%ds,%0" : "=r" (seg));
292 return task->thread.ds;
293 case offsetof(struct user_regs_struct, es):
294 if (task == current) {
295 asm("movl %%es,%0" : "=r" (seg));
298 return task->thread.es;
300 case offsetof(struct user_regs_struct, cs):
301 case offsetof(struct user_regs_struct, ss):
304 return *pt_regs_access(task_pt_regs(task), offset);
307 static int set_segment_reg(struct task_struct *task,
308 unsigned long offset, u16 value)
311 * The value argument was already truncated to 16 bits.
313 if (invalid_selector(value))
317 case offsetof(struct user_regs_struct,fs):
319 * If this is setting fs as for normal 64-bit use but
320 * setting fs_base has implicitly changed it, leave it.
322 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
323 task->thread.fs != 0) ||
324 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
325 task->thread.fs == 0))
327 task->thread.fsindex = value;
329 loadsegment(fs, task->thread.fsindex);
331 case offsetof(struct user_regs_struct,gs):
333 * If this is setting gs as for normal 64-bit use but
334 * setting gs_base has implicitly changed it, leave it.
336 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
337 task->thread.gs != 0) ||
338 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
339 task->thread.gs == 0))
341 task->thread.gsindex = value;
343 load_gs_index(task->thread.gsindex);
345 case offsetof(struct user_regs_struct,ds):
346 task->thread.ds = value;
348 loadsegment(ds, task->thread.ds);
350 case offsetof(struct user_regs_struct,es):
351 task->thread.es = value;
353 loadsegment(es, task->thread.es);
357 * Can't actually change these in 64-bit mode.
359 case offsetof(struct user_regs_struct,cs):
360 if (unlikely(value == 0))
362 #ifdef CONFIG_IA32_EMULATION
363 if (test_tsk_thread_flag(task, TIF_IA32))
364 task_pt_regs(task)->cs = value;
367 case offsetof(struct user_regs_struct,ss):
368 if (unlikely(value == 0))
370 #ifdef CONFIG_IA32_EMULATION
371 if (test_tsk_thread_flag(task, TIF_IA32))
372 task_pt_regs(task)->ss = value;
380 #endif /* CONFIG_X86_32 */
382 static unsigned long get_flags(struct task_struct *task)
384 unsigned long retval = task_pt_regs(task)->flags;
387 * If the debugger set TF, hide it from the readout.
389 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
390 retval &= ~X86_EFLAGS_TF;
395 static int set_flags(struct task_struct *task, unsigned long value)
397 struct pt_regs *regs = task_pt_regs(task);
400 * If the user value contains TF, mark that
401 * it was not "us" (the debugger) that set it.
402 * If not, make sure it stays set if we had.
404 if (value & X86_EFLAGS_TF)
405 clear_tsk_thread_flag(task, TIF_FORCED_TF);
406 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
407 value |= X86_EFLAGS_TF;
409 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
414 static int putreg(struct task_struct *child,
415 unsigned long offset, unsigned long value)
418 case offsetof(struct user_regs_struct, cs):
419 case offsetof(struct user_regs_struct, ds):
420 case offsetof(struct user_regs_struct, es):
421 case offsetof(struct user_regs_struct, fs):
422 case offsetof(struct user_regs_struct, gs):
423 case offsetof(struct user_regs_struct, ss):
424 return set_segment_reg(child, offset, value);
426 case offsetof(struct user_regs_struct, flags):
427 return set_flags(child, value);
430 case offsetof(struct user_regs_struct,fs_base):
431 if (value >= TASK_SIZE_OF(child))
434 * When changing the segment base, use do_arch_prctl
435 * to set either thread.fs or thread.fsindex and the
436 * corresponding GDT slot.
438 if (child->thread.fs != value)
439 return do_arch_prctl(child, ARCH_SET_FS, value);
441 case offsetof(struct user_regs_struct,gs_base):
443 * Exactly the same here as the %fs handling above.
445 if (value >= TASK_SIZE_OF(child))
447 if (child->thread.gs != value)
448 return do_arch_prctl(child, ARCH_SET_GS, value);
453 *pt_regs_access(task_pt_regs(child), offset) = value;
457 static unsigned long getreg(struct task_struct *task, unsigned long offset)
460 case offsetof(struct user_regs_struct, cs):
461 case offsetof(struct user_regs_struct, ds):
462 case offsetof(struct user_regs_struct, es):
463 case offsetof(struct user_regs_struct, fs):
464 case offsetof(struct user_regs_struct, gs):
465 case offsetof(struct user_regs_struct, ss):
466 return get_segment_reg(task, offset);
468 case offsetof(struct user_regs_struct, flags):
469 return get_flags(task);
472 case offsetof(struct user_regs_struct, fs_base): {
474 * do_arch_prctl may have used a GDT slot instead of
475 * the MSR. To userland, it appears the same either
476 * way, except the %fs segment selector might not be 0.
478 unsigned int seg = task->thread.fsindex;
479 if (task->thread.fs != 0)
480 return task->thread.fs;
482 asm("movl %%fs,%0" : "=r" (seg));
483 if (seg != FS_TLS_SEL)
485 return get_desc_base(&task->thread.tls_array[FS_TLS]);
487 case offsetof(struct user_regs_struct, gs_base): {
489 * Exactly the same here as the %fs handling above.
491 unsigned int seg = task->thread.gsindex;
492 if (task->thread.gs != 0)
493 return task->thread.gs;
495 asm("movl %%gs,%0" : "=r" (seg));
496 if (seg != GS_TLS_SEL)
498 return get_desc_base(&task->thread.tls_array[GS_TLS]);
503 return *pt_regs_access(task_pt_regs(task), offset);
506 static int genregs_get(struct task_struct *target,
507 const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 void *kbuf, void __user *ubuf)
512 unsigned long *k = kbuf;
513 while (count >= sizeof(*k)) {
514 *k++ = getreg(target, pos);
519 unsigned long __user *u = ubuf;
520 while (count >= sizeof(*u)) {
521 if (__put_user(getreg(target, pos), u++))
531 static int genregs_set(struct task_struct *target,
532 const struct user_regset *regset,
533 unsigned int pos, unsigned int count,
534 const void *kbuf, const void __user *ubuf)
538 const unsigned long *k = kbuf;
539 while (count >= sizeof(*k) && !ret) {
540 ret = putreg(target, pos, *k++);
545 const unsigned long __user *u = ubuf;
546 while (count >= sizeof(*u) && !ret) {
548 ret = __get_user(word, u++);
551 ret = putreg(target, pos, word);
559 static void ptrace_triggered(struct perf_event *bp,
560 struct perf_sample_data *data,
561 struct pt_regs *regs)
564 struct thread_struct *thread = &(current->thread);
567 * Store in the virtual DR6 register the fact that the breakpoint
568 * was hit so the thread's debugger will see it.
570 for (i = 0; i < HBP_NUM; i++) {
571 if (thread->ptrace_bps[i] == bp)
575 thread->debugreg6 |= (DR_TRAP0 << i);
579 * Walk through every ptrace breakpoints for this thread and
580 * build the dr7 value on top of their attributes.
583 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
587 struct arch_hw_breakpoint *info;
589 for (i = 0; i < HBP_NUM; i++) {
590 if (bp[i] && !bp[i]->attr.disabled) {
591 info = counter_arch_bp(bp[i]);
592 dr7 |= encode_dr7(i, info->len, info->type);
600 ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
601 struct task_struct *tsk, int disabled)
604 int gen_len, gen_type;
605 struct perf_event_attr attr;
608 * We should have at least an inactive breakpoint at this
609 * slot. It means the user is writing dr7 without having
610 * written the address register first
615 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
620 attr.bp_len = gen_len;
621 attr.bp_type = gen_type;
622 attr.disabled = disabled;
624 return modify_user_hw_breakpoint(bp, &attr);
628 * Handle ptrace writes to debug register 7.
630 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
632 struct thread_struct *thread = &(tsk->thread);
633 unsigned long old_dr7;
634 int i, orig_ret = 0, rc = 0;
635 int enabled, second_pass = 0;
637 struct perf_event *bp;
639 if (ptrace_get_breakpoints(tsk) < 0)
642 data &= ~DR_CONTROL_RESERVED;
643 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
646 * Loop through all the hardware breakpoints, making the
647 * appropriate changes to each.
649 for (i = 0; i < HBP_NUM; i++) {
650 enabled = decode_dr7(data, i, &len, &type);
651 bp = thread->ptrace_bps[i];
656 * Don't unregister the breakpoints right-away,
657 * unless all register_user_hw_breakpoint()
658 * requests have succeeded. This prevents
659 * any window of opportunity for debug
660 * register grabbing by other users.
665 rc = ptrace_modify_breakpoint(bp, len, type,
673 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
678 * Make a second pass to free the remaining unused breakpoints
679 * or to restore the original breakpoints if an error occurred.
690 ptrace_put_breakpoints(tsk);
692 return ((orig_ret < 0) ? orig_ret : rc);
696 * Handle PTRACE_PEEKUSR calls for the debug register area.
698 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
700 struct thread_struct *thread = &(tsk->thread);
701 unsigned long val = 0;
704 struct perf_event *bp;
706 if (ptrace_get_breakpoints(tsk) < 0)
709 bp = thread->ptrace_bps[n];
713 val = bp->hw.info.address;
715 ptrace_put_breakpoints(tsk);
717 val = thread->debugreg6;
719 val = thread->ptrace_dr7;
724 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
727 struct perf_event *bp;
728 struct thread_struct *t = &tsk->thread;
729 struct perf_event_attr attr;
732 if (ptrace_get_breakpoints(tsk) < 0)
735 if (!t->ptrace_bps[nr]) {
736 ptrace_breakpoint_init(&attr);
738 * Put stub len and type to register (reserve) an inactive but
742 attr.bp_len = HW_BREAKPOINT_LEN_1;
743 attr.bp_type = HW_BREAKPOINT_W;
746 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
750 * CHECKME: the previous code returned -EIO if the addr wasn't
751 * a valid task virtual addr. The new one will return -EINVAL in
753 * -EINVAL may be what we want for in-kernel breakpoints users,
754 * but -EIO looks better for ptrace, since we refuse a register
755 * writing for the user. And anyway this is the previous
763 t->ptrace_bps[nr] = bp;
765 bp = t->ptrace_bps[nr];
769 err = modify_user_hw_breakpoint(bp, &attr);
773 ptrace_put_breakpoints(tsk);
778 * Handle PTRACE_POKEUSR calls for the debug register area.
780 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
782 struct thread_struct *thread = &(tsk->thread);
785 /* There are no DR4 or DR5 registers */
786 if (n == 4 || n == 5)
790 thread->debugreg6 = val;
794 rc = ptrace_set_breakpoint_addr(tsk, n, val);
798 /* All that's left is DR7 */
800 rc = ptrace_write_dr7(tsk, val);
802 thread->ptrace_dr7 = val;
810 * These access the current or another (stopped) task's io permission
811 * bitmap for debugging or core dump.
813 static int ioperm_active(struct task_struct *target,
814 const struct user_regset *regset)
816 return target->thread.io_bitmap_max / regset->size;
819 static int ioperm_get(struct task_struct *target,
820 const struct user_regset *regset,
821 unsigned int pos, unsigned int count,
822 void *kbuf, void __user *ubuf)
824 if (!target->thread.io_bitmap_ptr)
827 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
828 target->thread.io_bitmap_ptr,
833 * Called by kernel/ptrace.c when detaching..
835 * Make sure the single step bit is not set.
837 void ptrace_disable(struct task_struct *child)
839 user_disable_single_step(child);
840 #ifdef TIF_SYSCALL_EMU
841 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
845 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
846 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
849 long arch_ptrace(struct task_struct *child, long request,
850 unsigned long addr, unsigned long data)
853 unsigned long __user *datap = (unsigned long __user *)data;
856 /* read the word at location addr in the USER area. */
857 case PTRACE_PEEKUSR: {
861 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
864 tmp = 0; /* Default return condition */
865 if (addr < sizeof(struct user_regs_struct))
866 tmp = getreg(child, addr);
867 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
868 addr <= offsetof(struct user, u_debugreg[7])) {
869 addr -= offsetof(struct user, u_debugreg[0]);
870 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
872 ret = put_user(tmp, datap);
876 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
878 if ((addr & (sizeof(data) - 1)) || addr >= sizeof(struct user))
881 if (addr < sizeof(struct user_regs_struct))
882 ret = putreg(child, addr, data);
883 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
884 addr <= offsetof(struct user, u_debugreg[7])) {
885 addr -= offsetof(struct user, u_debugreg[0]);
886 ret = ptrace_set_debugreg(child,
887 addr / sizeof(data), data);
891 case PTRACE_GETREGS: /* Get all gp regs from the child. */
892 return copy_regset_to_user(child,
893 task_user_regset_view(current),
895 0, sizeof(struct user_regs_struct),
898 case PTRACE_SETREGS: /* Set all gp regs in the child. */
899 return copy_regset_from_user(child,
900 task_user_regset_view(current),
902 0, sizeof(struct user_regs_struct),
905 case PTRACE_GETFPREGS: /* Get the child FPU state. */
906 return copy_regset_to_user(child,
907 task_user_regset_view(current),
909 0, sizeof(struct user_i387_struct),
912 case PTRACE_SETFPREGS: /* Set the child FPU state. */
913 return copy_regset_from_user(child,
914 task_user_regset_view(current),
916 0, sizeof(struct user_i387_struct),
920 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
921 return copy_regset_to_user(child, &user_x86_32_view,
923 0, sizeof(struct user_fxsr_struct),
926 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
927 return copy_regset_from_user(child, &user_x86_32_view,
929 0, sizeof(struct user_fxsr_struct),
933 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
934 case PTRACE_GET_THREAD_AREA:
937 ret = do_get_thread_area(child, addr,
938 (struct user_desc __user *)data);
941 case PTRACE_SET_THREAD_AREA:
944 ret = do_set_thread_area(child, addr,
945 (struct user_desc __user *)data, 0);
950 /* normal 64bit interface to access TLS data.
951 Works just like arch_prctl, except that the arguments
953 case PTRACE_ARCH_PRCTL:
954 ret = do_arch_prctl(child, data, addr);
959 ret = ptrace_request(child, request, addr, data);
966 #ifdef CONFIG_IA32_EMULATION
968 #include <linux/compat.h>
969 #include <linux/syscalls.h>
970 #include <asm/ia32.h>
971 #include <asm/user32.h>
974 case offsetof(struct user32, regs.l): \
975 regs->q = value; break
978 case offsetof(struct user32, regs.rs): \
979 return set_segment_reg(child, \
980 offsetof(struct user_regs_struct, rs), \
984 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
986 struct pt_regs *regs = task_pt_regs(child);
1007 case offsetof(struct user32, regs.orig_eax):
1009 * A 32-bit debugger setting orig_eax means to restore
1010 * the state of the task restarting a 32-bit syscall.
1011 * Make sure we interpret the -ERESTART* codes correctly
1012 * in case the task is not actually still sitting at the
1013 * exit from a 32-bit syscall with TS_COMPAT still set.
1015 regs->orig_ax = value;
1016 if (syscall_get_nr(child, regs) >= 0)
1017 task_thread_info(child)->status |= TS_COMPAT;
1020 case offsetof(struct user32, regs.eflags):
1021 return set_flags(child, value);
1023 case offsetof(struct user32, u_debugreg[0]) ...
1024 offsetof(struct user32, u_debugreg[7]):
1025 regno -= offsetof(struct user32, u_debugreg[0]);
1026 return ptrace_set_debugreg(child, regno / 4, value);
1029 if (regno > sizeof(struct user32) || (regno & 3))
1033 * Other dummy fields in the virtual user structure
1045 case offsetof(struct user32, regs.l): \
1046 *val = regs->q; break
1049 case offsetof(struct user32, regs.rs): \
1050 *val = get_segment_reg(child, \
1051 offsetof(struct user_regs_struct, rs)); \
1054 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1056 struct pt_regs *regs = task_pt_regs(child);
1074 R32(orig_eax, orig_ax);
1078 case offsetof(struct user32, regs.eflags):
1079 *val = get_flags(child);
1082 case offsetof(struct user32, u_debugreg[0]) ...
1083 offsetof(struct user32, u_debugreg[7]):
1084 regno -= offsetof(struct user32, u_debugreg[0]);
1085 *val = ptrace_get_debugreg(child, regno / 4);
1089 if (regno > sizeof(struct user32) || (regno & 3))
1093 * Other dummy fields in the virtual user structure
1105 static int genregs32_get(struct task_struct *target,
1106 const struct user_regset *regset,
1107 unsigned int pos, unsigned int count,
1108 void *kbuf, void __user *ubuf)
1111 compat_ulong_t *k = kbuf;
1112 while (count >= sizeof(*k)) {
1113 getreg32(target, pos, k++);
1114 count -= sizeof(*k);
1118 compat_ulong_t __user *u = ubuf;
1119 while (count >= sizeof(*u)) {
1120 compat_ulong_t word;
1121 getreg32(target, pos, &word);
1122 if (__put_user(word, u++))
1124 count -= sizeof(*u);
1132 static int genregs32_set(struct task_struct *target,
1133 const struct user_regset *regset,
1134 unsigned int pos, unsigned int count,
1135 const void *kbuf, const void __user *ubuf)
1139 const compat_ulong_t *k = kbuf;
1140 while (count >= sizeof(*k) && !ret) {
1141 ret = putreg32(target, pos, *k++);
1142 count -= sizeof(*k);
1146 const compat_ulong_t __user *u = ubuf;
1147 while (count >= sizeof(*u) && !ret) {
1148 compat_ulong_t word;
1149 ret = __get_user(word, u++);
1152 ret = putreg32(target, pos, word);
1153 count -= sizeof(*u);
1160 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1161 compat_ulong_t caddr, compat_ulong_t cdata)
1163 unsigned long addr = caddr;
1164 unsigned long data = cdata;
1165 void __user *datap = compat_ptr(data);
1170 case PTRACE_PEEKUSR:
1171 ret = getreg32(child, addr, &val);
1173 ret = put_user(val, (__u32 __user *)datap);
1176 case PTRACE_POKEUSR:
1177 ret = putreg32(child, addr, data);
1180 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1181 return copy_regset_to_user(child, &user_x86_32_view,
1183 0, sizeof(struct user_regs_struct32),
1186 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1187 return copy_regset_from_user(child, &user_x86_32_view,
1189 sizeof(struct user_regs_struct32),
1192 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1193 return copy_regset_to_user(child, &user_x86_32_view,
1195 sizeof(struct user_i387_ia32_struct),
1198 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1199 return copy_regset_from_user(
1200 child, &user_x86_32_view, REGSET_FP,
1201 0, sizeof(struct user_i387_ia32_struct), datap);
1203 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1204 return copy_regset_to_user(child, &user_x86_32_view,
1206 sizeof(struct user32_fxsr_struct),
1209 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1210 return copy_regset_from_user(child, &user_x86_32_view,
1212 sizeof(struct user32_fxsr_struct),
1215 case PTRACE_GET_THREAD_AREA:
1216 case PTRACE_SET_THREAD_AREA:
1217 return arch_ptrace(child, request, addr, data);
1220 return compat_ptrace_request(child, request, addr, data);
1226 #endif /* CONFIG_IA32_EMULATION */
1228 #ifdef CONFIG_X86_64
1230 static struct user_regset x86_64_regsets[] __read_mostly = {
1231 [REGSET_GENERAL] = {
1232 .core_note_type = NT_PRSTATUS,
1233 .n = sizeof(struct user_regs_struct) / sizeof(long),
1234 .size = sizeof(long), .align = sizeof(long),
1235 .get = genregs_get, .set = genregs_set
1238 .core_note_type = NT_PRFPREG,
1239 .n = sizeof(struct user_i387_struct) / sizeof(long),
1240 .size = sizeof(long), .align = sizeof(long),
1241 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1244 .core_note_type = NT_X86_XSTATE,
1245 .size = sizeof(u64), .align = sizeof(u64),
1246 .active = xstateregs_active, .get = xstateregs_get,
1247 .set = xstateregs_set
1249 [REGSET_IOPERM64] = {
1250 .core_note_type = NT_386_IOPERM,
1251 .n = IO_BITMAP_LONGS,
1252 .size = sizeof(long), .align = sizeof(long),
1253 .active = ioperm_active, .get = ioperm_get
1257 static const struct user_regset_view user_x86_64_view = {
1258 .name = "x86_64", .e_machine = EM_X86_64,
1259 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1262 #else /* CONFIG_X86_32 */
1264 #define user_regs_struct32 user_regs_struct
1265 #define genregs32_get genregs_get
1266 #define genregs32_set genregs_set
1268 #define user_i387_ia32_struct user_i387_struct
1269 #define user32_fxsr_struct user_fxsr_struct
1271 #endif /* CONFIG_X86_64 */
1273 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1274 static struct user_regset x86_32_regsets[] __read_mostly = {
1275 [REGSET_GENERAL] = {
1276 .core_note_type = NT_PRSTATUS,
1277 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1278 .size = sizeof(u32), .align = sizeof(u32),
1279 .get = genregs32_get, .set = genregs32_set
1282 .core_note_type = NT_PRFPREG,
1283 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1284 .size = sizeof(u32), .align = sizeof(u32),
1285 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1288 .core_note_type = NT_PRXFPREG,
1289 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1290 .size = sizeof(u32), .align = sizeof(u32),
1291 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1294 .core_note_type = NT_X86_XSTATE,
1295 .size = sizeof(u64), .align = sizeof(u64),
1296 .active = xstateregs_active, .get = xstateregs_get,
1297 .set = xstateregs_set
1300 .core_note_type = NT_386_TLS,
1301 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1302 .size = sizeof(struct user_desc),
1303 .align = sizeof(struct user_desc),
1304 .active = regset_tls_active,
1305 .get = regset_tls_get, .set = regset_tls_set
1307 [REGSET_IOPERM32] = {
1308 .core_note_type = NT_386_IOPERM,
1309 .n = IO_BITMAP_BYTES / sizeof(u32),
1310 .size = sizeof(u32), .align = sizeof(u32),
1311 .active = ioperm_active, .get = ioperm_get
1315 static const struct user_regset_view user_x86_32_view = {
1316 .name = "i386", .e_machine = EM_386,
1317 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1322 * This represents bytes 464..511 in the memory layout exported through
1323 * the REGSET_XSTATE interface.
1325 u64 xstate_fx_sw_bytes[USER_XSTATE_FX_SW_WORDS];
1327 void update_regset_xstate_info(unsigned int size, u64 xstate_mask)
1329 #ifdef CONFIG_X86_64
1330 x86_64_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1332 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1333 x86_32_regsets[REGSET_XSTATE].n = size / sizeof(u64);
1335 xstate_fx_sw_bytes[USER_XSTATE_XCR0_WORD] = xstate_mask;
1338 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1340 #ifdef CONFIG_IA32_EMULATION
1341 if (test_tsk_thread_flag(task, TIF_IA32))
1343 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1344 return &user_x86_32_view;
1346 #ifdef CONFIG_X86_64
1347 return &user_x86_64_view;
1351 static void fill_sigtrap_info(struct task_struct *tsk,
1352 struct pt_regs *regs,
1353 int error_code, int si_code,
1354 struct siginfo *info)
1356 tsk->thread.trap_no = 1;
1357 tsk->thread.error_code = error_code;
1359 memset(info, 0, sizeof(*info));
1360 info->si_signo = SIGTRAP;
1361 info->si_code = si_code;
1362 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1365 void user_single_step_siginfo(struct task_struct *tsk,
1366 struct pt_regs *regs,
1367 struct siginfo *info)
1369 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1372 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1373 int error_code, int si_code)
1375 struct siginfo info;
1377 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1378 /* Send us the fake SIGTRAP */
1379 force_sig_info(SIGTRAP, &info, tsk);
1383 #ifdef CONFIG_X86_32
1385 #elif defined CONFIG_IA32_EMULATION
1386 # define IS_IA32 is_compat_task()
1392 * We must return the syscall number to actually look up in the table.
1393 * This can be -1L to skip running any syscall at all.
1395 long syscall_trace_enter(struct pt_regs *regs)
1400 * If we stepped into a sysenter/syscall insn, it trapped in
1401 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1402 * If user-mode had set TF itself, then it's still clear from
1403 * do_debug() and we need to set it again to restore the user
1404 * state. If we entered on the slow path, TF was already set.
1406 if (test_thread_flag(TIF_SINGLESTEP))
1407 regs->flags |= X86_EFLAGS_TF;
1409 /* do the secure computing check first */
1410 secure_computing(regs->orig_ax);
1412 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1415 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1416 tracehook_report_syscall_entry(regs))
1419 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1420 trace_sys_enter(regs, regs->orig_ax);
1422 if (unlikely(current->audit_context)) {
1424 audit_syscall_entry(AUDIT_ARCH_I386,
1427 regs->dx, regs->si);
1428 #ifdef CONFIG_X86_64
1430 audit_syscall_entry(AUDIT_ARCH_X86_64,
1433 regs->dx, regs->r10);
1437 return ret ?: regs->orig_ax;
1440 void syscall_trace_leave(struct pt_regs *regs)
1444 if (unlikely(current->audit_context))
1445 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1447 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1448 trace_sys_exit(regs, regs->ax);
1451 * If TIF_SYSCALL_EMU is set, we only get here because of
1452 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1453 * We already reported this syscall instruction in
1454 * syscall_trace_enter().
1456 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1457 !test_thread_flag(TIF_SYSCALL_EMU);
1458 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1459 tracehook_report_syscall_exit(regs, step);