Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/patches/e9e0ee3fc6bf854b # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aienux#133 (code only) closed derived `e9e0ee3fc6bf854b` #133 Imported from GitHub aien-dev/aienux#133 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `3f8ebf387d69576df0a5640b1856fc916a232d9c` · Head `883df3617fc8c739f4836e15f01bab2ace51c398` ### Apply it ``` git checkout -b patch-e9e0ee3fc6bf854b 3f8ebf387d69576df0a5640b1856fc916a232d9c curl -fsSL https://interplane.io/drakestapleton/aienux/patches/e9e0ee3fc6bf854b.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 133 883df3617fc8c739f4836e15f01bab2ace51c398 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":133,"url":"https://github.com/aien-dev/aienux/pull/133","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:35:48Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/crates/aienos-boot/src/nvme_read.rs b/crates/aienos-boot/src/nvme_read.rs index e7252129..7d247b59 100644 --- a/crates/aienos-boot/src/nvme_read.rs +++ b/crates/aienos-boot/src/nvme_read.rs @@ -23,6 +23,7 @@ use aienos_kernel::block::BlockDevice; use aienos_kernel::console::EarlyConsole; use aienos_kernel::display::Screen; use aienos_kernel::dma_gate::{self, BusMasterSweep, DmaGrant, PciConfig, PCI_COMMAND}; +use aienos_kernel::nvme::atomicity::AtomicityDecision; use aienos_kernel::nvme::driver::{Delay, DmaMemory, DmaRegion, NvmeController, NvmeError}; use aienos_kernel::nvme::Registers; use core::fmt::Write; @@ -374,6 +375,21 @@ fn hex_digest(out: &mut impl Write, digest: &[u8; 32]) { /// Post-exit: bring up the controller, read the sentinel LBA, and report. #[allow(clippy::too_many_arguments)] +/// Render a power-fail atomicity decision into a report line. +fn describe_atomicity(buf: &mut aienos_kernel::report::ReportBuf<512>, d: AtomicityDecision) { + match d { + AtomicityDecision::Atomic { effective_blocks } => { + let _ = write!(buf, "atomic(effective={effective_blocks})"); + } + AtomicityDecision::NotAtomic(reason) => { + let _ = write!(buf, "not_atomic({reason:?})"); + } + AtomicityDecision::Unknown(reason) => { + let _ = write!(buf, "unknown({reason:?})"); + } + } +} + pub fn run( nvme: Option, takeover: Option, @@ -499,6 +515,42 @@ pub fn run( ); say(screen, geometry.as_str()); + // NVMe 1.4 power-fail atomicity observation and System Store root-write + // eligibility. The acceptance marker is emitted only when both Store + // superblock slots satisfy the complete predicate. + if let Some(a) = controller.atomicity() { + let mut fields = aienos_kernel::report::ReportBuf::<512>::new(); + let _ = writeln!( + fields, + "NVME_ATOMICITY_IDENTIFY_QEMU: block_size={} lbads={} awupf_raw={} nawupf_raw={} nabspf_raw={} nabo_blocks={} effective_pf_blocks={} boundary_blocks={}", + a.lba_bytes, + a.lba_bytes.trailing_zeros(), + a.awupf_raw.map_or(-1, i32::from), + a.nawupf_raw.map_or(-1, i32::from), + a.nabspf_raw.map_or(-1, i32::from), + a.nabo_blocks, + a.effective_power_fail_blocks().map_or(-1, |v| v as i64), + a.boundary_blocks().map_or(-1, |v| v as i64), + ); + say(screen, fields.as_str()); + + let slot0 = a.store_root_decision(0); + let slot1 = a.store_root_decision(1); + let both_atomic = matches!(slot0, AtomicityDecision::Atomic { .. }) + && matches!(slot1, AtomicityDecision::Atomic { .. }); + let mut decision = aienos_kernel::report::ReportBuf::<512>::new(); + let _ = write!( + decision, + "NVME_STORE_ROOT_ATOMICITY_QEMU: {} (slot0=", + if both_atomic { "PASS" } else { "FAIL" } + ); + describe_atomicity(&mut decision, slot0); + let _ = write!(decision, " slot1="); + describe_atomicity(&mut decision, slot1); + let _ = writeln!(decision, ")"); + say(screen, decision.as_str()); + } + if let Err(error) = controller.create_io_queues(4) { let mut msg = aienos_kernel::report::ReportBuf::<256>::new(); let _ = writeln!(msg, "NVME_READ_QEMU: FAIL (create_io_queues {error:?})"); diff --git a/crates/aienos-kernel/src/nvme.rs b/crates/aienos-kernel/src/nvme.rs index 7ea33abd..a38dc26a 100644 --- a/crates/aienos-kernel/src/nvme.rs +++ b/crates/aienos-kernel/src/nvme.rs @@ -3,6 +3,7 @@ use core::convert::TryInto; +pub mod atomicity; pub mod driver; pub const REG_CAP: u32 = 0x00; diff --git a/crates/aienos-kernel/src/nvme/atomicity.rs b/crates/aienos-kernel/src/nvme/atomicity.rs new file mode 100644 index 00000000..9475ef42 --- /dev/null +++ b/crates/aienos-kernel/src/nvme/atomicity.rs @@ -0,0 +1,334 @@ +//! NVMe power-fail atomic-write qualification for the System Store root write. +//! +//! NVMe reports power-fail atomic write units as **0-based** logical-block +//! counts (decoded size = raw + 1). The effective guarantee for a namespace is +//! the namespace value (`NAWUPF`) when the namespace advertises one, otherwise +//! the controller value (`AWUPF`). A write is power-fail atomic only if it fits +//! entirely inside that unit AND does not cross an atomic boundary +//! (`NABSPF`/`NABO`). If the guarantee cannot be established, qualification +//! fails closed. +//! +//! Field byte offsets follow NVMe 1.4 Identify Controller (Figure 249) and +//! Identify Namespace (Figure 247). + +/// Identify Controller AWUPF byte offset (u16 LE, 0-based). +pub const ID_CTRL_AWUPF_OFFSET: usize = 528; +/// Identify Namespace NAWUPF byte offset (u16 LE, 0-based). +pub const ID_NS_NAWUPF_OFFSET: usize = 36; +/// Identify Namespace NABO byte offset (u16 LE, blocks). +pub const ID_NS_NABO_OFFSET: usize = 42; +/// Identify Namespace NABSPF byte offset (u16 LE, 0-based blocks). +pub const ID_NS_NABSPF_OFFSET: usize = 44; +/// Permanent System Store unit size the root activation write must cover. +pub const STORE_UNIT_BYTES: u64 = 4096; + +/// Read a little-endian u16 at `offset`, or `None` if the page is too short. +pub fn read_u16_le(bytes: &[u8], offset: usize) -> Option { + let end = offset.checked_add(2)?; + let slice = bytes.get(offset..end)?; + Some(u16::from_le_bytes([slice[0], slice[1]])) +} + +/// Decoded atomicity fields for one namespace. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct AtomicityFields { + pub lba_bytes: u32, + /// Controller AWUPF raw value (0-based); `None` if not observed. + pub awupf_raw: Option, + /// Namespace NAWUPF raw value (0-based); `Some(0)`/`None` means the + /// namespace does not advertise its own power-fail unit. + pub nawupf_raw: Option, + /// Namespace atomic boundary size raw value (0-based blocks); 0 = none. + pub nabspf_raw: Option, + /// Namespace atomic boundary offset in logical blocks. + pub nabo_blocks: u16, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum AtomicityRefusal { + EmptyWrite, + ExceedsAtomicUnit { + required_blocks: u64, + guaranteed_blocks: u64, + }, + CrossesAtomicBoundary { + boundary_blocks: u64, + start_lba: u64, + lba_count: u64, + }, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum AtomicityUnknown { + NoPowerFailGuaranteeReported, + UnsupportedGeometry, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum AtomicityDecision { + Atomic { effective_blocks: u64 }, + NotAtomic(AtomicityRefusal), + Unknown(AtomicityUnknown), +} + +impl AtomicityFields { + /// Effective guaranteed power-fail atomic size in logical blocks. + pub fn effective_power_fail_blocks(&self) -> Option { + match self.nawupf_raw { + Some(raw) if raw > 0 => Some(u64::from(raw) + 1), + _ => self.awupf_raw.map(|raw| u64::from(raw) + 1), + } + } + + /// Atomic boundary size in logical blocks, if the namespace reports one. + pub fn boundary_blocks(&self) -> Option { + self.nabspf_raw + .filter(|raw| *raw > 0) + .map(|raw| u64::from(raw) + 1) + } + + fn region(&self, block: u64, size: u64) -> i128 { + let offset = i128::from(self.nabo_blocks % size as u16); + (i128::from(block) - offset).div_euclid(i128::from(size)) + } + + /// Deterministic power-fail atomicity predicate. + pub fn write_is_power_fail_atomic(&self, start_lba: u64, lba_count: u64) -> AtomicityDecision { + if lba_count == 0 { + return AtomicityDecision::NotAtomic(AtomicityRefusal::EmptyWrite); + } + let Some(guaranteed) = self.effective_power_fail_blocks() else { + return AtomicityDecision::Unknown(AtomicityUnknown::NoPowerFailGuaranteeReported); + }; + if lba_count > guaranteed { + return AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { + required_blocks: lba_count, + guaranteed_blocks: guaranteed, + }); + } + if let Some(size) = self.boundary_blocks() { + if self.region(start_lba, size) != self.region(start_lba + lba_count - 1, size) { + return AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { + boundary_blocks: size, + start_lba, + lba_count, + }); + } + } + AtomicityDecision::Atomic { + effective_blocks: guaranteed, + } + } + + /// (start LBA, LBA count) of a 4096-byte Store superblock unit. + pub fn store_unit_lba(&self, slot: u32) -> Option<(u64, u64)> { + let block_size = u64::from(self.lba_bytes); + if block_size == 0 || !STORE_UNIT_BYTES.is_multiple_of(block_size) { + return None; + } + let blocks = STORE_UNIT_BYTES / block_size; + Some((u64::from(slot).checked_mul(blocks)?, blocks)) + } + + /// Qualification decision for one Store superblock slot (0 = A, 1 = B). + pub fn store_root_decision(&self, slot: u32) -> AtomicityDecision { + match self.store_unit_lba(slot) { + Some((start, count)) => self.write_is_power_fail_atomic(start, count), + None => AtomicityDecision::Unknown(AtomicityUnknown::UnsupportedGeometry), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fields( + lba_bytes: u32, + awupf_raw: Option, + nawupf_raw: Option, + nabspf_raw: Option, + nabo_blocks: u16, + ) -> AtomicityFields { + AtomicityFields { + lba_bytes, + awupf_raw, + nawupf_raw, + nabspf_raw, + nabo_blocks, + } + } + + #[test] + fn insufficient_controller_awupf_rejects() { + let f = fields(512, Some(6), None, None, 0); // decoded 7 blocks < 8 + assert_eq!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { + required_blocks: 8, + guaranteed_blocks: 7, + }) + ); + } + + #[test] + fn sufficient_decoded_awupf_accepts() { + let f = fields(512, Some(7), None, None, 0); // decoded 8 blocks + assert_eq!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::Atomic { + effective_blocks: 8 + } + ); + } + + #[test] + fn namespace_override_wins_over_controller() { + // NAWUPF 3h -> 4 blocks; controller AWUPF 15h -> 16 blocks. The + // namespace value governs, so an 8-block write is not atomic. + let f = fields(512, Some(15), Some(3), None, 0); + assert_eq!(f.effective_power_fail_blocks(), Some(4)); + assert_eq!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::NotAtomic(AtomicityRefusal::ExceedsAtomicUnit { + required_blocks: 8, + guaranteed_blocks: 4, + }) + ); + } + + #[test] + fn namespace_zero_falls_back_to_controller() { + // NVMe 1.4: NAWUPF 0h means the controller AWUPF applies. + let f = fields(512, Some(7), Some(0), None, 0); + assert_eq!(f.effective_power_fail_blocks(), Some(8)); + assert!(matches!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn controller_fallback_when_namespace_absent() { + let f = fields(512, Some(7), None, None, 0); + assert!(matches!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn sufficient_unit_but_crosses_boundary_rejects() { + let f = fields(512, Some(15), None, Some(3), 0); // boundary size 4 + assert_eq!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { + boundary_blocks: 4, + start_lba: 0, + lba_count: 8, + }) + ); + // A write inside one boundary region is fine. + assert!(matches!( + f.write_is_power_fail_atomic(0, 4), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn boundary_safe_write_accepts() { + let f = fields(512, Some(7), None, None, 0); + assert!(matches!( + f.write_is_power_fail_atomic(8, 8), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn boundary_offset_is_respected() { + // size 8, offset 4: [0,8) spans regions -1 and 0 -> crosses. + let f = fields(512, Some(15), None, Some(7), 4); + assert!(matches!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::NotAtomic(AtomicityRefusal::CrossesAtomicBoundary { .. }) + )); + // [4,12) lies wholly in region 0 -> atomic. + assert!(matches!( + f.write_is_power_fail_atomic(4, 8), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn store_root_512_geometry() { + let f = fields(512, Some(7), None, None, 0); + assert_eq!(f.store_unit_lba(0), Some((0, 8))); + assert_eq!(f.store_unit_lba(1), Some((8, 8))); + assert!(matches!( + f.store_root_decision(0), + AtomicityDecision::Atomic { .. } + )); + assert!(matches!( + f.store_root_decision(1), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn store_root_4096_geometry() { + let f = fields(4096, Some(0), None, None, 0); // 1 block + assert_eq!(f.store_unit_lba(0), Some((0, 1))); + assert_eq!(f.store_unit_lba(1), Some((1, 1))); + assert!(matches!( + f.store_root_decision(1), + AtomicityDecision::Atomic { .. } + )); + } + + #[test] + fn encoding_edge_zero_and_max() { + assert_eq!( + fields(512, Some(0), None, None, 0).effective_power_fail_blocks(), + Some(1) + ); + assert_eq!( + fields(512, Some(u16::MAX), None, None, 0).effective_power_fail_blocks(), + Some(65536) + ); + } + + #[test] + fn unknown_state_fails_closed() { + let f = fields(512, None, None, None, 0); + assert_eq!( + f.write_is_power_fail_atomic(0, 8), + AtomicityDecision::Unknown(AtomicityUnknown::NoPowerFailGuaranteeReported) + ); + } + + #[test] + fn unsupported_geometry_fails_closed() { + let f = fields(600, Some(15), None, None, 0); + assert_eq!( + f.store_root_decision(0), + AtomicityDecision::Unknown(AtomicityUnknown::UnsupportedGeometry) + ); + } + + #[test] + fn empty_write_rejects() { + let f = fields(512, Some(15), None, None, 0); + assert_eq!( + f.write_is_power_fail_atomic(0, 0), + AtomicityDecision::NotAtomic(AtomicityRefusal::EmptyWrite) + ); + } + + #[test] + fn read_u16_le_bounds() { + let bytes = [0x07u8, 0x00, 0xff]; + assert_eq!(read_u16_le(&bytes, 0), Some(7)); + assert_eq!(read_u16_le(&bytes, 2), None); + assert_eq!(read_u16_le(&bytes, 99), None); + } +} diff --git a/crates/aienos-kernel/src/nvme/driver.rs b/crates/aienos-kernel/src/nvme/driver.rs index 0b795af2..5500619d 100644 --- a/crates/aienos-kernel/src/nvme/driver.rs +++ b/crates/aienos-kernel/src/nvme/driver.rs @@ -9,6 +9,8 @@ use super::{ CSTS_CFS, CSTS_RDY, REG_ACQ, REG_AQA, REG_ASQ, REG_CC, REG_CSTS, }; +use super::atomicity::{self, AtomicityFields}; + pub const PAGE_SIZE: usize = 4096; pub const ADMIN_DEPTH: usize = 8; /// Admin commands complete quickly; bound the wait in real time. @@ -106,6 +108,10 @@ pub struct NvmeController { io_queue: Option, mdts: u8, namespace: Option, + awupf_raw: Option, + nawupf_raw: Option, + nabspf_raw: Option, + nabo_blocks: u16, } impl NvmeController { @@ -153,6 +159,10 @@ impl NvmeController { io_queue: None, mdts: 0, namespace: None, + awupf_raw: None, + nawupf_raw: None, + nabspf_raw: None, + nabo_blocks: 0, }; controller.identify_controller()?; controller.identify_namespace(1)?; @@ -160,7 +170,11 @@ impl NvmeController { } pub fn identify_controller(&mut self) -> Result<(), NvmeError> { - self.mdts = self.submit_identify(0, 1)?[77]; + let bytes = self.submit_identify(0, 1)?; + self.mdts = bytes[77]; + // NVMe 1.4 Identify Controller: AWUPF is a 0-based power-fail atomic + // write unit in logical blocks. + self.awupf_raw = atomicity::read_u16_le(&bytes, atomicity::ID_CTRL_AWUPF_OFFSET); Ok(()) } @@ -182,9 +196,29 @@ impl NvmeController { block_size: 1u32 << lbads, }; self.namespace = Some(info); + // NVMe 1.4 Identify Namespace: NAWUPF/NABO/NABSPF, all little-endian. + // NAWUPF 0h means the controller AWUPF applies; NABSPF 0h means no + // atomic boundary. + self.nawupf_raw = atomicity::read_u16_le(&bytes, atomicity::ID_NS_NAWUPF_OFFSET); + self.nabo_blocks = + atomicity::read_u16_le(&bytes, atomicity::ID_NS_NABO_OFFSET).unwrap_or(0); + self.nabspf_raw = atomicity::read_u16_le(&bytes, atomicity::ID_NS_NABSPF_OFFSET); Ok(info) } + /// The effective power-fail atomicity contract for the identified + /// namespace, or `None` before a namespace has been identified. + pub fn atomicity(&self) -> Option { + let namespace = self.namespace?; + Some(AtomicityFields { + lba_bytes: namespace.block_size, + awupf_raw: self.awupf_raw, + nawupf_raw: self.nawupf_raw, + nabspf_raw: self.nabspf_raw, + nabo_blocks: self.nabo_blocks, + }) + } + /// Create polled I/O completion and submission queues, both with QID 1. pub fn create_io_queues(&mut self, depth: u16) -> Result<(), NvmeError> { // NVMe requires at least 2 entries, and the queue may not exceed the @@ -561,6 +595,10 @@ mod tests { cap_high: u32, /// LBA data size (lbads) the fake reports for namespace 1. lbads: u8, + awupf: u16, + nawupf: u16, + nabspf: u16, + nabo: u16, } impl Registers for FakeRegs { fn read32(&mut self, offset: u32) -> u32 { @@ -643,6 +681,11 @@ mod tests { data[0..8].copy_from_slice(&self.namespace_size.to_le_bytes()); data[26] = 0; data[130] = self.lbads; + data[36..38].copy_from_slice(&self.nawupf.to_le_bytes()); + data[42..44].copy_from_slice(&self.nabo.to_le_bytes()); + data[44..46].copy_from_slice(&self.nabspf.to_le_bytes()); + } else if cns == 1 { + data[528..530].copy_from_slice(&self.awupf.to_le_bytes()); } } 0x05 => { @@ -817,6 +860,10 @@ mod tests { io_phase: true, cap_high: 0, lbads: 9, + awupf: 0, + nawupf: 0, + nabspf: 0, + nabo: 0, }, FakeDma { shared, @@ -859,6 +906,29 @@ mod tests { let _ = dma; } + #[test] + fn parses_power_fail_atomicity_fields() { + use super::atomicity::AtomicityDecision; + let (mut regs, dma) = fixture(); + regs.awupf = 7; // 8 blocks + regs.nawupf = 3; // 4 blocks: namespace overrides + regs.nabspf = 7; // boundary size 8 blocks + regs.nabo = 0; + let controller = NvmeController::init(regs, dma, CountingDelay::default()).unwrap(); + let a = controller.atomicity().expect("atomicity after identify"); + assert_eq!(a.lba_bytes, 512); + assert_eq!(a.awupf_raw, Some(7)); + assert_eq!(a.nawupf_raw, Some(3)); + assert_eq!(a.nabspf_raw, Some(7)); + assert_eq!(a.nabo_blocks, 0); + assert_eq!(a.effective_power_fail_blocks(), Some(4)); + assert_eq!(a.store_unit_lba(1), Some((8, 8))); + assert!(matches!( + a.store_root_decision(0), + AtomicityDecision::NotAtomic(_) + )); + } + #[test] fn initializes_and_identifies_namespace() { let (regs, dma) = fixture(); diff --git a/evidence/p3_nvme_atomicity_2026-09-25.md b/evidence/p3_nvme_atomicity_2026-09-25.md new file mode 100644 index 00000000..8db9610e --- /dev/null +++ b/evidence/p3_nvme_atomicity_2026-09-25.md @@ -0,0 +1,55 @@ +# P3 NVMe power-fail atomicity evidence (2026-09-25) + +Basis for the later recovery-architecture investigation. These results are +preserved as-is; they must NOT be "fixed" in the Store-over-NVMe integration +lane. + +## Native DGX Spark NVMe (read-only Identify; no writes/format/reconfig) + +Device: SAMSUNG MZALC4T0HBL1-00B07, FW NXHB202Q, PCI 0004:01:00.0 (a810), 4.10 TB. +Commands: `sudo nvme id-ctrl /dev/nvme0`, `sudo nvme id-ns /dev/nvme0n1` (plus raw `-b`). + +```text +awun=1023 awupf=0 ; mdts byte 77 = 7 ; nn = 1 +namespace: nsze=8,001,573,552 flbas=0 nlbaf=0 lbads=9 (512 B) + nawupf=0 nabsn=0 nabo=0 nabspf=0 +decoded: effective power-fail atomic unit = 1 x 512 B ; no atomic boundary +Store root: 4096 B = 8 blocks @ 512 B ; 8 > 1 +NATIVE_GB10_STORE_ROOT_ATOMICITY: FAIL (ExceedsAtomicUnit { required: 8, guaranteed: 1 }) +``` +Only one LBA format (512-byte) is advertised (`nlbaf=0`), so no 4K-LBA path. + +## QEMU, 512-byte LBA (default harness geometry) + +```text +NVME_ATOMICITY_IDENTIFY_QEMU: block_size=512 lbads=9 awupf_raw=0 nawupf_raw=0 nabspf_raw=0 nabo_blocks=0 effective_pf_blocks=1 boundary_blocks=-1 +NVME_STORE_ROOT_ATOMICITY_QEMU: FAIL (slot0=not_atomic(ExceedsAtomicUnit { required_blocks: 8, guaranteed_blocks: 1 }) slot1=...) +``` +`QEMU_NVME_RW: PASS (smmu)` and `PASS (fail-closed)` are unaffected. + +## QEMU, 4096-byte LBA (compliant geometry) + +Device: `-device nvme,...,logical_block_size=4096,physical_block_size=4096`. + +```text +NVME_GEOMETRY_QEMU: PASS (nsid=1 block_count=16384 block_size=4096) +NVME_ATOMICITY_IDENTIFY_QEMU: block_size=4096 lbads=12 awupf_raw=0 nawupf_raw=0 nabspf_raw=0 nabo_blocks=0 effective_pf_blocks=1 boundary_blocks=-1 +NVME_STORE_ROOT_ATOMICITY_QEMU: PASS (slot0=atomic(effective=1) slot1=atomic(effective=1)) +QEMU_NVME_ATOMICITY_4K: PASS +``` + +## Reproduce + +```bash +bash scripts/qemu_nvme_rw_test.sh # 512-byte: RW PASS, atomicity FAIL +bash scripts/qemu_nvme_atomicity_test.sh # 4096-byte: atomicity PASS +``` + +## Consequence + +AIEN's System Store v1 commit design is correct under its stated storage +contract (a 4096-byte root write that cannot tear). The native Spark SSD +(512-byte LBA, AWUPF=0 -> 1 block) and the default QEMU namespace do not meet +that contract. A compliant 4096-byte-LBA namespace does. Native adaptation +requires either a compliant device or a recovery-design change for a torn +inactive superblock; ADR 0015 is not revised by this evidence. diff --git a/scripts/qemu_nvme_atomicity_test.sh b/scripts/qemu_nvme_atomicity_test.sh new file mode 100755 index 00000000..9b284b7a --- /dev/null +++ b/scripts/qemu_nvme_atomicity_test.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +# QEMU NVMe power-fail atomicity qualification for the System Store root write. +# +# Configures a disposable namespace with a 4096-byte logical block size and +# requires the guest to observe block_size=4096 (lbads=12) with a power-fail +# atomic guarantee covering the single-block Store superblock: +# NVME_STORE_ROOT_ATOMICITY_QEMU: PASS +# +# This does not weaken the Store contract: the 4096-byte Store unit is one +# logical block on this namespace. It is emulator qualification, not Machine 1 +# evidence. Read/write durability markers are not asserted here. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" + +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" +command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; } +[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; } + +boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}" +img_bytes=67108864 +lba_bytes=4096 +bounds_lba=$(( img_bytes / lba_bytes )) +machine="virt,virtualization=on,gic-version=3,iommu=smmuv3" +target_dir="target/qemu-nvme-atomicity" + +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" +AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \ + -p aienos-boot --target aarch64-unknown-uefi --features nvme-write --bin aienos-handoff \ + --target-dir "${target_dir}" + +work="$(mktemp -d)" +qemu_pid="" +cleanup() { [[ -n "${qemu_pid}" ]] && kill "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; } +trap cleanup EXIT +mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" +touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" +cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" + +image="${work}/nvme.img" +truncate -s "${img_bytes}" "${image}" +echo "nvme image: ${img_bytes} bytes, ${bounds_lba} x ${lba_bytes} B LBAs" + +cp "${vars_fd}" "${work}/vars.fd" +rm -f "${work}/serial.log" +qemu-system-aarch64 \ + -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ + -drive if=pflash,format=raw,file="${work}/vars.fd" \ + -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ + -device virtio-blk-pci,drive=esp \ + -drive if=none,id=nvme0,format=raw,file="${image}" \ + -device nvme,drive=nvme0,serial=aienos-nvme-atomicity,logical_block_size=${lba_bytes},physical_block_size=${lba_bytes} \ + -device ramfb -display none -nic none \ + -serial file:"${work}/serial.log" -no-reboot & +qemu_pid=$! + +deadline=$(( $(date +%s) + boot_timeout )) +while (( $(date +%s) < deadline )) && kill -0 "${qemu_pid}" 2>/dev/null; do + grep -q -- "NVME_STORE_ROOT_ATOMICITY_QEMU" "${work}/serial.log" 2>/dev/null && break + sleep 0.2 +done +kill "${qemu_pid}" 2>/dev/null || true +wait "${qemu_pid}" 2>/dev/null || true + +serial="$(tr -d '\r' <"${work}/serial.log" 2>/dev/null || true)" +failed=0 +check() { if printf '%s' "${serial}" | grep -q -- "$2"; then echo "PASS $1"; else echo "FAIL $1"; failed=1; fi; } + +echo "---- observed ----" +printf '%s\n' "${serial}" | grep -E 'NVME_IDENTIFY_QEMU|NVME_GEOMETRY_QEMU|NVME_ATOMICITY_IDENTIFY_QEMU|NVME_STORE_ROOT_ATOMICITY_QEMU' || true +echo "-------------------" +check "controller and namespace identify completed" "NVME_IDENTIFY_QEMU: PASS" +check "namespace geometry is 4096-byte LBA" "NVME_GEOMETRY_QEMU: PASS (nsid=1 block_count=${bounds_lba} block_size=${lba_bytes})" +check "atomicity Identify observed with 4096-byte LBA" "NVME_ATOMICITY_IDENTIFY_QEMU: block_size=${lba_bytes} lbads=12" +check "Store root write is power-fail atomic and boundary-safe" "NVME_STORE_ROOT_ATOMICITY_QEMU: PASS" + +[[ "${failed}" == 0 ]] && echo "QEMU_NVME_ATOMICITY_4K: PASS" || { echo "QEMU_NVME_ATOMICITY_4K: FAIL"; exit 1; } diff --git a/scripts/qemu_nvme_rw_test.sh b/scripts/qemu_nvme_rw_test.sh index ad589b1c..47e1ba39 100755 --- a/scripts/qemu_nvme_rw_test.sh +++ b/scripts/qemu_nvme_rw_test.sh @@ -179,6 +179,14 @@ if [[ "${mode}" == "smmu" ]]; then check "ECAM discovery found the NVMe class device" "NVME_DISCOVERY_QEMU: PASS" check "controller and namespace identify completed" "NVME_IDENTIFY_QEMU: PASS" check "namespace geometry matches the image" "NVME_GEOMETRY_QEMU: PASS (nsid=1 block_count=${bounds_lba} block_size=${lba_bytes})" + check "NVMe power-fail atomicity fields observed" "NVME_ATOMICITY_IDENTIFY_QEMU:" + if grep -q -- "NVME_STORE_ROOT_ATOMICITY_QEMU: PASS" "${boot1}"; then + echo "PASS Store root-write power-fail atomicity satisfied" + else + echo "INFO Store root-write power-fail atomicity NOT proven on this namespace" + grep -o -- "NVME_ATOMICITY_IDENTIFY_QEMU:.*" "${boot1}" | head -1 | sed 's/^/INFO /' || true + grep -o -- "NVME_STORE_ROOT_ATOMICITY_QEMU:.*" "${boot1}" | head -1 | sed 's/^/INFO /' || true + fi check "read sentinel LBA returned exact bytes and SHA-256" "NVME_READ_QEMU: PASS (lba=${sentinel_lba} blocks=1 bytes=512 sha256=${sentinel_sha})" check "read past the namespace end is rejected" "NVME_BOUNDS_QEMU: PASS" check "device-reported read error is surfaced" "NVME_ERROR_QEMU: PASS" ```