pub const MAX_CPUS: usize = 8; pub const MAX_UPDATES: u64 = 1024; pub const AFFINITY_MASK: u64 = 0xff00ffffff; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Configuration, State, Cpu, Address, Duplicate, Updates, Ticket, Register, Payload, Busy, Exhausted } /// Inert bytes; copying an area does not grant a CPU binding capability. #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Area { pub issuer: u64, pub epoch: u64, pub generation: u64, pub affinity: u64, pub logical: u64, pub physical: u64, pub barrier: u64, pub updates: u64, pub counter: u64, pub sum: u64, pub prior_tpidr: u64, pub active: u64, pub reserved: [u64; 4], } impl Area { pub const EMPTY: Self = Self { issuer: 0, epoch: 0, generation: 0, affinity: 0, logical: 0, physical: 0, barrier: 0, updates: 0, counter: 0, sum: 0, prior_tpidr: 0, active: 0, reserved: [0; 4] }; pub fn bytes(&self) -> [u8; 128] { let mut b = [0; 128]; for (i, word) in [self.issuer, self.epoch, self.generation, self.affinity, self.logical, self.physical, self.barrier, self.updates, self.counter, self.sum, self.prior_tpidr, self.active, self.reserved[0], self.reserved[1], self.reserved[2], self.reserved[3]].iter().enumerate() { b[i * 8..i * 8 + 8].copy_from_slice(&word.to_le_bytes()); } b } } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Record { pub affinity: u64, pub physical: u64, pub generation: u64, pub phase: u64, pub prior_tpidr: u64, pub updates: u64 } const EMPTY_RECORD: Record = Record { affinity: 0, physical: 0, generation: 0, phase: 0, prior_tpidr: 0, updates: 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 published: u64, pub bound: u64, pub detached: u64, pub aborted: u64 } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Snapshot { pub state: State, pub records: [Record; MAX_CPUS] } #[repr(C)] pub struct Ticket { issuer: u64, epoch: u64, cpu: u64, generation: u64, physical: u64 } impl Ticket { pub fn physical(&self) -> u64 { self.physical } } #[repr(C)] pub struct Bound { issuer: u64, epoch: u64, cpu: u64, generation: u64, physical: u64, prior_tpidr: u64 } pub struct Publication { pub ticket: Ticket, pub area: Area } #[repr(C)] pub struct Registry { state: State, ram_start: u64, ram_end: u64, barrier: u64, records: [Record; MAX_CPUS] } fn range(start: u64, bytes: u64, low: u64, high: u64) -> bool { start >= low && start.checked_add(bytes).is_some_and(|end| end <= high) } impl Registry { /// # Safety /// The kernel supplies a fresh issuer/epoch, its validated CPU affinity list, /// one exclusively owned and mapped lease page per CPU, and a live barrier /// transport. It alone serializes registry changes and retains all resources /// while any CPU can still access its area. pub unsafe fn new(issuer: u64, epoch: u64, cpus: &[(u64, u64)], 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 || !range(barrier, 1152, ram_start, ram_end) { return Err(Error::Configuration); } let mut records = [EMPTY_RECORD; MAX_CPUS]; for (i, (affinity, physical)) in cpus.iter().enumerate() { if affinity & !AFFINITY_MASK != 0 { return Err(Error::Cpu); } if physical % 4096 != 0 || !range(*physical, 4096, ram_start, ram_end) { return Err(Error::Address); } if *physical < barrier + 1152 && barrier < physical + 4096 { return Err(Error::Address); } if cpus[..i].iter().any(|(a, p)| a == affinity || p == physical) { return Err(Error::Duplicate); } records[i] = Record { affinity: *affinity, physical: *physical, generation: 0, phase: 1, prior_tpidr: 0, updates: 0 }; } Ok(Self { state: State { issuer, epoch, count: cpus.len() as u64, phase: 1, published: 0, bound: 0, detached: 0, aborted: 0 }, ram_start, ram_end, barrier, records }) } pub fn snapshot(&self) -> Snapshot { Snapshot { state: self.state, records: self.records } } fn area(&self, cpu: usize) -> Area { let r = self.records[cpu]; Area { issuer: self.state.issuer, epoch: self.state.epoch, generation: r.generation, affinity: r.affinity, logical: cpu as u64, physical: r.physical, barrier: self.barrier, updates: r.updates, ..Area::EMPTY } } pub fn prepare(&mut self, cpu: usize, updates: u64) -> 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); } if updates == 0 || updates > MAX_UPDATES { return Err(Error::Updates); } let generation = self.records[cpu].generation.checked_add(1).ok_or(Error::Exhausted)?; let published = self.state.published.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].generation = generation; self.records[cpu].phase = 2; self.records[cpu].updates = updates; self.records[cpu].prior_tpidr = 0; self.state.published = published; Ok(Publication { ticket: Ticket { issuer: self.state.issuer, epoch: self.state.epoch, cpu: cpu as u64, generation, physical: self.records[cpu].physical }, area: self.area(cpu) }) } fn check(&self, issuer: u64, epoch: u64, cpu: u64, generation: u64, physical: 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 index = cpu as usize; let r = self.records[index]; if generation != r.generation || physical != r.physical { return Err(Error::Ticket); } if r.phase != phase { return Err(Error::State); } Ok(index) } /// # Safety /// An independently observed receipt comes from this CPU after TPIDR_EL1 /// binding and its bounded counter work. The register values are genuine, /// and the CPU will hold its area until the owner releases the startup /// barrier. No other CPU or primary writer may modify the owned area. pub unsafe fn bind_from_kernel(&mut self, ticket: Ticket, bytes: &[u8], actual_mpidr: u64, active_tpidr: u64, prior_tpidr: u64) -> Result { let cpu = self.check(ticket.issuer, ticket.epoch, ticket.cpu, ticket.generation, ticket.physical, 2)?; let r = self.records[cpu]; if actual_mpidr & AFFINITY_MASK != r.affinity { return Err(Error::Cpu); } if active_tpidr != r.physical || prior_tpidr == r.physical { return Err(Error::Register); } let mut expected = self.area(cpu); expected.counter = r.updates; expected.sum = r.updates * (r.updates + 1) / 2; expected.prior_tpidr = prior_tpidr; expected.active = 1; if bytes != expected.bytes() { return Err(Error::Payload); } let bound = self.state.bound.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].phase = 3; self.records[cpu].prior_tpidr = prior_tpidr; self.state.bound = bound; Ok(Bound { issuer: ticket.issuer, epoch: ticket.epoch, cpu: ticket.cpu, generation: ticket.generation, physical: ticket.physical, prior_tpidr }) } /// # Safety /// This CPU has restored its actual previous TPIDR_EL1, published its terminal /// barrier acknowledgement, and cannot access the area again. The captured /// area and register values are genuine, acquired after that publication. pub unsafe fn detach_from_kernel(&mut self, bound: Bound, bytes: &[u8], actual_mpidr: u64, restored_tpidr: u64) -> Result<(), Error> { let cpu = self.check(bound.issuer, bound.epoch, bound.cpu, bound.generation, bound.physical, 3)?; let r = self.records[cpu]; if actual_mpidr & AFFINITY_MASK != r.affinity { return Err(Error::Cpu); } if bound.prior_tpidr != r.prior_tpidr || restored_tpidr != r.prior_tpidr { return Err(Error::Register); } let mut expected = self.area(cpu); expected.counter = r.updates; expected.sum = r.updates * (r.updates + 1) / 2; expected.prior_tpidr = r.prior_tpidr; if bytes != expected.bytes() { return Err(Error::Payload); } let detached = self.state.detached.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].phase = 4; self.state.detached = detached; Ok(()) } /// # Safety /// The target has not received or started this publication, and cannot use /// its page. A merely delayed secondary does not satisfy this precondition. 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.physical, 2)?; let aborted = self.state.aborted.checked_add(1).ok_or(Error::Exhausted)?; self.records[cpu].phase = 1; self.records[cpu].updates = 0; 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(()) } }