signals: demultiplexing SIGTRAP signal
[linux-2.6/mini2440.git] / arch / x86 / kernel / ptrace.c
blobbf45cdf1aaca340a7332cd54dd5ff43938521708
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/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>
29 #include <asm/i387.h>
30 #include <asm/debugreg.h>
31 #include <asm/ldt.h>
32 #include <asm/desc.h>
33 #include <asm/prctl.h>
34 #include <asm/proto.h>
35 #include <asm/ds.h>
37 #include "tls.h"
39 enum x86_regset {
40 REGSET_GENERAL,
41 REGSET_FP,
42 REGSET_XFP,
43 REGSET_TLS,
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);
69 #ifdef CONFIG_X86_32
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);
76 regno >>= 2;
77 if (regno > FS)
78 --regno;
79 return &regs->bx + regno;
82 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
85 * Returning the value truncates it to 16 bits.
87 unsigned int retval;
88 if (offset != offsetof(struct user_regs_struct, gs))
89 retval = *pt_regs_access(task_pt_regs(task), offset);
90 else {
91 retval = task->thread.gs;
92 if (task == current)
93 savesegment(gs, retval);
95 return 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))
105 return -EIO;
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.
116 switch (offset) {
117 case offsetof(struct user_regs_struct, cs):
118 case offsetof(struct user_regs_struct, ss):
119 if (unlikely(value == 0))
120 return -EIO;
122 default:
123 *pt_regs_access(task_pt_regs(task), offset) = value;
124 break;
126 case offsetof(struct user_regs_struct, gs):
127 task->thread.gs = value;
128 if (task == current)
130 * The user-mode %gs is not affected by
131 * kernel entry, so we must update the CPU.
133 loadsegment(gs, value);
136 return 0;
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 &regs->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.
159 unsigned int seg;
161 switch (offset) {
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));
166 return 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));
172 return 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));
178 return 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));
184 return seg;
186 return task->thread.es;
188 case offsetof(struct user_regs_struct, cs):
189 case offsetof(struct user_regs_struct, ss):
190 break;
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))
202 return -EIO;
204 switch (offset) {
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))
214 break;
215 task->thread.fsindex = value;
216 if (task == current)
217 loadsegment(fs, task->thread.fsindex);
218 break;
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))
228 break;
229 task->thread.gsindex = value;
230 if (task == current)
231 load_gs_index(task->thread.gsindex);
232 break;
233 case offsetof(struct user_regs_struct,ds):
234 task->thread.ds = value;
235 if (task == current)
236 loadsegment(ds, task->thread.ds);
237 break;
238 case offsetof(struct user_regs_struct,es):
239 task->thread.es = value;
240 if (task == current)
241 loadsegment(es, task->thread.es);
242 break;
245 * Can't actually change these in 64-bit mode.
247 case offsetof(struct user_regs_struct,cs):
248 if (unlikely(value == 0))
249 return -EIO;
250 #ifdef CONFIG_IA32_EMULATION
251 if (test_tsk_thread_flag(task, TIF_IA32))
252 task_pt_regs(task)->cs = value;
253 #endif
254 break;
255 case offsetof(struct user_regs_struct,ss):
256 if (unlikely(value == 0))
257 return -EIO;
258 #ifdef CONFIG_IA32_EMULATION
259 if (test_tsk_thread_flag(task, TIF_IA32))
260 task_pt_regs(task)->ss = value;
261 #endif
262 break;
265 return 0;
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;
273 #endif
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;
289 return retval;
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);
308 return 0;
311 static int putreg(struct task_struct *child,
312 unsigned long offset, unsigned long value)
314 switch (offset) {
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);
326 #ifdef CONFIG_X86_64
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;
335 break;
337 case offsetof(struct user_regs_struct,fs_base):
338 if (value >= TASK_SIZE_OF(child))
339 return -EIO;
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);
347 return 0;
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))
353 return -EIO;
354 if (child->thread.gs != value)
355 return do_arch_prctl(child, ARCH_SET_GS, value);
356 return 0;
357 #endif
360 *pt_regs_access(task_pt_regs(child), offset) = value;
361 return 0;
364 static unsigned long getreg(struct task_struct *task, unsigned long offset)
366 switch (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);
378 #ifdef CONFIG_X86_64
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;
388 if (task == current)
389 asm("movl %%fs,%0" : "=r" (seg));
390 if (seg != FS_TLS_SEL)
391 return 0;
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;
401 if (task == current)
402 asm("movl %%gs,%0" : "=r" (seg));
403 if (seg != GS_TLS_SEL)
404 return 0;
405 return get_desc_base(&task->thread.tls_array[GS_TLS]);
407 #endif
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)
418 if (kbuf) {
419 unsigned long *k = kbuf;
420 while (count > 0) {
421 *k++ = getreg(target, pos);
422 count -= sizeof(*k);
423 pos += sizeof(*k);
425 } else {
426 unsigned long __user *u = ubuf;
427 while (count > 0) {
428 if (__put_user(getreg(target, pos), u++))
429 return -EFAULT;
430 count -= sizeof(*u);
431 pos += sizeof(*u);
435 return 0;
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)
443 int ret = 0;
444 if (kbuf) {
445 const unsigned long *k = kbuf;
446 while (count > 0 && !ret) {
447 ret = putreg(target, pos, *k++);
448 count -= sizeof(*k);
449 pos += sizeof(*k);
451 } else {
452 const unsigned long __user *u = ubuf;
453 while (count > 0 && !ret) {
454 unsigned long word;
455 ret = __get_user(word, u++);
456 if (ret)
457 break;
458 ret = putreg(target, pos, word);
459 count -= sizeof(*u);
460 pos += sizeof(*u);
463 return ret;
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)
473 switch (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;
481 return 0;
484 static int ptrace_set_debugreg(struct task_struct *child,
485 int n, unsigned long data)
487 int i;
489 if (unlikely(n == 4 || n == 5))
490 return -EIO;
492 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
493 return -EIO;
495 switch (n) {
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;
501 case 6:
502 if ((data & ~0xffffffffUL) != 0)
503 return -EIO;
504 child->thread.debugreg6 = data;
505 break;
507 case 7:
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
515 * values.
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
524 * mask |= 0x4444.
525 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
526 * 0x1110.
528 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
530 * See the Intel Manual "System Programming Guide",
531 * 15.2.4
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)
538 #ifdef CONFIG_X86_32
539 #define DR7_MASK 0x5f54
540 #else
541 #define DR7_MASK 0x5554
542 #endif
543 data &= ~DR_CONTROL_RESERVED;
544 for (i = 0; i < 4; i++)
545 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
546 return -EIO;
547 child->thread.debugreg7 = data;
548 if (data)
549 set_tsk_thread_flag(child, TIF_DEBUG);
550 else
551 clear_tsk_thread_flag(child, TIF_DEBUG);
552 break;
555 return 0;
558 #ifdef X86_BTS
560 static int ptrace_bts_get_size(struct task_struct *child)
562 if (!child->thread.ds_area_msr)
563 return -ENXIO;
565 return ds_get_bts_index((void *)child->thread.ds_area_msr);
568 static int ptrace_bts_read_record(struct task_struct *child,
569 long index,
570 struct bts_struct __user *out)
572 struct bts_struct ret;
573 int retval;
574 int bts_end;
575 int bts_index;
577 if (!child->thread.ds_area_msr)
578 return -ENXIO;
580 if (index < 0)
581 return -EINVAL;
583 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
584 if (bts_end <= index)
585 return -EINVAL;
587 /* translate the ptrace bts index into the ds bts index */
588 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
589 bts_index -= (index + 1);
590 if (bts_index < 0)
591 bts_index += bts_end;
593 retval = ds_read_bts((void *)child->thread.ds_area_msr,
594 bts_index, &ret);
595 if (retval < 0)
596 return retval;
598 if (copy_to_user(out, &ret, sizeof(ret)))
599 return -EFAULT;
601 return sizeof(ret);
604 static int ptrace_bts_clear(struct task_struct *child)
606 if (!child->thread.ds_area_msr)
607 return -ENXIO;
609 return ds_clear((void *)child->thread.ds_area_msr);
612 static int ptrace_bts_drain(struct task_struct *child,
613 long size,
614 struct bts_struct __user *out)
616 int end, i;
617 void *ds = (void *)child->thread.ds_area_msr;
619 if (!ds)
620 return -ENXIO;
622 end = ds_get_bts_index(ds);
623 if (end <= 0)
624 return end;
626 if (size < (end * sizeof(struct bts_struct)))
627 return -EIO;
629 for (i = 0; i < end; i++, out++) {
630 struct bts_struct ret;
631 int retval;
633 retval = ds_read_bts(ds, i, &ret);
634 if (retval < 0)
635 return retval;
637 if (copy_to_user(out, &ret, sizeof(ret)))
638 return -EFAULT;
641 ds_clear(ds);
643 return end;
646 static int ptrace_bts_config(struct task_struct *child,
647 long cfg_size,
648 const struct ptrace_bts_config __user *ucfg)
650 struct ptrace_bts_config cfg;
651 int bts_size, ret = 0;
652 void *ds;
654 if (cfg_size < sizeof(cfg))
655 return -EIO;
657 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
658 return -EFAULT;
660 if ((int)cfg.size < 0)
661 return -EINVAL;
663 bts_size = 0;
664 ds = (void *)child->thread.ds_area_msr;
665 if (ds) {
666 bts_size = ds_get_bts_size(ds);
667 if (bts_size < 0)
668 return bts_size;
670 cfg.size = PAGE_ALIGN(cfg.size);
672 if (bts_size != cfg.size) {
673 ret = ptrace_bts_realloc(child, cfg.size,
674 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
675 if (ret < 0)
676 goto errout;
678 ds = (void *)child->thread.ds_area_msr;
681 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
682 ret = ds_set_overflow(ds, DS_O_SIGNAL);
683 else
684 ret = ds_set_overflow(ds, DS_O_WRAP);
685 if (ret < 0)
686 goto errout;
688 if (cfg.flags & PTRACE_BTS_O_TRACE)
689 child->thread.debugctlmsr |= ds_debugctl_mask();
690 else
691 child->thread.debugctlmsr &= ~ds_debugctl_mask();
693 if (cfg.flags & PTRACE_BTS_O_SCHED)
694 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
695 else
696 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
698 ret = sizeof(cfg);
700 out:
701 if (child->thread.debugctlmsr)
702 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
703 else
704 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
706 return ret;
708 errout:
709 child->thread.debugctlmsr &= ~ds_debugctl_mask();
710 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
711 goto out;
714 static int ptrace_bts_status(struct task_struct *child,
715 long cfg_size,
716 struct ptrace_bts_config __user *ucfg)
718 void *ds = (void *)child->thread.ds_area_msr;
719 struct ptrace_bts_config cfg;
721 if (cfg_size < sizeof(cfg))
722 return -EIO;
724 memset(&cfg, 0, sizeof(cfg));
726 if (ds) {
727 cfg.size = ds_get_bts_size(ds);
729 if (ds_get_overflow(ds) == DS_O_SIGNAL)
730 cfg.flags |= PTRACE_BTS_O_SIGNAL;
732 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
733 child->thread.debugctlmsr & ds_debugctl_mask())
734 cfg.flags |= PTRACE_BTS_O_TRACE;
736 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
737 cfg.flags |= PTRACE_BTS_O_SCHED;
740 cfg.bts_size = sizeof(struct bts_struct);
742 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
743 return -EFAULT;
745 return sizeof(cfg);
749 static int ptrace_bts_write_record(struct task_struct *child,
750 const struct bts_struct *in)
752 int retval;
754 if (!child->thread.ds_area_msr)
755 return -ENXIO;
757 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
758 if (retval)
759 return retval;
761 return sizeof(*in);
764 static int ptrace_bts_realloc(struct task_struct *child,
765 int size, int reduce_size)
767 unsigned long rlim, vm;
768 int ret, old_size;
770 if (size < 0)
771 return -EINVAL;
773 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
774 if (old_size < 0)
775 return old_size;
777 ret = ds_free((void **)&child->thread.ds_area_msr);
778 if (ret < 0)
779 goto out;
781 size >>= PAGE_SHIFT;
782 old_size >>= PAGE_SHIFT;
784 current->mm->total_vm -= old_size;
785 current->mm->locked_vm -= old_size;
787 if (size == 0)
788 goto out;
790 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
791 vm = current->mm->total_vm + size;
792 if (rlim < vm) {
793 ret = -ENOMEM;
795 if (!reduce_size)
796 goto out;
798 size = rlim - current->mm->total_vm;
799 if (size <= 0)
800 goto out;
803 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
804 vm = current->mm->locked_vm + size;
805 if (rlim < vm) {
806 ret = -ENOMEM;
808 if (!reduce_size)
809 goto out;
811 size = rlim - current->mm->locked_vm;
812 if (size <= 0)
813 goto out;
816 ret = ds_allocate((void **)&child->thread.ds_area_msr,
817 size << PAGE_SHIFT);
818 if (ret < 0)
819 goto out;
821 current->mm->total_vm += size;
822 current->mm->locked_vm += size;
824 out:
825 if (child->thread.ds_area_msr)
826 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
827 else
828 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
830 return ret;
833 void ptrace_bts_take_timestamp(struct task_struct *tsk,
834 enum bts_qualifier qualifier)
836 struct bts_struct rec = {
837 .qualifier = qualifier,
838 .variant.jiffies = jiffies_64
841 ptrace_bts_write_record(tsk, &rec);
843 #endif /* X86_BTS */
846 * Called by kernel/ptrace.c when detaching..
848 * Make sure the single step bit is not set.
850 void ptrace_disable(struct task_struct *child)
852 user_disable_single_step(child);
853 #ifdef TIF_SYSCALL_EMU
854 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
855 #endif
856 if (child->thread.ds_area_msr) {
857 #ifdef X86_BTS
858 ptrace_bts_realloc(child, 0, 0);
859 #endif
860 child->thread.debugctlmsr &= ~ds_debugctl_mask();
861 if (!child->thread.debugctlmsr)
862 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
863 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
867 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
868 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
869 #endif
871 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
873 int ret;
874 unsigned long __user *datap = (unsigned long __user *)data;
876 switch (request) {
877 /* read the word at location addr in the USER area. */
878 case PTRACE_PEEKUSR: {
879 unsigned long tmp;
881 ret = -EIO;
882 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
883 addr >= sizeof(struct user))
884 break;
886 tmp = 0; /* Default return condition */
887 if (addr < sizeof(struct user_regs_struct))
888 tmp = getreg(child, addr);
889 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
890 addr <= offsetof(struct user, u_debugreg[7])) {
891 addr -= offsetof(struct user, u_debugreg[0]);
892 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
894 ret = put_user(tmp, datap);
895 break;
898 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
899 ret = -EIO;
900 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
901 addr >= sizeof(struct user))
902 break;
904 if (addr < sizeof(struct user_regs_struct))
905 ret = putreg(child, addr, data);
906 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
907 addr <= offsetof(struct user, u_debugreg[7])) {
908 addr -= offsetof(struct user, u_debugreg[0]);
909 ret = ptrace_set_debugreg(child,
910 addr / sizeof(data), data);
912 break;
914 case PTRACE_GETREGS: /* Get all gp regs from the child. */
915 return copy_regset_to_user(child,
916 task_user_regset_view(current),
917 REGSET_GENERAL,
918 0, sizeof(struct user_regs_struct),
919 datap);
921 case PTRACE_SETREGS: /* Set all gp regs in the child. */
922 return copy_regset_from_user(child,
923 task_user_regset_view(current),
924 REGSET_GENERAL,
925 0, sizeof(struct user_regs_struct),
926 datap);
928 case PTRACE_GETFPREGS: /* Get the child FPU state. */
929 return copy_regset_to_user(child,
930 task_user_regset_view(current),
931 REGSET_FP,
932 0, sizeof(struct user_i387_struct),
933 datap);
935 case PTRACE_SETFPREGS: /* Set the child FPU state. */
936 return copy_regset_from_user(child,
937 task_user_regset_view(current),
938 REGSET_FP,
939 0, sizeof(struct user_i387_struct),
940 datap);
942 #ifdef CONFIG_X86_32
943 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
944 return copy_regset_to_user(child, &user_x86_32_view,
945 REGSET_XFP,
946 0, sizeof(struct user_fxsr_struct),
947 datap) ? -EIO : 0;
949 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
950 return copy_regset_from_user(child, &user_x86_32_view,
951 REGSET_XFP,
952 0, sizeof(struct user_fxsr_struct),
953 datap) ? -EIO : 0;
954 #endif
956 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
957 case PTRACE_GET_THREAD_AREA:
958 if (addr < 0)
959 return -EIO;
960 ret = do_get_thread_area(child, addr,
961 (struct user_desc __user *) data);
962 break;
964 case PTRACE_SET_THREAD_AREA:
965 if (addr < 0)
966 return -EIO;
967 ret = do_set_thread_area(child, addr,
968 (struct user_desc __user *) data, 0);
969 break;
970 #endif
972 #ifdef CONFIG_X86_64
973 /* normal 64bit interface to access TLS data.
974 Works just like arch_prctl, except that the arguments
975 are reversed. */
976 case PTRACE_ARCH_PRCTL:
977 ret = do_arch_prctl(child, data, addr);
978 break;
979 #endif
982 * These bits need more cooking - not enabled yet:
984 #ifdef X86_BTS
985 case PTRACE_BTS_CONFIG:
986 ret = ptrace_bts_config
987 (child, data, (struct ptrace_bts_config __user *)addr);
988 break;
990 case PTRACE_BTS_STATUS:
991 ret = ptrace_bts_status
992 (child, data, (struct ptrace_bts_config __user *)addr);
993 break;
995 case PTRACE_BTS_SIZE:
996 ret = ptrace_bts_get_size(child);
997 break;
999 case PTRACE_BTS_GET:
1000 ret = ptrace_bts_read_record
1001 (child, data, (struct bts_struct __user *) addr);
1002 break;
1004 case PTRACE_BTS_CLEAR:
1005 ret = ptrace_bts_clear(child);
1006 break;
1008 case PTRACE_BTS_DRAIN:
1009 ret = ptrace_bts_drain
1010 (child, data, (struct bts_struct __user *) addr);
1011 break;
1012 #endif
1014 default:
1015 ret = ptrace_request(child, request, addr, data);
1016 break;
1019 return ret;
1022 #ifdef CONFIG_IA32_EMULATION
1024 #include <linux/compat.h>
1025 #include <linux/syscalls.h>
1026 #include <asm/ia32.h>
1027 #include <asm/user32.h>
1029 #define R32(l,q) \
1030 case offsetof(struct user32, regs.l): \
1031 regs->q = value; break
1033 #define SEG32(rs) \
1034 case offsetof(struct user32, regs.rs): \
1035 return set_segment_reg(child, \
1036 offsetof(struct user_regs_struct, rs), \
1037 value); \
1038 break
1040 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1042 struct pt_regs *regs = task_pt_regs(child);
1044 switch (regno) {
1046 SEG32(cs);
1047 SEG32(ds);
1048 SEG32(es);
1049 SEG32(fs);
1050 SEG32(gs);
1051 SEG32(ss);
1053 R32(ebx, bx);
1054 R32(ecx, cx);
1055 R32(edx, dx);
1056 R32(edi, di);
1057 R32(esi, si);
1058 R32(ebp, bp);
1059 R32(eax, ax);
1060 R32(eip, ip);
1061 R32(esp, sp);
1063 case offsetof(struct user32, regs.orig_eax):
1065 * Sign-extend the value so that orig_eax = -1
1066 * causes (long)orig_ax < 0 tests to fire correctly.
1068 regs->orig_ax = (long) (s32) value;
1069 break;
1071 case offsetof(struct user32, regs.eflags):
1072 return set_flags(child, value);
1074 case offsetof(struct user32, u_debugreg[0]) ...
1075 offsetof(struct user32, u_debugreg[7]):
1076 regno -= offsetof(struct user32, u_debugreg[0]);
1077 return ptrace_set_debugreg(child, regno / 4, value);
1079 default:
1080 if (regno > sizeof(struct user32) || (regno & 3))
1081 return -EIO;
1084 * Other dummy fields in the virtual user structure
1085 * are ignored
1087 break;
1089 return 0;
1092 #undef R32
1093 #undef SEG32
1095 #define R32(l,q) \
1096 case offsetof(struct user32, regs.l): \
1097 *val = regs->q; break
1099 #define SEG32(rs) \
1100 case offsetof(struct user32, regs.rs): \
1101 *val = get_segment_reg(child, \
1102 offsetof(struct user_regs_struct, rs)); \
1103 break
1105 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1107 struct pt_regs *regs = task_pt_regs(child);
1109 switch (regno) {
1111 SEG32(ds);
1112 SEG32(es);
1113 SEG32(fs);
1114 SEG32(gs);
1116 R32(cs, cs);
1117 R32(ss, ss);
1118 R32(ebx, bx);
1119 R32(ecx, cx);
1120 R32(edx, dx);
1121 R32(edi, di);
1122 R32(esi, si);
1123 R32(ebp, bp);
1124 R32(eax, ax);
1125 R32(orig_eax, orig_ax);
1126 R32(eip, ip);
1127 R32(esp, sp);
1129 case offsetof(struct user32, regs.eflags):
1130 *val = get_flags(child);
1131 break;
1133 case offsetof(struct user32, u_debugreg[0]) ...
1134 offsetof(struct user32, u_debugreg[7]):
1135 regno -= offsetof(struct user32, u_debugreg[0]);
1136 *val = ptrace_get_debugreg(child, regno / 4);
1137 break;
1139 default:
1140 if (regno > sizeof(struct user32) || (regno & 3))
1141 return -EIO;
1144 * Other dummy fields in the virtual user structure
1145 * are ignored
1147 *val = 0;
1148 break;
1150 return 0;
1153 #undef R32
1154 #undef SEG32
1156 static int genregs32_get(struct task_struct *target,
1157 const struct user_regset *regset,
1158 unsigned int pos, unsigned int count,
1159 void *kbuf, void __user *ubuf)
1161 if (kbuf) {
1162 compat_ulong_t *k = kbuf;
1163 while (count > 0) {
1164 getreg32(target, pos, k++);
1165 count -= sizeof(*k);
1166 pos += sizeof(*k);
1168 } else {
1169 compat_ulong_t __user *u = ubuf;
1170 while (count > 0) {
1171 compat_ulong_t word;
1172 getreg32(target, pos, &word);
1173 if (__put_user(word, u++))
1174 return -EFAULT;
1175 count -= sizeof(*u);
1176 pos += sizeof(*u);
1180 return 0;
1183 static int genregs32_set(struct task_struct *target,
1184 const struct user_regset *regset,
1185 unsigned int pos, unsigned int count,
1186 const void *kbuf, const void __user *ubuf)
1188 int ret = 0;
1189 if (kbuf) {
1190 const compat_ulong_t *k = kbuf;
1191 while (count > 0 && !ret) {
1192 ret = putreg32(target, pos, *k++);
1193 count -= sizeof(*k);
1194 pos += sizeof(*k);
1196 } else {
1197 const compat_ulong_t __user *u = ubuf;
1198 while (count > 0 && !ret) {
1199 compat_ulong_t word;
1200 ret = __get_user(word, u++);
1201 if (ret)
1202 break;
1203 ret = putreg32(target, pos, word);
1204 count -= sizeof(*u);
1205 pos += sizeof(*u);
1208 return ret;
1211 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1212 compat_ulong_t caddr, compat_ulong_t cdata)
1214 unsigned long addr = caddr;
1215 unsigned long data = cdata;
1216 void __user *datap = compat_ptr(data);
1217 int ret;
1218 __u32 val;
1220 switch (request) {
1221 case PTRACE_PEEKUSR:
1222 ret = getreg32(child, addr, &val);
1223 if (ret == 0)
1224 ret = put_user(val, (__u32 __user *)datap);
1225 break;
1227 case PTRACE_POKEUSR:
1228 ret = putreg32(child, addr, data);
1229 break;
1231 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1232 return copy_regset_to_user(child, &user_x86_32_view,
1233 REGSET_GENERAL,
1234 0, sizeof(struct user_regs_struct32),
1235 datap);
1237 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1238 return copy_regset_from_user(child, &user_x86_32_view,
1239 REGSET_GENERAL, 0,
1240 sizeof(struct user_regs_struct32),
1241 datap);
1243 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1244 return copy_regset_to_user(child, &user_x86_32_view,
1245 REGSET_FP, 0,
1246 sizeof(struct user_i387_ia32_struct),
1247 datap);
1249 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1250 return copy_regset_from_user(
1251 child, &user_x86_32_view, REGSET_FP,
1252 0, sizeof(struct user_i387_ia32_struct), datap);
1254 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1255 return copy_regset_to_user(child, &user_x86_32_view,
1256 REGSET_XFP, 0,
1257 sizeof(struct user32_fxsr_struct),
1258 datap);
1260 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1261 return copy_regset_from_user(child, &user_x86_32_view,
1262 REGSET_XFP, 0,
1263 sizeof(struct user32_fxsr_struct),
1264 datap);
1266 case PTRACE_GET_THREAD_AREA:
1267 case PTRACE_SET_THREAD_AREA:
1268 return arch_ptrace(child, request, addr, data);
1270 default:
1271 return compat_ptrace_request(child, request, addr, data);
1274 return ret;
1277 #endif /* CONFIG_IA32_EMULATION */
1279 #ifdef CONFIG_X86_64
1281 static const struct user_regset x86_64_regsets[] = {
1282 [REGSET_GENERAL] = {
1283 .core_note_type = NT_PRSTATUS,
1284 .n = sizeof(struct user_regs_struct) / sizeof(long),
1285 .size = sizeof(long), .align = sizeof(long),
1286 .get = genregs_get, .set = genregs_set
1288 [REGSET_FP] = {
1289 .core_note_type = NT_PRFPREG,
1290 .n = sizeof(struct user_i387_struct) / sizeof(long),
1291 .size = sizeof(long), .align = sizeof(long),
1292 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1296 static const struct user_regset_view user_x86_64_view = {
1297 .name = "x86_64", .e_machine = EM_X86_64,
1298 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1301 #else /* CONFIG_X86_32 */
1303 #define user_regs_struct32 user_regs_struct
1304 #define genregs32_get genregs_get
1305 #define genregs32_set genregs_set
1307 #define user_i387_ia32_struct user_i387_struct
1308 #define user32_fxsr_struct user_fxsr_struct
1310 #endif /* CONFIG_X86_64 */
1312 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1313 static const struct user_regset x86_32_regsets[] = {
1314 [REGSET_GENERAL] = {
1315 .core_note_type = NT_PRSTATUS,
1316 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1317 .size = sizeof(u32), .align = sizeof(u32),
1318 .get = genregs32_get, .set = genregs32_set
1320 [REGSET_FP] = {
1321 .core_note_type = NT_PRFPREG,
1322 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1323 .size = sizeof(u32), .align = sizeof(u32),
1324 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1326 [REGSET_XFP] = {
1327 .core_note_type = NT_PRXFPREG,
1328 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1329 .size = sizeof(u32), .align = sizeof(u32),
1330 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1332 [REGSET_TLS] = {
1333 .core_note_type = NT_386_TLS,
1334 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1335 .size = sizeof(struct user_desc),
1336 .align = sizeof(struct user_desc),
1337 .active = regset_tls_active,
1338 .get = regset_tls_get, .set = regset_tls_set
1342 static const struct user_regset_view user_x86_32_view = {
1343 .name = "i386", .e_machine = EM_386,
1344 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1346 #endif
1348 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1350 #ifdef CONFIG_IA32_EMULATION
1351 if (test_tsk_thread_flag(task, TIF_IA32))
1352 #endif
1353 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1354 return &user_x86_32_view;
1355 #endif
1356 #ifdef CONFIG_X86_64
1357 return &user_x86_64_view;
1358 #endif
1361 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1362 int error_code, int si_code)
1364 struct siginfo info;
1366 tsk->thread.trap_no = 1;
1367 tsk->thread.error_code = error_code;
1369 memset(&info, 0, sizeof(info));
1370 info.si_signo = SIGTRAP;
1371 info.si_code = si_code;
1373 /* User-mode ip? */
1374 info.si_addr = user_mode_vm(regs) ? (void __user *) regs->ip : NULL;
1376 /* Send us the fake SIGTRAP */
1377 force_sig_info(SIGTRAP, &info, tsk);
1381 #ifdef CONFIG_X86_32
1382 # define IS_IA32 1
1383 #elif defined CONFIG_IA32_EMULATION
1384 # define IS_IA32 test_thread_flag(TIF_IA32)
1385 #else
1386 # define IS_IA32 0
1387 #endif
1390 * We must return the syscall number to actually look up in the table.
1391 * This can be -1L to skip running any syscall at all.
1393 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1395 long ret = 0;
1398 * If we stepped into a sysenter/syscall insn, it trapped in
1399 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1400 * If user-mode had set TF itself, then it's still clear from
1401 * do_debug() and we need to set it again to restore the user
1402 * state. If we entered on the slow path, TF was already set.
1404 if (test_thread_flag(TIF_SINGLESTEP))
1405 regs->flags |= X86_EFLAGS_TF;
1407 /* do the secure computing check first */
1408 secure_computing(regs->orig_ax);
1410 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1411 ret = -1L;
1413 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1414 tracehook_report_syscall_entry(regs))
1415 ret = -1L;
1417 if (unlikely(current->audit_context)) {
1418 if (IS_IA32)
1419 audit_syscall_entry(AUDIT_ARCH_I386,
1420 regs->orig_ax,
1421 regs->bx, regs->cx,
1422 regs->dx, regs->si);
1423 #ifdef CONFIG_X86_64
1424 else
1425 audit_syscall_entry(AUDIT_ARCH_X86_64,
1426 regs->orig_ax,
1427 regs->di, regs->si,
1428 regs->dx, regs->r10);
1429 #endif
1432 return ret ?: regs->orig_ax;
1435 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1437 if (unlikely(current->audit_context))
1438 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1440 if (test_thread_flag(TIF_SYSCALL_TRACE))
1441 tracehook_report_syscall_exit(regs, 0);
1444 * If TIF_SYSCALL_EMU is set, we only get here because of
1445 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1446 * We already reported this syscall instruction in
1447 * syscall_trace_enter(), so don't do any more now.
1449 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1450 return;
1453 * If we are single-stepping, synthesize a trap to follow the
1454 * system call instruction.
1456 if (test_thread_flag(TIF_SINGLESTEP) &&
1457 tracehook_consider_fatal_signal(current, SIGTRAP, SIG_DFL))
1458 send_sigtrap(current, regs, 0, TRAP_BRKPT);