2
0

armcap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. # if defined(__arm__) || defined (__arm)
  95. # define HWCAP 16
  96. /* AT_HWCAP */
  97. # define HWCAP_NEON (1 << 12)
  98. # define HWCAP_CE 26
  99. /* AT_HWCAP2 */
  100. # define HWCAP_CE_AES (1 << 0)
  101. # define HWCAP_CE_PMULL (1 << 1)
  102. # define HWCAP_CE_SHA1 (1 << 2)
  103. # define HWCAP_CE_SHA256 (1 << 3)
  104. # elif defined(__aarch64__)
  105. # define HWCAP 16
  106. /* AT_HWCAP */
  107. # define HWCAP_NEON (1 << 1)
  108. # define HWCAP_CE HWCAP
  109. # define HWCAP_CE_AES (1 << 3)
  110. # define HWCAP_CE_PMULL (1 << 4)
  111. # define HWCAP_CE_SHA1 (1 << 5)
  112. # define HWCAP_CE_SHA256 (1 << 6)
  113. # define HWCAP_CE_SHA512 (1 << 21)
  114. # endif
  115. void OPENSSL_cpuid_setup(void)
  116. {
  117. const char *e;
  118. struct sigaction ill_oact, ill_act;
  119. sigset_t oset;
  120. static int trigger = 0;
  121. if (trigger)
  122. return;
  123. trigger = 1;
  124. if ((e = getenv("OPENSSL_armcap"))) {
  125. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  126. return;
  127. }
  128. # if defined(__APPLE__) && !defined(__aarch64__)
  129. /*
  130. * Capability probing by catching SIGILL appears to be problematic
  131. * on iOS. But since Apple universe is "monocultural", it's actually
  132. * possible to simply set pre-defined processor capability mask.
  133. */
  134. if (1) {
  135. OPENSSL_armcap_P = ARMV7_NEON;
  136. return;
  137. }
  138. /*
  139. * One could do same even for __aarch64__ iOS builds. It's not done
  140. * exclusively for reasons of keeping code unified across platforms.
  141. * Unified code works because it never triggers SIGILL on Apple
  142. * devices...
  143. */
  144. # endif
  145. OPENSSL_armcap_P = 0;
  146. # ifdef OSSL_IMPLEMENT_GETAUXVAL
  147. if (getauxval(HWCAP) & HWCAP_NEON) {
  148. unsigned long hwcap = getauxval(HWCAP_CE);
  149. OPENSSL_armcap_P |= ARMV7_NEON;
  150. if (hwcap & HWCAP_CE_AES)
  151. OPENSSL_armcap_P |= ARMV8_AES;
  152. if (hwcap & HWCAP_CE_PMULL)
  153. OPENSSL_armcap_P |= ARMV8_PMULL;
  154. if (hwcap & HWCAP_CE_SHA1)
  155. OPENSSL_armcap_P |= ARMV8_SHA1;
  156. if (hwcap & HWCAP_CE_SHA256)
  157. OPENSSL_armcap_P |= ARMV8_SHA256;
  158. # ifdef __aarch64__
  159. if (hwcap & HWCAP_CE_SHA512)
  160. OPENSSL_armcap_P |= ARMV8_SHA512;
  161. # endif
  162. }
  163. # endif
  164. sigfillset(&all_masked);
  165. sigdelset(&all_masked, SIGILL);
  166. sigdelset(&all_masked, SIGTRAP);
  167. sigdelset(&all_masked, SIGFPE);
  168. sigdelset(&all_masked, SIGBUS);
  169. sigdelset(&all_masked, SIGSEGV);
  170. memset(&ill_act, 0, sizeof(ill_act));
  171. ill_act.sa_handler = ill_handler;
  172. ill_act.sa_mask = all_masked;
  173. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  174. sigaction(SIGILL, &ill_act, &ill_oact);
  175. /* If we used getauxval, we already have all the values */
  176. # ifndef OSSL_IMPLEMENT_GETAUXVAL
  177. if (sigsetjmp(ill_jmp, 1) == 0) {
  178. _armv7_neon_probe();
  179. OPENSSL_armcap_P |= ARMV7_NEON;
  180. if (sigsetjmp(ill_jmp, 1) == 0) {
  181. _armv8_pmull_probe();
  182. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  183. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  184. _armv8_aes_probe();
  185. OPENSSL_armcap_P |= ARMV8_AES;
  186. }
  187. if (sigsetjmp(ill_jmp, 1) == 0) {
  188. _armv8_sha1_probe();
  189. OPENSSL_armcap_P |= ARMV8_SHA1;
  190. }
  191. if (sigsetjmp(ill_jmp, 1) == 0) {
  192. _armv8_sha256_probe();
  193. OPENSSL_armcap_P |= ARMV8_SHA256;
  194. }
  195. # if defined(__aarch64__) && !defined(__APPLE__)
  196. if (sigsetjmp(ill_jmp, 1) == 0) {
  197. _armv8_sha512_probe();
  198. OPENSSL_armcap_P |= ARMV8_SHA512;
  199. }
  200. # endif
  201. }
  202. # endif
  203. /* Things that getauxval didn't tell us */
  204. if (sigsetjmp(ill_jmp, 1) == 0) {
  205. _armv7_tick();
  206. OPENSSL_armcap_P |= ARMV7_TICK;
  207. }
  208. sigaction(SIGILL, &ill_oact, NULL);
  209. sigprocmask(SIG_SETMASK, &oset, NULL);
  210. }
  211. #endif