2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2010-04-03 09:59:07 +04:00
|
|
|
// $Id: proc_ctrl.cc,v 1.329 2010-04-03 05:59:07 sshwarts Exp $
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2010-03-26 00:33:07 +03:00
|
|
|
// Copyright (C) 2001-2010 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-10-11 22:12:00 +04: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"
|
2010-03-12 14:35:34 +03:00
|
|
|
#include "param_names.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
|
|
|
|
2002-09-15 03:17:55 +04:00
|
|
|
#if BX_SUPPORT_X86_64==0
|
|
|
|
// Make life easier for merging code.
|
|
|
|
#define RAX EAX
|
|
|
|
#define RCX ECX
|
|
|
|
#define RDX EDX
|
2008-04-15 18:41:50 +04:00
|
|
|
#define RIP EIP
|
2002-09-15 03:17:55 +04:00
|
|
|
#endif
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::UndefinedOpcode(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2008-08-09 23:18:09 +04:00
|
|
|
BX_DEBUG(("UndefinedOpcode: b1 = 0x%02x causes #UD exception", i->b1()));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::NOP(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2003-11-14 00:17:31 +03:00
|
|
|
// No operation.
|
2002-10-16 21:37:35 +04:00
|
|
|
}
|
|
|
|
|
2009-01-27 23:29:05 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::PAUSE(bxInstruction_c *i)
|
|
|
|
{
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_PAUSE(i);
|
|
|
|
#endif
|
2009-01-27 23:29:05 +03:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::PREFETCH(bxInstruction_c *i)
|
2002-10-16 21:37:35 +04:00
|
|
|
{
|
2008-01-10 22:37:56 +03:00
|
|
|
#if BX_INSTRUMENTATION
|
2008-08-08 13:22:49 +04:00
|
|
|
bx_address eaddr = BX_CPU_CALL_METHODR(i->ResolveModrm, (i));
|
|
|
|
BX_INSTR_PREFETCH_HINT(BX_CPU_ID, i->nnn(), i->seg(), eaddr);
|
2008-01-10 22:37:56 +03:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-04-08 00:47:32 +04:00
|
|
|
//
|
2008-02-03 00:46:54 +03:00
|
|
|
// The shutdown state is very similar to the state following the exection
|
|
|
|
// if HLT instruction. In this mode the processor stops executing
|
|
|
|
// instructions until #NMI, #SMI, #RESET or #INIT is received. If
|
|
|
|
// shutdown occurs why in NMI interrupt handler or in SMM, a hardware
|
2006-04-08 00:47:32 +04:00
|
|
|
// reset must be used to restart the processor execution.
|
|
|
|
//
|
2006-04-05 21:31:35 +04:00
|
|
|
void BX_CPU_C::shutdown(void)
|
|
|
|
{
|
|
|
|
BX_PANIC(("Entering to shutdown state still not implemented"));
|
2006-04-08 00:47:32 +04:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
|
|
|
|
// artificial trap bit, why use another variable.
|
2009-01-29 23:27:57 +03:00
|
|
|
BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_HLT;
|
2006-04-08 00:47:32 +04:00
|
|
|
BX_CPU_THIS_PTR async_event = 1; // so processor knows to check
|
|
|
|
// Execution of this instruction completes. The processor
|
|
|
|
// will remain in a halt state until one of the above conditions
|
|
|
|
// is met.
|
|
|
|
|
|
|
|
BX_INSTR_HLT(BX_CPU_ID);
|
|
|
|
|
2008-04-16 01:29:18 +04:00
|
|
|
#if BX_DEBUGGER
|
|
|
|
bx_dbg_halt(BX_CPU_ID);
|
|
|
|
#endif
|
|
|
|
|
2008-02-03 00:46:54 +03:00
|
|
|
#if BX_USE_IDLE_HACK
|
2007-10-11 22:12:00 +04:00
|
|
|
bx_gui->sim_is_idle();
|
|
|
|
#endif
|
2006-04-08 00:47:32 +04:00
|
|
|
|
|
|
|
longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop
|
2006-04-05 21:31:35 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::HLT(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2005-09-15 00:01:42 +04:00
|
|
|
if (!real_mode() && CPL!=0) {
|
2007-04-26 00:14:15 +04:00
|
|
|
BX_DEBUG(("HLT: %s priveledge check failed, CPL=%d, generate #GP(0)",
|
2006-08-11 21:23:36 +04:00
|
|
|
cpu_mode_string(BX_CPU_THIS_PTR cpu_mode), CPL));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-04-08 00:47:32 +04:00
|
|
|
if (! BX_CPU_THIS_PTR get_IF()) {
|
2005-11-04 18:15:02 +03:00
|
|
|
BX_INFO(("WARNING: HLT instruction with IF=0!"));
|
2003-11-14 00:17:31 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_HLT(i);
|
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
// stops instruction execution and places the processor in a
|
|
|
|
// HALT state. An enabled interrupt, NMI, or reset will resume
|
|
|
|
// execution. If interrupt (including NMI) is used to resume
|
|
|
|
// execution after HLT, the saved CS:eIP points to instruction
|
|
|
|
// following HLT.
|
|
|
|
|
|
|
|
// artificial trap bit, why use another variable.
|
2009-01-29 23:27:57 +03:00
|
|
|
BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_HLT;
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR async_event = 1; // so processor knows to check
|
|
|
|
// Execution of this instruction completes. The processor
|
|
|
|
// will remain in a halt state until one of the above conditions
|
|
|
|
// is met.
|
2001-11-12 03:45:09 +03:00
|
|
|
|
2005-07-07 22:40:35 +04:00
|
|
|
BX_INSTR_HLT(BX_CPU_ID);
|
|
|
|
|
2008-03-24 00:39:01 +03:00
|
|
|
#if BX_DEBUGGER
|
|
|
|
bx_dbg_halt(BX_CPU_ID);
|
|
|
|
#endif
|
|
|
|
|
2008-02-03 00:46:54 +03:00
|
|
|
#if BX_USE_IDLE_HACK
|
2007-10-11 22:12:00 +04:00
|
|
|
bx_gui->sim_is_idle();
|
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::CLTS(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2005-09-15 00:01:42 +04:00
|
|
|
if (!real_mode() && CPL!=0) {
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_ERROR(("CLTS: priveledge check failed, generate #GP(0)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
if(VMexit_CLTS(i)) return;
|
|
|
|
#endif
|
|
|
|
|
2007-07-09 19:16:14 +04:00
|
|
|
BX_CPU_THIS_PTR cr0.set_TS(0);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2009-01-23 12:26:24 +03:00
|
|
|
/* 0F 08 */
|
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::INVD(bxInstruction_c *i)
|
|
|
|
{
|
|
|
|
if (!real_mode() && CPL!=0) {
|
|
|
|
BX_ERROR(("INVD: priveledge check failed, generate #GP(0)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2009-01-23 12:26:24 +03:00
|
|
|
}
|
|
|
|
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
if (BX_CPU_THIS_PTR in_vmx_guest) {
|
|
|
|
BX_ERROR(("VMEXIT: INVD in VMX non-root operation"));
|
|
|
|
VMexit(i, VMX_VMEXIT_INVD, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-23 12:26:24 +03:00
|
|
|
invalidate_prefetch_q();
|
|
|
|
|
|
|
|
BX_DEBUG(("INVD: Flush internal caches !"));
|
|
|
|
BX_INSTR_CACHE_CNTRL(BX_CPU_ID, BX_INSTR_INVD);
|
|
|
|
|
|
|
|
flushICaches();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 0F 09 */
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::WBINVD(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2006-10-04 23:08:40 +04:00
|
|
|
if (!real_mode() && CPL!=0) {
|
2008-09-19 23:18:57 +04:00
|
|
|
BX_ERROR(("INVD/WBINVD: priveledge check failed, generate #GP(0)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2005-10-18 22:07:52 +04:00
|
|
|
|
2010-03-15 18:48:01 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
2010-03-16 17:51:20 +03:00
|
|
|
VMexit_WBINVD(i);
|
2010-03-15 18:48:01 +03:00
|
|
|
#endif
|
|
|
|
|
2007-12-14 23:41:09 +03:00
|
|
|
invalidate_prefetch_q();
|
|
|
|
|
2009-01-23 12:26:24 +03:00
|
|
|
BX_DEBUG(("WBINVD: Flush internal caches !"));
|
|
|
|
BX_INSTR_CACHE_CNTRL(BX_CPU_ID, BX_INSTR_WBINVD);
|
2007-12-14 23:41:09 +03:00
|
|
|
|
2005-10-18 22:07:52 +04:00
|
|
|
flushICaches();
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::CLFLUSH(bxInstruction_c *i)
|
2007-01-29 00:27:31 +03:00
|
|
|
{
|
2007-12-14 23:41:09 +03:00
|
|
|
bx_segment_reg_t *seg = &BX_CPU_THIS_PTR sregs[i->seg()];
|
2008-01-17 01:56:17 +03:00
|
|
|
|
2008-08-08 13:22:49 +04:00
|
|
|
bx_address eaddr = BX_CPU_CALL_METHODR(i->ResolveModrm, (i));
|
|
|
|
bx_address laddr = BX_CPU_THIS_PTR get_laddr(i->seg(), eaddr);
|
2008-09-09 00:47:33 +04:00
|
|
|
|
2008-05-27 01:46:39 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2008-09-09 00:47:33 +04:00
|
|
|
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64) {
|
|
|
|
if (! IsCanonical(laddr)) {
|
|
|
|
BX_ERROR(("CLFLUSH: non-canonical access !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
2008-05-27 01:46:39 +04:00
|
|
|
}
|
2008-09-09 00:47:33 +04:00
|
|
|
else
|
2008-05-27 01:46:39 +04:00
|
|
|
#endif
|
2008-09-09 00:47:33 +04:00
|
|
|
{
|
|
|
|
// check if we could access the memory segment
|
|
|
|
if (!(seg->cache.valid & SegAccessROK)) {
|
2009-04-07 20:12:19 +04:00
|
|
|
if (! execute_virtual_checks(seg, (Bit32u) eaddr, 1))
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (eaddr > seg->cache.u.segment.limit_scaled) {
|
|
|
|
BX_ERROR(("CLFLUSH: segment limit violation"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-27 01:46:39 +04:00
|
|
|
|
2010-03-19 20:00:05 +03:00
|
|
|
#if BX_INSTRUMENTATION
|
|
|
|
bx_phy_address paddr =
|
|
|
|
#endif
|
|
|
|
A20ADDR(dtranslate_linear(laddr, CPL, BX_READ));
|
2008-01-17 01:56:17 +03:00
|
|
|
|
|
|
|
BX_INSTR_CLFLUSH(BX_CPU_ID, laddr, paddr);
|
|
|
|
|
2009-08-19 13:59:30 +04:00
|
|
|
#if BX_X86_DEBUGGER
|
|
|
|
hwbreakpoint_match(laddr, 1, BX_READ);
|
|
|
|
#endif
|
2007-01-29 00:27:31 +03:00
|
|
|
}
|
|
|
|
|
2004-05-11 01:05:51 +04:00
|
|
|
#if BX_CPU_LEVEL == 2
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::LOADALL(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
Bit16u msw, tr, flags, ip, ldtr;
|
|
|
|
Bit16u ds_raw, ss_raw, cs_raw, es_raw;
|
|
|
|
Bit16u base_15_0, limit;
|
|
|
|
Bit8u base_23_16, access;
|
|
|
|
|
2002-08-10 16:06:26 +04:00
|
|
|
if (v8086_mode()) BX_PANIC(("proc_ctrl: LOADALL in v8086 mode unsupported"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-02-03 00:46:54 +03:00
|
|
|
if (BX_CPU_THIS_PTR cr0.get_PE())
|
2004-05-11 01:05:51 +04:00
|
|
|
{
|
2003-11-14 00:17:31 +03:00
|
|
|
BX_PANIC(("LOADALL not yet supported for protected mode"));
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2003-11-14 00:17:31 +03:00
|
|
|
BX_PANIC(("LOADALL: handle CR0.val32"));
|
2001-04-10 05:04:59 +04:00
|
|
|
/* MSW */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x806, 2, &msw);
|
2007-07-09 19:16:14 +04:00
|
|
|
BX_CPU_THIS_PTR cr0.set_PE(msw & 0x01); msw >>= 1;
|
|
|
|
BX_CPU_THIS_PTR cr0.set_MP(msw & 0x01); msw >>= 1;
|
|
|
|
BX_CPU_THIS_PTR cr0.set_EM(msw & 0x01); msw >>= 1;
|
|
|
|
BX_CPU_THIS_PTR cr0.set_TS(msw & 0x01);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2007-07-09 19:16:14 +04:00
|
|
|
if (BX_CPU_THIS_PTR cr0.get_PE() || BX_CPU_THIS_PTR cr0.get_MP() || BX_CPU_THIS_PTR cr0.get_EM() || BX_CPU_THIS_PTR cr0.get_TS())
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("LOADALL set PE, MP, EM or TS bits in MSW!"));
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* TR */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x816, 2, &tr);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR tr.selector.value = tr;
|
2006-04-08 00:47:32 +04:00
|
|
|
BX_CPU_THIS_PTR tr.selector.rpl = (tr & 0x03); tr >>= 2;
|
|
|
|
BX_CPU_THIS_PTR tr.selector.ti = (tr & 0x01); tr >>= 1;
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR tr.selector.index = tr;
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x860, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x862, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x863, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x864, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR tr.cache.valid =
|
|
|
|
BX_CPU_THIS_PTR tr.cache.p = (access & 0x80) >> 7;
|
|
|
|
BX_CPU_THIS_PTR tr.cache.dpl = (access & 0x60) >> 5;
|
|
|
|
BX_CPU_THIS_PTR tr.cache.segment = (access & 0x10) >> 4;
|
2001-10-10 01:15:14 +04:00
|
|
|
// don't allow busy bit in tr.cache.type, so bit 2 is masked away too.
|
|
|
|
BX_CPU_THIS_PTR tr.cache.type = (access & 0x0d);
|
2009-04-05 23:09:44 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
|
|
|
BX_CPU_THIS_PTR tr.cache.u.segment.limit_scaled = limit;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-02-03 21:25:10 +03:00
|
|
|
if ((BX_CPU_THIS_PTR tr.selector.value & 0xfffc) == 0) {
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.valid = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2009-04-05 23:09:44 +04:00
|
|
|
if (BX_CPU_THIS_PTR tr.cache.u.segment.limit_scaled < 43 ||
|
2006-04-08 00:47:32 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.type != BX_SYS_SEGMENT_AVAIL_286_TSS ||
|
|
|
|
BX_CPU_THIS_PTR tr.cache.segment)
|
|
|
|
{
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.valid = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR tr.cache.valid==0)
|
|
|
|
{
|
2006-05-22 00:41:48 +04:00
|
|
|
BX_CPU_THIS_PTR tr.selector.value = 0;
|
|
|
|
BX_CPU_THIS_PTR tr.selector.index = 0;
|
|
|
|
BX_CPU_THIS_PTR tr.selector.ti = 0;
|
|
|
|
BX_CPU_THIS_PTR tr.selector.rpl = 0;
|
2009-04-05 23:09:44 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.u.segment.base = 0;
|
|
|
|
BX_CPU_THIS_PTR tr.cache.u.segment.limit_scaled = 0;
|
2006-05-22 00:41:48 +04:00
|
|
|
BX_CPU_THIS_PTR tr.cache.p = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* FLAGS */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x818, 2, &flags);
|
2001-04-10 05:04:59 +04:00
|
|
|
write_flags(flags, 1, 1);
|
|
|
|
|
|
|
|
/* IP */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x81a, 2, &IP);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* LDTR */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x81c, 2, &ldtr);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.selector.value = ldtr;
|
2006-04-08 00:47:32 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.selector.rpl = (ldtr & 0x03); ldtr >>= 2;
|
|
|
|
BX_CPU_THIS_PTR ldtr.selector.ti = (ldtr & 0x01); ldtr >>= 1;
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.selector.index = ldtr;
|
2005-02-03 21:25:10 +03:00
|
|
|
if ((BX_CPU_THIS_PTR ldtr.selector.value & 0xfffc) == 0)
|
|
|
|
{
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.cache.valid = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.p = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.segment = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.type = 0;
|
2009-04-05 23:09:44 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.cache.u.segment.base = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.u.segment.limit_scaled = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.selector.value = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.selector.index = 0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.selector.ti = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
else {
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x854, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x856, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x857, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x858, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.cache.valid =
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.p = access >> 7;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.dpl = (access >> 5) & 0x03;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.segment = (access >> 4) & 0x01;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.type = (access & 0x0f);
|
2009-04-05 23:09:44 +04:00
|
|
|
BX_CPU_THIS_PTR ldtr.cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
|
|
|
BX_CPU_THIS_PTR ldtr.cache.u.segment.limit_scaled = limit;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
if (access == 0) {
|
2008-02-07 21:28:50 +03:00
|
|
|
BX_PANIC(("loadall: LDTR case access byte=0"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR ldtr.cache.valid==0) {
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("loadall: ldtr.valid=0"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
if (BX_CPU_THIS_PTR ldtr.cache.segment) { /* not a system segment */
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_INFO((" AR byte = %02x", (unsigned) access));
|
|
|
|
BX_PANIC(("loadall: LDTR descriptor cache loaded with non system segment"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2006-08-25 23:56:03 +04:00
|
|
|
if (BX_CPU_THIS_PTR ldtr.cache.type != BX_SYS_SEGMENT_LDT) {
|
|
|
|
BX_PANIC(("loadall: LDTR.type(%u) != LDT", (unsigned) (access & 0x0f)));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* DS */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x81e, 2, &ds_raw);
|
2008-06-23 06:56:31 +04:00
|
|
|
parse_selector(ds_raw, &BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector);
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x848, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x84a, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x84b, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x84c, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
2009-04-05 22:16:29 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.u.segment.limit_scaled = limit;
|
2006-06-12 20:58:27 +04:00
|
|
|
set_ar_byte(BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache, access);
|
|
|
|
|
|
|
|
if ((BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value & 0xfffc) == 0) {
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.valid = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2006-06-12 20:58:27 +04:00
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.valid = 1;
|
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.valid==0 ||
|
2005-02-03 21:25:10 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.segment==0)
|
|
|
|
{
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("loadall: DS invalid"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* SS */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x820, 2, &ss_raw);
|
2008-06-23 06:56:31 +04:00
|
|
|
parse_selector(ss_raw, &BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x842, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x844, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x845, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x846, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
2009-04-05 22:16:29 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled = limit;
|
2006-06-12 20:58:27 +04:00
|
|
|
set_ar_byte(BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache, access);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-02-03 21:25:10 +03:00
|
|
|
if ((BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value & 0xfffc) == 0) {
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2006-06-12 20:58:27 +04:00
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = 1;
|
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid==0 ||
|
2005-02-03 21:25:10 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment==0)
|
|
|
|
{
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("loadall: SS invalid"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2008-02-03 00:46:54 +03:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
/* CS */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x822, 2, &cs_raw);
|
2008-06-23 06:56:31 +04:00
|
|
|
parse_selector(cs_raw, &BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x83c, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x83e, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x83f, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x840, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
2009-04-05 22:16:29 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = limit;
|
2006-06-12 20:58:27 +04:00
|
|
|
set_ar_byte(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache, access);
|
|
|
|
|
|
|
|
if ((BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value & 0xfffc) == 0) {
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = 0;
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2006-06-12 20:58:27 +04:00
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = 1;
|
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid==0 ||
|
2005-02-03 21:25:10 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment==0)
|
|
|
|
{
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("loadall: CS invalid"));
|
2005-02-03 21:25:10 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-04-07 22:39:17 +04:00
|
|
|
handleCpuModeChange();
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
/* ES */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x824, 2, &es_raw);
|
2008-06-23 06:56:31 +04:00
|
|
|
parse_selector(es_raw, &BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector);
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x836, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x838, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x839, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x83a, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.u.segment.base = (base_23_16 << 16) | base_15_0;
|
2009-04-05 22:16:29 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.u.segment.limit_scaled = limit;
|
2006-06-12 20:58:27 +04:00
|
|
|
set_ar_byte(BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache, access);
|
|
|
|
|
|
|
|
if ((BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value & 0xfffc) == 0) {
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.valid = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.valid = 1;
|
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.valid==0 ||
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.segment==0)
|
|
|
|
{
|
|
|
|
BX_PANIC(("loadall: ES invalid"));
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x826, 2, &DI);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x828, 2, &SI);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x82a, 2, &BP);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x82c, 2, &SP);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x82e, 2, &BX);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x830, 2, &DX);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x832, 2, &CX);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x834, 2, &AX);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
/* GDTR */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x84e, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x850, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x851, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x852, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR gdtr.base = (base_23_16 << 16) | base_15_0;
|
|
|
|
BX_CPU_THIS_PTR gdtr.limit = limit;
|
|
|
|
|
|
|
|
/* IDTR */
|
2008-04-27 23:49:02 +04:00
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x85a, 2, &base_15_0);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x85c, 1, &base_23_16);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x85d, 1, &access);
|
|
|
|
BX_MEM(0)->readPhysicalPage(BX_CPU_THIS, 0x85e, 2, &limit);
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR idtr.base = (base_23_16 << 16) | base_15_0;
|
|
|
|
BX_CPU_THIS_PTR idtr.limit = limit;
|
|
|
|
}
|
2004-05-11 01:05:51 +04:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2006-04-05 21:31:35 +04:00
|
|
|
void BX_CPU_C::handleCpuModeChange(void)
|
|
|
|
{
|
2008-02-01 16:25:23 +03:00
|
|
|
unsigned mode = BX_CPU_THIS_PTR cpu_mode;
|
|
|
|
|
2006-04-05 21:31:35 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2008-04-01 00:56:27 +04:00
|
|
|
if (BX_CPU_THIS_PTR efer.get_LMA()) {
|
2007-07-09 19:16:14 +04:00
|
|
|
if (! BX_CPU_THIS_PTR cr0.get_PE()) {
|
2006-04-05 21:31:35 +04:00
|
|
|
BX_PANIC(("change_cpu_mode: EFER.LMA is set when CR0.PE=0 !"));
|
|
|
|
}
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l) {
|
|
|
|
BX_CPU_THIS_PTR cpu_mode = BX_MODE_LONG_64;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR cpu_mode = BX_MODE_LONG_COMPAT;
|
2008-05-12 00:46:11 +04:00
|
|
|
// clear upper part of RIP/RSP when leaving 64-bit long mode
|
|
|
|
BX_CLEAR_64BIT_HIGH(BX_64BIT_REG_RIP);
|
|
|
|
BX_CLEAR_64BIT_HIGH(BX_64BIT_REG_RSP);
|
2006-04-05 21:31:35 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-03 00:46:54 +03:00
|
|
|
else
|
2006-04-05 21:31:35 +04:00
|
|
|
#endif
|
|
|
|
{
|
2007-07-09 19:16:14 +04:00
|
|
|
if (BX_CPU_THIS_PTR cr0.get_PE()) {
|
2008-06-16 00:41:34 +04:00
|
|
|
if (BX_CPU_THIS_PTR get_VM()) {
|
2006-04-05 21:31:35 +04:00
|
|
|
BX_CPU_THIS_PTR cpu_mode = BX_MODE_IA32_V8086;
|
2008-06-16 00:41:34 +04:00
|
|
|
}
|
2008-02-01 16:25:23 +03:00
|
|
|
else
|
2006-04-05 21:31:35 +04:00
|
|
|
BX_CPU_THIS_PTR cpu_mode = BX_MODE_IA32_PROTECTED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_CPU_THIS_PTR cpu_mode = BX_MODE_IA32_REAL;
|
2009-01-10 12:36:44 +03:00
|
|
|
|
|
|
|
// CS segment in real mode always allows full access
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
|
|
|
|
2008-06-16 00:41:34 +04:00
|
|
|
BX_ASSERT(CPL == 0);
|
2006-04-05 21:31:35 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-01 16:25:23 +03:00
|
|
|
|
2009-03-11 01:28:08 +03:00
|
|
|
updateFetchModeMask();
|
|
|
|
|
2008-04-07 23:59:53 +04:00
|
|
|
if (mode != BX_CPU_THIS_PTR cpu_mode) {
|
2008-02-01 16:25:23 +03:00
|
|
|
BX_DEBUG(("%s activated", cpu_mode_string(BX_CPU_THIS_PTR cpu_mode)));
|
2008-04-07 23:59:53 +04:00
|
|
|
#if BX_DEBUGGER
|
|
|
|
if (BX_CPU_THIS_PTR mode_break) {
|
|
|
|
BX_CPU_THIS_PTR stop_reason = STOP_MODE_BREAK_POINT;
|
|
|
|
bx_debug_break(); // trap into debugger
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2006-04-05 21:31:35 +04:00
|
|
|
}
|
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
|
|
|
void BX_CPU_C::handleAlignmentCheck(void)
|
|
|
|
{
|
|
|
|
if (CPL == 3 && BX_CPU_THIS_PTR cr0.get_AM() && BX_CPU_THIS_PTR get_AC()) {
|
2008-09-16 23:20:03 +04:00
|
|
|
if (BX_CPU_THIS_PTR alignment_check_mask == 0) {
|
|
|
|
BX_CPU_THIS_PTR alignment_check_mask = 0xF;
|
|
|
|
BX_INFO(("Enable alignment check (#AC exception)"));
|
2010-01-31 21:06:45 +03:00
|
|
|
// BX_CPU_THIS_PTR iCache.flushICacheEntries();
|
2008-09-16 23:20:03 +04:00
|
|
|
}
|
2007-11-21 00:22:03 +03:00
|
|
|
}
|
|
|
|
else {
|
2008-09-16 23:20:03 +04:00
|
|
|
if (BX_CPU_THIS_PTR alignment_check_mask != 0) {
|
|
|
|
BX_CPU_THIS_PTR alignment_check_mask = 0;
|
|
|
|
BX_INFO(("Disable alignment check (#AC exception)"));
|
2010-01-31 21:06:45 +03:00
|
|
|
// BX_CPU_THIS_PTR iCache.flushICacheEntries();
|
2008-09-16 23:20:03 +04:00
|
|
|
}
|
2007-11-21 00:22:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::RDPMC(bxInstruction_c *i)
|
2003-10-24 22:34:16 +04:00
|
|
|
{
|
2010-02-26 14:44:50 +03:00
|
|
|
#if BX_CPU_LEVEL >= 5
|
|
|
|
if (BX_CPU_THIS_PTR cr4.get_PCE() || CPL==0 || real_mode()) {
|
2009-01-31 13:43:24 +03:00
|
|
|
|
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_RDPMC(i);
|
|
|
|
#endif
|
|
|
|
|
2004-05-11 01:05:51 +04:00
|
|
|
/* According to manual, Pentium 4 has 18 counters,
|
|
|
|
* previous versions have two. And the P4 also can do
|
|
|
|
* short read-out (EDX always 0). Otherwise it is
|
|
|
|
* limited to 40 bits.
|
|
|
|
*/
|
|
|
|
|
2010-04-03 09:59:07 +04:00
|
|
|
if (BX_CPU_SUPPORT_ISA_EXTENSION(BX_CPU_SSE2)) { // Pentium 4 processor (see cpuid.cc)
|
2010-02-26 14:44:50 +03:00
|
|
|
if ((ECX & 0x7fffffff) >= 18)
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2010-02-26 14:44:50 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((ECX & 0xffffffff) >= 2)
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2010-02-26 14:44:50 +03:00
|
|
|
}
|
2010-02-26 01:04:31 +03:00
|
|
|
|
2004-05-11 01:05:51 +04:00
|
|
|
// Most counters are for hardware specific details, which
|
|
|
|
// we anyhow do not emulate (like pipeline stalls etc)
|
|
|
|
|
|
|
|
// Could be interesting to count number of memory reads,
|
|
|
|
// writes. Misaligned etc... But to monitor bochs, this
|
|
|
|
// is easier done from the host.
|
|
|
|
|
2007-09-25 20:11:32 +04:00
|
|
|
RAX = 0;
|
|
|
|
RDX = 0; // if P4 and ECX & 0x10000000, then always 0 (short read 32 bits)
|
2004-05-11 01:05:51 +04:00
|
|
|
|
|
|
|
BX_ERROR(("RDPMC: Performance Counters Support not reasonably implemented yet"));
|
|
|
|
} else {
|
|
|
|
// not allowed to use RDPMC!
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2004-05-11 01:05:51 +04:00
|
|
|
}
|
2003-10-24 22:34:16 +04:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2006-01-21 15:06:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 5
|
2009-01-19 20:43:54 +03:00
|
|
|
Bit64u BX_CPU_C::get_TSC(void)
|
2006-01-21 15:06:03 +03:00
|
|
|
{
|
2009-01-19 20:43:54 +03:00
|
|
|
Bit64u tsc = bx_pc_system.time_ticks() - BX_CPU_THIS_PTR msr.tsc_last_reset;
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
tsc += VMX_TSC_Offset();
|
|
|
|
#endif
|
2009-01-19 20:43:54 +03:00
|
|
|
return tsc;
|
2006-01-21 15:06:03 +03:00
|
|
|
}
|
|
|
|
|
2008-04-18 22:32:40 +04:00
|
|
|
void BX_CPU_C::set_TSC(Bit64u newval)
|
2006-01-21 15:06:03 +03:00
|
|
|
{
|
|
|
|
// compute the correct setting of tsc_last_reset so that a get_TSC()
|
|
|
|
// will return newval
|
2008-04-18 22:32:40 +04:00
|
|
|
BX_CPU_THIS_PTR msr.tsc_last_reset = bx_pc_system.time_ticks() - newval;
|
2006-01-21 15:06:03 +03:00
|
|
|
|
|
|
|
// verify
|
2009-01-19 20:43:54 +03:00
|
|
|
BX_ASSERT(get_TSC() == newval);
|
2006-01-21 15:06:03 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::RDTSC(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
#if BX_CPU_LEVEL >= 5
|
2008-04-18 22:32:40 +04:00
|
|
|
if (! BX_CPU_THIS_PTR cr4.get_TSD() || CPL==0) {
|
2009-01-31 13:43:24 +03:00
|
|
|
|
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_RDTSC(i);
|
|
|
|
#endif
|
|
|
|
|
2001-05-23 12:16:07 +04:00
|
|
|
// return ticks
|
2006-01-21 15:06:03 +03:00
|
|
|
Bit64u ticks = BX_CPU_THIS_PTR get_TSC();
|
2009-01-31 13:43:24 +03:00
|
|
|
|
2009-01-19 20:43:54 +03:00
|
|
|
RAX = GET32L(ticks);
|
|
|
|
RDX = GET32H(ticks);
|
2009-01-31 13:43:24 +03:00
|
|
|
|
2001-05-23 12:16:07 +04:00
|
|
|
} else {
|
2009-01-19 20:43:54 +03:00
|
|
|
BX_ERROR(("RDTSC: not allowed to use instruction !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2001-05-23 12:16:07 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-08-05 16:47:33 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::RDTSCP(bxInstruction_c *i)
|
2005-08-05 16:47:33 +04:00
|
|
|
{
|
2010-03-15 16:47:18 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
2010-03-15 18:48:01 +03:00
|
|
|
if (BX_CPU_THIS_PTR in_vmx_guest) {
|
|
|
|
if (! SECONDARY_VMEXEC_CONTROL(VMX_VM_EXEC_CTRL3_RDTSCP)) {
|
|
|
|
BX_ERROR(("RDTSCP in VMX guest: not allowed to use instruction !"));
|
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
|
|
|
}
|
2010-03-15 16:47:18 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-05 16:47:33 +04:00
|
|
|
RDTSC(i);
|
|
|
|
RCX = MSR_TSC_AUX;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-11-01 21:03:48 +03:00
|
|
|
#if BX_SUPPORT_MONITOR_MWAIT
|
|
|
|
bx_bool BX_CPU_C::is_monitor(bx_phy_address begin_addr, unsigned len)
|
|
|
|
{
|
2010-03-07 12:16:24 +03:00
|
|
|
if (! BX_CPU_THIS_PTR monitor.armed) return 0;
|
|
|
|
|
2007-11-01 21:03:48 +03:00
|
|
|
bx_phy_address end_addr = begin_addr + len;
|
|
|
|
if (begin_addr >= BX_CPU_THIS_PTR monitor.monitor_end || end_addr <= BX_CPU_THIS_PTR monitor.monitor_begin)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BX_CPU_C::check_monitor(bx_phy_address begin_addr, unsigned len)
|
|
|
|
{
|
|
|
|
if (is_monitor(begin_addr, len)) {
|
|
|
|
// wakeup from MWAIT state
|
2010-03-07 12:16:24 +03:00
|
|
|
if(BX_CPU_THIS_PTR activity_state >= BX_ACTIVITY_STATE_MWAIT)
|
|
|
|
BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_ACTIVE;
|
2007-11-01 21:03:48 +03:00
|
|
|
// clear monitor
|
2008-10-03 21:00:46 +04:00
|
|
|
BX_CPU_THIS_PTR monitor.reset_monitor();
|
2010-03-07 12:16:24 +03:00
|
|
|
}
|
2007-11-01 21:03:48 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MONITOR(bxInstruction_c *i)
|
2007-10-12 01:29:01 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_MONITOR_MWAIT
|
2009-02-13 12:51:57 +03:00
|
|
|
if (!real_mode() && CPL != 0) {
|
|
|
|
BX_DEBUG(("MWAIT instruction not recognized when CPL != 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2009-02-13 12:51:57 +03:00
|
|
|
}
|
|
|
|
|
2007-11-09 15:06:34 +03:00
|
|
|
BX_DEBUG(("MONITOR instruction executed EAX = 0x08x", (unsigned) EAX));
|
2007-10-12 01:29:01 +04:00
|
|
|
|
2009-02-13 12:51:57 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_MONITOR(i);
|
|
|
|
#endif
|
|
|
|
|
2007-10-12 01:29:01 +04:00
|
|
|
if (RCX != 0) {
|
|
|
|
BX_ERROR(("MONITOR: no optional extensions supported"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2007-10-12 01:29:01 +04:00
|
|
|
}
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
bx_segment_reg_t *seg = &BX_CPU_THIS_PTR sregs[i->seg()];
|
|
|
|
|
|
|
|
bx_address offset;
|
2007-10-12 01:29:01 +04:00
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->as64L()) {
|
2008-04-07 22:39:17 +04:00
|
|
|
offset = RAX;
|
2007-10-12 01:29:01 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (i->as32L()) {
|
2008-04-07 22:39:17 +04:00
|
|
|
offset = EAX;
|
2007-10-12 01:29:01 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-04-07 22:39:17 +04:00
|
|
|
offset = AX;
|
2007-10-12 01:29:01 +04:00
|
|
|
}
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
// set MONITOR
|
|
|
|
bx_address laddr = BX_CPU_THIS_PTR get_laddr(i->seg(), offset);
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64) {
|
|
|
|
if (! IsCanonical(laddr)) {
|
|
|
|
BX_ERROR(("MONITOR: non-canonical access !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// check if we could access the memory segment
|
|
|
|
if (!(seg->cache.valid & SegAccessROK)) {
|
2010-03-07 12:41:12 +03:00
|
|
|
if (! execute_virtual_checks(seg, offset, 1))
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (offset > seg->cache.u.segment.limit_scaled) {
|
|
|
|
BX_ERROR(("MONITOR: segment limit violation"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(int_number(i->seg()), 0);
|
2008-09-09 00:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
2008-06-12 23:14:40 +04:00
|
|
|
}
|
2007-10-12 01:29:01 +04:00
|
|
|
|
2010-03-19 20:00:05 +03:00
|
|
|
bx_phy_address paddr = A20ADDR(dtranslate_linear(laddr, CPL, BX_READ));
|
2007-11-01 21:03:48 +03:00
|
|
|
|
2008-10-03 20:53:08 +04:00
|
|
|
// Set the monitor immediately. If monitor is still armed when we MWAIT,
|
|
|
|
// the processor will stall.
|
|
|
|
bx_pc_system.invlpg(BX_CPU_THIS_PTR monitor.monitor_begin);
|
|
|
|
if ((BX_CPU_THIS_PTR monitor.monitor_end & ~0xfff) != (BX_CPU_THIS_PTR monitor.monitor_begin & ~0xfff))
|
|
|
|
bx_pc_system.invlpg(BX_CPU_THIS_PTR monitor.monitor_end);
|
2010-03-07 12:16:24 +03:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR monitor.arm(paddr);
|
2008-10-03 20:53:08 +04:00
|
|
|
BX_DEBUG(("MONITOR for phys_addr=0x" FMT_PHY_ADDRX, BX_CPU_THIS_PTR monitor.monitor_begin));
|
2007-10-12 01:29:01 +04:00
|
|
|
#else
|
|
|
|
BX_INFO(("MONITOR: use --enable-monitor-mwait to enable MONITOR/MWAIT support"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2007-10-12 01:29:01 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MWAIT(bxInstruction_c *i)
|
2007-10-12 01:29:01 +04:00
|
|
|
{
|
|
|
|
#if BX_SUPPORT_MONITOR_MWAIT
|
2009-02-13 12:51:57 +03:00
|
|
|
if (!real_mode() && CPL != 0) {
|
|
|
|
BX_DEBUG(("MWAIT instruction not recognized when CPL != 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2009-02-13 12:51:57 +03:00
|
|
|
}
|
|
|
|
|
2007-10-12 23:30:51 +04:00
|
|
|
BX_DEBUG(("MWAIT instruction executed ECX = 0x%08x", ECX));
|
2007-10-12 01:29:01 +04:00
|
|
|
|
2009-02-13 12:51:57 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
|
|
|
VMexit_MWAIT(i);
|
|
|
|
#endif
|
|
|
|
|
2007-10-12 01:29:01 +04:00
|
|
|
// only one extension is supported
|
|
|
|
// ECX[0] - interrupt MWAIT even if EFLAGS.IF = 0
|
2007-11-01 21:03:48 +03:00
|
|
|
if (RCX & ~(BX_CONST64(1))) {
|
2007-10-12 01:29:01 +04:00
|
|
|
BX_ERROR(("MWAIT: incorrect optional extensions in RCX"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2007-10-12 01:29:01 +04:00
|
|
|
}
|
|
|
|
|
2008-10-03 20:53:08 +04:00
|
|
|
// If monitor has already triggered, we just return.
|
2010-03-07 12:16:24 +03:00
|
|
|
if (! BX_CPU_THIS_PTR monitor.armed) {
|
|
|
|
BX_DEBUG(("MWAIT: the MONITOR was not armed or already triggered"));
|
2008-10-03 20:53:08 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-11-01 21:03:48 +03:00
|
|
|
|
2010-03-12 14:28:59 +03:00
|
|
|
static bool mwait_is_nop = SIM->get_param_bool(BXPN_CPUID_MWAIT_IS_NOP)->get();
|
|
|
|
if (mwait_is_nop) {
|
|
|
|
BX_DEBUG(("MWAIT: stay awake"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-01 21:03:48 +03:00
|
|
|
// stops instruction execution and places the processor in a optimized
|
|
|
|
// state. Events that cause exit from MWAIT state are:
|
|
|
|
// A store from another processor to monitored range, any unmasked
|
|
|
|
// interrupt, including INTR, NMI, SMI, INIT or reset will resume
|
|
|
|
// the execution. Any far control transfer between MONITOR and MWAIT
|
|
|
|
// resets the monitoring logic.
|
|
|
|
|
|
|
|
if (ECX & 1)
|
2009-01-29 23:27:57 +03:00
|
|
|
BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_MWAIT_IF;
|
|
|
|
else
|
|
|
|
BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_MWAIT;
|
|
|
|
|
2007-11-01 21:03:48 +03:00
|
|
|
BX_CPU_THIS_PTR async_event = 1; // so processor knows to check
|
|
|
|
// Execution of this instruction completes. The processor
|
2008-02-03 00:46:54 +03:00
|
|
|
// will remain in a optimized state until one of the above
|
2007-11-01 21:03:48 +03:00
|
|
|
// conditions is met.
|
|
|
|
|
2007-12-14 00:41:32 +03:00
|
|
|
BX_INSTR_MWAIT(BX_CPU_ID, BX_CPU_THIS_PTR monitor.monitor_begin, CACHE_LINE_SIZE, ECX);
|
|
|
|
|
2008-02-03 00:46:54 +03:00
|
|
|
#if BX_USE_IDLE_HACK
|
2007-11-01 21:03:48 +03:00
|
|
|
bx_gui->sim_is_idle();
|
|
|
|
#endif
|
|
|
|
|
2009-01-17 19:55:13 +03:00
|
|
|
#if BX_DEBUGGER
|
|
|
|
bx_dbg_halt(BX_CPU_ID);
|
|
|
|
#endif
|
|
|
|
|
2007-10-12 01:29:01 +04:00
|
|
|
#else
|
|
|
|
BX_INFO(("MWAIT: use --enable-monitor-mwait to enable MONITOR/MWAIT support"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2007-10-12 01:29:01 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::SYSENTER(bxInstruction_c *i)
|
2003-01-20 23:10:31 +03:00
|
|
|
{
|
2010-02-26 14:44:50 +03:00
|
|
|
#if BX_CPU_LEVEL >= 6
|
2008-04-21 01:44:13 +04:00
|
|
|
if (real_mode()) {
|
|
|
|
BX_ERROR(("SYSENTER not recognized in real mode !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2003-01-20 23:10:31 +03:00
|
|
|
}
|
2006-02-28 22:50:08 +03:00
|
|
|
if ((BX_CPU_THIS_PTR msr.sysenter_cs_msr & BX_SELECTOR_RPL_MASK) == 0) {
|
2007-10-12 01:29:01 +04:00
|
|
|
BX_ERROR(("SYSENTER with zero sysenter_cs_msr !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2003-01-20 23:10:31 +03:00
|
|
|
}
|
|
|
|
|
2003-05-11 02:25:55 +04:00
|
|
|
invalidate_prefetch_q();
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2005-09-29 21:32:32 +04:00
|
|
|
BX_CPU_THIS_PTR clear_VM(); // do this just like the book says to do
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (long_mode()) {
|
|
|
|
if (!IsCanonical(BX_CPU_THIS_PTR msr.sysenter_eip_msr)) {
|
|
|
|
BX_ERROR(("SYSENTER with non-canonical SYSENTER_EIP_MSR !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2008-04-15 18:41:50 +04:00
|
|
|
}
|
|
|
|
if (!IsCanonical(BX_CPU_THIS_PTR msr.sysenter_esp_msr)) {
|
|
|
|
BX_ERROR(("SYSENTER with non-canonical SYSENTER_ESP_MSR !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2008-04-15 18:41:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
parse_selector(BX_CPU_THIS_PTR msr.sysenter_cs_msr & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2003-01-20 23:10:31 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; // base address
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; // scaled segment limit
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; // 4k granularity
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; // available for use by system
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = !long_mode();
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = long_mode();
|
|
|
|
#endif
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2008-04-20 22:17:14 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
handleCpuModeChange(); // mode change could happen only when in long_mode()
|
2009-03-11 01:28:08 +03:00
|
|
|
#else
|
|
|
|
updateFetchModeMask();
|
2008-04-20 22:17:14 +04:00
|
|
|
#endif
|
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
2008-09-08 19:45:57 +04:00
|
|
|
BX_CPU_THIS_PTR alignment_check_mask = 0; // CPL=0
|
2007-11-21 00:22:03 +03:00
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
parse_selector((BX_CPU_THIS_PTR msr.sysenter_cs_msr + 8) & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2003-01-20 23:10:31 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.base = 0; // base address
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled = 0xFFFFFFFF; // scaled segment limit
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.g = 1; // 4k granularity
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b = 1; // 32-bit mode
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.avl = 0; // available for use by system
|
2008-04-15 18:41:50 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.l = 0;
|
|
|
|
#endif
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (long_mode()) {
|
|
|
|
RSP = BX_CPU_THIS_PTR msr.sysenter_esp_msr;
|
|
|
|
RIP = BX_CPU_THIS_PTR msr.sysenter_eip_msr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ESP = (Bit32u) BX_CPU_THIS_PTR msr.sysenter_esp_msr;
|
|
|
|
EIP = (Bit32u) BX_CPU_THIS_PTR msr.sysenter_eip_msr;
|
|
|
|
}
|
2008-01-18 11:57:35 +03:00
|
|
|
|
|
|
|
BX_INSTR_FAR_BRANCH(BX_CPU_ID, BX_INSTR_IS_SYSENTER,
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value, RIP);
|
2003-01-20 23:10:31 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::SYSEXIT(bxInstruction_c *i)
|
2003-01-20 23:10:31 +03:00
|
|
|
{
|
2010-02-26 14:44:50 +03:00
|
|
|
#if BX_CPU_LEVEL >= 6
|
2008-04-21 01:44:13 +04:00
|
|
|
if (real_mode() || CPL != 0) {
|
|
|
|
BX_ERROR(("SYSEXIT from real mode or with CPL<>0 !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2003-01-20 23:10:31 +03:00
|
|
|
}
|
2006-02-28 22:50:08 +03:00
|
|
|
if ((BX_CPU_THIS_PTR msr.sysenter_cs_msr & BX_SELECTOR_RPL_MASK) == 0) {
|
2007-10-12 01:29:01 +04:00
|
|
|
BX_ERROR(("SYSEXIT with zero sysenter_cs_msr !"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2003-01-20 23:10:31 +03:00
|
|
|
}
|
2008-04-15 18:41:50 +04:00
|
|
|
|
2010-04-03 01:22:17 +04:00
|
|
|
invalidate_prefetch_q();
|
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->os64L()) {
|
|
|
|
if (!IsCanonical(RDX)) {
|
2010-04-03 01:22:17 +04:00
|
|
|
BX_ERROR(("SYSEXIT with non-canonical RDX (RIP) pointer !"));
|
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2008-04-15 18:41:50 +04:00
|
|
|
}
|
|
|
|
if (!IsCanonical(RCX)) {
|
2010-04-03 01:22:17 +04:00
|
|
|
BX_ERROR(("SYSEXIT with non-canonical RCX (RSP) pointer !"));
|
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2008-04-15 18:41:50 +04:00
|
|
|
}
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
parse_selector(((BX_CPU_THIS_PTR msr.sysenter_cs_msr + 32) & BX_SELECTOR_RPL_MASK) | 3,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
2006-06-12 01:37:22 +04:00
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; // base address
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; // scaled segment limit
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; // 4k granularity
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; // available for use by system
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 1;
|
|
|
|
|
|
|
|
RSP = RCX;
|
|
|
|
RIP = RDX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
parse_selector(((BX_CPU_THIS_PTR msr.sysenter_cs_msr + 16) & BX_SELECTOR_RPL_MASK) | 3,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; // base address
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; // scaled segment limit
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; // 4k granularity
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; // available for use by system
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 1;
|
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ESP = ECX;
|
|
|
|
EIP = EDX;
|
|
|
|
}
|
2003-01-20 23:10:31 +03:00
|
|
|
|
2008-04-20 22:17:14 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
handleCpuModeChange(); // mode change could happen only when in long_mode()
|
2009-03-11 01:28:08 +03:00
|
|
|
#else
|
|
|
|
updateFetchModeMask();
|
2008-04-20 22:17:14 +04:00
|
|
|
#endif
|
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
|
|
|
handleAlignmentCheck(); // CPL was modified
|
|
|
|
#endif
|
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
parse_selector(((BX_CPU_THIS_PTR msr.sysenter_cs_msr + (i->os64L() ? 40:24)) & BX_SELECTOR_RPL_MASK) | 3,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
2006-06-12 01:37:22 +04:00
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2003-01-20 23:10:31 +03:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.base = 0; // base address
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled = 0xFFFFFFFF; // scaled segment limit
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.g = 1; // 4k granularity
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b = 1; // 32-bit mode
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.avl = 0; // available for use by system
|
2008-04-15 18:41:50 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.l = 0;
|
|
|
|
#endif
|
2008-01-18 11:57:35 +03:00
|
|
|
|
|
|
|
BX_INSTR_FAR_BRANCH(BX_CPU_ID, BX_INSTR_IS_SYSEXIT,
|
2008-04-15 18:41:50 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value, RIP);
|
2003-01-20 23:10:31 +03:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-25 18:09:08 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::SYSCALL(bxInstruction_c *i)
|
2005-03-03 23:24:52 +03:00
|
|
|
{
|
|
|
|
bx_address temp_RIP;
|
|
|
|
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_DEBUG(("Execute SYSCALL instruction"));
|
|
|
|
|
2008-04-01 00:56:27 +04:00
|
|
|
if (!BX_CPU_THIS_PTR efer.get_SCE()) {
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invalidate_prefetch_q();
|
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
if (long_mode())
|
2005-03-03 23:24:52 +03:00
|
|
|
{
|
|
|
|
RCX = RIP;
|
2005-10-17 17:06:09 +04:00
|
|
|
R11 = read_eflags() & ~(EFlagsRFMask);
|
2005-03-03 23:24:52 +03:00
|
|
|
|
|
|
|
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64) {
|
|
|
|
temp_RIP = MSR_LSTAR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
temp_RIP = MSR_CSTAR;
|
|
|
|
}
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// set up CS segment, flat, 64-bit DPL=0
|
|
|
|
parse_selector((MSR_STAR >> 32) & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 1; /* 64-bit code */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2008-04-20 22:17:14 +04:00
|
|
|
handleCpuModeChange(); // mode change could only happen when in long_mode()
|
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
2008-09-08 19:45:57 +04:00
|
|
|
BX_CPU_THIS_PTR alignment_check_mask = 0; // CPL=0
|
2007-11-21 00:22:03 +03:00
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// set up SS segment, flat, 64-bit DPL=0
|
|
|
|
parse_selector(((MSR_STAR >> 32) + 8) & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b = 1; /* 32 bit stack */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.l = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2005-10-17 17:06:09 +04:00
|
|
|
writeEFlags(read_eflags() & (~MSR_FMASK), EFlagsValidMask);
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
2005-03-03 23:24:52 +03:00
|
|
|
RIP = temp_RIP;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// legacy mode
|
|
|
|
|
|
|
|
ECX = EIP;
|
|
|
|
temp_RIP = MSR_STAR & 0xFFFFFFFF;
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// set up CS segment, flat, 32-bit DPL=0
|
|
|
|
parse_selector((MSR_STAR >> 32) & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 0; /* 32-bit code */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2008-08-03 23:53:09 +04:00
|
|
|
updateFetchModeMask();
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
2008-09-08 19:45:57 +04:00
|
|
|
BX_CPU_THIS_PTR alignment_check_mask = 0; // CPL=0
|
2007-11-21 00:22:03 +03:00
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// set up SS segment, flat, 32-bit DPL=0
|
|
|
|
parse_selector(((MSR_STAR >> 32) + 8) & BX_SELECTOR_RPL_MASK,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b = 1; /* 32 bit stack */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.l = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.avl = 0; /* available for use by system */
|
|
|
|
|
|
|
|
BX_CPU_THIS_PTR clear_VM();
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
2005-03-03 23:24:52 +03:00
|
|
|
RIP = temp_RIP;
|
|
|
|
}
|
2008-01-18 11:57:35 +03:00
|
|
|
|
|
|
|
BX_INSTR_FAR_BRANCH(BX_CPU_ID, BX_INSTR_IS_SYSCALL,
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value, RIP);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::SYSRET(bxInstruction_c *i)
|
2005-03-03 23:24:52 +03:00
|
|
|
{
|
|
|
|
bx_address temp_RIP;
|
|
|
|
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_DEBUG(("Execute SYSRET instruction"));
|
|
|
|
|
2008-04-01 00:56:27 +04:00
|
|
|
if (!BX_CPU_THIS_PTR efer.get_SCE()) {
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_UD_EXCEPTION, 0);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
|
2008-04-15 18:41:50 +04:00
|
|
|
if(!protected_mode() || CPL != 0) {
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_ERROR(("SYSRET: priveledge check failed, generate #GP(0)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invalidate_prefetch_q();
|
2008-02-03 00:46:54 +03:00
|
|
|
|
2005-03-03 23:24:52 +03:00
|
|
|
if (BX_CPU_THIS_PTR cpu_mode == BX_MODE_LONG_64)
|
|
|
|
{
|
2006-06-12 01:37:22 +04:00
|
|
|
if (i->os64L()) {
|
2010-04-03 01:22:17 +04:00
|
|
|
if (!IsCanonical(RCX)) {
|
|
|
|
BX_ERROR(("SYSRET: canonical failure for RCX (RIP)"));
|
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
|
|
|
}
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// Return to 64-bit mode, set up CS segment, flat, 64-bit DPL=3
|
2008-04-15 18:41:50 +04:00
|
|
|
parse_selector((((MSR_STAR >> 48) + 16) & BX_SELECTOR_RPL_MASK) | 3,
|
2006-06-12 01:37:22 +04:00
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 1; /* 64-bit code */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
|
|
|
temp_RIP = RCX;
|
|
|
|
}
|
2006-06-12 01:37:22 +04:00
|
|
|
else {
|
|
|
|
// Return to 32-bit compatibility mode, set up CS segment, flat, 32-bit DPL=3
|
|
|
|
parse_selector((MSR_STAR >> 48) | 3,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 0; /* 32-bit code */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
|
|
|
temp_RIP = ECX;
|
|
|
|
}
|
|
|
|
|
2008-04-20 22:17:14 +04:00
|
|
|
handleCpuModeChange(); // mode change could only happen when in long64 mode
|
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
|
|
|
handleAlignmentCheck(); // CPL was modified
|
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// SS base, limit, attributes unchanged
|
2009-11-21 12:57:10 +03:00
|
|
|
parse_selector((Bit16u)(((MSR_STAR >> 48) + 8) | 3),
|
2006-06-12 01:37:22 +04:00
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2007-12-23 20:21:28 +03:00
|
|
|
writeEFlags((Bit32u) R11, EFlagsValidMask);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
else { // (!64BIT_MODE)
|
2006-06-12 01:37:22 +04:00
|
|
|
// Return to 32-bit legacy mode, set up CS segment, flat, 32-bit DPL=3
|
|
|
|
parse_selector((MSR_STAR >> 48) | 3,
|
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector);
|
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.segment = 1; /* data/code segment */
|
2009-10-07 19:45:15 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.type = BX_CODE_EXEC_READ_ACCESSED;
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.base = 0; /* base address */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled = 0xFFFFFFFF; /* scaled segment limit */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.g = 1; /* 4k granularity */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.l = 0; /* 32-bit code */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.avl = 0; /* available for use by system */
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2008-08-03 23:53:09 +04:00
|
|
|
updateFetchModeMask();
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2007-11-21 00:22:03 +03:00
|
|
|
#if BX_CPU_LEVEL >= 4 && BX_SUPPORT_ALIGNMENT_CHECK
|
|
|
|
handleAlignmentCheck(); // CPL was modified
|
|
|
|
#endif
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
// SS base, limit, attributes unchanged
|
2009-11-21 12:57:10 +03:00
|
|
|
parse_selector((Bit16u)(((MSR_STAR >> 48) + 8) | 3),
|
2006-06-12 01:37:22 +04:00
|
|
|
&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector);
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2008-09-09 00:47:33 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.valid = SegValidCache | SegAccessROK | SegAccessWOK;
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.p = 1;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl = 3;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.segment = 1; /* data/code segment */
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.type = BX_DATA_READ_WRITE_ACCESSED;
|
2005-03-03 23:24:52 +03:00
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
BX_CPU_THIS_PTR assert_IF();
|
|
|
|
temp_RIP = ECX;
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
2006-06-12 01:37:22 +04:00
|
|
|
|
2008-04-20 22:10:32 +04:00
|
|
|
handleCpuModeChange();
|
|
|
|
|
2006-06-12 01:37:22 +04:00
|
|
|
RIP = temp_RIP;
|
2008-01-18 11:57:35 +03:00
|
|
|
|
|
|
|
BX_INSTR_FAR_BRANCH(BX_CPU_ID, BX_INSTR_IS_SYSRET,
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value, RIP);
|
2005-03-03 23:24:52 +03:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:29:41 +03:00
|
|
|
void BX_CPP_AttrRegparmN(1) BX_CPU_C::SWAPGS(bxInstruction_c *i)
|
2002-09-25 18:09:08 +04:00
|
|
|
{
|
2003-08-30 01:20:52 +04:00
|
|
|
if(CPL != 0)
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2003-08-30 01:20:52 +04:00
|
|
|
|
2010-03-27 19:30:01 +03:00
|
|
|
Bit64u temp_GS_base = MSR_GSBASE;
|
2002-09-25 18:09:08 +04:00
|
|
|
MSR_GSBASE = MSR_KERNELGSBASE;
|
2002-10-08 18:43:18 +04:00
|
|
|
MSR_KERNELGSBASE = temp_GS_base;
|
2002-09-25 18:09:08 +04:00
|
|
|
}
|
|
|
|
#endif
|