2 * i386 breakpoint helpers
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "exec/helper-proto.h"
25 #ifndef CONFIG_USER_ONLY
26 static inline bool hw_local_breakpoint_enabled(unsigned long dr7, int index)
28 return (dr7 >> (index * 2)) & 1;
31 static inline bool hw_global_breakpoint_enabled(unsigned long dr7, int index)
33 return (dr7 >> (index * 2)) & 2;
36 static inline bool hw_breakpoint_enabled(unsigned long dr7, int index)
38 return hw_global_breakpoint_enabled(dr7, index) ||
39 hw_local_breakpoint_enabled(dr7, index);
42 static inline int hw_breakpoint_type(unsigned long dr7, int index)
44 return (dr7 >> (DR7_TYPE_SHIFT + (index * 4))) & 3;
47 static inline int hw_breakpoint_len(unsigned long dr7, int index)
49 int len = ((dr7 >> (DR7_LEN_SHIFT + (index * 4))) & 3);
50 return (len == 2) ? 8 : len + 1;
53 static int hw_breakpoint_insert(CPUX86State *env, int index)
55 CPUState *cs = CPU(x86_env_get_cpu(env));
56 target_ulong dr7 = env->dr[7];
57 target_ulong drN = env->dr[index];
60 switch (hw_breakpoint_type(dr7, index)) {
61 case DR7_TYPE_BP_INST:
62 if (hw_breakpoint_enabled(dr7, index)) {
63 err = cpu_breakpoint_insert(cs, drN, BP_CPU,
64 &env->cpu_breakpoint[index]);
69 /* Notice when we should enable calls to bpt_io. */
70 return hw_breakpoint_enabled(env->dr[7], index)
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]);
82 case DR7_TYPE_DATA_RW:
83 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]);
92 env->cpu_breakpoint[index] = NULL;
97 static void hw_breakpoint_remove(CPUX86State *env, int index)
99 CPUState *cs = CPU(x86_env_get_cpu(env));
101 switch (hw_breakpoint_type(env->dr[7], index)) {
102 case DR7_TYPE_BP_INST:
103 if (env->cpu_breakpoint[index]) {
104 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
105 env->cpu_breakpoint[index] = NULL;
109 case DR7_TYPE_DATA_WR:
110 case DR7_TYPE_DATA_RW:
111 if (env->cpu_breakpoint[index]) {
112 cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
113 env->cpu_breakpoint[index] = NULL;
118 /* HF_IOBPT_MASK cleared elsewhere. */
123 void cpu_x86_update_dr7(CPUX86State *env, uint32_t new_dr7)
125 target_ulong old_dr7 = env->dr[7];
129 new_dr7 |= DR7_FIXED_1;
131 /* 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;
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);
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)) {
147 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;
154 for (i = 0; i < DR7_MAX_BP; i++) {
155 hw_breakpoint_remove(env, i);
157 env->dr[7] = new_dr7;
158 for (i = 0; i < DR7_MAX_BP; i++) {
159 iobpt |= hw_breakpoint_insert(env, i);
163 env->hflags = (env->hflags & ~HF_IOBPT_MASK) | iobpt;
166 static bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update)
170 bool hit_enabled = false;
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;
177 switch (hw_breakpoint_type(env->dr[7], reg)) {
178 case DR7_TYPE_BP_INST:
179 if (env->dr[reg] == env->eip) {
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) {
193 if (bp_match || wp_match) {
195 if (hw_breakpoint_enabled(env->dr[7], reg)) {
201 if (hit_enabled || force_dr6_update) {
208 void breakpoint_handler(CPUState *cs)
210 X86CPU *cpu = X86_CPU(cs);
211 CPUX86State *env = &cpu->env;
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);
220 cpu_resume_from_signal(cs, NULL);
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);
237 void helper_single_step(CPUX86State *env)
239 #ifndef CONFIG_USER_ONLY
240 check_hw_breakpoints(env, true);
241 env->dr[6] |= DR6_BS;
243 raise_exception(env, EXCP01_DB);
246 void helper_set_dr(CPUX86State *env, int reg, target_ulong t0)
248 #ifndef CONFIG_USER_ONLY
250 case 0: case 1: case 2: case 3:
251 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);
255 hw_breakpoint_insert(env, reg);
261 if (env->cr[4] & CR4_DE_MASK) {
266 env->dr[6] = t0 | DR6_FIXED_1;
269 if (env->cr[4] & CR4_DE_MASK) {
274 cpu_x86_update_dr7(env, t0);
277 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
281 target_ulong helper_get_dr(CPUX86State *env, int reg)
284 case 0: case 1: case 2: case 3: case 6: case 7:
287 if (env->cr[4] & CR4_DE_MASK) {
293 if (env->cr[4] & CR4_DE_MASK) {
299 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
302 /* Check if Port I/O is trapped by a breakpoint. */
303 void helper_bpt_io(CPUX86State *env, uint32_t port,
304 uint32_t size, target_ulong next_eip)
306 #ifndef CONFIG_USER_ONLY
307 target_ulong dr7 = env->dr[7];
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) {
322 env->dr[6] = (env->dr[6] & ~0xf) | hit;
324 raise_exception(env, EXCP01_DB);