2005-08-02 02:18:40 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2011-02-25 00:54:04 +03:00
|
|
|
// $Id$
|
2005-08-02 02:18:40 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2009-10-15 00:45:29 +04:00
|
|
|
// Copyright (c) 2005-2009 Stanislav Shwartsman
|
2007-11-18 02:28:33 +03:00
|
|
|
// 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
|
|
|
|
|
|
|
|
void BX_CPP_AttrRegparmN(2)
|
|
|
|
BX_CPU_C::return_protected(bxInstruction_c *i, Bit16u pop_bytes)
|
|
|
|
{
|
|
|
|
Bit16u raw_cs_selector, raw_ss_selector;
|
|
|
|
bx_selector_t cs_selector, ss_selector;
|
|
|
|
bx_descriptor_t cs_descriptor, ss_descriptor;
|
2005-08-02 22:44:20 +04:00
|
|
|
Bit32u stack_param_offset;
|
2005-08-02 02:18:40 +04:00
|
|
|
bx_address return_RIP, return_RSP, temp_RSP;
|
|
|
|
Bit32u dword1, dword2;
|
|
|
|
|
|
|
|
/* + 6+N*2: SS | +12+N*4: SS | +24+N*8 SS */
|
|
|
|
/* + 4+N*2: SP | + 8+N*4: ESP | +16+N*8 RSP */
|
|
|
|
/* parm N | + parm N | + parm N */
|
|
|
|
/* parm 3 | + parm 3 | + parm 3 */
|
|
|
|
/* parm 2 | + parm 2 | + parm 2 */
|
|
|
|
/* + 4: parm 1 | + 8: parm 1 | +16: parm 1 */
|
|
|
|
/* + 2: CS | + 4: CS | + 8: CS */
|
|
|
|
/* + 0: IP | + 0: EIP | + 0: RIP */
|
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (StackAddrSize64()) temp_RSP = RSP;
|
2008-02-03 00:46:54 +03:00
|
|
|
else
|
2005-08-02 22:44:20 +04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b) temp_RSP = ESP;
|
|
|
|
else temp_RSP = SP;
|
|
|
|
}
|
|
|
|
|
2005-08-02 02:18:40 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2006-06-10 02:29:07 +04:00
|
|
|
if (i->os64L()) {
|
2008-05-10 22:10:53 +04:00
|
|
|
raw_cs_selector = (Bit16u) read_virtual_qword_64(BX_SEG_REG_SS, temp_RSP + 8);
|
|
|
|
return_RIP = read_virtual_qword_64(BX_SEG_REG_SS, temp_RSP);
|
2005-08-02 02:18:40 +04:00
|
|
|
stack_param_offset = 16;
|
2008-02-03 00:46:54 +03:00
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
else
|
|
|
|
#endif
|
2006-06-10 02:29:07 +04:00
|
|
|
if (i->os32L()) {
|
2008-06-13 12:02:22 +04:00
|
|
|
raw_cs_selector = (Bit16u) read_virtual_dword(BX_SEG_REG_SS, temp_RSP + 4);
|
|
|
|
return_RIP = read_virtual_dword(BX_SEG_REG_SS, temp_RSP);
|
2005-08-02 02:18:40 +04:00
|
|
|
stack_param_offset = 8;
|
|
|
|
}
|
|
|
|
else {
|
2008-06-13 12:02:22 +04:00
|
|
|
raw_cs_selector = read_virtual_word(BX_SEG_REG_SS, temp_RSP + 2);
|
|
|
|
return_RIP = read_virtual_word(BX_SEG_REG_SS, temp_RSP);
|
2005-08-02 02:18:40 +04:00
|
|
|
stack_param_offset = 4;
|
|
|
|
}
|
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// selector must be non-null else #GP(0)
|
2006-06-10 02:29:07 +04:00
|
|
|
if ((raw_cs_selector & 0xfffc) == 0) {
|
|
|
|
BX_ERROR(("return_protected: CS selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
parse_selector(raw_cs_selector, &cs_selector);
|
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// selector index must be within its descriptor table limits,
|
|
|
|
// else #GP(selector)
|
|
|
|
fetch_raw_descriptor(&cs_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
|
|
|
|
|
|
|
// descriptor AR byte must indicate code segment, else #GP(selector)
|
|
|
|
parse_descriptor(dword1, dword2, &cs_descriptor);
|
|
|
|
|
|
|
|
// return selector RPL must be >= CPL, else #GP(return selector)
|
2005-08-02 02:18:40 +04:00
|
|
|
if (cs_selector.rpl < CPL) {
|
|
|
|
BX_ERROR(("return_protected: CS.rpl < CPL"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_cs_selector & 0xfffc);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
|
2005-08-02 22:44:20 +04:00
|
|
|
// check code-segment descriptor
|
|
|
|
check_cs(&cs_descriptor, raw_cs_selector, 0, cs_selector.rpl);
|
|
|
|
|
2005-08-02 02:18:40 +04:00
|
|
|
// if return selector RPL == CPL then
|
|
|
|
// RETURN TO SAME PRIVILEGE LEVEL
|
|
|
|
if (cs_selector.rpl == CPL)
|
|
|
|
{
|
2005-08-02 22:44:20 +04:00
|
|
|
BX_DEBUG(("return_protected: return to SAME PRIVILEGE LEVEL"));
|
2005-08-02 02:18:40 +04:00
|
|
|
|
|
|
|
branch_far64(&cs_selector, &cs_descriptor, return_RIP, CPL);
|
|
|
|
|
|
|
|
#if BX_SUPPORT_X86_64
|
2008-02-03 00:46:54 +03:00
|
|
|
if (StackAddrSize64())
|
2005-08-02 22:44:20 +04:00
|
|
|
RSP += stack_param_offset + pop_bytes;
|
2008-02-03 00:46:54 +03:00
|
|
|
else
|
2005-08-02 02:18:40 +04:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS].cache.u.segment.d_b)
|
2008-04-23 21:25:21 +04:00
|
|
|
RSP = ESP + stack_param_offset + pop_bytes;
|
2005-08-02 02:18:40 +04:00
|
|
|
else
|
|
|
|
SP += stack_param_offset + pop_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* RETURN TO OUTER PRIVILEGE LEVEL */
|
|
|
|
else {
|
|
|
|
/* + 6+N*2: SS | +12+N*4: SS | +24+N*8 SS */
|
|
|
|
/* + 4+N*2: SP | + 8+N*4: ESP | +16+N*8 RSP */
|
|
|
|
/* parm N | + parm N | + parm N */
|
|
|
|
/* parm 3 | + parm 3 | + parm 3 */
|
|
|
|
/* parm 2 | + parm 2 | + parm 2 */
|
|
|
|
/* + 4: parm 1 | + 8: parm 1 | +16: parm 1 */
|
|
|
|
/* + 2: CS | + 4: CS | + 8: CS */
|
|
|
|
/* + 0: IP | + 0: EIP | + 0: RIP */
|
|
|
|
|
2008-04-21 01:44:13 +04:00
|
|
|
BX_DEBUG(("return_protected: return to OUTER PRIVILEGE LEVEL"));
|
|
|
|
|
2005-08-02 02:18:40 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
|
|
|
if (i->os64L()) {
|
2008-06-12 23:14:40 +04:00
|
|
|
raw_ss_selector = read_virtual_word_64(BX_SEG_REG_SS, temp_RSP + 24 + pop_bytes);
|
2008-05-10 22:10:53 +04:00
|
|
|
return_RSP = read_virtual_qword_64(BX_SEG_REG_SS, temp_RSP + 16 + pop_bytes);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (i->os32L()) {
|
2008-06-13 12:02:22 +04:00
|
|
|
raw_ss_selector = read_virtual_word(BX_SEG_REG_SS, temp_RSP + 12 + pop_bytes);
|
|
|
|
return_RSP = read_virtual_dword(BX_SEG_REG_SS, temp_RSP + 8 + pop_bytes);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
else {
|
2008-06-13 12:02:22 +04:00
|
|
|
raw_ss_selector = read_virtual_word(BX_SEG_REG_SS, temp_RSP + 6 + pop_bytes);
|
|
|
|
return_RSP = read_virtual_word(BX_SEG_REG_SS, temp_RSP + 4 + pop_bytes);
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* selector index must be within its descriptor table limits,
|
|
|
|
* else #GP(selector) */
|
|
|
|
parse_selector(raw_ss_selector, &ss_selector);
|
2005-08-02 22:44:20 +04:00
|
|
|
|
|
|
|
if ((raw_ss_selector & 0xfffc) == 0) {
|
2010-04-29 23:56:10 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2006-10-04 23:08:40 +04:00
|
|
|
if (long_mode()) {
|
2005-08-04 01:01:02 +04:00
|
|
|
if (! IS_LONG64_SEGMENT(cs_descriptor) || (cs_selector.rpl == 3)) {
|
2005-08-02 22:44:20 +04:00
|
|
|
BX_ERROR(("return_protected: SS selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-08-02 22:44:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else // not in long or compatibility mode
|
2010-04-29 23:56:10 +04:00
|
|
|
#endif
|
2005-08-02 22:44:20 +04:00
|
|
|
{
|
|
|
|
BX_ERROR(("return_protected: SS selector null"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, 0);
|
2005-08-02 22:44:20 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-19 23:00:35 +03:00
|
|
|
else {
|
|
|
|
fetch_raw_descriptor(&ss_selector, &dword1, &dword2, BX_GP_EXCEPTION);
|
|
|
|
parse_descriptor(dword1, dword2, &ss_descriptor);
|
|
|
|
|
|
|
|
/* selector RPL must = RPL of the return CS selector,
|
|
|
|
* else #GP(selector) */
|
|
|
|
if (ss_selector.rpl != cs_selector.rpl) {
|
|
|
|
BX_ERROR(("return_protected: ss.rpl != cs.rpl"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
|
2009-11-19 23:00:35 +03:00
|
|
|
}
|
2005-08-02 22:44:20 +04:00
|
|
|
|
2009-11-19 23:00:35 +03:00
|
|
|
/* descriptor AR byte must indicate a writable data segment,
|
|
|
|
* else #GP(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(("return_protected: SS.AR byte not writable data"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
|
2009-11-19 23:00:35 +03:00
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2009-11-19 23:00:35 +03:00
|
|
|
/* descriptor dpl must = RPL of the return CS selector,
|
|
|
|
* else #GP(selector) */
|
|
|
|
if (ss_descriptor.dpl != cs_selector.rpl) {
|
|
|
|
BX_ERROR(("return_protected: SS.dpl != cs.rpl"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_GP_EXCEPTION, raw_ss_selector & 0xfffc);
|
2009-11-19 23:00:35 +03:00
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
2009-11-19 23:00:35 +03:00
|
|
|
/* segment must be present else #SS(selector) */
|
|
|
|
if (! IS_PRESENT(ss_descriptor)) {
|
|
|
|
BX_ERROR(("return_protected: ss.present == 0"));
|
2010-03-14 18:51:27 +03:00
|
|
|
exception(BX_SS_EXCEPTION, raw_ss_selector & 0xfffc);
|
2009-11-19 23:00:35 +03:00
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
branch_far64(&cs_selector, &cs_descriptor, return_RIP, cs_selector.rpl);
|
|
|
|
|
2009-11-19 23:00:35 +03:00
|
|
|
if ((raw_ss_selector & 0xfffc) != 0) {
|
|
|
|
// load SS:RSP from stack
|
|
|
|
// load the SS-cache with SS descriptor
|
|
|
|
load_ss(&ss_selector, &ss_descriptor, cs_selector.rpl);
|
|
|
|
}
|
2005-08-02 22:44:20 +04:00
|
|
|
#if BX_SUPPORT_X86_64
|
2009-11-19 23:00:35 +03:00
|
|
|
else {
|
2010-04-29 23:56:10 +04:00
|
|
|
// we are in 64-bit mode (checked above)
|
2009-11-19 23:00:35 +03:00
|
|
|
load_null_selector(&BX_CPU_THIS_PTR sregs[BX_SEG_REG_SS], raw_ss_selector);
|
|
|
|
}
|
|
|
|
|
2008-04-23 21:25:21 +04:00
|
|
|
if (StackAddrSize64())
|
|
|
|
RSP = return_RSP + pop_bytes;
|
2008-02-03 00:46:54 +03:00
|
|
|
else
|
2005-08-02 22:44:20 +04:00
|
|
|
#endif
|
2010-04-29 23:56:10 +04:00
|
|
|
{
|
|
|
|
if (ss_descriptor.u.segment.d_b)
|
|
|
|
RSP = (Bit32u)(return_RSP + pop_bytes);
|
|
|
|
else
|
|
|
|
SP = (Bit16u)(return_RSP + pop_bytes);
|
|
|
|
}
|
2005-08-02 02:18:40 +04:00
|
|
|
|
|
|
|
/* check ES, DS, FS, GS for validity */
|
|
|
|
validate_seg_regs();
|
|
|
|
}
|
|
|
|
}
|