aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
GitHub aien-dev/aienux#197 (code only)
Apply it
git checkout -b patch-86d0a31feb09a4bc 4d9bdb2ea98ff4fe08f735857cdb8a7b77e7484e curl -fsSL https://interplane.io/drakestapleton/aienux/patches/86d0a31feb09a4bc.patch | git am
Provenance
github-import 1
aien-dev/aienux
197
e88963de0aee4f7362b965b983234c31a56460c8
{"kind":"github-import","owner":"aien-dev","repo":"aienux","number":197,"url":"https://github.com/aien-dev/aienux/pull/197","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:35:56Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/docs/TRUST-1-M5-GATE-MATRIX.md b/docs/TRUST-1-M5-GATE-MATRIX.md index 609c9ea7..ab95b21a 100644 --- a/docs/TRUST-1-M5-GATE-MATRIX.md +++ b/docs/TRUST-1-M5-GATE-MATRIX.md @@ -53,7 +53,7 @@ The C sealed Store (`native/store/store_sealed.c`, lane 11) wires `native/m5` in | Production geometry rule | the non-qualification path requires the 4096-byte atomic-root predicate and fails closed otherwise (`crates/aienos-boot/src/nvme_read.rs`, Store-over-NVMe section); the 512-byte path is a TEST-ONLY qualification mode | n/a | | Machine 1 SSD | 512-byte logical block, one-block power-fail atomic unit: `NATIVE_GB10_STORE_ROOT_ATOMICITY: FAIL` for 4K (evidence/p3_nvme_atomicity_2026-09-25.md) | n/a | | Protocol for 512-byte tearing | proven twice: Rust engine root-tear closure (#136 Tier 2a: every non-zero superblock byte in sector 0) and the C host reference `native/store/torn_slot.c` (#175, every block subset enumerated); neither is the production path | n/a | -| Production Store on 512-byte geometry | C disk layer now exists: `native/disk` (block layer + freestanding C NVMe driver, #190, QEMU virtual NVMe only) and `native/store` (byte-identical C twin of the ADR 0015 engine, 89/89 golden-vector rows, plus the sealed Store with the torn_slot anchor); host receipt `evidence/trust1_m5_qualification_6fd36c3012ad792862e6098492ae78d7b091c5fada8af73c7c60e6a8f544a650.json`. QEMU kernel-path evidence: the sealed C Store runs in the C kernel boot path on a 512 B QEMU namespace (`evidence/trust1_m5_qualification_3358bc2585f0982e1dfbcd0f5ed2fe077847332591782edff8974f0da9caeded.json`, row `ck_store_kernel_qemu`; QEMU-only unsafe DMA bypass, no crash campaign). Missing: the binding on a real device (`m5_store_kernel_binding`) and any run on the Machine 1 SSD | MISSING_IMPLEMENTATION | +| Production Store on 512-byte geometry | C disk layer now exists: `native/disk` (block layer + freestanding C NVMe driver, #190, QEMU virtual NVMe only) and `native/store` (byte-identical C twin of the ADR 0015 engine, 89/89 golden-vector rows, plus the sealed Store with the torn_slot anchor); host receipt `evidence/trust1_m5_qualification_6fd36c3012ad792862e6098492ae78d7b091c5fada8af73c7c60e6a8f544a650.json`. QEMU kernel-path evidence: the sealed C Store runs in the C kernel boot path on a 512 B QEMU namespace (`evidence/trust1_m5_qualification_3358bc2585f0982e1dfbcd0f5ed2fe077847332591782edff8974f0da9caeded.json`, row `ck_store_kernel_qemu`; QEMU-only unsafe DMA bypass, no crash campaign). The same C kernel Store path now runs SMMU-confined in QEMU (`iommu=smmuv3`, NVMe stream mapped to its DMA window only, out-of-window DMA faulted; `evidence/ck_gates_23482745517ec1967d2667b24d2a1cd9eafb27cdd91bdb91be7712a3a2968812.json`, gates `SMMU`, `M4_NVME`, `M4_STORE`; QEMU only, physical NOT_RUN). Missing: the binding on a real device (`m5_store_kernel_binding`) and any run on the Machine 1 SSD | MISSING_IMPLEMENTATION | ## Update 2026-10-01 (lane 1) diff --git a/evidence/ck_gates_23482745517ec1967d2667b24d2a1cd9eafb27cdd91bdb91be7712a3a2968812.json b/evidence/ck_gates_23482745517ec1967d2667b24d2a1cd9eafb27cdd91bdb91be7712a3a2968812.json new file mode 100644 index 00000000..098311ff --- /dev/null +++ b/evidence/ck_gates_23482745517ec1967d2667b24d2a1cd9eafb27cdd91bdb91be7712a3a2968812.json @@ -0,0 +1,38 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-01T08:47:43Z", + "finished_utc": "2026-10-01T08:49:18Z", + "commit": "63b295efce58213a6ff9e575c4d5d6d66df270dc", + "commit_subject": "native/kernel: review fixes (CFGI_CD_ALL on STE changes, IORT node array bound 48, unconfine independent of bus master)", + "tree_clean_before": true, + "tree_clean_after": true, + "quiet_flag": "/tmp/lane25.quiet", + "quiet_flag_present_at_start": false, + "host": {"hostname": "spark-b87b", "arch": "aarch64", "kernel": "7.0.0-1019-nvidia", "cpu_model": "Cortex-A725;Cortex-X925"}, + "qemu": {"binary": "/usr/bin/qemu-system-aarch64", "version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)"}, + "firmware": {"aavmf_code": "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd", "aavmf_code_sha256": "4a4cb7f6d8106bb2a7dd8c763fab14b1810152136fc4304e5b728f0043e84f12", "aavmf_vars": "/usr/share/AAVMF/AAVMF_VARS.fd", "aavmf_vars_sha256": "b3b855c5a80310168051164986855692d1bdb06e67619856177965cd87c6774f"}, + "nvme_dma_mode": "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately", + "children": [ + {"name": "boot", "script": "scripts/qemu_ck_boot_test.sh", "present": true, "script_sha256": "73a9522aa8dbaf18dcc83be36f136e8251b0ed927e4d0d4de7b84ec902b16c8d", "exit": "0", "seconds": 7, "log_sha256": "224b34e39aa6f6cd623822f0c345a92691e476fd4861c0c74565f92c380c90b8", "log_lines": 21}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "fa6188fe658556ddb8873c6b0d11ba132735b51c834e8cf83ec2fdb2a4ca3f30", "exit": "0", "seconds": 87, "log_sha256": "140400acc975d5e6ac1dc89b91f106c7f310d2e0c80f5741145eda67a9242c1b", "log_lines": 528} + ], + "gates": [ + {"id": "M1", "marker": "AIENOS_CK_M1", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M3", "marker": "AIENOS_CK_M3", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no threads, EL0 tasks, preemption, MADT placement or typed IPC in the C kernel"}, + {"id": "SMMU", "marker": "AIENOS_CK_SMMU", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "P2_ARTIFACT", "marker": "AIENOS_CK_P2_ARTIFACT", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no signed artifact loader (EL0, W^X, admission receipts) in the C kernel"}, + {"id": "M0_ROLLBACK", "marker": "AIENOS_CK_M0_ROLLBACK", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md)"}, + {"id": "M4_NVME", "marker": "AIENOS_CK_M4_NVME", "source": "store", "verdict": "PASS", "reason": "SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"}, + {"id": "M4_STORE", "marker": "AIENOS_CK_M4_STORE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_STORE_CRASH", "marker": "AIENOS_CK_M4_STORE_CRASH", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no C Store crash/kill campaign (4 KiB or 512 B) and no slot reuse (C Store is append-only)"}, + {"id": "M4_CONTINUITY", "marker": "AIENOS_CK_M4_CONTINUITY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no continuity core (ADR 0016 agent identity and memory) in the C kernel"}, + {"id": "M4_RECOVERY", "marker": "AIENOS_CK_M4_RECOVERY", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no Recovery Core (ADR 0006) in the C kernel"}, + {"id": "ARGUS1_REVOKE", "marker": "AIENOS_CK_ARGUS1_REVOKE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "KEYBOARD", "marker": "AIENOS_CK_KEYBOARD", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel"} + ], + "counts": {"total": 12, "pass": 5, "fail": 0, "not_run": 7}, + "verdict": "NOT_ALL_GATES_PASS", + "parity": "native/kernel/GATES.md", + "physical": "NOT_RUN", + "physical_statement": "QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." +} diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index e6e07ad4..f83af2b2 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -19,13 +19,15 @@ Status vocabulary: C gates (one line each from `ck_gates.sh`): `M1` (from `scripts/qemu_ck_boot_test.sh`), `M4_NVME`, `M4_STORE`, `ARGUS1_REVOKE` -(from `scripts/qemu_ck_store_test.sh`; rows 47-72, 76 and 96-98 were checked -against its exact grep patterns), and the NOT_RUN gates -`M3`, `SMMU`, `P2_ARTIFACT`, `M0_ROLLBACK`, `M4_STORE_CRASH`, +`SMMU` (from `scripts/qemu_ck_store_test.sh`; rows 21-24b, 47-72, 76 and +96-98 were checked against its exact grep patterns), and the NOT_RUN gates +`M3`, `P2_ARTIFACT`, `M0_ROLLBACK`, `M4_STORE_CRASH`, `M4_CONTINUITY`, `M4_RECOVERY`, `KEYBOARD`. -`M4_NVME` can PASS only through the QEMU-only unsafe DMA bypass build -(`make full CK_QEMU_UNSAFE_DMA=1`); the SMMU-confined NVMe mode is NOT_RUN. +`M4_NVME` and `SMMU` PASS in the SMMU-confined mode (default `make full` +image, QEMU `iommu=smmuv3`). The unconfined bypass build +(`make full CK_QEMU_UNSAFE_DMA=1`) is TEST-ONLY: one boot per geometry checks +that it announces itself; it never counts toward a PASS. QEMU only. ## M1 boot: scripts/qemu_boot_test.sh -> CK `M1` (scripts/qemu_ck_boot_test.sh) @@ -66,12 +68,25 @@ toward `AIENOS_CK_M1`. ## SMMU: scripts/qemu_smmu_test.sh (= keyboard test, SMMU on, bypass forced off) -> CK `SMMU` +The Rust gate proves the SMMU on the xHCI stream (keyboard). The C kernel has +no xHCI driver, so the C `SMMU` gate proves the same properties on the NVMe +stream instead (boots 1-4 of `scripts/qemu_ck_store_test.sh`, default +`make full` image, QEMU `iommu=smmuv3`, both geometries). Every row below +therefore DIFFERS in the device named; the property checked is the same. +The C SMMU service is `core/smmu.c` (port of `crates/aienos-kernel/src/smmu.rs`: +linear stream table, stage 1 only, global abort set first and never cleared, +every stream in abort until installed) plus `core/smmu_svc.c` (IORT lookup, +per-stream page tables, `ck_dma_confine` / `ck_dma_unconfine` / +`ck_dma_faults` in ck.h); host tests in `tests/test_smmu.c`. + | # | Rust check (pattern) | CK gate | Status | | --- | --- | --- | --- | -| 21 | IORT stream configured (`smmu: enabled`) | SMMU | NOT_RUN (MISSING_IMPLEMENTATION: no IORT/SMMUv3 service in ck.h) | -| 22 | DMA window translated (`smmu_dma_window: xhci only, translation active`) | SMMU | NOT_RUN (MISSING_IMPLEMENTATION: no SMMU) | -| 23 | DMA granted only as confined (`dma_gate: xhci granted (Confined), bus master on`) | SMMU | NOT_RUN (MISSING_IMPLEMENTATION: no SMMU; C NVMe DMA exists only via the QEMU-only unsafe bypass) | -| 24 | no unsafe bypass in the image (`UNSAFE DMA BYPASS` absent) | SMMU | NOT_RUN (MISSING_IMPLEMENTATION: no SMMU gate; the default `make full` image does pass the same absence check, `UNSAFE NVME DMA BYPASS` absent, in boot 5 of qemu_ck_store_test.sh, see row 56) | +| 21 | IORT stream configured (`smmu: enabled`) | SMMU | DIFFERS (same prefix; C: `smmu: enabled base=.. stream_id=..` for the NVMe stream) | +| 22 | DMA window translated (`smmu_dma_window: xhci only, translation active`) | SMMU | DIFFERS (C: `smmu_dma_window: nvme only, translation active iova=.. len=.. rid=..`) | +| 23 | DMA granted only as confined (`dma_gate: xhci granted (Confined), bus master on`) | SMMU | DIFFERS (C: `dma_gate: nvme granted (Confined), bus master on`; absent `dma_gate: nvme granted (UnsafeBypass)`) | +| 24 | no unsafe bypass in the image (`UNSAFE DMA BYPASS` absent) | SMMU | DIFFERS (C: `UNSAFE NVME DMA BYPASS` absent in boots 1-5) | +| 24a | (C only) DMA outside the window is faulted | SMMU | C only: the controller is pointed at a page outside its window; `smmu_negative: refused dma outside window ... faults>=1 type=0x10 (F_TRANSLATION) sid=<nvme> addr=<that page> page=intact ... recovery_read=ok` | +| 24b | (C only) stream returned to abort after use | SMMU | C only: `smmu: nvme stream 0x.. returned to abort (rc=0)` after `dma_gate: nvme bus master revoked` | ## SEED-0A keyboard: scripts/qemu_keyboard_test.sh (SMMU=1 and SMMU=0) -> CK `KEYBOARD` @@ -112,15 +127,17 @@ toward `AIENOS_CK_M1`. ## M4 NVMe read: scripts/qemu_nvme_test.sh (SMMU=1 and SMMU=0) -> CK `M4_NVME` -The C NVMe path (native/disk via the stage `devices`) does I/O only in the -QEMU-only unsafe DMA bypass image (`make full CK_QEMU_UNSAFE_DMA=1`). The -default `make full` image denies NVMe DMA (no SMMU) and is checked as the -fail-closed case (boot 5). The Rust gate never runs a bypass build. The C +The C NVMe path (native/disk via the stage `devices`) does I/O SMMU-confined: +the default `make full` image with QEMU `iommu=smmuv3` (boots 1-4). The same +image without an SMMU denies NVMe DMA and is checked as the fail-closed case +(boot 5). The TEST-ONLY bypass image (`make full CK_QEMU_UNSAFE_DMA=1`) is +booted once more (boot 6) only to check its warnings; the Rust gate never +runs a bypass build. The C observables are free-form lines, not the Rust `NVME_*_QEMU: PASS` markers. qemu_ck_store_test.sh runs every check on a 512 B and a 4096 B namespace. -**M4_NVME note:** a PASS of `M4_NVME` means the QEMU-only unsafe DMA bypass -build passed. The SMMU-confined NVMe mode is NOT_RUN (row 53). ck_gates.sh +**M4_NVME note:** a PASS of `M4_NVME` means the SMMU-confined NVMe mode +passed in QEMU (row 53); an SMMU check failure fails `M4_NVME` too. ck_gates.sh prints this on the PASS line and records `nvme_dma_mode` in the receipt. | # | Rust check (pattern) | CK gate | Status | @@ -131,11 +148,11 @@ prints this on the PASS line and records `nvme_dma_mode` in the receipt. | 50 | sentinel LBA read with exact SHA-256 (`NVME_READ_QEMU: PASS (... sha256=..)`) | M4_NVME | DIFFERS (C: `nvme: read lba=0 blocks=1 ok`; no host-planted sentinel hash, weaker) | | 51 | read past namespace end rejected (`NVME_BOUNDS_QEMU: PASS`) | M4_NVME | DIFFERS (C: `nvme: bounds read lba=N -> refused` with N = block_count) | | 52 | device-reported command error surfaced (`NVME_ERROR_QEMU: PASS`) | M4_NVME | NOT_RUN (MISSING_IMPLEMENTATION: no C error-injection probe) | -| 53 | SMMU mode: `smmu: enabled`, `smmu_dma_window: nvme only`, `dma_gate: nvme granted (Confined)` | M4_NVME | NOT_RUN (MISSING_IMPLEMENTATION: no SMMU service in the C core; **the SMMU-confined NVMe mode is NOT_RUN**, M4_NVME passes only through the QEMU-only unsafe bypass build) | +| 53 | SMMU mode: `smmu: enabled`, `smmu_dma_window: nvme only`, `dma_gate: nvme granted (Confined)` | M4_NVME | IDENTICAL prefixes (C appends base/stream/window values), boots 1-4, both geometries; plus the C-only out-of-window fault check (row 24a) | | 54 | fail-closed mode: `dma_gate: nvme denied (NoSmmu)...`, `nvme: unavailable (SMMU DMA isolation not active)`, no grant/identify/read | M4_NVME | DIFFERS (boot 5, default `make full` image: the two lines and absent `dma_gate: nvme granted` are IDENTICAL; "no identify" is absent `nvme: identify`; no explicit no-read check, but Store is refused `proof=io step="no boot disk"` and the image sha256 is unchanged) | -| 55 | bus master revoked after the phase (`dma_gate: nvme bus master revoked`) | M4_NVME | IDENTICAL (same line, bypass image, both geometries) | -| 56 | no `UNSAFE NVME DMA BYPASS` in the image | M4_NVME | IDENTICAL for the safe image (boot 5 checks `UNSAFE NVME DMA BYPASS` absent). The bypass image instead must print `WARNING: UNSAFE NVME DMA BYPASS BUILD`, `... ACTIVE` and `dma_gate: nvme granted (UnsafeBypass)` (same text as the Rust bypass build, which verify_all never runs); ck_gates.sh records nvme_dma_mode in the receipt | -| 57 | no panic or fault | M4_NVME | IDENTICAL (`report_kind: (panic\|fault)` absent, via scripts/lib_ck_m1_checks.sh on every one of the 5 boots per geometry) | +| 55 | bus master revoked after the phase (`dma_gate: nvme bus master revoked`) | M4_NVME | IDENTICAL (same line, confined boots 1-4 and bypass boot 6, both geometries) | +| 56 | no `UNSAFE NVME DMA BYPASS` in the image | M4_NVME | IDENTICAL for the default image (boots 1-5 check `UNSAFE NVME DMA BYPASS` absent). The TEST-ONLY bypass image (boot 6) instead must print `WARNING: UNSAFE NVME DMA BYPASS BUILD`, `... ACTIVE` and `dma_gate: nvme granted (UnsafeBypass)` (same text as the Rust bypass build, which verify_all never runs); ck_gates.sh records nvme_dma_mode in the receipt | +| 57 | no panic or fault | M4_NVME | IDENTICAL (`report_kind: (panic\|fault)` absent, via scripts/lib_ck_m1_checks.sh on every one of the 6 boots per geometry) | ## M4 NVMe write/flush: scripts/qemu_nvme_rw_test.sh -> CK `M4_NVME` / `M4_STORE` @@ -146,7 +163,7 @@ prints this on the PASS line and records `nvme_dma_mode` in the receipt. | 60 | write past namespace end rejected (`NVME_WRITE_BOUNDS_QEMU: PASS`) | M4_NVME | NOT_RUN (MISSING_IMPLEMENTATION: no C write-bounds probe line; host tests cover it) | | 61 | write + flush + read-back exact (`NVME_WRITE_QEMU`, `NVME_FLUSH_QEMU`, `NVME_DURABILITY_QEMU` with sha256) | M4_NVME | DIFFERS (C: `nvme: rw probe lba=N bytes=4096 write+flush+readback match` on the scratch unit, restored afterwards; no sha256 printed) | | 62 | invalid-namespace write error surfaced (`NVME_WRITE_ERROR_QEMU: PASS`) | M4_NVME | NOT_RUN (MISSING_IMPLEMENTATION: no C error probe) | -| 63 | SMMU confined grant + revoke | M4_NVME | NOT_RUN (MISSING_IMPLEMENTATION: no SMMU) | +| 63 | SMMU confined grant + revoke | M4_NVME | DIFFERS (confined grant row 53, `dma_gate: nvme bus master revoked`, then `smmu: nvme stream .. returned to abort`; the rw probe runs inside the confined window) | | 64 | host read-back of the image after power off equals written bytes | M4_STORE | DIFFERS (C: durability shown by Store boot_count read back across 3 boots, not a host dd of one LBA) | | 65 | boot 2 observes the persisted pattern (`NVME_DURABILITY_QEMU: PASS (persisted lba=..)`) | M4_STORE | DIFFERS (C: `store: opened generation= boot_count=` then `committed boot_count=N+1` on the next boot) | | 66 | fail-closed: no write/flush/durability markers without DMA | M4_NVME | DIFFERS (safe image, boot 5: absent `nvme: identify`, Store refused `proof=io`, image sha256 unchanged; no explicit absent rw-probe check) | diff --git a/native/kernel/Makefile b/native/kernel/Makefile index e0812690..d1497e19 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -5,9 +5,9 @@ # "not linked"; the M1 gate boots this one) # make full full image with the boot stages (devices, security, # Store) from stage.mk: $(OUT)/full/BOOTAA64.EFI, -# NVMe DMA refused (no SMMU service in ck.h) +# NVMe DMA SMMU-confined, refused without an SMMU # make full CK_QEMU_UNSAFE_DMA=1 -# QEMU-only image with the unconfined NVMe DMA bypass: +# TEST-ONLY QEMU image with the unconfined NVMe DMA bypass: # $(OUT)/full-qemu-unsafe-dma/BOOTAA64.EFI (only # scripts/qemu_ck_store_test.sh sets this; refused # together with CK_HARDWARE_STAGING=1) @@ -48,7 +48,8 @@ AIENOS_COMMIT ?= $(shell git -C $(REPO) rev-parse HEAD 2>/dev/null || echo unkno CK_EXTRA_CFLAGS ?= # Build-time DMA policy for the full image (see dev/nvme_bind.c): -# default refuse; 1 = QEMU-only unconfined bypass. Never with hardware staging. +# default SMMU-confined or refuse; 1 = TEST-ONLY QEMU unconfined bypass when +# no SMMU exists. Never with hardware staging. CK_QEMU_UNSAFE_DMA ?= CK_HARDWARE_STAGING ?= ifneq ($(strip $(CK_QEMU_UNSAFE_DMA)),) @@ -74,6 +75,7 @@ CORE_C := $(BOOT)/efi_main.c \ $(KDIR)/arch/exception.c $(KDIR)/arch/gic.c $(KDIR)/arch/timer.c \ $(KDIR)/core/acpi.c $(KDIR)/core/console.c $(KDIR)/core/fmt.c \ $(KDIR)/core/kmain.c $(KDIR)/core/libc_min.c $(KDIR)/core/report.c \ + $(KDIR)/core/smmu.c $(KDIR)/core/smmu_svc.c \ $(KDIR)/mm/frames.c $(KDIR)/mm/heap.c $(KDIR)/mm/mmu.c $(KDIR)/mm/pt.c CORE_S := $(BOOT)/efi_entry.S $(KDIR)/arch/vectors.S HDRS := $(wildcard $(KDIR)/include/*.h $(KDIR)/*/*.h $(BOOT)/*.h) @@ -172,9 +174,9 @@ $(FULL_EFI): $(FULL_ELF) $(call pe_image,$<,$@) # ---- host unit tests ---- -HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/mm/frames.c \ +HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/mm/frames.c \ $(KDIR)/mm/pt.c $(KDIR)/mm/heap.c -TESTS := test_fmt test_frames test_pt test_acpi test_heap +TESTS := test_fmt test_frames test_pt test_acpi test_heap test_smmu HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer diff --git a/native/kernel/README.md b/native/kernel/README.md index d4b5aea4..48c24920 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -83,9 +83,13 @@ low-water free bytes. `STAGE_CFLAGS` first, then the kernel flags and `-U_FORTIFY_SOURCE`. The core defines no `ck_stage_*` defaults; the core-only image is checked to contain none and the full image to contain all three. -- NVMe DMA is refused by default (no SMMU service). `CK_QEMU_UNSAFE_DMA=1` - (QEMU only, refused with `CK_HARDWARE_STAGING`) builds the unconfined bypass - and prints the `WARNING: UNSAFE NVME DMA BYPASS` lines. +- NVMe DMA is granted only SMMU-confined: the core SMMUv3 service + (`core/smmu.c`, `core/smmu_svc.c`; ck.h `ck_dma_confine` / + `ck_dma_unconfine` / `ck_dma_faults`) finds the SMMU through the ACPI IORT + and maps only the NVMe DMA region for the NVMe stream. With no SMMU, NVMe + DMA is refused. `CK_QEMU_UNSAFE_DMA=1` (TEST-ONLY, QEMU only, refused with + `CK_HARDWARE_STAGING`) builds the unconfined bypass used only when no SMMU + exists, and prints the `WARNING: UNSAFE NVME DMA BYPASS` lines. ## Differences from the Rust kernel diff --git a/native/kernel/core/acpi.c b/native/kernel/core/acpi.c index 44f3ee99..71b106d9 100644 --- a/native/kernel/core/acpi.c +++ b/native/kernel/core/acpi.c @@ -203,3 +203,90 @@ int ck_acpi_spans(uint64_t rsdp, void (*fn)(uint64_t lo, uint64_t hi, void *ctx) } return n; } + +/* IORT walk: calls back for every node after checking its header, length + * and ID-mapping array bounds. -1 on any malformed node. */ +#define IORT_NODE_HDR 16u +#define IORT_NODE_ROOT_COMPLEX 2u +#define IORT_NODE_SMMUV3 4u +#define IORT_MAP_BYTES 20u +#define IORT_TABLE_HDR 48u /* SDT header + node count, node array offset, reserved */ + +static int iort_nodes(const uint8_t *t, uint32_t len, uint32_t *count, uint32_t *first) +{ + if (!sig_eq(t, "IORT", 4) || len < IORT_TABLE_HDR) + return -1; + *count = rd32(t + 36); + *first = rd32(t + 40); + return 0; +} + +int ck_iort_parse(const void *iort, struct ck_iort_smmu *out) +{ + const uint8_t *t = iort; + struct ck_iort_smmu s = { 0 }; + uint32_t len = rd32(t + 4), count, at; + if (iort_nodes(t, len, &count, &at)) + return -1; + int found = 0; + for (int pass = 0; pass < 2; pass++) { + uint32_t off = at; + for (uint32_t i = 0; i < count; i++) { + if (off < IORT_TABLE_HDR || off > len || len - off < IORT_NODE_HDR) + return -1; + const uint8_t *n = t + off; + uint8_t type = n[0]; + uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; + if (nlen < IORT_NODE_HDR || nlen > len - off) + return -1; + uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); + if (mcount) { + if (mcount > nlen / IORT_MAP_BYTES || moff < IORT_NODE_HDR || moff > nlen || + (uint64_t)mcount * IORT_MAP_BYTES > (uint64_t)(nlen - moff)) + return -1; + } + if (pass == 0 && type == IORT_NODE_SMMUV3 && !found) { + if (nlen < 24) + return -1; + s.base = rd64(n + 16); + if (!s.base) + return -1; + s.node_off = off; + found = 1; + } + if (pass == 1 && type == IORT_NODE_ROOT_COMPLEX) { + for (uint32_t m = 0; m < mcount; m++) { + const uint8_t *e = n + moff + m * IORT_MAP_BYTES; + if (rd32(e + 12) != s.node_off) + continue; + if (s.nmaps >= CK_IORT_MAX_MAPS) + return -1; + s.map[s.nmaps].input_base = rd32(e); + s.map[s.nmaps].id_count = rd32(e + 4); + s.map[s.nmaps].output_base = rd32(e + 8); + s.nmaps++; + } + } + off += nlen; + } + if (pass == 0 && !found) + return 0; + } + *out = s; + return 1; +} + +int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t rid, uint32_t *sid) +{ + for (uint32_t i = 0; i < s->nmaps; i++) { + const struct ck_iort_map *m = &s->map[i]; + if (rid < m->input_base) + continue; + uint32_t rel = rid - m->input_base; + if (rel > m->id_count || m->output_base + rel < m->output_base) + continue; + *sid = m->output_base + rel; + return 0; + } + return -1; +} diff --git a/native/kernel/core/acpi.h b/native/kernel/core/acpi.h index 314a13db..980e1c0e 100644 --- a/native/kernel/core/acpi.h +++ b/native/kernel/core/acpi.h @@ -58,4 +58,25 @@ int ck_spcr_parse(const void *spcr, struct ck_spcr *out); * -1 if the FADT is too short to carry the field. */ int ck_fadt_arm_boot_arch(const void *fadt, uint16_t *flags); +/* IORT (Arm IO Remapping Table): the first SMMUv3 node and the PCI root + * complex ID mappings that point at it (ported from the Rust + * aienos-kernel acpi::iort_smmuv3). */ +#define CK_IORT_MAX_MAPS 8 +struct ck_iort_map { + uint32_t input_base, id_count, output_base; /* id_count = number of IDs - 1 */ +}; +struct ck_iort_smmu { + uint64_t base; /* SMMUv3 register base */ + uint32_t node_off; /* offset of the SMMUv3 node in the table */ + uint32_t nmaps; + struct ck_iort_map map[CK_IORT_MAX_MAPS]; +}; +/* 1 found, 0 no SMMUv3 node, -1 malformed (bad signature, truncation, a + * node or mapping array outside the table, base 0, more than + * CK_IORT_MAX_MAPS mappings to the SMMU). */ +int ck_iort_parse(const void *iort, struct ck_iort_smmu *out); +/* Stream ID for PCI requester id `rid` (bus<<8 | dev<<3 | fn); 0 ok, -1 if + * no mapping covers it. */ +int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t rid, uint32_t *sid); + #endif diff --git a/native/kernel/core/smmu.c b/native/kernel/core/smmu.c new file mode 100644 index 00000000..6eeaad3d --- /dev/null +++ b/native/kernel/core/smmu.c @@ -0,0 +1,329 @@ +/* smmu.c -- see smmu.h. Port of crates/aienos-kernel/src/smmu.rs. */ +#include "smmu.h" + +#define IDR1_SIDSIZE_MASK 0x3fu +#define IDR1_EVENTQS_SHIFT 16u +#define IDR1_CMDQS_SHIFT 21u +#define IDR1_QS_MASK 0x1fu +#define CR0_ACK_MASK 0x0fu +#define CMDQ_CONS_ERR_MASK (0x7fu << 24) +#define EVENTQ_OVERFLOW (1u << 31) + +static uint32_t rd(const struct ck_smmu_regs *r, uint32_t off) { return r->rd32(r->ctx, off); } +static void wr(const struct ck_smmu_regs *r, uint32_t off, uint32_t v) { r->wr32(r->ctx, off, v); } +static void wr64(const struct ck_smmu_regs *r, uint32_t off, uint64_t v) { r->wr64(r->ctx, off, v); } +static void bar(const struct ck_smmu_regs *r) { r->barrier(r->ctx); } + +static int pow2(uint32_t n, uint32_t min) { return n >= min && (n & (n - 1)) == 0; } +static uint32_t log2u(uint32_t n) +{ + uint32_t s = 0; + while (n > 1u) { + n >>= 1; + s++; + } + return s; +} +static int aligned(const void *p, uint64_t bytes) +{ + uint64_t a = (uint64_t)(uintptr_t)p; + if (bytes < 32u) bytes = 32u; + return a != 0 && (a & (bytes - 1u)) == 0; +} + +int ck_smmu_tables_check(const struct ck_smmu_tables *t) +{ + if (!pow2(t->ste_n, 1) || !pow2(t->cmd_n, 2) || !pow2(t->evt_n, 2) || t->ste_n > (1u << 16) || + t->cmd_n > (1u << 19) || t->evt_n > (1u << 19) || + !aligned(t->strtab, (uint64_t)t->ste_n * CK_SMMU_STE_BYTES) || + !aligned(t->cmdq, (uint64_t)t->cmd_n * CK_SMMU_CMD_BYTES) || + !aligned(t->evtq, (uint64_t)t->evt_n * CK_SMMU_EVT_BYTES)) + return CK_SMMU_EINVAL; + return 0; +} + +void ck_smmu_ste_abort(uint64_t ste[8]) +{ + /* V = 1, Config = 0b000: abort every transaction. No bypass builder. */ + ste[0] = 1; + for (int i = 1; i < 8; i++) + ste[i] = 0; +} + +void ck_smmu_ste_stage1(uint64_t ste[8], uint64_t cd_addr) +{ + /* V (bit 0), Config 0b101 (S1 translate, S2 bypass) in bits 3:1, + * S1Fmt 0 (linear, one CD), S1ContextPtr bits 51:6. */ + ste[0] = 1u | (0x5ull << 1) | (cd_addr & 0x000fffffffffffc0ull); + for (int i = 1; i < 8; i++) + ste[i] = 0; +} + +int ck_smmu_cd_stage1(uint64_t cd[8], uint64_t ttb0, uint16_t asid, uint64_t mair, uint8_t t0sz) +{ + if (t0sz > 39u || (ttb0 & 0xfffu)) + return CK_SMMU_EINVAL; + /* T0SZ, TG0 = 0 (4 KiB), IRGN0/ORGN0 = 0 and SH0 = 0 (Non-cacheable + * walks: the tables are in Non-cacheable memory; the Rust reference + * uses write-back walks over cleaned write-back memory), EPD1, V, + * IPS = 5 (48-bit), AA64, R (record faults), A (abort faulting + * transactions), ASID. */ + cd[0] = (uint64_t)t0sz | (1ull << 30) | (1ull << 31) | (0x5ull << 32) | (1ull << 41) | (1ull << 45) | + (1ull << 46) | ((uint64_t)asid << 48); + cd[1] = ttb0 & 0x0000fffffffffff0ull; + cd[2] = 0; + cd[3] = mair; + for (int i = 4; i < 8; i++) + cd[i] = 0; + return 0; +} + +void ck_smmu_cmd(uint64_t w[2], uint8_t opcode, uint32_t sid, int leaf) +{ + w[0] = (uint64_t)opcode | ((uint64_t)sid << 32); + w[1] = opcode == CK_SMMU_CMD_CFGI_ALL ? 31u : (leaf ? 1u : 0u); +} + +uint32_t ck_smmu_q_encode(uint32_t index, int wrap, uint32_t entries) +{ + return (index & (entries - 1u)) | ((wrap ? 1u : 0u) << log2u(entries)); +} + +struct qc { + uint32_t index; + int wrap; +}; +static struct qc q_decode(uint32_t v, uint32_t entries) +{ + struct qc c = { v & (entries - 1u), (v & entries) != 0 }; + return c; +} +static void q_advance(struct qc *c, uint32_t entries) +{ + if (++c->index == entries) { + c->index = 0; + c->wrap = !c->wrap; + } +} + +static int cmdq_cons(const struct ck_smmu_regs *r, uint32_t entries, struct qc *out) +{ + uint32_t v = rd(r, CK_SMMU_CMDQ_CONS); + if (v & CMDQ_CONS_ERR_MASK) + return CK_SMMU_ECMDQ; + *out = q_decode(v, entries); + return 0; +} + +int ck_smmu_submit(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, const uint64_t (*cmds)[2], + uint32_t n, uint32_t spins) +{ + uint32_t entries = t->cmd_n; + struct qc prod = q_decode(rd(r, CK_SMMU_CMDQ_PROD), entries), cons; + for (uint32_t k = 0; k < n; k++) { + /* Full when the indexes match and the wrap flags differ. */ + int space = 0; + for (uint32_t i = 0; i < spins; i++) { + int rc = cmdq_cons(r, entries, &cons); + if (rc) + return rc; + if (!(cons.index == prod.index && cons.wrap != prod.wrap)) { + space = 1; + break; + } + } + if (!space) + return CK_SMMU_ETIMEOUT; + volatile uint64_t *slot = t->cmdq + (uint64_t)prod.index * 2u; + slot[0] = cmds[k][0]; + slot[1] = cmds[k][1]; + bar(r); + q_advance(&prod, entries); + wr(r, CK_SMMU_CMDQ_PROD, ck_smmu_q_encode(prod.index, prod.wrap, entries)); + } + for (uint32_t i = 0; i < spins; i++) { + int rc = cmdq_cons(r, entries, &cons); + if (rc) + return rc; + if (cons.index == prod.index && cons.wrap == prod.wrap) + return 0; + } + return CK_SMMU_ETIMEOUT; +} + +static int poll_gbpa_idle(const struct ck_smmu_regs *r, uint32_t spins, uint32_t *out) +{ + for (uint32_t i = 0; i < spins; i++) { + uint32_t g = rd(r, CK_SMMU_GBPA); + if (!(g & CK_SMMU_GBPA_UPDATE)) { + *out = g; + return 0; + } + } + return CK_SMMU_ETIMEOUT; +} + +static int set_global_abort(const struct ck_smmu_regs *r, uint32_t spins) +{ + uint32_t g; + int rc = poll_gbpa_idle(r, spins, &g); + if (rc) + return rc; + g = (g & ~CK_SMMU_GBPA_UPDATE) | CK_SMMU_GBPA_ABORT; + wr(r, CK_SMMU_GBPA, g | CK_SMMU_GBPA_UPDATE); + rc = poll_gbpa_idle(r, spins, &g); + if (rc) + return rc; + return (g & CK_SMMU_GBPA_ABORT) ? 0 : CK_SMMU_ENOABORT; +} + +static int wait_cr0(const struct ck_smmu_regs *r, uint32_t want, uint32_t spins) +{ + for (uint32_t i = 0; i < spins; i++) + if ((rd(r, CK_SMMU_CR0ACK) & CR0_ACK_MASK) == (want & CR0_ACK_MASK)) + return 0; + return CK_SMMU_ETIMEOUT; +} + +static void write_ste_word(const struct ck_smmu_tables *t, uint32_t sid, int word, uint64_t v) +{ + /* A 64-bit aligned store is single-copy atomic: no torn word 0. */ + ((volatile uint64_t *)t->strtab)[(uint64_t)sid * 8u + (uint64_t)word] = v; +} + +static int invalidate_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, + uint32_t spins) +{ + /* STE, every cached CD of this stream (the CD page is reused when a + * stream is re-confined), then the TLBs. */ + uint64_t c[4][2]; + ck_smmu_cmd(c[0], CK_SMMU_CMD_CFGI_STE, sid, 1); + ck_smmu_cmd(c[1], CK_SMMU_CMD_CFGI_CD_ALL, sid, 0); + ck_smmu_cmd(c[2], CK_SMMU_CMD_TLBI_NSNH_ALL, 0, 0); + ck_smmu_cmd(c[3], CK_SMMU_CMD_SYNC, 0, 0); + bar(r); + return ck_smmu_submit(r, t, (const uint64_t (*)[2])c, 4, spins); +} + +static int program(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins) +{ + uint32_t sid_bits = log2u(t->ste_n); + if (!(rd(r, CK_SMMU_IDR0) & CK_SMMU_IDR0_S1P)) + return CK_SMMU_EUNSUP; + uint32_t idr1 = rd(r, CK_SMMU_IDR1); + if ((idr1 & IDR1_SIDSIZE_MASK) < sid_bits || + ((idr1 >> IDR1_CMDQS_SHIFT) & IDR1_QS_MASK) < log2u(t->cmd_n) || + ((idr1 >> IDR1_EVENTQS_SHIFT) & IDR1_QS_MASK) < log2u(t->evt_n)) + return CK_SMMU_EUNSUP; + uint64_t abort[8]; + ck_smmu_ste_abort(abort); + for (uint32_t sid = 0; sid < t->ste_n; sid++) + for (int w = 0; w < 8; w++) + write_ste_word(t, sid, w, abort[w]); + volatile uint64_t *cq = t->cmdq, *eq = t->evtq; + for (uint64_t i = 0; i < (uint64_t)t->cmd_n * 2u; i++) + cq[i] = 0; + for (uint64_t i = 0; i < (uint64_t)t->evt_n * 4u; i++) + eq[i] = 0; + bar(r); /* everything the SMMU will fetch is written */ + + wr(r, CK_SMMU_CR0, 0); + int rc = wait_cr0(r, 0, spins); + if (rc) + return rc; + /* CR1 = 0: queue and table fetches Non-cacheable, matching the + * Non-cacheable DMA pool (the Rust reference uses 0x0d75, write-back). */ + wr(r, CK_SMMU_CR1, 0); + wr64(r, CK_SMMU_CMDQ_BASE, (uint64_t)(uintptr_t)t->cmdq | log2u(t->cmd_n)); + wr(r, CK_SMMU_CMDQ_CONS, 0); + wr(r, CK_SMMU_CMDQ_PROD, 0); + wr64(r, CK_SMMU_EVENTQ_BASE, (uint64_t)(uintptr_t)t->evtq | log2u(t->evt_n)); + wr(r, CK_SMMU_EVENTQ_CONS, 0); + wr(r, CK_SMMU_EVENTQ_PROD, 0); + wr(r, CK_SMMU_STRTAB_BASE_CFG, sid_bits); /* FMT = 0: linear */ + wr64(r, CK_SMMU_STRTAB_BASE, (uint64_t)(uintptr_t)t->strtab); + wr(r, CK_SMMU_CR0, CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN); + rc = wait_cr0(r, CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN, spins); + if (rc) + return rc; + uint64_t c[3][2]; + ck_smmu_cmd(c[0], CK_SMMU_CMD_CFGI_ALL, 0, 0); + ck_smmu_cmd(c[1], CK_SMMU_CMD_TLBI_NSNH_ALL, 0, 0); + ck_smmu_cmd(c[2], CK_SMMU_CMD_SYNC, 0, 0); + rc = ck_smmu_submit(r, t, (const uint64_t (*)[2])c, 3, spins); + if (rc) + return rc; + uint32_t en = CK_SMMU_CR0_SMMUEN | CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN; + wr(r, CK_SMMU_CR0, en); + return wait_cr0(r, en, spins); +} + +int ck_smmu_enable(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins) +{ + if (ck_smmu_tables_check(t)) + return CK_SMMU_EINVAL; + int rc = set_global_abort(r, spins); + if (rc) + return rc; /* ABORT may not have latched: caller treats the SMMU as unusable */ + rc = program(r, t, spins); + if (rc) { + /* SMMU off behind GBPA.ABORT: every stream aborts. Best effort ack. */ + wr(r, CK_SMMU_CR0, 0); + (void)wait_cr0(r, 0, spins); + } + return rc; +} + +int ck_smmu_abort_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, + uint32_t spins) +{ + if (sid >= t->ste_n) + return CK_SMMU_EINVAL; + uint64_t abort[8]; + ck_smmu_ste_abort(abort); + /* Word 0 first: once V = 1 and Config = abort the rest is ignored. */ + for (int w = 0; w < 8; w++) + write_ste_word(t, sid, w, abort[w]); + return invalidate_ste(r, t, sid, spins); +} + +int ck_smmu_install_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, + const uint64_t ste[8], uint32_t spins) +{ + if (sid >= t->ste_n) + return CK_SMMU_EINVAL; + int rc = ck_smmu_abort_ste(r, t, sid, spins); + if (rc) + return rc; + for (int w = 1; w < 8; w++) + write_ste_word(t, sid, w, ste[w]); + bar(r); /* words 1..7 visible before word 0 makes the entry live */ + write_ste_word(t, sid, 0, ste[0]); + return invalidate_ste(r, t, sid, spins); +} + +int ck_smmu_read_events(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, + struct ck_smmu_event *out, int n, int *overflowed) +{ + uint32_t entries = t->evt_n; + uint32_t prod_raw = rd(r, CK_SMMU_EVENTQ_PROD), cons_raw = rd(r, CK_SMMU_EVENTQ_CONS); + struct qc prod = q_decode(prod_raw, entries), cons = q_decode(cons_raw, entries); + uint32_t ovf = prod_raw & EVENTQ_OVERFLOW; + int count = 0; + bar(r); + while ((cons.index != prod.index || cons.wrap != prod.wrap) && count < n) { + volatile const uint64_t *slot = t->evtq + (uint64_t)cons.index * 4u; + struct ck_smmu_event *e = &out[count++]; + for (int w = 0; w < 4; w++) + e->raw[w] = slot[w]; + e->type = (uint8_t)e->raw[0]; + e->sid = (uint32_t)(e->raw[0] >> 32); + e->addr = e->raw[2]; + q_advance(&cons, entries); + } + /* Copying OVFLG into OVACKFLG acknowledges an overflow. */ + wr(r, CK_SMMU_EVENTQ_CONS, ck_smmu_q_encode(cons.index, cons.wrap, entries) | ovf); + if (overflowed) + *overflowed = ovf != (cons_raw & EVENTQ_OVERFLOW); + return count; +} diff --git a/native/kernel/core/smmu.h b/native/kernel/core/smmu.h new file mode 100644 index 00000000..b73bba8f --- /dev/null +++ b/native/kernel/core/smmu.h @@ -0,0 +1,144 @@ +/* smmu.h -- Arm SMMUv3 programming for the AIENOS C kernel, ported from the + * Rust reference crates/aienos-kernel/src/smmu.rs (linear stream table, + * stage-1 translation only, one linear context descriptor per stream). + * + * Pure logic over a register-access callback and table memory, so the host + * tests drive it against a model SMMU. The kernel glue (core/smmu_svc.c) + * supplies MMIO accessors and Normal Non-cacheable table memory from the + * DMA pool (ck_dma_alloc), so no cache maintenance is needed; the barrier + * callback orders table writes before the register write that publishes + * them. Addresses are identity mapped: a table pointer is its bus address. + * + * Fail-closed rules (same as the Rust reference): + * - GBPA.ABORT is set first and never cleared: a disabled SMMU aborts all + * DMA. There is no bypass STE constructor. + * - Every stream starts as an abort STE; only an explicit install makes a + * stream translate, and only through a stage-1 table. + * - Any error after GBPA.ABORT latched writes CR0 = 0 (SMMU off, so ABORT + * governs every stream). Every poll is bounded by `spins`. + * QEMU is not hardware: nothing built on this is physically qualified. */ +#ifndef AIENOS_CK_SMMU_H +#define AIENOS_CK_SMMU_H +#include <stdint.h> + +#define CK_SMMU_STE_BYTES 64u +#define CK_SMMU_CD_BYTES 64u +#define CK_SMMU_CMD_BYTES 16u +#define CK_SMMU_EVT_BYTES 32u +#define CK_SMMU_MMIO_BYTES 0x20000u /* register pages 0 and 1 */ + +/* Register offsets (SMMUv3 architecture). */ +#define CK_SMMU_IDR0 0x00u +#define CK_SMMU_IDR1 0x04u +#define CK_SMMU_CR0 0x20u +#define CK_SMMU_CR0ACK 0x24u +#define CK_SMMU_CR1 0x28u +#define CK_SMMU_CR2 0x2cu +#define CK_SMMU_GBPA 0x44u +#define CK_SMMU_STRTAB_BASE 0x80u +#define CK_SMMU_STRTAB_BASE_CFG 0x88u +#define CK_SMMU_CMDQ_BASE 0x90u +#define CK_SMMU_CMDQ_PROD 0x98u +#define CK_SMMU_CMDQ_CONS 0x9cu +#define CK_SMMU_EVENTQ_BASE 0xa0u +#define CK_SMMU_EVENTQ_PROD 0x100a8u /* page 1 */ +#define CK_SMMU_EVENTQ_CONS 0x100acu /* page 1 */ + +#define CK_SMMU_IDR0_S1P (1u << 1) +#define CK_SMMU_GBPA_ABORT (1u << 20) +#define CK_SMMU_GBPA_UPDATE (1u << 31) +#define CK_SMMU_CR0_SMMUEN (1u << 0) +#define CK_SMMU_CR0_EVENTQEN (1u << 2) +#define CK_SMMU_CR0_CMDQEN (1u << 3) + +#define CK_SMMU_CMD_CFGI_STE 0x03u +#define CK_SMMU_CMD_CFGI_ALL 0x04u +#define CK_SMMU_CMD_CFGI_CD_ALL 0x06u +#define CK_SMMU_CMD_TLBI_NSNH_ALL 0x30u +#define CK_SMMU_CMD_SYNC 0x46u + +/* Event record types (word 0 bits 7:0). */ +#define CK_SMMU_EVT_F_TRANSLATION 0x10u +#define CK_SMMU_EVT_F_ADDR_SIZE 0x11u +#define CK_SMMU_EVT_F_ACCESS 0x12u +#define CK_SMMU_EVT_F_PERMISSION 0x13u +#define CK_SMMU_EVT_C_BAD_STE 0x04u +#define CK_SMMU_EVT_F_STREAM_DISABLED 0x06u + +#define CK_SMMU_DEFAULT_SPINS 1000000u + +/* Errors (negative). */ +#define CK_SMMU_ETIMEOUT (-10) /* a bounded poll ran out */ +#define CK_SMMU_ECMDQ (-11) /* CMDQ_CONS.ERR set */ +#define CK_SMMU_EUNSUP (-12) /* IDR0/IDR1 cannot run this geometry */ +#define CK_SMMU_EINVAL (-13) /* bad stream id, geometry or alignment */ +#define CK_SMMU_ENOABORT (-14) /* GBPA.ABORT did not latch */ + +struct ck_smmu_regs { + void *ctx; + uint32_t (*rd32)(void *ctx, uint32_t off); + void (*wr32)(void *ctx, uint32_t off, uint32_t v); + void (*wr64)(void *ctx, uint32_t off, uint64_t v); + void (*barrier)(void *ctx); /* table writes visible before the next register write */ +}; + +/* Linear stream table + queues. Entry counts are powers of two; every base + * is aligned to its own size (min 32 bytes). Pointers are bus addresses. */ +struct ck_smmu_tables { + uint64_t *strtab; /* ste_n x 8 words */ + uint32_t ste_n; + uint64_t *cmdq; /* cmd_n x 2 words */ + uint32_t cmd_n; + uint64_t *evtq; /* evt_n x 4 words */ + uint32_t evt_n; +}; + +struct ck_smmu_event { + uint8_t type; + uint32_t sid; + uint64_t addr; /* word 2: input address for translation-class faults */ + uint64_t raw[4]; +}; + +/* 0 if the geometry and alignment are valid, else CK_SMMU_EINVAL. */ +int ck_smmu_tables_check(const struct ck_smmu_tables *t); + +/* Descriptor builders. */ +void ck_smmu_ste_abort(uint64_t ste[8]); +void ck_smmu_ste_stage1(uint64_t ste[8], uint64_t cd_addr); +/* CD for a stage-1 walk: 4 KiB granule, TTBR0 only (EPD1), 48-bit IPS, + * walks Non-cacheable (the kernel's tables live in Non-cacheable memory), + * faults recorded (R) and aborted (A). CK_SMMU_EINVAL if t0sz > 39 or ttb0 + * is not 4 KiB aligned. */ +int ck_smmu_cd_stage1(uint64_t cd[8], uint64_t ttb0, uint16_t asid, uint64_t mair, uint8_t t0sz); +void ck_smmu_cmd(uint64_t w[2], uint8_t opcode, uint32_t sid, int leaf); + +/* Queue index encoding: index in bits n-1:0, wrap flag in bit n. */ +uint32_t ck_smmu_q_encode(uint32_t index, int wrap, uint32_t entries); + +/* Append commands and wait until the SMMU consumed them all. */ +int ck_smmu_submit(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, const uint64_t (*cmds)[2], + uint32_t n, uint32_t spins); + +/* Bring-up with every stream in abort: + * 1. GBPA.ABORT set and latched (else return without touching CR0); + * 2. IDR0.S1P, IDR1 SIDSIZE / CMDQS / EVENTQS checked against the tables; + * 3. all STEs abort, queues zeroed, barrier; + * 4. CR0 = 0 (ack), queues + stream table programmed, queues enabled (ack), + * CFGI_ALL + TLBI_NSNH_ALL + SYNC, SMMUEN (ack). + * Any error after step 1 writes CR0 = 0 and returns it. */ +int ck_smmu_enable(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t spins); + +/* Point stream `sid` at `ste`: abort + invalidate first, write words 1..7, + * barrier, then word 0, then CFGI_STE + TLBI_NSNH_ALL + SYNC. */ +int ck_smmu_install_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, + const uint64_t ste[8], uint32_t spins); +/* Return `sid` to abort and invalidate it. On 0 no further DMA translates. */ +int ck_smmu_abort_ste(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, uint32_t sid, + uint32_t spins); + +/* Consume up to n event records, oldest first; acknowledges an overflow. + * Returns the number of records written to out. */ +int ck_smmu_read_events(const struct ck_smmu_regs *r, const struct ck_smmu_tables *t, + struct ck_smmu_event *out, int n, int *overflowed); +#endif diff --git a/native/kernel/core/smmu_svc.c b/native/kernel/core/smmu_svc.c new file mode 100644 index 00000000..275e8071 --- /dev/null +++ b/native/kernel/core/smmu_svc.c @@ -0,0 +1,178 @@ +/* smmu_svc.c -- the ck.h DMA isolation services (ck_dma_confine and + * friends) over core/smmu.c, the IORT parser and the DMA pool. Kernel only + * (the host tests drive core/smmu.c directly against a model SMMU). + * + * Memory: stream table (4096 STEs, 256 KiB), command and event queues, the + * context descriptors and every stage-1 table come from ck_dma_alloc, which + * is Normal Non-cacheable, so a dsb orders them before register writes. */ +#include "ck_internal.h" +#include "pt.h" +#include "smmu.h" + +#define STE_N 4096u /* stream ids 0..4095: PCI buses 0..15 (QEMU virt uses bus 0) */ +#define QUEUE_N 16u +/* Window leaf: Normal Non-cacheable (same as the CPU's DMA pool mapping), + * read/write, AP[1] = 1 so unprivileged device transactions are allowed + * (PCIe requests are usually unprivileged), never executable. */ +#define WINDOW_ATTRS (CK_PT_NORMAL_NC | (1ull << 6)) + +static struct { + int state; /* 0 not tried, 1 up, CK_SMMU_ABSENT / CK_SMMU_FAILED */ + uint64_t base; + struct ck_iort_smmu iort; + struct ck_smmu_tables t; + uint64_t *cds; /* CK_DMA_MAX_STREAMS context descriptors, one page */ + uint32_t sid[CK_DMA_MAX_STREAMS]; + int used[CK_DMA_MAX_STREAMS]; +} g; + +static uint32_t r_rd32(void *ctx, uint32_t off) +{ + (void)ctx; + return *(volatile uint32_t *)(uintptr_t)(g.base + off); +} +static void r_wr32(void *ctx, uint32_t off, uint32_t v) +{ + (void)ctx; + *(volatile uint32_t *)(uintptr_t)(g.base + off) = v; +} +static void r_wr64(void *ctx, uint32_t off, uint64_t v) +{ + (void)ctx; + *(volatile uint64_t *)(uintptr_t)(g.base + off) = v; +} +static void r_bar(void *ctx) +{ + (void)ctx; + ck_mb(); +} +static const struct ck_smmu_regs regs = { 0, r_rd32, r_wr32, r_wr64, r_bar }; + +static int pt_alloc(void *ctx, uint64_t *phys) +{ + (void)ctx; + return ck_dma_alloc(4096, 4096, phys) ? 0 : -1; +} + +static int bring_up(void) +{ + if (g.state) + return g.state == 1 ? 0 : g.state; + g.state = CK_SMMU_ABSENT; + const void *iort = ck_acpi_find("IORT"); + if (!iort) + return g.state; + int rc = ck_iort_parse(iort, &g.iort); + if (rc == 0) + return g.state; + g.state = CK_SMMU_FAILED; + if (rc < 0) { + ck_printf("smmu: IORT malformed, SMMU unusable (fail closed)\n"); + return g.state; + } + g.base = g.iort.base; + ck_mmio_map(g.base, CK_SMMU_MMIO_BYTES); + uint64_t p; + g.t.ste_n = STE_N; + g.t.cmd_n = QUEUE_N; + g.t.evt_n = QUEUE_N; + g.t.strtab = ck_dma_alloc(STE_N * CK_SMMU_STE_BYTES, STE_N * CK_SMMU_STE_BYTES, &p); + g.t.cmdq = ck_dma_alloc(4096, 4096, &p); + g.t.evtq = ck_dma_alloc(4096, 4096, &p); + g.cds = ck_dma_alloc(4096, 4096, &p); + if (!g.t.strtab || !g.t.cmdq || !g.t.evtq || !g.cds) { + ck_printf("smmu: table allocation failed (fail closed)\n"); + return g.state; + } + rc = ck_smmu_enable(®s, &g.t, CK_SMMU_DEFAULT_SPINS); + if (rc) { + ck_printf("smmu: bring-up failed rc=%d base=0x%llx (fail closed)\n", rc, (unsigned long long)g.base); + return g.state; + } + g.state = 1; + return 0; +} + +int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) +{ + if (!len || (phys & 0xfffu) || (len & 0xfffu) || phys + len < phys) + return CK_SMMU_EARG; + int rc = bring_up(); + if (rc) + return rc; + uint32_t sid; + if (ck_iort_stream_id(&g.iort, rid, &sid)) + return CK_SMMU_NOSTREAM; + if (sid >= STE_N) + return CK_SMMU_NOSTREAM; + int slot = -1; + for (int i = 0; i < CK_DMA_MAX_STREAMS; i++) { + if (g.used[i] && g.sid[i] == sid) + return CK_SMMU_EARG; + if (!g.used[i] && slot < 0) + slot = i; + } + if (slot < 0) + return CK_SMMU_EARG; + struct ck_pt pt; + if (ck_pt_init(&pt, pt_alloc, 0) || ck_pt_map(&pt, phys, phys, len, WINDOW_ATTRS)) + return CK_SMMU_FAILED; + uint64_t *cd = g.cds + (uint64_t)slot * 8u; + if (ck_smmu_cd_stage1(cd, pt.root, (uint16_t)(slot + 1), CK_MAIR_VALUE, 16)) + return CK_SMMU_FAILED; + uint64_t ste[8]; + ck_smmu_ste_stage1(ste, (uint64_t)(uintptr_t)cd); + ck_mb(); /* tables and CD written before the STE goes live */ + rc = ck_smmu_install_ste(®s, &g.t, sid, ste, CK_SMMU_DEFAULT_SPINS); + if (rc) { + (void)ck_smmu_abort_ste(®s, &g.t, sid, CK_SMMU_DEFAULT_SPINS); + ck_printf("smmu: stream 0x%x install failed rc=%d (stream aborts)\n", sid, rc); + return CK_SMMU_FAILED; + } + g.used[slot] = 1; + g.sid[slot] = sid; + out->smmu_base = g.base; + out->stream_id = sid; + out->iova = phys; + out->len = len; + return 0; +} + +int ck_dma_unconfine(uint32_t stream_id) +{ + for (int i = 0; i < CK_DMA_MAX_STREAMS; i++) { + if (!g.used[i] || g.sid[i] != stream_id) + continue; + if (ck_smmu_abort_ste(®s, &g.t, stream_id, CK_SMMU_DEFAULT_SPINS)) + return CK_SMMU_FAILED; + g.used[i] = 0; + return 0; + } + return CK_SMMU_EARG; +} + +int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) +{ + if (g.state != 1) + return -1; + int matched = 0, ovf = 0, n; + struct ck_smmu_event ev[4]; + do { + int o = 0; + n = ck_smmu_read_events(®s, &g.t, ev, 4, &o); + ovf |= o; + for (int i = 0; i < n; i++) { + if (ev[i].sid != stream_id) + continue; + if (!matched && first) { + first->type = ev[i].type; + first->stream_id = ev[i].sid; + first->addr = ev[i].addr; + } + matched++; + } + } while (n == 4); + if (first) + first->overflow = ovf; + return matched; +} diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c index 2d145535..1703b5ee 100644 --- a/native/kernel/dev/devices.c +++ b/native/kernel/dev/devices.c @@ -12,13 +12,23 @@ const disk_dev *ck_dev_boot_disk(void) { return g_nvme.bound ? &g_nvme.disk : 0; void ck_dev_nvme_release(void) { - if (!g_nvme.pf || !g_nvme.bm_on) return; + if (!g_nvme.pf) return; g_nvme.bound = 0; - g_nvme.bm_on = 0; - if (pci_bus_master_off(g_nvme.pf) == 0) - ck_printf("dma_gate: nvme bus master revoked\n"); - else - ck_printf("dma_gate: nvme bus master revoke FAILED (command register still has BME)\n"); + if (g_nvme.bm_on) { + g_nvme.bm_on = 0; + if (pci_bus_master_off(g_nvme.pf) == 0) + ck_printf("dma_gate: nvme bus master revoked\n"); + else + ck_printf("dma_gate: nvme bus master revoke FAILED (command register still has BME)\n"); + } + if (g_nvme.confined) { + /* Bus mastering is off (or never came on); return the stream to + * abort as well, independently of the bus master state. */ + int urc = ck_dma_unconfine(g_nvme.stream_id); + g_nvme.confined = 0; + ck_printf("smmu: nvme stream 0x%x %s (rc=%d)\n", g_nvme.stream_id, + urc == 0 ? "returned to abort" : "abort FAILED", urc); + } } int ck_stage_devices(void) diff --git a/native/kernel/dev/nvme_bind.c b/native/kernel/dev/nvme_bind.c index 36f69f0a..480c9386 100644 --- a/native/kernel/dev/nvme_bind.c +++ b/native/kernel/dev/nvme_bind.c @@ -3,10 +3,12 @@ #include "ck.h" #include "disk_layout.h" -/* NVMe DMA rule: no SMMU confinement means no DMA. ck.h has no SMMU service, - * so the default build refuses NVMe DMA (fail-closed). The unconfined bypass - * exists only for QEMU and only when the QEMU gate script asks for it with - * CK_QEMU_UNSAFE_DMA=1; it can never be combined with a hardware-staging +/* NVMe DMA rule: no SMMU confinement means no DMA. The default build asks + * the core for SMMU confinement (ck_dma_confine) and refuses NVMe DMA when + * there is no SMMU or it did not come up (fail-closed). The unconfined + * bypass is TEST-ONLY: it exists only for QEMU without an SMMU, only when a + * QEMU gate script asks for it with CK_QEMU_UNSAFE_DMA=1, and is never used + * when an SMMU is present; it can never be combined with a hardware-staging * build (same rule as the compile_error! in crates/aienos-boot/src/lib.rs). */ #if defined(CK_NVME_DMA_BYPASS) #error "CK_NVME_DMA_BYPASS is retired: the unconfined NVMe DMA bypass is CK_QEMU_UNSAFE_DMA=1 (QEMU only)" @@ -47,6 +49,46 @@ static uint8_t probe_buf[CK_LAYOUT_UNIT]; static uint8_t probe_rd[CK_LAYOUT_UNIT]; static uint8_t probe_orig[CK_LAYOUT_UNIT]; +/* SMMU negative test (confined mode only). The driver's bounce-buffer bus + * address is pointed at a pattern-filled page that is NOT in the stream's + * window and a one-block read of LBA 0 is issued, so the controller tries + * to DMA into that page. Pass: the SMMU records a translation fault for this + * stream at that page, the page keeps its pattern (the write never landed), + * and after restoring the bounce address a normal read works again. The + * read's own status is reported but not judged (QEMU may complete it). */ +static int smmu_negative_test(ck_nvme *n) +{ + uint64_t cphys = 0; + volatile uint8_t *canary = ck_dma_alloc(NVME_PAGE, NVME_PAGE, &cphys); + if (!canary) { + ck_printf("smmu_negative: FAIL (no canary page)\n"); + return -1; + } + for (uint32_t i = 0; i < NVME_PAGE; i++) + canary[i] = 0x5a; + (void)ck_dma_faults(n->stream_id, 0); /* drain anything older */ + nvme_ctrl *c = &n->ctrl; + uint64_t saved = c->bounce_phys; + c->bounce_phys = cphys; + ck_mb(); + int rc = disk_read(&n->disk, 0, 1, probe_rd); + c->bounce_phys = saved; + ck_mb(); + struct ck_dma_fault fl = { 0, 0, 0, 0 }; + int faults = ck_dma_faults(n->stream_id, &fl); + int intact = 1; + for (uint32_t i = 0; i < NVME_PAGE; i++) + if (canary[i] != 0x5a) intact = 0; + int rc2 = disk_read(&n->disk, 0, 1, probe_rd); + int ok = faults >= 1 && fl.type == CK_DMA_FAULT_TRANSLATION && fl.stream_id == n->stream_id && + (fl.addr & ~(uint64_t)(NVME_PAGE - 1u)) == cphys && intact && rc2 == 0; + ck_printf("smmu_negative: %s dma outside window iova=0x%llx faults=%d type=0x%x sid=0x%x addr=0x%llx " + "page=%s cmd_rc=%d recovery_read=%s\n", + ok ? "refused" : "FAIL", (unsigned long long)cphys, faults, fl.type, fl.stream_id, + (unsigned long long)fl.addr, intact ? "intact" : "MODIFIED", rc, rc2 ? "FAIL" : "ok"); + return ok ? 0 : -1; +} + int ck_disk_rw_probe(const disk_dev *d, uint32_t seed, uint64_t *lba_out) { uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; @@ -89,8 +131,10 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) { n->bound = 0; n->bm_on = 0; + n->confined = 0; + n->stream_id = 0; #if CK_NVME_UNSAFE_BYPASS - ck_printf("WARNING: UNSAFE NVME DMA BYPASS BUILD (CK_QEMU_UNSAFE_DMA=1, QEMU debug only)\n"); + ck_printf("WARNING: UNSAFE NVME DMA BYPASS BUILD (CK_QEMU_UNSAFE_DMA=1, QEMU debug only, TEST-ONLY)\n"); #endif const pci_func *f = pci_find_class(pci, 0x010802u, 0xffffffu); if (!f) { @@ -106,27 +150,45 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) ck_printf("nvme: unavailable (BAR0 unusable)\n"); return NVME_EARG; } - /* DMA gate. The ck core has no SMMU service, so DMA cannot be confined - * to a window. The Rust kernel denies NVMe DMA without an SMMU unless - * built with its unsafe bypass; this stage mirrors that choice at build - * time (CK_QEMU_UNSAFE_DMA=1, QEMU gate only). */ -#if CK_NVME_UNSAFE_BYPASS - ck_printf("WARNING: UNSAFE NVME DMA BYPASS ACTIVE (ck core has no SMMU service; device can DMA anywhere)\n"); - ck_printf("dma_gate: nvme granted (UnsafeBypass), bus master on\n"); -#else - ck_printf("dma_gate: nvme denied (NoSmmu), bus master stays off\n"); - ck_printf("nvme: unavailable (SMMU DMA isolation not active)\n"); - return NVME_ESTATE; -#endif - n->bar0 = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); - pci_enable(f, 1); - n->bm_on = 1; + /* DMA gate (same policy as the Rust dma_gate::dma_grant): SMMU + * confinement first; with no SMMU at all, only the QEMU-only unsafe + * bypass build may enable DMA; an SMMU that is present but did not come + * up never falls back to the bypass. The DMA region is allocated first + * so the confinement window is exactly the region the driver uses. */ uint64_t phys = 0; uint8_t *dma = ck_dma_alloc(CK_NVME_DMA_BYTES, NVME_PAGE, &phys); if (!dma) { ck_printf("nvme: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)CK_NVME_DMA_BYTES); return NVME_EARG; } + uint32_t rid = ((uint32_t)f->bus << 8) | ((uint32_t)f->dev << 3) | (uint32_t)f->fn; + uint64_t win = ((uint64_t)CK_NVME_DMA_BYTES + NVME_PAGE - 1u) & ~(uint64_t)(NVME_PAGE - 1u); + struct ck_dma_confinement cf; + int src = ck_dma_confine(rid, phys, win, &cf); + if (src == 0) { + n->confined = 1; + n->stream_id = cf.stream_id; + ck_printf("smmu: enabled base=0x%llx stream_id=0x%x\n", (unsigned long long)cf.smmu_base, cf.stream_id); + ck_printf("smmu_dma_window: nvme only, translation active iova=0x%llx len=0x%llx rid=0x%x\n", + (unsigned long long)cf.iova, (unsigned long long)cf.len, rid); + ck_printf("dma_gate: nvme granted (Confined), bus master on\n"); + } else if (src == CK_SMMU_ABSENT) { +#if CK_NVME_UNSAFE_BYPASS + ck_printf("WARNING: UNSAFE NVME DMA BYPASS ACTIVE (no SMMU on this machine; device can DMA anywhere; TEST-ONLY build)\n"); + ck_printf("dma_gate: nvme granted (UnsafeBypass), bus master on\n"); +#else + ck_printf("dma_gate: nvme denied (NoSmmu), bus master stays off\n"); + ck_printf("nvme: unavailable (SMMU DMA isolation not active)\n"); + return NVME_ESTATE; +#endif + } else { + ck_printf("dma_gate: nvme denied (SmmuNotReady rc=%d), bus master stays off\n", src); + ck_printf("nvme: unavailable (SMMU DMA isolation not active)\n"); + return NVME_ESTATE; + } + n->bar0 = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); + pci_enable(f, 1); + n->bm_on = 1; nvme_ops ops = {n, m_r32, m_w32, 0, 0, m_barrier, m_delay}; nvme_dma region = {dma, phys, CK_NVME_DMA_BYTES}; nvme_config cfg = {0, 0}; @@ -161,6 +223,10 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) ck_printf("nvme: rw probe lba=%llu bytes=%u write+flush+readback %s (rc=%d)\n", (unsigned long long)lba, CK_LAYOUT_UNIT, rc ? "FAIL" : "match", rc); if (rc) return rc; + if (n->confined) { + rc = smmu_negative_test(n); + if (rc) return DISK_EIO; + } n->bound = 1; return 0; } diff --git a/native/kernel/dev/nvme_bind.h b/native/kernel/dev/nvme_bind.h index 8f3cba9a..1eb2b5c5 100644 --- a/native/kernel/dev/nvme_bind.h +++ b/native/kernel/dev/nvme_bind.h @@ -16,6 +16,8 @@ typedef struct { const pci_func *pf; int bound; int bm_on; /* bus mastering was enabled for this function */ + int confined; /* DMA confined by the SMMU (ck_dma_confine) */ + uint32_t stream_id; /* SMMU stream when confined */ } ck_nvme; /* Find the first class 01/08/02 function, gate DMA, map BAR0, init the diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index e475b8c5..7622217b 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -70,6 +70,45 @@ int ck_irq_register(uint32_t intid, void (*fn)(void *arg), void *arg); int ck_irq_enable(uint32_t intid); void ck_irq_cpu_enable(int on); +/* DMA isolation (IORT + SMMUv3, core/smmu_svc.c; added by Lane 25). The + * first call brings the SMMU up with GBPA.ABORT set and every stream in + * abort, then gives PCI requester `rid` (bus<<8 | dev<<3 | fn, segment 0) + * a stage-1 table that maps only [phys, phys+len) at the same bus address + * (identity IOVA, Normal Non-cacheable, read/write). Any DMA outside the + * window faults and is aborted. phys and len must be 4 KiB aligned. At most + * CK_DMA_MAX_STREAMS confinements, one per stream. + * Returns 0 and fills *out, or: + * CK_SMMU_ABSENT no SMMUv3 in the IORT (or no IORT): no confinement + * CK_SMMU_FAILED SMMU present but bring-up or this stream failed; + * the SMMU stays off behind ABORT or the stream aborts + * CK_SMMU_NOSTREAM the IORT maps no stream for this requester + * CK_SMMU_EARG bad window, stream already confined, or table full + * Device DMA may be enabled only after a 0 return. */ +#define CK_SMMU_ABSENT (-1) +#define CK_SMMU_FAILED (-2) +#define CK_SMMU_NOSTREAM (-3) +#define CK_SMMU_EARG (-4) +#define CK_DMA_MAX_STREAMS 8 +struct ck_dma_confinement { + uint64_t smmu_base; + uint32_t stream_id; + uint64_t iova, len; +}; +int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out); +/* Return a confined stream to abort (after the device's bus mastering is + * off). 0 ok, CK_SMMU_EARG if not confined, CK_SMMU_FAILED on a command + * timeout. */ +int ck_dma_unconfine(uint32_t stream_id); +/* Drain the SMMU event queue. Returns how many records named `stream_id` + * (others are dropped); *first gets the first of them. -1 if no SMMU. */ +#define CK_DMA_FAULT_TRANSLATION 0x10u /* SMMUv3 F_TRANSLATION event */ +struct ck_dma_fault { + uint32_t type, stream_id; + uint64_t addr; + int overflow; +}; +int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first); + /* ---- boot stages (implemented by the stage worker, all optional) -------- */ /* Each returns 0 on success, negative on failure; core prints the result. * Declared weak so the core links and boots without them. */ diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index ee363d36..276d25f1 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -37,9 +37,9 @@ STAGE_INC := -I$(STAGE_DIR)/include -I$(STAGE_DIR)/dev -I$(STAGE_DIR)/svc \ -I$(STAGE_NATIVE)/disk -I$(STAGE_NATIVE)/store -I$(STAGE_NATIVE)/m5 -I$(STAGE_NATIVE)/crypto \ -I$(STAGE_NATIVE)/argus -I$(STAGE_NATIVE)/capability -I$(STAGE_NATIVE)/net # compat first: <pthread.h> <stdlib.h> <stdio.h> <time.h> resolve to the stand-ins. -# NVMe DMA: refused by default (ck.h has no SMMU service). The kernel Makefile -# adds -DCK_QEMU_UNSAFE_DMA=1 only for "make full CK_QEMU_UNSAFE_DMA=1" (the QEMU -# store gate); dev/nvme_bind.c refuses it together with CK_HARDWARE_STAGING. +# NVMe DMA: SMMU-confined (ck_dma_confine) or refused. The kernel Makefile +# adds -DCK_QEMU_UNSAFE_DMA=1 only for "make full CK_QEMU_UNSAFE_DMA=1" (TEST-ONLY, +# one boot of the QEMU store gate); dev/nvme_bind.c refuses it together with CK_HARDWARE_STAGING. STAGE_CFLAGS := -I$(STAGE_DIR)/svc/compat $(STAGE_INC) # ---- freestanding check -------------------------------------------------- @@ -51,6 +51,7 @@ STAGE_FREE_FLAGS := -std=gnu11 -O2 -Wall -Wextra -Werror -ffreestanding -nostdli -fno-pic -fno-pie -fstack-usage -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 # The only undefined symbols the stages may leave for the core: ck.h services. STAGE_ALLOWED_U := ck_puts ck_printf ck_vprintf ck_panic ck_alloc ck_free ck_dma_alloc ck_mmio_map \ + ck_dma_confine ck_dma_unconfine ck_dma_faults \ ck_mb ck_acpi_find ck_time_us ck_udelay ck_commit STAGE_FREE_OBJS := $(foreach s,$(STAGE_SRCS),$(STAGE_OUT)/free/$(subst /,_,$(subst $(STAGE_NATIVE)/,,$(s:.c=.o)))) diff --git a/native/kernel/svc/tests/ck_host.c b/native/kernel/svc/tests/ck_host.c index 041929c8..459d4351 100644 --- a/native/kernel/svc/tests/ck_host.c +++ b/native/kernel/svc/tests/ck_host.c @@ -75,3 +75,16 @@ uint32_t ck_boot_count_hint(void) { return 0; } /* Host: the capability library reads the real /dev/urandom. */ int ck_compat_entropy_source(void) { return CK_ENTROPY_HOST; } + +/* Host: no IORT, so no SMMU (the NVMe gate stays fail-closed). */ +int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) +{ + (void)rid; (void)phys; (void)len; (void)out; + return CK_SMMU_ABSENT; +} +int ck_dma_unconfine(uint32_t stream_id) { (void)stream_id; return CK_SMMU_EARG; } +int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) +{ + (void)stream_id; (void)first; + return -1; +} diff --git a/native/kernel/tests/test_smmu.c b/native/kernel/tests/test_smmu.c new file mode 100644 index 00000000..3d7184a4 --- /dev/null +++ b/native/kernel/tests/test_smmu.c @@ -0,0 +1,395 @@ +/* test_smmu.c -- core/smmu.c against a model SMMUv3, and the IORT parser + * (core/acpi.c) on a crafted table shaped like QEMU virt's with + * iommu=smmuv3. Ported in spirit from the Rust smmu.rs unit tests: bring-up + * keeps GBPA.ABORT through SMMUEN, every failure fails closed (CR0 = 0), + * STE install/abort invalidate the stream, queues wrap, events decode. */ +#include <stdint.h> +#include <string.h> +#include "ck_test.h" +#include "acpi.h" +#include "smmu.h" + +/* ---- model SMMU ---------------------------------------------------------- */ +#define LOGN 256 +struct model { + uint32_t idr0, idr1, cr0, gbpa, cmdq_prod, cmdq_cons, evtq_prod, evtq_cons, strtab_cfg, cr1; + uint64_t strtab, cmdq_base, evtq_base; + int gbpa_stuck; /* UPDATE never clears */ + int abort_ignored; /* ABORT never latches */ + int ack_stuck_en; /* CR0ACK never shows SMMUEN */ + int cmdq_stuck; /* CONS never advances */ + int cmdq_err; /* CONS reports an error */ + uint32_t wlog_off[LOGN], wlog_val[LOGN]; + int wn; + uint64_t cmds[64][2]; + int ncmd; + int barriers; +}; +static struct model M; + +static uint32_t m_rd(void *ctx, uint32_t off) +{ + struct model *m = ctx; + switch (off) { + case CK_SMMU_IDR0: return m->idr0; + case CK_SMMU_IDR1: return m->idr1; + case CK_SMMU_CR0ACK: return m->ack_stuck_en ? (m->cr0 & ~CK_SMMU_CR0_SMMUEN) : m->cr0; + case CK_SMMU_GBPA: return m->gbpa; + case CK_SMMU_CMDQ_PROD: return m->cmdq_prod; + case CK_SMMU_CMDQ_CONS: return m->cmdq_cons | (m->cmdq_err ? (1u << 24) : 0); + case CK_SMMU_EVENTQ_PROD: return m->evtq_prod; + case CK_SMMU_EVENTQ_CONS: return m->evtq_cons; + } + return 0; +} +static void m_consume(struct model *m) +{ + uint32_t n = 1u << (m->cmdq_base & 0x1f); + uint64_t *q = (uint64_t *)(uintptr_t)(m->cmdq_base & ~0x1full); + while (m->cmdq_cons != m->cmdq_prod) { + uint32_t idx = m->cmdq_cons & (n - 1); + if (m->ncmd < 64) { + m->cmds[m->ncmd][0] = q[idx * 2]; + m->cmds[m->ncmd][1] = q[idx * 2 + 1]; + m->ncmd++; + } + uint32_t wrap = m->cmdq_cons & n; + idx++; + if (idx == n) { idx = 0; wrap ^= n; } + m->cmdq_cons = idx | wrap; + } +} +static void m_wr(void *ctx, uint32_t off, uint32_t v) +{ + struct model *m = ctx; + if (m->wn < LOGN) { m->wlog_off[m->wn] = off; m->wlog_val[m->wn] = v; m->wn++; } + switch (off) { + case CK_SMMU_CR0: m->cr0 = v; break; + case CK_SMMU_CR1: m->cr1 = v; break; + case CK_SMMU_GBPA: + if (m->gbpa_stuck) { m->gbpa = v; break; } + m->gbpa = v & ~CK_SMMU_GBPA_UPDATE; + if (m->abort_ignored) m->gbpa &= ~CK_SMMU_GBPA_ABORT; + break; + case CK_SMMU_CMDQ_PROD: + m->cmdq_prod = v; + if (!m->cmdq_stuck && !m->cmdq_err) m_consume(m); + break; + case CK_SMMU_CMDQ_CONS: m->cmdq_cons = v; break; + case CK_SMMU_EVENTQ_PROD: m->evtq_prod = v; break; + case CK_SMMU_EVENTQ_CONS: m->evtq_cons = v; break; + case CK_SMMU_STRTAB_BASE_CFG: m->strtab_cfg = v; break; + } +} +static void m_wr64(void *ctx, uint32_t off, uint64_t v) +{ + struct model *m = ctx; + if (m->wn < LOGN) { m->wlog_off[m->wn] = off; m->wlog_val[m->wn] = (uint32_t)v; m->wn++; } + if (off == CK_SMMU_CMDQ_BASE) m->cmdq_base = v; + if (off == CK_SMMU_EVENTQ_BASE) m->evtq_base = v; + if (off == CK_SMMU_STRTAB_BASE) m->strtab = v; +} +static void m_bar(void *ctx) { ((struct model *)ctx)->barriers++; } +static const struct ck_smmu_regs R = { &M, m_rd, m_wr, m_wr64, m_bar }; + +#define SIDS 16u +#define QN 16u +static struct { + uint64_t ste[SIDS * 8] __attribute__((aligned(1024))); + uint64_t cmd[QN * 2] __attribute__((aligned(256))); + uint64_t evt[QN * 4] __attribute__((aligned(512))); +} mem; +static struct ck_smmu_tables T; + +static void reset(void) +{ + memset(&M, 0, sizeof M); + memset(&mem, 0xee, sizeof mem); + M.idr0 = CK_SMMU_IDR0_S1P; + M.idr1 = 16u | (19u << 16) | (19u << 21); + T.strtab = mem.ste; T.ste_n = SIDS; + T.cmdq = mem.cmd; T.cmd_n = QN; + T.evtq = mem.evt; T.evt_n = QN; +} +static int first_write(uint32_t off, uint32_t mask, uint32_t val) +{ + for (int i = 0; i < M.wn; i++) + if (M.wlog_off[i] == off && (M.wlog_val[i] & mask) == val) return i; + return -1; +} +static uint32_t last_cr0(void) +{ + uint32_t v = 0xffffffffu; + for (int i = 0; i < M.wn; i++) + if (M.wlog_off[i] == CK_SMMU_CR0) v = M.wlog_val[i]; + return v; +} + +static void test_layouts(void) +{ + uint64_t s[8], cd[8], c[2]; + ck_smmu_ste_abort(s); + CHECK(s[0] == 1 && s[1] == 0 && s[7] == 0); + ck_smmu_ste_stage1(s, 0x40001040ull); + CHECK(s[0] == (1ull | (5ull << 1) | 0x40001040ull)); + ck_smmu_ste_stage1(s, 0x40001047ull); /* low bits never leak into control bits */ + CHECK((s[0] & 0x3f) == 0xb); + CHECK(ck_smmu_cd_stage1(cd, 0x80000000ull, 3, 0x4404ff, 16) == 0); + CHECK((cd[0] & 0x3f) == 16); /* T0SZ */ + CHECK(((cd[0] >> 6) & 3) == 0); /* TG0 4 KiB */ + CHECK(((cd[0] >> 8) & 0x3f) == 0); /* IRGN0/ORGN0/SH0: Non-cacheable */ + CHECK(((cd[0] >> 14) & 1) == 0); /* EPD0 clear: TTBR0 walks */ + CHECK((cd[0] >> 30) & 1); /* EPD1 */ + CHECK((cd[0] >> 31) & 1); /* V */ + CHECK(((cd[0] >> 32) & 7) == 5); /* IPS 48-bit */ + CHECK((cd[0] >> 41) & 1); /* AA64 */ + CHECK(((cd[0] >> 45) & 3) == 3); /* R and A */ + CHECK((cd[0] >> 48) == 3); /* ASID */ + CHECK(cd[1] == 0x80000000ull && cd[2] == 0 && cd[3] == 0x4404ff); + CHECK(ck_smmu_cd_stage1(cd, 0x80000800ull, 1, 0, 16) == CK_SMMU_EINVAL); + CHECK(ck_smmu_cd_stage1(cd, 0x80000000ull, 1, 0, 40) == CK_SMMU_EINVAL); + ck_smmu_cmd(c, CK_SMMU_CMD_CFGI_ALL, 0, 0); + CHECK(c[0] == 0x04 && c[1] == 31); + ck_smmu_cmd(c, CK_SMMU_CMD_CFGI_STE, 0x10, 1); + CHECK(c[0] == (0x03ull | (0x10ull << 32)) && c[1] == 1); + CHECK(ck_smmu_q_encode(3, 0, 16) == 3 && ck_smmu_q_encode(3, 1, 16) == 0x13 && ck_smmu_q_encode(17, 0, 16) == 1); +} + +static void test_tables_check(void) +{ + reset(); + CHECK(ck_smmu_tables_check(&T) == 0); + struct ck_smmu_tables b = T; + b.ste_n = 12; + CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL); + b = T; + b.cmd_n = 1; + CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL); + b = T; + b.strtab = mem.ste + 8; /* 64-byte aligned, not 1 KiB */ + CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL); + b = T; + b.evtq = 0; + CHECK(ck_smmu_tables_check(&b) == CK_SMMU_EINVAL); + reset(); + T.strtab = mem.ste + 8; + CHECK(ck_smmu_enable(&R, &T, 100) == CK_SMMU_EINVAL); + CHECK(M.wn == 0); /* nothing touched */ +} + +static void test_enable_ok(void) +{ + reset(); + CHECK(ck_smmu_enable(&R, &T, 100) == 0); + int abort_at = first_write(CK_SMMU_GBPA, CK_SMMU_GBPA_ABORT, CK_SMMU_GBPA_ABORT); + int en_at = first_write(CK_SMMU_CR0, CK_SMMU_CR0_SMMUEN, CK_SMMU_CR0_SMMUEN); + CHECK(abort_at >= 0 && en_at > abort_at); + CHECK(M.wlog_off[0] == CK_SMMU_GBPA); /* abort is the very first write */ + CHECK(M.gbpa & CK_SMMU_GBPA_ABORT); /* and never cleared */ + CHECK(M.cr0 == (CK_SMMU_CR0_SMMUEN | CK_SMMU_CR0_CMDQEN | CK_SMMU_CR0_EVENTQEN)); + CHECK(M.cr1 == 0); + CHECK(M.strtab_cfg == 4 && M.strtab == (uint64_t)(uintptr_t)mem.ste); + CHECK(M.cmdq_base == ((uint64_t)(uintptr_t)mem.cmd | 4) && M.evtq_base == ((uint64_t)(uintptr_t)mem.evt | 4)); + int all_abort = 1; + for (unsigned i = 0; i < SIDS; i++) + for (unsigned w = 0; w < 8; w++) + if (mem.ste[i * 8 + w] != (w == 0 ? 1u : 0u)) all_abort = 0; + CHECK(all_abort); + CHECK(M.ncmd == 3 && (M.cmds[0][0] & 0xff) == CK_SMMU_CMD_CFGI_ALL && + (M.cmds[1][0] & 0xff) == CK_SMMU_CMD_TLBI_NSNH_ALL && (M.cmds[2][0] & 0xff) == CK_SMMU_CMD_SYNC); + /* SMMUEN only after the invalidation commands were consumed. */ + CHECK(first_write(CK_SMMU_CMDQ_PROD, 0xffffffffu, 3) < en_at); + CHECK(M.barriers > 0); +} + +static void test_enable_fail_closed(void) +{ + reset(); + M.gbpa = CK_SMMU_GBPA_UPDATE; + M.gbpa_stuck = 1; + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT); + CHECK(first_write(CK_SMMU_CR0, 0, 0) < 0); /* CR0 never touched */ + + reset(); + M.abort_ignored = 1; + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ENOABORT); + CHECK(first_write(CK_SMMU_CR0, 0, 0) < 0); + + reset(); + M.ack_stuck_en = 1; + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT); + CHECK(last_cr0() == 0 && (M.gbpa & CK_SMMU_GBPA_ABORT)); + + reset(); + M.cmdq_err = 1; + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ECMDQ); + CHECK(last_cr0() == 0); + CHECK(first_write(CK_SMMU_CR0, CK_SMMU_CR0_SMMUEN, CK_SMMU_CR0_SMMUEN) < 0); + + reset(); + M.cmdq_stuck = 1; + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_ETIMEOUT); + CHECK(last_cr0() == 0); + + reset(); + M.idr0 = 0; /* no stage 1 */ + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP); + CHECK(last_cr0() == 0); + + reset(); + M.idr1 = 3u | (19u << 16) | (19u << 21); /* 8 stream ids < 16 */ + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP); + reset(); + M.idr1 = 16u | (19u << 16) | (3u << 21); /* command queue too small */ + CHECK(ck_smmu_enable(&R, &T, 50) == CK_SMMU_EUNSUP); +} + +static void test_install_abort(void) +{ + reset(); + CHECK(ck_smmu_enable(&R, &T, 100) == 0); + M.ncmd = 0; + uint64_t ste[8]; + ck_smmu_ste_stage1(ste, 0x12345640ull); + CHECK(ck_smmu_install_ste(&R, &T, 5, ste, 100) == 0); + CHECK(mem.ste[5 * 8] == ste[0] && mem.ste[4 * 8] == 1 && mem.ste[6 * 8] == 1); + /* abort+invalidate, then install+invalidate: two CFGI_STE(5) batches */ + CHECK(M.ncmd == 8 && (M.cmds[0][0] & 0xff) == CK_SMMU_CMD_CFGI_STE && (M.cmds[0][0] >> 32) == 5 && + M.cmds[0][1] == 1 && (M.cmds[1][0] & 0xff) == CK_SMMU_CMD_CFGI_CD_ALL && (M.cmds[1][0] >> 32) == 5 && + (M.cmds[4][0] & 0xff) == CK_SMMU_CMD_CFGI_STE && (M.cmds[5][0] & 0xff) == CK_SMMU_CMD_CFGI_CD_ALL && + (M.cmds[6][0] & 0xff) == CK_SMMU_CMD_TLBI_NSNH_ALL && (M.cmds[7][0] & 0xff) == CK_SMMU_CMD_SYNC); + CHECK(ck_smmu_install_ste(&R, &T, SIDS, ste, 100) == CK_SMMU_EINVAL); + CHECK(ck_smmu_abort_ste(&R, &T, SIDS, 100) == CK_SMMU_EINVAL); + M.ncmd = 0; + CHECK(ck_smmu_abort_ste(&R, &T, 5, 100) == 0); + CHECK(mem.ste[5 * 8] == 1 && mem.ste[5 * 8 + 1] == 0); + CHECK(M.ncmd == 4 && (M.cmds[0][0] >> 32) == 5 && (M.cmds[1][0] >> 32) == 5); + /* A stuck queue on install leaves the stream in abort. */ + M.cmdq_stuck = 1; + CHECK(ck_smmu_install_ste(&R, &T, 7, ste, 50) == CK_SMMU_ETIMEOUT); + CHECK(mem.ste[7 * 8] == 1); +} + +static void test_queue_wrap(void) +{ + reset(); + CHECK(ck_smmu_enable(&R, &T, 100) == 0); + M.ncmd = 0; + uint64_t c[20][2]; + for (int i = 0; i < 20; i++) + ck_smmu_cmd(c[i], CK_SMMU_CMD_SYNC, (uint32_t)i, 0); + CHECK(ck_smmu_submit(&R, &T, (const uint64_t (*)[2])c, 20, 100) == 0); + CHECK(M.ncmd == 20 && (M.cmds[19][0] >> 32) == 19); + /* 3 (enable) + 20 = 23 = one wrap of 16 + index 7 */ + CHECK(M.cmdq_prod == (7u | 16u) && M.cmdq_cons == M.cmdq_prod); + /* A full queue that never drains times out. */ + M.cmdq_stuck = 1; + CHECK(ck_smmu_submit(&R, &T, (const uint64_t (*)[2])c, 20, 50) == CK_SMMU_ETIMEOUT); +} + +static void test_events(void) +{ + reset(); + CHECK(ck_smmu_enable(&R, &T, 100) == 0); + mem.evt[0] = 0x10 | (0x10ull << 32); + mem.evt[2] = 0x7000; + mem.evt[4] = 0x06 | (0x20ull << 32); + mem.evt[6] = 0; + M.evtq_prod = 2; + struct ck_smmu_event ev[4]; + int ovf = -1; + CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 2); + CHECK(ev[0].type == 0x10 && ev[0].sid == 0x10 && ev[0].addr == 0x7000 && ev[1].type == 0x06 && ev[1].sid == 0x20); + CHECK(ovf == 0 && M.evtq_cons == 2); + CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 0); + /* Wrapped producer with overflow flag: acknowledged once. */ + M.evtq_prod = 1u | 16u | (1u << 31); + M.evtq_cons = 15; + CHECK(ck_smmu_read_events(&R, &T, ev, 1, &ovf) == 1 && ovf == 1); + CHECK(M.evtq_cons == (0u | 16u | (1u << 31))); + CHECK(ck_smmu_read_events(&R, &T, ev, 4, &ovf) == 1 && ovf == 0); +} + +/* ---- IORT -------------------------------------------------------------- */ +static void w16(uint8_t *p, uint16_t v) { memcpy(p, &v, 2); } +static void w32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); } +static void w64(uint8_t *p, uint64_t v) { memcpy(p, &v, 8); } + +/* header(48) | ITS group (24 bytes, type 0) | SMMUv3 node (68 bytes, one + * mapping to the ITS) | root complex (36 + 20*2 bytes, mappings to SMMU + * and to the ITS) */ +static uint32_t build_iort(uint8_t *t, int smmu_present) +{ + memset(t, 0, 512); + memcpy(t, "IORT", 4); + uint32_t at = 48, its = at, smmu, rc; + t[at] = 0; w16(t + at + 1, 24); w32(t + at + 16, 1); at += 24; + smmu = at; + t[at] = smmu_present ? 4 : 1; w16(t + at + 1, 88); + w32(t + at + 8, 1); w32(t + at + 12, 68); + w64(t + at + 16, 0x09050000ull); + w32(t + at + 68 + 4, 0xffff); w32(t + at + 68 + 12, its); + at += 88; + rc = at; + t[at] = 2; w16(t + at + 1, 36 + 40); + w32(t + at + 8, 2); w32(t + at + 12, 36); + w32(t + at + 36 + 0, 0); w32(t + at + 36 + 4, 0xffff); w32(t + at + 36 + 8, 0); w32(t + at + 36 + 12, smmu); + w32(t + at + 56 + 0, 0x10000); w32(t + at + 56 + 4, 0xff); w32(t + at + 56 + 8, 0); w32(t + at + 56 + 12, its); + at += 76; + w32(t + 4, at); + w32(t + 36, 3); + w32(t + 40, 48); + (void)rc; + return at; +} + +static void test_iort(void) +{ + static uint8_t t[512]; + struct ck_iort_smmu s; + uint32_t sid = 0; + build_iort(t, 1); + CHECK(ck_iort_parse(t, &s) == 1); + CHECK(s.base == 0x09050000ull && s.nmaps == 1 && s.map[0].id_count == 0xffff); + CHECK(ck_iort_stream_id(&s, 0x0010, &sid) == 0 && sid == 0x10); + CHECK(ck_iort_stream_id(&s, 0xffff, &sid) == 0 && sid == 0xffff); + CHECK(ck_iort_stream_id(&s, 0x10000, &sid) == -1); /* the ITS-only mapping is not an SMMU stream */ + build_iort(t, 0); + CHECK(ck_iort_parse(t, &s) == 0); + /* Malformed: node length past the table, mapping array past the node, + * SMMU base 0, bad signature. */ + build_iort(t, 1); + w16(t + 48 + 24 + 1, 400); + CHECK(ck_iort_parse(t, &s) == -1); + build_iort(t, 1); + w32(t + 48 + 24 + 88 + 8, 9); + CHECK(ck_iort_parse(t, &s) == -1); + build_iort(t, 1); + w64(t + 48 + 24 + 16, 0); + CHECK(ck_iort_parse(t, &s) == -1); + build_iort(t, 1); + t[0] = 'X'; + CHECK(ck_iort_parse(t, &s) == -1); + build_iort(t, 1); + w32(t + 36, 9); /* more nodes than fit */ + CHECK(ck_iort_parse(t, &s) == -1); + build_iort(t, 1); + w32(t + 40, 40); /* node array inside the 48-byte IORT header */ + CHECK(ck_iort_parse(t, &s) == -1); + /* A mapping whose output would wrap is never a stream. */ + struct ck_iort_smmu w = { 1, 0, 1, { { 0, 0xffff, 0xfffffff0u } } }; + CHECK(ck_iort_stream_id(&w, 0x20, &sid) == -1); +} + +int main(void) +{ + test_layouts(); + test_tables_check(); + test_enable_ok(); + test_enable_fail_closed(); + test_install_abort(); + test_queue_wrap(); + test_events(); + test_iort(); + return ck_t_verdict("test_smmu"); +} diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index 223e77a8..a2295ba7 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -42,7 +42,7 @@ set -uo pipefail CK_GATE_TABLE=' M1|boot|- M3|missing|no threads, EL0 tasks, preemption, MADT placement or typed IPC in the C kernel -SMMU|missing|no IORT/SMMUv3 service in ck.h; C NVMe DMA exists only through the QEMU-only unsafe bypass +SMMU|store|- P2_ARTIFACT|missing|no signed artifact loader (EL0, W^X, admission receipts) in the C kernel M0_ROLLBACK|missing|C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md) M4_NVME|store|- @@ -61,7 +61,7 @@ QUIET_FLAG="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" AAVMF_CODE_FD="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" AAVMF_VARS_FD="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" PHYSICAL_STATEMENT="QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." -M4_NVME_PASS_NOTE="QEMU-only unsafe DMA bypass build (make full CK_QEMU_UNSAFE_DMA=1); SMMU-confined NVMe mode NOT_RUN" +M4_NVME_PASS_NOTE="SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately" die() { echo "ck_gates: $*" >&2; exit 2; } json_str() { @@ -160,8 +160,15 @@ nvme_dma_mode() { local log="${child_log[store]:-}" if [[ "${child_present[store]:-0}" != 1 || ! -f "${log}" ]]; then echo "not run"; return; fi # The store script prints check descriptions, not serial lines; accept - # its PASS line for the bypass grant as well as the raw serial line. - if grep -qE '^PASS NVMe DMA granted through the unsafe bypass$|dma_gate: nvme granted \(UnsafeBypass\)' "${log}"; then + # its PASS lines as well as the raw serial lines. Confined is reported + # only from the confined PASS line and the negative-test PASS line. + if grep -qE '^PASS NVMe DMA granted confined$' "${log}" \ + && grep -qE '^PASS DMA outside the window faulted, page intact, controller still usable \(differs\)$' "${log}"; then + local m="Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted)" + grep -qE '^PASS NVMe DMA denied without an SMMU$' "${log}" && m+="; no-SMMU boot denied (NoSmmu)" + grep -qE '^PASS NVMe DMA granted through the TEST-ONLY bypass$' "${log}" && m+="; TEST-ONLY bypass image checked separately" + echo "${m}" + elif grep -qE '^PASS NVMe DMA granted through the unsafe bypass$|dma_gate: nvme granted \(UnsafeBypass\)' "${log}"; then if grep -qE '^PASS NVMe DMA denied without an SMMU$' "${log}"; then echo "UnsafeBypass (QEMU-only build, no SMMU confinement); default image denied (NoSmmu)" else @@ -181,7 +188,7 @@ evaluate_table() { [[ -n "${gate}" ]] || continue out="$(classify_gate "${gate}" "${src}" "${why}")" v="${out%%|*}"; r="${out#*|}" - # M4_NVME can only pass through the unsafe bypass build: say so. + # M4_NVME PASS means the SMMU-confined mode: say so. [[ "${v}" == PASS && "${gate}" == M4_NVME ]] && r="${M4_NVME_PASS_NOTE}" gate_rows+=("${gate}|${src}|${v}|${r}") if [[ "${v}" == PASS && "${gate}" == M4_NVME ]]; then @@ -310,7 +317,7 @@ self_test() { } expect_missing_all() { local g - for g in M3 SMMU P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD; do expect "${g}" NOT_RUN; done + for g in M3 P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD; do expect "${g}" NOT_RUN; done } scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT scen="$1" @@ -323,21 +330,23 @@ self_test() { # with no C implementation, which must stay NOT_RUN. scenario A "$(fake bootA 0 'PASS x' 'AIENOS_CK_M1: PASS')" \ "$(fake storeA 0 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS (narrow)' \ - 'AIENOS_CK_M3: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted through the unsafe bypass' 'PASS NVMe DMA denied without an SMMU')" - expect M1 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect_missing_all + 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_M3: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted confined' \ + 'PASS DMA outside the window faulted, page intact, controller still usable (differs)' 'PASS NVMe DMA denied without an SMMU' \ + 'PASS NVMe DMA granted through the TEST-ONLY bypass')" + expect M1 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect_missing_all count_rows - [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "4/0/8/12" && ${overall} == NOT_ALL_GATES_PASS ]] \ - && ok "A: counts 4/0/8 of 12, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" - [[ "$(nvme_dma_mode)" == "UnsafeBypass (QEMU-only build, no SMMU confinement); default image denied (NoSmmu)" ]] && ok "A: unsafe-bypass DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'" + [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "5/0/7/12" && ${overall} == NOT_ALL_GATES_PASS ]] \ + && ok "A: counts 5/0/7 of 12, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" + [[ "$(nvme_dma_mode)" == "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately" ]] && ok "A: confined DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'" grep -qxF "AIENOS_CK_M4_NVME: PASS (${M4_NVME_PASS_NOTE})" "${tmp}/A.out" \ - && ok "A: M4_NVME PASS line carries the unsafe-bypass / SMMU NOT_RUN note" || bad "A: M4_NVME PASS line lacks the note" + && ok "A: M4_NVME PASS line carries the confined-mode note" || bad "A: M4_NVME PASS line lacks the note" grep -qx 'AIENOS_CK_M3: NOT_RUN (MISSING_IMPLEMENTATION: no threads, EL0 tasks, preemption, MADT placement or typed IPC in the C kernel)' "${tmp}/A.out" \ && ok "A: missing gate prints NOT_RUN (MISSING_IMPLEMENTATION: reason)" || bad "A: M3 line wrong" [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 12 ]] && ok "A: exactly 12 verdict lines" || bad "A: verdict line count" # B: boot FAIL; store script missing -> its three gates NOT_RUN, never PASS. scenario B "$(fake bootB 1 'FAIL kernel: alive' 'AIENOS_CK_M1: FAIL')" "${tmp}/kids/does_not_exist.sh" - expect M1 FAIL; expect M4_NVME NOT_RUN; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN; expect_missing_all + expect M1 FAIL; expect M4_NVME NOT_RUN; expect M4_STORE NOT_RUN; expect ARGUS1_REVOKE NOT_RUN; expect SMMU NOT_RUN; expect_missing_all [[ "$(nvme_dma_mode)" == "not run" ]] && ok "B: dma mode 'not run' when the store script is missing" || bad "B: dma mode" # C: boot NOT_RUN (quiet flag held, exit 3); store mixed with exit 1. diff --git a/scripts/qemu_ck_store_test.sh b/scripts/qemu_ck_store_test.sh index 2adc94fe..27bb0cfc 100755 --- a/scripts/qemu_ck_store_test.sh +++ b/scripts/qemu_ck_store_test.sh @@ -6,9 +6,11 @@ # hardware: a PASS here qualifies nothing physical. # # Images (native/kernel/Makefile): -# - make full CK_QEMU_UNSAFE_DMA=1: QEMU-only build with the unconfined NVMe -# DMA bypass (the C core has no SMMU service yet); used for boots 1-4; -# - make full: the default build; NVMe DMA must be refused (fail-closed boot). +# - make full: the default build. With an SMMUv3 (QEMU iommu=smmuv3, found +# through the ACPI IORT) the NVMe stream is confined to its DMA window +# (boots 1-4); without one NVMe DMA must be refused (boot 5). +# - make full CK_QEMU_UNSAFE_DMA=1: TEST-ONLY build with the unconfined NVMe +# DMA bypass, kept so the bypass path stays honest (boot 6 only). # # Per NVMe geometry (512 B blocks like qemu_nvme_test.sh, 4 KiB blocks like # qemu_store_crash_test.sh / qemu_continuity_test.sh), on one fresh 64 MiB @@ -24,8 +26,19 @@ # (the Store superblock; same corruption as svc/tests/stage_test.c): # must be refused as proof=structural with nothing committed, and # the image sha256 must be the same before and after the boot. -# boot 5 default (safe) image on the same disk: NVMe DMA denied, no +# boot 5 default image, no SMMU, same disk: NVMe DMA denied, no # unsafe-bypass text, Store refused (no disk), image unchanged. +# boot 6 TEST-ONLY bypass image, no SMMU, same (corrupt) disk: the bypass +# warnings and grant are printed, the Store is still refused as +# structural and the image is unchanged. +# SMMU observables (boots 1-4; patterns of qemu_nvme_test.sh where the Rust +# kernel has them): "smmu: enabled", "smmu_dma_window: nvme only, translation +# active", "dma_gate: nvme granted (Confined), bus master on", absent +# "UNSAFE NVME DMA BYPASS"; C only (differs): "smmu_negative: refused dma +# outside window ... page=intact ... recovery_read=ok" (the controller is +# pointed at a page outside the window, the SMMU must report a translation +# fault for exactly that page and the page must be unchanged) and "smmu: nvme +# stream 0x.. returned to abort" after the bus master is revoked. # # Patterns copied verbatim from the Rust scripts (qemu_nvme_test.sh): # "WARNING: UNSAFE NVME DMA BYPASS BUILD", "WARNING: UNSAFE NVME DMA BYPASS @@ -45,14 +58,14 @@ # nvme=bound", "stage <name>: ok|FAIL", "store: ..." lines, "caps: ok ..." # and "argus: ok narrow_revoke=1 ...". The Rust NVME_ERROR_QEMU check (a # device-reported command error) has no C counterpart and is not run. -# The Rust Store scripts add iommu=smmuv3; this gate omits it because the C -# core has no SMMU service (hence the QEMU-only bypass image). +# Boots 1-4 add iommu=smmuv3 to the machine like the Rust Store scripts. # # Takes the machine quiet flag itself like qemu_ck_boot_test.sh # (AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG; released at most once, only if it # still holds exactly this run's text). Any QEMU exit status other than 0 fails. # Final lines: AIENOS_CK_M4_NVME, AIENOS_CK_M4_STORE, AIENOS_CK_ARGUS1_REVOKE, -# each PASS|FAIL|NOT_RUN. A boot that fails an M1 check fails all three. +# AIENOS_CK_SMMU, each PASS|FAIL|NOT_RUN. A boot that fails an M1 check fails +# all four. # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). set -euo pipefail @@ -63,6 +76,7 @@ verdicts() { # one value for all three echo "AIENOS_CK_M4_NVME: $1" echo "AIENOS_CK_M4_STORE: $1" echo "AIENOS_CK_ARGUS1_REVOKE: $1" + echo "AIENOS_CK_SMMU: $1" } code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" @@ -103,14 +117,15 @@ trap cleanup EXIT # shellcheck source=scripts/lib_ck_m1_checks.sh source "${repo_root}/scripts/lib_ck_m1_checks.sh" img_bytes=67108864 -m1_fail=0; nvme_fail=0; store_fail=0; argus_fail=0 +m1_fail=0; nvme_fail=0; store_fail=0; argus_fail=0; smmu_fail=0 fail_into() { # bucket variable name: move the current failed flag into it if [[ "${failed}" != 0 ]]; then printf -v "$1" 1; fi failed=0 } sha() { sha256sum "$1" | cut -d' ' -f1; } -# boot <name> <efi> <disk image> <block bytes> <serial>: one QEMU boot; leaves +# boot <name> <efi> <disk image> <block bytes> <serial> [smmu]: one QEMU boot +# (with "smmu" the machine gets an SMMUv3); leaves # the serial text in ${work}/serial.txt and the exit status in qemu_status. boot() { work="${top}/$1" @@ -120,10 +135,12 @@ boot() { cp "${vars_fd}" "${work}/vars.fd" local nvme_dev="nvme,drive=nvme0,serial=$5" [[ "$4" == 512 ]] || nvme_dev="${nvme_dev},logical_block_size=$4,physical_block_size=$4" + local machine="virt,virtualization=on,gic-version=3" + [[ "${6:-}" != smmu ]] || machine+=",iommu=smmuv3" set +e # Issue #61: single-threaded TCG (see qemu_boot_test.sh). timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \ - -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -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" \ @@ -147,11 +164,24 @@ field() { line=$(grep -E "$1" "${work}/serial.txt" | tail -1 || true) [[ "${line}" =~ $1 ]] && echo "${BASH_REMATCH[1]}" || echo "" } -nvme_checks() { # block bytes: the bound NVMe path of the unsafe image +nvme_checks() { # block bytes, confined|bypass: the bound NVMe path local bs="$1" count=$(( img_bytes / $1 )) - check "unsafe bypass build announced on serial" "WARNING: UNSAFE NVME DMA BYPASS BUILD" - check "unsafe bypass grant announced on serial" "WARNING: UNSAFE NVME DMA BYPASS ACTIVE" - check "NVMe DMA granted through the unsafe bypass" "dma_gate: nvme granted (UnsafeBypass)" + if [[ "$2" == confined ]]; then + check "SMMUv3 enabled from the IORT" "smmu: enabled" + check "NVMe DMA window mapped through the SMMU" "smmu_dma_window: nvme only, translation active" + check "NVMe DMA granted confined" "dma_gate: nvme granted (Confined), bus master on" + check_absent "no unsafe DMA bypass in this image" "UNSAFE NVME DMA BYPASS" + check_absent "no unconfined grant" "dma_gate: nvme granted (UnsafeBypass)" + check "DMA outside the window faulted, page intact, controller still usable (differs)" \ + "smmu_negative: refused dma outside window iova=0x[0-9a-f]* faults=[1-9][0-9]* type=0x10 sid=0x[0-9a-f]* addr=0x[0-9a-f]* page=intact cmd_rc=-\?[0-9]* recovery_read=ok$" + check "NVMe stream returned to abort after the revoke (differs)" "smmu: nvme stream 0x[0-9a-f]* returned to abort (rc=0)" + fail_into smmu_fail + else + check "TEST-ONLY bypass build announced on serial" "WARNING: UNSAFE NVME DMA BYPASS BUILD" + check "TEST-ONLY bypass grant announced on serial" "WARNING: UNSAFE NVME DMA BYPASS ACTIVE" + check "NVMe DMA granted through the TEST-ONLY bypass" "dma_gate: nvme granted (UnsafeBypass)" + check_absent "no SMMU window claimed by the bypass" "smmu_dma_window:" + fi check "ECAM discovery found the NVMe class device (differs)" "nvme: discovery " check "controller and namespace identify completed (differs)" "nvme: identify ok " check "namespace geometry matches the image (differs)" \ @@ -182,8 +212,8 @@ for bs in 512 4096; do truncate -s "${img_bytes}" "${image}" prev_gen="" for k in 1 2 3; do - boot "${bs}-boot${k}" "${unsafe_efi}" "${image}" "${bs}" "${serial}" - nvme_checks "${bs}" + boot "${bs}-boot${k}" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu + nvme_checks "${bs}" confined argus_checks check "stage store ok (differs)" "stage store: ok" check_absent "Store not refused (differs)" "store: REFUSED" @@ -244,8 +274,8 @@ for bs in 512 4096; do [[ "${sha_corrupt}" != "${sha_clean}" && $(stat -c %s "${image}") == "${img_bytes}" ]] \ && echo "PASS host corrupted the Store superblock (8192 bytes at ${soff})" \ || store_fail_msg "host corruption did not change the image" - boot "${bs}-boot4-corrupt" "${unsafe_efi}" "${image}" "${bs}" "${serial}" - nvme_checks "${bs}" + boot "${bs}-boot4-corrupt" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu + nvme_checks "${bs}" confined argus_checks check "corrupt Store refused as structural (differs)" "store: REFUSED proof=structural " check "corrupt Store left as found, not reformatted (differs)" "disk left as found, not reformatted" @@ -273,18 +303,32 @@ for bs in 512 4096; do sha_safe=$(sha "${image}") [[ "${sha_safe}" == "${sha_corrupt}" ]] && echo "PASS image sha256 unchanged by the fail-closed boot" \ || store_fail_msg "image changed by the fail-closed boot" + + # Boot 6: TEST-ONLY bypass image, no SMMU. Kept so the bypass path stays + # honest: it must announce itself, and the corrupt Store is still refused. + boot "${bs}-boot6-bypass" "${unsafe_efi}" "${image}" "${bs}" "${serial}" + nvme_checks "${bs}" bypass + argus_checks + check "corrupt Store refused under the bypass image too (differs)" "store: REFUSED proof=structural " + check_absent "nothing committed under the bypass image (differs)" "store: committed" + fail_into store_fail + sha_bypass=$(sha "${image}") + [[ "${sha_bypass}" == "${sha_corrupt}" ]] && echo "PASS image sha256 unchanged by the bypass boot" \ + || store_fail_msg "image changed by the bypass boot" done release_flag -if [[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}" != 0000 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then +if [[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}" != 00000 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then for s in "${top}"/*/serial.txt; do echo "---- serial console $(basename "$(dirname "${s}")") (stage lines) ----" - grep -E '^(stage |nvme:|dma_gate:|WARNING|devices:|store:|caps:|argus:|report_kind:)' "${s}" | head -60 || true + grep -E '^(stage |smmu|nvme:|dma_gate:|WARNING|devices:|store:|caps:|argus:|report_kind:)' "${s}" | head -60 || true done fi [[ "${m1_fail}" != 0 ]] && echo "M1 checks failed on at least one boot: no verdict can pass" v() { [[ "${m1_fail}" == 0 && "$1" == 0 ]] && echo PASS || echo FAIL; } -echo "AIENOS_CK_M4_NVME: $(v "${nvme_fail}")" +# Confined mode is part of the NVMe gate: an SMMU failure fails it too. +echo "AIENOS_CK_M4_NVME: $(v "$(( nvme_fail | smmu_fail ))")" echo "AIENOS_CK_M4_STORE: $(v "${store_fail}")" echo "AIENOS_CK_ARGUS1_REVOKE: $(v "${argus_fail}")" -[[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}" == 0000 ]] || exit 1 +echo "AIENOS_CK_SMMU: $(v "${smmu_fail}")" +[[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}" == 00000 ]] || exit 1