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
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-12-14 20:20:35 +03:00
|
|
|
// Copyright (C) 2001-2019 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
|
2008-01-29 20:13:10 +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
|
|
|
|
2010-02-26 17:18:19 +03:00
|
|
|
#include "param_names.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "iodev/iodev.h"
|
|
|
|
|
2005-12-12 22:54:48 +03:00
|
|
|
#if BX_SUPPORT_X86_64==0
|
|
|
|
// Make life easier merging cpu64 & cpu code.
|
|
|
|
#define RIP EIP
|
|
|
|
#define RSP ESP
|
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2021-01-30 11:35:35 +03:00
|
|
|
void BX_CPU_C::long_mode_int(Bit8u vector, unsigned soft_int, bool push_error, Bit16u error_code)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2005-04-12 22:08:10 +04:00
|
|
|
bx_descriptor_t gate_descriptor, cs_descriptor;
|
|
|
|
bx_selector_t cs_selector;
|
|
|
|
|
|
|
|
// interrupt vector must be within IDT table limits,
|
2010-06-21 09:35:45 +04:00
|
|
|
// else #GP(vector*8 + 2 + EXT)
|
2008-04-08 21:58:56 +04:00
|
|
|
if ((vector*16 + 15) > BX_CPU_THIS_PTR idtr.limit) {
|
2008-05-05 01:25:16 +04:00
|
|
|
BX_ERROR(("interrupt(long mode): vector must be within IDT table limits, IDT.limit = 0x%x", BX_CPU_THIS_PTR idtr.limit));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2010-03-18 00:55:19 +03:00
|
|
|
Bit64u desctmp1 = system_read_qword(BX_CPU_THIS_PTR idtr.base + vector*16);
|
|
|
|
Bit64u desctmp2 = system_read_qword(BX_CPU_THIS_PTR idtr.base + vector*16 + 8);
|
2008-05-05 01:25:16 +04:00
|
|
|
|
2008-09-06 21:44:02 +04:00
|
|
|
if (desctmp2 & BX_CONST64(0x00001F0000000000)) {
|
2008-05-05 01:25:16 +04:00
|
|
|
BX_ERROR(("interrupt(long mode): IDT entry extended attributes DWORD4 TYPE != 0"));
|
2010-06-21 09:35:45 +04:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2008-05-05 01:25:16 +04:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
|
2008-09-06 21:44:02 +04:00
|
|
|
Bit32u dword1 = GET32L(desctmp1);
|
|
|
|
Bit32u dword2 = GET32H(desctmp1);
|
|
|
|
Bit32u dword3 = GET32L(desctmp2);
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
parse_descriptor(dword1, dword2, &gate_descriptor);
|
|
|
|
|
|
|
|
if ((gate_descriptor.valid==0) || gate_descriptor.segment)
|
|
|
|
{
|
|
|
|
BX_ERROR(("interrupt(long mode): gate descriptor is not valid sys seg"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2008-05-05 01:25:16 +04:00
|
|
|
// descriptor AR byte must indicate interrupt gate, trap gate,
|
2010-06-21 09:35:45 +04:00
|
|
|
// or task gate, else #GP(vector*8 + 2 + EXT)
|
2008-02-03 00:46:54 +03:00
|
|
|
if (gate_descriptor.type != BX_386_INTERRUPT_GATE &&
|
2005-08-03 00:20:22 +04:00
|
|
|
gate_descriptor.type != BX_386_TRAP_GATE)
|
|
|
|
{
|
2005-08-04 01:10:42 +04:00
|
|
|
BX_ERROR(("interrupt(long mode): unsupported gate type %u",
|
2005-04-12 22:08:10 +04:00
|
|
|
(unsigned) gate_descriptor.type));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:11:08 +03:00
|
|
|
// if software interrupt, then gate descriptor DPL must be >= CPL,
|
2010-06-21 09:35:45 +04:00
|
|
|
// else #GP(vector * 8 + 2 + EXT)
|
2010-03-18 00:55:19 +03:00
|
|
|
if (soft_int && gate_descriptor.dpl < CPL)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
2010-03-18 00:55:19 +03:00
|
|
|
BX_ERROR(("interrupt(long mode): soft_int && gate.dpl < CPL"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2002-03-12 21:59:31 +03:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
|
2010-06-21 09:35:45 +04:00
|
|
|
// Gate must be present, else #NP(vector * 8 + 2 + EXT)
|
2005-07-11 00:32:32 +04:00
|
|
|
if (! IS_PRESENT(gate_descriptor)) {
|
2009-01-14 01:54:49 +03:00
|
|
|
BX_ERROR(("interrupt(long mode): gate.p == 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_NP_EXCEPTION, vector*8 + 2);
|
2001-05-23 12:16:07 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2007-11-06 22:17:42 +03:00
|
|
|
Bit16u gate_dest_selector = gate_descriptor.u.gate.dest_selector;
|
2019-12-20 10:42:07 +03:00
|
|
|
Bit64u gate_dest_offset = ((Bit64u)dword3 << 32) | gate_descriptor.u.gate.dest_offset;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2007-11-06 22:17:42 +03:00
|
|
|
unsigned ist = gate_descriptor.u.gate.param_count & 0x7;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// examine CS selector and descriptor given in gate descriptor
|
|
|
|
// selector must be non-null else #GP(EXT)
|
2006-06-12 20:58:27 +04:00
|
|
|
if ((gate_dest_selector & 0xfffc) == 0) {
|
2005-08-03 00:20:22 +04:00
|
|
|
BX_ERROR(("int_trap_gate(long mode): selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
parse_selector(gate_dest_selector, &cs_selector);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// selector must be within its descriptor table limits
|
|
|
|
// else #GP(selector+EXT)
|
2005-08-04 01:10:42 +04:00
|
|
|
fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
2005-04-12 22:08:10 +04:00
|
|
|
parse_descriptor(dword1, dword2, &cs_descriptor);
|
|
|
|
|
|
|
|
// descriptor AR byte must indicate code seg
|
|
|
|
// and code segment descriptor DPL<=CPL, else #GP(selector+EXT)
|
2008-02-03 00:46:54 +03:00
|
|
|
if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
|
2006-06-17 16:09:55 +04:00
|
|
|
IS_DATA_SEGMENT(cs_descriptor.type) ||
|
2010-06-21 09:35:45 +04:00
|
|
|
cs_descriptor.dpl > CPL)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
2010-03-05 22:52:18 +03:00
|
|
|
BX_ERROR(("interrupt(long mode): not accessible or not code segment"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// check that it's a 64 bit segment
|
2005-08-04 01:10:42 +04:00
|
|
|
if (! IS_LONG64_SEGMENT(cs_descriptor) || cs_descriptor.u.segment.d_b)
|
2005-02-02 00:17:57 +03:00
|
|
|
{
|
2005-04-12 22:08:10 +04:00
|
|
|
BX_ERROR(("interrupt(long mode): must be 64 bit segment"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// segment must be present, else #NP(selector + EXT)
|
2005-07-11 00:32:32 +04:00
|
|
|
if (! IS_PRESENT(cs_descriptor)) {
|
2005-04-12 22:08:10 +04:00
|
|
|
BX_ERROR(("interrupt(long mode): segment not present"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_NP_EXCEPTION, cs_selector.value & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2010-06-21 09:35:45 +04:00
|
|
|
|
|
|
|
Bit64u RSP_for_cpl_x;
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
bx_address new_SSP = 0; // keep warning silent
|
|
|
|
unsigned old_SS_DPL = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl;
|
|
|
|
unsigned old_CPL = CPL;
|
|
|
|
bx_address return_LIP = get_laddr(BX_SEG_REG_CS, RIP);
|
2021-01-30 11:35:35 +03:00
|
|
|
bool check_ss_token = true;
|
2019-12-20 10:42:07 +03:00
|
|
|
#endif
|
2010-06-21 09:35:45 +04:00
|
|
|
|
|
|
|
Bit64u old_CS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
|
|
|
Bit64u old_RIP = RIP;
|
|
|
|
Bit64u old_SS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
|
|
|
|
Bit64u old_RSP = RSP;
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// if code segment is non-conforming and DPL < CPL then
|
|
|
|
// INTERRUPT TO INNER PRIVILEGE:
|
2010-06-21 09:35:45 +04:00
|
|
|
if (IS_CODE_SEGMENT_NON_CONFORMING(cs_descriptor.type) && cs_descriptor.dpl < CPL)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
|
|
|
BX_DEBUG(("interrupt(long mode): INTERRUPT TO INNER PRIVILEGE"));
|
|
|
|
|
|
|
|
// check selector and descriptor for new stack in current TSS
|
2010-06-21 09:35:45 +04:00
|
|
|
if (ist > 0) {
|
2008-04-22 01:29:43 +04:00
|
|
|
BX_DEBUG(("interrupt(long mode): trap to IST, vector = %d", ist));
|
2008-09-06 21:44:02 +04:00
|
|
|
RSP_for_cpl_x = get_RSP_from_TSS(ist+3);
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
if (ShadowStackEnabled(0)) {
|
|
|
|
bx_address new_SSP_addr = BX_CPU_THIS_PTR msr.ia32_interrupt_ssp_table + (ist<<3);
|
|
|
|
new_SSP = system_read_qword(new_SSP_addr);
|
|
|
|
}
|
|
|
|
#endif
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-09-06 21:44:02 +04:00
|
|
|
RSP_for_cpl_x = get_RSP_from_TSS(cs_descriptor.dpl);
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
new_SSP = BX_CPU_THIS_PTR msr.ia32_pl_ssp[cs_descriptor.dpl];
|
|
|
|
#endif
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
|
|
|
|
2010-06-21 09:35:45 +04:00
|
|
|
// align stack
|
2005-08-04 23:31:59 +04:00
|
|
|
RSP_for_cpl_x &= BX_CONST64(0xfffffffffffffff0);
|
|
|
|
|
2007-10-19 14:59:39 +04:00
|
|
|
// push old stack long pointer onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 8, cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 16, cs_descriptor.dpl, old_RSP);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 24, cs_descriptor.dpl, read_eflags());
|
2007-10-19 14:59:39 +04:00
|
|
|
// push long pointer to return address onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 32, cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 40, cs_descriptor.dpl, old_RIP);
|
2007-10-19 14:59:39 +04:00
|
|
|
RSP_for_cpl_x -= 40;
|
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2007-10-19 14:59:39 +04:00
|
|
|
RSP_for_cpl_x -= 8;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x, cs_descriptor.dpl, error_code);
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
|
|
|
|
2005-08-04 01:10:42 +04:00
|
|
|
// load CS:RIP (guaranteed to be in 64 bit mode)
|
2019-12-14 20:20:35 +03:00
|
|
|
branch_far(&cs_selector, &cs_descriptor, gate_dest_offset, cs_descriptor.dpl);
|
2005-04-12 22:08:10 +04:00
|
|
|
|
2005-08-04 01:10:42 +04:00
|
|
|
// set up null SS descriptor
|
2010-02-21 09:56:48 +03:00
|
|
|
load_null_selector(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS], cs_descriptor.dpl);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2010-06-21 09:35:45 +04:00
|
|
|
else if(IS_CODE_SEGMENT_CONFORMING(cs_descriptor.type) || cs_descriptor.dpl==CPL)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
2010-06-21 09:35:45 +04:00
|
|
|
// if code segment is conforming OR code segment DPL = CPL then
|
|
|
|
// INTERRUPT TO SAME PRIVILEGE LEVEL:
|
2005-04-12 22:08:10 +04:00
|
|
|
|
2010-06-21 09:35:45 +04:00
|
|
|
BX_DEBUG(("interrupt(long mode): INTERRUPT TO SAME PRIVILEGE"));
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// check selector and descriptor for new stack in current TSS
|
|
|
|
if (ist > 0) {
|
2008-05-22 01:38:59 +04:00
|
|
|
BX_DEBUG(("interrupt(long mode): trap to IST, vector = %d", ist));
|
2010-06-21 09:35:45 +04:00
|
|
|
RSP_for_cpl_x = get_RSP_from_TSS(ist+3);
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
if (ShadowStackEnabled(CPL)) {
|
|
|
|
bx_address new_SSP_addr = BX_CPU_THIS_PTR msr.ia32_interrupt_ssp_table + (ist<<3);
|
|
|
|
new_SSP = system_read_qword(new_SSP_addr);
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-21 09:35:45 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
RSP_for_cpl_x = RSP;
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
new_SSP = SSP;
|
|
|
|
check_ss_token = false;
|
|
|
|
#endif
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// align stack
|
2010-06-21 09:35:45 +04:00
|
|
|
RSP_for_cpl_x &= BX_CONST64(0xfffffffffffffff0);
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// push flags onto stack
|
|
|
|
// push current CS selector onto stack
|
|
|
|
// push return offset onto stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 8, cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 16, cs_descriptor.dpl, old_RSP);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 24, cs_descriptor.dpl, read_eflags());
|
2010-06-21 09:35:45 +04:00
|
|
|
// push long pointer to return address onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 32, cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_qword(RSP_for_cpl_x - 40, cs_descriptor.dpl, old_RIP);
|
2010-06-21 09:35:45 +04:00
|
|
|
RSP_for_cpl_x -= 40;
|
2008-03-24 00:24:05 +03:00
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2010-06-21 09:35:45 +04:00
|
|
|
RSP_for_cpl_x -= 8;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_qword(RSP_for_cpl_x, cs_descriptor.dpl, error_code);
|
2008-03-24 00:24:05 +03:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// set the RPL field of CS to CPL
|
2019-12-14 20:20:35 +03:00
|
|
|
branch_far(&cs_selector, &cs_descriptor, gate_dest_offset, CPL);
|
2010-06-21 09:35:45 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_ERROR(("interrupt(long mode): bad descriptor type %u (CS.DPL=%u CPL=%u)",
|
|
|
|
(unsigned) cs_descriptor.type, (unsigned) cs_descriptor.dpl, (unsigned) CPL));
|
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
if(ShadowStackEnabled(old_CPL)) {
|
|
|
|
if (old_CPL == 3)
|
|
|
|
BX_CPU_THIS_PTR msr.ia32_pl_ssp[3] = SSP;
|
|
|
|
}
|
|
|
|
if (ShadowStackEnabled(CPL)) {
|
|
|
|
bx_address old_SSP = SSP;
|
|
|
|
if(check_ss_token)
|
|
|
|
shadow_stack_switch(new_SSP);
|
|
|
|
if (old_SS_DPL != 3)
|
|
|
|
call_far_shadow_stack_push(old_CS, return_LIP, old_SSP);
|
|
|
|
}
|
|
|
|
track_indirect(CPL);
|
|
|
|
#endif
|
|
|
|
|
2010-06-21 09:35:45 +04:00
|
|
|
RSP = RSP_for_cpl_x;
|
|
|
|
|
|
|
|
// if interrupt gate then set IF to 0
|
|
|
|
if (!(gate_descriptor.type & 1)) // even is int-gate
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
BX_CPU_THIS_PTR clear_TF();
|
|
|
|
//BX_CPU_THIS_PTR clear_VM(); // VM is clear in long mode
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
|
|
|
BX_CPU_THIS_PTR clear_NT();
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-30 11:35:35 +03:00
|
|
|
void BX_CPU_C::protected_mode_int(Bit8u vector, unsigned soft_int, bool push_error, Bit16u error_code)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
|
|
|
bx_descriptor_t gate_descriptor, cs_descriptor;
|
|
|
|
bx_selector_t cs_selector;
|
|
|
|
|
|
|
|
Bit16u raw_tss_selector;
|
|
|
|
bx_selector_t tss_selector;
|
|
|
|
bx_descriptor_t tss_descriptor;
|
|
|
|
|
|
|
|
// interrupt vector must be within IDT table limits,
|
2010-06-21 09:35:45 +04:00
|
|
|
// else #GP(vector*8 + 2 + EXT)
|
2008-02-16 01:05:43 +03:00
|
|
|
if ((vector*8 + 7) > BX_CPU_THIS_PTR idtr.limit) {
|
2009-02-08 20:37:19 +03:00
|
|
|
BX_ERROR(("interrupt(): vector must be within IDT table limits, IDT.limit = 0x%x", BX_CPU_THIS_PTR idtr.limit));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2008-09-06 21:44:02 +04:00
|
|
|
Bit64u desctmp = system_read_qword(BX_CPU_THIS_PTR idtr.base + vector*8);
|
|
|
|
|
2010-03-18 00:55:19 +03:00
|
|
|
Bit32u dword1 = GET32L(desctmp);
|
|
|
|
Bit32u dword2 = GET32H(desctmp);
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
parse_descriptor(dword1, dword2, &gate_descriptor);
|
|
|
|
|
|
|
|
if ((gate_descriptor.valid==0) || gate_descriptor.segment) {
|
2009-02-08 20:37:19 +03:00
|
|
|
BX_ERROR(("interrupt(): gate descriptor is not valid sys seg (vector=0x%02x)", vector));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2008-09-06 21:44:02 +04:00
|
|
|
// descriptor AR byte must indicate interrupt gate, trap gate,
|
|
|
|
// or task gate, else #GP(vector*8 + 2 + EXT)
|
2005-04-12 22:08:10 +04:00
|
|
|
switch (gate_descriptor.type) {
|
|
|
|
case BX_TASK_GATE:
|
|
|
|
case BX_286_INTERRUPT_GATE:
|
|
|
|
case BX_286_TRAP_GATE:
|
|
|
|
case BX_386_INTERRUPT_GATE:
|
|
|
|
case BX_386_TRAP_GATE:
|
|
|
|
break;
|
|
|
|
default:
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_ERROR(("interrupt(): gate.type(%u) != {5,6,7,14,15}",
|
2005-04-12 22:08:10 +04:00
|
|
|
(unsigned) gate_descriptor.type));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
2014-12-16 23:11:08 +03:00
|
|
|
// if software interrupt, then gate descriptor DPL must be >= CPL,
|
2005-04-12 22:08:10 +04:00
|
|
|
// else #GP(vector * 8 + 2 + EXT)
|
2010-03-18 00:55:19 +03:00
|
|
|
if (soft_int && gate_descriptor.dpl < CPL) {
|
|
|
|
BX_ERROR(("interrupt(): soft_int && (gate.dpl < CPL)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gate must be present, else #NP(vector * 8 + 2 + EXT)
|
2005-07-11 00:32:32 +04:00
|
|
|
if (! IS_PRESENT(gate_descriptor)) {
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_ERROR(("interrupt(): gate not present"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_NP_EXCEPTION, vector*8 + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (gate_descriptor.type) {
|
|
|
|
case BX_TASK_GATE:
|
|
|
|
// examine selector to TSS, given in task gate descriptor
|
|
|
|
raw_tss_selector = gate_descriptor.u.taskgate.tss_selector;
|
|
|
|
parse_selector(raw_tss_selector, &tss_selector);
|
|
|
|
|
|
|
|
// must specify global in the local/global bit,
|
2005-12-12 22:44:06 +03:00
|
|
|
// else #GP(TSS selector)
|
2005-04-12 22:08:10 +04:00
|
|
|
if (tss_selector.ti) {
|
2009-01-21 00:28:43 +03:00
|
|
|
BX_ERROR(("interrupt(): tss_selector.ti=1 from gate descriptor - #GP(tss_selector)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// index must be within GDT limits, else #TS(TSS selector)
|
2005-12-13 01:01:22 +03:00
|
|
|
fetch_raw_descriptor(&tss_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
parse_descriptor(dword1, dword2, &tss_descriptor);
|
|
|
|
|
2005-12-12 22:44:06 +03:00
|
|
|
// AR byte must specify available TSS,
|
|
|
|
// else #GP(TSS selector)
|
2005-04-12 22:08:10 +04:00
|
|
|
if (tss_descriptor.valid==0 || tss_descriptor.segment) {
|
2009-01-21 00:28:43 +03:00
|
|
|
BX_ERROR(("interrupt(): TSS selector points to invalid or bad TSS - #GP(tss_selector)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2008-04-18 14:19:33 +04:00
|
|
|
|
|
|
|
if (tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_286_TSS &&
|
|
|
|
tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_386_TSS)
|
|
|
|
{
|
2009-01-21 00:28:43 +03:00
|
|
|
BX_ERROR(("interrupt(): TSS selector points to bad TSS - #GP(tss_selector)"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc);
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// TSS must be present, else #NP(TSS selector)
|
2005-08-02 22:44:20 +04:00
|
|
|
if (! IS_PRESENT(tss_descriptor)) {
|
2009-01-21 00:28:43 +03:00
|
|
|
BX_ERROR(("interrupt(): TSS descriptor.p == 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_NP_EXCEPTION, raw_tss_selector & 0xfffc);
|
2005-08-02 22:44:20 +04:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// switch tasks with nesting to TSS
|
2009-01-31 13:43:24 +03:00
|
|
|
task_switch(0, &tss_selector, &tss_descriptor,
|
2012-01-12 00:21:29 +04:00
|
|
|
BX_TASK_FROM_INT, dword1, dword2, push_error, error_code);
|
2005-04-12 22:08:10 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
case BX_286_INTERRUPT_GATE:
|
|
|
|
case BX_286_TRAP_GATE:
|
|
|
|
case BX_386_INTERRUPT_GATE:
|
|
|
|
case BX_386_TRAP_GATE:
|
2019-12-20 10:42:07 +03:00
|
|
|
{
|
|
|
|
Bit16u gate_dest_selector = gate_descriptor.u.gate.dest_selector;
|
|
|
|
Bit32u gate_dest_offset = gate_descriptor.u.gate.dest_offset;
|
2003-02-26 05:48:12 +03:00
|
|
|
|
2002-09-14 21:29:47 +04:00
|
|
|
// examine CS selector and descriptor given in gate descriptor
|
|
|
|
// selector must be non-null else #GP(EXT)
|
2006-06-12 20:58:27 +04:00
|
|
|
if ((gate_dest_selector & 0xfffc) == 0) {
|
2005-07-07 22:40:35 +04:00
|
|
|
BX_ERROR(("int_trap_gate(): selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
|
|
|
parse_selector(gate_dest_selector, &cs_selector);
|
|
|
|
|
|
|
|
// selector must be within its descriptor table limits
|
|
|
|
// else #GP(selector+EXT)
|
2005-04-12 22:08:10 +04:00
|
|
|
fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
2002-09-14 21:29:47 +04:00
|
|
|
parse_descriptor(dword1, dword2, &cs_descriptor);
|
|
|
|
|
|
|
|
// descriptor AR byte must indicate code seg
|
|
|
|
// and code segment descriptor DPL<=CPL, else #GP(selector+EXT)
|
2006-06-12 20:58:27 +04:00
|
|
|
if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
|
|
|
|
IS_DATA_SEGMENT(cs_descriptor.type) ||
|
2010-06-21 09:35:45 +04:00
|
|
|
cs_descriptor.dpl > CPL)
|
2004-07-12 23:20:55 +04:00
|
|
|
{
|
2010-03-05 22:52:18 +03:00
|
|
|
BX_ERROR(("interrupt(): not accessible or not code segment cs=0x%04x", cs_selector.value));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2004-07-12 23:20:55 +04:00
|
|
|
}
|
2005-02-02 00:17:57 +03:00
|
|
|
|
2002-09-14 21:29:47 +04:00
|
|
|
// segment must be present, else #NP(selector + EXT)
|
2005-07-11 00:32:32 +04:00
|
|
|
if (! IS_PRESENT(cs_descriptor)) {
|
2006-06-10 02:29:07 +04:00
|
|
|
BX_ERROR(("interrupt(): segment not present"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_NP_EXCEPTION, cs_selector.value & 0xfffc);
|
2004-07-12 23:20:55 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
Bit32u old_ESP = ESP;
|
|
|
|
Bit16u old_SS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
|
|
|
|
Bit32u old_EIP = EIP;
|
|
|
|
Bit16u old_CS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
|
|
|
|
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
bx_address new_SSP = BX_CPU_THIS_PTR msr.ia32_pl_ssp[cs_descriptor.dpl];
|
|
|
|
Bit32u return_LIP = get_laddr(BX_SEG_REG_CS, EIP);
|
|
|
|
unsigned old_SS_DPL = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.dpl;
|
|
|
|
unsigned old_CPL = CPL;
|
|
|
|
#endif
|
|
|
|
|
2002-09-14 21:29:47 +04:00
|
|
|
// if code segment is non-conforming and DPL < CPL then
|
2008-11-10 01:08:21 +03:00
|
|
|
// INTERRUPT TO INNER PRIVILEGE
|
2010-06-21 09:35:45 +04:00
|
|
|
if(IS_CODE_SEGMENT_NON_CONFORMING(cs_descriptor.type) && cs_descriptor.dpl < CPL)
|
2004-07-12 23:20:55 +04:00
|
|
|
{
|
2019-12-20 10:42:07 +03:00
|
|
|
Bit16u SS_for_cpl_x;
|
|
|
|
Bit32u ESP_for_cpl_x;
|
2005-04-12 22:08:10 +04:00
|
|
|
bx_descriptor_t ss_descriptor;
|
|
|
|
bx_selector_t ss_selector;
|
|
|
|
int is_v8086_mode = v8086_mode();
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
BX_DEBUG(("interrupt(): INTERRUPT TO INNER PRIVILEGE"));
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2009-10-03 11:25:03 +04:00
|
|
|
// check selector and descriptor for new stack in current TSS
|
2019-12-20 10:42:07 +03:00
|
|
|
get_SS_ESP_from_TSS(cs_descriptor.dpl, &SS_for_cpl_x, &ESP_for_cpl_x);
|
2009-10-03 11:25:03 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
if (is_v8086_mode && cs_descriptor.dpl != 0) {
|
|
|
|
// if code segment DPL != 0 then #GP(new code segment selector)
|
2008-11-10 01:08:21 +03:00
|
|
|
BX_ERROR(("interrupt(): code segment DPL(%d) != 0 in v8086 mode", cs_descriptor.dpl));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2003-02-26 05:48:12 +03:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// Selector must be non-null else #TS(EXT)
|
2006-06-12 20:58:27 +04:00
|
|
|
if ((SS_for_cpl_x & 0xfffc) == 0) {
|
|
|
|
BX_ERROR(("interrupt(): SS selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_TS_EXCEPTION, 0); /* TS(ext) */
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// selector index must be within its descriptor table limits
|
|
|
|
// else #TS(SS selector + EXT)
|
|
|
|
parse_selector(SS_for_cpl_x, &ss_selector);
|
|
|
|
// fetch 2 dwords of descriptor; call handles out of limits checks
|
|
|
|
fetch_raw_descriptor(&ss_selector, &dword1, &dword2, BX_TS_EXCEPTION);
|
|
|
|
parse_descriptor(dword1, dword2, &ss_descriptor);
|
|
|
|
|
|
|
|
// selector rpl must = dpl of code segment,
|
|
|
|
// else #TS(SS selector + ext)
|
|
|
|
if (ss_selector.rpl != cs_descriptor.dpl) {
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_ERROR(("interrupt(): SS.rpl != CS.dpl"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-10-08 18:43:18 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// stack seg DPL must = DPL of code segment,
|
|
|
|
// else #TS(SS selector + ext)
|
|
|
|
if (ss_descriptor.dpl != cs_descriptor.dpl) {
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_ERROR(("interrupt(): SS.dpl != CS.dpl"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-10-08 18:43:18 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// descriptor must indicate writable data segment,
|
|
|
|
// else #TS(SS selector + EXT)
|
2006-06-12 20:58:27 +04:00
|
|
|
if (ss_descriptor.valid==0 || ss_descriptor.segment==0 ||
|
|
|
|
IS_CODE_SEGMENT(ss_descriptor.type) ||
|
|
|
|
!IS_DATA_SEGMENT_WRITEABLE(ss_descriptor.type))
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_ERROR(("interrupt(): SS is not writable data segment"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// seg must be present, else #SS(SS selector + ext)
|
2005-07-11 00:32:32 +04:00
|
|
|
if (! IS_PRESENT(ss_descriptor)) {
|
|
|
|
BX_ERROR(("interrupt(): SS not present"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_SS_EXCEPTION, SS_for_cpl_x & 0xfffc);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// IP must be within CS segment boundaries, else #GP(0)
|
|
|
|
if (gate_dest_offset > cs_descriptor.u.segment.limit_scaled) {
|
2009-02-08 20:37:19 +03:00
|
|
|
BX_ERROR(("interrupt(): gate EIP > CS.limit"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2007-10-19 14:59:39 +04:00
|
|
|
// Prepare new stack segment
|
|
|
|
bx_segment_reg_t new_stack;
|
|
|
|
new_stack.selector = ss_selector;
|
|
|
|
new_stack.cache = ss_descriptor;
|
|
|
|
new_stack.selector.rpl = cs_descriptor.dpl;
|
|
|
|
// add cpl to the selector value
|
2019-12-20 10:42:07 +03:00
|
|
|
new_stack.selector.value = (0xfffc & new_stack.selector.value) | new_stack.selector.rpl;
|
2007-10-19 14:59:39 +04:00
|
|
|
|
2008-04-26 01:21:46 +04:00
|
|
|
if (ss_descriptor.u.segment.d_b) {
|
|
|
|
Bit32u temp_ESP = ESP_for_cpl_x;
|
|
|
|
|
|
|
|
if (is_v8086_mode)
|
|
|
|
{
|
|
|
|
if (gate_descriptor.type>=14) { // 386 int/trap gate
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-4, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-8, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-12, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-16, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value);
|
|
|
|
temp_ESP -= 16;
|
|
|
|
}
|
|
|
|
else {
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP-2, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP-4, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP-6, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP-8, cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value);
|
|
|
|
temp_ESP -= 8;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
|
|
|
|
if (gate_descriptor.type>=14) { // 386 int/trap gate
|
2008-04-26 01:21:46 +04:00
|
|
|
// push long pointer to old stack onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-4, cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-8, cs_descriptor.dpl, old_ESP);
|
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-12, cs_descriptor.dpl, read_eflags());
|
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-16, cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_dword(&new_stack, temp_ESP-20, cs_descriptor.dpl, old_EIP);
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_ESP -= 20;
|
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_ESP -= 4;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_ESP, cs_descriptor.dpl, error_code);
|
2008-04-26 01:21:46 +04:00
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
2008-04-26 01:21:46 +04:00
|
|
|
else { // 286 int/trap gate
|
|
|
|
// push long pointer to old stack onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP-2, cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_word(&new_stack, temp_ESP-4, cs_descriptor.dpl, (Bit16u) old_ESP);
|
|
|
|
write_new_stack_word(&new_stack, temp_ESP-6, cs_descriptor.dpl, (Bit16u) read_eflags());
|
|
|
|
write_new_stack_word(&new_stack, temp_ESP-8, cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_word(&new_stack, temp_ESP-10, cs_descriptor.dpl, (Bit16u) old_EIP);
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_ESP -= 10;
|
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_ESP -= 2;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_ESP, cs_descriptor.dpl, error_code);
|
2008-04-26 01:21:46 +04:00
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
2008-04-26 01:21:46 +04:00
|
|
|
|
|
|
|
ESP = temp_ESP;
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
2008-04-26 01:21:46 +04:00
|
|
|
else {
|
|
|
|
Bit16u temp_SP = (Bit16u) ESP_for_cpl_x;
|
|
|
|
|
|
|
|
if (is_v8086_mode)
|
|
|
|
{
|
|
|
|
if (gate_descriptor.type>=14) { // 386 int/trap gate
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-12), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-16), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value);
|
|
|
|
temp_SP -= 16;
|
|
|
|
}
|
|
|
|
else {
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-6), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value);
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl,
|
2008-04-26 01:21:46 +04:00
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value);
|
|
|
|
temp_SP -= 8;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
|
2008-04-26 01:21:46 +04:00
|
|
|
if (gate_descriptor.type>=14) { // 386 int/trap gate
|
|
|
|
// push long pointer to old stack onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, old_ESP);
|
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-12), cs_descriptor.dpl, read_eflags());
|
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-16), cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_dword(&new_stack, (Bit16u)(temp_SP-20), cs_descriptor.dpl, old_EIP);
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_SP -= 20;
|
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_SP -= 4;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_dword(&new_stack, temp_SP, cs_descriptor.dpl, error_code);
|
2008-04-26 01:21:46 +04:00
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
2008-04-26 01:21:46 +04:00
|
|
|
else { // 286 int/trap gate
|
|
|
|
// push long pointer to old stack onto new stack
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl, old_SS);
|
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, (Bit16u) old_ESP);
|
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-6), cs_descriptor.dpl, (Bit16u) read_eflags());
|
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, old_CS);
|
|
|
|
write_new_stack_word(&new_stack, (Bit16u)(temp_SP-10), cs_descriptor.dpl, (Bit16u) old_EIP);
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_SP -= 10;
|
|
|
|
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error) {
|
2008-04-26 01:21:46 +04:00
|
|
|
temp_SP -= 2;
|
2014-10-12 22:59:10 +04:00
|
|
|
write_new_stack_word(&new_stack, temp_SP, cs_descriptor.dpl, error_code);
|
2008-04-26 01:21:46 +04:00
|
|
|
}
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
2008-04-26 01:21:46 +04:00
|
|
|
|
|
|
|
SP = temp_SP;
|
2007-10-19 14:59:39 +04:00
|
|
|
}
|
|
|
|
|
2008-04-26 01:21:46 +04:00
|
|
|
// load new CS:eIP values from gate
|
2005-04-12 22:08:10 +04:00
|
|
|
// set CPL to new code segment DPL
|
|
|
|
// set RPL of CS to CPL
|
|
|
|
load_cs(&cs_selector, &cs_descriptor, cs_descriptor.dpl);
|
2010-03-05 11:54:07 +03:00
|
|
|
|
|
|
|
// load new SS:eSP values from TSS
|
|
|
|
load_ss(&ss_selector, &ss_descriptor, cs_descriptor.dpl);
|
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
if(ShadowStackEnabled(old_CPL)) {
|
|
|
|
if (old_CPL == 3)
|
|
|
|
BX_CPU_THIS_PTR msr.ia32_pl_ssp[3] = SSP;
|
|
|
|
}
|
|
|
|
if (ShadowStackEnabled(CPL)) {
|
|
|
|
bx_address old_SSP = SSP;
|
|
|
|
shadow_stack_switch(new_SSP);
|
|
|
|
if (old_SS_DPL != 3) {
|
|
|
|
call_far_shadow_stack_push(old_CS, return_LIP, old_SSP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
track_indirect(CPL);
|
|
|
|
#endif
|
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
if (is_v8086_mode)
|
|
|
|
{
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].cache.valid = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_GS].selector.value = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].cache.valid = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_FS].selector.value = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].cache.valid = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_DS].selector.value = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].cache.valid = 0;
|
|
|
|
BX_CPU_THIS_PTR sregs[BX_SEG_REG_ES].selector.value = 0;
|
|
|
|
}
|
2004-07-12 23:20:55 +04:00
|
|
|
}
|
2008-11-10 01:08:21 +03:00
|
|
|
else
|
2004-07-12 23:20:55 +04:00
|
|
|
{
|
2008-11-10 01:08:21 +03:00
|
|
|
BX_DEBUG(("interrupt(): INTERRUPT TO SAME PRIVILEGE"));
|
|
|
|
|
|
|
|
if (v8086_mode() && (IS_CODE_SEGMENT_CONFORMING(cs_descriptor.type) || cs_descriptor.dpl != 0)) {
|
|
|
|
// if code segment DPL != 0 then #GP(new code segment selector)
|
|
|
|
BX_ERROR(("interrupt(): code segment conforming or DPL(%d) != 0 in v8086 mode", cs_descriptor.dpl));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, cs_selector.value & 0xfffc);
|
2008-11-10 01:08:21 +03:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
|
|
|
|
// EIP must be in CS limit else #GP(0)
|
|
|
|
if (gate_dest_offset > cs_descriptor.u.segment.limit_scaled) {
|
2008-11-10 01:08:21 +03:00
|
|
|
BX_ERROR(("interrupt(): IP > CS descriptor limit"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
|
|
|
// push flags onto stack
|
|
|
|
// push current CS selector onto stack
|
|
|
|
// push return offset onto stack
|
2005-04-12 22:08:10 +04:00
|
|
|
if (gate_descriptor.type >= 14) { // 386 gate
|
|
|
|
push_32(read_eflags());
|
|
|
|
push_32(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
|
|
|
push_32(EIP);
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error)
|
2005-04-12 22:08:10 +04:00
|
|
|
push_32(error_code);
|
|
|
|
}
|
|
|
|
else { // 286 gate
|
2008-04-08 21:58:56 +04:00
|
|
|
push_16((Bit16u) read_eflags());
|
2005-04-12 22:08:10 +04:00
|
|
|
push_16(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
|
|
|
push_16(IP);
|
2009-01-22 01:09:59 +03:00
|
|
|
if (push_error)
|
2005-04-12 22:08:10 +04:00
|
|
|
push_16(error_code);
|
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
if(ShadowStackEnabled(CPL)) {
|
|
|
|
call_far_shadow_stack_push(old_CS, return_LIP, SSP);
|
|
|
|
}
|
|
|
|
track_indirect(CPL);
|
|
|
|
#endif
|
|
|
|
|
2002-09-14 21:29:47 +04:00
|
|
|
// load CS:IP from gate
|
|
|
|
// load CS descriptor
|
|
|
|
// set the RPL field of CS to CPL
|
|
|
|
load_cs(&cs_selector, &cs_descriptor, CPL);
|
2004-07-12 23:20:55 +04:00
|
|
|
}
|
2002-09-14 21:29:47 +04:00
|
|
|
|
2010-06-21 09:35:45 +04:00
|
|
|
EIP = gate_dest_offset;
|
|
|
|
|
|
|
|
// if interrupt gate then set IF to 0
|
|
|
|
if (!(gate_descriptor.type & 1)) // even is int-gate
|
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
BX_CPU_THIS_PTR clear_TF();
|
|
|
|
BX_CPU_THIS_PTR clear_NT();
|
|
|
|
BX_CPU_THIS_PTR clear_VM();
|
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
|
|
|
return;
|
2019-12-20 10:42:07 +03:00
|
|
|
}
|
2005-04-12 22:08:10 +04:00
|
|
|
default:
|
|
|
|
BX_PANIC(("bad descriptor type in interrupt()!"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2021-01-30 11:35:35 +03:00
|
|
|
void BX_CPU_C::real_mode_int(Bit8u vector, bool push_error, Bit16u error_code)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
2006-06-12 20:58:27 +04:00
|
|
|
if ((vector*4+3) > BX_CPU_THIS_PTR idtr.limit) {
|
2005-04-12 22:08:10 +04:00
|
|
|
BX_ERROR(("interrupt(real mode) vector > idtr.limit"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2008-04-08 21:58:56 +04:00
|
|
|
push_16((Bit16u) read_eflags());
|
2008-09-06 21:44:02 +04:00
|
|
|
push_16(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
|
|
|
push_16(IP);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2009-03-27 19:42:21 +03:00
|
|
|
Bit16u new_ip = system_read_word(BX_CPU_THIS_PTR idtr.base + 4 * vector);
|
|
|
|
// CS.LIMIT can't change when in real/v8086 mode
|
|
|
|
if (new_ip > BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled) {
|
|
|
|
BX_ERROR(("interrupt(real mode): instruction pointer not within code segment limits"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2009-03-27 19:42:21 +03:00
|
|
|
}
|
2010-03-18 00:55:19 +03:00
|
|
|
|
|
|
|
Bit16u cs_selector = system_read_word(BX_CPU_THIS_PTR idtr.base + 4 * vector + 2);
|
2005-04-12 22:08:10 +04:00
|
|
|
load_seg_reg(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS], cs_selector);
|
2009-03-27 19:42:21 +03:00
|
|
|
EIP = new_ip;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
/* INT affects the following flags: I,T */
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR clear_IF();
|
|
|
|
BX_CPU_THIS_PTR clear_TF();
|
2005-04-12 22:08:10 +04:00
|
|
|
#if BX_CPU_LEVEL >= 4
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR clear_AC();
|
2005-04-12 22:08:10 +04:00
|
|
|
#endif
|
2006-06-12 20:58:27 +04:00
|
|
|
BX_CPU_THIS_PTR clear_RF();
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2021-01-30 11:35:35 +03:00
|
|
|
void BX_CPU_C::interrupt(Bit8u vector, unsigned type, bool push_error, Bit16u error_code)
|
2005-04-12 22:08:10 +04:00
|
|
|
{
|
|
|
|
#if BX_DEBUGGER
|
|
|
|
BX_CPU_THIS_PTR show_flag |= Flag_intsig;
|
|
|
|
#if BX_DEBUG_LINUX
|
|
|
|
if (bx_dbg.linux_syscall) {
|
2006-01-26 01:20:00 +03:00
|
|
|
if (vector == 0x80) bx_dbg_linux_syscall(BX_CPU_ID);
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
2006-06-22 23:53:58 +04:00
|
|
|
bx_dbg_interrupt(BX_CPU_ID, vector, error_code);
|
2005-04-12 22:08:10 +04:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
BX_INSTR_INTERRUPT(BX_CPU_ID, vector);
|
2009-01-22 01:09:59 +03:00
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
invalidate_prefetch_q();
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2021-01-30 11:35:35 +03:00
|
|
|
bool soft_int = 0;
|
2009-01-22 01:09:59 +03:00
|
|
|
switch(type) {
|
|
|
|
case BX_SOFTWARE_INTERRUPT:
|
|
|
|
case BX_SOFTWARE_EXCEPTION:
|
2010-03-18 00:55:19 +03:00
|
|
|
soft_int = 1;
|
2009-01-22 01:09:59 +03:00
|
|
|
break;
|
2009-02-13 12:51:57 +03:00
|
|
|
case BX_PRIVILEGED_SOFTWARE_INTERRUPT:
|
2009-01-22 01:09:59 +03:00
|
|
|
case BX_EXTERNAL_INTERRUPT:
|
|
|
|
case BX_NMI:
|
|
|
|
case BX_HARDWARE_EXCEPTION:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BX_PANIC(("interrupt(): unknown exception type %d", type));
|
|
|
|
}
|
|
|
|
|
2009-02-08 20:37:19 +03:00
|
|
|
BX_DEBUG(("interrupt(): vector = %02x, TYPE = %u, EXT = %u",
|
2009-01-22 01:09:59 +03:00
|
|
|
vector, type, (unsigned) BX_CPU_THIS_PTR EXT));
|
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
// Discard any traps and inhibits for new context; traps will
|
|
|
|
// resume upon return.
|
|
|
|
BX_CPU_THIS_PTR debug_trap = 0;
|
|
|
|
BX_CPU_THIS_PTR inhibit_mask = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2011-12-26 20:33:13 +04:00
|
|
|
#if BX_SUPPORT_VMX || BX_SUPPORT_SVM
|
2009-01-31 13:43:24 +03:00
|
|
|
BX_CPU_THIS_PTR in_event = 1;
|
|
|
|
#endif
|
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
RSP_SPECULATIVE;
|
|
|
|
|
2005-04-12 22:08:10 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2006-10-04 23:08:40 +04:00
|
|
|
if (long_mode()) {
|
2010-03-18 00:55:19 +03:00
|
|
|
long_mode_int(vector, soft_int, push_error, error_code);
|
2005-04-12 22:08:10 +04:00
|
|
|
}
|
2009-01-22 01:09:59 +03:00
|
|
|
else
|
2006-10-04 23:08:40 +04:00
|
|
|
#endif
|
2009-01-22 01:09:59 +03:00
|
|
|
{
|
2014-12-16 23:11:08 +03:00
|
|
|
// software interrupt can be redirected in v8086 mode
|
2012-01-10 12:13:34 +04:00
|
|
|
if (type != BX_SOFTWARE_INTERRUPT || !v8086_mode() || !v86_redirect_interrupt(vector))
|
|
|
|
{
|
|
|
|
if(real_mode()) {
|
|
|
|
real_mode_int(vector, push_error, error_code);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
protected_mode_int(vector, soft_int, push_error, error_code);
|
|
|
|
}
|
2009-01-22 01:09:59 +03:00
|
|
|
}
|
2005-02-02 00:17:57 +03:00
|
|
|
}
|
2009-01-31 13:43:24 +03:00
|
|
|
|
2019-12-20 10:42:07 +03:00
|
|
|
RSP_COMMIT;
|
|
|
|
|
2009-10-30 12:13:19 +03:00
|
|
|
#if BX_X86_DEBUGGER
|
|
|
|
BX_CPU_THIS_PTR in_repeat = 0;
|
|
|
|
#endif
|
|
|
|
|
2011-12-26 20:33:13 +04:00
|
|
|
#if BX_SUPPORT_VMX || BX_SUPPORT_SVM
|
2009-01-31 13:43:24 +03:00
|
|
|
BX_CPU_THIS_PTR in_event = 0;
|
|
|
|
#endif
|
2012-09-29 13:31:34 +04:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR EXT = 0;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2010-03-14 19:02:42 +03:00
|
|
|
/* Exception classes. These are used as indexes into the 'is_exception_OK'
|
|
|
|
* array below, and are stored in the 'exception' array also
|
|
|
|
*/
|
2017-03-19 00:25:06 +03:00
|
|
|
enum {
|
|
|
|
BX_ET_BENIGN = 0,
|
|
|
|
BX_ET_CONTRIBUTORY = 1,
|
|
|
|
BX_ET_PAGE_FAULT = 2,
|
|
|
|
BX_ET_DOUBLE_FAULT = 10
|
|
|
|
};
|
2010-03-14 19:02:42 +03:00
|
|
|
|
2021-01-30 11:35:35 +03:00
|
|
|
static const bool is_exception_OK[3][3] = {
|
2010-03-14 19:02:42 +03:00
|
|
|
{ 1, 1, 1 }, /* 1st exception is BENIGN */
|
|
|
|
{ 1, 0, 1 }, /* 1st exception is CONTRIBUTORY */
|
|
|
|
{ 1, 0, 0 } /* 1st exception is PAGE_FAULT */
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:25:06 +03:00
|
|
|
enum {
|
|
|
|
BX_EXCEPTION_CLASS_TRAP = 0,
|
|
|
|
BX_EXCEPTION_CLASS_FAULT = 1,
|
|
|
|
BX_EXCEPTION_CLASS_ABORT = 2
|
|
|
|
};
|
2010-03-14 19:02:42 +03:00
|
|
|
|
2012-01-11 10:27:35 +04:00
|
|
|
struct BxExceptionInfo exceptions_info[BX_CPU_HANDLED_EXCEPTIONS] = {
|
2009-01-23 12:26:24 +03:00
|
|
|
/* DE */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* DB */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
2012-01-11 10:27:35 +04:00
|
|
|
/* 02 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 }, // NMI
|
2009-01-23 12:26:24 +03:00
|
|
|
/* BP */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_TRAP, 0 },
|
|
|
|
/* OF */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_TRAP, 0 },
|
|
|
|
/* BR */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* UD */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* NM */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
2010-04-14 21:33:19 +04:00
|
|
|
/* DF */ { BX_ET_DOUBLE_FAULT, BX_EXCEPTION_CLASS_FAULT, 1 },
|
2009-01-23 12:26:24 +03:00
|
|
|
// coprocessor segment overrun (286,386 only)
|
2012-01-11 10:27:35 +04:00
|
|
|
/* 09 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
2009-01-23 12:26:24 +03:00
|
|
|
/* TS */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 1 },
|
|
|
|
/* NP */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 1 },
|
|
|
|
/* SS */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 1 },
|
|
|
|
/* GP */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 1 },
|
|
|
|
/* PF */ { BX_ET_PAGE_FAULT, BX_EXCEPTION_CLASS_FAULT, 1 },
|
2012-01-11 10:27:35 +04:00
|
|
|
/* 15 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 }, // reserved
|
2009-01-23 12:26:24 +03:00
|
|
|
/* MF */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* AC */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 1 },
|
|
|
|
/* MC */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_ABORT, 0 },
|
|
|
|
/* XM */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
2013-01-16 20:57:48 +04:00
|
|
|
/* VE */ { BX_ET_PAGE_FAULT, BX_EXCEPTION_CLASS_FAULT, 0 },
|
2019-12-20 10:42:07 +03:00
|
|
|
/* CP */ { BX_ET_CONTRIBUTORY, BX_EXCEPTION_CLASS_FAULT, 1 },
|
2012-01-11 10:27:35 +04:00
|
|
|
/* 22 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 23 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 24 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 25 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 26 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 27 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 28 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 29 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 },
|
|
|
|
/* 30 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 }, // FIXME: SVM #SF
|
|
|
|
/* 31 */ { BX_ET_BENIGN, BX_EXCEPTION_CLASS_FAULT, 0 }
|
2009-01-23 12:26:24 +03:00
|
|
|
};
|
|
|
|
|
2005-02-02 00:17:57 +03:00
|
|
|
// vector: 0..255: vector in IDT
|
|
|
|
// error_code: if exception generates and error, push this error code
|
2010-03-14 18:51:27 +03:00
|
|
|
void BX_CPU_C::exception(unsigned vector, Bit16u error_code)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2009-01-23 12:26:24 +03:00
|
|
|
unsigned exception_type = 0;
|
|
|
|
unsigned exception_class = BX_EXCEPTION_CLASS_FAULT;
|
2021-01-30 11:35:35 +03:00
|
|
|
bool push_error = 0;
|
2009-01-23 12:26:24 +03:00
|
|
|
|
|
|
|
if (vector < BX_CPU_HANDLED_EXCEPTIONS) {
|
|
|
|
push_error = exceptions_info[vector].push_error;
|
|
|
|
exception_class = exceptions_info[vector].exception_class;
|
|
|
|
exception_type = exceptions_info[vector].exception_type;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_PANIC(("exception(%u): bad vector", vector));
|
|
|
|
}
|
|
|
|
|
2014-12-18 22:45:03 +03:00
|
|
|
/* Excluding page faults and double faults, error_code may not have the
|
|
|
|
* least significant bit set correctly. This correction is applied first
|
|
|
|
* to make the change transparent to any instrumentation.
|
|
|
|
*/
|
2019-12-20 10:42:07 +03:00
|
|
|
if (vector != BX_PF_EXCEPTION && vector != BX_DF_EXCEPTION && vector != BX_CP_EXCEPTION) {
|
2009-01-23 12:26:24 +03:00
|
|
|
// Page faults have different format
|
2021-02-08 16:06:44 +03:00
|
|
|
error_code = (error_code & 0xfffe) | (Bit16u)(BX_CPU_THIS_PTR EXT);
|
2009-01-23 12:26:24 +03:00
|
|
|
}
|
|
|
|
|
2014-12-18 22:45:03 +03:00
|
|
|
BX_INSTR_EXCEPTION(BX_CPU_ID, vector, error_code);
|
|
|
|
|
|
|
|
#if BX_DEBUGGER
|
|
|
|
bx_dbg_exception(BX_CPU_ID, vector, error_code);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BX_DEBUG(("exception(0x%02x): error_code=%04x", vector, error_code));
|
|
|
|
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
2012-07-26 20:03:26 +04:00
|
|
|
VMexit_Event(BX_HARDWARE_EXCEPTION, vector, error_code, push_error);
|
2009-01-31 13:43:24 +03:00
|
|
|
#endif
|
|
|
|
|
2011-12-26 20:33:13 +04:00
|
|
|
#if BX_SUPPORT_SVM
|
|
|
|
SvmInterceptException(BX_HARDWARE_EXCEPTION, vector, error_code, push_error);
|
|
|
|
#endif
|
|
|
|
|
2012-10-05 01:30:50 +04:00
|
|
|
if (exception_class == BX_EXCEPTION_CLASS_FAULT)
|
|
|
|
{
|
2012-10-05 00:52:27 +04:00
|
|
|
// restore RIP/RSP to value before error occurred
|
|
|
|
RIP = BX_CPU_THIS_PTR prev_rip;
|
2019-12-20 10:42:07 +03:00
|
|
|
if (BX_CPU_THIS_PTR speculative_rsp) {
|
2012-10-05 00:52:27 +04:00
|
|
|
RSP = BX_CPU_THIS_PTR prev_rsp;
|
2019-12-20 10:42:07 +03:00
|
|
|
#if BX_SUPPORT_CET
|
|
|
|
SSP = BX_CPU_THIS_PTR prev_ssp;
|
|
|
|
#endif
|
|
|
|
}
|
2013-10-24 01:18:19 +04:00
|
|
|
BX_CPU_THIS_PTR speculative_rsp = 0;
|
2012-10-05 00:52:27 +04:00
|
|
|
|
2012-10-05 01:30:50 +04:00
|
|
|
if (BX_CPU_THIS_PTR last_exception_type == BX_ET_DOUBLE_FAULT)
|
|
|
|
{
|
|
|
|
debug(BX_CPU_THIS_PTR prev_rip); // print debug information to the log
|
2009-01-31 13:43:24 +03:00
|
|
|
#if BX_SUPPORT_VMX
|
2012-10-05 01:30:50 +04:00
|
|
|
VMexit_TripleFault();
|
2009-01-31 13:43:24 +03:00
|
|
|
#endif
|
2006-06-12 23:51:31 +04:00
|
|
|
#if BX_DEBUGGER
|
2014-12-16 23:11:08 +03:00
|
|
|
// trap into debugger (the same as when a PANIC occurs)
|
2012-10-05 01:30:50 +04:00
|
|
|
bx_debug_break();
|
2006-06-12 23:51:31 +04:00
|
|
|
#endif
|
2012-10-05 01:30:50 +04:00
|
|
|
if (SIM->get_param_bool(BXPN_RESET_ON_TRIPLE_FAULT)->get()) {
|
|
|
|
BX_ERROR(("exception(): 3rd (%d) exception with no resolution, shutdown status is %02xh, resetting", vector, DEV_cmos_get_reg(0x0f)));
|
|
|
|
bx_pc_system.Reset(BX_RESET_HARDWARE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BX_PANIC(("exception(): 3rd (%d) exception with no resolution", vector));
|
|
|
|
BX_ERROR(("WARNING: Any simulation after this point is completely bogus !"));
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop
|
2006-02-01 21:12:08 +03:00
|
|
|
}
|
2006-02-11 12:08:02 +03:00
|
|
|
|
2009-01-20 21:15:25 +03:00
|
|
|
if (vector != BX_DB_EXCEPTION) BX_CPU_THIS_PTR assert_RF();
|
2006-02-11 12:08:02 +03:00
|
|
|
}
|
|
|
|
|
2009-02-01 23:47:06 +03:00
|
|
|
if (vector == BX_DB_EXCEPTION) {
|
2012-08-09 17:11:25 +04:00
|
|
|
// Commit debug events to DR6: preserve DR5.BS and DR6.BD values,
|
|
|
|
// only software can clear them
|
2019-12-26 19:48:33 +03:00
|
|
|
BX_CPU_THIS_PTR dr6.val32 = (BX_CPU_THIS_PTR dr6.val32 & 0xffff6ff0) |
|
|
|
|
(BX_CPU_THIS_PTR debug_trap & 0x0000e00f);
|
2012-07-11 19:07:54 +04:00
|
|
|
|
2009-02-01 23:47:06 +03:00
|
|
|
// clear GD flag in the DR7 prior entering debug exception handler
|
2011-03-15 23:20:15 +03:00
|
|
|
BX_CPU_THIS_PTR dr7.set_GD(0);
|
2009-02-01 23:47:06 +03:00
|
|
|
}
|
2008-06-02 23:50:40 +04:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
BX_CPU_THIS_PTR EXT = 1;
|
|
|
|
|
|
|
|
/* if we've already had 1st exception, see if 2nd causes a
|
2012-10-05 00:52:27 +04:00
|
|
|
* Double Fault instead. Otherwise, just record 1st exception.
|
2001-04-10 05:04:59 +04:00
|
|
|
*/
|
2012-10-05 00:52:27 +04:00
|
|
|
if (exception_type != BX_ET_DOUBLE_FAULT) {
|
|
|
|
if (! is_exception_OK[BX_CPU_THIS_PTR last_exception_type][exception_type]) {
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_DF_EXCEPTION, 0);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2001-05-15 19:29:33 +04:00
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2012-10-05 00:52:27 +04:00
|
|
|
BX_CPU_THIS_PTR last_exception_type = exception_type;
|
2008-04-15 21:22:11 +04:00
|
|
|
|
2006-02-01 21:12:08 +03:00
|
|
|
if (real_mode()) {
|
2010-02-21 21:23:50 +03:00
|
|
|
push_error = 0; // not INT, no error code pushed
|
|
|
|
error_code = 0;
|
2004-07-12 23:20:55 +04:00
|
|
|
}
|
2010-02-21 21:23:50 +03:00
|
|
|
|
|
|
|
interrupt(vector, BX_HARDWARE_EXCEPTION, push_error, error_code);
|
2012-10-05 00:52:27 +04:00
|
|
|
|
|
|
|
BX_CPU_THIS_PTR last_exception_type = 0; // error resolved
|
|
|
|
|
2010-02-21 21:23:50 +03:00
|
|
|
longjmp(BX_CPU_THIS_PTR jmp_buf_env, 1); // go back to main decode loop
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|