Skip to content

Commit

Permalink
Added error handling, minor design changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CPTforever committed Aug 1, 2024
1 parent 44aa5dd commit 38eb2c1
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 95 deletions.
2 changes: 2 additions & 0 deletions src/lib/twizzler-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ bitset-core = { version = "0.1", optional = true, default-features = false }
cfg-if = "1.0"
num_enum = { version = "0.7", default-features = false }
thiserror = { package = "thiserror-no-std", version = "2.0", default-features = false }
stable-vec = "0.4.1"
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }

[dependencies.volatile]
optional = true
Expand Down
55 changes: 28 additions & 27 deletions src/lib/twizzler-abi/src/runtime/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use core::{intrinsics::size_of, ptr::NonNull};
use rustc_alloc::{borrow::ToOwned, sync::Arc};
use crate::runtime::object::slot;

use twizzler_runtime_api::{OwnedFd, FsError, SeekFrom};
use twizzler_runtime_api::{RawFd, FsError, SeekFrom};

use stable_vec::{self, StableVec};
use lazy_static::lazy_static;

struct FileDesc {
slot_id: u32,
Expand All @@ -37,15 +40,16 @@ struct FileMetadata {

const MAGIC_NUMBER: u64 = 0xBEEFDEAD;

static FD_SLOTS: Mutex<BTreeMap<u32, Arc<Mutex<FileDesc>>>> = Mutex::new(BTreeMap::new());
static FD_ID_COUNTER: IdCounter = IdCounter::new_one();
lazy_static! {
static ref FD_SLOTS: Mutex<StableVec<Arc<Mutex<FileDesc>>>> = Mutex::new(StableVec::new());
}

fn get_fd_slots() -> &'static Mutex<BTreeMap<u32, Arc<Mutex<FileDesc>>>> {
fn get_fd_slots() -> &'static Mutex<StableVec<Arc<Mutex<FileDesc>>>> {
&FD_SLOTS
}

impl RustFsRuntime for MinimalRuntime {
fn open(&self, path: &core::ffi::CStr) -> Result<OwnedFd, FsError> {
fn open(&self, path: &core::ffi::CStr) -> Result<RawFd, FsError> {
let obj_id = ObjID::new(
path
.to_str()
Expand All @@ -66,63 +70,60 @@ impl RustFsRuntime for MinimalRuntime {
} };
}

let fd = FD_ID_COUNTER.fresh();
get_fd_slots()
let fd = get_fd_slots()
.lock()
.insert(fd, Arc::new(Mutex::new(FileDesc {
.push(Arc::new(Mutex::new(FileDesc {
slot_id: 0,
pos: 0,
handle: handle
})));

Ok (OwnedFd{
internal_fd: fd
})

Ok (fd.try_into().unwrap())
}

fn read(&self, fd: OwnedFd, buf: *mut u8, len: usize) -> Result<usize, FsError> {
fn read(&self, fd: RawFd, buf: &mut [u8]) -> Result<usize, FsError> {
let binding = get_fd_slots()
.lock();
let mut file_desc =
binding
.get(&fd.internal_fd)
.get(fd.try_into().unwrap())
.ok_or(FsError::LookupError)?;

let mut binding = file_desc.lock();

unsafe { buf.copy_from(binding.handle.start.offset(NULLPAGE_SIZE as isize + binding.pos as isize + size_of::<FileMetadata>() as isize), len) }
unsafe { buf.as_mut_ptr().copy_from(binding.handle.start.offset(NULLPAGE_SIZE as isize + binding.pos as isize + size_of::<FileMetadata>() as isize), buf.len()) }

binding.pos += len as u64;
binding.pos += buf.len() as u64;

Ok(len)
Ok(buf.len())
}

fn write(&self, fd: OwnedFd, buf: *const u8, len: usize) -> Result<usize, FsError> {
fn write(&self, fd: RawFd, buf: &[u8]) -> Result<usize, FsError> {
let binding = get_fd_slots()
.lock();
let file_desc =
binding
.get(&fd.internal_fd)
.get(fd.try_into().unwrap())
.ok_or(FsError::LookupError)?;

let mut binding = file_desc.lock();

unsafe {
unsafe {
binding.handle.start
.offset(NULLPAGE_SIZE as isize + binding.pos as isize + size_of::<FileMetadata>() as isize)
.copy_from(buf, len)
.copy_from(buf.as_ptr(), buf.len())
}

binding.pos += len as u64;
binding.pos += buf.len() as u64;

Ok(len)
Ok(buf.len())
}

fn close(&self, fd: OwnedFd) -> Result<(), FsError> {
fn close(&self, fd: RawFd) -> Result<(), FsError> {
let file_desc =
get_fd_slots()
.lock()
.remove(&fd.internal_fd)
.remove(fd.try_into().unwrap())
.ok_or(FsError::LookupError)?;

let mut binding = file_desc.lock();
Expand All @@ -132,13 +133,13 @@ impl RustFsRuntime for MinimalRuntime {
Ok(())
}

fn seek(&self, fd: OwnedFd, pos: SeekFrom) -> Result<usize, FsError> {
fn seek(&self, fd: RawFd, pos: SeekFrom) -> Result<usize, FsError> {
let binding = get_fd_slots()
.lock();

let file_desc =
binding
.get(&fd.internal_fd)
.get(fd.try_into().unwrap())
.ok_or(FsError::LookupError)?;

let mut binding = file_desc.lock();
Expand Down
42 changes: 23 additions & 19 deletions src/lib/twizzler-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,43 +498,47 @@ pub struct BasicReturn {
pub enum FsError {
/// Error is unclassified.
Other,
// Path provided isn't a valid u128 integer
InvalidPath,
// Couldn't find the file descriptor
LookupError
LookupError,
// Seek Error
SeekError,
}

impl Display for FsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FsError::Other => write!(f, "unknown error"),
FsError::InvalidPath => write!(f, "Path is invalid"),
FsError::LookupError => write!(f, "Couldn't find file descriptor"),
FsError::SeekError => write!(f, "Couldn't seek to this position"),
}
}
}

impl core::error::Error for FsError {}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub enum SeekFrom {
Start(u64),
End(i64),
Current(i64),
}


#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct OwnedFd {
pub internal_fd: u32
}

/*impl core::fmt::Debug for OwnedFd {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("OwnedFd")
.field("internal_fd", &self.internal_fd)
.finish()
}
}*/
pub type RawFd = u32;

/// Runtime that implements std's FS support. Currently being implemented.
pub trait RustFsRuntime {
fn open(&self, path: &CStr) -> Result<OwnedFd, FsError>;
fn open(&self, path: &CStr) -> Result<RawFd, FsError>;

fn read(&self, fd: OwnedFd, buf: *mut u8, len: usize) -> Result<usize, FsError>;
fn read(&self, fd: RawFd, buf: &mut [u8]) -> Result<usize, FsError>;

fn write(&self, fd: OwnedFd, buf: *const u8, len: usize) -> Result<usize, FsError>;
fn write(&self, fd: RawFd, buf: &[u8]) -> Result<usize, FsError>;

fn close(&self, fd: OwnedFd) -> Result<(), FsError>;
fn close(&self, fd: RawFd) -> Result<(), FsError>;

fn seek(&self, fd: OwnedFd, pos: SeekFrom) -> Result<usize, FsError>;
fn seek(&self, fd: RawFd, pos: SeekFrom) -> Result<usize, FsError>;
}

/// Runtime that implements std's process and command support. Currently unimplemented.
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/twz-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ elf = "0.7"
static_assertions = "1.1"
monitor-api = { path = "../monitor-api" }
secgate = { path = "../secgate" }

stable-vec = "0.4.1"

[features]
runtime = []
Expand Down
Loading

0 comments on commit 38eb2c1

Please sign in to comment.