//! K2131–K2135 PSCI SYSTEM_RESET path. Before arming a controlled guest //! reboot, the kernel verifies that PSCI is present, enabled and compatible, //! has a valid conduit (HVC or SMC), reports at least PSCI 0.2 and supports //! SYSTEM_RESET (0x8400_0009). Only a committed Reset ticket (op=2) may arm //! the reboot, and the post-reset observation must carry a fresh non-zero //! `new_boot_id != old_boot_id`, an advancing `new_boot0_seq > old_boot0_seq` //! and `reused_log = false` so an old boot log or stale boot identity is never //! accepted as a fresh reboot. pub const PSCI_VERSION:u64=0x8400_0000; pub const PSCI_FEATURES:u64=0x8400_000a; pub const SYSTEM_RESET:u64=0x8400_0009; pub const MIN_VERSION:u32=0x0000_0002; pub const OP_RESET:u64=2; #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Conduit { Hvc=1, Smc=2 } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, NoPsci, Unsupported, Ticket, State, StaleBootIdentity, ReusedBootLog } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Phase { Unprobed=0, Ready=1, Armed=2, RebootVerified=3 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Probe { pub present:bool, pub enabled:bool, pub compatible:bool, pub conduit:Option, pub version:u32, pub features_reset:i32 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Invocation { pub conduit:Conduit, pub function:u64, pub old_boot_id:u64, pub old_boot0_seq:u64, pub ticket_sequence:u64 } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct RebootObservation { pub new_boot_id:u64, pub new_boot0_seq:u64, pub conduit:Conduit, pub function:u64, pub ticket_sequence:u64, pub reused_log:bool } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub old_boot_id:u64, pub new_boot_id:u64, pub old_boot0_seq:u64, pub new_boot0_seq:u64, pub phase:Phase, pub conduit:u64, pub version:u32, pub armed:u64, pub verified_reboots:u64, pub refused:u64, pub last_ticket:u64 } #[repr(C)] #[derive(Debug)] pub struct ResetCoordinator { old_boot_id:u64, new_boot_id:u64, old_boot0_seq:u64, new_boot0_seq:u64, phase:Phase, conduit:u64, version:u32, armed:u64, verified_reboots:u64, refused:u64, last_ticket:u64 } impl ResetCoordinator { pub fn new(old_boot_id:u64,old_boot0_seq:u64)->Result{ if old_boot_id==0||old_boot0_seq==0{return Err(Error::Configuration)} Ok(Self{old_boot_id,new_boot_id:0,old_boot0_seq,new_boot0_seq:0,phase:Phase::Unprobed,conduit:0,version:0,armed:0,verified_reboots:0,refused:0,last_ticket:0}) } pub fn status(&self)->Status { Status{old_boot_id:self.old_boot_id,new_boot_id:self.new_boot_id,old_boot0_seq:self.old_boot0_seq,new_boot0_seq:self.new_boot0_seq,phase:self.phase,conduit:self.conduit,version:self.version,armed:self.armed,verified_reboots:self.verified_reboots,refused:self.refused,last_ticket:self.last_ticket} } pub fn probe(&mut self,p:Probe)->Result{ if self.phase!=Phase::Unprobed{return Err(Error::State)} if !p.present||!p.enabled||!p.compatible{self.refused+=1;return Err(Error::NoPsci)} let Some(conduit)=p.conduit else{self.refused+=1;return Err(Error::NoPsci)}; if p.versionResult{ if self.phase!=Phase::Ready{return Err(Error::State)} if ticket_op!=OP_RESET||ticket_sequence==0{self.refused+=1;return Err(Error::Ticket)} self.phase=Phase::Armed;self.armed+=1;self.last_ticket=ticket_sequence; let conduit=if self.conduit==Conduit::Hvc as u64{Conduit::Hvc}else{Conduit::Smc}; Ok(Invocation{conduit,function:SYSTEM_RESET,old_boot_id:self.old_boot_id,old_boot0_seq:self.old_boot0_seq,ticket_sequence}) } pub fn observe_reboot(&mut self,obs:RebootObservation)->Result{ if self.phase!=Phase::Armed{return Err(Error::State)} if obs.reused_log||obs.new_boot0_seq<=self.old_boot0_seq{self.refused+=1;return Err(Error::ReusedBootLog)} if obs.new_boot_id==0||obs.new_boot_id==self.old_boot_id{self.refused+=1;return Err(Error::StaleBootIdentity)} if obs.conduit as u64!=self.conduit||obs.function!=SYSTEM_RESET||obs.ticket_sequence!=self.last_ticket{self.refused+=1;return Err(Error::Ticket)} self.new_boot_id=obs.new_boot_id;self.new_boot0_seq=obs.new_boot0_seq;self.phase=Phase::RebootVerified;self.verified_reboots+=1;Ok(self.status()) } }