std_detect: Support run-time detection of crc/aes/sha2/crypto on arm FreeBSD

This commit is contained in:
Taiki Endo 2023-03-02 03:21:34 +09:00 committed by Amanieu d'Antras
parent 8f9ed37be7
commit ba58f91769
2 changed files with 25 additions and 4 deletions

View file

@ -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

View file

@ -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"));