Skip to content

Commit 398d0eb

Browse files
authored
cpufeatures: add Speculation Barrier (SB) support for AArch64 (#1536)
As suggested in #1472, this instruction is needed by the `aarch64-dit` crate to properly implement Data-Independent Timing: https://support.arm.com/documentation/ddi0487/mb/-Part-B-The-AArch64-Application-Level-Architecture/-Chapter-B2-The-AArch64-Application-Level-Memory-Model/-B2-6-Memory-barriers/-B2-6-3-Speculation-Barrier The usage on Apple targets is covered here: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations > "To ensure that subsequent instruction timing reflects the updated DIT > processor state, add a speculation barrier after turning on DIT. > Use the sb instruction to add a speculation barrier" This PR is effectively an extraction of the `cpufeatures`-related changes originally suggested in this patch from #1472: f31f41c
1 parent 98d49ed commit 398d0eb

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

cpufeatures/src/aarch64.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ macro_rules! __expand_check_macro {
6767
#[cfg(any(target_os = "linux", target_os = "android"))]
6868
__expand_check_macro! {
6969
("aes", AES), // Enable AES support.
70-
("dit", DIT), // Enable DIT support.
70+
("dit", DIT), // Enable Data-Independent Timing support.
7171
("sha2", SHA2), // Enable SHA1 and SHA256 support.
7272
("sha3", SHA3), // Enable SHA512 and SHA3 support.
7373
("sm4", SM4), // Enable SM3 and SM4 support.
74+
("sb", SB), // Enable Speculation Barrier support
7475
}
7576

7677
/// Linux hardware capabilities mapped to target features.
@@ -89,6 +90,7 @@ pub mod hwcaps {
8990
pub const SHA2: c_ulong = libc::HWCAP_SHA2;
9091
pub const SHA3: c_ulong = libc::HWCAP_SHA3 | libc::HWCAP_SHA512;
9192
pub const SM4: c_ulong = libc::HWCAP_SM3 | libc::HWCAP_SM4;
93+
pub const SB: c_ulong = libc::HWCAP_SB;
9294
}
9395

9496
// Apple OS (macOS, iOS, watchOS, and tvOS) `check!` macro.
@@ -128,6 +130,12 @@ macro_rules! check {
128130
("sm4") => {
129131
false
130132
};
133+
("sb") => {
134+
// https://support.arm.com/documentation/ddi0487/mb/-Part-B-The-AArch64-Application-Level-Architecture/-Chapter-B2-The-AArch64-Application-Level-Memory-Model/-B2-6-Memory-barriers/-B2-6-3-Speculation-Barrier
135+
unsafe {
136+
$crate::aarch64::sysctlbyname(b"hw.optional.arm.FEAT_SB\0")
137+
}
138+
};
131139
}
132140

133141
/// Apple helper function for calling `sysctlbyname`.

0 commit comments

Comments
 (0)