-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathaarch64.rs
More file actions
500 lines (468 loc) · 17.8 KB
/
aarch64.rs
File metadata and controls
500 lines (468 loc) · 17.8 KB
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
// Atomic{I,U}128 implementation for 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 (armv8.1-a)
// - LDP/STP (DW load/store) if FEAT_LSE2 (armv8.4-a) is available
//
// If the `outline-atomics` feature is not enabled, we use CASP if
// FEAT_LSE is enabled at compile-time, otherwise, use LDXP/STXP loop.
// If the `outline-atomics` feature is enabled, we use CASP for
// compare_exchange(_weak) if FEAT_LSE is available at run-time.
// If FEAT_LSE2 is available at compile-time, we use LDP/STP for load/store.
//
// Note: As of rustc 1.61.0, -C target-feature=+lse2 does not
// implicitly enable lse. Also, target_feature "lse2" is not available on rustc side:
// https://github.com/rust-lang/rust/blob/1.61.0/compiler/rustc_codegen_ssa/src/target_features.rs#L45
//
// Refs:
// - ARM Compiler armasm User Guide
// https://developer.arm.com/documentation/dui0801/latest
// - Arm Architecture Reference Manual for A-profile architecture
// https://developer.arm.com/documentation/ddi0487/latest
// - progress64 https://github.com/ARM-software/progress64
// - atomic-maybe-uninit https://github.com/taiki-e/atomic-maybe-uninit
//
// Generated asm:
// - aarch64 https://godbolt.org/z/s8jb3K1aj
// - aarch64 (+lse) https://godbolt.org/z/4hvWj9eME
// - aarch64 (+lse,+lse2) https://godbolt.org/z/5d3jsfPM8
include!("macros.rs");
#[cfg(not(portable_atomic_no_asm))]
use core::arch::asm;
use core::sync::atomic::Ordering;
/// A 128-bit value represented as a pair of 64-bit values.
// This type is #[repr(C)], both fields have the same in-memory representation
// and are plain old datatypes, so access to the fields is always safe.
#[derive(Clone, Copy)]
#[repr(C)]
union U128 {
whole: u128,
pair: [u64; 2],
}
// Note: ldxp (by itself) does not guarantee atomicity; to complete an atomic
// operation (even load/store), a corresponding stxp 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.
#[inline(always)]
unsafe fn ldxp(src: *mut u128, order: Ordering) -> u128 {
debug_assert!(src as usize % 16 == 0);
// SAFETY: the caller must guarantee that `src` 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/g/A64-Data-Transfer-Instructions/LDXP
// - LDAXP: https://developer.arm.com/documentation/dui0801/g/A64-Data-Transfer-Instructions/LDAXP
unsafe {
let (prev_lo, prev_hi);
match order {
Ordering::Relaxed => {
asm!(
"ldxp {prev_lo}, {prev_hi}, [{src}]",
src = in(reg) src,
prev_lo = out(reg) prev_lo,
prev_hi = out(reg) prev_hi,
options(nostack),
);
}
Ordering::Acquire | Ordering::SeqCst => {
asm!(
"ldaxp {prev_lo}, {prev_hi}, [{src}]",
src = in(reg) src,
prev_lo = out(reg) prev_lo,
prev_hi = out(reg) prev_hi,
options(nostack),
);
}
_ => unreachable!("{:?}", order),
}
U128 { pair: [prev_lo, prev_hi] }.whole
}
}
#[inline(always)]
unsafe fn stxp(dst: *mut u128, val: u128, order: Ordering) -> bool {
debug_assert!(dst as usize % 16 == 0);
// 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:
// - STXP: https://developer.arm.com/documentation/dui0801/g/A64-Data-Transfer-Instructions/STXP
// - STLXP: https://developer.arm.com/documentation/dui0801/g/A64-Data-Transfer-Instructions/STLXP
unsafe {
let r: i32;
let val = U128 { whole: val };
match order {
Ordering::Relaxed => {
asm!(
"stxp {r:w}, {val_lo}, {val_hi}, [{dst}]",
dst = in(reg) dst,
r = out(reg) r,
val_lo = in(reg) val.pair[0],
val_hi = in(reg) val.pair[1],
options(nostack),
);
}
Ordering::Release | Ordering::SeqCst => {
asm!(
"stlxp {r:w}, {val_lo}, {val_hi}, [{dst}]",
dst = in(reg) dst,
r = out(reg) r,
val_lo = in(reg) val.pair[0],
val_hi = in(reg) val.pair[1],
options(nostack),
);
}
_ => unreachable!("{:?}", order),
}
// 0 if the store was successful, 1 if no store was performed
debug_assert!(r == 0 || r == 1, "r={}", r);
r == 0
}
}
#[cfg(any(
target_feature = "lse",
portable_atomic_target_feature = "lse",
not(portable_atomic_no_aarch64_target_feature),
))]
#[cfg_attr(not(portable_atomic_no_aarch64_target_feature), target_feature(enable = "lse"))]
#[inline]
unsafe fn _casp(dst: *mut u128, old: u128, new: 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_LSE.
//
// Refs:
// - https://developer.arm.com/documentation/dui0801/g/A64-Data-Transfer-Instructions/CASPA--CASPAL--CASP--CASPL--CASPAL--CASP--CASPL
unsafe {
let old = U128 { whole: old };
let new = U128 { whole: new };
let (prev_lo, prev_hi);
match order {
Ordering::Relaxed => {
asm!(
"casp x6, x7, x4, x5, [{dst}]",
dst = in(reg) dst,
// must be allocated to even/odd register pair
inout("x6") old.pair[0] => prev_lo,
inout("x7") old.pair[1] => prev_hi,
// must be allocated to even/odd register pair
in("x4") new.pair[0],
in("x5") new.pair[1],
options(nostack),
);
}
Ordering::Acquire => {
asm!(
"caspa x6, x7, x4, x5, [{dst}]",
dst = in(reg) dst,
// must be allocated to even/odd register pair
inout("x6") old.pair[0] => prev_lo,
inout("x7") old.pair[1] => prev_hi,
// must be allocated to even/odd register pair
in("x4") new.pair[0],
in("x5") new.pair[1],
options(nostack),
);
}
Ordering::Release => {
asm!(
"caspl x6, x7, x4, x5, [{dst}]",
dst = in(reg) dst,
// must be allocated to even/odd register pair
inout("x6") old.pair[0] => prev_lo,
inout("x7") old.pair[1] => prev_hi,
// must be allocated to even/odd register pair
in("x4") new.pair[0],
in("x5") new.pair[1],
options(nostack),
);
}
Ordering::AcqRel | Ordering::SeqCst => {
asm!(
"caspal x6, x7, x4, x5, [{dst}]",
dst = in(reg) dst,
// must be allocated to even/odd register pair
inout("x6") old.pair[0] => prev_lo,
inout("x7") old.pair[1] => prev_hi,
// must be allocated to even/odd register pair
in("x4") new.pair[0],
in("x5") new.pair[1],
options(nostack),
);
}
_ => unreachable!("{:?}", order),
}
U128 { pair: [prev_lo, prev_hi] }.whole
}
}
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2", test))]
#[inline]
unsafe fn _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,
// and the CPU supports FEAT_LSE2.
unsafe {
let (prev_lo, prev_hi);
match order {
Ordering::Relaxed => {
asm!(
"ldp {prev_lo}, {prev_hi}, [{src}]",
src = in(reg) src,
prev_lo = lateout(reg) prev_lo,
prev_hi = lateout(reg) prev_hi,
options(nostack),
);
}
Ordering::Acquire => {
asm!(
"ldp {prev_lo}, {prev_hi}, [{src}]",
"dmb ishld",
src = in(reg) src,
prev_lo = lateout(reg) prev_lo,
prev_hi = lateout(reg) prev_hi,
options(nostack),
);
}
Ordering::SeqCst => {
asm!(
"ldp {prev_lo}, {prev_hi}, [{src}]",
"dmb ish",
src = in(reg) src,
prev_lo = lateout(reg) prev_lo,
prev_hi = lateout(reg) prev_hi,
options(nostack),
);
}
_ => unreachable!("{:?}", order),
}
U128 { pair: [prev_lo, prev_hi] }.whole
}
}
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2", test))]
#[inline]
unsafe fn _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,
// and the CPU supports FEAT_LSE2.
unsafe {
let val = U128 { whole: val };
match order {
Ordering::Relaxed => {
asm!(
"stp {val_lo}, {val_hi}, [{dst}]",
dst = in(reg) dst,
val_lo = in(reg) val.pair[0],
val_hi = in(reg) val.pair[1],
options(nostack),
);
}
Ordering::Release => {
asm!(
"dmb ish",
"stp {val_lo}, {val_hi}, [{dst}]",
dst = in(reg) dst,
val_lo = in(reg) val.pair[0],
val_hi = in(reg) val.pair[1],
options(nostack),
);
}
Ordering::SeqCst => {
asm!(
"dmb ish",
"stp {val_lo}, {val_hi}, [{dst}]",
"dmb ish",
dst = in(reg) dst,
val_lo = in(reg) val.pair[0],
val_hi = in(reg) val.pair[1],
options(nostack),
);
}
_ => unreachable!("{:?}", order),
}
}
}
#[inline]
fn ldx_ordering(order: Ordering) -> Ordering {
match order {
Ordering::Release | Ordering::Relaxed => Ordering::Relaxed,
Ordering::SeqCst => Ordering::SeqCst,
Ordering::AcqRel | Ordering::Acquire => Ordering::Acquire,
_ => unreachable!("{:?}", order),
}
}
#[inline]
fn stx_ordering(order: Ordering) -> Ordering {
match order {
Ordering::Acquire | Ordering::Relaxed => Ordering::Relaxed,
Ordering::SeqCst => Ordering::SeqCst,
Ordering::AcqRel | Ordering::Release => Ordering::Release,
_ => unreachable!("{:?}", order),
}
}
#[inline]
unsafe fn atomic_compare_exchange(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
failure: Ordering,
) -> Result<u128, u128> {
let success = crate::utils::upgrade_success_ordering(success, failure);
#[cfg(any(target_feature = "lse", portable_atomic_target_feature = "lse"))]
// SAFETY: the caller must uphold the safety contract for `atomic_compare_exchange`.
// cfg guarantee that the CPU supports FEAT_LSE.
let res = unsafe { _casp(dst, old, new, success) };
#[cfg(not(all(
not(portable_atomic_no_aarch64_target_feature),
feature = "outline-atomics",
// https://github.com/rust-lang/stdarch/blob/bcbe010614f398ec86f3a9274d22e33e5f2ee60b/crates/std_detect/src/detect/mod.rs
// Note: aarch64 freebsd is tier 3, so std may not be available.
any(feature = "std", target_os = "linux", target_os = "windows", /* target_os = "freebsd" */)
)))]
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
// SAFETY: the caller must uphold the safety contract for `atomic_compare_exchange`.
let res = unsafe { _compare_exchange_ldxp_stxp(dst, old, new, success) };
#[cfg(all(
not(portable_atomic_no_aarch64_target_feature),
feature = "outline-atomics",
// https://github.com/rust-lang/stdarch/blob/bcbe010614f398ec86f3a9274d22e33e5f2ee60b/crates/std_detect/src/detect/mod.rs
// Note: aarch64 freebsd is tier 3, so std may not be available.
any(feature = "std", target_os = "linux", target_os = "windows", /* target_os = "freebsd" */)
))]
#[cfg(not(any(target_feature = "lse", portable_atomic_target_feature = "lse")))]
let res = {
extern crate std;
// 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 {
ifunc!(unsafe fn(dst: *mut u128, old: u128, new: u128, success: Ordering) -> u128
= if std::arch::is_aarch64_feature_detected!("lse") {
_casp
} else {
_compare_exchange_ldxp_stxp
})
}
};
if res == old {
Ok(res)
} else {
Err(res)
}
}
// LLVM appears to generate strong CAS for aarch64 128-bit weak CAS,
// so we always use strong CAS.
use self::atomic_compare_exchange as atomic_compare_exchange_weak;
#[inline]
unsafe fn _compare_exchange_ldxp_stxp(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
) -> u128 {
// SAFETY: the caller must uphold the safety contract for `compare_exchange_ldxp_stxp`.
unsafe { atomic_update(dst, success, |x| if x == old { new } else { x }) }
}
// Note: closure should not panic.
#[inline]
unsafe fn atomic_update<F>(dst: *mut u128, order: Ordering, mut f: F) -> u128
where
F: FnMut(u128) -> u128,
{
debug_assert!(dst as usize % 16 == 0);
// SAFETY: the caller must uphold the safety contract for `atomic_update`.
unsafe {
let ldx_order = ldx_ordering(order);
let stx_order = stx_ordering(order);
let mut prev;
loop {
prev = ldxp(dst, ldx_order);
let next = f(prev);
if stxp(dst, next, stx_order) {
break;
}
}
prev
}
}
#[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 for `atomic_load`.
// cfg guarantee that the CPU supports FEAT_LSE2.
unsafe {
_ldp(src, order)
}
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))]
// SAFETY: the caller must uphold the safety contract for `atomic_load`.
unsafe {
_compare_exchange_ldxp_stxp(src, 0, 0, order)
}
}
#[inline]
unsafe fn atomic_store(dst: *mut u128, val: u128, order: Ordering) {
#[cfg(any(target_feature = "lse2", portable_atomic_target_feature = "lse2"))]
// SAFETY: the caller must uphold the safety contract for `atomic_store`.
// cfg guarantee that the CPU supports FEAT_LSE2.
unsafe {
_stp(dst, val, order);
}
#[cfg(not(any(target_feature = "lse2", portable_atomic_target_feature = "lse2")))]
// SAFETY: the caller must uphold the safety contract for `atomic_store`.
unsafe {
atomic_swap(dst, val, order);
}
}
#[inline]
unsafe fn atomic_swap(dst: *mut u128, val: u128, order: Ordering) -> u128 {
// SAFETY: the caller must uphold the safety contract for `atomic_swap`.
unsafe { atomic_update(dst, order, |_| val) }
}
atomic128!(AtomicI128, i128);
atomic128!(AtomicU128, u128);
// Miri and Sanitizer do not support inline assembly.
#[cfg(not(any(miri, sanitize_thread)))]
#[cfg(test)]
mod tests {
use super::*;
test_atomic_int!(i128);
test_atomic_int!(u128);
}
// Miri and Sanitizer do not support inline assembly.
#[cfg(not(any(miri, sanitize_thread)))]
#[cfg(test)]
#[allow(dead_code, clippy::undocumented_unsafe_blocks, clippy::wildcard_imports)]
mod no_outline_atomics {
use super::*;
#[inline]
unsafe fn atomic_compare_exchange(
dst: *mut u128,
old: u128,
new: u128,
success: Ordering,
_failure: Ordering,
) -> Result<u128, u128> {
// SAFETY: the caller must uphold the safety contract for `atomic_compare_exchange`.
let res = unsafe { _compare_exchange_ldxp_stxp(dst, old, new, success) };
if res == old {
Ok(res)
} else {
Err(res)
}
}
// LLVM appears to generate strong CAS for aarch64 128-bit weak CAS,
// so we always use strong CAS.
use self::atomic_compare_exchange as atomic_compare_exchange_weak;
atomic128!(AtomicI128, i128);
atomic128!(AtomicU128, u128);
mod tests {
use super::*;
test_atomic_int!(i128);
test_atomic_int!(u128);
}
}