From 7b4d5a4d594be8885a9171db60df42ffdb379f38 Mon Sep 17 00:00:00 2001 From: Haoyan Qi Date: Fri, 21 Feb 2020 20:07:42 +0800 Subject: [PATCH] Initial Commit --- .gitignore | 3 + SMCAMDProcessor.xcodeproj/project.pbxproj | 347 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcschemes/xcschememanagement.plist | 14 + SMCAMDProcessor/Info.plist | 72 ++++ SMCAMDProcessor/KeyImplementations.hpp | 34 ++ SMCAMDProcessor/Keyimplementations.cpp | 33 ++ SMCAMDProcessor/SMCAMDProcessor.cpp | 306 +++++++++++++++ SMCAMDProcessor/SMCAMDProcessor.hpp | 159 ++++++++ SMCAMDProcessor/SMCAMDProcessorUserClient.cpp | 113 ++++++ SMCAMDProcessor/SMCAMDProcessorUserClient.hpp | 42 +++ 11 files changed, 1130 insertions(+) create mode 100644 .gitignore create mode 100644 SMCAMDProcessor.xcodeproj/project.pbxproj create mode 100644 SMCAMDProcessor.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 SMCAMDProcessor.xcodeproj/xcuserdata/qihaoyan.xcuserdatad/xcschemes/xcschememanagement.plist create mode 100644 SMCAMDProcessor/Info.plist create mode 100644 SMCAMDProcessor/KeyImplementations.hpp create mode 100644 SMCAMDProcessor/Keyimplementations.cpp create mode 100644 SMCAMDProcessor/SMCAMDProcessor.cpp create mode 100644 SMCAMDProcessor/SMCAMDProcessor.hpp create mode 100644 SMCAMDProcessor/SMCAMDProcessorUserClient.cpp create mode 100644 SMCAMDProcessor/SMCAMDProcessorUserClient.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc9897c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ + +Lilu.kext/ +VirtualSMC.kext/ \ No newline at end of file diff --git a/SMCAMDProcessor.xcodeproj/project.pbxproj b/SMCAMDProcessor.xcodeproj/project.pbxproj new file mode 100644 index 0000000..9843b15 --- /dev/null +++ b/SMCAMDProcessor.xcodeproj/project.pbxproj @@ -0,0 +1,347 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + B57D280723F66C8E002BC699 /* SMCAMDProcessor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B57D280123F66C8E002BC699 /* SMCAMDProcessor.cpp */; }; + B57D280823F66C8E002BC699 /* Keyimplementations.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B57D280223F66C8E002BC699 /* Keyimplementations.cpp */; }; + B57D280923F66C8E002BC699 /* SMCAMDProcessor.hpp in Headers */ = {isa = PBXBuildFile; fileRef = B57D280323F66C8E002BC699 /* SMCAMDProcessor.hpp */; }; + B57D280A23F66C8E002BC699 /* KeyImplementations.hpp in Headers */ = {isa = PBXBuildFile; fileRef = B57D280423F66C8E002BC699 /* KeyImplementations.hpp */; }; + B57D280B23F66C8E002BC699 /* SMCAMDProcessorUserClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B57D280523F66C8E002BC699 /* SMCAMDProcessorUserClient.cpp */; }; + B57D280C23F66C8E002BC699 /* SMCAMDProcessorUserClient.hpp in Headers */ = {isa = PBXBuildFile; fileRef = B57D280623F66C8E002BC699 /* SMCAMDProcessorUserClient.hpp */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + B57D27F423F66AE7002BC699 /* SMCAMDProcessor.kext */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SMCAMDProcessor.kext; sourceTree = BUILT_PRODUCTS_DIR; }; + B57D27FB23F66AE7002BC699 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + B57D280123F66C8E002BC699 /* SMCAMDProcessor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SMCAMDProcessor.cpp; sourceTree = ""; }; + B57D280223F66C8E002BC699 /* Keyimplementations.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Keyimplementations.cpp; sourceTree = ""; }; + B57D280323F66C8E002BC699 /* SMCAMDProcessor.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SMCAMDProcessor.hpp; sourceTree = ""; }; + B57D280423F66C8E002BC699 /* KeyImplementations.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = KeyImplementations.hpp; sourceTree = ""; }; + B57D280523F66C8E002BC699 /* SMCAMDProcessorUserClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SMCAMDProcessorUserClient.cpp; sourceTree = ""; }; + B57D280623F66C8E002BC699 /* SMCAMDProcessorUserClient.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = SMCAMDProcessorUserClient.hpp; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + B57D27F123F66AE7002BC699 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + B57D27EA23F66AE7002BC699 = { + isa = PBXGroup; + children = ( + B57D27F623F66AE7002BC699 /* SMCAMDProcessor */, + B57D27F523F66AE7002BC699 /* Products */, + ); + sourceTree = ""; + }; + B57D27F523F66AE7002BC699 /* Products */ = { + isa = PBXGroup; + children = ( + B57D27F423F66AE7002BC699 /* SMCAMDProcessor.kext */, + ); + name = Products; + sourceTree = ""; + }; + B57D27F623F66AE7002BC699 /* SMCAMDProcessor */ = { + isa = PBXGroup; + children = ( + B57D280123F66C8E002BC699 /* SMCAMDProcessor.cpp */, + B57D280323F66C8E002BC699 /* SMCAMDProcessor.hpp */, + B57D280523F66C8E002BC699 /* SMCAMDProcessorUserClient.cpp */, + B57D280623F66C8E002BC699 /* SMCAMDProcessorUserClient.hpp */, + B57D280223F66C8E002BC699 /* Keyimplementations.cpp */, + B57D280423F66C8E002BC699 /* KeyImplementations.hpp */, + B57D27FB23F66AE7002BC699 /* Info.plist */, + ); + path = SMCAMDProcessor; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + B57D27EF23F66AE7002BC699 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + B57D280C23F66C8E002BC699 /* SMCAMDProcessorUserClient.hpp in Headers */, + B57D280A23F66C8E002BC699 /* KeyImplementations.hpp in Headers */, + B57D280923F66C8E002BC699 /* SMCAMDProcessor.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + B57D27F323F66AE7002BC699 /* SMCAMDProcessor */ = { + isa = PBXNativeTarget; + buildConfigurationList = B57D27FE23F66AE7002BC699 /* Build configuration list for PBXNativeTarget "SMCAMDProcessor" */; + buildPhases = ( + B57D27EF23F66AE7002BC699 /* Headers */, + B57D27F023F66AE7002BC699 /* Sources */, + B57D27F123F66AE7002BC699 /* Frameworks */, + B57D27F223F66AE7002BC699 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SMCAMDProcessor; + productName = SMCAMDProcessor; + productReference = B57D27F423F66AE7002BC699 /* SMCAMDProcessor.kext */; + productType = "com.apple.product-type.kernel-extension"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + B57D27EB23F66AE7002BC699 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 1130; + ORGANIZATIONNAME = trulyspinach; + TargetAttributes = { + B57D27F323F66AE7002BC699 = { + CreatedOnToolsVersion = 11.3.1; + }; + }; + }; + buildConfigurationList = B57D27EE23F66AE7002BC699 /* Build configuration list for PBXProject "SMCAMDProcessor" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = B57D27EA23F66AE7002BC699; + productRefGroup = B57D27F523F66AE7002BC699 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + B57D27F323F66AE7002BC699 /* SMCAMDProcessor */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + B57D27F223F66AE7002BC699 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + B57D27F023F66AE7002BC699 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + B57D280B23F66C8E002BC699 /* SMCAMDProcessorUserClient.cpp in Sources */, + B57D280723F66C8E002BC699 /* SMCAMDProcessor.cpp in Sources */, + B57D280823F66C8E002BC699 /* Keyimplementations.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + B57D27FC23F66AE7002BC699 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + B57D27FD23F66AE7002BC699 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.15; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + }; + name = Release; + }; + B57D27FF23F66AE7002BC699 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Manual; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1.0.0d1; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + "MODULE_VERSION=$(MODULE_VERSION)", + "PRODUCT_NAME=$(PRODUCT_NAME)", + ); + HEADER_SEARCH_PATHS = ( + "${PROJECT_DIR}/Lilu.kext/Contents/Resources", + "${PROJECT_DIR}/VirtualSMC.kext/Contents/Resources", + ); + INFOPLIST_FILE = SMCAMDProcessor/Info.plist; + LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/Lilu.kext/Contents/Resources/Library"; + MODULE_NAME = wtf.spinach.SMCAMDProcessor; + MODULE_VERSION = 1.0.0d1; + OTHER_CPLUSPLUSFLAGS = "-Wno-inconsistent-missing-override"; + OTHER_LDFLAGS = "-static"; + PRODUCT_BUNDLE_IDENTIFIER = wtf.spinach.SMCAMDProcessor; + PRODUCT_NAME = "$(TARGET_NAME)"; + WRAPPER_EXTENSION = kext; + }; + name = Debug; + }; + B57D280023F66AE7002BC699 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Manual; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1.0.0d1; + GCC_PREPROCESSOR_DEFINITIONS = ( + "PRODUCT_NAME=$(PRODUCT_NAME)", + "MODULE_VERSION=$(MODULE_VERSION)", + ); + HEADER_SEARCH_PATHS = ( + "${PROJECT_DIR}/Lilu.kext/Contents/Resources", + "${PROJECT_DIR}/VirtualSMC.kext/Contents/Resources", + ); + INFOPLIST_FILE = SMCAMDProcessor/Info.plist; + LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/Lilu.kext/Contents/Resources/Library"; + MODULE_NAME = wtf.spinach.SMCAMDProcessor; + MODULE_VERSION = 1.0.0d1; + OTHER_CPLUSPLUSFLAGS = "-Wno-inconsistent-missing-override"; + OTHER_LDFLAGS = "-static"; + PRODUCT_BUNDLE_IDENTIFIER = wtf.spinach.SMCAMDProcessor; + PRODUCT_NAME = "$(TARGET_NAME)"; + WRAPPER_EXTENSION = kext; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + B57D27EE23F66AE7002BC699 /* Build configuration list for PBXProject "SMCAMDProcessor" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B57D27FC23F66AE7002BC699 /* Debug */, + B57D27FD23F66AE7002BC699 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + B57D27FE23F66AE7002BC699 /* Build configuration list for PBXNativeTarget "SMCAMDProcessor" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + B57D27FF23F66AE7002BC699 /* Debug */, + B57D280023F66AE7002BC699 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = B57D27EB23F66AE7002BC699 /* Project object */; +} diff --git a/SMCAMDProcessor.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/SMCAMDProcessor.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..5e70afb --- /dev/null +++ b/SMCAMDProcessor.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/SMCAMDProcessor.xcodeproj/xcuserdata/qihaoyan.xcuserdatad/xcschemes/xcschememanagement.plist b/SMCAMDProcessor.xcodeproj/xcuserdata/qihaoyan.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..e4ce224 --- /dev/null +++ b/SMCAMDProcessor.xcodeproj/xcuserdata/qihaoyan.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + SMCAMDProcessor.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/SMCAMDProcessor/Info.plist b/SMCAMDProcessor/Info.plist new file mode 100644 index 0000000..d4353c1 --- /dev/null +++ b/SMCAMDProcessor/Info.plist @@ -0,0 +1,72 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + KEXT + CFBundleShortVersionString + $(MODULE_VERSION) + CFBundleVersion + $(MODULE_VERSION) + IOKitPersonalities + + wtf.spinach.SMCAMDProcessor + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + IOClass + $(PRODUCT_NAME) + IOMatchCategory + $(PRODUCT_NAME) + IONameMatch + processor + IOPropertyMatch + + processor-index + 0 + + IOProviderClass + IOACPIPlatformDevice + IOResourceMatch + ACPI + IOUserClientClass + SMCAMDProcessorUserClient + + + NSHumanReadableCopyright + Copyright © 2020 trulyspinach. All rights reserved. + OSBundleLibraries + + as.vit9696.Lilu + 1.3.9 + as.vit9696.VirtualSMC + 1.0.0 + com.apple.iokit.IOPCIFamily + 2.9 + com.apple.kpi.bsd + 12.0.0 + com.apple.kpi.dsep + 12.0.0 + com.apple.kpi.iokit + 19.3 + com.apple.kpi.libkern + 19.3 + com.apple.kpi.mach + 12.0.0 + com.apple.kpi.unsupported + 19.3 + + OSBundleRequired + Root + + diff --git a/SMCAMDProcessor/KeyImplementations.hpp b/SMCAMDProcessor/KeyImplementations.hpp new file mode 100644 index 0000000..ea6c1e3 --- /dev/null +++ b/SMCAMDProcessor/KeyImplementations.hpp @@ -0,0 +1,34 @@ +// +// KeyImplementations.hpp +// SMCAMDProcessor +// +// Created by Qi HaoYan on 2/12/20. +// Copyright © 2020 Qi HaoYan. All rights reserved. +// + +#ifndef KeyImplementations_hpp +#define KeyImplementations_hpp + +#include +#include "SMCAMDProcessor.hpp" + +class SMCAMDProcessor; + + +class AMDSupportVsmcValue : public VirtualSMCValue { +protected: + SMCAMDProcessor *provider; + size_t package; + size_t core; +public: + AMDSupportVsmcValue(SMCAMDProcessor *provider, size_t package, size_t core=0) : provider(provider), package(package), core(core) {} +}; + + +class TempPackage : public AMDSupportVsmcValue { using AMDSupportVsmcValue::AMDSupportVsmcValue; protected: SMC_RESULT readAccess() override; }; +class TempCore : public AMDSupportVsmcValue { using AMDSupportVsmcValue::AMDSupportVsmcValue; protected: SMC_RESULT readAccess() override; }; + +class EnegryPackage: public AMDSupportVsmcValue +{ using AMDSupportVsmcValue::AMDSupportVsmcValue; protected: SMC_RESULT readAccess() override; }; + +#endif /* KeyImplementations_hpp */ diff --git a/SMCAMDProcessor/Keyimplementations.cpp b/SMCAMDProcessor/Keyimplementations.cpp new file mode 100644 index 0000000..7ceb770 --- /dev/null +++ b/SMCAMDProcessor/Keyimplementations.cpp @@ -0,0 +1,33 @@ +// +// NewNewLaLa.cpp +// AMDCPUSupport +// +// Created by Qi HaoYan on 2/12/20. +// Copyright © 2020 Qi HaoYan. All rights reserved. +// + +#include "KeyImplementations.hpp" + + +SMC_RESULT TempPackage::readAccess() { + uint16_t *ptr = reinterpret_cast(data); + *ptr = VirtualSMCAPI::encodeSp(type, (double)provider->PACKAGE_TEMPERATURE_perPackage[0]); + + return SmcSuccess; +} + +SMC_RESULT TempCore::readAccess() { + uint16_t *ptr = reinterpret_cast(data); + *ptr = VirtualSMCAPI::encodeSp(type, (double)provider->PACKAGE_TEMPERATURE_perPackage[0]); + + return SmcSuccess; +} + +SMC_RESULT EnegryPackage::readAccess(){ + if (type == SmcKeyTypeFloat) + *reinterpret_cast(data) = VirtualSMCAPI::encodeFlt(provider->uniPackageEnegry); + else + *reinterpret_cast(data) = VirtualSMCAPI::encodeSp(type, provider->uniPackageEnegry); + + return SmcSuccess; +} diff --git a/SMCAMDProcessor/SMCAMDProcessor.cpp b/SMCAMDProcessor/SMCAMDProcessor.cpp new file mode 100644 index 0000000..bdc8245 --- /dev/null +++ b/SMCAMDProcessor/SMCAMDProcessor.cpp @@ -0,0 +1,306 @@ +#include "SMCAMDProcessor.hpp" + + +OSDefineMetaClassAndStructors(SMCAMDProcessor, IOService); + +bool ADDPR(debugEnabled) = false; +uint32_t ADDPR(debugPrintDelay) = 0; + +bool SMCAMDProcessor::init(OSDictionary *dictionary){ + + IOLog("AMDCPUSupport got inited !!!!!!!!!!!\n"); + + return IOService::init(dictionary); +} + +void SMCAMDProcessor::free(){ + IOService::free(); +} + +bool SMCAMDProcessor::setupKeysVsmc(){ + + vsmcNotifier = VirtualSMCAPI::registerHandler(vsmcNotificationHandler, this); + + + bool suc = true; + + suc &= VirtualSMCAPI::addKey(KeyTCxD(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxE(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxF(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxG(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78)); + suc &= VirtualSMCAPI::addKey(KeyTCxH(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxJ(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78)); + suc &= VirtualSMCAPI::addKey(KeyTCxP(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxT(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyTCxp(0), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempPackage(this, 0))); + + + suc &= VirtualSMCAPI::addKey(KeyPCPR, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyPCPT, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); + suc &= VirtualSMCAPI::addKey(KeyPCTR, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); + + +// VirtualSMCAPI::addKey(KeyPC0C, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPC0R, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPCAM, vsmcPlugin.data, VirtualSMCAPI::valueWithFlt(0, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPCPC, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); +// +// VirtualSMCAPI::addKey(KeyPC0G, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPCGC, vsmcPlugin.data, VirtualSMCAPI::valueWithFlt(0, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPCGM, vsmcPlugin.data, VirtualSMCAPI::valueWithFlt(0, new EnegryPackage(this, 0))); +// VirtualSMCAPI::addKey(KeyPCPG, vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp96, new EnegryPackage(this, 0))); + + //Since AMD cpu dont have temperature MSR for each core, we simply report the same package temperature for all cores. +// for(int core = 0; core < totalNumberOfPhysicalCores; core++){ +// VirtualSMCAPI::addKey(KeyTCxC(core), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempCore(this, 0, core))); +// VirtualSMCAPI::addKey(KeyTCxc(core), vsmcPlugin.data, VirtualSMCAPI::valueWithSp(0, SmcKeyTypeSp78, new TempCore(this, 0, core))); +// } + + if(!suc){ + IOLog("AMDCPUSupport::setupKeysVsmc: VirtualSMCAPI::addKey returned false. \n"); + } + + return suc; +} + +bool SMCAMDProcessor::vsmcNotificationHandler(void *sensors, void *refCon, IOService *vsmc, IONotifier *notifier) { + if (sensors && vsmc) { + IOLog("AMDCPUSupport: got vsmc notification\n"); + auto &plugin = static_cast(sensors)->vsmcPlugin; + auto ret = vsmc->callPlatformFunction(VirtualSMCAPI::SubmitPlugin, true, sensors, &plugin, nullptr, nullptr); + if (ret == kIOReturnSuccess) { + IOLog("AMDCPUSupport: submitted plugin\n"); + return true; + } else if (ret != kIOReturnUnsupported) { + IOLog("AMDCPUSupport: plugin submission failure %X\n", ret); + } else { + IOLog("AMDCPUSupport: plugin submission to non vsmc\n"); + } + } else { + IOLog("AMDCPUSupport: got null vsmc notification\n"); + } + return false; +} + + +bool SMCAMDProcessor::getPCIService(){ + + + OSDictionary *matching_dict = serviceMatching("IOPCIDevice"); + if(!matching_dict){ + IOLog("AMDCPUSupport::getPCIService: serviceMatching unable to generate matching dictonary.\n"); + return false; + } + + //Wait for PCI services to init. + waitForMatchingService(matching_dict); + + OSIterator *service_iter = getMatchingServices(matching_dict); + IOPCIDevice *service = 0; + + if(!service_iter){ + IOLog("AMDCPUSupport::getPCIService: unable to find a matching IOPCIDevice.\n"); + return false; + } + + while (true){ + OSObject *obj = service_iter->getNextObject(); + if(!obj) break; + + service = OSDynamicCast(IOPCIDevice, obj); + break; + } + + if(!service){ + IOLog("AMDCPUSupport::getPCIService: unable to get IOPCIDevice on host system.\n"); + return false; + } + + IOLog("AMDCPUSupport::getPCIService: succeed!\n"); + fIOPCIDevice = service; + + return true; +} + + +bool SMCAMDProcessor::start(IOService *provider){ + + bool success = IOService::start(provider); + if(!success){ + IOLog("AMDCPUSupport::start failed to start. :(\n"); + return false; + } + registerService(); + + cpuGeneration = CPUInfo::getGeneration(&cpuFamily, &cpuModel, &cpuStepping); + + uint32_t cpuid_eax = 0; + uint32_t cpuid_ebx = 0; + uint32_t cpuid_ecx = 0; + uint32_t cpuid_edx = 0; + CPUInfo::getCpuid(0, 0, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx); + IOLog("AMDCPUSupport::start got CPUID: %X %X %X %X\n", cpuid_eax, cpuid_ebx, cpuid_ecx, cpuid_edx); + + if(cpuid_ebx != CPUInfo::signature_AMD_ebx + || cpuid_ecx != CPUInfo::signature_AMD_ecx + || cpuid_edx != CPUInfo::signature_AMD_edx){ + IOLog("AMDCPUSupport::start no AMD signature detected, failing..\n"); + + return false; + } + + workLoop = IOWorkLoop::workLoop(); + timerEventSource = IOTimerEventSource::timerEventSource(this, [](OSObject *object, IOTimerEventSource *sender) { + SMCAMDProcessor *provider = OSDynamicCast(SMCAMDProcessor, object); + + + mp_rendezvous_no_intrs([](void *obj) { + auto provider = static_cast(obj); + + //Read current clock speed from MSR for each core + provider->updateClockSpeed(); + }, provider); + + //Read stats from package. + provider->updatePackageTemp(); + provider->updatePackageEnegry(); + + provider->timerEventSource->setTimeoutMS(1000); + }); + + if(!CPUInfo::getCpuTopology(cpuTopology)){ + IOLog("AMDCPUSupport::start unable to get CPU Topology.\n"); + } + IOLog("AMDCPUSupport::start got %hhu CPU(s): Physical Count: %hhu, Logical Count %hhu.\n", + cpuTopology.packageCount, cpuTopology.totalPhysical(), cpuTopology.totalLogical()); + + totalNumberOfPhysicalCores = cpuTopology.totalPhysical(); + + + IOLog("AMDCPUSupport::start trying to init PCI service...\n"); + if(!getPCIService()){ + IOLog("AMDCPUSupport::start no PCI support found, failing...\n"); + return false; + } + + + lastUpdateTime = getCurrentTimeNs(); + + workLoop->addEventSource(timerEventSource); + timerEventSource->setTimeoutMS(1000); + + IOLog("AMDCPUSupport::start registering VirtualSMC keys...\n"); + setupKeysVsmc(); + + return success; +} + +void SMCAMDProcessor::stop(IOService *provider){ + IOLog("AMDCPUSupport stopped, you have no more support :(\n"); + + timerEventSource->cancelTimeout(); + + IOService::stop(provider); +} + +bool SMCAMDProcessor::read_msr(uint32_t addr, uint64_t *value){ + + uint32_t lo, hi; +// IOLog("AMDCPUSupport lalala \n"); + int err = rdmsr_carefully(addr, &lo, &hi); +// IOLog("AMDCPUSupport rdmsr_carefully %d\n", err); + + if(!err) *value = lo | ((uint64_t)hi << 32); + + return err == 0; +} + +void SMCAMDProcessor::updateClockSpeed(){ + + uint32_t cpu_num = cpu_number(); + + // Ignore hyper-threaded cores + uint8_t package = cpuTopology.numberToPackage[cpu_num]; + uint8_t logical = cpuTopology.numberToLogical[cpu_num]; + if (logical >= cpuTopology.physicalCount[package]) + return; + + uint8_t physical = cpuTopology.numberToPhysicalUnique(cpu_num); + + uint64_t msr_value_buf = 0; + bool err = !read_msr(kMSR_HARDWARE_PSTATE_STATUS, &msr_value_buf); + if(err) IOLog("AMDCPUSupport::updateClockSpeed: failed somewhere"); + +// IOLog("AMDCPUSupport::updateClockSpeed: i am CPU %hhu, physical %hhu\n", package, physical); + + MSR_HARDWARE_PSTATE_STATUS_perCore[physical] = msr_value_buf; +} + +void SMCAMDProcessor::updatePackageTemp(){ + + IOPCIAddressSpace space; + space.bits = 0x00; + + fIOPCIDevice->configWrite32(space, (UInt8)kFAMILY_17H_PCI_CONTROL_REGISTER, (UInt32)kF17H_M01H_THM_TCON_CUR_TMP); + uint32_t v = fIOPCIDevice->configRead32(space, kFAMILY_17H_PCI_CONTROL_REGISTER + 4); + v = (v >> 21) * 125; + float temperature = v * 0.001f; + + PACKAGE_TEMPERATURE_perPackage[0] = temperature; +// IOLog("AMDCPUSupport::updatePackageTemp: read from pci device %d \n", (int)PACKAGE_TEMPERATURE_perPackage[0]); +} + +void SMCAMDProcessor::updatePackageEnegry(){ + + uint64_t time = getCurrentTimeNs(); + + uint64_t msr_value_buf = 0; + read_msr(kMSR_PKG_ENERGY_STAT, &msr_value_buf); + + uint32_t enegryValue = (uint32_t)(msr_value_buf & 0xffffffff); + + uint64_t enegryDelta = (lastUpdateEnegryValue <= enegryValue) ? + enegryValue - lastUpdateEnegryValue : UINT64_MAX - lastUpdateEnegryValue; + + double e = (0.0000153 * enegryDelta) / ((time - lastUpdateTime) / 1000000000.0); + uniPackageEnegry = e; + + lastUpdateEnegryValue = enegryValue; + lastUpdateTime = time; + +// IOLog("AMDCPUSupport::updatePackageEnegry: %d \n", (int)e); +// IOLog("AMDCPUSupport::updatePackageEnegry: %d la\n", (int)enegryDelta); + +} + +EXPORT extern "C" kern_return_t ADDPR(kern_start)(kmod_info_t *, void *) { + // Report success but actually do not start and let I/O Kit unload us. + // This works better and increases boot speed in some cases. + PE_parse_boot_argn("liludelay", &ADDPR(debugPrintDelay), sizeof(ADDPR(debugPrintDelay))); + ADDPR(debugEnabled) = checkKernelArgument("-amdcpudbg"); + return KERN_SUCCESS; +} + +EXPORT extern "C" kern_return_t ADDPR(kern_stop)(kmod_info_t *, void *) { + // It is not safe to unload VirtualSMC plugins! + return KERN_FAILURE; +} + + +#ifdef __MAC_10_15 + +// macOS 10.15 adds Dispatch function to all OSObject instances and basically +// every header is now incompatible with 10.14 and earlier. +// Here we add a stub to permit older macOS versions to link. +// Note, this is done in both kern_util and plugin_start as plugins will not link +// to Lilu weak exports from vtable. + +kern_return_t WEAKFUNC PRIVATE OSObject::Dispatch(const IORPC rpc) { + PANIC("util", "OSObject::Dispatch smcproc stub called"); +} + +kern_return_t WEAKFUNC PRIVATE OSMetaClassBase::Dispatch(const IORPC rpc) { + PANIC("util", "OSMetaClassBase::Dispatch smcproc stub called"); +} + +#endif diff --git a/SMCAMDProcessor/SMCAMDProcessor.hpp b/SMCAMDProcessor/SMCAMDProcessor.hpp new file mode 100644 index 0000000..a2d9648 --- /dev/null +++ b/SMCAMDProcessor/SMCAMDProcessor.hpp @@ -0,0 +1,159 @@ +#ifndef SMCAMDProcessor_h +#define SMCAMDProcessor_h + + +#include +#include + +#include +#include + +#include +#include + + +#include +#include +#include + +#include +#include + +#include "KeyImplementations.hpp" + + +extern "C" { + int cpu_number(void); + void mp_rendezvous_no_intrs(void (*action_func)(void *), void *arg); +}; + + + +class SMCAMDProcessor : public IOService { + OSDeclareDefaultStructors(SMCAMDProcessor) + + /** + * VirtualSMC service registration notifier + */ + IONotifier *vsmcNotifier {nullptr}; + + static bool vsmcNotificationHandler(void *sensors, void *refCon, IOService *vsmc, IONotifier *notifier); + + /** + * Registered plugin instance + */ + VirtualSMCAPI::Plugin vsmcPlugin { + xStringify(PRODUCT_NAME), + parseModuleVersion(xStringify(MODULE_VERSION)), + VirtualSMCAPI::Version, + }; + + + /** + * MSRs supported by AMD 17h CPU from: + * https://github.com/LibreHardwareMonitor/LibreHardwareMonitor/blob/master/LibreHardwareMonitorLib/Hardware/Cpu/Amd17Cpu.cs + */ + static constexpr uint32_t kCOFVID_STATUS = 0xC0010071; + static constexpr uint32_t k17H_M01H_SVI = 0x0005A000; + static constexpr uint32_t kF17H_M01H_THM_TCON_CUR_TMP = 0x00059800; + static constexpr uint32_t kF17H_M70H_CCD1_TEMP = 0x00059954; + static constexpr uint32_t kF17H_TEMP_OFFSET_FLAG = 0x80000; + static constexpr uint8_t kFAMILY_17H_PCI_CONTROL_REGISTER = 0x60; + static constexpr uint32_t kHWCR = 0xC0010015; + static constexpr uint32_t kMSR_CORE_ENERGY_STAT = 0xC001029A; + static constexpr uint32_t kMSR_HARDWARE_PSTATE_STATUS = 0xC0010293; + static constexpr uint32_t kMSR_PKG_ENERGY_STAT = 0xC001029B; + static constexpr uint32_t kMSR_PSTATE_0 = 0xC0010064; + static constexpr uint32_t kMSR_PWR_UNIT = 0xC0010299; + static constexpr uint32_t kPERF_CTL_0 = 0xC0010000; + static constexpr uint32_t kPERF_CTR_0 = 0xC0010004; + + + /** + * Key name index mapping + */ + static constexpr size_t MaxIndexCount = sizeof("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") - 1; + static constexpr const char *KeyIndexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + + /** + * Supported SMC keys + */ + static constexpr SMC_KEY KeyPC0C = SMC_MAKE_IDENTIFIER('P','C','0','C'); + static constexpr SMC_KEY KeyPC0G = SMC_MAKE_IDENTIFIER('P','C','0','G'); + static constexpr SMC_KEY KeyPC0R = SMC_MAKE_IDENTIFIER('P','C','0','R'); + static constexpr SMC_KEY KeyPC3C = SMC_MAKE_IDENTIFIER('P','C','3','C'); + static constexpr SMC_KEY KeyPCAC = SMC_MAKE_IDENTIFIER('P','C','A','C'); + static constexpr SMC_KEY KeyPCAM = SMC_MAKE_IDENTIFIER('P','C','A','M'); + static constexpr SMC_KEY KeyPCEC = SMC_MAKE_IDENTIFIER('P','C','E','C'); + static constexpr SMC_KEY KeyPCGC = SMC_MAKE_IDENTIFIER('P','C','G','C'); + static constexpr SMC_KEY KeyPCGM = SMC_MAKE_IDENTIFIER('P','C','G','M'); + static constexpr SMC_KEY KeyPCPC = SMC_MAKE_IDENTIFIER('P','C','P','C'); + static constexpr SMC_KEY KeyPCPG = SMC_MAKE_IDENTIFIER('P','C','P','G'); + static constexpr SMC_KEY KeyPCPR = SMC_MAKE_IDENTIFIER('P','C','P','R'); + static constexpr SMC_KEY KeyPCPT = SMC_MAKE_IDENTIFIER('P','C','P','T'); + static constexpr SMC_KEY KeyPCTR = SMC_MAKE_IDENTIFIER('P','C','T','R'); + static constexpr SMC_KEY KeyTCxD(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'D'); } + static constexpr SMC_KEY KeyTCxE(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'E'); } + static constexpr SMC_KEY KeyTCxF(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'F'); } + static constexpr SMC_KEY KeyTCxG(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'G'); } + static constexpr SMC_KEY KeyTCxJ(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'J'); } + static constexpr SMC_KEY KeyTCxH(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'H'); } + static constexpr SMC_KEY KeyTCxP(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'P'); } + static constexpr SMC_KEY KeyTCxT(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'T'); } + static constexpr SMC_KEY KeyTCxp(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'p'); } + + static constexpr SMC_KEY KeyTCxC(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'C'); } + static constexpr SMC_KEY KeyTCxc(size_t i) { return SMC_MAKE_IDENTIFIER('T','C',KeyIndexes[i],'c'); } + +public: + virtual bool init(OSDictionary *dictionary = 0) override; + virtual void free(void) override; + + virtual bool start(IOService *provider) override; + virtual void stop(IOService *provider) override; + + + /** + * A simple wrapper for the kernel function readmsr_carefully. + */ + bool read_msr(uint32_t addr, uint64_t *value); + + void updateClockSpeed(); + void updatePackageTemp(); + void updatePackageEnegry(); + + uint32_t totalNumberOfPhysicalCores; + + + /** + * Hard allocate space for cached readings. + */ + uint64_t MSR_HARDWARE_PSTATE_STATUS_perCore[24] {}; + float PACKAGE_TEMPERATURE_perPackage[CPUInfo::MaxCpus]; + + uint64_t lastUpdateTime; + uint64_t lastUpdateEnegryValue; + + double uniPackageEnegry; + + +private: + + IOWorkLoop *workLoop; + IOTimerEventSource *timerEventSource; + + + CPUInfo::CpuGeneration cpuGeneration {CPUInfo::CpuGeneration::Unknown}; + + uint32_t cpuFamily {0}, cpuModel {0}, cpuStepping {0}; + + CPUInfo::CpuTopology cpuTopology {}; + + IOPCIDevice *fIOPCIDevice; + + bool setupKeysVsmc(); + bool getPCIService(); + +}; +#endif diff --git a/SMCAMDProcessor/SMCAMDProcessorUserClient.cpp b/SMCAMDProcessor/SMCAMDProcessorUserClient.cpp new file mode 100644 index 0000000..8b0e3df --- /dev/null +++ b/SMCAMDProcessor/SMCAMDProcessorUserClient.cpp @@ -0,0 +1,113 @@ +// +// SMCAMDProcessorUserClient.cpp +// SMCAMDProcessor +// +// Created by Qi HaoYan on 2/4/20. +// Copyright © 2020 Qi HaoYan. All rights reserved. +// + +#include "SMCAMDProcessorUserClient.hpp" + + + +OSDefineMetaClassAndStructors(SMCAMDProcessorUserClient, IOUserClient); + + +bool SMCAMDProcessorUserClient::start(IOService *provider){ + + IOLog("AMDCPUSupportUserClient::start\n"); + + bool success = IOService::start(provider); + + if(success){ + fProvider = OSDynamicCast(SMCAMDProcessor, provider); + } + + return success; +} + +void SMCAMDProcessorUserClient::stop(IOService *provider){ + IOLog("AMDCPUSupportUserClient::stop\n"); + + fProvider = nullptr; + IOService::stop(provider); +} + +uint64_t multiply_two_numbers(uint64_t number_one, uint64_t number_two){ + uint64_t number_three = 0; + for(uint32_t i = 0; i < number_two; i++){ + number_three = number_three + number_one; + } + return number_three; +} + +IOReturn SMCAMDProcessorUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments *arguments, + IOExternalMethodDispatch *dispatch, OSObject *target, void *reference){ + + IOLog("AMDCPUSupportUserClient::externalMethod selector:%d\n", selector); + + + switch (selector) { + case 0: { + // multiply_two_numbers + uint64_t r = multiply_two_numbers(arguments->scalarInput[0], arguments->scalarInput[1]); + arguments->scalarOutput[0] = r; + arguments->scalarOutputCount = 1; + + IOLog("AMDCPUSupportUserClient::multiply_two_numbers r:%llu\n", r); + + break; + } + case 1: { + // read_msr +// IOLog("AMDCPUSupportUserClient::read_msr: got raw address %llu\n", arguments->scalarInput[0]); + uint32_t msr_addr = (uint32_t)(arguments->scalarInput[0]); + uint64_t msr_value_buf = 0; + bool err = !fProvider->read_msr(msr_addr, &msr_value_buf); + if(err){ + IOLog("AMDCPUSupportUserClient::read_msr: failed at address %u\n", msr_addr); + } else { + arguments->scalarOutput[0] = msr_value_buf; + arguments->scalarOutputCount = 1; + } + + + break; + } + + case 2: { + + uint32_t numPhyCores = fProvider->totalNumberOfPhysicalCores; + + arguments->scalarOutputCount = 1; + arguments->scalarOutput[0] = numPhyCores; + + arguments->structureOutputSize = numPhyCores * sizeof(uint64_t); + + uint64_t *dataOut = (uint64_t*) arguments->structureOutput; + + for(uint32_t i = 0; i < numPhyCores; i++){ + dataOut[i] = fProvider->MSR_HARDWARE_PSTATE_STATUS_perCore[i]; + } + + break; + } + + case 3: { + arguments->scalarOutputCount = 0; + + arguments->structureOutputSize = 1 * sizeof(float); + + float *dataOut = (float*) arguments->structureOutput; + dataOut[0] = fProvider->PACKAGE_TEMPERATURE_perPackage[0]; + break; + } + + default: { + IOLog("AMDCPUSupportUserClient::externalMethod: invalid method.\n"); + break; + } + } + + return kIOReturnSuccess; +} diff --git a/SMCAMDProcessor/SMCAMDProcessorUserClient.hpp b/SMCAMDProcessor/SMCAMDProcessorUserClient.hpp new file mode 100644 index 0000000..393f609 --- /dev/null +++ b/SMCAMDProcessor/SMCAMDProcessorUserClient.hpp @@ -0,0 +1,42 @@ +// +// SMCAMDProcessorUserClient.hpp +// SMCAMDProcessor +// +// Created by Qi HaoYan on 2/4/20. +// Copyright © 2020 Qi HaoYan. All rights reserved. +// + +#ifndef SMCAMDProcessorUserClient_hpp +#define SMCAMDProcessorUserClient_hpp + + +#include +#include +#include + +#include "SMCAMDProcessor.hpp" + +class SMCAMDProcessorUserClient : public IOUserClient +{ + + OSDeclareDefaultStructors(SMCAMDProcessorUserClient) + + +public: + // IOUserClient methods + virtual void stop(IOService* provider) override; + virtual bool start(IOService* provider) override; + + + +protected: + + SMCAMDProcessor *fProvider; + + // KPI for supporting access from both 32-bit and 64-bit user processes beginning with Mac OS X 10.5. + virtual IOReturn externalMethod(uint32_t selector, IOExternalMethodArguments* arguments, + IOExternalMethodDispatch* dispatch, OSObject* target, void* reference) override; + +}; + +#endif