|
| 1 | +//! Aarch64 targets have two possible implementations for atomics: |
| 2 | +//! 1. Load-Locked, Store-Conditional (LL/SC), older and slower. |
| 3 | +//! 2. Large System Extensions (LSE), newer and faster. |
| 4 | +//! To avoid breaking backwards compat, C toolchains introduced a concept of "outlined atomics", |
| 5 | +//! where atomic operations call into the compiler runtime to dispatch between two depending on |
| 6 | +//! which is supported on the current CPU. |
| 7 | +//! See https://community.arm.com/arm-community-blogs/b/tools-software-ides-blog/posts/making-the-most-of-the-arm-architecture-in-gcc-10#:~:text=out%20of%20line%20atomics for more discussion. |
| 8 | +//! |
| 9 | +//! Currently we only support LL/SC, because LSE requires `getauxval` from libc in order to do runtime detection. |
| 10 | +//! Use the `compiler-rt` intrinsics if you want LSE support. |
| 11 | +//! |
| 12 | +//! Ported from `aarch64/lse.S` in LLVM's compiler-rt. |
| 13 | +//! |
| 14 | +//! Generate functions for each of the following symbols: |
| 15 | +//! __aarch64_swpN_ORDER |
| 16 | +//! __aarch64_ldaddN_ORDER |
| 17 | +//! __aarch64_ldclrN_ORDER |
| 18 | +//! __aarch64_ldeorN_ORDER |
| 19 | +//! __aarch64_ldsetN_ORDER |
| 20 | +//! for N = {1, 2, 4, 8}, M = {1, 2, 4, 8}, ORDER = { relax, acq, rel, acq_rel } |
| 21 | +//! |
| 22 | +//! TODO: M = 16 |
| 23 | +//! |
| 24 | +//! The original `lse.S` has some truly horrifying code that expects to be compiled multiple times with different constants. |
| 25 | +//! We do something similar, but with macro arguments. |
| 26 | +
|
| 27 | +/// We don't do runtime dispatch so we don't have to worry about the global ctor. |
| 28 | +/// Apparently MacOS uses a different number of underscores in the symbol name (???) |
| 29 | +// #[cfg(target_vendor = "apple")] |
| 30 | +// macro_rules! have_lse { |
| 31 | +// () => { ___aarch64_have_lse_atomics } |
| 32 | +// } |
| 33 | + |
| 34 | +// #[cfg(not(target_vendor = "apple"))] |
| 35 | +// macro_rules! have_lse { |
| 36 | +// () => { __aarch64_have_lse_atomics } |
| 37 | +// } |
| 38 | + |
| 39 | +/// Translate a byte size to a Rust type. |
| 40 | +macro_rules! int_ty { |
| 41 | + // forward |
| 42 | + // ($bytes:tt) => { int_ty!(@ $bytes) }; |
| 43 | + (1) => { i8 }; |
| 44 | + (2) => { i16 }; |
| 45 | + (4) => { i32 }; |
| 46 | + (8) => { i64 }; |
| 47 | + (16) => { i128 }; |
| 48 | +} |
| 49 | + |
| 50 | +/// Given a byte size and a register number, return a register of the appropriate size. |
| 51 | +/// |
| 52 | +/// See <https://developer.arm.com/documentation/102374/0101/Registers-in-AArch64---general-purpose-registers>. |
| 53 | +macro_rules! reg { |
| 54 | + (1, $num:literal) => { concat!("w", $num) }; |
| 55 | + (2, $num:literal) => { concat!("w", $num) }; |
| 56 | + (4, $num:literal) => { concat!("w", $num) }; |
| 57 | + (8, $num:literal) => { concat!("x", $num) }; |
| 58 | +} |
| 59 | + |
| 60 | +/// Given an atomic ordering, translate it to the acquire suffix for the lxdr aarch64 ASM instruction. |
| 61 | +macro_rules! acquire { |
| 62 | + (Relaxed) => { "" }; |
| 63 | + (Acquire) => { "a" }; |
| 64 | + (Release) => { "" }; |
| 65 | + (AcqRel) => { "a" }; |
| 66 | +} |
| 67 | + |
| 68 | +/// Given an atomic ordering, translate it to the release suffix for the stxr aarch64 ASM instruction. |
| 69 | +macro_rules! release { |
| 70 | + (Relaxed) => { "" }; |
| 71 | + (Acquire) => { "" }; |
| 72 | + (Release) => { "l" }; |
| 73 | + (AcqRel) => { "l" }; |
| 74 | +} |
| 75 | + |
| 76 | +/// Given a size in bytes, translate it to the byte suffix for the aarch64 ASM instruction. |
| 77 | +macro_rules! size { |
| 78 | + (1) => { "b" }; |
| 79 | + (2) => { "h" }; |
| 80 | + (4) => { "" }; |
| 81 | + (8) => { "" }; |
| 82 | + (16) => { "" }; |
| 83 | +} |
| 84 | + |
| 85 | +/// Given a byte size, translate it to an Unsigned eXTend instruction |
| 86 | +/// with the correct semantics. |
| 87 | +/// |
| 88 | +/// See <https://developer.arm.com/documentation/ddi0596/2020-12/Base-Instructions/UXTB--Unsigned-Extend-Byte--an-alias-of-UBFM-> |
| 89 | +macro_rules! uxt { |
| 90 | + // forward |
| 91 | + ($bytes:tt) => { uxt!(@ $bytes) }; |
| 92 | + (@ 1) => { "uxtb" }; |
| 93 | + (@ 2) => { "uxth" }; |
| 94 | + (@ $_:tt) => { "mov" }; |
| 95 | +} |
| 96 | + |
| 97 | +/// Given an atomic ordering and byte size, translate it to a LoaD eXclusive Register instruction |
| 98 | +/// with the correct semantics. |
| 99 | +/// |
| 100 | +/// See <https://developer.arm.com/documentation/ddi0596/2020-12/Base-Instructions/LDXR--Load-Exclusive-Register->. |
| 101 | +macro_rules! ldxr { |
| 102 | + ($ordering:ident, $bytes:tt) => { concat!("ld", acquire!($ordering), "xr", size!($bytes)) } |
| 103 | +} |
| 104 | + |
| 105 | +/// Given an atomic ordering and byte size, translate it to a STore eXclusive Register instruction |
| 106 | +/// with the correct semantics. |
| 107 | +/// |
| 108 | +/// See <https://developer.arm.com/documentation/ddi0596/2020-12/Base-Instructions/STXR--Store-Exclusive-Register->. |
| 109 | +macro_rules! stxr { |
| 110 | + ($ordering:ident, $bytes:tt) => { concat!("st", release!($ordering), "xr", size!($bytes)) } |
| 111 | +} |
| 112 | + |
| 113 | +/// See <https://doc.rust-lang.org/stable/std/sync/atomic/struct.AtomicI8.html#method.compare_and_swap>. |
| 114 | +macro_rules! compare_and_swap { |
| 115 | + ($ordering:ident, $bytes:tt, $name:ident) => { |
| 116 | + intrinsics! { |
| 117 | + #[maybe_use_optimized_c_shim] |
| 118 | + #[naked] |
| 119 | + pub extern "C" fn $name ( |
| 120 | + expected: int_ty!($bytes), desired: int_ty!($bytes), ptr: *mut int_ty!($bytes) |
| 121 | + ) -> int_ty!($bytes) { |
| 122 | + // We can't use `AtomicI8::compare_and_swap`; we *are* compare_and_swap. |
| 123 | + unsafe { core::arch::asm! { |
| 124 | + // UXT s(tmp0), s(0) |
| 125 | + concat!(uxt!($bytes), " ", reg!($bytes, 16), ", ", reg!($bytes, 0)), |
| 126 | + "0:", |
| 127 | + // LDXR s(0), [x2] |
| 128 | + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x2]"), |
| 129 | + // cmp s(0), s(tmp0) |
| 130 | + concat!("cmp ", reg!($bytes, 0), ", ", reg!($bytes, 16)), |
| 131 | + "bne 1f", |
| 132 | + // STXR w(tmp1), s(1), [x2] |
| 133 | + concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 1), ", [x2]"), |
| 134 | + "cbnz w17, 0b", |
| 135 | + "1:", |
| 136 | + "ret", |
| 137 | + options(noreturn) |
| 138 | + } } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +macro_rules! swap { |
| 146 | + ($ordering:ident, $bytes:tt, $name:ident) => { |
| 147 | + intrinsics! { |
| 148 | + #[maybe_use_optimized_c_shim] |
| 149 | + #[naked] |
| 150 | + pub extern "C" fn $name ( |
| 151 | + left: int_ty!($bytes), right_ptr: *mut int_ty!($bytes) |
| 152 | + ) -> int_ty!($bytes) { |
| 153 | + unsafe { core::arch::asm! { |
| 154 | + // mov s(tmp0), s(0) |
| 155 | + concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), |
| 156 | + "0:", |
| 157 | + // LDXR s(0), [x1] |
| 158 | + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), |
| 159 | + // STXR w(tmp1), s(tmp0), [x1] |
| 160 | + concat!(stxr!($ordering, $bytes), " w17, ", reg!($bytes, 16), ", [x1]"), |
| 161 | + "cbnz w17, 0b", |
| 162 | + "ret", |
| 163 | + options(noreturn) |
| 164 | + } } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +macro_rules! fetch_op { |
| 171 | + ($ordering:ident, $bytes:tt, $name:ident, $op:literal) => { |
| 172 | + intrinsics! { |
| 173 | + #[maybe_use_optimized_c_shim] |
| 174 | + #[naked] |
| 175 | + pub extern "C" fn $name ( |
| 176 | + val: int_ty!($bytes), ptr: *mut int_ty!($bytes) |
| 177 | + ) -> int_ty!($bytes) { |
| 178 | + unsafe { core::arch::asm! { |
| 179 | + // mov s(tmp0), s(0) |
| 180 | + concat!("mov ", reg!($bytes, 16), ", ", reg!($bytes, 0)), |
| 181 | + "0:", |
| 182 | + // LDXR s(0), [x1] |
| 183 | + concat!(ldxr!($ordering, $bytes), " ", reg!($bytes, 0), ", [x1]"), |
| 184 | + // OP s(tmp1), s(0), s(tmp0) |
| 185 | + concat!($op, " ", reg!($bytes, 17), ", ", reg!($bytes, 0), ", ", reg!($bytes, 16)), |
| 186 | + // STXR w(tmp2), s(tmp1), [x1] |
| 187 | + concat!(stxr!($ordering, $bytes), " w15, ", reg!($bytes, 17), ", [x1]"), |
| 188 | + "cbnz w15, 0b", |
| 189 | + "ret", |
| 190 | + options(noreturn) |
| 191 | + } } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +macro_rules! add { |
| 198 | + ($ordering:ident, $bytes:tt, $name:ident) => { fetch_op! { $ordering, $bytes, $name, "add" } } |
| 199 | +} |
| 200 | + |
| 201 | +macro_rules! and { |
| 202 | + ($ordering:ident, $bytes:tt, $name:ident) => { fetch_op! { $ordering, $bytes, $name, "bic" } } |
| 203 | +} |
| 204 | + |
| 205 | +macro_rules! xor { |
| 206 | + ($ordering:ident, $bytes:tt, $name:ident) => { fetch_op! { $ordering, $bytes, $name, "eor" } } |
| 207 | +} |
| 208 | + |
| 209 | +macro_rules! or { |
| 210 | + ($ordering:ident, $bytes:tt, $name:ident) => { fetch_op! { $ordering, $bytes, $name, "orr" } } |
| 211 | +} |
| 212 | + |
| 213 | +include!(concat!(env!("OUT_DIR"), "/outlined_atomics.rs")); |
| 214 | +foreach_cas!(compare_and_swap); |
| 215 | +foreach_swp!(swap); |
| 216 | +foreach_ldadd!(add); |
| 217 | +foreach_ldclr!(and); |
| 218 | +foreach_ldeor!(xor); |
| 219 | +foreach_ldset!(or); |
| 220 | + |
| 221 | +// TODO: CAS 16 |
0 commit comments