xref: /freebsd/crypto/openssl/crypto/armcap.c (revision c697fb7f)
1 /*
2  * Copyright 2011-2019 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 __attribute__ ((visibility("hidden")))
21 unsigned int OPENSSL_armcap_P = 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 # endif
53 uint32_t _armv7_tick(void);
54 
55 uint32_t OPENSSL_rdtsc(void)
56 {
57     if (OPENSSL_armcap_P & ARMV7_TICK)
58         return _armv7_tick();
59     else
60         return 0;
61 }
62 
63 # if defined(__GNUC__) && __GNUC__>=2
64 void OPENSSL_cpuid_setup(void) __attribute__ ((constructor));
65 # endif
66 
67 # if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
68 #  if __GLIBC_PREREQ(2, 16)
69 #   include <sys/auxv.h>
70 #   define OSSL_IMPLEMENT_GETAUXVAL
71 #  endif
72 # endif
73 
74 /*
75  * ARM puts the feature bits for Crypto Extensions in AT_HWCAP2, whereas
76  * AArch64 used AT_HWCAP.
77  */
78 # if defined(__arm__) || defined (__arm)
79 #  define HWCAP                  16
80                                   /* AT_HWCAP */
81 #  define HWCAP_NEON             (1 << 12)
82 
83 #  define HWCAP_CE               26
84                                   /* AT_HWCAP2 */
85 #  define HWCAP_CE_AES           (1 << 0)
86 #  define HWCAP_CE_PMULL         (1 << 1)
87 #  define HWCAP_CE_SHA1          (1 << 2)
88 #  define HWCAP_CE_SHA256        (1 << 3)
89 # elif defined(__aarch64__)
90 #  define HWCAP                  16
91                                   /* AT_HWCAP */
92 #  define HWCAP_NEON             (1 << 1)
93 
94 #  define HWCAP_CE               HWCAP
95 #  define HWCAP_CE_AES           (1 << 3)
96 #  define HWCAP_CE_PMULL         (1 << 4)
97 #  define HWCAP_CE_SHA1          (1 << 5)
98 #  define HWCAP_CE_SHA256        (1 << 6)
99 #  define HWCAP_CE_SHA512        (1 << 21)
100 # endif
101 
102 void OPENSSL_cpuid_setup(void)
103 {
104     const char *e;
105     struct sigaction ill_oact, ill_act;
106     sigset_t oset;
107     static int trigger = 0;
108 
109     if (trigger)
110         return;
111     trigger = 1;
112 
113     if ((e = getenv("OPENSSL_armcap"))) {
114         OPENSSL_armcap_P = (unsigned int)strtoul(e, NULL, 0);
115         return;
116     }
117 
118 # if defined(__APPLE__) && !defined(__aarch64__)
119     /*
120      * Capability probing by catching SIGILL appears to be problematic
121      * on iOS. But since Apple universe is "monocultural", it's actually
122      * possible to simply set pre-defined processor capability mask.
123      */
124     if (1) {
125         OPENSSL_armcap_P = ARMV7_NEON;
126         return;
127     }
128     /*
129      * One could do same even for __aarch64__ iOS builds. It's not done
130      * exclusively for reasons of keeping code unified across platforms.
131      * Unified code works because it never triggers SIGILL on Apple
132      * devices...
133      */
134 # endif
135 
136     OPENSSL_armcap_P = 0;
137 
138 # ifdef OSSL_IMPLEMENT_GETAUXVAL
139     if (getauxval(HWCAP) & HWCAP_NEON) {
140         unsigned long hwcap = getauxval(HWCAP_CE);
141 
142         OPENSSL_armcap_P |= ARMV7_NEON;
143 
144         if (hwcap & HWCAP_CE_AES)
145             OPENSSL_armcap_P |= ARMV8_AES;
146 
147         if (hwcap & HWCAP_CE_PMULL)
148             OPENSSL_armcap_P |= ARMV8_PMULL;
149 
150         if (hwcap & HWCAP_CE_SHA1)
151             OPENSSL_armcap_P |= ARMV8_SHA1;
152 
153         if (hwcap & HWCAP_CE_SHA256)
154             OPENSSL_armcap_P |= ARMV8_SHA256;
155 
156 #  ifdef __aarch64__
157         if (hwcap & HWCAP_CE_SHA512)
158             OPENSSL_armcap_P |= ARMV8_SHA512;
159 #  endif
160     }
161 # endif
162 
163     sigfillset(&all_masked);
164     sigdelset(&all_masked, SIGILL);
165     sigdelset(&all_masked, SIGTRAP);
166     sigdelset(&all_masked, SIGFPE);
167     sigdelset(&all_masked, SIGBUS);
168     sigdelset(&all_masked, SIGSEGV);
169 
170     memset(&ill_act, 0, sizeof(ill_act));
171     ill_act.sa_handler = ill_handler;
172     ill_act.sa_mask = all_masked;
173 
174     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
175     sigaction(SIGILL, &ill_act, &ill_oact);
176 
177     /* If we used getauxval, we already have all the values */
178 # ifndef OSSL_IMPLEMENT_GETAUXVAL
179     if (sigsetjmp(ill_jmp, 1) == 0) {
180         _armv7_neon_probe();
181         OPENSSL_armcap_P |= ARMV7_NEON;
182         if (sigsetjmp(ill_jmp, 1) == 0) {
183             _armv8_pmull_probe();
184             OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES;
185         } else if (sigsetjmp(ill_jmp, 1) == 0) {
186             _armv8_aes_probe();
187             OPENSSL_armcap_P |= ARMV8_AES;
188         }
189         if (sigsetjmp(ill_jmp, 1) == 0) {
190             _armv8_sha1_probe();
191             OPENSSL_armcap_P |= ARMV8_SHA1;
192         }
193         if (sigsetjmp(ill_jmp, 1) == 0) {
194             _armv8_sha256_probe();
195             OPENSSL_armcap_P |= ARMV8_SHA256;
196         }
197 #  if defined(__aarch64__) && !defined(__APPLE__)
198         if (sigsetjmp(ill_jmp, 1) == 0) {
199             _armv8_sha512_probe();
200             OPENSSL_armcap_P |= ARMV8_SHA512;
201         }
202 #  endif
203     }
204 # endif
205 
206     /* Things that getauxval didn't tell us */
207     if (sigsetjmp(ill_jmp, 1) == 0) {
208         _armv7_tick();
209         OPENSSL_armcap_P |= ARMV7_TICK;
210     }
211 
212     sigaction(SIGILL, &ill_oact, NULL);
213     sigprocmask(SIG_SETMASK, &oset, NULL);
214 }
215 #endif
216