-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuid.cpp
331 lines (260 loc) · 10.7 KB
/
cpuid.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
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
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include "cpuint.h"
#include "constants.h"
using namespace std;
template <typename T>
void printFeatureValue(string featureString, T value) {
cout << left;
cout << setw(FEATURE_NAME_WIDTH) << featureString << ": " << value << endl;
cout << right;
}
template <typename T>
void printFeatureValueHex(string featureString, T value) {
cout << hex << uppercase << left;
cout << setw(FEATURE_NAME_WIDTH) << featureString << ": 0x";
cout << setfill('0') << right;
cout << setw(sizeof(value) * 2) << value << endl;
cout << dec << nouppercase << setfill(' ');
}
void printSupported(uint32_t flagData, uint16_t flagIndex, string featureString) {
// string value = flagAtIndex(flagData, flagIndex) ? "SUPPORTED" : "NOT SUPPORTED";
string value = flagAtIndex(flagData, flagIndex) ? "true" : "false";
printFeatureValue(featureString, value);
}
void printSupported(string featureString, bool supported) {
// string value = supported ? "SUPPORTED" : "NOT SUPPORTED";
string value = supported ? "true" : "false";
printFeatureValue(featureString, value);
}
int main() {
auto data = new CPUID_DATA();
bool supported = CPUIDInstructionSupported();
printSupported("CPUID instruction", supported);
if (!supported) {
delete data;
return 1;
}
//////////////////////////////////////////////////////////
// EAX = 0 //
//////////////////////////////////////////////////////////
getCPUIDData(0, data);
uint32_t maxBasicInput = data->EAX;
printFeatureValueHex("Basic CPUID Max Input", maxBasicInput);
string manufacturerString;
manufacturerString.reserve(12);
manufacturerString.append(stringFromRaw(reinterpret_cast<char *>(&(data->EBX)), 4));
manufacturerString.append(stringFromRaw(reinterpret_cast<char *>(&(data->EDX)), 4));
manufacturerString.append(stringFromRaw(reinterpret_cast<char *>(&(data->ECX)), 4));
printFeatureValue("Manufacturer ID String", manufacturerString);
//////////////////////////////////////////////////////////
// EAX = 80000000H //
//////////////////////////////////////////////////////////
clearStruct(data);
getCPUIDData(0x80000000, data);
uint32_t maxExtendedInput = data->EAX;
bool brandStringSupported = maxExtendedInput >= 0x80000004;
printSupported("Brand String Method", brandStringSupported);
string brandString;
brandString.reserve(48);
if (brandStringSupported) {
for (uint32_t i = 0x80000002; i <= 0x80000004; i++) {
clearStruct(data);
getCPUIDData(i, data);
auto one = stringFromRaw(reinterpret_cast<char *>(&(data->EAX)), 4);
auto two = stringFromRaw(reinterpret_cast<char *>(&(data->EBX)), 4);
auto three = stringFromRaw(reinterpret_cast<char *>(&(data->ECX)), 4);
auto four = stringFromRaw(reinterpret_cast<char *>(&(data->EDX)), 4);
brandString.append(one).append(two).append(three).append(four);
}
printFeatureValue("CPU Brand String", brandString);
}
//////////////////////////////////////////////////////////
// EAX = 1 //
//////////////////////////////////////////////////////////
cout << endl << "-------- Section One --------" << endl;
clearStruct(data);
getCPUIDData(1, data);
uint32_t signature = data->EAX;
uint16_t steppingID = intFromBitRange(data->EAX, 0, 3),
model = intFromBitRange(data->EAX, 4, 7),
procType = intFromBitRange(data->EAX, 12, 13),
familyID = intFromBitRange(data->EAX, 8, 11);
if (familyID == 0x0FU) {
familyID |= intFromBitRange(data->EAX, 20, 27) << 4;
}
if (familyID == 0x0FU || familyID == 0x06) {
model |= intFromBitRange(data->EAX, 16, 19) << 4;
}
printFeatureValueHex("Signature", signature);
printFeatureValueHex("Family ID", familyID);
printFeatureValueHex("Model #", model);
printFeatureValueHex("Stepping ID", steppingID);
printFeatureValue("Processor Type", PROC_TYPES[procType]);
uint32_t brandIndex = intFromBitRange(data->EBX, 0, 7),
CLFLUSHCacheSize = 8 * intFromBitRange(data->EBX, 8, 15),
APICLocalID = intFromBitRange(data->EBX, 23, 31);
bool indexSupported = brandIndex > 0;
printSupported("Brand Index Method", indexSupported);
if (indexSupported) {
if (signature == 0x00000F13U) {
if (brandIndex == 11) {
brandIndex = 12;
}
else if (brandIndex == 14) {
brandIndex = 11;
}
}
else if (signature == 0x000006B1U && brandIndex == 3) {
brandIndex = 1;
}
brandString = BRAND_TABLE.at(brandIndex);
printFeatureValue("CPU Brand String", brandString);
}
printFeatureValue("CLFLUSH Cache Line Size", CLFLUSHCacheSize);
printFeatureValueHex("Local APIC ID", APICLocalID);
printSupported(data->ECX, 0, "SSE3");
printSupported(data->ECX, 1, "PCLMULQDQ");
printSupported(data->ECX, 2, "DTES64");
printSupported(data->ECX, 3, "MONITOR");
printSupported(data->ECX, 4, "DS-CPL");
printSupported(data->ECX, 5, "VMX");
printSupported(data->ECX, 6, "SMX");
printSupported(data->ECX, 7, "EIST");
printSupported(data->ECX, 8, "TM2");
printSupported(data->ECX, 9, "SSSE3");
printSupported(data->ECX, 10, "CNXT-ID");
printSupported(data->ECX, 11, "SDBG");
printSupported(data->ECX, 12, "FMA");
printSupported(data->ECX, 13, "CMPXCHG16B");
printSupported(data->ECX, 14, "xTPR Update Control");
printSupported(data->ECX, 15, "PDCM");
printSupported(data->ECX, 17, "PCID");
printSupported(data->ECX, 18, "DCA");
printSupported(data->ECX, 19, "SSE4.1");
printSupported(data->ECX, 20, "SSE4.2");
printSupported(data->ECX, 21, "x2APIC");
printSupported(data->ECX, 22, "MOVBE");
printSupported(data->ECX, 23, "POPCNT");
printSupported(data->ECX, 24, "TSC-Deadline");
printSupported(data->ECX, 25, "AESNI");
printSupported(data->ECX, 26, "XSAVE");
printSupported(data->ECX, 27, "OSXSAVE");
printSupported(data->ECX, 28, "AVX");
printSupported(data->ECX, 29, "F16C");
printSupported(data->ECX, 30, "RDRAND");
printSupported(data->EDX, 0, "FPU");
printSupported(data->EDX, 1, "VME");
printSupported(data->EDX, 2, "DE");
printSupported(data->EDX, 3, "PSE");
printSupported(data->EDX, 4, "TSC");
printSupported(data->EDX, 5, "MSR");
printSupported(data->EDX, 6, "PAE");
printSupported(data->EDX, 7, "MCE");
printSupported(data->EDX, 8, "CX8");
printSupported(data->EDX, 9, "APIC");
printSupported(data->EDX, 11, "SEP");
printSupported(data->EDX, 12, "MTRR");
printSupported(data->EDX, 13, "PGR");
printSupported(data->EDX, 14, "MCA");
printSupported(data->EDX, 15, "CMOV");
printSupported(data->EDX, 16, "PAT");
printSupported(data->EDX, 17, "PSE-36");
bool serialNumberSupported = intFromBitRange(data->EDX, 18, 18) == 1;
printSupported("PSN", serialNumberSupported);
printSupported(data->EDX, 19, "CLFSH");
printSupported(data->EDX, 21, "DS");
printSupported(data->EDX, 22, "ACPI");
printSupported(data->EDX, 23, "MMX");
printSupported(data->EDX, 24, "FXSR");
printSupported(data->EDX, 25, "SSE");
printSupported(data->EDX, 26, "SSE2");
printSupported(data->EDX, 27, "SS");
printSupported(data->EDX, 28, "HTT");
printSupported(data->EDX, 29, "TM");
printSupported(data->EDX, 31, "PBE");
//////////////////////////////////////////////////////////
// EAX = 2 //
//////////////////////////////////////////////////////////
cout << endl << "-------- Section Two --------" << endl;
clearStruct(data);
getCPUIDData(2, data);
cout << "TLB/Cache/Prefetch Descriptors:" << endl;
uint8_t count = data->EAX;
while (true) {
uint32_t registers[] = {data->EAX, data->EBX, data->ECX, data->EDX};
for (int r = 0; r < 4; r++) {
uint32_t reg = registers[r];
// If register is eax...
if (r == 0) {
// Clear the least significant byte to keep
// from interpreting it as a descriptor
reg &= 0xFFFFFF00;
}
// If most significant bit is 1; register is invalid
if (reg & 0x80000000 != 0) continue;
for (int i = 0; i <= 24; i += 8) {
uint8_t byte = intFromBitRange(reg, i, i + 7);
if (byte == 0) continue;
try {
auto descriptor = CACHE_DESCRIPTORS.at(byte);
cout << descriptor << endl;
}
catch (out_of_range) {
continue;
}
}
}
count--;
if (count <= 1) break;
clearStruct(data);
getCPUIDData(2, data);
}
//////////////////////////////////////////////////////////
// EAX = 3 //
//////////////////////////////////////////////////////////
cout << endl << "------- Section Three -------" << endl;
if (serialNumberSupported) {
clearStruct(data);
getCPUIDData(3, data);
uint32_t mid = data->EDX;
uint32_t low = data->ECX;
char highString[9];
char midString[9];
char lowString[9];
snprintf(highString, 9, "%08X", signature);
snprintf(midString, 9, "%08X", mid);
snprintf(lowString, 9, "%08X", low);
string serial;
serial.append(stringFromRaw(highString, 4));
serial.append(1, '-');
serial.append(stringFromRaw(highString + 4, 4));
serial.append(1, '-');
serial.append(stringFromRaw(midString, 4));
serial.append(1, '-');
serial.append(stringFromRaw(midString + 4, 4));
serial.append(1, '-');
serial.append(stringFromRaw(lowString, 4));
serial.append(1, '-');
serial.append(stringFromRaw(lowString + 4, 4));
printFeatureValue("Processor Serial Number", serial);
}
// for (int i = 1; i <= maxBasicInput; i++) {
// clearStruct(data);
// getCPUIDData(i, data);
// cout << dec << i << hex
// << "\tEAX:\t" << data->EAX
// << "\tEBX:\t" << data->EBX
// << "\tECX:\t" << data->ECX
// << "\tEDX:\t" << data->EDX
// << endl;
// }
delete data;
return 0;
}