// K2135 guest experiment: PSCI SYSTEM_RESET path in the product kernel runtime. // Discovers the PSCI conduit from the live DTB, queries real QEMU PSCI // firmware via HVC for PSCI_VERSION, PSCI_FEATURES(SYSTEM_RESET) and an // unsupported function ID, verifies negative probes (absent PSCI and // unsupported function), runs an EL0 shell tool that is denied reboot, and // runs EL0 PID1 to reject an Off ticket on the Reset path, arm SYSTEM_RESET // with a committed Reset ticket, reject a reused boot log and a replayed // boot_id, and verify a fresh BOOT0 sequence and new boot_id. use super::feature_el0 as el0; use turkos_kernel::runtime::guest_power_capability::{ALL,Authority,Identity,Op,Ticket}; use turkos_kernel::runtime::guest_power_psci_reset::{Conduit,Error,Phase,Probe,RebootObservation,ResetCoordinator,PSCI_FEATURES,PSCI_VERSION,SYSTEM_RESET}; use turkos_kernel::runtime::psci_call::{discover_conduit,Conduit as DtbConduit}; use core::arch::asm; use core::ptr::{addr_of,addr_of_mut,read_volatile,write_volatile}; #[unsafe(no_mangle)] static mut OS_K2135_EVIDENCE:[u64;64]=[0;64]; #[unsafe(no_mangle)] static mut OS_K2135_CAPTURE_GATE:u8=0; static SHELL_TOOL:&[u8]=include_bytes!("k2135-shell-tool.bin"); static PID1:&[u8]=include_bytes!("k2135-pid1.bin"); const MAGIC:u64=0x4b32_3133_355f_5052; // "K2135_PR" const BOOT_DELTA:u64=0x5245_5345_545f_3235; const SYS_EXIT:u64=1; const SYS_REQUEST:u64=16; const SYS_ARM_RESET:u64=17; const SYS_WORK:u64=18; const SYS_OBSERVE:u64=19; const UNSUPPORTED_FN:u64=0x8400_00ff; const REFUSED:u64=1<<63; const PID1_ID:el0::Identity=el0::Identity{pid:1,generation:0x2135_0001}; struct State{ authority:Option, coord:Option, held:Option<(el0::Identity,Ticket)>, user:Option, old_boot_id:u64, new_boot_id:u64, conduit:Conduit, work:u64, calls:usize, base:usize, } static mut STATE:State=State{authority:None,coord:None,held:None,user:None,old_boot_id:0,new_boot_id:0,conduit:Conduit::Hvc,work:0,calls:0,base:0}; fn state()->&'static mut State{unsafe{&mut *addr_of_mut!(STATE)}} fn word(i:usize,v:u64){unsafe{write_volatile(addr_of_mut!(OS_K2135_EVIDENCE).cast::().add(i),v)}} #[inline(never)] fn checkpoint(stage:u64){ word(1,stage); unsafe{write_volatile(addr_of_mut!(OS_K2135_CAPTURE_GATE),0)}; el0::write_fields("TURKOS/OS/K2135: CAPTURE",&[("STAGE",stage)]); while unsafe{read_volatile(addr_of!(OS_K2135_CAPTURE_GATE))}==0{unsafe{asm!("yield",options(nomem,nostack,preserves_flags))}} } fn identity(id:el0::Identity)->Identity{Identity{pid:id.pid,generation:id.generation}} fn hvc(function:u64,arg0:u64)->u64{ let out:u64; unsafe{asm!("hvc #0",inout("x0")function=>out,inout("x1")arg0=>_,lateout("x2")_,lateout("x3")_,options(nomem,nostack))}; out } fn code(r:Result)->u64{match r{Ok(_)=>0,Err(e)=>REFUSED|e as u64}} #[inline(never)] fn probe_stage(boot:&super::BootData)->Result<(),&'static str>{ let old_boot_id=unsafe{read_volatile(addr_of!(super::OS_K680_EVIDENCE).cast::())}; let new_boot_id=if old_boot_id^BOOT_DELTA!=0{old_boot_id^BOOT_DELTA}else{1}; let blob=unsafe{core::slice::from_raw_parts(boot.dtb_address as *const u8,boot.dtb_size)}; let scratch=unsafe{&mut *addr_of_mut!(super::OS_K700_WORKSPACE)}; let dtb_conduit=discover_conduit(blob,scratch).map_err(|_|"k2135-dtb-psci")?; let conduit=match dtb_conduit{DtbConduit::Hvc=>Conduit::Hvc,DtbConduit::Smc=>Conduit::Smc}; if conduit!=Conduit::Hvc{return Err("k2135-expected-hvc")} let version=hvc(PSCI_VERSION,0) as u32; let features_reset=hvc(PSCI_FEATURES,SYSTEM_RESET) as u32 as i32; let features_bad=hvc(PSCI_FEATURES,UNSUPPORTED_FN) as u32 as i32; let mut neg_absent=ResetCoordinator::new(old_boot_id,1).map_err(|_|"k2135-neg-absent")?; let err_absent=code(neg_absent.probe(Probe{present:false,enabled:true,compatible:true,conduit:None,version:0,features_reset:-1})); let mut neg_unsup=ResetCoordinator::new(old_boot_id,1).map_err(|_|"k2135-neg-unsup")?; let err_unsup=code(neg_unsup.probe(Probe{present:true,enabled:true,compatible:true,conduit:Some(conduit),version,features_reset:features_bad})); let mut coord=ResetCoordinator::new(old_boot_id,1).map_err(|_|"k2135-coord")?; let probed=coord.probe(Probe{present:true,enabled:true,compatible:true,conduit:Some(conduit),version,features_reset}).map_err(|_|"k2135-probe")?; for(j,v)in[old_boot_id,new_boot_id,version as u64,features_reset as i64 as u64,features_bad as u32 as u64,err_absent,neg_absent.status().refused,err_unsup,neg_unsup.status().refused,probed.phase as u64,probed.conduit,probed.version as u64].into_iter().enumerate(){word(4+j,v)} let st=state(); st.authority=Some(Authority::new(identity(PID1_ID),ALL).map_err(|_|"k2135-authority")?); st.coord=Some(coord); st.held=None; st.old_boot_id=old_boot_id; st.new_boot_id=new_boot_id; st.conduit=conduit; Ok(()) } fn dispatch(caller:el0::Identity,frame:&mut el0::Frame)->el0::Step{ let st=state(); let authority=st.authority.as_mut().unwrap(); let coord=st.coord.as_mut().unwrap(); let r=match frame.number(){ SYS_REQUEST=>{ let op=match frame.arg(0){1=>Op::Off,2=>Op::Reset,_=>return el0::Step::Exit(0xbad)}; match authority.request(identity(caller),op){Ok(t)=>{let s=t.sequence();st.held=Some((caller,t));s}Err(e)=>REFUSED|e as u64} } SYS_ARM_RESET=>match st.held.take(){ Some((owner,t)) if owner==caller=>{ let seq=t.sequence(); match authority.commit(t){ Ok(op)=>match coord.arm_reset(op as u64,seq){Ok(inv)=>inv.function,Err(e)=>REFUSED|e as u64}, Err((e,t))=>{st.held=Some((owner,t));REFUSED|e as u64} } } other=>{st.held=other;REFUSED|Error::Ticket as u64} }, SYS_WORK=>{st.work+=1;st.work} SYS_OBSERVE=>{ let seq=coord.status().last_ticket; let obs=match frame.arg(0){ 0=>RebootObservation{new_boot_id:st.new_boot_id,new_boot0_seq:2,conduit:st.conduit,function:SYSTEM_RESET,ticket_sequence:seq,reused_log:true}, 1=>RebootObservation{new_boot_id:st.old_boot_id,new_boot0_seq:2,conduit:st.conduit,function:SYSTEM_RESET,ticket_sequence:seq,reused_log:false}, 2=>RebootObservation{new_boot_id:st.new_boot_id,new_boot0_seq:2,conduit:st.conduit,function:SYSTEM_RESET,ticket_sequence:seq,reused_log:false}, _=>return el0::Step::Exit(0xbad), }; match coord.observe_reboot(obs){Ok(s)=>s.verified_reboots,Err(e)=>REFUSED|e as u64} } SYS_EXIT=>return el0::Step::Exit(frame.arg(0)), _=>return el0::Step::Exit(0xbad), }; if st.calls<7{word(st.base+st.calls,r);st.calls+=1} frame.ret(r);el0::Step::Resume } #[inline(never)] fn run_program(allocator:&mut super::memory::FrameAllocator,tables:&super::mmu::Tables,boot:&super::BootData,id:el0::Identity,image:&[u8],base:usize)->Result{ let st=state(); st.user=Some(unsafe{tables.build_user_space(allocator,boot.hardware.ram_base,boot.hardware.ram_size)}.map_err(|_|"k2135-user")?); let user=st.user.as_ref().unwrap(); if !user.contract_valid(tables.root){return Err("k2135-user-contract")} unsafe{el0::load(user,image)}?; st.work=0;st.calls=0;st.base=base; let mut handler=dispatch; let outcome=unsafe{el0::run(st.user.as_ref().unwrap(),id,[0,0],&mut handler)}; let user=st.user.take().unwrap(); unsafe{user.release_after_tlbi(allocator,tables.root)}.map_err(|_|"k2135-release")?; Ok(outcome) } #[inline(never)] fn record_shell(shell:&el0::Outcome){ let st=state(); let as1=st.authority.as_ref().unwrap().status(); let cs1=st.coord.as_ref().unwrap().status(); for(j,v)in[shell.exit,shell.syscalls,shell.fault,as1.denied,as1.issued,cs1.phase as u64,cs1.armed,cs1.refused].into_iter().enumerate(){word(24+j,v)} } #[inline(never)] fn finish_stage(shell:&el0::Outcome,pid1:&el0::Outcome,free_before:u64,free_after:u64)->Result<(),&'static str>{ let st=state(); let as2=st.authority.as_ref().unwrap().status(); let cs2=st.coord.as_ref().unwrap().status(); for(j,v)in[pid1.exit,pid1.syscalls,pid1.fault,as2.issued,as2.committed,as2.denied,as2.pending,cs2.old_boot_id,cs2.new_boot_id,cs2.old_boot0_seq,cs2.new_boot0_seq,cs2.phase as u64,cs2.conduit,cs2.version as u64,cs2.armed,cs2.verified_reboots,cs2.refused,cs2.last_ticket,SYSTEM_RESET].into_iter().enumerate(){word(40+j,v)} word(3,free_after); if free_after!=free_before{return Err("k2135-frame-accounting")} if shell.exit!=4||pid1.exit!=0x202||cs2.phase!=Phase::RebootVerified||cs2.verified_reboots!=1||cs2.refused!=3||as2.committed!=2{return Err("k2135-outcome")} checkpoint(3); el0::write_fields("TURKOS/OS/K2135-PSCI-RESET:",&[("OLD_BOOT",st.old_boot_id),("NEW_BOOT",st.new_boot_id),("BOOT0_SEQ",cs2.new_boot0_seq),("CONDUIT",cs2.conduit),("VERSION",cs2.version as u64),("VERIFIED",cs2.verified_reboots),("REFUSED",cs2.refused),("FREE_EQUAL",1)]); Ok(()) } #[inline(never)] fn run_inner(runtime:&mut super::OsK660Runtime,tables:&super::mmu::Tables,boot:&super::BootData)->Result<(),&'static str>{ let allocator=&mut runtime.memory_mut().map_err(|_|"k2135-memory")?.allocator; let free_before=allocator.remaining_pages();word(0,MAGIC);word(2,free_before as u64); probe_stage(boot)?; checkpoint(1); let shell=run_program(allocator,tables,boot,el0::Identity{pid:9,generation:0x2135_0009},SHELL_TOOL,16)?; record_shell(&shell); checkpoint(2); let pid1=run_program(allocator,tables,boot,PID1_ID,PID1,32)?; let free_after=allocator.remaining_pages(); finish_stage(&shell,&pid1,free_before,free_after) } extern "C" fn entry(runtime:*mut super::OsK660Runtime,tables:*const super::mmu::Tables,boot:*const super::BootData,out:*mut Result<(),&'static str>){ unsafe{*out=run_inner(&mut *runtime,&*tables,&*boot)} } #[inline(never)] pub fn run(runtime:&mut super::OsK660Runtime,tables:&super::mmu::Tables,boot:&super::BootData)->Result<(),&'static str>{ let mut out:Result<(),&'static str>=Ok(()); let top=(addr_of!(super::OS_K900_STACK) as u64+4096+65536-16)&!15; unsafe{asm!( "mov x9, sp", "mov sp, x4", "stp x9, x30, [sp, #-16]!", "blr x5", "ldp x9, x30, [sp], #16", "mov sp, x9", in("x0")runtime as *mut _, in("x1")tables as *const _, in("x2")boot as *const _, in("x3")&mut out as *mut _, in("x4")top, in("x5")entry as usize, clobber_abi("C"), )}; out }