-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpu.pas
479 lines (409 loc) Β· 13.7 KB
/
cpu.pas
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
{-----------------------------------
Cpu Identifier implementation
-------------------------------------
Class that encapsulate detail of
getting processor vendor name, brand string
supported features and etc
-------------------------------------
(c) 2017 Zamrony P. Juhara <[email protected]>
http://github.com/zamronypj/cpuid
-------------------------------------}
unit Cpu;
{$mode objfpc}{$H+}
{$asmmode intel}
interface
uses
Classes, SysUtils, CpuInterface;
type
TCPUIDResult = record
eax : cardinal;
ebx : cardinal;
ecx : cardinal;
edx : cardinal;
end;
TCPUVendorId = array [0..2] of cardinal;
TCPUVendorIdStr = array [0..11] of ansichar;
TCPUBasicInfo = record
operation : cardinal;
case boolean of
true : (VendorIdStr : TCPUVendorIdStr);
false: (VendorId : TCPUVendorId);
end;
TCPUBrand = array[0..15] of ansichar;
TCPUBrandString = record
case boolean of
true : (res : TCPUIDResult);
false : (brand : TCPUBrand);
end;
TCPUExtInfo = record
extOperation : cardinal;
moreOperation : cardinal;
extOperationAvail : boolean;
brandStringAvail : boolean;
brandString : string;
end;
{ TCpu }
TCpuIdentifier = class(TInterfacedObject, ICpuIdentifier)
private
function cpuidExec(const operation : cardinal) : TCPUIDResult;
function getCPUBasicInfo() : TCPUBasicInfo;
function getCPUExtInfo() : TCPUExtInfo;
public
function cpuidSupported() : boolean;
function processorName() : string;
function vendorName() : string;
function hasFeature(const feature : string) : boolean;
function family() : byte;
function model() : byte;
function stepping() : byte;
function maximumFrequency() : word;
function baseFrequency() : word;
function busReferenceFrequency() : word;
end;
implementation
{ TCpu }
const
CPUID_BIT = $200000;
CPUID_OPR_BASIC_INFO = 0;
CPUID_OPR_VERSION_FEATURE_INFO = 1;
CPUID_OPR_PROC_FREQUENCY_INFO = $16;
CPUID_OPR_EXTENDED_INFO = $80000000;
CPUID_OPR_EXTENDED_INFO_MORE = $80000001;
CPUID_OPR_BRAND_INFO_AVAIL = $80000004;
CPUID_OPR_BRAND_INFO_0 = $80000002;
CPUID_OPR_BRAND_INFO_1 = $80000003;
CPUID_OPR_BRAND_INFO_2 = $80000004;
{$IFDEF CPU32}
{------------start 32 bit architecture code ----------}
{**
* Test availability of CPUID instruction
*
* @return boolean true if CPUID supported
*}
function TCpuIdentifier.cpuidSupported() : boolean;
var supported:boolean;
begin
asm
push eax
push ecx
//copy EFLAGS -> EAX
pushfd
pop eax
//store original RFLAGS
//so we can restore it later
mov ecx, eax
//change bit 21
xor eax, CPUID_BIT
//copy EAX -> EFLAGS
push eax
popfd
//copy EFLAGS back to EAX
pushfd
pop eax
//CPUID_BIT is reserved bit and cannot be changed
//for 386 processor or lower
//if we can change, it means we run on newer processor
and eax, CPUID_BIT
shr eax, 21
mov supported, al
//restore original EFLAGS
push ecx
popfd
pop ecx
pop eax
end;
result := supported;
end;
{*
* Run CPUID instruction
* @param cardinal operation store code for operation to be performed
*}
function TCpuIdentifier.cpuidExec(const operation : cardinal) : TCPUIDResult;
var tmpEax, tmpEbx, tmpEcx, tmpEdx : cardinal;
begin
asm
push eax
push ebx
push ecx
push edx
mov eax, operation
cpuid
mov tmpEax, eax
mov tmpEbx, ebx
mov tmpEcx, ecx
mov tmpEdx, edx
pop edx
pop ecx
pop ebx
pop eax
end;
result.eax := tmpEax;
result.ebx := tmpEbx;
result.ecx := tmpEcx;
result.edx := tmpEdx;
end;
{------------end 32 bit architecture code ----------}
{$ENDIF}
{$IFDEF CPU64}
{------------start 64 bit architecture code ----------}
{**
* Test availability of CPUID instruction
*
* @return boolean true if CPUID supported
*}
function TCpuIdentifier.cpuidSupported() : boolean;
var supported:boolean;
begin
asm
push rax
push rcx
//copy RFLAGS -> RAX
pushfq
pop rax
//store original RFLAGS
//so we can restore it later
mov rcx, rax
//change bit 21
xor rax, CPUID_BIT
//copy RAX -> RFLAGS
push rax
popfq
//copy RFLAGS back to EAX
pushfq
pop rax
//CPUID_BIT is reserved bit and cannot be changed
//for 386 processor or lower
//if we can change, it means we run on newer processor
and rax, CPUID_BIT
shr rax, 21
mov supported, al
//restore original RFLAGS
push rcx
popfq
pop rcx
pop rax
end;
result := supported;
end;
{*
* Run CPUID instruction
* @param cardinal operation store code for operation to be performed
*}
function TCpuIdentifier.cpuidExec(const operation : cardinal) : TCPUIDResult;
var tmpEax, tmpEbx, tmpEcx, tmpEdx : cardinal;
begin
asm
push rax
push rbx
push rcx
push rdx
mov eax, operation
cpuid
mov tmpEax, eax
mov tmpEbx, ebx
mov tmpEcx, ecx
mov tmpEdx, edx
pop rdx
pop rcx
pop rbx
pop rax
end;
result.eax := tmpEax;
result.ebx := tmpEbx;
result.ecx := tmpEcx;
result.edx := tmpEdx;
end;
{------------end 64 bit architecture code ----------}
{$ENDIF}
function TCpuIdentifier.getCPUBasicInfo() : TCPUBasicInfo;
var res:TCPUIDResult;
begin
result := Default(TCPUBasicInfo);
if not cpuidSupported() then
begin
exit;
end;
res := cpuidExec(CPUID_OPR_BASIC_INFO);
result.operation := res.eax;
result.VendorId[0] := res.ebx;
result.VendorId[1] := res.edx;
result.VendorId[2] := res.ecx;
end;
function TCpuIdentifier.getCPUExtInfo() : TCPUExtInfo;
var res:TCPUIDResult;
br:TCPUBrandString;
begin
result := Default(TCPUExtInfo);
if not cpuidSupported() then
begin
exit;
end;
res := cpuidExec(CPUID_OPR_EXTENDED_INFO_MORE);
result.moreOperation := res.ecx;
res := cpuidExec(CPUID_OPR_EXTENDED_INFO);
result.extOperation := res.eax;
result.extOperationAvail := (res.eax > CPUID_OPR_EXTENDED_INFO);
result.brandStringAvail:=(res.eax >= CPUID_OPR_BRAND_INFO_AVAIL);
result.brandString:='';
if result.brandStringAvail then
begin
br.res := cpuidExec(CPUID_OPR_BRAND_INFO_0);
result.brandString := result.brandString + br.brand;
br.res := cpuidExec(CPUID_OPR_BRAND_INFO_1);
result.brandString := result.brandString + br.brand;
br.res := cpuidExec(CPUID_OPR_BRAND_INFO_2);
result.brandString := result.brandString + br.brand;
end;
end;
function TCpuIdentifier.processorName() : string;
var cpuExtInfo : TCPUExtInfo;
begin
cpuExtInfo := getCPUExtInfo();
result := cpuExtInfo.brandString;
end;
function TCpuIdentifier.vendorName() : string;
var basicInfo : TCPUBasicInfo;
begin
basicInfo := getCPUBasicInfo();
result := basicInfo.VendorIdStr;
end;
const
CPUID_VERSION_EXTFAMILY_BIT = $f00000;
CPUID_VERSION_EXTMODEL_BIT = $0f0000;
CPUID_VERSION_PROCTYPE_BIT = $003000;
CPUID_VERSION_FAMILY_BIT = $000f00;
CPUID_VERSION_MODEL_BIT = $0000f0;
CPUID_VERSION_STEPPING_BIT = $00000f;
function TCpuIdentifier.family() : byte;
var res:TCPUIDResult;
familyValue, extFamilyValue : byte;
begin
res := cpuidExec(CPUID_OPR_VERSION_FEATURE_INFO);
familyValue := (res.eax and CPUID_VERSION_FAMILY_BIT) shr 8;
result := familyValue;
if (familyValue = $0f) then
begin
extFamilyValue := (res.eax and CPUID_VERSION_EXTFAMILY_BIT) shr 20;
result := extFamilyValue + familyValue;
end;
end;
function TCpuIdentifier.model(): byte;
var res:TCPUIDResult;
modelValue, familyValue, extModelValue : byte;
begin
res := cpuidExec(CPUID_OPR_VERSION_FEATURE_INFO);
modelValue := (res.eax and CPUID_VERSION_MODEL_BIT) shr 4;
familyValue := (res.eax and CPUID_VERSION_FAMILY_BIT) shr 8;
result := modelValue;
if ((familyValue = $06) or (familyValue = $0f)) then
begin
extModelValue := (res.eax and CPUID_VERSION_EXTMODEL_BIT) shr 16;
result := (extModelValue shl 4) + modelValue;
end;
end;
function TCpuIdentifier.stepping(): byte;
var res:TCPUIDResult;
begin
res := cpuidExec(CPUID_OPR_VERSION_FEATURE_INFO);
result := res.eax and CPUID_VERSION_STEPPING_BIT;
end;
function TCpuIdentifier.maximumFrequency() : word;
var res:TCPUIDResult;
begin
res := cpuidExec(CPUID_OPR_PROC_FREQUENCY_INFO);
result := res.ebx and $ff;
end;
function TCpuIdentifier.baseFrequency() : word;
var res:TCPUIDResult;
begin
res := cpuidExec(CPUID_OPR_PROC_FREQUENCY_INFO);
result := res.eax and $ff;
end;
function TCpuIdentifier.busReferenceFrequency() : word;
var res:TCPUIDResult;
begin
res := cpuidExec(CPUID_OPR_PROC_FREQUENCY_INFO);
result := res.ecx and $ff;
end;
function TCpuIdentifier.hasFeature(const feature: string): boolean;
var res:TCPUIDResult;
begin
result := false;
res := cpuidExec(CPUID_OPR_VERSION_FEATURE_INFO);
case feature of
'SSE3' : result := ((res.ecx and $1) = $1);
'PCLMULQDQ' : result := ((res.ecx and $2) = $2);
'DTES64' : result := ((res.ecx and $4) = $4);
'MONITOR' : result := ((res.ecx and $8) = $8);
'DS-CPL' : result := ((res.ecx and $10) = $10);
'VMX' : result := ((res.ecx and $20) = $20);
'SMX' : result := ((res.ecx and $40) = $40);
'EIST' : result := ((res.ecx and $80) = $80);
'TM2' : result := ((res.ecx and $100) = $100);
'SSSE3' : result := ((res.ecx and $200) = $200);
'CNXT-ID' : result := ((res.ecx and $400) = $400);
'SDBG' : result := ((res.ecx and $800) = $800);
'FMA' : result := ((res.ecx and $1000) = $1000);
'CMPXCHG16B' : result := ((res.ecx and $2000) = $2000);
'xTPR' : result := ((res.ecx and $4000) = $4000);
'PDCM' : result := ((res.ecx and $8000) = $8000);
//'reserved' : result := ((res.ecx and $10000) = $10000);
'PCID' : result := ((res.ecx and $20000) = $20000);
'DCA' : result := ((res.ecx and $40000) = $40000);
'SSE4_1' : result := ((res.ecx and $80000) = $80000);
'SSE4_2' : result := ((res.ecx and $100000) = $100000);
'x2APIC' : result := ((res.ecx and $200000) = $200000);
'MOVBE' : result := ((res.ecx and $400000) = $400000);
'POPCNT' : result := ((res.ecx and $800000) = $800000);
'TSC-DEADLINE' : result := ((res.ecx and $1000000) = $1000000);
'AES' : result := ((res.ecx and $2000000) = $2000000);
'XSAVE' : result := ((res.ecx and $4000000) = $4000000);
'OSXSAVE' : result := ((res.ecx and $8000000) = $8000000);
'AVX' : result := ((res.ecx and $10000000) = $10000000);
'F16C' : result := ((res.ecx and $20000000) = $20000000);
'RDRAND' : result := ((res.ecx and $40000000) = $40000000);
//'notused' : result := ((res.ecx and $80000000) = $80000000);
'FPU' : result := ((res.edx and $1) = $1);
'VME' : result := ((res.edx and $2) = $2);
'DE' : result := ((res.edx and $4) = $4);
'PSE' : result := ((res.edx and $8) = $8);
'TSC' : result := ((res.edx and $10) = $10);
'MSR' : result := ((res.edx and $20) = $20);
'PAE' : result := ((res.edx and $40) = $40);
'MCE' : result := ((res.edx and $80) = $80);
'CX8' : result := ((res.edx and $100) = $100);
'APIC' : result := ((res.edx and $200) = $200);
//'reserved' : result := ((res.edx and $400) = $400);
'SEP' : result := ((res.edx and $800) = $800);
'MTRR' : result := ((res.edx and $1000) = $1000);
'PGE' : result := ((res.edx and $2000) = $2000);
'MCA' : result := ((res.edx and $4000) = $4000);
'CMOV' : result := ((res.edx and $8000) = $8000);
'PAT' : result := ((res.edx and $10000) = $10000);
'PSE-36' : result := ((res.edx and $20000) = $20000);
'PSN' : result := ((res.edx and $40000) = $40000);
'CLFSH' : result := ((res.edx and $80000) = $80000);
//'reserved' : result := ((res.edx and $100000) = $100000);
'DS' : result := ((res.edx and $200000) = $200000);
'ACPI' : result := ((res.edx and $400000) = $400000);
'MMX' : result := ((res.edx and $800000) = $800000);
'FXSR' : result := ((res.edx and $1000000) = $1000000);
'SSE' : result := ((res.edx and $2000000) = $2000000);
'SSE2' : result := ((res.edx and $4000000) = $4000000);
'SS' : result := ((res.edx and $8000000) = $8000000);
'HTT' : result := ((res.edx and $10000000) = $10000000);
'TM' : result := ((res.edx and $20000000) = $20000000);
//'reserved' : result := ((res.edx and $40000000) = $40000000);
'PBE' : result := ((res.edx and $80000000) = $80000000);
end;
res := cpuidExec(CPUID_OPR_EXTENDED_INFO_MORE);
case feature of
// ECX
'SAHF' : result := ((res.ecx and $1) = $1);
'PREFETCHW' : result := ((res.ecx and $100) = $100);
// EDX
'NX' : result := ((res.edx and $100000) = $100000);
'PDPE1GB' : result := ((res.edx and $4000000) = $4000000);
end;
end;
end.