njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame^] | 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 | // {x86,amd64}-linux (part 1 of 2) |
| 22 | //--------------------------------------------------------------------------- |
| 23 | #if defined(VGP_x86_linux) || defined(VGP_amd64_linux) |
| 24 | static void cpuid ( unsigned int n, |
| 25 | unsigned int* a, unsigned int* b, |
| 26 | unsigned int* c, unsigned int* d ) |
| 27 | { |
| 28 | __asm__ __volatile__ ( |
| 29 | "cpuid" |
| 30 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 31 | : "0" (n) /* input */ |
| 32 | ); |
| 33 | } |
| 34 | #endif // VGP_x86_linux || VGP_amd64_linux |
| 35 | |
| 36 | //--------------------------------------------------------------------------- |
| 37 | // {x86,amd64}-linux (part 2 of 2) |
| 38 | //--------------------------------------------------------------------------- |
| 39 | #if defined(VGA_x86) || defined(VGA_amd64) |
| 40 | static Bool go(char* cpu) |
| 41 | { |
| 42 | unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d; |
| 43 | |
| 44 | if ( strcmp( cpu, "x86-fpu" ) == 0 ) { |
| 45 | level = 1; |
| 46 | dmask = 1 << 0; |
| 47 | } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) { |
| 48 | level = 1; |
| 49 | dmask = 1 << 15; |
| 50 | } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) { |
| 51 | level = 1; |
| 52 | dmask = 1 << 23; |
| 53 | } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) { |
| 54 | level = 0x80000001; |
| 55 | dmask = 1 << 22; |
| 56 | } else if ( strcmp( cpu, "x86-sse" ) == 0 ) { |
| 57 | level = 1; |
| 58 | dmask = 1 << 25; |
| 59 | } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) { |
| 60 | level = 1; |
| 61 | dmask = 1 << 26; |
| 62 | } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) { |
| 63 | level = 1; |
| 64 | cmask = 1 << 0; |
| 65 | } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) { |
| 66 | level = 1; |
| 67 | cmask = 1 << 9; |
| 68 | #if defined(VGA_amd64) |
| 69 | } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) { |
| 70 | level = 1; |
| 71 | cmask = 1 << 0; |
| 72 | } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) { |
| 73 | level = 1; |
| 74 | cmask = 1 << 9; |
| 75 | #endif |
| 76 | } else { |
| 77 | return 2; // Unrecognised feature. |
| 78 | } |
| 79 | |
| 80 | assert( !(cmask != 0 && dmask != 0) ); |
| 81 | assert( !(cmask == 0 && dmask == 0) ); |
| 82 | |
| 83 | cpuid( level & 0x80000000, &a, &b, &c, &d ); |
| 84 | |
| 85 | if ( a >= level ) { |
| 86 | cpuid( level, &a, &b, &c, &d ); |
| 87 | |
| 88 | if (dmask > 0 && (d & dmask) != 0) return 0; // Feature present. |
| 89 | if (cmask > 0 && (c & cmask) != 0) return 0; // Feature present. |
| 90 | } |
| 91 | return 1; // Feature not present. |
| 92 | } |
| 93 | |
| 94 | #else // defined(VGA_x86) || defined(VGA_amd64) |
| 95 | |
| 96 | static Bool go(char* cpu) |
| 97 | { |
| 98 | return 2; // Feature not recognised (non-x86/AMD64 machine!) |
| 99 | } |
| 100 | |
| 101 | #endif // defined(VGA_x86) || defined(VGA_amd64) |
| 102 | |
| 103 | |
| 104 | //--------------------------------------------------------------------------- |
| 105 | // main |
| 106 | //--------------------------------------------------------------------------- |
| 107 | int main(int argc, char **argv) |
| 108 | { |
| 109 | if ( argc != 2 ) { |
| 110 | fprintf( stderr, "usage: x86_amd64_features <feature>\n" ); |
| 111 | exit(3); // Usage error. |
| 112 | } |
| 113 | return go(argv[1]); |
| 114 | } |