2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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;
21 unsigned int OPENSSL_arm_midr = 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);
52 unsigned int _armv8_cpuid_probe(void);
54 uint32_t _armv7_tick(void);
56 uint32_t OPENSSL_rdtsc(void)
58 if (OPENSSL_armcap_P & ARMV7_TICK)
64 # if defined(__GNUC__) && __GNUC__>=2
65 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
68 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
69 # if __GLIBC_PREREQ(2, 16)
70 # include <sys/auxv.h>
71 # define OSSL_IMPLEMENT_GETAUXVAL
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_CPUID (1 << 11)
101 # define HWCAP_CE_SHA512 (1 << 21)
104 void OPENSSL_cpuid_setup(void)
107 struct sigaction ill_oact, ill_act;
109 static int trigger = 0;
115 if ((e = getenv("OPENSSL_armcap"))) {
116 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
120 # if defined(__APPLE__) && !defined(__aarch64__)
122 * Capability probing by catching SIGILL appears to be problematic
123 * on iOS. But since Apple universe is "monocultural", it's actually
124 * possible to simply set pre-defined processor capability mask.
127 OPENSSL_armcap_P = ARMV7_NEON;
131 * One could do same even for __aarch64__ iOS builds. It's not done
132 * exclusively for reasons of keeping code unified across platforms.
133 * Unified code works because it never triggers SIGILL on Apple
138 OPENSSL_armcap_P = 0;
140 # ifdef OSSL_IMPLEMENT_GETAUXVAL
141 if (getauxval(HWCAP) & HWCAP_NEON) {
142 unsigned long hwcap = getauxval(HWCAP_CE);
144 OPENSSL_armcap_P |= ARMV7_NEON;
146 if (hwcap & HWCAP_CE_AES)
147 OPENSSL_armcap_P |= ARMV8_AES;
149 if (hwcap & HWCAP_CE_PMULL)
150 OPENSSL_armcap_P |= ARMV8_PMULL;
152 if (hwcap & HWCAP_CE_SHA1)
153 OPENSSL_armcap_P |= ARMV8_SHA1;
155 if (hwcap & HWCAP_CE_SHA256)
156 OPENSSL_armcap_P |= ARMV8_SHA256;
159 if (hwcap & HWCAP_CE_SHA512)
160 OPENSSL_armcap_P |= ARMV8_SHA512;
162 if (hwcap & HWCAP_CPUID)
163 OPENSSL_armcap_P |= ARMV8_CPUID;
168 sigfillset(&all_masked);
169 sigdelset(&all_masked, SIGILL);
170 sigdelset(&all_masked, SIGTRAP);
171 sigdelset(&all_masked, SIGFPE);
172 sigdelset(&all_masked, SIGBUS);
173 sigdelset(&all_masked, SIGSEGV);
175 memset(&ill_act, 0, sizeof(ill_act));
176 ill_act.sa_handler = ill_handler;
177 ill_act.sa_mask = all_masked;
179 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
180 sigaction(SIGILL, &ill_act, &ill_oact);
182 /* If we used getauxval, we already have all the values */
183 # ifndef OSSL_IMPLEMENT_GETAUXVAL
184 if (sigsetjmp(ill_jmp, 1) == 0) {
186 OPENSSL_armcap_P |= ARMV7_NEON;
187 if (sigsetjmp(ill_jmp, 1) == 0) {
188 _armv8_pmull_probe();
189 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
190 } else if (sigsetjmp(ill_jmp, 1) == 0) {
192 OPENSSL_armcap_P |= ARMV8_AES;
194 if (sigsetjmp(ill_jmp, 1) == 0) {
196 OPENSSL_armcap_P |= ARMV8_SHA1;
198 if (sigsetjmp(ill_jmp, 1) == 0) {
199 _armv8_sha256_probe();
200 OPENSSL_armcap_P |= ARMV8_SHA256;
202 # if defined(__aarch64__) && !defined(__APPLE__)
203 if (sigsetjmp(ill_jmp, 1) == 0) {
204 _armv8_sha512_probe();
205 OPENSSL_armcap_P |= ARMV8_SHA512;
211 /* Things that getauxval didn't tell us */
212 if (sigsetjmp(ill_jmp, 1) == 0) {
214 OPENSSL_armcap_P |= ARMV7_TICK;
217 sigaction(SIGILL, &ill_oact, NULL);
218 sigprocmask(SIG_SETMASK, &oset, NULL);
221 if (OPENSSL_armcap_P & ARMV8_CPUID)
222 OPENSSL_arm_midr = _armv8_cpuid_probe();