[Mammoth] Move thread syscalls to wrappers.
This commit is contained in:
parent
612a5ac572
commit
ca67da16e6
|
@ -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<T>(id: u64, req: &T) -> Result<(), zion::ZError> {
|
||||
pub fn syscall<T>(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<z_cap_t, ZError> {
|
||||
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!();
|
||||
}
|
||||
|
|
|
@ -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<Self, zion::ZError> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.");
|
||||
|
||||
|
|
Loading…
Reference in New Issue