// K2120 guest experiment: PID1's crash-loop guard in the product kernel // runtime. Three generations of a native EL0 service crash with a real EL0 // data abort (store to address zero); the third crash inside the window // quarantines the service: no lease, no fourth start. Each generation also // sends a health reply that PID1 only receives after that generation is gone; // it is refused as Stale. An EL0 operator program then requests explicit // recovery, and one fresh generation starts, answers health and exits. use super::feature_el0 as el0; use turkos_kernel::runtime::service_crashloop::{Error,Guard,Identity,Phase,Policy}; use core::ptr::{addr_of,addr_of_mut,read_volatile,write_volatile}; #[unsafe(no_mangle)] static mut OS_K2120_EVIDENCE:[u64;64]=[0;64]; #[unsafe(no_mangle)] static mut OS_K2120_CAPTURE_GATE:u8=0; static CRASHER:&[u8]=include_bytes!("k2120-crasher.bin"); static HEALTHY:&[u8]=include_bytes!("k2120-healthy.bin"); static OPERATOR:&[u8]=include_bytes!("k2120-operator.bin"); const MAGIC:u64=0x4b32_3132_305f_434c; // "K2120_CL" const SYS_EXIT:u64=1; const SYS_HEALTH:u64=16; const SYS_RECOVER:u64=17; const REFUSED:u64=1<<63; const SERVICE_PID:u64=7; const OPERATOR_ID:el0::Identity=el0::Identity{pid:1,generation:0x2120_0001}; fn word(i:usize,v:u64){unsafe{write_volatile(addr_of_mut!(OS_K2120_EVIDENCE).cast::().add(i),v)}} fn checkpoint(stage:u64){ word(1,stage); unsafe{write_volatile(addr_of_mut!(OS_K2120_CAPTURE_GATE),0)}; el0::write_fields("TURKOS/OS/K2120: CAPTURE",&[("STAGE",stage)]); while unsafe{read_volatile(addr_of!(OS_K2120_CAPTURE_GATE))}==0{unsafe{core::arch::asm!("yield",options(nomem,nostack,preserves_flags))}} } fn identity(id:el0::Identity)->Identity{Identity{pid:id.pid,generation:id.generation}} fn code(r:Result)->u64{match r{Ok(_)=>0,Err(e)=>REFUSED|e as u64}} pub fn run(runtime:&mut super::OsK660Runtime,tables:&super::mmu::Tables,boot:&super::BootData)->Result<(),&'static str>{ let allocator=&mut runtime.memory_mut().map_err(|_|"k2120-memory")?.allocator; let frequency=el0::frequency();if frequency==0{return Err("k2120-frequency")} let free_before=allocator.remaining_pages(); let mut guard=Guard::new(Policy{limit:3,window:2*frequency}).map_err(|_|"k2120-policy")?; word(0,MAGIC);word(2,frequency);word(3,2*frequency);word(4,free_before as u64); checkpoint(1); let mut in_flight:Option=None; let mut run_program=|guard:&mut Guard,id:el0::Identity,program:&[u8],arg:u64,in_flight:&mut Option|->Result<(el0::Outcome,u64),&'static str>{ let user=unsafe{tables.build_user_space(allocator,boot.hardware.ram_base,boot.hardware.ram_size)}.map_err(|_|"k2120-user")?; if !user.contract_valid(tables.root){return Err("k2120-user-contract")} unsafe{el0::load(&user,program)}?; let mut immediate=0u64; let outcome=unsafe{el0::run(&user,id,[arg,0],&mut |caller,frame|{ match frame.number(){ SYS_HEALTH=>{ let r=if frame.arg(0)==0{let r=code(guard.health(identity(caller)));if r==0{immediate+=1}r}else{*in_flight=Some(identity(caller));0}; frame.ret(r);el0::Step::Resume } SYS_RECOVER=>{ let r=if caller!=OPERATOR_ID||frame.arg(0)!=SERVICE_PID{REFUSED|0xa}else{code(guard.recover(el0::counter()))}; frame.ret(r);el0::Step::Resume } SYS_EXIT=>el0::Step::Exit(frame.arg(0)), _=>el0::Step::Exit(0xbad), } })}; unsafe{user.release_after_tlbi(allocator,tables.root)}.map_err(|_|"k2120-release")?; Ok((outcome,immediate)) }; // Three quick crashing generations. let mut stale=0u64; for g in 0..3u64{ let id=el0::Identity{pid:SERVICE_PID,generation:0x2120_0101+g}; guard.start(identity(id),el0::counter()).map_err(|_|"k2120-start")?; // A reply sent by the previous generation arrives only now. if let Some(old)=in_flight.take(){if guard.health(old)==Err(Error::Stale){stale+=1}} let lease=guard.status().leases; let (o,immediate)=run_program(&mut guard,id,CRASHER,0,&mut in_flight)?; if o.fault==el0::FAULT_NONE{return Err("k2120-no-crash")} let decision=guard.crashed(identity(id),el0::counter()).map_err(|_|"k2120-crash")?; let s=guard.status(); for(j,v)in[id.generation,lease,immediate,o.esr,o.far,o.elr,decision as u64,s.crashes_in_window,s.leases,s.phase as u64].into_iter().enumerate(){word(8+(g as usize)*10+j,v)} } let q=guard.status(); let fourth=code(guard.start(Identity{pid:SERVICE_PID,generation:0x2120_0104},el0::counter())); let late=in_flight.take().map(|old|code(guard.health(old))).unwrap_or(0); for(j,v)in[q.phase as u64,q.leases,q.quarantined_at,q.restarts,q.crashes_total,fourth,late,stale,guard.status().stale_refusals].into_iter().enumerate(){word(38+j,v)} checkpoint(2); // Explicit operator recovery, then one fresh healthy generation. let (op,_)=run_program(&mut guard,OPERATOR_ID,OPERATOR,SERVICE_PID,&mut in_flight)?; let r=guard.status(); let fresh=el0::Identity{pid:SERVICE_PID,generation:0x2120_0104}; guard.start(identity(fresh),el0::counter()).map_err(|_|"k2120-fresh-start")?; let (h,immediate)=run_program(&mut guard,fresh,HEALTHY,0,&mut in_flight)?; let f=guard.status(); for(j,v)in[op.exit,op.fault,r.phase as u64,r.recoveries,r.crashes_in_window,h.exit,h.fault,immediate,f.phase as u64,f.leases,f.health_replies,f.restarts,f.crashes_total].into_iter().enumerate(){word(48+j,v)} let free_after=allocator.remaining_pages();word(5,free_after as u64); if free_after!=free_before{return Err("k2120-frame-accounting")} if q.phase!=Phase::Quarantined||fourth==0||stale!=2||op.exit!=0||h.exit!=0||f.phase!=Phase::Running{return Err("k2120-outcome")} checkpoint(3); el0::write_fields("TURKOS/OS/K2120-CRASH-LOOP:",&[("CRASHES",q.crashes_total),("QUARANTINED",u64::from(q.phase==Phase::Quarantined)),("LEASES",q.leases), ("STALE",stale),("RECOVERIES",r.recoveries),("FRESH_HEALTH",immediate),("FREE_EQUAL",1)]); Ok(()) }