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>
47 * does not yet catch signals sent when the child dies.
48 * in exit.c or in signal.c.
52 * Determines which flags the user has access to [1 = access, 0 = no access].
54 #define FLAG_MASK_32 ((unsigned long) \
55 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
56 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
57 X86_EFLAGS_SF | X86_EFLAGS_TF | \
58 X86_EFLAGS_DF | X86_EFLAGS_OF | \
59 X86_EFLAGS_RF | X86_EFLAGS_AC))
62 * Determines whether a value may be installed in a segment register.
64 static inline bool invalid_selector(u16 value)
66 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
71 #define FLAG_MASK FLAG_MASK_32
73 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
75 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
79 return ®s->bx + regno;
82 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
85 * Returning the value truncates it to 16 bits.
88 if (offset != offsetof(struct user_regs_struct, gs))
89 retval = *pt_regs_access(task_pt_regs(task), offset);
91 retval = task->thread.gs;
93 savesegment(gs, retval);
98 static int set_segment_reg(struct task_struct *task,
99 unsigned long offset, u16 value)
102 * The value argument was already truncated to 16 bits.
104 if (invalid_selector(value))
108 * For %cs and %ss we cannot permit a null selector.
109 * We can permit a bogus selector as long as it has USER_RPL.
110 * Null selectors are fine for other segment registers, but
111 * we will never get back to user mode with invalid %cs or %ss
112 * and will take the trap in iret instead. Much code relies
113 * on user_mode() to distinguish a user trap frame (which can
114 * safely use invalid selectors) from a kernel trap frame.
117 case offsetof(struct user_regs_struct, cs):
118 case offsetof(struct user_regs_struct, ss):
119 if (unlikely(value == 0))
123 *pt_regs_access(task_pt_regs(task), offset) = value;
126 case offsetof(struct user_regs_struct, gs):
127 task->thread.gs = value;
130 * The user-mode %gs is not affected by
131 * kernel entry, so we must update the CPU.
133 loadsegment(gs, value);
139 static unsigned long debugreg_addr_limit(struct task_struct *task)
141 return TASK_SIZE - 3;
144 #else /* CONFIG_X86_64 */
146 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
148 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
150 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
151 return ®s->r15 + (offset / sizeof(regs->r15));
154 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
157 * Returning the value truncates it to 16 bits.
162 case offsetof(struct user_regs_struct, fs):
163 if (task == current) {
164 /* Older gas can't assemble movq %?s,%r?? */
165 asm("movl %%fs,%0" : "=r" (seg));
168 return task->thread.fsindex;
169 case offsetof(struct user_regs_struct, gs):
170 if (task == current) {
171 asm("movl %%gs,%0" : "=r" (seg));
174 return task->thread.gsindex;
175 case offsetof(struct user_regs_struct, ds):
176 if (task == current) {
177 asm("movl %%ds,%0" : "=r" (seg));
180 return task->thread.ds;
181 case offsetof(struct user_regs_struct, es):
182 if (task == current) {
183 asm("movl %%es,%0" : "=r" (seg));
186 return task->thread.es;
188 case offsetof(struct user_regs_struct, cs):
189 case offsetof(struct user_regs_struct, ss):
192 return *pt_regs_access(task_pt_regs(task), offset);
195 static int set_segment_reg(struct task_struct *task,
196 unsigned long offset, u16 value)
199 * The value argument was already truncated to 16 bits.
201 if (invalid_selector(value))
205 case offsetof(struct user_regs_struct,fs):
207 * If this is setting fs as for normal 64-bit use but
208 * setting fs_base has implicitly changed it, leave it.
210 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
211 task->thread.fs != 0) ||
212 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
213 task->thread.fs == 0))
215 task->thread.fsindex = value;
217 loadsegment(fs, task->thread.fsindex);
219 case offsetof(struct user_regs_struct,gs):
221 * If this is setting gs as for normal 64-bit use but
222 * setting gs_base has implicitly changed it, leave it.
224 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
225 task->thread.gs != 0) ||
226 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
227 task->thread.gs == 0))
229 task->thread.gsindex = value;
231 load_gs_index(task->thread.gsindex);
233 case offsetof(struct user_regs_struct,ds):
234 task->thread.ds = value;
236 loadsegment(ds, task->thread.ds);
238 case offsetof(struct user_regs_struct,es):
239 task->thread.es = value;
241 loadsegment(es, task->thread.es);
245 * Can't actually change these in 64-bit mode.
247 case offsetof(struct user_regs_struct,cs):
248 if (unlikely(value == 0))
250 #ifdef CONFIG_IA32_EMULATION
251 if (test_tsk_thread_flag(task, TIF_IA32))
252 task_pt_regs(task)->cs = value;
255 case offsetof(struct user_regs_struct,ss):
256 if (unlikely(value == 0))
258 #ifdef CONFIG_IA32_EMULATION
259 if (test_tsk_thread_flag(task, TIF_IA32))
260 task_pt_regs(task)->ss = value;
268 static unsigned long debugreg_addr_limit(struct task_struct *task)
270 #ifdef CONFIG_IA32_EMULATION
271 if (test_tsk_thread_flag(task, TIF_IA32))
272 return IA32_PAGE_OFFSET - 3;
274 return TASK_SIZE64 - 7;
277 #endif /* CONFIG_X86_32 */
279 static unsigned long get_flags(struct task_struct *task)
281 unsigned long retval = task_pt_regs(task)->flags;
284 * If the debugger set TF, hide it from the readout.
286 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
287 retval &= ~X86_EFLAGS_TF;
292 static int set_flags(struct task_struct *task, unsigned long value)
294 struct pt_regs *regs = task_pt_regs(task);
297 * If the user value contains TF, mark that
298 * it was not "us" (the debugger) that set it.
299 * If not, make sure it stays set if we had.
301 if (value & X86_EFLAGS_TF)
302 clear_tsk_thread_flag(task, TIF_FORCED_TF);
303 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
304 value |= X86_EFLAGS_TF;
306 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
311 static int putreg(struct task_struct *child,
312 unsigned long offset, unsigned long value)
315 case offsetof(struct user_regs_struct, cs):
316 case offsetof(struct user_regs_struct, ds):
317 case offsetof(struct user_regs_struct, es):
318 case offsetof(struct user_regs_struct, fs):
319 case offsetof(struct user_regs_struct, gs):
320 case offsetof(struct user_regs_struct, ss):
321 return set_segment_reg(child, offset, value);
323 case offsetof(struct user_regs_struct, flags):
324 return set_flags(child, value);
328 * Orig_ax is really just a flag with small positive and
329 * negative values, so make sure to always sign-extend it
330 * from 32 bits so that it works correctly regardless of
331 * whether we come from a 32-bit environment or not.
333 case offsetof(struct user_regs_struct, orig_ax):
334 value = (long) (s32) value;
337 case offsetof(struct user_regs_struct,fs_base):
338 if (value >= TASK_SIZE_OF(child))
341 * When changing the segment base, use do_arch_prctl
342 * to set either thread.fs or thread.fsindex and the
343 * corresponding GDT slot.
345 if (child->thread.fs != value)
346 return do_arch_prctl(child, ARCH_SET_FS, value);
348 case offsetof(struct user_regs_struct,gs_base):
350 * Exactly the same here as the %fs handling above.
352 if (value >= TASK_SIZE_OF(child))
354 if (child->thread.gs != value)
355 return do_arch_prctl(child, ARCH_SET_GS, value);
360 *pt_regs_access(task_pt_regs(child), offset) = value;
364 static unsigned long getreg(struct task_struct *task, unsigned long offset)
367 case offsetof(struct user_regs_struct, cs):
368 case offsetof(struct user_regs_struct, ds):
369 case offsetof(struct user_regs_struct, es):
370 case offsetof(struct user_regs_struct, fs):
371 case offsetof(struct user_regs_struct, gs):
372 case offsetof(struct user_regs_struct, ss):
373 return get_segment_reg(task, offset);
375 case offsetof(struct user_regs_struct, flags):
376 return get_flags(task);
379 case offsetof(struct user_regs_struct, fs_base): {
381 * do_arch_prctl may have used a GDT slot instead of
382 * the MSR. To userland, it appears the same either
383 * way, except the %fs segment selector might not be 0.
385 unsigned int seg = task->thread.fsindex;
386 if (task->thread.fs != 0)
387 return task->thread.fs;
389 asm("movl %%fs,%0" : "=r" (seg));
390 if (seg != FS_TLS_SEL)
392 return get_desc_base(&task->thread.tls_array[FS_TLS]);
394 case offsetof(struct user_regs_struct, gs_base): {
396 * Exactly the same here as the %fs handling above.
398 unsigned int seg = task->thread.gsindex;
399 if (task->thread.gs != 0)
400 return task->thread.gs;
402 asm("movl %%gs,%0" : "=r" (seg));
403 if (seg != GS_TLS_SEL)
405 return get_desc_base(&task->thread.tls_array[GS_TLS]);
410 return *pt_regs_access(task_pt_regs(task), offset);
413 static int genregs_get(struct task_struct *target,
414 const struct user_regset *regset,
415 unsigned int pos, unsigned int count,
416 void *kbuf, void __user *ubuf)
419 unsigned long *k = kbuf;
421 *k++ = getreg(target, pos);
426 unsigned long __user *u = ubuf;
428 if (__put_user(getreg(target, pos), u++))
438 static int genregs_set(struct task_struct *target,
439 const struct user_regset *regset,
440 unsigned int pos, unsigned int count,
441 const void *kbuf, const void __user *ubuf)
445 const unsigned long *k = kbuf;
446 while (count > 0 && !ret) {
447 ret = putreg(target, pos, *k++);
452 const unsigned long __user *u = ubuf;
453 while (count > 0 && !ret) {
455 ret = __get_user(word, u++);
458 ret = putreg(target, pos, word);
467 * This function is trivial and will be inlined by the compiler.
468 * Having it separates the implementation details of debug
469 * registers from the interface details of ptrace.
471 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
474 case 0: return child->thread.debugreg0;
475 case 1: return child->thread.debugreg1;
476 case 2: return child->thread.debugreg2;
477 case 3: return child->thread.debugreg3;
478 case 6: return child->thread.debugreg6;
479 case 7: return child->thread.debugreg7;
484 static int ptrace_set_debugreg(struct task_struct *child,
485 int n, unsigned long data)
489 if (unlikely(n == 4 || n == 5))
492 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
496 case 0: child->thread.debugreg0 = data; break;
497 case 1: child->thread.debugreg1 = data; break;
498 case 2: child->thread.debugreg2 = data; break;
499 case 3: child->thread.debugreg3 = data; break;
502 if ((data & ~0xffffffffUL) != 0)
504 child->thread.debugreg6 = data;
509 * Sanity-check data. Take one half-byte at once with
510 * check = (val >> (16 + 4*i)) & 0xf. It contains the
511 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
512 * 2 and 3 are LENi. Given a list of invalid values,
513 * we do mask |= 1 << invalid_value, so that
514 * (mask >> check) & 1 is a correct test for invalid
517 * R/Wi contains the type of the breakpoint /
518 * watchpoint, LENi contains the length of the watched
519 * data in the watchpoint case.
521 * The invalid values are:
522 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
523 * - R/Wi == 0x10 (break on I/O reads or writes), so
525 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
530 * See the Intel Manual "System Programming Guide",
533 * Note that LENi == 0x10 is defined on x86_64 in long
534 * mode (i.e. even for 32-bit userspace software, but
535 * 64-bit kernel), so the x86_64 mask value is 0x5454.
536 * See the AMD manual no. 24593 (AMD64 System Programming)
539 #define DR7_MASK 0x5f54
541 #define DR7_MASK 0x5554
543 data &= ~DR_CONTROL_RESERVED;
544 for (i = 0; i < 4; i++)
545 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
547 child->thread.debugreg7 = data;
549 set_tsk_thread_flag(child, TIF_DEBUG);
551 clear_tsk_thread_flag(child, TIF_DEBUG);
558 #ifdef CONFIG_X86_PTRACE_BTS
560 * The configuration for a particular BTS hardware implementation.
562 struct bts_configuration {
563 /* the size of a BTS record in bytes; at most BTS_MAX_RECORD_SIZE */
564 unsigned char sizeof_bts;
565 /* the size of a field in the BTS record in bytes */
566 unsigned char sizeof_field;
567 /* a bitmask to enable/disable BTS in DEBUGCTL MSR */
568 unsigned long debugctl_mask;
570 static struct bts_configuration bts_cfg;
572 #define BTS_MAX_RECORD_SIZE (8 * 3)
576 * Branch Trace Store (BTS) uses the following format. Different
577 * architectures vary in the size of those fields.
578 * - source linear address
579 * - destination linear address
582 * Later architectures use 64bit pointers throughout, whereas earlier
583 * architectures use 32bit pointers in 32bit mode.
585 * We compute the base address for the first 8 fields based on:
586 * - the field size stored in the DS configuration
587 * - the relative field position
589 * In order to store additional information in the BTS buffer, we use
590 * a special source address to indicate that the record requires
591 * special interpretation.
593 * Netburst indicated via a bit in the flags field whether the branch
594 * was predicted; this is ignored.
602 bts_escape = (unsigned long)-1,
604 bts_jiffies = bts_flags
607 static inline unsigned long bts_get(const char *base, enum bts_field field)
609 base += (bts_cfg.sizeof_field * field);
610 return *(unsigned long *)base;
613 static inline void bts_set(char *base, enum bts_field field, unsigned long val)
615 base += (bts_cfg.sizeof_field * field);;
616 (*(unsigned long *)base) = val;
620 * Translate a BTS record from the raw format into the bts_struct format
622 * out (out): bts_struct interpretation
623 * raw: raw BTS record
625 static void ptrace_bts_translate_record(struct bts_struct *out, const void *raw)
627 memset(out, 0, sizeof(*out));
628 if (bts_get(raw, bts_from) == bts_escape) {
629 out->qualifier = bts_get(raw, bts_qual);
630 out->variant.jiffies = bts_get(raw, bts_jiffies);
632 out->qualifier = BTS_BRANCH;
633 out->variant.lbr.from_ip = bts_get(raw, bts_from);
634 out->variant.lbr.to_ip = bts_get(raw, bts_to);
638 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
639 struct bts_struct __user *out)
641 struct bts_struct ret;
642 const void *bts_record;
643 size_t bts_index, bts_end;
646 error = ds_get_bts_end(child, &bts_end);
650 if (bts_end <= index)
653 error = ds_get_bts_index(child, &bts_index);
657 /* translate the ptrace bts index into the ds bts index */
658 bts_index += bts_end - (index + 1);
659 if (bts_end <= bts_index)
660 bts_index -= bts_end;
662 error = ds_access_bts(child, bts_index, &bts_record);
666 ptrace_bts_translate_record(&ret, bts_record);
668 if (copy_to_user(out, &ret, sizeof(ret)))
674 static int ptrace_bts_drain(struct task_struct *child,
676 struct bts_struct __user *out)
678 struct bts_struct ret;
679 const unsigned char *raw;
683 error = ds_get_bts_index(child, &end);
687 if (size < (end * sizeof(struct bts_struct)))
690 error = ds_access_bts(child, 0, (const void **)&raw);
694 for (i = 0; i < end; i++, out++, raw += bts_cfg.sizeof_bts) {
695 ptrace_bts_translate_record(&ret, raw);
697 if (copy_to_user(out, &ret, sizeof(ret)))
701 error = ds_clear_bts(child);
708 static void ptrace_bts_ovfl(struct task_struct *child)
710 send_sig(child->thread.bts_ovfl_signal, child, 0);
713 static int ptrace_bts_config(struct task_struct *child,
715 const struct ptrace_bts_config __user *ucfg)
717 struct ptrace_bts_config cfg;
721 if (!bts_cfg.sizeof_bts)
725 if (cfg_size < sizeof(cfg))
729 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
733 if ((cfg.flags & PTRACE_BTS_O_SIGNAL) &&
734 !(cfg.flags & PTRACE_BTS_O_ALLOC))
737 if (cfg.flags & PTRACE_BTS_O_ALLOC) {
738 ds_ovfl_callback_t ovfl = NULL;
739 unsigned int sig = 0;
741 /* we ignore the error in case we were not tracing child */
742 (void)ds_release_bts(child);
744 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
749 ovfl = ptrace_bts_ovfl;
752 error = ds_request_bts(child, /* base = */ NULL, cfg.size, ovfl);
756 child->thread.bts_ovfl_signal = sig;
760 if (!child->thread.ds_ctx && cfg.flags)
763 if (cfg.flags & PTRACE_BTS_O_TRACE)
764 child->thread.debugctlmsr |= bts_cfg.debugctl_mask;
766 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
768 if (cfg.flags & PTRACE_BTS_O_SCHED)
769 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
771 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
776 if (child->thread.debugctlmsr)
777 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
779 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
784 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
785 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
789 static int ptrace_bts_status(struct task_struct *child,
791 struct ptrace_bts_config __user *ucfg)
793 struct ptrace_bts_config cfg;
795 const void *base, *max;
798 if (cfg_size < sizeof(cfg))
801 error = ds_get_bts_end(child, &end);
805 error = ds_access_bts(child, /* index = */ 0, &base);
809 error = ds_access_bts(child, /* index = */ end, &max);
813 memset(&cfg, 0, sizeof(cfg));
814 cfg.size = (max - base);
815 cfg.signal = child->thread.bts_ovfl_signal;
816 cfg.bts_size = sizeof(struct bts_struct);
819 cfg.flags |= PTRACE_BTS_O_SIGNAL;
821 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
822 child->thread.debugctlmsr & bts_cfg.debugctl_mask)
823 cfg.flags |= PTRACE_BTS_O_TRACE;
825 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
826 cfg.flags |= PTRACE_BTS_O_SCHED;
828 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
834 static int ptrace_bts_write_record(struct task_struct *child,
835 const struct bts_struct *in)
837 unsigned char bts_record[BTS_MAX_RECORD_SIZE];
839 BUG_ON(BTS_MAX_RECORD_SIZE < bts_cfg.sizeof_bts);
841 memset(bts_record, 0, bts_cfg.sizeof_bts);
842 switch (in->qualifier) {
847 bts_set(bts_record, bts_from, in->variant.lbr.from_ip);
848 bts_set(bts_record, bts_to, in->variant.lbr.to_ip);
851 case BTS_TASK_ARRIVES:
852 case BTS_TASK_DEPARTS:
853 bts_set(bts_record, bts_from, bts_escape);
854 bts_set(bts_record, bts_qual, in->qualifier);
855 bts_set(bts_record, bts_jiffies, in->variant.jiffies);
862 /* The writing task will be the switched-to task on a context
863 * switch. It needs to write into the switched-from task's BTS
865 return ds_unchecked_write_bts(child, bts_record, bts_cfg.sizeof_bts);
868 void ptrace_bts_take_timestamp(struct task_struct *tsk,
869 enum bts_qualifier qualifier)
871 struct bts_struct rec = {
872 .qualifier = qualifier,
873 .variant.jiffies = jiffies_64
876 ptrace_bts_write_record(tsk, &rec);
879 static const struct bts_configuration bts_cfg_netburst = {
880 .sizeof_bts = sizeof(long) * 3,
881 .sizeof_field = sizeof(long),
882 .debugctl_mask = (1<<2)|(1<<3)|(1<<5)
885 static const struct bts_configuration bts_cfg_pentium_m = {
886 .sizeof_bts = sizeof(long) * 3,
887 .sizeof_field = sizeof(long),
888 .debugctl_mask = (1<<6)|(1<<7)
891 static const struct bts_configuration bts_cfg_core2 = {
894 .debugctl_mask = (1<<6)|(1<<7)|(1<<9)
897 static inline void bts_configure(const struct bts_configuration *cfg)
902 void __cpuinit ptrace_bts_init_intel(struct cpuinfo_x86 *c)
906 switch (c->x86_model) {
908 case 0xE: /* Pentium M */
909 bts_configure(&bts_cfg_pentium_m);
911 case 0xF: /* Core2 */
912 case 0x1C: /* Atom */
913 bts_configure(&bts_cfg_core2);
916 /* sorry, don't know about them */
921 switch (c->x86_model) {
924 case 0x2: /* Netburst */
925 bts_configure(&bts_cfg_netburst);
928 /* sorry, don't know about them */
933 /* sorry, don't know about them */
937 #endif /* CONFIG_X86_PTRACE_BTS */
940 * Called by kernel/ptrace.c when detaching..
942 * Make sure the single step bit is not set.
944 void ptrace_disable(struct task_struct *child)
946 user_disable_single_step(child);
947 #ifdef TIF_SYSCALL_EMU
948 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
950 #ifdef CONFIG_X86_PTRACE_BTS
951 (void)ds_release_bts(child);
953 child->thread.debugctlmsr &= ~bts_cfg.debugctl_mask;
954 if (!child->thread.debugctlmsr)
955 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
957 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
958 #endif /* CONFIG_X86_PTRACE_BTS */
961 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
962 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
965 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
968 unsigned long __user *datap = (unsigned long __user *)data;
971 /* read the word at location addr in the USER area. */
972 case PTRACE_PEEKUSR: {
976 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
977 addr >= sizeof(struct user))
980 tmp = 0; /* Default return condition */
981 if (addr < sizeof(struct user_regs_struct))
982 tmp = getreg(child, addr);
983 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
984 addr <= offsetof(struct user, u_debugreg[7])) {
985 addr -= offsetof(struct user, u_debugreg[0]);
986 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
988 ret = put_user(tmp, datap);
992 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
994 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
995 addr >= sizeof(struct user))
998 if (addr < sizeof(struct user_regs_struct))
999 ret = putreg(child, addr, data);
1000 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1001 addr <= offsetof(struct user, u_debugreg[7])) {
1002 addr -= offsetof(struct user, u_debugreg[0]);
1003 ret = ptrace_set_debugreg(child,
1004 addr / sizeof(data), data);
1008 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1009 return copy_regset_to_user(child,
1010 task_user_regset_view(current),
1012 0, sizeof(struct user_regs_struct),
1015 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1016 return copy_regset_from_user(child,
1017 task_user_regset_view(current),
1019 0, sizeof(struct user_regs_struct),
1022 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1023 return copy_regset_to_user(child,
1024 task_user_regset_view(current),
1026 0, sizeof(struct user_i387_struct),
1029 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1030 return copy_regset_from_user(child,
1031 task_user_regset_view(current),
1033 0, sizeof(struct user_i387_struct),
1036 #ifdef CONFIG_X86_32
1037 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1038 return copy_regset_to_user(child, &user_x86_32_view,
1040 0, sizeof(struct user_fxsr_struct),
1043 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1044 return copy_regset_from_user(child, &user_x86_32_view,
1046 0, sizeof(struct user_fxsr_struct),
1050 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1051 case PTRACE_GET_THREAD_AREA:
1054 ret = do_get_thread_area(child, addr,
1055 (struct user_desc __user *) data);
1058 case PTRACE_SET_THREAD_AREA:
1061 ret = do_set_thread_area(child, addr,
1062 (struct user_desc __user *) data, 0);
1066 #ifdef CONFIG_X86_64
1067 /* normal 64bit interface to access TLS data.
1068 Works just like arch_prctl, except that the arguments
1070 case PTRACE_ARCH_PRCTL:
1071 ret = do_arch_prctl(child, data, addr);
1076 * These bits need more cooking - not enabled yet:
1078 #ifdef CONFIG_X86_PTRACE_BTS
1079 case PTRACE_BTS_CONFIG:
1080 ret = ptrace_bts_config
1081 (child, data, (struct ptrace_bts_config __user *)addr);
1084 case PTRACE_BTS_STATUS:
1085 ret = ptrace_bts_status
1086 (child, data, (struct ptrace_bts_config __user *)addr);
1089 case PTRACE_BTS_SIZE:
1090 ret = ds_get_bts_index(child, /* pos = */ NULL);
1093 case PTRACE_BTS_GET:
1094 ret = ptrace_bts_read_record
1095 (child, data, (struct bts_struct __user *) addr);
1098 case PTRACE_BTS_CLEAR:
1099 ret = ds_clear_bts(child);
1102 case PTRACE_BTS_DRAIN:
1103 ret = ptrace_bts_drain
1104 (child, data, (struct bts_struct __user *) addr);
1106 #endif /* CONFIG_X86_PTRACE_BTS */
1109 ret = ptrace_request(child, request, addr, data);
1116 #ifdef CONFIG_IA32_EMULATION
1118 #include <linux/compat.h>
1119 #include <linux/syscalls.h>
1120 #include <asm/ia32.h>
1121 #include <asm/user32.h>
1124 case offsetof(struct user32, regs.l): \
1125 regs->q = value; break
1128 case offsetof(struct user32, regs.rs): \
1129 return set_segment_reg(child, \
1130 offsetof(struct user_regs_struct, rs), \
1134 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1136 struct pt_regs *regs = task_pt_regs(child);
1157 case offsetof(struct user32, regs.orig_eax):
1159 * Sign-extend the value so that orig_eax = -1
1160 * causes (long)orig_ax < 0 tests to fire correctly.
1162 regs->orig_ax = (long) (s32) value;
1165 case offsetof(struct user32, regs.eflags):
1166 return set_flags(child, value);
1168 case offsetof(struct user32, u_debugreg[0]) ...
1169 offsetof(struct user32, u_debugreg[7]):
1170 regno -= offsetof(struct user32, u_debugreg[0]);
1171 return ptrace_set_debugreg(child, regno / 4, value);
1174 if (regno > sizeof(struct user32) || (regno & 3))
1178 * Other dummy fields in the virtual user structure
1190 case offsetof(struct user32, regs.l): \
1191 *val = regs->q; break
1194 case offsetof(struct user32, regs.rs): \
1195 *val = get_segment_reg(child, \
1196 offsetof(struct user_regs_struct, rs)); \
1199 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1201 struct pt_regs *regs = task_pt_regs(child);
1219 R32(orig_eax, orig_ax);
1223 case offsetof(struct user32, regs.eflags):
1224 *val = get_flags(child);
1227 case offsetof(struct user32, u_debugreg[0]) ...
1228 offsetof(struct user32, u_debugreg[7]):
1229 regno -= offsetof(struct user32, u_debugreg[0]);
1230 *val = ptrace_get_debugreg(child, regno / 4);
1234 if (regno > sizeof(struct user32) || (regno & 3))
1238 * Other dummy fields in the virtual user structure
1250 static int genregs32_get(struct task_struct *target,
1251 const struct user_regset *regset,
1252 unsigned int pos, unsigned int count,
1253 void *kbuf, void __user *ubuf)
1256 compat_ulong_t *k = kbuf;
1258 getreg32(target, pos, k++);
1259 count -= sizeof(*k);
1263 compat_ulong_t __user *u = ubuf;
1265 compat_ulong_t word;
1266 getreg32(target, pos, &word);
1267 if (__put_user(word, u++))
1269 count -= sizeof(*u);
1277 static int genregs32_set(struct task_struct *target,
1278 const struct user_regset *regset,
1279 unsigned int pos, unsigned int count,
1280 const void *kbuf, const void __user *ubuf)
1284 const compat_ulong_t *k = kbuf;
1285 while (count > 0 && !ret) {
1286 ret = putreg32(target, pos, *k++);
1287 count -= sizeof(*k);
1291 const compat_ulong_t __user *u = ubuf;
1292 while (count > 0 && !ret) {
1293 compat_ulong_t word;
1294 ret = __get_user(word, u++);
1297 ret = putreg32(target, pos, word);
1298 count -= sizeof(*u);
1305 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1306 compat_ulong_t caddr, compat_ulong_t cdata)
1308 unsigned long addr = caddr;
1309 unsigned long data = cdata;
1310 void __user *datap = compat_ptr(data);
1315 case PTRACE_PEEKUSR:
1316 ret = getreg32(child, addr, &val);
1318 ret = put_user(val, (__u32 __user *)datap);
1321 case PTRACE_POKEUSR:
1322 ret = putreg32(child, addr, data);
1325 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1326 return copy_regset_to_user(child, &user_x86_32_view,
1328 0, sizeof(struct user_regs_struct32),
1331 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1332 return copy_regset_from_user(child, &user_x86_32_view,
1334 sizeof(struct user_regs_struct32),
1337 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1338 return copy_regset_to_user(child, &user_x86_32_view,
1340 sizeof(struct user_i387_ia32_struct),
1343 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1344 return copy_regset_from_user(
1345 child, &user_x86_32_view, REGSET_FP,
1346 0, sizeof(struct user_i387_ia32_struct), datap);
1348 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1349 return copy_regset_to_user(child, &user_x86_32_view,
1351 sizeof(struct user32_fxsr_struct),
1354 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1355 return copy_regset_from_user(child, &user_x86_32_view,
1357 sizeof(struct user32_fxsr_struct),
1360 case PTRACE_GET_THREAD_AREA:
1361 case PTRACE_SET_THREAD_AREA:
1362 return arch_ptrace(child, request, addr, data);
1365 return compat_ptrace_request(child, request, addr, data);
1371 #endif /* CONFIG_IA32_EMULATION */
1373 #ifdef CONFIG_X86_64
1375 static const struct user_regset x86_64_regsets[] = {
1376 [REGSET_GENERAL] = {
1377 .core_note_type = NT_PRSTATUS,
1378 .n = sizeof(struct user_regs_struct) / sizeof(long),
1379 .size = sizeof(long), .align = sizeof(long),
1380 .get = genregs_get, .set = genregs_set
1383 .core_note_type = NT_PRFPREG,
1384 .n = sizeof(struct user_i387_struct) / sizeof(long),
1385 .size = sizeof(long), .align = sizeof(long),
1386 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1390 static const struct user_regset_view user_x86_64_view = {
1391 .name = "x86_64", .e_machine = EM_X86_64,
1392 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1395 #else /* CONFIG_X86_32 */
1397 #define user_regs_struct32 user_regs_struct
1398 #define genregs32_get genregs_get
1399 #define genregs32_set genregs_set
1401 #define user_i387_ia32_struct user_i387_struct
1402 #define user32_fxsr_struct user_fxsr_struct
1404 #endif /* CONFIG_X86_64 */
1406 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1407 static const struct user_regset x86_32_regsets[] = {
1408 [REGSET_GENERAL] = {
1409 .core_note_type = NT_PRSTATUS,
1410 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1411 .size = sizeof(u32), .align = sizeof(u32),
1412 .get = genregs32_get, .set = genregs32_set
1415 .core_note_type = NT_PRFPREG,
1416 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1417 .size = sizeof(u32), .align = sizeof(u32),
1418 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1421 .core_note_type = NT_PRXFPREG,
1422 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1423 .size = sizeof(u32), .align = sizeof(u32),
1424 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1427 .core_note_type = NT_386_TLS,
1428 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1429 .size = sizeof(struct user_desc),
1430 .align = sizeof(struct user_desc),
1431 .active = regset_tls_active,
1432 .get = regset_tls_get, .set = regset_tls_set
1436 static const struct user_regset_view user_x86_32_view = {
1437 .name = "i386", .e_machine = EM_386,
1438 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1442 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1444 #ifdef CONFIG_IA32_EMULATION
1445 if (test_tsk_thread_flag(task, TIF_IA32))
1447 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1448 return &user_x86_32_view;
1450 #ifdef CONFIG_X86_64
1451 return &user_x86_64_view;
1455 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1456 int error_code, int si_code)
1458 struct siginfo info;
1460 tsk->thread.trap_no = 1;
1461 tsk->thread.error_code = error_code;
1463 memset(&info, 0, sizeof(info));
1464 info.si_signo = SIGTRAP;
1465 info.si_code = si_code;
1468 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1470 /* Send us the fake SIGTRAP */
1471 force_sig_info(SIGTRAP, &info, tsk);
1475 #ifdef CONFIG_X86_32
1477 #elif defined CONFIG_IA32_EMULATION
1478 # define IS_IA32 test_thread_flag(TIF_IA32)
1484 * We must return the syscall number to actually look up in the table.
1485 * This can be -1L to skip running any syscall at all.
1487 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1492 * If we stepped into a sysenter/syscall insn, it trapped in
1493 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1494 * If user-mode had set TF itself, then it's still clear from
1495 * do_debug() and we need to set it again to restore the user
1496 * state. If we entered on the slow path, TF was already set.
1498 if (test_thread_flag(TIF_SINGLESTEP))
1499 regs->flags |= X86_EFLAGS_TF;
1501 /* do the secure computing check first */
1502 secure_computing(regs->orig_ax);
1504 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1507 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1508 tracehook_report_syscall_entry(regs))
1511 if (unlikely(current->audit_context)) {
1513 audit_syscall_entry(AUDIT_ARCH_I386,
1516 regs->dx, regs->si);
1517 #ifdef CONFIG_X86_64
1519 audit_syscall_entry(AUDIT_ARCH_X86_64,
1522 regs->dx, regs->r10);
1526 return ret ?: regs->orig_ax;
1529 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1531 if (unlikely(current->audit_context))
1532 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1534 if (test_thread_flag(TIF_SYSCALL_TRACE))
1535 tracehook_report_syscall_exit(regs, 0);
1538 * If TIF_SYSCALL_EMU is set, we only get here because of
1539 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1540 * We already reported this syscall instruction in
1541 * syscall_trace_enter(), so don't do any more now.
1543 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1547 * If we are single-stepping, synthesize a trap to follow the
1548 * system call instruction.
1550 if (test_thread_flag(TIF_SINGLESTEP) &&
1551 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1552 send_sigtrap(current, regs, 0, TRAP_BRKPT);