Skip to content

Commit e536969

Browse files
taiki-eAmanieu
authored andcommitted
std_detect: Support run-time detection of crc/aes/sha2/crypto on arm FreeBSD
1 parent 0e04692 commit e536969

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

crates/std_detect/src/detect/os/freebsd/arm.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
use super::auxvec;
44
use crate::detect::{cache, Feature};
55

6+
// Defined in machine/elf.h.
7+
// https://github.com/freebsd/freebsd-src/blob/deb63adf945d446ed91a9d84124c71f15ae571d1/sys/arm/include/elf.h
8+
const HWCAP_NEON: usize = 0x00001000;
9+
const HWCAP2_AES: usize = 0x00000001;
10+
const HWCAP2_PMULL: usize = 0x00000002;
11+
const HWCAP2_SHA1: usize = 0x00000004;
12+
const HWCAP2_SHA2: usize = 0x00000008;
13+
const HWCAP2_CRC32: usize = 0x00000010;
14+
615
/// Try to read the features from the auxiliary vector
716
pub(crate) fn detect_features() -> cache::Initializer {
817
let mut value = cache::Initializer::default();
@@ -13,8 +22,17 @@ pub(crate) fn detect_features() -> cache::Initializer {
1322
};
1423

1524
if let Ok(auxv) = auxvec::auxv() {
16-
enable_feature(&mut value, Feature::neon, auxv.hwcap & 0x00001000 != 0);
17-
enable_feature(&mut value, Feature::pmull, auxv.hwcap2 & 0x00000002 != 0);
25+
enable_feature(&mut value, Feature::neon, auxv.hwcap & HWCAP_NEON != 0);
26+
let pmull = auxv.hwcap2 & HWCAP2_PMULL != 0;
27+
enable_feature(&mut value, Feature::pmull, pmull);
28+
enable_feature(&mut value, Feature::crc, auxv.hwcap2 & HWCAP2_CRC32 != 0);
29+
let aes = auxv.hwcap2 & HWCAP2_AES != 0;
30+
enable_feature(&mut value, Feature::aes, aes);
31+
// SHA2 requires SHA1 & SHA2 features
32+
let sha1 = auxv.hwcap2 & HWCAP2_SHA1 != 0;
33+
let sha2 = auxv.hwcap2 & HWCAP2_SHA2 != 0;
34+
enable_feature(&mut value, Feature::sha2, sha1 && sha2);
35+
enable_feature(&mut value, Feature::crypto, aes && pmull && sha1 && sha2);
1836
return value;
1937
}
2038
value

crates/std_detect/tests/cpu-detection.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ fn all() {
2020
}
2121

2222
#[test]
23-
#[cfg(all(target_arch = "arm", any(target_os = "linux", target_os = "android")))]
24-
fn arm_linux() {
23+
#[cfg(all(
24+
target_arch = "arm",
25+
any(target_os = "linux", target_os = "android", target_os = "freebsd"),
26+
))]
27+
fn arm_linux_or_freebsd() {
2528
println!("neon: {}", is_arm_feature_detected!("neon"));
2629
println!("pmull: {}", is_arm_feature_detected!("pmull"));
2730
println!("crc: {}", is_arm_feature_detected!("crc"));

0 commit comments

Comments
 (0)