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