ivshmem: Drop ivshmem_event() stub
[qemu/kevin.git] / target-i386 / bpt_helper.c
blobf47df19988793c52f8346adc7051c4455118632f
1 /*
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"
21 #include "cpu.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];
58 int err = 0;
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]);
66 break;
68 case DR7_TYPE_IO_RW:
69 /* Notice when we should enable calls to bpt_io. */
70 return hw_breakpoint_enabled(env->dr[7], index)
71 ? HF_IOBPT_MASK : 0;
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]);
80 break;
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]);
89 break;
91 if (err) {
92 env->cpu_breakpoint[index] = NULL;
94 return 0;
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;
107 break;
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;
115 break;
117 case DR7_TYPE_IO_RW:
118 /* HF_IOBPT_MASK cleared elsewhere. */
119 break;
123 void cpu_x86_update_dr7(CPUX86State *env, uint32_t new_dr7)
125 target_ulong old_dr7 = env->dr[7];
126 int iobpt = 0;
127 int i;
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;
153 } else {
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)
168 target_ulong dr6;
169 int reg;
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) {
180 bp_match = true;
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;
189 break;
190 case DR7_TYPE_IO_RW:
191 break;
193 if (bp_match || wp_match) {
194 dr6 |= 1 << reg;
195 if (hw_breakpoint_enabled(env->dr[7], reg)) {
196 hit_enabled = true;
201 if (hit_enabled || force_dr6_update) {
202 env->dr[6] = dr6;
205 return hit_enabled;
208 void breakpoint_handler(CPUState *cs)
210 X86CPU *cpu = X86_CPU(cs);
211 CPUX86State *env = &cpu->env;
212 CPUBreakpoint *bp;
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);
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);
230 break;
235 #endif
237 void helper_single_step(CPUX86State *env)
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);
246 void helper_set_dr(CPUX86State *env, int reg, target_ulong t0)
248 #ifndef CONFIG_USER_ONLY
249 switch (reg) {
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);
254 env->dr[reg] = t0;
255 hw_breakpoint_insert(env, reg);
256 } else {
257 env->dr[reg] = t0;
259 return;
260 case 4:
261 if (env->cr[4] & CR4_DE_MASK) {
262 break;
264 /* fallthru */
265 case 6:
266 env->dr[6] = t0 | DR6_FIXED_1;
267 return;
268 case 5:
269 if (env->cr[4] & CR4_DE_MASK) {
270 break;
272 /* fallthru */
273 case 7:
274 cpu_x86_update_dr7(env, t0);
275 return;
277 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
278 #endif
281 target_ulong helper_get_dr(CPUX86State *env, int reg)
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];
292 case 5:
293 if (env->cr[4] & CR4_DE_MASK) {
294 break;
295 } else {
296 return env->dr[7];
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];
308 int i, hit = 0;
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;
321 if (hit) {
322 env->dr[6] = (env->dr[6] & ~0xf) | hit;
323 env->eip = next_eip;
324 raise_exception(env, EXCP01_DB);
326 #endif