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>
43 REGSET_IOPERM64 = REGSET_XFP,
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
54 * Determines which flags the user has access to [1 = access, 0 = no access].
56 #define FLAG_MASK_32 ((unsigned long) \
57 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
58 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
59 X86_EFLAGS_SF | X86_EFLAGS_TF | \
60 X86_EFLAGS_DF | X86_EFLAGS_OF | \
61 X86_EFLAGS_RF | X86_EFLAGS_AC))
64 * Determines whether a value may be installed in a segment register.
66 static inline bool invalid_selector(u16 value)
68 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
73 #define FLAG_MASK FLAG_MASK_32
75 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
77 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
81 return ®s->bx + regno;
84 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
87 * Returning the value truncates it to 16 bits.
90 if (offset != offsetof(struct user_regs_struct, gs))
91 retval = *pt_regs_access(task_pt_regs(task), offset);
94 retval = get_user_gs(task_pt_regs(task));
96 retval = task_user_gs(task);
101 static int set_segment_reg(struct task_struct *task,
102 unsigned long offset, u16 value)
105 * The value argument was already truncated to 16 bits.
107 if (invalid_selector(value))
111 * For %cs and %ss we cannot permit a null selector.
112 * We can permit a bogus selector as long as it has USER_RPL.
113 * Null selectors are fine for other segment registers, but
114 * we will never get back to user mode with invalid %cs or %ss
115 * and will take the trap in iret instead. Much code relies
116 * on user_mode() to distinguish a user trap frame (which can
117 * safely use invalid selectors) from a kernel trap frame.
120 case offsetof(struct user_regs_struct, cs):
121 case offsetof(struct user_regs_struct, ss):
122 if (unlikely(value == 0))
126 *pt_regs_access(task_pt_regs(task), offset) = value;
129 case offsetof(struct user_regs_struct, gs):
131 set_user_gs(task_pt_regs(task), value);
133 task_user_gs(task) = 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);
559 * These access the current or another (stopped) task's io permission
560 * bitmap for debugging or core dump.
562 static int ioperm_active(struct task_struct *target,
563 const struct user_regset *regset)
565 return target->thread.io_bitmap_max / regset->size;
568 static int ioperm_get(struct task_struct *target,
569 const struct user_regset *regset,
570 unsigned int pos, unsigned int count,
571 void *kbuf, void __user *ubuf)
573 if (!target->thread.io_bitmap_ptr)
576 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
577 target->thread.io_bitmap_ptr,
581 #ifdef CONFIG_X86_PTRACE_BTS
582 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
583 struct bts_struct __user *out)
585 const struct bts_trace *trace;
586 struct bts_struct bts;
587 const unsigned char *at;
590 trace = ds_read_bts(child->bts);
594 at = trace->ds.top - ((index + 1) * trace->ds.size);
595 if ((void *)at < trace->ds.begin)
596 at += (trace->ds.n * trace->ds.size);
601 error = trace->read(child->bts, at, &bts);
605 if (copy_to_user(out, &bts, sizeof(bts)))
611 static int ptrace_bts_drain(struct task_struct *child,
613 struct bts_struct __user *out)
615 const struct bts_trace *trace;
616 const unsigned char *at;
617 int error, drained = 0;
619 trace = ds_read_bts(child->bts);
626 if (size < (trace->ds.top - trace->ds.begin))
629 for (at = trace->ds.begin; (void *)at < trace->ds.top;
630 out++, drained++, at += trace->ds.size) {
631 struct bts_struct bts;
634 error = trace->read(child->bts, at, &bts);
638 if (copy_to_user(out, &bts, sizeof(bts)))
642 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
644 error = ds_reset_bts(child->bts);
651 static int ptrace_bts_allocate_buffer(struct task_struct *child, size_t size)
653 child->bts_buffer = alloc_locked_buffer(size);
654 if (!child->bts_buffer)
657 child->bts_size = size;
662 static void ptrace_bts_free_buffer(struct task_struct *child)
664 free_locked_buffer(child->bts_buffer, child->bts_size);
665 child->bts_buffer = NULL;
669 static int ptrace_bts_config(struct task_struct *child,
671 const struct ptrace_bts_config __user *ucfg)
673 struct ptrace_bts_config cfg;
674 unsigned int flags = 0;
676 if (cfg_size < sizeof(cfg))
679 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
683 ds_release_bts(child->bts);
687 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
693 child->thread.bts_ovfl_signal = cfg.signal;
696 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
697 (cfg.size != child->bts_size)) {
700 ptrace_bts_free_buffer(child);
702 error = ptrace_bts_allocate_buffer(child, cfg.size);
707 if (cfg.flags & PTRACE_BTS_O_TRACE)
710 if (cfg.flags & PTRACE_BTS_O_SCHED)
711 flags |= BTS_TIMESTAMPS;
713 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
714 /* ovfl = */ NULL, /* th = */ (size_t)-1,
716 if (IS_ERR(child->bts)) {
717 int error = PTR_ERR(child->bts);
719 ptrace_bts_free_buffer(child);
728 static int ptrace_bts_status(struct task_struct *child,
730 struct ptrace_bts_config __user *ucfg)
732 const struct bts_trace *trace;
733 struct ptrace_bts_config cfg;
735 if (cfg_size < sizeof(cfg))
738 trace = ds_read_bts(child->bts);
742 memset(&cfg, 0, sizeof(cfg));
743 cfg.size = trace->ds.end - trace->ds.begin;
744 cfg.signal = child->thread.bts_ovfl_signal;
745 cfg.bts_size = sizeof(struct bts_struct);
748 cfg.flags |= PTRACE_BTS_O_SIGNAL;
750 if (trace->ds.flags & BTS_USER)
751 cfg.flags |= PTRACE_BTS_O_TRACE;
753 if (trace->ds.flags & BTS_TIMESTAMPS)
754 cfg.flags |= PTRACE_BTS_O_SCHED;
756 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
762 static int ptrace_bts_clear(struct task_struct *child)
764 const struct bts_trace *trace;
766 trace = ds_read_bts(child->bts);
770 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
772 return ds_reset_bts(child->bts);
775 static int ptrace_bts_size(struct task_struct *child)
777 const struct bts_trace *trace;
779 trace = ds_read_bts(child->bts);
783 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
786 static void ptrace_bts_fork(struct task_struct *tsk)
789 tsk->bts_buffer = NULL;
791 tsk->thread.bts_ovfl_signal = 0;
794 static void ptrace_bts_untrace(struct task_struct *child)
796 if (unlikely(child->bts)) {
797 ds_release_bts(child->bts);
800 /* We cannot update total_vm and locked_vm since
801 child's mm is already gone. But we can reclaim the
803 kfree(child->bts_buffer);
804 child->bts_buffer = NULL;
809 static void ptrace_bts_detach(struct task_struct *child)
811 if (unlikely(child->bts)) {
812 ds_release_bts(child->bts);
815 ptrace_bts_free_buffer(child);
819 static inline void ptrace_bts_fork(struct task_struct *tsk) {}
820 static inline void ptrace_bts_detach(struct task_struct *child) {}
821 static inline void ptrace_bts_untrace(struct task_struct *child) {}
822 #endif /* CONFIG_X86_PTRACE_BTS */
824 void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
826 ptrace_bts_fork(child);
829 void x86_ptrace_untrace(struct task_struct *child)
831 ptrace_bts_untrace(child);
835 * Called by kernel/ptrace.c when detaching..
837 * Make sure the single step bit is not set.
839 void ptrace_disable(struct task_struct *child)
841 user_disable_single_step(child);
842 #ifdef TIF_SYSCALL_EMU
843 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
845 ptrace_bts_detach(child);
848 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
849 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
852 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
855 unsigned long __user *datap = (unsigned long __user *)data;
858 /* read the word at location addr in the USER area. */
859 case PTRACE_PEEKUSR: {
863 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
864 addr >= sizeof(struct user))
867 tmp = 0; /* Default return condition */
868 if (addr < sizeof(struct user_regs_struct))
869 tmp = getreg(child, addr);
870 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
871 addr <= offsetof(struct user, u_debugreg[7])) {
872 addr -= offsetof(struct user, u_debugreg[0]);
873 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
875 ret = put_user(tmp, datap);
879 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
881 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
882 addr >= sizeof(struct user))
885 if (addr < sizeof(struct user_regs_struct))
886 ret = putreg(child, addr, data);
887 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
888 addr <= offsetof(struct user, u_debugreg[7])) {
889 addr -= offsetof(struct user, u_debugreg[0]);
890 ret = ptrace_set_debugreg(child,
891 addr / sizeof(data), data);
895 case PTRACE_GETREGS: /* Get all gp regs from the child. */
896 return copy_regset_to_user(child,
897 task_user_regset_view(current),
899 0, sizeof(struct user_regs_struct),
902 case PTRACE_SETREGS: /* Set all gp regs in the child. */
903 return copy_regset_from_user(child,
904 task_user_regset_view(current),
906 0, sizeof(struct user_regs_struct),
909 case PTRACE_GETFPREGS: /* Get the child FPU state. */
910 return copy_regset_to_user(child,
911 task_user_regset_view(current),
913 0, sizeof(struct user_i387_struct),
916 case PTRACE_SETFPREGS: /* Set the child FPU state. */
917 return copy_regset_from_user(child,
918 task_user_regset_view(current),
920 0, sizeof(struct user_i387_struct),
924 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
925 return copy_regset_to_user(child, &user_x86_32_view,
927 0, sizeof(struct user_fxsr_struct),
930 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
931 return copy_regset_from_user(child, &user_x86_32_view,
933 0, sizeof(struct user_fxsr_struct),
937 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
938 case PTRACE_GET_THREAD_AREA:
941 ret = do_get_thread_area(child, addr,
942 (struct user_desc __user *) data);
945 case PTRACE_SET_THREAD_AREA:
948 ret = do_set_thread_area(child, addr,
949 (struct user_desc __user *) data, 0);
954 /* normal 64bit interface to access TLS data.
955 Works just like arch_prctl, except that the arguments
957 case PTRACE_ARCH_PRCTL:
958 ret = do_arch_prctl(child, data, addr);
963 * These bits need more cooking - not enabled yet:
965 #ifdef CONFIG_X86_PTRACE_BTS
966 case PTRACE_BTS_CONFIG:
967 ret = ptrace_bts_config
968 (child, data, (struct ptrace_bts_config __user *)addr);
971 case PTRACE_BTS_STATUS:
972 ret = ptrace_bts_status
973 (child, data, (struct ptrace_bts_config __user *)addr);
976 case PTRACE_BTS_SIZE:
977 ret = ptrace_bts_size(child);
981 ret = ptrace_bts_read_record
982 (child, data, (struct bts_struct __user *) addr);
985 case PTRACE_BTS_CLEAR:
986 ret = ptrace_bts_clear(child);
989 case PTRACE_BTS_DRAIN:
990 ret = ptrace_bts_drain
991 (child, data, (struct bts_struct __user *) addr);
993 #endif /* CONFIG_X86_PTRACE_BTS */
996 ret = ptrace_request(child, request, addr, data);
1003 #ifdef CONFIG_IA32_EMULATION
1005 #include <linux/compat.h>
1006 #include <linux/syscalls.h>
1007 #include <asm/ia32.h>
1008 #include <asm/user32.h>
1011 case offsetof(struct user32, regs.l): \
1012 regs->q = value; break
1015 case offsetof(struct user32, regs.rs): \
1016 return set_segment_reg(child, \
1017 offsetof(struct user_regs_struct, rs), \
1021 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1023 struct pt_regs *regs = task_pt_regs(child);
1044 case offsetof(struct user32, regs.orig_eax):
1046 * Sign-extend the value so that orig_eax = -1
1047 * causes (long)orig_ax < 0 tests to fire correctly.
1049 regs->orig_ax = (long) (s32) value;
1052 case offsetof(struct user32, regs.eflags):
1053 return set_flags(child, value);
1055 case offsetof(struct user32, u_debugreg[0]) ...
1056 offsetof(struct user32, u_debugreg[7]):
1057 regno -= offsetof(struct user32, u_debugreg[0]);
1058 return ptrace_set_debugreg(child, regno / 4, value);
1061 if (regno > sizeof(struct user32) || (regno & 3))
1065 * Other dummy fields in the virtual user structure
1077 case offsetof(struct user32, regs.l): \
1078 *val = regs->q; break
1081 case offsetof(struct user32, regs.rs): \
1082 *val = get_segment_reg(child, \
1083 offsetof(struct user_regs_struct, rs)); \
1086 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1088 struct pt_regs *regs = task_pt_regs(child);
1106 R32(orig_eax, orig_ax);
1110 case offsetof(struct user32, regs.eflags):
1111 *val = get_flags(child);
1114 case offsetof(struct user32, u_debugreg[0]) ...
1115 offsetof(struct user32, u_debugreg[7]):
1116 regno -= offsetof(struct user32, u_debugreg[0]);
1117 *val = ptrace_get_debugreg(child, regno / 4);
1121 if (regno > sizeof(struct user32) || (regno & 3))
1125 * Other dummy fields in the virtual user structure
1137 static int genregs32_get(struct task_struct *target,
1138 const struct user_regset *regset,
1139 unsigned int pos, unsigned int count,
1140 void *kbuf, void __user *ubuf)
1143 compat_ulong_t *k = kbuf;
1145 getreg32(target, pos, k++);
1146 count -= sizeof(*k);
1150 compat_ulong_t __user *u = ubuf;
1152 compat_ulong_t word;
1153 getreg32(target, pos, &word);
1154 if (__put_user(word, u++))
1156 count -= sizeof(*u);
1164 static int genregs32_set(struct task_struct *target,
1165 const struct user_regset *regset,
1166 unsigned int pos, unsigned int count,
1167 const void *kbuf, const void __user *ubuf)
1171 const compat_ulong_t *k = kbuf;
1172 while (count > 0 && !ret) {
1173 ret = putreg32(target, pos, *k++);
1174 count -= sizeof(*k);
1178 const compat_ulong_t __user *u = ubuf;
1179 while (count > 0 && !ret) {
1180 compat_ulong_t word;
1181 ret = __get_user(word, u++);
1184 ret = putreg32(target, pos, word);
1185 count -= sizeof(*u);
1192 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1193 compat_ulong_t caddr, compat_ulong_t cdata)
1195 unsigned long addr = caddr;
1196 unsigned long data = cdata;
1197 void __user *datap = compat_ptr(data);
1202 case PTRACE_PEEKUSR:
1203 ret = getreg32(child, addr, &val);
1205 ret = put_user(val, (__u32 __user *)datap);
1208 case PTRACE_POKEUSR:
1209 ret = putreg32(child, addr, data);
1212 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1213 return copy_regset_to_user(child, &user_x86_32_view,
1215 0, sizeof(struct user_regs_struct32),
1218 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1219 return copy_regset_from_user(child, &user_x86_32_view,
1221 sizeof(struct user_regs_struct32),
1224 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1225 return copy_regset_to_user(child, &user_x86_32_view,
1227 sizeof(struct user_i387_ia32_struct),
1230 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1231 return copy_regset_from_user(
1232 child, &user_x86_32_view, REGSET_FP,
1233 0, sizeof(struct user_i387_ia32_struct), datap);
1235 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1236 return copy_regset_to_user(child, &user_x86_32_view,
1238 sizeof(struct user32_fxsr_struct),
1241 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1242 return copy_regset_from_user(child, &user_x86_32_view,
1244 sizeof(struct user32_fxsr_struct),
1247 case PTRACE_GET_THREAD_AREA:
1248 case PTRACE_SET_THREAD_AREA:
1249 #ifdef CONFIG_X86_PTRACE_BTS
1250 case PTRACE_BTS_CONFIG:
1251 case PTRACE_BTS_STATUS:
1252 case PTRACE_BTS_SIZE:
1253 case PTRACE_BTS_GET:
1254 case PTRACE_BTS_CLEAR:
1255 case PTRACE_BTS_DRAIN:
1256 #endif /* CONFIG_X86_PTRACE_BTS */
1257 return arch_ptrace(child, request, addr, data);
1260 return compat_ptrace_request(child, request, addr, data);
1266 #endif /* CONFIG_IA32_EMULATION */
1268 #ifdef CONFIG_X86_64
1270 static const struct user_regset x86_64_regsets[] = {
1271 [REGSET_GENERAL] = {
1272 .core_note_type = NT_PRSTATUS,
1273 .n = sizeof(struct user_regs_struct) / sizeof(long),
1274 .size = sizeof(long), .align = sizeof(long),
1275 .get = genregs_get, .set = genregs_set
1278 .core_note_type = NT_PRFPREG,
1279 .n = sizeof(struct user_i387_struct) / sizeof(long),
1280 .size = sizeof(long), .align = sizeof(long),
1281 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1283 [REGSET_IOPERM64] = {
1284 .core_note_type = NT_386_IOPERM,
1285 .n = IO_BITMAP_LONGS,
1286 .size = sizeof(long), .align = sizeof(long),
1287 .active = ioperm_active, .get = ioperm_get
1291 static const struct user_regset_view user_x86_64_view = {
1292 .name = "x86_64", .e_machine = EM_X86_64,
1293 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1296 #else /* CONFIG_X86_32 */
1298 #define user_regs_struct32 user_regs_struct
1299 #define genregs32_get genregs_get
1300 #define genregs32_set genregs_set
1302 #define user_i387_ia32_struct user_i387_struct
1303 #define user32_fxsr_struct user_fxsr_struct
1305 #endif /* CONFIG_X86_64 */
1307 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1308 static const struct user_regset x86_32_regsets[] = {
1309 [REGSET_GENERAL] = {
1310 .core_note_type = NT_PRSTATUS,
1311 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1312 .size = sizeof(u32), .align = sizeof(u32),
1313 .get = genregs32_get, .set = genregs32_set
1316 .core_note_type = NT_PRFPREG,
1317 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1318 .size = sizeof(u32), .align = sizeof(u32),
1319 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1322 .core_note_type = NT_PRXFPREG,
1323 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1324 .size = sizeof(u32), .align = sizeof(u32),
1325 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1328 .core_note_type = NT_386_TLS,
1329 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1330 .size = sizeof(struct user_desc),
1331 .align = sizeof(struct user_desc),
1332 .active = regset_tls_active,
1333 .get = regset_tls_get, .set = regset_tls_set
1335 [REGSET_IOPERM32] = {
1336 .core_note_type = NT_386_IOPERM,
1337 .n = IO_BITMAP_BYTES / sizeof(u32),
1338 .size = sizeof(u32), .align = sizeof(u32),
1339 .active = ioperm_active, .get = ioperm_get
1343 static const struct user_regset_view user_x86_32_view = {
1344 .name = "i386", .e_machine = EM_386,
1345 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1349 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1351 #ifdef CONFIG_IA32_EMULATION
1352 if (test_tsk_thread_flag(task, TIF_IA32))
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 struct siginfo info;
1367 tsk->thread.trap_no = 1;
1368 tsk->thread.error_code = error_code;
1370 memset(&info, 0, sizeof(info));
1371 info.si_signo = SIGTRAP;
1372 info.si_code = si_code;
1375 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1377 /* Send us the fake SIGTRAP */
1378 force_sig_info(SIGTRAP, &info, tsk);
1382 #ifdef CONFIG_X86_32
1384 #elif defined CONFIG_IA32_EMULATION
1385 # define IS_IA32 test_thread_flag(TIF_IA32)
1391 * We must return the syscall number to actually look up in the table.
1392 * This can be -1L to skip running any syscall at all.
1394 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1399 * If we stepped into a sysenter/syscall insn, it trapped in
1400 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1401 * If user-mode had set TF itself, then it's still clear from
1402 * do_debug() and we need to set it again to restore the user
1403 * state. If we entered on the slow path, TF was already set.
1405 if (test_thread_flag(TIF_SINGLESTEP))
1406 regs->flags |= X86_EFLAGS_TF;
1408 /* do the secure computing check first */
1409 secure_computing(regs->orig_ax);
1411 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1414 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1415 tracehook_report_syscall_entry(regs))
1418 if (unlikely(current->audit_context)) {
1420 audit_syscall_entry(AUDIT_ARCH_I386,
1423 regs->dx, regs->si);
1424 #ifdef CONFIG_X86_64
1426 audit_syscall_entry(AUDIT_ARCH_X86_64,
1429 regs->dx, regs->r10);
1433 return ret ?: regs->orig_ax;
1436 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1438 if (unlikely(current->audit_context))
1439 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1441 if (test_thread_flag(TIF_SYSCALL_TRACE))
1442 tracehook_report_syscall_exit(regs, 0);
1445 * If TIF_SYSCALL_EMU is set, we only get here because of
1446 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1447 * We already reported this syscall instruction in
1448 * syscall_trace_enter(), so don't do any more now.
1450 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1454 * If we are single-stepping, synthesize a trap to follow the
1455 * system call instruction.
1457 if (test_thread_flag(TIF_SINGLESTEP) &&
1458 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1459 send_sigtrap(current, regs, 0, TRAP_BRKPT);