Update copyright year
[openssl.git] / crypto / armcap.c
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 #include "internal/cryptlib.h"
17
18 #include "arm_arch.h"
19
20 unsigned int OPENSSL_armcap_P = 0;
21 unsigned int OPENSSL_arm_midr = 0;
22
23 #if __ARM_MAX_ARCH__<7
24 void OPENSSL_cpuid_setup(void)
25 {
26 }
27
28 uint32_t OPENSSL_rdtsc(void)
29 {
30     return 0;
31 }
32 #else
33 static sigset_t all_masked;
34
35 static sigjmp_buf ill_jmp;
36 static void ill_handler(int sig)
37 {
38     siglongjmp(ill_jmp, sig);
39 }
40
41 /*
42  * Following subroutines could have been inlined, but it's not all
43  * ARM compilers support inline assembler...
44  */
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);
50 # ifdef __aarch64__
51 void _armv8_sha512_probe(void);
52 unsigned int _armv8_cpuid_probe(void);
53 # endif
54 uint32_t _armv7_tick(void);
55
56 uint32_t OPENSSL_rdtsc(void)
57 {
58     if (OPENSSL_armcap_P & ARMV7_TICK)
59         return _armv7_tick();
60     else
61         return 0;
62 }
63
64 # if defined(__GNUC__) && __GNUC__>=2
65 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
66 # endif
67
68 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
69 #  if __GLIBC_PREREQ(2, 16)
70 #   include <sys/auxv.h>
71 #   define OSSL_IMPLEMENT_GETAUXVAL
72 #  endif
73 # endif
74 # if defined(__FreeBSD__)
75 #  include <sys/param.h>
76 #  if __FreeBSD_version >= 1200000
77 #   include <sys/auxv.h>
78 #   define OSSL_IMPLEMENT_GETAUXVAL
79
80 static unsigned long getauxval(unsigned long key)
81 {
82   unsigned long val = 0ul;
83
84   if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
85     return 0ul;
86
87   return val;
88 }
89 #  endif
90 # endif
91
92 /*
93  * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
94  * AArch64 used AT_HWCAP.
95  */
96 # if defined(__arm__) || defined (__arm)
97 #  define HWCAP                  16
98                                   /* AT_HWCAP */
99 #  define HWCAP_NEON             (1 << 12)
100
101 #  define HWCAP_CE               26
102                                   /* AT_HWCAP2 */
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__)
108 #  define HWCAP                  16
109                                   /* AT_HWCAP */
110 #  define HWCAP_NEON             (1 << 1)
111
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)
119 # endif
120
121 void OPENSSL_cpuid_setup(void)
122 {
123     const char *e;
124     struct sigaction ill_oact, ill_act;
125     sigset_t oset;
126     static int trigger = 0;
127
128     if (trigger)
129         return;
130     trigger = 1;
131
132     if ((e = getenv("OPENSSL_armcap"))) {
133         OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
134         return;
135     }
136
137 # if defined(__APPLE__) && !defined(__aarch64__)
138     /*
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.
142      */
143     if (1) {
144         OPENSSL_armcap_P = ARMV7_NEON;
145         return;
146     }
147     /*
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
151      * devices...
152      */
153 # endif
154
155     OPENSSL_armcap_P = 0;
156
157 # ifdef OSSL_IMPLEMENT_GETAUXVAL
158     if (getauxval(HWCAP) & HWCAP_NEON) {
159         unsigned long hwcap = getauxval(HWCAP_CE);
160
161         OPENSSL_armcap_P |= ARMV7_NEON;
162
163         if (hwcap & HWCAP_CE_AES)
164             OPENSSL_armcap_P |= ARMV8_AES;
165
166         if (hwcap & HWCAP_CE_PMULL)
167             OPENSSL_armcap_P |= ARMV8_PMULL;
168
169         if (hwcap & HWCAP_CE_SHA1)
170             OPENSSL_armcap_P |= ARMV8_SHA1;
171
172         if (hwcap & HWCAP_CE_SHA256)
173             OPENSSL_armcap_P |= ARMV8_SHA256;
174
175 #  ifdef __aarch64__
176         if (hwcap & HWCAP_CE_SHA512)
177             OPENSSL_armcap_P |= ARMV8_SHA512;
178
179         if (hwcap & HWCAP_CPUID)
180             OPENSSL_armcap_P |= ARMV8_CPUID;
181 #  endif
182     }
183 # endif
184
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);
191
192     memset(&ill_act, 0, sizeof(ill_act));
193     ill_act.sa_handler = ill_handler;
194     ill_act.sa_mask = all_masked;
195
196     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
197     sigaction(SIGILL, &ill_act, &ill_oact);
198
199     /* If we used getauxval, we already have all the values */
200 # ifndef OSSL_IMPLEMENT_GETAUXVAL
201     if (sigsetjmp(ill_jmp, 1) == 0) {
202         _armv7_neon_probe();
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) {
208             _armv8_aes_probe();
209             OPENSSL_armcap_P |= ARMV8_AES;
210         }
211         if (sigsetjmp(ill_jmp, 1) == 0) {
212             _armv8_sha1_probe();
213             OPENSSL_armcap_P |= ARMV8_SHA1;
214         }
215         if (sigsetjmp(ill_jmp, 1) == 0) {
216             _armv8_sha256_probe();
217             OPENSSL_armcap_P |= ARMV8_SHA256;
218         }
219 #  if defined(__aarch64__) && !defined(__APPLE__)
220         if (sigsetjmp(ill_jmp, 1) == 0) {
221             _armv8_sha512_probe();
222             OPENSSL_armcap_P |= ARMV8_SHA512;
223         }
224 #  endif
225     }
226 # endif
227
228     /* Things that getauxval didn't tell us */
229     if (sigsetjmp(ill_jmp, 1) == 0) {
230         _armv7_tick();
231         OPENSSL_armcap_P |= ARMV7_TICK;
232     }
233
234     sigaction(SIGILL, &ill_oact, NULL);
235     sigprocmask(SIG_SETMASK, &oset, NULL);
236
237 # ifdef __aarch64__
238     if (OPENSSL_armcap_P & ARMV8_CPUID)
239         OPENSSL_arm_midr = _armv8_cpuid_probe();
240 # endif
241 }
242 #endif