From 22b1fea96d6ef9d9ae7710da93630b8882d5c0e9 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Thu, 10 Aug 2023 11:42:20 -0700 Subject: [PATCH] WOW64: Handle unaligned atomic accesses This is done in EnsureConsistentState rather than as a VEH to avoid needing to go through all of wine's exception handling logic for such a hot path. --- Source/Windows/WOW64/Module.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp index 890712c1f..42b803aa3 100644 --- a/Source/Windows/WOW64/Module.cpp +++ b/Source/Windows/WOW64/Module.cpp @@ -204,6 +204,21 @@ namespace Context { Context::StoreWowContextFromState(TLS.ThreadState(), &TmpWowContext); return RtlWow64SetThreadContext(Thread, &TmpWowContext); } + + bool HandleUnalignedAccess(CONTEXT *Context) { + if (!GetTLS().ThreadState()->CPUBackend->IsAddressInCodeBuffer(Context->Pc)) { + return false; + } + + FEX_CONFIG_OPT(ParanoidTSO, PARANOIDTSO); + const auto Result = FEXCore::ArchHelpers::Arm64::HandleUnalignedAccess(ParanoidTSO(), Context->Pc, &Context->X0); + if (!Result.first) { + return false; + } + + Context->Pc += Result.second; + return true; + } } namespace Logging { @@ -423,6 +438,18 @@ void BTCpuSimulate() { } } +NTSTATUS BTCpuResetToConsistentState(EXCEPTION_POINTERS *Ptrs) { + auto *Context = Ptrs->ContextRecord; + const auto *Exception = Ptrs->ExceptionRecord; + + if (Exception->ExceptionCode == EXCEPTION_DATATYPE_MISALIGNMENT && Context::HandleUnalignedAccess(Context)) { + LogMan::Msg::DFmt("Handled unaligned atomic: new pc: {:X}", Context->Pc); + NtContinue(Context, FALSE); + } + + return STATUS_SUCCESS; +} + BOOLEAN WINAPI BTCpuIsProcessorFeaturePresent(UINT Feature) { switch (Feature) { case PF_FLOATING_POINT_PRECISION_ERRATA: