//! D4 native admission adapter. //! //! Hardware discovery is observable here, but the inherited product runtime //! does not publish a live net/IPC/shell service. Do not substitute the old DMA //! experiment, a local ChannelTable, scheduler counters, or a host socket for //! that missing service. The adapter must fail closed until those owners exist. use super::super::*; const UNAVAILABLE: u64 = u64::MAX; const RUNTIME_OBSERVED: u64 = 1; const IRQ_OBSERVED: u64 = 1 << 1; const DEVICE_OBSERVED: u64 = 1 << 2; const DEVICE_MAPPING_OBSERVED: u64 = 1 << 3; const DEVICE_IRQ_OBSERVED: u64 = 1 << 4; /// A shutdown caller needs a receipt from the persistent service owner. /// Discovery without starting DMA is not proof that another service drained. pub(super) fn require_drained() -> Result<(), &'static str> { Err("native-d4-missing-live-service-drain-receipt") } /// Read only after proving the complete register lies in a Device mapping. unsafe fn register(base: u64, offset: u64) -> u64 { u64::from(unsafe { core::ptr::read_volatile((base + offset) as *const u32) }) } pub(super) fn run( runtime: &mut super::super::OsK660Runtime, tables: &super::super::mmu::Tables, boot: &super::super::BootData, out: &mut [u64; 32], ) -> Result<(), &'static str> { // Unobserved packet, channel and shell fields cannot look like successful // zero-count measurements. Slot zero alone is the observation-valid mask. *out = [UNAVAILABLE; 32]; out[0] = 0; let daif: u64; unsafe { asm!("mrs {value}, DAIF", value = out(reg) daif, options(nomem, nostack, preserves_flags)); } out[31] = daif; if daif & 0x80 == 0 { return Err("native-d4-snapshot-requires-masked-irq"); } { let (_, memory, irq, tasks) = runtime.running().map_err(os_k660_policy::Error::tag)?; out[9] = memory.allocator.remaining_pages(); out[12] = tasks.progress; out[13] = tasks.scheduler.ready_len() as u64; if let Some(id) = tasks.id { out[10] = u64::from(id.slot); out[11] = id.generation; } if let Some(id) = tasks.scheduler.running() { out[14] = u64::from(id.slot); out[15] = id.generation; } out[30] = u64::from(irq.installed); out[0] |= RUNTIME_OBSERVED; } // This count belongs to the runtime IRQ callback source. It is deliberately // not a network completion count; no network IRQ owner exists yet. out[7] = unsafe { core::ptr::read_volatile(core::ptr::addr_of!(OS_K660_IRQ_COUNT)) }; out[8] = u64::from(unsafe { core::ptr::read_volatile(core::ptr::addr_of!(OS_K660_IRQ_ACTIVE)) }); out[0] |= IRQ_OBSERVED; if boot.dtb_address == 0 || boot.dtb_size == 0 { return Err("native-d4-no-dtb"); } let blob = unsafe { core::slice::from_raw_parts(boot.dtb_address as *const u8, boot.dtb_size) }; let nodes = dtb::discover_virtio_mmio(blob).map_err(|_| "native-d4-net-discovery")?; let mut net = None; for node in nodes.as_slice() { // Do not touch an unverified MMIO address, including a placeholder node. let descriptor = os_k700_translation(tables, node.base)?; let last_register = node.base.checked_add(0xff).ok_or("native-d4-mmio-range")?; let final_descriptor = os_k700_translation(tables, last_register)?; if descriptor & 1 == 0 || (descriptor >> 2) & 7 != 1 || final_descriptor & 1 == 0 || (final_descriptor >> 2) & 7 != 1 { return Err("native-d4-mmio-not-device-mapped"); } if unsafe { register(node.base, 0) } != 0x7472_6976 { return Err("native-d4-virtio-magic"); } if unsafe { register(node.base, 8) } != 1 { continue; } if unsafe { register(node.base, 4) } != 2 || net.is_some() { return Err("native-d4-ambiguous-or-legacy-net"); } net = Some((node.base, descriptor)); } let (base, descriptor) = net.ok_or("native-d4-missing-virtio-net")?; out[1] = base; out[2] = descriptor; out[0] |= DEVICE_OBSERVED | DEVICE_MAPPING_OBSERVED; out[4] = unsafe { register(base, 0x70) }; out[5] = unsafe { register(base, 0x60) }; out[6] = unsafe { register(base, 0xfc) }; let (spi, _, _, _) = turkos_kernel::runtime::platform::os_k940_virtio_spi( blob, unsafe { &*core::ptr::addr_of!(OS_K700_WORKSPACE) }, base, boot.interrupts.gic_distributor, ) .map_err(|_| "native-d4-net-irq-discovery")?; out[3] = u64::from(spi); out[0] |= DEVICE_IRQ_OBSERVED; // No QueueSel/Status/InterruptACK write is authorized without a persistent // device owner and a teardown path. In particular, do not reset a device // that another product service may already own. No DMA pages are acquired. // A live channel needs real independently scheduled endpoints and the // shell's process identity; allocating model TaskIds here would forge D4. Err("native-d4-missing-net-ipc-shell-service") }