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/>.
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 void hw_breakpoint_insert(CPUX86State
*env
, int index
)
54 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
55 int type
= 0, err
= 0;
57 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
58 case DR7_TYPE_BP_INST
:
59 if (hw_breakpoint_enabled(env
->dr
[7], index
)) {
60 err
= cpu_breakpoint_insert(cs
, env
->dr
[index
], BP_CPU
,
61 &env
->cpu_breakpoint
[index
]);
64 case DR7_TYPE_DATA_WR
:
65 type
= BP_CPU
| BP_MEM_WRITE
;
68 /* No support for I/O watchpoints yet */
70 case DR7_TYPE_DATA_RW
:
71 type
= BP_CPU
| BP_MEM_ACCESS
;
76 err
= cpu_watchpoint_insert(cs
, env
->dr
[index
],
77 hw_breakpoint_len(env
->dr
[7], index
),
78 type
, &env
->cpu_watchpoint
[index
]);
82 env
->cpu_breakpoint
[index
] = NULL
;
86 static void hw_breakpoint_remove(CPUX86State
*env
, int index
)
90 if (!env
->cpu_breakpoint
[index
]) {
93 cs
= CPU(x86_env_get_cpu(env
));
94 switch (hw_breakpoint_type(env
->dr
[7], index
)) {
95 case DR7_TYPE_BP_INST
:
96 if (hw_breakpoint_enabled(env
->dr
[7], index
)) {
97 cpu_breakpoint_remove_by_ref(cs
, env
->cpu_breakpoint
[index
]);
100 case DR7_TYPE_DATA_WR
:
101 case DR7_TYPE_DATA_RW
:
102 cpu_watchpoint_remove_by_ref(cs
, env
->cpu_watchpoint
[index
]);
105 /* No support for I/O watchpoints yet */
110 void cpu_x86_update_dr7(CPUX86State
*env
, uint32_t new_dr7
)
112 target_ulong old_dr7
= env
->dr
[7];
115 new_dr7
|= DR7_FIXED_1
;
117 /* If nothing is changing except the global/local enable bits,
118 then we can make the change more efficient. */
119 if (((old_dr7
^ new_dr7
) & ~0xff) == 0) {
120 /* Fold the global and local enable bits together into the
121 global fields, then xor to show which registers have
122 changed collective enable state. */
123 int mod
= ((old_dr7
| old_dr7
* 2) ^ (new_dr7
| new_dr7
* 2)) & 0xff;
125 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
126 if ((mod
& (2 << i
* 2)) && !hw_breakpoint_enabled(new_dr7
, i
)) {
127 hw_breakpoint_remove(env
, i
);
130 env
->dr
[7] = new_dr7
;
131 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
132 if (mod
& (2 << i
* 2) && hw_breakpoint_enabled(new_dr7
, i
)) {
133 hw_breakpoint_insert(env
, i
);
137 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
138 hw_breakpoint_remove(env
, i
);
140 env
->dr
[7] = new_dr7
;
141 for (i
= 0; i
< DR7_MAX_BP
; i
++) {
142 hw_breakpoint_insert(env
, i
);
147 static bool check_hw_breakpoints(CPUX86State
*env
, bool force_dr6_update
)
151 bool hit_enabled
= false;
153 dr6
= env
->dr
[6] & ~0xf;
154 for (reg
= 0; reg
< DR7_MAX_BP
; reg
++) {
155 bool bp_match
= false;
156 bool wp_match
= false;
158 switch (hw_breakpoint_type(env
->dr
[7], reg
)) {
159 case DR7_TYPE_BP_INST
:
160 if (env
->dr
[reg
] == env
->eip
) {
164 case DR7_TYPE_DATA_WR
:
165 case DR7_TYPE_DATA_RW
:
166 if (env
->cpu_watchpoint
[reg
] &&
167 env
->cpu_watchpoint
[reg
]->flags
& BP_WATCHPOINT_HIT
) {
174 if (bp_match
|| wp_match
) {
176 if (hw_breakpoint_enabled(env
->dr
[7], reg
)) {
182 if (hit_enabled
|| force_dr6_update
) {
189 void breakpoint_handler(CPUState
*cs
)
191 X86CPU
*cpu
= X86_CPU(cs
);
192 CPUX86State
*env
= &cpu
->env
;
195 if (cs
->watchpoint_hit
) {
196 if (cs
->watchpoint_hit
->flags
& BP_CPU
) {
197 cs
->watchpoint_hit
= NULL
;
198 if (check_hw_breakpoints(env
, false)) {
199 raise_exception(env
, EXCP01_DB
);
201 cpu_resume_from_signal(cs
, NULL
);
205 QTAILQ_FOREACH(bp
, &cs
->breakpoints
, entry
) {
206 if (bp
->pc
== env
->eip
) {
207 if (bp
->flags
& BP_CPU
) {
208 check_hw_breakpoints(env
, true);
209 raise_exception(env
, EXCP01_DB
);
218 void helper_single_step(CPUX86State
*env
)
220 #ifndef CONFIG_USER_ONLY
221 check_hw_breakpoints(env
, true);
222 env
->dr
[6] |= DR6_BS
;
224 raise_exception(env
, EXCP01_DB
);
227 void helper_movl_drN_T0(CPUX86State
*env
, int reg
, target_ulong t0
)
229 #ifndef CONFIG_USER_ONLY
231 hw_breakpoint_remove(env
, reg
);
233 hw_breakpoint_insert(env
, reg
);
234 } else if (reg
== 7) {
235 cpu_x86_update_dr7(env
, t0
);