7 // This file determines x86/AMD64 features a processor supports.
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).
21 #if defined(VGA_x86) || defined(VGA_amd64)
22 static void cpuid ( unsigned int n,
23 unsigned int* a, unsigned int* b,
24 unsigned int* c, unsigned int* d )
26 __asm__ __volatile__ (
28 : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */
33 static Bool vendorStringEquals ( char* str )
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);
42 return 0 == strcmp(vstr, str);
45 static Bool go(char* cpu)
47 unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d;
48 Bool require_amd = False;
50 if ( strcmp( cpu, "x86-fpu" ) == 0 ) {
53 } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) {
56 } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) {
59 } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) {
62 } else if ( strcmp( cpu, "x86-sse" ) == 0 ) {
65 } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) {
68 } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) {
71 } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) {
74 } else if ( strcmp( cpu, "x86-lzcnt" ) == 0 ) {
78 #if defined(VGA_amd64)
79 } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) {
82 } else if ( strcmp( cpu, "amd64-pclmulqdq" ) == 0 ) {
85 } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) {
88 } else if ( strcmp( cpu, "amd64-cx16" ) == 0 ) {
91 } else if ( strcmp( cpu, "amd64-lzcnt" ) == 0 ) {
95 } else if ( strcmp( cpu, "amd64-sse42" ) == 0 ) {
100 return 2; // Unrecognised feature.
103 assert( !(cmask != 0 && dmask != 0) );
104 assert( !(cmask == 0 && dmask == 0) );
106 if (require_amd && !vendorStringEquals("AuthenticAMD"))
107 return 1; // Feature not present
108 // regardless of what that feature actually is
110 cpuid( level & 0x80000000, &a, &b, &c, &d );
113 cpuid( level, &a, &b, &c, &d );
115 if (dmask > 0 && (d & dmask) != 0) return 0; // Feature present.
116 if (cmask > 0 && (c & cmask) != 0) return 0; // Feature present.
118 return 1; // Feature not present.
123 static Bool go(char* cpu)
125 return 2; // Feature not recognised (non-x86/AMD64 machine!)
128 #endif // defined(VGA_x86) || defined(VGA_amd64)
131 //---------------------------------------------------------------------------
133 //---------------------------------------------------------------------------
134 int main(int argc, char **argv)
137 fprintf( stderr, "usage: x86_amd64_features <feature>\n" );
138 exit(3); // Usage error.