Skip to content

Commit adce380

Browse files
authored
Merge pull request #436 from nealsid/client-build-and-topology-structure-mismatch-build-topology-stack-allocation
Remove of use of volatile and make memory allocation only for the lif…
2 parents c022f7a + 3ce12eb commit adce380

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ latex/
3131
*.vcxproj.user
3232
.vs/
3333
.idea/
34+
build
35+
src/simdjson

src/MacMSRDriver/PcmMsr/PcmMsr.cpp

+39-35
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ inline void WRMSR(uint32_t msr, uint64_t value)
4040

4141
void 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

5050
void 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

5959
void 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

115104
bool 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

127115
void 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

src/MacMSRDriver/PcmMsr/PcmMsr.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PcmMsrDriverClassName : public IOService
2121
virtual bool handleIsOpen(const IOService* forClient) const override;
2222
virtual void handleClose(IOService* forClient, IOOptionBits opts) override;
2323

24-
virtual uint32_t getNumCores();
24+
virtual int32_t getNumCores();
2525

2626
virtual IOReturn incrementNumInstances(uint32_t* num_instances);
2727
virtual IOReturn decrementNumInstances(uint32_t* num_instances);
@@ -36,8 +36,7 @@ class PcmMsrDriverClassName : public IOService
3636
private:
3737
// number of providers currently using the driver
3838
uint32_t num_clients = 0;
39-
uint32_t num_cores;
40-
topologyEntry *topologies;
39+
int32_t num_cores;
4140
};
4241

4342
#ifdef DEBUG

src/MacMSRDriver/PcmMsr/UserKernelShared.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ typedef struct {
2323
// The topologyEntry struct that is used by PCM
2424
typedef struct
2525
{
26-
uint32_t os_id;
27-
uint32_t thread_id;
28-
uint32_t core_id;
29-
uint32_t tile_id;
30-
uint32_t socket;
31-
uint32_t native_cpu_model;
32-
uint32_t core_type; // This is an enum in the userland structure.
33-
uint32_t padding;
26+
int32_t os_id;
27+
int32_t thread_id;
28+
int32_t core_id;
29+
int32_t tile_id;
30+
int32_t socket;
31+
int32_t native_cpu_model;
32+
int32_t core_type; // This is an enum in the userland structure.
33+
int32_t padding;
3434
} topologyEntry;
3535

3636
enum {

0 commit comments

Comments
 (0)