From 659c8712c55389ed05d58dc3c5894f7262a41aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Fri, 15 Sep 2023 16:07:11 +0200 Subject: [PATCH] fix(interrupts): add compiler fences to enable and disable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without this, the compiler may reorder memory accesses over the sti and cli calls. Signed-off-by: Martin Kröning --- src/instructions/interrupts.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/instructions/interrupts.rs b/src/instructions/interrupts.rs index 0495e0dd..dc4937f9 100644 --- a/src/instructions/interrupts.rs +++ b/src/instructions/interrupts.rs @@ -1,6 +1,7 @@ //! Enabling and disabling interrupts use core::arch::asm; +use core::sync::atomic::{compiler_fence, Ordering}; /// Returns whether interrupts are enabled. #[inline] @@ -15,6 +16,8 @@ pub fn are_enabled() -> bool { /// This is a wrapper around the `sti` instruction. #[inline] pub fn enable() { + // Prevent earlier writes to be moved beyond this point + compiler_fence(Ordering::Release); unsafe { asm!("sti", options(nomem, nostack)); } @@ -25,6 +28,8 @@ pub fn enable() { /// This is a wrapper around the `cli` instruction. #[inline] pub fn disable() { + // Prevent future writes to be moved before this point. + compiler_fence(Ordering::Acquire); unsafe { asm!("cli", options(nomem, nostack)); }