2001-04-10 06:20:02 +04:00
|
|
|
// Copyright (C) 2001 MandrakeSoft S.A.
|
2001-04-10 05:04:59 +04:00
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// This is the glue logic needed to connect the wm-FPU-emu
|
|
|
|
// FPU emulator written by Bill Metzenthen to bochs.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "bochs.h"
|
2001-09-15 10:55:14 +04:00
|
|
|
#include <math.h>
|
2002-12-12 18:29:45 +03:00
|
|
|
|
|
|
|
#if !BX_WITH_MACOS
|
2001-04-10 05:04:59 +04:00
|
|
|
extern "C" {
|
2002-12-12 18:29:45 +03:00
|
|
|
#endif
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
#include "fpu_emu.h"
|
|
|
|
#include "linux/signal.h"
|
2002-12-12 18:29:45 +03:00
|
|
|
|
|
|
|
#if !BX_WITH_MACOS
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2002-12-12 18:29:45 +03:00
|
|
|
#endif
|
2001-04-10 05:04:59 +04:00
|
|
|
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
#define LOG_THIS genlog->
|
2001-05-23 12:16:07 +04:00
|
|
|
#if BX_USE_CPU_SMF
|
|
|
|
#define this (BX_CPU(0))
|
|
|
|
#endif
|
merge in BRANCH-io-cleanup.
To see the commit logs for this use either cvsweb or
cvs update -r BRANCH-io-cleanup and then 'cvs log' the various files.
In general this provides a generic interface for logging.
logfunctions:: is a class that is inherited by some classes, and also
. allocated as a standalone global called 'genlog'. All logging uses
. one of the ::info(), ::error(), ::ldebug(), ::panic() methods of this
. class through 'BX_INFO(), BX_ERROR(), BX_DEBUG(), BX_PANIC()' macros
. respectively.
.
. An example usage:
. BX_INFO(("Hello, World!\n"));
iofunctions:: is a class that is allocated once by default, and assigned
as the iofunction of each logfunctions instance. It is this class that
maintains the file descriptor and other output related code, at this
point using vfprintf(). At some future point, someone may choose to
write a gui 'console' for bochs to which messages would be redirected
simply by assigning a different iofunction class to the various logfunctions
objects.
More cleanup is coming, but this works for now. If you want to see alot
of debugging output, in main.cc, change onoff[LOGLEV_DEBUG]=0 to =1.
Comments, bugs, flames, to me: todd@fries.net
2001-05-15 18:49:57 +04:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
// Use this to hold a pointer to the instruction since
|
|
|
|
// we can't pass this to the FPU emulation routines, which
|
|
|
|
// will ultimately call routines here.
|
2002-09-18 02:50:53 +04:00
|
|
|
static bxInstruction_c *fpu_iptr = NULL;
|
2001-05-23 12:16:07 +04:00
|
|
|
static BX_CPU_C *fpu_cpu_ptr = NULL;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2002-09-09 20:11:25 +04:00
|
|
|
i387_t *current_i387;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
extern "C" void
|
2003-04-23 00:21:34 +04:00
|
|
|
math_emulate(fpu_addr_modes addr_modes,
|
2001-04-10 05:04:59 +04:00
|
|
|
u_char FPU_modrm,
|
|
|
|
u_char byte1,
|
2003-07-25 12:59:46 +04:00
|
|
|
bx_address data_address,
|
2001-04-10 05:04:59 +04:00
|
|
|
struct address data_sel_off,
|
|
|
|
struct address entry_sel_off);
|
|
|
|
|
|
|
|
|
|
|
|
// This is called by bochs upon reset
|
|
|
|
void
|
|
|
|
BX_CPU_C::fpu_init(void)
|
|
|
|
{
|
2002-09-09 20:11:25 +04:00
|
|
|
current_i387 = &(BX_CPU_THIS_PTR the_i387);
|
2001-04-10 05:04:59 +04:00
|
|
|
finit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-09-18 02:50:53 +04:00
|
|
|
BX_CPU_C::fpu_execute(bxInstruction_c *i)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
fpu_addr_modes addr_modes;
|
2003-07-25 12:59:46 +04:00
|
|
|
bx_address data_address;
|
2001-04-10 05:04:59 +04:00
|
|
|
struct address data_sel_off;
|
|
|
|
struct address entry_sel_off;
|
2002-10-25 15:44:41 +04:00
|
|
|
bx_bool is_32;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
fpu_iptr = i;
|
2001-05-23 12:16:07 +04:00
|
|
|
fpu_cpu_ptr = this;
|
2002-09-09 20:11:25 +04:00
|
|
|
current_i387 = &(BX_CPU_THIS_PTR the_i387);
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2003-01-03 03:20:05 +03:00
|
|
|
is_32 = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b;
|
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
if (protected_mode()) {
|
2003-01-03 03:20:05 +03:00
|
|
|
if (is_32) addr_modes.default_mode = SEG32;
|
|
|
|
else addr_modes.default_mode = PM16;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
else if (v8086_mode()) {
|
|
|
|
addr_modes.default_mode = VM86;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// real mode, use vm86 for now
|
|
|
|
addr_modes.default_mode = VM86;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark if instruction used opsize or addrsize prefixes
|
2002-09-18 09:36:48 +04:00
|
|
|
if (i->as32B() == is_32)
|
2001-04-10 05:04:59 +04:00
|
|
|
addr_modes.override.address_size = 0;
|
|
|
|
else
|
|
|
|
addr_modes.override.address_size = ADDR_SIZE_PREFIX;
|
2003-08-01 01:07:38 +04:00
|
|
|
|
2002-09-18 09:36:48 +04:00
|
|
|
if (i->os32B() == is_32)
|
2001-04-10 05:04:59 +04:00
|
|
|
addr_modes.override.operand_size = 0;
|
|
|
|
else
|
|
|
|
addr_modes.override.operand_size = OP_SIZE_PREFIX;
|
|
|
|
|
|
|
|
// fill in orig eip here in offset
|
|
|
|
// fill in CS in selector
|
|
|
|
entry_sel_off.offset = BX_CPU_THIS_PTR prev_eip;
|
2001-05-23 12:16:07 +04:00
|
|
|
entry_sel_off.selector = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
// should set these fields to 0 if mem operand not used
|
2003-07-25 12:59:46 +04:00
|
|
|
data_address = RMAddr(i);
|
2002-09-18 09:36:48 +04:00
|
|
|
data_sel_off.offset = RMAddr(i);
|
|
|
|
data_sel_off.selector = BX_CPU_THIS_PTR sregs[i->seg()].selector.value;
|
2001-04-10 05:04:59 +04:00
|
|
|
|
2003-04-23 00:21:34 +04:00
|
|
|
math_emulate(addr_modes, i->modrm(), i->b1(), data_address,
|
2001-04-10 05:04:59 +04:00
|
|
|
data_sel_off, entry_sel_off);
|
|
|
|
}
|
|
|
|
|
2003-08-04 17:36:15 +04:00
|
|
|
void BX_CPU_C::print_state_FPU()
|
2001-09-15 10:55:14 +04:00
|
|
|
{
|
2003-11-01 21:36:19 +03:00
|
|
|
static double sigh_scale_factor = pow(2.0, -31.0);
|
|
|
|
static double sigl_scale_factor = pow(2.0, -63.0);
|
|
|
|
|
2001-09-15 10:55:14 +04:00
|
|
|
Bit32u reg;
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.cwd;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "cwd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.swd;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "swd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.twd;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "twd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.fip;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "fip 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.fcs;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "fcs 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.foo;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "foo 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
2003-04-13 01:02:08 +04:00
|
|
|
reg = i387.fos;
|
2001-09-15 10:55:14 +04:00
|
|
|
fprintf(stderr, "fos 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
|
|
|
|
// print stack too
|
|
|
|
for (int i=0; i<8; i++) {
|
|
|
|
FPU_REG *fpr = &st(i);
|
|
|
|
double f1 = pow(2.0, ((0x7fff&fpr->exp) - EXTENDED_Ebias));
|
|
|
|
if (fpr->exp & SIGN_Negative) f1 = -f1;
|
|
|
|
double f2 = ((double)fpr->sigh * sigh_scale_factor);
|
|
|
|
double f3 = ((double)fpr->sigl * sigl_scale_factor);
|
|
|
|
double f = f1*(f2+f3);
|
|
|
|
fprintf(stderr, "st%d %.10f (raw 0x%04x%08x%08x)\n", i, f, 0xffff&fpr->exp, fpr->sigh, fpr->sigl);
|
|
|
|
}
|
|
|
|
}
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
unsigned
|
|
|
|
fpu_get_ds(void)
|
|
|
|
{
|
2001-05-23 12:16:07 +04:00
|
|
|
return(fpu_cpu_ptr->sregs[BX_SEG_REG_DS].selector.value);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-11-01 21:36:19 +03:00
|
|
|
fpu_set_ax(Bit16u val16)
|
|
|
|
{
|
|
|
|
fpu_cpu_ptr->set_AX(val16);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fpu_set_eflags(Bit32u val32)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
2003-11-01 21:36:19 +03:00
|
|
|
fpu_cpu_ptr->writeEFlags(val32, 0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit32u
|
|
|
|
fpu_get_eflags(void)
|
|
|
|
{
|
|
|
|
return fpu_cpu_ptr->read_eflags();
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
|
2003-03-03 02:59:12 +03:00
|
|
|
void BX_CPP_AttrRegparmN(3)
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_verify_area(unsigned what, bx_address ptr, unsigned n)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
bx_segment_reg_t *seg;
|
|
|
|
|
2002-09-18 09:36:48 +04:00
|
|
|
seg = &fpu_cpu_ptr->sregs[fpu_iptr->seg()];
|
2001-04-10 05:04:59 +04:00
|
|
|
|
|
|
|
if (what == VERIFY_READ) {
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->read_virtual_checks(seg, ptr, n);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
else { // VERIFY_WRITE
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->write_virtual_checks(seg, ptr, n);
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-01 21:36:19 +03:00
|
|
|
Bit32u BX_CPP_AttrRegparmN(2)
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_get_user(bx_address ptr, unsigned len)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
Bit32u val32;
|
|
|
|
Bit16u val16;
|
|
|
|
Bit8u val8;
|
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->read_virtual_byte(fpu_iptr->seg(), ptr, &val8);
|
2001-04-10 05:04:59 +04:00
|
|
|
val32 = val8;
|
|
|
|
break;
|
|
|
|
case 2:
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->read_virtual_word(fpu_iptr->seg(), ptr, &val16);
|
2001-04-10 05:04:59 +04:00
|
|
|
val32 = val16;
|
|
|
|
break;
|
|
|
|
case 4:
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->read_virtual_dword(fpu_iptr->seg(), ptr, &val32);
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("fpu_get_user: len=%u", len));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
return(val32);
|
|
|
|
}
|
|
|
|
|
2003-10-04 16:52:29 +04:00
|
|
|
void BX_CPP_AttrRegparmN(3)
|
2003-11-01 21:36:19 +03:00
|
|
|
fpu_put_user(Bit32u val, bx_address ptr, unsigned len)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
Bit32u val32;
|
|
|
|
Bit16u val16;
|
|
|
|
Bit8u val8;
|
|
|
|
|
|
|
|
switch (len) {
|
|
|
|
case 1:
|
|
|
|
val8 = val;
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->write_virtual_byte(fpu_iptr->seg(), ptr, &val8);
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
val16 = val;
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->write_virtual_word(fpu_iptr->seg(), ptr, &val16);
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
val32 = val;
|
2003-07-25 12:59:46 +04:00
|
|
|
fpu_cpu_ptr->write_virtual_dword(fpu_iptr->seg(), ptr, &val32);
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC(("fpu_put_user: len=%u", len));
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-07-31 22:54:48 +04:00
|
|
|
math_abort(void *info, unsigned int signal)
|
2001-04-10 05:04:59 +04:00
|
|
|
{
|
|
|
|
UNUSED(info); // info is always passed NULL
|
|
|
|
#if BX_CPU_LEVEL >= 4
|
|
|
|
|
|
|
|
// values of signal:
|
|
|
|
// SIGILL : opcodes which are illegal
|
|
|
|
// SIGFPE : unmasked FP exception before WAIT or non-control instruction
|
|
|
|
// SIGSEGV : access data beyond segment violation
|
|
|
|
switch (signal) {
|
|
|
|
case SIGFPE:
|
2001-05-23 12:16:07 +04:00
|
|
|
if (fpu_cpu_ptr->cr0.ne == 0) {
|
2001-04-10 05:04:59 +04:00
|
|
|
// MSDOS compatibility external interrupt (IRQ13)
|
2003-01-06 05:20:47 +03:00
|
|
|
BX_INFO (("math_abort: MSDOS compatibility FPU exception"));
|
|
|
|
|
|
|
|
DEV_pic_raise_irq(13);
|
|
|
|
return;
|
2001-04-10 05:04:59 +04:00
|
|
|
}
|
2001-05-23 12:16:07 +04:00
|
|
|
fpu_cpu_ptr->exception(BX_MF_EXCEPTION, 0, 0);
|
2001-04-10 05:04:59 +04:00
|
|
|
// execution does not reach here
|
|
|
|
|
|
|
|
case SIGILL:
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC (("math_abort: SIGILL not implemented yet."));
|
2003-08-01 20:58:00 +04:00
|
|
|
fpu_cpu_ptr->UndefinedOpcode(fpu_iptr);
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
2003-08-01 20:58:00 +04:00
|
|
|
|
2001-04-10 05:04:59 +04:00
|
|
|
case SIGSEGV:
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_PANIC (("math_abort: SIGSEGV not implemented yet."));
|
2001-04-10 05:04:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
UNUSED(signal);
|
2001-05-30 22:56:02 +04:00
|
|
|
BX_INFO(("math_abort: CPU<4 not supported yet"));
|
2001-04-10 05:04:59 +04:00
|
|
|
#endif
|
|
|
|
}
|
2003-11-01 21:36:19 +03:00
|
|
|
|
|
|
|
extern "C" int printk(const char * fmt, ...)
|
|
|
|
{
|
|
|
|
BX_INFO(("math abort: %s", fmt));
|
2003-11-25 22:34:34 +03:00
|
|
|
return 0;
|
2003-11-01 21:36:19 +03:00
|
|
|
}
|