Skip to content

Commit

Permalink
FPU: use [backend_]{get,set}_fpu_state.
Browse files Browse the repository at this point in the history
Instead of enter/leave, for clarity.
  • Loading branch information
bartoldeman committed Jan 25, 2023
1 parent a7701fa commit 7d4a194
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 102 deletions.
53 changes: 41 additions & 12 deletions src/base/emu-i386/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,29 +238,58 @@ static void fpu_init(void)
}
#endif

static void fpu_reset(void)
void get_fpu_state(emu_fpstate *fpstate)
{
emu_fpstate vm86_fpu_state;
int cpu_vm;

memset(&vm86_fpu_state, 0, sizeof vm86_fpu_state);
vm86_fpu_state.cwd = 0x0040; //bochs
vm86_fpu_state.ftw = 0xff; //all valid (-> 0x5555 in fsave tag)
cpu_vm = in_dpmi_pm() ? config.cpu_vm_dpmi : config.cpu_vm;
int cpu_vm = in_dpmi_pm() ? config.cpu_vm_dpmi : config.cpu_vm;
switch(cpu_vm) {
case CPUVM_KVM:
kvm_update_fpu(&vm86_fpu_state);
kvm_get_fpu_state(fpstate);
break;
case CPUVM_NATIVE:
native_dpmi_get_fpu_state(fpstate);
break;
case CPUVM_EMU:
e_update_fpu(&vm86_fpu_state);
e_get_fpu_state(fpstate);
break;
#ifdef __i386__
case CPUVM_VM86:
true_vm86_get_fpu_state(fpstate);
break;
#endif
}
}

void set_fpu_state(const emu_fpstate *fpstate)
{
int cpu_vm = in_dpmi_pm() ? config.cpu_vm_dpmi : config.cpu_vm;
switch(cpu_vm) {
case CPUVM_KVM:
kvm_set_fpu_state(fpstate);
break;
case CPUVM_NATIVE:
native_dpmi_update_fpu(&vm86_fpu_state);
native_dpmi_set_fpu_state(fpstate);
break;
case CPUVM_EMU:
e_set_fpu_state(fpstate);
break;
#ifdef __i386__
case CPUVM_VM86:
true_vm86_update_fpu(&vm86_fpu_state);
true_vm86_set_fpu_state(fpstate);
break;
#endif
}
}

static void fpu_reset(void)
{
emu_fpstate vm86_fpu_state;

memset(&vm86_fpu_state, 0, sizeof vm86_fpu_state);
vm86_fpu_state.cwd = 0x0040; //bochs
vm86_fpu_state.ftw = 0xff; //all valid (-> 0x5555 in fsave tag)
set_fpu_state(&vm86_fpu_state);
}

static Bit8u fpu_io_read(ioport_t port)
{
return 0xff;
Expand Down
9 changes: 2 additions & 7 deletions src/base/emu-i386/do_vm86.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,20 +485,15 @@ static int true_vm86(union vm86_union *x)
return ret;
}

void true_vm86_update_fpu(const emu_fpstate *fpstate)
void true_vm86_set_fpu_state(const emu_fpstate *fpstate)
{
if (config.cpufxsr)
true_vm86_fxsave = *fpstate;
else
fxsave_to_fsave(fpstate, &true_vm86_fsave);
}

void true_vm86_enter(const emu_fpstate *fpstate)
{
true_vm86_update_fpu(fpstate);
}

void true_vm86_leave(emu_fpstate *fpstate)
void true_vm86_get_fpu_state(emu_fpstate *fpstate)
{
if (config.cpufxsr)
*fpstate = true_vm86_fxsave;
Expand Down
9 changes: 2 additions & 7 deletions src/base/emu-i386/kvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ static void set_ldt_seg(struct kvm_segment *seg, unsigned selector)
seg->unusable = !desc->present;
}

void kvm_update_fpu(const emu_fpstate *fpstate)
void kvm_set_fpu_state(const emu_fpstate *fpstate)
{
struct kvm_xsave fpu = {};
int ret;
Expand All @@ -796,12 +796,7 @@ void kvm_update_fpu(const emu_fpstate *fpstate)
}
}

