| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic operations implementation on x86/x86_64. |
| 5 |
|
| 6 |
This module provides atomic operations not supported by LLVM or optimizes |
| 7 |
cases where LLVM code generation is not optimal. |
| 8 |
|
| 9 |
Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use |
| 10 |
this module and use CAS loop instead. |
| 11 |
|
| 12 |
Refs: |
| 13 |
- x86 and amd64 instruction reference https://www.felixcloutier.com/x86 |
| 14 |
|
| 15 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 16 |
*/ |
| 17 |
|
| 18 |
#[cfg(not(portable_atomic_no_asm))] |
| 19 |
use core::arch::asm; |
| 20 |
use core::sync::atomic::Ordering; |
| 21 |
|
| 22 |
#[cfg(target_pointer_width = "32")] |
| 23 |
macro_rules! ptr_modifier { |
| 24 |
() => { |
| 25 |
":e" |
| 26 |
}; |
| 27 |
} |
| 28 |
#[cfg(target_pointer_width = "64")] |
| 29 |
macro_rules! ptr_modifier { |
| 30 |
() => { |
| 31 |
"" |
| 32 |
}; |
| 33 |
} |
| 34 |
|
| 35 |
/// An atomic fence. |
| 36 |
/// |
| 37 |
/// # Panics |
| 38 |
/// |
| 39 |
/// Panics if `order` is [`Relaxed`](Ordering::Relaxed). |
| 40 |
#[cfg_attr(doc, allow(dead_code))] |
| 41 |
#[inline] |
| 42 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 43 |
pub fn fence(order: Ordering) { |
| 44 |
if order == Ordering::SeqCst { |
| 45 |
let p = core::cell::UnsafeCell::new(core::mem::MaybeUninit::<usize>::uninit()); |
| 46 |
// SAFETY: raw pointer passed in is valid because we got it from a reference. |
| 47 |
unsafe { |
| 48 |
// Equivalent to `mfence`, but is up to 3.1x faster on Coffee Lake and up to 2.4x faster on Raptor Lake-H at least in simple cases. |
| 49 |
// - https://github.com/taiki-e/portable-atomic/pull/156 |
| 50 |
// - LLVM uses `lock or` https://godbolt.org/z/vv6rjzfYd |
| 51 |
// - Windows uses `xchg` for x86_32 for MemoryBarrier https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-memorybarrier |
| 52 |
// - MSVC STL uses `lock inc` https://github.com/microsoft/STL/pull/740 |
| 53 |
// - boost uses `lock or` https://github.com/boostorg/atomic/commit/559eba81af71386cedd99f170dc6101c6ad7bf22 |
| 54 |
#[cfg(target_pointer_width = "64")] |
| 55 |
asm!( |
| 56 |
concat!("xchg qword ptr [{p", ptr_modifier!(), "}], {tmp}"), |
| 57 |
p = in(reg) p.get(), |
| 58 |
tmp = out(reg) _, |
| 59 |
options(nostack, preserves_flags), |
| 60 |
); |
| 61 |
#[cfg(target_pointer_width = "32")] |
| 62 |
asm!( |
| 63 |
concat!("xchg dword ptr [{p", ptr_modifier!(), "}], {tmp:e}"), |
| 64 |
p = in(reg) p.get(), |
| 65 |
tmp = out(reg) _, |
| 66 |
options(nostack, preserves_flags), |
| 67 |
); |
| 68 |
} |
| 69 |
} else { |
| 70 |
core::sync::atomic::fence(order); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
cfg_core_atomic!({ |
| 75 |
use super::core_atomic::{ |
| 76 |
AtomicI8, AtomicI16, AtomicI32, AtomicI64, AtomicIsize, AtomicPtr, AtomicU8, AtomicU16, |
| 77 |
AtomicU32, AtomicU64, AtomicUsize, |
| 78 |
}; |
| 79 |
|
| 80 |
macro_rules! atomic_int { |
| 81 |
($atomic_type:ident, $ptr_size:tt) => { |
| 82 |
impl $atomic_type { |
| 83 |
#[inline] |
| 84 |
pub(crate) fn not(&self, _order: Ordering) { |
| 85 |
let dst = self.as_ptr(); |
| 86 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 87 |
// pointer passed in is valid because we got it from a reference. |
| 88 |
// |
| 89 |
// https://www.felixcloutier.com/x86/not |
| 90 |
unsafe { |
| 91 |
// atomic RMW is always SeqCst. |
| 92 |
asm!( |
| 93 |
concat!("lock not ", $ptr_size, " ptr [{dst", ptr_modifier!(), "}]"), |
| 94 |
dst = in(reg) dst, |
| 95 |
options(nostack, preserves_flags), |
| 96 |
); |
| 97 |
} |
| 98 |
} |
| 99 |
#[inline] |
| 100 |
pub(crate) fn neg(&self, _order: Ordering) { |
| 101 |
let dst = self.as_ptr(); |
| 102 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 103 |
// pointer passed in is valid because we got it from a reference. |
| 104 |
// |
| 105 |
// https://www.felixcloutier.com/x86/neg |
| 106 |
unsafe { |
| 107 |
// atomic RMW is always SeqCst. |
| 108 |
asm!( |
| 109 |
concat!("lock neg ", $ptr_size, " ptr [{dst", ptr_modifier!(), "}]"), |
| 110 |
dst = in(reg) dst, |
| 111 |
// Do not use `preserves_flags` because NEG modifies the CF, OF, SF, ZF, AF, and PF flag. |
| 112 |
options(nostack), |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
}; |
| 118 |
} |
| 119 |
|
| 120 |
atomic_int!(AtomicI8, "byte"); |
| 121 |
atomic_int!(AtomicU8, "byte"); |
| 122 |
atomic_int!(AtomicI16, "word"); |
| 123 |
atomic_int!(AtomicU16, "word"); |
| 124 |
atomic_int!(AtomicI32, "dword"); |
| 125 |
atomic_int!(AtomicU32, "dword"); |
| 126 |
#[cfg(target_arch = "x86_64")] |
| 127 |
atomic_int!(AtomicI64, "qword"); |
| 128 |
#[cfg(target_arch = "x86_64")] |
| 129 |
atomic_int!(AtomicU64, "qword"); |
| 130 |
#[cfg(target_pointer_width = "32")] |
| 131 |
atomic_int!(AtomicIsize, "dword"); |
| 132 |
#[cfg(target_pointer_width = "32")] |
| 133 |
atomic_int!(AtomicUsize, "dword"); |
| 134 |
#[cfg(target_pointer_width = "64")] |
| 135 |
atomic_int!(AtomicIsize, "qword"); |
| 136 |
#[cfg(target_pointer_width = "64")] |
| 137 |
atomic_int!(AtomicUsize, "qword"); |
| 138 |
|
| 139 |
#[cfg(target_arch = "x86")] |
| 140 |
impl AtomicI64 { |
| 141 |
#[inline] |
| 142 |
pub(crate) fn not(&self, order: Ordering) { |
| 143 |
self.fetch_not(order); |
| 144 |
} |
| 145 |
#[inline] |
| 146 |
pub(crate) fn neg(&self, order: Ordering) { |
| 147 |
self.fetch_neg(order); |
| 148 |
} |
| 149 |
} |
| 150 |
#[cfg(target_arch = "x86")] |
| 151 |
impl AtomicU64 { |
| 152 |
#[inline] |
| 153 |
pub(crate) fn not(&self, order: Ordering) { |
| 154 |
self.fetch_not(order); |
| 155 |
} |
| 156 |
#[inline] |
| 157 |
pub(crate) fn neg(&self, order: Ordering) { |
| 158 |
self.fetch_neg(order); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
macro_rules! atomic_bit_opts { |
| 163 |
( |
| 164 |
$([$($generics:tt)*])? $atomic_type:ident, $int_type:ident, $val_modifier:tt, |
| 165 |
$ptr_size:tt |
| 166 |
) => { |
| 167 |
// LLVM 14 and older don't support generating `lock bt{s,r,c}`. |
| 168 |
// LLVM 15 only supports generating `lock bt{s,r,c}` for immediate bit offsets. |
| 169 |
// LLVM 16+ can generate `lock bt{s,r,c}` for both immediate and register bit offsets. |
| 170 |
// https://godbolt.org/z/TGhr5z4ds |
| 171 |
// So, use fetch_* based implementations on LLVM 16+, otherwise use asm based implementations. |
| 172 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 173 |
impl_default_bit_opts!($atomic_type, $int_type); |
| 174 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 175 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 176 |
// `<integer>::BITS` requires Rust 1.53 |
| 177 |
const BITS: u32 = (core::mem::size_of::<$int_type>() * 8) as u32; |
| 178 |
#[inline] |
| 179 |
pub(crate) fn bit_set(&self, bit: u32, _order: Ordering) -> bool { |
| 180 |
let dst = self.as_ptr(); |
| 181 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 182 |
// pointer passed in is valid because we got it from a reference. |
| 183 |
// the masking by the bit size of the type ensures that we do not shift |
| 184 |
// out of bounds. |
| 185 |
// |
| 186 |
// https://www.felixcloutier.com/x86/bts |
| 187 |
unsafe { |
| 188 |
let r: u8; |
| 189 |
// atomic RMW is always SeqCst. |
| 190 |
asm!( |
| 191 |
concat!("lock bts ", $ptr_size, " ptr [{dst", ptr_modifier!(), "}], {bit", $val_modifier, "}"), |
| 192 |
"setb {r}", |
| 193 |
dst = in(reg) dst, |
| 194 |
bit = in(reg) (bit & (Self::BITS - 1)) as $int_type, |
| 195 |
r = out(reg_byte) r, |
| 196 |
// Do not use `preserves_flags` because BTS modifies the CF flag. |
| 197 |
options(nostack), |
| 198 |
); |
| 199 |
crate::utils::assert_unchecked(r == 0 || r == 1); // may help remove extra test |
| 200 |
r != 0 |
| 201 |
} |
| 202 |
} |
| 203 |
#[inline] |
| 204 |
pub(crate) fn bit_clear(&self, bit: u32, _order: Ordering) -> bool { |
| 205 |
let dst = self.as_ptr(); |
| 206 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 207 |
// pointer passed in is valid because we got it from a reference. |
| 208 |
// the masking by the bit size of the type ensures that we do not shift |
| 209 |
// out of bounds. |
| 210 |
// |
| 211 |
// https://www.felixcloutier.com/x86/btr |
| 212 |
unsafe { |
| 213 |
let r: u8; |
| 214 |
// atomic RMW is always SeqCst. |
| 215 |
asm!( |
| 216 |
concat!("lock btr ", $ptr_size, " ptr [{dst", ptr_modifier!(), "}], {bit", $val_modifier, "}"), |
| 217 |
"setb {r}", |
| 218 |
dst = in(reg) dst, |
| 219 |
bit = in(reg) (bit & (Self::BITS - 1)) as $int_type, |
| 220 |
r = out(reg_byte) r, |
| 221 |
// Do not use `preserves_flags` because BTR modifies the CF flag. |
| 222 |
options(nostack), |
| 223 |
); |
| 224 |
crate::utils::assert_unchecked(r == 0 || r == 1); // may help remove extra test |
| 225 |
r != 0 |
| 226 |
} |
| 227 |
} |
| 228 |
#[inline] |
| 229 |
pub(crate) fn bit_toggle(&self, bit: u32, _order: Ordering) -> bool { |
| 230 |
let dst = self.as_ptr(); |
| 231 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 232 |
// pointer passed in is valid because we got it from a reference. |
| 233 |
// the masking by the bit size of the type ensures that we do not shift |
| 234 |
// out of bounds. |
| 235 |
// |
| 236 |
// https://www.felixcloutier.com/x86/btc |
| 237 |
unsafe { |
| 238 |
let r: u8; |
| 239 |
// atomic RMW is always SeqCst. |
| 240 |
asm!( |
| 241 |
concat!("lock btc ", $ptr_size, " ptr [{dst", ptr_modifier!(), "}], {bit", $val_modifier, "}"), |
| 242 |
"setb {r}", |
| 243 |
dst = in(reg) dst, |
| 244 |
bit = in(reg) (bit & (Self::BITS - 1)) as $int_type, |
| 245 |
r = out(reg_byte) r, |
| 246 |
// Do not use `preserves_flags` because BTC modifies the CF flag. |
| 247 |
options(nostack), |
| 248 |
); |
| 249 |
crate::utils::assert_unchecked(r == 0 || r == 1); // may help remove extra test |
| 250 |
r != 0 |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
}; |
| 255 |
} |
| 256 |
|
| 257 |
impl_default_bit_opts!(AtomicI8, i8); |
| 258 |
impl_default_bit_opts!(AtomicU8, u8); |
| 259 |
atomic_bit_opts!(AtomicI16, i16, ":x", "word"); |
| 260 |
atomic_bit_opts!(AtomicU16, u16, ":x", "word"); |
| 261 |
atomic_bit_opts!(AtomicI32, i32, ":e", "dword"); |
| 262 |
atomic_bit_opts!(AtomicU32, u32, ":e", "dword"); |
| 263 |
#[cfg(target_arch = "x86_64")] |
| 264 |
atomic_bit_opts!(AtomicI64, i64, "", "qword"); |
| 265 |
#[cfg(target_arch = "x86_64")] |
| 266 |
atomic_bit_opts!(AtomicU64, u64, "", "qword"); |
| 267 |
#[cfg(target_arch = "x86")] |
| 268 |
impl_default_bit_opts!(AtomicI64, i64); |
| 269 |
#[cfg(target_arch = "x86")] |
| 270 |
impl_default_bit_opts!(AtomicU64, u64); |
| 271 |
#[cfg(target_pointer_width = "32")] |
| 272 |
atomic_bit_opts!(AtomicIsize, isize, ":e", "dword"); |
| 273 |
#[cfg(target_pointer_width = "32")] |
| 274 |
atomic_bit_opts!(AtomicUsize, usize, ":e", "dword"); |
| 275 |
#[cfg(target_pointer_width = "32")] |
| 276 |
atomic_bit_opts!([T] AtomicPtr, usize, ":e", "dword"); |
| 277 |
#[cfg(target_pointer_width = "64")] |
| 278 |
atomic_bit_opts!(AtomicIsize, isize, "", "qword"); |
| 279 |
#[cfg(target_pointer_width = "64")] |
| 280 |
atomic_bit_opts!(AtomicUsize, usize, "", "qword"); |
| 281 |
#[cfg(target_pointer_width = "64")] |
| 282 |
atomic_bit_opts!([T] AtomicPtr, usize, "", "qword"); |
| 283 |
}); |