2 * Copyright 2011-2018 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>
16 #include "internal/cryptlib.h"
20 unsigned int OPENSSL_armcap_P = 0;
22 #if __ARM_MAX_ARCH__<7
23 void OPENSSL_cpuid_setup(void)
27 uint32_t OPENSSL_rdtsc(void)
32 static sigset_t all_masked;
34 static sigjmp_buf ill_jmp;
35 static void ill_handler(int sig)
37 siglongjmp(ill_jmp, sig);
41 * Following subroutines could have been inlined, but it's not all
42 * ARM compilers support inline assembler...
44 void _armv7_neon_probe(void);
45 void _armv8_aes_probe(void);
46 void _armv8_sha1_probe(void);
47 void _armv8_sha256_probe(void);
48 void _armv8_pmull_probe(void);
50 void _armv8_sha512_probe(void);
52 uint32_t _armv7_tick(void);
54 uint32_t OPENSSL_rdtsc(void)
56 if (OPENSSL_armcap_P & ARMV7_TICK)
62 # if defined(__GNUC__) && __GNUC__>=2
63 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
66 * Use a weak reference to getauxval() so we can use it if it is available but
67 * don't break the build if it is not.
69 # if defined(__GNUC__) && __GNUC__>=2 && defined(__ELF__)
70 extern unsigned long getauxval(unsigned long type) __attribute__ ((weak));
72 static unsigned long (*getauxval) (unsigned long) = NULL;
76 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
77 * AArch64 used AT_HWCAP.
79 # if defined(__arm__) || defined (__arm)
82 # define HWCAP_NEON (1 << 12)
86 # define HWCAP_CE_AES (1 << 0)
87 # define HWCAP_CE_PMULL (1 << 1)
88 # define HWCAP_CE_SHA1 (1 << 2)
89 # define HWCAP_CE_SHA256 (1 << 3)
90 # elif defined(__aarch64__)
93 # define HWCAP_NEON (1 << 1)
95 # define HWCAP_CE HWCAP
96 # define HWCAP_CE_AES (1 << 3)
97 # define HWCAP_CE_PMULL (1 << 4)
98 # define HWCAP_CE_SHA1 (1 << 5)
99 # define HWCAP_CE_SHA256 (1 << 6)
100 # define HWCAP_CE_SHA512 (1 << 21)
103 void OPENSSL_cpuid_setup(void)
106 struct sigaction ill_oact, ill_act;
108 static int trigger = 0;
114 if ((e = getenv("OPENSSL_armcap"))) {
115 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
119 # if defined(__APPLE__) && !defined(__aarch64__)
121 * Capability probing by catching SIGILL appears to be problematic
122 * on iOS. But since Apple universe is "monocultural", it's actually
123 * possible to simply set pre-defined processor capability mask.
126 OPENSSL_armcap_P = ARMV7_NEON;
130 * One could do same even for __aarch64__ iOS builds. It's not done
131 * exclusively for reasons of keeping code unified across platforms.
132 * Unified code works because it never triggers SIGILL on Apple
137 sigfillset(&all_masked);
138 sigdelset(&all_masked, SIGILL);
139 sigdelset(&all_masked, SIGTRAP);
140 sigdelset(&all_masked, SIGFPE);
141 sigdelset(&all_masked, SIGBUS);
142 sigdelset(&all_masked, SIGSEGV);
144 OPENSSL_armcap_P = 0;
146 memset(&ill_act, 0, sizeof(ill_act));
147 ill_act.sa_handler = ill_handler;
148 ill_act.sa_mask = all_masked;
150 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
151 sigaction(SIGILL, &ill_act, &ill_oact);
153 if (getauxval != NULL) {
154 if (getauxval(HWCAP) & HWCAP_NEON) {
155 unsigned long hwcap = getauxval(HWCAP_CE);
157 OPENSSL_armcap_P |= ARMV7_NEON;
159 if (hwcap & HWCAP_CE_AES)
160 OPENSSL_armcap_P |= ARMV8_AES;
162 if (hwcap & HWCAP_CE_PMULL)
163 OPENSSL_armcap_P |= ARMV8_PMULL;
165 if (hwcap & HWCAP_CE_SHA1)
166 OPENSSL_armcap_P |= ARMV8_SHA1;
168 if (hwcap & HWCAP_CE_SHA256)
169 OPENSSL_armcap_P |= ARMV8_SHA256;
172 if (hwcap & HWCAP_CE_SHA512)
173 OPENSSL_armcap_P |= ARMV8_SHA512;
176 } else if (sigsetjmp(ill_jmp, 1) == 0) {
178 OPENSSL_armcap_P |= ARMV7_NEON;
179 if (sigsetjmp(ill_jmp, 1) == 0) {
180 _armv8_pmull_probe();
181 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
182 } else if (sigsetjmp(ill_jmp, 1) == 0) {
184 OPENSSL_armcap_P |= ARMV8_AES;
186 if (sigsetjmp(ill_jmp, 1) == 0) {
188 OPENSSL_armcap_P |= ARMV8_SHA1;
190 if (sigsetjmp(ill_jmp, 1) == 0) {
191 _armv8_sha256_probe();
192 OPENSSL_armcap_P |= ARMV8_SHA256;
194 # if defined(__aarch64__) && !defined(__APPLE__)
195 if (sigsetjmp(ill_jmp, 1) == 0) {
196 _armv8_sha512_probe();
197 OPENSSL_armcap_P |= ARMV8_SHA512;
201 if (sigsetjmp(ill_jmp, 1) == 0) {
203 OPENSSL_armcap_P |= ARMV7_TICK;
206 sigaction(SIGILL, &ill_oact, NULL);
207 sigprocmask(SIG_SETMASK, &oset, NULL);