Skip to content

Commit

Permalink
Merge pull request #447 from nealsid/driver-interface-update
Browse files Browse the repository at this point in the history
Changes to DriverInterface
  • Loading branch information
opcm authored Sep 22, 2022
2 parents d7dd17b + c311546 commit 9e1f80b
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/MacMSRDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")

file(GLOB LIB_FILES DriverInterface.c PCIDriverInterface.cpp MSRAccessor.cpp)
file(GLOB LIB_FILES PCIDriverInterface.cpp MSRAccessor.cpp)

find_library(IOKIT_LIBRARY IOKit)
add_library(PcmMsr SHARED ${LIB_FILES})
Expand Down
64 changes: 0 additions & 64 deletions src/MacMSRDriver/DriverInterface.c

This file was deleted.

18 changes: 0 additions & 18 deletions src/MacMSRDriver/DriverInterface.h

This file was deleted.

156 changes: 110 additions & 46 deletions src/MacMSRDriver/MSRAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,163 @@
//
#include "MSRAccessor.h"
#include <exception>
MSRAccessor::MSRAccessor(){
#include <iostream>
#include <iomanip>

using namespace std;

MSRAccessor::MSRAccessor()
{
service = IOServiceGetMatchingService(kIOMainPortDefault,
IOServiceMatching(kPcmMsrDriverClassName));
openConnection();
}

int32_t MSRAccessor::buildTopology(uint32_t num_cores ,void* pTopos){
topologyEntry *entries = (topologyEntry*)pTopos;
size_t size = sizeof(topologyEntry)*num_cores;
kern_return_t ret = getTopologyInfo(connect, entries, &size);
int32_t MSRAccessor::buildTopology(uint32_t num_cores, void* pTopos)
{
size_t topology_struct_size = sizeof(TopologyEntry)*num_cores;

kern_return_t ret = IOConnectCallStructMethod(connect, kBuildTopology,
NULL, 0,
pTopos, &topology_struct_size);
return (ret == KERN_SUCCESS) ? 0 : -1;
}

int32_t MSRAccessor::read(uint32_t core_num, uint64_t msr_num, uint64_t * value){
int32_t MSRAccessor::read(uint32_t core_num, uint64_t msr_num, uint64_t * value)
{
pcm_msr_data_t idatas, odatas;
size_t size = sizeof(pcm_msr_data_t);

size_t struct_size = sizeof(pcm_msr_data_t);
idatas.msr_num = (uint32_t)msr_num;
idatas.cpu_num = core_num;
kern_return_t ret = readMSR(connect, &idatas, &size, &odatas, &size);

kern_return_t ret = IOConnectCallStructMethod(connect, kReadMSR,
&idatas, struct_size,
&odatas, &struct_size);

if(ret == KERN_SUCCESS)
{
*value = odatas.value;
return sizeof(uint64_t);
}
else{
} else {
return -1;
}
}

int32_t MSRAccessor::write(uint32_t core_num, uint64_t msr_num, uint64_t value){
pcm_msr_data_t idatas;
size_t size = sizeof(pcm_msr_data_t);

idatas.value = value;
idatas.msr_num = (uint32_t)msr_num;
idatas.cpu_num = core_num;
kern_return_t ret = writeMSR(connect, &idatas, &size);

kern_return_t ret = IOConnectCallStructMethod(connect, kWriteMSR,
&idatas, sizeof(pcm_msr_data_t),
NULL, NULL);

if(ret == KERN_SUCCESS)
{
return sizeof(uint64_t);
}
else
{
} else {
return -1;
}
}

uint32_t MSRAccessor::getNumInstances(){
uint32_t num_instances;
getNumClients(connect, &num_instances);
return num_instances;
uint32_t MSRAccessor::getNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;

kernResult = IOConnectCallScalarMethod(connect,
kGetNumInstances,
NULL, 0,
&knum_insts, &output_count);

if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << endl;
}
// TODO add error handling; also, number-of-instance related
// functions may go away as they do not appear to be used.
return knum_insts;
}

uint32_t MSRAccessor::incrementNumInstances(){
uint32_t num_instances;
incrementNumClients(connect, &num_instances);
return num_instances;
uint32_t MSRAccessor::incrementNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;

kernResult = IOConnectCallScalarMethod(connect,
kIncrementNumInstances,
NULL, 0,
&knum_insts, &output_count);

if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << endl;
}
// TODO add error handling; also, these functions may go away as
// they do not appear to be used.
return knum_insts;
}

uint32_t MSRAccessor::decrementNumInstances(){
uint32_t num_instances;
decrementNumClients(connect, &num_instances);
return num_instances;
uint32_t MSRAccessor::decrementNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;

kernResult = IOConnectCallScalarMethod(connect, kDecrementNumInstances,
NULL, 0,
&knum_insts, &output_count);

if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << endl;
}
// TODO add error handling; also, these functions may go away as
// they do not appear to be used.
return knum_insts;
}

