From b363f434cb9e41f214ff864df32c88d77c3c5d0a Mon Sep 17 00:00:00 2001 From: Neal Sidhwaney Date: Mon, 12 Sep 2022 13:15:54 -0700 Subject: [PATCH] macOS: buildTopology in kernel now fills output buffer directly 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. --- src/MacMSRDriver/PcmMsr/PcmMsr.cpp | 26 +++------------------- src/MacMSRDriver/PcmMsr/UserKernelShared.h | 7 ++++-- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/src/MacMSRDriver/PcmMsr/PcmMsr.cpp b/src/MacMSRDriver/PcmMsr/PcmMsr.cpp index 61678add..df651e5d 100644 --- a/src/MacMSRDriver/PcmMsr/PcmMsr.cpp +++ b/src/MacMSRDriver/PcmMsr/PcmMsr.cpp @@ -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; } diff --git a/src/MacMSRDriver/PcmMsr/UserKernelShared.h b/src/MacMSRDriver/PcmMsr/UserKernelShared.h index b10b863c..6cd55a46 100644 --- a/src/MacMSRDriver/PcmMsr/UserKernelShared.h +++ b/src/MacMSRDriver/PcmMsr/UserKernelShared.h @@ -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; @@ -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 {