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;
17 seg = regs->cs & 0xffff;
20 seg = regs->xcs & 0xffff;
21 if (regs->eflags & X86_EFLAGS_VM) {
22 addr = (addr & 0xffff) + (seg << 4);
28 * We'll assume that the code segments in the GDT
29 * are all zero-based. That is largely true: the
30 * TLS segments are used for data, and the PNPBIOS
31 * and APM bios ones we just ignore here.
33 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
39 mutex_lock(&child->mm->context.lock);
40 if (unlikely((seg >> 3) >= child->mm->context.size))
41 addr = -1L; /* bogus selector, access would fault */
43 desc = child->mm->context.ldt + seg;
44 base = ((desc[0] >> 16) |
45 ((desc[1] & 0xff) << 16) |
46 (desc[1] & 0xff000000));
48 /* 16-bit code segment? */
49 if (!((desc[1] >> 22) & 1))
53 mutex_unlock(&child->mm->context.lock);
59 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
62 unsigned char opcode[15];
63 unsigned long addr = convert_rip_to_linear(child, regs);
65 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
66 for (i = 0; i < copied; i++) {
74 /* opcode and address size prefixes */
77 /* irrelevant prefixes (segment overrides and repeats) */
81 case 0xf0: case 0xf2: case 0xf3:
86 if (regs->cs != __USER_CS)
87 /* 32-bit mode: register increment */
89 /* 64-bit mode: REX prefix */
96 * pushf: NOTE! We should probably not let
97 * the user see the TF bit being set. But
98 * it's more pain than it's worth to avoid
99 * it, and a debugger could emulate this
100 * all in user space if it _really_ cares.
111 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
113 static int enable_single_step(struct task_struct *child)
115 struct pt_regs *regs = task_pt_regs(child);
118 * Always set TIF_SINGLESTEP - this guarantees that
119 * we single-step system calls etc.. This will also
120 * cause us to set TF when returning to user mode.
122 set_tsk_thread_flag(child, TIF_SINGLESTEP);
125 * If TF was already set, don't do anything else
127 if (regs->eflags & X86_EFLAGS_TF)
130 /* Set TF on the kernel stack.. */
131 regs->eflags |= X86_EFLAGS_TF;
134 * ..but if TF is changed by the instruction we will trace,
135 * don't mark it as being "us" that set it, so that we
136 * won't clear it by hand later.
138 if (is_setting_trap_flag(child, regs))
141 set_tsk_thread_flag(child, TIF_FORCED_TF);
147 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
149 static void write_debugctlmsr(struct task_struct *child, unsigned long val)
151 child->thread.debugctlmsr = val;
153 if (child != current)
157 wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
159 wrmsr(MSR_IA32_DEBUGCTLMSR, val, 0);
164 * Enable single or block step.
166 static void enable_step(struct task_struct *child, bool block)
169 * Make sure block stepping (BTF) is not enabled unless it should be.
170 * Note that we don't try to worry about any is_setting_trap_flag()
171 * instructions after the first when using block stepping.
172 * So noone should try to use debugger block stepping in a program
173 * that uses user-mode single stepping itself.
175 if (enable_single_step(child) && block) {
176 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
177 write_debugctlmsr(child, DEBUGCTLMSR_BTF);
178 } else if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR)) {
179 write_debugctlmsr(child, 0);
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 if (test_and_clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR))
199 write_debugctlmsr(child, 0);
201 /* Always clear TIF_SINGLESTEP... */
202 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
204 /* But touch TF only if it was set by us.. */
205 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
206 task_pt_regs(child)->eflags &= ~X86_EFLAGS_TF;