Trim some redundant Arm feature detection files

cpu_arm_openbsd.c is the same as OPENSSL_STATIC_ARMCAP.
cpu_aarch64_freebsd.c is the same as cpu_aarch64_sysreg.c. (The FreeBSD
one was using the macros in their headers, but those macros expand to
the same inline assembly.)

Also send ANDROID_BAREMETAL + 32-bit Arm to OPENSSL_STATIC_ARMCAP. This
way we can remove OPENSSL_STATIC_ARMCAP from the Android baremetal build
without having to chase down constraining it to aarch64. See
b/291106677#comment6

Update-Note: This is a slight change to the OpenBSD build. Previously,
we assumed OpenBSD on 32-bit Arm implies NEON. Now, we pick it up from
the __ARM_NEON define from ACLE, i.e. whether the compiler has been told
that NEON is available. (This comes from -march or other options.) Doing
that is desirable anyway: if NEON is in your baseline, you should tell
your compiler so it can vectorize loops.

Change-Id: Icd43a2b56bb6e3f04f0fed996ae750fba65e3312
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/62066
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index c7eb598..07d0ee3 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -127,14 +127,12 @@
   cipher_extra/tls_cbc.c
   conf/conf.c
   cpu_aarch64_apple.c
-  cpu_aarch64_freebsd.c
   cpu_aarch64_openbsd.c
   cpu_aarch64_fuchsia.c
   cpu_aarch64_linux.c
   cpu_aarch64_sysreg.c
   cpu_aarch64_win.c
   cpu_arm_freebsd.c
-  cpu_arm_openbsd.c
   cpu_arm_linux.c
   cpu_arm.c
   cpu_intel.c
diff --git a/crypto/cpu_aarch64_freebsd.c b/crypto/cpu_aarch64_freebsd.c
deleted file mode 100644
index 9910261..0000000
--- a/crypto/cpu_aarch64_freebsd.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/* Copyright (c) 2022, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#include "internal.h"
-
-#if defined(OPENSSL_AARCH64) && defined(OPENSSL_FREEBSD) && \
-    !defined(OPENSSL_STATIC_ARMCAP)
-
-#include <machine/armreg.h>
-#include <sys/types.h>
-
-#include <openssl/arm_arch.h>
-
-
-// ID_AA64ISAR0_*_VAL are defined starting FreeBSD 13.0. When FreeBSD
-// 12.x is out of support, these compatibility macros can be removed.
-
-#ifndef ID_AA64ISAR0_AES_VAL
-#define ID_AA64ISAR0_AES_VAL ID_AA64ISAR0_AES
-#endif
-#ifndef ID_AA64ISAR0_SHA1_VAL
-#define ID_AA64ISAR0_SHA1_VAL ID_AA64ISAR0_SHA1
-#endif
-#ifndef ID_AA64ISAR0_SHA2_VAL
-#define ID_AA64ISAR0_SHA2_VAL ID_AA64ISAR0_SHA2
-#endif
-
-void OPENSSL_cpuid_setup(void) {
-  uint64_t id_aa64isar0 = READ_SPECIALREG(id_aa64isar0_el1);
-
-  OPENSSL_armcap_P |= ARMV7_NEON;
-
-  if (ID_AA64ISAR0_AES_VAL(id_aa64isar0) >= ID_AA64ISAR0_AES_BASE) {
-    OPENSSL_armcap_P |= ARMV8_AES;
-  }
-  if (ID_AA64ISAR0_AES_VAL(id_aa64isar0) >= ID_AA64ISAR0_AES_PMULL) {
-    OPENSSL_armcap_P |= ARMV8_PMULL;
-  }
-  if (ID_AA64ISAR0_SHA1_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA1_BASE) {
-    OPENSSL_armcap_P |= ARMV8_SHA1;
-  }
-  if (ID_AA64ISAR0_SHA2_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA2_BASE) {
-    OPENSSL_armcap_P |= ARMV8_SHA256;
-  }
-  if (ID_AA64ISAR0_SHA2_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA2_512) {
-    OPENSSL_armcap_P |= ARMV8_SHA512;
-  }
-}
-
-#endif  // OPENSSL_AARCH64 && OPENSSL_FREEBSD && !OPENSSL_STATIC_ARMCAP
diff --git a/crypto/cpu_aarch64_sysreg.c b/crypto/cpu_aarch64_sysreg.c
index 0750501..6d4a0c8 100644
--- a/crypto/cpu_aarch64_sysreg.c
+++ b/crypto/cpu_aarch64_sysreg.c
@@ -14,8 +14,11 @@
 
 #include "internal.h"
 
+// While Arm system registers are normally not available to userspace, FreeBSD
+// expects userspace to simply read them. It traps the reads and fills in CPU
+// capabilities.
 #if defined(OPENSSL_AARCH64) && !defined(OPENSSL_STATIC_ARMCAP) && \
-    defined(ANDROID_BAREMETAL)
+    (defined(ANDROID_BAREMETAL) || defined(OPENSSL_FREEBSD))
 
 #include <openssl/arm_arch.h>
 
@@ -86,4 +89,5 @@
 
 void OPENSSL_cpuid_setup(void) { OPENSSL_armcap_P |= read_armcap(); }
 
-#endif  // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP && ANDROID_BAREMETAL
+#endif  // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP &&
+        // (ANDROID_BAREMETAL || OPENSSL_FREEBSD)
diff --git a/crypto/cpu_arm_openbsd.c b/crypto/cpu_arm_openbsd.c
deleted file mode 100644
index cd11ceb..0000000
--- a/crypto/cpu_arm_openbsd.c
+++ /dev/null
@@ -1,30 +0,0 @@
-/* Copyright (c) 2023, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#include "internal.h"
-
-#if defined(OPENSSL_ARM) && defined(OPENSSL_OPENBSD) && \
-    !defined(OPENSSL_STATIC_ARMCAP)
-
-#include <openssl/arm_arch.h>
-
-
-void OPENSSL_cpuid_setup(void) {
-  // OpenBSD does not support arm32 machines without NEON
-  OPENSSL_armcap_P |= ARMV7_NEON;
-
-  // OpenBSD does not support v8 features on non aarch64
-}
-
-#endif  // OPENSSL_ARM && OPENSSL_OPENBSD && !OPENSSL_STATIC_ARMCAP
diff --git a/crypto/internal.h b/crypto/internal.h
index 13d3f6f..6f5a9e0 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -1355,9 +1355,13 @@
 
 extern uint32_t OPENSSL_armcap_P;
 
-#if defined(OPENSSL_APPLE) && defined(OPENSSL_ARM)
-// We do not detect any features at runtime for Apple's 32-bit ARM platforms. On
-// 64-bit ARM, we detect some post-ARMv8.0 features.
+// We do not detect any features at runtime on several 32-bit Arm platforms.
+// Apple platforms and OpenBSD require NEON and moved to 64-bit to pick up Armv8
+// extensions. Android baremetal does not aim to support 32-bit Arm at all, but
+// it simplifies things to make it build.
+#if defined(OPENSSL_ARM) && !defined(OPENSSL_STATIC_ARMCAP) && \
+    (defined(OPENSSL_APPLE) || defined(OPENSSL_OPENBSD) ||     \
+     defined(ANDROID_BAREMETAL))
 #define OPENSSL_STATIC_ARMCAP
 #endif