Merge commit 'v2.6.33' into perf/core
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / ptrace.c
blobd03146f71b2f21becd387c5fe7297c8318b5f584
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>
24 #include <linux/workqueue.h>
25 #include <linux/perf_event.h>
26 #include <linux/hw_breakpoint.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
32 #include <asm/i387.h>
33 #include <asm/debugreg.h>
34 #include <asm/ldt.h>
35 #include <asm/desc.h>
36 #include <asm/prctl.h>
37 #include <asm/proto.h>
38 #include <asm/ds.h>
39 #include <asm/hw_breakpoint.h>
41 #include "tls.h"
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
46 enum x86_regset {
47 REGSET_GENERAL,
48 REGSET_FP,
49 REGSET_XFP,
50 REGSET_IOPERM64 = REGSET_XFP,
51 REGSET_TLS,
52 REGSET_IOPERM32,
55 struct pt_regs_offset {
56 const char *name;
57 int offset;
60 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
61 #define REG_OFFSET_END {.name = NULL, .offset = 0}
63 static const struct pt_regs_offset regoffset_table[] = {
64 #ifdef CONFIG_X86_64
65 REG_OFFSET_NAME(r15),
66 REG_OFFSET_NAME(r14),
67 REG_OFFSET_NAME(r13),
68 REG_OFFSET_NAME(r12),
69 REG_OFFSET_NAME(r11),
70 REG_OFFSET_NAME(r10),
71 REG_OFFSET_NAME(r9),
72 REG_OFFSET_NAME(r8),
73 #endif
74 REG_OFFSET_NAME(bx),
75 REG_OFFSET_NAME(cx),
76 REG_OFFSET_NAME(dx),
77 REG_OFFSET_NAME(si),
78 REG_OFFSET_NAME(di),
79 REG_OFFSET_NAME(bp),
80 REG_OFFSET_NAME(ax),
81 #ifdef CONFIG_X86_32
82 REG_OFFSET_NAME(ds),
83 REG_OFFSET_NAME(es),
84 REG_OFFSET_NAME(fs),
85 REG_OFFSET_NAME(gs),
86 #endif
87 REG_OFFSET_NAME(orig_ax),
88 REG_OFFSET_NAME(ip),
89 REG_OFFSET_NAME(cs),
90 REG_OFFSET_NAME(flags),
91 REG_OFFSET_NAME(sp),
92 REG_OFFSET_NAME(ss),
93 REG_OFFSET_END,
96 /**
97 * regs_query_register_offset() - query register offset from its name
98 * @name: the name of a register
100 * regs_query_register_offset() returns the offset of a register in struct
101 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
103 int regs_query_register_offset(const char *name)
105 const struct pt_regs_offset *roff;
106 for (roff = regoffset_table; roff->name != NULL; roff++)
107 if (!strcmp(roff->name, name))
108 return roff->offset;
109 return -EINVAL;
113 * regs_query_register_name() - query register name from its offset
114 * @offset: the offset of a register in struct pt_regs.
116 * regs_query_register_name() returns the name of a register from its
117 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
119 const char *regs_query_register_name(unsigned int offset)
121 const struct pt_regs_offset *roff;
122 for (roff = regoffset_table; roff->name != NULL; roff++)
123 if (roff->offset == offset)
124 return roff->name;
125 return NULL;
128 static const int arg_offs_table[] = {
129 #ifdef CONFIG_X86_32
130 [0] = offsetof(struct pt_regs, ax),
131 [1] = offsetof(struct pt_regs, dx),
132 [2] = offsetof(struct pt_regs, cx)
133 #else /* CONFIG_X86_64 */
134 [0] = offsetof(struct pt_regs, di),
135 [1] = offsetof(struct pt_regs, si),
136 [2] = offsetof(struct pt_regs, dx),
137 [3] = offsetof(struct pt_regs, cx),
138 [4] = offsetof(struct pt_regs, r8),
139 [5] = offsetof(struct pt_regs, r9)
140 #endif
144 * does not yet catch signals sent when the child dies.
145 * in exit.c or in signal.c.
149 * Determines which flags the user has access to [1 = access, 0 = no access].
151 #define FLAG_MASK_32 ((unsigned long) \
152 (X86_EFLAGS_CF | X86_EFLAGS_PF | \
153 X86_EFLAGS_AF | X86_EFLAGS_ZF | \
154 X86_EFLAGS_SF | X86_EFLAGS_TF | \
155 X86_EFLAGS_DF | X86_EFLAGS_OF | \
156 X86_EFLAGS_RF | X86_EFLAGS_AC))
159 * Determines whether a value may be installed in a segment register.
161 static inline bool invalid_selector(u16 value)
163 return unlikely(value != 0 && (value & SEGMENT_RPL_MASK) != USER_RPL);
166 #ifdef CONFIG_X86_32
168 #define FLAG_MASK FLAG_MASK_32
170 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long regno)
172 BUILD_BUG_ON(offsetof(struct pt_regs, bx) != 0);
173 return &regs->bx + (regno >> 2);
176 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
179 * Returning the value truncates it to 16 bits.
181 unsigned int retval;
182 if (offset != offsetof(struct user_regs_struct, gs))
183 retval = *pt_regs_access(task_pt_regs(task), offset);
184 else {
185 if (task == current)
186 retval = get_user_gs(task_pt_regs(task));
187 else
188 retval = task_user_gs(task);
190 return retval;
193 static int set_segment_reg(struct task_struct *task,
194 unsigned long offset, u16 value)
197 * The value argument was already truncated to 16 bits.
199 if (invalid_selector(value))
200 return -EIO;
203 * For %cs and %ss we cannot permit a null selector.
204 * We can permit a bogus selector as long as it has USER_RPL.
205 * Null selectors are fine for other segment registers, but
206 * we will never get back to user mode with invalid %cs or %ss
207 * and will take the trap in iret instead. Much code relies
208 * on user_mode() to distinguish a user trap frame (which can
209 * safely use invalid selectors) from a kernel trap frame.
211 switch (offset) {
212 case offsetof(struct user_regs_struct, cs):
213 case offsetof(struct user_regs_struct, ss):
214 if (unlikely(value == 0))
215 return -EIO;
217 default:
218 *pt_regs_access(task_pt_regs(task), offset) = value;
219 break;
221 case offsetof(struct user_regs_struct, gs):
222 if (task == current)
223 set_user_gs(task_pt_regs(task), value);
224 else
225 task_user_gs(task) = value;
228 return 0;
231 #else /* CONFIG_X86_64 */
233 #define FLAG_MASK (FLAG_MASK_32 | X86_EFLAGS_NT)
235 static unsigned long *pt_regs_access(struct pt_regs *regs, unsigned long offset)
237 BUILD_BUG_ON(offsetof(struct pt_regs, r15) != 0);
238 return &regs->r15 + (offset / sizeof(regs->r15));
241 static u16 get_segment_reg(struct task_struct *task, unsigned long offset)
244 * Returning the value truncates it to 16 bits.
246 unsigned int seg;
248 switch (offset) {
249 case offsetof(struct user_regs_struct, fs):
250 if (task == current) {
251 /* Older gas can't assemble movq %?s,%r?? */
252 asm("movl %%fs,%0" : "=r" (seg));
253 return seg;
255 return task->thread.fsindex;
256 case offsetof(struct user_regs_struct, gs):
257 if (task == current) {
258 asm("movl %%gs,%0" : "=r" (seg));
259 return seg;
261 return task->thread.gsindex;
262 case offsetof(struct user_regs_struct, ds):
263 if (task == current) {
264 asm("movl %%ds,%0" : "=r" (seg));
265 return seg;
267 return task->thread.ds;
268 case offsetof(struct user_regs_struct, es):
269 if (task == current) {
270 asm("movl %%es,%0" : "=r" (seg));
271 return seg;
273 return task->thread.es;
275 case offsetof(struct user_regs_struct, cs):
276 case offsetof(struct user_regs_struct, ss):
277 break;
279 return *pt_regs_access(task_pt_regs(task), offset);
282 static int set_segment_reg(struct task_struct *task,
283 unsigned long offset, u16 value)
286 * The value argument was already truncated to 16 bits.
288 if (invalid_selector(value))
289 return -EIO;
291 switch (offset) {
292 case offsetof(struct user_regs_struct,fs):
294 * If this is setting fs as for normal 64-bit use but
295 * setting fs_base has implicitly changed it, leave it.
297 if ((value == FS_TLS_SEL && task->thread.fsindex == 0 &&
298 task->thread.fs != 0) ||
299 (value == 0 && task->thread.fsindex == FS_TLS_SEL &&
300 task->thread.fs == 0))
301 break;
302 task->thread.fsindex = value;
303 if (task == current)
304 loadsegment(fs, task->thread.fsindex);
305 break;
306 case offsetof(struct user_regs_struct,gs):
308 * If this is setting gs as for normal 64-bit use but
309 * setting gs_base has implicitly changed it, leave it.
311 if ((value == GS_TLS_SEL && task->thread.gsindex == 0 &&
312 task->thread.gs != 0) ||
313 (value == 0 && task->thread.gsindex == GS_TLS_SEL &&
314 task->thread.gs == 0))
315 break;
316 task->thread.gsindex = value;
317 if (task == current)
318 load_gs_index(task->thread.gsindex);
319 break;
320 case offsetof(struct user_regs_struct,ds):
321 task->thread.ds = value;
322 if (task == current)
323 loadsegment(ds, task->thread.ds);
324 break;
325 case offsetof(struct user_regs_struct,es):
326 task->thread.es = value;
327 if (task == current)
328 loadsegment(es, task->thread.es);
329 break;
332 * Can't actually change these in 64-bit mode.
334 case offsetof(struct user_regs_struct,cs):
335 if (unlikely(value == 0))
336 return -EIO;
337 #ifdef CONFIG_IA32_EMULATION
338 if (test_tsk_thread_flag(task, TIF_IA32))
339 task_pt_regs(task)->cs = value;
340 #endif
341 break;
342 case offsetof(struct user_regs_struct,ss):
343 if (unlikely(value == 0))
344 return -EIO;
345 #ifdef CONFIG_IA32_EMULATION
346 if (test_tsk_thread_flag(task, TIF_IA32))
347 task_pt_regs(task)->ss = value;
348 #endif
349 break;
352 return 0;
355 #endif /* CONFIG_X86_32 */
357 static unsigned long get_flags(struct task_struct *task)
359 unsigned long retval = task_pt_regs(task)->flags;
362 * If the debugger set TF, hide it from the readout.
364 if (test_tsk_thread_flag(task, TIF_FORCED_TF))
365 retval &= ~X86_EFLAGS_TF;
367 return retval;
370 static int set_flags(struct task_struct *task, unsigned long value)
372 struct pt_regs *regs = task_pt_regs(task);
375 * If the user value contains TF, mark that
376 * it was not "us" (the debugger) that set it.
377 * If not, make sure it stays set if we had.
379 if (value & X86_EFLAGS_TF)
380 clear_tsk_thread_flag(task, TIF_FORCED_TF);
381 else if (test_tsk_thread_flag(task, TIF_FORCED_TF))
382 value |= X86_EFLAGS_TF;
384 regs->flags = (regs->flags & ~FLAG_MASK) | (value & FLAG_MASK);
386 return 0;
389 static int putreg(struct task_struct *child,
390 unsigned long offset, unsigned long value)
392 switch (offset) {
393 case offsetof(struct user_regs_struct, cs):
394 case offsetof(struct user_regs_struct, ds):
395 case offsetof(struct user_regs_struct, es):
396 case offsetof(struct user_regs_struct, fs):
397 case offsetof(struct user_regs_struct, gs):
398 case offsetof(struct user_regs_struct, ss):
399 return set_segment_reg(child, offset, value);
401 case offsetof(struct user_regs_struct, flags):
402 return set_flags(child, value);
404 #ifdef CONFIG_X86_64
405 case offsetof(struct user_regs_struct,fs_base):
406 if (value >= TASK_SIZE_OF(child))
407 return -EIO;
409 * When changing the segment base, use do_arch_prctl
410 * to set either thread.fs or thread.fsindex and the
411 * corresponding GDT slot.
413 if (child->thread.fs != value)
414 return do_arch_prctl(child, ARCH_SET_FS, value);
415 return 0;
416 case offsetof(struct user_regs_struct,gs_base):
418 * Exactly the same here as the %fs handling above.
420 if (value >= TASK_SIZE_OF(child))
421 return -EIO;
422 if (child->thread.gs != value)
423 return do_arch_prctl(child, ARCH_SET_GS, value);
424 return 0;
425 #endif
428 *pt_regs_access(task_pt_regs(child), offset) = value;
429 return 0;
432 static unsigned long getreg(struct task_struct *task, unsigned long offset)
434 switch (offset) {
435 case offsetof(struct user_regs_struct, cs):
436 case offsetof(struct user_regs_struct, ds):
437 case offsetof(struct user_regs_struct, es):
438 case offsetof(struct user_regs_struct, fs):
439 case offsetof(struct user_regs_struct, gs):
440 case offsetof(struct user_regs_struct, ss):
441 return get_segment_reg(task, offset);
443 case offsetof(struct user_regs_struct, flags):
444 return get_flags(task);
446 #ifdef CONFIG_X86_64
447 case offsetof(struct user_regs_struct, fs_base): {
449 * do_arch_prctl may have used a GDT slot instead of
450 * the MSR. To userland, it appears the same either
451 * way, except the %fs segment selector might not be 0.
453 unsigned int seg = task->thread.fsindex;
454 if (task->thread.fs != 0)
455 return task->thread.fs;
456 if (task == current)
457 asm("movl %%fs,%0" : "=r" (seg));
458 if (seg != FS_TLS_SEL)
459 return 0;
460 return get_desc_base(&task->thread.tls_array[FS_TLS]);
462 case offsetof(struct user_regs_struct, gs_base): {
464 * Exactly the same here as the %fs handling above.
466 unsigned int seg = task->thread.gsindex;
467 if (task->thread.gs != 0)
468 return task->thread.gs;
469 if (task == current)
470 asm("movl %%gs,%0" : "=r" (seg));
471 if (seg != GS_TLS_SEL)
472 return 0;
473 return get_desc_base(&task->thread.tls_array[GS_TLS]);
475 #endif
478 return *pt_regs_access(task_pt_regs(task), offset);
481 static int genregs_get(struct task_struct *target,
482 const struct user_regset *regset,
483 unsigned int pos, unsigned int count,
484 void *kbuf, void __user *ubuf)
486 if (kbuf) {
487 unsigned long *k = kbuf;
488 while (count >= sizeof(*k)) {
489 *k++ = getreg(target, pos);
490 count -= sizeof(*k);
491 pos += sizeof(*k);
493 } else {
494 unsigned long __user *u = ubuf;
495 while (count >= sizeof(*u)) {
496 if (__put_user(getreg(target, pos), u++))
497 return -EFAULT;
498 count -= sizeof(*u);
499 pos += sizeof(*u);
503 return 0;
506 static int genregs_set(struct task_struct *target,
507 const struct user_regset *regset,
508 unsigned int pos, unsigned int count,
509 const void *kbuf, const void __user *ubuf)
511 int ret = 0;
512 if (kbuf) {
513 const unsigned long *k = kbuf;
514 while (count >= sizeof(*k) && !ret) {
515 ret = putreg(target, pos, *k++);
516 count -= sizeof(*k);
517 pos += sizeof(*k);
519 } else {
520 const unsigned long __user *u = ubuf;
521 while (count >= sizeof(*u) && !ret) {
522 unsigned long word;
523 ret = __get_user(word, u++);
524 if (ret)
525 break;
526 ret = putreg(target, pos, word);
527 count -= sizeof(*u);
528 pos += sizeof(*u);
531 return ret;
534 static void ptrace_triggered(struct perf_event *bp, int nmi,
535 struct perf_sample_data *data,
536 struct pt_regs *regs)
538 int i;
539 struct thread_struct *thread = &(current->thread);
542 * Store in the virtual DR6 register the fact that the breakpoint
543 * was hit so the thread's debugger will see it.
545 for (i = 0; i < HBP_NUM; i++) {
546 if (thread->ptrace_bps[i] == bp)
547 break;
550 thread->debugreg6 |= (DR_TRAP0 << i);
554 * Walk through every ptrace breakpoints for this thread and
555 * build the dr7 value on top of their attributes.
558 static unsigned long ptrace_get_dr7(struct perf_event *bp[])
560 int i;
561 int dr7 = 0;
562 struct arch_hw_breakpoint *info;
564 for (i = 0; i < HBP_NUM; i++) {
565 if (bp[i] && !bp[i]->attr.disabled) {
566 info = counter_arch_bp(bp[i]);
567 dr7 |= encode_dr7(i, info->len, info->type);
571 return dr7;
574 static int
575 ptrace_modify_breakpoint(struct perf_event *bp, int len, int type,
576 struct task_struct *tsk, int disabled)
578 int err;
579 int gen_len, gen_type;
580 struct perf_event_attr attr;
583 * We shoud have at least an inactive breakpoint at this
584 * slot. It means the user is writing dr7 without having
585 * written the address register first
587 if (!bp)
588 return -EINVAL;
590 err = arch_bp_generic_fields(len, type, &gen_len, &gen_type);
591 if (err)
592 return err;
594 attr = bp->attr;
595 attr.bp_len = gen_len;
596 attr.bp_type = gen_type;
597 attr.disabled = disabled;
599 return modify_user_hw_breakpoint(bp, &attr);
603 * Handle ptrace writes to debug register 7.
605 static int ptrace_write_dr7(struct task_struct *tsk, unsigned long data)
607 struct thread_struct *thread = &(tsk->thread);
608 unsigned long old_dr7;
609 int i, orig_ret = 0, rc = 0;
610 int enabled, second_pass = 0;
611 unsigned len, type;
612 struct perf_event *bp;
614 data &= ~DR_CONTROL_RESERVED;
615 old_dr7 = ptrace_get_dr7(thread->ptrace_bps);
616 restore:
618 * Loop through all the hardware breakpoints, making the
619 * appropriate changes to each.
621 for (i = 0; i < HBP_NUM; i++) {
622 enabled = decode_dr7(data, i, &len, &type);
623 bp = thread->ptrace_bps[i];
625 if (!enabled) {
626 if (bp) {
628 * Don't unregister the breakpoints right-away,
629 * unless all register_user_hw_breakpoint()
630 * requests have succeeded. This prevents
631 * any window of opportunity for debug
632 * register grabbing by other users.
634 if (!second_pass)
635 continue;
637 rc = ptrace_modify_breakpoint(bp, len, type,
638 tsk, 1);
639 if (rc)
640 break;
642 continue;
645 rc = ptrace_modify_breakpoint(bp, len, type, tsk, 0);
646 if (rc)
647 break;
650 * Make a second pass to free the remaining unused breakpoints
651 * or to restore the original breakpoints if an error occurred.
653 if (!second_pass) {
654 second_pass = 1;
655 if (rc < 0) {
656 orig_ret = rc;
657 data = old_dr7;
659 goto restore;
661 return ((orig_ret < 0) ? orig_ret : rc);
665 * Handle PTRACE_PEEKUSR calls for the debug register area.
667 static unsigned long ptrace_get_debugreg(struct task_struct *tsk, int n)
669 struct thread_struct *thread = &(tsk->thread);
670 unsigned long val = 0;
672 if (n < HBP_NUM) {
673 struct perf_event *bp;
674 bp = thread->ptrace_bps[n];
675 if (!bp)
676 return 0;
677 val = bp->hw.info.address;
678 } else if (n == 6) {
679 val = thread->debugreg6;
680 } else if (n == 7) {
681 val = thread->ptrace_dr7;
683 return val;
686 static int ptrace_set_breakpoint_addr(struct task_struct *tsk, int nr,
687 unsigned long addr)
689 struct perf_event *bp;
690 struct thread_struct *t = &tsk->thread;
691 struct perf_event_attr attr;
693 if (!t->ptrace_bps[nr]) {
694 hw_breakpoint_init(&attr);
696 * Put stub len and type to register (reserve) an inactive but
697 * correct bp
699 attr.bp_addr = addr;
700 attr.bp_len = HW_BREAKPOINT_LEN_1;
701 attr.bp_type = HW_BREAKPOINT_W;
702 attr.disabled = 1;
704 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
707 * CHECKME: the previous code returned -EIO if the addr wasn't
708 * a valid task virtual addr. The new one will return -EINVAL in
709 * this case.
710 * -EINVAL may be what we want for in-kernel breakpoints users,
711 * but -EIO looks better for ptrace, since we refuse a register
712 * writing for the user. And anyway this is the previous
713 * behaviour.
715 if (IS_ERR(bp))
716 return PTR_ERR(bp);
718 t->ptrace_bps[nr] = bp;
719 } else {
720 int err;
722 bp = t->ptrace_bps[nr];
724 attr = bp->attr;
725 attr.bp_addr = addr;
726 err = modify_user_hw_breakpoint(bp, &attr);
727 if (err)
728 return err;
732 return 0;
736 * Handle PTRACE_POKEUSR calls for the debug register area.
738 int ptrace_set_debugreg(struct task_struct *tsk, int n, unsigned long val)
740 struct thread_struct *thread = &(tsk->thread);
741 int rc = 0;
743 /* There are no DR4 or DR5 registers */
744 if (n == 4 || n == 5)
745 return -EIO;
747 if (n == 6) {
748 thread->debugreg6 = val;
749 goto ret_path;
751 if (n < HBP_NUM) {
752 rc = ptrace_set_breakpoint_addr(tsk, n, val);
753 if (rc)
754 return rc;
756 /* All that's left is DR7 */
757 if (n == 7) {
758 rc = ptrace_write_dr7(tsk, val);
759 if (!rc)
760 thread->ptrace_dr7 = val;
763 ret_path:
764 return rc;
768 * These access the current or another (stopped) task's io permission
769 * bitmap for debugging or core dump.
771 static int ioperm_active(struct task_struct *target,
772 const struct user_regset *regset)
774 return target->thread.io_bitmap_max / regset->size;
777 static int ioperm_get(struct task_struct *target,
778 const struct user_regset *regset,
779 unsigned int pos, unsigned int count,
780 void *kbuf, void __user *ubuf)
782 if (!target->thread.io_bitmap_ptr)
783 return -ENXIO;
785 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
786 target->thread.io_bitmap_ptr,
787 0, IO_BITMAP_BYTES);
790 #ifdef CONFIG_X86_PTRACE_BTS
792 * A branch trace store context.
794 * Contexts may only be installed by ptrace_bts_config() and only for
795 * ptraced tasks.
797 * Contexts are destroyed when the tracee is detached from the tracer.
798 * The actual destruction work requires interrupts enabled, so the
799 * work is deferred and will be scheduled during __ptrace_unlink().
801 * Contexts hold an additional task_struct reference on the traced
802 * task, as well as a reference on the tracer's mm.
804 * Ptrace already holds a task_struct for the duration of ptrace operations,
805 * but since destruction is deferred, it may be executed after both
806 * tracer and tracee exited.
808 struct bts_context {
809 /* The branch trace handle. */
810 struct bts_tracer *tracer;
812 /* The buffer used to store the branch trace and its size. */
813 void *buffer;
814 unsigned int size;
816 /* The mm that paid for the above buffer. */
817 struct mm_struct *mm;
819 /* The task this context belongs to. */
820 struct task_struct *task;
822 /* The signal to send on a bts buffer overflow. */
823 unsigned int bts_ovfl_signal;
825 /* The work struct to destroy a context. */
826 struct work_struct work;
829 static int alloc_bts_buffer(struct bts_context *context, unsigned int size)
831 void *buffer = NULL;
832 int err = -ENOMEM;
834 err = account_locked_memory(current->mm, current->signal->rlim, size);
835 if (err < 0)
836 return err;
838 buffer = kzalloc(size, GFP_KERNEL);
839 if (!buffer)
840 goto out_refund;
842 context->buffer = buffer;
843 context->size = size;
844 context->mm = get_task_mm(current);
846 return 0;
848 out_refund:
849 refund_locked_memory(current->mm, size);
850 return err;
853 static inline void free_bts_buffer(struct bts_context *context)
855 if (!context->buffer)
856 return;
858 kfree(context->buffer);
859 context->buffer = NULL;
861 refund_locked_memory(context->mm, context->size);
862 context->size = 0;
864 mmput(context->mm);
865 context->mm = NULL;
868 static void free_bts_context_work(struct work_struct *w)
870 struct bts_context *context;
872 context = container_of(w, struct bts_context, work);
874 ds_release_bts(context->tracer);
875 put_task_struct(context->task);
876 free_bts_buffer(context);
877 kfree(context);
880 static inline void free_bts_context(struct bts_context *context)
882 INIT_WORK(&context->work, free_bts_context_work);
883 schedule_work(&context->work);
886 static inline struct bts_context *alloc_bts_context(struct task_struct *task)
888 struct bts_context *context = kzalloc(sizeof(*context), GFP_KERNEL);
889 if (context) {
890 context->task = task;
891 task->bts = context;
893 get_task_struct(task);
896 return context;
899 static int ptrace_bts_read_record(struct task_struct *child, size_t index,
900 struct bts_struct __user *out)
902 struct bts_context *context;
903 const struct bts_trace *trace;
904 struct bts_struct bts;
905 const unsigned char *at;
906 int error;
908 context = child->bts;
909 if (!context)
910 return -ESRCH;
912 trace = ds_read_bts(context->tracer);
913 if (!trace)
914 return -ESRCH;
916 at = trace->ds.top - ((index + 1) * trace->ds.size);
917 if ((void *)at < trace->ds.begin)
918 at += (trace->ds.n * trace->ds.size);
920 if (!trace->read)
921 return -EOPNOTSUPP;
923 error = trace->read(context->tracer, at, &bts);
924 if (error < 0)
925 return error;
927 if (copy_to_user(out, &bts, sizeof(bts)))
928 return -EFAULT;
930 return sizeof(bts);
933 static int ptrace_bts_drain(struct task_struct *child,
934 long size,
935 struct bts_struct __user *out)
937 struct bts_context *context;
938 const struct bts_trace *trace;
939 const unsigned char *at;
940 int error, drained = 0;
942 context = child->bts;
943 if (!context)
944 return -ESRCH;
946 trace = ds_read_bts(context->tracer);
947 if (!trace)
948 return -ESRCH;
950 if (!trace->read)
951 return -EOPNOTSUPP;
953 if (size < (trace->ds.top - trace->ds.begin))
954 return -EIO;
956 for (at = trace->ds.begin; (void *)at < trace->ds.top;
957 out++, drained++, at += trace->ds.size) {
958 struct bts_struct bts;
960 error = trace->read(context->tracer, at, &bts);
961 if (error < 0)
962 return error;
964 if (copy_to_user(out, &bts, sizeof(bts)))
965 return -EFAULT;
968 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
970 error = ds_reset_bts(context->tracer);
971 if (error < 0)
972 return error;
974 return drained;
977 static int ptrace_bts_config(struct task_struct *child,
978 long cfg_size,
979 const struct ptrace_bts_config __user *ucfg)
981 struct bts_context *context;
982 struct ptrace_bts_config cfg;
983 unsigned int flags = 0;
985 if (cfg_size < sizeof(cfg))
986 return -EIO;
988 if (copy_from_user(&cfg, ucfg, sizeof(cfg)))
989 return -EFAULT;
991 context = child->bts;
992 if (!context)
993 context = alloc_bts_context(child);
994 if (!context)
995 return -ENOMEM;
997 if (cfg.flags & PTRACE_BTS_O_SIGNAL) {
998 if (!cfg.signal)
999 return -EINVAL;
1001 return -EOPNOTSUPP;
1002 context->bts_ovfl_signal = cfg.signal;
1005 ds_release_bts(context->tracer);
1006 context->tracer = NULL;
1008 if ((cfg.flags & PTRACE_BTS_O_ALLOC) && (cfg.size != context->size)) {
1009 int err;
1011 free_bts_buffer(context);
1012 if (!cfg.size)
1013 return 0;
1015 err = alloc_bts_buffer(context, cfg.size);
1016 if (err < 0)
1017 return err;
1020 if (cfg.flags & PTRACE_BTS_O_TRACE)
1021 flags |= BTS_USER;
1023 if (cfg.flags & PTRACE_BTS_O_SCHED)
1024 flags |= BTS_TIMESTAMPS;
1026 context->tracer =
1027 ds_request_bts_task(child, context->buffer, context->size,
1028 NULL, (size_t)-1, flags);
1029 if (unlikely(IS_ERR(context->tracer))) {
1030 int error = PTR_ERR(context->tracer);
1032 free_bts_buffer(context);
1033 context->tracer = NULL;
1034 return error;
1037 return sizeof(cfg);
1040 static int ptrace_bts_status(struct task_struct *child,
1041 long cfg_size,
1042 struct ptrace_bts_config __user *ucfg)
1044 struct bts_context *context;
1045 const struct bts_trace *trace;
1046 struct ptrace_bts_config cfg;
1048 context = child->bts;
1049 if (!context)
1050 return -ESRCH;
1052 if (cfg_size < sizeof(cfg))
1053 return -EIO;
1055 trace = ds_read_bts(context->tracer);
1056 if (!trace)
1057 return -ESRCH;
1059 memset(&cfg, 0, sizeof(cfg));
1060 cfg.size = trace->ds.end - trace->ds.begin;
1061 cfg.signal = context->bts_ovfl_signal;
1062 cfg.bts_size = sizeof(struct bts_struct);
1064 if (cfg.signal)
1065 cfg.flags |= PTRACE_BTS_O_SIGNAL;
1067 if (trace->ds.flags & BTS_USER)
1068 cfg.flags |= PTRACE_BTS_O_TRACE;
1070 if (trace->ds.flags & BTS_TIMESTAMPS)
1071 cfg.flags |= PTRACE_BTS_O_SCHED;
1073 if (copy_to_user(ucfg, &cfg, sizeof(cfg)))
1074 return -EFAULT;
1076 return sizeof(cfg);
1079 static int ptrace_bts_clear(struct task_struct *child)
1081 struct bts_context *context;
1082 const struct bts_trace *trace;
1084 context = child->bts;
1085 if (!context)
1086 return -ESRCH;
1088 trace = ds_read_bts(context->tracer);
1089 if (!trace)
1090 return -ESRCH;
1092 memset(trace->ds.begin, 0, trace->ds.n * trace->ds.size);
1094 return ds_reset_bts(context->tracer);
1097 static int ptrace_bts_size(struct task_struct *child)
1099 struct bts_context *context;
1100 const struct bts_trace *trace;
1102 context = child->bts;
1103 if (!context)
1104 return -ESRCH;
1106 trace = ds_read_bts(context->tracer);
1107 if (!trace)
1108 return -ESRCH;
1110 return (trace->ds.top - trace->ds.begin) / trace->ds.size;
1114 * Called from __ptrace_unlink() after the child has been moved back
1115 * to its original parent.
1117 void ptrace_bts_untrace(struct task_struct *child)
1119 if (unlikely(child->bts)) {
1120 free_bts_context(child->bts);
1121 child->bts = NULL;
1124 #endif /* CONFIG_X86_PTRACE_BTS */
1127 * Called by kernel/ptrace.c when detaching..
1129 * Make sure the single step bit is not set.
1131 void ptrace_disable(struct task_struct *child)
1133 user_disable_single_step(child);
1134 #ifdef TIF_SYSCALL_EMU
1135 clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
1136 #endif
1139 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1140 static const struct user_regset_view user_x86_32_view; /* Initialized below. */
1141 #endif
1143 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
1145 int ret;
1146 unsigned long __user *datap = (unsigned long __user *)data;
1148 switch (request) {
1149 /* read the word at location addr in the USER area. */
1150 case PTRACE_PEEKUSR: {
1151 unsigned long tmp;
1153 ret = -EIO;
1154 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1155 addr >= sizeof(struct user))
1156 break;
1158 tmp = 0; /* Default return condition */
1159 if (addr < sizeof(struct user_regs_struct))
1160 tmp = getreg(child, addr);
1161 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1162 addr <= offsetof(struct user, u_debugreg[7])) {
1163 addr -= offsetof(struct user, u_debugreg[0]);
1164 tmp = ptrace_get_debugreg(child, addr / sizeof(data));
1166 ret = put_user(tmp, datap);
1167 break;
1170 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
1171 ret = -EIO;
1172 if ((addr & (sizeof(data) - 1)) || addr < 0 ||
1173 addr >= sizeof(struct user))
1174 break;
1176 if (addr < sizeof(struct user_regs_struct))
1177 ret = putreg(child, addr, data);
1178 else if (addr >= offsetof(struct user, u_debugreg[0]) &&
1179 addr <= offsetof(struct user, u_debugreg[7])) {
1180 addr -= offsetof(struct user, u_debugreg[0]);
1181 ret = ptrace_set_debugreg(child,
1182 addr / sizeof(data), data);
1184 break;
1186 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1187 return copy_regset_to_user(child,
1188 task_user_regset_view(current),
1189 REGSET_GENERAL,
1190 0, sizeof(struct user_regs_struct),
1191 datap);
1193 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1194 return copy_regset_from_user(child,
1195 task_user_regset_view(current),
1196 REGSET_GENERAL,
1197 0, sizeof(struct user_regs_struct),
1198 datap);
1200 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1201 return copy_regset_to_user(child,
1202 task_user_regset_view(current),
1203 REGSET_FP,
1204 0, sizeof(struct user_i387_struct),
1205 datap);
1207 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1208 return copy_regset_from_user(child,
1209 task_user_regset_view(current),
1210 REGSET_FP,
1211 0, sizeof(struct user_i387_struct),
1212 datap);
1214 #ifdef CONFIG_X86_32
1215 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1216 return copy_regset_to_user(child, &user_x86_32_view,
1217 REGSET_XFP,
1218 0, sizeof(struct user_fxsr_struct),
1219 datap) ? -EIO : 0;
1221 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1222 return copy_regset_from_user(child, &user_x86_32_view,
1223 REGSET_XFP,
1224 0, sizeof(struct user_fxsr_struct),
1225 datap) ? -EIO : 0;
1226 #endif
1228 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1229 case PTRACE_GET_THREAD_AREA:
1230 if (addr < 0)
1231 return -EIO;
1232 ret = do_get_thread_area(child, addr,
1233 (struct user_desc __user *) data);
1234 break;
1236 case PTRACE_SET_THREAD_AREA:
1237 if (addr < 0)
1238 return -EIO;
1239 ret = do_set_thread_area(child, addr,
1240 (struct user_desc __user *) data, 0);
1241 break;
1242 #endif
1244 #ifdef CONFIG_X86_64
1245 /* normal 64bit interface to access TLS data.
1246 Works just like arch_prctl, except that the arguments
1247 are reversed. */
1248 case PTRACE_ARCH_PRCTL:
1249 ret = do_arch_prctl(child, data, addr);
1250 break;
1251 #endif
1254 * These bits need more cooking - not enabled yet:
1256 #ifdef CONFIG_X86_PTRACE_BTS
1257 case PTRACE_BTS_CONFIG:
1258 ret = ptrace_bts_config
1259 (child, data, (struct ptrace_bts_config __user *)addr);
1260 break;
1262 case PTRACE_BTS_STATUS:
1263 ret = ptrace_bts_status
1264 (child, data, (struct ptrace_bts_config __user *)addr);
1265 break;
1267 case PTRACE_BTS_SIZE:
1268 ret = ptrace_bts_size(child);
1269 break;
1271 case PTRACE_BTS_GET:
1272 ret = ptrace_bts_read_record
1273 (child, data, (struct bts_struct __user *) addr);
1274 break;
1276 case PTRACE_BTS_CLEAR:
1277 ret = ptrace_bts_clear(child);
1278 break;
1280 case PTRACE_BTS_DRAIN:
1281 ret = ptrace_bts_drain
1282 (child, data, (struct bts_struct __user *) addr);
1283 break;
1284 #endif /* CONFIG_X86_PTRACE_BTS */
1286 default:
1287 ret = ptrace_request(child, request, addr, data);
1288 break;
1291 return ret;
1294 #ifdef CONFIG_IA32_EMULATION
1296 #include <linux/compat.h>
1297 #include <linux/syscalls.h>
1298 #include <asm/ia32.h>
1299 #include <asm/user32.h>
1301 #define R32(l,q) \
1302 case offsetof(struct user32, regs.l): \
1303 regs->q = value; break
1305 #define SEG32(rs) \
1306 case offsetof(struct user32, regs.rs): \
1307 return set_segment_reg(child, \
1308 offsetof(struct user_regs_struct, rs), \
1309 value); \
1310 break
1312 static int putreg32(struct task_struct *child, unsigned regno, u32 value)
1314 struct pt_regs *regs = task_pt_regs(child);
1316 switch (regno) {
1318 SEG32(cs);
1319 SEG32(ds);
1320 SEG32(es);
1321 SEG32(fs);
1322 SEG32(gs);
1323 SEG32(ss);
1325 R32(ebx, bx);
1326 R32(ecx, cx);
1327 R32(edx, dx);
1328 R32(edi, di);
1329 R32(esi, si);
1330 R32(ebp, bp);
1331 R32(eax, ax);
1332 R32(eip, ip);
1333 R32(esp, sp);
1335 case offsetof(struct user32, regs.orig_eax):
1337 * A 32-bit debugger setting orig_eax means to restore
1338 * the state of the task restarting a 32-bit syscall.
1339 * Make sure we interpret the -ERESTART* codes correctly
1340 * in case the task is not actually still sitting at the
1341 * exit from a 32-bit syscall with TS_COMPAT still set.
1343 regs->orig_ax = value;
1344 if (syscall_get_nr(child, regs) >= 0)
1345 task_thread_info(child)->status |= TS_COMPAT;
1346 break;
1348 case offsetof(struct user32, regs.eflags):
1349 return set_flags(child, value);
1351 case offsetof(struct user32, u_debugreg[0]) ...
1352 offsetof(struct user32, u_debugreg[7]):
1353 regno -= offsetof(struct user32, u_debugreg[0]);
1354 return ptrace_set_debugreg(child, regno / 4, value);
1356 default:
1357 if (regno > sizeof(struct user32) || (regno & 3))
1358 return -EIO;
1361 * Other dummy fields in the virtual user structure
1362 * are ignored
1364 break;
1366 return 0;
1369 #undef R32
1370 #undef SEG32
1372 #define R32(l,q) \
1373 case offsetof(struct user32, regs.l): \
1374 *val = regs->q; break
1376 #define SEG32(rs) \
1377 case offsetof(struct user32, regs.rs): \
1378 *val = get_segment_reg(child, \
1379 offsetof(struct user_regs_struct, rs)); \
1380 break
1382 static int getreg32(struct task_struct *child, unsigned regno, u32 *val)
1384 struct pt_regs *regs = task_pt_regs(child);
1386 switch (regno) {
1388 SEG32(ds);
1389 SEG32(es);
1390 SEG32(fs);
1391 SEG32(gs);
1393 R32(cs, cs);
1394 R32(ss, ss);
1395 R32(ebx, bx);
1396 R32(ecx, cx);
1397 R32(edx, dx);
1398 R32(edi, di);
1399 R32(esi, si);
1400 R32(ebp, bp);
1401 R32(eax, ax);
1402 R32(orig_eax, orig_ax);
1403 R32(eip, ip);
1404 R32(esp, sp);
1406 case offsetof(struct user32, regs.eflags):
1407 *val = get_flags(child);
1408 break;
1410 case offsetof(struct user32, u_debugreg[0]) ...
1411 offsetof(struct user32, u_debugreg[7]):
1412 regno -= offsetof(struct user32, u_debugreg[0]);
1413 *val = ptrace_get_debugreg(child, regno / 4);
1414 break;
1416 default:
1417 if (regno > sizeof(struct user32) || (regno & 3))
1418 return -EIO;
1421 * Other dummy fields in the virtual user structure
1422 * are ignored
1424 *val = 0;
1425 break;
1427 return 0;
1430 #undef R32
1431 #undef SEG32
1433 static int genregs32_get(struct task_struct *target,
1434 const struct user_regset *regset,
1435 unsigned int pos, unsigned int count,
1436 void *kbuf, void __user *ubuf)
1438 if (kbuf) {
1439 compat_ulong_t *k = kbuf;
1440 while (count >= sizeof(*k)) {
1441 getreg32(target, pos, k++);
1442 count -= sizeof(*k);
1443 pos += sizeof(*k);
1445 } else {
1446 compat_ulong_t __user *u = ubuf;
1447 while (count >= sizeof(*u)) {
1448 compat_ulong_t word;
1449 getreg32(target, pos, &word);
1450 if (__put_user(word, u++))
1451 return -EFAULT;
1452 count -= sizeof(*u);
1453 pos += sizeof(*u);
1457 return 0;
1460 static int genregs32_set(struct task_struct *target,
1461 const struct user_regset *regset,
1462 unsigned int pos, unsigned int count,
1463 const void *kbuf, const void __user *ubuf)
1465 int ret = 0;
1466 if (kbuf) {
1467 const compat_ulong_t *k = kbuf;
1468 while (count >= sizeof(*k) && !ret) {
1469 ret = putreg32(target, pos, *k++);
1470 count -= sizeof(*k);
1471 pos += sizeof(*k);
1473 } else {
1474 const compat_ulong_t __user *u = ubuf;
1475 while (count >= sizeof(*u) && !ret) {
1476 compat_ulong_t word;
1477 ret = __get_user(word, u++);
1478 if (ret)
1479 break;
1480 ret = putreg32(target, pos, word);
1481 count -= sizeof(*u);
1482 pos += sizeof(*u);
1485 return ret;
1488 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
1489 compat_ulong_t caddr, compat_ulong_t cdata)
1491 unsigned long addr = caddr;
1492 unsigned long data = cdata;
1493 void __user *datap = compat_ptr(data);
1494 int ret;
1495 __u32 val;
1497 switch (request) {
1498 case PTRACE_PEEKUSR:
1499 ret = getreg32(child, addr, &val);
1500 if (ret == 0)
1501 ret = put_user(val, (__u32 __user *)datap);
1502 break;
1504 case PTRACE_POKEUSR:
1505 ret = putreg32(child, addr, data);
1506 break;
1508 case PTRACE_GETREGS: /* Get all gp regs from the child. */
1509 return copy_regset_to_user(child, &user_x86_32_view,
1510 REGSET_GENERAL,
1511 0, sizeof(struct user_regs_struct32),
1512 datap);
1514 case PTRACE_SETREGS: /* Set all gp regs in the child. */
1515 return copy_regset_from_user(child, &user_x86_32_view,
1516 REGSET_GENERAL, 0,
1517 sizeof(struct user_regs_struct32),
1518 datap);
1520 case PTRACE_GETFPREGS: /* Get the child FPU state. */
1521 return copy_regset_to_user(child, &user_x86_32_view,
1522 REGSET_FP, 0,
1523 sizeof(struct user_i387_ia32_struct),
1524 datap);
1526 case PTRACE_SETFPREGS: /* Set the child FPU state. */
1527 return copy_regset_from_user(
1528 child, &user_x86_32_view, REGSET_FP,
1529 0, sizeof(struct user_i387_ia32_struct), datap);
1531 case PTRACE_GETFPXREGS: /* Get the child extended FPU state. */
1532 return copy_regset_to_user(child, &user_x86_32_view,
1533 REGSET_XFP, 0,
1534 sizeof(struct user32_fxsr_struct),
1535 datap);
1537 case PTRACE_SETFPXREGS: /* Set the child extended FPU state. */
1538 return copy_regset_from_user(child, &user_x86_32_view,
1539 REGSET_XFP, 0,
1540 sizeof(struct user32_fxsr_struct),
1541 datap);
1543 case PTRACE_GET_THREAD_AREA:
1544 case PTRACE_SET_THREAD_AREA:
1545 #ifdef CONFIG_X86_PTRACE_BTS
1546 case PTRACE_BTS_CONFIG:
1547 case PTRACE_BTS_STATUS:
1548 case PTRACE_BTS_SIZE:
1549 case PTRACE_BTS_GET:
1550 case PTRACE_BTS_CLEAR:
1551 case PTRACE_BTS_DRAIN:
1552 #endif /* CONFIG_X86_PTRACE_BTS */
1553 return arch_ptrace(child, request, addr, data);
1555 default:
1556 return compat_ptrace_request(child, request, addr, data);
1559 return ret;
1562 #endif /* CONFIG_IA32_EMULATION */
1564 #ifdef CONFIG_X86_64
1566 static const struct user_regset x86_64_regsets[] = {
1567 [REGSET_GENERAL] = {
1568 .core_note_type = NT_PRSTATUS,
1569 .n = sizeof(struct user_regs_struct) / sizeof(long),
1570 .size = sizeof(long), .align = sizeof(long),
1571 .get = genregs_get, .set = genregs_set
1573 [REGSET_FP] = {
1574 .core_note_type = NT_PRFPREG,
1575 .n = sizeof(struct user_i387_struct) / sizeof(long),
1576 .size = sizeof(long), .align = sizeof(long),
1577 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1579 [REGSET_IOPERM64] = {
1580 .core_note_type = NT_386_IOPERM,
1581 .n = IO_BITMAP_LONGS,
1582 .size = sizeof(long), .align = sizeof(long),
1583 .active = ioperm_active, .get = ioperm_get
1587 static const struct user_regset_view user_x86_64_view = {
1588 .name = "x86_64", .e_machine = EM_X86_64,
1589 .regsets = x86_64_regsets, .n = ARRAY_SIZE(x86_64_regsets)
1592 #else /* CONFIG_X86_32 */
1594 #define user_regs_struct32 user_regs_struct
1595 #define genregs32_get genregs_get
1596 #define genregs32_set genregs_set
1598 #define user_i387_ia32_struct user_i387_struct
1599 #define user32_fxsr_struct user_fxsr_struct
1601 #endif /* CONFIG_X86_64 */
1603 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1604 static const struct user_regset x86_32_regsets[] = {
1605 [REGSET_GENERAL] = {
1606 .core_note_type = NT_PRSTATUS,
1607 .n = sizeof(struct user_regs_struct32) / sizeof(u32),
1608 .size = sizeof(u32), .align = sizeof(u32),
1609 .get = genregs32_get, .set = genregs32_set
1611 [REGSET_FP] = {
1612 .core_note_type = NT_PRFPREG,
1613 .n = sizeof(struct user_i387_ia32_struct) / sizeof(u32),
1614 .size = sizeof(u32), .align = sizeof(u32),
1615 .active = fpregs_active, .get = fpregs_get, .set = fpregs_set
1617 [REGSET_XFP] = {
1618 .core_note_type = NT_PRXFPREG,
1619 .n = sizeof(struct user32_fxsr_struct) / sizeof(u32),
1620 .size = sizeof(u32), .align = sizeof(u32),
1621 .active = xfpregs_active, .get = xfpregs_get, .set = xfpregs_set
1623 [REGSET_TLS] = {
1624 .core_note_type = NT_386_TLS,
1625 .n = GDT_ENTRY_TLS_ENTRIES, .bias = GDT_ENTRY_TLS_MIN,
1626 .size = sizeof(struct user_desc),
1627 .align = sizeof(struct user_desc),
1628 .active = regset_tls_active,
1629 .get = regset_tls_get, .set = regset_tls_set
1631 [REGSET_IOPERM32] = {
1632 .core_note_type = NT_386_IOPERM,
1633 .n = IO_BITMAP_BYTES / sizeof(u32),
1634 .size = sizeof(u32), .align = sizeof(u32),
1635 .active = ioperm_active, .get = ioperm_get
1639 static const struct user_regset_view user_x86_32_view = {
1640 .name = "i386", .e_machine = EM_386,
1641 .regsets = x86_32_regsets, .n = ARRAY_SIZE(x86_32_regsets)
1643 #endif
1645 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1647 #ifdef CONFIG_IA32_EMULATION
1648 if (test_tsk_thread_flag(task, TIF_IA32))
1649 #endif
1650 #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
1651 return &user_x86_32_view;
1652 #endif
1653 #ifdef CONFIG_X86_64
1654 return &user_x86_64_view;
1655 #endif
1658 static void fill_sigtrap_info(struct task_struct *tsk,
1659 struct pt_regs *regs,
1660 int error_code, int si_code,
1661 struct siginfo *info)
1663 tsk->thread.trap_no = 1;
1664 tsk->thread.error_code = error_code;
1666 memset(info, 0, sizeof(*info));
1667 info->si_signo = SIGTRAP;
1668 info->si_code = si_code;
1669 info->si_addr = user_mode_vm(regs) ? (void __user *)regs->ip : NULL;
1672 void user_single_step_siginfo(struct task_struct *tsk,
1673 struct pt_regs *regs,
1674 struct siginfo *info)
1676 fill_sigtrap_info(tsk, regs, 0, TRAP_BRKPT, info);
1679 void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
1680 int error_code, int si_code)
1682 struct siginfo info;
1684 fill_sigtrap_info(tsk, regs, error_code, si_code, &info);
1685 /* Send us the fake SIGTRAP */
1686 force_sig_info(SIGTRAP, &info, tsk);
1690 #ifdef CONFIG_X86_32
1691 # define IS_IA32 1
1692 #elif defined CONFIG_IA32_EMULATION
1693 # define IS_IA32 is_compat_task()
1694 #else
1695 # define IS_IA32 0
1696 #endif
1699 * We must return the syscall number to actually look up in the table.
1700 * This can be -1L to skip running any syscall at all.
1702 asmregparm long syscall_trace_enter(struct pt_regs *regs)
1704 long ret = 0;
1707 * If we stepped into a sysenter/syscall insn, it trapped in
1708 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
1709 * If user-mode had set TF itself, then it's still clear from
1710 * do_debug() and we need to set it again to restore the user
1711 * state. If we entered on the slow path, TF was already set.
1713 if (test_thread_flag(TIF_SINGLESTEP))
1714 regs->flags |= X86_EFLAGS_TF;
1716 /* do the secure computing check first */
1717 secure_computing(regs->orig_ax);
1719 if (unlikely(test_thread_flag(TIF_SYSCALL_EMU)))
1720 ret = -1L;
1722 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
1723 tracehook_report_syscall_entry(regs))
1724 ret = -1L;
1726 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1727 trace_sys_enter(regs, regs->orig_ax);
1729 if (unlikely(current->audit_context)) {
1730 if (IS_IA32)
1731 audit_syscall_entry(AUDIT_ARCH_I386,
1732 regs->orig_ax,
1733 regs->bx, regs->cx,
1734 regs->dx, regs->si);
1735 #ifdef CONFIG_X86_64
1736 else
1737 audit_syscall_entry(AUDIT_ARCH_X86_64,
1738 regs->orig_ax,
1739 regs->di, regs->si,
1740 regs->dx, regs->r10);
1741 #endif
1744 return ret ?: regs->orig_ax;
1747 asmregparm void syscall_trace_leave(struct pt_regs *regs)
1749 bool step;
1751 if (unlikely(current->audit_context))
1752 audit_syscall_exit(AUDITSC_RESULT(regs->ax), regs->ax);
1754 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1755 trace_sys_exit(regs, regs->ax);
1758 * If TIF_SYSCALL_EMU is set, we only get here because of
1759 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
1760 * We already reported this syscall instruction in
1761 * syscall_trace_enter().
1763 step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
1764 !test_thread_flag(TIF_SYSCALL_EMU);
1765 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
1766 tracehook_report_syscall_exit(regs, step);