From e73e42be66e7ab8c0a0d9b199782f9c01cffc0ad Mon Sep 17 00:00:00 2001
From: bjorn3 <17426603+bjorn3@users.noreply.github.com>
Date: Tue, 4 Jul 2023 19:13:53 +0200
Subject: [PATCH] Replace intrinsics::abort() call in switch_finish_hook with
 safer abort mechanism

abort will still run the illegal instruction interrupt handler which may
not be safe.
---
 src/arch/aarch64/stop.rs | 12 +++++++-----
 src/arch/x86/stop.rs     |  8 +++++---
 src/arch/x86_64/stop.rs  |  8 +++++---
 src/context/switch.rs    |  2 +-
 src/lib.rs               |  1 -
 5 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/src/arch/aarch64/stop.rs b/src/arch/aarch64/stop.rs
index 90f55227..6f120017 100644
--- a/src/arch/aarch64/stop.rs
+++ b/src/arch/aarch64/stop.rs
@@ -6,9 +6,13 @@ pub unsafe extern fn kreset() -> ! {
 
     let val: u32 = 0x8400_0009;
     asm!("mov   x0, {}", in(reg) val);
-    asm!("hvc   #0");
+    asm!("hvc   #0", options(noreturn));
+}
 
-    unreachable!();
+pub unsafe fn emergency_reset() -> ! {
+    let val: u32 = 0x8400_0009;
+    asm!("mov   x0, {}", in(reg) val);
+    asm!("hvc   #0", options(noreturn));
 }
 
 #[no_mangle]
@@ -17,7 +21,5 @@ pub unsafe extern fn kstop() -> ! {
 
     let val: u32 = 0x8400_0008;
     asm!("mov   x0, {}", in(reg) val);
-    asm!("hvc   #0");
-
-    unreachable!();
+    asm!("hvc   #0", options(noreturn));
 }
diff --git a/src/arch/x86/stop.rs b/src/arch/x86/stop.rs
index 38aaef87..246d1ddd 100644
--- a/src/arch/x86/stop.rs
+++ b/src/arch/x86/stop.rs
@@ -19,14 +19,16 @@ pub unsafe extern fn kreset() -> ! {
         port.write(0xFE);
     }
 
+    emergency_reset();
+}
+
+pub unsafe fn emergency_reset() -> ! {
     // Use triple fault to guarantee reset
     core::arch::asm!("
         cli
         lidt cs:0
         int $3
-    ");
-
-    unreachable!();
+    ", options(noreturn));
 }
 
 #[cfg(feature = "acpi")]
diff --git a/src/arch/x86_64/stop.rs b/src/arch/x86_64/stop.rs
index 38aaef87..246d1ddd 100644
--- a/src/arch/x86_64/stop.rs
+++ b/src/arch/x86_64/stop.rs
@@ -19,14 +19,16 @@ pub unsafe extern fn kreset() -> ! {
         port.write(0xFE);
     }
 
+    emergency_reset();
+}
+
+pub unsafe fn emergency_reset() -> ! {
     // Use triple fault to guarantee reset
     core::arch::asm!("
         cli
         lidt cs:0
         int $3
-    ");
-
-    unreachable!();
+    ", options(noreturn));
 }
 
 #[cfg(feature = "acpi")]
diff --git a/src/context/switch.rs b/src/context/switch.rs
index 5d3a4d18..bca5f7ac 100644
--- a/src/context/switch.rs
+++ b/src/context/switch.rs
@@ -97,7 +97,7 @@ pub unsafe extern "C" fn switch_finish_hook() {
         next_lock.force_write_unlock();
     } else {
         // TODO: unreachable_unchecked()?
-        core::intrinsics::abort();
+        crate::arch::stop::emergency_reset();
     }
     arch::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
 }
diff --git a/src/lib.rs b/src/lib.rs
index ec53c4e4..3be0afc1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -47,7 +47,6 @@
 #![feature(array_chunks)]
 #![feature(iter_array_chunks)]
 #![feature(asm_const)] // TODO: Relax requirements of most asm invocations
-#![feature(core_intrinsics)]
 #![feature(int_roundings)]
 #![feature(naked_functions)]
 #![feature(slice_ptr_get, slice_ptr_len)]
-- 
GitLab