6 #include <openssl/crypto.h>
10 unsigned int OPENSSL_armcap_P = 0;
12 #if __ARM_MAX_ARCH__<7
13 void OPENSSL_cpuid_setup(void)
17 unsigned long OPENSSL_rdtsc(void)
22 static sigset_t all_masked;
24 static sigjmp_buf ill_jmp;
25 static void ill_handler(int sig)
27 siglongjmp(ill_jmp, sig);
31 * Following subroutines could have been inlined, but it's not all
32 * ARM compilers support inline assembler...
34 void _armv7_neon_probe(void);
35 void _armv8_aes_probe(void);
36 void _armv8_sha1_probe(void);
37 void _armv8_sha256_probe(void);
38 void _armv8_pmull_probe(void);
39 unsigned long _armv7_tick(void);
41 unsigned long OPENSSL_rdtsc(void)
43 if (OPENSSL_armcap_P & ARMV7_TICK)
49 # if defined(__GNUC__) && __GNUC__>=2
50 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
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.
56 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
57 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
59 static unsigned long (*getauxval) (unsigned long) = NULL;
63 * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
64 * AArch64 used AT_HWCAP.
66 # if defined(__arm__) || defined (__arm)
69 # define HWCAP_NEON (1 << 12)
73 # define HWCAP_CE_AES (1 << 0)
74 # define HWCAP_CE_PMULL (1 << 1)
75 # define HWCAP_CE_SHA1 (1 << 2)
76 # define HWCAP_CE_SHA256 (1 << 3)
77 # elif defined(__aarch64__)
80 # define HWCAP_NEON (1 << 1)
82 # define HWCAP_CE HWCAP
83 # define HWCAP_CE_AES (1 << 3)
84 # define HWCAP_CE_PMULL (1 << 4)
85 # define HWCAP_CE_SHA1 (1 << 5)
86 # define HWCAP_CE_SHA256 (1 << 6)
89 void OPENSSL_cpuid_setup(void)
92 struct sigaction ill_oact, ill_act;
94 static int trigger = 0;
100 if ((e = getenv("OPENSSL_armcap"))) {
101 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
105 sigfillset(&all_masked);
106 sigdelset(&all_masked, SIGILL);
107 sigdelset(&all_masked, SIGTRAP);
108 sigdelset(&all_masked, SIGFPE);
109 sigdelset(&all_masked, SIGBUS);
110 sigdelset(&all_masked, SIGSEGV);
112 OPENSSL_armcap_P = 0;
114 memset(&ill_act, 0, sizeof(ill_act));
115 ill_act.sa_handler = ill_handler;
116 ill_act.sa_mask = all_masked;
118 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
119 sigaction(SIGILL, &ill_act, &ill_oact);
121 if (getauxval != NULL) {
122 if (getauxval(HWCAP) & HWCAP_NEON) {
123 unsigned long hwcap = getauxval(HWCAP_CE);
125 OPENSSL_armcap_P |= ARMV7_NEON;
127 if (hwcap & HWCAP_CE_AES)
128 OPENSSL_armcap_P |= ARMV8_AES;
130 if (hwcap & HWCAP_CE_PMULL)
131 OPENSSL_armcap_P |= ARMV8_PMULL;
133 if (hwcap & HWCAP_CE_SHA1)
134 OPENSSL_armcap_P |= ARMV8_SHA1;
136 if (hwcap & HWCAP_CE_SHA256)
137 OPENSSL_armcap_P |= ARMV8_SHA256;
139 } else if (sigsetjmp(ill_jmp, 1) == 0) {
141 OPENSSL_armcap_P |= ARMV7_NEON;
142 if (sigsetjmp(ill_jmp, 1) == 0) {
143 _armv8_pmull_probe();
144 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
145 } else if (sigsetjmp(ill_jmp, 1) == 0) {
147 OPENSSL_armcap_P |= ARMV8_AES;
149 if (sigsetjmp(ill_jmp, 1) == 0) {
151 OPENSSL_armcap_P |= ARMV8_SHA1;
153 if (sigsetjmp(ill_jmp, 1) == 0) {
154 _armv8_sha256_probe();
155 OPENSSL_armcap_P |= ARMV8_SHA256;
158 if (sigsetjmp(ill_jmp, 1) == 0) {
160 OPENSSL_armcap_P |= ARMV7_TICK;
163 sigaction(SIGILL, &ill_oact, NULL);
164 sigprocmask(SIG_SETMASK, &oset, NULL);