-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaarch64.rs
1416 lines (1372 loc) · 58.7 KB
/
aarch64.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Atomic{I,U}128 implementation on AArch64.
//
// There are a few ways to implement 128-bit atomic operations in AArch64.
//
// - LDXP/STXP loop (DW LL/SC)
// - CASP (DWCAS) added as FEAT_LSE (mandatory from armv8.1-a)
// - LDP/STP (DW load/store) if FEAT_LSE2 (optional from armv8.2-a, mandatory from armv8.4-a) is available
// - LDIAPP/STILP (DW acquire-load/release-store) added as FEAT_LRCPC3 (optional from armv8.9-a/armv9.4-a) (if FEAT_LSE2 is also available)
// - LDCLRP/LDSETP/SWPP (DW RMW) added as FEAT_LSE128 (optional from armv9.4-a)
//
// If outline-atomics is not enabled and FEAT_LSE is not available at
// compile-time, we use LDXP/STXP loop.
// If outline-atomics is enabled and FEAT_LSE is not available at
// compile-time, we use CASP for CAS if FEAT_LSE is available
// at run-time, otherwise, use LDXP/STXP loop.
// If FEAT_LSE is available at compile-time, we use CASP for load/store/CAS/RMW.
// However, when portable_atomic_ll_sc_rmw cfg is set, use LDXP/STXP loop instead of CASP
// loop for RMW (by default, it is set on Apple hardware; see build script for details).
// If FEAT_LSE2 is available at compile-time, we use LDP/STP for load/store.
// If FEAT_LSE128 is available at compile-time, we use LDCLRP/LDSETP/SWPP for fetch_and/fetch_or/swap/{release,seqcst}-store.
// If FEAT_LSE2 and FEAT_LRCPC3 are available at compile-time, we use LDIAPP/STILP for acquire-load/release-store.
//
// Note: FEAT_LSE2 doesn't imply FEAT_LSE. FEAT_LSE128 implies FEAT_LSE but not FEAT_LSE2.
//
// Note that we do not separate LL and SC into separate functions, but handle
// them within a single asm block. This is because it is theoretically possible
// for the compiler to insert operations that might clear the reservation between
// LL and SC. Considering the type of operations we are providing and the fact
// that [progress64](https://github.com/ARM-software/progress64) uses such code,
// this is probably not a problem for aarch64, but it seems that aarch64 doesn't
// guarantee it and hexagon is the only architecture with hardware guarantees
// that such code works. See also:
//
// - https://yarchive.net/comp/linux/cmpxchg_ll_sc_portability.html
// - https://lists.llvm.org/pipermail/llvm-dev/2016-May/099490.html
// - https://lists.llvm.org/pipermail/llvm-dev/2018-June/123993.html
//
// Also, even when using a CAS loop to implement atomic RMW, include the loop itself
// in the asm block because it is more efficient for some codegen backends.
// https://github.com/rust-lang/compiler-builtins/issues/339#issuecomment-1191260474
//
// Note: On Miri and ThreadSanitizer which do not support inline assembly, we don't use
// this module and use intrinsics.rs instead.
//
// Refs:
// - ARM Compiler armasm User Guide
// https://developer.arm.com/documentation/dui0801/latest
// - Arm A-profile A64 Instruction Set Architecture
// https://developer.arm.com/documentation/ddi0602/latest
// - Arm Architecture Reference Manual for A-profile architecture
// https://developer.arm.com/documentation/ddi0487/latest
// - atomic-maybe-uninit https://github.com/taiki-e/atomic-maybe-uninit
//
// Generated asm:
// - aarch64 https://godbolt.org/z/M6Kh7z5T3
// - aarch64 msvc https://godbolt.org/z/f6h4onao9
// - aarch64 (+lse) https://godbolt.org/z/e4jKxxrM5
// - aarch64 msvc (+lse) https://godbolt.org/z/oav3aYjhM
// - aarch64 (+lse,+lse2) https://godbolt.org/z/EcW6fsr6K
// - aarch64 (+lse,+lse2,+rcpc3) https://godbolt.org/z/aa5ooa4G7
// - aarch64 (+lse2,+lse128) https://godbolt.org/z/nc3eKnseb
// - aarch64 (+lse2,+lse128,+rcpc3) https://godbolt.org/z/xbdhfzddh
include!("macros.rs");
// On musl with static linking, it seems that getauxval is not always available.
// See detect/auxv.rs for more.
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[cfg(any(
all(
target_os = "linux",
any(
target_env = "gnu",
all(any(target_env = "musl", target_env = "ohos"), not(target_feature = "crt-static")),
portable_atomic_outline_atomics,
),
),
target_os = "android",
target_os = "freebsd",
))]
#[path = "detect/auxv.rs"]
mod detect;
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[cfg(target_os = "openbsd")]
#[path = "detect/aarch64_aa64reg.rs"]
mod detect;
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[cfg(target_os = "fuchsia")]
#[path = "detect/aarch64_fuchsia.rs"]
mod detect;
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[cfg(target_os = "windows")]
#[path = "detect/aarch64_windows.rs"]
mod detect;
// test only
#[cfg(test)]
#[cfg(not(qemu))]
#[cfg(not(valgrind))]
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
#[path = "detect/aarch64_aa64reg.rs"]
mod detect_aa64reg;
#[cfg(test)]
#[cfg(not(portable_atomic_no_outline_atomics))]
#[cfg(target_os = "macos")]
#[path = "detect/aarch64_macos.rs"]
mod detect_macos;
#[cfg(not(portable_atomic_no_asm))]
use core::arch::asm;
use core::sync::atomic::Ordering;
use crate::utils::{Pair, U128};
#[cfg(any(
target_feature = "lse",
portable_atomic_target_feature = "lse",
not(portable_atomic_no_outline_atomics),
))]
macro_rules! debug_assert_lse {
() => {
#[cfg(all(
not(portable_atomic_no_outline_atomics),
any(
all(
target_os = "linux",
any(
target_env = "gnu",
all(
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "fuchsia",
target_os = "windows",
),
))]
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
{
debug_assert!(detect::detect().has_lse());
}
};
}
#[cfg(target_endian = "little")]
macro_rules! select_le_or_be {
($le:expr, $be:expr) => {
$le
};
}
#[cfg(target_endian = "big")]
macro_rules! select_le_or_be {
($le:expr, $be:expr) => {
$be
};
}
macro_rules! atomic_rmw {
($op:ident, $order:ident) => {
atomic_rmw!($op, $order, write = $order)
};
($op:ident, $order:ident, write = $write:ident) => {
match $order {
Ordering::Relaxed => $op!("", "", ""),
Ordering::Acquire => $op!("a", "", ""),
Ordering::Release => $op!("", "l", ""),
Ordering::AcqRel => $op!("a", "l", ""),
// In MSVC environments, SeqCst stores/writes needs fences after writes.
// https://reviews.llvm.org/D141748
#[cfg(target_env = "msvc")]
Ordering::SeqCst if $write == Ordering::SeqCst => $op!("a", "l", "dmb ish"),
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments.
Ordering::SeqCst => $op!("a", "l", ""),
_ => unreachable!("{:?}", $order),
}
};
}
#[inline]
unsafe fn atomic_load(src: *mut u128, order: Ordering) -> u128 {
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE2.
unsafe {
atomic_load_ldp(src, order)
}
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))]
{
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE.
unsafe {
_atomic_load_casp(src, order)
}
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
// SAFETY: the caller must uphold the safety contract.
unsafe {
_atomic_load_ldxp_stxp(src, order)
}
}
}
// If CPU supports FEAT_LSE2, LDP/LDIAPP is single-copy atomic reads,
// otherwise it is two single-copy atomic reads.
// Refs: B2.2.1 of the Arm Architecture Reference Manual Armv8, for Armv8-A architecture profile
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))]
#[inline]
unsafe fn atomic_load_ldp(src: *mut u128, order: Ordering) -> u128 {
debug_assert!(src as usize % 16 == 0);
// SAFETY: the caller must guarantee that `dst` is valid for reads,
// 16-byte aligned, that there are no concurrent non-atomic operations.
//
// Refs:
// - LDP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/LDP--A64-
unsafe {
let (out_lo, out_hi);
macro_rules! atomic_load_relaxed {
($acquire:tt $(, $readonly:tt)?) => {
asm!(
"ldp {out_lo}, {out_hi}, [{src}]",
$acquire,
src = in(reg) ptr_reg!(src),
out_hi = lateout(reg) out_hi,
out_lo = lateout(reg) out_lo,
options(nostack, preserves_flags $(, $readonly)?),
)
};
}
match order {
Ordering::Relaxed => atomic_load_relaxed!("", readonly),
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))]
Ordering::Acquire => {
// SAFETY: cfg guarantee that the CPU supports FEAT_LRCPC3.
// Refs: https://developer.arm.com/documentation/ddi0602/2023-03/Base-Instructions/LDIAPP--Load-Acquire-RCpc-ordered-Pair-of-registers-
asm!(
"ldiapp {out_lo}, {out_hi}, [{src}]",
src = in(reg) ptr_reg!(src),
out_hi = lateout(reg) out_hi,
out_lo = lateout(reg) out_lo,
options(nostack, preserves_flags),
);
}
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))]
Ordering::Acquire => atomic_load_relaxed!("dmb ishld"),
Ordering::SeqCst => {
asm!(
// ldar (or dmb ishld) is required to prevent reordering with preceding stlxp.
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108891 for details.
"ldar {tmp}, [{src}]",
"ldp {out_lo}, {out_hi}, [{src}]",
"dmb ishld",
src = in(reg) ptr_reg!(src),
out_hi = lateout(reg) out_hi,
out_lo = lateout(reg) out_lo,
tmp = out(reg) _,
options(nostack, preserves_flags),
);
}
_ => unreachable!("{:?}", order),
}
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole
}
}
// Do not use _atomic_compare_exchange_casp because it needs extra MOV to implement load.
#[cfg(any(test, not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))))]
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
#[inline]
unsafe fn _atomic_load_casp(src: *mut u128, order: Ordering) -> u128 {
debug_assert!(src as usize % 16 == 0);
debug_assert_lse!();
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE.
unsafe {
let (out_lo, out_hi);
macro_rules! atomic_load {
($acquire:tt, $release:tt) => {
asm!(
concat!("casp", $acquire, $release, " x2, x3, x2, x3, [{src}]"),
src = in(reg) ptr_reg!(src),
// must be allocated to even/odd register pair
inout("x2") 0_u64 => out_lo,
inout("x3") 0_u64 => out_hi,
options(nostack, preserves_flags),
)
};
}
match order {
Ordering::Relaxed => atomic_load!("", ""),
Ordering::Acquire => atomic_load!("a", ""),
Ordering::SeqCst => atomic_load!("a", "l"),
_ => unreachable!("{:?}", order),
}
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole
}
}
#[cfg(any(
test,
all(
not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")),
not(any(target_feature = "lse", portable_atomic_target_feature = "lse")),
),
))]
#[inline]
unsafe fn _atomic_load_ldxp_stxp(src: *mut u128, order: Ordering) -> u128 {
debug_assert!(src as usize % 16 == 0);
// SAFETY: the caller must uphold the safety contract.
unsafe {
let (mut out_lo, mut out_hi);
macro_rules! atomic_load {
($acquire:tt, $release:tt) => {
asm!(
"2:",
concat!("ld", $acquire, "xp {out_lo}, {out_hi}, [{src}]"),
concat!("st", $release, "xp {r:w}, {out_lo}, {out_hi}, [{src}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {r:w}, 2b",
src = in(reg) ptr_reg!(src),
out_lo = out(reg) out_lo,
out_hi = out(reg) out_hi,
r = out(reg) _,
options(nostack, preserves_flags),
)
};
}
match order {
Ordering::Relaxed => atomic_load!("", ""),
Ordering::Acquire => atomic_load!("a", ""),
Ordering::SeqCst => atomic_load!("a", "l"),
_ => unreachable!("{:?}", order),
}
U128 { pair: Pair { lo: out_lo, hi: out_hi } }.whole
}
}
#[inline]
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))]
{
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE2 and FEAT_FSE128.
unsafe {
// Use swpp if stp requires fences.
// https://reviews.llvm.org/D143506
match order {
Ordering::Relaxed => atomic_store_stp(dst, val, order),
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))]
Ordering::Release => atomic_store_stp(dst, val, order),
#[cfg(not(any(
target_feature = "rcpc3",
portable_atomic_target_feature = "rcpc3",
)))]
Ordering::Release => {
_atomic_swap_swpp(dst, val, order);
}
Ordering::SeqCst => {
_atomic_swap_swpp(dst, val, order);
}
_ => unreachable!("{:?}", order),
}
}
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE2.
unsafe {
atomic_store_stp(dst, val, order);
}
}
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))]
{
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set,
// we use CAS-based atomic RMW.
#[cfg(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE.
unsafe {
_atomic_swap_casp(dst, val, order);
}
#[cfg(not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
)))]
// SAFETY: the caller must uphold the safety contract.
unsafe {
_atomic_store_ldxp_stxp(dst, val, order);
}
}
}
// If CPU supports FEAT_LSE2, STP/STILP is single-copy atomic writes,
// otherwise it is two single-copy atomic writes.
// Refs: B2.2.1 of the Arm Architecture Reference Manual Armv8, for Armv8-A architecture profile
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))]
#[inline]
unsafe fn atomic_store_stp(dst: *mut u128, val: u128, order: Ordering) {
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must guarantee that `dst` is valid for writes,
// 16-byte aligned, that there are no concurrent non-atomic operations.
//
// Refs:
// - STP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/STP--A64-
unsafe {
let val = U128 { whole: val };
macro_rules! atomic_store {
($acquire:tt, $release:tt) => {
asm!(
$release,
"stp {val_lo}, {val_hi}, [{dst}]",
$acquire,
dst = in(reg) ptr_reg!(dst),
val_lo = in(reg) val.pair.lo,
val_hi = in(reg) val.pair.hi,
options(nostack, preserves_flags),
)
};
}
match order {
Ordering::Relaxed => atomic_store!("", ""),
#[cfg(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3"))]
Ordering::Release => {
// SAFETY: cfg guarantee that the CPU supports FEAT_LRCPC3.
// Refs: https://developer.arm.com/documentation/ddi0602/2023-03/Base-Instructions/STILP--Store-Release-ordered-Pair-of-registers-
asm!(
"stilp {val_lo}, {val_hi}, [{dst}]",
dst = in(reg) ptr_reg!(dst),
val_lo = in(reg) val.pair.lo,
val_hi = in(reg) val.pair.hi,
options(nostack, preserves_flags),
);
}
#[cfg(not(any(target_feature = "rcpc3", portable_atomic_target_feature = "rcpc3")))]
Ordering::Release => atomic_store!("", "dmb ish"),
Ordering::SeqCst => atomic_store!("dmb ish", "dmb ish"),
_ => unreachable!("{:?}", order),
}
}
}
// Do not use _atomic_swap_ldxp_stxp because it needs extra registers to implement store.
#[cfg(any(
test,
not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))
))]
#[inline]
unsafe fn _atomic_store_ldxp_stxp(dst: *mut u128, val: u128, order: Ordering) {
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must uphold the safety contract.
unsafe {
let val = U128 { whole: val };
macro_rules! store {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
"2:",
concat!("ld", $acquire, "xp xzr, {tmp}, [{dst}]"),
concat!("st", $release, "xp {tmp:w}, {val_lo}, {val_hi}, [{dst}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {tmp:w}, 2b",
$fence,
dst = in(reg) ptr_reg!(dst),
val_lo = in(reg) val.pair.lo,
val_hi = in(reg) val.pair.hi,
tmp = out(reg) _,
options(nostack, preserves_flags),
)
};
}
atomic_rmw!(store, order);
}
}
#[inline]
unsafe fn atomic_compare_exchange(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
failure: Ordering,
) -> Result<u128, u128> {
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE.
let prev = unsafe { _atomic_compare_exchange_casp(dst, old, new, success, failure) };
#[cfg(not(all(
not(portable_atomic_no_outline_atomics),
any(
all(
target_os = "linux",
any(
target_env = "gnu",
all(
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "fuchsia",
target_os = "windows",
),
)))]
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
// SAFETY: the caller must uphold the safety contract.
let prev = unsafe { _atomic_compare_exchange_ldxp_stxp(dst, old, new, success, failure) };
#[cfg(all(
not(portable_atomic_no_outline_atomics),
any(
all(
target_os = "linux",
any(
target_env = "gnu",
all(
any(target_env = "musl", target_env = "ohos"),
not(target_feature = "crt-static"),
),
portable_atomic_outline_atomics,
),
),
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "fuchsia",
target_os = "windows",
),
))]
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
let prev = {
fn_alias! {
#[target_feature(enable = "lse")]
unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128;
atomic_compare_exchange_casp_relaxed
= _atomic_compare_exchange_casp(Ordering::Relaxed, Ordering::Relaxed);
atomic_compare_exchange_casp_acquire
= _atomic_compare_exchange_casp(Ordering::Acquire, Ordering::Acquire);
atomic_compare_exchange_casp_release
= _atomic_compare_exchange_casp(Ordering::Release, Ordering::Relaxed);
atomic_compare_exchange_casp_acqrel
= _atomic_compare_exchange_casp(Ordering::AcqRel, Ordering::Acquire);
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments.
#[cfg(target_env = "msvc")]
atomic_compare_exchange_casp_seqcst
= _atomic_compare_exchange_casp(Ordering::SeqCst, Ordering::SeqCst);
}
fn_alias! {
unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128;
atomic_compare_exchange_ldxp_stxp_relaxed
= _atomic_compare_exchange_ldxp_stxp(Ordering::Relaxed, Ordering::Relaxed);
atomic_compare_exchange_ldxp_stxp_acquire
= _atomic_compare_exchange_ldxp_stxp(Ordering::Acquire, Ordering::Acquire);
atomic_compare_exchange_ldxp_stxp_release
= _atomic_compare_exchange_ldxp_stxp(Ordering::Release, Ordering::Relaxed);
atomic_compare_exchange_ldxp_stxp_acqrel
= _atomic_compare_exchange_ldxp_stxp(Ordering::AcqRel, Ordering::Acquire);
// AcqRel and SeqCst RMWs are equivalent in non-MSVC environments.
#[cfg(target_env = "msvc")]
atomic_compare_exchange_ldxp_stxp_seqcst
= _atomic_compare_exchange_ldxp_stxp(Ordering::SeqCst, Ordering::SeqCst);
}
// SAFETY: the caller must guarantee that `dst` is valid for both writes and
// reads, 16-byte aligned, that there are no concurrent non-atomic operations,
// and we've checked if FEAT_LSE is available.
unsafe {
let success = crate::utils::upgrade_success_ordering(success, failure);
match success {
Ordering::Relaxed => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_relaxed
} else {
atomic_compare_exchange_ldxp_stxp_relaxed
}
})
}
Ordering::Acquire => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_acquire
} else {
atomic_compare_exchange_ldxp_stxp_acquire
}
})
}
Ordering::Release => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_release
} else {
atomic_compare_exchange_ldxp_stxp_release
}
})
}
// AcqRel and SeqCst RMWs are equivalent in both implementations in non-MSVC environments.
#[cfg(not(target_env = "msvc"))]
Ordering::AcqRel | Ordering::SeqCst => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_acqrel
} else {
atomic_compare_exchange_ldxp_stxp_acqrel
}
})
}
#[cfg(target_env = "msvc")]
Ordering::AcqRel => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_acqrel
} else {
atomic_compare_exchange_ldxp_stxp_acqrel
}
})
}
#[cfg(target_env = "msvc")]
Ordering::SeqCst => {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128) -> u128 {
if detect::detect().has_lse() {
atomic_compare_exchange_casp_seqcst
} else {
atomic_compare_exchange_ldxp_stxp_seqcst
}
})
}
_ => unreachable!("{:?}", success),
}
}
};
if prev == old {
Ok(prev)
} else {
Err(prev)
}
}
#[cfg(any(
target_feature = "lse",
portable_atomic_target_feature = "lse",
not(portable_atomic_no_outline_atomics),
))]
#[cfg_attr(
not(any(target_feature = "lse", portable_atomic_target_feature = "lse")),
target_feature(enable = "lse")
)]
#[inline]
unsafe fn _atomic_compare_exchange_casp(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
failure: Ordering,
) -> u128 {
debug_assert!(dst as usize % 16 == 0);
debug_assert_lse!();
let order = crate::utils::upgrade_success_ordering(success, failure);
// SAFETY: the caller must guarantee that `dst` is valid for both writes and
// reads, 16-byte aligned, that there are no concurrent non-atomic operations,
// and the CPU supports FEAT_LSE.
//
// Refs:
// - https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/CASPA--CASPAL--CASP--CASPL--CASPAL--CASP--CASPL--A64-
// - https://developer.arm.com/documentation/ddi0602/2023-06/Base-Instructions/CASP--CASPA--CASPAL--CASPL--Compare-and-Swap-Pair-of-words-or-doublewords-in-memory-
unsafe {
let old = U128 { whole: old };
let new = U128 { whole: new };
let (prev_lo, prev_hi);
macro_rules! cmpxchg {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
concat!("casp", $acquire, $release, " x6, x7, x4, x5, [{dst}]"),
$fence,
dst = in(reg) ptr_reg!(dst),
// must be allocated to even/odd register pair
inout("x6") old.pair.lo => prev_lo,
inout("x7") old.pair.hi => prev_hi,
// must be allocated to even/odd register pair
in("x4") new.pair.lo,
in("x5") new.pair.hi,
options(nostack, preserves_flags),
)
};
}
atomic_rmw!(cmpxchg, order, write = success);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
#[cfg(any(test, not(any(target_feature = "lse", portable_atomic_target_feature = "lse"))))]
#[inline]
unsafe fn _atomic_compare_exchange_ldxp_stxp(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
failure: Ordering,
) -> u128 {
debug_assert!(dst as usize % 16 == 0);
let order = crate::utils::upgrade_success_ordering(success, failure);
// SAFETY: the caller must guarantee that `dst` is valid for both writes and
// reads, 16-byte aligned, and that there are no concurrent non-atomic operations.
//
// Refs:
// - LDXP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/LDXP--A64-
// - LDAXP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/LDAXP--A64-
// - STXP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/STXP--A64-
// - STLXP: https://developer.arm.com/documentation/dui0801/l/A64-Data-Transfer-Instructions/STLXP--A64-
//
// Note: Load-Exclusive pair (by itself) does not guarantee atomicity; to complete an atomic
// operation (even load/store), a corresponding Store-Exclusive pair must succeed.
// See Arm Architecture Reference Manual for A-profile architecture
// Section B2.2.1 "Requirements for single-copy atomicity", and
// Section B2.9 "Synchronization and semaphores" for more.
unsafe {
let old = U128 { whole: old };
let new = U128 { whole: new };
let (mut prev_lo, mut prev_hi);
macro_rules! cmpxchg {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
"2:",
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"),
"cmp {prev_lo}, {old_lo}",
"cset {r:w}, ne",
"cmp {prev_hi}, {old_hi}",
"cinc {r:w}, {r:w}, ne",
"cbz {r:w}, 3f",
concat!("st", $release, "xp {r:w}, {prev_lo}, {prev_hi}, [{dst}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {r:w}, 2b",
"b 4f",
"3:",
concat!("st", $release, "xp {r:w}, {new_lo}, {new_hi}, [{dst}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {r:w}, 2b",
"4:",
$fence,
dst = in(reg) ptr_reg!(dst),
old_lo = in(reg) old.pair.lo,
old_hi = in(reg) old.pair.hi,
new_lo = in(reg) new.pair.lo,
new_hi = in(reg) new.pair.hi,
prev_lo = out(reg) prev_lo,
prev_hi = out(reg) prev_hi,
r = out(reg) _,
// Do not use `preserves_flags` because CMP modifies the condition flags.
options(nostack),
)
};
}
atomic_rmw!(cmpxchg, order, write = success);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
// casp is always strong, and ldxp requires a corresponding (succeed) stxp for
// its atomicity (see code comment in _atomic_compare_exchange_ldxp_stxp).
// (i.e., aarch64 doesn't have 128-bit weak CAS)
use self::atomic_compare_exchange as atomic_compare_exchange_weak;
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set,
// we use CAS-based atomic RMW.
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))]
#[cfg(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))]
use _atomic_swap_casp as atomic_swap;
#[cfg(not(any(target_feature = "lse128", portable_atomic_target_feature = "lse128")))]
#[cfg(not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
)))]
use _atomic_swap_ldxp_stxp as atomic_swap;
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))]
use _atomic_swap_swpp as atomic_swap;
#[cfg(any(target_feature = "lse128", portable_atomic_target_feature = "lse128"))]
#[inline]
unsafe fn _atomic_swap_swpp(dst: *mut u128, val: u128, order: Ordering) -> u128 {
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must guarantee that `dst` is valid for both writes and
// reads, 16-byte aligned, that there are no concurrent non-atomic operations,
// and the CPU supports FEAT_LSE128.
//
// Refs:
// - https://developer.arm.com/documentation/ddi0602/2023-03/Base-Instructions/SWPP--SWPPA--SWPPAL--SWPPL--Swap-quadword-in-memory-?lang=en
unsafe {
let val = U128 { whole: val };
let (prev_lo, prev_hi);
macro_rules! swap {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
concat!("swpp", $acquire, $release, " {val_lo}, {val_hi}, [{dst}]"),
$fence,
dst = in(reg) ptr_reg!(dst),
val_lo = inout(reg) val.pair.lo => prev_lo,
val_hi = inout(reg) val.pair.hi => prev_hi,
options(nostack, preserves_flags),
)
};
}
atomic_rmw!(swap, order);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
// Do not use atomic_rmw_cas_3 because it needs extra MOV to implement swap.
#[cfg(any(
test,
all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
)
))]
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
#[inline]
unsafe fn _atomic_swap_casp(dst: *mut u128, val: u128, order: Ordering) -> u128 {
debug_assert!(dst as usize % 16 == 0);
debug_assert_lse!();
// SAFETY: the caller must uphold the safety contract.
// cfg guarantee that the CPU supports FEAT_LSE.
unsafe {
let val = U128 { whole: val };
let (mut prev_lo, mut prev_hi);
macro_rules! swap {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
// If FEAT_LSE2 is not supported, this works like byte-wise atomic.
// This is not single-copy atomic reads, but this is ok because subsequent
// CAS will check for consistency.
"ldp x4, x5, [{dst}]",
"2:",
// casp writes the current value to the first register pair,
// so copy the `out`'s value for later comparison.
"mov {tmp_lo}, x4",
"mov {tmp_hi}, x5",
concat!("casp", $acquire, $release, " x4, x5, x2, x3, [{dst}]"),
"cmp {tmp_hi}, x5",
"ccmp {tmp_lo}, x4, #0, eq",
"b.ne 2b",
$fence,
dst = in(reg) ptr_reg!(dst),
tmp_lo = out(reg) _,
tmp_hi = out(reg) _,
// must be allocated to even/odd register pair
out("x4") prev_lo,
out("x5") prev_hi,
// must be allocated to even/odd register pair
in("x2") val.pair.lo,
in("x3") val.pair.hi,
// Do not use `preserves_flags` because CMP and CCMP modify the condition flags.
options(nostack),
)
};
}
atomic_rmw!(swap, order);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
// Do not use atomic_rmw_ll_sc_3 because it needs extra MOV to implement swap.
#[cfg(any(
test,
not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))
))]
#[inline]
unsafe fn _atomic_swap_ldxp_stxp(dst: *mut u128, val: u128, order: Ordering) -> u128 {
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must uphold the safety contract.
unsafe {
let val = U128 { whole: val };
let (mut prev_lo, mut prev_hi);
macro_rules! swap {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
"2:",
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"),
concat!("st", $release, "xp {r:w}, {val_lo}, {val_hi}, [{dst}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {r:w}, 2b",
$fence,
dst = in(reg) ptr_reg!(dst),
val_lo = in(reg) val.pair.lo,
val_hi = in(reg) val.pair.hi,
prev_lo = out(reg) prev_lo,
prev_hi = out(reg) prev_hi,
r = out(reg) _,
options(nostack, preserves_flags),
)
};
}
atomic_rmw!(swap, order);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
/// Atomic RMW by LL/SC loop (3 arguments)
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;`
///
/// `$op` can use the following registers:
/// - val_lo/val_hi pair: val argument (read-only for `$op`)
/// - prev_lo/prev_hi pair: previous value loaded by ll (read-only for `$op`)
/// - new_lo/new_hi pair: new value that will to stored by sc
macro_rules! atomic_rmw_ll_sc_3 {
($name:ident as $reexport_name:ident $(($preserves_flags:tt))?, $($op:tt)*) => {
// If FEAT_LSE is available at compile-time and portable_atomic_ll_sc_rmw cfg is not set,
// we use CAS-based atomic RMW generated by atomic_rmw_cas_3! macro instead.
#[cfg(not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
)))]
use $name as $reexport_name;
#[cfg(any(
test,
not(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))
))]
#[inline]
unsafe fn $name(dst: *mut u128, val: u128, order: Ordering) -> u128 {
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must uphold the safety contract.
unsafe {
let val = U128 { whole: val };
let (mut prev_lo, mut prev_hi);
macro_rules! op {
($acquire:tt, $release:tt, $fence:tt) => {
asm!(
"2:",
concat!("ld", $acquire, "xp {prev_lo}, {prev_hi}, [{dst}]"),
$($op)*
concat!("st", $release, "xp {r:w}, {new_lo}, {new_hi}, [{dst}]"),
// 0 if the store was successful, 1 if no store was performed
"cbnz {r:w}, 2b",
$fence,
dst = in(reg) ptr_reg!(dst),
val_lo = in(reg) val.pair.lo,
val_hi = in(reg) val.pair.hi,
prev_lo = out(reg) prev_lo,
prev_hi = out(reg) prev_hi,
new_lo = out(reg) _,
new_hi = out(reg) _,
r = out(reg) _,
options(nostack $(, $preserves_flags)?),
)
};
}
atomic_rmw!(op, order);
U128 { pair: Pair { lo: prev_lo, hi: prev_hi } }.whole
}
}
};
}
/// Atomic RMW by CAS loop (3 arguments)
/// `unsafe fn(dst: *mut u128, val: u128, order: Ordering) -> u128;`
///
/// `$op` can use the following registers:
/// - val_lo/val_hi pair: val argument (read-only for `$op`)
/// - x6/x7 pair: previous value loaded (read-only for `$op`)
/// - x4/x5 pair: new value that will to stored
macro_rules! atomic_rmw_cas_3 {
($name:ident as $reexport_name:ident, $($op:tt)*) => {
// If FEAT_LSE is not available at compile-time or portable_atomic_ll_sc_rmw cfg is set,
// we use LL/SC-based atomic RMW generated by atomic_rmw_ll_sc_3! macro instead.
#[cfg(all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
))]
use $name as $reexport_name;
#[cfg(any(
test,
all(
any(target_feature = "lse", portable_atomic_target_feature = "lse"),
not(portable_atomic_ll_sc_rmw),
)
))]
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
#[inline]