//! The new product entry. Unsupported native services retain diagnostic evidence //! and fail; they never inherit success from the historical experiment chain. use super::*; pub(super) mod abi; mod d2_startup; mod d3_shell; mod d4_net_ipc; mod d5_smp; mod d6_accounting; mod d7_shutdown; mod i2_turkish; #[used] #[unsafe(no_mangle)] static mut PRODUCT_NATIVE_EVIDENCE: [u64; abi::WORDS] = [0; abi::WORDS]; #[used] #[unsafe(no_mangle)] static mut PRODUCT_NATIVE_SHUTDOWN_RELEASE: u64 = 0; static ENTRIES: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0); fn put(index: usize, value: u64) { assert!(index < abi::WORDS); unsafe { core::ptr::write_volatile(core::ptr::addr_of_mut!(PRODUCT_NATIVE_EVIDENCE).cast::().add(index), value); } } fn get(index: usize) -> u64 { unsafe { core::ptr::read_volatile(core::ptr::addr_of!(PRODUCT_NATIVE_EVIDENCE).cast::().add(index)) } } fn commit(index: usize, out: &[u64; 32]) { for (word, value) in out.iter().enumerate() { put(abi::BLOCK_BASE+index*abi::BLOCK_WORDS+word,*value); } } fn counter() -> u64 { let v:u64;unsafe{asm!("mrs {v}, CNTPCT_EL0",v=out(reg)v,options(nomem,nostack));}v } fn frequency() -> u64 { let v:u64;unsafe{asm!("mrs {v}, CNTFRQ_EL0",v=out(reg)v,options(nomem,nostack));}v } fn current_el() -> u64 { let v:u64;unsafe{asm!("mrs {v}, CurrentEL",v=out(reg)v,options(nomem,nostack));}v } fn d1(runtime:&mut OsK660Runtime,tables:&mmu::Tables,boot:&BootData,config:abi::BootConfig,out:&mut[u64;32])->Result<(),&'static str>{ unsafe extern "C" { static _image_start:u8; static _image_end:u8; } let start=core::ptr::addr_of!(_image_start) as u64; let end=core::ptr::addr_of!(_image_end) as u64; let magic=unsafe{core::ptr::read_volatile((start+0x38) as *const u32)}; let manifest=runtime.manifest(); if manifest.profile!=os_k660_policy::Profile::Product || manifest.probes!=0 {return Err("native-product-profile");} if current_el()!=4 || start!=0x40080000 || end<=start || end-start>8*1024*1024 || magic!=0x644d5241 {return Err("native-product-image-entry");} if !tables.owns_table_frame(tables.root) || runtime.snapshot().map_err(os_k660_policy::Error::tag)?.registered != [true;4] {return Err("native-product-foundation");} let free=runtime.memory().ok_or("native-product-memory")?.allocator.remaining_pages(); out[0]=abi::D1_MAGIC;out[1]=1;out[2]=start;out[3]=end-start; out[4]=boot.dtb_address as u64;out[5]=config.boot_id;out[6]=u64::from(config.route);out[7]=1; out[8]=ENTRIES.fetch_add(1,core::sync::atomic::Ordering::SeqCst)+1;out[9]=u64::from(magic); out[10]=free;out[11]=boot.dtb_size as u64;out[12]=current_el();out[13]=1; if out[8]!=1 {return Err("native-product-duplicate-entry");} put(13,free);Ok(()) } pub(super) fn boot_id()->Result{let value=get(2);if value==0{Err("native-product-no-boot-id")}else{Ok(value)}} pub(super) fn require_shutdown_prerequisites()->Result<(),&'static str>{ if get(4)&0xbf!=0xbf {return Err("native-product-shutdown-prerequisites");} if !d5_smp::stopped(){return Err("native-product-cpus-not-stopped");} d6_accounting::require_reconciled() } pub(super) fn checkpoint_shutdown(out:&[u64;32])->Result<(),&'static str>{ require_shutdown_prerequisites()?;commit(6,out);put(7,3);put(8,counter()); unsafe{asm!("dsb sy",options(nostack));}write_line("TURKOS/PRODUCT: EVIDENCE_READY"); while unsafe{core::ptr::read_volatile(core::ptr::addr_of!(PRODUCT_NATIVE_SHUTDOWN_RELEASE))}!=1{unsafe{asm!("yield",options(nomem,nostack));}} Ok(()) } pub(super) fn run(runtime:&mut OsK660Runtime,tables:&mmu::Tables,boot:&BootData)->Result<(),&'static str>{ let blob=unsafe{core::slice::from_raw_parts(boot.dtb_address as *const u8,boot.dtb_size)}; let config=abi::BootConfig::parse(blob)?; for i in 0..abi::WORDS {put(i,0);} for (i,v) in [abi::MAGIC,abi::VERSION,config.boot_id,u64::from(config.route),0,0,u64::from(config.stage_mask),0,counter(),frequency(),boot.dtb_address as u64,boot.dtb_size as u64,current_el()].iter().enumerate(){put(i,*v);} let mut out=[0u64;32]; d1(runtime,tables,boot,config,&mut out)?;commit(0,&out);put(4,1); // I2 executes before the final shutdown checkpoint even though its block is 7. for stage in [1usize,2,3,4,5,7,6] { if config.stage_mask&(1<d2_startup::run(runtime,tables,boot,&mut out), 2=>d3_shell::run(runtime,tables,boot,&mut out), 3=>d4_net_ipc::run(runtime,tables,boot,&mut out), 4=>d5_smp::run(runtime,tables,boot,&mut out), 5=>d6_accounting::run(runtime,tables,boot,&mut out), 6=>d7_shutdown::run(runtime,tables,boot,&mut out), 7=>i2_turkish::run(runtime,tables,boot,&mut out), _=>Err("native-product-stage"), }; commit(stage,&out); if let Err(reason)=result { put(5,(stage+1) as u64);put(7,2);put(8,counter()); unsafe{asm!("dsb sy",options(nostack));} write_str("TURKOS/PRODUCT: INCOMPLETE reason=");write_line(reason); write_line("TURKOS/PRODUCT: EVIDENCE_READY");return Err(reason); } put(4,get(4)|(1<