//! Bounded transport diagnostics. Passing these checks never credits entropy. pub const MAX_SAMPLE: usize = 64; #[unsafe(no_mangle)]pub static mut OS_K1910_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::Accounting)?;write_volatile(core::ptr::addr_of_mut!(NEXT),next);write_volatile(core::ptr::addr_of_mut!(OS_K1910_NONCE_MASK),daif&0x3c0);Ok(value)}} #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(u64)] pub enum Error { Identity=1, State=2, Length=3, Foreign=4, Accounting=5, Untrusted=6 } #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(C)] pub struct Binding { pub device:u64, pub epoch:u64 } #[derive(Clone, Copy, Debug, PartialEq, Eq)] #[repr(C)] pub struct Snapshot { pub identity:u64, pub device:u64, pub epoch:u64, pub operation:u64, pub pending:u64, pub requested:u64, pub completions:u64, pub received:u64, pub last_length:u64, pub constant_samples:u64, pub repeated_samples:u64, pub suspect:u64, pub credited_bits:u64, } /// A token is neither clonable nor constructible by callers. One source owns /// one outstanding operation. A completion is still an untrusted observation. #[derive(Debug)] pub struct Request { source:u64, binding:Binding, operation:u64, length:usize } impl Request { pub fn length(&self)->usize { self.length } } #[repr(C)] pub struct Source { state:Snapshot, previous:[u8;MAX_SAMPLE] } impl Source { pub fn new(binding:Binding)->Result { if binding.device==0 || binding.device%512!=0 || binding.epoch==0 { return Err(Error::Identity); } let identity=identity()?; Ok(Self { state:Snapshot { identity,device:binding.device, epoch:binding.epoch, operation:0,pending:0,requested:0,completions:0,received:0, last_length:0,constant_samples:0,repeated_samples:0,suspect:0,credited_bits:0 },previous:[0;MAX_SAMPLE] }) } pub fn snapshot(&self)->Snapshot { self.state } pub fn begin(&mut self,length:usize)->Result { if self.state.pending!=0 { return Err(Error::State); } if length==0 || length>MAX_SAMPLE { return Err(Error::Length); } let operation=self.state.operation.checked_add(1).ok_or(Error::Accounting)?; self.state.operation=operation; self.state.pending=1; self.state.requested=length as u64; Ok(Request { source:self.state.identity,binding:Binding{device:self.state.device,epoch:self.state.epoch},operation,length }) } /// The driver binds these bytes to the actual used-ring length. A short /// completion is allowed; bytes beyond it must not influence the monitor. pub fn complete(&mut self,request:&Request,binding:Binding,bytes:&[u8])->Result<(),Error> { if request.source!=self.state.identity || binding!=request.binding || binding.device!=self.state.device || binding.epoch!=self.state.epoch || request.operation!=self.state.operation { return Err(Error::Foreign); } if self.state.pending!=1 || request.length as u64!=self.state.requested { return Err(Error::State); } if bytes.is_empty() || bytes.len()>request.length { return Err(Error::Length); } let completions=self.state.completions.checked_add(1).ok_or(Error::Accounting)?; let received=self.state.received.checked_add(bytes.len()as u64).ok_or(Error::Accounting)?; // A single byte is not enough to classify a sample as constant. let constant=bytes.len()>=2 && bytes.iter().all(|b|*b==bytes[0]); let repeated=self.state.last_length==bytes.len()as u64 && self.previous[..bytes.len()]==*bytes; let constants=self.state.constant_samples.checked_add(constant as u64).ok_or(Error::Accounting)?; let repeats=self.state.repeated_samples.checked_add(repeated as u64).ok_or(Error::Accounting)?; self.previous.fill(0); self.previous[..bytes.len()].copy_from_slice(bytes); self.state.pending=0; self.state.requested=0; self.state.completions=completions; self.state.received=received; self.state.last_length=bytes.len()as u64; self.state.constant_samples=constants; self.state.repeated_samples=repeats; self.state.suspect|=(constant||repeated)as u64; // Deliberately no transition from transport success to entropy credit. Ok(()) } pub fn secure_bytes(&self,_output:&mut[u8])->Result<(),Error> { Err(Error::Untrusted) } /// Only called after the driver's reset barrier and zero status/ready /// observations. It cannot cancel a live DMA request or release DMA pages. pub fn clear_history(&mut self)->Result<(),Error> { if self.state.pending!=0 { return Err(Error::State); } self.previous.fill(0); self.state.last_length=0; // Sticky suspicion and accounting survive erasure of raw history. Ok(()) } } // K1910 extension: reachable only inside this crate, after Service observes // actual zero status and queue-ready following the adapter reset barrier. impl Source {pub(super) fn finish_device_reset(&mut self){self.state.pending=0;self.state.requested=0;self.previous.fill(0);self.state.last_length=0;}} impl Source {pub const fn os_k1910_physical_layout()->[u64;17]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Self,state)as u64,offset_of!(Self,previous)as u64,size_of::()as u64,offset_of!(Snapshot,identity)as u64,offset_of!(Snapshot,device)as u64,offset_of!(Snapshot,epoch)as u64,offset_of!(Snapshot,operation)as u64,offset_of!(Snapshot,pending)as u64,offset_of!(Snapshot,requested)as u64,offset_of!(Snapshot,completions)as u64,offset_of!(Snapshot,received)as u64,offset_of!(Snapshot,last_length)as u64,offset_of!(Snapshot,constant_samples)as u64,offset_of!(Snapshot,repeated_samples)as u64,offset_of!(Snapshot,suspect)as u64,offset_of!(Snapshot,credited_bits)as u64]}} impl Request {pub const fn os_k1910_physical_layout()->[u64;7]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Self,source)as u64,offset_of!(Self,binding)as u64,offset_of!(Self,operation)as u64,offset_of!(Self,length)as u64,offset_of!(Binding,device)as u64,offset_of!(Binding,epoch)as u64]}}