util: Remove the x86 exception handlers.
[mesa.git] / src / gallium / auxiliary / util / u_cpu_detect.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Dennis Smit
4 * 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * AUTHORS, COPYRIGHT HOLDERS, AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * CPU feature detection.
30 *
31 * @author Dennis Smit
32 * @author Based on the work of Eric Anholt <anholt@FreeBSD.org>
33 */
34
35 #include "pipe/p_config.h"
36
37 #include "u_debug.h"
38 #include "u_cpu_detect.h"
39
40 #if defined(PIPE_ARCH_PPC)
41 #if defined(PIPE_OS_APPLE)
42 #include <sys/sysctl.h>
43 #else
44 #include <signal.h>
45 #include <setjmp.h>
46 #endif
47 #endif
48
49 #if defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD)
50 #include <sys/param.h>
51 #include <sys/sysctl.h>
52 #include <machine/cpu.h>
53 #endif
54
55 #if defined(PIPE_OS_FREEBSD)
56 #include <sys/types.h>
57 #include <sys/sysctl.h>
58 #endif
59
60 #if defined(PIPE_OS_LINUX)
61 #include <signal.h>
62 #endif
63
64 #ifdef PIPE_OS_UNIX
65 #include <unistd.h>
66 #endif
67
68 #if defined(PIPE_OS_WINDOWS)
69 #include <windows.h>
70 #if defined(MSVC)
71 #include <intrin.h>
72 #endif
73 #endif
74
75
76 DEBUG_GET_ONCE_BOOL_OPTION(dump_cpu, "GALLIUM_DUMP_CPU", FALSE)
77
78
79 struct util_cpu_caps util_cpu_caps;
80
81 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
82 static int has_cpuid(void);
83 #endif
84
85
86 #if defined(PIPE_ARCH_PPC) && !defined(PIPE_OS_APPLE)
87 static jmp_buf __lv_powerpc_jmpbuf;
88 static volatile sig_atomic_t __lv_powerpc_canjump = 0;
89
90 static void
91 sigill_handler(int sig)
92 {
93 if (!__lv_powerpc_canjump) {
94 signal (sig, SIG_DFL);
95 raise (sig);
96 }
97
98 __lv_powerpc_canjump = 0;
99 longjmp(__lv_powerpc_jmpbuf, 1);
100 }
101 #endif
102
103 #if defined(PIPE_ARCH_PPC)
104 static void
105 check_os_altivec_support(void)
106 {
107 #if defined(PIPE_OS_APPLE)
108 int sels[2] = {CTL_HW, HW_VECTORUNIT};
109 int has_vu = 0;
110 int len = sizeof (has_vu);
111 int err;
112
113 err = sysctl(sels, 2, &has_vu, &len, NULL, 0);
114
115 if (err == 0) {
116 if (has_vu != 0) {
117 util_cpu_caps.has_altivec = 1;
118 }
119 }
120 #else /* !PIPE_OS_APPLE */
121 /* not on Apple/Darwin, do it the brute-force way */
122 /* this is borrowed from the libmpeg2 library */
123 signal(SIGILL, sigill_handler);
124 if (setjmp(__lv_powerpc_jmpbuf)) {
125 signal(SIGILL, SIG_DFL);
126 } else {
127 __lv_powerpc_canjump = 1;
128
129 __asm __volatile
130 ("mtspr 256, %0\n\t"
131 "vand %%v0, %%v0, %%v0"
132 :
133 : "r" (-1));
134
135 signal(SIGILL, SIG_DFL);
136 util_cpu_caps.has_altivec = 1;
137 }
138 #endif /* !PIPE_OS_APPLE */
139 }
140 #endif /* PIPE_ARCH_PPC */
141
142
143 #if defined(PIPE_ARCH_X86) || defined (PIPE_ARCH_X86_64)
144 static int has_cpuid(void)
145 {
146 #if defined(PIPE_ARCH_X86)
147 #if defined(PIPE_OS_GCC)
148 int a, c;
149
150 __asm __volatile
151 ("pushf\n"
152 "popl %0\n"
153 "movl %0, %1\n"
154 "xorl $0x200000, %0\n"
155 "push %0\n"
156 "popf\n"
157 "pushf\n"
158 "popl %0\n"
159 : "=a" (a), "=c" (c)
160 :
161 : "cc");
162
163 return a != c;
164 #else
165 /* FIXME */
166 return 1;
167 #endif
168 #elif defined(PIPE_ARCH_X86_64)
169 return 1;
170 #else
171 return 0;
172 #endif
173 }
174
175
176 /**
177 * @sa cpuid.h included in gcc-4.3 onwards.
178 * @sa http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
179 */
180 static INLINE void
181 cpuid(uint32_t ax, uint32_t *p)
182 {
183 #if defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86)
184 __asm __volatile (
185 "xchgl %%ebx, %1\n\t"
186 "cpuid\n\t"
187 "xchgl %%ebx, %1"
188 : "=a" (p[0]),
189 "=S" (p[1]),
190 "=c" (p[2]),
191 "=d" (p[3])
192 : "0" (ax)
193 );
194 #elif defined(PIPE_CC_GCC) && defined(PIPE_ARCH_X86_64)
195 __asm __volatile (
196 "cpuid\n\t"
197 : "=a" (p[0]),
198 "=b" (p[1]),
199 "=c" (p[2]),
200 "=d" (p[3])
201 : "0" (ax)
202 );
203 #elif defined(PIPE_CC_MSVC)
204 __cpuid(p, ax);
205 #else
206 p[0] = 0;
207 p[1] = 0;
208 p[2] = 0;
209 p[3] = 0;
210 #endif
211 }
212 #endif /* X86 or X86_64 */
213
214 void
215 util_cpu_detect(void)
216 {
217 static boolean util_cpu_detect_initialized = FALSE;
218
219 if(util_cpu_detect_initialized)
220 return;
221
222 memset(&util_cpu_caps, 0, sizeof util_cpu_caps);
223
224 /* Count the number of CPUs in system */
225 #if defined(PIPE_OS_WINDOWS)
226 {
227 SYSTEM_INFO system_info;
228 GetSystemInfo(&system_info);
229 util_cpu_caps.nr_cpus = system_info.dwNumberOfProcessors;
230 }
231 #elif defined(PIPE_OS_UNIX) && defined(_SC_NPROCESSORS_ONLN)
232 util_cpu_caps.nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
233 if (util_cpu_caps.nr_cpus == -1)
234 util_cpu_caps.nr_cpus = 1;
235 #elif defined(PIPE_OS_BSD)
236 {
237 int mib[2], ncpu;
238 int len;
239
240 mib[0] = CTL_HW;
241 mib[1] = HW_NCPU;
242
243 len = sizeof (ncpu);
244 sysctl(mib, 2, &ncpu, &len, NULL, 0);
245 util_cpu_caps.nr_cpus = ncpu;
246 }
247 #else
248 util_cpu_caps.nr_cpus = 1;
249 #endif
250
251 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
252 if (has_cpuid()) {
253 uint32_t regs[4];
254 uint32_t regs2[4];
255
256 util_cpu_caps.cacheline = 32;
257
258 /* Get max cpuid level */
259 cpuid(0x00000000, regs);
260
261 if (regs[0] >= 0x00000001) {
262 unsigned int cacheline;
263
264 cpuid (0x00000001, regs2);
265
266 util_cpu_caps.x86_cpu_type = (regs2[0] >> 8) & 0xf;
267 if (util_cpu_caps.x86_cpu_type == 0xf)
268 util_cpu_caps.x86_cpu_type = 8 + ((regs2[0] >> 20) & 255); /* use extended family (P4, IA64) */
269
270 /* general feature flags */
271 util_cpu_caps.has_tsc = (regs2[3] & (1 << 8 )) >> 8; /* 0x0000010 */
272 util_cpu_caps.has_mmx = (regs2[3] & (1 << 23 )) >> 23; /* 0x0800000 */
273 util_cpu_caps.has_sse = (regs2[3] & (1 << 25 )) >> 25; /* 0x2000000 */
274 util_cpu_caps.has_sse2 = (regs2[3] & (1 << 26 )) >> 26; /* 0x4000000 */
275 util_cpu_caps.has_sse3 = (regs2[2] & (1)); /* 0x0000001 */
276 util_cpu_caps.has_ssse3 = (regs2[2] & (1 << 9 )) >> 9; /* 0x0000020 */
277 util_cpu_caps.has_sse4_1 = (regs2[2] & (1 << 19)) >> 19;
278 util_cpu_caps.has_mmx2 = util_cpu_caps.has_sse; /* SSE cpus supports mmxext too */
279
280 cacheline = ((regs2[1] >> 8) & 0xFF) * 8;
281 if (cacheline > 0)
282 util_cpu_caps.cacheline = cacheline;
283 }
284
285 cpuid(0x80000000, regs);
286
287 if (regs[0] >= 0x80000001) {
288
289 cpuid(0x80000001, regs2);
290
291 util_cpu_caps.has_mmx |= (regs2[3] & (1 << 23 )) >> 23; /* 0x0800000 */
292 util_cpu_caps.has_mmx2 |= (regs2[3] & (1 << 22 )) >> 22; /* 0x400000 */
293 util_cpu_caps.has_3dnow = (regs2[3] & (1 << 31 )) >> 31; /* 0x80000000 */
294 util_cpu_caps.has_3dnow_ext = (regs2[3] & (1 << 30 )) >> 30;
295 }
296
297 if (regs[0] >= 0x80000006) {
298 cpuid(0x80000006, regs2);
299 util_cpu_caps.cacheline = regs2[2] & 0xFF;
300 }
301
302 if (!util_cpu_caps.has_sse) {
303 util_cpu_caps.has_sse2 = 0;
304 util_cpu_caps.has_sse3 = 0;
305 util_cpu_caps.has_ssse3 = 0;
306 util_cpu_caps.has_sse4_1 = 0;
307 }
308 }
309 #endif /* PIPE_ARCH_X86 || PIPE_ARCH_X86_64 */
310
311 #if defined(PIPE_ARCH_PPC)
312 check_os_altivec_support();
313 #endif /* PIPE_ARCH_PPC */
314
315 #ifdef DEBUG
316 if (debug_get_option_dump_cpu()) {
317 debug_printf("util_cpu_caps.nr_cpus = %u\n", util_cpu_caps.nr_cpus);
318
319 debug_printf("util_cpu_caps.x86_cpu_type = %u\n", util_cpu_caps.x86_cpu_type);
320 debug_printf("util_cpu_caps.cacheline = %u\n", util_cpu_caps.cacheline);
321
322 debug_printf("util_cpu_caps.has_tsc = %u\n", util_cpu_caps.has_tsc);
323 debug_printf("util_cpu_caps.has_mmx = %u\n", util_cpu_caps.has_mmx);
324 debug_printf("util_cpu_caps.has_mmx2 = %u\n", util_cpu_caps.has_mmx2);
325 debug_printf("util_cpu_caps.has_sse = %u\n", util_cpu_caps.has_sse);
326 debug_printf("util_cpu_caps.has_sse2 = %u\n", util_cpu_caps.has_sse2);
327 debug_printf("util_cpu_caps.has_sse3 = %u\n", util_cpu_caps.has_sse3);
328 debug_printf("util_cpu_caps.has_ssse3 = %u\n", util_cpu_caps.has_ssse3);
329 debug_printf("util_cpu_caps.has_sse4_1 = %u\n", util_cpu_caps.has_sse4_1);
330 debug_printf("util_cpu_caps.has_3dnow = %u\n", util_cpu_caps.has_3dnow);
331 debug_printf("util_cpu_caps.has_3dnow_ext = %u\n", util_cpu_caps.has_3dnow_ext);
332 debug_printf("util_cpu_caps.has_altivec = %u\n", util_cpu_caps.has_altivec);
333 }
334 #endif
335
336 util_cpu_detect_initialized = TRUE;
337 }