diff options
author | David Benjamin <davidben@google.com> | 2016-03-02 01:35:47 +0300 |
---|---|---|
committer | David Benjamin <davidben@google.com> | 2016-03-26 07:54:44 +0300 |
commit | 054e151b16be6ada891ee8fd71915088dda30886 (patch) | |
tree | aac89f08e31fb55354b8c31aabc5051a96db65f9 /crypto/cpu-arm.c | |
parent | dc6c1b83819cb3788c60dd669241adc6752a4604 (diff) |
Rewrite ARM feature detection.
This removes the thread-unsafe SIGILL-based detection and the
multi-consumer-hostile CRYPTO_set_NEON_capable API. (Changing
OPENSSL_armcap_P after initialization is likely to cause problems.)
The right way to detect ARM features on Linux is getauxval. On aarch64,
we should be able to rely on this, so use it straight. Split this out
into its own file. The #ifdefs in the old cpu-arm.c meant it shared all
but no code with its arm counterpart anyway.
Unfortunately, various versions of Android have different missing APIs, so, on
arm, we need a series of workarounds. Previously, we used a SIGILL fallback
based on OpenSSL's logic, but this is inherently not thread-safe. (SIGILL also
does not tell us if the OS knows how to save and restore NEON state.) Instead,
base the behavior on Android NDK's cpu-features library, what Chromium
currently uses with CRYPTO_set_NEON_capable:
- Android before API level 20 does not provide getauxval. Where missing,
we can read from /proc/self/auxv.
- On some versions of Android, /proc/self/auxv is also not readable, so
use /proc/cpuinfo's Features line.
- Linux only advertises optional features in /proc/cpuinfo. ARMv8 makes NEON
mandatory, so /proc/cpuinfo can't be used without additional effort.
Finally, we must blacklist a particular chip because the NEON unit is broken
(https://crbug.com/341598).
Unfortunately, this means CRYPTO_library_init now depends on /proc being
available, which will require some care with Chromium's sandbox. The
simplest solution is to just call CRYPTO_library_init before entering
the sandbox.
It's worth noting that Chromium's current EnsureOpenSSLInit function already
depends on /proc/cpuinfo to detect the broken CPU, by way of base::CPU.
android_getCpuFeatures also interally depends on it. We were already relying on
both of those being stateful and primed prior to entering the sandbox.
BUG=chromium:589200
Change-Id: Ic5d1c341aab5a614eb129d8aa5ada2809edd6af8
Reviewed-on: https://boringssl-review.googlesource.com/7506
Reviewed-by: David Benjamin <davidben@google.com>
Diffstat (limited to 'crypto/cpu-arm.c')
-rw-r--r-- | crypto/cpu-arm.c | 150 |
1 files changed, 0 insertions, 150 deletions
diff --git a/crypto/cpu-arm.c b/crypto/cpu-arm.c index 20bf0ff7..ef395eae 100644 --- a/crypto/cpu-arm.c +++ b/crypto/cpu-arm.c @@ -17,22 +17,8 @@ #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \ !defined(OPENSSL_STATIC_ARMCAP) -#include <inttypes.h> -#include <string.h> - -#include <setjmp.h> -#include <signal.h> - #include <openssl/arm_arch.h> -#include "internal.h" - - -/* We can't include <sys/auxv.h> because the Android SDK version against which - * Chromium builds is too old to have it. Instead we define all the constants - * that we need and have a weak pointer to getauxval. */ - -unsigned long getauxval(unsigned long type) __attribute__((weak)); extern uint32_t OPENSSL_armcap_P; @@ -40,18 +26,6 @@ char CRYPTO_is_NEON_capable_at_runtime(void) { return (OPENSSL_armcap_P & ARMV7_NEON) != 0; } -static char g_set_neon_called = 0; - -void CRYPTO_set_NEON_capable(char neon_capable) { - g_set_neon_called = 1; - - if (neon_capable) { - OPENSSL_armcap_P |= ARMV7_NEON; - } else { - OPENSSL_armcap_P &= ~ARMV7_NEON; - } -} - int CRYPTO_is_ARMv8_AES_capable(void) { return (OPENSSL_armcap_P & ARMV8_AES) != 0; } @@ -60,129 +34,5 @@ int CRYPTO_is_ARMv8_PMULL_capable(void) { return (OPENSSL_armcap_P & ARMV8_PMULL) != 0; } -#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) - -static sigjmp_buf sigill_jmp; - -static void sigill_handler(int signal) { - siglongjmp(sigill_jmp, signal); -} - -void CRYPTO_arm_neon_probe(void); - -// probe_for_NEON returns 1 if a NEON instruction runs successfully. Because -// getauxval doesn't exist on Android until Jelly Bean, supporting NEON on -// older devices requires this. -static int probe_for_NEON(void) { - int supported = 0; - - sigset_t sigmask; - sigfillset(&sigmask); - sigdelset(&sigmask, SIGILL); - sigdelset(&sigmask, SIGTRAP); - sigdelset(&sigmask, SIGFPE); - sigdelset(&sigmask, SIGBUS); - sigdelset(&sigmask, SIGSEGV); - - struct sigaction sigill_original_action, sigill_action; - memset(&sigill_action, 0, sizeof(sigill_action)); - sigill_action.sa_handler = sigill_handler; - sigill_action.sa_mask = sigmask; - - sigset_t original_sigmask; - sigprocmask(SIG_SETMASK, &sigmask, &original_sigmask); - - if (sigsetjmp(sigill_jmp, 1 /* save signals */) == 0) { - sigaction(SIGILL, &sigill_action, &sigill_original_action); - - // This function cannot be inline asm because GCC will refuse to compile - // inline NEON instructions unless building with -mfpu=neon, which would - // defeat the point of probing for support at runtime. - CRYPTO_arm_neon_probe(); - supported = 1; - } - // Note that Android up to and including Lollipop doesn't restore the signal - // mask correctly after returning from a sigsetjmp. So that would need to be - // set again here if more probes were added. - // See https://android-review.googlesource.com/#/c/127624/ - - sigaction(SIGILL, &sigill_original_action, NULL); - sigprocmask(SIG_SETMASK, &original_sigmask, NULL); - - return supported; -} - -#else - -static int probe_for_NEON(void) { - return 0; -} - -#endif /* !OPENSSL_NO_ASM && OPENSSL_ARM */ - -void OPENSSL_cpuid_setup(void) { - if (getauxval == NULL) { - // On ARM, but not AArch64, try a NEON instruction and see whether it works - // in order to probe for NEON support. - // - // Note that |CRYPTO_is_NEON_capable| can be true even if - // |CRYPTO_set_NEON_capable| has never been called if the code was compiled - // with NEON support enabled (e.g. -mfpu=neon). - if (!g_set_neon_called && !CRYPTO_is_NEON_capable() && probe_for_NEON()) { - OPENSSL_armcap_P |= ARMV7_NEON; - } - return; - } - - static const unsigned long AT_HWCAP = 16; - unsigned long hwcap = getauxval(AT_HWCAP); - -#if defined(OPENSSL_ARM) - static const unsigned long kNEON = 1 << 12; - if ((hwcap & kNEON) == 0) { - return; - } - - /* In 32-bit mode, the ARMv8 feature bits are in a different aux vector - * value. */ - static const unsigned long AT_HWCAP2 = 26; - hwcap = getauxval(AT_HWCAP2); - - /* See /usr/include/asm/hwcap.h on an ARM installation for the source of - * these values. */ - static const unsigned long kAES = 1 << 0; - static const unsigned long kPMULL = 1 << 1; - static const unsigned long kSHA1 = 1 << 2; - static const unsigned long kSHA256 = 1 << 3; -#elif defined(OPENSSL_AARCH64) - /* See /usr/include/asm/hwcap.h on an aarch64 installation for the source of - * these values. */ - static const unsigned long kNEON = 1 << 1; - static const unsigned long kAES = 1 << 3; - static const unsigned long kPMULL = 1 << 4; - static const unsigned long kSHA1 = 1 << 5; - static const unsigned long kSHA256 = 1 << 6; - - if ((hwcap & kNEON) == 0) { - return; - } -#endif - - OPENSSL_armcap_P |= ARMV7_NEON; - - if (hwcap & kAES) { - OPENSSL_armcap_P |= ARMV8_AES; - } - if (hwcap & kPMULL) { - OPENSSL_armcap_P |= ARMV8_PMULL; - } - if (hwcap & kSHA1) { - OPENSSL_armcap_P |= ARMV8_SHA1; - } - if (hwcap & kSHA256) { - OPENSSL_armcap_P |= ARMV8_SHA256; - } -} - #endif /* (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && !defined(OPENSSL_STATIC_ARMCAP) */ |