diff --git a/service/ptos/head/arch/amd64/prim.h b/service/ptos/head/arch/amd64/prim.h new file mode 100644 index 0000000..73743ff --- /dev/null +++ b/service/ptos/head/arch/amd64/prim.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Processor primitives + * Author: Chloe M. + */ + +#ifndef _MACHINE_PRIM_H_ +#define _MACHINE_PRIM_H_ 1 + +#include + +#define MdCpuSuspend() ASMV("hlt") +#define MdCpuSpinWait() ASMV("pause") + +/* + * Atomic swap operation + * + * @Ptr: Location to swap with @Value + * @Value: Value to swap to @Ptr + */ +ALWAYS_INLINE static inline UQUAD +MdCpuAswap(UUQUAD *Ptr, UQUAD Value) +{ + UQUAD RetVal; + + if (Ptr == NULL) { + return 0; + } + + RetVal = *(volatile UQUAD *)Ptr; + ASMV( + "xchg %0, %1\n" + : "+m" (*Ptr), "+r" (Value) + : + : "memory" + ); + + return RetVal; +} + +#endif /* !_MACHINE_PRIM_H_ */ diff --git a/service/ptos/head/hal/prim.h b/service/ptos/head/hal/prim.h new file mode 100644 index 0000000..c217571 --- /dev/null +++ b/service/ptos/head/hal/prim.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause. + * + * Description: Processor primitives + * Author: Chloe M. + */ + +#ifndef _HAL_PRIM_H_ +#define _HAL_PRIM_H_ 1 + +#include +#include + +/* + * Suspend the current processor until the next + * interrupt. + */ +ALWAYS_INLINE static inline VOID +HalCpuSuspend(VOID) +{ + MdCpuSuspend(); +} + +/* + * Hint to the processor core that it is in a spin + * loop. + */ +ALWAYS_INLINE static inline VOID +HalCpuSpinWait(VOID) +{ + MdCpuSpinWait(); +} + +/* + * Perform an atomic swap operation + * + * @Ptr: Location to swap with @Value + * @Value: Value to swap with data at @Ptr + */ +ALWAYS_INLINE static inline VOID +HalCpuAswap(UQUAD *Ptr, UQUAD Value) +{ + MdCpuAswap(Ptr, Value); +} + +#endif /* !_HAL_PRIM_H_ */