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>
24 #include <linux/workqueue.h>
26 #include <asm/uaccess.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/processor.h>
31 #include <asm/debugreg.h>
34 #include <asm/prctl.h>
35 #include <asm/proto.h>
40 #define CREATE_TRACE_POINTS
41 #include <trace/events/syscalls.h>
47 REGSET_IOPERM64 = REGSET_XFP,
52 struct pt_regs_offset {
57 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
58 #define REG_OFFSET_END {.name = NULL, .offset = 0}
60 static const struct pt_regs_offset regoffset_table[] = {
84 REG_OFFSET_NAME(orig_ax),
87 REG_OFFSET_NAME(flags),
94 * regs_query_register_offset() - query register offset from its name
95 * @name: the name of a register
97 * regs_query_register_offset() returns the offset of a register in struct
98 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
100 int regs_query_register_offset(const char *name)
102 const struct pt_regs_offset *roff;
103 for (roff = regoffset_table; roff->name != NULL; roff++)
104 if (!strcmp(roff->name, name))
110 * regs_query_register_name() - query register name from its offset
111 * @offset: the offset of a register in struct pt_regs.
113 * regs_query_register_name() returns the name of a register from its
114 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
116 const char *regs_query_register_name(unsigned int offset)
118 const struct pt_regs_offset *roff;
119 for (roff = regoffset_table; roff->name != NULL; roff++)
120 if (roff->offset == offset)
125 static const int arg_offs_table[] = {
127 [0] = offsetof(struct pt_regs, ax),
128 [1] = offsetof(struct pt_regs, dx),
129 [2] = offsetof(struct pt_regs, cx)
130 #else /* CONFIG_X86_64 */
131 [0] = offsetof(struct pt_regs, di),
132 [1] = offsetof(struct pt_regs, si),
133 [2] = offsetof(struct pt_regs, dx),
134 [3] = offsetof(struct pt_regs, cx),
135 [4] = offsetof(struct pt_regs, r8),
136 [5] = offsetof(struct pt_regs, r9)
141 * regs_get_argument_nth() - get Nth argument at function call
142 * @regs: pt_regs which contains registers at function entry.
143 * @n: argument number.
145 * regs_get_argument_nth() returns @n th argument of a function call.
146 * Since usually the kernel stack will be changed right after function entry,
147 * you must use this at function entry. If the @n th entry is NOT in the
148 * kernel stack or pt_regs, this returns 0.
150 unsigned long regs_get_argument_nth(struct pt_regs *regs, unsigned int n)
152 if (n < ARRAY_SIZE(arg_offs_table))
153 return *((unsigned long *)regs + arg_offs_table[n]);
156 * The typical case: arg n is on the stack.
157 * (Note: stack[0] = return address, so skip it)
159 n -= ARRAY_SIZE(arg_offs_table);
160 return regs_get_kernel_stack_nth(regs, 1 + n);
165 * does not yet catch signals sent when the child dies.
166 * in exit.c or in signal.c.
170 * Determines which flags the user has access to [1 = access, 0 = no access].
172 #define FLAG_MASK_32 ((unsigned long) \
173 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
174 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
175 X86_EFLAGS_SF | X86_EFLAGS_TF | \
176 X86_EFLAGS_DF | X86_EFLAGS_OF | \
177 X86_EFLAGS_RF | X86_EFLAGS_AC))
180 * Determines whether a value may be installed in a segment register.
182 static inline bool invalid_selector(u16 value)
184 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
189 #define FLAG_MASK FLAG_MASK_32
191 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
193 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
194 return ®s->bx + (regno >> 2);
197 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
200 * Returning the value truncates it to 16 bits.
203 if (offset != offsetof(struct user_regs_struct, gs))
204 retval = *pt_regs_access(task_pt_regs(task), offset);
207 retval = get_user_gs(task_pt_regs(task));
209 retval = task_user_gs(task);
214 static int set_segment_reg(struct task_struct *task,
215 unsigned long offset, u16 value)
218 * The value argument was already truncated to 16 bits.
220 if (invalid_selector(value))
224 * For %cs and %ss we cannot permit a null selector.
225 * We can permit a bogus selector as long as it has USER_RPL.
226 * Null selectors are fine for other segment registers, but
227 * we will never get back to user mode with invalid %cs or %ss
228 * and will take the trap in iret instead. Much code relies
229 * on user_mode() to distinguish a user trap frame (which can
230 * safely use invalid selectors) from a kernel trap frame.
233 case offsetof(struct user_regs_struct, cs):
234 case offsetof(struct user_regs_struct, ss):
235 if (unlikely(value == 0))
239 *pt_regs_access(task_pt_regs(task), offset) = value;
242 case offsetof(struct user_regs_struct, gs):
244 set_user_gs(task_pt_regs(task), value);
246 task_user_gs(task) = value;
252 static unsigned long debugreg_addr_limit(struct task_struct *task)
254 return TASK_SIZE - 3;
257 #else /* CONFIG_X86_64 */
259 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
261 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
263 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
264 return ®s->r15 + (offset / sizeof(regs->r15));
267 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
270 * Returning the value truncates it to 16 bits.
275 case offsetof(struct user_regs_struct, fs):
276 if (task == current) {
277 /* Older gas can't assemble movq %?s,%r?? */
278 asm("movl %%fs,%0" : "=r" (seg));
281 return task->thread.fsindex;
282 case offsetof(struct user_regs_struct, gs):
283 if (task == current) {
284 asm("movl %%gs,%0" : "=r" (seg));
287 return task->thread.gsindex;
288 case offsetof(struct user_regs_struct, ds):
289 if (task == current) {
290 asm("movl %%ds,%0" : "=r" (seg));
293 return task->thread.ds;
294 case offsetof(struct user_regs_struct, es):
295 if (task == current) {
296 asm("movl %%es,%0" : "=r" (seg));
299 return task->thread.es;
301 case offsetof(struct user_regs_struct, cs):
302 case offsetof(struct user_regs_struct, ss):
305 return *pt_regs_access(task_pt_regs(task), offset);
308 static int set_segment_reg(struct task_struct *task,
309 unsigned long offset, u16 value)
312 * The value argument was already truncated to 16 bits.
314 if (invalid_selector(value))
318 case offsetof(struct user_regs_struct,fs):
320 * If this is setting fs as for normal 64-bit use but
321 * setting fs_base has implicitly changed it, leave it.
323 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
324 task->thread.fs != 0) ||
325 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
326 task->thread.fs == 0))
328 task->thread.fsindex = value;
330 loadsegment(fs, task->thread.fsindex);
332 case offsetof(struct user_regs_struct,gs):
334 * If this is setting gs as for normal 64-bit use but
335 * setting gs_base has implicitly changed it, leave it.
337 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
338 task->thread.gs != 0) ||
339 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
340 task->thread.gs == 0))
342 task->thread.gsindex = value;
344 load_gs_index(task->thread.gsindex);
346 case offsetof(struct user_regs_struct,ds):
347 task->thread.ds = value;
349 loadsegment(ds, task->thread.ds);
351 case offsetof(struct user_regs_struct,es):
352 task->thread.es = value;
354 loadsegment(es, task->thread.es);
358 * Can't actually change these in 64-bit mode.
360 case offsetof(struct user_regs_struct,cs):
361 if (unlikely(value == 0))
363 #ifdef CONFIG_IA32_EMULATION
364 if (test_tsk_thread_flag(task, TIF_IA32))
365 task_pt_regs(task)->cs = value;
368 case offsetof(struct user_regs_struct,ss):
369 if (unlikely(value == 0))
371 #ifdef CONFIG_IA32_EMULATION
372 if (test_tsk_thread_flag(task, TIF_IA32))
373 task_pt_regs(task)->ss = value;
381 static unsigned long debugreg_addr_limit(struct task_struct *task)
383 #ifdef CONFIG_IA32_EMULATION
384 if (test_tsk_thread_flag(task, TIF_IA32))
385 return IA32_PAGE_OFFSET - 3;
387 return TASK_SIZE_MAX - 7;
390 #endif /* CONFIG_X86_32 */
392 static unsigned long get_flags(struct task_struct *task)
394 unsigned long retval = task_pt_regs(task)->flags;
397 * If the debugger set TF, hide it from the readout.
399 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
400 retval &= ~X86_EFLAGS_TF;
405 static int set_flags(struct task_struct *task, unsigned long value)
407 struct pt_regs *regs = task_pt_regs(task);
410 * If the user value contains TF, mark that
411 * it was not "us" (the debugger) that set it.
412 * If not, make sure it stays set if we had.
414 if (value & X86_EFLAGS_TF)
415 clear_tsk_thread_flag(task, TIF_FORCED_TF);
416 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
417 value |= X86_EFLAGS_TF;
419 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
424 static int putreg(struct task_struct *child,
425 unsigned long offset, unsigned long value)
428 case offsetof(struct user_regs_struct, cs):
429 case offsetof(struct user_regs_struct, ds):
430 case offsetof(struct user_regs_struct, es):
431 case offsetof(struct user_regs_struct, fs):
432 case offsetof(struct user_regs_struct, gs):
433 case offsetof(struct user_regs_struct, ss):
434 return set_segment_reg(child, offset, value);
436 case offsetof(struct user_regs_struct, flags):
437 return set_flags(child, value);
441 * Orig_ax is really just a flag with small positive and
442 * negative values, so make sure to always sign-extend it
443 * from 32 bits so that it works correctly regardless of
444 * whether we come from a 32-bit environment or not.
446 case offsetof(struct user_regs_struct, orig_ax):
447 value = (long) (s32) value;
450 case offsetof(struct user_regs_struct,fs_base):
451 if (value >= TASK_SIZE_OF(child))
454 * When changing the segment base, use do_arch_prctl
455 * to set either thread.fs or thread.fsindex and the
456 * corresponding GDT slot.
458 if (child->thread.fs != value)
459 return do_arch_prctl(child, ARCH_SET_FS, value);
461 case offsetof(struct user_regs_struct,gs_base):
463 * Exactly the same here as the %fs handling above.
465 if (value >= TASK_SIZE_OF(child))
467 if (child->thread.gs != value)
468 return do_arch_prctl(child, ARCH_SET_GS, value);
473 *pt_regs_access(task_pt_regs(child), offset) = value;
477 static unsigned long getreg(struct task_struct *task, unsigned long offset)
480 case offsetof(struct user_regs_struct, cs):
481 case offsetof(struct user_regs_struct, ds):
482 case offsetof(struct user_regs_struct, es):
483 case offsetof(struct user_regs_struct, fs):
484 case offsetof(struct user_regs_struct, gs):
485 case offsetof(struct user_regs_struct, ss):
486 return get_segment_reg(task, offset);
488 case offsetof(struct user_regs_struct, flags):
489 return get_flags(task);
492 case offsetof(struct user_regs_struct, fs_base): {
494 * do_arch_prctl may have used a GDT slot instead of
495 * the MSR. To userland, it appears the same either
496 * way, except the %fs segment selector might not be 0.
498 unsigned int seg = task->thread.fsindex;
499 if (task->thread.fs != 0)
500 return task->thread.fs;
502 asm("movl %%fs,%0" : "=r" (seg));
503 if (seg != FS_TLS_SEL)
505 return get_desc_base(&task->thread.tls_array[FS_TLS]);
507 case offsetof(struct user_regs_struct, gs_base): {
509 * Exactly the same here as the %fs handling above.
511 unsigned int seg = task->thread.gsindex;
512 if (task->thread.gs != 0)
513 return task->thread.gs;
515 asm("movl %%gs,%0" : "=r" (seg));
516 if (seg != GS_TLS_SEL)
518 return get_desc_base(&task->thread.tls_array[GS_TLS]);
523 return *pt_regs_access(task_pt_regs(task), offset);
526 static int genregs_get(struct task_struct *target,
527 const struct user_regset *regset,
528 unsigned int pos, unsigned int count,
529 void *kbuf, void __user *ubuf)
532 unsigned long *k = kbuf;
534 *k++ = getreg(target, pos);
539 unsigned long __user *u = ubuf;
541 if (__put_user(getreg(target, pos), u++))
551 static int genregs_set(struct task_struct *target,
552 const struct user_regset *regset,
553 unsigned int pos, unsigned int count,
554 const void *kbuf, const void __user *ubuf)
558 const unsigned long *k = kbuf;
559 while (count > 0 && !ret) {
560 ret = putreg(target, pos, *k++);
565 const unsigned long __user *u = ubuf;
566 while (count > 0 && !ret) {
568 ret = __get_user(word, u++);
571 ret = putreg(target, pos, word);
580 * This function is trivial and will be inlined by the compiler.
581 * Having it separates the implementation details of debug
582 * registers from the interface details of ptrace.
584 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
587 case 0: return child->thread.debugreg0;
588 case 1: return child->thread.debugreg1;
589 case 2: return child->thread.debugreg2;
590 case 3: return child->thread.debugreg3;
591 case 6: return child->thread.debugreg6;
592 case 7: return child->thread.debugreg7;
597 static int ptrace_set_debugreg(struct task_struct *child,
598 int n, unsigned long data)
602 if (unlikely(n == 4 || n == 5))
605 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
609 case 0: child->thread.debugreg0 = data; break;
610 case 1: child->thread.debugreg1 = data; break;
611 case 2: child->thread.debugreg2 = data; break;
612 case 3: child->thread.debugreg3 = data; break;
615 if ((data & ~0xffffffffUL) != 0)
617 child->thread.debugreg6 = data;
622 * Sanity-check data. Take one half-byte at once with
623 * check = (val >> (16 + 4*i)) & 0xf. It contains the
624 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
625 * 2 and 3 are LENi. Given a list of invalid values,
626 * we do mask |= 1 << invalid_value, so that
627 * (mask >> check) & 1 is a correct test for invalid
630 * R/Wi contains the type of the breakpoint /
631 * watchpoint, LENi contains the length of the watched
632 * data in the watchpoint case.
634 * The invalid values are:
635 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
636 * - R/Wi == 0x10 (break on I/O reads or writes), so
638 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
641 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
643 * See the Intel Manual "System Programming Guide",
646 * Note that LENi == 0x10 is defined on x86_64 in long
647 * mode (i.e. even for 32-bit userspace software, but
648 * 64-bit kernel), so the x86_64 mask value is 0x5454.
649 * See the AMD manual no. 24593 (AMD64 System Programming)
652 #define DR7_MASK 0x5f54
654 #define DR7_MASK 0x5554
656 data &= ~DR_CONTROL_RESERVED;
657 for (i = 0; i < 4; i++)
658 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
660 child->thread.debugreg7 = data;
662 set_tsk_thread_flag(child, TIF_DEBUG);
664 clear_tsk_thread_flag(child, TIF_DEBUG);
672 * These access the current or another (stopped) task's io permission
673 * bitmap for debugging or core dump.
675 static int ioperm_active(struct task_struct *target,
676 const struct user_regset *regset)
678 return target->thread.io_bitmap_max / regset->size;
681 static int ioperm_get(struct task_struct *target,
682 const struct user_regset *regset,
683 unsigned int pos, unsigned int count,
684 void *kbuf, void __user *ubuf)
686 if (!target->thread.io_bitmap_ptr)
689 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
690 target->thread.io_bitmap_ptr,
694 #ifdef CONFIG_X86_PTRACE_BTS
696 * A branch trace store context.
698 * Contexts may only be installed by ptrace_bts_config() and only for
701 * Contexts are destroyed when the tracee is detached from the tracer.
702 * The actual destruction work requires interrupts enabled, so the
703 * work is deferred and will be scheduled during __ptrace_unlink().
705 * Contexts hold an additional task_struct reference on the traced
706 * task, as well as a reference on the tracer's mm.
708 * Ptrace already holds a task_struct for the duration of ptrace operations,
709 * but since destruction is deferred, it may be executed after both
710 * tracer and tracee exited.
713 /* The branch trace handle. */
714 struct bts_tracer *tracer;
716 /* The buffer used to store the branch trace and its size. */
720 /* The mm that paid for the above buffer. */
721 struct mm_struct *mm;
723 /* The task this context belongs to. */
724 struct task_struct *task;
726 /* The signal to send on a bts buffer overflow. */
727 unsigned int bts_ovfl_signal;
729 /* The work struct to destroy a context. */
730 struct work_struct work;
733 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
738 err = account_locked_memory(current->mm, current->signal->rlim, size);
742 buffer = kzalloc(size, GFP_KERNEL);
746 context->buffer = buffer;
747 context->size = size;
748 context->mm = get_task_mm(current);
753 refund_locked_memory(current->mm, size);
757 static inline void free_bts_buffer(struct bts_context *context)
759 if (!context->buffer)
762 kfree(context->buffer);
763 context->buffer = NULL;
765 refund_locked_memory(context->mm, context->size);
772 static void free_bts_context_work(struct work_struct *w)
774 struct bts_context *context;
776 context = container_of(w, struct bts_context, work);
778 ds_release_bts(context->tracer);
779 put_task_struct(context->task);
780 free_bts_buffer(context);
784 static inline void free_bts_context(struct bts_context *context)
786 INIT_WORK(&context->work, free_bts_context_work);
787 schedule_work(&context->work);
790 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
792 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
794 context->task = task;
797 get_task_struct(task);
803 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
804 struct bts_struct __user *out)
806 struct bts_context *context;
807 const struct bts_trace *trace;
808 struct bts_struct bts;
809 const unsigned char *at;
812 context = child->bts;
816 trace = ds_read_bts(context->tracer);
820 at = trace->ds.top - ((index + 1) * trace->ds.size);
821 if ((void *)at < trace->ds.begin)
822 at += (trace->ds.n * trace->ds.size);
827 error = trace->read(context->tracer, at, &bts);
831 if (copy_to_user(out, &bts, sizeof(bts)))
837 static int ptrace_bts_drain(struct task_struct *child,
839 struct bts_struct __user *out)
841 struct bts_context *context;
842 const struct bts_trace *trace;
843 const unsigned char *at;
844 int error, drained = 0;
846 context = child->bts;
850 trace = ds_read_bts(context->tracer);
857 if (size < (trace->ds.top - trace->ds.begin))
860 for (at = trace->ds.begin; (void *)at < trace->ds.top;
861 out++, drained++, at += trace->ds.size) {
862 struct bts_struct bts;
864 error = trace->read(context->tracer, at, &bts);
868 if (copy_to_user(out, &bts, sizeof(bts)))
872 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
874 error = ds_reset_bts(context->tracer);
881 static int ptrace_bts_config(struct task_struct *child,
883 const struct ptrace_bts_config __user *ucfg)
885 struct bts_context *context;
886 struct ptrace_bts_config cfg;
887 unsigned int flags = 0;
889 if (cfg_size < sizeof(cfg))
892 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
895 context = child->bts;
897 context = alloc_bts_context(child);
901 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
906 context->bts_ovfl_signal = cfg.signal;
909 ds_release_bts(context->tracer);
910 context->tracer = NULL;
912 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
915 free_bts_buffer(context);
919 err = alloc_bts_buffer(context, cfg.size);
924 if (cfg.flags & PTRACE_BTS_O_TRACE)
927 if (cfg.flags & PTRACE_BTS_O_SCHED)
928 flags |= BTS_TIMESTAMPS;
931 ds_request_bts_task(child, context->buffer, context->size,
932 NULL, (size_t)-1, flags);
933 if (unlikely(IS_ERR(context->tracer))) {
934 int error = PTR_ERR(context->tracer);
936 free_bts_buffer(context);
937 context->tracer = NULL;
944 static int ptrace_bts_status(struct task_struct *child,
946 struct ptrace_bts_config __user *ucfg)
948 struct bts_context *context;
949 const struct bts_trace *trace;
950 struct ptrace_bts_config cfg;
952 context = child->bts;
956 if (cfg_size < sizeof(cfg))
959 trace = ds_read_bts(context->tracer);
963 memset(&cfg, 0, sizeof(cfg));
964 cfg.size = trace->ds.end - trace->ds.begin;
965 cfg.signal = context->bts_ovfl_signal;
966 cfg.bts_size = sizeof(struct bts_struct);
969 cfg.flags |= PTRACE_BTS_O_SIGNAL;
971 if (trace->ds.flags & BTS_USER)
972 cfg.flags |= PTRACE_BTS_O_TRACE;
974 if (trace->ds.flags & BTS_TIMESTAMPS)
975 cfg.flags |= PTRACE_BTS_O_SCHED;
977 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
983 static int ptrace_bts_clear(struct task_struct *child)
985 struct bts_context *context;
986 const struct bts_trace *trace;
988 context = child->bts;
992 trace = ds_read_bts(context->tracer);
996 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
998 return ds_reset_bts(context->tracer);
1001 static int ptrace_bts_size(struct task_struct *child)
1003 struct bts_context *context;
1004 const struct bts_trace *trace;
1006 context = child->bts;
1010 trace = ds_read_bts(context->tracer);
1014 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1018 * Called from __ptrace_unlink() after the child has been moved back
1019 * to its original parent.
1021 void ptrace_bts_untrace(struct task_struct *child)
1023 if (unlikely(child->bts)) {
1024 free_bts_context(child->bts);
1028 #endif /* CONFIG_X86_PTRACE_BTS */
1031 * Called by kernel/ptrace.c when detaching..
1033 * Make sure the single step bit is not set.
1035 void ptrace_disable(struct task_struct *child)
1037 user_disable_single_step(child);
1038 #ifdef TIF_SYSCALL_EMU
1039 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1043 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1044 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1047 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1050 unsigned long __user *datap = (unsigned long __user *)data;
1053 /* read the word at location addr in the USER area. */
1054 case PTRACE_PEEKUSR: {
1058 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1059 addr >= sizeof(struct user))
1062 tmp = 0; /* Default return condition */
1063 if (addr < sizeof(struct user_regs_struct))
1064 tmp = getreg(child, addr);
1065 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1066 addr <= offsetof(struct user, u_debugreg[7])) {
1067 addr -= offsetof(struct user, u_debugreg[0]);
1068 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1070 ret = put_user(tmp, datap);
1074 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1076 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1077 addr >= sizeof(struct user))
1080 if (addr < sizeof(struct user_regs_struct))
1081 ret = putreg(child, addr, data);
1082 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1083 addr <= offsetof(struct user, u_debugreg[7])) {
1084 addr -= offsetof(struct user, u_debugreg[0]);
1085 ret = ptrace_set_debugreg(child,
1086 addr / sizeof(data), data);
1090 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1091 return copy_regset_to_user(child,
1092 task_user_regset_view(current),
1094 0, sizeof(struct user_regs_struct),
1097 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1098 return copy_regset_from_user(child,
1099 task_user_regset_view(current),
1101 0, sizeof(struct user_regs_struct),
1104 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1105 return copy_regset_to_user(child,
1106 task_user_regset_view(current),
1108 0, sizeof(struct user_i387_struct),
1111 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1112 return copy_regset_from_user(child,
1113 task_user_regset_view(current),
1115 0, sizeof(struct user_i387_struct),
1118 #ifdef CONFIG_X86_32
1119 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1120 return copy_regset_to_user(child, &user_x86_32_view,
1122 0, sizeof(struct user_fxsr_struct),
1125 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1126 return copy_regset_from_user(child, &user_x86_32_view,
1128 0, sizeof(struct user_fxsr_struct),
1132 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1133 case PTRACE_GET_THREAD_AREA:
1136 ret = do_get_thread_area(child, addr,
1137 (struct user_desc __user *) data);
1140 case PTRACE_SET_THREAD_AREA:
1143 ret = do_set_thread_area(child, addr,
1144 (struct user_desc __user *) data, 0);
1148 #ifdef CONFIG_X86_64
1149 /* normal 64bit interface to access TLS data.
1150 Works just like arch_prctl, except that the arguments
1152 case PTRACE_ARCH_PRCTL:
1153 ret = do_arch_prctl(child, data, addr);
1158 * These bits need more cooking - not enabled yet:
1160 #ifdef CONFIG_X86_PTRACE_BTS
1161 case PTRACE_BTS_CONFIG:
1162 ret = ptrace_bts_config
1163 (child, data, (struct ptrace_bts_config __user *)addr);
1166 case PTRACE_BTS_STATUS:
1167 ret = ptrace_bts_status
1168 (child, data, (struct ptrace_bts_config __user *)addr);
1171 case PTRACE_BTS_SIZE:
1172 ret = ptrace_bts_size(child);
1175 case PTRACE_BTS_GET:
1176 ret = ptrace_bts_read_record
1177 (child, data, (struct bts_struct __user *) addr);
1180 case PTRACE_BTS_CLEAR:
1181 ret = ptrace_bts_clear(child);
1184 case PTRACE_BTS_DRAIN:
1185 ret = ptrace_bts_drain
1186 (child, data, (struct bts_struct __user *) addr);
1188 #endif /* CONFIG_X86_PTRACE_BTS */
1191 ret = ptrace_request(child, request, addr, data);
1198 #ifdef CONFIG_IA32_EMULATION
1200 #include <linux/compat.h>
1201 #include <linux/syscalls.h>
1202 #include <asm/ia32.h>
1203 #include <asm/user32.h>
1206 case offsetof(struct user32, regs.l): \
1207 regs->q = value; break
1210 case offsetof(struct user32, regs.rs): \
1211 return set_segment_reg(child, \
1212 offsetof(struct user_regs_struct, rs), \
1216 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1218 struct pt_regs *regs = task_pt_regs(child);
1239 case offsetof(struct user32, regs.orig_eax):
1241 * Sign-extend the value so that orig_eax = -1
1242 * causes (long)orig_ax < 0 tests to fire correctly.
1244 regs->orig_ax = (long) (s32) value;
1247 case offsetof(struct user32, regs.eflags):
1248 return set_flags(child, value);
1250 case offsetof(struct user32, u_debugreg[0]) ...
1251 offsetof(struct user32, u_debugreg[7]):
1252 regno -= offsetof(struct user32, u_debugreg[0]);
1253 return ptrace_set_debugreg(child, regno / 4, value);
1256 if (regno > sizeof(struct user32) || (regno & 3))
1260 * Other dummy fields in the virtual user structure
1272 case offsetof(struct user32, regs.l): \
1273 *val = regs->q; break
1276 case offsetof(struct user32, regs.rs): \
1277 *val = get_segment_reg(child, \
1278 offsetof(struct user_regs_struct, rs)); \
1281 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1283 struct pt_regs *regs = task_pt_regs(child);
1301 R32(orig_eax, orig_ax);
1305 case offsetof(struct user32, regs.eflags):
1306 *val = get_flags(child);
1309 case offsetof(struct user32, u_debugreg[0]) ...
1310 offsetof(struct user32, u_debugreg[7]):
1311 regno -= offsetof(struct user32, u_debugreg[0]);
1312 *val = ptrace_get_debugreg(child, regno / 4);
1316 if (regno > sizeof(struct user32) || (regno & 3))
1320 * Other dummy fields in the virtual user structure
1332 static int genregs32_get(struct task_struct *target,
1333 const struct user_regset *regset,
1334 unsigned int pos, unsigned int count,
1335 void *kbuf, void __user *ubuf)
1338 compat_ulong_t *k = kbuf;
1340 getreg32(target, pos, k++);
1341 count -= sizeof(*k);
1345 compat_ulong_t __user *u = ubuf;
1347 compat_ulong_t word;
1348 getreg32(target, pos, &word);
1349 if (__put_user(word, u++))
1351 count -= sizeof(*u);
1359 static int genregs32_set(struct task_struct *target,
1360 const struct user_regset *regset,
1361 unsigned int pos, unsigned int count,
1362 const void *kbuf, const void __user *ubuf)
1366 const compat_ulong_t *k = kbuf;
1367 while (count > 0 && !ret) {
1368 ret = putreg32(target, pos, *k++);
1369 count -= sizeof(*k);
1373 const compat_ulong_t __user *u = ubuf;
1374 while (count > 0 && !ret) {
1375 compat_ulong_t word;
1376 ret = __get_user(word, u++);
1379 ret = putreg32(target, pos, word);
1380 count -= sizeof(*u);
1387 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1388 compat_ulong_t caddr, compat_ulong_t cdata)
1390 unsigned long addr = caddr;
1391 unsigned long data = cdata;
1392 void __user *datap = compat_ptr(data);
1397 case PTRACE_PEEKUSR:
1398 ret = getreg32(child, addr, &val);
1400 ret = put_user(val, (__u32 __user *)datap);
1403 case PTRACE_POKEUSR:
1404 ret = putreg32(child, addr, data);
1407 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1408 return copy_regset_to_user(child, &user_x86_32_view,
1410 0, sizeof(struct user_regs_struct32),
1413 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1414 return copy_regset_from_user(child, &user_x86_32_view,
1416 sizeof(struct user_regs_struct32),
1419 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1420 return copy_regset_to_user(child, &user_x86_32_view,
1422 sizeof(struct user_i387_ia32_struct),
1425 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1426 return copy_regset_from_user(
1427 child, &user_x86_32_view, REGSET_FP,
1428 0, sizeof(struct user_i387_ia32_struct), datap);
1430 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1431 return copy_regset_to_user(child, &user_x86_32_view,
1433 sizeof(struct user32_fxsr_struct),
1436 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1437 return copy_regset_from_user(child, &user_x86_32_view,
1439 sizeof(struct user32_fxsr_struct),
1442 case PTRACE_GET_THREAD_AREA:
1443 case PTRACE_SET_THREAD_AREA:
1444 #ifdef CONFIG_X86_PTRACE_BTS
1445 case PTRACE_BTS_CONFIG:
1446 case PTRACE_BTS_STATUS:
1447 case PTRACE_BTS_SIZE:
1448 case PTRACE_BTS_GET:
1449 case PTRACE_BTS_CLEAR:
1450 case PTRACE_BTS_DRAIN:
1451 #endif /* CONFIG_X86_PTRACE_BTS */
1452 return arch_ptrace(child, request, addr, data);
1455 return compat_ptrace_request(child, request, addr, data);
1461 #endif /* CONFIG_IA32_EMULATION */
1463 #ifdef CONFIG_X86_64
1465 static const struct user_regset x86_64_regsets[] = {
1466 [REGSET_GENERAL] = {
1467 .core_note_type = NT_PRSTATUS,
1468 .n = sizeof(struct user_regs_struct) / sizeof(long),
1469 .size = sizeof(long), .align = sizeof(long),
1470 .get = genregs_get, .set = genregs_set
1473 .core_note_type = NT_PRFPREG,
1474 .n = sizeof(struct user_i387_struct) / sizeof(long),
1475 .size = sizeof(long), .align = sizeof(long),
1476 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1478 [REGSET_IOPERM64] = {
1479 .core_note_type = NT_386_IOPERM,
1480 .n = IO_BITMAP_LONGS,
1481 .size = sizeof(long), .align = sizeof(long),
1482 .active = ioperm_active, .get = ioperm_get
1486 static const struct user_regset_view user_x86_64_view = {
1487 .name = "x86_64", .e_machine = EM_X86_64,
1488 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1491 #else /* CONFIG_X86_32 */
1493 #define user_regs_struct32 user_regs_struct
1494 #define genregs32_get genregs_get
1495 #define genregs32_set genregs_set
1497 #define user_i387_ia32_struct user_i387_struct
1498 #define user32_fxsr_struct user_fxsr_struct
1500 #endif /* CONFIG_X86_64 */
1502 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1503 static const struct user_regset x86_32_regsets[] = {
1504 [REGSET_GENERAL] = {
1505 .core_note_type = NT_PRSTATUS,
1506 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1507 .size = sizeof(u32), .align = sizeof(u32),
1508 .get = genregs32_get, .set = genregs32_set
1511 .core_note_type = NT_PRFPREG,
1512 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1513 .size = sizeof(u32), .align = sizeof(u32),
1514 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1517 .core_note_type = NT_PRXFPREG,
1518 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1519 .size = sizeof(u32), .align = sizeof(u32),
1520 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1523 .core_note_type = NT_386_TLS,
1524 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1525 .size = sizeof(struct user_desc),
1526 .align = sizeof(struct user_desc),
1527 .active = regset_tls_active,
1528 .get = regset_tls_get, .set = regset_tls_set
1530 [REGSET_IOPERM32] = {
1531 .core_note_type = NT_386_IOPERM,
1532 .n = IO_BITMAP_BYTES / sizeof(u32),
1533 .size = sizeof(u32), .align = sizeof(u32),
1534 .active = ioperm_active, .get = ioperm_get
1538 static const struct user_regset_view user_x86_32_view = {
1539 .name = "i386", .e_machine = EM_386,
1540 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1544 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1546 #ifdef CONFIG_IA32_EMULATION
1547 if (test_tsk_thread_flag(task, TIF_IA32))
1549 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1550 return &user_x86_32_view;
1552 #ifdef CONFIG_X86_64
1553 return &user_x86_64_view;
1557 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1558 int error_code, int si_code)
1560 struct siginfo info;
1562 tsk->thread.trap_no = 1;
1563 tsk->thread.error_code = error_code;
1565 memset(&info, 0, sizeof(info));
1566 info.si_signo = SIGTRAP;
1567 info.si_code = si_code;
1570 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1572 /* Send us the fake SIGTRAP */
1573 force_sig_info(SIGTRAP, &info, tsk);
1577 #ifdef CONFIG_X86_32
1579 #elif defined CONFIG_IA32_EMULATION
1580 # define IS_IA32 is_compat_task()
1586 * We must return the syscall number to actually look up in the table.
1587 * This can be -1L to skip running any syscall at all.
1589 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1594 * If we stepped into a sysenter/syscall insn, it trapped in
1595 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1596 * If user-mode had set TF itself, then it's still clear from
1597 * do_debug() and we need to set it again to restore the user
1598 * state. If we entered on the slow path, TF was already set.
1600 if (test_thread_flag(TIF_SINGLESTEP))
1601 regs->flags |= X86_EFLAGS_TF;
1603 /* do the secure computing check first */
1604 secure_computing(regs->orig_ax);
1606 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1609 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1610 tracehook_report_syscall_entry(regs))
1613 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1614 trace_sys_enter(regs, regs->orig_ax);
1616 if (unlikely(current->audit_context)) {
1618 audit_syscall_entry(AUDIT_ARCH_I386,
1621 regs->dx, regs->si);
1622 #ifdef CONFIG_X86_64
1624 audit_syscall_entry(AUDIT_ARCH_X86_64,
1627 regs->dx, regs->r10);
1631 return ret ?: regs->orig_ax;
1634 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1636 if (unlikely(current->audit_context))
1637 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1639 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1640 trace_sys_exit(regs, regs->ax);
1642 if (test_thread_flag(TIF_SYSCALL_TRACE))
1643 tracehook_report_syscall_exit(regs, 0);
1646 * If TIF_SYSCALL_EMU is set, we only get here because of
1647 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1648 * We already reported this syscall instruction in
1649 * syscall_trace_enter(), so don't do any more now.
1651 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1655 * If we are single-stepping, synthesize a trap to follow the
1656 * system call instruction.
1658 if (test_thread_flag(TIF_SINGLESTEP) &&
1659 tracehook_consider_fatal_signal(current, SIGTRAP))
1660 send_sigtrap(current, regs, 0, TRAP_BRKPT);