armcap.c 6.8 KB

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