1 /* By Ross Biro 1/23/92 */
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/tracehook.h>
18 #include <linux/user.h>
19 #include <linux/elf.h>
20 #include <linux/security.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/signal.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>
47 * does not yet catch signals sent when the child dies.
48 * in exit.c or in signal.c.
52 * Determines which flags the user has access to [1 = access, 0 = no access].
54 #define FLAG_MASK_32 ((unsigned long) \
55 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
56 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
57 X86_EFLAGS_SF | X86_EFLAGS_TF | \
58 X86_EFLAGS_DF | X86_EFLAGS_OF | \
59 X86_EFLAGS_RF | X86_EFLAGS_AC))
62 * Determines whether a value may be installed in a segment register.
64 static inline bool invalid_selector(u16 value)
66 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
71 #define FLAG_MASK FLAG_MASK_32
73 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
75 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
79 return ®s->bx + regno;
82 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
85 * Returning the value truncates it to 16 bits.
88 if (offset != offsetof(struct user_regs_struct, gs))
89 retval = *pt_regs_access(task_pt_regs(task), offset);
91 retval = task->thread.gs;
93 savesegment(gs, retval);
98 static int set_segment_reg(struct task_struct *task,
99 unsigned long offset, u16 value)
102 * The value argument was already truncated to 16 bits.
104 if (invalid_selector(value))
108 * For %cs and %ss we cannot permit a null selector.
109 * We can permit a bogus selector as long as it has USER_RPL.
110 * Null selectors are fine for other segment registers, but
111 * we will never get back to user mode with invalid %cs or %ss
112 * and will take the trap in iret instead. Much code relies
113 * on user_mode() to distinguish a user trap frame (which can
114 * safely use invalid selectors) from a kernel trap frame.
117 case offsetof(struct user_regs_struct, cs):
118 case offsetof(struct user_regs_struct, ss):
119 if (unlikely(value == 0))
123 *pt_regs_access(task_pt_regs(task), offset) = value;
126 case offsetof(struct user_regs_struct, gs):
127 task->thread.gs = value;
130 * The user-mode %gs is not affected by
131 * kernel entry, so we must update the CPU.
133 loadsegment(gs, value);
139 static unsigned long debugreg_addr_limit(struct task_struct *task)
141 return TASK_SIZE - 3;
144 #else /* CONFIG_X86_64 */
146 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
148 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
150 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
151 return ®s->r15 + (offset / sizeof(regs->r15));
154 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
157 * Returning the value truncates it to 16 bits.
162 case offsetof(struct user_regs_struct, fs):
163 if (task == current) {
164 /* Older gas can't assemble movq %?s,%r?? */
165 asm("movl %%fs,%0" : "=r" (seg));
168 return task->thread.fsindex;
169 case offsetof(struct user_regs_struct, gs):
170 if (task == current) {
171 asm("movl %%gs,%0" : "=r" (seg));
174 return task->thread.gsindex;
175 case offsetof(struct user_regs_struct, ds):
176 if (task == current) {
177 asm("movl %%ds,%0" : "=r" (seg));
180 return task->thread.ds;
181 case offsetof(struct user_regs_struct, es):
182 if (task == current) {
183 asm("movl %%es,%0" : "=r" (seg));
186 return task->thread.es;
188 case offsetof(struct user_regs_struct, cs):
189 case offsetof(struct user_regs_struct, ss):
192 return *pt_regs_access(task_pt_regs(task), offset);
195 static int set_segment_reg(struct task_struct *task,
196 unsigned long offset, u16 value)
199 * The value argument was already truncated to 16 bits.
201 if (invalid_selector(value))
205 case offsetof(struct user_regs_struct,fs):
207 * If this is setting fs as for normal 64-bit use but
208 * setting fs_base has implicitly changed it, leave it.
210 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
211 task->thread.fs != 0) ||
212 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
213 task->thread.fs == 0))
215 task->thread.fsindex = value;
217 loadsegment(fs, task->thread.fsindex);
219 case offsetof(struct user_regs_struct,gs):
221 * If this is setting gs as for normal 64-bit use but
222 * setting gs_base has implicitly changed it, leave it.
224 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
225 task->thread.gs != 0) ||
226 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
227 task->thread.gs == 0))
229 task->thread.gsindex = value;
231 load_gs_index(task->thread.gsindex);
233 case offsetof(struct user_regs_struct,ds):
234 task->thread.ds = value;
236 loadsegment(ds, task->thread.ds);
238 case offsetof(struct user_regs_struct,es):
239 task->thread.es = value;
241 loadsegment(es, task->thread.es);
245 * Can't actually change these in 64-bit mode.
247 case offsetof(struct user_regs_struct,cs):
248 if (unlikely(value == 0))
250 #ifdef CONFIG_IA32_EMULATION
251 if (test_tsk_thread_flag(task, TIF_IA32))
252 task_pt_regs(task)->cs = value;
255 case offsetof(struct user_regs_struct,ss):
256 if (unlikely(value == 0))
258 #ifdef CONFIG_IA32_EMULATION
259 if (test_tsk_thread_flag(task, TIF_IA32))
260 task_pt_regs(task)->ss = value;
268 static unsigned long debugreg_addr_limit(struct task_struct *task)
270 #ifdef CONFIG_IA32_EMULATION
271 if (test_tsk_thread_flag(task, TIF_IA32))
272 return IA32_PAGE_OFFSET - 3;
274 return TASK_SIZE64 - 7;
277 #endif /* CONFIG_X86_32 */
279 static unsigned long get_flags(struct task_struct *task)
281 unsigned long retval = task_pt_regs(task)->flags;
284 * If the debugger set TF, hide it from the readout.
286 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
287 retval &= ~X86_EFLAGS_TF;
292 static int set_flags(struct task_struct *task, unsigned long value)
294 struct pt_regs *regs = task_pt_regs(task);
297 * If the user value contains TF, mark that
298 * it was not "us" (the debugger) that set it.
299 * If not, make sure it stays set if we had.
301 if (value & X86_EFLAGS_TF)
302 clear_tsk_thread_flag(task, TIF_FORCED_TF);
303 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
304 value |= X86_EFLAGS_TF;
306 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
311 static int putreg(struct task_struct *child,
312 unsigned long offset, unsigned long value)
315 case offsetof(struct user_regs_struct, cs):
316 case offsetof(struct user_regs_struct, ds):
317 case offsetof(struct user_regs_struct, es):
318 case offsetof(struct user_regs_struct, fs):
319 case offsetof(struct user_regs_struct, gs):
320 case offsetof(struct user_regs_struct, ss):
321 return set_segment_reg(child, offset, value);
323 case offsetof(struct user_regs_struct, flags):
324 return set_flags(child, value);
328 * Orig_ax is really just a flag with small positive and
329 * negative values, so make sure to always sign-extend it
330 * from 32 bits so that it works correctly regardless of
331 * whether we come from a 32-bit environment or not.
333 case offsetof(struct user_regs_struct, orig_ax):
334 value = (long) (s32) value;
337 case offsetof(struct user_regs_struct,fs_base):
338 if (value >= TASK_SIZE_OF(child))
341 * When changing the segment base, use do_arch_prctl
342 * to set either thread.fs or thread.fsindex and the
343 * corresponding GDT slot.
345 if (child->thread.fs != value)
346 return do_arch_prctl(child, ARCH_SET_FS, value);
348 case offsetof(struct user_regs_struct,gs_base):
350 * Exactly the same here as the %fs handling above.
352 if (value >= TASK_SIZE_OF(child))
354 if (child->thread.gs != value)
355 return do_arch_prctl(child, ARCH_SET_GS, value);
360 *pt_regs_access(task_pt_regs(child), offset) = value;
364 static unsigned long getreg(struct task_struct *task, unsigned long offset)
367 case offsetof(struct user_regs_struct, cs):
368 case offsetof(struct user_regs_struct, ds):
369 case offsetof(struct user_regs_struct, es):
370 case offsetof(struct user_regs_struct, fs):
371 case offsetof(struct user_regs_struct, gs):
372 case offsetof(struct user_regs_struct, ss):
373 return get_segment_reg(task, offset);
375 case offsetof(struct user_regs_struct, flags):
376 return get_flags(task);
379 case offsetof(struct user_regs_struct, fs_base): {
381 * do_arch_prctl may have used a GDT slot instead of
382 * the MSR. To userland, it appears the same either
383 * way, except the %fs segment selector might not be 0.
385 unsigned int seg = task->thread.fsindex;
386 if (task->thread.fs != 0)
387 return task->thread.fs;
389 asm("movl %%fs,%0" : "=r" (seg));
390 if (seg != FS_TLS_SEL)
392 return get_desc_base(&task->thread.tls_array[FS_TLS]);
394 case offsetof(struct user_regs_struct, gs_base): {
396 * Exactly the same here as the %fs handling above.
398 unsigned int seg = task->thread.gsindex;
399 if (task->thread.gs != 0)
400 return task->thread.gs;
402 asm("movl %%gs,%0" : "=r" (seg));
403 if (seg != GS_TLS_SEL)
405 return get_desc_base(&task->thread.tls_array[GS_TLS]);
410 return *pt_regs_access(task_pt_regs(task), offset);
413 static int genregs_get(struct task_struct *target,
414 const struct user_regset *regset,
415 unsigned int pos, unsigned int count,
416 void *kbuf, void __user *ubuf)
419 unsigned long *k = kbuf;
421 *k++ = getreg(target, pos);
426 unsigned long __user *u = ubuf;
428 if (__put_user(getreg(target, pos), u++))
438 static int genregs_set(struct task_struct *target,
439 const struct user_regset *regset,
440 unsigned int pos, unsigned int count,
441 const void *kbuf, const void __user *ubuf)
445 const unsigned long *k = kbuf;
446 while (count > 0 && !ret) {
447 ret = putreg(target, pos, *k++);
452 const unsigned long __user *u = ubuf;
453 while (count > 0 && !ret) {
455 ret = __get_user(word, u++);
458 ret = putreg(target, pos, word);
467 * This function is trivial and will be inlined by the compiler.
468 * Having it separates the implementation details of debug
469 * registers from the interface details of ptrace.
471 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
474 case 0: return child->thread.debugreg0;
475 case 1: return child->thread.debugreg1;
476 case 2: return child->thread.debugreg2;
477 case 3: return child->thread.debugreg3;
478 case 6: return child->thread.debugreg6;
479 case 7: return child->thread.debugreg7;
484 static int ptrace_set_debugreg(struct task_struct *child,
485 int n, unsigned long data)
489 if (unlikely(n == 4 || n == 5))
492 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
496 case 0: child->thread.debugreg0 = data; break;
497 case 1: child->thread.debugreg1 = data; break;
498 case 2: child->thread.debugreg2 = data; break;
499 case 3: child->thread.debugreg3 = data; break;
502 if ((data & ~0xffffffffUL) != 0)
504 child->thread.debugreg6 = data;
509 * Sanity-check data. Take one half-byte at once with
510 * check = (val >> (16 + 4*i)) & 0xf. It contains the
511 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
512 * 2 and 3 are LENi. Given a list of invalid values,
513 * we do mask |= 1 << invalid_value, so that
514 * (mask >> check) & 1 is a correct test for invalid
517 * R/Wi contains the type of the breakpoint /
518 * watchpoint, LENi contains the length of the watched
519 * data in the watchpoint case.
521 * The invalid values are:
522 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
523 * - R/Wi == 0x10 (break on I/O reads or writes), so
525 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
530 * See the Intel Manual "System Programming Guide",
533 * Note that LENi == 0x10 is defined on x86_64 in long
534 * mode (i.e. even for 32-bit userspace software, but
535 * 64-bit kernel), so the x86_64 mask value is 0x5454.
536 * See the AMD manual no. 24593 (AMD64 System Programming)
539 #define DR7_MASK 0x5f54
541 #define DR7_MASK 0x5554
543 data &= ~DR_CONTROL_RESERVED;
544 for (i = 0; i < 4; i++)
545 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
547 child->thread.debugreg7 = data;
549 set_tsk_thread_flag(child, TIF_DEBUG);
551 clear_tsk_thread_flag(child, TIF_DEBUG);
560 static int ptrace_bts_get_size(struct task_struct *child)
562 if (!child->thread.ds_area_msr)
565 return ds_get_bts_index((void *)child->thread.ds_area_msr);
568 static int ptrace_bts_read_record(struct task_struct *child,
570 struct bts_struct __user *out)
572 struct bts_struct ret;
577 if (!child->thread.ds_area_msr)
583 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
584 if (bts_end <= index)
587 /* translate the ptrace bts index into the ds bts index */
588 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
589 bts_index -= (index + 1);
591 bts_index += bts_end;
593 retval = ds_read_bts((void *)child->thread.ds_area_msr,
598 if (copy_to_user(out, &ret, sizeof(ret)))
604 static int ptrace_bts_clear(struct task_struct *child)
606 if (!child->thread.ds_area_msr)
609 return ds_clear((void *)child->thread.ds_area_msr);
612 static int ptrace_bts_drain(struct task_struct *child,
614 struct bts_struct __user *out)
617 void *ds = (void *)child->thread.ds_area_msr;
622 end = ds_get_bts_index(ds);
626 if (size < (end * sizeof(struct bts_struct)))
629 for (i = 0; i < end; i++, out++) {
630 struct bts_struct ret;
633 retval = ds_read_bts(ds, i, &ret);
637 if (copy_to_user(out, &ret, sizeof(ret)))
646 static int ptrace_bts_config(struct task_struct *child,
648 const struct ptrace_bts_config __user *ucfg)
650 struct ptrace_bts_config cfg;
651 int bts_size, ret = 0;
654 if (cfg_size < sizeof(cfg))
657 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
660 if ((int)cfg.size < 0)
664 ds = (void *)child->thread.ds_area_msr;
666 bts_size = ds_get_bts_size(ds);
670 cfg.size = PAGE_ALIGN(cfg.size);
672 if (bts_size != cfg.size) {
673 ret = ptrace_bts_realloc(child, cfg.size,
674 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
678 ds = (void *)child->thread.ds_area_msr;
681 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
682 ret = ds_set_overflow(ds, DS_O_SIGNAL);
684 ret = ds_set_overflow(ds, DS_O_WRAP);
688 if (cfg.flags & PTRACE_BTS_O_TRACE)
689 child->thread.debugctlmsr |= ds_debugctl_mask();
691 child->thread.debugctlmsr &= ~ds_debugctl_mask();
693 if (cfg.flags & PTRACE_BTS_O_SCHED)
694 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
696 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
701 if (child->thread.debugctlmsr)
702 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
704 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
709 child->thread.debugctlmsr &= ~ds_debugctl_mask();
710 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
714 static int ptrace_bts_status(struct task_struct *child,
716 struct ptrace_bts_config __user *ucfg)
718 void *ds = (void *)child->thread.ds_area_msr;
719 struct ptrace_bts_config cfg;
721 if (cfg_size < sizeof(cfg))
724 memset(&cfg, 0, sizeof(cfg));
727 cfg.size = ds_get_bts_size(ds);
729 if (ds_get_overflow(ds) == DS_O_SIGNAL)
730 cfg.flags |= PTRACE_BTS_O_SIGNAL;
732 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
733 child->thread.debugctlmsr & ds_debugctl_mask())
734 cfg.flags |= PTRACE_BTS_O_TRACE;
736 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
737 cfg.flags |= PTRACE_BTS_O_SCHED;
740 cfg.bts_size = sizeof(struct bts_struct);
742 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
749 static int ptrace_bts_write_record(struct task_struct *child,
750 const struct bts_struct *in)
754 if (!child->thread.ds_area_msr)
757 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
764 static int ptrace_bts_realloc(struct task_struct *child,
765 int size, int reduce_size)
767 unsigned long rlim, vm;
773 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
777 ret = ds_free((void **)&child->thread.ds_area_msr);
782 old_size >>= PAGE_SHIFT;
784 current->mm->total_vm -= old_size;
785 current->mm->locked_vm -= old_size;
790 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
791 vm = current->mm->total_vm + size;
798 size = rlim - current->mm->total_vm;
803 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
804 vm = current->mm->locked_vm + size;
811 size = rlim - current->mm->locked_vm;
816 ret = ds_allocate((void **)&child->thread.ds_area_msr,
821 current->mm->total_vm += size;
822 current->mm->locked_vm += size;
825 if (child->thread.ds_area_msr)
826 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
828 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
833 void ptrace_bts_take_timestamp(struct task_struct *tsk,
834 enum bts_qualifier qualifier)
836 struct bts_struct rec = {
837 .qualifier = qualifier,
838 .variant.jiffies = jiffies_64
841 ptrace_bts_write_record(tsk, &rec);
846 * Called by kernel/ptrace.c when detaching..
848 * Make sure the single step bit is not set.
850 void ptrace_disable(struct task_struct *child)
852 user_disable_single_step(child);
853 #ifdef TIF_SYSCALL_EMU
854 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
856 if (child->thread.ds_area_msr) {
858 ptrace_bts_realloc(child, 0, 0);
860 child->thread.debugctlmsr &= ~ds_debugctl_mask();
861 if (!child->thread.debugctlmsr)
862 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
863 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
867 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
868 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
871 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
874 unsigned long __user *datap = (unsigned long __user *)data;
877 /* read the word at location addr in the USER area. */
878 case PTRACE_PEEKUSR: {
882 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
883 addr >= sizeof(struct user))
886 tmp = 0; /* Default return condition */
887 if (addr < sizeof(struct user_regs_struct))
888 tmp = getreg(child, addr);
889 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
890 addr <= offsetof(struct user, u_debugreg[7])) {
891 addr -= offsetof(struct user, u_debugreg[0]);
892 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
894 ret = put_user(tmp, datap);
898 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
900 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
901 addr >= sizeof(struct user))
904 if (addr < sizeof(struct user_regs_struct))
905 ret = putreg(child, addr, data);
906 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
907 addr <= offsetof(struct user, u_debugreg[7])) {
908 addr -= offsetof(struct user, u_debugreg[0]);
909 ret = ptrace_set_debugreg(child,
910 addr / sizeof(data), data);
914 case PTRACE_GETREGS: /* Get all gp regs from the child. */
915 return copy_regset_to_user(child,
916 task_user_regset_view(current),
918 0, sizeof(struct user_regs_struct),
921 case PTRACE_SETREGS: /* Set all gp regs in the child. */
922 return copy_regset_from_user(child,
923 task_user_regset_view(current),
925 0, sizeof(struct user_regs_struct),
928 case PTRACE_GETFPREGS: /* Get the child FPU state. */
929 return copy_regset_to_user(child,
930 task_user_regset_view(current),
932 0, sizeof(struct user_i387_struct),
935 case PTRACE_SETFPREGS: /* Set the child FPU state. */
936 return copy_regset_from_user(child,
937 task_user_regset_view(current),
939 0, sizeof(struct user_i387_struct),
943 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
944 return copy_regset_to_user(child, &user_x86_32_view,
946 0, sizeof(struct user_fxsr_struct),
949 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
950 return copy_regset_from_user(child, &user_x86_32_view,
952 0, sizeof(struct user_fxsr_struct),
956 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
957 case PTRACE_GET_THREAD_AREA:
960 ret = do_get_thread_area(child, addr,
961 (struct user_desc __user *) data);
964 case PTRACE_SET_THREAD_AREA:
967 ret = do_set_thread_area(child, addr,
968 (struct user_desc __user *) data, 0);
973 /* normal 64bit interface to access TLS data.
974 Works just like arch_prctl, except that the arguments
976 case PTRACE_ARCH_PRCTL:
977 ret = do_arch_prctl(child, data, addr);
982 * These bits need more cooking - not enabled yet:
985 case PTRACE_BTS_CONFIG:
986 ret = ptrace_bts_config
987 (child, data, (struct ptrace_bts_config __user *)addr);
990 case PTRACE_BTS_STATUS:
991 ret = ptrace_bts_status
992 (child, data, (struct ptrace_bts_config __user *)addr);
995 case PTRACE_BTS_SIZE:
996 ret = ptrace_bts_get_size(child);
1000 ret = ptrace_bts_read_record
1001 (child, data, (struct bts_struct __user *) addr);
1004 case PTRACE_BTS_CLEAR:
1005 ret = ptrace_bts_clear(child);
1008 case PTRACE_BTS_DRAIN:
1009 ret = ptrace_bts_drain
1010 (child, data, (struct bts_struct __user *) addr);
1015 ret = ptrace_request(child, request, addr, data);
1022 #ifdef CONFIG_IA32_EMULATION
1024 #include <linux/compat.h>
1025 #include <linux/syscalls.h>
1026 #include <asm/ia32.h>
1027 #include <asm/user32.h>
1030 case offsetof(struct user32, regs.l): \
1031 regs->q = value; break
1034 case offsetof(struct user32, regs.rs): \
1035 return set_segment_reg(child, \
1036 offsetof(struct user_regs_struct, rs), \
1040 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1042 struct pt_regs *regs = task_pt_regs(child);
1063 case offsetof(struct user32, regs.orig_eax):
1065 * Sign-extend the value so that orig_eax = -1
1066 * causes (long)orig_ax < 0 tests to fire correctly.
1068 regs->orig_ax = (long) (s32) value;
1071 case offsetof(struct user32, regs.eflags):
1072 return set_flags(child, value);
1074 case offsetof(struct user32, u_debugreg[0]) ...
1075 offsetof(struct user32, u_debugreg[7]):
1076 regno -= offsetof(struct user32, u_debugreg[0]);
1077 return ptrace_set_debugreg(child, regno / 4, value);
1080 if (regno > sizeof(struct user32) || (regno & 3))
1084 * Other dummy fields in the virtual user structure
1096 case offsetof(struct user32, regs.l): \
1097 *val = regs->q; break
1100 case offsetof(struct user32, regs.rs): \
1101 *val = get_segment_reg(child, \
1102 offsetof(struct user_regs_struct, rs)); \
1105 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1107 struct pt_regs *regs = task_pt_regs(child);
1125 R32(orig_eax, orig_ax);
1129 case offsetof(struct user32, regs.eflags):
1130 *val = get_flags(child);
1133 case offsetof(struct user32, u_debugreg[0]) ...
1134 offsetof(struct user32, u_debugreg[7]):
1135 regno -= offsetof(struct user32, u_debugreg[0]);
1136 *val = ptrace_get_debugreg(child, regno / 4);
1140 if (regno > sizeof(struct user32) || (regno & 3))
1144 * Other dummy fields in the virtual user structure
1156 static int genregs32_get(struct task_struct *target,
1157 const struct user_regset *regset,
1158 unsigned int pos, unsigned int count,
1159 void *kbuf, void __user *ubuf)
1162 compat_ulong_t *k = kbuf;
1164 getreg32(target, pos, k++);
1165 count -= sizeof(*k);
1169 compat_ulong_t __user *u = ubuf;
1171 compat_ulong_t word;
1172 getreg32(target, pos, &word);
1173 if (__put_user(word, u++))
1175 count -= sizeof(*u);
1183 static int genregs32_set(struct task_struct *target,
1184 const struct user_regset *regset,
1185 unsigned int pos, unsigned int count,
1186 const void *kbuf, const void __user *ubuf)
1190 const compat_ulong_t *k = kbuf;
1191 while (count > 0 && !ret) {
1192 ret = putreg32(target, pos, *k++);
1193 count -= sizeof(*k);
1197 const compat_ulong_t __user *u = ubuf;
1198 while (count > 0 && !ret) {
1199 compat_ulong_t word;
1200 ret = __get_user(word, u++);
1203 ret = putreg32(target, pos, word);
1204 count -= sizeof(*u);
1211 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1212 compat_ulong_t caddr, compat_ulong_t cdata)
1214 unsigned long addr = caddr;
1215 unsigned long data = cdata;
1216 void __user *datap = compat_ptr(data);
1221 case PTRACE_PEEKUSR:
1222 ret = getreg32(child, addr, &val);
1224 ret = put_user(val, (__u32 __user *)datap);
1227 case PTRACE_POKEUSR:
1228 ret = putreg32(child, addr, data);
1231 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1232 return copy_regset_to_user(child, &user_x86_32_view,
1234 0, sizeof(struct user_regs_struct32),
1237 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1238 return copy_regset_from_user(child, &user_x86_32_view,
1240 sizeof(struct user_regs_struct32),
1243 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1244 return copy_regset_to_user(child, &user_x86_32_view,
1246 sizeof(struct user_i387_ia32_struct),
1249 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1250 return copy_regset_from_user(
1251 child, &user_x86_32_view, REGSET_FP,
1252 0, sizeof(struct user_i387_ia32_struct), datap);
1254 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1255 return copy_regset_to_user(child, &user_x86_32_view,
1257 sizeof(struct user32_fxsr_struct),
1260 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1261 return copy_regset_from_user(child, &user_x86_32_view,
1263 sizeof(struct user32_fxsr_struct),
1266 case PTRACE_GET_THREAD_AREA:
1267 case PTRACE_SET_THREAD_AREA:
1268 return arch_ptrace(child, request, addr, data);
1271 return compat_ptrace_request(child, request, addr, data);
1277 #endif /* CONFIG_IA32_EMULATION */
1279 #ifdef CONFIG_X86_64
1281 static const struct user_regset x86_64_regsets[] = {
1282 [REGSET_GENERAL] = {
1283 .core_note_type = NT_PRSTATUS,
1284 .n = sizeof(struct user_regs_struct) / sizeof(long),
1285 .size = sizeof(long), .align = sizeof(long),
1286 .get = genregs_get, .set = genregs_set
1289 .core_note_type = NT_PRFPREG,
1290 .n = sizeof(struct user_i387_struct) / sizeof(long),
1291 .size = sizeof(long), .align = sizeof(long),
1292 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1296 static const struct user_regset_view user_x86_64_view = {
1297 .name = "x86_64", .e_machine = EM_X86_64,
1298 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1301 #else /* CONFIG_X86_32 */
1303 #define user_regs_struct32 user_regs_struct
1304 #define genregs32_get genregs_get
1305 #define genregs32_set genregs_set
1307 #define user_i387_ia32_struct user_i387_struct
1308 #define user32_fxsr_struct user_fxsr_struct
1310 #endif /* CONFIG_X86_64 */
1312 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1313 static const struct user_regset x86_32_regsets[] = {
1314 [REGSET_GENERAL] = {
1315 .core_note_type = NT_PRSTATUS,
1316 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1317 .size = sizeof(u32), .align = sizeof(u32),
1318 .get = genregs32_get, .set = genregs32_set
1321 .core_note_type = NT_PRFPREG,
1322 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1323 .size = sizeof(u32), .align = sizeof(u32),
1324 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1327 .core_note_type = NT_PRXFPREG,
1328 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1329 .size = sizeof(u32), .align = sizeof(u32),
1330 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1333 .core_note_type = NT_386_TLS,
1334 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1335 .size = sizeof(struct user_desc),
1336 .align = sizeof(struct user_desc),
1337 .active = regset_tls_active,
1338 .get = regset_tls_get, .set = regset_tls_set
1342 static const struct user_regset_view user_x86_32_view = {
1343 .name = "i386", .e_machine = EM_386,
1344 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1350 #ifdef CONFIG_IA32_EMULATION
1351 if (test_tsk_thread_flag(task, TIF_IA32))
1353 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 return &user_x86_32_view;
1356 #ifdef CONFIG_X86_64
1357 return &user_x86_64_view;
1361 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1363 struct siginfo info;
1365 tsk->thread.trap_no = 1;
1366 tsk->thread.error_code = error_code;
1368 memset(&info, 0, sizeof(info));
1369 info.si_signo = SIGTRAP;
1370 info.si_code = TRAP_BRKPT;
1373 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1375 /* Send us the fake SIGTRAP */
1376 force_sig_info(SIGTRAP, &info, tsk);
1380 #ifdef CONFIG_X86_32
1382 #elif defined CONFIG_IA32_EMULATION
1383 # define IS_IA32 test_thread_flag(TIF_IA32)
1389 * We must return the syscall number to actually look up in the table.
1390 * This can be -1L to skip running any syscall at all.
1392 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1397 * If we stepped into a sysenter/syscall insn, it trapped in
1398 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1399 * If user-mode had set TF itself, then it's still clear from
1400 * do_debug() and we need to set it again to restore the user
1401 * state. If we entered on the slow path, TF was already set.
1403 if (test_thread_flag(TIF_SINGLESTEP))
1404 regs->flags |= X86_EFLAGS_TF;
1406 /* do the secure computing check first */
1407 secure_computing(regs->orig_ax);
1409 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1412 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1413 tracehook_report_syscall_entry(regs))
1416 if (unlikely(current->audit_context)) {
1418 audit_syscall_entry(AUDIT_ARCH_I386,
1421 regs->dx, regs->si);
1422 #ifdef CONFIG_X86_64
1424 audit_syscall_entry(AUDIT_ARCH_X86_64,
1427 regs->dx, regs->r10);
1431 return ret ?: regs->orig_ax;
1434 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1436 if (unlikely(current->audit_context))
1437 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1439 if (test_thread_flag(TIF_SYSCALL_TRACE))
1440 tracehook_report_syscall_exit(regs, 0);
1443 * If TIF_SYSCALL_EMU is set, we only get here because of
1444 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1445 * We already reported this syscall instruction in
1446 * syscall_trace_enter(), so don't do any more now.
1448 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1452 * If we are single-stepping, synthesize a trap to follow the
1453 * system call instruction.
1455 if (test_thread_flag(TIF_SINGLESTEP) &&
1456 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1457 send_sigtrap(current, regs, 0);