MSRAccessor::~MSRAccessor(){
MSRAccessor::~MSRAccessor()
{
closeConnection();
}

kern_return_t MSRAccessor::openConnection(){
kern_return_t MSRAccessor::openConnection()
{
kern_return_t kernResult = IOServiceOpen(service, mach_task_self(), 0, &connect);

if (kernResult != KERN_SUCCESS) {
fprintf(stderr, "IOServiceOpen returned 0x%08x\n", kernResult);
}
else {
kernResult = openMSRClient(connect);

if (kernResult != KERN_SUCCESS) {
fprintf(stderr, "openClient returned 0x%08x.\n\n", kernResult);

if (kernResult != KERN_SUCCESS)
{
cerr << "IOServiceOpen returned 0x" << hex << setw(8) << kernResult << endl;
} else {
kernResult = IOConnectCallScalarMethod(connect, kOpenDriver, NULL, 0, NULL, NULL);

if (kernResult != KERN_SUCCESS)
{
cerr << "kOpenDriver returned 0x" << hex << setw(8) << kernResult << endl;
}
}

return kernResult;
}

void MSRAccessor::closeConnection(){
kern_return_t kernResult = closeMSRClient(connect);
if (kernResult != KERN_SUCCESS) {
fprintf(stderr, "closeClient returned 0x%08x.\n\n", kernResult);
void MSRAccessor::closeConnection()
{
kern_return_t kernResult = IOConnectCallScalarMethod(connect, kCloseDriver,
NULL, 0, NULL, NULL);
if (kernResult != KERN_SUCCESS)
{
cerr << "kCloseDriver returned 0x" << hex << setw(8) << kernResult << endl;
}

kernResult = IOServiceClose(connect);
if (kernResult != KERN_SUCCESS) {
fprintf(stderr, "IOServiceClose returned 0x%08x\n\n", kernResult);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOServiceClose returned 0x" << hex << setw(8) << kernResult << endl;
}
}
8 changes: 4 additions & 4 deletions src/MacMSRDriver/MSRAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Copyright (c) 2012, Intel Corporation
// written by Austen Ott
//

#include <IOKit/IOKitLib.h>
extern "C" {
#include "DriverInterface.h"
}
#include "PcmMsr/UserKernelShared.h"

class MSRAccessor{
class MSRAccessor
{
private:
io_service_t service;
io_connect_t connect;
Expand Down
10 changes: 5 additions & 5 deletions src/MacMSRDriver/PcmMsr/PcmMsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void cpuWriteMSR(void* pIDatas){
}

void cpuGetTopoData(void* pTopos){
topologyEntry* entries = (topologyEntry*)pTopos;
TopologyEntry* entries = (TopologyEntry*)pTopos;
int cpu = cpu_number();
int info[4];
entries[cpu].os_id = cpu;
Expand Down Expand Up @@ -166,18 +166,18 @@ IOReturn PcmMsrDriverClassName::writeMSR(pcm_msr_data_t* idata){
return ret;
}

IOReturn PcmMsrDriverClassName::buildTopology(topologyEntry* odata, uint32_t input_num_cores)
IOReturn PcmMsrDriverClassName::buildTopology(TopologyEntry* odata, uint32_t input_num_cores)
{
size_t topologyBufferSize;

// TODO figure out when input_num_cores is used rather than num_cores
if (os_mul_overflow(sizeof(topologyEntry), (size_t) num_cores, &topologyBufferSize))
if (os_mul_overflow(sizeof(TopologyEntry), (size_t) num_cores, &topologyBufferSize))
{
return kIOReturnBadArgument;
}

topologyEntry *topologies =
(topologyEntry *)IOMallocAligned(topologyBufferSize, 32);
TopologyEntry *topologies =
(TopologyEntry *)IOMallocAligned(topologyBufferSize, 32);

if (topologies == nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MacMSRDriver/PcmMsr/PcmMsr.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PcmMsrDriverClassName : public IOService

virtual IOReturn writeMSR(pcm_msr_data_t* data);
virtual IOReturn readMSR(pcm_msr_data_t* idata,pcm_msr_data_t* odata);
virtual IOReturn buildTopology(topologyEntry* odata, uint32_t input_num_cores);
virtual IOReturn buildTopology(TopologyEntry* odata, uint32_t input_num_cores);
virtual bool init(OSDictionary *dict) override;
virtual void free(void) override;
virtual bool handleOpen(IOService* forClient, IOOptionBits opts, void* args) override;
Expand Down
6 changes: 3 additions & 3 deletions src/MacMSRDriver/PcmMsr/PcmMsrClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ IOReturn PcmMsrClientClassName::writeMSR(pcm_msr_data_t* data)
}

IOReturn PcmMsrClientClassName::sBuildTopology(PcmMsrClientClassName* target, void* reference, IOExternalMethodArguments* args){
return target -> buildTopology((topologyEntry*)args->structureOutput, args->structureOutputSize);
return target -> buildTopology((TopologyEntry*)args->structureOutput, args->structureOutputSize);
}

IOReturn PcmMsrClientClassName::buildTopology(topologyEntry* data, size_t output_size)
IOReturn PcmMsrClientClassName::buildTopology(TopologyEntry* data, size_t output_size)
{
uint32_t num_cores = (uint32_t) (output_size / sizeof(topologyEntry) );
uint32_t num_cores = (uint32_t) (output_size / sizeof(TopologyEntry) );
IOReturn result = checkActiveAndOpened (__FUNCTION__);

if (result == kIOReturnSuccess)
Expand Down
2 changes: 1 addition & 1 deletion src/MacMSRDriver/PcmMsr/PcmMsrClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PcmMsrClientClassName : public IOUserClient
virtual IOReturn writeMSR(pcm_msr_data_t* data);

static IOReturn sBuildTopology(PcmMsrClientClassName* target, void* reference, IOExternalMethodArguments* args);
virtual IOReturn buildTopology(topologyEntry* data, size_t output_size);
virtual IOReturn buildTopology(TopologyEntry* data, size_t output_size);

static IOReturn sGetNumInstances(PcmMsrClientClassName* target, void* reference, IOExternalMethodArguments* args);
virtual IOReturn getNumInstances(uint32_t* num_insts);
Expand Down
Loading

0 comments on commit 9e1f80b

Please sign in to comment.