Bochs/bochs/fpu/wmFPUemu_glue.cc

296 lines
7.8 KiB
C++
Raw Normal View History

// Copyright (C) 2001 MandrakeSoft S.A.
//
// 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"
#include <math.h>
extern "C" {
#include "fpu_emu.h"
#include "linux/signal.h"
}
#define LOG_THIS genlog->
#if BX_USE_CPU_SMF
#define this (BX_CPU(0))
#endif
// 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.
static bxInstruction_c *fpu_iptr = NULL;
static BX_CPU_C *fpu_cpu_ptr = NULL;
i387_t *current_i387;
extern "C" void
math_emulate2(fpu_addr_modes addr_modes,
u_char FPU_modrm,
u_char byte1,
void *data_address,
struct address data_sel_off,
struct address entry_sel_off);
extern "C" void printfp(char *s, FPU_REG *r);
// This is called by bochs upon reset
void
BX_CPU_C::fpu_init(void)
{
current_i387 = &(BX_CPU_THIS_PTR the_i387);
finit();
}
void
BX_CPU_C::fpu_execute(bxInstruction_c *i)
{
fpu_addr_modes addr_modes;
void *data_address;
struct address data_sel_off;
struct address entry_sel_off;
- Apply patch.replace-Boolean rev 1.3. Every "Boolean" is now changed to a "bx_bool" which is always defined as Bit32u on all platforms. In Carbon specific code, Boolean is still used because the Carbon header files define it to unsigned char. - this fixes bug [ 623152 ] MacOSX: Triple Exception Booting win95. The bug was that some code in Bochs depends on Boolean to be a 32 bit value. (This should be fixed, but I don't know all the places where it needs to be fixed yet.) Because Carbon defined Boolean as an unsigned char, Bochs just followed along and used the unsigned char definition to avoid compile problems. This exposed the dependency on 32 bit Boolean on MacOS X only and led to major simulation problems, that could only be reproduced and debugged on that platform. - On the mailing list we debated whether to make all Booleans into "bool" or our own type. I chose bx_bool for several reasons. 1. Unlike C++'s bool, we can guarantee that bx_bool is the same size on all platforms, which makes it much less likely to have more platform-specific simulation differences in the future. (I spent hours on a borrowed MacOSX machine chasing bug 618388 before discovering that different sized Booleans were the problem, and I don't want to repeat that.) 2. We still have at least one dependency on 32 bit Booleans which must be fixed some time, but I don't want to risk introducing new bugs into the simulation just before the 2.0 release. Modified Files: bochs.h config.h.in gdbstub.cc logio.cc main.cc pc_system.cc pc_system.h plugin.cc plugin.h bios/rombios.c cpu/apic.cc cpu/arith16.cc cpu/arith32.cc cpu/arith64.cc cpu/arith8.cc cpu/cpu.cc cpu/cpu.h cpu/ctrl_xfer16.cc cpu/ctrl_xfer32.cc cpu/ctrl_xfer64.cc cpu/data_xfer16.cc cpu/data_xfer32.cc cpu/data_xfer64.cc cpu/debugstuff.cc cpu/exception.cc cpu/fetchdecode.cc cpu/flag_ctrl_pro.cc cpu/init.cc cpu/io_pro.cc cpu/lazy_flags.cc cpu/lazy_flags.h cpu/mult16.cc cpu/mult32.cc cpu/mult64.cc cpu/mult8.cc cpu/paging.cc cpu/proc_ctrl.cc cpu/segment_ctrl_pro.cc cpu/stack_pro.cc cpu/tasking.cc debug/dbg_main.cc debug/debug.h debug/sim2.cc disasm/dis_decode.cc disasm/disasm.h doc/docbook/Makefile docs-html/cosimulation.html fpu/wmFPUemu_glue.cc gui/amigaos.cc gui/beos.cc gui/carbon.cc gui/gui.cc gui/gui.h gui/keymap.cc gui/keymap.h gui/macintosh.cc gui/nogui.cc gui/rfb.cc gui/sdl.cc gui/siminterface.cc gui/siminterface.h gui/term.cc gui/win32.cc gui/wx.cc gui/wxmain.cc gui/wxmain.h gui/x.cc instrument/example0/instrument.cc instrument/example0/instrument.h instrument/example1/instrument.cc instrument/example1/instrument.h instrument/stubs/instrument.cc instrument/stubs/instrument.h iodev/cdrom.cc iodev/cdrom.h iodev/cdrom_osx.cc iodev/cmos.cc iodev/devices.cc iodev/dma.cc iodev/dma.h iodev/eth_arpback.cc iodev/eth_packetmaker.cc iodev/eth_packetmaker.h iodev/floppy.cc iodev/floppy.h iodev/guest2host.h iodev/harddrv.cc iodev/harddrv.h iodev/ioapic.cc iodev/ioapic.h iodev/iodebug.cc iodev/iodev.h iodev/keyboard.cc iodev/keyboard.h iodev/ne2k.h iodev/parallel.h iodev/pci.cc iodev/pci.h iodev/pic.h iodev/pit.cc iodev/pit.h iodev/pit_wrap.cc iodev/pit_wrap.h iodev/sb16.cc iodev/sb16.h iodev/serial.cc iodev/serial.h iodev/vga.cc iodev/vga.h memory/memory.h memory/misc_mem.cc
2002-10-25 15:44:41 +04:00
bx_bool is_32;
fpu_iptr = i;
fpu_cpu_ptr = this;
current_i387 = &(BX_CPU_THIS_PTR the_i387);
#if 0
addr_modes.default_mode = VM86;
addr_modes.default_mode = 0; // FPU_CS == __USER_CS && FPU_DS == __USER_DS
addr_modes.default_mode = SEG32;
addr_modes.default_mode = PM16;
#endif
if (protected_mode()) {
addr_modes.default_mode = SEG32;
}
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
// Actually, addr_modes.override.address_size is not used,
// could delete that code.
is_32 = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].cache.u.segment.d_b;
if (i->as32B() == is_32)
addr_modes.override.address_size = 0;
else
addr_modes.override.address_size = ADDR_SIZE_PREFIX;
if (i->os32B() == is_32)
addr_modes.override.operand_size = 0;
else
addr_modes.override.operand_size = OP_SIZE_PREFIX;
// For now set access_limit to max. It seems to be
// a number from 0..255 denoting how many bytes the
// current instruction can access according to its
// memory operand. 255 means >= 255.
access_limit = 0xff;
// fill in orig eip here in offset
// fill in CS in selector
entry_sel_off.offset = BX_CPU_THIS_PTR prev_eip;
entry_sel_off.selector = BX_CPU_THIS_PTR sregs[BX_SEG_REG_CS].selector.value;
// should set these fields to 0 if mem operand not used
data_address = (void *) RMAddr(i);
data_sel_off.offset = RMAddr(i);
data_sel_off.selector = BX_CPU_THIS_PTR sregs[i->seg()].selector.value;
math_emulate2(addr_modes, i->modrm(), i->b1(), data_address,
data_sel_off, entry_sel_off);
}
static double sigh_scale_factor = pow(2.0, -31.0);
static double sigl_scale_factor = pow(2.0, -63.0);
void
BX_CPU_C::fpu_print_regs()
{
Bit32u reg;
reg = i387.soft.cwd;
fprintf(stderr, "cwd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.swd;
fprintf(stderr, "swd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.twd;
fprintf(stderr, "twd 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.fip;
fprintf(stderr, "fip 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.fcs;
fprintf(stderr, "fcs 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.foo;
fprintf(stderr, "foo 0x%-8x\t%d\n", (unsigned) reg, (int) reg);
reg = i387.soft.fos;
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);
}
}
unsigned
fpu_get_ds(void)
{
return(fpu_cpu_ptr->sregs[BX_SEG_REG_DS].selector.value);
}
void
fpu_set_ax(unsigned short val16)
{
// define to set AX in the current CPU -- not ideal.
2001-05-23 12:44:59 +04:00
#undef AX
#define AX (fpu_cpu_ptr->gen_reg[0].word.rx)
AX = val16;
#undef AX
//BX_DEBUG(( "fpu_set_ax(0x%04x)", (unsigned) val16));
}
void
fpu_verify_area(unsigned what, void *ptr, unsigned n)
{
bx_segment_reg_t *seg;
seg = &fpu_cpu_ptr->sregs[fpu_iptr->seg()];
if (what == VERIFY_READ) {
fpu_cpu_ptr->read_virtual_checks(seg, PTR2INT(ptr), n);
}
else { // VERIFY_WRITE
fpu_cpu_ptr->write_virtual_checks(seg, PTR2INT(ptr), n);
}
//BX_DEBUG(( "verify_area: 0x%x", PTR2INT(ptr)));
}
void
FPU_printall(void)
{
BX_PANIC(("FPU_printall"));
}
unsigned
fpu_get_user(void *ptr, unsigned len)
{
Bit32u val32;
Bit16u val16;
Bit8u val8;
switch (len) {
case 1:
fpu_cpu_ptr->read_virtual_byte(fpu_iptr->seg(), PTR2INT(ptr), &val8);
val32 = val8;
break;
case 2:
fpu_cpu_ptr->read_virtual_word(fpu_iptr->seg(), PTR2INT(ptr), &val16);
val32 = val16;
break;
case 4:
fpu_cpu_ptr->read_virtual_dword(fpu_iptr->seg(), PTR2INT(ptr), &val32);
break;
default:
BX_PANIC(("fpu_get_user: len=%u", len));
}
return(val32);
}
void
fpu_put_user(unsigned val, void *ptr, unsigned len)
{
Bit32u val32;
Bit16u val16;
Bit8u val8;
switch (len) {
case 1:
val8 = val;
fpu_cpu_ptr->write_virtual_byte(fpu_iptr->seg(), PTR2INT(ptr), &val8);
break;
case 2:
val16 = val;
fpu_cpu_ptr->write_virtual_word(fpu_iptr->seg(), PTR2INT(ptr), &val16);
break;
case 4:
val32 = val;
fpu_cpu_ptr->write_virtual_dword(fpu_iptr->seg(), PTR2INT(ptr), &val32);
break;
default:
BX_PANIC(("fpu_put_user: len=%u", len));
}
}
void
math_abort(struct info *info, unsigned int signal)
{
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:
if (fpu_cpu_ptr->cr0.ne == 0) {
// MSDOS compatibility external interrupt (IRQ13)
BX_PANIC (("math_abort: MSDOS compatibility not supported yet"));
}
fpu_cpu_ptr->exception(BX_MF_EXCEPTION, 0, 0);
// execution does not reach here
case SIGILL:
BX_PANIC (("math_abort: SIGILL not implemented yet."));
break;
case SIGSEGV:
BX_PANIC (("math_abort: SIGSEGV not implemented yet."));
break;
}
#else
UNUSED(signal);
BX_INFO(("math_abort: CPU<4 not supported yet"));
#endif
}
int
printk(const char * fmt, ...)
{
BX_INFO(("printk not complete: %s", fmt));
return(0); // for now
}