OMAP: DISPC: Fix to disable also interface clocks. 2nd.
[linux-ginger.git] / arch / x86 / kernel / ptrace.c
bloba7835f2829361bcd964f85118839751fba24b12b
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
327 * Orig_ax is really just a flag with small positive and
328 * negative values, so make sure to always sign-extend it
329 * from 32 bits so that it works correctly regardless of
330 * whether we come from a 32-bit environment or not.
332 case offsetof(struct user_regs_struct, orig_ax):
333 value = (long) (s32) value;
334 break;
336 case offsetof(struct user_regs_struct,fs_base):
337 if (value >= TASK_SIZE_OF(child))
338 return -EIO;
340 * When changing the segment base, use do_arch_prctl
341 * to set either thread.fs or thread.fsindex and the
342 * corresponding GDT slot.
344 if (child->thread.fs != value)
345 return do_arch_prctl(child, ARCH_SET_FS, value);
346 return 0;
347 case offsetof(struct user_regs_struct,gs_base):
349 * Exactly the same here as the %fs handling above.
351 if (value >= TASK_SIZE_OF(child))
352 return -EIO;
353 if (child->thread.gs != value)
354 return do_arch_prctl(child, ARCH_SET_GS, value);
355 return 0;
356 #endif
359 *pt_regs_access(task_pt_regs(child), offset) = value;
360 return 0;
363 static unsigned long getreg(struct task_struct *task, unsigned long offset)
365 switch (offset) {
366 case offsetof(struct user_regs_struct, cs):
367 case offsetof(struct user_regs_struct, ds):
368 case offsetof(struct user_regs_struct, es):
369 case offsetof(struct user_regs_struct, fs):
370 case offsetof(struct user_regs_struct, gs):
371 case offsetof(struct user_regs_struct, ss):
372 return get_segment_reg(task, offset);
374 case offsetof(struct user_regs_struct, flags):
375 return get_flags(task);
377 #ifdef CONFIG_X86_64
378 case offsetof(struct user_regs_struct, fs_base): {
380 * do_arch_prctl may have used a GDT slot instead of
381 * the MSR. To userland, it appears the same either
382 * way, except the %fs segment selector might not be 0.
384 unsigned int seg = task->thread.fsindex;
385 if (task->thread.fs != 0)
386 return task->thread.fs;
387 if (task == current)
388 asm("movl %%fs,%0" : "=r" (seg));
389 if (seg != FS_TLS_SEL)
390 return 0;
391 return get_desc_base(&task->thread.tls_array[FS_TLS]);
393 case offsetof(struct user_regs_struct, gs_base): {
395 * Exactly the same here as the %fs handling above.
397 unsigned int seg = task->thread.gsindex;
398 if (task->thread.gs != 0)
399 return task->thread.gs;
400 if (task == current)
401 asm("movl %%gs,%0" : "=r" (seg));
402 if (seg != GS_TLS_SEL)
403 return 0;
404 return get_desc_base(&task->thread.tls_array[GS_TLS]);
406 #endif
409 return *pt_regs_access(task_pt_regs(task), offset);
412 static int genregs_get(struct task_struct *target,
413 const struct user_regset *regset,
414 unsigned int pos, unsigned int count,
415 void *kbuf, void __user *ubuf)
417 if (kbuf) {
418 unsigned long *k = kbuf;
419 while (count > 0) {
420 *k++ = getreg(target, pos);
421 count -= sizeof(*k);
422 pos += sizeof(*k);
424 } else {
425 unsigned long __user *u = ubuf;
426 while (count > 0) {
427 if (__put_user(getreg(target, pos), u++))
428 return -EFAULT;
429 count -= sizeof(*u);
430 pos += sizeof(*u);
434 return 0;
437 static int genregs_set(struct task_struct *target,
438 const struct user_regset *regset,
439 unsigned int pos, unsigned int count,
440 const void *kbuf, const void __user *ubuf)
442 int ret = 0;
443 if (kbuf) {
444 const unsigned long *k = kbuf;
445 while (count > 0 && !ret) {
446 ret = putreg(target, pos, *k++);
447 count -= sizeof(*k);
448 pos += sizeof(*k);
450 } else {
451 const unsigned long __user *u = ubuf;
452 while (count > 0 && !ret) {
453 unsigned long word;
454 ret = __get_user(word, u++);
455 if (ret)
456 break;
457 ret = putreg(target, pos, word);
458 count -= sizeof(*u);
459 pos += sizeof(*u);
462 return ret;
466 * This function is trivial and will be inlined by the compiler.
467 * Having it separates the implementation details of debug
468 * registers from the interface details of ptrace.
470 static unsigned long ptrace_get_debugreg(struct task_struct *child, int n)
472 switch (n) {
473 case 0: return child->thread.debugreg0;
474 case 1: return child->thread.debugreg1;
475 case 2: return child->thread.debugreg2;
476 case 3: return child->thread.debugreg3;
477 case 6: return child->thread.debugreg6;
478 case 7: return child->thread.debugreg7;
480 return 0;
483 static int ptrace_set_debugreg(struct task_struct *child,
484 int n, unsigned long data)
486 int i;
488 if (unlikely(n == 4 || n == 5))
489 return -EIO;
491 if (n < 4 && unlikely(data >= debugreg_addr_limit(child)))
492 return -EIO;
494 switch (n) {
495 case 0: child->thread.debugreg0 = data; break;
496 case 1: child->thread.debugreg1 = data; break;
497 case 2: child->thread.debugreg2 = data; break;
498 case 3: child->thread.debugreg3 = data; break;
500 case 6:
501 if ((data & ~0xffffffffUL) != 0)
502 return -EIO;
503 child->thread.debugreg6 = data;
504 break;
506 case 7:
508 * Sanity-check data. Take one half-byte at once with
509 * check = (val >> (16 + 4*i)) & 0xf. It contains the
510 * R/Wi and LENi bits; bits 0 and 1 are R/Wi, and bits
511 * 2 and 3 are LENi. Given a list of invalid values,
512 * we do mask |= 1 << invalid_value, so that
513 * (mask >> check) & 1 is a correct test for invalid
514 * values.
516 * R/Wi contains the type of the breakpoint /
517 * watchpoint, LENi contains the length of the watched
518 * data in the watchpoint case.
520 * The invalid values are:
521 * - LENi == 0x10 (undefined), so mask |= 0x0f00. [32-bit]
522 * - R/Wi == 0x10 (break on I/O reads or writes), so
523 * mask |= 0x4444.
524 * - R/Wi == 0x00 && LENi != 0x00, so we have mask |=
525 * 0x1110.
527 * Finally, mask = 0x0f00 | 0x4444 | 0x1110 == 0x5f54.
529 * See the Intel Manual "System Programming Guide",
530 * 15.2.4
532 * Note that LENi == 0x10 is defined on x86_64 in long
533 * mode (i.e. even for 32-bit userspace software, but
534 * 64-bit kernel), so the x86_64 mask value is 0x5454.
535 * See the AMD manual no. 24593 (AMD64 System Programming)
537 #ifdef CONFIG_X86_32
538 #define DR7_MASK 0x5f54
539 #else
540 #define DR7_MASK 0x5554
541 #endif
542 data &= ~DR_CONTROL_RESERVED;
543 for (i = 0; i < 4; i++)
544 if ((DR7_MASK >> ((data >> (16 + 4*i)) & 0xf)) & 1)
545 return -EIO;
546 child->thread.debugreg7 = data;
547 if (data)
548 set_tsk_thread_flag(child, TIF_DEBUG);
549 else
550 clear_tsk_thread_flag(child, TIF_DEBUG);
551 break;
554 return 0;
557 #ifdef X86_BTS
559 static int ptrace_bts_get_size(struct task_struct *child)
561 if (!child->thread.ds_area_msr)
562 return -ENXIO;
564 return ds_get_bts_index((void *)child->thread.ds_area_msr);
567 static int ptrace_bts_read_record(struct task_struct *child,
568 long index,
569 struct bts_struct __user *out)
571 struct bts_struct ret;
572 int retval;
573 int bts_end;
574 int bts_index;
576 if (!child->thread.ds_area_msr)
577 return -ENXIO;
579 if (index < 0)
580 return -EINVAL;
582 bts_end = ds_get_bts_end((void *)child->thread.ds_area_msr);
583 if (bts_end <= index)
584 return -EINVAL;
586 /* translate the ptrace bts index into the ds bts index */
587 bts_index = ds_get_bts_index((void *)child->thread.ds_area_msr);
588 bts_index -= (index + 1);
589 if (bts_index < 0)
590 bts_index += bts_end;
592 retval = ds_read_bts((void *)child->thread.ds_area_msr,
593 bts_index, &ret);
594 if (retval < 0)
595 return retval;
597 if (copy_to_user(out, &ret, sizeof(ret)))
598 return -EFAULT;
600 return sizeof(ret);
603 static int ptrace_bts_clear(struct task_struct *child)
605 if (!child->thread.ds_area_msr)
606 return -ENXIO;
608 return ds_clear((void *)child->thread.ds_area_msr);
611 static int ptrace_bts_drain(struct task_struct *child,
612 long size,
613 struct bts_struct __user *out)
615 int end, i;
616 void *ds = (void *)child->thread.ds_area_msr;
618 if (!ds)
619 return -ENXIO;
621 end = ds_get_bts_index(ds);
622 if (end <= 0)
623 return end;
625 if (size < (end * sizeof(struct bts_struct)))
626 return -EIO;
628 for (i = 0; i < end; i++, out++) {
629 struct bts_struct ret;
630 int retval;
632 retval = ds_read_bts(ds, i, &ret);
633 if (retval < 0)
634 return retval;
636 if (copy_to_user(out, &ret, sizeof(ret)))
637 return -EFAULT;
640 ds_clear(ds);
642 return end;
645 static int ptrace_bts_config(struct task_struct *child,
646 long cfg_size,
647 const struct ptrace_bts_config __user *ucfg)
649 struct ptrace_bts_config cfg;
650 int bts_size, ret = 0;
651 void *ds;
653 if (cfg_size < sizeof(cfg))
654 return -EIO;
656 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
657 return -EFAULT;
659 if ((int)cfg.size < 0)
660 return -EINVAL;
662 bts_size = 0;
663 ds = (void *)child->thread.ds_area_msr;
664 if (ds) {
665 bts_size = ds_get_bts_size(ds);
666 if (bts_size < 0)
667 return bts_size;
669 cfg.size = PAGE_ALIGN(cfg.size);
671 if (bts_size != cfg.size) {
672 ret = ptrace_bts_realloc(child, cfg.size,
673 cfg.flags & PTRACE_BTS_O_CUT_SIZE);
674 if (ret < 0)
675 goto errout;
677 ds = (void *)child->thread.ds_area_msr;
680 if (cfg.flags & PTRACE_BTS_O_SIGNAL)
681 ret = ds_set_overflow(ds, DS_O_SIGNAL);
682 else
683 ret = ds_set_overflow(ds, DS_O_WRAP);
684 if (ret < 0)
685 goto errout;
687 if (cfg.flags & PTRACE_BTS_O_TRACE)
688 child->thread.debugctlmsr |= ds_debugctl_mask();
689 else
690 child->thread.debugctlmsr &= ~ds_debugctl_mask();
692 if (cfg.flags & PTRACE_BTS_O_SCHED)
693 set_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
694 else
695 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
697 ret = sizeof(cfg);
699 out:
700 if (child->thread.debugctlmsr)
701 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
702 else
703 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
705 return ret;
707 errout:
708 child->thread.debugctlmsr &= ~ds_debugctl_mask();
709 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
710 goto out;
713 static int ptrace_bts_status(struct task_struct *child,
714 long cfg_size,
715 struct ptrace_bts_config __user *ucfg)
717 void *ds = (void *)child->thread.ds_area_msr;
718 struct ptrace_bts_config cfg;
720 if (cfg_size < sizeof(cfg))
721 return -EIO;
723 memset(&cfg, 0, sizeof(cfg));
725 if (ds) {
726 cfg.size = ds_get_bts_size(ds);
728 if (ds_get_overflow(ds) == DS_O_SIGNAL)
729 cfg.flags |= PTRACE_BTS_O_SIGNAL;
731 if (test_tsk_thread_flag(child, TIF_DEBUGCTLMSR) &&
732 child->thread.debugctlmsr & ds_debugctl_mask())
733 cfg.flags |= PTRACE_BTS_O_TRACE;
735 if (test_tsk_thread_flag(child, TIF_BTS_TRACE_TS))
736 cfg.flags |= PTRACE_BTS_O_SCHED;
739 cfg.bts_size = sizeof(struct bts_struct);
741 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
742 return -EFAULT;
744 return sizeof(cfg);
748 static int ptrace_bts_write_record(struct task_struct *child,
749 const struct bts_struct *in)
751 int retval;
753 if (!child->thread.ds_area_msr)
754 return -ENXIO;
756 retval = ds_write_bts((void *)child->thread.ds_area_msr, in);
757 if (retval)
758 return retval;
760 return sizeof(*in);
763 static int ptrace_bts_realloc(struct task_struct *child,
764 int size, int reduce_size)
766 unsigned long rlim, vm;
767 int ret, old_size;
769 if (size < 0)
770 return -EINVAL;
772 old_size = ds_get_bts_size((void *)child->thread.ds_area_msr);
773 if (old_size < 0)
774 return old_size;
776 ret = ds_free((void **)&child->thread.ds_area_msr);
777 if (ret < 0)
778 goto out;
780 size >>= PAGE_SHIFT;
781 old_size >>= PAGE_SHIFT;
783 current->mm->total_vm -= old_size;
784 current->mm->locked_vm -= old_size;
786 if (size == 0)
787 goto out;
789 rlim = current->signal->rlim[RLIMIT_AS].rlim_cur >> PAGE_SHIFT;
790 vm = current->mm->total_vm + size;
791 if (rlim < vm) {
792 ret = -ENOMEM;
794 if (!reduce_size)
795 goto out;
797 size = rlim - current->mm->total_vm;
798 if (size <= 0)
799 goto out;
802 rlim = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >> PAGE_SHIFT;
803 vm = current->mm->locked_vm + size;
804 if (rlim < vm) {
805 ret = -ENOMEM;
807 if (!reduce_size)
808 goto out;
810 size = rlim - current->mm->locked_vm;
811 if (size <= 0)
812 goto out;
815 ret = ds_allocate((void **)&child->thread.ds_area_msr,
816 size << PAGE_SHIFT);
817 if (ret < 0)
818 goto out;
820 current->mm->total_vm += size;
821 current->mm->locked_vm += size;
823 out:
824 if (child->thread.ds_area_msr)
825 set_tsk_thread_flag(child, TIF_DS_AREA_MSR);
826 else
827 clear_tsk_thread_flag(child, TIF_DS_AREA_MSR);
829 return ret;
832 void ptrace_bts_take_timestamp(struct task_struct *tsk,
833 enum bts_qualifier qualifier)
835 struct bts_struct rec = {
836 .qualifier = qualifier,
837 .variant.jiffies = jiffies_64
840 ptrace_bts_write_record(tsk, &rec);
842 #endif /* X86_BTS */
845 * Called by kernel/ptrace.c when detaching..
847 * Make sure the single step bit is not set.
849 void ptrace_disable(struct task_struct *child)
851 user_disable_single_step(child);
852 #ifdef TIF_SYSCALL_EMU
853 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
854 #endif
855 if (child->thread.ds_area_msr) {
856 #ifdef X86_BTS
857 ptrace_bts_realloc(child, 0, 0);
858 #endif
859 child->thread.debugctlmsr &= ~ds_debugctl_mask();
860 if (!child->thread.debugctlmsr)
861 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
862 clear_tsk_thread_flag(child, TIF_BTS_TRACE_TS);
866 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
867 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
868 #endif
870 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
872 int ret;
873 unsigned long __user *datap = (unsigned long __user *)data;
875 switch (request) {
876 /* read the word at location addr in the USER area. */
877 case PTRACE_PEEKUSR: {
878 unsigned long tmp;
880 ret = -EIO;
881 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
882 addr >= sizeof(struct user))
883 break;
885 tmp = 0; /* Default return condition */
886 if (addr < sizeof(struct user_regs_struct))
887 tmp = getreg(child, addr);
888 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
889 addr <= offsetof(struct user, u_debugreg[7])) {
890 addr -= offsetof(struct user, u_debugreg[0]);
891 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
893 ret = put_user(tmp, datap);
894 break;
897 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
898 ret = -EIO;
899 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
900 addr >= sizeof(struct user))
901 break;
903 if (addr < sizeof(struct user_regs_struct))
904 ret = putreg(child, addr, data);
905 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
906 addr <= offsetof(struct user, u_debugreg[7])) {
907 addr -= offsetof(struct user, u_debugreg[0]);
908 ret = ptrace_set_debugreg(child,
909 addr / sizeof(data), data);
911 break;
913 case PTRACE_GETREGS: /* Get all gp regs from the child. */
914 return copy_regset_to_user(child,
915 task_user_regset_view(current),
916 REGSET_GENERAL,
917 0, sizeof(struct user_regs_struct),
918 datap);
920 case PTRACE_SETREGS: /* Set all gp regs in the child. */
921 return copy_regset_from_user(child,
922 task_user_regset_view(current),
923 REGSET_GENERAL,
924 0, sizeof(struct user_regs_struct),
925 datap);
927 case PTRACE_GETFPREGS: /* Get the child FPU state. */
928 return copy_regset_to_user(child,
929 task_user_regset_view(current),
930 REGSET_FP,
931 0, sizeof(struct user_i387_struct),
932 datap);
934 case PTRACE_SETFPREGS: /* Set the child FPU state. */
935 return copy_regset_from_user(child,
936 task_user_regset_view(current),
937 REGSET_FP,
938 0, sizeof(struct user_i387_struct),
939 datap);
941 #ifdef CONFIG_X86_32
942 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
943 return copy_regset_to_user(child, &user_x86_32_view,
944 REGSET_XFP,
945 0, sizeof(struct user_fxsr_struct),
946 datap);
948 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
949 return copy_regset_from_user(child, &user_x86_32_view,
950 REGSET_XFP,
951 0, sizeof(struct user_fxsr_struct),
952 datap);
953 #endif
955 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
956 case PTRACE_GET_THREAD_AREA:
957 if (addr < 0)
958 return -EIO;
959 ret = do_get_thread_area(child, addr,
960 (struct user_desc __user *) data);
961 break;
963 case PTRACE_SET_THREAD_AREA:
964 if (addr < 0)
965 return -EIO;
966 ret = do_set_thread_area(child, addr,
967 (struct user_desc __user *) data, 0);
968 break;
969 #endif
971 #ifdef CONFIG_X86_64
972 /* normal 64bit interface to access TLS data.
973 Works just like arch_prctl, except that the arguments
974 are reversed. */
975 case PTRACE_ARCH_PRCTL:
976 ret = do_arch_prctl(child, data, addr);
977 break;
978 #endif
981 * These bits need more cooking - not enabled yet:
983 #ifdef X86_BTS
984 case PTRACE_BTS_CONFIG:
985 ret = ptrace_bts_config
986 (child, data, (struct ptrace_bts_config __user *)addr);
987 break;
989 case PTRACE_BTS_STATUS:
990 ret = ptrace_bts_status
991 (child, data, (struct ptrace_bts_config __user *)addr);
992 break;
994 case PTRACE_BTS_SIZE:
995 ret = ptrace_bts_get_size(child);
996 break;
998 case PTRACE_BTS_GET:
999 ret = ptrace_bts_read_record
1000 (child, data, (struct bts_struct __user *) addr);
1001 break;
1003 case PTRACE_BTS_CLEAR:
1004 ret = ptrace_bts_clear(child);
1005 break;
1007 case PTRACE_BTS_DRAIN:
1008 ret = ptrace_bts_drain
1009 (child, data, (struct bts_struct __user *) addr);
1010 break;
1011 #endif
1013 default:
1014 ret = ptrace_request(child, request, addr, data);
1015 break;
1018 return ret;
1021 #ifdef CONFIG_IA32_EMULATION
1023 #include <linux/compat.h>
1024 #include <linux/syscalls.h>
1025 #include <asm/ia32.h>
1026 #include <asm/user32.h>
1028 #define R32(l,q) \
1029 case offsetof(struct user32, regs.l): \
1030 regs->q = value; break
1032 #define SEG32(rs) \
1033 case offsetof(struct user32, regs.rs): \
1034 return set_segment_reg(child, \
1035 offsetof(struct user_regs_struct, rs), \
1036 value); \
1037 break
1039 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1041 struct pt_regs *regs = task_pt_regs(child);
1043 switch (regno) {
1045 SEG32(cs);
1046 SEG32(ds);
1047 SEG32(es);
1048 SEG32(fs);
1049 SEG32(gs);
1050 SEG32(ss);
1052 R32(ebx, bx);
1053 R32(ecx, cx);
1054 R32(edx, dx);
1055 R32(edi, di);
1056 R32(esi, si);
1057 R32(ebp, bp);
1058 R32(eax, ax);
1059 R32(eip, ip);
1060 R32(esp, sp);
1062 case offsetof(struct user32, regs.orig_eax):
1064 * Sign-extend the value so that orig_eax = -1
1065 * causes (long)orig_ax < 0 tests to fire correctly.
1067 regs->orig_ax = (long) (s32) value;
1068 break;
1070 case offsetof(struct user32, regs.eflags):
1071 return set_flags(child, value);
1073 case offsetof(struct user32, u_debugreg[0]) ...
1074 offsetof(struct user32, u_debugreg[7]):
1075 regno -= offsetof(struct user32, u_debugreg[0]);
1076 return ptrace_set_debugreg(child, regno / 4, value);
1078 default:
1079 if (regno > sizeof(struct user32) || (regno & 3))
1080 return -EIO;
1083 * Other dummy fields in the virtual user structure
1084 * are ignored
1086 break;
1088 return 0;
1091 #undef R32
1092 #undef SEG32
1094 #define R32(l,q) \
1095 case offsetof(struct user32, regs.l): \
1096 *val = regs->q; break
1098 #define SEG32(rs) \
1099 case offsetof(struct user32, regs.rs): \
1100 *val = get_segment_reg(child, \
1101 offsetof(struct user_regs_struct, rs)); \
1102 break
1104 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1106 struct pt_regs *regs = task_pt_regs(child);
1108 switch (regno) {
1110 SEG32(ds);
1111 SEG32(es);
1112 SEG32(fs);
1113 SEG32(gs);
1115 R32(cs, cs);
1116 R32(ss, ss);
1117 R32(ebx, bx);
1118 R32(ecx, cx);
1119 R32(edx, dx);
1120 R32(edi, di);
1121 R32(esi, si);
1122 R32(ebp, bp);
1123 R32(eax, ax);
1124 R32(orig_eax, orig_ax);
1125 R32(eip, ip);
1126 R32(esp, sp);
1128 case offsetof(struct user32, regs.eflags):
1129 *val = get_flags(child);
1130 break;
1132 case offsetof(struct user32, u_debugreg[0]) ...
1133 offsetof(struct user32, u_debugreg[7]):
1134 regno -= offsetof(struct user32, u_debugreg[0]);
1135 *val = ptrace_get_debugreg(child, regno / 4);
1136 break;
1138 default:
1139 if (regno > sizeof(struct user32) || (regno & 3))
1140 return -EIO;
1143 * Other dummy fields in the virtual user structure
1144 * are ignored
1146 *val = 0;
1147 break;
1149 return 0;
1152 #undef R32
1153 #undef SEG32
1155 static int genregs32_get(struct task_struct *target,
1156 const struct user_regset *regset,
1157 unsigned int pos, unsigned int count,
1158 void *kbuf, void __user *ubuf)
1160 if (kbuf) {
1161 compat_ulong_t *k = kbuf;
1162 while (count > 0) {
1163 getreg32(target, pos, k++);
1164 count -= sizeof(*k);
1165 pos += sizeof(*k);
1167 } else {
1168 compat_ulong_t __user *u = ubuf;
1169 while (count > 0) {
1170 compat_ulong_t word;
1171 getreg32(target, pos, &word);
1172 if (__put_user(word, u++))
1173 return -EFAULT;
1174 count -= sizeof(*u);
1175 pos += sizeof(*u);
1179 return 0;
1182 static int genregs32_set(struct task_struct *target,
1183 const struct user_regset *regset,
1184 unsigned int pos, unsigned int count,
1185 const void *kbuf, const void __user *ubuf)
1187 int ret = 0;
1188 if (kbuf) {
1189 const compat_ulong_t *k = kbuf;
1190 while (count > 0 && !ret) {
1191 ret = putreg32(target, pos, *k++);
1192 count -= sizeof(*k);
1193 pos += sizeof(*k);
1195 } else {
1196 const compat_ulong_t __user *u = ubuf;
1197 while (count > 0 && !ret) {
1198 compat_ulong_t word;
1199 ret = __get_user(word, u++);
1200 if (ret)
1201 break;
1202 ret = putreg32(target, pos, word);
1203 count -= sizeof(*u);
1204 pos += sizeof(*u);
1207 return ret;
1210 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1211 compat_ulong_t caddr, compat_ulong_t cdata)
1213 unsigned long addr = caddr;
1214 unsigned long data = cdata;
1215 void __user *datap = compat_ptr(data);
1216 int ret;
1217 __u32 val;
1219 switch (request) {
1220 case PTRACE_PEEKUSR:
1221 ret = getreg32(child, addr, &val);
1222 if (ret == 0)
1223 ret = put_user(val, (__u32 __user *)datap);
1224 break;
1226 case PTRACE_POKEUSR:
1227 ret = putreg32(child, addr, data);
1228 break;
1230 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1231 return copy_regset_to_user(child, &user_x86_32_view,
1232 REGSET_GENERAL,
1233 0, sizeof(struct user_regs_struct32),
1234 datap);
1236 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1237 return copy_regset_from_user(child, &user_x86_32_view,
1238 REGSET_GENERAL, 0,
1239 sizeof(struct user_regs_struct32),
1240 datap);
1242 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1243 return copy_regset_to_user(child, &user_x86_32_view,
1244 REGSET_FP, 0,
1245 sizeof(struct user_i387_ia32_struct),
1246 datap);
1248 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1249 return copy_regset_from_user(
1250 child, &user_x86_32_view, REGSET_FP,
1251 0, sizeof(struct user_i387_ia32_struct), datap);
1253 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1254 return copy_regset_to_user(child, &user_x86_32_view,
1255 REGSET_XFP, 0,
1256 sizeof(struct user32_fxsr_struct),
1257 datap);
1259 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1260 return copy_regset_from_user(child, &user_x86_32_view,
1261 REGSET_XFP, 0,
1262 sizeof(struct user32_fxsr_struct),
1263 datap);
1265 case PTRACE_GET_THREAD_AREA:
1266 case PTRACE_SET_THREAD_AREA:
1267 return arch_ptrace(child, request, addr, data);
1269 default:
1270 return compat_ptrace_request(child, request, addr, data);
1273 return ret;
1276 #endif /* CONFIG_IA32_EMULATION */
1278 #ifdef CONFIG_X86_64
1280 static const struct user_regset x86_64_regsets[] = {
1281 [REGSET_GENERAL] = {
1282 .core_note_type = NT_PRSTATUS,
1283 .n = sizeof(struct user_regs_struct) / sizeof(long),
1284 .size = sizeof(long), .align = sizeof(long),
1285 .get = genregs_get, .set = genregs_set
1287 [REGSET_FP] = {
1288 .core_note_type = NT_PRFPREG,
1289 .n = sizeof(struct user_i387_struct) / sizeof(long),
1290 .size = sizeof(long), .align = sizeof(long),
1291 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1295 static const struct user_regset_view user_x86_64_view = {
1296 .name = "x86_64", .e_machine = EM_X86_64,
1297 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1300 #else /* CONFIG_X86_32 */
1302 #define user_regs_struct32 user_regs_struct
1303 #define genregs32_get genregs_get
1304 #define genregs32_set genregs_set
1306 #define user_i387_ia32_struct user_i387_struct
1307 #define user32_fxsr_struct user_fxsr_struct
1309 #endif /* CONFIG_X86_64 */
1311 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1312 static const struct user_regset x86_32_regsets[] = {
1313 [REGSET_GENERAL] = {
1314 .core_note_type = NT_PRSTATUS,
1315 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1316 .size = sizeof(u32), .align = sizeof(u32),
1317 .get = genregs32_get, .set = genregs32_set
1319 [REGSET_FP] = {
1320 .core_note_type = NT_PRFPREG,
1321 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1322 .size = sizeof(u32), .align = sizeof(u32),
1323 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1325 [REGSET_XFP] = {
1326 .core_note_type = NT_PRXFPREG,
1327 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1328 .size = sizeof(u32), .align = sizeof(u32),
1329 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1331 [REGSET_TLS] = {
1332 .core_note_type = NT_386_TLS,
1333 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1334 .size = sizeof(struct user_desc),
1335 .align = sizeof(struct user_desc),
1336 .active = regset_tls_active,
1337 .get = regset_tls_get, .set = regset_tls_set
1341 static const struct user_regset_view user_x86_32_view = {
1342 .name = "i386", .e_machine = EM_386,
1343 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1345 #endif
1347 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1349 #ifdef CONFIG_IA32_EMULATION
1350 if (test_tsk_thread_flag(task, TIF_IA32))
1351 #endif
1352 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1353 return &user_x86_32_view;
1354 #endif
1355 #ifdef CONFIG_X86_64
1356 return &user_x86_64_view;
1357 #endif
1360 #ifdef CONFIG_X86_32
1362 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs, int error_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 = TRAP_BRKPT;
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);
1380 /* notification of system call entry/exit
1381 * - triggered by current->work.syscall_trace
1383 int do_syscall_trace(struct pt_regs *regs, int entryexit)
1385 int is_sysemu = test_thread_flag(TIF_SYSCALL_EMU);
1387 * With TIF_SYSCALL_EMU set we want to ignore TIF_SINGLESTEP for syscall
1388 * interception
1390 int is_singlestep = !is_sysemu && test_thread_flag(TIF_SINGLESTEP);
1391 int ret = 0;
1393 /* do the secure computing check first */
1394 if (!entryexit)
1395 secure_computing(regs->orig_ax);
1397 if (unlikely(current->audit_context)) {
1398 if (entryexit)
1399 audit_syscall_exit(AUDITSC_RESULT(regs->ax),
1400 regs->ax);
1401 /* Debug traps, when using PTRACE_SINGLESTEP, must be sent only
1402 * on the syscall exit path. Normally, when TIF_SYSCALL_AUDIT is
1403 * not used, entry.S will call us only on syscall exit, not
1404 * entry; so when TIF_SYSCALL_AUDIT is used we must avoid
1405 * calling send_sigtrap() on syscall entry.
1407 * Note that when PTRACE_SYSEMU_SINGLESTEP is used,
1408 * is_singlestep is false, despite his name, so we will still do
1409 * the correct thing.
1411 else if (is_singlestep)
1412 goto out;
1415 if (!(current->ptrace & PT_PTRACED))
1416 goto out;
1418 /* If a process stops on the 1st tracepoint with SYSCALL_TRACE
1419 * and then is resumed with SYSEMU_SINGLESTEP, it will come in
1420 * here. We have to check this and return */
1421 if (is_sysemu && entryexit)
1422 return 0;
1424 /* Fake a debug trap */
1425 if (is_singlestep)
1426 send_sigtrap(current, regs, 0);
1428 if (!test_thread_flag(TIF_SYSCALL_TRACE) && !is_sysemu)
1429 goto out;
1431 /* the 0x80 provides a way for the tracing parent to distinguish
1432 between a syscall stop and SIGTRAP delivery */
1433 /* Note that the debugger could change the result of test_thread_flag!*/
1434 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80:0));
1437 * this isn't the same as continuing with a signal, but it will do
1438 * for normal use. strace only continues with a signal if the
1439 * stopping signal is not SIGTRAP. -brl
1441 if (current->exit_code) {
1442 send_sig(current->exit_code, current, 1);
1443 current->exit_code = 0;
1445 ret = is_sysemu;
1446 out:
1447 if (unlikely(current->audit_context) && !entryexit)
1448 audit_syscall_entry(AUDIT_ARCH_I386, regs->orig_ax,
1449 regs->bx, regs->cx, regs->dx, regs->si);
1450 if (ret == 0)
1451 return 0;
1453 regs->orig_ax = -1; /* force skip of syscall restarting */
1454 if (unlikely(current->audit_context))
1455 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1456 return 1;
1459 #else /* CONFIG_X86_64 */
1461 static void syscall_trace(struct pt_regs *regs)
1464 #if 0
1465 printk("trace %s ip %lx sp %lx ax %d origrax %d caller %lx tiflags %x ptrace %x\n",
1466 current->comm,
1467 regs->ip, regs->sp, regs->ax, regs->orig_ax, __builtin_return_address(0),
1468 current_thread_info()->flags, current->ptrace);
1469 #endif
1471 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1472 ? 0x80 : 0));
1474 * this isn't the same as continuing with a signal, but it will do
1475 * for normal use. strace only continues with a signal if the
1476 * stopping signal is not SIGTRAP. -brl
1478 if (current->exit_code) {
1479 send_sig(current->exit_code, current, 1);
1480 current->exit_code = 0;
1484 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
1486 /* do the secure computing check first */
1487 secure_computing(regs->orig_ax);
1489 if (test_thread_flag(TIF_SYSCALL_TRACE)
1490 && (current->ptrace & PT_PTRACED))
1491 syscall_trace(regs);
1493 if (unlikely(current->audit_context)) {
1494 if (test_thread_flag(TIF_IA32)) {
1495 audit_syscall_entry(AUDIT_ARCH_I386,
1496 regs->orig_ax,
1497 regs->bx, regs->cx,
1498 regs->dx, regs->si);
1499 } else {
1500 audit_syscall_entry(AUDIT_ARCH_X86_64,
1501 regs->orig_ax,
1502 regs->di, regs->si,
1503 regs->dx, regs->r10);
1508 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1510 if (unlikely(current->audit_context))
1511 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1513 if ((test_thread_flag(TIF_SYSCALL_TRACE)
1514 || test_thread_flag(TIF_SINGLESTEP))
1515 && (current->ptrace & PT_PTRACED))
1516 syscall_trace(regs);
1519 #endif /* CONFIG_X86_32 */