2 * x86 single-step support code, common to 32-bit and 64-bit.
4 #include <linux/sched.h>
6 #include <linux/ptrace.h>
10 unsigned long convert_rip_to_linear(struct task_struct *child, struct pt_regs *regs)
12 unsigned long addr, seg;
15 seg = regs->cs & 0xffff;
18 * We'll assume that the code segments in the GDT
19 * are all zero-based. That is largely true: the
20 * TLS segments are used for data, and the PNPBIOS
21 * and APM bios ones we just ignore here.
23 if (seg & LDT_SEGMENT) {
29 mutex_lock(&child->mm->context.lock);
30 if (unlikely((seg >> 3) >= child->mm->context.size))
31 addr = -1L; /* bogus selector, access would fault */
33 desc = child->mm->context.ldt + seg;
34 base = ((desc[0] >> 16) |
35 ((desc[1] & 0xff) << 16) |
36 (desc[1] & 0xff000000));
38 /* 16-bit code segment? */
39 if (!((desc[1] >> 22) & 1))
43 mutex_unlock(&child->mm->context.lock);
49 static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
52 unsigned char opcode[15];
53 unsigned long addr = convert_rip_to_linear(child, regs);
55 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
56 for (i = 0; i < copied; i++) {
64 /* opcode and address size prefixes */
67 /* irrelevant prefixes (segment overrides and repeats) */
75 if (regs->cs != __USER_CS)
76 /* 32-bit mode: register increment */
78 /* 64-bit mode: REX prefix */
84 * pushf: NOTE! We should probably not let
85 * the user see the TF bit being set. But
86 * it's more pain than it's worth to avoid
87 * it, and a debugger could emulate this
88 * all in user space if it _really_ cares.
98 void user_enable_single_step(struct task_struct *child)
100 struct pt_regs *regs = task_pt_regs(child);
103 * Always set TIF_SINGLESTEP - this guarantees that
104 * we single-step system calls etc.. This will also
105 * cause us to set TF when returning to user mode.
107 set_tsk_thread_flag(child, TIF_SINGLESTEP);
110 * If TF was already set, don't do anything else
112 if (regs->eflags & X86_EFLAGS_TF)
115 /* Set TF on the kernel stack.. */
116 regs->eflags |= X86_EFLAGS_TF;
119 * ..but if TF is changed by the instruction we will trace,
120 * don't mark it as being "us" that set it, so that we
121 * won't clear it by hand later.
123 if (is_setting_trap_flag(child, regs))
126 child->ptrace |= PT_DTRACE;
129 void user_disable_single_step(struct task_struct *child)
131 /* Always clear TIF_SINGLESTEP... */
132 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
134 /* But touch TF only if it was set by us.. */
135 if (child->ptrace & PT_DTRACE) {
136 struct pt_regs *regs = task_pt_regs(child);
137 regs->eflags &= ~X86_EFLAGS_TF;
138 child->ptrace &= ~PT_DTRACE;