D6 - SMMU / DMA review: P3 Native NVMe Write + Flush lane
Repo: aien-dev/aienux, base ef359ec (mirror ~/aienos-nvme-rw).
Scope: read-only review of the DMA/SMMU authority that a future write+flush lane
would inherit from the existing read substrate. No edits, no cargo, no hardware.
Verdict up front: the read lane already establishes a single, confined DMA
authority. Because write and read share the same driver transfer() and the
same NativeDma/DmaMemory implementation, a write lane that reuses the
existing module inherits that authority unchanged. There is **no write lane in
this tree yet** (crates/aienos-kernel/src/nvme/ contains only driver.rs; no
nvme_write.rs), so the lane must be added without introducing a second window,
a second SMMU policy, or a second BME toggle.
1. Write DMA uses the SAME confined authority as read DMA
1.1 One static arena window for both directions
ARENA_BYTES = 128 * 1024and the singlestatic mut DMA_ARENA: DmaArenaare defined once -crates/aienos-boot/src/nvme_read.rs:43,:45-48.NvmeLocation::dma_window()returns exactly that static, page-aligned:nvme_read.rs:75-78(base & !0xfff,size_of::<DmaArena>()).- There is no second arena and no per-direction window.
grepforDMA_ARENA/dma_windowfinds only this one definition plus its users (nvme_read.rs:76,271,279,313,331,457). - The SMMU Stage-1 policy is built from that same call:
configure_smmu_for_nvmetakesnvme.dma_window()(handoff.rs:305) and passes a singleDmaWindow { iova: dma_base, pa: dma_base, length }tobuild_dma_policy-handoff.rs:317-327. build_dma_policyrequires identity (window.iova != window.pa.0→InvalidWindow) and maps only the supplied windows; anything else does not translate -crates/aienos-kernel/src/smmu.rs:739-746.
1.2 Same dma_gate decision for the whole phase
- The gate is evaluated once, before any command, in
run:dma_gate::dma_grant(smmu_ready, smmu_base.is_some(), UNSAFE_NVME_DMA_BYPASS)nvme_read.rs:413-417. Its result gates the entire run (read today, write later); there is no second grant call.
- Decision logic is direction-agnostic: confined iff
smmu_ready, bypass only when no SMMU exists AND the unsafe feature is compiled, never a fallback after a failed SMMU -crates/aienos-kernel/src/dma_gate.rs:68-82, testfailed_smmu_never_falls_back_to_bypass:279-282. nvme_read::runis invoked withnvme_smmu_result.is_ok()assmmu_ready(handoff.rs:2111-2117), i.e. the grant is tied to the sameconfigure_smmu_for_nvmeresult (handoff.rs:1794).
1.3 Same allocator and same DmaMemory path
NativeDmabump-allocates only insideDMA_ARENA, bounds-checked againstARENA_BYTES-nvme_read.rs:269-283,:286-305.- Write and read commands both flow through
NvmeController::transfer(write, …)(crates/aienos-kernel/src/nvme/driver.rs:293-362): writes copy data intoregion.bytesthendma.write(...)(driver.rs:325-330); reads calldma.read(...)(driver.rs:354-357). Both go through the sameD: DmaMemory=NativeDma. write_blocksis a thin wrapper over the sametransfer(true, …)(driver.rs:428-431);flushuses the samesubmit_io(driver.rs:432-439). No separate DMA authority exists for writes.
Confirmed: one window, one SMMU policy, one gate decision, one allocator. No separate or widened window for writes.
2. No unconstrained bus mastering
- Bus Master Enable is cleared on the controller's whole ECAM segment before
any SMMU or driver setup:
take_over_dma→dma_gate::sweep_bus_master(nvme_read.rs:201-210), called athandoff.rs:1790(and the sweep clears every endpoint with BME set -dma_gate.rs:150-218). runrefuses to proceed if BME did not clear:if dma_gate::bus_master_enabled(command) { … return; }(nvme_read.rs:409-412).- BME is set only after
dma_grantreturnsOk:ecam.set_command(&at, dma_gate::with_bus_master(command))atnvme_read.rs:460, which is after thematch dma_gate::dma_grant(...)at:413-429. On denial it returns with BME still off (:419-428). - BME is revoked after the phase:
revoke_dmaclears BME and re-reads to confirm (nvme_read.rs:232-247), called on every post-grant exit: init failure:473, queue failure:499, normal completion:566. - Only one
with_bus_mastercall site exists for NVMe (nvme_read.rs:460); the only other is xHCI (usb_keyboard.rs:341), and the two features are documented as mutually exclusive builds (handoff.rs:1785-1786).
Confirmed: BME set only after dma_grant Ok; revoked after the phase.
3. Unsafe QEMU bypass cannot enter a hardware-staging image
- Feature definition:
unsafe-debug-nvme-dma-without-smmu = ["nvme-read"](crates/aienos-boot/Cargo.toml:42-48);hardware-staging = ["handoff"](:32). - Compile guard: `#[cfg(all(feature = "unsafe-debug-nvme-dma-without-smmu",
feature = "hardware-staging"))] compile_error!(...)
-src/lib.rs:26-33`. The xHCI bypass has the equivalent guard -lib.rs:12-19. - Banner:
UNSAFE_NVME_BANNERis printed on every boot of such an image (nvme_read.rs:33-36,:379-381), and the active-grant warning is printed when the bypass actually grants DMA (nvme_read.rs:443-450). - The bypass cannot be requested silently: the QEMU harness requires
AIENOS_UNSAFE_DMA_BYPASS=1and refuses the feature otherwise (scripts/qemu_nvme_test.sh:49-71); the SMMU harness forces it off (scripts/qemu_smmu_test.sh:3,8). The hardware staging script buildshandoff,hardware-stagingwith no bypass (scripts/stage_one_time_boot.sh:32). - The harness asserts the confined path: denied without SMMU
(
qemu_nvme_test.sh:246-248), confined grant + revoke (:252-261), andcheck_absent "no unsafe DMA bypass in this image"on non-bypass runs (:263-264).
Confirmed: unbuildable with hardware-staging; banner on every boot; loud
warning when active.
4. Cache maintenance on the write path
CPU-writes-cleaned-to-device and device-writes-invalidated-before-CPU-read are
both present, and both are issued with a trailing dsb.
clean_dcache_range=dc cvacper line +dsb-arch/aarch64.rs:270-278, helper ends withdsb()at:291-301.clean_invalidate_dcache_range=dc civacper line +dsb-arch/aarch64.rs:282-289(clean-first so no CPU write in a shared line is lost; documented:279-281).- Write direction:
NativeDma::writecopies into the arena then callsclean_dcache_range-nvme_read.rs:326-339. Intransfer, write payloads reach the device only viadma.write(driver.rs:325-330). - Read direction:
NativeDma::readcallsclean_invalidate_dcache_rangebefore the CPU copy -nvme_read.rs:307-324; used atdriver.rs:354-357. - Queue traffic is on the same discipline: SQ command written via
dma.write(clean) before the doorbell (driver.rs:372-378); CQ read viadma.read(clean+invalidate) before inspecting phase/status (driver.rs:380-383, admin:240-243). - Host tests exercise the write path end-to-end, including PRP-list and MDTS
chunking (
driver.rs:917-945) and write-completion-error propagation (driver.rs:947-977).
Confirmed: correct direction of maintenance for write (clean) and read
(clean+invalidate), each fenced with dsb.
5. Gaps / risks specific to the write+flush lane
- No write lane exists.
nvme_read.rsis explicitly read-only and claims no write/flush (nvme_read.rs:9-11). The lane must add a new module/feature (e.g.nvme-write) and QEMU harness; it must not simply callwrite_blocksfrom the read qualification path without its own evidence gates.driver.rsalready implementswrite_blocks/flush, so the driver side is ready. Arena exhaustion (functional, fail-closed).
NativeDmanever frees (nvme_read.rs:269-305), andtransfer's MDTS chunk cap is 128 KiB (driver.rs:308-316) - exactlyARENA_BYTES(nvme_read.rs:43). Admin queues + identify + I/O queues already consume part of the arena (driver.rs:125-127,196-200), so a maximum-size transfer cannot fit. The write lane must bound per-command chunk size well below the arena (or add arena reuse/free), otherwise writes fail withNvmeError::Dma. Fail-closed, but it caps the lane's usefulness and must be an explicit, tested limit.BME is not revoked on a panic/fault during the phase. Revocation happens only on the enumerated normal/error returns (
nvme_read.rs:473,499,566).fatal_reportresets without clearing BME (handoff.rs:1571+), so a fault mid-write leaves BME set until the cold reset. The write lane should clear BME in the fault path (or explicitly accept reset as the only containment).Flush has no durable evidence yet.
flush()submits opcode 0 withcdw10=1throughsubmit_io(driver.rs:432-439), which only distinguishesOk/DeviceError(driver.rs:399-403). The lane must emit a distinctNVME_FLUSH_*evidence line and must only claim durability after a successful flush completion; it should also record that all prior writes were submitted serially (they are: eachsubmit_iowaits for completion -driver.rs:379-404).Namespace bounds are enforced before any command (
driver.rs:301-307,OutOfRange), so a write cannot escape the namespace. Keep this invariant: do not add a raw command path that bypassestransfer.Cache-maintenance caveat. The maintenance is unconditional. If the arena mapping were ever Device-nGnRnE rather than Normal memory,
dc cvac/civacsemantics would be wrong. Today the arena is Normal identity-mapped (handoff.rs:266-270comment;map_planidentity path). The lane must not change the arena mapping attributes.
Invariants the write+flush lane MUST preserve
- Reuse
NvmeLocation/DMA_ARENA/NvmeLocation::dma_window()unchanged - no new static arena, no second window. - Reuse
configure_smmu_for_nvmeand its singleDmaWindow- no secondbuild_dma_policycall, no widened policy. - Reuse the single
dma_gate::dma_grantresult - no second grant, and no BME write anywhere outsidedma_gate::with_bus_master/without_bus_master. - Keep BME set only between the post-grant point and revocation, including on fault.
- Keep all data movement through
DmaMemory/NativeDma(clean on write, clean+invalidate on read) - no direct MMIO or cache-bypassing copies. - Keep the
unsafe-debug-nvme-dma-without-smmucompile_error + banner contract and thehardware-stagingincompatibility.
Evidence index (file:line)
| Claim | Evidence |
|---|---|
| Single arena | nvme_read.rs:43,45-48,75-78 |
| Single SMMU window | handoff.rs:305,317-327; smmu.rs:713-717,739-746 |
| Single gate decision | nvme_read.rs:413-429; dma_gate.rs:68-82,279-282 |
| BME off before setup | nvme_read.rs:201-210; handoff.rs:1790 |
| BME set only after grant | nvme_read.rs:409-412,460 |
| BME revoked | nvme_read.rs:232-247,473,499,566 |
| Bypass compile guard | lib.rs:26-33 (and :12-19) |
| Bypass banner | nvme_read.rs:33-36,379-381,443-450 |
| Cache write/read | nvme_read.rs:307-324,326-339; arch/aarch64.rs:270-289,291-301 |
| Write/flush driver | driver.rs:293-362,428-439; tests :917-977 |
| No write lane yet | nvme_read.rs:9-11; nvme/ contains only driver.rs |