Implement yunq server in rust.

This commit is contained in:
Drew Galbraith 2024-07-31 19:59:46 -07:00
parent dbc4e7e2ad
commit 76f8795a46
12 changed files with 312 additions and 23 deletions

View file

@ -508,7 +508,7 @@ pub struct ZReplyPortSendReq {
pub num_bytes: u64,
pub data: *const ::core::ffi::c_void,
pub num_caps: u64,
pub caps: *mut z_cap_t,
pub caps: *const z_cap_t,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]

View file

@ -122,6 +122,17 @@ pub fn port_poll(
Ok((num_bytes, num_caps))
}
pub fn endpoint_create() -> Result<z_cap_t, ZError> {
let mut endpoint_cap: z_cap_t = 0;
syscall(
zion::kZionEndpointCreate,
&zion::ZEndpointCreateReq {
endpoint_cap: &mut endpoint_cap,
},
)?;
Ok(endpoint_cap)
}
pub fn endpoint_send(
endpoint_cap: z_cap_t,
bytes: &[u8],
@ -134,7 +145,7 @@ pub fn endpoint_send(
endpoint_cap,
data: bytes.as_ptr() as *const c_void,
num_bytes: bytes.len() as u64,
reply_port_cap: &mut reply_port_cap as *mut z_cap_t,
reply_port_cap: &mut reply_port_cap,
};
syscall(zion::kZionEndpointSend, &send_req)?;
@ -142,6 +153,45 @@ pub fn endpoint_send(
Ok(reply_port_cap)
}
pub fn endpoint_recv(
endpoint_cap: z_cap_t,
bytes: &mut [u8],
caps: &mut [z_cap_t],
) -> Result<(u64, u64, z_cap_t), ZError> {
let mut num_bytes = bytes.len() as u64;
let mut num_caps = caps.len() as u64;
let mut reply_port_cap = 0;
let recv_req = zion::ZEndpointRecvReq {
endpoint_cap,
data: bytes.as_mut_ptr() as *mut c_void,
num_bytes: &mut num_bytes,
caps: caps.as_mut_ptr(),
num_caps: &mut num_caps,
reply_port_cap: &mut reply_port_cap,
};
syscall(zion::kZionEndpointRecv, &recv_req)?;
Ok((num_bytes, num_caps, reply_port_cap))
}
pub fn reply_port_send(
reply_port_cap: z_cap_t,
bytes: &[u8],
caps: &[z_cap_t],
) -> Result<(), ZError> {
syscall(
zion::kZionReplyPortSend,
&zion::ZReplyPortSendReq {
reply_port_cap,
data: bytes.as_ptr() as *const c_void,
num_bytes: bytes.len() as u64,
caps: caps.as_ptr(),
num_caps: caps.len() as u64,
},
)
}
pub fn reply_port_recv(
reply_port_cap: z_cap_t,
bytes: &mut [u8],
@ -152,9 +202,9 @@ pub fn reply_port_recv(
let recv_req = zion::ZReplyPortRecvReq {
reply_port_cap,
caps: caps.as_mut_ptr(),
num_caps: &mut num_caps as *mut u64,
num_caps: &mut num_caps,
data: bytes.as_mut_ptr() as *mut c_void,
num_bytes: &mut num_bytes as *mut u64,
num_bytes: &mut num_bytes,
};
syscall(zion::kZionReplyPortRecv, &recv_req)?;

View file

@ -2,39 +2,40 @@ use crate::syscall;
use crate::zion;
use crate::zion::z_cap_t;
use alloc::boxed::Box;
use core::ffi::c_void;
pub type ThreadEntry = fn(*const c_void) -> ();
#[no_mangle]
extern "C" fn internal_entry_point(entry_ptr: *const ThreadEntry, arg1: *const c_void) -> ! {
let entry = unsafe { *entry_ptr };
extern "C" fn internal_entry_point(thread_ptr: *const Thread, arg1: *const c_void) -> ! {
let thread: &Thread = unsafe { thread_ptr.as_ref().expect("Failed to unwrap thread ref") };
entry(arg1);
(thread.entry)(arg1);
syscall::thread_exit()
}
// TODO: Add a Drop implementation that kills this thread and drops its capability.
pub struct Thread<'a> {
pub struct Thread {
cap: z_cap_t,
// This field only exists to ensure that the entry reference will outlive the thread object
// itself.
_entry: &'a ThreadEntry,
entry: ThreadEntry,
}
impl<'a> Thread<'a> {
pub fn spawn(entry: &'a ThreadEntry, arg1: *const c_void) -> Result<Self, zion::ZError> {
impl Thread {
pub fn spawn(entry: ThreadEntry, arg1: *const c_void) -> Result<Box<Self>, zion::ZError> {
let proc_cap = unsafe { crate::init::SELF_PROC_CAP };
let cap = syscall::thread_create(proc_cap)?;
let thread = Box::new(Self { cap, entry });
syscall::thread_start(
cap,
internal_entry_point as u64,
entry as *const ThreadEntry as u64,
thread.as_ref() as *const Thread as u64,
arg1 as u64,
)?;
Ok(Self { cap, _entry: entry })
Ok(thread)
}
pub fn join(&self) -> Result<(), zion::ZError> {