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). |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 15 | // viz: |
| 16 | #define FEATURE_PRESENT 0 |
| 17 | #define FEATURE_NOT_PRESENT 1 |
| 18 | #define UNRECOGNISED_FEATURE 2 |
sewardj | 9670e8a | 2012-06-05 00:30:24 +0000 | [diff] [blame] | 19 | #define USAGE_ERROR 3 |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 20 | |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 21 | |
| 22 | #define False 0 |
| 23 | #define True 1 |
| 24 | typedef int Bool; |
| 25 | |
njn | 3ea5b0b | 2009-05-18 06:23:25 +0000 | [diff] [blame] | 26 | |
| 27 | #if defined(VGA_x86) || defined(VGA_amd64) |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 28 | static void cpuid ( unsigned int n, |
| 29 | unsigned int* a, unsigned int* b, |
| 30 | unsigned int* c, unsigned int* d ) |
| 31 | { |
| 32 | __asm__ __volatile__ ( |
| 33 | "cpuid" |
| 34 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 35 | : "0" (n) /* input */ |
| 36 | ); |
| 37 | } |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 38 | |
| 39 | static Bool vendorStringEquals ( char* str ) |
| 40 | { |
| 41 | char vstr[13]; |
| 42 | unsigned int a, b, c, d; |
| 43 | cpuid(0, &a, &b, &c, &d); |
| 44 | memcpy(&vstr[0], &b, 4); |
| 45 | memcpy(&vstr[4], &d, 4); |
| 46 | memcpy(&vstr[8], &c, 4); |
| 47 | vstr[12] = 0; |
| 48 | return 0 == strcmp(vstr, str); |
| 49 | } |
| 50 | |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 51 | static Bool have_xgetbv ( void ) |
| 52 | { |
| 53 | #if defined(VGA_amd64) |
| 54 | unsigned long long int w; |
| 55 | __asm__ __volatile__("movq $0,%%rcx ; " |
| 56 | ".byte 0x0F,0x01,0xD0 ; " /* xgetbv */ |
| 57 | "movq %%rax,%0" |
| 58 | :/*OUT*/"=r"(w) :/*IN*/ |
| 59 | :/*TRASH*/"rdx","rcx"); |
| 60 | if ((w & 6) == 6) { |
| 61 | /* OS has enabled both XMM and YMM state support */ |
| 62 | return True; |
| 63 | } else { |
| 64 | return False; |
| 65 | } |
| 66 | #else |
| 67 | return False; |
| 68 | #endif |
| 69 | } |
| 70 | |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 71 | static Bool go(char* cpu) |
| 72 | { |
| 73 | unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d; |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 74 | Bool require_amd = False; |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 75 | Bool require_xgetbv = False; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 76 | if ( strcmp( cpu, "x86-fpu" ) == 0 ) { |
| 77 | level = 1; |
| 78 | dmask = 1 << 0; |
| 79 | } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) { |
| 80 | level = 1; |
| 81 | dmask = 1 << 15; |
| 82 | } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) { |
| 83 | level = 1; |
| 84 | dmask = 1 << 23; |
| 85 | } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) { |
| 86 | level = 0x80000001; |
| 87 | dmask = 1 << 22; |
| 88 | } else if ( strcmp( cpu, "x86-sse" ) == 0 ) { |
| 89 | level = 1; |
| 90 | dmask = 1 << 25; |
| 91 | } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) { |
| 92 | level = 1; |
| 93 | dmask = 1 << 26; |
| 94 | } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) { |
| 95 | level = 1; |
| 96 | cmask = 1 << 0; |
| 97 | } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) { |
| 98 | level = 1; |
| 99 | cmask = 1 << 9; |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 100 | } else if ( strcmp( cpu, "x86-lzcnt" ) == 0 ) { |
| 101 | level = 0x80000001; |
| 102 | cmask = 1 << 5; |
| 103 | require_amd = True; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 104 | #if defined(VGA_amd64) |
| 105 | } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) { |
| 106 | level = 1; |
| 107 | cmask = 1 << 0; |
sewardj | e3ae8a3 | 2010-09-28 19:59:47 +0000 | [diff] [blame] | 108 | } else if ( strcmp( cpu, "amd64-pclmulqdq" ) == 0 ) { |
| 109 | level = 1; |
| 110 | cmask = 1 << 1; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 111 | } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) { |
| 112 | level = 1; |
| 113 | cmask = 1 << 9; |
sewardj | a01ed5e | 2009-07-12 13:17:18 +0000 | [diff] [blame] | 114 | } else if ( strcmp( cpu, "amd64-cx16" ) == 0 ) { |
| 115 | level = 1; |
| 116 | cmask = 1 << 13; |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 117 | } else if ( strcmp( cpu, "amd64-lzcnt" ) == 0 ) { |
| 118 | level = 0x80000001; |
| 119 | cmask = 1 << 5; |
| 120 | require_amd = True; |
sewardj | 27d176a | 2011-01-17 11:15:48 +0000 | [diff] [blame] | 121 | } else if ( strcmp( cpu, "amd64-sse42" ) == 0 ) { |
| 122 | level = 1; |
| 123 | cmask = 1 << 20; |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 124 | } else if ( strcmp( cpu, "amd64-avx" ) == 0 ) { |
| 125 | level = 1; |
| 126 | cmask = (1 << 20) | (1 << 28); |
| 127 | require_xgetbv = True; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 128 | #endif |
| 129 | } else { |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 130 | return UNRECOGNISED_FEATURE; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | assert( !(cmask != 0 && dmask != 0) ); |
| 134 | assert( !(cmask == 0 && dmask == 0) ); |
| 135 | |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 136 | if (require_amd && !vendorStringEquals("AuthenticAMD")) |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 137 | return FEATURE_NOT_PRESENT; |
sewardj | 3cf36a9 | 2010-07-30 15:07:42 +0000 | [diff] [blame] | 138 | // regardless of what that feature actually is |
| 139 | |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 140 | cpuid( level & 0x80000000, &a, &b, &c, &d ); |
| 141 | |
| 142 | if ( a >= level ) { |
| 143 | cpuid( level, &a, &b, &c, &d ); |
| 144 | |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 145 | if (dmask > 0 && (d & dmask) == dmask) { |
| 146 | if (require_xgetbv && !have_xgetbv()) |
| 147 | return FEATURE_NOT_PRESENT; |
| 148 | else |
| 149 | return FEATURE_PRESENT; |
| 150 | } |
sewardj | 9670e8a | 2012-06-05 00:30:24 +0000 | [diff] [blame] | 151 | if (cmask > 0 && (c & cmask) == cmask) { |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 152 | if (require_xgetbv && !have_xgetbv()) |
| 153 | return FEATURE_NOT_PRESENT; |
| 154 | else |
| 155 | return FEATURE_PRESENT; |
| 156 | } |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 157 | } |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 158 | return FEATURE_NOT_PRESENT; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 159 | } |
| 160 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 161 | #else |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 162 | |
| 163 | static Bool go(char* cpu) |
| 164 | { |
sewardj | bedf990 | 2012-06-04 07:39:30 +0000 | [diff] [blame] | 165 | // Feature not recognised (non-x86/AMD64 machine!) |
| 166 | return UNRECOGNISED_FEATURE; |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #endif // defined(VGA_x86) || defined(VGA_amd64) |
| 170 | |
| 171 | |
| 172 | //--------------------------------------------------------------------------- |
| 173 | // main |
| 174 | //--------------------------------------------------------------------------- |
| 175 | int main(int argc, char **argv) |
| 176 | { |
| 177 | if ( argc != 2 ) { |
| 178 | fprintf( stderr, "usage: x86_amd64_features <feature>\n" ); |
sewardj | 9670e8a | 2012-06-05 00:30:24 +0000 | [diff] [blame] | 179 | exit(USAGE_ERROR); |
njn | 54cd2af | 2009-03-04 04:18:33 +0000 | [diff] [blame] | 180 | } |
| 181 | return go(argv[1]); |
| 182 | } |