From cfd769e56deea99b21f49eebe460f205f4f6525d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niels=20M=C3=B6ller?= Date: Sat, 7 Nov 2020 10:35:02 +0100 Subject: [PATCH] ppc: Add altivec detection to fat builds --- ChangeLog | 9 ++++++ configure.ac | 17 ++++++++--- fat-ppc.c | 33 +++++++++++++++++++-- powerpc64/fat/chacha-core-internal-2.asm | 37 ++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 powerpc64/fat/chacha-core-internal-2.asm diff --git a/ChangeLog b/ChangeLog index 5e994dbc..16030369 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2020-11-07 Niels Möller + + * configure.ac: New command line option --enable-power-altivec. + Update asm_path logic, and add altivec to FAT_TEST_LIST. + * fat-ppc.c (get_ppc_features): Add logic to check for altivec and + vsx support, and select aither C or altivec implementation of + chacha_core. + * powerpc64/fat/chacha-core-internal-2.asm: New file. + 2020-09-25 Niels Möller * powerpc64/p7/chacha-core-internal.asm: New file. diff --git a/configure.ac b/configure.ac index ab12496f..2a47f940 100644 --- a/configure.ac +++ b/configure.ac @@ -93,6 +93,10 @@ AC_ARG_ENABLE(power-crypto-ext, AC_HELP_STRING([--enable-power-crypto-ext], [Enable POWER crypto extensions. (default=no)]),, [enable_power_crypto_ext=no]) +AC_ARG_ENABLE(power-altivec, + AC_HELP_STRING([--enable-power-altivec], [Enable POWER altivec and vsx extensions. (default=no)]),, + [enable_altivec=no]) + AC_ARG_ENABLE(mini-gmp, AC_HELP_STRING([--enable-mini-gmp], [Enable mini-gmp, used instead of libgmp.]),, [enable_mini_gmp=no]) @@ -458,13 +462,18 @@ if test "x$enable_assembler" = xyes ; then *powerpc64*) if test "$ABI" = 64 ; then GMP_ASM_POWERPC_R_REGISTERS - asm_path="powerpc64/p7 powerpc64" + asm_path="powerpc64" if test "x$enable_fat" = xyes ; then asm_path="powerpc64/fat $asm_path" OPT_NETTLE_SOURCES="fat-ppc.c $OPT_NETTLE_SOURCES" - FAT_TEST_LIST="none crypto_ext" - elif test "x$enable_power_crypto_ext" = xyes ; then - asm_path="powerpc64/p8 $asm_path" + FAT_TEST_LIST="none crypto_ext altivec" + else + if test "$enable_power_crypto_ext" = yes ; then + asm_path="powerpc64/p8 $asm_path" + fi + if test "$enable_power_altivec" = yes ; then + asm_path="powerpc64/p7 $asm_path" + fi fi fi ;; diff --git a/fat-ppc.c b/fat-ppc.c index 2bc50481..2bfd649f 100644 --- a/fat-ppc.c +++ b/fat-ppc.c @@ -66,6 +66,7 @@ struct ppc_features { int have_crypto_ext; + int have_altivec; }; #define MATCH(s, slen, literal, llen) \ @@ -76,6 +77,7 @@ get_ppc_features (struct ppc_features *features) { const char *s; features->have_crypto_ext = 0; + features->have_altivec = 0; s = secure_getenv (ENV_OVERRIDE); if (s) @@ -86,6 +88,8 @@ get_ppc_features (struct ppc_features *features) if (MATCH (s, length, "crypto_ext", 10)) features->have_crypto_ext = 1; + else if (MATCH(s, length, "altivec", 7)) + features->have_altivec = 1; if (!sep) break; s = sep + 1; @@ -95,8 +99,10 @@ get_ppc_features (struct ppc_features *features) #if defined(_AIX) && defined(__power_8_andup) features->have_crypto_ext = __power_8_andup() != 0 ? 1 : 0; #else + unsigned long hwcap = 0; unsigned long hwcap2 = 0; # if defined(__linux__) + hwcap = getauxval(AT_HWCAP); hwcap2 = getauxval(AT_HWCAP2); # elif defined(__FreeBSD__) # if __FreeBSD__ >= 12 @@ -106,8 +112,13 @@ get_ppc_features (struct ppc_features *features) sysctlbyname("hw.cpu_features2", &hwcap2, &len, NULL, 0); # endif # endif - features->have_crypto_ext = - (hwcap2 & PPC_FEATURE2_VEC_CRYPTO) == PPC_FEATURE2_VEC_CRYPTO ? 1 : 0; + features->have_crypto_ext + = ((hwcap2 & PPC_FEATURE2_VEC_CRYPTO) == PPC_FEATURE2_VEC_CRYPTO); + + /* We also need VSX instructions, mainly for load and store. */ + features->have_altivec + = ((hwcap & (PPC_FEATURE_HAS_ALTIVEC | PPC_FEATURE_HAS_VSX)) + == (PPC_FEATURE_HAS_ALTIVEC | PPC_FEATURE_HAS_VSX)); #endif } } @@ -120,6 +131,10 @@ DECLARE_FAT_FUNC(_nettle_aes_decrypt, aes_crypt_internal_func) DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, c) DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, ppc64) +DECLARE_FAT_FUNC(_nettle_chacha_core, chacha_core_func) +DECLARE_FAT_FUNC_VAR(chacha_core, chacha_core_func, c); +DECLARE_FAT_FUNC_VAR(chacha_core, chacha_core_func, altivec); + static void CONSTRUCTOR fat_init (void) { @@ -145,6 +160,16 @@ fat_init (void) _nettle_aes_encrypt_vec = _nettle_aes_encrypt_c; _nettle_aes_decrypt_vec = _nettle_aes_decrypt_c; } + if (features.have_altivec) + { + if (verbose) + fprintf (stderr, "libnettle: enabling altivec code.\n"); + _nettle_chacha_core_vec = _nettle_chacha_core_altivec; + } + else + { + _nettle_chacha_core_vec = _nettle_chacha_core_c; + } } DEFINE_FAT_FUNC(_nettle_aes_encrypt, void, @@ -160,3 +185,7 @@ DEFINE_FAT_FUNC(_nettle_aes_decrypt, void, size_t length, uint8_t *dst, const uint8_t *src), (rounds, keys, T, length, dst, src)) + +DEFINE_FAT_FUNC(_nettle_chacha_core, void, + (uint32_t *dst, const uint32_t *src, unsigned rounds), + (dst, src, rounds)) diff --git a/powerpc64/fat/chacha-core-internal-2.asm b/powerpc64/fat/chacha-core-internal-2.asm new file mode 100644 index 00000000..35c059ea --- /dev/null +++ b/powerpc64/fat/chacha-core-internal-2.asm @@ -0,0 +1,37 @@ +C powerpc64/fat/chacha-core-internal-2.asm + + +ifelse(` + Copyright (C) 2020 Niels Möller + + This file is part of GNU Nettle. + + GNU Nettle is free software: you can redistribute it and/or + modify it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at your + option) any later version. + + or both in parallel, as here. + + GNU Nettle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see http://www.gnu.org/licenses/. +') + +dnl PROLOGUE(_nettle_chacha_core) picked up by configure + +define(`fat_transform', `$1_altivec') +include_src(`powerpc64/p7/chacha-core-internal.asm') -- 2.47.2