From e54e7fbfcffe167ecefaf854b3584d731b7686bd Mon Sep 17 00:00:00 2001 From: Luke Usher Date: Thu, 3 May 2018 21:23:09 +0100 Subject: [PATCH] Make new RDTSC patch the default, the old method is optional --- resource/Cxbx.rc | Bin 38626 -> 38610 bytes src/Cxbx/ResCxbx.h | 2 +- src/Cxbx/WndMain.cpp | 16 ++++++++-------- src/Cxbx/WndMain.h | 2 +- src/CxbxKrnl/CxbxKrnl.cpp | 12 ++++++------ src/CxbxKrnl/Emu.cpp | 4 ++-- src/CxbxKrnl/Emu.h | 2 +- src/CxbxKrnl/EmuKrnlKe.cpp | 4 ---- src/CxbxKrnl/EmuShared.h | 6 +++--- src/CxbxKrnl/EmuXapi.cpp | 10 +--------- 10 files changed, 23 insertions(+), 35 deletions(-) diff --git a/resource/Cxbx.rc b/resource/Cxbx.rc index 641f567fa0b7acd946c26b882c4ba31955c1e0a9..9e60c89dc4496b020ce24017b388a9cf7d32ae4c 100644 GIT binary patch delta 92 zcmaE~mg&-3rVSlts=*A|44DiC3gdx49*Mz452{UjiHDkm7$QK6iDU)SetDisablePixelShaders(&m_DisablePixelShaders); g_EmuShared->SetUncapFramerate(&m_UncapFramerate); g_EmuShared->SetUseAllCores(&m_UseAllCores); - g_EmuShared->SetPatchCpuFrequency(&m_PatchCpuFrequency); + g_EmuShared->SetSkipRdtscPatching(&m_SkipRdtscPatching); g_EmuShared->SetScaleViewport(&m_ScaleViewport); if (m_ScaleViewport) { diff --git a/src/Cxbx/WndMain.h b/src/Cxbx/WndMain.h index c479c1058..229d0eb83 100644 --- a/src/Cxbx/WndMain.h +++ b/src/Cxbx/WndMain.h @@ -221,7 +221,7 @@ class WndMain : public Wnd int m_DisablePixelShaders; int m_UncapFramerate; int m_UseAllCores; - int m_PatchCpuFrequency; + int m_SkipRdtscPatching; int m_ScaleViewport; // ****************************************************************** diff --git a/src/CxbxKrnl/CxbxKrnl.cpp b/src/CxbxKrnl/CxbxKrnl.cpp index 99994b897..934f9c789 100644 --- a/src/CxbxKrnl/CxbxKrnl.cpp +++ b/src/CxbxKrnl/CxbxKrnl.cpp @@ -583,7 +583,7 @@ void PrintCurrentConfigurationLog() printf("Disable Pixel Shaders: %s\n", g_DisablePixelShaders == 1 ? "On" : "Off"); printf("Uncap Framerate: %s\n", g_UncapFramerate == 1 ? "On" : "Off"); printf("Run Xbox threads on all cores: %s\n", g_UseAllCores == 1 ? "On" : "Off"); - printf("Patch CPU Frequency (rdtsc): %s\n", g_PatchCpuFrequency == 1 ? "On" : "Off"); + printf("Skip RDTSC Patching: %s\n", g_SkipRdtscPatching == 1 ? "On" : "Off"); printf("Scale Xbox to host viewport (and back): %s\n", g_ScaleViewport == 1 ? "On" : "Off"); } @@ -708,7 +708,7 @@ const uint8_t rdtsc_pattern[] = { }; const int sizeof_rdtsc_pattern = sizeof(rdtsc_pattern); -void PatchRdtscInstruction() +void PatchRdtscInstructions() { uint8_t rdtsc[2] = { 0x0F, 0x31 }; DWORD sizeOfImage = CxbxKrnl_XbeHeader->dwSizeofImage; @@ -1200,8 +1200,8 @@ __declspec(noreturn) void CxbxKrnlInit g_UncapFramerate = !!HackEnabled; g_EmuShared->GetUseAllCores(&HackEnabled); g_UseAllCores = !!HackEnabled; - g_EmuShared->GetPatchCpuFrequency(&HackEnabled); - g_PatchCpuFrequency = !!HackEnabled; + g_EmuShared->GetSkipRdtscPatching(&HackEnabled); + g_SkipRdtscPatching = !!HackEnabled; g_EmuShared->GetScaleViewport(&HackEnabled); g_ScaleViewport = !!HackEnabled; } @@ -1366,9 +1366,9 @@ __declspec(noreturn) void CxbxKrnlInit // See: https://multimedia.cx/eggs/xbox-sphinx-protocol/ ApplyMediaPatches(); - if(g_PatchCpuFrequency) + if(!g_SkipRdtscPatching) { - PatchRdtscInstruction(); + PatchRdtscInstructions(); } // Setup per-title encryption keys diff --git a/src/CxbxKrnl/Emu.cpp b/src/CxbxKrnl/Emu.cpp index 410b7aa73..b26bd15d6 100644 --- a/src/CxbxKrnl/Emu.cpp +++ b/src/CxbxKrnl/Emu.cpp @@ -67,7 +67,7 @@ bool g_XInputEnabled = false; bool g_DisablePixelShaders = false; bool g_UncapFramerate = false; bool g_UseAllCores = false; -bool g_PatchCpuFrequency = false; +bool g_SkipRdtscPatching = false; bool g_ScaleViewport = false; // Delta added to host SystemTime, used in xboxkrnl::KeQuerySystemTime and xboxkrnl::NtSetSystemTime @@ -236,7 +236,7 @@ extern int EmuException(LPEXCEPTION_POINTERS e) else { // Check if this exception came from rdtsc, but only whe g_PatchCpuFrequency hack is set. - if (e->ExceptionRecord->ExceptionCode == STATUS_PRIVILEGED_INSTRUCTION && g_PatchCpuFrequency ) + if (e->ExceptionRecord->ExceptionCode == STATUS_PRIVILEGED_INSTRUCTION && !g_SkipRdtscPatching ) { if (IsRdtscInstruction(e->ContextRecord->Eip)) { LARGE_INTEGER PerformanceCount; diff --git a/src/CxbxKrnl/Emu.h b/src/CxbxKrnl/Emu.h index b825da40a..79aad3310 100644 --- a/src/CxbxKrnl/Emu.h +++ b/src/CxbxKrnl/Emu.h @@ -110,6 +110,6 @@ typedef struct DUMMY_KERNEL extern bool g_DisablePixelShaders; extern bool g_UncapFramerate; extern bool g_UseAllCores; -extern bool g_PatchCpuFrequency; +extern bool g_SkipRdtscPatching; extern bool g_ScaleViewport; #endif diff --git a/src/CxbxKrnl/EmuKrnlKe.cpp b/src/CxbxKrnl/EmuKrnlKe.cpp index 1e75d1ab2..e8e4b4b06 100644 --- a/src/CxbxKrnl/EmuKrnlKe.cpp +++ b/src/CxbxKrnl/EmuKrnlKe.cpp @@ -1096,10 +1096,6 @@ XBSYSAPI EXPORTNUM(126) xboxkrnl::ULONGLONG NTAPI xboxkrnl::KeQueryPerformanceCo ULONGLONG ret; ::LARGE_INTEGER PerformanceCounter; //no matter rdtsc is patched or not, we should always return a scaled performance counter here. - /*if (g_PatchCpuFrequency) - { - RETURN((ULONGLONG)__rdtsc()); - }*/ // Dxbx note : Xbox actually uses the RDTSC machine code instruction for this, // and we we're bound to a single core, so we could do that too, but on Windows diff --git a/src/CxbxKrnl/EmuShared.h b/src/CxbxKrnl/EmuShared.h index 9ffedb314..0dc6e802d 100644 --- a/src/CxbxKrnl/EmuShared.h +++ b/src/CxbxKrnl/EmuShared.h @@ -126,8 +126,8 @@ class EmuShared : public Mutex void SetUncapFramerate(int* value) { Lock(); m_UncapFramerate = *value; Unlock(); } void GetUseAllCores(int* value) { Lock(); *value = m_UseAllCores; Unlock(); } void SetUseAllCores(int* value) { Lock(); m_UseAllCores = *value; Unlock(); } - void GetPatchCpuFrequency(int* value) { Lock(); *value = m_PatchCpuFrequeny; Unlock(); } - void SetPatchCpuFrequency(int* value) { Lock(); m_PatchCpuFrequeny = *value; Unlock(); } + void GetSkipRdtscPatching(int* value) { Lock(); *value = m_SkipRdtscPatching; Unlock(); } + void SetSkipRdtscPatching(int* value) { Lock(); m_SkipRdtscPatching = *value; Unlock(); } void GetScaleViewport(int* value) { Lock(); *value = m_ScaleViewport; Unlock(); } void SetScaleViewport(int* value) { Lock(); m_ScaleViewport = *value; Unlock(); } @@ -198,7 +198,7 @@ class EmuShared : public Mutex int m_DisablePixelShaders; int m_UncapFramerate; int m_UseAllCores; - int m_PatchCpuFrequeny; + int m_SkipRdtscPatching; float m_MSpF; float m_FPS; bool m_bMultiXbeFlag; diff --git a/src/CxbxKrnl/EmuXapi.cpp b/src/CxbxKrnl/EmuXapi.cpp index 3fc3c1a5f..f9710a005 100644 --- a/src/CxbxKrnl/EmuXapi.cpp +++ b/src/CxbxKrnl/EmuXapi.cpp @@ -954,15 +954,7 @@ BOOL WINAPI XTL::EMUPATCH(QueryPerformanceCounter) { FUNC_EXPORTS; - //if the rdtsc is patched, we shall keep using scaled PerformanceCounter. - /*if (g_PatchCpuFrequency) - { - lpPerformanceCount->QuadPart = (LONGLONG)__rdtsc(); - return TRUE; - - }*/ - - lpPerformanceCount->QuadPart = xboxkrnl::KeQueryPerformanceCounter(); + lpPerformanceCount->QuadPart = xboxkrnl::KeQueryPerformanceCounter(); return TRUE; }