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", |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 20 | "ppc64", |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 21 | "x86", |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 22 | NULL |
| 23 | }; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 24 | |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 25 | #if defined(__powerpc__) && !defined(__powerpc64__) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 26 | static Bool go(char* cpu) |
| 27 | { |
sewardj | fcbb942 | 2005-11-10 23:30:21 +0000 | [diff] [blame] | 28 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 29 | return True; |
sewardj | 87f7fdd | 2006-01-04 03:08:16 +0000 | [diff] [blame] | 30 | return False; |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 31 | } |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 32 | #endif // __powerpc__ (32) |
| 33 | |
| 34 | #if defined(__powerpc__) && defined(__powerpc64__) |
| 35 | static Bool go(char* cpu) |
| 36 | { |
| 37 | if ( strcmp( cpu, "ppc64" ) == 0 ) |
| 38 | return True; |
sewardj | 87f7fdd | 2006-01-04 03:08:16 +0000 | [diff] [blame] | 39 | if ( strcmp( cpu, "ppc32" ) == 0 ) |
| 40 | return True; |
| 41 | return False; |
sewardj | 0d4f8ac | 2006-01-02 16:24:03 +0000 | [diff] [blame] | 42 | } |
| 43 | #endif // __powerpc__ (64) |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 44 | |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame] | 45 | #if defined(__i386__) || defined(__x86_64__) |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 46 | static void cpuid ( unsigned int n, |
| 47 | unsigned int* a, unsigned int* b, |
| 48 | unsigned int* c, unsigned int* d ) |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 49 | { |
| 50 | __asm__ __volatile__ ( |
| 51 | "cpuid" |
| 52 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 53 | : "0" (n) /* input */ |
| 54 | ); |
| 55 | } |
| 56 | |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 57 | static Bool go(char* cpu) |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 58 | { |
| 59 | unsigned int level = 0, mask = 0, a, b, c, d; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 60 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 61 | if ( strcmp( cpu, "x86" ) == 0 ) { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 62 | return True; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 63 | } else if ( strcmp( cpu, "x86-fpu" ) == 0 ) { |
| 64 | level = 1; |
| 65 | mask = 1 << 0; |
| 66 | } else if ( strcmp( cpu, "x86-cmov" ) == 0 ) { |
| 67 | level = 1; |
| 68 | mask = 1 << 15; |
| 69 | } else if ( strcmp( cpu, "x86-mmx" ) == 0 ) { |
| 70 | level = 1; |
| 71 | mask = 1 << 23; |
| 72 | } else if ( strcmp( cpu, "x86-mmxext" ) == 0 ) { |
| 73 | level = 0x80000001; |
| 74 | mask = 1 << 22; |
| 75 | } else if ( strcmp( cpu, "x86-sse" ) == 0 ) { |
| 76 | level = 1; |
| 77 | mask = 1 << 25; |
| 78 | } else if ( strcmp( cpu, "x86-sse2" ) == 0 ) { |
| 79 | level = 1; |
| 80 | mask = 1 << 26; |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame] | 81 | #if defined(__x86_64__) |
| 82 | } else if ( strcmp( cpu, "amd64" ) == 0 ) { |
| 83 | return True; |
| 84 | #endif |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 85 | } else { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 86 | return False; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 87 | } |
| 88 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 89 | cpuid( level & 0x80000000, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 90 | |
nethercote | 1018bdd | 2004-02-11 23:33:29 +0000 | [diff] [blame] | 91 | if ( a >= level ) { |
| 92 | cpuid( level, &a, &b, &c, &d ); |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 93 | |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 94 | if ( ( d & mask ) != 0 ) return True; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 95 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 96 | return False; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 97 | } |
tom | ef56732 | 2005-11-11 14:33:43 +0000 | [diff] [blame] | 98 | #endif // __i386__ || __x86_64__ |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 99 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 100 | |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 101 | int main(int argc, char **argv) |
| 102 | { |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 103 | int i; |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 104 | if ( argc != 2 ) { |
| 105 | fprintf( stderr, "usage: cputest <cpu-type>\n" ); |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 106 | exit( 2 ); |
nethercote | b625bcb | 2004-10-19 16:29:30 +0000 | [diff] [blame] | 107 | } |
nethercote | 362c0d2 | 2004-11-18 18:21:56 +0000 | [diff] [blame] | 108 | if (go( argv[1] )) { |
| 109 | return 0; // matched |
| 110 | } |
| 111 | for (i = 0; NULL != all_archs[i]; i++) { |
| 112 | if ( strcmp( argv[1], all_archs[i] ) == 0 ) |
| 113 | return 1; |
| 114 | } |
| 115 | return 2; |
nethercote | 288b766 | 2004-01-20 09:24:53 +0000 | [diff] [blame] | 116 | } |