pub const MAX_CPUS:usize=8; pub const AFFINITY_MASK:u64=0xff00ffffff; pub const MAX_FREQUENCY:u64=1_000_000_000; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error{Configuration,Cpu,State,Busy,Ticket,Frequency,Control,Deadline,Interrupt,Restore,Exhausted} #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct State{pub issuer:u64,pub epoch:u64,pub count:u64,pub phase:u64,pub registered:u64,pub armed:u64,pub fired:u64,pub cancelled:u64,pub unregistered:u64} #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Record{pub affinity:u64,pub frequency:u64,pub generation:u64,pub phase:u64,pub arm_generation:u64,pub start:u64,pub deadline:u64,pub observed:u64,pub interrupts:u64,pub prior_control:u64,pub prior_compare:u64} const EMPTY:Record=Record{affinity:0,frequency:0,generation:0,phase:0,arm_generation:0,start:0,deadline:0,observed:0,interrupts:0,prior_control:0,prior_compare:0}; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Snapshot{pub state:State,pub records:[Record;MAX_CPUS]} #[repr(C)] pub struct Registration{issuer:u64,epoch:u64,cpu:u64,generation:u64} #[repr(C)] pub struct Arm{issuer:u64,epoch:u64,cpu:u64,generation:u64,arm_generation:u64} #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Program{pub affinity:u64,pub frequency:u64,pub start:u64,pub deadline:u64,pub arm_generation:u64} pub struct Prepared{pub arm:Arm,pub program:Program} #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Delivery{pub mpidr:u64,pub frequency:u64,pub counter:u64,pub compare:u64,pub before_control:u64,pub after_control:u64,pub intid:u64,pub pending:u64,pub active:u64} #[repr(C)] pub struct Registry{state:State,records:[Record;MAX_CPUS]} impl Registry{ /// # Safety /// Kernel supplies a fresh issuer/epoch and its validated CPU topology. It /// serializes mutations and holds the per-CPU PPI/vector/stack owners until /// all registrations are unregistered; no competing timer owner can exist. pub unsafe fn new(issuer:u64,epoch:u64,affinities:&[u64])->Result{ if issuer==0||epoch==0||affinities.is_empty()||affinities.len()>MAX_CPUS{return Err(Error::Configuration)} let mut records=[EMPTY;MAX_CPUS];for(i,a)in affinities.iter().enumerate(){if a&!AFFINITY_MASK!=0||affinities[..i].contains(a){return Err(Error::Cpu)}records[i]=Record{affinity:*a,phase:1,..EMPTY};} Ok(Self{state:State{issuer,epoch,count:affinities.len()as u64,phase:1,registered:0,armed:0,fired:0,cancelled:0,unregistered:0},records}) } pub fn snapshot(&self)->Snapshot{Snapshot{state:self.state,records:self.records}} fn cpu(&self,cpu:usize,mpidr:u64)->Result{if self.state.phase!=1{return Err(Error::State)}if cpu>=self.state.count as usize{return Err(Error::Cpu)}let r=self.records[cpu];if mpidr&AFFINITY_MASK!=r.affinity{return Err(Error::Cpu)}Ok(r)} fn identity(&self,issuer:u64,epoch:u64,cpu:u64,generation:u64)->Result{if self.state.phase!=1{return Err(Error::State)}if issuer!=self.state.issuer||epoch!=self.state.epoch||cpu>=self.state.count{return Err(Error::Ticket)}let r=self.records[cpu as usize];if generation!=r.generation{return Err(Error::Ticket)}Ok(cpu as usize)} /// # Safety /// Values come from actual MPIDR/CNTFRQ/CNTP_CTL/CNTP_CVAL reads on this CPU. /// Its previous timer is disabled and no pending/active PPI remains. The old /// register image is retained for exact restoration of writable timer state. pub unsafe fn register_from_kernel(&mut self,cpu:usize,mpidr:u64,frequency:u64,control:u64,compare:u64,pending:u64,active:u64)->Result{ let r=self.cpu(cpu,mpidr)?;if !matches!(r.phase,1|5){return Err(Error::Busy)}if frequency==0||frequency>MAX_FREQUENCY{return Err(Error::Frequency)}if control&1!=0||control&!7!=0||pending!=0||active!=0{return Err(Error::Control)} let generation=r.generation.checked_add(1).ok_or(Error::Exhausted)?;let registered=self.state.registered.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu]=Record{affinity:r.affinity,frequency,generation,phase:2,prior_control:control&3,prior_compare:compare,..EMPTY};self.state.registered=registered;Ok(Registration{issuer:self.state.issuer,epoch:self.state.epoch,cpu:cpu as u64,generation}) } /// # Safety /// MPIDR, frequency and counter are fresh genuine reads on the registration's /// CPU while timer IRQ is masked. The kernel programs the returned deadline /// exactly and retains the returned arm until a terminal hardware observation. pub unsafe fn arm_from_kernel(&mut self,registration:&Registration,mpidr:u64,frequency:u64,counter:u64,delta:u64)->Result{ let cpu=self.identity(registration.issuer,registration.epoch,registration.cpu,registration.generation)?;let r=self.cpu(cpu,mpidr)?;if !matches!(r.phase,2|4){return Err(Error::Busy)}if frequency!=r.frequency{return Err(Error::Frequency)}if delta==0||delta>frequency{return Err(Error::Deadline)}let deadline=counter.checked_add(delta).ok_or(Error::Deadline)?;let arm_generation=r.arm_generation.checked_add(1).ok_or(Error::Exhausted)?;let armed=self.state.armed.checked_add(1).ok_or(Error::Exhausted)?; let out=&mut self.records[cpu];out.phase=3;out.arm_generation=arm_generation;out.start=counter;out.deadline=deadline;out.observed=0;self.state.armed=armed; Ok(Prepared{arm:Arm{issuer:self.state.issuer,epoch:self.state.epoch,cpu:cpu as u64,generation:r.generation,arm_generation},program:Program{affinity:r.affinity,frequency,start:counter,deadline,arm_generation}}) } fn armed(&self,arm:&Arm)->Result{let cpu=self.identity(arm.issuer,arm.epoch,arm.cpu,arm.generation)?;let r=self.records[cpu];if arm.arm_generation!=r.arm_generation{return Err(Error::Ticket)}if r.phase!=3{return Err(Error::State)}Ok(cpu)} /// # Safety /// Receipt comes from a real PPI 30 handler after timer disable, EOI and /// deactivation; counter, compare, controls and GIC pending/active are actual /// reads. The retained arm alone owns this hardware episode. pub unsafe fn delivered_from_kernel(&mut self,arm:Arm,h:Delivery)->Result<(),Error>{ let cpu=self.armed(&arm)?;let r=self.cpu(cpu,h.mpidr)?;if h.frequency!=r.frequency{return Err(Error::Frequency)}if h.compare!=r.deadline||h.counterResult<(),Error>{ let cpu=self.armed(&arm)?;let r=self.cpu(cpu,mpidr)?;if frequency!=r.frequency{return Err(Error::Frequency)}if counter=r.deadline||compare!=r.deadline{return Err(Error::Deadline)}if control&3!=0||control&!7!=0||pending!=0||active!=0{return Err(Error::Control)}let cancelled=self.state.cancelled.checked_add(1).ok_or(Error::Exhausted)?;self.records[cpu].phase=2;self.records[cpu].observed=counter;self.state.cancelled=cancelled;Ok(()) } /// # Safety /// CPU restored its original disabled timer and compare, quiesced the PPI, /// and cannot program this registration again. The observations are genuine. pub unsafe fn unregister_from_kernel(&mut self,registration:Registration,mpidr:u64,control:u64,compare:u64,pending:u64,active:u64)->Result<(),Error>{ let cpu=self.identity(registration.issuer,registration.epoch,registration.cpu,registration.generation)?;let r=self.cpu(cpu,mpidr)?;if !matches!(r.phase,2|4){return Err(Error::Busy)}if control&3!=r.prior_control||control&!7!=0||compare!=r.prior_compare||pending!=0||active!=0{return Err(Error::Restore)}let unregistered=self.state.unregistered.checked_add(1).ok_or(Error::Exhausted)?;self.records[cpu].phase=5;self.state.unregistered=unregistered;Ok(()) } pub fn close(&mut self)->Result<(),Error>{if self.state.phase!=1{return Err(Error::State)}if self.records[..self.state.count as usize].iter().any(|r|matches!(r.phase,2|3|4)){return Err(Error::Busy)}self.state.phase=2;Ok(())} } impl Registry{pub const fn os_k2035_physical_layout()->[u64;54]{use core::mem::{size_of,offset_of};[size_of::()as u64,offset_of!(Registry,state)as u64,offset_of!(Registry,records)as u64,size_of::()as u64,offset_of!(State,issuer)as u64,offset_of!(State,epoch)as u64,offset_of!(State,count)as u64,offset_of!(State,phase)as u64,offset_of!(State,registered)as u64,offset_of!(State,armed)as u64,offset_of!(State,fired)as u64,offset_of!(State,cancelled)as u64,offset_of!(State,unregistered)as u64,size_of::()as u64,offset_of!(Record,affinity)as u64,offset_of!(Record,frequency)as u64,offset_of!(Record,generation)as u64,offset_of!(Record,phase)as u64,offset_of!(Record,arm_generation)as u64,offset_of!(Record,start)as u64,offset_of!(Record,deadline)as u64,offset_of!(Record,observed)as u64,offset_of!(Record,interrupts)as u64,offset_of!(Record,prior_control)as u64,offset_of!(Record,prior_compare)as u64,size_of::()as u64,offset_of!(Registration,issuer)as u64,offset_of!(Registration,epoch)as u64,offset_of!(Registration,cpu)as u64,offset_of!(Registration,generation)as u64,size_of::()as u64,offset_of!(Arm,issuer)as u64,offset_of!(Arm,epoch)as u64,offset_of!(Arm,cpu)as u64,offset_of!(Arm,generation)as u64,offset_of!(Arm,arm_generation)as u64,size_of::()as u64,offset_of!(Program,affinity)as u64,offset_of!(Program,frequency)as u64,offset_of!(Program,start)as u64,offset_of!(Program,deadline)as u64,offset_of!(Program,arm_generation)as u64,size_of::()as u64,offset_of!(Delivery,mpidr)as u64,offset_of!(Delivery,frequency)as u64,offset_of!(Delivery,counter)as u64,offset_of!(Delivery,compare)as u64,offset_of!(Delivery,before_control)as u64,offset_of!(Delivery,after_control)as u64,offset_of!(Delivery,intid)as u64,offset_of!(Delivery,pending)as u64,offset_of!(Delivery,active)as u64,MAX_CPUS as u64,MAX_FREQUENCY]}}