No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

cpu-arm.c 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include <inttypes.h>
  17. #include <string.h>
  18. #if !defined(OPENSSL_TRUSTY)
  19. #include <setjmp.h>
  20. #include <signal.h>
  21. #endif
  22. #include "arm_arch.h"
  23. /* We can't include <sys/auxv.h> because the Android SDK version against which
  24. * Chromium builds is too old to have it. Instead we define all the constants
  25. * that we need and have a weak pointer to getauxval. */
  26. unsigned long getauxval(unsigned long type) __attribute__((weak));
  27. char CRYPTO_is_NEON_capable(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. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) && !defined(OPENSSL_TRUSTY)
  51. static sigjmp_buf sigill_jmp;
  52. static void sigill_handler(int signal) {
  53. siglongjmp(sigill_jmp, signal);
  54. }
  55. void CRYPTO_arm_neon_probe();
  56. // probe_for_NEON returns 1 if a NEON instruction runs successfully. Because
  57. // getauxval doesn't exist on Android until Jelly Bean, supporting NEON on
  58. // older devices requires this.
  59. static int probe_for_NEON() {
  60. int supported = 0;
  61. sigset_t sigmask;
  62. sigfillset(&sigmask);
  63. sigdelset(&sigmask, SIGILL);
  64. sigdelset(&sigmask, SIGTRAP);
  65. sigdelset(&sigmask, SIGFPE);
  66. sigdelset(&sigmask, SIGBUS);
  67. sigdelset(&sigmask, SIGSEGV);
  68. struct sigaction sigill_original_action, sigill_action;
  69. memset(&sigill_action, 0, sizeof(sigill_action));
  70. sigill_action.sa_handler = sigill_handler;
  71. sigill_action.sa_mask = sigmask;
  72. sigset_t original_sigmask;
  73. sigprocmask(SIG_SETMASK, &sigmask, &original_sigmask);
  74. if (sigsetjmp(sigill_jmp, 1 /* save signals */) == 0) {
  75. sigaction(SIGILL, &sigill_action, &sigill_original_action);
  76. // This function cannot be inline asm because GCC will refuse to compile
  77. // inline NEON instructions unless building with -mfpu=neon, which would
  78. // defeat the point of probing for support at runtime.
  79. CRYPTO_arm_neon_probe();
  80. supported = 1;
  81. }
  82. // Note that Android up to and including Lollipop doesn't restore the signal
  83. // mask correctly after returning from a sigsetjmp. So that would need to be
  84. // set again here if more probes were added.
  85. // See https://android-review.googlesource.com/#/c/127624/
  86. sigaction(SIGILL, &sigill_original_action, NULL);
  87. sigprocmask(SIG_SETMASK, &original_sigmask, NULL);
  88. return supported;
  89. }
  90. #else
  91. static int probe_for_NEON(void) {
  92. return 0;
  93. }
  94. #endif /* !OPENSSL_NO_ASM && OPENSSL_ARM && !OPENSSL_TRUSTY */
  95. void OPENSSL_cpuid_setup(void) {
  96. if (getauxval == NULL) {
  97. // On ARM, but not AArch64, try a NEON instruction and see whether it works
  98. // in order to probe for NEON support.
  99. //
  100. // Note that |CRYPTO_is_NEON_capable| can be true even if
  101. // |CRYPTO_set_NEON_capable| has never been called if the code was compiled
  102. // with NEON support enabled (e.g. -mfpu=neon).
  103. if (!g_set_neon_called && !CRYPTO_is_NEON_capable() && probe_for_NEON()) {
  104. OPENSSL_armcap_P |= ARMV7_NEON;
  105. }
  106. return;
  107. }
  108. static const unsigned long AT_HWCAP = 16;
  109. unsigned long hwcap = getauxval(AT_HWCAP);
  110. #if defined(OPENSSL_ARM)
  111. static const unsigned long kNEON = 1 << 12;
  112. if ((hwcap & kNEON) == 0) {
  113. return;
  114. }
  115. /* In 32-bit mode, the ARMv8 feature bits are in a different aux vector
  116. * value. */
  117. static const unsigned long AT_HWCAP2 = 26;
  118. hwcap = getauxval(AT_HWCAP2);
  119. /* See /usr/include/asm/hwcap.h on an ARM installation for the source of
  120. * these values. */
  121. static const unsigned long kAES = 1 << 0;
  122. static const unsigned long kPMULL = 1 << 1;
  123. static const unsigned long kSHA1 = 1 << 2;
  124. static const unsigned long kSHA256 = 1 << 3;
  125. #elif defined(OPENSSL_AARCH64)
  126. /* See /usr/include/asm/hwcap.h on an aarch64 installation for the source of
  127. * these values. */
  128. static const unsigned long kNEON = 1 << 1;
  129. static const unsigned long kAES = 1 << 3;
  130. static const unsigned long kPMULL = 1 << 4;
  131. static const unsigned long kSHA1 = 1 << 5;
  132. static const unsigned long kSHA256 = 1 << 6;
  133. if ((hwcap & kNEON) == 0) {
  134. return;
  135. }
  136. #endif
  137. OPENSSL_armcap_P |= ARMV7_NEON;
  138. if (hwcap & kAES) {
  139. OPENSSL_armcap_P |= ARMV8_AES;
  140. }
  141. if (hwcap & kPMULL) {
  142. OPENSSL_armcap_P |= ARMV8_PMULL;
  143. }
  144. if (hwcap & kSHA1) {
  145. OPENSSL_armcap_P |= ARMV8_SHA1;
  146. }
  147. if (hwcap & kSHA256) {
  148. OPENSSL_armcap_P |= ARMV8_SHA256;
  149. }
  150. }
  151. #endif /* defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) */