summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-12-22 16:56:37 -0500
committerIan Moffett <ian@osmora.org>2025-12-22 16:56:37 -0500
commitf132a621cd26a1c083dc9054870bc2a00e653898 (patch)
tree7861b3432dc9e023f3bd64e74187e008dc12c3ac
parented6d9a6f67b44e5ff3f5546bd06c22a2b603e01e (diff)
mos/x86_64: cpu: Add atomic swap primitive + MU if
The atomic swap primitive can be used to implement spinlocks among other things. This must be implemented per architecture as it is exposed via the machine unifier. Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--mos/sys/arch/x86_64/cpu/prim.S5
-rw-r--r--mos/sys/inc/mu/cpu.h11
2 files changed, 16 insertions, 0 deletions
diff --git a/mos/sys/arch/x86_64/cpu/prim.S b/mos/sys/arch/x86_64/cpu/prim.S
index 6f82153..707eb86 100644
--- a/mos/sys/arch/x86_64/cpu/prim.S
+++ b/mos/sys/arch/x86_64/cpu/prim.S
@@ -7,6 +7,7 @@
.globl mu_cpu_halt
.globl mu_irq_state
.globl mu_irq_setmask
+ .globl mu_cpu_aswap
mu_cpu_halt:
hlt
retq
@@ -19,6 +20,10 @@ mu_irq_state:
shr $9, %rax /* IF */
and $1, %rax /* Isolate */
retq
+mu_cpu_aswap:
+ xchg %rsi, (%rdi) /* Swap */
+ mov %rsi, %rax /* Return old value */
+ retq
mu_irq_setmask:
test $1, %rdi
jnz irq_mask
diff --git a/mos/sys/inc/mu/cpu.h b/mos/sys/inc/mu/cpu.h
index e91a084..9856df8 100644
--- a/mos/sys/inc/mu/cpu.h
+++ b/mos/sys/inc/mu/cpu.h
@@ -23,6 +23,17 @@ BOOL mu_irq_state(void);
void mu_irq_setmask(BOOL mask);
/*
+ * Perform an atomic swap operation
+ *
+ * @p: Memory area to store `val' to
+ * @val: Value to write to `p'
+ *
+ * Returns the value at `p' before `val' was
+ * written
+ */
+USIZE mu_cpu_aswap(USIZE *p, USIZE val);
+
+/*
* Halt the current processor core
*/
void mu_cpu_halt(void);