Skip to content

Commit 0f4ccff

Browse files
authored
cpufeatures: map aarch64 HWCAPs to target features; add crypto (#456)
Closes #395 The HWCAPs provided by Linux are finer-grained than LLVM target features. This crate is trying to detect the latter, so we need to group together these finer grained features detected via HWCAPs. This commit attempts to group together finer grained HWCAPs into coarser grained target features, ensuring all of the relevant HWCAPs for a given target feature are available. Additionally, this adds support for the `crypto` target feature, which implies `aes` (and with it PMULL), along with `sha2` and `neon` (although the latter is always present on `aarch64`)
1 parent 863f59d commit 0f4ccff

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

Diff for: cpufeatures/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ edition = "2018"
1515
readme = "README.md"
1616

1717
[target.aarch64-apple-darwin.dependencies]
18-
libc = "0.2"
18+
libc = "0.2.95"
1919

2020
[target.'cfg(all(target_arch = "aarch64", target_os = "linux"))'.dependencies]
21-
libc = "0.2"
21+
libc = "0.2.95"

Diff for: cpufeatures/src/aarch64.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,27 @@ macro_rules! __expand_check_macro {
6666
// Linux `expand_check_macro`
6767
#[cfg(target_os = "linux")]
6868
__expand_check_macro! {
69-
("aes", HWCAP_AES), // Enable AES support.
70-
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
71-
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
69+
("aes", AES), // Enable AES support.
70+
("crypto", CRYPTO), // Enable cryptographic instructions.
71+
("sha2", SHA2), // Enable SHA1 and SHA256 support.
72+
("sha3", SHA3), // Enable SHA512 and SHA3 support.
7273
}
7374

74-
/// Linux hardware capabilities
75+
/// Linux hardware capabilities mapped to target features.
7576
///
76-
/// Workaround for these being missing from certain environments (i.e. Musl)
77-
/// See: <https://github.com/rust-lang/libc/issues/2171>
77+
/// Note that LLVM target features are coarser grained than what Linux supports
78+
/// and imply more capabilities under each feature. This module attempts to
79+
/// provide that mapping accordingly.
80+
///
81+
/// See this issue for more info: <https://github.com/RustCrypto/utils/issues/395>
7882
#[cfg(target_os = "linux")]
7983
pub mod hwcaps {
80-
pub const HWCAP_AES: libc::c_ulong = 1 << 3;
81-
pub const HWCAP_NEON: libc::c_ulong = 1 << 12;
82-
pub const HWCAP_SHA2: libc::c_ulong = 1 << 6;
83-
pub const HWCAP_SHA3: libc::c_ulong = 1 << 17;
84+
use libc::c_ulong;
85+
86+
pub const AES: c_ulong = libc::HWCAP_AES | libc::HWCAP_PMULL;
87+
pub const CRYPTO: c_ulong = AES | SHA2;
88+
pub const SHA2: c_ulong = libc::HWCAP_SHA2;
89+
pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512;
8490
}
8591

8692
// macOS `check!` macro.
@@ -101,11 +107,18 @@ macro_rules! check {
101107
("aes") => {
102108
true
103109
};
110+
("crypto") => {
111+
true
112+
};
104113
("sha2") => {
105114
true
106115
};
107116
("sha3") => {
108-
unsafe { $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0") }
117+
unsafe {
118+
// `sha3` target feature implies SHA-512 as well
119+
$crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha512\0")
120+
&& $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0")
121+
}
109122
};
110123
}
111124

0 commit comments

Comments
 (0)