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 / evidence / fix_m3_smmuv3_ste_cd_2026-09-24.md
| 1 | # Fix chain: SMMUv3 xHCI DMA confinement (branch fix/m3-smmuv3-ste-cd-layout) |
| 2 | |
| 3 | Base: 675d8c4 (forensic snapshot, branch wip/m3-smmu-c-bad-ste-635111c). |
| 4 | This note is appended to per commit; the original snapshot evidence is left |
| 5 | untouched in wip_m3_smmuv3_c_bad_ste_2026-09-24.md. |
| 6 | |
| 7 | ## Commit 1: fix(smmu): runtime-align linear stream table for QEMU SMMUv3 |
| 8 | |
| 9 | ### Build blocker (root cause) |
| 10 | |
| 11 | The snapshot tree did not build for aarch64-unknown-uefi. `cargo check` passes, |
| 12 | but the real `cargo build` traps rustc (SIGTRAP) in the COFF object writer. |
| 13 | A/B on the static alignment attribute: |
| 14 | |
| 15 | align(8192) BUILD OK |
| 16 | align(16384) SIGTRAP |
| 17 | align(262144) SIGTRAP |
| 18 | |
| 19 | COFF/PE cannot encode section alignment above 8192, so `repr(C, align(262144))` |
| 20 | on SMMU_TABLES was unbuildable. This is why the alignment fix had never been |
| 21 | re-run; the reported C_BAD_STE run came from the older align(4096) image. |
| 22 | |
| 23 | ### Change |
| 24 | |
| 25 | - Removed `repr(C, align(262144))` from SMMU_TABLES. |
| 26 | - Allocate the tables at runtime: `Layout::from_size_align(size_of::<SmmuTables>(), |
| 27 | 262144)`, zeroed, cached in a shared `AtomicUsize`; one accessor |
| 28 | `smmu_tables()` feeds both the configure path and the fault-dump path. |
| 29 | - Kept 4096 stream table entries. |
| 30 | - Added explicit geometry asserts: table size == 262144, LOG2SIZE == 12, |
| 31 | SID < 4096, and the aligned base % 262144 == 0. |
| 32 | |
| 33 | ### Verification |
| 34 | |
| 35 | cargo fmt --check OK |
| 36 | cargo build --release -p aienos-boot --target aarch64-unknown-uefi \ |
| 37 | --features usb-keyboard OK |
| 38 | |
| 39 | ### QEMU result (AIENOS_QEMU_SMMU=1) |
| 40 | |
| 41 | smmu_ste: base=0xbbf40000 cfg=0xc sid=0x10 addr=0xbbf40400 words=[0xbbf8030b,0x0] |
| 42 | |
| 43 | - base 0xbbf40000 is 256 KiB aligned; cfg 0xc is LOG2SIZE=12, FMT=0 (linear). |
| 44 | - STE address == base + 0x10 * 64. STE is valid (V=1, Config=5, S1Fmt=0, |
| 45 | CTXPTR=0xbbf80300). |
| 46 | - C_BAD_STE (0x04) is eliminated. The next event was C_BAD_CD (0x0a): the STE |
| 47 | is accepted, the CD is fetched, and QEMU rejected the CD. |
| 48 | |
| 49 | ## Commit 2: fix(smmu): correct AArch64 context descriptor layout |
| 50 | |
| 51 | ### Root cause |
| 52 | |
| 53 | ContextDescriptor::stage1 placed the CD control fields one 64-bit word too |
| 54 | high. QEMU and Linux define them inside CD word 0: |
| 55 | |
| 56 | CD_0: T0SZ[5:0] TG0[7:6] IRGN0[9:8] ORGN0[11:10] SH0[13:12] EPD0[14] |
| 57 | ENDI[15] T1SZ[21:16] TG1[23:22] EPD1[30] V[31] IPS[34:32] |
| 58 | AA64[41] R[45] A[46] ASID[63:48] |
| 59 | CD_1: TTBR0 |
| 60 | CD_2: TTBR1 |
| 61 | CD_3: MAIR |
| 62 | |
| 63 | The old code put IPS/AA64/R/A/ASID in word 1 and TTBR0 in word 2, so QEMU read |
| 64 | CD_AARCH64=0 and CD_A=0 and rejected the descriptor (C_BAD_CD). It also set |
| 65 | word 0 bit 14 = EPD0, disabling the TTBR0 walk, and put MAIR in word 4. |
| 66 | |
| 67 | ### Change |
| 68 | |
| 69 | - CD word 0 now holds T0SZ/TG0/IRGN0/ORGN0/SH0, EPD1, V, IPS, AA64, R, A and |
| 70 | ASID; EPD0 is clear so the TTBR0 stage-1 walk runs. |
| 71 | - CD word 1 is TTBR0, word 2 is TTBR1 (unused, EPD1 set), word 3 is MAIR. |
| 72 | - descriptor_layouts and policy_maps_only_allowed_windows updated to the new |
| 73 | layout (TTBR0 now cd.0[1], MAIR now cd.0[3]). |
| 74 | |
| 75 | Note: MAIR is CD word 3 per the SMMUv3 spec and Linux |
| 76 | (arm_smmu_write_ctx_desc writes cdptr[3] = cd->mair), not word 2. |
| 77 | |
| 78 | ### Verification |
| 79 | |
| 80 | cargo fmt --check OK |
| 81 | cargo test -p aienos-kernel smmu 5 passed |
| 82 | cargo build --release -p aienos-boot --target aarch64-unknown-uefi \ |
| 83 | --features usb-keyboard OK |
| 84 | AIENOS_QEMU_TIMEOUT=90 bash scripts/qemu_smmu_test.sh QEMU_KEYBOARD: PASS |
| 85 | |
| 86 | ### QEMU smmuv3 trace (config decoded once, then cached) |
| 87 | |
| 88 | smmuv3_get_ste STE addr: 0xbbf40400 |
| 89 | smmuv3_get_cd CD addr: 0xbbf80300 |
| 90 | smmuv3_decode_cd oas=44 |
| 91 | smmuv3_decode_cd_tt TT[0]:tsz:16 ttb:0xbc32f000 granule_sz:12 had:0 |
| 92 | smmuv3_translate_success 583 |
| 93 | smmuv3_config_cache_hit 582 |
| 94 | smmuv3_translate_disable 192 (pre-enable bypass) |
| 95 | smmuv3_get_ste / get_cd / decode_cd = 1 each (accepted, then cached) |
| 96 | no smmuv3_record_event, no fault events |
| 97 | |
| 98 | ### AIENOS serial proof |
| 99 | |
| 100 | smmu: enabled base=0x9050000 stream_id=0x10 |
| 101 | smmu_dma_window: xhci only, translation active |
| 102 | keyboard: ready (port 5, slot 1, endpoint 0x81) |
| 103 | keyboard_echo: abc / help / el / mem / exit |
| 104 | keyboard: done (exit) |
| 105 | |
| 106 | ### Acceptance progression |
| 107 | |
| 108 | C_BAD_STE gone PROVEN |
| 109 | C_BAD_CD gone PROVEN |
| 110 | CD accepted PROVEN |
| 111 | stage-1 PT walk reached PROVEN (TT[0] tsz16 granule12, 583 translate_success) |
| 112 | DMA translation succeeds PROVEN |
| 113 | xHCI Enable Slot completes PROVEN (keyboard: ready) |
| 114 | keyboard attaches PROVEN |
| 115 | typed-input proof PROVEN (keyboard_echo: abc ... exit) |
| 116 | |
| 117 | SMMUv3 DMA confinement for the xHCI controller is now demonstrated in QEMU. |
| 118 | Emulator only; this says nothing about Machine 1 hardware. |