nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 5 | // We return: |
| 6 | // - 0 if the machine matches the asked-for cpu |
| 7 | // - 1 if it didn't match, but did match the name of another arch |
| 8 | // - 2 otherwise |
| 9 | |
| 10 | // When updating this file for a new architecture, add the name to |
| 11 | // 'all_archs' as well as adding go(). |
| 12 | |
| 13 | #define False 0 |
| 14 | #define True 1 |
| 15 | typedef int Bool; |
| 16 | |
| 17 | char* all_archs[] = { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 18 | "amd64", |
njn | 10c122e | 2005-09-13 00:45:14 +0000 | [diff] [blame] | 19 | "ppc32", |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 20 | "x86", |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 21 | NULL |
| 22 | }; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 23 | |
sewardj | 5c52298 | 2005-06-21 10:11:36 +0000 | [diff] [blame] | 24 | #ifdef __powerpc__ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 25 | static Bool go(char* cpu) |
| 26 | { |
sewardj | fcbb942 | 2005-11-10 23:30:21 +0000 | [diff] [blame] | 27 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 28 | return True; |
| 29 | else |
| 30 | return False; |
| 31 | } |
sewardj | 5c52298 | 2005-06-21 10:11:36 +0000 | [diff] [blame] | 32 | #endif // __powerpc__ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 33 | |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame^] | 34 | #if defined(__i386__) || defined(__x86_64__) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 35 | static void cpuid ( unsigned int n, |
| 36 | unsigned int* a, unsigned int* b, |
| 37 | unsigned int* c, unsigned int* d ) |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 38 | { |
| 39 | __asm__ __volatile__ ( |
| 40 | "cpuid" |
| 41 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 42 | : "0" (n) /* input */ |
| 43 | ); |
| 44 | } |
| 45 | |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 46 | static Bool go(char* cpu) |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned int level = 0, mask = 0, a, b, c, d; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 49 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 50 | if ( strcmp( cpu, "x86" ) == 0 ) { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 51 | return True; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 52 | } else if ( strcmp( cpu, "x86-fpu" ) == 0 ) { |
| 53 | level = 1; |
| 54 | mask = 1 << 0; |
| 55 | } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) { |
| 56 | level = 1; |
| 57 | mask = 1 << 15; |
| 58 | } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) { |
| 59 | level = 1; |
| 60 | mask = 1 << 23; |
| 61 | } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) { |
| 62 | level = 0x80000001; |
| 63 | mask = 1 << 22; |
| 64 | } else if ( strcmp( cpu, "x86-sse" ) == 0 ) { |
| 65 | level = 1; |
| 66 | mask = 1 << 25; |
| 67 | } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) { |
| 68 | level = 1; |
| 69 | mask = 1 << 26; |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame^] | 70 | #if defined(__x86_64__) |
| 71 | } else if ( strcmp( cpu, "amd64" ) == 0 ) { |
| 72 | return True; |
| 73 | #endif |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 74 | } else { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 75 | return False; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 76 | } |
| 77 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 78 | cpuid( level & 0x80000000, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 79 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 80 | if ( a >= level ) { |
| 81 | cpuid( level, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 82 | |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 83 | if ( ( d & mask ) != 0 ) return True; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 84 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 85 | return False; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 86 | } |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame^] | 87 | #endif // __i386__ || __x86_64__ |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 88 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 89 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 90 | int main(int argc, char **argv) |
| 91 | { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 92 | int i; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 93 | if ( argc != 2 ) { |
| 94 | fprintf( stderr, "usage: cputest <cpu-type>\n" ); |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 95 | exit( 2 ); |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 96 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 97 | if (go( argv[1] )) { |
| 98 | return 0; // matched |
| 99 | } |
| 100 | for (i = 0; NULL != all_archs[i]; i++) { |
| 101 | if ( strcmp( argv[1], all_archs[i] ) == 0 ) |
| 102 | return 1; |
| 103 | } |
| 104 | return 2; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 105 | } |