aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / docs / P3_NVME_WRITE_FLUSH_SUBSTRATE.md
| 1 | # D5 - QEMU native NVMe Write + Flush durability harness (P3) |
| 2 | |
| 3 | > **Status: implemented and passing in QEMU (2026-09-25).** The merged |
| 4 | > implementation keeps the read phase in `crates/aienos-boot/src/nvme_read.rs` |
| 5 | > and adds the write/flush phase there under the `nvme-write` feature (whose |
| 6 | > harness is `scripts/qemu_nvme_rw_test.sh`), rather than a separate |
| 7 | > `nvme_write.rs` module or a mode file. The phase is content-adaptive: on a |
| 8 | > boot where the target LBA already holds the expected pattern it reports |
| 9 | > cross-restart durability; otherwise it writes, flushes, and reads back. See |
| 10 | > [`evidence/p3_nvme_write_flush_qemu_2026-09-25.md`](../evidence/p3_nvme_write_flush_qemu_2026-09-25.md). |
| 11 | > The design below remains the rationale and mode contract. |
| 12 | |
| 13 | Target placement: `scripts/qemu_nvme_rw_test.sh` (this lane authors a candidate |
| 14 | at `nvme-rw-subagents/d5/qemu_nvme_rw_test.sh`). Companion to the merged read |
| 15 | qualification `scripts/qemu_nvme_test.sh` and follows the mode conventions of |
| 16 | `scripts/qemu_keyboard_test.sh`. |
| 17 | |
| 18 | Base: `aien-dev/aienux` @ `ef359ec`. Read-only against the repo; no repo edits, |
| 19 | no `cargo`, no `qemu`, no hardware, no Python. |
| 20 | |
| 21 | ## 1. What is being proved |
| 22 | |
| 23 | The read lane (`crates/aienos-boot/src/nvme_read.rs`) deliberately issues only |
| 24 | Identify (`0x06`) and Read (`0x02`) and states that "write and flush are not |
| 25 | claimed, proven, or exercised" (`nvme_read.rs:9-11`, `docs/P3_NVME_READ_SUBSTRATE.md:21-25`). |
| 26 | This lane proves the next two things: |
| 27 | |
| 28 | 1. **Write + flush reaches durable media.** The native driver writes a known |
| 29 | pattern to a known LBA and issues an NVM Flush (`BlockDevice::flush`, |
| 30 | `crates/aienos-kernel/src/nvme/driver.rs:432-439`). After QEMU is gone, the |
| 31 | **host image file** at that LBA holds exactly the expected bytes and hash. |
| 32 | 2. **Durability across a restart.** A second QEMU run over the same image file |
| 33 | reads that LBA back through the native driver and reports the exact bytes |
| 34 | and SHA-256 (guest hash), which must equal the host's hash. |
| 35 | |
| 36 | The kernel already exposes the primitives this lane needs: |
| 37 | `BlockDevice::write_blocks`/`flush` (`crates/aienos-kernel/src/block.rs:15-16`) |
| 38 | and the NVMe implementation `write_blocks` (`driver.rs:428-431`) and `flush` |
| 39 | (`driver.rs:432-439`). The write module reuses the read module's pre-exit |
| 40 | discovery and post-exit scaffolding: `find_nvme` (`nvme_read.rs:83`), |
| 41 | `take_over_dma` (`nvme_read.rs:203`), the `Ecam` adapter (`nvme_read.rs:170`), |
| 42 | `NativeRegisters`/`NativeDma`/`NativeDelay`, the DMA gate |
| 43 | (`nvme_read.rs:413-461`), and `revoke_dma` (`nvme_read.rs:232`). |
| 44 | |
| 45 | ## 2. Image geometry, target LBA and payloads |
| 46 | |
| 47 | Built only with `dd` / `od` / `printf` / `truncate` / `sha256sum`, exactly like |
| 48 | `qemu_nvme_test.sh:94-131`; no Python. |
| 49 | |
| 50 | | Property | Value | |
| 51 | |---|---| |
| 52 | | Image size | 64 MiB = `67108864` bytes | |
| 53 | | LBA size | 512 bytes | |
| 54 | | LBA count / bounds LBA | `131072` (`== block_count`) | |
| 55 | | **Target LBA** | **4096** (offset **2097152**) | |
| 56 | | Read-lane sentinel LBA (untouched here) | 2048 (offset 1048576) | |
| 57 | |
| 58 | Target LBA 4096 is deliberately distinct from the read sentinel LBA 2048 |
| 59 | (`nvme_read.rs:41`, `qemu_nvme_test.sh:107`) so a write can never be confused |
| 60 | with a read-lane sentinel check. |
| 61 | |
| 62 | Payloads are one 512-byte LBA: a 16-byte magic repeated 32 times, no newline |
| 63 | (the read lane uses `AIENOS-NVME-SENT` x32 at `nvme_read.rs:40` and |
| 64 | `qemu_nvme_test.sh:104,113-116`). |
| 65 | |
| 66 | | Name | Magic (x32 = 512 B) | SHA-256 | |
| 67 | |---|---|---| |
| 68 | | **OLD** (host seed, pre-existing bytes) | `AIENOS-NVME-OLD0` | `46fe054d5bfa8bf3b14ce240830da7445b26ebebe273349f9af22e6454244379` | |
| 69 | | **NEW** (guest writes, expected durable bytes) | `AIENOS-NVME-RW01` | `76fa34c132173497a7bcd0c9618ebdcf0629f550934c164adf81cfa05e6a4451` | |
| 70 | |
| 71 | The host seeds OLD at LBA 4096, then `od`-checks the first 16 bytes and |
| 72 | `sha256sum`s the seeded block before boot (`qemu_nvme_rw_test.sh` image build, |
| 73 | mirroring `qemu_nvme_test.sh:118-129`). |
| 74 | |
| 75 | ## 3. Restart simulation and per-boot modes |
| 76 | |
| 77 | Restart is **two QEMU runs over the same `nvme.img` file**. The image is built |
| 78 | once and is never rebuilt between boots; `cache=writethrough` on the NVMe |
| 79 | `-drive` keeps the host file coherent with each guest flush. QEMU is asked to |
| 80 | exit cleanly via the HMP monitor (`quit`) before any signal, so block backends |
| 81 | flush to the host file (`qemu_nvme_rw_test.sh` `stop_qemu`). |
| 82 | |
| 83 | The guest reads its mode from an ESP file, because the handoff image has no |
| 84 | runtime environment and `AIENOS_RESTART_SECS` is a **compile-time** env var |
| 85 | (`qemu_nvme_test.sh:78`). Contract: |
| 86 | |
| 87 | - File: `\EFI\AIENOS\NVME_RW_MODE.TXT` on the ESP. |
| 88 | - Exact bytes, no trailing newline: `write` or `verify`. |
| 89 | - Read **before** `ExitBootServices` (boot services still live) and passed into |
| 90 | the post-exit phase; post-exit code cannot use the file protocol. |
| 91 | |
| 92 | | Boot | Mode file | Guest action | Host action | |
| 93 | |---|---|---|---| |
| 94 | | 1 | `write` | Discover, identify, write NEW at LBA 4096, `flush()`, run write bounds + device-error probes | After QEMU exits, read image at LBA 4096; require NEW bytes and `new_sha` | |
| 95 | | 2 | `verify` | Discover, identify, `read_blocks(4096)`; report guest hash and durability | Cross-check guest hash == `new_sha`; require it to match the host hash | |
| 96 | |
| 97 | Each boot copies a fresh `vars.fd` from `AAVMF_VARS`, exactly as |
| 98 | `qemu_nvme_test.sh:157`. A boot with no AIENOS output is retried (firmware hang, |
| 99 | issue #61, `qemu_nvme_test.sh:25-26,185-189`); a failure after AIENOS output |
| 100 | fails immediately. Retrying boot 1 is safe because the mode is explicit |
| 101 | (`write` rewrites NEW idempotently); it does not depend on image state. |
| 102 | |
| 103 | ## 4. Exact markers per boot |
| 104 | |
| 105 | Markers follow the read lane's `NVME_*_QEMU: PASS` style |
| 106 | (`nvme_read.rs:392,481,491,516,541,557`). The write module must emit: |
| 107 | |
| 108 | **Boot 1 (`write`), granted-DMA modes (`smmu`, `unsafe-bypass`):** |
| 109 | |
| 110 | - `NVME_DISCOVERY_QEMU: PASS` - ECAM walk found class `0x0108`. |
| 111 | - `NVME_IDENTIFY_QEMU: PASS` - controller + namespace identify completed. |
| 112 | - `NVME_WRITE_QEMU: PASS (lba=4096 blocks=1 bytes=512 sha256=76fa34c1…)` - |
| 113 | `write_blocks(TARGET_LBA, NEW)` returned `Ok`, and the module hashed the NEW |
| 114 | buffer it submitted. |
| 115 | - `NVME_FLUSH_QEMU: PASS (nsid=1)` - `flush()` returned `Ok`. |
| 116 | - `NVME_WRITE_BOUNDS_QEMU: PASS (lba=131072 rejected OutOfRange no_command)` - |
| 117 | `write_blocks(block_count, …)` rejected client-side with no doorbell write, |
| 118 | the write-side analogue of `NVME_BOUNDS_QEMU` (`nvme_read.rs:536-548`). |
| 119 | - `NVME_WRITE_ERROR_QEMU: PASS` - a device-reported error is surfaced, not |
| 120 | swallowed. Normal path: an unallocated namespace identify |
| 121 | (`identify_namespace(0xffff_ffff)` → `NvmeError::CompletionStatus`, as |
| 122 | `nvme_read.rs:552-564`). Error-injection path: see §5. |
| 123 | |
| 124 | In every path where DMA was granted the module must revoke it |
| 125 | (`dma_gate: nvme bus master revoked`, `nvme_read.rs:232-247,566`) - including |
| 126 | when the write itself fails in `write-error` mode - so the host's revoke check |
| 127 | holds in all granted-DMA modes. |
| 128 | |
| 129 | **Boot 2 (`verify`):** |
| 130 | |
| 131 | - `NVME_DISCOVERY_QEMU: PASS`, `NVME_IDENTIFY_QEMU: PASS`. |
| 132 | - `NVME_READ_QEMU: PASS (lba=4096 blocks=1 bytes=512 sha256=76fa34c1…)` - the |
| 133 | guest's own SHA-256 of the block it read natively (**guest hash**); it must |
| 134 | equal `new_sha`. |
| 135 | - `NVME_DURABILITY_QEMU: PASS (lba=4096 restarted guest_sha256=76fa34c1…)` - |
| 136 | guest asserts the readback bytes equal NEW after restart. |
| 137 | |
| 138 | The host emits the final `QEMU_NVME_RW: PASS (${mode})`, mirroring |
| 139 | `QEMU_NVME: PASS` (`qemu_nvme_test.sh:278`). The required marker set is |
| 140 | therefore: `NVME_DISCOVERY_QEMU`, `NVME_IDENTIFY_QEMU`, `NVME_READ_QEMU`, |
| 141 | `NVME_WRITE_QEMU`, `NVME_FLUSH_QEMU`, `NVME_WRITE_BOUNDS_QEMU`, |
| 142 | `NVME_WRITE_ERROR_QEMU`, `NVME_DURABILITY_QEMU`, `QEMU_NVME_RW: PASS`. |
| 143 | |
| 144 | ## 5. Modes |
| 145 | |
| 146 | Selected by the same env conventions as `qemu_nvme_test.sh:48-73` / |
| 147 | `qemu_keyboard_test.sh:37-61`. |
| 148 | |
| 149 | - **`smmu` (default)** - `-M virt,…,iommu=smmuv3`; feature `nvme-write`. Full |
| 150 | two-boot write → flush → persist → restart-read proof. Requires |
| 151 | `smmu: enabled`, `smmu_dma_window: nvme only, translation active`, |
| 152 | `dma_gate: nvme granted (Confined), bus master on`, and |
| 153 | `dma_gate: nvme bus master revoked` (`nvme_read.rs:233-247,430-461`). |
| 154 | - **`fail-closed`** - `AIENOS_QEMU_SMMU=0`, no SMMU. The controller must stay |
| 155 | unavailable (`dma_gate: nvme denied (NoSmmu), bus master stays off` and |
| 156 | `nvme: unavailable (SMMU DMA isolation not active)`, `nvme_read.rs:419-428`). |
| 157 | No write/read/flush/durability marker may appear, and the host must still see |
| 158 | OLD at LBA 4096. |
| 159 | - **`write-error`** - `AIENOS_NVME_WRITE_ERROR=1`. SMMU on, but the backend is |
| 160 | attached with `-drive …,readonly=on`. QEMU rejects the media write, so the |
| 161 | driver must surface a device-reported WRITE error: |
| 162 | `NVME_WRITE_ERROR_QEMU: PASS`, with **no** `NVME_WRITE_QEMU: PASS` and no |
| 163 | `NVME_FLUSH_QEMU`, and the host must still see OLD at LBA 4096. This is the |
| 164 | required dedicated error-injection mode. |
| 165 | - **`unsafe-bypass`** - `AIENOS_UNSAFE_DMA_BYPASS=1`. Same as `smmu` but the |
| 166 | unsafe debug build lets NVMe DMA run unconfined; announces |
| 167 | `WARNING: UNSAFE NVME DMA BYPASS BUILD` / `… ACTIVE` |
| 168 | (`nvme_read.rs:34-36,443-450`). QEMU debugging only; never in `verify_all.sh`. |
| 169 | |
| 170 | ## 6. Host-vs-guest hash agreement |
| 171 | |
| 172 | - Host builds OLD and NEW, hashing both with `sha256sum` (values in §2). |
| 173 | - After boot 1, host runs |
| 174 | `dd if=nvme.img bs=512 skip=4096 count=1 | sha256sum` and requires it to equal |
| 175 | `new_sha`; this is the power-off persistence check. |
| 176 | - Boot 2's guest prints the SHA-256 of its own native readback in |
| 177 | `NVME_READ_QEMU` and asserts byte equality in `NVME_DURABILITY_QEMU`. The host |
| 178 | greps the exact expected string, so `guest_sha256 == new_sha == host_sha`. |
| 179 | Agreement across all three is the durability proof. |
| 180 | |
| 181 | ## 7. Integration prerequisites (owned by the integration lane) |
| 182 | |
| 183 | This harness assumes the write lane adds, mirroring the read lane: |
| 184 | |
| 185 | 1. Feature `nvme-write = ["handoff"]` in `crates/aienos-boot/Cargo.toml` (next |
| 186 | to `nvme-read` at `Cargo.toml:23`). |
| 187 | 2. Module `nvme_write` gated in `crates/aienos-boot/src/handoff.rs` and a |
| 188 | post-exit `nvme_write::run(...)` call next to the read call |
| 189 | (`handoff.rs:560-561`, `handoff.rs:2110-2119`). |
| 190 | 3. The `\EFI\AIENOS\NVME_RW_MODE.TXT` reader described in §3. |
| 191 | 4. If `unsafe-bypass` is kept: `unsafe-debug-nvme-dma-without-smmu` currently |
| 192 | depends on `["nvme-read"]` (`Cargo.toml:48`); it must additionally enable |
| 193 | `nvme-write`, or the bypass build will not contain the write module. |
| 194 | |
| 195 | `scripts/verify_all.sh` is **not** edited by this lane. Wiring this harness in |
| 196 | (a two-boot RW QEMU step plus the aarch64 clippy feature loop in step 3b) is a |
| 197 | follow-up, exactly as the read harness notes at `qemu_nvme_test.sh:30-33`. |