17 #if defined (__NetBSD__) || defined(__OpenBSD__)
18 #include <sys/param.h>
19 #include <sys/sysctl.h>
20 #include <machine/cpu.h>
23 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
32 #if defined(__MINGW32__) || defined(__CYGWIN__)
42 #include <proto/exec.h>
45 /* Thanks to the FreeBSD project for some of this cpuid code, and
46 * help understanding how to use it. Thanks to the Mesa
47 * team for SSE support detection and more cpu detect code.
50 /* I believe this code works. However, it has only been used on a PII and PIII */
52 static void check_os_katmai_support( void );
54 // return TRUE if cpuid supported
55 static int has_cpuid(void)
59 // code from libavcodec:
61 /* See if CPUID instruction is supported ... */
62 /* ... Get copies of EFLAGS into eax and ecx */
67 /* ... Toggle the ID bit in one copy and store */
68 /* to the EFLAGS reg */
69 "xor $0x200000, %0\n\t"
73 /* ... Get the (hopefully modified) EFLAGS */
85 do_cpuid(unsigned int ax
, unsigned int *p
)
90 : "=a" (p
[0]), "=b" (p
[1]), "=c" (p
[2]), "=d" (p
[3])
94 // code from libavcodec:
96 ("mov %%"REG_b
", %%"REG_S
"\n\t"
98 "xchg %%"REG_b
", %%"REG_S
99 : "=a" (p
[0]), "=S" (p
[1]),
100 "=c" (p
[2]), "=d" (p
[3])
106 void GetCpuCaps( CpuCaps
*caps
)
108 unsigned int regs
[4];
109 unsigned int regs2
[4];
111 memset(caps
, 0, sizeof(*caps
));
113 caps
->cl_size
=32; /* default */
115 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"CPUID not supported!??? (maybe an old 486?)\n");
118 do_cpuid(0x00000000, regs
); // get _max_ cpuid level and vendor name
119 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"CPU vendor name: %.4s%.4s%.4s max cpuid level: %d\n",
120 (char*) (regs
+1),(char*) (regs
+3),(char*) (regs
+2), regs
[0]);
121 if (regs
[0]>=0x00000001)
123 char *tmpstr
, *ptmpstr
;
126 do_cpuid(0x00000001, regs2
);
128 caps
->cpuType
=(regs2
[0] >> 8)&0xf;
129 caps
->cpuModel
=(regs2
[0] >> 4)&0xf;
131 // see AMD64 Architecture Programmer's Manual, Volume 3: General-purpose and
132 // System Instructions, Table 3-2: Effective family computation, page 120.
133 if(caps
->cpuType
==0xf){
134 // use extended family (P4, IA64, K8)
135 caps
->cpuType
=0xf+((regs2
[0]>>20)&255);
137 if(caps
->cpuType
==0xf || caps
->cpuType
==6)
138 caps
->cpuModel
|= ((regs2
[0]>>16)&0xf) << 4;
140 caps
->cpuStepping
=regs2
[0] & 0xf;
142 // general feature flags:
143 caps
->hasTSC
= (regs2
[3] & (1 << 8 )) >> 8; // 0x0000010
144 caps
->hasMMX
= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
145 caps
->hasSSE
= (regs2
[3] & (1 << 25 )) >> 25; // 0x2000000
146 caps
->hasSSE2
= (regs2
[3] & (1 << 26 )) >> 26; // 0x4000000
147 caps
->hasSSSE3
= (regs2
[3] & (1 << 9 )) >> 9; // 0x0000200
148 caps
->hasSSE4a
= (regs2
[3] & (1 << 6 )) >> 6; // 0x0000040
149 caps
->hasMMX2
= caps
->hasSSE
; // SSE cpus supports mmxext too
150 cl_size
= ((regs2
[1] >> 8) & 0xFF)*8;
151 if(cl_size
) caps
->cl_size
= cl_size
;
153 ptmpstr
=tmpstr
=GetCpuFriendlyName(regs
, regs2
);
154 while(*ptmpstr
== ' ') // strip leading spaces
156 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: %s ", ptmpstr
);
158 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"(Family: %d, Model: %d, Stepping: %d)\n",
159 caps
->cpuType
, caps
->cpuModel
, caps
->cpuStepping
);
162 do_cpuid(0x80000000, regs
);
163 if (regs
[0]>=0x80000001) {
164 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cpuid-level: %d\n",regs
[0]&0x7FFFFFFF);
165 do_cpuid(0x80000001, regs2
);
166 caps
->hasMMX
|= (regs2
[3] & (1 << 23 )) >> 23; // 0x0800000
167 caps
->hasMMX2
|= (regs2
[3] & (1 << 22 )) >> 22; // 0x400000
168 caps
->has3DNow
= (regs2
[3] & (1 << 31 )) >> 31; //0x80000000
169 caps
->has3DNowExt
= (regs2
[3] & (1 << 30 )) >> 30;
171 if(regs
[0]>=0x80000006)
173 do_cpuid(0x80000006, regs2
);
174 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"extended cache-info: %d\n",regs2
[2]&0x7FFFFFFF);
175 caps
->cl_size
= regs2
[2] & 0xFF;
177 mp_msg(MSGT_CPUDETECT
,MSGL_V
,"Detected cache-line size is %u bytes\n",caps
->cl_size
);
179 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"cpudetect: MMX=%d MMX2=%d SSE=%d SSE2=%d 3DNow=%d 3DNowExt=%d\n",
185 gCpuCaps
.has3DNowExt
);
188 /* FIXME: Does SSE2 need more OS support, too? */
189 #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
190 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
191 || defined(__APPLE__) || defined(__CYGWIN__) || defined(__MINGW32__) \
194 check_os_katmai_support();
202 // caps->hasMMX2 = 0;
205 #ifndef RUNTIME_CPUDETECT
207 if(caps
->hasMMX
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX supported but disabled\n");
211 if(caps
->hasMMX2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"MMX2 supported but disabled\n");
215 if(caps
->hasSSE
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE supported but disabled\n");
219 if(caps
->hasSSE2
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"SSE2 supported but disabled\n");
223 if(caps
->has3DNow
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNow supported but disabled\n");
227 if(caps
->has3DNowExt
) mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"3DNowExt supported but disabled\n");
230 #endif // RUNTIME_CPUDETECT
234 #define CPUID_EXTFAMILY ((regs2[0] >> 20)&0xFF) /* 27..20 */
235 #define CPUID_EXTMODEL ((regs2[0] >> 16)&0x0F) /* 19..16 */
236 #define CPUID_TYPE ((regs2[0] >> 12)&0x04) /* 13..12 */
237 #define CPUID_FAMILY ((regs2[0] >> 8)&0x0F) /* 11..08 */
238 #define CPUID_MODEL ((regs2[0] >> 4)&0x0F) /* 07..04 */
239 #define CPUID_STEPPING ((regs2[0] >> 0)&0x0F) /* 03..00 */
241 char *GetCpuFriendlyName(unsigned int regs
[], unsigned int regs2
[]){
242 #include "cputable.h" /* get cpuname and cpuvendors */
247 if (NULL
==(retname
=malloc(256))) {
248 mp_msg(MSGT_CPUDETECT
,MSGL_FATAL
,"Error: GetCpuFriendlyName() not enough memory\n");
252 sprintf(vendor
,"%.4s%.4s%.4s",(char*)(regs
+1),(char*)(regs
+3),(char*)(regs
+2));
254 do_cpuid(0x80000000,regs
);
255 if (regs
[0] >= 0x80000004)
257 // CPU has built-in namestring
259 for (i
= 0x80000002; i
<= 0x80000004; i
++)
262 strncat(retname
, (char*)regs
, 16);
267 for(i
=0; i
<MAX_VENDORS
; i
++){
268 if(!strcmp(cpuvendors
[i
].string
,vendor
)){
269 if(cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]){
270 snprintf(retname
,255,"%s %s",cpuvendors
[i
].name
,cpuname
[i
][CPUID_FAMILY
][CPUID_MODEL
]);
272 snprintf(retname
,255,"unknown %s %d. Generation CPU",cpuvendors
[i
].name
,CPUID_FAMILY
);
273 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"unknown %s CPU:\n",cpuvendors
[i
].name
);
274 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Vendor: %s\n",cpuvendors
[i
].string
);
275 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Type: %d\n",CPUID_TYPE
);
276 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Family: %d (ext: %d)\n",CPUID_FAMILY
,CPUID_EXTFAMILY
);
277 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Model: %d (ext: %d)\n",CPUID_MODEL
,CPUID_EXTMODEL
);
278 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Stepping: %d\n",CPUID_STEPPING
);
279 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
,"Please send the above info along with the exact CPU name"
280 "to the MPlayer-Developers, so we can add it to the list!\n");
286 //printf("Detected CPU: %s\n", retname);
290 #undef CPUID_EXTFAMILY
291 #undef CPUID_EXTMODEL
295 #undef CPUID_STEPPING
298 #if defined(__linux__) && defined(_POSIX_SOURCE) && !defined(ARCH_X86_64)
299 static void sigill_handler_sse( int signal
, struct sigcontext sc
)
301 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
303 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
304 * instructions are 3 bytes long. We must increment the instruction
305 * pointer manually to avoid repeated execution of the offending
308 * If the SIGILL is caused by a divide-by-zero when unmasked
309 * exceptions aren't supported, the SIMD FPU status and control
310 * word will be restored at the end of the test, so we don't need
311 * to worry about doing it here. Besides, we may not be able to...
317 #endif /* __linux__ && _POSIX_SOURCE */
319 #if defined(__MINGW32__) || defined(__CYGWIN__)
320 LONG CALLBACK
win32_sig_handler_sse(EXCEPTION_POINTERS
* ep
)
322 if(ep
->ExceptionRecord
->ExceptionCode
==EXCEPTION_ILLEGAL_INSTRUCTION
){
323 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "SIGILL, " );
324 ep
->ContextRecord
->Eip
+=3;
326 return EXCEPTION_CONTINUE_EXECUTION
;
328 return EXCEPTION_CONTINUE_SEARCH
;
330 #endif /* defined(__MINGW32__) || defined(__CYGWIN__) */
333 ULONG _System
os2_sig_handler_sse( PEXCEPTIONREPORTRECORD p1
,
334 PEXCEPTIONREGISTRATIONRECORD p2
,
338 if(p1
->ExceptionNum
== XCPT_ILLEGAL_INSTRUCTION
){
339 mp_msg(MSGT_CPUDETECT
, MSGL_V
, "SIGILL, ");
344 return XCPT_CONTINUE_EXECUTION
;
346 return XCPT_CONTINUE_SEARCH
;
350 /* If we're running on a processor that can do SSE, let's see if we
351 * are allowed to or not. This will catch 2.4.0 or later kernels that
352 * haven't been configured for a Pentium III but are running on one,
353 * and RedHat patched 2.2 kernels that have broken exception handling
354 * support for user space apps that do SSE.
357 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
358 #define SSE_SYSCTL_NAME "hw.instruction_sse"
359 #elif defined(__APPLE__)
360 #define SSE_SYSCTL_NAME "hw.optional.sse"
363 static void check_os_katmai_support( void )
368 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__)
370 size_t len
=sizeof(has_sse
);
372 ret
= sysctlbyname(SSE_SYSCTL_NAME
, &has_sse
, &len
, NULL
, 0);
376 #elif defined(__NetBSD__) || defined (__OpenBSD__)
377 #if __NetBSD_Version__ >= 105250000 || (defined __OpenBSD__)
378 int has_sse
, has_sse2
, ret
, mib
[2];
381 mib
[0] = CTL_MACHDEP
;
383 varlen
= sizeof(has_sse
);
385 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
386 ret
= sysctl(mib
, 2, &has_sse
, &varlen
, NULL
, 0);
387 gCpuCaps
.hasSSE
= ret
>= 0 && has_sse
;
388 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
391 varlen
= sizeof(has_sse2
);
392 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE2... " );
393 ret
= sysctl(mib
, 2, &has_sse2
, &varlen
, NULL
, 0);
394 gCpuCaps
.hasSSE2
= ret
>= 0 && has_sse2
;
395 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE2
? "yes.\n" : "no!\n" );
398 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "No OS support for SSE, disabling to be safe.\n" );
400 #elif defined(__MINGW32__) || defined(__CYGWIN__)
401 LPTOP_LEVEL_EXCEPTION_FILTER exc_fil
;
402 if ( gCpuCaps
.hasSSE
) {
403 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
404 exc_fil
= SetUnhandledExceptionFilter(win32_sig_handler_sse
);
405 __asm__
volatile ("xorps %xmm0, %xmm0");
406 SetUnhandledExceptionFilter(exc_fil
);
407 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
409 #elif defined(__OS2__)
410 EXCEPTIONREGISTRATIONRECORD RegRec
= { 0, &os2_sig_handler_sse
};
411 if ( gCpuCaps
.hasSSE
) {
412 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
413 DosSetExceptionHandler( &RegRec
);
414 __asm__
volatile ("xorps %xmm0, %xmm0");
415 DosUnsetExceptionHandler( &RegRec
);
416 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
418 #elif defined(__linux__)
419 #if defined(_POSIX_SOURCE)
420 struct sigaction saved_sigill
;
422 /* Save the original signal handlers.
424 sigaction( SIGILL
, NULL
, &saved_sigill
);
426 signal( SIGILL
, (void (*)(int))sigill_handler_sse
);
428 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
429 * supports the extended FPU save and restore required for SSE. If
430 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
431 * doesn't support Streaming SIMD Exceptions, even if the processor
434 if ( gCpuCaps
.hasSSE
) {
435 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Testing OS support for SSE... " );
437 // __asm__ volatile ("xorps %%xmm0, %%xmm0");
438 __asm__
volatile ("xorps %xmm0, %xmm0");
440 mp_msg(MSGT_CPUDETECT
,MSGL_V
, gCpuCaps
.hasSSE
? "yes.\n" : "no!\n" );
443 /* Restore the original signal handlers.
445 sigaction( SIGILL
, &saved_sigill
, NULL
);
447 /* If we've gotten to here and the XMM CPUID bit is still set, we're
448 * safe to go ahead and hook out the SSE code throughout Mesa.
450 mp_msg(MSGT_CPUDETECT
,MSGL_V
, "Tests of OS support for SSE %s\n", gCpuCaps
.hasSSE
? "passed." : "failed!" );
452 /* We can't use POSIX signal handling to test the availability of
453 * SSE, so we disable it by default.
455 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, disabling to be safe.\n" );
457 #endif /* _POSIX_SOURCE */
459 /* Do nothing on other platforms for now.
461 mp_msg(MSGT_CPUDETECT
,MSGL_WARN
, "Cannot test OS support for SSE, leaving disabled.\n" );
463 #endif /* __linux__ */
468 #include <sys/sysctl.h>
475 static sigjmp_buf jmpbuf
;
476 static volatile sig_atomic_t canjump
= 0;
478 static void sigill_handler (int sig
)
481 signal (sig
, SIG_DFL
);
486 siglongjmp (jmpbuf
, 1);
488 #endif /* __APPLE__ */
490 void GetCpuCaps( CpuCaps
*caps
)
504 caps
->hasAltiVec
= 0;
508 rip-off from ffmpeg altivec detection code.
509 this code also appears on Apple's AltiVec pages.
512 int sels
[2] = {CTL_HW
, HW_VECTORUNIT
};
514 size_t len
= sizeof(has_vu
);
517 err
= sysctl(sels
, 2, &has_vu
, &len
, NULL
, 0);
521 caps
->hasAltiVec
= 1;
526 GetCPUInfoTags(GCIT_VectorUnit
, &result
, TAG_DONE
);
527 if (result
== VECTORTYPE_ALTIVEC
)
528 caps
->hasAltiVec
= 1;
530 /* no Darwin, do it the brute-force way */
531 /* this is borrowed from the libmpeg2 library */
533 signal (SIGILL
, sigill_handler
);
534 if (sigsetjmp (jmpbuf
, 1)) {
535 signal (SIGILL
, SIG_DFL
);
539 __asm__
volatile ("mtspr 256, %0\n\t"
540 "vand %%v0, %%v0, %%v0"
544 signal (SIGILL
, SIG_DFL
);
545 caps
->hasAltiVec
= 1;
548 #endif /* __APPLE__ */
549 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"AltiVec %sfound\n", (caps
->hasAltiVec
? "" : "not "));
550 #endif /* HAVE_ALTIVEC */
553 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Intel Itanium\n");
557 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Sun Sparc\n");
561 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: ARM\n");
565 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: PowerPC\n");
569 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Digital Alpha\n");
573 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: SGI MIPS\n");
577 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: Hewlett-Packard PA-RISC\n");
581 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390\n");
585 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
,"CPU: IBM S/390X\n");
589 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
, "CPU: Digital VAX\n" );
593 mp_msg(MSGT_CPUDETECT
,MSGL_INFO
, "CPU: Tensilica Xtensa\n" );
596 #endif /* !ARCH_X86 */