Skip to content

Commit 768b75f

Browse files
osyosy
authored andcommitted
Rewrite to depend on Lilu again, fix black screen
* Requires Lilu 1.3.8 which fixed all the issues we originally had * Added new patch to fix booting into a black screen after 10.14.5 beta 2
1 parent eabf34a commit 768b75f

File tree

10 files changed

+656
-630
lines changed

10 files changed

+656
-630
lines changed

Polaris22Fixup.xcodeproj/project.pbxproj

Lines changed: 462 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
<key>CFBundlePackageType</key>
1616
<string>KEXT</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
18+
<string>$(MODULE_VERSION)</string>
1919
<key>CFBundleVersion</key>
20-
<string>1</string>
20+
<string>$(MODULE_VERSION)</string>
2121
<key>IOKitPersonalities</key>
2222
<dict>
23-
<key>$(PRODUCT_NAME:rfc1034identifier)</key>
23+
<key>Polaris22Fixup</key>
2424
<dict>
2525
<key>CFBundleIdentifier</key>
2626
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
@@ -35,17 +35,25 @@
3535
</dict>
3636
</dict>
3737
<key>NSHumanReadableCopyright</key>
38-
<string>Copyright © 2019 osy86. All rights reserved.</string>
38+
<string>Copyright © 2020 osy86. All rights reserved.</string>
3939
<key>OSBundleLibraries</key>
4040
<dict>
41-
<key>com.osy86.CSValidateRangeProxy</key>
42-
<string>12.0.0</string>
41+
<key>as.vit9696.Lilu</key>
42+
<string>1.3.8</string>
4343
<key>com.apple.kpi.bsd</key>
4444
<string>12.0.0</string>
45+
<key>com.apple.kpi.dsep</key>
46+
<string>12.0.0</string>
4547
<key>com.apple.kpi.iokit</key>
4648
<string>12.0.0</string>
4749
<key>com.apple.kpi.libkern</key>
4850
<string>12.0.0</string>
51+
<key>com.apple.kpi.mach</key>
52+
<string>12.0.0</string>
53+
<key>com.apple.kpi.unsupported</key>
54+
<string>12.0.0</string>
4955
</dict>
56+
<key>OSBundleRequired</key>
57+
<string>Root</string>
5058
</dict>
5159
</plist>

