// K2285 guest experiment: Pseudo-terminal byte stream (`guest_terminal.pty`) // in the product kernel runtime. Enforces "PTY taraf ömrü ve backpressure // açıktır": an EL0 negative actor (`k2285-closedreader`) attempts to commit a // PTY byte stream with reader closed while writer is full ("Reader kapanır // writer dolu"), unbounded backpressure overflow, duplicate/missing EOF wake // delivery ("EOF hata ve wake tek teslim"), PTY ring capacity mismatch, and // shell stream bytes mismatch ("Shell reader writer"); all five are refused and // `committed_receipt()` remains None. Then EL0 `k2285-shellpty` commits a // 4096-byte PTY ring with 512 shell stream bytes, 2 backpressure events, and 1 // single-delivery EOF/wake notification, 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::guest_terminal_pty::{ compute_guest_terminal_pty_binding_digest, GuestTerminalPtyCoordinator, GuestTerminalPtyRequest, EXPECTED_BACKPRESSURE_EVENTS, EXPECTED_EOF_WAKE_DELIVERIES, EXPECTED_PTY_RING_CAPACITY_BYTES, EXPECTED_SHELL_STREAM_BYTES, }; use core::arch::asm; use core::ptr::{addr_of, addr_of_mut, read_volatile, write_volatile}; #[unsafe(no_mangle)] static mut OS_K2285_EVIDENCE: [u64; 64] = [0; 64]; #[unsafe(no_mangle)] static mut OS_K2285_CAPTURE_GATE: u8 = 0; static CLOSEDREADER: &[u8] = include_bytes!("k2285-closedreader.bin"); static SHELLPTY: &[u8] = include_bytes!("k2285-shellpty.bin"); const MAGIC: u64 = 0x4b32_3238_355f_5054; // "K2285_PT" const SYS_EXIT: u64 = 1; const SYS_GUEST_TERMINAL_PTY: u64 = 62; const SYS_GUEST_TERMINAL_PTY_RECEIPT: u64 = 63; const REFUSED: u64 = 1 << 63; const PREVIOUS_PTY_EPOCH: u64 = 46; const PTY_EPOCH: u64 = 47; const PTY_SESSION_DIGEST: u64 = 0x2285_5054_5953_4553; const CLOSEDREADER_ID: el0::Identity = el0::Identity { pid: 9, generation: 0x2285_0009, }; const SHELLPTY_ID: el0::Identity = el0::Identity { pid: 1, generation: 0x2285_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_K2285_EVIDENCE).cast::().add(i), v, ) } } #[inline(never)] fn checkpoint(stage: u64) { word(1, stage); unsafe { write_volatile(addr_of_mut!(OS_K2285_CAPTURE_GATE), 0) }; el0::write_fields("TURKOS/OS/K2285: CAPTURE", &[("STAGE", stage)]); while unsafe { read_volatile(addr_of!(OS_K2285_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_guest_terminal_pty_binding_digest( EXPECTED_PTY_RING_CAPACITY_BYTES, EXPECTED_SHELL_STREAM_BYTES, EXPECTED_BACKPRESSURE_EVENTS, EXPECTED_EOF_WAKE_DELIVERIES, PTY_EPOCH, PTY_SESSION_DIGEST, ); let expected_receipt = expected_binding ^ 0x2285_ACE0_5054_5931; let request = GuestTerminalPtyRequest { reader_closed_while_writer_full: false, unbounded_backpressure_overflow: false, eof_wake_deliveries: EXPECTED_EOF_WAKE_DELIVERIES, pty_ring_capacity_bytes: EXPECTED_PTY_RING_CAPACITY_BYTES, shell_stream_bytes: EXPECTED_SHELL_STREAM_BYTES, backpressure_events: EXPECTED_BACKPRESSURE_EVENTS, pty_epoch: PTY_EPOCH, previous_pty_epoch: PREVIOUS_PTY_EPOCH, pty_session_digest: PTY_SESSION_DIGEST, pty_binding_digest: expected_binding, }; let coord = GuestTerminalPtyCoordinator::new(); for (j, v) in [ EXPECTED_PTY_RING_CAPACITY_BYTES as u64, EXPECTED_SHELL_STREAM_BYTES as u64, u64::from(coord.is_committed()), EXPECTED_BACKPRESSURE_EVENTS as u64, EXPECTED_EOF_WAKE_DELIVERIES as u64, PTY_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_GUEST_TERMINAL_PTY => match frame.arg(0) { 0 => match coord.commit_pty_stream(&base_req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), }, 1 => { let mut req = base_req; req.reader_closed_while_writer_full = true; match coord.commit_pty_stream(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 2 => { let mut req = base_req; req.unbounded_backpressure_overflow = true; match coord.commit_pty_stream(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 3 => { let mut req = base_req; req.eof_wake_deliveries = 2; match coord.commit_pty_stream(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 4 => { let mut req = base_req; req.pty_ring_capacity_bytes = 1024; match coord.commit_pty_stream(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } _ => { let mut req = base_req; req.shell_stream_bytes = 0; match coord.commit_pty_stream(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } }, SYS_GUEST_TERMINAL_PTY_RECEIPT => match coord.committed_receipt() { Some(_) => base_req.pty_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(|_| "k2285-user")?, ); let user = st.user.as_ref().unwrap(); if !user.contract_valid(tables.root) { return Err("k2285-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(|_| "k2285-release")?; Ok(outcome) } #[inline(never)] fn record_closedreader(closedreader: &el0::Outcome) { let coord = state().coord.as_ref().unwrap(); for (j, v) in [ closedreader.exit, closedreader.syscalls, closedreader.fault, coord.rejected_total(), u64::from(coord.is_committed()), coord.lifecycle_backpressure_refusals(), coord.stream_geometry_refusals(), 0, ] .into_iter() .enumerate() { word(20 + j, v); } } #[inline(never)] fn finish_stage( closedreader: &el0::Outcome, shellpty: &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("k2285-missing-receipt")?; for (j, v) in [ shellpty.exit, shellpty.syscalls, shellpty.fault, u64::from(rec.single_eof_wake_verified), u64::from(coord.is_committed()), rec.pty_ring_capacity_bytes as u64, rec.shell_stream_bytes as u64, rec.backpressure_events as u64, rec.eof_wake_deliveries as u64, expected_binding, rec.receipt_digest, coord.lifecycle_backpressure_refusals(), coord.stream_geometry_refusals(), 0, coord.rejected_total(), coord.committed_total(), rec.pty_epoch, ] .into_iter() .enumerate() { word(36 + j, v); } word(3, free_after); if free_after != free_before { return Err("k2285-frame-accounting"); } if closedreader.exit != 0x2284 || shellpty.exit != 83 || !coord.is_committed() || !rec.single_eof_wake_verified || rec.pty_ring_capacity_bytes != EXPECTED_PTY_RING_CAPACITY_BYTES || rec.shell_stream_bytes != EXPECTED_SHELL_STREAM_BYTES || rec.backpressure_events != EXPECTED_BACKPRESSURE_EVENTS || rec.eof_wake_deliveries != EXPECTED_EOF_WAKE_DELIVERIES || rec.pty_epoch != PTY_EPOCH || rec.receipt_digest != expected_receipt || coord.lifecycle_backpressure_refusals() != 3 || coord.stream_geometry_refusals() != 2 || coord.rejected_total() != 6 || coord.committed_total() != 1 { return Err("k2285-outcome"); } checkpoint(3); el0::write_fields( "TURKOS/OS/K2285-GUEST-TERMINAL-PTY:", &[ ("COMMITTED", u64::from(coord.is_committed())), ("RING", rec.pty_ring_capacity_bytes as u64), ("STREAM", rec.shell_stream_bytes as u64), ("BACKPRESSURE", rec.backpressure_events as u64), ("EOF_WAKE", rec.eof_wake_deliveries as u64), ("EPOCH", rec.pty_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(|_| "k2285-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 closedreader = run_program( allocator, tables, boot, CLOSEDREADER_ID, CLOSEDREADER, [expected_binding, expected_receipt], 12, )?; record_closedreader(&closedreader); checkpoint(2); let shellpty = run_program( allocator, tables, boot, SHELLPTY_ID, SHELLPTY, [expected_binding, expected_receipt], 28, )?; let free_after = allocator.remaining_pages() as u64; finish_stage( &closedreader, &shellpty, 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 }