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