| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Arm A-Profile Architectures, Arm R-Profile Architectures, Legacy Arm Architectures |
| 5 |
|
| 6 |
Refs: https://developer.arm.com/documentation/ddi0406/cb/System-Level-Architecture/The-System-Level-Programmers--Model/ARM-processor-modes-and-ARM-core-registers/Program-Status-Registers--PSRs- |
| 7 |
|
| 8 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 9 |
*/ |
| 10 |
|
| 11 |
#[cfg(not(portable_atomic_no_asm))] |
| 12 |
use core::arch::asm; |
| 13 |
|
| 14 |
// - 0x80 - I (IRQ mask) bit (1 << 7) |
| 15 |
// - 0x40 - F (FIQ mask) bit (1 << 6) |
| 16 |
// We disable only IRQs by default. See also https://github.com/taiki-e/portable-atomic/pull/28#issuecomment-1214146912. |
| 17 |
#[cfg(not(portable_atomic_disable_fiq))] |
| 18 |
macro_rules! mask { |
| 19 |
() => { |
| 20 |
"0x80" |
| 21 |
}; |
| 22 |
} |
| 23 |
#[cfg(portable_atomic_disable_fiq)] |
| 24 |
macro_rules! mask { |
| 25 |
() => { |
| 26 |
"0xC0" // 0x80 | 0x40 |
| 27 |
}; |
| 28 |
} |
| 29 |
|
| 30 |
pub(crate) type State = u32; |
| 31 |
|
| 32 |
/// Disables interrupts and returns the previous interrupt state. |
| 33 |
#[inline] |
| 34 |
#[cfg_attr( |
| 35 |
not(any(target_feature = "v7", portable_atomic_target_feature = "v7")), |
| 36 |
instruction_set(arm::a32) |
| 37 |
)] |
| 38 |
pub(crate) fn disable() -> State { |
| 39 |
let cpsr: State; |
| 40 |
// SAFETY: reading CPSR and disabling interrupts are safe. |
| 41 |
// (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions) |
| 42 |
unsafe { |
| 43 |
asm!( |
| 44 |
"mrs {prev}, cpsr", // prev = CPSR |
| 45 |
concat!("orr {new}, {prev}, ", mask!()), // new = prev | mask |
| 46 |
"msr cpsr_c, {new}", // CPSR.{I,F,T,M} = new.{I,F,T,M} |
| 47 |
prev = out(reg) cpsr, |
| 48 |
new = out(reg) _, |
| 49 |
// Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled. |
| 50 |
options(nostack, preserves_flags), |
| 51 |
); |
| 52 |
} |
| 53 |
cpsr |
| 54 |
} |
| 55 |
|
| 56 |
/// Restores the previous interrupt state. |
| 57 |
/// |
| 58 |
/// # Safety |
| 59 |
/// |
| 60 |
/// The state must be the one retrieved by the previous `disable`. |
| 61 |
#[inline] |
| 62 |
#[cfg_attr( |
| 63 |
not(any(target_feature = "v7", portable_atomic_target_feature = "v7")), |
| 64 |
instruction_set(arm::a32) |
| 65 |
)] |
| 66 |
pub(crate) unsafe fn restore(prev_cpsr: State) { |
| 67 |
// SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`, |
| 68 |
// |
| 69 |
// This clobbers the control field mask byte of CPSR. See msp430.rs for safety on this. |
| 70 |
// (preserves_flags is fine because we can clobber only the I, F, T, and M bits of CPSR.) |
| 71 |
// |
| 72 |
// Refs: https://developer.arm.com/documentation/dui0473/m/arm-and-thumb-instructions/msr--general-purpose-register-to-psr- |
| 73 |
unsafe { |
| 74 |
asm!( |
| 75 |
"msr cpsr_c, {prev_cpsr}", // CPSR.{I,F,T,M} = prev_cpsr.{I,F,T,M} |
| 76 |
prev_cpsr = in(reg) prev_cpsr, |
| 77 |
// Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled. |
| 78 |
options(nostack, preserves_flags), |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// On pre-v6 Arm, we cannot use core::sync::atomic here because they call the |
| 84 |
// `__sync_*` builtins for non-relaxed load/store (because pre-v6 Arm doesn't |
| 85 |
// have Data Memory Barrier). |
| 86 |
#[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(any(test, portable_atomic_no_atomic_cas)))] |
| 87 |
#[cfg_attr( |
| 88 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 89 |
cfg(any(test, not(target_has_atomic = "ptr"))) |
| 90 |
)] |
| 91 |
pub(super) mod atomic { |
| 92 |
#[cfg(not(portable_atomic_no_asm))] |
| 93 |
use core::arch::asm; |
| 94 |
use core::{cell::UnsafeCell, sync::atomic::Ordering}; |
| 95 |
|
| 96 |
macro_rules! atomic { |
| 97 |
($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $suffix:tt) => { |
| 98 |
#[repr(transparent)] |
| 99 |
pub(crate) struct $atomic_type $(<$($generics)*>)? { |
| 100 |
v: UnsafeCell<$value_type>, |
| 101 |
} |
| 102 |
|
| 103 |
// Send is implicitly implemented for atomic integers, but not for atomic pointers. |
| 104 |
// SAFETY: any data races are prevented by atomic operations. |
| 105 |
unsafe impl $(<$($generics)*>)? Send for $atomic_type $(<$($generics)*>)? {} |
| 106 |
// SAFETY: any data races are prevented by atomic operations. |
| 107 |
unsafe impl $(<$($generics)*>)? Sync for $atomic_type $(<$($generics)*>)? {} |
| 108 |
|
| 109 |
impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { |
| 110 |
#[inline] |
| 111 |
pub(crate) fn load(&self, _order: Ordering) -> $value_type { |
| 112 |
let src = self.v.get(); |
| 113 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 114 |
// pointer passed in is valid because we got it from a reference. |
| 115 |
unsafe { |
| 116 |
let out; |
| 117 |
// inline asm without nomem/readonly implies compiler fence. |
| 118 |
// And compiler fence is fine because the user explicitly declares that |
| 119 |
// the system is single-core by using an unsafe cfg. |
| 120 |
asm!( |
| 121 |
concat!("ldr", $suffix, " {out}, [{src}]"), // atomic { out = *src } |
| 122 |
src = in(reg) src, |
| 123 |
out = lateout(reg) out, |
| 124 |
options(nostack, preserves_flags), |
| 125 |
); |
| 126 |
out |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[inline] |
| 131 |
pub(crate) fn store(&self, val: $value_type, _order: Ordering) { |
| 132 |
let dst = self.v.get(); |
| 133 |
// SAFETY: any data races are prevented by atomic intrinsics and the raw |
| 134 |
// pointer passed in is valid because we got it from a reference. |
| 135 |
unsafe { |
| 136 |
// inline asm without nomem/readonly implies compiler fence. |
| 137 |
// And compiler fence is fine because the user explicitly declares that |
| 138 |
// the system is single-core by using an unsafe cfg. |
| 139 |
asm!( |
| 140 |
concat!("str", $suffix, " {val}, [{dst}]"), // atomic { *dst = val } |
| 141 |
dst = in(reg) dst, |
| 142 |
val = in(reg) val, |
| 143 |
options(nostack, preserves_flags), |
| 144 |
); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
}; |
| 149 |
} |
| 150 |
|
| 151 |
atomic!(AtomicI8, i8, "b"); |
| 152 |
atomic!(AtomicU8, u8, "b"); |
| 153 |
atomic!(AtomicI16, i16, "h"); |
| 154 |
atomic!(AtomicU16, u16, "h"); |
| 155 |
atomic!(AtomicI32, i32, ""); |
| 156 |
atomic!(AtomicU32, u32, ""); |
| 157 |
atomic!(AtomicIsize, isize, ""); |
| 158 |
atomic!(AtomicUsize, usize, ""); |
| 159 |
atomic!([T] AtomicPtr, *mut T, ""); |
| 160 |
} |