From 7315d09fb50d2e1a39f872f8108935bcfb565df9 Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Thu, 12 May 2022 22:53:05 +0000 Subject: [PATCH] Merge "kata-slot-allocator: add a 1-slot RAII helper" GitOrigin-RevId: 5d339c11e080d99280438552d69147745122780b --- .../src/slot-allocator/Cargo.toml | 1 + .../src/slot-allocator/src/lib.rs | 4 + .../src/slot-allocator/src/one_slot.rs | 116 ++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 apps/system/components/kata-os-common/src/slot-allocator/src/one_slot.rs diff --git a/apps/system/components/kata-os-common/src/slot-allocator/Cargo.toml b/apps/system/components/kata-os-common/src/slot-allocator/Cargo.toml index b5ff1b3..ae326cc 100644 --- a/apps/system/components/kata-os-common/src/slot-allocator/Cargo.toml +++ b/apps/system/components/kata-os-common/src/slot-allocator/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" [dependencies] # TODO(sleffler): v1.0 requires rust edition2021 bitvec = { version = "0.22", default-features = false, features = ["alloc"] } +sel4-sys = { path = "../sel4-sys" } spin = "0.9" diff --git a/apps/system/components/kata-os-common/src/slot-allocator/src/lib.rs b/apps/system/components/kata-os-common/src/slot-allocator/src/lib.rs index 68bdf21..758d436 100644 --- a/apps/system/components/kata-os-common/src/slot-allocator/src/lib.rs +++ b/apps/system/components/kata-os-common/src/slot-allocator/src/lib.rs @@ -3,11 +3,15 @@ //! can be used for other purposes. #![cfg_attr(not(test), no_std)] +#![allow(non_snake_case)] use bitvec::prelude::*; use core::ops::Range; use spin::Mutex; +mod one_slot; +pub use one_slot::CSpaceSlot; + struct Slots { bits: Option>, used: usize, diff --git a/apps/system/components/kata-os-common/src/slot-allocator/src/one_slot.rs b/apps/system/components/kata-os-common/src/slot-allocator/src/one_slot.rs new file mode 100644 index 0000000..a9263cc --- /dev/null +++ b/apps/system/components/kata-os-common/src/slot-allocator/src/one_slot.rs @@ -0,0 +1,116 @@ +//! RAII wrapper for a dynamically allocated CSpace slot. + +use crate::KATA_CSPACE_SLOTS; + +use sel4_sys::seL4_CapRights; +use sel4_sys::seL4_CNode_Copy; +use sel4_sys::seL4_CNode_Delete; +use sel4_sys::seL4_CNode_Move; +use sel4_sys::seL4_CPtr; +use sel4_sys::seL4_Result; +use sel4_sys::seL4_SetCapReceivePath; +use sel4_sys::seL4_WordBits; + +extern "C" { + static SELF_CNODE: seL4_CPtr; +} + +pub struct CSpaceSlot { + pub slot: seL4_CPtr, +} +impl CSpaceSlot { + pub fn new() -> Self { + CSpaceSlot { + slot: unsafe { KATA_CSPACE_SLOTS.alloc(1) }.expect("CSpaceSlot"), + } + } + + // Release ownership of the slot; this inhibits the normal cleanup + // done by drop. + pub fn release(&mut self) { + self.slot = seL4_CPtr::MAX; + } + + // Returns the (root, index, depth) seL4 path for the slot. + pub fn get_path(&self) -> (seL4_CPtr, seL4_CPtr, u8) { + (unsafe { SELF_CNODE }, self.slot, seL4_WordBits as u8) + } + + // Sets the receive path used for receiving a capability attached + // to an seL4 IPC message. + pub fn set_recv_path(&self) { + unsafe { seL4_SetCapReceivePath(SELF_CNODE, self.slot, seL4_WordBits) }; + } + + // Copies the specified path to our slot. + pub fn copy_to( + &self, + src_root: seL4_CPtr, + src_index: seL4_CPtr, + src_depth: u8, + ) -> seL4_Result { + let seL4_AllRights = seL4_CapRights::new( + /*grant_reply=*/ 1, /*grant=*/ 1, /*read=*/ 1, /*write=*/ 1, + ); + unsafe { + seL4_CNode_Copy( + /*dest_root=*/ SELF_CNODE, + /*dest_index= */ self.slot, + /*dest_depth=*/ seL4_WordBits as u8, + src_root, + src_index, + src_depth, + seL4_AllRights, + ) + } + } + + // Moves the specified path to our slot. + pub fn move_to( + &self, + src_root: seL4_CPtr, + src_slot: seL4_CPtr, + src_depth: u8, + ) -> seL4_Result { + unsafe { + seL4_CNode_Move( + /*dest_root=*/ SELF_CNODE, + /*dest_index= */ self.slot, + /*dest_depth=*/ seL4_WordBits as u8, + src_root, + src_slot, + src_depth, + ) + } + } + + // Moves our slot to the specified path. + pub fn move_from( + &self, + dest_root: seL4_CPtr, + dest_slot: seL4_CPtr, + dest_depth: u8, + ) -> seL4_Result { + unsafe { + seL4_CNode_Move( + dest_root, + dest_slot, + dest_depth, + /*src_root=*/ SELF_CNODE, + /*src_index= */ self.slot, + /*src_depth=*/ seL4_WordBits as u8, + ) + } + } +} +impl Drop for CSpaceSlot { + fn drop(&mut self) { + if self.slot != seL4_CPtr::MAX { + unsafe { + seL4_CNode_Delete(SELF_CNODE, self.slot, seL4_WordBits as u8) + .expect("CSpaceSlot"); + KATA_CSPACE_SLOTS.free(self.slot, 1); + } + } + } +}