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 ------------------------------------------------------*/
36 #include <mLib/macros.h>
40 /*----- Intel x86/AMD64 feature probing -----------------------------------*/
44 #define EFLAGS_ID (1u << 21)
45 #define CPUID1D_SSE2 (1u << 26)
46 #define CPUID1D_FXSR (1u << 24)
47 #define CPUID1C_AESNI (1u << 25)
49 struct cpuid { unsigned a, b, c, d; };
53 * Arguments: @struct cpuid *cc@ = where to write the result
54 * @unsigned a, c@ = EAX and ECX registers to set
58 * Use: Minimal C wrapper around the x86 `CPUID' instruction. Checks
59 * that the instruction is actually available before invoking
60 * it; fills the output structure with zero if it's not going to
65 static __inline__ unsigned getflags(void)
66 { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); }
67 static __inline__ unsigned setflags(unsigned f)
70 __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf"
77 static void cpuid(struct cpuid *cc, unsigned a, unsigned c)
83 cc->a = cc->b = cc->c = cc->d = 0;
86 /* Stupid dance to detect whether the CPUID instruction is available. */
88 if (!(setflags(f | EFLAGS_ID) & EFLAGS_ID)) return;
89 if ( setflags(f & ~EFLAGS_ID) & EFLAGS_ID ) return;
92 /* Alas, EBX is magical in PIC code, so abuse ESI instead. This isn't
93 * pretty, but it works.
95 __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx"
96 : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
101 static unsigned cpuid_maxleaf(void)
102 { struct cpuid c; cpuid(&c, 0, 0); return (c.a); }
104 static int cpuid_features_p(unsigned dbits, unsigned cbits)
107 if (cpuid_maxleaf() < 1) return (0);
109 return ((c.d & dbits) == dbits && (c.c & cbits) == cbits);
112 static int xmm_registers_available_p(void)
116 /* This hack is by Agner Fog. Use FXSAVE/FXRSTOR to figure out whether the
117 * XMM registers are actually alive.
119 if (!cpuid_features_p(CPUID1D_FXSR, 0)) return (0);
120 __asm__ ("movl %%esp, %%edx; subl $512, %%esp; andl $~15, %%esp\n"
122 "movl 160(%%esp), %%eax; xorl $0xaaaa5555, 160(%%esp)\n"
123 "fxrstor (%%esp); fxsave (%%esp)\n"
124 "movl 160(%%esp), %%ecx; movl %%eax, 160(%%esp)\n"
125 "fxrstor (%%esp); movl %%edx, %%esp\n"
138 /*----- External interface ------------------------------------------------*/
140 /* --- @check_env@ --- *
142 * Arguments: @const char *ftok@ = feature token
144 * Returns: Zero if the feature is forced off; positive if it's forced
145 * on; negative if the user hasn't decided.
147 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
148 * feature token @ftok@. The variable, if it exists, should be
149 * a space-separated sequence of `+tok' and `-tok' items. These
150 * tokens may end in `*', which matches any suffix.
153 static int IGNORABLE check_env(const char *ftok)
155 const char *p, *q, *pp;
158 p = getenv("CATACOMB_CPUFEAT");
162 while (isspace((unsigned char)*p)) p++;
163 if (!*p) return (-1);
165 case '+': d = +1; p++; break;
166 case '-': d = 0; p++; break;
167 default: d = -1; break;
169 for (q = p; *q && !isspace((unsigned char)*q); q++);
171 for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++);
172 if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d);
179 /* --- @cpu_feature_p@ --- *
181 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
183 * Returns: Nonzero if the feature is available.
188 int cpu_feature_p(int feat)
192 #define CHECK_ENV(ftok) \
193 do { if ((f = check_env(ftok)) >= 0) return (f); } while (0)
197 case CPUFEAT_X86_SSE2: {
198 CHECK_ENV("x86:sse2");
199 return (xmm_registers_available_p() &&
200 cpuid_features_p(CPUID1D_SSE2, 0));
202 case CPUFEAT_X86_AESNI: {
203 check_env("x86:aesni");
204 return (xmm_registers_available_p() &&
205 cpuid_features_p(CPUID1D_SSE2, CPUID1C_AESNI));
214 /*----- That's all, folks -------------------------------------------------*/