2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
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
15 #include <openssl/crypto.h>
19 unsigned int OPENSSL_armcap_P = 0;
21 #if __ARM_MAX_ARCH__<7
22 void OPENSSL_cpuid_setup(void)
26 unsigned long OPENSSL_rdtsc(void)
31 static sigset_t all_masked;
33 static sigjmp_buf ill_jmp;
34 static void ill_handler(int sig)
36 siglongjmp(ill_jmp, sig);
40 * Following subroutines could have been inlined, but it's not all
41 * ARM compilers support inline assembler...
43 void _armv7_neon_probe(void);
44 void _armv8_aes_probe(void);
45 void _armv8_sha1_probe(void);
46 void _armv8_sha256_probe(void);
47 void _armv8_pmull_probe(void);
48 unsigned long _armv7_tick(void);
50 unsigned long OPENSSL_rdtsc(void)
52 if (OPENSSL_armcap_P & ARMV7_TICK)
58 # if defined(__GNUC__) && __GNUC__>=2
59 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
62 * Use a weak reference to getauxval() so we can use it if it is available but
63 * don't break the build if it is not.
65 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
66 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
68 static unsigned long (*getauxval) (unsigned long) = NULL;
72 * ARM puts the the feature bits for Crypto Extensions in AT_HWCAP2, whereas
73 * AArch64 used AT_HWCAP.
75 # if defined(__arm__) || defined (__arm)
78 # define HWCAP_NEON (1 << 12)
82 # define HWCAP_CE_AES (1 << 0)
83 # define HWCAP_CE_PMULL (1 << 1)
84 # define HWCAP_CE_SHA1 (1 << 2)
85 # define HWCAP_CE_SHA256 (1 << 3)
86 # elif defined(__aarch64__)
89 # define HWCAP_NEON (1 << 1)
91 # define HWCAP_CE HWCAP
92 # define HWCAP_CE_AES (1 << 3)
93 # define HWCAP_CE_PMULL (1 << 4)
94 # define HWCAP_CE_SHA1 (1 << 5)
95 # define HWCAP_CE_SHA256 (1 << 6)
98 void OPENSSL_cpuid_setup(void)
101 struct sigaction ill_oact, ill_act;
103 static int trigger = 0;
109 if ((e = getenv("OPENSSL_armcap"))) {
110 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
114 sigfillset(&all_masked);
115 sigdelset(&all_masked, SIGILL);
116 sigdelset(&all_masked, SIGTRAP);
117 sigdelset(&all_masked, SIGFPE);
118 sigdelset(&all_masked, SIGBUS);
119 sigdelset(&all_masked, SIGSEGV);
121 OPENSSL_armcap_P = 0;
123 memset(&ill_act, 0, sizeof(ill_act));
124 ill_act.sa_handler = ill_handler;
125 ill_act.sa_mask = all_masked;
127 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
128 sigaction(SIGILL, &ill_act, &ill_oact);
130 if (getauxval != NULL) {
131 if (getauxval(HWCAP) & HWCAP_NEON) {
132 unsigned long hwcap = getauxval(HWCAP_CE);
134 OPENSSL_armcap_P |= ARMV7_NEON;
136 if (hwcap & HWCAP_CE_AES)
137 OPENSSL_armcap_P |= ARMV8_AES;
139 if (hwcap & HWCAP_CE_PMULL)
140 OPENSSL_armcap_P |= ARMV8_PMULL;
142 if (hwcap & HWCAP_CE_SHA1)
143 OPENSSL_armcap_P |= ARMV8_SHA1;
145 if (hwcap & HWCAP_CE_SHA256)
146 OPENSSL_armcap_P |= ARMV8_SHA256;
148 } else if (sigsetjmp(ill_jmp, 1) == 0) {
150 OPENSSL_armcap_P |= ARMV7_NEON;
151 if (sigsetjmp(ill_jmp, 1) == 0) {
152 _armv8_pmull_probe();
153 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
154 } else if (sigsetjmp(ill_jmp, 1) == 0) {
156 OPENSSL_armcap_P |= ARMV8_AES;
158 if (sigsetjmp(ill_jmp, 1) == 0) {
160 OPENSSL_armcap_P |= ARMV8_SHA1;
162 if (sigsetjmp(ill_jmp, 1) == 0) {
163 _armv8_sha256_probe();
164 OPENSSL_armcap_P |= ARMV8_SHA256;
167 if (sigsetjmp(ill_jmp, 1) == 0) {
169 OPENSSL_armcap_P |= ARMV7_TICK;
172 sigaction(SIGILL, &ill_oact, NULL);
173 sigprocmask(SIG_SETMASK, &oset, NULL);