dca3c5342bf5ff2386a95db2a91491721365e693
[openssl.git] / crypto / ppccap.c
1 /*
2  * Copyright 2009-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 <unistd.h>
16 #if defined(__linux) || defined(_AIX)
17 # include <sys/utsname.h>
18 #endif
19 #if defined(_AIX53)     /* defined even on post-5.3 */
20 # include <sys/systemcfg.h>
21 # if !defined(__power_set)
22 #  define __power_set(a) (_system_configuration.implementation & (a))
23 # endif
24 #endif
25 #if defined(__APPLE__) && defined(__MACH__)
26 # include <sys/types.h>
27 # include <sys/sysctl.h>
28 #endif
29 #include <openssl/crypto.h>
30 #include "internal/cryptlib.h"
31 #include "crypto/ppc_arch.h"
32
33 unsigned int OPENSSL_ppccap_P = 0;
34
35 static sigset_t all_masked;
36
37 static sigjmp_buf ill_jmp;
38 static void ill_handler(int sig)
39 {
40     siglongjmp(ill_jmp, sig);
41 }
42
43 void OPENSSL_fpu_probe(void);
44 void OPENSSL_ppc64_probe(void);
45 void OPENSSL_altivec_probe(void);
46 void OPENSSL_crypto207_probe(void);
47 void OPENSSL_madd300_probe(void);
48
49 long OPENSSL_rdtsc_mftb(void);
50 long OPENSSL_rdtsc_mfspr268(void);
51
52 uint32_t OPENSSL_rdtsc(void)
53 {
54     if (OPENSSL_ppccap_P & PPC_MFTB)
55         return OPENSSL_rdtsc_mftb();
56     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
57         return OPENSSL_rdtsc_mfspr268();
58     else
59         return 0;
60 }
61
62 size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
63 size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
64
65 size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
66 {
67     if (OPENSSL_ppccap_P & PPC_MFTB)
68         return OPENSSL_instrument_bus_mftb(out, cnt);
69     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
70         return OPENSSL_instrument_bus_mfspr268(out, cnt);
71     else
72         return 0;
73 }
74
75 size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
76 size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
77
78 size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
79 {
80     if (OPENSSL_ppccap_P & PPC_MFTB)
81         return OPENSSL_instrument_bus2_mftb(out, cnt, max);
82     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
83         return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
84     else
85         return 0;
86 }
87
88 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
89 # if __GLIBC_PREREQ(2, 16)
90 #  include <sys/auxv.h>
91 #  define OSSL_IMPLEMENT_GETAUXVAL
92 # elif defined(__ANDROID_API__)
93 /* see https://developer.android.google.cn/ndk/guides/cpu-features */
94 #  if __ANDROID_API__ >= 18
95 #   include <sys/auxv.h>
96 #   define OSSL_IMPLEMENT_GETAUXVAL
97 #  endif
98 # endif
99 #endif
100
101 #if defined(__FreeBSD__)
102 # include <sys/param.h>
103 # if __FreeBSD_version >= 1200000
104 #  include <sys/auxv.h>
105 #  define OSSL_IMPLEMENT_GETAUXVAL
106
107 static unsigned long getauxval(unsigned long key)
108 {
109   unsigned long val = 0ul;
110
111   if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
112     return 0ul;
113
114   return val;
115 }
116 # endif
117 #endif
118
119 /* I wish <sys/auxv.h> was universally available */
120 #define HWCAP                   16      /* AT_HWCAP */
121 #define HWCAP_PPC64             (1U << 30)
122 #define HWCAP_ALTIVEC           (1U << 28)
123 #define HWCAP_FPU               (1U << 27)
124 #define HWCAP_POWER6_EXT        (1U << 9)
125 #define HWCAP_VSX               (1U << 7)
126
127 #define HWCAP2                  26      /* AT_HWCAP2 */
128 #define HWCAP_VEC_CRYPTO        (1U << 25)
129 #define HWCAP_ARCH_3_00         (1U << 23)
130
131 # if defined(__GNUC__) && __GNUC__>=2
132 __attribute__ ((constructor))
133 # endif
134 void OPENSSL_cpuid_setup(void)
135 {
136     char *e;
137     struct sigaction ill_oact, ill_act;
138     sigset_t oset;
139     static int trigger = 0;
140
141     if (trigger)
142         return;
143     trigger = 1;
144
145     if ((e = getenv("OPENSSL_ppccap"))) {
146         OPENSSL_ppccap_P = strtoul(e, NULL, 0);
147         return;
148     }
149
150     OPENSSL_ppccap_P = 0;
151
152 #if defined(_AIX)
153     OPENSSL_ppccap_P |= PPC_FPU;
154
155     if (sizeof(size_t) == 4) {
156         struct utsname uts;
157 # if defined(_SC_AIX_KERNEL_BITMODE)
158         if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
159             return;
160 # endif
161         if (uname(&uts) != 0 || atoi(uts.version) < 6)
162             return;
163     }
164
165 # if defined(__power_set)
166     /*
167      * Value used in __power_set is a single-bit 1<<n one denoting
168      * specific processor class. Incidentally 0xffffffff<<n can be
169      * used to denote specific processor and its successors.
170      */
171     if (sizeof(size_t) == 4) {
172         /* In 32-bit case PPC_FPU64 is always fastest [if option] */
173         if (__power_set(0xffffffffU<<13))       /* POWER5 and later */
174             OPENSSL_ppccap_P |= PPC_FPU64;
175     } else {
176         /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
177         if (__power_set(0x1U<<14))              /* POWER6 */
178             OPENSSL_ppccap_P |= PPC_FPU64;
179     }
180
181     if (__power_set(0xffffffffU<<14))           /* POWER6 and later */
182         OPENSSL_ppccap_P |= PPC_ALTIVEC;
183
184     if (__power_set(0xffffffffU<<16))           /* POWER8 and later */
185         OPENSSL_ppccap_P |= PPC_CRYPTO207;
186
187     if (__power_set(0xffffffffU<<17))           /* POWER9 and later */
188         OPENSSL_ppccap_P |= PPC_MADD300;
189
190     return;
191 # endif
192 #endif
193
194 #if defined(__APPLE__) && defined(__MACH__)
195     OPENSSL_ppccap_P |= PPC_FPU;
196
197     {
198         int val;
199         size_t len = sizeof(val);
200
201         if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
202             if (val)
203                 OPENSSL_ppccap_P |= PPC_FPU64;
204         }
205
206         len = sizeof(val);
207         if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
208             if (val)
209                 OPENSSL_ppccap_P |= PPC_ALTIVEC;
210         }
211
212         return;
213     }
214 #endif
215
216 #ifdef OSSL_IMPLEMENT_GETAUXVAL
217     {
218         unsigned long hwcap = getauxval(HWCAP);
219         unsigned long hwcap2 = getauxval(HWCAP2);
220
221         if (hwcap & HWCAP_FPU) {
222             OPENSSL_ppccap_P |= PPC_FPU;
223
224             if (sizeof(size_t) == 4) {
225                 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
226                 if (hwcap & HWCAP_PPC64)
227                     OPENSSL_ppccap_P |= PPC_FPU64;
228             } else {
229                 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
230                 if (hwcap & HWCAP_POWER6_EXT)
231                     OPENSSL_ppccap_P |= PPC_FPU64;
232             }
233         }
234
235         if (hwcap & HWCAP_ALTIVEC) {
236             OPENSSL_ppccap_P |= PPC_ALTIVEC;
237
238             if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
239                 OPENSSL_ppccap_P |= PPC_CRYPTO207;
240         }
241
242         if (hwcap2 & HWCAP_ARCH_3_00) {
243             OPENSSL_ppccap_P |= PPC_MADD300;
244         }
245     }
246 #endif
247
248     sigfillset(&all_masked);
249     sigdelset(&all_masked, SIGILL);
250     sigdelset(&all_masked, SIGTRAP);
251 #ifdef SIGEMT
252     sigdelset(&all_masked, SIGEMT);
253 #endif
254     sigdelset(&all_masked, SIGFPE);
255     sigdelset(&all_masked, SIGBUS);
256     sigdelset(&all_masked, SIGSEGV);
257
258     memset(&ill_act, 0, sizeof(ill_act));
259     ill_act.sa_handler = ill_handler;
260     ill_act.sa_mask = all_masked;
261
262     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
263     sigaction(SIGILL, &ill_act, &ill_oact);
264
265 #ifndef OSSL_IMPLEMENT_GETAUXVAL
266     if (sigsetjmp(ill_jmp, 1) == 0) {
267         OPENSSL_fpu_probe();
268         OPENSSL_ppccap_P |= PPC_FPU;
269
270         if (sizeof(size_t) == 4) {
271 # ifdef __linux
272             struct utsname uts;
273             if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
274 # endif
275                 if (sigsetjmp(ill_jmp, 1) == 0) {
276                     OPENSSL_ppc64_probe();
277                     OPENSSL_ppccap_P |= PPC_FPU64;
278                 }
279         } else {
280             /*
281              * Wanted code detecting POWER6 CPU and setting PPC_FPU64
282              */
283         }
284     }
285
286     if (sigsetjmp(ill_jmp, 1) == 0) {
287         OPENSSL_altivec_probe();
288         OPENSSL_ppccap_P |= PPC_ALTIVEC;
289         if (sigsetjmp(ill_jmp, 1) == 0) {
290             OPENSSL_crypto207_probe();
291             OPENSSL_ppccap_P |= PPC_CRYPTO207;
292         }
293     }
294
295     if (sigsetjmp(ill_jmp, 1) == 0) {
296         OPENSSL_madd300_probe();
297         OPENSSL_ppccap_P |= PPC_MADD300;
298     }
299 #endif
300
301     if (sigsetjmp(ill_jmp, 1) == 0) {
302         OPENSSL_rdtsc_mftb();
303         OPENSSL_ppccap_P |= PPC_MFTB;
304     } else if (sigsetjmp(ill_jmp, 1) == 0) {
305         OPENSSL_rdtsc_mfspr268();
306         OPENSSL_ppccap_P |= PPC_MFSPR268;
307     }
308
309     sigaction(SIGILL, &ill_oact, NULL);
310     sigprocmask(SIG_SETMASK, &oset, NULL);
311 }