Skip to content

Commit c093745

Browse files
committed
VERSION 1.1.1: Various typo fixed
1 parent 59a3bc5 commit c093745

12 files changed

+395
-369
lines changed

CPUFriend.xcodeproj/project.pbxproj

+22-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
1C748C2D1C21952C0024EED2 /* kern_start.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C748C2C1C21952C0024EED2 /* kern_start.cpp */; };
1111
CE405EC91E49DD9700AA0B3D /* libkmod.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CE405EC71E49DD7100AA0B3D /* libkmod.a */; };
1212
CE405ED91E4A080700AA0B3D /* plugin_start.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CE405ED81E4A080700AA0B3D /* plugin_start.cpp */; };
13-
CEBD8C191F372A4600BBCB85 /* kern_cpuf.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CEBD8C171F372A4600BBCB85 /* kern_cpuf.cpp */; };
14-
CEBD8C1A1F372A4600BBCB85 /* kern_cpuf.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CEBD8C181F372A4600BBCB85 /* kern_cpuf.hpp */; };
13+
CEBD8C191F372A4600BBCB85 /* CPUFriend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CEBD8C171F372A4600BBCB85 /* CPUFriend.cpp */; };
14+
CEBD8C1A1F372A4600BBCB85 /* CPUFriend.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CEBD8C181F372A4600BBCB85 /* CPUFriend.hpp */; };
1515
/* End PBXBuildFile section */
1616

1717
/* Begin PBXCopyFilesBuildPhase section */
@@ -32,7 +32,7 @@
3232
1C748C2E1C21952C0024EED2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3333
1CF01C901C8CF97F002DCEA3 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
3434
1CF01C921C8CF997002DCEA3 /* Changelog.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = Changelog.md; sourceTree = "<group>"; };
35-
1CF01C931C8DF02E002DCEA3 /* LICENSE.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE.txt; sourceTree = "<group>"; };
35+
1CF01C931C8DF02E002DCEA3 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
3636
83023F381F3747EC00850499 /* Instructions.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = Instructions.md; sourceTree = "<group>"; };
3737
CE2DC5071F7626EC004FAC48 /* ResourceConverter.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = ResourceConverter.sh; sourceTree = "<group>"; };
3838
CE405EBA1E49DD7100AA0B3D /* kern_compression.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = kern_compression.hpp; sourceTree = "<group>"; };
@@ -50,8 +50,9 @@
5050
CE405ED21E49F9FC00AA0B3D /* kern_api.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = kern_api.hpp; sourceTree = "<group>"; };
5151
CE405ED81E4A080700AA0B3D /* plugin_start.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = plugin_start.cpp; sourceTree = "<group>"; };
5252
CE405EDA1E4A080F00AA0B3D /* plugin_start.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = plugin_start.hpp; sourceTree = "<group>"; };
53-
CEBD8C171F372A4600BBCB85 /* kern_cpuf.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = kern_cpuf.cpp; sourceTree = "<group>"; };
54-
CEBD8C181F372A4600BBCB85 /* kern_cpuf.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = kern_cpuf.hpp; sourceTree = "<group>"; };
53+
CEB1294E1F99F986004BA715 /* build.command */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = build.command; sourceTree = "<group>"; };
54+
CEBD8C171F372A4600BBCB85 /* CPUFriend.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CPUFriend.cpp; sourceTree = "<group>"; };
55+
CEBD8C181F372A4600BBCB85 /* CPUFriend.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CPUFriend.hpp; sourceTree = "<group>"; };
5556
/* End PBXFileReference section */
5657

