Skip to content

Commit 60d1b6f

Browse files
author
osy
committed
Big Sur: fix WindowServer crash on startup
A new function amdMtl_CreateTargetDeviceArchitecture in AMDMTLBronzeDriver has a bug where if the deviceID is not found in a list of supported ids, then amdMtl_HWL_GetDeviceArchGroup returns NULL and crashes the process (WindowServer). We patch AMDRadeonX4000_AMDAccelDevice::getHardwareInfo which returns the deviceID to return a fake ID and prevent the crash.
1 parent 0c9b743 commit 60d1b6f

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

Polaris22Fixup.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@
404404
MODULE_NAME = com.osy86.Polaris22Fixup;
405405
MODULE_START = "$(PRODUCT_NAME)_kern_start";
406406
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
407-
MODULE_VERSION = 1.2.1;
407+
MODULE_VERSION = 1.3.0b1;
408408
PRODUCT_BUNDLE_IDENTIFIER = com.osy86.Polaris22Fixup;
409409
PRODUCT_NAME = "$(TARGET_NAME)";
410410
WRAPPER_EXTENSION = kext;
@@ -428,7 +428,7 @@
428428
MODULE_NAME = com.osy86.Polaris22Fixup;
429429
MODULE_START = "$(PRODUCT_NAME)_kern_start";
430430
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
431-
MODULE_VERSION = 1.2.1;
431+
MODULE_VERSION = 1.3.0b1;
432432
PRODUCT_BUNDLE_IDENTIFIER = com.osy86.Polaris22Fixup;
433433
PRODUCT_NAME = "$(TARGET_NAME)";
434434
WRAPPER_EXTENSION = kext;

Polaris22Fixup/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<key>OSBundleLibraries</key>
4040
<dict>
4141
<key>as.vit9696.Lilu</key>
42-
<string>1.3.8</string>
42+
<string>1.4.7</string>
4343
<key>com.apple.kpi.bsd</key>
4444
<string>12.0.0</string>
4545
<key>com.apple.kpi.dsep</key>

Polaris22Fixup/kern_start.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ static const int kPathMaxLen = 1024;
1717

1818
#pragma mark - Patches
1919

20+
static const int kEllesmereDeviceId = 0x67DF;
21+
2022
static const uint8_t kAmdBronzeMtlAddrLibGetBaseArrayModeReturnOriginal[] = {
2123
0xb8, 0x02, 0x00, 0x00, 0x00, 0x0f, 0x43, 0xc1, 0xeb,
2224
};
@@ -39,13 +41,21 @@ static const char kDyldCachePath[] = "/private/var/db/dyld/dyld_shared_cache_x86
3941

4042
static const char *kAmdRadeonX4000HwLibsPath[] { "/System/Library/Extensions/AMDRadeonX4000HWServices.kext/Contents/PlugIns/AMDRadeonX4000HWLibs.kext/Contents/MacOS/AMDRadeonX4000HWLibs" };
4143

44+
static const char *kAmdRadeonX4000Path[] { "/System/Library/Extensions/AMDRadeonX4000.kext/Contents/MacOS/AMDRadeonX4000" };
45+
46+
enum {
47+
kAmdRadeonX4000=0,
48+
kAmdRadeonX4000HwLibs,
49+
};
50+
4251
static KernelPatcher::KextInfo kAMDHWLibsInfo[] = {
43-
{ "com.apple.kext.AMDRadeonX4000HWLibs", kAmdRadeonX4000HwLibsPath, arrsize(kAmdRadeonX4000HwLibsPath), {true}, {}, KernelPatcher::KextInfo::Unloaded },
52+
[kAmdRadeonX4000] = { "com.apple.kext.AMDRadeonX4000", kAmdRadeonX4000Path, arrsize(kAmdRadeonX4000Path), {true}, {}, KernelPatcher::KextInfo::Unloaded },
53+
[kAmdRadeonX4000HwLibs] = { "com.apple.kext.AMDRadeonX4000HWLibs", kAmdRadeonX4000HwLibsPath, arrsize(kAmdRadeonX4000HwLibsPath), {true}, {}, KernelPatcher::KextInfo::Unloaded },
4454
};
4555

4656
static mach_vm_address_t orig_cs_validate_range {};
47-
4857
static mach_vm_address_t orig_IsEarlySAMUInitEnabled {};
58+
static mach_vm_address_t orig_getHardwareInfo {};
4959

5060
static uint8_t const *patchFind {};
5161
static uint8_t const *patchReplace {};
@@ -106,6 +116,16 @@ static int patched_IsEarlySAMUInitEnabled(void *ctx) {
106116
return 0;
107117
}
108118

119+
static int patched_getHardwareInfo(void *obj, uint32_t *hwInfo) {
120+
int ret = FunctionCast(patched_getHardwareInfo, orig_getHardwareInfo)(obj, hwInfo);
121+
DBGLOG(MODULE_SHORT, "AMDRadeonX4000_AMDAccelDevice::getHardwareInfo: return 0x%08X");
122+
if (ret == 0) {
123+
SYSLOG(MODULE_SHORT, "getHardwareInfo: deviceId = 0x%x", *hwInfo);
124+
*hwInfo = kEllesmereDeviceId;
125+
}
126+
return ret;
127+
}
128+
109129
#pragma mark - Patches on start/stop
110130

111131
static void pluginStart() {
@@ -145,7 +165,16 @@ static void pluginStart() {
145165
error = lilu.onKextLoad(kAMDHWLibsInfo, arrsize(kAMDHWLibsInfo), [](void *user, KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size){
146166
DBGLOG(MODULE_SHORT, "processing AMDRadeonX4000HWLibs");
147167
for (size_t i = 0; i < arrsize(kAMDHWLibsInfo); i++) {
148-
if (kAMDHWLibsInfo[i].loadIndex == index) {
168+
if (i == kAmdRadeonX4000 && kAMDHWLibsInfo[i].loadIndex == index) {
169+
KernelPatcher::RouteRequest amd_requests[] {
170+
KernelPatcher::RouteRequest("__ZN29AMDRadeonX4000_AMDAccelDevice15getHardwareInfoEP24_sAMD_GET_HW_INFO_VALUES", patched_getHardwareInfo, orig_getHardwareInfo),
171+
};
172+
if (patcher.routeMultiple(index, amd_requests, address, size, true, true)) {
173+
DBGLOG(MODULE_SHORT, "patched getHardwareInfo");
174+
} else {
175+
SYSLOG(MODULE_SHORT, "failed to patch getHardwareInfo: %d", patcher.getError());
176+
}
177+
} else if (i == kAmdRadeonX4000HwLibs && kAMDHWLibsInfo[i].loadIndex == index) {
149178
KernelPatcher::RouteRequest amd_requests[] {
150179
KernelPatcher::RouteRequest("_PECI_IsEarlySAMUInitEnabled", patched_IsEarlySAMUInitEnabled, orig_IsEarlySAMUInitEnabled),
151180
};
@@ -185,6 +214,6 @@ PluginConfiguration ADDPR(config) {
185214
bootargBeta,
186215
arrsize(bootargBeta),
187216
KernelVersion::Mojave,
188-
KernelVersion::Catalina,
217+
KernelVersion::BigSur,
189218
pluginStart
190219
};

0 commit comments

Comments
 (0)