bugfix
This commit is contained in:
parent
30c9eef6f9
commit
33ad9296ff
@ -1,5 +1,5 @@
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// $Id: call_far.cc,v 1.50 2009-10-14 20:45:29 sshwarts Exp $
|
||||
// $Id: call_far.cc,v 1.51 2009-12-22 11:58:25 sshwarts Exp $
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2005-2009 Stanislav Shwartsman
|
||||
@ -136,8 +136,6 @@ BX_CPU_C::call_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
||||
else { // gate & special segment
|
||||
bx_descriptor_t gate_descriptor = cs_descriptor;
|
||||
bx_selector_t gate_selector = cs_selector;
|
||||
Bit32u new_EIP;
|
||||
Bit16u dest_selector;
|
||||
|
||||
// descriptor DPL must be >= CPL else #GP(gate selector)
|
||||
if (gate_descriptor.dpl < CPL) {
|
||||
@ -158,6 +156,12 @@ BX_CPU_C::call_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
||||
BX_ERROR(("call_protected: gate type %u unsupported in long mode", (unsigned) gate_descriptor.type));
|
||||
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
||||
}
|
||||
// gate descriptor must be present else #NP(gate selector)
|
||||
if (! IS_PRESENT(gate_descriptor)) {
|
||||
BX_ERROR(("call_protected: call gate not present"));
|
||||
exception(BX_NP_EXCEPTION, cs_raw & 0xfffc, 0);
|
||||
}
|
||||
|
||||
call_gate64(&gate_selector);
|
||||
return;
|
||||
}
|
||||
@ -188,245 +192,12 @@ BX_CPU_C::call_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
||||
|
||||
case BX_286_CALL_GATE:
|
||||
case BX_386_CALL_GATE:
|
||||
// examine code segment selector in call gate descriptor
|
||||
BX_DEBUG(("call_protected: call gate"));
|
||||
|
||||
// gate descriptor must be present else #NP(gate selector)
|
||||
if (! IS_PRESENT(gate_descriptor)) {
|
||||
BX_ERROR(("call_protected: gate not present"));
|
||||
exception(BX_NP_EXCEPTION, cs_raw & 0xfffc, 0);
|
||||
}
|
||||
|
||||
dest_selector = gate_descriptor.u.gate.dest_selector;
|
||||
new_EIP = gate_descriptor.u.gate.dest_offset;
|
||||
|
||||
// selector must not be null else #GP(0)
|
||||
if ((dest_selector & 0xfffc) == 0) {
|
||||
BX_ERROR(("call_protected: selector in gate null"));
|
||||
exception(BX_GP_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// AR byte of selected descriptor must indicate code segment,
|
||||
// else #GP(code segment selector)
|
||||
// DPL of selected descriptor must be <= CPL,
|
||||
// else #GP(code segment selector)
|
||||
if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
|
||||
IS_DATA_SEGMENT(cs_descriptor.type) ||
|
||||
cs_descriptor.dpl > CPL)
|
||||
{
|
||||
BX_ERROR(("call_protected: selected descriptor is not code"));
|
||||
exception(BX_GP_EXCEPTION, dest_selector & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// code segment must be present else #NP(selector)
|
||||
if (! IS_PRESENT(cs_descriptor)) {
|
||||
BX_ERROR(("call_protected: code segment not present !"));
|
||||
exception(BX_NP_EXCEPTION, dest_selector & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// CALL GATE TO MORE PRIVILEGE
|
||||
// if non-conforming code segment and DPL < CPL then
|
||||
if (IS_CODE_SEGMENT_NON_CONFORMING(cs_descriptor.type) && (cs_descriptor.dpl < CPL))
|
||||
{
|
||||
Bit16u SS_for_cpl_x;
|
||||
Bit32u ESP_for_cpl_x;
|
||||
bx_selector_t ss_selector;
|
||||
bx_descriptor_t ss_descriptor;
|
||||
Bit16u return_SS, return_CS;
|
||||
Bit32u return_ESP, return_EIP;
|
||||
|
||||
BX_DEBUG(("CALL GATE TO MORE PRIVILEGE LEVEL"));
|
||||
|
||||
// get new SS selector for new privilege level from TSS
|
||||
get_SS_ESP_from_TSS(cs_descriptor.dpl, &SS_for_cpl_x, &ESP_for_cpl_x);
|
||||
|
||||
// check selector & descriptor for new SS:
|
||||
// selector must not be null, else #TS(0)
|
||||
if ((SS_for_cpl_x & 0xfffc) == 0) {
|
||||
BX_ERROR(("call_protected: new SS null"));
|
||||
exception(BX_TS_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
// selector index must be within its descriptor table limits,
|
||||
// else #TS(SS selector)
|
||||
parse_selector(SS_for_cpl_x, &ss_selector);
|
||||
fetch_raw_descriptor(&ss_selector, &dword1, &dword2, BX_TS_EXCEPTION);
|
||||
parse_descriptor(dword1, dword2, &ss_descriptor);
|
||||
|
||||
// selector's RPL must equal DPL of code segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_selector.rpl != cs_descriptor.dpl) {
|
||||
BX_ERROR(("call_protected: SS selector.rpl != CS descr.dpl"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// stack segment DPL must equal DPL of code segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_descriptor.dpl != cs_descriptor.dpl) {
|
||||
BX_ERROR(("call_protected: SS descr.rpl != CS descr.dpl"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// descriptor must indicate writable data segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_descriptor.valid==0 || ss_descriptor.segment==0 ||
|
||||
IS_CODE_SEGMENT(ss_descriptor.type) ||
|
||||
!IS_DATA_SEGMENT_WRITEABLE(ss_descriptor.type))
|
||||
{
|
||||
BX_ERROR(("call_protected: ss descriptor is not writable data seg"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// segment must be present, else #SS(SS selector)
|
||||
if (! IS_PRESENT(ss_descriptor)) {
|
||||
BX_ERROR(("call_protected: ss descriptor not present"));
|
||||
exception(BX_SS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// get word count from call gate, mask to 5 bits
|
||||
unsigned param_count = gate_descriptor.u.gate.param_count & 0x1f;
|
||||
|
||||
// save return SS:eSP to be pushed on new stack
|
||||
return_SS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
|
||||
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
|
||||
return_ESP = ESP;
|
||||
else
|
||||
return_ESP = SP;
|
||||
|
||||
// save return CS:eIP to be pushed on new stack
|
||||
return_CS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
||||
if (cs_descriptor.u.segment.d_b)
|
||||
return_EIP = EIP;
|
||||
else
|
||||
return_EIP = IP;
|
||||
|
||||
// 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
|
||||
new_stack.selector.value = (0xfffc & new_stack.selector.value) |
|
||||
new_stack.selector.rpl;
|
||||
|
||||
/* load new SS:SP value from TSS */
|
||||
if (ss_descriptor.u.segment.d_b) {
|
||||
Bit32u temp_ESP = ESP_for_cpl_x;
|
||||
|
||||
// push pointer of old stack onto new stack
|
||||
if (gate_descriptor.type==BX_386_CALL_GATE) {
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-8, cs_descriptor.dpl, return_ESP);
|
||||
temp_ESP -= 8;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_ESP -= 4;
|
||||
Bit32u param = read_virtual_dword_32(BX_SEG_REG_SS, return_ESP + (n-1)*4);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-8, cs_descriptor.dpl, return_EIP);
|
||||
temp_ESP -= 8;
|
||||
}
|
||||
else {
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-2, cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, (Bit16u) return_ESP);
|
||||
temp_ESP -= 4;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_ESP -= 2;
|
||||
Bit16u param = read_virtual_word_32(BX_SEG_REG_SS, return_ESP + (n-1)*2);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-2, cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, (Bit16u) return_EIP);
|
||||
temp_ESP -= 4;
|
||||
}
|
||||
|
||||
ESP = temp_ESP;
|
||||
}
|
||||
else {
|
||||
Bit16u temp_SP = (Bit16u) ESP_for_cpl_x;
|
||||
|
||||
// push pointer of old stack onto new stack
|
||||
if (gate_descriptor.type==BX_386_CALL_GATE) {
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, return_ESP);
|
||||
temp_SP -= 8;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_SP -= 4;
|
||||
Bit32u param = read_virtual_dword_32(BX_SEG_REG_SS, return_ESP + (n-1)*4);
|
||||
write_new_stack_dword_32(&new_stack, temp_SP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, return_EIP);
|
||||
temp_SP -= 8;
|
||||
}
|
||||
else {
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, (Bit16u) return_ESP);
|
||||
temp_SP -= 4;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_SP -= 2;
|
||||
Bit16u param = read_virtual_word_32(BX_SEG_REG_SS, return_ESP + (n-1)*2);
|
||||
write_new_stack_word_32(&new_stack, temp_SP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, (Bit16u) return_EIP);
|
||||
temp_SP -= 4;
|
||||
}
|
||||
|
||||
SP = temp_SP;
|
||||
}
|
||||
|
||||
// new eIP must be in code segment limit else #GP(0)
|
||||
if (new_EIP > cs_descriptor.u.segment.limit_scaled) {
|
||||
BX_ERROR(("call_protected: EIP not within CS limits"));
|
||||
exception(BX_GP_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
/* load SS descriptor */
|
||||
load_ss(&ss_selector, &ss_descriptor, cs_descriptor.dpl);
|
||||
|
||||
/* load new CS:IP value from gate */
|
||||
/* load CS descriptor */
|
||||
/* set CPL to stack segment DPL */
|
||||
/* set RPL of CS to CPL */
|
||||
load_cs(&cs_selector, &cs_descriptor, cs_descriptor.dpl);
|
||||
EIP = new_EIP;
|
||||
}
|
||||
else // CALL GATE TO SAME PRIVILEGE
|
||||
{
|
||||
BX_DEBUG(("CALL GATE TO SAME PRIVILEGE"));
|
||||
|
||||
if (gate_descriptor.type == BX_386_CALL_GATE) {
|
||||
// call gate 32bit, push return address onto stack
|
||||
push_32(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
||||
push_32(EIP);
|
||||
}
|
||||
else {
|
||||
// call gate 16bit, push return address onto stack
|
||||
push_16(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
||||
push_16(IP);
|
||||
}
|
||||
|
||||
// load CS:EIP from gate
|
||||
// load code segment descriptor into CS register
|
||||
// set RPL of CS to CPL
|
||||
branch_far32(&cs_selector, &cs_descriptor, new_EIP, CPL);
|
||||
}
|
||||
call_gate(&gate_selector, &gate_descriptor);
|
||||
return;
|
||||
|
||||
default: // can't get here
|
||||
@ -436,6 +207,245 @@ BX_CPU_C::call_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
||||
}
|
||||
}
|
||||
|
||||
void BX_CPP_AttrRegparmN(2) BX_CPU_C::call_gate(bx_selector_t *gate_selector, bx_descriptor_t *gate_descriptor)
|
||||
{
|
||||
bx_selector_t cs_selector;
|
||||
Bit32u dword1, dword2;
|
||||
bx_descriptor_t cs_descriptor;
|
||||
|
||||
// examine code segment selector in call gate descriptor
|
||||
BX_DEBUG(("call_protected: call gate"));
|
||||
|
||||
Bit16u dest_selector = gate_descriptor->u.gate.dest_selector;
|
||||
Bit32u new_EIP = gate_descriptor->u.gate.dest_offset;
|
||||
|
||||
// selector must not be null else #GP(0)
|
||||
if ((dest_selector & 0xfffc) == 0) {
|
||||
BX_ERROR(("call_protected: selector in gate null"));
|
||||
exception(BX_GP_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// AR byte of selected descriptor must indicate code segment,
|
||||
// else #GP(code segment selector)
|
||||
// DPL of selected descriptor must be <= CPL,
|
||||
// else #GP(code segment selector)
|
||||
if (cs_descriptor.valid==0 || cs_descriptor.segment==0 ||
|
||||
IS_DATA_SEGMENT(cs_descriptor.type) || cs_descriptor.dpl > CPL)
|
||||
{
|
||||
BX_ERROR(("call_protected: selected descriptor is not code"));
|
||||
exception(BX_GP_EXCEPTION, dest_selector & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// code segment must be present else #NP(selector)
|
||||
if (! IS_PRESENT(cs_descriptor)) {
|
||||
BX_ERROR(("call_protected: code segment not present !"));
|
||||
exception(BX_NP_EXCEPTION, dest_selector & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// CALL GATE TO MORE PRIVILEGE
|
||||
// if non-conforming code segment and DPL < CPL then
|
||||
if (IS_CODE_SEGMENT_NON_CONFORMING(cs_descriptor.type) && (cs_descriptor.dpl < CPL))
|
||||
{
|
||||
Bit16u SS_for_cpl_x;
|
||||
Bit32u ESP_for_cpl_x;
|
||||
bx_selector_t ss_selector;
|
||||
bx_descriptor_t ss_descriptor;
|
||||
Bit16u return_SS, return_CS;
|
||||
Bit32u return_ESP, return_EIP;
|
||||
|
||||
BX_DEBUG(("CALL GATE TO MORE PRIVILEGE LEVEL"));
|
||||
|
||||
// get new SS selector for new privilege level from TSS
|
||||
get_SS_ESP_from_TSS(cs_descriptor.dpl, &SS_for_cpl_x, &ESP_for_cpl_x);
|
||||
|
||||
// check selector & descriptor for new SS:
|
||||
// selector must not be null, else #TS(0)
|
||||
if ((SS_for_cpl_x & 0xfffc) == 0) {
|
||||
BX_ERROR(("call_protected: new SS null"));
|
||||
exception(BX_TS_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
// selector index must be within its descriptor table limits,
|
||||
// else #TS(SS selector)
|
||||
parse_selector(SS_for_cpl_x, &ss_selector);
|
||||
fetch_raw_descriptor(&ss_selector, &dword1, &dword2, BX_TS_EXCEPTION);
|
||||
parse_descriptor(dword1, dword2, &ss_descriptor);
|
||||
|
||||
// selector's RPL must equal DPL of code segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_selector.rpl != cs_descriptor.dpl) {
|
||||
BX_ERROR(("call_protected: SS selector.rpl != CS descr.dpl"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// stack segment DPL must equal DPL of code segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_descriptor.dpl != cs_descriptor.dpl) {
|
||||
BX_ERROR(("call_protected: SS descr.rpl != CS descr.dpl"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// descriptor must indicate writable data segment,
|
||||
// else #TS(SS selector)
|
||||
if (ss_descriptor.valid==0 || ss_descriptor.segment==0 ||
|
||||
IS_CODE_SEGMENT(ss_descriptor.type) || !IS_DATA_SEGMENT_WRITEABLE(ss_descriptor.type))
|
||||
{
|
||||
BX_ERROR(("call_protected: ss descriptor is not writable data seg"));
|
||||
exception(BX_TS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// segment must be present, else #SS(SS selector)
|
||||
if (! IS_PRESENT(ss_descriptor)) {
|
||||
BX_ERROR(("call_protected: ss descriptor not present"));
|
||||
exception(BX_SS_EXCEPTION, SS_for_cpl_x & 0xfffc, 0);
|
||||
}
|
||||
|
||||
// get word count from call gate, mask to 5 bits
|
||||
unsigned param_count = gate_descriptor->u.gate.param_count & 0x1f;
|
||||
|
||||
// save return SS:eSP to be pushed on new stack
|
||||
return_SS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].selector.value;
|
||||
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
|
||||
return_ESP = ESP;
|
||||
else
|
||||
return_ESP = SP;
|
||||
|
||||
// save return CS:eIP to be pushed on new stack
|
||||
return_CS = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
||||
if (cs_descriptor.u.segment.d_b)
|
||||
return_EIP = EIP;
|
||||
else
|
||||
return_EIP = IP;
|
||||
|
||||
// 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
|
||||
new_stack.selector.value = (0xfffc & new_stack.selector.value) |
|
||||
new_stack.selector.rpl;
|
||||
|
||||
/* load new SS:SP value from TSS */
|
||||
if (ss_descriptor.u.segment.d_b) {
|
||||
Bit32u temp_ESP = ESP_for_cpl_x;
|
||||
|
||||
// push pointer of old stack onto new stack
|
||||
if (gate_descriptor->type==BX_386_CALL_GATE) {
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-8, cs_descriptor.dpl, return_ESP);
|
||||
temp_ESP -= 8;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_ESP -= 4;
|
||||
Bit32u param = read_virtual_dword_32(BX_SEG_REG_SS, return_ESP + (n-1)*4);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_dword_32(&new_stack, temp_ESP-8, cs_descriptor.dpl, return_EIP);
|
||||
temp_ESP -= 8;
|
||||
}
|
||||
else {
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-2, cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, (Bit16u) return_ESP);
|
||||
temp_ESP -= 4;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_ESP -= 2;
|
||||
Bit16u param = read_virtual_word_32(BX_SEG_REG_SS, return_ESP + (n-1)*2);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-2, cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_word_32(&new_stack, temp_ESP-4, cs_descriptor.dpl, (Bit16u) return_EIP);
|
||||
temp_ESP -= 4;
|
||||
}
|
||||
|
||||
ESP = temp_ESP;
|
||||
}
|
||||
else {
|
||||
Bit16u temp_SP = (Bit16u) ESP_for_cpl_x;
|
||||
|
||||
// push pointer of old stack onto new stack
|
||||
if (gate_descriptor->type==BX_386_CALL_GATE) {
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, return_ESP);
|
||||
temp_SP -= 8;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_SP -= 4;
|
||||
Bit32u param = read_virtual_dword_32(BX_SEG_REG_SS, return_ESP + (n-1)*4);
|
||||
write_new_stack_dword_32(&new_stack, temp_SP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_dword_32(&new_stack, (Bit16u)(temp_SP-8), cs_descriptor.dpl, return_EIP);
|
||||
temp_SP -= 8;
|
||||
}
|
||||
else {
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl, return_SS);
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, (Bit16u) return_ESP);
|
||||
temp_SP -= 4;
|
||||
|
||||
for (unsigned n=param_count; n>0; n--) {
|
||||
temp_SP -= 2;
|
||||
Bit16u param = read_virtual_word_32(BX_SEG_REG_SS, return_ESP + (n-1)*2);
|
||||
write_new_stack_word_32(&new_stack, temp_SP, cs_descriptor.dpl, param);
|
||||
}
|
||||
// push return address onto new stack
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-2), cs_descriptor.dpl, return_CS);
|
||||
write_new_stack_word_32(&new_stack, (Bit16u)(temp_SP-4), cs_descriptor.dpl, (Bit16u) return_EIP);
|
||||
temp_SP -= 4;
|
||||
}
|
||||
|
||||
SP = temp_SP;
|
||||
}
|
||||
|
||||
// new eIP must be in code segment limit else #GP(0)
|
||||
if (new_EIP > cs_descriptor.u.segment.limit_scaled) {
|
||||
BX_ERROR(("call_protected: EIP not within CS limits"));
|
||||
exception(BX_GP_EXCEPTION, 0, 0);
|
||||
}
|
||||
|
||||
/* load SS descriptor */
|
||||
load_ss(&ss_selector, &ss_descriptor, cs_descriptor.dpl);
|
||||
|
||||
/* load new CS:IP value from gate */
|
||||
/* load CS descriptor */
|
||||
/* set CPL to stack segment DPL */
|
||||
/* set RPL of CS to CPL */
|
||||
load_cs(&cs_selector, &cs_descriptor, cs_descriptor.dpl);
|
||||
EIP = new_EIP;
|
||||
}
|
||||
else // CALL GATE TO SAME PRIVILEGE
|
||||
{
|
||||
BX_DEBUG(("CALL GATE TO SAME PRIVILEGE"));
|
||||
|
||||
if (gate_descriptor->type == BX_386_CALL_GATE) {
|
||||
// call gate 32bit, push return address onto stack
|
||||
push_32(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
||||
push_32(EIP);
|
||||
}
|
||||
else {
|
||||
// call gate 16bit, push return address onto stack
|
||||
push_16(BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value);
|
||||
push_16(IP);
|
||||
}
|
||||
|
||||
// load CS:EIP from gate
|
||||
// load code segment descriptor into CS register
|
||||
// set RPL of CS to CPL
|
||||
branch_far32(&cs_selector, &cs_descriptor, new_EIP, CPL);
|
||||
}
|
||||
}
|
||||
|
||||
#if BX_SUPPORT_X86_64
|
||||
void BX_CPP_AttrRegparmN(1) BX_CPU_C::call_gate64(bx_selector_t *gate_selector)
|
||||
{
|
||||
@ -450,12 +460,6 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::call_gate64(bx_selector_t *gate_selector)
|
||||
fetch_raw_descriptor_64(gate_selector, &dword1, &dword2, &dword3, BX_GP_EXCEPTION);
|
||||
parse_descriptor(dword1, dword2, &gate_descriptor);
|
||||
|
||||
// gate descriptor must be present else #NP(gate selector)
|
||||
if (! IS_PRESENT(gate_descriptor)) {
|
||||
BX_ERROR(("call_gate64: call gate not present"));
|
||||
exception(BX_NP_EXCEPTION, gate_selector->value & 0xfffc, 0);
|
||||
}
|
||||
|
||||
Bit16u dest_selector = gate_descriptor.u.gate.dest_selector;
|
||||
// selector must not be null else #GP(0)
|
||||
if ((dest_selector & 0xfffc) == 0) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// $Id: cpu.h,v 1.629 2009-12-21 13:38:06 sshwarts Exp $
|
||||
// $Id: cpu.h,v 1.630 2009-12-22 11:58:26 sshwarts Exp $
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2001-2009 The Bochs Project
|
||||
@ -3126,6 +3126,7 @@ public: // for now...
|
||||
BX_SMF void task_gate(bxInstruction_c *i, bx_selector_t *selector, bx_descriptor_t *gate_descriptor, unsigned source);
|
||||
BX_SMF void jump_protected(bxInstruction_c *i, Bit16u cs, bx_address disp) BX_CPP_AttrRegparmN(3);
|
||||
BX_SMF void jmp_call_gate(bx_selector_t *selector, bx_descriptor_t *gate_descriptor) BX_CPP_AttrRegparmN(2);
|
||||
BX_SMF void call_gate(bx_selector_t *gate_selector, bx_descriptor_t *gate_descriptor) BX_CPP_AttrRegparmN(2);
|
||||
#if BX_SUPPORT_X86_64
|
||||
BX_SMF void jmp_call_gate64(bx_selector_t *selector) BX_CPP_AttrRegparmN(1);
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// $Id: jmp_far.cc,v 1.21 2009-10-14 20:45:29 sshwarts Exp $
|
||||
// $Id: jmp_far.cc,v 1.22 2009-12-22 11:58:26 sshwarts Exp $
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2005-2009 Stanislav Shwartsman
|
||||
@ -77,6 +77,12 @@ BX_CPU_C::jump_protected(bxInstruction_c *i, Bit16u cs_raw, bx_address disp)
|
||||
BX_ERROR(("jump_protected: gate type %u unsupported in long mode", (unsigned) descriptor.type));
|
||||
exception(BX_GP_EXCEPTION, cs_raw & 0xfffc, 0);
|
||||
}
|
||||
// gate must be present else #NP(gate selector)
|
||||
if (! IS_PRESENT(descriptor)) {
|
||||
BX_ERROR(("jump_protected: call gate not present!"));
|
||||
exception(BX_NP_EXCEPTION, cs_raw & 0xfffc, 0);
|
||||
}
|
||||
|
||||
jmp_call_gate64(&selector);
|
||||
return;
|
||||
}
|
||||
@ -233,12 +239,6 @@ BX_CPU_C::jmp_call_gate64(bx_selector_t *gate_selector)
|
||||
fetch_raw_descriptor_64(gate_selector, &dword1, &dword2, &dword3, BX_GP_EXCEPTION);
|
||||
parse_descriptor(dword1, dword2, &gate_descriptor);
|
||||
|
||||
// gate must be present else #NP(gate selector)
|
||||
if (! IS_PRESENT(gate_descriptor)) {
|
||||
BX_ERROR(("jmp_call_gate64: call gate.p == 0"));
|
||||
exception(BX_NP_EXCEPTION, gate_selector->value & 0xfffc, 0);
|
||||
}
|
||||
|
||||
Bit16u dest_selector = gate_descriptor.u.gate.dest_selector;
|
||||
// selector must not be null else #GP(0)
|
||||
if ((dest_selector & 0xfffc) == 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user