Drop the funky SSE exception test on linux.
[mesa.git] / src / mesa / x86 / common_x86.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file common_x86.c
27 *
28 * Check CPU capabilities & initialize optimized funtions for this particular
29 * processor.
30 *
31 * Changed by Andre Werthmann for using the new SSE functions.
32 *
33 * \author Holger Waechtler <holger@akaflieg.extern.tu-berlin.de>
34 * \author Andre Werthmann <wertmann@cs.uni-potsdam.de>
35 */
36
37 /* XXX these includes should probably go into imports.h or glheader.h */
38 #if defined(USE_SSE_ASM) && defined(__linux__)
39 #include <linux/version.h>
40 #endif
41 #if defined(USE_SSE_ASM) && defined(__FreeBSD__)
42 #include <sys/types.h>
43 #include <sys/sysctl.h>
44 #endif
45
46 #include "common_x86_asm.h"
47 #include "imports.h"
48
49
50 int _mesa_x86_cpu_features = 0;
51
52 /* No reason for this to be public.
53 */
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);
60
61
62 #if defined(USE_SSE_ASM)
63 /*
64 * We must verify that the Streaming SIMD Extensions are truly supported
65 * on this processor before we go ahead and hook out the optimized code.
66 *
67 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
68 * kernels provide full SSE support on all processors that expose SSE via
69 * the CPUID mechanism.
70 */
71 extern void _mesa_test_os_sse_support( void );
72 extern void _mesa_test_os_sse_exception_support( void );
73
74 #if defined(WIN32)
75 #ifndef STATUS_FLOAT_MULTIPLE_TRAPS
76 # define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
77 #endif
78 static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
79 {
80 PEXCEPTION_RECORD rec = exp->ExceptionRecord;
81 PCONTEXT ctx = exp->ContextRecord;
82
83 if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
84 _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
85 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
86 } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
87 _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
88 /* Windows seems to clear the exception flag itself, we just have to increment Eip */
89 } else {
90 _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
91 return EXCEPTION_EXECUTE_HANDLER;
92 }
93
94 if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
95 _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
96 return EXCEPTION_EXECUTE_HANDLER;
97 }
98 ctx->Eip += 3;
99
100 return EXCEPTION_CONTINUE_EXECUTION;
101 }
102 #endif /* WIN32 */
103
104
105 static void check_os_sse_support( void )
106 {
107 #if defined(__linux__)
108 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
109 _mesa_debug(NULL, "Cannot safely enable SSE on pre-2.4 kernels.\n");
110 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
111 #endif
112 #elif defined(__FreeBSD__)
113 {
114 int ret, enabled;
115 unsigned int len;
116 len = sizeof(enabled);
117 ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
118 if (ret || !enabled)
119 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
120 }
121 #elif defined(WIN32)
122 LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
123
124 /* Install our ExceptionFilter */
125 oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
126
127 if ( cpu_has_xmm ) {
128 _mesa_debug(NULL, "Testing OS support for SSE...\n");
129
130 _mesa_test_os_sse_support();
131
132 if ( cpu_has_xmm ) {
133 _mesa_debug(NULL, "Yes.\n");
134 } else {
135 _mesa_debug(NULL, "No!\n");
136 }
137 }
138
139 if ( cpu_has_xmm ) {
140 _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
141
142 _mesa_test_os_sse_exception_support();
143
144 if ( cpu_has_xmm ) {
145 _mesa_debug(NULL, "Yes.\n");
146 } else {
147 _mesa_debug(NULL, "No!\n");
148 }
149 }
150
151 /* Restore previous exception filter */
152 SetUnhandledExceptionFilter( oldFilter );
153
154 if ( cpu_has_xmm ) {
155 _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
156 } else {
157 _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
158 }
159 #else
160 /* Do nothing on other platforms for now.
161 */
162 _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
163 #endif /* __linux__ */
164 }
165
166 #endif /* USE_SSE_ASM */
167
168
169 void _mesa_init_all_x86_transform_asm( void )
170 {
171 #ifdef USE_X86_ASM
172 _mesa_x86_cpu_features = 0;
173
174 if (!_mesa_x86_has_cpuid()) {
175 _mesa_debug(NULL, "CPUID not detected\n");
176 }
177 else {
178 GLuint cpu_features;
179 GLuint cpu_ext_features;
180 GLuint cpu_ext_info;
181 char cpu_vendor[13];
182 GLuint result;
183
184 /* get vendor name */
185 _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
186 cpu_vendor[12] = '\0';
187
188 _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
189
190 /* get cpu features */
191 cpu_features = _mesa_x86_cpuid_edx(1);
192
193 if (cpu_features & X86_CPU_FPU)
194 _mesa_x86_cpu_features |= X86_FEATURE_FPU;
195 if (cpu_features & X86_CPU_CMOV)
196 _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
197
198 #ifdef USE_MMX_ASM
199 if (cpu_features & X86_CPU_MMX)
200 _mesa_x86_cpu_features |= X86_FEATURE_MMX;
201 #endif
202
203 #ifdef USE_SSE_ASM
204 if (cpu_features & X86_CPU_XMM)
205 _mesa_x86_cpu_features |= X86_FEATURE_XMM;
206 if (cpu_features & X86_CPU_XMM2)
207 _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
208 #endif
209
210 /* query extended cpu features */
211 if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
212 if (cpu_ext_info >= 0x80000001) {
213
214 cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
215
216 if (cpu_features & X86_CPU_MMX) {
217
218 #ifdef USE_3DNOW_ASM
219 if (cpu_ext_features & X86_CPUEXT_3DNOW)
220 _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
221 if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
222 _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
223 #endif
224
225 #ifdef USE_MMX_ASM
226 if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
227 _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
228 #endif
229 }
230 }
231
232 /* query cpu name */
233 if (cpu_ext_info >= 0x80000002) {
234 GLuint ofs;
235 char cpu_name[49];
236 for (ofs = 0; ofs < 3; ofs++)
237 _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));
238 cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
239
240 _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
241 }
242 }
243
244 }
245
246 if ( _mesa_getenv( "MESA_NO_ASM" ) ) {
247 _mesa_x86_cpu_features = 0;
248 }
249
250 if ( _mesa_x86_cpu_features ) {
251 _mesa_init_x86_transform_asm();
252 }
253
254 #ifdef USE_MMX_ASM
255 if ( cpu_has_mmx ) {
256 if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
257 _mesa_debug(NULL, "MMX cpu detected.\n");
258 } else {
259 _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
260 }
261 }
262 #endif
263
264 #ifdef USE_3DNOW_ASM
265 if ( cpu_has_3dnow ) {
266 if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
267 _mesa_debug(NULL, "3DNow! cpu detected.\n");
268 _mesa_init_3dnow_transform_asm();
269 } else {
270 _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
271 }
272 }
273 #endif
274
275 #ifdef USE_SSE_ASM
276 if ( cpu_has_xmm ) {
277 if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
278 _mesa_debug(NULL, "SSE cpu detected.\n");
279 if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
280 check_os_sse_support();
281 }
282 if ( cpu_has_xmm ) {
283 _mesa_init_sse_transform_asm();
284 }
285 } else {
286 _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
287 _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
288 }
289 }
290 #endif
291 #endif
292 }
293