// K2125 guest experiment: the power authority in the product kernel runtime. // An EL0 shell tool is denied Off and Reset (and a cancel of a ticket it does // not hold) and keeps working; an EL0 service then runs undisturbed; the EL0 // PID1 program holds the grant, obtains one Reset and one Off ticket (a second // request while pending is refused) and cancels both. No PSCI call is made. use super::feature_el0 as el0; use turkos_kernel::runtime::guest_power_capability::{ALL,Authority,Error,Identity,Op,Ticket}; use core::ptr::{addr_of,addr_of_mut,read_volatile,write_volatile}; #[unsafe(no_mangle)] static mut OS_K2125_EVIDENCE:[u64;64]=[0;64]; #[unsafe(no_mangle)] static mut OS_K2125_CAPTURE_GATE:u8=0; static SHELL_TOOL:&[u8]=include_bytes!("k2125-shell-tool.bin"); static SERVICE:&[u8]=include_bytes!("k2125-service.bin"); static PID1:&[u8]=include_bytes!("k2125-pid1.bin"); const MAGIC:u64=0x4b32_3132_355f_5043; // "K2125_PC" const SYS_EXIT:u64=1; const SYS_POWER:u64=16; const SYS_CANCEL:u64=17; const SYS_WORK:u64=18; const REFUSED:u64=1<<63; const PID1_ID:el0::Identity=el0::Identity{pid:1,generation:0x2125_0001}; fn word(i:usize,v:u64){unsafe{write_volatile(addr_of_mut!(OS_K2125_EVIDENCE).cast::().add(i),v)}} fn checkpoint(stage:u64){ word(1,stage); unsafe{write_volatile(addr_of_mut!(OS_K2125_CAPTURE_GATE),0)}; el0::write_fields("TURKOS/OS/K2125: CAPTURE",&[("STAGE",stage)]); while unsafe{read_volatile(addr_of!(OS_K2125_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}} 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(|_|"k2125-memory")?.allocator; let free_before=allocator.remaining_pages();word(0,MAGIC);word(2,free_before as u64); let mut authority=Authority::new(identity(PID1_ID),ALL).map_err(|_|"k2125-authority")?; let mut held:Option<(el0::Identity,Ticket)>=None; checkpoint(1); let mut program=|authority:&mut Authority,held:&mut Option<(el0::Identity,Ticket)>,id:el0::Identity,image:&[u8],base:usize|->Result{ let user=unsafe{tables.build_user_space(allocator,boot.hardware.ram_base,boot.hardware.ram_size)}.map_err(|_|"k2125-user")?; if !user.contract_valid(tables.root){return Err("k2125-user-contract")} unsafe{el0::load(&user,image)}?; let (mut work,mut calls)=(0u64,0usize); let outcome=unsafe{el0::run(&user,id,[0,0],&mut |caller,frame|{ let r=match frame.number(){ SYS_POWER=>{ 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();*held=Some((caller,t));s}Err(e)=>REFUSED|e as u64} } SYS_CANCEL=>match held.take(){ Some((owner,t)) if owner==caller=>match authority.cancel(t){Ok(())=>0,Err((e,t))=>{*held=Some((owner,t));REFUSED|e as u64}}, other=>{*held=other;REFUSED|Error::Denied as u64} }, SYS_WORK=>{work+=1;work} SYS_EXIT=>return el0::Step::Exit(frame.arg(0)), _=>return el0::Step::Exit(0xbad), }; if calls<6{word(base+calls,r);calls+=1} frame.ret(r);el0::Step::Resume })}; unsafe{user.release_after_tlbi(allocator,tables.root)}.map_err(|_|"k2125-release")?; Ok(outcome) }; let shell=program(&mut authority,&mut held,el0::Identity{pid:9,generation:0x2125_0009},SHELL_TOOL,8)?; let after_shell=authority.status(); let service=program(&mut authority,&mut held,el0::Identity{pid:10,generation:0x2125_000a},SERVICE,16)?; for(j,v)in[shell.exit,shell.syscalls,shell.fault,service.exit,service.syscalls,service.fault,after_shell.denied,after_shell.issued,after_shell.pending].into_iter().enumerate(){word(24+j,v)} checkpoint(2); let pid1=program(&mut authority,&mut held,PID1_ID,PID1,34)?; let s=authority.status(); for(j,v)in[pid1.exit,pid1.syscalls,pid1.fault,s.holder.pid,s.holder.generation,s.grant,s.ops,s.pending,s.issued,s.committed,s.cancelled,s.denied,u64::from(s.revoked),u64::from(held.is_some())].into_iter().enumerate(){word(40+j,v)} let free_after=allocator.remaining_pages();word(3,free_after as u64); if free_after!=free_before{return Err("k2125-frame-accounting")} if shell.exit!=8||service.exit!=4||pid1.exit!=0x102||s.issued!=2||s.cancelled!=2||s.committed!=0||s.denied!=2{return Err("k2125-outcome")} checkpoint(3); el0::write_fields("TURKOS/OS/K2125-POWER-AUTHORITY:",&[("DENIED",s.denied),("SHELL_WORK",shell.exit),("SERVICE_WORK",service.exit),("TICKETS",s.issued),("CANCELLED",s.cancelled),("COMMITTED",s.committed),("FREE_EQUAL",1)]); Ok(()) }