Skip to content

Commit

Permalink
kernel: refactor port tokens to be endpoint-centric
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Jan 30, 2025
1 parent ef889bd commit e7bc124
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 135 deletions.
16 changes: 6 additions & 10 deletions oro-kernel/src/iface/kernel/mem_token_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,15 @@ impl KernelInterface for MemTokenV0 {
_ => InterfaceResponse::immediate(SysError::BadKey, 0),
}
}
Token::SlotMap(token, side) => {
// SAFETY(qix-): Ensure that the `usize` fits within a `u64`,
// SAFETY(qix-): otherwise the below `as` casts will truncate.
::oro_macro::assert::fits_within::<usize, u64>();

Token::PortEndpoint(token) => {
match key {
key!("type") => InterfaceResponse::ok(t.type_id()),
key!("subtype") => InterfaceResponse::ok(*side as u64),
key!("subtype") => InterfaceResponse::ok(token.side() as u64),
key!("forget") => InterfaceResponse::immediate(SysError::WriteOnly, 0),
key!("pagesize") => InterfaceResponse::ok(token.page_size() as u64),
key!("pages") => InterfaceResponse::ok(token.page_count() as u64),
key!("size") => InterfaceResponse::ok(token.size() as u64),
key!("commit") => InterfaceResponse::ok(token.commit() as u64),
key!("pagesize") => InterfaceResponse::ok(4096),
key!("pages") => InterfaceResponse::ok(1),
key!("size") => InterfaceResponse::ok(4096),
key!("commit") => InterfaceResponse::ok(1),
key!("base") => InterfaceResponse::immediate(SysError::WriteOnly, 0),
_ => InterfaceResponse::immediate(SysError::BadKey, 0),
}
Expand Down
65 changes: 46 additions & 19 deletions oro-kernel/src/iface/root_ring/test_ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ use core::marker::PhantomData;
use oro::{key, syscall::Error as SysError};

use crate::{
arch::Arch, interface::Interface, port::Port, syscall::InterfaceResponse, tab::Tab,
arch::Arch,
interface::Interface,
port::{PortEnd, PortState},
syscall::InterfaceResponse,
tab::Tab,
thread::Thread,
};

/// Interface specific errors
#[repr(u64)]
pub enum Error {
/// The endpoint is already claimed.
Claimed = key!("claimed"),
/// The system is out of memory.
OutOfMemory = key!("oom"),
}

/// Temporary interface for testing ports.
///
/// # Do not use!
/// Do not use this interface. It's just here to test ports.
pub struct RootTestPorts<A: Arch>(Tab<Port>, PhantomData<A>);
pub struct RootTestPorts<A: Arch>(Tab<PortState>, PhantomData<A>);

impl<A: Arch> RootTestPorts<A> {
/// Creates a new `RootTestPorts` instance.
Expand All @@ -26,12 +39,7 @@ impl<A: Arch> RootTestPorts<A> {
/// Panics if the system is out of memory.
#[must_use]
pub fn new() -> Self {
Self(
Port::new()
.and_then(|p| crate::tab::get().add(p))
.expect("out of memory"),
PhantomData,
)
Self(PortState::new().expect("out of memory"), PhantomData)
}
}

Expand All @@ -47,17 +55,36 @@ impl<A: Arch> Interface<A> for RootTestPorts<A> {

match key {
key!("health") => InterfaceResponse::ok(1337),
key!("prodtkn") => {
// Mark it owned by the current thread's instead.
let tkn = self.0.with(|p| p.producer());
thread.with_mut(|t| t.insert_token(tkn.clone()));
InterfaceResponse::ok(tkn.id())
}
key!("cnsmtkn") => {
// Mark it owned by the current thread's instead.
let tkn = self.0.with(|p| p.consumer());
thread.with_mut(|t| t.insert_token(tkn.clone()));
InterfaceResponse::ok(tkn.id())
key!("prodtkn") | key!("cnsmtkn") => {
match PortState::endpoint(
&self.0,
if key == key!("prodtkn") {
PortEnd::Producer
} else {
PortEnd::Consumer
},
)
.map(|r| {
r.map_err(|_| {
InterfaceResponse::immediate(
SysError::InterfaceError,
Error::Claimed as u64,
)
})
})
.ok_or(InterfaceResponse::immediate(
SysError::InterfaceError,
Error::OutOfMemory as u64,
))
.flatten()
.map(|tkn| {
let id = tkn.id();
thread.with_mut(|t| t.insert_token(tkn));
InterfaceResponse::ok(id)
}) {
Ok(r) => r,
Err(e) => e,
}
}
_ => InterfaceResponse::immediate(SysError::BadKey, 0),
}
Expand Down
3 changes: 3 additions & 0 deletions oro-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
// SAFETY(qix-): has equally good codegen. Either, way this is zero risk.
// SAFETY(qix-): https://github.com/rust-lang/rust/issues/90850
#![feature(downcast_unchecked)]
// SAFETY(qix-): This is almost stabilized.
// SAFETY(qix-): https://github.com/rust-lang/rust/issues/70142
#![feature(result_flattening)]

pub mod arch;
pub mod iface;
Expand Down
Loading

0 comments on commit e7bc124

Please sign in to comment.