base/dispatch.c: Check operating system support for XMM registers.
[catacomb] / base / dispatch.c
1 /* -*-c-*-
2 *
3 * CPU-specific dispatch
4 *
5 * (c) 2015 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <mLib/macros.h>
37
38 #include "dispatch.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 #ifdef CPUFAM_X86
43
44 #define EFLAGS_ID (1u << 21)
45 #define CPUID1D_SSE2 (1u << 26)
46 #define CPUID1D_FXSR (1u << 24)
47
48 struct cpuid { unsigned a, b, c, d; };
49
50 /* --- @cpuid@ --- *
51 *
52 * Arguments: @struct cpuid *cc@ = where to write the result
53 * @unsigned a, c@ = EAX and ECX registers to set
54 *
55 * Returns: ---
56 *
57 * Use: Minimal C wrapper around the x86 `CPUID' instruction. Checks
58 * that the instruction is actually available before invoking
59 * it; fills the output structure with zero if it's not going to
60 * work.
61 */
62
63 #ifdef __GNUC__
64 static __inline__ unsigned getflags(void)
65 { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); }
66 static __inline__ unsigned setflags(unsigned f)
67 {
68 unsigned ff;
69 __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf"
70 : "=g" (ff)
71 : "g" (f));
72 return (ff);
73 }
74 #endif
75
76 static void cpuid(struct cpuid *cc, unsigned a, unsigned c)
77 {
78 #ifdef __GNUC__
79 unsigned f;
80 #endif
81
82 cc->a = cc->b = cc->c = cc->d = 0;
83
84 #ifdef __GNUC__
85 /* Stupid dance to detect whether the CPUID instruction is available. */
86 f = getflags();
87 if (!(setflags(f | EFLAGS_ID) & EFLAGS_ID)) return;
88 if ( setflags(f & ~EFLAGS_ID) & EFLAGS_ID ) return;
89 setflags(f);
90
91 /* Alas, EBX is magical in PIC code, so abuse ESI instead. This isn't
92 * pretty, but it works.
93 */
94 __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx"
95 : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
96 : "a" (a) , "c" (c));
97 #endif
98 }
99
100 static unsigned cpuid_maxleaf(void)
101 { struct cpuid c; cpuid(&c, 0, 0); return (c.a); }
102
103 static int cpuid_features_p(unsigned dbits, unsigned cbits)
104 {
105 struct cpuid c;
106 if (cpuid_maxleaf() < 1) return (0);
107 cpuid(&c, 1, 0);
108 return ((c.d & dbits) == dbits && (c.c & cbits) == cbits);
109 }
110
111 static int xmm_registers_available_p(void)
112 {
113 #ifdef __GNUC__
114 unsigned f;
115 /* This hack is by Agner Fog. Use FXSAVE/FXRSTOR to figure out whether the
116 * XMM registers are actually alive.
117 */
118 if (!cpuid_features_p(CPUID1D_FXSR, 0)) return (0);
119 __asm__ ("movl %%esp, %%edx; subl $512, %%esp; andl $~15, %%esp\n"
120 "fxsave (%%esp)\n"
121 "movl 160(%%esp), %%eax; xorl $0xaaaa5555, 160(%%esp)\n"
122 "fxrstor (%%esp); fxsave (%%esp)\n"
123 "movl 160(%%esp), %%ecx; movl %%eax, 160(%%esp)\n"
124 "fxrstor (%%esp); movl %%edx, %%esp\n"
125 "xorl %%ecx, %%eax"
126 : "=a" (f)
127 : /* no inputs */
128 : "%ecx", "%edx");
129 return (f);
130 #else
131 return (0);
132 #endif
133 }
134
135 #endif
136
137 /* --- @check_env@ --- *
138 *
139 * Arguments: @const char *ftok@ = feature token
140 *
141 * Returns: Zero if the feature is forced off; positive if it's forced
142 * on; negative if the user hasn't decided.
143 *
144 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
145 * feature token @ftok@. The variable, if it exists, should be
146 * a space-separated sequence of `+tok' and `-tok' items. These
147 * tokens may end in `*', which matches any suffix.
148 */
149
150 static int IGNORABLE check_env(const char *ftok)
151 {
152 const char *p, *q, *pp;
153 int d;
154
155 p = getenv("CATACOMB_CPUFEAT");
156 if (!p) return (-1);
157
158 for (;;) {
159 while (isspace((unsigned char)*p)) p++;
160 if (!*p) return (-1);
161 switch (*p) {
162 case '+': d = +1; p++; break;
163 case '-': d = 0; p++; break;
164 default: d = -1; break;
165 }
166 for (q = p; *q && !isspace((unsigned char)*q); q++);
167 if (d >= 0) {
168 for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++);
169 if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d);
170 }
171 p = q;
172 }
173 return (-1);
174 }
175
176 /* --- @cpu_feature_p@ --- *
177 *
178 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
179 *
180 * Returns: Nonzero if the feature is available.
181 */
182
183 #include <stdio.h>
184
185 int cpu_feature_p(int feat)
186 {
187 int IGNORABLE f;
188 IGNORE(f);
189 #define CHECK_ENV(ftok) \
190 do { if ((f = check_env(ftok)) >= 0) return (f); } while (0)
191
192 switch (feat) {
193 #ifdef CPUFAM_X86
194 case CPUFEAT_X86_SSE2: {
195 CHECK_ENV("x86:sse2");
196 return (xmm_registers_available_p() &&
197 cpuid_features_p(CPUID1D_SSE2, 0));
198 }
199 #endif
200 default:
201 return (0);
202 }
203 #undef CHECK_ENV
204 }
205
206 /*----- That's all, folks -------------------------------------------------*/