armcap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include <openssl/crypto.h>
  15. #include "internal/cryptlib.h"
  16. #include "arm_arch.h"
  17. unsigned int OPENSSL_armcap_P = 0;
  18. #if __ARM_MAX_ARCH__<7
  19. void OPENSSL_cpuid_setup(void)
  20. {
  21. }
  22. uint32_t OPENSSL_rdtsc(void)
  23. {
  24. return 0;
  25. }
  26. #else
  27. static sigset_t all_masked;
  28. static sigjmp_buf ill_jmp;
  29. static void ill_handler(int sig)
  30. {
  31. siglongjmp(ill_jmp, sig);
  32. }
  33. /*
  34. * Following subroutines could have been inlined, but it's not all
  35. * ARM compilers support inline assembler...
  36. */
  37. void _armv7_neon_probe(void);
  38. void _armv8_aes_probe(void);
  39. void _armv8_sha1_probe(void);
  40. void _armv8_sha256_probe(void);
  41. void _armv8_pmull_probe(void);
  42. # ifdef __aarch64__
  43. void _armv8_sha512_probe(void);
  44. # endif
  45. uint32_t _armv7_tick(void);
  46. uint32_t OPENSSL_rdtsc(void)
  47. {
  48. if (OPENSSL_armcap_P & ARMV7_TICK)
  49. return _armv7_tick();
  50. else
  51. return 0;
  52. }
  53. # if defined(__GNUC__) && __GNUC__>=2
  54. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  55. # endif
  56. /*
  57. * Use a weak reference to getauxval() so we can use it if it is available but
  58. * don't break the build if it is not.
  59. */
  60. # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
  61. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  62. # else
  63. static unsigned long (*getauxval) (unsigned long) = NULL;
  64. # endif
  65. /*
  66. * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  67. * AArch64 used AT_HWCAP.
  68. */
  69. # if defined(__arm__) || defined (__arm)
  70. # define HWCAP 16
  71. /* AT_HWCAP */
  72. # define HWCAP_NEON (1 << 12)
  73. # define HWCAP_CE 26
  74. /* AT_HWCAP2 */
  75. # define HWCAP_CE_AES (1 << 0)
  76. # define HWCAP_CE_PMULL (1 << 1)
  77. # define HWCAP_CE_SHA1 (1 << 2)
  78. # define HWCAP_CE_SHA256 (1 << 3)
  79. # elif defined(__aarch64__)
  80. # define HWCAP 16
  81. /* AT_HWCAP */
  82. # define HWCAP_NEON (1 << 1)
  83. # define HWCAP_CE HWCAP
  84. # define HWCAP_CE_AES (1 << 3)
  85. # define HWCAP_CE_PMULL (1 << 4)
  86. # define HWCAP_CE_SHA1 (1 << 5)
  87. # define HWCAP_CE_SHA256 (1 << 6)
  88. # define HWCAP_CE_SHA512 (1 << 21)
  89. # endif
  90. void OPENSSL_cpuid_setup(void)
  91. {
  92. const char *e;
  93. struct sigaction ill_oact, ill_act;
  94. sigset_t oset;
  95. static int trigger = 0;
  96. if (trigger)
  97. return;
  98. trigger = 1;
  99. if ((e = getenv("OPENSSL_armcap"))) {
  100. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  101. return;
  102. }
  103. # if defined(__APPLE__) && !defined(__aarch64__)
  104. /*
  105. * Capability probing by catching SIGILL appears to be problematic
  106. * on iOS. But since Apple universe is "monocultural", it's actually
  107. * possible to simply set pre-defined processor capability mask.
  108. */
  109. if (1) {
  110. OPENSSL_armcap_P = ARMV7_NEON;
  111. return;
  112. }
  113. /*
  114. * One could do same even for __aarch64__ iOS builds. It's not done
  115. * exclusively for reasons of keeping code unified across platforms.
  116. * Unified code works because it never triggers SIGILL on Apple
  117. * devices...
  118. */
  119. # endif
  120. sigfillset(&all_masked);
  121. sigdelset(&all_masked, SIGILL);
  122. sigdelset(&all_masked, SIGTRAP);
  123. sigdelset(&all_masked, SIGFPE);
  124. sigdelset(&all_masked, SIGBUS);
  125. sigdelset(&all_masked, SIGSEGV);
  126. OPENSSL_armcap_P = 0;
  127. memset(&ill_act, 0, sizeof(ill_act));
  128. ill_act.sa_handler = ill_handler;
  129. ill_act.sa_mask = all_masked;
  130. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  131. sigaction(SIGILL, &ill_act, &ill_oact);
  132. if (getauxval != NULL) {
  133. if (getauxval(HWCAP) & HWCAP_NEON) {
  134. unsigned long hwcap = getauxval(HWCAP_CE);
  135. OPENSSL_armcap_P |= ARMV7_NEON;
  136. if (hwcap & HWCAP_CE_AES)
  137. OPENSSL_armcap_P |= ARMV8_AES;
  138. if (hwcap & HWCAP_CE_PMULL)
  139. OPENSSL_armcap_P |= ARMV8_PMULL;
  140. if (hwcap & HWCAP_CE_SHA1)
  141. OPENSSL_armcap_P |= ARMV8_SHA1;
  142. if (hwcap & HWCAP_CE_SHA256)
  143. OPENSSL_armcap_P |= ARMV8_SHA256;
  144. # ifdef __aarch64__
  145. if (hwcap & HWCAP_CE_SHA512)
  146. OPENSSL_armcap_P |= ARMV8_SHA512;
  147. # endif
  148. }
  149. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  150. _armv7_neon_probe();
  151. OPENSSL_armcap_P |= ARMV7_NEON;
  152. if (sigsetjmp(ill_jmp, 1) == 0) {
  153. _armv8_pmull_probe();
  154. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  155. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  156. _armv8_aes_probe();
  157. OPENSSL_armcap_P |= ARMV8_AES;
  158. }
  159. if (sigsetjmp(ill_jmp, 1) == 0) {
  160. _armv8_sha1_probe();
  161. OPENSSL_armcap_P |= ARMV8_SHA1;
  162. }
  163. if (sigsetjmp(ill_jmp, 1) == 0) {
  164. _armv8_sha256_probe();
  165. OPENSSL_armcap_P |= ARMV8_SHA256;
  166. }
  167. # if defined(__aarch64__) && !defined(__APPLE__)
  168. if (sigsetjmp(ill_jmp, 1) == 0) {
  169. _armv8_sha512_probe();
  170. OPENSSL_armcap_P |= ARMV8_SHA512;
  171. }
  172. # endif
  173. }
  174. if (sigsetjmp(ill_jmp, 1) == 0) {
  175. _armv7_tick();
  176. OPENSSL_armcap_P |= ARMV7_TICK;
  177. }
  178. sigaction(SIGILL, &ill_oact, NULL);
  179. sigprocmask(SIG_SETMASK, &oset, NULL);
  180. }
  181. #endif