armcap.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2011-2021 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. # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  57. # if __GLIBC_PREREQ(2, 16)
  58. # include <sys/auxv.h>
  59. # define OSSL_IMPLEMENT_GETAUXVAL
  60. # endif
  61. # elif defined(__ANDROID_API__)
  62. /* see https://developer.android.google.cn/ndk/guides/cpu-features */
  63. # if __ANDROID_API__ >= 18
  64. # include <sys/auxv.h>
  65. # define OSSL_IMPLEMENT_GETAUXVAL
  66. # endif
  67. # endif
  68. # if defined(__FreeBSD__)
  69. # include <sys/param.h>
  70. # if __FreeBSD_version >= 1200000
  71. # include <sys/auxv.h>
  72. # define OSSL_IMPLEMENT_GETAUXVAL
  73. static unsigned long getauxval(unsigned long key)
  74. {
  75. unsigned long val = 0ul;
  76. if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
  77. return 0ul;
  78. return val;
  79. }
  80. # endif
  81. # endif
  82. /*
  83. * Android: according to https://developer.android.com/ndk/guides/cpu-features,
  84. * getauxval is supported starting with API level 18
  85. */
  86. # if defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 18
  87. # include <sys/auxv.h>
  88. # define OSSL_IMPLEMENT_GETAUXVAL
  89. # endif
  90. /*
  91. * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  92. * AArch64 used AT_HWCAP.
  93. */
  94. # ifndef AT_HWCAP
  95. # define AT_HWCAP 16
  96. # endif
  97. # ifndef AT_HWCAP2
  98. # define AT_HWCAP2 26
  99. # endif
  100. # if defined(__arm__) || defined (__arm)
  101. # define HWCAP AT_HWCAP
  102. # define HWCAP_NEON (1 << 12)
  103. # define HWCAP_CE AT_HWCAP2
  104. # define HWCAP_CE_AES (1 << 0)
  105. # define HWCAP_CE_PMULL (1 << 1)
  106. # define HWCAP_CE_SHA1 (1 << 2)
  107. # define HWCAP_CE_SHA256 (1 << 3)
  108. # elif defined(__aarch64__)
  109. # define HWCAP AT_HWCAP
  110. # define HWCAP_NEON (1 << 1)
  111. # define HWCAP_CE HWCAP
  112. # define HWCAP_CE_AES (1 << 3)
  113. # define HWCAP_CE_PMULL (1 << 4)
  114. # define HWCAP_CE_SHA1 (1 << 5)
  115. # define HWCAP_CE_SHA256 (1 << 6)
  116. # define HWCAP_CE_SHA512 (1 << 21)
  117. # endif
  118. void OPENSSL_cpuid_setup(void)
  119. {
  120. const char *e;
  121. struct sigaction ill_oact, ill_act;
  122. sigset_t oset;
  123. static int trigger = 0;
  124. if (trigger)
  125. return;
  126. trigger = 1;
  127. if ((e = getenv("OPENSSL_armcap"))) {
  128. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  129. return;
  130. }
  131. # if defined(__APPLE__) && !defined(__aarch64__)
  132. /*
  133. * Capability probing by catching SIGILL appears to be problematic
  134. * on iOS. But since Apple universe is "monocultural", it's actually
  135. * possible to simply set pre-defined processor capability mask.
  136. */
  137. if (1) {
  138. OPENSSL_armcap_P = ARMV7_NEON;
  139. return;
  140. }
  141. /*
  142. * One could do same even for __aarch64__ iOS builds. It's not done
  143. * exclusively for reasons of keeping code unified across platforms.
  144. * Unified code works because it never triggers SIGILL on Apple
  145. * devices...
  146. */
  147. # endif
  148. OPENSSL_armcap_P = 0;
  149. # ifdef OSSL_IMPLEMENT_GETAUXVAL
  150. if (getauxval(HWCAP) & HWCAP_NEON) {
  151. unsigned long hwcap = getauxval(HWCAP_CE);
  152. OPENSSL_armcap_P |= ARMV7_NEON;
  153. if (hwcap & HWCAP_CE_AES)
  154. OPENSSL_armcap_P |= ARMV8_AES;
  155. if (hwcap & HWCAP_CE_PMULL)
  156. OPENSSL_armcap_P |= ARMV8_PMULL;
  157. if (hwcap & HWCAP_CE_SHA1)
  158. OPENSSL_armcap_P |= ARMV8_SHA1;
  159. if (hwcap & HWCAP_CE_SHA256)
  160. OPENSSL_armcap_P |= ARMV8_SHA256;
  161. # ifdef __aarch64__
  162. if (hwcap & HWCAP_CE_SHA512)
  163. OPENSSL_armcap_P |= ARMV8_SHA512;
  164. # endif
  165. }
  166. # endif
  167. sigfillset(&all_masked);
  168. sigdelset(&all_masked, SIGILL);
  169. sigdelset(&all_masked, SIGTRAP);
  170. sigdelset(&all_masked, SIGFPE);
  171. sigdelset(&all_masked, SIGBUS);
  172. sigdelset(&all_masked, SIGSEGV);
  173. memset(&ill_act, 0, sizeof(ill_act));
  174. ill_act.sa_handler = ill_handler;
  175. ill_act.sa_mask = all_masked;
  176. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  177. sigaction(SIGILL, &ill_act, &ill_oact);
  178. /* If we used getauxval, we already have all the values */
  179. # ifndef OSSL_IMPLEMENT_GETAUXVAL
  180. if (sigsetjmp(ill_jmp, 1) == 0) {
  181. _armv7_neon_probe();
  182. OPENSSL_armcap_P |= ARMV7_NEON;
  183. if (sigsetjmp(ill_jmp, 1) == 0) {
  184. _armv8_pmull_probe();
  185. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  186. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  187. _armv8_aes_probe();
  188. OPENSSL_armcap_P |= ARMV8_AES;
  189. }
  190. if (sigsetjmp(ill_jmp, 1) == 0) {
  191. _armv8_sha1_probe();
  192. OPENSSL_armcap_P |= ARMV8_SHA1;
  193. }
  194. if (sigsetjmp(ill_jmp, 1) == 0) {
  195. _armv8_sha256_probe();
  196. OPENSSL_armcap_P |= ARMV8_SHA256;
  197. }
  198. # if defined(__aarch64__) && !defined(__APPLE__)
  199. if (sigsetjmp(ill_jmp, 1) == 0) {
  200. _armv8_sha512_probe();
  201. OPENSSL_armcap_P |= ARMV8_SHA512;
  202. }
  203. # endif
  204. }
  205. # endif
  206. #ifndef ARCADIA_OPENSSL_DISABLE_ARMV7_TICK
  207. /* Things that getauxval didn't tell us */
  208. if (sigsetjmp(ill_jmp, 1) == 0) {
  209. _armv7_tick();
  210. OPENSSL_armcap_P |= ARMV7_TICK;
  211. }
  212. #endif
  213. sigaction(SIGILL, &ill_oact, NULL);
  214. sigprocmask(SIG_SETMASK, &oset, NULL);
  215. }
  216. #endif