refactor: large code cleanup and safety pass

This commit is contained in:
Zoe
2026-09-07 13:31:49 -05:00
parent 3308fd2959
commit bf1a81cf82
20 changed files with 1026 additions and 388 deletions
+3 -5
View File
@@ -8,12 +8,10 @@ pub extern "C" fn _start() -> ! {
let msg = "Hello from client!";
println!("[client] Sent: {}", msg);
// TODO: we assume the echo server is task 1 (spawned by omega3)
sys_send(1, msg.as_ptr() as usize, msg.len()).unwrap();
sys_send(1, msg.as_bytes()).unwrap();
let out = [0u8; 128];
let out_ptr = out.as_ptr() as usize;
let max_len = out.len();
let (actual_len, _) = sys_recv(out_ptr, max_len).unwrap();
let mut out = [0u8; 128];
let (actual_len, _) = sys_recv(&mut out).unwrap();
println!(
"[client] Received: {}",
core::str::from_utf8(&out[..actual_len]).unwrap()
+37 -24
View File
@@ -10,15 +10,25 @@ pub enum Status {
BadFileDescriptor = 3,
NoSuchTask = 4,
OutOfMemory = 5,
BadHandle = 6,
}
// Opaque handle type
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Handle(usize);
pub struct AddressSpaceHandle(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FrameHandle(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MappingHandle(usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ThreadHandle(usize);
// our own address space and thread handle are always given to us
pub const SELF_AS: Handle = Handle(0);
pub const SELF_THREAD: Handle = Handle(1);
pub const SELF_AS: AddressSpaceHandle = AddressSpaceHandle(0);
pub const SELF_THREAD: ThreadHandle = ThreadHandle(1);
impl From<usize> for Status {
fn from(value: usize) -> Self {
@@ -28,6 +38,7 @@ impl From<usize> for Status {
3 => Self::BadFileDescriptor,
4 => Self::NoSuchTask,
5 => Self::OutOfMemory,
6 => Self::BadHandle,
_ => Self::InvalidArgument,
}
}
@@ -125,15 +136,15 @@ pub fn sys_exit(exit_code: usize) -> ! {
}
}
pub fn sys_send(dest_task_id: usize, msg_ptr: usize, len: usize) -> Result<(), Status> {
pub fn sys_send(dest_task_id: usize, msg: &[u8]) -> Result<(), Status> {
unsafe {
let status: usize;
asm!(
"syscall",
in("rdi") dest_task_id,
in("rsi") msg_ptr,
in("rdx") len,
in("rsi") msg.as_ptr(),
in("rdx") msg.len(),
inlateout("rax") SyscallNumber::Send as usize => status,
lateout("rcx") _,
lateout("r11") _,
@@ -147,7 +158,7 @@ pub fn sys_send(dest_task_id: usize, msg_ptr: usize, len: usize) -> Result<(), S
}
}
pub fn sys_recv(buf_ptr: usize, max_len: usize) -> Result<(usize, usize), Status> {
pub fn sys_recv(buf: &mut [u8]) -> Result<(usize, usize), Status> {
let mut actual_len: usize = 0;
let mut sender: usize = 0;
@@ -156,8 +167,8 @@ pub fn sys_recv(buf_ptr: usize, max_len: usize) -> Result<(usize, usize), Status
asm!(
"syscall",
in("rdi") buf_ptr,
in("rsi") max_len,
in("rdi") buf.as_mut_ptr(),
in("rsi") buf.len(),
in("rdx") &raw mut actual_len as usize,
in("r10") &raw mut sender as usize,
inlateout("rax") SyscallNumber::Recv as usize => status,
@@ -173,7 +184,7 @@ pub fn sys_recv(buf_ptr: usize, max_len: usize) -> Result<(usize, usize), Status
}
}
pub fn sys_frame_alloc() -> Result<Handle, Status> {
pub fn sys_frame_alloc() -> Result<FrameHandle, Status> {
let mut handle: usize = 0;
unsafe {
let status: usize;
@@ -189,12 +200,12 @@ pub fn sys_frame_alloc() -> Result<Handle, Status> {
if status != 0 {
Err(status.into())
} else {
Ok(Handle(handle))
Ok(FrameHandle(handle))
}
}
}
pub fn sys_frame_dealloc(frame_handle: Handle) -> Result<(), Status> {
pub fn sys_frame_dealloc(frame_handle: FrameHandle) -> Result<(), Status> {
unsafe {
let status: usize;
@@ -214,7 +225,7 @@ pub fn sys_frame_dealloc(frame_handle: Handle) -> Result<(), Status> {
}
}
pub fn sys_as_create() -> Result<Handle, Status> {
pub fn sys_as_create() -> Result<AddressSpaceHandle, Status> {
let mut handle: usize = 0;
unsafe {
let status: usize;
@@ -230,17 +241,19 @@ pub fn sys_as_create() -> Result<Handle, Status> {
if status != 0 {
Err(status.into())
} else {
Ok(Handle(handle))
Ok(AddressSpaceHandle(handle))
}
}
}
pub fn sys_map(
as_handle: Handle,
frame_handle: Handle,
as_handle: AddressSpaceHandle,
frame_handle: FrameHandle,
virtual_addr: usize,
permissions: usize,
) -> Result<(), Status> {
) -> Result<MappingHandle, Status> {
let mut handle: usize = 0;
unsafe {
let status: usize;
@@ -250,6 +263,7 @@ pub fn sys_map(
in("rsi") frame_handle.0,
in("rdx") virtual_addr,
in("r10") permissions,
in("r8") &raw mut handle as usize,
inlateout("rax") SyscallNumber::Map as usize => status,
lateout("rcx") _,
lateout("r11") _,
@@ -258,19 +272,18 @@ pub fn sys_map(
if status != 0 {
Err(status.into())
} else {
Ok(())
Ok(MappingHandle(handle))
}
}
}
pub fn sys_unmap(as_handle: Handle, virtual_addr: usize) -> Result<(), Status> {
pub fn sys_unmap(mapping_handle: MappingHandle) -> Result<(), Status> {
unsafe {
let status: usize;
asm!(
"syscall",
in("rdi") as_handle.0,
in("rsi") virtual_addr,
in("rdi") mapping_handle.0,
inlateout("rax") SyscallNumber::Unmap as usize => status,
lateout("rcx") _,
lateout("r11") _,
@@ -285,10 +298,10 @@ pub fn sys_unmap(as_handle: Handle, virtual_addr: usize) -> Result<(), Status> {
}
pub fn sys_task_create(
as_handle: Handle,
as_handle: AddressSpaceHandle,
entry: usize,
user_stack: usize,
) -> Result<Handle, Status> {
) -> Result<ThreadHandle, Status> {
let mut handle: usize = 0;
unsafe {
@@ -308,7 +321,7 @@ pub fn sys_task_create(
if status != 0 {
Err(status.into())
} else {
Ok(Handle(handle))
Ok(ThreadHandle(handle))
}
}
}
+3 -3
View File
@@ -5,14 +5,14 @@ use dusk_sys::{println, sys_exit, sys_recv, sys_send};
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
let out = [0u8; 128];
let mut out = [0u8; 128];
loop {
let (actual_len, sender) = sys_recv(out.as_ptr() as usize, out.len()).unwrap();
let (actual_len, sender) = sys_recv(&mut out).unwrap();
println!(
"[echo] Received: {}",
core::str::from_utf8(&out[..actual_len]).unwrap()
);
sys_send(sender, out.as_ptr() as usize, actual_len).unwrap();
sys_send(sender, &out[..actual_len]).unwrap();
}
}
+11 -6
View File
@@ -5,8 +5,8 @@ mod cpio;
mod elf;
use dusk_sys::{
Handle, SELF_AS, println, sys_as_create, sys_exit, sys_frame_alloc, sys_map, sys_task_create,
sys_unmap, sys_yield,
AddressSpaceHandle, SELF_AS, println, sys_as_create, sys_exit, sys_frame_alloc, sys_map,
sys_task_create, sys_unmap, sys_yield,
};
// Mapped into the root task's address space by the kernel.
@@ -40,10 +40,15 @@ pub extern "C" fn _start() -> ! {
map_stack(client_as, STACK_TOP, STACK_PAGES);
let _ = sys_task_create(client_as, client_entry, STACK_TOP).unwrap();
let ptr = 0xDEAD_BEEF as *mut u32;
unsafe {
core::ptr::write_volatile(&mut *ptr, 0xDEAD_BEEF);
}
sys_exit(0);
}
fn load_elf(elf: &elf::Elf, target_as: Handle) -> usize {
fn load_elf(elf: &elf::Elf, target_as: AddressSpaceHandle) -> usize {
for header in elf.program_headers().unwrap() {
let header = header.unwrap();
if header.segment_type != elf::ProgramHeaderType::Load || header.memory_size == 0 {
@@ -66,7 +71,7 @@ fn load_elf(elf: &elf::Elf, target_as: Handle) -> usize {
for page in (page_start..segment_end).step_by(0x1000) {
let frame = sys_frame_alloc().unwrap();
sys_map(SELF_AS, frame, SCRATCH_PAGE, 0b01).unwrap();
let scratch_handle = sys_map(SELF_AS, frame, SCRATCH_PAGE, 0b01).unwrap();
unsafe {
core::ptr::write_bytes(SCRATCH_PAGE as *mut u8, 0, 0x1000);
@@ -84,7 +89,7 @@ fn load_elf(elf: &elf::Elf, target_as: Handle) -> usize {
);
}
}
sys_unmap(SELF_AS, SCRATCH_PAGE).unwrap();
sys_unmap(scratch_handle).unwrap();
sys_map(target_as, frame, page, perms).unwrap();
}
@@ -93,7 +98,7 @@ fn load_elf(elf: &elf::Elf, target_as: Handle) -> usize {
elf.entry()
}
fn map_stack(target_as: Handle, stack_top: usize, pages: usize) {
fn map_stack(target_as: AddressSpaceHandle, stack_top: usize, pages: usize) {
for i in 1..=pages {
let frame = sys_frame_alloc().unwrap();
let page_addr = stack_top - i * 0x1000;