From ae321a99235d8a9a4dbd317f4178c294b1128998 Mon Sep 17 00:00:00 2001 From: able Date: Mon, 10 Jul 2023 22:54:05 -0500 Subject: [PATCH] CAPS: Adding in capabilities --- kernel/src/capabilities.rs | 177 +++++++++++++++++++++++++++++++++++++ kernel/src/handle.rs | 1 + kernel/src/lib.rs | 1 + 3 files changed, 179 insertions(+) create mode 100644 kernel/src/capabilities.rs diff --git a/kernel/src/capabilities.rs b/kernel/src/capabilities.rs new file mode 100644 index 000000000..ed95595d9 --- /dev/null +++ b/kernel/src/capabilities.rs @@ -0,0 +1,177 @@ +//! AbleOS capability tree implementation + +use { + alloc::{ + format, + string::{String, ToString}, + vec, + }, + core::fmt, +}; + +// Seperate +use alloc::vec::Vec; + +struct Argument { + name: String, + type_: String, +} + +struct Function { + name: String, + args: Vec, + ret: Option, +} + +struct Capability { + name: String, + functions: Vec, + sub_capabilities: Vec, +} + +impl Capability { + fn new(name: String) -> Capability { + Capability { + name, + functions: Vec::new(), + sub_capabilities: Vec::new(), + } + } +} + +impl fmt::Display for Capability { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Capability: {}\n", self.name)?; + for function in &self.functions { + write!(f, " Function: {}\n", function.name)?; + for arg in &function.args { + write!(f, " Argument: {} (Type: {})\n", arg.name, arg.type_)?; + } + if let Some(ret_type) = &function.ret { + write!(f, " Return Type: {}\n", ret_type)?; + } else { + write!(f, " Return Type: None\n")?; + } + } + for sub_capability in &self.sub_capabilities { + write!(f, "{}", sub_capability.to_string_with_indentation(2))?; + } + Ok(()) + } +} + +impl Capability { + fn to_string_with_indentation(&self, level: usize) -> String { + let indent = " ".repeat(level); + let mut result = format!("{}Capability: {}\n", indent, self.name); + for function in &self.functions { + result.push_str(&format!("{} Function: {}\n", indent, function.name)); + for arg in &function.args { + result.push_str(&format!( + "{} Argument: {} (Type: {})\n", + indent, arg.name, arg.type_ + )); + } + if let Some(ret_type) = &function.ret { + result.push_str(&format!("{} Return Type: {}\n", indent, ret_type)); + } else { + result.push_str(&format!("{} Return Type: None\n", indent)); + } + } + for sub_capability in &self.sub_capabilities { + result.push_str(&sub_capability.to_string_with_indentation(level + 1)); + } + result + } +} + +struct CapabilityTree { + capabilities: Vec, +} + +impl CapabilityTree { + fn new() -> CapabilityTree { + CapabilityTree { + capabilities: Vec::new(), + } + } +} + +fn example() { + let mut capability_tree = Capability::new("VFS".to_string()); + + let mut file_management_capability = Capability::new("File Management".to_string()); + file_management_capability.functions.push(Function { + name: "OpenFile".to_string(), + args: vec![ + Argument { + name: "path".to_string(), + type_: "String".to_string(), + }, + Argument { + name: "mode".to_string(), + type_: "String".to_string(), + }, + ], + ret: Some("FileHandle".to_string()), + }); + file_management_capability.functions.push(Function { + name: "ReadFile".to_string(), + args: vec![ + Argument { + name: "file".to_string(), + type_: "FileHandle".to_string(), + }, + Argument { + name: "buffer".to_string(), + type_: "&mut [u8]".to_string(), + }, + Argument { + name: "length".to_string(), + type_: "usize".to_string(), + }, + ], + ret: Some("usize".to_string()), + }); + file_management_capability.functions.push(Function { + name: "WriteFile".to_string(), + args: vec![ + Argument { + name: "file".to_string(), + type_: "FileHandle".to_string(), + }, + Argument { + name: "buffer".to_string(), + type_: "&[u8]".to_string(), + }, + ], + ret: None, + }); + + let mut directory_management_capability = Capability::new("Directory Management".to_string()); + directory_management_capability.functions.push(Function { + name: "CreateDirectory".to_string(), + args: vec![Argument { + name: "path".to_string(), + type_: "String".to_string(), + }], + ret: Some("bool".to_string()), + }); + directory_management_capability.functions.push(Function { + name: "ListDirectory".to_string(), + args: vec![Argument { + name: "path".to_string(), + type_: "String".to_string(), + }], + ret: Some("Vec".to_string()), + }); + + capability_tree + .sub_capabilities + .push(file_management_capability); + capability_tree + .sub_capabilities + .push(directory_management_capability); + + log::debug!("{}", capability_tree); +} diff --git a/kernel/src/handle.rs b/kernel/src/handle.rs index 9cb34c87e..cc76284da 100644 --- a/kernel/src/handle.rs +++ b/kernel/src/handle.rs @@ -26,6 +26,7 @@ impl OSHandle { #[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)] pub struct Handle { id: OSHandle, + // TODO: Update this to be indexes into the caps perms: Permissions, } diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 4aeb67c93..570bb5bdc 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -22,6 +22,7 @@ extern crate alloc; mod allocator; mod arch; mod bootmodules; +pub mod capabilities; pub mod device_tree; pub mod handle; pub mod host;