]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/tests/x86_amd64_features.c
aaa2a55cc42dfdbba392a0f022f28a9f2ca7115e
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / tests / x86_amd64_features.c
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)
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 static Bool go(char* cpu)
33
34    unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d;
35
36    if        ( strcmp( cpu, "x86-fpu" ) == 0 ) {
37      level = 1;
38      dmask = 1 << 0;
39    } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) {
40      level = 1;
41      dmask = 1 << 15;
42    } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) {
43      level = 1;
44      dmask = 1 << 23;
45    } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) {
46      level = 0x80000001;
47      dmask = 1 << 22;
48    } else if ( strcmp( cpu, "x86-sse" ) == 0 ) {
49      level = 1;
50      dmask = 1 << 25;
51    } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) {
52      level = 1;
53      dmask = 1 << 26;
54    } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) {
55      level = 1;
56      cmask = 1 << 0;
57    } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) {
58      level = 1;
59      cmask = 1 << 9;
60 #if defined(VGA_amd64)
61    } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) {
62      level = 1;
63      cmask = 1 << 0;
64    } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) {
65      level = 1;
66      cmask = 1 << 9;
67    } else if ( strcmp( cpu, "amd64-cx16" ) == 0 ) {
68      level = 1;
69      cmask = 1 << 13;
70 #endif
71    } else {
72      return 2;          // Unrecognised feature.
73    }
74
75    assert( !(cmask != 0 && dmask != 0) );
76    assert( !(cmask == 0 && dmask == 0) );
77
78    cpuid( level & 0x80000000, &a, &b, &c, &d );
79
80    if ( a >= level ) {
81       cpuid( level, &a, &b, &c, &d );
82
83       if (dmask > 0 && (d & dmask) != 0) return 0;    // Feature present.
84       if (cmask > 0 && (c & cmask) != 0) return 0;    // Feature present.
85    }
86    return 1;                                          // Feature not present.
87 }
88
89 #else
90
91 static Bool go(char* cpu)
92 {
93    return 2;      // Feature not recognised (non-x86/AMD64 machine!)
94 }
95
96 #endif   // defined(VGA_x86)  || defined(VGA_amd64)
97
98
99 //---------------------------------------------------------------------------
100 // main
101 //---------------------------------------------------------------------------
102 int main(int argc, char **argv)
103 {
104    if ( argc != 2 ) {
105       fprintf( stderr, "usage: x86_amd64_features <feature>\n" );
106       exit(3);                // Usage error.
107    }
108    return go(argv[1]);
109 }