14ec2eca9Schristos /*
2856fa3afSchristos * Copyright 2009-2021 The OpenSSL Project Authors. All Rights Reserved.
34ec2eca9Schristos *
4*5b10f583Schristos * Licensed under the Apache License 2.0 (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>
34*5b10f583Schristos #include "internal/cryptlib.h"
35*5b10f583Schristos #include "crypto/ppc_arch.h"
3613ce5681Schristos
377fa80bafSspz unsigned int OPENSSL_ppccap_P = 0;
3813ce5681Schristos
3913ce5681Schristos static sigset_t all_masked;
4013ce5681Schristos
4113ce5681Schristos static sigjmp_buf ill_jmp;
ill_handler(int sig)42d572d25fSspz static void ill_handler(int sig)
43d572d25fSspz {
44d572d25fSspz siglongjmp(ill_jmp, sig);
45d572d25fSspz }
4613ce5681Schristos
474ec2eca9Schristos void OPENSSL_fpu_probe(void);
4813ce5681Schristos void OPENSSL_ppc64_probe(void);
4921bfa713Schristos void OPENSSL_altivec_probe(void);
507fa80bafSspz void OPENSSL_crypto207_probe(void);
514ec2eca9Schristos void OPENSSL_madd300_probe(void);
5213ce5681Schristos
5392d83a12Schristos long OPENSSL_rdtsc_mftb(void);
5492d83a12Schristos long OPENSSL_rdtsc_mfspr268(void);
5592d83a12Schristos
OPENSSL_rdtsc(void)5692d83a12Schristos uint32_t OPENSSL_rdtsc(void)
5792d83a12Schristos {
5892d83a12Schristos if (OPENSSL_ppccap_P & PPC_MFTB)
5992d83a12Schristos return OPENSSL_rdtsc_mftb();
6092d83a12Schristos else if (OPENSSL_ppccap_P & PPC_MFSPR268)
6192d83a12Schristos return OPENSSL_rdtsc_mfspr268();
6292d83a12Schristos else
6392d83a12Schristos return 0;
6492d83a12Schristos }
6592d83a12Schristos
6692d83a12Schristos size_t OPENSSL_instrument_bus_mftb(unsigned int *, size_t);
6792d83a12Schristos size_t OPENSSL_instrument_bus_mfspr268(unsigned int *, size_t);
6892d83a12Schristos
OPENSSL_instrument_bus(unsigned int * out,size_t cnt)6992d83a12Schristos size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt)
7092d83a12Schristos {
7192d83a12Schristos if (OPENSSL_ppccap_P & PPC_MFTB)
7292d83a12Schristos return OPENSSL_instrument_bus_mftb(out, cnt);
7392d83a12Schristos else if (OPENSSL_ppccap_P & PPC_MFSPR268)
7492d83a12Schristos return OPENSSL_instrument_bus_mfspr268(out, cnt);
7592d83a12Schristos else
7692d83a12Schristos return 0;
7792d83a12Schristos }
7892d83a12Schristos
7992d83a12Schristos size_t OPENSSL_instrument_bus2_mftb(unsigned int *, size_t, size_t);
8092d83a12Schristos size_t OPENSSL_instrument_bus2_mfspr268(unsigned int *, size_t, size_t);
8192d83a12Schristos
OPENSSL_instrument_bus2(unsigned int * out,size_t cnt,size_t max)8292d83a12Schristos size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max)
8392d83a12Schristos {
8492d83a12Schristos if (OPENSSL_ppccap_P & PPC_MFTB)
8592d83a12Schristos return OPENSSL_instrument_bus2_mftb(out, cnt, max);
8692d83a12Schristos else if (OPENSSL_ppccap_P & PPC_MFSPR268)
8792d83a12Schristos return OPENSSL_instrument_bus2_mfspr268(out, cnt, max);
8892d83a12Schristos else
8992d83a12Schristos return 0;
9092d83a12Schristos }
9192d83a12Schristos
9292d83a12Schristos #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
9392d83a12Schristos # if __GLIBC_PREREQ(2, 16)
9492d83a12Schristos # include <sys/auxv.h>
9592d83a12Schristos # define OSSL_IMPLEMENT_GETAUXVAL
96e1f0537eSchristos # elif defined(__ANDROID_API__)
97e1f0537eSchristos /* see https://developer.android.google.cn/ndk/guides/cpu-features */
98e1f0537eSchristos # if __ANDROID_API__ >= 18
99e1f0537eSchristos # include <sys/auxv.h>
100e1f0537eSchristos # define OSSL_IMPLEMENT_GETAUXVAL
101e1f0537eSchristos # endif
10292d83a12Schristos # endif
1034ec2eca9Schristos #endif
1044ec2eca9Schristos
105856fa3afSchristos #if defined(__FreeBSD__)
106856fa3afSchristos # include <sys/param.h>
107856fa3afSchristos # if __FreeBSD_version >= 1200000
108856fa3afSchristos # include <sys/auxv.h>
109856fa3afSchristos # define OSSL_IMPLEMENT_GETAUXVAL
110856fa3afSchristos
getauxval(unsigned long key)111856fa3afSchristos static unsigned long getauxval(unsigned long key)
112856fa3afSchristos {
113856fa3afSchristos unsigned long val = 0ul;
114856fa3afSchristos
115856fa3afSchristos if (elf_aux_info((int)key, &val, sizeof(val)) != 0)
116856fa3afSchristos return 0ul;
117856fa3afSchristos
118856fa3afSchristos return val;
119856fa3afSchristos }
120856fa3afSchristos # endif
121856fa3afSchristos #endif
122856fa3afSchristos
1234ec2eca9Schristos /* I wish <sys/auxv.h> was universally available */
1244ec2eca9Schristos #define HWCAP 16 /* AT_HWCAP */
1254ec2eca9Schristos #define HWCAP_PPC64 (1U << 30)
1264ec2eca9Schristos #define HWCAP_ALTIVEC (1U << 28)
1274ec2eca9Schristos #define HWCAP_FPU (1U << 27)
1284ec2eca9Schristos #define HWCAP_POWER6_EXT (1U << 9)
1294ec2eca9Schristos #define HWCAP_VSX (1U << 7)
1304ec2eca9Schristos
1314ec2eca9Schristos #define HWCAP2 26 /* AT_HWCAP2 */
1324ec2eca9Schristos #define HWCAP_VEC_CRYPTO (1U << 25)
1334ec2eca9Schristos #define HWCAP_ARCH_3_00 (1U << 23)
1344ec2eca9Schristos
1354ec2eca9Schristos # if defined(__GNUC__) && __GNUC__>=2
1364ec2eca9Schristos __attribute__ ((constructor))
1374ec2eca9Schristos # endif
OPENSSL_cpuid_setup(void)13813ce5681Schristos void OPENSSL_cpuid_setup(void)
13913ce5681Schristos {
14013ce5681Schristos char *e;
14113ce5681Schristos struct sigaction ill_oact, ill_act;
14213ce5681Schristos sigset_t oset;
14313ce5681Schristos static int trigger = 0;
14413ce5681Schristos
145d572d25fSspz if (trigger)
146d572d25fSspz return;
14713ce5681Schristos trigger = 1;
14813ce5681Schristos
1494ec2eca9Schristos if ((e = getenv("OPENSSL_ppccap"))) {
1504ec2eca9Schristos OPENSSL_ppccap_P = strtoul(e, NULL, 0);
1514ec2eca9Schristos return;
1524ec2eca9Schristos }
1534ec2eca9Schristos
1544ec2eca9Schristos OPENSSL_ppccap_P = 0;
1554ec2eca9Schristos
1564ec2eca9Schristos #if defined(_AIX)
1574ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU;
1584ec2eca9Schristos
1594ec2eca9Schristos if (sizeof(size_t) == 4) {
1604ec2eca9Schristos struct utsname uts;
1614ec2eca9Schristos # if defined(_SC_AIX_KERNEL_BITMODE)
1624ec2eca9Schristos if (sysconf(_SC_AIX_KERNEL_BITMODE) != 64)
1634ec2eca9Schristos return;
1644ec2eca9Schristos # endif
1654ec2eca9Schristos if (uname(&uts) != 0 || atoi(uts.version) < 6)
1664ec2eca9Schristos return;
1674ec2eca9Schristos }
1684ec2eca9Schristos
1694ec2eca9Schristos # if defined(__power_set)
1704ec2eca9Schristos /*
1714ec2eca9Schristos * Value used in __power_set is a single-bit 1<<n one denoting
1724ec2eca9Schristos * specific processor class. Incidentally 0xffffffff<<n can be
1734ec2eca9Schristos * used to denote specific processor and its successors.
1744ec2eca9Schristos */
1754ec2eca9Schristos if (sizeof(size_t) == 4) {
1764ec2eca9Schristos /* In 32-bit case PPC_FPU64 is always fastest [if option] */
1774ec2eca9Schristos if (__power_set(0xffffffffU<<13)) /* POWER5 and later */
1784ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU64;
1794ec2eca9Schristos } else {
1804ec2eca9Schristos /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
1814ec2eca9Schristos if (__power_set(0x1U<<14)) /* POWER6 */
1824ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU64;
1834ec2eca9Schristos }
1844ec2eca9Schristos
1854ec2eca9Schristos if (__power_set(0xffffffffU<<14)) /* POWER6 and later */
1864ec2eca9Schristos OPENSSL_ppccap_P |= PPC_ALTIVEC;
1874ec2eca9Schristos
1884ec2eca9Schristos if (__power_set(0xffffffffU<<16)) /* POWER8 and later */
1894ec2eca9Schristos OPENSSL_ppccap_P |= PPC_CRYPTO207;
1904ec2eca9Schristos
1914ec2eca9Schristos if (__power_set(0xffffffffU<<17)) /* POWER9 and later */
1924ec2eca9Schristos OPENSSL_ppccap_P |= PPC_MADD300;
1934ec2eca9Schristos
1944ec2eca9Schristos return;
1954ec2eca9Schristos # endif
1964ec2eca9Schristos #endif
1974ec2eca9Schristos
1984ec2eca9Schristos #if defined(__APPLE__) && defined(__MACH__)
1994ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU;
2004ec2eca9Schristos
2014ec2eca9Schristos {
2024ec2eca9Schristos int val;
2034ec2eca9Schristos size_t len = sizeof(val);
2044ec2eca9Schristos
2054ec2eca9Schristos if (sysctlbyname("hw.optional.64bitops", &val, &len, NULL, 0) == 0) {
2064ec2eca9Schristos if (val)
2074ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU64;
2084ec2eca9Schristos }
2094ec2eca9Schristos
2104ec2eca9Schristos len = sizeof(val);
2114ec2eca9Schristos if (sysctlbyname("hw.optional.altivec", &val, &len, NULL, 0) == 0) {
2124ec2eca9Schristos if (val)
2134ec2eca9Schristos OPENSSL_ppccap_P |= PPC_ALTIVEC;
2144ec2eca9Schristos }
2154ec2eca9Schristos
2164ec2eca9Schristos return;
2174ec2eca9Schristos }
2184ec2eca9Schristos #endif
2194ec2eca9Schristos
22092d83a12Schristos #ifdef OSSL_IMPLEMENT_GETAUXVAL
22192d83a12Schristos {
2224ec2eca9Schristos unsigned long hwcap = getauxval(HWCAP);
223380ea7e6Schristos unsigned long hwcap2 = getauxval(HWCAP2);
2244ec2eca9Schristos
2254ec2eca9Schristos if (hwcap & HWCAP_FPU) {
2264ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU;
2274ec2eca9Schristos
2284ec2eca9Schristos if (sizeof(size_t) == 4) {
2294ec2eca9Schristos /* In 32-bit case PPC_FPU64 is always fastest [if option] */
2304ec2eca9Schristos if (hwcap & HWCAP_PPC64)
2314ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU64;
2324ec2eca9Schristos } else {
2334ec2eca9Schristos /* In 64-bit case PPC_FPU64 is fastest only on POWER6 */
2344ec2eca9Schristos if (hwcap & HWCAP_POWER6_EXT)
2354ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU64;
2364ec2eca9Schristos }
2374ec2eca9Schristos }
2384ec2eca9Schristos
2394ec2eca9Schristos if (hwcap & HWCAP_ALTIVEC) {
2404ec2eca9Schristos OPENSSL_ppccap_P |= PPC_ALTIVEC;
2414ec2eca9Schristos
242380ea7e6Schristos if ((hwcap & HWCAP_VSX) && (hwcap2 & HWCAP_VEC_CRYPTO))
2434ec2eca9Schristos OPENSSL_ppccap_P |= PPC_CRYPTO207;
2444ec2eca9Schristos }
2454ec2eca9Schristos
246380ea7e6Schristos if (hwcap2 & HWCAP_ARCH_3_00) {
2474ec2eca9Schristos OPENSSL_ppccap_P |= PPC_MADD300;
2484ec2eca9Schristos }
2494ec2eca9Schristos }
25092d83a12Schristos #endif
2514ec2eca9Schristos
25213ce5681Schristos sigfillset(&all_masked);
25313ce5681Schristos sigdelset(&all_masked, SIGILL);
25413ce5681Schristos sigdelset(&all_masked, SIGTRAP);
25513ce5681Schristos #ifdef SIGEMT
25613ce5681Schristos sigdelset(&all_masked, SIGEMT);
25713ce5681Schristos #endif
25813ce5681Schristos sigdelset(&all_masked, SIGFPE);
25913ce5681Schristos sigdelset(&all_masked, SIGBUS);
26013ce5681Schristos sigdelset(&all_masked, SIGSEGV);
26113ce5681Schristos
26213ce5681Schristos memset(&ill_act, 0, sizeof(ill_act));
26313ce5681Schristos ill_act.sa_handler = ill_handler;
26413ce5681Schristos ill_act.sa_mask = all_masked;
26513ce5681Schristos
26613ce5681Schristos sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
26713ce5681Schristos sigaction(SIGILL, &ill_act, &ill_oact);
26813ce5681Schristos
26992d83a12Schristos #ifndef OSSL_IMPLEMENT_GETAUXVAL
270d4503398Srin # ifdef __NetBSD__
271d4503398Srin int error, val;
272d4503398Srin size_t len = sizeof(val);
273d4503398Srin
274d4503398Srin /*
27590ed1af2Sriastradh * If machdep.fpu_present == 0, FPU is absent and emulated by
27690ed1af2Sriastradh * software. In that case, using FPU instructions hurts rather
27790ed1af2Sriastradh * than helps performance, and the software is unlikely to run in
27890ed1af2Sriastradh * constant time so it would expose us to timing side channel
27990ed1af2Sriastradh * attacks. So don't do it!
280d4503398Srin */
281d4503398Srin error = sysctlbyname("machdep.fpu_present", &val, &len, NULL, 0);
282d4503398Srin if (error != 0 || (error == 0 && val != 0))
283d4503398Srin # endif
2844ec2eca9Schristos if (sigsetjmp(ill_jmp,1) == 0) {
2854ec2eca9Schristos OPENSSL_fpu_probe();
2864ec2eca9Schristos OPENSSL_ppccap_P |= PPC_FPU;
2874ec2eca9Schristos
288d572d25fSspz if (sizeof(size_t) == 4) {
2897fa80bafSspz # ifdef __linux
2907fa80bafSspz struct utsname uts;
2917fa80bafSspz if (uname(&uts) == 0 && strcmp(uts.machine, "ppc64") == 0)
2927fa80bafSspz # endif
293d572d25fSspz if (sigsetjmp(ill_jmp, 1) == 0) {
29413ce5681Schristos OPENSSL_ppc64_probe();
29513ce5681Schristos OPENSSL_ppccap_P |= PPC_FPU64;
29613ce5681Schristos }
297d572d25fSspz } else {
29813ce5681Schristos /*
29913ce5681Schristos * Wanted code detecting POWER6 CPU and setting PPC_FPU64
30013ce5681Schristos */
30113ce5681Schristos }
3024ec2eca9Schristos }
30313ce5681Schristos
304d572d25fSspz if (sigsetjmp(ill_jmp, 1) == 0) {
30513ce5681Schristos OPENSSL_altivec_probe();
30613ce5681Schristos OPENSSL_ppccap_P |= PPC_ALTIVEC;
3077fa80bafSspz if (sigsetjmp(ill_jmp, 1) == 0) {
3087fa80bafSspz OPENSSL_crypto207_probe();
3097fa80bafSspz OPENSSL_ppccap_P |= PPC_CRYPTO207;
3107fa80bafSspz }
31113ce5681Schristos }
31213ce5681Schristos
3134ec2eca9Schristos if (sigsetjmp(ill_jmp, 1) == 0) {
3144ec2eca9Schristos OPENSSL_madd300_probe();
3154ec2eca9Schristos OPENSSL_ppccap_P |= PPC_MADD300;
3164ec2eca9Schristos }
31792d83a12Schristos #endif
31892d83a12Schristos
31992d83a12Schristos if (sigsetjmp(ill_jmp, 1) == 0) {
32092d83a12Schristos OPENSSL_rdtsc_mftb();
32192d83a12Schristos OPENSSL_ppccap_P |= PPC_MFTB;
32292d83a12Schristos } else if (sigsetjmp(ill_jmp, 1) == 0) {
32392d83a12Schristos OPENSSL_rdtsc_mfspr268();
32492d83a12Schristos OPENSSL_ppccap_P |= PPC_MFSPR268;
32592d83a12Schristos }
3264ec2eca9Schristos
32713ce5681Schristos sigaction(SIGILL, &ill_oact, NULL);
32813ce5681Schristos sigprocmask(SIG_SETMASK, &oset, NULL);
32913ce5681Schristos }
330