1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6
7 // This file determines x86/AMD64 features a processor supports.
8 //
9 // We return:
10 // - 0 if the machine matches the asked-for feature.
11 // - 1 if the machine does not.
12 // - 2 if the asked-for feature isn't recognised (this will be the case for
13 // any feature if run on a non-x86/AMD64 machine).
14 // - 3 if there was a usage error (it also prints an error message).
15
16 #define False 0
17 #define True 1
18 typedef int Bool;
19
20
21 #if defined(VGA_x86) || defined(VGA_amd64)
cpuid(unsigned int n,unsigned int * a,unsigned int * b,unsigned int * c,unsigned int * d)22 static void cpuid ( unsigned int n,
23 unsigned int* a, unsigned int* b,
24 unsigned int* c, unsigned int* d )
25 {
26 __asm__ __volatile__ (
27 "cpuid"
28 : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */
29 : "0" (n) /* input */
30 );
31 }
32
vendorStringEquals(char * str)33 static Bool vendorStringEquals ( char* str )
34 {
35 char vstr[13];
36 unsigned int a, b, c, d;
37 cpuid(0, &a, &b, &c, &d);
38 memcpy(&vstr[0], &b, 4);
39 memcpy(&vstr[4], &d, 4);
40 memcpy(&vstr[8], &c, 4);
41 vstr[12] = 0;
42 return 0 == strcmp(vstr, str);
43 }
44
go(char * cpu)45 static Bool go(char* cpu)
46 {
47 unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d;
48 Bool require_amd = False;
49
50 if ( strcmp( cpu, "x86-fpu" ) == 0 ) {
51 level = 1;
52 dmask = 1 << 0;
53 } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) {
54 level = 1;
55 dmask = 1 << 15;
56 } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) {
57 level = 1;
58 dmask = 1 << 23;
59 } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) {
60 level = 0x80000001;
61 dmask = 1 << 22;
62 } else if ( strcmp( cpu, "x86-sse" ) == 0 ) {
63 level = 1;
64 dmask = 1 << 25;
65 } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) {
66 level = 1;
67 dmask = 1 << 26;
68 } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) {
69 level = 1;
70 cmask = 1 << 0;
71 } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) {
72 level = 1;
73 cmask = 1 << 9;
74 } else if ( strcmp( cpu, "x86-lzcnt" ) == 0 ) {
75 level = 0x80000001;
76 cmask = 1 << 5;
77 require_amd = True;
78 #if defined(VGA_amd64)
79 } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) {
80 level = 1;
81 cmask = 1 << 0;
82 } else if ( strcmp( cpu, "amd64-pclmulqdq" ) == 0 ) {
83 level = 1;
84 cmask = 1 << 1;
85 } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) {
86 level = 1;
87 cmask = 1 << 9;
88 } else if ( strcmp( cpu, "amd64-cx16" ) == 0 ) {
89 level = 1;
90 cmask = 1 << 13;
91 } else if ( strcmp( cpu, "amd64-lzcnt" ) == 0 ) {
92 level = 0x80000001;
93 cmask = 1 << 5;
94 require_amd = True;
95 #endif
96 } else {
97 return 2; // Unrecognised feature.
98 }
99
100 assert( !(cmask != 0 && dmask != 0) );
101 assert( !(cmask == 0 && dmask == 0) );
102
103 if (require_amd && !vendorStringEquals("AuthenticAMD"))
104 return 1; // Feature not present
105 // regardless of what that feature actually is
106
107 cpuid( level & 0x80000000, &a, &b, &c, &d );
108
109 if ( a >= level ) {
110 cpuid( level, &a, &b, &c, &d );
111
112 if (dmask > 0 && (d & dmask) != 0) return 0; // Feature present.
113 if (cmask > 0 && (c & cmask) != 0) return 0; // Feature present.
114 }
115 return 1; // Feature not present.
116 }
117
118 #else
119
go(char * cpu)120 static Bool go(char* cpu)
121 {
122 return 2; // Feature not recognised (non-x86/AMD64 machine!)
123 }
124
125 #endif // defined(VGA_x86) || defined(VGA_amd64)
126
127
128 //---------------------------------------------------------------------------
129 // main
130 //---------------------------------------------------------------------------
main(int argc,char ** argv)131 int main(int argc, char **argv)
132 {
133 if ( argc != 2 ) {
134 fprintf( stderr, "usage: x86_amd64_features <feature>\n" );
135 exit(3); // Usage error.
136 }
137 return go(argv[1]);
138 }
139