-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to patch the spin lock taking a lot of time
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
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
x64/ | ||
Win32/ | ||
cyberpunk_amd_patch/vsxmake2019/* | ||
cyberpunk_amd_patch/vs2019/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters