Skip to content

Commit

Permalink
macOS: buildTopology in kernel now fills output buffer directly
Browse files Browse the repository at this point in the history
Previously, buildTopology would allocate a buffer, fill it, and copy
it to the output buffer.  The buffer passed from userland is of the
same type that the kernel needs, and it is easier to fill it directly.
Also added comment clarifying need to keep types in user/kernel land
in sync.
  • Loading branch information
nealsid committed Sep 14, 2022
1 parent d7dd17b commit b363f43
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
26 changes: 3 additions & 23 deletions src/MacMSRDriver/PcmMsr/PcmMsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,12 @@ IOReturn PcmMsrDriverClassName::writeMSR(pcm_msr_data_t* idata){

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))
{
return kIOReturnBadArgument;
}

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

if (topologies == nullptr)
{
return kIOReturnNoMemory;
if (odata == nullptr) {
return kIOReturnBadArgument;
}

mp_rendezvous_no_intrs(cpuGetTopoData, (void*)topologies);

for(uint32_t i = 0; i < num_cores && i < input_num_cores; i++)
{
odata[i].core_id = topologies[i].core_id;
odata[i].os_id = topologies[i].os_id;
odata[i].socket = topologies[i].socket;
}
mp_rendezvous_no_intrs(cpuGetTopoData, (void*)odata);

IOFreeAligned(topologies, topologyBufferSize);
return kIOReturnSuccess;
}

Expand Down
7 changes: 5 additions & 2 deletions src/MacMSRDriver/PcmMsr/UserKernelShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ typedef struct {
char padding[115];
} k_pcm_msr_data_t;

// The topologyEntry struct that is used by PCM
// The topologyEntry struct that is used by PCM in the kernel. It
// needs to be kept in sync with the one in cpucounters.h (at least,
// the first 3 fields). Ideally we would just include that, but
// cpucounters.h has dependencies on the platform SDK and cannot be
// compiled in the kernel on macOS today.
typedef struct
{
int32_t os_id;
Expand All @@ -30,7 +34,6 @@ typedef struct
int32_t socket;
int32_t native_cpu_model;
int32_t core_type; // This is an enum in the userland structure.
int32_t padding;
} topologyEntry;

enum {
Expand Down

0 comments on commit b363f43

Please sign in to comment.