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> {

View file

@ -28,5 +28,11 @@ pub fn call_endpoint<Req: YunqMessage, Resp: YunqMessage, const N: usize>(
cap_buffer.as_mut_slice(),
)?;
let resp_code: u64 = byte_buffer.at(8)?;
if resp_code != 0 {
return Err(ZError::from(resp_code));
}
Ok(Resp::parse_from_request(&byte_buffer, &cap_buffer)?)
}

View file

@ -6,6 +6,7 @@ extern crate alloc;
mod buffer;
pub mod client;
pub mod message;
pub mod server;
pub use buffer::ByteBuffer;
pub use message::YunqMessage;

View file

@ -4,13 +4,23 @@ use mammoth::zion::z_cap_t;
use mammoth::zion::ZError;
pub const MESSAGE_IDENT: u32 = 0x33441122;
const SENTINEL: u32 = 0xBEEFDEAD;
pub const MESSAGE_HEADER_SIZE: usize = 24; // 4x uint32, 1x uint64
const SENTINEL: u32 = 0xBEEFDEAD;
const SERIALIZE_HEADER_SIZE: u32 = 0x10;
pub fn field_offset(offset: usize, field_index: usize) -> usize {
offset + MESSAGE_HEADER_SIZE + (8 * field_index)
}
pub fn serialize_error<const N: usize>(buf: &mut ByteBuffer<N>, err: ZError) {
buf.write_at(0, SENTINEL)
.expect("Failed to serialize SENTINEL");
buf.write_at(4, SERIALIZE_HEADER_SIZE)
.expect("Failed to serialize size");
buf.write_at(8, err as u64)
.expect("Failed to serialize error");
}
pub trait YunqMessage {
fn parse<const N: usize>(
buf: &ByteBuffer<N>,
@ -31,11 +41,6 @@ pub trait YunqMessage {
return Err(ZError::INVALID_RESPONSE);
}
let resp_code: u64 = buf.at(8)?;
if resp_code != 0 {
return Err(ZError::from(resp_code));
}
Ok(Self::parse(&buf, 16, &caps)?)
}

View file

@ -0,0 +1,46 @@
use crate::buffer::ByteBuffer;
use alloc::vec::Vec;
use mammoth::syscall;
use mammoth::zion::z_cap_t;
use mammoth::zion::ZError;
pub trait YunqServer {
fn server_loop(&self) {
loop {
let mut byte_buffer = ByteBuffer::<1024>::new();
let mut cap_buffer = vec![0; 10];
let (_, _, reply_port_cap) = syscall::endpoint_recv(
self.endpoint_cap(),
byte_buffer.mut_slice(),
&mut cap_buffer,
)
.expect("Failed to call endpoint recv");
let method = byte_buffer
.at::<u64>(8)
.expect("Failed to access request length.");
let resp = self.handle_request(method, &mut byte_buffer, &mut cap_buffer);
match resp {
Ok(resp_len) => syscall::reply_port_send(
reply_port_cap,
byte_buffer.slice(resp_len),
&cap_buffer,
)
.expect("Failed to reply"),
Err(err) => {
crate::message::serialize_error(&mut byte_buffer, err);
syscall::reply_port_send(reply_port_cap, &byte_buffer.slice(0x10), &[])
.expect("Failed to reply w/ error")
}
}
}
}
fn endpoint_cap(&self) -> z_cap_t;
fn handle_request(
&self,
method_number: u64,
byte_buffer: &mut ByteBuffer<1024>,
cap_buffer: &mut Vec<z_cap_t>,
) -> Result<usize, ZError>;
}