[TSM.ID].[11031972] bare-metal dalam subdirektori

This commit is contained in:
TSM.ID
2026-05-25 03:56:22 +07:00
parent 5665621227
commit 0debb53b1d
6 changed files with 0 additions and 0 deletions
@@ -0,0 +1,11 @@
# [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential.
[package]
name = "xcu-ebpf-loader"
version = "0.1.0"
edition = "2021"
authors = ["TSM.ID <tsm@tsm.id>"]
description = "[TSM.ID].[11031972] eBPF Program Loader"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
@@ -0,0 +1,82 @@
//! [TSM.ID].[11031972] -- Platform X Ecosystem
//! xcu-ebpf-loader -- Cross-platform eBPF program loader
#![deny(warnings)]
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub enum LoaderError {
FileNotFound(String),
ParseFailed(String),
ValidationFailed(String),
LoadFailed(String),
}
impl std::fmt::Display for LoaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FileNotFound(e) => write!(f, "File not found: {e}"),
Self::ParseFailed(e) => write!(f, "Parse failed: {e}"),
Self::ValidationFailed(e) => write!(f, "Validation: {e}"),
Self::LoadFailed(e) => write!(f, "Load failed: {e}"),
}
}
}
impl std::error::Error for LoaderError {}
pub type Result<T> = std::result::Result<T, LoaderError>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProgramSpec {
pub name: String,
pub prog_type: String,
pub bytecode_path: String,
pub maps: Vec<String>,
}
pub struct ProgramLoader {
loaded: Arc<Mutex<HashMap<String, ProgramSpec>>>,
verified: Arc<Mutex<Vec<String>>>,
}
impl ProgramLoader {
pub fn new() -> Self {
Self { loaded: Arc::new(Mutex::new(HashMap::new())), verified: Arc::new(Mutex::new(Vec::new())) }
}
pub fn load_spec(&self, spec: ProgramSpec) -> Result<()> {
let mut loaded = self.loaded.lock().map_err(|e| LoaderError::LoadFailed(e.to_string()))?;
loaded.insert(spec.name.clone(), spec);
Ok(())
}
pub fn verify(&self, name: &str) -> Result<bool> {
let loaded = self.loaded.lock().map_err(|e| LoaderError::LoadFailed(e.to_string()))?;
if !loaded.contains_key(name) { return Err(LoaderError::FileNotFound(name.into())); }
let mut verified = self.verified.lock().map_err(|e| LoaderError::LoadFailed(e.to_string()))?;
verified.push(name.to_string());
Ok(true)
}
pub fn loaded_count(&self) -> usize { self.loaded.lock().map(|l| l.len()).unwrap_or(0) }
}
impl Default for ProgramLoader {
fn default() -> Self { Self::new() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_loader() {
let loader = ProgramLoader::new();
loader.load_spec(ProgramSpec {
name: "test".into(), prog_type: "xdp".into(),
bytecode_path: "/dev/null".into(), maps: vec![],
}).unwrap();
assert_eq!(loader.loaded_count(), 1);
assert!(loader.verify("test").unwrap());
}
}
+11
View File
@@ -0,0 +1,11 @@
# [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential.
[package]
name = "xcu-ebpf"
version = "0.1.0"
edition = "2021"
authors = ["TSM.ID <tsm@tsm.id>"]
description = "[TSM.ID].[11031972] eBPF Abstraction Layer"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+92
View File
@@ -0,0 +1,92 @@
//! [TSM.ID].[11031972] -- Platform X Ecosystem
//! xcu-ebpf -- eBPF Abstraction Layer (cross-platform)
#![deny(warnings)]
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub enum EbpfError {
ProgramLoadFailed(String),
MapAccessFailed(String),
NotSupported(String),
AttachFailed(String),
}
impl std::fmt::Display for EbpfError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ProgramLoadFailed(e) => write!(f, "eBPF load failed: {e}"),
Self::MapAccessFailed(e) => write!(f, "Map access: {e}"),
Self::NotSupported(e) => write!(f, "Not supported: {e}"),
Self::AttachFailed(e) => write!(f, "Attach failed: {e}"),
}
}
}
impl std::error::Error for EbpfError {}
pub type Result<T> = std::result::Result<T, EbpfError>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EbpfProgram {
pub name: String,
pub prog_type: String,
pub bytecode: Vec<u8>,
pub attach_point: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EbpfState { Unloaded, Loaded, Attached, Detached, Error(String) }
pub struct EbpfManager {
programs: Arc<Mutex<HashMap<String, (EbpfProgram, EbpfState)>>>,
supported: bool,
}
impl EbpfManager {
pub fn new() -> Self {
let supported = cfg!(target_os = "linux");
Self { programs: Arc::new(Mutex::new(HashMap::new())), supported }
}
pub fn is_supported(&self) -> bool { self.supported }
pub fn load(&self, program: EbpfProgram) -> Result<()> {
if !self.supported { return Err(EbpfError::NotSupported("eBPF requires Linux".into())); }
let mut progs = self.programs.lock().map_err(|e| EbpfError::ProgramLoadFailed(e.to_string()))?;
progs.insert(program.name.clone(), (program, EbpfState::Loaded));
Ok(())
}
pub fn attach(&self, name: &str) -> Result<()> {
let mut progs = self.programs.lock().map_err(|e| EbpfError::AttachFailed(e.to_string()))?;
match progs.get_mut(name) {
Some((_, state)) => { *state = EbpfState::Attached; Ok(()) }
None => Err(EbpfError::AttachFailed(format!("Program {name} not found"))),
}
}
pub fn detach(&self, name: &str) -> Result<()> {
let mut progs = self.programs.lock().map_err(|e| EbpfError::AttachFailed(e.to_string()))?;
match progs.get_mut(name) {
Some((_, state)) => { *state = EbpfState::Detached; Ok(()) }
None => Err(EbpfError::AttachFailed(format!("Program {name} not found"))),
}
}
pub fn program_count(&self) -> usize { self.programs.lock().map(|p| p.len()).unwrap_or(0) }
}
impl Default for EbpfManager {
fn default() -> Self { Self::new() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ebpf_manager() {
let mgr = EbpfManager::new();
assert_eq!(mgr.program_count(), 0);
}
}
@@ -0,0 +1,11 @@
# [TSM.ID].[11031972] -- All Rights Reserved. Proprietary & Confidential.
[package]
name = "xcu-omega"
version = "0.1.0"
edition = "2021"
authors = ["TSM.ID <tsm@tsm.id>"]
description = "[TSM.ID].[11031972] Phase 40: The Omega Unikernel Runtime"
[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
+111
View File
@@ -0,0 +1,111 @@
//! [TSM.ID].[11031972] -- Platform X Ecosystem
//! xcu-omega -- Omega Unikernel Runtime Abstraction Layer
//! Provides bare-metal abstractions that compile on all platforms
#![deny(warnings)]
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[derive(Debug)]
pub enum OmegaError {
BootFailed(String),
HardwareFault(String),
MemoryViolation(String),
SchedulerFailed(String),
}
impl std::fmt::Display for OmegaError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BootFailed(e) => write!(f, "Boot failed: {e}"),
Self::HardwareFault(e) => write!(f, "HW fault: {e}"),
Self::MemoryViolation(e) => write!(f, "Memory violation: {e}"),
Self::SchedulerFailed(e) => write!(f, "Scheduler: {e}"),
}
}
}
impl std::error::Error for OmegaError {}
pub type Result<T> = std::result::Result<T, OmegaError>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelConfig {
pub stack_size: usize,
pub heap_size: usize,
pub tick_hz: u32,
pub max_tasks: usize,
pub params: HashMap<String, String>,
}
impl Default for KernelConfig {
fn default() -> Self {
Self { stack_size: 8192, heap_size: 1_048_576, tick_hz: 1000, max_tasks: 64, params: HashMap::new() }
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum KernelState { Boot, Init, Running, Idle, Panic, Shutdown }
pub struct MicroKernel {
config: KernelConfig,
state: Arc<Mutex<KernelState>>,
uptime_ms: Arc<Mutex<u64>>,
task_count: Arc<Mutex<usize>>,
}
impl MicroKernel {
pub fn new(config: KernelConfig) -> Result<Self> {
Ok(Self {
config, state: Arc::new(Mutex::new(KernelState::Boot)),
uptime_ms: Arc::new(Mutex::new(0)),
task_count: Arc::new(Mutex::new(0)),
})
}
pub fn boot(&self) -> Result<()> {
let mut s = self.state.lock().map_err(|e| OmegaError::BootFailed(e.to_string()))?;
*s = KernelState::Init;
*s = KernelState::Running;
Ok(())
}
pub fn shutdown(&self) -> Result<()> {
let mut s = self.state.lock().map_err(|e| OmegaError::SchedulerFailed(e.to_string()))?;
*s = KernelState::Shutdown;
Ok(())
}
pub fn state(&self) -> Result<KernelState> {
Ok(self.state.lock().map_err(|e| OmegaError::SchedulerFailed(e.to_string()))?.clone())
}
pub fn tick(&self) -> Result<u64> {
let mut t = self.uptime_ms.lock().map_err(|e| OmegaError::SchedulerFailed(e.to_string()))?;
*t += 1000 / self.config.tick_hz as u64;
Ok(*t)
}
pub fn spawn_task(&self) -> Result<usize> {
let mut c = self.task_count.lock().map_err(|e| OmegaError::SchedulerFailed(e.to_string()))?;
if *c >= self.config.max_tasks { return Err(OmegaError::SchedulerFailed("max tasks reached".into())); }
*c += 1;
Ok(*c)
}
pub fn config(&self) -> &KernelConfig { &self.config }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_kernel() {
let k = MicroKernel::new(KernelConfig::default()).unwrap();
k.boot().unwrap();
assert_eq!(k.state().unwrap(), KernelState::Running);
k.tick().unwrap();
k.spawn_task().unwrap();
k.shutdown().unwrap();
assert_eq!(k.state().unwrap(), KernelState::Shutdown);
}
}