]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/armcap.c
Import OpenSSL 1.1.1j.
[FreeBSD/FreeBSD.git] / crypto / armcap.c
1 /*
2  * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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 #include "internal/cryptlib.h"
17
18 #include "arm_arch.h"
19
20 unsigned int OPENSSL_armcap_P = 0;
21
22 #if __ARM_MAX_ARCH__<7
23 void OPENSSL_cpuid_setup(void)
24 {
25 }
26
27 uint32_t OPENSSL_rdtsc(void)
28 {
29     return 0;
30 }
31 #else
32 static sigset_t all_masked;
33
34 static sigjmp_buf ill_jmp;
35 static void ill_handler(int sig)
36 {
37     siglongjmp(ill_jmp, sig);
38 }
39
40 /*
41  * Following subroutines could have been inlined, but it's not all
42  * ARM compilers support inline assembler...
43  */
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);
49 # ifdef __aarch64__
50 void _armv8_sha512_probe(void);
51 # endif
52 uint32_t _armv7_tick(void);
53
54 uint32_t OPENSSL_rdtsc(void)
55 {
56     if (OPENSSL_armcap_P & ARMV7_TICK)
57         return _armv7_tick();
58     else
59         return 0;
60 }
61
62 # if defined(__GNUC__) && __GNUC__>=2
63 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
64 # endif
65
66 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
67 #  if __GLIBC_PREREQ(2, 16)
68 #   include <sys/auxv.h>
69 #   define OSSL_IMPLEMENT_GETAUXVAL
70 #  endif
71 # endif
72 # if defined(__FreeBSD__)
73 #  include <sys/param.h>
74 #  if __FreeBSD_version >= 1200000
75 #   include <sys/auxv.h>
76 #   define OSSL_IMPLEMENT_GETAUXVAL
77
78 static unsigned long getauxval(unsigned long key)
79 {
80   unsigned long val = 0ul;
81
82   if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
83     return 0ul;
84
85   return val;
86 }
87 #  endif
88 # endif
89
90 /*
91  * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
92  * AArch64 used AT_HWCAP.
93  */
94 # if defined(__arm__) || defined (__arm)
95 #  define HWCAP                  16
96                                   /* AT_HWCAP */
97 #  define HWCAP_NEON             (1 << 12)
98
99 #  define HWCAP_CE               26
100                                   /* AT_HWCAP2 */
101 #  define HWCAP_CE_AES           (1 << 0)
102 #  define HWCAP_CE_PMULL         (1 << 1)
103 #  define HWCAP_CE_SHA1          (1 << 2)
104 #  define HWCAP_CE_SHA256        (1 << 3)
105 # elif defined(__aarch64__)
106 #  define HWCAP                  16
107                                   /* AT_HWCAP */
108 #  define HWCAP_NEON             (1 << 1)
109
110 #  define HWCAP_CE               HWCAP
111 #  define HWCAP_CE_AES           (1 << 3)
112 #  define HWCAP_CE_PMULL         (1 << 4)
113 #  define HWCAP_CE_SHA1          (1 << 5)
114 #  define HWCAP_CE_SHA256        (1 << 6)
115 #  define HWCAP_CE_SHA512        (1 << 21)
116 # endif
117
118 void OPENSSL_cpuid_setup(void)
119 {
120     const char *e;
121     struct sigaction ill_oact, ill_act;
122     sigset_t oset;
123     static int trigger = 0;
124
125     if (trigger)
126         return;
127     trigger = 1;
128
129     if ((e = getenv("OPENSSL_armcap"))) {
130         OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
131         return;
132     }
133
134 # if defined(__APPLE__) && !defined(__aarch64__)
135     /*
136      * Capability probing by catching SIGILL appears to be problematic
137      * on iOS. But since Apple universe is "monocultural", it's actually
138      * possible to simply set pre-defined processor capability mask.
139      */
140     if (1) {
141         OPENSSL_armcap_P = ARMV7_NEON;
142         return;
143     }
144     /*
145      * One could do same even for __aarch64__ iOS builds. It's not done
146      * exclusively for reasons of keeping code unified across platforms.
147      * Unified code works because it never triggers SIGILL on Apple
148      * devices...
149      */
150 # endif
151
152     OPENSSL_armcap_P = 0;
153
154 # ifdef OSSL_IMPLEMENT_GETAUXVAL
155     if (getauxval(HWCAP) & HWCAP_NEON) {
156         unsigned long hwcap = getauxval(HWCAP_CE);
157
158         OPENSSL_armcap_P |= ARMV7_NEON;
159
160         if (hwcap & HWCAP_CE_AES)
161             OPENSSL_armcap_P |= ARMV8_AES;
162
163         if (hwcap & HWCAP_CE_PMULL)
164             OPENSSL_armcap_P |= ARMV8_PMULL;
165
166         if (hwcap & HWCAP_CE_SHA1)
167             OPENSSL_armcap_P |= ARMV8_SHA1;
168
169         if (hwcap & HWCAP_CE_SHA256)
170             OPENSSL_armcap_P |= ARMV8_SHA256;
171
172 #  ifdef __aarch64__
173         if (hwcap & HWCAP_CE_SHA512)
174             OPENSSL_armcap_P |= ARMV8_SHA512;
175 #  endif
176     }
177 # endif
178
179     sigfillset(&all_masked);
180     sigdelset(&all_masked, SIGILL);
181     sigdelset(&all_masked, SIGTRAP);
182     sigdelset(&all_masked, SIGFPE);
183     sigdelset(&all_masked, SIGBUS);
184     sigdelset(&all_masked, SIGSEGV);
185
186     memset(&ill_act, 0, sizeof(ill_act));
187     ill_act.sa_handler = ill_handler;
188     ill_act.sa_mask = all_masked;
189
190     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
191     sigaction(SIGILL, &ill_act, &ill_oact);
192
193     /* If we used getauxval, we already have all the values */
194 # ifndef OSSL_IMPLEMENT_GETAUXVAL
195     if (sigsetjmp(ill_jmp, 1) == 0) {
196         _armv7_neon_probe();
197         OPENSSL_armcap_P |= ARMV7_NEON;
198         if (sigsetjmp(ill_jmp, 1) == 0) {
199             _armv8_pmull_probe();
200             OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
201         } else if (sigsetjmp(ill_jmp, 1) == 0) {
202             _armv8_aes_probe();
203             OPENSSL_armcap_P |= ARMV8_AES;
204         }
205         if (sigsetjmp(ill_jmp, 1) == 0) {
206             _armv8_sha1_probe();
207             OPENSSL_armcap_P |= ARMV8_SHA1;
208         }
209         if (sigsetjmp(ill_jmp, 1) == 0) {
210             _armv8_sha256_probe();
211             OPENSSL_armcap_P |= ARMV8_SHA256;
212         }
213 #  if defined(__aarch64__) && !defined(__APPLE__)
214         if (sigsetjmp(ill_jmp, 1) == 0) {
215             _armv8_sha512_probe();
216             OPENSSL_armcap_P |= ARMV8_SHA512;
217         }
218 #  endif
219     }
220 # endif
221
222     /* Things that getauxval didn't tell us */
223     if (sigsetjmp(ill_jmp, 1) == 0) {
224         _armv7_tick();
225         OPENSSL_armcap_P |= ARMV7_TICK;
226     }
227
228     sigaction(SIGILL, &ill_oact, NULL);
229     sigprocmask(SIG_SETMASK, &oset, NULL);
230 }
231 #endif