From ca67da16e665e088cc1acf8bef1599a6e641e1c2 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sun, 28 Jul 2024 19:48:05 -0700 Subject: [PATCH] [Mammoth] Move thread syscalls to wrappers. --- rust/lib/mammoth/src/syscall.rs | 37 ++++++++++++++++++++++++++- rust/lib/mammoth/src/thread.rs | 44 ++++++++++----------------------- rust/usr/testbed/src/main.rs | 16 ++---------- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/rust/lib/mammoth/src/syscall.rs b/rust/lib/mammoth/src/syscall.rs index d46bea0..aa0fc5b 100644 --- a/rust/lib/mammoth/src/syscall.rs +++ b/rust/lib/mammoth/src/syscall.rs @@ -1,11 +1,13 @@ extern crate alloc; use crate::zion; +use crate::zion::z_cap_t; +use crate::zion::ZError; use core::ffi::c_void; use core::panic::PanicInfo; #[must_use] -pub fn syscall(id: u64, req: &T) -> Result<(), zion::ZError> { +pub fn syscall(id: u64, req: &T) -> Result<(), ZError> { unsafe { let resp = zion::SysCall1(id, req as *const T as *const c_void); if resp != 0 { @@ -37,3 +39,36 @@ pub fn debug(msg: &str) { }; syscall(zion::kZionDebug, &req).expect("Failed to write"); } + +pub fn thread_create(proc_cap: z_cap_t) -> Result { + let mut cap = 0; + syscall( + zion::kZionThreadCreate, + &zion::ZThreadCreateReq { + proc_cap, + thread_cap: &mut cap as *mut z_cap_t, + }, + )?; + Ok(cap) +} + +pub fn thread_start(thread_cap: z_cap_t, entry: u64, arg1: u64, arg2: u64) -> Result<(), ZError> { + syscall( + zion::kZionThreadStart, + &zion::ZThreadStartReq { + thread_cap, + entry, + arg1, + arg2, + }, + ) +} + +pub fn thread_wait(thread_cap: z_cap_t) -> Result<(), ZError> { + syscall(zion::kZionThreadWait, &zion::ZThreadWaitReq { thread_cap }) +} + +pub fn thread_exit() -> ! { + let _ = syscall(zion::kZionThreadExit, &zion::ZThreadExitReq {}); + unreachable!(); +} diff --git a/rust/lib/mammoth/src/thread.rs b/rust/lib/mammoth/src/thread.rs index d596c20..1ddc85d 100644 --- a/rust/lib/mammoth/src/thread.rs +++ b/rust/lib/mammoth/src/thread.rs @@ -7,17 +7,13 @@ use core::ffi::c_void; pub type ThreadEntry = fn(*const c_void) -> (); #[no_mangle] -extern "C" fn entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! { - debug!("Entry {:#p} arg1 {:#x}", entry_ptr, arg1 as u64); +extern "C" fn internal_entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! { let entry = unsafe { *entry_ptr }; entry(arg1); - let _ = syscall::syscall(zion::kZionThreadExit, &zion::ZThreadExitReq {}); - - unreachable!(); + syscall::thread_exit() } - // TODO: Add a Drop implementation that kills this thread and drops its capability. pub struct Thread<'a> { cap: z_cap_t, @@ -27,35 +23,21 @@ pub struct Thread<'a> { } impl<'a> Thread<'a> { - pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Self { - let mut cap: z_cap_t = 0; - let req = zion::ZThreadCreateReq { - proc_cap: unsafe { crate::init::SELF_PROC_CAP }, - thread_cap: &mut cap as *mut z_cap_t, - }; + pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Result { + let proc_cap = unsafe { crate::init::SELF_PROC_CAP }; + let cap = syscall::thread_create(proc_cap)?; - syscall::syscall(zion::kZionThreadCreate, &req).expect("Failed to create thread."); + syscall::thread_start( + cap, + internal_entry_point as u64, + entry as *const ThreadEntry as u64, + arg1 as u64, + )?; - syscall::syscall( - zion::kZionThreadStart, - &zion::ZThreadStartReq { - thread_cap: cap, - entry: entry_point as u64, - arg1: entry as *const ThreadEntry as u64, - arg2: arg1 as u64, - }, - ) - .expect("Failed to start thread."); - - Self { cap, _entry: entry } + Ok(Self { cap, _entry: entry }) } pub fn join(&self) -> Result<(), zion::ZError> { - syscall::syscall( - zion::kZionThreadWait, - &zion::ZThreadWaitReq { - thread_cap: self.cap, - }, - ) + syscall::thread_wait(self.cap) } } diff --git a/rust/usr/testbed/src/main.rs b/rust/usr/testbed/src/main.rs index b612523..661c98b 100644 --- a/rust/usr/testbed/src/main.rs +++ b/rust/usr/testbed/src/main.rs @@ -3,11 +3,9 @@ extern crate alloc; -use alloc::boxed::Box; use alloc::string::ToString; use mammoth::debug; use mammoth::define_entry; -use mammoth::syscall::debug; use mammoth::thread; use mammoth::zion::z_err_t; use yellowstone::GetEndpointRequest; @@ -17,17 +15,7 @@ define_entry!(); #[no_mangle] pub extern "C" fn main() -> z_err_t { - debug("Testing!"); - let x = Box::new("Heap str"); - debug(&x); - debug!("Formatted {}", "string"); - let mut vmmo_cap: u64 = 0; - let obj_req = mammoth::zion::ZMemoryObjectCreateReq { - size: 0, - vmmo_cap: &mut vmmo_cap as *mut u64, - }; - mammoth::syscall::syscall(mammoth::zion::kZionMemoryObjectCreate, &obj_req) - .expect("Failed to create memory object"); + debug!("Testing!"); let mut yellowstone; unsafe { @@ -45,7 +33,7 @@ pub extern "C" fn main() -> z_err_t { let e: thread::ThreadEntry = |_| { debug!("Testing 1 2 3"); }; - let t = thread::Thread::spawn(&e, core::ptr::null()); + let t = thread::Thread::spawn(&e, core::ptr::null()).expect("Failed to spawn thread"); t.join().expect("Failed to wait.");