[PATCH 1/3] hwf-arm: add ARM HW feature detection support for MacOS
Jussi Kivilinna
jussi.kivilinna at iki.fi
Sat Jul 16 18:00:03 CEST 2022
* configure.ac: Add detection for header 'sys/sysctl.h' and system
function 'sysctlbyname'.
* src/hwf-arm.c (HAS_APPLE_SYSCTLBYNAME)
(detect_arm_apple_sysctlbyname): New.
(detect_arm_hwf_by_toolchain) [__ARM_FEATURE_CRYPTO]: Also check for
ENABLE_ARM_CRYPTO_SUPPORT.
(_gcry_hwf_detect_arm) [HAS_APPLE_SYSCTLBYNAME]: Check HWFs with
'detect_arm_apple_sysctlbyname' function.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
configure.ac | 4 +--
src/hwf-arm.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 69 insertions(+), 4 deletions(-)
diff --git a/configure.ac b/configure.ac
index 946659c5..a907e085 100644
--- a/configure.ac
+++ b/configure.ac
@@ -806,7 +806,7 @@ AC_SEARCH_LIBS(setsockopt, [nsl])
#### Checks for header files. ####
##################################
-AC_CHECK_HEADERS(unistd.h sys/auxv.h sys/random.h)
+AC_CHECK_HEADERS(unistd.h sys/auxv.h sys/random.h sys/sysctl.h)
##########################################
@@ -2211,7 +2211,7 @@ AC_CHECK_FUNCS(strtoul memmove stricmp atexit raise)
AC_CHECK_FUNCS(strerror rand mmap getpagesize sysconf waitpid wait4)
AC_CHECK_FUNCS(gettimeofday getrusage gethrtime clock_gettime syslog)
AC_CHECK_FUNCS(syscall fcntl ftruncate flockfile getauxval elf_aux_info)
-AC_CHECK_FUNCS(explicit_bzero explicit_memset getentropy)
+AC_CHECK_FUNCS(explicit_bzero explicit_memset getentropy sysctlbyname)
GNUPG_CHECK_MLOCK
diff --git a/src/hwf-arm.c b/src/hwf-arm.c
index 70d375b2..a0205ee1 100644
--- a/src/hwf-arm.c
+++ b/src/hwf-arm.c
@@ -1,5 +1,5 @@
/* hwf-arm.c - Detect hardware features - ARM part
- * Copyright (C) 2013,2019 Jussi Kivilinna <jussi.kivilinna at iki.fi>
+ * Copyright (C) 2013,2019,2022 Jussi Kivilinna <jussi.kivilinna at iki.fi>
*
* This file is part of Libgcrypt.
*
@@ -28,6 +28,10 @@
defined(HAVE_ELF_AUX_INFO))
#include <sys/auxv.h>
#endif
+#if defined(__APPLE__) && defined(HAVE_SYS_SYSCTL_H) && \
+ defined(HAVE_SYSCTLBYNAME)
+#include <sys/sysctl.h>
+#endif
#include "g10lib.h"
#include "hwf-common.h"
@@ -385,6 +389,63 @@ detect_arm_proc_cpuinfo(unsigned int *broken_hwfs)
#endif /* __linux__ */
+
+#undef HAS_APPLE_SYSCTLBYNAME
+#if defined(__APPLE__) && defined(HAVE_SYS_SYSCTL_H) && \
+ defined(HAVE_SYSCTLBYNAME)
+#define HAS_APPLE_SYSCTLBYNAME 1
+
+static unsigned int
+detect_arm_apple_sysctlbyname (void)
+{
+ static const struct
+ {
+ const char *feat_name;
+ unsigned int hwf_flag;
+ } hw_optional_arm_features[] =
+ {
+#ifdef ENABLE_NEON_SUPPORT
+ { "hw.optional.neon", HWF_ARM_NEON },
+ { "hw.optional.AdvSIMD", HWF_ARM_NEON },
+#endif
+#ifdef ENABLE_ARM_CRYPTO_SUPPORT
+ { "hw.optional.arm.FEAT_AES", HWF_ARM_AES },
+ { "hw.optional.arm.FEAT_SHA1", HWF_ARM_SHA1 },
+ { "hw.optional.arm.FEAT_SHA256", HWF_ARM_SHA2 },
+ { "hw.optional.arm.FEAT_PMULL", HWF_ARM_PMULL },
+ { "hw.optional.arm.FEAT_SHA3", HWF_ARM_SHA3 },
+ { "hw.optional.armv8_2_sha3", HWF_ARM_SHA3 },
+ { "hw.optional.arm.FEAT_SHA512", HWF_ARM_SHA512 },
+ { "hw.optional.armv8_2_sha512", HWF_ARM_SHA512 },
+#endif
+ };
+ unsigned int i;
+ unsigned int hwf = 0;
+
+ for (i = 0; i < DIM(hw_optional_arm_features); i++)
+ {
+ const char *name = hw_optional_arm_features[i].feat_name;
+ int sysctl_value = 0;
+ size_t value_size = sizeof(sysctl_value);
+
+ if (sysctlbyname (name, &sysctl_value, &value_size, NULL, 0) != 0)
+ continue;
+
+ if (value_size != sizeof(sysctl_value))
+ continue;
+
+ if (sysctl_value == 1)
+ {
+ hwf |= hw_optional_arm_features[i].hwf_flag;
+ }
+ }
+
+ return hwf;
+}
+
+#endif /* __APPLE__ */
+
+
static unsigned int
detect_arm_hwf_by_toolchain (void)
{
@@ -414,7 +475,7 @@ detect_arm_hwf_by_toolchain (void)
#endif /* __ARM_NEON */
-#if defined(__ARM_FEATURE_CRYPTO)
+#if defined(__ARM_FEATURE_CRYPTO) && defined(ENABLE_ARM_CRYPTO_SUPPORT)
/* ARMv8 crypto extensions include support for PMULL, AES, SHA1 and SHA2
* instructions. */
ret |= HWF_ARM_PMULL;
@@ -464,6 +525,10 @@ _gcry_hwf_detect_arm (void)
ret |= detect_arm_proc_cpuinfo (&broken_hwfs);
#endif
+#if defined (HAS_APPLE_SYSCTLBYNAME)
+ ret |= detect_arm_apple_sysctlbyname ();
+#endif
+
ret |= detect_arm_hwf_by_toolchain ();
ret &= ~broken_hwfs;
--
2.34.1
More information about the Gcrypt-devel
mailing list