From 28fafce975f9eaa43a06461bb60b7df63879b0d6 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Fri, 24 May 2024 22:58:43 +0200 Subject: [PATCH] introduce public interface to shutdown the system --- src/arch/aarch64/kernel/scheduler.rs | 10 +++++++++- src/arch/x86_64/kernel/processor.rs | 2 +- src/arch/x86_64/kernel/scheduler.rs | 3 ++- src/lib.rs | 7 +++---- src/scheduler/mod.rs | 4 ++++ src/shell.rs | 2 +- src/syscalls/semaphore.rs | 3 ++- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/arch/aarch64/kernel/scheduler.rs b/src/arch/aarch64/kernel/scheduler.rs index bb24e7c578..ba1d7db638 100644 --- a/src/arch/aarch64/kernel/scheduler.rs +++ b/src/arch/aarch64/kernel/scheduler.rs @@ -13,6 +13,8 @@ use crate::arch::aarch64::kernel::CURRENT_STACK_ADDRESS; use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize, PageTableEntryFlags}; use crate::arch::aarch64::mm::{PhysAddr, VirtAddr}; use crate::scheduler::task::{Task, TaskFrame}; +#[cfg(target_os = "none")] +use crate::scheduler::PerCoreSchedulerExt; use crate::{kernel, DEFAULT_STACK_SIZE, KERNEL_STACK_SIZE}; #[derive(Debug)] @@ -314,6 +316,12 @@ extern "C" fn task_start(_f: extern "C" fn(usize), _arg: usize, _user_stack: u64 unimplemented!() } +#[cfg(target_os = "none")] +extern "C" fn thread_exit(status: i32) -> ! { + debug!("Exit thread with error code {}!", status); + core_scheduler().exit(status) +} + #[cfg(target_os = "none")] #[naked] extern "C" fn task_start(_f: extern "C" fn(usize), _arg: usize) -> ! { @@ -331,7 +339,7 @@ extern "C" fn task_start(_f: extern "C" fn(usize), _arg: usize) -> ! { "add x4, x4, #:lo12:{exit}", "br x4", l0 = const 0, - exit = sym crate::sys_thread_exit, + exit = sym thread_exit, options(noreturn) ) } diff --git a/src/arch/x86_64/kernel/processor.rs b/src/arch/x86_64/kernel/processor.rs index 1c6afcbcf8..4271e5849c 100644 --- a/src/arch/x86_64/kernel/processor.rs +++ b/src/arch/x86_64/kernel/processor.rs @@ -844,7 +844,7 @@ pub fn configure() { #[cfg(feature = "fsgsbase")] if !supports_fsgs() { error!("FSGSBASE support is enabled, but the processor doesn't support it!"); - crate::shutdown(1); + crate::scheduler::shutdown(1); } debug!("Set CR4 to {:#x}", cr4); diff --git a/src/arch/x86_64/kernel/scheduler.rs b/src/arch/x86_64/kernel/scheduler.rs index c81ae9a57f..982034cc14 100644 --- a/src/arch/x86_64/kernel/scheduler.rs +++ b/src/arch/x86_64/kernel/scheduler.rs @@ -332,7 +332,8 @@ extern "C" fn task_entry(func: extern "C" fn(usize), arg: usize) -> ! { func(arg); // Exit task - crate::sys_thread_exit(0) + debug!("Exit thread with error code 0!"); + core_scheduler().exit(0) } impl TaskFrame for Task { diff --git a/src/lib.rs b/src/lib.rs index 650f5448ac..b939c26384 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,7 +67,6 @@ pub(crate) use crate::config::*; pub use crate::fs::create_file; use crate::kernel::is_uhyve_with_pci; use crate::scheduler::{PerCoreScheduler, PerCoreSchedulerExt}; -pub use crate::syscalls::*; #[macro_use] mod macros; @@ -103,7 +102,7 @@ hermit_entry::define_entry_version!(); extern "C" fn runtime_entry(_argc: i32, _argv: *const *const u8, _env: *const *const u8) -> ! { println!("Executing hermit unittests. Any arguments are dropped"); test_main(); - sys_exit(0); + core_scheduler().exit(0) } //https://github.com/rust-lang/rust/issues/50297#issuecomment-524180479 @@ -113,7 +112,7 @@ pub fn test_runner(tests: &[&dyn Fn()]) { for test in tests { test(); } - sys_exit(0); + core_scheduler().exit(0) } #[cfg(target_os = "none")] @@ -275,5 +274,5 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! { let core_id = crate::arch::core_local::core_id(); println!("[{core_id}][PANIC] {info}"); - crate::shutdown(1); + crate::scheduler::shutdown(1); } diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index 8404852746..9c7ca0f29e 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -986,6 +986,10 @@ pub fn join(id: TaskId) -> Result<(), ()> { } } +pub fn shutdown(arg: i32) -> ! { + crate::syscalls::shutdown(arg) +} + fn get_task_handle(id: TaskId) -> Option { TASKS.lock().get(&id).copied() } diff --git a/src/shell.rs b/src/shell.rs index 52cab9ac17..77dd76659a 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -39,7 +39,7 @@ static mut SHELL: Lazy> = Lazy::new(|| { ShellCommand { help: "Shutdown HermitOS", func: |_, shell| { - crate::shutdown(0); + crate::scheduler::shutdown(0); Ok(()) }, aliases: &["s"], diff --git a/src/syscalls/semaphore.rs b/src/syscalls/semaphore.rs index 98fc0336bc..c11db47667 100644 --- a/src/syscalls/semaphore.rs +++ b/src/syscalls/semaphore.rs @@ -2,6 +2,7 @@ use alloc::boxed::Box; use crate::errno::*; use crate::synch::semaphore::Semaphore; +use crate::syscalls::{sys_clock_gettime, CLOCK_REALTIME}; use crate::time::timespec; #[allow(non_camel_case_types)] @@ -120,7 +121,7 @@ pub unsafe extern "C" fn sys_sem_timedwait(sem: *mut sem_t, ts: *const timespec) let mut current_ts: timespec = Default::default(); unsafe { - crate::sys_clock_gettime(crate::CLOCK_REALTIME, &mut current_ts as *mut _); + sys_clock_gettime(CLOCK_REALTIME, &mut current_ts as *mut _); let ts = &*ts; let ms: i64 = (ts.tv_sec - current_ts.tv_sec) * 1000