2 * x86 single-step support code, common to 32-bit and 64-bit.
4 #include <linux/sched.h>
6 #include <linux/ptrace.h>
11 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
13 unsigned long addr, seg;
16 seg = regs->cs & 0xffff;
17 if (v8086_mode(regs)) {
18 addr = (addr & 0xffff) + (seg << 4);
23 * We'll assume that the code segments in the GDT
24 * are all zero-based. That is largely true: the
25 * TLS segments are used for data, and the PNPBIOS
26 * and APM bios ones we just ignore here.
28 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
34 mutex_lock(&child->mm->context.lock);
35 if (unlikely((seg >> 3) >= child->mm->context.size))
36 addr = -1L; /* bogus selector, access would fault */
38 desc = child->mm->context.ldt + seg;
39 base = ((desc[0] >> 16) |
40 ((desc[1] & 0xff) << 16) |
41 (desc[1] & 0xff000000));
43 /* 16-bit code segment? */
44 if (!((desc[1] >> 22) & 1))
48 mutex_unlock(&child->mm->context.lock);
54 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
57 unsigned char opcode[15];
58 unsigned long addr = convert_rip_to_linear(child, regs);
60 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
61 for (i = 0; i < copied; i++) {
69 /* opcode and address size prefixes */
72 /* irrelevant prefixes (segment overrides and repeats) */
76 case 0xf0: case 0xf2: case 0xf3:
81 if (regs->cs != __USER_CS)
82 /* 32-bit mode: register increment */
84 /* 64-bit mode: REX prefix */
91 * pushf: NOTE! We should probably not let
92 * the user see the TF bit being set. But
93 * it's more pain than it's worth to avoid
94 * it, and a debugger could emulate this
95 * all in user space if it _really_ cares.
106 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
108 static int enable_single_step(struct task_struct *child)
110 struct pt_regs *regs = task_pt_regs(child);
113 * Always set TIF_SINGLESTEP - this guarantees that
114 * we single-step system calls etc.. This will also
115 * cause us to set TF when returning to user mode.
117 set_tsk_thread_flag(child, TIF_SINGLESTEP);
120 * If TF was already set, don't do anything else
122 if (regs->flags & X86_EFLAGS_TF)
125 /* Set TF on the kernel stack.. */
126 regs->flags |= X86_EFLAGS_TF;
129 * ..but if TF is changed by the instruction we will trace,
130 * don't mark it as being "us" that set it, so that we
131 * won't clear it by hand later.
133 if (is_setting_trap_flag(child, regs))
136 set_tsk_thread_flag(child, TIF_FORCED_TF);
142 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
144 static void write_debugctlmsr(struct task_struct *child, unsigned long val)
146 child->thread.debugctlmsr = val;
148 if (child != current)
152 wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
154 wrmsr(MSR_IA32_DEBUGCTLMSR, val, 0);
159 * Enable single or block step.
161 static void enable_step(struct task_struct *child, bool block)
164 * Make sure block stepping (BTF) is not enabled unless it should be.
165 * Note that we don't try to worry about any is_setting_trap_flag()
166 * instructions after the first when using block stepping.
167 * So noone should try to use debugger block stepping in a program
168 * that uses user-mode single stepping itself.
170 if (enable_single_step(child) && block) {
171 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
172 write_debugctlmsr(child,
173 child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
175 write_debugctlmsr(child,
176 child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
178 if (!child->thread.debugctlmsr)
179 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
183 void user_enable_single_step(struct task_struct *child)
185 enable_step(child, 0);
188 void user_enable_block_step(struct task_struct *child)
190 enable_step(child, 1);
193 void user_disable_single_step(struct task_struct *child)
196 * Make sure block stepping (BTF) is disabled.
198 write_debugctlmsr(child,
199 child->thread.debugctlmsr & ~TIF_DEBUGCTLMSR);
201 if (!child->thread.debugctlmsr)
202 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
204 /* Always clear TIF_SINGLESTEP... */
205 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
207 /* But touch TF only if it was set by us.. */
208 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
209 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;