| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Atomic implementation on MSP430. |
| 5 |
|
| 6 |
Adapted from https://github.com/pftbest/msp430-atomic. |
| 7 |
|
| 8 |
Operations not supported here are provided by disabling interrupts. |
| 9 |
See also src/imp/interrupt/msp430.rs. |
| 10 |
|
| 11 |
See "Atomic operation overview by architecture" in atomic-maybe-uninit for a more comprehensive and |
| 12 |
detailed description of the atomic and synchronize instructions in this architecture: |
| 13 |
https://github.com/taiki-e/atomic-maybe-uninit/blob/HEAD/src/arch/README.md#msp430 |
| 14 |
|
| 15 |
Note: Ordering is always SeqCst. |
| 16 |
|
| 17 |
Refs: |
| 18 |
- MSP430x5xx and MSP430x6xx Family User's Guide, Rev. Q |
| 19 |
https://www.ti.com/lit/ug/slau208q/slau208q.pdf |
| 20 |
- atomic-maybe-uninit |
| 21 |
https://github.com/taiki-e/atomic-maybe-uninit |
| 22 |
|
| 23 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 24 |
*/ |
| 25 |
|
| 26 |
#[cfg(not(portable_atomic_no_asm))] |
| 27 |
use core::arch::asm; |
| 28 |
#[cfg(not(feature = "critical-section"))] |
| 29 |
use core::cell::UnsafeCell; |
| 30 |
use core::sync::atomic::Ordering; |
| 31 |
|
| 32 |
/// An atomic fence. |
| 33 |
/// |
| 34 |
/// # Panics |
| 35 |
/// |
| 36 |
/// Panics if `order` is [`Relaxed`](Ordering::Relaxed). |
| 37 |
#[inline] |
| 38 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 39 |
pub fn fence(order: Ordering) { |
| 40 |
match order { |
| 41 |
Ordering::Relaxed => panic!("there is no such thing as a relaxed fence"), |
| 42 |
// MSP430 is single-core and a compiler fence works as an atomic fence. |
| 43 |
_ => compiler_fence(order), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/// A compiler memory fence. |
| 48 |
/// |
| 49 |
/// # Panics |
| 50 |
/// |
| 51 |
/// Panics if `order` is [`Relaxed`](Ordering::Relaxed). |
| 52 |
#[inline] |
| 53 |
#[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] |
| 54 |
pub fn compiler_fence(order: Ordering) { |
| 55 |
match order { |
| 56 |
Ordering::Relaxed => panic!("there is no such thing as a relaxed compiler fence"), |
| 57 |
_ => {} |
| 58 |
} |
| 59 |
// SAFETY: using an empty asm is safe. |
| 60 |
unsafe { |
| 61 |
// Do not use `nomem` and `readonly` because prevent preceding and subsequent memory accesses from being reordered. |
| 62 |
#[cfg(not(portable_atomic_no_asm))] |
| 63 |
asm!("", options(nostack, preserves_flags)); |
| 64 |
#[cfg(portable_atomic_no_asm)] |
| 65 |
llvm_asm!("" ::: "memory" : "volatile"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[cfg(not(feature = "critical-section"))] |
| 70 |
items!({ |
| 71 |
macro_rules! atomic { |
| 72 |
(load_store, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $size:tt) => { |
| 73 |
#[repr(transparent)] |
| 74 |
pub(crate) struct $atomic_type $(<$($generics)*>)? { |
| 75 |
v: UnsafeCell<$value_type>, |
| 76 |
} |
| 77 |
|
| 78 |
// Send is implicitly implemented for atomic integers, but not for atomic pointers. |
| 79 |
// SAFETY: any data races are prevented by atomic operations. |
| 80 |
unsafe impl $(<$($generics)*>)? Send for $atomic_type $(<$($generics)*>)? {} |
| 81 |
// SAFETY: any data races are prevented by atomic operations. |
| 82 |
unsafe impl $(<$($generics)*>)? Sync for $atomic_type $(<$($generics)*>)? {} |
| 83 |
|
| 84 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 85 |
#[inline] |
| 86 |
#[cfg_attr( |
| 87 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 88 |
track_caller |
| 89 |
)] |
| 90 |
pub(crate) fn load(&self, order: Ordering) -> $value_type { |
| 91 |
crate::utils::assert_load_ordering(order); |
| 92 |
let src = self.v.get(); |
| 93 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 94 |
// pointer passed in is valid because we got it from a reference. |
| 95 |
unsafe { |
| 96 |
let out; |
| 97 |
#[cfg(not(portable_atomic_no_asm))] |
| 98 |
asm!( |
| 99 |
concat!("mov.", $size, " @{src}, {out}"), // atomic { out = *src } |
| 100 |
src = in(reg) src, |
| 101 |
out = lateout(reg) out, |
| 102 |
options(nostack, preserves_flags), |
| 103 |
); |
| 104 |
#[cfg(portable_atomic_no_asm)] |
| 105 |
llvm_asm!( |
| 106 |
concat!("mov.", $size, " $1, $0") |
| 107 |
: "=r"(out) : "*m"(src) : "memory" : "volatile" |
| 108 |
); |
| 109 |
out |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[inline] |
| 114 |
#[cfg_attr( |
| 115 |
all(debug_assertions, not(portable_atomic_no_track_caller)), |
| 116 |
track_caller |
| 117 |
)] |
| 118 |
pub(crate) fn store(&self, val: $value_type, order: Ordering) { |
| 119 |
crate::utils::assert_store_ordering(order); |
| 120 |
let dst = self.v.get(); |
| 121 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 122 |
// pointer passed in is valid because we got it from a reference. |
| 123 |
unsafe { |
| 124 |
#[cfg(not(portable_atomic_no_asm))] |
| 125 |
asm!( |
| 126 |
concat!("mov.", $size, " {val}, 0({dst})"), // atomic { *dst = val } |
| 127 |
dst = in(reg) dst, |
| 128 |
val = in(reg) val, |
| 129 |
options(nostack, preserves_flags), |
| 130 |
); |
| 131 |
#[cfg(portable_atomic_no_asm)] |
| 132 |
llvm_asm!( |
| 133 |
concat!("mov.", $size, " $1, $0") |
| 134 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
}; |
| 140 |
($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $size:tt) => { |
| 141 |
atomic!(load_store, $([$($generics)*])? $atomic_type, $value_type, $size); |
| 142 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 143 |
#[inline] |
| 144 |
pub(crate) fn add(&self, val: $value_type, _order: Ordering) { |
| 145 |
let dst = self.v.get(); |
| 146 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 147 |
// pointer passed in is valid because we got it from a reference. |
| 148 |
unsafe { |
| 149 |
#[cfg(not(portable_atomic_no_asm))] |
| 150 |
asm!( |
| 151 |
concat!("add.", $size, " {val}, 0({dst})"), // atomic { *dst += val } |
| 152 |
dst = in(reg) dst, |
| 153 |
val = in(reg) val, |
| 154 |
// Do not use `preserves_flags` because ADD modifies the V, N, Z, and C bits of the status register. |
| 155 |
options(nostack), |
| 156 |
); |
| 157 |
#[cfg(portable_atomic_no_asm)] |
| 158 |
llvm_asm!( |
| 159 |
concat!("add.", $size, " $1, $0") |
| 160 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[inline] |
| 166 |
pub(crate) fn sub(&self, val: $value_type, _order: Ordering) { |
| 167 |
let dst = self.v.get(); |
| 168 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 169 |
// pointer passed in is valid because we got it from a reference. |
| 170 |
unsafe { |
| 171 |
#[cfg(not(portable_atomic_no_asm))] |
| 172 |
asm!( |
| 173 |
concat!("sub.", $size, " {val}, 0({dst})"), // atomic { *dst -= val } |
| 174 |
dst = in(reg) dst, |
| 175 |
val = in(reg) val, |
| 176 |
// Do not use `preserves_flags` because SUB modifies the V, N, Z, and C bits of the status register. |
| 177 |
options(nostack), |
| 178 |
); |
| 179 |
#[cfg(portable_atomic_no_asm)] |
| 180 |
llvm_asm!( |
| 181 |
concat!("sub.", $size, " $1, $0") |
| 182 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
#[inline] |
| 188 |
pub(crate) fn and(&self, val: $value_type, _order: Ordering) { |
| 189 |
let dst = self.v.get(); |
| 190 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 191 |
// pointer passed in is valid because we got it from a reference. |
| 192 |
unsafe { |
| 193 |
#[cfg(not(portable_atomic_no_asm))] |
| 194 |
asm!( |
| 195 |
concat!("and.", $size, " {val}, 0({dst})"), // atomic { *dst &= val } |
| 196 |
dst = in(reg) dst, |
| 197 |
val = in(reg) val, |
| 198 |
// Do not use `preserves_flags` because AND modifies the V, N, Z, and C bits of the status register. |
| 199 |
options(nostack), |
| 200 |
); |
| 201 |
#[cfg(portable_atomic_no_asm)] |
| 202 |
llvm_asm!( |
| 203 |
concat!("and.", $size, " $1, $0") |
| 204 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 205 |
); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
#[inline] |
| 210 |
pub(crate) fn or(&self, val: $value_type, _order: Ordering) { |
| 211 |
let dst = self.v.get(); |
| 212 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 213 |
// pointer passed in is valid because we got it from a reference. |
| 214 |
unsafe { |
| 215 |
#[cfg(not(portable_atomic_no_asm))] |
| 216 |
asm!( |
| 217 |
concat!("bis.", $size, " {val}, 0({dst})"), // atomic { *dst |= val } |
| 218 |
dst = in(reg) dst, |
| 219 |
val = in(reg) val, |
| 220 |
options(nostack, preserves_flags), |
| 221 |
); |
| 222 |
#[cfg(portable_atomic_no_asm)] |
| 223 |
llvm_asm!( |
| 224 |
concat!("bis.", $size, " $1, $0") |
| 225 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[inline] |
| 231 |
pub(crate) fn xor(&self, val: $value_type, _order: Ordering) { |
| 232 |
let dst = self.v.get(); |
| 233 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 234 |
// pointer passed in is valid because we got it from a reference. |
| 235 |
unsafe { |
| 236 |
#[cfg(not(portable_atomic_no_asm))] |
| 237 |
asm!( |
| 238 |
concat!("xor.", $size, " {val}, 0({dst})"), // atomic { *dst ^= val } |
| 239 |
dst = in(reg) dst, |
| 240 |
val = in(reg) val, |
| 241 |
// Do not use `preserves_flags` because XOR modifies the V, N, Z, and C bits of the status register. |
| 242 |
options(nostack), |
| 243 |
); |
| 244 |
#[cfg(portable_atomic_no_asm)] |
| 245 |
llvm_asm!( |
| 246 |
concat!("xor.", $size, " $1, $0") |
| 247 |
:: "*m"(dst), "ir"(val) : "memory" : "volatile" |
| 248 |
); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
#[inline] |
| 253 |
pub(crate) fn not(&self, _order: Ordering) { |
| 254 |
let dst = self.v.get(); |
| 255 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 256 |
// pointer passed in is valid because we got it from a reference. |
| 257 |
unsafe { |
| 258 |
#[cfg(not(portable_atomic_no_asm))] |
| 259 |
asm!( |
| 260 |
concat!("inv.", $size, " 0({dst})"), // atomic { *dst = !*dst } |
| 261 |
dst = in(reg) dst, |
| 262 |
// Do not use `preserves_flags` because INV modifies the V, N, Z, and C bits of the status register. |
| 263 |
options(nostack), |
| 264 |
); |
| 265 |
#[cfg(portable_atomic_no_asm)] |
| 266 |
llvm_asm!( |
| 267 |
concat!("inv.", $size, " $0") |
| 268 |
:: "*m"(dst) : "memory" : "volatile" |
| 269 |
); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
}; |
| 274 |
} |
| 275 |
|
| 276 |
atomic!(AtomicI8, i8, "b"); |
| 277 |
atomic!(AtomicU8, u8, "b"); |
| 278 |
atomic!(AtomicI16, i16, "w"); |
| 279 |
atomic!(AtomicU16, u16, "w"); |
| 280 |
atomic!(AtomicIsize, isize, "w"); |
| 281 |
atomic!(AtomicUsize, usize, "w"); |
| 282 |
atomic!(load_store, [T] AtomicPtr, *mut T, "w"); |
| 283 |
}); |