// K2480 guest experiment: Supported device decision table // (`portability.devices`) in the product kernel runtime. Enforces "Absent // unsupported untested ayrı durumlardır": an EL0 negative actor // (`k2480-untestedsupported`) attempts to commit the device decision table with // a supported label without a real test ("Test yokken supported etiketi"), // absent/unsupported/untested states collapsed, a support cell unbound to a // real case ("Her destek hücresi gerçek casee bağlı"), device decision profiles // mismatch ("MMIO PCI GPU input profilleri"), and distinct non-support states // mismatch; all five are refused and `committed_receipt()` remains None. Then // EL0 `k2480-devicedecisiontable` commits 4 device decision profiles (MMIO, // PCI, GPU, input), 3 distinct non-support states, and 4 case-bound support // cells with 0 untested supported labels, 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::portability_devices::{ compute_portability_devices_binding_digest, PortabilityDevicesCoordinator, PortabilityDevicesRequest, EXPECTED_CASE_BOUND_SUPPORT_CELLS, EXPECTED_DEVICE_DECISION_PROFILES, EXPECTED_DISTINCT_NON_SUPPORT_STATES, EXPECTED_UNTESTED_SUPPORTED_LABELS, }; use core::arch::asm; use core::ptr::{addr_of, addr_of_mut, read_volatile, write_volatile}; #[unsafe(no_mangle)] static mut OS_K2480_EVIDENCE: [u64; 64] = [0; 64]; #[unsafe(no_mangle)] static mut OS_K2480_CAPTURE_GATE: u8 = 0; static UNTESTEDSUPPORTED: &[u8] = include_bytes!("k2480-untestedsupported.bin"); static DEVICEDECISIONTABLE: &[u8] = include_bytes!("k2480-devicedecisiontable.bin"); const MAGIC: u64 = 0x4b32_3438_305f_4456; // "K2480_DV" const SYS_EXIT: u64 = 1; const SYS_PORTABILITY_DEVICES: u64 = 140; const SYS_PORTABILITY_DEVICES_RECEIPT: u64 = 141; const REFUSED: u64 = 1 << 63; const PREVIOUS_DEVICES_EPOCH: u64 = 85; const DEVICES_EPOCH: u64 = 86; const DEVICES_TABLE_DIGEST: u64 = 0x2480_5052_4445_5653; const UNTESTEDSUPPORTED_ID: el0::Identity = el0::Identity { pid: 9, generation: 0x2480_0009, }; const DEVICEDECISIONTABLE_ID: el0::Identity = el0::Identity { pid: 1, generation: 0x2480_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_K2480_EVIDENCE).cast::().add(i), v, ) } } #[inline(never)] fn checkpoint(stage: u64) { word(1, stage); unsafe { write_volatile(addr_of_mut!(OS_K2480_CAPTURE_GATE), 0) }; el0::write_fields("TURKOS/OS/K2480: CAPTURE", &[("STAGE", stage)]); while unsafe { read_volatile(addr_of!(OS_K2480_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_portability_devices_binding_digest( EXPECTED_DEVICE_DECISION_PROFILES, EXPECTED_DISTINCT_NON_SUPPORT_STATES, EXPECTED_CASE_BOUND_SUPPORT_CELLS, EXPECTED_UNTESTED_SUPPORTED_LABELS, DEVICES_EPOCH, DEVICES_TABLE_DIGEST, ); let expected_receipt = expected_binding ^ 0x2480_ACE0_4445_5631; let request = PortabilityDevicesRequest { supported_label_without_real_test: false, absent_unsupported_untested_collapsed: false, every_support_cell_bound_to_real_case: true, device_decision_profiles: EXPECTED_DEVICE_DECISION_PROFILES, distinct_non_support_states: EXPECTED_DISTINCT_NON_SUPPORT_STATES, case_bound_support_cells: EXPECTED_CASE_BOUND_SUPPORT_CELLS, devices_epoch: DEVICES_EPOCH, previous_devices_epoch: PREVIOUS_DEVICES_EPOCH, devices_table_digest: DEVICES_TABLE_DIGEST, devices_binding_digest: expected_binding, }; let coord = PortabilityDevicesCoordinator::new(); for (j, v) in [ EXPECTED_DEVICE_DECISION_PROFILES as u64, EXPECTED_DISTINCT_NON_SUPPORT_STATES as u64, u64::from(coord.is_committed()), EXPECTED_CASE_BOUND_SUPPORT_CELLS as u64, EXPECTED_UNTESTED_SUPPORTED_LABELS as u64, DEVICES_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_PORTABILITY_DEVICES => match frame.arg(0) { 0 => match coord.commit_device_decision_table(&base_req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), }, 1 => { let mut req = base_req; req.supported_label_without_real_test = true; match coord.commit_device_decision_table(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 2 => { let mut req = base_req; req.absent_unsupported_untested_collapsed = true; match coord.commit_device_decision_table(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 3 => { let mut req = base_req; req.every_support_cell_bound_to_real_case = false; match coord.commit_device_decision_table(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } 4 => { let mut req = base_req; req.device_decision_profiles = 0; match coord.commit_device_decision_table(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } _ => { let mut req = base_req; req.distinct_non_support_states = 0; match coord.commit_device_decision_table(&req) { Ok(rec) => rec.receipt_digest, Err(e) => REFUSED | (e as u64), } } }, SYS_PORTABILITY_DEVICES_RECEIPT => match coord.committed_receipt() { Some(_) => base_req.devices_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(|_| "k2480-user")?, ); let user = st.user.as_ref().unwrap(); if !user.contract_valid(tables.root) { return Err("k2480-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(|_| "k2480-release")?; Ok(outcome) } #[inline(never)] fn record_untestedsupported(untestedsupported: &el0::Outcome) { let coord = state().coord.as_ref().unwrap(); for (j, v) in [ untestedsupported.exit, untestedsupported.syscalls, untestedsupported.fault, coord.rejected_total(), u64::from(coord.is_committed()), coord.device_classification_refusals(), coord.device_accounting_refusals(), 0, ] .into_iter() .enumerate() { word(20 + j, v); } } #[inline(never)] fn finish_stage( untestedsupported: &el0::Outcome, devicedecisiontable: &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("k2480-missing-receipt")?; for (j, v) in [ devicedecisiontable.exit, devicedecisiontable.syscalls, devicedecisiontable.fault, u64::from(rec.every_support_cell_bound_to_real_case), u64::from(coord.is_committed()), rec.device_decision_profiles as u64, rec.distinct_non_support_states as u64, rec.case_bound_support_cells as u64, rec.untested_supported_labels as u64, expected_binding, rec.receipt_digest, coord.device_classification_refusals(), coord.device_accounting_refusals(), 0, coord.rejected_total(), coord.committed_total(), rec.devices_epoch, ] .into_iter() .enumerate() { word(36 + j, v); } word(3, free_after); if free_after != free_before { return Err("k2480-frame-accounting"); } if untestedsupported.exit != 0x2479 || devicedecisiontable.exit != 140 || !coord.is_committed() || !rec.every_support_cell_bound_to_real_case || rec.device_decision_profiles != EXPECTED_DEVICE_DECISION_PROFILES || rec.distinct_non_support_states != EXPECTED_DISTINCT_NON_SUPPORT_STATES || rec.case_bound_support_cells != EXPECTED_CASE_BOUND_SUPPORT_CELLS || rec.untested_supported_labels != EXPECTED_UNTESTED_SUPPORTED_LABELS || rec.devices_epoch != DEVICES_EPOCH || rec.receipt_digest != expected_receipt || coord.device_classification_refusals() != 3 || coord.device_accounting_refusals() != 2 || coord.rejected_total() != 6 || coord.committed_total() != 1 { return Err("k2480-outcome"); } checkpoint(3); el0::write_fields( "TURKOS/OS/K2480-PORTABILITY-DEVICES:", &[ ("COMMITTED", u64::from(coord.is_committed())), ("PROFILES", rec.device_decision_profiles as u64), ("NON_SUPPORT_STATES", rec.distinct_non_support_states as u64), ("CASE_BOUND_CELLS", rec.case_bound_support_cells as u64), ("UNTESTED_SUPPORTED", rec.untested_supported_labels as u64), ("EPOCH", rec.devices_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(|_| "k2480-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 untestedsupported = run_program( allocator, tables, boot, UNTESTEDSUPPORTED_ID, UNTESTEDSUPPORTED, [expected_binding, expected_receipt], 12, )?; record_untestedsupported(&untestedsupported); checkpoint(2); let devicedecisiontable = run_program( allocator, tables, boot, DEVICEDECISIONTABLE_ID, DEVICEDECISIONTABLE, [expected_binding, expected_receipt], 28, )?; let free_after = allocator.remaining_pages() as u64; finish_stage( &untestedsupported, &devicedecisiontable, 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 }