blob: 0d2cb363ea752a721cf62372c19c8e89423b3756 [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>
7
Harvey Harrison37cd9cf2008-01-30 13:33:12 +01008unsigned long convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs)
Roland McGrathfa1e03e2008-01-30 13:30:50 +01009{
10 unsigned long addr, seg;
11
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010012 addr = regs->ip;
Roland McGrathfa1e03e2008-01-30 13:30:50 +010013 seg = regs->cs & 0xffff;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010014 if (v8086_mode(regs)) {
Roland McGrath7122ec82008-01-30 13:30:50 +010015 addr = (addr & 0xffff) + (seg << 4);
16 return addr;
17 }
Roland McGrathfa1e03e2008-01-30 13:30:50 +010018
19 /*
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.
24 */
Roland McGrath3f80c1a2008-01-30 13:30:50 +010025 if ((seg & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Roland McGrathfa1e03e2008-01-30 13:30:50 +010026 u32 *desc;
27 unsigned long base;
28
29 seg &= ~7UL;
30
31 mutex_lock(&child->mm->context.lock);
32 if (unlikely((seg >> 3) >= child->mm->context.size))
33 addr = -1L; /* bogus selector, access would fault */
34 else {
35 desc = child->mm->context.ldt + seg;
36 base = ((desc[0] >> 16) |
37 ((desc[1] & 0xff) << 16) |
38 (desc[1] & 0xff000000));
39
40 /* 16-bit code segment? */
41 if (!((desc[1] >> 22) & 1))
42 addr &= 0xffff;
43 addr += base;
44 }
45 mutex_unlock(&child->mm->context.lock);
46 }
47
48 return addr;
49}
50
51static int is_setting_trap_flag(struct task_struct *child, struct pt_regs *regs)
52{
53 int i, copied;
54 unsigned char opcode[15];
Harvey Harrison37cd9cf2008-01-30 13:33:12 +010055 unsigned long addr = convert_ip_to_linear(child, regs);
Roland McGrathfa1e03e2008-01-30 13:30:50 +010056
57 copied = access_process_vm(child, addr, opcode, sizeof(opcode), 0);
58 for (i = 0; i < copied; i++) {
59 switch (opcode[i]) {
60 /* popf and iret */
61 case 0x9d: case 0xcf:
62 return 1;
63
64 /* CHECKME: 64 65 */
65
66 /* opcode and address size prefixes */
67 case 0x66: case 0x67:
68 continue;
69 /* irrelevant prefixes (segment overrides and repeats) */
70 case 0x26: case 0x2e:
71 case 0x36: case 0x3e:
72 case 0x64: case 0x65:
Roland McGrath5f76cb12008-01-30 13:30:50 +010073 case 0xf0: case 0xf2: case 0xf3:
Roland McGrathfa1e03e2008-01-30 13:30:50 +010074 continue;
75
Roland McGrath7122ec82008-01-30 13:30:50 +010076#ifdef CONFIG_X86_64
Roland McGrathfa1e03e2008-01-30 13:30:50 +010077 case 0x40 ... 0x4f:
78 if (regs->cs != __USER_CS)
79 /* 32-bit mode: register increment */
80 return 0;
81 /* 64-bit mode: REX prefix */
82 continue;
Roland McGrath7122ec82008-01-30 13:30:50 +010083#endif
Roland McGrathfa1e03e2008-01-30 13:30:50 +010084
85 /* CHECKME: f2, f3 */
86
87 /*
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.
93 */
94 case 0x9c:
95 default:
96 return 0;
97 }
98 }
99 return 0;
100}
101
Roland McGrath10faa812008-01-30 13:30:54 +0100102/*
103 * Enable single-stepping. Return nonzero if user mode is not using TF itself.
104 */
105static int enable_single_step(struct task_struct *child)
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100106{
107 struct pt_regs *regs = task_pt_regs(child);
Roland McGrath6718d0d2008-07-09 01:07:02 -0700108 unsigned long oflags;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100109
110 /*
111 * Always set TIF_SINGLESTEP - this guarantees that
112 * we single-step system calls etc.. This will also
113 * cause us to set TF when returning to user mode.
114 */
115 set_tsk_thread_flag(child, TIF_SINGLESTEP);
116
Roland McGrath6718d0d2008-07-09 01:07:02 -0700117 oflags = regs->flags;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100118
119 /* Set TF on the kernel stack.. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100120 regs->flags |= X86_EFLAGS_TF;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100121
122 /*
123 * ..but if TF is changed by the instruction we will trace,
124 * don't mark it as being "us" that set it, so that we
125 * won't clear it by hand later.
Roland McGrath6718d0d2008-07-09 01:07:02 -0700126 *
127 * Note that if we don't actually execute the popf because
128 * of a signal arriving right now or suchlike, we will lose
129 * track of the fact that it really was "us" that set it.
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100130 */
Roland McGrath6718d0d2008-07-09 01:07:02 -0700131 if (is_setting_trap_flag(child, regs)) {
132 clear_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrath10faa812008-01-30 13:30:54 +0100133 return 0;
Roland McGrath6718d0d2008-07-09 01:07:02 -0700134 }
135
136 /*
137 * If TF was already set, check whether it was us who set it.
138 * If not, we should never attempt a block step.
139 */
140 if (oflags & X86_EFLAGS_TF)
141 return test_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100142
Roland McGrathe1f28772008-01-30 13:30:50 +0100143 set_tsk_thread_flag(child, TIF_FORCED_TF);
Roland McGrath10faa812008-01-30 13:30:54 +0100144
145 return 1;
146}
147
148/*
149 * Install this value in MSR_IA32_DEBUGCTLMSR whenever child is running.
150 */
151static void write_debugctlmsr(struct task_struct *child, unsigned long val)
152{
Roland McGrath4ba51fd2008-04-03 14:18:55 -0700153 if (child->thread.debugctlmsr == val)
154 return;
155
Roland McGrath10faa812008-01-30 13:30:54 +0100156 child->thread.debugctlmsr = val;
157
158 if (child != current)
159 return;
160
Jan Beulich5b0e5082008-03-10 13:11:17 +0000161 update_debugctlmsr(val);
Roland McGrath10faa812008-01-30 13:30:54 +0100162}
163
164/*
165 * Enable single or block step.
166 */
167static void enable_step(struct task_struct *child, bool block)
168{
169 /*
170 * Make sure block stepping (BTF) is not enabled unless it should be.
171 * Note that we don't try to worry about any is_setting_trap_flag()
172 * instructions after the first when using block stepping.
173 * So noone should try to use debugger block stepping in a program
174 * that uses user-mode single stepping itself.
175 */
176 if (enable_single_step(child) && block) {
177 set_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
Markus Metzgereee3af42008-01-30 13:31:09 +0100178 write_debugctlmsr(child,
179 child->thread.debugctlmsr | DEBUGCTLMSR_BTF);
180 } else {
Roland McGrath4ba51fd2008-04-03 14:18:55 -0700181 write_debugctlmsr(child,
182 child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
Markus Metzgereee3af42008-01-30 13:31:09 +0100183
Roland McGrath4ba51fd2008-04-03 14:18:55 -0700184 if (!child->thread.debugctlmsr)
185 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
Roland McGrath10faa812008-01-30 13:30:54 +0100186 }
187}
188
189void user_enable_single_step(struct task_struct *child)
190{
191 enable_step(child, 0);
192}
193
194void user_enable_block_step(struct task_struct *child)
195{
196 enable_step(child, 1);
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100197}
198
199void user_disable_single_step(struct task_struct *child)
200{
Roland McGrath10faa812008-01-30 13:30:54 +0100201 /*
202 * Make sure block stepping (BTF) is disabled.
203 */
Markus Metzgereee3af42008-01-30 13:31:09 +0100204 write_debugctlmsr(child,
Jan Beulichd032b312008-03-05 08:36:48 +0000205 child->thread.debugctlmsr & ~DEBUGCTLMSR_BTF);
Markus Metzgereee3af42008-01-30 13:31:09 +0100206
207 if (!child->thread.debugctlmsr)
208 clear_tsk_thread_flag(child, TIF_DEBUGCTLMSR);
Roland McGrath10faa812008-01-30 13:30:54 +0100209
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100210 /* Always clear TIF_SINGLESTEP... */
211 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
212
213 /* But touch TF only if it was set by us.. */
Roland McGrathe1f28772008-01-30 13:30:50 +0100214 if (test_and_clear_tsk_thread_flag(child, TIF_FORCED_TF))
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100215 task_pt_regs(child)->flags &= ~X86_EFLAGS_TF;
Roland McGrathfa1e03e2008-01-30 13:30:50 +0100216}