mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: add initial caps
This commit is contained in:
parent
f11e3781d3
commit
ee6166c657
23
kernel/src/objects/cap.rs
Normal file
23
kernel/src/objects/cap.rs
Normal file
@ -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
|
||||
}
|
||||
}
|
38
kernel/src/objects/mod.rs
Normal file
38
kernel/src/objects/mod.rs
Normal file
@ -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<RawCap>,
|
||||
cap_type: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// KernelObject is the base trait for all kernel objects
|
||||
pub trait KernelObject {
|
||||
// this should be optimized by compiler?
|
||||
const OBJ_TYPE: ObjectType;
|
||||
}
|
24
kernel/src/objects/null.rs
Normal file
24
kernel/src/objects/null.rs
Normal file
@ -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<T: KernelObject>(self, new: RawCap) -> Cap<'a, T> {
|
||||
assert!(T::OBJ_TYPE == new.cap_type());
|
||||
self.cap.set(new);
|
||||
Cap {
|
||||
cap: self.cap,
|
||||
cap_type: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user