armcap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <setjmp.h>
  5. #include <signal.h>
  6. #include <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. /*
  42. * Use a weak reference to getauxval() so we can use it if it is available but
  43. * don't break the build if it is not.
  44. */
  45. # if defined(__GNUC__) && __GNUC__>=2
  46. void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
  47. extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
  48. # else
  49. static unsigned long (*getauxval) (unsigned long) = NULL;
  50. # endif
  51. /*
  52. * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
  53. * AArch64 used AT_HWCAP.
  54. */
  55. # if defined(__arm__) || defined (__arm)
  56. # define HWCAP 16
  57. /* AT_HWCAP */
  58. # define HWCAP_NEON (1 << 12)
  59. # define HWCAP_CE 26
  60. /* AT_HWCAP2 */
  61. # define HWCAP_CE_AES (1 << 0)
  62. # define HWCAP_CE_PMULL (1 << 1)
  63. # define HWCAP_CE_SHA1 (1 << 2)
  64. # define HWCAP_CE_SHA256 (1 << 3)
  65. # elif defined(__aarch64__)
  66. # define HWCAP 16
  67. /* AT_HWCAP */
  68. # define HWCAP_NEON (1 << 1)
  69. # define HWCAP_CE HWCAP
  70. # define HWCAP_CE_AES (1 << 3)
  71. # define HWCAP_CE_PMULL (1 << 4)
  72. # define HWCAP_CE_SHA1 (1 << 5)
  73. # define HWCAP_CE_SHA256 (1 << 6)
  74. # endif
  75. void OPENSSL_cpuid_setup(void)
  76. {
  77. char *e;
  78. struct sigaction ill_oact, ill_act;
  79. sigset_t oset;
  80. static int trigger = 0;
  81. if (trigger)
  82. return;
  83. trigger = 1;
  84. if ((e = getenv("OPENSSL_armcap"))) {
  85. OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
  86. return;
  87. }
  88. sigfillset(&all_masked);
  89. sigdelset(&all_masked, SIGILL);
  90. sigdelset(&all_masked, SIGTRAP);
  91. sigdelset(&all_masked, SIGFPE);
  92. sigdelset(&all_masked, SIGBUS);
  93. sigdelset(&all_masked, SIGSEGV);
  94. OPENSSL_armcap_P = 0;
  95. memset(&ill_act, 0, sizeof(ill_act));
  96. ill_act.sa_handler = ill_handler;
  97. ill_act.sa_mask = all_masked;
  98. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  99. sigaction(SIGILL, &ill_act, &ill_oact);
  100. if (getauxval != NULL) {
  101. if (getauxval(HWCAP) & HWCAP_NEON) {
  102. unsigned long hwcap = getauxval(HWCAP_CE);
  103. OPENSSL_armcap_P |= ARMV7_NEON;
  104. if (hwcap & HWCAP_CE_AES)
  105. OPENSSL_armcap_P |= ARMV8_AES;
  106. if (hwcap & HWCAP_CE_PMULL)
  107. OPENSSL_armcap_P |= ARMV8_PMULL;
  108. if (hwcap & HWCAP_CE_SHA1)
  109. OPENSSL_armcap_P |= ARMV8_SHA1;
  110. if (hwcap & HWCAP_CE_SHA256)
  111. OPENSSL_armcap_P |= ARMV8_SHA256;
  112. }
  113. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  114. _armv7_neon_probe();
  115. OPENSSL_armcap_P |= ARMV7_NEON;
  116. if (sigsetjmp(ill_jmp, 1) == 0) {
  117. _armv8_pmull_probe();
  118. OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
  119. } else if (sigsetjmp(ill_jmp, 1) == 0) {
  120. _armv8_aes_probe();
  121. OPENSSL_armcap_P |= ARMV8_AES;
  122. }
  123. if (sigsetjmp(ill_jmp, 1) == 0) {
  124. _armv8_sha1_probe();
  125. OPENSSL_armcap_P |= ARMV8_SHA1;
  126. }
  127. if (sigsetjmp(ill_jmp, 1) == 0) {
  128. _armv8_sha256_probe();
  129. OPENSSL_armcap_P |= ARMV8_SHA256;
  130. }
  131. }
  132. if (sigsetjmp(ill_jmp, 1) == 0) {
  133. _armv7_tick();
  134. OPENSSL_armcap_P |= ARMV7_TICK;
  135. }
  136. sigaction(SIGILL, &ill_oact, NULL);
  137. sigprocmask(SIG_SETMASK, &oset, NULL);
  138. }
  139. #endif