14ec2eca9Schristos /*
2856fa3afSchristos  * Copyright 2009-2021 The OpenSSL Project Authors. All Rights Reserved.
34ec2eca9Schristos  *
44ec2eca9Schristos  * Licensed under the OpenSSL license (the "License").  You may not use
54ec2eca9Schristos  * this file except in compliance with the License.  You can obtain a copy
64ec2eca9Schristos  * in the file LICENSE in the source distribution or at
74ec2eca9Schristos  * https://www.openssl.org/source/license.html
84ec2eca9Schristos  */
94ec2eca9Schristos 
1013ce5681Schristos #include <stdio.h>
1113ce5681Schristos #include <stdlib.h>
1213ce5681Schristos #include <string.h>
1313ce5681Schristos #include <setjmp.h>
1413ce5681Schristos #include <signal.h>
1521bfa713Schristos #include <unistd.h>
167fa80bafSspz #if defined(__linux) || defined(_AIX)
177fa80bafSspz # include <sys/utsname.h>
187fa80bafSspz #endif
194ec2eca9Schristos #if defined(_AIX53)     /* defined even on post-5.3 */
204ec2eca9Schristos # include <sys/systemcfg.h>
214ec2eca9Schristos # if !defined(__power_set)
224ec2eca9Schristos #  define __power_set(a) (_system_configuration.implementation & (a))
234ec2eca9Schristos # endif
244ec2eca9Schristos #endif
254ec2eca9Schristos #if defined(__APPLE__) && defined(__MACH__)
264ec2eca9Schristos # include <sys/types.h>
274ec2eca9Schristos # include <sys/sysctl.h>
284ec2eca9Schristos #endif
29d4503398Srin #if defined(__NetBSD__)
30d4503398Srin # include <sys/param.h>
31d4503398Srin # include <sys/sysctl.h>
32d4503398Srin #endif
337fa80bafSspz #include <openssl/crypto.h>
3413ce5681Schristos #include <openssl/bn.h>
35e7ccb6d1Schristos #include <internal/cryptlib.h>
36cb006352Schristos #include <crypto/chacha.h>
37cb006352Schristos #include "bn/bn_local.h"
3813ce5681Schristos 
397fa80bafSspz #include "ppc_arch.h"
4013ce5681Schristos 
417fa80bafSspz unsigned int OPENSSL_ppccap_P = 0;
4213ce5681Schristos 
4313ce5681Schristos static sigset_t all_masked;
4413ce5681Schristos 
4513ce5681Schristos #ifdef OPENSSL_BN_ASM_MONT
46d572d25fSspz int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
47d572d25fSspz                 const BN_ULONG *np, const BN_ULONG *n0, int num)
4813ce5681Schristos {
49d572d25fSspz     int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
50d572d25fSspz                         const BN_ULONG *np, const BN_ULONG *n0, int num);
51e7ccb6d1Schristos     int bn_mul4x_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
52e7ccb6d1Schristos                           const BN_ULONG *np, const BN_ULONG *n0, int num);
5313ce5681Schristos 
54e7ccb6d1Schristos     if (num < 4)
55e7ccb6d1Schristos         return 0;
56e7ccb6d1Schristos 
57e7ccb6d1Schristos     if ((num & 3) == 0)
58e7ccb6d1Schristos         return bn_mul4x_mont_int(rp, ap, bp, np, n0, num);
59e7ccb6d1Schristos 
60d572d25fSspz     /*
61e7ccb6d1Schristos      * There used to be [optional] call to bn_mul_mont_fpu64 here,
62e7ccb6d1Schristos      * but above subroutine is faster on contemporary processors.
63e7ccb6d1Schristos      * Formulation means that there might be old processors where
64e7ccb6d1Schristos      * FPU code path would be faster, POWER6 perhaps, but there was
65e7ccb6d1Schristos      * no opportunity to figure it out...
66d572d25fSspz      */
6713ce5681Schristos 
6813ce5681Schristos     return bn_mul_mont_int(rp, ap, bp, np, n0, num);
6913ce5681Schristos }
7013ce5681Schristos #endif
7113ce5681Schristos 
727fa80bafSspz #ifdef DONT_USE_SHA256_FROM_LIBC
737fa80bafSspz void sha256_block_p8(void *ctx, const void *inp, size_t len);
747fa80bafSspz void sha256_block_ppc(void *ctx, const void *inp, size_t len);
75e7ccb6d1Schristos void sha256_block_data_order(void *ctx, const void *inp, size_t len);
767fa80bafSspz void sha256_block_data_order(void *ctx, const void *inp, size_t len)
777fa80bafSspz {
787fa80bafSspz     OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha256_block_p8(ctx, inp, len) :
797fa80bafSspz         sha256_block_ppc(ctx, inp, len);
807fa80bafSspz }
817fa80bafSspz #endif
827fa80bafSspz 
837fa80bafSspz #ifdef DONT_USE_SHA512_FROM_LIBC
847fa80bafSspz void sha512_block_p8(void *ctx, const void *inp, size_t len);
857fa80bafSspz void sha512_block_ppc(void *ctx, const void *inp, size_t len);
86e7ccb6d1Schristos void sha512_block_data_order(void *ctx, const void *inp, size_t len);
877fa80bafSspz void sha512_block_data_order(void *ctx, const void *inp, size_t len)
887fa80bafSspz {
897fa80bafSspz     OPENSSL_ppccap_P & PPC_CRYPTO207 ? sha512_block_p8(ctx, inp, len) :
907fa80bafSspz         sha512_block_ppc(ctx, inp, len);
917fa80bafSspz }
927fa80bafSspz #endif
937fa80bafSspz 
944ec2eca9Schristos #ifndef OPENSSL_NO_CHACHA
954ec2eca9Schristos void ChaCha20_ctr32_int(unsigned char *out, const unsigned char *inp,
964ec2eca9Schristos                         size_t len, const unsigned int key[8],
974ec2eca9Schristos                         const unsigned int counter[4]);
984ec2eca9Schristos void ChaCha20_ctr32_vmx(unsigned char *out, const unsigned char *inp,
994ec2eca9Schristos                         size_t len, const unsigned int key[8],
1004ec2eca9Schristos                         const unsigned int counter[4]);
101e7ccb6d1Schristos void ChaCha20_ctr32_vsx(unsigned char *out, const unsigned char *inp,
102e7ccb6d1Schristos                         size_t len, const unsigned int key[8],
103e7ccb6d1Schristos                         const unsigned int counter[4]);
1044ec2eca9Schristos void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
1054ec2eca9Schristos                     size_t len, const unsigned int key[8],
1064ec2eca9Schristos                     const unsigned int counter[4])
1074ec2eca9Schristos {
108e7ccb6d1Schristos     OPENSSL_ppccap_P & PPC_CRYPTO207
109e7ccb6d1Schristos         ? ChaCha20_ctr32_vsx(out, inp, len, key, counter)
110e7ccb6d1Schristos         : OPENSSL_ppccap_P & PPC_ALTIVEC
1114ec2eca9Schristos             ? ChaCha20_ctr32_vmx(out, inp, len, key, counter)
1124ec2eca9Schristos             : ChaCha20_ctr32_int(out, inp, len, key, counter);
1134ec2eca9Schristos }
1144ec2eca9Schristos #endif
1154ec2eca9Schristos 
1164ec2eca9Schristos #ifndef OPENSSL_NO_POLY1305
1174ec2eca9Schristos void poly1305_init_int(void *ctx, const unsigned char key[16]);
1184ec2eca9Schristos void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
1194ec2eca9Schristos                          unsigned int padbit);
1204ec2eca9Schristos void poly1305_emit(void *ctx, unsigned char mac[16],
1214ec2eca9Schristos                        const unsigned int nonce[4]);
1224ec2eca9Schristos void poly1305_init_fpu(void *ctx, const unsigned char key[16]);
1234ec2eca9Schristos void poly1305_blocks_fpu(void *ctx, const unsigned char *inp, size_t len,
1244ec2eca9Schristos                          unsigned int padbit);
1254ec2eca9Schristos void poly1305_emit_fpu(void *ctx, unsigned char mac[16],
1264ec2eca9Schristos                        const unsigned int nonce[4]);
127e7ccb6d1Schristos int poly1305_init(void *ctx, const unsigned char key[16], void *func[2]);
1284ec2eca9Schristos int poly1305_init(void *ctx, const unsigned char key[16], void *func[2])
1294ec2eca9Schristos {
1304ec2eca9Schristos     if (sizeof(size_t) == 4 && (OPENSSL_ppccap_P & PPC_FPU)) {
1314ec2eca9Schristos         poly1305_init_fpu(ctx, key);
132e7ccb6d1Schristos         func[0] = (void*)(uintptr_t)poly1305_blocks_fpu;
133e7ccb6d1Schristos         func[1] = (void*)(uintptr_t)poly1305_emit_fpu;
1344ec2eca9Schristos     } else {
1354ec2eca9Schristos         poly1305_init_int(ctx, key);
136e7ccb6d1Schristos         func[0] = (void*)(uintptr_t)poly1305_blocks;
137e7ccb6d1Schristos         func[1] = (void*)(uintptr_t)poly1305_emit;
1384ec2eca9Schristos     }
1394ec2eca9Schristos     return 1;
1404ec2eca9Schristos }
1414ec2eca9Schristos #endif
1424ec2eca9Schristos 
143e7ccb6d1Schristos #ifdef ECP_NISTZ256_ASM
144e7ccb6d1Schristos void ecp_nistz256_mul_mont(unsigned long res[4], const unsigned long a[4],
145e7ccb6d1Schristos                            const unsigned long b[4]);
146e7ccb6d1Schristos 
147e7ccb6d1Schristos void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4]);
148e7ccb6d1Schristos void ecp_nistz256_to_mont(unsigned long res[4], const unsigned long in[4])
149e7ccb6d1Schristos {
150e7ccb6d1Schristos     static const unsigned long RR[] = { 0x0000000000000003U,
151e7ccb6d1Schristos                                         0xfffffffbffffffffU,
152e7ccb6d1Schristos                                         0xfffffffffffffffeU,
153e7ccb6d1Schristos                                         0x00000004fffffffdU };
154e7ccb6d1Schristos 
155e7ccb6d1Schristos     ecp_nistz256_mul_mont(res, in, RR);
156e7ccb6d1Schristos }
157e7ccb6d1Schristos 
158e7ccb6d1Schristos void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4]);
159e7ccb6d1Schristos void ecp_nistz256_from_mont(unsigned long res[4], const unsigned long in[4])
160e7ccb6d1Schristos {
161e7ccb6d1Schristos     static const unsigned long one[] = { 1, 0, 0, 0 };
162e7ccb6d1Schristos 
163e7ccb6d1Schristos     ecp_nistz256_mul_mont(res, in, one);
164e7ccb6d1Schristos }
165e7ccb6d1Schristos #endif
166e7ccb6d1Schristos 
16713ce5681Schristos static sigjmp_buf ill_jmp;
168d572d25fSspz static void ill_handler(int sig)
169d572d25fSspz {
170d572d25fSspz     siglongjmp(ill_jmp, sig);
171d572d25fSspz }
17213ce5681Schristos 
1734ec2eca9Schristos void OPENSSL_fpu_probe(void);
17413ce5681Schristos void OPENSSL_ppc64_probe(void);
17521bfa713Schristos void OPENSSL_altivec_probe(void);
1767fa80bafSspz void OPENSSL_crypto207_probe(void);
1774ec2eca9Schristos void OPENSSL_madd300_probe(void);
17813ce5681Schristos 
17992d83a12Schristos long OPENSSL_rdtsc_mftb(void);
18092d83a12Schristos long OPENSSL_rdtsc_mfspr268(void);
18192d83a12Schristos 
18292d83a12Schristos uint32_t OPENSSL_rdtsc(void)
18392d83a12Schristos {
18492d83a12Schristos     if (OPENSSL_ppccap_P & PPC_MFTB)
18592d83a12Schristos         return OPENSSL_rdtsc_mftb();
18692d83a12Schristos     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
18792d83a12Schristos         return OPENSSL_rdtsc_mfspr268();
18892d83a12Schristos     else
18992d83a12Schristos         return 0;
19092d83a12Schristos }
19192d83a12Schristos 
19292d83a12Schristos size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
19392d83a12Schristos size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
19492d83a12Schristos 
19592d83a12Schristos size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
19692d83a12Schristos {
19792d83a12Schristos     if (OPENSSL_ppccap_P & PPC_MFTB)
19892d83a12Schristos         return OPENSSL_instrument_bus_mftb(out, cnt);
19992d83a12Schristos     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
20092d83a12Schristos         return OPENSSL_instrument_bus_mfspr268(out, cnt);
20192d83a12Schristos     else
20292d83a12Schristos         return 0;
20392d83a12Schristos }
20492d83a12Schristos 
20592d83a12Schristos size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
20692d83a12Schristos size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
20792d83a12Schristos 
20892d83a12Schristos size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
20992d83a12Schristos {
21092d83a12Schristos     if (OPENSSL_ppccap_P & PPC_MFTB)
21192d83a12Schristos         return OPENSSL_instrument_bus2_mftb(out, cnt, max);
21292d83a12Schristos     else if (OPENSSL_ppccap_P & PPC_MFSPR268)
21392d83a12Schristos         return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
21492d83a12Schristos     else
21592d83a12Schristos         return 0;
21692d83a12Schristos }
21792d83a12Schristos 
21892d83a12Schristos #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
21992d83a12Schristos # if __GLIBC_PREREQ(2, 16)
22092d83a12Schristos #  include <sys/auxv.h>
22192d83a12Schristos #  define OSSL_IMPLEMENT_GETAUXVAL
222*e1f0537eSchristos # elif defined(__ANDROID_API__)
223*e1f0537eSchristos /* see https://developer.android.google.cn/ndk/guides/cpu-features */
224*e1f0537eSchristos #  if __ANDROID_API__ >= 18
225*e1f0537eSchristos #   include <sys/auxv.h>
226*e1f0537eSchristos #   define OSSL_IMPLEMENT_GETAUXVAL
227*e1f0537eSchristos #  endif
22892d83a12Schristos # endif
2294ec2eca9Schristos #endif
2304ec2eca9Schristos 
231856fa3afSchristos #if defined(__FreeBSD__)
232856fa3afSchristos # include <sys/param.h>
233856fa3afSchristos # if __FreeBSD_version >= 1200000
234856fa3afSchristos #  include <sys/auxv.h>
235856fa3afSchristos #  define OSSL_IMPLEMENT_GETAUXVAL
236856fa3afSchristos 
237856fa3afSchristos static unsigned long getauxval(unsigned long key)
238856fa3afSchristos {
239856fa3afSchristos   unsigned long val = 0ul;
240856fa3afSchristos 
241856fa3afSchristos   if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
242856fa3afSchristos     return 0ul;
243856fa3afSchristos 
244856fa3afSchristos   return val;
245856fa3afSchristos }
246856fa3afSchristos # endif
247856fa3afSchristos #endif
248856fa3afSchristos 
2494ec2eca9Schristos /* I wish <sys/auxv.h> was universally available */
2504ec2eca9Schristos #define HWCAP                   16      /* AT_HWCAP */
2514ec2eca9Schristos #define HWCAP_PPC64             (1U << 30)
2524ec2eca9Schristos #define HWCAP_ALTIVEC           (1U << 28)
2534ec2eca9Schristos #define HWCAP_FPU               (1U << 27)
2544ec2eca9Schristos #define HWCAP_POWER6_EXT        (1U << 9)
2554ec2eca9Schristos #define HWCAP_VSX               (1U << 7)
2564ec2eca9Schristos 
2574ec2eca9Schristos #define HWCAP2                  26      /* AT_HWCAP2 */
2584ec2eca9Schristos #define HWCAP_VEC_CRYPTO        (1U << 25)
2594ec2eca9Schristos #define HWCAP_ARCH_3_00         (1U << 23)
2604ec2eca9Schristos 
2614ec2eca9Schristos # if defined(__GNUC__) && __GNUC__>=2
2624ec2eca9Schristos __attribute__ ((constructor))
2634ec2eca9Schristos # endif
26413ce5681Schristos void OPENSSL_cpuid_setup(void)
26513ce5681Schristos {
26613ce5681Schristos     char *e;
26713ce5681Schristos     struct sigaction ill_oact, ill_act;
26813ce5681Schristos     sigset_t oset;
26913ce5681Schristos     static int trigger = 0;
27013ce5681Schristos 
271d572d25fSspz     if (trigger)
272d572d25fSspz         return;
27313ce5681Schristos     trigger = 1;
27413ce5681Schristos 
2754ec2eca9Schristos     if ((e = getenv("OPENSSL_ppccap"))) {
2764ec2eca9Schristos         OPENSSL_ppccap_P = strtoul(e, NULL, 0);
2774ec2eca9Schristos         return;
2784ec2eca9Schristos     }
2794ec2eca9Schristos 
2804ec2eca9Schristos     OPENSSL_ppccap_P = 0;
2814ec2eca9Schristos 
2824ec2eca9Schristos #if defined(_AIX)
2834ec2eca9Schristos     OPENSSL_ppccap_P |= PPC_FPU;
2844ec2eca9Schristos 
2854ec2eca9Schristos     if (sizeof(size_t) == 4) {
2864ec2eca9Schristos         struct utsname uts;
2874ec2eca9Schristos # if defined(_SC_AIX_KERNEL_BITMODE)
2884ec2eca9Schristos         if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
2894ec2eca9Schristos             return;
2904ec2eca9Schristos # endif
2914ec2eca9Schristos         if (uname(&uts) != 0 || atoi(uts.version) < 6)
2924ec2eca9Schristos             return;
2934ec2eca9Schristos     }
2944ec2eca9Schristos 
2954ec2eca9Schristos # if defined(__power_set)
2964ec2eca9Schristos     /*
2974ec2eca9Schristos      * Value used in __power_set is a single-bit 1<<n one denoting
2984ec2eca9Schristos      * specific processor class. Incidentally 0xffffffff<<n can be
2994ec2eca9Schristos      * used to denote specific processor and its successors.
3004ec2eca9Schristos      */
3014ec2eca9Schristos     if (sizeof(size_t) == 4) {
3024ec2eca9Schristos         /* In 32-bit case PPC_FPU64 is always fastest [if option] */
3034ec2eca9Schristos         if (__power_set(0xffffffffU<<13))       /* POWER5 and later */
3044ec2eca9Schristos             OPENSSL_ppccap_P |= PPC_FPU64;
3054ec2eca9Schristos     } else {
3064ec2eca9Schristos         /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
3074ec2eca9Schristos         if (__power_set(0x1U<<14))              /* POWER6 */
3084ec2eca9Schristos             OPENSSL_ppccap_P |= PPC_FPU64;
3094ec2eca9Schristos     }
3104ec2eca9Schristos 
3114ec2eca9Schristos     if (__power_set(0xffffffffU<<14))           /* POWER6 and later */
3124ec2eca9Schristos         OPENSSL_ppccap_P |= PPC_ALTIVEC;
3134ec2eca9Schristos 
3144ec2eca9Schristos     if (__power_set(0xffffffffU<<16))           /* POWER8 and later */
3154ec2eca9Schristos         OPENSSL_ppccap_P |= PPC_CRYPTO207;
3164ec2eca9Schristos 
3174ec2eca9Schristos     if (__power_set(0xffffffffU<<17))           /* POWER9 and later */
3184ec2eca9Schristos         OPENSSL_ppccap_P |= PPC_MADD300;
3194ec2eca9Schristos 
3204ec2eca9Schristos     return;
3214ec2eca9Schristos # endif
3224ec2eca9Schristos #endif
3234ec2eca9Schristos 
3244ec2eca9Schristos #if defined(__APPLE__) && defined(__MACH__)
3254ec2eca9Schristos     OPENSSL_ppccap_P |= PPC_FPU;
3264ec2eca9Schristos 
3274ec2eca9Schristos     {
3284ec2eca9Schristos         int val;
3294ec2eca9Schristos         size_t len = sizeof(val);
3304ec2eca9Schristos 
3314ec2eca9Schristos         if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
3324ec2eca9Schristos             if (val)
3334ec2eca9Schristos                 OPENSSL_ppccap_P |= PPC_FPU64;
3344ec2eca9Schristos         }
3354ec2eca9Schristos 
3364ec2eca9Schristos         len = sizeof(val);
3374ec2eca9Schristos         if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
3384ec2eca9Schristos             if (val)
3394ec2eca9Schristos                 OPENSSL_ppccap_P |= PPC_ALTIVEC;
3404ec2eca9Schristos         }
3414ec2eca9Schristos 
3424ec2eca9Schristos         return;
3434ec2eca9Schristos     }
3444ec2eca9Schristos #endif
3454ec2eca9Schristos 
34692d83a12Schristos #ifdef OSSL_IMPLEMENT_GETAUXVAL
34792d83a12Schristos     {
3484ec2eca9Schristos         unsigned long hwcap = getauxval(HWCAP);
349380ea7e6Schristos         unsigned long hwcap2 = getauxval(HWCAP2);
3504ec2eca9Schristos 
3514ec2eca9Schristos         if (hwcap & HWCAP_FPU) {
3524ec2eca9Schristos             OPENSSL_ppccap_P |= PPC_FPU;
3534ec2eca9Schristos 
3544ec2eca9Schristos             if (sizeof(size_t) == 4) {
3554ec2eca9Schristos                 /* In 32-bit case PPC_FPU64 is always fastest [if option] */
3564ec2eca9Schristos                 if (hwcap & HWCAP_PPC64)
3574ec2eca9Schristos                     OPENSSL_ppccap_P |= PPC_FPU64;
3584ec2eca9Schristos             } else {
3594ec2eca9Schristos                 /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
3604ec2eca9Schristos                 if (hwcap & HWCAP_POWER6_EXT)
3614ec2eca9Schristos                     OPENSSL_ppccap_P |= PPC_FPU64;
3624ec2eca9Schristos             }
3634ec2eca9Schristos         }
3644ec2eca9Schristos 
3654ec2eca9Schristos         if (hwcap & HWCAP_ALTIVEC) {
3664ec2eca9Schristos             OPENSSL_ppccap_P |= PPC_ALTIVEC;
3674ec2eca9Schristos 
368380ea7e6Schristos             if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
3694ec2eca9Schristos                 OPENSSL_ppccap_P |= PPC_CRYPTO207;
3704ec2eca9Schristos         }
3714ec2eca9Schristos 
372380ea7e6Schristos         if (hwcap2 & HWCAP_ARCH_3_00) {
3734ec2eca9Schristos             OPENSSL_ppccap_P |= PPC_MADD300;
3744ec2eca9Schristos         }
3754ec2eca9Schristos     }
37692d83a12Schristos #endif
3774ec2eca9Schristos 
37813ce5681Schristos     sigfillset(&all_masked);
37913ce5681Schristos     sigdelset(&all_masked, SIGILL);
38013ce5681Schristos     sigdelset(&all_masked, SIGTRAP);
38113ce5681Schristos #ifdef SIGEMT
38213ce5681Schristos     sigdelset(&all_masked, SIGEMT);
38313ce5681Schristos #endif
38413ce5681Schristos     sigdelset(&all_masked, SIGFPE);
38513ce5681Schristos     sigdelset(&all_masked, SIGBUS);
38613ce5681Schristos     sigdelset(&all_masked, SIGSEGV);
38713ce5681Schristos 
38813ce5681Schristos     memset(&ill_act, 0, sizeof(ill_act));
38913ce5681Schristos     ill_act.sa_handler = ill_handler;
39013ce5681Schristos     ill_act.sa_mask = all_masked;
39113ce5681Schristos 
39213ce5681Schristos     sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
39313ce5681Schristos     sigaction(SIGILL, &ill_act, &ill_oact);
39413ce5681Schristos 
39592d83a12Schristos #ifndef OSSL_IMPLEMENT_GETAUXVAL
396d4503398Srin # ifdef __NetBSD__
397d4503398Srin     int error, val;
398d4503398Srin     size_t len = sizeof(val);
399d4503398Srin 
400d4503398Srin     /*
40190ed1af2Sriastradh      * If machdep.fpu_present == 0, FPU is absent and emulated by
40290ed1af2Sriastradh      * software.  In that case, using FPU instructions hurts rather
40390ed1af2Sriastradh      * than helps performance, and the software is unlikely to run in
40490ed1af2Sriastradh      * constant time so it would expose us to timing side channel
40590ed1af2Sriastradh      * attacks.  So don't do it!
406d4503398Srin      */
407d4503398Srin     error = sysctlbyname("machdep.fpu_present", &val, &len, NULL, 0);
408d4503398Srin     if (error != 0 || (error == 0 && val != 0))
409d4503398Srin # endif
4104ec2eca9Schristos     if (sigsetjmp(ill_jmp,1) == 0) {
4114ec2eca9Schristos         OPENSSL_fpu_probe();
4124ec2eca9Schristos         OPENSSL_ppccap_P |= PPC_FPU;
4134ec2eca9Schristos 
414d572d25fSspz         if (sizeof(size_t) == 4) {
4157fa80bafSspz # ifdef __linux
4167fa80bafSspz             struct utsname uts;
4177fa80bafSspz             if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
4187fa80bafSspz # endif
419d572d25fSspz                 if (sigsetjmp(ill_jmp, 1) == 0) {
42013ce5681Schristos                     OPENSSL_ppc64_probe();
42113ce5681Schristos                     OPENSSL_ppccap_P |= PPC_FPU64;
42213ce5681Schristos                 }
423d572d25fSspz         } else {
42413ce5681Schristos             /*
42513ce5681Schristos              * Wanted code detecting POWER6 CPU and setting PPC_FPU64
42613ce5681Schristos              */
42713ce5681Schristos         }
4284ec2eca9Schristos     }
42913ce5681Schristos 
430d572d25fSspz     if (sigsetjmp(ill_jmp, 1) == 0) {
43113ce5681Schristos         OPENSSL_altivec_probe();
43213ce5681Schristos         OPENSSL_ppccap_P |= PPC_ALTIVEC;
4337fa80bafSspz         if (sigsetjmp(ill_jmp, 1) == 0) {
4347fa80bafSspz             OPENSSL_crypto207_probe();
4357fa80bafSspz             OPENSSL_ppccap_P |= PPC_CRYPTO207;
4367fa80bafSspz         }
43713ce5681Schristos     }
43813ce5681Schristos 
4394ec2eca9Schristos     if (sigsetjmp(ill_jmp, 1) == 0) {
4404ec2eca9Schristos         OPENSSL_madd300_probe();
4414ec2eca9Schristos         OPENSSL_ppccap_P |= PPC_MADD300;
4424ec2eca9Schristos     }
44392d83a12Schristos #endif
44492d83a12Schristos 
44592d83a12Schristos     if (sigsetjmp(ill_jmp, 1) == 0) {
44692d83a12Schristos         OPENSSL_rdtsc_mftb();
44792d83a12Schristos         OPENSSL_ppccap_P |= PPC_MFTB;
44892d83a12Schristos     } else if (sigsetjmp(ill_jmp, 1) == 0) {
44992d83a12Schristos         OPENSSL_rdtsc_mfspr268();
45092d83a12Schristos         OPENSSL_ppccap_P |= PPC_MFSPR268;
45192d83a12Schristos     }
4524ec2eca9Schristos 
45313ce5681Schristos     sigaction(SIGILL, &ill_oact, NULL);
45413ce5681Schristos     sigprocmask(SIG_SETMASK, &oset, NULL);
45513ce5681Schristos }
456