| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
128-bit atomic implementation on PowerPC64. |
| 5 |
|
| 6 |
This architecture provides the following 128-bit atomic instructions: |
| 7 |
|
| 8 |
- lq/stq: load/store (ISA 2.07 or later, included in the Linux Compliancy subset and AIX Compliancy subset) |
| 9 |
- lqarx/stqcx.: LL/SC (ISA 2.07 or later, included in the Linux Compliancy subset and AIX Compliancy subset) |
| 10 |
- plq/pstq: load/store (ISA 3.1 or later, included in the Linux Compliancy subset and AIX Compliancy subset) |
| 11 |
|
| 12 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 13 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 14 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#powerpc |
| 15 |
|
| 16 |
Note that we do not separate LL and SC into separate functions, but handle |
| 17 |
them within a single asm block. This is because it is theoretically possible |
| 18 |
for the compiler to insert operations that might clear the reservation between |
| 19 |
LL and SC. See aarch64.rs for details. |
| 20 |
|
| 21 |
Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use |
| 22 |
this module and use intrinsics.rs instead. |
| 23 |
|
| 24 |
Refs: |
| 25 |
- Power ISA |
| 26 |
https://openpowerfoundation.org/specifications/isa |
| 27 |
- AIX Assembler language reference |
| 28 |
https://www.ibm.com/docs/en/aix/7.3?topic=aix-assembler-language-reference |
| 29 |
- atomic-maybe-uninit |
| 30 |
https://github.com/taiki-e/atomic-maybe-uninit |
| 31 |
|
| 32 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 33 |
*/ |
| 34 |
|
| 35 |
include!("macros.rs"); |
| 36 |
|
| 37 |
#[cfg(not(any( |
| 38 |
target_feature = "quadword-atomics", |
| 39 |
portable_atomic_target_feature = "quadword-atomics", |
| 40 |
)))] |
| 41 |
#[path = "../fallback/outline_atomics.rs"] |
| 42 |
mod fallback; |
| 43 |
|
| 44 |
// On musl with static linking, it seems that getauxval is not always available. |
| 45 |
// See detect/auxv.rs for more. |
| 46 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 47 |
#[cfg(any( |
| 48 |
test, |
| 49 |
not(any( |
| 50 |
target_feature = "quadword-atomics", |
| 51 |
portable_atomic_target_feature = "quadword-atomics", |
| 52 |
)), |
| 53 |
))] |
| 54 |
#[cfg(any( |
| 55 |
all( |
| 56 |
target_os = "linux", |
| 57 |
any( |
| 58 |
all( |
| 59 |
target_env = "gnu", |
| 60 |
any(target_endian = "little", not(target_feature = "crt-static")), |
| 61 |
), |
| 62 |
all(target_env = "musl", any(not(target_feature = "crt-static"), feature = "std")), |
| 63 |
target_env = "ohos", |
| 64 |
all(target_env = "uclibc", not(target_feature = "crt-static")), |
| 65 |
portable_atomic_outline_atomics, |
| 66 |
), |
| 67 |
), |
| 68 |
target_os = "android", |
| 69 |
all( |
| 70 |
target_os = "freebsd", |
| 71 |
any( |
| 72 |
target_endian = "little", |
| 73 |
not(target_feature = "crt-static"), |
| 74 |
portable_atomic_outline_atomics, |
| 75 |
), |
| 76 |
), |
| 77 |
target_os = "openbsd", |
| 78 |
))] |
| 79 |
#[path = "../detect/auxv.rs"] |
| 80 |
mod detect; |
| 81 |
#[cfg(not(portable_atomic_no_outline_atomics))] |
| 82 |
#[cfg(any( |
| 83 |
test, |
| 84 |
not(any( |
| 85 |
target_feature = "quadword-atomics", |
| 86 |
portable_atomic_target_feature = "quadword-atomics", |
| 87 |
)), |
| 88 |
))] |
| 89 |
#[cfg(target_os = "aix")] |
| 90 |
#[cfg(not(portable_atomic_pre_llvm_20))] // SIGTRAP on LLVM 19 |
| 91 |
#[cfg(any(test, portable_atomic_outline_atomics))] // TODO(aix): currently disabled by default |
| 92 |
#[path = "../detect/powerpc64_aix.rs"] |
| 93 |
mod detect; |
| 94 |
|
| 95 |
use core::{arch::asm, sync::atomic::Ordering}; |
| 96 |
|
| 97 |
#[cfg(portable_atomic_no_strict_provenance)] |
| 98 |
use crate::utils::ptr::PtrExt as _; |
| 99 |
use crate::utils::{Pair, U128}; |
| 100 |
|
| 101 |
macro_rules! debug_assert_pwr8 { |
| 102 |
() => { |
| 103 |
#[cfg(not(any( |
| 104 |
target_feature = "quadword-atomics", |
| 105 |
portable_atomic_target_feature = "quadword-atomics", |
| 106 |
)))] |
| 107 |
{ |
| 108 |
debug_assert!(detect::detect().quadword_atomics()); |
| 109 |
} |
| 110 |
}; |
| 111 |
} |
| 112 |
|
| 113 |
// Refs: https://www.ibm.com/docs/en/aix/7.3?topic=ops-machine-pseudo-op |
| 114 |
// |
| 115 |
// This is similar to #[target_feature(enable = "quadword-atomics")], except that there are |
| 116 |
// no compiler guarantees regarding (un)inlining, and the scope is within an asm |
| 117 |
// block rather than a function. We use this directive because #[target_feature(enable = "quadword-atomics")] |
| 118 |
// is unstable and unavailable on old nightly and incompatible with rustc_codegen_cranelift: |
| 119 |
// https://github.com/rust-lang/rustc_codegen_cranelift/issues/1400#issuecomment-1774599775 |
| 120 |
// |
| 121 |
// Note: start_pwr8 and end_pwr8 must be used in pairs. |
| 122 |
// |
| 123 |
// Note: If power8 instructions are not available at compile-time, we must guarantee that |
| 124 |
// the function that uses it is not inlined into a function where it is not |
| 125 |
// clear whether power8 instructions are available. Otherwise, (even if we checked whether |
| 126 |
// power8 instructions are available at run-time) optimizations that reorder its |
| 127 |
// instructions across the if condition might introduce undefined behavior. |
| 128 |
// (see also https://rust-lang.github.io/rfcs/2045-target-feature.html#safely-inlining-target_feature-functions-on-more-contexts) |
| 129 |
// However, our code uses the ifunc helper macro that works with function pointers, |
| 130 |
// so we don't have to worry about this unless calling without helper macro. |
| 131 |
macro_rules! start_pwr8 { |
| 132 |
() => { |
| 133 |
".machine push\n.machine power8" |
| 134 |
}; |
| 135 |
} |
| 136 |
macro_rules! end_pwr8 { |
| 137 |
() => { |
| 138 |
".machine pop" |
| 139 |
}; |
| 140 |
} |
| 141 |
|
| 142 |
macro_rules! atomic_rmw { |
| 143 |
($op:ident, $order:ident) => { |
| 144 |
match $order { |
| 145 |
Ordering::Relaxed => $op!("", ""), |
| 146 |
Ordering::Acquire => $op!("isync", ""), |
| 147 |
Ordering::Release => $op!("", "lwsync"), |
| 148 |
Ordering::AcqRel => $op!("isync", "lwsync"), |
| 149 |
Ordering::SeqCst => $op!("isync", "sync"), |
| 150 |
_ => unreachable!(), |
| 151 |
} |
| 152 |
}; |
| 153 |
} |
| 154 |
macro_rules! atomic_cas { |
| 155 |
($op:ident, $success:ident, $failure:ident) => { |
| 156 |
if $failure == Ordering::Relaxed { |
| 157 |
match $success { |
| 158 |
Ordering::Relaxed => $op!("", "", ""), |
| 159 |
Ordering::Acquire => $op!("", "isync", ""), |
| 160 |
Ordering::Release => $op!("", "", "lwsync"), |
| 161 |
Ordering::AcqRel => $op!("", "isync", "lwsync"), |
| 162 |
Ordering::SeqCst => $op!("", "isync", "sync"), |
| 163 |
_ => unreachable!(), |
| 164 |
} |
| 165 |
} else { |
| 166 |
let order = crate::utils::upgrade_success_ordering($success, $failure); |
| 167 |
match order { |
| 168 |
// Relaxed and Release are covered in $failure == Relaxed branch. |
| 169 |
Ordering::Acquire => $op!("isync", "", ""), |
| 170 |
Ordering::AcqRel => $op!("isync", "", "lwsync"), |
| 171 |
Ordering::SeqCst => $op!("isync", "", "sync"), |
| 172 |
_ => unreachable!(), |
| 173 |
} |
| 174 |
} |
| 175 |
}; |
| 176 |
} |
| 177 |
|
| 178 |
// Extracts and checks the EQ bit of cr0. |
| 179 |
#[inline] |
| 180 |
fn test_cr0_eq(cr: u64) -> bool { |
| 181 |
cr & 0x20000000 != 0 |
| 182 |
} |
| 183 |
|
| 184 |
// If quadword-atomics is available at compile-time, we can always use pwr8_fn. |
| 185 |
#[cfg(any( |
| 186 |
target_feature = "quadword-atomics", |
| 187 |
portable_atomic_target_feature = "quadword-atomics", |
| 188 |
))] |
| 189 |
use self::atomic_load_pwr8 as atomic_load; |
| 190 |
// Otherwise, we need to do run-time detection and can use pwr8_fn only if quadword-atomics is available. |
| 191 |
#[cfg(not(any( |
| 192 |
target_feature = "quadword-atomics", |
| 193 |
portable_atomic_target_feature = "quadword-atomics", |
| 194 |
)))] |
| 195 |
#[inline] |
| 196 |
unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 { |
| 197 |
fn_alias! { |
| 198 |
// inline(never) is just a hint and also not strictly necessary |
| 199 |
// because we use ifunc helper macro, but used for clarity. |
| 200 |
#[inline(never)] |
| 201 |
unsafe fn(src: *mut u128) -> u128; |
| 202 |
atomic_load_pwr8_relaxed = atomic_load_pwr8(Ordering::Relaxed); |
| 203 |
atomic_load_pwr8_acquire = atomic_load_pwr8(Ordering::Acquire); |
| 204 |
atomic_load_pwr8_seqcst = atomic_load_pwr8(Ordering::SeqCst); |
| 205 |
} |
| 206 |
// SAFETY: the caller must uphold the safety contract. |
| 207 |
// we only calls atomic_load_pwr8 if quadword-atomics is available. |
| 208 |
unsafe { |
| 209 |
match order { |
| 210 |
Ordering::Relaxed => { |
| 211 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 212 |
if detect::detect().quadword_atomics() { |
| 213 |
atomic_load_pwr8_relaxed |
| 214 |
} else { |
| 215 |
fallback::atomic_load_non_seqcst |
| 216 |
} |
| 217 |
}) |
| 218 |
} |
| 219 |
Ordering::Acquire => { |
| 220 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 221 |
if detect::detect().quadword_atomics() { |
| 222 |
atomic_load_pwr8_acquire |
| 223 |
} else { |
| 224 |
fallback::atomic_load_non_seqcst |
| 225 |
} |
| 226 |
}) |
| 227 |
} |
| 228 |
Ordering::SeqCst => { |
| 229 |
ifunc!(unsafe fn(src: *mut u128) -> u128 { |
| 230 |
if detect::detect().quadword_atomics() { |
| 231 |
atomic_load_pwr8_seqcst |
| 232 |
} else { |
| 233 |
fallback::atomic_load_seqcst |
| 234 |
} |
| 235 |
}) |
| 236 |
} |
| 237 |
_ => unreachable!(), |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
#[inline] |
| 242 |
unsafe fn atomic_load_pwr8(src: *mut u128, order: Ordering) -> u128 { |
| 243 |
debug_assert!(src.addr() % 16 == 0); |
| 244 |
debug_assert_pwr8!(); |
| 245 |
let (out_hi, out_lo); |
| 246 |
|
| 247 |
// SAFETY: the caller must uphold the safety contract. |
| 248 |
// |
| 249 |
// Refs: Section 3.3.4 "Fixed Point Load and Store Quadword Instructions" of Power ISA 3.1C Book I |
| 250 |
unsafe { |
| 251 |
macro_rules! atomic_load_acquire { |
| 252 |
($release:tt) => { |
| 253 |
asm!( |
| 254 |
start_pwr8!(), |
| 255 |
$release, |
| 256 |
"lq %r4, 0({src})", // atomic { r4:r5 = *src } |
| 257 |
"cmpw %r4, %r4", // if r4 == r4 { cr0.EQ = 1 } else { cr0.EQ = 0 } |
| 258 |
"bne- %cr0, 2f", // if unlikely(cr0.EQ == 0) { jump 'never } |
| 259 |
"2:", // 'never: |
| 260 |
"isync", // fence (works in combination with a branch that depends on the loaded value) |
| 261 |
end_pwr8!(), |
| 262 |
src = in(reg_nonzero) ptr_reg!(src), |
| 263 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 264 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 265 |
out("r4") out_hi, |
| 266 |
out("r5") out_lo, |
| 267 |
out("cr0") _, |
| 268 |
options(nostack, preserves_flags), |
| 269 |
) |
| 270 |
}; |
| 271 |
} |
| 272 |
match order { |
| 273 |
Ordering::Relaxed => { |
| 274 |
asm!( |
| 275 |
start_pwr8!(), |
| 276 |
"lq %r4, 0({src})", // atomic { r4:r5 = *src } |
| 277 |
end_pwr8!(), |
| 278 |
src = in(reg_nonzero) ptr_reg!(src), |
| 279 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 280 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 281 |
out("r4") out_hi, |
| 282 |
out("r5") out_lo, |
| 283 |
options(nostack, preserves_flags), |
| 284 |
); |
| 285 |
} |
| 286 |
Ordering::Acquire => atomic_load_acquire!(""), |
| 287 |
Ordering::SeqCst => atomic_load_acquire!("sync"), |
| 288 |
_ => unreachable!(), |
| 289 |
} |
| 290 |
U128 { pair: Pair { hi: out_hi, lo: out_lo } }.whole |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
// If quadword-atomics is available at compile-time, we can always use pwr8_fn. |
| 295 |
#[cfg(any( |
| 296 |
target_feature = "quadword-atomics", |
| 297 |
portable_atomic_target_feature = "quadword-atomics", |
| 298 |
))] |
| 299 |
use self::atomic_store_pwr8 as atomic_store; |
| 300 |
// Otherwise, we need to do run-time detection and can use pwr8_fn only if quadword-atomics is available. |
| 301 |
#[cfg(not(any( |
| 302 |
target_feature = "quadword-atomics", |
| 303 |
portable_atomic_target_feature = "quadword-atomics", |
| 304 |
)))] |
| 305 |
#[inline] |
| 306 |
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) { |
| 307 |
fn_alias! { |
| 308 |
// inline(never) is just a hint and also not strictly necessary |
| 309 |
// because we use ifunc helper macro, but used for clarity. |
| 310 |
#[inline(never)] |
| 311 |
unsafe fn(dst: *mut u128, val: u128); |
| 312 |
atomic_store_pwr8_relaxed = atomic_store_pwr8(Ordering::Relaxed); |
| 313 |
atomic_store_pwr8_release = atomic_store_pwr8(Ordering::Release); |
| 314 |
atomic_store_pwr8_seqcst = atomic_store_pwr8(Ordering::SeqCst); |
| 315 |
} |
| 316 |
// SAFETY: the caller must uphold the safety contract. |
| 317 |
// we only calls atomic_store_pwr8 if quadword-atomics is available. |
| 318 |
unsafe { |
| 319 |
match order { |
| 320 |
Ordering::Relaxed => { |
| 321 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 322 |
if detect::detect().quadword_atomics() { |
| 323 |
atomic_store_pwr8_relaxed |
| 324 |
} else { |
| 325 |
fallback::atomic_store_non_seqcst |
| 326 |
} |
| 327 |
}); |
| 328 |
} |
| 329 |
Ordering::Release => { |
| 330 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 331 |
if detect::detect().quadword_atomics() { |
| 332 |
atomic_store_pwr8_release |
| 333 |
} else { |
| 334 |
fallback::atomic_store_non_seqcst |
| 335 |
} |
| 336 |
}); |
| 337 |
} |
| 338 |
Ordering::SeqCst => { |
| 339 |
ifunc!(unsafe fn(dst: *mut u128, val: u128) { |
| 340 |
if detect::detect().quadword_atomics() { |
| 341 |
atomic_store_pwr8_seqcst |
| 342 |
} else { |
| 343 |
fallback::atomic_store_seqcst |
| 344 |
} |
| 345 |
}); |
| 346 |
} |
| 347 |
_ => unreachable!(), |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
#[inline] |
| 352 |
unsafe fn atomic_store_pwr8(dst: *mut u128, val: u128, order: Ordering) { |
| 353 |
debug_assert!(dst.addr() % 16 == 0); |
| 354 |
debug_assert_pwr8!(); |
| 355 |
let val = U128 { whole: val }; |
| 356 |
|
| 357 |
// SAFETY: the caller must uphold the safety contract. |
| 358 |
// |
| 359 |
// Refs: Section 3.3.4 "Fixed Point Load and Store Quadword Instructions" of Power ISA 3.1C Book I |
| 360 |
unsafe { |
| 361 |
macro_rules! atomic_store { |
| 362 |
($release:tt) => { |
| 363 |
asm!( |
| 364 |
start_pwr8!(), |
| 365 |
$release, // fence |
| 366 |
"stq %r4, 0({dst})", // atomic { *dst = r4:r5 } |
| 367 |
end_pwr8!(), |
| 368 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 369 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 370 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 371 |
in("r4") val.pair.hi, |
| 372 |
in("r5") val.pair.lo, |
| 373 |
options(nostack, preserves_flags), |
| 374 |
) |
| 375 |
}; |
| 376 |
} |
| 377 |
match order { |
| 378 |
Ordering::Relaxed => atomic_store!(""), |
| 379 |
Ordering::Release => atomic_store!("lwsync"), |
| 380 |
Ordering::SeqCst => atomic_store!("sync"), |
| 381 |
_ => unreachable!(), |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
#[inline] |
| 387 |
unsafe fn atomic_compare_exchange( |
| 388 |
dst: *mut u128, |
| 389 |
old: u128, |
| 390 |
new: u128, |
| 391 |
success: Ordering, |
| 392 |
failure: Ordering, |
| 393 |
) -> Result<u128, u128> { |
| 394 |
#[cfg(any( |
| 395 |
target_feature = "quadword-atomics", |
| 396 |
portable_atomic_target_feature = "quadword-atomics", |
| 397 |
))] |
| 398 |
// SAFETY: the caller must uphold the safety contract. |
| 399 |
// cfg guarantees that quadword atomics instructions are available at compile-time. |
| 400 |
let (prev, ok) = unsafe { atomic_compare_exchange_pwr8(dst, old, new, success, failure) }; |
| 401 |
#[cfg(not(any( |
| 402 |
target_feature = "quadword-atomics", |
| 403 |
portable_atomic_target_feature = "quadword-atomics", |
| 404 |
)))] |
| 405 |
// SAFETY: the caller must uphold the safety contract. |
| 406 |
let (prev, ok) = { |
| 407 |
fn_alias! { |
| 408 |
// inline(never) is just a hint and also not strictly necessary |
| 409 |
// because we use ifunc helper macro, but used for clarity. |
| 410 |
#[inline(never)] |
| 411 |
unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool); |
| 412 |
pwr8_relaxed_fn = atomic_compare_exchange_pwr8(Ordering::Relaxed, Ordering::Relaxed); |
| 413 |
pwr8_acquire_fn = atomic_compare_exchange_pwr8(Ordering::Acquire, Ordering::Acquire); |
| 414 |
pwr8_release_fn = atomic_compare_exchange_pwr8(Ordering::Release, Ordering::Relaxed); |
| 415 |
pwr8_acqrel_fn = atomic_compare_exchange_pwr8(Ordering::AcqRel, Ordering::Acquire); |
| 416 |
pwr8_seqcst_fn = atomic_compare_exchange_pwr8(Ordering::SeqCst, Ordering::SeqCst); |
| 417 |
} |
| 418 |
// SAFETY: the caller must uphold the safety contract. |
| 419 |
// we only calls pwr8_fn if quadword-atomics is available. |
| 420 |
unsafe { |
| 421 |
let success = crate::utils::upgrade_success_ordering(success, failure); |
| 422 |
match success { |
| 423 |
Ordering::Relaxed => { |
| 424 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) { |
| 425 |
if detect::detect().quadword_atomics() { |
| 426 |
pwr8_relaxed_fn |
| 427 |
} else { |
| 428 |
fallback::atomic_compare_exchange_non_seqcst |
| 429 |
} |
| 430 |
}) |
| 431 |
} |
| 432 |
Ordering::Acquire => { |
| 433 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) { |
| 434 |
if detect::detect().quadword_atomics() { |
| 435 |
pwr8_acquire_fn |
| 436 |
} else { |
| 437 |
fallback::atomic_compare_exchange_non_seqcst |
| 438 |
} |
| 439 |
}) |
| 440 |
} |
| 441 |
Ordering::Release => { |
| 442 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) { |
| 443 |
if detect::detect().quadword_atomics() { |
| 444 |
pwr8_release_fn |
| 445 |
} else { |
| 446 |
fallback::atomic_compare_exchange_non_seqcst |
| 447 |
} |
| 448 |
}) |
| 449 |
} |
| 450 |
Ordering::AcqRel => { |
| 451 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) { |
| 452 |
if detect::detect().quadword_atomics() { |
| 453 |
pwr8_acqrel_fn |
| 454 |
} else { |
| 455 |
fallback::atomic_compare_exchange_non_seqcst |
| 456 |
} |
| 457 |
}) |
| 458 |
} |
| 459 |
Ordering::SeqCst => { |
| 460 |
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> (u128, bool) { |
| 461 |
if detect::detect().quadword_atomics() { |
| 462 |
pwr8_seqcst_fn |
| 463 |
} else { |
| 464 |
fallback::atomic_compare_exchange_seqcst |
| 465 |
} |
| 466 |
}) |
| 467 |
} |
| 468 |
_ => unreachable!(), |
| 469 |
} |
| 470 |
} |
| 471 |
}; |
| 472 |
if ok { Ok(prev) } else { Err(prev) } |
| 473 |
} |
| 474 |
#[inline] |
| 475 |
unsafe fn atomic_compare_exchange_pwr8( |
| 476 |
dst: *mut u128, |
| 477 |
old: u128, |
| 478 |
new: u128, |
| 479 |
success: Ordering, |
| 480 |
failure: Ordering, |
| 481 |
) -> (u128, bool) { |
| 482 |
debug_assert!(dst.addr() % 16 == 0); |
| 483 |
debug_assert_pwr8!(); |
| 484 |
let old = U128 { whole: old }; |
| 485 |
let new = U128 { whole: new }; |
| 486 |
let (mut prev_hi, mut prev_lo); |
| 487 |
let mut r; |
| 488 |
|
| 489 |
// SAFETY: the caller must uphold the safety contract. |
| 490 |
// |
| 491 |
// Refs: Section 4.6.2.2 "128-bit Load And Reserve and Store Conditional Instructions" of Power ISA 3.1C Book II |
| 492 |
unsafe { |
| 493 |
macro_rules! cmpxchg { |
| 494 |
($acquire_always:tt, $acquire_success:tt, $release:tt) => { |
| 495 |
asm!( |
| 496 |
start_pwr8!(), |
| 497 |
$release, // fence |
| 498 |
"2:", // 'retry: |
| 499 |
"lqarx %r8, 0, {dst}", // atomic { RESERVE = (dst, 16); r8:r9 = *dst } |
| 500 |
"xor {tmp_lo}, %r9, {old_lo}", // tmp_lo = r9 ^ old_lo |
| 501 |
"xor {tmp_hi}, %r8, {old_hi}", // tmp_hi = r8 ^ old_hi |
| 502 |
"or. {tmp_lo}, {tmp_lo}, {tmp_hi}", // tmp_lo |= tmp_hi; if tmp_lo == 0 { cr0.EQ = 1 } else { cr0.EQ = 0 } |
| 503 |
"bne %cr0, 3f", // if cr0.EQ == 0 { jump 'cmp-fail } |
| 504 |
"stqcx. %r6, 0, {dst}", // atomic { if RESERVE == (dst, 16) { *dst = r6:r7; cr0.EQ = 1 } else { cr0.EQ = 0 }; RESERVE = None } |
| 505 |
"bne %cr0, 2b", // if cr0.EQ == 0 { jump 'retry } |
| 506 |
$acquire_success, // fence |
| 507 |
"3:", // 'cmp-fail: |
| 508 |
$acquire_always, // fence |
| 509 |
"mfcr {tmp_lo}", // tmp_lo = zero_extend(cr) |
| 510 |
end_pwr8!(), |
| 511 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 512 |
old_hi = in(reg) old.pair.hi, |
| 513 |
old_lo = in(reg) old.pair.lo, |
| 514 |
tmp_hi = out(reg) _, |
| 515 |
tmp_lo = out(reg) r, |
| 516 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 517 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 518 |
in("r6") new.pair.hi, |
| 519 |
in("r7") new.pair.lo, |
| 520 |
out("r8") prev_hi, |
| 521 |
out("r9") prev_lo, |
| 522 |
out("cr0") _, |
| 523 |
options(nostack, preserves_flags), |
| 524 |
) |
| 525 |
}; |
| 526 |
} |
| 527 |
atomic_cas!(cmpxchg, success, failure); |
| 528 |
// if compare failed EQ bit is cleared, if store succeeds EQ bit is set. |
| 529 |
(U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole, test_cr0_eq(r)) |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
// Always use strong CAS for outline-atomics. |
| 534 |
#[cfg(not(any( |
| 535 |
target_feature = "quadword-atomics", |
| 536 |
portable_atomic_target_feature = "quadword-atomics", |
| 537 |
)))] |
| 538 |
use self::atomic_compare_exchange as atomic_compare_exchange_weak; |
| 539 |
#[cfg(any( |
| 540 |
target_feature = "quadword-atomics", |
| 541 |
portable_atomic_target_feature = "quadword-atomics", |
| 542 |
))] |
| 543 |
#[inline] |
| 544 |
unsafe fn atomic_compare_exchange_weak( |
| 545 |
dst: *mut u128, |
| 546 |
old: u128, |
| 547 |
new: u128, |
| 548 |
success: Ordering, |
| 549 |
failure: Ordering, |
| 550 |
) -> Result<u128, u128> { |
| 551 |
// SAFETY: the caller must uphold the safety contract. |
| 552 |
// cfg guarantees that quadword atomics instructions are available at compile-time. |
| 553 |
let (prev, ok) = unsafe { atomic_compare_exchange_weak_pwr8(dst, old, new, success, failure) }; |
| 554 |
if ok { Ok(prev) } else { Err(prev) } |
| 555 |
} |
| 556 |
#[cfg(any( |
| 557 |
target_feature = "quadword-atomics", |
| 558 |
portable_atomic_target_feature = "quadword-atomics", |
| 559 |
))] |
| 560 |
#[inline] |
| 561 |
unsafe fn atomic_compare_exchange_weak_pwr8( |
| 562 |
dst: *mut u128, |
| 563 |
old: u128, |
| 564 |
new: u128, |
| 565 |
success: Ordering, |
| 566 |
failure: Ordering, |
| 567 |
) -> (u128, bool) { |
| 568 |
debug_assert!(dst.addr() % 16 == 0); |
| 569 |
debug_assert_pwr8!(); |
| 570 |
let old = U128 { whole: old }; |
| 571 |
let new = U128 { whole: new }; |
| 572 |
let (mut prev_hi, mut prev_lo); |
| 573 |
let mut r; |
| 574 |
|
| 575 |
// SAFETY: the caller must uphold the safety contract. |
| 576 |
// |
| 577 |
// Refs: Section 4.6.2.2 "128-bit Load And Reserve and Store Conditional Instructions" of Power ISA 3.1C Book II |
| 578 |
unsafe { |
| 579 |
macro_rules! cmpxchg_weak { |
| 580 |
($acquire_always:tt, $acquire_success:tt, $release:tt) => { |
| 581 |
asm!( |
| 582 |
start_pwr8!(), |
| 583 |
$release, // fence |
| 584 |
"lqarx %r8, 0, {dst}", // atomic { RESERVE = (dst, 16); r8:r9 = *dst } |
| 585 |
"xor {tmp_lo}, %r9, {old_lo}", // tmp_lo = r9 ^ old_lo |
| 586 |
"xor {tmp_hi}, %r8, {old_hi}", // tmp_hi = r8 ^ old_hi |
| 587 |
"or. {tmp_lo}, {tmp_lo}, {tmp_hi}", // tmp_lo |= tmp_hi; if tmp_lo == 0 { cr0.EQ = 1 } else { cr0.EQ = 0 } |
| 588 |
"bne %cr0, 3f", // if cr0.EQ == 0 { jump 'cmp-fail } |
| 589 |
"stqcx. %r6, 0, {dst}", // atomic { if RESERVE == (dst, 16) { *dst = r6:r7; cr0.EQ = 1 } else { cr0.EQ = 0 }; RESERVE = None } |
| 590 |
$acquire_success, // fence |
| 591 |
"3:", // 'cmp-fail: |
| 592 |
$acquire_always, // fence |
| 593 |
"mfcr {tmp_lo}", // tmp_lo = zero_extend(cr) |
| 594 |
end_pwr8!(), |
| 595 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 596 |
old_hi = in(reg) old.pair.hi, |
| 597 |
old_lo = in(reg) old.pair.lo, |
| 598 |
tmp_hi = out(reg) _, |
| 599 |
tmp_lo = out(reg) r, |
| 600 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 601 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 602 |
in("r6") new.pair.hi, |
| 603 |
in("r7") new.pair.lo, |
| 604 |
out("r8") prev_hi, |
| 605 |
out("r9") prev_lo, |
| 606 |
out("cr0") _, |
| 607 |
options(nostack, preserves_flags), |
| 608 |
) |
| 609 |
}; |
| 610 |
} |
| 611 |
atomic_cas!(cmpxchg_weak, success, failure); |
| 612 |
// if compare or store failed EQ bit is cleared, if store succeeds EQ bit is set. |
| 613 |
(U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole, test_cr0_eq(r)) |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
// Do not use atomic_rmw_ll_sc_3 because it needs extra MR to implement swap. |
| 618 |
#[inline] |
| 619 |
unsafe fn atomic_swap_pwr8(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 620 |
debug_assert!(dst.addr() % 16 == 0); |
| 621 |
debug_assert_pwr8!(); |
| 622 |
let val = U128 { whole: val }; |
| 623 |
let (mut prev_hi, mut prev_lo); |
| 624 |
|
| 625 |
// SAFETY: the caller must uphold the safety contract. |
| 626 |
unsafe { |
| 627 |
macro_rules! swap { |
| 628 |
($acquire:tt, $release:tt) => { |
| 629 |
asm!( |
| 630 |
start_pwr8!(), |
| 631 |
$release, // fence |
| 632 |
"2:", // 'retry: |
| 633 |
"lqarx %r6, 0, {dst}", // atomic { RESERVE = (dst, 16); r6:r7 = *dst } |
| 634 |
"stqcx. %r8, 0, {dst}", // atomic { if RESERVE == (dst, 16) { *dst = r8:r9; cr0.EQ = 1 } else { cr0.EQ = 0 }; RESERVE = None } |
| 635 |
"bne %cr0, 2b", // if cr0.EQ == 0 { jump 'retry } |
| 636 |
$acquire, // fence |
| 637 |
end_pwr8!(), |
| 638 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 639 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 640 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 641 |
out("r6") prev_hi, |
| 642 |
out("r7") prev_lo, |
| 643 |
in("r8") val.pair.hi, |
| 644 |
in("r9") val.pair.lo, |
| 645 |
out("cr0") _, |
| 646 |
options(nostack, preserves_flags), |
| 647 |
) |
| 648 |
}; |
| 649 |
} |
| 650 |
atomic_rmw!(swap, order); |
| 651 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
/// Atomic RMW by LL/SC loop (3 arguments) |
| 656 |
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;` |
| 657 |
/// |
| 658 |
/// $op can use the following registers: |
| 659 |
/// - val_hi/val_lo pair: val argument (read-only for `$op`) |
| 660 |
/// - r6/r7 pair: previous value loaded by ll (read-only for `$op`) |
| 661 |
/// - r8/r9 pair: new value that will be stored by sc |
| 662 |
macro_rules! atomic_rmw_ll_sc_3 { |
| 663 |
($name:ident, [$($reg:tt)*], $($op:tt)*) => { |
| 664 |
#[inline] |
| 665 |
unsafe fn $name(dst: *mut u128, val: u128, order: Ordering) -> u128 { |
| 666 |
debug_assert!(dst.addr() % 16 == 0); |
| 667 |
debug_assert_pwr8!(); |
| 668 |
let val = U128 { whole: val }; |
| 669 |
let (mut prev_hi, mut prev_lo); |
| 670 |
|
| 671 |
// SAFETY: the caller must uphold the safety contract. |
| 672 |
unsafe { |
| 673 |
macro_rules! op { |
| 674 |
($acquire:tt, $release:tt) => { |
| 675 |
asm!( |
| 676 |
start_pwr8!(), |
| 677 |
$release, // fence |
| 678 |
"2:", // 'retry: |
| 679 |
"lqarx %r6, 0, {dst}", // atomic { RESERVE = (dst, 16); r6:r7 = *dst } |
| 680 |
$($op)* |
| 681 |
"stqcx. %r8, 0, {dst}", // atomic { if RESERVE == (dst, 16) { *dst = r8:r9; cr0.EQ = 1 } else { cr0.EQ = 0 }; RESERVE = None } |
| 682 |
"bne %cr0, 2b", // if cr0.EQ == 0 { jump 'retry } |
| 683 |
$acquire, // fence |
| 684 |
end_pwr8!(), |
| 685 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 686 |
val_hi = in(reg) val.pair.hi, |
| 687 |
val_lo = in(reg) val.pair.lo, |
| 688 |
$($reg)* |
| 689 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 690 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 691 |
out("r6") prev_hi, |
| 692 |
out("r7") prev_lo, |
| 693 |
out("r8") _, // new (hi) |
| 694 |
out("r9") _, // new (lo) |
| 695 |
out("cr0") _, |
| 696 |
options(nostack, preserves_flags), |
| 697 |
) |
| 698 |
}; |
| 699 |
} |
| 700 |
atomic_rmw!(op, order); |
| 701 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 702 |
} |
| 703 |
} |
| 704 |
}; |
| 705 |
} |
| 706 |
/// Atomic RMW by LL/SC loop (2 arguments) |
| 707 |
/// `unsafe fn(dst: *mut u128, order: Ordering) -> u128;` |
| 708 |
/// |
| 709 |
/// $op can use the following registers: |
| 710 |
/// - r6/r7 pair: previous value loaded by ll (read-only for `$op`) |
| 711 |
/// - r8/r9 pair: new value that will be stored by sc |
| 712 |
macro_rules! atomic_rmw_ll_sc_2 { |
| 713 |
($name:ident, [$($reg:tt)*], $($op:tt)*) => { |
| 714 |
#[inline] |
| 715 |
unsafe fn $name(dst: *mut u128, order: Ordering) -> u128 { |
| 716 |
debug_assert!(dst.addr() % 16 == 0); |
| 717 |
debug_assert_pwr8!(); |
| 718 |
let (mut prev_hi, mut prev_lo); |
| 719 |
|
| 720 |
// SAFETY: the caller must uphold the safety contract. |
| 721 |
unsafe { |
| 722 |
macro_rules! op { |
| 723 |
($acquire:tt, $release:tt) => { |
| 724 |
asm!( |
| 725 |
start_pwr8!(), |
| 726 |
$release, // fence |
| 727 |
"2:", // 'retry: |
| 728 |
"lqarx %r6, 0, {dst}", // atomic { RESERVE = (dst, 16); r6:r7 = *dst } |
| 729 |
$($op)* |
| 730 |
"stqcx. %r8, 0, {dst}", // atomic { if RESERVE == (dst, 16) { *dst = r8:r9; cr0.EQ = 1 } else { cr0.EQ = 0 }; RESERVE = None } |
| 731 |
"bne %cr0, 2b", // if cr0.EQ == 0 { jump 'retry } |
| 732 |
$acquire, // fence |
| 733 |
end_pwr8!(), |
| 734 |
dst = in(reg_nonzero) ptr_reg!(dst), |
| 735 |
$($reg)* |
| 736 |
// Quadword atomic instructions work with even/odd pair of specified register and subsequent register. |
| 737 |
// We cannot use r1 (sp) and r2 (system reserved), so start with r4 or grater. |
| 738 |
out("r6") prev_hi, |
| 739 |
out("r7") prev_lo, |
| 740 |
out("r8") _, // new (hi) |
| 741 |
out("r9") _, // new (lo) |
| 742 |
out("cr0") _, |
| 743 |
options(nostack, preserves_flags), |
| 744 |
) |
| 745 |
}; |
| 746 |
} |
| 747 |
atomic_rmw!(op, order); |
| 748 |
U128 { pair: Pair { hi: prev_hi, lo: prev_lo } }.whole |
| 749 |
} |
| 750 |
} |
| 751 |
}; |
| 752 |
} |
| 753 |
|
| 754 |
atomic_rmw_ll_sc_3! { |
| 755 |
atomic_add_pwr8, [out("xer") _,], |
| 756 |
"addc %r9, {val_lo}, %r7", // r9 = val_lo + r7; xer.CA = carry |
| 757 |
"adde %r8, {val_hi}, %r6", // r8 = val_hi + r6 + xer.CA |
| 758 |
} |
| 759 |
atomic_rmw_ll_sc_3! { |
| 760 |
atomic_sub_pwr8, [out("xer") _,], |
| 761 |
"subc %r9, %r7, {val_lo}", // r9 = val_lo - r7; xer.CA = borrow |
| 762 |
"subfe %r8, {val_hi}, %r6", // r8 = val_hi - r6 - xer.CA |
| 763 |
} |
| 764 |
atomic_rmw_ll_sc_3! { |
| 765 |
atomic_and_pwr8, [], |
| 766 |
"and %r9, {val_lo}, %r7", // r9 = val_lo & r7 |
| 767 |
"and %r8, {val_hi}, %r6", // r8 = val_hi & r6 |
| 768 |
} |
| 769 |
atomic_rmw_ll_sc_3! { |
| 770 |
atomic_nand_pwr8, [], |
| 771 |
"nand %r9, {val_lo}, %r7", // r9 = !(val_lo & r7) |
| 772 |
"nand %r8, {val_hi}, %r6", // r8 = !(val_hi & r6) |
| 773 |
} |
| 774 |
atomic_rmw_ll_sc_3! { |
| 775 |
atomic_or_pwr8, [], |
| 776 |
"or %r9, {val_lo}, %r7", // r9 = val_lo | r7 |
| 777 |
"or %r8, {val_hi}, %r6", // r8 = val_hi | r6 |
| 778 |
} |
| 779 |
atomic_rmw_ll_sc_3! { |
| 780 |
atomic_xor_pwr8, [], |
| 781 |
"xor %r9, {val_lo}, %r7", // r9 = val_lo ^ r7 |
| 782 |
"xor %r8, {val_hi}, %r6", // r8 = val_hi ^ r6 |
| 783 |
} |
| 784 |
atomic_rmw_ll_sc_3! { |
| 785 |
atomic_max_pwr8, [out("cr1") _,], |
| 786 |
"cmpld %r7, {val_lo}", // if r7(u) < val_lo(u) { cr0 = { LT: 1, ..0 } } else if r7(u) > val_lo(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 787 |
"iselgt %r9, %r7, {val_lo}", // if cr0.GT == 1 { r9 = r7 } else { r9 = val_lo } |
| 788 |
"cmpd %cr1, %r6, {val_hi}", // if r6(i) < val_hi(i) { cr1 = { LT: 1, ..0 } } else if r6(i) > val_hi(i) { cr1 = { GT: 1, ..0 } } else { cr1 = { EQ: 1, ..0 } } |
| 789 |
"isel %r8, %r7, {val_lo}, 5", // if cr1.GT == 1 { r8 = r7 } else { r8 = val_lo } |
| 790 |
"cmpld %r6, {val_hi}", // if r6(u) < val_hi(u) { cr0 = { LT: 1, ..0 } } else if r6(u) > val_hi(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 791 |
"iseleq %r9, %r9, %r8", // if cr0.EQ == 1 { r9 = r9 } else { r9 = r8 } |
| 792 |
"isel %r8, %r6, {val_hi}, 5", // if cr1.GT == 1 { r8 = r6 } else { r8 = val_hi } |
| 793 |
} |
| 794 |
atomic_rmw_ll_sc_3! { |
| 795 |
atomic_umax_pwr8, [], |
| 796 |
"cmpld %r7, {val_lo}", // if r7(u) < val_lo(u) { cr0 = { LT: 1, ..0 } } else if r7(u) > val_lo(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 797 |
"iselgt %r9, %r7, {val_lo}", // if cr0.GT == 1 { r9 = r7 } else { r9 = val_lo } |
| 798 |
"cmpld %r6, {val_hi}", // if r6(u) < val_hi(u) { cr0 = { LT: 1, ..0 } } else if r6(u) > val_hi(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 799 |
"iselgt %r8, %r7, {val_lo}", // if cr0.GT == 1 { r8 = r7 } else { r8 = val_lo } |
| 800 |
"iseleq %r9, %r9, %r8", // if cr0.EQ == 1 { r9 = r9 } else { r9 = r8 } |
| 801 |
"iselgt %r8, %r6, {val_hi}", // if cr0.GT == 1 { r8 = r6 } else { r8 = val_hi } |
| 802 |
} |
| 803 |
atomic_rmw_ll_sc_3! { |
| 804 |
atomic_min_pwr8, [out("cr1") _,], |
| 805 |
"cmpld %r7, {val_lo}", // if r7(u) < val_lo(u) { cr0 = { LT: 1, ..0 } } else if r7(u) > val_lo(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 806 |
"isellt %r9, %r7, {val_lo}", // if cr0.LT == 1 { r9 = r7 } else { r9 = val_lo } |
| 807 |
"cmpd %cr1, %r6, {val_hi}", // if r6(i) < val_hi(i) { cr1 = { LT: 1, ..0 } } else if r6(i) > val_hi(i) { cr1 = { GT: 1, ..0 } } else { cr1 = { EQ: 1, ..0 } } |
| 808 |
"isel %r8, %r7, {val_lo}, 4", // if cr1.LT == 1 { r8 = r7 } else { r8 = val_lo } |
| 809 |
"cmpld %r6, {val_hi}", // if r6(u) < val_hi(u) { cr0 = { LT: 1, ..0 } } else if r6(u) > val_hi(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 810 |
"iseleq %r9, %r9, %r8", // if cr0.EQ == 1 { r9 = r9 } else { r9 = r8 } |
| 811 |
"isel %r8, %r6, {val_hi}, 4", // if cr1.LT == 1 { r8 = r6 } else { r8 = val_hi } |
| 812 |
} |
| 813 |
atomic_rmw_ll_sc_3! { |
| 814 |
atomic_umin_pwr8, [], |
| 815 |
"cmpld %r7, {val_lo}", // if r7(u) < val_lo(u) { cr0 = { LT: 1, ..0 } } else if r7(u) > val_lo(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 816 |
"isellt %r9, %r7, {val_lo}", // if cr0.LT == 1 { r9 = r7 } else { r9 = val_lo } |
| 817 |
"cmpld %r6, {val_hi}", // if r6(u) < val_hi(u) { cr0 = { LT: 1, ..0 } } else if r6(u) > val_hi(u) { cr0 = { GT: 1, ..0 } } else { cr0 = { EQ: 1, ..0 } } |
| 818 |
"isellt %r8, %r7, {val_lo}", // if cr0.LT == 1 { r8 = r7 } else { r8 = val_lo } |
| 819 |
"iseleq %r9, %r9, %r8", // if cr0.EQ == 1 { r9 = r9 } else { r9 = r8 } |
| 820 |
"isellt %r8, %r6, {val_hi}", // if cr0.LT == 1 { r8 = r6 } else { r8 = val_hi } |
| 821 |
} |
| 822 |
|
| 823 |
#[inline] |
| 824 |
unsafe fn atomic_not_pwr8(dst: *mut u128, order: Ordering) -> u128 { |
| 825 |
// SAFETY: the caller must uphold the safety contract. |
| 826 |
unsafe { atomic_xor_pwr8(dst, !0, order) } |
| 827 |
} |
| 828 |
|
| 829 |
#[cfg(not(portable_atomic_pre_llvm_16))] |
| 830 |
atomic_rmw_ll_sc_2! { |
| 831 |
atomic_neg_pwr8, [out("xer") _,], |
| 832 |
"subfic %r9, %r7, 0", // r9 = 0 - r7; xer.CA = borrow |
| 833 |
"subfze %r8, %r6", // r8 = 0 - r6 - xer.CA |
| 834 |
} |
| 835 |
// LLVM 15 miscompiles subfic. |
| 836 |
#[cfg(portable_atomic_pre_llvm_16)] |
| 837 |
atomic_rmw_ll_sc_2! { |
| 838 |
atomic_neg_pwr8, [zero = in(reg) 0_u64, out("xer") _,], |
| 839 |
"subc %r9, {zero}, %r7", // r9 = 0 - r7; xer.CA = borrow |
| 840 |
"subfze %r8, %r6", // r8 = 0 - r6 - xer.CA |
| 841 |
} |
| 842 |
|
| 843 |
macro_rules! select_atomic_rmw { |
| 844 |
( |
| 845 |
unsafe fn $name:ident($($arg:tt)*) $(-> $ret_ty:ty)?; |
| 846 |
pwr8 = $pwr8_fn:ident; |
| 847 |
non_seqcst_fallback = $non_seqcst_fallback_fn:ident; |
| 848 |
seqcst_fallback = $seqcst_fallback_fn:ident; |
| 849 |
) => { |
| 850 |
// If quadword-atomics is available at compile-time, we can always use pwr8_fn. |
| 851 |
#[cfg(any( |
| 852 |
target_feature = "quadword-atomics", |
| 853 |
portable_atomic_target_feature = "quadword-atomics", |
| 854 |
))] |
| 855 |
use self::$pwr8_fn as $name; |
| 856 |
// Otherwise, we need to do run-time detection and can use pwr8_fn only if quadword-atomics is available. |
| 857 |
#[cfg(not(any( |
| 858 |
target_feature = "quadword-atomics", |
| 859 |
portable_atomic_target_feature = "quadword-atomics", |
| 860 |
)))] |
| 861 |
#[inline] |
| 862 |
unsafe fn $name($($arg)*, order: Ordering) $(-> $ret_ty)? { |
| 863 |
fn_alias! { |
| 864 |
// inline(never) is just a hint and also not strictly necessary |
| 865 |
// because we use ifunc helper macro, but used for clarity. |
| 866 |
#[inline(never)] |
| 867 |
unsafe fn($($arg)*) $(-> $ret_ty)?; |
| 868 |
pwr8_relaxed_fn = $pwr8_fn(Ordering::Relaxed); |
| 869 |
pwr8_acquire_fn = $pwr8_fn(Ordering::Acquire); |
| 870 |
pwr8_release_fn = $pwr8_fn(Ordering::Release); |
| 871 |
pwr8_acqrel_fn = $pwr8_fn(Ordering::AcqRel); |
| 872 |
pwr8_seqcst_fn = $pwr8_fn(Ordering::SeqCst); |
| 873 |
} |
| 874 |
// SAFETY: the caller must uphold the safety contract. |
| 875 |
// we only calls pwr8_fn if quadword-atomics is available. |
| 876 |
unsafe { |
| 877 |
match order { |
| 878 |
Ordering::Relaxed => { |
| 879 |
ifunc!(unsafe fn($($arg)*) $(-> $ret_ty)? { |
| 880 |
if detect::detect().quadword_atomics() { |
| 881 |
pwr8_relaxed_fn |
| 882 |
} else { |
| 883 |
fallback::$non_seqcst_fallback_fn |
| 884 |
} |
| 885 |
}) |
| 886 |
} |
| 887 |
Ordering::Acquire => { |
| 888 |
ifunc!(unsafe fn($($arg)*) $(-> $ret_ty)? { |
| 889 |
if detect::detect().quadword_atomics() { |
| 890 |
pwr8_acquire_fn |
| 891 |
} else { |
| 892 |
fallback::$non_seqcst_fallback_fn |
| 893 |
} |
| 894 |
}) |
| 895 |
} |
| 896 |
Ordering::Release => { |
| 897 |
ifunc!(unsafe fn($($arg)*) $(-> $ret_ty)? { |
| 898 |
if detect::detect().quadword_atomics() { |
| 899 |
pwr8_release_fn |
| 900 |
} else { |
| 901 |
fallback::$non_seqcst_fallback_fn |
| 902 |
} |
| 903 |
}) |
| 904 |
} |
| 905 |
Ordering::AcqRel => { |
| 906 |
ifunc!(unsafe fn($($arg)*) $(-> $ret_ty)? { |
| 907 |
if detect::detect().quadword_atomics() { |
| 908 |
pwr8_acqrel_fn |
| 909 |
} else { |
| 910 |
fallback::$non_seqcst_fallback_fn |
| 911 |
} |
| 912 |
}) |
| 913 |
} |
| 914 |
Ordering::SeqCst => { |
| 915 |
ifunc!(unsafe fn($($arg)*) $(-> $ret_ty)? { |
| 916 |
if detect::detect().quadword_atomics() { |
| 917 |
pwr8_seqcst_fn |
| 918 |
} else { |
| 919 |
fallback::$seqcst_fallback_fn |
| 920 |
} |
| 921 |
}) |
| 922 |
} |
| 923 |
_ => unreachable!(), |
| 924 |
} |
| 925 |
} |
| 926 |
} |
| 927 |
}; |
| 928 |
} |
| 929 |
|
| 930 |
select_atomic_rmw! { |
| 931 |
unsafe fn atomic_swap(dst: *mut u128, val: u128) -> u128; |
| 932 |
pwr8 = atomic_swap_pwr8; |
| 933 |
non_seqcst_fallback = atomic_swap_non_seqcst; |
| 934 |
seqcst_fallback = atomic_swap_seqcst; |
| 935 |
} |
| 936 |
select_atomic_rmw! { |
| 937 |
unsafe fn atomic_add(dst: *mut u128, val: u128) -> u128; |
| 938 |
pwr8 = atomic_add_pwr8; |
| 939 |
non_seqcst_fallback = atomic_add_non_seqcst; |
| 940 |
seqcst_fallback = atomic_add_seqcst; |
| 941 |
} |
| 942 |
select_atomic_rmw! { |
| 943 |
unsafe fn atomic_sub(dst: *mut u128, val: u128) -> u128; |
| 944 |
pwr8 = atomic_sub_pwr8; |
| 945 |
non_seqcst_fallback = atomic_sub_non_seqcst; |
| 946 |
seqcst_fallback = atomic_sub_seqcst; |
| 947 |
} |
| 948 |
select_atomic_rmw! { |
| 949 |
unsafe fn atomic_and(dst: *mut u128, val: u128) -> u128; |
| 950 |
pwr8 = atomic_and_pwr8; |
| 951 |
non_seqcst_fallback = atomic_and_non_seqcst; |
| 952 |
seqcst_fallback = atomic_and_seqcst; |
| 953 |
} |
| 954 |
select_atomic_rmw! { |
| 955 |
unsafe fn atomic_nand(dst: *mut u128, val: u128) -> u128; |
| 956 |
pwr8 = atomic_nand_pwr8; |
| 957 |
non_seqcst_fallback = atomic_nand_non_seqcst; |
| 958 |
seqcst_fallback = atomic_nand_seqcst; |
| 959 |
} |
| 960 |
select_atomic_rmw! { |
| 961 |
unsafe fn atomic_or(dst: *mut u128, val: u128) -> u128; |
| 962 |
pwr8 = atomic_or_pwr8; |
| 963 |
non_seqcst_fallback = atomic_or_non_seqcst; |
| 964 |
seqcst_fallback = atomic_or_seqcst; |
| 965 |
} |
| 966 |
select_atomic_rmw! { |
| 967 |
unsafe fn atomic_xor(dst: *mut u128, val: u128) -> u128; |
| 968 |
pwr8 = atomic_xor_pwr8; |
| 969 |
non_seqcst_fallback = atomic_xor_non_seqcst; |
| 970 |
seqcst_fallback = atomic_xor_seqcst; |
| 971 |
} |
| 972 |
select_atomic_rmw! { |
| 973 |
unsafe fn atomic_max(dst: *mut u128, val: u128) -> u128; |
| 974 |
pwr8 = atomic_max_pwr8; |
| 975 |
non_seqcst_fallback = atomic_max_non_seqcst; |
| 976 |
seqcst_fallback = atomic_max_seqcst; |
| 977 |
} |
| 978 |
select_atomic_rmw! { |
| 979 |
unsafe fn atomic_umax(dst: *mut u128, val: u128) -> u128; |
| 980 |
pwr8 = atomic_umax_pwr8; |
| 981 |
non_seqcst_fallback = atomic_umax_non_seqcst; |
| 982 |
seqcst_fallback = atomic_umax_seqcst; |
| 983 |
} |
| 984 |
select_atomic_rmw! { |
| 985 |
unsafe fn atomic_min(dst: *mut u128, val: u128) -> u128; |
| 986 |
pwr8 = atomic_min_pwr8; |
| 987 |
non_seqcst_fallback = atomic_min_non_seqcst; |
| 988 |
seqcst_fallback = atomic_min_seqcst; |
| 989 |
} |
| 990 |
select_atomic_rmw! { |
| 991 |
unsafe fn atomic_umin(dst: *mut u128, val: u128) -> u128; |
| 992 |
pwr8 = atomic_umin_pwr8; |
| 993 |
non_seqcst_fallback = atomic_umin_non_seqcst; |
| 994 |
seqcst_fallback = atomic_umin_seqcst; |
| 995 |
} |
| 996 |
select_atomic_rmw! { |
| 997 |
unsafe fn atomic_not(dst: *mut u128) -> u128; |
| 998 |
pwr8 = atomic_not_pwr8; |
| 999 |
non_seqcst_fallback = atomic_not_non_seqcst; |
| 1000 |
seqcst_fallback = atomic_not_seqcst; |
| 1001 |
} |
| 1002 |
select_atomic_rmw! { |
| 1003 |
unsafe fn atomic_neg(dst: *mut u128) -> u128; |
| 1004 |
pwr8 = atomic_neg_pwr8; |
| 1005 |
non_seqcst_fallback = atomic_neg_non_seqcst; |
| 1006 |
seqcst_fallback = atomic_neg_seqcst; |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[inline] |
| 1010 |
fn is_lock_free() -> bool { |
| 1011 |
#[cfg(any( |
| 1012 |
target_feature = "quadword-atomics", |
| 1013 |
portable_atomic_target_feature = "quadword-atomics", |
| 1014 |
))] |
| 1015 |
{ |
| 1016 |
// lqarx and stqcx. instructions are statically available. |
| 1017 |
true |
| 1018 |
} |
| 1019 |
#[cfg(not(any( |
| 1020 |
target_feature = "quadword-atomics", |
| 1021 |
portable_atomic_target_feature = "quadword-atomics", |
| 1022 |
)))] |
| 1023 |
{ |
| 1024 |
detect::detect().quadword_atomics() |
| 1025 |
} |
| 1026 |
} |
| 1027 |
const IS_ALWAYS_LOCK_FREE: bool = cfg!(any( |
| 1028 |
target_feature = "quadword-atomics", |
| 1029 |
portable_atomic_target_feature = "quadword-atomics", |
| 1030 |
)); |
| 1031 |
|
| 1032 |
atomic128!(AtomicI128, i128, atomic_max, atomic_min); |
| 1033 |
atomic128!(AtomicU128, u128, atomic_umax, atomic_umin); |
| 1034 |
|
| 1035 |
#[cfg(not(valgrind))] // TODO(powerpc64): Hang (as of Valgrind 3.26) |
| 1036 |
#[cfg(test)] |
| 1037 |
mod tests { |
| 1038 |
use super::*; |
| 1039 |
|
| 1040 |
test_atomic_int!(i128); |
| 1041 |
test_atomic_int!(u128); |
| 1042 |
|
| 1043 |
// load/store/swap implementation is not affected by signedness, so it is |
| 1044 |
// enough to test only unsigned types. |
| 1045 |
stress_test!(u128); |
| 1046 |
} |