Debian Codegraph @

debian/rust-vendor/cpufeatures/src/aarch64.rs

raw
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 // Evaluate the given `$body` expression any of the supplied target features
8 // are not enabled. Otherwise returns true.
9 #[macro_export]
10 #[doc(hidden)]
11 macro_rules! __unless_target_features {
12 ($($tf:tt),+ => $body:expr ) => {
13 {
14 #[cfg(not(all($(target_feature=$tf,)*)))]
15 $body
16
17 #[cfg(all($(target_feature=$tf,)*))]
18 true
19 }
20 };
21 }
22
23 // Linux runtime detection of target CPU features using `getauxval`.
24 #[cfg(any(target_os = "linux", target_os = "android"))]
25 #[macro_export]
26 #[doc(hidden)]
27 macro_rules! __detect_target_features {
28 ($($tf:tt),+) => {{
29 let hwcaps = $crate::aarch64::getauxval_hwcap();
30 $($crate::check!(hwcaps, $tf) & )+ true
31 }};
32 }
33
34 /// Linux helper function for calling `getauxval` to get `AT_HWCAP`.
35 #[cfg(any(target_os = "linux", target_os = "android"))]
36 pub fn getauxval_hwcap() -> u64 {
37 unsafe { libc::getauxval(libc::AT_HWCAP) }
38 }
39
40 // Apple platform's runtime detection of target CPU features using `sysctlbyname`.
41 #[cfg(target_vendor = "apple")]
42 #[macro_export]
43 #[doc(hidden)]
44 macro_rules! __detect_target_features {
45 ($($tf:tt),+) => {{
46 $($crate::check!($tf) & )+ true
47 }};
48 }
49
50 // Linux `expand_check_macro`
51 #[cfg(any(target_os = "linux", target_os = "android"))]
52 macro_rules! __expand_check_macro {
53 ($(($name:tt, $hwcap:ident)),* $(,)?) => {
54 #[macro_export]
55 #[doc(hidden)]
56 macro_rules! check {
57 $(
58 ($hwcaps:expr, $name) => {
59 (($hwcaps & $crate::aarch64::hwcaps::$hwcap) != 0)
60 };
61 )*
62 }
63 };
64 }
65
66 // Linux `expand_check_macro`
67 #[cfg(any(target_os = "linux", target_os = "android"))]
68 __expand_check_macro! {
69 ("aes", AES), // Enable AES support.
70 ("dit", DIT), // Enable DIT support.
71 ("sha2", SHA2), // Enable SHA1 and SHA256 support.
72 ("sha3", SHA3), // Enable SHA512 and SHA3 support.
73 ("sm4", SM4), // Enable SM3 and SM4 support.
74 }
75
76 /// Linux hardware capabilities mapped to target features.
77 ///
78 /// Note that LLVM target features are coarser grained than what Linux supports
79 /// and imply more capabilities under each feature. This module attempts to
80 /// provide that mapping accordingly.
81 ///
82 /// See this issue for more info: <https://github.com/RustCrypto/utils/issues/395>
83 #[cfg(any(target_os = "linux", target_os = "android"))]
84 pub mod hwcaps {
85 use libc::c_ulong;
86
87 pub const AES: c_ulong = libc::HWCAP_AES | libc::HWCAP_PMULL;
88 pub const DIT: c_ulong = libc::HWCAP_DIT;
89 pub const SHA2: c_ulong = libc::HWCAP_SHA2;
90 pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512;
91 pub const SM4: c_ulong = libc::HWCAP_SM3 | libc::HWCAP_SM4;
92 }
93
94 // Apple OS (macOS, iOS, watchOS, and tvOS) `check!` macro.
95 //
96 // NOTE: several of these instructions (e.g. `aes`, `sha2`) can be assumed to
97 // be present on all Apple ARM64 hardware.
98 //
99 // Newer CPU instructions now have nodes within sysctl's `hw.optional`
100 // namespace, however the ones that do not can safely be assumed to be
101 // present on all Apple ARM64 devices, now and for the foreseeable future.
102 //
103 // See discussion on this issue for more information:
104 // <https://github.com/RustCrypto/utils/issues/378>
105 #[cfg(target_vendor = "apple")]
106 #[macro_export]
107 #[doc(hidden)]
108 macro_rules! check {
109 ("aes") => {
110 true
111 };
112 ("dit") => {
113 // https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations
114 unsafe {
115 $crate::aarch64::sysctlbyname(b"hw.optional.arm.FEAT_DIT\0")
116 }
117 };
118 ("sha2") => {
119 true
120 };
121 ("sha3") => {
122 unsafe {
123 // `sha3` target feature implies SHA-512 as well
124 $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha512\0")
125 && $crate::aarch64::sysctlbyname(b"hw.optional.armv8_2_sha3\0")
126 }
127 };
128 ("sm4") => {
129 false
130 };
131 }
132
133 /// Apple helper function for calling `sysctlbyname`.
134 ///
135 /// <https://developer.apple.com/documentation/kernel/1387446-sysctlbyname>
136 ///
137 /// # Panics
138 /// If `name` is not NUL terminated
139 #[cfg(target_vendor = "apple")]
140 #[must_use]
141 pub unsafe fn sysctlbyname(name: &[u8]) -> bool {
142 assert_eq!(
143 name.last().cloned(),
144 Some(0),
145 "name is not NUL terminated: {:?}",
146 name
147 );
148
149 let mut value: u32 = 0;
150 let mut size = size_of::<u32>();
151
152 // SAFETY:
153 // - `name` is being cast from a valid byte slice we asserted was NUL terminated above.
154 // - `value` is a properly-aligned, writable integer.
155 // - `size` is initialized to the size of `value` (4-bytes).
156 // - The last two arguments,`newp` and `newlen`, are for setting system parameters, which we
157 // aren't doing here (and requires root privileges). The docs say the following:
158 // - `newp`: "Specify NULL if you don’t want to set the attribute’s value"
159 // - `newlen`: "Specify 0 if you don’t want to set the attribute’s value"
160 let rc = unsafe {
161 libc::sysctlbyname(
162 name.as_ptr().cast::<i8>(),
163 (&raw mut value).cast::<libc::c_void>(),
164 &raw mut size,
165 core::ptr::null_mut(),
166 0,
167 )
168 };
169
170 assert_eq!(size, 4, "unexpected sysctlbyname(3) result size");
171 assert_eq!(rc, 0, "sysctlbyname returned error code: {}", rc);
172 value != 0
173 }
174
175 // On other targets, runtime CPU feature detection is unavailable
176 #[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android",)))]
177 #[macro_export]
178 #[doc(hidden)]
179 macro_rules! __detect_target_features {
180 ($($tf:tt),+) => {
181 false
182 };
183 }