Skip to content

Commit fd95867

Browse files
authored
cpufeatures: fix access to hwcaps on Linux/aarch64 (#399)
As in #398, the `libc` symbol is still being emitted from a macro, in this case to reference the individual hwcaps. This commit re-exports them all from the `cpufeatures::aarch64` module and has the macro reference them that way.
1 parent d3210e8 commit fd95867

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

cpufeatures/src/aarch64.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! unprivileged userspace code, so this implementation relies on OS-specific
55
//! APIs for feature detection.
66
7+
// Evaluate the given `$body` expression any of the supplied target features
8+
// are not enabled. Otherwise returns true.
79
#[macro_export]
810
#[doc(hidden)]
911
macro_rules! __unless_target_features {
@@ -18,6 +20,7 @@ macro_rules! __unless_target_features {
1820
};
1921
}
2022

23+
// Linux runtime detection of target CPU features using `getauxval`.
2124
#[cfg(target_os = "linux")]
2225
#[macro_export]
2326
#[doc(hidden)]
@@ -34,6 +37,7 @@ pub fn getauxval_hwcap() -> u64 {
3437
unsafe { libc::getauxval(libc::AT_HWCAP) }
3538
}
3639

40+
// MacOS runtime detection of target CPU features using `sysctlbyname`.
3741
#[cfg(target_os = "macos")]
3842
#[macro_export]
3943
#[doc(hidden)]
@@ -43,39 +47,43 @@ macro_rules! __detect_target_features {
4347
}};
4448
}
4549

46-
/// Linux `expand_check_macro`
50+
// Linux `expand_check_macro`
4751
#[cfg(target_os = "linux")]
4852
macro_rules! __expand_check_macro {
49-
($(($name:tt, $hwcap:expr)),* $(,)?) => {
53+
($(($name:tt, $hwcap:ident)),* $(,)?) => {
54+
$(
55+
pub use libc::$hwcap;
56+
)*
57+
5058
#[macro_export]
5159
#[doc(hidden)]
5260
macro_rules! check {
5361
$(
54-
($hwcaps:expr, $name) => { (($hwcaps & libc::$hwcap) != 0) };
62+
($hwcaps:expr, $name) => { (($hwcaps & $crate::aarch64::$hwcap) != 0) };
5563
)*
5664
}
5765
};
5866
}
5967

60-
/// Linux `expand_check_macro`
68+
// Linux `expand_check_macro`
6169
#[cfg(target_os = "linux")]
6270
__expand_check_macro! {
6371
("aes", HWCAP_AES), // Enable AES support.
6472
("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support.
6573
("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support.
6674
}
6775

68-
/// macOS `check!` macro.
69-
///
70-
/// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
71-
/// be present on all Apple ARM64 hardware.
72-
///
73-
/// Newer CPU instructions now have nodes within sysctl's `hw.optional`
74-
/// namespace, however the ones that do not can safely be assumed to be
75-
/// present on all Apple ARM64 devices, now and for the foreseeable future.
76-
///
77-
/// See discussion on this issue for more information:
78-
/// <https://github.com/RustCrypto/utils/issues/378>
76+
// macOS `check!` macro.
77+
//
78+
// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
79+
// be present on all Apple ARM64 hardware.
80+
//
81+
// Newer CPU instructions now have nodes within sysctl's `hw.optional`
82+
// namespace, however the ones that do not can safely be assumed to be
83+
// present on all Apple ARM64 devices, now and for the foreseeable future.
84+
//
85+
// See discussion on this issue for more information:
86+
// <https://github.com/RustCrypto/utils/issues/378>
7987
#[cfg(target_os = "macos")]
8088
#[macro_export]
8189
#[doc(hidden)]

cpufeatures/src/x86.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
//! Portable, `no_std`-friendly implementation that relies on the x86 `CPUID`
44
//! instruction for feature detection.
55
6+
// Evaluate the given `$body` expression any of the supplied target features
7+
// are not enabled. Otherwise returns true.
8+
//
9+
// The `$body` expression is not evaluated on SGX targets, and returns false
10+
// on these targets unless *all* supplied target features are enabled.
611
#[macro_export]
712
#[doc(hidden)]
813
macro_rules! __unless_target_features {
@@ -22,6 +27,7 @@ macro_rules! __unless_target_features {
2227
}};
2328
}
2429

30+
// Use CPUID to detect the presence of all supplied target features.
2531
#[macro_export]
2632
#[doc(hidden)]
2733
macro_rules! __detect_target_features {

0 commit comments

Comments
 (0)