diff options
-rw-r--r-- | configure.ac | 56 | ||||
-rw-r--r-- | mpi/config.links | 5 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/g10lib.h | 4 | ||||
-rw-r--r-- | src/hwf-common.h | 1 | ||||
-rw-r--r-- | src/hwf-s390x.c | 224 | ||||
-rw-r--r-- | src/hwfeatures.c | 8 |
7 files changed, 299 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac index fda74056..cf0d5e61 100644 --- a/configure.ac +++ b/configure.ac @@ -2001,6 +2001,58 @@ if test "$gcry_cv_gcc_inline_asm_ppc_arch_3_00" = "yes" ; then fi +# +# Check whether GCC inline assembler supports zSeries instructions +# +AC_CACHE_CHECK([whether GCC inline assembler supports zSeries instructions], + [gcry_cv_gcc_inline_asm_s390x], + [if test "$mpi_cpu_arch" != "s390x" || + test "$try_asm_modules" != "yes" ; then + gcry_cv_gcc_inline_asm_s390x="n/a" + else + gcry_cv_gcc_inline_asm_s390x=no + AC_COMPILE_IFELSE([AC_LANG_SOURCE( + [[typedef unsigned int u128_t __attribute__ ((mode (TI))); + unsigned int testfunc(unsigned int x, void *y, unsigned int z) + { + unsigned long fac[8]; + register unsigned long reg0 asm("0") = 0; + register unsigned long reg1 asm("1") = x; + u128_t r1 = ((u128_t)(unsigned long)y << 64) | (unsigned long)z; + u128_t r2 = 0; + u128_t r3 = 0; + asm volatile (".insn rre,0xb92e << 16, %[r1], %[r2]\n\t" + : [r1] "+a" (r1), [r2] "+a" (r2) + : "r" (reg0), "r" (reg1) + : "cc", "memory"); + asm volatile (".insn rrf,0xb929 << 16, %[r1], %[r2], %[r3], 0\n\t" + : [r1] "+a" (r1), [r2] "+a" (r2), [r3] "+a" (r3) + : "r" (reg0), "r" (reg1) + : "cc", "memory"); + reg0 = 8 - 1; + asm ("stfle %1\n\t" + : "+d" (reg0), "=Q" (fac[0]) + : + : "cc", "memory"); + asm volatile ("mvc 0(16, %0), 0(%1)\n\t" + : + : "a" (y), "a" (fac) + : "memory"); + asm volatile ("xc 0(16, %0), 0(%0)\n\t" + : + : "a" (fac) + : "memory"); + return (unsigned int)r1 ^ reg0; + } + ]])], + [gcry_cv_gcc_inline_asm_s390x=yes]) + fi]) +if test "$gcry_cv_gcc_inline_asm_s390x" = "yes" ; then + AC_DEFINE(HAVE_GCC_INLINE_ASM_S390X,1, + [Defined if inline assembler supports zSeries instructions]) +fi + + ####################################### #### Checks for library functions. #### ####################################### @@ -2962,6 +3014,10 @@ case "$mpi_cpu_arch" in AC_DEFINE(HAVE_CPU_ARCH_ARM, 1, [Defined for ARM AArch64 platforms]) GCRYPT_HWF_MODULES="libgcrypt_la-hwf-arm.lo" ;; + s390x) + AC_DEFINE(HAVE_CPU_ARCH_S390X, 1, [Defined for s390x/zSeries platforms]) + GCRYPT_HWF_MODULES="libgcrypt_la-hwf-s390x.lo" + ;; esac AC_SUBST([GCRYPT_HWF_MODULES]) diff --git a/mpi/config.links b/mpi/config.links index ce6822db..e4fc4fc4 100644 --- a/mpi/config.links +++ b/mpi/config.links @@ -246,6 +246,11 @@ case "${host}" in path="mips2" mpi_cpu_arch="mips" ;; + s390x*-*-*) + echo '/* No working assembler modules available */' >>./mpi/asm-syntax.h + path="" + mpi_cpu_arch="s390x" + ;; # Motorola 68k configurations. Let m68k mean 68020-68040. # mc68000 or mc68060 configurations need to be specified explicitly diff --git a/src/Makefile.am b/src/Makefile.am index 74772857..31aa6660 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -66,7 +66,7 @@ libgcrypt_la_SOURCES = \ hmac256.c hmac256.h context.c context.h \ ec-context.h -EXTRA_libgcrypt_la_SOURCES = hwf-x86.c hwf-arm.c hwf-ppc.c +EXTRA_libgcrypt_la_SOURCES = hwf-x86.c hwf-arm.c hwf-ppc.c hwf-s390x.c gcrypt_hwf_modules = @GCRYPT_HWF_MODULES@ diff --git a/src/g10lib.h b/src/g10lib.h index ffd71018..665c2175 100644 --- a/src/g10lib.h +++ b/src/g10lib.h @@ -246,6 +246,10 @@ char **_gcry_strtokenize (const char *string, const char *delim); #define HWF_PPC_ARCH_3_00 (1 << 23) #define HWF_PPC_ARCH_2_07 (1 << 24) +#define HWF_S390X_MSA (1 << 25) +#define HWF_S390X_MSA_4 (1 << 26) +#define HWF_S390X_MSA_8 (1 << 27) + gpg_err_code_t _gcry_disable_hw_feature (const char *name); void _gcry_detect_hw_features (void); unsigned int _gcry_get_hw_features (void); diff --git a/src/hwf-common.h b/src/hwf-common.h index 76f346e9..b10f86be 100644 --- a/src/hwf-common.h +++ b/src/hwf-common.h @@ -23,5 +23,6 @@ unsigned int _gcry_hwf_detect_x86 (void); unsigned int _gcry_hwf_detect_arm (void); unsigned int _gcry_hwf_detect_ppc (void); +unsigned int _gcry_hwf_detect_s390x (void); #endif /*HWF_COMMON_H*/ diff --git a/src/hwf-s390x.c b/src/hwf-s390x.c new file mode 100644 index 00000000..991e8c4c --- /dev/null +++ b/src/hwf-s390x.c @@ -0,0 +1,224 @@ +/* hwf-s390x.c - Detect hardware features - s390x/zSeries part + * Copyright (C) 2020 Jussi Kivilinna <jussi.kivilinna@iki.fi> + * + * This file is part of Libgcrypt. + * + * Libgcrypt is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * Libgcrypt 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see <http://www.gnu.org/licenses/>. + */ + +#include <config.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <unistd.h> +#include <errno.h> +#if defined(HAVE_SYS_AUXV_H) && (defined(HAVE_GETAUXVAL) || \ + defined(HAVE_ELF_AUX_INFO)) +#include <sys/auxv.h> +#endif + +#include "g10lib.h" +#include "hwf-common.h" + +#if !defined (__s390x__) +# error Module build for wrong CPU. +#endif + +#undef HAVE_STFLE +#ifdef HAVE_GCC_INLINE_ASM_S390X +# define HAVE_STFLE 1 +#endif + +struct feature_map_s + { + unsigned int facilities_bit; + unsigned int hwcap_flag; + unsigned int hwf_flag; + }; + +static const struct feature_map_s s390x_features[] = + { + { 17, 0, HWF_S390X_MSA }, + { 77, 0, HWF_S390X_MSA_4 }, + { 146, 0, HWF_S390X_MSA_8 }, + }; + +#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_ELF_AUX_INFO) && \ + !defined(HAVE_GETAUXVAL) && defined(AT_HWCAP) +#define HAVE_GETAUXVAL +static unsigned long getauxval(unsigned long type) +{ + unsigned long auxval = 0; + int err; + + /* FreeBSD provides 'elf_aux_info' function that does the same as + * 'getauxval' on Linux. */ + + err = elf_aux_info (type, &auxval, sizeof(auxval)); + if (err) + { + errno = err; + auxval = 0; + } + + return auxval; +} +#endif + + +#undef HAS_SYS_AT_HWCAP +#if defined(__linux__) || \ + (defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL)) +#define HAS_SYS_AT_HWCAP 1 + +struct facilities_s + { + u64 bits[3]; + }; + +#ifndef AT_HWCAP +# define AT_HWCAP 16 +#endif +#ifndef HWCAP_S390_STFLE +# define HWCAP_S390_STFLE 4 +#endif + +static int +get_hwcap(unsigned int *hwcap) +{ + struct { unsigned long a_type; unsigned long a_val; } auxv; + FILE *f; + int err = -1; + static int hwcap_initialized = 0; + static unsigned int stored_hwcap = 0; + + if (hwcap_initialized) + { + *hwcap = stored_hwcap; + return 0; + } + +#if defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL) + errno = 0; + auxv.a_val = getauxval (AT_HWCAP); + if (errno == 0) + { + stored_hwcap |= auxv.a_val; + hwcap_initialized = 1; + } + + if (hwcap_initialized && stored_hwcap) + { + *hwcap = stored_hwcap; + return 0; + } +#endif + + f = fopen("/proc/self/auxv", "r"); + if (!f) + { + *hwcap = stored_hwcap; + return -1; + } + + while (fread(&auxv, sizeof(auxv), 1, f) > 0) + { + if (auxv.a_type == AT_HWCAP) + { + stored_hwcap |= auxv.a_val; + hwcap_initialized = 1; + } + } + + if (hwcap_initialized) + err = 0; + + fclose(f); + + *hwcap = stored_hwcap; + return err; +} +#endif + +#ifdef HAVE_STFLE +static void +get_stfle(struct facilities_s *out) +{ + static int stfle_initialized = 0; + static struct facilities_s stored_facilities; + + if (!stfle_initialized) + { + register unsigned long reg0 asm("0") = DIM(stored_facilities.bits) - 1; + + asm ("stfle %1\n\t" + : "+d" (reg0), + "=Q" (stored_facilities.bits[0]) + : + : "cc", "memory"); + + stfle_initialized = 1; + } + + *out = stored_facilities; +} +#endif + +static unsigned int +detect_s390x_features(void) +{ + struct facilities_s facilities = { { 0, } }; + unsigned int hwcap = 0; + unsigned int features = 0; + unsigned int i; + +#if defined (HAS_SYS_AT_HWCAP) + if (get_hwcap(&hwcap) < 0) + return features; +#endif + + if ((hwcap & HWCAP_S390_STFLE) == 0) + return features; + +#ifdef HAVE_STFLE + get_stfle(&facilities); +#endif + + for (i = 0; i < DIM(s390x_features); i++) + { + if (s390x_features[i].hwcap_flag == 0 || + (s390x_features[i].hwcap_flag & hwcap)) + { + unsigned int idx = s390x_features[i].facilities_bit; + unsigned int u64_idx = idx / 64; + unsigned int u64_bit = 63 - (idx % 64); + + if (facilities.bits[u64_idx] & (U64_C(1) << u64_bit)) + features |= s390x_features[i].hwf_flag; + } + } + + return features; +} + +unsigned int +_gcry_hwf_detect_s390x (void) +{ + unsigned int ret = 0; + + ret |= detect_s390x_features (); + + return ret; +} diff --git a/src/hwfeatures.c b/src/hwfeatures.c index bff0f1c8..d7f2047c 100644 --- a/src/hwfeatures.c +++ b/src/hwfeatures.c @@ -70,6 +70,10 @@ static struct { HWF_PPC_VCRYPTO, "ppc-vcrypto" }, { HWF_PPC_ARCH_3_00, "ppc-arch_3_00" }, { HWF_PPC_ARCH_2_07, "ppc-arch_2_07" }, +#elif defined(HAVE_CPU_ARCH_S390X) + { HWF_S390X_MSA, "s390x-msa" }, + { HWF_S390X_MSA_4, "s390x-msa-4" }, + { HWF_S390X_MSA_8, "s390x-msa-8" }, #endif }; @@ -223,6 +227,10 @@ _gcry_detect_hw_features (void) { hw_features = _gcry_hwf_detect_ppc (); } +#elif defined (HAVE_CPU_ARCH_S390X) + { + hw_features = _gcry_hwf_detect_s390x (); + } #endif hw_features &= ~disabled_hw_features; } |