common_x86.c revision a12cd26a
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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#if defined(USE_SSE_ASM) && defined(__NetBSD__)
46#include <sys/types.h>
47#include <sys/sysctl.h>
48#endif
49#if defined(USE_SSE_ASM) && defined(__OpenBSD__)
50#include <sys/param.h>
51#include <sys/sysctl.h>
52#include <machine/cpu.h>
53#endif
54#if defined(USE_X86_64_ASM)
55#include <cpuid.h>
56#if !defined(bit_SSE4_1) && defined(bit_SSE41)
57/* XXX: clang defines bit_SSE41 instead of bit_SSE4_1 */
58#define bit_SSE4_1 bit_SSE41
59#elif !defined(bit_SSE4_1) && !defined(bit_SSE41)
60#define bit_SSE4_1 0x00080000
61#endif
62#endif
63
64#include "main/imports.h"
65#include "common_x86_asm.h"
66
67
68/** Bitmask of X86_FEATURE_x bits */
69int _mesa_x86_cpu_features = 0x0;
70
71static int detection_debug = GL_FALSE;
72
73/* No reason for this to be public.
74 */
75extern GLuint	_ASMAPI _mesa_x86_has_cpuid(void);
76extern void	_ASMAPI _mesa_x86_cpuid(GLuint op, GLuint *reg_eax, GLuint *reg_ebx, GLuint *reg_ecx, GLuint *reg_edx);
77extern GLuint	_ASMAPI _mesa_x86_cpuid_eax(GLuint op);
78extern GLuint	_ASMAPI _mesa_x86_cpuid_ebx(GLuint op);
79extern GLuint	_ASMAPI _mesa_x86_cpuid_ecx(GLuint op);
80extern GLuint	_ASMAPI _mesa_x86_cpuid_edx(GLuint op);
81
82
83#if defined(USE_SSE_ASM)
84/*
85 * We must verify that the Streaming SIMD Extensions are truly supported
86 * on this processor before we go ahead and hook out the optimized code.
87 *
88 * However, I have been told by Alan Cox that all 2.4 (and later) Linux
89 * kernels provide full SSE support on all processors that expose SSE via
90 * the CPUID mechanism.
91 */
92
93/* These are assembly functions: */
94extern void _mesa_test_os_sse_support( void );
95extern void _mesa_test_os_sse_exception_support( void );
96
97
98#if defined(_WIN32)
99#ifndef STATUS_FLOAT_MULTIPLE_TRAPS
100# define STATUS_FLOAT_MULTIPLE_TRAPS (0xC00002B5L)
101#endif
102static LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS exp)
103{
104   PEXCEPTION_RECORD rec = exp->ExceptionRecord;
105   PCONTEXT ctx = exp->ContextRecord;
106
107   if ( rec->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION ) {
108      _mesa_debug(NULL, "EXCEPTION_ILLEGAL_INSTRUCTION\n" );
109      _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
110   } else if ( rec->ExceptionCode == STATUS_FLOAT_MULTIPLE_TRAPS ) {
111      _mesa_debug(NULL, "STATUS_FLOAT_MULTIPLE_TRAPS\n");
112      /* Windows seems to clear the exception flag itself, we just have to increment Eip */
113   } else {
114      _mesa_debug(NULL, "UNEXPECTED EXCEPTION (0x%08x), terminating!\n" );
115      return EXCEPTION_EXECUTE_HANDLER;
116   }
117
118   if ( (ctx->ContextFlags & CONTEXT_CONTROL) != CONTEXT_CONTROL ) {
119      _mesa_debug(NULL, "Context does not contain control registers, terminating!\n");
120      return EXCEPTION_EXECUTE_HANDLER;
121   }
122   ctx->Eip += 3;
123
124   return EXCEPTION_CONTINUE_EXECUTION;
125}
126#endif /* _WIN32 */
127
128
129/**
130 * Check if SSE is supported.
131 * If not, turn off the X86_FEATURE_XMM flag in _mesa_x86_cpu_features.
132 */
133void _mesa_check_os_sse_support( void )
134{
135#if defined(__FreeBSD__)
136   {
137      int ret, enabled;
138      unsigned int len;
139      len = sizeof(enabled);
140      ret = sysctlbyname("hw.instruction_sse", &enabled, &len, NULL, 0);
141      if (ret || !enabled)
142         _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
143   }
144#elif defined (__NetBSD__)
145   {
146      int ret, enabled;
147      size_t len = sizeof(enabled);
148      ret = sysctlbyname("machdep.sse", &enabled, &len, (void *)NULL, 0);
149      if (ret || !enabled)
150         _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
151   }
152#elif defined(__OpenBSD__)
153   {
154      int mib[2];
155      int ret, enabled;
156      size_t len = sizeof(enabled);
157
158      mib[0] = CTL_MACHDEP;
159      mib[1] = CPU_SSE;
160
161      ret = sysctl(mib, 2, &enabled, &len, NULL, 0);
162      if (ret || !enabled)
163         _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
164   }
165#elif defined(_WIN32)
166   LPTOP_LEVEL_EXCEPTION_FILTER oldFilter;
167
168   /* Install our ExceptionFilter */
169   oldFilter = SetUnhandledExceptionFilter( ExceptionFilter );
170
171   if ( cpu_has_xmm ) {
172      _mesa_debug(NULL, "Testing OS support for SSE...\n");
173
174      _mesa_test_os_sse_support();
175
176      if ( cpu_has_xmm ) {
177	 _mesa_debug(NULL, "Yes.\n");
178      } else {
179	 _mesa_debug(NULL, "No!\n");
180      }
181   }
182
183   if ( cpu_has_xmm ) {
184      _mesa_debug(NULL, "Testing OS support for SSE unmasked exceptions...\n");
185
186      _mesa_test_os_sse_exception_support();
187
188      if ( cpu_has_xmm ) {
189	 _mesa_debug(NULL, "Yes.\n");
190      } else {
191	 _mesa_debug(NULL, "No!\n");
192      }
193   }
194
195   /* Restore previous exception filter */
196   SetUnhandledExceptionFilter( oldFilter );
197
198   if ( cpu_has_xmm ) {
199      _mesa_debug(NULL, "Tests of OS support for SSE passed.\n");
200   } else {
201      _mesa_debug(NULL, "Tests of OS support for SSE failed!\n");
202   }
203#else
204   /* Do nothing on other platforms for now.
205    */
206   if (detection_debug)
207      _mesa_debug(NULL, "Not testing OS support for SSE, leaving enabled.\n");
208#endif /* __FreeBSD__ */
209}
210
211#endif /* USE_SSE_ASM */
212
213
214/**
215 * Initialize the _mesa_x86_cpu_features bitfield.
216 * This is a no-op if called more than once.
217 */
218void
219_mesa_get_x86_features(void)
220{
221   static int called = 0;
222
223   if (called)
224      return;
225
226   called = 1;
227
228#ifdef USE_X86_ASM
229   _mesa_x86_cpu_features = 0x0;
230
231   if (_mesa_getenv( "MESA_NO_ASM")) {
232      return;
233   }
234
235   if (!_mesa_x86_has_cpuid()) {
236       _mesa_debug(NULL, "CPUID not detected\n");
237   }
238   else {
239       GLuint cpu_features, cpu_features_ecx;
240       GLuint cpu_ext_features;
241       GLuint cpu_ext_info;
242       char cpu_vendor[13];
243       GLuint result;
244
245       /* get vendor name */
246       _mesa_x86_cpuid(0, &result, (GLuint *)(cpu_vendor + 0), (GLuint *)(cpu_vendor + 8), (GLuint *)(cpu_vendor + 4));
247       cpu_vendor[12] = '\0';
248
249       if (detection_debug)
250	  _mesa_debug(NULL, "CPU vendor: %s\n", cpu_vendor);
251
252       /* get cpu features */
253       cpu_features = _mesa_x86_cpuid_edx(1);
254       cpu_features_ecx = _mesa_x86_cpuid_ecx(1);
255
256       if (cpu_features & X86_CPU_FPU)
257	   _mesa_x86_cpu_features |= X86_FEATURE_FPU;
258       if (cpu_features & X86_CPU_CMOV)
259	   _mesa_x86_cpu_features |= X86_FEATURE_CMOV;
260
261#ifdef USE_MMX_ASM
262       if (cpu_features & X86_CPU_MMX)
263	   _mesa_x86_cpu_features |= X86_FEATURE_MMX;
264#endif
265
266#ifdef USE_SSE_ASM
267       if (cpu_features & X86_CPU_XMM)
268	   _mesa_x86_cpu_features |= X86_FEATURE_XMM;
269       if (cpu_features & X86_CPU_XMM2)
270	   _mesa_x86_cpu_features |= X86_FEATURE_XMM2;
271       if (cpu_features_ecx & X86_CPU_SSE4_1)
272	   _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
273#endif
274
275       /* query extended cpu features */
276       if ((cpu_ext_info = _mesa_x86_cpuid_eax(0x80000000)) > 0x80000000) {
277	   if (cpu_ext_info >= 0x80000001) {
278
279	       cpu_ext_features = _mesa_x86_cpuid_edx(0x80000001);
280
281	       if (cpu_features & X86_CPU_MMX) {
282
283#ifdef USE_3DNOW_ASM
284		   if (cpu_ext_features & X86_CPUEXT_3DNOW)
285		       _mesa_x86_cpu_features |= X86_FEATURE_3DNOW;
286		   if (cpu_ext_features & X86_CPUEXT_3DNOW_EXT)
287		       _mesa_x86_cpu_features |= X86_FEATURE_3DNOWEXT;
288#endif
289
290#ifdef USE_MMX_ASM
291		   if (cpu_ext_features & X86_CPUEXT_MMX_EXT)
292		       _mesa_x86_cpu_features |= X86_FEATURE_MMXEXT;
293#endif
294	       }
295	   }
296
297	   /* query cpu name */
298	   if (cpu_ext_info >= 0x80000002) {
299	       GLuint ofs;
300	       char cpu_name[49];
301	       for (ofs = 0; ofs < 3; ofs++)
302		   _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));
303	       cpu_name[48] = '\0'; /* the name should be NULL terminated, but just to be sure */
304
305	       if (detection_debug)
306		  _mesa_debug(NULL, "CPU name: %s\n", cpu_name);
307	   }
308       }
309
310   }
311
312#ifdef USE_MMX_ASM
313   if ( cpu_has_mmx ) {
314      if ( _mesa_getenv( "MESA_NO_MMX" ) == 0 ) {
315	 if (detection_debug)
316	    _mesa_debug(NULL, "MMX cpu detected.\n");
317      } else {
318         _mesa_x86_cpu_features &= ~(X86_FEATURE_MMX);
319      }
320   }
321#endif
322
323#ifdef USE_3DNOW_ASM
324   if ( cpu_has_3dnow ) {
325      if ( _mesa_getenv( "MESA_NO_3DNOW" ) == 0 ) {
326	 if (detection_debug)
327	    _mesa_debug(NULL, "3DNow! cpu detected.\n");
328      } else {
329         _mesa_x86_cpu_features &= ~(X86_FEATURE_3DNOW);
330      }
331   }
332#endif
333
334#ifdef USE_SSE_ASM
335   if ( cpu_has_xmm ) {
336      if ( _mesa_getenv( "MESA_NO_SSE" ) == 0 ) {
337	 if (detection_debug)
338	    _mesa_debug(NULL, "SSE cpu detected.\n");
339         if ( _mesa_getenv( "MESA_FORCE_SSE" ) == 0 ) {
340            _mesa_check_os_sse_support();
341         }
342      } else {
343         _mesa_debug(NULL, "SSE cpu detected, but switched off by user.\n");
344         _mesa_x86_cpu_features &= ~(X86_FEATURE_XMM);
345      }
346   }
347#endif
348
349#elif defined(USE_X86_64_ASM)
350   {
351      unsigned int uninitialized_var(eax), uninitialized_var(ebx),
352                   uninitialized_var(ecx), uninitialized_var(edx);
353
354      /* Always available on x86-64. */
355      _mesa_x86_cpu_features |= X86_FEATURE_XMM | X86_FEATURE_XMM2;
356
357      __get_cpuid(1, &eax, &ebx, &ecx, &edx);
358
359      if (ecx & bit_SSE4_1)
360         _mesa_x86_cpu_features |= X86_FEATURE_SSE4_1;
361   }
362#endif /* USE_X86_64_ASM */
363
364   (void) detection_debug;
365}
366