Skip to content

Commit

Permalink
Make new HWAlignMgr patches static
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualEhrmanntraut committed Aug 16, 2024
1 parent 3e56dee commit ac31b2c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
57 changes: 4 additions & 53 deletions NootedRed/X6000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ void X6000::init() {
lilu.onKextLoadForce(&kextRadeonX6000);
}

template<UInt32 N>
struct HWAlignVTableFix {
void *func;
const UInt32 offs[N];
const UInt32 occurances[N];
const UInt32 len {N};

void apply() const {
for (UInt32 i = 0; i < this->len; i += 1) {
const UInt32 off = this->offs[i];
const UInt32 newOff = (off == 0x128) ? 0x230 : (off - 8);
const UInt32 count = this->occurances[i];
const UInt8 vtableCallPattern[] = {0xFF, 0x00, static_cast<UInt8>(off & 0xFF),
static_cast<UInt8>((off >> 8) & 0xFF), static_cast<UInt8>((off >> 16) & 0xFF),
static_cast<UInt8>((off >> 24) & 0xFF)};
const UInt8 vtableCallMask[] = {0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
const UInt8 vtableCallReplacement[] = {0xFF, 0x00, static_cast<UInt8>(newOff & 0xFF),
static_cast<UInt8>((newOff >> 8) & 0xFF), static_cast<UInt8>((newOff >> 16) & 0xFF),
static_cast<UInt8>((newOff >> 24) & 0xFF)};
PANIC_COND(!KernelPatcher::findAndReplaceWithMask(this->func, PAGE_SIZE, vtableCallPattern, vtableCallMask,
vtableCallReplacement, vtableCallMask, count, 0),
"X6000", "Failed to apply virtual call fix");
}
}
};

bool X6000::processKext(KernelPatcher &patcher, size_t id, mach_vm_address_t slide, size_t size) {
if (kextRadeonX6000.loadIndex == id) {
NRed::callback->setRMMIOIfNecessary();
Expand Down Expand Up @@ -192,33 +166,10 @@ bool X6000::processKext(KernelPatcher &patcher, size_t id, mach_vm_address_t sli
}

// Now, for AMDHWDisplay, fix the VTable offsets to calls in HWAlignManager2.
const HWAlignVTableFix<2> fixOrgFillUBMSurface {
orgFillUBMSurface,
{0x1B8, 0x218},
{1, 1},
};
fixOrgFillUBMSurface.apply();
const HWAlignVTableFix<3> fixConfigureDisplay {
orgConfigureDisplay,
{0x1B8, 0x200, 0x218},
{2, 2, 2},
};
fixConfigureDisplay.apply();
const HWAlignVTableFix<4> fixGetDisplayInfo {
orgGetDisplayInfo,
{0x128, 0x130, 0x138, 0x1D0},
{1, 2, 2, 4},
};
fixGetDisplayInfo.apply();

if (orgAllocateScanoutFB != nullptr) {
const HWAlignVTableFix<5> fixAllocateScanoutFB {
orgAllocateScanoutFB,
{0x130, 0x138, 0x190, 0x1B0, 0x218},
{1, 1, 1, 1, 1},
};
fixAllocateScanoutFB.apply();
}
FillUBMSurfaceVTFix.apply(orgFillUBMSurface);
ConfigureDisplayVTFix.apply(orgConfigureDisplay);
GetDisplayInfoVTFix.apply(orgGetDisplayInfo);
if (orgAllocateScanoutFB != nullptr) { AllocateScanoutFBVTFix.apply(orgAllocateScanoutFB); }

return true;
}
Expand Down
45 changes: 45 additions & 0 deletions NootedRed/X6000.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ class X6000 {
static void wrapInitDCNRegistersOffsets(void *that);
};

template<UInt32 N>
struct HWAlignVTableFix {
const UInt32 offs[N];
const UInt32 occurances[N];
const UInt32 len {N};

void apply(void *toFunction) const {
for (UInt32 i = 0; i < this->len; i += 1) {
const UInt32 off = this->offs[i];
const UInt32 newOff = (off == 0x128) ? 0x230 : (off - 8);
const UInt32 count = this->occurances[i];
const UInt8 vtableCallPattern[] = {0xFF, 0x00, static_cast<UInt8>(off & 0xFF),
static_cast<UInt8>((off >> 8) & 0xFF), static_cast<UInt8>((off >> 16) & 0xFF),
static_cast<UInt8>((off >> 24) & 0xFF)};
const UInt8 vtableCallMask[] = {0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
const UInt8 vtableCallReplacement[] = {0xFF, 0x00, static_cast<UInt8>(newOff & 0xFF),
static_cast<UInt8>((newOff >> 8) & 0xFF), static_cast<UInt8>((newOff >> 16) & 0xFF),
static_cast<UInt8>((newOff >> 24) & 0xFF)};
PANIC_COND(!KernelPatcher::findAndReplaceWithMask(toFunction, PAGE_SIZE, vtableCallPattern, vtableCallMask,
vtableCallReplacement, vtableCallMask, count, 0),
"X6000", "Failed to apply virtual call fix");
}
}
};

//------ Patches ------//

// Mismatched `getTtlInterface` virtual calls
Expand Down Expand Up @@ -195,3 +220,23 @@ static const UInt8 kGetHWEngineCallPatched[] = {0x00, 0x00, 0x00, 0xFF, 0x90, 0x
// Mismatched `getAMDHWHandler` virtual calls.
static const UInt8 kGetAMDHWHandlerCallOriginal[] = {0xFF, 0x90, 0xD0, 0x02, 0x00, 0x00};
static const UInt8 kGetAMDHWHandlerCallPatched[] = {0xFF, 0x90, 0xC8, 0x02, 0x00, 0x00};

static const HWAlignVTableFix<2> FillUBMSurfaceVTFix {
{0x1B8, 0x218},
{1, 1},
};

static const HWAlignVTableFix<3> ConfigureDisplayVTFix {
{0x1B8, 0x200, 0x218},
{2, 2, 2},
};

static const HWAlignVTableFix<4> GetDisplayInfoVTFix {
{0x128, 0x130, 0x138, 0x1D0},
{1, 2, 2, 4},
};

static const HWAlignVTableFix<5> AllocateScanoutFBVTFix {
{0x130, 0x138, 0x190, 0x1B0, 0x218},
{1, 1, 1, 1, 1},
};

0 comments on commit ac31b2c

Please sign in to comment.