|
| 1 | +//! ARM64 CPU feature detection support. |
| 2 | +//! |
| 3 | +//! Unfortunately ARM instructions to detect CPU features cannot be called from |
| 4 | +//! unprivileged userspace code, so this implementation relies on OS-specific |
| 5 | +//! APIs for feature detection. |
| 6 | +
|
| 7 | +/// Create module with CPU feature detection code. |
| 8 | +#[macro_export] |
| 9 | +macro_rules! new { |
| 10 | + ($mod_name:ident, $($tf:tt),+ $(,)? ) => { |
| 11 | + mod $mod_name { |
| 12 | + use core::sync::atomic::{AtomicU8, Ordering::Relaxed}; |
| 13 | + |
| 14 | + const UNINIT: u8 = u8::max_value(); |
| 15 | + static STORAGE: AtomicU8 = AtomicU8::new(UNINIT); |
| 16 | + |
| 17 | + /// Initialization token |
| 18 | + #[derive(Copy, Clone, Debug)] |
| 19 | + pub struct InitToken(()); |
| 20 | + |
| 21 | + impl InitToken { |
| 22 | + /// Get initialized value |
| 23 | + #[inline(always)] |
| 24 | + pub fn get(&self) -> bool { |
| 25 | + #[cfg(not(all($(target_feature=$tf, )*)))] |
| 26 | + let res = STORAGE.load(Relaxed) == 1; |
| 27 | + #[cfg(all($(target_feature=$tf, )*))] |
| 28 | + let res = true; |
| 29 | + res |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Initialize underlying storage if needed and get |
| 34 | + /// stored value and initialization token. |
| 35 | + #[inline] |
| 36 | + pub fn init_get() -> (InitToken, bool) { |
| 37 | + #[cfg(not(all($(target_feature=$tf, )*)))] |
| 38 | + let res = { |
| 39 | + // Relaxed ordering is fine, as we only have a single atomic variable. |
| 40 | + let val = STORAGE.load(Relaxed); |
| 41 | + if val == UNINIT { |
| 42 | + let res = { |
| 43 | + #[cfg(target_os = "linux")] |
| 44 | + { |
| 45 | + let hwcaps = unsafe { libc::getauxval(libc::AT_HWCAP) }; |
| 46 | + $(cpufeatures::check!(hwcaps, $tf) & )+ true |
| 47 | + } |
| 48 | + |
| 49 | + #[cfg(target_os = "macos")] |
| 50 | + { |
| 51 | + $(cpufeatures::check!($tf) & )+ true |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + STORAGE.store(res as u8, Relaxed); |
| 56 | + res |
| 57 | + } else { |
| 58 | + val == 1 |
| 59 | + } |
| 60 | + }; |
| 61 | + #[cfg(all($(target_feature=$tf, )*))] |
| 62 | + let res = true; |
| 63 | + |
| 64 | + (InitToken(()), res) |
| 65 | + } |
| 66 | + |
| 67 | + /// Initialize underlying storage if needed and get |
| 68 | + /// initialization token. |
| 69 | + #[inline] |
| 70 | + pub fn init() -> InitToken { |
| 71 | + init_get().0 |
| 72 | + } |
| 73 | + |
| 74 | + /// Initialize underlying storage if needed and get |
| 75 | + /// stored value. |
| 76 | + #[inline] |
| 77 | + pub fn get() -> bool { |
| 78 | + init_get().1 |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +/// Linux `expand_check_macro` |
| 85 | +#[cfg(target_os = "linux")] |
| 86 | +macro_rules! expand_check_macro { |
| 87 | + ($(($name:tt, $hwcap:expr)),* $(,)?) => { |
| 88 | + #[macro_export] |
| 89 | + #[doc(hidden)] |
| 90 | + macro_rules! check { |
| 91 | + $( |
| 92 | + ($hwcaps:expr, $name) => { (($hwcaps & libc::$hwcap) != 0) }; |
| 93 | + )* |
| 94 | + } |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +/// Linux `expand_check_macro` |
| 99 | +#[cfg(target_os = "linux")] |
| 100 | +expand_check_macro! { |
| 101 | + ("aes", HWCAP_AES), // Enable AES support. |
| 102 | + ("sha2", HWCAP_SHA2), // Enable SHA1 and SHA256 support. |
| 103 | + ("sha3", HWCAP_SHA3), // Enable SHA512 and SHA3 support. |
| 104 | +} |
| 105 | + |
| 106 | +/// macOS `check!` macro. |
| 107 | +/// |
| 108 | +/// NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to |
| 109 | +/// be present on all Apple ARM64 hardware. |
| 110 | +/// |
| 111 | +/// Newer CPU instructions now have nodes within sysctl's `hw.optional` |
| 112 | +/// namespace, however the ones that do not can safely be assumed to be |
| 113 | +/// present on all Apple ARM64 devices, now and for the foreseeable future. |
| 114 | +/// |
| 115 | +/// See discussion on this issue for more information: |
| 116 | +/// <https://github.com/RustCrypto/utils/issues/378> |
| 117 | +#[cfg(target_os = "macos")] |
| 118 | +#[macro_export] |
| 119 | +#[doc(hidden)] |
| 120 | +macro_rules! check { |
| 121 | + ("aes") => { |
| 122 | + true |
| 123 | + }; |
| 124 | + ("sha2") => { |
| 125 | + true |
| 126 | + }; |
| 127 | + ("sha3") => { |
| 128 | + unsafe { cpufeatures::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0") } |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +/// macOS helper function for calling `sysctlbyname`. |
| 133 | +#[cfg(target_os = "macos")] |
| 134 | +pub unsafe fn sysctlbyname(name: &[u8]) -> bool { |
| 135 | + assert_eq!( |
| 136 | + name.last().cloned(), |
| 137 | + Some(0), |
| 138 | + "name is not NUL terminated: {:?}", |
| 139 | + name |
| 140 | + ); |
| 141 | + |
| 142 | + let mut value: u32 = 0; |
| 143 | + let mut size = core::mem::size_of::<u32>(); |
| 144 | + |
| 145 | + let rc = libc::sysctlbyname( |
| 146 | + name.as_ptr() as *const i8, |
| 147 | + &mut value as *mut _ as *mut libc::c_void, |
| 148 | + &mut size, |
| 149 | + core::ptr::null_mut(), |
| 150 | + 0, |
| 151 | + ); |
| 152 | + |
| 153 | + assert_eq!(size, 4, "unexpected sysctlbyname(3) result size"); |
| 154 | + assert_eq!(rc, 0, "sysctlbyname returned error code: {}", rc); |
| 155 | + value != 0 |
| 156 | +} |
0 commit comments