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