From ba58f917696d4ca2a18ab01177b19c35c3bc8b91 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 2 Mar 2023 03:21:34 +0900 Subject: [PATCH] std_detect: Support run-time detection of crc/aes/sha2/crypto on arm FreeBSD --- .../std_detect/src/detect/os/freebsd/arm.rs | 22 +++++++++++++++++-- .../crates/std_detect/tests/cpu-detection.rs | 7 ++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs b/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs index 4c9d763b446c..97ede1d26b7e 100644 --- a/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs +++ b/library/stdarch/crates/std_detect/src/detect/os/freebsd/arm.rs @@ -3,6 +3,15 @@ use super::auxvec; use crate::detect::{cache, Feature}; +// Defined in machine/elf.h. +// https://github.com/freebsd/freebsd-src/blob/deb63adf945d446ed91a9d84124c71f15ae571d1/sys/arm/include/elf.h +const HWCAP_NEON: usize = 0x00001000; +const HWCAP2_AES: usize = 0x00000001; +const HWCAP2_PMULL: usize = 0x00000002; +const HWCAP2_SHA1: usize = 0x00000004; +const HWCAP2_SHA2: usize = 0x00000008; +const HWCAP2_CRC32: usize = 0x00000010; + /// Try to read the features from the auxiliary vector pub(crate) fn detect_features() -> cache::Initializer { let mut value = cache::Initializer::default(); @@ -13,8 +22,17 @@ pub(crate) fn detect_features() -> cache::Initializer { }; if let Ok(auxv) = auxvec::auxv() { - enable_feature(&mut value, Feature::neon, auxv.hwcap & 0x00001000 != 0); - enable_feature(&mut value, Feature::pmull, auxv.hwcap2 & 0x00000002 != 0); + enable_feature(&mut value, Feature::neon, auxv.hwcap & HWCAP_NEON != 0); + let pmull = auxv.hwcap2 & HWCAP2_PMULL != 0; + enable_feature(&mut value, Feature::pmull, pmull); + enable_feature(&mut value, Feature::crc, auxv.hwcap2 & HWCAP2_CRC32 != 0); + let aes = auxv.hwcap2 & HWCAP2_AES != 0; + enable_feature(&mut value, Feature::aes, aes); + // SHA2 requires SHA1 & SHA2 features + let sha1 = auxv.hwcap2 & HWCAP2_SHA1 != 0; + let sha2 = auxv.hwcap2 & HWCAP2_SHA2 != 0; + enable_feature(&mut value, Feature::sha2, sha1 && sha2); + enable_feature(&mut value, Feature::crypto, aes && pmull && sha1 && sha2); return value; } value diff --git a/library/stdarch/crates/std_detect/tests/cpu-detection.rs b/library/stdarch/crates/std_detect/tests/cpu-detection.rs index e0ba88f06147..eb3a3e409402 100644 --- a/library/stdarch/crates/std_detect/tests/cpu-detection.rs +++ b/library/stdarch/crates/std_detect/tests/cpu-detection.rs @@ -20,8 +20,11 @@ fn all() { } #[test] -#[cfg(all(target_arch = "arm", any(target_os = "linux", target_os = "android")))] -fn arm_linux() { +#[cfg(all( + target_arch = "arm", + any(target_os = "linux", target_os = "android", target_os = "freebsd"), +))] +fn arm_linux_or_freebsd() { println!("neon: {}", is_arm_feature_detected!("neon")); println!("pmull: {}", is_arm_feature_detected!("pmull")); println!("crc: {}", is_arm_feature_detected!("crc"));