Debian Codegraph @

libmmxnow/src/i386/cpudetect.c

raw symbols SCIP
1 /*
2 * code from mplayer * ffmpeg
3 * renamed to match mmxnow style
4 */
5
6 #include "cpudetect.h"
7
8 #ifdef HAVE_MALLOC_H
9 #include <malloc.h>
10 #endif
11 #include <stdlib.h>
12
13 #ifdef ARCH_X86
14
15 #include <stdio.h>
16
17 #ifdef __FreeBSD__
18 #include <sys/types.h>
19 #include <sys/sysctl.h>
20 #endif
21
22 #ifdef __linux__
23 #include <signal.h>
24 #endif
25
26 //#define X86_FXSR_MAGIC
27 /* Thanks to the FreeBSD project for some of this cpuid code, and
28 * help understanding how to use it. Thanks to the Mesa
29 * team for SSE support detection and more cpu detect code.
30 */
31
32 /* I believe this code works. However, it has only been used on a PII and PIII */
33
34 static void check_os_katmai_support( void );
35
36 #if 1
37 // return TRUE if cpuid supported
38 static int has_cpuid()
39 {
40 int a, c;
41
42 // code from libavcodec:
43 __asm__ __volatile__ (
44 /* See if CPUID instruction is supported ... */
45 /* ... Get copies of EFLAGS into eax and ecx */
46 "pushf\n\t"
47 "popl %0\n\t"
48 "movl %0, %1\n\t"
49
50 /* ... Toggle the ID bit in one copy and store */
51 /* to the EFLAGS reg */
52 "xorl $0x200000, %0\n\t"
53 "push %0\n\t"
54 "popf\n\t"
55
56 /* ... Get the (hopefully modified) EFLAGS */
57 "pushf\n\t"
58 "popl %0\n\t"
59 : "=a" (a), "=c" (c)
60 :
61 : "cc"
62 );
63
64 return (a!=c);
65 }
66 #endif
67
68 static void do_cpuid(unsigned int ax, unsigned int *p)
69 {
70 #if 0
71 __asm __volatile(
72 "cpuid;"
73 : "=a" (p[0]), "=b" (p[1]), "=c" (p[2]), "=d" (p[3])
74 : "0" (ax)
75 );
76 #else
77 // code from libavcodec:
78 __asm __volatile
79 ("movl %%ebx, %%esi\n\t"
80 "cpuid\n\t"
81 "xchgl %%ebx, %%esi"
82 : "=a" (p[0]), "=S" (p[1]),
83 "=c" (p[2]), "=d" (p[3])
84 : "0" (ax));
85 #endif
86
87 }
88
89 void mmxnow_get_cpu_caps(cpu_caps_t *caps)
90 {
91 unsigned int regs[4];
92 unsigned int regs2[4];
93
94 caps->is_X86=1;
95
96 memset(caps, 0, sizeof(*caps));
97 if (!has_cpuid()) {
98 printf("CPUID not supported!???\n");
99 return;
100 }
101 do_cpuid(0x00000000, regs); // get _max_ cpuid level and vendor name
102 printf("CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
103 (char*) (regs+1),(char*) (regs+3),(char*) (regs+2), regs[0]);
104 if (regs[0]>=0x00000001)
105 {
106 char *tmpstr;
107
108 do_cpuid(0x00000001, regs2);
109
110 tmpstr=GetCpuFriendlyName(regs, regs2);
111 printf("CPU: %s ",tmpstr);
112 free(tmpstr);
113
114 caps->cpu_type=(regs2[0] >> 8)&0xf;
115 if(caps->cpu_type==0xf){
116 // use extended family (P4, IA64)
117 caps->cpu_type=8+((regs2[0]>>20)&255);
118 }
119 caps->cpu_stepping=regs2[0] & 0xf;
120 printf("(Type: %d, Stepping: %d)\n",
121 caps->cpu_type, caps->cpu_stepping);
122
123 // general feature flags:
124 caps->has_MMX = (regs2[3] & (1 << 23 )) >> 23; // 0x0800000
125 caps->has_SSE = (regs2[3] & (1 << 25 )) >> 25; // 0x2000000
126 caps->has_SSE2 = (regs2[3] & (1 << 26 )) >> 26; // 0x4000000
127 caps->has_MMX2 = caps->has_SSE; // SSE cpus supports mmxext too
128 }
129 do_cpuid(0x80000000, regs);
130 if (regs[0]>=0x80000001) {
131 printf("extended cpuid-level: %d\n",regs[0]&0x7FFFFFFF);
132 do_cpuid(0x80000001, regs2);
133 caps->has_MMX |= (regs2[3] & (1 << 23 )) >> 23; // 0x0800000
134 caps->has_MMX2 |= (regs2[3] & (1 << 22 )) >> 22; // 0x400000
135 caps->has_3DNow = (regs2[3] & (1 << 31 )) >> 31; //0x80000000
136 caps->has_3DNowExt = (regs2[3] & (1 << 30 )) >> 30;
137 }
138 #if 0
139 printf("cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
140 gCpuCaps.hasMMX,
141 gCpuCaps.hasMMX2,
142 gCpuCaps.hasSSE,
143 gCpuCaps.hasSSE2,
144 gCpuCaps.has3DNow,
145 gCpuCaps.has3DNowExt );
146 #endif
147
148 /* FIXME: Does SSE2 need more OS support, too? */
149 #if defined(__linux__) || defined(__FreeBSD__)
150 if (caps->has_SSE)
151 check_os_katmai_support();
152 if (!caps->has_SSE)
153 caps->has_SSE2 = 0;
154 #else
155 caps->has_SSE=0;
156 caps->has_SSE2 = 0;
157 #endif
158 // caps->has_3DNow=1;
159 // caps->has_MMX2 = 0;
160 // caps->has_MMX = 0;
161
162 }
163
164
165 #define CPUID_EXTFAMILY ((regs2[0] >> 20)&0xFF) /* 27..20 */
166 #define CPUID_EXTMODEL ((regs2[0] >> 16)&0x0F) /* 19..16 */
167 #define CPUID_TYPE ((regs2[0] >> 12)&0x04) /* 13..12 */
168 #define CPUID_FAMILY ((regs2[0] >> 8)&0x0F) /* 11..08 */
169 #define CPUID_MODEL ((regs2[0] >> 4)&0x0F) /* 07..04 */
170 #define CPUID_STEPPING ((regs2[0] >> 0)&0x0F) /* 03..00 */
171
172 char *mmxnow_get_cpu_name(unsigned int regs[], unsigned int regs2[]){
173 #include "cputable.h" /* get cpuname and cpuvendors */
174 char vendor[17];
175 char *retname;
176 int i;
177
178 if (NULL==(retname=(char*)malloc(256))) {
179 printf("Error: GetCpuFriendlyName() not enough memory\n");
180 exit(1);
181 }
182
183 sprintf(vendor,"%.4s%.4s%.4s",(char*)(regs+1),(char*)(regs+3),(char*)(regs+2));
184
185 for(i=0; i<MAX_VENDORS; i++){
186 if(!strcmp(cpuvendors[i].string,vendor)){
187 if(cpuname[i][CPUID_FAMILY][CPUID_MODEL]){
188 snprintf(retname,255,"%s %s",cpuvendors[i].name,cpuname[i][CPUID_FAMILY][CPUID_MODEL]);
189 } else {
190 snprintf(retname,255,"unknown %s %d. Generation CPU",cpuvendors[i].name,CPUID_FAMILY);
191 printf("unknown %s CPU:\n",cpuvendors[i].name);
192 printf("Vendor: %s\n",cpuvendors[i].string);
193 printf("Type: %d\n",CPUID_TYPE);
194 printf("Family: %d (ext: %d)\n",CPUID_FAMILY,CPUID_EXTFAMILY);
195 printf("Model: %d (ext: %d)\n",CPUID_MODEL,CPUID_EXTMODEL);
196 printf("Stepping: %d\n",CPUID_STEPPING);
197 printf("Please send the above info along with the exact CPU name"
198 "to the MPlayer-Developers, so we can add it to the list!\n");
199 }
200 }
201 }
202
203 //printf("Detected CPU: %s\n", retname);
204 return retname;
205 }
206
207 #undef CPUID_EXTFAMILY
208 #undef CPUID_EXTMODEL
209 #undef CPUID_TYPE
210 #undef CPUID_FAMILY
211 #undef CPUID_MODEL
212 #undef CPUID_STEPPING
213
214
215 #if defined(__linux__) && defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
216 static void sigill_handler_sse( int signal, struct sigcontext sc )
217 {
218 printf( "SIGILL, " );
219
220 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
221 * instructions are 3 bytes long. We must increment the instruction
222 * pointer manually to avoid repeated execution of the offending
223 * instruction.
224 *
225 * If the SIGILL is caused by a divide-by-zero when unmasked
226 * exceptions aren't supported, the SIMD FPU status and control
227 * word will be restored at the end of the test, so we don't need
228 * to worry about doing it here. Besides, we may not be able to...
229 */
230 sc.eip += 3;
231
232 gCpuCaps.hasSSE=0;
233 }
234
235 static void sigfpe_handler_sse( int signal, struct sigcontext sc )
236 {
237 printf( "SIGFPE, " );
238
239 if ( sc.fpstate->magic != 0xffff ) {
240 /* Our signal context has the extended FPU state, so reset the
241 * divide-by-zero exception mask and clear the divide-by-zero
242 * exception bit.
243 */
244 sc.fpstate->mxcsr |= 0x00000200;
245 sc.fpstate->mxcsr &= 0xfffffffb;
246 } else {
247 /* If we ever get here, we're completely hosed.
248 */
249 printf( "\n\n" );
250 printf( "SSE enabling test failed badly!" );
251 }
252 }
253 #endif /* __linux__ && _POSIX_SOURCE && X86_FXSR_MAGIC */
254
255 /* If we're running on a processor that can do SSE, let's see if we
256 * are allowed to or not. This will catch 2.4.0 or later kernels that
257 * haven't been configured for a Pentium III but are running on one,
258 * and RedHat patched 2.2 kernels that have broken exception handling
259 * support for user space apps that do SSE.
260 */
261 static void check_os_katmai_support( void )
262 {
263 #if defined(__FreeBSD__)
264 int has_sse=0, ret;
265 size_t len=sizeof(has_sse);
266
267 ret = sysctlbyname("hw.instruction_sse", &has_sse, &len, NULL, 0);
268 if (ret || !has_sse)
269 gCpuCaps.hasSSE=0;
270
271 #elif defined(__linux__)
272 #if defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
273 struct sigaction saved_sigill;
274 struct sigaction saved_sigfpe;
275
276 /* Save the original signal handlers.
277 */
278 sigaction( SIGILL, NULL, &saved_sigill );
279 sigaction( SIGFPE, NULL, &saved_sigfpe );
280
281 signal( SIGILL, (void (*)(int))sigill_handler_sse );
282 signal( SIGFPE, (void (*)(int))sigfpe_handler_sse );
283
284 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
285 * supports the extended FPU save and restore required for SSE. If
286 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
287 * doesn't support Streaming SIMD Exceptions, even if the processor
288 * does.
289 */
290 if ( gCpuCaps.hasSSE ) {
291 printf( "Testing OS support for SSE... " );
292
293 // __asm __volatile ("xorps %%xmm0, %%xmm0");
294 __asm __volatile ("xorps %xmm0, %xmm0");
295
296 if ( gCpuCaps.hasSSE ) {
297 printf( "yes.\n" );
298 } else {
299 printf( "no!\n" );
300 }
301 }
302
303 /* Emulate test for OSXMMEXCPT in CR4. The OS will set this bit if
304 * it supports unmasked SIMD FPU exceptions. If we unmask the
305 * exceptions, do a SIMD divide-by-zero and get a SIGILL, the OS
306 * doesn't support unmasked SIMD FPU exceptions. If we get a SIGFPE
307 * as expected, we're okay but we need to clean up after it.
308 *
309 * Are we being too stringent in our requirement that the OS support
310 * unmasked exceptions? Certain RedHat 2.2 kernels enable SSE by
311 * setting CR4.OSFXSR but don't support unmasked exceptions. Win98
312 * doesn't even support them. We at least know the user-space SSE
313 * support is good in kernels that do support unmasked exceptions,
314 * and therefore to be safe I'm going to leave this test in here.
315 */
316 if ( gCpuCaps.hasSSE ) {
317 printf( "Testing OS support for SSE unmasked exceptions... " );
318
319 // test_os_katmai_exception_support();
320
321 if ( gCpuCaps.hasSSE ) {
322 printf( "yes.\n" );
323 } else {
324 printf( "no!\n" );
325 }
326 }
327
328 /* Restore the original signal handlers.
329 */
330 sigaction( SIGILL, &saved_sigill, NULL );
331 sigaction( SIGFPE, &saved_sigfpe, NULL );
332
333 /* If we've gotten to here and the XMM CPUID bit is still set, we're
334 * safe to go ahead and hook out the SSE code throughout Mesa.
335 */
336 if ( gCpuCaps.hasSSE ) {
337 printf( "Tests of OS support for SSE passed.\n" );
338 } else {
339 printf( "Tests of OS support for SSE failed!\n" );
340 }
341 #else
342 /* We can't use POSIX signal handling to test the availability of
343 * SSE, so we disable it by default.
344 */
345 printf( "Cannot test OS support for SSE, disabling to be safe.\n" );
346 gCpuCaps.has_SSE=0;
347 #endif /* _POSIX_SOURCE && X86_FXSR_MAGIC */
348 #else
349 /* Do nothing on other platforms for now.
350 */
351 printf( "Not testing OS support for SSE, leaving disabled.\n" );
352 gCpuCaps.has_SSE=0;
353 #endif /* __linux__ */
354 }
355 #else /* ARCH_X86 */
356
357 void mmxnow_get_cpu_caps(cpu_caps_t* caps)
358 {
359 caps->cpu_type=0;
360 caps->cpu_stepping=0;
361 caps->has_MMX=0;
362 caps->has_MMX2=0;
363 caps->has_3DNow=0;
364 caps->has_3DNowExt=0;
365 caps->has_SSE=0;
366 caps->has_SSE2=0;
367 caps->is_x86=0;
368 }
369 #endif /* !ARCH_X86 */