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_DMA_REVIEW.md
| 1 | # D6 - SMMU / DMA review: P3 Native NVMe Write + Flush lane |
| 2 | |
| 3 | Repo: `aien-dev/aienux`, base `ef359ec` (mirror `~/aienos-nvme-rw`). |
| 4 | Scope: read-only review of the DMA/SMMU authority that a future write+flush lane |
| 5 | would inherit from the existing read substrate. No edits, no cargo, no hardware. |
| 6 | |
| 7 | Verdict up front: the read lane already establishes a single, confined DMA |
| 8 | authority. Because write and read share the same driver `transfer()` and the |
| 9 | same `NativeDma`/`DmaMemory` implementation, a write lane that reuses the |
| 10 | existing module inherits that authority unchanged. There is **no write lane in |
| 11 | this tree yet** (`crates/aienos-kernel/src/nvme/` contains only `driver.rs`; no |
| 12 | `nvme_write.rs`), so the lane must be added without introducing a second window, |
| 13 | a second SMMU policy, or a second BME toggle. |
| 14 | |
| 15 | --- |
| 16 | |
| 17 | ## 1. Write DMA uses the SAME confined authority as read DMA |
| 18 | |
| 19 | ### 1.1 One static arena window for both directions |
| 20 | |
| 21 | - `ARENA_BYTES = 128 * 1024` and the single `static mut DMA_ARENA: DmaArena` |
| 22 | are defined once - `crates/aienos-boot/src/nvme_read.rs:43`, `:45-48`. |
| 23 | - `NvmeLocation::dma_window()` returns exactly that static, page-aligned: |
| 24 | `nvme_read.rs:75-78` (`base & !0xfff`, `size_of::<DmaArena>()`). |
| 25 | - There is no second arena and no per-direction window. `grep` for |
| 26 | `DMA_ARENA`/`dma_window` finds only this one definition plus its users |
| 27 | (`nvme_read.rs:76,271,279,313,331,457`). |
| 28 | - The SMMU Stage-1 policy is built from that same call: |
| 29 | `configure_smmu_for_nvme` takes `nvme.dma_window()` (`handoff.rs:305`) and |
| 30 | passes a **single** `DmaWindow { iova: dma_base, pa: dma_base, length }` to |
| 31 | `build_dma_policy` - `handoff.rs:317-327`. |
| 32 | - `build_dma_policy` requires identity (`window.iova != window.pa.0` → |
| 33 | `InvalidWindow`) and maps only the supplied windows; anything else does not |
| 34 | translate - `crates/aienos-kernel/src/smmu.rs:739-746`. |
| 35 | |
| 36 | ### 1.2 Same `dma_gate` decision for the whole phase |
| 37 | |
| 38 | - The gate is evaluated **once**, before any command, in `run`: |
| 39 | `dma_gate::dma_grant(smmu_ready, smmu_base.is_some(), UNSAFE_NVME_DMA_BYPASS)` |
| 40 | - `nvme_read.rs:413-417`. Its result gates the entire run (read today, write |
| 41 | later); there is no second grant call. |
| 42 | - Decision logic is direction-agnostic: confined iff `smmu_ready`, bypass only |
| 43 | when no SMMU exists AND the unsafe feature is compiled, never a fallback after |
| 44 | a failed SMMU - `crates/aienos-kernel/src/dma_gate.rs:68-82`, test |
| 45 | `failed_smmu_never_falls_back_to_bypass` `:279-282`. |
| 46 | - `nvme_read::run` is invoked with `nvme_smmu_result.is_ok()` as `smmu_ready` |
| 47 | (`handoff.rs:2111-2117`), i.e. the grant is tied to the same |
| 48 | `configure_smmu_for_nvme` result (`handoff.rs:1794`). |
| 49 | |
| 50 | ### 1.3 Same allocator and same `DmaMemory` path |
| 51 | |
| 52 | - `NativeDma` bump-allocates only inside `DMA_ARENA`, bounds-checked against |
| 53 | `ARENA_BYTES` - `nvme_read.rs:269-283`, `:286-305`. |
| 54 | - Write and read commands both flow through `NvmeController::transfer(write, …)` |
| 55 | (`crates/aienos-kernel/src/nvme/driver.rs:293-362`): writes copy data into |
| 56 | `region.bytes` then `dma.write(...)` (`driver.rs:325-330`); reads call |
| 57 | `dma.read(...)` (`driver.rs:354-357`). Both go through the same |
| 58 | `D: DmaMemory` = `NativeDma`. |
| 59 | - `write_blocks` is a thin wrapper over the same `transfer(true, …)` |
| 60 | (`driver.rs:428-431`); `flush` uses the same `submit_io` |
| 61 | (`driver.rs:432-439`). No separate DMA authority exists for writes. |
| 62 | |
| 63 | **Confirmed:** one window, one SMMU policy, one gate decision, one allocator. |
| 64 | No separate or widened window for writes. |
| 65 | |
| 66 | --- |
| 67 | |
| 68 | ## 2. No unconstrained bus mastering |
| 69 | |
| 70 | - Bus Master Enable is cleared on the controller's whole ECAM segment **before** |
| 71 | any SMMU or driver setup: `take_over_dma` → `dma_gate::sweep_bus_master` |
| 72 | (`nvme_read.rs:201-210`), called at `handoff.rs:1790` (and the sweep clears |
| 73 | every endpoint with BME set - `dma_gate.rs:150-218`). |
| 74 | - `run` refuses to proceed if BME did not clear: |
| 75 | `if dma_gate::bus_master_enabled(command) { … return; }` |
| 76 | (`nvme_read.rs:409-412`). |
| 77 | - BME is set **only after** `dma_grant` returns `Ok`: |
| 78 | `ecam.set_command(&at, dma_gate::with_bus_master(command))` at |
| 79 | `nvme_read.rs:460`, which is after the `match dma_gate::dma_grant(...)` at |
| 80 | `:413-429`. On denial it returns with BME still off (`:419-428`). |
| 81 | - BME is revoked after the phase: `revoke_dma` clears BME and re-reads to |
| 82 | confirm (`nvme_read.rs:232-247`), called on every post-grant exit: |
| 83 | init failure `:473`, queue failure `:499`, normal completion `:566`. |
| 84 | - Only one `with_bus_master` call site exists for NVMe |
| 85 | (`nvme_read.rs:460`); the only other is xHCI (`usb_keyboard.rs:341`), and the |
| 86 | two features are documented as mutually exclusive builds |
| 87 | (`handoff.rs:1785-1786`). |
| 88 | |
| 89 | **Confirmed:** BME set only after `dma_grant` `Ok`; revoked after the phase. |
| 90 | |
| 91 | --- |
| 92 | |
| 93 | ## 3. Unsafe QEMU bypass cannot enter a hardware-staging image |
| 94 | |
| 95 | - Feature definition: `unsafe-debug-nvme-dma-without-smmu = ["nvme-read"]` |
| 96 | (`crates/aienos-boot/Cargo.toml:42-48`); `hardware-staging = ["handoff"]` |
| 97 | (`:32`). |
| 98 | - Compile guard: `#[cfg(all(feature = "unsafe-debug-nvme-dma-without-smmu", |
| 99 | feature = "hardware-staging"))] compile_error!(...)` - `src/lib.rs:26-33`. |
| 100 | The xHCI bypass has the equivalent guard - `lib.rs:12-19`. |
| 101 | - Banner: `UNSAFE_NVME_BANNER` is printed on every boot of such an image |
| 102 | (`nvme_read.rs:33-36`, `:379-381`), and the active-grant warning is printed |
| 103 | when the bypass actually grants DMA (`nvme_read.rs:443-450`). |
| 104 | - The bypass cannot be requested silently: the QEMU harness requires |
| 105 | `AIENOS_UNSAFE_DMA_BYPASS=1` and refuses the feature otherwise |
| 106 | (`scripts/qemu_nvme_test.sh:49-71`); the SMMU harness forces it off |
| 107 | (`scripts/qemu_smmu_test.sh:3,8`). The hardware staging script builds |
| 108 | `handoff,hardware-staging` with no bypass (`scripts/stage_one_time_boot.sh:32`). |
| 109 | - The harness asserts the confined path: denied without SMMU |
| 110 | (`qemu_nvme_test.sh:246-248`), confined grant + revoke (`:252-261`), and |
| 111 | `check_absent "no unsafe DMA bypass in this image"` on non-bypass runs |
| 112 | (`:263-264`). |
| 113 | |
| 114 | **Confirmed:** unbuildable with `hardware-staging`; banner on every boot; loud |
| 115 | warning when active. |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | ## 4. Cache maintenance on the write path |
| 120 | |
| 121 | CPU-writes-cleaned-to-device and device-writes-invalidated-before-CPU-read are |
| 122 | both present, and both are issued with a trailing `dsb`. |
| 123 | |
| 124 | - `clean_dcache_range` = `dc cvac` per line + `dsb` - `arch/aarch64.rs:270-278`, |
| 125 | helper ends with `dsb()` at `:291-301`. |
| 126 | - `clean_invalidate_dcache_range` = `dc civac` per line + `dsb` - |
| 127 | `arch/aarch64.rs:282-289` (clean-first so no CPU write in a shared line is |
| 128 | lost; documented `:279-281`). |
| 129 | - Write direction: `NativeDma::write` copies into the arena then calls |
| 130 | `clean_dcache_range` - `nvme_read.rs:326-339`. In `transfer`, write payloads |
| 131 | reach the device only via `dma.write` (`driver.rs:325-330`). |
| 132 | - Read direction: `NativeDma::read` calls `clean_invalidate_dcache_range` |
| 133 | **before** the CPU copy - `nvme_read.rs:307-324`; used at |
| 134 | `driver.rs:354-357`. |
| 135 | - Queue traffic is on the same discipline: SQ command written via `dma.write` |
| 136 | (clean) before the doorbell (`driver.rs:372-378`); CQ read via `dma.read` |
| 137 | (clean+invalidate) before inspecting phase/status (`driver.rs:380-383`, |
| 138 | admin `:240-243`). |
| 139 | - Host tests exercise the write path end-to-end, including PRP-list and MDTS |
| 140 | chunking (`driver.rs:917-945`) and write-completion-error propagation |
| 141 | (`driver.rs:947-977`). |
| 142 | |
| 143 | **Confirmed:** correct direction of maintenance for write (clean) and read |
| 144 | (clean+invalidate), each fenced with `dsb`. |
| 145 | |
| 146 | --- |
| 147 | |
| 148 | ## 5. Gaps / risks specific to the write+flush lane |
| 149 | |
| 150 | 1. **No write lane exists.** `nvme_read.rs` is explicitly read-only and claims |
| 151 | no write/flush (`nvme_read.rs:9-11`). The lane must add a new module/feature |
| 152 | (e.g. `nvme-write`) and QEMU harness; it must not simply call |
| 153 | `write_blocks` from the read qualification path without its own evidence |
| 154 | gates. `driver.rs` already implements `write_blocks`/`flush`, so the driver |
| 155 | side is ready. |
| 156 | |
| 157 | 2. **Arena exhaustion (functional, fail-closed).** `NativeDma` never frees |
| 158 | (`nvme_read.rs:269-305`), and `transfer`'s MDTS chunk cap is 128 KiB |
| 159 | (`driver.rs:308-316`) - exactly `ARENA_BYTES` (`nvme_read.rs:43`). Admin |
| 160 | queues + identify + I/O queues already consume part of the arena |
| 161 | (`driver.rs:125-127,196-200`), so a maximum-size transfer cannot fit. The |
| 162 | write lane must bound per-command chunk size well below the arena (or add |
| 163 | arena reuse/free), otherwise writes fail with `NvmeError::Dma`. Fail-closed, |
| 164 | but it caps the lane's usefulness and must be an explicit, tested limit. |
| 165 | |
| 166 | 3. **BME is not revoked on a panic/fault during the phase.** Revocation happens |
| 167 | only on the enumerated normal/error returns (`nvme_read.rs:473,499,566`). |
| 168 | `fatal_report` resets without clearing BME (`handoff.rs:1571+`), so a fault |
| 169 | mid-write leaves BME set until the cold reset. The write lane should clear |
| 170 | BME in the fault path (or explicitly accept reset as the only containment). |
| 171 | |
| 172 | 4. **Flush has no durable evidence yet.** `flush()` submits opcode 0 with |
| 173 | `cdw10=1` through `submit_io` (`driver.rs:432-439`), which only distinguishes |
| 174 | `Ok`/`DeviceError` (`driver.rs:399-403`). The lane must emit a distinct |
| 175 | `NVME_FLUSH_*` evidence line and must only claim durability after a |
| 176 | successful flush completion; it should also record that all prior writes were |
| 177 | submitted serially (they are: each `submit_io` waits for completion - |
| 178 | `driver.rs:379-404`). |
| 179 | |
| 180 | 5. **Namespace bounds are enforced before any command** (`driver.rs:301-307`, |
| 181 | `OutOfRange`), so a write cannot escape the namespace. Keep this invariant: |
| 182 | do not add a raw command path that bypasses `transfer`. |
| 183 | |
| 184 | 6. **Cache-maintenance caveat.** The maintenance is unconditional. If the arena |
| 185 | mapping were ever Device-nGnRnE rather than Normal memory, `dc cvac`/`civac` |
| 186 | semantics would be wrong. Today the arena is Normal identity-mapped |
| 187 | (`handoff.rs:266-270` comment; `map_plan` identity path). The lane must not |
| 188 | change the arena mapping attributes. |
| 189 | |
| 190 | ### Invariants the write+flush lane MUST preserve |
| 191 | |
| 192 | - Reuse `NvmeLocation` / `DMA_ARENA` / `NvmeLocation::dma_window()` unchanged - |
| 193 | no new static arena, no second window. |
| 194 | - Reuse `configure_smmu_for_nvme` and its single `DmaWindow` - no second |
| 195 | `build_dma_policy` call, no widened policy. |
| 196 | - Reuse the single `dma_gate::dma_grant` result - no second grant, and no BME |
| 197 | write anywhere outside `dma_gate::with_bus_master`/`without_bus_master`. |
| 198 | - Keep BME set only between the post-grant point and revocation, including on |
| 199 | fault. |
| 200 | - Keep all data movement through `DmaMemory`/`NativeDma` (clean on write, |
| 201 | clean+invalidate on read) - no direct MMIO or cache-bypassing copies. |
| 202 | - Keep the `unsafe-debug-nvme-dma-without-smmu` compile_error + banner contract |
| 203 | and the `hardware-staging` incompatibility. |
| 204 | |
| 205 | --- |
| 206 | |
| 207 | ## Evidence index (file:line) |
| 208 | |
| 209 | | Claim | Evidence | |
| 210 | |---|---| |
| 211 | | Single arena | `nvme_read.rs:43,45-48,75-78` | |
| 212 | | Single SMMU window | `handoff.rs:305,317-327`; `smmu.rs:713-717,739-746` | |
| 213 | | Single gate decision | `nvme_read.rs:413-429`; `dma_gate.rs:68-82,279-282` | |
| 214 | | BME off before setup | `nvme_read.rs:201-210`; `handoff.rs:1790` | |
| 215 | | BME set only after grant | `nvme_read.rs:409-412,460` | |
| 216 | | BME revoked | `nvme_read.rs:232-247,473,499,566` | |
| 217 | | Bypass compile guard | `lib.rs:26-33` (and `:12-19`) | |
| 218 | | Bypass banner | `nvme_read.rs:33-36,379-381,443-450` | |
| 219 | | Cache write/read | `nvme_read.rs:307-324,326-339`; `arch/aarch64.rs:270-289,291-301` | |
| 220 | | Write/flush driver | `driver.rs:293-362,428-439`; tests `:917-977` | |
| 221 | | No write lane yet | `nvme_read.rs:9-11`; `nvme/` contains only `driver.rs` | |