Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpufeatures: map aarch64 HWCAPs to target features; add crypto #456

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cpufeatures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"
readme = "README.md"

[target.aarch64-apple-darwin.dependencies]
libc = "0.2"
libc = "0.2.95"

[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]
libc = "0.2"
libc = "0.2.95"
35 changes: 24 additions & 11 deletions cpufeatures/src/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,27 @@ macro_rules! __expand_check_macro {
// Linux `expand_check_macro`
#[cfg(target_os = "linux")]
__expand_check_macro! {
("aes", HWCAP_AES), // Enable AES support.
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
("aes", AES), // Enable AES support.
("crypto", CRYPTO), // Enable cryptographic instructions.
("sha2", SHA2), // Enable SHA1 and SHA256 support.
("sha3", SHA3), // Enable SHA512 and SHA3 support.
}

/// Linux hardware capabilities
/// Linux hardware capabilities mapped to target features.
///
/// Workaround for these being missing from certain environments (i.e. Musl)
/// See: <https://github.com/rust-lang/libc/issues/2171>
/// Note that LLVM target features are coarser grained than what Linux supports
/// and imply more capabilities under each feature. This module attempts to
/// provide that mapping accordingly.
///
/// See this issue for more info: <https://github.com/RustCrypto/utils/issues/395>
#[cfg(target_os = "linux")]
pub mod hwcaps {
pub const HWCAP_AES: libc::c_ulong = 1 << 3;
pub const HWCAP_NEON: libc::c_ulong = 1 << 12;
pub const HWCAP_SHA2: libc::c_ulong = 1 << 6;
pub const HWCAP_SHA3: libc::c_ulong = 1 << 17;
use libc::c_ulong;

pub const AES: c_ulong = libc::HWCAP_AES | libc::HWCAP_PMULL;
pub const CRYPTO: c_ulong = AES | SHA2;
pub const SHA2: c_ulong = libc::HWCAP_SHA2;
pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512;
}

// macOS `check!` macro.
Expand All @@ -101,11 +107,18 @@ macro_rules! check {
("aes") => {
true
};
("crypto") => {
true
};
("sha2") => {
true
};
("sha3") => {
unsafe { $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0") }
unsafe {
// `sha3` target feature implies SHA-512 as well
$crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha512\0")
&& $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0")
}
};
}

Expand Down