2 * Copyright 2011-2019 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 __attribute__ ((visibility("hidden")))
21 unsigned int OPENSSL_armcap_P = 0;
23 #if __ARM_MAX_ARCH__<7
24 void OPENSSL_cpuid_setup(void)
28 uint32_t OPENSSL_rdtsc(void)
33 static sigset_t all_masked;
35 static sigjmp_buf ill_jmp;
36 static void ill_handler(int sig)
38 siglongjmp(ill_jmp, sig);
42 * Following subroutines could have been inlined, but it's not all
43 * ARM compilers support inline assembler...
45 void _armv7_neon_probe(void);
46 void _armv8_aes_probe(void);
47 void _armv8_sha1_probe(void);
48 void _armv8_sha256_probe(void);
49 void _armv8_pmull_probe(void);
51 void _armv8_sha512_probe(void);
53 uint32_t _armv7_tick(void);
55 uint32_t OPENSSL_rdtsc(void)
57 if (OPENSSL_armcap_P & ARMV7_TICK)
63 # if defined(__GNUC__) && __GNUC__>=2
64 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
67 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
68 # if __GLIBC_PREREQ(2, 16)
69 # include <sys/auxv.h>
70 # define OSSL_IMPLEMENT_GETAUXVAL
75 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
76 * AArch64 used AT_HWCAP.
78 # if defined(__arm__) || defined (__arm)
81 # define HWCAP_NEON (1 << 12)
85 # define HWCAP_CE_AES (1 << 0)
86 # define HWCAP_CE_PMULL (1 << 1)
87 # define HWCAP_CE_SHA1 (1 << 2)
88 # define HWCAP_CE_SHA256 (1 << 3)
89 # elif defined(__aarch64__)
92 # define HWCAP_NEON (1 << 1)
94 # define HWCAP_CE HWCAP
95 # define HWCAP_CE_AES (1 << 3)
96 # define HWCAP_CE_PMULL (1 << 4)
97 # define HWCAP_CE_SHA1 (1 << 5)
98 # define HWCAP_CE_SHA256 (1 << 6)
99 # define HWCAP_CE_SHA512 (1 << 21)
102 void OPENSSL_cpuid_setup(void)
105 struct sigaction ill_oact, ill_act;
107 static int trigger = 0;
113 if ((e = getenv("OPENSSL_armcap"))) {
114 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
118 # if defined(__APPLE__) && !defined(__aarch64__)
120 * Capability probing by catching SIGILL appears to be problematic
121 * on iOS. But since Apple universe is "monocultural", it's actually
122 * possible to simply set pre-defined processor capability mask.
125 OPENSSL_armcap_P = ARMV7_NEON;
129 * One could do same even for __aarch64__ iOS builds. It's not done
130 * exclusively for reasons of keeping code unified across platforms.
131 * Unified code works because it never triggers SIGILL on Apple
136 OPENSSL_armcap_P = 0;
138 # ifdef OSSL_IMPLEMENT_GETAUXVAL
139 if (getauxval(HWCAP) & HWCAP_NEON) {
140 unsigned long hwcap = getauxval(HWCAP_CE);
142 OPENSSL_armcap_P |= ARMV7_NEON;
144 if (hwcap & HWCAP_CE_AES)
145 OPENSSL_armcap_P |= ARMV8_AES;
147 if (hwcap & HWCAP_CE_PMULL)
148 OPENSSL_armcap_P |= ARMV8_PMULL;
150 if (hwcap & HWCAP_CE_SHA1)
151 OPENSSL_armcap_P |= ARMV8_SHA1;
153 if (hwcap & HWCAP_CE_SHA256)
154 OPENSSL_armcap_P |= ARMV8_SHA256;
157 if (hwcap & HWCAP_CE_SHA512)
158 OPENSSL_armcap_P |= ARMV8_SHA512;
163 sigfillset(&all_masked);
164 sigdelset(&all_masked, SIGILL);
165 sigdelset(&all_masked, SIGTRAP);
166 sigdelset(&all_masked, SIGFPE);
167 sigdelset(&all_masked, SIGBUS);
168 sigdelset(&all_masked, SIGSEGV);
170 memset(&ill_act, 0, sizeof(ill_act));
171 ill_act.sa_handler = ill_handler;
172 ill_act.sa_mask = all_masked;
174 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
175 sigaction(SIGILL, &ill_act, &ill_oact);
177 /* If we used getauxval, we already have all the values */
178 # ifndef OSSL_IMPLEMENT_GETAUXVAL
179 if (sigsetjmp(ill_jmp, 1) == 0) {
181 OPENSSL_armcap_P |= ARMV7_NEON;
182 if (sigsetjmp(ill_jmp, 1) == 0) {
183 _armv8_pmull_probe();
184 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
185 } else if (sigsetjmp(ill_jmp, 1) == 0) {
187 OPENSSL_armcap_P |= ARMV8_AES;
189 if (sigsetjmp(ill_jmp, 1) == 0) {
191 OPENSSL_armcap_P |= ARMV8_SHA1;
193 if (sigsetjmp(ill_jmp, 1) == 0) {
194 _armv8_sha256_probe();
195 OPENSSL_armcap_P |= ARMV8_SHA256;
197 # if defined(__aarch64__) && !defined(__APPLE__)
198 if (sigsetjmp(ill_jmp, 1) == 0) {
199 _armv8_sha512_probe();
200 OPENSSL_armcap_P |= ARMV8_SHA512;
206 /* Things that getauxval didn't tell us */
207 if (sigsetjmp(ill_jmp, 1) == 0) {
209 OPENSSL_armcap_P |= ARMV7_TICK;
212 sigaction(SIGILL, &ill_oact, NULL);
213 sigprocmask(SIG_SETMASK, &oset, NULL);