armcap.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #ifdef __APPLE__
  16. #include <sys/sysctl.h>
  17. #endif
  18. #include "internal/cryptlib.h"
  19. #include "arm_arch.h"
  20. unsigned int OPENSSL_armcap_P = 0;
  21. unsigned int OPENSSL_arm_midr = 0;
  22. unsigned int OPENSSL_armv8_rsa_neonized = 0;
  23. #if __ARM_MAX_ARCH__<7
  24. void OPENSSL_cpuid_setup(void)
  25. {
  26. }
  27. uint32_t OPENSSL_rdtsc(void)
  28. {
  29. return 0;
  30. }
  31. #else
  32. static sigset_t all_masked;
  33. static sigjmp_buf ill_jmp;
  34. static void ill_handler(int sig)
  35. {
  36. siglongjmp(ill_jmp, sig);
  37. }
  38. /*
  39. * Following subroutines could have been inlined, but it's not all
  40. * ARM compilers support inline assembler...
  41. */
  42. void _armv7_neon_probe(void);
  43. void _armv8_aes_probe(void);
  44. void _armv8_sha1_probe(void);
  45. void _armv8_sha256_probe(void);
  46. void _armv8_pmull_probe(void);
  47. # ifdef __aarch64__
  48. void _armv8_sha512_probe(void);
  49. unsigned int _armv8_cpuid_probe(void);
  50. # endif
  51. uint32_t _armv7_tick(void);
  52. uint32_t OPENSSL_rdtsc(void)
  53. {
  54. if (OPENSSL_armcap_P & ARMV7_TICK)
  55. return _armv7_tick();
  56. else
  57. return 0;
  58. }
  59. # if defined(__GNUC__) && __GNUC__>=2
  60. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  61. # endif
  62. # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  63. # if __GLIBC_PREREQ(2, 16)
  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_CPUID (1 << 11)
  114. # define HWCAP_CE_SHA512 (1 << 21)
  115. # endif
  116. void OPENSSL_cpuid_setup(void)
  117. {
  118. const char *e;
  119. struct sigaction ill_oact, ill_act;
  120. sigset_t oset;
  121. static int trigger = 0;
  122. if (trigger)
  123. return;
  124. trigger = 1;
  125. OPENSSL_armcap_P = 0;
  126. if ((e = getenv("OPENSSL_armcap"))) {
  127. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  128. return;
  129. }
  130. # if defined(__APPLE__)
  131. # if !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. # else
  148. {
  149. unsigned int sha512;
  150. size_t len = sizeof(sha512);
  151. if (sysctlbyname("hw.optional.armv8_2_sha512", &sha512, &len, NULL, 0) == 0 && sha512 == 1)
  152. OPENSSL_armcap_P |= ARMV8_SHA512;
  153. }
  154. # endif
  155. # endif
  156. # ifdef OSSL_IMPLEMENT_GETAUXVAL
  157. if (getauxval(HWCAP) & HWCAP_NEON) {
  158. unsigned long hwcap = getauxval(HWCAP_CE);
  159. OPENSSL_armcap_P |= ARMV7_NEON;
  160. if (hwcap & HWCAP_CE_AES)
  161. OPENSSL_armcap_P |= ARMV8_AES;
  162. if (hwcap & HWCAP_CE_PMULL)
  163. OPENSSL_armcap_P |= ARMV8_PMULL;
  164. if (hwcap & HWCAP_CE_SHA1)
  165. OPENSSL_armcap_P |= ARMV8_SHA1;
  166. if (hwcap & HWCAP_CE_SHA256)
  167. OPENSSL_armcap_P |= ARMV8_SHA256;
  168. # ifdef __aarch64__
  169. if (hwcap & HWCAP_CE_SHA512)
  170. OPENSSL_armcap_P |= ARMV8_SHA512;
  171. if (hwcap & HWCAP_CPUID)
  172. OPENSSL_armcap_P |= ARMV8_CPUID;
  173. # endif
  174. }
  175. # endif
  176. sigfillset(&all_masked);
  177. sigdelset(&all_masked, SIGILL);
  178. sigdelset(&all_masked, SIGTRAP);
  179. sigdelset(&all_masked, SIGFPE);
  180. sigdelset(&all_masked, SIGBUS);
  181. sigdelset(&all_masked, SIGSEGV);
  182. memset(&ill_act, 0, sizeof(ill_act));
  183. ill_act.sa_handler = ill_handler;
  184. ill_act.sa_mask = all_masked;
  185. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  186. sigaction(SIGILL, &ill_act, &ill_oact);
  187. /* If we used getauxval, we already have all the values */
  188. # ifndef OSSL_IMPLEMENT_GETAUXVAL
  189. if (sigsetjmp(ill_jmp, 1) == 0) {
  190. _armv7_neon_probe();
  191. OPENSSL_armcap_P |= ARMV7_NEON;
  192. if (sigsetjmp(ill_jmp, 1) == 0) {
  193. _armv8_pmull_probe();
  194. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  195. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  196. _armv8_aes_probe();
  197. OPENSSL_armcap_P |= ARMV8_AES;
  198. }
  199. if (sigsetjmp(ill_jmp, 1) == 0) {
  200. _armv8_sha1_probe();
  201. OPENSSL_armcap_P |= ARMV8_SHA1;
  202. }
  203. if (sigsetjmp(ill_jmp, 1) == 0) {
  204. _armv8_sha256_probe();
  205. OPENSSL_armcap_P |= ARMV8_SHA256;
  206. }
  207. # if defined(__aarch64__) && !defined(__APPLE__)
  208. if (sigsetjmp(ill_jmp, 1) == 0) {
  209. _armv8_sha512_probe();
  210. OPENSSL_armcap_P |= ARMV8_SHA512;
  211. }
  212. # endif
  213. }
  214. # endif
  215. /* Things that getauxval didn't tell us */
  216. if (sigsetjmp(ill_jmp, 1) == 0) {
  217. _armv7_tick();
  218. OPENSSL_armcap_P |= ARMV7_TICK;
  219. }
  220. sigaction(SIGILL, &ill_oact, NULL);
  221. sigprocmask(SIG_SETMASK, &oset, NULL);
  222. # ifdef __aarch64__
  223. if (OPENSSL_armcap_P & ARMV8_CPUID)
  224. OPENSSL_arm_midr = _armv8_cpuid_probe();
  225. if ((MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72) ||
  226. MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1)) &&
  227. (OPENSSL_armcap_P & ARMV7_NEON)) {
  228. OPENSSL_armv8_rsa_neonized = 1;
  229. }
  230. # endif
  231. }
  232. #endif