// K2210 guest experiment: Virtio-GPU 2D resource creation and backing attachment // (`virtio_gpu.resources`) in the product kernel runtime. Enforces "Kaynak RAM // ve scanout boyu taşmasızdır": an EL0 negative actor (`k2210-overflower`) // attempts to allocate a resource with width exceeding MAX_RESOURCE_DIMENSION // ("Width height stride overflow"), stride smaller than width * 4, stride * // height multiplication overflow, mismatched backing byte count, and an // unsupported pixel format; all five are refused before reaching the device // ("Bozuk kaynak aygıta sunulmaz") and `committed_receipt()` remains None. Then // EL0 `k2210-allocator` creates a 640x480 RGBA resource ("640x480 RGBA kaynak", // 1_228_800 bytes, 300 pages), verifies the resource binding digest, refuses a // duplicate allocation, and returns all user-space frames. use super::feature_el0 as el0; use turkos_kernel::runtime::virtio_gpu_resources::{ compute_resource_binding, compute_scanout_receipt_digest, ResourcePhase, VirtioGpuResourceCoordinator, VirtioGpuResourceRequest, MAX_RESOURCE_DIMENSION, VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM, }; use core::arch::asm; use core::ptr::{addr_of, addr_of_mut, read_volatile, write_volatile}; #[unsafe(no_mangle)] static mut OS_K2210_EVIDENCE: [u64; 64] = [0; 64]; #[unsafe(no_mangle)] static mut OS_K2210_CAPTURE_GATE: u8 = 0; static OVERFLOWER: &[u8] = include_bytes!("k2210-overflower.bin"); static ALLOCATOR: &[u8] = include_bytes!("k2210-allocator.bin"); const MAGIC: u64 = 0x4b32_3231_305f_5253; // "K2210_RS" const SYS_EXIT: u64 = 1; const SYS_GPU_RESOURCE: u64 = 32; const SYS_GPU_RESOURCE_RECEIPT: u64 = 33; const REFUSED: u64 = 1 << 63; const RESOURCE_ID: u32 = 1; const SCANOUT_ID: u32 = 0; const WIDTH: u32 = 640; const HEIGHT: u32 = 480; const STRIDE: u32 = 2560; const BACKING_BYTES: u32 = 1_228_800; const BACKING_PAGES: u32 = 300; const OVERFLOWER_ID: el0::Identity = el0::Identity { pid: 9, generation: 0x2210_0009, }; const ALLOCATOR_ID: el0::Identity = el0::Identity { pid: 1, generation: 0x2210_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_K2210_EVIDENCE).cast::().add(i), v, ) } } #[inline(never)] fn checkpoint(stage: u64) { word(1, stage); unsafe { write_volatile(addr_of_mut!(OS_K2210_CAPTURE_GATE), 0) }; el0::write_fields("TURKOS/OS/K2210: CAPTURE", &[("STAGE", stage)]); while unsafe { read_volatile(addr_of!(OS_K2210_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_resource_binding( RESOURCE_ID, SCANOUT_ID, VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM, WIDTH, HEIGHT, STRIDE, BACKING_BYTES, ); let expected_scanout = compute_scanout_receipt_digest(expected_binding, BACKING_PAGES, SCANOUT_ID); let request = VirtioGpuResourceRequest { resource_id: RESOURCE_ID, scanout_id: SCANOUT_ID, format: VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM, width: WIDTH, height: HEIGHT, stride: STRIDE, backing_bytes: BACKING_BYTES, backing_pages: BACKING_PAGES, resource_binding_digest: expected_binding, }; let coord = VirtioGpuResourceCoordinator::new(); let s = coord.status(); for (j, v) in [ RESOURCE_ID as u64, s.phase as u64, u64::from(s.allocated), WIDTH as u64, HEIGHT as u64, BACKING_BYTES as u64, expected_binding, expected_scanout, ] .into_iter() .enumerate() { word(4 + j, v); } let st = state(); st.request = Some(request); st.coord = Some(coord); Ok((expected_binding, expected_scanout)) } 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_GPU_RESOURCE => match frame.arg(0) { 0 => match coord.allocate_and_attach(&base_req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), }, 1 => { let mut req = base_req; req.width = MAX_RESOURCE_DIMENSION + 1; match coord.allocate_and_attach(&req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), } } 2 => { let mut req = base_req; req.stride = 2556; match coord.allocate_and_attach(&req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), } } 3 => { let mut req = base_req; req.stride = u32::MAX & !3; match coord.allocate_and_attach(&req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), } } 4 => { let mut req = base_req; req.backing_bytes += 4096; match coord.allocate_and_attach(&req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), } } _ => { let mut req = base_req; req.format = 99; match coord.allocate_and_attach(&req) { Ok(rec) => rec.scanout_receipt_digest, Err(e) => REFUSED | (e as u64), } } }, SYS_GPU_RESOURCE_RECEIPT => match coord.committed_receipt() { Some(rec) => rec.resource_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(|_| "k2210-user")?, ); let user = st.user.as_ref().unwrap(); if !user.contract_valid(tables.root) { return Err("k2210-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(|_| "k2210-release")?; Ok(outcome) } #[inline(never)] fn record_overflower(overflower: &el0::Outcome) { let s = state().coord.as_ref().unwrap().status(); for (j, v) in [ overflower.exit, overflower.syscalls, overflower.fault, s.phase as u64, u64::from(s.allocated), s.overflow_refusals, s.backing_mismatch_refusals, s.format_or_id_refusals, ] .into_iter() .enumerate() { word(20 + j, v); } } #[inline(never)] fn finish_stage( overflower: &el0::Outcome, allocator_out: &el0::Outcome, free_before: u64, free_after: u64, expected_binding: u64, expected_scanout: u64, ) -> Result<(), &'static str> { let coord = state().coord.as_ref().unwrap(); let s = coord.status(); for (j, v) in [ allocator_out.exit, allocator_out.syscalls, allocator_out.fault, s.phase as u64, u64::from(s.allocated), s.resource_id as u64, s.width as u64, s.height as u64, s.backing_bytes as u64, s.resource_binding_digest, s.scanout_receipt_digest, s.overflow_refusals, s.backing_mismatch_refusals, s.format_or_id_refusals, s.rejected_total, s.allocated_total, s.backing_pages as u64, ] .into_iter() .enumerate() { word(36 + j, v); } word(3, free_after); if free_after != free_before { return Err("k2210-frame-accounting"); } if overflower.exit != 0x2209 || allocator_out.exit != 64 || s.phase != ResourcePhase::ScanoutAttached || !s.allocated || s.resource_id != RESOURCE_ID || s.width != WIDTH || s.height != HEIGHT || s.backing_bytes != BACKING_BYTES || s.backing_pages != BACKING_PAGES || s.resource_binding_digest != expected_binding || s.scanout_receipt_digest != expected_scanout || s.overflow_refusals != 3 || s.backing_mismatch_refusals != 1 || s.format_or_id_refusals != 1 || s.rejected_total != 6 || s.allocated_total != 1 { return Err("k2210-outcome"); } checkpoint(3); el0::write_fields( "TURKOS/OS/K2210-VIRTIO-GPU-RESOURCES:", &[ ("ALLOCATED", u64::from(s.allocated)), ("WIDTH", s.width as u64), ("HEIGHT", s.height as u64), ("BYTES", s.backing_bytes as u64), ("PAGES", s.backing_pages as u64), ("BINDING", s.resource_binding_digest), ("RECEIPT", s.scanout_receipt_digest), ("REJECTED", s.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(|_| "k2210-memory")?.allocator; let free_before = allocator.remaining_pages() as u64; word(0, MAGIC); word(2, free_before); let (expected_binding, expected_scanout) = setup_stage()?; checkpoint(1); let overflower = run_program( allocator, tables, boot, OVERFLOWER_ID, OVERFLOWER, [expected_binding, expected_scanout], 12, )?; record_overflower(&overflower); checkpoint(2); let allocator_out = run_program( allocator, tables, boot, ALLOCATOR_ID, ALLOCATOR, [expected_binding, expected_scanout], 28, )?; let free_after = allocator.remaining_pages() as u64; finish_stage( &overflower, &allocator_out, free_before, free_after, expected_binding, expected_scanout, ) } 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 }