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/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
29 #include <asm/debugreg.h>
32 #include <asm/prctl.h>
33 #include <asm/proto.h>
46 * does not yet catch signals sent when the child dies.
47 * in exit.c or in signal.c.
51 * Determines which flags the user has access to [1 = access, 0 = no access].
53 #define FLAG_MASK_32 ((unsigned long) \
54 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
55 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
56 X86_EFLAGS_SF | X86_EFLAGS_TF | \
57 X86_EFLAGS_DF | X86_EFLAGS_OF | \
58 X86_EFLAGS_RF | X86_EFLAGS_AC))
61 * Determines whether a value may be installed in a segment register.
63 static inline bool invalid_selector(u16 value)
65 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
70 #define FLAG_MASK FLAG_MASK_32
72 static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
74 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
78 return ®s->bx + regno;
81 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
84 * Returning the value truncates it to 16 bits.
87 if (offset != offsetof(struct user_regs_struct, gs))
88 retval = *pt_regs_access(task_pt_regs(task), offset);
90 retval = task->thread.gs;
92 savesegment(gs, retval);
97 static int set_segment_reg(struct task_struct *task,
98 unsigned long offset, u16 value)
101 * The value argument was already truncated to 16 bits.
103 if (invalid_selector(value))
106 if (offset != offsetof(struct user_regs_struct, gs))
107 *pt_regs_access(task_pt_regs(task), offset) = value;
109 task->thread.gs = value;
112 * The user-mode %gs is not affected by
113 * kernel entry, so we must update the CPU.
115 loadsegment(gs, value);
121 static unsigned long debugreg_addr_limit(struct task_struct *task)
123 return TASK_SIZE - 3;
126 #else /* CONFIG_X86_64 */
128 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
130 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
132 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
133 return ®s->r15 + (offset / sizeof(regs->r15));
136 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
139 * Returning the value truncates it to 16 bits.
144 case offsetof(struct user_regs_struct, fs):
145 if (task == current) {
146 /* Older gas can't assemble movq %?s,%r?? */
147 asm("movl %%fs,%0" : "=r" (seg));
150 return task->thread.fsindex;
151 case offsetof(struct user_regs_struct, gs):
152 if (task == current) {
153 asm("movl %%gs,%0" : "=r" (seg));
156 return task->thread.gsindex;
157 case offsetof(struct user_regs_struct, ds):
158 if (task == current) {
159 asm("movl %%ds,%0" : "=r" (seg));
162 return task->thread.ds;
163 case offsetof(struct user_regs_struct, es):
164 if (task == current) {
165 asm("movl %%es,%0" : "=r" (seg));
168 return task->thread.es;
170 case offsetof(struct user_regs_struct, cs):
171 case offsetof(struct user_regs_struct, ss):
174 return *pt_regs_access(task_pt_regs(task), offset);
177 static int set_segment_reg(struct task_struct *task,
178 unsigned long offset, u16 value)
181 * The value argument was already truncated to 16 bits.
183 if (invalid_selector(value))
187 case offsetof(struct user_regs_struct,fs):
189 * If this is setting fs as for normal 64-bit use but
190 * setting fs_base has implicitly changed it, leave it.
192 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
193 task->thread.fs != 0) ||
194 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
195 task->thread.fs == 0))
197 task->thread.fsindex = value;
199 loadsegment(fs, task->thread.fsindex);
201 case offsetof(struct user_regs_struct,gs):
203 * If this is setting gs as for normal 64-bit use but
204 * setting gs_base has implicitly changed it, leave it.
206 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
207 task->thread.gs != 0) ||
208 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
209 task->thread.gs == 0))
211 task->thread.gsindex = value;
213 load_gs_index(task->thread.gsindex);
215 case offsetof(struct user_regs_struct,ds):
216 task->thread.ds = value;
218 loadsegment(ds, task->thread.ds);
220 case offsetof(struct user_regs_struct,es):
221 task->thread.es = value;
223 loadsegment(es, task->thread.es);
227 * Can't actually change these in 64-bit mode.
229 case offsetof(struct user_regs_struct,cs):
230 #ifdef CONFIG_IA32_EMULATION
231 if (test_tsk_thread_flag(task, TIF_IA32))
232 task_pt_regs(task)->cs = value;
235 case offsetof(struct user_regs_struct,ss):
236 #ifdef CONFIG_IA32_EMULATION
237 if (test_tsk_thread_flag(task, TIF_IA32))
238 task_pt_regs(task)->ss = value;
246 static unsigned long debugreg_addr_limit(struct task_struct *task)
248 #ifdef CONFIG_IA32_EMULATION
249 if (test_tsk_thread_flag(task, TIF_IA32))
250 return IA32_PAGE_OFFSET - 3;
252 return TASK_SIZE64 - 7;
255 #endif /* CONFIG_X86_32 */
257 static unsigned long get_flags(struct task_struct *task)
259 unsigned long retval = task_pt_regs(task)->flags;
262 * If the debugger set TF, hide it from the readout.
264 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
265 retval &= ~X86_EFLAGS_TF;
270 static int set_flags(struct task_struct *task, unsigned long value)
272 struct pt_regs *regs = task_pt_regs(task);
275 * If the user value contains TF, mark that
276 * it was not "us" (the debugger) that set it.
277 * If not, make sure it stays set if we had.
279 if (value & X86_EFLAGS_TF)
280 clear_tsk_thread_flag(task, TIF_FORCED_TF);
281 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
282 value |= X86_EFLAGS_TF;
284 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
289 static int putreg(struct task_struct *child,
290 unsigned long offset, unsigned long value)
293 case offsetof(struct user_regs_struct, cs):
294 case offsetof(struct user_regs_struct, ds):
295 case offsetof(struct user_regs_struct, es):
296 case offsetof(struct user_regs_struct, fs):
297 case offsetof(struct user_regs_struct, gs):
298 case offsetof(struct user_regs_struct, ss):
299 return set_segment_reg(child, offset, value);
301 case offsetof(struct user_regs_struct, flags):
302 return set_flags(child, value);
305 case offsetof(struct user_regs_struct,fs_base):
306 if (value >= TASK_SIZE_OF(child))
309 * When changing the segment base, use do_arch_prctl
310 * to set either thread.fs or thread.fsindex and the
311 * corresponding GDT slot.
313 if (child->thread.fs != value)
314 return do_arch_prctl(child, ARCH_SET_FS, value);
316 case offsetof(struct user_regs_struct,gs_base):
318 * Exactly the same here as the %fs handling above.
320 if (value >= TASK_SIZE_OF(child))
322 if (child->thread.gs != value)
323 return do_arch_prctl(child, ARCH_SET_GS, value);
328 *pt_regs_access(task_pt_regs(child), offset) = value;
332 static unsigned long getreg(struct task_struct *task, unsigned long offset)
335 case offsetof(struct user_regs_struct, cs):
336 case offsetof(struct user_regs_struct, ds):
337 case offsetof(struct user_regs_struct, es):
338 case offsetof(struct user_regs_struct, fs):
339 case offsetof(struct user_regs_struct, gs):
340 case offsetof(struct user_regs_struct, ss):
341 return get_segment_reg(task, offset);
343 case offsetof(struct user_regs_struct, flags):
344 return get_flags(task);
347 case offsetof(struct user_regs_struct, fs_base): {
349 * do_arch_prctl may have used a GDT slot instead of
350 * the MSR. To userland, it appears the same either
351 * way, except the %fs segment selector might not be 0.
353 unsigned int seg = task->thread.fsindex;
354 if (task->thread.fs != 0)
355 return task->thread.fs;
357 asm("movl %%fs,%0" : "=r" (seg));
358 if (seg != FS_TLS_SEL)
360 return get_desc_base(&task->thread.tls_array[FS_TLS]);
362 case offsetof(struct user_regs_struct, gs_base): {
364 * Exactly the same here as the %fs handling above.
366 unsigned int seg = task->thread.gsindex;
367 if (task->thread.gs != 0)
368 return task->thread.gs;
370 asm("movl %%gs,%0" : "=r" (seg));
371 if (seg != GS_TLS_SEL)
373 return get_desc_base(&task->thread.tls_array[GS_TLS]);
378 return *pt_regs_access(task_pt_regs(task), offset);
381 static int genregs_get(struct task_struct *target,
382 const struct user_regset *regset,
383 unsigned int pos, unsigned int count,
384 void *kbuf, void __user *ubuf)
387 unsigned long *k = kbuf;
389 *k++ = getreg(target, pos);
394 unsigned long __user *u = ubuf;
396 if (__put_user(getreg(target, pos), u++))
406 static int genregs_set(struct task_struct *target,
407 const struct user_regset *regset,
408 unsigned int pos, unsigned int count,
409 const void *kbuf, const void __user *ubuf)
413 const unsigned long *k = kbuf;
414 while (count > 0 && !ret) {
415 ret = putreg(target, pos, *k++);
420 const unsigned long __user *u = ubuf;
421 while (count > 0 && !ret) {
423 ret = __get_user(word, u++);
426 ret = putreg(target, pos, word);
435 * This function is trivial and will be inlined by the compiler.
436 * Having it separates the implementation details of debug
437 * registers from the interface details of ptrace.
439 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
442 case 0: return child->thread.debugreg0;
443 case 1: return child->thread.debugreg1;
444 case 2: return child->thread.debugreg2;
445 case 3: return child->thread.debugreg3;
446 case 6: return child->thread.debugreg6;
447 case 7: return child->thread.debugreg7;
452 static int ptrace_set_debugreg(struct task_struct *child,
453 int n, unsigned long data)
457 if (unlikely(n == 4 || n == 5))
460 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
464 case 0: child->thread.debugreg0 = data; break;
465 case 1: child->thread.debugreg1 = data; break;
466 case 2: child->thread.debugreg2 = data; break;
467 case 3: child->thread.debugreg3 = data; break;
470 if ((data & ~0xffffffffUL) != 0)
472 child->thread.debugreg6 = data;
477 * Sanity-check data. Take one half-byte at once with
478 * check = (val >> (16 + 4*i)) & 0xf. It contains the
479 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
480 * 2 and 3 are LENi. Given a list of invalid values,
481 * we do mask |= 1 << invalid_value, so that
482 * (mask >> check) & 1 is a correct test for invalid
485 * R/Wi contains the type of the breakpoint /
486 * watchpoint, LENi contains the length of the watched
487 * data in the watchpoint case.
489 * The invalid values are:
490 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
491 * - R/Wi == 0x10 (break on I/O reads or writes), so
493 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
496 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
498 * See the Intel Manual "System Programming Guide",
501 * Note that LENi == 0x10 is defined on x86_64 in long
502 * mode (i.e. even for 32-bit userspace software, but
503 * 64-bit kernel), so the x86_64 mask value is 0x5454.
504 * See the AMD manual no. 24593 (AMD64 System Programming)
507 #define DR7_MASK 0x5f54
509 #define DR7_MASK 0x5554
511 data &= ~DR_CONTROL_RESERVED;
512 for (i = 0; i < 4; i++)
513 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
515 child->thread.debugreg7 = data;
517 set_tsk_thread_flag(child, TIF_DEBUG);
519 clear_tsk_thread_flag(child, TIF_DEBUG);
526 static int ptrace_bts_get_size(struct task_struct *child)
528 if (!child->thread.ds_area_msr)
531 return ds_get_bts_index((void *)child->thread.ds_area_msr);
534 static int ptrace_bts_read_record(struct task_struct *child,
536 struct bts_struct __user *out)
538 struct bts_struct ret;
543 if (!child->thread.ds_area_msr)
549 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
550 if (bts_end <= index)
553 /* translate the ptrace bts index into the ds bts index */
554 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
555 bts_index -= (index + 1);
557 bts_index += bts_end;
559 retval = ds_read_bts((void *)child->thread.ds_area_msr,
564 if (copy_to_user(out, &ret, sizeof(ret)))
570 static int ptrace_bts_write_record(struct task_struct *child,
571 const struct bts_struct *in)
575 if (!child->thread.ds_area_msr)
578 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
585 static int ptrace_bts_clear(struct task_struct *child)
587 if (!child->thread.ds_area_msr)
590 return ds_clear((void *)child->thread.ds_area_msr);
593 static int ptrace_bts_drain(struct task_struct *child,
594 struct bts_struct __user *out)
597 void *ds = (void *)child->thread.ds_area_msr;
602 end = ds_get_bts_index(ds);
606 for (i = 0; i < end; i++, out++) {
607 struct bts_struct ret;
610 retval = ds_read_bts(ds, i, &ret);
614 if (copy_to_user(out, &ret, sizeof(ret)))
623 static int ptrace_bts_realloc(struct task_struct *child,
624 int size, int reduce_size)
626 unsigned long rlim, vm;
632 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
636 ret = ds_free((void **)&child->thread.ds_area_msr);
641 old_size >>= PAGE_SHIFT;
643 current->mm->total_vm -= old_size;
644 current->mm->locked_vm -= old_size;
649 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
650 vm = current->mm->total_vm + size;
657 size = rlim - current->mm->total_vm;
662 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
663 vm = current->mm->locked_vm + size;
670 size = rlim - current->mm->locked_vm;
675 ret = ds_allocate((void **)&child->thread.ds_area_msr,
680 current->mm->total_vm += size;
681 current->mm->locked_vm += size;
684 if (child->thread.ds_area_msr)
685 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
687 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
692 static int ptrace_bts_config(struct task_struct *child,
693 const struct ptrace_bts_config __user *ucfg)
695 struct ptrace_bts_config cfg;
696 int bts_size, ret = 0;
699 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
703 ds = (void *)child->thread.ds_area_msr;
705 bts_size = ds_get_bts_size(ds);
709 cfg.size = PAGE_ALIGN(cfg.size);
711 if (bts_size != cfg.size) {
712 ret = ptrace_bts_realloc(child, cfg.size,
713 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
717 ds = (void *)child->thread.ds_area_msr;
720 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
721 ret = ds_set_overflow(ds, DS_O_SIGNAL);
723 ret = ds_set_overflow(ds, DS_O_WRAP);
727 if (cfg.flags & PTRACE_BTS_O_TRACE)
728 child->thread.debugctlmsr |= ds_debugctl_mask();
730 child->thread.debugctlmsr &= ~ds_debugctl_mask();
732 if (cfg.flags & PTRACE_BTS_O_SCHED)
733 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
735 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
738 if (child->thread.debugctlmsr)
739 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
741 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
746 child->thread.debugctlmsr &= ~ds_debugctl_mask();
747 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
751 static int ptrace_bts_status(struct task_struct *child,
752 struct ptrace_bts_config __user *ucfg)
754 void *ds = (void *)child->thread.ds_area_msr;
755 struct ptrace_bts_config cfg;
757 memset(&cfg, 0, sizeof(cfg));
760 cfg.size = ds_get_bts_size(ds);
762 if (ds_get_overflow(ds) == DS_O_SIGNAL)
763 cfg.flags |= PTRACE_BTS_O_SIGNAL;
765 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
766 child->thread.debugctlmsr & ds_debugctl_mask())
767 cfg.flags |= PTRACE_BTS_O_TRACE;
769 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
770 cfg.flags |= PTRACE_BTS_O_SCHED;
773 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
779 void ptrace_bts_take_timestamp(struct task_struct *tsk,
780 enum bts_qualifier qualifier)
782 struct bts_struct rec = {
783 .qualifier = qualifier,
784 .variant.jiffies = jiffies_64
787 ptrace_bts_write_record(tsk, &rec);
791 * Called by kernel/ptrace.c when detaching..
793 * Make sure the single step bit is not set.
795 void ptrace_disable(struct task_struct *child)
797 user_disable_single_step(child);
798 #ifdef TIF_SYSCALL_EMU
799 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
801 if (child->thread.ds_area_msr) {
802 ptrace_bts_realloc(child, 0, 0);
803 child->thread.debugctlmsr &= ~ds_debugctl_mask();
804 if (!child->thread.debugctlmsr)
805 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
806 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
810 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
811 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
814 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
817 unsigned long __user *datap = (unsigned long __user *)data;
820 /* read the word at location addr in the USER area. */
821 case PTRACE_PEEKUSR: {
825 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
826 addr >= sizeof(struct user))
829 tmp = 0; /* Default return condition */
830 if (addr < sizeof(struct user_regs_struct))
831 tmp = getreg(child, addr);
832 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
833 addr <= offsetof(struct user, u_debugreg[7])) {
834 addr -= offsetof(struct user, u_debugreg[0]);
835 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
837 ret = put_user(tmp, datap);
841 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
843 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
844 addr >= sizeof(struct user))
847 if (addr < sizeof(struct user_regs_struct))
848 ret = putreg(child, addr, data);
849 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
850 addr <= offsetof(struct user, u_debugreg[7])) {
851 addr -= offsetof(struct user, u_debugreg[0]);
852 ret = ptrace_set_debugreg(child,
853 addr / sizeof(data), data);
857 case PTRACE_GETREGS: /* Get all gp regs from the child. */
858 return copy_regset_to_user(child,
859 task_user_regset_view(current),
861 0, sizeof(struct user_regs_struct),
864 case PTRACE_SETREGS: /* Set all gp regs in the child. */
865 return copy_regset_from_user(child,
866 task_user_regset_view(current),
868 0, sizeof(struct user_regs_struct),
871 case PTRACE_GETFPREGS: /* Get the child FPU state. */
872 return copy_regset_to_user(child,
873 task_user_regset_view(current),
875 0, sizeof(struct user_i387_struct),
878 case PTRACE_SETFPREGS: /* Set the child FPU state. */
879 return copy_regset_from_user(child,
880 task_user_regset_view(current),
882 0, sizeof(struct user_i387_struct),
886 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
887 return copy_regset_to_user(child, &user_x86_32_view,
889 0, sizeof(struct user_fxsr_struct),
892 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
893 return copy_regset_from_user(child, &user_x86_32_view,
895 0, sizeof(struct user_fxsr_struct),
899 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
900 case PTRACE_GET_THREAD_AREA:
903 ret = do_get_thread_area(child, addr,
904 (struct user_desc __user *) data);
907 case PTRACE_SET_THREAD_AREA:
910 ret = do_set_thread_area(child, addr,
911 (struct user_desc __user *) data, 0);
916 /* normal 64bit interface to access TLS data.
917 Works just like arch_prctl, except that the arguments
919 case PTRACE_ARCH_PRCTL:
920 ret = do_arch_prctl(child, data, addr);
924 case PTRACE_BTS_CONFIG:
925 ret = ptrace_bts_config
926 (child, (struct ptrace_bts_config __user *)addr);
929 case PTRACE_BTS_STATUS:
930 ret = ptrace_bts_status
931 (child, (struct ptrace_bts_config __user *)addr);
934 case PTRACE_BTS_SIZE:
935 ret = ptrace_bts_get_size(child);
939 ret = ptrace_bts_read_record
940 (child, data, (struct bts_struct __user *) addr);
943 case PTRACE_BTS_CLEAR:
944 ret = ptrace_bts_clear(child);
947 case PTRACE_BTS_DRAIN:
948 ret = ptrace_bts_drain
949 (child, (struct bts_struct __user *) addr);
953 ret = ptrace_request(child, request, addr, data);
960 #ifdef CONFIG_IA32_EMULATION
962 #include <linux/compat.h>
963 #include <linux/syscalls.h>
964 #include <asm/ia32.h>
965 #include <asm/user32.h>
968 case offsetof(struct user32, regs.l): \
969 regs->q = value; break
972 case offsetof(struct user32, regs.rs): \
973 return set_segment_reg(child, \
974 offsetof(struct user_regs_struct, rs), \
978 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
980 struct pt_regs *regs = task_pt_regs(child);
998 R32(orig_eax, orig_ax);
1002 case offsetof(struct user32, regs.eflags):
1003 return set_flags(child, value);
1005 case offsetof(struct user32, u_debugreg[0]) ...
1006 offsetof(struct user32, u_debugreg[7]):
1007 regno -= offsetof(struct user32, u_debugreg[0]);
1008 return ptrace_set_debugreg(child, regno / 4, value);
1011 if (regno > sizeof(struct user32) || (regno & 3))
1015 * Other dummy fields in the virtual user structure
1027 case offsetof(struct user32, regs.l): \
1028 *val = regs->q; break
1031 case offsetof(struct user32, regs.rs): \
1032 *val = get_segment_reg(child, \
1033 offsetof(struct user_regs_struct, rs)); \
1036 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1038 struct pt_regs *regs = task_pt_regs(child);
1056 R32(orig_eax, orig_ax);
1060 case offsetof(struct user32, regs.eflags):
1061 *val = get_flags(child);
1064 case offsetof(struct user32, u_debugreg[0]) ...
1065 offsetof(struct user32, u_debugreg[7]):
1066 regno -= offsetof(struct user32, u_debugreg[0]);
1067 *val = ptrace_get_debugreg(child, regno / 4);
1071 if (regno > sizeof(struct user32) || (regno & 3))
1075 * Other dummy fields in the virtual user structure
1087 static int genregs32_get(struct task_struct *target,
1088 const struct user_regset *regset,
1089 unsigned int pos, unsigned int count,
1090 void *kbuf, void __user *ubuf)
1093 compat_ulong_t *k = kbuf;
1095 getreg32(target, pos, k++);
1096 count -= sizeof(*k);
1100 compat_ulong_t __user *u = ubuf;
1102 compat_ulong_t word;
1103 getreg32(target, pos, &word);
1104 if (__put_user(word, u++))
1106 count -= sizeof(*u);
1114 static int genregs32_set(struct task_struct *target,
1115 const struct user_regset *regset,
1116 unsigned int pos, unsigned int count,
1117 const void *kbuf, const void __user *ubuf)
1121 const compat_ulong_t *k = kbuf;
1122 while (count > 0 && !ret) {
1123 ret = putreg(target, pos, *k++);
1124 count -= sizeof(*k);
1128 const compat_ulong_t __user *u = ubuf;
1129 while (count > 0 && !ret) {
1130 compat_ulong_t word;
1131 ret = __get_user(word, u++);
1134 ret = putreg(target, pos, word);
1135 count -= sizeof(*u);
1142 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
1144 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
1145 compat_siginfo_t __user *si32 = compat_ptr(data);
1149 if (request == PTRACE_SETSIGINFO) {
1150 memset(&ssi, 0, sizeof(siginfo_t));
1151 ret = copy_siginfo_from_user32(&ssi, si32);
1154 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
1157 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
1160 if (request == PTRACE_GETSIGINFO) {
1161 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
1163 ret = copy_siginfo_to_user32(si32, &ssi);
1168 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1170 struct task_struct *child;
1171 struct pt_regs *childregs;
1172 void __user *datap = compat_ptr(data);
1177 case PTRACE_TRACEME:
1181 case PTRACE_SINGLESTEP:
1182 case PTRACE_SINGLEBLOCK:
1184 case PTRACE_SYSCALL:
1185 case PTRACE_OLDSETOPTIONS:
1186 case PTRACE_SETOPTIONS:
1187 case PTRACE_SET_THREAD_AREA:
1188 case PTRACE_GET_THREAD_AREA:
1189 case PTRACE_BTS_CONFIG:
1190 case PTRACE_BTS_STATUS:
1191 case PTRACE_BTS_SIZE:
1192 case PTRACE_BTS_GET:
1193 case PTRACE_BTS_CLEAR:
1194 case PTRACE_BTS_DRAIN:
1195 return sys_ptrace(request, pid, addr, data);
1200 case PTRACE_PEEKTEXT:
1201 case PTRACE_PEEKDATA:
1202 case PTRACE_POKEDATA:
1203 case PTRACE_POKETEXT:
1204 case PTRACE_POKEUSR:
1205 case PTRACE_PEEKUSR:
1206 case PTRACE_GETREGS:
1207 case PTRACE_SETREGS:
1208 case PTRACE_SETFPREGS:
1209 case PTRACE_GETFPREGS:
1210 case PTRACE_SETFPXREGS:
1211 case PTRACE_GETFPXREGS:
1212 case PTRACE_GETEVENTMSG:
1215 case PTRACE_SETSIGINFO:
1216 case PTRACE_GETSIGINFO:
1217 return ptrace32_siginfo(request, pid, addr, data);
1220 child = ptrace_get_task_struct(pid);
1222 return PTR_ERR(child);
1224 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1228 childregs = task_pt_regs(child);
1231 case PTRACE_PEEKUSR:
1232 ret = getreg32(child, addr, &val);
1234 ret = put_user(val, (__u32 __user *)datap);
1237 case PTRACE_POKEUSR:
1238 ret = putreg32(child, addr, data);
1241 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1242 return copy_regset_to_user(child, &user_x86_32_view,
1244 0, sizeof(struct user_regs_struct32),
1247 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1248 return copy_regset_from_user(child, &user_x86_32_view,
1250 sizeof(struct user_regs_struct32),
1253 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1254 return copy_regset_to_user(child, &user_x86_32_view,
1256 sizeof(struct user_i387_ia32_struct),
1259 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1260 return copy_regset_from_user(
1261 child, &user_x86_32_view, REGSET_FP,
1262 0, sizeof(struct user_i387_ia32_struct), datap);
1264 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1265 return copy_regset_to_user(child, &user_x86_32_view,
1267 sizeof(struct user32_fxsr_struct),
1270 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1271 return copy_regset_from_user(child, &user_x86_32_view,
1273 sizeof(struct user32_fxsr_struct),
1277 return compat_ptrace_request(child, request, addr, data);
1281 put_task_struct(child);
1285 #endif /* CONFIG_IA32_EMULATION */
1287 #ifdef CONFIG_X86_64
1289 static const struct user_regset x86_64_regsets[] = {
1290 [REGSET_GENERAL] = {
1291 .core_note_type = NT_PRSTATUS,
1292 .n = sizeof(struct user_regs_struct) / sizeof(long),
1293 .size = sizeof(long), .align = sizeof(long),
1294 .get = genregs_get, .set = genregs_set
1297 .core_note_type = NT_PRFPREG,
1298 .n = sizeof(struct user_i387_struct) / sizeof(long),
1299 .size = sizeof(long), .align = sizeof(long),
1300 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1304 static const struct user_regset_view user_x86_64_view = {
1305 .name = "x86_64", .e_machine = EM_X86_64,
1306 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1309 #else /* CONFIG_X86_32 */
1311 #define user_regs_struct32 user_regs_struct
1312 #define genregs32_get genregs_get
1313 #define genregs32_set genregs_set
1315 #endif /* CONFIG_X86_64 */
1317 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1318 static const struct user_regset x86_32_regsets[] = {
1319 [REGSET_GENERAL] = {
1320 .core_note_type = NT_PRSTATUS,
1321 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1322 .size = sizeof(u32), .align = sizeof(u32),
1323 .get = genregs32_get, .set = genregs32_set
1326 .core_note_type = NT_PRFPREG,
1327 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1328 .size = sizeof(u32), .align = sizeof(u32),
1329 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1332 .core_note_type = NT_PRXFPREG,
1333 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1334 .size = sizeof(u32), .align = sizeof(u32),
1335 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1338 .core_note_type = NT_386_TLS,
1339 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1340 .size = sizeof(struct user_desc),
1341 .align = sizeof(struct user_desc),
1342 .active = regset_tls_active,
1343 .get = regset_tls_get, .set = regset_tls_set
1347 static const struct user_regset_view user_x86_32_view = {
1348 .name = "i386", .e_machine = EM_386,
1349 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1353 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1355 #ifdef CONFIG_IA32_EMULATION
1356 if (test_tsk_thread_flag(task, TIF_IA32))
1358 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1359 return &user_x86_32_view;
1361 #ifdef CONFIG_X86_64
1362 return &user_x86_64_view;
1366 #ifdef CONFIG_X86_32
1368 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1370 struct siginfo info;
1372 tsk->thread.trap_no = 1;
1373 tsk->thread.error_code = error_code;
1375 memset(&info, 0, sizeof(info));
1376 info.si_signo = SIGTRAP;
1377 info.si_code = TRAP_BRKPT;
1380 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1382 /* Send us the fake SIGTRAP */
1383 force_sig_info(SIGTRAP, &info, tsk);
1386 /* notification of system call entry/exit
1387 * - triggered by current->work.syscall_trace
1389 __attribute__((regparm(3)))
1390 int do_syscall_trace(struct pt_regs *regs, int entryexit)
1392 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1394 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1397 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
1400 /* do the secure computing check first */
1402 secure_computing(regs->orig_ax);
1404 if (unlikely(current->audit_context)) {
1406 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1408 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1409 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1410 * not used, entry.S will call us only on syscall exit, not
1411 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1412 * calling send_sigtrap() on syscall entry.
1414 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1415 * is_singlestep is false, despite his name, so we will still do
1416 * the correct thing.
1418 else if (is_singlestep)
1422 if (!(current->ptrace & PT_PTRACED))
1425 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1426 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1427 * here. We have to check this and return */
1428 if (is_sysemu && entryexit)
1431 /* Fake a debug trap */
1433 send_sigtrap(current, regs, 0);
1435 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
1438 /* the 0x80 provides a way for the tracing parent to distinguish
1439 between a syscall stop and SIGTRAP delivery */
1440 /* Note that the debugger could change the result of test_thread_flag!*/
1441 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
1444 * this isn't the same as continuing with a signal, but it will do
1445 * for normal use. strace only continues with a signal if the
1446 * stopping signal is not SIGTRAP. -brl
1448 if (current->exit_code) {
1449 send_sig(current->exit_code, current, 1);
1450 current->exit_code = 0;
1454 if (unlikely(current->audit_context) && !entryexit)
1455 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1456 regs->bx, regs->cx, regs->dx, regs->si);
1460 regs->orig_ax = -1; /* force skip of syscall restarting */
1461 if (unlikely(current->audit_context))
1462 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1466 #else /* CONFIG_X86_64 */
1468 static void syscall_trace(struct pt_regs *regs)
1472 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1474 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1475 current_thread_info()->flags, current->ptrace);
1478 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1481 * this isn't the same as continuing with a signal, but it will do
1482 * for normal use. strace only continues with a signal if the
1483 * stopping signal is not SIGTRAP. -brl
1485 if (current->exit_code) {
1486 send_sig(current->exit_code, current, 1);
1487 current->exit_code = 0;
1491 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1493 /* do the secure computing check first */
1494 secure_computing(regs->orig_ax);
1496 if (test_thread_flag(TIF_SYSCALL_TRACE)
1497 && (current->ptrace & PT_PTRACED))
1498 syscall_trace(regs);
1500 if (unlikely(current->audit_context)) {
1501 if (test_thread_flag(TIF_IA32)) {
1502 audit_syscall_entry(AUDIT_ARCH_I386,
1505 regs->dx, regs->si);
1507 audit_syscall_entry(AUDIT_ARCH_X86_64,
1510 regs->dx, regs->r10);
1515 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1517 if (unlikely(current->audit_context))
1518 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1520 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1521 || test_thread_flag(TIF_SINGLESTEP))
1522 && (current->ptrace & PT_PTRACED))
1523 syscall_trace(regs);
1526 #endif /* CONFIG_X86_32 */