b3e1401b8a2d2f51ed2272ee6b4e176b5ac9d86c
2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Check CPU capabilities & initialize optimized funtions for this particular
31 * Changed by Andre Werthmann for using the new SSE functions.
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
37 /* XXX these includes should probably go into imports.h or glheader.h */
38 #if defined(USE_SSE_ASM) && defined(__linux__)
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
46 #include "common_x86_asm.h"
50 int _mesa_x86_cpu_features
= 0;
52 /* No reason for this to be public.
54 extern GLuint _ASMAPI
_mesa_x86_has_cpuid(void);
55 extern void _ASMAPI
_mesa_x86_cpuid(GLuint op
, GLuint
*reg_eax
, GLuint
*reg_ebx
, GLuint
*reg_ecx
, GLuint
*reg_edx
);
56 extern GLuint _ASMAPI
_mesa_x86_cpuid_eax(GLuint op
);
57 extern GLuint _ASMAPI
_mesa_x86_cpuid_ebx(GLuint op
);
58 extern GLuint _ASMAPI
_mesa_x86_cpuid_ecx(GLuint op
);
59 extern GLuint _ASMAPI
_mesa_x86_cpuid_edx(GLuint op
);
61 static void message( const char *msg
)
67 if ( _mesa_getenv( "MESA_DEBUG" ) ) {
74 fprintf( stderr
, "%s", msg
);
78 #if defined(USE_SSE_ASM)
80 * We must verify that the Streaming SIMD Extensions are truly supported
81 * on this processor before we go ahead and hook out the optimized code.
82 * Unfortunately, the CPUID bit isn't enough, as the OS must set the
83 * OSFXSR bit in CR4 if it supports the extended FPU save and restore
84 * required to use SSE. Unfortunately, we can't just go ahead and read
85 * this register, as only the kernel can do that. Similarly, we must
86 * verify that the OSXMMEXCPT bit in CR4 has been set by the OS,
87 * signifying that it supports unmasked SIMD FPU exceptions. If we take
88 * an unmasked exception and the OS doesn't correctly support them, the
89 * best we'll get is a SIGILL and the worst we'll get is an infinite
90 * loop in the signal delivery from the kernel as we can't interact with
91 * the SIMD FPU state to clear the exception bits. Either way, this is
94 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
95 * kernels provide full SSE support on all processors that expose SSE via
96 * the CPUID mechanism. It just so happens that this is the exact set of
97 * kernels supported DRI. Therefore, when building for DRI the funky SSE
98 * exception test is omitted.
101 extern void _mesa_test_os_sse_support( void );
102 extern void _mesa_test_os_sse_exception_support( void );
104 #if defined(__linux__) && defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC) \
105 && !defined(DRI_NEW_INTERFACE_ONLY)
106 static void sigill_handler( int signal
, struct sigcontext sc
)
108 message( "SIGILL, " );
110 /* Both the "xorps %%xmm0,%%xmm0" and "divps %xmm0,%%xmm1"
111 * instructions are 3 bytes long. We must increment the instruction
112 * pointer manually to avoid repeated execution of the offending
115 * If the SIGILL is caused by a divide-by-zero when unmasked
116 * exceptions aren't supported, the SIMD FPU status and control
117 * word will be restored at the end of the test, so we don't need
118 * to worry about doing it here. Besides, we may not be able to...
122 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
125 static void sigfpe_handler( int signal
, struct sigcontext sc
)
127 message( "SIGFPE, " );
129 if ( sc
.fpstate
->magic
!= 0xffff ) {
130 /* Our signal context has the extended FPU state, so reset the
131 * divide-by-zero exception mask and clear the divide-by-zero
134 sc
.fpstate
->mxcsr
|= 0x00000200;
135 sc
.fpstate
->mxcsr
&= 0xfffffffb;
137 /* If we ever get here, we're completely hosed.
140 _mesa_problem( NULL
, "SSE enabling test failed badly!" );
143 #endif /* __linux__ && _POSIX_SOURCE && X86_FXSR_MAGIC */
145 /* If we're running on a processor that can do SSE, let's see if we
146 * are allowed to or not. This will catch 2.4.0 or later kernels that
147 * haven't been configured for a Pentium III but are running on one,
148 * and RedHat patched 2.2 kernels that have broken exception handling
149 * support for user space apps that do SSE.
151 * GH: Isn't this just awful?
153 static void check_os_sse_support( void )
155 #if defined(__linux__) && !defined(DRI_NEW_INTERFACE_ONLY)
156 #if defined(_POSIX_SOURCE) && defined(X86_FXSR_MAGIC)
157 struct sigaction saved_sigill
;
158 struct sigaction saved_sigfpe
;
160 /* Save the original signal handlers.
162 sigaction( SIGILL
, NULL
, &saved_sigill
);
163 sigaction( SIGFPE
, NULL
, &saved_sigfpe
);
165 signal( SIGILL
, (void (*)(int))sigill_handler
);
166 signal( SIGFPE
, (void (*)(int))sigfpe_handler
);
168 /* Emulate test for OSFXSR in CR4. The OS will set this bit if it
169 * supports the extended FPU save and restore required for SSE. If
170 * we execute an SSE instruction on a PIII and get a SIGILL, the OS
171 * doesn't support Streaming SIMD Exceptions, even if the processor
175 message( "Testing OS support for SSE... " );
177 _mesa_test_os_sse_support();
186 /* Emulate test for OSXMMEXCPT in CR4. The OS will set this bit if
187 * it supports unmasked SIMD FPU exceptions. If we unmask the
188 * exceptions, do a SIMD divide-by-zero and get a SIGILL, the OS
189 * doesn't support unmasked SIMD FPU exceptions. If we get a SIGFPE
190 * as expected, we're okay but we need to clean up after it.
192 * Are we being too stringent in our requirement that the OS support
193 * unmasked exceptions? Certain RedHat 2.2 kernels enable SSE by
194 * setting CR4.OSFXSR but don't support unmasked exceptions. Win98
195 * doesn't even support them. We at least know the user-space SSE
196 * support is good in kernels that do support unmasked exceptions,
197 * and therefore to be safe I'm going to leave this test in here.
200 message( "Testing OS support for SSE unmasked exceptions... " );
202 _mesa_test_os_sse_exception_support();
211 /* Restore the original signal handlers.
213 sigaction( SIGILL
, &saved_sigill
, NULL
);
214 sigaction( SIGFPE
, &saved_sigfpe
, NULL
);
216 /* If we've gotten to here and the XMM CPUID bit is still set, we're
217 * safe to go ahead and hook out the SSE code throughout Mesa.
220 message( "Tests of OS support for SSE passed.\n" );
222 message( "Tests of OS support for SSE failed!\n" );
225 /* We can't use POSIX signal handling to test the availability of
226 * SSE, so we disable it by default.
228 message( "Cannot test OS support for SSE, disabling to be safe.\n" );
229 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
230 #endif /* _POSIX_SOURCE && X86_FXSR_MAGIC */
231 #elif defined(__FreeBSD__)
235 len
= sizeof(enabled
);
236 ret
= sysctlbyname("hw.instruction_sse", &enabled
, &len
, NULL
, 0);
238 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);
241 /* Do nothing on other platforms for now.
243 message( "Not testing OS support for SSE, leaving enabled.\n" );
244 #endif /* __linux__ */
247 #endif /* USE_SSE_ASM */
250 void _mesa_init_all_x86_transform_asm( void )
252 (void) message
; /* silence warning */
254 _mesa_x86_cpu_features
= 0;
256 if (!_mesa_x86_has_cpuid()) {
257 message("CPUID not detected");
261 GLuint cpu_ext_features
;
266 /* get vendor name */
267 _mesa_x86_cpuid(0, &result
, (GLuint
*)(cpu_vendor
+ 0), (GLuint
*)(cpu_vendor
+ 8), (GLuint
*)(cpu_vendor
+ 4));
268 cpu_vendor
[12] = '\0';
270 message("cpu vendor: ");
274 /* get cpu features */
275 cpu_features
= _mesa_x86_cpuid_edx(1);
277 if (cpu_features
& X86_CPU_FPU
)
278 _mesa_x86_cpu_features
|= X86_FEATURE_FPU
;
279 if (cpu_features
& X86_CPU_CMOV
)
280 _mesa_x86_cpu_features
|= X86_FEATURE_CMOV
;
283 if (cpu_features
& X86_CPU_MMX
)
284 _mesa_x86_cpu_features
|= X86_FEATURE_MMX
;
288 if (cpu_features
& X86_CPU_XMM
)
289 _mesa_x86_cpu_features
|= X86_FEATURE_XMM
;
290 if (cpu_features
& X86_CPU_XMM2
)
291 _mesa_x86_cpu_features
|= X86_FEATURE_XMM2
;
294 /* query extended cpu features */
295 if ((cpu_ext_info
= _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
296 if (cpu_ext_info
>= 0x80000001) {
298 cpu_ext_features
= _mesa_x86_cpuid_edx(0x80000001);
300 if (cpu_features
& X86_CPU_MMX
) {
303 if (cpu_ext_features
& X86_CPUEXT_3DNOW
)
304 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOW
;
305 if (cpu_ext_features
& X86_CPUEXT_3DNOW_EXT
)
306 _mesa_x86_cpu_features
|= X86_FEATURE_3DNOWEXT
;
310 if (cpu_ext_features
& X86_CPUEXT_MMX_EXT
)
311 _mesa_x86_cpu_features
|= X86_FEATURE_MMXEXT
;
317 if (cpu_ext_info
>= 0x80000002) {
320 for (ofs
= 0; ofs
< 3; ofs
++)
321 _mesa_x86_cpuid(0x80000002+ofs
, (GLuint
*)(cpu_name
+ (16*ofs
)+0), (GLuint
*)(cpu_name
+ (16*ofs
)+4), (GLuint
*)(cpu_name
+ (16*ofs
)+8), (GLuint
*)(cpu_name
+ (16*ofs
)+12));
322 cpu_name
[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
324 message("cpu name: ");
332 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
333 _mesa_x86_cpu_features
= 0;
336 if ( _mesa_x86_cpu_features
) {
337 _mesa_init_x86_transform_asm();
342 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
343 message( "MMX cpu detected.\n" );
345 _mesa_x86_cpu_features
&= ~(X86_FEATURE_MMX
);
351 if ( cpu_has_3dnow
) {
352 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
353 message( "3DNow! cpu detected.\n" );
354 _mesa_init_3dnow_transform_asm();
356 _mesa_x86_cpu_features
&= ~(X86_FEATURE_3DNOW
);
362 if ( cpu_has_xmm
&& _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
363 check_os_sse_support();
366 if (_mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
367 message( "SSE cpu detected.\n" );
368 _mesa_init_sse_transform_asm();
370 message( "SSE cpu detected, but switched off by user.\n" );
371 _mesa_x86_cpu_features
&= ~(X86_FEATURE_XMM
);