Skip to content

Commit

Permalink
fix(interrupts): add compiler fences to enable and disable
Browse files Browse the repository at this point in the history
Without this, the compiler may reorder memory accesses over the sti and cli calls.

Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Sep 15, 2023
1 parent 950a941 commit 659c871
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/instructions/interrupts.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down

0 comments on commit 659c871

Please sign in to comment.