Files
Xenia-Canary/src/xenia/kernel/xboxkrnl/xboxkrnl_misc.cc
chss95cs@gmail.com 324a8eb818 A bunch of fixes for division logic:
"turns out theres a lot of quirks with the div instructions we havent been covering
if the denom is 0, we jump to the end and mov eax/rax to dst, which is correct because ppc raises no exceptions for divide by 0 unlike x86
except we don't initialize eax before that jump, so whatever garbage from the previous sequence that has been left in eax/rax is what the result of the instruction will be
and then in our constant folding, we don't do the same zero check in Value::Div, so if we constant folded the denom to 0 we will host crash
the ppc manual says the result for a division by 0 is undefined, but in reality it seems it is always 0
there are a few posts i saw from googling about it, and tests on my rgh gave me 0, but then another issue came up
and that is that we dont check for signed overflow in our division, so we raise an exception if guest code ever does (1<<signbit_pos) / -1
signed overflow in division also produces 0 on ppc
the last thing is that if src2 is constant we skip the 0 check for division
without checking if its nonzero
all weird, likely very rare edge cases, except for maybe the signed overflow division
chrispy — Today at 9:51 AM
oh yeah, and because the int members of constantvalue are all signed ints, we were actually doing signed division always with constant folding"

fixed an earlier mistake by me with the precision of fresx
made some optimization disableable

implemented vkpkx
fixed possible bugs with vsr/vsl constant folding
disabled the nice imul code for now, there was a bug with int64 version and i dont have time to check
started on multiplication/addition/subtraction/division identities
Removed optimized VSL implementation, it's going to have to be rewritten anyway
Added ppc_ctx_t to xboxkrnl shim for direct context access
started working on KeSaveFloatingPointState, re'ed most of it
Exposed some more state/functionality to the kernel for implementing lower level routines like the save/restore ones
Add cvar to re-enable incorrect mxcsr behavior if a user doesnt care and wants better cpu performance
Stubbed out more impossible sequences, replace mul_hi_i32 with a 64 bit multiply
2022-08-07 10:41:26 -07:00

120 lines
3.3 KiB
C++

/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2022 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/logging.h"
#include "xenia/cpu/ppc/ppc_frontend.h"
#include "xenia/cpu/processor.h"
#include "xenia/kernel/kernel_state.h"
#include "xenia/kernel/util/shim_utils.h"
#include "xenia/kernel/xboxkrnl/xboxkrnl_private.h"
#include "xenia/kernel/xthread.h"
#include "xenia/xbox.h"
namespace xe {
namespace kernel {
namespace xboxkrnl {
void KeEnableFpuExceptions_entry(dword_t enabled) {
// TODO(benvanik): can we do anything about exceptions?
}
DECLARE_XBOXKRNL_EXPORT1(KeEnableFpuExceptions, kNone, kStub);
#if 0
struct __declspec(align(8)) fpucontext_ptr_t {
char unknown_data[158];
__int16 field_9E;
char field_A0[2272];
unsigned __int64 saved_FPSCR;
double saved_fpu_regs[32];
};
#pragma pack(push, 1)
struct __declspec(align(1)) r13_struct_t {
char field_0[6];
__int16 field_6;
char field_8[2];
char field_A;
char field_B[5];
int field_10;
char field_14[315];
char field_14F;
unsigned int field_150;
char field_154[427];
char field_2FF;
char field_300;
};
#pragma pack(pop)
static uint64_t Do_mfmsr(ppc_context_t& ctx) {
auto frontend = ctx->thread_state->processor()->frontend();
cpu::ppc::CheckGlobalLock(
ctx, reinterpret_cast<void*>(&xe::global_critical_region::mutex()),
reinterpret_cast<void*>(&frontend->builtins()->global_lock_count));
return ctx->scratch;
}
void KeSaveFloatingPointState_entry(ppc_context_t& ctx) {
xe::Memory* memory = ctx->thread_state->memory();
unsigned int r13 = static_cast<unsigned int>(ctx->r[13]);
r13_struct_t* st = memory->TranslateVirtual<r13_struct_t*>(r13);
/*
lwz r10, 0x150(r13)
lbz r11, 0xA(r13)
tweqi r10, 0
twnei r11, 0
*/
unsigned int r10 = st->field_150;
unsigned char r11 = st->field_A;
if (r10 == 0 || r11 != 0) {
//trap!
}
//should do mfmsr here
unsigned int r3 = xe::load_and_swap<unsigned int>(&st->field_10);
//too much work to do the mfmsr/mtmsr stuff right now
int to_store = -2049;
xe::store_and_swap(&st->field_10, (unsigned int)to_store);
xe::store_and_swap(&st->field_6, (short)to_store);
if (r3 != ~0u) {
fpucontext_ptr_t* fpucontext =
memory->TranslateVirtual<fpucontext_ptr_t*>(r3);
xe::store_and_swap<uint64_t>(&fpucontext->saved_FPSCR, ctx->fpscr.value);
for (unsigned int i = 0; i < 32; ++i) {
xe::store_and_swap(&fpucontext->saved_fpu_regs[i], ctx->f[i]);
}
xe::store_and_swap<unsigned short>(&fpucontext->field_9E, 0xD7FF);
}
ctx->processor->backend()->SetGuestRoundingMode(ctx.value(), 0);
ctx->fpscr.value = 0;
st->field_A = 1;
xe::store_and_swap(&st->field_10, r13 + 0x300);
ctx->r[3] = r3;
}
DECLARE_XBOXKRNL_EXPORT1(KeSaveFloatingPointState, kNone, kImplemented);
#endif
} // namespace xboxkrnl
} // namespace kernel
} // namespace xe
DECLARE_XBOXKRNL_EMPTY_REGISTER_EXPORTS(Misc);