File size: 4,407 Bytes
119e586 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | use super::rights::Rights;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CapPtr(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum CapType {
Null = 0,
Untyped = 1,
CNode = 2,
Tcb = 3,
Endpoint = 4,
Notification = 5,
Frame = 6,
PageTable = 7,
AsidPool = 8,
IrqControl = 9,
IrqHandler = 10,
SoulCap = 11,
BifrostCap = 12,
DeviceCap = 13,
}
#[derive(Clone, Copy, Debug)]
pub struct Cap {
pub cap_type: CapType,
pub object_id: u64,
pub rights: Rights,
pub badge: u64,
pub sealed: bool,
}
impl Cap {
pub const NULL: Self = Self {
cap_type: CapType::Null,
object_id: 0,
rights: Rights::NONE,
badge: 0,
sealed: false,
};
pub const fn is_null(&self) -> bool {
matches!(self.cap_type, CapType::Null)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapError {
InvalidSlot,
SlotOccupied,
NullCap,
InsufficientRights,
AlreadySealed,
}
/// Fixed-size capability node — N slots, no heap.
pub struct CNode<const N: usize> {
slots: [Cap; N],
}
impl<const N: usize> CNode<N> {
pub const fn new() -> Self {
Self { slots: [Cap::NULL; N] }
}
pub fn get(&self, ptr: CapPtr) -> Result<&Cap, CapError> {
self.slots.get(ptr.0).ok_or(CapError::InvalidSlot)
}
fn get_mut(&mut self, ptr: CapPtr) -> Result<&mut Cap, CapError> {
self.slots.get_mut(ptr.0).ok_or(CapError::InvalidSlot)
}
pub fn insert(&mut self, ptr: CapPtr, cap: Cap) -> Result<(), CapError> {
let slot = self.get_mut(ptr)?;
if !slot.is_null() {
return Err(CapError::SlotOccupied);
}
*slot = cap;
Ok(())
}
pub fn delete(&mut self, ptr: CapPtr) -> Result<Cap, CapError> {
let slot = self.get_mut(ptr)?;
if slot.is_null() {
return Err(CapError::NullCap);
}
let cap = *slot;
*slot = Cap::NULL;
Ok(cap)
}
pub fn size(&self) -> usize { N }
}
/// Per-soul capability namespace backed by a root CNode.
pub struct CSpace<const N: usize> {
pub soul_id: u64,
root: CNode<N>,
}
impl<const N: usize> CSpace<N> {
pub const fn new(soul_id: u64) -> Self {
Self { soul_id, root: CNode::new() }
}
pub fn lookup(&self, ptr: CapPtr) -> Result<&Cap, CapError> {
self.root.get(ptr)
}
pub fn insert(&mut self, ptr: CapPtr, cap: Cap) -> Result<(), CapError> {
self.root.insert(ptr, cap)
}
pub fn delete(&mut self, ptr: CapPtr) -> Result<Cap, CapError> {
self.root.delete(ptr)
}
pub fn cnode_size(&self) -> usize { N }
}
#[cfg(test)]
mod tests {
use super::*;
fn make_cap(id: u64) -> Cap {
Cap {
cap_type: CapType::Endpoint,
object_id: id,
rights: Rights::ALL,
badge: 0,
sealed: false,
}
}
#[test]
fn insert_and_lookup() {
let mut cs: CSpace<16> = CSpace::new(1);
let cap = make_cap(42);
cs.insert(CapPtr(0), cap).unwrap();
let found = cs.lookup(CapPtr(0)).unwrap();
assert_eq!(found.object_id, 42);
}
#[test]
fn double_insert_fails() {
let mut cs: CSpace<16> = CSpace::new(1);
cs.insert(CapPtr(0), make_cap(1)).unwrap();
let err = cs.insert(CapPtr(0), make_cap(2)).unwrap_err();
assert_eq!(err, CapError::SlotOccupied);
}
#[test]
fn delete_clears_slot() {
let mut cs: CSpace<16> = CSpace::new(1);
cs.insert(CapPtr(3), make_cap(99)).unwrap();
let removed = cs.delete(CapPtr(3)).unwrap();
assert_eq!(removed.object_id, 99);
let err = cs.delete(CapPtr(3)).unwrap_err();
assert_eq!(err, CapError::NullCap);
}
#[test]
fn out_of_bounds_slot() {
let cs: CSpace<4> = CSpace::new(1);
let err = cs.lookup(CapPtr(4)).unwrap_err();
assert_eq!(err, CapError::InvalidSlot);
}
#[test]
fn null_cap_is_null() {
assert!(Cap::NULL.is_null());
}
}
|