Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / x86 / kernel / ptrace.c
blob24c74ca3811260bbf4caeb256f1dfa6254e3becb
1 /* By Ross Biro 1/23/92 */
2 /*
3 * Pentium III FXSR, SSE support
4 * Gareth Hughes <gareth@valinux.com>, May 2000
6 * BTS tracing
7 * Markus Metzger <markus.t.metzger@intel.com>, Dec 2007
8 */
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/regset.h>
17 #include <linux/user.h>
18 #include <linux/elf.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/seccomp.h>
22 #include <linux/signal.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
28 #include <asm/i387.h>
29 #include <asm/debugreg.h>
30 #include <asm/ldt.h>
31 #include <asm/desc.h>
32 #include <asm/prctl.h>
33 #include <asm/proto.h>
34 #include <asm/ds.h>
36 #include "tls.h"
38 enum x86_regset {
39 REGSET_GENERAL,
40 REGSET_FP,
41 REGSET_XFP,
42 REGSET_TLS,
46 * does not yet catch signals sent when the child dies.
47 * in exit.c or in signal.c.
51 * Determines which flags the user has access to [1 = access, 0 = no access].
53 #define FLAG_MASK_32 ((unsigned long) \
54 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
55 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
56 X86_EFLAGS_SF | X86_EFLAGS_TF | \
57 X86_EFLAGS_DF | X86_EFLAGS_OF | \
58 X86_EFLAGS_RF | X86_EFLAGS_AC))
61 * Determines whether a value may be installed in a segment register.
63 static inline bool invalid_selector(u16 value)
65 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
68 #ifdef CONFIG_X86_32
70 #define FLAG_MASK FLAG_MASK_32
72 static long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
74 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
75 regno >>= 2;
76 if (regno > FS)
77 --regno;
78 return &regs->bx + regno;
81 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
84 * Returning the value truncates it to 16 bits.
86 unsigned int retval;
87 if (offset != offsetof(struct user_regs_struct, gs))
88 retval = *pt_regs_access(task_pt_regs(task), offset);
89 else {
90 retval = task->thread.gs;
91 if (task == current)
92 savesegment(gs, retval);
94 return retval;
97 static int set_segment_reg(struct task_struct *task,
98 unsigned long offset, u16 value)
101 * The value argument was already truncated to 16 bits.
103 if (invalid_selector(value))
104 return -EIO;
107 * For %cs and %ss we cannot permit a null selector.
108 * We can permit a bogus selector as long as it has USER_RPL.
109 * Null selectors are fine for other segment registers, but
110 * we will never get back to user mode with invalid %cs or %ss
111 * and will take the trap in iret instead. Much code relies
112 * on user_mode() to distinguish a user trap frame (which can
113 * safely use invalid selectors) from a kernel trap frame.
115 switch (offset) {
116 case offsetof(struct user_regs_struct, cs):
117 case offsetof(struct user_regs_struct, ss):
118 if (unlikely(value == 0))
119 return -EIO;
121 default:
122 *pt_regs_access(task_pt_regs(task), offset) = value;
123 break;
125 case offsetof(struct user_regs_struct, gs):
126 task->thread.gs = value;
127 if (task == current)
129 * The user-mode %gs is not affected by
130 * kernel entry, so we must update the CPU.
132 loadsegment(gs, value);
135 return 0;
138 static unsigned long debugreg_addr_limit(struct task_struct *task)
140 return TASK_SIZE - 3;
143 #else /* CONFIG_X86_64 */
145 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
147 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
149 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
150 return &regs->r15 + (offset / sizeof(regs->r15));
153 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
156 * Returning the value truncates it to 16 bits.
158 unsigned int seg;
160 switch (offset) {
161 case offsetof(struct user_regs_struct, fs):
162 if (task == current) {
163 /* Older gas can't assemble movq %?s,%r?? */
164 asm("movl %%fs,%0" : "=r" (seg));
165 return seg;
167 return task->thread.fsindex;
168 case offsetof(struct user_regs_struct, gs):
169 if (task == current) {
170 asm("movl %%gs,%0" : "=r" (seg));
171 return seg;
173 return task->thread.gsindex;
174 case offsetof(struct user_regs_struct, ds):
175 if (task == current) {
176 asm("movl %%ds,%0" : "=r" (seg));
177 return seg;
179 return task->thread.ds;
180 case offsetof(struct user_regs_struct, es):
181 if (task == current) {
182 asm("movl %%es,%0" : "=r" (seg));
183 return seg;
185 return task->thread.es;
187 case offsetof(struct user_regs_struct, cs):
188 case offsetof(struct user_regs_struct, ss):
189 break;
191 return *pt_regs_access(task_pt_regs(task), offset);
194 static int set_segment_reg(struct task_struct *task,
195 unsigned long offset, u16 value)
198 * The value argument was already truncated to 16 bits.
200 if (invalid_selector(value))
201 return -EIO;
203 switch (offset) {
204 case offsetof(struct user_regs_struct,fs):
206 * If this is setting fs as for normal 64-bit use but
207 * setting fs_base has implicitly changed it, leave it.
209 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
210 task->thread.fs != 0) ||
211 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
212 task->thread.fs == 0))
213 break;
214 task->thread.fsindex = value;
215 if (task == current)
216 loadsegment(fs, task->thread.fsindex);
217 break;
218 case offsetof(struct user_regs_struct,gs):
220 * If this is setting gs as for normal 64-bit use but
221 * setting gs_base has implicitly changed it, leave it.
223 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
224 task->thread.gs != 0) ||
225 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
226 task->thread.gs == 0))
227 break;
228 task->thread.gsindex = value;
229 if (task == current)
230 load_gs_index(task->thread.gsindex);
231 break;
232 case offsetof(struct user_regs_struct,ds):
233 task->thread.ds = value;
234 if (task == current)
235 loadsegment(ds, task->thread.ds);
236 break;
237 case offsetof(struct user_regs_struct,es):
238 task->thread.es = value;
239 if (task == current)
240 loadsegment(es, task->thread.es);
241 break;
244 * Can't actually change these in 64-bit mode.
246 case offsetof(struct user_regs_struct,cs):
247 if (unlikely(value == 0))
248 return -EIO;
249 #ifdef CONFIG_IA32_EMULATION
250 if (test_tsk_thread_flag(task, TIF_IA32))
251 task_pt_regs(task)->cs = value;
252 #endif
253 break;
254 case offsetof(struct user_regs_struct,ss):
255 if (unlikely(value == 0))
256 return -EIO;
257 #ifdef CONFIG_IA32_EMULATION
258 if (test_tsk_thread_flag(task, TIF_IA32))
259 task_pt_regs(task)->ss = value;
260 #endif
261 break;
264 return 0;
267 static unsigned long debugreg_addr_limit(struct task_struct *task)
269 #ifdef CONFIG_IA32_EMULATION
270 if (test_tsk_thread_flag(task, TIF_IA32))
271 return IA32_PAGE_OFFSET - 3;
272 #endif
273 return TASK_SIZE64 - 7;
276 #endif /* CONFIG_X86_32 */
278 static unsigned long get_flags(struct task_struct *task)
280 unsigned long retval = task_pt_regs(task)->flags;
283 * If the debugger set TF, hide it from the readout.
285 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
286 retval &= ~X86_EFLAGS_TF;
288 return retval;
291 static int set_flags(struct task_struct *task, unsigned long value)
293 struct pt_regs *regs = task_pt_regs(task);
296 * If the user value contains TF, mark that
297 * it was not "us" (the debugger) that set it.
298 * If not, make sure it stays set if we had.
300 if (value & X86_EFLAGS_TF)
301 clear_tsk_thread_flag(task, TIF_FORCED_TF);
302 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
303 value |= X86_EFLAGS_TF;
305 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
307 return 0;
310 static int putreg(struct task_struct *child,
311 unsigned long offset, unsigned long value)
313 switch (offset) {
314 case offsetof(struct user_regs_struct, cs):
315 case offsetof(struct user_regs_struct, ds):
316 case offsetof(struct user_regs_struct, es):
317 case offsetof(struct user_regs_struct, fs):
318 case offsetof(struct user_regs_struct, gs):
319 case offsetof(struct user_regs_struct, ss):
320 return set_segment_reg(child, offset, value);
322 case offsetof(struct user_regs_struct, flags):
323 return set_flags(child, value);
325 #ifdef CONFIG_X86_64
326 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
327 =======
329 * Orig_ax is really just a flag with small positive and
330 * negative values, so make sure to always sign-extend it
331 * from 32 bits so that it works correctly regardless of
332 * whether we come from a 32-bit environment or not.
334 case offsetof(struct user_regs_struct, orig_ax):
335 value = (long) (s32) value;
336 break;
338 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
339 case offsetof(struct user_regs_struct,fs_base):
340 if (value >= TASK_SIZE_OF(child))
341 return -EIO;
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);
349 return 0;
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))
355 return -EIO;
356 if (child->thread.gs != value)
357 return do_arch_prctl(child, ARCH_SET_GS, value);
358 return 0;
359 #endif
362 *pt_regs_access(task_pt_regs(child), offset) = value;
363 return 0;
366 static unsigned long getreg(struct task_struct *task, unsigned long offset)
368 switch (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);
380 #ifdef CONFIG_X86_64
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;
390 if (task == current)
391 asm("movl %%fs,%0" : "=r" (seg));
392 if (seg != FS_TLS_SEL)
393 return 0;
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;
403 if (task == current)
404 asm("movl %%gs,%0" : "=r" (seg));
405 if (seg != GS_TLS_SEL)
406 return 0;
407 return get_desc_base(&task->thread.tls_array[GS_TLS]);
409 #endif
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)
420 if (kbuf) {
421 unsigned long *k = kbuf;
422 while (count > 0) {
423 *k++ = getreg(target, pos);
424 count -= sizeof(*k);
425 pos += sizeof(*k);
427 } else {
428 unsigned long __user *u = ubuf;
429 while (count > 0) {
430 if (__put_user(getreg(target, pos), u++))
431 return -EFAULT;
432 count -= sizeof(*u);
433 pos += sizeof(*u);
437 return 0;
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)
445 int ret = 0;
446 if (kbuf) {
447 const unsigned long *k = kbuf;
448 while (count > 0 && !ret) {
449 ret = putreg(target, pos, *k++);
450 count -= sizeof(*k);
451 pos += sizeof(*k);
453 } else {
454 const unsigned long __user *u = ubuf;
455 while (count > 0 && !ret) {
456 unsigned long word;
457 ret = __get_user(word, u++);
458 if (ret)
459 break;
460 ret = putreg(target, pos, word);
461 count -= sizeof(*u);
462 pos += sizeof(*u);
465 return ret;
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)
475 switch (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;
483 return 0;
486 static int ptrace_set_debugreg(struct task_struct *child,
487 int n, unsigned long data)
489 int i;
491 if (unlikely(n == 4 || n == 5))
492 return -EIO;
494 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
495 return -EIO;
497 switch (n) {
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;
503 case 6:
504 if ((data & ~0xffffffffUL) != 0)
505 return -EIO;
506 child->thread.debugreg6 = data;
507 break;
509 case 7:
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
517 * values.
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
526 * mask |= 0x4444.
527 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
528 * 0x1110.
530 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
532 * See the Intel Manual "System Programming Guide",
533 * 15.2.4
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)
540 #ifdef CONFIG_X86_32
541 #define DR7_MASK 0x5f54
542 #else
543 #define DR7_MASK 0x5554
544 #endif
545 data &= ~DR_CONTROL_RESERVED;
546 for (i = 0; i < 4; i++)
547 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
548 return -EIO;
549 child->thread.debugreg7 = data;
550 if (data)
551 set_tsk_thread_flag(child, TIF_DEBUG);
552 else
553 clear_tsk_thread_flag(child, TIF_DEBUG);
554 break;
557 return 0;
560 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
561 =======
562 #ifdef X86_BTS
564 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
565 static int ptrace_bts_get_size(struct task_struct *child)
567 if (!child->thread.ds_area_msr)
568 return -ENXIO;
570 return ds_get_bts_index((void *)child->thread.ds_area_msr);
573 static int ptrace_bts_read_record(struct task_struct *child,
574 long index,
575 struct bts_struct __user *out)
577 struct bts_struct ret;
578 int retval;
579 int bts_end;
580 int bts_index;
582 if (!child->thread.ds_area_msr)
583 return -ENXIO;
585 if (index < 0)
586 return -EINVAL;
588 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
589 if (bts_end <= index)
590 return -EINVAL;
592 /* translate the ptrace bts index into the ds bts index */
593 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
594 bts_index -= (index + 1);
595 if (bts_index < 0)
596 bts_index += bts_end;
598 retval = ds_read_bts((void *)child->thread.ds_area_msr,
599 bts_index, &ret);
600 if (retval < 0)
601 return retval;
603 if (copy_to_user(out, &ret, sizeof(ret)))
604 return -EFAULT;
606 return sizeof(ret);
609 static int ptrace_bts_write_record(struct task_struct *child,
610 const struct bts_struct *in)
612 int retval;
614 if (!child->thread.ds_area_msr)
615 return -ENXIO;
617 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
618 if (retval)
619 return retval;
621 return sizeof(*in);
624 static int ptrace_bts_clear(struct task_struct *child)
626 if (!child->thread.ds_area_msr)
627 return -ENXIO;
629 return ds_clear((void *)child->thread.ds_area_msr);
632 static int ptrace_bts_drain(struct task_struct *child,
633 long size,
634 struct bts_struct __user *out)
636 int end, i;
637 void *ds = (void *)child->thread.ds_area_msr;
639 if (!ds)
640 return -ENXIO;
642 end = ds_get_bts_index(ds);
643 if (end <= 0)
644 return end;
646 if (size < (end * sizeof(struct bts_struct)))
647 return -EIO;
649 for (i = 0; i < end; i++, out++) {
650 struct bts_struct ret;
651 int retval;
653 retval = ds_read_bts(ds, i, &ret);
654 if (retval < 0)
655 return retval;
657 if (copy_to_user(out, &ret, sizeof(ret)))
658 return -EFAULT;
661 ds_clear(ds);
663 return end;
666 static int ptrace_bts_realloc(struct task_struct *child,
667 int size, int reduce_size)
669 unsigned long rlim, vm;
670 int ret, old_size;
672 if (size < 0)
673 return -EINVAL;
675 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
676 if (old_size < 0)
677 return old_size;
679 ret = ds_free((void **)&child->thread.ds_area_msr);
680 if (ret < 0)
681 goto out;
683 size >>= PAGE_SHIFT;
684 old_size >>= PAGE_SHIFT;
686 current->mm->total_vm -= old_size;
687 current->mm->locked_vm -= old_size;
689 if (size == 0)
690 goto out;
692 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
693 vm = current->mm->total_vm + size;
694 if (rlim < vm) {
695 ret = -ENOMEM;
697 if (!reduce_size)
698 goto out;
700 size = rlim - current->mm->total_vm;
701 if (size <= 0)
702 goto out;
705 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
706 vm = current->mm->locked_vm + size;
707 if (rlim < vm) {
708 ret = -ENOMEM;
710 if (!reduce_size)
711 goto out;
713 size = rlim - current->mm->locked_vm;
714 if (size <= 0)
715 goto out;
718 ret = ds_allocate((void **)&child->thread.ds_area_msr,
719 size << PAGE_SHIFT);
720 if (ret < 0)
721 goto out;
723 current->mm->total_vm += size;
724 current->mm->locked_vm += size;
726 out:
727 if (child->thread.ds_area_msr)
728 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
729 else
730 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
732 return ret;
735 static int ptrace_bts_config(struct task_struct *child,
736 long cfg_size,
737 const struct ptrace_bts_config __user *ucfg)
739 struct ptrace_bts_config cfg;
740 int bts_size, ret = 0;
741 void *ds;
743 if (cfg_size < sizeof(cfg))
744 return -EIO;
746 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
747 return -EFAULT;
749 if ((int)cfg.size < 0)
750 return -EINVAL;
752 bts_size = 0;
753 ds = (void *)child->thread.ds_area_msr;
754 if (ds) {
755 bts_size = ds_get_bts_size(ds);
756 if (bts_size < 0)
757 return bts_size;
759 cfg.size = PAGE_ALIGN(cfg.size);
761 if (bts_size != cfg.size) {
762 ret = ptrace_bts_realloc(child, cfg.size,
763 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
764 if (ret < 0)
765 goto errout;
767 ds = (void *)child->thread.ds_area_msr;
770 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
771 ret = ds_set_overflow(ds, DS_O_SIGNAL);
772 else
773 ret = ds_set_overflow(ds, DS_O_WRAP);
774 if (ret < 0)
775 goto errout;
777 if (cfg.flags & PTRACE_BTS_O_TRACE)
778 child->thread.debugctlmsr |= ds_debugctl_mask();
779 else
780 child->thread.debugctlmsr &= ~ds_debugctl_mask();
782 if (cfg.flags & PTRACE_BTS_O_SCHED)
783 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
784 else
785 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
787 ret = sizeof(cfg);
789 out:
790 if (child->thread.debugctlmsr)
791 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
792 else
793 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
795 return ret;
797 errout:
798 child->thread.debugctlmsr &= ~ds_debugctl_mask();
799 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
800 goto out;
803 static int ptrace_bts_status(struct task_struct *child,
804 long cfg_size,
805 struct ptrace_bts_config __user *ucfg)
807 void *ds = (void *)child->thread.ds_area_msr;
808 struct ptrace_bts_config cfg;
810 if (cfg_size < sizeof(cfg))
811 return -EIO;
813 memset(&cfg, 0, sizeof(cfg));
815 if (ds) {
816 cfg.size = ds_get_bts_size(ds);
818 if (ds_get_overflow(ds) == DS_O_SIGNAL)
819 cfg.flags |= PTRACE_BTS_O_SIGNAL;
821 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
822 child->thread.debugctlmsr & ds_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;
829 cfg.bts_size = sizeof(struct bts_struct);
831 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
832 return -EFAULT;
834 return sizeof(cfg);
837 void ptrace_bts_take_timestamp(struct task_struct *tsk,
838 enum bts_qualifier qualifier)
840 struct bts_struct rec = {
841 .qualifier = qualifier,
842 .variant.jiffies = jiffies_64
845 ptrace_bts_write_record(tsk, &rec);
847 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
848 =======
849 #endif /* X86_BTS */
850 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
853 * Called by kernel/ptrace.c when detaching..
855 * Make sure the single step bit is not set.
857 void ptrace_disable(struct task_struct *child)
859 user_disable_single_step(child);
860 #ifdef TIF_SYSCALL_EMU
861 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
862 #endif
863 if (child->thread.ds_area_msr) {
864 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
865 =======
866 #ifdef X86_BTS
867 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
868 ptrace_bts_realloc(child, 0, 0);
869 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
870 =======
871 #endif
872 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
873 child->thread.debugctlmsr &= ~ds_debugctl_mask();
874 if (!child->thread.debugctlmsr)
875 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
876 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
880 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
881 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
882 #endif
884 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
886 int ret;
887 unsigned long __user *datap = (unsigned long __user *)data;
889 switch (request) {
890 /* read the word at location addr in the USER area. */
891 case PTRACE_PEEKUSR: {
892 unsigned long tmp;
894 ret = -EIO;
895 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
896 addr >= sizeof(struct user))
897 break;
899 tmp = 0; /* Default return condition */
900 if (addr < sizeof(struct user_regs_struct))
901 tmp = getreg(child, addr);
902 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
903 addr <= offsetof(struct user, u_debugreg[7])) {
904 addr -= offsetof(struct user, u_debugreg[0]);
905 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
907 ret = put_user(tmp, datap);
908 break;
911 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
912 ret = -EIO;
913 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
914 addr >= sizeof(struct user))
915 break;
917 if (addr < sizeof(struct user_regs_struct))
918 ret = putreg(child, addr, data);
919 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
920 addr <= offsetof(struct user, u_debugreg[7])) {
921 addr -= offsetof(struct user, u_debugreg[0]);
922 ret = ptrace_set_debugreg(child,
923 addr / sizeof(data), data);
925 break;
927 case PTRACE_GETREGS: /* Get all gp regs from the child. */
928 return copy_regset_to_user(child,
929 task_user_regset_view(current),
930 REGSET_GENERAL,
931 0, sizeof(struct user_regs_struct),
932 datap);
934 case PTRACE_SETREGS: /* Set all gp regs in the child. */
935 return copy_regset_from_user(child,
936 task_user_regset_view(current),
937 REGSET_GENERAL,
938 0, sizeof(struct user_regs_struct),
939 datap);
941 case PTRACE_GETFPREGS: /* Get the child FPU state. */
942 return copy_regset_to_user(child,
943 task_user_regset_view(current),
944 REGSET_FP,
945 0, sizeof(struct user_i387_struct),
946 datap);
948 case PTRACE_SETFPREGS: /* Set the child FPU state. */
949 return copy_regset_from_user(child,
950 task_user_regset_view(current),
951 REGSET_FP,
952 0, sizeof(struct user_i387_struct),
953 datap);
955 #ifdef CONFIG_X86_32
956 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
957 return copy_regset_to_user(child, &user_x86_32_view,
958 REGSET_XFP,
959 0, sizeof(struct user_fxsr_struct),
960 datap);
962 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
963 return copy_regset_from_user(child, &user_x86_32_view,
964 REGSET_XFP,
965 0, sizeof(struct user_fxsr_struct),
966 datap);
967 #endif
969 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
970 case PTRACE_GET_THREAD_AREA:
971 if (addr < 0)
972 return -EIO;
973 ret = do_get_thread_area(child, addr,
974 (struct user_desc __user *) data);
975 break;
977 case PTRACE_SET_THREAD_AREA:
978 if (addr < 0)
979 return -EIO;
980 ret = do_set_thread_area(child, addr,
981 (struct user_desc __user *) data, 0);
982 break;
983 #endif
985 #ifdef CONFIG_X86_64
986 /* normal 64bit interface to access TLS data.
987 Works just like arch_prctl, except that the arguments
988 are reversed. */
989 case PTRACE_ARCH_PRCTL:
990 ret = do_arch_prctl(child, data, addr);
991 break;
992 #endif
994 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
995 =======
997 * These bits need more cooking - not enabled yet:
999 #ifdef X86_BTS
1000 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1001 case PTRACE_BTS_CONFIG:
1002 ret = ptrace_bts_config
1003 (child, data, (struct ptrace_bts_config __user *)addr);
1004 break;
1006 case PTRACE_BTS_STATUS:
1007 ret = ptrace_bts_status
1008 (child, data, (struct ptrace_bts_config __user *)addr);
1009 break;
1011 case PTRACE_BTS_SIZE:
1012 ret = ptrace_bts_get_size(child);
1013 break;
1015 case PTRACE_BTS_GET:
1016 ret = ptrace_bts_read_record
1017 (child, data, (struct bts_struct __user *) addr);
1018 break;
1020 case PTRACE_BTS_CLEAR:
1021 ret = ptrace_bts_clear(child);
1022 break;
1024 case PTRACE_BTS_DRAIN:
1025 ret = ptrace_bts_drain
1026 (child, data, (struct bts_struct __user *) addr);
1027 break;
1028 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1029 =======
1030 #endif
1031 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1033 default:
1034 ret = ptrace_request(child, request, addr, data);
1035 break;
1038 return ret;
1041 #ifdef CONFIG_IA32_EMULATION
1043 #include <linux/compat.h>
1044 #include <linux/syscalls.h>
1045 #include <asm/ia32.h>
1046 #include <asm/user32.h>
1048 #define R32(l,q) \
1049 case offsetof(struct user32, regs.l): \
1050 regs->q = value; break
1052 #define SEG32(rs) \
1053 case offsetof(struct user32, regs.rs): \
1054 return set_segment_reg(child, \
1055 offsetof(struct user_regs_struct, rs), \
1056 value); \
1057 break
1059 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1061 struct pt_regs *regs = task_pt_regs(child);
1063 switch (regno) {
1065 SEG32(cs);
1066 SEG32(ds);
1067 SEG32(es);
1068 SEG32(fs);
1069 SEG32(gs);
1070 SEG32(ss);
1072 R32(ebx, bx);
1073 R32(ecx, cx);
1074 R32(edx, dx);
1075 R32(edi, di);
1076 R32(esi, si);
1077 R32(ebp, bp);
1078 R32(eax, ax);
1079 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1080 R32(orig_eax, orig_ax);
1081 =======
1082 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1083 R32(eip, ip);
1084 R32(esp, sp);
1086 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1087 =======
1088 case offsetof(struct user32, regs.orig_eax):
1090 * Sign-extend the value so that orig_eax = -1
1091 * causes (long)orig_ax < 0 tests to fire correctly.
1093 regs->orig_ax = (long) (s32) value;
1094 break;
1096 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1097 case offsetof(struct user32, regs.eflags):
1098 return set_flags(child, value);
1100 case offsetof(struct user32, u_debugreg[0]) ...
1101 offsetof(struct user32, u_debugreg[7]):
1102 regno -= offsetof(struct user32, u_debugreg[0]);
1103 return ptrace_set_debugreg(child, regno / 4, value);
1105 default:
1106 if (regno > sizeof(struct user32) || (regno & 3))
1107 return -EIO;
1110 * Other dummy fields in the virtual user structure
1111 * are ignored
1113 break;
1115 return 0;
1118 #undef R32
1119 #undef SEG32
1121 #define R32(l,q) \
1122 case offsetof(struct user32, regs.l): \
1123 *val = regs->q; break
1125 #define SEG32(rs) \
1126 case offsetof(struct user32, regs.rs): \
1127 *val = get_segment_reg(child, \
1128 offsetof(struct user_regs_struct, rs)); \
1129 break
1131 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1133 struct pt_regs *regs = task_pt_regs(child);
1135 switch (regno) {
1137 SEG32(ds);
1138 SEG32(es);
1139 SEG32(fs);
1140 SEG32(gs);
1142 R32(cs, cs);
1143 R32(ss, ss);
1144 R32(ebx, bx);
1145 R32(ecx, cx);
1146 R32(edx, dx);
1147 R32(edi, di);
1148 R32(esi, si);
1149 R32(ebp, bp);
1150 R32(eax, ax);
1151 R32(orig_eax, orig_ax);
1152 R32(eip, ip);
1153 R32(esp, sp);
1155 case offsetof(struct user32, regs.eflags):
1156 *val = get_flags(child);
1157 break;
1159 case offsetof(struct user32, u_debugreg[0]) ...
1160 offsetof(struct user32, u_debugreg[7]):
1161 regno -= offsetof(struct user32, u_debugreg[0]);
1162 *val = ptrace_get_debugreg(child, regno / 4);
1163 break;
1165 default:
1166 if (regno > sizeof(struct user32) || (regno & 3))
1167 return -EIO;
1170 * Other dummy fields in the virtual user structure
1171 * are ignored
1173 *val = 0;
1174 break;
1176 return 0;
1179 #undef R32
1180 #undef SEG32
1182 static int genregs32_get(struct task_struct *target,
1183 const struct user_regset *regset,
1184 unsigned int pos, unsigned int count,
1185 void *kbuf, void __user *ubuf)
1187 if (kbuf) {
1188 compat_ulong_t *k = kbuf;
1189 while (count > 0) {
1190 getreg32(target, pos, k++);
1191 count -= sizeof(*k);
1192 pos += sizeof(*k);
1194 } else {
1195 compat_ulong_t __user *u = ubuf;
1196 while (count > 0) {
1197 compat_ulong_t word;
1198 getreg32(target, pos, &word);
1199 if (__put_user(word, u++))
1200 return -EFAULT;
1201 count -= sizeof(*u);
1202 pos += sizeof(*u);
1206 return 0;
1209 static int genregs32_set(struct task_struct *target,
1210 const struct user_regset *regset,
1211 unsigned int pos, unsigned int count,
1212 const void *kbuf, const void __user *ubuf)
1214 int ret = 0;
1215 if (kbuf) {
1216 const compat_ulong_t *k = kbuf;
1217 while (count > 0 && !ret) {
1218 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1219 ret = putreg(target, pos, *k++);
1220 =======
1221 ret = putreg32(target, pos, *k++);
1222 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1223 count -= sizeof(*k);
1224 pos += sizeof(*k);
1226 } else {
1227 const compat_ulong_t __user *u = ubuf;
1228 while (count > 0 && !ret) {
1229 compat_ulong_t word;
1230 ret = __get_user(word, u++);
1231 if (ret)
1232 break;
1233 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1234 ret = putreg(target, pos, word);
1235 =======
1236 ret = putreg32(target, pos, word);
1237 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1238 count -= sizeof(*u);
1239 pos += sizeof(*u);
1242 return ret;
1245 static long ptrace32_siginfo(unsigned request, u32 pid, u32 addr, u32 data)
1247 siginfo_t __user *si = compat_alloc_user_space(sizeof(siginfo_t));
1248 compat_siginfo_t __user *si32 = compat_ptr(data);
1249 siginfo_t ssi;
1250 int ret;
1252 if (request == PTRACE_SETSIGINFO) {
1253 memset(&ssi, 0, sizeof(siginfo_t));
1254 ret = copy_siginfo_from_user32(&ssi, si32);
1255 if (ret)
1256 return ret;
1257 if (copy_to_user(si, &ssi, sizeof(siginfo_t)))
1258 return -EFAULT;
1260 ret = sys_ptrace(request, pid, addr, (unsigned long)si);
1261 if (ret)
1262 return ret;
1263 if (request == PTRACE_GETSIGINFO) {
1264 if (copy_from_user(&ssi, si, sizeof(siginfo_t)))
1265 return -EFAULT;
1266 ret = copy_siginfo_to_user32(si32, &ssi);
1268 return ret;
1271 asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data)
1273 struct task_struct *child;
1274 struct pt_regs *childregs;
1275 void __user *datap = compat_ptr(data);
1276 int ret;
1277 __u32 val;
1279 switch (request) {
1280 case PTRACE_TRACEME:
1281 case PTRACE_ATTACH:
1282 case PTRACE_KILL:
1283 case PTRACE_CONT:
1284 case PTRACE_SINGLESTEP:
1285 case PTRACE_SINGLEBLOCK:
1286 case PTRACE_DETACH:
1287 case PTRACE_SYSCALL:
1288 case PTRACE_OLDSETOPTIONS:
1289 case PTRACE_SETOPTIONS:
1290 case PTRACE_SET_THREAD_AREA:
1291 case PTRACE_GET_THREAD_AREA:
1292 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1293 =======
1294 #ifdef X86_BTS
1295 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1296 case PTRACE_BTS_CONFIG:
1297 case PTRACE_BTS_STATUS:
1298 case PTRACE_BTS_SIZE:
1299 case PTRACE_BTS_GET:
1300 case PTRACE_BTS_CLEAR:
1301 case PTRACE_BTS_DRAIN:
1302 <<<<<<< HEAD:arch/x86/kernel/ptrace.c
1303 =======
1304 #endif
1305 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/x86/kernel/ptrace.c
1306 return sys_ptrace(request, pid, addr, data);
1308 default:
1309 return -EINVAL;
1311 case PTRACE_PEEKTEXT:
1312 case PTRACE_PEEKDATA:
1313 case PTRACE_POKEDATA:
1314 case PTRACE_POKETEXT:
1315 case PTRACE_POKEUSR:
1316 case PTRACE_PEEKUSR:
1317 case PTRACE_GETREGS:
1318 case PTRACE_SETREGS:
1319 case PTRACE_SETFPREGS:
1320 case PTRACE_GETFPREGS:
1321 case PTRACE_SETFPXREGS:
1322 case PTRACE_GETFPXREGS:
1323 case PTRACE_GETEVENTMSG:
1324 break;
1326 case PTRACE_SETSIGINFO:
1327 case PTRACE_GETSIGINFO:
1328 return ptrace32_siginfo(request, pid, addr, data);
1331 child = ptrace_get_task_struct(pid);
1332 if (IS_ERR(child))
1333 return PTR_ERR(child);
1335 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1336 if (ret < 0)
1337 goto out;
1339 childregs = task_pt_regs(child);
1341 switch (request) {
1342 case PTRACE_PEEKUSR:
1343 ret = getreg32(child, addr, &val);
1344 if (ret == 0)
1345 ret = put_user(val, (__u32 __user *)datap);
1346 break;
1348 case PTRACE_POKEUSR:
1349 ret = putreg32(child, addr, data);
1350 break;
1352 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1353 return copy_regset_to_user(child, &user_x86_32_view,
1354 REGSET_GENERAL,
1355 0, sizeof(struct user_regs_struct32),
1356 datap);
1358 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1359 return copy_regset_from_user(child, &user_x86_32_view,
1360 REGSET_GENERAL, 0,
1361 sizeof(struct user_regs_struct32),
1362 datap);
1364 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1365 return copy_regset_to_user(child, &user_x86_32_view,
1366 REGSET_FP, 0,
1367 sizeof(struct user_i387_ia32_struct),
1368 datap);
1370 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1371 return copy_regset_from_user(
1372 child, &user_x86_32_view, REGSET_FP,
1373 0, sizeof(struct user_i387_ia32_struct), datap);
1375 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1376 return copy_regset_to_user(child, &user_x86_32_view,
1377 REGSET_XFP, 0,
1378 sizeof(struct user32_fxsr_struct),
1379 datap);
1381 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1382 return copy_regset_from_user(child, &user_x86_32_view,
1383 REGSET_XFP, 0,
1384 sizeof(struct user32_fxsr_struct),
1385 datap);
1387 default:
1388 return compat_ptrace_request(child, request, addr, data);
1391 out:
1392 put_task_struct(child);
1393 return ret;
1396 #endif /* CONFIG_IA32_EMULATION */
1398 #ifdef CONFIG_X86_64
1400 static const struct user_regset x86_64_regsets[] = {
1401 [REGSET_GENERAL] = {
1402 .core_note_type = NT_PRSTATUS,
1403 .n = sizeof(struct user_regs_struct) / sizeof(long),
1404 .size = sizeof(long), .align = sizeof(long),
1405 .get = genregs_get, .set = genregs_set
1407 [REGSET_FP] = {
1408 .core_note_type = NT_PRFPREG,
1409 .n = sizeof(struct user_i387_struct) / sizeof(long),
1410 .size = sizeof(long), .align = sizeof(long),
1411 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1415 static const struct user_regset_view user_x86_64_view = {
1416 .name = "x86_64", .e_machine = EM_X86_64,
1417 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1420 #else /* CONFIG_X86_32 */
1422 #define user_regs_struct32 user_regs_struct
1423 #define genregs32_get genregs_get
1424 #define genregs32_set genregs_set
1426 #endif /* CONFIG_X86_64 */
1428 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1429 static const struct user_regset x86_32_regsets[] = {
1430 [REGSET_GENERAL] = {
1431 .core_note_type = NT_PRSTATUS,
1432 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1433 .size = sizeof(u32), .align = sizeof(u32),
1434 .get = genregs32_get, .set = genregs32_set
1436 [REGSET_FP] = {
1437 .core_note_type = NT_PRFPREG,
1438 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1439 .size = sizeof(u32), .align = sizeof(u32),
1440 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1442 [REGSET_XFP] = {
1443 .core_note_type = NT_PRXFPREG,
1444 .n = sizeof(struct user_i387_struct) / sizeof(u32),
1445 .size = sizeof(u32), .align = sizeof(u32),
1446 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1448 [REGSET_TLS] = {
1449 .core_note_type = NT_386_TLS,
1450 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1451 .size = sizeof(struct user_desc),
1452 .align = sizeof(struct user_desc),
1453 .active = regset_tls_active,
1454 .get = regset_tls_get, .set = regset_tls_set
1458 static const struct user_regset_view user_x86_32_view = {
1459 .name = "i386", .e_machine = EM_386,
1460 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1462 #endif
1464 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1466 #ifdef CONFIG_IA32_EMULATION
1467 if (test_tsk_thread_flag(task, TIF_IA32))
1468 #endif
1469 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1470 return &user_x86_32_view;
1471 #endif
1472 #ifdef CONFIG_X86_64
1473 return &user_x86_64_view;
1474 #endif
1477 #ifdef CONFIG_X86_32
1479 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_code)
1481 struct siginfo info;
1483 tsk->thread.trap_no = 1;
1484 tsk->thread.error_code = error_code;
1486 memset(&info, 0, sizeof(info));
1487 info.si_signo = SIGTRAP;
1488 info.si_code = TRAP_BRKPT;
1490 /* User-mode ip? */
1491 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1493 /* Send us the fake SIGTRAP */
1494 force_sig_info(SIGTRAP, &info, tsk);
1497 /* notification of system call entry/exit
1498 * - triggered by current->work.syscall_trace
1500 __attribute__((regparm(3)))
1501 int do_syscall_trace(struct pt_regs *regs, int entryexit)
1503 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1505 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1506 * interception
1508 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
1509 int ret = 0;
1511 /* do the secure computing check first */
1512 if (!entryexit)
1513 secure_computing(regs->orig_ax);
1515 if (unlikely(current->audit_context)) {
1516 if (entryexit)
1517 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1518 regs->ax);
1519 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1520 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1521 * not used, entry.S will call us only on syscall exit, not
1522 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1523 * calling send_sigtrap() on syscall entry.
1525 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1526 * is_singlestep is false, despite his name, so we will still do
1527 * the correct thing.
1529 else if (is_singlestep)
1530 goto out;
1533 if (!(current->ptrace & PT_PTRACED))
1534 goto out;
1536 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1537 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1538 * here. We have to check this and return */
1539 if (is_sysemu && entryexit)
1540 return 0;
1542 /* Fake a debug trap */
1543 if (is_singlestep)
1544 send_sigtrap(current, regs, 0);
1546 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
1547 goto out;
1549 /* the 0x80 provides a way for the tracing parent to distinguish
1550 between a syscall stop and SIGTRAP delivery */
1551 /* Note that the debugger could change the result of test_thread_flag!*/
1552 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
1555 * this isn't the same as continuing with a signal, but it will do
1556 * for normal use. strace only continues with a signal if the
1557 * stopping signal is not SIGTRAP. -brl
1559 if (current->exit_code) {
1560 send_sig(current->exit_code, current, 1);
1561 current->exit_code = 0;
1563 ret = is_sysemu;
1564 out:
1565 if (unlikely(current->audit_context) && !entryexit)
1566 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1567 regs->bx, regs->cx, regs->dx, regs->si);
1568 if (ret == 0)
1569 return 0;
1571 regs->orig_ax = -1; /* force skip of syscall restarting */
1572 if (unlikely(current->audit_context))
1573 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1574 return 1;
1577 #else /* CONFIG_X86_64 */
1579 static void syscall_trace(struct pt_regs *regs)
1582 #if 0
1583 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1584 current->comm,
1585 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1586 current_thread_info()->flags, current->ptrace);
1587 #endif
1589 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1590 ? 0x80 : 0));
1592 * this isn't the same as continuing with a signal, but it will do
1593 * for normal use. strace only continues with a signal if the
1594 * stopping signal is not SIGTRAP. -brl
1596 if (current->exit_code) {
1597 send_sig(current->exit_code, current, 1);
1598 current->exit_code = 0;
1602 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1604 /* do the secure computing check first */
1605 secure_computing(regs->orig_ax);
1607 if (test_thread_flag(TIF_SYSCALL_TRACE)
1608 && (current->ptrace & PT_PTRACED))
1609 syscall_trace(regs);
1611 if (unlikely(current->audit_context)) {
1612 if (test_thread_flag(TIF_IA32)) {
1613 audit_syscall_entry(AUDIT_ARCH_I386,
1614 regs->orig_ax,
1615 regs->bx, regs->cx,
1616 regs->dx, regs->si);
1617 } else {
1618 audit_syscall_entry(AUDIT_ARCH_X86_64,
1619 regs->orig_ax,
1620 regs->di, regs->si,
1621 regs->dx, regs->r10);
1626 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1628 if (unlikely(current->audit_context))
1629 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1631 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1632 || test_thread_flag(TIF_SINGLESTEP))
1633 && (current->ptrace & PT_PTRACED))
1634 syscall_trace(regs);
1637 #endif /* CONFIG_X86_32 */