3 * CPU-specific dispatch
5 * (c) 2015 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
38 #include <mLib/macros.h>
42 /*----- Intel x86/AMD64 feature probing -----------------------------------*/
46 #define EFLAGS_ID (1u << 21)
47 #define CPUID1D_SSE2 (1u << 26)
48 #define CPUID1D_FXSR (1u << 24)
49 #define CPUID1C_AESNI (1u << 25)
51 struct cpuid { unsigned a, b, c, d; };
55 * Arguments: @struct cpuid *cc@ = where to write the result
56 * @unsigned a, c@ = EAX and ECX registers to set
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
67 static __inline__ unsigned getflags(void)
68 { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); }
69 static __inline__ unsigned setflags(unsigned f)
72 __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf"
79 static void cpuid(struct cpuid *cc, unsigned a, unsigned c)
85 cc->a = cc->b = cc->c = cc->d = 0;
88 /* Stupid dance to detect whether the CPUID instruction is available. */
90 if (!(setflags(f | EFLAGS_ID) & EFLAGS_ID) ||
91 setflags(f & ~EFLAGS_ID) & EFLAGS_ID) {
92 dispatch_debug("CPUID instruction not available");
97 /* Alas, EBX is magical in PIC code, so abuse ESI instead. This isn't
98 * pretty, but it works.
100 __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx"
101 : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
102 : "a" (a) , "c" (c));
104 dispatch_debug("GNU inline assembler not available; can't CPUID");
108 static unsigned cpuid_maxleaf(void)
109 { struct cpuid c; cpuid(&c, 0, 0); return (c.a); }
111 /* --- @cpuid_features_p@ --- *
113 * Arguments: @unsigned dbits@ = bits to check in EDX
114 * @unsigned cbits@ = bits to check in ECX
116 * Returns: Nonzero if all the requested bits are set in the CPUID result
120 static int cpuid_features_p(unsigned dbits, unsigned cbits)
123 if (cpuid_maxleaf() < 1) return (0);
125 return ((c.d & dbits) == dbits && (c.c & cbits) == cbits);
128 /* --- @xmm_registers_available_p@ --- *
132 * Returns: Nonzero if the operating system has made the XMM registers
136 static int xmm_registers_available_p(void)
140 /* This hack is by Agner Fog. Use FXSAVE/FXRSTOR to figure out whether the
141 * XMM registers are actually alive.
143 if (!cpuid_features_p(CPUID1D_FXSR, 0)) return (0);
144 __asm__ ("movl %%esp, %%edx; subl $512, %%esp; andl $~15, %%esp\n"
146 "movl 160(%%esp), %%eax; xorl $0xaaaa5555, 160(%%esp)\n"
147 "fxrstor (%%esp); fxsave (%%esp)\n"
148 "movl 160(%%esp), %%ecx; movl %%eax, 160(%%esp)\n"
149 "fxrstor (%%esp); movl %%edx, %%esp\n"
156 dispatch_debug("GNU inline assembler not available; can't check for XMM");
163 /*----- External interface ------------------------------------------------*/
165 /* --- @dispatch_debug@ --- *
167 * Arguments: @const char *fmt@ = a format string
168 * @...@ = additional arguments
172 * Use: Writes a formatted message to standard output if dispatch
173 * debugging is enabled.
176 void dispatch_debug(const char *fmt, ...)
179 const char *e = getenv("CATACOMB_CPUDISPATCH_DEBUG");
181 if (e && *e != 'n' && *e != '0') {
183 fputs("Catacomb CPUDISPATCH: ", stderr);
184 vfprintf(stderr, fmt, ap);
190 /* --- @check_env@ --- *
192 * Arguments: @const char *ftok@ = feature token
194 * Returns: Zero if the feature is forced off; positive if it's forced
195 * on; negative if the user hasn't decided.
197 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
198 * feature token @ftok@. The variable, if it exists, should be
199 * a space-separated sequence of `+tok' and `-tok' items. These
200 * tokens may end in `*', which matches any suffix.
203 static int IGNORABLE check_env(const char *ftok)
205 const char *p, *q, *pp;
208 p = getenv("CATACOMB_CPUFEAT");
212 while (isspace((unsigned char)*p)) p++;
213 if (!*p) return (-1);
215 case '+': d = +1; p++; break;
216 case '-': d = 0; p++; break;
217 default: d = -1; break;
219 for (q = p; *q && !isspace((unsigned char)*q); q++);
221 for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++);
222 if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d);
229 /* --- @cpu_feature_p@ --- *
231 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
233 * Returns: Nonzero if the feature is available.
239 feat_debug(const char *ftok, const char *check, int verdict)
242 dispatch_debug("feature `%s': %s -> %s", ftok, check,
243 verdict ? "available" : "absent");
248 int cpu_feature_p(int feat)
252 #define CASE_CPUFEAT(feat, ftok, cond) case CPUFEAT_##feat: \
253 if ((f = feat_debug(ftok, "environment override", \
254 check_env(ftok))) >= 0) \
257 return (feat_debug(ftok, "runtime probe", cond));
261 CASE_CPUFEAT(X86_SSE2, "x86:sse2",
262 xmm_registers_available_p() &&
263 cpuid_features_p(CPUID1D_SSE2, 0));
264 CASE_CPUFEAT(X86_AESNI, "x86:aesni",
265 xmm_registers_available_p() &&
266 cpuid_features_p(CPUID1D_SSE2, CPUID1C_AESNI));
269 dispatch_debug("denying unknown feature %d", feat);
275 /*----- That's all, folks -------------------------------------------------*/