[Mammoth] Move thread syscalls to wrappers.
This commit is contained in:
parent
612a5ac572
commit
ca67da16e6
|
@ -1,11 +1,13 @@
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
use crate::zion;
|
use crate::zion;
|
||||||
|
use crate::zion::z_cap_t;
|
||||||
|
use crate::zion::ZError;
|
||||||
use core::ffi::c_void;
|
use core::ffi::c_void;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn syscall<T>(id: u64, req: &T) -> Result<(), zion::ZError> {
|
pub fn syscall<T>(id: u64, req: &T) -> Result<(), ZError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let resp = zion::SysCall1(id, req as *const T as *const c_void);
|
let resp = zion::SysCall1(id, req as *const T as *const c_void);
|
||||||
if resp != 0 {
|
if resp != 0 {
|
||||||
|
@ -37,3 +39,36 @@ pub fn debug(msg: &str) {
|
||||||
};
|
};
|
||||||
syscall(zion::kZionDebug, &req).expect("Failed to write");
|
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) -> ();
|
pub type ThreadEntry = fn(*const c_void) -> ();
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
|
extern "C" fn internal_entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
|
||||||
debug!("Entry {:#p} arg1 {:#x}", entry_ptr, arg1 as u64);
|
|
||||||
let entry = unsafe { *entry_ptr };
|
let entry = unsafe { *entry_ptr };
|
||||||
|
|
||||||
entry(arg1);
|
entry(arg1);
|
||||||
|
|
||||||
let _ = syscall::syscall(zion::kZionThreadExit, &zion::ZThreadExitReq {});
|
syscall::thread_exit()
|
||||||
|
|
||||||
unreachable!();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add a Drop implementation that kills this thread and drops its capability.
|
// TODO: Add a Drop implementation that kills this thread and drops its capability.
|
||||||
pub struct Thread<'a> {
|
pub struct Thread<'a> {
|
||||||
cap: z_cap_t,
|
cap: z_cap_t,
|
||||||
|
@ -27,35 +23,21 @@ pub struct Thread<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Thread<'a> {
|
impl<'a> Thread<'a> {
|
||||||
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Self {
|
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Result<Self, zion::ZError> {
|
||||||
let mut cap: z_cap_t = 0;
|
let proc_cap = unsafe { crate::init::SELF_PROC_CAP };
|
||||||
let req = zion::ZThreadCreateReq {
|
let cap = syscall::thread_create(proc_cap)?;
|
||||||
proc_cap: unsafe { crate::init::SELF_PROC_CAP },
|
|
||||||
thread_cap: &mut cap as *mut z_cap_t,
|
|
||||||
};
|
|
||||||
|
|
||||||
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(
|
Ok(Self { cap, _entry: entry })
|
||||||
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 }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join(&self) -> Result<(), zion::ZError> {
|
pub fn join(&self) -> Result<(), zion::ZError> {
|
||||||
syscall::syscall(
|
syscall::thread_wait(self.cap)
|
||||||
zion::kZionThreadWait,
|
|
||||||
&zion::ZThreadWaitReq {
|
|
||||||
thread_cap: self.cap,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,9 @@
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
use alloc::boxed::Box;
|
|
||||||
use alloc::string::ToString;
|
use alloc::string::ToString;
|
||||||
use mammoth::debug;
|
use mammoth::debug;
|
||||||
use mammoth::define_entry;
|
use mammoth::define_entry;
|
||||||
use mammoth::syscall::debug;
|
|
||||||
use mammoth::thread;
|
use mammoth::thread;
|
||||||
use mammoth::zion::z_err_t;
|
use mammoth::zion::z_err_t;
|
||||||
use yellowstone::GetEndpointRequest;
|
use yellowstone::GetEndpointRequest;
|
||||||
|
@ -17,17 +15,7 @@ define_entry!();
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn main() -> z_err_t {
|
pub extern "C" fn main() -> z_err_t {
|
||||||
debug("Testing!");
|
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");
|
|
||||||
|
|
||||||
let mut yellowstone;
|
let mut yellowstone;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -45,7 +33,7 @@ pub extern "C" fn main() -> z_err_t {
|
||||||
let e: thread::ThreadEntry = |_| {
|
let e: thread::ThreadEntry = |_| {
|
||||||
debug!("Testing 1 2 3");
|
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.");
|
t.join().expect("Failed to wait.");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue