introduce bswap functions, big endian fix for CPUID

This commit is contained in:
Stanislav Shwartsman 2010-09-20 20:43:16 +00:00
parent a6ad162346
commit e0fcc80ec3
3 changed files with 65 additions and 61 deletions

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////
// $Id: bochs.h,v 1.255 2010-06-10 15:59:04 sshwarts Exp $
// $Id: bochs.h,v 1.256 2010-09-20 20:43:16 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2001-2010 The Bochs Project
@ -599,4 +599,26 @@ void bx_center_print(FILE *file, const char *line, unsigned maxwidth);
#endif
BX_CPP_INLINE Bit32u bx_bswap32(Bit32u val32)
{
Bit32u b0 = val32 & 0xff; val32 >>= 8;
Bit32u b1 = val32 & 0xff; val32 >>= 8;
Bit32u b2 = val32 & 0xff; val32 >>= 8;
Bit32u b3 = val32;
return (b0<<24) | (b1<<16) | (b2<<8) | b3;
}
BX_CPP_INLINE Bit64u bx_bswap64(Bit64u val64)
{
Bit64u b0 = val64 & 0xff; val64 >>= 8;
Bit64u b1 = val64 & 0xff; val64 >>= 8;
Bit64u b2 = val64 & 0xff; val64 >>= 8;
Bit64u b3 = val64 & 0xff; val64 >>= 8;
Bit64u b4 = val64 & 0xff; val64 >>= 8;
Bit64u b5 = val64 & 0xff; val64 >>= 8;
Bit64u b6 = val64 & 0xff; val64 >>= 8;
Bit64u b7 = val64;
return (b0<<56) | (b1<<48) | (b2<<40) | (b3<<32) | (b4<<24) | (b5<<16) | (b6<<8) | b7;
}
#endif /* BX_BOCHS_H */

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////
// $Id: bit.cc,v 1.68 2010-03-19 10:00:48 sshwarts Exp $
// $Id: bit.cc,v 1.69 2010-09-20 20:43:16 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2001-2009 The Bochs Project
@ -244,35 +244,17 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::BSWAP_RX(bxInstruction_c *i)
void BX_CPP_AttrRegparmN(1) BX_CPU_C::BSWAP_ERX(bxInstruction_c *i)
{
Bit32u val32, b0, b1, b2, b3;
Bit32u val32 = BX_READ_32BIT_REG(i->opcodeReg());
val32 = BX_READ_32BIT_REG(i->opcodeReg());
b0 = val32 & 0xff; val32 >>= 8;
b1 = val32 & 0xff; val32 >>= 8;
b2 = val32 & 0xff; val32 >>= 8;
b3 = val32;
val32 = (b0<<24) | (b1<<16) | (b2<<8) | b3;
BX_WRITE_32BIT_REGZ(i->opcodeReg(), val32);
BX_WRITE_32BIT_REGZ(i->opcodeReg(), bx_bswap32(val32));
}
#if BX_SUPPORT_X86_64
void BX_CPP_AttrRegparmN(1) BX_CPU_C::BSWAP_RRX(bxInstruction_c *i)
{
Bit64u val64, b0, b1, b2, b3, b4, b5, b6, b7;
Bit64u val64 = BX_READ_64BIT_REG(i->opcodeReg());
val64 = BX_READ_64BIT_REG(i->opcodeReg());
b0 = val64 & 0xff; val64 >>= 8;
b1 = val64 & 0xff; val64 >>= 8;
b2 = val64 & 0xff; val64 >>= 8;
b3 = val64 & 0xff; val64 >>= 8;
b4 = val64 & 0xff; val64 >>= 8;
b5 = val64 & 0xff; val64 >>= 8;
b6 = val64 & 0xff; val64 >>= 8;
b7 = val64;
val64 = (b0<<56) | (b1<<48) | (b2<<40) | (b3<<32) | (b4<<24) | (b5<<16) | (b6<<8) | b7;
BX_WRITE_64BIT_REG(i->opcodeReg(), val64);
BX_WRITE_64BIT_REG(i->opcodeReg(), bx_bswap64(val64));
}
#endif
@ -314,7 +296,7 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_EwGw(bxInstruction_c *i)
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_GdEd(bxInstruction_c *i)
{
Bit32u val32, b0, b1, b2, b3;
Bit32u val32;
if (i->modC0()) {
val32 = BX_READ_32BIT_REG(i->rm());
@ -324,24 +306,14 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_GdEd(bxInstruction_c *i)
val32 = read_virtual_dword(i->seg(), eaddr);
}
b0 = val32 & 0xff; val32 >>= 8;
b1 = val32 & 0xff; val32 >>= 8;
b2 = val32 & 0xff; val32 >>= 8;
b3 = val32;
val32 = (b0<<24) | (b1<<16) | (b2<<8) | b3;
BX_WRITE_32BIT_REGZ(i->nnn(), val32);
BX_WRITE_32BIT_REGZ(i->nnn(), bx_bswap32(val32));
}
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_EdGd(bxInstruction_c *i)
{
Bit32u val32 = BX_READ_32BIT_REG(i->nnn()), b0, b1, b2, b3;
Bit32u val32 = BX_READ_32BIT_REG(i->nnn());
b0 = val32 & 0xff; val32 >>= 8;
b1 = val32 & 0xff; val32 >>= 8;
b2 = val32 & 0xff; val32 >>= 8;
b3 = val32;
val32 = (b0<<24) | (b1<<16) | (b2<<8) | b3;
val32 = bx_bswap32(val32);
if (i->modC0()) {
BX_WRITE_32BIT_REGZ(i->rm(), val32);
@ -356,7 +328,7 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_EdGd(bxInstruction_c *i)
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_GqEq(bxInstruction_c *i)
{
Bit64u val64, b0, b1, b2, b3, b4, b5, b6, b7;
Bit64u val64;
if (i->modC0()) {
val64 = BX_READ_64BIT_REG(i->rm());
@ -366,33 +338,14 @@ void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_GqEq(bxInstruction_c *i)
val64 = read_virtual_qword(i->seg(), eaddr);
}
b0 = val64 & 0xff; val64 >>= 8;
b1 = val64 & 0xff; val64 >>= 8;
b2 = val64 & 0xff; val64 >>= 8;
b3 = val64 & 0xff; val64 >>= 8;
b4 = val64 & 0xff; val64 >>= 8;
b5 = val64 & 0xff; val64 >>= 8;
b6 = val64 & 0xff; val64 >>= 8;
b7 = val64;
val64 = (b0<<56) | (b1<<48) | (b2<<40) | (b3<<32) | (b4<<24) | (b5<<16) | (b6<<8) | b7;
BX_WRITE_64BIT_REG(i->nnn(), val64);
BX_WRITE_64BIT_REG(i->nnn(), bx_bswap64(val64));
}
void BX_CPP_AttrRegparmN(1) BX_CPU_C::MOVBE_EqGq(bxInstruction_c *i)
{
Bit64u val64 = BX_READ_64BIT_REG(i->nnn());
Bit64u b0, b1, b2, b3, b4, b5, b6, b7;
b0 = val64 & 0xff; val64 >>= 8;
b1 = val64 & 0xff; val64 >>= 8;
b2 = val64 & 0xff; val64 >>= 8;
b3 = val64 & 0xff; val64 >>= 8;
b4 = val64 & 0xff; val64 >>= 8;
b5 = val64 & 0xff; val64 >>= 8;
b6 = val64 & 0xff; val64 >>= 8;
b7 = val64;
val64 = (b0<<56) | (b1<<48) | (b2<<40) | (b3<<32) | (b4<<24) | (b5<<16) | (b6<<8) | b7;
val64 = bx_bswap64(val64);
if (i->modC0()) {
BX_WRITE_64BIT_REG(i->rm(), val64);

View File

@ -1,5 +1,5 @@
/////////////////////////////////////////////////////////////////////////
// $Id: cpuid.cc,v 1.122 2010-07-22 16:41:59 sshwarts Exp $
// $Id: cpuid.cc,v 1.123 2010-09-20 20:43:16 sshwarts Exp $
/////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2007-2010 Stanislav Shwartsman
@ -449,6 +449,11 @@ void BX_CPU_C::set_cpuid_defaults(void)
memcpy(&(cpuid->ebx), vendor_string, 4);
memcpy(&(cpuid->edx), vendor_string + 4, 4);
memcpy(&(cpuid->ecx), vendor_string + 8, 4);
#ifdef BX_BIG_ENDIAN
cpuid->ebx = bx_bswap32(cpuid->ebx);
cpuid->ecx = bx_bswap32(cpuid->ecx);
cpuid->edx = bx_bswap32(cpuid->edx);
#endif
BX_INFO(("CPUID[0x00000000]: %08x %08x %08x %08x", cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx));
@ -609,6 +614,12 @@ void BX_CPU_C::set_cpuid_defaults(void)
memcpy(&(cpuid->ecx), vendor_string + 8, 4);
#endif
#ifdef BX_BIG_ENDIAN
cpuid->ebx = bx_bswap32(cpuid->ebx);
cpuid->ecx = bx_bswap32(cpuid->ecx);
cpuid->edx = bx_bswap32(cpuid->edx);
#endif
BX_INFO(("CPUID[0x80000000]: %08x %08x %08x %08x", cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx));
// ------------------------------------------------------
@ -701,6 +712,12 @@ void BX_CPU_C::set_cpuid_defaults(void)
memcpy(&(cpuid->ebx), brand_string + 4, 4);
memcpy(&(cpuid->ecx), brand_string + 8, 4);
memcpy(&(cpuid->edx), brand_string + 12, 4);
#ifdef BX_BIG_ENDIAN
cpuid->eax = bx_bswap32(cpuid->eax);
cpuid->ebx = bx_bswap32(cpuid->ebx);
cpuid->ecx = bx_bswap32(cpuid->ecx);
cpuid->edx = bx_bswap32(cpuid->edx);
#endif
BX_INFO(("CPUID[0x80000002]: %08x %08x %08x %08x", cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx));
@ -710,6 +727,12 @@ void BX_CPU_C::set_cpuid_defaults(void)
memcpy(&(cpuid->ebx), brand_string + 20, 4);
memcpy(&(cpuid->ecx), brand_string + 24, 4);
memcpy(&(cpuid->edx), brand_string + 28, 4);
#ifdef BX_BIG_ENDIAN
cpuid->eax = bx_bswap32(cpuid->eax);
cpuid->ebx = bx_bswap32(cpuid->ebx);
cpuid->ecx = bx_bswap32(cpuid->ecx);
cpuid->edx = bx_bswap32(cpuid->edx);
#endif
BX_INFO(("CPUID[0x80000003]: %08x %08x %08x %08x", cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx));
@ -719,6 +742,12 @@ void BX_CPU_C::set_cpuid_defaults(void)
memcpy(&(cpuid->ebx), brand_string + 36, 4);
memcpy(&(cpuid->ecx), brand_string + 40, 4);
memcpy(&(cpuid->edx), brand_string + 44, 4);
#ifdef BX_BIG_ENDIAN
cpuid->eax = bx_bswap32(cpuid->eax);
cpuid->ebx = bx_bswap32(cpuid->ebx);
cpuid->ecx = bx_bswap32(cpuid->ecx);
cpuid->edx = bx_bswap32(cpuid->edx);
#endif
BX_INFO(("CPUID[0x80000004]: %08x %08x %08x %08x", cpuid->eax, cpuid->ebx, cpuid->ecx, cpuid->edx));