//! One pending receive per coordinator, with an opaque generation ticket. //! Kernel adapters retain process memory and perform the actual suspension. use super::udp_binding::{Bindings,Handle,Principal}; use super::udp_buffering::Queue; #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(u64)] pub enum Error{Binding=1,Foreign=2,State=3,Pending=4,Ready=5,Buffer=6,Deadline=7,Clock=8,Exhausted=9,Stale=10} #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(u64)] pub enum Outcome{Ready=1,Timeout=2,Closed=3} #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Ticket{identity:u64,generation:u64} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Buffer{address:u64,capacity:u64} impl Buffer{pub fn checked(address:u64,capacity:usize)->Result{if address==0||capacity>1472||address.checked_add(capacity as u64).is_none(){return Err(Error::Buffer);}Ok(Self{address,capacity:capacity as u64})}pub fn address(self)->u64{self.address}pub fn capacity(self)->usize{self.capacity as usize}} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Completion{pub outcome:Outcome,pub buffer:Buffer,pub ticket:Ticket} #[derive(Clone,Copy,Debug,PartialEq,Eq)] #[repr(C)] pub struct Snapshot{pub identity:u64,pub principal:Principal,pub handle:Handle,pub phase:u64,pub generation:u64,pub address:u64,pub capacity:u64,pub deadline:u64,pub last_now:u64,pub outcome:u64,pub completed:u64,pub returned:u64} #[repr(C)] pub struct Waiter{state:Snapshot} #[unsafe(no_mangle)]pub static mut OS_K1880_NONCE_MASK:u64=0; fn identity()->Result{use core::ptr::{read_volatile,write_volatile};static mut NEXT:u64=1;let cpu:u64;let daif:u64;unsafe{core::arch::asm!("mrs {cpu}, MPIDR_EL1","mrs {daif}, DAIF",cpu=out(reg)cpu,daif=out(reg)daif,options(nomem,nostack,preserves_flags));}if cpu&0xff!=0||daif&0x3c0!=0x3c0{return Err(Error::State);}unsafe{let value=read_volatile(core::ptr::addr_of!(NEXT));let next=value.checked_add(1).ok_or(Error::Exhausted)?;write_volatile(core::ptr::addr_of_mut!(NEXT),next);write_volatile(core::ptr::addr_of_mut!(OS_K1880_NONCE_MASK),daif&0x3c0);Ok(value)}} impl Waiter{ pub fn new(table:&Bindings,principal:Principal,handle:Handle)->Result{table.lookup(principal,handle).map_err(|_|Error::Binding)?;Ok(Self{state:Snapshot{identity:identity()?,principal,handle,phase:0,generation:0,address:0,capacity:0,deadline:0,last_now:0,outcome:0,completed:0,returned:0}})} pub fn snapshot(&self)->Snapshot{self.state} fn owner(&self,p:Principal)->Result<(),Error>{if p!=self.state.principal{return Err(Error::Foreign);}Ok(())} fn ticket(&self,t:Ticket)->Result<(),Error>{if t.identity!=self.state.identity{return Err(Error::Foreign);}if t.generation!=self.state.generation{return Err(Error::Stale);}Ok(())} fn clock(&self,now:u64)->Result<(),Error>{if now(&self,t:&Bindings)->Result<(),Error>{t.lookup(self.state.principal,self.state.handle).map_err(|_|Error::Binding)?;Ok(())} fn queue(&self,q:&Queue)->Result{let s=q.snapshot();if s.principal!=self.state.principal||s.handle!=self.state.handle{return Err(Error::Foreign);}Ok(s.len)} pub fn arm(&mut self,table:&Bindings,queue:&Queue,principal:Principal,buffer:Buffer,now:u64,deadline:u64)->Result{ self.owner(principal)?;self.live(table)?;if self.state.phase!=0{return Err(Error::State);}self.clock(now)?;if deadline<=now{return Err(Error::Deadline);}if self.queue(queue)?!=0{return Err(Error::Ready);}let generation=self.state.generation.checked_add(1).ok_or(Error::Exhausted)?; self.state.phase=1;self.state.generation=generation;self.state.address=buffer.address;self.state.capacity=buffer.capacity;self.state.deadline=deadline;self.state.last_now=now;self.state.outcome=0;Ok(Ticket{identity:self.state.identity,generation}) } fn finish(&mut self,outcome:Outcome,now:u64)->Result<(),Error>{let completed=self.state.completed.checked_add(1).ok_or(Error::Exhausted)?;self.state.phase=2;self.state.outcome=outcome as u64;self.state.last_now=now;self.state.completed=completed;Ok(())} pub fn expire(&mut self,table:&Bindings,principal:Principal,ticket:Ticket,now:u64)->Result<(),Error>{self.owner(principal)?;self.ticket(ticket)?;if self.state.phase!=1{return Err(Error::State);}self.live(table)?;self.clock(now)?;if now(&mut self,table:&Bindings,queue:&Queue,principal:Principal,ticket:Ticket,source:Handle,now:u64)->Result<(),Error>{self.owner(principal)?;self.ticket(ticket)?;if source!=self.state.handle{return Err(Error::Stale);}if self.state.phase!=1{return Err(Error::State);}self.live(table)?;self.clock(now)?;if self.queue(queue)?==0{return Err(Error::Pending);}self.finish(Outcome::Ready,now)} /// Closing a binding still succeeds after a timeout/readiness winner. Only /// an unresolved wait receives Closed; an existing completion is preserved. pub fn close(&mut self,table:&mut Bindings,principal:Principal,now:u64)->Result{self.owner(principal)?;self.live(table)?;self.clock(now)?;let pending=self.state.phase==1;if pending{self.state.completed.checked_add(1).ok_or(Error::Exhausted)?;}table.close(principal,self.state.handle).map_err(|_|Error::Binding)?;if pending{self.finish(Outcome::Closed,now)?;}else{self.state.last_now=now;}Ok(pending)} pub fn take(&mut self,principal:Principal,ticket:Ticket)->Result{self.owner(principal)?;self.ticket(ticket)?;if self.state.phase!=2{return Err(Error::State);}let returned=self.state.returned.checked_add(1).ok_or(Error::Exhausted)?;let outcome=match self.state.outcome{1=>Outcome::Ready,2=>Outcome::Timeout,3=>Outcome::Closed,_=>return Err(Error::State)};let completion=Completion{outcome,buffer:Buffer{address:self.state.address,capacity:self.state.capacity},ticket};self.state.phase=0;self.state.address=0;self.state.capacity=0;self.state.deadline=0;self.state.outcome=0;self.state.returned=returned;Ok(completion)} } impl Waiter{pub const fn os_k1880_layout()->[u64;15]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Snapshot,identity)as u64,offset_of!(Snapshot,principal)as u64,offset_of!(Snapshot,handle)as u64,offset_of!(Snapshot,phase)as u64,offset_of!(Snapshot,generation)as u64,offset_of!(Snapshot,address)as u64,offset_of!(Snapshot,capacity)as u64,offset_of!(Snapshot,deadline)as u64,offset_of!(Snapshot,last_now)as u64,offset_of!(Snapshot,outcome)as u64,offset_of!(Snapshot,completed)as u64,offset_of!(Snapshot,returned)as u64,size_of::()as u64,size_of::()as u64]}}