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
585 * The configuration for a particular BTS hardware implementation.
587 struct bts_configuration {
588 /* the size of a BTS record in bytes; at most BTS_MAX_RECORD_SIZE */
589 unsigned char sizeof_bts;
590 /* the size of a field in the BTS record in bytes */
591 unsigned char sizeof_field;
592 /* a bitmask to enable/disable BTS in DEBUGCTL MSR */
593 unsigned long debugctl_mask;
595 static struct bts_configuration bts_cfg;
597 #define BTS_MAX_RECORD_SIZE (8 * 3)
601 * Branch Trace Store (BTS) uses the following format. Different
602 * architectures vary in the size of those fields.
603 * - source linear address
604 * - destination linear address
607 * Later architectures use 64bit pointers throughout, whereas earlier
608 * architectures use 32bit pointers in 32bit mode.
610 * We compute the base address for the first 8 fields based on:
611 * - the field size stored in the DS configuration
612 * - the relative field position
614 * In order to store additional information in the BTS buffer, we use
615 * a special source address to indicate that the record requires
616 * special interpretation.
618 * Netburst indicated via a bit in the flags field whether the branch
619 * was predicted; this is ignored.
627 bts_escape = (unsigned long)-1,
629 bts_jiffies = bts_flags
632 static inline unsigned long bts_get(const char *base, enum bts_field field)
634 base += (bts_cfg.sizeof_field * field);
635 return *(unsigned long *)base;
638 static inline void bts_set(char *base, enum bts_field field, unsigned long val)
640 base += (bts_cfg.sizeof_field * field);;
641 (*(unsigned long *)base) = val;
645 * Translate a BTS record from the raw format into the bts_struct format
647 * out (out): bts_struct interpretation
648 * raw: raw BTS record
650 static void ptrace_bts_translate_record(struct bts_struct *out, const void *raw)
652 memset(out, 0, sizeof(*out));
653 if (bts_get(raw, bts_from) == bts_escape) {
654 out->qualifier = bts_get(raw, bts_qual);
655 out->variant.jiffies = bts_get(raw, bts_jiffies);
657 out->qualifier = BTS_BRANCH;
658 out->variant.lbr.from_ip = bts_get(raw, bts_from);
659 out->variant.lbr.to_ip = bts_get(raw, bts_to);
663 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
664 struct bts_struct __user *out)
666 struct bts_struct ret;
667 const void *bts_record;
668 size_t bts_index, bts_end;
671 error = ds_get_bts_end(child->bts, &bts_end);
675 if (bts_end <= index)
678 error = ds_get_bts_index(child->bts, &bts_index);
682 /* translate the ptrace bts index into the ds bts index */
683 bts_index += bts_end - (index + 1);
684 if (bts_end <= bts_index)
685 bts_index -= bts_end;
687 error = ds_access_bts(child->bts, bts_index, &bts_record);
691 ptrace_bts_translate_record(&ret, bts_record);
693 if (copy_to_user(out, &ret, sizeof(ret)))
699 static int ptrace_bts_drain(struct task_struct *child,
701 struct bts_struct __user *out)
703 struct bts_struct ret;
704 const unsigned char *raw;
708 error = ds_get_bts_index(child->bts, &end);
712 if (size < (end * sizeof(struct bts_struct)))
715 error = ds_access_bts(child->bts, 0, (const void **)&raw);
719 for (i = 0; i < end; i++, out++, raw += bts_cfg.sizeof_bts) {
720 ptrace_bts_translate_record(&ret, raw);
722 if (copy_to_user(out, &ret, sizeof(ret)))
726 error = ds_clear_bts(child->bts);
733 static int ptrace_bts_config(struct task_struct *child,
735 const struct ptrace_bts_config __user *ucfg)
737 struct ptrace_bts_config cfg;
741 if (!bts_cfg.sizeof_bts)
745 if (cfg_size < sizeof(cfg))
749 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
753 if ((cfg.flags & PTRACE_BTS_O_SIGNAL) &&
754 !(cfg.flags & PTRACE_BTS_O_ALLOC))
757 if (cfg.flags & PTRACE_BTS_O_ALLOC) {
758 bts_ovfl_callback_t ovfl = NULL;
759 unsigned int sig = 0;
762 if (cfg.size < (10 * bts_cfg.sizeof_bts))
765 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
776 (void)ds_release_bts(child->bts);
777 kfree(child->bts_buffer);
780 child->bts_buffer = NULL;
784 child->bts_buffer = kzalloc(cfg.size, GFP_KERNEL);
785 if (!child->bts_buffer)
788 child->bts = ds_request_bts(child, child->bts_buffer, cfg.size,
789 ovfl, /* th = */ (size_t)-1);
790 if (IS_ERR(child->bts)) {
791 error = PTR_ERR(child->bts);
792 kfree(child->bts_buffer);
794 child->bts_buffer = NULL;
798 child->thread.bts_ovfl_signal = sig;
802 if (!child->thread.ds_ctx && cfg.flags)
805 if (cfg.flags & PTRACE_BTS_O_TRACE)
806 child->thread.debugctlmsr |= bts_cfg.debugctl_mask;
808 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
810 if (cfg.flags & PTRACE_BTS_O_SCHED)
811 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
813 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
818 if (child->thread.debugctlmsr)
819 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
821 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
826 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
827 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
831 static int ptrace_bts_status(struct task_struct *child,
833 struct ptrace_bts_config __user *ucfg)
835 struct ptrace_bts_config cfg;
837 const void *base, *max;
840 if (cfg_size < sizeof(cfg))
843 error = ds_get_bts_end(child->bts, &end);
847 error = ds_access_bts(child->bts, /* index = */ 0, &base);
851 error = ds_access_bts(child->bts, /* index = */ end, &max);
855 memset(&cfg, 0, sizeof(cfg));
856 cfg.size = (max - base);
857 cfg.signal = child->thread.bts_ovfl_signal;
858 cfg.bts_size = sizeof(struct bts_struct);
861 cfg.flags |= PTRACE_BTS_O_SIGNAL;
863 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
864 child->thread.debugctlmsr & bts_cfg.debugctl_mask)
865 cfg.flags |= PTRACE_BTS_O_TRACE;
867 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
868 cfg.flags |= PTRACE_BTS_O_SCHED;
870 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
876 static int ptrace_bts_write_record(struct task_struct *child,
877 const struct bts_struct *in)
879 unsigned char bts_record[BTS_MAX_RECORD_SIZE];
881 if (BTS_MAX_RECORD_SIZE < bts_cfg.sizeof_bts)
884 memset(bts_record, 0, bts_cfg.sizeof_bts);
885 switch (in->qualifier) {
890 bts_set(bts_record, bts_from, in->variant.lbr.from_ip);
891 bts_set(bts_record, bts_to, in->variant.lbr.to_ip);
894 case BTS_TASK_ARRIVES:
895 case BTS_TASK_DEPARTS:
896 bts_set(bts_record, bts_from, bts_escape);
897 bts_set(bts_record, bts_qual, in->qualifier);
898 bts_set(bts_record, bts_jiffies, in->variant.jiffies);
905 return ds_write_bts(child->bts, bts_record, bts_cfg.sizeof_bts);
908 void ptrace_bts_take_timestamp(struct task_struct *tsk,
909 enum bts_qualifier qualifier)
911 struct bts_struct rec = {
912 .qualifier = qualifier,
913 .variant.jiffies = jiffies_64
916 ptrace_bts_write_record(tsk, &rec);
919 static const struct bts_configuration bts_cfg_netburst = {
920 .sizeof_bts = sizeof(long) * 3,
921 .sizeof_field = sizeof(long),
922 .debugctl_mask = (1<<2)|(1<<3)|(1<<5)
925 static const struct bts_configuration bts_cfg_pentium_m = {
926 .sizeof_bts = sizeof(long) * 3,
927 .sizeof_field = sizeof(long),
928 .debugctl_mask = (1<<6)|(1<<7)
931 static const struct bts_configuration bts_cfg_core2 = {
934 .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
937 static inline void bts_configure(const struct bts_configuration *cfg)
942 void __cpuinit ptrace_bts_init_intel(struct cpuinfo_x86 *c)
946 switch (c->x86_model) {
948 /* sorry, don't know about them */
951 case 0xE: /* Pentium M */
952 bts_configure(&bts_cfg_pentium_m);
954 default: /* Core2, Atom, ... */
955 bts_configure(&bts_cfg_core2);
960 switch (c->x86_model) {
963 case 0x2: /* Netburst */
964 bts_configure(&bts_cfg_netburst);
967 /* sorry, don't know about them */
972 /* sorry, don't know about them */
976 #endif /* CONFIG_X86_PTRACE_BTS */
979 * Called by kernel/ptrace.c when detaching..
981 * Make sure the single step bit is not set.
983 void ptrace_disable(struct task_struct *child)
985 user_disable_single_step(child);
986 #ifdef TIF_SYSCALL_EMU
987 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
989 #ifdef CONFIG_X86_PTRACE_BTS
991 (void)ds_release_bts(child->bts);
992 kfree(child->bts_buffer);
993 child->bts_buffer = NULL;
995 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
996 if (!child->thread.debugctlmsr)
997 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
999 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
1001 #endif /* CONFIG_X86_PTRACE_BTS */
1004 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1005 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1008 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1011 unsigned long __user *datap = (unsigned long __user *)data;
1014 /* read the word at location addr in the USER area. */
1015 case PTRACE_PEEKUSR: {
1019 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1020 addr >= sizeof(struct user))
1023 tmp = 0; /* Default return condition */
1024 if (addr < sizeof(struct user_regs_struct))
1025 tmp = getreg(child, addr);
1026 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1027 addr <= offsetof(struct user, u_debugreg[7])) {
1028 addr -= offsetof(struct user, u_debugreg[0]);
1029 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1031 ret = put_user(tmp, datap);
1035 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1037 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1038 addr >= sizeof(struct user))
1041 if (addr < sizeof(struct user_regs_struct))
1042 ret = putreg(child, addr, data);
1043 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1044 addr <= offsetof(struct user, u_debugreg[7])) {
1045 addr -= offsetof(struct user, u_debugreg[0]);
1046 ret = ptrace_set_debugreg(child,
1047 addr / sizeof(data), data);
1051 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1052 return copy_regset_to_user(child,
1053 task_user_regset_view(current),
1055 0, sizeof(struct user_regs_struct),
1058 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1059 return copy_regset_from_user(child,
1060 task_user_regset_view(current),
1062 0, sizeof(struct user_regs_struct),
1065 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1066 return copy_regset_to_user(child,
1067 task_user_regset_view(current),
1069 0, sizeof(struct user_i387_struct),
1072 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1073 return copy_regset_from_user(child,
1074 task_user_regset_view(current),
1076 0, sizeof(struct user_i387_struct),
1079 #ifdef CONFIG_X86_32
1080 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1081 return copy_regset_to_user(child, &user_x86_32_view,
1083 0, sizeof(struct user_fxsr_struct),
1086 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1087 return copy_regset_from_user(child, &user_x86_32_view,
1089 0, sizeof(struct user_fxsr_struct),
1093 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1094 case PTRACE_GET_THREAD_AREA:
1097 ret = do_get_thread_area(child, addr,
1098 (struct user_desc __user *) data);
1101 case PTRACE_SET_THREAD_AREA:
1104 ret = do_set_thread_area(child, addr,
1105 (struct user_desc __user *) data, 0);
1109 #ifdef CONFIG_X86_64
1110 /* normal 64bit interface to access TLS data.
1111 Works just like arch_prctl, except that the arguments
1113 case PTRACE_ARCH_PRCTL:
1114 ret = do_arch_prctl(child, data, addr);
1119 * These bits need more cooking - not enabled yet:
1121 #ifdef CONFIG_X86_PTRACE_BTS
1122 case PTRACE_BTS_CONFIG:
1123 ret = ptrace_bts_config
1124 (child, data, (struct ptrace_bts_config __user *)addr);
1127 case PTRACE_BTS_STATUS:
1128 ret = ptrace_bts_status
1129 (child, data, (struct ptrace_bts_config __user *)addr);
1132 case PTRACE_BTS_SIZE: {
1135 ret = ds_get_bts_index(child->bts, &size);
1137 WARN_ON_ONCE(size != (int) size);
1143 case PTRACE_BTS_GET:
1144 ret = ptrace_bts_read_record
1145 (child, data, (struct bts_struct __user *) addr);
1148 case PTRACE_BTS_CLEAR:
1149 ret = ds_clear_bts(child->bts);
1152 case PTRACE_BTS_DRAIN:
1153 ret = ptrace_bts_drain
1154 (child, data, (struct bts_struct __user *) addr);
1156 #endif /* CONFIG_X86_PTRACE_BTS */
1159 ret = ptrace_request(child, request, addr, data);
1166 #ifdef CONFIG_IA32_EMULATION
1168 #include <linux/compat.h>
1169 #include <linux/syscalls.h>
1170 #include <asm/ia32.h>
1171 #include <asm/user32.h>
1174 case offsetof(struct user32, regs.l): \
1175 regs->q = value; break
1178 case offsetof(struct user32, regs.rs): \
1179 return set_segment_reg(child, \
1180 offsetof(struct user_regs_struct, rs), \
1184 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1186 struct pt_regs *regs = task_pt_regs(child);
1207 case offsetof(struct user32, regs.orig_eax):
1209 * Sign-extend the value so that orig_eax = -1
1210 * causes (long)orig_ax < 0 tests to fire correctly.
1212 regs->orig_ax = (long) (s32) value;
1215 case offsetof(struct user32, regs.eflags):
1216 return set_flags(child, value);
1218 case offsetof(struct user32, u_debugreg[0]) ...
1219 offsetof(struct user32, u_debugreg[7]):
1220 regno -= offsetof(struct user32, u_debugreg[0]);
1221 return ptrace_set_debugreg(child, regno / 4, value);
1224 if (regno > sizeof(struct user32) || (regno & 3))
1228 * Other dummy fields in the virtual user structure
1240 case offsetof(struct user32, regs.l): \
1241 *val = regs->q; break
1244 case offsetof(struct user32, regs.rs): \
1245 *val = get_segment_reg(child, \
1246 offsetof(struct user_regs_struct, rs)); \
1249 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1251 struct pt_regs *regs = task_pt_regs(child);
1269 R32(orig_eax, orig_ax);
1273 case offsetof(struct user32, regs.eflags):
1274 *val = get_flags(child);
1277 case offsetof(struct user32, u_debugreg[0]) ...
1278 offsetof(struct user32, u_debugreg[7]):
1279 regno -= offsetof(struct user32, u_debugreg[0]);
1280 *val = ptrace_get_debugreg(child, regno / 4);
1284 if (regno > sizeof(struct user32) || (regno & 3))
1288 * Other dummy fields in the virtual user structure
1300 static int genregs32_get(struct task_struct *target,
1301 const struct user_regset *regset,
1302 unsigned int pos, unsigned int count,
1303 void *kbuf, void __user *ubuf)
1306 compat_ulong_t *k = kbuf;
1308 getreg32(target, pos, k++);
1309 count -= sizeof(*k);
1313 compat_ulong_t __user *u = ubuf;
1315 compat_ulong_t word;
1316 getreg32(target, pos, &word);
1317 if (__put_user(word, u++))
1319 count -= sizeof(*u);
1327 static int genregs32_set(struct task_struct *target,
1328 const struct user_regset *regset,
1329 unsigned int pos, unsigned int count,
1330 const void *kbuf, const void __user *ubuf)
1334 const compat_ulong_t *k = kbuf;
1335 while (count > 0 && !ret) {
1336 ret = putreg32(target, pos, *k++);
1337 count -= sizeof(*k);
1341 const compat_ulong_t __user *u = ubuf;
1342 while (count > 0 && !ret) {
1343 compat_ulong_t word;
1344 ret = __get_user(word, u++);
1347 ret = putreg32(target, pos, word);
1348 count -= sizeof(*u);
1355 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1356 compat_ulong_t caddr, compat_ulong_t cdata)
1358 unsigned long addr = caddr;
1359 unsigned long data = cdata;
1360 void __user *datap = compat_ptr(data);
1365 case PTRACE_PEEKUSR:
1366 ret = getreg32(child, addr, &val);
1368 ret = put_user(val, (__u32 __user *)datap);
1371 case PTRACE_POKEUSR:
1372 ret = putreg32(child, addr, data);
1375 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1376 return copy_regset_to_user(child, &user_x86_32_view,
1378 0, sizeof(struct user_regs_struct32),
1381 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1382 return copy_regset_from_user(child, &user_x86_32_view,
1384 sizeof(struct user_regs_struct32),
1387 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1388 return copy_regset_to_user(child, &user_x86_32_view,
1390 sizeof(struct user_i387_ia32_struct),
1393 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1394 return copy_regset_from_user(
1395 child, &user_x86_32_view, REGSET_FP,
1396 0, sizeof(struct user_i387_ia32_struct), datap);
1398 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1399 return copy_regset_to_user(child, &user_x86_32_view,
1401 sizeof(struct user32_fxsr_struct),
1404 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1405 return copy_regset_from_user(child, &user_x86_32_view,
1407 sizeof(struct user32_fxsr_struct),
1410 case PTRACE_GET_THREAD_AREA:
1411 case PTRACE_SET_THREAD_AREA:
1412 return arch_ptrace(child, request, addr, data);
1415 return compat_ptrace_request(child, request, addr, data);
1421 #endif /* CONFIG_IA32_EMULATION */
1423 #ifdef CONFIG_X86_64
1425 static const struct user_regset x86_64_regsets[] = {
1426 [REGSET_GENERAL] = {
1427 .core_note_type = NT_PRSTATUS,
1428 .n = sizeof(struct user_regs_struct) / sizeof(long),
1429 .size = sizeof(long), .align = sizeof(long),
1430 .get = genregs_get, .set = genregs_set
1433 .core_note_type = NT_PRFPREG,
1434 .n = sizeof(struct user_i387_struct) / sizeof(long),
1435 .size = sizeof(long), .align = sizeof(long),
1436 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1438 [REGSET_IOPERM64] = {
1439 .core_note_type = NT_386_IOPERM,
1440 .n = IO_BITMAP_LONGS,
1441 .size = sizeof(long), .align = sizeof(long),
1442 .active = ioperm_active, .get = ioperm_get
1446 static const struct user_regset_view user_x86_64_view = {
1447 .name = "x86_64", .e_machine = EM_X86_64,
1448 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1451 #else /* CONFIG_X86_32 */
1453 #define user_regs_struct32 user_regs_struct
1454 #define genregs32_get genregs_get
1455 #define genregs32_set genregs_set
1457 #define user_i387_ia32_struct user_i387_struct
1458 #define user32_fxsr_struct user_fxsr_struct
1460 #endif /* CONFIG_X86_64 */
1462 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1463 static const struct user_regset x86_32_regsets[] = {
1464 [REGSET_GENERAL] = {
1465 .core_note_type = NT_PRSTATUS,
1466 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1467 .size = sizeof(u32), .align = sizeof(u32),
1468 .get = genregs32_get, .set = genregs32_set
1471 .core_note_type = NT_PRFPREG,
1472 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1473 .size = sizeof(u32), .align = sizeof(u32),
1474 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1477 .core_note_type = NT_PRXFPREG,
1478 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1479 .size = sizeof(u32), .align = sizeof(u32),
1480 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1483 .core_note_type = NT_386_TLS,
1484 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1485 .size = sizeof(struct user_desc),
1486 .align = sizeof(struct user_desc),
1487 .active = regset_tls_active,
1488 .get = regset_tls_get, .set = regset_tls_set
1490 [REGSET_IOPERM32] = {
1491 .core_note_type = NT_386_IOPERM,
1492 .n = IO_BITMAP_BYTES / sizeof(u32),
1493 .size = sizeof(u32), .align = sizeof(u32),
1494 .active = ioperm_active, .get = ioperm_get
1498 static const struct user_regset_view user_x86_32_view = {
1499 .name = "i386", .e_machine = EM_386,
1500 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1504 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1506 #ifdef CONFIG_IA32_EMULATION
1507 if (test_tsk_thread_flag(task, TIF_IA32))
1509 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1510 return &user_x86_32_view;
1512 #ifdef CONFIG_X86_64
1513 return &user_x86_64_view;
1517 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1518 int error_code, int si_code)
1520 struct siginfo info;
1522 tsk->thread.trap_no = 1;
1523 tsk->thread.error_code = error_code;
1525 memset(&info, 0, sizeof(info));
1526 info.si_signo = SIGTRAP;
1527 info.si_code = si_code;
1530 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1532 /* Send us the fake SIGTRAP */
1533 force_sig_info(SIGTRAP, &info, tsk);
1537 #ifdef CONFIG_X86_32
1539 #elif defined CONFIG_IA32_EMULATION
1540 # define IS_IA32 test_thread_flag(TIF_IA32)
1546 * We must return the syscall number to actually look up in the table.
1547 * This can be -1L to skip running any syscall at all.
1549 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1554 * If we stepped into a sysenter/syscall insn, it trapped in
1555 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1556 * If user-mode had set TF itself, then it's still clear from
1557 * do_debug() and we need to set it again to restore the user
1558 * state. If we entered on the slow path, TF was already set.
1560 if (test_thread_flag(TIF_SINGLESTEP))
1561 regs->flags |= X86_EFLAGS_TF;
1563 /* do the secure computing check first */
1564 secure_computing(regs->orig_ax);
1566 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1569 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1570 tracehook_report_syscall_entry(regs))
1573 if (unlikely(current->audit_context)) {
1575 audit_syscall_entry(AUDIT_ARCH_I386,
1578 regs->dx, regs->si);
1579 #ifdef CONFIG_X86_64
1581 audit_syscall_entry(AUDIT_ARCH_X86_64,
1584 regs->dx, regs->r10);
1588 return ret ?: regs->orig_ax;
1591 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1593 if (unlikely(current->audit_context))
1594 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1596 if (test_thread_flag(TIF_SYSCALL_TRACE))
1597 tracehook_report_syscall_exit(regs, 0);
1600 * If TIF_SYSCALL_EMU is set, we only get here because of
1601 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1602 * We already reported this syscall instruction in
1603 * syscall_trace_enter(), so don't do any more now.
1605 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1609 * If we are single-stepping, synthesize a trap to follow the
1610 * system call instruction.
1612 if (test_thread_flag(TIF_SINGLESTEP) &&
1613 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1614 send_sigtrap(current, regs, 0, TRAP_BRKPT);