5758
/* Begin PBXFrameworksBuildPhase section */
@@ -71,6 +72,7 @@
7172
children = (
7273
1CF01C911C8CF982002DCEA3 /* Docs */,
7374
CE405EC81E49DD7B00AA0B3D /* SDK */,
75+
CEB1294D1F99F986004BA715 /* Tools */,
7476
CE2DC5061F7626EC004FAC48 /* ResourceConverter */,
7577
1C748C291C21952C0024EED2 /* CPUFriend */,
7678
1C748C281C21952C0024EED2 /* Products */,
@@ -97,7 +99,7 @@
9799
1CF01C911C8CF982002DCEA3 /* Docs */ = {
98100
isa = PBXGroup;
99101
children = (
100-
1CF01C931C8DF02E002DCEA3 /* LICENSE.txt */,
102+
1CF01C931C8DF02E002DCEA3 /* LICENSE */,
101103
1CF01C921C8CF997002DCEA3 /* Changelog.md */,
102104
83023F381F3747EC00850499 /* Instructions.md */,
103105
1CF01C901C8CF97F002DCEA3 /* README.md */,
@@ -109,8 +111,8 @@
109111
isa = PBXGroup;
110112
children = (
111113
1C748C2C1C21952C0024EED2 /* kern_start.cpp */,
112-
CEBD8C181F372A4600BBCB85 /* kern_cpuf.hpp */,
113-
CEBD8C171F372A4600BBCB85 /* kern_cpuf.cpp */,
114+
CEBD8C181F372A4600BBCB85 /* CPUFriend.hpp */,
115+
CEBD8C171F372A4600BBCB85 /* CPUFriend.cpp */,
114116
);
115117
name = Source;
116118
sourceTree = "<group>";
@@ -171,14 +173,22 @@
171173
name = SDK;
172174
sourceTree = "<group>";
173175
};
176+
CEB1294D1F99F986004BA715 /* Tools */ = {
177+
isa = PBXGroup;
178+
children = (
179+
CEB1294E1F99F986004BA715 /* build.command */,
180+
);
181+
path = Tools;
182+
sourceTree = "<group>";
183+
};
174184
/* End PBXGroup section */
175185

176186
/* Begin PBXHeadersBuildPhase section */
177187
1C748C241C21952C0024EED2 /* Headers */ = {
178188
isa = PBXHeadersBuildPhase;
179189
buildActionMask = 2147483647;
180190
files = (
181-
CEBD8C1A1F372A4600BBCB85 /* kern_cpuf.hpp in Headers */,
191+
CEBD8C1A1F372A4600BBCB85 /* CPUFriend.hpp in Headers */,
182192
);
183193
runOnlyForDeploymentPostprocessing = 0;
184194
};
@@ -239,7 +249,7 @@
239249
isa = PBXSourcesBuildPhase;
240250
buildActionMask = 2147483647;
241251
files = (
242-
CEBD8C191F372A4600BBCB85 /* kern_cpuf.cpp in Sources */,
252+
CEBD8C191F372A4600BBCB85 /* CPUFriend.cpp in Sources */,
243253
CE405ED91E4A080700AA0B3D /* plugin_start.cpp in Sources */,
244254
1C748C2D1C21952C0024EED2 /* kern_start.cpp in Sources */,
245255
);
@@ -357,7 +367,7 @@
357367
MODULE_NAME = org.vanilla.driver.CPUFriend;
358368
MODULE_START = "$(PRODUCT_NAME)_kern_start";
359369
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
360-
MODULE_VERSION = 1.1.0;
370+
MODULE_VERSION = 1.1.1;
361371
OTHER_CFLAGS = (
362372
"-mmmx",
363373
"-msse",
@@ -402,7 +412,7 @@
402412
MODULE_NAME = org.vanilla.driver.CPUFriend;
403413
MODULE_START = "$(PRODUCT_NAME)_kern_start";
404414
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
405-
MODULE_VERSION = 1.1.0;
415+
MODULE_VERSION = 1.1.1;
406416
OTHER_CFLAGS = (
407417
"-mmmx",
408418
"-msse",

CPUFriend/CPUFriend.cpp

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//
2+
// CPUFriend.cpp
3+
// CPUFriend
4+
//
5+
// Copyright © 2017 Vanilla. All rights reserved.
6+
//
7+
8+
#include <Headers/kern_api.hpp>
9+
10+
#include "CPUFriend.hpp"
11+
12+
static const char * binList[] {
13+
"/System/Library/Extensions/IOPlatformPluginFamily.kext/Contents/PlugIns/X86PlatformPlugin.kext/Contents/MacOS/X86PlatformPlugin"
14+
};
15+
16+
static const char * idList[] {
17+
"com.apple.driver.X86PlatformPlugin"
18+
};
19+
20+
static const char * symbolList[] {
21+
"__ZN17X86PlatformPlugin22configResourceCallbackEjiPKvjPv"
22+
};
23+
24+
static KernelPatcher::KextInfo kextList[] {
25+
{ idList[0], & binList[0], arrsize(binList), { false, false }, {}, KernelPatcher::KextInfo::Unloaded }
26+
};
27+
28+
static constexpr size_t kextListSize = arrsize(kextList);
29+
30+
static CPUFriendPlugin * callbackCpuf = nullptr;
31+
32+
OSDefineMetaClassAndStructors(CPUFriendPlatform, IOService)
33+
34+
IOService * CPUFriendPlatform::probe(IOService * provider, SInt32 * score) {
35+
if (provider) {
36+
if (callbackCpuf) {
37+
if (! callbackCpuf->frequencyData) {
38+
auto name = provider->getName();
39+
if (! name)
40+
name = "(null)";
41+
DBGLOG("probe", "looking for cf-frequency-data in %s", name);
42+
43+
auto data = OSDynamicCast(OSData, provider->getProperty("cf-frequency-data"));
44+
if (! data) {
45+
auto cpu = provider->getParentEntry(gIOServicePlane);
46+
if (cpu) {
47+
name = cpu->getName();
48+
if (! name)
49+
name = "(null)";
50+
DBGLOG("probe", "looking for cf-frequency-data in %s", name);
51+
data = OSDynamicCast(OSData, cpu->getProperty("cf-frequency-data"));
52+
} else {
53+
SYSLOG("probe", "unable to access cpu parent");
54+
}
55+
}
56+
57+
if (data) {
58+
callbackCpuf->frequencyDataSize = data->getLength();
59+
callbackCpuf->frequencyData = data->getBytesNoCopy();
60+
} else {
61+
SYSLOG("probe", "failed to obtain cf-frequency-data");
62+
}
63+
}
64+
} else
65+
SYSLOG("probe", "missing storage instance");
66+
}
67+
68+
return nullptr;
69+
}
70+
71+
bool CPUFriendPlugin::init() {
72+
callbackCpuf = this;
73+
74+
LiluAPI::Error error = lilu.onKextLoad(kextList, kextListSize, [](void * user, KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size) {
75+
static_cast<CPUFriendPlugin * >(user)->processKext(patcher, index, address, size);
76+
}, this);
77+
78+
if (error != LiluAPI::Error::NoError) {
79+
SYSLOG("init", "failed to register onKextLoad method %d", error);
80+
return false;
81+
}
82+
83+
return true;
84+
}
85+
86+
void CPUFriendPlugin::myConfigResourceCallback(uint32_t requestTag, kern_return_t result, const void * resourceData, uint32_t resourceDataLength, void * context) {
87+
if (callbackCpuf && callbackCpuf->orgConfigLoadCallback) {
88+
auto data = callbackCpuf->frequencyData;
89+
auto sz = callbackCpuf->frequencyDataSize;
90+
if (data && sz > 0) {
91+
DBGLOG("myConfigResourceCallback", "feeding frequency data %u", sz);
92+
resourceData = data;
93+
resourceDataLength = sz;
94+
result = kOSReturnSuccess;
95+
} else {
96+
SYSLOG("myConfigResourceCallback", "failed to feed cpu data (%u, %d)", sz, data != nullptr);
97+
}
98+
callbackCpuf->orgConfigLoadCallback(requestTag, result, resourceData, resourceDataLength, context);
99+
} else {
100+
SYSLOG("myConfigResourceCallback", "config callback arrived at nowhere");
101+
}
102+
}
103+
104+
void CPUFriendPlugin::processKext(KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size) {
105+
if (progressState != ProcessingState::EverythingDone) {
106+
for (size_t i = 0; i < kextListSize; i++) {
107+
if (kextList[i].loadIndex == index) {
108+
DBGLOG("processKext", "current kext is %s progressState %d", kextList[i].id, progressState);
109+
// clear error from the very beginning just in case
110+
patcher.clearError();
111+
if (! strcmp(kextList[i].id, idList[0])) {
112+
auto callback = patcher.solveSymbol(index, symbolList[0]);
113+
if (callback) {
114+
orgConfigLoadCallback = reinterpret_cast<t_callback>(patcher.routeFunction(callback, reinterpret_cast<mach_vm_address_t>(myConfigResourceCallback), true));
115+
if (patcher.getError() == KernelPatcher::Error::NoError) {
116+
DBGLOG("processKext", "routed %s", symbolList[0]);
117+
} else {
118+
SYSLOG("processKext", "failed to route %s", symbolList[0]);
119+
}
120+
} else {
121+
SYSLOG("processKext", "failed to find %s", symbolList[0]);
122+
}
123+
progressState |= ProcessingState::CallbackRouted;
124+
}
125+
}
126+
}
127+
}
128+
// Ignore all the errors for other processors
129+
patcher.clearError();
130+
}

CPUFriend/CPUFriend.hpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// CPUFriend.hpp
3+
// CPUFriend
4+
//
5+
// Copyright © 2017 Vanilla. All rights reserved.
6+
//
7+
8+
#ifndef kern_cpuf_hpp
9+
#define kern_cpuf_hpp
10+
11+
#include <Headers/kern_patcher.hpp>
12+
#include <Library/LegacyIOService.h>
13+
14+
class EXPORT CPUFriendPlatform : public IOService {
15+
OSDeclareDefaultStructors(CPUFriendPlatform)
16+
public:
17+
IOService * probe(IOService * provider, SInt32 *score) override;
18+
};
19+
20+
class CPUFriendPlugin {
21+
public:
22+
bool init();
23+
24+
/**
25+
* ResourceLoad callback type
26+
*/
27+
using t_callback = void (*)(uint32_t, kern_return_t, const void *, uint32_t, void *);
28+
29+
/**
30+
* Trampolines for original resource load callback
31+
*/
32+
t_callback orgConfigLoadCallback = nullptr;
33+
34+
/**
35+
* Loaded user-specified frequency data
36+
*/
37+
const void * frequencyData = nullptr;
38+
39+
/**
40+
* Loaded user-specified frequency data size
41+
*/
42+
uint32_t frequencyDataSize = 0;
43+
44+
private:
45+
/**
46+
* Hooked ResourceLoad callback returning user-specified platform data
47+
*/
48+
static void myConfigResourceCallback(uint32_t requestTag, kern_return_t result, const void * resourceData, uint32_t resourceDataLength, void * context);
49+
50+
/**
51+
* Patch kext if needed and prepare other patches
52+
*
53+
* @param patcher KernelPatcher instance
54+
* @param index kinfo handle
55+
* @param address kinfo load address
56+
* @param size kinfo memory size
57+
*/
58+
void processKext(KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size);
59+
60+
/**
61+
* Current progress mask
62+
*/
63+
struct ProcessingState {
64+
enum : uint32_t {
65+
NothingReady = 0,
66+
CallbackRouted = 1,
67+
EverythingDone = CallbackRouted
68+
};
69+
};
70+
uint32_t progressState = ProcessingState::NothingReady;
71+
};
72+
73+
#endif /* kern_cpuf_hpp */

0 commit comments

Comments
 (0)