//! Native product shutdown adapter. Completion of earlier experiments is not //! authority to power off a product. All live product owners must first have //! published their real teardown and accounting receipts. //! //! The current native D3 path is read-only. This adapter can issue an actual //! negotiated VirtIO FLUSH barrier after that owner closes, but cannot turn it //! into evidence of data/metadata/journal writes that the product never made. use super::super::*; use turkos_kernel::runtime::{platform, psci_call}; use turkos_kernel::{virtio_mmio, virtio_ring}; const UNKNOWN: u64 = u64::MAX; const OBSERVED_RUNTIME: u64 = 1; const OBSERVED_LIFECYCLE: u64 = 1 << 1; const OBSERVED_PSCI: u64 = 1 << 2; const OBSERVED_BLOCK: u64 = 1 << 3; const OBSERVED_LEASE: u64 = 1 << 4; const OBSERVED_FLUSH: u64 = 1 << 5; const OBSERVED_RESET: u64 = 1 << 6; const OBSERVED_RECONCILED: u64 = 1 << 7; const OBSERVED_OFF_RETURN: u64 = 1 << 8; const PSCI_VERSION: u64 = 0x8400_0000; const PSCI_FEATURES: u64 = 0x8400_000a; const SYSTEM_OFF: u64 = 0x8400_0008; const SHUTDOWN_MAGIC: u64 = 0x4437_5348_5554_3031; /// Raw same-boot teardown receipt, version 1. These bytes describe observations, /// never a completed SYSTEM_OFF (which cannot return to publish success). /// 0 magic,1 version,2 boot,3 state(1 closing/2 ready/3 off-issued/4 returned/ /// 255 failed),4 completed ordered-phase bits,5 current/failed phase, /// 6 failure errno,7 start,8 last counter,9 frequency,10 DAIF,11 MPIDR, /// 12..25 phase start/end pairs: admission close,D5 join,D4 drain, /// D3 cancellation,FLUSH,D6,gate;26 SYSTEM_OFF issue counter, /// 27 return value(if any),28 conduit,29 version,30 FEATURES(SYSTEM_OFF), /// 31 requested function,32..63 legacy D7 block, /// 64 free-at-entry,65 free-final,66 retained FLUSH lease(0/1), /// 67 actual FLUSH completion(0/1),68 read-only release scope(1), /// 69 gate host release observed(0/1);70 block SPI,71 GICD,72 DT trigger, /// 73 GICD_TYPER,74 actual trigger config,75..77 initial owned SPI enable/ /// active/pending bits,78..80 final bits,81/82 initial/final block interrupt /// status,83 terminal IRQ observation(1). No IRQ delivery is claimed by a /// polled FLUSH. Unused words remain zero. #[used] #[unsafe(no_mangle)] pub static mut OS_PRODUCT_D7_SHUTDOWN: [u64; 128] = [0; 128]; /// Pre-reset DMA bytes retained before the queue's frames are scrubbed: /// descriptors[0..32],request header[32..48],status[48],avail[56..64], /// used[64..80],physical desc/avail/used/request addresses[80..112], /// observation counter[112..120],MMIO base[120..128]. #[used] #[unsafe(no_mangle)] pub static mut OS_PRODUCT_D7_FLUSH_BYTES: [u8; 128] = [0; 128]; // The masked boot CPU exclusively owns this state. A failed reset deliberately // leaves the *typed* DMA lease here; retaining only integer addresses would // strand the resource owner and could permit unsafe reuse in a recovery path. static mut FLUSH_OWNER: Option = None; static mut ENTERED: bool = false; fn receipt(index: usize, value: u64) { unsafe { core::ptr::write_volatile( core::ptr::addr_of_mut!(OS_PRODUCT_D7_SHUTDOWN) .cast::() .add(index), value, ) } } fn receipt_get(index: usize) -> u64 { unsafe { core::ptr::read_volatile( core::ptr::addr_of!(OS_PRODUCT_D7_SHUTDOWN) .cast::() .add(index), ) } } fn begin_phase(phase: usize) { receipt(5, phase as u64); receipt(12 + (phase - 1) * 2, counter()); } fn finish_phase(phase: usize) -> Result<(), &'static str> { if !(1..=7).contains(&phase) { return Err("native-d7-unordered-teardown-phase"); } let expected = (1u64 << (phase - 1)) - 1; if receipt_get(4) != expected { return Err("native-d7-unordered-teardown-receipt"); } let now = counter(); if now < receipt_get(12 + (phase - 1) * 2) { return Err("native-d7-clock-regressed"); } receipt(13 + (phase - 1) * 2, now); receipt(4, expected | (1 << (phase - 1))); receipt(8, now); Ok(()) } fn retained_flush() -> bool { unsafe { (&*core::ptr::addr_of!(FLUSH_OWNER)).is_some() } } fn capture_flush_bytes(base: u64, frames: [u64; 4]) { let destination = core::ptr::addr_of_mut!(OS_PRODUCT_D7_FLUSH_BYTES).cast::(); let [desc, avail, used, request] = frames; unsafe { asm!("dmb oshld", options(nostack, preserves_flags)); for (source, offset, length) in [ (desc, 0, 32), (request, 32, 16), (request + 16, 48, 1), (avail, 56, 8), (used, 64, 16), ] { for index in 0..length { let value = core::ptr::read_volatile((source as *const u8).add(index)); core::ptr::write_volatile(destination.add(offset + index), value); } } for (index, value) in frames.into_iter().chain([counter(), base]).enumerate() { for (byte, value) in value.to_le_bytes().into_iter().enumerate() { core::ptr::write_volatile(destination.add(80 + index * 8 + byte), value); } } } } fn counter() -> u64 { let value: u64; unsafe { asm!("mrs {value}, CNTPCT_EL0", value = out(reg) value, options(nomem, nostack, preserves_flags)); } value } unsafe fn read32(base: u64, offset: u64) -> u32 { unsafe { core::ptr::read_volatile((base + offset) as *const u32) } } unsafe fn write32(base: u64, offset: u64, value: u32) { unsafe { core::ptr::write_volatile((base + offset) as *mut u32, value) } } unsafe fn write64(base: u64, offset: u64, value: u64) { unsafe { write32(base, offset, value as u32); write32(base, offset + 4, (value >> 32) as u32); } } fn psci(conduit: psci_call::Conduit, function: u64, argument: u64) -> u64 { let result: u64; // The conduit is discovered from the bounded boot DTB, never guessed from // QEMU defaults. Only EL1 on the boot CPU can reach this adapter. unsafe { match conduit { psci_call::Conduit::Hvc => asm!("hvc #0", inout("x0") function => result, inout("x1") argument => _, in("x2") 0u64, in("x3") 0u64, clobber_abi("C"), options(nostack)), psci_call::Conduit::Smc => asm!("smc #0", inout("x0") function => result, inout("x1") argument => _, in("x2") 0u64, in("x3") 0u64, clobber_abi("C"), options(nostack)), } } result } fn require_closed_owners() -> Result<(), &'static str> { // A completed phase bit alone does not prove that its device/CPU owners // are closed. Each owner has a separate receipt, initially unavailable. super::require_shutdown_prerequisites()?; super::d3_shell::require_closed()?; super::d3_shell::require_processes_stopped()?; super::d4_net_ipc::require_drained()?; if !super::d5_smp::stopped() { return Err("native-d7-secondary-stop-receipt-missing"); } super::d6_accounting::require_reconciled() } fn block_base(boot: &BootData, tables: &mmu::Tables) -> Result { if boot.dtb_address == 0 || boot.dtb_size == 0 { return Err("native-d7-dtb-missing"); } 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-d7-block-discovery")?; let mut block = None; for node in nodes.as_slice() { let last = node .base .checked_add(0x107) .ok_or("native-d7-block-mmio-overflow")?; for address in [node.base, last] { let descriptor = os_k700_translation(tables, address)?; if descriptor & 1 == 0 || (descriptor >> 2) & 7 != 1 { return Err("native-d7-block-mmio-not-device"); } } if unsafe { read32(node.base, 0) } != virtio_mmio::MAGIC { return Err("native-d7-block-magic"); } if unsafe { read32(node.base, 8) } != 2 { continue; } if unsafe { read32(node.base, 4) } != 2 || block.replace(node.base).is_some() { return Err("native-d7-block-ambiguous-or-legacy"); } } block.ok_or("native-d7-block-missing") } /// This read-only FLUSH owner uses polling; it does not borrow a live GIC /// handler or change an inherited route. Its actual DT-bound SPI must already /// be disabled and inactive on both sides of the DMA interval. Pending state /// is preserved as a diagnostic, never cleared to manufacture completion. fn block_irq_observation( base: u64, tables: &mmu::Tables, boot: &BootData, final_sample: bool, ) -> Result<(), &'static str> { let distributor = boot.interrupts.gic_distributor; let end = distributor .checked_add(0xcff) .ok_or("native-d7-gic-range")?; for address in [distributor, end] { let descriptor = os_k700_translation(tables, address)?; if descriptor & 1 == 0 || (descriptor >> 2) & 7 != 1 { return Err("native-d7-gic-not-device"); } } let blob = unsafe { core::slice::from_raw_parts(boot.dtb_address as *const u8, boot.dtb_size) }; let scratch = unsafe { &*core::ptr::addr_of!(OS_K700_WORKSPACE) }; let (intid, _, _, trigger) = platform::os_k940_virtio_spi(blob, scratch, base, distributor) .map_err(|_| "native-d7-block-spi-binding")?; let typer = unsafe { read32(distributor, 4) }; if !(32..1020).contains(&intid) || intid >= ((typer & 31) + 1) * 32 { return Err("native-d7-block-spi-capacity"); } let word = 4 * u64::from(intid / 32); let bit = 1 << (intid % 32); let config = (unsafe { read32(distributor, 0xc00 + 4 * u64::from(intid / 16)) } >> (2 * (intid % 16))) & 3; let expected_config = match trigger { 1 => 2, 4 => 0, _ => return Err("native-d7-block-spi-trigger"), }; let enabled = unsafe { read32(distributor, 0x100 + word) } & bit; let active = unsafe { read32(distributor, 0x300 + word) } & bit; let pending = unsafe { read32(distributor, 0x200 + word) } & bit; let interrupt = unsafe { read32(base, 0x60) }; if final_sample { if receipt_get(70) != u64::from(intid) || receipt_get(71) != distributor || receipt_get(72) != u64::from(trigger) || receipt_get(73) != u64::from(typer) || receipt_get(74) != u64::from(config) { return Err("native-d7-block-spi-identity-changed"); } receipt(78, u64::from(enabled)); receipt(79, u64::from(active)); receipt(80, u64::from(pending)); receipt(82, u64::from(interrupt)); } else { for (index, value) in [ u64::from(intid), distributor, u64::from(trigger), u64::from(typer), u64::from(config), u64::from(enabled), u64::from(active), u64::from(pending), ] .into_iter() .enumerate() { receipt(70 + index, value); } receipt(81, u64::from(interrupt)); } if config != expected_config || enabled != 0 || active != 0 || interrupt != 0 { return Err("native-d7-block-spi-not-quiescent"); } if final_sample { receipt(83, 1); } Ok(()) } /// Issue exactly one real FLUSH request. No completion count is supplied by a /// caller: the used-ring identity, request type, length and status are read /// from the device-owned DMA pages after an acquire barrier. fn flush_queue( base: u64, lease: &mut virtio_ring::DmaLease, boot: &BootData, frequency: u64, out: &mut [u64; 32], ) -> Result<(), &'static str> { let frames = lease.frames().map_err(|_| "native-d7-dma-frames")?; let [desc, avail, used, _] = frames; virtio_ring::split_ring_pages( desc, avail, used, 4, boot.hardware.ram_base, boot.hardware.ram_size, ) .map_err(|_| "native-d7-ring-geometry")?; for (index, frame) in frames.iter().enumerate() { out[11 + index] = *frame; unsafe { core::ptr::write_bytes(*frame as *mut u8, 0, 4096) }; } unsafe { // The caller already required an unowned, reset device. Never reset a // live service merely to obtain a shutdown-looking measurement. write32(base, 0x70, 3); write32(base, 0x14, 0); let low = read32(base, 0x10); write32(base, 0x14, 1); let high = read32(base, 0x10); if low & virtio_mmio::BLK_F_FLUSH == 0 || high & virtio_mmio::MODERN_F_VERSION_1 == 0 || high & virtio_mmio::MODERN_F_IOMMU_PLATFORM != 0 { return Err("native-d7-flush-feature-required"); } // Preserve RO if offered. No OUT descriptor is ever submitted. let accepted_low = low & (virtio_mmio::BLK_F_FLUSH | virtio_mmio::BLK_F_RO); out[7] = u64::from(accepted_low); out[8] = u64::from(virtio_mmio::MODERN_F_VERSION_1); write32(base, 0x24, 0); write32(base, 0x20, accepted_low); write32(base, 0x24, 1); write32(base, 0x20, virtio_mmio::MODERN_F_VERSION_1); write32(base, 0x70, 0x0b); if read32(base, 0x70) != 0x0b { return Err("native-d7-features-readback"); } write32(base, 0x30, 0); if read32(base, 0x34) < 4 || read32(base, 0x44) != 0 { return Err("native-d7-queue-owned-or-small"); } write32(base, 0x38, 4); write64(base, 0x80, desc); write64(base, 0x90, avail); write64(base, 0xa0, used); asm!("dmb oshst", options(nostack, preserves_flags)); // Mark ownership live before making it visible to the device. lease.mark_ready().map_err(|_| "native-d7-lease-ready")?; write32(base, 0x44, 1); if read32(base, 0x44) != 1 { return Err("native-d7-queue-ready-readback"); } write32(base, 0x70, 0x0f); if read32(base, 0x70) != 0x0f { return Err("native-d7-driver-ready-readback"); } } let header = lease .request_subregion(0, 16) .map_err(|_| "native-d7-flush-header")?; let status = lease .request_subregion(16, 1) .map_err(|_| "native-d7-flush-status")?; let chain = [ virtio_ring::Descriptor { addr_le: header.to_le(), len_le: 16u32.to_le(), flags_le: virtio_ring::DESC_F_NEXT.to_le(), next_le: 1u16.to_le(), }, virtio_ring::Descriptor { addr_le: status.to_le(), len_le: 1u32.to_le(), flags_le: virtio_ring::DESC_F_WRITE.to_le(), next_le: 0, }, ]; out[20] = counter(); out[22] = virtio_ring::deadline_after( out[20], frequency .checked_mul(2) .ok_or("native-d7-deadline-overflow")?, ) .map_err(|_| "native-d7-deadline")?; unsafe { for (index, descriptor) in chain.into_iter().enumerate() { core::ptr::write_volatile( (desc as *mut virtio_ring::Descriptor).add(index), descriptor, ); } core::ptr::write_volatile( header as *mut virtio_ring::BlockReadHeader, virtio_ring::BlockReadHeader { type_le: 4u32.to_le(), reserved_le: 0, sector_le: 0, }, ); core::ptr::write_volatile(status as *mut u8, 0xff); core::ptr::write_volatile((avail + 4) as *mut u16, 0); asm!("dmb oshst", options(nostack, preserves_flags)); core::ptr::write_volatile((avail + 2) as *mut u16, 1u16.to_le()); asm!("dmb oshst", options(nostack, preserves_flags)); write32(base, 0x50, 0); } let observed = loop { let index = unsafe { u16::from_le(core::ptr::read_volatile((used + 2) as *const u16)) }; if index != 0 || virtio_ring::deadline_expired(counter(), out[22]) { break index; } core::hint::spin_loop(); }; unsafe { asm!("dmb oshld", options(nostack, preserves_flags)) }; out[21] = counter(); out[15] = u64::from(unsafe { u16::from_le(core::ptr::read_volatile((avail + 2) as *const u16)) }); out[16] = u64::from(observed); out[17] = u64::from(unsafe { u32::from_le(core::ptr::read_volatile((used + 4) as *const u32)) }); out[18] = u64::from(unsafe { u32::from_le(core::ptr::read_volatile((used + 8) as *const u32)) }); out[19] = u64::from(unsafe { core::ptr::read_volatile(status as *const u8) }); let actual_header = unsafe { core::ptr::read_volatile(header as *const virtio_ring::BlockReadHeader) }; capture_flush_bytes(base, frames); if observed == 0 { out[31] = 110; return Err("native-d7-flush-timeout"); } if out[15] != 1 || observed != 1 || out[17] != 0 || out[18] != 1 || out[19] != 0 || u32::from_le(actual_header.type_le) != 4 || actual_header.reserved_le != 0 || actual_header.sector_le != 0 { out[31] = 5; return Err("native-d7-flush-completion-invalid"); } out[0] |= OBSERVED_FLUSH; out[31] = 0; Ok(()) } fn flush_and_release( runtime: &mut OsK660Runtime, tables: &mmu::Tables, boot: &BootData, out: &mut [u64; 32], ) -> Result<(), &'static str> { let base = block_base(boot, tables)?; out[5] = base; out[6] = unsafe { u64::from(read32(base, 0x100)) | u64::from(read32(base, 0x104)) << 32 }; out[9] = u64::from(unsafe { read32(base, 0x70) }); out[10] = u64::from(unsafe { read32(base, 0x44) }); out[0] |= OBSERVED_BLOCK; if out[9] != 0 || out[10] != 0 || out[6] == 0 { return Err("native-d7-block-owner-not-closed"); } block_irq_observation(base, tables, boot, false)?; let frequency: u64; unsafe { asm!("mrs {value}, CNTFRQ_EL0", value = out(reg) frequency, options(nomem, nostack, preserves_flags)); } if frequency == 0 { return Err("native-d7-counter-frequency-zero"); } let allocator = &mut runtime .memory_mut() .map_err(|_| "native-d7-memory-owner")? .allocator; out[2] = allocator.remaining_pages(); if retained_flush() { return Err("native-d7-retained-flush-owner-already-present"); } let acquired = virtio_ring::DmaLease::allocate(allocator, boot.hardware.ram_base, boot.hardware.ram_size) .map_err(|_| "native-d7-dma-allocate")?; unsafe { core::ptr::write(core::ptr::addr_of_mut!(FLUSH_OWNER), Some(acquired)); } receipt(66, 1); let lease = unsafe { (&mut *core::ptr::addr_of_mut!(FLUSH_OWNER)).as_mut() } .ok_or("native-d7-flush-owner-missing")?; out[3] = allocator.remaining_pages(); out[0] |= OBSERVED_LEASE; if lease .frames() .map_err(|_| "native-d7-dma-frames")? .into_iter() .any(|frame| !tables.normal_shareable_identity(frame)) { // No queue was exposed and no frame was dereferenced. In particular, // do not attempt to scrub an address that failed the mapping check. lease .release(allocator) .map_err(|_| "native-d7-unused-dma-release")?; out[4] = allocator.remaining_pages(); unsafe { core::ptr::write(core::ptr::addr_of_mut!(FLUSH_OWNER), None); } receipt(66, 0); return Err("native-d7-dma-not-normal-identity"); } let result = flush_queue(base, lease, boot, frequency, out); // A timeout or EIO is not clean shutdown. Even those paths must first // prove reset before scrubbing or releasing a possibly live DMA frame. unsafe { write32(base, 0x70, 0); asm!("dsb osh", options(nostack, preserves_flags)); out[23] = u64::from(read32(base, 0x70)); out[24] = u64::from(read32(base, 0x44)); } if out[23] != 0 || out[24] != 0 { // Deliberately retain allocated frames. Reuse while a device may // still DMA would hide both a leak and a memory safety failure. return Err("native-d7-reset-failed-dma-retained"); } // A reset is insufficient if an inherited IRQ owner became live. Retain // every lease on that failure as well, without changing GIC state. block_irq_observation(base, tables, boot, true)?; lease.mark_reset().map_err(|_| "native-d7-dma-mark-reset")?; for frame in lease.frames().map_err(|_| "native-d7-dma-frames")? { unsafe { core::ptr::write_bytes(frame as *mut u8, 0, 4096) }; } unsafe { asm!("dsb oshst", options(nostack, preserves_flags)) }; lease .release(allocator) .map_err(|_| "native-d7-dma-release")?; out[4] = allocator.remaining_pages(); unsafe { core::ptr::write(core::ptr::addr_of_mut!(FLUSH_OWNER), None); } receipt(66, 0); out[0] |= OBSERVED_RESET; if out[3].checked_add(4) != Some(out[2]) || out[4] != out[2] { return Err("native-d7-dma-accounting-mismatch"); } result?; out[0] |= OBSERVED_RECONCILED; Ok(()) } /// Shut down the owners created by this boot, in dependency order. The normal /// read-only scope has no data/metadata/journal writes to invent: one negotiated /// actual block FLUSH provides the storage barrier, while the host must still /// reopen/hash the unchanged disk after guest-originated SYSTEM_OFF. fn run_sequence( runtime: &mut OsK660Runtime, tables: &mmu::Tables, boot: &BootData, out: &mut [u64; 32], ) -> Result<(), &'static str> { // Stage evidence is necessary but never sufficient; real live owners are // stopped below. In particular, do not ask D6 for a final receipt before // the D3/D4 allocations have actually been relinquished. if super::get(4) & 0xbf != 0xbf { return Err("native-d7-preceding-stages-incomplete"); } out[1] = super::boot_id()?; receipt(2, out[1]); if boot.dtb_address == 0 || boot.dtb_size == 0 || boot.dtb_size > platform::MAX_BLOB { return Err("native-d7-dtb-missing-or-unbounded"); } let blob = unsafe { core::slice::from_raw_parts(boot.dtb_address as *const u8, boot.dtb_size) }; let scratch = unsafe { &mut *core::ptr::addr_of_mut!(OS_K700_WORKSPACE) }; let conduit = psci_call::discover_conduit(blob, scratch).map_err(|_| "native-d7-psci-dtb")?; out[25] = conduit as u64; out[26] = psci(conduit, PSCI_VERSION, 0); out[27] = psci(conduit, PSCI_FEATURES, SYSTEM_OFF); out[28] = SYSTEM_OFF; receipt(28, out[25]); receipt(29, out[26]); receipt(30, out[27]); receipt(31, SYSTEM_OFF); if (out[26] as u32 as i32) < 0 || (out[26] as u32) < 2 || (out[27] as u32 as i32) < 0 { return Err("native-d7-psci-system-off-unsupported"); } out[0] |= OBSERVED_PSCI; // Admission closes without touching live DMA. Joining worker CPUs comes // before draining/releasing the net owner they could still reference. begin_phase(1); super::d4_net_ipc::close_admission()?; finish_phase(1)?; begin_phase(2); super::d5_smp::stop_join()?; if !super::d5_smp::stopped() { return Err("native-d7-secondary-stop-receipt-missing"); } finish_phase(2)?; begin_phase(3); super::d4_net_ipc::begin_shutdown(runtime, tables, boot)?; super::d4_net_ipc::require_drained()?; finish_phase(3)?; begin_phase(4); // This is orderly kernel cancellation at a cooperative checkpoint, not a // fabricated userspace exit syscall. Real PIDs, spaces, tables and stacks // remain owned until this method has retired them and invalidated TLBs. super::d3_shell::shutdown(runtime, tables, boot)?; super::d3_shell::require_processes_stopped()?; super::d3_shell::require_closed()?; finish_phase(4)?; out[0] |= OBSERVED_LIFECYCLE; begin_phase(5); flush_and_release(runtime, tables, boot, out)?; receipt(67, 1); finish_phase(5)?; begin_phase(6); let dma_inventory = super::d4_device::accounting_inventory()?; super::d6_accounting::finalize_shutdown(runtime, &dma_inventory)?; require_closed_owners()?; finish_phase(6)?; let free = runtime .memory() .ok_or("native-d7-memory-owner-missing")? .allocator .remaining_pages(); receipt(65, free); if free != out[4] || retained_flush() { return Err("native-d7-final-resource-state-changed"); } for (index, value) in out.iter().enumerate() { receipt(32 + index, *value); } // Publish SHUTDOWN_READY, never "clean/off". Only a real host-captured QMP // SHUTDOWN{guest:true,reason:guest-shutdown} followed by disk reopening can // establish that the nonreturning firmware operation actually completed. begin_phase(7); receipt(3, 2); unsafe { asm!("dsb sy", options(nostack, preserves_flags)) }; super::checkpoint_shutdown(out)?; receipt(69, 1); if super::boot_id()? != out[1] || runtime .memory() .ok_or("native-d7-memory-owner-missing")? .allocator .remaining_pages() != free { return Err("native-d7-state-changed-at-shutdown-gate"); } require_closed_owners()?; finish_phase(7)?; receipt(3, 3); receipt(26, counter()); unsafe { asm!("dsb sy", options(nostack, preserves_flags)) }; write_line("TURKOS/PRODUCT: GUEST_SYSTEM_OFF"); out[29] = psci(conduit, SYSTEM_OFF, 0); receipt(27, out[29]); receipt(3, 4); out[0] |= OBSERVED_OFF_RETURN; // SYSTEM_OFF success cannot return to manufacture an acceptance bit. Err("native-d7-psci-system-off-returned") } pub(super) fn run( runtime: &mut OsK660Runtime, tables: &mmu::Tables, boot: &BootData, out: &mut [u64; 32], ) -> Result<(), &'static str> { let daif: u64; let mpidr: u64; let el: u64; let frequency: u64; unsafe { asm!("mrs {v}, DAIF", v = out(reg) daif, options(nomem, nostack, preserves_flags)); asm!("mrs {v}, MPIDR_EL1", v = out(reg) mpidr, options(nomem, nostack, preserves_flags)); asm!("mrs {v}, CurrentEL", v = out(reg) el, options(nomem, nostack, preserves_flags)); asm!("mrs {v}, CNTFRQ_EL0", v = out(reg) frequency, options(nomem, nostack, preserves_flags)); } if daif & 0x3c0 != 0x3c0 || mpidr & psci_call::AFFINITY_MASK != 0 || el != 4 { return Err("native-d7-requires-masked-el1-boot-cpu"); } if unsafe { core::ptr::read_volatile(core::ptr::addr_of!(ENTERED)) } { return Err("native-d7-repeat-entry-retained-evidence"); } unsafe { core::ptr::write_volatile(core::ptr::addr_of_mut!(ENTERED), true) }; for index in 0..128 { receipt(index, 0); } receipt(0, SHUTDOWN_MAGIC); receipt(1, 1); receipt(3, 1); receipt(7, counter()); receipt(9, frequency); receipt(10, daif); receipt(11, mpidr); receipt(68, 1); *out = [UNKNOWN; 32]; out[0] = 0; out[30] = daif; let initial_free = match runtime.memory() { Some(memory) => memory.allocator.remaining_pages(), None => { receipt(3, 255); receipt(6, 5); receipt(8, counter()); return Err("native-d7-memory-owner-missing"); } }; receipt(64, initial_free); out[2] = initial_free; out[0] |= OBSERVED_RUNTIME; let result = if frequency == 0 { Err("native-d7-counter-frequency-zero") } else { run_sequence(runtime, tables, boot, out) }; if result.is_err() { receipt(3, 255); // The original failure string is preserved by the native entry. This // numeric field only distinguishes actual I/O timeout from EIO and // other teardown errors; neither path can proceed to SYSTEM_OFF. receipt(6, if out[31] == 110 { 110 } else { 5 }); receipt(8, counter()); receipt(66, retained_flush() as u64); for (index, value) in out.iter().enumerate() { receipt(32 + index, *value); } unsafe { asm!("dsb sy", options(nostack, preserves_flags)) }; } result }