Skip to content

Commit

Permalink
Attempt to patch the spin lock taking a lot of time
Browse files Browse the repository at this point in the history
I am guessing this is waiting for the render to complete so it probably won't help to change it but we never know.
Disabled because the mov rax, jmp rax method is messing with branch prediction, the code needs to be placed in .text for this to work correctly
  • Loading branch information
maximegmd committed Dec 13, 2020
1 parent 6c5f8c9 commit 57326cd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
x64/
Win32/
cyberpunk_amd_patch/vsxmake2019/*
cyberpunk_amd_patch/vs2019/*
5 changes: 3 additions & 2 deletions cyberpunk_amd_patch/src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
void PatchAmd(Image* apImage);
void HotPatchFix(Image* apImage);
void StringInitializerFix(Image* apImage);

void PatchSpin(Image* apImage);

void Initialize()
{
Expand All @@ -31,6 +31,7 @@ void Initialize()
Image image;
PatchAmd(&image);
//StringInitializerFix(&image);
//PatchSpin(&image);
HotPatchFix(&image);

logger->flush();
Expand Down Expand Up @@ -92,7 +93,7 @@ void PatchAvx(Image* apImage)
}

spdlog::warn("\tAVX Patch: failed");

}

BOOL APIENTRY DllMain(HMODULE mod, DWORD ul_reason_for_call, LPVOID) {
Expand Down
66 changes: 66 additions & 0 deletions cyberpunk_amd_patch/src/hook_spin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <windows.h>

#include "Image.h"
#include <utility>
#include <cstring>
#include <spdlog/spdlog.h>

struct Mutex
{
uint32_t unk0;
HANDLE handle;
uint8_t pad4[0x40 - 0x10];
int32_t unk40;
};

static_assert(offsetof(Mutex, handle) == 8);
static_assert(offsetof(Mutex, unk40) == 0x40);

void HookSpin(Mutex* apMutex)
{
auto spinCount = apMutex->unk40;
while (true)
{
if (apMutex->unk0 > 0)
{
const auto oldValue = apMutex->unk0;

if (oldValue == _InterlockedCompareExchange(&apMutex->unk0, apMutex->unk0 - 1, apMutex->unk0))
return;
}

const auto oldSpin = spinCount;
spinCount--;

if (oldSpin <= 0)
{
const auto result = _InterlockedExchangeAdd(&apMutex->unk0, 0xFFFFFFFF);
if((int(result) - 1) < 0)
WaitForSingleObject(apMutex->handle, 0xFFFFFFFF);
return;
}
}
}

void PatchSpin(Image* apImage)
{
if (apImage->version != Image::MakeVersion(1, 4))
{
spdlog::info("\tSpin Patch: failed");
return;
}

auto* pFuncPtr = &HookSpin;

auto addr = (uint8_t*)(0x2AEEC70 + apImage->base_address);
DWORD oldProtect = 0;
VirtualProtect(addr, 32, PAGE_EXECUTE_WRITECOPY, &oldProtect);
addr[0] = 0x48;
addr[1] = 0xB8;
std::memcpy(addr + 2, &pFuncPtr, 8);
addr[10] = 0xFF;
addr[11] = 0xE0;
VirtualProtect(addr, 32, oldProtect, nullptr);

spdlog::info("\tSpin Patch: success");
}
6 changes: 4 additions & 2 deletions cyberpunk_amd_patch/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ set_languages("cxx17")

add_requires("zlib", "spdlog")

set_optimize("fastest")

target("performance_overhaul")
set_kind("shared")
add_files("src/**.cpp")
add_includedirs("src/", {public = true})
add_includedirs("src/")
add_syslinks("User32")
add_packages("zlib", "spdlog")
add_packages("zlib", "spdlog")

0 comments on commit 57326cd

Please sign in to comment.