]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/armcap.c
2 * Copyright 2011-2021 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
74 # if defined(__FreeBSD__)
75 # include <sys/param.h>
76 # if __FreeBSD_version >= 1200000
77 # include <sys/auxv.h>
78 # define OSSL_IMPLEMENT_GETAUXVAL
80 static unsigned long getauxval(unsigned long key
)
82 unsigned long val
= 0ul;
84 if (elf_aux_info((int)key
, &val
, sizeof(val
)) != 0)
93 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
94 * AArch64 used AT_HWCAP.
96 # if defined(__arm__) || defined (__arm)
99 # define HWCAP_NEON (1 << 12)
103 # define HWCAP_CE_AES (1 << 0)
104 # define HWCAP_CE_PMULL (1 << 1)
105 # define HWCAP_CE_SHA1 (1 << 2)
106 # define HWCAP_CE_SHA256 (1 << 3)
107 # elif defined(__aarch64__)
110 # define HWCAP_NEON (1 << 1)
112 # define HWCAP_CE HWCAP
113 # define HWCAP_CE_AES (1 << 3)
114 # define HWCAP_CE_PMULL (1 << 4)
115 # define HWCAP_CE_SHA1 (1 << 5)
116 # define HWCAP_CE_SHA256 (1 << 6)
117 # define HWCAP_CPUID (1 << 11)
118 # define HWCAP_CE_SHA512 (1 << 21)
121 void OPENSSL_cpuid_setup(void)
124 struct sigaction ill_oact
, ill_act
;
126 static int trigger
= 0;
132 if ((e
= getenv("OPENSSL_armcap"))) {
133 OPENSSL_armcap_P
= (unsigned int)strtoul(e
, NULL
, 0);
137 # if defined(__APPLE__) && !defined(__aarch64__)
139 * Capability probing by catching SIGILL appears to be problematic
140 * on iOS. But since Apple universe is "monocultural", it's actually
141 * possible to simply set pre-defined processor capability mask.
144 OPENSSL_armcap_P
= ARMV7_NEON
;
148 * One could do same even for __aarch64__ iOS builds. It's not done
149 * exclusively for reasons of keeping code unified across platforms.
150 * Unified code works because it never triggers SIGILL on Apple
155 OPENSSL_armcap_P
= 0;
157 # ifdef OSSL_IMPLEMENT_GETAUXVAL
158 if (getauxval(HWCAP
) & HWCAP_NEON
) {
159 unsigned long hwcap
= getauxval(HWCAP_CE
);
161 OPENSSL_armcap_P
|= ARMV7_NEON
;
163 if (hwcap
& HWCAP_CE_AES
)
164 OPENSSL_armcap_P
|= ARMV8_AES
;
166 if (hwcap
& HWCAP_CE_PMULL
)
167 OPENSSL_armcap_P
|= ARMV8_PMULL
;
169 if (hwcap
& HWCAP_CE_SHA1
)
170 OPENSSL_armcap_P
|= ARMV8_SHA1
;
172 if (hwcap
& HWCAP_CE_SHA256
)
173 OPENSSL_armcap_P
|= ARMV8_SHA256
;
176 if (hwcap
& HWCAP_CE_SHA512
)
177 OPENSSL_armcap_P
|= ARMV8_SHA512
;
179 if (hwcap
& HWCAP_CPUID
)
180 OPENSSL_armcap_P
|= ARMV8_CPUID
;
185 sigfillset(&all_masked
);
186 sigdelset(&all_masked
, SIGILL
);
187 sigdelset(&all_masked
, SIGTRAP
);
188 sigdelset(&all_masked
, SIGFPE
);
189 sigdelset(&all_masked
, SIGBUS
);
190 sigdelset(&all_masked
, SIGSEGV
);
192 memset(&ill_act
, 0, sizeof(ill_act
));
193 ill_act
.sa_handler
= ill_handler
;
194 ill_act
.sa_mask
= all_masked
;
196 sigprocmask(SIG_SETMASK
, &ill_act
.sa_mask
, &oset
);
197 sigaction(SIGILL
, &ill_act
, &ill_oact
);
199 /* If we used getauxval, we already have all the values */
200 # ifndef OSSL_IMPLEMENT_GETAUXVAL
201 if (sigsetjmp(ill_jmp
, 1) == 0) {
203 OPENSSL_armcap_P
|= ARMV7_NEON
;
204 if (sigsetjmp(ill_jmp
, 1) == 0) {
205 _armv8_pmull_probe();
206 OPENSSL_armcap_P
|= ARMV8_PMULL
| ARMV8_AES
;
207 } else if (sigsetjmp(ill_jmp
, 1) == 0) {
209 OPENSSL_armcap_P
|= ARMV8_AES
;
211 if (sigsetjmp(ill_jmp
, 1) == 0) {
213 OPENSSL_armcap_P
|= ARMV8_SHA1
;
215 if (sigsetjmp(ill_jmp
, 1) == 0) {
216 _armv8_sha256_probe();
217 OPENSSL_armcap_P
|= ARMV8_SHA256
;
219 # if defined(__aarch64__) && !defined(__APPLE__)
220 if (sigsetjmp(ill_jmp
, 1) == 0) {
221 _armv8_sha512_probe();
222 OPENSSL_armcap_P
|= ARMV8_SHA512
;
228 /* Things that getauxval didn't tell us */
229 if (sigsetjmp(ill_jmp
, 1) == 0) {
231 OPENSSL_armcap_P
|= ARMV7_TICK
;
234 sigaction(SIGILL
, &ill_oact
, NULL
);
235 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
238 if (OPENSSL_armcap_P
& ARMV8_CPUID
)
239 OPENSSL_arm_midr
= _armv8_cpuid_probe();