Bochs/bochs/cpu/xsave.cc

445 lines
14 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008-2011 Stanislav Shwartsman
// Written by Stanislav Shwartsman [sshwarts at sourceforge net]
//
// 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
//
/////////////////////////////////////////////////////////////////////////
#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR
/* 0F AE /4 */
BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::XSAVE(bxInstruction_c *i)
{
2010-02-27 01:53:43 +03:00
#if BX_CPU_LEVEL >= 6
2008-02-13 19:45:21 +03:00
unsigned index;
BxPackedXmmRegister xmm;
BX_CPU_THIS_PTR prepareXSAVE();
2008-12-06 13:21:55 +03:00
BX_DEBUG(("XSAVE: save processor state XCR0=0x%08x", BX_CPU_THIS_PTR xcr0.get32()));
2008-02-13 19:45:21 +03:00
bx_address eaddr = BX_CPU_CALL_METHODR(i->ResolveModrm, (i));
bx_address laddr = get_laddr(i->seg(), eaddr);
2008-02-13 19:45:21 +03:00
if (laddr & 0x3f) {
BX_ERROR(("XSAVE: access not aligned to 64-byte"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2010-10-19 02:19:45 +04:00
bx_address asize_mask = i->asize_mask();
2008-02-13 19:45:21 +03:00
//
// We will go feature-by-feature and not run over all XCR0 bits
//
2010-10-19 02:19:45 +04:00
Bit64u header1 = read_virtual_qword(i->seg(), (eaddr + 512) & asize_mask);
2011-10-09 13:19:49 +04:00
Bit32u features_save_enable_mask = BX_CPU_THIS_PTR xcr0.get32() & EAX;
2008-02-13 19:45:21 +03:00
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_save_enable_mask & BX_XCR0_FPU_MASK) != 0)
2008-02-13 19:45:21 +03:00
{
xmm.xmm16u(0) = BX_CPU_THIS_PTR the_i387.get_control_word();
2008-05-10 17:34:47 +04:00
xmm.xmm16u(1) = BX_CPU_THIS_PTR the_i387.get_status_word();
xmm.xmm16u(2) = pack_FPU_TW(BX_CPU_THIS_PTR the_i387.get_tag_word());
2008-02-13 19:45:21 +03:00
/* x87 FPU Opcode (16 bits) */
/* The lower 11 bits contain the FPU opcode, upper 5 bits are reserved */
xmm.xmm16u(3) = BX_CPU_THIS_PTR the_i387.foo;
/*
* x87 FPU IP Offset (32/64 bits)
* The contents of this field differ depending on the current
* addressing mode (16/32/64 bit) when the FXSAVE instruction was executed:
* + 64-bit mode - 64-bit IP offset
* + 32-bit mode - 32-bit IP offset
* + 16-bit mode - low 16 bits are IP offset; high 16 bits are reserved.
* x87 CS FPU IP Selector
* + 16 bit, in 16/32 bit mode only
*/
#if BX_SUPPORT_X86_64
if (i->os64L()) {
xmm.xmm64u(1) = (BX_CPU_THIS_PTR the_i387.fip);
}
else
#endif
{
2008-08-16 16:19:30 +04:00
xmm.xmm32u(2) = (Bit32u)(BX_CPU_THIS_PTR the_i387.fip);
2008-02-13 19:45:21 +03:00
xmm.xmm32u(3) = (BX_CPU_THIS_PTR the_i387.fcs);
}
write_virtual_dqword(i->seg(), eaddr, (Bit8u *) &xmm);
2008-02-13 19:45:21 +03:00
/*
* x87 FPU Instruction Operand (Data) Pointer Offset (32/64 bits)
* The contents of this field differ depending on the current
* addressing mode (16/32 bit) when the FXSAVE instruction was executed:
* + 64-bit mode - 64-bit offset
* + 32-bit mode - 32-bit offset
* + 16-bit mode - low 16 bits are offset; high 16 bits are reserved.
* x87 DS FPU Instruction Operand (Data) Pointer Selector
* + 16 bit, in 16/32 bit mode only
*/
#if BX_SUPPORT_X86_64
if (i->os64L()) {
2010-10-19 02:19:45 +04:00
write_virtual_qword(i->seg(), (eaddr + 16) & asize_mask, BX_CPU_THIS_PTR the_i387.fdp);
2008-02-13 19:45:21 +03:00
}
else
#endif
{
2010-10-19 02:19:45 +04:00
write_virtual_dword(i->seg(), (eaddr + 16) & asize_mask, (Bit32u) BX_CPU_THIS_PTR the_i387.fdp);
write_virtual_dword(i->seg(), (eaddr + 20) & asize_mask, (Bit32u) BX_CPU_THIS_PTR the_i387.fds);
2008-02-13 19:45:21 +03:00
}
/* do not touch MXCSR state */
/* store i387 register file */
for(index=0; index < 8; index++)
{
2010-07-02 00:00:33 +04:00
const floatx80 &fp = BX_READ_FPU_REG(index);
2008-02-13 19:45:21 +03:00
xmm.xmm64u(0) = fp.fraction;
xmm.xmm64u(1) = 0;
xmm.xmm16u(4) = fp.exp;
2010-10-19 02:19:45 +04:00
write_virtual_dqword(i->seg(), (eaddr+index*16+32) & asize_mask, (Bit8u *) &xmm);
2008-02-13 19:45:21 +03:00
}
header1 |= BX_XCR0_FPU_MASK;
2008-02-13 19:45:21 +03:00
}
2008-02-13 19:45:21 +03:00
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_save_enable_mask & (BX_XCR0_SSE_MASK | BX_XCR0_AVX_MASK)) != 0)
2008-02-13 19:45:21 +03:00
{
2011-10-09 13:19:49 +04:00
// store MXCSR
2010-10-19 02:19:45 +04:00
write_virtual_dword(i->seg(), (eaddr + 24) & asize_mask, BX_MXCSR_REGISTER);
write_virtual_dword(i->seg(), (eaddr + 28) & asize_mask, MXCSR_MASK);
2011-10-09 13:19:49 +04:00
}
2008-02-13 19:45:21 +03:00
2011-10-09 13:19:49 +04:00
/////////////////////////////////////////////////////////////////////////////
if ((features_save_enable_mask & BX_XCR0_SSE_MASK) != 0)
{
2008-02-13 19:45:21 +03:00
/* store XMM register file */
for(index=0; index < BX_XMM_REGISTERS; index++)
{
// save XMM8-XMM15 only in 64-bit mode
2009-11-30 00:01:26 +03:00
if (index < 8 || long64_mode()) {
write_virtual_dqword(i->seg(),
2010-10-19 02:19:45 +04:00
(eaddr+index*16+160) & asize_mask, (Bit8u *)(&BX_READ_XMM_REG(index)));
2008-02-13 19:45:21 +03:00
}
}
header1 |= BX_XCR0_SSE_MASK;
2008-02-13 19:45:21 +03:00
}
#if BX_SUPPORT_AVX
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_save_enable_mask & BX_XCR0_AVX_MASK) != 0)
{
/* store AVX state */
for(index=0; index < BX_XMM_REGISTERS; index++)
{
// save YMM8-YMM15 only in 64-bit mode
if (index < 8 || long64_mode()) {
write_virtual_dqword(i->seg(),
2011-10-09 13:19:49 +04:00
(eaddr+index*16+576) & asize_mask, (Bit8u *)(&BX_READ_AVX_REG_LINE(index, 1)));
}
}
header1 |= BX_XCR0_AVX_MASK;
}
#endif
// always update header to 'dirty' state
2010-10-19 02:19:45 +04:00
write_virtual_qword(i->seg(), (eaddr + 512) & asize_mask, header1);
#endif
BX_NEXT_INSTR(i);
}
/* 0F AE /5 */
BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::XRSTOR(bxInstruction_c *i)
{
2010-02-27 01:53:43 +03:00
#if BX_CPU_LEVEL >= 6
2008-02-13 19:45:21 +03:00
unsigned index;
BxPackedXmmRegister xmm;
BX_CPU_THIS_PTR prepareXSAVE();
2008-12-06 13:21:55 +03:00
BX_DEBUG(("XRSTOR: restore processor state XCR0=0x%08x", BX_CPU_THIS_PTR xcr0.get32()));
2008-02-13 19:45:21 +03:00
bx_address eaddr = BX_CPU_CALL_METHODR(i->ResolveModrm, (i));
bx_address laddr = get_laddr(i->seg(), eaddr);
2008-02-13 19:45:21 +03:00
if (laddr & 0x3f) {
BX_ERROR(("XRSTOR: access not aligned to 64-byte"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2010-10-19 02:19:45 +04:00
bx_address asize_mask = i->asize_mask();
Bit64u header1 = read_virtual_qword(i->seg(), (eaddr + 512) & asize_mask);
Bit64u header2 = read_virtual_qword(i->seg(), (eaddr + 520) & asize_mask);
Bit64u header3 = read_virtual_qword(i->seg(), (eaddr + 528) & asize_mask);
2008-02-13 19:45:21 +03:00
2008-12-06 13:21:55 +03:00
if ((~BX_CPU_THIS_PTR xcr0.get32() & header1) != 0) {
2008-02-13 19:45:21 +03:00
BX_ERROR(("XRSTOR: Broken header state"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
if (header2 != 0 || header3 != 0) {
BX_ERROR(("XRSTOR: Reserved header state is not '0"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
//
// We will go feature-by-feature and not run over all XCR0 bits
//
2011-10-09 13:19:49 +04:00
Bit32u features_load_enable_mask = BX_CPU_THIS_PTR xcr0.get32() & EAX;
2008-02-13 19:45:21 +03:00
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_load_enable_mask & BX_XCR0_FPU_MASK) != 0)
2008-02-13 19:45:21 +03:00
{
if (header1 & BX_XCR0_FPU_MASK) {
// load FPU state from XSAVE area
read_virtual_dqword(i->seg(), eaddr, (Bit8u *) &xmm);
2008-02-13 19:45:21 +03:00
BX_CPU_THIS_PTR the_i387.cwd = xmm.xmm16u(0);
BX_CPU_THIS_PTR the_i387.swd = xmm.xmm16u(1);
BX_CPU_THIS_PTR the_i387.tos = (xmm.xmm16u(1) >> 11) & 0x07;
2011-06-14 23:56:28 +04:00
/* always set bit 6 as '1 */
BX_CPU_THIS_PTR the_i387.cwd =
(BX_CPU_THIS_PTR the_i387.cwd & ~FPU_CW_Reserved_Bits) | 0x0040;
2008-02-13 19:45:21 +03:00
/* Restore x87 FPU Opcode */
/* The lower 11 bits contain the FPU opcode, upper 5 bits are reserved */
BX_CPU_THIS_PTR the_i387.foo = xmm.xmm16u(3) & 0x7FF;
2008-02-13 19:45:21 +03:00
/* Restore x87 FPU IP */
#if BX_SUPPORT_X86_64
if (i->os64L()) {
BX_CPU_THIS_PTR the_i387.fip = xmm.xmm64u(1);
BX_CPU_THIS_PTR the_i387.fcs = 0;
}
else
#endif
{
BX_CPU_THIS_PTR the_i387.fip = xmm.xmm32u(2);
2008-08-16 16:19:30 +04:00
BX_CPU_THIS_PTR the_i387.fcs = xmm.xmm16u(6);
2008-02-13 19:45:21 +03:00
}
Bit32u tag_byte = xmm.xmmubyte(4);
/* Restore x87 FPU DP */
2010-10-19 02:19:45 +04:00
read_virtual_dqword(i->seg(), (eaddr + 16) & asize_mask, (Bit8u *) &xmm);
2008-02-13 19:45:21 +03:00
#if BX_SUPPORT_X86_64
if (i->os64L()) {
BX_CPU_THIS_PTR the_i387.fdp = xmm.xmm64u(0);
BX_CPU_THIS_PTR the_i387.fds = 0;
}
else
#endif
{
BX_CPU_THIS_PTR the_i387.fdp = xmm.xmm32u(0);
BX_CPU_THIS_PTR the_i387.fds = xmm.xmm16u(2);
}
/* load i387 register file */
for(index=0; index < 8; index++)
{
floatx80 reg;
2010-10-19 02:19:45 +04:00
reg.fraction = read_virtual_qword(i->seg(), (eaddr+index*16+32) & asize_mask);
reg.exp = read_virtual_word (i->seg(), (eaddr+index*16+40) & asize_mask);
2010-07-02 00:00:33 +04:00
// update tag only if it is not empty
BX_WRITE_FPU_REGISTER_AND_TAG(reg,
IS_TAG_EMPTY(index) ? FPU_Tag_Empty : FPU_tagof(reg), index);
2008-02-13 19:45:21 +03:00
}
/* Restore floating point tag word - see desription for FXRSTOR instruction */
BX_CPU_THIS_PTR the_i387.twd = unpack_FPU_TW(tag_byte);
/* check for unmasked exceptions */
if (FPU_PARTIAL_STATUS & ~FPU_CONTROL_WORD & FPU_CW_Exceptions_Mask) {
/* set the B and ES bits in the status-word */
FPU_PARTIAL_STATUS |= FPU_SW_Summary | FPU_SW_Backward;
}
else {
/* clear the B and ES bits in the status-word */
FPU_PARTIAL_STATUS &= ~(FPU_SW_Summary | FPU_SW_Backward);
}
2008-02-13 19:45:21 +03:00
}
else {
// initialize FPU with reset values
BX_CPU_THIS_PTR the_i387.init();
2008-02-13 19:45:21 +03:00
for (index=0;index<8;index++) {
2011-06-14 23:56:28 +04:00
static floatx80 reg = { 0, 0 };
BX_FPU_REG(index) = reg;
}
2008-02-13 19:45:21 +03:00
}
}
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_load_enable_mask & (BX_XCR0_SSE_MASK | BX_XCR0_AVX_MASK)) != 0)
2008-02-13 19:45:21 +03:00
{
2010-10-19 02:19:45 +04:00
Bit32u new_mxcsr = read_virtual_dword(i->seg(), (eaddr + 24) & asize_mask);
2008-02-13 19:45:21 +03:00
if(new_mxcsr & ~MXCSR_MASK)
exception(BX_GP_EXCEPTION, 0);
2009-10-18 21:33:35 +04:00
BX_MXCSR_REGISTER = new_mxcsr;
2011-10-09 13:19:49 +04:00
}
static BxPackedXmmRegister xmmnil; /* compiler will clear the variable */
2008-02-13 19:45:21 +03:00
2011-10-09 13:19:49 +04:00
/////////////////////////////////////////////////////////////////////////////
if ((features_load_enable_mask & BX_XCR0_SSE_MASK) != 0)
{
2008-02-13 19:45:21 +03:00
if (header1 & BX_XCR0_SSE_MASK) {
// load SSE state from XSAVE area
for(index=0; index < BX_XMM_REGISTERS; index++)
{
// restore XMM8-XMM15 only in 64-bit mode
2009-11-30 00:01:26 +03:00
if (index < 8 || long64_mode()) {
read_virtual_dqword(i->seg(),
2010-10-19 02:19:45 +04:00
(eaddr+index*16+160) & asize_mask, (Bit8u *)(&BX_READ_XMM_REG(index)));
2008-02-13 19:45:21 +03:00
}
}
}
else {
// initialize SSE with reset values
for(index=0; index < BX_XMM_REGISTERS; index++) {
// set XMM8-XMM15 only in 64-bit mode
if (index < 8 || long64_mode())
BX_WRITE_XMM_REG(index, xmmnil);
}
}
}
#if BX_SUPPORT_AVX
/////////////////////////////////////////////////////////////////////////////
2011-10-09 13:19:49 +04:00
if ((features_load_enable_mask & BX_XCR0_AVX_MASK) != 0)
{
if (header1 & BX_XCR0_AVX_MASK) {
// load AVX state from XSAVE area
for(index=0; index < BX_XMM_REGISTERS; index++)
{
// restore YMM8-YMM15 only in 64-bit mode
2009-11-30 00:01:26 +03:00
if (index < 8 || long64_mode()) {
read_virtual_dqword(i->seg(),
(eaddr+index*16+576) & asize_mask, (Bit8u *)(&BX_READ_AVX_REG_LINE(index, 1)));
}
}
}
else {
// initialize upper part of AVX registers with reset values
for(index=0; index < BX_XMM_REGISTERS; index++) {
// set YMM8-YMM15 only in 64-bit mode
if (index < 8 || long64_mode()) {
for (int j=2;j < BX_VLMAX*2;j++)
BX_CPU_THIS_PTR vmm[index].avx64u(j) = 0;
}
2008-02-13 19:45:21 +03:00
}
}
}
#endif
#endif
BX_NEXT_INSTR(i);
}
/* 0F 01 D0 */
BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::XGETBV(bxInstruction_c *i)
{
2010-02-27 01:53:43 +03:00
#if BX_CPU_LEVEL >= 6
2009-12-05 00:27:17 +03:00
if(! BX_CPU_THIS_PTR cr4.get_OSXSAVE()) {
2008-02-13 19:45:21 +03:00
BX_ERROR(("XGETBV: OSXSAVE feature is not enabled in CR4!"));
exception(BX_UD_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2008-02-13 19:45:21 +03:00
// For now hardcoded handle only XCR0 register, it should take a few
// years until extension will be required
if (ECX != 0) {
BX_ERROR(("XGETBV: Invalid XCR register %d", ECX));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2008-02-13 19:45:21 +03:00
RDX = 0;
2008-12-06 13:21:55 +03:00
RAX = BX_CPU_THIS_PTR xcr0.get32();
#endif
BX_NEXT_INSTR(i);
}
/* 0F 01 D1 */
BX_INSF_TYPE BX_CPP_AttrRegparmN(1) BX_CPU_C::XSETBV(bxInstruction_c *i)
{
2010-02-27 01:53:43 +03:00
#if BX_CPU_LEVEL >= 6
2009-12-05 00:27:17 +03:00
if(! BX_CPU_THIS_PTR cr4.get_OSXSAVE()) {
2008-02-13 19:45:21 +03:00
BX_ERROR(("XSETBV: OSXSAVE feature is not enabled in CR4!"));
exception(BX_UD_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2011-07-30 00:22:35 +04:00
#if BX_SUPPORT_VMX
if (BX_CPU_THIS_PTR in_vmx_guest) {
BX_ERROR(("VMEXIT: XSETBV in VMX non-root operation"));
2011-07-30 17:21:31 +04:00
VMexit(i, VMX_VMEXIT_XSETBV, 0);
2011-07-30 00:22:35 +04:00
}
#endif
2011-08-24 01:25:34 +04:00
// CPL is always 3 in vm8086 mode
if (/* v8086_mode() || */ CPL != 0) {
2008-02-13 19:45:21 +03:00
BX_ERROR(("XSETBV: The current priveledge level is not 0"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
// For now hardcoded handle only XCR0 register, it should take a few
// years until extension will be required
if (ECX != 0) {
BX_ERROR(("XSETBV: Invalid XCR register %d", ECX));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
if (EDX != 0 || (EAX & ~BX_CPU_THIS_PTR xcr0_suppmask) != 0 || (EAX & 1) == 0) {
2008-02-13 19:45:21 +03:00
BX_ERROR(("XSETBV: Attempting to change reserved bits!"));
exception(BX_GP_EXCEPTION, 0);
2008-02-13 19:45:21 +03:00
}
2011-03-20 21:27:31 +03:00
#if BX_SUPPORT_AVX
if ((EAX & (BX_XCR0_AVX_BIT | BX_XCR0_SSE_BIT)) == BX_XCR0_AVX_BIT) {
BX_ERROR(("XSETBV: Attempting to set AVX without SSE!"));
exception(BX_GP_EXCEPTION, 0);
}
#endif
2008-12-06 13:21:55 +03:00
BX_CPU_THIS_PTR xcr0.set32(EAX);
#if BX_SUPPORT_AVX
handleAvxModeChange();
#endif
#endif // BX_CPU_LEVEL >= 6
BX_NEXT_TRACE(i);
}