1 /////////////////////////////////////////////////////////////////////////
2 // $Id: flag_ctrl.cc,v 1.26 2006/06/09 22:29:07 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2002 MandrakeSoft S.A.
9 // 75002 Paris - France
10 // http://www.linux-mandrake.com/
11 // http://www.mandrakesoft.com/
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2 of the License, or (at your option) any later version.
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #define NEED_CPU_REG_SHORTCUTS 1
32 #define LOG_THIS BX_CPU_THIS_PTR
35 void BX_CPU_C::SAHF(bxInstruction_c
*i
)
37 set_SF((AH
& 0x80) >> 7);
38 set_ZF((AH
& 0x40) >> 6);
39 set_AF((AH
& 0x10) >> 4);
41 set_PF((AH
& 0x04) >> 2);
44 void BX_CPU_C::LAHF(bxInstruction_c
*i
)
46 AH
= (get_SF() ? 0x80 : 0) |
47 (get_ZF() ? 0x40 : 0) |
48 (get_AF() ? 0x10 : 0) |
49 (get_PF() ? 0x04 : 0) |
51 (get_CF() ? 0x01 : 0);
54 void BX_CPU_C::CLC(bxInstruction_c
*i
)
59 void BX_CPU_C::STC(bxInstruction_c
*i
)
64 void BX_CPU_C::CLI(bxInstruction_c
*i
)
66 Bit32u IOPL
= BX_CPU_THIS_PTR
get_IOPL();
73 if (BX_CPU_THIS_PTR cr4
.get_PVI() && (cpl
== 3))
76 BX_CPU_THIS_PTR
clear_VIF();
84 BX_DEBUG(("CLI: IOPL < CPL in protected mode"));
85 exception(BX_GP_EXCEPTION
, 0, 0);
90 else if (v8086_mode())
94 if (CR4_VME_ENABLED
) {
95 BX_CPU_THIS_PTR
clear_VIF();
99 BX_DEBUG(("CLI: IOPL != 3 in v8086 mode"));
100 exception(BX_GP_EXCEPTION
, 0, 0);
106 BX_CPU_THIS_PTR
clear_IF();
109 void BX_CPU_C::STI(bxInstruction_c
*i
)
111 Bit32u IOPL
= BX_CPU_THIS_PTR
get_IOPL();
114 #if BX_CPU_LEVEL >= 2
115 if (protected_mode())
118 if (BX_CPU_THIS_PTR cr4
.get_PVI())
120 if (cpl
== 3 && IOPL
< 3) {
121 if (! BX_CPU_THIS_PTR
get_VIP())
123 BX_CPU_THIS_PTR
assert_VIF();
127 BX_DEBUG(("STI: #GP(0) in VME mode"));
128 exception(BX_GP_EXCEPTION
, 0, 0);
133 BX_DEBUG(("STI: CPL > IOPL in protected mode"));
134 exception(BX_GP_EXCEPTION
, 0, 0);
137 #if BX_CPU_LEVEL >= 3
138 else if (v8086_mode())
142 if (CR4_VME_ENABLED
&& BX_CPU_THIS_PTR
get_VIP() == 0)
144 BX_CPU_THIS_PTR
assert_VIF();
148 BX_DEBUG(("STI: IOPL != 3 in v8086 mode"));
149 exception(BX_GP_EXCEPTION
, 0, 0);
155 if (!BX_CPU_THIS_PTR
get_IF()) {
156 BX_CPU_THIS_PTR
assert_IF();
157 BX_CPU_THIS_PTR inhibit_mask
|= BX_INHIBIT_INTERRUPTS
;
158 BX_CPU_THIS_PTR async_event
= 1;
162 void BX_CPU_C::CLD(bxInstruction_c
*i
)
164 BX_CPU_THIS_PTR
clear_DF();
167 void BX_CPU_C::STD(bxInstruction_c
*i
)
169 BX_CPU_THIS_PTR
assert_DF();
172 void BX_CPU_C::CMC(bxInstruction_c
*i
)
177 void BX_CPU_C::PUSHF_Fw(bxInstruction_c
*i
)
179 Bit16u flags
= read_flags();
182 if ((BX_CPU_THIS_PTR
get_IOPL() < 3) && (CR4_VME_ENABLED
== 0)) {
183 BX_DEBUG(("PUSHFW: #GP(0) in v8086 (no VME) mode"));
184 exception(BX_GP_EXCEPTION
, 0, 0);
188 if (CR4_VME_ENABLED
&& BX_CPU_THIS_PTR
get_IOPL() < 3) {
189 flags
|= EFlagsIOPLMask
;
190 if (BX_CPU_THIS_PTR
get_VIF())
191 flags
|= EFlagsIFMask
;
193 flags
&= ~EFlagsIFMask
;
201 void BX_CPU_C::POPF_Fw(bxInstruction_c
*i
)
203 // Build a mask of the following bits:
204 // x,NT,IOPL,OF,DF,IF,TF,SF,ZF,x,AF,x,PF,x,CF
205 Bit32u changeMask
= EFlagsOSZAPCMask
| EFlagsTFMask
| EFlagsDFMask
;
206 #if BX_CPU_LEVEL >= 3
207 changeMask
|= EFlagsNTMask
; // NT could be modified
211 if (protected_mode()) {
214 changeMask
|= EFlagsIOPLMask
;
215 if (CPL
<= BX_CPU_THIS_PTR
get_IOPL())
216 changeMask
|= EFlagsIFMask
;
218 else if (v8086_mode()) {
219 if ((BX_CPU_THIS_PTR
get_IOPL() < 3) && (CR4_VME_ENABLED
== 0)) {
220 BX_DEBUG(("POPFW: #GP(0) in v8086 (no VME) mode"));
221 exception(BX_GP_EXCEPTION
, 0, 0);
226 if (CR4_VME_ENABLED
&& BX_CPU_THIS_PTR
get_IOPL() < 3) {
227 if (((flags16
& EFlagsIFMask
) && BX_CPU_THIS_PTR
get_VIP()) ||
228 (flags16
& EFlagsTFMask
))
230 BX_DEBUG(("POPFW: #GP(0) in VME mode"));
231 exception(BX_GP_EXCEPTION
, 0, 0);
234 // IF, IOPL unchanged, EFLAGS.VIF = TMP_FLAGS.IF
235 changeMask
|= EFlagsVIFMask
;
236 Bit32u flags32
= (Bit32u
) flags16
;
237 if (BX_CPU_THIS_PTR
get_IF()) flags32
|= EFlagsVIFMask
;
238 writeEFlags(flags32
, changeMask
);
242 changeMask
|= EFlagsIFMask
;
246 // All non-reserved flags can be modified
247 changeMask
|= (EFlagsIOPLMask
| EFlagsIFMask
);
250 writeEFlags((Bit32u
) flags16
, changeMask
);
253 #if BX_CPU_LEVEL >= 3
255 void BX_CPU_C::PUSHF_Fd(bxInstruction_c
*i
)
257 if (v8086_mode() && (BX_CPU_THIS_PTR
get_IOPL()<3)) {
258 BX_DEBUG(("PUSHFD: #GP(0) in v8086 mode"));
259 exception(BX_GP_EXCEPTION
, 0, 0);
263 // VM & RF flags cleared in image stored on the stack
264 push_32(read_eflags() & 0x00fcffff);
267 void BX_CPU_C::POPF_Fd(bxInstruction_c
*i
)
269 // Build a mask of the following bits:
270 // ID,VIP,VIF,AC,VM,RF,x,NT,IOPL,OF,DF,IF,TF,SF,ZF,x,AF,x,PF,x,CF
271 Bit32u changeMask
= EFlagsOSZAPCMask
| EFlagsTFMask
|
272 EFlagsDFMask
| EFlagsNTMask
| EFlagsRFMask
;
273 #if BX_CPU_LEVEL >= 4
274 changeMask
|= (EFlagsIDMask
| EFlagsACMask
); // ID/AC
278 if (protected_mode()) {
280 // IOPL changed only if (CPL == 0),
281 // IF changed only if (CPL <= EFLAGS.IOPL),
282 // VIF, VIP, VM are unaffected
284 changeMask
|= EFlagsIOPLMask
;
285 if (CPL
<= BX_CPU_THIS_PTR
get_IOPL())
286 changeMask
|= EFlagsIFMask
;
288 else if (v8086_mode()) {
289 if (BX_CPU_THIS_PTR
get_IOPL() < 3) {
290 BX_DEBUG(("POPFD: #GP(0) in v8086 mode"));
291 exception(BX_GP_EXCEPTION
, 0, 0);
295 // v8086-mode: VM, IOPL, VIP, VIF are unaffected
296 changeMask
|= EFlagsIFMask
;
300 // VIF, VIP, VM are unaffected
301 changeMask
|= (EFlagsIOPLMask
| EFlagsIFMask
);
304 writeEFlags(flags32
, changeMask
);
307 #if BX_SUPPORT_X86_64
308 void BX_CPU_C::PUSHF_Fq(bxInstruction_c
*i
)
310 // VM & RF flags cleared in image stored on the stack
311 push_64(read_eflags() & 0x00fcffff);
314 void BX_CPU_C::POPF_Fq(bxInstruction_c
*i
)
316 // Build a mask of the following bits:
317 // ID,VIP,VIF,AC,VM,RF,x,NT,IOPL,OF,DF,IF,TF,SF,ZF,x,AF,x,PF,x,CF
318 Bit32u changeMask
= EFlagsOSZAPCMask
| EFlagsTFMask
| EFlagsDFMask
319 | EFlagsNTMask
| EFlagsRFMask
| EFlagsACMask
323 BX_ASSERT (protected_mode());
326 Bit32u flags32
= (Bit32u
) flags64
;
328 changeMask
|= EFlagsIOPLMask
;
329 if (CPL
<= BX_CPU_THIS_PTR
get_IOPL())
330 changeMask
|= EFlagsIFMask
;
332 // VIF, VIP, VM are unaffected
333 writeEFlags(flags32
, changeMask
);
337 #endif // BX_CPU_LEVEL >= 3
339 void BX_CPU_C::SALC(bxInstruction_c
*i
)