Polaris22Fixup/kern_start.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//
2+
// kern_start.cpp
3+
// Polaris22Fixup
4+
//
5+
// Copyright © 2020 osy86. All rights reserved.
6+
//
7+
8+
#include <Headers/plugin_start.hpp>
9+
#include <Headers/kern_api.hpp>
10+
11+
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
12+
#define MODULE_SHORT "p22"
13+
14+
extern "C" void *memmem(const void *h0, size_t k, const void *n0, size_t l);
15+
16+
static const int kPathMaxLen = 1024;
17+
18+
#pragma mark - Patches
19+
20+
static const uint8_t kAmdBronzeMtlAddrLibGetBaseArrayModeReturnOriginal[] = {
21+
0xb8, 0x02, 0x00, 0x00, 0x00, 0x0f, 0x43, 0xc1, 0xeb,
22+
};
23+
24+
static const uint8_t kAmdBronzeMtlAddrLibGetBaseArrayModeReturnPatched[] = {
25+
0xb8, 0x02, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90, 0xeb,
26+
};
27+
28+
static const char kAmdBronzeMtlDriverPath[] = "/System/Library/Extensions/AMDMTLBronzeDriver.bundle/Contents/MacOS/AMDMTLBronzeDriver";
29+
30+
static const char kDyldCachePath[] = "/private/var/db/dyld/dyld_shared_cache_x86_64h";
31+
32+
static const char *kAmdRadeonX4000HwLibsPath[] { "/System/Library/Extensions/AMDRadeonX4000HWServices.kext/Contents/PlugIns/AMDRadeonX4000HWLibs.kext/Contents/MacOS/AMDRadeonX4000HWLibs" };
33+
34+
static KernelPatcher::KextInfo kAMDHWLibsInfo[] = {
35+
{ "com.apple.kext.AMDRadeonX4000HWLibs", kAmdRadeonX4000HwLibsPath, arrsize(kAmdRadeonX4000HwLibsPath), {true}, {}, KernelPatcher::KextInfo::Unloaded },
36+
};
37+
38+
static mach_vm_address_t orig_cs_validate_range {};
39+
40+
static mach_vm_address_t orig_IsEarlySAMUInitEnabled {};
41+
42+
#pragma mark - Kernel patching code
43+
44+
/**
45+
* Call block with interrupts and protections disabled
46+
*/
47+
static void doKernelPatch(void (^patchFunc)(void)) {
48+
if (MachInfo::setKernelWriting(true, KernelPatcher::kernelWriteLock) == KERN_SUCCESS) {
49+
DBGLOG(MODULE_SHORT, "obtained write permssions");
50+
} else {
51+
SYSLOG(MODULE_SHORT, "failed to obtain write permissions");
52+
return;
53+
}
54+
55+
patchFunc();
56+
57+
if (MachInfo::setKernelWriting(false, KernelPatcher::kernelWriteLock) == KERN_SUCCESS) {
58+
DBGLOG(MODULE_SHORT, "restored write permssions");
59+
} else {
60+
SYSLOG(MODULE_SHORT, "failed to restore write permissions");
61+
}
62+
}
63+
64+
#pragma mark - Patched functions
65+
66+
static boolean_t patched_cs_validate_range(vnode_t vp,
67+
memory_object_t pager,
68+
memory_object_offset_t offset,
69+
const void *data,
70+
vm_size_t size,
71+
unsigned *result) {
72+
char path[kPathMaxLen];
73+
int pathlen = kPathMaxLen;
74+
boolean_t res = FunctionCast(patched_cs_validate_range, orig_cs_validate_range)(vp, pager, offset, data, size, result);
75+
if (res && vn_getpath(vp, path, &pathlen) == 0) {
76+
static_assert(sizeof(kAmdBronzeMtlDriverPath) <= sizeof(path), "path too long");
77+
static_assert(sizeof(kDyldCachePath) <= sizeof(path), "path too long");
78+
if (UNLIKELY(strncmp(path, kAmdBronzeMtlDriverPath, sizeof(kAmdBronzeMtlDriverPath)) == 0) ||
79+
UNLIKELY(strncmp(path, kDyldCachePath, sizeof(kDyldCachePath)) == 0)) {
80+
void *res;
81+
if (UNLIKELY((res = memmem(data, size, kAmdBronzeMtlAddrLibGetBaseArrayModeReturnOriginal, sizeof(kAmdBronzeMtlAddrLibGetBaseArrayModeReturnOriginal))) != NULL)) {
82+
SYSLOG(MODULE_SHORT, "found function to patch!");
83+
doKernelPatch(^{
84+
static_assert(sizeof(kAmdBronzeMtlAddrLibGetBaseArrayModeReturnOriginal) == sizeof(kAmdBronzeMtlAddrLibGetBaseArrayModeReturnPatched), "patch size invalid");
85+
lilu_os_memcpy(res, kAmdBronzeMtlAddrLibGetBaseArrayModeReturnPatched, sizeof(kAmdBronzeMtlAddrLibGetBaseArrayModeReturnPatched));
86+
});
87+
}
88+
}
89+
}
90+
return res;
91+
}
92+
93+
static int patched_IsEarlySAMUInitEnabled(void *ctx) {
94+
DBGLOG(MODULE_SHORT, "PECI_IsEarlySAMUInitEnabled: return 0");
95+
return 0;
96+
}
97+
98+
#pragma mark - Patches on start/stop
99+
100+
static void pluginStart() {
101+
LiluAPI::Error error;
102+
103+
DBGLOG(MODULE_SHORT, "start");
104+
error = lilu.onPatcherLoad([](void *user, KernelPatcher &patcher){
105+
DBGLOG(MODULE_SHORT, "patching cs_validate_range");
106+
mach_vm_address_t kern = patcher.solveSymbol(KernelPatcher::KernelID, "_cs_validate_range");
107+
108+
if (patcher.getError() == KernelPatcher::Error::NoError) {
109+
orig_cs_validate_range = patcher.routeFunctionLong(kern, reinterpret_cast<mach_vm_address_t>(patched_cs_validate_range), true, true);
110+
111+
if (patcher.getError() != KernelPatcher::Error::NoError) {
112+
SYSLOG(MODULE_SHORT, "failed to hook _cs_validate_range");
113+
} else {
114+
DBGLOG(MODULE_SHORT, "hooked cs_validate_range");
115+
}
116+
} else {
117+
SYSLOG(MODULE_SHORT, "failed to find _cs_validate_range");
118+
}
119+
});
120+
if (error != LiluAPI::Error::NoError) {
121+
SYSLOG(MODULE_SHORT, "failed to register onPatcherLoad method: %d", error);
122+
}
123+
error = lilu.onKextLoad(kAMDHWLibsInfo, arrsize(kAMDHWLibsInfo), [](void *user, KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size){
124+
DBGLOG(MODULE_SHORT, "processing AMDRadeonX4000HWLibs");
125+
for (size_t i = 0; i < arrsize(kAMDHWLibsInfo); i++) {
126+
if (kAMDHWLibsInfo[i].loadIndex == index) {
127+
KernelPatcher::RouteRequest amd_requests[] {
128+
KernelPatcher::RouteRequest("_PECI_IsEarlySAMUInitEnabled", patched_IsEarlySAMUInitEnabled, orig_IsEarlySAMUInitEnabled),
129+
};
130+
if (patcher.routeMultiple(index, amd_requests, address, size, true, true)) {
131+
DBGLOG(MODULE_SHORT, "patched PECI_IsEarlySAMUInitEnabled");
132+
} else {
133+
SYSLOG(MODULE_SHORT, "failed to patch PECI_IsEarlySAMUInitEnabled: %d", patcher.getError());
134+
}
135+
}
136+
}
137+
});
138+
if (error != LiluAPI::Error::NoError) {
139+
SYSLOG(MODULE_SHORT, "failed to register onKextLoad method: %d", error);
140+
}
141+
}
142+
143+
// Boot args.
144+
static const char *bootargOff[] {
145+
"-polaris22off"
146+
};
147+
static const char *bootargDebug[] {
148+
"-polaris22dbg"
149+
};
150+
static const char *bootargBeta[] {
151+
"-polaris22beta"
152+
};
153+
154+
// Plugin configuration.
155+
PluginConfiguration ADDPR(config) {
156+
xStringify(PRODUCT_NAME),
157+
parseModuleVersion(xStringify(MODULE_VERSION)),
158+
LiluAPI::AllowNormal,
159+
bootargOff,
160+
arrsize(bootargOff),
161+
bootargDebug,
162+
arrsize(bootargDebug),
163+
bootargBeta,
164+
arrsize(bootargBeta),
165+
KernelVersion::Mojave,
166+
KernelVersion::Catalina,
167+
pluginStart
168+
};

0 commit comments

Comments
 (0)