armcap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "internal/cryptlib.h"
  16. #include "arm_arch.h"
  17. unsigned int OPENSSL_armcap_P = 0;
  18. unsigned int OPENSSL_arm_midr = 0;
  19. #if __ARM_MAX_ARCH__<7
  20. void OPENSSL_cpuid_setup(void)
  21. {
  22. }
  23. uint32_t OPENSSL_rdtsc(void)
  24. {
  25. return 0;
  26. }
  27. #else
  28. static sigset_t all_masked;
  29. static sigjmp_buf ill_jmp;
  30. static void ill_handler(int sig)
  31. {
  32. siglongjmp(ill_jmp, sig);
  33. }
  34. /*
  35. * Following subroutines could have been inlined, but it's not all
  36. * ARM compilers support inline assembler...
  37. */
  38. void _armv7_neon_probe(void);
  39. void _armv8_aes_probe(void);
  40. void _armv8_sha1_probe(void);
  41. void _armv8_sha256_probe(void);
  42. void _armv8_pmull_probe(void);
  43. # ifdef __aarch64__
  44. void _armv8_sha512_probe(void);
  45. unsigned int _armv8_cpuid_probe(void);
  46. # endif
  47. uint32_t _armv7_tick(void);
  48. uint32_t OPENSSL_rdtsc(void)
  49. {
  50. if (OPENSSL_armcap_P & ARMV7_TICK)
  51. return _armv7_tick();
  52. else
  53. return 0;
  54. }
  55. # if defined(__GNUC__) && __GNUC__>=2
  56. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  57. # endif
  58. # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  59. # if __GLIBC_PREREQ(2, 16)
  60. # include <sys/auxv.h>
  61. # define OSSL_IMPLEMENT_GETAUXVAL
  62. # endif
  63. # endif
  64. # if defined(__FreeBSD__)
  65. # include <sys/param.h>
  66. # if __FreeBSD_version >= 1200000
  67. # include <sys/auxv.h>
  68. # define OSSL_IMPLEMENT_GETAUXVAL
  69. static unsigned long getauxval(unsigned long key)
  70. {
  71. unsigned long val = 0ul;
  72. if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
  73. return 0ul;
  74. return val;
  75. }
  76. # endif
  77. # endif
  78. /*
  79. * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  80. * AArch64 used AT_HWCAP.
  81. */
  82. # if defined(__arm__) || defined (__arm)
  83. # define HWCAP 16
  84. /* AT_HWCAP */
  85. # define HWCAP_NEON (1 << 12)
  86. # define HWCAP_CE 26
  87. /* AT_HWCAP2 */
  88. # define HWCAP_CE_AES (1 << 0)
  89. # define HWCAP_CE_PMULL (1 << 1)
  90. # define HWCAP_CE_SHA1 (1 << 2)
  91. # define HWCAP_CE_SHA256 (1 << 3)
  92. # elif defined(__aarch64__)
  93. # define HWCAP 16
  94. /* AT_HWCAP */
  95. # define HWCAP_NEON (1 << 1)
  96. # define HWCAP_CE HWCAP
  97. # define HWCAP_CE_AES (1 << 3)
  98. # define HWCAP_CE_PMULL (1 << 4)
  99. # define HWCAP_CE_SHA1 (1 << 5)
  100. # define HWCAP_CE_SHA256 (1 << 6)
  101. # define HWCAP_CPUID (1 << 11)
  102. # define HWCAP_CE_SHA512 (1 << 21)
  103. # endif
  104. void OPENSSL_cpuid_setup(void)
  105. {
  106. const char *e;
  107. struct sigaction ill_oact, ill_act;
  108. sigset_t oset;
  109. static int trigger = 0;
  110. if (trigger)
  111. return;
  112. trigger = 1;
  113. if ((e = getenv("OPENSSL_armcap"))) {
  114. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  115. return;
  116. }
  117. # if defined(__APPLE__) && !defined(__aarch64__)
  118. /*
  119. * Capability probing by catching SIGILL appears to be problematic
  120. * on iOS. But since Apple universe is "monocultural", it's actually
  121. * possible to simply set pre-defined processor capability mask.
  122. */
  123. if (1) {
  124. OPENSSL_armcap_P = ARMV7_NEON;
  125. return;
  126. }
  127. /*
  128. * One could do same even for __aarch64__ iOS builds. It's not done
  129. * exclusively for reasons of keeping code unified across platforms.
  130. * Unified code works because it never triggers SIGILL on Apple
  131. * devices...
  132. */
  133. # endif
  134. OPENSSL_armcap_P = 0;
  135. # ifdef OSSL_IMPLEMENT_GETAUXVAL
  136. if (getauxval(HWCAP) & HWCAP_NEON) {
  137. unsigned long hwcap = getauxval(HWCAP_CE);
  138. OPENSSL_armcap_P |= ARMV7_NEON;
  139. if (hwcap & HWCAP_CE_AES)
  140. OPENSSL_armcap_P |= ARMV8_AES;
  141. if (hwcap & HWCAP_CE_PMULL)
  142. OPENSSL_armcap_P |= ARMV8_PMULL;
  143. if (hwcap & HWCAP_CE_SHA1)
  144. OPENSSL_armcap_P |= ARMV8_SHA1;
  145. if (hwcap & HWCAP_CE_SHA256)
  146. OPENSSL_armcap_P |= ARMV8_SHA256;
  147. # ifdef __aarch64__
  148. if (hwcap & HWCAP_CE_SHA512)
  149. OPENSSL_armcap_P |= ARMV8_SHA512;
  150. if (hwcap & HWCAP_CPUID)
  151. OPENSSL_armcap_P |= ARMV8_CPUID;
  152. # endif
  153. }
  154. # endif
  155. sigfillset(&all_masked);
  156. sigdelset(&all_masked, SIGILL);
  157. sigdelset(&all_masked, SIGTRAP);
  158. sigdelset(&all_masked, SIGFPE);
  159. sigdelset(&all_masked, SIGBUS);
  160. sigdelset(&all_masked, SIGSEGV);
  161. memset(&ill_act, 0, sizeof(ill_act));
  162. ill_act.sa_handler = ill_handler;
  163. ill_act.sa_mask = all_masked;
  164. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  165. sigaction(SIGILL, &ill_act, &ill_oact);
  166. /* If we used getauxval, we already have all the values */
  167. # ifndef OSSL_IMPLEMENT_GETAUXVAL
  168. if (sigsetjmp(ill_jmp, 1) == 0) {
  169. _armv7_neon_probe();
  170. OPENSSL_armcap_P |= ARMV7_NEON;
  171. if (sigsetjmp(ill_jmp, 1) == 0) {
  172. _armv8_pmull_probe();
  173. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  174. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  175. _armv8_aes_probe();
  176. OPENSSL_armcap_P |= ARMV8_AES;
  177. }
  178. if (sigsetjmp(ill_jmp, 1) == 0) {
  179. _armv8_sha1_probe();
  180. OPENSSL_armcap_P |= ARMV8_SHA1;
  181. }
  182. if (sigsetjmp(ill_jmp, 1) == 0) {
  183. _armv8_sha256_probe();
  184. OPENSSL_armcap_P |= ARMV8_SHA256;
  185. }
  186. # if defined(__aarch64__) && !defined(__APPLE__)
  187. if (sigsetjmp(ill_jmp, 1) == 0) {
  188. _armv8_sha512_probe();
  189. OPENSSL_armcap_P |= ARMV8_SHA512;
  190. }
  191. # endif
  192. }
  193. # endif
  194. /* Things that getauxval didn't tell us */
  195. if (sigsetjmp(ill_jmp, 1) == 0) {
  196. _armv7_tick();
  197. OPENSSL_armcap_P |= ARMV7_TICK;
  198. }
  199. sigaction(SIGILL, &ill_oact, NULL);
  200. sigprocmask(SIG_SETMASK, &oset, NULL);
  201. # ifdef __aarch64__
  202. if (OPENSSL_armcap_P & ARMV8_CPUID)
  203. OPENSSL_arm_midr = _armv8_cpuid_probe();
  204. # endif
  205. }
  206. #endif