2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 00:54:04 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2012-03-25 15:54:32 +04:00
|
|
|
// Copyright (C) 2001-2012 The Bochs Project
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-01-16 21:18:59 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
|
2007-11-18 02:28:33 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2001-05-24 22:46:34 +04:00
|
|
|
#define NEED_CPU_REG_SHORTCUTS 1
|
2001-04-10 05:04:59 +04:00
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu.h"
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
#define LOG_THIS BX_CPU_THIS_PTR
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2007-12-22 00:14:48 +03:00
|
|
|
//
|
2001-04-10 05:04:59 +04:00
|
|
|
// Notes:
|
|
|
|
//
|
|
|
|
// The high bits of the 32bit eip image are ignored by
|
|
|
|
// the IRET to VM. The high bits of the 32bit esp image
|
|
|
|
// are loaded into ESP. A subsequent push uses
|
|
|
|
// only the low 16bits since it's in VM. In neither case
|
|
|
|
// did a protection fault occur during actual tests. This
|
|
|
|
// is contrary to the Intel docs which claim a #GP for
|
|
|
|
// eIP out of code limits.
|
|
|
|
//
|
|
|
|
// IRET to VM does affect IOPL, IF, VM, and RF
|
2007-12-22 00:14:48 +03:00
|
|
|
//
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
#if BX_CPU_LEVEL >= 3
|
|
|
|
|
2008-03-25 01:35:37 +03:00
|
|
|
void BX_CPU_C::stack_return_to_v86(Bit32u new_eip, Bit32u raw_cs_selector, Bit32u flags32)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2007-12-23 20:21:28 +03:00
|
|
|
Bit32u temp_ESP, new_esp;
|
2001-04-10 05:04:59 +04:00
|
|
|
Bit16u raw_es_selector, raw_ds_selector, raw_fs_selector,
|
|
|
|
raw_gs_selector, raw_ss_selector;
|
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
// Must be 32bit effective opsize, VM is set in upper 16bits of eFLAGS
|
|
|
|
// and CPL = 0 to get here
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// ----------------
|
|
|
|
// | | OLD GS | eSP+32
|
|
|
|
// | | OLD FS | eSP+28
|
|
|
|
// | | OLD DS | eSP+24
|
|
|
|
// | | OLD ES | eSP+20
|
|
|
|
// | | OLD SS | eSP+16
|
|
|
|
// | OLD ESP | eSP+12
|
|
|
|
// | OLD EFLAGS | eSP+8
|
|
|
|
// | | OLD CS | eSP+4
|
|
|
|
// | OLD EIP | eSP+0
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
|
|
|
|
temp_ESP = ESP;
|
|
|
|
else
|
|
|
|
temp_ESP = SP;
|
|
|
|
|
|
|
|
// load SS:ESP from stack
|
2012-03-25 15:54:32 +04:00
|
|
|
new_esp = stack_read_dword(temp_ESP+12);
|
|
|
|
raw_ss_selector = (Bit16u) stack_read_dword(temp_ESP+16);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// load ES,DS,FS,GS from stack
|
2012-03-25 15:54:32 +04:00
|
|
|
raw_es_selector = (Bit16u) stack_read_dword(temp_ESP+20);
|
|
|
|
raw_ds_selector = (Bit16u) stack_read_dword(temp_ESP+24);
|
|
|
|
raw_fs_selector = (Bit16u) stack_read_dword(temp_ESP+28);
|
|
|
|
raw_gs_selector = (Bit16u) stack_read_dword(temp_ESP+32);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
writeEFlags(flags32, EFlagsValidMask);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 03:13:19 +04:00
|
|
|
// load CS:IP from stack; already read and passed as args
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value = raw_cs_selector;
|
2005-10-17 03:13:19 +04:00
|
|
|
EIP = new_eip & 0xffff;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value = raw_es_selector;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value = raw_ds_selector;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value = raw_fs_selector;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value = raw_gs_selector;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value = raw_ss_selector;
|
2007-12-22 00:14:48 +03:00
|
|
|
ESP = new_esp; // full 32 bit are loaded
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
init_v8086_mode();
|
|
|
|
}
|
|
|
|
|
2009-11-02 18:00:47 +03:00
|
|
|
#if BX_CPU_LEVEL >= 5
|
|
|
|
#define BX_CR4_VME_ENABLED (BX_CPU_THIS_PTR cr4.get_VME())
|
|
|
|
#else
|
|
|
|
#define BX_CR4_VME_ENABLED (0)
|
|
|
|
#endif
|
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
void BX_CPU_C::iret16_stack_return_from_v86(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2009-01-10 13:37:23 +03:00
|
|
|
if ((BX_CPU_THIS_PTR get_IOPL() < 3) && (BX_CR4_VME_ENABLED == 0)) {
|
2001-04-10 05:04:59 +04:00
|
|
|
// trap to virtual 8086 monitor
|
2005-10-17 03:13:19 +04:00
|
|
|
BX_DEBUG(("IRET in vm86 with IOPL != 3, VME = 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-03-10 01:01:13 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
Bit16u ip, cs_raw, flags16;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2007-12-20 21:29:42 +03:00
|
|
|
ip = pop_16();
|
|
|
|
cs_raw = pop_16();
|
|
|
|
flags16 = pop_16();
|
2005-10-17 17:06:09 +04:00
|
|
|
|
2009-08-10 19:44:50 +04:00
|
|
|
#if BX_CPU_LEVEL >= 5
|
2009-11-02 18:00:47 +03:00
|
|
|
if (BX_CPU_THIS_PTR cr4.get_VME() && BX_CPU_THIS_PTR get_IOPL() < 3)
|
2005-10-17 17:06:09 +04:00
|
|
|
{
|
2008-02-03 00:46:54 +03:00
|
|
|
if (((flags16 & EFlagsIFMask) && BX_CPU_THIS_PTR get_VIP()) ||
|
2005-10-17 17:06:09 +04:00
|
|
|
(flags16 & EFlagsTFMask))
|
2002-06-27 17:31:54 +04:00
|
|
|
{
|
2007-12-17 00:46:39 +03:00
|
|
|
BX_DEBUG(("iret16_stack_return_from_v86(): #GP(0) in VME mode"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2002-06-27 17:31:54 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
load_seg_reg(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS], cs_raw);
|
2007-12-22 00:14:48 +03:00
|
|
|
EIP = (Bit32u) ip;
|
2005-10-17 17:06:09 +04:00
|
|
|
|
|
|
|
// IF, IOPL unchanged, EFLAGS.VIF = TMP_FLAGS.IF
|
2008-02-03 00:46:54 +03:00
|
|
|
Bit32u changeMask = EFlagsOSZAPCMask | EFlagsTFMask |
|
2005-10-17 17:06:09 +04:00
|
|
|
EFlagsDFMask | EFlagsNTMask | EFlagsVIFMask;
|
|
|
|
Bit32u flags32 = (Bit32u) flags16;
|
2009-08-09 22:40:18 +04:00
|
|
|
if (flags16 & EFlagsIFMask) flags32 |= EFlagsVIFMask;
|
2005-10-17 17:06:09 +04:00
|
|
|
writeEFlags(flags32, changeMask);
|
|
|
|
|
|
|
|
return;
|
2004-07-09 00:15:23 +04:00
|
|
|
}
|
2005-10-17 17:06:09 +04:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
load_seg_reg(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS], cs_raw);
|
2007-12-22 00:14:48 +03:00
|
|
|
EIP = (Bit32u) ip;
|
2005-10-17 17:06:09 +04:00
|
|
|
write_flags(flags16, /*IOPL*/ 0, /*IF*/ 1);
|
|
|
|
}
|
2002-06-27 17:31:54 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
void BX_CPU_C::iret32_stack_return_from_v86(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
if (BX_CPU_THIS_PTR get_IOPL() < 3) {
|
|
|
|
// trap to virtual 8086 monitor
|
|
|
|
BX_DEBUG(("IRET in vm86 with IOPL != 3, VME = 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-10-17 17:06:09 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
Bit32u eip, cs_raw, flags32;
|
|
|
|
// Build a mask of the following bits:
|
|
|
|
// ID,VIP,VIF,AC,VM,RF,x,NT,IOPL,OF,DF,IF,TF,SF,ZF,x,AF,x,PF,x,CF
|
2008-02-03 00:46:54 +03:00
|
|
|
Bit32u change_mask = EFlagsOSZAPCMask | EFlagsTFMask | EFlagsIFMask
|
2005-10-17 17:06:09 +04:00
|
|
|
| EFlagsDFMask | EFlagsNTMask | EFlagsRFMask;
|
|
|
|
|
|
|
|
#if BX_CPU_LEVEL >= 4
|
|
|
|
change_mask |= (EFlagsIDMask | EFlagsACMask); // ID/AC
|
|
|
|
#endif
|
|
|
|
|
2007-12-20 21:29:42 +03:00
|
|
|
eip = pop_32();
|
|
|
|
cs_raw = pop_32();
|
|
|
|
flags32 = pop_32();
|
2005-10-17 17:06:09 +04:00
|
|
|
|
|
|
|
load_seg_reg(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS], (Bit16u) cs_raw);
|
2007-12-22 00:14:48 +03:00
|
|
|
EIP = eip;
|
2005-10-17 17:06:09 +04:00
|
|
|
// VIF, VIP, VM, IOPL unchanged
|
|
|
|
writeEFlags(flags32, change_mask);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2010-03-15 16:22:14 +03:00
|
|
|
int BX_CPU_C::v86_redirect_interrupt(Bit8u vector)
|
2005-10-17 17:06:09 +04:00
|
|
|
{
|
2010-03-15 16:22:14 +03:00
|
|
|
#if BX_CPU_LEVEL >= 5
|
|
|
|
if (BX_CPU_THIS_PTR cr4.get_VME())
|
|
|
|
{
|
|
|
|
bx_address tr_base = BX_CPU_THIS_PTR tr.cache.u.segment.base;
|
|
|
|
if (BX_CPU_THIS_PTR tr.cache.u.segment.limit_scaled < 103) {
|
2012-01-10 00:52:15 +04:00
|
|
|
BX_ERROR(("v86_redirect_interrupt(): TR.limit < 103 in VME"));
|
2010-03-15 16:22:14 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
|
|
|
}
|
2005-10-17 17:06:09 +04:00
|
|
|
|
2010-03-15 16:22:14 +03:00
|
|
|
Bit32u io_base = system_read_word(tr_base + 102), offset = io_base - 32 + (vector >> 3);
|
|
|
|
if (offset > BX_CPU_THIS_PTR tr.cache.u.segment.limit_scaled) {
|
2012-01-10 00:52:15 +04:00
|
|
|
BX_ERROR(("v86_redirect_interrupt(): failed to fetch VME redirection bitmap"));
|
2010-03-15 16:22:14 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
|
|
|
}
|
2005-10-17 17:06:09 +04:00
|
|
|
|
2010-03-15 16:22:14 +03:00
|
|
|
Bit8u vme_redirection_bitmap = system_read_byte(tr_base + offset);
|
|
|
|
if (!(vme_redirection_bitmap & (1 << (vector & 7))))
|
|
|
|
{
|
|
|
|
// redirect interrupt through virtual-mode idt
|
|
|
|
Bit16u temp_flags = (Bit16u) read_eflags();
|
|
|
|
|
|
|
|
Bit16u temp_CS = system_read_word(vector*4 + 2);
|
|
|
|
Bit16u temp_IP = system_read_word(vector*4);
|
|
|
|
|
|
|
|
if (BX_CPU_THIS_PTR get_IOPL() < 3) {
|
|
|
|
temp_flags |= EFlagsIOPLMask;
|
|
|
|
if (BX_CPU_THIS_PTR get_VIF())
|
|
|
|
temp_flags |= EFlagsIFMask;
|
|
|
|
else
|
|
|
|
temp_flags &= ~EFlagsIFMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit16u old_IP = IP;
|
|
|
|
Bit16u old_CS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
|
|
|
|
|
|
|
push_16(temp_flags);
|
|
|
|
// push return address onto new stack
|
|
|
|
push_16(old_CS);
|
|
|
|
push_16(old_IP);
|
|
|
|
|
|
|
|
load_seg_reg(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS], (Bit16u) temp_CS);
|
|
|
|
EIP = temp_IP;
|
|
|
|
|
|
|
|
BX_CPU_THIS_PTR clear_TF();
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
|
|
|
if (BX_CPU_THIS_PTR get_IOPL() == 3)
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
else
|
|
|
|
BX_CPU_THIS_PTR clear_VIF();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// interrupt is not redirected or VME is OFF
|
|
|
|
if (BX_CPU_THIS_PTR get_IOPL() < 3)
|
|
|
|
{
|
2012-01-10 00:52:15 +04:00
|
|
|
BX_DEBUG(("v86_redirect_interrupt(): interrupt cannot be redirected, generate #GP(0)"));
|
2010-03-15 16:22:14 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-10-17 17:06:09 +04:00
|
|
|
}
|
|
|
|
|
2010-03-15 16:22:14 +03:00
|
|
|
return 0;
|
2005-10-17 17:06:09 +04:00
|
|
|
}
|
|
|
|
|
2005-03-10 01:01:13 +03:00
|
|
|
void BX_CPU_C::init_v8086_mode(void)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2009-11-02 18:00:47 +03:00
|
|
|
for(unsigned sreg = 0; sreg < 6; sreg++) {
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.segment = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
|
|
|
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.u.segment.base =
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].selector.value << 4;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.u.segment.limit_scaled = 0xffff;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.u.segment.g = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.u.segment.d_b = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].cache.u.segment.avl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[sreg].selector.rpl = 3;
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2009-01-13 22:00:30 +03:00
|
|
|
handleCpuModeChange();
|
2006-01-16 22:22:28 +03:00
|
|
|
|
2012-03-25 23:07:17 +04:00
|
|
|
#if BX_CPU_LEVEL >= 4
|
2010-04-22 21:51:37 +04:00
|
|
|
handleAlignmentCheck(/* CPL change */);
|
2007-11-21 00:22:03 +03:00
|
|
|
#endif
|
2012-03-25 15:54:32 +04:00
|
|
|
|
|
|
|
invalidate_stack_cache();
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* BX_CPU_LEVEL >= 3 */
|