| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic operations implementation on pre-v6 ARM Linux/Android. |
| 5 |
|
| 6 |
This module optimizes cases where LLVM code generation is not optimal, |
| 7 |
and provides workaround for core::sync::atomic bug (https://github.com/rust-lang/compiler-builtins/pull/1234). |
| 8 |
|
| 9 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 10 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 11 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#arm |
| 12 |
|
| 13 |
Refs: |
| 14 |
- https://github.com/torvalds/linux/blob/v6.19/Documentation/arch/arm/kernel_user_helpers.rst |
| 15 |
- https://github.com/rust-lang/compiler-builtins/blob/compiler_builtins-v0.1.124/src/arm_linux.rs |
| 16 |
- https://github.com/taiki-e/atomic-maybe-uninit |
| 17 |
|
| 18 |
Note: __kuser_memory_barrier is always SeqCst. |
| 19 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/v0.3.21/src/arch/arm.rs#L215 |
| 20 |
|
| 21 |
Note: On Miri and ThreadSanitizer which do not go through compiler-builtins, we don't use |
| 22 |
this module and use normal core::sync::atomic instead. |
| 23 |
*/ |
| 24 |
|
| 25 |
use core::{mem, sync::atomic::Ordering}; |
| 26 |
|
| 27 |
// https://github.com/torvalds/linux/blob/v6.19/Documentation/arch/arm/kernel_user_helpers.rst |
| 28 |
// __kuser_helper_version >= 3 (kernel version 2.6.15+) |
| 29 |
const KUSER_MEMORY_BARRIER: usize = 0xFFFF0FA0; |
| 30 |
#[inline] |
| 31 |
fn __kuser_memory_barrier() { |
| 32 |
// SAFETY: kernel docs specify a known address with the given signature. |
| 33 |
// core assumes __kuser_helper_version >= 3. |
| 34 |
unsafe { |
| 35 |
let f: extern "C" fn() = |
| 36 |
mem::transmute(crate::utils::ptr::with_exposed_provenance::<()>(KUSER_MEMORY_BARRIER)); |
| 37 |
f(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/// An atomic fence. |
| 42 |
/// |
| 43 |
/// # Panics |
| 44 |
/// |
| 45 |
/// Panics if `order` is [`Relaxed`](Ordering::Relaxed). |
| 46 |
#[cfg_attr(doc, allow(dead_code))] |
| 47 |
#[inline] |
| 48 |
#[cfg_attr(any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), track_caller)] |
| 49 |
pub fn fence(order: Ordering) { |
| 50 |
// Call __kuser_memory_barrier without going through __sync_synchronize. |
| 51 |
match order { |
| 52 |
Ordering::Relaxed => panic!("there is no such thing as a relaxed fence"), |
| 53 |
_ => __kuser_memory_barrier(), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
cfg_core_atomic!({ |
| 58 |
use super::core_atomic::{ |
| 59 |
AtomicI8, AtomicI16, AtomicI32, AtomicIsize, AtomicPtr, AtomicU8, AtomicU16, AtomicU32, |
| 60 |
AtomicUsize, |
| 61 |
}; |
| 62 |
macro_rules! atomic { |
| 63 |
($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty) => { |
| 64 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 65 |
#[inline] |
| 66 |
#[cfg_attr( |
| 67 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 68 |
track_caller |
| 69 |
)] |
| 70 |
pub(crate) fn load(&self, order: Ordering) -> $value_type { |
| 71 |
crate::utils::assert_load_ordering(order); |
| 72 |
match order { |
| 73 |
Ordering::Relaxed => self.inner.load(Ordering::Relaxed), |
| 74 |
// Acquire and SeqCst loads are equivalent. |
| 75 |
// This matches with LLVM, but GCC emits `dmb; ldr*; dmb` for SeqCst load. |
| 76 |
Ordering::Acquire | Ordering::SeqCst => { |
| 77 |
let out = self.inner.load(Ordering::Relaxed); |
| 78 |
__kuser_memory_barrier(); |
| 79 |
out |
| 80 |
} |
| 81 |
_ => unreachable!(), |
| 82 |
} |
| 83 |
} |
| 84 |
#[inline] |
| 85 |
#[cfg_attr( |
| 86 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 87 |
track_caller |
| 88 |
)] |
| 89 |
pub(crate) fn store(&self, val: $value_type, order: Ordering) { |
| 90 |
crate::utils::assert_store_ordering(order); |
| 91 |
match order { |
| 92 |
Ordering::Relaxed => self.inner.store(val, Ordering::Relaxed), |
| 93 |
Ordering::Release => { |
| 94 |
__kuser_memory_barrier(); |
| 95 |
self.inner.store(val, Ordering::Relaxed); |
| 96 |
} |
| 97 |
Ordering::SeqCst => { |
| 98 |
__kuser_memory_barrier(); |
| 99 |
self.inner.store(val, Ordering::Relaxed); |
| 100 |
__kuser_memory_barrier(); |
| 101 |
} |
| 102 |
_ => unreachable!(), |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
#[cfg_attr( |
| 107 |
portable_atomic_no_cfg_target_has_atomic, |
| 108 |
cfg(not(portable_atomic_no_atomic_cas)) |
| 109 |
)] |
| 110 |
#[cfg_attr( |
| 111 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 112 |
cfg(target_has_atomic = "ptr") |
| 113 |
)] |
| 114 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 115 |
#[inline] |
| 116 |
#[cfg_attr( |
| 117 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 118 |
track_caller |
| 119 |
)] |
| 120 |
pub(crate) fn compare_exchange( |
| 121 |
&self, |
| 122 |
current: $value_type, |
| 123 |
new: $value_type, |
| 124 |
success: Ordering, |
| 125 |
failure: Ordering, |
| 126 |
) -> Result<$value_type, $value_type> { |
| 127 |
crate::utils::assert_compare_exchange_ordering(success, failure); // for track_caller (compiler can omit double check) |
| 128 |
#[cfg(portable_atomic_no_stronger_failure_ordering)] |
| 129 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 130 |
let res = self.inner.compare_exchange(current, new, success, failure); |
| 131 |
if failure != Ordering::Relaxed && res.is_err() { |
| 132 |
__kuser_memory_barrier(); |
| 133 |
} |
| 134 |
res |
| 135 |
} |
| 136 |
#[inline] |
| 137 |
#[cfg_attr( |
| 138 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 139 |
track_caller |
| 140 |
)] |
| 141 |
pub(crate) fn compare_exchange_weak( |
| 142 |
&self, |
| 143 |
current: $value_type, |
| 144 |
new: $value_type, |
| 145 |
success: Ordering, |
| 146 |
failure: Ordering, |
| 147 |
) -> Result<$value_type, $value_type> { |
| 148 |
crate::utils::assert_compare_exchange_ordering(success, failure); // for track_caller (compiler can omit double check) |
| 149 |
#[cfg(portable_atomic_no_stronger_failure_ordering)] |
| 150 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 151 |
let res = self.inner.compare_exchange_weak(current, new, success, failure); |
| 152 |
if failure != Ordering::Relaxed && res.is_err() { |
| 153 |
__kuser_memory_barrier(); |
| 154 |
} |
| 155 |
res |
| 156 |
} |
| 157 |
} |
| 158 |
}; |
| 159 |
} |
| 160 |
|
| 161 |
atomic!(AtomicI8, i8); |
| 162 |
atomic!(AtomicU8, u8); |
| 163 |
atomic!(AtomicI16, i16); |
| 164 |
atomic!(AtomicU16, u16); |
| 165 |
atomic!(AtomicI32, i32); |
| 166 |
atomic!(AtomicU32, u32); |
| 167 |
atomic!(AtomicIsize, isize); |
| 168 |
atomic!(AtomicUsize, usize); |
| 169 |
atomic!([T] AtomicPtr, *mut T); |
| 170 |
}); |
| 171 |
|
| 172 |
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(not(portable_atomic_no_atomic_64)))] |
| 173 |
#[cfg_attr( |
| 174 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 175 |
cfg(any( |
| 176 |
target_has_atomic = "64", |
| 177 |
not(any(target_pointer_width = "16", target_pointer_width = "32")), |
| 178 |
)) |
| 179 |
)] |
| 180 |
items!({ |
| 181 |
// This is not normally reached on this platform. |
| 182 |
// It is only reached if something strange occurs, such as using target-feature=-v6 on armv6 target. |
| 183 |
// Since 64-bit atomic operations differ from 32-bit or smaller ones on Arm, similar optimizations |
| 184 |
// or workarounds are useless here, so do the same as atomic_int! macro in core_atomic.rs. |
| 185 |
// NB: Sync with atomic_int! macro in core_atomic.rs |
| 186 |
use super::core_atomic::{AtomicI64, AtomicU64}; |
| 187 |
macro_rules! atomic_int { |
| 188 |
($atomic_type:ident, $int_type:ident) => { |
| 189 |
impl $atomic_type { |
| 190 |
#[inline] |
| 191 |
#[cfg_attr( |
| 192 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 193 |
track_caller |
| 194 |
)] |
| 195 |
pub(crate) fn load(&self, order: Ordering) -> $int_type { |
| 196 |
crate::utils::assert_load_ordering(order); // for track_caller (compiler can omit double check) |
| 197 |
self.inner.load(order) |
| 198 |
} |
| 199 |
#[inline] |
| 200 |
#[cfg_attr( |
| 201 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 202 |
track_caller |
| 203 |
)] |
| 204 |
pub(crate) fn store(&self, val: $int_type, order: Ordering) { |
| 205 |
crate::utils::assert_store_ordering(order); // for track_caller (compiler can omit double check) |
| 206 |
self.inner.store(val, order); |
| 207 |
} |
| 208 |
} |
| 209 |
#[cfg_attr( |
| 210 |
portable_atomic_no_cfg_target_has_atomic, |
| 211 |
cfg(not(portable_atomic_no_atomic_cas)) |
| 212 |
)] |
| 213 |
#[cfg_attr( |
| 214 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 215 |
cfg(target_has_atomic = "ptr") |
| 216 |
)] |
| 217 |
impl $atomic_type { |
| 218 |
#[inline] |
| 219 |
#[cfg_attr( |
| 220 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 221 |
track_caller |
| 222 |
)] |
| 223 |
pub(crate) fn compare_exchange( |
| 224 |
&self, |
| 225 |
current: $int_type, |
| 226 |
new: $int_type, |
| 227 |
success: Ordering, |
| 228 |
failure: Ordering, |
| 229 |
) -> Result<$int_type, $int_type> { |
| 230 |
crate::utils::assert_compare_exchange_ordering(success, failure); // for track_caller (compiler can omit double check) |
| 231 |
#[cfg(portable_atomic_no_stronger_failure_ordering)] |
| 232 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 233 |
self.inner.compare_exchange(current, new, success, failure) |
| 234 |
} |
| 235 |
#[inline] |
| 236 |
#[cfg_attr( |
| 237 |
any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri), |
| 238 |
track_caller |
| 239 |
)] |
| 240 |
pub(crate) fn compare_exchange_weak( |
| 241 |
&self, |
| 242 |
current: $int_type, |
| 243 |
new: $int_type, |
| 244 |
success: Ordering, |
| 245 |
failure: Ordering, |
| 246 |
) -> Result<$int_type, $int_type> { |
| 247 |
crate::utils::assert_compare_exchange_ordering(success, failure); // for track_caller (compiler can omit double check) |
| 248 |
#[cfg(portable_atomic_no_stronger_failure_ordering)] |
| 249 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 250 |
self.inner.compare_exchange_weak(current, new, success, failure) |
| 251 |
} |
| 252 |
} |
| 253 |
}; |
| 254 |
} |
| 255 |
atomic_int!(AtomicI64, i64); |
| 256 |
atomic_int!(AtomicU64, u64); |
| 257 |
}); |