void kvm_enter(int pm, const emu_fpstate *fpstate)
{
kvm_update_fpu(fpstate);
}

void kvm_leave(int pm, emu_fpstate *fpstate)
void kvm_get_fpu_state(emu_fpstate *fpstate)
{
struct kvm_xsave fpu;
int ret = ioctl(vcpufd, KVM_GET_XSAVE, &fpu);
Expand Down
9 changes: 2 additions & 7 deletions src/base/emu-i386/simx86/cpu-emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void Cpu2Reg (void)
REG(eflags),get_FLAGS(TheCPU.eflags),TheCPU.eflags);
}

void e_update_fpu(const emu_fpstate *fpstate)
void e_set_fpu_state(const emu_fpstate *fpstate)
{
if (CONFIG_CPUSIM)
fp87_load_fpstate(fpstate);
Expand All @@ -593,12 +593,7 @@ void e_update_fpu(const emu_fpstate *fpstate)
}
}

void e_enter(const emu_fpstate *fpstate)
{
e_update_fpu(fpstate);
}

void e_leave(emu_fpstate *fpstate)
void e_get_fpu_state(emu_fpstate *fpstate)
{
if (CONFIG_CPUSIM)
fp87_save_fpstate(fpstate);
Expand Down
9 changes: 2 additions & 7 deletions src/dosext/dpmi/dnative/dnative.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static void copy_to_dpmi(sigcontext_t *scp, cpuctx_t *s)
scp_fpregs = NULL;
}

void native_dpmi_update_fpu(const emu_fpstate *fpstate)
void native_dpmi_set_fpu_state(const emu_fpstate *fpstate)
{
if (scp_fpregs) {
void *fpregs = scp_fpregs;
Expand All @@ -127,11 +127,6 @@ void native_dpmi_update_fpu(const emu_fpstate *fpstate)
}
}

void native_dpmi_enter_from_vm86(const emu_fpstate *fpstate)
{
native_dpmi_update_fpu(fpstate);
}

static void copy_to_emu(cpuctx_t *d, sigcontext_t *scp)
{
#define _D(x) get_##x(d) = _scp_##x
Expand All @@ -157,7 +152,7 @@ static void copy_to_emu(cpuctx_t *d, sigcontext_t *scp)
scp_fpregs = scp->fpregs;
}

