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_ip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
)
10 unsigned long addr
, seg
;
13 seg
= regs
->cs
& 0xffff;
14 if (v8086_mode(regs
)) {
15 addr
= (addr
& 0xffff) + (seg
<< 4);
20 * We'll assume that the code segments in the GDT
21 * are all zero-based. That is largely true: the
22 * TLS segments are used for data, and the PNPBIOS
23 * and APM bios ones we just ignore here.
25 if ((seg
& SEGMENT_TI_MASK
) == SEGMENT_LDT
) {
31 mutex_lock(&child
->mm
->context
.lock
);
32 if (unlikely((seg
>> 3) >= child
->mm
->context
.size
))
33 addr
= -1L; /* bogus selector, access would fault */
35 desc
= child
->mm
->context
.ldt
+ seg
;
36 base
= ((desc
[0] >> 16) |
37 ((desc
[1] & 0xff) << 16) |
38 (desc
[1] & 0xff000000));
40 /* 16-bit code segment? */
41 if (!((desc
[1] >> 22) & 1))
45 mutex_unlock(&child
->mm
->context
.lock
);
51 static int is_setting_trap_flag(struct task_struct
*child
, struct pt_regs
*regs
)
54 unsigned char opcode
[15];
55 unsigned long addr
= convert_ip_to_linear(child
, regs
);
57 copied
= access_process_vm(child
, addr
, opcode
, sizeof(opcode
), 0);
58 for (i
= 0; i
< copied
; i
++) {
66 /* opcode and address size prefixes */
69 /* irrelevant prefixes (segment overrides and repeats) */
73 case 0xf0: case 0xf2: case 0xf3:
78 if (regs
->cs
!= __USER_CS
)
79 /* 32-bit mode: register increment */
81 /* 64-bit mode: REX prefix */
88 * pushf: NOTE! We should probably not let
89 * the user see the TF bit being set. But
90 * it's more pain than it's worth to avoid
91 * it, and a debugger could emulate this
92 * all in user space if it _really_ cares.
103 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
105 static int enable_single_step(struct task_struct
*child
)
107 struct pt_regs
*regs
= task_pt_regs(child
);
110 * Always set TIF_SINGLESTEP - this guarantees that
111 * we single-step system calls etc.. This will also
112 * cause us to set TF when returning to user mode.
114 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
117 * If TF was already set, don't do anything else
119 if (regs
->flags
& X86_EFLAGS_TF
)
122 /* Set TF on the kernel stack.. */
123 regs
->flags
|= X86_EFLAGS_TF
;
126 * ..but if TF is changed by the instruction we will trace,
127 * don't mark it as being "us" that set it, so that we
128 * won't clear it by hand later.
130 if (is_setting_trap_flag(child
, regs
))
133 set_tsk_thread_flag(child
, TIF_FORCED_TF
);
139 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
141 static void write_debugctlmsr(struct task_struct
*child
, unsigned long val
)
143 child
->thread
.debugctlmsr
= val
;
145 if (child
!= current
)
148 wrmsrl(MSR_IA32_DEBUGCTLMSR
, val
);
152 * Enable single or block step.
154 static void enable_step(struct task_struct
*child
, bool block
)
157 * Make sure block stepping (BTF) is not enabled unless it should be.
158 * Note that we don't try to worry about any is_setting_trap_flag()
159 * instructions after the first when using block stepping.
160 * So noone should try to use debugger block stepping in a program
161 * that uses user-mode single stepping itself.
163 if (enable_single_step(child
) && block
) {
164 set_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
165 write_debugctlmsr(child
,
166 child
->thread
.debugctlmsr
| DEBUGCTLMSR_BTF
);
168 write_debugctlmsr(child
,
169 <<<<<<< HEAD
:arch
/x86
/kernel
/step
.c
170 child
->thread
.debugctlmsr
& ~TIF_DEBUGCTLMSR
);
172 child
->thread
.debugctlmsr
& ~DEBUGCTLMSR_BTF
);
173 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kernel
/step
.c
175 if (!child
->thread
.debugctlmsr
)
176 clear_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
180 void user_enable_single_step(struct task_struct
*child
)
182 enable_step(child
, 0);
185 void user_enable_block_step(struct task_struct
*child
)
187 enable_step(child
, 1);
190 void user_disable_single_step(struct task_struct
*child
)
193 * Make sure block stepping (BTF) is disabled.
195 write_debugctlmsr(child
,
196 <<<<<<< HEAD
:arch
/x86
/kernel
/step
.c
197 child
->thread
.debugctlmsr
& ~TIF_DEBUGCTLMSR
);
199 child
->thread
.debugctlmsr
& ~DEBUGCTLMSR_BTF
);
200 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/x86
/kernel
/step
.c
202 if (!child
->thread
.debugctlmsr
)
203 clear_tsk_thread_flag(child
, TIF_DEBUGCTLMSR
);
205 /* Always clear TIF_SINGLESTEP... */
206 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
208 /* But touch TF only if it was set by us.. */
209 if (test_and_clear_tsk_thread_flag(child
, TIF_FORCED_TF
))
210 task_pt_regs(child
)->flags
&= ~X86_EFLAGS_TF
;