From d45a68f6d63425963cefc3360156e20f9de2ceaa Mon Sep 17 00:00:00 2001 From: tb Date: Wed, 23 Mar 2022 15:13:31 +0000 Subject: [PATCH] Start disentangling armv7 and aarch64 code arm_arch.h and armcap.c are shared between armv7 and aarch64 which results in an inscrutable #ifdef maze. Move copies of these files into arch/{arm,aarch64}/ with appropriate names and some trivial minor adjustments. ok deraadt inoguchi kettenis --- lib/libcrypto/Makefile | 3 +- lib/libcrypto/arch/aarch64/Makefile.inc | 4 +- lib/libcrypto/arch/aarch64/arm64_arch.h | 59 +++++++++++++ lib/libcrypto/arch/aarch64/arm64cap.c | 88 +++++++++++++++++++ lib/libcrypto/{ => arch/aarch64}/arm64cpuid.S | 2 +- lib/libcrypto/{ => arch/arm}/arm_arch.h | 2 +- lib/libcrypto/{ => arch/arm}/armcap.c | 2 +- lib/libcrypto/{ => arch/arm}/armv4cpuid.S | 0 8 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 lib/libcrypto/arch/aarch64/arm64_arch.h create mode 100644 lib/libcrypto/arch/aarch64/arm64cap.c rename lib/libcrypto/{ => arch/aarch64}/arm64cpuid.S (97%) rename lib/libcrypto/{ => arch/arm}/arm_arch.h (95%) rename lib/libcrypto/{ => arch/arm}/armcap.c (96%) rename lib/libcrypto/{ => arch/arm}/armv4cpuid.S (100%) diff --git a/lib/libcrypto/Makefile b/lib/libcrypto/Makefile index 00cd2e07214..15f3ba1e602 100644 --- a/lib/libcrypto/Makefile +++ b/lib/libcrypto/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.71 2022/01/14 08:38:05 tb Exp $ +# $OpenBSD: Makefile,v 1.72 2022/03/23 15:13:31 tb Exp $ LIB= crypto LIBREBUILD=y @@ -28,6 +28,7 @@ CFLAGS+= -DDSO_DLFCN -DHAVE_DLFCN_H -DHAVE_FUNOPEN CFLAGS+= -DOPENSSL_NO_HW_PADLOCK # XXX enable this? CFLAGS+= -I${LCRYPTO_SRC} +CFLAGS+= -I${LCRYPTO_SRC}/arch/${MACHINE_CPU} CFLAGS+= -I${LCRYPTO_SRC}/asn1 CFLAGS+= -I${LCRYPTO_SRC}/bio CFLAGS+= -I${LCRYPTO_SRC}/bn diff --git a/lib/libcrypto/arch/aarch64/Makefile.inc b/lib/libcrypto/arch/aarch64/Makefile.inc index d3d33e59417..48a340a6a20 100644 --- a/lib/libcrypto/arch/aarch64/Makefile.inc +++ b/lib/libcrypto/arch/aarch64/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.2 2019/07/02 19:31:28 patrick Exp $ +# $OpenBSD: Makefile.inc,v 1.3 2022/03/23 15:13:31 tb Exp $ # aarch64-specific libcrypto build rules @@ -28,4 +28,4 @@ ${f}.S: ${LCRYPTO_SRC}/${dir}/asm/${f}.pl .endfor CFLAGS+= -DOPENSSL_CPUID_OBJ -SRCS+= arm64cpuid.S armcap.c +SRCS+= arm64cpuid.S arm64cap.c diff --git a/lib/libcrypto/arch/aarch64/arm64_arch.h b/lib/libcrypto/arch/aarch64/arm64_arch.h new file mode 100644 index 00000000000..7f35acaa7db --- /dev/null +++ b/lib/libcrypto/arch/aarch64/arm64_arch.h @@ -0,0 +1,59 @@ +/* $OpenBSD: arm64_arch.h,v 1.1 2022/03/23 15:13:31 tb Exp $ */ +#ifndef __ARM_ARCH_H__ +#define __ARM_ARCH_H__ + +#if !defined(__ARM_ARCH__) +# if defined(__CC_ARM) +# define __ARM_ARCH__ __TARGET_ARCH_ARM +# if defined(__BIG_ENDIAN) +# define __ARMEB__ +# else +# define __ARMEL__ +# endif +# elif defined(__GNUC__) + /* + * Why doesn't gcc define __ARM_ARCH__? Instead it defines + * bunch of below macros. See all_architectures[] table in + * gcc/config/arm/arm.c. On a side note it defines + * __ARMEL__/__ARMEB__ for little-/big-endian. + */ +# if defined(__ARM_ARCH) +# define __ARM_ARCH__ __ARM_ARCH +# elif defined(__ARM_ARCH_8A__) +# define __ARM_ARCH__ 8 +# elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \ + defined(__ARM_ARCH_7R__)|| defined(__ARM_ARCH_7M__) || \ + defined(__ARM_ARCH_7EM__) +# define __ARM_ARCH__ 7 +# elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || \ + defined(__ARM_ARCH_6K__)|| defined(__ARM_ARCH_6M__) || \ + defined(__ARM_ARCH_6Z__)|| defined(__ARM_ARCH_6ZK__) || \ + defined(__ARM_ARCH_6T2__) +# define __ARM_ARCH__ 6 +# elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || \ + defined(__ARM_ARCH_5E__)|| defined(__ARM_ARCH_5TE__) || \ + defined(__ARM_ARCH_5TEJ__) +# define __ARM_ARCH__ 5 +# elif defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) +# define __ARM_ARCH__ 4 +# else +# error "unsupported ARM architecture" +# endif +# endif +#endif + +#if !defined(__ASSEMBLER__) +extern unsigned int OPENSSL_armcap_P; + +#define ARMV7_NEON (1<<0) +#define ARMV8_AES (1<<1) +#define ARMV8_SHA1 (1<<2) +#define ARMV8_SHA256 (1<<3) +#define ARMV8_PMULL (1<<4) +#endif + +#if defined(__OpenBSD__) +#define __STRICT_ALIGNMENT +#endif + +#endif diff --git a/lib/libcrypto/arch/aarch64/arm64cap.c b/lib/libcrypto/arch/aarch64/arm64cap.c new file mode 100644 index 00000000000..b541ac31b9f --- /dev/null +++ b/lib/libcrypto/arch/aarch64/arm64cap.c @@ -0,0 +1,88 @@ +/* $OpenBSD: arm64cap.c,v 1.1 2022/03/23 15:13:31 tb Exp $ */ +#include +#include +#include +#include +#include +#include + +#include "arm64_arch.h" + +unsigned int OPENSSL_armcap_P; + +#if __ARM_ARCH__ >= 7 +static sigset_t all_masked; + +static sigjmp_buf ill_jmp; + static void ill_handler (int sig) { siglongjmp(ill_jmp, sig); +} + +/* + * Following subroutines could have been inlined, but it's not all + * ARM compilers support inline assembler... + */ +void _armv7_neon_probe(void); +void _armv8_aes_probe(void); +void _armv8_sha1_probe(void); +void _armv8_sha256_probe(void); +void _armv8_pmull_probe(void); +#endif + +#if defined(__GNUC__) && __GNUC__>=2 +void OPENSSL_cpuid_setup(void) __attribute__((constructor)); +#endif + +void +OPENSSL_cpuid_setup(void) +{ +#if __ARM_ARCH__ >= 7 + struct sigaction ill_oact, ill_act; + sigset_t oset; +#endif + static int trigger = 0; + + if (trigger) + return; + trigger = 1; + + OPENSSL_armcap_P = 0; + +#if __ARM_ARCH__ >= 7 + sigfillset(&all_masked); + sigdelset(&all_masked, SIGILL); + sigdelset(&all_masked, SIGTRAP); + sigdelset(&all_masked, SIGFPE); + sigdelset(&all_masked, SIGBUS); + sigdelset(&all_masked, SIGSEGV); + + memset(&ill_act, 0, sizeof(ill_act)); + ill_act.sa_handler = ill_handler; + ill_act.sa_mask = all_masked; + + sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset); + sigaction(SIGILL, &ill_act, &ill_oact); + + if (sigsetjmp(ill_jmp, 1) == 0) { + _armv7_neon_probe(); + OPENSSL_armcap_P |= ARMV7_NEON; + if (sigsetjmp(ill_jmp, 1) == 0) { + _armv8_pmull_probe(); + OPENSSL_armcap_P |= ARMV8_PMULL | ARMV8_AES; + } else if (sigsetjmp(ill_jmp, 1) == 0) { + _armv8_aes_probe(); + OPENSSL_armcap_P |= ARMV8_AES; + } + if (sigsetjmp(ill_jmp, 1) == 0) { + _armv8_sha1_probe(); + OPENSSL_armcap_P |= ARMV8_SHA1; + } + if (sigsetjmp(ill_jmp, 1) == 0) { + _armv8_sha256_probe(); + OPENSSL_armcap_P |= ARMV8_SHA256; + } + } + + sigaction (SIGILL, &ill_oact, NULL); + sigprocmask(SIG_SETMASK, &oset, NULL); +#endif +} diff --git a/lib/libcrypto/arm64cpuid.S b/lib/libcrypto/arch/aarch64/arm64cpuid.S similarity index 97% rename from lib/libcrypto/arm64cpuid.S rename to lib/libcrypto/arch/aarch64/arm64cpuid.S index 5eeff91c6ea..d267ad6897b 100644 --- a/lib/libcrypto/arm64cpuid.S +++ b/lib/libcrypto/arch/aarch64/arm64cpuid.S @@ -1,4 +1,4 @@ -#include "arm_arch.h" +#include "arm64_arch.h" .text .arch armv8-a+crypto+sha3 diff --git a/lib/libcrypto/arm_arch.h b/lib/libcrypto/arch/arm/arm_arch.h similarity index 95% rename from lib/libcrypto/arm_arch.h rename to lib/libcrypto/arch/arm/arm_arch.h index 8c5115ea440..5ac3b935f1b 100644 --- a/lib/libcrypto/arm_arch.h +++ b/lib/libcrypto/arch/arm/arm_arch.h @@ -1,4 +1,4 @@ -/* $OpenBSD: arm_arch.h,v 1.10 2019/07/02 19:31:28 patrick Exp $ */ +/* $OpenBSD: arm_arch.h,v 1.1 2022/03/23 15:13:31 tb Exp $ */ #ifndef __ARM_ARCH_H__ #define __ARM_ARCH_H__ diff --git a/lib/libcrypto/armcap.c b/lib/libcrypto/arch/arm/armcap.c similarity index 96% rename from lib/libcrypto/armcap.c rename to lib/libcrypto/arch/arm/armcap.c index 8c4983280e8..e1a721b71fc 100644 --- a/lib/libcrypto/armcap.c +++ b/lib/libcrypto/arch/arm/armcap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: armcap.c,v 1.8 2019/03/13 10:18:30 patrick Exp $ */ +/* $OpenBSD: armcap.c,v 1.1 2022/03/23 15:13:31 tb Exp $ */ #include #include #include diff --git a/lib/libcrypto/armv4cpuid.S b/lib/libcrypto/arch/arm/armv4cpuid.S similarity index 100% rename from lib/libcrypto/armv4cpuid.S rename to lib/libcrypto/arch/arm/armv4cpuid.S -- 2.20.1