]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/x86/kernel/step.c
2 * x86 single-step support code, common to 32-bit and 64-bit.
4 #include <linux/sched.h>
5 #include <linux/sched/task_stack.h>
7 #include <linux/ptrace.h>
9 #include <asm/mmu_context.h>
11 unsigned long convert_ip_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);
22 #ifdef CONFIG_MODIFY_LDT_SYSCALL
24 * We'll assume that the code segments in the GDT
25 * are all zero-based. That is largely true: the
26 * TLS segments are used for data, and the PNPBIOS
27 * and APM bios ones we just ignore here.
29 if ((seg
& SEGMENT_TI_MASK
) == SEGMENT_LDT
) {
30 struct desc_struct
*desc
;
35 mutex_lock(&child
->mm
->context
.lock
);
36 if (unlikely(!child
->mm
->context
.ldt
||
37 seg
>= child
->mm
->context
.ldt
->nr_entries
))
38 addr
= -1L; /* bogus selector, access would fault */
40 desc
= &child
->mm
->context
.ldt
->entries
[seg
];
41 base
= get_desc_base(desc
);
43 /* 16-bit code segment? */
48 mutex_unlock(&child
->mm
->context
.lock
);
55 static int is_setting_trap_flag(struct task_struct
*child
, struct pt_regs
*regs
)
58 unsigned char opcode
[15];
59 unsigned long addr
= convert_ip_to_linear(child
, regs
);
61 copied
= access_process_vm(child
, addr
, opcode
, sizeof(opcode
),
63 for (i
= 0; i
< copied
; i
++) {
71 /* opcode and address size prefixes */
74 /* irrelevant prefixes (segment overrides and repeats) */
78 case 0xf0: case 0xf2: case 0xf3:
83 if (!user_64bit_mode(regs
))
84 /* 32-bit mode: register increment */
86 /* 64-bit mode: REX prefix */
93 * pushf: NOTE! We should probably not let
94 * the user see the TF bit being set. But
95 * it's more pain than it's worth to avoid
96 * it, and a debugger could emulate this
97 * all in user space if it _really_ cares.
108 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
110 static int enable_single_step(struct task_struct
*child
)
112 struct pt_regs
*regs
= task_pt_regs(child
);
113 unsigned long oflags
;
116 * If we stepped into a sysenter/syscall insn, it trapped in
117 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
118 * If user-mode had set TF itself, then it's still clear from
119 * do_debug() and we need to set it again to restore the user
120 * state so we don't wrongly set TIF_FORCED_TF below.
121 * If enable_single_step() was used last and that is what
122 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
123 * already set and our bookkeeping is fine.
125 if (unlikely(test_tsk_thread_flag(child
, TIF_SINGLESTEP
)))
126 regs
->flags
|= X86_EFLAGS_TF
;
129 * Always set TIF_SINGLESTEP - this guarantees that
130 * we single-step system calls etc.. This will also
131 * cause us to set TF when returning to user mode.
133 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
135 oflags
= regs
->flags
;
137 /* Set TF on the kernel stack.. */
138 regs
->flags
|= X86_EFLAGS_TF
;
141 * ..but if TF is changed by the instruction we will trace,
142 * don't mark it as being "us" that set it, so that we
143 * won't clear it by hand later.
145 * Note that if we don't actually execute the popf because
146 * of a signal arriving right now or suchlike, we will lose
147 * track of the fact that it really was "us" that set it.
149 if (is_setting_trap_flag(child
, regs
)) {
150 clear_tsk_thread_flag(child
, TIF_FORCED_TF
);
155 * If TF was already set, check whether it was us who set it.
156 * If not, we should never attempt a block step.
158 if (oflags
& X86_EFLAGS_TF
)
159 return test_tsk_thread_flag(child
, TIF_FORCED_TF
);
161 set_tsk_thread_flag(child
, TIF_FORCED_TF
);
166 void set_task_blockstep(struct task_struct
*task
, bool on
)
168 unsigned long debugctl
;
171 * Ensure irq/preemption can't change debugctl in between.
172 * Note also that both TIF_BLOCKSTEP and debugctl should
173 * be changed atomically wrt preemption.
175 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if
176 * task is current or it can't be running, otherwise we can race
177 * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but
178 * PTRACE_KILL is not safe.
181 debugctl
= get_debugctlmsr();
183 debugctl
|= DEBUGCTLMSR_BTF
;
184 set_tsk_thread_flag(task
, TIF_BLOCKSTEP
);
186 debugctl
&= ~DEBUGCTLMSR_BTF
;
187 clear_tsk_thread_flag(task
, TIF_BLOCKSTEP
);
190 update_debugctlmsr(debugctl
);
195 * Enable single or block step.
197 static void enable_step(struct task_struct
*child
, bool block
)
200 * Make sure block stepping (BTF) is not enabled unless it should be.
201 * Note that we don't try to worry about any is_setting_trap_flag()
202 * instructions after the first when using block stepping.
203 * So no one should try to use debugger block stepping in a program
204 * that uses user-mode single stepping itself.
206 if (enable_single_step(child
) && block
)
207 set_task_blockstep(child
, true);
208 else if (test_tsk_thread_flag(child
, TIF_BLOCKSTEP
))
209 set_task_blockstep(child
, false);
212 void user_enable_single_step(struct task_struct
*child
)
214 enable_step(child
, 0);
217 void user_enable_block_step(struct task_struct
*child
)
219 enable_step(child
, 1);
222 void user_disable_single_step(struct task_struct
*child
)
225 * Make sure block stepping (BTF) is disabled.
227 if (test_tsk_thread_flag(child
, TIF_BLOCKSTEP
))
228 set_task_blockstep(child
, false);
230 /* Always clear TIF_SINGLESTEP... */
231 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
233 /* But touch TF only if it was set by us.. */
234 if (test_and_clear_tsk_thread_flag(child
, TIF_FORCED_TF
))
235 task_pt_regs(child
)->flags
&= ~X86_EFLAGS_TF
;