1 /*
2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <setjmp.h>
14 #include <signal.h>
15 #include <openssl/crypto.h>
16 #ifdef __APPLE__
17 #include <sys/sysctl.h>
18 #endif
19 #include "internal/cryptlib.h"
20
21 #include "arm_arch.h"
22
23 unsigned int OPENSSL_armcap_P = 0;
24 unsigned int OPENSSL_arm_midr = 0;
25 unsigned int OPENSSL_armv8_rsa_neonized = 0;
26
27 #if __ARM_MAX_ARCH__<7
OPENSSL_cpuid_setup(void)28 void OPENSSL_cpuid_setup(void)
29 {
30 }
31
OPENSSL_rdtsc(void)32 uint32_t OPENSSL_rdtsc(void)
33 {
34 return 0;
35 }
36 #else
37 static sigset_t all_masked;
38
39 static sigjmp_buf ill_jmp;
ill_handler(int sig)40 static void ill_handler(int sig)
41 {
42 siglongjmp(ill_jmp, sig);
43 }
44
45 /*
46 * Following subroutines could have been inlined, but it's not all
47 * ARM compilers support inline assembler...
48 */
49 void _armv7_neon_probe(void);
50 void _armv8_aes_probe(void);
51 void _armv8_sha1_probe(void);
52 void _armv8_sha256_probe(void);
53 void _armv8_pmull_probe(void);
54 # ifdef __aarch64__
55 void _armv8_sha512_probe(void);
56 unsigned int _armv8_cpuid_probe(void);
57 # endif
58 uint32_t _armv7_tick(void);
59
OPENSSL_rdtsc(void)60 uint32_t OPENSSL_rdtsc(void)
61 {
62 if (OPENSSL_armcap_P & ARMV7_TICK)
63 return _armv7_tick();
64 else
65 return 0;
66 }
67
68 # if defined(__GNUC__) && __GNUC__>=2
69 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
70 # endif
71
72 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
73 # if __GLIBC_PREREQ(2, 16)
74 # include <sys/auxv.h>
75 # define OSSL_IMPLEMENT_GETAUXVAL
76 # endif
77 # elif defined(__ANDROID_API__)
78 /* see https://developer.android.google.cn/ndk/guides/cpu-features */
79 # if __ANDROID_API__ >= 18
80 # include <sys/auxv.h>
81 # define OSSL_IMPLEMENT_GETAUXVAL
82 # endif
83 # endif
84 # if defined(__FreeBSD__)
85 # include <sys/param.h>
86 # if __FreeBSD_version >= 1200000
87 # include <sys/auxv.h>
88 # define OSSL_IMPLEMENT_GETAUXVAL
89
getauxval(unsigned long key)90 static unsigned long getauxval(unsigned long key)
91 {
92 unsigned long val = 0ul;
93
94 if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
95 return 0ul;
96
97 return val;
98 }
99 # endif
100 # endif
101
102 /*
103 * Android: according to https://developer.android.com/ndk/guides/cpu-features,
104 * getauxval is supported starting with API level 18
105 */
106 # if defined(__ANDROID__) && defined(__ANDROID_API__) && __ANDROID_API__ >= 18
107 # include <sys/auxv.h>
108 # define OSSL_IMPLEMENT_GETAUXVAL
109 # endif
110
111 /*
112 * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
113 * AArch64 used AT_HWCAP.
114 */
115 # if defined(__arm__) || defined (__arm)
116 # define HWCAP 16
117 /* AT_HWCAP */
118 # define HWCAP_NEON (1 << 12)
119
120 # define HWCAP_CE 26
121 /* AT_HWCAP2 */
122 # define HWCAP_CE_AES (1 << 0)
123 # define HWCAP_CE_PMULL (1 << 1)
124 # define HWCAP_CE_SHA1 (1 << 2)
125 # define HWCAP_CE_SHA256 (1 << 3)
126 # elif defined(__aarch64__)
127 # define HWCAP 16
128 /* AT_HWCAP */
129 # define HWCAP_NEON (1 << 1)
130
131 # define HWCAP_CE HWCAP
132 # define HWCAP_CE_AES (1 << 3)
133 # define HWCAP_CE_PMULL (1 << 4)
134 # define HWCAP_CE_SHA1 (1 << 5)
135 # define HWCAP_CE_SHA256 (1 << 6)
136 # define HWCAP_CPUID (1 << 11)
137 # define HWCAP_CE_SHA512 (1 << 21)
138 # endif
139
OPENSSL_cpuid_setup(void)140 void OPENSSL_cpuid_setup(void)
141 {
142 const char *e;
143 struct sigaction ill_oact, ill_act;
144 sigset_t oset;
145 static int trigger = 0;
146
147 if (trigger)
148 return;
149 trigger = 1;
150
151 OPENSSL_armcap_P = 0;
152
153 if ((e = getenv("OPENSSL_armcap"))) {
154 OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
155 return;
156 }
157
158 # if defined(__APPLE__)
159 # if !defined(__aarch64__)
160 /*
161 * Capability probing by catching SIGILL appears to be problematic
162 * on iOS. But since Apple universe is "monocultural", it's actually
163 * possible to simply set pre-defined processor capability mask.
164 */
165 if (1) {
166 OPENSSL_armcap_P = ARMV7_NEON;
167 return;
168 }
169 /*
170 * One could do same even for __aarch64__ iOS builds. It's not done
171 * exclusively for reasons of keeping code unified across platforms.
172 * Unified code works because it never triggers SIGILL on Apple
173 * devices...
174 */
175 # else
176 {
177 unsigned int sha512;
178 size_t len = sizeof(sha512);
179
180 if (sysctlbyname("hw.optional.armv8_2_sha512", &sha512, &len, NULL, 0) == 0 && sha512 == 1)
181 OPENSSL_armcap_P |= ARMV8_SHA512;
182 }
183 # endif
184 # endif
185
186 # ifdef OSSL_IMPLEMENT_GETAUXVAL
187 if (getauxval(HWCAP) & HWCAP_NEON) {
188 unsigned long hwcap = getauxval(HWCAP_CE);
189
190 OPENSSL_armcap_P |= ARMV7_NEON;
191
192 if (hwcap & HWCAP_CE_AES)
193 OPENSSL_armcap_P |= ARMV8_AES;
194
195 if (hwcap & HWCAP_CE_PMULL)
196 OPENSSL_armcap_P |= ARMV8_PMULL;
197
198 if (hwcap & HWCAP_CE_SHA1)
199 OPENSSL_armcap_P |= ARMV8_SHA1;
200
201 if (hwcap & HWCAP_CE_SHA256)
202 OPENSSL_armcap_P |= ARMV8_SHA256;
203
204 # ifdef __aarch64__
205 if (hwcap & HWCAP_CE_SHA512)
206 OPENSSL_armcap_P |= ARMV8_SHA512;
207
208 if (hwcap & HWCAP_CPUID)
209 OPENSSL_armcap_P |= ARMV8_CPUID;
210 # endif
211 }
212 # endif
213
214 sigfillset(&all_masked);
215 sigdelset(&all_masked, SIGILL);
216 sigdelset(&all_masked, SIGTRAP);
217 sigdelset(&all_masked, SIGFPE);
218 sigdelset(&all_masked, SIGBUS);
219 sigdelset(&all_masked, SIGSEGV);
220
221 memset(&ill_act, 0, sizeof(ill_act));
222 ill_act.sa_handler = ill_handler;
223 ill_act.sa_mask = all_masked;
224
225 sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
226 sigaction(SIGILL, &ill_act, &ill_oact);
227
228 /* If we used getauxval, we already have all the values */
229 # ifndef OSSL_IMPLEMENT_GETAUXVAL
230 if (sigsetjmp(ill_jmp, 1) == 0) {
231 _armv7_neon_probe();
232 OPENSSL_armcap_P |= ARMV7_NEON;
233 if (sigsetjmp(ill_jmp, 1) == 0) {
234 _armv8_pmull_probe();
235 OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
236 } else if (sigsetjmp(ill_jmp, 1) == 0) {
237 _armv8_aes_probe();
238 OPENSSL_armcap_P |= ARMV8_AES;
239 }
240 if (sigsetjmp(ill_jmp, 1) == 0) {
241 _armv8_sha1_probe();
242 OPENSSL_armcap_P |= ARMV8_SHA1;
243 }
244 if (sigsetjmp(ill_jmp, 1) == 0) {
245 _armv8_sha256_probe();
246 OPENSSL_armcap_P |= ARMV8_SHA256;
247 }
248 # if defined(__aarch64__) && !defined(__APPLE__)
249 if (sigsetjmp(ill_jmp, 1) == 0) {
250 _armv8_sha512_probe();
251 OPENSSL_armcap_P |= ARMV8_SHA512;
252 }
253 # endif
254 }
255 # endif
256
257 /* Things that getauxval didn't tell us */
258 if (sigsetjmp(ill_jmp, 1) == 0) {
259 _armv7_tick();
260 OPENSSL_armcap_P |= ARMV7_TICK;
261 }
262
263 sigaction(SIGILL, &ill_oact, NULL);
264 sigprocmask(SIG_SETMASK, &oset, NULL);
265
266 # ifdef __aarch64__
267 if (OPENSSL_armcap_P & ARMV8_CPUID)
268 OPENSSL_arm_midr = _armv8_cpuid_probe();
269
270 if ((MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72) ||
271 MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1)) &&
272 (OPENSSL_armcap_P & ARMV7_NEON)) {
273 OPENSSL_armv8_rsa_neonized = 1;
274 }
275 # endif
276 }
277 #endif
278