Skip to content

Commit

Permalink
r0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
trulyspinach committed Mar 19, 2020
1 parent 3cc76ab commit 308952f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 39 deletions.
4 changes: 4 additions & 0 deletions AMD Power Gadget/Power Tool/PStateEditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ class PStateEditorViewController: NSViewController, NSTableViewDelegate, NSTable
alert.messageText = "Unable to Set PStateDef"
alert.informativeText = """
Action was denied by kernel as current user does not have enough privilege.
Or was canceled by current user.
Run AMD Power Gadget as root user or disable privilege check with boot-arg '-amdpnopchk'
Otherwise, click 'Authorize' to in the warning message to allow this change.
"""
alert.alertStyle = .critical
alert.addButton(withTitle: "Done")
Expand Down
4 changes: 2 additions & 2 deletions AMD Power Gadget/Power Tool/PowerToolViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ class PowerToolViewController: NSViewController, NSWindowDelegate {
\(ProcessorModel.shared.systemConfig["cpu"]!)
Family: \(String(format:"%02X", id[0]))h, Model: \(String(format:"%02X", id[1]))h
Physical: \(id[2]), Logical: \(id[3])
L1(Total): \(id[4] * id[2]) KB, L2(Total): \(id[5] * id[2] / 1024) MB
L3(Shared): \(id[6] / 1024) MB
L1(Total): \(id[4] * id[2]) KB, L2(Total): \(id[5] * id[2] / 1024) MB, L3(Shared): \(id[6] / 1024) MB
Motherboard: \(ProcessorModel.shared.boardName)
\(ProcessorModel.shared.boardVender)
Graphics: \(ProcessorModel.shared.systemConfig["gpu"]!)
Memory: \(memGB)GB, Storage: \(storageGB)GB
macOS Version: \(ProcessorModel.shared.systemConfig["os"]!)
Expand Down
17 changes: 14 additions & 3 deletions AMD Power Gadget/ProcessorModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ProcessorModel {
var boardName : String = "Unknown"
var boardVender : String = "Unknown"

var fetchRetry : Int = 10
var retryTimer : Timer?

init() {
if !initDriver() {
alertAndQuit()
Expand Down Expand Up @@ -376,7 +379,6 @@ class ProcessorModel {
}

func fetchSupportedProcessor() {

let url = URL(string: "https://bot1.spinach.wtf/chksupport")

guard let requestUrl = url else { fatalError() }
Expand All @@ -392,9 +394,18 @@ class ProcessorModel {

request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {return}
if error != nil {
if self.fetchRetry > 0 {
self.fetchRetry -= 1

DispatchQueue.main.asyncAfter(deadline: .now() + 8.0, execute: {
self.fetchSupportedProcessor()
})
}
return
}


if let data = data, let dataString = String(data: data, encoding: .utf8) {
self.cpuListedAsSupported = dataString == "true"
}
Expand Down
12 changes: 6 additions & 6 deletions SMCAMDProcessor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = "wtf.spinach.AMD-Power-Gadget";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
Expand All @@ -364,7 +364,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = "wtf.spinach.AMD-Power-Gadget";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -487,7 +487,7 @@
buildSettings = {
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 0.3.2;
CURRENT_PROJECT_VERSION = 0.3.3;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
Expand All @@ -501,7 +501,7 @@
INFOPLIST_FILE = SMCAMDProcessor/Info.plist;
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/Lilu.kext/Contents/Resources/Library";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.3.2;
MARKETING_VERSION = 0.3.3;
MODULE_NAME = wtf.spinach.SMCAMDProcessor;
MODULE_VERSION = "$(CURRENT_PROJECT_VERSION)";
OTHER_CFLAGS = (
Expand Down Expand Up @@ -531,7 +531,7 @@
buildSettings = {
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 0.3.2;
CURRENT_PROJECT_VERSION = 0.3.3;
GCC_PREPROCESSOR_DEFINITIONS = (
"PRODUCT_NAME=$(PRODUCT_NAME)",
"MODULE_VERSION=$(MODULE_VERSION)",
Expand All @@ -543,7 +543,7 @@
INFOPLIST_FILE = SMCAMDProcessor/Info.plist;
LIBRARY_SEARCH_PATHS = "$(PROJECT_DIR)/Lilu.kext/Contents/Resources/Library";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.3.2;
MARKETING_VERSION = 0.3.3;
MODULE_NAME = wtf.spinach.SMCAMDProcessor;
MODULE_VERSION = "$(CURRENT_PROJECT_VERSION)";
OTHER_CFLAGS = (
Expand Down
74 changes: 52 additions & 22 deletions SMCAMDProcessor/SMCAMDProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@

OSDefineMetaClassAndStructors(SMCAMDProcessor, IOService);

#define TCTL_OFFSET_TABLE_LEN 6
static constexpr const struct tctl_offset tctl_offset_table[] = {
{ 0x17, "AMD Ryzen 5 1600X", 20 },
{ 0x17, "AMD Ryzen 7 1700X", 20 },
{ 0x17, "AMD Ryzen 7 1800X", 20 },
{ 0x17, "AMD Ryzen 7 2700X", 10 },
{ 0x17, "AMD Ryzen Threadripper 19", 27 }, /* 19{00,20,50}X */
{ 0x17, "AMD Ryzen Threadripper 29", 27 }, /* 29{20,50,70,90}[W]X */
};

bool ADDPR(debugEnabled) = false;
uint32_t ADDPR(debugPrintDelay) = 0;

bool SMCAMDProcessor::init(OSDictionary *dictionary){
IOLog("AMDCPUSupport v%s, %u, init\n", kMODULE_VERSION, (uint32_t)strlen(kMODULE_VERSION));
// strcpy((char*)kMODULE_VERSION, xStringify(MODULE_VERSION), (uint32_t)strlen(xStringify(MODULE_VERSION)));
IOLog("AMDCPUSupport v%s, init\n", xStringify(MODULE_VERSION));


return IOService::init(dictionary);
}

Expand Down Expand Up @@ -170,6 +183,27 @@ bool SMCAMDProcessor::start(IOService *provider){
CPUInfo::getCpuid(0x80000007, 0, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx);
cpbSupported = (cpuid_edx >> 9) & 0x1;

uint32_t nameString[12]{};
CPUInfo::getCpuid(0x80000002, 0, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx);
nameString[0] = cpuid_eax; nameString[1] = cpuid_ebx; nameString[2] = cpuid_ecx; nameString[3] = cpuid_edx;
CPUInfo::getCpuid(0x80000003, 0, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx);
nameString[4] = cpuid_eax; nameString[5] = cpuid_ebx; nameString[6] = cpuid_ecx; nameString[7] = cpuid_edx;
CPUInfo::getCpuid(0x80000004, 0, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx);
nameString[8] = cpuid_eax; nameString[9] = cpuid_ebx; nameString[10] = cpuid_ecx; nameString[11] = cpuid_edx;

IOLog("AMDCPUSupport::start Processor: %s))\n", (char*)nameString);

//Check tctl temperature offset
for(int i = 0; i < TCTL_OFFSET_TABLE_LEN; i++){
const TempOffset *to = tctl_offset_table + i;
IOLog("############%s##########\n", to->id);
if(cpuFamily == to->model && strstr((char*)nameString, to->id)){

tempOffset = (float)to->offset;
break;
}
}


if(!CPUInfo::getCpuTopology(cpuTopology)){
IOLog("AMDCPUSupport::start unable to get CPU Topology.\n");
Expand All @@ -183,9 +217,19 @@ bool SMCAMDProcessor::start(IOService *provider){
void *safe_wrmsr = lookup_symbol("_wrmsr_carefully");
if(!safe_wrmsr){
IOLog("AMDCPUSupport::start WARN: Can't find _wrmsr_carefully, proceeding with unsafe wrmsr\n");
} else {
wrmsr_carefully = (int(*)(uint32_t,uint32_t,uint32_t)) safe_wrmsr;
}
wrmsr_carefully = (int(*)(uint32_t,uint32_t,uint32_t)) safe_wrmsr;

void *_kunc_alert = lookup_symbol("_KUNCUserNotificationDisplayAlert");
IOLog("kunc_alert %p\n", kunc_alert);
if(!_kunc_alert){
IOLog("AMDCPUSupport::start WARN: Can't find _KUNCUserNotificationDisplayAlert.\n");
} else {
kunc_alert =
(kern_return_t(*)(int,unsigned,const char*,const char*,const char*,
const char*,const char*,const char*,const char*,const char*,unsigned*))_kunc_alert;
}


auto efiRT = EfiRuntimeServices::get();
Expand Down Expand Up @@ -317,9 +361,9 @@ bool SMCAMDProcessor::start(IOService *provider){
timerEventSource->setTimeoutMS(1);

//
IOLog("AMDCPUSupport::start registering VirtualSMC keys...\n");
setupKeysVsmc();

// IOLog("AMDCPUSupport::start registering VirtualSMC keys...\n");
// setupKeysVsmc();
return success;
}

Expand Down Expand Up @@ -541,26 +585,12 @@ void SMCAMDProcessor::updatePackageTemp(){
bool tempOffsetFlag = (temperature & kF17H_TEMP_OFFSET_FLAG) != 0;
temperature = (temperature >> 21) * 125;

float offset = 0.0f;
float t = temperature * 0.001f;

// Offset table: https://github.com/torvalds/linux/blob/master/drivers/hwmon/k10temp.c#L78
uint32_t totalNumberOfPhysicalCores = cpuTopology.totalPhysical();

if(tempOffsetFlag) {
if (totalNumberOfPhysicalCores == 6) // 1600X,1700X,1800X
offset = -20.0f;
else if (totalNumberOfPhysicalCores == 8) // 2700X
offset = -10.0f;
else if (totalNumberOfPhysicalCores == 12 || totalNumberOfPhysicalCores == 32) // TR1900,2900
offset = -27.0f;

if (tempOffsetFlag)
t += -49.0f;
}
t -= tempOffset;

if (offset < 0)
t += offset;
if (tempOffsetFlag)
t -= 49.0f;


PACKAGE_TEMPERATURE_perPackage[0] = t;
Expand Down
25 changes: 23 additions & 2 deletions SMCAMDProcessor/SMCAMDProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ extern "C" {
};


/**
* Offset table: https://github.com/torvalds/linux/blob/master/drivers/hwmon/k10temp.c#L78
*/
typedef struct tctl_offset {
uint8_t model;
char const *id;
int offset;
} TempOffset;




class SMCAMDProcessor : public IOService {
OSDeclareDefaultStructors(SMCAMDProcessor)
Expand All @@ -70,7 +81,14 @@ class SMCAMDProcessor : public IOService {

public:

static constexpr char const *kMODULE_VERSION = xStringify(MODULE_VERSION);
char kMODULE_VERSION[12]{};








/**
* MSRs supported by AMD 17h CPU from:
Expand Down Expand Up @@ -233,7 +251,8 @@ class SMCAMDProcessor : public IOService {

bool disablePrivilegeCheck = false;


kern_return_t (*kunc_alert)(int,unsigned,const char*,const char*,const char*,
const char*,const char*,const char*,const char*,const char*,unsigned*) {nullptr};

private:

Expand All @@ -249,8 +268,10 @@ class SMCAMDProcessor : public IOService {
uint32_t estimatedRequestTimeInterval = 0;
uint32_t timeOfLastMissedRequest = 0;

float tempOffset = 0;

int (*wrmsr_carefully)(uint32_t, uint32_t, uint32_t) {nullptr};


CPUInfo::CpuTopology cpuTopology {};

Expand Down
23 changes: 20 additions & 3 deletions SMCAMDProcessor/SMCAMDProcessorUserClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ bool SMCAMDProcessorUserClient::initWithTask(task_t owningTask,

token = securityToken;

proc_t proc = get_bsdtask_info(owningTask);
proc_name(proc_pid(proc), taskProcessBinaryName, 32);

return true;

}
Expand Down Expand Up @@ -59,8 +62,22 @@ uint64_t multiply_two_numbers(uint64_t number_one, uint64_t number_two){

bool SMCAMDProcessorUserClient::hasPrivilege(){
if(fProvider->disablePrivilegeCheck) return true;
if(clientHasPrivilege(token, kIOClientPrivilegeAdministrator) == kIOReturnSuccess) return true;


char buf[128];
snprintf(buf, 128,
"A process is trying to make changes to your system.\n\nAffected process name: %s",
taskProcessBinaryName);

unsigned int rf;
(*(fProvider->kunc_alert))(0, 0, NULL, NULL, NULL,
"SMCAMDProcessor", buf, "Deny", "I'm not sure.", "Authorize", &rf);


if(rf == 2) return true;

return clientHasPrivilege(token, kIOClientPrivilegeAdministrator) == kIOReturnSuccess;
return false;
}

IOReturn SMCAMDProcessorUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments *arguments,
Expand Down Expand Up @@ -209,11 +226,11 @@ IOReturn SMCAMDProcessorUserClient::externalMethod(uint32_t selector, IOExternal
case 8: {
arguments->scalarOutputCount = 0;

arguments->structureOutputSize = (uint32_t)strlen(fProvider->kMODULE_VERSION);
arguments->structureOutputSize = (uint32_t)strlen(xStringify(MODULE_VERSION));
char *dataOut = (char*) arguments->structureOutput;

for(uint32_t i = 0; i < arguments->structureOutputSize; i++){
dataOut[i] = fProvider->kMODULE_VERSION[i];
dataOut[i] = xStringify(MODULE_VERSION)[i];
}

break;
Expand Down
12 changes: 11 additions & 1 deletion SMCAMDProcessor/SMCAMDProcessorUserClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
#include <Library/LegacyIOService.h>
#include "LegacyHeaders/LegacyIOUserClient.h"

#include <sys/proc.h>

//#include <IOKit/IOService.h>
//#include <IOKit/IOUserClient.h>
//#include <IOKit/IOLib.h>

#include "SMCAMDProcessor.hpp"

extern "C" {
proc_t get_bsdtask_info(task_t t);
};



class SMCAMDProcessorUserClient : public IOUserClient
{

Expand All @@ -45,7 +53,9 @@ class SMCAMDProcessorUserClient : public IOUserClient
// 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;



char taskProcessBinaryName[32]{};
};

#endif

0 comments on commit 308952f

Please sign in to comment.