diff --git a/kernel/src/objects/cap.rs b/kernel/src/objects/cap.rs new file mode 100644 index 0000000..19518e4 --- /dev/null +++ b/kernel/src/objects/cap.rs @@ -0,0 +1,23 @@ +use api::cap::ObjectType; + +/// RawCap is the specific implementation of capability which stores in CNode +pub struct RawCap { + /// args: in vanilla seL4 implementation, a cap use two 64-bit words to store all information, + /// but we'll waste some space rather than using bitfield to simplify the implementation + args: [usize; 2], + /// cap_type: every capability links to one specific object + cap_type: ObjectType, +} + +impl RawCap { + pub fn new(arg0: usize, arg1: usize, cap_type: ObjectType) -> Self { + Self { + args: [arg0, arg1], + cap_type, + } + } + + pub fn cap_type(&self) -> ObjectType { + self.cap_type + } +} diff --git a/kernel/src/objects/mod.rs b/kernel/src/objects/mod.rs new file mode 100644 index 0000000..951e188 --- /dev/null +++ b/kernel/src/objects/mod.rs @@ -0,0 +1,38 @@ +/* + +10 kernel object types: +• Threads (thread-control blocks: TCBs) +• Scheduling contexts (SCs) +• Address spaces (page table objects: PDs, PTs) +• Endpoints (IPC) +• Reply objects (ROs) +• Notifications +• Capability spaces (CNodes) +• Frames +• Interrupt objects (architecture specific) +• Untyped memory + +*/ + +use api::cap::ObjectType; +use cap::RawCap; +use core::cell::Cell; +use core::marker::PhantomData; + +pub mod cap; +pub mod null; +pub mod untyped; + +/// Cap is the high-level wrapper of RawCap, it's a typed reference to RawCap (which is untyped in Rust) +/// For the typed objects, we should bound it with an empty traits `KernelObject` +/// And for those objects, at least implement `mint` method and `decodeInvocation` (not enforcing in `KernelObject` for complexity) +pub struct Cap<'a, T: KernelObject> { + cap: &'a Cell, + cap_type: PhantomData, +} + +/// KernelObject is the base trait for all kernel objects +pub trait KernelObject { + // this should be optimized by compiler? + const OBJ_TYPE: ObjectType; +} diff --git a/kernel/src/objects/null.rs b/kernel/src/objects/null.rs new file mode 100644 index 0000000..c3e2cc6 --- /dev/null +++ b/kernel/src/objects/null.rs @@ -0,0 +1,24 @@ +use super::cap::RawCap; +use super::{Cap, KernelObject}; +use api::cap::ObjectType; +use core::marker::PhantomData; + +/// NullObject is used as empty (capability) slot +pub struct NullObject {} +pub type NullCap<'a> = Cap<'a, NullObject>; + +impl<'a> NullCap<'a> { + pub fn mint() -> RawCap { + let mut cap = RawCap::new(0, 0, ObjectType::Null); + cap + } + + pub fn retype(self, new: RawCap) -> Cap<'a, T> { + assert!(T::OBJ_TYPE == new.cap_type()); + self.cap.set(new); + Cap { + cap: self.cap, + cap_type: PhantomData, + } + } +}