#[repr(C)]#[derive(Clone,Copy,Debug,Default,PartialEq,Eq)] pub struct Owner{pub table:u64,pub slot:u64,pub generation:u64} #[repr(C)]#[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Session{registry:u64,slot:u64,generation:u64,user:u64} impl Session{pub fn generation(self)->u64{self.generation}pub fn user(self)->u64{self.user}pub fn slot(self)->u64{self.slot}} #[repr(C)]#[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct ReadGrant{session:Session,object:u64} #[repr(C)]#[derive(Clone,Copy,Debug,Default,PartialEq,Eq)] pub struct Record{pub owner:Owner,pub generation:u64,pub user:u64,pub live:u64,pub read_granted:u64,pub reads:u64} #[repr(C)]#[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Snapshot{pub registry:u64,pub phase:u64,pub records:[Record;2]} #[derive(Clone,Copy,Debug,PartialEq,Eq)]pub enum Error{Configuration,Capacity,Principal,Session,Permission,Length,Exhausted,State} #[repr(C)]pub struct Registry<'a>{state:Snapshot,first:&'a mut[u8;64],second:&'a mut[u8;64]} impl<'a> Registry<'a>{ /// A fresh identity is supplied by the trusted kernel issuer; it must never /// repeat for a different registry. The two private stores stay borrowed /// until close. This Rust API is not a public login/authentication syscall. /// # Safety /// identity comes from the unique kernel issuer, and each owner passed to /// open/read/logout is obtained from the retained kernel dispatch context. pub unsafe fn new(identity:u64,first:&'a mut[u8;64],second:&'a mut[u8;64])->Result{if identity==0||first.as_ptr()==second.as_ptr(){return Err(Error::Configuration)}first.fill(0);second.fill(0);Ok(Self{state:Snapshot{registry:identity,phase:1,records:[Record::default();2]},first,second})} pub fn snapshot(&self)->Snapshot{self.state} pub fn open_from_kernel(&mut self,owner:Owner,user:u64)->Result{ if self.state.phase!=1{return Err(Error::State)}if owner.table==0||owner.generation==0||user==0{return Err(Error::Configuration)} if self.state.records.iter().any(|r|r.live==1&&r.owner==owner){return Err(Error::Principal)} let index=self.state.records.iter().position(|r|r.live==0).ok_or(Error::Capacity)?;let generation=self.state.records[index].generation.checked_add(1).ok_or(Error::Exhausted)?; self.state.records[index]=Record{owner,generation,user,live:1,read_granted:0,reads:0};Ok(Session{registry:self.state.registry,slot:index as u64,generation,user}) } fn gate(&self,owner:Owner,s:Session)->Result{ if self.state.phase!=1{return Err(Error::State)}if s.registry!=self.state.registry{return Err(Error::Session)}let i=usize::try_from(s.slot).map_err(|_|Error::Session)?;let r=self.state.records.get(i).ok_or(Error::Session)?; if r.live!=1||r.generation!=s.generation||r.user!=s.user{return Err(Error::Session)}if r.owner!=owner{return Err(Error::Principal)}Ok(i) } pub fn grant_from_kernel(&mut self,owner:Owner,s:Session,contents:[u8;64])->Result{ let i=self.gate(owner,s)?;if self.state.records[i].read_granted!=0{return Err(Error::Permission)} if i==0{self.first.copy_from_slice(&contents)}else{self.second.copy_from_slice(&contents)}self.state.records[i].read_granted=1;Ok(ReadGrant{session:s,object:0x194500+i as u64}) } pub fn read(&mut self,owner:Owner,grant:ReadGrant,out:&mut[u8])->Result{let i=self.gate(owner,grant.session)?;if grant.object!=0x194500+i as u64||self.state.records[i].read_granted!=1{return Err(Error::Permission)}if out.is_empty()||out.len()>64{return Err(Error::Length)}let reads=self.state.records[i].reads.checked_add(1).ok_or(Error::Exhausted)?;if i==0{out.copy_from_slice(&self.first[..out.len()])}else{out.copy_from_slice(&self.second[..out.len()])}self.state.records[i].reads=reads;Ok(out.len())} pub fn logout(&mut self,owner:Owner,s:Session)->Result<(),Error>{let i=self.gate(owner,s)?;if i==0{self.first.fill(0)}else{self.second.fill(0)}let generation=self.state.records[i].generation;self.state.records[i]=Record{generation,..Record::default()};Ok(())} pub fn close_from_kernel(&mut self)->Result<(),Error>{if self.state.phase!=1{return Err(Error::State)}if self.state.records.iter().any(|r|r.live!=0){return Err(Error::State)}self.first.fill(0);self.second.fill(0);self.state.phase=2;Ok(())} } impl Registry<'_>{pub const fn os_k1945_physical_layout()->[u64;15]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Self,state)as u64,offset_of!(Self,first)as u64,offset_of!(Self,second)as u64,size_of::()as u64,offset_of!(Snapshot,registry)as u64,offset_of!(Snapshot,phase)as u64,offset_of!(Snapshot,records)as u64,size_of::()as u64,offset_of!(Record,owner)as u64,offset_of!(Record,generation)as u64,offset_of!(Record,user)as u64,offset_of!(Record,live)as u64,offset_of!(Record,read_granted)as u64,offset_of!(Record,reads)as u64]}}