//! Admission of measured combined workloads. This module supplies no service //! implementation and cannot turn a host model into evidence of guest work. pub const SERVICES: usize = 6; pub const MIN_SECONDS: u64 = 7_200; #[repr(u64)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Configuration=1, Context, Clock, Identity, Resources, Stalled, Counters, StaleWake, Pending, Closed, Exhausted } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Binding { pub issuer:u64, pub epoch:u64, pub generation:u64, pub cpu:u64 } /// Completed process, IPC, VFS, timer, shell and network operations, in that /// order. Incrementing these numbers is not itself an implementation. #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct CpuProgress { pub binding:Binding, pub completed:[u64;SERVICES] } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Resources { pub pages:u64, pub owners:u64, pub guarded_stacks:u64 } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Observation { pub counter:u64, pub cpus:[CpuProgress;4], pub resources:Resources, pub canary_failures:u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ProcessIdentity { pub table:u64, pub slot:u64, pub generation:u64 } /// A rejection observed by the adapter while the actual process table and /// pending wake owners are exclusively held. The original full identity must /// be submitted to the real wake path; merely comparing two numbers is invalid. pub struct RejectedWake { old:ProcessIdentity, current:ProcessIdentity, counter:u64, resources:Resources, unchanged:bool, } impl RejectedWake { /// # Safety /// The actual wake operation returned Stale for `old`, the same table slot /// contains `current`, and `unchanged` compares complete owner state before /// and after refusal. `resources` is read from those actual owners. Counter /// is a real observation from the same monotonic guest clock as the run. pub unsafe fn from_kernel(old:ProcessIdentity,current:ProcessIdentity, counter:u64,resources:Resources,unchanged:bool)->Self { Self{old,current,counter,resources,unchanged} } } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Execution { pub cpu:u64, pub root:u64, pub masked:bool, pub counter:u64 } /// # Safety /// Reads the executing CPU, active root, interrupt mask and monotonic counter; /// it cannot return a caller supplied execution identity or virtual elapsed time. pub unsafe trait Context { fn execution(&self)->Execution; } #[repr(C)] #[derive(Debug, PartialEq, Eq)] pub struct Monitor { root:u64, frequency:u64, cpu_count:u64, started:u64, previous:Observation, windows:u64, stale_refusals:u64, last_stale:u64, closed:u64, } impl Monitor { /// # Safety /// All online bindings must be pinned for the whole run. Counters in `first` /// come from real service owners, resources from the central ledger, and /// canaries from the guarded execution stacks. No resource is owned by this /// monitor; its result never authorizes freeing or revoking resources. pub unsafe fn bind(root:u64,frequency:u64,cpu_count:u64, first:Observation)->Result { if root==0||root&4095!=0||frequency==0||frequency>1_000_000_000 ||![1,2,4].contains(&cpu_count)||first.counter==0 { return Err(Error::Configuration) } let duration=frequency.checked_mul(MIN_SECONDS).ok_or(Error::Exhausted)?; first.counter.checked_add(duration).ok_or(Error::Exhausted)?; if first.canary_failures!=0||first.resources.pages==0 ||first.resources.owners==0||first.resources.guarded_stacks(&self,context:&C)->Result { let e=context.execution(); if e.cpu!=0||e.root!=self.root||!e.masked {return Err(Error::Context)} if self.closed!=0 {return Err(Error::Closed)} if e.counter(&mut self,next:Observation,context:&C) ->Result<(),Error> { let e=self.gate(context)?; if next.counter<=self.previous.counter||next.counter>e.counter { return Err(Error::Clock) } if next.canary_failures!=0||next.resources!=self.previous.resources { return Err(Error::Resources) } for (i,(next,old)) in next.cpus.iter().zip(&self.previous.cpus).enumerate() { if next.binding!=old.binding {return Err(Error::Identity)} if i>=self.cpu_count as usize { if *next!=EMPTY_CPU {return Err(Error::Identity)} continue } for (a,b) in next.completed.iter().zip(old.completed) { if *a(&mut self,wake:RejectedWake,context:&C) ->Result<(),(Error,RejectedWake)> { let check=(||{ let e=self.gate(context)?; if wake.old.table==0||wake.old.table!=wake.current.table ||wake.old.slot!=wake.current.slot||wake.old.generation==0 ||wake.old.generation>=wake.current.generation||!wake.unchanged ||wake.resources!=self.previous.resources {return Err(Error::StaleWake)} if wake.countere.counter {return Err(Error::Clock)} self.stale_refusals.checked_add(1).ok_or(Error::Exhausted) })(); match check {Ok(count)=>{self.stale_refusals=count;self.last_stale=wake.counter;Ok(())}, Err(error)=>Err((error,wake))} } pub fn close(&mut self,context:&C)->Result<(),Error> { self.gate(context)?; let deadline=self.started+self.frequency*MIN_SECONDS; if self.previous.counter=self.previous.counter { return Err(Error::Pending) } self.closed=1;Ok(()) } pub fn windows(&self)->u64 {self.windows} pub fn stale_refusals(&self)->u64 {self.stale_refusals} pub fn closed(&self)->bool {self.closed!=0} } pub const EMPTY_CPU:CpuProgress=CpuProgress{ binding:Binding{issuer:0,epoch:0,generation:0,cpu:0},completed:[0;SERVICES]};