Add support for AMD64 processors and Microsoft Windows.
[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 <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <mLib/macros.h>
39
40 #include "dispatch.h"
41
42 /*----- Intel x86/AMD64 feature probing -----------------------------------*/
43
44 #if CPUFAM_X86 || CPUFAM_AMD64
45
46 # define EFLAGS_ID (1u << 21)
47 # define CPUID1D_SSE2 (1u << 26)
48 # define CPUID1D_FXSR (1u << 24)
49 # define CPUID1C_AESNI (1u << 25)
50
51 struct cpuid { unsigned a, b, c, d; };
52
53 /* --- @cpuid@ --- *
54 *
55 * Arguments: @struct cpuid *cc@ = where to write the result
56 * @unsigned a, c@ = EAX and ECX registers to set
57 *
58 * Returns: ---
59 *
60 * Use: Minimal C wrapper around the x86 `CPUID' instruction. Checks
61 * that the instruction is actually available before invoking
62 * it; fills the output structure with zero if it's not going to
63 * work.
64 */
65
66 #ifdef __GNUC__
67 # if CPUFAM_X86
68 static __inline__ unsigned getflags(void)
69 { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); }
70 static __inline__ unsigned setflags(unsigned f)
71 {
72 unsigned ff;
73 __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf"
74 : "=g" (ff)
75 : "g" (f));
76 return (ff);
77 }
78 # else
79 static __inline__ unsigned long getflags(void)
80 { unsigned long f; __asm__ ("pushf; popq %0" : "=g" (f)); return (f); }
81 static __inline__ unsigned long long setflags(unsigned long f)
82 {
83 unsigned long ff;
84 __asm__ ("pushf; pushq %1; popf; pushf; popq %0; popf"
85 : "=g" (ff)
86 : "g" (f));
87 return (ff);
88 }
89 # endif
90 #endif
91
92 static void cpuid(struct cpuid *cc, unsigned a, unsigned c)
93 {
94 #ifdef __GNUC__
95 unsigned f;
96 #endif
97
98 cc->a = cc->b = cc->c = cc->d = 0;
99
100 #ifdef __GNUC__
101 /* Stupid dance to detect whether the CPUID instruction is available. */
102 f = getflags();
103 if (!(setflags(f | EFLAGS_ID) & EFLAGS_ID) ||
104 setflags(f & ~EFLAGS_ID) & EFLAGS_ID) {
105 dispatch_debug("CPUID instruction not available");
106 return;
107 }
108 setflags(f);
109
110 /* Alas, EBX is magical in PIC code, so abuse ESI instead. This isn't
111 * pretty, but it works.
112 */
113 # if CPUFAM_X86
114 __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx"
115 : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
116 : "a" (a) , "c" (c));
117 # elif CPUFAM_AMD64
118 __asm__ ("pushq %%rbx; cpuid; movl %%ebx, %%esi; popq %%rbx"
119 : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
120 : "a" (a) , "c" (c));
121 # else
122 # error "I'm confused."
123 # endif
124 dispatch_debug("CPUID(%08x, %08x) -> %08x, %08x, %08x, %08x",
125 a, c, cc->a, cc->b, cc->c, cc->d);
126 #else
127 dispatch_debug("GNU inline assembler not available; can't CPUID");
128 #endif
129 }
130
131 static unsigned cpuid_maxleaf(void)
132 { struct cpuid c; cpuid(&c, 0, 0); return (c.a); }
133
134 /* --- @cpuid_features_p@ --- *
135 *
136 * Arguments: @unsigned dbits@ = bits to check in EDX
137 * @unsigned cbits@ = bits to check in ECX
138 *
139 * Returns: Nonzero if all the requested bits are set in the CPUID result
140 * on leaf 1.
141 */
142
143 static int cpuid_features_p(unsigned dbits, unsigned cbits)
144 {
145 struct cpuid c;
146 if (cpuid_maxleaf() < 1) return (0);
147 cpuid(&c, 1, 0);
148 return ((c.d & dbits) == dbits && (c.c & cbits) == cbits);
149 }
150
151 /* --- @xmm_registers_available_p@ --- *
152 *
153 * Arguments: ---
154 *
155 * Returns: Nonzero if the operating system has made the XMM registers
156 * available for use.
157 */
158
159 static int xmm_registers_available_p(void)
160 {
161 #ifdef __GNUC__
162 unsigned f;
163 /* This hack is by Agner Fog. Use FXSAVE/FXRSTOR to figure out whether the
164 * XMM registers are actually alive.
165 */
166 if (!cpuid_features_p(CPUID1D_FXSR, 0)) return (0);
167 # if CPUFAM_X86
168 __asm__ ("movl %%esp, %%edx; subl $512, %%esp; andl $~15, %%esp\n"
169 "fxsave (%%esp)\n"
170 "movl 160(%%esp), %%eax; xorl $0xaaaa5555, 160(%%esp)\n"
171 "fxrstor (%%esp); fxsave (%%esp)\n"
172 "movl 160(%%esp), %%ecx; movl %%eax, 160(%%esp)\n"
173 "fxrstor (%%esp); movl %%edx, %%esp\n"
174 "xorl %%ecx, %%eax"
175 : "=a" (f)
176 : /* no inputs */
177 : "%ecx", "%edx");
178 # elif CPUFAM_AMD64
179 __asm__ ("movq %%rsp, %%rdx; subq $512, %%rsp; andq $~15, %%rsp\n"
180 "fxsave (%%rsp)\n"
181 "movl 160(%%rsp), %%eax; xorl $0xaaaa5555, 160(%%rsp)\n"
182 "fxrstor (%%rsp); fxsave (%%rsp)\n"
183 "movl 160(%%rsp), %%ecx; movl %%eax, 160(%%rsp)\n"
184 "fxrstor (%%rsp); movq %%rdx, %%rsp\n"
185 "xorl %%ecx, %%eax"
186 : "=a" (f)
187 : /* no inputs */
188 : "%ecx", "%rdx");
189 # else
190 # error "I'm confused."
191 # endif
192 dispatch_debug("XMM registers %savailable", f ? "" : "not ");
193 return (f);
194 #else
195 dispatch_debug("GNU inline assembler not available; can't check for XMM");
196 return (0);
197 #endif
198 }
199
200 #endif
201
202 /*----- External interface ------------------------------------------------*/
203
204 /* --- @dispatch_debug@ --- *
205 *
206 * Arguments: @const char *fmt@ = a format string
207 * @...@ = additional arguments
208 *
209 * Returns: ---
210 *
211 * Use: Writes a formatted message to standard output if dispatch
212 * debugging is enabled.
213 */
214
215 void dispatch_debug(const char *fmt, ...)
216 {
217 va_list ap;
218 const char *e = getenv("CATACOMB_CPUDISPATCH_DEBUG");
219
220 if (e && *e != 'n' && *e != '0') {
221 va_start(ap, fmt);
222 fputs("Catacomb CPUDISPATCH: ", stderr);
223 vfprintf(stderr, fmt, ap);
224 fputc('\n', stderr);
225 va_end(ap);
226 }
227 }
228
229 /* --- @check_env@ --- *
230 *
231 * Arguments: @const char *ftok@ = feature token
232 *
233 * Returns: Zero if the feature is forced off; positive if it's forced
234 * on; negative if the user hasn't decided.
235 *
236 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
237 * feature token @ftok@. The variable, if it exists, should be
238 * a space-separated sequence of `+tok' and `-tok' items. These
239 * tokens may end in `*', which matches any suffix.
240 */
241
242 static int IGNORABLE check_env(const char *ftok)
243 {
244 const char *p, *q, *pp;
245 int d;
246
247 p = getenv("CATACOMB_CPUFEAT");
248 if (!p) return (-1);
249
250 for (;;) {
251 while (isspace((unsigned char)*p)) p++;
252 if (!*p) return (-1);
253 switch (*p) {
254 case '+': d = +1; p++; break;
255 case '-': d = 0; p++; break;
256 default: d = -1; break;
257 }
258 for (q = p; *q && !isspace((unsigned char)*q); q++);
259 if (d >= 0) {
260 for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++);
261 if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d);
262 }
263 p = q;
264 }
265 return (-1);
266 }
267
268 /* --- @cpu_feature_p@ --- *
269 *
270 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
271 *
272 * Returns: Nonzero if the feature is available.
273 */
274
275 #include <stdio.h>
276
277 static int IGNORABLE
278 feat_debug(const char *ftok, const char *check, int verdict)
279 {
280 if (verdict >= 0) {
281 dispatch_debug("feature `%s': %s -> %s", ftok, check,
282 verdict ? "available" : "absent");
283 }
284 return (verdict);
285 }
286
287 int cpu_feature_p(int feat)
288 {
289 int IGNORABLE f;
290 IGNORE(f);
291 #define CASE_CPUFEAT(feat, ftok, cond) case CPUFEAT_##feat: \
292 if ((f = feat_debug(ftok, "environment override", \
293 check_env(ftok))) >= 0) \
294 return (f); \
295 else \
296 return (feat_debug(ftok, "runtime probe", cond));
297
298 switch (feat) {
299 #if CPUFAM_X86 || CPUFAM_AMD64
300 CASE_CPUFEAT(X86_SSE2, "x86:sse2",
301 xmm_registers_available_p() &&
302 cpuid_features_p(CPUID1D_SSE2, 0));
303 CASE_CPUFEAT(X86_AESNI, "x86:aesni",
304 xmm_registers_available_p() &&
305 cpuid_features_p(CPUID1D_SSE2, CPUID1C_AESNI));
306 #endif
307 default:
308 dispatch_debug("denying unknown feature %d", feat);
309 return (0);
310 }
311 #undef CASE_CPUFEAT
312 }
313
314 /*----- That's all, folks -------------------------------------------------*/