[Teton] Move console/shell to rust. WIP

This commit is contained in:
Drew Galbraith 2024-08-12 11:35:54 -07:00
parent 76f8795a46
commit 18e512cf1f
17 changed files with 409 additions and 5 deletions

View file

@ -1,4 +1,6 @@
use crate::syscall;
use crate::zion::{z_cap_t, ZError};
use alloc::slice;
use linked_list_allocator::LockedHeap;
#[global_allocator]
@ -16,3 +18,57 @@ pub fn init_heap() {
CAN_ALLOC = true;
}
}
pub struct MemoryRegion {
mem_cap: z_cap_t,
virt_addr: u64,
size: u64,
}
impl MemoryRegion {
pub fn direct_physical(paddr: u64, size: u64) -> Result<Self, ZError> {
let mem_cap = syscall::memory_object_direct_physical(paddr, size)?;
let virt_addr = syscall::address_space_map(mem_cap)?;
Ok(Self {
mem_cap,
virt_addr,
size,
})
}
pub fn from_cap(mem_cap: z_cap_t) -> Result<Self, ZError> {
let virt_addr = syscall::address_space_map(mem_cap)?;
let size = syscall::memory_object_inspect(mem_cap)?;
Ok(Self {
mem_cap,
virt_addr,
size,
})
}
pub fn slice<T>(&self) -> &[T] {
unsafe {
slice::from_raw_parts(
self.virt_addr as *const T,
self.size as usize / size_of::<T>(),
)
}
}
pub fn mut_slice<T>(&self) -> &mut [T] {
unsafe {
slice::from_raw_parts_mut(
self.virt_addr as *mut T,
self.size as usize / size_of::<T>(),
)
}
}
}
impl Drop for MemoryRegion {
fn drop(&mut self) {
syscall::address_space_unmap(self.virt_addr, self.virt_addr + self.size)
.expect("Failed to unmap memory");
syscall::cap_release(self.mem_cap).expect("Failed to release memory cap");
}
}

View file

@ -89,6 +89,31 @@ pub fn memory_object_create(size: u64) -> Result<z_cap_t, ZError> {
Ok(vmmo_cap)
}
pub fn memory_object_direct_physical(paddr: u64, size: u64) -> Result<z_cap_t, ZError> {
let mut vmmo_cap = 0;
syscall(
zion::kZionMemoryObjectCreatePhysical,
&zion::ZMemoryObjectCreatePhysicalReq {
paddr,
size,
vmmo_cap: &mut vmmo_cap,
},
)?;
Ok(vmmo_cap)
}
pub fn memory_object_inspect(mem_cap: z_cap_t) -> Result<u64, ZError> {
let mut mem_size = 0;
syscall(
zion::kZionMemoryObjectInspect,
&zion::ZMemoryObjectInspectReq {
vmmo_cap: mem_cap,
size: &mut mem_size,
},
)?;
Ok(mem_size)
}
pub fn address_space_map(vmmo_cap: z_cap_t) -> Result<u64, ZError> {
let mut vaddr: u64 = 0;
// FIXME: Allow caller to pass these options.
@ -104,6 +129,17 @@ pub fn address_space_map(vmmo_cap: z_cap_t) -> Result<u64, ZError> {
Ok(vaddr)
}
pub fn address_space_unmap(lower_addr: u64, upper_addr: u64) -> Result<(), ZError> {
syscall(
zion::kZionAddressSpaceUnmap,
&zion::ZAddressSpaceUnmapReq {
vmas_cap: unsafe { crate::init::SELF_VMAS_CAP },
lower_addr,
upper_addr,
},
)
}
pub fn port_poll(
port_cap: z_cap_t,
bytes: &mut [u8],
@ -211,3 +247,7 @@ pub fn reply_port_recv(
Ok((num_bytes, num_caps))
}
pub fn cap_release(cap: z_cap_t) -> Result<(), ZError> {
syscall(zion::kZionCapRelease, &zion::ZCapReleaseReq { cap })
}

View file

@ -0,0 +1,15 @@
[package]
name = "victoriafalls"
version = "0.1.0"
edition = "2021"
[dependencies]
mammoth = { path = "../mammoth" }
yellowstone = { path = "../yellowstone" }
yunq = {path = "../yunq"}
yunq-derive = {path = "../yunq-derive"}
[build-dependencies]
yunqc = {path = "../../../yunq/rust"}

View file

@ -0,0 +1,14 @@
use std::fs;
fn main() {
let input_file = "../../../sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq";
println!("cargo::rerun-if-changed={input_file}");
let input = fs::read_to_string(input_file).expect("Failed to read input file");
let code = yunqc::codegen(&input).expect("Failed to generate yunq code.");
let out = std::env::var("OUT_DIR").unwrap() + "/yunq.rs";
fs::write(out, code).expect("Failed to write generated code.");
}

View file

@ -0,0 +1,42 @@
use crate::OpenFileRequest;
use crate::VFSClient;
use alloc::string::ToString;
use mammoth::zion::ZError;
static mut VFS_CLIENT: Option<VFSClient> = None;
fn get_client() -> &'static mut VFSClient {
unsafe {
if let None = VFS_CLIENT {
let endpoint_cap = yellowstone::from_init_endpoint()
.get_endpoint(&yellowstone::GetEndpointRequest {
endpoint_name: "victoriafalls".to_string(),
})
.expect("Failed to get VFS endpoint");
VFS_CLIENT = Some(VFSClient::new(endpoint_cap.endpoint));
}
VFS_CLIENT.as_mut().unwrap()
}
}
pub struct File {
memory: mammoth::mem::MemoryRegion,
}
impl File {
pub fn open(path: &str) -> Result<Self, ZError> {
let vfs = get_client();
let resp = vfs.open_file(&OpenFileRequest {
path: path.to_string(),
})?;
Ok(Self {
memory: mammoth::mem::MemoryRegion::from_cap(resp.memory)?,
})
}
pub fn slice(&self, offset: usize, len: usize) -> &[u8] {
&self.memory.slice()[offset..offset + len]
}
}

View file

@ -0,0 +1,7 @@
#![no_std]
use core::include;
include!(concat!(env!("OUT_DIR"), "/yunq.rs"));
pub mod file;

View file

@ -3,3 +3,17 @@
use core::include;
include!(concat!(env!("OUT_DIR"), "/yunq.rs"));
use mammoth::init::INIT_ENDPOINT;
static mut YELLOWSTONE_INIT: Option<YellowstoneClient> = None;
pub fn from_init_endpoint() -> &'static mut YellowstoneClient {
unsafe {
if let None = YELLOWSTONE_INIT {
YELLOWSTONE_INIT = Some(YellowstoneClient::new(INIT_ENDPOINT));
}
YELLOWSTONE_INIT.as_mut().unwrap()
}
}

View file

@ -79,7 +79,7 @@ impl YunqMessage for Empty {
where
Self: Sized,
{
todo!()
Ok(Self {})
}
fn serialize<const N: usize>(
@ -88,6 +88,6 @@ impl YunqMessage for Empty {
_offset: usize,
_caps: &mut Vec<z_cap_t>,
) -> Result<usize, ZError> {
todo!()
Ok(0)
}
}