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);
93 retval = task->thread.gs;
95 savesegment(gs, retval);
100 static int set_segment_reg(struct task_struct *task,
101 unsigned long offset, u16 value)
104 * The value argument was already truncated to 16 bits.
106 if (invalid_selector(value))
110 * For %cs and %ss we cannot permit a null selector.
111 * We can permit a bogus selector as long as it has USER_RPL.
112 * Null selectors are fine for other segment registers, but
113 * we will never get back to user mode with invalid %cs or %ss
114 * and will take the trap in iret instead. Much code relies
115 * on user_mode() to distinguish a user trap frame (which can
116 * safely use invalid selectors) from a kernel trap frame.
119 case offsetof(struct user_regs_struct, cs):
120 case offsetof(struct user_regs_struct, ss):
121 if (unlikely(value == 0))
125 *pt_regs_access(task_pt_regs(task), offset) = value;
128 case offsetof(struct user_regs_struct, gs):
129 task->thread.gs = value;
132 * The user-mode %gs is not affected by
133 * kernel entry, so we must update the CPU.
135 loadsegment(gs, value);
141 static unsigned long debugreg_addr_limit(struct task_struct *task)
143 return TASK_SIZE - 3;
146 #else /* CONFIG_X86_64 */
148 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
150 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
152 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
153 return ®s->r15 + (offset / sizeof(regs->r15));
156 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
159 * Returning the value truncates it to 16 bits.
164 case offsetof(struct user_regs_struct, fs):
165 if (task == current) {
166 /* Older gas can't assemble movq %?s,%r?? */
167 asm("movl %%fs,%0" : "=r" (seg));
170 return task->thread.fsindex;
171 case offsetof(struct user_regs_struct, gs):
172 if (task == current) {
173 asm("movl %%gs,%0" : "=r" (seg));
176 return task->thread.gsindex;
177 case offsetof(struct user_regs_struct, ds):
178 if (task == current) {
179 asm("movl %%ds,%0" : "=r" (seg));
182 return task->thread.ds;
183 case offsetof(struct user_regs_struct, es):
184 if (task == current) {
185 asm("movl %%es,%0" : "=r" (seg));
188 return task->thread.es;
190 case offsetof(struct user_regs_struct, cs):
191 case offsetof(struct user_regs_struct, ss):
194 return *pt_regs_access(task_pt_regs(task), offset);
197 static int set_segment_reg(struct task_struct *task,
198 unsigned long offset, u16 value)
201 * The value argument was already truncated to 16 bits.
203 if (invalid_selector(value))
207 case offsetof(struct user_regs_struct,fs):
209 * If this is setting fs as for normal 64-bit use but
210 * setting fs_base has implicitly changed it, leave it.
212 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
213 task->thread.fs != 0) ||
214 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
215 task->thread.fs == 0))
217 task->thread.fsindex = value;
219 loadsegment(fs, task->thread.fsindex);
221 case offsetof(struct user_regs_struct,gs):
223 * If this is setting gs as for normal 64-bit use but
224 * setting gs_base has implicitly changed it, leave it.
226 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
227 task->thread.gs != 0) ||
228 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
229 task->thread.gs == 0))
231 task->thread.gsindex = value;
233 load_gs_index(task->thread.gsindex);
235 case offsetof(struct user_regs_struct,ds):
236 task->thread.ds = value;
238 loadsegment(ds, task->thread.ds);
240 case offsetof(struct user_regs_struct,es):
241 task->thread.es = value;
243 loadsegment(es, task->thread.es);
247 * Can't actually change these in 64-bit mode.
249 case offsetof(struct user_regs_struct,cs):
250 if (unlikely(value == 0))
252 #ifdef CONFIG_IA32_EMULATION
253 if (test_tsk_thread_flag(task, TIF_IA32))
254 task_pt_regs(task)->cs = value;
257 case offsetof(struct user_regs_struct,ss):
258 if (unlikely(value == 0))
260 #ifdef CONFIG_IA32_EMULATION
261 if (test_tsk_thread_flag(task, TIF_IA32))
262 task_pt_regs(task)->ss = value;
270 static unsigned long debugreg_addr_limit(struct task_struct *task)
272 #ifdef CONFIG_IA32_EMULATION
273 if (test_tsk_thread_flag(task, TIF_IA32))
274 return IA32_PAGE_OFFSET - 3;
276 return TASK_SIZE64 - 7;
279 #endif /* CONFIG_X86_32 */
281 static unsigned long get_flags(struct task_struct *task)
283 unsigned long retval = task_pt_regs(task)->flags;
286 * If the debugger set TF, hide it from the readout.
288 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
289 retval &= ~X86_EFLAGS_TF;
294 static int set_flags(struct task_struct *task, unsigned long value)
296 struct pt_regs *regs = task_pt_regs(task);
299 * If the user value contains TF, mark that
300 * it was not "us" (the debugger) that set it.
301 * If not, make sure it stays set if we had.
303 if (value & X86_EFLAGS_TF)
304 clear_tsk_thread_flag(task, TIF_FORCED_TF);
305 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
306 value |= X86_EFLAGS_TF;
308 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
313 static int putreg(struct task_struct *child,
314 unsigned long offset, unsigned long value)
317 case offsetof(struct user_regs_struct, cs):
318 case offsetof(struct user_regs_struct, ds):
319 case offsetof(struct user_regs_struct, es):
320 case offsetof(struct user_regs_struct, fs):
321 case offsetof(struct user_regs_struct, gs):
322 case offsetof(struct user_regs_struct, ss):
323 return set_segment_reg(child, offset, value);
325 case offsetof(struct user_regs_struct, flags):
326 return set_flags(child, value);
330 * Orig_ax is really just a flag with small positive and
331 * negative values, so make sure to always sign-extend it
332 * from 32 bits so that it works correctly regardless of
333 * whether we come from a 32-bit environment or not.
335 case offsetof(struct user_regs_struct, orig_ax):
336 value = (long) (s32) value;
339 case offsetof(struct user_regs_struct,fs_base):
340 if (value >= TASK_SIZE_OF(child))
343 * When changing the segment base, use do_arch_prctl
344 * to set either thread.fs or thread.fsindex and the
345 * corresponding GDT slot.
347 if (child->thread.fs != value)
348 return do_arch_prctl(child, ARCH_SET_FS, value);
350 case offsetof(struct user_regs_struct,gs_base):
352 * Exactly the same here as the %fs handling above.
354 if (value >= TASK_SIZE_OF(child))
356 if (child->thread.gs != value)
357 return do_arch_prctl(child, ARCH_SET_GS, value);
362 *pt_regs_access(task_pt_regs(child), offset) = value;
366 static unsigned long getreg(struct task_struct *task, unsigned long offset)
369 case offsetof(struct user_regs_struct, cs):
370 case offsetof(struct user_regs_struct, ds):
371 case offsetof(struct user_regs_struct, es):
372 case offsetof(struct user_regs_struct, fs):
373 case offsetof(struct user_regs_struct, gs):
374 case offsetof(struct user_regs_struct, ss):
375 return get_segment_reg(task, offset);
377 case offsetof(struct user_regs_struct, flags):
378 return get_flags(task);
381 case offsetof(struct user_regs_struct, fs_base): {
383 * do_arch_prctl may have used a GDT slot instead of
384 * the MSR. To userland, it appears the same either
385 * way, except the %fs segment selector might not be 0.
387 unsigned int seg = task->thread.fsindex;
388 if (task->thread.fs != 0)
389 return task->thread.fs;
391 asm("movl %%fs,%0" : "=r" (seg));
392 if (seg != FS_TLS_SEL)
394 return get_desc_base(&task->thread.tls_array[FS_TLS]);
396 case offsetof(struct user_regs_struct, gs_base): {
398 * Exactly the same here as the %fs handling above.
400 unsigned int seg = task->thread.gsindex;
401 if (task->thread.gs != 0)
402 return task->thread.gs;
404 asm("movl %%gs,%0" : "=r" (seg));
405 if (seg != GS_TLS_SEL)
407 return get_desc_base(&task->thread.tls_array[GS_TLS]);
412 return *pt_regs_access(task_pt_regs(task), offset);
415 static int genregs_get(struct task_struct *target,
416 const struct user_regset *regset,
417 unsigned int pos, unsigned int count,
418 void *kbuf, void __user *ubuf)
421 unsigned long *k = kbuf;
423 *k++ = getreg(target, pos);
428 unsigned long __user *u = ubuf;
430 if (__put_user(getreg(target, pos), u++))
440 static int genregs_set(struct task_struct *target,
441 const struct user_regset *regset,
442 unsigned int pos, unsigned int count,
443 const void *kbuf, const void __user *ubuf)
447 const unsigned long *k = kbuf;
448 while (count > 0 && !ret) {
449 ret = putreg(target, pos, *k++);
454 const unsigned long __user *u = ubuf;
455 while (count > 0 && !ret) {
457 ret = __get_user(word, u++);
460 ret = putreg(target, pos, word);
469 * This function is trivial and will be inlined by the compiler.
470 * Having it separates the implementation details of debug
471 * registers from the interface details of ptrace.
473 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
476 case 0: return child->thread.debugreg0;
477 case 1: return child->thread.debugreg1;
478 case 2: return child->thread.debugreg2;
479 case 3: return child->thread.debugreg3;
480 case 6: return child->thread.debugreg6;
481 case 7: return child->thread.debugreg7;
486 static int ptrace_set_debugreg(struct task_struct *child,
487 int n, unsigned long data)
491 if (unlikely(n == 4 || n == 5))
494 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
498 case 0: child->thread.debugreg0 = data; break;
499 case 1: child->thread.debugreg1 = data; break;
500 case 2: child->thread.debugreg2 = data; break;
501 case 3: child->thread.debugreg3 = data; break;
504 if ((data & ~0xffffffffUL) != 0)
506 child->thread.debugreg6 = data;
511 * Sanity-check data. Take one half-byte at once with
512 * check = (val >> (16 + 4*i)) & 0xf. It contains the
513 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
514 * 2 and 3 are LENi. Given a list of invalid values,
515 * we do mask |= 1 << invalid_value, so that
516 * (mask >> check) & 1 is a correct test for invalid
519 * R/Wi contains the type of the breakpoint /
520 * watchpoint, LENi contains the length of the watched
521 * data in the watchpoint case.
523 * The invalid values are:
524 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
525 * - R/Wi == 0x10 (break on I/O reads or writes), so
527 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
530 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
532 * See the Intel Manual "System Programming Guide",
535 * Note that LENi == 0x10 is defined on x86_64 in long
536 * mode (i.e. even for 32-bit userspace software, but
537 * 64-bit kernel), so the x86_64 mask value is 0x5454.
538 * See the AMD manual no. 24593 (AMD64 System Programming)
541 #define DR7_MASK 0x5f54
543 #define DR7_MASK 0x5554
545 data &= ~DR_CONTROL_RESERVED;
546 for (i = 0; i < 4; i++)
547 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
549 child->thread.debugreg7 = data;
551 set_tsk_thread_flag(child, TIF_DEBUG);
553 clear_tsk_thread_flag(child, TIF_DEBUG);
561 * These access the current or another (stopped) task's io permission
562 * bitmap for debugging or core dump.
564 static int ioperm_active(struct task_struct *target,
565 const struct user_regset *regset)
567 return target->thread.io_bitmap_max / regset->size;
570 static int ioperm_get(struct task_struct *target,
571 const struct user_regset *regset,
572 unsigned int pos, unsigned int count,
573 void *kbuf, void __user *ubuf)
575 if (!target->thread.io_bitmap_ptr)
578 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
579 target->thread.io_bitmap_ptr,
583 #ifdef CONFIG_X86_PTRACE_BTS
584 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
585 struct bts_struct __user *out)
587 const struct bts_trace *trace;
588 struct bts_struct bts;
589 const unsigned char *at;
592 trace = ds_read_bts(child->bts);
596 at = trace->ds.top - ((index + 1) * trace->ds.size);
597 if ((void *)at < trace->ds.begin)
598 at += (trace->ds.n * trace->ds.size);
603 error = trace->read(child->bts, at, &bts);
607 if (copy_to_user(out, &bts, sizeof(bts)))
613 static int ptrace_bts_drain(struct task_struct *child,
615 struct bts_struct __user *out)
617 const struct bts_trace *trace;
618 const unsigned char *at;
619 int error, drained = 0;
621 trace = ds_read_bts(child->bts);
628 if (size < (trace->ds.top - trace->ds.begin))
631 for (at = trace->ds.begin; (void *)at < trace->ds.top;
632 out++, drained++, at += trace->ds.size) {
633 struct bts_struct bts;
636 error = trace->read(child->bts, at, &bts);
640 if (copy_to_user(out, &bts, sizeof(bts)))
644 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
646 error = ds_reset_bts(child->bts);
653 static int ptrace_bts_allocate_buffer(struct task_struct *child, size_t size)
655 child->bts_buffer = alloc_locked_buffer(size);
656 if (!child->bts_buffer)
659 child->bts_size = size;
664 static void ptrace_bts_free_buffer(struct task_struct *child)
666 free_locked_buffer(child->bts_buffer, child->bts_size);
667 child->bts_buffer = NULL;
671 static int ptrace_bts_config(struct task_struct *child,
673 const struct ptrace_bts_config __user *ucfg)
675 struct ptrace_bts_config cfg;
676 unsigned int flags = 0;
678 if (cfg_size < sizeof(cfg))
681 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
685 ds_release_bts(child->bts);
689 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
695 child->thread.bts_ovfl_signal = cfg.signal;
698 if ((cfg.flags & PTRACE_BTS_O_ALLOC) &&
699 (cfg.size != child->bts_size)) {
702 ptrace_bts_free_buffer(child);
704 error = ptrace_bts_allocate_buffer(child, cfg.size);
709 if (cfg.flags & PTRACE_BTS_O_TRACE)
712 if (cfg.flags & PTRACE_BTS_O_SCHED)
713 flags |= BTS_TIMESTAMPS;
715 child->bts = ds_request_bts(child, child->bts_buffer, child->bts_size,
716 /* ovfl = */ NULL, /* th = */ (size_t)-1,
718 if (IS_ERR(child->bts)) {
719 int error = PTR_ERR(child->bts);
721 ptrace_bts_free_buffer(child);
730 static int ptrace_bts_status(struct task_struct *child,
732 struct ptrace_bts_config __user *ucfg)
734 const struct bts_trace *trace;
735 struct ptrace_bts_config cfg;
737 if (cfg_size < sizeof(cfg))
740 trace = ds_read_bts(child->bts);
744 memset(&cfg, 0, sizeof(cfg));
745 cfg.size = trace->ds.end - trace->ds.begin;
746 cfg.signal = child->thread.bts_ovfl_signal;
747 cfg.bts_size = sizeof(struct bts_struct);
750 cfg.flags |= PTRACE_BTS_O_SIGNAL;
752 if (trace->ds.flags & BTS_USER)
753 cfg.flags |= PTRACE_BTS_O_TRACE;
755 if (trace->ds.flags & BTS_TIMESTAMPS)
756 cfg.flags |= PTRACE_BTS_O_SCHED;
758 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
764 static int ptrace_bts_clear(struct task_struct *child)
766 const struct bts_trace *trace;
768 trace = ds_read_bts(child->bts);
772 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
774 return ds_reset_bts(child->bts);
777 static int ptrace_bts_size(struct task_struct *child)
779 const struct bts_trace *trace;
781 trace = ds_read_bts(child->bts);
785 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
788 static void ptrace_bts_fork(struct task_struct *tsk)
791 tsk->bts_buffer = NULL;
793 tsk->thread.bts_ovfl_signal = 0;
796 static void ptrace_bts_untrace(struct task_struct *child)
798 if (unlikely(child->bts)) {
799 ds_release_bts(child->bts);
802 /* We cannot update total_vm and locked_vm since
803 child's mm is already gone. But we can reclaim the
805 kfree(child->bts_buffer);
806 child->bts_buffer = NULL;
811 static void ptrace_bts_detach(struct task_struct *child)
813 if (unlikely(child->bts)) {
814 ds_release_bts(child->bts);
817 ptrace_bts_free_buffer(child);
821 static inline void ptrace_bts_fork(struct task_struct *tsk) {}
822 static inline void ptrace_bts_detach(struct task_struct *child) {}
823 static inline void ptrace_bts_untrace(struct task_struct *child) {}
824 #endif /* CONFIG_X86_PTRACE_BTS */
826 void x86_ptrace_fork(struct task_struct *child, unsigned long clone_flags)
828 ptrace_bts_fork(child);
831 void x86_ptrace_untrace(struct task_struct *child)
833 ptrace_bts_untrace(child);
837 * Called by kernel/ptrace.c when detaching..
839 * Make sure the single step bit is not set.
841 void ptrace_disable(struct task_struct *child)
843 user_disable_single_step(child);
844 #ifdef TIF_SYSCALL_EMU
845 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
847 ptrace_bts_detach(child);
850 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
851 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
854 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
857 unsigned long __user *datap = (unsigned long __user *)data;
860 /* read the word at location addr in the USER area. */
861 case PTRACE_PEEKUSR: {
865 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
866 addr >= sizeof(struct user))
869 tmp = 0; /* Default return condition */
870 if (addr < sizeof(struct user_regs_struct))
871 tmp = getreg(child, addr);
872 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
873 addr <= offsetof(struct user, u_debugreg[7])) {
874 addr -= offsetof(struct user, u_debugreg[0]);
875 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
877 ret = put_user(tmp, datap);
881 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
883 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
884 addr >= sizeof(struct user))
887 if (addr < sizeof(struct user_regs_struct))
888 ret = putreg(child, addr, data);
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 ret = ptrace_set_debugreg(child,
893 addr / sizeof(data), data);
897 case PTRACE_GETREGS: /* Get all gp regs from the child. */
898 return copy_regset_to_user(child,
899 task_user_regset_view(current),
901 0, sizeof(struct user_regs_struct),
904 case PTRACE_SETREGS: /* Set all gp regs in the child. */
905 return copy_regset_from_user(child,
906 task_user_regset_view(current),
908 0, sizeof(struct user_regs_struct),
911 case PTRACE_GETFPREGS: /* Get the child FPU state. */
912 return copy_regset_to_user(child,
913 task_user_regset_view(current),
915 0, sizeof(struct user_i387_struct),
918 case PTRACE_SETFPREGS: /* Set the child FPU state. */
919 return copy_regset_from_user(child,
920 task_user_regset_view(current),
922 0, sizeof(struct user_i387_struct),
926 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
927 return copy_regset_to_user(child, &user_x86_32_view,
929 0, sizeof(struct user_fxsr_struct),
932 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
933 return copy_regset_from_user(child, &user_x86_32_view,
935 0, sizeof(struct user_fxsr_struct),
939 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
940 case PTRACE_GET_THREAD_AREA:
943 ret = do_get_thread_area(child, addr,
944 (struct user_desc __user *) data);
947 case PTRACE_SET_THREAD_AREA:
950 ret = do_set_thread_area(child, addr,
951 (struct user_desc __user *) data, 0);
956 /* normal 64bit interface to access TLS data.
957 Works just like arch_prctl, except that the arguments
959 case PTRACE_ARCH_PRCTL:
960 ret = do_arch_prctl(child, data, addr);
965 * These bits need more cooking - not enabled yet:
967 #ifdef CONFIG_X86_PTRACE_BTS
968 case PTRACE_BTS_CONFIG:
969 ret = ptrace_bts_config
970 (child, data, (struct ptrace_bts_config __user *)addr);
973 case PTRACE_BTS_STATUS:
974 ret = ptrace_bts_status
975 (child, data, (struct ptrace_bts_config __user *)addr);
978 case PTRACE_BTS_SIZE:
979 ret = ptrace_bts_size(child);
983 ret = ptrace_bts_read_record
984 (child, data, (struct bts_struct __user *) addr);
987 case PTRACE_BTS_CLEAR:
988 ret = ptrace_bts_clear(child);
991 case PTRACE_BTS_DRAIN:
992 ret = ptrace_bts_drain
993 (child, data, (struct bts_struct __user *) addr);
995 #endif /* CONFIG_X86_PTRACE_BTS */
998 ret = ptrace_request(child, request, addr, data);
1005 #ifdef CONFIG_IA32_EMULATION
1007 #include <linux/compat.h>
1008 #include <linux/syscalls.h>
1009 #include <asm/ia32.h>
1010 #include <asm/user32.h>
1013 case offsetof(struct user32, regs.l): \
1014 regs->q = value; break
1017 case offsetof(struct user32, regs.rs): \
1018 return set_segment_reg(child, \
1019 offsetof(struct user_regs_struct, rs), \
1023 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1025 struct pt_regs *regs = task_pt_regs(child);
1046 case offsetof(struct user32, regs.orig_eax):
1048 * Sign-extend the value so that orig_eax = -1
1049 * causes (long)orig_ax < 0 tests to fire correctly.
1051 regs->orig_ax = (long) (s32) value;
1054 case offsetof(struct user32, regs.eflags):
1055 return set_flags(child, value);
1057 case offsetof(struct user32, u_debugreg[0]) ...
1058 offsetof(struct user32, u_debugreg[7]):
1059 regno -= offsetof(struct user32, u_debugreg[0]);
1060 return ptrace_set_debugreg(child, regno / 4, value);
1063 if (regno > sizeof(struct user32) || (regno & 3))
1067 * Other dummy fields in the virtual user structure
1079 case offsetof(struct user32, regs.l): \
1080 *val = regs->q; break
1083 case offsetof(struct user32, regs.rs): \
1084 *val = get_segment_reg(child, \
1085 offsetof(struct user_regs_struct, rs)); \
1088 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1090 struct pt_regs *regs = task_pt_regs(child);
1108 R32(orig_eax, orig_ax);
1112 case offsetof(struct user32, regs.eflags):
1113 *val = get_flags(child);
1116 case offsetof(struct user32, u_debugreg[0]) ...
1117 offsetof(struct user32, u_debugreg[7]):
1118 regno -= offsetof(struct user32, u_debugreg[0]);
1119 *val = ptrace_get_debugreg(child, regno / 4);
1123 if (regno > sizeof(struct user32) || (regno & 3))
1127 * Other dummy fields in the virtual user structure
1139 static int genregs32_get(struct task_struct *target,
1140 const struct user_regset *regset,
1141 unsigned int pos, unsigned int count,
1142 void *kbuf, void __user *ubuf)
1145 compat_ulong_t *k = kbuf;
1147 getreg32(target, pos, k++);
1148 count -= sizeof(*k);
1152 compat_ulong_t __user *u = ubuf;
1154 compat_ulong_t word;
1155 getreg32(target, pos, &word);
1156 if (__put_user(word, u++))
1158 count -= sizeof(*u);
1166 static int genregs32_set(struct task_struct *target,
1167 const struct user_regset *regset,
1168 unsigned int pos, unsigned int count,
1169 const void *kbuf, const void __user *ubuf)
1173 const compat_ulong_t *k = kbuf;
1174 while (count > 0 && !ret) {
1175 ret = putreg32(target, pos, *k++);
1176 count -= sizeof(*k);
1180 const compat_ulong_t __user *u = ubuf;
1181 while (count > 0 && !ret) {
1182 compat_ulong_t word;
1183 ret = __get_user(word, u++);
1186 ret = putreg32(target, pos, word);
1187 count -= sizeof(*u);
1194 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1195 compat_ulong_t caddr, compat_ulong_t cdata)
1197 unsigned long addr = caddr;
1198 unsigned long data = cdata;
1199 void __user *datap = compat_ptr(data);
1204 case PTRACE_PEEKUSR:
1205 ret = getreg32(child, addr, &val);
1207 ret = put_user(val, (__u32 __user *)datap);
1210 case PTRACE_POKEUSR:
1211 ret = putreg32(child, addr, data);
1214 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1215 return copy_regset_to_user(child, &user_x86_32_view,
1217 0, sizeof(struct user_regs_struct32),
1220 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1221 return copy_regset_from_user(child, &user_x86_32_view,
1223 sizeof(struct user_regs_struct32),
1226 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1227 return copy_regset_to_user(child, &user_x86_32_view,
1229 sizeof(struct user_i387_ia32_struct),
1232 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1233 return copy_regset_from_user(
1234 child, &user_x86_32_view, REGSET_FP,
1235 0, sizeof(struct user_i387_ia32_struct), datap);
1237 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1238 return copy_regset_to_user(child, &user_x86_32_view,
1240 sizeof(struct user32_fxsr_struct),
1243 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1244 return copy_regset_from_user(child, &user_x86_32_view,
1246 sizeof(struct user32_fxsr_struct),
1249 case PTRACE_GET_THREAD_AREA:
1250 case PTRACE_SET_THREAD_AREA:
1251 #ifdef CONFIG_X86_PTRACE_BTS
1252 case PTRACE_BTS_CONFIG:
1253 case PTRACE_BTS_STATUS:
1254 case PTRACE_BTS_SIZE:
1255 case PTRACE_BTS_GET:
1256 case PTRACE_BTS_CLEAR:
1257 case PTRACE_BTS_DRAIN:
1258 #endif /* CONFIG_X86_PTRACE_BTS */
1259 return arch_ptrace(child, request, addr, data);
1262 return compat_ptrace_request(child, request, addr, data);
1268 #endif /* CONFIG_IA32_EMULATION */
1270 #ifdef CONFIG_X86_64
1272 static const struct user_regset x86_64_regsets[] = {
1273 [REGSET_GENERAL] = {
1274 .core_note_type = NT_PRSTATUS,
1275 .n = sizeof(struct user_regs_struct) / sizeof(long),
1276 .size = sizeof(long), .align = sizeof(long),
1277 .get = genregs_get, .set = genregs_set
1280 .core_note_type = NT_PRFPREG,
1281 .n = sizeof(struct user_i387_struct) / sizeof(long),
1282 .size = sizeof(long), .align = sizeof(long),
1283 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1285 [REGSET_IOPERM64] = {
1286 .core_note_type = NT_386_IOPERM,
1287 .n = IO_BITMAP_LONGS,
1288 .size = sizeof(long), .align = sizeof(long),
1289 .active = ioperm_active, .get = ioperm_get
1293 static const struct user_regset_view user_x86_64_view = {
1294 .name = "x86_64", .e_machine = EM_X86_64,
1295 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1298 #else /* CONFIG_X86_32 */
1300 #define user_regs_struct32 user_regs_struct
1301 #define genregs32_get genregs_get
1302 #define genregs32_set genregs_set
1304 #define user_i387_ia32_struct user_i387_struct
1305 #define user32_fxsr_struct user_fxsr_struct
1307 #endif /* CONFIG_X86_64 */
1309 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1310 static const struct user_regset x86_32_regsets[] = {
1311 [REGSET_GENERAL] = {
1312 .core_note_type = NT_PRSTATUS,
1313 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1314 .size = sizeof(u32), .align = sizeof(u32),
1315 .get = genregs32_get, .set = genregs32_set
1318 .core_note_type = NT_PRFPREG,
1319 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1320 .size = sizeof(u32), .align = sizeof(u32),
1321 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1324 .core_note_type = NT_PRXFPREG,
1325 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1326 .size = sizeof(u32), .align = sizeof(u32),
1327 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1330 .core_note_type = NT_386_TLS,
1331 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1332 .size = sizeof(struct user_desc),
1333 .align = sizeof(struct user_desc),
1334 .active = regset_tls_active,
1335 .get = regset_tls_get, .set = regset_tls_set
1337 [REGSET_IOPERM32] = {
1338 .core_note_type = NT_386_IOPERM,
1339 .n = IO_BITMAP_BYTES / sizeof(u32),
1340 .size = sizeof(u32), .align = sizeof(u32),
1341 .active = ioperm_active, .get = ioperm_get
1345 static const struct user_regset_view user_x86_32_view = {
1346 .name = "i386", .e_machine = EM_386,
1347 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1351 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1353 #ifdef CONFIG_IA32_EMULATION
1354 if (test_tsk_thread_flag(task, TIF_IA32))
1356 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1357 return &user_x86_32_view;
1359 #ifdef CONFIG_X86_64
1360 return &user_x86_64_view;
1364 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1365 int error_code, int si_code)
1367 struct siginfo info;
1369 tsk->thread.trap_no = 1;
1370 tsk->thread.error_code = error_code;
1372 memset(&info, 0, sizeof(info));
1373 info.si_signo = SIGTRAP;
1374 info.si_code = si_code;
1377 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1379 /* Send us the fake SIGTRAP */
1380 force_sig_info(SIGTRAP, &info, tsk);
1384 #ifdef CONFIG_X86_32
1386 #elif defined CONFIG_IA32_EMULATION
1387 # define IS_IA32 test_thread_flag(TIF_IA32)
1393 * We must return the syscall number to actually look up in the table.
1394 * This can be -1L to skip running any syscall at all.
1396 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1401 * If we stepped into a sysenter/syscall insn, it trapped in
1402 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1403 * If user-mode had set TF itself, then it's still clear from
1404 * do_debug() and we need to set it again to restore the user
1405 * state. If we entered on the slow path, TF was already set.
1407 if (test_thread_flag(TIF_SINGLESTEP))
1408 regs->flags |= X86_EFLAGS_TF;
1410 /* do the secure computing check first */
1411 secure_computing(regs->orig_ax);
1413 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1416 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1417 tracehook_report_syscall_entry(regs))
1420 if (unlikely(current->audit_context)) {
1422 audit_syscall_entry(AUDIT_ARCH_I386,
1425 regs->dx, regs->si);
1426 #ifdef CONFIG_X86_64
1428 audit_syscall_entry(AUDIT_ARCH_X86_64,
1431 regs->dx, regs->r10);
1435 return ret ?: regs->orig_ax;
1438 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1440 if (unlikely(current->audit_context))
1441 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1443 if (test_thread_flag(TIF_SYSCALL_TRACE))
1444 tracehook_report_syscall_exit(regs, 0);
1447 * If TIF_SYSCALL_EMU is set, we only get here because of
1448 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1449 * We already reported this syscall instruction in
1450 * syscall_trace_enter(), so don't do any more now.
1452 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1456 * If we are single-stepping, synthesize a trap to follow the
1457 * system call instruction.
1459 if (test_thread_flag(TIF_SINGLESTEP) &&
1460 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1461 send_sigtrap(current, regs, 0, TRAP_BRKPT);