7652f323b200c6439efdc6c8bf8b7c6d314a0d73
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 ------------------------------------------------------*/
39 #include <mLib/macros.h>
43 /*----- Intel x86/AMD64 feature probing -----------------------------------*/
45 #if CPUFAM_X86 || CPUFAM_AMD64
48 CPUID_1_D
, /* eax = 1 => edx&?? */
49 # define CPUID1D_SSE2 (1u << 26)
50 # define CPUID1D_FXSR (1u << 24)
52 CPUID_1_C
, /* eax = 1 => ecx&?? */
53 # define CPUID1C_PCLMUL (1u << 1)
54 # define CPUID1C_SSSE3 (1u << 9)
55 # define CPUID1C_AESNI (1u << 25)
56 # define CPUID1C_AVX (1u << 28)
57 # define CPUID1C_RDRAND (1u << 30)
61 struct cpuid
{ unsigned a
, b
, c
, d
; };
62 extern int dispatch_x86ish_cpuid(struct cpuid
*, unsigned a
, unsigned c
);
63 extern int dispatch_x86ish_xmmregisters_p(void);
64 extern int dispatch_x86ish_rdrand(unsigned *);
66 static void cpuid(struct cpuid
*cc
, unsigned a
, unsigned c
)
68 int rc
= dispatch_x86ish_cpuid(cc
, a
, c
);
70 dispatch_debug("CPUID instruction not available");
72 dispatch_debug("CPUID(%08x, %08x) -> %08x, %08x, %08x, %08x",
73 a
, c
, cc
->a
, cc
->b
, cc
->c
, cc
->d
);
76 static unsigned cpuid_maxleaf(void)
77 { struct cpuid c
; cpuid(&c
, 0, 0); return (c
.a
); }
79 /* --- @cpuid_feature_p@ --- *
81 * Arguments: @unsigned leaf@ = leaf to look up
82 * @unsigned bits@ = bits to check
84 * Returns: Nonzero if all the requested bits are set in the requested
88 static int cpuid_feature_p(unsigned leaf
, unsigned bits
)
95 if (cpuid_maxleaf() < 1) return (0);
96 cpuid(&c
, 1, 0); r
= c
.d
;
99 if (cpuid_maxleaf() < 1) return (0);
100 cpuid(&c
, 1, 0); r
= c
.c
;
103 assert(!"unknown cpuid leaf");
105 return ((r
&bits
) == bits
);
108 /* --- @xmm_registers_available_p@ --- *
112 * Returns: Nonzero if the operating system has made the XMM registers
116 static int xmm_registers_available_p(void)
118 int f
= dispatch_x86ish_xmmregisters_p();
120 dispatch_debug("XMM registers %savailable", f ?
"" : "not ");
124 /* --- @rdrand_works_p@ --- *
129 * Returns: Nonzero if the `rdrand' instruction actually works. Assumes
130 * that it's already been verified to be safe to issue.
133 static int rdrand_works_p(void)
137 /* Check that it doesn't always give the same answer. Try four times: this
138 * will fail with probability %$2^{-128}$% with a truly random generator,
139 * which seems fair enough.
141 if (dispatch_x86ish_rdrand(&ref
)) goto fail
;
142 for (i
= 0; i
< 4; i
++) {
143 if (dispatch_x86ish_rdrand(&x
)) goto fail
;
144 if (x
!= ref
) goto not_stuck
;
146 dispatch_debug("RDRAND always returns 0x%08x!", ref
);
150 dispatch_debug("RDRAND instruction looks plausible");
154 dispatch_debug("RDRAND instruction fails too often");
160 /*----- General feature probing using auxiliary vectors -------------------*/
162 /* Try to find the system's definitions for auxiliary vector entries. */
163 #ifdef HAVE_SYS_AUXV_H
164 # include <sys/auxv.h>
166 #ifdef HAVE_LINUX_AUXVEC_H
167 # include <linux/auxvec.h>
169 #ifdef HAVE_ASM_HWCAP_H
170 # include <asm/hwcap.h>
173 /* The type of entries in the auxiliary vector. I'm assuming that `unsigned
174 * long' matches each platform's word length; if this is false then we'll
175 * need some host-specific tweaking here.
177 union auxval
{ long i
; unsigned long u
; const void *p
; };
178 struct auxentry
{ unsigned long type
; union auxval value
; };
180 /* Register each CPU family's interest in the auxiliary vector. Make sure
181 * that the necessary entry types are defined. This is primarily ordered by
182 * entry type to minimize duplication.
184 #if defined(AT_HWCAP) && CPUFAM_ARMEL
186 # define WANT_AT_HWCAP(_) _(AT_HWCAP, u, hwcap)
189 #if defined(AT_HWCAP) && CPUFAM_ARM64
191 # define WANT_AT_HWCAP(_) _(AT_HWCAP, u, hwcap)
194 #if defined(AT_HWCAP2) && CPUFAM_ARMEL
196 # define WANT_AT_HWCAP2(_) _(AT_HWCAP2, u, hwcap2)
199 /* If we couldn't find any interesting entries then we can switch all of this
200 * machinery off. Also do that if we have no means for atomic updates.
202 #if WANT_ANY && CPU_DISPATCH_P
204 /* The main output of this section is a bitmask of detected features. The
205 * least significant bit will be set if we've tried to probe. Always access
206 * this using `DISPATCH_LOAD' and `DISPATCH_STORE'.
208 static unsigned hwcaps
= 0;
210 /* For each potentially interesting type which turned out not to exist or be
211 * wanted, define a dummy macro for the sake of the next step.
213 #ifndef WANT_AT_HWCAP
214 # define WANT_AT_HWCAP(_)
216 #ifndef WANT_AT_HWCAP2
217 # define WANT_AT_HWCAP2(_)
220 /* For each CPU family, define two lists.
222 * * `WANTAUX' is a list of the `WANT_AT_MUMBLE' macros which the CPU
223 * family tried to register interest in above. Each entry contains the
224 * interesting auxiliary vector entry type, the name of the union branch
225 * for its value, and the name of the slot in `struct auxprobe' in which
226 * to store the value.
228 * * `CAPMAP' is a list describing the output features which the CPU family
229 * intends to satisfy from the auxiliary vector. Each entry contains a
230 * feature name suffix, and the token name (for `check_env').
233 # define WANTAUX(_) \
237 _(ARM_VFP, "arm:vfp") \
238 _(ARM_NEON, "arm:neon") \
239 _(ARM_V4, "arm:v4") \
240 _(ARM_D32, "arm:d32") \
241 _(ARM_AES, "arm:aes") \
242 _(ARM_PMULL, "arm:pmull")
245 # define WANTAUX(_) \
248 _(ARM_NEON, "arm:neon") \
249 _(ARM_AES, "arm:aes") \
250 _(ARM_PMULL, "arm:pmull")
253 /* Build the bitmask for `hwcaps' from the `CAPMAP' list. */
256 #define HFI__ENUM(feat, tok) HFI_##feat,
263 #define HF__FLAG(feat, tok) HF_##feat = 1 << HFI_##feat,
269 /* Build a structure in which we can capture the interesting data from the
272 #define AUXUTYPE_i long
273 #define AUXUTYPE_u unsigned long
274 #define AUXUTYPE_p const void *
276 #define AUXPROBE__SLOT(type, ubranch, slot) AUXUTYPE_##ubranch slot;
277 WANTAUX(AUXPROBE__SLOT
)
281 /* --- @probe_hwcaps@ --- *
287 * Use: Attempt to find the auxiliary vector (which is well hidden)
288 * and discover interesting features from it.
291 static void probe_hwcaps(void)
293 unsigned hw
= HF_PROBED
;
294 struct auxprobe probed
= { 0 };
296 /* Populate `probed' with the information we manage to retrieve from the
297 * auxiliary vector. Slots we couldn't find are left zero-valued.
299 #if defined(HAVE_GETAUXVAL)
300 /* Shiny new libc lets us request individual entry types. This is almost
303 # define CAP__GET(type, ubranch, slot) \
304 probed.slot = (AUXUTYPE_##ubranch)getauxval(type);
307 /* Otherwise we're a bit stuck, really. Modern Linux kernels make a copy
308 * of the vector available in `/procc' so we could try that.
310 * The usual place is stuck on the end of the environment vector, but that
311 * may well have moved, and we have no way of telling whether it has or
312 * whether there was ever an auxiliary vector there at all; so don't do
317 unsigned char *p
= 0, *q
= 0;
318 const struct auxentry
*a
;
321 /* Open the file and read it into a memory chunk. */
322 if ((fp
= fopen("/proc/self/auxv", "rb")) == 0) goto clean
;
324 if ((p
= malloc(sz
)) == 0) goto clean
;
326 n
= fread(p
+ off
, 1, sz
- off
, fp
);
329 sz
*= 2; if ((q
= realloc(p
, sz
)) == 0) break;
333 /* Work through the vector (or as much of it as we found) and extract the
334 * types we're interested in.
336 for (a
= (const struct auxentry
*)p
,
337 n
= sz
/sizeof(struct auxentry
);
340 #define CAP__SWITCH(type, ubranch, slot) \
341 case type: probed.slot = a->value.ubranch; break;
343 case AT_NULL
: goto clean
;
353 /* Each CPU family now has to pick through what was found and stashed in
354 * `probed', and set the appropriate flag bits in `hw'.
357 if (probed
.hwcap
& HWCAP_VFPv3
) hw
|= HF_ARM_VFP
;
358 if (probed
.hwcap
& HWCAP_NEON
) hw
|= HF_ARM_NEON
;
359 if (probed
.hwcap
& HWCAP_VFPD32
) hw
|= HF_ARM_D32
;
360 if (probed
.hwcap
& HWCAP_VFPv4
) hw
|= HF_ARM_V4
;
362 if (probed
.hwcap2
& HWCAP2_AES
) hw
|= HF_ARM_AES
;
365 if (probed
.hwcap2
& HWCAP2_PMULL
) hw
|= HF_ARM_PMULL
;
369 if (probed
.hwcap
& HWCAP_ASIMD
) hw
|= HF_ARM_NEON
;
370 if (probed
.hwcap
& HWCAP_AES
) hw
|= HF_ARM_AES
;
371 if (probed
.hwcap
& HWCAP_PMULL
) hw
|= HF_ARM_PMULL
;
374 /* Store the bitmask of features we probed for everyone to see. */
375 DISPATCH_STORE(hwcaps
, hw
);
377 /* Finally, make a report about the things we found. (Doing this earlier
378 * will pointlessly widen the window in which multiple threads will do the
379 * above auxiliary-vector probing.)
381 #define CAP__DEBUG(feat, tok) \
382 dispatch_debug("check auxv for feature `%s': %s", tok, \
383 hw & HF_##feat ? "available" : "absent");
388 /* --- @get_hwcaps@ --- *
392 * Returns: A mask of hardware capabilities and other features, as probed
393 * from the auxiliary vector.
396 static unsigned get_hwcaps(void)
400 DISPATCH_LOAD(hwcaps
, hw
);
401 if (!(hwcaps
& HF_PROBED
)) { probe_hwcaps(); DISPATCH_LOAD(hwcaps
, hw
); }
407 /*----- External interface ------------------------------------------------*/
409 /* --- @dispatch_debug@ --- *
411 * Arguments: @const char *fmt@ = a format string
412 * @...@ = additional arguments
416 * Use: Writes a formatted message to standard output if dispatch
417 * debugging is enabled.
420 void dispatch_debug(const char *fmt
, ...)
423 const char *e
= getenv("CATACOMB_CPUDISPATCH_DEBUG");
425 if (e
&& *e
!= 'n' && *e
!= '0') {
427 fputs("Catacomb CPUDISPATCH: ", stderr
);
428 vfprintf(stderr
, fmt
, ap
);
434 /* --- @check_env@ --- *
436 * Arguments: @const char *ftok@ = feature token
438 * Returns: Zero if the feature is forced off; positive if it's forced
439 * on; negative if the user hasn't decided.
441 * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the
442 * feature token @ftok@. The variable, if it exists, should be
443 * a space-separated sequence of `+tok' and `-tok' items. These
444 * tokens may end in `*', which matches any suffix.
447 static int IGNORABLE
check_env(const char *ftok
)
449 const char *p
, *q
, *pp
;
452 p
= getenv("CATACOMB_CPUFEAT");
456 while (ISSPACE(*p
)) p
++;
457 if (!*p
) return (-1);
459 case '+': d
= +1; p
++; break;
460 case '-': d
= 0; p
++; break;
461 default: d
= -1; break;
463 for (q
= p
; *q
&& !ISSPACE(*q
); q
++);
465 for (pp
= ftok
; p
< q
&& *pp
&& *p
== *pp
; p
++, pp
++);
466 if ((p
== q
&& !*pp
) || (*p
== '*' && p
+ 1 == q
)) return (d
);
473 /* --- @cpu_feature_p@ --- *
475 * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code
477 * Returns: Nonzero if the feature is available.
483 feat_debug(const char *ftok
, const char *check
, int verdict
)
486 dispatch_debug("feature `%s': %s -> %s", ftok
, check
,
487 verdict ?
"available" : "absent");
492 int cpu_feature_p(int feat
)
496 #define CASE_CPUFEAT(feat, ftok, cond) case CPUFEAT_##feat: \
497 if ((f = feat_debug(ftok, "environment override", check_env(ftok))) >= 0) \
500 return (feat_debug(ftok, "runtime probe", cond));
503 #if CPUFAM_X86 || CPUFAM_AMD64
504 CASE_CPUFEAT(X86_SSE2
, "x86:sse2",
505 cpuid_feature_p(CPUID_1_D
, CPUID1D_SSE2
) &&
506 xmm_registers_available_p());
507 CASE_CPUFEAT(X86_AESNI
, "x86:aesni",
508 cpuid_feature_p(CPUID_1_D
, CPUID1C_AESNI
) &&
509 xmm_registers_available_p());
510 CASE_CPUFEAT(X86_RDRAND
, "x86:rdrand",
511 cpuid_feature_p(CPUID_1_C
, CPUID1C_RDRAND
) &&
513 CASE_CPUFEAT(X86_AVX
, "x86:avx",
514 cpuid_feature_p(CPUID_1_C
, CPUID1C_AVX
) &&
515 xmm_registers_available_p());
516 CASE_CPUFEAT(X86_SSSE3
, "x86:ssse3",
517 cpuid_feature_p(CPUID_1_C
, CPUID1C_SSSE3
) &&
518 xmm_registers_available_p());
519 CASE_CPUFEAT(X86_PCLMUL
, "x86:pclmul",
520 cpuid_feature_p(CPUID_1_C
, CPUID1C_PCLMUL
) &&
521 xmm_registers_available_p());
524 # define FEATP__CASE(feat, tok) \
525 CASE_CPUFEAT(feat, tok, get_hwcaps() & HF_##feat)
530 dispatch_debug("denying unknown feature %d", feat
);
536 /*----- That's all, folks -------------------------------------------------*/