blob: f47df19988793c52f8346adc7051c4455118632f [file] [log] [blame]
Richard Hendersonba4b5c62015-09-15 11:45:06 -07001/*
2 * i386 breakpoint helpers
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydellb6a0aa02016-01-26 18:17:03 +000020#include "qemu/osdep.h"
Richard Hendersonba4b5c62015-09-15 11:45:06 -070021#include "cpu.h"
22#include "exec/helper-proto.h"
23
24
Richard Henderson93d00d02015-09-15 11:45:08 -070025#ifndef CONFIG_USER_ONLY
Richard Henderson696ad9e2015-09-15 11:45:10 -070026static inline bool hw_local_breakpoint_enabled(unsigned long dr7, int index)
27{
28 return (dr7 >> (index * 2)) & 1;
29}
30
31static inline bool hw_global_breakpoint_enabled(unsigned long dr7, int index)
32{
33 return (dr7 >> (index * 2)) & 2;
34
35}
36static inline bool hw_breakpoint_enabled(unsigned long dr7, int index)
37{
38 return hw_global_breakpoint_enabled(dr7, index) ||
39 hw_local_breakpoint_enabled(dr7, index);
40}
41
42static inline int hw_breakpoint_type(unsigned long dr7, int index)
43{
44 return (dr7 >> (DR7_TYPE_SHIFT + (index * 4))) & 3;
45}
46
47static inline int hw_breakpoint_len(unsigned long dr7, int index)
48{
49 int len = ((dr7 >> (DR7_LEN_SHIFT + (index * 4))) & 3);
50 return (len == 2) ? 8 : len + 1;
51}
52
Eduardo Habkost5223a942015-10-19 15:14:35 -020053static int hw_breakpoint_insert(CPUX86State *env, int index)
Richard Hendersonba4b5c62015-09-15 11:45:06 -070054{
55 CPUState *cs = CPU(x86_env_get_cpu(env));
Eduardo Habkost5223a942015-10-19 15:14:35 -020056 target_ulong dr7 = env->dr[7];
57 target_ulong drN = env->dr[index];
58 int err = 0;
Richard Hendersonba4b5c62015-09-15 11:45:06 -070059
Eduardo Habkost5223a942015-10-19 15:14:35 -020060 switch (hw_breakpoint_type(dr7, index)) {
Richard Hendersonba4b5c62015-09-15 11:45:06 -070061 case DR7_TYPE_BP_INST:
Eduardo Habkost5223a942015-10-19 15:14:35 -020062 if (hw_breakpoint_enabled(dr7, index)) {
63 err = cpu_breakpoint_insert(cs, drN, BP_CPU,
Richard Hendersonba4b5c62015-09-15 11:45:06 -070064 &env->cpu_breakpoint[index]);
65 }
66 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -020067
Richard Hendersonba4b5c62015-09-15 11:45:06 -070068 case DR7_TYPE_IO_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -020069 /* Notice when we should enable calls to bpt_io. */
70 return hw_breakpoint_enabled(env->dr[7], index)
71 ? HF_IOBPT_MASK : 0;
72
73 case DR7_TYPE_DATA_WR:
74 if (hw_breakpoint_enabled(dr7, index)) {
75 err = cpu_watchpoint_insert(cs, drN,
76 hw_breakpoint_len(dr7, index),
77 BP_CPU | BP_MEM_WRITE,
78 &env->cpu_watchpoint[index]);
79 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070080 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -020081
Richard Hendersonba4b5c62015-09-15 11:45:06 -070082 case DR7_TYPE_DATA_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -020083 if (hw_breakpoint_enabled(dr7, index)) {
84 err = cpu_watchpoint_insert(cs, drN,
85 hw_breakpoint_len(dr7, index),
86 BP_CPU | BP_MEM_ACCESS,
87 &env->cpu_watchpoint[index]);
88 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070089 break;
90 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070091 if (err) {
92 env->cpu_breakpoint[index] = NULL;
93 }
Eduardo Habkost5223a942015-10-19 15:14:35 -020094 return 0;
Richard Hendersonba4b5c62015-09-15 11:45:06 -070095}
96
Richard Henderson93d00d02015-09-15 11:45:08 -070097static void hw_breakpoint_remove(CPUX86State *env, int index)
Richard Hendersonba4b5c62015-09-15 11:45:06 -070098{
Eduardo Habkost5223a942015-10-19 15:14:35 -020099 CPUState *cs = CPU(x86_env_get_cpu(env));
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700100
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700101 switch (hw_breakpoint_type(env->dr[7], index)) {
102 case DR7_TYPE_BP_INST:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200103 if (env->cpu_breakpoint[index]) {
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700104 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
Eduardo Habkost5223a942015-10-19 15:14:35 -0200105 env->cpu_breakpoint[index] = NULL;
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700106 }
107 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -0200108
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700109 case DR7_TYPE_DATA_WR:
110 case DR7_TYPE_DATA_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200111 if (env->cpu_breakpoint[index]) {
112 cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
113 env->cpu_breakpoint[index] = NULL;
114 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700115 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -0200116
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700117 case DR7_TYPE_IO_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200118 /* HF_IOBPT_MASK cleared elsewhere. */
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700119 break;
120 }
121}
122
Richard Henderson93d00d02015-09-15 11:45:08 -0700123void cpu_x86_update_dr7(CPUX86State *env, uint32_t new_dr7)
124{
Richard Henderson36eb6e02015-09-15 11:45:09 -0700125 target_ulong old_dr7 = env->dr[7];
Eduardo Habkost5223a942015-10-19 15:14:35 -0200126 int iobpt = 0;
Richard Henderson93d00d02015-09-15 11:45:08 -0700127 int i;
128
Eduardo Habkost90553302015-10-08 17:10:27 -0300129 new_dr7 |= DR7_FIXED_1;
130
Richard Henderson36eb6e02015-09-15 11:45:09 -0700131 /* If nothing is changing except the global/local enable bits,
132 then we can make the change more efficient. */
133 if (((old_dr7 ^ new_dr7) & ~0xff) == 0) {
134 /* Fold the global and local enable bits together into the
135 global fields, then xor to show which registers have
136 changed collective enable state. */
137 int mod = ((old_dr7 | old_dr7 * 2) ^ (new_dr7 | new_dr7 * 2)) & 0xff;
138
139 for (i = 0; i < DR7_MAX_BP; i++) {
140 if ((mod & (2 << i * 2)) && !hw_breakpoint_enabled(new_dr7, i)) {
141 hw_breakpoint_remove(env, i);
142 }
143 }
144 env->dr[7] = new_dr7;
145 for (i = 0; i < DR7_MAX_BP; i++) {
146 if (mod & (2 << i * 2) && hw_breakpoint_enabled(new_dr7, i)) {
Eduardo Habkost5223a942015-10-19 15:14:35 -0200147 iobpt |= hw_breakpoint_insert(env, i);
148 } else if (hw_breakpoint_type(new_dr7, i) == DR7_TYPE_IO_RW
149 && hw_breakpoint_enabled(new_dr7, i)) {
150 iobpt |= HF_IOBPT_MASK;
Richard Henderson36eb6e02015-09-15 11:45:09 -0700151 }
152 }
153 } else {
154 for (i = 0; i < DR7_MAX_BP; i++) {
155 hw_breakpoint_remove(env, i);
156 }
157 env->dr[7] = new_dr7;
158 for (i = 0; i < DR7_MAX_BP; i++) {
Eduardo Habkost5223a942015-10-19 15:14:35 -0200159 iobpt |= hw_breakpoint_insert(env, i);
Richard Henderson36eb6e02015-09-15 11:45:09 -0700160 }
Richard Henderson93d00d02015-09-15 11:45:08 -0700161 }
Eduardo Habkost5223a942015-10-19 15:14:35 -0200162
163 env->hflags = (env->hflags & ~HF_IOBPT_MASK) | iobpt;
Richard Henderson93d00d02015-09-15 11:45:08 -0700164}
Richard Henderson93d00d02015-09-15 11:45:08 -0700165
Richard Hendersondd941cd2015-09-15 11:45:07 -0700166static bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update)
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700167{
168 target_ulong dr6;
169 int reg;
170 bool hit_enabled = false;
171
172 dr6 = env->dr[6] & ~0xf;
173 for (reg = 0; reg < DR7_MAX_BP; reg++) {
174 bool bp_match = false;
175 bool wp_match = false;
176
177 switch (hw_breakpoint_type(env->dr[7], reg)) {
178 case DR7_TYPE_BP_INST:
179 if (env->dr[reg] == env->eip) {
180 bp_match = true;
181 }
182 break;
183 case DR7_TYPE_DATA_WR:
184 case DR7_TYPE_DATA_RW:
185 if (env->cpu_watchpoint[reg] &&
186 env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT) {
187 wp_match = true;
188 }
189 break;
190 case DR7_TYPE_IO_RW:
191 break;
192 }
193 if (bp_match || wp_match) {
194 dr6 |= 1 << reg;
195 if (hw_breakpoint_enabled(env->dr[7], reg)) {
196 hit_enabled = true;
197 }
198 }
199 }
200
201 if (hit_enabled || force_dr6_update) {
202 env->dr[6] = dr6;
203 }
204
205 return hit_enabled;
206}
207
208void breakpoint_handler(CPUState *cs)
209{
210 X86CPU *cpu = X86_CPU(cs);
211 CPUX86State *env = &cpu->env;
212 CPUBreakpoint *bp;
213
214 if (cs->watchpoint_hit) {
215 if (cs->watchpoint_hit->flags & BP_CPU) {
216 cs->watchpoint_hit = NULL;
217 if (check_hw_breakpoints(env, false)) {
218 raise_exception(env, EXCP01_DB);
219 } else {
220 cpu_resume_from_signal(cs, NULL);
221 }
222 }
223 } else {
224 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
225 if (bp->pc == env->eip) {
226 if (bp->flags & BP_CPU) {
227 check_hw_breakpoints(env, true);
228 raise_exception(env, EXCP01_DB);
229 }
230 break;
231 }
232 }
233 }
234}
Richard Henderson696ad9e2015-09-15 11:45:10 -0700235#endif
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700236
237void helper_single_step(CPUX86State *env)
238{
239#ifndef CONFIG_USER_ONLY
240 check_hw_breakpoints(env, true);
241 env->dr[6] |= DR6_BS;
242#endif
243 raise_exception(env, EXCP01_DB);
244}
245
Richard Hendersond0052332015-09-15 11:45:13 -0700246void helper_set_dr(CPUX86State *env, int reg, target_ulong t0)
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700247{
248#ifndef CONFIG_USER_ONLY
Richard Hendersond0052332015-09-15 11:45:13 -0700249 switch (reg) {
250 case 0: case 1: case 2: case 3:
Richard Henderson7525b552015-09-15 11:45:11 -0700251 if (hw_breakpoint_enabled(env->dr[7], reg)
252 && hw_breakpoint_type(env->dr[7], reg) != DR7_TYPE_IO_RW) {
253 hw_breakpoint_remove(env, reg);
254 env->dr[reg] = t0;
255 hw_breakpoint_insert(env, reg);
256 } else {
257 env->dr[reg] = t0;
258 }
Richard Hendersond0052332015-09-15 11:45:13 -0700259 return;
260 case 4:
261 if (env->cr[4] & CR4_DE_MASK) {
262 break;
263 }
264 /* fallthru */
265 case 6:
Eduardo Habkost462f8ed2015-10-07 17:19:18 -0300266 env->dr[6] = t0 | DR6_FIXED_1;
Richard Hendersond0052332015-09-15 11:45:13 -0700267 return;
268 case 5:
269 if (env->cr[4] & CR4_DE_MASK) {
270 break;
271 }
272 /* fallthru */
273 case 7:
Richard Henderson93d00d02015-09-15 11:45:08 -0700274 cpu_x86_update_dr7(env, t0);
Richard Hendersond0052332015-09-15 11:45:13 -0700275 return;
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700276 }
Richard Hendersond0052332015-09-15 11:45:13 -0700277 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700278#endif
279}
Eduardo Habkost5223a942015-10-19 15:14:35 -0200280
Richard Hendersond0052332015-09-15 11:45:13 -0700281target_ulong helper_get_dr(CPUX86State *env, int reg)
282{
283 switch (reg) {
284 case 0: case 1: case 2: case 3: case 6: case 7:
285 return env->dr[reg];
286 case 4:
287 if (env->cr[4] & CR4_DE_MASK) {
288 break;
289 } else {
290 return env->dr[6];
291 }
292 case 5:
293 if (env->cr[4] & CR4_DE_MASK) {
294 break;
295 } else {
296 return env->dr[7];
297 }
298 }
299 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
300}
301
Eduardo Habkost5223a942015-10-19 15:14:35 -0200302/* Check if Port I/O is trapped by a breakpoint. */
303void helper_bpt_io(CPUX86State *env, uint32_t port,
304 uint32_t size, target_ulong next_eip)
305{
306#ifndef CONFIG_USER_ONLY
307 target_ulong dr7 = env->dr[7];
308 int i, hit = 0;
309
310 for (i = 0; i < DR7_MAX_BP; ++i) {
311 if (hw_breakpoint_type(dr7, i) == DR7_TYPE_IO_RW
312 && hw_breakpoint_enabled(dr7, i)) {
313 int bpt_len = hw_breakpoint_len(dr7, i);
314 if (port + size - 1 >= env->dr[i]
315 && port <= env->dr[i] + bpt_len - 1) {
316 hit |= 1 << i;
317 }
318 }
319 }
320
321 if (hit) {
322 env->dr[6] = (env->dr[6] & ~0xf) | hit;
323 env->eip = next_eip;
324 raise_exception(env, EXCP01_DB);
325 }
326#endif
327}