2005-08-02 02:18:40 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2009-01-31 13:43:24 +03:00
|
|
|
// $Id: jmp_far.cc,v 1.18 2009-01-31 10:43:23 sshwarts Exp $
|
2005-08-02 02:18:40 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2007-11-18 02:28:33 +03:00
|
|
|
// Copyright (c) 2005 Stanislav Shwartsman
|
|
|
|
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
|
2005-08-02 02:18:40 +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
|
2005-08-02 02:18:40 +04:00
|
|
|
//
|
2008-01-29 20:13:10 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2005-08-02 02:18:40 +04:00
|
|
|
|
|
|
|
#define NEED_CPU_REG_SHORTCUTS 1
|
|
|
|
#include "bochs.h"
|
2006-03-07 01:03:16 +03:00
|
|
|
#include "cpu.h"
|
2005-08-02 02:18:40 +04:00
|
|
|
#define LOG_THIS BX_CPU_THIS_PTR
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64==0
|
|
|
|
// Make life easier merging cpu64 & cpu code.
|
|
|
|
#define RIP EIP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void BX_CPP_AttrRegparmN(3)
|
|
|
|
BX_CPU_C::jump_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
|
|
|
{
|
|
|
|
bx_descriptor_t descriptor;
|
|
|
|
bx_selector_t selector;
|
|
|
|
Bit32u dword1, dword2;
|
|
|
|
|
|
|
|
/* destination selector is not null else #GP(0) */
|
|
|
|
if ((cs_raw & 0xfffc) == 0) {
|
|
|
|
BX_ERROR(("jump_protected: cs == 0"));
|
|
|
|
exception(BX_GP_EXCEPTION, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_selector(cs_raw, &selector);
|
|
|
|
|
2006-04-26 18:05:28 +04:00
|
|
|
/* destination selector index is within its descriptor table
|
2005-08-02 02:18:40 +04:00
|
|
|
limits else #GP(selector) */
|
|
|
|
fetch_raw_descriptor(&selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
|
|
|
|
|
|
|
/* examine AR byte of destination selector for legal values: */
|
|
|
|
parse_descriptor(dword1, dword2, &descriptor);
|
|
|
|
|
2006-03-22 23:47:11 +03:00
|
|
|
if (descriptor.segment) {
|
2005-08-02 22:44:20 +04:00
|
|
|
check_cs(&descriptor, cs_raw, BX_SELECTOR_RPL(cs_raw), CPL);
|
2005-08-02 02:18:40 +04:00
|
|
|
branch_far64(&selector, &descriptor, disp, CPL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// call gate DPL must be >= CPL else #GP(gate selector)
|
|
|
|
if (descriptor.dpl < CPL) {
|
|
|
|
BX_ERROR(("jump_protected: call gate.dpl < CPL"));
|
|
|
|
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// call gate DPL must be >= gate selector RPL else #GP(gate selector)
|
|
|
|
if (descriptor.dpl < selector.rpl) {
|
|
|
|
BX_ERROR(("jump_protected: call gate.dpl < selector.rpl"));
|
|
|
|
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
|
2008-04-20 00:00:28 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (long_mode()) {
|
|
|
|
if (descriptor.type != BX_386_CALL_GATE) {
|
|
|
|
BX_ERROR(("jump_protected: gate type %u unsupported in long mode", (unsigned) descriptor.type));
|
|
|
|
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
switch (descriptor.type) {
|
|
|
|
case BX_SYS_SEGMENT_AVAIL_286_TSS:
|
|
|
|
case BX_SYS_SEGMENT_AVAIL_386_TSS:
|
|
|
|
case BX_286_CALL_GATE:
|
|
|
|
case BX_386_CALL_GATE:
|
|
|
|
case BX_TASK_GATE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BX_ERROR(("jump_protected: gate type %u unsupported", (unsigned) descriptor.type));
|
|
|
|
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// task gate must be present else #NP(gate selector)
|
|
|
|
if (! IS_PRESENT(descriptor)) {
|
|
|
|
BX_ERROR(("jump_protected: call gate.p == 0"));
|
|
|
|
exception(BX_NP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
2006-10-04 23:08:40 +04:00
|
|
|
if (long_mode()) {
|
2008-04-20 00:00:28 +04:00
|
|
|
jmp_call_gate64(&selector);
|
|
|
|
return;
|
2005-08-02 22:44:20 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-03-22 23:47:11 +03:00
|
|
|
switch (descriptor.type) {
|
2005-08-02 02:18:40 +04:00
|
|
|
case BX_SYS_SEGMENT_AVAIL_286_TSS:
|
|
|
|
case BX_SYS_SEGMENT_AVAIL_386_TSS:
|
|
|
|
|
2006-03-22 23:47:11 +03:00
|
|
|
if (descriptor.type==BX_SYS_SEGMENT_AVAIL_286_TSS)
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_DEBUG(("jump_protected: jump to 286 TSS"));
|
2005-08-02 22:44:20 +04:00
|
|
|
else
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_DEBUG(("jump_protected: jump to 386 TSS"));
|
2005-08-02 02:18:40 +04:00
|
|
|
|
|
|
|
// SWITCH_TASKS _without_ nesting to TSS
|
2009-01-31 13:43:24 +03:00
|
|
|
task_switch(i, &selector, &descriptor, BX_TASK_FROM_JUMP, dword1, dword2);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
|
|
|
// EIP must be in code seg limit, else #GP(0)
|
|
|
|
if (EIP > BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled) {
|
|
|
|
BX_ERROR(("jump_protected: EIP not within CS limits"));
|
|
|
|
exception(BX_GP_EXCEPTION, 0, 0);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case BX_TASK_GATE:
|
2009-01-31 13:43:24 +03:00
|
|
|
jmp_task_gate(i, &descriptor);
|
2005-08-02 22:44:20 +04:00
|
|
|
return;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2008-05-09 22:09:04 +04:00
|
|
|
case BX_286_CALL_GATE:
|
2005-08-02 22:44:20 +04:00
|
|
|
case BX_386_CALL_GATE:
|
2008-05-09 22:09:04 +04:00
|
|
|
jmp_call_gate(&descriptor);
|
2005-08-02 22:44:20 +04:00
|
|
|
return;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2008-04-20 00:00:28 +04:00
|
|
|
default: // can't get here
|
|
|
|
BX_PANIC(("jump_protected: gate type %u unsupported", (unsigned) descriptor.type));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2009-01-31 13:43:24 +03:00
|
|
|
void BX_CPP_AttrRegparmN(2)
|
|
|
|
BX_CPU_C::jmp_task_gate(bxInstruction_c *i, bx_descriptor_t *gate_descriptor)
|
2005-08-02 22:44:20 +04:00
|
|
|
{
|
|
|
|
Bit16u raw_tss_selector;
|
|
|
|
bx_selector_t tss_selector;
|
|
|
|
bx_descriptor_t tss_descriptor;
|
|
|
|
Bit32u dword1, dword2;
|
|
|
|
Bit32u temp_eIP;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// examine selector to TSS, given in Task Gate descriptor
|
|
|
|
// must specify global in the local/global bit else #GP(TSS selector)
|
|
|
|
raw_tss_selector = gate_descriptor->u.taskgate.tss_selector;
|
|
|
|
parse_selector(raw_tss_selector, &tss_selector);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
if (tss_selector.ti) {
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_task_gate: tss_selector.ti=1"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc, 0);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// index must be within GDT limits else #GP(TSS selector)
|
|
|
|
fetch_raw_descriptor(&tss_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// descriptor AR byte must specify available TSS
|
|
|
|
// else #GP(TSS selector)
|
|
|
|
parse_descriptor(dword1, dword2, &tss_descriptor);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
if (tss_descriptor.valid==0 || tss_descriptor.segment) {
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_task_gate: TSS selector points to bad TSS"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc, 0);
|
|
|
|
}
|
2008-04-26 23:41:28 +04:00
|
|
|
if (tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_286_TSS &&
|
|
|
|
tss_descriptor.type!=BX_SYS_SEGMENT_AVAIL_386_TSS)
|
|
|
|
{
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_task_gate: TSS selector points to bad TSS"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, raw_tss_selector & 0xfffc, 0);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// task state segment must be present, else #NP(tss selector)
|
|
|
|
if (! IS_PRESENT(tss_descriptor)) {
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_task_gate: TSS descriptor.p == 0"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_NP_EXCEPTION, raw_tss_selector & 0xfffc, 0);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// SWITCH_TASKS _without_ nesting to TSS
|
2009-01-31 13:43:24 +03:00
|
|
|
task_switch(i, &tss_selector, &tss_descriptor, BX_TASK_FROM_JUMP, dword1, dword2);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// EIP must be within code segment limit, else #GP(0)
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b)
|
|
|
|
temp_eIP = EIP;
|
|
|
|
else
|
|
|
|
temp_eIP = IP;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
if (temp_eIP > BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.limit_scaled) {
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_task_gate: EIP > CS.limit"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
void BX_CPP_AttrRegparmN(1)
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_CPU_C::jmp_call_gate(bx_descriptor_t *gate_descriptor)
|
2005-08-02 22:44:20 +04:00
|
|
|
{
|
|
|
|
bx_selector_t gate_cs_selector;
|
|
|
|
bx_descriptor_t gate_cs_descriptor;
|
|
|
|
Bit32u dword1, dword2;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2008-05-09 22:09:04 +04:00
|
|
|
if (gate_descriptor->type==BX_286_CALL_GATE)
|
|
|
|
BX_DEBUG(("jmp_call_gate: jump to 286 CALL GATE"));
|
|
|
|
else
|
|
|
|
BX_DEBUG(("jmp_call_gate: jump to 386 CALL GATE"));
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// examine selector to code segment given in call gate descriptor
|
|
|
|
// selector must not be null, else #GP(0)
|
2007-11-06 22:17:42 +03:00
|
|
|
Bit16u gate_cs_raw = gate_descriptor->u.gate.dest_selector;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
if ((gate_cs_raw & 0xfffc) == 0) {
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_call_gate: CS selector null"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, 0, 0);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
parse_selector(gate_cs_raw, &gate_cs_selector);
|
|
|
|
// selector must be within its descriptor table limits else #GP(CS selector)
|
|
|
|
fetch_raw_descriptor(&gate_cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
|
|
|
parse_descriptor(dword1, dword2, &gate_cs_descriptor);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// check code-segment descriptor
|
|
|
|
check_cs(&gate_cs_descriptor, gate_cs_raw, 0, CPL);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2007-11-06 22:17:42 +03:00
|
|
|
Bit32u temp_EIP = gate_descriptor->u.gate.dest_offset;
|
2005-08-02 22:44:20 +04:00
|
|
|
branch_far32(&gate_cs_selector, &gate_cs_descriptor, temp_EIP, CPL);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
void BX_CPP_AttrRegparmN(1)
|
2006-03-22 23:47:11 +03:00
|
|
|
BX_CPU_C::jmp_call_gate64(bx_selector_t *gate_selector)
|
2005-08-02 22:44:20 +04:00
|
|
|
{
|
2006-03-22 23:47:11 +03:00
|
|
|
bx_selector_t cs_selector;
|
2005-08-02 22:44:20 +04:00
|
|
|
Bit32u dword1, dword2, dword3;
|
2006-03-22 23:47:11 +03:00
|
|
|
bx_descriptor_t cs_descriptor;
|
|
|
|
bx_descriptor_t gate_descriptor;
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2008-04-20 00:00:28 +04:00
|
|
|
BX_DEBUG(("jmp_call_gate64: jump to CALL GATE 64"));
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2008-05-25 19:53:29 +04:00
|
|
|
fetch_raw_descriptor_64(gate_selector, &dword1, &dword2, &dword3, BX_GP_EXCEPTION);
|
2006-03-22 23:47:11 +03:00
|
|
|
parse_descriptor(dword1, dword2, &gate_descriptor);
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2007-11-06 22:17:42 +03:00
|
|
|
Bit16u dest_selector = gate_descriptor.u.gate.dest_selector;
|
2006-03-22 23:47:11 +03:00
|
|
|
// selector must not be null else #GP(0)
|
2008-02-15 22:03:54 +03:00
|
|
|
if ((dest_selector & 0xfffc) == 0) {
|
2008-04-20 00:00:28 +04:00
|
|
|
BX_ERROR(("jmp_call_gate64: selector in gate null"));
|
2005-08-02 22:44:20 +04:00
|
|
|
exception(BX_GP_EXCEPTION, 0, 0);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
2005-08-02 22:44:20 +04:00
|
|
|
|
2006-03-22 23:47:11 +03:00
|
|
|
parse_selector(dest_selector, &cs_selector);
|
|
|
|
// selector must be within its descriptor table limits,
|
|
|
|
// else #GP(code segment selector)
|
|
|
|
fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
|
|
|
parse_descriptor(dword1, dword2, &cs_descriptor);
|
|
|
|
|
|
|
|
// find the RIP in the gate_descriptor
|
2007-11-06 22:17:42 +03:00
|
|
|
Bit64u new_RIP = gate_descriptor.u.gate.dest_offset;
|
2006-03-22 23:47:11 +03:00
|
|
|
new_RIP |= ((Bit64u)dword3 << 32);
|
|
|
|
|
|
|
|
// AR byte of selected descriptor must indicate code segment,
|
|
|
|
// else #GP(code segment selector)
|
|
|
|
if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
|
2006-06-12 20:58:27 +04:00
|
|
|
IS_DATA_SEGMENT(cs_descriptor.type))
|
2006-03-22 23:47:11 +03:00
|
|
|
{
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_call_gate64: not code segment in 64-bit call gate"));
|
2006-03-22 23:47:11 +03:00
|
|
|
exception(BX_GP_EXCEPTION, dest_selector & 0xfffc, 0);
|
|
|
|
}
|
2005-08-02 22:44:20 +04:00
|
|
|
|
|
|
|
// In long mode, only 64-bit call gates are allowed, and they must point
|
|
|
|
// to 64-bit code segments, else #GP(selector)
|
2006-03-22 23:47:11 +03:00
|
|
|
if (! IS_LONG64_SEGMENT(cs_descriptor) || cs_descriptor.u.segment.d_b)
|
2005-08-02 22:44:20 +04:00
|
|
|
{
|
2008-05-09 22:09:04 +04:00
|
|
|
BX_ERROR(("jmp_call_gate64: not 64-bit code segment in 64-bit call gate"));
|
2006-03-22 23:47:11 +03:00
|
|
|
exception(BX_GP_EXCEPTION, dest_selector & 0xfffc, 0);
|
2005-08-02 22:44:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check code-segment descriptor
|
2006-03-22 23:47:11 +03:00
|
|
|
check_cs(&cs_descriptor, dest_selector, 0, CPL);
|
|
|
|
// and transfer the control
|
|
|
|
branch_far64(&cs_selector, &cs_descriptor, new_RIP, CPL);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
2005-08-02 22:44:20 +04:00
|
|
|
#endif
|