[Mammoth] Move thread syscalls to wrappers.
This commit is contained in:
parent
612a5ac572
commit
ca67da16e6
3 changed files with 51 additions and 46 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue