-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.cpp
381 lines (371 loc) · 11 KB
/
cpu.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
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
// cpu.cpp -- Gathers CPU information
// File Version: 1.1
#include "common.h"
#include "cpu.h"
#include <iostream>
using namespace std;
// Detect CPU ID
void cpuid(int func, int &ax, int &bx, int &cx, int &dx) {
asm volatile("cpuid": "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
}
// Detects the current CPU
int cpu(void) {
int ebx, unused;
cpuid(0, unused, ebx, unused, unused);
switch (ebx) {
case 0x756e6547: /* Intel Magic Code */
intel();
break;
case 0x68747541: /* AMD Magic Code */
amd();
break;
default:
cout << "Unknown x86 CPU Detected\n";
break;
}
return 0;
}
// Print Registers
void registers(int eax, int ebx, int ecx, int edx) {
int j;
char string[17];
string[16] = '\0';
for (j = 0; j < 4; j++) {
string[j] = eax >> (8 * j);
string[j + 4] = ebx >> (8 * j);
string[j + 8] = ecx >> (8 * j);
string[j + 12] = edx >> (8 * j);
}
cout << string;
}
// AMD-specific information
int amd(void) {
cout << "AMD Specific Features:\n";
int extended, eax, ebx, ecx, edx, unused;
int family, model, stepping, reserved;
cpuid(1, eax, unused, unused, unused);
model = (eax >> 4) & 0xf;
family = (eax >> 8) & 0xf;
stepping = eax & 0xf;
reserved = eax >> 12;
cout << "Family: " << family << " Model: " << model << " [";
switch (family) {
case 4:
cout << "486 Model " << model;
break;
case 5:
switch (model) {
case 0:
case 1:
case 2:
case 3:
case 6:
case 7:
cout << "K6 Model " << model;
break;
case 8:
cout << "K6-2 Model 8";
break;
case 9:
cout << "K6-III Model 9";
break;
default:
cout << "K5/K6 Model " << model;
break;
}
break;
case 6:
switch (model) {
case 1:
case 2:
case 4:
cout << "Athlon Model " << model;
break;
case 3:
cout << "Duron Model 3";
break;
case 6:
cout << "Athlon MP/Mobile Athlon Model 6";
break;
case 7:
cout << "Mobile Duron Model 7";
break;
default:
cout << "Duron/Athlon Model " << model;
break;
}
break;
default:
cout << "Unknown";
break;
}
cout << "]\n";
cpuid(0x80000000, extended, unused, unused, unused);
if (extended == 0) {
return 0;
}
if (extended >= 0x80000002) {
unsigned int j;
cout << "Detected Processor Name: ";
for (j = 0x80000002; j <= 0x80000004; j++) {
cpuid(j, eax, ebx, ecx, edx);
registers(eax, ebx, ecx, edx);
}
cout << "\n";
}
if (extended >= 0x80000007) {
cpuid(0x80000007, unused, unused, unused, edx);
if (edx & 1) {
cout << "Temperature Sensing Diode Detected!\n";
}
}
cout << "Stepping: " << stepping << " Reserved: " << reserved << "\n";
return 0;
}
/* Intel Specific brand list */
const char *Intel[] = {
"Brand ID Not Supported.",
"Intel(R) Celeron(R) processor",
"Intel(R) Pentium(R) III processor",
"Intel(R) Pentium(R) III Xeon(R) processor",
"Intel(R) Pentium(R) III processor",
"Reserved",
"Mobile Intel(R) Pentium(R) III processor-M",
"Mobile Intel(R) Celeron(R) processor",
"Intel(R) Pentium(R) 4 processor",
"Intel(R) Pentium(R) 4 processor",
"Intel(R) Celeron(R) processor",
"Intel(R) Xeon(R) Processor",
"Intel(R) Xeon(R) processor MP",
"Reserved",
"Mobile Intel(R) Pentium(R) 4 processor-M",
"Mobile Intel(R) Pentium(R) Celeron(R) processor",
"Reserved",
"Mobile Genuine Intel(R) processor",
"Intel(R) Celeron(R) M processor",
"Mobile Intel(R) Celeron(R) processor",
"Intel(R) Celeron(R) processor",
"Mobile Geniune Intel(R) processor",
"Intel(R) Pentium(R) M processor",
"Mobile Intel(R) Celeron(R) processor"
};
/* This table is for those brand strings that have two values depending on the processor signature.
* It should have the same number of entries as the above table. */
const char *Intel_Other[] = {
"Reserved",
"Reserved",
"Reserved",
"Intel(R) Celeron(R) processor",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Intel(R) Xeon(R) processor MP",
"Reserved",
"Reserved",
"Intel(R) Xeon(R) processor",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved",
"Reserved"
};
// Intel-specific information
int intel(void) {
cout << "Intel Specific Features:\n";
int eax, ebx, ecx, edx, max_eax, signature, unused;
int model, family, type, brand, stepping, reserved;
int extended_family = -1;
cpuid(1, eax, ebx, unused, unused);
model = (eax >> 4) & 0xf;
family = (eax >> 8) & 0xf;
type = (eax >> 12) & 0x3;
brand = ebx & 0xff;
stepping = eax & 0xf;
reserved = eax >> 14;
signature = eax;
cout << "Type " << type << " - ";
switch (type) {
case 0:
cout << "Original OEM";
break;
case 1:
cout << "Overdrive";
break;
case 2:
cout << "Dual-capable";
break;
case 3:
cout << "Reserved";
break;
default:
cout << "Unknown";
break;
}
cout << "\n";
cout << "Family " << family << " - ";
switch (family) {
case 3:
cout << "i386";
break;
case 4:
cout << "i486";
break;
case 5:
cout << "Pentium";
break;
case 6:
cout << "Pentium Pro";
break;
case 15:
cout << "Pentium 4";
break;
default:
cout << "Unknown";
break;
}
cout << "\n";
if (family == 15) {
extended_family = (eax >> 20) & 0xff;
cout << "Extended family " << extended_family << "\n";
}
cout << "Model " << model << " - ";
switch (family) {
case 3:
break;
case 4:
switch (model) {
case 0:
case 1:
cout << "DX";
break;
case 2:
cout << "SX";
break;
case 3:
cout << "487/DX2";
break;
case 4:
cout << "SL";
break;
case 5:
cout << "SX2";
break;
case 7:
cout << "Write-back enhanced DX2";
break;
case 8:
cout << "DX4";
break;
default:
cout << "Unknown";
break;
}
break;
case 5:
switch (model) {
case 1:
cout << "60/66";
break;
case 2:
cout << "75-200";
break;
case 3:
cout << "for 486 system";
break;
case 4:
cout << "MMX";
break;
default:
cout << "Unknown";
break;
}
break;
case 6:
switch (model) {
case 1:
cout << "Pentium Pro";
break;
case 3:
cout << "Pentium II Model 3";
break;
case 5:
cout << "Pentium II Model 5/Xeon/Celeron";
break;
case 6:
cout << "Celeron";
break;
case 7:
cout << "Pentium III/Pentium III Xeon - external L2 cache";
break;
case 8:
cout << "Pentium III/Pentium III Xeon - internal L2 cache";
break;
case 9:
cout << "Pentium M/Celeron M";
break;
case 10:
cout << "Pentium III Xeon";
break;
case 11:
cout << "Pentium III/Celeron";
break;
case 13:
cout << "Pentium M";
break;
default:
cout << "Unknown";
break;
}
break;
case 15:
break;
default:
cout << "Unknown";
break;
}
cout << "\n";
cout << "Stepping: " << stepping << "\n";
cout << "Reserved: " << reserved << "\n";
cpuid(0x80000000, max_eax, unused, unused, unused);
/* Quok said: If the max extended eax value is high enough to support the processor brand string
(values 0x80000002 to 0x80000004), then we'll use that information to return the brand information.
Otherwise, we'll refer back to the brand tables above for backwards compatibility with older processors.
According to the Sept. 2006 Intel Arch Software Developer's Guide, if extended eax values are supported,
then all 3 values for the processor brand string are supported, but we'll test just to make sure and be safe. */
if (max_eax >= 0x80000004) {
cout << "Brand: ";
if (max_eax >= 0x80000002) {
cpuid(0x80000002, eax, ebx, ecx, edx);
registers(eax, ebx, ecx, edx);
}
if (max_eax >= 0x80000003) {
cpuid(0x80000003, eax, ebx, ecx, edx);
registers(eax, ebx, ecx, edx);
}
if (max_eax >= 0x80000004) {
cpuid(0x80000004, eax, ebx, ecx, edx);
registers(eax, ebx, ecx, edx);
}
cout << "\n";
} else if (brand > 0) {
cout << "Brand %d - " << brand;
if (brand < 0x18) {
if (signature == 0x000006B1 || signature == 0x00000F13) {
cout << "Intel_Other[brand]\n";
} else {
cout << "Intel[brand]\n";
}
} else {
cout << "Reserved\n";
}
}
return 0;
}