-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathcpuid.c
More file actions
2415 lines (2179 loc) · 90.5 KB
/
cpuid.c
File metadata and controls
2415 lines (2179 loc) · 90.5 KB
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* *
* File name cpuid.c *
* *
* Description Identify the CPU ID and speed. *
* *
* Notes References: *
* Intel Application Note 485 - The CPUID Instruction *
* Intel 64 and IA-32 Architectures Software Developer's Manual
* Wikipedia: https://en.wikipedia.org/wiki/CPUID *
* *
* TO DO: Add support for VMX capabilities detection, like *
* Intel's EPT (Extended Page Tables) (= AMD's SLAT (Second *
* Level Address Translation)) *
* Unfortunately, this is not defined in CPUID, but in MSRs. *
* See Intel's IA32 Software Development Manual volume 3 *
* chapter 28.2 (EPT) and appendix A (VMX Capability *
* Reporting Facility *
* And reading MSRs requires using a device driver. *
* https://stackoverflow.com/questions/45428588/can-i-read-the-cpu-performance-counters-from-a-user-mode-program-in-windows
* https://github.com/intel/pcm/tree/master/src/WinMSRDriver *
* https://github.com/intel/pcm/blob/master/doc/WINDOWS_HOWTO.md
* *
* Microsoft's amd64 C compiler does not support inline *
* assembly. Instead, use the intrinsic functions that *
* allow emitting cpuid or readmsr, etc, instructions. *
* Reference: *
* https://learn.microsoft.com/en-us/cpp/intrinsics/compiler-intrinsics
* https://learn.microsoft.com/en-us/cpp/intrinsics/cpuid-cpuidex
* https://learn.microsoft.com/en-us/cpp/intrinsics/readmsr *
* *
* History *
* 1997-06-13 JFL Created this file. *
* 1997-09-03 JFL Updated comments. *
* 1998-03-18 JFL Added -i option to display processor ID information. *
* 1999-02-01 JFL Updated processors IDs and capability flags from the *
* latest specs from Intel. *
* 2000-03-22 JFL Added Pentium III and some Willamette support. *
* 2003-01-22 JFL Added Pentium IV support. *
* 2005-04-28 JFL Added Extended Feature Flags support. *
* 2005-09-30 JFL Override NODOSLIB's putchar, to output on stdout. *
* 2009-08-29 JFL Read the extended model number too. *
* Added numerous processor models to the list. *
* Added numerous new feature flags definitions. *
* 2009-08-31 JFL Added the definition of several AMD extended features. *
* 2009-09-01 JFL Added numerous processor code names from wikipedia. *
* Added a version time stamp, displayed by -?. *
* 2009-10-06 JFL Adapted to WIN32. *
* 2012-10-18 JFL Added my name in the help. *
* 2013-01-31 JFL Display the number of cores. *
* 2013-05-30 JFL Some CPUs pretend to support function 0xB, but do not. *
* 2016-04-12 JFL Removed a duplicate prototype, now defined in pmode.h. *
* 2017-05-31 JFL Fixed WIN32 warnings. No functional code change. *
* 2017-12-18 JFL Fixed DOS warnings. No functional code change. *
* 2019-04-19 JFL Use the version strings from the new stversion.h. *
* 2019-06-12 JFL Added PROGRAM_DESCRIPTION definition. *
* 2019-09-27 JFL Fixed a minor formating error in a debug message. *
* 2020-02-23 JFL Decode cpuid(7, 0) output. *
* 2020-02-24 JFL Added option -m to experiment with reading MSRs. *
* 2020-02-26 JFL Added option -w to experiment with reading WMI props. *
* Output a few WMI props, including SLAT, in Windows. *
* Skip head spaces in brand string, if any. *
* 2020-03-02 JFL Display the CPUID index for every set of feature flags. *
* Corrected typos and errors about MTRR registers. *
* 2022-11-09 JFL Added option -c to manually test one CPUID call. *
* Added support for the WIN64 operating system. *
* 2022-11-10 JFL Rewrite support for cpuid(0x0B), replaced by cpuid(0x1F). *
* Fixed the extended family calculation and display. *
* 2022-11-11 JFL Added many new feature bits definitions. *
* Added the short alias for each feature bit. *
* Don't display a computed name if we have the brand string.*
* Use debug and experimental features in debug builds only. *
* Restructured main() to use action flags and subroutines. *
* Added options -a, -f, -n, -t to invoke individual actions.*
* Added option -q to query if a given feature is available. *
* 2022-11-15 JFL Improved option -q to support feature sets. *
* 2022-11-16 JFL Added option -ls to list feature sets. *
* 2022-11-17 JFL Option -c is implied when passing arguments. *
* 2022-11-18 JFL Decode leafs 16H, 17H, 1AH. *
* 2022-11-20 JFL Decode leaf 18H. *
* 2022-11-21 JFL Display the reserved feature bits that are set. *
* Streamlined the output in non-verbose mode. *
* Decode the cache parameters in leaf 4. *
* Fixed a bug displaying the measured frequency in DOS. *
* Option -w alone calls DisplayWmiProcInfo(). *
* 2022-11-27 JFL Make sure lodos.h is included before BiosLib's clibdef.h. *
* This way, the DOS program output can be redirected. *
* 2022-12-07 JFL Fix the -? option which crashed under BIOS & LODOS. *
* 2025-08-12 JFL Make sure DCOM definitions are included in all cases. *
* *
* © Copyright 2016 Hewlett Packard Enterprise Development LP *
* Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 *
\*****************************************************************************/
#define PROGRAM_DESCRIPTION "Identify the processor and its features"
#define PROGRAM_NAME "cpuid"
#define PROGRAM_VERSION "2025-08-12"
/* Definitions */
#pragma warning(disable:4001) /* Ignore the // C++ comment warning */
#define _WIN32_DCOM /* Force including DCOM definitions in windows.h */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/************************ Win32-specific definitions *************************/
#if defined(_WIN64)
/* As the Microsoft amd64 compiler does not support inline assembly, the only
way to invoke cpuid and rdmsr instructions is to use compiler intrinsics */
#include <intrin.h>
#elif defined(_WIN32) /* Automatically defined when targeting a Win32 applic. */
/* These intrinsics could also be used for the x86 compiler, but as this was
already implemented using inline assembly, leaving it this way for now */
// Definitions, from MS-DOS' pmode.h, for data emission within the code stream.
#define DB(x) __asm _emit x // BYTE
#define DW(x) DB((x) & 0xFF) DB((x) >> 8U) // WORD
#define DD(x) DW((x) & 0xFFFF) DW((x) >> 16U) // DWORD
#endif
#ifdef _WIN32 /* Automatically defined when targeting both WIN32 and WIN64 apps. */
#define SUPPORTED_OS 1
#define _CRT_SECURE_NO_WARNINGS // Don't warn about old unsecure functions.
#include <windows.h>
// Manipulate bytes, words and dwords. Can be used both as rvalue and as lvalue.
#define DWORD0(qw) (((DWORD *)(&(qw)))[0])
#define DWORD1(qw) (((DWORD *)(&(qw)))[1])
#define WORD0(qw) (((WORD *)(&(qw)))[0])
#define WORD1(qw) (((WORD *)(&(qw)))[1])
#define WORD2(qw) (((WORD *)(&(qw)))[2])
#define WORD3(qw) (((WORD *)(&(qw)))[3])
#define BYTE0(qw) (((BYTE *)(&(qw)))[0])
#define BYTE1(qw) (((BYTE *)(&(qw)))[1])
#define BYTE2(qw) (((BYTE *)(&(qw)))[2])
#define BYTE3(qw) (((BYTE *)(&(qw)))[3])
#define BYTE4(qw) (((BYTE *)(&(qw)))[4])
#define BYTE5(qw) (((BYTE *)(&(qw)))[5])
#define BYTE6(qw) (((BYTE *)(&(qw)))[6])
#define BYTE7(qw) (((BYTE *)(&(qw)))[7])
int identify_processor(void); // 0=8086, 1=80186, 2=80286, etc...
#if _DEBUG
#ifdef _MSVCLIBX_H_
extern int iDebug;
#else
int iDebug = FALSE;
#endif
#endif
#pragma warning(disable:4996) // Ignore the sscanf function or variable may be unsafe.... warning
#endif /* _WIN32 */
/************************ MS-DOS-specific definitions ************************/
#ifdef _MSDOS /* Automatically defined when targeting an MS-DOS applic. */
#define SUPPORTED_OS 1
#include "lodos.h" /* LoDosLib definitions */
#include "utildef.h" /* BiosLib definitions */
#include "pmode.h" /* PModeLib definitions */
#define vcpi2prot vm2prot
#if _DEBUG
int iDebug = FALSE;
#endif
#endif /* _MSDOS */
/******************************* Any other OS ********************************/
#ifndef SUPPORTED_OS
#error "Unsupported OS"
#endif
/********************** End of OS-specific definitions ***********************/
/* SysToolsLib include files */
#include "mainutil.h" /* SysLib helper routines for main() */
#include "stversion.h" /* SysToolsLib version strings. Include last. */
/* Intel processors list */
typedef struct
{
int iFamily;
int iModel;
char *pszCodeName;
char *pszName;
} INTEL_PROC;
INTEL_PROC IntelProcList[] =
{ // See http://en.wikipedia.org/wiki/List_of_Intel_microprocessors
// Family Model CodeName Name Brand name
{ 4, 0, "", "486 DX" },
{ 4, 1, "", "486 DX" },
{ 4, 2, "", "486 SX" },
{ 4, 3, "", "486 DX2" },
{ 4, 4, "", "486 SL" },
{ 4, 5, "", "486 SX2" },
{ 4, 7, "", "486 DX2 enhanced" },
{ 4, 8, "", "486 DX4" },
{ 5, 1, "P5", "Pentium" }, // Initial Pentium (60, 66)
{ 5, 2, "P54C", "Pentium" }, // Pentium (75-200)
{ 5, 3, "", "Pentium Overdrive for 486 systems" },
{ 5, 4, "P55C/Tillamook", "Pentium MMX" }, // Pentium MMX (166-233)
{ 6, 1, "P6", "Pentium Pro" }, // Pentium Pro
{ 6, 3, "Klamath", "Pentium II" }, // Pentium II (233-450)
{ 6, 5, "DesChutes", "Pentium II" }, // Portable P. II
{ 6, 6, "Mendocino", "Celeron" },
{ 6, 7, "Katmai", "Pentium III" },
{ 6, 8, "CopperMine", "Pentium III" },
{ 6, 9, "Banias", "Pentium M model 9 130nm" },
{ 6, 10, "", "Pentium III Xeon A" },
{ 6, 11, "Tualatin", "Pentium III model B" },
{ 6, 13, "Dothan", "Pentium M model D 90nm" },
{ 6, 14, "Yonah", "Core model E 65nm" },
{ 6, 15, "Conroe", "Core 2 model F 65nm" },
{ 6, 21, "Tolapai", "EP80579 Integrated Processor" },
{ 6, 22, "", "Celeron model 16h" },
{ 6, 23, "Wolfdale", "Core 2 Extreme 45nm" },
{ 6, 26, "Bloomfield", "Core i7 45nm" },
{ 6, 28, "", "Atom 45nm" },
{ 6, 29, "", "Xeon MP 45nm" },
{ 7, 0, "Merced", "Itanium" },
{ 15, 0, "Willamette", "Pentium 4 model 0 180nm" },
{ 15, 1, "Willamette", "Pentium 4 model 1 180nm" },
{ 15, 2, "Northwood", "Pentium 4 model 2 130nm" },
{ 15, 3, "Prescott", "Pentium 4 model 3 90nm" },
{ 15, 4, "Prescott-2M", "Pentium 4 model 4 90nm" }, // 64 bits
{ 15, 6, "Cedar Mill", "Pentium 4 model 6 65nm" }, // 64 bits
{ 16, 0, "McKinley", "Itanium 2 180nm" },
{ 16, 1, "Madison", "Itanium 2 130nm" },
{ 16, 2, "Madison 9M", "Itanium 2 130nm" },
/* The Itanium processor list in Wikipedia...
https://en.wikipedia.org/wiki/List_of_Intel_Itanium_processors
... has inconsistent family numbers for subsequent itanium processors.
It's unlikely that any one of them ever ran DOS or Windows anyway, so
no need to extend the list here. */
};
#define N_INTEL_PROCS (sizeof(IntelProcList) / sizeof(INTEL_PROC))
/* Action flags */
#define SHOW_NAME 0x0001
#define SHOW_FEATURES 0x0002
#define SHOW_FREQUENCY 0x0004
#define SHOW_CPUID_LEAF 0x0008
#define SHOW_CPUID_SUBLEAF 0x0010
#define SHOW_FEATURE_SETS 0x0020
#define SHOW_WMI_PROPS 0x0040
/* Global variables */
int iVerbose = FALSE;
/* Forward references */
void usage(void);
DWORD _rdtsc(void);
long getms(void);
int GetProcessorName(int iFamily, char *pBuf, int iBufSize);
int MeasureProcSpeed(void);
int DisplayProcInfo(char *pszQuery);
int QueryFeature(char *pszFeature);
void ShowFeatureSets();
DWORD _cpuid(DWORD dwId, DWORD *pEax, DWORD *pEbx, DWORD *pEcx, DWORD *pEdx);
#if _DEBUG
void _rdmsr(DWORD dwECX, DWORD pdwMSR[2]);
#endif
#ifdef _WIN32
int GetWmiProcInfo(char *lpszPropName, void *lpBuf, size_t lBuf);
int FormatWmiProcInfo(char *lpszPropName, void *lpBuf, size_t lBuf);
void DisplayWmiProcInfo(void);
#endif // defined(_WIN32)
/*---------------------------------------------------------------------------*\
* *
| Function main |
| |
| Description EXE program main initialization routine |
| |
| Parameters int argc Number of arguments |
| char *argv[] List of arguments |
| |
| Returns The return code to pass to the BIOS, if run from ROM. |
| |
| History |
| 1996/11/20 JFL Created this routine |
* *
\*---------------------------------------------------------------------------*/
int _cdecl main(int argc, char *argv[]) {
int i;
int iFamily;
int iFrequency;
char *pszQuery = NULL;
int iAction = 0;
int iFirst = TRUE;
DWORD dwEAX=0, dwEBX=0, dwECX=0, dwEDX=0;
#if defined(_WIN32)
char *pszWmiPropName = NULL;
#endif
#ifdef _MSDOS
int iErr;
_dos_setblock(0x1000, (WORD)_psp, (WORD *)&iErr); /* 3rd arg = max § size */
#endif // defined(_MSDOS)
/* Process arguments */
for (i=1 ; i<argc ; i++) {
char *arg = argv[i];
if (IsSwitch(arg)) { /* It's a switch */
char *opt = arg+1;
if (streq(opt, "?")) { /* -?: Help */
usage(); /* Display help */
}
if (streq(opt, "a")) { /* -f: Show all */
iAction = ~0;
continue;
}
if (streq(opt, "c")) { /* -c: Call the CPUID instruction */
if (((i+1) < argc) && sscanf(argv[i+1], "%X", &dwEAX)) { /* Leaf number */
i += 1;
iAction = SHOW_CPUID_LEAF;
if (((i+1) < argc) && sscanf(argv[i+1], "%X", &dwECX)) { /* Sub-leaf number */
i += 1;
iAction = SHOW_CPUID_SUBLEAF;
}
} else {
fprintf(stderr, "Missing or invalid CPUID leaf number\n");
return 1;
}
continue;
}
#if _DEBUG
if (streq(opt, "d")) { /* -d: Debug information */
iDebug = TRUE;
iVerbose = TRUE;
continue;
}
#endif
if (streq(opt, "f")) { /* -f: Show features */
iAction |= SHOW_FEATURES;
continue;
}
if (streq(opt, "ls")) { /* -ls: List feature sets */
iAction |= SHOW_FEATURE_SETS;
continue;
}
#if _DEBUG
if (streq(opt, "m")) { /* -m: Read MSR (Experimental)*/
int iMSR;
if (((i+1) < argc) && sscanf(argv[i+1], "%X", &iMSR)) {
DWORD pdwMSR[2];
i += 1;
printf("Reading MSR(0x%X)\n", iMSR);
fflush(stdout);
_rdmsr(iMSR, pdwMSR);
printf("MSR(0x%X) = 0x%08ulX:%08ulX\n", iMSR, pdwMSR[1], pdwMSR[0]);
return 0;
} else {
fprintf(stderr, "Missing or invalid MSR number\n");
return 1;
}
}
#endif /* _DEBUG */
if (streq(opt, "n")) { /* -n: Show name */
iAction |= SHOW_NAME;
continue;
}
if (streq(opt, "q")) { /* -q: Query if a feature is supported */
if ((i+1) < argc) {
pszQuery = strlwr(argv[++i]);
} else {
fprintf(stderr, "Missing feature name\n");
return 1;
}
iAction |= SHOW_FEATURES;
continue;
}
if (streq(opt, "t")) { /* -t: Measure the frequency using the TSC */
iAction |= SHOW_FREQUENCY;
continue;
}
if (streq(opt, "v")) { /* -v: Verbose information */
iVerbose = TRUE;
continue;
}
if (streq(opt, "V")) { /* -V: Display version information */
puts(DETAILED_VERSION);
return 0;
}
#if defined(_WIN32)
if (streq(opt, "w")) { /* -w: Get WMI processor information */
if (((i+1) < argc) && !IsSwitch(argv[i+1])) {
pszWmiPropName = argv[++i];
}
iAction |= SHOW_WMI_PROPS;
continue;
}
#endif // defined(_WIN32)
fprintf(stderr, "Error: Unsupported switch %s\n", arg);
exit(1);
} /* End if it's a switch */
/* If it's an argument */
if (!iAction) {
if (!sscanf(arg, "%X", &dwEAX)) goto err_unexpected_arg;
iAction = SHOW_CPUID_LEAF;
continue;
}
if (iAction == SHOW_CPUID_LEAF) {
if (!sscanf(arg, "%X", &dwECX)) goto err_unexpected_arg;
iAction = SHOW_CPUID_SUBLEAF;
continue;
}
err_unexpected_arg:
fprintf(stderr, "Error: Unexpected argument \"%s\"\n", arg);
exit(1);
}
if (!iAction) iAction = SHOW_NAME;
/* First process the action flags that are exclusive of the others */
if (iAction == SHOW_FEATURE_SETS) {
ShowFeatureSets();
return 0;
}
if ((iAction == SHOW_CPUID_LEAF) || (iAction == SHOW_CPUID_SUBLEAF)) {
if (iVerbose) {
printf("CPUID(0x%lX", (ULONG)dwEAX);
if (iAction == SHOW_CPUID_SUBLEAF) printf(", 0x%lX", (ULONG)dwECX);
printf("):\n");
}
_cpuid(dwEAX, &dwEAX, &dwEBX, &dwECX, &dwEDX);
printf("EAX = 0x%08lX\n", (ULONG)dwEAX);
printf("EBX = 0x%08lX\n", (ULONG)dwEBX);
printf("ECX = 0x%08lX\n", (ULONG)dwECX);
printf("EDX = 0x%08lX\n", (ULONG)dwEDX);
return 0;
}
/* Then process all other action flags */
iFamily = identify_processor(); /* Actually the Family + Extended Family */
/* Display the processor name */
if (iAction & SHOW_NAME) {
char szName[64] = "";
if (GetProcessorName(iFamily, szName, sizeof(szName))) {
/* if (!iFirst) printf("\n"); */ iFirst = FALSE;
if (iVerbose) printf("The processor is an ");
printf("%s\n", szName);
}
}
/* The following actions can only be done on a Pentium or better */
if (iFamily >= 5) {
/* On Pentium or better, display the processor feature flags */
if (iAction & SHOW_FEATURES) {
if (!iFirst) printf("\n"); iFirst = FALSE;
if (pszQuery) {
QueryFeature(pszQuery);
} else {
DisplayProcInfo(pszQuery);
}
}
}
#ifdef _WIN32
/* WMI properties allow double-checking some results of DisplayProcInfo() */
if (iAction & SHOW_WMI_PROPS) { /* -w: Get WMI processor information */
if (!iFirst) printf("\n"); iFirst = FALSE;
if (pszWmiPropName) {
char buf[1024];
int iResult = FormatWmiProcInfo(pszWmiPropName, buf, sizeof(buf));
if (iResult == -1) {
HRESULT hr = *(HRESULT *)buf;
fprintf(stderr, "Failed to get WMI Win32_Processor property %s. HRESULT 0x%X", pszWmiPropName, hr);
return 1;
}
if (iVerbose) printf("%s = ", pszWmiPropName);
printf("%s\n", buf);
} else {
DisplayWmiProcInfo();
}
}
#endif // defined(_WIN32)
if (iFamily >= 5) {
/* On Pentium or better, compute the processor frequency using the TSC */
/* Note: This algorithm is compatible with Windows 95 & NT */
if (iAction & SHOW_FREQUENCY) {
if (!iFirst) printf("\n"); iFirst = FALSE;
iFrequency = MeasureProcSpeed();
printf("Measured frequency: %d MHz\n", iFrequency);
}
}
return 0;
}
/*---------------------------------------------------------------------------*\
* *
| Function usage |
| |
| Description Display a brief help for this program |
| |
| Parameters None |
| |
| Returns N/A |
| |
| History |
| 1996/10/10 JFL Created this routine |
* *
\*---------------------------------------------------------------------------*/
void usage(void) {
fputs( /* Don't use printf(), or if you need to, split the string in halves,
to avoid passing the 1KB limit of the BiosLib printf() */
PROGRAM_NAME_AND_VERSION " - " PROGRAM_DESCRIPTION "\n\
\n\
Usage: cpuid [SWITCHES] [LEAF [SUBLEAF]]\n\
\n\
Get a CPUID leaf (EAX) and optional subleaf (ECX)\n\
Default: Get the CPU name\n\
\n\
Optional switches:\n\
\n\
-a Display all we know about the processor\n\
[-c] LEAF [SUBLEAF] Get a CPUID leaf (EAX) and optional sub-leaf (ECX)\n"
#if _DEBUG
"\
-d Output debug information\n"
#endif
"\
-f Display detailed processor features\n\
-ls List supported feature sets\n"
#if _DEBUG
"\
-m MSR Read a Model Specific Register\n"
#endif
"\
-n Display the processor name (Default)\n\
-q FEAT Query if the given feature is available (1)\n\
-t Measure the CPU clock frequency using the Time Stamp Counter\n\
-v Verbose mode\n\
-V Display this program version and exit\n"
#if defined(_WIN32)
"\
-w [PROP] Get a WMI Win32_Processor property (2)\n\
Default: Display a selection of properties\n"
#endif
"\
\n\
(1) FEAT = A short feature name, as defined in Wikipedia page\n\
https://en.wikipedia.org/wiki/CPUID\n\
Ex: \"fpu\" or \"pae\"\n\
Option -f shows the short feature name ahead of each description.\n\
\n\
Or FEAT = A feature set name, based on the processor alias names and\n\
corresponding instruction sets in the GCC documentation page:\n\
https://gcc.gnu.org/onlinedocs/gcc/gcc-command-options/machine-dependent-options/x86-options.html\n\
Use option -ls to list the supported feature sets\n"
#if defined(_WIN32)
"\n\
(2) WMI Win32_Processor properties can be listed by running in a cmd shell:\n\
wmic cpu get * /format:list\n\
or in PowerShell:\n\
Get-WmiObject Win32_Processor | fl *\n"
#endif
#include "footnote.h"
, stdout);
exit(0);
}
/*---------------------------------------------------------------------------*\
* *
| Function _rdtsc |
| |
| Description Read the low DWORD of the time stamp counter |
| |
| Parameters None |
| |
| Returns DX:AX = Cycle count |
| |
| Notes Warning: The RDTSC instruction generates a GPF when run |
| under Windows in virtual 86 mode. |
| This is why it is recommended to only call this routine |
| while in 16-bits protected mode. |
| |
| History |
| 1996/11/20 JFL Created this routine |
* *
\*---------------------------------------------------------------------------*/
#pragma warning(disable:4704) // Ignore the inline assembler etc... warning
#define rdtsc DW(0x310F)
#ifdef _MSDOS
DWORD _rdtsc(void) {
_asm {
rdtsc
DATASIZE
mov dx, ax // mov edx, eax
mov cl, 16
DATASIZE
shr dx, cl // shr edx, 16
}
#pragma warning(disable:4035) // Ignore the no return value warning
}
#pragma warning(default:4035)
#endif
#if defined(_WIN64)
DWORD _rdtsc(void) {
return (DWORD)__rdtsc();
}
#elif defined(_WIN32)
DWORD _rdtsc(void) {
_asm {
rdtsc
mov edx, eax
mov cl, 16
shr edx, cl
}
#pragma warning(disable:4035) // Ignore the no return value warning
}
#pragma warning(default:4035)
#endif
#pragma warning(default:4704) // Ignore the inline assembler etc... warning
/*---------------------------------------------------------------------------*\
* *
| Function _dos_get100th |
| |
| Description Get the MS-DOS 1/100th of a second relative to 0h00. |
| |
| Parameters None |
| |
| Returns DX:AX = Current 1/100th of a second relative to 0h00. |
| |
| History |
| 1996/11/20 JFL Created this routine |
| 2003/01/21 JFL Rewrote using Microsoft-compatible _dos_getime(). |
* *
\*---------------------------------------------------------------------------*/
#ifdef _MSDOS
long _dos_get100th(void) {
long l;
_dostime_t dostime;
_dos_gettime(&dostime);
l = dostime.hour;
l *= 60;
l += dostime.minute;
l *= 60;
l += dostime.second;
l *= 100;
l += dostime.hsecond;
return l;
}
long getms(void) {
return _dos_get100th() * 10;
}
#endif // defined(_MSDOS)
#ifdef _WIN32
long getms(void) {
long l;
SYSTEMTIME wintime;
GetSystemTime(&wintime);
l = wintime.wHour;
l *= 60;
l += wintime.wMinute;
l *= 60;
l += wintime.wSecond;
l *= 1000;
l += wintime.wMilliseconds;
return l;
}
#endif // defined(_WIN32)
/*---------------------------------------------------------------------------*\
* *
| Function GetProcessorName |
| |
| Description Get or build the processor name |
| |
| Parameters None |
| |
| Returns |
| |
| History |
| 2022-11-11 JFL Extracted this routine from cpuid.c main routine. |
* *
\*---------------------------------------------------------------------------*/
int GetProcessorName(int iFamily, char *pBuf, int iBufSize) {
int i;
if (iBufSize < 49) return 0; /* We need that much for the brand string */
if (iFamily < 5) {
sprintf(pBuf, "80%d", (iFamily * 100) + 86);
/* TODO: Distinguish siblings, like 8086/8088 or 386DX/386SX, etc */
} else { // if (iFamily >= 5)
int iModel;
DWORD dwModel;
dwModel = _cpuid(1, NULL, NULL, NULL, NULL);
/* Compute the extended model number */
iModel = BYTE0(dwModel) >> 4;
if ((iFamily == 6) || (iFamily == 15)) {
int iExtModel = BYTE2(dwModel) & 0x0F;
iModel |= (iExtModel << 4);
}
/* On Pentium or better, get the processor brand name from CPUID output */
if (_cpuid(0x80000000, NULL, NULL, NULL, NULL) >= 0x80000004) {
DWORD *pdwBrand = (DWORD *)pBuf;
char *pszName;
char *pc;
// CPUID(0x80000002 - 0x80000004) : Get brand string.
_cpuid(0x80000002, pdwBrand+0, pdwBrand+1, pdwBrand+2, pdwBrand+3);
_cpuid(0x80000003, pdwBrand+4, pdwBrand+5, pdwBrand+6, pdwBrand+7);
_cpuid(0x80000004, pdwBrand+8, pdwBrand+9, pdwBrand+10, pdwBrand+11);
// Skip leading spaces
for (pszName = pBuf; *pszName == ' '; pszName++) ;
// Compress multiple spaces into a single space
for (pc=pszName; *pc; pc++) {
while ((pc[0]==' ') && (pc[1]==' ')) {
char *pc2;
for (pc2=pc; (pc2[0]=pc2[1]) != '\0'; pc2++) ;
}
}
} else {
/* Else compute the processor name from the CPUID family and model numbers */
for (i=0; i<N_INTEL_PROCS; i++) {
if ( (IntelProcList[i].iFamily == iFamily)
&& (IntelProcList[i].iModel == iModel)) {
break;
}
}
if (i < N_INTEL_PROCS) {
strcpy(pBuf, IntelProcList[i].pszName);
} else {
char *pszFamily;
char szFamily[16];
switch (iFamily) {
/* Families < 5 are handled separately above */
case 5:
pszFamily = "Pentium";
break;
case 6:
pszFamily = "P6";
break;
case 7:
pszFamily = "Itanium";
break;
case 15:
pszFamily = "Pentium 4";
break;
case 16:
case 17:
pszFamily = "Itanium 2";
break;
default:
sprintf(szFamily, "Family %d", iFamily);
pszFamily = szFamily;
break;
}
sprintf(pBuf, "%s model %d", pszFamily, iModel);
}
}
}
return (int)strlen(pBuf);
}
/*---------------------------------------------------------------------------*\
* *
| Function MeasureProcSpeed |
| |
| Description Measure the processor speed |
| |
| Parameters None |
| |
| Returns The speed in MHz |
| |
| History |
| 2022-11-11 JFL Extracted this routine from cpuid.c main routine. |
* *
\*---------------------------------------------------------------------------*/
int MeasureProcSpeed() {
long lt0, lt1;
DWORD dwt0, dwt1;
#ifdef _MSDOS
int iErr;
#endif // defined(_MSDOS)
#ifdef _MSDOS
// Switch to protected mode since the time-stamp counter is NOT
// accessible in virtual 86 mode.
if (dpmi_detect() == 0) {
#if _DEBUG
if (iDebug) printf("Switching to 16-bits PM using DPMI.\n");
#endif
iErr = dpmi2prot();
if (iErr) {
fprintf(stderr, "DPMI error %d switching to protected mode.\n", iErr);
exit(1);
}
} else if (vcpi_detect() == 0) {
#if _DEBUG
if (iDebug) printf("Switching to 16-bits PM using VCPI.\n");
#endif
iErr = vcpi2prot();
if (iErr) {
fprintf(stderr, "VCPI error %d switching to protected mode.\n", iErr);
exit(1);
}
} else { // No virtual machine control program detected.
#if _DEBUG
if (iDebug) printf("Staying in real mode.\n");
#endif
}
#endif // defined(_MSDOS)
/* TODO: This code generates an exception every other time it's run in DOS!
This is reproducible in both Windows 95 and Windows XP VMs.
The exception occurs at a seemingly random place after this point,
so this does not seem to be related to a particular instruction,
but rather to the critical section itself, or even to the switch
to protected mode.
When fixed, remove the debug output of "." inside the critical
section below.
*/
#ifdef _MSDOS
#if _DEBUG
if (iDebug) printf("BeginCriticalSection()\n"); fflush(stdout);
#endif
BeginCriticalSection();
#endif // defined(_MSDOS)
// Wait for the end of the current tick
lt0 = getms();
while (lt0 == (lt1 = getms())) ;
lt0 = lt1;
dwt0 = _rdtsc();
// Wait for 1 second
while ((lt1 = getms()) < (lt0 + 1000)) {
#ifdef _MSDOS
#if _DEBUG
if (iDebug) printf("."); fflush(stdout);
#endif
#endif
}
dwt1 = _rdtsc();
#ifdef _MSDOS
EndCriticalSection();
#if _DEBUG
if (iDebug) printf("\nEndCriticalSection()\n"); fflush(stdout);
#endif
#endif // defined(_MSDOS)
// Compute frequency
dwt1 -= dwt0; // Number of cycles
lt1 -= lt0; // Number of 1/1000th of a second
#if _DEBUG
if (iDebug) printf("Counted %lu cycles in %ld ms\n", dwt1, lt1);
#endif
if (lt1 < 0) lt1 += 86400000; // Possible wrap-around at midnight
lt1 *= 1000; // Convert to microseconds
dwt1 += lt1/2; // Round quotient to the nearest value
dwt1 /= lt1; // First frequency evaluation
#if _DEBUG
if (iDebug) printf("Raw frequency measure: %lu MHz\n", dwt1);
#endif
// Round to the nearest multiple of 16.66666 = (100/6)
if (dwt1 > 95) { // Rule applies only for processors above 100 MHz
dwt1 *= 6;
dwt1 += 50;
dwt1 /= 100;
dwt1 *= 100;
dwt1 /= 6;
}
return (int)dwt1;
}
/*---------------------------------------------------------------------------*\
* *
| Function DisplayProcInfo |
| |
| Description Display detailed processor information, from CPUID output.|
| |
| Parameters char *pszQuery Name of a feature to check |
| |
| Returns TRUE if the feature was found (Whether enabled or not) |
| |
| History |
| 1998-03-18 JFL Created this routine |
| 2009-08-31 JFL Restructured to display both enabled and disabled features|
| Added the definitions of numerous AMD extended features. |
| 2009-09-01 JFL Renamed from DisplayProcId to DisplayProcInfo. |
| 2022-11-11 JFL Restructured to allow searching for features. |
* *
\*---------------------------------------------------------------------------*/
#pragma warning(disable:4704) // Ignore the inline assembler etc... warning
/* CAUTION: Make sure the strings below do not contain any TAB character! */
char *ppszFeatures[32] = { /* Intel Features Flags - EAX=1 -> EDX */
/* 0x00000001 0 */ "fpu - Integrated FPU",
/* 0x00000002 1 */ "vme - Enhanced V86 mode",
/* 0x00000004 2 */ "de - I/O breakpoints",
/* 0x00000008 3 */ "pse - 4 MB pages",
/* 0x00000010 4 */ "tsc - Time stamp counter",
/* 0x00000020 5 */ "msr - Model-specific registers",
/* 0x00000040 6 */ "pae - Physical address extensions",
/* 0x00000080 7 */ "mce - Machine-check exception",
/* 0x00000100 8 */ "cx8 - CMPXCHG8B instruction",
/* 0x00000200 9 */ "apic - Integrated APIC",
/* 0x00000400 10 */ "", // EDX bit 10 reserved
/* 0x00000800 11 */ "sep - SYSENTER/SYSEXIT instructions",
/* 0x00001000 12 */ "mttr - MTRR registers, and the MTRR_CAP register",
/* 0x00002000 13 */ "pge - Page Global Enable bit in CR4",
/* 0x00004000 14 */ "mca - Machine check architecture",
/* 0x00008000 15 */ "cmov - CMOV instructions",
/* 0x00010000 16 */ "pat - Page Attribute table in MTRRs",
/* 0x00020000 17 */ "pse-36 - 36-bit page size extensions",
/* 0x00040000 18 */ "psn - Processor Serial Number in CPUID#3",
/* 0x00080000 19 */ "clfsh - CLFLUSH instruction",
/* 0x00100000 20 */ "", // EDX bit 20 reserved
/* 0x00200000 21 */ "ds - Debug Trace Store & Event Mon.",
/* 0x00400000 22 */ "acpi - ACPI thermal and clock control registers",
/* 0x00800000 23 */ "mmx - MMX instructions",
/* 0x01000000 24 */ "fxsr - FXSAVE and FXRSTOR Instructions",
/* 0x02000000 25 */ "sse - SSE (Streaming SIMD Extensions)",
/* 0x04000000 26 */ "sse2 - SSE 2 (Streaming SIMD Extensions v2)",
/* 0x08000000 27 */ "ss - Self-Snoop memory and caches",
/* 0x10000000 28 */ "htt - Hyper-threading capable",
/* 0x20000000 29 */ "tm - Thermal monitoring circuit",
/* 0x40000000 30 */ "ia64 - IA64 capable",
/* 0x80000000 31 */ "pbe - Pending Break Enable (PBE# pin) wakeup capability",
};
char *ppszFeatures2[32] = { /* Intel Features Flags - EAX=1 -> ECX */
/* 0x00000001 0 */ "sse3 - SSE 3 (Streaming SIMD Extensions v3)",
/* 0x00000002 1 */ "pclmulqdq - PCLMULDQ instruction",
/* 0x00000004 2 */ "dtes64 - 64-Bit Debug Store",
/* 0x00000008 3 */ "monitor - MONITOR and MWAIT instructions",
/* 0x00000010 4 */ "ds-cpl - CPL Qualified Debug Store",
/* 0x00000020 5 */ "vmx - VMX (Virtual Machine Extensions)",
/* 0x00000040 6 */ "smx - Safer Mode Extensions (Trusted Execution)",
/* 0x00000080 7 */ "est - Enhanced SpeedStep Technology",
/* 0x00000100 8 */ "tm2 - Thermal Monitor 2 Control Circuit",
/* 0x00000200 9 */ "ssse3 - SSSE 3 (Suplemental Streaming SIMD Extensions v3)",
/* 0x00000400 10 */ "cnxt-id - L1 data cache Context ID",
/* 0x00000800 11 */ "sdbg - SDBG (Silicon Debug interface)",