blob: 0ccb53a9fcd9361b83c7acd26e1f64601816a3d1 [file] [log] [blame]
Roland McGrathfa1e03e2008-01-30 13:30:50 +01001/*
2 * x86 single-step support code, common to 32-bit and 64-bit.
3 */
4#include <linux/sched.h>
5#include <linux/mm.h>
6#include <linux/ptrace.h>
Akinobu Mita254e0a62009-07-19 00:08:54 +09007#include <asm/desc.h>
Andy Lutomirski37868fe2015-07-30 14:31:32 -07008#include <asm/mmu_context.h>
Roland McGrathfa1e03e2008-01-30 13:30:50 +01009
Harvey Harrison37cd9cf2008-01-30 13:33:12 +010010unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
Roland McGrathfa1e03e2008-01-30 13:30:50 +010011{
12 unsigned long addr, seg;
13
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010014 addr = regs->ip;
Roland McGrathfa1e03e2008-01-30 13:30:50 +010015 seg = regs->cs & 0xffff;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010016 if (v8086_mode(regs)) {
Roland McGrath7122ec82008-01-30 13:30:50 +010017 addr = (addr & 0xffff) + (seg << 4);
18 return addr;
19 }
Roland McGrathfa1e03e2008-01-30 13:30:50 +010020
21 /*
22 * We'll assume that the code segments in the GDT
23 * are all zero-based. That is largely true: the
24 * TLS segments are used for data, and the PNPBIOS
25 * and APM bios ones we just ignore here.
26 */
Roland McGrath3f80c1a2008-01-30 13:30:50 +010027 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Akinobu Mita254e0a62009-07-19 00:08:54 +090028 struct desc_struct *desc;
Roland McGrathfa1e03e2008-01-30 13:30:50 +010029 unsigned long base;
30
Juergen Gross136d9d82015-08-06 10:04:38 +020031 seg >>= 3;
Roland McGrathfa1e03e2008-01-30 13:30:50 +010032
33 mutex_lock(&child->mm->context.lock);
Andy Lutomirski37868fe2015-07-30 14:31:32 -070034 if (unlikely(!child->mm->context.ldt ||
Juergen Gross136d9d82015-08-06 10:04:38 +020035 seg >= child->mm->context.ldt->size))
Roland McGrathfa1e03e2008-01-30 13:30:50 +010036 addr = -1L; /* bogus selector, access would fault */
37 else {
Andy Lutomirski37868fe2015-07-30 14:31:32 -070038 desc = &child->mm->context.ldt->entries[seg];
Akinobu Mita254e0a62009-07-19 00:08:54 +090039 base = get_desc_base(desc);
Roland McGrathfa1e03e2008-01-30 13:30:50 +010040
41 /* 16-bit code segment? */
Akinobu Mita254e0a62009-07-19 00:08:54 +090042 if (!desc->d)
Roland McGrathfa1e03e2008-01-30 13:30:50 +010043 addr &= 0xffff;
44 addr += base;
45 }
46 mutex_unlock(&child->mm->context.lock);
47 }
48
49 return addr;
50}
51
52static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
53{
54 int i, copied;
55 unsigned char opcode[15];
Harvey Harrison37cd9cf2008-01-30 13:33:12 +010056 unsigned long addr = convert_ip_to_linear(child, regs);
Roland McGrathfa1e03e2008-01-30 13:30:50 +010057
58 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
59 for (i = 0; i < copied; i++) {
60 switch (opcode[i]) {
61 /* popf and iret */
62 case 0x9d: case 0xcf:
63 return 1;
64
65 /* CHECKME: 64 65 */
66
67 /* opcode and address size prefixes */
68 case 0x66: case 0x67:
69 continue;
70 /* irrelevant prefixes (segment overrides and repeats) */
71 case 0x26: case 0x2e:
72 case 0x36: case 0x3e:
73 case 0x64: case 0x65:
Roland McGrath5f76cb12008-01-30 13:30:50 +010074 case 0xf0: case 0xf2: case 0xf3:
Roland McGrathfa1e03e2008-01-30 13:30:50 +010075 continue;
76
Roland McGrath7122ec82008-01-30 13:30:50 +010077#ifdef CONFIG_X86_64
Roland McGrathfa1e03e2008-01-30 13:30:50 +010078 case 0x40 ... 0x4f:
Andy Lutomirski318f5a22011-08-03 09:31:53 -040079 if (!user_64bit_mode(regs))
Roland McGrathfa1e03e2008-01-30 13:30:50 +010080 /* 32-bit mode: register increment */
81 return 0;
82 /* 64-bit mode: REX prefix */
83 continue;
Roland McGrath7122ec82008-01-30 13:30:50 +010084#endif
Roland McGrathfa1e03e2008-01-30 13:30:50 +010085
86 /* CHECKME: f2, f3 */
87
88 /*
89 * pushf: NOTE! We should probably not let
90 * the user see the TF bit being set. But
91 * it's more pain than it's worth to avoid
92 * it, and a debugger could emulate this
93 * all in user space if it _really_ cares.
94 */
95 case 0x9c:
96 default:
97 return 0;
98 }
99 }
100 return 0;
101}
102
Roland McGrath10faa812008-01-30 13:30:54 +0100103/*
104 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
105 */
106static int enable_single_step(struct task_struct *child)
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100107{
108 struct pt_regs *regs = task_pt_regs(child);
Roland McGrath6718d0d2008-07-09 01:07:02 -0700109 unsigned long oflags;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100110
111 /*
Roland McGrath380fdd72008-07-09 02:39:29 -0700112 * If we stepped into a sysenter/syscall insn, it trapped in
113 * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
114 * If user-mode had set TF itself, then it's still clear from
115 * do_debug() and we need to set it again to restore the user
116 * state so we don't wrongly set TIF_FORCED_TF below.
117 * If enable_single_step() was used last and that is what
118 * set TIF_SINGLESTEP, then both TF and TIF_FORCED_TF are
119 * already set and our bookkeeping is fine.
120 */
121 if (unlikely(test_tsk_thread_flag(child, TIF_SINGLESTEP)))
122 regs->flags |= X86_EFLAGS_TF;
123
124 /*
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100125 * Always set TIF_SINGLESTEP - this guarantees that
126 * we single-step system calls etc.. This will also
127 * cause us to set TF when returning to user mode.
128 */
129 set_tsk_thread_flag(child, TIF_SINGLESTEP);
130
Roland McGrath6718d0d2008-07-09 01:07:02 -0700131 oflags = regs->flags;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100132
133 /* Set TF on the kernel stack.. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100134 regs->flags |= X86_EFLAGS_TF;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100135
136 /*
137 * ..but if TF is changed by the instruction we will trace,
138 * don't mark it as being "us" that set it, so that we
139 * won't clear it by hand later.
Roland McGrath6718d0d2008-07-09 01:07:02 -0700140 *
141 * Note that if we don't actually execute the popf because
142 * of a signal arriving right now or suchlike, we will lose
143 * track of the fact that it really was "us" that set it.
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100144 */
Roland McGrath6718d0d2008-07-09 01:07:02 -0700145 if (is_setting_trap_flag(child, regs)) {
146 clear_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrath10faa812008-01-30 13:30:54 +0100147 return 0;
Roland McGrath6718d0d2008-07-09 01:07:02 -0700148 }
149
150 /*
151 * If TF was already set, check whether it was us who set it.
152 * If not, we should never attempt a block step.
153 */
154 if (oflags & X86_EFLAGS_TF)
155 return test_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100156
Roland McGrathe1f28772008-01-30 13:30:50 +0100157 set_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrath10faa812008-01-30 13:30:54 +0100158
159 return 1;
160}
161
Oleg Nesterov9bd11902012-09-03 15:24:17 +0200162void set_task_blockstep(struct task_struct *task, bool on)
Oleg Nesterov848e8f52012-08-03 17:31:46 +0200163{
164 unsigned long debugctl;
165
Oleg Nesterov95cf00f2012-08-11 18:06:42 +0200166 /*
167 * Ensure irq/preemption can't change debugctl in between.
168 * Note also that both TIF_BLOCKSTEP and debugctl should
169 * be changed atomically wrt preemption.
Oleg Nesterov9899d112013-01-21 20:48:00 +0100170 *
171 * NOTE: this means that set/clear TIF_BLOCKSTEP is only safe if
172 * task is current or it can't be running, otherwise we can race
173 * with __switch_to_xtra(). We rely on ptrace_freeze_traced() but
174 * PTRACE_KILL is not safe.
Oleg Nesterov95cf00f2012-08-11 18:06:42 +0200175 */
176 local_irq_disable();
Oleg Nesterov848e8f52012-08-03 17:31:46 +0200177 debugctl = get_debugctlmsr();
178 if (on) {
179 debugctl |= DEBUGCTLMSR_BTF;
180 set_tsk_thread_flag(task, TIF_BLOCKSTEP);
181 } else {
182 debugctl &= ~DEBUGCTLMSR_BTF;
183 clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
184 }
Oleg Nesterov95cf00f2012-08-11 18:06:42 +0200185 if (task == current)
186 update_debugctlmsr(debugctl);
187 local_irq_enable();
Oleg Nesterov848e8f52012-08-03 17:31:46 +0200188}
189
Roland McGrath10faa812008-01-30 13:30:54 +0100190/*
Roland McGrath10faa812008-01-30 13:30:54 +0100191 * Enable single or block step.
192 */
193static void enable_step(struct task_struct *child, bool block)
194{
195 /*
196 * Make sure block stepping (BTF) is not enabled unless it should be.
197 * Note that we don't try to worry about any is_setting_trap_flag()
198 * instructions after the first when using block stepping.
Lucas De Marchi0d2eb442011-03-17 16:24:16 -0300199 * So no one should try to use debugger block stepping in a program
Roland McGrath10faa812008-01-30 13:30:54 +0100200 * that uses user-mode single stepping itself.
201 */
Oleg Nesterov848e8f52012-08-03 17:31:46 +0200202 if (enable_single_step(child) && block)
203 set_task_blockstep(child, true);
204 else if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
205 set_task_blockstep(child, false);
Roland McGrath10faa812008-01-30 13:30:54 +0100206}
207
208void user_enable_single_step(struct task_struct *child)
209{
210 enable_step(child, 0);
211}
212
213void user_enable_block_step(struct task_struct *child)
214{
215 enable_step(child, 1);
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100216}
217
218void user_disable_single_step(struct task_struct *child)
219{
Roland McGrath10faa812008-01-30 13:30:54 +0100220 /*
221 * Make sure block stepping (BTF) is disabled.
222 */
Oleg Nesterov848e8f52012-08-03 17:31:46 +0200223 if (test_tsk_thread_flag(child, TIF_BLOCKSTEP))
224 set_task_blockstep(child, false);
Roland McGrath10faa812008-01-30 13:30:54 +0100225
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100226 /* Always clear TIF_SINGLESTEP... */
227 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
228
229 /* But touch TF only if it was set by us.. */
Roland McGrathe1f28772008-01-30 13:30:50 +0100230 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100231 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100232}