chiark / gitweb /
4b5e17a99cfeafc8c146ea3db0a9c1c379ba4541
[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 /*----- Intel x86/AMD64 feature probing -----------------------------------*/
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 #define CPUID1C_AESNI (1u << 25)
48
49 struct cpuid { unsigned a, b, c, d; };
50
51 /* --- @cpuid@ --- *
52  *
53  * Arguments:   @struct cpuid *cc@ = where to write the result
54  *              @unsigned a, c@ = EAX and ECX registers to set
55  *
56  * Returns:     ---
57  *
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
61  *              work.
62  */
63
64 #ifdef __GNUC__
65 static __inline__ unsigned getflags(void)
66   { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); }
67 static __inline__ unsigned setflags(unsigned f)
68 {
69   unsigned ff;
70   __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf"
71            : "=g" (ff)
72            : "g" (f));
73   return (ff);
74 }
75 #endif
76
77 static void cpuid(struct cpuid *cc, unsigned a, unsigned c)
78 {
79 #ifdef __GNUC__
80   unsigned f;
81 #endif
82
83   cc->a = cc->b = cc->c = cc->d = 0;
84
85 #ifdef __GNUC__
86   /* Stupid dance to detect whether the CPUID instruction is available. */
87   f = getflags();
88   if (!(setflags(f |  EFLAGS_ID) & EFLAGS_ID)) return;
89   if (  setflags(f & ~EFLAGS_ID) & EFLAGS_ID ) return;
90   setflags(f);
91
92   /* Alas, EBX is magical in PIC code, so abuse ESI instead.  This isn't
93    * pretty, but it works.
94    */
95   __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx"
96            : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d)
97            : "a" (a) , "c" (c));
98 #endif
99 }
100
101 static unsigned cpuid_maxleaf(void)
102   { struct cpuid c; cpuid(&c, 0, 0); return (c.a); }
103
104 /* --- @cpuid_features_p@ --- *
105  *
106  * Arguments:   @unsigned dbits@ = bits to check in EDX
107  *              @unsigned cbits@ = bits to check in ECX
108  *
109  * Returns:     Nonzero if all the requested bits are set in the CPUID result
110  *              on leaf 1.
111  */
112
113 static int cpuid_features_p(unsigned dbits, unsigned cbits)
114 {
115   struct cpuid c;
116   if (cpuid_maxleaf() < 1) return (0);
117   cpuid(&c, 1, 0);
118   return ((c.d & dbits) == dbits && (c.c & cbits) == cbits);
119 }
120
121 /* --- @xmm_registers_available_p@ --- *
122  *
123  * Arguments:   ---
124  *
125  * Returns:     Nonzero if the operating system has made the XMM registers
126  *              available for use.
127  */
128
129 static int xmm_registers_available_p(void)
130 {
131 #ifdef __GNUC__
132   unsigned f;
133   /* This hack is by Agner Fog.  Use FXSAVE/FXRSTOR to figure out whether the
134    * XMM registers are actually alive.
135    */
136   if (!cpuid_features_p(CPUID1D_FXSR, 0)) return (0);
137   __asm__ ("movl %%esp, %%edx; subl $512, %%esp; andl $~15, %%esp\n"
138            "fxsave (%%esp)\n"
139            "movl 160(%%esp), %%eax; xorl $0xaaaa5555, 160(%%esp)\n"
140            "fxrstor (%%esp); fxsave (%%esp)\n"
141            "movl 160(%%esp), %%ecx; movl %%eax, 160(%%esp)\n"
142            "fxrstor (%%esp); movl %%edx, %%esp\n"
143            "xorl %%ecx, %%eax"
144            : "=a" (f)
145            : /* no inputs */
146            : "%ecx", "%edx");
147   return (f);
148 #else
149   return (0);
150 #endif
151 }
152
153 #endif
154
155 /*----- External interface ------------------------------------------------*/
156
157 /* --- @check_env@ --- *
158  *
159  * Arguments:   @const char *ftok@ = feature token
160  *
161  * Returns:     Zero if the feature is forced off; positive if it's forced
162  *              on; negative if the user hasn't decided.
163  *
164  * Use:         Checks the environment variable `CATACOMB_CPUFEAT' for the
165  *              feature token @ftok@.  The variable, if it exists, should be
166  *              a space-separated sequence of `+tok' and `-tok' items.  These
167  *              tokens may end in `*', which matches any suffix.
168  */
169
170 static int IGNORABLE check_env(const char *ftok)
171 {
172   const char *p, *q, *pp;
173   int d;
174
175   p = getenv("CATACOMB_CPUFEAT");
176   if (!p) return (-1);
177
178   for (;;) {
179     while (isspace((unsigned char)*p)) p++;
180     if (!*p) return (-1);
181     switch (*p) {
182       case '+': d = +1; p++; break;
183       case '-': d =  0; p++; break;
184       default:  d = -1;      break;
185     }
186     for (q = p; *q && !isspace((unsigned char)*q); q++);
187     if (d >= 0) {
188       for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++);
189       if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d);
190     }
191     p = q;
192   }
193   return (-1);
194 }
195
196 /* --- @cpu_feature_p@ --- *
197  *
198  * Arguments:   @unsigned feat@ = a @CPUFEAT_...@ code
199  *
200  * Returns:     Nonzero if the feature is available.
201  */
202
203 #include <stdio.h>
204
205 int cpu_feature_p(int feat)
206 {
207   int IGNORABLE f;
208   IGNORE(f);
209 #define CHECK_ENV(ftok)                                                 \
210   do { if ((f = check_env(ftok)) >= 0) return (f); } while (0)
211
212   switch (feat) {
213 #ifdef CPUFAM_X86
214     case CPUFEAT_X86_SSE2: {
215       CHECK_ENV("x86:sse2");
216       return (xmm_registers_available_p() &&
217               cpuid_features_p(CPUID1D_SSE2, 0));
218     }
219     case CPUFEAT_X86_AESNI: {
220       check_env("x86:aesni");
221       return (xmm_registers_available_p() &&
222               cpuid_features_p(CPUID1D_SSE2, CPUID1C_AESNI));
223     }
224 #endif
225     default:
226       return (0);
227   }
228 #undef CHECK_ENV
229 }
230
231 /*----- That's all, folks -------------------------------------------------*/