-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpuid.c
More file actions
132 lines (115 loc) · 3.2 KB
/
cpuid.c
File metadata and controls
132 lines (115 loc) · 3.2 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define arr_sze(X) (sizeof(X) / sizeof(*(X)))
#if __x86_64__
#define REG "r"
#else
#define REG "e"
#endif
static uint32_t swap_endianness(uint32_t in)
{
union { uint32_t val; uint8_t c[4]; } tmp;
union { uint32_t val; uint8_t c[4]; } out;
tmp.val = in;
out.c[0] = tmp.c[3];
out.c[1] = tmp.c[2];
out.c[2] = tmp.c[1];
out.c[3] = tmp.c[0];
return out.val;
}
#define W(U32) (swap_endianness(*((uint32_t*)(U32))))
#define S(U32, Idx) ((uint16_t) ((W(U32) >> ((Idx) * 16)) & 0xffff))
#define B(U32, Idx) (*(((uint8_t*)(U32)) + (Idx)))
#define EAX ((char*)(val.i + 3))
#define EBX ((char*)(val.i + 0))
#define ECX ((char*)(val.i + 2))
#define EDX ((char*)(val.i + 1))
#define H8 "0x%02hhX"
#define H32 "0x%08X"
#define BE(X) (swap_endianness(X))
int main(int argc, char** argv)
{
static union {
uint8_t c[16];
uint32_t i[4];
} val;
uint32_t cpuid_arg = 0;
argv++;
if (argc <= 1)
goto default_opt;
while (*argv) {
cpuid_arg = strtoul(*argv, NULL, 0);
argv++;
default_opt:
__asm__ __volatile__("mov %%" REG "bx, %%" REG "si\n\t"
"cpuid\n\t"
"mov %%ebx, (%%" REG "si)\n\t"
"mov %%edx, 4(%%" REG "si)\n\t"
"mov %%ecx, 8(%%" REG "si)\n\t"
"mov %%eax, 12(%%" REG "si)\n\t"
: : "a"(cpuid_arg), "b" (&val)
: "ecx", "edx", REG "si" );
switch (cpuid_arg) {
case 0x0:
printf("%.12s\n0x%X\n", val.c, BE(val.i[3]));
break;
case 0x1:
printf(
"Version Information: " H32 "\n"
"Brand Index: " H8 "\n"
"CLFLUSH line size: " H8 "\n"
"Number of logical processor per physical core: " H8 "\n"
"Local APIC ID: " H8 "\n"
"Extended Feature informations: " H32 "\n"
"Feature Information: " H32 "\n",
W(EAX), B(EBX, 0), B(EBX, 1), B(EBX, 2), B(EBX, 3),
W(ECX), W(EDX));
break;
case 0xA:
printf(
"Version ID of architectural performance monitoring: " H8 "\n"
"General-purpose performance monitoring counter per logical processor: " H8 "\n"
"Bit width of general-purpose, performance monitoring counter: " H8 "\n"
"Length of EBX bit vector to enumerate architectural performance monitoring events: " H8 "\n"
, B(EAX, 0), B(EAX, 1), B(EAX, 2), B(EAX, 3));
const char* ebx_str[] = {
"Core cycle",
"Instruction retired",
"Reference cycles",
"Last-level cache reference",
"Last-level cache misses",
"Branch instruction retired",
"Branch mispredict retired",
};
for (size_t i = 0; i < B(EAX, 3) && i < 32; i++) {
if (i < arr_sze(ebx_str))
printf("%s", ebx_str[i]);
else
printf("%zu", i);
printf(" event%s available\n",
((1<<i) & W(EBX)) ? " NOT" : "");
}
if (B(EAX, 0) > 1)
printf("Number of fixed-function performance counters: %d\n"
"Bit width of fixed-function performance counters: %d\n",
B(EDX, 0) & 0xf, B(EDX, 0) & 0xf);
printf("AnyThread deprecation: %d\n", !!(W(EBX) & (1<<15)));
break;
case 0x80000002:
case 0x80000003:
case 0x80000004:
printf("%.4s%.4s%.4s%.4s\n", EAX, EBX, ECX, EDX);
break;
default:
printf(
"cpuid(" H32 "): "
"eax: " H32 ", "
"ebx: " H32 ", "
"ecx: " H32 ", "
"edx: " H32 "\n",
cpuid_arg, W(EAX), W(EBX), W(ECX), W(EDX));
}
}
return 0;
}