Ver código fonte

kernel: put kernel shutdown into `power` module

- Add abstraction for kernel shutting down.
- Use this in the kernel panic routine.

Signed-off-by: greatbridf <greatbridf@icloud.com>
greatbridf 1 semana atrás
pai
commit
7b45c831b5
4 arquivos alterados com 54 adições e 38 exclusões
  1. 2 0
      src/kernel.rs
  2. 46 0
      src/kernel/power.rs
  3. 3 23
      src/lib.rs
  4. 3 15
      src/panic.rs

+ 2 - 0
src/kernel.rs

@@ -12,8 +12,10 @@ pub mod user;
 pub mod vfs;
 
 mod chardev;
+mod power;
 mod terminal;
 
 #[allow(unused_imports)]
 pub use chardev::{CharDevice, CharDeviceType, VirtualCharDevice};
+pub use power::shutdown_system;
 pub use terminal::{Terminal, TerminalDevice};

+ 46 - 0
src/kernel/power.rs

@@ -0,0 +1,46 @@
+use core::sync::atomic::{AtomicUsize, Ordering};
+
+use eonix_hal::processor::{halt, CPU, CPU_COUNT};
+use eonix_log::println_info;
+
+static CPU_SHUTTING_DOWN: AtomicUsize = AtomicUsize::new(0);
+
+fn shutdown() {
+    #[cfg(arch_has_shutdown)]
+    {
+        eonix_hal::arch_exported::bootstrap::shutdown();
+    }
+    #[cfg(not(arch_has_shutdown))]
+    {
+        println_info!("Shutdown not supported on this architecture...");
+    }
+}
+
+pub fn shutdown_system_nowait() {
+    shutdown();
+
+    loop {
+        halt();
+    }
+}
+
+pub fn shutdown_system() -> ! {
+    // TODO: We don't have IPI system for now.
+    shutdown_system_nowait();
+
+    let cpu_count = CPU_COUNT.load(Ordering::Relaxed);
+
+    if CPU_SHUTTING_DOWN.fetch_add(1, Ordering::AcqRel) + 1 == cpu_count {
+        println_info!("All CPUs are shutting down. Gracefully powering off...");
+        shutdown();
+    } else {
+        println_info!(
+            "CPU {} is shutting down. Waiting for other CPUs...",
+            CPU::local().cpuid()
+        );
+    }
+
+    loop {
+        halt();
+    }
+}

+ 3 - 23
src/lib.rs

@@ -28,11 +28,10 @@ mod sync;
 
 use alloc::ffi::CString;
 use core::hint::spin_loop;
-use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
+use core::sync::atomic::{AtomicBool, Ordering};
 
-use eonix_hal::arch_exported::bootstrap::shutdown;
 use eonix_hal::context::TaskContext;
-use eonix_hal::processor::{halt, CPU, CPU_COUNT};
+use eonix_hal::processor::CPU;
 use eonix_hal::symbol_addr;
 use eonix_hal::traits::context::RawTaskContext;
 use eonix_hal::traits::trap::IrqState;
@@ -48,31 +47,12 @@ use kernel::vfs::mount::{
 };
 use kernel::vfs::types::Permission;
 use kernel::vfs::FsContext;
-use kernel::CharDevice;
+use kernel::{shutdown_system, CharDevice};
 use kernel_init::setup_memory;
 use path::Path;
 use prelude::*;
 
 static BSP_OK: AtomicBool = AtomicBool::new(false);
-static CPU_SHUTTING_DOWN: AtomicUsize = AtomicUsize::new(0);
-
-fn shutdown_system() -> ! {
-    let cpu_count = CPU_COUNT.load(Ordering::Relaxed);
-
-    if CPU_SHUTTING_DOWN.fetch_add(1, Ordering::AcqRel) + 1 == cpu_count {
-        println_info!("All CPUs are shutting down. Gracefully powering off...");
-        shutdown();
-    } else {
-        println_info!(
-            "CPU {} is shutting down. Waiting for other CPUs...",
-            CPU::local().cpuid()
-        );
-
-        loop {
-            halt();
-        }
-    }
-}
 
 #[eonix_hal::main]
 fn kernel_init(mut data: eonix_hal::bootstrap::BootStrapData) -> ! {

+ 3 - 15
src/panic.rs

@@ -2,6 +2,8 @@ use core::panic::PanicInfo;
 
 use eonix_log::println_fatal;
 
+use crate::kernel::shutdown_system;
+
 #[panic_handler]
 fn panic(info: &PanicInfo) -> ! {
     if let Some(location) = info.location() {
@@ -20,21 +22,7 @@ fn panic(info: &PanicInfo) -> ! {
     #[cfg(arch_has_stacktrace)]
     stacktrace::print_stacktrace();
 
-    panic_forever();
-}
-
-fn panic_forever() -> ! {
-    #[cfg(arch_has_shutdown)]
-    {
-        eonix_hal::arch_exported::bootstrap::shutdown();
-    }
-    #[cfg(not(arch_has_shutdown))]
-    {
-        // Spin forever
-        loop {
-            core::hint::spin_loop();
-        }
-    }
+    shutdown_system();
 }
 
 #[cfg(arch_has_stacktrace)]