armcap.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <openssl/crypto.h>
  7. #include "arm_arch.h"
  8. unsigned int OPENSSL_armcap_P = 0;
  9. #if __ARM_MAX_ARCH__<7
  10. void OPENSSL_cpuid_setup(void)
  11. {
  12. }
  13. unsigned long OPENSSL_rdtsc(void)
  14. {
  15. return 0;
  16. }
  17. #else
  18. static sigset_t all_masked;
  19. static sigjmp_buf ill_jmp;
  20. static void ill_handler(int sig)
  21. {
  22. siglongjmp(ill_jmp, sig);
  23. }
  24. /*
  25. * Following subroutines could have been inlined, but it's not all
  26. * ARM compilers support inline assembler...
  27. */
  28. void _armv7_neon_probe(void);
  29. void _armv8_aes_probe(void);
  30. void _armv8_sha1_probe(void);
  31. void _armv8_sha256_probe(void);
  32. void _armv8_pmull_probe(void);
  33. unsigned long _armv7_tick(void);
  34. unsigned long OPENSSL_rdtsc(void)
  35. {
  36. if (OPENSSL_armcap_P & ARMV7_TICK)
  37. return _armv7_tick();
  38. else
  39. return 0;
  40. }
  41. # if defined(__GNUC__) && __GNUC__>=2
  42. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  43. # endif
  44. /*
  45. * Use a weak reference to getauxval() so we can use it if it is available but
  46. * don't break the build if it is not.
  47. */
  48. # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
  49. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  50. # else
  51. static unsigned long (*getauxval) (unsigned long) = NULL;
  52. # endif
  53. /*
  54. * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  55. * AArch64 used AT_HWCAP.
  56. */
  57. # if defined(__arm__) || defined (__arm)
  58. # define HWCAP 16
  59. /* AT_HWCAP */
  60. # define HWCAP_NEON (1 << 12)
  61. # define HWCAP_CE 26
  62. /* AT_HWCAP2 */
  63. # define HWCAP_CE_AES (1 << 0)
  64. # define HWCAP_CE_PMULL (1 << 1)
  65. # define HWCAP_CE_SHA1 (1 << 2)
  66. # define HWCAP_CE_SHA256 (1 << 3)
  67. # elif defined(__aarch64__)
  68. # define HWCAP 16
  69. /* AT_HWCAP */
  70. # define HWCAP_NEON (1 << 1)
  71. # define HWCAP_CE HWCAP
  72. # define HWCAP_CE_AES (1 << 3)
  73. # define HWCAP_CE_PMULL (1 << 4)
  74. # define HWCAP_CE_SHA1 (1 << 5)
  75. # define HWCAP_CE_SHA256 (1 << 6)
  76. # endif
  77. void OPENSSL_cpuid_setup(void)
  78. {
  79. char *e;
  80. struct sigaction ill_oact, ill_act;
  81. sigset_t oset;
  82. static int trigger = 0;
  83. if (trigger)
  84. return;
  85. trigger = 1;
  86. if ((e = getenv("OPENSSL_armcap"))) {
  87. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  88. return;
  89. }
  90. sigfillset(&all_masked);
  91. sigdelset(&all_masked, SIGILL);
  92. sigdelset(&all_masked, SIGTRAP);
  93. sigdelset(&all_masked, SIGFPE);
  94. sigdelset(&all_masked, SIGBUS);
  95. sigdelset(&all_masked, SIGSEGV);
  96. OPENSSL_armcap_P = 0;
  97. memset(&ill_act, 0, sizeof(ill_act));
  98. ill_act.sa_handler = ill_handler;
  99. ill_act.sa_mask = all_masked;
  100. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  101. sigaction(SIGILL, &ill_act, &ill_oact);
  102. if (getauxval != NULL) {
  103. if (getauxval(HWCAP) & HWCAP_NEON) {
  104. unsigned long hwcap = getauxval(HWCAP_CE);
  105. OPENSSL_armcap_P |= ARMV7_NEON;
  106. if (hwcap & HWCAP_CE_AES)
  107. OPENSSL_armcap_P |= ARMV8_AES;
  108. if (hwcap & HWCAP_CE_PMULL)
  109. OPENSSL_armcap_P |= ARMV8_PMULL;
  110. if (hwcap & HWCAP_CE_SHA1)
  111. OPENSSL_armcap_P |= ARMV8_SHA1;
  112. if (hwcap & HWCAP_CE_SHA256)
  113. OPENSSL_armcap_P |= ARMV8_SHA256;
  114. }
  115. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  116. _armv7_neon_probe();
  117. OPENSSL_armcap_P |= ARMV7_NEON;
  118. if (sigsetjmp(ill_jmp, 1) == 0) {
  119. _armv8_pmull_probe();
  120. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  121. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  122. _armv8_aes_probe();
  123. OPENSSL_armcap_P |= ARMV8_AES;
  124. }
  125. if (sigsetjmp(ill_jmp, 1) == 0) {
  126. _armv8_sha1_probe();
  127. OPENSSL_armcap_P |= ARMV8_SHA1;
  128. }
  129. if (sigsetjmp(ill_jmp, 1) == 0) {
  130. _armv8_sha256_probe();
  131. OPENSSL_armcap_P |= ARMV8_SHA256;
  132. }
  133. }
  134. if (sigsetjmp(ill_jmp, 1) == 0) {
  135. _armv7_tick();
  136. OPENSSL_armcap_P |= ARMV7_TICK;
  137. }
  138. sigaction(SIGILL, &ill_oact, NULL);
  139. sigprocmask(SIG_SETMASK, &oset, NULL);
  140. }
  141. #endif