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 -----------------------------------*/
44 #if CPUFAM_X86 || CPUFAM_AMD64
46 # define CPUID1D_SSE2 (1u << 26)
47 # define CPUID1D_FXSR (1u << 24)
48 # define CPUID1C_PCLMUL (1u << 1)
49 # define CPUID1C_SSSE3 (1u << 9)
50 # define CPUID1C_AESNI (1u << 25)
51 # define CPUID1C_AVX (1u << 28)
52 # define CPUID1C_RDRAND (1u << 30)
54 struct cpuid
{ unsigned a
, b
, c
, d
; };
55 extern int dispatch_x86ish_cpuid(struct cpuid
*, unsigned a
, unsigned c
);
56 extern int dispatch_x86ish_xmmregisters_p(void);
57 extern int dispatch_x86ish_rdrand(unsigned *);
59 static void cpuid(struct cpuid
*cc
, unsigned a
, unsigned c
)
61 int rc
= dispatch_x86ish_cpuid(cc
, a
, c
);
63 dispatch_debug("CPUID instruction not available");
65 dispatch_debug("CPUID(%08x, %08x) -> %08x, %08x, %08x, %08x",
66 a
, c
, cc
->a
, cc
->b
, cc
->c
, cc
->d
);
69 static unsigned cpuid_maxleaf(void)
70 { struct cpuid c
; cpuid(&c
, 0, 0); return (c
.a
); }
72 /* --- @cpuid_features_p@ --- *
74 * Arguments: @unsigned dbits@ = bits to check in EDX
75 * @unsigned cbits@ = bits to check in ECX
77 * Returns: Nonzero if all the requested bits are set in the CPUID result
81 static int cpuid_features_p(unsigned dbits
, unsigned cbits
)
84 if (cpuid_maxleaf() < 1) return (0);
86 return ((c
.d
& dbits
) == dbits
&& (c
.c
& cbits
) == cbits
);
89 /* --- @xmm_registers_available_p@ --- *
93 * Returns: Nonzero if the operating system has made the XMM registers
97 static int xmm_registers_available_p(void)
99 int f
= dispatch_x86ish_xmmregisters_p();
101 dispatch_debug("XMM registers %savailable", f ?
"" : "not ");
105 /* --- @rdrand_works_p@ --- *
110 * Returns: Nonzero if the `rdrand' instruction actually works. Assumes
111 * that it's already been verified to be safe to issue.
114 static int rdrand_works_p(void)
118 /* Check that it doesn't always give the same answer. Try four times: this
119 * will fail with probability %$2^{-128}$% with a truly random generator,
120 * which seems fair enough.
122 if (dispatch_x86ish_rdrand(&ref
)) goto fail
;
123 for (i
= 0; i
< 4; i
++) {
124 if (dispatch_x86ish_rdrand(&x
)) goto fail
;
125 if (x
!= ref
) goto not_stuck
;
127 dispatch_debug("RDRAND always returns 0x%08x!", ref
);
131 dispatch_debug("RDRAND instruction looks plausible");
135 dispatch_debug("RDRAND instruction fails too often");
141 /*----- General feature probing using auxiliary vectors -------------------*/
143 /* Try to find the system's definitions for auxiliary vector entries. */
144 #ifdef HAVE_SYS_AUXV_H
145 # include <sys/auxv.h>
147 #ifdef HAVE_LINUX_AUXVEC_H
148 # include <linux/auxvec.h>
150 #ifdef HAVE_ASM_HWCAP_H
151 # include <asm/hwcap.h>
154 /* The type of entries in the auxiliary vector. I'm assuming that `unsigned
155 * long' matches each platform's word length; if this is false then we'll
156 * need some host-specific tweaking here.
158 union auxval
{ long i
; unsigned long u
; const void *p
; };
159 struct auxentry
{ unsigned long type
; union auxval value
; };
161 /* Register each CPU family's interest in the auxiliary vector. Make sure
162 * that the necessary entry types are defined. This is primarily ordered by
163 * entry type to minimize duplication.
165 #if defined(AT_HWCAP) && CPUFAM_ARMEL
167 # define WANT_AT_HWCAP(_) _(AT_HWCAP, u, hwcap)
170 #if defined(AT_HWCAP) && CPUFAM_ARM64
172 # define WANT_AT_HWCAP(_) _(AT_HWCAP, u, hwcap)
175 #if defined(AT_HWCAP2) && CPUFAM_ARMEL
177 # define WANT_AT_HWCAP2(_) _(AT_HWCAP2, u, hwcap2)
180 /* If we couldn't find any interesting entries then we can switch all of this
181 * machinery off. Also do that if we have no means for atomic updates.
183 #if WANT_ANY && CPU_DISPATCH_P
185 /* The main output of this section is a bitmask of detected features. The
186 * least significant bit will be set if we've tried to probe. Always access
187 * this using `DISPATCH_LOAD' and `DISPATCH_STORE'.
189 static unsigned hwcaps
= 0;
191 /* For each potentially interesting type which turned out not to exist or be
192 * wanted, define a dummy macro for the sake of the next step.
194 #ifndef WANT_AT_HWCAP
195 # define WANT_AT_HWCAP(_)
197 #ifndef WANT_AT_HWCAP2
198 # define WANT_AT_HWCAP2(_)
201 /* For each CPU family, define two lists.
203 * * `WANTAUX' is a list of the `WANT_AT_MUMBLE' macros which the CPU
204 * family tried to register interest in above. Each entry contains the
205 * interesting auxiliary vector entry type, the name of the union branch
206 * for its value, and the name of the slot in `struct auxprobe' in which
207 * to store the value.
209 * * `CAPMAP' is a list describing the output features which the CPU family
210 * intends to satisfy from the auxiliary vector. Each entry contains a
211 * feature name suffix, and the token name (for `check_env').
214 # define WANTAUX(_) \
218 _(ARM_VFP, "arm:vfp") \
219 _(ARM_NEON, "arm:neon") \
220 _(ARM_V4, "arm:v4") \
221 _(ARM_D32, "arm:d32") \
222 _(ARM_AES, "arm:aes") \
223 _(ARM_PMULL, "arm:pmull")
226 # define WANTAUX(_) \
229 _(ARM_NEON, "arm:neon") \
230 _(ARM_AES, "arm:aes") \
231 _(ARM_PMULL, "arm:pmull")
234 /* Build the bitmask for `hwcaps' from the `CAPMAP' list. */
237 #define HFI__ENUM(feat, tok) HFI_##feat,
244 #define HF__FLAG(feat, tok) HF_##feat = 1 << HFI_##feat,
250 /* Build a structure in which we can capture the interesting data from the
253 #define AUXUTYPE_i long
254 #define AUXUTYPE_u unsigned long
255 #define AUXUTYPE_p const void *
257 #define AUXPROBE__SLOT(type, ubranch, slot) AUXUTYPE_##ubranch slot;
258 WANTAUX(AUXPROBE__SLOT
)
262 /* --- @probe_hwcaps@ --- *
268 * Use: Attempt to find the auxiliary vector (which is well hidden)
269 * and discover interesting features from it.
272 static void probe_hwcaps(void)
274 unsigned hw
= HF_PROBED
;
275 struct auxprobe probed
= { 0 };
277 /* Populate `probed' with the information we manage to retrieve from the
278 * auxiliary vector. Slots we couldn't find are left zero-valued.
280 #if defined(HAVE_GETAUXVAL)
281 /* Shiny new libc lets us request individual entry types. This is almost
284 # define CAP__GET(type, ubranch, slot) \
285 probed.slot = (AUXUTYPE_##ubranch)getauxval(type);
288 /* Otherwise we're a bit stuck, really. Modern Linux kernels make a copy
289 * of the vector available in `/procc' so we could try that.
291 * The usual place is stuck on the end of the environment vector, but that
292 * may well have moved, and we have no way of telling whether it has or
293 * whether there was ever an auxiliary vector there at all; so don't do
298 unsigned char *p
= 0, *q
= 0;
299 const struct auxentry
*a
;
302 /* Open the file and read it into a memory chunk. */
303 if ((fp
= fopen("/proc/self/auxv", "rb")) == 0) goto clean
;
305 if ((p
= malloc(sz
)) == 0) goto clean
;
307 n
= fread(p
+ off
, 1, sz
- off
, fp
);
310 sz
*= 2; if ((q
= realloc(p
, sz
)) == 0) break;
314 /* Work through the vector (or as much of it as we found) and extract the
315 * types we're interested in.
317 for (a
= (const struct auxentry
*)p
,
318 n
= sz
/sizeof(struct auxentry
);
321 #define CAP__SWITCH(type, ubranch, slot) \
322 case type: probed.slot = a->value.ubranch; break;
324 case AT_NULL
: goto clean
;
334 /* Each CPU family now has to pick through what was found and stashed in
335 * `probed', and set the appropriate flag bits in `hw'.
338 if (probed
.hwcap
& HWCAP_VFPv3
) hw
|= HF_ARM_VFP
;
339 if (probed
.hwcap
& HWCAP_NEON
) hw
|= HF_ARM_NEON
;
340 if (probed
.hwcap
& HWCAP_VFPD32
) hw
|= HF_ARM_D32
;
341 if (probed
.hwcap
& HWCAP_VFPv4
) hw
|= HF_ARM_V4
;
343 if (probed
.hwcap2
& HWCAP2_AES
) hw
|= HF_ARM_AES
;
346 if (probed
.hwcap2
& HWCAP2_PMULL
) hw
|= HF_ARM_PMULL
;
350 if (probed
.hwcap
& HWCAP_ASIMD
) hw
|= HF_ARM_NEON
;
351 if (probed
.hwcap
& HWCAP_AES
) hw
|= HF_ARM_AES
;
352 if (probed
.hwcap
& HWCAP_PMULL
) hw
|= HF_ARM_PMULL
;
355 /* Store the bitmask of features we probed for everyone to see. */
356 DISPATCH_STORE(hwcaps
, hw
);
358 /* Finally, make a report about the things we found. (Doing this earlier
359 * will pointlessly widen the window in which multiple threads will do the
360 * above auxiliary-vector probing.)
362 #define CAP__DEBUG(feat, tok) \
363 dispatch_debug("check auxv for feature `%s': %s", tok, \
364 hw & HF_##feat ? "available" : "absent");
369 /* --- @get_hwcaps@ --- *
373 * Returns: A mask of hardware capabilities and other features, as probed
374 * from the auxiliary vector.
377 static unsigned get_hwcaps(void)
381 DISPATCH_LOAD(hwcaps
, hw
);
382 if (!(hwcaps
& HF_PROBED
)) { probe_hwcaps(); DISPATCH_LOAD(hwcaps
, hw
); }
388 /*----- External interface ------------------------------------------------*/
390 /* --- @dispatch_debug@ --- *
392 * Arguments: @const char *fmt@ = a format string
393 * @...@ = additional arguments
397 * Use: Writes a formatted message to standard output if dispatch
398 * debugging is enabled.
401 void dispatch_debug(const char *fmt
, ...)
404 const char *e
= getenv("CATACOMB_CPUDISPATCH_DEBUG");
406 if (e
&& *e
!= 'n' && *e
!= '0') {
408 fputs("Catacomb CPUDISPATCH: ", stderr
);
409 vfprintf(stderr
, fmt
, ap
);
415 /* --- @check_env@ --- *
417 * Arguments: @const char *ftok@ = feature token
419 * Returns: Zero if the feature is forced off; positive if it's forced
420 * on; negative if the user hasn't decided.
422 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
423 * feature token @ftok@. The variable, if it exists, should be
424 * a space-separated sequence of `+tok' and `-tok' items. These
425 * tokens may end in `*', which matches any suffix.
428 static int IGNORABLE
check_env(const char *ftok
)
430 const char *p
, *q
, *pp
;
433 p
= getenv("CATACOMB_CPUFEAT");
437 while (ISSPACE(*p
)) p
++;
438 if (!*p
) return (-1);
440 case '+': d
= +1; p
++; break;
441 case '-': d
= 0; p
++; break;
442 default: d
= -1; break;
444 for (q
= p
; *q
&& !ISSPACE(*q
); q
++);
446 for (pp
= ftok
; p
< q
&& *pp
&& *p
== *pp
; p
++, pp
++);
447 if ((p
== q
&& !*pp
) || (*p
== '*' && p
+ 1 == q
)) return (d
);
454 /* --- @cpu_feature_p@ --- *
456 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
458 * Returns: Nonzero if the feature is available.
464 feat_debug(const char *ftok
, const char *check
, int verdict
)
467 dispatch_debug("feature `%s': %s -> %s", ftok
, check
,
468 verdict ?
"available" : "absent");
473 int cpu_feature_p(int feat
)
477 #define CASE_CPUFEAT(feat, ftok, cond) case CPUFEAT_##feat: \
478 if ((f = feat_debug(ftok, "environment override", \
479 check_env(ftok))) >= 0) \
482 return (feat_debug(ftok, "runtime probe", cond));
485 #if CPUFAM_X86 || CPUFAM_AMD64
486 CASE_CPUFEAT(X86_SSE2
, "x86:sse2",
487 cpuid_features_p(CPUID1D_SSE2
, 0) &&
488 xmm_registers_available_p());
489 CASE_CPUFEAT(X86_AESNI
, "x86:aesni",
490 cpuid_features_p(CPUID1D_SSE2
, CPUID1C_AESNI
) &&
491 xmm_registers_available_p());
492 CASE_CPUFEAT(X86_RDRAND
, "x86:rdrand",
493 cpuid_features_p(0, CPUID1C_RDRAND
) && rdrand_works_p());
494 CASE_CPUFEAT(X86_AVX
, "x86:avx",
495 cpuid_features_p(0, CPUID1C_AVX
) &&
496 xmm_registers_available_p());
497 CASE_CPUFEAT(X86_SSSE3
, "x86:ssse3",
498 cpuid_features_p(0, CPUID1C_SSSE3
) &&
499 xmm_registers_available_p());
500 CASE_CPUFEAT(X86_PCLMUL
, "x86:pclmul",
501 cpuid_features_p(0, CPUID1C_PCLMUL
) &&
502 xmm_registers_available_p());
505 # define FEATP__CASE(feat, tok) \
506 CASE_CPUFEAT(feat, tok, get_hwcaps() & HF_##feat)
511 dispatch_debug("denying unknown feature %d", feat
);
517 /*----- That's all, folks -------------------------------------------------*/