forked from pytorch/cpuinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpuid-dump.c
128 lines (121 loc) · 3.89 KB
/
cpuid-dump.c
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
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <x86/cpuid.h>
static void print_cpuid(struct cpuid_regs regs, uint32_t eax) {
printf("CPUID %08"PRIX32": %08"PRIX32"-%08"PRIX32"-%08"PRIX32"-%08"PRIX32"\n",
eax, regs.eax, regs.ebx, regs.ecx, regs.edx);
}
static void print_cpuidex(struct cpuid_regs regs, uint32_t eax, uint32_t ecx) {
printf("CPUID %08"PRIX32": %08"PRIX32"-%08"PRIX32"-%08"PRIX32"-%08"PRIX32" [SL %02"PRIX32"]\n",
eax, regs.eax, regs.ebx, regs.ecx, regs.edx, ecx);
}
static void print_cpuid_vendor(struct cpuid_regs regs, uint32_t eax) {
if (regs.ebx | regs.ecx | regs.edx) {
char vendor_id[12];
memcpy(&vendor_id[0], ®s.ebx, sizeof(regs.ebx));
memcpy(&vendor_id[4], ®s.edx, sizeof(regs.edx));
memcpy(&vendor_id[8], ®s.ecx, sizeof(regs.ecx));
printf("CPUID %08"PRIX32": %08"PRIX32"-%08"PRIX32"-%08"PRIX32"-%08"PRIX32" [%.12s]\n",
eax, regs.eax, regs.ebx, regs.ecx, regs.edx, vendor_id);
} else {
print_cpuid(regs, eax);
}
}
static void print_cpuid_brand_string(struct cpuid_regs regs, uint32_t eax) {
char brand_string[16];
memcpy(&brand_string[0], ®s.eax, sizeof(regs.eax));
memcpy(&brand_string[4], ®s.ebx, sizeof(regs.ebx));
memcpy(&brand_string[8], ®s.ecx, sizeof(regs.ecx));
memcpy(&brand_string[12], ®s.edx, sizeof(regs.edx));
printf("CPUID %08"PRIX32": %08"PRIX32"-%08"PRIX32"-%08"PRIX32"-%08"PRIX32" [%.16s]\n",
eax, regs.eax, regs.ebx, regs.ecx, regs.edx, brand_string);
}
int main(int argc, char** argv) {
const uint32_t max_base_index = cpuid(0).eax;
uint32_t max_structured_index = 0, max_trace_index = 0, max_socid_index = 0;
bool has_sgx = false;
for (uint32_t eax = 0; eax <= max_base_index; eax++) {
switch (eax) {
case UINT32_C(0x00000000):
print_cpuid_vendor(cpuid(eax), eax);
break;
case UINT32_C(0x00000004):
for (uint32_t ecx = 0; ; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if ((regs.eax & UINT32_C(0x1F)) == 0) {
break;
}
print_cpuidex(regs, eax, ecx);
}
break;
case UINT32_C(0x00000007):
for (uint32_t ecx = 0; ecx <= max_structured_index; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if (ecx == 0) {
max_structured_index = regs.eax;
has_sgx = !!(regs.ebx & UINT32_C(0x00000004));
}
print_cpuidex(regs, eax, ecx);
}
break;
case UINT32_C(0x0000000B):
for (uint32_t ecx = 0; ; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if ((regs.ecx & UINT32_C(0x0000FF00)) == 0) {
break;
}
print_cpuidex(regs, eax, ecx);
}
break;
case UINT32_C(0x00000012):
if (has_sgx) {
for (uint32_t ecx = 0; ; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if (ecx >= 2 && (regs.eax & UINT32_C(0x0000000F)) == 0) {
break;
}
print_cpuidex(regs, eax, ecx);
}
}
break;
case UINT32_C(0x00000014):
for (uint32_t ecx = 0; ecx <= max_trace_index; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if (ecx == 0) {
max_trace_index = regs.eax;
}
print_cpuidex(regs, eax, ecx);
}
break;
case UINT32_C(0x00000017):
for (uint32_t ecx = 0; ecx <= max_socid_index; ecx++) {
const struct cpuid_regs regs = cpuidex(eax, ecx);
if (ecx == 0) {
max_socid_index = regs.eax;
}
print_cpuidex(regs, eax, ecx);
}
break;
default:
print_cpuid(cpuidex(eax, 0), eax);
break;
}
}
const uint32_t max_extended_index = cpuid(UINT32_C(0x80000000)).eax;
for (uint32_t eax = UINT32_C(0x80000000); eax <= max_extended_index; eax++) {
switch (eax) {
case UINT32_C(0x80000000):
print_cpuid_vendor(cpuid(eax), eax);
break;
case UINT32_C(0x80000002):
case UINT32_C(0x80000003):
case UINT32_C(0x80000004):
print_cpuid_brand_string(cpuid(eax), eax);
break;
default:
print_cpuid(cpuidex(eax, 0), eax);
}
}
}