Skip to content

Commit

Permalink
introduce public interface to shutdown the system
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed May 25, 2024
1 parent 4f7cdc9 commit 9653732
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/arch/aarch64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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) -> ! {
Expand All @@ -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)
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86_64/kernel/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/arch/x86_64/kernel/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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")]
Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TaskHandle> {
TASKS.lock().get(&id).copied()
}
Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static mut SHELL: Lazy<Shell<'_>> = Lazy::new(|| {
ShellCommand {
help: "Shutdown HermitOS",
func: |_, shell| {
crate::shutdown(0);
crate::scheduler::shutdown(0);
Ok(())
},
aliases: &["s"],
Expand Down
3 changes: 2 additions & 1 deletion src/syscalls/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ pub fn exit(failure: bool) -> ! {
if hermit::_is_uhyve() {
match failure {
//ToDo: Add uhyve exit code enum
true => hermit::sys_exit(1),
false => hermit::sys_exit(0),
true => hermit::syscalls::sys_exit(1),
false => hermit::syscalls::sys_exit(0),
}
} else {
match failure {
Expand All @@ -223,7 +223,7 @@ pub fn exit_qemu(exit_code: QemuExitCode) -> ! {
outl(0xf4, exit_code as u32);
}
println!("Warning - Failed to debug exit qemu - exiting via sys_exit()");
hermit::sys_exit(0) //sys_exit exitcode on qemu gets silently dropped
hermit::syscalls::sys_exit(0) //sys_exit exitcode on qemu gets silently dropped
}

// ToDo: Maybe we could add a hard limit on the length of `s` to make this slightly safer?
Expand Down
2 changes: 1 addition & 1 deletion tests/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use alloc::vec;

use hermit::errno::{EAGAIN, ETIMEDOUT};
use hermit::time::timespec;
use hermit::{sys_futex_wait, sys_futex_wake, sys_join, sys_spawn2, sys_usleep};
use hermit::syscalls::{sys_futex_wait, sys_futex_wake, sys_join, sys_spawn2, sys_usleep};

const USER_STACK_SIZE: usize = 1_048_576;
const NORMAL_PRIO: u8 = 2;
Expand Down

0 comments on commit 9653732

Please sign in to comment.