@@ -40,7 +40,7 @@ inline void WRMSR(uint32_t msr, uint64_t value)
40
40
41
41
void cpuReadMSR (void * pIData){
42
42
pcm_msr_data_t * data = (pcm_msr_data_t *)pIData;
43
- volatile uint cpu = cpu_number ();
43
+ int cpu = cpu_number ();
44
44
if (data->cpu_num == cpu)
45
45
{
46
46
data->value = RDMSR (data->msr_num );
@@ -49,7 +49,7 @@ void cpuReadMSR(void* pIData){
49
49
50
50
void cpuWriteMSR (void * pIDatas){
51
51
pcm_msr_data_t * idatas = (pcm_msr_data_t *)pIDatas;
52
- volatile uint cpu = cpu_number ();
52
+ int cpu = cpu_number ();
53
53
if (idatas->cpu_num == cpu)
54
54
{
55
55
WRMSR (idatas->msr_num , idatas->value );
@@ -58,7 +58,7 @@ void cpuWriteMSR(void* pIDatas){
58
58
59
59
void cpuGetTopoData (void * pTopos){
60
60
topologyEntry* entries = (topologyEntry*)pTopos;
61
- volatile uint cpu = cpu_number ();
61
+ int cpu = cpu_number ();
62
62
int info[4 ];
63
63
entries[cpu].os_id = cpu;
64
64
cpuid (0xB , 1 , info[0 ], info[1 ], info[2 ], info[3 ]);
@@ -86,50 +86,34 @@ bool PcmMsrDriverClassName::start(IOService* provider){
86
86
87
87
return success;
88
88
}
89
- uint32_t PcmMsrDriverClassName::getNumCores ()
89
+
90
+ int32_t PcmMsrDriverClassName::getNumCores ()
90
91
{
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 ))
95
95
{
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 ;
111
99
}
112
- return ret;
100
+
101
+ return ncpus;
113
102
}
114
103
115
104
bool PcmMsrDriverClassName::init (OSDictionary *dict)
116
105
{
117
- num_cores = getNumCores ();
118
106
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 ();
123
110
}
124
- return (result && topologies && num_cores != 0 );
111
+
112
+ return result && num_cores;
125
113
}
126
114
127
115
void PcmMsrDriverClassName::free ()
128
116
{
129
- if (topologies)
130
- {
131
- IOFreeAligned (topologies, sizeof (topologyEntry)*num_cores);
132
- }
133
117
super::free ();
134
118
}
135
119
@@ -182,14 +166,34 @@ IOReturn PcmMsrDriverClassName::writeMSR(pcm_msr_data_t* idata){
182
166
return ret;
183
167
}
184
168
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
+
186
187
mp_rendezvous_no_intrs (cpuGetTopoData, (void *)topologies);
188
+
187
189
for (uint32_t i = 0 ; i < num_cores && i < input_num_cores; i++)
188
190
{
189
191
odata[i].core_id = topologies[i].core_id ;
190
192
odata[i].os_id = topologies[i].os_id ;
191
193
odata[i].socket = topologies[i].socket ;
192
194
}
195
+
196
+ IOFreeAligned (topologies, topologyBufferSize);
193
197
return kIOReturnSuccess ;
194
198
}
195
199
0 commit comments