armcap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2011-2018 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. #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. # endif
  62. /*
  63. * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  64. * AArch64 used AT_HWCAP.
  65. */
  66. # if defined(__arm__) || defined (__arm)
  67. # define HWCAP 16
  68. /* AT_HWCAP */
  69. # define HWCAP_NEON (1 << 12)
  70. # define HWCAP_CE 26
  71. /* AT_HWCAP2 */
  72. # define HWCAP_CE_AES (1 << 0)
  73. # define HWCAP_CE_PMULL (1 << 1)
  74. # define HWCAP_CE_SHA1 (1 << 2)
  75. # define HWCAP_CE_SHA256 (1 << 3)
  76. # elif defined(__aarch64__)
  77. # define HWCAP 16
  78. /* AT_HWCAP */
  79. # define HWCAP_NEON (1 << 1)
  80. # define HWCAP_CE HWCAP
  81. # define HWCAP_CE_AES (1 << 3)
  82. # define HWCAP_CE_PMULL (1 << 4)
  83. # define HWCAP_CE_SHA1 (1 << 5)
  84. # define HWCAP_CE_SHA256 (1 << 6)
  85. # define HWCAP_CE_SHA512 (1 << 21)
  86. # endif
  87. void OPENSSL_cpuid_setup(void)
  88. {
  89. const char *e;
  90. struct sigaction ill_oact, ill_act;
  91. sigset_t oset;
  92. static int trigger = 0;
  93. if (trigger)
  94. return;
  95. trigger = 1;
  96. if ((e = getenv("OPENSSL_armcap"))) {
  97. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  98. return;
  99. }
  100. # if defined(__APPLE__) && !defined(__aarch64__)
  101. /*
  102. * Capability probing by catching SIGILL appears to be problematic
  103. * on iOS. But since Apple universe is "monocultural", it's actually
  104. * possible to simply set pre-defined processor capability mask.
  105. */
  106. if (1) {
  107. OPENSSL_armcap_P = ARMV7_NEON;
  108. return;
  109. }
  110. /*
  111. * One could do same even for __aarch64__ iOS builds. It's not done
  112. * exclusively for reasons of keeping code unified across platforms.
  113. * Unified code works because it never triggers SIGILL on Apple
  114. * devices...
  115. */
  116. # endif
  117. OPENSSL_armcap_P = 0;
  118. # ifdef OSSL_IMPLEMENT_GETAUXVAL
  119. if (getauxval(HWCAP) & HWCAP_NEON) {
  120. unsigned long hwcap = getauxval(HWCAP_CE);
  121. OPENSSL_armcap_P |= ARMV7_NEON;
  122. if (hwcap & HWCAP_CE_AES)
  123. OPENSSL_armcap_P |= ARMV8_AES;
  124. if (hwcap & HWCAP_CE_PMULL)
  125. OPENSSL_armcap_P |= ARMV8_PMULL;
  126. if (hwcap & HWCAP_CE_SHA1)
  127. OPENSSL_armcap_P |= ARMV8_SHA1;
  128. if (hwcap & HWCAP_CE_SHA256)
  129. OPENSSL_armcap_P |= ARMV8_SHA256;
  130. # ifdef __aarch64__
  131. if (hwcap & HWCAP_CE_SHA512)
  132. OPENSSL_armcap_P |= ARMV8_SHA512;
  133. # endif
  134. }
  135. # endif
  136. sigfillset(&all_masked);
  137. sigdelset(&all_masked, SIGILL);
  138. sigdelset(&all_masked, SIGTRAP);
  139. sigdelset(&all_masked, SIGFPE);
  140. sigdelset(&all_masked, SIGBUS);
  141. sigdelset(&all_masked, SIGSEGV);
  142. memset(&ill_act, 0, sizeof(ill_act));
  143. ill_act.sa_handler = ill_handler;
  144. ill_act.sa_mask = all_masked;
  145. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  146. sigaction(SIGILL, &ill_act, &ill_oact);
  147. /* If we used getauxval, we already have all the values */
  148. # ifndef OSSL_IMPLEMENT_GETAUXVAL
  149. if (sigsetjmp(ill_jmp, 1) == 0) {
  150. _armv7_neon_probe();
  151. OPENSSL_armcap_P |= ARMV7_NEON;
  152. if (sigsetjmp(ill_jmp, 1) == 0) {
  153. _armv8_pmull_probe();
  154. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  155. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  156. _armv8_aes_probe();
  157. OPENSSL_armcap_P |= ARMV8_AES;
  158. }
  159. if (sigsetjmp(ill_jmp, 1) == 0) {
  160. _armv8_sha1_probe();
  161. OPENSSL_armcap_P |= ARMV8_SHA1;
  162. }
  163. if (sigsetjmp(ill_jmp, 1) == 0) {
  164. _armv8_sha256_probe();
  165. OPENSSL_armcap_P |= ARMV8_SHA256;
  166. }
  167. # if defined(__aarch64__) && !defined(__APPLE__)
  168. if (sigsetjmp(ill_jmp, 1) == 0) {
  169. _armv8_sha512_probe();
  170. OPENSSL_armcap_P |= ARMV8_SHA512;
  171. }
  172. # endif
  173. }
  174. # endif
  175. /* Things that getauxval didn't tell us */
  176. if (sigsetjmp(ill_jmp, 1) == 0) {
  177. _armv7_tick();
  178. OPENSSL_armcap_P |= ARMV7_TICK;
  179. }
  180. sigaction(SIGILL, &ill_oact, NULL);
  181. sigprocmask(SIG_SETMASK, &oset, NULL);
  182. }
  183. #endif