2 * x86 single-step support code, common to 32-bit and 64-bit.
4 #include <linux/sched.h>
6 #include <linux/ptrace.h>
8 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
10 unsigned long addr, seg;
13 seg = regs->cs & 0xffff;
16 * We'll assume that the code segments in the GDT
17 * are all zero-based. That is largely true: the
18 * TLS segments are used for data, and the PNPBIOS
19 * and APM bios ones we just ignore here.
21 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
27 mutex_lock(&child->mm->context.lock);
28 if (unlikely((seg >> 3) >= child->mm->context.size))
29 addr = -1L; /* bogus selector, access would fault */
31 desc = child->mm->context.ldt + seg;
32 base = ((desc[0] >> 16) |
33 ((desc[1] & 0xff) << 16) |
34 (desc[1] & 0xff000000));
36 /* 16-bit code segment? */
37 if (!((desc[1] >> 22) & 1))
41 mutex_unlock(&child->mm->context.lock);
47 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
50 unsigned char opcode[15];
51 unsigned long addr = convert_rip_to_linear(child, regs);
53 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
54 for (i = 0; i < copied; i++) {
62 /* opcode and address size prefixes */
65 /* irrelevant prefixes (segment overrides and repeats) */
69 case 0xf0: case 0xf2: case 0xf3:
73 if (regs->cs != __USER_CS)
74 /* 32-bit mode: register increment */
76 /* 64-bit mode: REX prefix */
82 * pushf: NOTE! We should probably not let
83 * the user see the TF bit being set. But
84 * it's more pain than it's worth to avoid
85 * it, and a debugger could emulate this
86 * all in user space if it _really_ cares.
96 void user_enable_single_step(struct task_struct *child)
98 struct pt_regs *regs = task_pt_regs(child);
101 * Always set TIF_SINGLESTEP - this guarantees that
102 * we single-step system calls etc.. This will also
103 * cause us to set TF when returning to user mode.
105 set_tsk_thread_flag(child, TIF_SINGLESTEP);
108 * If TF was already set, don't do anything else
110 if (regs->eflags & X86_EFLAGS_TF)
113 /* Set TF on the kernel stack.. */
114 regs->eflags |= X86_EFLAGS_TF;
117 * ..but if TF is changed by the instruction we will trace,
118 * don't mark it as being "us" that set it, so that we
119 * won't clear it by hand later.
121 if (is_setting_trap_flag(child, regs))
124 child->ptrace |= PT_DTRACE;
127 void user_disable_single_step(struct task_struct *child)
129 /* Always clear TIF_SINGLESTEP... */
130 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
132 /* But touch TF only if it was set by us.. */
133 if (child->ptrace & PT_DTRACE) {
134 struct pt_regs *regs = task_pt_regs(child);
135 regs->eflags &= ~X86_EFLAGS_TF;
136 child->ptrace &= ~PT_DTRACE;