9pfs: factor out virtio_9p_push_and_notify
[qemu/ar7.git] / target-i386 / bpt_helper.c
blobdac1b1a360002813e283731e232449686f82715f
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 "cpu.h"
21 #include "exec/helper-proto.h"
24 #ifndef CONFIG_USER_ONLY
25 static inline bool hw_local_breakpoint_enabled(unsigned long dr7, int index)
27 return (dr7 >> (index * 2)) & 1;
30 static inline bool hw_global_breakpoint_enabled(unsigned long dr7, int index)
32 return (dr7 >> (index * 2)) & 2;
35 static inline bool hw_breakpoint_enabled(unsigned long dr7, int index)
37 return hw_global_breakpoint_enabled(dr7, index) ||
38 hw_local_breakpoint_enabled(dr7, index);
41 static inline int hw_breakpoint_type(unsigned long dr7, int index)
43 return (dr7 >> (DR7_TYPE_SHIFT + (index * 4))) & 3;
46 static inline int hw_breakpoint_len(unsigned long dr7, int index)
48 int len = ((dr7 >> (DR7_LEN_SHIFT + (index * 4))) & 3);
49 return (len == 2) ? 8 : len + 1;
52 static int hw_breakpoint_insert(CPUX86State *env, int index)
54 CPUState *cs = CPU(x86_env_get_cpu(env));
55 target_ulong dr7 = env->dr[7];
56 target_ulong drN = env->dr[index];
57 int err = 0;
59 switch (hw_breakpoint_type(dr7, index)) {
60 case DR7_TYPE_BP_INST:
61 if (hw_breakpoint_enabled(dr7, index)) {
62 err = cpu_breakpoint_insert(cs, drN, BP_CPU,
63 &env->cpu_breakpoint[index]);
65 break;
67 case DR7_TYPE_IO_RW:
68 /* Notice when we should enable calls to bpt_io. */
69 return hw_breakpoint_enabled(env->dr[7], index)
70 ? HF_IOBPT_MASK : 0;
72 case DR7_TYPE_DATA_WR:
73 if (hw_breakpoint_enabled(dr7, index)) {
74 err = cpu_watchpoint_insert(cs, drN,
75 hw_breakpoint_len(dr7, index),
76 BP_CPU | BP_MEM_WRITE,
77 &env->cpu_watchpoint[index]);
79 break;
81 case DR7_TYPE_DATA_RW:
82 if (hw_breakpoint_enabled(dr7, index)) {
83 err = cpu_watchpoint_insert(cs, drN,
84 hw_breakpoint_len(dr7, index),
85 BP_CPU | BP_MEM_ACCESS,
86 &env->cpu_watchpoint[index]);
88 break;
90 if (err) {
91 env->cpu_breakpoint[index] = NULL;
93 return 0;
96 static void hw_breakpoint_remove(CPUX86State *env, int index)
98 CPUState *cs = CPU(x86_env_get_cpu(env));
100 switch (hw_breakpoint_type(env->dr[7], index)) {
101 case DR7_TYPE_BP_INST:
102 if (env->cpu_breakpoint[index]) {
103 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
104 env->cpu_breakpoint[index] = NULL;
106 break;
108 case DR7_TYPE_DATA_WR:
109 case DR7_TYPE_DATA_RW:
110 if (env->cpu_breakpoint[index]) {
111 cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
112 env->cpu_breakpoint[index] = NULL;
114 break;
116 case DR7_TYPE_IO_RW:
117 /* HF_IOBPT_MASK cleared elsewhere. */
118 break;
122 void cpu_x86_update_dr7(CPUX86State *env, uint32_t new_dr7)
124 target_ulong old_dr7 = env->dr[7];
125 int iobpt = 0;
126 int i;
128 new_dr7 |= DR7_FIXED_1;
130 /* If nothing is changing except the global/local enable bits,
131 then we can make the change more efficient. */
132 if (((old_dr7 ^ new_dr7) & ~0xff) == 0) {
133 /* Fold the global and local enable bits together into the
134 global fields, then xor to show which registers have
135 changed collective enable state. */
136 int mod = ((old_dr7 | old_dr7 * 2) ^ (new_dr7 | new_dr7 * 2)) & 0xff;
138 for (i = 0; i < DR7_MAX_BP; i++) {
139 if ((mod & (2 << i * 2)) && !hw_breakpoint_enabled(new_dr7, i)) {
140 hw_breakpoint_remove(env, i);
143 env->dr[7] = new_dr7;
144 for (i = 0; i < DR7_MAX_BP; i++) {
145 if (mod & (2 << i * 2) && hw_breakpoint_enabled(new_dr7, i)) {
146 iobpt |= hw_breakpoint_insert(env, i);
147 } else if (hw_breakpoint_type(new_dr7, i) == DR7_TYPE_IO_RW
148 && hw_breakpoint_enabled(new_dr7, i)) {
149 iobpt |= HF_IOBPT_MASK;
152 } else {
153 for (i = 0; i < DR7_MAX_BP; i++) {
154 hw_breakpoint_remove(env, i);
156 env->dr[7] = new_dr7;
157 for (i = 0; i < DR7_MAX_BP; i++) {
158 iobpt |= hw_breakpoint_insert(env, i);
162 env->hflags = (env->hflags & ~HF_IOBPT_MASK) | iobpt;
165 static bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update)
167 target_ulong dr6;
168 int reg;
169 bool hit_enabled = false;
171 dr6 = env->dr[6] & ~0xf;
172 for (reg = 0; reg < DR7_MAX_BP; reg++) {
173 bool bp_match = false;
174 bool wp_match = false;
176 switch (hw_breakpoint_type(env->dr[7], reg)) {
177 case DR7_TYPE_BP_INST:
178 if (env->dr[reg] == env->eip) {
179 bp_match = true;
181 break;
182 case DR7_TYPE_DATA_WR:
183 case DR7_TYPE_DATA_RW:
184 if (env->cpu_watchpoint[reg] &&
185 env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT) {
186 wp_match = true;
188 break;
189 case DR7_TYPE_IO_RW:
190 break;
192 if (bp_match || wp_match) {
193 dr6 |= 1 << reg;
194 if (hw_breakpoint_enabled(env->dr[7], reg)) {
195 hit_enabled = true;
200 if (hit_enabled || force_dr6_update) {
201 env->dr[6] = dr6;
204 return hit_enabled;
207 void breakpoint_handler(CPUState *cs)
209 X86CPU *cpu = X86_CPU(cs);
210 CPUX86State *env = &cpu->env;
211 CPUBreakpoint *bp;
213 if (cs->watchpoint_hit) {
214 if (cs->watchpoint_hit->flags & BP_CPU) {
215 cs->watchpoint_hit = NULL;
216 if (check_hw_breakpoints(env, false)) {
217 raise_exception(env, EXCP01_DB);
218 } else {
219 cpu_resume_from_signal(cs, NULL);
222 } else {
223 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
224 if (bp->pc == env->eip) {
225 if (bp->flags & BP_CPU) {
226 check_hw_breakpoints(env, true);
227 raise_exception(env, EXCP01_DB);
229 break;
234 #endif
236 void helper_single_step(CPUX86State *env)
238 #ifndef CONFIG_USER_ONLY
239 check_hw_breakpoints(env, true);
240 env->dr[6] |= DR6_BS;
241 #endif
242 raise_exception(env, EXCP01_DB);
245 void helper_set_dr(CPUX86State *env, int reg, target_ulong t0)
247 #ifndef CONFIG_USER_ONLY
248 switch (reg) {
249 case 0: case 1: case 2: case 3:
250 if (hw_breakpoint_enabled(env->dr[7], reg)
251 && hw_breakpoint_type(env->dr[7], reg) != DR7_TYPE_IO_RW) {
252 hw_breakpoint_remove(env, reg);
253 env->dr[reg] = t0;
254 hw_breakpoint_insert(env, reg);
255 } else {
256 env->dr[reg] = t0;
258 return;
259 case 4:
260 if (env->cr[4] & CR4_DE_MASK) {
261 break;
263 /* fallthru */
264 case 6:
265 env->dr[6] = t0 | DR6_FIXED_1;
266 return;
267 case 5:
268 if (env->cr[4] & CR4_DE_MASK) {
269 break;
271 /* fallthru */
272 case 7:
273 cpu_x86_update_dr7(env, t0);
274 return;
276 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
277 #endif
280 target_ulong helper_get_dr(CPUX86State *env, int reg)
282 switch (reg) {
283 case 0: case 1: case 2: case 3: case 6: case 7:
284 return env->dr[reg];
285 case 4:
286 if (env->cr[4] & CR4_DE_MASK) {
287 break;
288 } else {
289 return env->dr[6];
291 case 5:
292 if (env->cr[4] & CR4_DE_MASK) {
293 break;
294 } else {
295 return env->dr[7];
298 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
301 /* Check if Port I/O is trapped by a breakpoint. */
302 void helper_bpt_io(CPUX86State *env, uint32_t port,
303 uint32_t size, target_ulong next_eip)
305 #ifndef CONFIG_USER_ONLY
306 target_ulong dr7 = env->dr[7];
307 int i, hit = 0;
309 for (i = 0; i < DR7_MAX_BP; ++i) {
310 if (hw_breakpoint_type(dr7, i) == DR7_TYPE_IO_RW
311 && hw_breakpoint_enabled(dr7, i)) {
312 int bpt_len = hw_breakpoint_len(dr7, i);
313 if (port + size - 1 >= env->dr[i]
314 && port <= env->dr[i] + bpt_len - 1) {
315 hit |= 1 << i;
320 if (hit) {
321 env->dr[6] = (env->dr[6] & ~0xf) | hit;
322 env->eip = next_eip;
323 raise_exception(env, EXCP01_DB);
325 #endif