From 582e3fd8eb6c77b6513bafa195ffa9aadeb2ff71 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Tue, 21 Apr 2020 21:03:17 -0600
Subject: [PATCH] Unlock CONTEXT_SWITCH_LOCK after loading registers but before
 switch

---
 src/context/arch/x86_64.rs | 6 +++++-
 src/context/switch.rs      | 3 ---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/context/arch/x86_64.rs b/src/context/arch/x86_64.rs
index fa108a0a..2517d268 100644
--- a/src/context/arch/x86_64.rs
+++ b/src/context/arch/x86_64.rs
@@ -1,5 +1,5 @@
 use core::mem;
-use core::sync::atomic::AtomicBool;
+use core::sync::atomic::{AtomicBool, Ordering};
 use syscall::data::FloatRegisters;
 
 /// This must be used by the kernel to ensure that context switches are done atomically
@@ -127,6 +127,7 @@ impl Context {
     }
 
     /// Switch to the next context by restoring its stack and registers
+    /// Check disassembly!
     #[cold]
     #[inline(never)]
     #[naked]
@@ -167,6 +168,9 @@ impl Context {
 
         asm!("mov $0, rbp" : "=r"(self.rbp) : : "memory" : "intel", "volatile");
         asm!("mov rbp, $0" : : "r"(next.rbp) : "memory" : "intel", "volatile");
+
+        // Unset global lock after loading registers but before switch
+        CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
     }
 }
 
diff --git a/src/context/switch.rs b/src/context/switch.rs
index 34fdd23a..6e10825f 100644
--- a/src/context/switch.rs
+++ b/src/context/switch.rs
@@ -156,9 +156,6 @@ pub unsafe fn switch() -> bool {
 
         (*from_ptr).arch.switch_to(&mut (*to_ptr).arch);
 
-        // Unset global lock after switch
-        arch::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
-
         true
     }
 }
-- 
GitLab