blob: b3efdc77ec3c4de98a9b4772418b1844f558de3c [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"
Paolo Bonzini63c91552016-03-15 13:18:37 +010022#include "exec/exec-all.h"
Richard Hendersonba4b5c62015-09-15 11:45:06 -070023#include "exec/helper-proto.h"
24
25
Richard Henderson93d00d02015-09-15 11:45:08 -070026#ifndef CONFIG_USER_ONLY
Richard Henderson696ad9e2015-09-15 11:45:10 -070027static inline bool hw_local_breakpoint_enabled(unsigned long dr7, int index)
28{
29 return (dr7 >> (index * 2)) & 1;
30}
31
32static inline bool hw_global_breakpoint_enabled(unsigned long dr7, int index)
33{
34 return (dr7 >> (index * 2)) & 2;
35
36}
37static inline bool hw_breakpoint_enabled(unsigned long dr7, int index)
38{
39 return hw_global_breakpoint_enabled(dr7, index) ||
40 hw_local_breakpoint_enabled(dr7, index);
41}
42
43static inline int hw_breakpoint_type(unsigned long dr7, int index)
44{
45 return (dr7 >> (DR7_TYPE_SHIFT + (index * 4))) & 3;
46}
47
48static inline int hw_breakpoint_len(unsigned long dr7, int index)
49{
50 int len = ((dr7 >> (DR7_LEN_SHIFT + (index * 4))) & 3);
51 return (len == 2) ? 8 : len + 1;
52}
53
Eduardo Habkost5223a942015-10-19 15:14:35 -020054static int hw_breakpoint_insert(CPUX86State *env, int index)
Richard Hendersonba4b5c62015-09-15 11:45:06 -070055{
56 CPUState *cs = CPU(x86_env_get_cpu(env));
Eduardo Habkost5223a942015-10-19 15:14:35 -020057 target_ulong dr7 = env->dr[7];
58 target_ulong drN = env->dr[index];
59 int err = 0;
Richard Hendersonba4b5c62015-09-15 11:45:06 -070060
Eduardo Habkost5223a942015-10-19 15:14:35 -020061 switch (hw_breakpoint_type(dr7, index)) {
Richard Hendersonba4b5c62015-09-15 11:45:06 -070062 case DR7_TYPE_BP_INST:
Eduardo Habkost5223a942015-10-19 15:14:35 -020063 if (hw_breakpoint_enabled(dr7, index)) {
64 err = cpu_breakpoint_insert(cs, drN, BP_CPU,
Richard Hendersonba4b5c62015-09-15 11:45:06 -070065 &env->cpu_breakpoint[index]);
66 }
67 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -020068
Richard Hendersonba4b5c62015-09-15 11:45:06 -070069 case DR7_TYPE_IO_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -020070 /* Notice when we should enable calls to bpt_io. */
71 return hw_breakpoint_enabled(env->dr[7], index)
72 ? HF_IOBPT_MASK : 0;
73
74 case DR7_TYPE_DATA_WR:
75 if (hw_breakpoint_enabled(dr7, index)) {
76 err = cpu_watchpoint_insert(cs, drN,
77 hw_breakpoint_len(dr7, index),
78 BP_CPU | BP_MEM_WRITE,
79 &env->cpu_watchpoint[index]);
80 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070081 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -020082
Richard Hendersonba4b5c62015-09-15 11:45:06 -070083 case DR7_TYPE_DATA_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -020084 if (hw_breakpoint_enabled(dr7, index)) {
85 err = cpu_watchpoint_insert(cs, drN,
86 hw_breakpoint_len(dr7, index),
87 BP_CPU | BP_MEM_ACCESS,
88 &env->cpu_watchpoint[index]);
89 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070090 break;
91 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -070092 if (err) {
93 env->cpu_breakpoint[index] = NULL;
94 }
Eduardo Habkost5223a942015-10-19 15:14:35 -020095 return 0;
Richard Hendersonba4b5c62015-09-15 11:45:06 -070096}
97
Richard Henderson93d00d02015-09-15 11:45:08 -070098static void hw_breakpoint_remove(CPUX86State *env, int index)
Richard Hendersonba4b5c62015-09-15 11:45:06 -070099{
Eduardo Habkost5223a942015-10-19 15:14:35 -0200100 CPUState *cs = CPU(x86_env_get_cpu(env));
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700101
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700102 switch (hw_breakpoint_type(env->dr[7], index)) {
103 case DR7_TYPE_BP_INST:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200104 if (env->cpu_breakpoint[index]) {
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700105 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
Eduardo Habkost5223a942015-10-19 15:14:35 -0200106 env->cpu_breakpoint[index] = NULL;
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700107 }
108 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -0200109
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700110 case DR7_TYPE_DATA_WR:
111 case DR7_TYPE_DATA_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200112 if (env->cpu_breakpoint[index]) {
113 cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
114 env->cpu_breakpoint[index] = NULL;
115 }
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700116 break;
Eduardo Habkost5223a942015-10-19 15:14:35 -0200117
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700118 case DR7_TYPE_IO_RW:
Eduardo Habkost5223a942015-10-19 15:14:35 -0200119 /* HF_IOBPT_MASK cleared elsewhere. */
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700120 break;
121 }
122}
123
Richard Henderson93d00d02015-09-15 11:45:08 -0700124void cpu_x86_update_dr7(CPUX86State *env, uint32_t new_dr7)
125{
Richard Henderson36eb6e02015-09-15 11:45:09 -0700126 target_ulong old_dr7 = env->dr[7];
Eduardo Habkost5223a942015-10-19 15:14:35 -0200127 int iobpt = 0;
Richard Henderson93d00d02015-09-15 11:45:08 -0700128 int i;
129
Eduardo Habkost90553302015-10-08 17:10:27 -0300130 new_dr7 |= DR7_FIXED_1;
131
Richard Henderson36eb6e02015-09-15 11:45:09 -0700132 /* If nothing is changing except the global/local enable bits,
133 then we can make the change more efficient. */
134 if (((old_dr7 ^ new_dr7) & ~0xff) == 0) {
135 /* Fold the global and local enable bits together into the
136 global fields, then xor to show which registers have
137 changed collective enable state. */
138 int mod = ((old_dr7 | old_dr7 * 2) ^ (new_dr7 | new_dr7 * 2)) & 0xff;
139
140 for (i = 0; i < DR7_MAX_BP; i++) {
141 if ((mod & (2 << i * 2)) && !hw_breakpoint_enabled(new_dr7, i)) {
142 hw_breakpoint_remove(env, i);
143 }
144 }
145 env->dr[7] = new_dr7;
146 for (i = 0; i < DR7_MAX_BP; i++) {
147 if (mod & (2 << i * 2) && hw_breakpoint_enabled(new_dr7, i)) {
Eduardo Habkost5223a942015-10-19 15:14:35 -0200148 iobpt |= hw_breakpoint_insert(env, i);
149 } else if (hw_breakpoint_type(new_dr7, i) == DR7_TYPE_IO_RW
150 && hw_breakpoint_enabled(new_dr7, i)) {
151 iobpt |= HF_IOBPT_MASK;
Richard Henderson36eb6e02015-09-15 11:45:09 -0700152 }
153 }
154 } else {
155 for (i = 0; i < DR7_MAX_BP; i++) {
156 hw_breakpoint_remove(env, i);
157 }
158 env->dr[7] = new_dr7;
159 for (i = 0; i < DR7_MAX_BP; i++) {
Eduardo Habkost5223a942015-10-19 15:14:35 -0200160 iobpt |= hw_breakpoint_insert(env, i);
Richard Henderson36eb6e02015-09-15 11:45:09 -0700161 }
Richard Henderson93d00d02015-09-15 11:45:08 -0700162 }
Eduardo Habkost5223a942015-10-19 15:14:35 -0200163
164 env->hflags = (env->hflags & ~HF_IOBPT_MASK) | iobpt;
Richard Henderson93d00d02015-09-15 11:45:08 -0700165}
Richard Henderson93d00d02015-09-15 11:45:08 -0700166
Richard Hendersondd941cd2015-09-15 11:45:07 -0700167static bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update)
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700168{
169 target_ulong dr6;
170 int reg;
171 bool hit_enabled = false;
172
173 dr6 = env->dr[6] & ~0xf;
174 for (reg = 0; reg < DR7_MAX_BP; reg++) {
175 bool bp_match = false;
176 bool wp_match = false;
177
178 switch (hw_breakpoint_type(env->dr[7], reg)) {
179 case DR7_TYPE_BP_INST:
180 if (env->dr[reg] == env->eip) {
181 bp_match = true;
182 }
183 break;
184 case DR7_TYPE_DATA_WR:
185 case DR7_TYPE_DATA_RW:
186 if (env->cpu_watchpoint[reg] &&
187 env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT) {
188 wp_match = true;
189 }
190 break;
191 case DR7_TYPE_IO_RW:
192 break;
193 }
194 if (bp_match || wp_match) {
195 dr6 |= 1 << reg;
196 if (hw_breakpoint_enabled(env->dr[7], reg)) {
197 hit_enabled = true;
198 }
199 }
200 }
201
202 if (hit_enabled || force_dr6_update) {
203 env->dr[6] = dr6;
204 }
205
206 return hit_enabled;
207}
208
209void breakpoint_handler(CPUState *cs)
210{
211 X86CPU *cpu = X86_CPU(cs);
212 CPUX86State *env = &cpu->env;
213 CPUBreakpoint *bp;
214
215 if (cs->watchpoint_hit) {
216 if (cs->watchpoint_hit->flags & BP_CPU) {
217 cs->watchpoint_hit = NULL;
218 if (check_hw_breakpoints(env, false)) {
219 raise_exception(env, EXCP01_DB);
220 } else {
Peter Maydell6886b982016-05-17 15:18:04 +0100221 cpu_loop_exit_noexc(cs);
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700222 }
223 }
224 } else {
225 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
226 if (bp->pc == env->eip) {
227 if (bp->flags & BP_CPU) {
228 check_hw_breakpoints(env, true);
229 raise_exception(env, EXCP01_DB);
230 }
231 break;
232 }
233 }
234 }
235}
Richard Henderson696ad9e2015-09-15 11:45:10 -0700236#endif
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700237
238void helper_single_step(CPUX86State *env)
239{
240#ifndef CONFIG_USER_ONLY
241 check_hw_breakpoints(env, true);
242 env->dr[6] |= DR6_BS;
243#endif
244 raise_exception(env, EXCP01_DB);
245}
246
Doug Evansc52ab082016-12-06 23:06:30 +0000247void helper_rechecking_single_step(CPUX86State *env)
248{
249 if ((env->eflags & TF_MASK) != 0) {
250 helper_single_step(env);
251 }
252}
253
Richard Hendersond0052332015-09-15 11:45:13 -0700254void helper_set_dr(CPUX86State *env, int reg, target_ulong t0)
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700255{
256#ifndef CONFIG_USER_ONLY
Richard Hendersond0052332015-09-15 11:45:13 -0700257 switch (reg) {
258 case 0: case 1: case 2: case 3:
Richard Henderson7525b552015-09-15 11:45:11 -0700259 if (hw_breakpoint_enabled(env->dr[7], reg)
260 && hw_breakpoint_type(env->dr[7], reg) != DR7_TYPE_IO_RW) {
261 hw_breakpoint_remove(env, reg);
262 env->dr[reg] = t0;
263 hw_breakpoint_insert(env, reg);
264 } else {
265 env->dr[reg] = t0;
266 }
Richard Hendersond0052332015-09-15 11:45:13 -0700267 return;
268 case 4:
269 if (env->cr[4] & CR4_DE_MASK) {
270 break;
271 }
272 /* fallthru */
273 case 6:
Eduardo Habkost462f8ed2015-10-07 17:19:18 -0300274 env->dr[6] = t0 | DR6_FIXED_1;
Richard Hendersond0052332015-09-15 11:45:13 -0700275 return;
276 case 5:
277 if (env->cr[4] & CR4_DE_MASK) {
278 break;
279 }
280 /* fallthru */
281 case 7:
Richard Henderson93d00d02015-09-15 11:45:08 -0700282 cpu_x86_update_dr7(env, t0);
Richard Hendersond0052332015-09-15 11:45:13 -0700283 return;
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700284 }
Richard Hendersond0052332015-09-15 11:45:13 -0700285 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
Richard Hendersonba4b5c62015-09-15 11:45:06 -0700286#endif
287}
Eduardo Habkost5223a942015-10-19 15:14:35 -0200288
Richard Hendersond0052332015-09-15 11:45:13 -0700289target_ulong helper_get_dr(CPUX86State *env, int reg)
290{
291 switch (reg) {
292 case 0: case 1: case 2: case 3: case 6: case 7:
293 return env->dr[reg];
294 case 4:
295 if (env->cr[4] & CR4_DE_MASK) {
296 break;
297 } else {
298 return env->dr[6];
299 }
300 case 5:
301 if (env->cr[4] & CR4_DE_MASK) {
302 break;
303 } else {
304 return env->dr[7];
305 }
306 }
307 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
308}
309
Eduardo Habkost5223a942015-10-19 15:14:35 -0200310/* Check if Port I/O is trapped by a breakpoint. */
311void helper_bpt_io(CPUX86State *env, uint32_t port,
312 uint32_t size, target_ulong next_eip)
313{
314#ifndef CONFIG_USER_ONLY
315 target_ulong dr7 = env->dr[7];
316 int i, hit = 0;
317
318 for (i = 0; i < DR7_MAX_BP; ++i) {
319 if (hw_breakpoint_type(dr7, i) == DR7_TYPE_IO_RW
320 && hw_breakpoint_enabled(dr7, i)) {
321 int bpt_len = hw_breakpoint_len(dr7, i);
322 if (port + size - 1 >= env->dr[i]
323 && port <= env->dr[i] + bpt_len - 1) {
324 hit |= 1 << i;
325 }
326 }
327 }
328
329 if (hit) {
330 env->dr[6] = (env->dr[6] & ~0xf) | hit;
331 env->eip = next_eip;
332 raise_exception(env, EXCP01_DB);
333 }
334#endif
335}