@@ -40,7 +40,7 @@ inline void WRMSR(uint32_t msr, uint64_t value)
4040
4141void cpuReadMSR (void * pIData){
4242 pcm_msr_data_t * data = (pcm_msr_data_t *)pIData;
43- volatile uint cpu = cpu_number ();
43+ int cpu = cpu_number ();
4444 if (data->cpu_num == cpu)
4545 {
4646 data->value = RDMSR (data->msr_num );
@@ -49,7 +49,7 @@ void cpuReadMSR(void* pIData){
4949
5050void cpuWriteMSR (void * pIDatas){
5151 pcm_msr_data_t * idatas = (pcm_msr_data_t *)pIDatas;
52- volatile uint cpu = cpu_number ();
52+ int cpu = cpu_number ();
5353 if (idatas->cpu_num == cpu)
5454 {
5555 WRMSR (idatas->msr_num , idatas->value );
@@ -58,7 +58,7 @@ void cpuWriteMSR(void* pIDatas){
5858
5959void cpuGetTopoData (void * pTopos){
6060 topologyEntry* entries = (topologyEntry*)pTopos;
61- volatile uint cpu = cpu_number ();
61+ int cpu = cpu_number ();
6262 int info[4 ];
6363 entries[cpu].os_id = cpu;
6464 cpuid (0xB , 1 , info[0 ], info[1 ], info[2 ], info[3 ]);
@@ -86,50 +86,34 @@ bool PcmMsrDriverClassName::start(IOService* provider){
8686
8787 return success;
8888}
89- uint32_t PcmMsrDriverClassName::getNumCores ()
89+
90+ int32_t PcmMsrDriverClassName::getNumCores ()
9091{
91- size_t size;
92- char * pParam;
93- uint32_t ret = 0 ;
94- if (!sysctlbyname (" hw.logicalcpu" , NULL , &size, NULL , 0 ))
92+ int32_t ncpus = 0 ;
93+ size_t ncpus_size = sizeof (ncpus);
94+ if (sysctlbyname (" hw.logicalcpu" , &ncpus, &ncpus_size, NULL , 0 ))
9595 {
96- if (NULL != (pParam = (char *)IOMalloc (size)))
97- {
98- if (!sysctlbyname (" hw.logicalcpu" , (void *)pParam, &size, NULL , 0 ))
99- {
100- if (sizeof (int ) == size)
101- ret = *(int *)pParam;
102- else if (sizeof (long ) == size)
103- ret = (uint32_t ) *(long *)pParam;
104- else if (sizeof (long long ) == size)
105- ret = (uint32_t ) *(long long *)pParam;
106- else
107- ret = *(int *)pParam;
108- }
109- IOFree (pParam, size);
110- }
96+ IOLog (" %s[%p]::%s() -- sysctl failure retrieving hw.logicalcpu" ,
97+ getName (), this , __FUNCTION__);
98+ ncpus = 0 ;
11199 }
112- return ret;
100+
101+ return ncpus;
113102}
114103
115104bool PcmMsrDriverClassName::init (OSDictionary *dict)
116105{
117- num_cores = getNumCores ();
118106 bool result = super::init (dict);
119- topologies = 0 ;
120- if (result && num_cores != 0 )
121- {
122- topologies = (topologyEntry*)IOMallocAligned (sizeof (topologyEntry)*num_cores, 32 );
107+
108+ if (result) {
109+ num_cores = getNumCores ();
123110 }
124- return (result && topologies && num_cores != 0 );
111+
112+ return result && num_cores;
125113}
126114
127115void PcmMsrDriverClassName::free ()
128116{
129- if (topologies)
130- {
131- IOFreeAligned (topologies, sizeof (topologyEntry)*num_cores);
132- }
133117 super::free ();
134118}
135119
@@ -182,14 +166,34 @@ IOReturn PcmMsrDriverClassName::writeMSR(pcm_msr_data_t* idata){
182166 return ret;
183167}
184168
185- IOReturn PcmMsrDriverClassName::buildTopology (topologyEntry* odata, uint32_t input_num_cores){
169+ IOReturn PcmMsrDriverClassName::buildTopology (topologyEntry* odata, uint32_t input_num_cores)
170+ {
171+ size_t topologyBufferSize;
172+
173+ // TODO figure out when input_num_cores is used rather than num_cores
174+ if (os_mul_overflow (sizeof (topologyEntry), (size_t ) num_cores, &topologyBufferSize))
175+ {
176+ return kIOReturnBadArgument ;
177+ }
178+
179+ topologyEntry *topologies =
180+ (topologyEntry *)IOMallocAligned (topologyBufferSize, 32 );
181+
182+ if (topologies == nullptr )
183+ {
184+ return kIOReturnNoMemory ;
185+ }
186+
186187 mp_rendezvous_no_intrs (cpuGetTopoData, (void *)topologies);
188+
187189 for (uint32_t i = 0 ; i < num_cores && i < input_num_cores; i++)
188190 {
189191 odata[i].core_id = topologies[i].core_id ;
190192 odata[i].os_id = topologies[i].os_id ;
191193 odata[i].socket = topologies[i].socket ;
192194 }
195+
196+ IOFreeAligned (topologies, topologyBufferSize);
193197 return kIOReturnSuccess ;
194198}
195199
0 commit comments