void native_dpmi_leave_to_vm86(emu_fpstate *fpstate)
void native_dpmi_get_fpu_state(emu_fpstate *fpstate)
{
if (scp_fpregs) {
void *fpregs = scp_fpregs;
Expand Down
13 changes: 4 additions & 9 deletions src/dosext/dpmi/dnative/dnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ int native_dpmi_control(cpuctx_t *scp);
int native_dpmi_exit(cpuctx_t *scp);
void native_dpmi_enter(void);
void native_dpmi_leave(void);
void native_dpmi_enter_from_vm86(const emu_fpstate *fpstate);
void native_dpmi_leave_to_vm86(emu_fpstate *fpstate);
void native_dpmi_update_fpu(const emu_fpstate *fpstate);
void native_dpmi_get_fpu_state(emu_fpstate *fpstate);
void native_dpmi_set_fpu_state(const emu_fpstate *fpstate);
void dpmi_return(sigcontext_t *scp, int retcode);

#else
Expand Down Expand Up @@ -44,15 +43,11 @@ static inline void native_dpmi_leave(void)
{
}

static inline void native_dpmi_enter_from_vm86(const emu_fpstate *fpstate)
static inline void native_dpmi_set_fpu_state(const emu_fpstate *fpstate)
{
}

static inline void native_dpmi_leave_to_vm86(emu_fpstate *fpstate)
{
}

static inline void native_dpmi_update_fpu(const emu_fpstate *fpstate)
static inline void native_dpmi_get_fpu_state(emu_fpstate *fpstate)
{
}

Expand Down
50 changes: 6 additions & 44 deletions src/dosext/dpmi/dpmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,60 +418,22 @@ static void print_ldt(void)
_print_dt(buffer, MAX_SELECTORS, 1);
}

static void leave_backend(int be, int pm, emu_fpstate *fpstate)
{
switch(be) {
case CPUVM_KVM:
kvm_leave(pm, fpstate);
break;
case CPUVM_NATIVE:
native_dpmi_leave_to_vm86(fpstate);
break;
case CPUVM_EMU:
e_leave(fpstate);
break;
#ifdef __i386__
case CPUVM_VM86:
true_vm86_leave(fpstate);
break;
#endif
}
}

static void enter_backend(int be, int pm, const emu_fpstate *fpstate)
{
switch(be) {
case CPUVM_KVM:
kvm_enter(pm, fpstate);
break;
case CPUVM_NATIVE:
native_dpmi_enter_from_vm86(fpstate);
break;
case CPUVM_EMU:
e_enter(fpstate);
break;
#ifdef __i386__
case CPUVM_VM86:
true_vm86_enter(fpstate);
break;
#endif
}
}

static void dpmi_set_pm(int pm)
{
static emu_fpstate fpstate;
assert(pm <= 1);
if (pm == dpmi_pm) {
if (!pm)
dosemu_error("DPMI: switch from real to real mode?\n");
return;
}
dpmi_pm = pm;
if (config.cpu_vm != config.cpu_vm_dpmi) {
static emu_fpstate fpstate;
leave_backend(pm ? config.cpu_vm : config.cpu_vm_dpmi, !pm, &fpstate);
enter_backend(!pm ? config.cpu_vm : config.cpu_vm_dpmi, pm, &fpstate);
get_fpu_state(&fpstate);
dpmi_pm = pm;
set_fpu_state(&fpstate);
}
else
dpmi_pm = pm;
}

static dpmi_pm_block *lookup_pm_blocks_by_addr(dosaddr_t addr)
Expand Down
5 changes: 2 additions & 3 deletions src/include/cpu-emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ void reset_emu_cpu (void);
int e_dpmi(cpuctx_t *scp);
void e_dpmi_b0x(int op,cpuctx_t *scp);
extern int in_dpmi_emu;
void e_enter(const emu_fpstate *fpstate);
void e_leave(emu_fpstate *fpstate);
void e_update_fpu(const emu_fpstate *fpstate);
void e_get_fpu_state(emu_fpstate *fpstate);
void e_set_fpu_state(const emu_fpstate *fpstate);

/* called from emu-ldt.c */
void InvalidateSegs(void);
Expand Down
3 changes: 3 additions & 0 deletions src/include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ void fsave_to_fxsave(const struct emu_fsave *fptr,
typedef struct emu_fpxstate emu_fpstate;
typedef emu_fpstate *emu_fpregset_t;

void get_fpu_state(emu_fpstate *);
void set_fpu_state(const emu_fpstate *);

union g_reg {
greg_t reg;
#ifdef __x86_64__
Expand Down
5 changes: 2 additions & 3 deletions src/include/emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ extern FILE *real_stderr;
void dos_ctrl_alt_del(void); /* disabled */

extern void vm86_helper(void);
extern void true_vm86_enter(const emu_fpstate *fpstate);
extern void true_vm86_leave(emu_fpstate *fpstate);
extern void true_vm86_update_fpu(const emu_fpstate *fpstate);
extern void true_vm86_get_fpu_state(emu_fpstate *fpstate);
extern void true_vm86_set_fpu_state(const emu_fpstate *fpstate);
extern void run_vm86(void);
extern void loopstep_run_vm86(void);
extern int do_call_back(Bit16u cs, Bit16u ip);
Expand Down
5 changes: 2 additions & 3 deletions src/include/kvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ void set_kvm_memory_regions(void);
void kvm_set_idt_default(int i);
void kvm_set_idt(int i, uint16_t sel, uint32_t offs, int is_32, int tg);

void kvm_enter(int pm, const emu_fpstate *fpstate);
void kvm_leave(int pm, emu_fpstate *fpstate);
void kvm_update_fpu(const emu_fpstate *fpstate);
void kvm_get_fpu_state(emu_fpstate *fpstate);
void kvm_set_fpu_state(const emu_fpstate *fpstate);

void kvm_done(void);

Expand Down

0 comments on commit 7d4a194

Please sign in to comment.