//! Worker component only. This never claims native device shutdown acceptance. use super::{BootData,OsK660Runtime,mmu,feature_el0 as el0}; use core::{arch::asm,ptr::{addr_of,addr_of_mut,read_volatile,write_volatile}}; #[path="k2155-workers.rs"] mod workers; #[path="k2150-capture-protocol.rs"] mod protocol; #[unsafe(no_mangle)] static mut OS_K2155_EVIDENCE:[u64;32]=[0;32]; #[unsafe(no_mangle)] static mut OS_K2155_CAPTURE_GATE:u8=0; fn word(index:usize,value:u64){unsafe{write_volatile(addr_of_mut!(OS_K2155_EVIDENCE).cast::().add(index),value)}} fn checkpoint(boot:&BootData,stage:u64){ word(1,stage); unsafe{write_volatile(addr_of_mut!(OS_K2155_CAPTURE_GATE),0);asm!("dsb sy",options(nostack,preserves_flags))}; el0::write_fields("TURKOS/OS/K2155: CAPTURE",&[("STAGE",stage)]); let mut gate=protocol::Release::new(stage as u32); loop{ let flags=unsafe{read_volatile((boot.hardware.uart_base+0x18)as*const u32)}; if flags&(1<<4)==0{ let data=unsafe{read_volatile(boot.hardware.uart_base as*const u32)}; if data&0xf00!=0{panic!("k2155-capture-uart-error")} if gate.push(data as u8){break} }else{core::hint::spin_loop()} } unsafe{write_volatile(addr_of_mut!(OS_K2155_CAPTURE_GATE),1)}; el0::write_fields("TURKOS/OS/K2155: RELEASE",&[("STAGE",stage)]); } fn inner(runtime:&mut OsK660Runtime,tables:&mmu::Tables,boot:&BootData)->Result<(),&'static str>{ let memory=runtime.memory_mut().map_err(|_|"k2155-worker-memory")?; let before=memory.allocator.remaining_pages();word(0,0x4b32313535574b52);word(2,before); let mut workers=workers::Workers::open(memory,tables)?; word(3,workers.free_pages());word(4,workers.live_threads()?as u64); workers.observe()?; let mut capture=|stage|checkpoint(boot,stage); capture(1); let mut executed=0u64; let mut work=|index:usize|{ let mut value=index as u64+1; for n in 0..4096u64{value=value.rotate_left(7)^n;core::hint::black_box(value);} word(8+index,value);executed+=1;Ok(()) }; workers.work(&mut work,&mut capture)?; if executed!=2{return Err("k2155-worker-task-count")} word(5,executed);workers.observe()?;capture(2); workers.stop(&mut capture)?; word(6,workers.live_threads()?as u64);word(7,workers.free_pages()); let mut illegal=|_|{word(11,1);Err("k2155-worker-work-after-stop")}; if workers.work(&mut illegal,&mut capture)!=Err("k2155-worker-admission-closed"){return Err("k2155-worker-admission")} word(10,1);workers.observe()?;capture(3); let memory=workers.release(&mut capture)?; let after=memory.allocator.remaining_pages();word(12,after); if before!=after{return Err("k2155-worker-final-accounting")} word(13,1);capture(4); el0::write_fields("TURKOS/OS/K2155-WORKERS:",&[("EXECUTED",executed),("STOPPED",2),("RECLAIMED_PAGES",10),("FREE_EQUAL",1)]); Ok(()) } extern "C" fn entry(runtime:*mut OsK660Runtime,tables:*const mmu::Tables,boot:*const BootData,out:*mut Result<(),&'static str>){ unsafe{*out=inner(&mut*runtime,&*tables,&*boot)}; } pub fn run(runtime:&mut OsK660Runtime,tables:&mmu::Tables,boot:&BootData)->Result<(),&'static str>{ let mut out=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*const()as usize,clobber_abi("C"))};out }