//! A response gate, never an owner of a secondary's memory or allocator leases. //! A deadline reports degradation. It cannot manufacture a response or authorize //! resource recycling. Normal teardown still requires the existing CPU_OFF path. #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, Context, Clock, Stale, Sequence, Pending, Closed } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Binding { pub issuer:u64,pub epoch:u64,pub generation:u64,pub cpu:u64 } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Execution { pub cpu:u64,pub root:u64,pub masked:bool,pub counter:u64 } /// Actual execution context and monotonic architectural counter. pub unsafe trait Context { fn execution(&self)->Execution; } #[repr(C)] #[derive(Debug)] pub struct Request { binding:Binding,issuer:u64,root:u64,sequence:u64,issued:u64,deadline:u64 } #[repr(C)] #[derive(Debug)] pub struct Receipt { request:Request,completed:u64 } #[repr(C)] #[derive(Debug)] pub struct Receiver { binding:Binding,issuer:u64,root:u64,completed:u64 } #[repr(C)] pub struct Supervisor { root:u64,issuer:u64,bindings:[Binding;3],sequence:u64,issued:u64,deadline:u64,last_poll:u64,acknowledged:u64,timed_out:u64,closed:u64 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub acknowledged:u64,pub unresponsive:u64,pub timed_out:u64,pub closed:u64 } impl Supervisor { /// Caller pins all three actual online generations and retains every resource /// until this response gate closes AND the ordinary owner/CPU_OFF teardown runs. pub unsafe fn bind(root:u64,issuer:u64,bindings:[Binding;3],issued:u64,duration:u64)->Result<(Self,[Receiver;3],[Request;3]),Error>{ let deadline=issued.checked_add(duration).ok_or(Error::Clock)?; if root==0||root&4095!=0||root>>48!=0||issuer==0||issued==0||duration==0||bindings.iter().enumerate().any(|(i,b)|b.cpu!=i as u64+1||b.issuer==0||b.epoch==0||b.generation==0||b.issuer!=bindings[0].issuer||b.epoch!=bindings[0].epoch){return Err(Error::Configuration)} Ok((Self{root,issuer,bindings,sequence:1,issued,deadline,last_poll:issued,acknowledged:0,timed_out:0,closed:0},core::array::from_fn(|i|Receiver{binding:bindings[i],issuer,root,completed:0}),core::array::from_fn(|i|Request{binding:bindings[i],issuer,root,sequence:1,issued,deadline}))) } pub fn status(&self)->Status { Status{acknowledged:self.acknowledged,unresponsive:if self.last_poll>=self.deadline{7^self.acknowledged}else{0},timed_out:self.timed_out,closed:self.closed} } pub fn poll(&mut self,now:u64)->Result{ if self.closed!=0{return Err(Error::Closed)}if now=self.deadline{self.timed_out|=7^self.acknowledged}Ok(self.status()) } pub fn acknowledge(&mut self,receipt:Receipt)->Result<(),(Error,Receipt)>{ let r=&receipt.request; let fail=if self.closed!=0{Some(Error::Closed)}else if r.binding.cpu==0||r.binding.cpu>3||r.issuer!=self.issuer||r.root!=self.root{Some(Error::Stale)}else{let i=r.binding.cpu as usize-1; if r.binding!=self.bindings[i]{Some(Error::Stale)}else if r.sequence!=self.sequence||r.issued!=self.issued||r.deadline!=self.deadline||self.acknowledged&(1<=self.deadline{self.timed_out|=bit}Ok(()) } /// Closing permits only entry into normal owner teardown. No frame, stack, /// queue, online token or memory-free authority is returned by this operation. pub fn close(&mut self)->Result{if self.closed!=0{return Err(Error::Closed)}if self.acknowledged!=7{return Err(Error::Pending)}self.closed=1;Ok(self.status())} } impl Receiver { pub fn completed(&self)->u64 { self.completed } pub fn complete(&mut self,request:Request,context:&impl Context)->Result{ let e=context.execution();let fail=if e.cpu!=self.binding.cpu||e.root!=self.root||!e.masked{Some(Error::Context)}else if request.binding!=self.binding||request.issuer!=self.issuer||request.root!=self.root{Some(Error::Stale)}else if self.completed.checked_add(1)!=Some(request.sequence){Some(Error::Sequence)}else if e.counter