| 1 |
//! Runs code with the hardware DIT feature enabled when possible. |
| 2 |
//! |
| 3 |
//! With the "std" feature, detects at compilation time and runtime whether FEAT_DIT is available, |
| 4 |
//! and uses it (together with FEAT_SB when available) to enable the data independent timing mode |
| 5 |
//! of the processor. |
| 6 |
//! |
| 7 |
//! Without the "std" feature, this detection is done at compilation time only, which is enough for |
| 8 |
//! some targets like aarch64-apple-darwin which is known to always have these features. |
| 9 |
|
| 10 |
use core::arch::asm; |
| 11 |
|
| 12 |
/// Describes whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 13 |
#[repr(u8)] |
| 14 |
#[derive(Clone, Copy)] |
| 15 |
enum Features { |
| 16 |
// Unknown = 0, |
| 17 |
#[allow(dead_code)] |
| 18 |
Neither = 1, |
| 19 |
#[allow(dead_code)] |
| 20 |
DitOnly = 2, |
| 21 |
DitSb = 3, |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(not(all(target_feature = "dit", target_feature = "sb")))] |
| 25 |
mod detect { |
| 26 |
use super::Features; |
| 27 |
use core::mem::transmute; |
| 28 |
use core::sync::atomic::{AtomicU8, Ordering}; |
| 29 |
|
| 30 |
static FEATURES: AtomicU8 = AtomicU8::new(0); |
| 31 |
|
| 32 |
/// Determines whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 33 |
#[inline] |
| 34 |
pub fn get_aarch64_dit_sb_features() -> Features { |
| 35 |
let features = FEATURES.load(Ordering::Relaxed); |
| 36 |
if features > 0 { |
| 37 |
// SAFETY: a non-zero value is a valid discriminant from Features |
| 38 |
unsafe { transmute::<u8, Features>(features) } |
| 39 |
} else { |
| 40 |
detect_aarch64_dit_sb_features() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/// Records whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 45 |
/// |
| 46 |
/// # Safety |
| 47 |
/// |
| 48 |
/// Either parameter must not be set to true if the corresponding feature |
| 49 |
/// is not implemented. |
| 50 |
pub unsafe fn set_aarch64_dit_sb_features(dit: bool, sb: bool) -> Features { |
| 51 |
let dit = dit || cfg!(target_feature = "dit"); |
| 52 |
let sb = sb || cfg!(target_feature = "sb"); |
| 53 |
let features = match (dit, sb) { |
| 54 |
(true, true) => Features::DitSb, |
| 55 |
(true, false) => Features::DitOnly, |
| 56 |
_ => Features::Neither, |
| 57 |
}; |
| 58 |
let _ = FEATURES.compare_exchange(0, features as u8, Ordering::Relaxed, Ordering::Relaxed); |
| 59 |
features |
| 60 |
} |
| 61 |
|
| 62 |
/// Detects whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 63 |
#[cfg(feature = "std")] |
| 64 |
#[cold] |
| 65 |
fn detect_aarch64_dit_sb_features() -> Features { |
| 66 |
use std::arch::is_aarch64_feature_detected; |
| 67 |
// SAFETY: each parameter is true only if the feature is implemented |
| 68 |
unsafe { |
| 69 |
set_aarch64_dit_sb_features( |
| 70 |
is_aarch64_feature_detected!("dit"), |
| 71 |
is_aarch64_feature_detected!("sb"), |
| 72 |
) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/// Detects whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 77 |
#[cfg(not(feature = "std"))] |
| 78 |
#[cold] |
| 79 |
fn detect_aarch64_dit_sb_features() -> Features { |
| 80 |
// It might or might not be possible to read the system registers |
| 81 |
// AA64PFR0_EL1 and AA64ISAR1_EL1 here; they might even be available |
| 82 |
// at EL0 if HWCAP_CPUID is set in AT_HWCAP, but being no_std means |
| 83 |
// this code might be called in a context where we cannot call into |
| 84 |
// the libc to obtain the auxv (and if we could, we could read from |
| 85 |
// AT_HWCAP the HWCAP_DIT and HWCAP_SB bits directly). |
| 86 |
// |
| 87 |
// The best that can be done, without adding several ARM-specific |
| 88 |
// features to specify "this code will run at EL1" or "this code |
| 89 |
// will run under a Linux kernel greater than 4.11", is to use what's |
| 90 |
// known to be implemented at compile time, and allow an override |
| 91 |
// through the undocumented `set_aarch64_dit_sb_features` function. |
| 92 |
|
| 93 |
// SAFETY: each parameter is true only if the feature is implemented |
| 94 |
unsafe { |
| 95 |
set_aarch64_dit_sb_features(cfg!(target_feature = "dit"), cfg!(target_feature = "sb")) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// Overrides the runtime detection of `FEAT_DIT` and `FEAT_SB`. |
| 101 |
/// |
| 102 |
/// This must be called before other threads are created, and before |
| 103 |
/// any other code in this `constant_time_eq` crate is called. |
| 104 |
/// |
| 105 |
/// # Safety |
| 106 |
/// |
| 107 |
/// Either parameter must not be set to true if the corresponding feature |
| 108 |
/// is not implemented. |
| 109 |
pub unsafe fn set_aarch64_dit_sb_features(_dit: bool, _sb: bool) { |
| 110 |
// SAFETY: the safety requirements are the same as this function |
| 111 |
#[cfg(not(all(target_feature = "dit", target_feature = "sb")))] |
| 112 |
unsafe { |
| 113 |
detect::set_aarch64_dit_sb_features(_dit, _sb); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(not(all(target_feature = "dit", target_feature = "sb")))] |
| 118 |
use detect::get_aarch64_dit_sb_features; |
| 119 |
|
| 120 |
/// Determines whether `FEAT_DIT` and `FEAT_SB` are known to be implemented. |
| 121 |
#[cfg(all(target_feature = "dit", target_feature = "sb"))] |
| 122 |
#[inline(always)] |
| 123 |
fn get_aarch64_dit_sb_features() -> Features { |
| 124 |
// Both are known to be implemented at compile time, skip the detection. |
| 125 |
Features::DitSb |
| 126 |
} |
| 127 |
|
| 128 |
/// Equivalent to `__arm_rsr64("dit")`. |
| 129 |
/// |
| 130 |
/// # Safety |
| 131 |
/// |
| 132 |
/// Must be called only when `FEAT_DIT` is implemented. |
| 133 |
#[inline] |
| 134 |
#[target_feature(enable = "dit")] |
| 135 |
unsafe fn rsr64_dit() -> u64 { |
| 136 |
let mut value; |
| 137 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 138 |
unsafe { |
| 139 |
asm!("mrs {}, dit", lateout(reg) value, options(nomem, preserves_flags, nostack)); |
| 140 |
} |
| 141 |
value |
| 142 |
} |
| 143 |
|
| 144 |
/// Equivalent to `__arm_wsr64("dit", value)`. |
| 145 |
/// |
| 146 |
/// # Safety |
| 147 |
/// |
| 148 |
/// Must be called only when `FEAT_DIT` is implemented. |
| 149 |
#[inline] |
| 150 |
#[target_feature(enable = "dit")] |
| 151 |
unsafe fn wsr64_dit(value: u64) { |
| 152 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 153 |
unsafe { |
| 154 |
// The compiler must not cache values or flags across this instruction. |
| 155 |
asm!("msr dit, {}", in(reg) value, options(nostack)); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/// Equivalent to `__arm_wsr64("dit", 1 << 24)`. |
| 160 |
/// |
| 161 |
/// # Safety |
| 162 |
/// |
| 163 |
/// Must be called only when `FEAT_DIT` is implemented. |
| 164 |
#[inline] |
| 165 |
#[target_feature(enable = "dit")] |
| 166 |
unsafe fn enable_dit() { |
| 167 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 168 |
unsafe { |
| 169 |
// The compiler must not cache values or flags across this instruction. |
| 170 |
asm!("msr dit, #{}", const 1, options(nostack)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/// Equivalent to `__asm__ __volatile__("sb" ::: "memory")`. |
| 175 |
/// |
| 176 |
/// # Safety |
| 177 |
/// |
| 178 |
/// Must be called only when `FEAT_SB` is implemented. |
| 179 |
#[inline] |
| 180 |
#[target_feature(enable = "sb")] |
| 181 |
unsafe fn speculation_barrier() { |
| 182 |
// SAFETY: called only when `FEAT_SB` is implemented |
| 183 |
unsafe { |
| 184 |
// The compiler must not cache values or flags across this instruction. |
| 185 |
asm!("sb", options(nostack)); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/// Synchronization barrier for when `FEAT_SB` is not available. |
| 190 |
/// |
| 191 |
/// This heavier alternative is recommended by Apple when SB is not available, see: |
| 192 |
/// <https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms> |
| 193 |
#[inline] |
| 194 |
fn synchronization_barrier() { |
| 195 |
// SAFETY: these instructions are always available, and have no effects |
| 196 |
// other than being a data barrier and flushing the pipeline |
| 197 |
unsafe { |
| 198 |
// The compiler must not cache values or flags across this instruction. |
| 199 |
asm!("dsb nsh", "isb sy", options(nostack)); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/// Wraps code with DIT when `FEAT_DIT` and `FEAT_SB` were detected. |
| 204 |
/// |
| 205 |
/// # Safety |
| 206 |
/// |
| 207 |
/// `FEAT_DIT` and `FEAT_SB` must have been detected. |
| 208 |
#[inline] |
| 209 |
#[target_feature(enable = "dit,sb")] |
| 210 |
unsafe fn with_feat_dit_sb<T, F>(f: F) -> T |
| 211 |
where |
| 212 |
F: FnOnce() -> T, |
| 213 |
{ |
| 214 |
struct Guard { |
| 215 |
dit: u64, |
| 216 |
} |
| 217 |
|
| 218 |
impl Drop for Guard { |
| 219 |
#[inline] |
| 220 |
fn drop(&mut self) { |
| 221 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 222 |
unsafe { wsr64_dit(self.dit) }; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
let _guard = Guard { |
| 227 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 228 |
dit: unsafe { rsr64_dit() }, |
| 229 |
}; |
| 230 |
|
| 231 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 232 |
unsafe { enable_dit() }; |
| 233 |
|
| 234 |
// SAFETY: called only when `FEAT_SB` is implemented |
| 235 |
unsafe { speculation_barrier() }; |
| 236 |
|
| 237 |
f() |
| 238 |
} |
| 239 |
|
| 240 |
/// Wraps code with DIT when `FEAT_DIT` was detected but not `FEAT_SB`. |
| 241 |
/// |
| 242 |
/// # Safety |
| 243 |
/// |
| 244 |
/// `FEAT_DIT` must have been detected. |
| 245 |
#[inline] |
| 246 |
#[target_feature(enable = "dit")] |
| 247 |
unsafe fn with_feat_dit<T, F>(f: F) -> T |
| 248 |
where |
| 249 |
F: FnOnce() -> T, |
| 250 |
{ |
| 251 |
struct Guard { |
| 252 |
dit: u64, |
| 253 |
} |
| 254 |
|
| 255 |
impl Drop for Guard { |
| 256 |
#[inline] |
| 257 |
fn drop(&mut self) { |
| 258 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 259 |
unsafe { wsr64_dit(self.dit) }; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
let _guard = Guard { |
| 264 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 265 |
dit: unsafe { rsr64_dit() }, |
| 266 |
}; |
| 267 |
|
| 268 |
// SAFETY: called only when `FEAT_DIT` is implemented |
| 269 |
unsafe { enable_dit() }; |
| 270 |
|
| 271 |
synchronization_barrier(); |
| 272 |
|
| 273 |
f() |
| 274 |
} |
| 275 |
|
| 276 |
/// Runs code with the hardware DIT feature enabled when possible. |
| 277 |
#[inline] |
| 278 |
pub(crate) fn with_dit<T, F>(f: F) -> T |
| 279 |
where |
| 280 |
F: FnOnce() -> T, |
| 281 |
{ |
| 282 |
// The use of #[target_feature] disables inlining in some cases. |
| 283 |
// Repeating the code three times with different #[target_feature] |
| 284 |
// generates better code. |
| 285 |
match get_aarch64_dit_sb_features() { |
| 286 |
Features::DitSb => { |
| 287 |
// SAFETY: both `FEAT_DIT` and `FEAT_SB` were detected |
| 288 |
unsafe { with_feat_dit_sb(f) } |
| 289 |
} |
| 290 |
Features::DitOnly => { |
| 291 |
// SAFETY: `FEAT_DIT` was detected |
| 292 |
unsafe { with_feat_dit(f) } |
| 293 |
} |
| 294 |
Features::Neither => f(), |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
#[cfg(test)] |
| 299 |
mod tests { |
| 300 |
extern crate std; |
| 301 |
|
| 302 |
use super::{rsr64_dit, with_dit}; |
| 303 |
use std::arch::is_aarch64_feature_detected; |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn dit_is_restored_after_with_dit() { |
| 307 |
if is_aarch64_feature_detected!("dit") { |
| 308 |
// SAFETY: `FEAT_DIT` was detected |
| 309 |
unsafe { |
| 310 |
let saved = rsr64_dit(); |
| 311 |
with_dit(|| assert_ne!(rsr64_dit(), 0)); |
| 312 |
assert_eq!(rsr64_dit(), saved); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |