armcap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2011-2016 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 "arm_arch.h"
  16. unsigned int OPENSSL_armcap_P = 0;
  17. #if __ARM_MAX_ARCH__<7
  18. void OPENSSL_cpuid_setup(void)
  19. {
  20. }
  21. unsigned long OPENSSL_rdtsc(void)
  22. {
  23. return 0;
  24. }
  25. #else
  26. static sigset_t all_masked;
  27. static sigjmp_buf ill_jmp;
  28. static void ill_handler(int sig)
  29. {
  30. siglongjmp(ill_jmp, sig);
  31. }
  32. /*
  33. * Following subroutines could have been inlined, but it's not all
  34. * ARM compilers support inline assembler...
  35. */
  36. void _armv7_neon_probe(void);
  37. void _armv8_aes_probe(void);
  38. void _armv8_sha1_probe(void);
  39. void _armv8_sha256_probe(void);
  40. void _armv8_pmull_probe(void);
  41. unsigned long _armv7_tick(void);
  42. unsigned long OPENSSL_rdtsc(void)
  43. {
  44. if (OPENSSL_armcap_P & ARMV7_TICK)
  45. return _armv7_tick();
  46. else
  47. return 0;
  48. }
  49. # if defined(__GNUC__) && __GNUC__>=2
  50. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  51. # endif
  52. /*
  53. * Use a weak reference to getauxval() so we can use it if it is available but
  54. * don't break the build if it is not.
  55. */
  56. # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
  57. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  58. # else
  59. static unsigned long (*getauxval) (unsigned long) = NULL;
  60. # endif
  61. /*
  62. * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  63. * AArch64 used AT_HWCAP.
  64. */
  65. # if defined(__arm__) || defined (__arm)
  66. # define HWCAP 16
  67. /* AT_HWCAP */
  68. # define HWCAP_NEON (1 << 12)
  69. # define HWCAP_CE 26
  70. /* AT_HWCAP2 */
  71. # define HWCAP_CE_AES (1 << 0)
  72. # define HWCAP_CE_PMULL (1 << 1)
  73. # define HWCAP_CE_SHA1 (1 << 2)
  74. # define HWCAP_CE_SHA256 (1 << 3)
  75. # elif defined(__aarch64__)
  76. # define HWCAP 16
  77. /* AT_HWCAP */
  78. # define HWCAP_NEON (1 << 1)
  79. # define HWCAP_CE HWCAP
  80. # define HWCAP_CE_AES (1 << 3)
  81. # define HWCAP_CE_PMULL (1 << 4)
  82. # define HWCAP_CE_SHA1 (1 << 5)
  83. # define HWCAP_CE_SHA256 (1 << 6)
  84. # endif
  85. void OPENSSL_cpuid_setup(void)
  86. {
  87. char *e;
  88. struct sigaction ill_oact, ill_act;
  89. sigset_t oset;
  90. static int trigger = 0;
  91. if (trigger)
  92. return;
  93. trigger = 1;
  94. if ((e = getenv("OPENSSL_armcap"))) {
  95. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  96. return;
  97. }
  98. # if defined(__APPLE__) && !defined(__aarch64__)
  99. /*
  100. * Capability probing by catching SIGILL appears to be problematic
  101. * on iOS. But since Apple universe is "monocultural", it's actually
  102. * possible to simply set pre-defined processor capability mask.
  103. */
  104. if (1) {
  105. OPENSSL_armcap_P = ARMV7_NEON;
  106. return;
  107. }
  108. /*
  109. * One could do same even for __aarch64__ iOS builds. It's not done
  110. * exclusively for reasons of keeping code unified across platforms.
  111. * Unified code works because it never triggers SIGILL on Apple
  112. * devices...
  113. */
  114. # endif
  115. sigfillset(&all_masked);
  116. sigdelset(&all_masked, SIGILL);
  117. sigdelset(&all_masked, SIGTRAP);
  118. sigdelset(&all_masked, SIGFPE);
  119. sigdelset(&all_masked, SIGBUS);
  120. sigdelset(&all_masked, SIGSEGV);
  121. OPENSSL_armcap_P = 0;
  122. memset(&ill_act, 0, sizeof(ill_act));
  123. ill_act.sa_handler = ill_handler;
  124. ill_act.sa_mask = all_masked;
  125. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  126. sigaction(SIGILL, &ill_act, &ill_oact);
  127. if (getauxval != NULL) {
  128. if (getauxval(HWCAP) & HWCAP_NEON) {
  129. unsigned long hwcap = getauxval(HWCAP_CE);
  130. OPENSSL_armcap_P |= ARMV7_NEON;
  131. if (hwcap & HWCAP_CE_AES)
  132. OPENSSL_armcap_P |= ARMV8_AES;
  133. if (hwcap & HWCAP_CE_PMULL)
  134. OPENSSL_armcap_P |= ARMV8_PMULL;
  135. if (hwcap & HWCAP_CE_SHA1)
  136. OPENSSL_armcap_P |= ARMV8_SHA1;
  137. if (hwcap & HWCAP_CE_SHA256)
  138. OPENSSL_armcap_P |= ARMV8_SHA256;
  139. }
  140. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  141. _armv7_neon_probe();
  142. OPENSSL_armcap_P |= ARMV7_NEON;
  143. if (sigsetjmp(ill_jmp, 1) == 0) {
  144. _armv8_pmull_probe();
  145. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  146. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  147. _armv8_aes_probe();
  148. OPENSSL_armcap_P |= ARMV8_AES;
  149. }
  150. if (sigsetjmp(ill_jmp, 1) == 0) {
  151. _armv8_sha1_probe();
  152. OPENSSL_armcap_P |= ARMV8_SHA1;
  153. }
  154. if (sigsetjmp(ill_jmp, 1) == 0) {
  155. _armv8_sha256_probe();
  156. OPENSSL_armcap_P |= ARMV8_SHA256;
  157. }
  158. }
  159. if (sigsetjmp(ill_jmp, 1) == 0) {
  160. _armv7_tick();
  161. OPENSSL_armcap_P |= ARMV7_TICK;
  162. }
  163. sigaction(SIGILL, &ill_oact, NULL);
  164. sigprocmask(SIG_SETMASK, &oset, NULL);
  165. }
  166. #endif