// K2330 guest experiment: Editor RO load and temporary staged save // (`editor.load_save`) in the product kernel runtime. Enforces "Save kaynak // dosyayı partial ezmez": an EL0 negative actor (`k2330-flusheio`) attempts to // commit an editor load/save operation with final flush EIO ("Son flush EIO"), // source file partially overwritten, missing explicit unsaved or old file state // ("Eski dosya veya açık unsaved durum"), temp stage writes mismatch ("Geçici // dosyaya tam yazı"), and atomic replace saves mismatch; all five are refused // and `committed_receipt()` remains None. Then EL0 `k2330-tempsave` commits 2 // full temporary-stage writes and 2 atomic-replace saves with 1 EIO flush // rollback check and 0 partial source overwrites, verifies the committed // binding digest, refuses a duplicate commit, and returns all user-space // frames. use super::feature_el0 as el0; use turkos_kernel::runtime::editor_load_save::{ compute_editor_load_save_binding_digest, EditorLoadSaveCoordinator, EditorLoadSaveRequest, EXPECTED_ATOMIC_REPLACE_SAVES, EXPECTED_EIO_FLUSH_ROLLBACKS, EXPECTED_PARTIAL_SOURCE_OVERWRITES, EXPECTED_TEMP_STAGE_WRITES, }; use core::arch::asm; use core::ptr::{addr_of, addr_of_mut, read_volatile, write_volatile}; #[unsafe(no_mangle)] static mut OS_K2330_EVIDENCE: [u64; 64] = [0; 64]; #[unsafe(no_mangle)] static mut OS_K2330_CAPTURE_GATE: u8 = 0; static FLUSHEIO: &[u8] = include_bytes!("k2330-flusheio.bin"); static TEMPSAVE: &[u8] = include_bytes!("k2330-tempsave.bin"); const MAGIC: u64 = 0x4b32_3333_305f_4c53; // "K2330_LS" const SYS_EXIT: u64 = 1; const SYS_EDITOR_LOAD_SAVE: u64 = 80; const SYS_EDITOR_LOAD_SAVE_RECEIPT: u64 = 81; const REFUSED: u64 = 1 << 63; const PREVIOUS_LOAD_SAVE_EPOCH: u64 = 55; const LOAD_SAVE_EPOCH: u64 = 56; const SAVE_DIGEST: u64 = 0x2330_4544_4c53_5356; const FLUSHEIO_ID: el0::Identity = el0::Identity { pid: 9, generation: 0x2330_0009, }; const TEMPSAVE_ID: el0::Identity = el0::Identity { pid: 1, generation: 0x2330_0001, }; struct State { coord: Option, user: Option, request: Option, calls: usize, base: usize, } static mut STATE: State = State { coord: None, user: None, request: None, 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_K2330_EVIDENCE).cast::().add(i), v, ) } } #[inline(never)] fn checkpoint(stage: u64) { word(1, stage); unsafe { write_volatile(addr_of_mut!(OS_K2330_CAPTURE_GATE), 0) }; el0::write_fields("TURKOS/OS/K2330: CAPTURE", &[("STAGE", stage)]); while unsafe { read_volatile(addr_of!(OS_K2330_CAPTURE_GATE)) } == 0 { unsafe { asm!("yield", options(nomem, nostack, preserves_flags)) } } } #[inline(never)] fn setup_stage() -> Result<(u64, u64), &'static str> { let expected_binding = compute_editor_load_save_binding_digest( EXPECTED_TEMP_STAGE_WRITES, EXPECTED_ATOMIC_REPLACE_SAVES, EXPECTED_EIO_FLUSH_ROLLBACKS, EXPECTED_PARTIAL_SOURCE_OVERWRITES, LOAD_SAVE_EPOCH, SAVE_DIGEST, ); let expected_receipt = expected_binding ^ 0x2330_ACE0_4c53_5631; let request = EditorLoadSaveRequest { final_flush_eio: false, source_file_partially_overwritten: false, old_file_or_explicit_unsaved_state_preserved: true, temp_stage_writes: EXPECTED_TEMP_STAGE_WRITES, atomic_replace_saves: EXPECTED_ATOMIC_REPLACE_SAVES, eio_flush_rollbacks: EXPECTED_EIO_FLUSH_ROLLBACKS, load_save_epoch: LOAD_SAVE_EPOCH, previous_load_save_epoch: PREVIOUS_LOAD_SAVE_EPOCH, save_digest: SAVE_DIGEST, load_save_binding_digest: expected_binding, }; let coord = EditorLoadSaveCoordinator::new(); for (j, v) in [ EXPECTED_TEMP_STAGE_WRITES as u64, EXPECTED_ATOMIC_REPLACE_SAVES as u64, u64::from(coord.is_committed()), EXPECTED_EIO_FLUSH_ROLLBACKS as u64, EXPECTED_PARTIAL_SOURCE_OVERWRITES as u64, LOAD_SAVE_EPOCH, expected_binding, expected_receipt, ] .into_iter() .enumerate() { word(4 + j, v); } let st = state(); st.request = Some(request); st.coord = Some(coord); Ok((expected_binding, expected_receipt)) } fn dispatch(_caller: el0::Identity, frame: &mut el0::Frame) -> el0::Step { let st = state(); let base_req = *st.request.as_ref().unwrap(); let coord = st.coord.as_mut().unwrap(); let r = match frame.number() { SYS_EDITOR_LOAD_SAVE => match frame.arg(0) { 0 => match coord.commit_load_save_operation(&base_req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), }, 1 => { let mut req = base_req; req.final_flush_eio = true; match coord.commit_load_save_operation(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 2 => { let mut req = base_req; req.source_file_partially_overwritten = true; match coord.commit_load_save_operation(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 3 => { let mut req = base_req; req.old_file_or_explicit_unsaved_state_preserved = false; match coord.commit_load_save_operation(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 4 => { let mut req = base_req; req.temp_stage_writes = 0; match coord.commit_load_save_operation(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } _ => { let mut req = base_req; req.atomic_replace_saves = 0; match coord.commit_load_save_operation(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } }, SYS_EDITOR_LOAD_SAVE_RECEIPT => match coord.committed_receipt() { Some(_) => base_req.load_save_binding_digest, None => 0, }, SYS_EXIT => return el0::Step::Exit(frame.arg(0)), _ => return el0::Step::Exit(0xbad), }; if st.calls < 8 { 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], args: [u64; 2], 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(|_| "k2330-user")?, ); let user = st.user.as_ref().unwrap(); if !user.contract_valid(tables.root) { return Err("k2330-user-contract"); } unsafe { el0::load(user, image) }?; st.calls = 0; st.base = base; let mut handler = dispatch; let outcome = unsafe { el0::run(st.user.as_ref().unwrap(), id, args, &mut handler) }; let user = st.user.take().unwrap(); unsafe { user.release_after_tlbi(allocator, tables.root) }.map_err(|_| "k2330-release")?; Ok(outcome) } #[inline(never)] fn record_flusheio(flusheio: &el0::Outcome) { let coord = state().coord.as_ref().unwrap(); for (j, v) in [ flusheio.exit, flusheio.syscalls, flusheio.fault, coord.rejected_total(), u64::from(coord.is_committed()), coord.flush_safety_refusals(), coord.save_accounting_refusals(), 0, ] .into_iter() .enumerate() { word(20 + j, v); } } #[inline(never)] fn finish_stage( flusheio: &el0::Outcome, tempsave: &el0::Outcome, free_before: u64, free_after: u64, expected_binding: u64, expected_receipt: u64, ) -> Result<(), &'static str> { let coord = state().coord.as_ref().unwrap(); let rec = coord.committed_receipt().ok_or("k2330-missing-receipt")?; for (j, v) in [ tempsave.exit, tempsave.syscalls, tempsave.fault, u64::from(rec.old_file_or_explicit_unsaved_state_preserved), u64::from(coord.is_committed()), rec.temp_stage_writes as u64, rec.atomic_replace_saves as u64, rec.eio_flush_rollbacks as u64, rec.partial_source_overwrites as u64, expected_binding, rec.receipt_digest, coord.flush_safety_refusals(), coord.save_accounting_refusals(), 0, coord.rejected_total(), coord.committed_total(), rec.load_save_epoch, ] .into_iter() .enumerate() { word(36 + j, v); } word(3, free_after); if free_after != free_before { return Err("k2330-frame-accounting"); } if flusheio.exit != 0x2329 || tempsave.exit != 92 || !coord.is_committed() || !rec.old_file_or_explicit_unsaved_state_preserved || rec.temp_stage_writes != EXPECTED_TEMP_STAGE_WRITES || rec.atomic_replace_saves != EXPECTED_ATOMIC_REPLACE_SAVES || rec.eio_flush_rollbacks != EXPECTED_EIO_FLUSH_ROLLBACKS || rec.partial_source_overwrites != EXPECTED_PARTIAL_SOURCE_OVERWRITES || rec.load_save_epoch != LOAD_SAVE_EPOCH || rec.receipt_digest != expected_receipt || coord.flush_safety_refusals() != 3 || coord.save_accounting_refusals() != 2 || coord.rejected_total() != 6 || coord.committed_total() != 1 { return Err("k2330-outcome"); } checkpoint(3); el0::write_fields( "TURKOS/OS/K2330-EDITOR-LOAD-SAVE:", &[ ("COMMITTED", u64::from(coord.is_committed())), ("STAGE_WRITES", rec.temp_stage_writes as u64), ("ATOMIC_SAVES", rec.atomic_replace_saves as u64), ("ROLLBACKS", rec.eio_flush_rollbacks as u64), ("PARTIAL_OVERWRITES", rec.partial_source_overwrites as u64), ("EPOCH", rec.load_save_epoch), ("BINDING", expected_binding), ("RECEIPT", rec.receipt_digest), ("REJECTED", coord.rejected_total()), ("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(|_| "k2330-memory")?.allocator; let free_before = allocator.remaining_pages() as u64; word(0, MAGIC); word(2, free_before); let (expected_binding, expected_receipt) = setup_stage()?; checkpoint(1); let flusheio = run_program( allocator, tables, boot, FLUSHEIO_ID, FLUSHEIO, [expected_binding, expected_receipt], 12, )?; record_flusheio(&flusheio); checkpoint(2); let tempsave = run_program( allocator, tables, boot, TEMPSAVE_ID, TEMPSAVE, [expected_binding, expected_receipt], 28, )?; let free_after = allocator.remaining_pages() as u64; finish_stage( &flusheio, &tempsave, free_before, free_after, expected_binding, expected_receipt, ) } 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 }