forked from mh0rst/turionpowercontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuPrimitives.cpp
293 lines (239 loc) · 5.39 KB
/
cpuPrimitives.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <sys/types.h>
#include "cpuPrimitives.h"
#include <time.h>
BOOL Cpuid(DWORD index, PDWORD eax, PDWORD ebx, PDWORD ecx, PDWORD edx)
{
char cpuid_filename[128];
DWORD data[4];
int fd;
sprintf(cpuid_filename, "/dev/cpu/0/cpuid");
fd = open(cpuid_filename, O_RDONLY);
if ( fd < 0 )
{
if ( errno == ENXIO )
{
fprintf(stderr, "cpuid: No CPUID on processor 0\n");
return false;
}
else if (errno == EIO )
{
fprintf(stderr, "cpuid: CPU 0 doesn't support CPUID\n");
return false;
}
else
{
perror("cpuid:open");
return false;
}
}
if ( pread(fd, &data, sizeof data, index) != sizeof data )
{
perror("cpuid:pread");
return false;
}
*eax=data[0];
*ebx=data[1];
*ecx=data[2];
*edx=data[3];
close(fd);
return true;
}
BOOL ReadPciConfigDwordEx(DWORD pciAddress, DWORD regAddress, PDWORD value)
{
char pcidev_filename[128];
int fd;
DWORD data;
DWORD bus, device, function;
bus=(pciAddress >> 8) & 0xff;
device=(pciAddress >> 3) & 0x1f;
function=pciAddress & 0x7;
sprintf(pcidev_filename, "/proc/bus/pci/%02x/%02x.%x",bus,device,function);
fd = open(pcidev_filename, O_RDONLY);
if ( fd < 0 )
{
if ( errno == ENXIO )
{
fprintf(stderr, "ReadPciConfigDwordEx: ENXIO error\n");
return false;
}
else if (errno == EIO )
{
fprintf(stderr, "ReadPciConfigDwordEx: EIO error\n");
return false;
}
else
{
perror("ReadPciConfigDwordEx: open");
return false;
}
}
if ( pread(fd, &data, sizeof data, regAddress) != sizeof data )
{
perror("ReadPciConfigDwordEx: pread");
return false;
}
*value = data;
close(fd);
return true;
}
BOOL WritePciConfigDwordEx(DWORD pciAddress, DWORD regAddress, DWORD value)
{
char pcidev_filename[128];
int fd;
DWORD data;
DWORD bus, device, function;
bus=(pciAddress >> 8) & 0xff;
device=(pciAddress >> 3) & 0x1f;
function=pciAddress & 0x7;
sprintf(pcidev_filename, "/proc/bus/pci/%02x/%02x.%x",bus,device,function);
fd = open(pcidev_filename, O_WRONLY);
if ( fd < 0 )
{
if ( errno == ENXIO )
{
fprintf(stderr, "WritePciConfigDwordEx: ENXIO error\n");
return false;
}
else if (errno == EIO )
{
fprintf(stderr, "WritePciConfigDwordEx: EIO error\n");
return false;
}
else
{
perror("WritePciConfigDwordEx: open");
return false;
}
}
data=value;
if ( pwrite(fd, &data, sizeof data, regAddress) != sizeof data )
{
perror("WritePciConfigDwordEx: pwrite");
return false;
}
close(fd);
return true;
}
BOOL RdmsrPx(DWORD index, PDWORD eax, PDWORD edx, DWORD_PTR processAffinityMask)
{
char msr_filename[128];
DWORD data[2];
int fd;
DWORD processor=0;
while (processAffinityMask)
{
if (processAffinityMask & 1)
{
sprintf(msr_filename, "/dev/cpu/%d/msr",processor);
fd = open(msr_filename, O_RDONLY);
if ( fd < 0 ) {
if ( errno == ENXIO ) {
fprintf(stderr, "RdmsrPx: Invalid %u processor\n",processor);
return false;
} else if (errno == EIO ) {
fprintf(stderr, "RdmsrPx: CPU %u doesn't support MSR\n",processor);
return false;
} else {
perror("RdmsrPx: open");
return false;
}
}
if ( pread(fd, &data, sizeof data, index) != sizeof data )
{
perror("rdmsr: pread");
return false;
}
*eax=data[0];
*edx=data[1];
close(fd);
//This is intended because this procedure can't report more than
//one CPU MSR, so we will report the first processor in the mask
//discarding the others.
//In the write flavour of this procedure, it is useful to try
//for all processors in the mask.
return true;
}
processor++;
processAffinityMask >>= 1;
}
return true;
}
BOOL Rdmsr(DWORD index, PDWORD eax, PDWORD edx)
{
return RdmsrPx(index, eax, edx, 0x1);
}
BOOL WrmsrPx(DWORD index, DWORD eax, DWORD edx, DWORD_PTR processAffinityMask)
{
char msr_filename[128];
DWORD data[2];
int fd;
DWORD processor=0;
// printf ("Mask: %x\n", processAffinityMask);
while (processAffinityMask) {
if (processAffinityMask & 1) {
// printf ("processor %d is valid\n", processor);
data[0]=eax;
data[1]=edx;
sprintf(msr_filename, "/dev/cpu/%u/msr",processor);
fd = open(msr_filename, O_WRONLY);
if ( fd < 0 )
{
if ( errno == ENXIO )
{
fprintf(stderr, "WrmsrPx: Invalid %u processor\n",processor);
return false;
}
else if (errno == EIO )
{
fprintf(stderr, "WrmsrPx: CPU %u doesn't support MSR\n",processor);
return false;
}
else
{
fprintf(stderr, "WrmsrPx: open");
return false;
}
}
if ( pwrite(fd, &data, sizeof data, index) != sizeof data)
{
if (errno == EIO)
fprintf(stderr, "wrmsr: CPU %d cannot set MSR %X to %X %X\n", processor, index, data[0], data[1]);
else
fprintf(stderr, "WrmsrPx pread Errno %x\n",errno);
close(fd);
return false;
}
close(fd);
}
processor++;
processAffinityMask >>= 1;
}
return true;
}
BOOL Wrmsr(DWORD index, DWORD eax, DWORD edx)
{
return WrmsrPx(index, eax, edx, 0x1);
}
void Sleep (DWORD ms) {
usleep (ms*1000);
return;
}
int GetTickCount ()
{
#ifndef CLOCK_MONOTONIC_HR
#define CLOCK_MONOTONIC_HR CLOCK_MONOTONIC
#endif
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC_HR, &tp))
return -1;
tp.tv_sec *= 1000;
tp.tv_nsec /= 1000000;
tp.tv_sec += tp.tv_nsec;
return tp.tv_sec;
}