nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 4 | #include <assert.h> |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 5 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 6 | // This file determines which architectures that this Valgrind installation |
| 7 | // supports, which depends on the machine's architecture. It also depends |
| 8 | // on the configuration options; for example, if Valgrind is installed on |
| 9 | // an AMD64 machine but has been configured with --enable-only32bit then |
| 10 | // this program will not match "amd64". |
| 11 | // |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 12 | // We return: |
| 13 | // - 0 if the machine matches the asked-for cpu |
| 14 | // - 1 if it didn't match, but did match the name of another arch |
| 15 | // - 2 otherwise |
| 16 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 17 | // Nb: When updating this file for a new architecture, add the name to |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 18 | // 'all_archs' as well as adding go(). |
| 19 | |
| 20 | #define False 0 |
| 21 | #define True 1 |
| 22 | typedef int Bool; |
| 23 | |
| 24 | char* all_archs[] = { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 25 | "amd64", |
njn | 10c122e | 2005-09-13 00:45:14 +0000 | [diff] [blame] | 26 | "ppc32", |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 27 | "ppc64", |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 28 | "x86", |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 29 | NULL |
| 30 | }; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 31 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 32 | //----------------------------------------------------------------------------- |
| 33 | // ppc32-linux |
| 34 | //--------------------------------------------------------------------------- |
| 35 | #if defined(VGP_ppc32_linux) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 36 | static Bool go(char* cpu) |
| 37 | { |
sewardj | fcbb942 | 2005-11-10 23:30:21 +0000 | [diff] [blame] | 38 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 39 | return True; |
sewardj | 87f7fdd | 2006-01-04 03:08:16 +0000 | [diff] [blame] | 40 | return False; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 41 | } |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 42 | #endif // VGP_ppc32_linux |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 43 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 44 | //--------------------------------------------------------------------------- |
| 45 | // ppc64-linux |
| 46 | //--------------------------------------------------------------------------- |
| 47 | #if defined(VGP_ppc64_linux) |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 48 | static Bool go(char* cpu) |
| 49 | { |
| 50 | if ( strcmp( cpu, "ppc64" ) == 0 ) |
| 51 | return True; |
sewardj | 87f7fdd | 2006-01-04 03:08:16 +0000 | [diff] [blame] | 52 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
| 53 | return True; |
| 54 | return False; |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 55 | } |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 56 | #endif // VGP_ppc64_linux |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 57 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 58 | //--------------------------------------------------------------------------- |
| 59 | // ppc{32,64}-aix |
| 60 | //--------------------------------------------------------------------------- |
| 61 | #if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
sewardj | be714ff | 2006-10-17 01:49:50 +0000 | [diff] [blame] | 62 | static Bool go(char* cpu) |
| 63 | { |
| 64 | if (sizeof(void*) == 8) { |
| 65 | /* cpu is in 64-bit mode */ |
| 66 | if ( strcmp( cpu, "ppc64" ) == 0 ) |
| 67 | return True; |
| 68 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
| 69 | return True; |
| 70 | } else { |
| 71 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
| 72 | return True; |
| 73 | } |
| 74 | return False; |
| 75 | } |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 76 | #endif // VGP_ppc32_aix5 || VGP_ppc64_aix5 |
sewardj | be714ff | 2006-10-17 01:49:50 +0000 | [diff] [blame] | 77 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 78 | //--------------------------------------------------------------------------- |
| 79 | // {x86,amd64}-linux (part 1 of 2) |
| 80 | //--------------------------------------------------------------------------- |
| 81 | #if defined(VGP_x86_linux) || defined(VGP_amd64_linux) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 82 | static void cpuid ( unsigned int n, |
| 83 | unsigned int* a, unsigned int* b, |
| 84 | unsigned int* c, unsigned int* d ) |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 85 | { |
| 86 | __asm__ __volatile__ ( |
| 87 | "cpuid" |
| 88 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 89 | : "0" (n) /* input */ |
| 90 | ); |
| 91 | } |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 92 | #endif // VGP_x86_linux || VGP_amd64_linux |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 93 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 94 | //--------------------------------------------------------------------------- |
| 95 | // {x86,amd64}-{linux} (part 2 of 2) |
| 96 | //--------------------------------------------------------------------------- |
| 97 | #if defined(VGP_x86_linux) || defined(VGP_amd64_linux) |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 98 | static Bool go(char* cpu) |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 99 | { |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 100 | unsigned int level = 0, cmask = 0, dmask = 0, a, b, c, d; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 101 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 102 | if ( strcmp( cpu, "x86" ) == 0 ) { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 103 | return True; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 104 | } else if ( strcmp( cpu, "x86-fpu" ) == 0 ) { |
| 105 | level = 1; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 106 | dmask = 1 << 0; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 107 | } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) { |
| 108 | level = 1; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 109 | dmask = 1 << 15; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 110 | } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) { |
| 111 | level = 1; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 112 | dmask = 1 << 23; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 113 | } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) { |
| 114 | level = 0x80000001; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 115 | dmask = 1 << 22; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 116 | } else if ( strcmp( cpu, "x86-sse" ) == 0 ) { |
| 117 | level = 1; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 118 | dmask = 1 << 25; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 119 | } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) { |
| 120 | level = 1; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 121 | dmask = 1 << 26; |
| 122 | } else if ( strcmp( cpu, "x86-sse3" ) == 0 ) { |
| 123 | level = 1; |
| 124 | cmask = 1 << 0; |
tom | a00febc | 2009-01-12 15:26:59 +0000 | [diff] [blame] | 125 | } else if ( strcmp( cpu, "x86-ssse3" ) == 0 ) { |
| 126 | level = 1; |
| 127 | cmask = 1 << 9; |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 128 | #if defined(VGA_amd64) |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame] | 129 | } else if ( strcmp( cpu, "amd64" ) == 0 ) { |
| 130 | return True; |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 131 | } else if ( strcmp( cpu, "amd64-sse3" ) == 0 ) { |
| 132 | level = 1; |
| 133 | cmask = 1 << 0; |
tom | a00febc | 2009-01-12 15:26:59 +0000 | [diff] [blame] | 134 | } else if ( strcmp( cpu, "amd64-ssse3" ) == 0 ) { |
| 135 | level = 1; |
| 136 | cmask = 1 << 9; |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame] | 137 | #endif |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 138 | } else { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 139 | return False; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 140 | } |
| 141 | |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 142 | assert( !(cmask != 0 && dmask != 0) ); |
| 143 | assert( !(cmask == 0 && dmask == 0) ); |
| 144 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 145 | cpuid( level & 0x80000000, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 146 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 147 | if ( a >= level ) { |
| 148 | cpuid( level, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 149 | |
sewardj | c49ae75 | 2006-08-05 12:22:52 +0000 | [diff] [blame] | 150 | if (dmask > 0 && (d & dmask) != 0) return True; |
| 151 | if (cmask > 0 && (c & cmask) != 0) return True; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 152 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 153 | return False; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 154 | } |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 155 | #endif // VGP_x86_linux || VGP_amd64_linux |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 156 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 157 | |
njn | 7fd6d38 | 2009-01-22 21:56:32 +0000 | [diff] [blame^] | 158 | //--------------------------------------------------------------------------- |
| 159 | // main |
| 160 | //--------------------------------------------------------------------------- |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 161 | int main(int argc, char **argv) |
| 162 | { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 163 | int i; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 164 | if ( argc != 2 ) { |
| 165 | fprintf( stderr, "usage: cputest <cpu-type>\n" ); |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 166 | exit( 2 ); |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 167 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 168 | if (go( argv[1] )) { |
| 169 | return 0; // matched |
| 170 | } |
| 171 | for (i = 0; NULL != all_archs[i]; i++) { |
| 172 | if ( strcmp( argv[1], all_archs[i] ) == 0 ) |
| 173 | return 1; |
| 174 | } |
| 175 | return 2; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 176 | } |