// K2140 guest experiment: unsupported/failed power fallback path in the // product kernel runtime. Queries real QEMU PSCI_FEATURES with an unsupported // function ID (-1), runs an EL0 shell tool that is denied power access, and // runs EL0 PID1 to verify immediate DegradedParked on an unsupported Off // profile (rejecting false-success and host-timeout diagnoses), followed by // bounded 2-attempt retry exhaustion on Reset and RetryLimit refusal on a // third attempt. use super::feature_el0 as el0; use turkos_kernel::runtime::guest_power_capability::{ALL,Authority,Identity,Op,Ticket}; use turkos_kernel::runtime::guest_power_fallback::{Diagnosis,Error,Fallback,Phase}; use core::arch::asm; use core::ptr::{addr_of,addr_of_mut,read_volatile,write_volatile}; #[unsafe(no_mangle)] static mut OS_K2140_EVIDENCE:[u64;64]=[0;64]; #[unsafe(no_mangle)] static mut OS_K2140_CAPTURE_GATE:u8=0; static SHELL_TOOL:&[u8]=include_bytes!("k2140-shell-tool.bin"); static PID1:&[u8]=include_bytes!("k2140-pid1.bin"); const MAGIC:u64=0x4b32_3134_305f_5046; // "K2140_PF" const PSCI_FEATURES:u64=0x8400_000a; const UNSUPPORTED_FN:u64=0x8400_00ff; const SYS_EXIT:u64=1; const SYS_REQUEST:u64=16; const SYS_BEGIN_OFF_UNSUP:u64=17; const SYS_WORK:u64=18; const SYS_VERIFY_OFF:u64=19; const SYS_BEGIN_RESET_RETRY:u64=20; const SYS_FAIL_STEP:u64=21; const REFUSED:u64=1<<63; const PID1_ID:el0::Identity=el0::Identity{pid:1,generation:0x2140_0001}; struct State{ authority:Option, fb_off:Option, fb_rst:Option, held:Option<(el0::Identity,Ticket)>, user:Option, features_unsup:i32, work:u64, calls:usize, base:usize, } static mut STATE:State=State{authority:None,fb_off:None,fb_rst:None,held:None,user:None,features_unsup:-1,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_K2140_EVIDENCE).cast::().add(i),v)}} #[inline(never)] fn checkpoint(stage:u64){ word(1,stage); unsafe{write_volatile(addr_of_mut!(OS_K2140_CAPTURE_GATE),0)}; el0::write_fields("TURKOS/OS/K2140: CAPTURE",&[("STAGE",stage)]); while unsafe{read_volatile(addr_of!(OS_K2140_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()->Result<(),&'static str>{ let features_unsup=hvc(PSCI_FEATURES,UNSUPPORTED_FN) as u32 as i32; if features_unsup>=0{return Err("k2140-expected-unsupported")} let fb_off=Fallback::new(2).map_err(|_|"k2140-fb-off")?; let fb_rst=Fallback::new(2).map_err(|_|"k2140-fb-rst")?; for(j,v)in[features_unsup as u32 as u64,fb_off.status().max_attempts,fb_off.status().phase as u64,fb_rst.status().phase as u64].into_iter().enumerate(){word(4+j,v)} let st=state(); st.authority=Some(Authority::new(identity(PID1_ID),ALL).map_err(|_|"k2140-authority")?); st.fb_off=Some(fb_off); st.fb_rst=Some(fb_rst); st.held=None; st.features_unsup=features_unsup; Ok(()) } fn dispatch(caller:el0::Identity,frame:&mut el0::Frame)->el0::Step{ let st=state(); let authority=st.authority.as_mut().unwrap(); let fb_off=st.fb_off.as_mut().unwrap(); let fb_rst=st.fb_rst.as_mut().unwrap(); let features_unsup=st.features_unsup; 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_BEGIN_OFF_UNSUP=>match st.held.take(){ Some((owner,t)) if owner==caller=>{ let seq=t.sequence(); match authority.commit(t){ Ok(op)=>match fb_off.begin(op as u64,seq,features_unsup>=0){Ok(a)=>a as u64,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_VERIFY_OFF=>{ let s=fb_off.status(); let d=match frame.arg(0){ 0=>Diagnosis{op:s.op,ticket_sequence:s.ticket_sequence,reason:s.reason,attempts:s.attempts,last_code:s.last_code,guest_parked:true,claimed_success:true,host_timeout:false}, 1=>Diagnosis{op:s.op,ticket_sequence:s.ticket_sequence,reason:s.reason,attempts:s.attempts,last_code:s.last_code,guest_parked:false,claimed_success:false,host_timeout:true}, 2=>Diagnosis{op:s.op,ticket_sequence:s.ticket_sequence,reason:s.reason,attempts:s.attempts,last_code:s.last_code,guest_parked:true,claimed_success:false,host_timeout:false}, _=>return el0::Step::Exit(0xbad), }; code(fb_off.verify_degraded_park(d)) } SYS_BEGIN_RESET_RETRY=>match st.held.take(){ Some((owner,t)) if owner==caller=>{ let seq=t.sequence(); match authority.commit(t){ Ok(op)=>match fb_rst.begin(op as u64,seq,true){Ok(a)=>a as u64,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_FAIL_STEP=>match fb_rst.record_failure(features_unsup){Ok(a)=>a as u64,Err(e)=>REFUSED|e as u64}, SYS_EXIT=>return el0::Step::Exit(frame.arg(0)), _=>return el0::Step::Exit(0xbad), }; if st.calls<10{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(|_|"k2140-user")?); let user=st.user.as_ref().unwrap(); if !user.contract_valid(tables.root){return Err("k2140-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(|_|"k2140-release")?; Ok(outcome) } #[inline(never)] fn record_shell(shell:&el0::Outcome){ let st=state(); let as1=st.authority.as_ref().unwrap().status(); let fb_off=st.fb_off.as_ref().unwrap(); let fb_rst=st.fb_rst.as_ref().unwrap(); for(j,v)in[shell.exit,shell.syscalls,shell.fault,as1.denied,as1.issued,fb_off.status().phase as u64,fb_rst.status().phase as u64,fb_rst.status().retry_refusals].into_iter().enumerate(){word(16+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 s_off=st.fb_off.as_ref().unwrap().status(); let s_rst=st.fb_rst.as_ref().unwrap().status(); let rst_diag=st.fb_rst.as_ref().unwrap().verify_degraded_park(Diagnosis{op:s_rst.op,ticket_sequence:s_rst.ticket_sequence,reason:s_rst.reason,attempts:s_rst.attempts,last_code:s_rst.last_code,guest_parked:true,claimed_success:false,host_timeout:false}).is_ok(); for(j,v)in[pid1.exit,pid1.syscalls,pid1.fault,as2.issued,as2.committed,as2.denied,as2.pending,s_off.phase as u64,s_off.op,s_off.ticket_sequence,s_off.reason as u64,s_off.attempts,s_off.parked_count,s_rst.phase as u64,s_rst.op,s_rst.ticket_sequence,s_rst.reason as u64,s_rst.attempts,s_rst.retry_refusals,s_rst.parked_count,s_rst.last_code as u32 as u64,u64::from(rst_diag)].into_iter().enumerate(){word(36+j,v)} word(3,free_after); if free_after!=free_before{return Err("k2140-frame-accounting")} if shell.exit!=4||pid1.exit!=0x202||s_off.phase!=Phase::DegradedParked||s_rst.phase!=Phase::DegradedParked||s_rst.attempts!=2||s_rst.retry_refusals!=1||!rst_diag{return Err("k2140-outcome")} checkpoint(3); el0::write_fields("TURKOS/OS/K2140-POWER-FALLBACK:",&[("UNSUP_PARKED",s_off.parked_count),("RETRY_ATTEMPTS",s_rst.attempts),("RETRY_REFUSED",s_rst.retry_refusals),("FALSE_SUCCESS_REJECTED",1),("HOST_TIMEOUT_SEPARATED",1),("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(|_|"k2140-memory")?.allocator; let free_before=allocator.remaining_pages();word(0,MAGIC);word(2,free_before as u64); probe_stage()?; checkpoint(1); let shell=run_program(allocator,tables,boot,el0::Identity{pid:9,generation:0x2140_0009},SHELL_TOOL,8)?; record_shell(&shell); checkpoint(2); let pid1=run_program(allocator,tables,boot,PID1_ID,PID1,24)?; 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 }