You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

200 lines
5.9 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/cpu.h>
  15. #if (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) && \
  16. !defined(OPENSSL_STATIC_ARMCAP)
  17. #include <inttypes.h>
  18. #include <string.h>
  19. #include <setjmp.h>
  20. #include <signal.h>
  21. #include <openssl/arm_arch.h>
  22. /* We can't include <sys/auxv.h> because the Android SDK version against which
  23. * Chromium builds is too old to have it. Instead we define all the constants
  24. * that we need and have a weak pointer to getauxval. */
  25. unsigned long getauxval(unsigned long type) __attribute__((weak));
  26. extern uint32_t OPENSSL_armcap_P;
  27. char CRYPTO_is_NEON_capable_at_runtime(void) {
  28. return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
  29. }
  30. static char g_set_neon_called = 0;
  31. void CRYPTO_set_NEON_capable(char neon_capable) {
  32. g_set_neon_called = 1;
  33. if (neon_capable) {
  34. OPENSSL_armcap_P |= ARMV7_NEON;
  35. } else {
  36. OPENSSL_armcap_P &= ~ARMV7_NEON;
  37. }
  38. }
  39. char CRYPTO_is_NEON_functional(void) {
  40. static const uint32_t kWantFlags = ARMV7_NEON | ARMV7_NEON_FUNCTIONAL;
  41. return (OPENSSL_armcap_P & kWantFlags) == kWantFlags;
  42. }
  43. void CRYPTO_set_NEON_functional(char neon_functional) {
  44. if (neon_functional) {
  45. OPENSSL_armcap_P |= ARMV7_NEON_FUNCTIONAL;
  46. } else {
  47. OPENSSL_armcap_P &= ~ARMV7_NEON_FUNCTIONAL;
  48. }
  49. }
  50. int CRYPTO_is_ARMv8_AES_capable(void) {
  51. return (OPENSSL_armcap_P & ARMV8_AES) != 0;
  52. }
  53. int CRYPTO_is_ARMv8_PMULL_capable(void) {
  54. return (OPENSSL_armcap_P & ARMV8_PMULL) != 0;
  55. }
  56. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM)
  57. static sigjmp_buf sigill_jmp;
  58. static void sigill_handler(int signal) {
  59. siglongjmp(sigill_jmp, signal);
  60. }
  61. void CRYPTO_arm_neon_probe(void);
  62. // probe_for_NEON returns 1 if a NEON instruction runs successfully. Because
  63. // getauxval doesn't exist on Android until Jelly Bean, supporting NEON on
  64. // older devices requires this.
  65. static int probe_for_NEON(void) {
  66. int supported = 0;
  67. sigset_t sigmask;
  68. sigfillset(&sigmask);
  69. sigdelset(&sigmask, SIGILL);
  70. sigdelset(&sigmask, SIGTRAP);
  71. sigdelset(&sigmask, SIGFPE);
  72. sigdelset(&sigmask, SIGBUS);
  73. sigdelset(&sigmask, SIGSEGV);
  74. struct sigaction sigill_original_action, sigill_action;
  75. memset(&sigill_action, 0, sizeof(sigill_action));
  76. sigill_action.sa_handler = sigill_handler;
  77. sigill_action.sa_mask = sigmask;
  78. sigset_t original_sigmask;
  79. sigprocmask(SIG_SETMASK, &sigmask, &original_sigmask);
  80. if (sigsetjmp(sigill_jmp, 1 /* save signals */) == 0) {
  81. sigaction(SIGILL, &sigill_action, &sigill_original_action);
  82. // This function cannot be inline asm because GCC will refuse to compile
  83. // inline NEON instructions unless building with -mfpu=neon, which would
  84. // defeat the point of probing for support at runtime.
  85. CRYPTO_arm_neon_probe();
  86. supported = 1;
  87. }
  88. // Note that Android up to and including Lollipop doesn't restore the signal
  89. // mask correctly after returning from a sigsetjmp. So that would need to be
  90. // set again here if more probes were added.
  91. // See https://android-review.googlesource.com/#/c/127624/
  92. sigaction(SIGILL, &sigill_original_action, NULL);
  93. sigprocmask(SIG_SETMASK, &original_sigmask, NULL);
  94. return supported;
  95. }
  96. #else
  97. static int probe_for_NEON(void) {
  98. return 0;
  99. }
  100. #endif /* !OPENSSL_NO_ASM && OPENSSL_ARM */
  101. void OPENSSL_cpuid_setup(void) {
  102. if (getauxval == NULL) {
  103. // On ARM, but not AArch64, try a NEON instruction and see whether it works
  104. // in order to probe for NEON support.
  105. //
  106. // Note that |CRYPTO_is_NEON_capable| can be true even if
  107. // |CRYPTO_set_NEON_capable| has never been called if the code was compiled
  108. // with NEON support enabled (e.g. -mfpu=neon).
  109. if (!g_set_neon_called && !CRYPTO_is_NEON_capable() && probe_for_NEON()) {
  110. OPENSSL_armcap_P |= ARMV7_NEON;
  111. }
  112. return;
  113. }
  114. static const unsigned long AT_HWCAP = 16;
  115. unsigned long hwcap = getauxval(AT_HWCAP);
  116. #if defined(OPENSSL_ARM)
  117. static const unsigned long kNEON = 1 << 12;
  118. if ((hwcap & kNEON) == 0) {
  119. return;
  120. }
  121. /* In 32-bit mode, the ARMv8 feature bits are in a different aux vector
  122. * value. */
  123. static const unsigned long AT_HWCAP2 = 26;
  124. hwcap = getauxval(AT_HWCAP2);
  125. /* See /usr/include/asm/hwcap.h on an ARM installation for the source of
  126. * these values. */
  127. static const unsigned long kAES = 1 << 0;
  128. static const unsigned long kPMULL = 1 << 1;
  129. static const unsigned long kSHA1 = 1 << 2;
  130. static const unsigned long kSHA256 = 1 << 3;
  131. #elif defined(OPENSSL_AARCH64)
  132. /* See /usr/include/asm/hwcap.h on an aarch64 installation for the source of
  133. * these values. */
  134. static const unsigned long kNEON = 1 << 1;
  135. static const unsigned long kAES = 1 << 3;
  136. static const unsigned long kPMULL = 1 << 4;
  137. static const unsigned long kSHA1 = 1 << 5;
  138. static const unsigned long kSHA256 = 1 << 6;
  139. if ((hwcap & kNEON) == 0) {
  140. return;
  141. }
  142. #endif
  143. OPENSSL_armcap_P |= ARMV7_NEON;
  144. if (hwcap & kAES) {
  145. OPENSSL_armcap_P |= ARMV8_AES;
  146. }
  147. if (hwcap & kPMULL) {
  148. OPENSSL_armcap_P |= ARMV8_PMULL;
  149. }
  150. if (hwcap & kSHA1) {
  151. OPENSSL_armcap_P |= ARMV8_SHA1;
  152. }
  153. if (hwcap & kSHA256) {
  154. OPENSSL_armcap_P |= ARMV8_SHA256;
  155. }
  156. }
  157. #endif /* (defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)) &&
  158. !defined(OPENSSL_STATIC_ARMCAP) */