pub const MAX_CPUS: usize = 8; pub const AFFINITY_MASK: u64 = 0xff00ffffff; pub const FRAME_BYTES: u64 = 288; pub const LOW_CANARY: u64 = 0x20304c4f57495251; pub const HIGH_CANARY: u64 = 0x2030484947495251; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Configuration, Cpu, Address, Overlap, State, Busy, Ticket, Registers, Frame, Payload, Exhausted } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Configuration { pub affinity: u64, pub context: u64, pub vector: u64, pub execution: u64, pub irq: u64, pub redistributor: u64 } const EMPTY_CONFIG: Configuration = Configuration { affinity: 0, context: 0, vector: 0, execution: 0, irq: 0, redistributor: 0 }; #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct State { pub issuer: u64, pub epoch: u64, pub count: u64, pub phase: u64, pub prepared: u64, pub installed: u64, pub retired: u64, pub aborted: u64 } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Record { pub config: Configuration, pub generation: u64, pub phase: u64, pub prior_vbar: u64, pub prior_sp: u64, pub prior_tpidr: u64, pub prior_tpidrro: u64, pub interrupts: u64 } const EMPTY_RECORD: Record = Record { config: EMPTY_CONFIG, generation: 0, phase: 0, prior_vbar: 0, prior_sp: 0, prior_tpidr: 0, prior_tpidrro: 0, interrupts: 0 }; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Snapshot { pub state: State, pub records: [Record; MAX_CPUS] } /// Inert installation descriptor. Copying it does not create an owner. #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Descriptor { pub words: [u64; 16] } impl Descriptor { pub fn bytes(&self) -> [u8; 128] { let mut b = [0; 128]; for (i, v) in self.words.iter().enumerate() { b[i*8..i*8+8].copy_from_slice(&v.to_le_bytes()); } b } } #[repr(C)] pub struct Ticket { issuer: u64, epoch: u64, cpu: u64, generation: u64, context: u64 } #[repr(C)] pub struct Installed { issuer: u64, epoch: u64, cpu: u64, generation: u64, context: u64, prior_vbar: u64, prior_sp: u64, prior_tpidr: u64, prior_tpidrro: u64 } pub struct Publication { pub ticket: Ticket, pub descriptor: Descriptor } /// Trusted hardware observations captured by the kernel after a real IRQ/return. #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Hardware { pub mpidr: u64, pub vbar: u64, pub sp: u64, pub irq_sp: u64, pub tpidr: u64, pub tpidrro: u64, pub prior_vbar: u64, pub prior_sp: u64, pub prior_tpidr: u64, pub prior_tpidrro: u64, pub frame_sp: u64, pub frame_elr: u64, pub frame_spsr: u64, pub interrupts: u64, pub intid: u64, pub low_canary: u64, pub high_canary: u64, pub daif: u64, pub pending: u64, pub active: u64, } #[repr(C)] pub struct Registry { state: State, ram_start: u64, ram_end: u64, barrier: u64, records: [Record; MAX_CPUS] } fn fits(start: u64, size: u64, low: u64, high: u64) -> bool { start >= low && start.checked_add(size).is_some_and(|end| end <= high) } fn overlap(a: (u64,u64), b: (u64,u64)) -> bool { a.0 < b.0+b.1 && b.0 < a.0+a.1 } fn regions(c: Configuration) -> [(u64,u64);4] { [(c.context,256),(c.vector,2048),(c.execution,4096),(c.irq,4096)] } impl Registry { /// # Safety /// Fresh issuer/epoch, validated topology and GICR affinity, exclusive stack /// leases and immutable executable vector tables are supplied by the kernel. /// Contexts and barrier stay live, mapped and serialized through retirement. pub unsafe fn new(issuer: u64, epoch: u64, cpus: &[Configuration], ram_start: u64, ram_end: u64, barrier: u64) -> Result { if issuer==0 || epoch==0 || cpus.is_empty() || cpus.len()>MAX_CPUS || ram_start%4096!=0 || ram_end%4096!=0 || ram_start>=ram_end || barrier%128!=0 || !fits(barrier,1152,ram_start,ram_end) { return Err(Error::Configuration); } let mut records=[EMPTY_RECORD;MAX_CPUS]; for (i,c) in cpus.iter().enumerate() { if c.affinity & !AFFINITY_MASK != 0 { return Err(Error::Cpu); } if c.context%128!=0 || c.vector%2048!=0 || c.execution%4096!=0 || c.irq%4096!=0 || c.redistributor==0 || c.redistributor%131072!=0 || c.redistributor.checked_add(131072).is_none() { return Err(Error::Address); } let current=regions(*c); if current.iter().any(|(base,size)| !fits(*base,*size,ram_start,ram_end)) { return Err(Error::Address); } if current.iter().enumerate().any(|(j,r)| overlap(*r,(barrier,1152)) || current[..j].iter().any(|p| overlap(*r,*p))) { return Err(Error::Overlap); } if cpus[..i].iter().any(|old| old.affinity==c.affinity || old.redistributor==c.redistributor || regions(*old).iter().any(|a| current.iter().any(|b| overlap(*a,*b)))) { return Err(Error::Overlap); } records[i]=Record { config:*c, phase:1, ..EMPTY_RECORD }; } Ok(Self { state:State { issuer,epoch,count:cpus.len()as u64,phase:1,prepared:0,installed:0,retired:0,aborted:0 },ram_start,ram_end,barrier,records }) } pub fn snapshot(&self)->Snapshot { Snapshot{state:self.state,records:self.records} } fn descriptor(&self,cpu:usize)->Descriptor { let r=self.records[cpu]; let c=r.config; Descriptor { words:[self.state.issuer,self.state.epoch,r.generation,c.affinity,cpu as u64,c.context,c.vector,c.execution,c.execution+4096-16,c.irq,c.irq+4096-16,self.barrier,c.redistributor,LOW_CANARY^(cpu as u64)^r.generation,HIGH_CANARY^(cpu as u64)^r.generation,0] } } pub fn prepare(&mut self,cpu:usize)->Result { if self.state.phase!=1 { return Err(Error::State); } if cpu>=self.state.count as usize { return Err(Error::Cpu); } if !matches!(self.records[cpu].phase,1|4) { return Err(Error::Busy); } let generation=self.records[cpu].generation.checked_add(1).ok_or(Error::Exhausted)?; let prepared=self.state.prepared.checked_add(1).ok_or(Error::Exhausted)?; let config=self.records[cpu].config; self.records[cpu]=Record{config,generation,phase:2,..EMPTY_RECORD}; self.state.prepared=prepared; Ok(Publication { ticket:Ticket{issuer:self.state.issuer,epoch:self.state.epoch,cpu:cpu as u64,generation,context:config.context},descriptor:self.descriptor(cpu) }) } fn check(&self,issuer:u64,epoch:u64,cpu:u64,generation:u64,context:u64,phase: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 || context!=r.config.context { return Err(Error::Ticket); } if r.phase!=phase { return Err(Error::State); } Ok(cpu as usize) } fn check_frame(&self,cpu:usize,h:Hardware)->Result<(),Error> { let r=self.records[cpu]; let c=r.config; if h.mpidr&AFFINITY_MASK!=c.affinity { return Err(Error::Cpu); } if h.irq_sp!=c.irq+4096-16-FRAME_BYTES || h.frame_sp!=c.execution+4096-16 || h.frame_elr%4!=0 || !fits(h.frame_elr,4,self.ram_start,self.ram_end) || h.frame_spsr&0x3cf!=0x345 || h.interrupts!=1 || h.intid!=15 || h.low_canary!=LOW_CANARY^(cpu as u64)^r.generation || h.high_canary!=HIGH_CANARY^(cpu as u64)^r.generation || h.daif&0x3c0!=0x3c0 || h.pending!=0 || h.active!=0 { return Err(Error::Frame); } Ok(()) } fn prior_live(&self,h:Hardware)->bool { if h.prior_vbar%2048!=0 || h.prior_sp%16!=0 { return false; } !self.records[..self.state.count as usize].iter().any(|r| { let c=r.config; (h.prior_vbar>=c.vector && h.prior_vbar=*base && *pResult { let cpu=self.check(ticket.issuer,ticket.epoch,ticket.cpu,ticket.generation,ticket.context,2)?; self.check_frame(cpu,h)?; let c=self.records[cpu].config; if h.vbar!=c.vector || h.sp!=c.execution+4096-16 || h.tpidr!=c.context || h.tpidrro!=h.prior_tpidrro || !self.prior_live(h) { return Err(Error::Registers); } if bytes!=self.descriptor(cpu).bytes() { return Err(Error::Payload); } let installed=self.state.installed.checked_add(1).ok_or(Error::Exhausted)?; let r=&mut self.records[cpu]; r.phase=3;r.prior_vbar=h.prior_vbar;r.prior_sp=h.prior_sp;r.prior_tpidr=h.prior_tpidr;r.prior_tpidrro=h.prior_tpidrro;r.interrupts=1;self.state.installed=installed; Ok(Installed { issuer:ticket.issuer,epoch:ticket.epoch,cpu:ticket.cpu,generation:ticket.generation,context:ticket.context,prior_vbar:h.prior_vbar,prior_sp:h.prior_sp,prior_tpidr:h.prior_tpidr,prior_tpidrro:h.prior_tpidrro }) } /// # Safety /// The CPU has masked IRQ, completed EOI/deactivation, restored its GIC bank, /// VBAR, SP and thread registers, published a terminal acknowledgement after /// its final stack access, and cannot use either stack or context again. pub unsafe fn retire_from_kernel(&mut self,token:Installed,bytes:&[u8],h:Hardware)->Result<(),Error> { let cpu=self.check(token.issuer,token.epoch,token.cpu,token.generation,token.context,3)?; self.check_frame(cpu,h)?;let r=self.records[cpu]; let expected=[r.prior_vbar,r.prior_sp,r.prior_tpidr,r.prior_tpidrro]; if [token.prior_vbar,token.prior_sp,token.prior_tpidr,token.prior_tpidrro]!=expected || [h.prior_vbar,h.prior_sp,h.prior_tpidr,h.prior_tpidrro]!=expected || [h.vbar,h.sp,h.tpidr,h.tpidrro]!=expected { return Err(Error::Registers); } if bytes!=self.descriptor(cpu).bytes() { return Err(Error::Payload); } let retired=self.state.retired.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].phase=4;self.state.retired=retired;Ok(()) } /// # Safety /// No target has received or can ever start this installation publication. pub unsafe fn abort_unstarted_from_kernel(&mut self,ticket:Ticket)->Result<(),Error> { let cpu=self.check(ticket.issuer,ticket.epoch,ticket.cpu,ticket.generation,ticket.context,2)?; let aborted=self.state.aborted.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].phase=1;self.state.aborted=aborted;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| r.phase==2 || r.phase==3) { return Err(Error::Busy); } self.state.phase=2;Ok(()) } }