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 # if defined(__APPLE__) && !defined(__aarch64__)
116 * Capability probing by catching SIGILL appears to be problematic
117 * on iOS. But since Apple universe is "monocultural", it's actually
118 * possible to simply set pre-defined processor capability mask.
121 OPENSSL_armcap_P = ARMV7_NEON;
125 * One could do same even for __aarch64__ iOS builds. It's not done
126 * exclusively for reasons of keeping code unified across platforms.
127 * Unified code works because it never triggers SIGILL on Apple
132 sigfillset(&all_masked);
133 sigdelset(&all_masked, SIGILL);
134 sigdelset(&all_masked, SIGTRAP);
135 sigdelset(&all_masked, SIGFPE);
136 sigdelset(&all_masked, SIGBUS);
137 sigdelset(&all_masked, SIGSEGV);
139 OPENSSL_armcap_P = 0;
141 memset(&ill_act, 0, sizeof(ill_act));
142 ill_act.sa_handler = ill_handler;
143 ill_act.sa_mask = all_masked;
145 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
146 sigaction(SIGILL, &ill_act, &ill_oact);
148 if (getauxval != NULL) {
149 if (getauxval(HWCAP) & HWCAP_NEON) {
150 unsigned long hwcap = getauxval(HWCAP_CE);
152 OPENSSL_armcap_P |= ARMV7_NEON;
154 if (hwcap & HWCAP_CE_AES)
155 OPENSSL_armcap_P |= ARMV8_AES;
157 if (hwcap & HWCAP_CE_PMULL)
158 OPENSSL_armcap_P |= ARMV8_PMULL;
160 if (hwcap & HWCAP_CE_SHA1)
161 OPENSSL_armcap_P |= ARMV8_SHA1;
163 if (hwcap & HWCAP_CE_SHA256)
164 OPENSSL_armcap_P |= ARMV8_SHA256;
166 } else if (sigsetjmp(ill_jmp, 1) == 0) {
168 OPENSSL_armcap_P |= ARMV7_NEON;
169 if (sigsetjmp(ill_jmp, 1) == 0) {
170 _armv8_pmull_probe();
171 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
172 } else if (sigsetjmp(ill_jmp, 1) == 0) {
174 OPENSSL_armcap_P |= ARMV8_AES;
176 if (sigsetjmp(ill_jmp, 1) == 0) {
178 OPENSSL_armcap_P |= ARMV8_SHA1;
180 if (sigsetjmp(ill_jmp, 1) == 0) {
181 _armv8_sha256_probe();
182 OPENSSL_armcap_P |= ARMV8_SHA256;
185 if (sigsetjmp(ill_jmp, 1) == 0) {
187 OPENSSL_armcap_P |= ARMV7_TICK;
190 sigaction(SIGILL, &ill_oact, NULL);
191 sigprocmask(SIG_SETMASK, &oset, NULL);