Skip to content

Commit

Permalink
VERSION 1.1.1: Various typo fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
PMheart committed Oct 20, 2017
1 parent 59a3bc5 commit c093745
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 369 deletions.
34 changes: 22 additions & 12 deletions CPUFriend.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
1C748C2D1C21952C0024EED2 /* kern_start.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1C748C2C1C21952C0024EED2 /* kern_start.cpp */; };
CE405EC91E49DD9700AA0B3D /* libkmod.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CE405EC71E49DD7100AA0B3D /* libkmod.a */; };
CE405ED91E4A080700AA0B3D /* plugin_start.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CE405ED81E4A080700AA0B3D /* plugin_start.cpp */; };
CEBD8C191F372A4600BBCB85 /* kern_cpuf.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CEBD8C171F372A4600BBCB85 /* kern_cpuf.cpp */; };
CEBD8C1A1F372A4600BBCB85 /* kern_cpuf.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CEBD8C181F372A4600BBCB85 /* kern_cpuf.hpp */; };
CEBD8C191F372A4600BBCB85 /* CPUFriend.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CEBD8C171F372A4600BBCB85 /* CPUFriend.cpp */; };
CEBD8C1A1F372A4600BBCB85 /* CPUFriend.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CEBD8C181F372A4600BBCB85 /* CPUFriend.hpp */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -71,6 +72,7 @@
children = (
1CF01C911C8CF982002DCEA3 /* Docs */,
CE405EC81E49DD7B00AA0B3D /* SDK */,
CEB1294D1F99F986004BA715 /* Tools */,
CE2DC5061F7626EC004FAC48 /* ResourceConverter */,
1C748C291C21952C0024EED2 /* CPUFriend */,
1C748C281C21952C0024EED2 /* Products */,
Expand All @@ -97,7 +99,7 @@
1CF01C911C8CF982002DCEA3 /* Docs */ = {
isa = PBXGroup;
children = (
1CF01C931C8DF02E002DCEA3 /* LICENSE.txt */,
1CF01C931C8DF02E002DCEA3 /* LICENSE */,
1CF01C921C8CF997002DCEA3 /* Changelog.md */,
83023F381F3747EC00850499 /* Instructions.md */,
1CF01C901C8CF97F002DCEA3 /* README.md */,
Expand All @@ -109,8 +111,8 @@
isa = PBXGroup;
children = (
1C748C2C1C21952C0024EED2 /* kern_start.cpp */,
CEBD8C181F372A4600BBCB85 /* kern_cpuf.hpp */,
CEBD8C171F372A4600BBCB85 /* kern_cpuf.cpp */,
CEBD8C181F372A4600BBCB85 /* CPUFriend.hpp */,
CEBD8C171F372A4600BBCB85 /* CPUFriend.cpp */,
);
name = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -171,14 +173,22 @@
name = SDK;
sourceTree = "<group>";
};
CEB1294D1F99F986004BA715 /* Tools */ = {
isa = PBXGroup;
children = (
CEB1294E1F99F986004BA715 /* build.command */,
);
path = Tools;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
1C748C241C21952C0024EED2 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
CEBD8C1A1F372A4600BBCB85 /* kern_cpuf.hpp in Headers */,
CEBD8C1A1F372A4600BBCB85 /* CPUFriend.hpp in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -239,7 +249,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CEBD8C191F372A4600BBCB85 /* kern_cpuf.cpp in Sources */,
CEBD8C191F372A4600BBCB85 /* CPUFriend.cpp in Sources */,
CE405ED91E4A080700AA0B3D /* plugin_start.cpp in Sources */,
1C748C2D1C21952C0024EED2 /* kern_start.cpp in Sources */,
);
Expand Down Expand Up @@ -357,7 +367,7 @@
MODULE_NAME = org.vanilla.driver.CPUFriend;
MODULE_START = "$(PRODUCT_NAME)_kern_start";
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
MODULE_VERSION = 1.1.0;
MODULE_VERSION = 1.1.1;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down Expand Up @@ -402,7 +412,7 @@
MODULE_NAME = org.vanilla.driver.CPUFriend;
MODULE_START = "$(PRODUCT_NAME)_kern_start";
MODULE_STOP = "$(PRODUCT_NAME)_kern_stop";
MODULE_VERSION = 1.1.0;
MODULE_VERSION = 1.1.1;
OTHER_CFLAGS = (
"-mmmx",
"-msse",
Expand Down
130 changes: 130 additions & 0 deletions CPUFriend/CPUFriend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// CPUFriend.cpp
// CPUFriend
//
// Copyright © 2017 Vanilla. All rights reserved.
//

#include <Headers/kern_api.hpp>

#include "CPUFriend.hpp"

static const char * binList[] {
"/System/Library/Extensions/IOPlatformPluginFamily.kext/Contents/PlugIns/X86PlatformPlugin.kext/Contents/MacOS/X86PlatformPlugin"
};

static const char * idList[] {
"com.apple.driver.X86PlatformPlugin"
};

static const char * symbolList[] {
"__ZN17X86PlatformPlugin22configResourceCallbackEjiPKvjPv"
};

static KernelPatcher::KextInfo kextList[] {
{ idList[0], & binList[0], arrsize(binList), { false, false }, {}, KernelPatcher::KextInfo::Unloaded }
};

static constexpr size_t kextListSize = arrsize(kextList);

static CPUFriendPlugin * callbackCpuf = nullptr;

OSDefineMetaClassAndStructors(CPUFriendPlatform, IOService)

IOService * CPUFriendPlatform::probe(IOService * provider, SInt32 * score) {
if (provider) {
if (callbackCpuf) {
if (! callbackCpuf->frequencyData) {
auto name = provider->getName();
if (! name)
name = "(null)";
DBGLOG("probe", "looking for cf-frequency-data in %s", name);

auto data = OSDynamicCast(OSData, provider->getProperty("cf-frequency-data"));
if (! data) {
auto cpu = provider->getParentEntry(gIOServicePlane);
if (cpu) {
name = cpu->getName();
if (! name)
name = "(null)";
DBGLOG("probe", "looking for cf-frequency-data in %s", name);
data = OSDynamicCast(OSData, cpu->getProperty("cf-frequency-data"));
} else {
SYSLOG("probe", "unable to access cpu parent");
}
}

if (data) {
callbackCpuf->frequencyDataSize = data->getLength();
callbackCpuf->frequencyData = data->getBytesNoCopy();
} else {
SYSLOG("probe", "failed to obtain cf-frequency-data");
}
}
} else
SYSLOG("probe", "missing storage instance");
}

return nullptr;
}

bool CPUFriendPlugin::init() {
callbackCpuf = this;

LiluAPI::Error error = lilu.onKextLoad(kextList, kextListSize, [](void * user, KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size) {
static_cast<CPUFriendPlugin * >(user)->processKext(patcher, index, address, size);
}, this);

if (error != LiluAPI::Error::NoError) {
SYSLOG("init", "failed to register onKextLoad method %d", error);
return false;
}

return true;
}

void CPUFriendPlugin::myConfigResourceCallback(uint32_t requestTag, kern_return_t result, const void * resourceData, uint32_t resourceDataLength, void * context) {
if (callbackCpuf && callbackCpuf->orgConfigLoadCallback) {
auto data = callbackCpuf->frequencyData;
auto sz = callbackCpuf->frequencyDataSize;
if (data && sz > 0) {
DBGLOG("myConfigResourceCallback", "feeding frequency data %u", sz);
resourceData = data;
resourceDataLength = sz;
result = kOSReturnSuccess;
} else {
SYSLOG("myConfigResourceCallback", "failed to feed cpu data (%u, %d)", sz, data != nullptr);
}
callbackCpuf->orgConfigLoadCallback(requestTag, result, resourceData, resourceDataLength, context);
} else {
SYSLOG("myConfigResourceCallback", "config callback arrived at nowhere");
}
}

void CPUFriendPlugin::processKext(KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size) {
if (progressState != ProcessingState::EverythingDone) {
for (size_t i = 0; i < kextListSize; i++) {
if (kextList[i].loadIndex == index) {
DBGLOG("processKext", "current kext is %s progressState %d", kextList[i].id, progressState);
// clear error from the very beginning just in case
patcher.clearError();
if (! strcmp(kextList[i].id, idList[0])) {
auto callback = patcher.solveSymbol(index, symbolList[0]);
if (callback) {
orgConfigLoadCallback = reinterpret_cast<t_callback>(patcher.routeFunction(callback, reinterpret_cast<mach_vm_address_t>(myConfigResourceCallback), true));
if (patcher.getError() == KernelPatcher::Error::NoError) {
DBGLOG("processKext", "routed %s", symbolList[0]);
} else {
SYSLOG("processKext", "failed to route %s", symbolList[0]);
}
} else {
SYSLOG("processKext", "failed to find %s", symbolList[0]);
}
progressState |= ProcessingState::CallbackRouted;
}
}
}
}
// Ignore all the errors for other processors
patcher.clearError();
}
73 changes: 73 additions & 0 deletions CPUFriend/CPUFriend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// CPUFriend.hpp
// CPUFriend
//
// Copyright © 2017 Vanilla. All rights reserved.
//

#ifndef kern_cpuf_hpp
#define kern_cpuf_hpp

#include <Headers/kern_patcher.hpp>
#include <Library/LegacyIOService.h>

class EXPORT CPUFriendPlatform : public IOService {
OSDeclareDefaultStructors(CPUFriendPlatform)
public:
IOService * probe(IOService * provider, SInt32 *score) override;
};

class CPUFriendPlugin {
public:
bool init();

/**
* ResourceLoad callback type
*/
using t_callback = void (*)(uint32_t, kern_return_t, const void *, uint32_t, void *);

/**
* Trampolines for original resource load callback
*/
t_callback orgConfigLoadCallback = nullptr;

/**
* Loaded user-specified frequency data
*/
const void * frequencyData = nullptr;

/**
* Loaded user-specified frequency data size
*/
uint32_t frequencyDataSize = 0;

private:
/**
* Hooked ResourceLoad callback returning user-specified platform data
*/
static void myConfigResourceCallback(uint32_t requestTag, kern_return_t result, const void * resourceData, uint32_t resourceDataLength, void * context);

/**
* Patch kext if needed and prepare other patches
*
* @param patcher KernelPatcher instance
* @param index kinfo handle
* @param address kinfo load address
* @param size kinfo memory size
*/
void processKext(KernelPatcher & patcher, size_t index, mach_vm_address_t address, size_t size);

/**
* Current progress mask
*/
struct ProcessingState {
enum : uint32_t {
NothingReady = 0,
CallbackRouted = 1,
EverythingDone = CallbackRouted
};
};
uint32_t progressState = ProcessingState::NothingReady;
};

#endif /* kern_cpuf_hpp */
Loading

0 comments on commit c093745

Please sign in to comment.